InputAssemblyExampleComponent.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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/RHI/CommandList.h>
  9. #include <Atom/RHI/Factory.h>
  10. #include <Atom/RHI/FrameScheduler.h>
  11. #include <Atom/RHI/ScopeProducerFunction.h>
  12. #include <Atom/RHI.Reflect/InputStreamLayoutBuilder.h>
  13. #include <Atom/RHI.Reflect/RenderAttachmentLayoutBuilder.h>
  14. #include <Atom/RPI.Reflect/Shader/ShaderAsset.h>
  15. #include <AzCore/Math/Matrix4x4.h>
  16. #include <AzCore/Math/Vector4.h>
  17. #include <AzCore/Serialization/SerializeContext.h>
  18. #include <RHI/InputAssemblyExampleComponent.h>
  19. #include <SampleComponentConfig.h>
  20. #include <SampleComponentManager.h>
  21. #include <Utils/Utils.h>
  22. namespace AtomSampleViewer
  23. {
  24. namespace InputAssembly
  25. {
  26. const char* SampleName = "InputaAssemblyExample";
  27. const char* const ShaderInputTime{ "m_time" };
  28. const char* const ShaderInpuIABuffer{ "m_IABuffer" };
  29. const char* const ShaderInputMatrix{ "m_matrix" };
  30. const char* const ShaderInputColor{ "m_color" };
  31. const char* InputAssemblyBufferAttachmentId = "InputAssemblyBufferAttachmentId";
  32. const char* ImportedInputAssemblyBufferAttachmentId = "ImportedInputAssemblyBufferAttachmentId";
  33. }
  34. void InputAssemblyExampleComponent::Reflect(AZ::ReflectContext* context)
  35. {
  36. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  37. {
  38. serializeContext->Class<InputAssemblyExampleComponent, AZ::Component>()->Version(0);
  39. }
  40. }
  41. InputAssemblyExampleComponent::InputAssemblyExampleComponent()
  42. {
  43. m_supportRHISamplePipeline = true;
  44. }
  45. void InputAssemblyExampleComponent::FrameBeginInternal(AZ::RHI::FrameGraphBuilder& frameGraphBuilder)
  46. {
  47. using namespace AZ;
  48. if (m_windowContext->GetSwapChain())
  49. {
  50. // Create Input Assembly buffer
  51. {
  52. RHI::TransientBufferDescriptor bufferDesc;
  53. bufferDesc.m_attachmentId = InputAssembly::InputAssemblyBufferAttachmentId;
  54. bufferDesc.m_bufferDescriptor = RHI::BufferDescriptor(
  55. RHI::BufferBindFlags::InputAssembly | RHI::BufferBindFlags::ShaderReadWrite,
  56. sizeof(BufferData));
  57. frameGraphBuilder.GetAttachmentDatabase().CreateTransientBuffer(bufferDesc);
  58. }
  59. {
  60. frameGraphBuilder.GetAttachmentDatabase().ImportBuffer(AZ::Name{ InputAssembly::ImportedInputAssemblyBufferAttachmentId }, m_inputAssemblyBuffer);
  61. }
  62. float aspectRatio = static_cast<float>(m_outputWidth / m_outputHeight);
  63. AZ::Vector2 scale(AZStd::min(1.0f / aspectRatio, 1.0f), AZStd::min(aspectRatio, 1.0f));
  64. {
  65. AZ::Matrix4x4 scaleTranslate =
  66. AZ::Matrix4x4::CreateTranslation(AZ::Vector3(0.4f, 0.4f, 0)) *
  67. AZ::Matrix4x4::CreateScale(AZ::Vector3(scale.GetX() * 0.6f, scale.GetY() * 0.6f, 1.0f));
  68. m_drawSRG[0]->SetConstant(m_drawMatrixIndex, scaleTranslate);
  69. m_drawSRG[0]->SetConstant(m_drawColorIndex, AZ::Vector4(1.0, 0, 0, 1.0f));
  70. m_drawSRG[0]->Compile();
  71. }
  72. {
  73. AZ::Matrix4x4 scaleTranslate =
  74. AZ::Matrix4x4::CreateTranslation(AZ::Vector3(-0.4f, -0.4f, 0)) *
  75. AZ::Matrix4x4::CreateScale(AZ::Vector3(scale.GetX() * 0.4f, scale.GetY() * 0.4f, 1.0f));
  76. m_drawSRG[1]->SetConstant(m_drawMatrixIndex, scaleTranslate);
  77. m_drawSRG[1]->SetConstant(m_drawColorIndex, AZ::Vector4(0.0, 1, 0, 1.0f));
  78. m_drawSRG[1]->Compile();
  79. }
  80. }
  81. }
  82. void InputAssemblyExampleComponent::OnTick(float deltaTime, AZ::ScriptTimePoint time)
  83. {
  84. AZ_UNUSED(time);
  85. m_time += deltaTime;
  86. }
  87. void InputAssemblyExampleComponent::Activate()
  88. {
  89. CreateInputAssemblyLayout();
  90. CreateBuffers();
  91. LoadComputeShader();
  92. LoadRasterShader();
  93. CreateComputeScope();
  94. CreateRasterScope();
  95. AZ::TickBus::Handler::BusConnect();
  96. AZ::RHI::RHISystemNotificationBus::Handler::BusConnect();
  97. }
  98. void InputAssemblyExampleComponent::Deactivate()
  99. {
  100. m_dispatchPipelineState = nullptr;
  101. m_drawPipelineState = nullptr;
  102. m_dispatchSRG[0] = nullptr;
  103. m_dispatchSRG[1] = nullptr;
  104. m_drawSRG[0] = nullptr;
  105. m_drawSRG[1] = nullptr;
  106. m_inputAssemblyBuffer = nullptr;
  107. m_inputAssemblyBufferPool = nullptr;
  108. m_scopeProducers.clear();
  109. m_windowContext = nullptr;
  110. AZ::TickBus::Handler::BusDisconnect();
  111. AZ::RHI::RHISystemNotificationBus::Handler::BusDisconnect();
  112. }
  113. void InputAssemblyExampleComponent::CreateInputAssemblyLayout()
  114. {
  115. using namespace AZ;
  116. RHI::Ptr<RHI::Device> device = Utils::GetRHIDevice();
  117. RHI::InputStreamLayoutBuilder layoutBuilder;
  118. layoutBuilder.AddBuffer()->Channel("POSITION", RHI::Format::R32G32B32A32_FLOAT);
  119. layoutBuilder.SetTopology(RHI::PrimitiveTopology::TriangleStrip);
  120. m_inputStreamLayout = layoutBuilder.End();
  121. }
  122. void InputAssemblyExampleComponent::CreateBuffers()
  123. {
  124. using namespace AZ;
  125. RHI::Ptr<RHI::Device> device = Utils::GetRHIDevice();
  126. m_inputAssemblyBufferPool = RHI::Factory::Get().CreateBufferPool();
  127. RHI::BufferPoolDescriptor bufferPoolDesc;
  128. bufferPoolDesc.m_bindFlags = RHI::BufferBindFlags::InputAssembly | RHI::BufferBindFlags::ShaderReadWrite;
  129. bufferPoolDesc.m_heapMemoryLevel = RHI::HeapMemoryLevel::Device;
  130. m_inputAssemblyBufferPool->Init(*device, bufferPoolDesc);
  131. m_inputAssemblyBuffer = RHI::Factory::Get().CreateBuffer();
  132. RHI::BufferInitRequest request;
  133. request.m_buffer = m_inputAssemblyBuffer.get();
  134. request.m_descriptor = RHI::BufferDescriptor{ RHI::BufferBindFlags::InputAssembly | RHI::BufferBindFlags::ShaderReadWrite, sizeof(BufferData) };
  135. request.m_initialData = nullptr;
  136. m_inputAssemblyBufferPool->InitBuffer(request);
  137. }
  138. void InputAssemblyExampleComponent::LoadComputeShader()
  139. {
  140. using namespace AZ;
  141. const char* shaderFilePath = "Shaders/RHI/InputAssemblyCompute.azshader";
  142. const auto shader = LoadShader(shaderFilePath, InputAssembly::SampleName);
  143. if (shader == nullptr)
  144. return;
  145. RHI::PipelineStateDescriptorForDispatch pipelineDesc;
  146. shader->GetVariant(RPI::ShaderAsset::RootShaderVariantStableId).ConfigurePipelineState(pipelineDesc);
  147. const auto& numThreads = shader->GetAsset()->GetAttribute(RHI::ShaderStage::Compute, Name("numthreads"));
  148. if (numThreads)
  149. {
  150. const RHI::ShaderStageAttributeArguments& args = *numThreads;
  151. m_numThreadsX = args[0].type() == azrtti_typeid<int>() ? AZStd::any_cast<int>(args[0]) : m_numThreadsX;
  152. m_numThreadsY = args[1].type() == azrtti_typeid<int>() ? AZStd::any_cast<int>(args[1]) : m_numThreadsY;
  153. m_numThreadsZ = args[2].type() == azrtti_typeid<int>() ? AZStd::any_cast<int>(args[2]) : m_numThreadsZ;
  154. }
  155. else
  156. {
  157. AZ_Error(InputAssembly::SampleName, false, "Did not find expected numthreads attribute");
  158. }
  159. m_dispatchPipelineState = shader->AcquirePipelineState(pipelineDesc);
  160. if (!m_dispatchPipelineState)
  161. {
  162. AZ_Error(InputAssembly::SampleName, false, "Failed to acquire default pipeline state for shader '%s'", shaderFilePath);
  163. return;
  164. }
  165. m_dispatchSRG[0] = CreateShaderResourceGroup(shader, "DispatchSRG", InputAssembly::SampleName);
  166. m_dispatchSRG[1] = CreateShaderResourceGroup(shader, "DispatchSRG", InputAssembly::SampleName);
  167. FindShaderInputIndex(&m_dispatchTimeConstantIndex, m_dispatchSRG[0], AZ::Name{ InputAssembly::ShaderInputTime }, InputAssembly::SampleName);
  168. FindShaderInputIndex(&m_dispatchIABufferIndex, m_dispatchSRG[0], AZ::Name{ InputAssembly::ShaderInpuIABuffer }, InputAssembly::SampleName);
  169. }
  170. void InputAssemblyExampleComponent::LoadRasterShader()
  171. {
  172. using namespace AZ;
  173. const char* shaderFilePath = "Shaders/RHI/InputAssemblyDraw.azshader";
  174. auto shader = LoadShader(shaderFilePath, InputAssembly::SampleName);
  175. if (shader == nullptr)
  176. return;
  177. RHI::PipelineStateDescriptorForDraw pipelineDesc;
  178. shader->GetVariant(RPI::ShaderAsset::RootShaderVariantStableId).ConfigurePipelineState(pipelineDesc);
  179. pipelineDesc.m_inputStreamLayout = m_inputStreamLayout;
  180. RHI::RenderAttachmentLayoutBuilder attachmentsBuilder;
  181. attachmentsBuilder.AddSubpass()
  182. ->RenderTargetAttachment(m_outputFormat);
  183. [[maybe_unused]] RHI::ResultCode result = attachmentsBuilder.End(pipelineDesc.m_renderAttachmentConfiguration.m_renderAttachmentLayout);
  184. AZ_Assert(result == RHI::ResultCode::Success, "Failed to create render attachment layout");
  185. m_drawPipelineState = shader->AcquirePipelineState(pipelineDesc);
  186. if (!m_drawPipelineState)
  187. {
  188. AZ_Error(InputAssembly::SampleName, false, "Failed to acquire default pipeline state for shader '%s'", shaderFilePath);
  189. return;
  190. }
  191. m_drawSRG[0] = CreateShaderResourceGroup(shader, "DrawSRG", InputAssembly::SampleName);
  192. m_drawSRG[1] = CreateShaderResourceGroup(shader, "DrawSRG", InputAssembly::SampleName);
  193. FindShaderInputIndex(&m_drawMatrixIndex, m_drawSRG[0], AZ::Name{ InputAssembly::ShaderInputMatrix }, InputAssembly::SampleName);
  194. FindShaderInputIndex(&m_drawColorIndex, m_drawSRG[0], AZ::Name{ InputAssembly::ShaderInputColor }, InputAssembly::SampleName);
  195. }
  196. void InputAssemblyExampleComponent::CreateComputeScope()
  197. {
  198. using namespace AZ;
  199. struct ScopeData
  200. {
  201. //UserDataParam - Empty for this samples
  202. };
  203. const auto prepareFunction = [](RHI::FrameGraphInterface frameGraph, ScopeData& scopeData)
  204. {
  205. AZ_UNUSED(scopeData);
  206. // Declare usage of the vertex buffer as UAV
  207. {
  208. RHI::BufferScopeAttachmentDescriptor attachmentDescriptor;
  209. attachmentDescriptor.m_attachmentId = InputAssembly::InputAssemblyBufferAttachmentId;
  210. attachmentDescriptor.m_bufferViewDescriptor = RHI::BufferViewDescriptor::CreateStructured(0, BufferData::array_size, sizeof(BufferData::value_type));
  211. attachmentDescriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::DontCare;
  212. frameGraph.UseShaderAttachment(attachmentDescriptor, RHI::ScopeAttachmentAccess::ReadWrite);
  213. }
  214. {
  215. RHI::BufferScopeAttachmentDescriptor attachmentDescriptor;
  216. attachmentDescriptor.m_attachmentId = InputAssembly::ImportedInputAssemblyBufferAttachmentId;
  217. attachmentDescriptor.m_bufferViewDescriptor = RHI::BufferViewDescriptor::CreateStructured(0, BufferData::array_size, sizeof(BufferData::value_type));
  218. attachmentDescriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::DontCare;
  219. frameGraph.UseShaderAttachment(attachmentDescriptor, RHI::ScopeAttachmentAccess::ReadWrite);
  220. }
  221. frameGraph.SetEstimatedItemCount(1);
  222. };
  223. const auto compileFunction = [this](const RHI::FrameGraphCompileContext& context, const ScopeData& scopeData)
  224. {
  225. AZ_UNUSED(scopeData);
  226. {
  227. const RHI::BufferView* inputAssemblyBufferView = context.GetBufferView(RHI::AttachmentId{ InputAssembly::InputAssemblyBufferAttachmentId });
  228. m_dispatchSRG[0]->SetBufferView(m_dispatchIABufferIndex, inputAssemblyBufferView);
  229. m_dispatchSRG[0]->SetConstant(m_dispatchTimeConstantIndex, m_time);
  230. m_dispatchSRG[0]->Compile();
  231. }
  232. {
  233. const RHI::BufferView* inputAssemblyBufferView = context.GetBufferView(RHI::AttachmentId{ InputAssembly::ImportedInputAssemblyBufferAttachmentId });
  234. m_dispatchSRG[1]->SetBufferView(m_dispatchIABufferIndex, inputAssemblyBufferView);
  235. m_dispatchSRG[1]->SetConstant(m_dispatchTimeConstantIndex, m_time);
  236. m_dispatchSRG[1]->Compile();
  237. }
  238. };
  239. const auto executeFunction = [this](const RHI::FrameGraphExecuteContext& context, const ScopeData& scopeData)
  240. {
  241. AZ_UNUSED(scopeData);
  242. RHI::CommandList* commandList = context.GetCommandList();
  243. RHI::DispatchItem dispatchItem;
  244. RHI::DispatchDirect dispatchArgs;
  245. dispatchArgs.m_threadsPerGroupX = aznumeric_cast<uint16_t>(m_numThreadsX);
  246. dispatchArgs.m_threadsPerGroupY = aznumeric_cast<uint16_t>(m_numThreadsY);
  247. dispatchArgs.m_threadsPerGroupZ = aznumeric_cast<uint16_t>(m_numThreadsZ);
  248. dispatchArgs.m_totalNumberOfThreadsX = 1;
  249. dispatchArgs.m_totalNumberOfThreadsY = 1;
  250. dispatchArgs.m_totalNumberOfThreadsZ = 1;
  251. dispatchItem.m_arguments = dispatchArgs;
  252. dispatchItem.m_pipelineState = m_dispatchPipelineState.get();
  253. dispatchItem.m_shaderResourceGroupCount = 1;
  254. dispatchItem.m_shaderResourceGroups[0] = m_dispatchSRG[0]->GetRHIShaderResourceGroup();
  255. commandList->Submit(dispatchItem);
  256. dispatchItem.m_shaderResourceGroups[0] = m_dispatchSRG[1]->GetRHIShaderResourceGroup();
  257. commandList->Submit(dispatchItem);
  258. };
  259. m_scopeProducers.emplace_back(
  260. aznew RHI::ScopeProducerFunction<
  261. ScopeData,
  262. decltype(prepareFunction),
  263. decltype(compileFunction),
  264. decltype(executeFunction)>(
  265. RHI::ScopeId{"IACompute"},
  266. ScopeData{},
  267. prepareFunction,
  268. compileFunction,
  269. executeFunction));
  270. }
  271. void InputAssemblyExampleComponent::CreateRasterScope()
  272. {
  273. using namespace AZ;
  274. struct ScopeData
  275. {
  276. };
  277. const auto prepareFunction = [this](RHI::FrameGraphInterface frameGraph, ScopeData& scopeData)
  278. {
  279. AZ_UNUSED(scopeData);
  280. // Binds the swap chain as a color attachment.
  281. {
  282. RHI::ImageScopeAttachmentDescriptor descriptor;
  283. descriptor.m_attachmentId = m_outputAttachmentId;
  284. descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Load;
  285. frameGraph.UseColorAttachment(descriptor);
  286. }
  287. // Declare the usage of the vertex buffer as Input Assembly. This is needed because we modify the vertex buffer in the GPU
  288. // and it needs synchronization.
  289. {
  290. RHI::BufferScopeAttachmentDescriptor attachmentDescriptor;
  291. attachmentDescriptor.m_attachmentId = InputAssembly::InputAssemblyBufferAttachmentId;
  292. attachmentDescriptor.m_bufferViewDescriptor = RHI::BufferViewDescriptor::CreateStructured(0, BufferData::array_size, sizeof(BufferData::value_type));
  293. attachmentDescriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Load;
  294. attachmentDescriptor.m_loadStoreAction.m_storeAction = RHI::AttachmentStoreAction::DontCare;
  295. frameGraph.UseInputAssemblyAttachment(attachmentDescriptor);
  296. }
  297. {
  298. RHI::BufferScopeAttachmentDescriptor attachmentDescriptor;
  299. attachmentDescriptor.m_attachmentId = InputAssembly::ImportedInputAssemblyBufferAttachmentId;
  300. attachmentDescriptor.m_bufferViewDescriptor = RHI::BufferViewDescriptor::CreateStructured(0, BufferData::array_size, sizeof(BufferData::value_type));
  301. attachmentDescriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Load;
  302. attachmentDescriptor.m_loadStoreAction.m_storeAction = RHI::AttachmentStoreAction::DontCare;
  303. frameGraph.UseInputAssemblyAttachment(attachmentDescriptor);
  304. }
  305. // We will submit a single draw item.
  306. frameGraph.SetEstimatedItemCount(1);
  307. };
  308. const auto compileFunction = [this](const RHI::FrameGraphCompileContext& context, const ScopeData& scopeData)
  309. {
  310. AZ_UNUSED(scopeData);
  311. {
  312. const RHI::BufferView* inputAssemblyBufferView = context.GetBufferView(RHI::AttachmentId{ InputAssembly::InputAssemblyBufferAttachmentId });
  313. if (inputAssemblyBufferView)
  314. {
  315. m_streamBufferView[0] = {inputAssemblyBufferView->GetBuffer(), 0, sizeof(BufferData), sizeof(BufferData::value_type)};
  316. RHI::ValidateStreamBufferViews(m_inputStreamLayout, AZStd::span<const RHI::StreamBufferView>(&m_streamBufferView[0], 1));
  317. }
  318. }
  319. {
  320. const RHI::BufferView* inputAssemblyBufferView = context.GetBufferView(RHI::AttachmentId{ InputAssembly::ImportedInputAssemblyBufferAttachmentId });
  321. if (inputAssemblyBufferView)
  322. {
  323. m_streamBufferView[1] = {inputAssemblyBufferView->GetBuffer(), 0, sizeof(BufferData), sizeof(BufferData::value_type)};
  324. RHI::ValidateStreamBufferViews(m_inputStreamLayout, AZStd::span<const RHI::StreamBufferView>(&m_streamBufferView[1], 1));
  325. }
  326. }
  327. };
  328. const auto executeFunction = [this](const RHI::FrameGraphExecuteContext& context, const ScopeData& scopeData)
  329. {
  330. AZ_UNUSED(scopeData);
  331. RHI::CommandList* commandList = context.GetCommandList();
  332. // Set persistent viewport and scissor state.
  333. commandList->SetViewports(&m_viewport, 1);
  334. commandList->SetScissors(&m_scissor, 1);
  335. RHI::DrawLinear drawLinear;
  336. drawLinear.m_vertexCount = BufferData::array_size;
  337. RHI::ShaderResourceGroup* rhiSRGS[] = { m_drawSRG[0]->GetRHIShaderResourceGroup() };
  338. RHI::DrawItem drawItem;
  339. drawItem.m_arguments = drawLinear;
  340. drawItem.m_pipelineState = m_drawPipelineState.get();
  341. drawItem.m_indexBufferView = nullptr;
  342. drawItem.m_streamBufferViewCount = 1;
  343. drawItem.m_streamBufferViews = &m_streamBufferView[0];
  344. drawItem.m_shaderResourceGroupCount = 1;
  345. drawItem.m_shaderResourceGroups = rhiSRGS;
  346. // Submit the draw item.
  347. commandList->Submit(drawItem);
  348. rhiSRGS[0] = { m_drawSRG[1]->GetRHIShaderResourceGroup() };
  349. drawItem.m_streamBufferViews = &m_streamBufferView[1];
  350. drawItem.m_shaderResourceGroups = rhiSRGS;
  351. commandList->Submit(drawItem);
  352. };
  353. m_scopeProducers.emplace_back(
  354. aznew RHI::ScopeProducerFunction<
  355. ScopeData,
  356. decltype(prepareFunction),
  357. decltype(compileFunction),
  358. decltype(executeFunction)>(
  359. RHI::ScopeId{"IARaster"},
  360. ScopeData{},
  361. prepareFunction,
  362. compileFunction,
  363. executeFunction));
  364. }
  365. }