Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

So just to restate that, let's say we have this:

float f = g / 2;

Given the divisor is a constant, will the shader compiler auto-optimise this to a bitshift operation, as some language compilers are known to do? Where would I find more info on shader program compiler implementation details?

(I'm guessing there will be different answers for OpenGL & DirectX.)

share|improve this question

1 Answer 1

up vote 4 down vote accepted

If f and g are ints, then yes, compilers will in general do this type of optimization where it makes sense.

If f and g are floats, then multiplies/divides by powers of two cannot be done by bit-shifting. However, as it happens, some GPUs have hardware that can do a free multiplication by certain powers of two (such as 0.5, 2, or 4) at the same time as some other operation. So, for instance, (f + g) / 2 could be translated to (f + g) * 0.5 and done for free along with the addition.

Shader compiler implementation details are, unfortunately, not readily available. Both NVIDIA and AMD tend to be fairly secretive about this stuff. However, you can often look at the disassembly produced by the compiler for a given shader. Each vendor provides some tools to do that, such as NVIDIA ShaderPerf and AMD GPUShaderAnalyzer.

Note that the details will vary by GPU vendor and by chipset within a vendor, but will generally be the same for OpenGL and DirectX as those are just two different front-ends to the same back-end instruction set.

share|improve this answer
    
Great answer, thanks! –  Nick Wiggill Nov 30 '12 at 22:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.