فهرست منبع

fixing GBR bug with UBO

Benjamin Guignabert 4 سال پیش
والد
کامیت
12de5e5e13

+ 1 - 1
src/Materials/Background/backgroundMaterial.ts

@@ -913,7 +913,7 @@ export class BackgroundMaterial extends PushMaterial {
                     this.onCompiled(effect);
                 }
 
-                this.bindSceneUniformBuffer(effect, scene.getSceneUniformBuffer());
+                MaterialHelper.BindSceneUniformBuffer(effect, scene.getSceneUniformBuffer());
             };
 
             var join = defines.toString();

+ 2 - 30
src/Materials/material.ts

@@ -928,34 +928,6 @@ export class Material implements IAnimatable {
     }
 
     /**
-     * Update the scene ubo before it can be used in rendering processing
-     * @param scene the scene to retrieve the ubo from
-     * @returns the scene UniformBuffer
-     */
-    public finalizeSceneUbo(scene: Scene): UniformBuffer {
-        const ubo = scene.getSceneUniformBuffer();
-        const eyePosition = MaterialHelper.BindEyePosition(null, scene);
-        ubo.updateFloat4("vEyePosition",
-            eyePosition.x,
-            eyePosition.y,
-            eyePosition.z,
-            eyePosition.w);
-
-        ubo.update();
-
-        return ubo;
-    }
-
-    /**
-     * Binds the scene's uniform buffer to the effect.
-     * @param effect defines the effect to bind to the scene uniform buffer
-     * @param sceneUbo defines the uniform buffer storing scene data
-     */
-    public bindSceneUniformBuffer(effect: Effect, sceneUbo: UniformBuffer): void {
-        sceneUbo.bindToEffect(effect, "Scene");
-    }
-
-    /**
      * Binds the view matrix to the effect
      * @param effect defines the effect to bind the view matrix to
      */
@@ -1002,8 +974,8 @@ export class Material implements IAnimatable {
         if (this._needToBindSceneUbo) {
             if (effect) {
                 this._needToBindSceneUbo = false;
-                this.finalizeSceneUbo(this.getScene());
-                this.bindSceneUniformBuffer(effect, this.getScene().getSceneUniformBuffer());
+                MaterialHelper.FinalizeSceneUbo(this.getScene());
+                MaterialHelper.BindSceneUniformBuffer(effect, this.getScene().getSceneUniformBuffer());
             }
         }
         if (mesh) {

+ 29 - 0
src/Materials/materialHelper.ts

@@ -59,6 +59,35 @@ export class MaterialHelper {
         return TmpVectors.Vector4[0];
     }
 
+
+    /**
+     * Update the scene ubo before it can be used in rendering processing
+     * @param scene the scene to retrieve the ubo from
+     * @returns the scene UniformBuffer
+     */
+    public static FinalizeSceneUbo(scene: Scene): UniformBuffer {
+        const ubo = scene.getSceneUniformBuffer();
+        const eyePosition = MaterialHelper.BindEyePosition(null, scene);
+        ubo.updateFloat4("vEyePosition",
+            eyePosition.x,
+            eyePosition.y,
+            eyePosition.z,
+            eyePosition.w);
+
+        ubo.update();
+
+        return ubo;
+    }
+
+    /**
+     * Binds the scene's uniform buffer to the effect.
+     * @param effect defines the effect to bind to the scene uniform buffer
+     * @param sceneUbo defines the uniform buffer storing scene data
+     */
+    public static BindSceneUniformBuffer(effect: Effect, sceneUbo: UniformBuffer): void {
+        sceneUbo.bindToEffect(effect, "Scene");
+    }
+
     /**
      * Helps preparing the defines values about the UVs in used in the effect.
      * UVs are shared as much as we can accross channels in the shaders.

+ 1 - 0
src/PostProcesses/postProcess.ts

@@ -516,6 +516,7 @@ export class PostProcess {
                 for (var j = 0; j < this._textures.length; j++) {
                     if (this._textures.data[j] === this._textureCache[i].texture) {
                         currentlyUsed = true;
+                        break;
                     }
                 }
 

+ 26 - 10
src/Rendering/geometryBufferRenderer.ts

@@ -98,6 +98,7 @@ export class GeometryBufferRenderer {
     private _linkedWithPrePass: boolean = false;
     private _prePassRenderer: PrePassRenderer;
     private _attachments: number[];
+    private _useUbo: boolean;
 
     protected _effect: Effect;
     protected _cachedDefines: string;
@@ -307,6 +308,7 @@ export class GeometryBufferRenderer {
     constructor(scene: Scene, ratio: number = 1) {
         this._scene = scene;
         this._ratio = ratio;
+        this._useUbo = scene.getEngine().supportsUniformBuffers;
 
         GeometryBufferRenderer._SceneComponentInitialization(this._scene);
 
@@ -452,14 +454,21 @@ export class GeometryBufferRenderer {
         if (this._cachedDefines !== join) {
             this._cachedDefines = join;
             this._effect = this._scene.getEngine().createEffect("geometry",
-                attribs,
-                [
-                    "world", "mBones", "viewProjection", "diffuseMatrix", "view", "previousWorld", "previousViewProjection", "mPreviousBones",
-                    "morphTargetInfluences", "bumpMatrix", "reflectivityMatrix", "vTangentSpaceParams", "vBumpInfos"
-                ],
-                ["diffuseSampler", "bumpSampler", "reflectivitySampler"], join,
-                undefined, undefined, undefined,
-                { buffersCount: this._multiRenderTarget.textures.length - 1, maxSimultaneousMorphTargets: numMorphInfluencers });
+                { 
+                    attributes: attribs,
+                    uniformsNames: [
+                        "world", "mBones", "viewProjection", "diffuseMatrix", "view", "previousWorld", "previousViewProjection", "mPreviousBones",
+                        "morphTargetInfluences", "bumpMatrix", "reflectivityMatrix", "vTangentSpaceParams", "vBumpInfos"
+                    ],
+                    samplers: ["diffuseSampler", "bumpSampler", "reflectivitySampler"],
+                    defines: join,
+                    onCompiled: null,
+                    fallbacks: null,
+                    onError: null,
+                    uniformBuffersNames: ["Scene"],
+                    indexParameters: { buffersCount: this._multiRenderTarget.textures.length - 1, maxSimultaneousMorphTargets: numMorphInfluencers },
+                },
+                this._scene.getEngine());
         }
 
         return this._effect.isReady();
@@ -588,8 +597,13 @@ export class GeometryBufferRenderer {
                 engine.enableEffect(this._effect);
                 renderingMesh._bind(subMesh, this._effect, material.fillMode);
 
-                this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
-                this._effect.setMatrix("view", scene.getViewMatrix());
+                if (!this._useUbo) {
+                    this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
+                    this._effect.setMatrix("view", scene.getViewMatrix());
+                } else {
+                    MaterialHelper.FinalizeSceneUbo(this._scene);
+                    MaterialHelper.BindSceneUniformBuffer(this._effect, this._scene.getSceneUniformBuffer());
+                }
 
                 if (material) {
                     var sideOrientation: Nullable<number>;
@@ -694,6 +708,7 @@ export class GeometryBufferRenderer {
                 renderSubMesh(opaqueSubMeshes.data[index]);
             }
 
+            engine.setDepthWrite(false);
             for (index = 0; index < alphaTestSubMeshes.length; index++) {
                 renderSubMesh(alphaTestSubMeshes.data[index]);
             }
@@ -703,6 +718,7 @@ export class GeometryBufferRenderer {
                     renderSubMesh(transparentSubMeshes.data[index]);
                 }
             }
+            engine.setDepthWrite(true);
         };
     }
 

+ 1 - 1
src/Rendering/prePassRenderer.ts

@@ -664,7 +664,7 @@ export class PrePassRenderer {
 
             }
         }
-        
+
         if (enablePrePass) {
             this._setRenderTargetEnabled(this.defaultRT, true);
         }

+ 1 - 0
src/Shaders/ShadersInclude/geometryUboDeclaration.fx

@@ -0,0 +1 @@
+#include<sceneUboDeclaration>

+ 3 - 0
src/Shaders/ShadersInclude/geometryVertexDeclaration.fx

@@ -0,0 +1,3 @@
+// Uniform
+uniform mat4 viewProjection;
+uniform mat4 view;

+ 1 - 4
src/Shaders/geometry.vertex.fx

@@ -6,6 +6,7 @@ precision highp float;
 #include<morphTargetsVertexDeclaration>[0..maxSimultaneousMorphTargets]
 
 #include<instancesDeclaration>
+#include<__decl__geometryVertex>
 
 attribute vec3 position;
 attribute vec3 normal;
@@ -34,10 +35,6 @@ attribute vec3 normal;
 	#endif
 #endif
 
-// Uniform
-uniform mat4 viewProjection;
-uniform mat4 view;
-
 #ifdef BUMP
 varying mat4 vWorldView;
 #endif