Framebuffer.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 AttachDepthStencil(TextureView depthStencil)
  27. {
  28. // Detach the last depth/stencil buffer if there is any.
  29. if (_lastDsAttachment != 0)
  30. {
  31. GL.FramebufferTexture(FramebufferTarget.Framebuffer, _lastDsAttachment, 0, 0);
  32. }
  33. if (depthStencil != null)
  34. {
  35. FramebufferAttachment attachment;
  36. if (IsPackedDepthStencilFormat(depthStencil.Format))
  37. {
  38. attachment = FramebufferAttachment.DepthStencilAttachment;
  39. }
  40. else if (IsDepthOnlyFormat(depthStencil.Format))
  41. {
  42. attachment = FramebufferAttachment.DepthAttachment;
  43. }
  44. else
  45. {
  46. attachment = FramebufferAttachment.StencilAttachment;
  47. }
  48. GL.FramebufferTexture(
  49. FramebufferTarget.Framebuffer,
  50. attachment,
  51. depthStencil.Handle,
  52. 0);
  53. _lastDsAttachment = attachment;
  54. }
  55. else
  56. {
  57. _lastDsAttachment = 0;
  58. }
  59. }
  60. public void SetDrawBuffers(int colorsCount)
  61. {
  62. DrawBuffersEnum[] drawBuffers = new DrawBuffersEnum[colorsCount];
  63. for (int index = 0; index < colorsCount; index++)
  64. {
  65. drawBuffers[index] = DrawBuffersEnum.ColorAttachment0 + index;
  66. }
  67. GL.DrawBuffers(colorsCount, drawBuffers);
  68. }
  69. private static bool IsPackedDepthStencilFormat(Format format)
  70. {
  71. return format == Format.D24UnormS8Uint ||
  72. format == Format.D32FloatS8Uint;
  73. }
  74. private static bool IsDepthOnlyFormat(Format format)
  75. {
  76. return format == Format.D16Unorm ||
  77. format == Format.D24X8Unorm ||
  78. format == Format.D32Float;
  79. }
  80. public void Dispose()
  81. {
  82. if (Handle != 0)
  83. {
  84. GL.DeleteFramebuffer(Handle);
  85. Handle = 0;
  86. }
  87. }
  88. }
  89. }