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

tgpu.resolve API #522

Open
iwoplaza opened this issue Nov 5, 2024 · 0 comments
Open

tgpu.resolve API #522

iwoplaza opened this issue Nov 5, 2024 · 0 comments
Assignees

Comments

@iwoplaza
Copy link
Collaborator

iwoplaza commented Nov 5, 2024

The tgpu.resolve function generates a WGSL code string that is representative of all inputs (+ extra dependencies).

Expected signature:

function resolve(
  input: string | TgpuResolvable | (string | TgpuResolvable)[],
  extraDependencies?: Record<string, TgpuResolvable>
): string;

Example uses

Adding dependencies to a plain code string.

const Gradient = struct({
  from: vec3f,
  to: vec3f,
});

const foo0 = tgpu.resolve('fn foo() { var v: Gradient; }', { Gradient });

console.log(foo0); // struct Gradient_1 { from: vec3f, to: vec3f, } fn foo() { var v: Gradient_1; }

Resolving two entry functions to get one shader code string

const { intensity } = tgpu
  .bindGroupLayout({
    intensity: { uniform: f32 },
  }).bound;

const vertex = tgpu
  .vertexFn({}, {})
  .does(() => {
    const v = intensity.value;
  });

const fragment = tgpu
  .fragmentFn({}, vec4f)
  .does(() => vec4f(intensity.value, 0, 0, 1));

// Deduplicates dependencies
const foo0 = tgpu.resolve([vertex, fragment]);

console.log(foo0);
// @group(0) @binding(0) var<uniform> intensity_1: f32;
//
// @vertex
// fn vertex_main() {
//   let v = intensity_1;
// }
//
// @fragment
// fn fragment_main() {
//   return vec4f(intensity_1, 0.0, 0.0, 1.0);
// }
@iwoplaza iwoplaza added this to the 0.3.0 - Typed WGSL function milestone Nov 5, 2024
@reczkok reczkok self-assigned this Nov 6, 2024
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

No branches or pull requests

2 participants