I am trying to send a Vector2 array to a shader but get keep getting the following error when calling SpriteBatch.End:
- System.ArgumentException: 'Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection’
The code where i am sending the data (edited down a bit for clarity):
Vector2[] normals = new Vector2[1024];
// createNormals is only called once
void createNormals()
{
for (var i = 0; i < normals.Length; i++)
{
normals[i] = new Vector2(0, 0);
}
addNormal(14, new Vector2(0, 1));
addNormal(12, new Vector2(0.5f, 1));
addNormal(78, new Vector2(0.5f, 1));
addNormal(6, new Vector2(-0.5f, 1));
addNormal(24, new Vector2(-1f, 1));
addNormal(94, new Vector2(0f, 1));
addNormal(4, new Vector2(0f, 1));
addNormal(76, new Vector2(0f, 1));
addNormal(30, new Vector2(-0.5f, 1));
effect.Parameters["normals"].SetValue(normals);
}
public void addNormal(int index, Vector2 normal)
{
normal.Normalize();
normals[index] = normal;
}
The shader code:
float2 normals[1024];
// the return value is used elsewhere so normals is not optimized away
float2 getNormal(int i) {
return normals[i];
}
stackTrace:
at System.Buffer.BlockCopy(Array src, Int32 srcOffset, Array dst, Int32 dstOffset, Int32 count)
at Microsoft.Xna.Framework.Graphics.ConstantBuffer.SetParameter(Int32 offset, EffectParameter param)
at Microsoft.Xna.Framework.Graphics.ConstantBuffer.SetParameter(Int32 offset, EffectParameter param)
at Microsoft.Xna.Framework.Graphics.ConstantBuffer.Update(EffectParameterCollection parameters)
at Microsoft.Xna.Framework.Graphics.EffectPass.Apply()
at Microsoft.Xna.Framework.Graphics.SpriteBatcher.FlushVertexArray(Int32 start, Int32 end, Effect effect, Texture texture)
at Microsoft.Xna.Framework.Graphics.SpriteBatcher.DrawBatch(SpriteSortMode sortMode, Effect effect)
at MyGame.Game1.Draw(GameTime gameTime) in C:\dev\myGame\myGame\MyGame\Game1.cs:line 181
at Microsoft.Xna.Framework.Game.DoDraw(GameTime gameTime)
at Microsoft.Xna.Framework.Game.Tick()
at Microsoft.Xna.Framework.SdlGamePlatform.RunLoop()
at Microsoft.Xna.Framework.Game.Run(GameRunBehavior runBehavior)
at Program.$(String[] args) in C:\dev\myGame\myGame\MyGame\Program.cs:line 3
I am new to writing shaders so maybe i am doing something very stupid and simply not noticing.