MeshDrawPacket.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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 <Atom/RPI.Public/MeshDrawPacket.h>
  9. #include <Atom/RPI.Public/RPIUtils.h>
  10. #include <Atom/RPI.Public/Shader/ShaderResourceGroup.h>
  11. #include <Atom/RPI.Public/Shader/ShaderSystemInterface.h>
  12. #include <Atom/RPI.Public/Scene.h>
  13. #include <Atom/RPI.Reflect/Material/MaterialFunctor.h>
  14. #include <Atom/RHI/DrawPacketBuilder.h>
  15. #include <Atom/RHI/RHISystemInterface.h>
  16. #include <AzCore/Console/Console.h>
  17. #include <Atom/RPI.Public/Shader/ShaderReloadDebugTracker.h>
  18. namespace AZ
  19. {
  20. namespace RPI
  21. {
  22. AZ_CVAR(bool,
  23. r_forceRootShaderVariantUsage,
  24. false,
  25. [](const bool&) { AZ::Interface<AZ::IConsole>::Get()->PerformCommand("MeshFeatureProcessor.ForceRebuildDrawPackets"); },
  26. ConsoleFunctorFlags::Null,
  27. "(For Testing) Forces usage of root shader variant in the mesh draw packet level, ignoring any other shader variants that may exist."
  28. );
  29. MeshDrawPacket::MeshDrawPacket(
  30. ModelLod& modelLod,
  31. size_t modelLodMeshIndex,
  32. Data::Instance<Material> materialOverride,
  33. Data::Instance<ShaderResourceGroup> objectSrg,
  34. const MaterialModelUvOverrideMap& materialModelUvMap
  35. )
  36. : m_modelLod(&modelLod)
  37. , m_modelLodMeshIndex(modelLodMeshIndex)
  38. , m_objectSrg(objectSrg)
  39. , m_material(materialOverride)
  40. , m_materialModelUvMap(materialModelUvMap)
  41. {
  42. if (!m_material)
  43. {
  44. m_material = GetMesh().m_material;
  45. }
  46. // set to all true so no items would be skipped
  47. m_drawListFilter.set();
  48. }
  49. Data::Instance<Material> MeshDrawPacket::GetMaterial() const
  50. {
  51. return m_material;
  52. }
  53. const ModelLod::Mesh& MeshDrawPacket::GetMesh() const
  54. {
  55. AZ_Assert(m_modelLodMeshIndex < m_modelLod->GetMeshes().size(), "m_modelLodMeshIndex %zu is out of range %zu", m_modelLodMeshIndex, m_modelLod->GetMeshes().size());
  56. return m_modelLod->GetMeshes()[m_modelLodMeshIndex];
  57. }
  58. void MeshDrawPacket::ForValidShaderOptionName(const Name& shaderOptionName, const AZStd::function<bool(const ShaderCollection::Item&, ShaderOptionIndex)>& callback)
  59. {
  60. m_material->ForAllShaderItems(
  61. [&](const Name&, const ShaderCollection::Item& shaderItem)
  62. {
  63. const ShaderOptionGroupLayout* layout = shaderItem.GetShaderOptions()->GetShaderOptionLayout();
  64. ShaderOptionIndex index = layout->FindShaderOptionIndex(shaderOptionName);
  65. if (index.IsValid())
  66. {
  67. bool shouldContinue = callback(shaderItem, index);
  68. if (!shouldContinue)
  69. {
  70. return false;
  71. }
  72. }
  73. return true;
  74. });
  75. }
  76. void MeshDrawPacket::SetStencilRef(uint8_t stencilRef)
  77. {
  78. if (m_stencilRef != stencilRef)
  79. {
  80. m_needUpdate = true;
  81. m_stencilRef = stencilRef;
  82. }
  83. }
  84. void MeshDrawPacket::SetSortKey(RHI::DrawItemSortKey sortKey)
  85. {
  86. if (m_sortKey != sortKey)
  87. {
  88. m_needUpdate = true;
  89. m_sortKey = sortKey;
  90. }
  91. }
  92. bool MeshDrawPacket::SetShaderOption(const Name& shaderOptionName, ShaderOptionValue value)
  93. {
  94. // check if the material owns this option in any of its shaders, if so it can't be set externally
  95. if (m_material->MaterialOwnsShaderOption(shaderOptionName))
  96. {
  97. return false;
  98. }
  99. // Try to find an existing option entry in the list
  100. for (ShaderOptionPair& shaderOptionPair : m_shaderOptions)
  101. {
  102. if (shaderOptionPair.first == shaderOptionName)
  103. {
  104. shaderOptionPair.second = value;
  105. m_needUpdate = true;
  106. return true;
  107. }
  108. }
  109. // Shader option isn't on the list, look to see if it's even valid for at least one shader item, and if so, add it.
  110. ForValidShaderOptionName(shaderOptionName,
  111. [&]([[maybe_unused]] const ShaderCollection::Item& shaderItem, [[maybe_unused]] ShaderOptionIndex index)
  112. {
  113. // Store the option name and value, they will be used in DoUpdate() to select the appropriate shader variant
  114. m_shaderOptions.push_back({ shaderOptionName, value });
  115. return false; // stop checking other shader items.
  116. }
  117. );
  118. m_needUpdate = true;
  119. return true;
  120. }
  121. bool MeshDrawPacket::UnsetShaderOption(const Name& shaderOptionName)
  122. {
  123. // try to find an existing option entry in the list, then remove it by swapping it with the back.
  124. for (ShaderOptionPair& shaderOptionPair : m_shaderOptions)
  125. {
  126. if (shaderOptionPair.first == shaderOptionName)
  127. {
  128. shaderOptionPair = m_shaderOptions.back();
  129. m_shaderOptions.pop_back();
  130. m_needUpdate = true;
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. void MeshDrawPacket::ClearShaderOptions()
  137. {
  138. m_needUpdate = m_shaderOptions.size() > 0;
  139. m_shaderOptions.clear();
  140. }
  141. void MeshDrawPacket::SetEnableDraw(RHI::DrawListTag drawListTag, bool enableDraw)
  142. {
  143. if (drawListTag.IsNull())
  144. {
  145. return;
  146. }
  147. uint8_t index = drawListTag.GetIndex();
  148. if (m_drawListFilter[index] != enableDraw)
  149. {
  150. m_needUpdate = true;
  151. m_drawListFilter[index] = enableDraw;
  152. }
  153. }
  154. RHI::DrawListMask MeshDrawPacket::GetDrawListFilter()
  155. {
  156. return m_drawListFilter;
  157. }
  158. void MeshDrawPacket::ClearDrawListFilter()
  159. {
  160. m_drawListFilter.set();
  161. m_needUpdate = true;
  162. }
  163. bool MeshDrawPacket::Update(const Scene& parentScene, bool forceUpdate /*= false*/)
  164. {
  165. // Setup the Shader variant handler when update this MeshDrawPacket the first time .
  166. // This is because the MeshDrawPacket data can be copied or moved right after it's created.
  167. // The m_shaderVariantHandler won't be copied correctly due to the capture of 'this' pointer.
  168. // Instead of override all the copy and move operators, this might be a better solution.
  169. if (!m_shaderVariantHandler.IsConnected())
  170. {
  171. m_shaderVariantHandler = Material::OnMaterialShaderVariantReadyEvent::Handler(
  172. [this]()
  173. {
  174. this->m_needUpdate = true;
  175. });
  176. m_material->ConnectEvent(m_shaderVariantHandler);
  177. }
  178. // Why we need to check "!m_material->NeedsCompile()"...
  179. // Frame A:
  180. // - Material::SetPropertyValue("foo",...). This bumps the material's CurrentChangeId()
  181. // - Material::Compile() updates all the material's outputs (SRG data, shader selection, shader options, etc).
  182. // - Material::SetPropertyValue("bar",...). This bumps the materials' CurrentChangeId() again.
  183. // - We do not process Material::Compile() a second time because you can only call SRG::Compile() once per frame. Material::Compile()
  184. // will be processed on the next frame. (See implementation of Material::Compile())
  185. // - MeshDrawPacket::Update() is called. It runs DoUpdate() to rebuild the draw packet, but everything is still in the state when "foo" was
  186. // set. The "bar" changes haven't been applied yet. It also sets m_materialChangeId to GetCurrentChangeId(), which corresponds to "bar" not "foo".
  187. // Frame B:
  188. // - Something calls Material::Compile(). This finally updates the material's outputs with the latest data corresponding to "bar".
  189. // - MeshDrawPacket::Update() is called. But since the GetCurrentChangeId() hasn't changed since last time, DoUpdate() is not called.
  190. // - The mesh continues rendering with only the "foo" change applied, indefinitely.
  191. if (forceUpdate || (!m_material->NeedsCompile() && m_materialChangeId != m_material->GetCurrentChangeId())
  192. || m_needUpdate)
  193. {
  194. DoUpdate(parentScene);
  195. m_materialChangeId = m_material->GetCurrentChangeId();
  196. m_needUpdate = false;
  197. DebugOutputShaderVariants();
  198. return true;
  199. }
  200. return false;
  201. }
  202. static bool HasRootConstants(const RHI::ConstantsLayout* rootConstantsLayout)
  203. {
  204. return rootConstantsLayout && rootConstantsLayout->GetDataSize() > 0;
  205. }
  206. void MeshDrawPacket::DebugOutputShaderVariants()
  207. {
  208. #ifdef DEBUG_MESH_SHADERVARIANTS
  209. uint32_t index = 0;
  210. AZ::Data::AssetInfo assetInfo;
  211. AZ::Data::AssetCatalogRequestBus::BroadcastResult(assetInfo, &AZ::Data::AssetCatalogRequestBus::Events::GetAssetInfoById, m_modelLod->GetAssetId());
  212. AZ_TracePrintf("MeshDrawPacket", "Mesh: %s", assetInfo.m_relativePath.data());
  213. for (const auto& variant : m_shaderVariantNames)
  214. {
  215. AZ_TracePrintf("MeshDrawPacket", "%d: %s", index++, variant.data());
  216. }
  217. #endif
  218. }
  219. bool MeshDrawPacket::DoUpdate(const Scene& parentScene)
  220. {
  221. auto meshes = m_modelLod->GetMeshes();
  222. ModelLod::Mesh& mesh = meshes[m_modelLodMeshIndex];
  223. if (!m_material)
  224. {
  225. AZ_Warning("MeshDrawPacket", false, "No material provided for mesh. Skipping.");
  226. return false;
  227. }
  228. ShaderReloadDebugTracker::ScopedSection reloadSection("MeshDrawPacket::DoUpdate");
  229. RHI::DrawPacketBuilder drawPacketBuilder{RHI::MultiDevice::AllDevices};
  230. drawPacketBuilder.Begin(nullptr);
  231. drawPacketBuilder.SetGeometryView(&mesh);
  232. drawPacketBuilder.AddShaderResourceGroup(m_objectSrg->GetRHIShaderResourceGroup());
  233. drawPacketBuilder.AddShaderResourceGroup(m_material->GetRHIShaderResourceGroup());
  234. // We build the list of used shaders in a local list rather than m_activeShaders so that
  235. // if DoUpdate() fails it won't modify any member data.
  236. MeshDrawPacket::ShaderList shaderList;
  237. shaderList.reserve(m_activeShaders.size());
  238. // The root constants are shared by all draw items in the draw packet. We must populate them with default values.
  239. // The draw packet builder needs to know where the data is coming from during appendShader, but it's not actually read
  240. // until drawPacketBuilder.End(), so store the default data out here.
  241. AZStd::vector<uint8_t> rootConstants;
  242. bool isFirstShaderItem = true;
  243. m_perDrawSrgs.clear();
  244. #ifdef DEBUG_MESH_SHADERVARIANTS
  245. m_shaderVariantNames.clear();
  246. #endif
  247. auto appendShader = [&](const ShaderCollection::Item& shaderItem, const Name& materialPipelineName)
  248. {
  249. // Skip the shader item without creating the shader instance
  250. // if the mesh is not going to be rendered based on the draw tag
  251. RHI::RHISystemInterface* rhiSystem = RHI::RHISystemInterface::Get();
  252. RHI::DrawListTagRegistry* drawListTagRegistry = rhiSystem->GetDrawListTagRegistry();
  253. // Use the explicit draw list override if exists.
  254. RHI::DrawListTag drawListTag = shaderItem.GetDrawListTagOverride();
  255. if (drawListTag.IsNull())
  256. {
  257. Data::Asset<RPI::ShaderAsset> shaderAsset = shaderItem.GetShaderAsset();
  258. if (!shaderAsset.IsReady())
  259. {
  260. // The shader asset needs to be loaded before we can check the draw tag.
  261. // If it's not loaded yet, the instance database will do a blocking load
  262. // when we create the instance below, so might as well load it now.
  263. shaderAsset.QueueLoad();
  264. if (shaderAsset.IsLoading())
  265. {
  266. shaderAsset.BlockUntilLoadComplete();
  267. }
  268. }
  269. drawListTag = drawListTagRegistry->FindTag(shaderAsset->GetDrawListName());
  270. }
  271. // draw list tag is filtered out. skip this item
  272. if (drawListTag.IsNull() || !m_drawListFilter[drawListTag.GetIndex()])
  273. {
  274. return false;
  275. }
  276. if (!parentScene.HasOutputForPipelineState(drawListTag))
  277. {
  278. // drawListTag not found in this scene, so don't render this item
  279. return false;
  280. }
  281. Data::Instance<Shader> shader = RPI::Shader::FindOrCreate(shaderItem.GetShaderAsset());
  282. if (!shader)
  283. {
  284. AZ_Error("MeshDrawPacket", false, "Shader '%s'. Failed to find or create instance", shaderItem.GetShaderAsset()->GetName().GetCStr());
  285. return false;
  286. }
  287. RPI::ShaderOptionGroup shaderOptions = *shaderItem.GetShaderOptions();
  288. // Set all unspecified shader options to default values, so that we get the most specialized variant possible.
  289. // (because FindVariantStableId treats unspecified options as a request specifically for a variant that doesn't specify those options)
  290. // [GFX TODO][ATOM-3883] We should consider updating the FindVariantStableId algorithm to handle default values for us, and remove this step here.
  291. // This might not be necessary anymore though, since ShaderAsset::GetDefaultShaderOptions() does this when the material type builder is creating the ShaderCollection.
  292. shaderOptions.SetUnspecifiedToDefaultValues();
  293. // [GFX_TODO][ATOM-14476]: according to this usage, we should make the shader input contract uniform across all shader variants.
  294. m_modelLod->CheckOptionalStreams(
  295. shaderOptions,
  296. shader->GetInputContract(),
  297. m_modelLodMeshIndex,
  298. m_materialModelUvMap,
  299. m_material->GetAsset()->GetMaterialTypeAsset()->GetUvNameMap());
  300. // apply shader options from this draw packet to the ShaderItem
  301. for (auto& meshShaderOption : m_shaderOptions)
  302. {
  303. Name& name = meshShaderOption.first;
  304. RPI::ShaderOptionValue& value = meshShaderOption.second;
  305. ShaderOptionIndex index = shaderOptions.FindShaderOptionIndex(name);
  306. // Shader options will be applied to any shader item that supports it, even if
  307. // not all the shader items in the draw packet support it
  308. if (index.IsValid())
  309. {
  310. shaderOptions.SetValue(name, value);
  311. }
  312. }
  313. const ShaderVariantId requestedVariantId = shaderOptions.GetShaderVariantId();
  314. const ShaderVariant& variant = r_forceRootShaderVariantUsage ? shader->GetRootVariant() : shader->GetVariant(requestedVariantId);
  315. #ifdef DEBUG_MESH_SHADERVARIANTS
  316. m_shaderVariantNames.push_back(variant.GetShaderVariantAsset().GetHint());
  317. #endif
  318. RHI::PipelineStateDescriptorForDraw pipelineStateDescriptor;
  319. variant.ConfigurePipelineState(pipelineStateDescriptor, shaderOptions);
  320. // Render states need to merge the runtime variation.
  321. // This allows materials to customize the render states that the shader uses.
  322. const RHI::RenderStates& renderStatesOverlay = *shaderItem.GetRenderStatesOverlay();
  323. RHI::MergeStateInto(renderStatesOverlay, pipelineStateDescriptor.m_renderStates);
  324. UvStreamTangentBitmask uvStreamTangentBitmask;
  325. RHI::StreamBufferIndices streamIndices;
  326. if (!m_modelLod->GetStreamsForMesh(
  327. pipelineStateDescriptor.m_inputStreamLayout,
  328. streamIndices,
  329. &uvStreamTangentBitmask,
  330. shader->GetInputContract(),
  331. m_modelLodMeshIndex,
  332. m_materialModelUvMap,
  333. m_material->GetAsset()->GetMaterialTypeAsset()->GetUvNameMap()))
  334. {
  335. return false;
  336. }
  337. Data::Instance<ShaderResourceGroup> drawSrg = shader->CreateDrawSrgForShaderVariant(shaderOptions, false);
  338. if (drawSrg)
  339. {
  340. // Pass UvStreamTangentBitmask to the shader if the draw SRG has it.
  341. AZ::Name shaderUvStreamTangentBitmask = AZ::Name(UvStreamTangentBitmask::SrgName);
  342. auto index = drawSrg->FindShaderInputConstantIndex(shaderUvStreamTangentBitmask);
  343. if (index.IsValid())
  344. {
  345. drawSrg->SetConstant(index, uvStreamTangentBitmask.GetFullTangentBitmask());
  346. }
  347. drawSrg->Compile();
  348. }
  349. parentScene.ConfigurePipelineState(drawListTag, pipelineStateDescriptor);
  350. const RHI::PipelineState* pipelineState = shader->AcquirePipelineState(pipelineStateDescriptor);
  351. if (!pipelineState)
  352. {
  353. AZ_Error("MeshDrawPacket", false, "Shader '%s'. Failed to acquire default pipeline state", shaderItem.GetShaderAsset()->GetName().GetCStr());
  354. return false;
  355. }
  356. const RHI::ConstantsLayout* rootConstantsLayout =
  357. pipelineStateDescriptor.m_pipelineLayoutDescriptor->GetRootConstantsLayout();
  358. if(isFirstShaderItem)
  359. {
  360. if (HasRootConstants(rootConstantsLayout))
  361. {
  362. m_rootConstantsLayout = rootConstantsLayout;
  363. rootConstants.resize(m_rootConstantsLayout->GetDataSize());
  364. drawPacketBuilder.SetRootConstants(rootConstants);
  365. }
  366. isFirstShaderItem = false;
  367. }
  368. else
  369. {
  370. AZ_Error(
  371. "MeshDrawPacket",
  372. (!m_rootConstantsLayout && !HasRootConstants(rootConstantsLayout)) ||
  373. (m_rootConstantsLayout && rootConstantsLayout && m_rootConstantsLayout->GetHash() == rootConstantsLayout->GetHash()),
  374. "Shader %s has mis-matched root constant layout in material %s. "
  375. "All draw items in a draw packet need to share the same root constants layout. This means that each pass "
  376. "(e.g. Depth, Shadows, Forward, MotionVectors) for a given materialtype should use the same layout.",
  377. shaderItem.GetShaderAsset()->GetName().GetCStr(),
  378. m_material->GetAsset().ToString<AZStd::string>().c_str());
  379. }
  380. RHI::DrawPacketBuilder::DrawRequest drawRequest;
  381. drawRequest.m_listTag = drawListTag;
  382. drawRequest.m_pipelineState = pipelineState;
  383. drawRequest.m_streamIndices = streamIndices;
  384. drawRequest.m_stencilRef = m_stencilRef;
  385. drawRequest.m_sortKey = m_sortKey;
  386. if (drawSrg)
  387. {
  388. drawRequest.m_uniqueShaderResourceGroup = drawSrg->GetRHIShaderResourceGroup();
  389. // Hold on to a reference to the drawSrg so the refcount doesn't drop to zero
  390. m_perDrawSrgs.push_back(drawSrg);
  391. }
  392. if (materialPipelineName != MaterialPipelineNone)
  393. {
  394. RHI::DrawFilterTag pipelineTag = parentScene.GetDrawFilterTagRegistry()->AcquireTag(materialPipelineName);
  395. AZ_Assert(pipelineTag.IsValid(), "Could not acquire pipeline filter tag '%s'.", materialPipelineName.GetCStr());
  396. drawRequest.m_drawFilterMask = 1 << pipelineTag.GetIndex();
  397. }
  398. drawPacketBuilder.AddDrawItem(drawRequest);
  399. ShaderData shaderData;
  400. shaderData.m_shader = AZStd::move(shader);
  401. shaderData.m_materialPipelineName = materialPipelineName;
  402. shaderData.m_shaderTag = shaderItem.GetShaderTag();
  403. shaderData.m_requestedShaderVariantId = requestedVariantId;
  404. shaderData.m_activeShaderVariantId = variant.GetShaderVariantId();
  405. shaderData.m_activeShaderVariantStableId = variant.GetStableId();
  406. shaderList.emplace_back(AZStd::move(shaderData));
  407. return true;
  408. };
  409. m_material->ApplyGlobalShaderOptions();
  410. // TODO(MaterialPipeline): We might want to detect duplicate ShaderItem objects here, and merge them to avoid redundant RHI DrawItems.
  411. m_material->ForAllShaderItems(
  412. [&](const Name& materialPipelineName, const ShaderCollection::Item& shaderItem)
  413. {
  414. if (shaderItem.IsEnabled())
  415. {
  416. if (shaderList.size() == RHI::DrawPacketBuilder::DrawItemCountMax)
  417. {
  418. AZ_Error("MeshDrawPacket", false, "Material has more than the limit of %d active shader items.", RHI::DrawPacketBuilder::DrawItemCountMax);
  419. return false;
  420. }
  421. appendShader(shaderItem, materialPipelineName);
  422. }
  423. return true;
  424. });
  425. m_drawPacket = drawPacketBuilder.End();
  426. if (m_drawPacket)
  427. {
  428. m_activeShaders = shaderList;
  429. m_materialSrg = m_material->GetRHIShaderResourceGroup();
  430. return true;
  431. }
  432. else
  433. {
  434. return false;
  435. }
  436. }
  437. const RHI::ConstPtr<RHI::ConstantsLayout> MeshDrawPacket::GetRootConstantsLayout() const
  438. {
  439. return m_rootConstantsLayout;
  440. }
  441. } // namespace RPI
  442. } // namespace AZ