浏览代码

removing gl calls from high level classes

Benjamin Guignabert 4 年之前
父节点
当前提交
e17b87bdef
共有 3 个文件被更改,包括 64 次插入26 次删除
  1. 34 0
      src/Engines/Extensions/engine.multiRender.ts
  2. 7 7
      src/Rendering/geometryBufferRenderer.ts
  3. 23 19
      src/Rendering/prePassRenderer.ts

+ 34 - 0
src/Engines/Extensions/engine.multiRender.ts

@@ -39,9 +39,43 @@ declare module "../../Engines/thinEngine" {
          * @param attachments gl attachments
          */
         bindAttachments(attachments: number[]) : void;
+
+        /**
+         * Creates a layout object to draw/clear on specific textures in a MRT
+         * @param textureStatus textureStatus[i] indicates if the i-th is active
+         * @returns A layout to be fed to the engine, calling `bindAttachments`.
+         */
+        buildTextureLayout(textureStatus: boolean[]) : number[];
+
+        /**
+         * Restores the webgl state to only draw on the main color attachment
+         */
+        restoreSingleAttachment() : void;
     }
 }
 
+ThinEngine.prototype.restoreSingleAttachment = function(): void {
+    const gl = this._gl;
+
+    this.bindAttachments([gl.BACK]);
+};
+
+ThinEngine.prototype.buildTextureLayout = function(textureStatus: boolean[]): number[] {
+    const gl = this._gl;
+
+    const result = [];
+
+    for (let i = 0; i < textureStatus.length; i++) {
+        if (textureStatus[i]) {
+            result.push((<any>gl)["COLOR_ATTACHMENT" + i]);
+        } else {
+            result.push(gl.NONE);
+        }
+    }
+
+    return result;
+};
+
 ThinEngine.prototype.bindAttachments = function(attachments: number[]): void {
     const gl = this._gl;
 

+ 7 - 7
src/Rendering/geometryBufferRenderer.ts

@@ -35,7 +35,7 @@ interface ISavedTransformationMatrix {
 export class GeometryBufferRenderer {
     /**
      * Constant used to retrieve the depth + normal texture index in the G-Buffer textures array
-     * using getIndex(GeometryBufferRenderer.DEPTHNORMAL_TYPE)
+     * using getIndex(GeometryBufferRenderer.DEPTHNORMAL_TEXTURE_INDEX)
      */
     public static readonly DEPTHNORMAL_TEXTURE_TYPE = 0;
     /**
@@ -100,7 +100,7 @@ export class GeometryBufferRenderer {
      * Sets up internal structures to share outputs with PrePassRenderer
      * This method should only be called by the PrePassRenderer itself
      */
-    public linkPrePassRenderer(prePassRenderer: PrePassRenderer) {
+    public _linkPrePassRenderer(prePassRenderer: PrePassRenderer) {
         this._linkedWithPrePass = true;
         this._prePassRenderer = prePassRenderer;
 
@@ -118,7 +118,7 @@ export class GeometryBufferRenderer {
      * Separates internal structures from PrePassRenderer so the geometry buffer can now operate by itself.
      * This method should only be called by the PrePassRenderer itself
      */
-    public unlinkPrePassRenderer() {
+    public _unlinkPrePassRenderer() {
         this._linkedWithPrePass = false;
         this._createRenderTargets();
     }
@@ -127,7 +127,7 @@ export class GeometryBufferRenderer {
      * @hidden
      * Resets the geometry buffer layout
      */
-    public resetLayout() {
+    public _resetLayout() {
         this._enablePosition = false;
         this._enableReflectivity = false;
         this._enableVelocity = false;
@@ -139,7 +139,7 @@ export class GeometryBufferRenderer {
      * Replaces a texture in the geometry buffer renderer
      * Useful when linking textures of the prepass renderer
      */
-    public replaceTexture(texture: Texture, geometryBufferType: number, index: number) {
+    public _forceTextureType(geometryBufferType: number, index: number) {
         if (geometryBufferType === GeometryBufferRenderer.POSITION_TEXTURE_TYPE) {
             this._positionIndex = index;
             this._enablePosition = true;
@@ -159,7 +159,7 @@ export class GeometryBufferRenderer {
      * Sets texture attachments
      * Useful when linking textures of the prepass renderer
      */
-    public setAttachments(attachments: number[]) {
+    public _setAttachments(attachments: number[]) {
         this._attachments = attachments;
     }
 
@@ -168,7 +168,7 @@ export class GeometryBufferRenderer {
      * Replaces the first texture which is hard coded as a depth texture in the geometry buffer
      * Useful when linking textures of the prepass renderer
      */
-    public linkInternalTexture(internalTexture: InternalTexture) {
+    public _linkInternalTexture(internalTexture: InternalTexture) {
         this._multiRenderTarget._texture = internalTexture;
     }
 

+ 23 - 19
src/Rendering/prePassRenderer.ts

@@ -152,11 +152,11 @@ export class PrePassRenderer {
             }
 
             this._geometryBuffer.renderList = [];
-            this._geometryBuffer.linkPrePassRenderer(this);
+            this._geometryBuffer._linkPrePassRenderer(this);
             this._updateGeometryBufferLayout();
         } else {
             if (this._geometryBuffer) {
-                this._geometryBuffer.unlinkPrePassRenderer();
+                this._geometryBuffer._unlinkPrePassRenderer();
             }
             this._geometryBuffer = null;
             this._scene.disableGeometryBufferRenderer();
@@ -176,20 +176,22 @@ export class PrePassRenderer {
     }
 
     private _initializeAttachments() {
-        let gl = this._engine._gl;
-
-        this._multiRenderAttachments = [];
-        this._clearAttachments = [gl.NONE];
-        this._defaultAttachments = [gl.COLOR_ATTACHMENT0];
+        const multiRenderLayout = [];
+        const clearLayout = [false];
+        const defaultLayout = [true];
 
         for (let i = 0; i < this.mrtCount; i++) {
-            this._multiRenderAttachments.push((<any>gl)["COLOR_ATTACHMENT" + i]);
+            multiRenderLayout.push(true);
 
             if (i > 0) {
-                this._clearAttachments.push((<any>gl)["COLOR_ATTACHMENT" + i]);
-                this._defaultAttachments.push(gl.NONE);
+                clearLayout.push(true);
+                defaultLayout.push(false);
             }
         }
+
+        this._multiRenderAttachments = this._engine.buildTextureLayout(multiRenderLayout);
+        this._clearAttachments = this._engine.buildTextureLayout(clearLayout);
+        this._defaultAttachments = this._engine.buildTextureLayout(defaultLayout);
     }
 
     private _createCompositionEffect() {
@@ -319,13 +321,15 @@ export class PrePassRenderer {
 
     private _updateGeometryBufferLayout() {
         if (this._geometryBuffer) {
-            this._geometryBuffer.resetLayout();
+            this._geometryBuffer._resetLayout();
 
-            const attachments = this._defaultAttachments.slice();
-            const gl = this._scene.getEngine()._gl;
-            attachments[0] = gl.NONE;
+            const texturesActivated = [];
+
+            for (let i = 0; i < this._mrtLayout.length; i++) {
+                texturesActivated.push(false);
+            }
 
-            this._geometryBuffer.linkInternalTexture(this.prePassRT.getInternalTexture()!);
+            this._geometryBuffer._linkInternalTexture(this.prePassRT.getInternalTexture()!);
 
             const matches = [
                 {
@@ -350,12 +354,12 @@ export class PrePassRenderer {
             for (let i = 0; i < matches.length; i++) {
                 const index = this._mrtLayout.indexOf(matches[i].prePassConstant);
                 if (index !== -1) {
-                    this._geometryBuffer.replaceTexture(this.prePassRT.textures[index], matches[i].geometryBufferConstant, index);
-                    attachments[index] = (<any>gl)["COLOR_ATTACHMENT" + index];
+                    this._geometryBuffer._forceTextureType(matches[i].geometryBufferConstant, index);
+                    texturesActivated[index] = true;
                 }
             }
 
-            this._geometryBuffer.setAttachments(attachments);
+            this._geometryBuffer._setAttachments(this._engine.buildTextureLayout(texturesActivated));
         }
     }
 
@@ -522,7 +526,7 @@ export class PrePassRenderer {
         if (!this.enabled) {
             // Prepass disabled, we render only on 1 color attachment
             this._engine.restoreDefaultFramebuffer();
-            this._engine.bindAttachments([this._engine._gl.BACK]);
+            this._engine.restoreSingleAttachment();
         }
     }