Reputation: 13
I'm trying to make a fragment shader that can be used in case where some material property is of color type or texture. But I encountered big problem. Lets say that I have a Material struct like this:
typedef struct {
vector_float4 diffuseColor;
bool useDiffuseTexture;
} Material;
and fragment function:
fragment float4 fragment_main(Vertex vert [[stage_in]],
constant Constants &uniforms [[buffer(0)]],
constant Material &material [[buffer(1)]],
texture2d<float> diffuseTexture [[texture(0)]],
sampler diffuseTextureSampler [[sampler(0)]]
)
{...}
In fragment body I will either have :
float4 diffuseColor = material.diffuseColor;
or if material has diffuse texture:
float4 diffuseColor = diffuseTexture.sample(diffuseTextureSampler, float2(vert.textcoords));
Problem is that I cannot really test for existence of texture in shader, so I thought that I will pass it as param in a struct :
bool useDiffuseTexture;
but for some reason this code :
float4 diffuseColor = material.diffuseColor;
if (material.useDiffuseTexture == true ) {
diffuseColor = diffuseTexture.sample(diffuseTextureSampler, float2(vert.textcoords));
}
always calls diffuseTexture and raises error:
missing texture binding at index 0 for diffuseTexture[0]
I'm not sure what I'm doing wrong. I cannot have a binding because there is no texture.
Thanks in advance
Upvotes: 1
Views: 947
Reputation: 31782
Even if you don't use it in your function, each texture, buffer, or sampler parameter must have a corresponding object bound in the argument table. In your case, you could bind a "dummy", 1x1 texture to satisfy this requirement, or you could have two shader variants (and thus two render pipeline states), one that uses the diffuse color and one that samples the diffuse texture.
Upvotes: 1