Skip to content

Commit 53f4277

Browse files
author
LoneWandererProductions
committed
sync up my renderengine
1 parent d4af626 commit 53f4277

3 files changed

Lines changed: 59 additions & 28 deletions

File tree

RenderEngine/RenderBatch.cs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@ public sealed class RenderBatch
1515
{
1616
public readonly List<float> SolidLineVertices = new();
1717
public readonly List<float> SolidTriangleVertices = new();
18-
public readonly List<float> TexVertices = new();
18+
public readonly Dictionary<int, List<float>> TexturedBatches = new();
19+
1920
public readonly List<Action> HostActions = new();
2021

21-
public void AddColoredLine(
22-
float x0, float y0,
23-
float x1, float y1,
24-
int r, int g, int b, int a)
22+
public void AddColoredLine(float x, float y, int r, int g, int b, int a)
2523
{
26-
AddColoredVertex(x0, y0, r, g, b, a, SolidLineVertices);
27-
AddColoredVertex(x1, y1, r, g, b, a, SolidLineVertices);
24+
// Pushes a single vertex into the line batch.
25+
// When flushed, OpenGL will connect every 2 consecutive vertices into a line segment.
26+
AddColoredVertex(x, y, r, g, b, a, SolidLineVertices);
27+
}
28+
29+
public void AddSolidTriangleVertex(float x, float y, int r, int g, int b, int a)
30+
{
31+
AddColoredVertex(x, y, r, g, b, a, SolidTriangleVertices);
2832
}
2933

3034
public void AddColoredTriangle(
@@ -54,25 +58,35 @@ public void AddSolidQuad(
5458
public void AddTexturedQuad(
5559
(int x, int y) p1, (int x, int y) p2,
5660
(int x, int y) p3, (int x, int y) p4,
57-
int textureId) // Note: Batching assumes the same textureId for the whole batch for now
61+
int textureId)
5862
{
59-
AddTexturedTriangle(p1, p2, p3);
60-
AddTexturedTriangle(p1, p3, p4);
63+
AddTexturedTriangle(p1, p2, p3, textureId);
64+
AddTexturedTriangle(p1, p3, p4, textureId);
6165
}
66+
6267
public void AddTexturedTriangle(
63-
(int x, int y) p1, (int x, int y) p2, (int x, int y) p3)
68+
(int x, int y) p1, (int x, int y) p2, (int x, int y) p3,
69+
int textureId)
6470
{
65-
AddTexturedVertex(p1.x, p1.y, 0f, 0f);
66-
AddTexturedVertex(p2.x, p2.y, 1f, 0f);
67-
AddTexturedVertex(p3.x, p3.y, 0f, 1f);
71+
// Grab the list for this specific texture, or create it if it doesn't exist
72+
if (!TexturedBatches.TryGetValue(textureId, out var list))
73+
{
74+
list = new List<float>();
75+
TexturedBatches[textureId] = list;
76+
}
77+
78+
// Notice we only push the 4 required floats, but into the correct texture's list
79+
AddTexturedVertex(p1.x, p1.y, 0f, 0f, list);
80+
AddTexturedVertex(p2.x, p2.y, 1f, 0f, list);
81+
AddTexturedVertex(p3.x, p3.y, 0f, 1f, list);
6882
}
6983

70-
private void AddTexturedVertex(float x, float y, float u, float v)
84+
private void AddTexturedVertex(float x, float y, float u, float v, List<float> targetList)
7185
{
72-
TexVertices.Add(x);
73-
TexVertices.Add(y);
74-
TexVertices.Add(u);
75-
TexVertices.Add(v);
86+
targetList.Add(x);
87+
targetList.Add(y);
88+
targetList.Add(u);
89+
targetList.Add(v);
7690
}
7791

7892
private void AddColoredVertex(
@@ -85,11 +99,12 @@ private void AddColoredVertex(
8599
targetList.Add(b / 255f);
86100
targetList.Add(a / 255f);
87101
}
102+
88103
public void Clear()
89104
{
90-
TexVertices.Clear();
91105
SolidLineVertices.Clear();
92106
SolidTriangleVertices.Clear();
107+
TexturedBatches.Clear(); // Clear the dictionary here
93108
HostActions.Clear();
94109
}
95110

RenderEngine/Simple2DRenderer.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,6 @@ public void DrawFullscreenQuad(int idx)
515515
((int)w, (int)h),
516516
(0, (int)h));
517517

518-
// FIXED: Using correct texture VBO, capacity, and checking data.Length instead of texture ID
519518
EnsureBufferCapacity(_vboTex, ref _vboTexCapacity, data.Length);
520519

521520
GL.BindBuffer(BufferTarget.ArrayBuffer, _vboTex);
@@ -563,19 +562,34 @@ private void FlushBatch(RenderBatch batch)
563562
}
564563

565564
// --- Textured Vertices ---
566-
if (batch.TexVertices.Count > 0)
565+
if (batch.TexturedBatches.Count > 0)
567566
{
568567
GL.UseProgram(_ui2DTextureShader);
569568
GL.BindVertexArray(_vaoTex);
570569

571-
var data = batch.TexVertices.ToArray();
572-
EnsureBufferCapacity(_vboTex, ref _vboTexCapacity, data.Length);
570+
// Loop through each texture group
571+
foreach (var kvp in batch.TexturedBatches)
572+
{
573+
int texId = kvp.Key;
574+
var dataList = kvp.Value;
573575

574-
GL.BindBuffer(BufferTarget.ArrayBuffer, _vboTex);
575-
GL.BufferSubData(BufferTarget.ArrayBuffer, IntPtr.Zero, data.Length * sizeof(float), data);
576+
if (dataList.Count == 0) continue;
577+
578+
var data = dataList.ToArray();
579+
EnsureBufferCapacity(_vboTex, ref _vboTexCapacity, data.Length);
580+
581+
// Bind the specific texture for this batch
582+
GL.ActiveTexture(TextureUnit.Texture0);
583+
584+
// Use checkerboard if the texture ID is invalid (< 0)
585+
GL.BindTexture(TextureTarget.Texture2D, texId < 0 ? GetOrCreateCheckerboardTexture() : texId);
586+
GL.Uniform1(GL.GetUniformLocation(_ui2DTextureShader, "uTexture"), 0);
576587

577-
// Note: For now, this assumes the appropriate texture was bound BEFORE calling FlushBatch.
578-
GL.DrawArrays(PrimitiveType.Triangles, 0, data.Length / 4);
588+
// Upload and draw
589+
GL.BindBuffer(BufferTarget.ArrayBuffer, _vboTex);
590+
GL.BufferSubData(BufferTarget.ArrayBuffer, IntPtr.Zero, data.Length * sizeof(float), data);
591+
GL.DrawArrays(PrimitiveType.Triangles, 0, data.Length / 4);
592+
}
579593

580594
GL.BindTexture(TextureTarget.Texture2D, 0);
581595
GL.BindVertexArray(0);

RenderEngine/UnmanagedImageBuffer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ public void BlitRegion(UnmanagedImageBuffer src, int srcX, int srcY, int width,
311311
/// </summary>
312312
public IntPtr Buffer => _buffer;
313313

314+
public int Id { get; set; }
315+
314316
/// <summary>
315317
/// Returns a hash code for this instance.
316318
/// </summary>

0 commit comments

Comments
 (0)