Framebuffer.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 int GetColorLayerCount(int index)
  121. {
  122. return _colors[index]?.Info.GetDepthOrLayers() ?? 0;
  123. }
  124. public int GetDepthStencilLayerCount()
  125. {
  126. return _depthStencil?.Info.GetDepthOrLayers() ?? 0;
  127. }
  128. public void AttachColorLayerForClear(int index, int layer)
  129. {
  130. TextureView color = _colors[index];
  131. if (!IsLayered(color))
  132. {
  133. return;
  134. }
  135. BindClearFb();
  136. GL.FramebufferTextureLayer(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0 + index, color.Handle, 0, layer);
  137. }
  138. public void DetachColorLayerForClear(int index)
  139. {
  140. TextureView color = _colors[index];
  141. if (!IsLayered(color))
  142. {
  143. return;
  144. }
  145. GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0 + index, 0, 0);
  146. Bind();
  147. }
  148. public void AttachDepthStencilLayerForClear(int layer)
  149. {
  150. TextureView depthStencil = _depthStencil;
  151. if (!IsLayered(depthStencil))
  152. {
  153. return;
  154. }
  155. BindClearFb();
  156. GL.FramebufferTextureLayer(FramebufferTarget.Framebuffer, GetAttachment(depthStencil.Format), depthStencil.Handle, 0, layer);
  157. }
  158. public void DetachDepthStencilLayerForClear()
  159. {
  160. TextureView depthStencil = _depthStencil;
  161. if (!IsLayered(depthStencil))
  162. {
  163. return;
  164. }
  165. GL.FramebufferTexture(FramebufferTarget.Framebuffer, GetAttachment(depthStencil.Format), 0, 0);
  166. Bind();
  167. }
  168. private void BindClearFb()
  169. {
  170. GL.BindFramebuffer(FramebufferTarget.Framebuffer, _clearFbHandle);
  171. if (!_clearFbInitialized)
  172. {
  173. SetDrawBuffersImpl(Constants.MaxRenderTargets);
  174. _clearFbInitialized = true;
  175. }
  176. }
  177. private static bool IsLayered(TextureView view)
  178. {
  179. return view != null &&
  180. view.Target != Target.Texture1D &&
  181. view.Target != Target.Texture2D &&
  182. view.Target != Target.Texture2DMultisample &&
  183. view.Target != Target.TextureBuffer;
  184. }
  185. public void Dispose()
  186. {
  187. if (Handle != 0)
  188. {
  189. GL.DeleteFramebuffer(Handle);
  190. Handle = 0;
  191. }
  192. if (_clearFbHandle != 0)
  193. {
  194. GL.DeleteFramebuffer(_clearFbHandle);
  195. _clearFbHandle = 0;
  196. }
  197. }
  198. }
  199. }