Framebuffer.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.OpenGL.Image;
  4. using System;
  5. namespace Ryujinx.Graphics.OpenGL
  6. {
  7. class Framebuffer : IDisposable
  8. {
  9. public int Handle { get; private set; }
  10. private FramebufferAttachment _lastDsAttachment;
  11. private readonly TextureView[] _colors;
  12. public Framebuffer()
  13. {
  14. Handle = GL.GenFramebuffer();
  15. _colors = new TextureView[8];
  16. }
  17. public int Bind()
  18. {
  19. GL.BindFramebuffer(FramebufferTarget.Framebuffer, Handle);
  20. return Handle;
  21. }
  22. public void AttachColor(int index, TextureView color)
  23. {
  24. FramebufferAttachment attachment = FramebufferAttachment.ColorAttachment0 + index;
  25. if (HwCapabilities.Vendor == HwCapabilities.GpuVendor.Amd ||
  26. HwCapabilities.Vendor == HwCapabilities.GpuVendor.Intel)
  27. {
  28. GL.FramebufferTexture(FramebufferTarget.Framebuffer, attachment, color?.GetIncompatibleFormatViewHandle() ?? 0, 0);
  29. _colors[index] = color;
  30. }
  31. else
  32. {
  33. GL.FramebufferTexture(FramebufferTarget.Framebuffer, attachment, color?.Handle ?? 0, 0);
  34. }
  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 SignalModified()
  71. {
  72. if (HwCapabilities.Vendor == HwCapabilities.GpuVendor.Amd ||
  73. HwCapabilities.Vendor == HwCapabilities.GpuVendor.Intel)
  74. {
  75. for (int i = 0; i < 8; i++)
  76. {
  77. if (_colors[i] != null)
  78. {
  79. _colors[i].SignalModified();
  80. }
  81. }
  82. }
  83. }
  84. public void SetDrawBuffers(int colorsCount)
  85. {
  86. DrawBuffersEnum[] drawBuffers = new DrawBuffersEnum[colorsCount];
  87. for (int index = 0; index < colorsCount; index++)
  88. {
  89. drawBuffers[index] = DrawBuffersEnum.ColorAttachment0 + index;
  90. }
  91. GL.DrawBuffers(colorsCount, drawBuffers);
  92. }
  93. private static bool IsPackedDepthStencilFormat(Format format)
  94. {
  95. return format == Format.D24UnormS8Uint ||
  96. format == Format.D32FloatS8Uint;
  97. }
  98. private static bool IsDepthOnlyFormat(Format format)
  99. {
  100. return format == Format.D16Unorm ||
  101. format == Format.D24X8Unorm ||
  102. format == Format.D32Float;
  103. }
  104. public void Dispose()
  105. {
  106. if (Handle != 0)
  107. {
  108. GL.DeleteFramebuffer(Handle);
  109. Handle = 0;
  110. }
  111. }
  112. }
  113. }