Trying to use MSAA with textures

130 Views Asked by At

I'd like to use multisample anti-aliasing (MSAA) with my texture, but it's not working. When I create the texture, I set sampleCount to 4:

const texture = device.createTexture({
    size: [imageBitmap.width, imageBitmap.height],
    format: 'rgba8unorm',
    sampleCount: 4,
    usage:
         GPUTextureUsage.RENDER_ATTACHMENT |
         GPUTextureUsage.TEXTURE_BINDING |
         GPUTextureUsage.COPY_DST
});

The WGSL documentation says multisampled textures can't be read with samplers, so the fragment shader uses textureLoad instead of textureSample:

@group(0) @binding(1) var tex : texture_multisampled_2d<f32>;

@fragment
fn fragmentMain(fragData: DataStruct) -> @location(0) vec4f {
    return textureLoad(tex, fragData.uvPos, 0);
}

When I run the application, I'm told "no matching call to textureLoad(texture_multisampled_2d, vec2, abstract-int)". Any ideas? Are there any examples that use MSAA textures?

1

There are 1 best solutions below

0
On BEST ANSWER

You didn't show the definition of DataStruct but my guess is DataStruct.uvPos is not a compatible type.

textureLoad takes an integer texel coordinate, not a floating point normalized texture coordinate like textureSample

From the spec

C is i32, or u32
S is i32, or u32
ST is i32, u32, or f32

fn textureLoad(t: texture_multisampled_2d<ST>,
               coords: vec2<C>,
               sample_index: S)-> vec4<ST>

Notice above, C, is only i32 or u32, not f32

Otherwise, what are you trying to use MSAA textures for? The most common usage is for anti-aliasing but in that case you never read the texture directly. You just set it as a render target and set a non-MSAA texture as the resolve target like this answer