0
0
Fork 0
This repository has been archived on 2024-10-12. You can view files and clone it, but cannot push or open issues or pull requests.
ryujinx-final/Ryujinx.Graphics.OpenGL/Framebuffer.cs

150 lines
4.1 KiB
C#
Raw Normal View History

2019-10-13 06:02:07 +00:00
using OpenTK.Graphics.OpenGL;
using Ryujinx.Graphics.GAL;
using Ryujinx.Graphics.OpenGL.Image;
2019-10-13 06:02:07 +00:00
using System;
2021-01-26 21:44:07 +00:00
using System.Runtime.CompilerServices;
2019-10-13 06:02:07 +00:00
namespace Ryujinx.Graphics.OpenGL
{
class Framebuffer : IDisposable
{
public int Handle { get; private set; }
private FramebufferAttachment _lastDsAttachment;
private readonly TextureView[] _colors;
private int _colorsCount;
private bool _dualSourceBlend;
2019-10-13 06:02:07 +00:00
public Framebuffer()
{
Handle = GL.GenFramebuffer();
_colors = new TextureView[8];
2019-10-13 06:02:07 +00:00
}
public int Bind()
2019-10-13 06:02:07 +00:00
{
GL.BindFramebuffer(FramebufferTarget.Framebuffer, Handle);
return Handle;
2019-10-13 06:02:07 +00:00
}
2021-01-26 21:44:07 +00:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2019-10-13 06:02:07 +00:00
public void AttachColor(int index, TextureView color)
{
2021-01-26 21:44:07 +00:00
if (_colors[index] == color)
{
return;
}
FramebufferAttachment attachment = FramebufferAttachment.ColorAttachment0 + index;
GL.FramebufferTexture(FramebufferTarget.Framebuffer, attachment, color?.Handle ?? 0, 0);
2021-01-26 21:44:07 +00:00
_colors[index] = color;
2019-10-13 06:02:07 +00: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;
}
}
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 06:02:07 +00:00
public void SetDrawBuffers(int colorsCount)
{
if (_colorsCount != colorsCount && !_dualSourceBlend)
{
SetDrawBuffersImpl(colorsCount);
}
_colorsCount = colorsCount;
}
private void SetDrawBuffersImpl(int colorsCount)
2019-10-13 06:02:07 +00:00
{
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 ||
format == Format.S8UintD24Unorm;
2019-10-13 06:02:07 +00:00
}
private static bool IsDepthOnlyFormat(Format format)
{
return format == Format.D16Unorm || format == Format.D32Float;
2019-10-13 06:02:07 +00:00
}
public void Dispose()
{
if (Handle != 0)
{
GL.DeleteFramebuffer(Handle);
Handle = 0;
}
}
}
}