SpriteBatch.End() throwing an invalid ArguementException

While trying to end a batch, I’m getting an ArgumentException from SpriteBatch.End. Here is my Draw method anyways

protected override void Draw(GameTime gameTime)
    {         
        renderTarget = new RenderTarget2D(GraphicsDevice, Window.ClientBounds.Width, Window.ClientBounds.Height);
        GraphicsDevice.SetRenderTarget(renderTarget);
        GraphicsDevice.Clear(Color.Black);
        Window.ClientBounds.Height / 2);
        renderer.LoadLights();            
        renderer.DrawAll();                    
        GraphicsDevice.SetRenderTarget(null);
        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.Additive, null, null, null, renderer.lighting);
        spriteBatch.Draw(renderTarget, Vector2.Zero, Color.White);            
        spriteBatch.End(); // Error being thrown here
        renderTarget.Dispose();
        base.Draw(gameTime);
    }

This almost definitely isn’t enough information but I don’t know what is relevant and what is, I can provide more if needed.

Don’t create and dispose of the render target in your draw loop

Create 1 when loading your game/level and dispose of it once your done with it.

Alright I will do that. I’m still getting the same error though, but thanks.

Step through your code.

You must have a spritebatch.begin call under renderer.drawall. Find where it is thats causing the error

Edit: sorry just realised you noted where the error is

Try remove your lighting effect and see if it still does it. It is probably your effect.
Spritebatch.end is the moment when all the spritebatches are actually drawn.

Perhaps your effect needs some parameters passed to it?

If I remove the effect and just do SpriteBatch.Begin with no arguments then it works. I think the error might be in LoadLights, if it has to do with parameters. Do you see anything wrong with this code?

public void LoadLights()
    {
        List<Vector3> lightData = new List<Vector3>();
        foreach(Light light in world.loadedChunk.lights)
        {
            Vector2 screenCoords = ToScreenSpace(light.position);
            lightData.Add(new Vector3(screenCoords.X, screenCoords.Y, light.intensity));                
        }

        lighting.Parameters["lights"].SetValue(lightData.ToArray());
    }

I have lights set as an array with 1000 ‘slots’ in the effect incase more lights are needed, but only load one, might that be the issue? I also set abreakpoint and verified that world.loadedChunk.lights does contain the light so it shouldn’t be trying to load nothing.

Can you show the code of your lighting effect?

Yep,

texture ScreenTexture;

float3 lights[1000];
float2 windowSize;
sampler TextureSampler = sampler_state
{
Texture = ;
};
float2 toWindowCoords(float2 texCoord){

return float2(texCoord.x*windowSize.x, texCoord.y*windowSize.y);

}
float4 PixelShaderFunction(float2 TexCoord : TEXCOORD0) : COLOR0
{
float a = 0;
float dist = distance(toWindowCoords(TexCoord), lights[0].xy);
a += lights[0].z/(dist*dist);
float4 color = tex2D(TextureSampler, TexCoord);
color.a = a;
return color;
}
technique Plain
{
pass Pass1
{
AlphaBlendEnable = TRUE;
DestBlend = INVSRCALPHA;
SrcBlend = SRCALPHA;
PixelShader = compile ps_3_0 PixelShaderFunction();
}
}

The formatting has decided that it doesn’t like me, sorry for the wonky layout. I’m not used to this really.

Update: Here’s the stack trace, if that helps
" at System.Buffer.BlockCopy(Array src, Int32 srcOffset, Array dst, Int32 dstOffset, Int32 count)\r\n at Microsoft.Xna.Framework.Graphics.ConstantBuffer.SetParameter(Int32 offset, EffectParameter param)\r\n at Microsoft.Xna.Framework.Graphics.ConstantBuffer.SetParameter(Int32 offset, EffectParameter param)\r\n at Microsoft.Xna.Framework.Graphics.ConstantBuffer.Update(EffectParameterCollection parameters)\r\n at Microsoft.Xna.Framework.Graphics.EffectPass.Apply()\r\n at Microsoft.Xna.Framework.Graphics.SpriteBatcher.FlushVertexArray(Int32 start, Int32 end, Effect effect, Texture texture)\r\n at Microsoft.Xna.Framework.Graphics.SpriteBatcher.DrawBatch(SpriteSortMode sortMode, Effect effect)\r\n at PlatformGame.Render.Renderer.DrawAll() in C:\Code\CSharp\PlatformGame\Render\Renderer.cs:line 85\r\n at PlatformGame.App.Draw(GameTime gameTime) in C:\Code\CSharp\PlatformGame\App.cs:line 98\r\n at Microsoft.Xna.Framework.Game.DoDraw(GameTime gameTime)\r\n at Microsoft.Xna.Framework.Game.Tick()\r\n at Microsoft.Xna.Framework.SdlGamePlatform.RunLoop()\r\n at Microsoft.Xna.Framework.Game.Run(GameRunBehavior runBehavior)\r\n at PlatformGame.Program.Main() in C:\Code\CSharp\PlatformGame\Program.cs:line 11"
Not sure what to make of it really, but I’m still stuck on this.

I got it to work. I’m not exactly sure how, but my theory is that because I was only using the first element of the lights array, the shader got mad at me for loading useless data and broke. Adding a loop to iterate over the lights made it work, even though most of them are blank.

Good pickup. Im not very good with shaders but it seems a highly inefficient way of doing it.

Yeah it doesn’t seem very efficient at all but it doesn’t let me use variable sized arrays so I don’t really see another option. At least not a simple one.

The problem is the passed array is not the correct size. It only contains the actual number of lights not 1000.

Change your LoadLights method to use an array of the proper size:

public void LoadLights()
{
    Vector3[] lightData = new Vector3[1000];
    int Count = 0;
    foreach(Light light in world.loadedChunk.lights)
    {
        Vector2 screenCoords = ToScreenSpace(light.position);
        lightData[Count++] = new Vector3(screenCoords.X, screenCoords.Y, light.intensity);
    }

    lighting.Parameters["lights"].SetValue(lightData);
}

`