I am writing a class to render a 2D bitmap to the screen. Every time the position of the bitmap is changed, the positions of the vertices which make up the quad for the image need to be updated. Here is that function:
/// <summary>
/// Modify vertices when the image is moved around the screen
/// </summary>
/// <param name="newPos"></param>
private void updateVBuffer(Point newPos)
{
Vector3[] positions =
{
transformToD3DCoords(newPos),
transformToD3DCoords(new Point(newPos.X + this.Width, newPos.Y)),
transformToD3DCoords(new Point(newPos.X + this.Width, newPos.Y + this.Height)),
transformToD3DCoords(new Point(newPos.X, newPos.Y + this.Height))
};
//create clockwise winding order
Vertex[] vertices =
{
new Vertex() {Normal = Vector3.Zero, Position = positions[0]},
new Vertex() {Normal = Vector3.Zero, Position = positions[1]},
new Vertex() {Normal = Vector3.Zero, Position = positions[2]},
new Vertex() {Normal = Vector3.Zero, Position = positions[3]},
};
DataBox dataBox = this.pipeline.Context.MapSubresource(this.vBuffer, MapMode.Write, MapFlags.None);
dataBox.Data.Position = 0;
dataBox.Data.WriteRange<Vertex>(vertices);
dataBox.Data.Position = 0;
this.pipeline.Context.UnmapSubresource(this.vBuffer, 0);
//dataBox.Data.Dispose();
}
In addition, here is the code where I create the index and vertex buffers:
public Bitmap(Rectangle screenBounds, D3DPipeline pipeline, Texture2D texture)
{
this.pipeline = pipeline;
this.Texture = texture;
this.Position = Point.Empty;
this.screenBounds = screenBounds;
BufferDescription vBufferDescr = new BufferDescription()
{
BindFlags = BindFlags.VertexBuffer,
CpuAccessFlags = CpuAccessFlags.Write,
OptionFlags = ResourceOptionFlags.None,
SizeInBytes = Marshal.SizeOf(typeof(Vertex)) * 4,
StructureByteStride = Marshal.SizeOf(typeof(Vertex)),
Usage = ResourceUsage.Dynamic
};
vBuffer = new SlimDX.Direct3D11.Buffer(this.pipeline.Device, vBufferDescr);
this.vBufferBinding = new VertexBufferBinding(this.vBuffer, Marshal.SizeOf(typeof(Vertex)), 0);
BufferDescription iBufferDescr = new BufferDescription()
{
BindFlags = BindFlags.IndexBuffer,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None,
SizeInBytes = Marshal.SizeOf(typeof(Int16)) * 6,
StructureByteStride = Marshal.SizeOf(typeof(Int16)),
Usage = ResourceUsage.Default
};
iBuffer = new SlimDX.Direct3D11.Buffer(this.pipeline.Device, iBufferDescr);
this.textureRsrcView = new ShaderResourceView(pipeline.Device, this.Texture);
this.updateVBuffer(new Point(0, 0));
}
The exception (E_INVALIDARG) gets thrown on the line where I call context.MapSubresource() in updateVBuffer().
Any ideas on what I'm doing wrong?