From 614602e7cdda79510b85700c130fede42375d81f Mon Sep 17 00:00:00 2001 From: Harrison Date: Tue, 7 Jul 2020 01:07:14 -0500 Subject: [PATCH] MeshBatch now records states to reduce data sent to shader per call. --- .../Graphics/Render/MeshBatch.cs | 13 ++++++++++--- .../Utilities/Playground/MainState.cs | 18 +++++++++--------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/SlatedGameToolkit.Framework/Graphics/Render/MeshBatch.cs b/src/SlatedGameToolkit.Framework/Graphics/Render/MeshBatch.cs index cd3878c..3685134 100644 --- a/src/SlatedGameToolkit.Framework/Graphics/Render/MeshBatch.cs +++ b/src/SlatedGameToolkit.Framework/Graphics/Render/MeshBatch.cs @@ -17,6 +17,7 @@ namespace SlatedGameToolkit.Framework.Graphics.Render private float batchDelta; public GLContext GLContext {get; private set; } private int projALoc, viewALoc, modelALoc, texturedALoc, singleChanneledALoc, flippedALoc; + private bool flipped, singleChanneled; private Camera camera; private RenderProgram renderProgram; private ITexture texture; @@ -31,7 +32,7 @@ namespace SlatedGameToolkit.Framework.Graphics.Render private int[] verticeOffsets; private int dataIndex, indicesIndex, offsetIndex; - public MeshBatch(Camera camera, RenderProgram renderProgram = null, uint BatchVertexSize = 4096) { + public MeshBatch(Camera camera, RenderProgram renderProgram = null, uint BatchVertexSize = 1024) { if (renderProgram == null) { VertexShader vert = new VertexShader(EmbeddedResUtils.ReadEmbeddedResourceText("default.vert")); FragmentShader frag = new FragmentShader(EmbeddedResUtils.ReadEmbeddedResourceText("default.frag")); @@ -94,8 +95,14 @@ namespace SlatedGameToolkit.Framework.Graphics.Render GLContext.Uniform1i(texturedALoc, texture == null ? 0 : 1); } if (texture != null) { - GLContext.Uniform1i(singleChanneledALoc, texture.SingleChanneled ? 1 : 0); - GLContext.Uniform1i(flippedALoc, texture.Flipped ? 1 : 0); + if (flipped != texture.Flipped) { + GLContext.Uniform1i(flippedALoc, texture.Flipped ? 1 : 0); + this.flipped = texture.Flipped; + } + if (singleChanneled != texture.SingleChanneled) { + GLContext.Uniform1i(singleChanneledALoc, texture.SingleChanneled ? 1 : 0); + this.singleChanneled = texture.SingleChanneled; + } } ValueTuple[] vertices = mesh.Vertices; uint[] indices = mesh.Elements; diff --git a/src/SlatedGameToolkit.Tools/Utilities/Playground/MainState.cs b/src/SlatedGameToolkit.Tools/Utilities/Playground/MainState.cs index 6800451..85fbd62 100644 --- a/src/SlatedGameToolkit.Tools/Utilities/Playground/MainState.cs +++ b/src/SlatedGameToolkit.Tools/Utilities/Playground/MainState.cs @@ -17,7 +17,7 @@ namespace SlatedGameToolkit.Tools.Utilities.Playground { private WindowContext window; private Camera2D camera; - private MeshBatch renderer; + private MeshBatch batch; private BitmapFont font; private Texture logoTexture, fillerTexture; private RectangleMesh logo, textureTester, untextured; @@ -40,7 +40,7 @@ namespace SlatedGameToolkit.Tools.Utilities.Playground logoTexture.Dispose(); fillerTexture.Dispose(); font.Dispose(); - renderer.Dispose(); + batch.Dispose(); window.Dispose(); } @@ -55,7 +55,7 @@ namespace SlatedGameToolkit.Tools.Utilities.Playground Vector2 drawableDimensions = window.GetDrawableDimensions(); float ratio = drawableDimensions.Y / drawableDimensions.X; camera = new Camera2D(2f, ratio * 2f); - renderer = new MeshBatch(camera); + batch = new MeshBatch(camera, BatchVertexSize: 2048); logoTexture = TextureLoader.Load2DTexture("Resources/Playground/yhdnbgnc.png"); logo = new RectangleMesh(logoTexture, Color.White); @@ -84,12 +84,12 @@ namespace SlatedGameToolkit.Tools.Utilities.Playground public void Render(double delta) { - renderer.Begin(Matrix4x4.Identity, delta); - renderer.Draw(logo); - renderer.Draw(textureTester); - renderer.Draw(untextured); - font.WriteLine(renderer, 0.25f, -0.35f, "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n1234567890", Color.White); - renderer.End(); + batch.Begin(Matrix4x4.Identity, delta); + batch.Draw(logo); + batch.Draw(textureTester); + batch.Draw(untextured); + font.WriteLine(batch, 0.25f, -0.35f, "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n1234567890", Color.White); + batch.End(); } public void Update(double timeStep)