Skip to content
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 to its Struct counterpart #747

Closed
reczkok opened this issue Jan 8, 2025 · 3 comments · Fixed by #781
Closed

Make Unstruct resolvable to its Struct counterpart #747

reczkok opened this issue Jan 8, 2025 · 3 comments · Fixed by #781
Assignees

Comments

@reczkok
Copy link
Collaborator

reczkok commented Jan 8, 2025

Unstructs could be used with raw WGSL vertex functions to generate attributes with proper locations (given every property is decorated with the location attribute).

const Vertex = d.unstruct({
 position: d.location(0, d.vec3f), // or d.float32x3
 color: d.location(1, d.unorm8x4),
});

const shader = /* wgsl */ `
 @vertex
 fn vert(v: Vertex) -> @builtin(position) pos: vec4f {
  ....
 }`;

const resolved = tgpu.resolve({
 template: shader,
 externals: { Vertex },
});

it would resolve to:

struct Vertex_0 {
 @location(0) postion: vec3f,
 @location(1) color: vec4f,
}

@vertex
fn vert(v:Vertex_0) -> @builtin(position) pos: vec4f {
  ....
}

Structs and Unstructs still need to be separate because buffers created with Unstructs as their schemas can't be used as storage or uniforms - they can be used as local variables tho.
We can assume a default representation for each format (eg. rgba16unorm -> vec4f). Potentially that could allows us to combine it with vertexLayouts to unwrap to appropriate GPUVertexBufferLayout entry and have just one sot (the struct).

const vLayout = tgpu.vertexLayout(n => d.disarrayOf(Vertex, n))

const vertexShader = device.createRenderPipeline({
  layout: device.createPipelineLayout({
    bindGroupLayouts: [root.unwrap(layout)],
  }),
  vertex: {
    module: device.createShaderModule({
      code: shader,
    }),
    buffers: [
      root.unwrap(vLayout) 
    ],
  },
...
});
@iwoplaza
Copy link
Collaborator

iwoplaza commented Jan 8, 2025

This could be a really nice improvement, creating a new point of integration for vanilla WebGPU projects (with unwrapping vertex layouts and resolving an external unstruct with attributes) and for full-TypeGPU projects (reduced duplication, which usually involved creating an unstruct, and its struct counterpart manually).

@iwoplaza
Copy link
Collaborator

iwoplaza commented Jan 9, 2025

@mhawryluk Thoughts? 🧠

@mhawryluk
Copy link
Collaborator

sure, I don't see why not 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants