Skip to content

Shaders in Java

March 8, 2010

I’ve recently started developing shaders in Java. Shaders are a set of software instructions used to calculate rendering effects on graphics hardware.  More specifically, they usually come coupled as two types of shaders: vertex and fragment (or pixel) shaders. There are also geometry shaders but these are less implemented. Shaders are used to program the graphics processing unit (GPU) rendering pipeline to allow for customizable geometry transformations and shading functions. To be more specific, we consider this image:

where the business-as-usual diagram of the graphics pipeline is shown on the top. What shaders do is redirect the pipeline so it processes the vertex processing and fragment processing how you want.  Note that setup and rasterization is left undisturbed, so interpolation (for example) is still being implemented by the hardware. Yay!

The language that is typically used (not always) it called GLSlang, or simply GLSL. Basically by making a few extra calls to OpenGL one can implement their own vertex and fragment shader algorithms. The shaders are written as a basic text file (extension is arbitrary – I’ve seen .glsl, .vertex, .fragment, .fp, .vp, .shd, etc. I use .glsl to avoid confusion). There is an extension to VIM to allow syntax highlighting, so for VIM users who use shaders this is useful.  Then by reading in the text file the user can compile and apply their shader algorithm. Let me emphasize that GLSL is its own language. It’s roughly based off C, and follows much of the guidelines that C and Java do, in that clauses are concluded with semicolons, and functions are wrapped in brackets, etc. For more information I recommend that you consult the OpenGL Orange Book. While there are quite robust methods of programming GPUs, it is not quite standardized as of yet (note CUDA, HLSL, Cg, and the list continues).

So, why do we want to use shaders? Put succinctly, shaders are written to apply transformations to a large set of pixels at a time. Meaning, shaders are well suited to parallel processing, and most modern GPUs have multiple shader pipelines to facilitate just that, thus dramatically reducing the computation time of rendering processes.

Using shaders in Java requires almost less effort than in C (I know — tell me what’s new). For example, forget using the OpenGL Extension Wrangler, or GLEW. This is unnecessary. What you need to look out for is how to load the files. Since shaders are essentially just text files, you want to load in the shader file as a String object. I find that this helps tremendously:

FileInputStream f = new FileInputStream(fileName);
int size = (int)(f.getChannel().size());
byte[] source = new byte[size];
f.read(source,0,size);
f.close();
return new String(source,0,size);

where, assuming this is embedded in a function that returns a String, this will read in the file to allow the function glCompileShader (or glCompileShaderARB) to compile your shader function.

Below is a very simple Java class I’ve created which reads in a shader file that can be called later for compilation. It’s very straightforward, and if you’re programming the GPU using shaders in Java this may be very useful. I might add more later if I see fit.

Advertisement
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.