Browse Source

Mange Left Over UBO for vertex and fragment

Sebastien Vandenberghe 6 years ago
parent
commit
0f4ec70610

+ 1 - 0
src/Engines/Processors/iShaderProcessor.ts

@@ -11,4 +11,5 @@ export interface IShaderProcessor {
     lineProcessor?: (line: string, isFragment: boolean, processingContext: Nullable<ShaderProcessingContext>) => string;
     preProcessor?: (code: string, defines: string[], isFragment: boolean, processingContext: Nullable<ShaderProcessingContext>) => string;
     postProcessor?: (code: string, defines: string[], isFragment: boolean, processingContext: Nullable<ShaderProcessingContext>) => string;
+    finalizeShaders?: (vertexCode: string, fragmentCode: string, processingContext: Nullable<ShaderProcessingContext>) => { vertexCode: string, fragmentCode: string };
 }

+ 8 - 0
src/Engines/Processors/shaderProcessor.ts

@@ -19,6 +19,14 @@ export class ShaderProcessor {
         });
     }
 
+    public static Finalize(vertexCode: string, fragmentCode: string, options: ProcessingOptions): { vertexCode: string, fragmentCode: string } {
+        if (!options.processor || !options.processor.finalizeShaders) {
+            return { vertexCode, fragmentCode };
+        }
+
+        return options.processor.finalizeShaders(vertexCode, fragmentCode, options.processingContext);
+    }
+
     private static _ProcessPrecision(source: string, options: ProcessingOptions): string {
         const shouldUseHighPrecisionShader = options.shouldUseHighPrecisionShader;
 

+ 8 - 2
src/Engines/WebGPU/webgpuShaderProcessors.ts

@@ -198,6 +198,10 @@ export class WebGPUShaderProcessor implements IShaderProcessor {
             code += "gl_Position.y *= -1.; }";
         }
 
+        return code;
+    }
+
+    public finalizeShaders(vertexCode: string, fragmentCode: string, processingContext: Nullable<ShaderProcessingContext>): { vertexCode: string, fragmentCode: string } {
         // Builds the leftover UBOs.
         const webgpuProcessingContext = processingContext! as WebGPUShaderProcessingContext;
         if (webgpuProcessingContext.leftOverUniforms.length) {
@@ -218,9 +222,11 @@ export class WebGPUShaderProcessor implements IShaderProcessor {
             }
             ubo += "};\n\n";
 
-            code = ubo + code;
+            // Currently set in both vert and frag but could be optim away if necessary.
+            vertexCode = ubo + vertexCode;
+            fragmentCode = ubo + fragmentCode;
         }
 
-        return code;
+        return { vertexCode, fragmentCode };
     }
 }

+ 2 - 1
src/Materials/effect.ts

@@ -396,7 +396,8 @@ export class Effect implements IDisposable {
                 ShaderProcessor.Process(vertexCode, processorOptions, (migratedVertexCode) => {
                     processorOptions.isFragment = true;
                     ShaderProcessor.Process(fragmentCode, processorOptions, (migratedFragmentCode) => {
-                        this._useFinalCode(migratedVertexCode, migratedFragmentCode, baseName);
+                        const finalShaders = ShaderProcessor.Finalize(migratedVertexCode, migratedFragmentCode, processorOptions);
+                        this._useFinalCode(finalShaders.vertexCode, finalShaders.fragmentCode, baseName);
                     });
                 });
             });