简体   繁体   English

Metal RGB 到 YUV 转换计算着色器

[英]Metal RGB to YUV conversion compute shader

I am trying to write a Metal compute shader for converting from RGB to YUV, but am getting build errors.我正在尝试编写用于从 RGB 转换为 YUV 的 Metal 计算着色器,但遇到构建错误。

typedef struct {
   float3x3 matrix;
   float3   offset;
} ColorConversion;

  // Compute kernel
 kernel void kernelRGBtoYUV(texture2d<half, access::sample> inputTexture [[ texture(0) ]],
                       texture2d<half, access::write> textureY [[ texture(1) ]],
                       texture2d<half, access::write> textureCbCr [[ texture(2) ]],
                       constant ColorConversion &colorConv [[ buffer(0) ]],
                       uint2 gid [[thread_position_in_grid]])
{
  // Make sure we don't read or write outside of the texture
  if ((gid.x >= inputTexture.get_width()) || (gid.y >= inputTexture.get_height())) {
      return;
  }



  float3 inputColor = float3(inputTexture.read(gid).rgb);

  float3 yuv = colorConv.matrix*inputColor + colorConv.offset;

  half2 uv = half2(yuv.gb);

  textureY.write(half(yuv.x), gid);

  if (gid.x % 2 == 0 && gid.y % 2 == 0) {
      textureCbCr.write(uv, uint2(gid.x / 2, gid.y / 2));
  }
} 

The last line, ie write to textureCbCr throws an error:最后一行,即写入 textureCbCr 会抛出错误:

  no matching member function for call to 'write'

在此处输入图片说明 What am I doing wrong?我究竟做错了什么?

According to the Metal Shading Language Specification, the first parameter of all overloads of write on texture2d<> are 4-element vectors.根据 Metal Shading Language Specification, texture2d<>上所有write重载的第一个参数是 4 元素向量。 This is the case even if the texture you're writing to has fewer than 4 components.即使您写入的纹理少于 4 个组件,情况也是如此。 So you can fix this by replacing the erroneous line with:因此,您可以通过将错误的行替换为以下内容来解决此问题:

textureCbCr.write(half4(yuv.xyzz), uint2(gid.x / 2, gid.y / 2));

And the superfluous components will be masked out when performing the write.并且在执行写入时将屏蔽掉多余的组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM