From 6665a023c81f5d9c3bbe6712504c72f3788a5c03 Mon Sep 17 00:00:00 2001 From: "H. Utku Maden" Date: Sun, 23 Aug 2026 22:04:46 +0300 Subject: [PATCH] Refactor ImmediateMode rendering logic to use DrawImmediate. --- Dashboard.OpenGL/Drawing/ImmediateMode.cs | 93 ++++------------------ Dashboard.OpenGL/Drawing/immediate.vert | 2 +- tests/Dashboard.TestApplication/Program.cs | 19 +++++ 3 files changed, 36 insertions(+), 78 deletions(-) diff --git a/Dashboard.OpenGL/Drawing/ImmediateMode.cs b/Dashboard.OpenGL/Drawing/ImmediateMode.cs index 6a38e7a..5f42941 100644 --- a/Dashboard.OpenGL/Drawing/ImmediateMode.cs +++ b/Dashboard.OpenGL/Drawing/ImmediateMode.cs @@ -27,7 +27,6 @@ namespace Dashboard.OpenGL.Drawing private uint _program_acolor; private int _program_transforms; private int _program_image; - private int _vao; private GLTexture _white; public void Dispose() @@ -73,8 +72,6 @@ namespace Dashboard.OpenGL.Drawing GL.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureSwizzleB, (int)All.One); GL.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest); GL.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest); - - GL.GenVertexArray(out _vao); } public void ClearColor(Color color) @@ -97,31 +94,11 @@ namespace Dashboard.OpenGL.Drawing new ImmediateVertex(new Vector3(a+tangent, depth), Vector2.Zero, color), ]; - int buffer = GL.GenBuffer(); - GL.BindBuffer(BufferTarget.ArrayBuffer, buffer); - GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * ImmediateVertex.Size, ref vertices[0], BufferUsage.StreamDraw); - - GL.BindVertexArray(_vao); - GL.VertexAttribPointer(_program_apos, 3, VertexAttribPointerType.Float, false, ImmediateVertex.Size, ImmediateVertex.PosOffset); - GL.EnableVertexAttribArray(_program_apos); - GL.VertexAttribPointer(_program_atexcoord, 2, VertexAttribPointerType.Float, false, ImmediateVertex.Size, ImmediateVertex.TexCoordsOffset); - GL.EnableVertexAttribArray(_program_atexcoord); - GL.VertexAttribPointer(_program_acolor, 4, VertexAttribPointerType.Float, false, ImmediateVertex.Size, ImmediateVertex.ColorOffset); - GL.EnableVertexAttribArray(_program_acolor); - - Matrix4x4 view = Context.ExtensionRequire().Transforms; - - GL.UseProgram(_program.Handle); - - GL.ActiveTexture(TextureUnit.Texture0); - GL.BindTexture(TextureTarget.Texture2D, _white.Handle); - - GL.UniformMatrix4f(_program_transforms, 1, true, ref view); - GL.Uniform1i(_program_image, 0); - - GL.DrawArrays(PrimitiveType.Triangles, 0, 6); - - GL.DeleteBuffer(buffer); + DrawImmediate(new ImmediateDrawCall(MeshPrimitive.Triangle, vertices.ToArray()) + { + Transforms = Context.ExtensionRequire().Transforms, + PipelineState = new PipelineState { CullMode = FaceCulling.None }, + }); } public void Rectangle(Box2d rectangle, float depth, Vector4 color) @@ -136,31 +113,11 @@ namespace Dashboard.OpenGL.Drawing new ImmediateVertex(new Vector3(rectangle.Min.X, rectangle.Max.Y, depth), Vector2.Zero, color), ]; - int buffer = GL.GenBuffer(); - GL.BindBuffer(BufferTarget.ArrayBuffer, buffer); - GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * ImmediateVertex.Size, ref vertices[0], BufferUsage.StreamDraw); - - GL.BindVertexArray(_vao); - GL.VertexAttribPointer(_program_apos, 3, VertexAttribPointerType.Float, false, ImmediateVertex.Size, ImmediateVertex.PosOffset); - GL.EnableVertexAttribArray(_program_apos); - GL.VertexAttribPointer(_program_atexcoord, 2, VertexAttribPointerType.Float, false, ImmediateVertex.Size, ImmediateVertex.TexCoordsOffset); - GL.EnableVertexAttribArray(_program_atexcoord); - GL.VertexAttribPointer(_program_acolor, 4, VertexAttribPointerType.Float, false, ImmediateVertex.Size, ImmediateVertex.ColorOffset); - GL.EnableVertexAttribArray(_program_acolor); - - Matrix4x4 view = Context.ExtensionRequire().Transforms; - - GL.UseProgram(_program.Handle); - - GL.ActiveTexture(TextureUnit.Texture0); - GL.BindTexture(TextureTarget.Texture2D, _white.Handle); - - GL.UniformMatrix4f(_program_transforms, 1, true, ref view); - GL.Uniform1i(_program_image, 0); - - GL.DrawArrays(PrimitiveType.Triangles, 0, 6); - - GL.DeleteBuffer(buffer); + DrawImmediate(new ImmediateDrawCall(MeshPrimitive.Triangle, vertices.ToArray()) + { + Transforms = Context.ExtensionRequire().Transforms, + PipelineState = new PipelineState { CullMode = FaceCulling.None }, + }); } public void Rectangle(in RectangleDrawInfo rectangle) @@ -189,30 +146,12 @@ namespace Dashboard.OpenGL.Drawing new ImmediateVertex(new Vector3(rectangle.Min.X, rectangle.Max.Y, depth), new Vector2(uv.Min.X, uv.Max.Y), Vector4.One), ]; - int buffer = GL.GenBuffer(); - GL.BindBuffer(BufferTarget.ArrayBuffer, buffer); - GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * ImmediateVertex.Size, ref vertices[0], BufferUsage.StreamDraw); - - GL.BindVertexArray(_vao); - GL.VertexAttribPointer(_program_apos, 3, VertexAttribPointerType.Float, false, ImmediateVertex.Size, ImmediateVertex.PosOffset); - GL.EnableVertexAttribArray(_program_apos); - GL.VertexAttribPointer(_program_atexcoord, 2, VertexAttribPointerType.Float, false, ImmediateVertex.Size, ImmediateVertex.TexCoordsOffset); - GL.EnableVertexAttribArray(_program_atexcoord); - GL.VertexAttribPointer(_program_acolor, 4, VertexAttribPointerType.Float, false, ImmediateVertex.Size, ImmediateVertex.ColorOffset); - GL.EnableVertexAttribArray(_program_acolor); - Matrix4x4 view = Context.ExtensionRequire().Transforms; - - GL.UseProgram(_program.Handle); - - GL.ActiveTexture(TextureUnit.Texture0); - GL.BindTexture(TextureTarget.Texture2D, ((GLTexture)texture).Handle); - - GL.UniformMatrix4f(_program_transforms, 1, true, ref view); - GL.Uniform1i(_program_image, 0); - - GL.DrawArrays(PrimitiveType.Triangles, 0, 6); - - GL.DeleteBuffer(buffer); + DrawImmediate(new ImmediateDrawCall(MeshPrimitive.Triangle, vertices.ToArray()) + { + Transforms = Context.ExtensionRequire().Transforms, + PipelineState = new PipelineState { CullMode = FaceCulling.None }, + Texture = texture, + }); } IContextBase IContextExtensionBase.Context => Context; diff --git a/Dashboard.OpenGL/Drawing/immediate.vert b/Dashboard.OpenGL/Drawing/immediate.vert index 1d93786..012b83f 100644 --- a/Dashboard.OpenGL/Drawing/immediate.vert +++ b/Dashboard.OpenGL/Drawing/immediate.vert @@ -10,7 +10,7 @@ out vec2 vTexCoords; out vec4 vColor; void main() { - vec4 position = vec4(aPos, 1.0) * transforms; + vec4 position = transforms * vec4(aPos, 1.0); gl_Position = position; vTexCoords = aTexCoords; diff --git a/tests/Dashboard.TestApplication/Program.cs b/tests/Dashboard.TestApplication/Program.cs index 403fc9c..fddfc53 100644 --- a/tests/Dashboard.TestApplication/Program.cs +++ b/tests/Dashboard.TestApplication/Program.cs @@ -88,7 +88,26 @@ window.EventRaised += (sender, eventArgs) => if (eventArgs is not PaintEventArgs paint) return; + // window.DeviceContext.Begin(); + var dcb = window.DeviceContext.ExtensionRequire(); + dcb.ResetTransforms(); + dcb.ClearDepth(); + + imm.Rectangle( + new Dashboard.Box2d( + new System.Numerics.Vector2(30, 30), + new System.Numerics.Vector2(140, 110)), + 0.2f, + new System.Numerics.Vector4(1, 1, 0, 1)); + imm.Line( + new System.Numerics.Vector2(180, 40), + new System.Numerics.Vector2(290, 180), + 6, + 0.2f, + new System.Numerics.Vector4(0, 1, 1, 1)); imm.DrawImmediate(new ImmediateDrawCall(MeshPrimitive.Triangle, vertices.AsMemory())); + + // window.DeviceContext.End(); }; app.Run(true, source.Token);