Jelajahi Sumber

BufferGeometry: Add `indirectOffset` parameter for indirect drawing (#32392)

Niklas Niehus 1 bulan lalu
induk
melakukan
58d9d506e5

+ 13 - 1
src/core/BufferGeometry.js

@@ -111,6 +111,16 @@ class BufferGeometry extends EventDispatcher {
 		 */
 		this.indirect = null;
 
+		/**
+		 * The offset, in bytes, into the indirect drawing buffer where the value data begins.
+		 *
+		 * Can only be used with {@link WebGPURenderer} and a WebGPU backend.
+		 *
+		 * @type {number}
+		 * @default 0
+		 */
+		this.indirectOffset = 0;
+
 		/**
 		 * This dictionary has as id the name of the attribute to be set and as value
 		 * the buffer attribute to set it to. Rather than accessing this property directly,
@@ -224,11 +234,13 @@ class BufferGeometry extends EventDispatcher {
 	 * Sets the given indirect attribute to this geometry.
 	 *
 	 * @param {BufferAttribute} indirect - The attribute holding indirect draw calls.
+	 * @param {number} [indirectOffset=0] - The offset, in bytes, into the indirect drawing buffer where the value data begins.
 	 * @return {BufferGeometry} A reference to this instance.
 	 */
-	setIndirect( indirect ) {
+	setIndirect( indirect, indirectOffset = 0 ) {
 
 		this.indirect = indirect;
+		this.indirectOffset = indirectOffset;
 
 		return this;
 

+ 12 - 0
src/renderers/common/Geometries.js

@@ -310,6 +310,18 @@ class Geometries extends DataMap {
 
 	}
 
+	/**
+	 * Returns the byte offset into the indirect attribute buffer of the given render object.
+	 *
+	 * @param {RenderObject} renderObject - The render object.
+	 * @return {number} The byte offset into the indirect attribute buffer.
+	 */
+	getIndirectOffset( renderObject ) {
+
+		return renderObject.geometry.indirectOffset;
+
+	}
+
 	/**
 	 * Returns the index of the given render object's geometry. This is implemented
 	 * in a method to return a wireframe index if necessary.

+ 11 - 0
src/renderers/common/RenderObject.js

@@ -444,6 +444,17 @@ class RenderObject {
 
 	}
 
+	/**
+	 * Returns the byte offset into the indirect attribute buffer.
+	 *
+	 * @return {number} The byte offset into the indirect attribute buffer.
+	 */
+	getIndirectOffset() {
+
+		return this._geometries.getIndirectOffset( this );
+
+	}
+
 	/**
 	 * Returns an array that acts as a key for identifying the render object in a chain map.
 	 *

+ 4 - 2
src/renderers/webgpu/WebGPUBackend.js

@@ -1600,8 +1600,9 @@ class WebGPUBackend extends Backend {
 				if ( indirect !== null ) {
 
 					const buffer = this.get( indirect ).buffer;
+					const indirectOffset = renderObject.getIndirectOffset();
 
-					passEncoderGPU.drawIndexedIndirect( buffer, 0 );
+					passEncoderGPU.drawIndexedIndirect( buffer, indirectOffset );
 
 				} else {
 
@@ -1620,8 +1621,9 @@ class WebGPUBackend extends Backend {
 				if ( indirect !== null ) {
 
 					const buffer = this.get( indirect ).buffer;
+					const indirectOffset = renderObject.getIndirectOffset();
 
-					passEncoderGPU.drawIndirect( buffer, 0 );
+					passEncoderGPU.drawIndirect( buffer, indirectOffset );
 
 				} else {
 

粤ICP备19079148号