Common.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Gr/Common.h>
  6. #include <AnKi/Gr/GrObject.h>
  7. #include <AnKi/Math.h>
  8. namespace anki {
  9. /// @warning Don't use Array because the compilers can't handle it for some reason.
  10. inline constexpr ShaderVariableDataTypeInfo kShaderVariableDataTypeInfos[] = {
  11. #define ANKI_SVDT_MACRO(type, baseType, rowCount, columnCount, isIntagralType) {ANKI_STRINGIZE(type), sizeof(type), false, isIntagralType},
  12. #define ANKI_SVDT_MACRO_OPAQUE(constant, type) {ANKI_STRINGIZE(type), kMaxU32, true, false},
  13. #include <AnKi/Gr/ShaderVariableDataType.def.h>
  14. #undef ANKI_SVDT_MACRO
  15. #undef ANKI_SVDT_MACRO_OPAQUE
  16. };
  17. void GrObjectDeleter::operator()(GrObject* ptr)
  18. {
  19. deleteInstance(GrMemoryPool::getSingleton(), ptr);
  20. }
  21. const ShaderVariableDataTypeInfo& getShaderVariableDataTypeInfo(ShaderVariableDataType type)
  22. {
  23. ANKI_ASSERT(type > ShaderVariableDataType::kNone && type < ShaderVariableDataType::kCount);
  24. return kShaderVariableDataTypeInfos[U32(type) - 1];
  25. }
  26. FormatInfo getFormatInfo(Format fmt)
  27. {
  28. FormatInfo out = {};
  29. switch(fmt)
  30. {
  31. #define ANKI_FORMAT_DEF(type, id, componentCount, texelSize, blockWidth, blockHeight, blockSize, shaderType, depthStencil) \
  32. case Format::k##type: \
  33. out = {componentCount, texelSize, blockWidth, blockHeight, blockSize, shaderType, DepthStencilAspectBit::k##depthStencil, \
  34. ANKI_STRINGIZE(type)}; \
  35. break;
  36. #include <AnKi/Gr/BackendCommon/Format.def.h>
  37. #undef ANKI_FORMAT_DEF
  38. default:
  39. ANKI_ASSERT(0);
  40. }
  41. return out;
  42. }
  43. PtrSize computeSurfaceSize(U32 width32, U32 height32, Format fmt)
  44. {
  45. const PtrSize width = width32;
  46. const PtrSize height = height32;
  47. ANKI_ASSERT(width > 0 && height > 0);
  48. ANKI_ASSERT(fmt != Format::kNone);
  49. const FormatInfo inf = getFormatInfo(fmt);
  50. if(inf.m_blockSize > 0)
  51. {
  52. // Compressed
  53. ANKI_ASSERT((width % inf.m_blockWidth) == 0);
  54. ANKI_ASSERT((height % inf.m_blockHeight) == 0);
  55. return (width / inf.m_blockWidth) * (height / inf.m_blockHeight) * inf.m_blockSize;
  56. }
  57. else
  58. {
  59. return width * height * inf.m_texelSize;
  60. }
  61. }
  62. PtrSize computeVolumeSize(U32 width32, U32 height32, U32 depth32, Format fmt)
  63. {
  64. const PtrSize width = width32;
  65. const PtrSize height = height32;
  66. const PtrSize depth = depth32;
  67. ANKI_ASSERT(width > 0 && height > 0 && depth > 0);
  68. ANKI_ASSERT(fmt != Format::kNone);
  69. const FormatInfo inf = getFormatInfo(fmt);
  70. if(inf.m_blockSize > 0)
  71. {
  72. // Compressed
  73. ANKI_ASSERT((width % inf.m_blockWidth) == 0);
  74. ANKI_ASSERT((height % inf.m_blockHeight) == 0);
  75. return (width / inf.m_blockWidth) * (height / inf.m_blockHeight) * inf.m_blockSize * depth;
  76. }
  77. else
  78. {
  79. return width * height * depth * inf.m_texelSize;
  80. }
  81. }
  82. U32 computeMaxMipmapCount2d(U32 w, U32 h, U32 minSizeOfLastMip)
  83. {
  84. ANKI_ASSERT(w >= minSizeOfLastMip && h >= minSizeOfLastMip);
  85. U32 s = (w < h) ? w : h;
  86. U32 count = 0;
  87. while(s >= minSizeOfLastMip)
  88. {
  89. s /= 2;
  90. ++count;
  91. }
  92. return count;
  93. }
  94. /// Compute max number of mipmaps for a 3D texture.
  95. U32 computeMaxMipmapCount3d(U32 w, U32 h, U32 d, U32 minSizeOfLastMip)
  96. {
  97. U32 s = (w < h) ? w : h;
  98. s = (s < d) ? s : d;
  99. U32 count = 0;
  100. while(s >= minSizeOfLastMip)
  101. {
  102. s /= 2;
  103. ++count;
  104. }
  105. return count;
  106. }
  107. #if ANKI_ASSERTIONS_ENABLED
  108. void ShaderReflection::validate() const
  109. {
  110. for(U32 set = 0; set < kMaxDescriptorSets; ++set)
  111. {
  112. const Bool setExists = m_descriptorSetMask.get(set);
  113. for(U32 binding = 0; binding < kMaxBindingsPerDescriptorSet; ++binding)
  114. {
  115. const U32 arraySize = m_descriptorArraySizes[set][binding];
  116. const DescriptorType type = m_descriptorTypes[set][binding];
  117. if(!setExists)
  118. {
  119. ANKI_ASSERT(arraySize == 0 && type == DescriptorType::kCount);
  120. }
  121. else if(arraySize != 0)
  122. {
  123. ANKI_ASSERT(type != DescriptorType::kCount);
  124. }
  125. else if(type != DescriptorType::kCount)
  126. {
  127. ANKI_ASSERT(arraySize > 0);
  128. }
  129. }
  130. }
  131. for(VertexAttributeSemantic semantic : EnumIterable<VertexAttributeSemantic>())
  132. {
  133. ANKI_ASSERT(!m_vertexAttributeMask.get(semantic) || m_vertexAttributeLocations[semantic] != kMaxU8);
  134. }
  135. }
  136. #endif
  137. } // end namespace anki