From ae0f4c6e1972aab2088644b530b4ce29e57dabc6 Mon Sep 17 00:00:00 2001 From: Matt Date: Fri, 14 Aug 2020 18:23:02 -0700 Subject: [PATCH] OpenGL Batcher work --- src/PlatformLayer/Batcher.bf | 82 ++++++++++++++++++++++---- src/PlatformLayer/GL.bf | 2 +- src/PlatformLayer/SDL2PlatformLayer.bf | 46 +++++++-------- 3 files changed, 94 insertions(+), 36 deletions(-) diff --git a/src/PlatformLayer/Batcher.bf b/src/PlatformLayer/Batcher.bf index 4d4b961..65ac00f 100644 --- a/src/PlatformLayer/Batcher.bf +++ b/src/PlatformLayer/Batcher.bf @@ -1,36 +1,98 @@ -using System.Collections.Generic; +using System.Collections; +using System; namespace Strawberry { public class Batcher { - List batchStack = new System.Collections.List() ~ DeleteContainerAndItems!(_); + static public int VertexSize => sizeof(Vertex); - Batch top => batchStack.Count > 0 ? batchStack[batchStack.Count - 1] : null; + private List batchStack = new List() ~ DeleteContainerAndItems!(_); + private Batch top => batchStack.Count > 0 ? batchStack[batchStack.Count - 1] : null; - public void PushQuad(Vertex a, Vertex b, Vertex c, Vertex d) + private List vertices = new .() ~ delete _; + private List indices = new .() ~ delete _; + + private uint32 vaoID; + private uint32 vertexBufferID; + private uint32 indexBufferID; + + public this() { - + GL.glGenVertexArrays(1, &vaoID); + GL.glBindVertexArray(vaoID); + GL.glGenBuffers(1, &vertexBufferID); + GL.glGenBuffers(1, &indexBufferID); + GL.glBindVertexArray(0); + } + + public ~this() + { + GL.glDeleteBuffers(1, &vertexBufferID); + GL.glDeleteBuffers(1, &indexBufferID); + GL.glDeleteVertexArrays(1, &vaoID); + } + + public void Draw() + { + GL.glBindVertexArray(vaoID); + GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexBufferID); + GL.glBufferData(GL.GL_ARRAY_BUFFER, vertices.Count * sizeof(Vertex), vertices.Ptr, GL.GL_DYNAMIC_DRAW); + GL.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, indexBufferID); + GL.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, indices.Count * sizeof(uint32), indices.Ptr, GL.GL_DYNAMIC_DRAW); + + GL.glDrawElements(GL.GL_TRIANGLES, indices.Count, GL.GL_UNSIGNED_INT, (void*)0); + GL.glBindVertexArray(0); + + vertices.Clear(); + indices.Clear(); + } + + public void PushQuad(Vector a, Vector b, Vector c, Vector d, Color color) + { + uint32 count = (uint32)vertices.Count; + + vertices.Add(Vertex.Shape(a, color)); + vertices.Add(Vertex.Shape(b, color)); + vertices.Add(Vertex.Shape(c, color)); + vertices.Add(Vertex.Shape(d, color)); + + indices.Add(count + 0); + indices.Add(count + 1); + indices.Add(count + 2); + indices.Add(count + 0); + indices.Add(count + 2); + indices.Add(count + 3); } private class Batch { uint32 bufferHandle; - List Vertices; - public this() { - GL.glGenBuffers(1, &bufferHandle); - GL.glBindBuffer(GL.GL_ARRAY_BUFFER, bufferHandle); - GL.glDeleteBuffers(1, &bufferHandle); + //GL.glGenBuffers(1, &bufferHandle); + //GL.glBindBuffer(GL.GL_ARRAY_BUFFER, bufferHandle); + //GL.glDeleteBuffers(1, &bufferHandle); } } + [Ordered, Packed, CRepr] private struct Vertex { public Vector Position; public Color Color; + public Vector TexCoord; + public (uint8, uint8, uint8) Mode; + + static public Vertex Shape(Vector pos, Color color) + { + Vertex v = Vertex(); + v.Position = pos; + v.Color = color; + v.Mode = (0, 0, 255); + return v; + } } } } diff --git a/src/PlatformLayer/GL.bf b/src/PlatformLayer/GL.bf index 8ce851b..66b1afc 100644 --- a/src/PlatformLayer/GL.bf +++ b/src/PlatformLayer/GL.bf @@ -942,7 +942,7 @@ namespace Strawberry public function void GlGetIntegerv(uint pname, int32* data); public static GlGetIntegerv glGetIntegerv; - public function uint8 GlGetString(uint name); + public function uint8* GlGetString(uint name); public static GlGetString glGetString; public function void GlGetTexImage(uint target, int level, uint format, uint type, void* pixels); diff --git a/src/PlatformLayer/SDL2PlatformLayer.bf b/src/PlatformLayer/SDL2PlatformLayer.bf index 64d30ea..22dd10c 100644 --- a/src/PlatformLayer/SDL2PlatformLayer.bf +++ b/src/PlatformLayer/SDL2PlatformLayer.bf @@ -13,8 +13,6 @@ namespace Strawberry private SDL2Shader shader; private SDL.SDL_GameController*[] gamepads; private bool* keyboard; - private uint32 vertexArray; - private uint32 vertexbuffer; private SDL.SDL_GLContext glContext; private uint glProgram; @@ -59,7 +57,7 @@ namespace Strawberry // vertex shader """ #version 330 - uniform mat3x2 u_matrix; + uniform mat4 u_matrix; layout(location=0) in vec2 a_position; layout(location=1) in vec2 a_tex; layout(location=2) in vec4 a_color; @@ -87,7 +85,7 @@ namespace Strawberry void main(void) { vec4 color = texture(u_texture, v_tex); - o_color = + o_color = v_type.x * color * v_col + v_type.y * color.a * v_col + v_type.z * v_col; @@ -105,18 +103,14 @@ namespace Strawberry if (logLen > 0) Calc.Log(&log, logLen); - GL.glGenVertexArrays(1, &vertexArray); - GL.glBindVertexArray(vertexArray); - - float[?] g_vertex_buffer_data = .( - -1.0f, -1.0f, - 1.0f, -1.0f, - 0.0f, 1.0f, - ); - - GL.glGenBuffers(1, &vertexbuffer); - GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexbuffer); - GL.glBufferData(GL.GL_ARRAY_BUFFER, sizeof(float[6]), &g_vertex_buffer_data, GL.GL_STATIC_DRAW); + GL.glEnableVertexAttribArray(0); + GL.glVertexAttribPointer(0, 2, GL.GL_FLOAT, GL.GL_FALSE, Batcher.VertexSize, (void*)0); + GL.glEnableVertexAttribArray(1); + GL.glVertexAttribPointer(1, 4, GL.GL_UNSIGNED_BYTE, GL.GL_TRUE, Batcher.VertexSize, (void*)8); + GL.glEnableVertexAttribArray(2); + GL.glVertexAttribPointer(2, 2, GL.GL_FLOAT, GL.GL_FALSE, Batcher.VertexSize, (void*)12); + GL.glEnableVertexAttribArray(3); + GL.glVertexAttribPointer(3, 3, GL.GL_UNSIGNED_BYTE, GL.GL_TRUE, Batcher.VertexSize, (void*)20); } //Audio @@ -163,16 +157,18 @@ namespace Strawberry GL.glClear(GL.GL_COLOR_BUFFER_BIT); GL.glUseProgram(glProgram); - int loc = GL.glGetUniformLocation(glProgram, "u_matrix"); - Matrix mat = Matrix.Identity; - GL.glUniformMatrix3x2fv(loc, 1, GL.GL_FALSE, &mat.Values); + float[16] mat = + .(1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 0, 0, 0, 1); - GL.glEnableVertexAttribArray(0); - GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertexbuffer); - GL.glVertexAttribPointer(0, 2, GL.GL_FLOAT, GL.GL_FALSE, 0, (void*)0 ); - // Draw the triangle ! - GL.glDrawArrays(GL.GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle - GL.glDisableVertexAttribArray(0); + let loc = GL.glGetUniformLocation(glProgram, "u_matrix"); + GL.glUniformMatrix4fv(loc, 1, GL.GL_FALSE, &mat); + + Batcher b = scope Batcher(); + b.PushQuad(.(-1, -1), .(1, -1), .(1, 1), .(-1, 1), .Magenta); + b.Draw(); } public override void RenderEnd()