Skip to content
Merged
Changes from all 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
23 changes: 14 additions & 9 deletions src/platform/graphics/webgpu/webgpu-shader-processor-wgsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class WebgpuShaderProcessorWGSL {
const resourcesData = WebgpuShaderProcessorWGSL.processResources(device, parsedResources, shaderDefinition.processingOptions, shader);

// generate fragment output struct
const fOutput = WebgpuShaderProcessorWGSL.generateFragmentOutputStruct(device.maxColorAttachments);
const fOutput = WebgpuShaderProcessorWGSL.generateFragmentOutputStruct(fragmentExtracted.src, device.maxColorAttachments);

// VS - insert the blocks to the source
const vBlock = `${attributesBlock}\n${vertexVaryingsBlock}\n${uniformsData.code}\n${resourcesData.code}\n`;
Expand Down Expand Up @@ -400,9 +400,9 @@ class WebgpuShaderProcessorWGSL {
meshUniforms.push(new UniformFormat(UNUSED_UNIFORM_NAME, UNIFORMTYPE_FLOAT));
}

const meshUniformBufferFormat = meshUniforms.length ? new UniformBufferFormat(device, meshUniforms) : null;
const meshUniformBufferFormat = new UniformBufferFormat(device, meshUniforms);

// generate code for uniform buffers
// generate code for uniform buffers, starts on the slot 0
let code = '';
processingOptions.uniformFormats.forEach((format, bindGroupIndex) => {
if (format) {
Expand Down Expand Up @@ -484,12 +484,12 @@ class WebgpuShaderProcessorWGSL {
let code = '';
processingOptions.bindGroupFormats.forEach((format, bindGroupIndex) => {
if (format) {
code += WebgpuShaderProcessorWGSL.getTextureShaderDeclaration(format, bindGroupIndex);
code += WebgpuShaderProcessorWGSL.getTextureShaderDeclaration(format, bindGroupIndex, 1);
}
});

// and also for generated mesh format
code += WebgpuShaderProcessorWGSL.getTextureShaderDeclaration(meshBindGroupFormat, BINDGROUP_MESH);
code += WebgpuShaderProcessorWGSL.getTextureShaderDeclaration(meshBindGroupFormat, BINDGROUP_MESH, 0);

return {
code,
Expand Down Expand Up @@ -537,11 +537,12 @@ class WebgpuShaderProcessorWGSL {
* ```
* @param {BindGroupFormat} format - The format of the bind group.
* @param {number} bindGroup - The bind group index.
* @param {number} startBindIndex - The starting bind index.
* @returns {string} - The shader code for the bind group.
*/
static getTextureShaderDeclaration(format, bindGroup) {
static getTextureShaderDeclaration(format, bindGroup, startBindIndex) {
let code = '';
let bindIndex = 0;
let bindIndex = startBindIndex;

format.textureFormats.forEach((format) => {

Expand Down Expand Up @@ -606,14 +607,18 @@ class WebgpuShaderProcessorWGSL {
return `struct ${structName} {\n${block}};\n`;
}

static generateFragmentOutputStruct(numRenderTargets) {
static generateFragmentOutputStruct(src, numRenderTargets) {
let structCode = 'struct FragmentOutput {\n';

for (let i = 0; i < numRenderTargets; i++) {
structCode += ` @location(${i}) color${i > 0 ? i : ''} : vec4f,\n`;
}

structCode += ' @builtin(frag_depth) fragDepth : f32\n';
// find if the src contains `.fragDepth =`, ignoring whitespace before = sign
const needsFragDepth = src.search(/\.fragDepth\s*=/) !== -1;
if (needsFragDepth) {
structCode += ' @builtin(frag_depth) fragDepth : f32\n';
}

return `${structCode}};\n`;
}
Expand Down