Framebuffer.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 void Bind()
  18. {
  19. GL.BindFramebuffer(FramebufferTarget.Framebuffer, Handle);
  20. }
  21. public void AttachColor(int index, TextureView color)
  22. {
  23. FramebufferAttachment attachment = FramebufferAttachment.ColorAttachment0 + index;
  24. if (HwCapabilities.Vendor == HwCapabilities.GpuVendor.Amd ||
  25. HwCapabilities.Vendor == HwCapabilities.GpuVendor.Intel)
  26. {
  27. GL.FramebufferTexture(FramebufferTarget.Framebuffer, attachment, color?.GetIncompatibleFormatViewHandle() ?? 0, 0);
  28. _colors[index] = color;
  29. }
  30. else
  31. {
  32. GL.FramebufferTexture(FramebufferTarget.Framebuffer, attachment, color?.Handle ?? 0, 0);
  33. }
  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 SignalModified()
  70. {
  71. if (HwCapabilities.Vendor == HwCapabilities.GpuVendor.Amd ||
  72. HwCapabilities.Vendor == HwCapabilities.GpuVendor.Intel)
  73. {
  74. for (int i = 0; i < 8; i++)
  75. {
  76. if (_colors[i] != null)
  77. {
  78. _colors[i].SignalModified();
  79. }
  80. }
  81. }
  82. }
  83. public void SetDrawBuffers(int colorsCount)
  84. {
  85. DrawBuffersEnum[] drawBuffers = new DrawBuffersEnum[colorsCount];
  86. for (int index = 0; index < colorsCount; index++)
  87. {
  88. drawBuffers[index] = DrawBuffersEnum.ColorAttachment0 + index;
  89. }
  90. GL.DrawBuffers(colorsCount, drawBuffers);
  91. }
  92. private static bool IsPackedDepthStencilFormat(Format format)
  93. {
  94. return format == Format.D24UnormS8Uint ||
  95. format == Format.D32FloatS8Uint;
  96. }
  97. private static bool IsDepthOnlyFormat(Format format)
  98. {
  99. return format == Format.D16Unorm ||
  100. format == Format.D24X8Unorm ||
  101. format == Format.D32Float;
  102. }
  103. public void Dispose()
  104. {
  105. if (Handle != 0)
  106. {
  107. GL.DeleteFramebuffer(Handle);
  108. Handle = 0;
  109. }
  110. }
  111. }
  112. }