Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Absolute beginner issue

  1. #1
    Newbie Newbie
    Join Date
    Oct 2018
    Posts
    2

    Smile 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 :|

    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
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	GeER95q.jpg 
Views:	6 
Size:	11.3 KB 
ID:	2848  

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,955
    Quote Originally Posted by jurito View Post
    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.
    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.

  3. #3
    Newbie Newbie
    Join Date
    Oct 2018
    Posts
    2
    Quote Originally Posted by GClements View Post
    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.

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •