金属功能

时间:2017-08-03 10:11:07

标签: ios metal

我有下一个功能:

float4 blur(float rad, texture2d<float> tex2D, sampler sampler2D, float2 textureCoordinate){    
    float width = tex2D.get_width();
    float height = tex2D.get_height();
    float weight = 1 / ((2 * rad + 1) * (2 * rad + 1));
    float4 blured_color = float4(0,0,0,0);
    for(int i = -1 * rad; i <= rad; i++){
        for (int j = -1 * rad; j <= rad; j++){
            blured_color += tex2D.sample(sampler2D, textureCoordinate + float2(i/width, j/height)) * weight;
        }
    }
    return blured_color;
}

它使给定的片段模糊。

我的问题是,当我调用此功能时,它无法正常工作 - 它只会使图像变暗。但是当我编写相同的代码而不将其包装在函数中时,它可以正常工作:

fragment float4 blured_background_fragment(VertexOut        interpolated [[ stage_in ]],
                                    texture2d<float> tex2D        [[ texture(0) ]],
                                    sampler          sampler2D    [[ sampler(0) ]])
{
    float4 color = tex2D.sample(sampler2D, interpolated.textureCoordinate);
    float3 color3 = float3(color[0] , color[1] , color[2]);
    if (is_skin(color3) && !(interpolated.color[0] == 1 && interpolated.color[1] == 1 && interpolated.color[2] == 1)){
        float width = tex2D.get_width();
        float height = tex2D.get_height();
        float rad = 13;
        float weight = 1 / ((2 * rad + 1) * (2 * rad + 1));
        float4 blured_color = float4(0,0,0,0);
        for(int i = -1 * rad; i <= rad; i++){
            for (int j = -1 * rad; j <= rad; j++){
                blured_color += tex2D.sample(sampler2D, interpolated.textureCoordinate + float2(i/width, j/height)) * weight;
            }
        }
//        Here I try to call this blur function     
//        float4 blured_color = blur(13, tex2D, sampler2D, interpolated.textureCoordinate);
        return blured_color * 0.43 + color * 0.57;
    }
    else{
        return tex2D.sample(sampler2D, interpolated.textureCoordinate);
    }
}

0 个答案:

没有答案