TextureView.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Gr/GrObject.h>
  7. #include <AnKi/Gr/Texture.h>
  8. namespace anki
  9. {
  10. /// @addtogroup graphics
  11. /// @{
  12. /// TextureView init info.
  13. class TextureViewInitInfo : public GrBaseInitInfo, public TextureSubresourceInfo
  14. {
  15. public:
  16. TexturePtr m_texture;
  17. TextureViewInitInfo(TexturePtr tex, CString name = {})
  18. : GrBaseInitInfo(name)
  19. , m_texture(tex)
  20. {
  21. m_firstMipmap = 0;
  22. m_mipmapCount = tex->getMipmapCount();
  23. m_firstLayer = 0;
  24. m_layerCount = tex->getLayerCount();
  25. m_firstFace = 0;
  26. m_faceCount =
  27. (tex->getTextureType() == TextureType::CUBE_ARRAY || tex->getTextureType() == TextureType::CUBE) ? 6 : 1;
  28. m_depthStencilAspect = computeFormatAspect(tex->getFormat());
  29. }
  30. TextureViewInitInfo(CString name = {})
  31. : GrBaseInitInfo(name)
  32. {
  33. }
  34. TextureViewInitInfo(TexturePtr tex, const TextureSurfaceInfo& surf,
  35. DepthStencilAspectBit aspect = DepthStencilAspectBit::NONE, CString name = {})
  36. : GrBaseInitInfo(name)
  37. , m_texture(tex)
  38. {
  39. m_firstMipmap = surf.m_level;
  40. m_mipmapCount = 1;
  41. m_firstLayer = surf.m_layer;
  42. m_layerCount = 1;
  43. m_firstFace = U8(surf.m_face);
  44. m_faceCount = 1;
  45. m_depthStencilAspect = aspect;
  46. ANKI_ASSERT(isValid());
  47. }
  48. TextureViewInitInfo(TexturePtr tex, const TextureSubresourceInfo& subresource, CString name = {})
  49. : GrBaseInitInfo(name)
  50. , m_texture(tex)
  51. {
  52. static_cast<TextureSubresourceInfo&>(*this) = subresource;
  53. ANKI_ASSERT(isValid());
  54. }
  55. Bool isValid() const
  56. {
  57. return m_texture.isCreated() && m_texture->isSubresourceValid(*this);
  58. }
  59. };
  60. /// GPU texture view.
  61. class TextureView : public GrObject
  62. {
  63. ANKI_GR_OBJECT
  64. public:
  65. static const GrObjectType CLASS_TYPE = GrObjectType::TEXTURE_VIEW;
  66. TextureType getTextureType() const
  67. {
  68. ANKI_ASSERT(initialized());
  69. return m_texType;
  70. }
  71. const TextureSubresourceInfo& getSubresource() const
  72. {
  73. ANKI_ASSERT(initialized());
  74. return m_subresource;
  75. }
  76. /// Returns an index to be used for bindless access. Only for sampling.
  77. /// @note It's thread-safe
  78. U32 getOrCreateBindlessTextureIndex();
  79. /// Returns an index to be used for bindless access. For image read/write.
  80. /// @note It's thread-safe
  81. U32 getOrCreateBindlessImageIndex();
  82. protected:
  83. TextureType m_texType = TextureType::COUNT;
  84. TextureSubresourceInfo m_subresource;
  85. /// Construct.
  86. TextureView(GrManager* manager, CString name)
  87. : GrObject(manager, CLASS_TYPE, name)
  88. {
  89. m_subresource.m_depthStencilAspect = DepthStencilAspectBit::NONE;
  90. m_subresource.m_firstMipmap = MAX_U32;
  91. m_subresource.m_mipmapCount = MAX_U32;
  92. m_subresource.m_firstLayer = MAX_U32;
  93. m_subresource.m_layerCount = MAX_U32;
  94. m_subresource.m_firstFace = MAX_U8;
  95. m_subresource.m_faceCount = MAX_U8;
  96. }
  97. /// Destroy.
  98. ~TextureView()
  99. {
  100. }
  101. Bool initialized() const
  102. {
  103. return m_texType != TextureType::COUNT && m_subresource.m_firstMipmap < MAX_U32
  104. && m_subresource.m_mipmapCount < MAX_U32 && m_subresource.m_firstLayer < MAX_U32
  105. && m_subresource.m_layerCount < MAX_U32 && m_subresource.m_firstFace < MAX_U8
  106. && m_subresource.m_faceCount < MAX_U8;
  107. }
  108. private:
  109. /// Allocate and initialize a new instance.
  110. static ANKI_USE_RESULT TextureView* newInstance(GrManager* manager, const TextureViewInitInfo& init);
  111. };
  112. /// @}
  113. } // end namespace anki