Metal Compute shader rescaling output

I am new to Metal compute shaders so have a doubt. When we use vertex/fragment shaders in render pipeline, it is possible for output texture to have a different size than input, we could simply specify sampler in the render pipeline as follows and it does the job to rescale output texture.


constexpr sampler s(s_address::clamp_to_edge, t_address::clamp_to_edge, min_filter::linear, mag_filter::linear)


But how do we achieve the same thing in compute shader, is it even possible? For instance, in this kernel how do we write to a outputTexture which has a different size than input texture?



kernel void rosyEffect(texture2d<half, access::read>  inputTexture  [[ texture(0) ]],
    texture2d<half, access::write> outputTexture [[ texture(1) ]],
    uint2 gid [[thread_position_in_grid]])
{
  /
  if ((gid.x >= inputTexture.get_width()) || (gid.y >= inputTexture.get_height())) {
  return;
  }
  half4 inputColor = inputTexture.read(gid);
  /
  half4 outputColor = half4(inputColor.r, 0.0, inputColor.b, 1.0);
  outputTexture.write(outputColor, gid);
}

Replies

How much bigger is the output texture? If it's twice as wide and tall than the input texture, for example, you can make the shader write 4 pixels to the output texture instead of just one.

It could be bigger or smaller, I want linear filtering. I think I should be using sample function than read to access texels.

Yes that will work. Launch one thread for each pixel in the output texture and use a sampler to read from the input texture. So the "gid" is now a pixel coordinate in the output texture, not the input texture.