فهرست منبع

Varying Auto Update

Sebastien Vandenberghe 6 سال پیش
والد
کامیت
dd5cc2d712

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

@@ -4,7 +4,7 @@ import { ShaderProcessingContext } from "./shaderProcessingOptions";
 /** @hidden */
 export interface IShaderProcessor {
     attributeProcessor?: (attribute: string, processingContext: Nullable<ShaderProcessingContext>) => string;
-    varyingProcessor?: (varying: string, isFragment: boolean) => string;
+    varyingProcessor?: (varying: string, isFragment: boolean, processingContext: Nullable<ShaderProcessingContext>) => string;
     uniformProcessor?: (uniform: string, isFragment: boolean) => string;
     uniformBufferProcessor?: (uniformBuffer: string, isFragment: boolean) => string;
     endOfUniformBufferProcessor?: (closingBracketLine: string, isFragment: boolean) => string;

+ 1 - 1
src/Engines/Processors/shaderCodeNode.ts

@@ -21,7 +21,7 @@ export class ShaderCodeNode {
                 if (processor.attributeProcessor && StringTools.StartsWith(this.line, "attribute")) {
                     value = processor.attributeProcessor(this.line, options.processingContext);
                 } else if (processor.varyingProcessor && StringTools.StartsWith(this.line, "varying")) {
-                    value = processor.varyingProcessor(this.line, options.isFragment);
+                    value = processor.varyingProcessor(this.line, options.isFragment, options.processingContext);
                 } else if ((processor.uniformProcessor || processor.uniformBufferProcessor) && StringTools.StartsWith(this.line, "uniform")) {
                     let regex = /uniform (.+) (.+)/;
 

+ 1 - 1
src/Engines/WebGPU/webgpuPipelineContext.ts

@@ -77,7 +77,7 @@ export class WebGPUPipelineContext implements IPipelineContext {
             uniforms[uniformsNames[index]] = uniform;
         });
 
-        // TODO. Cleanup SEB.
+        // TODO WEBGPU. Cleanup SEB.
         // Prevent Memory Leak by reducing the number of string, refer to the string instead of copy.
         effect._fragmentSourceCode = "";
         effect._vertexSourceCode = "";

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

@@ -5,11 +5,16 @@ import { ShaderProcessingContext } from "../processors/shaderProcessingOptions";
  */
 export class WebGPUShaderProcessingContext implements ShaderProcessingContext {
     public attributeNextLocation: number;
+    public varyingNextLocation: number;
 
     public availableAttributes: { [key: string]: number };
+    public availableVaryings: { [key: string]: number };
 
     constructor() {
         this.attributeNextLocation = 0;
+        this.varyingNextLocation = 0;
+
         this.availableAttributes = { };
+        this.availableVaryings = { };
     }
 }

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

@@ -5,7 +5,6 @@ import { WebGPUShaderProcessingContext } from './webgpuShaderProcessingContext';
 
 /** @hidden */
 export class WebGPUShaderProcessor implements IShaderProcessor {
-    // varyingProcessor?: (varying: string, isFragment: boolean) => string;
     // uniformProcessor?: (uniform: string, isFragment: boolean) => string;
     // uniformBufferProcessor?: (uniformBuffer: string, isFragment: boolean) => string;
     // endOfUniformBufferProcessor?: (closingBracketLine: string, isFragment: boolean) => string;
@@ -16,13 +15,12 @@ export class WebGPUShaderProcessor implements IShaderProcessor {
     public attributeProcessor(attribute: string, processingContext: Nullable<ShaderProcessingContext>) {
         const webgpuProcessingContext = processingContext! as WebGPUShaderProcessingContext;
 
-        // return attribute.replace("attribute", "in");
         const attribRegex = new RegExp(/\s*attribute\s+(\S+)\s+(\S+)\s*;/gm);
 
         const match = attribRegex.exec(attribute);
-        const location = webgpuProcessingContext.attributeNextLocation++;
         if (match != null) {
             const name = match[2];
+            const location = webgpuProcessingContext.attributeNextLocation++;
 
             webgpuProcessingContext.availableAttributes[name] = location;
             attribute = attribute.replace(match[0], `layout(location = ${location}) in ${match[1]} ${name};`);
@@ -30,21 +28,28 @@ export class WebGPUShaderProcessor implements IShaderProcessor {
         return attribute;
     }
 
-    public varyingProcessor(varying: string, isFragment: boolean) {
-        return varying.replace("varying", isFragment ? "in" : "out");
-        // const inOut = isFragment ? "in" : "out";
-        // const attribRegex = new RegExp(/\s+varying\s+(\w+)\s+/gm);
+    public varyingProcessor(varying: string, isFragment: boolean, processingContext: Nullable<ShaderProcessingContext>) {
+        const webgpuProcessingContext = processingContext! as WebGPUShaderProcessingContext;
+
+        const varyingRegex = new RegExp(/\s*varying\s+(\S+)\s+(\S+)\s*;/gm);
+
+        const match = varyingRegex.exec(varying);
+        if (match != null) {
+            const name = match[2];
+            let location: number;
 
-        // let location = 0;
-        // let match = attribRegex.exec(code);
-        // while (match != null) {
-        //     if (match[1]) {
-        //         code = code.replace(match[0], ` layout(location = ${location}) ${inOut} ${match[1]} `);
-        //         location++;
-        //     }
-        //     match = attribRegex.exec(code);
-        // }
-        // return code;
+            if (isFragment) {
+                location = webgpuProcessingContext.availableVaryings[name];
+            }
+            else {
+                location = webgpuProcessingContext.varyingNextLocation++;
+                webgpuProcessingContext.availableVaryings[name] = location;
+            }
+
+            webgpuProcessingContext.availableAttributes[name] = location;
+            varying = varying.replace(match[0], `layout(location = ${location}) ${isFragment ? "in" : "out"} ${match[1]} ${name};`);
+        }
+        return varying;
     }
 
     public postProcessor(code: string, defines: string[], isFragment: boolean) {

+ 7 - 39
src/Shaders/ShadersInclude/pbrFragmentExtraDeclaration.fx

@@ -1,58 +1,26 @@
 // Input
-#ifdef WEBGPU
-    layout(location = 0) in vec3 vPositionW;
-#else
-    varying vec3 vPositionW;
-#endif
+varying vec3 vPositionW;
 
 #ifdef NORMAL
-    #ifdef WEBGPU
-        layout(location = 1) in vec3 vNormalW;
-    #else
-        varying vec3 vNormalW;
-    #endif
+    varying vec3 vNormalW;
 
     #if defined(USESPHERICALFROMREFLECTIONMAP) && defined(USESPHERICALINVERTEX)
-        #ifdef WEBGPU
-            #ifdef WEBGPU
-                layout(location = 2) in vec3 vEnvironmentIrradiance;
-            #else
-                varying vec3 vEnvironmentIrradiance;
-            #endif
-        #else
-            varying vec3 vEnvironmentIrradiance;
-        #endif
+        varying vec3 vEnvironmentIrradiance;
     #endif
 #endif
 
 #ifdef VERTEXCOLOR
-    #ifdef WEBGPU
-        layout(location = 3) in vec4 vColor;
-    #else
-        varying vec4 vColor;
-    #endif
+    varying vec4 vColor;
 #endif
 
 #if DEBUGMODE > 0
-    #ifdef WEBGPU
-        layout(location = 5) in vec4 vClipSpacePosition;
-    #else
-        varying vec4 vClipSpacePosition;
-    #endif
+    varying vec4 vClipSpacePosition;
 #endif
 
 #ifdef MAINUV1
-    #ifdef WEBGPU
-        layout(location = 6) in vec2 vMainUV1;
-    #else
-        varying vec2 vMainUV1;
-    #endif
+    varying vec2 vMainUV1;
 #endif
 
 #ifdef MAINUV2
-    #ifdef WEBGPU
-        layout(location = 7) in vec2 vMainUV2;
-    #else
-        varying vec2 vMainUV2;
-    #endif
+    varying vec2 vMainUV2;
 #endif

+ 1 - 5
src/Shaders/ShadersInclude/pbrFragmentSamplersDeclaration.fx

@@ -49,11 +49,7 @@
     #endif
 
     #ifdef REFLECTIONMAP_SKYBOX
-        #ifdef WEBGPU
-            layout(location = 4) in vec3 vPositionUVW;
-        #else
-            varying vec3 vPositionUVW;
-        #endif
+        varying vec3 vPositionUVW;
     #else
         #if defined(REFLECTIONMAP_EQUIRECTANGULAR_FIXED) || defined(REFLECTIONMAP_MIRROREDEQUIRECTANGULAR_FIXED)
             varying vec3 vDirectionW;

+ 8 - 40
src/Shaders/pbr.vertex.fx

@@ -39,68 +39,36 @@ attribute vec3 position;
 #include<bonesDeclaration>
 
 // Output
-#ifdef WEBGPU
-    layout(location = 0) out vec3 vPositionW;
-#else
-    varying vec3 vPositionW;
-#endif
+varying vec3 vPositionW;
 
 #ifdef NORMAL
-    #ifdef WEBGPU
-        layout(location = 1) out vec3 vNormalW;
-    #else
-        varying vec3 vNormalW;
-    #endif
+    varying vec3 vNormalW;
 
     #if defined(USESPHERICALFROMREFLECTIONMAP) && defined(USESPHERICALINVERTEX)
-        #ifdef WEBGPU
-            layout(location = 2) out vec3 vEnvironmentIrradiance;
-        #else
-            varying vec3 vEnvironmentIrradiance;
-        #endif
+        varying vec3 vEnvironmentIrradiance;
         
         #include<harmonicsFunctions>
     #endif
 #endif
 
 #ifdef VERTEXCOLOR
-    #ifdef WEBGPU
-        layout(location = 3) out vec4 vColor;
-    #else
-        varying vec4 vColor;
-    #endif
+    varying vec4 vColor;
 #endif
 
 #ifdef REFLECTIONMAP_SKYBOX
-    #ifdef WEBGPU
-        layout(location = 4) out vec3 vPositionUVW;
-    #else
-        varying vec3 vPositionUVW;
-    #endif
+    varying vec3 vPositionUVW;
 #endif
 
 #if DEBUGMODE > 0
-    #ifdef WEBGPU
-        layout(location = 5) out vec4 vClipSpacePosition;
-    #else
-        varying vec4 vClipSpacePosition;
-    #endif
+    varying vec4 vClipSpacePosition;
 #endif
 
 #ifdef MAINUV1
-    #ifdef WEBGPU
-        layout(location = 6) out vec2 vMainUV1;
-    #else
-        varying vec2 vMainUV1;
-    #endif
+    varying vec2 vMainUV1;
 #endif
 
 #ifdef MAINUV2
-    #ifdef WEBGPU
-        layout(location = 7) out vec2 vMainUV2;
-    #else
-        varying vec2 vMainUV2;
-    #endif
+    varying vec2 vMainUV2;
 #endif
 
 #if defined(ALBEDO) && ALBEDODIRECTUV == 0