-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
maybe this would be better else where?
- Loading branch information
Showing
2 changed files
with
224 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
webgpu/webgpu-fragment-shader-builtin-position-separate-modules.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>WebGPU Fragment Shader using @builtin(position)</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 vsModule = device.createShaderModule({ | ||
label: 'hardcoded triangle', | ||
code: ` | ||
struct OurVertexShaderOutput { | ||
@builtin(position) position: vec4f, | ||
}; | ||
@vertex fn vs( | ||
@builtin(vertex_index) vertexIndex : u32 | ||
) -> OurVertexShaderOutput { | ||
let pos = array( | ||
vec2f( 0.0, 0.5), // top center | ||
vec2f(-0.5, -0.5), // bottom left | ||
vec2f( 0.5, -0.5) // bottom right | ||
); | ||
var vsOutput: OurVertexShaderOutput; | ||
vsOutput.position = vec4f(pos[vertexIndex], 0.0, 1.0); | ||
return vsOutput; | ||
} | ||
`, | ||
}); | ||
|
||
const fsModule = device.createShaderModule({ | ||
label: 'checkerboard', | ||
code: ` | ||
@fragment fn fs(@builtin(position) pixelPosition: vec4f) -> @location(0) vec4f { | ||
let red = vec4f(1, 0, 0, 1); | ||
let cyan = vec4f(0, 1, 1, 1); | ||
let grid = vec2u(pixelPosition.xy) / 8; | ||
let checker = (grid.x + grid.y) % 2 == 1; | ||
return select(red, cyan, checker); | ||
} | ||
`, | ||
}); | ||
|
||
const pipeline = device.createRenderPipeline({ | ||
label: 'hardcoded checkerboard triangle pipeline', | ||
layout: 'auto', | ||
vertex: { | ||
module: vsModule, | ||
entryPoint: 'vs', | ||
}, | ||
fragment: { | ||
module: fsModule, | ||
entryPoint: 'fs', | ||
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.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)); | ||
// re-render | ||
render(); | ||
} | ||
}); | ||
observer.observe(canvas); | ||
} | ||
|
||
function fail(msg) { | ||
// eslint-disable-next-line no-alert | ||
alert(msg); | ||
} | ||
|
||
main(); | ||
</script> | ||
</html> |