瀏覽代碼

Implementation of setArrayX

Popov72 4 年之前
父節點
當前提交
a076e1935d
共有 3 個文件被更改,包括 31 次插入9 次删除
  1. 7 4
      src/Engines/WebGPU/webgpuPipelineContext.ts
  2. 5 5
      src/Materials/effect.ts
  3. 19 0
      src/Materials/uniformBuffer.ts

+ 7 - 4
src/Engines/WebGPU/webgpuPipelineContext.ts

@@ -228,7 +228,10 @@ export class WebGPUPipelineContext implements IPipelineContext {
      * @param array array to be set.
      */
     public setArray(uniformName: string, array: number[]): void {
-        throw "setArray not Supported in LeftOver UBO.";
+        if (!this.uniformBuffer || !this.leftOverUniformsByName[uniformName]) {
+            return;
+        }
+        this.uniformBuffer.updateArray(uniformName, array);
     }
 
     /**
@@ -237,7 +240,7 @@ export class WebGPUPipelineContext implements IPipelineContext {
      * @param array array to be set.
      */
     public setArray2(uniformName: string, array: number[]): void {
-        throw "setArray2 not Supported in LeftOver UBO.";
+        this.setArray(uniformName, array);
     }
 
     /**
@@ -247,7 +250,7 @@ export class WebGPUPipelineContext implements IPipelineContext {
      * @returns this effect.
      */
     public setArray3(uniformName: string, array: number[]): void {
-        throw "setArray3 not Supported in LeftOver UBO.";
+        this.setArray(uniformName, array);
     }
 
     /**
@@ -256,7 +259,7 @@ export class WebGPUPipelineContext implements IPipelineContext {
      * @param array array to be set.
      */
     public setArray4(uniformName: string, array: number[]): void {
-        throw "setArray4 not Supported in LeftOver UBO.";
+        this.setArray(uniformName, array);
     }
 
     /**

+ 5 - 5
src/Materials/effect.ts

@@ -1,5 +1,5 @@
 import { Observable } from "../Misc/observable";
-import { Nullable } from "../types";
+import { FloatArray, Nullable } from "../types";
 import { Constants } from "../Engines/constants";
 import { DomManagement } from "../Misc/domManagement";
 import { Logger } from "../Misc/logger";
@@ -950,7 +950,7 @@ export class Effect implements IDisposable {
      * @param array array to be set.
      * @returns this effect.
      */
-    public setFloatArray(uniformName: string, array: Float32Array): Effect {
+    public setFloatArray(uniformName: string, array: FloatArray): Effect {
         this._pipelineContext!.setArray(uniformName, array);
         return this;
     }
@@ -961,7 +961,7 @@ export class Effect implements IDisposable {
      * @param array array to be set.
      * @returns this effect.
      */
-    public setFloatArray2(uniformName: string, array: Float32Array): Effect {
+    public setFloatArray2(uniformName: string, array: FloatArray): Effect {
         this._pipelineContext!.setArray2(uniformName, array);
         return this;
     }
@@ -972,7 +972,7 @@ export class Effect implements IDisposable {
      * @param array array to be set.
      * @returns this effect.
      */
-    public setFloatArray3(uniformName: string, array: Float32Array): Effect {
+    public setFloatArray3(uniformName: string, array: FloatArray): Effect {
         this._pipelineContext!.setArray3(uniformName, array);
         return this;
     }
@@ -983,7 +983,7 @@ export class Effect implements IDisposable {
      * @param array array to be set.
      * @returns this effect.
      */
-    public setFloatArray4(uniformName: string, array: Float32Array): Effect {
+    public setFloatArray4(uniformName: string, array: FloatArray): Effect {
         this._pipelineContext!.setArray4(uniformName, array);
         return this;
     }

+ 19 - 0
src/Materials/uniformBuffer.ts

@@ -91,6 +91,13 @@ export class UniformBuffer {
     public updateFloatArray: (name: string, array: Float32Array) => void;
 
     /**
+     * Lambda to Update an array of number in a uniform buffer.
+     * This is dynamic to allow compat with webgl 1 and 2.
+     * You will need to pass the name of the uniform as well as the value.
+     */
+    public updateArray: (name: string, array: number[]) => void;
+
+    /**
      * Lambda to Update a 4x4 Matrix in a uniform buffer.
      * This is dynamic to allow compat with webgl 1 and 2.
      * You will need to pass the name of the uniform as well as the value.
@@ -166,6 +173,7 @@ export class UniformBuffer {
             this.updateFloat3 = this._updateFloat3ForEffect;
             this.updateFloat4 = this._updateFloat4ForEffect;
             this.updateFloatArray = this._updateFloatArrayForEffect;
+            this.updateArray = this._updateArrayForEffect;
             this.updateMatrix = this._updateMatrixForEffect;
             this.updateMatrices = this._updateMatricesForEffect;
             this.updateVector3 = this._updateVector3ForEffect;
@@ -182,6 +190,7 @@ export class UniformBuffer {
             this.updateFloat3 = this._updateFloat3ForUniform;
             this.updateFloat4 = this._updateFloat4ForUniform;
             this.updateFloatArray = this._updateFloatArrayForUniform;
+            this.updateArray = this._updateArrayForUniform;
             this.updateMatrix = this._updateMatrixForUniform;
             this.updateMatrices = this._updateMatricesForUniform;
             this.updateVector3 = this._updateVector3ForUniform;
@@ -289,6 +298,8 @@ export class UniformBuffer {
                 throw "addUniform should not be use with Array in UBO: " + name;
             }
 
+            this._fillAlignment(4);
+
             this._uniformArraySizes[name] = { strideSize: size, arraySize };
             if (size == 16) {
                 size = size * arraySize;
@@ -659,6 +670,14 @@ export class UniformBuffer {
         this.updateUniformArray(name, array, array.length);
     }
 
+    private _updateArrayForEffect(name: string, array: number[]) {
+        this._currentEffect.setArray(name, array);
+    }
+
+    private _updateArrayForUniform(name: string, array: number[]) {
+        this.updateUniformArray(name, array, array.length);
+    }
+
     private _updateMatrixForEffect(name: string, mat: IMatrixLike) {
         this._currentEffect.setMatrix(name, mat);
     }