3
0

AzslCompiler.cpp 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Casting/numeric_cast.h>
  9. #include <AzCore/Component/ComponentApplicationBus.h>
  10. #include <AzCore/IO/SystemFile.h>
  11. #include <AzCore/std/string/string.h>
  12. #include <AzCore/std/string/conversions.h>
  13. #include <AzCore/std/containers/unordered_map.h>
  14. #include <AzCore/Memory/OSAllocator.h>
  15. #include <Atom/RHI.Edit/ShaderPlatformInterface.h>
  16. #include <Atom/RHI.Edit/Utils.h>
  17. #include <Atom/RPI.Edit/Common/JsonUtils.h>
  18. #include <Atom/RPI.Reflect/Shader/ShaderOptionGroupLayout.h>
  19. #include <AzCore/Serialization/Json/JsonUtils.h>
  20. #include <AzFramework/IO/LocalFileIO.h>
  21. #include <AzFramework/IO/FileOperations.h> // [GFX TODO] Remove when [ATOM-15472]
  22. #include <AzFramework/StringFunc/StringFunc.h>
  23. #include <AzFramework/Process/ProcessCommunicator.h>
  24. #include <AzFramework/Process/ProcessWatcher.h>
  25. #include <AzslCompiler.h>
  26. #include <CommonFiles/CommonTypes.h>
  27. #include <ShaderBuilder_Traits_Platform.h>
  28. // To quickly test changes to the AZSL compiler, this settings registry key may be used to override the executable used
  29. constexpr static const char* AzslCompilerOverridePath = "/O3DE/Atom/AzslCompilerOverridePath";
  30. namespace AZ
  31. {
  32. namespace ShaderBuilder
  33. {
  34. AZStd::any AsAny(const rapidjson::Value& value);
  35. static const char* ShaderCompilerName = "AZSL Compiler";
  36. AzslCompiler::AzslCompiler(const AZStd::string& inputFilePath, const AZStd::string& tempFolder)
  37. : m_inputFilePath(inputFilePath),
  38. m_tempFolder(tempFolder)
  39. {
  40. }
  41. bool AzslCompiler::Compile(const AZStd::string& compilerParams,
  42. const AZStd::string& outputFilePath) const
  43. {
  44. // Shader compiler executable
  45. AZStd::string azslcPath = "Builders/AZSLc/";
  46. azslcPath += AZ_TRAIT_ATOM_SHADERBUILDER_AZSLC;
  47. if (auto setReg = AZ::Interface<SettingsRegistryInterface>::Get())
  48. {
  49. AZStd::string overridePath;
  50. if (setReg->Get(overridePath, AzslCompilerOverridePath))
  51. {
  52. AZ_TraceOnce("AzslCompiler", "AZSLc executable override specified, using %s", overridePath.c_str());
  53. azslcPath = AZStd::move(overridePath);
  54. }
  55. }
  56. // Compilation parameters
  57. AZStd::string azslcCommandOptions = AZStd::string::format("\"%s\"", m_inputFilePath.c_str());
  58. // NOTE: On macOS AZSLc executable fails if there is an extra space in the command line when there are no compiler parameters,
  59. // checking if there is no compiler parameters to avoid adding the extra space.
  60. if (!compilerParams.empty())
  61. {
  62. AZStd::vector<AZStd::string> tokenizedArguments;
  63. AzFramework::StringFunc::Tokenize(compilerParams, tokenizedArguments, " ");
  64. AZStd::string cleanParams;
  65. AzFramework::StringFunc::Join(cleanParams, tokenizedArguments.begin(), tokenizedArguments.end(), " ");
  66. azslcCommandOptions += AZStd::string::format(" %s", cleanParams.c_str());
  67. }
  68. if (!outputFilePath.empty())
  69. {
  70. azslcCommandOptions += AZStd::string::format(" -o \"%s\"", outputFilePath.c_str());
  71. }
  72. // Run Shader Compiler
  73. if (!RHI::ExecuteShaderCompiler(azslcPath, azslcCommandOptions, m_inputFilePath, m_tempFolder, "AZSLc"))
  74. {
  75. return false;
  76. }
  77. return true;
  78. }
  79. bool AzslCompiler::EmitShader(AZ::IO::GenericStream& outputStream,
  80. const AZStd::string& compilerParams) const
  81. {
  82. // .azslin for input and .azslout for output (in the same folder)
  83. AZStd::string hlslOutputFile = m_inputFilePath;
  84. AzFramework::StringFunc::Path::ReplaceExtension(hlslOutputFile, "azslout");
  85. // Add the flag for the warning level
  86. const AZStd::string parameters = compilerParams;
  87. if (!Compile(parameters, hlslOutputFile))
  88. {
  89. return false;
  90. }
  91. AZ::IO::FileIOStream readAZSLOutput(hlslOutputFile.data(), AZ::IO::OpenMode::ModeRead);
  92. if (!readAZSLOutput.IsOpen())
  93. {
  94. AZ_Error(ShaderCompilerName, false, "Failed because the shader source file \"%s\" could not be opened %s", hlslOutputFile.data());
  95. return false;
  96. }
  97. if (!readAZSLOutput.CanRead())
  98. {
  99. AZ_Error(ShaderCompilerName, false, "Failed because the shader source file \"%s\" could not be read %s", hlslOutputFile.data());
  100. readAZSLOutput.Close();
  101. AZ::IO::SystemFile::Delete(hlslOutputFile.c_str());
  102. return false;
  103. }
  104. AZStd::string readBuffer;
  105. readBuffer.resize(readAZSLOutput.GetLength());
  106. readAZSLOutput.Read(readBuffer.size(), readBuffer.data());
  107. AZ_Assert(outputStream.CanWrite(), "Failed because the output stream for azslcout is not open for write!");
  108. outputStream.Write(readBuffer.size(), readBuffer.data());
  109. readAZSLOutput.Close();
  110. return true;
  111. }
  112. namespace SubProducts = ShaderBuilderUtility::AzslSubProducts;
  113. Outcome<SubProducts::Paths> AzslCompiler::EmitFullData(const AZStd::vector<AZStd::string>& azslcArguments,
  114. const AZStd::string& outputFile) const
  115. {
  116. const auto azslArgsStr = RHI::ShaderBuildArguments::ListAsString(azslcArguments);
  117. bool success = Compile(azslArgsStr, outputFile);
  118. if (!success)
  119. {
  120. return Failure();
  121. }
  122. // reconstruct the paths that has been created by azslc after a successful --full build, to inform this method's caller of the artifacts to look for:
  123. SubProducts::Paths productPaths = SubProducts::Paths(SubProducts::Paths::capacity());
  124. for (auto subProduct : SubProducts::SuffixListMembers)
  125. {
  126. AZStd::string subProductFilePath = outputFile.empty() ? m_inputFilePath : outputFile; // that's a reproduction of azslc's behavior (no "-o" = input name is used)
  127. AzFramework::StringFunc::Path::ReplaceExtension(subProductFilePath, subProduct.m_string.data());
  128. // append .json if it's one of those subs:
  129. auto listOfJsons = { SubProducts::ia, SubProducts::om, SubProducts::srg, SubProducts::options, SubProducts::bindingdep };
  130. subProductFilePath += AZStd::any_of(AZ_BEGIN_END(listOfJsons), [&](auto v) { return v == subProduct.m_value; }) ? ".json" : "";
  131. productPaths[subProduct.m_value] = subProductFilePath;
  132. }
  133. productPaths[SubProducts::azslin] = GetInputFilePath(); // post-fixup this one after the loop, because it's not an output of azslc, it's an output of the builder though.
  134. return { productPaths };
  135. }
  136. bool AzslCompiler::EmitInputAssembler(rapidjson::Document& output) const
  137. {
  138. return CompileToFileAndPrepareJsonDocument(output, "--ia", "ia.json") == BuildResult::Success;
  139. }
  140. bool AzslCompiler::ParseIaPopulateStructData(const rapidjson::Document& input, const AZStd::string& vertexEntryName, StructData& outStructData) const
  141. {
  142. AZStd::vector<StructParameter> structMembers;
  143. StructParameter inputStructParams;
  144. bool matchEntry = false;
  145. const rapidjson::Value& attributesLayout = input["inputLayouts"];
  146. AZ_Assert(attributesLayout.IsArray(), "Attribute inputLayouts is not an array");
  147. for (rapidjson::Value::ConstValueIterator itr = attributesLayout.Begin(); itr != attributesLayout.End(); ++itr)
  148. {
  149. const rapidjson::Value& attributeEntry = *itr;
  150. AZ_Assert(attributeEntry.IsObject(), "Attribute is not an object");
  151. if (attributeEntry.HasMember("entry") && (attributeEntry["entry"].GetString() == vertexEntryName))
  152. {
  153. matchEntry = true;
  154. }
  155. if (attributeEntry.HasMember("streams") && matchEntry)
  156. {
  157. const rapidjson::Value& streamArray = attributeEntry["streams"];
  158. AZ_Assert(streamArray.IsArray(), "Attribute streams is not an array");
  159. for (rapidjson::Value::ConstValueIterator itr3 = streamArray.Begin(); itr3 != streamArray.End(); ++itr3)
  160. {
  161. const rapidjson::Value& attributeMember = *itr3;
  162. AZ_Assert(attributeMember.IsObject(), "Attribute is not an object");
  163. for (rapidjson::Value::ConstMemberIterator itr2 = attributeMember.MemberBegin(); itr2 != attributeMember.MemberEnd(); ++itr2)
  164. {
  165. AZStd::string name = itr2->name.GetString();
  166. const rapidjson::Value& value = itr2->value;
  167. rapidjson::Type type = value.GetType();
  168. switch (type)
  169. {
  170. case rapidjson::kStringType:
  171. if (name == "baseType")
  172. {
  173. inputStructParams.m_variable.m_type = StringToBaseType(itr2->value.GetString());
  174. }
  175. else if (name == "name")
  176. {
  177. inputStructParams.m_variable.m_name = itr2->value.GetString();
  178. }
  179. else if (name == "semanticName")
  180. {
  181. inputStructParams.m_semanticText = itr2->value.GetString();
  182. }
  183. break;
  184. case rapidjson::kArrayType:
  185. if (name == "dimensions")
  186. {
  187. for (rapidjson::SizeType i = 0; i < value.Size(); i++)
  188. {
  189. ArrayItem arrayItem;
  190. arrayItem.m_count = value[i].GetInt();
  191. arrayItem.m_text = "";
  192. inputStructParams.m_variable.m_arrayDefinition.push_back(arrayItem);
  193. }
  194. }
  195. break;
  196. case rapidjson::kNumberType:
  197. if (name == "cols")
  198. {
  199. inputStructParams.m_variable.m_cols = static_cast<uint8_t>(itr2->value.GetInt());
  200. }
  201. else if (name == "rows")
  202. {
  203. inputStructParams.m_variable.m_rows = static_cast<uint8_t>(itr2->value.GetInt());
  204. }
  205. else if (name == "semanticIndex")
  206. {
  207. inputStructParams.m_semanticIndex = itr2->value.GetInt();
  208. if (inputStructParams.m_semanticIndex > 0)
  209. {
  210. inputStructParams.m_semanticText = inputStructParams.m_semanticText + AZStd::to_string(inputStructParams.m_semanticIndex);
  211. }
  212. }
  213. break;
  214. default:
  215. break;
  216. }
  217. }
  218. structMembers.push_back(inputStructParams);
  219. }
  220. }
  221. if (matchEntry)
  222. {
  223. break;
  224. }
  225. }
  226. if (!matchEntry)
  227. {
  228. return false;
  229. }
  230. outStructData.m_id = vertexEntryName;
  231. outStructData.m_members = structMembers;
  232. return true;
  233. }
  234. bool AzslCompiler::EmitOutputMerger(rapidjson::Document& output) const
  235. {
  236. return CompileToFileAndPrepareJsonDocument(output, "--om", "om.json") == BuildResult::Success;
  237. }
  238. bool AzslCompiler::ParseOmPopulateStructData(const rapidjson::Document& input, const AZStd::string& fragmentShaderName, StructData& outStructData) const
  239. {
  240. AZStd::vector<StructParameter> structMembers;
  241. StructParameter outputStructParams;
  242. bool matchEntry = false;
  243. const rapidjson::Value& attributesLayout = input["outputLayouts"];
  244. AZ_Assert(attributesLayout.IsArray(), "Attribute outputLayouts is not an array");
  245. for (rapidjson::Value::ConstValueIterator itr = attributesLayout.Begin(); itr != attributesLayout.End(); ++itr)
  246. {
  247. const rapidjson::Value& attributeEntry = *itr;
  248. AZ_Assert(attributeEntry.IsObject(), "Attribute is not an object");
  249. if (attributeEntry.HasMember("entry") && (attributeEntry["entry"].GetString() == fragmentShaderName))
  250. {
  251. matchEntry = true;
  252. }
  253. if (attributeEntry.HasMember("renderTargets") && matchEntry)
  254. {
  255. const rapidjson::Value& streamArray = attributeEntry["renderTargets"];
  256. AZ_Assert(streamArray.IsArray(), "Attribute renderTargets is not an array");
  257. for (rapidjson::Value::ConstValueIterator itr3 = streamArray.Begin(); itr3 != streamArray.End(); ++itr3)
  258. {
  259. const rapidjson::Value& attributeMember = *itr3;
  260. AZ_Assert(attributeMember.IsObject(), "Attribute is not an object");
  261. for (rapidjson::Value::ConstMemberIterator itr2 = attributeMember.MemberBegin(); itr2 != attributeMember.MemberEnd(); ++itr2)
  262. {
  263. AZStd::string name = itr2->name.GetString();
  264. const rapidjson::Value& value = itr2->value;
  265. rapidjson::Type type = value.GetType();
  266. switch (type)
  267. {
  268. case rapidjson::kStringType:
  269. if (name == "baseType")
  270. {
  271. outputStructParams.m_variable.m_type = StringToBaseType(itr2->value.GetString());
  272. }
  273. else if (name == "semanticName")
  274. {
  275. outputStructParams.m_semanticText = itr2->value.GetString();
  276. }
  277. else if (name == "format")
  278. {
  279. outputStructParams.m_format = StringToFormat(itr2->value.GetString());
  280. }
  281. break;
  282. case rapidjson::kNumberType:
  283. if (name == "cols")
  284. {
  285. outputStructParams.m_variable.m_cols = static_cast<uint8_t>(itr2->value.GetInt());
  286. }
  287. else if (name == "semanticIndex")
  288. {
  289. outputStructParams.m_semanticIndex = itr2->value.GetInt();
  290. if (outputStructParams.m_semanticIndex > 0)
  291. {
  292. outputStructParams.m_semanticText = outputStructParams.m_semanticText + AZStd::to_string(outputStructParams.m_semanticIndex);
  293. }
  294. }
  295. break;
  296. default:
  297. break;
  298. }
  299. }
  300. structMembers.push_back(outputStructParams);
  301. }
  302. }
  303. if (matchEntry)
  304. {
  305. break;
  306. }
  307. }
  308. if (!matchEntry)
  309. {
  310. return false;
  311. }
  312. outStructData.m_id = fragmentShaderName;
  313. outStructData.m_members = structMembers;
  314. return true;
  315. }
  316. bool AzslCompiler::ParseIaPopulateFunctionData(const rapidjson::Document& input, AzslFunctions& functionData) const
  317. {
  318. AZStd::vector<StructParameter> structMembers;
  319. StructParameter inputStructParams;
  320. const rapidjson::Value& attributesLayout = input["inputLayouts"];
  321. AZ_Assert(attributesLayout.IsArray(), "Attribute inputLayouts renderTargets is not an array");
  322. for (rapidjson::Value::ConstValueIterator itr = attributesLayout.Begin(); itr != attributesLayout.End(); ++itr)
  323. {
  324. const rapidjson::Value& attributeEntry = *itr;
  325. AZ_Assert(attributeEntry.IsObject(), "Attribute is not an object");
  326. FunctionData functionEntry;
  327. if (!attributeEntry.HasMember("entry"))
  328. {
  329. continue;
  330. }
  331. functionEntry.m_name = attributeEntry["entry"].GetString();
  332. if (attributeEntry.HasMember("streams"))
  333. {
  334. const rapidjson::Value& streamArray = attributeEntry["streams"];
  335. AZ_Assert(streamArray.IsArray(), "Attribute streams is not an array");
  336. if (streamArray.Size() > 0)
  337. {
  338. functionEntry.m_hasShaderStageVaryings = true;
  339. }
  340. }
  341. // Note: switch to using range-based for loop in RapidJSON v1.1.0 - for (const auto& attr : attributeEntry)
  342. for (auto attr = attributeEntry.MemberBegin(); attr != attributeEntry.MemberEnd(); ++attr)
  343. {
  344. const auto& attrName = attr->name.GetString();
  345. if (std::strcmp(attrName, "entry") == 0 || std::strcmp(attrName, "streams") == 0)
  346. {
  347. continue;
  348. }
  349. RHI::ShaderStageAttributeArguments argList;
  350. if (attr->value.IsArray())
  351. {
  352. for (rapidjson::Value::ConstValueIterator argItr = attr->value.Begin(); argItr != attr->value.End(); ++argItr)
  353. {
  354. argList.push_back(AsAny(*argItr));
  355. }
  356. }
  357. functionEntry.attributesList[Name{attrName}] = argList;
  358. }
  359. functionData.push_back(functionEntry);
  360. }
  361. if (functionData.empty())
  362. {
  363. AZ_Error(ShaderCompilerName, false, "AzslCompiler::EmitFunctionData The number of valid shader entry functions in %s was 0!", m_inputFilePath.c_str());
  364. return false;
  365. }
  366. return true;
  367. }
  368. bool AzslCompiler::ParseSrgPopulateRootConstantData(const rapidjson::Document& input, RootConstantData& rootConstantData) const
  369. {
  370. if (input.HasMember("RootConstantBuffer"))
  371. {
  372. const rapidjson::Value& rootConstantBufferValue = input["RootConstantBuffer"];
  373. AZ_Assert(rootConstantBufferValue.IsObject(), "RootConstantBuffer is not an object");
  374. for (rapidjson::Value::ConstMemberIterator itr = rootConstantBufferValue.MemberBegin(); itr != rootConstantBufferValue.MemberEnd(); ++itr)
  375. {
  376. AZStd::string_view rootConstantBufferMemberName = itr->name.GetString();
  377. const rapidjson::Value& rootConstantBufferMemberValue = itr->value;
  378. if (rootConstantBufferMemberName == "bufferForRootConstants")
  379. {
  380. AZ_Assert(rootConstantBufferMemberValue.IsObject(), "bufferForRootConstants is not an object");
  381. for (rapidjson::Value::ConstMemberIterator itr2 = rootConstantBufferMemberValue.MemberBegin(); itr2 != rootConstantBufferMemberValue.MemberEnd(); ++itr2)
  382. {
  383. AZStd::string_view bufferForRootConstantsName = itr2->name.GetString();
  384. const rapidjson::Value& bufferForRootConstantsValue = itr2->value;
  385. if (bufferForRootConstantsName == "id")
  386. {
  387. AZStd::string nameId = bufferForRootConstantsValue.GetString();
  388. rootConstantData.m_bindingInfo.m_nameId = nameId;
  389. }
  390. else if (bufferForRootConstantsName == "index")
  391. {
  392. rootConstantData.m_bindingInfo.m_registerId = bufferForRootConstantsValue.GetInt();
  393. }
  394. else if (bufferForRootConstantsName == "space")
  395. {
  396. rootConstantData.m_bindingInfo.m_space = bufferForRootConstantsValue.GetInt();
  397. }
  398. else if (bufferForRootConstantsName == "sizeInBytes")
  399. {
  400. rootConstantData.m_bindingInfo.m_sizeInBytes = bufferForRootConstantsValue.GetInt();
  401. AZ_Assert(rootConstantData.m_bindingInfo.m_sizeInBytes > 0, "Invalid constant buffer size %d", rootConstantData.m_bindingInfo.m_sizeInBytes);
  402. }
  403. }
  404. }
  405. else if (rootConstantBufferMemberName == "inputsForRootConstants")
  406. {
  407. AZ_Assert(rootConstantBufferMemberValue.IsArray(), "inputsForRootConstants is not an array");
  408. for (rapidjson::Value::ConstValueIterator itr2 = rootConstantBufferMemberValue.Begin(); itr2 != rootConstantBufferMemberValue.End(); ++itr2)
  409. {
  410. const rapidjson::Value& rootConstantBufferValue2 = *itr2;
  411. AZ_Assert(rootConstantBufferValue2.IsObject(), "Entry in inputsForRootConstants is not an object");
  412. SrgConstantData rootConstantInputs;
  413. for (rapidjson::Value::ConstMemberIterator itr3 = rootConstantBufferValue2.MemberBegin(); itr3 != rootConstantBufferValue2.MemberEnd(); ++itr3)
  414. {
  415. AZStd::string rootConstantBufferMemberName2 = itr3->name.GetString();
  416. const rapidjson::Value& rootConstantBufferMemberValue2 = itr3->value;
  417. if (rootConstantBufferMemberName2 == "constantId")
  418. {
  419. AZStd::string nameId = rootConstantBufferMemberValue2.GetString();
  420. rootConstantInputs.m_nameId = nameId;
  421. }
  422. else if (rootConstantBufferMemberName2 == "constantByteOffset")
  423. {
  424. rootConstantInputs.m_constantByteOffset = rootConstantBufferMemberValue2.GetInt();
  425. }
  426. else if (rootConstantBufferMemberName2 == "constantByteSize")
  427. {
  428. rootConstantInputs.m_constantByteSize = rootConstantBufferMemberValue2.GetInt();
  429. }
  430. else if (rootConstantBufferMemberName2 == "qualifiedName")
  431. {
  432. rootConstantInputs.m_qualifiedName = rootConstantBufferMemberValue2.GetString();
  433. }
  434. else if (rootConstantBufferMemberName2 == "typeKind")
  435. {
  436. rootConstantInputs.m_typeKind = rootConstantBufferMemberValue2.GetString();
  437. }
  438. else if (rootConstantBufferMemberName2 == "typeName")
  439. {
  440. rootConstantInputs.m_typeName = rootConstantBufferMemberValue2.GetString();
  441. }
  442. else if (rootConstantBufferMemberName2 == "typeDimensions")
  443. {
  444. AZ_Assert(rootConstantBufferMemberValue2.IsArray(), "typeDimensions is not an array");
  445. for (rapidjson::Value::ConstValueIterator itr4 = rootConstantBufferMemberValue2.Begin(); itr4 != rootConstantBufferMemberValue2.End(); ++itr4)
  446. {
  447. const rapidjson::Value& typeDimensionsValue = *itr4;
  448. ArrayItem arrayItem;
  449. arrayItem.m_count = typeDimensionsValue.GetUint();
  450. rootConstantInputs.m_typeDimensions.push_back(arrayItem);
  451. }
  452. }
  453. }
  454. rootConstantData.m_constants.push_back(rootConstantInputs);
  455. }
  456. }
  457. }
  458. }
  459. return true;
  460. }
  461. const AZStd::string& AzslCompiler::GetInputFilePath() const
  462. {
  463. return m_inputFilePath;
  464. }
  465. bool AzslCompiler::EmitSrgData(rapidjson::Document& output, const AZStd::string& extraCompilerParams) const
  466. {
  467. AZStd::string parameters = AZStd::string::format("--srg %s", extraCompilerParams.c_str());
  468. return CompileToFileAndPrepareJsonDocument(output, parameters.c_str(), "srg.json") == BuildResult::Success;
  469. }
  470. bool AzslCompiler::ParseSrgPopulateSrgData(const rapidjson::Document& input, SrgDataContainer& outSrgData) const
  471. {
  472. const rapidjson::Value& shaderResourceGroups = input["ShaderResourceGroups"];
  473. AZ_Assert(shaderResourceGroups.IsArray(), "Attribute ShaderResourceGroups is not an array");
  474. for (rapidjson::Value::ConstValueIterator itr = shaderResourceGroups.Begin(); itr != shaderResourceGroups.End(); ++itr)
  475. {
  476. const rapidjson::Value& srgEntry = *itr;
  477. AZ_Assert(srgEntry.IsObject(), "Value is not an object");
  478. SrgData srgData;
  479. for (rapidjson::Value::ConstMemberIterator itr2 = srgEntry.MemberBegin(); itr2 != srgEntry.MemberEnd(); ++itr2)
  480. {
  481. AZStd::string attributeName = itr2->name.GetString();
  482. const rapidjson::Value& subArray = itr2->value;
  483. if (attributeName == "bindingSlot")
  484. {
  485. srgData.m_bindingSlot.m_index = itr2->value.GetInt();
  486. }
  487. else if (attributeName == "id")
  488. {
  489. srgData.m_name = itr2->value.GetString();
  490. }
  491. else if (attributeName == "fallbackName")
  492. {
  493. srgData.m_fallbackName = itr2->value.GetString();
  494. }
  495. else if (attributeName == "fallbackSize")
  496. {
  497. srgData.m_fallbackSize = itr2->value.GetInt();
  498. }
  499. if (attributeName == "originalFileName")
  500. {
  501. srgData.m_containingFileName = itr2->value.GetString();
  502. }
  503. else if (attributeName == "inputsForImageViews")
  504. {
  505. AZ_Assert(subArray.IsArray(), "Object inputsForImageViews is not an array");
  506. for (rapidjson::Value::ConstValueIterator itr3 = subArray.Begin(); itr3 != subArray.End(); ++itr3)
  507. {
  508. const rapidjson::Value& attributeArray = *itr3;
  509. AZ_Assert(attributeArray.IsObject(), "Value is not an object");
  510. TextureSrgData inputsForImageViews;
  511. for (rapidjson::Value::ConstMemberIterator itr4 = attributeArray.MemberBegin(); itr4 != attributeArray.MemberEnd(); ++itr4)
  512. {
  513. AZStd::string attributeArrayMemberName = itr4->name.GetString();
  514. if (attributeArrayMemberName == "count")
  515. {
  516. inputsForImageViews.m_count = itr4->value.GetInt();
  517. }
  518. else if (attributeArrayMemberName == "id")
  519. {
  520. inputsForImageViews.m_nameId = itr4->value.GetString();
  521. }
  522. else if (attributeArrayMemberName == "type")
  523. {
  524. inputsForImageViews.m_type = StringToTextureType(itr4->value.GetString());
  525. }
  526. else if (attributeArrayMemberName == "usage")
  527. {
  528. if(AzFramework::StringFunc::Equal(itr4->value.GetString(), "Read"))
  529. {
  530. inputsForImageViews.m_isReadOnlyType = true;
  531. }
  532. else if (AzFramework::StringFunc::Equal(itr4->value.GetString(), "ReadWrite"))
  533. {
  534. inputsForImageViews.m_isReadOnlyType = false;
  535. }
  536. else
  537. {
  538. AZ_Assert(false, "%s is an unexpected usage type", itr4->value.GetString());
  539. }
  540. }
  541. else if (attributeArrayMemberName == "index")
  542. {
  543. inputsForImageViews.m_registerId = itr4->value.GetInt();
  544. }
  545. else if (attributeArrayMemberName == "space")
  546. {
  547. inputsForImageViews.m_spaceId = itr4->value.GetInt();
  548. }
  549. }
  550. srgData.m_textures.push_back(inputsForImageViews);
  551. }
  552. }
  553. else if (attributeName == "inputsForSamplers")
  554. {
  555. AZ_Assert(subArray.IsArray(), "Object inputsForSamplers is not an array");
  556. for (rapidjson::Value::ConstValueIterator itr3 = subArray.Begin(); itr3 != subArray.End(); ++itr3)
  557. {
  558. const rapidjson::Value& attributeArray = *itr3;
  559. AZ_Assert(attributeArray.IsObject(), "Value is not an object");
  560. SamplerSrgData sampler;
  561. AZ::RHI::SamplerState samplerStateDesc;
  562. for (rapidjson::Value::ConstMemberIterator itr4 = attributeArray.MemberBegin(); itr4 != attributeArray.MemberEnd(); ++itr4)
  563. {
  564. const char* attributeArrayMemberName = itr4->name.GetString();
  565. if (AzFramework::StringFunc::Equal(attributeArrayMemberName, "addressU"))
  566. {
  567. samplerStateDesc.m_addressU = StringToTextureAddressMode(itr4->value.GetString());
  568. }
  569. else if (AzFramework::StringFunc::Equal(attributeArrayMemberName, "addressV"))
  570. {
  571. samplerStateDesc.m_addressV = StringToTextureAddressMode(itr4->value.GetString());
  572. }
  573. else if (AzFramework::StringFunc::Equal(attributeArrayMemberName, "addressW"))
  574. {
  575. samplerStateDesc.m_addressW = StringToTextureAddressMode(itr4->value.GetString());
  576. }
  577. else if (AzFramework::StringFunc::Equal(attributeArrayMemberName, "anisotropyEnable"))
  578. {
  579. samplerStateDesc.m_anisotropyEnable = itr4->value.GetBool();
  580. }
  581. else if (AzFramework::StringFunc::Equal(attributeArrayMemberName, "anisotropyMax"))
  582. {
  583. samplerStateDesc.m_anisotropyMax = itr4->value.GetInt();
  584. }
  585. else if (AzFramework::StringFunc::Equal(attributeArrayMemberName, "borderColor"))
  586. {
  587. samplerStateDesc.m_borderColor = StringToTextureBorderColor(itr4->value.GetString());
  588. }
  589. else if (AzFramework::StringFunc::Equal(attributeArrayMemberName, "comparisonFunc"))
  590. {
  591. samplerStateDesc.m_comparisonFunc = StringToComparisonFunc(itr4->value.GetString());
  592. }
  593. else if (AzFramework::StringFunc::Equal(attributeArrayMemberName, "filterMag"))
  594. {
  595. samplerStateDesc.m_filterMag = StringToFilterMode(itr4->value.GetString());
  596. }
  597. else if (AzFramework::StringFunc::Equal(attributeArrayMemberName, "filterMin"))
  598. {
  599. samplerStateDesc.m_filterMin = StringToFilterMode(itr4->value.GetString());
  600. }
  601. else if (AzFramework::StringFunc::Equal(attributeArrayMemberName, "filterMip"))
  602. {
  603. samplerStateDesc.m_filterMip = StringToFilterMode(itr4->value.GetString());
  604. }
  605. else if (AzFramework::StringFunc::Equal(attributeArrayMemberName, "mipLodBias"))
  606. {
  607. samplerStateDesc.m_mipLodBias = aznumeric_caster(itr4->value.GetDouble());
  608. }
  609. else if (AzFramework::StringFunc::Equal(attributeArrayMemberName, "mipLodMax"))
  610. {
  611. samplerStateDesc.m_mipLodMax = aznumeric_caster(itr4->value.GetDouble());
  612. }
  613. else if (AzFramework::StringFunc::Equal(attributeArrayMemberName, "mipLodMin"))
  614. {
  615. samplerStateDesc.m_mipLodMin = aznumeric_caster(itr4->value.GetDouble());
  616. }
  617. else if (AzFramework::StringFunc::Equal(attributeArrayMemberName, "reductionType"))
  618. {
  619. samplerStateDesc.m_reductionType = StringToReductionType(itr4->value.GetString());
  620. }
  621. else if (AzFramework::StringFunc::Equal(attributeArrayMemberName, "id"))
  622. {
  623. sampler.m_nameId = itr4->value.GetString();
  624. }
  625. else if (AzFramework::StringFunc::Equal(attributeArrayMemberName, "isDynamic"))
  626. {
  627. sampler.m_isDynamic = itr4->value.GetBool();
  628. }
  629. else if (AzFramework::StringFunc::Equal(attributeArrayMemberName, "count"))
  630. {
  631. sampler.m_count = itr4->value.GetInt();
  632. }
  633. else if (AzFramework::StringFunc::Equal(attributeArrayMemberName, "index"))
  634. {
  635. sampler.m_registerId = itr4->value.GetInt();
  636. }
  637. else if (AzFramework::StringFunc::Equal(attributeArrayMemberName, "space"))
  638. {
  639. sampler.m_spaceId = itr4->value.GetInt();
  640. }
  641. }
  642. sampler.m_descriptor = samplerStateDesc;
  643. srgData.m_samplers.push_back(sampler);
  644. }
  645. }
  646. else if (attributeName == "inputsForBufferViews")
  647. {
  648. AZ_Assert(subArray.IsArray(), "Object inputsForBufferViews is not an array");
  649. for (rapidjson::Value::ConstValueIterator itr3 = subArray.Begin(); itr3 != subArray.End(); ++itr3)
  650. {
  651. const rapidjson::Value& attributeArray = *itr3;
  652. AZ_Assert(attributeArray.IsObject(), "Value is not an object");
  653. BufferSrgData buffer;
  654. ConstantBufferData constantBuffer;
  655. bool isConstantBuffer = false;
  656. if(AzFramework::StringFunc::StartsWith(attributeArray["type"].GetString(), "ConstantBuffer"))
  657. {
  658. isConstantBuffer = true;
  659. }
  660. if (isConstantBuffer)
  661. {
  662. for (rapidjson::Value::ConstMemberIterator itr4 = attributeArray.MemberBegin(); itr4 != attributeArray.MemberEnd(); ++itr4)
  663. {
  664. AZStd::string attributeArrayMemberName = itr4->name.GetString();
  665. if (attributeArrayMemberName == "count")
  666. {
  667. uint32_t count = itr4->value.GetInt();
  668. AZ_Assert(count == 1, "Invalid constant buffer count %d", count);
  669. constantBuffer.m_count = count;
  670. }
  671. else if (attributeArrayMemberName == "id")
  672. {
  673. constantBuffer.m_nameId = itr4->value.GetString();
  674. }
  675. else if (attributeArrayMemberName == "stride")
  676. {
  677. constantBuffer.m_strideSize = itr4->value.GetInt();
  678. }
  679. else if (attributeArrayMemberName == "index")
  680. {
  681. constantBuffer.m_registerId = itr4->value.GetInt();
  682. }
  683. else if (attributeArrayMemberName == "space")
  684. {
  685. constantBuffer.m_spaceId = itr4->value.GetInt();
  686. }
  687. }
  688. srgData.m_constantBuffers.push_back(constantBuffer);
  689. }
  690. else
  691. {
  692. for (rapidjson::Value::ConstMemberIterator itr4 = attributeArray.MemberBegin(); itr4 != attributeArray.MemberEnd(); ++itr4)
  693. {
  694. AZStd::string attributeArrayMemberName = itr4->name.GetString();
  695. if (attributeArrayMemberName == "count")
  696. {
  697. buffer.m_count = itr4->value.GetInt();
  698. }
  699. else if (attributeArrayMemberName == "id")
  700. {
  701. buffer.m_nameId = itr4->value.GetString();
  702. }
  703. else if (attributeArrayMemberName == "type")
  704. {
  705. buffer.m_type = StringToBufferType(itr4->value.GetString());
  706. }
  707. else if (attributeArrayMemberName == "usage")
  708. {
  709. if (AzFramework::StringFunc::Equal(itr4->value.GetString(), "Read"))
  710. {
  711. buffer.m_isReadOnlyType = true;
  712. }
  713. else if (AzFramework::StringFunc::Equal(itr4->value.GetString(), "ReadWrite"))
  714. {
  715. buffer.m_isReadOnlyType = false;
  716. }
  717. else
  718. {
  719. AZ_Assert(false, "%s is an unexpected usage type", itr4->value.GetString());
  720. }
  721. }
  722. else if (attributeArrayMemberName == "stride")
  723. {
  724. buffer.m_strideSize = itr4->value.GetInt();
  725. }
  726. else if (attributeArrayMemberName == "index")
  727. {
  728. buffer.m_registerId = itr4->value.GetInt();
  729. }
  730. else if (attributeArrayMemberName == "space")
  731. {
  732. buffer.m_spaceId = itr4->value.GetInt();
  733. }
  734. }
  735. srgData.m_buffers.push_back(buffer);
  736. }
  737. }
  738. }
  739. else if (attributeName == "inputsForSRGConstants")
  740. {
  741. AZ_Assert(subArray.IsArray(), "Object inputsForSRGConstants is not an array");
  742. for (rapidjson::Value::ConstValueIterator itr3 = subArray.Begin(); itr3 != subArray.End(); ++itr3)
  743. {
  744. const rapidjson::Value& attributeArray = *itr3;
  745. AZ_Assert(attributeArray.IsObject(), "Value is not an object");
  746. SrgConstantData srgConstants;
  747. for (rapidjson::Value::ConstMemberIterator itr4 = attributeArray.MemberBegin(); itr4 != attributeArray.MemberEnd(); ++itr4)
  748. {
  749. AZStd::string attributeArrayMemberName = itr4->name.GetString();
  750. const rapidjson::Value& attributeArrayMemberValue = itr4->value;
  751. if (attributeArrayMemberName == "constantByteOffset")
  752. {
  753. srgConstants.m_constantByteOffset = itr4->value.GetInt();
  754. }
  755. else if (attributeArrayMemberName == "constantByteSize")
  756. {
  757. srgConstants.m_constantByteSize = itr4->value.GetInt();
  758. }
  759. else if (attributeArrayMemberName == "constantId")
  760. {
  761. srgConstants.m_nameId = itr4->value.GetString();
  762. }
  763. else if (attributeArrayMemberName == "qualifiedName") // This is a meta data
  764. {
  765. srgConstants.m_qualifiedName = itr4->value.GetString();
  766. }
  767. else if (attributeArrayMemberName == "typeKind") // This is a meta data
  768. {
  769. srgConstants.m_typeKind = itr4->value.GetString();
  770. }
  771. else if (attributeArrayMemberName == "typeName") // This is a meta data
  772. {
  773. srgConstants.m_typeName = itr4->value.GetString();
  774. }
  775. else if (attributeArrayMemberName == "typeDimensions") // This is a meta data
  776. {
  777. for (rapidjson::SizeType i = 0; i < attributeArrayMemberValue.Size(); i++)
  778. {
  779. ArrayItem arrayItem;
  780. arrayItem.m_count = attributeArrayMemberValue[i].GetInt();
  781. arrayItem.m_text = "";
  782. srgConstants.m_typeDimensions.push_back(arrayItem);
  783. }
  784. }
  785. }
  786. srgData.m_srgConstantData.push_back(srgConstants);
  787. }
  788. }
  789. else if (attributeName == "bufferForSRGConstants")
  790. {
  791. for (rapidjson::Value::ConstMemberIterator itr3 = subArray.MemberBegin(); itr3 != subArray.MemberEnd(); ++itr3)
  792. {
  793. AZStd::string attributeArrayMemberName = itr3->name.GetString();
  794. if (attributeArrayMemberName == "index")
  795. {
  796. srgData.m_srgConstantDataRegisterId = itr3->value.GetInt();
  797. }
  798. else if (attributeArrayMemberName == "space")
  799. {
  800. srgData.m_srgConstantDataSpaceId = itr3->value.GetInt();
  801. }
  802. }
  803. }
  804. }
  805. outSrgData.push_back(srgData);
  806. }
  807. return true;
  808. }
  809. bool AzslCompiler::EmitOptionsList(rapidjson::Document& output) const
  810. {
  811. return CompileToFileAndPrepareJsonDocument(output, "--options", "options.json") == BuildResult::Success;
  812. }
  813. bool AzslCompiler::ParseOptionsPopulateOptionGroupLayout(
  814. const rapidjson::Document& input,
  815. RPI::Ptr<RPI::ShaderOptionGroupLayout>& shaderOptionGroupLayout,
  816. bool& outUseSpecializationConstants) const
  817. {
  818. auto totalBitOffset = (uint32_t) 0u;
  819. auto onSuccess = [&shaderOptionGroupLayout, &totalBitOffset]()
  820. {
  821. if (totalBitOffset == 0)
  822. {
  823. AZStd::vector<RPI::ShaderOptionValuePair> idIndexList;
  824. idIndexList.push_back( { Name("false"), RPI::ShaderOptionValue(0) } );
  825. idIndexList.push_back( { Name("true"), RPI::ShaderOptionValue(1) } );
  826. RPI::ShaderOptionDescriptor shaderOption(Name("DefaultOption"),
  827. RPI::ShaderOptionType::Boolean,
  828. totalBitOffset,
  829. 0,
  830. idIndexList,
  831. Name("false"));
  832. shaderOptionGroupLayout->AddShaderOption(shaderOption);
  833. totalBitOffset += shaderOption.GetBitCount();
  834. }
  835. shaderOptionGroupLayout->Finalize();
  836. return true;
  837. };
  838. auto onFail = [&shaderOptionGroupLayout]()
  839. {
  840. shaderOptionGroupLayout->Finalize();
  841. return false;
  842. };
  843. outUseSpecializationConstants = false;
  844. if (input.HasMember("specializationConstants"))
  845. {
  846. outUseSpecializationConstants = input["specializationConstants"].GetBool();
  847. }
  848. const rapidjson::Value& shaderOptions = input["ShaderOptions"];
  849. AZ_Assert(shaderOptions.IsArray(), "Attribute ShaderOptions must be an array");
  850. uint32_t explicitlyOrdered = 0;
  851. uint32_t implicitlyOrdered = 0;
  852. for (rapidjson::Value::ConstValueIterator optItr = shaderOptions.Begin(); optItr != shaderOptions.End(); ++optItr)
  853. {
  854. const rapidjson::Value& optionEntry = *optItr;
  855. AZ_Assert(optionEntry.IsObject(), "Expected option entry to be an object!");
  856. Name defaultValueId = optionEntry.HasMember("defaultValue") ? Name(optionEntry["defaultValue"].GetString()) : Name();
  857. const AZStd::string optionName = optionEntry.HasMember("name") ? optionEntry["name"].GetString() : "";
  858. [[maybe_unused]] const bool valuesAreRange = optionEntry.HasMember("range") ? optionEntry["range"].GetBool() : false;
  859. const bool isPredefinedType = optionEntry.HasMember("kind") ? AzFramework::StringFunc::Equal(optionEntry["kind"].GetString(), "predefined") : false;
  860. auto optionType = RPI::ShaderOptionType::Unknown;
  861. if (isPredefinedType && optionEntry.HasMember("type"))
  862. { // Bool or int
  863. const auto typeName = AZStd::string_view(optionEntry["type"].GetString());
  864. if (typeName.find("bool") != AZStd::string::npos)
  865. {
  866. optionType = RPI::ShaderOptionType::Boolean;
  867. }
  868. else if (typeName.find("int") != AZStd::string::npos)
  869. {
  870. AZ_Assert(valuesAreRange, "Integer options must have a range!");
  871. optionType = RPI::ShaderOptionType::IntegerRange;
  872. }
  873. }
  874. else
  875. { // We don't support complex structures for options yet, so the only user-defined type is an enumeration.
  876. optionType = RPI::ShaderOptionType::Enumeration;
  877. }
  878. AZStd::vector<RPI::ShaderOptionValuePair> idIndexList;
  879. if (optionEntry.HasMember("values"))
  880. {
  881. uint32_t optIndex = 0;
  882. for (rapidjson::Value::ConstValueIterator valItr = optionEntry["values"].Begin(); valItr != optionEntry["values"].End(); ++valItr)
  883. {
  884. if (optionType == RPI::ShaderOptionType::IntegerRange)
  885. {
  886. auto intValue = atoi(valItr->GetString());
  887. idIndexList.push_back( { Name(valItr->GetString()), RPI::ShaderOptionValue(intValue) } );
  888. }
  889. else
  890. {
  891. idIndexList.push_back( { Name(valItr->GetString()), RPI::ShaderOptionValue(optIndex++) } );
  892. }
  893. }
  894. }
  895. if (optionName.empty())
  896. {
  897. AZ_Error(ShaderCompilerName, false, "New option in file '%s' must specify an option name!", m_inputFilePath.c_str());
  898. return onFail();
  899. }
  900. if (idIndexList.empty())
  901. {
  902. AZ_Error(ShaderCompilerName, false, "Option '%s' must have at least one value!", optionName.c_str());
  903. return onFail();
  904. }
  905. if (optionType == RPI::ShaderOptionType::IntegerRange && idIndexList.size() != 2)
  906. {
  907. AZ_Error(ShaderCompilerName, false, "Option '%s' is marked as integer range and must provide exactly two values [min, max]!", optionName.c_str());
  908. return onFail();
  909. }
  910. if (defaultValueId.IsEmpty())
  911. {
  912. defaultValueId = idIndexList[0].first;
  913. AZ_Printf(ShaderCompilerName, "Option {%s} doesn't provide a default value, using {%s} instead.", optionName.c_str(), defaultValueId.GetCStr());
  914. }
  915. { // Unnamed scope to limit the use of shaderOption
  916. if (!optionEntry.HasMember("keyOffset") || !optionEntry.HasMember("keySize"))
  917. {
  918. AZ_Error(ShaderCompilerName, false, "Option {%s} must specify keyOffset and keySize. You might want to bump your AZSLc to ver.", optionName.c_str());
  919. return onFail();
  920. }
  921. const uint32_t keyOffset = optionEntry["keyOffset"].GetUint();
  922. const uint32_t keySize = optionEntry["keySize"].GetUint();
  923. uint32_t order;
  924. if (optionEntry.HasMember("order"))
  925. {
  926. order = optionEntry["order"].GetUint();
  927. explicitlyOrdered++;
  928. }
  929. else
  930. {
  931. order = implicitlyOrdered;
  932. implicitlyOrdered++;
  933. }
  934. uint32_t cost = 0;
  935. if (optionEntry.HasMember("costImpact"))
  936. {
  937. cost = optionEntry["costImpact"].GetUint();
  938. }
  939. int specializationId = -1;
  940. if (optionEntry.HasMember("specializationId"))
  941. {
  942. specializationId = optionEntry["specializationId"].GetInt();
  943. }
  944. RPI::ShaderOptionDescriptor shaderOption(Name(optionName),
  945. optionType,
  946. keyOffset,
  947. order,
  948. idIndexList,
  949. defaultValueId,
  950. cost,
  951. specializationId);
  952. if (!shaderOptionGroupLayout->AddShaderOption(shaderOption))
  953. {
  954. // AddShaderOption will report error messages
  955. return onFail();
  956. }
  957. totalBitOffset = keyOffset + keySize;
  958. if (keySize != shaderOption.GetBitCount())
  959. {
  960. AZ_Error(ShaderCompilerName, false, "Option {%} specifies a different bit size than calculated!", optionName.c_str());
  961. return onFail();
  962. }
  963. }
  964. }
  965. if (explicitlyOrdered > 0 && implicitlyOrdered > 0)
  966. {
  967. AZ_Error(ShaderCompilerName, false, "Either all or none of the options must have the \"order\" attribute defined. It's not allowed to add it to some options and not to others.");
  968. return onFail();
  969. }
  970. return onSuccess();
  971. }
  972. bool AzslCompiler::EmitBindingDependencies(rapidjson::Document& output) const
  973. {
  974. return PrepareJsonDocument(output, "bindingdep.json") == BuildResult::Success;
  975. }
  976. bool AzslCompiler::ParseBindingdepPopulateBindingDependencies(
  977. const rapidjson::Document& input, BindingDependencies& bindingDependencies) const
  978. {
  979. for (rapidjson::Value::ConstMemberIterator itr = input.MemberBegin(); itr != input.MemberEnd(); ++itr)
  980. {
  981. AZStd::string_view srgKey = itr->name.GetString();
  982. const rapidjson::Value& srgValue = itr->value;
  983. AZ_Assert(srgValue.IsObject(), "Root is not an object");
  984. BindingDependencies::SrgResources srg;
  985. for (rapidjson::Value::ConstMemberIterator itr2 = srgValue.MemberBegin(); itr2 != srgValue.MemberEnd(); ++itr2)
  986. {
  987. AZStd::string_view resourceKey = itr2->name.GetString();
  988. const rapidjson::Value& srgEntryValue = itr2->value;
  989. AZ_Assert(srgEntryValue.IsObject(), "Value in SRG is not an object");
  990. BindingDependencies::Resource* binding = nullptr;
  991. for (rapidjson::Value::ConstMemberIterator itr3 = srgEntryValue.MemberBegin(); itr3 != srgEntryValue.MemberEnd(); ++itr3)
  992. {
  993. AZStd::string_view srgMemberName = itr3->name.GetString();
  994. const rapidjson::Value& srgMemberValue = itr3->value;
  995. if (srgMemberName == "binding")
  996. {
  997. AZ_Assert(srgMemberValue.IsObject(), "binding is not an object");
  998. BindingDependencies::Register registerId = ~static_cast<BindingDependencies::Register>(0x0);
  999. BindingDependencies::Register registerSpace = ~static_cast<BindingDependencies::Register>(0x0);
  1000. uint32_t registerSpan = 0;
  1001. for (rapidjson::Value::ConstMemberIterator itr4 = srgMemberValue.MemberBegin(); itr4 != srgMemberValue.MemberEnd(); ++itr4)
  1002. {
  1003. AZStd::string bindingName = itr4->name.GetString();
  1004. const rapidjson::Value& bindingValue = itr4->value;
  1005. if (bindingName == "type")
  1006. {
  1007. AZStd::string type = bindingValue.GetString();
  1008. binding = type == "SrgConstantCB" ? &srg.m_srgConstantsDependencies.m_binding
  1009. : &srg.m_resources[resourceKey];
  1010. binding->m_type = type;
  1011. binding->m_selfName = resourceKey;
  1012. }
  1013. else if (bindingName == "index-merged")
  1014. {
  1015. registerId = bindingValue.GetUint();
  1016. }
  1017. else if (bindingName == "range")
  1018. {
  1019. registerSpan = bindingValue.GetUint();
  1020. }
  1021. else if (bindingName == "space-merged")
  1022. {
  1023. registerSpace = bindingValue.GetUint();
  1024. }
  1025. }
  1026. AZ_Assert(binding, "binding type is not defined");
  1027. binding->m_registerId = registerId;
  1028. binding->m_registerSpan = registerSpan;
  1029. binding->m_registerSpace = registerSpace;
  1030. }
  1031. else if (srgMemberName == "dependentFunctions")
  1032. {
  1033. AZ_Assert(srgMemberValue.IsArray(), "dependentFunctions is not an array");
  1034. AZ_Assert(binding, "binding is not defined");
  1035. for (rapidjson::Value::ConstValueIterator itr4 = srgMemberValue.Begin(); itr4 != srgMemberValue.End(); ++itr4)
  1036. {
  1037. const rapidjson::Value& dependentFunctionsValue = *itr4;
  1038. binding->m_dependentFunctions.emplace_back(dependentFunctionsValue.GetString());
  1039. }
  1040. }
  1041. else if (srgMemberName == "participantConstants")
  1042. {
  1043. AZ_Assert(srgMemberValue.IsArray(), "participantConstants is not an array");
  1044. for (rapidjson::Value::ConstValueIterator itr4 = srgMemberValue.Begin(); itr4 != srgMemberValue.End(); ++itr4)
  1045. {
  1046. const rapidjson::Value& participantConstantsValue = *itr4;
  1047. srg.m_srgConstantsDependencies.m_partipicantConstants.emplace_back(participantConstantsValue.GetString());
  1048. }
  1049. }
  1050. }
  1051. }
  1052. bindingDependencies.m_orderedSrgs.push_back(srg);
  1053. bindingDependencies.m_srgNameToVectorIndex[srgKey] = aznumeric_cast<int>(bindingDependencies.m_orderedSrgs.size()) - 1;
  1054. }
  1055. return true;
  1056. }
  1057. AzslCompiler::BuildResult AzslCompiler::CompileToFileAndPrepareJsonDocument(
  1058. rapidjson::Document& outputJson,
  1059. const char* compilerCommandSwitch,
  1060. const char* outputExtension,
  1061. AfterRead deleteOutputFileAfterReading /*= AfterRead::Keep*/) const
  1062. {
  1063. // Emitted output filename and path is same as input file, but extension is .json
  1064. AZStd::string outputFile = m_inputFilePath;
  1065. AzFramework::StringFunc::Path::ReplaceExtension(outputFile, outputExtension);
  1066. AZ_Error("AzslCompiler", AZ::IO::SystemFile::Exists(outputFile.c_str()), "Destination file %s, exists. will be overwritten", outputFile.c_str());
  1067. if (!Compile(compilerCommandSwitch, outputFile))
  1068. {
  1069. return BuildResult::CompilationFailed;
  1070. }
  1071. auto readJsonResult = JsonSerializationUtils::ReadJsonFile(outputFile, AZ::RPI::JsonUtils::DefaultMaxFileSize);
  1072. if (readJsonResult.IsSuccess())
  1073. {
  1074. outputJson = readJsonResult.TakeValue();
  1075. if (deleteOutputFileAfterReading == AfterRead::Delete)
  1076. {
  1077. AZ::IO::SystemFile::Delete(outputFile.c_str()); // Delete the .json file after reading
  1078. }
  1079. }
  1080. return BuildResult::Success;
  1081. }
  1082. AzslCompiler::BuildResult AzslCompiler::PrepareJsonDocument(
  1083. rapidjson::Document& outputJson,
  1084. const char* outputExtension) const
  1085. {
  1086. // Emitted output filename and path is same as input file, but extension is .json
  1087. AZStd::string outputFile = m_inputFilePath;
  1088. AzFramework::StringFunc::Path::ReplaceExtension(outputFile, outputExtension);
  1089. auto readJsonResult = JsonSerializationUtils::ReadJsonFile(outputFile, AZ::RPI::JsonUtils::DefaultMaxFileSize);
  1090. if (readJsonResult.IsSuccess())
  1091. {
  1092. outputJson = readJsonResult.TakeValue();
  1093. return BuildResult::Success;
  1094. }
  1095. return BuildResult::JsonReadbackFailed;
  1096. }
  1097. AZStd::any AsAny(const rapidjson::Value& value)
  1098. {
  1099. if (value.IsBool())
  1100. {
  1101. return AZStd::any{ value.GetBool() };
  1102. }
  1103. else if (value.IsInt())
  1104. {
  1105. return AZStd::any{ value.GetInt() };
  1106. }
  1107. else if (value.IsDouble())
  1108. {
  1109. return AZStd::any{ value.GetDouble() };
  1110. }
  1111. else if (value.IsString())
  1112. {
  1113. return AZStd::any{ value.GetString() };
  1114. }
  1115. // Not any type we recognize
  1116. AZ_Error(ShaderBuilder::ShaderCompilerName, false, "Unrecognized argument type!");
  1117. // Return empty element to prevent compiler warnings
  1118. return AZStd::any{ };
  1119. }
  1120. } // namespace ShaderBuilder
  1121. } // namespace AZ