Pārlūkot izejas kodu

fixed multisampling on MRT

Benjamin Guignabert 5 gadi atpakaļ
vecāks
revīzija
051dc643ea

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

@@ -10,11 +10,12 @@ declare module "../../Engines/thinEngine" {
         /**
          * Unbind a list of render target textures from the webGL context
          * This is used only when drawBuffer extension or webGL2 are active
+         * @param count number of color textures
          * @param textures defines the render target textures to unbind
          * @param disableGenerateMipMaps defines a boolean indicating that mipmaps must not be generated
          * @param onBeforeUnbind defines a function which will be called before the effective unbind
          */
-        unBindMultiColorAttachmentFramebuffer(textures: InternalTexture[], disableGenerateMipMaps: boolean, onBeforeUnbind?: () => void): void;
+        unBindMultiColorAttachmentFramebuffer(count: number, textures: InternalTexture[], disableGenerateMipMaps: boolean, onBeforeUnbind?: () => void): void;
 
         /**
          * Create a multi render target texture
@@ -28,15 +29,16 @@ declare module "../../Engines/thinEngine" {
         /**
          * Update the sample count for a given multiple render target texture
          * @see http://doc.babylonjs.com/features/webgl2#multisample-render-targets
+         * @param count number of color textures
          * @param textures defines the textures to update
          * @param samples defines the sample count to set
          * @returns the effective sample count (could be 0 if multisample render targets are not supported)
          */
-        updateMultipleRenderTargetTextureSampleCount(textures: Nullable<InternalTexture[]>, samples: number): number;
+        updateMultipleRenderTargetTextureSampleCount(count: number, textures: Nullable<InternalTexture[]>, samples: number): number;
     }
 }
 
-ThinEngine.prototype.unBindMultiColorAttachmentFramebuffer = function(textures: InternalTexture[], disableGenerateMipMaps: boolean = false, onBeforeUnbind?: () => void): void {
+ThinEngine.prototype.unBindMultiColorAttachmentFramebuffer = function(count: number, textures: InternalTexture[], disableGenerateMipMaps: boolean = false, onBeforeUnbind?: () => void): void {
     this._currentRenderTarget = null;
 
     // If MSAA, we need to bitblt back to main texture
@@ -48,11 +50,11 @@ ThinEngine.prototype.unBindMultiColorAttachmentFramebuffer = function(textures:
 
         var attachments = textures[0]._attachments;
         if (!attachments) {
-            attachments = new Array(textures.length);
+            attachments = new Array(count);
             textures[0]._attachments = attachments;
         }
 
-        for (var i = 0; i < textures.length; i++) {
+        for (var i = 0; i < count; i++) {
             var texture = textures[i];
 
             for (var j = 0; j < attachments.length; j++) {
@@ -73,7 +75,7 @@ ThinEngine.prototype.unBindMultiColorAttachmentFramebuffer = function(textures:
         gl.drawBuffers(attachments);
     }
 
-    for (var i = 0; i < textures.length; i++) {
+    for (var i = 0; i < count; i++) {
         var texture = textures[i];
         if (texture.generateMipMaps && !disableGenerateMipMaps && !texture.isCube) {
             this._bindTextureDirectly(gl.TEXTURE_2D, texture, true);
@@ -250,8 +252,8 @@ ThinEngine.prototype.createMultipleRenderTarget = function(size: any, options: I
     return textures;
 };
 
-ThinEngine.prototype.updateMultipleRenderTargetTextureSampleCount = function(textures: Nullable<InternalTexture[]>, samples: number): number {
-    if (this.webGLVersion < 2 || !textures || textures.length == 0) {
+ThinEngine.prototype.updateMultipleRenderTargetTextureSampleCount = function(count: number, textures: Nullable<InternalTexture[]>, samples: number): number {
+    if (this.webGLVersion < 2 || !textures || count == 0) {
         return 1;
     }
 
@@ -274,7 +276,7 @@ ThinEngine.prototype.updateMultipleRenderTargetTextureSampleCount = function(tex
         textures[0]._MSAAFramebuffer = null;
     }
 
-    for (var i = 0; i < textures.length; i++) {
+    for (var i = 0; i < count; i++) {
         if (textures[i]._MSAARenderBuffer) {
             gl.deleteRenderbuffer(textures[i]._MSAARenderBuffer);
             textures[i]._MSAARenderBuffer = null;
@@ -294,7 +296,7 @@ ThinEngine.prototype.updateMultipleRenderTargetTextureSampleCount = function(tex
 
         var attachments = [];
 
-        for (var i = 0; i < textures.length; i++) {
+        for (var i = 0; i < count; i++) {
             var texture = textures[i];
             var attachment = (<any>gl)[this.webGLVersion > 1 ? "COLOR_ATTACHMENT" + i : "COLOR_ATTACHMENT" + i + "_WEBGL"];
 

+ 2 - 2
src/Materials/PBR/pbrBaseMaterial.ts

@@ -1549,9 +1549,9 @@ export abstract class PBRBaseMaterial extends PushMaterial {
         }
 
         if (scene.highDefinitionPipeline) {
-            defines.HIGH_DEFINITION_PIPELINE = true;            
+            defines.HIGH_DEFINITION_PIPELINE = true;
         } else {
-            defines.HIGH_DEFINITION_PIPELINE = false;            
+            defines.HIGH_DEFINITION_PIPELINE = false;
         }
 
         defines.FORCENORMALFORWARD = this._forceNormalForward;

+ 12 - 2
src/Materials/Textures/multiRenderTarget.ts

@@ -60,6 +60,7 @@ export class MultiRenderTarget extends RenderTargetTexture {
     private _internalTextures: InternalTexture[];
     private _textures: Texture[];
     private _multiRenderTargetOptions: IMultiRenderTargetOptions;
+    private _count: number;
 
     /**
      * Get if draw buffers are currently supported by the used hardware and browser.
@@ -76,6 +77,13 @@ export class MultiRenderTarget extends RenderTargetTexture {
     }
 
     /**
+     * Gets the number of textures in this MRT. This number can be different from `_textures.length` in case a depth texture is generated.
+     */
+    public get count(): number {
+        return this._count;
+    }
+
+    /**
      * Get the depth texture generated by the multi render target if options.generateDepthTexture has been set
      */
     public get depthTexture(): Texture {
@@ -161,6 +169,8 @@ export class MultiRenderTarget extends RenderTargetTexture {
             textureCount: count
         };
 
+        this._count = count;
+
         this._createInternalTextures();
         this._createTextures();
     }
@@ -207,7 +217,7 @@ export class MultiRenderTarget extends RenderTargetTexture {
             return;
         }
 
-        this._samples = this._getEngine()!.updateMultipleRenderTargetTextureSampleCount(this._internalTextures, value);
+        this._samples = this._getEngine()!.updateMultipleRenderTargetTextureSampleCount(this.count, this._internalTextures, value);
     }
 
     /**
@@ -222,7 +232,7 @@ export class MultiRenderTarget extends RenderTargetTexture {
     }
 
     protected unbindFrameBuffer(engine: Engine, faceIndex: number): void {
-        engine.unBindMultiColorAttachmentFramebuffer(this._internalTextures, this.isCube, () => {
+        engine.unBindMultiColorAttachmentFramebuffer(this.count, this._internalTextures, this.isCube, () => {
             this.onAfterRenderObservable.notifyObservers(faceIndex);
         });
     }

+ 1 - 1
src/Shaders/pbr.fragment.fx

@@ -494,7 +494,7 @@ void main(void) {
 
 
 #ifdef HIGH_DEFINITION_PIPELINE
-    gl_FragData[0] = vec4(1.0, 0.0, 0.0, 1.0);
+    gl_FragData[0] = finalColor;
     gl_FragData[1] = vec4(1.0, 0.0, 0.0, 1.0);
     gl_FragData[2] = vec4(1.0, 0.0, 0.0, 1.0);
     gl_FragData[3] = vec4(1.0, 0.0, 0.0, 1.0);

+ 2 - 2
src/scene.ts

@@ -250,7 +250,7 @@ export class Scene extends AbstractScene implements IAnimatable {
 
     private _highDefinitionPipeline: boolean = true;
 
-    public get highDefinitionPipeline () {
+    public get highDefinitionPipeline() {
         return this._highDefinitionPipeline;
     }
 
@@ -1442,7 +1442,7 @@ export class Scene extends AbstractScene implements IAnimatable {
         // TODO : TEMPORARY
         this.highDefinitionMRT = new MultiRenderTarget("sceneHighDefinitionMRT", { width: engine.getRenderWidth(), height: engine.getRenderHeight() }, 5, this,
             { generateMipMaps: false, generateDepthTexture: true, defaultType: Constants.TEXTURETYPE_UNSIGNED_INT });
-
+        this.highDefinitionMRT.samples = 1;
         this.sceneCompositorPostProcess = new SceneCompositorPostProcess("sceneCompositor", 1, null, undefined, this._engine);
         this.sceneCompositorPostProcess.inputTexture = this.highDefinitionMRT.getInternalTexture()!;
     }