Metal Fragment Shader,访问当前帧缓冲颜色

时间:2017-07-30 07:51:30

标签: fragment-shader raytracing metal

我正在写一个金属着色器。

我想要的只是访问片段的当前颜色。

例如。

在我的片段着色器结束时,我放了 返回currentColor + 0.1,例如

结果将是FPS的屏幕从黑色变为白色。 这是一个绘制填充屏幕的三角形条带的基本程序。 最终目标是在着色器中编写路径跟踪器,我使用opengl + glsl完成此操作。 我在缓冲区遇到问题。我认为一个简单的解决方案是将当前输出颜色传递回着色器并在那里进行平均。

这些是着色器:

#include <metal_stdlib>
using namespace metal;
#import "AAPLShaderTypes.h"

vertex float4 vertexShader(uint vertexID [[vertex_id]], constant AAPLVertex *vertices [[buffer(AAPLVertexInputIndexVertices)]], constant vector_uint2 *viewportSizePointer [[buffer(AAPLVertexInputIndexViewportSize)]], constant vector_float4 * seeds) {

    float4 clipSpacePosition = vector_float4(0.0, 0.0, 0.0, 1.0);

    float2 pixelSpacePosition = vertices[vertexID].position.xy;
    vector_float2 viewportSize = vector_float2(*viewportSizePointer);
    clipSpacePosition.xy = pixelSpacePosition / (viewportSize / 2.0);

    return clipSpacePosition;
}

fragment float4 fragmentShader()
{
    // I want to do this
    // return currentColor + 0.1;

    return float4(1.0, 1.0, 1.0, 1.0);
}

1 个答案:

答案 0 :(得分:1)

不用担心,答案一直盯着我。

使用float4 color0 [[color(0)]]

将当前颜色传递到片段着色器

设置了这两个选项 renderPassDescriptor.colorAttachments [0] .storeAction = MTLStoreActionStore; renderPassDescriptor.colorAttachments [0] .loadAction = MTLLoadActionLoad;

我的问题不是颜色,而是计算平均值。 在我的情况下,我不能做正常的平均值,因为到目前为止将所有颜色加起来并除以样本数。

我实际需要做的是计算移动平均线。

https://en.wikipedia.org/wiki/Moving_average#Cumulative_moving_average

相关问题