webgpu-utils

    Function makeBindGroupLayoutDescriptors

    • Gets GPUBindGroupLayoutDescriptors for the given pipeline.

      Important: Assumes your pipeline is valid (it doesn't check for errors).

      Note: In WebGPU some layouts must be specified manually. For example an unfiltered-float sampler can not be derived since it is unknown at compile time pipeline creation time which texture you'll use.

      import {
      makeShaderDataDefinitions,
      makeBindGroupLayoutDescriptors,
      } from 'webgpu-utils';

      const code = `
      @group(0) @binding(0) var<uniform> mat: mat4x4f;

      struct MyVSOutput {
      @builtin(position) position: vec4f,
      @location(1) texcoord: vec2f,
      };

      @vertex
      fn myVSMain(v: MyVSInput) -> MyVSOutput {
      var vsOut: MyVSOutput;
      vsOut.position = mat * v.position;
      vsOut.texcoord = v.texcoord;
      return vsOut;
      }

      @group(0) @binding(2) var diffuseSampler: sampler;
      @group(0) @binding(3) var diffuseTexture: texture_2d<f32>;

      @fragment
      fn myFSMain(v: MyVSOutput) -> @location(0) vec4f {
      return textureSample(diffuseTexture, diffuseSampler, v.texcoord);
      }
      `;

      const module = device.createShaderModule({code});
      const defs = wgh.makeShaderDataDefinitions(code);

      const pipelineDesc = {
      vertex: {
      module,
      entryPoint: 'myVSMain',
      buffers: bufferLayouts,
      },
      fragment: {
      module,
      entryPoint: 'myFSMain',
      targets: [
      {format: 'rgba8unorm'},
      ],
      },
      };

      const descriptors = wgh.makeBindGroupLayoutDescriptors(defs, pipelineDesc);
      const bindGroupLayouts = descriptors.map(desc => device.createBindGroupLayout(desc));
      const layout = device.createPipelineLayout({ bindGroupLayouts });
      const pipeline = device.createRenderPipeline({
      layout,
      ...pipelineDesc,
      });

      Parameters

      • defs: ShaderDataDefinitions | ShaderDataDefinitions[]

        ShaderDataDefinitions or an array of ShaderDataDefinitions as returned from makeShaderDataDefinitions. If an array of more than 1 definition it's assumed the vertex shader is in the first and the fragment shader in the second.

      • desc: PipelineDescriptor

        A PipelineDescriptor. You should be able to pass in the same object you would pass to createRenderPipeline or createComputePipeline. In particular, you need the vertex / fragment or compute properties with or without entryPoints. The existence of the property means this shader type exists in the pipeline. If no entry point is specified the default entry point will be used, which, like WebGPU, defaults to the only entry point of that type. If there is more than one it's an error and you must specify an entry point.

      Returns GPUBindGroupLayoutDescriptor[]

      An array of GPUBindGroupLayoutDescriptors which you can pass, one at a time, to createBindGroupLayout. Note: the array will be sparse if there are gaps in group numbers. Note: Each GPUBindGroupLayoutDescriptor.entries will be sorted by binding.

    MMNEPVFCICPMFPCPTTAAATR