diff --git a/examples/jsm/renderers/webgpu/WebGPURenderPipelines.js b/examples/jsm/renderers/webgpu/WebGPURenderPipelines.js index 05aaf6aa0986c3..60c43b7bc44610 100644 --- a/examples/jsm/renderers/webgpu/WebGPURenderPipelines.js +++ b/examples/jsm/renderers/webgpu/WebGPURenderPipelines.js @@ -15,6 +15,8 @@ class WebGPURenderPipelines { fragment: new WeakMap() }; + this.shaderAttributes = new WeakMap(); + } get( object ) { @@ -90,6 +92,7 @@ class WebGPURenderPipelines { // vertex buffers const vertexBuffers = []; + const shaderAttributes = []; // Find "layout (location = num) in type name" in vertex shader const regex = /^\s*layout\s*\(\s*location\s*=\s*(?[0-9]+)\s*\)\s*in\s+(?\w+)\s+(?\w+)\s*;/gmi; @@ -101,6 +104,8 @@ class WebGPURenderPipelines { const arrayStride = this._getArrayStride( shaderAttribute.groups.type ); const vertexFormat = this._getVertexFormat( shaderAttribute.groups.type ); + shaderAttributes.push( { name: shaderAttribute.groups.name, slot: shaderLocation } ); + vertexBuffers.push( { arrayStride: arrayStride, attributes: [ { shaderLocation: shaderLocation, offset: 0, format: vertexFormat } ] @@ -143,6 +148,8 @@ class WebGPURenderPipelines { } ); this.pipelines.set( object, pipeline ); + this.shaderAttributes.set( pipeline, shaderAttributes ); + } @@ -150,6 +157,12 @@ class WebGPURenderPipelines { } + getShaderAttributes( pipeline ) { + + return this.shaderAttributes.get( pipeline ); + + } + dispose() { this.pipelines = new WeakMap(); @@ -254,8 +267,7 @@ const ShaderLib = { vertexShader: `#version 450 layout(location = 0) in vec3 position; - layout(location = 1) in vec3 normal; - layout(location = 2) in vec2 uv; + layout(location = 1) in vec2 uv; layout(location = 0) out vec2 vUv; diff --git a/examples/jsm/renderers/webgpu/WebGPURenderer.js b/examples/jsm/renderers/webgpu/WebGPURenderer.js index 1e997786b2c474..d81c48936996fb 100644 --- a/examples/jsm/renderers/webgpu/WebGPURenderer.js +++ b/examples/jsm/renderers/webgpu/WebGPURenderer.js @@ -559,13 +559,13 @@ class WebGPURenderer { if ( hasIndex === true ) { - this._setupIndexBuffer( passEncoder, index ); + this._setupIndexBuffer( index, passEncoder ); } // vertex buffers - this._setupVertexBuffers( passEncoder, geometry.attributes ); + this._setupVertexBuffers( geometry.attributes, passEncoder, pipeline ); // draw @@ -593,7 +593,7 @@ class WebGPURenderer { } - _setupIndexBuffer( encoder, index ) { + _setupIndexBuffer( index, encoder ) { const buffer = this._attributes.get( index ).buffer; const indexFormat = ( index.array instanceof Uint16Array ) ? GPUIndexFormat.Uint16 : GPUIndexFormat.Uint32; @@ -602,18 +602,23 @@ class WebGPURenderer { } - _setupVertexBuffers( encoder, geometryAttributes ) { + _setupVertexBuffers( geometryAttributes, encoder, pipeline ) { - let slot = 0; + const shaderAttributes = this._renderPipelines.getShaderAttributes( pipeline ); - for ( const name in geometryAttributes ) { + for ( const shaderAttribute of shaderAttributes ) { + + const name = shaderAttribute.name; + const slot = shaderAttribute.slot; const attribute = geometryAttributes[ name ]; - const buffer = this._attributes.get( attribute ).buffer; - encoder.setVertexBuffer( slot, buffer ); + if ( attribute !== undefined ) { + + const buffer = this._attributes.get( attribute ).buffer; + encoder.setVertexBuffer( slot, buffer ); - slot ++; + } }