ReflectionProbeFeatureProcessor.cpp 30 KB

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