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:
Simon Lübeß
2021-09-29 23:27:41 +02:00
parent 9a9d75afbc
commit 4dc6c916ae
14 changed files with 568 additions and 360 deletions
+13 -7
View File
@@ -3,30 +3,33 @@ SamplerState Sampler : register(s0);
cbuffer Constants
{
float4x4 ViewProjection;
float2 UnitRange;
float screenPixelRange = 2;
}
struct VS_Input
{
float2 Position : POSITION;
float2 Texcoord : TEXCOORD0;
float4x4 Tranform : TRANSFORM;
float4 Color : COLOR;
float4 UVTransform : TEXCOORD;
float4 UVTransform : TEXCOORD1;
};
struct PS_Input
{
float4 Position : SV_Position;
float2 TexCoord : TEXCOORD0;
float4 Color : COLOR;
float4 Color : COLOR;
};
PS_Input VS(VS_Input input)
{
PS_Input output;
output.Position = mul(float4(input.Position, 0.0f, 1.0f), input.Tranform);
output.TexCoord = input.UVTransform.xy + input.UVTransform.zw * input.Position;
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;
return output;
@@ -36,14 +39,17 @@ float median(float r, float g, float b) {
return max(min(r, g), min(max(r, g), b));
}
float ScreenPxRange(float2 texcoord) {
float2 screenTexSize = 1.0f / fwidth(texcoord);
return max(0.5f * dot(UnitRange, screenTexSize), 1.0f);
}
float4 PS(PS_Input input) : SV_Target0
{
float3 msd = Texture.Sample(Sampler, input.TexCoord);
float sd = median(msd.r, msd.g, msd.b);
float screenPxDistance = screenPixelRange * (sd - 0.5);
float screenPxDistance = ScreenPxRange(input.TexCoord) * (sd - 0.5);
float opacity = clamp(screenPxDistance + 0.5, 0.0, 1.0);
//float4 color = lerp(float4(1, 0, 0, 1), input.Color, opacity);
//return color;
return float4(input.Color.rgb, opacity * input.Color.a);
}
+41
View File
@@ -0,0 +1,41 @@
Texture2D Texture : register(t0);
SamplerState Sampler : register(s0);
cbuffer Constants : register(b0)
{
float4x4 ViewProjection;
};
struct VS_Input
{
float2 Position : POSITION;
float2 Texcoord : TEXCOORD0;
float4x4 Transform : TRANSFORM;
float4 Color : COLOR;
float4 UVTransform : TEXCOORD1;
};
struct PS_Input
{
float4 Position : SV_Position;
float2 Texcoord : TEXCOORD;
float4 Color : COLOR;
};
PS_Input VS(VS_Input input)
{
PS_Input output;
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.Color = input.Color;
return output;
}
float4 PS(PS_Input input) : SV_Target0
{
return Texture.Sample(Sampler, input.Texcoord) * input.Color;
}
#effect[VS=VS, PS=PS]
+42
View File
@@ -0,0 +1,42 @@
Texture2D Texture : register(t0);
SamplerState Sampler : register(s0);
cbuffer Constants : register(b0)
{
float4x4 World;
// TODO: move ViewProjection to seperate cbuffer
float4x4 ViewProjection;
float4 Color;
float4 UVTransform;
};
struct VS_Input
{
float2 Position : POSITION;
float2 Texcoord : TEXCOORD;
};
struct PS_Input
{
float4 Position : SV_Position;
float2 Texcoord : TEXCOORD;
};
PS_Input VS(VS_Input input)
{
PS_Input output;
output.Position = mul(ViewProjection, mul(World, float4(input.Position, 0.0f, 1.0f)));
output.Texcoord = UVTransform.xy + UVTransform.zw * input.Texcoord;
return output;
}
float4 PS(PS_Input input) : SV_Target0
{
return Texture.Sample(Sampler, input.Texcoord) * Color;
}
#effect[VS=VS, PS=PS]
@@ -3,6 +3,8 @@ SamplerState Sampler : register(s0);
cbuffer Constants
{
float4x4 ViewProjection;
float ColorOffset = 0.5f;
float AlphaOffset = 0.0f;
float ColorScale = 0.5f;
@@ -13,16 +15,17 @@ cbuffer Constants
struct VS_Input
{
float2 Position : POSITION;
float4x4 Tranform : TRANSFORM;
float4 Color : COLOR;
float4 UVTransform : TEXCOORD;
float2 Position : POSITION;
float2 Texcoord : TEXCOORD0;
float4x4 Transform : TRANSFORM;
float4 Color : COLOR;
float4 UVTransform : TEXCOORD1;
};
struct PS_Input
{
float4 Position : SV_Position;
float2 TexCoord : TEXCOORD0;
float2 Texcoord : TEXCOORD0;
float4 Color : COLOR;
};
@@ -30,8 +33,8 @@ PS_Input VS(VS_Input input)
{
PS_Input output;
output.Position = mul(float4(input.Position, 0.0f, 1.0f), input.Tranform);
output.TexCoord = input.UVTransform.xy + input.UVTransform.zw * input.Position;
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.Color = input.Color;
return output;
@@ -39,7 +42,7 @@ PS_Input VS(VS_Input input)
float4 PS(PS_Input input) : SV_Target0
{
float4 color = Texture.Sample(Sampler, input.TexCoord);
float4 color = Texture.Sample(Sampler, input.Texcoord);
float4 final = float4(ColorOffset.xxx, AlphaOffset) + color * float4(ColorScale.xxx, AlphaScale);
@@ -49,7 +49,6 @@ namespace Sandbox
DepthStencilTarget _depthTarget ~ _?.ReleaseRef();
Texture2D _texture ~ _?.ReleaseRef();
Texture2D _ge_logo ~ _?.ReleaseRef();
BlendState _alphaBlendState ~ _?.ReleaseRef();
BlendState _opaqueBlendState ~ _?.ReleaseRef();
@@ -58,9 +57,7 @@ namespace Sandbox
EcsWorld _world = new EcsWorld() ~ delete _;
Renderer2D Renderer2D ~ delete _;
Texture2D _testTexture ~ _?.ReleaseRef();
//Renderer2D Renderer2D ~ delete _;
String testText ~ delete _;
@@ -78,22 +75,22 @@ namespace Sandbox
_effectLibrary.LoadNoRefInc("content\\Shaders\\basicShader.hlsl");
// Create rasterizer state
GlitchyEngine.Renderer.RasterizerStateDescription rsDesc = .(.Solid, .Back, true);
GlitchyEngine.Renderer.RasterizerStateDescription rsDesc = .(.Solid, .Back, false);
_rasterizerState = new RasterizerState(_context, rsDesc);
_depthTarget = new DepthStencilTarget(_context, _context.SwapChain.Width, _context.SwapChain.Height);
_texture = new Texture2D(_context, "content/Textures/Checkerboard.dds");
_ge_logo = new Texture2D(_context, "content/Textures/GE_Logo.dds");
let sampler = SamplerStateManager.GetSampler(
SamplerStateDescription()
{
MagFilter = .Point
MagFilter = .Point,
AddressModeU = .Wrap,
AddressModeV = .Wrap,
});
_texture.SamplerState = sampler;
_ge_logo.SamplerState = sampler;
sampler.ReleaseRef();
@@ -104,7 +101,7 @@ namespace Sandbox
InitEcs();
Renderer2D = new Renderer2D(_context, _effectLibrary);
//Renderer2D = new Renderer2D(_context, _effectLibrary);
//Init2D();
Texture2DDesc desc = .();
@@ -116,14 +113,6 @@ namespace Sandbox
desc.MipLevels = 1;
desc.Usage = .Default;
_testTexture = new Texture2D(_context, desc);
Color[4] colors = .(
.Red, .Green,
.Blue, .White);
_testTexture.SetData<Color>(&colors, 0, 1, 1, 1);
fonty = new Font(_context, "C:\\Windows\\Fonts\\arial.ttf", 64, true, 'A', 16);
var japanese = new Font(_context, "C:\\Windows\\Fonts\\YuGothM.ttc", 64, true, '\0', 1);
var emojis = new Font(_context, "C:\\Windows\\Fonts\\seguiemj.ttf", 64, true, '😂' - 10, 1);
@@ -135,15 +124,12 @@ namespace Sandbox
// Load test text
File.ReadAllText("test.txt", testText = new String(), true);
_textureViewer = new TextureViewer(_context, Renderer2D, _effectLibrary);
_textureViewer = new TextureViewer();
FontRenderer.Init(_effectLibrary);
controller = new OrthographicCameraController(16 / 9f);
}
public ~this()
{
FontRenderer.DeInit();
}
OrthographicCameraController controller;
Font fonty ~ _.ReleaseRef();
@@ -262,20 +248,41 @@ namespace Sandbox
var camera = _world.GetComponent<CameraComponent>(_cameraEntity);
camera.Aspect = Application.Get().Window.Context.SwapChain.BackbufferViewport.Width /
Application.Get().Window.Context.SwapChain.BackbufferViewport.Height;
controller.Update(gameTime);
TransformSystem.Update(_world);
RenderCommand.Clear(null, .(0.2f, 0.2f, 0.2f));
RenderCommand.Clear(_depthTarget, 1.0f, 0, .Depth);
_depthTarget..Clear(1.0f, 0, .Depth).Bind();
// Draw test geometry
_context.SetRenderTarget(null);
_depthTarget.Bind();
_context.BindRenderTargets();
_context.SetRasterizerState(_rasterizerState);
RenderCommand.SetViewport(_context.SwapChain.BackbufferViewport);
Renderer2D.BeginScene(controller.Camera, .BackToFront);
_alphaBlendState.Bind();
for(int x < 10)
for(int y < 10)
{
int i = (x + y) % 2;
Renderer2D.DrawQuad(Vector3(2 * x, 2 * y, 0.5f), Vector2(1.5f, 1), MathHelper.PiOverFour, (i == 0) ? _squareColor0 : _squareColor1);
}
Renderer2D.DrawQuad(Vector3(0, 0, 1), Vector2(10), 0, _texture, .White, .(0, 0, 1, 1));
FontRenderer.DrawText(fonty, "Hallo! gjy", 0, 0, 64, .White, .White);
Renderer2D.EndScene();
/*
Renderer2D.Begin(.FrontToBack, .(80, 80));
@@ -322,8 +329,7 @@ namespace Sandbox
Renderer2D.End();
*/
_alphaBlendState.Bind();
/*
Renderer2D.Begin(.FrontToBack, .(_context.SwapChain.Width, _context.SwapChain.Height));
Renderer2D.End();
@@ -331,9 +337,10 @@ namespace Sandbox
_alphaBlendState.Bind();
Renderer2D.Begin(.FrontToBack, .(_context.SwapChain.Width, _context.SwapChain.Height), 100);
FontRenderer.DrawText(Renderer2D, fonty, "Hallo! gjy", 0, 0, 512, .White, .White);
FontRenderer.DrawText(Renderer2D, fonty, "Hallo! gjy", 0, 0, 64, .White, .White);
Renderer2D.End();
*/
}
int32 size = 500;
@@ -348,40 +355,20 @@ namespace Sandbox
dispatcher.Dispatch<ImGuiRenderEvent>(scope (e) => OnImGuiRender(e));
dispatcher.Dispatch<WindowResizeEvent>(scope (e) => OnWindowResize(e));
controller.OnEvent(event);
}
private bool OnImGuiRender(ImGuiRenderEvent e)
{
/*
ImGui.Begin("SDF Test");
ImGui.InputInt("Size", &size);
float f = (size / 64f * 4.0f);
ImGui.Text($"SPR: {f}");
ImGui.End();
*/
//return true;
/*
return true;
ImGui.Begin("Test");
ImGui.ColorEdit3("Square Color", ref _squareColor0);
ImGui.ColorPicker4("Color", *(float[4]*)&_squareColor0);
_squareColor1 = ColorRGBA.White - _squareColor0;
//ImGui.Begin
//ImGui.Image(fonty.[Friend]_atlas.[Friend]nativeView, .(100, 200));
//ImGui.Scrollbar(.X);
//ImGui.Image(fonty.[Friend]_atlas.[Friend]nativeView, .(fonty.[Friend]_atlas.Width, fonty.[Friend]_atlas.Height), .(0.0f, 0.0f), .(1.0f, 1.0f), .(1.0f, 1.0f, 1.0f, 1.0f), .(1.0f, 0, 0, 1));
_squareColor1.A = _squareColor0.A;
ImGui.End();
*/
_textureViewer.ViewTexture(fonty.[Friend]_atlas);
@@ -400,20 +387,4 @@ namespace Sandbox
return false;
}
}
class SandboxApp2D : Application
{
public this()
{
PushLayer(new ExampleLayer2D());
}
#if SANDBOX_2D
[Export, LinkName("CreateApplication")]
#endif
public static Application CreateApplication()
{
return new SandboxApp2D();
}
}
}
+4 -2
View File
@@ -570,12 +570,14 @@ namespace Sandbox
{
public this()
{
#if SANDBOX_2D
PushLayer(new ExampleLayer2D());
#else
PushLayer(new ExampleLayer());
#endif
}
#if !SANDBOX_2D
[Export, LinkName("CreateApplication")]
#endif
public static Application CreateApplication()
{
return new SandboxApp();
+28 -29
View File
@@ -24,8 +24,6 @@ namespace Sandbox
Effect _effect ~ _.ReleaseRef();
Renderer2D _renderer;
float _zoom = 1.0f;
BackgroundMode _backgroundMode = .Checkerboard;
@@ -39,29 +37,18 @@ namespace Sandbox
SamplerState _samplerPoint ~ _.ReleaseRef();
SamplerState _samplerLinear ~ _.ReleaseRef();
public this(GraphicsContext context, Renderer2D renderer2D, EffectLibrary effectLibrary = null)
public this()
{
Log.EngineLogger.AssertDebug(context != null);
_context = Renderer.[Friend]_context..AddRef();
_context = context;
_renderer = renderer2D;
InitEffect(effectLibrary);
InitEffect();
InitState();
// TODO: rasterizerstate and depthstencilstate
}
private void InitEffect(EffectLibrary effectLibrary)
private void InitEffect()
{
var effectLibrary;
if(effectLibrary == null)
{
effectLibrary = new EffectLibrary(_context);
defer:: delete effectLibrary;
}
_effect = effectLibrary.Load("content\\Shaders\\textureViewerShader.hlsl");
_effect = new Effect(_context, "content\\Shaders\\textureViewerShader.hlsl");
}
private void InitState()
@@ -174,6 +161,8 @@ namespace Sandbox
}
}
OrthographicCamera _camera = new OrthographicCamera() ~ delete _;
private void RenderTexture(Texture2D viewedTexture)
{
Viewport vp = .(0, 0, _target.Width, _target.Height);
@@ -198,28 +187,38 @@ namespace Sandbox
_effect.Variables["AlphaOffset"].SetData(_alphaOffset);
_effect.Variables["AlphaScale"].SetData(_alphaScale);
_renderer.Begin(.SortByTexture, targetSize);
_camera.Left = 0;
_camera.Top = 0;
_camera.Right = targetSize.X;
_camera.Bottom = -targetSize.Y;
_camera.NearPlane = -5;
_camera.FarPlane = 5;
_camera.Update();
Renderer2D.BeginScene(_camera);
switch(_backgroundMode)
{
case .Black:
_renderer.Draw(null, 0, 0, targetSize.X, targetSize.Y, .Black, 1);
Renderer2D.DrawQuadPivotCorner(Vector3(0, 0, 1), targetSize, 0, .Black);
case .White:
_renderer.Draw(null, 0, 0, targetSize.X, targetSize.Y, .White, 1);
Renderer2D.DrawQuadPivotCorner(Vector3(0, 0, 1), targetSize, 0, .White);
case .Checkerboard:
float quadSize = 50.0f;
for(int x = 0; x < (targetSize.X / 500f) * 10f; x++)
Vector2 numQuads = (targetSize / 500f) * 10f;
for(float x = 0; x < numQuads.X; x++)
{
for(int y = 0; y < (targetSize.Y / 500f) * 10f; y++)
for(float y = 0; y < numQuads.Y; y++)
{
_renderer.Draw(null, x * quadSize, y * quadSize, quadSize, quadSize, ((x + y) % 2 == 0) ? .White : .Gray, 1);
Renderer2D.DrawQuadPivotCorner(Vector3(x * quadSize, -y * quadSize, 1), quadSize.XX, 0, ((x + y) % 2 == 0) ? .White : .Gray);
}
}
break;
}
_renderer.End();
Renderer2D.EndScene();
var sampler = viewedTexture.SamplerState;
@@ -231,11 +230,11 @@ namespace Sandbox
viewedTexture.SamplerState = _samplerLinear;
}
_renderer.Begin(.SortByTexture, targetSize, 100, _effect);
Renderer2D.BeginScene(_camera, .SortByTexture, _effect);
_renderer.Draw(viewedTexture, _position.X, _position.Y, zoomedTextureSize.X, zoomedTextureSize.Y);
Renderer2D.DrawQuadPivotCorner(Vector3(_position * .(1, -1), 0), zoomedTextureSize, 0, viewedTexture);
_renderer.End();
Renderer2D.EndScene();
viewedTexture.SamplerState = sampler;
}