XNA and RenderTargets

依然范特西╮ 提交于 2019-12-12 01:35:27

问题


I have a problem with using render targets that I am hoping someone can explain to me. I am trying to use a render target to render some sprites, and then draw said render target onto the normal back buffer 50% opaque (null). It works great, except if I use the target more than once in a draw call, in which case only the last group of sprites will appear on the screen.

Here is some psuedo code.

// Normal Sprite Drawing.  
DrawSomeSprites();

SetRenderTarget(CompositeTarget);

// These go on the render target.  
DrawSomeMoreSprites();

SetRenderTarget(null);

// And now I draw onto the back buffer.  
DrawSprite(CompositeTarget);


// So now I want to draw another bunch of sprites via my render target using the same approach.
SetRenderTarget(CompositeTarget);

// These are the only sprites that I can see on the screen.
DrawSomeMoreSprites();             
SetRenderTarget(null);
DrawSprite(CompositeTarget);

I hope that makes sense. It seems to me that every time I change the render target, the previous render target (null) is cleared for some reason. I am ending all of my sprite batches before swapping the targets, and I am not getting any errors, so I don't know what to do here. Obviously the goal is to be able to see all of my sprite groups on the screen at the same time. What am I missing here ??


回答1:


This article explains why your render targets are being cleared.

Basically it's a quirk of the way the XBox graphics system works. There's only one region of memory the XBox GPU can render into, and it's quite small. When you change render targets, that memory gets clobbered. In XNA 2.0 (as the article explains), the default behaviour of Windows was changed to match the Xbox 360 behaviour to keep things consistent and fast across platforms.

The parameter to select which behaviour you want is RenderTargetUsage. Just be aware that preserving contents on the XBox is slow.

Alternately you may be able to change the order you are doing your rendering so you don't run into this problem.



来源:https://stackoverflow.com/questions/4953191/xna-and-rendertargets

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!