MatrixAlignmentTestExampleComponent.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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_geometryView.SetDrawArguments(RHI::DrawIndexed(0, 6, 0));
  210. m_geometryView.SetIndexBufferView({
  211. *m_inputAssemblyBuffer,
  212. offsetof(BufferData, m_indices),
  213. sizeof(BufferData::m_indices),
  214. RHI::IndexFormat::Uint16
  215. });
  216. m_geometryView.AddStreamBufferView({
  217. *m_inputAssemblyBuffer,
  218. offsetof(BufferData, m_positions),
  219. sizeof(BufferData::m_positions),
  220. sizeof(VertexPosition)
  221. });
  222. m_geometryView.AddStreamBufferView({
  223. *m_inputAssemblyBuffer,
  224. offsetof(BufferData, m_colors),
  225. sizeof(BufferData::m_colors),
  226. sizeof(VertexColor)
  227. });
  228. RHI::InputStreamLayoutBuilder layoutBuilder;
  229. layoutBuilder.AddBuffer()->Channel("POSITION", RHI::Format::R32G32B32_FLOAT);
  230. layoutBuilder.AddBuffer()->Channel("COLOR", RHI::Format::R32G32B32A32_FLOAT);
  231. pipelineStateDescriptor.m_inputStreamLayout = layoutBuilder.End();
  232. RHI::ValidateStreamBufferViews(pipelineStateDescriptor.m_inputStreamLayout, m_geometryView, m_geometryView.GetFullStreamBufferIndices());
  233. }
  234. {
  235. const char* triangeShaderFilePath = "Shaders/RHI/MatrixAlignmentTest.azshader";
  236. const char* sampleName = "MatrixAlignmentTest";
  237. const AZ::Name supervariantName = (m_numFloatsAfterMatrix == 1) ? AZ::Name{""} : AZ::Name{"float2"};
  238. auto shader = LoadShader(triangeShaderFilePath, sampleName, &supervariantName);
  239. if (shader == nullptr)
  240. return;
  241. auto shaderVariant = shader->GetRootVariant();
  242. shaderVariant.ConfigurePipelineState(pipelineStateDescriptor);
  243. RHI::RenderAttachmentLayoutBuilder attachmentsBuilder;
  244. attachmentsBuilder.AddSubpass()
  245. ->RenderTargetAttachment(m_outputFormat);
  246. [[maybe_unused]] AZ::RHI::ResultCode result = attachmentsBuilder.End(pipelineStateDescriptor.m_renderAttachmentConfiguration.m_renderAttachmentLayout);
  247. AZ_Assert(result == AZ::RHI::ResultCode::Success, "Failed to create render attachment layout");
  248. m_pipelineState = shader->AcquirePipelineState(pipelineStateDescriptor);
  249. if (!m_pipelineState)
  250. {
  251. AZ_Error(sampleName, false, "Failed to acquire default pipeline state for shader '%s'", triangeShaderFilePath);
  252. return;
  253. }
  254. m_shaderResourceGroup = CreateShaderResourceGroup(shader, "AlignmentValidatorSrg", sampleName);
  255. const Name resolutionConstantId{ "m_resolution" };
  256. FindShaderInputIndex(&m_resolutionConstantIndex, m_shaderResourceGroup, resolutionConstantId, sampleName);
  257. const Name rowSelectionConstantId{ "m_numRows" };
  258. FindShaderInputIndex(&m_numRowsConstantIndex, m_shaderResourceGroup, rowSelectionConstantId, sampleName);
  259. const Name colSelectionConstantId{ "m_numColumns" };
  260. FindShaderInputIndex(&m_numColumnsConstantIndex, m_shaderResourceGroup, colSelectionConstantId, sampleName);
  261. {
  262. const Name MatrixName{ "m_matrix11" };
  263. FindShaderInputIndex(&m_matrix11ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  264. const Name floatAfterMatrixName{ "m_fAfter11" };
  265. FindShaderInputIndex(&m_fAfter11ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  266. }
  267. {
  268. const Name MatrixName{ "m_matrix12" };
  269. FindShaderInputIndex(&m_matrix12ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  270. const Name floatAfterMatrixName{ "m_fAfter12" };
  271. FindShaderInputIndex(&m_fAfter12ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  272. }
  273. {
  274. const Name MatrixName{ "m_matrix13" };
  275. FindShaderInputIndex(&m_matrix13ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  276. const Name floatAfterMatrixName{ "m_fAfter13" };
  277. FindShaderInputIndex(&m_fAfter13ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  278. }
  279. {
  280. const Name MatrixName{ "m_matrix14" };
  281. FindShaderInputIndex(&m_matrix14ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  282. const Name floatAfterMatrixName{ "m_fAfter14" };
  283. FindShaderInputIndex(&m_fAfter14ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  284. }
  285. // Not supported by AZSLc
  286. //{
  287. // const Name MatrixName{ "m_matrix21" };
  288. // FindShaderInputIndex(&m_matrix21ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  289. // const Name floatAfterMatrixName{ "m_fAfter21" };
  290. // FindShaderInputIndex(&m_fAfter21ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  291. //}
  292. {
  293. const Name MatrixName{ "m_matrix22" };
  294. FindShaderInputIndex(&m_matrix22ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  295. const Name floatAfterMatrixName{ "m_fAfter22" };
  296. FindShaderInputIndex(&m_fAfter22ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  297. }
  298. {
  299. const Name MatrixName{ "m_matrix23" };
  300. FindShaderInputIndex(&m_matrix23ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  301. const Name floatAfterMatrixName{ "m_fAfter23" };
  302. FindShaderInputIndex(&m_fAfter23ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  303. }
  304. {
  305. const Name MatrixName{ "m_matrix24" };
  306. FindShaderInputIndex(&m_matrix24ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  307. const Name floatAfterMatrixName{ "m_fAfter24" };
  308. FindShaderInputIndex(&m_fAfter24ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  309. }
  310. // Not supported by AZSLc
  311. //{
  312. // const Name MatrixName{ "m_matrix31" };
  313. // FindShaderInputIndex(&m_matrix31ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  314. // const Name floatAfterMatrixName{ "m_fAfter31" };
  315. // FindShaderInputIndex(&m_fAfter31ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  316. //}
  317. {
  318. const Name MatrixName{ "m_matrix32" };
  319. FindShaderInputIndex(&m_matrix32ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  320. const Name floatAfterMatrixName{ "m_fAfter32" };
  321. FindShaderInputIndex(&m_fAfter32ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  322. }
  323. {
  324. const Name MatrixName{ "m_matrix33" };
  325. FindShaderInputIndex(&m_matrix33ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  326. const Name floatAfterMatrixName{ "m_fAfter33" };
  327. FindShaderInputIndex(&m_fAfter33ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  328. }
  329. {
  330. const Name MatrixName{ "m_matrix34" };
  331. FindShaderInputIndex(&m_matrix34ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  332. const Name floatAfterMatrixName{ "m_fAfter34" };
  333. FindShaderInputIndex(&m_fAfter34ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  334. }
  335. // Not supported by AZSLc
  336. //{
  337. // const Name MatrixName{ "m_matrix41" };
  338. // FindShaderInputIndex(&m_matrix41ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  339. // const Name floatAfterMatrixName{ "m_fAfter41" };
  340. // FindShaderInputIndex(&m_fAfter41ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  341. //}
  342. {
  343. const Name MatrixName{ "m_matrix42" };
  344. FindShaderInputIndex(&m_matrix42ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  345. const Name floatAfterMatrixName{ "m_fAfter42" };
  346. FindShaderInputIndex(&m_fAfter42ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  347. }
  348. {
  349. const Name MatrixName{ "m_matrix43" };
  350. FindShaderInputIndex(&m_matrix43ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  351. const Name floatAfterMatrixName{ "m_fAfter43" };
  352. FindShaderInputIndex(&m_fAfter43ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  353. }
  354. {
  355. const Name MatrixName{ "m_matrix44" };
  356. FindShaderInputIndex(&m_matrix44ConstantIndex, m_shaderResourceGroup, MatrixName, sampleName);
  357. const Name floatAfterMatrixName{ "m_fAfter44" };
  358. FindShaderInputIndex(&m_fAfter44ConstantIndex, m_shaderResourceGroup, floatAfterMatrixName, sampleName);
  359. }
  360. }
  361. // Creates a scope for rendering the triangle.
  362. {
  363. struct ScopeData
  364. {
  365. };
  366. const auto prepareFunction = [this](RHI::FrameGraphInterface frameGraph, [[maybe_unused]] ScopeData& scopeData)
  367. {
  368. // Binds the swap chain as a color attachment. Clears it to white.
  369. {
  370. RHI::ImageScopeAttachmentDescriptor descriptor;
  371. descriptor.m_attachmentId = m_outputAttachmentId;
  372. descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Load;
  373. frameGraph.UseColorAttachment(descriptor);
  374. }
  375. // We will submit a single draw item.
  376. frameGraph.SetEstimatedItemCount(1);
  377. };
  378. RHI::EmptyCompileFunction<ScopeData> compileFunction;
  379. const auto executeFunction = [this](const RHI::FrameGraphExecuteContext& context, [[maybe_unused]] const ScopeData& scopeData)
  380. {
  381. RHI::CommandList* commandList = context.GetCommandList();
  382. // Set persistent viewport and scissor state.
  383. commandList->SetViewports(&m_viewport, 1);
  384. commandList->SetScissors(&m_scissor, 1);
  385. const RHI::DeviceShaderResourceGroup* shaderResourceGroups[] = {
  386. m_shaderResourceGroup->GetRHIShaderResourceGroup()->GetDeviceShaderResourceGroup(context.GetDeviceIndex()).get()
  387. };
  388. RHI::DeviceDrawItem drawItem;
  389. drawItem.m_drawInstanceArgs = RHI::DrawInstanceArguments(2, 0);
  390. drawItem.m_geometryView = m_geometryView.GetDeviceGeometryView(context.GetDeviceIndex());
  391. drawItem.m_streamIndices = m_geometryView.GetFullStreamBufferIndices();
  392. drawItem.m_pipelineState = m_pipelineState->GetDevicePipelineState(context.GetDeviceIndex()).get();
  393. drawItem.m_shaderResourceGroupCount = static_cast<uint8_t>(RHI::ArraySize(shaderResourceGroups));
  394. drawItem.m_shaderResourceGroups = shaderResourceGroups;
  395. // Submit the triangle draw item.
  396. commandList->Submit(drawItem);
  397. };
  398. m_scopeProducers.emplace_back(
  399. aznew RHI::ScopeProducerFunction<
  400. ScopeData,
  401. decltype(prepareFunction),
  402. decltype(compileFunction),
  403. decltype(executeFunction)>(
  404. RHI::ScopeId{"Triangle"},
  405. ScopeData{},
  406. prepareFunction,
  407. compileFunction,
  408. executeFunction));
  409. }
  410. m_needPipelineReload = false;
  411. }
  412. void MatrixAlignmentTestExampleComponent::Activate()
  413. {
  414. using namespace AZ;
  415. m_numRows = 4;
  416. m_numColumns = 4;
  417. m_matrixLocationValue = 1.0f;
  418. m_checkedMatrixValues[0][0] = true; m_checkedMatrixValues[0][1] = false; m_checkedMatrixValues[0][2] = true; m_checkedMatrixValues[0][3] = false;
  419. m_checkedMatrixValues[1][0] = false; m_checkedMatrixValues[1][1] = true; m_checkedMatrixValues[1][2] = false; m_checkedMatrixValues[1][3] = true;
  420. m_checkedMatrixValues[2][0] = true; m_checkedMatrixValues[2][1] = false; m_checkedMatrixValues[2][2] = true; m_checkedMatrixValues[2][3] = false;
  421. m_checkedMatrixValues[3][0] = false; m_checkedMatrixValues[3][1] = true; m_checkedMatrixValues[3][2] = false; m_checkedMatrixValues[3][3] = true;
  422. m_numFloatsAfterMatrix = 1;
  423. m_floatAfterMatrix = 0.5;
  424. m_needPipelineReload = true;
  425. InitializeRenderPipeline();
  426. m_imguiSidebar.Activate();
  427. AZ::RHI::RHISystemNotificationBus::Handler::BusConnect();
  428. AZ::TickBus::Handler::BusConnect();
  429. }
  430. void MatrixAlignmentTestExampleComponent::ReleaseRhiData()
  431. {
  432. m_inputAssemblyBuffer = nullptr;
  433. m_inputAssemblyBufferPool = nullptr;
  434. m_pipelineState = nullptr;
  435. m_shaderResourceGroup = nullptr;
  436. m_scopeProducers.clear();
  437. }
  438. void MatrixAlignmentTestExampleComponent::Deactivate()
  439. {
  440. m_inputAssemblyBuffer = nullptr;
  441. m_inputAssemblyBufferPool = nullptr;
  442. m_pipelineState = nullptr;
  443. m_shaderResourceGroup = nullptr;
  444. AZ::TickBus::Handler::BusDisconnect();
  445. AZ::RHI::RHISystemNotificationBus::Handler::BusDisconnect();
  446. m_imguiSidebar.Deactivate();
  447. m_windowContext = nullptr;
  448. m_scopeProducers.clear();
  449. }
  450. } // namespace AtomSampleViewer