Jump to content
  • Advertisement
Sign in to follow this  
Nokame

Problems with Blending Images XNA 3.1

This topic is 2442 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there,
I've been working on a 2D tile-based game. The whole thing is created in C#, XNA 3.1 and uses spritebatch to draw everything. Lately I've been trying to add a bit of depth to the game with some basic lighting technique that blends images over the current map. This seemed to work nicely at first, but then I realized that after the game has been running for a few minutes the FPS drops by half or more, and begins to run sluggishly. Also note, the only time the FPS drops is when the lighting is on, and when i turn the lighting off... the FPS jumps right back up to 60.

I've attatched a screenshot that shows what the lighting looks like, and what it looks like without. I'm at a loss. I can't figure out why it begins to run sluggishly... Any help/suggestions would be appreciated.


DrawToRenderTarget...()
{
this.GraphicsDevice.SetRenderTarget(0, this.region.lightMap);

//clear to some small ambient light
this.GraphicsDevice.Clear(this.region.ambience);

//where Alpha is 0, nothing will be written
this.spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);

this.spriteBatch.GraphicsDevice.RenderState.DestinationBlend = Blend.One;
this.spriteBatch.GraphicsDevice.RenderState.SourceBlend = Blend.DestinationAlpha;
this.spriteBatch.GraphicsDevice.RenderState.BlendFunction = BlendFunction.Add;
this.spriteBatch.GraphicsDevice.RenderState.SeparateAlphaBlendEnabled = true;

foreach (LightSource light in this.region.regionLightSourceInstances)
{
light.Draw(this.spriteBatch);
}
this.spriteBatch.End();
this.GraphicsDevice.SetRenderTarget(0, null);
}


Thank you.

Share this post


Link to post
Share on other sites
Advertisement
I don’t fully understand what you are doing. Particularly the line: light.Draw(this.spriteBatch);

However, the SpriteSortMode.Immediate could be changed to reduce draw calls, and the saveStateMode.SaveState could be avoided at the expense of some extra lines of code (http://blogs.msdn.com/b/shawnhar/archive/2006/11/13/spritebatch-and-renderstates.aspx).

Good luck!!!

Share this post


Link to post
Share on other sites
First of all, thank you for the response. I'll look into those suggestions. Sorry if the post was a bit vague. The "[color="#660066"]DrawToRenderTarget()" method shown above renders a 'lightmap' to a RenderTarget2D object. It clears the surface to the "ambience" color, then all the lights, which are just images, are drawn onto the RenderTarget.

So the line: light.Draw(this.spriteBatch);

just means...

[size="2"]spriteBatch.Draw(lightTexture, position, [size="2"][color="#0000ff"][size="2"][color="#0000ff"]null[size="2"], color, 0, center, scale, [size="2"][color="#2b91af"][size="2"][color="#2b91af"]SpriteEffects[size="2"].None, 1);

Once the RenderTarget2D object is "filled" i use the following code to Draw it to the screen, after everything else has been drawn (that I want "light" to fall on):


// Draw lightmap over everything
this.spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);

this.spriteBatch.GraphicsDevice.RenderState.SourceBlend = this.region.sourceBlendType;
this.spriteBatch.GraphicsDevice.RenderState.DestinationBlend = this.region.destBlendType;
this.spriteBatch.GraphicsDevice.RenderState.BlendFunction = this.region.blendFunction;

this.spriteBatch.Draw(this.region.lightMap.GetTexture(), Vector2.Zero, Color.White);

this.spriteBatch.End();


Note: this.region.lightMap is the RenderTarget2D object. I suppose it's also worth mentioning that I've messed around with the RenderStates, BlendModes, SaveStateModes and the lighting effect is either skewed too much to be useful, or still causes the game to run sluggishly.

I hope this made the question more clear.

Share this post


Link to post
Share on other sites
I don’t see anything that could provoke your problem. Are you sure that the problem is there?

You said: “the only time the FPS drops is when the lighting is on”. Do you call/do anything else?

The GPU memory is not managed, that means you can potentially have a leak. But I don’t see it in your code. Do you create some asset (shader, texture, render target, etc.) every frame?

Share this post


Link to post
Share on other sites
Hidden
As it is... i create a screensize lightmap (RenderTarget) every frame. This is wrong, I take it?

Share this post


Link to post
I create that lightmap (RenderTarget2D) every frame because the background scrolls and it moves the lights onto the screen. Should I find a way to create a large lightmap once and then pull portions from it every frame?

Share this post


Link to post
Share on other sites
That's the problem!!


The render targets are stored in the GPU memory in an unmanaged way. If the dispose method is not called then a memory portion will be reserved for this render target. Moreover creating a render target each frame even if you destroy the old one is a bad practice. You have to reuse it.

Share this post


Link to post
Share on other sites
Well, I messed up. I actually do just reuse the RenderTarget2Ds by drawing on them every frame, i dont reinitialize them every frame, just redraw. Sorry about that.

Share this post


Link to post
Share on other sites
:) you're being very helpful, let me say that i appreciate this.

At initialization spritebatch is loaded.

[size="2"][color="#0000ff"][size="2"][color="#0000ff"]this[size="2"].spriteBatch = [size="2"][color="#0000ff"][size="2"][color="#0000ff"]new [size="2"][color="#2b91af"][size="2"][color="#2b91af"]SpriteBatch[size="2"]([size="2"][color="#0000ff"][size="2"][color="#0000ff"]this[size="2"].GraphicsDevice);

and during initialization the following is called when a map is loaded.


PresentationParameters pp = device.PresentationParameters;

// This RenderTarget2D object holds the map (or region) and all sprites.
region.regionSurface = new RenderTarget2D(device, pp.BackBufferWidth, pp.BackBufferHeight, 1, SurfaceFormat.Color, pp.MultiSampleType, pp.MultiSampleQuality);

// This RenderTarget2D object holdes the "lightmap".
region.lightMap = new RenderTarget2D(device, pp.BackBufferWidth, pp.BackBufferHeight, 1, SurfaceFormat.Color, pp.MultiSampleType, pp.MultiSampleQuality);


then for the textures of all the lighting is loaded (also at initialization) is as follows:


// Get the light texture images
int numberOfTextureObjects = Convert.ToInt32(temp[count]); count++;
for (int i = 0; i < numberOfTextureObjects; i++)
{
// Create a list of textures and filenames
region.regionLightSourceTextures.Add(Content.Load<Texture2D>("Lighting\\" + temp[count].Trim()));
region.regionLightSourceTextureFileName.Add(temp[count].Trim()); count++;
}

// Get the number of LightSource instances on the map
int numberOfLightInstances = Convert.ToInt32(temp[count]); count++;
for (int i = 0; i < numberOfLightInstances; i++)
{
// Create a list of LightSources.
//

// Get the index of our texture list for the image to be used
int imageIndex = Convert.ToInt32(temp[count]); count++;
region.regionLightSourceInstanceTextureIndex.Add(imageIndex);

// Get the color of the light
Color color = Region.GetColor(temp[count]); count++;

// Get the size of the light
float range = (float)Convert.ToDouble(temp[count]); count++;

// Get the location of the light on the map.
int x = Convert.ToInt32(temp[count]); count++;
int y = Convert.ToInt32(temp[count]); count++;
Vector2 location = new Vector2(x, y);

// Distance
if (temp[count].ToUpper().Trim().Equals("DISTANCE"))
{
count++;

// Get the distance
float distance = (float)Convert.ToDouble(temp[count]); count++;

// Add the light source
region.regionLightSourceInstances.Add(new LightSource(region.regionLightSourceTextures[imageIndex], color, range, location, distance));
}
else
{
region.regionLightSourceInstances.Add(new LightSource(region.regionLightSourceTextures[imageIndex], color, range, location));
}
}


the code above is part of a method that loads a map from a text file. As far as the lighting goes, it copies a Texture2D object from a list of possible textures to a new lighting instance, which is then added to a map's (or region's) list of light sources. The lighting textures aren't instanced, rather they are copied. Each lightsource has it's own texture2d object. I would think the non-instancing part as troubling, but some of the maps only have a few lights going, and still have the eventual reduction in FPS. These are all the assets i can think of. There are primitives on the map, but those work fine while the lighting is turned off.

again, thanks for the back and forth.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!