Skip to content

Commit

Permalink
handle bool
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Nov 18, 2023
1 parent cb61348 commit 693e33b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/attribute-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ export type ArrayUnion = number | number[] | TypedArray | FullArraySpec;
* Each array can be 1 of 4 things. A native JavaScript array, a TypedArray, a number, a {@link FullArraySpec}
*
* If it's a native array then, if the name of the array is `indices` the data will be converted
* to a `Uint32Array`, otherwise a `Float32Array. Use a TypedArray or a FullArraySpec to choose a different type.
* The FullArraySpec type is only used if it's not already a TypedArray
* to a `Uint32Array`, otherwise a `Float32Array`. Use a TypedArray or a {@link FullArraySpec} to choose a different type.
* The {@link FullArraySpec} `type` is only used if it's not already a TypedArray
*
* If it's a native array or a TypedArray or if `numComponents` in a {@link FullArraySpec} is not
* specified it will be guess. If the name contains 'coord', 'texture' or 'uv' then numComponents will be 2.
* specified it will be guessed. If the name contains 'coord', 'texture' or 'uv' then numComponents will be 2.
* If the name contains 'color' or 'colour' then numComponents will be 4. Otherwise it's 3.
*
* For attribute formats, guesses are made based on type at number of components. The guess is
* For attribute formats, guesses are made based on type and number of components. The guess is
* based on this table where (d) is the default for that type if `normalize` is not specified
*
* | Type | .. | normalize |
Expand Down
5 changes: 5 additions & 0 deletions src/buffer-views.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ const b: Record<string, TypeDef> = {
mat3x4h: { numElements: 12, align: 8, size: 24, pad: [3, 1], type: 'u16', View: Uint16Array },
mat4x4f: { numElements: 16, align: 16, size: 64, type: 'f32', View: Float32Array },
mat4x4h: { numElements: 16, align: 8, size: 32, type: 'u16', View: Uint16Array },

// Note: At least as of WGSL V1 you can not create a bool for uniform or storage.
// You can only create one in an internal struct. But, this code generates
// views of structs and it needs to not fail if the struct has a bool
bool: { numElements: 0, align: 1, size: 0, type: 'bool', View: Uint32Array },
};

const typeInfo: Record<string, TypeDef> = {
Expand Down
35 changes: 35 additions & 0 deletions test/tests/buffer-views-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -897,4 +897,39 @@ describe('buffer-views-tests', () => {

});

// Note: At least as of WGSL V1 you can not create a bool for uniform or storage.
// You can only create one in an internal struct. But, this code generates
// views of structs and it needs to not fail if the struct has a bool
it('generates handles bool', () => {
const shader = `
struct Test {
a: u32,
b: bool,
c: u32,
};
`;
const defs = makeShaderDataDefinitions(shader).structs;
const {views, arrayBuffer} = makeStructuredView(defs.Test);
assertEqual(arrayBuffer.byteLength, 8);
assertEqual(views.a.byteOffset, 0);
assertEqual(views.c.byteOffset, 4);
});

/* wgsl_reflect returns bad data for this case. See comment above though.
it('generates handles bool array', () => {
const shader = `
struct Test {
a: u32,
b: array<bool, 3>,
c: u32,
};
`;
const defs = makeShaderDataDefinitions(shader).structs;
const {views, arrayBuffer} = makeStructuredView(defs.Test);
assertEqual(arrayBuffer.byteLength, 8);
assertEqual(views.a.byteOffset, 0);
assertEqual(views.c.byteOffset, 4);
});
*/

});
1 change: 1 addition & 0 deletions test/tests/data-definition-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe('data-definition-tests', () => {
foo: u32,
bar: f32,
moo: vec3f,
brp: bool, // bool is not valid in WGSL 1 for storage/uniforms but can appear internally
mrp: i32,
};
@group(4) @binding(1) var<uniform> uni1: VSUniforms;
Expand Down

0 comments on commit 693e33b

Please sign in to comment.