wgpu Compute Write Direct to Surface Texture View

1.1k Views Asked by At

I am relatively new to using gpu apis, even newer to wgpu, and wanted to mess around with compute shaders drawing to a surface. However, it seems that this is not allowed directly?

During run time upon attempting to create a binding to the texture view from the surface, an error stating that the STORAGE BINDING bit is necessary, however, that is not allowed to be defined during the surface configuration. I have also attempted to have the shader accept the texture as a regular texture rather than a storage texture, but that came with its own error of the binding being invalid.

Is there a good way to write directly to the surface texture, or is it necessary to create a separate storage texture? Does the render pipeline under the hood not write directly to the surface's texture view? If a separate texture (which I am guessing it is), is there a best method to follow?

2

There are 2 best solutions below

0
On BEST ANSWER

The compute shader cannot write to surface texture directly, that is the responsibility of the fragment shader.

Since swapchain uses double or multi-buffering technology, the surface texture changes from frame to frame; Also, the usage of surface texture is RENDER_ATTACHMENT, which means that it can only be used for RenderPass's color_attachments;

Compute shader can only output Storaga Buffer and Storage Texture, these two types of data can be used by binding to a fragment shader.

0
On

You can write to a texture using textureStore and then render that texture to the final surface using a render_pass

@group(0) @binding(0)
var out_texture: texture_storage_2d<rgba8unorm, write>;

@compute @workgroup_size(16 , 16)
fn main(@builtin(global_invocation_id) global_id: vec3u) {
   textureStore(out_texture, vec2<u32>(global_id.x, global_id.y), vec4<f32>(your_color.xyz, 1.0));
}