Absolute beginner issue

Hello. Im not sure this is the right forum to ask this as it’s a VERY beginner question, so feel free to bounce me somewhere else if it’s the case.

I am a newbie in GLSL and after many tutorial i’d like to make a fragment shader and i’m wondering about the right approach.
Such shader should basically produce the attached image.

It’s just x horizontal lines with random height (in a range of course) filled with a random color from a predefined palette, regenerating every frame.

It’s something pretty stupid but I can’t manage to find a decent GLSL-way to do that :expressionless:

Right now i feed to the shader an array of ‘heights’ ([.1,.12,.16…]) and an array of colors and it works, but I feel this is a very bad way to do that, style-wise and performance-wise.

Anybody can help?

TIA

The best solution is to just pass the colour index for each row as a uniform array (or 1D texture).

Making the fragment shader do this is rather awkward, given the way that fragment shaders work. Each invocation of the fragment shader is independent of the others, so you can’t use the results of calculations performed in one fragment shader in the “next” invocation (there isn’t any ordering; invocations may run in parallel). So you would have to perform the exact same calculations for every fragment, which is rather inefficient. It would be more efficient to perform the calculations on the CPU and pass in the array indicating which colour is used for each row, so the fragment shader just needs to read an array element and look up the corresponding colour in the palette.

[QUOTE=GClements;1292687]The best solution is to just pass the colour index for each row as a uniform array (or 1D texture).

Making the fragment shader do this is rather awkward, given the way that fragment shaders work. Each invocation of the fragment shader is independent of the others, so you can’t use the results of calculations performed in one fragment shader in the “next” invocation (there isn’t any ordering; invocations may run in parallel). So you would have to perform the exact same calculations for every fragment, which is rather inefficient. It would be more efficient to perform the calculations on the CPU and pass in the array indicating which colour is used for each row, so the fragment shader just needs to read an array element and look up the corresponding colour in the palette.[/QUOTE]

Well thank you VERY much for this answer. What I need is to learn a bit of GPU state-of-mind, as a beginner it’s not always obvious what’s the best way to do things shader-wise.

Thanks again.

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