英文:
Checking Metal texture is valid from shader
问题
我正在使用Metal 3,并且有一个包含材质结构的参数缓冲区。每个材质可能具有漫反射纹理字段 texture2d<float> diffuseTexture;
但有些材质可能没有。
如何从Metal着色器中检查特定的金属纹理是否有效(从主机端初始化)?(一种可能的解决方案是传递额外的标志isDiffuseTexture available)
但我希望找到与以下代码片段类似的解决方法:
if (material.diffuseTexture)
{
const float4 diffuseFromTex = material.diffuseTexture.sample(textureSampler, state.textureCoordinates);
state.diffuse *= diffuseFromTex.rgb;
}
英文:
I'm using Metal 3 and have an Argument buffers with structures of Materials. Each material may have diffuse texture field texture2d<float> diffuseTexture;
But some materials could don't have one.
How can I check from metal shader if specific metal texture is valid (initialised from host side)? (One solution that could be is to pass additional flags isDiffuseTexture available)
But I'm looking something close to this snippet:
if (material.diffuseTexture)
{
const float4 diffuseFromTex = material.diffuseTexture.sample(textureSampler, state.textureCoordinates);
state.diffuse *= diffuseFromTex.rgb;
}
答案1
得分: 1
is_null_texture
是你正在寻找的函数。根据《Metal Shading Language规范》,使用以下函数来确定纹理是否为null纹理。如果纹理是null纹理,is_null_texture
返回true;否则返回false。
所以你的代码片段变成了:
if (!is_null_texture(material.diffuseTexture))
{
const float4 diffuseFromTex = material.diffuseTexture.sample(textureSampler, state.textureCoordinates);
state.diffuse *= diffuseFromTex.rgb;
}
英文:
is_null_texture
is the function you are looking for. From Metal Shading Language specification:
> Use the following functions to determine if a texture is a null texture. If the texture is a null
texture, is_null_texture returns true; otherwise it returns false.
So your snippet turns into:
if (!is_null_texture(material.diffuseTexture))
{
const float4 diffuseFromTex = material.diffuseTexture.sample(textureSampler, state.textureCoordinates);
state.diffuse *= diffuseFromTex.rgb;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论