Check if alpha channel exists?

Is there an easy way to detect if an alpha channel exists for a given texture in HLSL? I’m using an alpha channel in a specular texture to control self-illumination. To make it easy as possible for artists, I want to make the alpha channel optional:

-if alpha channel exists, use that to control self-illumination
-if alpha channel does not exist, set to zero

In my current shader, even if an alpha channel is not present, it still sets the value to 1 (full bright). The solution eludes me!

You really want two different shaders. FX shaders can’t tell you anything about the textures that are sampled: size, bit-depth, etc.

If you’re doing this in Max, you can do a scripted material plug-in and use conditionals in the shader to turn the alpha channel use on and off.

Branching in shaders is costly and you should avoid it.

Thanks, I had a feeling that would be the case. I’ll just use 2 different shaders then. The game-engine I use is primarily aimed at kids and entry-level game creation, and I just wanted to make it as easy and foolproof as possible.

You can add a boolean that the artist can set like bUseAlphaAsEmissive and if that is checked you assume that the texture will have the .a .

That’s how we usually handle options like that, the compiler will automatically generate the different permutations of the options on and off so if you have it set to off it won’t be wasting cycles.

2 different shaders can quickly get out of synch.

Thanks for the response Ikruel, I will pass this along to my engine programmer as well, like I said this software is aimed at beginners with little to no game making experience - so things need to be as straightforward and easy to use as possible.

@Ikruel

I agree with the use of shader permutations, but I’m curious about your statement that the compiler will generate the permutations. We use a shader permutation system, but it’s a custom system to not only compile the shaders but to do so with our engine specifics and assets in mind.

Is there some magic FXC stuff that I don’t know about? I will confess that I haven’t compiled a shader by hand in quite some time.

I’m not 100% on wether the engine needs to do any legwork, but in three engines that I’ve messed with (one being unreal, the other one being source) the booleans do get compiled out into different files and some voodoo magic picks which shader to use.

[QUOTE=hyrumark;5130]
-if alpha channel does not exist, set to zero
[/QUOTE]

There’s also some obscure hardware on the ps3 and 360 that will actually let you remap texture channels and default channel values on the gpu before a value is fetched, for free. We used this to optimize some of our monochromatic textures, but i think it would work in your case as well. Basically, you’d write the shader assuming the self illumination is always defined in the alpha channel, but then default that value to zero in the hardware so if doesn’t exist in the texture to begin with, it just doesn’t do anything.

Then you’d of course need to define the textures that are used this way so that when you’re setting the samplers you can pull this trickery (probably through naming conventions or something “Blah_Glow.dds”).

And of course your self illum code needs to be very cheap, since it’s running all the time. If it’s not then it would definitely be better to have the compiler spin out 2 permutations. Though as other people have mentioned, that’s not trivial either unless your engine is already set up for it.

Note: I seriously doubt this is the quickest way of solving your problem, but it’s a good trick to know about if you’ve got engine programmers that know the hardware well. In the ps3 docs, i think this feature is called “crossbar”.

Use 1 - alpha. Post-process your glow textures to flip them. 2 instructions aren’t going to kill you. The workflow win isn’t there for a small team. Assuming you have a uniform free, you could also have a glow intensity parameter. With a default texture, leave it zero, you get nothing, this would also give you an easy path to overbrighten, or modulate from the engine. Intensity * texture + other lighting should at max add 2 instructions to your shader, mov/mul/mad compared to mov/mad if you have a separately settable color.