Skip to content

Commit

Permalink
fix(VertexState): Don't throw when preferred shader locations matches…
Browse files Browse the repository at this point in the history
… the vertex state (#950)
  • Loading branch information
jespertheend authored Aug 22, 2024
1 parent 10182d8 commit 3e44b64
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/rendering/VertexState.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ export class VertexState {

for (const buffer of this.buffers) {
for (const attribute of buffer.attributes) {
const loc = preferredShaderLocationsMap.get(attribute.attributeType);
if (loc !== undefined) {
if (takenShaderLocations.has(loc)) {
throw new Error(`Preferred shader location ${loc} is already taken by an attribute in the VertexState.`);
const location = preferredShaderLocationsMap.get(attribute.attributeType);
if (location !== undefined && location != attribute.shaderLocation) {
if (takenShaderLocations.has(location)) {
throw new Error(`Preferred shader location ${location} is already taken by an attribute in the VertexState.`);
}
takenShaderLocations.add(loc);
takenShaderLocations.add(location);
}
}
}
Expand Down
82 changes: 82 additions & 0 deletions test/unit/src/rendering/VertexState.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,88 @@ Deno.test({
},
});

Deno.test({
name: "getDescriptor() prioritizes the shader location from the vertex state over that of the preferredShaderLocations",
fn() {
/** @type {import("../../../../src/rendering/VertexState.js").VertexStateOptions} */
const options = {
buffers: [
{
attributes: [
{
attributeType: Mesh.AttributeType.POSITION,
componentCount: 3,
format: Mesh.AttributeFormat.FLOAT32,
shaderLocation: 1,
},
{
attributeType: Mesh.AttributeType.COLOR,
componentCount: 3,
format: Mesh.AttributeFormat.FLOAT32,
shaderLocation: 2,
},
],
},
],
};
const vertexState = new VertexState(options);

const result = vertexState.getDescriptor({
preferredShaderLocations: [
{ attributeType: Mesh.AttributeType.POSITION, location: 3 },
{ attributeType: Mesh.AttributeType.COLOR, location: 4 },
],
});

assertEquals(result.buffers.length, 1);
const attributes = /** @type {GPUVertexAttribute[]} */ (result.buffers[0].attributes);
assertEquals(attributes.length, 2);
assertEquals(attributes[0].shaderLocation, 1);
assertEquals(attributes[1].shaderLocation, 2);
},
});

Deno.test({
name: "getDescriptor() doesn't throw when preferred shader locations are the same as that in the vertexstate",
fn() {
/** @type {import("../../../../src/rendering/VertexState.js").VertexStateOptions} */
const options = {
buffers: [
{
attributes: [
{
attributeType: Mesh.AttributeType.POSITION,
componentCount: 3,
format: Mesh.AttributeFormat.FLOAT32,
shaderLocation: 1,
},
{
attributeType: Mesh.AttributeType.COLOR,
componentCount: 3,
format: Mesh.AttributeFormat.FLOAT32,
shaderLocation: 2,
},
],
},
],
};
const vertexState = new VertexState(options);

const result = vertexState.getDescriptor({
preferredShaderLocations: [
{ attributeType: Mesh.AttributeType.POSITION, location: 1 },
{ attributeType: Mesh.AttributeType.COLOR, location: 2 },
],
});

assertEquals(result.buffers.length, 1);
const attributes = /** @type {GPUVertexAttribute[]} */ (result.buffers[0].attributes);
assertEquals(attributes.length, 2);
assertEquals(attributes[0].shaderLocation, 1);
assertEquals(attributes[1].shaderLocation, 2);
},
});

Deno.test({
name: "getDescriptor() uses the first available shader location for automatic shader location attributes",
fn() {
Expand Down

0 comments on commit 3e44b64

Please sign in to comment.