2019-10-13 08:02:07 +02:00
|
|
|
using OpenTK.Graphics.OpenGL;
|
|
|
|
using Ryujinx.Graphics.GAL;
|
2020-05-23 11:46:09 +02:00
|
|
|
using Ryujinx.Graphics.OpenGL.Image;
|
2019-10-13 08:02:07 +02:00
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.OpenGL
|
|
|
|
{
|
|
|
|
class Framebuffer : IDisposable
|
|
|
|
{
|
|
|
|
public int Handle { get; private set; }
|
|
|
|
|
|
|
|
private FramebufferAttachment _lastDsAttachment;
|
|
|
|
|
2020-03-29 14:48:39 +02:00
|
|
|
private readonly TextureView[] _colors;
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
public Framebuffer()
|
|
|
|
{
|
|
|
|
Handle = GL.GenFramebuffer();
|
2020-03-29 14:48:39 +02:00
|
|
|
|
|
|
|
_colors = new TextureView[8];
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 15:44:12 +02:00
|
|
|
public int Bind()
|
2019-10-13 08:02:07 +02:00
|
|
|
{
|
|
|
|
GL.BindFramebuffer(FramebufferTarget.Framebuffer, Handle);
|
2020-05-24 15:44:12 +02:00
|
|
|
return Handle;
|
2019-10-13 08:02:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void AttachColor(int index, TextureView color)
|
|
|
|
{
|
2020-03-29 14:48:39 +02:00
|
|
|
FramebufferAttachment attachment = FramebufferAttachment.ColorAttachment0 + index;
|
|
|
|
|
|
|
|
if (HwCapabilities.Vendor == HwCapabilities.GpuVendor.Amd ||
|
|
|
|
HwCapabilities.Vendor == HwCapabilities.GpuVendor.Intel)
|
|
|
|
{
|
|
|
|
GL.FramebufferTexture(FramebufferTarget.Framebuffer, attachment, color?.GetIncompatibleFormatViewHandle() ?? 0, 0);
|
|
|
|
|
|
|
|
_colors[index] = color;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GL.FramebufferTexture(FramebufferTarget.Framebuffer, attachment, color?.Handle ?? 0, 0);
|
|
|
|
}
|
2019-10-13 08:02:07 +02: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)
|
|
|
|
{
|
|
|
|
FramebufferAttachment attachment;
|
|
|
|
|
|
|
|
if (IsPackedDepthStencilFormat(depthStencil.Format))
|
|
|
|
{
|
|
|
|
attachment = FramebufferAttachment.DepthStencilAttachment;
|
|
|
|
}
|
|
|
|
else if (IsDepthOnlyFormat(depthStencil.Format))
|
|
|
|
{
|
|
|
|
attachment = FramebufferAttachment.DepthAttachment;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
attachment = FramebufferAttachment.StencilAttachment;
|
|
|
|
}
|
|
|
|
|
|
|
|
GL.FramebufferTexture(
|
|
|
|
FramebufferTarget.Framebuffer,
|
|
|
|
attachment,
|
|
|
|
depthStencil.Handle,
|
|
|
|
0);
|
|
|
|
|
|
|
|
_lastDsAttachment = attachment;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_lastDsAttachment = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 14:48:39 +02:00
|
|
|
public void SignalModified()
|
|
|
|
{
|
|
|
|
if (HwCapabilities.Vendor == HwCapabilities.GpuVendor.Amd ||
|
|
|
|
HwCapabilities.Vendor == HwCapabilities.GpuVendor.Intel)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
if (_colors[i] != null)
|
|
|
|
{
|
|
|
|
_colors[i].SignalModified();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 08:02:07 +02:00
|
|
|
public void SetDrawBuffers(int colorsCount)
|
|
|
|
{
|
|
|
|
DrawBuffersEnum[] drawBuffers = new DrawBuffersEnum[colorsCount];
|
|
|
|
|
|
|
|
for (int index = 0; index < colorsCount; index++)
|
|
|
|
{
|
|
|
|
drawBuffers[index] = DrawBuffersEnum.ColorAttachment0 + index;
|
|
|
|
}
|
|
|
|
|
|
|
|
GL.DrawBuffers(colorsCount, drawBuffers);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool IsPackedDepthStencilFormat(Format format)
|
|
|
|
{
|
|
|
|
return format == Format.D24UnormS8Uint ||
|
|
|
|
format == Format.D32FloatS8Uint;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static bool IsDepthOnlyFormat(Format format)
|
|
|
|
{
|
|
|
|
return format == Format.D16Unorm ||
|
|
|
|
format == Format.D24X8Unorm ||
|
|
|
|
format == Format.D32Float;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
if (Handle != 0)
|
|
|
|
{
|
|
|
|
GL.DeleteFramebuffer(Handle);
|
|
|
|
|
|
|
|
Handle = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|