mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Reimplemented Renderer2D
- Renderer2D now static - Renderer now calls Init and Deinit of Renderer2D - Renderer2D now calls Init and Deinit of FontRenderer - Fixed Deinit order of Layers and renderer (e.g. Layers now deinit first) - Sandbox2D now uses SandboxApp (only loads different layer)
This commit is contained in:
@@ -16,11 +16,11 @@ namespace GlitchyEngine
|
||||
private bool _running = true;
|
||||
private bool _isMinimized = false;
|
||||
|
||||
private LayerStack _layerStack = new LayerStack() ~ delete _;
|
||||
private LayerStack _layerStack = new LayerStack();
|
||||
|
||||
private ImGuiLayer _imGuiLayer;
|
||||
|
||||
private GameTime _gameTime = new GameTime(true) ~ delete _;
|
||||
private GameTime _gameTime = new GameTime(true);
|
||||
|
||||
public bool IsRunning => _running;
|
||||
public Window Window => _window;
|
||||
@@ -57,7 +57,10 @@ namespace GlitchyEngine
|
||||
|
||||
public ~this()
|
||||
{
|
||||
delete _layerStack;
|
||||
SamplerStateManager.Uninit();
|
||||
Renderer.Deinit();
|
||||
delete _gameTime;
|
||||
}
|
||||
|
||||
public void OnEvent(Event e)
|
||||
|
||||
@@ -113,6 +113,12 @@ namespace GlitchyEngine.Renderer
|
||||
*(T*)firstByte = value;
|
||||
}
|
||||
|
||||
[Inline]
|
||||
private T GetData<T>()
|
||||
{
|
||||
return *(T*)firstByte;
|
||||
}
|
||||
|
||||
public void SetData(bool value) => SetData<bool>(value);
|
||||
|
||||
public void SetData(float value) => SetData<float>(value);
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace GlitchyEngine.Renderer
|
||||
public Matrix Transform;
|
||||
}
|
||||
|
||||
static GraphicsContext _context ~ _?.ReleaseRef();
|
||||
static internal GraphicsContext _context ~ _?.ReleaseRef();
|
||||
|
||||
//static Buffer<SceneConstants> _sceneConstants ~ _?.ReleaseRef();
|
||||
|
||||
@@ -39,10 +39,16 @@ namespace GlitchyEngine.Renderer
|
||||
*/
|
||||
|
||||
RenderCommand.Init();
|
||||
Renderer2D.Init();
|
||||
|
||||
InitLineRenderer(effectLibrary);
|
||||
}
|
||||
|
||||
public static void Deinit()
|
||||
{
|
||||
Renderer2D.Deinit();
|
||||
}
|
||||
|
||||
static void InitLineRenderer(EffectLibrary effectLibrary)
|
||||
{
|
||||
LineEffect = effectLibrary.Load("content\\Shaders\\lineShader.hlsl");
|
||||
|
||||
@@ -2,13 +2,61 @@ using GlitchyEngine.Math;
|
||||
using System.Collections;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using GlitchyEngine.Renderer.Text;
|
||||
|
||||
namespace GlitchyEngine.Renderer
|
||||
{
|
||||
using internal GlitchyEngine.Renderer;
|
||||
|
||||
public class Renderer2D
|
||||
public static class Renderer2D
|
||||
{
|
||||
[CRepr]
|
||||
struct QuadVertex : IVertexData
|
||||
{
|
||||
public Vector2 Position;
|
||||
public Vector2 Texcoord;
|
||||
|
||||
public this(Vector2 position, Vector2 texcoord)
|
||||
{
|
||||
Position = position;
|
||||
Texcoord = texcoord;
|
||||
}
|
||||
|
||||
public this(float x, float y, float texX, float texY)
|
||||
{
|
||||
Position = .(x, y);
|
||||
Texcoord = .(texX, texY);
|
||||
}
|
||||
|
||||
public static VertexElement[] VertexElements ~ delete _;
|
||||
|
||||
public static VertexElement[] IVertexData.VertexElements => VertexElements;
|
||||
|
||||
static this()
|
||||
{
|
||||
VertexElements = new VertexElement[]
|
||||
(
|
||||
VertexElement(.R32G32_Float, "POSITION"),
|
||||
VertexElement(.R32G32_Float, "TEXCOORD"),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
[CRepr]
|
||||
struct BatchVertex
|
||||
{
|
||||
public Matrix Transform;
|
||||
public ColorRGBA Color;
|
||||
public Vector4 UVTransform;
|
||||
|
||||
public this(Matrix transform, ColorRGBA color, Vector4 uvTransform)
|
||||
{
|
||||
Transform = transform;
|
||||
Color = color;
|
||||
UVTransform = uvTransform;
|
||||
}
|
||||
}
|
||||
|
||||
enum DrawOrder
|
||||
{
|
||||
/// Immediately draw the sprites.
|
||||
@@ -30,96 +78,55 @@ namespace GlitchyEngine.Renderer
|
||||
FrontToBack
|
||||
}
|
||||
|
||||
struct RenderVertex : IVertexData
|
||||
struct QueueQuad: this(Matrix Transform, ColorRGBA Color, Texture2D Texture, float Depth, Vector4 uvTransform) { }
|
||||
|
||||
#if DEBUG
|
||||
private static bool s_initialized;
|
||||
private static bool s_sceneRunning;
|
||||
#endif
|
||||
|
||||
private static Effect s_textureColorEffect;
|
||||
private static Effect s_batchEffect;
|
||||
|
||||
private static GeometryBinding s_quadGeometry;
|
||||
|
||||
private static Texture2D s_whiteTexture;
|
||||
|
||||
private static GeometryBinding s_batchBinding;
|
||||
private static VertexBuffer s_instanceBuffer;
|
||||
|
||||
private static BatchVertex[] s_rawInstances;
|
||||
private static uint32 s_setInstances;
|
||||
|
||||
private static List<QueueQuad> s_instanceQueue;
|
||||
|
||||
private static DrawOrder s_drawOrder;
|
||||
|
||||
/// The effect that is currently used to draw the sprites.
|
||||
private static Effect s_currentEffect;
|
||||
|
||||
private static void InitEffect()
|
||||
{
|
||||
public Vector2 Position;
|
||||
|
||||
public this(Vector2 position)
|
||||
{
|
||||
Position = position;
|
||||
}
|
||||
|
||||
public this(float x, float y)
|
||||
{
|
||||
Position = .(x, y);
|
||||
}
|
||||
|
||||
public static VertexElement[] VertexElements ~ delete _;
|
||||
|
||||
public static VertexElement[] IVertexData.VertexElements => VertexElements;
|
||||
|
||||
static this()
|
||||
{
|
||||
VertexElements = new VertexElement[]
|
||||
(
|
||||
VertexElement(.R32G32_Float, "POSITION")
|
||||
);
|
||||
}
|
||||
s_textureColorEffect = new Effect(Renderer._context, "content\\Shaders\\textureColor.hlsl");
|
||||
s_batchEffect = new Effect(Renderer._context, "content\\Shaders\\spritebatch.hlsl");
|
||||
}
|
||||
|
||||
[Ordered]
|
||||
struct BatchVertex
|
||||
private static void InitGeometry()
|
||||
{
|
||||
public Matrix Transform;
|
||||
public Color Color;
|
||||
public Vector4 UVTransform;
|
||||
VertexLayout layout = new VertexLayout(Renderer._context, QuadVertex.VertexElements, false, s_textureColorEffect.VertexShader);
|
||||
|
||||
public this(Matrix transform, Color color, Vector4 uvTransform)
|
||||
{
|
||||
Transform = transform;
|
||||
Color = color;
|
||||
UVTransform = uvTransform;
|
||||
}
|
||||
}
|
||||
VertexBuffer quadVertices = new VertexBuffer(Renderer._context, typeof(QuadVertex), 4, .Immutable);
|
||||
|
||||
private GraphicsContext _context ~ _?.ReleaseRef();
|
||||
|
||||
private Vector2 _virtualResolution;
|
||||
|
||||
private Effect quadEffect ~ _?.ReleaseRef();
|
||||
private GeometryBinding quadBinding ~ _?.ReleaseRef();
|
||||
|
||||
private Effect instancingEffect ~ _?.ReleaseRef();
|
||||
private GeometryBinding instancingBinding ~ _?.ReleaseRef();
|
||||
private VertexBuffer instanceBuffer ~ _?.ReleaseRef();
|
||||
|
||||
private Texture2D whiteTexture ~ _.ReleaseRef();
|
||||
|
||||
private BatchVertex[] _rawInstances = new BatchVertex[1024] ~ delete _;
|
||||
private uint32 _setInstances = 0;
|
||||
|
||||
Matrix _projection;
|
||||
|
||||
DrawOrder _drawOrder;
|
||||
|
||||
/// The effect that is currently being used to draw the sprites.
|
||||
private Effect _currentEffect ~ _?.ReleaseRef();
|
||||
|
||||
public this(GraphicsContext context, EffectLibrary effectLibrary)
|
||||
{
|
||||
_context = context..AddRef();
|
||||
Init(effectLibrary);
|
||||
InitInstancing(effectLibrary);
|
||||
}
|
||||
|
||||
void Init(EffectLibrary effectLibrary)
|
||||
{
|
||||
quadEffect = effectLibrary.Load("content\\Shaders\\render2dShader.hlsl", "Renderer2D");
|
||||
|
||||
VertexLayout layout = new VertexLayout(_context, RenderVertex.VertexElements, false, quadEffect.VertexShader);
|
||||
|
||||
VertexBuffer quadVertices = new VertexBuffer(_context, typeof(RenderVertex), 4, .Immutable);
|
||||
|
||||
RenderVertex[4] vertices = .(
|
||||
.(0, 0),
|
||||
.(0, 1),
|
||||
.(1, 1),
|
||||
.(1, 0)
|
||||
QuadVertex[4] vertices = .(
|
||||
.(-0.5f,-0.5f, 0, 1),
|
||||
.(-0.5f, 0.5f, 0, 0),
|
||||
.( 0.5f, 0.5f, 1, 0),
|
||||
.( 0.5f,-0.5f, 1, 1)
|
||||
);
|
||||
|
||||
quadVertices.SetData(vertices);
|
||||
|
||||
IndexBuffer quadIndices = new IndexBuffer(_context, 6, .Immutable);
|
||||
IndexBuffer quadIndices = new IndexBuffer(Renderer._context, 6, .Immutable);
|
||||
|
||||
uint16[6] indices = .(
|
||||
0, 1, 2,
|
||||
@@ -128,194 +135,227 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
quadIndices.SetData(indices);
|
||||
|
||||
quadBinding = new GeometryBinding(_context);
|
||||
quadBinding.SetVertexLayout(layout..ReleaseRefNoDelete());
|
||||
quadBinding.SetPrimitiveTopology(.TriangleList);
|
||||
quadBinding.SetVertexBufferSlot(quadVertices, 0);
|
||||
quadBinding.SetIndexBuffer(quadIndices);
|
||||
s_quadGeometry = new GeometryBinding(Renderer._context);
|
||||
s_quadGeometry.SetVertexLayout(layout..ReleaseRefNoDelete());
|
||||
s_quadGeometry.SetPrimitiveTopology(.TriangleList);
|
||||
s_quadGeometry.SetVertexBufferSlot(quadVertices, 0);
|
||||
s_quadGeometry.SetIndexBuffer(quadIndices);
|
||||
|
||||
quadVertices.ReleaseRef();
|
||||
quadIndices.ReleaseRef();
|
||||
|
||||
Texture2DDesc tex2Ddesc;
|
||||
tex2Ddesc.Format = .R8G8B8A8_UNorm;
|
||||
tex2Ddesc.Width = 1;
|
||||
tex2Ddesc.Height = 1;
|
||||
tex2Ddesc.MipLevels = 1;
|
||||
tex2Ddesc.ArraySize = 1;
|
||||
tex2Ddesc.Usage = .Immutable;
|
||||
tex2Ddesc.CpuAccess = .None;
|
||||
whiteTexture = new Texture2D(_context, tex2Ddesc);
|
||||
|
||||
Color color = .White;
|
||||
whiteTexture.SetData(&color);
|
||||
|
||||
SamplerState sampler = SamplerStateManager.GetSampler(SamplerStateDescription());
|
||||
|
||||
whiteTexture.SamplerState = sampler;
|
||||
|
||||
sampler..ReleaseRef();
|
||||
}
|
||||
|
||||
void InitInstancing(EffectLibrary effectLibrary)
|
||||
private static void InitInstancingGeometry()
|
||||
{
|
||||
instancingEffect = effectLibrary.Load("content\\Shaders\\render2dShaderInst.hlsl", "Renderer2DInstancing");
|
||||
|
||||
instanceBuffer = new VertexBuffer(_context, typeof(BatchVertex), 1024, .Dynamic, .Write);
|
||||
instanceBuffer.SetData(0);
|
||||
s_instanceBuffer = new VertexBuffer(Renderer._context, typeof(BatchVertex), 1024, .Dynamic, .Write);
|
||||
s_instanceBuffer.SetData(0);
|
||||
|
||||
VertexElement[] vertexElements = new .(
|
||||
VertexElement(.R32G32_Float, "POSITION", false, 0, 0, 0, .PerVertexData, 0),
|
||||
VertexElement(.R32G32_Float, "TEXCOORD", false, 0, 0, (.)-1, .PerVertexData, 0),
|
||||
|
||||
VertexElement(.R32G32B32A32_Float, "TRANSFORM", false, 0, 1, (.)-1, .PerInstanceData, 1),
|
||||
VertexElement(.R32G32B32A32_Float, "TRANSFORM", false, 1, 1, (.)-1, .PerInstanceData, 1),
|
||||
VertexElement(.R32G32B32A32_Float, "TRANSFORM", false, 2, 1, (.)-1, .PerInstanceData, 1),
|
||||
VertexElement(.R32G32B32A32_Float, "TRANSFORM", false, 3, 1, (.)-1, .PerInstanceData, 1),
|
||||
VertexElement( .R8G8B8A8_UNorm, "COLOR", false, 0, 1, (.)-1, .PerInstanceData, 1),
|
||||
VertexElement(.R32G32B32A32_Float, "TEXCOORD", false, 0, 1, (.)-1, .PerInstanceData, 1)
|
||||
VertexElement(.R32G32B32A32_Float, "COLOR", false, 0, 1, (.)-1, .PerInstanceData, 1),
|
||||
VertexElement(.R32G32B32A32_Float, "TEXCOORD", false, 1, 1, (.)-1, .PerInstanceData, 1)
|
||||
);
|
||||
|
||||
VertexLayout instancingLayout = new VertexLayout(_context, vertexElements, true, instancingEffect.VertexShader);
|
||||
VertexLayout batchLayout = new VertexLayout(Renderer._context, vertexElements, true, s_batchEffect.VertexShader);
|
||||
|
||||
instancingBinding = new GeometryBinding(_context);
|
||||
instancingBinding.SetVertexLayout(instancingLayout..ReleaseRefNoDelete());
|
||||
instancingBinding.SetPrimitiveTopology(.TriangleList);
|
||||
instancingBinding.SetVertexBufferSlot(quadBinding.GetVertexBuffer(0), 0);
|
||||
instancingBinding.SetVertexBufferSlot(instanceBuffer, 1);
|
||||
instancingBinding.SetIndexBuffer(quadBinding.GetIndexBuffer(), 0);
|
||||
s_batchBinding = new GeometryBinding(Renderer._context);
|
||||
s_batchBinding.SetVertexLayout(batchLayout..ReleaseRefNoDelete());
|
||||
s_batchBinding.SetPrimitiveTopology(.TriangleList);
|
||||
|
||||
s_batchBinding.SetVertexBufferSlot(s_quadGeometry.GetVertexBuffer(0), 0);
|
||||
s_batchBinding.SetIndexBuffer(s_quadGeometry.GetIndexBuffer(), 0);
|
||||
|
||||
s_batchBinding.SetVertexBufferSlot(s_instanceBuffer, 1);
|
||||
|
||||
s_rawInstances = new BatchVertex[1024];
|
||||
s_setInstances = 0;
|
||||
|
||||
s_instanceQueue = new List<QueueQuad>(1024);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the renderer.
|
||||
* @param drawOrder Determines if and how the sprites will be ordered before rendering.
|
||||
* @param virtualResolution The virtual resolution used for rendering.
|
||||
* Can be used to draw resolution independent stuff. // TODO: find a better explanation
|
||||
*/ // TODO: RenderMode (Immediate, Deferred)
|
||||
public void Begin(DrawOrder drawOrder = .SortByTexture, Vector2 virtualResolution = .Zero, float maxDepth = 100, Effect effect = null)
|
||||
private static void InitWhitetexture()
|
||||
{
|
||||
_drawOrder = drawOrder;
|
||||
// Create a texture with a single white pixel
|
||||
Texture2DDesc tex2Ddesc = .{
|
||||
Format = .R8G8B8A8_UNorm,
|
||||
Width = 1,
|
||||
Height = 1,
|
||||
MipLevels = 1,
|
||||
ArraySize = 1,
|
||||
Usage = .Immutable,
|
||||
CpuAccess = .None
|
||||
};
|
||||
s_whiteTexture = new Texture2D(Renderer._context, tex2Ddesc);
|
||||
|
||||
_virtualResolution = virtualResolution;
|
||||
Color color = .White;
|
||||
s_whiteTexture.SetData(&color);
|
||||
|
||||
if(virtualResolution == .Zero)
|
||||
{
|
||||
_virtualResolution = .(_context.SwapChain.BackbufferViewport.Width, _context.SwapChain.BackbufferViewport.Height);
|
||||
}
|
||||
// Create the default sampler for the white texture
|
||||
SamplerState sampler = SamplerStateManager.GetSampler(SamplerStateDescription());
|
||||
s_whiteTexture.SamplerState = sampler;
|
||||
|
||||
sampler.ReleaseRef();
|
||||
}
|
||||
|
||||
public static void Init()
|
||||
{
|
||||
InitEffect();
|
||||
InitGeometry();
|
||||
InitInstancingGeometry();
|
||||
InitWhitetexture();
|
||||
|
||||
FontRenderer.Init();
|
||||
|
||||
#if DEBUG
|
||||
s_initialized = true;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void Deinit()
|
||||
{
|
||||
Log.EngineLogger.AssertDebug(s_initialized, "Renderer2D was not initialized.");
|
||||
|
||||
FontRenderer.Deinit();
|
||||
|
||||
s_textureColorEffect.ReleaseRef();
|
||||
s_batchEffect.ReleaseRef();
|
||||
|
||||
s_quadGeometry.ReleaseRef();
|
||||
|
||||
s_whiteTexture.ReleaseRef();
|
||||
|
||||
_projection = .(2.0f / _virtualResolution.X, 0, 0, 0,
|
||||
0, -2.0f / _virtualResolution.Y, 0, 0,
|
||||
0, 0, 1.0f / maxDepth, 0,
|
||||
-1, 1, 0, 1);
|
||||
s_batchBinding.ReleaseRef();
|
||||
s_instanceBuffer.ReleaseRef();
|
||||
|
||||
delete s_rawInstances;
|
||||
delete s_instanceQueue;
|
||||
|
||||
s_currentEffect?.ReleaseRef();
|
||||
|
||||
#if DEBUG
|
||||
s_initialized = false;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void BeginScene(OrthographicCamera camera, DrawOrder drawOrder = .SortByTexture, Effect effect = null)
|
||||
{
|
||||
Log.EngineLogger.AssertDebug(s_initialized, "Renderer2D was not initialized.");
|
||||
Log.EngineLogger.AssertDebug(!s_sceneRunning, "You have to call EndScene before you can make another call to BeginScene.");
|
||||
|
||||
//s_textureColorEffect.Bind(Renderer._context);
|
||||
|
||||
if(effect != null)
|
||||
{
|
||||
_currentEffect?.ReleaseRef();
|
||||
_currentEffect = effect..AddRef();
|
||||
s_currentEffect?.ReleaseRef();
|
||||
s_currentEffect = effect..AddRef();
|
||||
}
|
||||
else
|
||||
{
|
||||
_currentEffect = instancingEffect..AddRef();
|
||||
s_currentEffect = s_batchEffect..AddRef();
|
||||
}
|
||||
|
||||
s_currentEffect.Variables["ViewProjection"].SetData(camera.ViewProjection);
|
||||
s_textureColorEffect.Variables["ViewProjection"].SetData(camera.ViewProjection);
|
||||
|
||||
s_drawOrder = drawOrder;
|
||||
|
||||
#if DEBUG
|
||||
s_sceneRunning = true;
|
||||
#endif
|
||||
}
|
||||
|
||||
struct QueuedQuad: this(Matrix Transform, Color Color, Texture2D Texture, float Depth, Vector4 uvTransform) { }
|
||||
|
||||
List<QueuedQuad> _quads = new .(128) ~ delete _;
|
||||
|
||||
public void Draw(Texture2D texture, float x, float y, float width, float height, Color color = .White, float depth = 0.0f, Vector4 uvTransform = .(0, 0, 1, 1))
|
||||
public static void EndScene()
|
||||
{
|
||||
Matrix transform = .(width, 0, 0, 0,
|
||||
0, height, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
x, y, depth, 1) * _projection;
|
||||
Log.EngineLogger.AssertDebug(s_sceneRunning, "Missing call of BeginScene.");
|
||||
|
||||
if(_drawOrder == .Immediate)
|
||||
{
|
||||
/*
|
||||
texture.Bind();
|
||||
|
||||
quadEffect.Variables["World"].SetData(transform);
|
||||
quadEffect.Variables["Color"].SetData(color);
|
||||
quadEffect.Variables["HasTexture"].SetData(true);
|
||||
quadEffect.Bind(_context);
|
||||
|
||||
quadBinding.Bind(_context);
|
||||
RenderCommand.DrawIndexed(quadBinding);
|
||||
*/
|
||||
_quads.Add(.(transform, color, texture ?? whiteTexture, depth, uvTransform));
|
||||
Flush();
|
||||
#if DEBUG
|
||||
s_sceneRunning = false;
|
||||
#endif
|
||||
}
|
||||
|
||||
public static void Flush()
|
||||
{
|
||||
Log.EngineLogger.AssertDebug(s_sceneRunning, "Missing call of BeginScene.");
|
||||
|
||||
if(s_drawOrder != .Immediate)
|
||||
DrawDeferred();
|
||||
}
|
||||
else
|
||||
{
|
||||
_quads.Add(.(transform, color, texture ?? whiteTexture, depth, uvTransform));
|
||||
}
|
||||
}
|
||||
|
||||
public void End()
|
||||
/// Adds a quad instance to the instance queue.
|
||||
[Inline]
|
||||
private static void QueueQuadInstance(Matrix transform, ColorRGBA color, Texture2D texture, float depth, Vector4 uvTransform)
|
||||
{
|
||||
if(_drawOrder != .Immediate)
|
||||
{
|
||||
DrawDeferred();
|
||||
}
|
||||
s_instanceQueue.Add(QueueQuad(transform, color, texture ?? s_whiteTexture, depth, uvTransform));
|
||||
}
|
||||
|
||||
void FlushInstances()
|
||||
|
||||
private static void FlushInstances()
|
||||
{
|
||||
if(_setInstances == 0)
|
||||
if(s_setInstances == 0)
|
||||
return;
|
||||
|
||||
instanceBuffer.SetData<BatchVertex>(_rawInstances.Ptr, _setInstances, 0, .WriteDiscard);
|
||||
s_instanceBuffer.SetData<BatchVertex>(s_rawInstances.Ptr, s_setInstances, 0, .WriteDiscard);
|
||||
|
||||
_currentEffect.Bind(_context);
|
||||
instancingBinding.InstanceCount = _setInstances;
|
||||
instancingBinding.Bind(_context);
|
||||
RenderCommand.DrawIndexedInstanced(instancingBinding);
|
||||
s_currentEffect.Bind(Renderer._context);
|
||||
s_batchBinding.InstanceCount = s_setInstances;
|
||||
s_batchBinding.Bind(Renderer._context);
|
||||
RenderCommand.DrawIndexedInstanced(s_batchBinding);
|
||||
|
||||
_setInstances = 0;
|
||||
s_setInstances = 0;
|
||||
}
|
||||
|
||||
int TextureComparison(QueuedQuad lhs, QueuedQuad rhs)
|
||||
private static int TextureComparison(QueueQuad lhs, QueueQuad rhs)
|
||||
{
|
||||
return (int)Internal.UnsafeCastToPtr(lhs.Texture) - (int)Internal.UnsafeCastToPtr(rhs.Texture);
|
||||
}
|
||||
int FrontToBackComparison(QueuedQuad lhs, QueuedQuad rhs)
|
||||
private static int BackToFrontComparison(QueueQuad lhs, QueueQuad rhs)
|
||||
{
|
||||
return rhs.Depth <=> lhs.Depth;
|
||||
}
|
||||
int BackToFrontComparison(QueuedQuad lhs, QueuedQuad rhs)
|
||||
private static int FrontToBackComparison(QueueQuad lhs, QueueQuad rhs)
|
||||
{
|
||||
return lhs.Depth <=> rhs.Depth;
|
||||
}
|
||||
|
||||
private void SortQuads()
|
||||
private static void SortQuads()
|
||||
{
|
||||
switch(_drawOrder)
|
||||
s_instanceQueue.Sort(scope => TextureComparison);
|
||||
|
||||
switch(s_drawOrder)
|
||||
{
|
||||
case .SortByTexture:
|
||||
_quads.Sort(scope => TextureComparison);
|
||||
s_instanceQueue.Sort(scope => TextureComparison);
|
||||
case .BackToFront:
|
||||
_quads.Sort(scope => BackToFrontComparison);
|
||||
s_instanceQueue.Sort(scope => BackToFrontComparison);
|
||||
case .FrontToBack:
|
||||
_quads.Sort(scope => FrontToBackComparison);
|
||||
s_instanceQueue.Sort(scope => FrontToBackComparison);
|
||||
case .Immediate:
|
||||
default:
|
||||
Log.EngineLogger.Error("Unknown instance draw order.");
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawDeferred()
|
||||
private static void DrawDeferred()
|
||||
{
|
||||
if(_quads.Count == 0)
|
||||
if(s_instanceQueue.IsEmpty)
|
||||
return;
|
||||
|
||||
SortQuads();
|
||||
|
||||
Texture2D texture = _quads[0].Texture;
|
||||
_currentEffect.SetTexture("Texture", texture);
|
||||
Texture2D texture = s_instanceQueue[0].Texture;
|
||||
s_currentEffect.SetTexture("Texture", texture);
|
||||
|
||||
_setInstances = 0;
|
||||
s_setInstances = 0;
|
||||
|
||||
for(int i < _quads.Count)
|
||||
for(int i < s_instanceQueue.Count)
|
||||
{
|
||||
var quad = ref _quads[i];
|
||||
var quad = ref s_instanceQueue[i];
|
||||
|
||||
// flush every time the texture changes
|
||||
if(quad.Texture != texture)
|
||||
@@ -323,12 +363,12 @@ namespace GlitchyEngine.Renderer
|
||||
FlushInstances();
|
||||
|
||||
texture = quad.Texture;
|
||||
_currentEffect.SetTexture("Texture", texture);
|
||||
s_currentEffect.SetTexture("Texture", texture);
|
||||
}
|
||||
|
||||
_rawInstances[_setInstances++] = .(quad.Transform, quad.Color, quad.uvTransform);
|
||||
s_rawInstances[s_setInstances++] = .(quad.Transform, quad.Color, quad.uvTransform);
|
||||
|
||||
if(_setInstances == _rawInstances.Count)
|
||||
if(s_setInstances == s_rawInstances.Count)
|
||||
{
|
||||
FlushInstances();
|
||||
}
|
||||
@@ -336,9 +376,78 @@ namespace GlitchyEngine.Renderer
|
||||
|
||||
FlushInstances();
|
||||
|
||||
_quads.Clear();
|
||||
s_instanceQueue.Clear();
|
||||
}
|
||||
|
||||
Debug.Assert(_quads.Count == 0);
|
||||
private static void DrawImmediate(Matrix transform, ColorRGBA color, Texture2D texture, Vector4 uvTransform)
|
||||
{
|
||||
s_textureColorEffect.SetTexture("Texture", texture ?? s_whiteTexture);
|
||||
|
||||
s_textureColorEffect.Variables["World"].SetData(transform);
|
||||
s_textureColorEffect.Variables["Color"].SetData(color);
|
||||
s_textureColorEffect.Variables["UVTransform"].SetData(uvTransform);
|
||||
|
||||
s_textureColorEffect.Bind(Renderer._context);
|
||||
|
||||
s_quadGeometry.Bind(Renderer._context);
|
||||
RenderCommand.DrawIndexed(s_quadGeometry);
|
||||
}
|
||||
|
||||
// Primitives
|
||||
|
||||
public static void DrawQuad(Vector2 position, Vector2 size, float rotation, ColorRGBA color)
|
||||
{
|
||||
DrawQuad(Vector3(position, 0.0f), size, rotation, s_whiteTexture, color);
|
||||
}
|
||||
|
||||
/// Like DrawQuad but the pivot point is the top left corner
|
||||
public static void DrawQuadPivotCorner(Vector2 position, Vector2 size, float rotation, ColorRGBA color)
|
||||
{
|
||||
DrawQuadPivotCorner(Vector3(position, 0.0f), size, rotation, s_whiteTexture, color);
|
||||
}
|
||||
|
||||
public static void DrawQuad(Vector3 position, Vector2 size, float rotation, ColorRGBA color)
|
||||
{
|
||||
DrawQuad(position, size, rotation, s_whiteTexture, color);
|
||||
}
|
||||
|
||||
public static void DrawQuadPivotCorner(Vector3 position, Vector2 size, float rotation, ColorRGBA color)
|
||||
{
|
||||
DrawQuadPivotCorner(position, size, rotation, s_whiteTexture, color);
|
||||
}
|
||||
|
||||
public static void DrawQuad(Vector2 position, Vector2 size, float rotation, Texture2D texture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1))
|
||||
{
|
||||
DrawQuad(Vector3(position, 0.0f), size, rotation, texture, color, uvTransform);
|
||||
}
|
||||
|
||||
public static void DrawQuadPivotCorner(Vector2 position, Vector2 size, float rotation, Texture2D texture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1))
|
||||
{
|
||||
DrawQuadPivotCorner(Vector3(position, 0.0f), size, rotation, texture, color, uvTransform);
|
||||
}
|
||||
|
||||
public static void DrawQuad(Vector3 position, Vector2 size, float rotation, Texture2D texture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1))
|
||||
{
|
||||
Log.EngineLogger.AssertDebug(s_sceneRunning, "Missing call of BeginScene.");
|
||||
|
||||
Matrix transform = Matrix.Translation(position) * Matrix.RotationZ(rotation) * Matrix.Scaling(size.X, size.Y, 1.0f);
|
||||
|
||||
if(s_drawOrder == .Immediate)
|
||||
{
|
||||
//DrawImmediate(transform, color, texture, uvTransform);
|
||||
QueueQuadInstance(transform, color, texture, position.Z, uvTransform);
|
||||
DrawDeferred();
|
||||
//Flush();
|
||||
}
|
||||
else
|
||||
{
|
||||
QueueQuadInstance(transform, color, texture, position.Z, uvTransform);
|
||||
}
|
||||
}
|
||||
|
||||
public static void DrawQuadPivotCorner(Vector3 position, Vector2 size, float rotation, Texture2D texture, ColorRGBA color = .White, Vector4 uvTransform = .(0, 0, 1, 1))
|
||||
{
|
||||
DrawQuad(position + Vector3(size.X / 2, size.Y / -2, 0), size, rotation, texture, color, uvTransform);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,20 +107,21 @@ namespace GlitchyEngine.Renderer.Text
|
||||
|
||||
public this(GraphicsContext context, String fontPath, uint32 fontSize, bool hasColor = true, char32 firstChar = '\0', uint32 charCount = 128, int32 faceIndex = 0)
|
||||
{
|
||||
// Make sure the fontrenderer is initialized (Font only cares about freetype)
|
||||
FontRenderer.Init();
|
||||
|
||||
_context = context..AddRef();
|
||||
|
||||
// Set default sampler
|
||||
Sampler = null;
|
||||
|
||||
FontRenderer.InitLibrary();
|
||||
|
||||
_glyphs.Add('\0', new GlyphDescriptor(){Font = this});
|
||||
|
||||
_fontSize = fontSize;
|
||||
_faceIndex = faceIndex;
|
||||
_hasColor = hasColor;
|
||||
|
||||
var res = FreeType.New_Face(FontRenderer.Library, fontPath, faceIndex, &_face);
|
||||
var res = FreeType.New_Face(FontRenderer.s_Library, fontPath, faceIndex, &_face);
|
||||
Log.EngineLogger.Assert(res.Success, scope $"New_Face failed({(int)res}): {res}");
|
||||
|
||||
res = FreeType.Set_Pixel_Sizes(_face, 0, _fontSize);
|
||||
@@ -408,11 +409,8 @@ namespace GlitchyEngine.Renderer.Text
|
||||
desc.TranslationX = translationX;
|
||||
desc.TranslationY = translationY;
|
||||
|
||||
//desc.AdjustToBaseLine = (float)(- height + _range + (_face.glyph.metrics.height / 64 - _face.glyph.metrics.horiBearingY / 64) * _geometryScaler);
|
||||
//(float)(- height + translationY * _geometryScaler + (_face.glyph.metrics.height / 64 - _face.glyph.metrics.horiBearingY / 64) * _geometryScaler);
|
||||
desc.AdjustToBaseLine = (float)(-height + translationY * _geometryScaler);
|
||||
desc.AdjustToBaseLine = (float)(-translationY * _geometryScaler);
|
||||
|
||||
//desc.AdjustToPen = (float)(- _range + (_face.glyph.metrics.horiBearingX / 64) * _geometryScaler);
|
||||
desc.AdjustToPen = (float)(-translationX);
|
||||
|
||||
return true;
|
||||
|
||||
@@ -5,41 +5,51 @@ using GlitchyEngine.Math;
|
||||
using System.Collections;
|
||||
|
||||
using internal GlitchyEngine.Renderer.Text;
|
||||
using internal GlitchyEngine.Renderer;
|
||||
|
||||
namespace GlitchyEngine.Renderer.Text
|
||||
{
|
||||
public static class FontRenderer
|
||||
{
|
||||
internal static FT_Library Library ~ FreeType.Done_FreeType(_);
|
||||
|
||||
internal static void InitLibrary()
|
||||
{
|
||||
if(Library == null)
|
||||
{
|
||||
var res = FreeType.Init_FreeType(&Library);
|
||||
Log.EngineLogger.Assert(res.Success, scope $"Init_FreeType failed({(int)res}): {res}");
|
||||
}
|
||||
}
|
||||
internal static FT_Library s_Library;
|
||||
|
||||
public static Effect _msdfEffect;
|
||||
|
||||
static this()
|
||||
internal static bool s_isInitialized;
|
||||
|
||||
internal static void Init()
|
||||
{
|
||||
FontRenderer.InitLibrary();
|
||||
if(s_isInitialized)
|
||||
return;
|
||||
|
||||
InitFreetype();
|
||||
|
||||
_msdfEffect = new Effect(Renderer._context, "content\\Shaders\\msdfShader.hlsl");
|
||||
|
||||
s_isInitialized = true;
|
||||
}
|
||||
|
||||
public static void Init(EffectLibrary effectLibrary)
|
||||
internal static void Deinit()
|
||||
{
|
||||
if(effectLibrary.Exists("msdfShader"))
|
||||
_msdfEffect = effectLibrary.Get("msdfShader");
|
||||
else
|
||||
_msdfEffect = effectLibrary.Load("content\\Shaders\\msdfShader.hlsl");
|
||||
_msdfEffect.ReleaseRef();
|
||||
|
||||
DeinitFreetype();
|
||||
|
||||
s_isInitialized = false;
|
||||
}
|
||||
|
||||
private static void InitFreetype()
|
||||
{
|
||||
if(s_Library == null)
|
||||
{
|
||||
var res = FreeType.Init_FreeType(&s_Library);
|
||||
Log.EngineLogger.Assert(res.Success, scope $"Failed to initialize freetype ({(int)res}): {res}");
|
||||
}
|
||||
}
|
||||
|
||||
public static void DeInit()
|
||||
private static void DeinitFreetype()
|
||||
{
|
||||
_msdfEffect?.ReleaseRef();
|
||||
_msdfEffect = null;
|
||||
FreeType.Done_FreeType(s_Library);
|
||||
}
|
||||
|
||||
/** @brief Draws a given text using a specified font stack and renderer.
|
||||
@@ -52,24 +62,33 @@ namespace GlitchyEngine.Renderer.Text
|
||||
* @param bitmapColor The (default) color used for glyphs that are bitmaps (e.g. emojies, if the font provies them as bitmap).
|
||||
* @param lineGapOffset Can be used to manually increase or decrease the gap between lines.
|
||||
*/
|
||||
public static void DrawText(Renderer2D renderer, Font font, String text, float x, float y, float fontSize, Color fontColor = .White, Color bitmapColor = .White, float lineGapOffset = 0)
|
||||
public static void DrawText(Font font, String text, float x, float y, float fontSize, Color fontColor = .White, Color bitmapColor = .White, float lineGapOffset = 0)
|
||||
{
|
||||
if(text.IsWhiteSpace)
|
||||
return;
|
||||
|
||||
renderer.End();
|
||||
var lastEffect = renderer.[Friend]_currentEffect;
|
||||
renderer.[Friend]_currentEffect = _msdfEffect..AddRef();
|
||||
|
||||
Renderer2D.Flush();
|
||||
|
||||
// TODO: this is very not good!
|
||||
var lastEffect = Renderer2D.[Friend]s_currentEffect;
|
||||
Renderer2D.[Friend]s_currentEffect = _msdfEffect..AddRef();
|
||||
// TODO: oh no....
|
||||
// Copy viewProjection from current effect
|
||||
Matrix viewProjection = lastEffect.Variables["ViewProjection"].[Friend]GetData<Matrix>();
|
||||
_msdfEffect.Variables["ViewProjection"].SetData(viewProjection);
|
||||
|
||||
float scale = (float)fontSize / (float)font._fontSize;
|
||||
|
||||
|
||||
_msdfEffect.Variables["screenPixelRange"].SetData(scale * 4.0f);
|
||||
|
||||
Vector2 unitRange = Vector2((float)font._range) / Vector2(font._atlas.Width, font._atlas.Height);
|
||||
_msdfEffect.Variables["UnitRange"].SetData(unitRange);
|
||||
|
||||
// Space between two baselines
|
||||
float linespace = (((font._face.size.metrics.ascender - font._face.size.metrics.descender) / 64) + lineGapOffset) * scale;
|
||||
|
||||
// The line we are writing on
|
||||
float baseline = y + linespace;
|
||||
float baseline = y;// + linespace;
|
||||
|
||||
//renderer.Draw(null, x, baseline, 10000, 1, .Blue);
|
||||
|
||||
@@ -80,7 +99,6 @@ namespace GlitchyEngine.Renderer.Text
|
||||
int movedLines = 0;
|
||||
|
||||
List<Texture2D> atlasses = scope .();
|
||||
|
||||
// We render every char with a slightly greater depth, so that the chars don't cull each other.
|
||||
// In order to do that we increment an integer for each glyph and use it as the depth for the next one.
|
||||
// Whenn passing the depth to the renderer we treat the integer as the binary representation of a float.
|
||||
@@ -88,8 +106,8 @@ namespace GlitchyEngine.Renderer.Text
|
||||
// smallest possible increase a float can represent.
|
||||
// Note: This might do funky stuff when objects are very close to each other. But realistically whe have to do
|
||||
// about 8 million (2^23) increments to reach a depth of 1 so I think it's safe enough.
|
||||
float f = 1.0f;
|
||||
int32 depthInt = *(int32*)&f;
|
||||
//float f = 1.0f;
|
||||
//int32 depthInt = *(int32*)&f;
|
||||
|
||||
// enumerate through the unicode codepoints
|
||||
for(char32 char in text.DecodedChars)
|
||||
@@ -104,7 +122,7 @@ namespace GlitchyEngine.Renderer.Text
|
||||
if(movedLines != 0)
|
||||
{
|
||||
// move baseline
|
||||
baseline += linespace * movedLines;
|
||||
baseline -= linespace * movedLines;
|
||||
|
||||
// TODO: make carriage return optional?
|
||||
// return pen to start of line
|
||||
@@ -155,18 +173,22 @@ namespace GlitchyEngine.Renderer.Text
|
||||
|
||||
texRect /= Vector4(atlasSize, atlasSize);
|
||||
|
||||
renderer.Draw(atlas, viewportRect.X, viewportRect.Y, viewportRect.Z, viewportRect.W, glyphColor, *(float*)(&depthInt), texRect);
|
||||
Renderer2D.DrawQuad(Vector2(viewportRect.X + viewportRect.Z / 2, viewportRect.Y + viewportRect.W / 2), .(viewportRect.Z, viewportRect.W), 0, atlas, glyphColor, texRect);
|
||||
|
||||
//renderer.Draw(atlas, viewportRect.X, viewportRect.Y, viewportRect.Z, viewportRect.W, glyphColor, *(float*)(&depthInt), texRect);
|
||||
|
||||
penPosition += glyphDesc.Advance * glyphFontScale;
|
||||
|
||||
// Increase depth for the next glyph
|
||||
depthInt--;
|
||||
//depthInt--;
|
||||
}
|
||||
|
||||
renderer.End();
|
||||
Renderer2D.Flush();
|
||||
|
||||
// TODO: not good!
|
||||
// Change back effect
|
||||
_msdfEffect.ReleaseRef();
|
||||
renderer.[Friend]_currentEffect = lastEffect;
|
||||
Renderer2D.[Friend]s_currentEffect = lastEffect;
|
||||
|
||||
// release all atlas textures
|
||||
for(int i < atlasses.Count)
|
||||
|
||||
Reference in New Issue
Block a user