-
-
Notifications
You must be signed in to change notification settings - Fork 10
feat: Introduce Index buffers and improve buffer UX #1340
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
Open
reczkok
wants to merge
19
commits into
main
Choose a base branch
from
feat/index-buffers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
8d7546a
initial
reczkok 8df90be
Add u16 support to data IO and compiled IO modules
reczkok 59a0bf7
Add square example and improve index buffer support
reczkok b784a96
Update thumbnail.png
reczkok 741e69d
Add tag
reczkok 2e2a265
Add tests for u16 and bool schema restrictions and index buffer usage
reczkok 17dd33a
Support decorated types in buffer compatibility checks
reczkok e7999d5
Thanks copilot
reczkok 0e9b73f
remove funny
reczkok 9b66052
Merge branch 'main' into feat/index-buffers
reczkok 3573a4c
Make pipeline methods return correct subtype with this typing
reczkok 7994623
Improve buffer tests with type-level error assertions
reczkok e6ced22
Add tests for drawIndexed usage and index buffer assignment
reczkok c605d33
Add test for combining timestamp writes and index buffer
reczkok 41b717e
Add test for timestamp writes, index buffer, and perf callback
reczkok 66ca591
deno did not even try to save it
reczkok 93345f0
Merge branch 'main' into feat/index-buffers
reczkok 4a29063
cleanup in square example
reczkok bb01878
Fix import
reczkok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
apps/typegpu-docs/src/content/examples/simple/square/index.html
This file contains hidden or 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 @@ | ||
<canvas></canvas> |
128 changes: 128 additions & 0 deletions
128
apps/typegpu-docs/src/content/examples/simple/square/index.ts
This file contains hidden or 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,128 @@ | ||
import tgpu from 'typegpu'; | ||
import * as d from 'typegpu/data'; | ||
|
||
const presentationFormat = navigator.gpu.getPreferredCanvasFormat(); | ||
const canvas = document.querySelector('canvas') as HTMLCanvasElement; | ||
const context = canvas.getContext('webgpu') as GPUCanvasContext; | ||
|
||
const root = await tgpu.init(); | ||
|
||
context.configure({ | ||
device: root.device, | ||
format: presentationFormat, | ||
alphaMode: 'premultiplied', | ||
}); | ||
|
||
const colors = { | ||
bottomLeft: d.vec4f(1, 0, 0, 1), | ||
bottomRight: d.vec4f(0, 1, 0, 1), | ||
topRight: d.vec4f(0, 0, 1, 1), | ||
topLeft: d.vec4f(1, 1, 0, 1), | ||
}; | ||
|
||
const colorIndices = { | ||
bottomLeft: 0, | ||
bottomRight: 1, | ||
topRight: 2, | ||
topLeft: 3, | ||
} as const; | ||
|
||
const colorBuffer = root | ||
.createBuffer(d.arrayOf(d.vec4f, 4), Object.values(colors)) | ||
.$usage('vertex'); | ||
const vertexLayout = tgpu.vertexLayout((n) => d.arrayOf(d.vec4f, n)); | ||
|
||
const vertex = tgpu['~unstable'].vertexFn({ | ||
in: { | ||
idx: d.builtin.vertexIndex, | ||
color: d.vec4f, | ||
}, | ||
out: { | ||
color: d.vec4f, | ||
pos: d.builtin.position, | ||
}, | ||
})(({ idx, color }) => { | ||
const vertices = [ | ||
d.vec2f(-1, -1), | ||
d.vec2f(1, -1), | ||
d.vec2f(1, 1), | ||
d.vec2f(-1, 1), | ||
]; | ||
return { | ||
color, | ||
pos: d.vec4f(vertices[idx], 0, 1), | ||
}; | ||
}); | ||
|
||
const mainFragment = tgpu['~unstable'].fragmentFn({ | ||
in: { | ||
color: d.vec4f, | ||
}, | ||
out: d.vec4f, | ||
})((input) => input.color); | ||
|
||
const indexBuffer = root.createBuffer(d.arrayOf(d.u16, 6), [0, 2, 1, 0, 3, 2]) | ||
.$usage('index'); | ||
|
||
const pipeline = root['~unstable'] | ||
.withVertex(vertex, { color: vertexLayout.attrib }) | ||
.withFragment(mainFragment, { format: presentationFormat }) | ||
.createPipeline() | ||
.withIndexBuffer(indexBuffer); | ||
|
||
function render() { | ||
pipeline | ||
.with(vertexLayout, colorBuffer) | ||
.withColorAttachment({ | ||
view: context.getCurrentTexture().createView(), | ||
loadOp: 'clear', | ||
storeOp: 'store', | ||
}) | ||
.drawIndexed(6); | ||
} | ||
render(); | ||
|
||
// #region Example controls & Cleanup | ||
|
||
function updateColor( | ||
color: readonly [number, number, number], | ||
position: keyof typeof colors, | ||
): void { | ||
colors[position] = d.vec4f(color[0], color[1], color[2], 1); | ||
const idx = colorIndices[position]; | ||
colorBuffer.writePartial([ | ||
{ | ||
idx, | ||
value: colors[position], | ||
}, | ||
]); | ||
render(); | ||
} | ||
|
||
export const controls = { | ||
topLeft: { | ||
onColorChange: (value: readonly [number, number, number]) => | ||
updateColor(value, 'topLeft'), | ||
initial: [...colors.topLeft.xyz], | ||
}, | ||
topRight: { | ||
onColorChange: (value: readonly [number, number, number]) => | ||
updateColor(value, 'topRight'), | ||
initial: [...colors.topRight.xyz], | ||
}, | ||
bottomLeft: { | ||
onColorChange: (value: readonly [number, number, number]) => | ||
updateColor(value, 'bottomLeft'), | ||
initial: [...colors.bottomLeft.xyz], | ||
}, | ||
bottomRight: { | ||
onColorChange: (value: readonly [number, number, number]) => | ||
updateColor(value, 'bottomRight'), | ||
initial: [...colors.bottomRight.xyz], | ||
}, | ||
}; | ||
|
||
export function onCleanup() { | ||
root.destroy(); | ||
} | ||
// #endregion |
5 changes: 5 additions & 0 deletions
5
apps/typegpu-docs/src/content/examples/simple/square/meta.json
This file contains hidden or 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,5 @@ | ||
{ | ||
"title": "Square", | ||
"category": "simple", | ||
"tags": ["experimental", "index buffer"] | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.