2

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?

BayL
  • 23
  • 4
  • Please provide enough code so others can better understand or reproduce the problem. – Community Mar 24 '22 at 08:43
  • I am not shure i understand the question. But... what you are trying to do does not really make sense to me. If you want to write into the surface texture you should be using a standard shader not a compute shader. since that is specifically what they are for. – Chris Fraser Dec 18 '22 at 02:17

2 Answers2

1

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));
}
frankelot
  • 13,666
  • 16
  • 54
  • 89
0

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.

Jinlei Li
  • 240
  • 6