Framebuffer.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Graphics.GAL;
  3. using System;
  4. namespace Ryujinx.Graphics.OpenGL
  5. {
  6. class Framebuffer : IDisposable
  7. {
  8. public int Handle { get; private set; }
  9. private FramebufferAttachment _lastDsAttachment;
  10. public Framebuffer()
  11. {
  12. Handle = GL.GenFramebuffer();
  13. }
  14. public void Bind()
  15. {
  16. GL.BindFramebuffer(FramebufferTarget.Framebuffer, Handle);
  17. }
  18. public void AttachColor(int index, TextureView color)
  19. {
  20. GL.FramebufferTexture(
  21. FramebufferTarget.Framebuffer,
  22. FramebufferAttachment.ColorAttachment0 + index,
  23. color?.Handle ?? 0,
  24. 0);
  25. }
  26. public void AttachColor(int index, TextureView color, int layer)
  27. {
  28. GL.FramebufferTextureLayer(
  29. FramebufferTarget.Framebuffer,
  30. FramebufferAttachment.ColorAttachment0 + index,
  31. color?.Handle ?? 0,
  32. 0,
  33. layer);
  34. }
  35. public void AttachDepthStencil(TextureView depthStencil)
  36. {
  37. // Detach the last depth/stencil buffer if there is any.
  38. if (_lastDsAttachment != 0)
  39. {
  40. GL.FramebufferTexture(FramebufferTarget.Framebuffer, _lastDsAttachment, 0, 0);
  41. }
  42. if (depthStencil != null)
  43. {
  44. FramebufferAttachment attachment;
  45. if (IsPackedDepthStencilFormat(depthStencil.Format))
  46. {
  47. attachment = FramebufferAttachment.DepthStencilAttachment;
  48. }
  49. else if (IsDepthOnlyFormat(depthStencil.Format))
  50. {
  51. attachment = FramebufferAttachment.DepthAttachment;
  52. }
  53. else
  54. {
  55. attachment = FramebufferAttachment.StencilAttachment;
  56. }
  57. GL.FramebufferTexture(
  58. FramebufferTarget.Framebuffer,
  59. attachment,
  60. depthStencil.Handle,
  61. 0);
  62. _lastDsAttachment = attachment;
  63. }
  64. else
  65. {
  66. _lastDsAttachment = 0;
  67. }
  68. }
  69. public void SetDrawBuffers(int colorsCount)
  70. {
  71. DrawBuffersEnum[] drawBuffers = new DrawBuffersEnum[colorsCount];
  72. for (int index = 0; index < colorsCount; index++)
  73. {
  74. drawBuffers[index] = DrawBuffersEnum.ColorAttachment0 + index;
  75. }
  76. GL.DrawBuffers(colorsCount, drawBuffers);
  77. }
  78. private static bool IsPackedDepthStencilFormat(Format format)
  79. {
  80. return format == Format.D24UnormS8Uint ||
  81. format == Format.D32FloatS8Uint;
  82. }
  83. private static bool IsDepthOnlyFormat(Format format)
  84. {
  85. return format == Format.D16Unorm ||
  86. format == Format.D24X8Unorm ||
  87. format == Format.D32Float;
  88. }
  89. public void Dispose()
  90. {
  91. if (Handle != 0)
  92. {
  93. GL.DeleteFramebuffer(Handle);
  94. Handle = 0;
  95. }
  96. }
  97. }
  98. }