TextureStorage.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Graphics.GAL;
  4. namespace Ryujinx.Graphics.OpenGL
  5. {
  6. class TextureStorage
  7. {
  8. public int Handle { get; private set; }
  9. public TextureCreateInfo Info { get; }
  10. private readonly Renderer _renderer;
  11. private int _viewsCount;
  12. public TextureStorage(Renderer renderer, TextureCreateInfo info)
  13. {
  14. _renderer = renderer;
  15. Info = info;
  16. Handle = GL.GenTexture();
  17. CreateImmutableStorage();
  18. }
  19. private void CreateImmutableStorage()
  20. {
  21. TextureTarget target = Info.Target.Convert();
  22. GL.ActiveTexture(TextureUnit.Texture0);
  23. GL.BindTexture(target, Handle);
  24. FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
  25. SizedInternalFormat internalFormat;
  26. if (format.IsCompressed)
  27. {
  28. internalFormat = (SizedInternalFormat)format.PixelFormat;
  29. }
  30. else
  31. {
  32. internalFormat = (SizedInternalFormat)format.PixelInternalFormat;
  33. }
  34. switch (Info.Target)
  35. {
  36. case Target.Texture1D:
  37. GL.TexStorage1D(
  38. TextureTarget1d.Texture1D,
  39. Info.Levels,
  40. internalFormat,
  41. Info.Width);
  42. break;
  43. case Target.Texture1DArray:
  44. GL.TexStorage2D(
  45. TextureTarget2d.Texture1DArray,
  46. Info.Levels,
  47. internalFormat,
  48. Info.Width,
  49. Info.Height);
  50. break;
  51. case Target.Texture2D:
  52. GL.TexStorage2D(
  53. TextureTarget2d.Texture2D,
  54. Info.Levels,
  55. internalFormat,
  56. Info.Width,
  57. Info.Height);
  58. break;
  59. case Target.Texture2DArray:
  60. GL.TexStorage3D(
  61. TextureTarget3d.Texture2DArray,
  62. Info.Levels,
  63. internalFormat,
  64. Info.Width,
  65. Info.Height,
  66. Info.Depth);
  67. break;
  68. case Target.Texture2DMultisample:
  69. GL.TexStorage2DMultisample(
  70. TextureTargetMultisample2d.Texture2DMultisample,
  71. Info.Samples,
  72. internalFormat,
  73. Info.Width,
  74. Info.Height,
  75. true);
  76. break;
  77. case Target.Texture2DMultisampleArray:
  78. GL.TexStorage3DMultisample(
  79. TextureTargetMultisample3d.Texture2DMultisampleArray,
  80. Info.Samples,
  81. internalFormat,
  82. Info.Width,
  83. Info.Height,
  84. Info.Depth,
  85. true);
  86. break;
  87. case Target.Texture3D:
  88. GL.TexStorage3D(
  89. TextureTarget3d.Texture3D,
  90. Info.Levels,
  91. internalFormat,
  92. Info.Width,
  93. Info.Height,
  94. Info.Depth);
  95. break;
  96. case Target.Cubemap:
  97. GL.TexStorage2D(
  98. TextureTarget2d.TextureCubeMap,
  99. Info.Levels,
  100. internalFormat,
  101. Info.Width,
  102. Info.Height);
  103. break;
  104. case Target.CubemapArray:
  105. GL.TexStorage3D(
  106. (TextureTarget3d)All.TextureCubeMapArray,
  107. Info.Levels,
  108. internalFormat,
  109. Info.Width,
  110. Info.Height,
  111. Info.Depth);
  112. break;
  113. default:
  114. Logger.PrintDebug(LogClass.Gpu, $"Invalid or unsupported texture target: {target}.");
  115. break;
  116. }
  117. }
  118. public ITexture CreateDefaultView()
  119. {
  120. return CreateView(Info, 0, 0);
  121. }
  122. public ITexture CreateView(TextureCreateInfo info, int firstLayer, int firstLevel)
  123. {
  124. IncrementViewsCount();
  125. return new TextureView(_renderer, this, info, firstLayer, firstLevel);
  126. }
  127. private void IncrementViewsCount()
  128. {
  129. _viewsCount++;
  130. }
  131. public void DecrementViewsCount()
  132. {
  133. // If we don't have any views, then the storage is now useless, delete it.
  134. if (--_viewsCount == 0)
  135. {
  136. Dispose();
  137. }
  138. }
  139. public void Dispose()
  140. {
  141. if (Handle != 0)
  142. {
  143. GL.DeleteTexture(Handle);
  144. Handle = 0;
  145. }
  146. }
  147. }
  148. }