Forráskód Böngészése

Fix ubo buffers management

Popov72 4 éve
szülő
commit
bf47335246
2 módosított fájl, 17 hozzáadás és 97 törlés
  1. 16 80
      src/Materials/uniformBuffer.ts
  2. 1 17
      src/scene.ts

+ 16 - 80
src/Materials/uniformBuffer.ts

@@ -27,7 +27,7 @@ export class UniformBuffer {
 
     private _engine: Engine;
     private _buffer: Nullable<DataBuffer>;
-    private _buffers : Array<[Nullable<DataBuffer>, Float32Array, { [name: string]: number }]>;
+    private _buffers : Array<DataBuffer>;
     private _bufferIndex: number;
     private _createBufferOnWrite: boolean;
     private _data: number[];
@@ -43,7 +43,6 @@ export class UniformBuffer {
     private _currentEffectName: string;
     private _name: string;
     private _currentFrameId: number;
-    private _updateList: Array<[number, number, string?]>;
 
     /** @hidden */
     public _alreadyBound = false;
@@ -218,7 +217,6 @@ export class UniformBuffer {
             this._bufferIndex = -1;
             this._createBufferOnWrite = false;
             this._currentFrameId = 0;
-            this._updateList = [];
         }
 
         if (this._noUBO) {
@@ -525,7 +523,7 @@ export class UniformBuffer {
         }
 
         if (ThinEngine.Features.trackUbosInFrame) {
-            this._buffers.push([this._buffer, this._bufferData, this._valueCache]);
+            this._buffers.push(this._buffer);
             this._bufferIndex = this._buffers.length - 1;
             this._createBufferOnWrite = false;
         }
@@ -574,41 +572,17 @@ export class UniformBuffer {
         this._createBufferOnWrite = ThinEngine.Features.trackUbosInFrame;
     }
 
-    private _createNewBuffer(excludedLocation: number): boolean {
+    private _createNewBuffer() {
         if (this._bufferIndex + 1 < this._buffers.length) {
-            const currentBufferData = this._bufferData;
             this._bufferIndex++;
-            this._buffer = this._buffers[this._bufferIndex][0];
-            this._bufferData = this._buffers[this._bufferIndex][1];
-            this._valueCache = this._buffers[this._bufferIndex][2];
+            this._buffer = this._buffers[this._bufferIndex];
             this._createBufferOnWrite = false;
-            for (let i = 0; i < this._updateList.length; ++i) {
-                let [location, size, name] = this._updateList[i];
-                if (location === excludedLocation) {
-                    continue;
-                } else if (location < 0) {
-                    this._valueCache[name!] = size;
-                } else {
-                    while (size--) {
-                        if (this._bufferData[location + size] !== currentBufferData[location + size]) {
-                            this._bufferData[location + size] = currentBufferData[location + size];
-                            this._needSync = true;
-                        }
-                    }
-                }
-            }
-            if (this._currentEffect && this._buffer) {
-                this._currentEffect.bindUniformBuffer(this._buffer, this._currentEffectName);
-            }
-            return false;
+            this._needSync = true;
         } else {
-            this._bufferData = this._bufferData.slice();
-            this._valueCache = { ...this._valueCache }; // clone
             this._rebuild();
-            if (this._currentEffect && this._buffer) {
-                this._currentEffect.bindUniformBuffer(this._buffer, this._currentEffectName);
-            }
-            return true;
+        }
+        if (this._currentEffect && this._buffer) {
+            this._currentEffect.bindUniformBuffer(this._buffer, this._currentEffectName);
         }
     }
 
@@ -616,18 +590,16 @@ export class UniformBuffer {
         if (ThinEngine.Features.trackUbosInFrame && this._currentFrameId !== this._engine.frameId) {
             this._currentFrameId = this._engine.frameId;
             this._createBufferOnWrite = false;
-            this._updateList.length = 0;
             if (this._buffers && this._buffers.length > 0) {
+                this._needSync = this._bufferIndex !== 0;
                 this._bufferIndex = 0;
-                this._buffer = this._buffers[this._bufferIndex][0];
-                this._bufferData = this._buffers[this._bufferIndex][1];
-                this._valueCache = this._buffers[this._bufferIndex][2];
-                if (this._currentEffect && this._buffer) {
-                    this._currentEffect.bindUniformBuffer(this._buffer, this._currentEffectName);
-                }
+                this._buffer = this._buffers[this._bufferIndex];
             } else {
                 this._bufferIndex = -1;
             }
+            if (this._currentEffect && this._buffer) {
+                this._currentEffect.bindUniformBuffer(this._buffer, this._currentEffectName);
+            }
         }
     }
 
@@ -663,26 +635,15 @@ export class UniformBuffer {
                 if ((size === 16 && !ThinEngine.Features.uniformBufferHardCheckMatrix) || this._bufferData[location + i] !== data[i]) {
                     changed = true;
                     if (this._createBufferOnWrite) {
-                        if (!this._createNewBuffer(location)) {
-                            // we didn't create a new buffer but advanced to the next one: retry the update, chances are that the new buffer already has the right data for uniformName so we won't set needSync to true
-                            this.updateUniform(uniformName, data, size);
-                            return;
-                        }
+                        this._createNewBuffer();
                     }
                     this._bufferData[location + i] = data[i];
                 }
             }
 
-            if (changed && ThinEngine.Features.trackUbosInFrame) {
-                this._updateList[this._updateList.length] = [location, size];
-            }
-
             this._needSync = this._needSync || changed;
         } else {
             // No cache for dynamic
-            if (this._createBufferOnWrite) {
-                this._createNewBuffer(-1);
-            }
             for (var i = 0; i < size; i++) {
                 this._bufferData[location + i] = data[i];
             }
@@ -719,11 +680,7 @@ export class UniformBuffer {
                 if (this._bufferData[location + baseStride * 4 + countToFour] !== data[i]) {
                     changed = true;
                     if (this._createBufferOnWrite) {
-                        if (!this._createNewBuffer(location)) {
-                            // we didn't create a new buffer but advanced to the next one: retry the update, chances are that the new buffer already has the right data for uniformName so we won't set needSync to true
-                            this.updateUniformArray(uniformName, data, size);
-                            return;
-                        }
+                        this._createNewBuffer();
                     }
                     this._bufferData[location + baseStride * 4 + countToFour] = data[i];
                 }
@@ -737,16 +694,9 @@ export class UniformBuffer {
                 }
             }
 
-            if (changed && ThinEngine.Features.trackUbosInFrame) {
-                this._updateList[this._updateList.length] = [location, baseStride * 4];
-            }
-
             this._needSync = this._needSync || changed;
         } else {
             // No cache for dynamic
-            if (this._createBufferOnWrite) {
-                this._createNewBuffer(-1);
-            }
             for (var i = 0; i < size; i++) {
                 this._bufferData[location + i] = data[i];
             }
@@ -764,23 +714,9 @@ export class UniformBuffer {
             return false;
         }
 
-        if (!this._createBufferOnWrite) {
-            this._valueCache[name] = flag;
-            if (ThinEngine.Features.trackUbosInFrame) {
-                this._updateList[this._updateList.length] = [-1, flag, name];
-            }
-        }
-
         return true;
     }
 
-    /** @hidden */
-    public _getMatrixUpdateFlagFromCache(matrixName: string): number | undefined {
-        this._checkNewFrame();
-
-        return this._valueCache[matrixName];
-    }
-
     // Update methods
 
     private _updateMatrix3x3ForUniform(name: string, matrix: Float32Array): void {
@@ -1035,7 +971,7 @@ export class UniformBuffer {
 
         if (ThinEngine.Features.trackUbosInFrame && this._buffers) {
             for (let i = 0; i < this._buffers.length; ++i) {
-                const buffer = this._buffers[i][0];
+                const buffer = this._buffers[i];
                 this._engine._releaseBuffer(buffer!);
             }
         } else if (this._buffer && this._engine._releaseBuffer(this._buffer)) {

+ 1 - 17
src/scene.ts

@@ -55,7 +55,6 @@ import { Frustum } from './Maths/math.frustum';
 import { UniqueIdGenerator } from './Misc/uniqueIdGenerator';
 import { FileTools, LoadFileError, RequestFileError, ReadFileError } from './Misc/fileTools';
 import { IClipPlanesHolder } from './Misc/interfaces/iClipPlanesHolder';
-import { ThinEngine } from './Engines/thinEngine';
 
 declare type Ray = import("./Culling/ray").Ray;
 declare type TrianglePickingPredicate = import("./Culling/ray").TrianglePickingPredicate;
@@ -2004,22 +2003,7 @@ export class Scene extends AbstractScene implements IAnimatable, IClipPlanesHold
      * @param projectionR defines the right Projection matrix to use (if provided)
      */
     public setTransformMatrix(viewL: Matrix, projectionL: Matrix, viewR?: Matrix, projectionR?: Matrix): void {
-        if (ThinEngine.Features.trackUbosInFrame) {
-            const indexBufferBefore = this._sceneUbo._indexBuffer;
-            const viewLUpdateFlag = this._sceneUbo._getMatrixUpdateFlagFromCache("view");
-            const projectionLUpdateFlag = this._sceneUbo._getMatrixUpdateFlagFromCache("projection");
-            const indexBufferAfter = this._sceneUbo._indexBuffer;
-
-            if (viewLUpdateFlag === viewL.updateFlag && projectionLUpdateFlag === projectionL.updateFlag) {
-                if (indexBufferAfter === indexBufferBefore) {
-                    return;
-                } else {
-                    // fall through
-                    // indexBufferAfter !== indexBufferBefore means we have cycled to the first buffer (because of a frame id change): this buffer is in line with
-                    // the matrices passed to setTransformMatrix but the matrices saved in the scene are (may) not (be) ok, we must update them
-                }
-            }
-        } else if (this._viewUpdateFlag === viewL.updateFlag && this._projectionUpdateFlag === projectionL.updateFlag) {
+        if (this._viewUpdateFlag === viewL.updateFlag && this._projectionUpdateFlag === projectionL.updateFlag) {
             return;
         }