Common.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright (C) 2009-present, 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, vk, d3d, 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. Error ShaderReflection::linkShaderReflection(const ShaderReflection& a, const ShaderReflection& b, ShaderReflection& c_)
  108. {
  109. ShaderReflection c;
  110. memcpy(&c.m_descriptor, &a.m_descriptor, sizeof(a.m_descriptor));
  111. for(U32 space = 0; space < kMaxRegisterSpaces; ++space)
  112. {
  113. for(U32 binding = 0; binding < b.m_descriptor.m_bindingCounts[space]; ++binding)
  114. {
  115. // Search for the binding in a
  116. const ShaderReflectionBinding& bbinding = b.m_descriptor.m_bindings[space][binding];
  117. Bool bindingFoundOnA = false;
  118. for(U32 binding2 = 0; binding2 < a.m_descriptor.m_bindingCounts[space]; ++binding2)
  119. {
  120. const ShaderReflectionBinding& abinding = a.m_descriptor.m_bindings[space][binding2];
  121. if(abinding.m_registerBindingPoint == bbinding.m_registerBindingPoint
  122. && descriptorTypeToHlslResourceType(abinding.m_type) == descriptorTypeToHlslResourceType(bbinding.m_type))
  123. {
  124. if(abinding != bbinding)
  125. {
  126. ANKI_GR_LOGE("Can't link shader reflection because of different bindings. Space %u binding %u", space, binding);
  127. return Error::kFunctionFailed;
  128. }
  129. bindingFoundOnA = true;
  130. break;
  131. }
  132. }
  133. if(!bindingFoundOnA)
  134. {
  135. c.m_descriptor.m_bindings[space][c.m_descriptor.m_bindingCounts[space]++] = bbinding;
  136. }
  137. }
  138. }
  139. if(a.m_descriptor.m_fastConstantsSize != 0 && b.m_descriptor.m_fastConstantsSize != 0
  140. && a.m_descriptor.m_fastConstantsSize != b.m_descriptor.m_fastConstantsSize)
  141. {
  142. ANKI_GR_LOGE("Can't link shader reflection because fast constant size doesn't match");
  143. return Error::kFunctionFailed;
  144. }
  145. c.m_descriptor.m_fastConstantsSize = max(a.m_descriptor.m_fastConstantsSize, b.m_descriptor.m_fastConstantsSize);
  146. c.m_descriptor.m_hasVkBindlessDescriptorSet = a.m_descriptor.m_hasVkBindlessDescriptorSet || b.m_descriptor.m_hasVkBindlessDescriptorSet;
  147. c.m_vertex.m_vkVertexAttributeLocations =
  148. (a.m_vertex.m_vertexAttributeMask.getAnySet()) ? a.m_vertex.m_vkVertexAttributeLocations : b.m_vertex.m_vkVertexAttributeLocations;
  149. c.m_vertex.m_vertexAttributeMask = a.m_vertex.m_vertexAttributeMask | b.m_vertex.m_vertexAttributeMask;
  150. c.m_pixel.m_colorRenderTargetWritemask = a.m_pixel.m_colorRenderTargetWritemask | b.m_pixel.m_colorRenderTargetWritemask;
  151. c.m_pixel.m_discards = a.m_pixel.m_discards || b.m_pixel.m_discards;
  152. c_ = c;
  153. return Error::kNone;
  154. }
  155. } // end namespace anki