Question

Is there a way to execute a function only once with some kind of boolean check on GLSL? Or am i missing something important? The code below doesn't seem to help.

bool once = false;

DoOnce()
{
    for (int i = 0; i < someConst; i++)
    {
        //do some things...
    }
    once = true;
}

void main()
{
    if ( !once )
    {
       DoOnce();
    }
    //do other things         
}

Or do I have to use a compute shader for this kind of behavior?

Was it helpful?

Solution

It looks like you want to generate some data that can later be used by shader invocations, and that's not how you do it. @Nicol was of course right, and that function will be called per-invocation, which will be one time for each vertex for vertex shader and so on. In theory you could use Transform Feedback buffer to store done value, but that's most certainly not a good solution.

If you want to write GLSL code generating such cache, separate that code in two parts: cache generation and cache usage. First, run the cache generation into some buffers (their type depends on what data you are generating; these can be textures, VBOs or something different), and then bind and use these buffers in actual drawing code.

If you specified the cache content, I might be able to help you more with the setup, but as that varies rather much (texture/VBO construction, then binding via either vertex attributes, perhaps with instance divisor or appropriate sampler for texture), it would be only a blind guess right now.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top