Framebuffer.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.OpenGL.Image;
  4. using System;
  5. using System.Runtime.CompilerServices;
  6. namespace Ryujinx.Graphics.OpenGL
  7. {
  8. class Framebuffer : IDisposable
  9. {
  10. public int Handle { get; private set; }
  11. private FramebufferAttachment _lastDsAttachment;
  12. private readonly TextureView[] _colors;
  13. private int _colorsCount;
  14. private bool _dualSourceBlend;
  15. public Framebuffer()
  16. {
  17. Handle = GL.GenFramebuffer();
  18. _colors = new TextureView[8];
  19. }
  20. public int Bind()
  21. {
  22. GL.BindFramebuffer(FramebufferTarget.Framebuffer, Handle);
  23. return Handle;
  24. }
  25. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  26. public void AttachColor(int index, TextureView color)
  27. {
  28. if (_colors[index] == color)
  29. {
  30. return;
  31. }
  32. FramebufferAttachment attachment = FramebufferAttachment.ColorAttachment0 + index;
  33. GL.FramebufferTexture(FramebufferTarget.Framebuffer, attachment, color?.Handle ?? 0, 0);
  34. _colors[index] = color;
  35. }
  36. public void AttachDepthStencil(TextureView depthStencil)
  37. {
  38. // Detach the last depth/stencil buffer if there is any.
  39. if (_lastDsAttachment != 0)
  40. {
  41. GL.FramebufferTexture(FramebufferTarget.Framebuffer, _lastDsAttachment, 0, 0);
  42. }
  43. if (depthStencil != null)
  44. {
  45. FramebufferAttachment attachment;
  46. if (IsPackedDepthStencilFormat(depthStencil.Format))
  47. {
  48. attachment = FramebufferAttachment.DepthStencilAttachment;
  49. }
  50. else if (IsDepthOnlyFormat(depthStencil.Format))
  51. {
  52. attachment = FramebufferAttachment.DepthAttachment;
  53. }
  54. else
  55. {
  56. attachment = FramebufferAttachment.StencilAttachment;
  57. }
  58. GL.FramebufferTexture(
  59. FramebufferTarget.Framebuffer,
  60. attachment,
  61. depthStencil.Handle,
  62. 0);
  63. _lastDsAttachment = attachment;
  64. }
  65. else
  66. {
  67. _lastDsAttachment = 0;
  68. }
  69. }
  70. public void SetDualSourceBlend(bool enable)
  71. {
  72. bool oldEnable = _dualSourceBlend;
  73. _dualSourceBlend = enable;
  74. // When dual source blend is used,
  75. // we can only have one draw buffer.
  76. if (enable)
  77. {
  78. GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
  79. }
  80. else if (oldEnable)
  81. {
  82. SetDrawBuffersImpl(_colorsCount);
  83. }
  84. }
  85. public void SetDrawBuffers(int colorsCount)
  86. {
  87. if (_colorsCount != colorsCount && !_dualSourceBlend)
  88. {
  89. SetDrawBuffersImpl(colorsCount);
  90. }
  91. _colorsCount = colorsCount;
  92. }
  93. private void SetDrawBuffersImpl(int colorsCount)
  94. {
  95. DrawBuffersEnum[] drawBuffers = new DrawBuffersEnum[colorsCount];
  96. for (int index = 0; index < colorsCount; index++)
  97. {
  98. drawBuffers[index] = DrawBuffersEnum.ColorAttachment0 + index;
  99. }
  100. GL.DrawBuffers(colorsCount, drawBuffers);
  101. }
  102. private static bool IsPackedDepthStencilFormat(Format format)
  103. {
  104. return format == Format.D24UnormS8Uint ||
  105. format == Format.D32FloatS8Uint;
  106. }
  107. private static bool IsDepthOnlyFormat(Format format)
  108. {
  109. return format == Format.D16Unorm ||
  110. format == Format.D24X8Unorm ||
  111. format == Format.D32Float;
  112. }
  113. public void Dispose()
  114. {
  115. if (Handle != 0)
  116. {
  117. GL.DeleteFramebuffer(Handle);
  118. Handle = 0;
  119. }
  120. }
  121. }
  122. }