瀏覽代碼

Fix bug too many ubo buffers created

Popov72 4 年之前
父節點
當前提交
ac94e6ea84
共有 4 個文件被更改,包括 14 次插入4 次删除
  1. 3 0
      src/Engines/engineFeatures.ts
  2. 1 0
      src/Engines/thinEngine.ts
  3. 1 0
      src/Engines/webgpuEngine.ts
  4. 9 4
      src/Materials/uniformBuffer.ts

+ 3 - 0
src/Engines/engineFeatures.ts

@@ -25,4 +25,7 @@ export interface EngineFeatures {
 
     /** @hidden */
     _collectUbosUpdatedInFrame: boolean;
+
+    /** @hidden */
+    _warnWhenTooManyBuffersInUniformBufferClass: number;
 }

+ 1 - 0
src/Engines/thinEngine.ts

@@ -184,6 +184,7 @@ export class ThinEngine {
         allowTexturePrefiltering: false,
         trackUbosInFrame: false,
         _collectUbosUpdatedInFrame: false,
+        _warnWhenTooManyBuffersInUniformBufferClass: 0,
     };
 
     /**

+ 1 - 0
src/Engines/webgpuEngine.ts

@@ -238,6 +238,7 @@ export class WebGPUEngine extends Engine {
         ThinEngine.Features.allowTexturePrefiltering = true;
         ThinEngine.Features.trackUbosInFrame = true;
         ThinEngine.Features._collectUbosUpdatedInFrame = true;
+        ThinEngine.Features._warnWhenTooManyBuffersInUniformBufferClass = 30;
 
         options.deviceDescriptor = options.deviceDescriptor || { };
         options.swapChainFormat = options.swapChainFormat || WebGPUConstants.TextureFormat.BGRA8Unorm;

+ 9 - 4
src/Materials/uniformBuffer.ts

@@ -557,6 +557,9 @@ export class UniformBuffer {
             if (this._currentEffect && this._buffer) {
                 this._currentEffect.bindUniformBuffer(this._buffer, this._currentEffectName);
             }
+            if (this._buffers.length >= ThinEngine.Features._warnWhenTooManyBuffersInUniformBufferClass) {
+                console.log(`%c Too many buffers created in the UniformBuffer class! name=${this.name}, number of buffers=${this._buffers.length}`, "background: #ff0000; color: #ffffff", this);
+            }
             return true;
         }
     }
@@ -667,10 +670,12 @@ export class UniformBuffer {
             for (var i = 0; i < size; i++) {
                 if (this._bufferData[location + baseStride * 4 + countToFour] !== data[i]) {
                     changed = true;
-                    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;
+                    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._bufferData[location + baseStride * 4 + countToFour] = data[i];
                 }