how to use an array of textures in metal

非 Y 不嫁゛ 提交于 2019-12-08 12:05:12

问题


I want to use array of textures in metal shader, but it crash when running my app(iPhone6, A8), here is the error log:Failed to created pipeline state, error Error Domain=AGXMetalA8 Code=3 "Could not resolve texture/sampler references" UserInfo={NSLocalizedDescription=Could not resolve texture/sampler references}

I have try to google but did not find useful information, can anyone give me some suggestions, Thanks.

Here is my code:

fragment float4 fragment_texture (ShaderInput vert [[stage_in]],
                        array<texture2d<float>, 2> u_texture [[texture(0)]],
                        sampler _mtlsmp_u_texture [[sampler(0)]])
{
    float4 srcColor = u_texture[0].sample(_mtlsmp_u_texture, vert.v_texCoord);
    float4 materialColor = u_texture[1].sample(_mtlsmp_u_texture, vert.v_texCoord);
    float4 mixColor = mix(srcColor, materialColor, 0.5);
    return mixColor;
}

In my app code:

    [renderEncoder setFragmentTexture:_textureDemo
                              atIndex:0];

    [renderEncoder setFragmentTexture:_textureBlend
                              atIndex:1];

    [renderEncoder setFragmentSamplerState:_sampler atIndex:0];

Update Issues

I try to use Argument Buffers to deal with array of textures, but still crashed on my iPhone6 with version iOS 11.4 and get the same error info:Failed to create pipeline state, error Could not resolve texture/sampler references

Here are some critical steps:

In my app code, argumentEncoder is a MTLArgumentEncoder type object and I encode the texture resources into the argument buffer by calling setTexture:atIndex:

 [argumentEncoder setArgumentBuffer:_fragmentShaderArgumentBuffer offset:0];
 [argumentEncoder setTexture:_texture[0] atIndex:0];
 [argumentEncoder setTexture:_texture[1] atIndex:1];

Calling the useResource:usage: method to make texture resources accessible to the GPU:

[renderEncoder useResource:_texture[0] usage:MTLResourceUsageSample];
[renderEncoder useResource:_texture[1] usage:MTLResourceUsageSample];

And set argument buffer _fragmentShaderArgumentBuffer as an argument to the fragment function:

[renderEncoder setFragmentBuffer:_fragmentShaderArgumentBuffer
                              offset:0
                             atIndex:0];

In my fragment shader:

typedef struct FragmentShaderArguments {
    array<texture2d<float>, 2> exampleTextures  [[ id(0)  ]];
} FragmentShaderArguments;

fragment float4
fragmentShader(       RasterizerData            in                 [[ stage_in ]],
               device FragmentShaderArguments & fragmentShaderArgs [[ buffer(0) ]])
{
    constexpr sampler textureSampler (mag_filter::linear,
                                      min_filter::linear);

    float4 color = float4(0, 0, 0, 1);
    float4 color1 = fragmentShaderArgs.exampleTextures[0].sample(textureSampler, in.texCoord);
    float4 color2 = fragmentShaderArgs.exampleTextures[1].sample(textureSampler, in.texCoord);
    color = mix(color1, color2, 0.5);
    return color;
}

Sincerely hope that someone can provide ideas to me, Thanks!


回答1:


I upgrade to iOS 12.0.1, and it works well.



来源:https://stackoverflow.com/questions/52849269/how-to-use-an-array-of-textures-in-metal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!