DynamicDrawExampleComponent.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 <DynamicDrawExampleComponent.h>
  9. #include <SampleComponentManager.h>
  10. #include <Automation/ScriptableImGui.h>
  11. #include <Automation/ScriptRunnerBus.h>
  12. #include <Atom/Component/DebugCamera/NoClipControllerComponent.h>
  13. #include <Atom/RPI.Public/DynamicDraw/DynamicDrawInterface.h>
  14. #include <Atom/RPI.Public/Pass/PassFilter.h>
  15. #include <Atom/RPI.Public/Pass/RasterPass.h>
  16. #include <Atom/RPI.Public/RPIUtils.h>
  17. namespace AtomSampleViewer
  18. {
  19. void DynamicDrawExampleComponent::Reflect(AZ::ReflectContext* context)
  20. {
  21. if (AZ::SerializeContext * serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  22. {
  23. serializeContext->Class<DynamicDrawExampleComponent, AZ::Component>()
  24. ->Version(0)
  25. ;
  26. }
  27. }
  28. DynamicDrawExampleComponent::DynamicDrawExampleComponent()
  29. {
  30. m_sampleName = "DynamicDrawExampleComponent";
  31. }
  32. void DynamicDrawExampleComponent::Activate()
  33. {
  34. using namespace AZ;
  35. // List of all assets this example needs.
  36. AZStd::vector<AZ::AssetCollectionAsyncLoader::AssetToLoadInfo> assetList = {
  37. {"Shaders/dynamicdraw/dynamicdrawexample.azshader", azrtti_typeid<AZ::RPI::ShaderAsset>()},
  38. };
  39. ScriptRunnerRequestBus::Broadcast(&ScriptRunnerRequests::PauseScript);
  40. PreloadAssets(assetList);
  41. }
  42. void DynamicDrawExampleComponent::OnAllAssetsReadyActivate()
  43. {
  44. AZ::TickBus::Handler::BusConnect();
  45. m_imguiSidebar.Activate();
  46. using namespace AZ;
  47. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Enable,
  48. azrtti_typeid<AZ::Debug::NoClipControllerComponent>());
  49. AZ::Debug::NoClipControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::NoClipControllerRequestBus::Events::SetPosition, Vector3(-0.11f, -3.01f, -0.02f));
  50. AZ::Debug::NoClipControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::NoClipControllerRequestBus::Events::SetHeading, DegToRad(-4.0f));
  51. AZ::Debug::NoClipControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::NoClipControllerRequestBus::Events::SetPitch, DegToRad(1.9f));
  52. AZStd::vector<RPI::DynamicDrawContext::VertexChannel> vertexChannels =
  53. {
  54. { "POSITION", RHI::Format::R32G32B32_FLOAT },
  55. { "COLOR", RHI::Format::R32G32B32A32_FLOAT }
  56. };
  57. // Create and initialize dynamic draw context
  58. m_dynamicDraw = RPI::DynamicDrawInterface::Get()->CreateDynamicDrawContext();
  59. const char* shaderFilepath = "Shaders/dynamicdraw/dynamicdrawexample.azshader";
  60. Data::Asset<RPI::ShaderAsset> shaderAsset = m_assetLoadManager.GetAsset<RPI::ShaderAsset>(shaderFilepath);
  61. m_dynamicDraw->InitShader(shaderAsset);
  62. m_dynamicDraw->InitVertexFormat(vertexChannels);
  63. m_dynamicDraw->AddDrawStateOptions(RPI::DynamicDrawContext::DrawStateOptions::BlendMode | RPI::DynamicDrawContext::DrawStateOptions::PrimitiveType
  64. | RPI::DynamicDrawContext::DrawStateOptions::DepthState | RPI::DynamicDrawContext::DrawStateOptions::FaceCullMode);
  65. m_dynamicDraw->SetOutputScope(RPI::RPISystemInterface::Get()->GetDefaultScene().get());
  66. m_dynamicDraw->EndInit();
  67. Data::Instance<RPI::ShaderResourceGroup> contextSrg = m_dynamicDraw->GetPerContextSrg();
  68. if (contextSrg)
  69. {
  70. auto index = contextSrg->FindShaderInputConstantIndex(Name("m_scale"));
  71. contextSrg->SetConstant(index, 1);
  72. contextSrg->Compile();
  73. }
  74. AZ_Assert(m_dynamicDraw->IsVertexSizeValid(sizeof(ExampleVertex)), "Invalid vertex format");
  75. // Dynamic draw for pass
  76. m_dynamicDraw1ForPass = RPI::DynamicDrawInterface::Get()->CreateDynamicDrawContext();
  77. m_dynamicDraw2ForPass = RPI::DynamicDrawInterface::Get()->CreateDynamicDrawContext();
  78. // Get auxGeom pass from the current render pipeline
  79. AZ::RPI::PassFilter passFilter = AZ::RPI::PassFilter::CreateWithPassHierarchy({"MainPipeline", "AuxGeomPass"});
  80. RPI::RasterPass* auxGeomPass = azrtti_cast<RPI::RasterPass*>(AZ::RPI::PassSystemInterface::Get()->FindFirstPass(passFilter));
  81. AZ_Assert(auxGeomPass, "AuxGeomPass should be a RasterPass or a derived RasterPass");
  82. m_dynamicDraw1ForPass->InitShader(shaderAsset);
  83. m_dynamicDraw1ForPass->InitVertexFormat(vertexChannels);
  84. m_dynamicDraw1ForPass->AddDrawStateOptions(RPI::DynamicDrawContext::DrawStateOptions::BlendMode
  85. | RPI::DynamicDrawContext::DrawStateOptions::DepthState);
  86. m_dynamicDraw1ForPass->SetOutputScope(auxGeomPass);
  87. m_dynamicDraw1ForPass->EndInit();
  88. m_dynamicDraw2ForPass->InitShader(shaderAsset);
  89. m_dynamicDraw2ForPass->InitVertexFormat(vertexChannels);
  90. m_dynamicDraw2ForPass->AddDrawStateOptions(RPI::DynamicDrawContext::DrawStateOptions::BlendMode
  91. | RPI::DynamicDrawContext::DrawStateOptions::DepthState);
  92. m_dynamicDraw2ForPass->SetOutputScope(auxGeomPass);
  93. m_dynamicDraw2ForPass->EndInit();
  94. ScriptRunnerRequestBus::Broadcast(&ScriptRunnerRequests::ResumeScript);
  95. }
  96. void DynamicDrawExampleComponent::Deactivate()
  97. {
  98. using namespace AZ;
  99. m_imguiSidebar.Deactivate();
  100. TickBus::Handler::BusDisconnect();
  101. AZ::Debug::CameraControllerRequestBus::Event(GetCameraEntityId(), &AZ::Debug::CameraControllerRequestBus::Events::Disable);
  102. m_dynamicDraw = nullptr;
  103. m_contextSrg = nullptr;
  104. m_dynamicDraw1ForPass = nullptr;
  105. m_dynamicDraw2ForPass = nullptr;
  106. }
  107. void DynamicDrawExampleComponent::OnTick([[maybe_unused]] float deltaTime, [[maybe_unused]] AZ::ScriptTimePoint timePoint)
  108. {
  109. using namespace AZ;
  110. if (m_imguiSidebar.Begin())
  111. {
  112. ScriptableImGui::Checkbox("CullMode::None", &m_showCullModeNull);
  113. ScriptableImGui::Checkbox("CullMode: Front", &m_showCullModeFront);
  114. ScriptableImGui::Checkbox("CullMode: Back", &m_showCullModeBack);
  115. ScriptableImGui::Checkbox("PrimitiveTopology: LineList", &m_showLineList);
  116. ScriptableImGui::Checkbox("Alpha Blend", &m_showAlphaBlend);
  117. ScriptableImGui::Checkbox("Alpha Additive", &m_showAlphaAdditive);
  118. ScriptableImGui::Checkbox("Per Draw Viewport", &m_showPerDrawViewport);
  119. ScriptableImGui::Checkbox("Sorting", &m_showSorting);
  120. m_imguiSidebar.End();
  121. }
  122. // draw srg with default offset
  123. Data::Instance<RPI::ShaderResourceGroup> drawSrg = m_dynamicDraw->NewDrawSrg();
  124. auto index = drawSrg->FindShaderInputConstantIndex(Name("m_positionOffset"));
  125. drawSrg->SetConstant(index, Vector3(0, 0, 0));
  126. drawSrg->Compile();
  127. // Tetrahedron
  128. const uint32_t TetrahedronVertexCount = 12;
  129. const uint32_t TetrahedronWireframeIndexCount = 12;
  130. float positions[4][3] =
  131. {
  132. { 0.2, 0.2f, 0.2f },
  133. { 0.2, -0.2f, -0.2f },
  134. { -0.2, 0.2f, -0.2f },
  135. { -0.2, -0.2f, 0.2f }
  136. };
  137. float colors[4][4] = { { 1, 0, 0, 0.5f }, { 0, 1, 0, 0.5f }, { 0, 0, 1, 0.5f }, {1, 1, 0, 0.5f}};
  138. ExampleVertex tetrahedronVerts[TetrahedronVertexCount] = {
  139. ExampleVertex{positions[0], colors[0]}, // 0
  140. ExampleVertex{positions[3], colors[0]}, // 3
  141. ExampleVertex{positions[1], colors[0]}, // 1
  142. ExampleVertex{positions[0], colors[1]}, // 0
  143. ExampleVertex{positions[1], colors[1]}, // 1
  144. ExampleVertex{positions[2], colors[1]}, // 2
  145. ExampleVertex{positions[0], colors[2]}, // 0
  146. ExampleVertex{positions[2], colors[2]}, // 2
  147. ExampleVertex{positions[3], colors[2]}, // 3
  148. ExampleVertex{positions[1], colors[3]}, // 1
  149. ExampleVertex{positions[3], colors[3]}, // 3
  150. ExampleVertex{positions[2], colors[3]} // 2
  151. };
  152. ExampleVertex tetrahedronWireFrameVerts[4] = {
  153. ExampleVertex{ positions[0], colors[0] },
  154. ExampleVertex{ positions[1], colors[1] },
  155. ExampleVertex{ positions[2], colors[2] },
  156. ExampleVertex{ positions[3], colors[3] }
  157. };
  158. u16 tetrahedronWireframeIndices[TetrahedronWireframeIndexCount] =
  159. {
  160. 0, 1,
  161. 0, 2,
  162. 0, 3,
  163. 1, 2,
  164. 2, 3,
  165. 3, 1
  166. };
  167. // Enable depth test and write
  168. RHI::DepthState depthState;
  169. depthState.m_enable = true;
  170. depthState.m_writeMask = RHI::DepthWriteMask::All;
  171. depthState.m_func = RHI::ComparisonFunc::GreaterEqual;
  172. m_dynamicDraw->SetDepthState(depthState);
  173. // Disable blend
  174. RHI::TargetBlendState blendState;
  175. blendState.m_enable = false;
  176. m_dynamicDraw->SetTarget0BlendState(blendState);
  177. float xPos = -1.5f;
  178. const float xOffset = 0.5f;
  179. // no cull
  180. if (m_showCullModeNull)
  181. {
  182. m_dynamicDraw->SetCullMode(RHI::CullMode::None);
  183. drawSrg = m_dynamicDraw->NewDrawSrg();
  184. drawSrg->SetConstant(index, Vector3(xPos, 0, 0));
  185. drawSrg->Compile();
  186. m_dynamicDraw->DrawLinear(tetrahedronVerts, TetrahedronVertexCount, drawSrg);
  187. }
  188. //front cull
  189. xPos += xOffset;
  190. if (m_showCullModeFront)
  191. {
  192. m_dynamicDraw->SetCullMode(RHI::CullMode::Front);
  193. drawSrg = m_dynamicDraw->NewDrawSrg();
  194. drawSrg->SetConstant(index, Vector3(xPos, 0, 0));
  195. drawSrg->Compile();
  196. m_dynamicDraw->DrawLinear(tetrahedronVerts, TetrahedronVertexCount, drawSrg);
  197. m_dynamicDraw->SetCullMode(RHI::CullMode::None);
  198. }
  199. // back cull
  200. xPos += xOffset;
  201. if (m_showCullModeBack)
  202. {
  203. m_dynamicDraw->SetCullMode(RHI::CullMode::Back);
  204. drawSrg = m_dynamicDraw->NewDrawSrg();
  205. drawSrg->SetConstant(index, Vector3(xPos, 0, 0));
  206. drawSrg->Compile();
  207. m_dynamicDraw->DrawLinear(tetrahedronVerts, TetrahedronVertexCount, drawSrg);
  208. m_dynamicDraw->SetCullMode(RHI::CullMode::None);
  209. }
  210. // Draw line lists
  211. xPos += xOffset;
  212. if (m_showLineList)
  213. {
  214. drawSrg = m_dynamicDraw->NewDrawSrg();
  215. drawSrg->SetConstant(index, Vector3(xPos, 0, 0));
  216. drawSrg->Compile();
  217. m_dynamicDraw->SetPrimitiveType(RHI::PrimitiveTopology::LineList);
  218. m_dynamicDraw->DrawIndexed(tetrahedronWireFrameVerts, 4, tetrahedronWireframeIndices, TetrahedronWireframeIndexCount, RHI::IndexFormat::Uint16, drawSrg);
  219. m_dynamicDraw->SetPrimitiveType(RHI::PrimitiveTopology::TriangleList);
  220. }
  221. // disable depth write
  222. depthState.m_writeMask = RHI::DepthWriteMask::Zero;
  223. m_dynamicDraw->SetDepthState(depthState);
  224. // alpha blend
  225. xPos += xOffset;
  226. if (m_showAlphaBlend)
  227. {
  228. blendState.m_enable = true;
  229. blendState.m_blendOp = RHI::BlendOp::Add;
  230. blendState.m_blendSource = RHI::BlendFactor::AlphaSource;
  231. blendState.m_blendDest = RHI::BlendFactor::AlphaSourceInverse;
  232. m_dynamicDraw->SetTarget0BlendState(blendState);
  233. drawSrg = m_dynamicDraw->NewDrawSrg();
  234. drawSrg->SetConstant(index, Vector3(xPos, 0, 0));
  235. drawSrg->Compile();
  236. m_dynamicDraw->DrawLinear(tetrahedronVerts, TetrahedronVertexCount, drawSrg);
  237. }
  238. // alpha additive
  239. xPos += xOffset;
  240. if (m_showAlphaAdditive)
  241. {
  242. blendState.m_enable = true;
  243. blendState.m_blendOp = RHI::BlendOp::Add;
  244. blendState.m_blendSource = RHI::BlendFactor::AlphaSource;
  245. blendState.m_blendDest = RHI::BlendFactor::One;
  246. m_dynamicDraw->SetTarget0BlendState(blendState);
  247. drawSrg = m_dynamicDraw->NewDrawSrg();
  248. drawSrg->SetConstant(index, Vector3(xPos, 0, 0));
  249. drawSrg->Compile();
  250. m_dynamicDraw->DrawLinear(tetrahedronVerts, TetrahedronVertexCount, drawSrg);
  251. }
  252. // enable depth write
  253. depthState.m_writeMask = RHI::DepthWriteMask::All;
  254. m_dynamicDraw->SetDepthState(depthState);
  255. // disable blend
  256. blendState.m_enable = false;
  257. m_dynamicDraw->SetTarget0BlendState(blendState);
  258. // per draw viewport
  259. xPos += xOffset;
  260. if (m_showPerDrawViewport)
  261. {
  262. m_dynamicDraw->SetViewport(RHI::Viewport(0, 200, 0, 200));
  263. drawSrg = m_dynamicDraw->NewDrawSrg();
  264. drawSrg->SetConstant(index, Vector3(0, 0, 0));
  265. drawSrg->Compile();
  266. m_dynamicDraw->DrawLinear(tetrahedronVerts, TetrahedronVertexCount, drawSrg);
  267. m_dynamicDraw->UnsetViewport();
  268. }
  269. // show draws from three different dynamic draw context with correct sorting
  270. if (m_showSorting)
  271. {
  272. float black[4] = {0, 0, 0, 1};
  273. float white[4] = {1, 1, 1, 1};
  274. float red[4] = {1, 0, 0, 1};
  275. float vertexPos[4][3] =
  276. {
  277. { -0.2, 0, 0.2f},
  278. { -0.2, 0, -0.2f},
  279. { 0.2, 0 , -0.2f},
  280. { 0.2, 0 , 0.2f}
  281. };
  282. ExampleVertex blackQuad[4] =
  283. {
  284. ExampleVertex{ vertexPos[0], black },
  285. ExampleVertex{ vertexPos[1], black },
  286. ExampleVertex{ vertexPos[2], black },
  287. ExampleVertex{ vertexPos[3], black }
  288. };
  289. ExampleVertex whiteQuad[4] =
  290. {
  291. ExampleVertex{ vertexPos[0], white },
  292. ExampleVertex{ vertexPos[1], white },
  293. ExampleVertex{ vertexPos[2], white },
  294. ExampleVertex{ vertexPos[3], white }
  295. };
  296. ExampleVertex redQuad[4] =
  297. {
  298. ExampleVertex{ vertexPos[0], red },
  299. ExampleVertex{ vertexPos[1], red },
  300. ExampleVertex{ vertexPos[2], red },
  301. ExampleVertex{ vertexPos[3], red }
  302. };
  303. u16 quadIndics[6] =
  304. {
  305. 0, 1, 2,
  306. 0, 2, 3
  307. };
  308. // disable depth for all and rely on sorting
  309. depthState.m_writeMask = RHI::DepthWriteMask::Zero;
  310. m_dynamicDraw->SetDepthState(depthState);
  311. m_dynamicDraw1ForPass->SetDepthState(depthState);
  312. m_dynamicDraw2ForPass->SetDepthState(depthState);
  313. blendState.m_enable = false;
  314. m_dynamicDraw->SetTarget0BlendState(blendState);
  315. m_dynamicDraw1ForPass->SetTarget0BlendState(blendState);
  316. m_dynamicDraw2ForPass->SetTarget0BlendState(blendState);
  317. // draw two red quads via view
  318. drawSrg = m_dynamicDraw->NewDrawSrg();
  319. drawSrg->SetConstant(index, Vector3(-0.3f, 0, 0.7f));
  320. drawSrg->Compile();
  321. m_dynamicDraw->SetSortKey(0x200);
  322. m_dynamicDraw->DrawIndexed(redQuad, 4, quadIndics, 6, RHI::IndexFormat::Uint16, drawSrg);
  323. drawSrg = m_dynamicDraw->NewDrawSrg();
  324. drawSrg->SetConstant(index, Vector3(0.3f, 0, 0.7f));
  325. drawSrg->Compile();
  326. m_dynamicDraw->SetSortKey(0);
  327. m_dynamicDraw->DrawIndexed(redQuad, 4, quadIndics, 6, RHI::IndexFormat::Uint16, drawSrg);
  328. // draw two white quads via ddc1
  329. drawSrg = m_dynamicDraw1ForPass->NewDrawSrg();
  330. drawSrg->SetConstant(index, Vector3(-0.3f, 0, 0.8f));
  331. drawSrg->Compile();
  332. m_dynamicDraw1ForPass->SetSortKey(0x100);
  333. m_dynamicDraw1ForPass->DrawIndexed(whiteQuad, 4, quadIndics, 6, RHI::IndexFormat::Uint16, drawSrg);
  334. drawSrg = m_dynamicDraw1ForPass->NewDrawSrg();
  335. drawSrg->SetConstant(index, Vector3(0.3f, 0, 0.8f));
  336. drawSrg->Compile();
  337. m_dynamicDraw1ForPass->SetSortKey(0x100);
  338. m_dynamicDraw1ForPass->DrawIndexed(whiteQuad, 4, quadIndics, 6, RHI::IndexFormat::Uint16, drawSrg);
  339. // draw two black quads via dcc2
  340. drawSrg = m_dynamicDraw2ForPass->NewDrawSrg();
  341. drawSrg->SetConstant(index, Vector3(-0.3f, 0, 0.9f));
  342. drawSrg->Compile();
  343. m_dynamicDraw2ForPass->SetSortKey(0);
  344. m_dynamicDraw2ForPass->DrawIndexed(blackQuad, 4, quadIndics, 6, RHI::IndexFormat::Uint16, drawSrg);
  345. drawSrg = m_dynamicDraw2ForPass->NewDrawSrg();
  346. drawSrg->SetConstant(index, Vector3(0.3f, 0, 0.9f));
  347. drawSrg->Compile();
  348. m_dynamicDraw2ForPass->SetSortKey(0x200);
  349. m_dynamicDraw2ForPass->DrawIndexed(blackQuad, 4, quadIndics, 6, RHI::IndexFormat::Uint16, drawSrg);
  350. }
  351. }
  352. } // namespace AtomSampleViewer