Jump to content

  • Log In with Google      Sign In   
  • Create Account

Generating Terrain on the GPU vs on the CPU


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 multifractal   Members   -  Reputation: 229

Like
0Likes
Like

Posted 18 July 2013 - 12:57 AM

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. 



Sponsor:

#2 Krohm   Crossbones+   -  Reputation: 3117

Like
0Likes
Like

Posted 18 July 2013 - 01:31 AM

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?



#3 Waterlimon   Crossbones+   -  Reputation: 2562

Like
0Likes
Like

Posted 18 July 2013 - 02:23 AM

You were likely bottlenecked by some other part of the pipeline. Drawcalls, pixel shading...

o3o


#4 ossaltu   Members   -  Reputation: 190

Like
0Likes
Like

Posted 18 July 2013 - 03:31 AM

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.



#5 swiftcoder   Senior Moderators   -  Reputation: 9992

Like
0Likes
Like

Posted 18 July 2013 - 05:40 AM

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.


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#6 RnaodmBiT   Members   -  Reputation: 641

Like
1Likes
Like

Posted 18 July 2013 - 04:37 PM

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


#7 multifractal   Members   -  Reputation: 229

Like
0Likes
Like

Posted 18 July 2013 - 07:56 PM

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?

#8 swiftcoder   Senior Moderators   -  Reputation: 9992

Like
1Likes
Like

Posted 18 July 2013 - 08:10 PM


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.


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#9 Ameise   Members   -  Reputation: 724

Like
1Likes
Like

Posted 19 July 2013 - 04:14 PM

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.



#10 multifractal   Members   -  Reputation: 229

Like
0Likes
Like

Posted 19 July 2013 - 08:13 PM

[quote name="Ameise" post="5079031" timestamp="1374272051"][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.[/quote]

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?

#11 swiftcoder   Senior Moderators   -  Reputation: 9992

Like
0Likes
Like

Posted 20 July 2013 - 09:24 AM


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.


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#12 phantom   Moderators   -  Reputation: 7267

Like
0Likes
Like

Posted 20 July 2013 - 11:36 AM

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.

#13 multifractal   Members   -  Reputation: 229

Like
0Likes
Like

Posted 05 August 2013 - 12:41 AM

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...






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS