Support materials in 2D Quad Renderer!

- Lost Circle rendering along the way
- Cleaned up 2D shaders
This commit is contained in:
Simon Lübeß
2025-02-11 14:02:16 +01:00
parent 76087d6ba4
commit 06b981e88a
9 changed files with 223 additions and 188 deletions
@@ -0,0 +1,74 @@
#pragma EngineBuffer[ Name = "SceneConstants"; Binding = "Scene" ]
/**
* Contains constants that apply to the current scene.
*/
cbuffer SceneConstants : register(b0)
{
/**
* View projection matrix of the active camera.
*/
float4x4 ViewProjection;
}
/**
* Contains the data provided by the engine for a 2D vertex shader.
*/
struct VS_Input
{
// Geometry
/**
* Vertex position
*/
float2 Position : POSITION;
/**
* Vertex texture coordinate
*/
float2 Texcoord : TEXCOORD0;
// Instance data
/**
* model to world transform matrix of the current quad.
*/
float4x4 Transform : TRANSFORM;
/**
* Color of the current quad.
*/
float4 Color : COLOR;
/**
* model to world transform matrix of the current quad.
*/
float4 UVTransform : TEXCOORD1;
#ifdef EDITOR
/**
* The ECS ID of the entity associated with the current quad.
*/
uint EntityId : ENTITYID;
#endif
};
/**
* Contains the output data of the vertex shader that will be passed into the pixel shader.
*/
typedef struct PS_Input
{
/**
* The screen space position of the current vertex.
*/
float4 Position : SV_Position;
/**
* The transformed texture coordinates of the current vertex.
*/
float2 Texcoord : TEXCOORD;
/**
* The color of the current vertex.
*/
float4 Color : COLOR;
#ifdef EDITOR
/**
* The ECS ID of the entity associated with the current quad.
* This should always just be passed through.
*/
nointerpolation uint EntityId : ENTITYID;
#endif
} VS_Output;
@@ -0,0 +1,11 @@
{
AssetLoader = null,
Config = null,
Importer = "ShaderImporter",
ImporterConfig = null,
Processor = null,
ProcessorConfig = null,
Exporter = null,
ExporterConfig = null,
AssetHandle = 9184910549692735900
}
@@ -87,4 +87,15 @@ float GetBitangentHandedness(float tangentz)
return (z & 1) > 0 ? 1.0 : -1.0; return (z & 1) > 0 ? 1.0 : -1.0;
} }
/**
* Transforms the given texture coordinates using the provided UV transform.
* @param texcoords The texture coordinates to transform.
* @param uvTransform the UV transform. X, Y: UV offset | Z, W: UV scaling.
* @return The transformed uv coordinates.
*/
float2 TransformTexcoords(float2 texcoords, float4 uvTransform)
{
return uvTransform.xy + uvTransform.zw * texcoords;
}
#endif // __SHADER_HELPERS_HLSL__ #endif // __SHADER_HELPERS_HLSL__
+32 -61
View File
@@ -1,61 +1,31 @@
#define EDITOR
#include "GlitchyEngine2D.hlsl"
#include "ShaderHelpers.hlsl"
Texture2D<float3> Texture : register(t0); Texture2D<float3> Texture : register(t0);
SamplerState Sampler : register(s0); SamplerState Sampler : register(s0);
cbuffer Constants cbuffer Material
{ {
float4x4 ViewProjection;
float2 UnitRange; float2 UnitRange;
float screenPixelRange = 2;
} }
struct VS_Input VS_Output VS(VS_Input input)
{ {
float2 Position : POSITION; VS_Output output;
float2 Texcoord : TEXCOORD0;
float4x4 Tranform : TRANSFORM;
float4 Color : COLOR;
float4 UVTransform : TEXCOORD1;
};
struct PS_Input output.Position = mul(ViewProjection, mul(input.Transform, float4(input.Position, 0.0f, 1.0f)));
{ output.Texcoord = TransformTexcoords(input.Texcoord, input.UVTransform);
float4 Position : SV_Position;
float2 TexCoord : TEXCOORD0;
float4 Color : COLOR;
};
PS_Input VS(VS_Input input)
{
PS_Input output;
output.Position = mul(ViewProjection, mul(input.Tranform, float4(input.Position, 0.0f, 1.0f)));
output.TexCoord = input.UVTransform.xy + input.UVTransform.zw * input.Texcoord;
output.Color = input.Color; output.Color = input.Color;
#ifdef EDITOR
output.EntityId = input.EntityId;
#endif
return output; return output;
} }
/*
in vec2 texCoord;
out vec4 color;
uniform sampler2D msdf;
uniform vec4 bgColor;
uniform vec4 fgColor;
float median(float r, float g, float b) {
return max(min(r, g), min(max(r, g), b));
}
void main() {
vec3 msd = texture(msdf, texCoord).rgb;
float sd = median(msd.r, msd.g, msd.b);
float screenPxDistance = screenPxRange()*(sd - 0.5);
float opacity = clamp(screenPxDistance + 0.5, 0.0, 1.0);
color = mix(bgColor, fgColor, opacity);
}
*/
float median(float r, float g, float b) float median(float r, float g, float b)
{ {
return max(min(r, g), min(max(r, g), b)); return max(min(r, g), min(max(r, g), b));
@@ -67,31 +37,32 @@ float ScreenPxRange(float2 texcoord)
return max(0.5f * dot(UnitRange, screenTexSize), 1.0f); return max(0.5f * dot(UnitRange, screenTexSize), 1.0f);
} }
float4 PS(PS_Input input) : SV_Target0 struct PS_Output
{ {
float3 msd = Texture.Sample(Sampler, input.TexCoord); float4 Color : SV_Target0;
#ifdef EDITOR
uint EntityId : SV_TARGET1;
#endif
};
PS_Output PS(PS_Input input)
{
PS_Output output;
float3 msd = Texture.Sample(Sampler, input.Texcoord);
float sd = median(msd.r, msd.g, msd.b); float sd = median(msd.r, msd.g, msd.b);
float screenPxDistance = ScreenPxRange(input.TexCoord) * (sd - 0.5); float screenPxDistance = ScreenPxRange(input.Texcoord) * (sd - 0.5);
float opacity = clamp(screenPxDistance + 0.5, 0.0, 1.0); float opacity = clamp(screenPxDistance + 0.5, 0.0, 1.0);
clip(opacity - 0.001f); clip(opacity - 0.001f);
//return float4(input.Color.rgb, opacity * input.Color.a) * 0.0001f + float4(opacity.xxxx); output.Color = float4(input.Color.rgb, input.Color.a * opacity);
return input.Color * opacity; // TODO: This is not quite right // float4(input.Color.rgb, input.Color.a * opacity);
//return float4(input.Color.rgb, opacity * input.Color.a) * 0.0001f + float4(msd, 1.0f);
}
/* #ifdef EDITOR
// 2D output.EntityId = input.EntityId;
float4 PS(PS_Input input) : SV_Target0 #endif
{
float3 msd = Texture.Sample(Sampler, input.TexCoord).rgb;
float sd = median(msd.r, msd.g, msd.b);
float screenPxDistance = screenPixelRange*(sd - 0.5);
float opacity = clamp(screenPxDistance + 0.5, 0.0, 1.0);
return float4(input.Color.rgb, opacity * input.Color.a); return output;
} }
*/
#pragma Effect[VS=VS; PS=PS] #pragma Effect[VS=VS; PS=PS]
@@ -1,41 +1,17 @@
#define EDITOR #define EDITOR
#include "GlitchyEngine2D.hlsl"
#include "ShaderHelpers.hlsl"
Texture2D Texture : register(t0); Texture2D Texture : register(t0);
SamplerState Sampler : register(s0); SamplerState Sampler : register(s0);
cbuffer Constants : register(b0) VS_Output VS(VS_Input input)
{ {
float4x4 ViewProjection; VS_Output output;
};
struct VS_Input
{
float2 Position : POSITION;
float2 Texcoord : TEXCOORD0;
float4x4 Transform : TRANSFORM;
float4 Color : COLOR;
float4 UVTransform : TEXCOORD1;
#ifdef EDITOR
uint EntityId : ENTITYID;
#endif
};
struct PS_Input
{
float4 Position : SV_Position;
float2 Texcoord : TEXCOORD;
float4 Color : COLOR;
#ifdef EDITOR
nointerpolation uint EntityId : ENTITYID;
#endif
};
PS_Input VS(VS_Input input)
{
PS_Input output;
output.Position = mul(ViewProjection, mul(input.Transform, float4(input.Position, 0.0f, 1.0f))); output.Position = mul(ViewProjection, mul(input.Transform, float4(input.Position, 0.0f, 1.0f)));
output.Texcoord = input.UVTransform.xy + input.UVTransform.zw * input.Texcoord; output.Texcoord = TransformTexcoords(input.Texcoord, input.UVTransform);
output.Color = input.Color; output.Color = input.Color;
// Premultiply Alpha // Premultiply Alpha
+2 -2
View File
@@ -514,7 +514,7 @@ namespace GlitchyEditor
Matrix world = Billboard(transform.WorldTransform); Matrix world = Billboard(transform.WorldTransform);
float alpha = CalculateAlpha(transform.WorldTransform.Translation); float alpha = CalculateAlpha(transform.WorldTransform.Translation);
Renderer2D.DrawQuad(world, _editorIcons.Camera, ColorRGBA(alpha, alpha, alpha, alpha), .(0, 0, 1, 1), entity.Index); Renderer2D.DrawQuad(world, _editorIcons.Camera, null, ColorRGBA(alpha, alpha, alpha, alpha), .(0, 0, 1, 1), entity.Index);
//Renderer2D.DrawQuad(world, _iconCamera, .White, .(0, 0, 1, 1), entity.Index); //Renderer2D.DrawQuad(world, _iconCamera, .White, .(0, 0, 1, 1), entity.Index);
} }
@@ -535,7 +535,7 @@ namespace GlitchyEditor
Matrix world = Billboard(transform.WorldTransform); Matrix world = Billboard(transform.WorldTransform);
float alpha = CalculateAlpha(transform.WorldTransform.Translation); float alpha = CalculateAlpha(transform.WorldTransform.Translation);
Renderer2D.DrawQuad(world, _editorIcons.DirectionalLight, ColorRGBA(light.SceneLight.Color.R * alpha, light.SceneLight.Color.G * alpha, light.SceneLight.Color.B * alpha, alpha), .(0, 0, 1, 1), entity.Index); Renderer2D.DrawQuad(world, _editorIcons.DirectionalLight, null, ColorRGBA(light.SceneLight.Color.R * alpha, light.SceneLight.Color.G * alpha, light.SceneLight.Color.B * alpha, alpha), .(0, 0, 1, 1), entity.Index);
} }
for (var (entity, transform, collider) in _activeScene.GetEntities<TransformComponent, BoxCollider2DComponent>()) for (var (entity, transform, collider) in _activeScene.GetEntities<TransformComponent, BoxCollider2DComponent>())
+68 -59
View File
@@ -113,14 +113,14 @@ namespace GlitchyEngine.Renderer
struct QueueLine: this(float4 Start, float4 End, ColorRGBA Color, float Depth, uint32 entityId = uint32.MaxValue) { } struct QueueLine: this(float4 Start, float4 End, ColorRGBA Color, float Depth, uint32 entityId = uint32.MaxValue) { }
struct QueueQuad: this(Matrix Transform, ColorRGBA Color, Texture Texture, float Depth, float4 uvTransform, uint32 entityId = uint32.MaxValue) { } struct QueueQuad: this(Matrix Transform, ColorRGBA Color, Texture Texture, Material Material, float Depth, float4 uvTransform, uint32 entityId = uint32.MaxValue) { }
struct QueueCircle : QueueQuad struct QueueCircle : QueueQuad
{ {
public float InnerRadius; public float InnerRadius;
public this(Matrix Transform, ColorRGBA Color, Texture Texture, float Depth, float4 uvTransform, float innerRadius, uint32 entityId = uint32.MaxValue) public this(Matrix Transform, ColorRGBA Color, Texture Texture, Material material, float Depth, float4 uvTransform, float innerRadius, uint32 entityId = uint32.MaxValue)
: base(Transform, Color, Texture, Depth, uvTransform, entityId) : base(Transform, Color, Texture, material, Depth, uvTransform, entityId)
{ {
InnerRadius = innerRadius; InnerRadius = innerRadius;
} }
@@ -131,6 +131,8 @@ namespace GlitchyEngine.Renderer
private static bool s_sceneRunning; private static bool s_sceneRunning;
#endif #endif
private static Matrix sceneViewProjection;
private static AssetHandle<Effect> s_quadBatchEffect; private static AssetHandle<Effect> s_quadBatchEffect;
private static Material s_quadBatchMaterial ~ _?.ReleaseRef(); private static Material s_quadBatchMaterial ~ _?.ReleaseRef();
private static AssetHandle<Effect> s_circleBatchEffect; private static AssetHandle<Effect> s_circleBatchEffect;
@@ -524,12 +526,12 @@ namespace GlitchyEngine.Renderer
s_currentLineEffect = s_lineBatchEffect; s_currentLineEffect = s_lineBatchEffect;
Matrix viewProjection = camera.Projection * Matrix.Invert(transform); sceneViewProjection = camera.Projection * Matrix.Invert(transform);
s_quadBatchMaterial.SetVariable("ViewProjection", viewProjection); s_quadBatchMaterial.SetVariable("ViewProjection", sceneViewProjection);
//s_currentQuadEffect.Get()?.Variables["ViewProjection"].SetData(viewProjection); //s_currentQuadEffect.Get()?.Variables["ViewProjection"].SetData(viewProjection);
s_currentCircleEffect.Get()?.Variables["ViewProjection"].SetData(viewProjection); s_currentCircleEffect.Get()?.Variables["ViewProjection"].SetData(sceneViewProjection);
s_currentLineEffect.Get()?.Variables["ViewProjection"].SetData(viewProjection); s_currentLineEffect.Get()?.Variables["ViewProjection"].SetData(sceneViewProjection);
s_drawOrder = drawOrder; s_drawOrder = drawOrder;
@@ -566,12 +568,12 @@ namespace GlitchyEngine.Renderer
s_currentLineEffect = s_lineBatchEffect; s_currentLineEffect = s_lineBatchEffect;
Matrix viewProjection = camera.Projection * camera.View; sceneViewProjection = camera.Projection * camera.View;
//s_currentQuadEffect.Get()?.Variables["ViewProjection"].SetData(viewProjection); //s_currentQuadEffect.Get()?.Variables["ViewProjection"].SetData(viewProjection);
s_quadBatchMaterial.SetVariable("ViewProjection", viewProjection); s_quadBatchMaterial.SetVariable("ViewProjection", sceneViewProjection);
s_currentCircleEffect.Get()?.Variables["ViewProjection"].SetData(viewProjection); s_currentCircleEffect.Get()?.Variables["ViewProjection"].SetData(sceneViewProjection);
s_currentLineEffect.Get()?.Variables["ViewProjection"].SetData(viewProjection); s_currentLineEffect.Get()?.Variables["ViewProjection"].SetData(sceneViewProjection);
s_drawOrder = drawOrder; s_drawOrder = drawOrder;
@@ -606,17 +608,17 @@ namespace GlitchyEngine.Renderer
/// Adds a quad instance to the instance queue. /// Adds a quad instance to the instance queue.
[Inline] [Inline]
private static void QueueQuadInstance(Matrix transform, ColorRGBA color, Texture texture, float depth, float4 uvTransform, uint32 id = uint32.MaxValue) private static void QueueQuadInstance(Matrix transform, ColorRGBA color, Texture texture, Material material, float depth, float4 uvTransform, uint32 id = uint32.MaxValue)
{ {
s_QuadinstanceQueue.Add(QueueQuad(transform, color, texture ?? s_whiteTexture, depth, uvTransform, id)); s_QuadinstanceQueue.Add(QueueQuad(transform, color, texture ?? s_whiteTexture, material, depth, uvTransform, id));
s_statistics.QuadCount++; s_statistics.QuadCount++;
} }
/// Adds a circle instance to the instance queue. /// Adds a circle instance to the instance queue.
[Inline] [Inline]
private static void QueueCircleInstance(Matrix transform, ColorRGBA color, Texture2D texture, float depth, float4 uvTransform, float innerRadius, uint32 id = uint32.MaxValue) private static void QueueCircleInstance(Matrix transform, ColorRGBA color, Texture2D texture, Material material, float depth, float4 uvTransform, float innerRadius, uint32 id = uint32.MaxValue)
{ {
s_circleInstanceQueue.Add(QueueCircle(transform, color, texture ?? s_whiteTexture, depth, uvTransform, innerRadius, id)); s_circleInstanceQueue.Add(QueueCircle(transform, color, texture ?? s_whiteTexture, material, depth, uvTransform, innerRadius, id));
s_statistics.CircleCount++; s_statistics.CircleCount++;
} }
@@ -628,7 +630,7 @@ namespace GlitchyEngine.Renderer
s_statistics.LineCount++; s_statistics.LineCount++;
} }
private static void FlushQuadInstances(Texture texture) private static void FlushQuadInstances(Material material, Texture texture)
{ {
Debug.Profiler.ProfileRendererFunction!(); Debug.Profiler.ProfileRendererFunction!();
@@ -639,11 +641,11 @@ namespace GlitchyEngine.Renderer
//s_currentQuadEffect.ApplyChanges(); //s_currentQuadEffect.ApplyChanges();
//s_currentQuadEffect.Bind(); //s_currentQuadEffect.Bind();
s_quadBatchMaterial.Bind(); material.Bind();
using (TextureViewBinding tvb = texture.GetViewBinding()) using (TextureViewBinding tvb = texture.GetViewBinding())
{ {
if (s_quadBatchMaterial.Effect.Textures.TryGetValue("Texture", let textureEntry)) if (material.Effect.Textures.TryGetValue("Texture", let textureEntry))
{ {
if (textureEntry.PsSlot != null) if (textureEntry.PsSlot != null)
{ {
@@ -721,7 +723,14 @@ namespace GlitchyEngine.Renderer
// Circle comparison // Circle comparison
private static int TextureComparison(QueueCircle lhs, QueueCircle rhs) private static int TextureComparison(QueueCircle lhs, QueueCircle rhs)
{ {
return (int)Internal.UnsafeCastToPtr(lhs.Texture) - (int)Internal.UnsafeCastToPtr(rhs.Texture); // When we sort by texture, we can choose the most optimal order.
// So we sort by material first, so we don't have to change too much state all the time.
int materialCmp = (int)Internal.UnsafeCastToPtr(lhs.Texture) <=> (int)Internal.UnsafeCastToPtr(rhs.Texture);
if (materialCmp != 0)
return materialCmp;
else
return (int)Internal.UnsafeCastToPtr(lhs.Texture) - (int)Internal.UnsafeCastToPtr(rhs.Texture);
} }
private static int BackToFrontComparison(QueueCircle lhs, QueueCircle rhs) private static int BackToFrontComparison(QueueCircle lhs, QueueCircle rhs)
{ {
@@ -799,13 +808,11 @@ namespace GlitchyEngine.Renderer
// TODO: per object blendstate // TODO: per object blendstate
RenderCommand.SetBlendState(s_transparentBlendState); RenderCommand.SetBlendState(s_transparentBlendState);
let quadMaterial = s_quadBatchMaterial;//s_currentQuadEffect.Get();
if (quadMaterial == null)
return;
Texture texture = s_QuadinstanceQueue[0].Texture; Texture texture = s_QuadinstanceQueue[0].Texture;
//quadMaterial.SetTexture("Texture", .Invalid); Material material = s_QuadinstanceQueue[0].Material;
// TODO: Actually use an engine buffer?
material.SetVariable("ViewProjection", sceneViewProjection);
s_setQuadInstances = 0; s_setQuadInstances = 0;
@@ -814,23 +821,23 @@ namespace GlitchyEngine.Renderer
var quad = ref s_QuadinstanceQueue[i]; var quad = ref s_QuadinstanceQueue[i];
// flush every time the texture changes // flush every time the texture changes
if(quad.Texture != texture) if(quad.Texture != texture || quad.Material != material)
{ {
FlushQuadInstances(texture); FlushQuadInstances(material, texture);
material = quad.Material;
texture = quad.Texture; texture = quad.Texture;
//quadMaterial.SetTexture("Texture", .Invalid);
} }
s_rawQuadInstances[s_setQuadInstances++] = .(quad.Transform, quad.Color, quad.uvTransform, quad.entityId); s_rawQuadInstances[s_setQuadInstances++] = .(quad.Transform, quad.Color, quad.uvTransform, quad.entityId);
if(s_setQuadInstances == s_rawQuadInstances.Count) if(s_setQuadInstances == s_rawQuadInstances.Count)
{ {
FlushQuadInstances(texture); FlushQuadInstances(material, texture);
} }
} }
FlushQuadInstances(texture); FlushQuadInstances(material, texture);
s_QuadinstanceQueue.Clear(); s_QuadinstanceQueue.Clear();
} }
@@ -1019,28 +1026,28 @@ namespace GlitchyEngine.Renderer
public static void DrawQuad(float2 position, float2 size, float rotation, ColorRGBA color) public static void DrawQuad(float2 position, float2 size, float rotation, ColorRGBA color)
{ {
DrawQuad(float3(position, 0.0f), size, rotation, s_whiteTexture, color); DrawQuad(float3(position, 0.0f), size, rotation, s_whiteTexture, s_quadBatchMaterial, color);
} }
/// Like DrawQuad but the pivot point is the top left corner /// Like DrawQuad but the pivot point is the top left corner
public static void DrawQuadPivotCorner(float2 position, float2 size, float rotation, ColorRGBA color) public static void DrawQuadPivotCorner(float2 position, float2 size, float rotation, ColorRGBA color)
{ {
DrawQuadPivotCorner(float3(position, 0.0f), size, rotation, s_whiteTexture, color); DrawQuadPivotCorner(float3(position, 0.0f), size, rotation, s_whiteTexture, s_quadBatchMaterial, color);
} }
public static void DrawQuad(float3 position, float2 size, float rotation, ColorRGBA color) public static void DrawQuad(float3 position, float2 size, float rotation, ColorRGBA color)
{ {
DrawQuad(position, size, rotation, s_whiteTexture, color); DrawQuad(position, size, rotation, s_whiteTexture, s_quadBatchMaterial, color);
} }
public static void DrawQuadPivotCorner(float3 position, float2 size, float rotation, ColorRGBA color) public static void DrawQuadPivotCorner(float3 position, float2 size, float rotation, ColorRGBA color)
{ {
DrawQuadPivotCorner(position, size, rotation, s_whiteTexture, color); DrawQuadPivotCorner(position, size, rotation, s_whiteTexture, s_quadBatchMaterial, color);
} }
public static void DrawQuad(Matrix transform, ColorRGBA color) public static void DrawQuad(Matrix transform, ColorRGBA color)
{ {
DrawQuad(transform, s_whiteTexture, color); DrawQuad(transform, s_whiteTexture, s_quadBatchMaterial, color);
} }
// Quad Subtexture // Quad Subtexture
@@ -1057,59 +1064,59 @@ namespace GlitchyEngine.Renderer
// Subtex only // Subtex only
public static void DrawQuad(float2 position, float2 size, float rotation, SubTexture2D texture, ColorRGBA color = .White) public static void DrawQuad(float2 position, float2 size, float rotation, SubTexture2D texture, Material material = null, ColorRGBA color = .White)
{ {
DrawQuad(float3(position, 0.0f), size, rotation, texture.Texture, .White, texture.TexCoords); DrawQuad(float3(position, 0.0f), size, rotation, texture.Texture, material, color, texture.TexCoords);
} }
public static void DrawQuad(float3 position, float2 size, float rotation, SubTexture2D texture, ColorRGBA color = .White) public static void DrawQuad(float3 position, float2 size, float rotation, SubTexture2D texture, Material material = null, ColorRGBA color = .White)
{ {
DrawQuad(position, size, rotation, texture.Texture, .White, texture.TexCoords); DrawQuad(position, size, rotation, texture.Texture, material, color, texture.TexCoords);
} }
public static void DrawQuad(Matrix transform, SubTexture2D texture, ColorRGBA color = .White) public static void DrawQuad(Matrix transform, SubTexture2D texture, Material material = null, ColorRGBA color = .White)
{ {
DrawQuad(transform, texture.Texture, color, texture.TexCoords); DrawQuad(transform, texture.Texture, material, color, texture.TexCoords);
} }
// Subtex + Texcoords // Subtex + Texcoords
public static void DrawQuad(float2 position, float2 size, float rotation, SubTexture2D subtexture, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1)) public static void DrawQuad(float2 position, float2 size, float rotation, SubTexture2D subtexture, Material material = null, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1))
{ {
float4 uv = CalculateSubTexcoords(subtexture.TexCoords, uvTransform); float4 uv = CalculateSubTexcoords(subtexture.TexCoords, uvTransform);
DrawQuad(float3(position, 0.0f), size, rotation, subtexture.Texture, .White, uv); DrawQuad(float3(position, 0.0f), size, rotation, subtexture.Texture, material, color, uv);
} }
public static void DrawQuad(float3 position, float2 size, float rotation, SubTexture2D subtexture, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1)) public static void DrawQuad(float3 position, float2 size, float rotation, SubTexture2D subtexture, Material material = null, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1))
{ {
float4 uv = CalculateSubTexcoords(subtexture.TexCoords, uvTransform); float4 uv = CalculateSubTexcoords(subtexture.TexCoords, uvTransform);
DrawQuad(position, size, rotation, subtexture.Texture, .White, uv); DrawQuad(position, size, rotation, subtexture.Texture, material, color, uv);
} }
public static void DrawQuad(Matrix transform, SubTexture2D subtexture, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1), uint32 entityId = uint32.MaxValue) public static void DrawQuad(Matrix transform, SubTexture2D subtexture, Material material = null, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1), uint32 entityId = uint32.MaxValue)
{ {
float4 uv = CalculateSubTexcoords(subtexture.TexCoords, uvTransform); float4 uv = CalculateSubTexcoords(subtexture.TexCoords, uvTransform);
DrawQuad(transform, subtexture.Texture, color, uv, entityId); DrawQuad(transform, subtexture.Texture, material, color, uv, entityId);
} }
// Textured Quad // Textured Quad
public static void DrawQuad(float2 position, float2 size, float rotation, Texture texture, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1)) public static void DrawQuad(float2 position, float2 size, float rotation, Texture texture, Material material = null, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1))
{ {
DrawQuad(float3(position, 0.0f), size, rotation, texture, color, uvTransform); DrawQuad(float3(position, 0.0f), size, rotation, texture, material, color, uvTransform);
} }
public static void DrawQuad(float3 position, float2 size, float rotation, Texture texture, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1)) public static void DrawQuad(float3 position, float2 size, float rotation, Texture texture, Material material = null, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1))
{ {
Matrix transform = Calculate2DTransform(position, size, rotation); Matrix transform = Calculate2DTransform(position, size, rotation);
DrawQuad(transform, texture, color, uvTransform); DrawQuad(transform, texture, material, color, uvTransform);
} }
public static void DrawQuad(Matrix transform, Texture texture, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1), uint32 entityId = uint32.MaxValue) public static void DrawQuad(Matrix transform, Texture texture, Material material = null, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1), uint32 entityId = uint32.MaxValue)
{ {
Debug.Profiler.ProfileRendererFunction!(); Debug.Profiler.ProfileRendererFunction!();
@@ -1117,7 +1124,7 @@ namespace GlitchyEngine.Renderer
Log.EngineLogger.AssertDebug(s_sceneRunning, "Missing call of BeginScene."); Log.EngineLogger.AssertDebug(s_sceneRunning, "Missing call of BeginScene.");
#endif #endif
QueueQuadInstance(transform, color, texture, transform.Translation.Z, uvTransform, entityId); QueueQuadInstance(transform, color, texture, material ?? s_quadBatchMaterial, transform.Translation.Z, uvTransform, entityId);
if(s_drawOrder == .Immediate) if(s_drawOrder == .Immediate)
{ {
@@ -1142,7 +1149,8 @@ namespace GlitchyEngine.Renderer
uvTransform = sprite.TextureCoordinates; uvTransform = sprite.TextureCoordinates;
} }
DrawQuad(transform, spriteTexture ?? s_whiteTexture, spriteRenderer.Color, uvTransform, entityId); // TODO: Material for Sprite renderer
DrawQuad(transform, spriteTexture ?? s_whiteTexture, null, spriteRenderer.Color, uvTransform, entityId);
} }
public static void DrawCircle(Matrix transform, CircleRendererComponent* spriteRenderer, uint32 entityId) public static void DrawCircle(Matrix transform, CircleRendererComponent* spriteRenderer, uint32 entityId)
@@ -1152,14 +1160,14 @@ namespace GlitchyEngine.Renderer
// Textured quad pivot // Textured quad pivot
public static void DrawQuadPivotCorner(float2 position, float2 size, float rotation, Texture2D texture, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1)) public static void DrawQuadPivotCorner(float2 position, float2 size, float rotation, Texture2D texture, Material material = null, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1))
{ {
DrawQuadPivotCorner(float3(position, 0.0f), size, rotation, texture, color, uvTransform); DrawQuadPivotCorner(float3(position, 0.0f), size, rotation, texture, material, color, uvTransform);
} }
public static void DrawQuadPivotCorner(float3 position, float2 size, float rotation, Texture2D texture, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1)) public static void DrawQuadPivotCorner(float3 position, float2 size, float rotation, Texture2D texture, Material material = null, ColorRGBA color = .White, float4 uvTransform = .(0, 0, 1, 1))
{ {
DrawQuad(position + float3(size.X / 2, size.Y / -2, 0), size, rotation, texture, color, uvTransform); DrawQuad(position + float3(size.X / 2, size.Y / -2, 0), size, rotation, texture, material, color, uvTransform);
} }
// Circle // Circle
@@ -1194,7 +1202,8 @@ namespace GlitchyEngine.Renderer
Log.EngineLogger.AssertDebug(s_sceneRunning, "Missing call of BeginScene."); Log.EngineLogger.AssertDebug(s_sceneRunning, "Missing call of BeginScene.");
#endif #endif
QueueCircleInstance(transform, color, texture, transform.Translation.Z, uvTransform, innerRadius, entityId); Runtime.FatalError("Circle renderer currently broken, do we need it?");
QueueCircleInstance(transform, color, texture, null, transform.Translation.Z, uvTransform, innerRadius, entityId);
if(s_drawOrder == .Immediate) if(s_drawOrder == .Immediate)
{ {
+13 -30
View File
@@ -19,6 +19,7 @@ namespace GlitchyEngine.Renderer.Text
internal static FT_Library s_Library; internal static FT_Library s_Library;
public static AssetHandle<Effect> _msdfEffect; public static AssetHandle<Effect> _msdfEffect;
public static Material _msdfMaterial ~ _?.ReleaseRef();
internal static bool s_isInitialized; internal static bool s_isInitialized;
@@ -33,6 +34,7 @@ namespace GlitchyEngine.Renderer.Text
// TODO: We might get away with a non blocking load here, but not for now! // TODO: We might get away with a non blocking load here, but not for now!
_msdfEffect = Content.LoadAsset("Resources/Shaders/msdfShader.hlsl", null, true); _msdfEffect = Content.LoadAsset("Resources/Shaders/msdfShader.hlsl", null, true);
_msdfMaterial = new Material(_msdfEffect);
s_isInitialized = true; s_isInitialized = true;
} }
@@ -558,9 +560,9 @@ namespace GlitchyEngine.Renderer.Text
textRenderer.NeedsRebuild = false; textRenderer.NeedsRebuild = false;
} }
public static void DrawText(PreparedText text, Matrix transform, ColorRGBA fontColor = .White) public static void DrawText(PreparedText text, Matrix transform, ColorRGBA fontColor = .White, uint32 entityId = uint32.MaxValue)
{ {
/*Debug.Profiler.ProfileRendererFunction!(); Debug.Profiler.ProfileRendererFunction!();
text.AddRef(); text.AddRef();
defer text.ReleaseRef(); defer text.ReleaseRef();
@@ -568,20 +570,9 @@ namespace GlitchyEngine.Renderer.Text
if (text.Glyphs.Count == 0) if (text.Glyphs.Count == 0)
return; return;
Renderer2D.Flush(); // TODO: this doesn't really work with fallback fonts unless we use the same settings for all fonts
// TODO: this is very not good!
// At some point we will support using materials with the 2D renderer. At that point we can simply bind different materials with fonts (or glyphs?) and don't need this hack anymore. One day...
var lastEffect = Renderer2D.[Friend]s_currentQuadEffect;
Renderer2D.[Friend]s_currentQuadEffect = _msdfEffect;
// TODO: oh no....
// Copy viewProjection from current effect
Matrix viewProjection = Renderer2D.[Friend]s_quadBatchEffect.Variables["ViewProjection"].[Friend]GetData<Matrix>();
_msdfEffect.Variables["ViewProjection"].SetData(viewProjection);
// TODO: this doesn't really work with fallback fonts
float2 unitRange = ((float)text.Font._range) / float2(text.Font._atlas.Width, text.Font._atlas.Height); float2 unitRange = ((float)text.Font._range) / float2(text.Font._atlas.Width, text.Font._atlas.Height);
_msdfEffect.Variables["UnitRange"].SetData(unitRange); _msdfMaterial.SetVariable("UnitRange", unitRange);
List<Texture2D> atlasses = scope .(); List<Texture2D> atlasses = scope .();
@@ -608,11 +599,9 @@ namespace GlitchyEngine.Renderer.Text
float2 atlasSize = .(atlas.Width, atlas.Height); float2 atlasSize = .(atlas.Width, atlas.Height);
float adjustToPenX = glyphDesc.AdjustToPen; float adjustToPenX = glyphDesc.AdjustToPen;
//adjustToPenX *= glyphFontScale;
adjustToPenX *= glyph.Scale; adjustToPenX *= glyph.Scale;
float adjustToBaseline = glyphDesc.AdjustToBaseLine; float adjustToBaseline = glyphDesc.AdjustToBaseLine;
//adjustToBaseline *= glyphFontScale;
adjustToBaseline *= glyph.Scale; adjustToBaseline *= glyph.Scale;
// Rectangle on the screen // Rectangle on the screen
@@ -630,31 +619,25 @@ namespace GlitchyEngine.Renderer.Text
texRect /= float4(atlasSize, atlasSize); texRect /= float4(atlasSize, atlasSize);
// Show quads
// Renderer2D.DrawQuad(float3(viewportRect.X + viewportRect.Z / 2, viewportRect.Y + viewportRect.W / 2, 1), .(viewportRect.Z, viewportRect.W), 0, .Red);
float3 position = .(viewportRect.X + viewportRect.Z / 2, viewportRect.Y + viewportRect.W / 2, 0); float3 position = .(viewportRect.X + viewportRect.Z / 2, viewportRect.Y + viewportRect.W / 2, 0);
Matrix glyphTransform = transform * Matrix.Translation(position) * Matrix.Scaling(viewportRect.Z, viewportRect.W, 1.0f); Matrix glyphTransform = transform * Matrix.Translation(position) * Matrix.Scaling(viewportRect.Z, viewportRect.W, 1.0f);
Renderer2D.DrawQuad(glyphTransform, atlas, glyphColor, texRect); Renderer2D.DrawQuad(glyphTransform, atlas, material: _msdfMaterial, color: glyphColor, uvTransform: texRect, entityId: entityId);
//Renderer2D.DrawQuad(float2(viewportRect.X + viewportRect.Z / 2, viewportRect.Y + viewportRect.W / 2), .(viewportRect.Z, viewportRect.W), 0, atlas, glyphColor, texRect);
// Show pen positions
// Renderer2D.DrawQuad(float3(float2(x, y) + glyph.Position, -1), .(1), 0, .Green);
} }
// TODO: Get rid of the flush.
// We need to flush, because currently Renderer2D doesn't increase the counter of passed textures.
// Once it does that, we can
// 1. Stop manually holding the references in this method
// 2. Stop forcing a flush (which could make having multiple text instances way more efficient)
Renderer2D.Flush(); Renderer2D.Flush();
// TODO: not good!
// Change back effect
Renderer2D.[Friend]s_currentQuadEffect = lastEffect;
// release all atlas textures // release all atlas textures
for(int i < atlasses.Count) for(int i < atlasses.Count)
{ {
atlasses[i].ReleaseRef(); atlasses[i].ReleaseRef();
}*/ }
} }
} }
} }
+2 -2
View File
@@ -139,7 +139,7 @@ class SceneRenderer
if (editorFlags.Flags.HasFlag(.HideInScene) || text.PreparedText == null) if (editorFlags.Flags.HasFlag(.HideInScene) || text.PreparedText == null)
continue; continue;
FontRenderer.DrawText(text.PreparedText, transform.WorldTransform, text.Color); FontRenderer.DrawText(text.PreparedText, transform.WorldTransform, text.Color, entity.Index);
} }
Renderer2D.EndScene(); Renderer2D.EndScene();
@@ -230,7 +230,7 @@ class SceneRenderer
if (editorFlags.Flags.HasFlag(.HideInScene) || text.PreparedText == null) if (editorFlags.Flags.HasFlag(.HideInScene) || text.PreparedText == null)
continue; continue;
FontRenderer.DrawText(text.PreparedText, transform.WorldTransform, text.Color); FontRenderer.DrawText(text.PreparedText, transform.WorldTransform, text.Color, entity.Index);
} }
//FontRenderer.DrawText(_smallLinesInfo, 0, 0); //FontRenderer.DrawText(_smallLinesInfo, 0, 0);