Framebuffer.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. if (HwCapabilities.Vendor == HwCapabilities.GpuVendor.Amd ||
  34. HwCapabilities.Vendor == HwCapabilities.GpuVendor.Intel)
  35. {
  36. GL.FramebufferTexture(FramebufferTarget.Framebuffer, attachment, color?.GetIncompatibleFormatViewHandle() ?? 0, 0);
  37. }
  38. else
  39. {
  40. GL.FramebufferTexture(FramebufferTarget.Framebuffer, attachment, color?.Handle ?? 0, 0);
  41. }
  42. _colors[index] = color;
  43. }
  44. public void AttachDepthStencil(TextureView depthStencil)
  45. {
  46. // Detach the last depth/stencil buffer if there is any.
  47. if (_lastDsAttachment != 0)
  48. {
  49. GL.FramebufferTexture(FramebufferTarget.Framebuffer, _lastDsAttachment, 0, 0);
  50. }
  51. if (depthStencil != null)
  52. {
  53. FramebufferAttachment attachment;
  54. if (IsPackedDepthStencilFormat(depthStencil.Format))
  55. {
  56. attachment = FramebufferAttachment.DepthStencilAttachment;
  57. }
  58. else if (IsDepthOnlyFormat(depthStencil.Format))
  59. {
  60. attachment = FramebufferAttachment.DepthAttachment;
  61. }
  62. else
  63. {
  64. attachment = FramebufferAttachment.StencilAttachment;
  65. }
  66. GL.FramebufferTexture(
  67. FramebufferTarget.Framebuffer,
  68. attachment,
  69. depthStencil.Handle,
  70. 0);
  71. _lastDsAttachment = attachment;
  72. }
  73. else
  74. {
  75. _lastDsAttachment = 0;
  76. }
  77. }
  78. public void SignalModified()
  79. {
  80. if (HwCapabilities.Vendor == HwCapabilities.GpuVendor.Amd ||
  81. HwCapabilities.Vendor == HwCapabilities.GpuVendor.Intel)
  82. {
  83. for (int i = 0; i < 8; i++)
  84. {
  85. if (_colors[i] != null)
  86. {
  87. _colors[i].SignalModified();
  88. }
  89. }
  90. }
  91. }
  92. public void SetDualSourceBlend(bool enable)
  93. {
  94. bool oldEnable = _dualSourceBlend;
  95. _dualSourceBlend = enable;
  96. // When dual source blend is used,
  97. // we can only have one draw buffer.
  98. if (enable)
  99. {
  100. GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
  101. }
  102. else if (oldEnable)
  103. {
  104. SetDrawBuffersImpl(_colorsCount);
  105. }
  106. }
  107. public void SetDrawBuffers(int colorsCount)
  108. {
  109. if (_colorsCount != colorsCount && !_dualSourceBlend)
  110. {
  111. SetDrawBuffersImpl(colorsCount);
  112. }
  113. _colorsCount = colorsCount;
  114. }
  115. private void SetDrawBuffersImpl(int colorsCount)
  116. {
  117. DrawBuffersEnum[] drawBuffers = new DrawBuffersEnum[colorsCount];
  118. for (int index = 0; index < colorsCount; index++)
  119. {
  120. drawBuffers[index] = DrawBuffersEnum.ColorAttachment0 + index;
  121. }
  122. GL.DrawBuffers(colorsCount, drawBuffers);
  123. }
  124. private static bool IsPackedDepthStencilFormat(Format format)
  125. {
  126. return format == Format.D24UnormS8Uint ||
  127. format == Format.D32FloatS8Uint;
  128. }
  129. private static bool IsDepthOnlyFormat(Format format)
  130. {
  131. return format == Format.D16Unorm ||
  132. format == Format.D24X8Unorm ||
  133. format == Format.D32Float;
  134. }
  135. public void Dispose()
  136. {
  137. if (Handle != 0)
  138. {
  139. GL.DeleteFramebuffer(Handle);
  140. Handle = 0;
  141. }
  142. }
  143. }
  144. }