Sign in to follow this  
PhillipHamlyn

Creating Textures on seperate RenderTarget2D "Bleeds Through"

Recommended Posts

Hi,

Can anyone help please ? I am creating mipmapped textures from PNG by creating a Texture2D and rendering it to a new RenderTarget2D like so;
[source lang="csharp"]Texture2D rawTexture = Texture2D.FromStream(device, memoryStream);
RenderTarget2D renderTarget = new RenderTarget2D(device, rawImage.Width, rawImage.Height, mipMap: true, preferredFormat: SurfaceFormat.Color, preferredDepthFormat: DepthFormat.None);

device.SetRenderTarget(renderTarget);
using (SpriteBatch sprite = new SpriteBatch(device))
{
sprite.Begin(SpriteSortMode.Immediate, BlendState.Opaque, SamplerState.AnisotropicClamp, DepthStencilState.None, RasterizerState.CullNone, effect: null);
sprite.Draw(rawTexture, new Vector2(0, 0), Color.White);
sprite.End();
}

Texture2D mipmappedTexture = (Texture2D)renderTarget;
device.SetRenderTarget(null);


return mipmappedTexture;[/source]
My problem is that this is almost always unnoticeable, but when I have a lot of this to do, during game load, this process interferes with the visible surface - the visible surface is corrupted and is frequently cleared back to turquiose (the default back color for a new RenderTarget2D). This is all occuring on the same thread (earlier attempts to multithread didn't syncronise SamplerStates well and I abandoned them).

I thought that by using a new RenderTarget there would be no possible interference with the users' visible RenderTarget, but I seem not to be understanding the relationship between the two. A nudge in the right direction would be very helpful.

Thanks

Share this post


Link to post
Share on other sites
What is going on in that memory stream?

Also, I'm not sure what you mean when you say "the visible surface". If you mean the surface your presenting to the end user (front buffer), then I'd check out what is happeneing in your BeginScene, EndScene and PresentScene. I see you set the render target to null for the device in your code, do you ever set it back to the back buffer?

The only way you can have the sprite bleed through to the backbuffer is:[list]
[*]the device is not cleared
[*]render target is presented in presentscene
[*]you render the sprite just like a mesh in your current loop, using blending or alpha params.
[/list]

The device is only going to present what you tell it to and where you tell it.

Share this post


Link to post
Share on other sites
DJTN,

Thanks for the reply. The memorystream is a byte[] buffer from a PNG resource which I'm loading through a SpriteBatch to generate a mipmap.

I thought that setting RenderTarget to null always selects the backbuffer for the next instruction, so it didn't need explicitly setting. However, I'll look up how to do that and make sure I am definitely explicitly setting the backbuffer in my main render loop.

Thanks.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this