From 143b4bad9e9c21dea62463142bb2b6a3dd2d317b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Sun, 15 Sep 2024 21:40:10 +0200 Subject: [PATCH] Copy UI to backbuffer, resize, stuff --- GlitchyEditor/resources/Shaders/Copy.hlsl | 31 ++++++++ GlitchyEditor/resources/Shaders/Copy.hlsl.ass | 5 ++ GlitchyEditor/src/UltralightLayer.bf | 79 ++++++++++++++++--- 3 files changed, 104 insertions(+), 11 deletions(-) create mode 100644 GlitchyEditor/resources/Shaders/Copy.hlsl create mode 100644 GlitchyEditor/resources/Shaders/Copy.hlsl.ass diff --git a/GlitchyEditor/resources/Shaders/Copy.hlsl b/GlitchyEditor/resources/Shaders/Copy.hlsl new file mode 100644 index 0000000..715404b --- /dev/null +++ b/GlitchyEditor/resources/Shaders/Copy.hlsl @@ -0,0 +1,31 @@ +Texture2D Texture : register(t0); +SamplerState TextureSampler : register(s0); + +struct VS_IN +{ + float2 Position : POSITION; + float2 TexCoord : TEXCOORD0; +}; + +struct PS_IN +{ + float4 Position : SV_POSITION; + float2 TexCoord : TEXCOORD; +}; + +PS_IN VS(VS_IN input) +{ + PS_IN output; + + output.Position = float4(input.Position, 0, 1); + output.TexCoord = input.TexCoord; + + return output; +} + +float4 PS(PS_IN input) : SV_TARGET +{ + return Texture.Sample(TextureSampler, input.TexCoord); +} + +#pragma Effect[VS = VS; PS = PS] diff --git a/GlitchyEditor/resources/Shaders/Copy.hlsl.ass b/GlitchyEditor/resources/Shaders/Copy.hlsl.ass new file mode 100644 index 0000000..9a81e28 --- /dev/null +++ b/GlitchyEditor/resources/Shaders/Copy.hlsl.ass @@ -0,0 +1,5 @@ +{ + AssetLoader = "EffectAssetLoader", + Config = (GlitchyEditor.Assets.EffectAssetLoaderConfig){}/* No reflection data for GlitchyEditor.Assets.EffectAssetLoaderConfig. Add [BonTarget] or force it */, + AssetHandle = 12483840759218743640 +} \ No newline at end of file diff --git a/GlitchyEditor/src/UltralightLayer.bf b/GlitchyEditor/src/UltralightLayer.bf index fec6a08..84753fd 100644 --- a/GlitchyEditor/src/UltralightLayer.bf +++ b/GlitchyEditor/src/UltralightLayer.bf @@ -9,6 +9,7 @@ using GlitchyEngine.ImGui; using ImGui; using System.Text; using GlitchyEngine.System; +using GlitchyEngine.Content; namespace GlitchyEditor; @@ -18,17 +19,23 @@ class UltralightLayer : Layer int2 cursorPosition; + private AssetHandle _copyEffect; + public this() { - NoApp(); - //RenderApp(); - WindowDescription windowDesc = .Default; windowDesc.Icon = "Resources/Textures/GlitchyEngineIcon.ico"; windowDesc.Title = "Test"; windoww = new Window(windowDesc); windoww.EventCallback = new => EventHandler; + + _copyEffect = Content.LoadAsset("Resources/Shaders/Copy.hlsl"); + + + NoApp(); + //RenderApp(); + } private void EventHandler(Event e) @@ -46,6 +53,8 @@ class UltralightLayer : Layer windoww = null; return true; }); + + dispatcher.Dispatch(scope => ResizeWindow); } private bool CharTypedEvent(KeyTypedEvent e) @@ -107,7 +116,8 @@ class UltralightLayer : Layer { Log.EngineLogger.Warning($"{e.PositionX}"); - ULMouseEvent evt = ulCreateMouseEvent(.kMouseEventType_MouseMoved, e.PositionX, e.PositionY, .kMouseButton_None); + ULMouseEvent evt = ulCreateMouseEvent(.kMouseEventType_MouseMoved, e.PositionX, e.PositionY, + Input.IsMouseButtonPressed(.LeftButton) ? .kMouseButton_Left : .kMouseButton_None); ulViewFireMouseEvent(view, evt); ulDestroyMouseEvent(evt); @@ -275,13 +285,45 @@ class UltralightLayer : Layer uint32 height = ulBitmapGetHeight(bitmap); uint32 stride = ulBitmapGetRowBytes(bitmap); - Span colors = Span((.)pixels, 500 * 500); - texture.SetData((.)pixels, 0, 0, width, height, 0, 0); ulBitmapUnlockPixels(bitmap); } + //bool needsResize = true; + + private void CreateTexture() + { + if (texture != null && (texture.Width == windoww.SwapChain.Width && texture.Height == windoww.SwapChain.Height)) + return; + + + Texture2DDesc desc = .(windoww.SwapChain.Width, windoww.SwapChain.Height, .B8G8R8A8_UNorm, usage: .Default, cpuAccess: .Write); + + Texture2D newTexture = new Texture2D(desc); + //newTexture.[Friend]Identifier = .("Window Target"); + newTexture.SamplerState = SamplerStateManager.PointClamp; + + //Application.Instance.ContentManager.ManageAsset(texture); + + texture?.ReleaseRef(); + texture = newTexture; + + ULIntRect bounds = ULIntRect(); + bounds.bottom = 0; + bounds.left = 0; + bounds.top = (.)desc.Height; + bounds.bottom = (.)desc.Width; + } + + bool ResizeWindow(WindowResizeEvent windowResizeEvent) + { + CreateTexture(); + ulViewResize(view, (uint32)windowResizeEvent.Width, (uint32)windowResizeEvent.Height); + + return true; + } + public override void Update(GameTime gameTime) { ulUpdate(renderer); @@ -304,7 +346,19 @@ class UltralightLayer : Layer if (windoww == null) return; - RenderCommand.Clear(windoww.SwapChain.BackBuffer, .Color | .Depth, .(0.7f, 0.2f, 0.2f), 1.0f, 0); + RenderCommand.Clear(windoww.SwapChain.BackBuffer, .Color, .(0.7f, 0.2f, 0.2f), 1.0f, 0); + + RenderCommand.UnbindRenderTargets(); + RenderCommand.SetRenderTarget(windoww.SwapChain.BackBuffer); + RenderCommand.BindRenderTargets(); + RenderCommand.SetViewport(windoww.SwapChain.BackbufferViewport); + + Effect copy = Content.GetAsset(_copyEffect); + copy.SetTexture("Texture", texture); + copy.ApplyChanges(); + copy.Bind(); + + FullscreenQuad.Draw(); } private void CreateView() @@ -312,7 +366,7 @@ class UltralightLayer : Layer ULViewConfig viewConfig = ulCreateViewConfig(); ulViewConfigSetIsAccelerated(viewConfig, false); - view = ulCreateView(renderer, 500, 500, viewConfig, null); + view = ulCreateView(renderer, windoww.SwapChain.Width, windoww.SwapChain.Height, viewConfig, null); ulViewSetDOMReadyCallback(view, => OnDOMReady, null); @@ -326,15 +380,18 @@ class UltralightLayer : Layer ulViewLoadURL(view, url); ulDestroyString(url); + CreateTexture(); + //surface = ulViewGetSurface(view); // TODO: Dynamic - Texture2DDesc desc = .(500, 500, .B8G8R8A8_UNorm, usage: .Default, cpuAccess: .Write); + /*Texture2DDesc desc = .(500, 500, .B8G8R8A8_UNorm, usage: .Default, cpuAccess: .Write); texture = new Texture2D(desc); texture.[Friend]Identifier = .("TestTexture"); + texture.SamplerState = SamplerStateManager.PointClamp; - Application.Instance.ContentManager.ManageAsset(texture); + Application.Instance.ContentManager.ManageAsset(texture);*/ } public override void OnEvent(GlitchyEngine.Events.Event event) @@ -354,7 +411,7 @@ class UltralightLayer : Layer { if (ImGui.Begin("Testlul")) { - ImGui.Image(texture, .(500, 500)); + //ImGui.Image(texture, .(500, 500)); } ImGui.End();