Skip to content

fix: Restrict using tgpu.slot for injecting raw shader code #2498

@iwoplaza

Description

@iwoplaza

Currently we treat slots that carry strings like WGSL-code containers:

const fooSlot = tgpu.slot<string>();

const bar = tgpu.fn([d.f32], d.f32)`(x) {
  return fooSlot;
}`.$uses({ fooSlot });

const shader = tgpu.resolve([bar.with(fooSlot, 'x + 5')]);

Generates:

fn bar(x: f32) -> f32 {
  return x + 5;
}

In JS-implemented functions, string-carrying slots have more uses than that, for example for comptime checks:

const modeSlot = tgpu.slot<'fast' | 'precise'>();

function bar() {
  'use gpu';
  if (modeSlot.$ === 'fast') {
    // ...
  } else {
    // ...
  }
}

The same WGSL-injection functionality can be achieved using tgpu.rawCodeSnippet, and provides more context when used from a JS-implemented function.

Solution

I suggest we just throw an Error when a string of type UnknownData is about to be injected into source code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    stabilityTasks that focus on improving stability, that includes tests, refining edge-cases, refactoring.

    Type

    No fields configured for Task.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions