检查来自着色器的金属纹理是否有效

huangapple go评论49阅读模式
英文:

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&lt;float&gt; 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;
}

huangapple
  • 本文由 发表于 2023年2月26日 21:01:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572162.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定