Default value of uninitialized local variable and uniforms

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.

[QUOTE=checkcheck;1282384]what is the default value of uninitialized local variables?

float myFloat;

Is it zero?[/QUOTE]
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.

[QUOTE=checkcheck;1282384]What is the value of an uniforms if it hasn’t been set?

uniform float myFloat;

Can’t find anything in the spec.[/QUOTE]
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.

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