Defining variables globally in glsl to avoid polutting function headers

I’m working on a pbr fragment shader, and I have a lot of functions that require variables like normal direction, camera direction etc. I can declare and initialize them inside the main function and pass them as arguments to every function that needs them. But I was wondering if it would be a bad practice to just declare them as “global” variables (declaring outside main or other functions; not sure what they are actually called), initialize them inside main, and then just use them inside functions. Because otherwise they kind of pollute functions headers and it’s harder to read the code.

One issue I encountered is that when declaring a variable “globally”, despite initializing it before I use it inside a function, the compiler complained that the variable was used unitialized. So I have to assign them a value when declaring them, despite initializing them again in main.

Possibly useful to you:


There may be another path through your code where the uninitialized value would be read before written. You might prune back an example to the bare minimum needed to repro (GLSL source for a single shader stage) and post here for ideas. I suspect though you may solve your problem in the process of cooking this minimal repro.

Also, which GPU driver and version are you using to compile this?