2019-10-13 07:02:07 +01:00
|
|
|
using OpenTK.Graphics.OpenGL;
|
|
|
|
using Ryujinx.Graphics.GAL;
|
2020-05-23 10:46:09 +01:00
|
|
|
using Ryujinx.Graphics.OpenGL.Image;
|
2019-10-13 07:02:07 +01:00
|
|
|
using System;
|
2021-01-26 21:44:07 +00:00
|
|
|
using System.Runtime.CompilerServices;
|
2019-10-13 07:02:07 +01:00
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.OpenGL
|
|
|
|
{
|
|
|
|
class Framebuffer : IDisposable
|
|
|
|
{
|
|
|
|
public int Handle { get; private set; }
|
2022-06-14 17:30:39 +01:00
|
|
|
private int _clearFbHandle;
|
|
|
|
private bool _clearFbInitialized;
|
2019-10-13 07:02:07 +01:00
|
|
|
|
|
|
|
private FramebufferAttachment _lastDsAttachment;
|
|
|
|
|
2020-03-29 13:48:39 +01:00
|
|
|
private readonly TextureView[] _colors;
|
2022-06-14 17:30:39 +01:00
|
|
|
private TextureView _depthStencil;
|
2020-03-29 13:48:39 +01:00
|
|
|
|
2020-10-13 01:50:41 +01:00
|
|
|
private int _colorsCount;
|
|
|
|
private bool _dualSourceBlend;
|
|
|
|
|
2019-10-13 07:02:07 +01:00
|
|
|
public Framebuffer()
|
|
|
|
{
|
|
|
|
Handle = GL.GenFramebuffer();
|
2022-06-14 17:30:39 +01:00
|
|
|
_clearFbHandle = GL.GenFramebuffer();
|
2020-03-29 13:48:39 +01:00
|
|
|
|
|
|
|
_colors = new TextureView[8];
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
|
2020-05-24 14:44:12 +01:00
|
|
|
public int Bind()
|
2019-10-13 07:02:07 +01:00
|
|
|
{
|
|
|
|
GL.BindFramebuffer(FramebufferTarget.Framebuffer, Handle);
|
2020-05-24 14:44:12 +01:00
|
|
|
return Handle;
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
|
2021-01-26 21:44:07 +00:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2019-10-13 07:02:07 +01:00
|
|
|
public void AttachColor(int index, TextureView color)
|
|
|
|
{
|
2021-01-26 21:44:07 +00:00
|
|
|
if (_colors[index] == color)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-29 13:48:39 +01:00
|
|
|
FramebufferAttachment attachment = FramebufferAttachment.ColorAttachment0 + index;
|
|
|
|
|
2021-05-16 19:43:27 +01:00
|
|
|
GL.FramebufferTexture(FramebufferTarget.Framebuffer, attachment, color?.Handle ?? 0, 0);
|
2021-01-26 21:44:07 +00:00
|
|
|
|
|
|
|
_colors[index] = color;
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void AttachDepthStencil(TextureView depthStencil)
|
|
|
|
{
|
|
|
|
// Detach the last depth/stencil buffer if there is any.
|
|
|
|
if (_lastDsAttachment != 0)
|
|
|
|
{
|
|
|
|
GL.FramebufferTexture(FramebufferTarget.Framebuffer, _lastDsAttachment, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (depthStencil != null)
|
|
|
|
{
|
2022-06-14 17:30:39 +01:00
|
|
|
FramebufferAttachment attachment = GetAttachment(depthStencil.Format);
|
2019-10-13 07:02:07 +01:00
|
|
|
|
|
|
|
GL.FramebufferTexture(
|
|
|
|
FramebufferTarget.Framebuffer,
|
|
|
|
attachment,
|
|
|
|
depthStencil.Handle,
|
|
|
|
0);
|
|
|
|
|
|
|
|
_lastDsAttachment = attachment;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_lastDsAttachment = 0;
|
|
|
|
}
|
2022-06-14 17:30:39 +01:00
|
|
|
|
|
|
|
_depthStencil = depthStencil;
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
|
2020-10-13 01:50:41 +01:00
|
|
|
public void SetDualSourceBlend(bool enable)
|
|
|
|
{
|
|
|
|
bool oldEnable = _dualSourceBlend;
|
|
|
|
|
|
|
|
_dualSourceBlend = enable;
|
|
|
|
|
|
|
|
// When dual source blend is used,
|
|
|
|
// we can only have one draw buffer.
|
|
|
|
if (enable)
|
|
|
|
{
|
|
|
|
GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
|
|
|
|
}
|
|
|
|
else if (oldEnable)
|
|
|
|
{
|
|
|
|
SetDrawBuffersImpl(_colorsCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 07:02:07 +01:00
|
|
|
public void SetDrawBuffers(int colorsCount)
|
2020-10-13 01:50:41 +01:00
|
|
|
{
|
|
|
|
if (_colorsCount != colorsCount && !_dualSourceBlend)
|
|
|
|
{
|
|
|
|
SetDrawBuffersImpl(colorsCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
_colorsCount = colorsCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SetDrawBuffersImpl(int colorsCount)
|
2019-10-13 07:02:07 +01:00
|
|
|
{
|
|
|
|
DrawBuffersEnum[] drawBuffers = new DrawBuffersEnum[colorsCount];
|
|
|
|
|
|
|
|
for (int index = 0; index < colorsCount; index++)
|
|
|
|
{
|
|
|
|
drawBuffers[index] = DrawBuffersEnum.ColorAttachment0 + index;
|
|
|
|
}
|
|
|
|
|
|
|
|
GL.DrawBuffers(colorsCount, drawBuffers);
|
|
|
|
}
|
|
|
|
|
2022-06-14 17:30:39 +01:00
|
|
|
private static FramebufferAttachment GetAttachment(Format format)
|
|
|
|
{
|
|
|
|
if (IsPackedDepthStencilFormat(format))
|
|
|
|
{
|
|
|
|
return FramebufferAttachment.DepthStencilAttachment;
|
|
|
|
}
|
|
|
|
else if (IsDepthOnlyFormat(format))
|
|
|
|
{
|
|
|
|
return FramebufferAttachment.DepthAttachment;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FramebufferAttachment.StencilAttachment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 07:02:07 +01:00
|
|
|
private static bool IsPackedDepthStencilFormat(Format format)
|
|
|
|
{
|
|
|
|
return format == Format.D24UnormS8Uint ||
|
2022-03-15 02:42:08 +00:00
|
|
|
format == Format.D32FloatS8Uint ||
|
|
|
|
format == Format.S8UintD24Unorm;
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private static bool IsDepthOnlyFormat(Format format)
|
|
|
|
{
|
2022-03-15 02:42:08 +00:00
|
|
|
return format == Format.D16Unorm || format == Format.D32Float;
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
|
2022-06-14 17:30:39 +01:00
|
|
|
public void AttachColorLayerForClear(int index, int layer)
|
|
|
|
{
|
|
|
|
TextureView color = _colors[index];
|
|
|
|
|
|
|
|
if (!IsLayered(color))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BindClearFb();
|
|
|
|
GL.FramebufferTextureLayer(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0 + index, color.Handle, 0, layer);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DetachColorLayerForClear(int index)
|
|
|
|
{
|
|
|
|
TextureView color = _colors[index];
|
|
|
|
|
|
|
|
if (!IsLayered(color))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0 + index, 0, 0);
|
|
|
|
Bind();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void AttachDepthStencilLayerForClear(int layer)
|
|
|
|
{
|
|
|
|
TextureView depthStencil = _depthStencil;
|
|
|
|
|
|
|
|
if (!IsLayered(depthStencil))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BindClearFb();
|
|
|
|
GL.FramebufferTextureLayer(FramebufferTarget.Framebuffer, GetAttachment(depthStencil.Format), depthStencil.Handle, 0, layer);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DetachDepthStencilLayerForClear()
|
|
|
|
{
|
|
|
|
TextureView depthStencil = _depthStencil;
|
|
|
|
|
|
|
|
if (!IsLayered(depthStencil))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GL.FramebufferTexture(FramebufferTarget.Framebuffer, GetAttachment(depthStencil.Format), 0, 0);
|
|
|
|
Bind();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void BindClearFb()
|
|
|
|
{
|
|
|
|
GL.BindFramebuffer(FramebufferTarget.Framebuffer, _clearFbHandle);
|
|
|
|
|
|
|
|
if (!_clearFbInitialized)
|
|
|
|
{
|
|
|
|
SetDrawBuffersImpl(Constants.MaxRenderTargets);
|
|
|
|
_clearFbInitialized = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool IsLayered(TextureView view)
|
|
|
|
{
|
|
|
|
return view != null &&
|
|
|
|
view.Target != Target.Texture1D &&
|
|
|
|
view.Target != Target.Texture2D &&
|
|
|
|
view.Target != Target.Texture2DMultisample &&
|
|
|
|
view.Target != Target.TextureBuffer;
|
|
|
|
}
|
|
|
|
|
2019-10-13 07:02:07 +01:00
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
if (Handle != 0)
|
|
|
|
{
|
|
|
|
GL.DeleteFramebuffer(Handle);
|
|
|
|
|
|
|
|
Handle = 0;
|
|
|
|
}
|
2022-06-14 17:30:39 +01:00
|
|
|
|
|
|
|
if (_clearFbHandle != 0)
|
|
|
|
{
|
|
|
|
GL.DeleteFramebuffer(_clearFbHandle);
|
|
|
|
|
|
|
|
_clearFbHandle = 0;
|
|
|
|
}
|
2019-10-13 07:02:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|