Skip to content
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

WebGPURenderer: instance mesh use binding group instead of attribute #28726

Merged
merged 7 commits into from
Jul 19, 2024
Merged
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
49 changes: 34 additions & 15 deletions src/nodes/accessors/InstanceNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { normalLocal } from './NormalNode.js';
import { positionLocal } from './PositionNode.js';
import { nodeProxy, vec3, mat3, mat4 } from '../shadernode/ShaderNode.js';
import { NodeUpdateType } from '../core/constants.js';
import { buffer } from '../accessors/BufferNode.js';
import { instanceIndex } from '../core/IndexNode.js';

import { InstancedInterleavedBuffer } from '../../core/InstancedInterleavedBuffer.js';
import { InstancedBufferAttribute } from '../../core/InstancedBufferAttribute.js';
Expand Down Expand Up @@ -32,40 +34,57 @@ class InstanceNode extends Node {
setup( /*builder*/ ) {

let instanceMatrixNode = this.instanceMatrixNode;
let instanceColorNode = this.instanceColorNode;

const instanceMesh = this.instanceMesh;

if ( instanceMatrixNode === null ) {

const instanceAttribute = instanceMesh.instanceMatrix;
const buffer = new InstancedInterleavedBuffer( instanceAttribute.array, 16, 1 );

this.buffer = buffer;
const bufferFn = instanceAttribute.usage === DynamicDrawUsage ? instancedDynamicBufferAttribute : instancedBufferAttribute;
// Both WebGPU and WebGL backends have UBO max limited to 64kb. Matrix count number bigger than 1000 ( 16 * 4 * 1000 = 64kb ) will fallback to attribute.

const instanceBuffers = [
// F.Signature -> bufferAttribute( array, type, stride, offset )
bufferFn( buffer, 'vec4', 16, 0 ),
bufferFn( buffer, 'vec4', 16, 4 ),
bufferFn( buffer, 'vec4', 16, 8 ),
bufferFn( buffer, 'vec4', 16, 12 )
];
if ( instanceMesh.count <= 1000 ) {

instanceMatrixNode = mat4( ...instanceBuffers );
instanceMatrixNode = buffer( instanceAttribute.array, 'mat4', instanceMesh.count ).element( instanceIndex );

} else {

const buffer = new InstancedInterleavedBuffer( instanceAttribute.array, 16, 1 );

this.buffer = buffer;

const bufferFn = instanceAttribute.usage === DynamicDrawUsage ? instancedDynamicBufferAttribute : instancedBufferAttribute;

const instanceBuffers = [
// F.Signature -> bufferAttribute( array, type, stride, offset )
bufferFn( buffer, 'vec4', 16, 0 ),
bufferFn( buffer, 'vec4', 16, 4 ),
bufferFn( buffer, 'vec4', 16, 8 ),
bufferFn( buffer, 'vec4', 16, 12 )
];

instanceMatrixNode = mat4( ...instanceBuffers );

}

this.instanceMatrixNode = instanceMatrixNode;

}

const instanceColorAttribute = instanceMesh.instanceColor;

if ( instanceColorAttribute && this.instanceColorNode === null ) {
if ( instanceColorAttribute && instanceColorNode === null ) {

const buffer = new InstancedBufferAttribute( instanceColorAttribute.array, 3 );

const bufferFn = instanceColorAttribute.usage === DynamicDrawUsage ? instancedDynamicBufferAttribute : instancedBufferAttribute;

this.bufferColor = buffer;
this.instanceColorNode = vec3( bufferFn( buffer, 'vec3', 3, 0 ) );

instanceColorNode = vec3( bufferFn( buffer, 'vec3', 3, 0 ) );

this.instanceColorNode = instanceColorNode;

}

Expand Down Expand Up @@ -98,13 +117,13 @@ class InstanceNode extends Node {

update( /*frame*/ ) {

if ( this.instanceMesh.instanceMatrix.usage !== DynamicDrawUsage && this.instanceMesh.instanceMatrix.version !== this.buffer.version ) {
if ( this.instanceMesh.instanceMatrix.usage !== DynamicDrawUsage && this.buffer != null && this.instanceMesh.instanceMatrix.version !== this.buffer.version ) {

this.buffer.version = this.instanceMesh.instanceMatrix.version;

}

if ( this.instanceMesh.instanceColor && this.instanceMesh.instanceColor.usage !== DynamicDrawUsage && this.instanceMesh.instanceColor.version !== this.bufferColor.version ) {
if ( this.instanceMesh.instanceColor && this.instanceMesh.instanceColor.usage !== DynamicDrawUsage && this.bufferColor != null && this.instanceMesh.instanceColor.version !== this.bufferColor.version ) {

this.bufferColor.version = this.instanceMesh.instanceColor.version;

Expand Down