-
-
Notifications
You must be signed in to change notification settings - Fork 6
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
Make unstruct resolvable and introduce root.unwrap(TgpuVertexLayout)
#781
Open
reczkok
wants to merge
16
commits into
main
Choose a base branch
from
feat/resolvable-unstruct
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
16 commits
Select commit
Hold shift + click to select a range
a97e1dc
lil work
reczkok 4a06ddc
Merge branch 'main' into feat/resolvable-unstruct
reczkok 4f9c387
temp test
reczkok 5b60a94
Merge branch 'main' into feat/resolvable-unstruct
reczkok 98892cc
fix loose data resolution
reczkok 91d71e3
Merge branch 'main' into feat/resolvable-unstruct
reczkok 85845f6
fix imports
reczkok 70cf646
fix game of life import
reczkok 1d61eb3
use root.unwrap on a vertex layout in boids
reczkok 0ad9f26
more loose data resolution tests
reczkok eeea196
add tests for unwrapping vertex layouts
reczkok fdaea5a
Merge branch 'main' into feat/resolvable-unstruct
reczkok 490ac72
add docs
reczkok 5581471
hide for now
reczkok 53bcec8
Apply suggestions from code review
reczkok ddbab23
more review fixes
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
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
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
189 changes: 189 additions & 0 deletions
189
apps/typegpu-docs/src/content/docs/fundamentals/vertex-layouts.mdx
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,189 @@ | ||
--- | ||
title: Vertex Layouts | ||
description: A guide on how to create and use typed vertex layouts | ||
draft: true | ||
--- | ||
|
||
Typed vertex layouts are a way to describe the structure of vertex data in a typed manner. They are used to create a single source of truth for vertex data, which can be used in combination with [`tgpu.resolve`](/TypeGPU/fundamentals/resolve) and [`root.unwrap`](/TypeGPU/fundamentals/roots) to easily create and manage vertex buffers. | ||
|
||
## Creating a vertex layout | ||
|
||
To create a vertex layout, use the `tgpu.vertexLayout` function. It takes a function that takes a number and returns an array containing any data type, and a string that describes the type of the vertex layout. It can be either `vertex` or `instance` (default is `vertex`). | ||
|
||
```ts | ||
import tgpu from 'typegpu'; | ||
import * as d from 'typegpu/data'; | ||
|
||
const ParticleGeometry = d.struct({ | ||
tilt: d.f32, | ||
angle: d.f32, | ||
color: d.vec4f, | ||
}); | ||
|
||
const geometryLayout = tgpu | ||
.vertexLayout((n: number) => d.arrayOf(ParticleGeometry, n), 'instance'); | ||
``` | ||
|
||
## Utilizing loose schemas with vertex layouts | ||
|
||
The above example is great if the vertex buffer will also be used as a storage or uniform buffer. However, if you're only interested in the vertex data, you can use a loose schema instead. | ||
It is not restricted by alignment rules and can utilize many useful [vertex formats](https://www.w3.org/TR/webgpu/#vertex-formats). To create loose schemas, use `d.unstruct` instead of `d.struct` and `d.disarrayOf` instead of `d.arrayOf`. | ||
Inside of loose schemas you can use vertex formats as well as the usual data types. | ||
|
||
```ts | ||
const LooseParticleGeometry = d.unstruct({ | ||
tilt: d.f32, | ||
angle: d.f32, | ||
color: d.unorm8x4, // 4x8-bit unsigned normalized | ||
}); | ||
``` | ||
|
||
The size of `LooseParticleGeometry` will be 12 bytes, compared to 32 bytes of `ParticleGeometry`. This can be useful when you're working with large amounts of vertex data and want to save memory. | ||
|
||
:::tip[Aligning loose schemas] | ||
Sometimes you might want to align the data in a loose schema due to external requirements or performance reasons. | ||
You can do this by using the `d.align` function, just like in normal schemas. Even though loose schemas don't have alignment requirements, they will still respect any alignment you specify. | ||
|
||
```ts | ||
const LooseParticleGeometry = d.unstruct({ | ||
tilt: d.f32, | ||
angle: d.f32, | ||
color: d.align(16, d.unorm8x4), | ||
// 4x8-bit unsigned normalized aligned to 16 bytes | ||
}); | ||
``` | ||
|
||
This will align the `color` field to 16 bytes, making the size of `LooseParticleGeometry` 20 bytes. | ||
::: | ||
|
||
## Using vertex layouts | ||
|
||
You can utilize [`root.unwrap`](/TypeGPU/fundamentals/roots) to get the raw `GPUVertexBufferLayout` from a typed vertex layout. It will automatically calculate the stride and attributes for you, according to the vertex layout you provided. | ||
|
||
:::caution | ||
Make sure that all attributes in the vertex layout are marked with the appropriate location. You can use the `d.location` function to specify the location of each attribute. | ||
If you don't do this, the unwrapping will fail at runtime. | ||
::: | ||
|
||
```ts | ||
const ParticleGeometry = d.struct({ | ||
tilt: d.location(0, d.f32), | ||
angle: d.location(1, d.f32), | ||
color: d.location(2, d.vec4f), | ||
}); | ||
|
||
const geometryLayout = tgpu | ||
.vertexLayout((n: number) => d.arrayOf(ParticleGeometry, n), 'instance'); | ||
|
||
const geometry = root.unwrap(geometryLayout); | ||
|
||
console.log(geometry); | ||
//{ | ||
// "arrayStride": 32, | ||
// "stepMode": "instance", | ||
// "attributes": [ | ||
// { | ||
// "format": "float32", | ||
// "offset": 0, | ||
// "shaderLocation": 0 | ||
// }, | ||
// { | ||
// "format": "float32", | ||
// "offset": 4, | ||
// "shaderLocation": 1 | ||
// }, | ||
// { | ||
// "format": "float32x4", | ||
// "offset": 16, | ||
// "shaderLocation": 2 | ||
// } | ||
// ] | ||
//} | ||
``` | ||
|
||
This will return a `GPUVertexBufferLayout` that can be used when creating a render pipeline. | ||
|
||
```diff lang=ts | ||
const renderPipeline = device.createRenderPipeline({ | ||
layout: device.createPipelineLayout({ | ||
bindGroupLayouts: [root.unwrap(bindGroupLayout)], | ||
}), | ||
primitive: { | ||
topology: 'triangle-strip', | ||
}, | ||
vertex: { | ||
module: renderShader, | ||
- buffers: [ | ||
- { | ||
- arrayStride: 32, | ||
- stepMode: 'instance', | ||
- attributes: [ | ||
- { | ||
- format: 'float32', | ||
- offset: 0, | ||
- shaderLocation: 0, | ||
- }, | ||
- { | ||
- format: 'float32', | ||
- offset: 4, | ||
- shaderLocation: 1, | ||
- }, | ||
- { | ||
- format: 'float32x4', | ||
- offset: 16, | ||
- shaderLocation: 2, | ||
- }, | ||
- ], | ||
- }, | ||
- ], | ||
+ buffers: [root.unwrap(geometryLayout)], | ||
}, | ||
fragment: { | ||
... | ||
}, | ||
}); | ||
``` | ||
|
||
If you are using a loose schema, you can now resolve it to get its WGSL representation. | ||
|
||
```ts | ||
const LooseParticleGeometry = d.unstruct({ | ||
tilt: d.location(0, d.f32), | ||
angle: d.location(1, d.f32), | ||
color: d.location(2, d.unorm8x4), | ||
}); | ||
|
||
const sampleShader = ` | ||
@vertex | ||
fn main(particleGeometry: LooseParticleGeometry) -> @builtin(position) pos: vec4f { | ||
return vec4f( | ||
particleGeometry.tilt, | ||
particleGeometry.angle, | ||
particleGeometry.color.rgb, | ||
1.0 | ||
); | ||
} | ||
`; | ||
|
||
const wgslDefinition = tgpu.resolve({ | ||
template: sampleShader, | ||
externals: { LooseParticleGeometry } | ||
}); | ||
|
||
console.log(wgslDefinition); | ||
// struct LooseParticleGeometry_0 { | ||
// @location(0) tilt: f32, | ||
// @location(1) angle: f32, | ||
// @location(2) color: vec4f, | ||
// } | ||
// | ||
// @vertex | ||
// fn main(particleGeometry: LooseParticleGeometry_0) -> @builtin(position) pos: vec4f { | ||
// return vec4f( | ||
// particleGeometry.tilt, | ||
// particleGeometry.angle, | ||
// particleGeometry.color.rgb, | ||
// 1.0 | ||
// ); | ||
// } | ||
``` |
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.
A suggestion, this wording might suggest more that: