Daniel
Daniel

Reputation: 6842

Why do programmers use constant and static variables in OpenGL?

I've noticed that programmers make variables const and static. I understand what these keywords do, but I have no idea why they use them.


Example 1
from The Official Guide to Learning OpenGL, Version 1.1 Chapter 2

    static GLint vertices[] = {25, 25,
                              100, 325,
                              175, 25,
                              175, 325,
                              250, 25,
                              325, 325};

    static GLfloat colors[] = {1.0, 0.2, 0.2,
                              0.2, 0.2, 1.0,
                              0.8, 1.0, 0.2,
                              0.75, 0.75, 0.75,
                              0.35, 0.35, 0.35,
                              0.5, 0.5, 0.5};

    glEnableClientState (GL_COLOR_ARRAY);
    glEnableClientState (GL_VERTEX_ARRAY);

    glColorPointer (3, GL_FLOAT, 0, colors);
    glVertexPointer (2, GL_INT, 0, vertices);

Why in the world do these arrays have to be static if they're only being used in this single object instance?


Example 2
from OpenGL Programming on wikibooks tutorial 1

    GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);

    const char *fs_source =
    "#version 120           \n"
    "void main(void) {        "
    "  gl_FragColor[0] = 0.0; "
    "  gl_FragColor[1] = 0.0; "
    "  gl_FragColor[2] = 1.0; "
    "}";

    glShaderSource(fs, 1, &fs_source, NULL);

The fs_source char array doesn't get changed after it gets set. But is it absolutely necessary to make it constant? Does it increase performance?

Upvotes: 4

Views: 914

Answers (3)

datenwolf
datenwolf

Reputation: 162164

I understand what these keywords do,

Do you?

but I have no idea why they use them.

DO you?!

Why in the world do these arrays have to be static if they're only being used in this single object instance?

What object? What instance? The book you quoted this example from is written in C, not C++, and while it is perfectly possible to implement objects and instances with C, the language itself has no definition of those built in. As such I think your very idea of what static does in this one case is wrong.

In the given example, if static is applied on the module level, onto a global variable. The effect of this is, that the variable is visible to only the compilation unit it has been defined in. It's a common and recommended practice in C programs to declare everything static that's not to be referenced outside the current compilation unit.

Using static on constant data within a functions scope saves the overhead of going through a full initialization of the variable, each time the function is called.

The fs_source char array doesn't get changed after it gets set.

And you actually could not. The C language specifies, that string literals that are not part of a char array initialization, are of type const char * and reside in a dedicated, read-only segment of the program image. Since the type of a non-initializer string literal is const char * assigning such a pointer variable to a char * invokes undefined behaviour.

But is it absolutely necessary to make it constant?

Try what happens if you remove the const and compile the program with -Werr. This is actually a matter of language correctness.

Upvotes: 4

wbit
wbit

Reputation: 192

In the file we're talking about, varray.c (example 1):

The statics in the example are not declared at module level and their visibility is inside the function only. The static keyword can mean different things at different times, but in this case, it's not to limit their accessibility from other code files (but by limiting their scope to function only that happens anyway).

Those arrays are declared/generated only once during the running of the program. Adding the static keyword in front of those declarations (say in setupPointers) means that those lines of code (59-64, 65-70) are only executed once, and the pointers themselves will not change, and those numbers will not have to "flow through the pipes" again (see above answer re performance) as they are already there.

This is because when the user clicks the mouse button the program toggles between two drawing routines. It makes sense to initialize the arrays only once instead of every time the setupPointers function is called.

So yeah that's one of the ways the static keyword works, it tells the compiler to only initialize this variable once. const is similar but it means more like "only write to this memory once".

You might say then, well why not make them const? I know, right?

Perhaps these links will help people:

varray.c

a blog post about the static keyword

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 881113

Yes, usually, it does increase performance, though not necessarily in the way you imagine.

With these hints, the compiler is free to choose different optimisations (both for space and time), making the code more efficient in the process.

For example, a static storage duration variable (a) is initialised once on program startup and never has to be initialised again.

Marking something as const allows the compiler to generate more efficient code, knowing full well that the data won't change. As an extreme example, it can load the value into a dedicated CPU register and never look at the data in memory again.

So yes, it can increase performance, not because the same instructions under the covers may run faster, but because the compiler is free to use different instructions based on the extra knowledge you have given it.


(a): Assuming that's the meaning you have of static. There's a couple of other things that keyword is used for regarding limiting its scope of visibility, which is good programming practice.

Upvotes: 4

Related Questions