• Advertisement
Sign in to follow this  

Generating Terrain on the GPU vs on the CPU

This topic is 1630 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, 

I am working on a little procedural planet (like many others) and I recently tried putting my terrain algorithms in a GLSL vertex shader to see if there was a performance difference. There wasn't. After seeing this I was wondering why many people decide to do terrain calculations on the GPU. Is there something I am missing to all of this? Thanks for any replies. 

Share this post


Link to post
Share on other sites
Advertisement

Last time I heard that in person, a friend of mine was comparing his i7 2xxx to a few years old GF 8600 GT.

If you were doing the computations on CPU, perhaps you were uploading the result as static data?

Share this post


Link to post
Share on other sites

Graphical applications usually are bottlenecked by pixel shader, while vertex shader and cpu are being idle most of the time. People usually move computations from cpu to vertex shader to save cpu/memory resources for other cpu specific tasks like AI, physics.

In your case, you have moved some calculation from cpu which was idle most of the time to vertex shader which was also idle most of the time. The performance of your application was determined by pixel shader which was not changed and overall performance of your application was not changed, too. But you should be able to see difference in loads on cpu and vertex shader using some performance profiling tool like Nvidia PerfHud.

Share this post


Link to post
Share on other sites

Last time I benchmarked it, I had a roughly 10x performance gain from running all my fractal noise generation and normal map (sobel filter) in the pixel shader, versus on the CPU.

 

That said, there was a significant degree of optimisation behind both my CPU and GPU implementations. If you haven't carefully tuned all aspects of your shader pipeline, you may not see such gains.

Share this post


Link to post
Share on other sites

I have also recently (this week) written a marching cubes terrain generator on both the CPU and GPU and measured both generation times based on different grid sizes. 

 
Using a 32x32x32 grid the results are:
GPU:    ~2ms        (30x faster)
CPU:    ~60ms
 
Using a 42x42x42 grid:
GPU:    ~2ms        (80x faster)
CPU:    ~160m
 
Using a 64x64x64 grid:
GPU:    ~2ms        (170x faster)
CPU:    ~340ms
 
These results seemed a little far-fetched to me so I ran the GPU generation every frame since at 2ms, this should mean real-time terrain updates, right? It slowed down considerably but after tweaking how the memory was managed and not having to allocate huge chunks of memory every frame it ran at ~10ms which is quite acceptable for a level editor or similar application. It is worth noting that this implemented using a ATI HD 6870 GPU and a AMD Phenom II x4 840 (3.21GHz) and the CPU was only using a single core. The GPU generation was done using a DX11 compute shader and an AppendStructuredBuffer. As stated above it will depend on your implementation and how that is optimized but here are some numbers to show at least how much of a gain I have seen and that it is worth doing.
 
-BiT

Share this post


Link to post
Share on other sites
Wow I didn't think gains like that we're possible. I must be mismanaging something to be getting these subpar results. I will run it through a profiler and see if there are any obvious issues.

Last time I benchmarked it, I had a roughly 10x performance gain from running all my fractal noise generation and normal map (sobel filter) in the pixel shader, versus on the CPU.
 
That said, there was a significant degree of optimisation behind both my CPU and GPU implementations. If you haven't carefully tuned all aspects of your shader pipeline, you may not see such gains.

Yea I really didn't do any optimization before just porting the code over to a shader so this may be a source of the error. Just out of curiosity how do you render terrain from a pixel shader?

Share this post


Link to post
Share on other sites


Just out of curiosity how do you render terrain from a pixel shader?

Every terrain tile has a heightmap, a normal map and a colour map - these are all generated into textures using an FBO and a pixel shader. There are three stages, the first generates the heightmap using a ridged multi-fractal simplex noise generator, the second stage takes that heightmap and runs a sobel filter to generate the normal map, and the third stage takes both height and normal maps, and produces a diffuse/specular texture based on the height and slope of the terrain.

 

The terrain itself is a pretty normal quad-tree of mesh tiles, which is all generated and managed on the CPU, and rendered much as you would expect. Basically your standard normal mapping + phong shading.

Share this post


Link to post
Share on other sites

Graphical applications usually are bottlenecked by pixel shader, while vertex shader and cpu are being idle most of the time. People usually move computations from cpu to vertex shader to save cpu/memory resources for other cpu specific tasks like AI, physics.

In your case, you have moved some calculation from cpu which was idle most of the time to vertex shader which was also idle most of the time. The performance of your application was determined by pixel shader which was not changed and overall performance of your application was not changed, too. But you should be able to see difference in loads on cpu and vertex shader using some performance profiling tool like Nvidia PerfHud.

 

Most modern GPUs have unified shader models, so there are no dedicated vertex or fragment units. They are shared.

Share this post


Link to post
Share on other sites
[quote name="ossaltu" post="5078660" timestamp="1374139902"]
Most modern GPUs have unified shader models, so there are no dedicated vertex or fragment units. They are shared.


So doing these calculations on the vertex shader are no different (performance wise) than doing them on a pixel shader?


Just out of curiosity how do you render terrain from a pixel shader?

Every terrain tile has a heightmap, a normal map and a colour map - these are all generated into textures using an FBO and a pixel shader. There are three stages, the first generates the heightmap using a ridged multi-fractal simplex noise generator, the second stage takes that heightmap and runs a sobel filter to generate the normal map, and the third stage takes both height and normal maps, and produces a diffuse/specular texture based on the height and slope of the terrain.

The terrain itself is a pretty normal quad-tree of mesh tiles, which is all generated and managed on the CPU, and rendered much as you would expect. Basically your standard normal mapping + phong shading.

That is really interesting. Is it more efficient to generate height maps in a pixel shader than to do the calculations on a vertex shader and just modify the position? Also are each of your quads/tiles individual meshes?

Share this post


Link to post
Share on other sites


That is really interesting. Is it more efficient to generate height maps in a pixel shader than to do the calculations on a vertex shader and just modify the position?

The heightmap and normal map are roughly 16x the resolution of the mesh tiles. At one point I had a separate CPU-side noise generator to produce the meshes, but vertex-texture-fetch seems to work just fine.

 

Also are each of your quads/tiles individual meshes?

There are all the exact same mesh, instanced once per tile.

Share this post


Link to post
Share on other sites

Most modern GPUs have unified shader models, so there are no dedicated vertex or fragment units. They are shared.


So doing these calculations on the vertex shader are no different (performance wise) than doing them on a pixel shader?


Yes and no.
Per-vertex/per-pixel the amount of time taken will be the same but the pixel shader is going to be (more than likely) executed many more times than if you did the same work per-vertex.

Share this post


Link to post
Share on other sites

Sorry to mess with this old thread but after a lot of tweaking I got my openCL program to generate a 128x128 heightmap in ~1ms. I was doing a lot of non-GPU friendly things in my previous implementation...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement