Common.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. };
  15. const ShaderVariableDataTypeInfo& getShaderVariableDataTypeInfo(ShaderVariableDataType type)
  16. {
  17. ANKI_ASSERT(type > ShaderVariableDataType::kNone && type < ShaderVariableDataType::kCount);
  18. return kShaderVariableDataTypeInfos[U32(type) - 1];
  19. }
  20. FormatInfo getFormatInfo(Format fmt)
  21. {
  22. FormatInfo out = {};
  23. switch(fmt)
  24. {
  25. #define ANKI_FORMAT_DEF(type, vk, d3d, componentCount, texelSize, blockWidth, blockHeight, blockSize, shaderType, depthStencil) \
  26. case Format::k##type: \
  27. out = {componentCount, texelSize, blockWidth, blockHeight, blockSize, shaderType, DepthStencilAspectBit::k##depthStencil, \
  28. ANKI_STRINGIZE(type)}; \
  29. break;
  30. #include <AnKi/Gr/BackendCommon/Format.def.h>
  31. #undef ANKI_FORMAT_DEF
  32. default:
  33. ANKI_ASSERT(0);
  34. }
  35. return out;
  36. }
  37. PtrSize computeSurfaceSize(U32 width32, U32 height32, Format fmt)
  38. {
  39. const PtrSize width = width32;
  40. const PtrSize height = height32;
  41. ANKI_ASSERT(width > 0 && height > 0);
  42. ANKI_ASSERT(fmt != Format::kNone);
  43. const FormatInfo inf = getFormatInfo(fmt);
  44. if(inf.m_blockSize > 0)
  45. {
  46. // Compressed
  47. ANKI_ASSERT((width % inf.m_blockWidth) == 0);
  48. ANKI_ASSERT((height % inf.m_blockHeight) == 0);
  49. return (width / inf.m_blockWidth) * (height / inf.m_blockHeight) * inf.m_blockSize;
  50. }
  51. else
  52. {
  53. return width * height * inf.m_texelSize;
  54. }
  55. }
  56. PtrSize computeVolumeSize(U32 width32, U32 height32, U32 depth32, Format fmt)
  57. {
  58. const PtrSize width = width32;
  59. const PtrSize height = height32;
  60. const PtrSize depth = depth32;
  61. ANKI_ASSERT(width > 0 && height > 0 && depth > 0);
  62. ANKI_ASSERT(fmt != Format::kNone);
  63. const FormatInfo inf = getFormatInfo(fmt);
  64. if(inf.m_blockSize > 0)
  65. {
  66. // Compressed
  67. ANKI_ASSERT((width % inf.m_blockWidth) == 0);
  68. ANKI_ASSERT((height % inf.m_blockHeight) == 0);
  69. return (width / inf.m_blockWidth) * (height / inf.m_blockHeight) * inf.m_blockSize * depth;
  70. }
  71. else
  72. {
  73. return width * height * depth * inf.m_texelSize;
  74. }
  75. }
  76. U8 computeMaxMipmapCount2d(U32 w, U32 h, U32 minSizeOfLastMip)
  77. {
  78. ANKI_ASSERT(w >= minSizeOfLastMip && h >= minSizeOfLastMip);
  79. U32 s = (w < h) ? w : h;
  80. U32 count = 0;
  81. while(s >= minSizeOfLastMip)
  82. {
  83. s /= 2;
  84. ++count;
  85. }
  86. ANKI_ASSERT(count < kMaxU8);
  87. return U8(count);
  88. }
  89. /// Compute max number of mipmaps for a 3D texture.
  90. U8 computeMaxMipmapCount3d(U32 w, U32 h, U32 d, U32 minSizeOfLastMip)
  91. {
  92. U32 s = (w < h) ? w : h;
  93. s = (s < d) ? s : d;
  94. U32 count = 0;
  95. while(s >= minSizeOfLastMip)
  96. {
  97. s /= 2;
  98. ++count;
  99. }
  100. ANKI_ASSERT(count < kMaxU8);
  101. return U8(count);
  102. }
  103. Error ShaderReflection::linkShaderReflection(const ShaderReflection& a, const ShaderReflection& b, ShaderReflection& c_)
  104. {
  105. ShaderReflection c;
  106. memcpy(&c.m_descriptor, &a.m_descriptor, sizeof(a.m_descriptor));
  107. for(U32 space = 0; space < kMaxRegisterSpaces; ++space)
  108. {
  109. for(U32 binding = 0; binding < b.m_descriptor.m_bindingCounts[space]; ++binding)
  110. {
  111. // Search for the binding in a
  112. const ShaderReflectionBinding& bbinding = b.m_descriptor.m_bindings[space][binding];
  113. Bool bindingFoundOnA = false;
  114. for(U32 binding2 = 0; binding2 < a.m_descriptor.m_bindingCounts[space]; ++binding2)
  115. {
  116. const ShaderReflectionBinding& abinding = a.m_descriptor.m_bindings[space][binding2];
  117. if(abinding.m_registerBindingPoint == bbinding.m_registerBindingPoint
  118. && descriptorTypeToHlslResourceType(abinding.m_type) == descriptorTypeToHlslResourceType(bbinding.m_type))
  119. {
  120. if(abinding != bbinding)
  121. {
  122. ANKI_GR_LOGE("Can't link shader reflection because of different bindings. Space %u binding %u", space, binding);
  123. return Error::kFunctionFailed;
  124. }
  125. bindingFoundOnA = true;
  126. break;
  127. }
  128. }
  129. if(!bindingFoundOnA)
  130. {
  131. c.m_descriptor.m_bindings[space][c.m_descriptor.m_bindingCounts[space]++] = bbinding;
  132. }
  133. }
  134. // Sort again
  135. std::sort(c.m_descriptor.m_bindings[space].getBegin(), c.m_descriptor.m_bindings[space].getBegin() + c.m_descriptor.m_bindingCounts[space]);
  136. }
  137. if(a.m_descriptor.m_fastConstantsSize != 0 && b.m_descriptor.m_fastConstantsSize != 0
  138. && a.m_descriptor.m_fastConstantsSize != b.m_descriptor.m_fastConstantsSize)
  139. {
  140. ANKI_GR_LOGE("Can't link shader reflection because fast constants size doesn't match");
  141. return Error::kFunctionFailed;
  142. }
  143. c.m_descriptor.m_fastConstantsSize = max(a.m_descriptor.m_fastConstantsSize, b.m_descriptor.m_fastConstantsSize);
  144. if(a.m_descriptor.m_d3dShaderBindingTableRecordConstantsSize != 0 && b.m_descriptor.m_d3dShaderBindingTableRecordConstantsSize != 0
  145. && a.m_descriptor.m_d3dShaderBindingTableRecordConstantsSize != b.m_descriptor.m_d3dShaderBindingTableRecordConstantsSize)
  146. {
  147. ANKI_GR_LOGE("Can't link shader reflection because SBT constants size is not correct");
  148. return Error::kFunctionFailed;
  149. }
  150. c.m_descriptor.m_d3dShaderBindingTableRecordConstantsSize =
  151. max(a.m_descriptor.m_d3dShaderBindingTableRecordConstantsSize, b.m_descriptor.m_d3dShaderBindingTableRecordConstantsSize);
  152. c.m_descriptor.m_d3dHasDrawId = a.m_descriptor.m_d3dHasDrawId || b.m_descriptor.m_d3dHasDrawId;
  153. c.m_descriptor.m_hasVkBindlessDescriptorSet = a.m_descriptor.m_hasVkBindlessDescriptorSet || b.m_descriptor.m_hasVkBindlessDescriptorSet;
  154. c.m_vertex.m_vkVertexAttributeLocations =
  155. (!!a.m_vertex.m_vertexAttributeMask) ? a.m_vertex.m_vkVertexAttributeLocations : b.m_vertex.m_vkVertexAttributeLocations;
  156. c.m_vertex.m_vertexAttributeMask = a.m_vertex.m_vertexAttributeMask | b.m_vertex.m_vertexAttributeMask;
  157. c.m_pixel.m_colorRenderTargetWritemask = a.m_pixel.m_colorRenderTargetWritemask | b.m_pixel.m_colorRenderTargetWritemask;
  158. c.m_pixel.m_discards = a.m_pixel.m_discards || b.m_pixel.m_discards;
  159. c_ = c;
  160. return Error::kNone;
  161. }
  162. StringList ShaderReflectionDescriptorRelated::toString() const
  163. {
  164. StringList list;
  165. for(U32 s = 0; s < kMaxRegisterSpaces; ++s)
  166. {
  167. for(U32 i = 0; i < m_bindingCounts[s]; ++i)
  168. {
  169. list.pushBackSprintf("space: %u, register: %u, type: %u", s, m_bindings[s][i].m_registerBindingPoint, U32(m_bindings[s][i].m_type));
  170. }
  171. }
  172. list.pushBackSprintf("Fast constants: %u", m_fastConstantsSize);
  173. list.pushBackSprintf("Has VK bindless sets: %u", m_hasVkBindlessDescriptorSet);
  174. list.pushBackSprintf("Has D3D DrawID: %u", m_d3dHasDrawId);
  175. return list;
  176. }
  177. StringList ShaderReflection::toString() const
  178. {
  179. StringList list = m_descriptor.toString();
  180. for(VertexAttributeSemantic attrib : EnumBitsIterable<VertexAttributeSemantic, VertexAttributeSemanticBit>(m_vertex.m_vertexAttributeMask))
  181. {
  182. list.pushBackSprintf("Vert attrib: %s", g_vertexAttributeSemanticNames[attrib].cstr());
  183. }
  184. list.pushBackSprintf("Color RT mask: %u", m_pixel.m_colorRenderTargetWritemask.getData()[0]);
  185. list.pushBackSprintf("Discards: %u", m_pixel.m_discards);
  186. return list;
  187. }
  188. } // end namespace anki