فهرست منبع

Revert "Backend: Introduce `createUniformBuffer()`" (#33409)

Michael Herzog 1 روز پیش
والد
کامیت
4286974d49

+ 3 - 21
examples/jsm/inspector/tabs/Timeline.js

@@ -1044,29 +1044,11 @@ class Timeline extends Tab {
 			case 'updateBindings': {
 
 				const bindGroup = args[ 0 ];
+				const details = { group: bindGroup.name || 'unknown' };
 
-				const details = {
-					group: bindGroup.name || 'unknown',
-					count: bindGroup.bindings.length
-				};
-
-				return details;
-
-			}
-
-			case 'createUniformBuffer':
-			case 'destroyUniformBuffer': {
-
-				const binding = args[ 0 ];
-
-				const details = {
-					group: binding.groupNode.name || 'unknown',
-					size: binding.byteLength + ' bytes'
-				};
-
-				if ( binding.name !== details.group ) {
+				if ( bindGroup.bindings ) {
 
-					details.name = binding.name;
+					details.count = bindGroup.bindings.length;
 
 				}
 

+ 0 - 16
src/renderers/common/Backend.js

@@ -389,22 +389,6 @@ class Backend {
 	 */
 	createStorageAttribute( /*attribute*/ ) { }
 
-	/**
-	 * Creates a uniform buffer.
-	 *
-	 * @abstract
-	 * @param {Buffer} uniformBuffer - The uniform buffer.
-	 */
-	createUniformBuffer( /*uniformBuffer*/ ) { }
-
-	/**
-	 * Destroys a uniform buffer.
-	 *
-	 * @abstract
-	 * @param {Buffer} uniformBuffer - The uniform buffer.
-	 */
-	destroyUniformBuffer( /*uniformBuffer*/ ) { }
-
 	/**
 	 * Updates the GPU buffer of a shader attribute.
 	 *

+ 32 - 74
src/renderers/common/Bindings.js

@@ -77,39 +77,39 @@ class Bindings extends DataMap {
 	 */
 	getForRender( renderObject ) {
 
-		return this._getBindings( renderObject, renderObject.getBindings() );
+		const bindings = renderObject.getBindings();
 
-	}
+		for ( const bindGroup of bindings ) {
 
-	/**
-	 * Returns the bind groups for the given compute node.
-	 *
-	 * @param {Node} computeNode - The compute node.
-	 * @return {Array<BindGroup>} The bind groups.
-	 */
-	getForCompute( computeNode ) {
+			const groupData = this.get( bindGroup );
 
-		return this._getBindings( computeNode, this.nodes.getForCompute( computeNode ).bindings );
+			if ( groupData.bindGroup === undefined ) {
 
-	}
+				// each object defines an array of bindings (ubos, textures, samplers etc.)
 
-	_getBindings( object, bindings ) {
+				this._init( bindGroup );
 
-		const data = this.get( object );
+				this.backend.createBindings( bindGroup, bindings, 0 );
 
-		if ( data.bindings !== bindings ) {
+				groupData.bindGroup = bindGroup;
 
-			if ( data.bindings !== undefined ) {
+			}
 
-				this._updateBindingsUsage( data.bindings, - 1 );
+		}
 
-			}
+		return bindings;
 
-			this._updateBindingsUsage( bindings, 1 );
+	}
 
-			data.bindings = bindings;
+	/**
+	 * Returns the bind groups for the given compute node.
+	 *
+	 * @param {Node} computeNode - The compute node.
+	 * @return {Array<BindGroup>} The bind groups.
+	 */
+	getForCompute( computeNode ) {
 
-		}
+		const bindings = this.nodes.getForCompute( computeNode ).bindings;
 
 		for ( const bindGroup of bindings ) {
 
@@ -160,7 +160,14 @@ class Bindings extends DataMap {
 	 */
 	deleteForCompute( computeNode ) {
 
-		this._deleteBindings( computeNode );
+		const bindings = this.nodes.getForCompute( computeNode ).bindings;
+
+		for ( const bindGroup of bindings ) {
+
+			this.backend.deleteBindGroupData( bindGroup );
+			this.delete( bindGroup );
+
+		}
 
 	}
 
@@ -171,57 +178,12 @@ class Bindings extends DataMap {
 	 */
 	deleteForRender( renderObject ) {
 
-		this._deleteBindings( renderObject );
-
-	}
-
-	_deleteBindings( object ) {
-
-		const data = this.get( object );
-
-		if ( data.bindings !== undefined ) {
-
-			this._updateBindingsUsage( data.bindings, - 1 );
-
-			data.bindings = undefined;
-
-		}
-
-	}
-
-	_updateBindingsUsage( bindings, delta ) {
+		const bindings = renderObject.getBindings();
 
 		for ( const bindGroup of bindings ) {
 
-			const groupData = this.get( bindGroup );
-
-			groupData.usedTimes = ( groupData.usedTimes || 0 ) + delta;
-
-			for ( const binding of bindGroup.bindings ) {
-
-				if ( binding.isUniformBuffer ) {
-
-					const bindingData = this.get( binding );
-
-					bindingData.usedTimes = ( bindingData.usedTimes || 0 ) + delta;
-
-					if ( bindingData.usedTimes === 0 ) {
-
-						this.backend.destroyUniformBuffer( binding );
-						this.delete( binding );
-
-					}
-
-				}
-
-			}
-
-			if ( groupData.usedTimes === 0 ) {
-
-				this.backend.deleteBindGroupData( bindGroup );
-				this.delete( bindGroup );
-
-			}
+			this.backend.deleteBindGroupData( bindGroup );
+			this.delete( bindGroup );
 
 		}
 
@@ -251,11 +213,7 @@ class Bindings extends DataMap {
 
 		for ( const binding of bindGroup.bindings ) {
 
-			if ( binding.isUniformBuffer ) {
-
-				this.backend.createUniformBuffer( binding );
-
-			} else if ( binding.isSampledTexture ) {
+			if ( binding.isSampledTexture ) {
 
 				this.textures.updateTexture( binding.texture );
 

+ 19 - 40
src/renderers/webgl-fallback/WebGLBackend.js

@@ -1834,9 +1834,24 @@ class WebGLBackend extends Backend {
 			if ( binding.isUniformsGroup || binding.isUniformBuffer ) {
 
 				const array = binding.buffer;
-				const bufferGPU = map.bufferGPU;
+				let { bufferGPU } = this.get( array );
 
-				gl.bindBuffer( gl.UNIFORM_BUFFER, bufferGPU );
+				if ( bufferGPU === undefined ) {
+
+					// create
+
+					bufferGPU = gl.createBuffer();
+
+					gl.bindBuffer( gl.UNIFORM_BUFFER, bufferGPU );
+					gl.bufferData( gl.UNIFORM_BUFFER, array.byteLength, gl.DYNAMIC_DRAW );
+
+					this.set( array, { bufferGPU } );
+
+				} else {
+
+					gl.bindBuffer( gl.UNIFORM_BUFFER, bufferGPU );
+
+				}
 
 				// update
 
@@ -1868,6 +1883,8 @@ class WebGLBackend extends Backend {
 
 				}
 
+				map.bufferGPU = bufferGPU;
+
 				this.set( binding, map );
 
 			} else if ( binding.isSampledTexture ) {
@@ -1934,44 +1951,6 @@ class WebGLBackend extends Backend {
 
 	// attributes
 
-	/**
-	 * Creates a uniform buffer.
-	 *
-	 * @param {Buffer} uniformBuffer - The uniform buffer.
-	 */
-	createUniformBuffer( uniformBuffer ) {
-
-		const uniformBufferData = this.get( uniformBuffer );
-
-		if ( uniformBufferData.bufferGPU === undefined ) {
-
-			const gl = this.gl;
-			const array = uniformBuffer.buffer;
-
-			uniformBufferData.bufferGPU = gl.createBuffer();
-
-			gl.bindBuffer( gl.UNIFORM_BUFFER, uniformBufferData.bufferGPU );
-			gl.bufferData( gl.UNIFORM_BUFFER, array.byteLength, gl.DYNAMIC_DRAW );
-
-		}
-
-	}
-
-	/**
-	 * Destroys the GPU data for the given uniform buffer.
-	 *
-	 * @param {Buffer} uniformBuffer - The uniform buffer.
-	 */
-	destroyUniformBuffer( uniformBuffer ) {
-
-		const uniformBufferData = this.get( uniformBuffer );
-
-		this.gl.deleteBuffer( uniformBufferData.bufferGPU );
-
-		this.delete( uniformBuffer );
-
-	}
-
 	/**
 	 * Creates the GPU buffer of an indexed shader attribute.
 	 *

+ 1 - 65
src/renderers/webgpu/WebGPUBackend.js

@@ -2,7 +2,7 @@
 import 'https://greggman.github.io/webgpu-avoid-redundant-state-setting/webgpu-check-redundant-state-setting.js';
 //*/
 
-import { GPUFeatureName, GPULoadOp, GPUStoreOp, GPUIndexFormat, GPUTextureViewDimension, GPUFeatureMap, GPUShaderStage } from './utils/WebGPUConstants.js';
+import { GPUFeatureName, GPULoadOp, GPUStoreOp, GPUIndexFormat, GPUTextureViewDimension, GPUFeatureMap } from './utils/WebGPUConstants.js';
 
 import WGSLNodeBuilder from './nodes/WGSLNodeBuilder.js';
 import Backend from '../common/Backend.js';
@@ -2178,70 +2178,6 @@ class WebGPUBackend extends Backend {
 
 	// bindings
 
-	/**
-	 * Creates a uniform buffer.
-	 *
-	 * @param {Buffer} uniformBuffer - The uniform buffer.
-	 */
-	createUniformBuffer( uniformBuffer ) {
-
-		const uniformBufferData = this.get( uniformBuffer );
-
-		if ( uniformBufferData.buffer === undefined ) {
-
-			const byteLength = uniformBuffer.byteLength;
-
-			const usage = GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST;
-
-			const visibilities = [];
-
-			if ( uniformBuffer.visibility & GPUShaderStage.VERTEX ) {
-
-				visibilities.push( 'vertex' );
-
-			}
-
-			if ( uniformBuffer.visibility & GPUShaderStage.FRAGMENT ) {
-
-				visibilities.push( 'fragment' );
-
-			}
-
-			if ( uniformBuffer.visibility & GPUShaderStage.COMPUTE ) {
-
-				visibilities.push( 'compute' );
-
-			}
-
-			const bufferVisibility = `(${visibilities.join( ',' )})`;
-
-			const bufferGPU = this.device.createBuffer( {
-				label: `bindingBuffer${uniformBuffer.id}_${uniformBuffer.name}_${bufferVisibility}`,
-				size: byteLength,
-				usage: usage
-			} );
-
-			uniformBufferData.buffer = bufferGPU;
-
-		}
-
-	}
-
-	/**
-	 * Destroys the GPU data for the given uniform buffer.
-	 *
-	 * @param {Buffer} uniformBuffer - The uniform buffer.
-	 */
-	destroyUniformBuffer( uniformBuffer ) {
-
-		const uniformBufferData = this.get( uniformBuffer );
-
-		uniformBufferData.buffer.destroy();
-
-		this.delete( uniformBuffer );
-
-	}
-
 	/**
 	 * Creates bindings from the given bind group definition.
 	 *

+ 37 - 0
src/renderers/webgpu/utils/WebGPUBindingUtils.js

@@ -283,6 +283,43 @@ class WebGPUBindingUtils {
 
 				const bindingData = backend.get( binding );
 
+				if ( bindingData.buffer === undefined ) {
+
+					const byteLength = binding.byteLength;
+
+					const usage = GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST;
+
+					const visibilities = [];
+					if ( binding.visibility & GPUShaderStage.VERTEX ) {
+
+						visibilities.push( 'vertex' );
+
+					}
+
+					if ( binding.visibility & GPUShaderStage.FRAGMENT ) {
+
+						visibilities.push( 'fragment' );
+
+					}
+
+					if ( binding.visibility & GPUShaderStage.COMPUTE ) {
+
+						visibilities.push( 'compute' );
+
+					}
+
+					const bufferVisibility = `(${visibilities.join( ',' )})`;
+
+					const bufferGPU = device.createBuffer( {
+						label: `bindingBuffer${binding.id}_${binding.name}_${bufferVisibility}`,
+						size: byteLength,
+						usage: usage
+					} );
+
+					bindingData.buffer = bufferGPU;
+
+				}
+
 				entriesGPU.push( { binding: bindingPoint, resource: { buffer: bindingData.buffer } } );
 
 			} else if ( binding.isStorageBuffer ) {

粤ICP备19079148号