浏览代码

Merge pull request #8176 from syntheticmagus/nativeEngineShaderInlining

Native engine shader inlining
sebavan 5 年之前
父节点
当前提交
b230896570

+ 1 - 1
src/Engines/Native/nativeShaderProcessor.ts

@@ -133,7 +133,7 @@ export class NativeShaderProcessor extends WebGL2ShaderProcessor {
         return code;
     }
 
-   public postProcessor(code: string, defines: string[], isFragment: boolean): string {
+    public postProcessor(code: string, defines: string[], isFragment: boolean): string {
         code = super.postProcessor(code, defines, isFragment);
         code = code.replace("<UNIFORM>", `layout(binding=0) uniform Frame {\n${this._uniforms.join("\n")}\n};`);
         code = code.replace("out vec4 glFragColor", "layout(location=0) out vec4 glFragColor");

+ 14 - 4
src/Engines/nativeEngine.ts

@@ -23,6 +23,7 @@ import { Constants } from './constants';
 import { ThinEngine, ISceneLike } from './thinEngine';
 import { IWebRequest } from '../Misc/interfaces/iWebRequest';
 import { EngineStore } from './engineStore';
+import { ShaderCodeInliner } from "./Processors/shaderCodeInliner";
 
 interface INativeEngine {
     dispose(): void;
@@ -482,10 +483,19 @@ export class NativeEngine extends Engine {
 
     public createShaderProgram(pipelineContext: IPipelineContext, vertexCode: string, fragmentCode: string, defines: Nullable<string>, context?: WebGLRenderingContext, transformFeedbackVaryings: Nullable<string[]> = null): any {
         this.onBeforeShaderCompilationObservable.notifyObservers(this);
-        const program = this._native.createProgram(
-            ThinEngine._ConcatenateShader(vertexCode, defines),
-            ThinEngine._ConcatenateShader(fragmentCode, defines)
-        );
+
+        const vertexInliner = new ShaderCodeInliner(vertexCode);
+        vertexInliner.processCode();
+        vertexCode = vertexInliner.code;
+
+        const fragmentInliner = new ShaderCodeInliner(fragmentCode);
+        fragmentInliner.processCode();
+        fragmentCode = fragmentInliner.code;
+
+        vertexCode = ThinEngine._ConcatenateShader(vertexCode, defines);
+        fragmentCode = ThinEngine._ConcatenateShader(fragmentCode, defines);
+
+        const program = this._native.createProgram(vertexCode, fragmentCode);
         this.onAfterShaderCompilationObservable.notifyObservers(this);
         return program;
     }

+ 2 - 2
src/Shaders/ShadersInclude/pbrBlockAlbedoOpacity.fx

@@ -8,7 +8,7 @@ void albedoOpacityBlock(
     const in vec4 vAlbedoColor,
 #ifdef ALBEDO
     const in vec4 albedoTexture,
-    const in vec2 vAlbedoInfos,
+    const in vec2 albedoInfos,
 #endif
 #ifdef OPACITY
     const in vec4 opacityMap,
@@ -32,7 +32,7 @@ void albedoOpacityBlock(
             surfaceAlbedo *= albedoTexture.rgb;
         #endif
 
-        surfaceAlbedo *= vAlbedoInfos.y;
+        surfaceAlbedo *= albedoInfos.y;
     #endif
 
     #ifdef VERTEXCOLOR

+ 3 - 3
src/Shaders/ShadersInclude/pbrBlockReflectivity.fx

@@ -24,7 +24,7 @@ void reflectivityBlock(
     const in vec4 metallicReflectanceFactors,
 #endif
 #ifdef REFLECTIVITY
-    const in vec3 vReflectivityInfos,
+    const in vec3 reflectivityInfos,
     const in vec4 surfaceMetallicOrReflectivityColorMap,
 #endif
 #if defined(METALLICWORKFLOW) && defined(REFLECTIVITY)  && defined(AOSTOREINMETALMAPRED)
@@ -49,7 +49,7 @@ void reflectivityBlock(
 
             #ifdef AOSTOREINMETALMAPRED
                 vec3 aoStoreInMetalMap = vec3(surfaceMetallicOrReflectivityColorMap.r, surfaceMetallicOrReflectivityColorMap.r, surfaceMetallicOrReflectivityColorMap.r);
-                outParams.ambientOcclusionColor = mix(ambientOcclusionColor, aoStoreInMetalMap, vReflectivityInfos.z);
+                outParams.ambientOcclusionColor = mix(ambientOcclusionColor, aoStoreInMetalMap, reflectivityInfos.z);
             #endif
 
             #ifdef METALLNESSSTOREINMETALMAPBLUE
@@ -118,7 +118,7 @@ void reflectivityBlock(
 
             #ifdef MICROSURFACEFROMREFLECTIVITYMAP
                 microSurface *= surfaceMetallicOrReflectivityColorMap.a;
-                microSurface *= vReflectivityInfos.z;
+                microSurface *= reflectivityInfos.z;
             #else
                 #ifdef MICROSURFACEAUTOMATIC
                     microSurface *= computeDefaultMicroSurface(microSurface, surfaceReflectivityColor);