Skip to content

About the GLSL shaders integration in gdx2d

PA Mudry edited this page Jan 30, 2014 · 1 revision

What is it about ?

I am currently adding OpenGL ES Shading Language (GLSL in short) support to gdx2d. This is done without breaking any API (which is good) and should provide a nice way to accelerate some heavy graphics operations.

Why using shaders ?

Rendering animation smoothly in Java is something not so easy to perform. Especially, imagine that you want to display an image with each pixel having a different color. This is doable but it will be very slow because every pixel will be treated sequentially and there are lots of them for an image (´width*height´).

GLSL shaders enable you to take a more powerful approach to the problem: you can describe what you want for the pixels and the graphics card runs this in parallel (which is very fast and does not use you main CPU). For instance, instead of making the standard nested-loop to iterate over all the pixels, you may say something like :

void main() {
// position of the fragment, normalized to [0..1]
vec2 position = ( gl_FragCoord.xy / resolution.xy );

// the color of the fragment depends on its position, linearly  
gl_FragColor = vec4(0, 0, position.y, 1.0);
}

which will make a smooth gradient from left to right. There is plenty of documentation out there, but the main characteristics of shaders are:

  1. They are written in a special language called a shading language which is tailored at describing pixel operations.
  2. They are very fast because executed on the GPU instead of the CPU.
  3. They require a different approach to graphics programming, because you describe the contents of the pixels and no longer how to draw them (more or less).

What is the current status of the integration

You can already have shaders in gdx2d and the latest versions of the repository contain many examples on how to use them (in the ´shaders´ directory of the Java source code).

Future plans

  1. Integrate shaders to render some of the primitives (such as the circles, rectangles...)
  2. Develop more demos !
Clone this wiki locally