Bläddra i källkod

fixed all buggy PGs

Benjamin Guignabert 4 år sedan
förälder
incheckning
c1d139012e

+ 12 - 6
src/Engines/Extensions/engine.multiRender.ts

@@ -22,18 +22,20 @@ declare module "../../Engines/thinEngine" {
          * @see https://doc.babylonjs.com/features/webgl2#multiple-render-target
          * @param size defines the size of the texture
          * @param options defines the creation options
+         * @param noDrawBuffers if set to true, the engine won't make an initializing call of drawBuffers
          * @returns the cube texture as an InternalTexture
          */
-        createMultipleRenderTarget(size: any, options: IMultiRenderTargetOptions): InternalTexture[];
+        createMultipleRenderTarget(size: any, options: IMultiRenderTargetOptions, noDrawBuffers?: boolean): InternalTexture[];
 
         /**
          * Update the sample count for a given multiple render target texture
          * @see https://doc.babylonjs.com/features/webgl2#multisample-render-targets
          * @param textures defines the textures to update
          * @param samples defines the sample count to set
+         * @param noDrawBuffers if set to true, the engine won't make an initializing call of drawBuffers
          * @returns the effective sample count (could be 0 if multisample render targets are not supported)
          */
-        updateMultipleRenderTargetTextureSampleCount(textures: Nullable<InternalTexture[]>, samples: number): number;
+        updateMultipleRenderTargetTextureSampleCount(textures: Nullable<InternalTexture[]>, samples: number, noDrawBuffers?: boolean): number;
 
         /**
          * Select a subsets of attachments to draw to.
@@ -182,7 +184,7 @@ ThinEngine.prototype.unBindMultiColorAttachmentFramebuffer = function(textures:
     this._bindUnboundFramebuffer(null);
 };
 
-ThinEngine.prototype.createMultipleRenderTarget = function(size: any, options: IMultiRenderTargetOptions): InternalTexture[] {
+ThinEngine.prototype.createMultipleRenderTarget = function(size: any, options: IMultiRenderTargetOptions, noDrawBuffers?: boolean): InternalTexture[] {
     var generateMipMaps = false;
     var generateDepthBuffer = true;
     var generateStencilBuffer = false;
@@ -331,8 +333,10 @@ ThinEngine.prototype.createMultipleRenderTarget = function(size: any, options: I
         textures.push(depthTexture);
         this._internalTexturesCache.push(depthTexture);
     }
+    if (!noDrawBuffers) {
+        gl.drawBuffers(attachments);
+    }
 
-    gl.drawBuffers(attachments);
     this._bindUnboundFramebuffer(null);
 
     this.resetTextureCache();
@@ -340,7 +344,7 @@ ThinEngine.prototype.createMultipleRenderTarget = function(size: any, options: I
     return textures;
 };
 
-ThinEngine.prototype.updateMultipleRenderTargetTextureSampleCount = function(textures: Nullable<InternalTexture[]>, samples: number): number {
+ThinEngine.prototype.updateMultipleRenderTargetTextureSampleCount = function(textures: Nullable<InternalTexture[]>, samples: number, noDrawBuffers?: boolean): number {
     if (this.webGLVersion < 2 || !textures) {
         return 1;
     }
@@ -412,7 +416,9 @@ ThinEngine.prototype.updateMultipleRenderTargetTextureSampleCount = function(tex
             gl.bindRenderbuffer(gl.RENDERBUFFER, null);
             attachments.push(attachment);
         }
-        gl.drawBuffers(attachments);
+        if (!noDrawBuffers) {
+            gl.drawBuffers(attachments);
+        }
     } else {
         this._bindUnboundFramebuffer(textures[0]._framebuffer);
     }

+ 19 - 4
src/Materials/Textures/multiRenderTarget.ts

@@ -47,6 +47,10 @@ export interface IMultiRenderTargetOptions {
      * Define the default type of the buffers we are creating
      */
     defaultType?: number;
+    /**
+     * Define the default type of the buffers we are creating
+     */
+    drawOnlyOnFirstAttachmentByDefault?: boolean;
 }
 
 /**
@@ -61,6 +65,7 @@ export class MultiRenderTarget extends RenderTargetTexture {
     private _textures: Texture[];
     private _multiRenderTargetOptions: IMultiRenderTargetOptions;
     private _count: number;
+    private _drawOnlyOnFirstAttachmentByDefault: boolean;
 
     /**
      * Get if draw buffers are currently supported by the used hardware and browser.
@@ -130,7 +135,7 @@ export class MultiRenderTarget extends RenderTargetTexture {
         var generateMipMaps = options && options.generateMipMaps ? options.generateMipMaps : false;
         var generateDepthTexture = options && options.generateDepthTexture ? options.generateDepthTexture : false;
         var doNotChangeAspectRatio = !options || options.doNotChangeAspectRatio === undefined ? true : options.doNotChangeAspectRatio;
-
+        var drawOnlyOnFirstAttachmentByDefault = options && options.drawOnlyOnFirstAttachmentByDefault ? options.drawOnlyOnFirstAttachmentByDefault : false;
         super(name, size, scene, generateMipMaps, doNotChangeAspectRatio, 
             undefined,
             undefined,
@@ -165,6 +170,7 @@ export class MultiRenderTarget extends RenderTargetTexture {
         };
 
         this._count = count;
+        this._drawOnlyOnFirstAttachmentByDefault = drawOnlyOnFirstAttachmentByDefault;
 
         if (count > 0) {
             this._createInternalTextures();
@@ -190,6 +196,10 @@ export class MultiRenderTarget extends RenderTargetTexture {
 
     /** @hidden */
     public _rebuild(forceFullRebuild: boolean = false): void {
+        if (this._count < 1) {
+            return;
+        }
+        
         this.releaseInternalTextures();
         this._createInternalTextures();
 
@@ -203,12 +213,12 @@ export class MultiRenderTarget extends RenderTargetTexture {
         }
 
         if (this.samples !== 1) {
-            this._getEngine()!.updateMultipleRenderTargetTextureSampleCount(this._internalTextures, this.samples);
+            this._getEngine()!.updateMultipleRenderTargetTextureSampleCount(this._internalTextures, this.samples, this._drawOnlyOnFirstAttachmentByDefault);
         }
     }
 
     private _createInternalTextures(): void {
-        this._internalTextures = this._getEngine()!.createMultipleRenderTarget(this._size, this._multiRenderTargetOptions);
+        this._internalTextures = this._getEngine()!.createMultipleRenderTarget(this._size, this._multiRenderTargetOptions, this._drawOnlyOnFirstAttachmentByDefault);
 
         // Keeps references to frame buffer and stencil/depth buffer
         this._texture = this._internalTextures[0];
@@ -250,7 +260,12 @@ export class MultiRenderTarget extends RenderTargetTexture {
             return;
         }
 
-        this._samples = this._getEngine()!.updateMultipleRenderTargetTextureSampleCount(this._internalTextures, value);
+        if (this._internalTextures) {
+            this._samples = this._getEngine()!.updateMultipleRenderTargetTextureSampleCount(this._internalTextures, value);
+        } else{
+            // In case samples are set with 0 textures created, we must save the desired samples value
+            this._samples = value;
+        }
     }
 
     /**

+ 1 - 12
src/Materials/Textures/prePassRenderTarget.ts

@@ -47,6 +47,7 @@ export class PrePassRenderTarget extends MultiRenderTarget {
 
     public _createCompositionEffect() {
         this.imageProcessingPostProcess = new ImageProcessingPostProcess("prePassComposition", 1, null, undefined, this._engine);
+        this.imageProcessingPostProcess._updateParameters();
     }
 
     /**
@@ -67,18 +68,6 @@ export class PrePassRenderTarget extends MultiRenderTarget {
     }
 
     /**
-     * How many samples are used for this RT
-     */
-    public get samples() {
-        return this._samples;
-    }
-
-    public set samples(n: number) {
-        this._samples = n;
-    }
-
-
-    /**
      * Changes the number of render targets in this MRT
      * Be careful as it will recreate all the data in the new texture.
      * @param count new texture count

+ 0 - 2
src/PostProcesses/postProcess.ts

@@ -502,7 +502,6 @@ export class PostProcess {
         const tex = this._engine.createRenderTargetTexture(textureSize, textureOptions);
         tex._postProcessChannel = channel;
         this._textureCache.push(tex);
-        console.log("Texture create. Cache length : " + this._textureCache.length);
 
         return tex;
     }
@@ -523,7 +522,6 @@ export class PostProcess {
                 if (!currentlyUsed) {
                     this._engine._releaseTexture(this._textureCache[i]);
                     this._textureCache.splice(i, 1);
-                    console.log("Texture flushed. Cache length : " + this._textureCache.length);
                 }
             }
         }

+ 27 - 13
src/Rendering/prePassRenderer.ts

@@ -68,11 +68,11 @@ export class PrePassRenderer {
      * How many samples are used for MSAA of the scene render target
      */
     public get samples() {
-        return 1;//this.defaultRT.samples;
+        return this.defaultRT.samples;
     }
 
     public set samples(n: number) {
-        // this.defaultRT.samples = n;
+        this.defaultRT.samples = n;
     }
 
     private static _textureFormats = [
@@ -217,10 +217,14 @@ export class PrePassRenderer {
 
     public _createRenderTarget(name: string, renderTargetTexture: Nullable<RenderTargetTexture>) : PrePassRenderTarget {
         const rt = new PrePassRenderTarget(name, renderTargetTexture, { width: this._engine.getRenderWidth(), height: this._engine.getRenderHeight() }, 0, this._scene,
-            { generateMipMaps: false, generateStencilBuffer: true, defaultType: Constants.TEXTURETYPE_UNSIGNED_INT, types: [] });
-        rt.samples = 1;
+            { generateMipMaps: false, generateStencilBuffer: true, defaultType: Constants.TEXTURETYPE_UNSIGNED_INT, types: [], drawOnlyOnFirstAttachmentByDefault: true });
 
         this.renderTargets.push(rt);
+
+        // Necessary because the engine assumes we want to draw on all 
+        // attachments by default
+        // this._drawOnlyOnDefaultAttachments(rt);
+
         return rt;
     }
 
@@ -337,7 +341,7 @@ export class PrePassRenderer {
      * Restores attachments for single texture draw.
      */
     public restoreAttachments() {
-        if (this.enabled && this._defaultAttachments) {
+        if (this.enabled && this._currentTarget.enabled && this._defaultAttachments) {
             this._engine.bindAttachments(this._defaultAttachments);
         }
     }
@@ -438,6 +442,18 @@ export class PrePassRenderer {
         }
     }
 
+    private _drawOnlyOnDefaultAttachments(prePassRenderTarget: PrePassRenderTarget) {
+        const internalTexture = prePassRenderTarget.getInternalTexture();
+
+        if (!internalTexture) {
+            return;
+        }
+
+        this._engine._bindUnboundFramebuffer(internalTexture._framebuffer);
+        this._engine.bindAttachments(this._defaultAttachments);
+        this._engine._bindUnboundFramebuffer(internalTexture._framebuffer);
+    }
+
     private _setState(enabled: boolean) {
         this._enabled = enabled;
     }
@@ -477,6 +493,7 @@ export class PrePassRenderer {
         for (let i = 0; i < this.renderTargets.length; i++) {
             if (this.mrtCount !== previousMrtCount) {
                 this.renderTargets[i].updateCount(this.mrtCount, { types: this._mrtFormats });
+                // this._drawOnlyOnDefaultAttachments(this.renderTargets[i]);
             }
 
             this.renderTargets[i]._resetPostProcessChain();
@@ -549,15 +566,12 @@ export class PrePassRenderer {
         const firstPrePassPP = prePassRenderTarget._beforeCompositionPostProcesses && prePassRenderTarget._beforeCompositionPostProcesses[0];
         let firstPP = null;
 
+        // Setting the scene-wide post process configuration
+        this._scene.imageProcessingConfiguration.applyByPostProcess = this._needsCompositionForThisPass || cameraHasImageProcessing;
+
         // Create composition effect if needed
-        if (this._needsCompositionForThisPass) {
-            if (!prePassRenderTarget.imageProcessingPostProcess) {
-                prePassRenderTarget._createCompositionEffect();
-            }
-            prePassRenderTarget.imageProcessingPostProcess.imageProcessingConfiguration.applyByPostProcess = true;
-        } else if (this._scene.imageProcessingConfiguration && !cameraHasImageProcessing) {
-            // in case image processing was applied before and no longer needed for this pass
-            this._scene.imageProcessingConfiguration.applyByPostProcess = false;
+        if (this._needsCompositionForThisPass && !prePassRenderTarget.imageProcessingPostProcess) {
+            prePassRenderTarget._createCompositionEffect();
         }
 
         // Setting the prePassRenderTarget as input texture of the first PP

BIN
tests/validation/ReferenceImages/prepass-ssao-dof.png


BIN
tests/validation/ReferenceImages/prepass-ssao-instances.png


+ 4 - 4
tests/validation/config.json

@@ -1021,7 +1021,7 @@
         {
             "title": "Prepass SSAO + instances",
             "renderCount": 10,
-            "playgroundId": "#YB006J#349",
+            "playgroundId": "#YB006J#355",
             "referenceImage": "prepass-ssao-instances.png"
         },
         {
@@ -1039,7 +1039,7 @@
         {
             "title": "Prepass SSAO + depth of field",
             "renderCount": 10,
-            "playgroundId": "#DX6AV#88",
+            "playgroundId": "#8F5HYV#14",
             "referenceImage": "prepass-ssao-dof.png"
         },
         {
@@ -1159,8 +1159,8 @@
         {
             "title": "Prepass SSAO + MSAA",
             "renderCount": 10,
-            "playgroundId": "#12MKMN#5",
-            "referenceImage": "prepass-mb-lens.png"
+            "playgroundId": "#12MKMN#7",
+            "referenceImage": "prepass-ssao-msaa.png"
         }
     ]
 }