将渲染和物理分离为游戏引擎的独立线程是否有效? [英] Is that effective to split rendering and physics to separate threads for a game engine ?

查看:104
本文介绍了将渲染和物理分离为游戏引擎的独立线程是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在开发一些3D游戏的实验性游戏引擎(通过XNA),我正在考虑将物理和渲染过程分成C#中的单独线程。



所以,我在StackOverflow的一篇文章中读到多线程并不总是有效甚至可能损害整体性能。



我认为它没关系,因为渲染对物理或游戏逻辑同步没什么关键,这可能导致程序在运行时崩溃。所以我想把物理学留在主线程中并将渲染过程交给另一个线程,然后它会影响物理和AI的计算。



我想这个想法很棒,因为无论游戏中的逻辑如何,渲染都可能会变慢,因此无论渲染是否延迟,都应计算下一个逻辑步骤。总体而言,渲染过程与其他所有逻辑过程一样繁重。



问题:

1)所以,它是好的想要在这里执行多线程吗?

2)如果渲染比逻辑计算慢,是否会导致鼠标滞后?

3)拆分是个好主意许多线程的游戏逻辑?像AI,Physics,Sound等?

Hello , I am developing now some experimental game engine for 3D games (via XNA) and i was thinking of splitting the physics and rendering processes to separate threads in C# .

So , i was reading in some article at StackOverflow that Multi-Threading isn''t always effective and may even hurt the overall performance .

I thought it would be OK because rendering have nothing critical to sync with the physics or game logic which may cause the program to crash at running time . So i wanted to leave the physics in the main thread and give the rendering process to another thread and then it will affect less the physics and the AI calculations .

I think this idea is great at all , because the Rendering may be slower regardless the logic in the game so the next logical step should be calculated whatever if the rendering is delays or not . And in overall the rendering is so heavy process as all the other logical processes together .

Questions :
1) So , is it good idea to perform here multithreading at all ?
2) If the rendering is slower than the logic calculation , Will it cause mouse lags ?
3) Is it good idea to split the game logic to many threads ? like AI , Physics , Sound etc ?

推荐答案

这实际上取决于你的UI库以及你所谓的渲染。例如,在WPF中,渲染已经是与UI线程分离的线程。我不认为这是一个正确提出的问题。在所有情况下,您应该将游戏场景与UI分开。然后你只需要以数据的形式呈现场景,这几乎不可能是与场景线程不同的线程。但UI渲染(GDI,DirectX,无论如何)几乎是不可避免的,来自不同的线程,但它是什么,UI线程或另一个线程,取决于技术。







首先,论坛的所有格式都不允许认真考虑这个问题,特别是当你试图做一些概括时。我会说,这里的概括特别危险。



考虑一下你正在打网球。有一个代表球的线程是件好事。 UI线程将为您提供可用于更改由于与球拍碰撞而导致的运动的事件。当你介绍对着电脑玩时,有一个代表你的对手玩家的线程很好。同时,如果这是足球,也许你需要一个代表整个球队的线程。



没有什么比你想象的那么简单。但是,我会尝试列出一些你不应该与一个线程混淆的架构概念。



  • 线程不应该与一个层耦合多层架构(实际上所有架构都应该是多层的)。线程可以执行多个层的代码;并且一个层可以托管多个线程。
  • 线程不应该与功能单元耦合。一个线程可以执行几个功能单元的代码;功能单元可以托管多个线程。特别是,线程不应与某些算法相关联或耦合。
  • 线程不应与编译单元耦合,例如.NET程序集。与上面相同。
It really depends on your UI library and what you call rendering. In WPF, for example, rendering is already a thread separate from UI thread. I don''t think this is a correctly posed question. In all cases, you should separate the game scenario from UI. Then you just have to render the scene in the form of data, and this hardly can be a thread different from "scenario" thread. But the UI rendering (GDI, DirectX, whatever) is nearly inevitable comes in a different thread, but what is it, UI thread or yet another thread, depends on the technology.



First of, all the format of the forum does not allow to consider this problem seriously, especially when you try to do some generalizations. I would say, generalization is especially dangerous here.

Consider you are playing tennis with a wall. It''s good to have a thread representing the ball. The UI thread will give you the events which you can use to change the motion due to collisions with a racket. When you introduce "play against a computer", its good to have a thread representing your opponent player. At the same time, if this is football, perhaps you will need a thread representing a whole team of players.

Nothing is so straightforward as you probably trying to imagine. However, I would try to list some architectural notions you should not mix up with a thread.

  • Thread should not be coupled with a layer in a multilayer architectures (and practically all architectures should be multilayer). A thread can execute the code of several layers; and a layer can host several threads.
  • Thread should not be coupled with a functional unit. A thread can execute the code of several functional units; and a functional unit can host several threads. In particular, a thread should not be associated or couples with some algorithm.
  • Thread should not be couples with a compilation unit, such as a .NET assembly. Same as above.


这篇关于将渲染和物理分离为游戏引擎的独立线程是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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