How to check that a Texture Sampler is null/nothing in glsl?

ui.frag

#version 450

layout (binding = 1) uniform sampler2D samplerColor;

// in
layout (location = 0) in vec2 in_uv;
layout (location = 1) in vec4 in_color;

// out
layout (location = 0) out vec4 outFragColor;

// texture
layout (binding = 0) uniform sampler2D tex;

void main() 
{
    outFragColor = in_color * texture(samplerColor, in_uv);
}

The code above is designed for rendering texts (ui), that’s why there is a texture sampler, but I want that the code is also designed for lines, polylines, rectangles for ui that don’t need texture at all, that’s why I ask the question below:
Is there a best way to check if the sampler is NULL, so the texture sampler function will be replaced with vec4(1, 1, 1, 1)?

If there is no solution then I will split the shader files into different files but my codes will become bigger and more complex, but I’m sure I will gain only a very tiny performance, but codes readability is more important.

1 Like

You don’t. You cannot detect if a sampler variable has a texture associated with it.

There are three ways to handle this: change to a different shader (not a great idea), use a second uniform variable to tell the shader if it should fetch from a texture, or use a tiny, white texture for “untextured” UI elements and always provide texture coordinates.

The latter is the common solution for cases like this. It also makes it easier to put textures into non-textual UI elements if that’s a thing you want to do later.

2 Likes

Thanks, now I believe that my wish above doesn’t exist.

I analyzed the uv(s) of the vertices then I realize that in Vulkan, we prefer that all Draw Lists are in one group (one command), that’s why we prefer to use one single Shader for one group of draw lists.

So, I updated the Shader source code as below:

void main() 
{
    vec4 font = vec4(1, 1, 1, 1);
    if (in_uv.x != 0 || in_uv.y != 0) {
        font = texture(samplerColor, in_uv);
    }
    outFragColor = in_color * font;
}

According to my experience, a shader with texture is slower than without texture
The most difficult in Shader performace, is when we choose if the ‘if’ must be [branch] or [flatten] (DirectX, but people say that OpenGL doesn’t have these feature). They are important about performance, and you must see the generated assembler text code to make sure.

I will re-ask that question in the future when I need performance. But 90% of the screen uses the ui shader I was always talking about, so if someone can answer the equivalent of [branch] if / [flatten] if from DirectX in OpenGL / Vulkan. Thanks.

As Alfonse said, use a 1x1 white texture instead. You don’t need to branch, you don’t need to change shaders, the texture read will be cached, and the performance will be better than anything that branches or changes shaders.

1 Like

Thanks, I will think about that 1x1 white texture, and I must post a new thread in the forum where I got the examples, because their shader source code is very simple without “if (in_uv.x != 0 || in_uv.y != 0) …” just like in the very first source code.

If I use the very first source simpler code, then the rectangles and lines don’t display, because the alpha is 0.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.