Dashboard/Quik/VertexGenerator/DrawQueue.cs

112 lines
3.8 KiB
C#
Raw Normal View History

2023-06-23 21:56:07 +02:00
using System;
2023-06-29 09:31:46 +02:00
using System.Collections;
2023-06-23 21:56:07 +02:00
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace Quik.VertexGenerator
{
2023-06-29 09:31:46 +02:00
public class DrawQueue : IEnumerable<DrawCall>
2023-06-23 21:56:07 +02:00
{
private readonly RefList<QuikVertex> _vertices = new RefList<QuikVertex>();
private readonly RefList<int> _elements = new RefList<int>();
private readonly List<DrawCall> _drawCalls = new List<DrawCall>();
private int _start;
private int _baseOffset;
private QRectangle _bounds;
2023-06-23 21:56:07 +02:00
private QuikTexture _texture;
public int ZDepth { get; private set; }
public QuikVertex[] VertexArray => _vertices.InternalArray;
public int VertexCount => _vertices.Count;
public int[] ElementArray => _elements.InternalArray;
public int ElementCount => _elements.Count;
public int DrawCallCount => _drawCalls.Count;
public int BaseOffset => _baseOffset;
public void Clear()
{
_vertices.Clear();
_elements.Clear();
_drawCalls.Clear();
ZDepth = 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void StartDrawCall(in QRectangle bounds, QuikTexture texture, int baseOffset)
2023-06-23 21:56:07 +02:00
{
_start = ElementCount;
_texture = texture;
_bounds = bounds;
_baseOffset = baseOffset;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void StartDrawCall(in QRectangle bounds) => StartDrawCall(bounds, null, _vertices.Count);
2023-06-23 21:56:07 +02:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void StartDrawCall(in QRectangle bounds, int baseOffset) => StartDrawCall(bounds, null, baseOffset);
2023-06-23 21:56:07 +02:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void StartDrawCall(in QRectangle bounds, QuikTexture texture) => StartDrawCall(bounds, texture, _vertices.Count);
2023-06-23 21:56:07 +02:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddVertex(in QuikVertex vertex)
{
_vertices.Add(in vertex);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddElement(int offset)
{
_elements.Add(offset + _baseOffset);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int RestoreOffset(int baseOffset)
{
int old = _baseOffset;
_baseOffset = baseOffset;
return old;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int RestoreOffset() => RestoreOffset(_vertices.Count);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int AbsoluteElement(int offset)
{
return _baseOffset + offset;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int RelativeElement(int baseOffset, int offset)
{
return AbsoluteElement(offset) - baseOffset;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void EndDrawCall()
{
2023-06-29 09:31:46 +02:00
int count = ElementCount - _start;
_drawCalls.Add(new DrawCall(_start, count, _bounds, _texture));
2023-06-23 21:56:07 +02:00
}
2023-06-29 09:31:46 +02:00
public IEnumerator<DrawCall> GetEnumerator() => _drawCalls.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _drawCalls.GetEnumerator();
2023-06-23 21:56:07 +02:00
}
public struct DrawCall
{
public int Start { get; }
public int Count { get; }
public QRectangle Bounds { get; }
2023-06-23 21:56:07 +02:00
public QuikTexture Texture { get; }
public DrawCall(int start, int count, in QRectangle bounds, QuikTexture texture)
2023-06-23 21:56:07 +02:00
{
Start = start;
Count = count;
Bounds = bounds;
Texture = texture;
}
}
}