Question

The question is 2D specific.

I am a constantly updating texture, which is a render target for one of my layers. The update is a whole redraw of the texture and is performed by drawing sprites and outputting text. The operation is performed frequently, consumes quite a lot of CPU and I have, of course, optimized the number of the redraws to keep it down.

Is there a way to buffer these operations in Direct3D? Because currently I have to repeatedly construct a chain of sprite/text operations. Lets assume any game performing a world update - how do they overcome this tedious work? Maybe by creating more layers?

The best thing for me would be creating a modifiable draw chain object, but I haven't found anything like this in Direct3D.

Was it helpful?

Solution

There are a few general methods you might look into:

  • Batching: Order and combine draws to perform as few calls as possible, and draw as many objects between state changes as you can.
  • Cache: Keep as much geometry in vertex buffers as you can. With 2D, this gets more interesting, since most things are textured quads. In that case...
  • Shaders: It may be possible to write a vertex shader that takes a float4 giving the X/Y position/size of your quad, then use that to draw 4 vertexes. You won't need to perform full matrix state changes then, just update 4 floats in your shader (skips all the view calculations, 75% less memory and math). To help make sure the right settings are being used with the shaders, ...
  • State Blocks: Save a state block for each type of sprite, with all the colors, modes, and shaders bound. Then simply apply the state block, bind your textures, set your coordinates, and draw. At best, you can get each sprite down to 4 calls. Even still...
  • Cull: It's best not to draw something at all. If you can do simple screen bounds-checking (can be faster than the per-poly culling that will be done otherwise), sorting and basic occlusion (flag sprites with transparency). With 2D, most culling checks are very very cheap. Sort and clip and cull wherever you can.

As far as actually buffering, the driver will handle that for you, when and where it's appropriate. State blocks can effect buffering, by delivering all the modes in a single call (I forget whether it's good or bad, though I believe they can be beneficial). Cutting down the calls to:

if (sprite.Visible && Active(sprite) && OnScreen(sprite))
{
    states[sprite.Type]->Apply(); 
    device->BindTexture(sprite.Texture); 
    device->SetVertexShaderF(sprite.PositionSize); 
    device->Draw(quad);
}

is very likely to help with CPU use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top