2
0

MSAAExampleComponent.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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/RPI.Public/Shader/Shader.h>
  10. #include <Atom/RHI.Reflect/InputStreamLayoutBuilder.h>
  11. #include <Atom/RHI.Reflect/RenderAttachmentLayoutBuilder.h>
  12. #include <Atom/RPI.Reflect/Shader/ShaderAsset.h>
  13. #include <AzFramework/Input/Devices/Mouse/InputDeviceMouse.h>
  14. #include <AzCore/Serialization/SerializeContext.h>
  15. #include <AzCore/std/createdestroy.h>
  16. #include <RHI/MSAAExampleComponent.h>
  17. #include <SampleComponentManager.h>
  18. #include <Utils/Utils.h>
  19. namespace AtomSampleViewer
  20. {
  21. namespace
  22. {
  23. const char* TriangeShaderFilePath = "Shaders/RHI/triangle.azshader";
  24. const char* CustomResolveShaderFilePath = "Shaders/RHI/MSAAResolve.azshader";
  25. const char* SampleName = "MSAAExample";
  26. }
  27. void MSAAExampleComponent::Reflect(AZ::ReflectContext* context)
  28. {
  29. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  30. {
  31. serializeContext->Class<MSAAExampleComponent, AZ::Component>()
  32. ->Version(0)
  33. ;
  34. }
  35. }
  36. MSAAExampleComponent::MSAAExampleComponent()
  37. {
  38. m_supportRHISamplePipeline = true;
  39. }
  40. void MSAAExampleComponent::Activate()
  41. {
  42. using namespace AZ;
  43. RHI::Ptr<RHI::Device> device = Utils::GetRHIDevice();
  44. m_inputAssemblyBufferPool = aznew RHI::BufferPool();
  45. RHI::BufferPoolDescriptor bufferPoolDesc;
  46. bufferPoolDesc.m_bindFlags = RHI::BufferBindFlags::InputAssembly;
  47. bufferPoolDesc.m_heapMemoryLevel = RHI::HeapMemoryLevel::Device;
  48. m_inputAssemblyBufferPool->Init(bufferPoolDesc);
  49. AZStd::vector<RHI::SamplePosition> emptySamplePositions;
  50. AZStd::vector<RHI::SamplePosition> customSamplePositions = { RHI::SamplePosition(3, 3), RHI::SamplePosition(11, 3), RHI::SamplePosition(3, 11), RHI::SamplePosition(11, 11) };
  51. m_sampleProperties[static_cast<uint32_t>(MSAAType::MSAA2X)] = { 2, true, emptySamplePositions, RHI::AttachmentStoreAction::DontCare, RHI::AttachmentId("MSAA2X"), true };
  52. m_sampleProperties[static_cast<uint32_t>(MSAAType::MSAA4X)] = { 4, true, emptySamplePositions, RHI::AttachmentStoreAction::DontCare, RHI::AttachmentId("MSAA4X"), true };
  53. m_sampleProperties[static_cast<uint32_t>(MSAAType::MSAA4X_Custom_Positions)] = { 4, true, customSamplePositions, RHI::AttachmentStoreAction::DontCare, RHI::AttachmentId("MSAA4X_Custom_Positions"), true };
  54. m_sampleProperties[static_cast<uint32_t>(MSAAType::MSAA4X_Custom_Resolve)] = { 4, false, emptySamplePositions, RHI::AttachmentStoreAction::Store, RHI::AttachmentId("MSAA4X_Custom_Resolve"), true };
  55. m_sampleProperties[static_cast<uint32_t>(MSAAType::NoAA)] = { 1, false, emptySamplePositions, RHI::AttachmentStoreAction::Store, m_outputAttachmentId, false };
  56. CreateTriangleResources();
  57. CreateQuadResources();
  58. for (uint32_t i = 0; i < s_numMSAAExamples; ++i)
  59. {
  60. auto msaaType = static_cast<MSAAType>(i);
  61. CreateScopeResources(msaaType);
  62. CreateScopeProducer(msaaType);
  63. }
  64. // Extra scopes for resolving the MSAA texture using a custom shader
  65. CreateCustomMSAAResolveResources();
  66. CreateCustomMSAAResolveScope();
  67. OnMSAATypeChanged(m_currentType);
  68. AZ::RHI::RHISystemNotificationBus::Handler::BusConnect();
  69. AzFramework::InputChannelEventListener::Connect();
  70. }
  71. void MSAAExampleComponent::Deactivate()
  72. {
  73. m_triangleInputAssemblyBuffer = nullptr;
  74. m_quadInputAssemblyBuffer = nullptr;
  75. m_inputAssemblyBufferPool = nullptr;
  76. m_pipelineStates.fill(nullptr);
  77. m_customResolveMSAAPipelineState = nullptr;
  78. m_triangleShaderResourceGroup = nullptr;
  79. m_customMSAAResolveShaderResourceGroup = nullptr;
  80. AzFramework::InputChannelEventListener::Disconnect();
  81. AZ::RHI::RHISystemNotificationBus::Handler::BusDisconnect();
  82. m_windowContext = nullptr;
  83. AZStd::for_each(m_MSAAScopeProducers.begin(), m_MSAAScopeProducers.end(), [](auto& list) { list.clear(); });
  84. m_scopeProducers.clear();
  85. }
  86. bool MSAAExampleComponent::OnInputChannelEventFiltered(const AzFramework::InputChannel & inputChannel)
  87. {
  88. const AzFramework::InputChannelId& inputChannelId = inputChannel.GetInputChannelId();
  89. switch (inputChannel.GetState())
  90. {
  91. case AzFramework::InputChannel::State::Ended:
  92. {
  93. if (inputChannelId == AzFramework::InputDeviceMouse::Button::Left)
  94. {
  95. OnMSAATypeChanged(static_cast<MSAAType>((static_cast<uint32_t>(m_currentType) + 1) % s_numMSAAExamples));
  96. }
  97. }
  98. default:
  99. {
  100. break;
  101. }
  102. }
  103. return false;
  104. }
  105. void MSAAExampleComponent::CreateTriangleResources()
  106. {
  107. using namespace AZ;
  108. TriangleBufferData bufferData;
  109. SetVertexPosition(bufferData.m_positions.data(), 0, 0.0, 0.5, 0.0);
  110. SetVertexPosition(bufferData.m_positions.data(), 1, -0.5, -0.5, 0.0);
  111. SetVertexPosition(bufferData.m_positions.data(), 2, 0.5, -0.5, 0.0);
  112. SetVertexColor(bufferData.m_colors.data(), 0, 1.0, 0.0, 0.0, 1.0);
  113. SetVertexColor(bufferData.m_colors.data(), 1, 0.0, 1.0, 0.0, 1.0);
  114. SetVertexColor(bufferData.m_colors.data(), 2, 0.0, 0.0, 1.0, 1.0);
  115. SetVertexIndexIncreasing(bufferData.m_indices.data(), bufferData.m_indices.size());
  116. m_triangleInputAssemblyBuffer = aznew RHI::Buffer();
  117. RHI::BufferInitRequest request;
  118. request.m_buffer = m_triangleInputAssemblyBuffer.get();
  119. request.m_descriptor = RHI::BufferDescriptor{ RHI::BufferBindFlags::InputAssembly, sizeof(bufferData) };
  120. request.m_initialData = &bufferData;
  121. m_inputAssemblyBufferPool->InitBuffer(request);
  122. m_triangleGeometryView.SetDrawArguments(RHI::DrawIndexed(0, 3, 0));
  123. m_triangleGeometryView.SetIndexBufferView({
  124. *m_triangleInputAssemblyBuffer,
  125. offsetof(TriangleBufferData, m_indices),
  126. sizeof(TriangleBufferData::m_indices),
  127. RHI::IndexFormat::Uint16
  128. });
  129. m_triangleGeometryView.AddStreamBufferView({
  130. *m_triangleInputAssemblyBuffer,
  131. offsetof(TriangleBufferData, m_positions),
  132. sizeof(TriangleBufferData::m_positions),
  133. sizeof(VertexPosition)
  134. });
  135. m_triangleGeometryView.AddStreamBufferView({
  136. *m_triangleInputAssemblyBuffer,
  137. offsetof(TriangleBufferData, m_colors),
  138. sizeof(TriangleBufferData::m_colors),
  139. sizeof(VertexColor)
  140. });
  141. RHI::InputStreamLayoutBuilder layoutBuilder;
  142. layoutBuilder.AddBuffer()->Channel("POSITION", RHI::Format::R32G32B32_FLOAT);
  143. layoutBuilder.AddBuffer()->Channel("COLOR", RHI::Format::R32G32B32A32_FLOAT);
  144. m_triangleInputStreamLayout = layoutBuilder.End();
  145. RHI::ValidateStreamBufferViews(m_triangleInputStreamLayout, m_triangleGeometryView, m_triangleGeometryView.GetFullStreamBufferIndices());
  146. m_triangleShader = LoadShader(TriangeShaderFilePath, SampleName);
  147. if (!m_triangleShader)
  148. {
  149. return;
  150. }
  151. m_triangleShaderResourceGroup = CreateShaderResourceGroup(m_triangleShader, "TriangleInstanceSrg", SampleName);
  152. const Name objectMatrixConstantId{ "m_objectMatrix" };
  153. FindShaderInputIndex(&m_objectMatrixConstantIndex, m_triangleShaderResourceGroup, objectMatrixConstantId, SampleName);
  154. [[maybe_unused]] bool success = m_triangleShaderResourceGroup->SetConstant(m_objectMatrixConstantIndex, AZ::Matrix4x4::CreateIdentity());
  155. AZ_Warning("MSAAExampleComponent", success, "Failed to set SRG Constant m_objectMatrix");
  156. m_triangleShaderResourceGroup->Compile();
  157. }
  158. void MSAAExampleComponent::CreateQuadResources()
  159. {
  160. using namespace AZ;
  161. QuadBufferData bufferData;
  162. SetFullScreenRect(bufferData.m_positions.data(), bufferData.m_uvs.data(), bufferData.m_indices.data());
  163. m_quadInputAssemblyBuffer = aznew RHI::Buffer();
  164. RHI::ResultCode result = RHI::ResultCode::Success;
  165. RHI::BufferInitRequest request;
  166. request.m_buffer = m_quadInputAssemblyBuffer.get();
  167. request.m_descriptor = RHI::BufferDescriptor{ RHI::BufferBindFlags::InputAssembly, sizeof(bufferData) };
  168. request.m_initialData = &bufferData;
  169. result = m_inputAssemblyBufferPool->InitBuffer(request);
  170. if (result != RHI::ResultCode::Success)
  171. {
  172. AZ_Error(SampleName, false, "Failed to initialize position buffer with error code %d", result);
  173. return;
  174. }
  175. m_quadGeometryView.SetDrawArguments(RHI::DrawIndexed(0, 6, 0));
  176. m_quadGeometryView.SetIndexBufferView({
  177. *m_quadInputAssemblyBuffer,
  178. offsetof(QuadBufferData, m_indices),
  179. sizeof(QuadBufferData::m_indices),
  180. RHI::IndexFormat::Uint16
  181. });
  182. m_quadGeometryView.AddStreamBufferView({
  183. *m_quadInputAssemblyBuffer,
  184. offsetof(QuadBufferData, m_positions),
  185. sizeof(QuadBufferData::m_positions),
  186. sizeof(VertexPosition)
  187. });
  188. m_quadGeometryView.AddStreamBufferView({
  189. *m_quadInputAssemblyBuffer,
  190. offsetof(QuadBufferData, m_uvs),
  191. sizeof(QuadBufferData::m_uvs),
  192. sizeof(VertexUV)
  193. });
  194. RHI::InputStreamLayoutBuilder layoutBuilder;
  195. layoutBuilder.AddBuffer()->Channel("POSITION", RHI::Format::R32G32B32_FLOAT);
  196. layoutBuilder.AddBuffer()->Channel("UV", RHI::Format::R32G32_FLOAT);
  197. m_quadInputStreamLayout = layoutBuilder.End();
  198. RHI::ValidateStreamBufferViews(m_quadInputStreamLayout, m_quadGeometryView, m_quadGeometryView.GetFullStreamBufferIndices());
  199. m_customMSAAResolveShader = LoadShader(CustomResolveShaderFilePath, SampleName);
  200. if (!m_customMSAAResolveShader)
  201. {
  202. return;
  203. }
  204. m_customMSAAResolveShaderResourceGroup = CreateShaderResourceGroup(m_customMSAAResolveShader, "TextureInstanceSrg", SampleName);
  205. const Name albedoMapId{ "m_albedoMap" };
  206. FindShaderInputIndex(&m_customMSAAResolveTextureInputIndex, m_customMSAAResolveShaderResourceGroup, albedoMapId, SampleName);
  207. }
  208. void MSAAExampleComponent::OnMSAATypeChanged(MSAAType type)
  209. {
  210. auto msaaTypeIndex = static_cast<uint32_t>(type);
  211. m_scopeProducers.clear();
  212. m_scopeProducers.insert(m_scopeProducers.end(), m_MSAAScopeProducers[msaaTypeIndex].begin(), m_MSAAScopeProducers[msaaTypeIndex].end());
  213. m_currentType = type;
  214. }
  215. void MSAAExampleComponent::CreateScopeResources(MSAAType type)
  216. {
  217. using namespace AZ;
  218. const auto& properties = m_sampleProperties[static_cast<uint32_t>(type)];
  219. AZ::RHI::PipelineStateDescriptorForDraw pipelineStateDescriptor;
  220. pipelineStateDescriptor.m_inputStreamLayout = m_triangleInputStreamLayout;
  221. auto shaderVariant = m_triangleShader->GetVariant(RPI::ShaderAsset::RootShaderVariantStableId);
  222. shaderVariant.ConfigurePipelineState(pipelineStateDescriptor);
  223. RHI::RenderAttachmentLayoutBuilder attachmentsBuilder;
  224. attachmentsBuilder.AddSubpass()
  225. ->RenderTargetAttachment(m_outputFormat, properties.m_resolve);
  226. [[maybe_unused]] RHI::ResultCode result = attachmentsBuilder.End(pipelineStateDescriptor.m_renderAttachmentConfiguration.m_renderAttachmentLayout);
  227. AZ_Assert(result == RHI::ResultCode::Success, "Failed to create render attachment layout");
  228. pipelineStateDescriptor.m_renderStates.m_multisampleState.m_samples = static_cast<uint16_t>(properties.m_samplesCount);
  229. auto& customPositionList = pipelineStateDescriptor.m_renderStates.m_multisampleState.m_customPositions;
  230. AZStd::copy(properties.m_customPositions.begin(), properties.m_customPositions.end(), customPositionList.begin());
  231. pipelineStateDescriptor.m_renderStates.m_multisampleState.m_customPositionsCount = static_cast<uint32_t>(properties.m_customPositions.size());
  232. auto& pipeline = m_pipelineStates[static_cast<uint32_t>(type)];
  233. pipeline = m_triangleShader->AcquirePipelineState(pipelineStateDescriptor);
  234. if (!pipeline)
  235. {
  236. AZ_Error(SampleName, false, "Failed to acquire default pipeline state for shader '%s'", TriangeShaderFilePath);
  237. return;
  238. }
  239. }
  240. void MSAAExampleComponent::CreateScopeProducer(MSAAType type)
  241. {
  242. using namespace AZ;
  243. uint32_t msaaTypeIndex = static_cast<uint32_t>(type);
  244. // Creates a scope for rendering the triangle.
  245. const auto prepareFunction = [this, msaaTypeIndex](RHI::FrameGraphInterface frameGraph, [[maybe_unused]] ScopeData& scopeData)
  246. {
  247. {
  248. // Binds the MSAA color attachment.
  249. RHI::ImageScopeAttachmentDescriptor descriptor;
  250. descriptor.m_attachmentId = m_sampleProperties[msaaTypeIndex].m_attachmentId;
  251. descriptor.m_loadStoreAction.m_clearValue = RHI::ClearValue::CreateVector4Float(1.0f, 1.0, 1.0, 0.0);
  252. descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Clear;
  253. descriptor.m_loadStoreAction.m_storeAction = m_sampleProperties[msaaTypeIndex].m_storeAction;
  254. if (m_sampleProperties[msaaTypeIndex].m_isTransient)
  255. {
  256. auto imageDesc = RHI::ImageDescriptor::Create2D(
  257. RHI::ImageBindFlags::Color | RHI::ImageBindFlags::ShaderRead,
  258. m_outputWidth,
  259. m_outputHeight,
  260. m_outputFormat);
  261. imageDesc.m_multisampleState = RHI::MultisampleState(static_cast<uint16_t>(m_sampleProperties[msaaTypeIndex].m_samplesCount), 0);
  262. if(m_sampleProperties[msaaTypeIndex].m_customPositions.size()>0)
  263. {
  264. imageDesc.m_multisampleState.m_customPositionsCount = static_cast<uint32_t>(m_sampleProperties[msaaTypeIndex].m_customPositions.size());
  265. auto& customPositionList = imageDesc.m_multisampleState.m_customPositions;
  266. AZStd::copy(m_sampleProperties[msaaTypeIndex].m_customPositions.begin(), m_sampleProperties[msaaTypeIndex].m_customPositions.end(), customPositionList.begin());
  267. }
  268. frameGraph.GetAttachmentDatabase().CreateTransientImage(RHI::TransientImageDescriptor(descriptor.m_attachmentId, imageDesc));
  269. }
  270. frameGraph.UseColorAttachment(descriptor);
  271. }
  272. // Binds the resolve color attachment.
  273. if (m_sampleProperties[msaaTypeIndex].m_resolve)
  274. {
  275. RHI::ResolveScopeAttachmentDescriptor descriptor;
  276. descriptor.m_attachmentId = m_outputAttachmentId;
  277. descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::DontCare;
  278. descriptor.m_resolveAttachmentId = m_sampleProperties[msaaTypeIndex].m_attachmentId;
  279. frameGraph.UseResolveAttachment(descriptor);
  280. }
  281. // We will submit a single draw item.
  282. frameGraph.SetEstimatedItemCount(1);
  283. };
  284. using namespace AZ;
  285. RHI::EmptyCompileFunction<ScopeData> compileFunction;
  286. const auto executeFunction = [this, msaaTypeIndex](const RHI::FrameGraphExecuteContext& context, [[maybe_unused]] const ScopeData& scopeData)
  287. {
  288. RHI::CommandList* commandList = context.GetCommandList();
  289. // Set persistent viewport and scissor state.
  290. commandList->SetViewports(&m_viewport, 1);
  291. commandList->SetScissors(&m_scissor, 1);
  292. const RHI::DeviceShaderResourceGroup* shaderResourceGroups[] = {
  293. m_triangleShaderResourceGroup->GetRHIShaderResourceGroup()->GetDeviceShaderResourceGroup(context.GetDeviceIndex()).get()
  294. };
  295. // Submit the triangle draw item.
  296. RHI::DeviceDrawItem drawItem;
  297. drawItem.m_geometryView = m_triangleGeometryView.GetDeviceGeometryView(context.GetDeviceIndex());
  298. drawItem.m_streamIndices = m_triangleGeometryView.GetFullStreamBufferIndices();
  299. drawItem.m_pipelineState = m_pipelineStates[msaaTypeIndex]->GetDevicePipelineState(context.GetDeviceIndex()).get();
  300. drawItem.m_shaderResourceGroupCount = static_cast<uint8_t>(RHI::ArraySize(shaderResourceGroups));
  301. drawItem.m_shaderResourceGroups = shaderResourceGroups;
  302. commandList->Submit(drawItem);
  303. };
  304. auto name = AZStd::string::format("MSAAType_%d", msaaTypeIndex);
  305. const AZ::RHI::ScopeId TriangleScope(name);
  306. m_MSAAScopeProducers[msaaTypeIndex].emplace_back(
  307. aznew RHI::ScopeProducerFunction<
  308. ScopeData,
  309. decltype(prepareFunction),
  310. decltype(compileFunction),
  311. decltype(executeFunction)>(
  312. TriangleScope,
  313. ScopeData{},
  314. prepareFunction,
  315. compileFunction,
  316. executeFunction));
  317. }
  318. void MSAAExampleComponent::CreateCustomMSAAResolveResources()
  319. {
  320. using namespace AZ;
  321. AZ::RHI::PipelineStateDescriptorForDraw pipelineStateDescriptor;
  322. pipelineStateDescriptor.m_inputStreamLayout = m_quadInputStreamLayout;
  323. if (!m_customMSAAResolveShader)
  324. {
  325. return;
  326. }
  327. auto shaderVariant = m_customMSAAResolveShader->GetVariant(AZ::RPI::ShaderAsset::RootShaderVariantStableId);
  328. shaderVariant.ConfigurePipelineState(pipelineStateDescriptor);
  329. RHI::RenderAttachmentLayoutBuilder attachmentsBuilder;
  330. attachmentsBuilder.AddSubpass()
  331. ->RenderTargetAttachment(m_outputFormat);
  332. [[maybe_unused]] RHI::ResultCode result = attachmentsBuilder.End(pipelineStateDescriptor.m_renderAttachmentConfiguration.m_renderAttachmentLayout);
  333. AZ_Assert(result == RHI::ResultCode::Success, "Failed to create render attachment layout");
  334. m_customResolveMSAAPipelineState = m_customMSAAResolveShader->AcquirePipelineState(pipelineStateDescriptor);
  335. if (!m_customResolveMSAAPipelineState)
  336. {
  337. AZ_Error(SampleName, false, "Failed to acquire default pipeline state for shader '%s'", CustomResolveShaderFilePath);
  338. return;
  339. }
  340. }
  341. void MSAAExampleComponent::CreateCustomMSAAResolveScope()
  342. {
  343. using namespace AZ;
  344. const RHI::Ptr<RHI::Device> device = Utils::GetRHIDevice();
  345. const auto prepareFunction = [this](RHI::FrameGraphInterface frameGraph, [[maybe_unused]] const ScopeData& scopeData)
  346. {
  347. // Bind MSAA as a texture attachment to sample
  348. {
  349. RHI::ImageScopeAttachmentDescriptor descriptor;
  350. descriptor.m_attachmentId = m_sampleProperties[static_cast<uint32_t>(MSAAType::MSAA4X_Custom_Resolve)].m_attachmentId;
  351. descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Load;
  352. frameGraph.UseShaderAttachment(descriptor, RHI::ScopeAttachmentAccess::Read, RHI::ScopeAttachmentStage::FragmentShader);
  353. }
  354. // Binds the swap chain as a color attachment.
  355. {
  356. RHI::ImageScopeAttachmentDescriptor descriptor;
  357. descriptor.m_attachmentId = m_outputAttachmentId;
  358. descriptor.m_loadStoreAction.m_clearValue = RHI::ClearValue::CreateVector4Float(1.0f, 1.0, 1.0, 0.0);
  359. descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Clear;
  360. frameGraph.UseColorAttachment(descriptor);
  361. }
  362. // We will submit a single draw item.
  363. frameGraph.SetEstimatedItemCount(1);
  364. };
  365. const auto compileFunction = [this](const AZ::RHI::FrameGraphCompileContext& context, [[maybe_unused]] const ScopeData& scopeData)
  366. {
  367. const auto* imageView = context.GetImageView(m_sampleProperties[static_cast<uint32_t>(MSAAType::MSAA4X_Custom_Resolve)].m_attachmentId);
  368. m_customMSAAResolveShaderResourceGroup->SetImageView(m_customMSAAResolveTextureInputIndex, imageView);
  369. m_customMSAAResolveShaderResourceGroup->Compile();
  370. };
  371. const auto executeFunction = [this](const RHI::FrameGraphExecuteContext& context, [[maybe_unused]] const ScopeData& scopeData)
  372. {
  373. RHI::CommandList* commandList = context.GetCommandList();
  374. // Set persistent viewport and scissor state.
  375. commandList->SetViewports(&m_viewport, 1);
  376. commandList->SetScissors(&m_scissor, 1);
  377. const RHI::DeviceShaderResourceGroup* shaderResourceGroups[] = { m_customMSAAResolveShaderResourceGroup
  378. ->GetRHIShaderResourceGroup()
  379. ->GetDeviceShaderResourceGroup(context.GetDeviceIndex())
  380. .get() };
  381. // Submit the quad draw item.
  382. RHI::DeviceDrawItem drawItem;
  383. drawItem.m_geometryView = m_quadGeometryView.GetDeviceGeometryView(context.GetDeviceIndex());
  384. drawItem.m_streamIndices = m_quadGeometryView.GetFullStreamBufferIndices();
  385. drawItem.m_pipelineState = m_customResolveMSAAPipelineState->GetDevicePipelineState(context.GetDeviceIndex()).get();
  386. drawItem.m_shaderResourceGroupCount = static_cast<uint8_t>(RHI::ArraySize(shaderResourceGroups));
  387. drawItem.m_shaderResourceGroups = shaderResourceGroups;
  388. commandList->Submit(drawItem);
  389. };
  390. m_MSAAScopeProducers[static_cast<uint32_t>(MSAAType::MSAA4X_Custom_Resolve)].emplace_back(
  391. aznew RHI::ScopeProducerFunction<
  392. ScopeData,
  393. decltype(prepareFunction),
  394. decltype(compileFunction),
  395. decltype(executeFunction)>(
  396. AZ::RHI::ScopeId("Resolve"),
  397. ScopeData{ },
  398. prepareFunction,
  399. compileFunction,
  400. executeFunction));
  401. }
  402. } // namespace AtomSampleViewer