319854902 319854902 319854902 319854902 webgpu交Q流群

之前在glsl里,定义常量一般就宏一个
但是看deferred demo的时候,发现新写法诶

@group(0) @binding(0) var gBufferPosition: texture_2d<f32>;
@group(0) @binding(1) var gBufferNormal: texture_2d<f32>;
@group(0) @binding(2) var gBufferAlbedo: texture_2d<f32>;

override canvasSizeWidth: f32;
override canvasSizeHeight: f32;

@fragment
fn main(
  @builtin(position) coord : vec4<f32>
) -> @location(0) vec4<f32> {
  var result : vec4<f32>;
  let c = coord.xy / vec2<f32>(canvasSizeWidth, canvasSizeHeight);
  if (c.x < 0.33333) {
    result = textureLoad(
      gBufferPosition,
      vec2<i32>(floor(coord.xy)),
      0
    );
  } else if (c.x < 0.66667) {
    result = textureLoad(
      gBufferNormal,
      vec2<i32>(floor(coord.xy)),
      0
    );
    result.x = (result.x + 1.0) * 0.5;
    result.y = (result.y + 1.0) * 0.5;
    result.z = (result.z + 1.0) * 0.5;
  } else {
    result = textureLoad(
      gBufferAlbedo,
      vec2<i32>(floor(coord.xy)),
      0
    );
  }
  return result;
}

看见了奇怪的override字眼,看起来wgsl推荐我们使用这种方式来定义噢

const gBuffersDebugViewPipeline = device.createRenderPipeline({
    layout: device.createPipelineLayout({
      bindGroupLayouts: [gBufferTexturesBindGroupLayout],
    }),
    vertex: {
      module: device.createShaderModule({
        code: vertexTextureQuad,
      }),
      entryPoint: 'main',
    },
    fragment: {
      module: device.createShaderModule({
        code: fragmentGBuffersDebugView,
      }),
      entryPoint: 'main',
      targets: [
        {
          format: presentationFormat,
        },
      ],
      constants: {
        canvasSizeWidth: canvas.width,
        canvasSizeHeight: canvas.height,
      },
    },
    primitive,
  });
319854902 319854902 319854902 319854902 webgpu交Q流群

更多推荐