From c058ce59113a13e8ba0dd4eddeda8d041057eace Mon Sep 17 00:00:00 2001 From: Stephan Sundermann Date: Sat, 17 Mar 2012 11:06:30 +0100 Subject: [PATCH] Small performance increase --- Vocaluxe/Lib/Draw/CDirect3D.cs | 41 ++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/Vocaluxe/Lib/Draw/CDirect3D.cs b/Vocaluxe/Lib/Draw/CDirect3D.cs index b7dd0d2b..3b4d828d 100644 --- a/Vocaluxe/Lib/Draw/CDirect3D.cs +++ b/Vocaluxe/Lib/Draw/CDirect3D.cs @@ -50,9 +50,9 @@ namespace Vocaluxe.Lib.Draw private STexture blankTexture; - private List _Vertices; - private List _VerticesTextures; - private List _VerticesRotationMatrices; + private Queue _Vertices; + private Queue _VerticesTextures; + private Queue _VerticesRotationMatrices; #endregion private vars @@ -66,9 +66,9 @@ namespace Vocaluxe.Lib.Draw _D3DTextures = new List(); _Queque = new List(); - _Vertices = new List(); - _VerticesTextures = new List(); - _VerticesRotationMatrices = new List(); + _Vertices = new Queue(); + _VerticesTextures = new Queue(); + _VerticesRotationMatrices = new Queue(); _Keys = new CKeys(); _D3D = new Direct3D(); @@ -547,19 +547,33 @@ namespace Vocaluxe.Lib.Draw return new RectangleF(text.X, text.Y, CFonts.GetTextWidth(CLanguage.Translate(text.Text)), CFonts.GetTextHeight(CLanguage.Translate(text.Text))); } + /// + /// Adds a quad a list which will be added and rendered to the vertexbuffer when calling RenderToVertexBuffer to reduce vertexbuffer calls each frame to a minimum + /// + /// A TexturedColoredVertex array containg 4 vertices + /// The texture the vertex should be textured with + /// The vertices' rotation private void AddToVertexBuffer(TexturedColoredVertex[] vertices, Texture tex, Matrix rotation) { + //The vertexbuffer is full, so we need to flush it before we can continue if (_Vertices.Count >= CSettings.iVertexBufferElements) RenderVertexBuffer(); - _Vertices.AddRange(vertices); - _VerticesTextures.Add(tex); - _VerticesRotationMatrices.Add(rotation); + _Vertices.Enqueue(vertices[0]); + _Vertices.Enqueue(vertices[1]); + _Vertices.Enqueue(vertices[2]); + _Vertices.Enqueue(vertices[3]); + _VerticesTextures.Enqueue(tex); + _VerticesRotationMatrices.Enqueue(rotation); } + /// + /// Renders the vertex buffer + /// private void RenderVertexBuffer() { if (_Vertices.Count > 0) { + //The vertex buffer locks are slow actions, its better to lock once per frame and write all vertices to the buffer at once DataStream stream = _VertexBuffer.Lock(0, _Vertices.Count * Marshal.SizeOf(typeof(TexturedColoredVertex)), LockFlags.Discard); stream.WriteRange(_Vertices.ToArray()); _VertexBuffer.Unlock(); @@ -567,11 +581,14 @@ namespace Vocaluxe.Lib.Draw for (int i = 0; i < _Vertices.Count; i += 4) { - _Device.SetTransform(TransformState.World, _VerticesRotationMatrices[i / 4]); - _Device.SetTexture(0, _VerticesTextures[i / 4]); + //Apply rotation + _Device.SetTransform(TransformState.World, _VerticesRotationMatrices.Dequeue()); + //Apply texture + _Device.SetTexture(0, _VerticesTextures.Dequeue()); + //Draw 2 triangles from vertexbuffer _Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, i, 0, 4, 0, 2); - _Device.SetTransform(TransformState.World, _VerticesRotationMatrices[i / 4]); } + //Clear the queues for the next frame _Vertices.Clear(); _VerticesTextures.Clear(); _VerticesRotationMatrices.Clear();