mirror of
https://github.com/webgpu/webgpufundamentals.git
synced 2026-11-30 22:11:18 -06:00
Adding a mini table of contents of ways to get data into a shader. This was one missing technique.
153 lines
4.2 KiB
HTML
153 lines
4.2 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
|
|
<title>WebGPU Misc - Instance Index as Input</title>
|
|
<style>
|
|
@import url(resources/webgpu-lesson.css);
|
|
|
|
html,
|
|
body {
|
|
margin: 0;
|
|
/* remove the default margin */
|
|
height: 100%;
|
|
/* make the html,body fill the page */
|
|
}
|
|
|
|
canvas {
|
|
display: block;
|
|
/* make the canvas act like a block */
|
|
width: 100%;
|
|
/* make the canvas fill its container */
|
|
height: 100%;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<canvas></canvas>
|
|
</body>
|
|
<script type="module">
|
|
async function main() {
|
|
const adapter = await navigator.gpu?.requestAdapter();
|
|
const device = await adapter?.requestDevice();
|
|
if (!device) {
|
|
fail('need a browser that supports WebGPU');
|
|
return;
|
|
}
|
|
|
|
// Get a WebGPU context from the canvas and configure it
|
|
const canvas = document.querySelector('canvas');
|
|
const context = canvas.getContext('webgpu');
|
|
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
|
|
context.configure({
|
|
device,
|
|
format: presentationFormat,
|
|
});
|
|
|
|
const module = device.createShaderModule({
|
|
label: 'our hardcoded red triangle shaders',
|
|
code: /* wgsl */ `
|
|
struct VertexOut {
|
|
@builtin(position) pos: vec4f,
|
|
@location(0) @interpolate(flat, either) colorNdx: u32,
|
|
};
|
|
|
|
@vertex fn vs(
|
|
@builtin(vertex_index) vertexIndex : u32,
|
|
@builtin(instance_index) instanceIndex: u32,
|
|
) -> VertexOut {
|
|
let pos = array(
|
|
vec2f( 0.0, 0.5), // top center
|
|
vec2f(-0.5, -0.5), // bottom left
|
|
vec2f( 0.5, -0.5) // bottom right
|
|
);
|
|
let offsets = array(
|
|
vec2f( 0.0, 0.5), // top middle
|
|
vec2f(-0.5, -0.5), // left bottom
|
|
vec2f( 0.5, -0.5), // right bottom
|
|
);
|
|
|
|
return VertexOut(
|
|
vec4f(pos[vertexIndex] + offsets[instanceIndex], 0.0, 1.0),
|
|
instanceIndex,
|
|
);
|
|
}
|
|
|
|
@fragment fn fs(in: VertexOut) -> @location(0) vec4f {
|
|
let colors = array(
|
|
vec4f(1, 1, 0, 1), // yellow
|
|
vec4f(0, 1, 1, 1), // cyan
|
|
vec4f(1, 0, 1, 1), // magenta
|
|
);
|
|
return colors[in.colorNdx];
|
|
}
|
|
`,
|
|
});
|
|
|
|
const pipeline = device.createRenderPipeline({
|
|
label: 'our hardcoded red triangle pipeline',
|
|
layout: 'auto',
|
|
vertex: {
|
|
module,
|
|
},
|
|
fragment: {
|
|
module,
|
|
targets: [{ format: presentationFormat }],
|
|
},
|
|
});
|
|
|
|
const renderPassDescriptor = {
|
|
label: 'our basic canvas renderPass',
|
|
colorAttachments: [
|
|
{
|
|
// view: <- to be filled out when we render
|
|
clearValue: [0.3, 0.3, 0.3, 1],
|
|
loadOp: 'clear',
|
|
storeOp: 'store',
|
|
},
|
|
],
|
|
};
|
|
|
|
function render() {
|
|
// Get the current texture from the canvas context and
|
|
// set it as the texture to render to.
|
|
renderPassDescriptor.colorAttachments[0].view =
|
|
context.getCurrentTexture().createView();
|
|
|
|
const encoder = device.createCommandEncoder({ label: 'our encoder' });
|
|
const pass = encoder.beginRenderPass(renderPassDescriptor);
|
|
pass.setPipeline(pipeline);
|
|
pass.draw(3); // call our vertex shader 3 times
|
|
pass.draw(3, 1, 0, 1); // pass 1 for instance_index
|
|
pass.draw(3, 1, 0, 2); // pass 2 for instance_index
|
|
pass.end();
|
|
|
|
const commandBuffer = encoder.finish();
|
|
device.queue.submit([commandBuffer]);
|
|
}
|
|
|
|
const observer = new ResizeObserver(entries => {
|
|
for (const entry of entries) {
|
|
const canvas = entry.target;
|
|
const width = entry.contentBoxSize[0].inlineSize;
|
|
const height = entry.contentBoxSize[0].blockSize;
|
|
canvas.width = Math.max(1, Math.min(width, device.limits.maxTextureDimension2D));
|
|
canvas.height = Math.max(1, Math.min(height, device.limits.maxTextureDimension2D));
|
|
}
|
|
render();
|
|
});
|
|
observer.observe(canvas);
|
|
}
|
|
|
|
function fail(msg) {
|
|
// eslint-disable-next-line no-alert
|
|
alert(msg);
|
|
}
|
|
|
|
main();
|
|
</script>
|
|
|
|
</html> |