brendan-duncan / wgsl_reflect

A WebGPU Shading Language parser and reflection library for Javascript.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error with new WGSL "enable" and "requires" directives

alienself opened this issue · comments

Hello again,

WGSL introduced some new directives, however when adding those to my wgsl code the library will no longer detect any module entry.
Adding support for requires would be really awesome to use some of the latest language features.

Unlock language features with requires:
requires readonly_and_readwrite_storage_textures;
https://developer.chrome.com/blog/new-in-webgpu-124

if (!navigator.gpu.wgslLanguageFeatures.has("readonly_and_readwrite_storage_textures")) {
  throw new Error("Read-only and read-write storage textures are not available");
}

const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();

const bindGroupLayout = device.createBindGroupLayout({
  entries: [{
    binding: 0,
    visibility: GPUShaderStage.COMPUTE,
    storageTexture: {
      access: "read-write", // <-- New!
      format: "r32uint",
    },
  }],
});

const shaderModule = device.createShaderModule({ code: `
  requires readonly_and_readwrite_storage_textures;

  @group(0) @binding(0) var tex : texture_storage_2d<r32uint, read_write>;

  @compute @workgroup_size(1, 1)
  fn main(@builtin(local_invocation_id) local_id: vec3u) {
    var data = textureLoad(tex, vec2i(local_id.xy));
    data.x *= 2;
    textureStore(tex, vec2i(local_id.xy), data);
  }`
});

Experimental flag with enable:
enable chromium_experimental_subgroups;
https://developer.chrome.com/blog/new-in-webgpu-125#subgroups_feature_in_development

const adapter = await navigator.gpu.requestAdapter();
if (!adapter.features.has("chromium-experimental-subgroups")) {
  throw new Error("Experimental subgroups support is not available");
}
// Explicitly request experimental subgroups support.
const device = await adapter.requestDevice({
  requiredFeatures: ["chromium-experimental-subgroups"],
});

const shaderModule = device.createShaderModule({ code: `
  enable chromium_experimental_subgroups;

  @compute @workgroup_size(64) fn main(
      @builtin(global_invocation_id) global_id : vec3u,
      @builtin(subgroup_size) sg_size : u32,
      @builtin(subgroup_invocation_id) sg_id : u32) {
    // TODO: Use subgroupBallot() and subgroupBroadcast().
  }`,
});

Requires was definitely missing, so I just added that and published the update on npm.

Enable was already there, I added your shader snippet to the tests and it seemed to do ok, reflect found main.

I'll assume this is completed. Feel free to tell me otherwise.

Oh my bad, sorry for the late response, thank you very much for this new feature support!