Browse Source

Bind resources only where they are used, not always to both vertex and fragment shaders

Popov72 4 years ago
parent
commit
86ab13b6d5

+ 2 - 0
src/Engines/WebGPU/webgpuShaderProcessingContext.ts

@@ -28,6 +28,8 @@ export interface WebGPUTextureSamplerBindingDescription {
 */
 export interface WebGPUBindingDescription {
     name: string;
+    usedInVertex: boolean;
+    usedInFragment: boolean;
 
     isSampler: boolean;
     isComparisonSampler?: boolean;

+ 43 - 17
src/Engines/WebGPU/webgpuShaderProcessors.ts

@@ -207,12 +207,22 @@ export class WebGPUShaderProcessor implements IShaderProcessor {
                 if (!webgpuProcessingContext.orderedUBOsAndSamplers[samplerSetIndex]) {
                     webgpuProcessingContext.orderedUBOsAndSamplers[samplerSetIndex] = [];
                 }
-                webgpuProcessingContext.orderedUBOsAndSamplers[samplerSetIndex][samplerBindingIndex] = {
-                    isSampler: true,
-                    isTexture: false,
-                    isComparisonSampler,
-                    name,
-                };
+                if (!webgpuProcessingContext.orderedUBOsAndSamplers[samplerSetIndex][samplerBindingIndex]) {
+                    webgpuProcessingContext.orderedUBOsAndSamplers[samplerSetIndex][samplerBindingIndex] = {
+                        isSampler: true,
+                        isTexture: false,
+                        isComparisonSampler,
+                        usedInVertex: false,
+                        usedInFragment: false,
+                        name,
+                    };
+                }
+
+                if (isFragment) {
+                    webgpuProcessingContext.orderedUBOsAndSamplers[samplerSetIndex][samplerBindingIndex].usedInFragment = true;
+                } else {
+                    webgpuProcessingContext.orderedUBOsAndSamplers[samplerSetIndex][samplerBindingIndex].usedInVertex = true;
+                }
 
                 for (let i = 0; i < arraySize; ++i) {
                     const textureSetIndex = samplerInfo.textures[i].setIndex;
@@ -221,15 +231,24 @@ export class WebGPUShaderProcessor implements IShaderProcessor {
                     if (!webgpuProcessingContext.orderedUBOsAndSamplers[textureSetIndex]) {
                         webgpuProcessingContext.orderedUBOsAndSamplers[textureSetIndex] = [];
                     }
-                    webgpuProcessingContext.orderedUBOsAndSamplers[textureSetIndex][textureBindingIndex] = {
-                        isSampler: false,
-                        isTexture: true,
-                        componentType: isComparisonSampler ? WebGPUConstants.TextureComponentType.DepthComparison :
-                                     componentType === 'u' ? WebGPUConstants.TextureComponentType.Uint :
-                                     componentType === 'i' ? WebGPUConstants.TextureComponentType.Sint : WebGPUConstants.TextureComponentType.Float,
-                        textureDimension,
-                        name: isTextureArray ? name + i.toString() : name,
-                    };
+                    if (!webgpuProcessingContext.orderedUBOsAndSamplers[textureSetIndex][textureBindingIndex]) {
+                        webgpuProcessingContext.orderedUBOsAndSamplers[textureSetIndex][textureBindingIndex] = {
+                            isSampler: false,
+                            isTexture: true,
+                            componentType: isComparisonSampler ? WebGPUConstants.TextureComponentType.DepthComparison :
+                                        componentType === 'u' ? WebGPUConstants.TextureComponentType.Uint :
+                                        componentType === 'i' ? WebGPUConstants.TextureComponentType.Sint : WebGPUConstants.TextureComponentType.Float,
+                            textureDimension,
+                            usedInVertex: false,
+                            usedInFragment: false,
+                            name: isTextureArray ? name + i.toString() : name,
+                        };
+                    }
+                    if (isFragment) {
+                        webgpuProcessingContext.orderedUBOsAndSamplers[textureSetIndex][textureBindingIndex].usedInFragment = true;
+                    } else {
+                        webgpuProcessingContext.orderedUBOsAndSamplers[textureSetIndex][textureBindingIndex].usedInVertex = true;
+                    }
                 }
             }
             else {
@@ -293,7 +312,14 @@ export class WebGPUShaderProcessor implements IShaderProcessor {
             if (!webgpuProcessingContext.orderedUBOsAndSamplers[setIndex]) {
                 webgpuProcessingContext.orderedUBOsAndSamplers[setIndex] = [];
             }
-            webgpuProcessingContext.orderedUBOsAndSamplers[setIndex][bindingIndex] = { isSampler: false, isTexture: false, name };
+            if (!webgpuProcessingContext.orderedUBOsAndSamplers[setIndex][bindingIndex]) {
+                webgpuProcessingContext.orderedUBOsAndSamplers[setIndex][bindingIndex] = { isSampler: false, isTexture: false, name, usedInVertex: false, usedInFragment: false };
+            }
+            if (isFragment) {
+                webgpuProcessingContext.orderedUBOsAndSamplers[setIndex][bindingIndex].usedInFragment = true;
+            } else {
+                webgpuProcessingContext.orderedUBOsAndSamplers[setIndex][bindingIndex].usedInVertex = true;
+            }
 
             uniformBuffer = uniformBuffer.replace("uniform", `layout(set = ${setIndex}, binding = ${bindingIndex}) uniform`);
         }
@@ -392,7 +418,7 @@ export class WebGPUShaderProcessor implements IShaderProcessor {
                 if (!webgpuProcessingContext.orderedUBOsAndSamplers[availableUBO.setIndex]) {
                     webgpuProcessingContext.orderedUBOsAndSamplers[availableUBO.setIndex] = [];
                 }
-                webgpuProcessingContext.orderedUBOsAndSamplers[availableUBO.setIndex][availableUBO.bindingIndex] = { isSampler: false, isTexture: false, name };
+                webgpuProcessingContext.orderedUBOsAndSamplers[availableUBO.setIndex][availableUBO.bindingIndex] = { isSampler: false, isTexture: false, usedInVertex: true, usedInFragment: true, name };
             }
 
             let ubo = `layout(set = ${availableUBO.setIndex}, binding = ${availableUBO.bindingIndex}) uniform ${name} {\n    `;

+ 12 - 4
src/Engines/webgpuEngine.ts

@@ -3292,20 +3292,28 @@ export class WebGPUEngine extends Engine {
                     continue;
                 }
 
+                let visibility = 0;
+                if (bindingDefinition.usedInVertex) {
+                    visibility = visibility | WebGPUConstants.ShaderStage.Vertex;
+                }
+                if (bindingDefinition.usedInFragment) {
+                    visibility = visibility | WebGPUConstants.ShaderStage.Fragment;
+                }
+
                 // TODO WEBGPU. Optimize shared samplers visibility for vertex/framgent.
                 if (bindingDefinition.isSampler) {
                     entries.push({
                         binding: j,
-                        visibility: WebGPUConstants.ShaderStage.Vertex | WebGPUConstants.ShaderStage.Fragment,
+                        visibility,
                         type: bindingDefinition.isComparisonSampler ? WebGPUConstants.BindingType.ComparisonSampler : WebGPUConstants.BindingType.Sampler
                     });
                 } else if (bindingDefinition.isTexture) {
                     entries.push({
                         binding: j,
-                        visibility: WebGPUConstants.ShaderStage.Vertex | WebGPUConstants.ShaderStage.Fragment,
+                        visibility,
                         type: WebGPUConstants.BindingType.SampledTexture,
                         viewDimension: bindingDefinition.textureDimension,
-                        //textureComponentType: bindingDefinition.componentType,
+                        textureComponentType: bindingDefinition.componentType,
                         // TODO WEBGPU.
                         // hasDynamicOffset?: boolean;
                         // storageTextureFormat?: GPUTextureFormat;
@@ -3314,7 +3322,7 @@ export class WebGPUEngine extends Engine {
                 } else {
                     entries.push({
                         binding: j,
-                        visibility: WebGPUConstants.ShaderStage.Vertex | WebGPUConstants.ShaderStage.Fragment,
+                        visibility,
                         type: WebGPUConstants.BindingType.UniformBuffer,
                     });
                 }