ReflectionProbeFeatureProcessor.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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 <ReflectionProbe/ReflectionProbeFeatureProcessor.h>
  9. #include <AzCore/Serialization/SerializeContext.h>
  10. #include <Atom/RPI.Public/RPIUtils.h>
  11. #include <Atom/RPI.Public/Scene.h>
  12. #include <Atom/RPI.Public/View.h>
  13. #include <Atom/RPI.Reflect/Asset/AssetUtils.h>
  14. #include <Atom/Feature/Mesh/MeshFeatureProcessor.h>
  15. #include <Atom/RHI/Factory.h>
  16. #include <Atom/RHI/RHISystemInterface.h>
  17. #include <Atom/RHI/PipelineState.h>
  18. #include <Atom/RHI.Reflect/InputStreamLayoutBuilder.h>
  19. #include <Atom/Feature/RenderCommon.h>
  20. namespace AZ
  21. {
  22. namespace Render
  23. {
  24. void ReflectionProbeFeatureProcessor::Reflect(ReflectContext* context)
  25. {
  26. if (auto* serializeContext = azrtti_cast<SerializeContext*>(context))
  27. {
  28. serializeContext
  29. ->Class<ReflectionProbeFeatureProcessor, FeatureProcessor>()
  30. ->Version(1);
  31. }
  32. }
  33. void ReflectionProbeFeatureProcessor::Activate()
  34. {
  35. RHI::RHISystemInterface* rhiSystem = RHI::RHISystemInterface::Get();
  36. m_reflectionProbes.reserve(InitialProbeAllocationSize);
  37. RHI::BufferPoolDescriptor desc;
  38. desc.m_heapMemoryLevel = RHI::HeapMemoryLevel::Device;
  39. desc.m_bindFlags = RHI::BufferBindFlags::InputAssembly;
  40. m_bufferPool = RHI::Factory::Get().CreateBufferPool();
  41. m_bufferPool->SetName(Name("ReflectionProbeBoxBufferPool"));
  42. [[maybe_unused]] RHI::ResultCode resultCode = m_bufferPool->Init(*rhiSystem->GetDevice(), desc);
  43. AZ_Error("ReflectionProbeFeatureProcessor", resultCode == RHI::ResultCode::Success, "Failed to initialize buffer pool");
  44. // create box mesh vertices and indices
  45. CreateBoxMesh();
  46. // load shaders for stencil, blend weight, and render passes
  47. LoadShader("shaders/reflections/reflectionprobestencil.azshader",
  48. m_reflectionRenderData.m_stencilPipelineState,
  49. m_reflectionRenderData.m_stencilShader,
  50. m_reflectionRenderData.m_stencilSrgLayout,
  51. m_reflectionRenderData.m_stencilDrawListTag);
  52. LoadShader("shaders/reflections/reflectionprobeblendweight.azshader",
  53. m_reflectionRenderData.m_blendWeightPipelineState,
  54. m_reflectionRenderData.m_blendWeightShader,
  55. m_reflectionRenderData.m_blendWeightSrgLayout,
  56. m_reflectionRenderData.m_blendWeightDrawListTag);
  57. LoadShader("shaders/reflections/reflectionproberenderouter.azshader",
  58. m_reflectionRenderData.m_renderOuterPipelineState,
  59. m_reflectionRenderData.m_renderOuterShader,
  60. m_reflectionRenderData.m_renderOuterSrgLayout,
  61. m_reflectionRenderData.m_renderOuterDrawListTag);
  62. LoadShader("shaders/reflections/reflectionproberenderinner.azshader",
  63. m_reflectionRenderData.m_renderInnerPipelineState,
  64. m_reflectionRenderData.m_renderInnerShader,
  65. m_reflectionRenderData.m_renderInnerSrgLayout,
  66. m_reflectionRenderData.m_renderInnerDrawListTag);
  67. EnableSceneNotification();
  68. }
  69. void ReflectionProbeFeatureProcessor::Deactivate()
  70. {
  71. AZ_Warning("ReflectionProbeFeatureProcessor", m_reflectionProbes.size() == 0,
  72. "Deactivating the ReflectionProbeFeatureProcessor, but there are still outstanding reflection probes. Components\n"
  73. "using ReflectionProbeHandles should free them before the ReflectionProbeFeatureProcessor is deactivated.\n"
  74. );
  75. DisableSceneNotification();
  76. if (m_bufferPool)
  77. {
  78. m_bufferPool.reset();
  79. }
  80. Data::AssetBus::MultiHandler::BusDisconnect();
  81. }
  82. void ReflectionProbeFeatureProcessor::Simulate([[maybe_unused]] const FeatureProcessor::SimulatePacket& packet)
  83. {
  84. AZ_PROFILE_SCOPE(AzRender, "ReflectionProbeFeatureProcessor: Simulate");
  85. // update pipeline states
  86. if (m_needUpdatePipelineStates)
  87. {
  88. UpdatePipelineStates();
  89. m_needUpdatePipelineStates = false;
  90. }
  91. // check pending cubemaps and notify that the asset is ready
  92. for (auto& notificationEntry : m_notifyCubeMapAssets)
  93. {
  94. if (notificationEntry.m_assetId.IsValid())
  95. {
  96. // asset already has an assetId
  97. continue;
  98. }
  99. // query for the assetId
  100. AZ::Data::AssetId assetId;
  101. AZ::Data::AssetCatalogRequestBus::BroadcastResult(
  102. assetId,
  103. &AZ::Data::AssetCatalogRequests::GetAssetIdByPath,
  104. notificationEntry.m_relativePath.c_str(),
  105. azrtti_typeid<AZ::RPI::StreamingImageAsset>(),
  106. false);
  107. if (assetId.IsValid())
  108. {
  109. notificationEntry.m_assetId = assetId;
  110. notificationEntry.m_asset.Create(assetId, true);
  111. Data::AssetBus::MultiHandler::BusConnect(assetId);
  112. }
  113. }
  114. // if the volumes changed we need to re-sort the probe list
  115. if (m_probeSortRequired)
  116. {
  117. AZ_PROFILE_SCOPE(AzRender, "Sort reflection probes");
  118. // sort the probes by descending inner volume size, so the smallest volumes are rendered last
  119. auto sortFn = [](ReflectionProbePtr const& probe1, ReflectionProbePtr const& probe2) -> bool
  120. {
  121. const Obb& obb1 = probe1->GetInnerObbWs();
  122. const Obb& obb2 = probe2->GetInnerObbWs();
  123. float size1 = obb1.GetHalfLengthX() * obb1.GetHalfLengthZ() * obb1.GetHalfLengthY();
  124. float size2 = obb2.GetHalfLengthX() * obb2.GetHalfLengthZ() * obb2.GetHalfLengthY();
  125. return (size1 > size2);
  126. };
  127. AZStd::sort(m_reflectionProbes.begin(), m_reflectionProbes.end(), sortFn);
  128. m_probeSortRequired = false;
  129. m_meshFeatureProcessorUpdateRequired = true;
  130. }
  131. // call Simulate on all reflection probes
  132. for (uint32_t probeIndex = 0; probeIndex < m_reflectionProbes.size(); ++probeIndex)
  133. {
  134. ReflectionProbePtr& reflectionProbe = m_reflectionProbes[probeIndex];
  135. AZ_Assert(reflectionProbe.use_count() > 1, "ReflectionProbe found with no corresponding owner, ensure that RemoveProbe() is called before releasing probe handles");
  136. reflectionProbe->Simulate(probeIndex);
  137. }
  138. }
  139. void ReflectionProbeFeatureProcessor::OnRenderEnd()
  140. {
  141. // call OnRenderEnd on all reflection probes
  142. for (auto& reflectionProbe : m_reflectionProbes)
  143. {
  144. AZ_Assert(reflectionProbe.use_count() > 1, "ReflectionProbe found with no corresponding owner, ensure that RemoveProbe() is called before releasing probe handles");
  145. reflectionProbe->OnRenderEnd();
  146. }
  147. // notify the MeshFeatureProcessor if there were changes to the ReflectionProbes
  148. // Note: This is done in OnRenderEnd to avoid a race between the two feature processors in Simulate, and any changes
  149. // will be applied on the next frame by the MeshFeatureProcessor.
  150. if (m_meshFeatureProcessorUpdateRequired)
  151. {
  152. MeshFeatureProcessor* meshFeatureProcessor = GetParentScene()->GetFeatureProcessor<MeshFeatureProcessor>();
  153. meshFeatureProcessor->UpdateMeshReflectionProbes();
  154. m_meshFeatureProcessorUpdateRequired = false;
  155. }
  156. }
  157. ReflectionProbeHandle ReflectionProbeFeatureProcessor::AddReflectionProbe(const AZ::Transform& transform, bool useParallaxCorrection)
  158. {
  159. ReflectionProbePtr reflectionProbe = AZStd::make_shared<ReflectionProbe>();
  160. reflectionProbe->Init(GetParentScene(), &m_reflectionRenderData);
  161. reflectionProbe->SetTransform(transform);
  162. reflectionProbe->SetUseParallaxCorrection(useParallaxCorrection);
  163. m_reflectionProbes.push_back(reflectionProbe);
  164. m_reflectionProbeMap[reflectionProbe->GetUuid()] = reflectionProbe;
  165. m_probeSortRequired = true;
  166. return reflectionProbe->GetUuid();
  167. }
  168. void ReflectionProbeFeatureProcessor::RemoveReflectionProbe(ReflectionProbeHandle& handle)
  169. {
  170. if (!ValidateHandle(handle))
  171. {
  172. return;
  173. }
  174. ReflectionProbePtr reflectionProbe = m_reflectionProbeMap[handle];
  175. auto itEntry = AZStd::find_if(m_reflectionProbes.begin(), m_reflectionProbes.end(), [&](ReflectionProbePtr const& entry)
  176. {
  177. return (entry == reflectionProbe);
  178. });
  179. AZ_Assert(itEntry != m_reflectionProbes.end(), "RemoveProbe called with a probe that is not in the probe list");
  180. m_reflectionProbes.erase(itEntry);
  181. m_reflectionProbeMap.erase(handle);
  182. m_meshFeatureProcessorUpdateRequired = true;
  183. }
  184. void ReflectionProbeFeatureProcessor::SetOuterExtents(const ReflectionProbeHandle& handle, const Vector3& outerExtents)
  185. {
  186. if (!ValidateHandle(handle))
  187. {
  188. return;
  189. }
  190. m_reflectionProbeMap[handle]->SetOuterExtents(outerExtents);
  191. m_probeSortRequired = true;
  192. }
  193. AZ::Vector3 ReflectionProbeFeatureProcessor::GetOuterExtents(const ReflectionProbeHandle& handle) const
  194. {
  195. if (!ValidateHandle(handle))
  196. {
  197. return AZ::Vector3::CreateZero();
  198. }
  199. ReflectionProbeMap::const_iterator it = m_reflectionProbeMap.find(handle);
  200. return it->second->GetOuterExtents();
  201. }
  202. void ReflectionProbeFeatureProcessor::SetInnerExtents(const ReflectionProbeHandle& handle, const Vector3& innerExtents)
  203. {
  204. if (!ValidateHandle(handle))
  205. {
  206. return;
  207. }
  208. m_reflectionProbeMap[handle]->SetInnerExtents(innerExtents);
  209. m_probeSortRequired = true;
  210. }
  211. AZ::Vector3 ReflectionProbeFeatureProcessor::GetInnerExtents(const ReflectionProbeHandle& handle) const
  212. {
  213. if (!ValidateHandle(handle))
  214. {
  215. return AZ::Vector3::CreateZero();
  216. }
  217. ReflectionProbeMap::const_iterator it = m_reflectionProbeMap.find(handle);
  218. return it->second->GetInnerExtents();
  219. }
  220. AZ::Obb ReflectionProbeFeatureProcessor::GetOuterObbWs(const ReflectionProbeHandle& handle) const
  221. {
  222. if (!ValidateHandle(handle))
  223. {
  224. return AZ::Obb();
  225. }
  226. ReflectionProbeMap::const_iterator it = m_reflectionProbeMap.find(handle);
  227. return it->second->GetOuterObbWs();
  228. }
  229. AZ::Obb ReflectionProbeFeatureProcessor::GetInnerObbWs(const ReflectionProbeHandle& handle) const
  230. {
  231. if (!ValidateHandle(handle))
  232. {
  233. return AZ::Obb();
  234. }
  235. ReflectionProbeMap::const_iterator it = m_reflectionProbeMap.find(handle);
  236. return it->second->GetInnerObbWs();
  237. }
  238. void ReflectionProbeFeatureProcessor::SetTransform(const ReflectionProbeHandle& handle, const AZ::Transform& transform)
  239. {
  240. if (!ValidateHandle(handle))
  241. {
  242. return;
  243. }
  244. m_reflectionProbeMap[handle]->SetTransform(transform);
  245. m_probeSortRequired = true;
  246. }
  247. AZ::Transform ReflectionProbeFeatureProcessor::GetTransform(const ReflectionProbeHandle& handle) const
  248. {
  249. if (!ValidateHandle(handle))
  250. {
  251. return AZ::Transform::CreateIdentity();
  252. }
  253. ReflectionProbeMap::const_iterator it = m_reflectionProbeMap.find(handle);
  254. return it->second->GetTransform();
  255. }
  256. void ReflectionProbeFeatureProcessor::SetCubeMap(const ReflectionProbeHandle& handle, Data::Instance<RPI::Image>& cubeMapImage, const AZStd::string& relativePath)
  257. {
  258. if (!ValidateHandle(handle))
  259. {
  260. return;
  261. }
  262. m_reflectionProbeMap[handle]->SetCubeMapImage(cubeMapImage, relativePath);
  263. m_meshFeatureProcessorUpdateRequired = true;
  264. }
  265. Data::Instance<RPI::Image> ReflectionProbeFeatureProcessor::GetCubeMap(const ReflectionProbeHandle& handle) const
  266. {
  267. if (!ValidateHandle(handle))
  268. {
  269. return nullptr;
  270. }
  271. ReflectionProbeMap::const_iterator it = m_reflectionProbeMap.find(handle);
  272. return it->second->GetCubeMapImage();
  273. }
  274. void ReflectionProbeFeatureProcessor::SetRenderExposure(const ReflectionProbeHandle& handle, float renderExposure)
  275. {
  276. if (!ValidateHandle(handle))
  277. {
  278. return;
  279. }
  280. m_reflectionProbeMap[handle]->SetRenderExposure(renderExposure);
  281. }
  282. float ReflectionProbeFeatureProcessor::GetRenderExposure(const ReflectionProbeHandle& handle) const
  283. {
  284. if (!ValidateHandle(handle))
  285. {
  286. return 0.0f;
  287. }
  288. ReflectionProbeMap::const_iterator it = m_reflectionProbeMap.find(handle);
  289. return it->second->GetRenderExposure();
  290. }
  291. void ReflectionProbeFeatureProcessor::SetBakeExposure(const ReflectionProbeHandle& handle, float bakeExposure)
  292. {
  293. if (!ValidateHandle(handle))
  294. {
  295. return;
  296. }
  297. m_reflectionProbeMap[handle]->SetBakeExposure(bakeExposure);
  298. }
  299. float ReflectionProbeFeatureProcessor::GetBakeExposure(const ReflectionProbeHandle& handle) const
  300. {
  301. if (!ValidateHandle(handle))
  302. {
  303. return 0.0f;
  304. }
  305. ReflectionProbeMap::const_iterator it = m_reflectionProbeMap.find(handle);
  306. return it->second->GetBakeExposure();
  307. }
  308. bool ReflectionProbeFeatureProcessor::GetUseParallaxCorrection(const ReflectionProbeHandle& handle) const
  309. {
  310. if (!ValidateHandle(handle))
  311. {
  312. return false;
  313. }
  314. ReflectionProbeMap::const_iterator it = m_reflectionProbeMap.find(handle);
  315. return it->second->GetUseParallaxCorrection();
  316. }
  317. void ReflectionProbeFeatureProcessor::Bake(const ReflectionProbeHandle& handle, RenderCubeMapCallback callback, const AZStd::string& relativePath)
  318. {
  319. if (!ValidateHandle(handle))
  320. {
  321. return;
  322. }
  323. m_reflectionProbeMap[handle]->Bake(callback);
  324. // check to see if this is an existing asset
  325. AZ::Data::AssetId assetId;
  326. AZ::Data::AssetCatalogRequestBus::BroadcastResult(
  327. assetId,
  328. &AZ::Data::AssetCatalogRequests::GetAssetIdByPath,
  329. relativePath.c_str(),
  330. azrtti_typeid<AZ::RPI::StreamingImageAsset>(),
  331. false);
  332. // we only track notifications for new cubemap assets, existing assets are automatically reloaded by the RPI
  333. if (!assetId.IsValid())
  334. {
  335. m_notifyCubeMapAssets.push_back({ relativePath, assetId });
  336. }
  337. }
  338. bool ReflectionProbeFeatureProcessor::CheckCubeMapAssetNotification(const AZStd::string& relativePath, Data::Asset<RPI::StreamingImageAsset>& outCubeMapAsset, CubeMapAssetNotificationType& outNotificationType)
  339. {
  340. for (NotifyCubeMapAssetVector::iterator itNotification = m_notifyCubeMapAssets.begin(); itNotification != m_notifyCubeMapAssets.end(); ++itNotification)
  341. {
  342. if (itNotification->m_relativePath == relativePath)
  343. {
  344. outNotificationType = itNotification->m_notificationType;
  345. if (outNotificationType != CubeMapAssetNotificationType::None)
  346. {
  347. outCubeMapAsset = itNotification->m_asset;
  348. m_notifyCubeMapAssets.erase(itNotification);
  349. }
  350. return true;
  351. }
  352. }
  353. return false;
  354. }
  355. bool ReflectionProbeFeatureProcessor::IsCubeMapReferenced(const AZStd::string& relativePath)
  356. {
  357. for (auto& reflectionProbe : m_reflectionProbes)
  358. {
  359. if (reflectionProbe->GetCubeMapRelativePath() == relativePath)
  360. {
  361. return true;
  362. }
  363. }
  364. return false;
  365. }
  366. void ReflectionProbeFeatureProcessor::ShowVisualization(const ReflectionProbeHandle& handle, bool showVisualization)
  367. {
  368. if (!ValidateHandle(handle))
  369. {
  370. return;
  371. }
  372. m_reflectionProbeMap[handle]->ShowVisualization(showVisualization);
  373. }
  374. void ReflectionProbeFeatureProcessor::FindReflectionProbes(const AZ::Vector3& position, ReflectionProbeHandleVector& reflectionProbeHandles)
  375. {
  376. FindReflectionProbesInternal(Aabb::CreateCenterRadius(position, 0.5f), reflectionProbeHandles, [&](const ReflectionProbe* reflectionProbe) -> bool
  377. {
  378. return reflectionProbe->GetOuterObbWs().Contains(position);
  379. });
  380. }
  381. void ReflectionProbeFeatureProcessor::FindReflectionProbes(const AZ::Aabb& aabb, ReflectionProbeHandleVector& reflectionProbeHandles)
  382. {
  383. FindReflectionProbesInternal(aabb, reflectionProbeHandles, [&](const ReflectionProbe* reflectionProbe) -> bool
  384. {
  385. // [GFX TODO] Implement Obb-Aabb intersection test in ShapeIntersectionTests (AzCore)
  386. AZ::Aabb outerAabb = AZ::Aabb::CreateFromObb(reflectionProbe->GetOuterObbWs());
  387. return outerAabb.Overlaps(aabb);
  388. });
  389. }
  390. void ReflectionProbeFeatureProcessor::FindReflectionProbesInternal(const AZ::Aabb& aabb, ReflectionProbeHandleVector& reflectionProbeHandles, AZStd::function<bool(const ReflectionProbe*)> filter)
  391. {
  392. reflectionProbeHandles.clear();
  393. AZStd::vector<AzFramework::VisibilityEntry*> visibilityEntries;
  394. GetParentScene()->GetCullingScene()->GetVisibilityScene()->Enumerate(
  395. aabb,
  396. [&](const AzFramework::IVisibilityScene::NodeData& nodeData)
  397. {
  398. for (AzFramework::VisibilityEntry* entry : nodeData.m_entries)
  399. {
  400. if (entry->m_typeFlags & AzFramework::VisibilityEntry::TypeFlags::TYPE_RPI_Cullable)
  401. {
  402. if (RPI::Cullable* cullable = static_cast<RPI::Cullable*>(entry->m_userData);
  403. cullable && cullable->m_cullData.m_componentType == Culling::ComponentType::ReflectionProbe)
  404. {
  405. AZ::Uuid uuid = cullable->m_cullData.m_componentUuid;
  406. ReflectionProbePtr reflectionProbe = m_reflectionProbeMap[cullable->m_cullData.m_componentUuid];
  407. AZ_Assert(reflectionProbe, "Unable to find reflection probe by Uuid");
  408. if (reflectionProbe
  409. && reflectionProbe->GetCubeMapImage()
  410. && reflectionProbe->GetCubeMapImage()->IsInitialized())
  411. {
  412. if (!filter || filter(reflectionProbe.get()))
  413. {
  414. reflectionProbeHandles.push_back(uuid);
  415. }
  416. }
  417. }
  418. }
  419. }
  420. }
  421. );
  422. // sort the probes by descending inner volume size
  423. auto sortFn = [&](const ReflectionProbeHandle& handle1, const ReflectionProbeHandle& handle2) -> bool
  424. {
  425. const Obb& obb1 = m_reflectionProbeMap[handle1]->GetInnerObbWs();
  426. const Obb& obb2 = m_reflectionProbeMap[handle2]->GetInnerObbWs();
  427. float size1 = obb1.GetHalfLengthX() * obb1.GetHalfLengthZ() * obb1.GetHalfLengthY();
  428. float size2 = obb2.GetHalfLengthX() * obb2.GetHalfLengthZ() * obb2.GetHalfLengthY();
  429. return (size1 > size2);
  430. };
  431. AZStd::sort(reflectionProbeHandles.begin(), reflectionProbeHandles.end(), sortFn);
  432. }
  433. bool ReflectionProbeFeatureProcessor::ValidateHandle(const ReflectionProbeHandle& handle) const
  434. {
  435. if (handle.IsNull() || m_reflectionProbeMap.find(handle) == m_reflectionProbeMap.end())
  436. {
  437. AZ_Assert(false, "Invalid ReflectionProbeHandle passed to the ReflectionProbeFeatureProcessor");
  438. return false;
  439. }
  440. return true;
  441. }
  442. void ReflectionProbeFeatureProcessor::CreateBoxMesh()
  443. {
  444. // vertex positions
  445. static const Position positions[] =
  446. {
  447. // front
  448. { -0.5f, -0.5f, 0.5f },
  449. { 0.5f, -0.5f, 0.5f },
  450. { 0.5f, 0.5f, 0.5f },
  451. { -0.5f, 0.5f, 0.5f },
  452. // back
  453. { -0.5f, -0.5f, -0.5f },
  454. { 0.5f, -0.5f, -0.5f },
  455. { 0.5f, 0.5f, -0.5f },
  456. { -0.5f, 0.5f, -0.5f },
  457. // left
  458. { -0.5f, -0.5f, 0.5f },
  459. { -0.5f, 0.5f, 0.5f },
  460. { -0.5f, 0.5f, -0.5f },
  461. { -0.5f, -0.5f, -0.5f },
  462. // right
  463. { 0.5f, -0.5f, 0.5f },
  464. { 0.5f, 0.5f, 0.5f },
  465. { 0.5f, 0.5f, -0.5f },
  466. { 0.5f, -0.5f, -0.5f },
  467. // bottom
  468. { -0.5f, -0.5f, 0.5f },
  469. { 0.5f, -0.5f, 0.5f },
  470. { 0.5f, -0.5f, -0.5f },
  471. { -0.5f, -0.5f, -0.5f },
  472. // top
  473. { -0.5f, 0.5f, 0.5f },
  474. { 0.5f, 0.5f, 0.5f },
  475. { 0.5f, 0.5f, -0.5f },
  476. { -0.5f, 0.5f, -0.5f },
  477. };
  478. static const u32 numPositions = sizeof(positions) / sizeof(positions[0]);
  479. for (u32 i = 0; i < numPositions; ++i)
  480. {
  481. m_boxPositions.push_back(positions[i]);
  482. }
  483. // indices
  484. static const uint16_t indices[] =
  485. {
  486. // front
  487. 0, 1, 2, 2, 3, 0,
  488. // back
  489. 5, 4, 7, 7, 6, 5,
  490. // left
  491. 8, 9, 10, 10, 11, 8,
  492. // right
  493. 14, 13, 12, 12, 15, 14,
  494. // bottom
  495. 18, 17, 16, 16, 19, 18,
  496. // top
  497. 23, 20, 21, 21, 22, 23
  498. };
  499. static const u32 numIndices = sizeof(indices) / sizeof(indices[0]);
  500. for (u32 i = 0; i < numIndices; ++i)
  501. {
  502. m_boxIndices.push_back(indices[i]);
  503. }
  504. // create stream layout
  505. RHI::InputStreamLayoutBuilder layoutBuilder;
  506. layoutBuilder.AddBuffer()->Channel("POSITION", RHI::Format::R32G32B32_FLOAT);
  507. layoutBuilder.SetTopology(RHI::PrimitiveTopology::TriangleList);
  508. m_boxStreamLayout = layoutBuilder.End();
  509. // create index buffer
  510. AZ::RHI::BufferInitRequest request;
  511. m_boxIndexBuffer = AZ::RHI::Factory::Get().CreateBuffer();
  512. request.m_buffer = m_boxIndexBuffer.get();
  513. request.m_descriptor = AZ::RHI::BufferDescriptor{ AZ::RHI::BufferBindFlags::InputAssembly, m_boxIndices.size() * sizeof(uint16_t) };
  514. request.m_initialData = m_boxIndices.data();
  515. [[maybe_unused]] AZ::RHI::ResultCode result = m_bufferPool->InitBuffer(request);
  516. AZ_Error("ReflectionProbeFeatureProcessor", result == RHI::ResultCode::Success, "Failed to initialize box index buffer - error [%d]", result);
  517. // create index buffer view
  518. AZ::RHI::IndexBufferView indexBufferView =
  519. {
  520. *m_boxIndexBuffer,
  521. 0,
  522. sizeof(indices),
  523. AZ::RHI::IndexFormat::Uint16,
  524. };
  525. m_reflectionRenderData.m_boxIndexBufferView = indexBufferView;
  526. m_reflectionRenderData.m_boxIndexCount = numIndices;
  527. // create position buffer
  528. m_boxPositionBuffer = AZ::RHI::Factory::Get().CreateBuffer();
  529. request.m_buffer = m_boxPositionBuffer.get();
  530. request.m_descriptor = AZ::RHI::BufferDescriptor{ AZ::RHI::BufferBindFlags::InputAssembly, m_boxPositions.size() * sizeof(Position) };
  531. request.m_initialData = m_boxPositions.data();
  532. result = m_bufferPool->InitBuffer(request);
  533. AZ_Error("ReflectionProbeFeatureProcessor", result == RHI::ResultCode::Success, "Failed to initialize box index buffer - error [%d]", result);
  534. // create position buffer view
  535. RHI::StreamBufferView positionBufferView =
  536. {
  537. *m_boxPositionBuffer,
  538. 0,
  539. (uint32_t)(m_boxPositions.size() * sizeof(Position)),
  540. sizeof(Position),
  541. };
  542. m_reflectionRenderData.m_boxPositionBufferView = { { positionBufferView } };
  543. AZ::RHI::ValidateStreamBufferViews(m_boxStreamLayout, m_reflectionRenderData.m_boxPositionBufferView);
  544. }
  545. void ReflectionProbeFeatureProcessor::LoadShader(
  546. const char* filePath,
  547. RPI::Ptr<RPI::PipelineStateForDraw>& pipelineState,
  548. Data::Instance<RPI::Shader>& shader,
  549. RHI::Ptr<RHI::ShaderResourceGroupLayout>& srgLayout,
  550. RHI::DrawListTag& drawListTag)
  551. {
  552. // load shader
  553. shader = RPI::LoadCriticalShader(filePath);
  554. if (shader == nullptr)
  555. {
  556. AZ_Error("ReflectionProbeFeatureProcessor", false, "Failed to find asset for shader [%s]", filePath);
  557. return;
  558. }
  559. // store drawlist tag
  560. drawListTag = shader->GetDrawListTag();
  561. // create pipeline state
  562. pipelineState = aznew RPI::PipelineStateForDraw;
  563. pipelineState->Init(shader); // uses default shader variant
  564. pipelineState->SetInputStreamLayout(m_boxStreamLayout);
  565. pipelineState->SetOutputFromScene(GetParentScene());
  566. pipelineState->Finalize();
  567. // load object shader resource group
  568. srgLayout = shader->FindShaderResourceGroupLayout(RPI::SrgBindingSlot::Object);
  569. AZ_Error("ReflectionProbeFeatureProcessor", srgLayout != nullptr, "Failed to find ObjectSrg layout from shader [%s]", filePath);
  570. }
  571. void ReflectionProbeFeatureProcessor::OnRenderPipelineChanged(RPI::RenderPipeline* renderPipeline,
  572. RPI::SceneNotification::RenderPipelineChangeType changeType)
  573. {
  574. if (changeType == RPI::SceneNotification::RenderPipelineChangeType::PassChanged)
  575. {
  576. for (auto& reflectionProbe : m_reflectionProbes)
  577. {
  578. reflectionProbe->OnRenderPipelinePassesChanged(renderPipeline);
  579. }
  580. }
  581. m_needUpdatePipelineStates = true;
  582. }
  583. void ReflectionProbeFeatureProcessor::UpdatePipelineStates()
  584. {
  585. auto scene = GetParentScene();
  586. m_reflectionRenderData.m_stencilPipelineState->SetOutputFromScene(scene);
  587. m_reflectionRenderData.m_stencilPipelineState->Finalize();
  588. m_reflectionRenderData.m_blendWeightPipelineState->SetOutputFromScene(scene);
  589. m_reflectionRenderData.m_blendWeightPipelineState->Finalize();
  590. m_reflectionRenderData.m_renderOuterPipelineState->SetOutputFromScene(scene);
  591. m_reflectionRenderData.m_renderOuterPipelineState->Finalize();
  592. m_reflectionRenderData.m_renderInnerPipelineState->SetOutputFromScene(scene);
  593. m_reflectionRenderData.m_renderInnerPipelineState->Finalize();
  594. }
  595. void ReflectionProbeFeatureProcessor::HandleAssetNotification(Data::Asset<Data::AssetData> asset, CubeMapAssetNotificationType notificationType)
  596. {
  597. for (NotifyCubeMapAssetVector::iterator itNotification = m_notifyCubeMapAssets.begin(); itNotification != m_notifyCubeMapAssets.end(); ++itNotification)
  598. {
  599. if (itNotification->m_assetId == asset.GetId())
  600. {
  601. // store the cubemap asset
  602. itNotification->m_asset = Data::static_pointer_cast<RPI::StreamingImageAsset>(asset);
  603. itNotification->m_notificationType = notificationType;
  604. // stop notifications on this asset
  605. Data::AssetBus::MultiHandler::BusDisconnect(itNotification->m_assetId);
  606. break;
  607. }
  608. }
  609. }
  610. void ReflectionProbeFeatureProcessor::OnAssetReady(Data::Asset<Data::AssetData> asset)
  611. {
  612. HandleAssetNotification(asset, CubeMapAssetNotificationType::Ready);
  613. }
  614. void ReflectionProbeFeatureProcessor::OnAssetError(Data::Asset<Data::AssetData> asset)
  615. {
  616. AZ_Error("ReflectionProbeFeatureProcessor", false, "Failed to load cubemap [%s]", asset.GetHint().c_str());
  617. HandleAssetNotification(asset, CubeMapAssetNotificationType::Error);
  618. }
  619. } // namespace Render
  620. } // namespace AZ