MeshBatch now records states to reduce data sent to shader per call.

This commit is contained in:
Harrison Deng 2020-07-07 01:07:14 -05:00
parent fd7edc2629
commit 614602e7cd
2 changed files with 19 additions and 12 deletions

View File

@ -17,6 +17,7 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
private float batchDelta; private float batchDelta;
public GLContext GLContext {get; private set; } public GLContext GLContext {get; private set; }
private int projALoc, viewALoc, modelALoc, texturedALoc, singleChanneledALoc, flippedALoc; private int projALoc, viewALoc, modelALoc, texturedALoc, singleChanneledALoc, flippedALoc;
private bool flipped, singleChanneled;
private Camera camera; private Camera camera;
private RenderProgram renderProgram; private RenderProgram renderProgram;
private ITexture texture; private ITexture texture;
@ -31,7 +32,7 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
private int[] verticeOffsets; private int[] verticeOffsets;
private int dataIndex, indicesIndex, offsetIndex; 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) { if (renderProgram == null) {
VertexShader vert = new VertexShader(EmbeddedResUtils.ReadEmbeddedResourceText("default.vert")); VertexShader vert = new VertexShader(EmbeddedResUtils.ReadEmbeddedResourceText("default.vert"));
FragmentShader frag = new FragmentShader(EmbeddedResUtils.ReadEmbeddedResourceText("default.frag")); FragmentShader frag = new FragmentShader(EmbeddedResUtils.ReadEmbeddedResourceText("default.frag"));
@ -94,8 +95,14 @@ namespace SlatedGameToolkit.Framework.Graphics.Render
GLContext.Uniform1i(texturedALoc, texture == null ? 0 : 1); GLContext.Uniform1i(texturedALoc, texture == null ? 0 : 1);
} }
if (texture != null) { if (texture != null) {
GLContext.Uniform1i(singleChanneledALoc, texture.SingleChanneled ? 1 : 0); if (flipped != texture.Flipped) {
GLContext.Uniform1i(flippedALoc, texture.Flipped ? 1 : 0); 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<Vector3, Vector2>[] vertices = mesh.Vertices; ValueTuple<Vector3, Vector2>[] vertices = mesh.Vertices;
uint[] indices = mesh.Elements; uint[] indices = mesh.Elements;

View File

@ -17,7 +17,7 @@ namespace SlatedGameToolkit.Tools.Utilities.Playground
{ {
private WindowContext window; private WindowContext window;
private Camera2D camera; private Camera2D camera;
private MeshBatch renderer; private MeshBatch batch;
private BitmapFont font; private BitmapFont font;
private Texture logoTexture, fillerTexture; private Texture logoTexture, fillerTexture;
private RectangleMesh logo, textureTester, untextured; private RectangleMesh logo, textureTester, untextured;
@ -40,7 +40,7 @@ namespace SlatedGameToolkit.Tools.Utilities.Playground
logoTexture.Dispose(); logoTexture.Dispose();
fillerTexture.Dispose(); fillerTexture.Dispose();
font.Dispose(); font.Dispose();
renderer.Dispose(); batch.Dispose();
window.Dispose(); window.Dispose();
} }
@ -55,7 +55,7 @@ namespace SlatedGameToolkit.Tools.Utilities.Playground
Vector2 drawableDimensions = window.GetDrawableDimensions(); Vector2 drawableDimensions = window.GetDrawableDimensions();
float ratio = drawableDimensions.Y / drawableDimensions.X; float ratio = drawableDimensions.Y / drawableDimensions.X;
camera = new Camera2D(2f, ratio * 2f); camera = new Camera2D(2f, ratio * 2f);
renderer = new MeshBatch(camera); batch = new MeshBatch(camera, BatchVertexSize: 2048);
logoTexture = TextureLoader.Load2DTexture("Resources/Playground/yhdnbgnc.png"); logoTexture = TextureLoader.Load2DTexture("Resources/Playground/yhdnbgnc.png");
logo = new RectangleMesh(logoTexture, Color.White); logo = new RectangleMesh(logoTexture, Color.White);
@ -84,12 +84,12 @@ namespace SlatedGameToolkit.Tools.Utilities.Playground
public void Render(double delta) public void Render(double delta)
{ {
renderer.Begin(Matrix4x4.Identity, delta); batch.Begin(Matrix4x4.Identity, delta);
renderer.Draw(logo); batch.Draw(logo);
renderer.Draw(textureTester); batch.Draw(textureTester);
renderer.Draw(untextured); batch.Draw(untextured);
font.WriteLine(renderer, 0.25f, -0.35f, "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n1234567890", Color.White); font.WriteLine(batch, 0.25f, -0.35f, "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n1234567890", Color.White);
renderer.End(); batch.End();
} }
public void Update(double timeStep) public void Update(double timeStep)