如何在金属中使用纹理阵列 [英] how to use an array of textures in metal

查看:141
本文介绍了如何在金属中使用纹理阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在金属着色器中使用纹理数组,但是会崩溃 当运行我的应用程序(iPhone6,A8)时,以下是错误日志: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 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}

我已经尝试过Google,但是找不到有用的信息,任何人都可以给我一些建议,谢谢.

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

这是我的代码:

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;
}

在我的应用代码中:

    [renderEncoder setFragmentTexture:_textureDemo
                              atIndex:0];

    [renderEncoder setFragmentTexture:_textureBlend
                              atIndex:1];

    [renderEncoder setFragmentSamplerState:_sampler atIndex:0];

更新问题

我尝试使用参数缓冲区处理纹理数组,但在iOS 11.4版的iPhone6上仍然崩溃,并得到相同的错误信息:Failed to create pipeline state, error Could not resolve texture/sampler references

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

以下是一些关键步骤:

在我的应用程序代码中,argumentEncoder是MTLArgumentEncoder类型的对象,我通过调用setTexture:atIndex:

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];

调用useResource:usage:方法使纹理资源可供GPU访问:

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];

并将参数缓冲区_fragmentShaderArgumentBuffer设置为片段函数的参数:

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

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

在我的片段着色器中:

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!

推荐答案

我升级到iOS 12.0.1,并且效果很好.

I upgrade to iOS 12.0.1, and it works well.

这篇关于如何在金属中使用纹理阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆