batching - 为整个网格而不是每个顶点设置颜色

标签 batching webgpu wgpu-rs

我想优化顶点缓冲区的大小。目前我的 VBO 布局是:

x y | r g b a
它像这样被着色器使用:

struct VertexInput {
   @location(0) position: vec2<f32>,
   @location(1) color: vec4<f32>,
}

我将网格存储在缓冲区中,如下所示:|Mesh1|Mesh2|LargeMes​​h3|,因为我的网格是动态的。它在一个 drawCall 中渲染(好像叫做 Draw Call Batching)。

我想通过为每个网格而不是每个顶点设置不同的颜色来减少发送到 GPU 的数据。而且每个网格都不一样。我怎样才能实现它?

我正在画笔画: enter image description here

最佳答案

我用 @trojanfoe 实现了它对多个 drawCalls 的帮助。 我使用 stepMode: 'instance' 创建了第二个缓冲区并将颜色传递给它。 布局:

vertex: {
   module: this.shaderModule,
   entryPoint: 'vertex',
   buffers: [
      {
         arrayStride: 2 * VBO_ARRAY.BYTES_PER_ELEMENT,
         stepMode: 'vertex',
         attributes: [
            {
               format: 'float32x2',
               offset: 0,
               shaderLocation: 0,
            },
         ],
      },
      {
         arrayStride: 4 * VBO_ARRAY.BYTES_PER_ELEMENT,
         stepMode: 'instance',
         attributes: [
            {
               format: 'float32x4',
               offset: 0,
               shaderLocation: 1,
            },
         ],
      },
   ],
}

添加到 renderPass:

pass.setVertexBuffer(0, this.vbo.buffer)
pass.setVertexBuffer(1, this.clrbo.buffer)

并按原样在着色器中使用:

struct VertexInput {
   @location(0) position: vec2<f32>,
   @location(1) color: vec4<f32>,
}
struct VSOutput {
   @builtin(position) position: vec4<f32>,
   @location(0) color: vec4<f32>,
}
@vertex
fn vertex(vert: VertexInput) -> VSOutput {
    var out: VSOutput;
    out.color = vert.color;
    ....
    return out;
}
@fragment
fn fragment(in: VSOutput) -> @location(0) vec4<f32> {
    return in.color;
}

但是,我不确定它是否适用于合并在一个缓冲区中并通过一次绘制调用渲染的多个网格。

关于batching - 为整个网格而不是每个顶点设置颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73638228/

相关文章:

java - 在 libgdx 中批处理多维数据集时出现问题

apache-flex - 如何防止 RemoteObject 一起批处理 AMF 消息?

chrome-canary - 如何让WebGPU在Chrome Canary 97中运行?

javascript - 无法读取 null 的属性 'requestDevice'

rust - write_buffer 不写入缓冲区而是输出零 WGPU

apache-kafka - Kafka 了解相关消息何时被消费

MSBuild 对三个独立变量进行批处理

2d - 如何在 webgpu 中创建 2d 剪贴蒙版?

rust - wgpu 计算直接写入表面纹理 View