3
0

MeshDrawPacket.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. // Why we need to check "!m_material->NeedsCompile()"...
  166. // Frame A:
  167. // - Material::SetPropertyValue("foo",...). This bumps the material's CurrentChangeId()
  168. // - Material::Compile() updates all the material's outputs (SRG data, shader selection, shader options, etc).
  169. // - Material::SetPropertyValue("bar",...). This bumps the materials' CurrentChangeId() again.
  170. // - We do not process Material::Compile() a second time because you can only call SRG::Compile() once per frame. Material::Compile()
  171. // will be processed on the next frame. (See implementation of Material::Compile())
  172. // - MeshDrawPacket::Update() is called. It runs DoUpdate() to rebuild the draw packet, but everything is still in the state when "foo" was
  173. // set. The "bar" changes haven't been applied yet. It also sets m_materialChangeId to GetCurrentChangeId(), which corresponds to "bar" not "foo".
  174. // Frame B:
  175. // - Something calls Material::Compile(). This finally updates the material's outputs with the latest data corresponding to "bar".
  176. // - MeshDrawPacket::Update() is called. But since the GetCurrentChangeId() hasn't changed since last time, DoUpdate() is not called.
  177. // - The mesh continues rendering with only the "foo" change applied, indefinitely.
  178. if (forceUpdate || (!m_material->NeedsCompile() && m_materialChangeId != m_material->GetCurrentChangeId())
  179. || m_needUpdate)
  180. {
  181. DoUpdate(parentScene);
  182. m_materialChangeId = m_material->GetCurrentChangeId();
  183. m_needUpdate = false;
  184. return true;
  185. }
  186. return false;
  187. }
  188. static bool HasRootConstants(const RHI::ConstantsLayout* rootConstantsLayout)
  189. {
  190. return rootConstantsLayout && rootConstantsLayout->GetDataSize() > 0;
  191. }
  192. bool MeshDrawPacket::DoUpdate(const Scene& parentScene)
  193. {
  194. const auto meshes = m_modelLod->GetMeshes();
  195. const ModelLod::Mesh& mesh = meshes[m_modelLodMeshIndex];
  196. if (!m_material)
  197. {
  198. AZ_Warning("MeshDrawPacket", false, "No material provided for mesh. Skipping.");
  199. return false;
  200. }
  201. ShaderReloadDebugTracker::ScopedSection reloadSection("MeshDrawPacket::DoUpdate");
  202. RHI::DrawPacketBuilder drawPacketBuilder;
  203. drawPacketBuilder.Begin(nullptr);
  204. drawPacketBuilder.SetDrawArguments(mesh.m_drawArguments);
  205. drawPacketBuilder.SetIndexBufferView(mesh.m_indexBufferView);
  206. drawPacketBuilder.AddShaderResourceGroup(m_objectSrg->GetRHIShaderResourceGroup());
  207. drawPacketBuilder.AddShaderResourceGroup(m_material->GetRHIShaderResourceGroup());
  208. // We build the list of used shaders in a local list rather than m_activeShaders so that
  209. // if DoUpdate() fails it won't modify any member data.
  210. MeshDrawPacket::ShaderList shaderList;
  211. shaderList.reserve(m_activeShaders.size());
  212. // We have to keep a list of these outside the loops that collect all the shaders because the DrawPacketBuilder
  213. // keeps pointers to StreamBufferViews until DrawPacketBuilder::End() is called. And we use a fixed_vector to guarantee
  214. // that the memory won't be relocated when new entries are added.
  215. AZStd::fixed_vector<ModelLod::StreamBufferViewList, RHI::DrawPacketBuilder::DrawItemCountMax> streamBufferViewsPerShader;
  216. // The root constants are shared by all draw items in the draw packet. We must populate them with default values.
  217. // The draw packet builder needs to know where the data is coming from during appendShader, but it's not actually read
  218. // until drawPacketBuilder.End(), so store the default data out here.
  219. AZStd::vector<uint8_t> rootConstants;
  220. bool isFirstShaderItem = true;
  221. m_perDrawSrgs.clear();
  222. auto appendShader = [&](const ShaderCollection::Item& shaderItem, const Name& materialPipelineName)
  223. {
  224. // Skip the shader item without creating the shader instance
  225. // if the mesh is not going to be rendered based on the draw tag
  226. RHI::RHISystemInterface* rhiSystem = RHI::RHISystemInterface::Get();
  227. RHI::DrawListTagRegistry* drawListTagRegistry = rhiSystem->GetDrawListTagRegistry();
  228. // Use the explicit draw list override if exists.
  229. RHI::DrawListTag drawListTag = shaderItem.GetDrawListTagOverride();
  230. if (drawListTag.IsNull())
  231. {
  232. Data::Asset<RPI::ShaderAsset> shaderAsset = shaderItem.GetShaderAsset();
  233. if (!shaderAsset.IsReady())
  234. {
  235. // The shader asset needs to be loaded before we can check the draw tag.
  236. // If it's not loaded yet, the instance database will do a blocking load
  237. // when we create the instance below, so might as well load it now.
  238. shaderAsset.QueueLoad();
  239. if (shaderAsset.IsLoading())
  240. {
  241. shaderAsset.BlockUntilLoadComplete();
  242. }
  243. }
  244. drawListTag = drawListTagRegistry->FindTag(shaderAsset->GetDrawListName());
  245. }
  246. // draw list tag is filtered out. skip this item
  247. if (drawListTag.IsNull() || !m_drawListFilter[drawListTag.GetIndex()])
  248. {
  249. return false;
  250. }
  251. if (!parentScene.HasOutputForPipelineState(drawListTag))
  252. {
  253. // drawListTag not found in this scene, so don't render this item
  254. return false;
  255. }
  256. Data::Instance<Shader> shader = RPI::Shader::FindOrCreate(shaderItem.GetShaderAsset());
  257. if (!shader)
  258. {
  259. AZ_Error("MeshDrawPacket", false, "Shader '%s'. Failed to find or create instance", shaderItem.GetShaderAsset()->GetName().GetCStr());
  260. return false;
  261. }
  262. RPI::ShaderOptionGroup shaderOptions = *shaderItem.GetShaderOptions();
  263. // Set all unspecified shader options to default values, so that we get the most specialized variant possible.
  264. // (because FindVariantStableId treats unspecified options as a request specifically for a variant that doesn't specify those options)
  265. // [GFX TODO][ATOM-3883] We should consider updating the FindVariantStableId algorithm to handle default values for us, and remove this step here.
  266. // This might not be necessary anymore though, since ShaderAsset::GetDefaultShaderOptions() does this when the material type builder is creating the ShaderCollection.
  267. shaderOptions.SetUnspecifiedToDefaultValues();
  268. // [GFX_TODO][ATOM-14476]: according to this usage, we should make the shader input contract uniform across all shader variants.
  269. m_modelLod->CheckOptionalStreams(
  270. shaderOptions,
  271. shader->GetInputContract(),
  272. m_modelLodMeshIndex,
  273. m_materialModelUvMap,
  274. m_material->GetAsset()->GetMaterialTypeAsset()->GetUvNameMap());
  275. // apply shader options from this draw packet to the ShaderItem
  276. for (auto& meshShaderOption : m_shaderOptions)
  277. {
  278. Name& name = meshShaderOption.first;
  279. RPI::ShaderOptionValue& value = meshShaderOption.second;
  280. ShaderOptionIndex index = shaderOptions.FindShaderOptionIndex(name);
  281. // Shader options will be applied to any shader item that supports it, even if
  282. // not all the shader items in the draw packet support it
  283. if (index.IsValid())
  284. {
  285. shaderOptions.SetValue(name, value);
  286. }
  287. }
  288. const ShaderVariantId requestedVariantId = shaderOptions.GetShaderVariantId();
  289. const ShaderVariant& variant = r_forceRootShaderVariantUsage ? shader->GetRootVariant() : shader->GetVariant(requestedVariantId);
  290. RHI::PipelineStateDescriptorForDraw pipelineStateDescriptor;
  291. variant.ConfigurePipelineState(pipelineStateDescriptor);
  292. // Render states need to merge the runtime variation.
  293. // This allows materials to customize the render states that the shader uses.
  294. const RHI::RenderStates& renderStatesOverlay = *shaderItem.GetRenderStatesOverlay();
  295. RHI::MergeStateInto(renderStatesOverlay, pipelineStateDescriptor.m_renderStates);
  296. auto& streamBufferViews = streamBufferViewsPerShader.emplace_back();
  297. UvStreamTangentBitmask uvStreamTangentBitmask;
  298. if (!m_modelLod->GetStreamsForMesh(
  299. pipelineStateDescriptor.m_inputStreamLayout,
  300. streamBufferViews,
  301. &uvStreamTangentBitmask,
  302. shader->GetInputContract(),
  303. m_modelLodMeshIndex,
  304. m_materialModelUvMap,
  305. m_material->GetAsset()->GetMaterialTypeAsset()->GetUvNameMap()))
  306. {
  307. return false;
  308. }
  309. Data::Instance<ShaderResourceGroup> drawSrg = shader->CreateDrawSrgForShaderVariant(shaderOptions, false);
  310. if (drawSrg)
  311. {
  312. // Pass UvStreamTangentBitmask to the shader if the draw SRG has it.
  313. AZ::Name shaderUvStreamTangentBitmask = AZ::Name(UvStreamTangentBitmask::SrgName);
  314. auto index = drawSrg->FindShaderInputConstantIndex(shaderUvStreamTangentBitmask);
  315. if (index.IsValid())
  316. {
  317. drawSrg->SetConstant(index, uvStreamTangentBitmask.GetFullTangentBitmask());
  318. }
  319. drawSrg->Compile();
  320. }
  321. parentScene.ConfigurePipelineState(drawListTag, pipelineStateDescriptor);
  322. const RHI::PipelineState* pipelineState = shader->AcquirePipelineState(pipelineStateDescriptor);
  323. if (!pipelineState)
  324. {
  325. AZ_Error("MeshDrawPacket", false, "Shader '%s'. Failed to acquire default pipeline state", shaderItem.GetShaderAsset()->GetName().GetCStr());
  326. return false;
  327. }
  328. const RHI::ConstantsLayout* rootConstantsLayout =
  329. pipelineStateDescriptor.m_pipelineLayoutDescriptor->GetRootConstantsLayout();
  330. if(isFirstShaderItem)
  331. {
  332. if (HasRootConstants(rootConstantsLayout))
  333. {
  334. m_rootConstantsLayout = rootConstantsLayout;
  335. rootConstants.resize(m_rootConstantsLayout->GetDataSize());
  336. drawPacketBuilder.SetRootConstants(rootConstants);
  337. }
  338. isFirstShaderItem = false;
  339. }
  340. else
  341. {
  342. AZ_Error(
  343. "MeshDrawPacket",
  344. (!m_rootConstantsLayout && !HasRootConstants(rootConstantsLayout)) ||
  345. (m_rootConstantsLayout && rootConstantsLayout && m_rootConstantsLayout->GetHash() == rootConstantsLayout->GetHash()),
  346. "Shader %s has mis-matched root constant layout in material %s. "
  347. "All draw items in a draw packet need to share the same root constants layout. This means that each pass "
  348. "(e.g. Depth, Shadows, Forward, MotionVectors) for a given materialtype should use the same layout.",
  349. shaderItem.GetShaderAsset()->GetName().GetCStr(),
  350. m_material->GetAsset().ToString<AZStd::string>().c_str());
  351. }
  352. RHI::DrawPacketBuilder::DrawRequest drawRequest;
  353. drawRequest.m_listTag = drawListTag;
  354. drawRequest.m_pipelineState = pipelineState;
  355. drawRequest.m_streamBufferViews = streamBufferViews;
  356. drawRequest.m_stencilRef = m_stencilRef;
  357. drawRequest.m_sortKey = m_sortKey;
  358. if (drawSrg)
  359. {
  360. drawRequest.m_uniqueShaderResourceGroup = drawSrg->GetRHIShaderResourceGroup();
  361. // Hold on to a reference to the drawSrg so the refcount doesn't drop to zero
  362. m_perDrawSrgs.push_back(drawSrg);
  363. }
  364. if (materialPipelineName != MaterialPipelineNone)
  365. {
  366. RHI::DrawFilterTag pipelineTag = parentScene.GetDrawFilterTagRegistry()->AcquireTag(materialPipelineName);
  367. AZ_Assert(pipelineTag.IsValid(), "Could not acquire pipeline filter tag '%s'.", materialPipelineName.GetCStr());
  368. drawRequest.m_drawFilterMask = 1 << pipelineTag.GetIndex();
  369. }
  370. drawPacketBuilder.AddDrawItem(drawRequest);
  371. ShaderData shaderData;
  372. shaderData.m_shader = AZStd::move(shader);
  373. shaderData.m_materialPipelineName = materialPipelineName;
  374. shaderData.m_shaderTag = shaderItem.GetShaderTag();
  375. shaderData.m_requestedShaderVariantId = requestedVariantId;
  376. shaderData.m_activeShaderVariantId = variant.GetShaderVariantId();
  377. shaderData.m_activeShaderVariantStableId = variant.GetStableId();
  378. shaderList.emplace_back(AZStd::move(shaderData));
  379. return true;
  380. };
  381. m_material->ApplyGlobalShaderOptions();
  382. // TODO(MaterialPipeline): We might want to detect duplicate ShaderItem objects here, and merge them to avoid redundant RHI DrawItems.
  383. m_material->ForAllShaderItems(
  384. [&](const Name& materialPipelineName, const ShaderCollection::Item& shaderItem)
  385. {
  386. if (shaderItem.IsEnabled())
  387. {
  388. if (shaderList.size() == RHI::DrawPacketBuilder::DrawItemCountMax)
  389. {
  390. AZ_Error("MeshDrawPacket", false, "Material has more than the limit of %d active shader items.", RHI::DrawPacketBuilder::DrawItemCountMax);
  391. return false;
  392. }
  393. appendShader(shaderItem, materialPipelineName);
  394. }
  395. return true;
  396. });
  397. m_drawPacket = drawPacketBuilder.End();
  398. if (m_drawPacket)
  399. {
  400. m_activeShaders = shaderList;
  401. m_materialSrg = m_material->GetRHIShaderResourceGroup();
  402. return true;
  403. }
  404. else
  405. {
  406. return false;
  407. }
  408. }
  409. const RHI::DrawPacket* MeshDrawPacket::GetRHIDrawPacket() const
  410. {
  411. return m_drawPacket.get();
  412. }
  413. const RHI::ConstPtr<RHI::ConstantsLayout> MeshDrawPacket::GetRootConstantsLayout() const
  414. {
  415. return m_rootConstantsLayout;
  416. }
  417. } // namespace RPI
  418. } // namespace AZ