antvis / G

💥 A flexible rendering engine for visualization.

Home Page:https://g.antv.antgroup.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[g-plugin-device-renderer] 简化创建 Bindings 的初始化参数

xiaoiver opened this issue · comments

目前创建 Bindings 和 Pipeline 都需要提供 BindingLayouts:

const bindingLayouts = [{ numSamplers: 0, numUniformBuffers: 1 }];
const bindings = device.createBindings({
  pipeline,
  bindingLayout: bindingLayouts[0],
  uniformBufferBindings: [
    {
      buffer: uniformBuffer,
      byteLength: 16 * 4,
    },
  ],
  samplerBindings: [],
});
const pipeline = device.createRenderPipeline({
  bindingLayouts
});

但实际上可以通过 Pipeline 自动生成:
https://www.w3.org/TR/webgpu/#dom-gpupipelinebase-getbindgrouplayout
https://toji.dev/webgpu-best-practices/bind-groups#auto-layout-generation

修改后的用法如下,无需创建 bindingLayouts:

const bindings = device.createBindings({
  pipeline,
  uniformBufferBindings: [
    {
      buffer: uniformBuffer,
      byteLength: 16 * 4,
    },
  ],
  samplerBindings: [],
});
const pipeline = device.createRenderPipeline({});

局限性是只支持一个 bindingGroup。这样 setBindings 也可以简化,第一个参数可以去掉:

// before
renderPass.setBindings(0, bindings);
// after
renderPass.setBindings(bindings);