PDA

View Full Version : Speed (textures, shaders and vertex buffers)



NecroDOME
22-08-2007, 10:44 PM
Again a speed question. This time about batching render calls.

When rendering a scene, I can have multiple shaders, textures and vertex buffers. These thins need to switch several times like


SetTexture 1
SetShader 1
SetVertexBuffer 1
DrawPrimitive (D3D render call)

SetTexture 2
SetShader 1
SetVertexBuffer 1
DrawPrimitive (D3D render call)

SetTexture 1
SetShader 1
SetVertexBuffer 1
DrawPrimitive (D3D render call)

Here the textures is set 3 times, and can be reduced to 2. But what is global the best way to batch? sort by texture, shader of vertex buffer? I personally would say shaders, cause you sometimes need to a matrix or some other stuff. (Im not asking whats best for my app, but just in global ways!)

cronodragon
23-08-2007, 12:00 AM
I sort my rendering queue based on these tests:

http://www.circlesoft.org/pages.php?pg=kbasepage&id=12

So vertex shaders have priority in the sorting, reducing their amount of calls.

NecroDOME
23-08-2007, 02:40 PM
Thanx, thats just what I needed :) . I guess I need also to sort on vertex/pixel shaders, then vertex buffers and finally textures.

cronodragon
23-08-2007, 03:06 PM
I'm glad :D You can sort all of them in a single pass, just defining a comparison function (for the sorting algorithm) that deals with their priority.

NecroDOME
23-08-2007, 03:29 PM
I was thinking of automatically sorting every frame. This way I can just and and remove object from the world and still have minimum performance lost.

More like this
Render 1 2 1 2
Sort 1 1 2 2
Render 1 1 2 2
Sort (its all sorted, so we can skip here)
Add new object 1
Render 1 1 2 2 1
Sort 1 1 1 2 2
Render 1 1 1 2 2
Sort (its all sorted, so we can skip here)

etc...

Mirage
23-08-2007, 05:58 PM
These timings are from the article "Accurately Profiling Direct3D API Calls" which can be found in Direct9 SDK help file.

If you read it you'll see that the timings are CPU clocks needed to queue commands. They tell us nothing about GPU clocks needed to actually perform the commands. If you change a shader, GPU will stall (GPU probably will need to clear its conveyor). When you switch a texture its only a pointer change.

More accurate priority (based on tests) you can find here:
http://tomsdxfaq.blogspot.com/
------
-Pixel shader changes.
-Pixel shader constant changes.
-Vertex shader changes.
-Vertex shader constant changes.
-Render target changes.
-Vertex format changes (SetVertexDeclaration).
-Sampler state changes.
-Vertex and index buffer changes (without changing the format).
-Texture changes.
-Misc. render state changes (alpha-blend mode, etc)
-DrawPrim calls.
------

I think on SM3 hardware shader constant changes should be less expensive as there is no need to internally replace a shader with a new one (on old GPU's shader constants is a part of a shader).

NecroDOME
23-08-2007, 07:00 PM
Cool, thanx for the information!!