Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 5 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions sample/primitivePicking/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ const canvas = document.querySelector('canvas') as HTMLCanvasElement;
const adapter = await navigator.gpu?.requestAdapter({
featureLevel: 'compatibility',
});
//@ts-expect-error primitive-index is not yet part of the Typescript definitions.

const requiredFeatures: Array<GPUFeatureName> = ['primitive-index'];
Comment thread
toji marked this conversation as resolved.
Outdated
Comment thread
toji marked this conversation as resolved.
Outdated
quitIfFeaturesNotAvailable(adapter, requiredFeatures);
const device = await adapter?.requestDevice({

const device = await adapter.requestDevice({
requiredFeatures,
});
quitIfWebGPUNotAvailable(adapter, device);
Comment thread
toji marked this conversation as resolved.
Expand All @@ -34,7 +35,7 @@ context.configure({
});

// Create the model vertex buffer.
const kVertexStride = 8;
const kVertexStride = 6;
const vertexBuffer = device.createBuffer({
// position: vec3, normal: vec3
size: mesh.positions.length * kVertexStride * Float32Array.BYTES_PER_ELEMENT,
Expand Down Expand Up @@ -82,7 +83,7 @@ const depthTexture = device.createTexture({

const vertexBuffers: Iterable<GPUVertexBufferLayout> = [
{
arrayStride: Float32Array.BYTES_PER_ELEMENT * 8,
arrayStride: Float32Array.BYTES_PER_ELEMENT * kVertexStride,
attributes: [
{
// position
Expand Down Expand Up @@ -242,16 +243,16 @@ const gui = new GUI();
gui.add(settings, 'mode', ['rendering', 'primitive indexes']);
gui.add(settings, 'rotate');

const MatrixSizeBytes = Float32Array.BYTES_PER_ELEMENT * 16;
const PickUniformsSizeBytes = Float32Array.BYTES_PER_ELEMENT * 4;
const kMatrixSizeBytes = Float32Array.BYTES_PER_ELEMENT * 16;
const kPickUniformsSizeBytes = Float32Array.BYTES_PER_ELEMENT * 4;

const modelUniformBuffer = device.createBuffer({
size: MatrixSizeBytes * 2, // two 4x4 matrix
size: kMatrixSizeBytes * 2, // two 4x4 matrix
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
});

const frameUniformBuffer = device.createBuffer({
size: MatrixSizeBytes * 2 + PickUniformsSizeBytes, // two 4x4 matrix + a vec4's worth of picking uniforms
size: kMatrixSizeBytes * 2 + kPickUniformsSizeBytes, // two 4x4 matrix + a vec4's worth of picking uniforms
usage:
GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST | GPUBufferUsage.STORAGE,
});
Expand Down Expand Up @@ -408,7 +409,7 @@ function frame() {
}
}
{
// Picking pass. Executes a single instance of a compue shader that loads
// Picking pass. Executes a single instance of a compute shader that loads
// the primitive index at the pointer coordinates from the primitive index
// texture written in the forward pass. The selected primitive index is
// saved in the frameUniformBuffer and used for highlighting on the next
Expand Down
2 changes: 1 addition & 1 deletion sample/primitivePicking/meta.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export default {
name: 'Primitive Picking',
description: `This example demonstrates use of the primitive_index builtin.
description: `This example demonstrates use of the primitive_index WGSL builtin.
It is used to render a unique ID for each primitive to a buffer, which is
then read at the current cursor/touch location to determine which primitive
has been selected. That primitive is then highlighted when rendering the
Expand Down
13 changes: 10 additions & 3 deletions sample/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,22 @@ export function quitIfLimitLessThan(
}
}

/**
* Shows an error dialog if getting an adapter wasn't successful or the adapter
* does not support the given list of features.
*/
export function quitIfFeaturesNotAvailable(
adapter: GPUAdapter,
adapter: GPUAdapter | null,
requiredFeatures: GPUFeatureName[]
) {
): asserts adapter {
quitIfAdapterNotAvailable(adapter);

for (const feature of requiredFeatures) {
if (!adapter.features.has(feature)) {
fail(
Comment thread
toji marked this conversation as resolved.
`This sample requries the '${feature}' feature, which is not supported by this system.`
`This sample requires the '${feature}' feature, which is not supported by this system.`
);
return;
}
}
}
Expand Down