Framebuffer.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 int _clearFbHandle;
  12. private bool _clearFbInitialized;
  13. private FramebufferAttachment _lastDsAttachment;
  14. private readonly TextureView[] _colors;
  15. private TextureView _depthStencil;
  16. private int _colorsCount;
  17. private bool _dualSourceBlend;
  18. public Framebuffer()
  19. {
  20. Handle = GL.GenFramebuffer();
  21. _clearFbHandle = GL.GenFramebuffer();
  22. _colors = new TextureView[8];
  23. }
  24. public int Bind()
  25. {
  26. GL.BindFramebuffer(FramebufferTarget.Framebuffer, Handle);
  27. return Handle;
  28. }
  29. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  30. public void AttachColor(int index, TextureView color)
  31. {
  32. if (_colors[index] == color)
  33. {
  34. return;
  35. }
  36. FramebufferAttachment attachment = FramebufferAttachment.ColorAttachment0 + index;
  37. GL.FramebufferTexture(FramebufferTarget.Framebuffer, attachment, color?.Handle ?? 0, 0);
  38. _colors[index] = color;
  39. }
  40. public void AttachDepthStencil(TextureView depthStencil)
  41. {
  42. // Detach the last depth/stencil buffer if there is any.
  43. if (_lastDsAttachment != 0)
  44. {
  45. GL.FramebufferTexture(FramebufferTarget.Framebuffer, _lastDsAttachment, 0, 0);
  46. }
  47. if (depthStencil != null)
  48. {
  49. FramebufferAttachment attachment = GetAttachment(depthStencil.Format);
  50. GL.FramebufferTexture(
  51. FramebufferTarget.Framebuffer,
  52. attachment,
  53. depthStencil.Handle,
  54. 0);
  55. _lastDsAttachment = attachment;
  56. }
  57. else
  58. {
  59. _lastDsAttachment = 0;
  60. }
  61. _depthStencil = depthStencil;
  62. }
  63. public void SetDualSourceBlend(bool enable)
  64. {
  65. bool oldEnable = _dualSourceBlend;
  66. _dualSourceBlend = enable;
  67. // When dual source blend is used,
  68. // we can only have one draw buffer.
  69. if (enable)
  70. {
  71. GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
  72. }
  73. else if (oldEnable)
  74. {
  75. SetDrawBuffersImpl(_colorsCount);
  76. }
  77. }
  78. public void SetDrawBuffers(int colorsCount)
  79. {
  80. if (_colorsCount != colorsCount && !_dualSourceBlend)
  81. {
  82. SetDrawBuffersImpl(colorsCount);
  83. }
  84. _colorsCount = colorsCount;
  85. }
  86. private void SetDrawBuffersImpl(int colorsCount)
  87. {
  88. DrawBuffersEnum[] drawBuffers = new DrawBuffersEnum[colorsCount];
  89. for (int index = 0; index < colorsCount; index++)
  90. {
  91. drawBuffers[index] = DrawBuffersEnum.ColorAttachment0 + index;
  92. }
  93. GL.DrawBuffers(colorsCount, drawBuffers);
  94. }
  95. private static FramebufferAttachment GetAttachment(Format format)
  96. {
  97. if (IsPackedDepthStencilFormat(format))
  98. {
  99. return FramebufferAttachment.DepthStencilAttachment;
  100. }
  101. else if (IsDepthOnlyFormat(format))
  102. {
  103. return FramebufferAttachment.DepthAttachment;
  104. }
  105. else
  106. {
  107. return FramebufferAttachment.StencilAttachment;
  108. }
  109. }
  110. private static bool IsPackedDepthStencilFormat(Format format)
  111. {
  112. return format == Format.D24UnormS8Uint ||
  113. format == Format.D32FloatS8Uint ||
  114. format == Format.S8UintD24Unorm;
  115. }
  116. private static bool IsDepthOnlyFormat(Format format)
  117. {
  118. return format == Format.D16Unorm || format == Format.D32Float;
  119. }
  120. public void AttachColorLayerForClear(int index, int layer)
  121. {
  122. TextureView color = _colors[index];
  123. if (!IsLayered(color))
  124. {
  125. return;
  126. }
  127. BindClearFb();
  128. GL.FramebufferTextureLayer(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0 + index, color.Handle, 0, layer);
  129. }
  130. public void DetachColorLayerForClear(int index)
  131. {
  132. TextureView color = _colors[index];
  133. if (!IsLayered(color))
  134. {
  135. return;
  136. }
  137. GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0 + index, 0, 0);
  138. Bind();
  139. }
  140. public void AttachDepthStencilLayerForClear(int layer)
  141. {
  142. TextureView depthStencil = _depthStencil;
  143. if (!IsLayered(depthStencil))
  144. {
  145. return;
  146. }
  147. BindClearFb();
  148. GL.FramebufferTextureLayer(FramebufferTarget.Framebuffer, GetAttachment(depthStencil.Format), depthStencil.Handle, 0, layer);
  149. }
  150. public void DetachDepthStencilLayerForClear()
  151. {
  152. TextureView depthStencil = _depthStencil;
  153. if (!IsLayered(depthStencil))
  154. {
  155. return;
  156. }
  157. GL.FramebufferTexture(FramebufferTarget.Framebuffer, GetAttachment(depthStencil.Format), 0, 0);
  158. Bind();
  159. }
  160. private void BindClearFb()
  161. {
  162. GL.BindFramebuffer(FramebufferTarget.Framebuffer, _clearFbHandle);
  163. if (!_clearFbInitialized)
  164. {
  165. SetDrawBuffersImpl(Constants.MaxRenderTargets);
  166. _clearFbInitialized = true;
  167. }
  168. }
  169. private static bool IsLayered(TextureView view)
  170. {
  171. return view != null &&
  172. view.Target != Target.Texture1D &&
  173. view.Target != Target.Texture2D &&
  174. view.Target != Target.Texture2DMultisample &&
  175. view.Target != Target.TextureBuffer;
  176. }
  177. public void Dispose()
  178. {
  179. if (Handle != 0)
  180. {
  181. GL.DeleteFramebuffer(Handle);
  182. Handle = 0;
  183. }
  184. if (_clearFbHandle != 0)
  185. {
  186. GL.DeleteFramebuffer(_clearFbHandle);
  187. _clearFbHandle = 0;
  188. }
  189. }
  190. }
  191. }