Source code of sphere_shader examples (raycasted spheres and mesh-based spheres) written in OpenGL 3.3. Code also includes handy camera, shader classes and (GPU) timer functions based on glQueryCounter. Tested on Linux and Windows.
Dependencies: OpenGL, GLEW, and FREEGLUT and glm.
Download source code:
https://github.com/tdd11235813/spheres_shader
(Update: 2016/03/12 - refactoring of code framework)
Developing the QPTool I was interested in the performance of the different types of sphere visualizations.
- Spheres with Geometry Instancing (mesh-based)
- Sphere Impostors with hardware based Point Sprites (GL_POINT_SPRITE_ARB)
- Sphere Impostors with Geometry Shaders
- Sphere Impostors with common Shaders (Parameters via TBO)
- Sphere Impostors with common Shaders (Parameters duplicated in VBO)
Mesh-based Spheres
The first implementation uses mesh-based spheres by the help of geometry instancing [2].
In the initialization a sphere mesh is sent down to the GPU. By glDrawElementsInstanced(...) the object geometry gets cloned many times. A shader places these objects into the scene.
Usually this is a good improvement for visualizing a massive amount of same (complex) geometry.
In our case there are two disadvantages:
- Meshes are a visible inaccurate approximation of the spheres.
- Polygon data increases with the resolution of the spheres, slows down the application regardless of the screen resolution.
In this picture you see in the bottom part the artifacts by the polygon approximation of the spheres. The upper two parts show raycasted sphere impostors (screenshot from QPTool v0.9.7).
Sphere Impostors by Raycast-Shaders
These two pictures are from the sphere_shader examples. First picture
shows the mesh-based spheres and the second one the impostors.
If we want to use shaders to render the spheres in the fragment shader on our own, we need a billboard.
A billboard is a flat object, usually a quad (square), which faces the camera. [3]
This quad must be generated with texture coordinates before we can use it. A good explanation can be found here [4]. There are several ways to achieve this. It is possible to use hardware based point sprites, where for each vertex a billboard is created. In the vertex shader you just have to set the point size, which makes up the billboard size later. There are some issues with compatibility and correct visuals. If you take the particles demo from CUDA SDK, where this rendering method is utilized, you might notice wrong quad sizes, especially when the quads move to the borders of the screen. I tried to fix that, but there are still some glitches.
Another possibility uses geometry shader. Dummy vertices (one per sphere) are sent down to GPU and the geometry shader creates four vertices making the billboard for one sphere.
The last way would be sending four vertices per sphere with predefined texture coordinates down to the GPU. By this either a second data buffer is needed for the parameters OR all the parameters are duplicated (4x) in the vertex buffer object.
One disadvantage of the implemented sphere impostor shaders is, Early depth test is not possible, since z-buffer is computed in fragment shader.
Results
Setting: 800x600
Hardware: GPU Nvidia GTX 470 (Fermi), CPU Intel Quad Core i7 @3.07GHz, 12GB RAM
OS: Windows 7 64bit
Number of spheres: 100,000
Parameters per sphere: Position (4 floats), Color (4 floats), Radius (1 float)
Frames measured after warmup: 200
Benchmark Results of Rendering Methods for Spheres (in ms) | ||||||
---|---|---|---|---|---|---|
FOV = 60 | ||||||
Method | Avg1 | Avg2 | Min1 | Min2 | Max1 | Max2 |
Sphere Geometry Instanced | 121.523 | 0.013 | 121.173 | 0.006 | 122.134 | 0.023 |
Sphere Billboard Quads (TBO) | 7.080 | 0.022 | 6.938 | 0.017 | 7.201 | 0.029 |
Sphere Billboard Quads (VBO) | 1.879 | 0.002 | 1.874 | 0.001 | 1.886 | 0.003 |
Sphere Impostor Geometry Shader | 1.915 | 0.001 | 1.900 | 0.001 | 1.933 | 0.002 |
Sphere Point Sprites, with Glitches :( | 1.541 | 0.001 | 1.501 | 0.001 | 1.619 | 0.002 |
FOV = 20 | ||||||
Sphere Geometry Instanced | 121.606 | 0.015 | 121.081 | 0.003 | 122.083 | 0.027 |
Sphere Billboard Quads (TBO) | 8.320 | 0.023 | 8.291 | 0.019 | 8.359 | 0.031 |
Sphere Billboard Quads (VBO) | 8.230 | 0.001 | 8.227 | 0.001 | 8.234 | 0.001 |
Sphere Impostor Geometry Shader | 8.229 | 0.001 | 8.224 | 0.001 | 8.243 | 0.001 |
Sphere Point Sprites, with Glitches :( | 7.756 | 0.001 | 7.752 | 0.001 | 7.763 | 0.001 |
FOV: Field of view. The lower the value, the more sphere-pixels have to be drawn. It is like zoom. | ||||||
***1: Value for whole frame in ms. | ||||||
***2: Value for binding and shader config in ms. |
Sphere Impostor Geometry Shader: Takes 1 dummy vertex, creates 4 vertices for billboard on the fly.
Sphere Billboard Quads: Takes 4 vertices with preset texture coordinates for billboard. Uses either texture buffer object (TBO) for sphere parameters or parameters which are duplicated (4x) as vertex attributes in the vertex buffer object (VBO).
Sphere Geometry Instanced: Polygon representation of sphere. Using geometry instancing.
Sphere Point Sprites. Hardwarebased Billboard generation (point sprites). Alas, there are glitches and incompatibilities, so I do not recommend to use this.
My current implementation uses the geometry shader method, because it has no glitches so far and it is fast. Nevertheless I would recommend to use predefined billboards with duplicated data, since you will not find geometry shader support "everywhere".
On Linux with a Kepler GPU I obtained following interesting results:
Setting: 800x600
Hardware: GPU Nvidia GTX 670 (Kepler), CPU Intel Quad Core i7 @3.4GHz, 8GB RAM
OS: Linux Kubuntu 12.04
Number of spheres: 100,000
Parameters per sphere: Position (4 floats), Color (4 floats), Radius (1 float)
Frames measured after warmup: 200
Please leave a comment, if you encounter bugs or know more or if you have other ideas.
On Linux with a Kepler GPU I obtained following interesting results:
Setting: 800x600
Hardware: GPU Nvidia GTX 670 (Kepler), CPU Intel Quad Core i7 @3.4GHz, 8GB RAM
OS: Linux Kubuntu 12.04
Number of spheres: 100,000
Parameters per sphere: Position (4 floats), Color (4 floats), Radius (1 float)
Frames measured after warmup: 200
Benchmark Results of Rendering Methods for Spheres (in ms) | ||||||
---|---|---|---|---|---|---|
FOV = 60 | ||||||
Method | Avg1 | Avg2 | Min1 | Min2 | Max1 | Max2 |
Sphere Geometry Instanced | 50.943 | 0.003 | 50.370 | 0.001 | 53.117 | 0.013 |
Sphere Billboard Quads (TBO) | 1.087 | 0.003 | 1.069 | 0.001 | 1.159 | 0.010 |
Sphere Billboard Quads (VBO) | 1.061 | 0.001 | 1.059 | 0.001 | 1.064 | 0.002 |
Sphere Impostor Geometry Shader | 1.066 | 0.001 | 1.063 | 0.001 | 1.069 | 0.003 |
Sphere Point Sprites, with Glitches :( | 0.897 | 0.001 | 0.895 | 0.001 | 0.902 | 0.003 |
FOV = 20 | ||||||
Sphere Geometry Instanced | 50.798 | 0.004 | 50.356 | 0.001 | 53.487 | 0.015 |
Sphere Billboard Quads (TBO) | 6.191 | 0.001 | 6.137 | 0.001 | 6.747 | 0.008 |
Sphere Billboard Quads (VBO) | 6.133 | 0.001 | 6.130 | 0.001 | 6.137 | 0.003 |
Sphere Impostor Geometry Shader | 6.121 | 0.001 | 6.116 | 0.001 | 6.127 | 0.002 |
Sphere Point Sprites, with Glitches :( | 5.442 | 0.001 | 5.440 | 0.001 | 5.446 | 0.002 |
FOV: Field of view. The lower the value, the more sphere-pixels have to be drawn. It is like zoom. | ||||||
***1: Value for whole frame in ms. | ||||||
***2: Value for binding and shader config in ms. |
Please leave a comment, if you encounter bugs or know more or if you have other ideas.
[1] -
https://web.archive.org/web/20150215085719/http://www.arcsynthesis.org/gltut/illumination/tutorial%2013.html
[2] - http://wiki.delphigl.com/index.php/shader_Instancing
[3] - http://nehe.gamedev.net/article/billboarding_how_to/18011/
[4] - http://www.sunsetlakesoftware.com/2011/05/08/enhancing-molecules-using-opengl-es-20