TextureView.h 3.4 KB

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