From 324eb76930f49b0c03a0d66ade47e72e90fa004e Mon Sep 17 00:00:00 2001 From: Michel Fedde Date: Mon, 22 Mar 2021 13:18:52 +0100 Subject: [PATCH 1/9] Missing commit --- SMCode/SM.Base/Window/WindowCode.cs | 2 ++ SMCode/SM.OGL/Framebuffer/Framebuffer.cs | 18 +++++++++++++++--- SM_TEST/TestRenderPipeline.cs | 7 +++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/SMCode/SM.Base/Window/WindowCode.cs b/SMCode/SM.Base/Window/WindowCode.cs index 0389b09..209994b 100644 --- a/SMCode/SM.Base/Window/WindowCode.cs +++ b/SMCode/SM.Base/Window/WindowCode.cs @@ -13,6 +13,7 @@ using SM.Base.Shaders.Extensions; using SM.Base.Time; using SM.Base.Utility; using SM.OGL; +using SM.OGL.Framebuffer; using Keyboard = SM.Base.Controls.Keyboard; using Mouse = SM.Base.Controls.Mouse; @@ -26,6 +27,7 @@ namespace SM.Base.Window { GLSystem.INIT_SYSTEM(); GLSettings.ShaderPreProcessing = true; + Framebuffer.ScreenWindow = window; var args = Environment.GetCommandLineArgs(); if (args.Contains("--advDebugging")) diff --git a/SMCode/SM.OGL/Framebuffer/Framebuffer.cs b/SMCode/SM.OGL/Framebuffer/Framebuffer.cs index cbf5eae..5eefede 100644 --- a/SMCode/SM.OGL/Framebuffer/Framebuffer.cs +++ b/SMCode/SM.OGL/Framebuffer/Framebuffer.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; +using System.Linq; using OpenTK; using OpenTK.Graphics.OpenGL4; @@ -16,6 +17,8 @@ namespace SM.OGL.Framebuffer { protected override bool AutoCompile { get; set; } = true; + public static IFramebufferWindow ScreenWindow; + /// /// Represents the screen buffer. /// @@ -23,6 +26,8 @@ namespace SM.OGL.Framebuffer { _id = 0, CanCompile = false, + _window = ScreenWindow, + _windowScale = 1, }; private IFramebufferWindow _window; @@ -42,7 +47,7 @@ namespace SM.OGL.Framebuffer public Dictionary ColorAttachments { get; private set; } = new Dictionary(); - public List RenderbufferAttachments { get; } = new List(); + public Dictionary RenderbufferAttachments { get; } = new Dictionary(); /// /// Creates a buffer without any options. @@ -75,6 +80,7 @@ namespace SM.OGL.Framebuffer /// public override void Compile() { + if (_id == 0) _window = ScreenWindow; if (_window != null) Size = new Vector2(_window.Width * _windowScale, _window.Height * _windowScale); base.Compile(); @@ -95,10 +101,11 @@ namespace SM.OGL.Framebuffer GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, pair.Value.FramebufferAttachment, pair.Value.Target, pair.Value.ID, 0); - foreach (RenderbufferAttachment attachment in RenderbufferAttachments) + foreach (RenderbufferAttachment attachment in RenderbufferAttachments.Keys.ToArray()) { int att = attachment.Generate(this); GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, attachment.FramebufferAttachment, RenderbufferTarget.Renderbuffer, att); + RenderbufferAttachments[attachment] = att; } var err = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer); @@ -114,6 +121,11 @@ namespace SM.OGL.Framebuffer { foreach (var attachment in ColorAttachments.Values) attachment.Dispose(); + foreach (KeyValuePair pair in RenderbufferAttachments.ToArray()) + { + GL.DeleteRenderbuffer(pair.Value); + RenderbufferAttachments[pair.Key] = -1; + } GL.DeleteFramebuffer(this); base.Dispose(); @@ -133,7 +145,7 @@ namespace SM.OGL.Framebuffer public void AppendRenderbuffer(RenderbufferAttachment attachment) { - RenderbufferAttachments.Add(attachment); + RenderbufferAttachments.Add(attachment, -1); } /// diff --git a/SM_TEST/TestRenderPipeline.cs b/SM_TEST/TestRenderPipeline.cs index fe53e1c..0a5c20c 100644 --- a/SM_TEST/TestRenderPipeline.cs +++ b/SM_TEST/TestRenderPipeline.cs @@ -2,6 +2,7 @@ using SM.Base.PostEffects; using SM.Base.Window; using SM.OGL.Framebuffer; +using SM.OGL.Texture; namespace SM_TEST { @@ -13,9 +14,9 @@ namespace SM_TEST public override void Initialization() { - MainFramebuffer = CreateWindowFramebuffer(16); + MainFramebuffer = CreateWindowFramebuffer(0, true, PixelInformation.RGBA_HDR); - _postBuffer = CreateWindowFramebuffer(); + _postBuffer = CreateWindowFramebuffer(0, false, PixelInformation.RGBA_HDR); Framebuffers.Add(_postBuffer); _bloom = new BloomEffect(MainFramebuffer, hdr: true, .5f) { @@ -33,8 +34,10 @@ namespace SM_TEST context.Scene.DrawBackground(context); context.Scene.DrawMainObjects(context); context.Scene.DrawHUD(context); + Framebuffer.Screen.Activate(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); + _bloom.Draw(context); context.Scene.DrawDebug(context); From 7ffe566f9b16ea3e77bdee38ab0cf000ac616082 Mon Sep 17 00:00:00 2001 From: Michel Fedde Date: Mon, 22 Mar 2021 13:21:12 +0100 Subject: [PATCH 2/9] Merge Fixed --- SMCode/SM.OGL/Framebuffer/Framebuffer.cs | 4 ++-- SM_TEST/TestRenderPipeline.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/SMCode/SM.OGL/Framebuffer/Framebuffer.cs b/SMCode/SM.OGL/Framebuffer/Framebuffer.cs index 9ed8b77..e498b67 100644 --- a/SMCode/SM.OGL/Framebuffer/Framebuffer.cs +++ b/SMCode/SM.OGL/Framebuffer/Framebuffer.cs @@ -31,8 +31,8 @@ namespace SM.OGL.Framebuffer _windowScale = 1, }; - private readonly IFramebufferWindow _window; - private readonly float _windowScale; + private IFramebufferWindow _window; + private float _windowScale; /// public override ObjectLabelIdentifier TypeIdentifier { get; } = ObjectLabelIdentifier.Framebuffer; diff --git a/SM_TEST/TestRenderPipeline.cs b/SM_TEST/TestRenderPipeline.cs index e4b47d7..8ce9791 100644 --- a/SM_TEST/TestRenderPipeline.cs +++ b/SM_TEST/TestRenderPipeline.cs @@ -14,9 +14,9 @@ namespace SM_TEST public override void Initialization() { - MainFramebuffer = CreateWindowFramebuffer(0, true, PixelInformation.RGBA_HDR); + MainFramebuffer = CreateWindowFramebuffer(0); - _postBuffer = CreateWindowFramebuffer(0, false, PixelInformation.RGBA_HDR); + _postBuffer = CreateWindowFramebuffer(0); Framebuffers.Add(_postBuffer); _bloom = new BloomEffect(MainFramebuffer, hdr: true, .75f) { From d17d1ac7658b51a0382ea6f7f016ee1fe767c1a8 Mon Sep 17 00:00:00 2001 From: Michel Fedde Date: Wed, 24 Mar 2021 11:21:39 +0100 Subject: [PATCH 3/9] Added more options to CreateWindowFramebuffer --- SMCode/SM.Base/SM.Base.csproj | 1 - SMCode/SM.Base/Window/RenderPipeline.cs | 17 ++++++++++------- SMCode/SM.OGL/Framebuffer/Framebuffer.cs | 3 +++ SMCode/SM2D/SM2D.csproj | 17 ++++++++--------- SM_TEST/SM_TEST.csproj | 5 ++++- 5 files changed, 25 insertions(+), 18 deletions(-) diff --git a/SMCode/SM.Base/SM.Base.csproj b/SMCode/SM.Base/SM.Base.csproj index ee29747..c2abcad 100644 --- a/SMCode/SM.Base/SM.Base.csproj +++ b/SMCode/SM.Base/SM.Base.csproj @@ -121,7 +121,6 @@ SM.OGL - diff --git a/SMCode/SM.Base/Window/RenderPipeline.cs b/SMCode/SM.Base/Window/RenderPipeline.cs index 649d11a..f0b5006 100644 --- a/SMCode/SM.Base/Window/RenderPipeline.cs +++ b/SMCode/SM.Base/Window/RenderPipeline.cs @@ -79,7 +79,7 @@ namespace SM.Base.Window foreach (var framebuffer in Framebuffers) framebuffer.Dispose(); - Thread.Sleep(50); + Thread.Sleep(100); foreach (var framebuffer in Framebuffers) framebuffer.Compile(); @@ -90,14 +90,17 @@ namespace SM.Base.Window /// /// /// - public Framebuffer CreateWindowFramebuffer(int multisamples = 0) + public Framebuffer CreateWindowFramebuffer(int multisamples = 0, PixelInformation? pixelInformation = null, bool depth = true) { Framebuffer framebuffer = new Framebuffer(ConnectedWindow); - framebuffer.Append("color", new ColorAttachment(0, PixelInformation.RGBA_LDR, multisamples)); - - RenderbufferAttachment depthAttach = RenderbufferAttachment.Depth; - depthAttach.Multisample = multisamples; - framebuffer.AppendRenderbuffer(depthAttach); + framebuffer.Append("color", new ColorAttachment(0, pixelInformation.GetValueOrDefault(PixelInformation.RGBA_LDR), multisamples)); + + if (depth) + { + RenderbufferAttachment depthAttach = RenderbufferAttachment.Depth; + depthAttach.Multisample = multisamples; + framebuffer.AppendRenderbuffer(depthAttach); + } return framebuffer; } diff --git a/SMCode/SM.OGL/Framebuffer/Framebuffer.cs b/SMCode/SM.OGL/Framebuffer/Framebuffer.cs index e498b67..c5d40a1 100644 --- a/SMCode/SM.OGL/Framebuffer/Framebuffer.cs +++ b/SMCode/SM.OGL/Framebuffer/Framebuffer.cs @@ -18,6 +18,9 @@ namespace SM.OGL.Framebuffer /// protected override bool AutoCompile { get; set; } = true; + /// + /// The window for the screen + /// public static IFramebufferWindow ScreenWindow; /// diff --git a/SMCode/SM2D/SM2D.csproj b/SMCode/SM2D/SM2D.csproj index f4b25f5..d58b4f3 100644 --- a/SMCode/SM2D/SM2D.csproj +++ b/SMCode/SM2D/SM2D.csproj @@ -53,6 +53,14 @@ + + + 3.3.1 + + + + + {8e733844-4204-43e7-b3dc-3913cddabb0d} @@ -63,14 +71,5 @@ SM.OGL - - - 3.3.1 - - - - - - \ No newline at end of file diff --git a/SM_TEST/SM_TEST.csproj b/SM_TEST/SM_TEST.csproj index b84ebe5..5cd19ac 100644 --- a/SM_TEST/SM_TEST.csproj +++ b/SM_TEST/SM_TEST.csproj @@ -5,7 +5,7 @@ Debug AnyCPU {6D4FB8E6-4D0B-4928-8F9E-EF5C2FBF44E8} - Exe + WinExe SM_TEST SM_TEST v4.5.2 @@ -32,6 +32,9 @@ prompt 4 + + + ..\packages\OpenTK.3.3.1\lib\net20\OpenTK.dll From c49247043057db3c475bce3f37f13152b9597a31 Mon Sep 17 00:00:00 2001 From: Michel Fedde Date: Wed, 24 Mar 2021 11:27:54 +0100 Subject: [PATCH 4/9] Added a field to provied the last drawing camera to DrawingBasis --- SMCode/SM.Base/Drawing/DrawingBasis.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/SMCode/SM.Base/Drawing/DrawingBasis.cs b/SMCode/SM.Base/Drawing/DrawingBasis.cs index 1cd76ce..7aa9180 100644 --- a/SMCode/SM.Base/Drawing/DrawingBasis.cs +++ b/SMCode/SM.Base/Drawing/DrawingBasis.cs @@ -15,6 +15,11 @@ namespace SM.Base.Drawing /// public abstract class DrawingBasis : IShowItem, IModelItem { + /// + /// The camera, that was used last time the object was rendered. + /// + public GenericCamera LastDrawingCamera; + /// /// The material it should use. /// @@ -86,6 +91,8 @@ namespace SM.Base.Drawing context.ForcedType = ForcedMeshType; context.TextureMatrix *= TextureTransform.GetMatrix(); context.LastObject = this; + + LastDrawingCamera = context.UseCamera; } } From 7ea788534cdba715ed14f0e17500581157c6b841 Mon Sep 17 00:00:00 2001 From: Michel Fedde Date: Wed, 24 Mar 2021 13:08:27 +0100 Subject: [PATCH 5/9] Improved Bloom Effect by adding a Radius-parameter --- SMCode/SM.Base/PostEffects/BloomEffect.cs | 8 ++++++++ SMCode/SM.Base/PostEffects/Shaders/bloom_blur.glsl | 6 ++++-- SM_TEST/Program.cs | 3 +++ SM_TEST/TestRenderPipeline.cs | 3 ++- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/SMCode/SM.Base/PostEffects/BloomEffect.cs b/SMCode/SM.Base/PostEffects/BloomEffect.cs index f5926ca..be90afd 100644 --- a/SMCode/SM.Base/PostEffects/BloomEffect.cs +++ b/SMCode/SM.Base/PostEffects/BloomEffect.cs @@ -77,6 +77,12 @@ namespace SM.Base.PostEffects /// public float Power = 1; + /// + /// Radius of the effect + /// Default: 2 + /// + public float Radius = 2; + /// /// This can disable the bloom calculation. /// Default: true @@ -175,6 +181,8 @@ namespace SM.Base.PostEffects collection["Weights"].SetUniform1(_weights); collection["WeightCount"].SetUniform1(WeightCurvePickAmount); collection["Power"].SetUniform1(Power); + + collection["Radius"].SetUniform1(_textureScale * Radius); }); hoz = !hoz; diff --git a/SMCode/SM.Base/PostEffects/Shaders/bloom_blur.glsl b/SMCode/SM.Base/PostEffects/Shaders/bloom_blur.glsl index 0393f55..4943dd6 100644 --- a/SMCode/SM.Base/PostEffects/Shaders/bloom_blur.glsl +++ b/SMCode/SM.Base/PostEffects/Shaders/bloom_blur.glsl @@ -13,6 +13,8 @@ uniform float[32] Weights; uniform int WeightCount; uniform float Power; +uniform float Radius; + layout(location = 0) out vec4 color; vec4 GetRenderColorOffset(vec2 offset); @@ -37,8 +39,8 @@ void main() { result *= GetWeight(0); for(int i = 1; i < WeightCount; i++) { - result += max(GetRenderColorOffset(tex_offset * i).rgb - thres, 0) * (First ? Power : 1) * GetWeight(i); - result += max(GetRenderColorOffset(-tex_offset * i).rgb - thres, 0) * (First ? Power : 1) * GetWeight(i); + result += max(GetRenderColorOffset(tex_offset * i * Radius).rgb - thres, 0) * (First ? Power : 1) * GetWeight(i); + result += max(GetRenderColorOffset(-tex_offset * i * Radius).rgb - thres, 0) * (First ? Power : 1) * GetWeight(i); } color = vec4(result, 1); diff --git a/SM_TEST/Program.cs b/SM_TEST/Program.cs index 6fc29b1..3e49a51 100644 --- a/SM_TEST/Program.cs +++ b/SM_TEST/Program.cs @@ -23,8 +23,11 @@ namespace SM_TEST { window = new GLWindow(); window.ApplySetup(new Window2DSetup()); + window.SetRenderPipeline(new TestRenderPipeline()); + window.SetScene(scene = new Scene()); scene.Objects.Add(new DrawObject2D()); + window.UpdateFrame += WindowOnUpdateFrame; window.Run(); diff --git a/SM_TEST/TestRenderPipeline.cs b/SM_TEST/TestRenderPipeline.cs index 8ce9791..cde2f5b 100644 --- a/SM_TEST/TestRenderPipeline.cs +++ b/SM_TEST/TestRenderPipeline.cs @@ -18,9 +18,10 @@ namespace SM_TEST _postBuffer = CreateWindowFramebuffer(0); Framebuffers.Add(_postBuffer); - _bloom = new BloomEffect(MainFramebuffer, hdr: true, .75f) + _bloom = new BloomEffect(MainFramebuffer, hdr: true, .5f) { Threshold = .5f, + Radius = 5 }; From ddd2171da29ebf1f5f5624295a99ec15a53c57d1 Mon Sep 17 00:00:00 2001 From: Michel Fedde Date: Wed, 24 Mar 2021 13:53:29 +0100 Subject: [PATCH 6/9] Fixed MSAA --- SMCode/SM.Base/PostEffects/BloomEffect.cs | 2 +- SMCode/SM.Base/PostEffects/PostProcessUtility.cs | 3 ++- SMCode/SM.OGL/Framebuffer/Framebuffer.cs | 2 ++ SM_TEST/Program.cs | 8 ++++++++ SM_TEST/TestRenderPipeline.cs | 15 ++++++++------- 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/SMCode/SM.Base/PostEffects/BloomEffect.cs b/SMCode/SM.Base/PostEffects/BloomEffect.cs index be90afd..42eeed5 100644 --- a/SMCode/SM.Base/PostEffects/BloomEffect.cs +++ b/SMCode/SM.Base/PostEffects/BloomEffect.cs @@ -133,7 +133,7 @@ namespace SM.Base.PostEffects /// protected override void InitProcess() { - _source = Pipeline.MainFramebuffer; + _source ??= Pipeline.MainFramebuffer; _source.ColorAttachments["color"].PixelInformation = PixelInformation.RGBA_HDR; diff --git a/SMCode/SM.Base/PostEffects/PostProcessUtility.cs b/SMCode/SM.Base/PostEffects/PostProcessUtility.cs index e8c7b31..55bef36 100644 --- a/SMCode/SM.Base/PostEffects/PostProcessUtility.cs +++ b/SMCode/SM.Base/PostEffects/PostProcessUtility.cs @@ -28,6 +28,7 @@ namespace SM.Base.PostEffects /// /// This resolves a multisampled framebuffer to a non-multisampled renderbuffer. + /// This removes the depth buffer. /// /// /// @@ -36,7 +37,7 @@ namespace SM.Base.PostEffects multisampledBuffers.Activate(FramebufferTarget.ReadFramebuffer); target.Activate(FramebufferTarget.DrawFramebuffer); GL.BlitFramebuffer(0, 0, (int) multisampledBuffers.Size.X, (int) multisampledBuffers.Size.Y, 0, 0, - (int) target.Size.X, (int) target.Size.Y, ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit, + (int) target.Size.X, (int) target.Size.Y, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Nearest); target.Activate(); diff --git a/SMCode/SM.OGL/Framebuffer/Framebuffer.cs b/SMCode/SM.OGL/Framebuffer/Framebuffer.cs index c5d40a1..8ffc5f1 100644 --- a/SMCode/SM.OGL/Framebuffer/Framebuffer.cs +++ b/SMCode/SM.OGL/Framebuffer/Framebuffer.cs @@ -55,6 +55,8 @@ namespace SM.OGL.Framebuffer /// public Dictionary RenderbufferAttachments { get; } = new Dictionary(); + public ColorAttachment this[string colorName] => ColorAttachments[colorName]; + /// /// Creates a buffer without any options. /// diff --git a/SM_TEST/Program.cs b/SM_TEST/Program.cs index 3e49a51..e0e3412 100644 --- a/SM_TEST/Program.cs +++ b/SM_TEST/Program.cs @@ -27,6 +27,14 @@ namespace SM_TEST window.SetScene(scene = new Scene()); scene.Objects.Add(new DrawObject2D()); + scene.Objects.Add(new DrawObject2D() + { + Transform = + { + Position = new SM.Base.Types.CVector2(20,20), + ZIndex = new SM.Base.Types.CVector1(1) + } + }); window.UpdateFrame += WindowOnUpdateFrame; window.Run(); diff --git a/SM_TEST/TestRenderPipeline.cs b/SM_TEST/TestRenderPipeline.cs index cde2f5b..8d540b7 100644 --- a/SM_TEST/TestRenderPipeline.cs +++ b/SM_TEST/TestRenderPipeline.cs @@ -14,14 +14,13 @@ namespace SM_TEST public override void Initialization() { - MainFramebuffer = CreateWindowFramebuffer(0); + MainFramebuffer = CreateWindowFramebuffer(16, PixelInformation.RGBA_HDR); - _postBuffer = CreateWindowFramebuffer(0); + _postBuffer = CreateWindowFramebuffer(0, depth: false); Framebuffers.Add(_postBuffer); - _bloom = new BloomEffect(MainFramebuffer, hdr: true, .5f) + _bloom = new BloomEffect(_postBuffer, hdr: true, .5f) { - Threshold = .5f, - Radius = 5 + Threshold = .5f }; @@ -36,10 +35,12 @@ namespace SM_TEST context.Scene.DrawMainObjects(context); context.Scene.DrawHUD(context); - - Framebuffer.Screen.Activate(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); + PostProcessUtility.ResolveMultisampledBuffers(MainFramebuffer, _postBuffer); _bloom.Draw(context); + Framebuffer.Screen.Activate(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); + + PostProcessUtility.FinalizeHDR(_postBuffer["color"], 1); context.Scene.DrawDebug(context); } From 9fa1ac6ad98379ab7987ae562e9e2f5fb1e5a802 Mon Sep 17 00:00:00 2001 From: Michel Fedde Date: Wed, 24 Mar 2021 14:02:48 +0100 Subject: [PATCH 7/9] Fragment brightness has now a effect on the radius --- SMCode/SM.Base/PostEffects/BloomEffect.cs | 2 +- SMCode/SM.Base/PostEffects/Shaders/bloom_blur.glsl | 9 +++++---- SM_TEST/TestRenderPipeline.cs | 6 +++--- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/SMCode/SM.Base/PostEffects/BloomEffect.cs b/SMCode/SM.Base/PostEffects/BloomEffect.cs index 42eeed5..095fdb7 100644 --- a/SMCode/SM.Base/PostEffects/BloomEffect.cs +++ b/SMCode/SM.Base/PostEffects/BloomEffect.cs @@ -81,7 +81,7 @@ namespace SM.Base.PostEffects /// Radius of the effect /// Default: 2 /// - public float Radius = 2; + public float Radius = 1; /// /// This can disable the bloom calculation. diff --git a/SMCode/SM.Base/PostEffects/Shaders/bloom_blur.glsl b/SMCode/SM.Base/PostEffects/Shaders/bloom_blur.glsl index 4943dd6..834ad23 100644 --- a/SMCode/SM.Base/PostEffects/Shaders/bloom_blur.glsl +++ b/SMCode/SM.Base/PostEffects/Shaders/bloom_blur.glsl @@ -35,12 +35,13 @@ void main() { vec2 tex_offset = 1.0 / textureSize(renderedTexture, 0) * vec2(Horizontal ? 1 : 0, Horizontal ? 0 : 1); - vec3 result = max(GetRenderColorOffset(vec2(0)).rgb - thres, 0) * (First ? Power : 1); - result *= GetWeight(0); + vec3 result = max(GetRenderColorOffset(vec2(0)).rgb - thres, 0) * (First ? Power : 1) * GetWeight(0); + + float radi = Radius + (length(result)); for(int i = 1; i < WeightCount; i++) { - result += max(GetRenderColorOffset(tex_offset * i * Radius).rgb - thres, 0) * (First ? Power : 1) * GetWeight(i); - result += max(GetRenderColorOffset(-tex_offset * i * Radius).rgb - thres, 0) * (First ? Power : 1) * GetWeight(i); + result += max(GetRenderColorOffset(tex_offset * i * radi).rgb - thres, 0) * (First ? Power : 1) * GetWeight(i); + result += max(GetRenderColorOffset(-tex_offset * i * radi).rgb - thres, 0) * (First ? Power : 1) * GetWeight(i); } color = vec4(result, 1); diff --git a/SM_TEST/TestRenderPipeline.cs b/SM_TEST/TestRenderPipeline.cs index 8d540b7..117db39 100644 --- a/SM_TEST/TestRenderPipeline.cs +++ b/SM_TEST/TestRenderPipeline.cs @@ -16,11 +16,11 @@ namespace SM_TEST MainFramebuffer = CreateWindowFramebuffer(16, PixelInformation.RGBA_HDR); - _postBuffer = CreateWindowFramebuffer(0, depth: false); + _postBuffer = CreateWindowFramebuffer(0, PixelInformation.RGBA_HDR, depth: false); Framebuffers.Add(_postBuffer); _bloom = new BloomEffect(_postBuffer, hdr: true, .5f) { - Threshold = .5f + Threshold = .8f, }; @@ -40,7 +40,7 @@ namespace SM_TEST _bloom.Draw(context); Framebuffer.Screen.Activate(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); - PostProcessUtility.FinalizeHDR(_postBuffer["color"], 1); + PostProcessUtility.FinalizeHDR(_postBuffer["color"], .5f); context.Scene.DrawDebug(context); } From 58c5bafa1ba21fe21213f2c7a253454624a9e447 Mon Sep 17 00:00:00 2001 From: Michel Fedde Date: Wed, 24 Mar 2021 14:54:49 +0100 Subject: [PATCH 8/9] + Mesh updating --- SMCode/SM.OGL/Mesh/GenericMesh.cs | 23 +++++++++++++++++++++ SMCode/SM.OGL/Mesh/VBO.cs | 33 +++++++++++++++++++++++++++++-- SM_TEST/Program.cs | 30 ++++++++++++++-------------- SM_TEST/TestRenderPipeline.cs | 2 +- 4 files changed, 70 insertions(+), 18 deletions(-) diff --git a/SMCode/SM.OGL/Mesh/GenericMesh.cs b/SMCode/SM.OGL/Mesh/GenericMesh.cs index cadd423..96e4b76 100644 --- a/SMCode/SM.OGL/Mesh/GenericMesh.cs +++ b/SMCode/SM.OGL/Mesh/GenericMesh.cs @@ -103,6 +103,29 @@ namespace SM.OGL.Mesh GL.BindVertexArray(0); } + /// + /// This updates the parts of the mesh, that needs updating. + /// + public void Update() + { + if (!WasCompiled) + { + Compile(); + return; + } + + GL.BindVertexArray(_id); + + UpdateBoundingBox(); + + foreach(var attrib in Attributes) + { + if (attrib.ConnectedVBO == null || !attrib.ConnectedVBO.Active || !attrib.ConnectedVBO.CanBeUpdated) continue; + attrib.ConnectedVBO.Update(); + } + + } + /// public override void Dispose() { diff --git a/SMCode/SM.OGL/Mesh/VBO.cs b/SMCode/SM.OGL/Mesh/VBO.cs index 738bb60..1fee8cc 100644 --- a/SMCode/SM.OGL/Mesh/VBO.cs +++ b/SMCode/SM.OGL/Mesh/VBO.cs @@ -1,5 +1,6 @@ #region usings +using System; using System.Collections.Generic; using OpenTK; using OpenTK.Graphics; @@ -14,6 +15,11 @@ namespace SM.OGL.Mesh /// public class VBO : List { + /// + /// The ID for the buffer. + /// + public int BufferID { get; private set; } + /// /// Specifies the expected usage pattern of the data store. /// @@ -51,6 +57,11 @@ namespace SM.OGL.Mesh /// public VertexAttribPointerType PointerType; + /// + /// If true it can be updated, otherwise it will get ignored, when the mesh gets updated. + /// + public bool CanBeUpdated = false; + /// /// Generates a VBO for inserting mesh data. /// @@ -91,12 +102,18 @@ namespace SM.OGL.Mesh Normalised = normalised; } + public void Add(float x) + { + CanBeUpdated = true; + } + /// /// Adds two values to the VBO. /// public void Add(float x, float y) { AddRange(new[] {x, y}); + CanBeUpdated = true; } /// @@ -105,6 +122,7 @@ namespace SM.OGL.Mesh public void Add(float x, float y, float z) { AddRange(new[] {x, y, z}); + CanBeUpdated = true; } /// @@ -113,6 +131,7 @@ namespace SM.OGL.Mesh public void Add(float x, float y, float z, float w) { AddRange(new[] {x, y, z, w}); + CanBeUpdated = true; } /// @@ -205,13 +224,23 @@ namespace SM.OGL.Mesh var data = ToArray(); - var buffer = GL.GenBuffer(); - GL.BindBuffer(BufferTarget.ArrayBuffer, buffer); + BufferID = GL.GenBuffer(); + GL.BindBuffer(BufferTarget.ArrayBuffer, BufferID); GL.BufferData(BufferTarget.ArrayBuffer, data.Length * sizeof(float), data, BufferUsageHint); GL.VertexAttribPointer(attribID, PointerSize, PointerType, Normalised, PointerStride, PointerOffset); GL.EnableVertexAttribArray(attribID); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); + + CanBeUpdated = false; + } + + internal void Update() + { + var data = ToArray(); + + GL.BindBuffer(BufferTarget.ArrayBuffer, BufferID); + GL.BufferSubData(BufferTarget.ArrayBuffer, IntPtr.Zero, data.Length * sizeof(float), data); } } } \ No newline at end of file diff --git a/SM_TEST/Program.cs b/SM_TEST/Program.cs index e0e3412..6feb15a 100644 --- a/SM_TEST/Program.cs +++ b/SM_TEST/Program.cs @@ -4,9 +4,12 @@ using OpenTK; using OpenTK.Graphics; using OpenTK.Input; using SM.Base; +using SM.Base.Time; using SM.Base.Window; using SM2D; +using SM2D.Controls; using SM2D.Drawing; +using SM2D.Object; using SM2D.Pipelines; using SM2D.Scene; using Font = SM.Base.Drawing.Text.Font; @@ -19,6 +22,7 @@ namespace SM_TEST static Scene scene; private static Font font; private static GLWindow window; + private static PolyLine line; static void Main(string[] args) { window = new GLWindow(); @@ -26,15 +30,14 @@ namespace SM_TEST window.SetRenderPipeline(new TestRenderPipeline()); window.SetScene(scene = new Scene()); - scene.Objects.Add(new DrawObject2D()); - scene.Objects.Add(new DrawObject2D() + + line = new PolyLine(new Vector2[] { Vector2.Zero, Vector2.One }, PolyLineType.Connected); + var display = new DrawObject2D() { - Transform = - { - Position = new SM.Base.Types.CVector2(20,20), - ZIndex = new SM.Base.Types.CVector1(1) - } - }); + Mesh = line + }; + display.Transform.Size.Set(1); + scene.Objects.Add(display); window.UpdateFrame += WindowOnUpdateFrame; window.Run(); @@ -44,13 +47,10 @@ namespace SM_TEST private static void WindowOnUpdateFrame(object sender, FrameEventArgs e) { - if (SM.Base.Controls.Keyboard.IsDown(Key.F, true)) - { - window.WindowFlags = WindowFlags.ExclusiveFullscreen; - window.ChangeFullscreenResolution(DisplayDevice.Default.SelectResolution(1280,720, DisplayDevice.Default.BitsPerPixel, DisplayDevice.Default.RefreshRate)); - } - if (SM.Base.Controls.Keyboard.IsDown(Key.W, true)) window.WindowFlags = WindowFlags.Window; - if (SM.Base.Controls.Keyboard.IsDown(Key.B, true)) window.WindowFlags = WindowFlags.BorderlessWindow; + + line.Vertex.RemoveRange(3, 3); + line.Vertex.Add(Mouse2D.InWorld(window.ViewportCamera as Camera), 0); + line.Update(); } diff --git a/SM_TEST/TestRenderPipeline.cs b/SM_TEST/TestRenderPipeline.cs index 117db39..9e3d3c3 100644 --- a/SM_TEST/TestRenderPipeline.cs +++ b/SM_TEST/TestRenderPipeline.cs @@ -20,7 +20,7 @@ namespace SM_TEST Framebuffers.Add(_postBuffer); _bloom = new BloomEffect(_postBuffer, hdr: true, .5f) { - Threshold = .8f, + Threshold = .5f, }; From eb8e1c639fd734d790149a287dde1ee90df361be Mon Sep 17 00:00:00 2001 From: Michel Fedde Date: Wed, 24 Mar 2021 15:14:52 +0100 Subject: [PATCH 9/9] Added Compile-methods to renderpipeline --- SMCode/SM.Base/Window/RenderPipeline.cs | 31 ++++++++++++++++++++++--- SM_TEST/Program.cs | 9 ++++++- SM_TEST/TestRenderPipeline.cs | 2 +- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/SMCode/SM.Base/Window/RenderPipeline.cs b/SMCode/SM.Base/Window/RenderPipeline.cs index f0b5006..a02ab36 100644 --- a/SMCode/SM.Base/Window/RenderPipeline.cs +++ b/SMCode/SM.Base/Window/RenderPipeline.cs @@ -75,14 +75,39 @@ namespace SM.Base.Window /// public virtual void Resize() { - if (Framebuffers == null) return; + Recompile(); + } + + + /// + /// Compiles the framebuffers. + /// + public void Compile() + { foreach (var framebuffer in Framebuffers) - framebuffer.Dispose(); + framebuffer.Compile(); + } + + /// + /// Recompiles the pipeline. + /// + public void Recompile() + { + if (Framebuffers == null) return; + Dispose(); Thread.Sleep(100); + Compile(); + } + + /// + /// Disposes unmanaged resources like Framebuffers. + /// + public void Dispose() + { foreach (var framebuffer in Framebuffers) - framebuffer.Compile(); + framebuffer.Dispose(); } /// diff --git a/SM_TEST/Program.cs b/SM_TEST/Program.cs index 6feb15a..63afed2 100644 --- a/SM_TEST/Program.cs +++ b/SM_TEST/Program.cs @@ -25,7 +25,7 @@ namespace SM_TEST private static PolyLine line; static void Main(string[] args) { - window = new GLWindow(); + window = new GLWindow(1280, 720, "0ms", WindowFlags.Window, VSyncMode.Off); window.ApplySetup(new Window2DSetup()); window.SetRenderPipeline(new TestRenderPipeline()); @@ -40,11 +40,17 @@ namespace SM_TEST scene.Objects.Add(display); window.UpdateFrame += WindowOnUpdateFrame; + window.RenderFrame += Window_RenderFrame; window.Run(); Debug.WriteLine("Window Closed"); } + private static void Window_RenderFrame(object sender, FrameEventArgs e) + { + window.Title = Math.Floor(e.Time * 1000) + "ms"; + } + private static void WindowOnUpdateFrame(object sender, FrameEventArgs e) { @@ -52,6 +58,7 @@ namespace SM_TEST line.Vertex.Add(Mouse2D.InWorld(window.ViewportCamera as Camera), 0); line.Update(); + } private static void WindowOnLoad(IGenericWindow window) diff --git a/SM_TEST/TestRenderPipeline.cs b/SM_TEST/TestRenderPipeline.cs index 9e3d3c3..7191e71 100644 --- a/SM_TEST/TestRenderPipeline.cs +++ b/SM_TEST/TestRenderPipeline.cs @@ -37,7 +37,7 @@ namespace SM_TEST PostProcessUtility.ResolveMultisampledBuffers(MainFramebuffer, _postBuffer); - _bloom.Draw(context); + //_bloom.Draw(context); Framebuffer.Screen.Activate(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); PostProcessUtility.FinalizeHDR(_postBuffer["color"], .5f);