Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebGPU Update #12312

Merged
merged 15 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions packages/dev/core/src/Engines/WebGPU/webgpuShaderProcessorsWGSL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export class WebGPUShaderProcessorWGSL extends WebGPUShaderProcessor {
protected _varyingsWGSL: string[];
protected _varyingsDeclWGSL: string[];
protected _varyingNamesWGSL: string[];
protected _stridedUniformArrays: string[];

public shaderLanguage = ShaderLanguage.WGSL;
public uniformRegexp = /uniform\s+(\w+)\s*:\s*(.+)\s*;/;
Expand Down Expand Up @@ -104,11 +105,12 @@ export class WebGPUShaderProcessorWGSL extends WebGPUShaderProcessor {
this._varyingsWGSL = [];
this._varyingsDeclWGSL = [];
this._varyingNamesWGSL = [];
this._stridedUniformArrays = [];
}

public preProcessShaderCode(code: string): string {
return (
`struct ${WebGPUShaderProcessor.InternalsUBOName} {\nyFactor__: f32;\ntextureOutputHeight__: f32;\n};\nvar<uniform> ${internalsVarName} : ${WebGPUShaderProcessor.InternalsUBOName};\n` +
`struct ${WebGPUShaderProcessor.InternalsUBOName} {\nyFactor__: f32,\ntextureOutputHeight__: f32,\n};\nvar<uniform> ${internalsVarName} : ${WebGPUShaderProcessor.InternalsUBOName};\n` +
RemoveComments(code)
);
}
Expand All @@ -128,7 +130,7 @@ export class WebGPUShaderProcessorWGSL extends WebGPUShaderProcessor {
} else {
location = this._webgpuProcessingContext.getVaryingNextLocation(varyingType, this._getArraySize(name, varyingType, preProcessors)[2]);
this._webgpuProcessingContext.availableVaryings[name] = location;
this._varyingsWGSL.push(`@location(${location}) ${name} : ${varyingType};`);
this._varyingsWGSL.push(`@location(${location}) ${name} : ${varyingType},`);
this._varyingsDeclWGSL.push(`var<private> ${name} : ${varyingType};`);
this._varyingNamesWGSL.push(name);
}
Expand All @@ -149,7 +151,7 @@ export class WebGPUShaderProcessorWGSL extends WebGPUShaderProcessor {
this._webgpuProcessingContext.availableAttributes[name] = location;
this._webgpuProcessingContext.orderedAttributes[location] = name;

this._attributesWGSL.push(`@location(${location}) ${name} : ${attributeType};`);
this._attributesWGSL.push(`@location(${location}) ${name} : ${attributeType},`);
this._attributesDeclWGSL.push(`var<private> ${name} : ${attributeType};`);
this._attributeNamesWGSL.push(name);
attribute = "";
Expand Down Expand Up @@ -260,20 +262,21 @@ export class WebGPUShaderProcessorWGSL extends WebGPUShaderProcessor {

// Vertex code
vertexCode = vertexCode.replace(/#define /g, "//#define ");
vertexCode = this._processStridedUniformArrays(vertexCode);

const varyingsDecl = this._varyingsDeclWGSL.join("\n") + "\n";

const vertexBuiltinDecl = `var<private> ${builtInName_vertex_index} : u32;\nvar<private> ${builtInName_instance_index} : u32;\nvar<private> ${builtInName_position} : vec4<f32>;\n`;

const vertexAttributesDecl = this._attributesDeclWGSL.join("\n") + "\n";

let vertexInputs = "struct VertexInputs {\n @builtin(vertex_index) vertexIndex : u32;\n @builtin(instance_index) instanceIndex : u32;\n";
let vertexInputs = "struct VertexInputs {\n @builtin(vertex_index) vertexIndex : u32,\n @builtin(instance_index) instanceIndex : u32,\n";
if (this._attributesWGSL.length > 0) {
vertexInputs += this._attributesWGSL.join("\n");
}
vertexInputs += "\n};\n";

let vertexFragmentInputs = "struct FragmentInputs {\n @builtin(position) position : vec4<f32>;\n";
let vertexFragmentInputs = "struct FragmentInputs {\n @builtin(position) position : vec4<f32>,\n";
if (this._varyingsWGSL.length > 0) {
vertexFragmentInputs += this._varyingsWGSL.join("\n");
}
Expand Down Expand Up @@ -301,17 +304,18 @@ export class WebGPUShaderProcessorWGSL extends WebGPUShaderProcessor {

// fragment code
fragmentCode = fragmentCode.replace(/#define /g, "//#define ");
fragmentCode = this._processStridedUniformArrays(fragmentCode);
fragmentCode = fragmentCode.replace(/dpdy/g, "(-internals.yFactor__)*dpdy"); // will also handle dpdyCoarse and dpdyFine

const fragmentBuiltinDecl = `var<private> ${builtInName_position_frag} : vec4<f32>;\nvar<private> ${builtInName_front_facing} : bool;\nvar<private> ${builtInName_FragColor} : vec4<f32>;\nvar<private> ${builtInName_frag_depth} : f32;\n`;

let fragmentFragmentInputs = "struct FragmentInputs {\n @builtin(position) position : vec4<f32>;\n @builtin(front_facing) frontFacing : bool;\n";
let fragmentFragmentInputs = "struct FragmentInputs {\n @builtin(position) position : vec4<f32>,\n @builtin(front_facing) frontFacing : bool,\n";
if (this._varyingsWGSL.length > 0) {
fragmentFragmentInputs += this._varyingsWGSL.join("\n");
}
fragmentFragmentInputs += "\n};\n";

let fragmentOutputs = "struct FragmentOutputs {\n @location(0) color : vec4<f32>;\n";
let fragmentOutputs = "struct FragmentOutputs {\n @location(0) color : vec4<f32>,\n";

let hasFragDepth = false;
let idx = 0;
Expand Down Expand Up @@ -365,23 +369,31 @@ export class WebGPUShaderProcessorWGSL extends WebGPUShaderProcessor {
}

protected _generateLeftOverUBOCode(name: string, uniformBufferDescription: WebGPUBufferDescription): string {
let stridedArrays = "";
let ubo = `struct ${name} {\n`;
for (const leftOverUniform of this._webgpuProcessingContext.leftOverUniforms) {
const type = leftOverUniform.type.replace(/^(.*?)(<.*>)?$/, "$1");
const size = WebGPUShaderProcessor.UniformSizes[type];

if (leftOverUniform.length > 0) {
if (size <= 2) {
ubo += ` @align(16) ${leftOverUniform.name} : @stride(16) array<${leftOverUniform.type}, ${leftOverUniform.length}>;\n`;
const stridedArrayType = `${name}_${this._stridedUniformArrays.length}_strided_arr`;
stridedArrays += `struct ${stridedArrayType} {
@size(16)
el: ${type},
}`;
this._stridedUniformArrays.push(leftOverUniform.name);

ubo += ` @align(16) ${leftOverUniform.name} : array<${stridedArrayType}, ${leftOverUniform.length}>,\n`;
} else {
ubo += ` ${leftOverUniform.name} : array<${leftOverUniform.type}, ${leftOverUniform.length}>;\n`;
ubo += ` ${leftOverUniform.name} : array<${leftOverUniform.type}, ${leftOverUniform.length}>,\n`;
}
} else {
ubo += ` ${leftOverUniform.name} : ${leftOverUniform.type};\n`;
ubo += ` ${leftOverUniform.name} : ${leftOverUniform.type},\n`;
}
}
ubo += "};\n";

ubo = `${stridedArrays}\n${ubo}`;
ubo += `@group(${uniformBufferDescription.binding.groupIndex}) @binding(${uniformBufferDescription.binding.bindingIndex}) var<uniform> ${leftOverVarName} : ${name};\n`;

return ubo;
Expand Down Expand Up @@ -494,4 +506,11 @@ export class WebGPUShaderProcessorWGSL extends WebGPUShaderProcessor {

return code;
}

private _processStridedUniformArrays(code: string): string {
for (const uniformArrayName of this._stridedUniformArrays) {
code = code.replace(new RegExp(`${uniformArrayName}\\s*\\[(.*)\\]`, "g"), `${uniformArrayName}[$1].el`);
}
return code;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
struct Mesh {
world : mat4x4<f32>;
visibility : f32;
world : mat4x4<f32>,
visibility : f32,
};

var<uniform> mesh : Mesh;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
struct Scene {
viewProjection : mat4x4<f32>;
viewProjection : mat4x4<f32>,
#ifdef MULTIVIEW
viewProjectionR : mat4x4<f32>;
viewProjectionR : mat4x4<f32>,
#endif
view : mat4x4<f32>;
projection : mat4x4<f32>;
vEyePosition : vec4<f32>;
view : mat4x4<f32>,
projection : mat4x4<f32>,
vEyePosition : vec4<f32>,
};

var<uniform> scene : Scene;
122 changes: 61 additions & 61 deletions packages/dev/core/src/ShadersWGSL/gpuUpdateParticles.compute.fx
Original file line number Diff line number Diff line change
@@ -1,126 +1,126 @@
struct Particle {
position : vec3<f32>;
age : f32;
size : vec3<f32>;
life : f32;
seed : vec4<f32>;
direction : vec3<f32>;
dummy0: f32;
position : vec3<f32>,
age : f32,
size : vec3<f32>,
life : f32,
seed : vec4<f32>,
direction : vec3<f32>,
dummy0: f32,

#ifdef CUSTOMEMITTER
initialPosition : vec3<f32>;
dummy1: f32;
initialPosition : vec3<f32>,
dummy1: f32,
#endif
#ifndef COLORGRADIENTS
color : vec4<f32>;
color : vec4<f32>,
#endif
#ifndef BILLBOARD
initialDirection : vec3<f32>;
dummy2: f32;
initialDirection : vec3<f32>,
dummy2: f32,
#endif
#ifdef NOISE
noiseCoordinates1 : vec3<f32>;
dummy3: f32;
noiseCoordinates2 : vec3<f32>;
dummy4: f32;
noiseCoordinates1 : vec3<f32>,
dummy3: f32,
noiseCoordinates2 : vec3<f32>,
dummy4: f32,
#endif
#ifdef ANGULARSPEEDGRADIENTS
angle : f32;
angle : f32,
#else
angle : vec2<f32>;
angle : vec2<f32>,
#endif
#ifdef ANIMATESHEET
cellIndex : f32;
cellIndex : f32,
#ifdef ANIMATESHEETRANDOMSTART
cellStartOffset : f32;
cellStartOffset : f32,
#endif
#endif
};
struct Particles {
particles : array<Particle>;
particles : array<Particle>,
};

struct SimParams {
currentCount : f32;
timeDelta : f32;
stopFactor : f32;
randomTextureSize: i32;
lifeTime : vec2<f32>;
emitPower : vec2<f32>;
currentCount : f32,
timeDelta : f32,
stopFactor : f32,
randomTextureSize: i32,
lifeTime : vec2<f32>,
emitPower : vec2<f32>,

#ifndef COLORGRADIENTS
color1 : vec4<f32>;
color2 : vec4<f32>;
color1 : vec4<f32>,
color2 : vec4<f32>,
#endif

sizeRange : vec2<f32>;
scaleRange : vec4<f32>;
angleRange : vec4<f32>;
gravity : vec3<f32>;
sizeRange : vec2<f32>,
scaleRange : vec4<f32>,
angleRange : vec4<f32>,
gravity : vec3<f32>,

#ifdef LIMITVELOCITYGRADIENTS
limitVelocityDamping : f32;
limitVelocityDamping : f32,
#endif

#ifdef ANIMATESHEET
cellInfos : vec4<f32>;
cellInfos : vec4<f32>,
#endif

#ifdef NOISE
noiseStrength : vec3<f32>;
noiseStrength : vec3<f32>,
#endif

#ifndef LOCAL
emitterWM : mat4x4<f32>;
emitterWM : mat4x4<f32>,
#endif

// Emitter types

#ifdef BOXEMITTER
direction1 : vec3<f32>;
direction2 : vec3<f32>;
minEmitBox : vec3<f32>;
maxEmitBox : vec3<f32>;
direction1 : vec3<f32>,
direction2 : vec3<f32>,
minEmitBox : vec3<f32>,
maxEmitBox : vec3<f32>,
#endif

#ifdef CONEEMITTER
radius : vec2<f32>;
coneAngle : f32;
height : vec2<f32>;
directionRandomizer : f32;
radius : vec2<f32>,
coneAngle : f32,
height : vec2<f32>,
directionRandomizer : f32,
#endif

#ifdef CYLINDEREMITTER
radius : f32;
height : f32;
radiusRange : f32;
radius : f32,
height : f32,
radiusRange : f32,
#ifdef DIRECTEDCYLINDEREMITTER
direction1 : vec3<f32>;
direction2 : vec3<f32>;
direction1 : vec3<f32>,
direction2 : vec3<f32>,
#else
directionRandomizer : f32;
directionRandomizer : f32,
#endif
#endif

#ifdef HEMISPHERICEMITTER
radius : f32;
radiusRange : f32;
directionRandomizer : f32;
radius : f32,
radiusRange : f32,
directionRandomizer : f32,
#endif

#ifdef POINTEMITTER
direction1 : vec3<f32>;
direction2 : vec3<f32>;
direction1 : vec3<f32>,
direction2 : vec3<f32>,
#endif

#ifdef SPHEREEMITTER
radius : f32;
radiusRange : f32;
radius : f32,
radiusRange : f32,
#ifdef DIRECTEDSPHEREEMITTER
direction1 : vec3<f32>;
direction2 : vec3<f32>;
direction1 : vec3<f32>,
direction2 : vec3<f32>,
#else
directionRandomizer : f32;
directionRandomizer : f32,
#endif
#endif
};
Expand Down
2 changes: 1 addition & 1 deletion packages/tools/babylonServer/public/twgsl/twgsl.js

Large diffs are not rendered by default.

Binary file modified packages/tools/babylonServer/public/twgsl/twgsl.wasm
Binary file not shown.