首页 > 解决方案 > Mac 应用程序的金属帧缓冲颜色

问题描述

在此处输入图像描述如何在我的着色器中为 mac 应用程序读取帧缓冲区当前颜色。我很容易使用 [[color(0)]] 在 ios 应用程序中做同样的事情。

我尝试使用如下所示的纹理,一些像素被遗漏了。

texture2d_array normal_1 [[纹理(0)]]

示例代码

fragment float4 funcname(QuadFragIn     inFrag [[ stage_in ]],
                                  texture2d_array<float> normal_1 [[texture(0)]])
{



    float4 color_0 = float4(normal_1.sample(tex_sampler, inFrag.m_TexCoord, 0));
    float4 color_1 = float4(normal_1.sample(tex_sampler, inFrag.m_TexCoord, 1));
    float4 color_2 = float4(normal_1.sample(tex_sampler, inFrag.m_TexCoord, 2));

    int index = inFrag.index;

    if(index == 0)
    {
        return color_0;
    }
    else if(index == 1)
    {
        return color_1;
    }
    else
    {
        return color_2;
    }

}[![enter image description here][1]][1]

在输出文件附件中,白条是相关问题。

标签: metalmetal-performance-shaders

解决方案


根据金属着色语言规范(https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf,表 12 下的注释)

对于[[color(m)]], m 用于在片段函数中访问(读取或写入)多个颜色附件时指定颜色附件索引。该[[color(m)]]属性仅在 iOS 中受支持。

这意味着您不能进行像素回读(从同一渲染通道中的附加纹理中读取已经渲染的像素)。

避免必须进行像素回读的一种方法是在单独的渲染通道中进行像素写入和读取:您在第一个渲染通道中渲染您想要的内容,然后将第一个渲染通道的结果附加为纹理,您可以对其进行采样或读取.


推荐阅读