smrendererv3/SMCode/SM.Base/Window/RenderPipeline.cs
2021-03-24 15:14:52 +01:00

133 lines
No EOL
3.8 KiB
C#

#region usings
using System.Collections.Generic;
using System.Threading;
using SM.Base.Drawing;
using SM.Base.Shaders;
using SM.Base.Utility;
using SM.OGL.Framebuffer;
using SM.OGL.Texture;
#endregion
namespace SM.Base.Window
{
/// <summary>
/// This represents a render pipeline.
/// </summary>
public abstract class RenderPipeline : IInitializable
{
/// <summary>
/// This contains the windows its connected to.
/// </summary>
public IGenericWindow ConnectedWindow { get; internal set; }
/// <summary>
/// This should contains the framebuffer, when any back buffer effects are used.
/// </summary>
public Framebuffer MainFramebuffer { get; protected set; }
/// <summary>
/// This list contains all framebuffers that are required to update.
/// </summary>
public List<Framebuffer> Framebuffers { get; } = new List<Framebuffer>();
/// <summary>
/// This contains the default shader.
/// <para>Default: <see cref="SMRenderer.DefaultMaterialShader"/></para>
/// </summary>
public virtual MaterialShader DefaultShader { get; protected set; }
/// <summary>
/// Here you can set a default material.
/// </summary>
public virtual Material DefaultMaterial { get; protected set; }
/// <inheritdoc/>
public bool IsInitialized { get; set; }
/// <inheritdoc/>
public virtual void Activate()
{
}
/// <inheritdoc/>
public virtual void Initialization()
{
if (MainFramebuffer != null) {
Framebuffers.Add(MainFramebuffer);
MainFramebuffer.Name = GetType().Name + ".MainFramebuffer";
}
DefaultShader ??= SMRenderer.DefaultMaterialShader;
}
internal void Render(ref DrawContext context)
{
RenderProcess(ref context);
}
/// <summary>
/// The process of rendering.
/// </summary>
protected abstract void RenderProcess(ref DrawContext context);
/// <summary>
/// The event when resizing.
/// </summary>
public virtual void Resize()
{
Recompile();
}
/// <summary>
/// Compiles the framebuffers.
/// </summary>
public void Compile()
{
foreach (var framebuffer in Framebuffers)
framebuffer.Compile();
}
/// <summary>
/// Recompiles the pipeline.
/// </summary>
public void Recompile()
{
if (Framebuffers == null) return;
Dispose();
Thread.Sleep(100);
Compile();
}
/// <summary>
/// Disposes unmanaged resources like Framebuffers.
/// </summary>
public void Dispose()
{
foreach (var framebuffer in Framebuffers)
framebuffer.Dispose();
}
/// <summary>
/// This creates a finished setup for a framebuffer.
/// </summary>
/// <param name="multisamples"></param>
/// <returns></returns>
public Framebuffer CreateWindowFramebuffer(int multisamples = 0, PixelInformation? pixelInformation = null, bool depth = true)
{
Framebuffer framebuffer = new Framebuffer(ConnectedWindow);
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;
}
}
}