This commit is contained in:
Michel Fedde 2021-03-18 09:23:35 +01:00
commit 31777faa11
107 changed files with 1146 additions and 803 deletions

View file

@ -1,38 +1,46 @@
using System.ComponentModel;
#region usings
using OpenTK;
using OpenTK.Graphics.OpenGL4;
using SM.Base;
using SM.Base.Drawing;
using SM.Base.PostProcess;
using SM.Base.Windows;
using SM.Base.Utility;
using SM.Base.Window;
using SM.OGL.Framebuffer;
using SM.OGL.Texture;
using SM.Utility;
#endregion
namespace SM.Base.PostEffects
{
public class BloomEffect : PostProcessEffect
{
private static BezierCurve _defaultCurve = new BezierCurve(Vector2.UnitY, Vector2.Zero, new Vector2(0.2f, 0f), new Vector2(1,0));
private static BezierCurve _defaultCurve = new BezierCurve(Vector2.UnitY, Vector2.Zero, new Vector2(0.4f, 0), new Vector2(.5f,0));
private static readonly PostProcessShader _mergeShader = new PostProcessShader(
AssemblyUtility.ReadAssemblyFile(SMRenderer.PostProcessPath + ".bloom_merge_vert.glsl"),
AssemblyUtility.ReadAssemblyFile(SMRenderer.PostProcessPath + ".bloom_merge.glsl"));
private static readonly PostProcessShader _shader =
new PostProcessShader(AssemblyUtility.ReadAssemblyFile(SMRenderer.PostProcessPath + ".bloom_blur.glsl"));
private const float _defaultTextureScale = .75f;
private float _textureScale = .75f;
private Framebuffer _source;
private Framebuffer _bloomBuffer1;
private Framebuffer _bloomBuffer2;
private readonly bool _hdr;
private readonly float _textureScale = .75f;
private BezierCurve _weightCurve;
private float[] _weights;
private ColorAttachment _xBuffer;
private ColorAttachment _yBuffer;
private PostProcessShader _shader = new PostProcessShader(AssemblyUtility.ReadAssemblyFile(SMRenderer.PostProcessPath+".bloom_blur.glsl"));
private PostProcessShader _mergeShader = new PostProcessShader(AssemblyUtility.ReadAssemblyFile(SMRenderer.PostProcessPath+".bloom_merge_vert.glsl"), AssemblyUtility.ReadAssemblyFile(SMRenderer.PostProcessPath+".bloom_merge.glsl"));
private bool _hdr;
private Framebuffer _source;
private BezierCurve _weightCurve ;
private float[] _weights;
public TextureBase AmountMap;
public TextureTransformation AmountTransform = new TextureTransformation();
public int Iterations = 8;
public float Threshold = .8f;
@ -40,21 +48,10 @@ namespace SM.Base.PostEffects
public bool Enable = true;
public float MinAmount = 0;
public float MaxAmount = 1;
public TextureBase AmountMap;
public TextureTransformation AmountTransform = new TextureTransformation();
public BezierCurve WeightCurve
{
get => _weightCurve;
set
{
_weightCurve = value;
UpdateWeights();
}
}
public float MinAmount = 0;
public int WeightCurvePickAmount = 4;
@ -67,18 +64,25 @@ namespace SM.Base.PostEffects
WeightCurve = _defaultCurve;
}
public BezierCurve WeightCurve
{
get => _weightCurve;
set
{
_weightCurve = value;
UpdateWeights();
}
}
private void UpdateWeights()
{
_weights = new float[WeightCurvePickAmount];
for (int i = 0; i < WeightCurvePickAmount; i++)
{
_weights[i] = _weightCurve.CalculatePoint((float)(i + 1) / (WeightCurvePickAmount + 1)).Y;
}
_weights[i] = _weightCurve.CalculatePoint((float) (i + 1) / (WeightCurvePickAmount + 1)).Y;
}
protected override void InitProcess()
{
@ -86,10 +90,16 @@ namespace SM.Base.PostEffects
_source.ColorAttachments["color"].PixelInformation = PixelInformation.RGBA_HDR;
_bloomBuffer1 = new Framebuffer(Pipeline.ConnectedWindow, _textureScale);
_bloomBuffer1 = new Framebuffer(Pipeline.ConnectedWindow, _textureScale)
{
Name = "BloomX"
};
_bloomBuffer1.Append("xBuffer", _xBuffer = new ColorAttachment(0, PixelInformation.RGBA_HDR));
_bloomBuffer1.Compile();
_bloomBuffer2 = new Framebuffer(Pipeline.ConnectedWindow, _textureScale);
_bloomBuffer2 = new Framebuffer(Pipeline.ConnectedWindow, _textureScale)
{
Name = "BloomY"
};
_bloomBuffer2.Append("yBuffer", _yBuffer = new ColorAttachment(0, PixelInformation.RGBA_HDR));
_bloomBuffer2.Compile();
@ -101,33 +111,34 @@ namespace SM.Base.PostEffects
{
if (Enable)
{
GL.Viewport(0,0, (int)(Pipeline.ConnectedWindow.Width * _textureScale), (int)(Pipeline.ConnectedWindow.Height * _textureScale));
GL.Viewport(0, 0, (int) (Pipeline.ConnectedWindow.Width * _textureScale),
(int) (Pipeline.ConnectedWindow.Height * _textureScale));
Framebuffer target = Framebuffer.GetCurrentlyActive();
bool first = true, hoz = true;
int iter = Iterations * 2;
for (int i = 0; i < iter; i++)
{
(hoz ? _bloomBuffer1 : _bloomBuffer2).Activate();
_shader.Draw(collection =>
{
collection["renderedTexture"].SetTexture(first ? _source.ColorAttachments["color"] : (hoz ? _yBuffer : _xBuffer));
collection["First"].SetUniform1(first);
collection["Threshold"].SetUniform1(Threshold);
collection["Horizontal"].SetUniform1(hoz);
collection["Weights"].SetUniform1(_weights);
collection["WeightCount"].SetUniform1(WeightCurvePickAmount);
collection["Power"].SetUniform1(Power);
});
hoz = !hoz;
if (first) first = false;
}
GL.Viewport(Pipeline.ConnectedWindow.ClientRectangle);
target.Activate();
}

View file

@ -1,46 +0,0 @@
using System.Windows.Controls;
using OpenTK.Graphics.OpenGL4;
using SM.Base.PostProcess;
using SM.Base.Windows;
using SM.OGL.Framebuffer;
using SM.Utility;
namespace SM.Base.PostEffects
{
public class PostProcessFinals
{
static PostProcessShader _hdrExposureShader = new PostProcessShader(AssemblyUtility.ReadAssemblyFile(SMRenderer.PostProcessPath+".finalize_hdr.glsl"));
static PostProcessShader _gammaShader = new PostProcessShader(AssemblyUtility.ReadAssemblyFile(SMRenderer.PostProcessPath + ".finalize_gamma.glsl"));
public static float Gamma = 2.2f;
public static void ResolveMultisampledBuffers(Framebuffer multisampledBuffers, Framebuffer target)
{
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, BlitFramebufferFilter.Nearest);
target.Activate();
}
public static void FinalizeHDR(ColorAttachment attachment, float exposure)
{
_hdrExposureShader.Draw(u =>
{
u["Gamma"].SetUniform1(Gamma);
u["Exposure"].SetUniform1(exposure);
u["Scene"].SetTexture(attachment);
});
}
public static void FinalizeGamma(ColorAttachment attachment)
{
_gammaShader.Draw(u =>
{
u["Gamma"].SetUniform1(Gamma);
u["Scene"].SetTexture(attachment);
});
}
}
}

View file

@ -0,0 +1,73 @@
#region usings
using OpenTK.Graphics.OpenGL4;
using SM.Base.PostProcess;
using SM.Base.Utility;
using SM.OGL.Framebuffer;
#endregion
namespace SM.Base.PostEffects
{
/// <summary>
/// This class has some utility for render pipelines
/// </summary>
public static class PostProcessUtility
{
private static readonly PostProcessShader _hdrExposureShader =
new PostProcessShader(AssemblyUtility.ReadAssemblyFile(SMRenderer.PostProcessPath + ".finalize_hdr.glsl"));
private static readonly PostProcessShader _gammaShader =
new PostProcessShader(
AssemblyUtility.ReadAssemblyFile(SMRenderer.PostProcessPath + ".finalize_gamma.glsl"));
/// <summary>
/// The gamma that is used for <see cref="FinalizeGamma"/> and <see cref="FinalizeHDR"/>.
/// </summary>
public static float Gamma = 2.2f;
/// <summary>
/// This resolves a multisampled framebuffer to a non-multisampled renderbuffer.
/// </summary>
/// <param name="multisampledBuffers"></param>
/// <param name="target"></param>
public static void ResolveMultisampledBuffers(Framebuffer multisampledBuffers, Framebuffer target)
{
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,
BlitFramebufferFilter.Nearest);
target.Activate();
}
/// <summary>
/// This converts HDR to LDR and applys gamma.
/// </summary>
/// <param name="attachment"></param>
/// <param name="exposure"></param>
public static void FinalizeHDR(ColorAttachment attachment, float exposure)
{
_hdrExposureShader.Draw(u =>
{
u["Gamma"].SetUniform1(Gamma);
u["Exposure"].SetUniform1(exposure);
u["Scene"].SetTexture(attachment);
});
}
/// <summary>
/// This applys gamma
/// </summary>
/// <param name="attachment"></param>
public static void FinalizeGamma(ColorAttachment attachment)
{
_gammaShader.Draw(u =>
{
u["Gamma"].SetUniform1(Gamma);
u["Scene"].SetTexture(attachment);
});
}
}
}