XRExampleComponent.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 <RHI/XRExampleComponent.h>
  9. #include <Atom/RHI/CommandList.h>
  10. #include <Atom/RHI.Reflect/InputStreamLayoutBuilder.h>
  11. #include <Atom/RHI.Reflect/RenderAttachmentLayoutBuilder.h>
  12. #include <Atom/RPI.Public/Shader/Shader.h>
  13. #include <Atom/RPI.Reflect/Shader/ShaderAsset.h>
  14. #include <AzCore/Math/Color.h>
  15. #include <AzCore/Serialization/SerializeContext.h>
  16. #include <SampleComponentManager.h>
  17. #include <Utils/Utils.h>
  18. namespace AtomSampleViewer
  19. {
  20. void XRExampleComponent::Reflect(AZ::ReflectContext* context)
  21. {
  22. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  23. {
  24. serializeContext->Class<XRExampleComponent, AZ::Component>()->Version(0);
  25. }
  26. }
  27. XRExampleComponent::XRExampleComponent()
  28. {
  29. m_supportRHISamplePipeline = true;
  30. }
  31. void XRExampleComponent::Activate()
  32. {
  33. if (!AZ::RPI::RPISystemInterface::Get()->GetXRSystem())
  34. {
  35. return;
  36. }
  37. m_depthStencilID = AZ::RHI::AttachmentId{ AZStd::string::format("DepthStencilID_%llu", GetId()) };
  38. CreateCubeInputAssemblyBuffer();
  39. CreateCubePipeline();
  40. CreateScope();
  41. AZ::RHI::RHISystemNotificationBus::Handler::BusConnect();
  42. AZ::TickBus::Handler::BusConnect();
  43. }
  44. void XRExampleComponent::OnTick(float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  45. {
  46. m_time += deltaTime;
  47. }
  48. void XRExampleComponent::OnFramePrepare(AZ::RHI::FrameGraphBuilder& frameGraphBuilder)
  49. {
  50. AZ::Matrix4x4 projection = AZ::Matrix4x4::CreateIdentity();
  51. AZ::RPI::XRRenderingInterface* xrSystem = AZ::RPI::RPISystemInterface::Get()->GetXRSystem();
  52. if (xrSystem && xrSystem->ShouldRender())
  53. {
  54. AZ::RPI::FovData fovData;
  55. AZ::RPI::PoseData poseData, frontViewPoseData;
  56. [[maybe_unused]] AZ::RHI::ResultCode resultCode = xrSystem->GetViewFov(m_viewIndex, fovData);
  57. resultCode = xrSystem->GetViewPose(m_viewIndex, poseData);
  58. static const float clip_near = 0.05f;
  59. static const float clip_far = 100.0f;
  60. bool reverseDepth = false;
  61. projection = xrSystem->CreateStereoscopicProjection(fovData.m_angleLeft, fovData.m_angleRight,
  62. fovData.m_angleDown, fovData.m_angleUp,
  63. clip_near, clip_far, reverseDepth);
  64. AZ::Quaternion poseOrientation = poseData.m_orientation;
  65. poseOrientation.InvertFast();
  66. AZ::Matrix4x4 viewMat = AZ::Matrix4x4::CreateFromQuaternionAndTranslation(poseOrientation, -poseData.m_position);
  67. m_viewProjMatrix = projection * viewMat;
  68. const AZ::Matrix4x4 initialScaleMat = AZ::Matrix4x4::CreateScale(AZ::Vector3(0.1f, 0.1f, 0.1f));
  69. //Model matrix for the cube related to the front view
  70. resultCode = xrSystem->GetViewFrontPose(frontViewPoseData);
  71. m_modelMatrices[0] = AZ::Matrix4x4::CreateFromQuaternionAndTranslation(frontViewPoseData.m_orientation, frontViewPoseData.m_position) * initialScaleMat;
  72. //Model matrix for the cube related to the left controller
  73. AZ::RPI::PoseData controllerLeftPose, controllerRightPose;
  74. resultCode = xrSystem->GetControllerPose(0, controllerLeftPose);
  75. AZ::Matrix4x4 leftScaleMat = initialScaleMat * AZ::Matrix4x4::CreateScale(AZ::Vector3(xrSystem->GetControllerScale(0)));
  76. m_modelMatrices[1] = AZ::Matrix4x4::CreateFromQuaternionAndTranslation(controllerLeftPose.m_orientation, controllerLeftPose.m_position) * leftScaleMat;
  77. //Model matrix for the cube related to the right controller
  78. AZ::Matrix4x4 rightScaleMat = initialScaleMat * AZ::Matrix4x4::CreateScale(AZ::Vector3(xrSystem->GetControllerScale(1)));
  79. resultCode = xrSystem->GetControllerPose(1, controllerRightPose);
  80. m_modelMatrices[2] = AZ::Matrix4x4::CreateFromQuaternionAndTranslation(controllerRightPose.m_orientation, controllerRightPose.m_position) * rightScaleMat;
  81. }
  82. for (int i = 0; i < NumberOfCubes; ++i)
  83. {
  84. m_shaderResourceGroups[i]->SetConstant(m_shaderIndexWorldMat, m_modelMatrices[i]);
  85. m_shaderResourceGroups[i]->SetConstant(m_shaderIndexViewProj, m_viewProjMatrix);
  86. m_shaderResourceGroups[i]->Compile();
  87. }
  88. BasicRHIComponent::OnFramePrepare(frameGraphBuilder);
  89. }
  90. XRExampleComponent::SingleCubeBufferData XRExampleComponent::CreateSingleCubeBufferData()
  91. {
  92. const AZStd::fixed_vector<AZ::Color, GeometryVertexCount> vertexColor =
  93. {
  94. //Front Face
  95. AZ::Colors::DarkBlue, AZ::Colors::DarkBlue, AZ::Colors::DarkBlue, AZ::Colors::DarkBlue,
  96. //Back Face
  97. AZ::Colors::Blue, AZ::Colors::Blue, AZ::Colors::Blue, AZ::Colors::Blue,
  98. //Left Face
  99. AZ::Colors::DarkGreen, AZ::Colors::DarkGreen, AZ::Colors::DarkGreen, AZ::Colors::DarkGreen,
  100. //Right Face
  101. AZ::Colors::Green, AZ::Colors::Green, AZ::Colors::Green, AZ::Colors::Green,
  102. //Top Face
  103. AZ::Colors::DarkRed, AZ::Colors::DarkRed, AZ::Colors::DarkRed, AZ::Colors::DarkRed,
  104. //Bottom Face
  105. AZ::Colors::Red, AZ::Colors::Red, AZ::Colors::Red, AZ::Colors::Red,
  106. };
  107. // Create vertices, colors and normals for a cube and a plane
  108. SingleCubeBufferData bufferData;
  109. {
  110. const AZStd::fixed_vector<AZ::Vector3, GeometryVertexCount> vertices =
  111. {
  112. //Front Face
  113. AZ::Vector3(1.0, 1.0, 1.0), AZ::Vector3(-1.0, 1.0, 1.0), AZ::Vector3(-1.0, -1.0, 1.0), AZ::Vector3(1.0, -1.0, 1.0),
  114. //Back Face
  115. AZ::Vector3(1.0, 1.0, -1.0), AZ::Vector3(-1.0, 1.0, -1.0), AZ::Vector3(-1.0, -1.0, -1.0), AZ::Vector3(1.0, -1.0, -1.0),
  116. //Left Face
  117. AZ::Vector3(-1.0, 1.0, 1.0), AZ::Vector3(-1.0, -1.0, 1.0), AZ::Vector3(-1.0, -1.0, -1.0), AZ::Vector3(-1.0, 1.0, -1.0),
  118. //Right Face
  119. AZ::Vector3(1.0, 1.0, 1.0), AZ::Vector3(1.0, -1.0, 1.0), AZ::Vector3(1.0, -1.0, -1.0), AZ::Vector3(1.0, 1.0, -1.0),
  120. //Top Face
  121. AZ::Vector3(1.0, 1.0, 1.0), AZ::Vector3(-1.0, 1.0, 1.0), AZ::Vector3(-1.0, 1.0, -1.0), AZ::Vector3(1.0, 1.0, -1.0),
  122. //Bottom Face
  123. AZ::Vector3(1.0, -1.0, 1.0), AZ::Vector3(-1.0, -1.0, 1.0), AZ::Vector3(-1.0, -1.0, -1.0), AZ::Vector3(1.0, -1.0, -1.0),
  124. };
  125. for (int i = 0; i < GeometryVertexCount; ++i)
  126. {
  127. SetVertexPosition(bufferData.m_positions.data(), i, vertices[i]);
  128. SetVertexColor(bufferData.m_colors.data(), i, vertexColor[i].GetAsVector4());
  129. }
  130. bufferData.m_indices =
  131. {
  132. {
  133. //Back
  134. 2, 0, 1,
  135. 0, 2, 3,
  136. //Front
  137. 4, 6, 5,
  138. 6, 4, 7,
  139. //Left
  140. 8, 10, 9,
  141. 10, 8, 11,
  142. //Right
  143. 14, 12, 13,
  144. 15, 12, 14,
  145. //Top
  146. 16, 18, 17,
  147. 18, 16, 19,
  148. //Bottom
  149. 22, 20, 21,
  150. 23, 20, 22,
  151. }
  152. };
  153. }
  154. return bufferData;
  155. }
  156. void XRExampleComponent::CreateCubeInputAssemblyBuffer()
  157. {
  158. const AZ::RHI::Ptr<AZ::RHI::Device> device = Utils::GetRHIDevice();
  159. AZ::RHI::ResultCode result = AZ::RHI::ResultCode::Success;
  160. m_bufferPool = AZ::RHI::Factory::Get().CreateBufferPool();
  161. AZ::RHI::BufferPoolDescriptor bufferPoolDesc;
  162. bufferPoolDesc.m_bindFlags = AZ::RHI::BufferBindFlags::InputAssembly;
  163. bufferPoolDesc.m_heapMemoryLevel = AZ::RHI::HeapMemoryLevel::Device;
  164. result = m_bufferPool->Init(*device, bufferPoolDesc);
  165. if (result != AZ::RHI::ResultCode::Success)
  166. {
  167. AZ_Error("XRExampleComponent", false, "Failed to initialize buffer pool with error code %d", result);
  168. return;
  169. }
  170. SingleCubeBufferData bufferData = CreateSingleCubeBufferData();
  171. m_inputAssemblyBuffer = AZ::RHI::Factory::Get().CreateBuffer();
  172. AZ::RHI::BufferInitRequest request;
  173. request.m_buffer = m_inputAssemblyBuffer.get();
  174. request.m_descriptor = AZ::RHI::BufferDescriptor{ AZ::RHI::BufferBindFlags::InputAssembly, sizeof(SingleCubeBufferData) };
  175. request.m_initialData = &bufferData;
  176. result = m_bufferPool->InitBuffer(request);
  177. if (result != AZ::RHI::ResultCode::Success)
  178. {
  179. AZ_Error("XRExampleComponent", false, "Failed to initialize buffer with error code %d", result);
  180. return;
  181. }
  182. m_streamBufferViews[0] =
  183. {
  184. *m_inputAssemblyBuffer,
  185. offsetof(SingleCubeBufferData, m_positions),
  186. sizeof(SingleCubeBufferData::m_positions),
  187. sizeof(VertexPosition)
  188. };
  189. m_streamBufferViews[1] =
  190. {
  191. *m_inputAssemblyBuffer,
  192. offsetof(SingleCubeBufferData, m_colors),
  193. sizeof(SingleCubeBufferData::m_colors),
  194. sizeof(VertexColor)
  195. };
  196. m_indexBufferView =
  197. {
  198. *m_inputAssemblyBuffer,
  199. offsetof(SingleCubeBufferData, m_indices),
  200. sizeof(SingleCubeBufferData::m_indices),
  201. AZ::RHI::IndexFormat::Uint16
  202. };
  203. AZ::RHI::InputStreamLayoutBuilder layoutBuilder;
  204. layoutBuilder.SetTopology(AZ::RHI::PrimitiveTopology::TriangleList);
  205. layoutBuilder.AddBuffer()->Channel("POSITION", AZ::RHI::Format::R32G32B32_FLOAT);
  206. layoutBuilder.AddBuffer()->Channel("COLOR", AZ::RHI::Format::R32G32B32A32_FLOAT);
  207. m_streamLayoutDescriptor.Clear();
  208. m_streamLayoutDescriptor = layoutBuilder.End();
  209. AZ::RHI::ValidateStreamBufferViews(m_streamLayoutDescriptor, m_streamBufferViews);
  210. }
  211. void XRExampleComponent::CreateCubePipeline()
  212. {
  213. const char* shaderFilePath = "Shaders/RHI/OpenXrSample.azshader";
  214. const char* sampleName = "XRExampleComponent";
  215. auto shader = LoadShader(shaderFilePath, sampleName);
  216. if (shader == nullptr)
  217. {
  218. return;
  219. }
  220. const AZ::RHI::Ptr<AZ::RHI::Device> device = Utils::GetRHIDevice();
  221. AZ::RHI::PipelineStateDescriptorForDraw pipelineDesc;
  222. shader->GetVariant(AZ::RPI::ShaderAsset::RootShaderVariantStableId).ConfigurePipelineState(pipelineDesc);
  223. pipelineDesc.m_inputStreamLayout = m_streamLayoutDescriptor;
  224. pipelineDesc.m_renderStates.m_depthStencilState.m_depth.m_enable = 1;
  225. pipelineDesc.m_renderStates.m_depthStencilState.m_depth.m_func = AZ::RHI::ComparisonFunc::LessEqual;
  226. AZ::RHI::RenderAttachmentLayoutBuilder attachmentsBuilder;
  227. attachmentsBuilder.AddSubpass()
  228. ->RenderTargetAttachment(m_outputFormat)
  229. ->DepthStencilAttachment(device->GetNearestSupportedFormat(AZ::RHI::Format::D24_UNORM_S8_UINT, AZ::RHI::FormatCapabilities::DepthStencil));
  230. [[maybe_unused]] AZ::RHI::ResultCode result = attachmentsBuilder.End(pipelineDesc.m_renderAttachmentConfiguration.m_renderAttachmentLayout);
  231. AZ_Assert(result == AZ::RHI::ResultCode::Success, "Failed to create render attachment layout");
  232. m_pipelineState = shader->AcquirePipelineState(pipelineDesc);
  233. if (!m_pipelineState)
  234. {
  235. AZ_Error("XRExampleComponent", false, "Failed to acquire default pipeline state for shader '%s'", shaderFilePath);
  236. return;
  237. }
  238. auto perInstanceSrgLayout = shader->FindShaderResourceGroupLayout(AZ::Name{ "OpenXrSrg" });
  239. if (!perInstanceSrgLayout)
  240. {
  241. AZ_Error("XRExampleComponent", false, "Failed to get shader resource group layout");
  242. return;
  243. }
  244. for (int i = 0; i < NumberOfCubes; ++i)
  245. {
  246. m_shaderResourceGroups[i] = CreateShaderResourceGroup(shader, "OpenXrSrg", sampleName);
  247. }
  248. // Using the first SRG to get the correct index as all the SRGs will have the same indices.
  249. FindShaderInputIndex(&m_shaderIndexWorldMat, m_shaderResourceGroups[0], AZ::Name{ "m_worldMatrix" }, "XRExampleComponent");
  250. FindShaderInputIndex(&m_shaderIndexViewProj, m_shaderResourceGroups[0], AZ::Name{ "m_viewProjMatrix" }, "XRExampleComponent");
  251. }
  252. void XRExampleComponent::CreateScope()
  253. {
  254. // Creates a scope for rendering the triangle.
  255. struct ScopeData
  256. {
  257. };
  258. const auto prepareFunction = [this](AZ::RHI::FrameGraphInterface frameGraph, [[maybe_unused]] ScopeData& scopeData)
  259. {
  260. // Binds the swap chain as a color attachment. Clears it to black.
  261. {
  262. AZ::RHI::ImageScopeAttachmentDescriptor descriptor;
  263. descriptor.m_attachmentId = m_outputAttachmentId;
  264. descriptor.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Load;
  265. frameGraph.UseColorAttachment(descriptor);
  266. }
  267. // Create & Binds DepthStencil image
  268. {
  269. const AZ::RHI::Ptr<AZ::RHI::Device> device = Utils::GetRHIDevice();
  270. const AZ::RHI::ImageDescriptor imageDescriptor = AZ::RHI::ImageDescriptor::Create2D(
  271. AZ::RHI::ImageBindFlags::DepthStencil,
  272. m_outputWidth,
  273. m_outputHeight,
  274. device->GetNearestSupportedFormat(AZ::RHI::Format::D24_UNORM_S8_UINT, AZ::RHI::FormatCapabilities::DepthStencil));
  275. const AZ::RHI::TransientImageDescriptor transientImageDescriptor(m_depthStencilID, imageDescriptor);
  276. frameGraph.GetAttachmentDatabase().CreateTransientImage(transientImageDescriptor);
  277. AZ::RHI::ImageScopeAttachmentDescriptor dsDesc;
  278. dsDesc.m_attachmentId = m_depthStencilID;
  279. dsDesc.m_imageViewDescriptor.m_overrideFormat = device->GetNearestSupportedFormat(AZ::RHI::Format::D24_UNORM_S8_UINT, AZ::RHI::FormatCapabilities::DepthStencil);
  280. dsDesc.m_loadStoreAction.m_clearValue = AZ::RHI::ClearValue::CreateDepthStencil(1.0f, 0);
  281. dsDesc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Clear;
  282. dsDesc.m_loadStoreAction.m_loadActionStencil = AZ::RHI::AttachmentLoadAction::DontCare;
  283. frameGraph.UseDepthStencilAttachment(dsDesc, AZ::RHI::ScopeAttachmentAccess::Write);
  284. }
  285. // We will submit NumberOfCubes draw items.
  286. frameGraph.SetEstimatedItemCount(NumberOfCubes);
  287. };
  288. AZ::RHI::EmptyCompileFunction<ScopeData> compileFunction;
  289. const auto executeFunction = [this](const AZ::RHI::FrameGraphExecuteContext& context, [[maybe_unused]] const ScopeData& scopeData)
  290. {
  291. AZ::RHI::CommandList* commandList = context.GetCommandList();
  292. // Set persistent viewport and scissor state.
  293. commandList->SetViewports(&m_viewport, 1);
  294. commandList->SetScissors(&m_scissor, 1);
  295. AZ::RHI::DrawIndexed drawIndexed;
  296. drawIndexed.m_indexCount = GeometryIndexCount;
  297. drawIndexed.m_instanceCount = 1;
  298. // Dividing NumberOfCubes by context.GetCommandListCount() to balance to number
  299. // of draw call equally between each thread.
  300. uint32_t numberOfCubesPerCommandList = NumberOfCubes / context.GetCommandListCount();
  301. uint32_t indexStart = context.GetCommandListIndex() * numberOfCubesPerCommandList;
  302. uint32_t indexEnd = indexStart + numberOfCubesPerCommandList;
  303. if (context.GetCommandListIndex() == context.GetCommandListCount() - 1)
  304. {
  305. indexEnd = NumberOfCubes;
  306. }
  307. for (uint32_t i = indexStart; i < indexEnd; ++i)
  308. {
  309. const AZ::RHI::ShaderResourceGroup* shaderResourceGroups[] = { m_shaderResourceGroups[i]->GetRHIShaderResourceGroup() };
  310. AZ::RHI::DrawItem drawItem;
  311. drawItem.m_arguments = drawIndexed;
  312. drawItem.m_pipelineState = m_pipelineState.get();
  313. drawItem.m_indexBufferView = &m_indexBufferView;
  314. drawItem.m_shaderResourceGroupCount = static_cast<uint8_t>(AZ::RHI::ArraySize(shaderResourceGroups));
  315. drawItem.m_shaderResourceGroups = shaderResourceGroups;
  316. drawItem.m_streamBufferViewCount = static_cast<uint8_t>(m_streamBufferViews.size());
  317. drawItem.m_streamBufferViews = m_streamBufferViews.data();
  318. commandList->Submit(drawItem);
  319. }
  320. };
  321. m_scopeProducers.emplace_back(
  322. aznew AZ::RHI::ScopeProducerFunction<
  323. ScopeData,
  324. decltype(prepareFunction),
  325. decltype(compileFunction),
  326. decltype(executeFunction)>(
  327. AZ::RHI::ScopeId{ AZStd::string::format("XRSample_Id_%llu", GetId()) },
  328. ScopeData{},
  329. prepareFunction,
  330. compileFunction,
  331. executeFunction));
  332. }
  333. void XRExampleComponent::Deactivate()
  334. {
  335. if (!AZ::RPI::RPISystemInterface::Get()->GetXRSystem())
  336. {
  337. return;
  338. }
  339. m_inputAssemblyBuffer = nullptr;
  340. m_bufferPool = nullptr;
  341. m_pipelineState = nullptr;
  342. m_shaderResourceGroups.fill(nullptr);
  343. AZ::RHI::RHISystemNotificationBus::Handler::BusDisconnect();
  344. m_windowContext = nullptr;
  345. m_scopeProducers.clear();
  346. }
  347. }