Skip to content

WebGPURenderer: BatchedMesh via drawIndexedIndirect #30645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 80 additions & 4 deletions src/nodes/accessors/BatchNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { textureSize } from './TextureSizeNode.js';
import { tangentLocal } from './Tangent.js';
import { instanceIndex, drawIndex } from '../core/IndexNode.js';
import { varyingProperty } from '../core/PropertyNode.js';
import { NodeUpdateType } from '../core/constants.js';
import IndirectStorageBufferAttribute from '../../renderers/common/IndirectStorageBufferAttribute.js';

/**
* This node implements the vertex shader logic which is required
Expand Down Expand Up @@ -46,6 +48,14 @@ class BatchNode extends Node {
* @default null
*/
this.batchingIdNode = null;
this.updateBeforeType = NodeUpdateType.FRAME;

/**
* A reference of the indirect version to prevent unnecessary updates.
* @type {Number}
* @default 0
*/
this.indirectVersion = 0;

}

Expand All @@ -72,12 +82,43 @@ class BatchNode extends Node {

}


if ( builder.isFlipY() === false ) {

const object = this.batchMesh;
const geometry = object.geometry;

const uint32 = new Uint32Array( 5 * object._maxInstanceCount );
const starts = object._multiDrawStarts;
const counts = object._multiDrawCounts;
const drawCount = object._multiDrawCount;
const drawInstances = object._multiDrawInstances;

for ( let i = 0; i < drawCount; i ++ ) {

const count = drawInstances ? drawInstances[ i ] : 1;

uint32[ i * 5 ] = counts[ i ]; // indexCount
uint32[ i * 5 + 1 ] = count; // instanceCount
uint32[ i * 5 + 2 ] = starts[ i ] / object.geometry.index.array.BYTES_PER_ELEMENT; // firstIndex
uint32[ i * 5 + 3 ] = 0; // baseVertex
uint32[ i * 5 + 4 ] = i; // firstInstance

}

const indirectAttribute = new IndirectStorageBufferAttribute( uint32, 5 );
geometry.setIndirect( indirectAttribute );

}

const getIndirectIndex = Fn( ( [ id ] ) => {

const size = int( textureSize( textureLoad( this.batchMesh._indirectTexture ), 0 ) );
const x = int( id ).modInt( size );
const y = int( id ).div( size );
return textureLoad( this.batchMesh._indirectTexture, ivec2( x, y ) ).x;
const size = textureLoad( this.batchMesh._indirectTexture ).size( 0 ).x.toInt().toConst( 'size' );
const x = int( id ).modInt( size ).toConst( 'x' );
const y = int( id ).div( size ).toConst( 'y' );
const index = textureLoad( this.batchMesh._indirectTexture, ivec2( x, y ) ).x.toFloat().toConst( 'index' );

return index;

} ).setLayout( {
name: 'getIndirectIndex',
Expand Down Expand Up @@ -148,6 +189,41 @@ class BatchNode extends Node {

}

updateBefore() {

const object = this.batchMesh;

const indirect = object.geometry.getIndirect();

if ( indirect !== null && object._indirectTexture.version > this.indirectVersion ) {

const uint32 = new Uint32Array( 5 * object._maxInstanceCount );
const starts = object._multiDrawStarts;
const counts = object._multiDrawCounts;
const drawCount = object._multiDrawCount;
const drawInstances = object._multiDrawInstances;

for ( let i = 0; i < drawCount; i ++ ) {

const count = drawInstances ? drawInstances[ i ] : 1;

uint32[ i * 5 ] = counts[ i ]; // indexCount
uint32[ i * 5 + 1 ] = count; // instanceCount
uint32[ i * 5 + 2 ] = starts[ i ] / object.geometry.index.array.BYTES_PER_ELEMENT; // firstIndex
uint32[ i * 5 + 3 ] = 0; // baseVertex
uint32[ i * 5 + 4 ] = i; // firstInstance

}

indirect.array = uint32;
indirect.needsUpdate = true;

this.indirectVersion = object._indirectTexture.version;

}

}

}

export default BatchNode;
Expand Down
23 changes: 21 additions & 2 deletions src/renderers/webgpu/WebGPUBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,8 @@ class WebGPUBackend extends Backend {

const draw = () => {

const indirect = renderObject.getIndirect();

if ( object.isBatchedMesh === true ) {

const starts = object._multiDrawStarts;
Expand All @@ -1263,14 +1265,32 @@ class WebGPUBackend extends Backend {

}

let indirectBuffer = null;

if ( indirect !== null ) {

indirectBuffer = this.get( indirect ).buffer;

}

for ( let i = 0; i < drawCount; i ++ ) {

const count = drawInstances ? drawInstances[ i ] : 1;
const firstInstance = count > 1 ? 0 : i;

if ( hasIndex === true ) {

passEncoderGPU.drawIndexed( counts[ i ], count, starts[ i ] / index.array.BYTES_PER_ELEMENT, 0, firstInstance );

if ( indirect !== null ) {

passEncoderGPU.drawIndexedIndirect( indirectBuffer, i * 5 * Uint32Array.BYTES_PER_ELEMENT );

} else {

passEncoderGPU.drawIndexed( counts[ i ], count, starts[ i ] / index.array.BYTES_PER_ELEMENT, 0, firstInstance );

}


} else {

Expand All @@ -1286,7 +1306,6 @@ class WebGPUBackend extends Backend {

const { vertexCount: indexCount, instanceCount, firstVertex: firstIndex } = drawParams;

const indirect = renderObject.getIndirect();

if ( indirect !== null ) {

Expand Down
Loading