libgpcogl - GLI

Tutorial

These examples are small and simple and designed to demonstrate concepts one by one, starting from simple going toward complex. They all compile directly from the source tree, without the lib installed. This makes the build part different from what a real application would have. There is a separate example later, showing how to use the installed library.

example description
glinfo Initialize the library, create a context and print context info.

Demonstrates initialization and context creation.

dist_sq Create a 20*20 array in GPU memory and calculate the distance square of each cell from 0;0 in a shader program, then download and print. Each array cell is a single uint32_t.

Demonstrates computation and download.

dist_sq_const Same as dist_sq, but instead of calculating the cell distance square from 0;0, it calculates distances from a specific cell named in the C program. The coords of this origin cell is passed to the shader program as a global constant.

Demonstrates global consts.

dist_sq_sub Same as dist_sq, but runs only one a small portion of the whole output. The output array needs to be cleared after creating it in GPU memory; previus examples didn't need to clear the array because they wrote every cell. The _box variant of compute limits the shader to a rectangle within the output, not changing any value outside of that box.

At the end the whole array is downloaded and printed, plus a partial download is performed.

Demonstrates partial execution and partial download.

stst_temp Steady state temperature distribution simulation. A metal plate with sorrunding temperature and cutouts with fixed temperature within the cutout is simulated (these are the boundary conditions). The metal plate is split up into finite element squares. By the end of the simulation the temperature fo each square is determined.

The simulation is naive and crude: in steady state at the end, it is true that other than the boundary condition fixed cells (the input), each cell's temperature is the average of its neighbors. The code simply iterates this over and over, swapping two state arrays, overwriting boundary condition cells from the input array between the swaps. If the process is repeated for long enough, the state array approximates steady state temperature.

The _cpu variant is implemented to run on the CPU (single thread), the _gpu variant uses libgpcogl to run on GPU. The stdout of the two must match.

Demonstrates how to pass in input arrays, how to execute multiple computations on arrays stored in GPU memory without transfers to CPU memory.

mx_scale Take an N*M input matrix and a scalar value and multiple each component of the matrix with the scalar. The scalar is passed as a global const.

Demonstrates how to use an acompute() call passing GPU arrays and global consts without having to use vararg. This is favorable in case the caller needs to build these bindings dynamically.

line_dir Input is an N*M (2*3 in this example) table of line segments. Each line segment is four 32 bit integer coordinates. Output is two 32 bit integers per line segment: quadrant, slope. Quadrant is 1, 2, 3 or 4 depending on which quadrant the segment takes if origin is placed on the starting point of the segment; slope is abs(dy/dx).

Demonstrates how to use integer vector arrays.

line_dir_find Same as line_dir, but also collects the x,y index (in the input) of the line segments whose slope is greater than 5000. This example requires opengl 4.3 or extensions for atomic counters and buffers.

Demonstrates using atomic counters and shared buffers.

Using the installed version of the library

Example dist_sq_inst is a copy of the base example dist_sq with the Makefile modified to rely on libgpcogl system-installed (to /usr or /usr/local typically). This is how an application would use libgpcogl.

Before compile, configure which GLI to use by commenting out one of the LDLIBS line in the Makefile.

Note: other than the -l directives in LDLIBS, there is nothing else that's gpcogl-specific in the Makefile. OpenGL lib dependencies are brought in trhough dynamic linking via those links in LDLIBS, header files are found on standard paths.

Benchmarking

Example stst_temp_big is used for benchmarking. It is the same example as stst_temp , but instead of a running 100 iterations on a 20x20 array, it runs 1000 iterations on an 1000x1000 array (which is not enough to reach steady state). The input is generated by filling up edges with 9 and placing a single 1 in the middle.

Benchmarking proces: