MatrixAlignmentTestExampleComponent.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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/MatrixAlignmentTestExampleComponent.h>
  9. #include <Utils/Utils.h>
  10. #include <SampleComponentManager.h>
  11. #include <Atom/RHI/CommandList.h>
  12. #include <Atom/RHI.Reflect/InputStreamLayoutBuilder.h>
  13. #include <Atom/RHI.Reflect/RenderAttachmentLayoutBuilder.h>
  14. #include <Atom/RPI.Public/Shader/Shader.h>
  15. #include <Atom/RPI.Reflect/Shader/ShaderAsset.h>
  16. #include <AzCore/Serialization/SerializeContext.h>
  17. namespace AtomSampleViewer
  18. {
  19. void MatrixAlignmentTestExampleComponent::Reflect(AZ::ReflectContext* context)
  20. {
  21. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  22. {
  23. serializeContext->Class<MatrixAlignmentTestExampleComponent, AZ::Component>()
  24. ->Version(0)
  25. ;
  26. }
  27. }
  28. void MatrixAlignmentTestExampleComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint time)
  29. {
  30. bool numFloatsChanged = false;
  31. if (m_imguiSidebar.Begin())
  32. {
  33. ImGui::Spacing();
  34. ImGui::Text("Number Of Rows");
  35. ScriptableImGui::RadioButton("1##R1", &m_numRows, 1);
  36. ImGui::SameLine();
  37. ScriptableImGui::RadioButton("2##R2", &m_numRows, 2);
  38. ImGui::SameLine();
  39. ScriptableImGui::RadioButton("3##R3", &m_numRows, 3);
  40. ImGui::SameLine();
  41. ScriptableImGui::RadioButton("4##R4", &m_numRows, 4);
  42. ImGui::Spacing();
  43. ImGui::Text("Number Of Columns");
  44. ScriptableImGui::RadioButton("1##C1", &m_numColumns, 1);
  45. ImGui::SameLine();
  46. ScriptableImGui::RadioButton("2##C2", &m_numColumns, 2);
  47. ImGui::SameLine();
  48. ScriptableImGui::RadioButton("3##C3", &m_numColumns, 3);
  49. ImGui::SameLine();
  50. ScriptableImGui::RadioButton("4##C4", &m_numColumns, 4);
  51. ImGui::Spacing();
  52. ImGui::Text("Checked Location Value:");
  53. ScriptableImGui::SliderFloat("##CheckedLocationValue", &m_matrixLocationValue, 0.f, 1.f, "%.1f", ImGuiSliderFlags_AlwaysClamp);
  54. ImGui::Text("Unchecked Location Value: ");
  55. ImGui::Text("%.1f", 1.0f - m_matrixLocationValue);
  56. DrawMatrixValuesTable();
  57. ImGui::Spacing();
  58. ImGui::Text("Data After Matrix:");
  59. numFloatsChanged |= ScriptableImGui::RadioButton("float", &m_numFloatsAfterMatrix, 1);
  60. ImGui::SameLine();
  61. numFloatsChanged |= ScriptableImGui::RadioButton("float2", &m_numFloatsAfterMatrix, 2);
  62. ImGui::Text("float value:");
  63. ScriptableImGui::SliderFloat("##FloatAfterMatrix", &m_floatAfterMatrix, 0.f, 1.f, "%.1f", ImGuiSliderFlags_AlwaysClamp);
  64. m_imguiSidebar.End();
  65. }
  66. if (numFloatsChanged)
  67. {
  68. m_needPipelineReload = true;
  69. }
  70. }
  71. void MatrixAlignmentTestExampleComponent::DrawMatrixValuesTable()
  72. {
  73. const auto flags = ImGuiTableFlags_Borders;
  74. if (ImGui::BeginTable("Matrix Data", m_numColumns + 1, flags))
  75. {
  76. // Table header setup
  77. ImGui::TableSetupColumn("R\\C");
  78. for (int col = 0; col < m_numColumns; ++col)
  79. {
  80. AZStd::string colName = AZStd::string::format("C%d", col);
  81. ImGui::TableSetupColumn(colName.c_str());
  82. }
  83. ImGui::TableHeadersRow();
  84. for (int row = 0; row < m_numRows; ++row)
  85. {
  86. for (int col = 0; col < m_numColumns + 1; ++col)
  87. {
  88. ImGui::TableNextColumn();
  89. if (col == 0)
  90. {
  91. ImGui::Text("R%d", row);
  92. }
  93. else
  94. {
  95. AZStd::string cellId = AZStd::string::format("##R%dC%d", row, col-1);
  96. ScriptableImGui::Checkbox(cellId.c_str(), &m_checkedMatrixValues[row][col-1]);
  97. }
  98. }
  99. }
  100. ImGui::EndTable();
  101. }
  102. }
  103. void MatrixAlignmentTestExampleComponent::FrameBeginInternal([[maybe_unused]] AZ::RHI::FrameGraphBuilder& frameGraphBuilder)
  104. {
  105. if (m_needPipelineReload)
  106. {
  107. ReleaseRhiData();
  108. InitializeRenderPipeline();
  109. }
  110. }
  111. bool MatrixAlignmentTestExampleComponent::SetSrgMatrixData(int numRows, int numColumns,
  112. const AZ::RHI::ShaderInputConstantIndex& dataAfterMatrixConstantId,
  113. const AZ::RHI::ShaderInputConstantIndex& matrixConstantId)
  114. {
  115. if ((m_numRows != numRows) || (m_numColumns != numColumns))
  116. {
  117. return false;
  118. }
  119. if (!dataAfterMatrixConstantId.IsValid() || !matrixConstantId.IsValid())
  120. {
  121. return false;
  122. }
  123. // Always set the float first, this way if there are alignment issues We'll notice the unexpected
  124. // colors
  125. [[maybe_unused]] bool success = false;
  126. if (m_numFloatsAfterMatrix == 1)
  127. {
  128. success = m_shaderResourceGroup->SetConstant(dataAfterMatrixConstantId, m_floatAfterMatrix);
  129. }
  130. else
  131. {
  132. AZ::Vector2 float2(m_floatAfterMatrix, m_floatAfterMatrix);
  133. success = m_shaderResourceGroup->SetConstant(dataAfterMatrixConstantId, float2);
  134. }
  135. AZ_Warning("MatrixAlignmentTestExampleComponent", success, "Failed to set SRG Constant m_fAfter%d%d", numRows, numColumns);
  136. constexpr size_t SizeOfFloat4 = sizeof(float) * 4;
  137. constexpr size_t SizeOfFloat = sizeof(float);
  138. uint32_t numBytes = (SizeOfFloat4 * numRows) - ((4 - numColumns) * SizeOfFloat);
  139. success = m_shaderResourceGroup->SetConstantRaw(matrixConstantId, m_rawMatrix, numBytes);
  140. AZ_Warning("MatrixAlignmentTestExampleComponent", success, "Failed to set SRG Constant m_matrix%d%d", numRows, numColumns);
  141. return true;
  142. }
  143. void MatrixAlignmentTestExampleComponent::OnFramePrepare(AZ::RHI::FrameGraphBuilder& frameGraphBuilder)
  144. {
  145. using namespace AZ;
  146. {
  147. AZ::Vector4 screenResolution(GetViewportWidth(), GetViewportHeight(), 0, 0);
  148. [[maybe_unused]] bool success = m_shaderResourceGroup->SetConstant(m_resolutionConstantIndex, screenResolution);
  149. AZ_Warning("MatrixAlignmentTestExampleComponent", success, "Failed to set SRG Constant m_resolution");
  150. success = m_shaderResourceGroup->SetConstant(m_numRowsConstantIndex, m_numRows);
  151. AZ_Warning("MatrixAlignmentTestExampleComponent", success, "Failed to set SRG Constant m_numRows");
  152. success = m_shaderResourceGroup->SetConstant(m_numColumnsConstantIndex, m_numColumns);
  153. AZ_Warning("MatrixAlignmentTestExampleComponent", success, "Failed to set SRG Constant m_numColumns");
  154. for (int row = 0; row < m_numRows; ++row)
  155. {
  156. for (int col = 0; col < m_numColumns; ++col)
  157. {
  158. m_rawMatrix[row][col] = m_checkedMatrixValues[row][col] ? m_matrixLocationValue : 1.0f - m_matrixLocationValue;
  159. }
  160. }
  161. if ( SetSrgMatrixData(1, 1, m_fAfter11ConstantIndex, m_matrix11ConstantIndex)) {}
  162. else if ( SetSrgMatrixData(1, 2, m_fAfter12ConstantIndex, m_matrix12ConstantIndex)) {}
  163. else if ( SetSrgMatrixData(1, 3, m_fAfter13ConstantIndex, m_matrix13ConstantIndex)) {}
  164. else if ( SetSrgMatrixData(1, 4, m_fAfter14ConstantIndex, m_matrix14ConstantIndex)) {}
  165. //else if (SetSrgMatrixData(2, 1, m_fAfter21ConstantIndex, m_matrix21ConstantIndex)) {} //Not supported by AZSLc
  166. else if ( SetSrgMatrixData(2, 2, m_fAfter22ConstantIndex, m_matrix22ConstantIndex)) {}
  167. else if ( SetSrgMatrixData(2, 3, m_fAfter23ConstantIndex, m_matrix23ConstantIndex)) {}
  168. else if ( SetSrgMatrixData(2, 4, m_fAfter24ConstantIndex, m_matrix24ConstantIndex)) {}
  169. //else if (SetSrgMatrixData(3, 1, m_fAfter31ConstantIndex, m_matrix31ConstantIndex)) {} //Not supported by AZSLc
  170. else if ( SetSrgMatrixData(3, 2, m_fAfter32ConstantIndex, m_matrix32ConstantIndex)) {}
  171. else if ( SetSrgMatrixData(3, 3, m_fAfter33ConstantIndex, m_matrix33ConstantIndex)) {}
  172. else if ( SetSrgMatrixData(3, 4, m_fAfter34ConstantIndex, m_matrix34ConstantIndex)) {}
  173. //else if (SetSrgMatrixData(4, 1, m_fAfter41ConstantIndex, m_matrix41ConstantIndex)) {} //Not supported by AZSLc
  174. else if ( SetSrgMatrixData(4, 2, m_fAfter42ConstantIndex, m_matrix42ConstantIndex)) {}
  175. else if ( SetSrgMatrixData(4, 3, m_fAfter43ConstantIndex, m_matrix43ConstantIndex)) {}
  176. else if ( SetSrgMatrixData(4, 4, m_fAfter44ConstantIndex, m_matrix44ConstantIndex)) {}
  177. m_shaderResourceGroup->Compile();
  178. }
  179. BasicRHIComponent::OnFramePrepare(frameGraphBuilder);
  180. }
  181. MatrixAlignmentTestExampleComponent::MatrixAlignmentTestExampleComponent()
  182. {
  183. m_supportRHISamplePipeline = true;
  184. }
  185. void MatrixAlignmentTestExampleComponent::InitializeRenderPipeline()
  186. {
  187. using namespace AZ;
  188. RHI::Ptr<RHI::Device> device = Utils::GetRHIDevice();
  189. AZ::RHI::PipelineStateDescriptorForDraw pipelineStateDescriptor;
  190. {
  191. m_inputAssemblyBufferPool = aznew RHI::BufferPool();
  192. RHI::BufferPoolDescriptor bufferPoolDesc;
  193. bufferPoolDesc.m_bindFlags = RHI::BufferBindFlags::InputAssembly;
  194. bufferPoolDesc.m_heapMemoryLevel = RHI::HeapMemoryLevel::Device;
  195. m_inputAssemblyBufferPool->Init(bufferPoolDesc);
  196. BufferData bufferData;
  197. SetFullScreenRect(bufferData.m_positions.data(), nullptr, bufferData.m_indices.data());
  198. // All blue.
  199. SetVertexColor(bufferData.m_colors.data(), 0, 0.0, 0.0, 1.0, 1.0);
  200. SetVertexColor(bufferData.m_colors.data(), 1, 0.0, 0.0, 1.0, 1.0);
  201. SetVertexColor(bufferData.m_colors.data(), 2, 0.0, 0.0, 1.0, 1.0);
  202. SetVertexColor(bufferData.m_colors.data(), 3, 0.0, 0.0, 1.0, 1.0);
  203. m_inputAssemblyBuffer = aznew RHI::Buffer();
  204. RHI::BufferInitRequest request;
  205. request.m_buffer = m_inputAssemblyBuffer.get();
  206. request.m_descriptor = RHI::BufferDescriptor{ RHI::BufferBindFlags::InputAssembly, sizeof(bufferData) };
  207. request.m_initialData = &bufferData;
  208. m_inputAssemblyBufferPool->InitBuffer(request);
  209. m_streamBufferViews[0] =
  210. {
  211. *m_inputAssemblyBuffer,
  212. offsetof(BufferData, m_positions),
  213. sizeof(BufferData::m_positions),
  214. sizeof(VertexPosition)
  215. };
  216. m_streamBufferViews[1] =
  217. {
  218. *m_inputAssemblyBuffer,
  219. offsetof(BufferData, m_colors),
  220. sizeof(BufferData::m_colors),
  221. sizeof(VertexColor)
  222. };
  223. RHI::InputStreamLayoutBuilder layoutBuilder;
  224. layoutBuilder.AddBuffer()->Channel("POSITION", RHI::Format::R32G32B32_FLOAT);
  225. layoutBuilder.AddBuffer()->Channel("COLOR", RHI::Format::R32G32B32A32_FLOAT);
  226. pipelineStateDescriptor.m_inputStreamLayout = layoutBuilder.End();
  227. RHI::ValidateStreamBufferViews(pipelineStateDescriptor.m_inputStreamLayout, m_streamBufferViews);
  228. }
  229. {
  230. const char* triangeShaderFilePath = "Shaders/RHI/MatrixAlignmentTest.azshader";
  231. const char* sampleName = "MatrixAlignmentTest";
  232. const AZ::Name supervariantName = (m_numFloatsAfterMatrix == 1) ? AZ::Name{""} : AZ::Name{"float2"};
  233. auto shader = LoadShader(triangeShaderFilePath, sampleName, &supervariantName);
  234. if (shader == nullptr)
  235. return;
  236. auto shaderVariant = shader->GetRootVariant();
  237. shaderVariant.ConfigurePipelineState(pipelineStateDescriptor);
  238. RHI::RenderAttachmentLayoutBuilder attachmentsBuilder;
  239. attachmentsBuilder.AddSubpass()
  240. ->RenderTargetAttachment(m_outputFormat);
  241. [[maybe_unused]] AZ::RHI::ResultCode result = attachmentsBuilder.End(pipelineStateDescriptor.m_renderAttachmentConfiguration.m_renderAttachmentLayout);
  242. AZ_Assert(result == AZ::RHI::ResultCode::Success, "Failed to create render attachment layout");
  243. m_pipelineState = shader->AcquirePipelineState(pipelineStateDescriptor);
  244. if (!m_pipelineState)
  245. {
  246. AZ_Error(sampleName, false, "Failed to acquire default pipeline state for shader '%s'", triangeShaderFilePath);
  247. return;
  248. }
  249. m_shaderResourceGroup = CreateShaderResourceGroup(shader, "AlignmentValidatorSrg", sampleName);
  250. const Name resolutionConstantId{ "m_resolution" };
  251. FindShaderInputIndex(&m_resolutionConstantIndex, m_shaderResourceGroup, resolutionConstantId, sampleName);
  252. const Name rowSelectionConstantId{ "m_numRows" };
  253. FindShaderInputIndex(&m_numRowsConstantIndex, m_shaderResourceGroup, rowSelectionConstantId, sampleName);
  254. const Name colSelectionConstantId{ "m_numColumns" };
  255. FindShaderInputIndex(&m_numColumnsConstantIndex, m_shaderResourceGroup, colSelectionConstantId, sampleName);
  256. {
  257. const Name MatrixName{ "m_matrix11" };
  258. FindShaderInputIndex(&m_matrix11ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  259. const Name floatAfterMatrixName{ "m_fAfter11" };
  260. FindShaderInputIndex(&m_fAfter11ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  261. }
  262. {
  263. const Name MatrixName{ "m_matrix12" };
  264. FindShaderInputIndex(&m_matrix12ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  265. const Name floatAfterMatrixName{ "m_fAfter12" };
  266. FindShaderInputIndex(&m_fAfter12ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  267. }
  268. {
  269. const Name MatrixName{ "m_matrix13" };
  270. FindShaderInputIndex(&m_matrix13ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  271. const Name floatAfterMatrixName{ "m_fAfter13" };
  272. FindShaderInputIndex(&m_fAfter13ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  273. }
  274. {
  275. const Name MatrixName{ "m_matrix14" };
  276. FindShaderInputIndex(&m_matrix14ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  277. const Name floatAfterMatrixName{ "m_fAfter14" };
  278. FindShaderInputIndex(&m_fAfter14ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  279. }
  280. // Not supported by AZSLc
  281. //{
  282. // const Name MatrixName{ "m_matrix21" };
  283. // FindShaderInputIndex(&m_matrix21ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  284. // const Name floatAfterMatrixName{ "m_fAfter21" };
  285. // FindShaderInputIndex(&m_fAfter21ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  286. //}
  287. {
  288. const Name MatrixName{ "m_matrix22" };
  289. FindShaderInputIndex(&m_matrix22ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  290. const Name floatAfterMatrixName{ "m_fAfter22" };
  291. FindShaderInputIndex(&m_fAfter22ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  292. }
  293. {
  294. const Name MatrixName{ "m_matrix23" };
  295. FindShaderInputIndex(&m_matrix23ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  296. const Name floatAfterMatrixName{ "m_fAfter23" };
  297. FindShaderInputIndex(&m_fAfter23ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  298. }
  299. {
  300. const Name MatrixName{ "m_matrix24" };
  301. FindShaderInputIndex(&m_matrix24ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  302. const Name floatAfterMatrixName{ "m_fAfter24" };
  303. FindShaderInputIndex(&m_fAfter24ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  304. }
  305. // Not supported by AZSLc
  306. //{
  307. // const Name MatrixName{ "m_matrix31" };
  308. // FindShaderInputIndex(&m_matrix31ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  309. // const Name floatAfterMatrixName{ "m_fAfter31" };
  310. // FindShaderInputIndex(&m_fAfter31ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  311. //}
  312. {
  313. const Name MatrixName{ "m_matrix32" };
  314. FindShaderInputIndex(&m_matrix32ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  315. const Name floatAfterMatrixName{ "m_fAfter32" };
  316. FindShaderInputIndex(&m_fAfter32ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  317. }
  318. {
  319. const Name MatrixName{ "m_matrix33" };
  320. FindShaderInputIndex(&m_matrix33ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  321. const Name floatAfterMatrixName{ "m_fAfter33" };
  322. FindShaderInputIndex(&m_fAfter33ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  323. }
  324. {
  325. const Name MatrixName{ "m_matrix34" };
  326. FindShaderInputIndex(&m_matrix34ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  327. const Name floatAfterMatrixName{ "m_fAfter34" };
  328. FindShaderInputIndex(&m_fAfter34ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  329. }
  330. // Not supported by AZSLc
  331. //{
  332. // const Name MatrixName{ "m_matrix41" };
  333. // FindShaderInputIndex(&m_matrix41ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  334. // const Name floatAfterMatrixName{ "m_fAfter41" };
  335. // FindShaderInputIndex(&m_fAfter41ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  336. //}
  337. {
  338. const Name MatrixName{ "m_matrix42" };
  339. FindShaderInputIndex(&m_matrix42ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  340. const Name floatAfterMatrixName{ "m_fAfter42" };
  341. FindShaderInputIndex(&m_fAfter42ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  342. }
  343. {
  344. const Name MatrixName{ "m_matrix43" };
  345. FindShaderInputIndex(&m_matrix43ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  346. const Name floatAfterMatrixName{ "m_fAfter43" };
  347. FindShaderInputIndex(&m_fAfter43ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  348. }
  349. {
  350. const Name MatrixName{ "m_matrix44" };
  351. FindShaderInputIndex(&m_matrix44ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  352. const Name floatAfterMatrixName{ "m_fAfter44" };
  353. FindShaderInputIndex(&m_fAfter44ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  354. }
  355. }
  356. // Creates a scope for rendering the triangle.
  357. {
  358. struct ScopeData
  359. {
  360. };
  361. const auto prepareFunction = [this](RHI::FrameGraphInterface frameGraph, [[maybe_unused]] ScopeData& scopeData)
  362. {
  363. // Binds the swap chain as a color attachment. Clears it to white.
  364. {
  365. RHI::ImageScopeAttachmentDescriptor descriptor;
  366. descriptor.m_attachmentId = m_outputAttachmentId;
  367. descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Load;
  368. frameGraph.UseColorAttachment(descriptor);
  369. }
  370. // We will submit a single draw item.
  371. frameGraph.SetEstimatedItemCount(1);
  372. };
  373. RHI::EmptyCompileFunction<ScopeData> compileFunction;
  374. const auto executeFunction = [this](const RHI::FrameGraphExecuteContext& context, [[maybe_unused]] const ScopeData& scopeData)
  375. {
  376. RHI::CommandList* commandList = context.GetCommandList();
  377. // Set persistent viewport and scissor state.
  378. commandList->SetViewports(&m_viewport, 1);
  379. commandList->SetScissors(&m_scissor, 1);
  380. const RHI::DeviceIndexBufferView indexBufferView = { *m_inputAssemblyBuffer->GetDeviceBuffer(context.GetDeviceIndex()),
  381. offsetof(BufferData, m_indices), sizeof(BufferData::m_indices),
  382. RHI::IndexFormat::Uint16 };
  383. RHI::DrawIndexed drawIndexed;
  384. drawIndexed.m_indexCount = 6;
  385. drawIndexed.m_instanceCount = 2;
  386. const RHI::DeviceShaderResourceGroup* shaderResourceGroups[] = {
  387. m_shaderResourceGroup->GetRHIShaderResourceGroup()->GetDeviceShaderResourceGroup(context.GetDeviceIndex()).get()
  388. };
  389. RHI::DeviceDrawItem drawItem;
  390. drawItem.m_arguments = drawIndexed;
  391. drawItem.m_pipelineState = m_pipelineState->GetDevicePipelineState(context.GetDeviceIndex()).get();
  392. drawItem.m_indexBufferView = &indexBufferView;
  393. drawItem.m_shaderResourceGroupCount = static_cast<uint8_t>(RHI::ArraySize(shaderResourceGroups));
  394. drawItem.m_shaderResourceGroups = shaderResourceGroups;
  395. drawItem.m_streamBufferViewCount = static_cast<uint8_t>(m_streamBufferViews.size());
  396. AZStd::array<AZ::RHI::DeviceStreamBufferView, 2> deviceStreamBufferViews{
  397. m_streamBufferViews[0].GetDeviceStreamBufferView(context.GetDeviceIndex()),
  398. m_streamBufferViews[1].GetDeviceStreamBufferView(context.GetDeviceIndex())
  399. };
  400. drawItem.m_streamBufferViews = deviceStreamBufferViews.data();
  401. // Submit the triangle draw item.
  402. commandList->Submit(drawItem);
  403. };
  404. m_scopeProducers.emplace_back(
  405. aznew RHI::ScopeProducerFunction<
  406. ScopeData,
  407. decltype(prepareFunction),
  408. decltype(compileFunction),
  409. decltype(executeFunction)>(
  410. RHI::ScopeId{"Triangle"},
  411. ScopeData{},
  412. prepareFunction,
  413. compileFunction,
  414. executeFunction));
  415. }
  416. m_needPipelineReload = false;
  417. }
  418. void MatrixAlignmentTestExampleComponent::Activate()
  419. {
  420. using namespace AZ;
  421. m_numRows = 4;
  422. m_numColumns = 4;
  423. m_matrixLocationValue = 1.0f;
  424. m_checkedMatrixValues[0][0] = true; m_checkedMatrixValues[0][1] = false; m_checkedMatrixValues[0][2] = true; m_checkedMatrixValues[0][3] = false;
  425. m_checkedMatrixValues[1][0] = false; m_checkedMatrixValues[1][1] = true; m_checkedMatrixValues[1][2] = false; m_checkedMatrixValues[1][3] = true;
  426. m_checkedMatrixValues[2][0] = true; m_checkedMatrixValues[2][1] = false; m_checkedMatrixValues[2][2] = true; m_checkedMatrixValues[2][3] = false;
  427. m_checkedMatrixValues[3][0] = false; m_checkedMatrixValues[3][1] = true; m_checkedMatrixValues[3][2] = false; m_checkedMatrixValues[3][3] = true;
  428. m_numFloatsAfterMatrix = 1;
  429. m_floatAfterMatrix = 0.5;
  430. m_needPipelineReload = true;
  431. InitializeRenderPipeline();
  432. m_imguiSidebar.Activate();
  433. AZ::RHI::RHISystemNotificationBus::Handler::BusConnect();
  434. AZ::TickBus::Handler::BusConnect();
  435. }
  436. void MatrixAlignmentTestExampleComponent::ReleaseRhiData()
  437. {
  438. m_inputAssemblyBuffer = nullptr;
  439. m_inputAssemblyBufferPool = nullptr;
  440. m_pipelineState = nullptr;
  441. m_shaderResourceGroup = nullptr;
  442. m_scopeProducers.clear();
  443. }
  444. void MatrixAlignmentTestExampleComponent::Deactivate()
  445. {
  446. m_inputAssemblyBuffer = nullptr;
  447. m_inputAssemblyBufferPool = nullptr;
  448. m_pipelineState = nullptr;
  449. m_shaderResourceGroup = nullptr;
  450. AZ::TickBus::Handler::BusDisconnect();
  451. AZ::RHI::RHISystemNotificationBus::Handler::BusDisconnect();
  452. m_imguiSidebar.Deactivate();
  453. m_windowContext = nullptr;
  454. m_scopeProducers.clear();
  455. }
  456. } // namespace AtomSampleViewer