![](/img/trans.png)
[英]Metal Shader Function that can deal with both RGB and YUV textures
[英]Metal RGB to YUV conversion compute shader
我正在嘗試編寫用於從 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));
}
}
最后一行,即寫入 textureCbCr 會拋出錯誤:
no matching member function for call to 'write'
根據 Metal Shading Language Specification, texture2d<>
上所有write
重載的第一個參數是 4 元素向量。 即使您寫入的紋理少於 4 個組件,情況也是如此。 因此,您可以通過將錯誤的行替換為以下內容來解決此問題:
textureCbCr.write(half4(yuv.xyzz), uint2(gid.x / 2, gid.y / 2));
並且在執行寫入時將屏蔽掉多余的組件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.