MultiGPUExampleComponent.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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/MultiGPUExampleComponent.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/RHI.Reflect/ImageScopeAttachmentDescriptor.h>
  16. #include <Atom/RPI.Reflect/Shader/ShaderAsset.h>
  17. #include <AzCore/Serialization/SerializeContext.h>
  18. #include <Atom/RHI/DrawItem.h>
  19. #include <Atom/RHI/CopyItem.h>
  20. #include <Atom/RHI.Reflect/BufferDescriptor.h>
  21. using namespace AZ;
  22. namespace AtomSampleViewer
  23. {
  24. void MultiGPUExampleComponent::Reflect(AZ::ReflectContext* context)
  25. {
  26. if (auto* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  27. {
  28. serializeContext->Class<MultiGPUExampleComponent, AZ::Component>()
  29. ->Version(0)
  30. ;
  31. }
  32. }
  33. void MultiGPUExampleComponent::OnFramePrepare(AZ::RHI::FrameGraphBuilder& frameGraphBuilder)
  34. {
  35. static float time = 0.0f;
  36. time += 0.005f;
  37. // Move the triangle around.
  38. AZ::Vector3 translation(
  39. sinf(time) * 0.25f,
  40. cosf(time) * 0.25f,
  41. 0.0f);
  42. if (m_shaderResourceGroupShared)
  43. {
  44. [[maybe_unused]] bool success =
  45. m_shaderResourceGroupShared->SetConstant(m_objectMatrixConstantIndex, AZ::Matrix4x4::CreateTranslation(translation));
  46. AZ_Warning("MultiGPUExampleComponent", success, "Failed to set SRG Constant m_objectMatrix");
  47. m_shaderResourceGroupShared->Compile();
  48. }
  49. BasicRHIComponent::OnFramePrepare(frameGraphBuilder);
  50. }
  51. void MultiGPUExampleComponent::FrameBeginInternal(AZ::RHI::FrameGraphBuilder& frameGraphBuilder)
  52. {
  53. if (m_outputWidth != m_imageWidth || m_outputHeight != m_imageHeight)
  54. {
  55. // Image used as color attachment only on first device
  56. {
  57. m_images[0] = aznew RHI::Image;
  58. RHI::ImageInitRequest initImageRequest;
  59. initImageRequest.m_image = m_images[0].get();
  60. initImageRequest.m_descriptor = RHI::ImageDescriptor::Create2D(
  61. RHI::ImageBindFlags::Color | RHI::ImageBindFlags::ShaderReadWrite, m_outputWidth, m_outputHeight, m_outputFormat);
  62. initImageRequest.m_deviceMask = m_deviceMask_1;
  63. m_imagePool->InitImage(initImageRequest);
  64. }
  65. // Image used as color attachment on both devices (rendered on device 1 and copied to device 0 for compositing)
  66. {
  67. m_images[1] = aznew RHI::Image;
  68. RHI::ImageInitRequest initImageRequest;
  69. initImageRequest.m_image = m_images[1].get();
  70. initImageRequest.m_descriptor = RHI::ImageDescriptor::Create2D(
  71. RHI::ImageBindFlags::Color | RHI::ImageBindFlags::ShaderReadWrite | RHI::ImageBindFlags::CopyRead | RHI::ImageBindFlags::CopyWrite, m_outputWidth, m_outputHeight, m_outputFormat);
  72. m_imagePool->InitImage(initImageRequest);
  73. }
  74. RHI::BufferBindFlags stagingBufferBindFlags{ RHI::BufferBindFlags::CopyWrite | RHI::BufferBindFlags::CopyRead };
  75. {
  76. m_stagingBufferToGPU = aznew RHI::Buffer;
  77. AZStd::vector<unsigned int> initialData(m_outputWidth * m_outputHeight, 0xFFFF00FFu);
  78. RHI::BufferInitRequest request;
  79. request.m_buffer = m_stagingBufferToGPU.get();
  80. request.m_descriptor = RHI::BufferDescriptor{stagingBufferBindFlags, initialData.size() * sizeof(unsigned int)};
  81. request.m_initialData = initialData.data();
  82. // This buffer is only necessary on device 0, but we test UpdateBufferDeviceMask below
  83. request.m_deviceMask = RHI::MultiDevice::AllDevices;
  84. if (m_stagingBufferPool->InitBuffer(request) != RHI::ResultCode::Success)
  85. {
  86. AZ_Error("MultiGPUExampleComponent", false, "StagingBufferToGPU was not created");
  87. }
  88. auto bufferViewDescriptor{ RHI::BufferViewDescriptor::CreateRaw(0, static_cast<u32>(request.m_descriptor.m_byteCount)) };
  89. auto bufferView = m_stagingBufferToGPU->BuildBufferView(bufferViewDescriptor);
  90. bufferView->GetDeviceBufferView(0);
  91. bufferView->GetDeviceBufferView(1);
  92. RHI::BufferDeviceMaskRequest updateRequest;
  93. updateRequest.m_buffer = m_stagingBufferToGPU.get();
  94. updateRequest.m_initialData = initialData.data();
  95. updateRequest.m_deviceMask = m_deviceMask_1;
  96. if (m_stagingBufferPool->UpdateBufferDeviceMask(updateRequest) != RHI::ResultCode::Success)
  97. {
  98. AZ_Error("MultiGPUExampleComponent", false, "StagingBufferToGPU was not created");
  99. }
  100. bufferView->GetDeviceBufferView(0);
  101. }
  102. {
  103. m_stagingBufferToCPU = aznew RHI::Buffer;
  104. RHI::BufferInitRequest request;
  105. request.m_buffer = m_stagingBufferToCPU.get();
  106. request.m_descriptor =
  107. RHI::BufferDescriptor{ stagingBufferBindFlags, m_outputWidth * m_outputHeight * sizeof(unsigned int) };
  108. // This buffer is necessary on device 1, but we test UpdateBufferDeviceMask below
  109. request.m_deviceMask = RHI::MultiDevice::NoDevices;
  110. if (m_stagingBufferPool->InitBuffer(request) != RHI::ResultCode::Success)
  111. {
  112. AZ_Error("MultiGPUExampleComponent", false, "StagingBufferToCPU was not created");
  113. }
  114. RHI::BufferDeviceMaskRequest updateRequest;
  115. updateRequest.m_buffer = m_stagingBufferToCPU.get();
  116. updateRequest.m_deviceMask = m_deviceMask_2;
  117. if (m_stagingBufferPool->UpdateBufferDeviceMask(updateRequest) != RHI::ResultCode::Success)
  118. {
  119. AZ_Error("MultiGPUExampleComponent", false, "StagingBufferToCPU was not created");
  120. }
  121. }
  122. m_scissors[0].m_minX = 0;
  123. m_scissors[0].m_minY = 0;
  124. m_scissors[0].m_maxX = m_outputWidth / 2 + 1;
  125. m_scissors[0].m_maxY = m_outputHeight;
  126. m_scissors[1].m_minX = m_outputWidth / 2;
  127. m_scissors[1].m_minY = 0;
  128. m_scissors[1].m_maxX = m_outputWidth;
  129. m_scissors[1].m_maxY = m_outputHeight;
  130. m_imageWidth = m_outputWidth;
  131. m_imageHeight = m_outputHeight;
  132. }
  133. frameGraphBuilder.GetAttachmentDatabase().ImportImage(
  134. m_imageAttachmentIds[0], m_images[0]);
  135. frameGraphBuilder.GetAttachmentDatabase().ImportImage(
  136. m_imageAttachmentIds[1], m_images[1]);
  137. frameGraphBuilder.GetAttachmentDatabase().ImportBuffer(
  138. m_bufferAttachmentIds[0], m_stagingBufferToCPU);
  139. frameGraphBuilder.GetAttachmentDatabase().ImportBuffer(
  140. m_bufferAttachmentIds[1], m_stagingBufferToGPU);
  141. RHI::DeviceBufferMapRequest request{};
  142. request.m_buffer = m_stagingBufferToCPU->GetDeviceBuffer(1).get();
  143. request.m_byteCount = m_imageWidth * m_imageHeight * sizeof(uint32_t);
  144. RHI::DeviceBufferMapResponse response{};
  145. m_stagingBufferPool->GetDeviceBufferPool(1)->MapBuffer(request, response);
  146. [[maybe_unused]] uint32_t* source = reinterpret_cast<uint32_t*>(response.m_data);
  147. request.m_buffer = m_stagingBufferToGPU->GetDeviceBuffer(0).get();
  148. m_stagingBufferPool->GetDeviceBufferPool(0)->MapBuffer(request, response);
  149. uint32_t* destination = reinterpret_cast<uint32_t*>(response.m_data);
  150. memcpy(destination, source, request.m_byteCount);
  151. m_stagingBufferPool->GetDeviceBufferPool(1)->UnmapBuffer(*m_stagingBufferToCPU->GetDeviceBuffer(1));
  152. m_stagingBufferPool->GetDeviceBufferPool(0)->UnmapBuffer(*m_stagingBufferToGPU->GetDeviceBuffer(0));
  153. }
  154. MultiGPUExampleComponent::MultiGPUExampleComponent()
  155. {
  156. m_supportRHISamplePipeline = true;
  157. }
  158. void MultiGPUExampleComponent::Activate()
  159. {
  160. AZ_Error("MultiGPUExampleComponent", RHI::RHISystemInterface::Get()->GetDeviceCount() >= 2, "At least 2 devices required to run this sample");
  161. m_device_1 = RHI::RHISystemInterface::Get()->GetDevice(0);
  162. m_device_2 = RHI::RHISystemInterface::Get()->GetDevice(1);
  163. m_deviceMask_1 = RHI::MultiDevice::DeviceMask{ 1u << 0 };
  164. m_deviceMask_2 = RHI::MultiDevice::DeviceMask{ 1u << 1 };
  165. m_deviceMask = m_deviceMask_1 | m_deviceMask_2;
  166. // Create multi-device resources
  167. RHI::ImageBindFlags imageBindFlags{ RHI::ImageBindFlags::Color | RHI::ImageBindFlags::ShaderReadWrite | RHI::ImageBindFlags::CopyRead | RHI::ImageBindFlags::CopyWrite };
  168. // ImagePool for both devices
  169. {
  170. m_imagePool = aznew RHI::ImagePool;
  171. m_imagePool->SetName(Name("MultiDeviceTexturePool"));
  172. RHI::ImagePoolDescriptor imagePoolDescriptor{};
  173. imagePoolDescriptor.m_bindFlags = imageBindFlags;
  174. imagePoolDescriptor.m_deviceMask = m_deviceMask;
  175. if (m_imagePool->Init(imagePoolDescriptor) != RHI::ResultCode::Success)
  176. {
  177. AZ_Error("MultiGPUExampleComponent", false, "Failed to initialize render texture image pool.");
  178. return;
  179. }
  180. }
  181. RHI::BufferBindFlags stagingBufferBindFlags{ RHI::BufferBindFlags::CopyWrite | RHI::BufferBindFlags::CopyRead };
  182. // Create staging buffer pool for buffer copy to the CPU and to GPU
  183. {
  184. m_stagingBufferPool = aznew RHI::BufferPool;
  185. RHI::BufferPoolDescriptor bufferPoolDesc;
  186. bufferPoolDesc.m_bindFlags = stagingBufferBindFlags;
  187. bufferPoolDesc.m_heapMemoryLevel = RHI::HeapMemoryLevel::Host;
  188. bufferPoolDesc.m_hostMemoryAccess = RHI::HostMemoryAccess::Write;
  189. bufferPoolDesc.m_deviceMask = m_deviceMask;
  190. if (m_stagingBufferPool->Init(bufferPoolDesc) != RHI::ResultCode::Success)
  191. {
  192. AZ_Error("MultiGPUExampleComponent", false, "StagingBufferPool was not initialized");
  193. }
  194. }
  195. // Setup main and secondary pipeline
  196. CreateRenderScopeProducer();
  197. CreateCopyToCPUScopeProducer();
  198. CreateCopyToGPUScopeProducer();
  199. CreateCompositeScopeProducer();
  200. RHI::RHISystemNotificationBus::Handler::BusConnect();
  201. }
  202. void MultiGPUExampleComponent::Deactivate()
  203. {
  204. m_inputAssemblyBuffer = nullptr;
  205. m_inputAssemblyBufferPool = nullptr;
  206. m_pipelineState = nullptr;
  207. m_shaderResourceGroupShared = nullptr;
  208. m_stagingBufferPool = nullptr;
  209. m_stagingBufferToGPU = nullptr;
  210. m_stagingBufferToCPU = nullptr;
  211. m_inputAssemblyBufferComposite = nullptr;
  212. m_pipelineStateComposite = nullptr;
  213. m_shaderResourceGroupComposite = nullptr;
  214. m_shaderResourceGroupDataComposite = RHI::ShaderResourceGroupData{};
  215. m_shaderResourceGroupPoolComposite = nullptr;
  216. RHI::RHISystemNotificationBus::Handler::BusDisconnect();
  217. m_windowContext = nullptr;
  218. m_scopeProducers.clear();
  219. m_secondaryScopeProducers.clear();
  220. }
  221. void MultiGPUExampleComponent::CreateRenderScopeProducer()
  222. {
  223. RHI::PipelineStateDescriptorForDraw pipelineStateDescriptor;
  224. {
  225. m_inputAssemblyBufferPool = aznew RHI::BufferPool;
  226. RHI::BufferPoolDescriptor bufferPoolDesc;
  227. bufferPoolDesc.m_bindFlags = RHI::BufferBindFlags::InputAssembly;
  228. bufferPoolDesc.m_heapMemoryLevel = RHI::HeapMemoryLevel::Device;
  229. bufferPoolDesc.m_deviceMask = m_deviceMask;
  230. m_inputAssemblyBufferPool->Init(bufferPoolDesc);
  231. BufferDataTrianglePass bufferData;
  232. SetVertexPosition(bufferData.m_positions.data(), 0, 0.0, 0.5, 0.0);
  233. SetVertexPosition(bufferData.m_positions.data(), 1, -0.5, -0.5, 0.0);
  234. SetVertexPosition(bufferData.m_positions.data(), 2, 0.5, -0.5, 0.0);
  235. SetVertexColor(bufferData.m_colors.data(), 0, 1.0, 0.0, 0.0, 1.0);
  236. SetVertexColor(bufferData.m_colors.data(), 1, 0.0, 1.0, 0.0, 1.0);
  237. SetVertexColor(bufferData.m_colors.data(), 2, 0.0, 0.0, 1.0, 1.0);
  238. SetVertexIndexIncreasing(bufferData.m_indices.data(), bufferData.m_indices.size());
  239. m_inputAssemblyBuffer = aznew RHI::Buffer;
  240. RHI::BufferInitRequest request;
  241. request.m_buffer = m_inputAssemblyBuffer.get();
  242. request.m_descriptor = RHI::BufferDescriptor{ RHI::BufferBindFlags::InputAssembly, sizeof(bufferData) };
  243. request.m_initialData = &bufferData;
  244. m_inputAssemblyBufferPool->InitBuffer(request);
  245. m_geometryView.SetDrawArguments(RHI::DrawIndexed(0, 3, 0));
  246. m_geometryView.SetIndexBufferView({
  247. *m_inputAssemblyBuffer,
  248. offsetof(BufferDataTrianglePass, m_indices),
  249. sizeof(BufferDataTrianglePass::m_indices),
  250. RHI::IndexFormat::Uint16
  251. });
  252. m_geometryView.AddStreamBufferView({
  253. *m_inputAssemblyBuffer,
  254. offsetof(BufferDataTrianglePass, m_positions),
  255. sizeof(BufferDataTrianglePass::m_positions),
  256. sizeof(VertexPosition)
  257. });
  258. m_geometryView.AddStreamBufferView({
  259. *m_inputAssemblyBuffer,
  260. offsetof(BufferDataTrianglePass, m_colors),
  261. sizeof(BufferDataTrianglePass::m_colors),
  262. sizeof(VertexColor)
  263. });
  264. RHI::InputStreamLayoutBuilder layoutBuilder;
  265. layoutBuilder.AddBuffer()->Channel("POSITION", RHI::Format::R32G32B32_FLOAT);
  266. layoutBuilder.AddBuffer()->Channel("COLOR", RHI::Format::R32G32B32A32_FLOAT);
  267. pipelineStateDescriptor.m_inputStreamLayout = layoutBuilder.End();
  268. RHI::ValidateStreamBufferViews(pipelineStateDescriptor.m_inputStreamLayout, m_geometryView, m_geometryView.GetFullStreamBufferIndices());
  269. }
  270. {
  271. const char* triangleShaderFilePath = "Shaders/RHI/triangle.azshader";
  272. const char* sampleName = "MultiGPUExample";
  273. auto shader = LoadShader(triangleShaderFilePath, sampleName);
  274. if (shader == nullptr)
  275. return;
  276. auto shaderOptionGroup = shader->CreateShaderOptionGroup();
  277. shaderOptionGroup.SetUnspecifiedToDefaultValues();
  278. // This is an example of how to set different shader options when searching for the shader variant you want to display
  279. // Searching by id is simple, but suboptimal. Here it's only used to demonstrate the principle,
  280. // but in practice the ShaderOptionIndex and the ShaderOptionValue should be cached for better performance
  281. // You can also try DrawMode::Green, DrawMode::Blue or DrawMode::White. The specified color will appear on top of the triangle.
  282. shaderOptionGroup.SetValue(AZ::Name("o_drawMode"), AZ::Name("DrawMode::Red"));
  283. auto shaderVariant = shader->GetVariant(shaderOptionGroup.GetShaderVariantId());
  284. shaderVariant.ConfigurePipelineState(pipelineStateDescriptor);
  285. RHI::RenderAttachmentLayoutBuilder attachmentsBuilder;
  286. attachmentsBuilder.AddSubpass()
  287. ->RenderTargetAttachment(m_outputFormat);
  288. [[maybe_unused]] RHI::ResultCode result = attachmentsBuilder.End(pipelineStateDescriptor.m_renderAttachmentConfiguration.m_renderAttachmentLayout);
  289. AZ_Assert(result == RHI::ResultCode::Success, "Failed to create render attachment layout");
  290. m_pipelineState = shader->AcquirePipelineState(pipelineStateDescriptor);
  291. if (!m_pipelineState)
  292. {
  293. AZ_Error(sampleName, false, "Failed to acquire default pipeline state for shader '%s'", triangleShaderFilePath);
  294. return;
  295. }
  296. m_shaderResourceGroupShared = CreateShaderResourceGroup(shader, "TriangleInstanceSrg", sampleName);
  297. const Name objectMatrixConstantId{ "m_objectMatrix" };
  298. FindShaderInputIndex(&m_objectMatrixConstantIndex, m_shaderResourceGroupShared, objectMatrixConstantId, sampleName);
  299. // In practice m_shaderResourceGroupShared should be one of the cached SRGs owned by the DrawItem
  300. if (!shaderVariant.IsFullyBaked() && m_shaderResourceGroupShared->HasShaderVariantKeyFallbackEntry())
  301. {
  302. // Normally if the requested variant isn't an exact match we have to set it by SetShaderVariantKeyFallbackValue
  303. // In most cases this should be the preferred behavior:
  304. m_shaderResourceGroupShared->SetShaderVariantKeyFallbackValue(shaderOptionGroup.GetShaderVariantKeyFallbackValue());
  305. AZ_Warning(
  306. sampleName, false, "Check the Triangle.shader file - some program variants haven't been baked ('%s')",
  307. triangleShaderFilePath);
  308. }
  309. }
  310. // Creates two scopes for rendering the halves of the triangle.
  311. {
  312. struct ScopeData
  313. {
  314. bool second{false};
  315. };
  316. const auto prepareFunction = [this](RHI::FrameGraphInterface frameGraph, [[maybe_unused]] ScopeData& scopeData)
  317. {
  318. // Binds the swap chain as a color attachment. Clears it to black.
  319. RHI::ImageScopeAttachmentDescriptor descriptor;
  320. descriptor.m_attachmentId = m_imageAttachmentIds[scopeData.second];
  321. descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Clear;
  322. descriptor.m_loadStoreAction.m_storeAction = RHI::AttachmentStoreAction::Store;
  323. descriptor.m_loadStoreAction.m_clearValue.m_vector4Uint = {0, 0, 0, 0};
  324. frameGraph.UseColorAttachment(descriptor);
  325. // We will submit a single draw item.
  326. frameGraph.SetEstimatedItemCount(1);
  327. };
  328. RHI::EmptyCompileFunction<ScopeData> compileFunction;
  329. const auto executeFunction = [this](const RHI::FrameGraphExecuteContext& context, [[maybe_unused]] const ScopeData& scopeData)
  330. {
  331. RHI::CommandList* commandList = context.GetCommandList();
  332. // Set persistent viewport and scissor state.
  333. commandList->SetViewports(&m_viewport, 1);
  334. commandList->SetScissors(&m_scissors[int(scopeData.second)], 1);
  335. const RHI::DeviceShaderResourceGroup* shaderResourceGroups[] = {
  336. m_shaderResourceGroupShared->GetRHIShaderResourceGroup()->GetDeviceShaderResourceGroup(context.GetDeviceIndex()).get()
  337. };
  338. // Submit the triangle draw item.
  339. RHI::DeviceDrawItem drawItem;
  340. drawItem.m_geometryView = m_geometryView.GetDeviceGeometryView(context.GetDeviceIndex());
  341. drawItem.m_streamIndices = m_geometryView.GetFullStreamBufferIndices();
  342. drawItem.m_pipelineState = m_pipelineState->GetDevicePipelineState(context.GetDeviceIndex()).get();
  343. drawItem.m_shaderResourceGroupCount = static_cast<uint8_t>(RHI::ArraySize(shaderResourceGroups));
  344. drawItem.m_shaderResourceGroups = shaderResourceGroups;
  345. commandList->Submit(drawItem);
  346. };
  347. m_scopeProducers.emplace_back(
  348. aznew
  349. RHI::ScopeProducerFunction<ScopeData, decltype(prepareFunction), decltype(compileFunction), decltype(executeFunction)>(
  350. RHI::ScopeId{ "MultiGPUTriangle0" }, ScopeData{}, prepareFunction, compileFunction, executeFunction, 0));
  351. m_scopeProducers.emplace_back(
  352. aznew
  353. RHI::ScopeProducerFunction<ScopeData, decltype(prepareFunction), decltype(compileFunction), decltype(executeFunction)>(
  354. RHI::ScopeId{ "MultiGPUTriangle1" }, ScopeData{true}, prepareFunction, compileFunction, executeFunction, 1));
  355. }
  356. }
  357. void MultiGPUExampleComponent::CreateCompositeScopeProducer()
  358. {
  359. BufferDataCompositePass bufferData;
  360. RHI::PipelineStateDescriptorForDraw pipelineStateDescriptor;
  361. // Setup input assembly for fullscreen pass
  362. {
  363. SetFullScreenRect(bufferData.m_positions.data(), bufferData.m_uvs.data(), bufferData.m_indices.data());
  364. m_inputAssemblyBufferComposite = aznew RHI::Buffer;
  365. RHI::BufferInitRequest request;
  366. request.m_buffer = m_inputAssemblyBufferComposite.get();
  367. request.m_descriptor = RHI::BufferDescriptor{ RHI::BufferBindFlags::InputAssembly, sizeof(bufferData) };
  368. request.m_initialData = &bufferData;
  369. request.m_deviceMask = m_deviceMask_1;
  370. m_inputAssemblyBufferPool->InitBuffer(request);
  371. m_geometryViewComposite.SetDrawArguments(RHI::DrawIndexed(0, 6, 0));
  372. m_geometryViewComposite.SetIndexBufferView({
  373. *m_inputAssemblyBufferComposite,
  374. offsetof(BufferDataCompositePass, m_indices),
  375. sizeof(BufferDataCompositePass::m_indices),
  376. RHI::IndexFormat::Uint16
  377. });
  378. m_geometryViewComposite.AddStreamBufferView({
  379. *m_inputAssemblyBufferComposite,
  380. offsetof(BufferDataCompositePass, m_positions),
  381. sizeof(BufferDataCompositePass::m_positions),
  382. sizeof(VertexPosition)
  383. });
  384. m_geometryViewComposite.AddStreamBufferView({
  385. *m_inputAssemblyBufferComposite,
  386. offsetof(BufferDataCompositePass, m_uvs),
  387. sizeof(BufferDataCompositePass::m_uvs),
  388. sizeof(VertexUV)
  389. });
  390. RHI::InputStreamLayoutBuilder layoutBuilder;
  391. layoutBuilder.AddBuffer()->Channel("POSITION", RHI::Format::R32G32B32_FLOAT);
  392. layoutBuilder.AddBuffer()->Channel("UV", RHI::Format::R32G32_FLOAT);
  393. pipelineStateDescriptor.m_inputStreamLayout = layoutBuilder.End();
  394. RHI::ValidateStreamBufferViews(pipelineStateDescriptor.m_inputStreamLayout, m_geometryViewComposite, m_geometryViewComposite.GetFullStreamBufferIndices());
  395. }
  396. // Load shader and connect inputs
  397. {
  398. const char* compositeShaderFilePath = "Shaders/RHI/multigpucomposite.azshader";
  399. const char* sampleName = "MultiGPUExample";
  400. auto shader = LoadShader(compositeShaderFilePath, sampleName);
  401. if (shader == nullptr)
  402. {
  403. AZ_Error("MultiGPUExampleComponent", false, "Could not load shader");
  404. return;
  405. }
  406. auto shaderVariant = shader->GetVariant(RPI::ShaderAsset::RootShaderVariantStableId);
  407. shaderVariant.ConfigurePipelineState(pipelineStateDescriptor);
  408. RHI::RenderAttachmentLayoutBuilder attachmentsBuilder;
  409. attachmentsBuilder.AddSubpass()->RenderTargetAttachment(m_outputFormat);
  410. [[maybe_unused]] RHI::ResultCode result =
  411. attachmentsBuilder.End(pipelineStateDescriptor.m_renderAttachmentConfiguration.m_renderAttachmentLayout);
  412. AZ_Assert(result == RHI::ResultCode::Success, "Failed to create render attachment layout");
  413. m_pipelineStateComposite = shader->AcquirePipelineState(pipelineStateDescriptor);
  414. if (!m_pipelineStateComposite)
  415. {
  416. AZ_Error(sampleName, false, "Failed to acquire default pipeline state for shader '%s'", compositeShaderFilePath);
  417. return;
  418. }
  419. RHI::ShaderResourceGroupPoolDescriptor srgPoolDescriptor{};
  420. srgPoolDescriptor.m_layout = shader->GetAsset()->FindShaderResourceGroupLayout(AZ::Name { "CompositeSrg" }, shader->GetSupervariantIndex()).get();
  421. srgPoolDescriptor.m_deviceMask = m_deviceMask_1;
  422. m_shaderResourceGroupPoolComposite = aznew RHI::ShaderResourceGroupPool;
  423. m_shaderResourceGroupPoolComposite->Init(srgPoolDescriptor);
  424. m_shaderResourceGroupComposite = aznew RHI::ShaderResourceGroup;
  425. m_shaderResourceGroupPoolComposite->InitGroup(*m_shaderResourceGroupComposite);
  426. m_shaderResourceGroupDataComposite = RHI::ShaderResourceGroupData{*m_shaderResourceGroupPoolComposite};
  427. {
  428. const AZ::Name inputTextureShaderInput{ "m_inputTextureLeft" };
  429. m_textureInputIndices[0] = srgPoolDescriptor.m_layout->FindShaderInputImageIndex(inputTextureShaderInput);
  430. }
  431. {
  432. const AZ::Name inputTextureShaderInput{ "m_inputTextureRight" };
  433. m_textureInputIndices[1] = srgPoolDescriptor.m_layout->FindShaderInputImageIndex(inputTextureShaderInput);
  434. }
  435. }
  436. // Setup ScopeProducer
  437. {
  438. struct ScopeData
  439. {
  440. };
  441. const auto prepareFunction = [this](RHI::FrameGraphInterface frameGraph, [[maybe_unused]] ScopeData& scopeData)
  442. {
  443. {
  444. RHI::ImageScopeAttachmentDescriptor descriptor{};
  445. descriptor.m_attachmentId = m_imageAttachmentIds[0];
  446. descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Load;
  447. descriptor.m_loadStoreAction.m_storeAction = RHI::AttachmentStoreAction::DontCare;
  448. frameGraph.UseShaderAttachment(descriptor, RHI::ScopeAttachmentAccess::Read, RHI::ScopeAttachmentStage::FragmentShader);
  449. }
  450. {
  451. RHI::ImageScopeAttachmentDescriptor descriptor{};
  452. descriptor.m_attachmentId = m_imageAttachmentIds[1];
  453. descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Load;
  454. descriptor.m_loadStoreAction.m_storeAction = RHI::AttachmentStoreAction::DontCare;
  455. frameGraph.UseShaderAttachment(descriptor, RHI::ScopeAttachmentAccess::Read, RHI::ScopeAttachmentStage::FragmentShader);
  456. }
  457. {
  458. RHI::ImageScopeAttachmentDescriptor desc{};
  459. desc.m_attachmentId = m_outputAttachmentId;
  460. frameGraph.UseColorAttachment(desc);
  461. }
  462. frameGraph.SetEstimatedItemCount(1);
  463. };
  464. const auto compileFunction = [this](const RHI::FrameGraphCompileContext& context, [[maybe_unused]] const ScopeData& scopeData)
  465. {
  466. m_shaderResourceGroupDataComposite.SetImageView(m_textureInputIndices[0], context.GetImageView(m_imageAttachmentIds[0]));
  467. m_shaderResourceGroupDataComposite.SetImageView(m_textureInputIndices[1], context.GetImageView(m_imageAttachmentIds[1]));
  468. m_shaderResourceGroupComposite->Compile(m_shaderResourceGroupDataComposite);
  469. };
  470. const auto executeFunction = [=](const RHI::FrameGraphExecuteContext& context, [[maybe_unused]] const ScopeData& scopeData)
  471. {
  472. RHI::CommandList* commandList = context.GetCommandList();
  473. commandList->SetViewports(&m_viewport, 1);
  474. commandList->SetScissors(&m_scissor, 1);
  475. const RHI::DeviceShaderResourceGroup* shaderResourceGroups[] = {
  476. m_shaderResourceGroupComposite->GetDeviceShaderResourceGroup(context.GetDeviceIndex()).get()
  477. };
  478. RHI::DeviceDrawItem drawItem;
  479. drawItem.m_geometryView = m_geometryViewComposite.GetDeviceGeometryView(context.GetDeviceIndex());
  480. drawItem.m_streamIndices = m_geometryViewComposite.GetFullStreamBufferIndices();
  481. drawItem.m_pipelineState = m_pipelineStateComposite->GetDevicePipelineState(context.GetDeviceIndex()).get();
  482. drawItem.m_shaderResourceGroupCount = static_cast<uint8_t>(RHI::ArraySize(shaderResourceGroups));
  483. drawItem.m_shaderResourceGroups = shaderResourceGroups;
  484. commandList->Submit(drawItem);
  485. };
  486. m_scopeProducers.emplace_back(
  487. aznew
  488. RHI::ScopeProducerFunction<ScopeData, decltype(prepareFunction), decltype(compileFunction), decltype(executeFunction)>(
  489. RHI::ScopeId{ "MultiGPUComposite" }, ScopeData{}, prepareFunction, compileFunction, executeFunction));
  490. }
  491. }
  492. void MultiGPUExampleComponent::CreateCopyToGPUScopeProducer()
  493. {
  494. struct ScopeData
  495. {
  496. };
  497. const auto prepareFunction = [this]([[maybe_unused]] RHI::FrameGraphInterface frameGraph, [[maybe_unused]] ScopeData& scopeData)
  498. {
  499. {
  500. RHI::BufferScopeAttachmentDescriptor descriptor{};
  501. descriptor.m_attachmentId = m_bufferAttachmentIds[1];
  502. descriptor.m_bufferViewDescriptor = RHI::BufferViewDescriptor::CreateRaw(0, static_cast<uint32_t>(m_stagingBufferToGPU->GetDescriptor().m_byteCount));
  503. descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Load;
  504. descriptor.m_loadStoreAction.m_storeAction = RHI::AttachmentStoreAction::DontCare;
  505. frameGraph.UseCopyAttachment(descriptor, RHI::ScopeAttachmentAccess::Read);
  506. }
  507. {
  508. RHI::ImageScopeAttachmentDescriptor descriptor{};
  509. descriptor.m_attachmentId = m_imageAttachmentIds[1];
  510. descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::DontCare;
  511. descriptor.m_loadStoreAction.m_storeAction = RHI::AttachmentStoreAction::Store;
  512. frameGraph.UseCopyAttachment(descriptor, RHI::ScopeAttachmentAccess::Write);
  513. }
  514. };
  515. const auto compileFunction = []([[maybe_unused]] const RHI::FrameGraphCompileContext& context, [[maybe_unused]] const ScopeData& scopeData)
  516. {
  517. };
  518. const auto executeFunction = [this](const RHI::FrameGraphExecuteContext& context, [[maybe_unused]] const ScopeData& scopeData)
  519. {
  520. RHI::DeviceCopyBufferToImageDescriptor copyDescriptor{};
  521. copyDescriptor.m_sourceBuffer = m_stagingBufferToGPU->GetDeviceBuffer(context.GetDeviceIndex()).get();
  522. copyDescriptor.m_sourceOffset = 0;
  523. copyDescriptor.m_sourceBytesPerRow = m_imageWidth * sizeof(uint32_t);
  524. copyDescriptor.m_sourceBytesPerImage = static_cast<uint32_t>(m_stagingBufferToGPU->GetDescriptor().m_byteCount);
  525. copyDescriptor.m_sourceSize = RHI::Size{ m_imageWidth, m_imageHeight, 1 };
  526. copyDescriptor.m_destinationImage = m_images[1]->GetDeviceImage(context.GetDeviceIndex()).get();
  527. RHI::DeviceCopyItem copyItem(copyDescriptor);
  528. context.GetCommandList()->Submit(copyItem);
  529. };
  530. m_scopeProducers.emplace_back(
  531. aznew RHI::ScopeProducerFunction<ScopeData, decltype(prepareFunction), decltype(compileFunction), decltype(executeFunction)>(
  532. RHI::ScopeId{ "MultiGPUCopyToGPU" }, ScopeData{}, prepareFunction, compileFunction, executeFunction));
  533. }
  534. void MultiGPUExampleComponent::CreateCopyToCPUScopeProducer()
  535. {
  536. struct ScopeData
  537. {
  538. };
  539. const auto prepareFunction = [this]([[maybe_unused]] RHI::FrameGraphInterface frameGraph, [[maybe_unused]] ScopeData& scopeData)
  540. {
  541. {
  542. RHI::BufferScopeAttachmentDescriptor descriptor{};
  543. descriptor.m_attachmentId = m_bufferAttachmentIds[0];
  544. descriptor.m_bufferViewDescriptor = RHI::BufferViewDescriptor::CreateRaw(0, static_cast<uint32_t>(m_stagingBufferToCPU->GetDescriptor().m_byteCount));
  545. descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::DontCare;
  546. descriptor.m_loadStoreAction.m_storeAction = RHI::AttachmentStoreAction::Store;
  547. frameGraph.UseCopyAttachment(descriptor, RHI::ScopeAttachmentAccess::Write);
  548. }
  549. {
  550. RHI::ImageScopeAttachmentDescriptor descriptor{};
  551. descriptor.m_attachmentId = m_imageAttachmentIds[1];
  552. descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Load;
  553. descriptor.m_loadStoreAction.m_storeAction = RHI::AttachmentStoreAction::DontCare;
  554. frameGraph.UseCopyAttachment(descriptor, RHI::ScopeAttachmentAccess::Read);
  555. }
  556. };
  557. const auto compileFunction = []([[maybe_unused]] const RHI::FrameGraphCompileContext& context, [[maybe_unused]] const ScopeData& scopeData)
  558. {
  559. };
  560. const auto executeFunction = [this](const RHI::FrameGraphExecuteContext& context, [[maybe_unused]] const ScopeData& scopeData)
  561. {
  562. RHI::DeviceCopyImageToBufferDescriptor copyDescriptor{};
  563. copyDescriptor.m_sourceImage = m_images[1]->GetDeviceImage(context.GetDeviceIndex()).get();
  564. copyDescriptor.m_sourceSize = RHI::Size{ m_imageWidth, m_imageHeight, 1 };
  565. copyDescriptor.m_destinationBuffer = m_stagingBufferToCPU->GetDeviceBuffer(context.GetDeviceIndex()).get();
  566. copyDescriptor.m_destinationOffset = 0;
  567. copyDescriptor.m_destinationBytesPerRow = m_imageWidth * sizeof(uint32_t);
  568. copyDescriptor.m_destinationBytesPerImage = static_cast<uint32_t>(m_stagingBufferToCPU->GetDescriptor().m_byteCount);
  569. copyDescriptor.m_destinationFormat = m_outputFormat;
  570. RHI::DeviceCopyItem copyItem(copyDescriptor);
  571. context.GetCommandList()->Submit(copyItem);
  572. };
  573. m_scopeProducers.emplace_back(
  574. aznew RHI::ScopeProducerFunction<ScopeData, decltype(prepareFunction), decltype(compileFunction), decltype(executeFunction)>(
  575. RHI::ScopeId{ "MultiGPUCopyToCPU" }, ScopeData{}, prepareFunction, compileFunction, executeFunction, 1));
  576. }
  577. } // namespace AtomSampleViewer