首页 > 解决方案 > 基于内核的瓷砖着色器可以写入颜色附件吗?

问题描述

基于金属着色语言规范中的示例:

Example:
struct Foo {
    float4 a [[color(0)]];
    int4 b [[color(1)]];
};

kernel void
my_kernel(imageblock<Foo, imageblock_layout_implicit> img_blk,
 ushort2 lid [[thread_index_in_threadgroup]] …) 
{
…
    Foo f = img_blk.read(lid); float4 r = f.a;
…
    f.a = r;
…
    img_blk.write(f, lid);
}

作为图像块别名颜色附件的成员,我认为在“imgblk.write(...)”之后图像块将被写入相应的颜色附件。

我在 Apple 的示例中对此进行了实验:Forward-plus with tile shading,我尝试使用 imageblock.write(..) 写入 tile 着色器中的颜色附件,但我得到了非常奇怪的结果:

  1. 只有背景的像素被改变,但结果比我设置的要暗得多,例如我设置 color=float4(1,0,0,1) 但在屏幕上它是 float4(0.057, 0, 0, 1)
  2. 其他部分的颜色奇怪地取决于在前一个片段着色器传递中是否/写入图像块的内容,考虑我将图像块中的值设置为常量。

无论如何,感觉 imageblock.write() 在平铺着色器中无法正常工作。或者如何正确使用?

标签: iosgraphicsshadermetaliosdeployment

解决方案


解决了,有几件事导致我得到奇怪的结果: 1. 颜色格式是 srgb 2. 示例项目使用的 numSamples 是 4 3. 我在没有指定 imageblock_data_rate 的情况下进行了 imageblock write

在多重采样的情况下,需要为每个样本读取/写入图像块


推荐阅读