Maximize
Bookmark

VX Heaven

Library Collection Sources Engines Constructors Simulators Utilities Links Forum

GPGPU using OpenGL

roy g biv
Valhalla #3
December 2012

[Back to index] [Comments]

What is it?

This is a project which I started in 2007. At that time, I had only ATI card which did not support fragment programs, and High Level Shader Language did not even exist. I did not make good progress from then until now, but finally in 2012, I spent one day to write most of the code. Many things have changed since I started, but ATI cards are still not good. ;)

GPGPU stands for General Purpose Graphics Processing Unit. It means to do non-video calculations using the video card hardware. We want to do that because video card hardware runs very fast and operations can be done in the background. It is also super anti-emulation environment.

How can we do that?

Video card supports memory blocks called textures. Textures are just array of 32-bit floating-point values. For some cards, textures must be squares (number of elements must have integer square root). Some cards allow array to be any size. We can allocate several textures and work with them at the same time.

It is common to allocate two or even more textures, with one or more for input values and one for output values. We can perform operations (by running shader program) on the input texture(s) and result will be stored in output texture. We can run more than one shader program on the textures, by swapping input and output textures. This is called "ping-pong" technique.

Shader programs are written in high-level language that looks like C.

Set up environment

To use shader program, we must create a window, and then set a pixel format that says to use OpenGL. Window does not have to be visible at any time, and can be almost any class. We create a context for OpenGL and make it current.

Even we don't care about the window, we must still behave like we do. We create a framebuffer object and bind it to our context. We set a viewport for the texture mapping. Viewport must be size of our texture, otherwise some values will be clipped and lost.

We create some textures, and set the environment mode for the textures. Now we are ready for our shader program.

Shader program

We create one or more shader programs, and bind one of them to a shader for immediate use. We bind some shader source to the shader, and then compile and link the shader program. If our shader program has variables, then we need to query the location of each of them, and pass the returned values as the index when setting the variables. We need to attach the textures to the framebuffer and enable the shader program.

Variables

To use textures requires to set a variable with the id of the texture. We activate the texture to use, bind the id to the texture, and then use the variable index when setting the texture id. We can set values for other variables by using the variable index, too.

Run it!

Finally, it's time to run the shader program. We use the texture size so that the operation occurs over the whole array, by passing the co-ordinates of each corner.

After the rendering is complete, we must read back the result. Since the values are floating-point, we might need to convert them back to integers, if we want to use the result for running code.

I can't understand why the whole thing seems so complicated, but I suppose that people did not have this idea in mind when they designed the system. ;)

Greets to friendly people (A-Z):

Active - Benny - herm1t - hh86 - jqwerty - Malum - Obleak - Prototype - Ratter - Ronin - RT Fishel - sars - SPTH - The Gingerbread Man - Ultras - uNdErX - Vallez - Vecna - Whitehead

rgb/defjam jun 2012
[email protected]
By accessing, viewing, downloading or otherwise using this content you agree to be bound by the Terms of Use! vxer.org aka vx.netlux.org
deenesitfrplruua