MultiThreadComponent.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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/MultiThreadComponent.h>
  9. #include <AzCore/Math/MatrixUtils.h>
  10. #include <Atom/RHI/DrawItem.h>
  11. #include <Atom/RHI.Reflect/RenderAttachmentLayoutBuilder.h>
  12. #include <Atom/RPI.Public/Shader/Shader.h>
  13. #include <AzCore/Math/Random.h>
  14. #include <SampleComponentManager.h>
  15. #include <Utils/Utils.h>
  16. namespace AtomSampleViewer
  17. {
  18. // static const variables.
  19. const AZ::Vector3 MultiThreadComponent::m_up = AZ::Vector3(0.0f, 1.0f, 0.0f);
  20. void MultiThreadComponent::Reflect(AZ::ReflectContext* context)
  21. {
  22. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  23. {
  24. serializeContext->Class<MultiThreadComponent, AZ::Component>()
  25. ->Version(0)
  26. ;
  27. }
  28. }
  29. MultiThreadComponent::MultiThreadComponent()
  30. {
  31. m_depthStencilID = AZ::RHI::AttachmentId{ "DepthStencilID" };
  32. uint32_t index = 0;
  33. // Create positions for each cube
  34. for (uint32_t j = 0; j < s_cubesPerLine*s_cubeSpacing; j+= s_cubeSpacing)
  35. {
  36. for (uint32_t i = 0; i < s_cubesPerLine*s_cubeSpacing; i+= s_cubeSpacing)
  37. {
  38. m_cubeTransforms[index] = AZ::Matrix4x4::CreateTranslation(AZ::Vector3(static_cast<float>(i), static_cast<float>(j), 0.0f));
  39. ++index;
  40. }
  41. }
  42. m_supportRHISamplePipeline = true;
  43. }
  44. void MultiThreadComponent::OnFramePrepare(AZ::RHI::FrameGraphBuilder& frameGraphBuilder)
  45. {
  46. m_time += 0.005f;
  47. BasicRHIComponent::OnFramePrepare(frameGraphBuilder);
  48. }
  49. void MultiThreadComponent::Activate()
  50. {
  51. // This is done here instead of doing it in the constructor,
  52. // since m_windowContext might not be yet initialized at construction time.
  53. float fieldOfView = AZ::Constants::Pi / 4.0f;
  54. float screenAspect = GetViewportWidth() / GetViewportHeight();
  55. float heighOfCubePlane = static_cast<float>(s_cubesPerLine * s_cubeSpacing);
  56. float distanceFromCubePlane = 1.0f * (1 / tanf(fieldOfView/2)) * heighOfCubePlane/2;
  57. float centerOfScreen = heighOfCubePlane/2;
  58. AZ::Vector3 m_worldPosition = AZ::Vector3(centerOfScreen, centerOfScreen, distanceFromCubePlane);
  59. m_lookAt = AZ::Vector3(centerOfScreen, centerOfScreen, 0.0f);
  60. MakePerspectiveFovMatrixRH(m_viewProjMatrix, fieldOfView, screenAspect, m_zNear, m_zFar);
  61. m_viewProjMatrix = m_viewProjMatrix * CreateViewMatrix(m_worldPosition, m_up, m_lookAt);
  62. CreateInputAssemblyBuffer();
  63. CreatePipeline();
  64. CreateScope();
  65. AZ::RHI::RHISystemNotificationBus::Handler::BusConnect();
  66. }
  67. void MultiThreadComponent::Deactivate()
  68. {
  69. AZ::RHI::RHISystemNotificationBus::Handler::BusDisconnect();
  70. m_windowContext = nullptr;
  71. m_bufferPool = nullptr;
  72. m_inputAssemblyBuffer = nullptr;
  73. m_shaderResourceGroups.fill(nullptr);
  74. m_pipelineState = nullptr;
  75. m_scopeProducers.clear();
  76. }
  77. MultiThreadComponent::SingleCubeBufferData MultiThreadComponent::CreateSingleCubeBufferData(const AZ::Vector4 color)
  78. {
  79. // Create vertices, colors and normals for a cube and a plane
  80. SingleCubeBufferData bufferData;
  81. {
  82. AZStd::vector<AZ::Vector3> vertices =
  83. {
  84. //Front Face
  85. 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),
  86. //Back Face
  87. 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),
  88. //Left Face
  89. 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),
  90. //Right Face
  91. 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),
  92. //Top Face
  93. 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),
  94. //Bottom Face
  95. 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),
  96. };
  97. for (int i = 0; i < s_geometryVertexCount; ++i)
  98. {
  99. SetVertexPosition(bufferData.m_positions.data(), i, vertices[i]);
  100. SetVertexColor(bufferData.m_colors.data(), i, color);
  101. }
  102. bufferData.m_indices =
  103. {
  104. {
  105. //Back
  106. 2, 0, 1,
  107. 0, 2, 3,
  108. //Front
  109. 4, 6, 5,
  110. 6, 4, 7,
  111. //Left
  112. 8, 10, 9,
  113. 10, 8, 11,
  114. //Right
  115. 14, 12, 13,
  116. 15, 12, 14,
  117. //Top
  118. 16, 18, 17,
  119. 18, 16, 19,
  120. //Bottom
  121. 22, 20, 21,
  122. 23, 20, 22,
  123. }
  124. };
  125. }
  126. return bufferData;
  127. }
  128. void MultiThreadComponent::CreateInputAssemblyBuffer()
  129. {
  130. const AZ::RHI::Ptr<AZ::RHI::Device> device = Utils::GetRHIDevice();
  131. AZ::RHI::ResultCode result = AZ::RHI::ResultCode::Success;
  132. m_bufferPool = aznew AZ::RHI::BufferPool();
  133. AZ::RHI::BufferPoolDescriptor bufferPoolDesc;
  134. bufferPoolDesc.m_bindFlags = AZ::RHI::BufferBindFlags::InputAssembly;
  135. bufferPoolDesc.m_heapMemoryLevel = AZ::RHI::HeapMemoryLevel::Device;
  136. result = m_bufferPool->Init(bufferPoolDesc);
  137. if (result != AZ::RHI::ResultCode::Success)
  138. {
  139. AZ_Error("MultiThreadComponent", false, "Failed to initialize buffer pool with error code %d", result);
  140. return;
  141. }
  142. SingleCubeBufferData bufferData = CreateSingleCubeBufferData(AZ::Vector4(1.0f, 0.0f, 0.0f, 0.0f));
  143. m_inputAssemblyBuffer = aznew AZ::RHI::Buffer();
  144. AZ::RHI::BufferInitRequest request;
  145. request.m_buffer = m_inputAssemblyBuffer.get();
  146. request.m_descriptor = AZ::RHI::BufferDescriptor{ AZ::RHI::BufferBindFlags::InputAssembly, sizeof(SingleCubeBufferData) };
  147. request.m_initialData = &bufferData;
  148. result = m_bufferPool->InitBuffer(request);
  149. if (result != AZ::RHI::ResultCode::Success)
  150. {
  151. AZ_Error("MultiThreadComponent", false, "Failed to initialize buffer with error code %d", result);
  152. return;
  153. }
  154. m_geometryView.SetDrawArguments(AZ::RHI::DrawIndexed(0, s_geometryIndexCount, 0));
  155. m_geometryView.AddStreamBufferView({
  156. *m_inputAssemblyBuffer,
  157. offsetof(SingleCubeBufferData, m_positions),
  158. sizeof(SingleCubeBufferData::m_positions),
  159. sizeof(VertexPosition)
  160. });
  161. m_geometryView.AddStreamBufferView({
  162. *m_inputAssemblyBuffer,
  163. offsetof(SingleCubeBufferData, m_colors),
  164. sizeof(SingleCubeBufferData::m_colors),
  165. sizeof(VertexColor)
  166. });
  167. m_geometryView.SetIndexBufferView({
  168. *m_inputAssemblyBuffer,
  169. offsetof(SingleCubeBufferData, m_indices),
  170. sizeof(SingleCubeBufferData::m_indices),
  171. AZ::RHI::IndexFormat::Uint16
  172. });
  173. AZ::RHI::InputStreamLayoutBuilder layoutBuilder;
  174. layoutBuilder.SetTopology(AZ::RHI::PrimitiveTopology::TriangleList);
  175. layoutBuilder.AddBuffer()->Channel("POSITION", AZ::RHI::Format::R32G32B32_FLOAT);
  176. layoutBuilder.AddBuffer()->Channel("COLOR", AZ::RHI::Format::R32G32B32A32_FLOAT);
  177. m_streamLayoutDescriptor.Clear();
  178. m_streamLayoutDescriptor = layoutBuilder.End();
  179. AZ::RHI::ValidateStreamBufferViews(m_streamLayoutDescriptor, m_geometryView, m_geometryView.GetFullStreamBufferIndices());
  180. }
  181. void MultiThreadComponent::CreatePipeline()
  182. {
  183. const char* shaderFilePath = "Shaders/RHI/MultiThread.azshader";
  184. const char* sampleName = "MultiThreadComponent";
  185. auto shader = LoadShader(shaderFilePath, sampleName);
  186. if (shader == nullptr)
  187. return;
  188. const AZ::RHI::Ptr<AZ::RHI::Device> device = Utils::GetRHIDevice();
  189. AZ::RHI::PipelineStateDescriptorForDraw pipelineDesc;
  190. shader->GetVariant(AZ::RPI::ShaderAsset::RootShaderVariantStableId).ConfigurePipelineState(pipelineDesc);
  191. pipelineDesc.m_inputStreamLayout = m_streamLayoutDescriptor;
  192. pipelineDesc.m_renderStates.m_depthStencilState.m_depth.m_enable = 1;
  193. pipelineDesc.m_renderStates.m_depthStencilState.m_depth.m_func = AZ::RHI::ComparisonFunc::LessEqual;
  194. AZ::RHI::RenderAttachmentLayoutBuilder attachmentsBuilder;
  195. attachmentsBuilder.AddSubpass()
  196. ->RenderTargetAttachment(m_outputFormat)
  197. ->DepthStencilAttachment(device->GetNearestSupportedFormat(AZ::RHI::Format::D24_UNORM_S8_UINT, AZ::RHI::FormatCapabilities::DepthStencil));
  198. [[maybe_unused]] AZ::RHI::ResultCode result = attachmentsBuilder.End(pipelineDesc.m_renderAttachmentConfiguration.m_renderAttachmentLayout);
  199. AZ_Assert(result == AZ::RHI::ResultCode::Success, "Failed to create render attachment layout");
  200. m_pipelineState = shader->AcquirePipelineState(pipelineDesc);
  201. if (!m_pipelineState)
  202. {
  203. AZ_Error("MultiThreadComponent", false, "Failed to acquire default pipeline state for shader '%s'", shaderFilePath);
  204. return;
  205. }
  206. auto perInstanceSrgLayout = shader->FindShaderResourceGroupLayout(AZ::Name{ "MultiThreadInstanceSrg" });
  207. if (!perInstanceSrgLayout)
  208. {
  209. AZ_Error("MultiThreadComponent", false, "Failed to get shader resource group layout");
  210. return;
  211. }
  212. for (int i = 0; i < s_numberOfCubes; ++i)
  213. {
  214. m_shaderResourceGroups[i] = CreateShaderResourceGroup(shader, "MultiThreadInstanceSrg", sampleName);
  215. FindShaderInputIndex(&m_shaderIndexWorldMat, m_shaderResourceGroups[i], AZ::Name{"m_worldMatrix"}, "MultiThreadComponent");
  216. FindShaderInputIndex(&m_shaderIndexViewProj, m_shaderResourceGroups[i], AZ::Name{"m_viewProjMatrix"}, "MultiThreadComponent");
  217. }
  218. }
  219. void MultiThreadComponent::CreateScope()
  220. {
  221. const auto prepareFunction = [this](AZ::RHI::FrameGraphInterface frameGraph, [[maybe_unused]] ScopeData& scopeData)
  222. {
  223. // Binds the swap chain as a color attachment. Clears it to black.
  224. {
  225. AZ::RHI::ImageScopeAttachmentDescriptor descriptor;
  226. descriptor.m_attachmentId = m_outputAttachmentId;
  227. descriptor.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Load;
  228. frameGraph.UseColorAttachment(descriptor);
  229. }
  230. // Create & Binds DepthStencil image
  231. {
  232. const AZ::RHI::Ptr<AZ::RHI::Device> device = Utils::GetRHIDevice();
  233. const AZ::RHI::ImageDescriptor imageDescriptor = AZ::RHI::ImageDescriptor::Create2D(
  234. AZ::RHI::ImageBindFlags::DepthStencil,
  235. m_outputWidth,
  236. m_outputHeight,
  237. device->GetNearestSupportedFormat(AZ::RHI::Format::D24_UNORM_S8_UINT, AZ::RHI::FormatCapabilities::DepthStencil));
  238. const AZ::RHI::TransientImageDescriptor transientImageDescriptor(m_depthStencilID, imageDescriptor);
  239. frameGraph.GetAttachmentDatabase().CreateTransientImage(transientImageDescriptor);
  240. AZ::RHI::ImageScopeAttachmentDescriptor dsDesc;
  241. dsDesc.m_attachmentId = m_depthStencilID;
  242. dsDesc.m_imageViewDescriptor.m_overrideFormat = device->GetNearestSupportedFormat(AZ::RHI::Format::D24_UNORM_S8_UINT, AZ::RHI::FormatCapabilities::DepthStencil);
  243. dsDesc.m_loadStoreAction.m_clearValue = AZ::RHI::ClearValue::CreateDepthStencil(1.0f, 0);
  244. dsDesc.m_loadStoreAction.m_loadAction = AZ::RHI::AttachmentLoadAction::Clear;
  245. frameGraph.UseDepthStencilAttachment(
  246. dsDesc, AZ::RHI::ScopeAttachmentAccess::Write,
  247. AZ::RHI::ScopeAttachmentStage::EarlyFragmentTest | AZ::RHI::ScopeAttachmentStage::LateFragmentTest);
  248. }
  249. // We will submit s_numberOfCubes draw items.
  250. frameGraph.SetEstimatedItemCount(s_numberOfCubes);
  251. };
  252. const auto compileFunction = [this]([[maybe_unused]] const AZ::RHI::FrameGraphCompileContext& context, [[maybe_unused]] const ScopeData& scopeData)
  253. {
  254. AZ::Matrix4x4 rotation = AZ::Matrix4x4::CreateRotationY(m_time);
  255. for(int i = 0; i<s_numberOfCubes; ++i)
  256. {
  257. AZ::Matrix4x4 transform = m_cubeTransforms[i] * rotation;
  258. m_shaderResourceGroups[i]->SetConstant(m_shaderIndexWorldMat, transform);
  259. m_shaderResourceGroups[i]->SetConstant(m_shaderIndexViewProj, m_viewProjMatrix);
  260. m_shaderResourceGroups[i]->Compile();
  261. }
  262. };
  263. const auto executeFunction = [this](const AZ::RHI::FrameGraphExecuteContext& context, [[maybe_unused]] const ScopeData& scopeData)
  264. {
  265. AZ::RHI::CommandList* commandList = context.GetCommandList();
  266. // Set persistent viewport and scissor state.
  267. commandList->SetViewports(&m_viewport, 1);
  268. commandList->SetScissors(&m_scissor, 1);
  269. if (context.GetCommandListIndex() == context.GetCommandListCount() - 1)
  270. {
  271. #if defined(AZ_DEBUG_BUILD)
  272. AZ_Printf("MultiThread", "Draw Calls: %d \n", s_numberOfCubes);
  273. AZ_Printf("MultiThread", "Num CommandLists: %d \n", context.GetCommandListCount());
  274. #endif
  275. }
  276. for (uint32_t i = context.GetSubmitRange().m_startIndex; i < context.GetSubmitRange().m_endIndex; ++i)
  277. {
  278. const AZ::RHI::DeviceShaderResourceGroup* shaderResourceGroups[] = {
  279. m_shaderResourceGroups[i]->GetRHIShaderResourceGroup()->GetDeviceShaderResourceGroup(context.GetDeviceIndex()).get()
  280. };
  281. AZ::RHI::DeviceDrawItem drawItem;
  282. drawItem.m_geometryView = m_geometryView.GetDeviceGeometryView(context.GetDeviceIndex());
  283. drawItem.m_streamIndices = m_geometryView.GetFullStreamBufferIndices();
  284. drawItem.m_pipelineState = m_pipelineState->GetDevicePipelineState(context.GetDeviceIndex()).get();
  285. drawItem.m_shaderResourceGroupCount = static_cast<uint8_t>(AZ::RHI::ArraySize(shaderResourceGroups));
  286. drawItem.m_shaderResourceGroups = shaderResourceGroups;
  287. commandList->Submit(drawItem, i);
  288. }
  289. };
  290. m_scopeProducers.emplace_back(
  291. aznew AZ::RHI::ScopeProducerFunction<
  292. ScopeData,
  293. decltype(prepareFunction),
  294. decltype(compileFunction),
  295. decltype(executeFunction)>(
  296. AZ::RHI::ScopeId{"MultiThreadMain"},
  297. ScopeData{},
  298. prepareFunction,
  299. compileFunction,
  300. executeFunction));
  301. }
  302. }// namespace AtomSampleViewer