如何防止在屏幕外绘制 XNA 组件? [英] How to prevent drawing XNA component when it is off-screen?

查看:53
本文介绍了如何防止在屏幕外绘制 XNA 组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 XNA 中制作 2d 游戏.使用可绘制的游戏组件时,哪个性能更好?

I'm making a 2d game in XNA. When using drawable game components which one is better for performance?

1.当组件不在屏幕上时,将其从组件列表中删除,并在屏幕上显示时添加.

1.When a component is not onscreen remove it from the components list and when its onscreen add it.

2.当它的屏幕外不运行它的绘制功能时(通过使用唤醒"布尔字段和一个if 语句围绕 draw 函数中的所有内容)

2.When its offscreen dont run its draw function (by using an "awake" bool field and an if statement around everything in the draw function)

我目前正在使用方法 2,它工作正常.它很方便因为组件的绘制顺序不会改变(如果我删除并重新添加它们,它会,我需要更多的代码来管理它)事实上,我突然想到,如果它不在组件列表中,它就不会更新,我需要其他东西来跟踪它是否出现在屏幕上..

I'm using method 2 at the moment and it works fine. Its handy cus the draworder of components wont change (if I remove and re-add them it will, and I'll need more code to manage that) In fact it just occured to me that if its not in the components list it wont update and I'll need something else to keep track of whether its onscreen or not..

此外,我没有带有分析器的精美版本的视觉工作室,所以我只是在此询问人们对他们的体验有何看法

Also I dont have the fancy version of visual studio with the profiler so I'm just asking here what do people think from their experience

推荐答案

使用 SpriteBatch 时 CPU 性能的一个有时被忽视的概念是您如何对 sprite 进行批处理.并且使用可绘制的游戏组件并不容易有效地批量处理精灵.

A sometimes overlooked concept of CPU performance while using SpriteBatch is how you go about batching your sprites. And using drawable game component doesn't lend itself easily to efficiently batching sprites.

基本上,绘制精灵的 GPU 并不慢 &CPU 将所有精灵组织成一个批次的速度并不慢.较慢的部分是 CPU 必须与 GPU 通信它需要绘制什么(向它发送批处理信息).当您调用 spriteBatch.Draw() 时,CPU 到 GPU 的通信不会发生.当您调用 spriteBatch.End() 时会发生这种情况.因此,效率低下的目标是减少调用 spriteBatch.End() 的频率.(当然,这也意味着调用 Begin() 的频率也会降低).此外,请谨慎使用 spriteSortMode.Imediate,因为它会立即导致 CPU 将每个精灵信息发送到 GPU(慢).

Basically, The GPU drawing the sprites is not slow & the CPU organizing all the sprites into a batch is not slow. The slow part is when the CPU has to communicate to the GPU what it needs to draw (sending it the batch info). This CPU to GPU communication does not happen when you call spriteBatch.Draw(). It happens when you call spriteBatch.End(). So the low hanging fruit to efficiency is calling spriteBatch.End() less often. (of course this means calling Begin() less often too). Also, use spriteSortMode.Imediate very sparingly because it immediately causes the CPU to send each sprites info to the GPU (slow)).

所以如果你调用 Begin() &End() 在每个游戏组件类中,并且有很多组件,您会不必要地花费大量时间,而且与担心屏幕外精灵相比,您可能会节省更多时间来制定更好的批处理方案.

So if you call Begin() & End() in each game component class, and have many components, you are costing yourself a lot of time unnecessarily and you will probably save more time coming up with a better batching scheme than worrying about offscreen sprites.

旁白:无论如何,GPU 都会自动忽略来自其像素着色器的屏幕外精灵.所以在 CPU 上剔除屏幕外精灵不会节省 GPU 时间......

Aside: The GPU automatically ignores offscreen sprites from its pixel shader anyway. So culling offscreen sprites on the CPU won't save GPU time...

这篇关于如何防止在屏幕外绘制 XNA 组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆