TextureStorage.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using OpenTK.Graphics.OpenGL;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Graphics.GAL;
  4. namespace Ryujinx.Graphics.OpenGL.Image
  5. {
  6. class TextureStorage : ITextureInfo
  7. {
  8. public ITextureInfo Storage => this;
  9. public int Handle { get; private set; }
  10. public float ScaleFactor { get; private set; }
  11. public TextureCreateInfo Info { get; }
  12. private readonly OpenGLRenderer _renderer;
  13. private int _viewsCount;
  14. internal ITexture DefaultView { get; private set; }
  15. public TextureStorage(OpenGLRenderer renderer, TextureCreateInfo info, float scaleFactor)
  16. {
  17. _renderer = renderer;
  18. Info = info;
  19. Handle = GL.GenTexture();
  20. ScaleFactor = scaleFactor;
  21. CreateImmutableStorage();
  22. }
  23. private void CreateImmutableStorage()
  24. {
  25. TextureTarget target = Info.Target.Convert();
  26. GL.ActiveTexture(TextureUnit.Texture0);
  27. GL.BindTexture(target, Handle);
  28. FormatInfo format = FormatTable.GetFormatInfo(Info.Format);
  29. SizedInternalFormat internalFormat;
  30. if (format.IsCompressed)
  31. {
  32. internalFormat = (SizedInternalFormat)format.PixelFormat;
  33. }
  34. else
  35. {
  36. internalFormat = (SizedInternalFormat)format.PixelInternalFormat;
  37. }
  38. int levels = Info.GetLevelsClamped();
  39. switch (Info.Target)
  40. {
  41. case Target.Texture1D:
  42. GL.TexStorage1D(
  43. TextureTarget1d.Texture1D,
  44. levels,
  45. internalFormat,
  46. Info.Width);
  47. break;
  48. case Target.Texture1DArray:
  49. GL.TexStorage2D(
  50. TextureTarget2d.Texture1DArray,
  51. levels,
  52. internalFormat,
  53. Info.Width,
  54. Info.Height);
  55. break;
  56. case Target.Texture2D:
  57. GL.TexStorage2D(
  58. TextureTarget2d.Texture2D,
  59. levels,
  60. internalFormat,
  61. Info.Width,
  62. Info.Height);
  63. break;
  64. case Target.Texture2DArray:
  65. GL.TexStorage3D(
  66. TextureTarget3d.Texture2DArray,
  67. levels,
  68. internalFormat,
  69. Info.Width,
  70. Info.Height,
  71. Info.Depth);
  72. break;
  73. case Target.Texture2DMultisample:
  74. GL.TexStorage2DMultisample(
  75. TextureTargetMultisample2d.Texture2DMultisample,
  76. Info.Samples,
  77. internalFormat,
  78. Info.Width,
  79. Info.Height,
  80. true);
  81. break;
  82. case Target.Texture2DMultisampleArray:
  83. GL.TexStorage3DMultisample(
  84. TextureTargetMultisample3d.Texture2DMultisampleArray,
  85. Info.Samples,
  86. internalFormat,
  87. Info.Width,
  88. Info.Height,
  89. Info.Depth,
  90. true);
  91. break;
  92. case Target.Texture3D:
  93. GL.TexStorage3D(
  94. TextureTarget3d.Texture3D,
  95. levels,
  96. internalFormat,
  97. Info.Width,
  98. Info.Height,
  99. Info.Depth);
  100. break;
  101. case Target.Cubemap:
  102. GL.TexStorage2D(
  103. TextureTarget2d.TextureCubeMap,
  104. levels,
  105. internalFormat,
  106. Info.Width,
  107. Info.Height);
  108. break;
  109. case Target.CubemapArray:
  110. GL.TexStorage3D(
  111. (TextureTarget3d)All.TextureCubeMapArray,
  112. levels,
  113. internalFormat,
  114. Info.Width,
  115. Info.Height,
  116. Info.Depth);
  117. break;
  118. default:
  119. Logger.Debug?.Print(LogClass.Gpu, $"Invalid or unsupported texture target: {target}.");
  120. break;
  121. }
  122. }
  123. public ITexture CreateDefaultView()
  124. {
  125. DefaultView = CreateView(Info, 0, 0);
  126. return DefaultView;
  127. }
  128. public ITexture CreateView(TextureCreateInfo info, int firstLayer, int firstLevel)
  129. {
  130. IncrementViewsCount();
  131. return new TextureView(_renderer, this, info, firstLayer, firstLevel);
  132. }
  133. private void IncrementViewsCount()
  134. {
  135. _viewsCount++;
  136. }
  137. public void DecrementViewsCount()
  138. {
  139. // If we don't have any views, then the storage is now useless, delete it.
  140. if (--_viewsCount == 0)
  141. {
  142. if (DefaultView == null)
  143. {
  144. Dispose();
  145. }
  146. else
  147. {
  148. // If the default view still exists, we can put it into the resource pool.
  149. Release();
  150. }
  151. }
  152. }
  153. /// <summary>
  154. /// Release the TextureStorage to the resource pool without disposing its handle.
  155. /// </summary>
  156. public void Release()
  157. {
  158. _viewsCount = 1; // When we are used again, we will have the default view.
  159. _renderer.ResourcePool.AddTexture((TextureView)DefaultView);
  160. }
  161. public void DeleteDefault()
  162. {
  163. DefaultView = null;
  164. }
  165. public void Dispose()
  166. {
  167. DefaultView = null;
  168. if (Handle != 0)
  169. {
  170. GL.DeleteTexture(Handle);
  171. Handle = 0;
  172. }
  173. }
  174. }
  175. }