MaterialResource.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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. #include <AnKi/Resource/MaterialResource.h>
  6. #include <AnKi/Resource/ResourceManager.h>
  7. #include <AnKi/Resource/ImageResource.h>
  8. #include <AnKi/Util/Xml.h>
  9. namespace anki {
  10. static const Array<CString, U32(BuiltinMutatorId::COUNT)> BUILTIN_MUTATOR_NAMES = {
  11. {"NONE", "ANKI_TECHNIQUE", "ANKI_LOD", "ANKI_BONES", "ANKI_VELOCITY"}};
  12. static const Array<CString, U(RenderingTechnique::COUNT)> TECHNIQUE_NAMES = {
  13. {"GBuffer", "GBufferEarlyZ", "Shadow", "Forward", "RtShadow"}};
  14. // This is some trickery to select calling between XmlElement::getAttributeNumber and XmlElement::getAttributeNumbers
  15. namespace {
  16. template<typename T>
  17. class IsShaderVarDataTypeAnArray
  18. {
  19. public:
  20. static constexpr Bool VALUE = false;
  21. };
  22. #define ANKI_SVDT_MACRO(capital, type, baseType, rowCount, columnCount, isIntagralType) \
  23. template<> \
  24. class IsShaderVarDataTypeAnArray<type> \
  25. { \
  26. public: \
  27. static constexpr Bool VALUE = rowCount * columnCount > 1; \
  28. };
  29. #include <AnKi/Gr/ShaderVariableDataType.defs.h>
  30. #undef ANKI_SVDT_MACRO
  31. template<typename T, Bool isArray = IsShaderVarDataTypeAnArray<T>::VALUE>
  32. class GetAttribute
  33. {
  34. public:
  35. Error operator()(const XmlElement& el, T& out)
  36. {
  37. return el.getAttributeNumbers("value", out);
  38. }
  39. };
  40. template<typename T>
  41. class GetAttribute<T, false>
  42. {
  43. public:
  44. Error operator()(const XmlElement& el, T& out)
  45. {
  46. return el.getAttributeNumber("value", out);
  47. }
  48. };
  49. } // namespace
  50. MaterialVariable::MaterialVariable()
  51. {
  52. m_Mat4 = Mat4::getZero();
  53. }
  54. MaterialVariable::~MaterialVariable()
  55. {
  56. }
  57. class MaterialResource::Program
  58. {
  59. public:
  60. ShaderProgramResourcePtr m_prog;
  61. mutable Array4d<MaterialVariant, U(RenderingTechnique::COUNT), MAX_LOD_COUNT, 2, 2> m_variantMatrix;
  62. mutable RWMutex m_variantMatrixMtx;
  63. DynamicArray<PartialMutation> m_partialMutation; ///< Only with the non-builtins.
  64. U32 m_presentBuildinMutators = 0;
  65. U32 m_localUniformsStructIdx = 0; ///< Struct index in the program binary.
  66. U8 m_lodCount = 1;
  67. Program() = default;
  68. Program(const Program&) = delete; // Non-copyable
  69. Program(Program&& b)
  70. {
  71. *this = std::move(b);
  72. }
  73. Program& operator=(const Program& b) = delete; // Non-copyable
  74. Program& operator=(Program&& b)
  75. {
  76. m_prog = std::move(b.m_prog);
  77. for(RenderingTechnique t : EnumIterable<RenderingTechnique>())
  78. {
  79. for(U32 l = 0; l < MAX_LOD_COUNT; ++l)
  80. {
  81. for(U32 skin = 0; skin < 2; ++skin)
  82. {
  83. for(U32 vel = 0; vel < 2; ++vel)
  84. {
  85. m_variantMatrix[t][skin][skin][vel] = std::move(b.m_variantMatrix[t][skin][skin][vel]);
  86. }
  87. }
  88. }
  89. }
  90. m_partialMutation = std::move(b.m_partialMutation);
  91. m_presentBuildinMutators = b.m_presentBuildinMutators;
  92. m_localUniformsStructIdx = b.m_localUniformsStructIdx;
  93. m_lodCount = b.m_lodCount;
  94. return *this;
  95. }
  96. };
  97. MaterialResource::MaterialResource(ResourceManager* manager)
  98. : ResourceObject(manager)
  99. {
  100. memset(m_techniqueToProgram.getBegin(), 0xFF, m_techniqueToProgram.getSizeInBytes());
  101. }
  102. MaterialResource::~MaterialResource()
  103. {
  104. for(Program& p : m_programs)
  105. {
  106. p.m_partialMutation.destroy(getAllocator());
  107. }
  108. m_textures.destroy(getAllocator());
  109. for(MaterialVariable& var : m_vars)
  110. {
  111. var.m_name.destroy(getAllocator());
  112. }
  113. m_vars.destroy(getAllocator());
  114. m_programs.destroy(getAllocator());
  115. getAllocator().deallocate(m_prefilledLocalUniforms, 0);
  116. }
  117. const MaterialVariable* MaterialResource::tryFindVariableInternal(CString name) const
  118. {
  119. for(const MaterialVariable& v : m_vars)
  120. {
  121. if(v.m_name == name)
  122. {
  123. return &v;
  124. }
  125. }
  126. return nullptr;
  127. }
  128. Error MaterialResource::load(const ResourceFilename& filename, Bool async)
  129. {
  130. XmlDocument doc;
  131. XmlElement el;
  132. ANKI_CHECK(openFileParseXml(filename, doc));
  133. // <material>
  134. XmlElement rootEl;
  135. ANKI_CHECK(doc.getChildElement("material", rootEl));
  136. // <shaderPrograms>
  137. XmlElement shaderProgramsEl;
  138. ANKI_CHECK(rootEl.getChildElement("shaderPrograms", shaderProgramsEl));
  139. XmlElement shaderProgramEl;
  140. ANKI_CHECK(shaderProgramsEl.getChildElement("shaderProgram", shaderProgramEl));
  141. do
  142. {
  143. ANKI_CHECK(parseShaderProgram(shaderProgramEl, async));
  144. ANKI_CHECK(shaderProgramEl.getNextSiblingElement("shaderProgram", shaderProgramEl));
  145. } while(shaderProgramEl);
  146. ANKI_ASSERT(!!m_techniquesMask);
  147. // <inputs>
  148. BitSet<128> varsSet(false);
  149. ANKI_CHECK(rootEl.getChildElementOptional("inputs", el));
  150. if(el)
  151. {
  152. XmlElement inputEl;
  153. ANKI_CHECK(el.getChildElement("input", inputEl));
  154. do
  155. {
  156. ANKI_CHECK(parseInput(inputEl, async, varsSet));
  157. ANKI_CHECK(inputEl.getNextSiblingElement("input", inputEl));
  158. } while(inputEl);
  159. }
  160. if(varsSet.getEnabledBitCount() != m_vars.getSize())
  161. {
  162. ANKI_RESOURCE_LOGE("Forgot to set a default value in %u input variables",
  163. U32(m_vars.getSize() - varsSet.getEnabledBitCount()));
  164. return Error::USER_DATA;
  165. }
  166. prefillLocalUniforms();
  167. return Error::NONE;
  168. }
  169. Error MaterialResource::parseShaderProgram(XmlElement shaderProgramEl, Bool async)
  170. {
  171. // name
  172. CString shaderName;
  173. ANKI_CHECK(shaderProgramEl.getAttributeText("name", shaderName));
  174. if(!getManager().getGrManager().getDeviceCapabilities().m_rayTracingEnabled && shaderName.find("Rt") == 0)
  175. {
  176. // Skip RT programs when RT is disabled
  177. return Error::NONE;
  178. }
  179. StringAuto fname(getTempAllocator());
  180. fname.sprintf("ShaderBinaries/%s.ankiprogbin", shaderName.cstr());
  181. Program& prog = *m_programs.emplaceBack(getAllocator());
  182. ANKI_CHECK(getManager().loadResource(fname, prog.m_prog, async));
  183. // <mutation>
  184. XmlElement mutatorsEl;
  185. ANKI_CHECK(shaderProgramEl.getChildElementOptional("mutation", mutatorsEl));
  186. if(mutatorsEl)
  187. {
  188. ANKI_CHECK(parseMutators(mutatorsEl, prog));
  189. }
  190. // And find the builtin mutators
  191. ANKI_CHECK(findBuiltinMutators(prog));
  192. // Create the vars
  193. ANKI_CHECK(createVars(prog));
  194. return Error::NONE;
  195. }
  196. Error MaterialResource::createVars(Program& prog)
  197. {
  198. const ShaderProgramBinary& binary = prog.m_prog->getBinary();
  199. // Find struct
  200. const ShaderProgramBinaryStruct* localUniformsStruct = nullptr;
  201. for(const ShaderProgramBinaryStruct& strct : binary.m_structs)
  202. {
  203. if(CString(strct.m_name.getBegin()) == "AnKiLocalUniforms")
  204. {
  205. localUniformsStruct = &strct;
  206. break;
  207. }
  208. ++prog.m_localUniformsStructIdx;
  209. }
  210. if(localUniformsStruct == nullptr)
  211. {
  212. prog.m_localUniformsStructIdx = MAX_U32;
  213. }
  214. // Iterate all members of the local uniforms struct to add its members
  215. U32 offsetof = 0;
  216. for(U32 i = 0; localUniformsStruct && i < localUniformsStruct->m_members.getSize(); ++i)
  217. {
  218. const ShaderProgramBinaryStructMember& member = localUniformsStruct->m_members[i];
  219. const CString memberName = member.m_name.getBegin();
  220. // Check if it needs to be added
  221. Bool addIt = false;
  222. if(member.m_dependentMutator == MAX_U32)
  223. {
  224. addIt = true;
  225. }
  226. else
  227. {
  228. Bool found = false;
  229. for(const PartialMutation& m : prog.m_partialMutation)
  230. {
  231. if(m.m_mutator->m_name == binary.m_mutators[member.m_dependentMutator].m_name.getBegin())
  232. {
  233. if(m.m_value == member.m_dependentMutatorValue)
  234. {
  235. addIt = true;
  236. }
  237. found = true;
  238. break;
  239. }
  240. }
  241. if(!found)
  242. {
  243. ANKI_RESOURCE_LOGE("Incorrect combination of member variable %s and dependent mutator %s",
  244. memberName.cstr(), binary.m_mutators[member.m_dependentMutator].m_name.getBegin());
  245. return Error::USER_DATA;
  246. }
  247. }
  248. if(addIt)
  249. {
  250. MaterialVariable* var = tryFindVariable(memberName);
  251. if(var)
  252. {
  253. if(var->m_dataType != member.m_type || var->m_offsetInLocalUniforms != offsetof)
  254. {
  255. ANKI_RESOURCE_LOGE("Member variable doesn't match between techniques: %s", memberName.cstr());
  256. return Error::USER_DATA;
  257. }
  258. }
  259. else
  260. {
  261. // Check that there are no other vars that overlap with the current var. This could happen if
  262. // different programs have different signature for AnKiLocalUniforms
  263. for(const MaterialVariable& otherVar : m_vars)
  264. {
  265. if(!otherVar.isUniform())
  266. {
  267. continue;
  268. }
  269. const U32 aVarOffset = otherVar.m_offsetInLocalUniforms;
  270. const U32 aVarEnd = aVarOffset + getShaderVariableDataTypeInfo(otherVar.m_dataType).m_size;
  271. const U32 bVarOffset = offsetof;
  272. const U32 bVarEnd = bVarOffset + getShaderVariableDataTypeInfo(member.m_type).m_size;
  273. if((aVarOffset <= bVarOffset && aVarEnd > bVarOffset)
  274. || (bVarOffset <= aVarOffset && bVarEnd > aVarOffset))
  275. {
  276. ANKI_RESOURCE_LOGE("Member %s in AnKiLocalUniforms overlaps with %s. Check your shaders",
  277. memberName.cstr(), otherVar.m_name.cstr());
  278. return Error::USER_DATA;
  279. }
  280. }
  281. // All good, add it
  282. var = m_vars.emplaceBack(getAllocator());
  283. var->m_name.create(getAllocator(), memberName);
  284. var->m_offsetInLocalUniforms = offsetof;
  285. var->m_dataType = member.m_type;
  286. offsetof += getShaderVariableDataTypeInfo(member.m_type).m_size;
  287. }
  288. }
  289. }
  290. m_localUniformsSize = max(offsetof, m_localUniformsSize);
  291. // Iterate all variants of builtin mutators to gather the opaques
  292. ShaderProgramResourceVariantInitInfo initInfo(prog.m_prog);
  293. for(const PartialMutation& m : prog.m_partialMutation)
  294. {
  295. initInfo.addMutation(m.m_mutator->m_name, m.m_value);
  296. }
  297. Array<const ShaderProgramResourceMutator*, U(BuiltinMutatorId::COUNT)> mutatorPtrs = {};
  298. for(BuiltinMutatorId id : EnumIterable<BuiltinMutatorId>())
  299. {
  300. mutatorPtrs[id] = prog.m_prog->tryFindMutator(BUILTIN_MUTATOR_NAMES[id]);
  301. }
  302. #define ANKI_LOOP(builtIn) \
  303. for(U32 i = 0; \
  304. i \
  305. < ((mutatorPtrs[BuiltinMutatorId::builtIn]) ? mutatorPtrs[BuiltinMutatorId::builtIn]->m_values.getSize() : 1); \
  306. ++i) \
  307. { \
  308. if(mutatorPtrs[BuiltinMutatorId::builtIn]) \
  309. { \
  310. initInfo.addMutation(mutatorPtrs[BuiltinMutatorId::builtIn]->m_name, \
  311. mutatorPtrs[BuiltinMutatorId::builtIn]->m_values[i]); \
  312. }
  313. #define ANKI_LOOP_END() }
  314. ANKI_LOOP(TECHNIQUE)
  315. ANKI_LOOP(LOD)
  316. ANKI_LOOP(BONES)
  317. ANKI_LOOP(VELOCITY)
  318. {
  319. const ShaderProgramResourceVariant* variant;
  320. prog.m_prog->getOrCreateVariant(initInfo, variant);
  321. // Add opaque vars
  322. for(const ShaderProgramBinaryOpaqueInstance& instance : variant->getBinaryVariant().m_opaques)
  323. {
  324. const ShaderProgramBinaryOpaque& opaque = binary.m_opaques[instance.m_index];
  325. if(opaque.m_type == ShaderVariableDataType::SAMPLER)
  326. {
  327. continue;
  328. }
  329. const CString opaqueName = opaque.m_name.getBegin();
  330. MaterialVariable* var = tryFindVariable(opaqueName);
  331. if(var)
  332. {
  333. if(var->m_dataType != opaque.m_type || var->m_opaqueBinding != opaque.m_binding)
  334. {
  335. ANKI_RESOURCE_LOGE("Opaque variable doesn't match between techniques: %s", opaqueName.cstr());
  336. return Error::USER_DATA;
  337. }
  338. }
  339. else
  340. {
  341. // Check that there are no other opaque with the same binding
  342. for(const MaterialVariable& otherVar : m_vars)
  343. {
  344. if(!otherVar.isBoundableTexture())
  345. {
  346. continue;
  347. }
  348. if(otherVar.m_opaqueBinding == opaque.m_binding)
  349. {
  350. ANKI_RESOURCE_LOGE("Opaque variable %s has the same binding as %s. Check your shaders",
  351. otherVar.m_name.cstr(), opaqueName.cstr());
  352. return Error::USER_DATA;
  353. }
  354. }
  355. // All good, add it
  356. var = m_vars.emplaceBack(getAllocator());
  357. var->m_name.create(getAllocator(), opaqueName);
  358. var->m_opaqueBinding = opaque.m_binding;
  359. var->m_dataType = opaque.m_type;
  360. }
  361. }
  362. }
  363. ANKI_LOOP_END()
  364. ANKI_LOOP_END()
  365. ANKI_LOOP_END()
  366. ANKI_LOOP_END()
  367. #undef ANKI_LOOP
  368. #undef ANKI_LOOP_END
  369. return Error::NONE;
  370. }
  371. Error MaterialResource::parseMutators(XmlElement mutatorsEl, Program& prog)
  372. {
  373. XmlElement mutatorEl;
  374. ANKI_CHECK(mutatorsEl.getChildElement("mutator", mutatorEl));
  375. U32 mutatorCount = 0;
  376. ANKI_CHECK(mutatorEl.getSiblingElementsCount(mutatorCount));
  377. ++mutatorCount;
  378. ANKI_ASSERT(mutatorCount > 0);
  379. prog.m_partialMutation.create(getAllocator(), mutatorCount);
  380. mutatorCount = 0;
  381. do
  382. {
  383. PartialMutation& pmutation = prog.m_partialMutation[mutatorCount];
  384. // name
  385. CString mutatorName;
  386. ANKI_CHECK(mutatorEl.getAttributeText("name", mutatorName));
  387. if(mutatorName.isEmpty())
  388. {
  389. ANKI_RESOURCE_LOGE("Mutator name is empty");
  390. return Error::USER_DATA;
  391. }
  392. for(BuiltinMutatorId id : EnumIterable<BuiltinMutatorId>())
  393. {
  394. if(id == BuiltinMutatorId::NONE)
  395. {
  396. continue;
  397. }
  398. if(mutatorName == BUILTIN_MUTATOR_NAMES[id])
  399. {
  400. ANKI_RESOURCE_LOGE("Materials shouldn't list builtin mutators: %s", mutatorName.cstr());
  401. return Error::USER_DATA;
  402. }
  403. }
  404. if(mutatorName.find("ANKI_") == 0)
  405. {
  406. ANKI_RESOURCE_LOGE("Mutators can't start with ANKI_: %s", mutatorName.cstr());
  407. return Error::USER_DATA;
  408. }
  409. // value
  410. ANKI_CHECK(mutatorEl.getAttributeNumber("value", pmutation.m_value));
  411. // Find mutator
  412. pmutation.m_mutator = prog.m_prog->tryFindMutator(mutatorName);
  413. if(!pmutation.m_mutator)
  414. {
  415. ANKI_RESOURCE_LOGE("Mutator not found in program %s", &mutatorName[0]);
  416. return Error::USER_DATA;
  417. }
  418. if(!pmutation.m_mutator->valueExists(pmutation.m_value))
  419. {
  420. ANKI_RESOURCE_LOGE("Value %d is not part of the mutator %s", pmutation.m_value, mutatorName.cstr());
  421. return Error::USER_DATA;
  422. }
  423. // Advance
  424. ++mutatorCount;
  425. ANKI_CHECK(mutatorEl.getNextSiblingElement("mutator", mutatorEl));
  426. } while(mutatorEl);
  427. ANKI_ASSERT(mutatorCount == prog.m_partialMutation.getSize());
  428. return Error::NONE;
  429. }
  430. Error MaterialResource::findBuiltinMutators(Program& prog)
  431. {
  432. U builtinMutatorCount = 0;
  433. // ANKI_TECHNIQUE
  434. CString techniqueMutatorName = BUILTIN_MUTATOR_NAMES[BuiltinMutatorId::TECHNIQUE];
  435. const ShaderProgramResourceMutator* techniqueMutator = prog.m_prog->tryFindMutator(techniqueMutatorName);
  436. if(techniqueMutator)
  437. {
  438. for(U32 i = 0; i < techniqueMutator->m_values.getSize(); ++i)
  439. {
  440. const MutatorValue mvalue = techniqueMutator->m_values[i];
  441. if(mvalue >= MutatorValue(RenderingTechnique::COUNT) || mvalue < MutatorValue(RenderingTechnique::FIRST))
  442. {
  443. ANKI_RESOURCE_LOGE("Mutator %s has a wrong value %d", techniqueMutatorName.cstr(), mvalue);
  444. return Error::USER_DATA;
  445. }
  446. const RenderingTechnique techniqueId = RenderingTechnique(mvalue);
  447. const PtrSize progIdx = &prog - m_programs.getBegin();
  448. ANKI_ASSERT(progIdx < m_programs.getSize());
  449. m_techniqueToProgram[techniqueId] = U8(progIdx);
  450. const RenderingTechniqueBit mask = RenderingTechniqueBit(1 << techniqueId);
  451. if(!!(m_techniquesMask & mask))
  452. {
  453. ANKI_RESOURCE_LOGE("The %s technique appeared more than once", TECHNIQUE_NAMES[mvalue].cstr());
  454. return Error::USER_DATA;
  455. }
  456. m_techniquesMask |= mask;
  457. }
  458. ++builtinMutatorCount;
  459. prog.m_presentBuildinMutators |= U32(1 << BuiltinMutatorId::TECHNIQUE);
  460. }
  461. else
  462. {
  463. ANKI_RESOURCE_LOGE("Mutator %s should be present in every shader program referenced by a material",
  464. techniqueMutatorName.cstr());
  465. return Error::USER_DATA;
  466. }
  467. // ANKI_LOD
  468. CString lodMutatorName = BUILTIN_MUTATOR_NAMES[BuiltinMutatorId::LOD];
  469. const ShaderProgramResourceMutator* lodMutator = prog.m_prog->tryFindMutator(lodMutatorName);
  470. if(lodMutator)
  471. {
  472. if(lodMutator->m_values.getSize() > MAX_LOD_COUNT)
  473. {
  474. ANKI_RESOURCE_LOGE("Mutator %s should have at least %u values in the program", lodMutatorName.cstr(),
  475. U32(MAX_LOD_COUNT));
  476. return Error::USER_DATA;
  477. }
  478. for(U32 i = 0; i < lodMutator->m_values.getSize(); ++i)
  479. {
  480. if(lodMutator->m_values[i] != MutatorValue(i))
  481. {
  482. ANKI_RESOURCE_LOGE("Values of the %s mutator in the program are not the expected",
  483. lodMutatorName.cstr());
  484. return Error::USER_DATA;
  485. }
  486. }
  487. prog.m_lodCount = U8(lodMutator->m_values.getSize());
  488. ++builtinMutatorCount;
  489. prog.m_presentBuildinMutators |= U32(1 << BuiltinMutatorId::LOD);
  490. }
  491. // ANKI_BONES
  492. CString bonesMutatorName = BUILTIN_MUTATOR_NAMES[BuiltinMutatorId::BONES];
  493. const ShaderProgramResourceMutator* bonesMutator = prog.m_prog->tryFindMutator(bonesMutatorName);
  494. if(bonesMutator)
  495. {
  496. if(bonesMutator->m_values.getSize() != 2)
  497. {
  498. ANKI_RESOURCE_LOGE("Mutator %s should have 2 values in the program", bonesMutatorName.cstr());
  499. return Error::USER_DATA;
  500. }
  501. for(U32 i = 0; i < bonesMutator->m_values.getSize(); ++i)
  502. {
  503. if(bonesMutator->m_values[i] != MutatorValue(i))
  504. {
  505. ANKI_RESOURCE_LOGE("Values of the %s mutator in the program are not the expected",
  506. bonesMutatorName.cstr());
  507. return Error::USER_DATA;
  508. }
  509. }
  510. ++builtinMutatorCount;
  511. // Find if the bindings are present
  512. ConstWeakArray<ShaderProgramBinaryBlock> storageBlocks = prog.m_prog->getBinary().m_storageBlocks;
  513. U foundCount = 0;
  514. for(U32 i = 0; i < storageBlocks.getSize(); ++i)
  515. {
  516. const U32 binding = storageBlocks[i].m_binding;
  517. const U32 set = storageBlocks[i].m_set;
  518. if((binding == MATERIAL_BINDING_BONE_TRANSFORMS || binding == MATERIAL_BINDING_PREVIOUS_BONE_TRANSFORMS)
  519. && set == MATERIAL_SET_LOCAL)
  520. {
  521. ++foundCount;
  522. }
  523. }
  524. if(foundCount != 2)
  525. {
  526. ANKI_RESOURCE_LOGE("The shader has %s mutation but not the storage buffers for the bone transforms",
  527. bonesMutatorName.cstr());
  528. return Error::USER_DATA;
  529. }
  530. m_supportsSkinning = true;
  531. prog.m_presentBuildinMutators |= U32(1 << BuiltinMutatorId::BONES);
  532. }
  533. // VELOCITY
  534. CString velocityMutatorName = BUILTIN_MUTATOR_NAMES[BuiltinMutatorId::VELOCITY];
  535. const ShaderProgramResourceMutator* velocityMutator = prog.m_prog->tryFindMutator(velocityMutatorName);
  536. if(velocityMutator)
  537. {
  538. if(velocityMutator->m_values.getSize() != 2)
  539. {
  540. ANKI_RESOURCE_LOGE("Mutator %s should have 2 values in the program", velocityMutatorName.cstr());
  541. return Error::USER_DATA;
  542. }
  543. for(U32 i = 0; i < velocityMutator->m_values.getSize(); ++i)
  544. {
  545. if(velocityMutator->m_values[i] != MutatorValue(i))
  546. {
  547. ANKI_RESOURCE_LOGE("Values of the %s mutator in the program are not the expected",
  548. velocityMutatorName.cstr());
  549. return Error::USER_DATA;
  550. }
  551. }
  552. ++builtinMutatorCount;
  553. prog.m_presentBuildinMutators |= U32(1 << BuiltinMutatorId::VELOCITY);
  554. }
  555. if(prog.m_partialMutation.getSize() + builtinMutatorCount != prog.m_prog->getMutators().getSize())
  556. {
  557. ANKI_RESOURCE_LOGE("Some mutatators are unacounted for");
  558. return Error::USER_DATA;
  559. }
  560. return Error::NONE;
  561. }
  562. Error MaterialResource::parseInput(XmlElement inputEl, Bool async, BitSet<128>& varsSet)
  563. {
  564. // Get var name
  565. CString varName;
  566. ANKI_CHECK(inputEl.getAttributeText("name", varName));
  567. // Try find var
  568. MaterialVariable* foundVar = tryFindVariable(varName);
  569. if(!foundVar)
  570. {
  571. ANKI_RESOURCE_LOGE("Input name is wrong, variable not found: %s", varName.cstr());
  572. return Error::USER_DATA;
  573. }
  574. // Set check
  575. const U32 idx = U32(foundVar - m_vars.getBegin());
  576. if(varsSet.get(idx))
  577. {
  578. ANKI_RESOURCE_LOGE("Input already has a value: %s", varName.cstr());
  579. return Error::USER_DATA;
  580. }
  581. varsSet.set(idx);
  582. // Set the value
  583. if(foundVar->isBoundableTexture())
  584. {
  585. CString texfname;
  586. ANKI_CHECK(inputEl.getAttributeText("value", texfname));
  587. ANKI_CHECK(getManager().loadResource(texfname, foundVar->m_image, async));
  588. m_textures.emplaceBack(getAllocator(), foundVar->m_image->getTexture());
  589. }
  590. else if(foundVar->m_dataType == ShaderVariableDataType::U32)
  591. {
  592. // U32 is a bit special. It might be a number or a bindless texture
  593. CString value;
  594. ANKI_CHECK(inputEl.getAttributeText("value", value));
  595. // Check if the value has letters
  596. Bool containsAlpharithmetic = false;
  597. for(Char c : value)
  598. {
  599. if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'a'))
  600. {
  601. containsAlpharithmetic = true;
  602. break;
  603. }
  604. }
  605. // If it has letters it's a texture
  606. if(containsAlpharithmetic)
  607. {
  608. ANKI_CHECK(getManager().loadResource(value, foundVar->m_image, async));
  609. foundVar->m_U32 = foundVar->m_image->getTextureView()->getOrCreateBindlessTextureIndex();
  610. }
  611. else
  612. {
  613. ANKI_CHECK(GetAttribute<U32>()(inputEl, foundVar->m_U32));
  614. }
  615. }
  616. else
  617. {
  618. switch(foundVar->m_dataType)
  619. {
  620. #define ANKI_SVDT_MACRO(capital, type, baseType, rowCount, columnCount, isIntagralType) \
  621. case ShaderVariableDataType::capital: \
  622. ANKI_CHECK(GetAttribute<type>()(inputEl, foundVar->ANKI_CONCATENATE(m_, type))); \
  623. break;
  624. #include <AnKi/Gr/ShaderVariableDataType.defs.h>
  625. #undef ANKI_SVDT_MACRO
  626. default:
  627. ANKI_ASSERT(0);
  628. break;
  629. }
  630. }
  631. return Error::NONE;
  632. }
  633. void MaterialResource::prefillLocalUniforms()
  634. {
  635. if(m_localUniformsSize == 0)
  636. {
  637. return;
  638. }
  639. m_prefilledLocalUniforms = getAllocator().allocate(m_localUniformsSize, 1);
  640. memset(m_prefilledLocalUniforms, 0, m_localUniformsSize);
  641. for(const MaterialVariable& var : m_vars)
  642. {
  643. if(!var.isUniform())
  644. {
  645. continue;
  646. }
  647. switch(var.m_dataType)
  648. {
  649. #define ANKI_SVDT_MACRO(capital, type, baseType, rowCount, columnCount, isIntagralType) \
  650. case ShaderVariableDataType::capital: \
  651. ANKI_ASSERT(var.m_offsetInLocalUniforms + sizeof(type) <= m_localUniformsSize); \
  652. memcpy(static_cast<U8*>(m_prefilledLocalUniforms) + var.m_offsetInLocalUniforms, &var.m_##type, sizeof(type)); \
  653. break;
  654. #include <AnKi/Gr/ShaderVariableDataType.defs.h>
  655. #undef ANKI_SVDT_MACRO
  656. default:
  657. ANKI_ASSERT(0);
  658. break;
  659. }
  660. }
  661. }
  662. const MaterialVariant& MaterialResource::getOrCreateVariant(const RenderingKey& key_) const
  663. {
  664. RenderingKey key = key_;
  665. ANKI_ASSERT(m_techniqueToProgram[key.getRenderingTechnique()] != MAX_U8);
  666. const Program& prog = m_programs[m_techniqueToProgram[key.getRenderingTechnique()]];
  667. key.setLod(min<U32>(prog.m_lodCount - 1, key.getLod()));
  668. ANKI_ASSERT(!key.getSkinned() || !!(prog.m_presentBuildinMutators & U32(1 << BuiltinMutatorId::BONES)));
  669. ANKI_ASSERT(!key.getVelocity() || !!(prog.m_presentBuildinMutators & U32(1 << BuiltinMutatorId::VELOCITY)));
  670. MaterialVariant& variant =
  671. prog.m_variantMatrix[key.getRenderingTechnique()][key.getLod()][key.getSkinned()][key.getVelocity()];
  672. // Check if it's initialized
  673. {
  674. RLockGuard<RWMutex> lock(prog.m_variantMatrixMtx);
  675. if(ANKI_LIKELY(variant.m_prog.isCreated()))
  676. {
  677. return variant;
  678. }
  679. }
  680. // Not initialized, init it
  681. WLockGuard<RWMutex> lock(prog.m_variantMatrixMtx);
  682. // Check again
  683. if(variant.m_prog.isCreated())
  684. {
  685. return variant;
  686. }
  687. ShaderProgramResourceVariantInitInfo initInfo(prog.m_prog);
  688. for(const PartialMutation& m : prog.m_partialMutation)
  689. {
  690. initInfo.addMutation(m.m_mutator->m_name, m.m_value);
  691. }
  692. initInfo.addMutation(BUILTIN_MUTATOR_NAMES[BuiltinMutatorId::TECHNIQUE], MutatorValue(key.getRenderingTechnique()));
  693. if(!!(prog.m_presentBuildinMutators & U32(1 << BuiltinMutatorId::LOD)))
  694. {
  695. initInfo.addMutation(BUILTIN_MUTATOR_NAMES[BuiltinMutatorId::LOD], MutatorValue(key.getLod()));
  696. }
  697. if(!!(prog.m_presentBuildinMutators & U32(1 << BuiltinMutatorId::BONES)))
  698. {
  699. initInfo.addMutation(BUILTIN_MUTATOR_NAMES[BuiltinMutatorId::BONES], MutatorValue(key.getSkinned()));
  700. }
  701. if(!!(prog.m_presentBuildinMutators & U32(1 << BuiltinMutatorId::VELOCITY)))
  702. {
  703. initInfo.addMutation(BUILTIN_MUTATOR_NAMES[BuiltinMutatorId::VELOCITY], MutatorValue(key.getVelocity()));
  704. }
  705. const ShaderProgramResourceVariant* progVariant;
  706. prog.m_prog->getOrCreateVariant(initInfo, progVariant);
  707. variant.m_prog = progVariant->getProgram();
  708. if(!!(RenderingTechniqueBit(1 << key.getRenderingTechnique()) & RenderingTechniqueBit::ALL_RT))
  709. {
  710. variant.m_rtShaderGroupHandleIndex = progVariant->getShaderGroupHandleIndex();
  711. }
  712. return variant;
  713. }
  714. } // end namespace anki