Optimizing drawing calls in a basic XNA engine



I want to optimize my basic XNA engine. The structure is somewhat like this: I've a GameWorld instance and more GameObjects attached to it. Now, in every frame I do a loop between GameObjects and I call the draw method inside of them. The con of this implementation is that the GameDevice draw function is called multiple times, one for every object.
Now, I want to reduce the drawing calls, implementing a structure that, before the drawing method is called, transfers all the geometry in a big vector cointains all the vertex data and performs a single drawing call to draw them all.

Is that an efficient way? Someone can tell me a solution to optimize?

Thanks


The first step is to reduce the number of objects you are drawing. There are many ways to do this, most commonly:

  • Frustum culling - ie cull all objects outside of the view frustum

  • Scene queries - eg organise your scene using a BSP tree or a QuadTree - some data structure that gives you the ability to reduce the potentially visible set of objects

  • Occlusion culling - more advanced topic but in some cases you can determine an object is not visible because it is occluded by other geometry.

  • There are loads of tutorials on the web covering all these. I would attack them in the order above, and probably ignore occlusion culling for now. The most important optimisation in any graphics engine is that the fastest primitive to draw is the one you don't have to draw.

    Once you have you potentially visible set of objects it is fine to send them all to GPU individually but you must ensure that you do so in a way that minimises the state changes on the GPU - eg group all objects that use the same texture/material properties together.

    Once that is done you should find everything is pretty fast. Of course you can always take it further but the above steps are probably the best way to start.

    Just to make the point clear - don't just assume that less drawing calls = faster. Of course it depends on many factors including hardware but generally XNA/DirectX API is pretty good at queueing geometry through the pipeline - this is what it's for after all. The key is not minimising calls but minimising the amount of changes in state (textures/shaders etc) required across the scene.

    链接地址: http://www.djcxy.com/p/95512.html

    上一篇: 构建2D XNA游戏引擎

    下一篇: 在基本的XNA引擎中优化绘图调用