From fb330e33dae734cdb8cc61f335cd80d5f15bf5a5 Mon Sep 17 00:00:00 2001 From: 4sval Date: Wed, 31 Aug 2022 23:04:15 +0200 Subject: [PATCH] msaa enabled --- FModel/MainWindow.xaml.cs | 2 +- FModel/Views/Snooper/FramebufferObject.cs | 47 +++++++++++++++------- FModel/Views/Snooper/RenderbufferObject.cs | 2 +- FModel/Views/Snooper/Snooper.cs | 7 ++-- FModel/Views/Snooper/Texture.cs | 33 +++++++-------- 5 files changed, 56 insertions(+), 35 deletions(-) diff --git a/FModel/MainWindow.xaml.cs b/FModel/MainWindow.xaml.cs index cb5099c6..563c3b27 100644 --- a/FModel/MainWindow.xaml.cs +++ b/FModel/MainWindow.xaml.cs @@ -79,7 +79,7 @@ public partial class MainWindow #if DEBUG await _threadWorkerView.Begin(_ => _applicationView.CUE4Parse.Extract( - "/Game/Characters/Player/Female/Medium/Bodies/F_MED_Neon_Cat_Tech/Meshes/F_MED_Neon_Cat_Tech.uasset")); + "FortniteGame/Content/Environments/Props/Winter/Meshes/SM_ChristmasTree_Llama.uasset")); #endif } diff --git a/FModel/Views/Snooper/FramebufferObject.cs b/FModel/Views/Snooper/FramebufferObject.cs index 90852810..947a7fc6 100644 --- a/FModel/Views/Snooper/FramebufferObject.cs +++ b/FModel/Views/Snooper/FramebufferObject.cs @@ -6,7 +6,8 @@ namespace FModel.Views.Snooper; public class FramebufferObject : IDisposable { - private uint _handle; + private uint _framebufferHandle; + private uint _postProcessingHandle; private GL _gl; private readonly int _width; @@ -18,7 +19,8 @@ public class FramebufferObject : IDisposable private VertexArrayObject _vao; private Shader _shader; - private Texture _texture; + private Texture _framebufferTexture; + private Texture _postProcessingTexture; public readonly uint[] Indices = { 0, 1, 2, 3, 4, 5 }; public readonly float[] Vertices = { @@ -42,10 +44,11 @@ public class FramebufferObject : IDisposable public void Setup(GL gl) { _gl = gl; - _handle = _gl.GenFramebuffer(); - Bind(); - _texture = new Texture(_gl, (uint) _width, (uint) _height); + _framebufferHandle = _gl.GenFramebuffer(); + Bind(_framebufferHandle); + + _framebufferTexture = new Texture(_gl, (uint) _width, (uint) _height); _renderbuffer.Setup(gl); @@ -63,18 +66,32 @@ public class FramebufferObject : IDisposable var status = _gl.CheckFramebufferStatus(FramebufferTarget.Framebuffer); if (status != GLEnum.FramebufferComplete) { - throw new Exception($"Framebuffer failed to bind with error: {_gl.GetProgramInfoLog(_handle)}"); + throw new Exception($"Framebuffer failed to bind with error: {_gl.GetProgramInfoLog(_framebufferHandle)}"); + } + + _postProcessingHandle = _gl.GenFramebuffer(); + Bind(_postProcessingHandle); + + _postProcessingTexture = new Texture(_gl, _width, _height); + + status = _gl.CheckFramebufferStatus(FramebufferTarget.Framebuffer); + if (status != GLEnum.FramebufferComplete) + { + throw new Exception($"Post-Processing framebuffer failed to bind with error: {_gl.GetProgramInfoLog(_postProcessingHandle)}"); } } - public void Bind() + public void Bind() => Bind(_framebufferHandle); + public void Bind(uint handle) { - _gl.BindFramebuffer(FramebufferTarget.DrawFramebuffer, _handle); + _gl.BindFramebuffer(FramebufferTarget.Framebuffer, handle); } - public void UnBind() + public void BindMsaa() { - _gl.BindFramebuffer(FramebufferTarget.DrawFramebuffer, 0); + _gl.BindFramebuffer(FramebufferTarget.ReadFramebuffer, _framebufferHandle); + _gl.BindFramebuffer(FramebufferTarget.DrawFramebuffer, _postProcessingHandle); + _gl.BlitFramebuffer(0, 0, _width, _height, 0, 0, _width, _height, ClearBufferMask.ColorBufferBit, BlitFramebufferFilter.Nearest); } public void BindStuff() @@ -84,21 +101,23 @@ public class FramebufferObject : IDisposable _shader.Use(); _vao.Bind(); - _texture.Bind(TextureUnit.Texture0); + _postProcessingTexture.Bind(TextureUnit.Texture0); _gl.DrawArrays(PrimitiveType.Triangles, 0, (uint) Indices.Length); _gl.DepthMask(true); } - public IntPtr GetPointer() => _texture.GetPointer(); + public IntPtr GetPointer() => _postProcessingTexture.GetPointer(); public void Dispose() { _vao.Dispose(); _shader.Dispose(); - _texture.Dispose(); + _framebufferTexture.Dispose(); + _postProcessingTexture.Dispose(); _renderbuffer.Dispose(); - _gl.DeleteFramebuffer(_handle); + _gl.DeleteFramebuffer(_framebufferHandle); + _gl.DeleteFramebuffer(_postProcessingHandle); } } diff --git a/FModel/Views/Snooper/RenderbufferObject.cs b/FModel/Views/Snooper/RenderbufferObject.cs index bf1fee56..bc789e4e 100644 --- a/FModel/Views/Snooper/RenderbufferObject.cs +++ b/FModel/Views/Snooper/RenderbufferObject.cs @@ -23,7 +23,7 @@ public class RenderbufferObject : IDisposable _handle = _gl.GenRenderbuffer(); _gl.BindRenderbuffer(RenderbufferTarget.Renderbuffer, _handle); - _gl.RenderbufferStorage(RenderbufferTarget.Renderbuffer, InternalFormat.Depth24Stencil8, _width, _height); + _gl.RenderbufferStorageMultisample(RenderbufferTarget.Renderbuffer, Constants.SAMPLES_COUNT, InternalFormat.Depth24Stencil8, _width, _height); _gl.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthStencilAttachment, RenderbufferTarget.Renderbuffer, _handle); } diff --git a/FModel/Views/Snooper/Snooper.cs b/FModel/Views/Snooper/Snooper.cs index 6c0ccd08..ee8c1158 100644 --- a/FModel/Views/Snooper/Snooper.cs +++ b/FModel/Views/Snooper/Snooper.cs @@ -51,7 +51,6 @@ public class Snooper options.Size = _size = new Vector2D(Convert.ToInt32(x * ratio), Convert.ToInt32(y * ratio)); options.WindowBorder = WindowBorder.Hidden; options.Title = "Snooper"; - options.Samples = (int) Constants.SAMPLES_COUNT; _window = Silk.NET.Windowing.Window.Create(options); unsafe @@ -161,7 +160,6 @@ public class Snooper ImGuiExtensions.DrawNavbar(); ImGui.ShowDemoWindow(); - _framebuffer.BindStuff(); _skybox.Bind(_camera); _grid.Bind(_camera); @@ -189,7 +187,10 @@ public class Snooper ImGuiExtensions.DrawViewport(_framebuffer, _camera, _mouse); ImGuiExtensions.DrawFPS(); - _framebuffer.UnBind(); // switch back to main window + _framebuffer.BindMsaa(); + _framebuffer.Bind(0); // switch back to main window + _framebuffer.BindStuff(); + _controller.Render(); // render ImGui in main window } diff --git a/FModel/Views/Snooper/Texture.cs b/FModel/Views/Snooper/Texture.cs index 8e00e124..1e7840c6 100644 --- a/FModel/Views/Snooper/Texture.cs +++ b/FModel/Views/Snooper/Texture.cs @@ -22,24 +22,24 @@ public class Texture : IDisposable Bind(TextureUnit.Texture0); } - // public Texture(GL gl, uint width, uint height) : this(gl, TextureType.MsaaFramebuffer) - // { - // _gl.TexImage2DMultisample(TextureTarget.Texture2DMultisample, Constants.SAMPLES_COUNT, InternalFormat.Rgb, width, height, Silk.NET.OpenGL.Boolean.True); - // - // _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) GLEnum.Nearest); - // _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) GLEnum.Nearest); - // _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int) GLEnum.ClampToEdge); - // _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int) GLEnum.ClampToEdge); - // - // _gl.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2DMultisample, _handle, 0); - // } - - public unsafe Texture(GL gl, uint width, uint height) : this(gl, TextureType.Framebuffer) + public Texture(GL gl, uint width, uint height) : this(gl, TextureType.MsaaFramebuffer) { - _gl.TexImage2D(TextureTarget.Texture2D, 0, (int) InternalFormat.Rgb, width, height, 0, PixelFormat.Rgb, PixelType.UnsignedByte, null); + _gl.TexImage2DMultisample(TextureTarget.Texture2DMultisample, Constants.SAMPLES_COUNT, InternalFormat.Rgb, width, height, Silk.NET.OpenGL.Boolean.True); - _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) GLEnum.Nearest); - _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) GLEnum.Nearest); + _gl.TexParameter(TextureTarget.Texture2DMultisample, TextureParameterName.TextureMinFilter, (int) GLEnum.Nearest); + _gl.TexParameter(TextureTarget.Texture2DMultisample, TextureParameterName.TextureMagFilter, (int) GLEnum.Nearest); + _gl.TexParameter(TextureTarget.Texture2DMultisample, TextureParameterName.TextureWrapS, (int) GLEnum.ClampToEdge); + _gl.TexParameter(TextureTarget.Texture2DMultisample, TextureParameterName.TextureWrapT, (int) GLEnum.ClampToEdge); + + _gl.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2DMultisample, _handle, 0); + } + + public unsafe Texture(GL gl, int width, int height) : this(gl, TextureType.Framebuffer) + { + _gl.TexImage2D(TextureTarget.Texture2D, 0, (int) InternalFormat.Rgb, (uint) width, (uint) height, 0, PixelFormat.Rgb, PixelType.UnsignedByte, null); + + _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int) GLEnum.Linear); + _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int) GLEnum.Linear); _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int) GLEnum.ClampToEdge); _gl.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int) GLEnum.ClampToEdge); @@ -94,6 +94,7 @@ public class Texture : IDisposable var target = _type switch { TextureType.Cubemap => TextureTarget.TextureCubeMap, + TextureType.MsaaFramebuffer => TextureTarget.Texture2DMultisample, _ => TextureTarget.Texture2D }; _gl.BindTexture(target, _handle);