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

fix(VertexState): Don't throw when preferred shader locations matches the vertex state #950

Merged
Merged
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
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
Loading