DirectX9 - 高效绘制精灵
我正在尝试创建一个平台游戏,并且我正在使用各种精灵块,并将它们拼凑在一起以绘制关卡。这需要每一帧在屏幕上绘制大量精灵。一台好的计算机在处理绘制所有精灵时没有问题,但它开始影响旧计算机的性能。由于这不是一个大型游戏,我希望它能够在几乎任何计算机上运行。现在,我正在使用以下 DirectX 函数来绘制我的精灵:
D3DXVECTOR3 center(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 position(static_cast<float>(x), static_cast<float>(y), z);
(my LPD3DXSPRITE object)->Draw((sprite texture pointer), NULL, ¢er, &position, D3DCOLOR_ARGB(a, r, g, b));
是否有更有效的方法在屏幕上绘制这些图片?有没有一种方法可以使用不太复杂的图片文件(我现在使用常规的 png)来加快速度?
总结一下:在 DirectX 中绘制精灵的最性能友好的方法是什么?谢谢!
I'm trying to create a platformer game, and I am taking various sprite blocks, and piecing them together in order to draw the level. This requires drawing a large number of sprites on the screen every single frame. A good computer has no problem handling drawing all the sprites, but it starts to impact performance on older computers. Since this is NOT a big game, I want it to be able to run on almost any computer. Right now, I am using the following DirectX function to draw my sprites:
D3DXVECTOR3 center(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 position(static_cast<float>(x), static_cast<float>(y), z);
(my LPD3DXSPRITE object)->Draw((sprite texture pointer), NULL, ¢er, &position, D3DCOLOR_ARGB(a, r, g, b));
Is there a more efficient way to draw these pictures on the screen? Is there a way that I can use less complex picture files (I'm using regular png's right now) to speed things up?
To sum it up: What is the most performance friendly way to draw sprites in DirectX? thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用的 ID3DXSPRITE 接口已经非常高效。如果可能的话,请确保所有精灵绘制调用在精灵开始和结束调用之间发生在一批中。这允许精灵界面以最有效的方式安排绘制。
为了获得额外的性能,您可以将多个较小的纹理加载到一个较大的纹理中,并使用纹理坐标将它们取出。这使得纹理不必频繁交换。请参阅:
http://nexe.gamedev.net/directknowledge/default.asp?p =ID3DXSprite
您用于纹理的文件类型并不重要,只要它们预加载到纹理中即可。确保在加载游戏/关卡时将它们全部加载到纹理中。一旦将它们加载到纹理中,它们最初的格式就不再重要了。
如果您仍然没有获得所需的性能,请尝试使用 PIX 来分析您的应用程序并找出真正的瓶颈所在。
编辑:
这太长了,无法容纳评论,所以我将编辑这篇文章。
当我说交换纹理时,我的意思是使用 SetTexture 将它们绑定到纹理阶段。每次调用 SetTexture 时,都会对性能造成一些影响,因为它会更改纹理阶段的状态。通常,这种延迟相当小,但如果 DirectX 必须将纹理从系统内存拉到视频内存,则可能会很糟糕。
ID3DXsprite 将为您重新排序开始和结束调用之间的绘制。这意味着 SetTexture 通常只会为每个纹理调用一次,无论您绘制它们的顺序如何。
通常值得将小纹理加载到大纹理中。例如,如果可以将所有小纹理放入一个大纹理中,那么纹理阶段可以在所有绘制中保持绑定到该纹理。通常这会带来显着的改进,但测试是确定它有多大帮助的唯一方法。它看起来很糟糕,但你可以放入任何大的纹理并假装它是组合的纹理来测试会有什么性能差异。
The ID3DXSPRITE interface you are using is already pretty efficient. Make sure all your sprite draw calls happen in one batch if possible between the sprite begin and end calls. This allows the sprite interface to arrange the draws in the most efficient way.
For extra performance you can load multiple smaller textures in to one larger texture and use texture coordinates to get them out. This makes it so textures don't have to be swapped as frequently. See:
http://nexe.gamedev.net/directknowledge/default.asp?p=ID3DXSprite
The file type you are using for the textures does not matter as long as they are are preloaded into textures. Make sure you load them all in to textures once when the game/level is loading. Once you have loaded them in to textures it does not matter what format they were originally in.
If you still are not getting the performance you want, try using PIX to profile your application and find where the bottlenecks really are.
Edit:
This is too long to fit in a comment, so I will edit this post.
When I say swapping textures I mean binding them to a texture stage with SetTexture. Each time SetTexture is called there is a small performance hit as it changes the state of the texture stage. Normally this delay is fairly small, but can be bad if DirectX has to pull the texture from system memory to video memory.
ID3DXsprite will reorder the draws that are between begin and end calls for you. This means SetTexture will typically only be called once for each texture regardless of the order you draw them in.
It is often worth loading small textures into a large one. For example if it were possible to fit all small textures in to one large one, then the texture stage could just stay bound to that texture for all draws. Normally this will give a noticeable improvement, but testing is the only way to know for sure how much it will help. It would look terrible, but you could just throw in any large texture and pretend it is the combined one to test what performance difference there would be.
我同意 dschaeffer 的观点,但想补充一点,如果您使用大量不同的纹理,最好将它们混合在单个(或几个)较大的纹理上,并相应地调整不同精灵的纹理坐标。纹理状态更改的成本很高,这可能会加快旧系统上的速度。
I agree with dschaeffer, but would like to add that if you are using a large number different textures, it may better to smush them together on a single (or few) larger textures and adjust the texture coordinates for different sprites accordingly. Texturing state changes cost a lot and this may speed things up on older systems.