Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I know the title is a bit vague but it's hard to describe what I'm really looking for, but here goes.

When it comes to CPU rendering, performance is mostly easy to estimate and straightforward, but when it comes to the GPU due to my lack of technical background information, I'm clueless. I'm using XNA so it'd be nice if theory could be related to that.

So what I actually wanna know is, what happens when and where (CPU/GPU) when you do specific draw actions? What is a batch? What influence do effects, projections etc have? Is data persisted on the graphics card or is it transferred over every step? When there's talk about bandwidth, are you talking about a graphics card internal bandwidth, or the pipeline from CPU to GPU?
Note: I'm not actually looking for information on how the drawing process happens, that's the GPU's business, I'm interested on all the overhead that precedes that.

I'd like to understand what's going on when I do action X, to adapt my architectures and practices to that.

Any articles (possibly with code examples), information, links, tutorials that give more insight in how to write better games are very much appreciated. Thanks :)

share|improve this question
1  
Although this was originally XNA, I've added the DirectX tag, as that is the underlying technology - it might help you get better answers. Also check out this answer which might give you a good starting point. –  Andrew Russell Sep 27 '12 at 12:59
    
@AndrewRussell Thanks a lot :). I've actually already read various articles on the topic including that one. But it hasn't covered everything I'd like to know. –  Aidiakapi Sep 27 '12 at 13:02

1 Answer 1

up vote 19 down vote accepted

I like to think of performance in terms of "limits". It's a handy way to conceptualise a fairly complicated, interconnected system. When you have a performance problem, you ask the question: "What limits am I hitting?" (Or: "Am I CPU/GPU bound?")

You can break it down into multiple levels. At the highest level you have the CPU and the GPU. You might be CPU bound (GPU sitting idle waiting for CPU), or GPU bound (CPU is waiting on GPU). Here is a good blog post on the topic.

You can break it down further. On the CPU side, you might be using all your cycles on data already in the CPU cache. Or you might be memory limited, leaving the CPU idle waiting for data to come in from main memory (so optimise your data layout). You could break it down further still.

(While I am doing a wide overview of performance regarding XNA, I will point out that an allocation of a reference type (class not struct), while normally cheap, could trigger the garbage collector, which will burn a lot of cycles - especially on Xbox 360. See here for details).

On the GPU side, I'll start out by pointing you to this excellent blog post which has lots of details. If you want an insane level of detail on the pipeline, read this series of blog posts. (Here's a simpler one).

To put it here simply, some of the big ones are the: "fill limit" (how many pixels you can write to the backbuffer - often how much overdraw you can have), "shader limit" (how complicated your shaders can be and how much data you can push through them), "texture-fetch/texture-bandwidth limit" (how much texture data you can access).

And, now, we come to the big one - which is what you're really asking - where the CPU and GPU have to interact (via the various APIs and drivers). Loosely there is the "batch limit" and "bandwidth". (Note that part one of the series I mentioned earlier goes into extensive details.)

But, basically, a batch (as you already know) happens whenever you call one of the GraphicsDevice.Draw* functions (or part of XNA, like SpriteBatch, does this for you). As you no doubt have already read, you get a few thousand* of these per frame. This is a CPU limit - so it competes with your other CPU usage. It's basically the driver packaging up everything about what you have told it to draw, and sending it off to the GPU.

And then there is the bandwidth to the GPU. This is how much raw data you can transfer there. This includes all the state information that goes with batches - everything from setting rendering state and shader constants/parameters (which includes things like world/view/project matrices), to vertices when using the DrawUser* functions. It also includes any calls to SetData and GetData on textures, vertex buffers, etc.

At this point I should say that anything that you can call SetData on (textures, vertex and index buffers, etc), as well as Effects - remains in GPU memory. It is not constantly re-sent to the GPU. A draw command that references that data is simply sent with a pointer to that data.

(Also: you can only send draw commands from the main thread, but you can SetData on any thread.)

XNA complicates things somewhat with its render state classes (BlendState, DepthStencilState, etc). This state data is sent per draw call (in each batch). I am not 100% sure, but I am under the impression that it is sent lazily (it only sends state that changes). Either way, state changes are cheap to the point of free, relative to the cost of a batch.

Finally, the last thing to mention is the internal GPU pipeline. You don't want to force it to flush by writing to data that it still needs to read, or reading data that it still needs to write. A pipeline flush means it waits for operations to finish, so that everything is in a consistent state when data is accessed.

The two particular cases to watch out for are: Calling GetData on anything dynamic - particularly on a RenderTarget2D that the GPU may be writing to. This is extremely bad for performance - don't do it.

The other case is calling SetData on vertex/index buffers. If you need to do this often, use a DynamicVertexBuffer (also DynamicIndexBuffer). These allow the GPU to know that they will be changing often, and to do some buffering magic internally to avoid the pipeline flush.

(Also note that dynamic buffers are faster than DrawUser* methods - but they have to pre-allocated at the maximum required size.)

... And that's pretty much everything I know about XNA performance :)

share|improve this answer
    
Thanks you very much! This is exactly what I was looking and hoping for :). –  Aidiakapi Sep 27 '12 at 15:36
1  
A few hundred batches per frame sounds overly pessimistic. The rule of thumb I've always heard is 2K to 3K batches per frame. Some games have been known to get up to 10K on PC, but I think that requires great care to achieve. –  Nathan Reed Dec 6 '13 at 20:44
    
Quite right. The "a few hundred" figure comes from the "batch batch batch" paper - which listed "25k batches/s @ 100% of a 1GHz CPU". But that paper is now a decade old, and drivers and CPUs have improved significantly since then. I'll update this (and my others) to read "a few thousand". –  Andrew Russell Dec 7 '13 at 8:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.