PDA

View Full Version : default value of uninitialized local variable and uniforms



checkcheck
05-04-2016, 03:02 PM
what is the default value of uninitialized local variables?

float myFloat;
Is it zero?
What is the value of an uniforms if it hasn't been set?

uniform float myFloat;

Can't find anything in the spec.

mhagain
05-04-2016, 03:41 PM
what is the default value of uninitialized local variables?

float myFloat;
Is it zero?
Depends. If you're talking about C/C++, and if it's a debug build your compiler may initialize it to something that's useful for detecting uninitialized variables in a debugger. If it's a release build you'll most likely get stack garbage in the variable, so it could be whatever was last on the stack, so - in theory - anything.

If you're talking about uninitialized local variables in GLSL, section 5.9 of the GLSL spec (using version 4.50) has the answer:
Reading a variable before writing (or initializing) it is legal, however the value is undefined.

As a general rule, if you're writing code in any language that depends on uninitialized local variables having some value, don't. Stop. Just initialize the variable instead. You'll thank yourself for it later.


What is the value of an uniforms if it hasn't been set?

uniform float myFloat;

Can't find anything in the spec.
Using the 4.50 spec, section 4.3.5 has the answer:
All uniform variables are read-only and are initialized externally either at link time or through the API. The link-time initial value is either the value of the variable's initializer, if present, or 0 if no initializer is present.