From b38344077f441b8dfc79ee91a014b65e208c5c58 Mon Sep 17 00:00:00 2001 From: CasualPokePlayer <50538166+CasualPokePlayer@users.noreply.github.com> Date: Tue, 14 May 2024 17:00:45 -0700 Subject: [PATCH] Cleanup some IGL_OpenGL variables, slightly optimize Draw() calls --- .../OpenGL/IGL_OpenGL.cs | 48 +++++++++---------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/src/BizHawk.Bizware.Graphics/OpenGL/IGL_OpenGL.cs b/src/BizHawk.Bizware.Graphics/OpenGL/IGL_OpenGL.cs index c94e48e8dc..2432d2ca59 100644 --- a/src/BizHawk.Bizware.Graphics/OpenGL/IGL_OpenGL.cs +++ b/src/BizHawk.Bizware.Graphics/OpenGL/IGL_OpenGL.cs @@ -102,12 +102,12 @@ namespace BizHawk.Bizware.Graphics private class ShaderWrapper { - public uint sid; + public uint SID; } private class PipelineWrapper { - public uint pid; + public uint PID; public BizShader FragmentShader, VertexShader; public List SamplerLocs; } @@ -136,8 +136,8 @@ namespace BizHawk.Bizware.Graphics var fsw = (ShaderWrapper)fragmentShader.Opaque; var pid = GL.CreateProgram(); - GL.AttachShader(pid, vsw.sid); - GL.AttachShader(pid, fsw.sid); + GL.AttachShader(pid, vsw.SID); + GL.AttachShader(pid, fsw.SID); _ = GL.GetError(); GL.LinkProgram(pid); @@ -234,7 +234,7 @@ namespace BizHawk.Bizware.Graphics if (!vertexShader.Available) success = false; if (!fragmentShader.Available) success = false; - var pw = new PipelineWrapper { pid = pid, VertexShader = vertexShader, FragmentShader = fragmentShader, SamplerLocs = samplers }; + var pw = new PipelineWrapper { PID = pid, VertexShader = vertexShader, FragmentShader = fragmentShader, SamplerLocs = samplers }; return new(this, pw, success, vertexLayout, uniforms, memo); } @@ -247,7 +247,7 @@ namespace BizHawk.Bizware.Graphics return; } - GL.DeleteProgram(pw.pid); + GL.DeleteProgram(pw.PID); pw.FragmentShader.Release(); pw.VertexShader.Release(); @@ -256,7 +256,7 @@ namespace BizHawk.Bizware.Graphics public void Internal_FreeShader(BizShader shader) { var sw = (ShaderWrapper)shader.Opaque; - GL.DeleteShader(sw.sid); + GL.DeleteShader(sw.SID); } /// . is @@ -276,7 +276,7 @@ namespace BizHawk.Bizware.Graphics } var pw = (PipelineWrapper)pipeline.Opaque; - GL.UseProgram(pw.pid); + GL.UseProgram(pw.PID); // this is dumb and confusing, but we have to bind physical sampler numbers to sampler variables. for (var i = 0; i < pw.SamplerLocs.Count; i++) @@ -287,18 +287,18 @@ namespace BizHawk.Bizware.Graphics private class VertexLayoutWrapper { - public uint vao; - public uint vbo; - public int bufferLen; + public uint VAO; + public uint VBO; + public int BufferLen; } public VertexLayout CreateVertexLayout() { - var vlw = new VertexLayoutWrapper() + var vlw = new VertexLayoutWrapper { - vao = GL.GenVertexArray(), - vbo = GL.GenBuffer(), - bufferLen = 0, + VAO = GL.GenVertexArray(), + VBO = GL.GenBuffer(), + BufferLen = 0, }; return new(this, vlw); @@ -307,8 +307,8 @@ namespace BizHawk.Bizware.Graphics public void Internal_FreeVertexLayout(VertexLayout vl) { var vlw = (VertexLayoutWrapper)vl.Opaque; - GL.DeleteVertexArray(vlw.vao); - GL.DeleteBuffer(vlw.vbo); + GL.DeleteVertexArray(vlw.VAO); + GL.DeleteBuffer(vlw.VBO); } private void BindTexture2d(Texture2d tex) @@ -394,8 +394,8 @@ namespace BizHawk.Bizware.Graphics } var vlw = (VertexLayoutWrapper)vertexLayout.Opaque; - GL.BindVertexArray(vlw.vao); - GL.BindBuffer(GLEnum.ArrayBuffer, vlw.vbo); + GL.BindVertexArray(vlw.VAO); + GL.BindBuffer(GLEnum.ArrayBuffer, vlw.VBO); var stride = vertexLayout.Items[0].Stride; Debug.Assert(vertexLayout.Items.All(i => i.Value.Stride == stride)); @@ -404,19 +404,17 @@ namespace BizHawk.Bizware.Graphics { var vertexes = new ReadOnlySpan(data.ToPointer(), count * stride); - // BufferData reallocs and BufferSubData doesn't, so only use the former if size changes - if (vertexes.Length != vlw.bufferLen) + // BufferData reallocs and BufferSubData doesn't, so only use the former if we need to grow the buffer + if (vertexes.Length > vlw.BufferLen) { GL.BufferData(GLEnum.ArrayBuffer, vertexes, GLEnum.DynamicDraw); + vlw.BufferLen = vertexes.Length; } else { - // I tried to do a CRC check to avoid BufferSubData calls, actually hurts performance GL.BufferSubData(GLEnum.ArrayBuffer, 0, vertexes); } - vlw.bufferLen = vertexes.Length; - foreach (var (i, item) in vertexLayout.Items) { GL.VertexAttribPointer( @@ -710,7 +708,7 @@ namespace BizHawk.Bizware.Graphics Errors = info }; - sw.sid = sid; + sw.SID = sid; return ret; }