Skip to content

Commit

Permalink
WebGPURenderer: Fix vertex buffer setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mugen87 committed Sep 6, 2020
1 parent 91e38e7 commit 1e36728
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
16 changes: 14 additions & 2 deletions examples/jsm/renderers/webgpu/WebGPURenderPipelines.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class WebGPURenderPipelines {
fragment: new WeakMap()
};

this.shaderAttributes = new WeakMap();

}

get( object ) {
Expand Down Expand Up @@ -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*(?<location>[0-9]+)\s*\)\s*in\s+(?<type>\w+)\s+(?<name>\w+)\s*;/gmi;
Expand All @@ -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 } ]
Expand Down Expand Up @@ -143,13 +148,21 @@ class WebGPURenderPipelines {
} );

this.pipelines.set( object, pipeline );
this.shaderAttributes.set( pipeline, shaderAttributes );


}

return pipeline;

}

getShaderAttributes( pipeline ) {

return this.shaderAttributes.get( pipeline );

}

dispose() {

this.pipelines = new WeakMap();
Expand Down Expand Up @@ -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;
Expand Down
23 changes: 14 additions & 9 deletions examples/jsm/renderers/webgpu/WebGPURenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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;
Expand All @@ -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 ++;
}

}

Expand Down

0 comments on commit 1e36728

Please sign in to comment.