MultiGPUExampleComponent.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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_stagingBufferPoolToGPU->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->GetBufferView(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_stagingBufferPoolToGPU->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_stagingBufferPoolToCPU->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_stagingBufferPoolToCPU->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_stagingBufferPoolToCPU->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_stagingBufferPoolToGPU->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_stagingBufferPoolToCPU->GetDeviceBufferPool(1)->UnmapBuffer(*m_stagingBufferToCPU->GetDeviceBuffer(1));
  152. m_stagingBufferPoolToGPU->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 GPU
  183. {
  184. m_stagingBufferPoolToGPU = 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_stagingBufferPoolToGPU->Init(bufferPoolDesc) != RHI::ResultCode::Success)
  191. {
  192. AZ_Error("MultiGPUExampleComponent", false, "StagingBufferPool was not initialized");
  193. }
  194. }
  195. // Create staging buffer pool for buffer copy to the CPU
  196. {
  197. m_stagingBufferPoolToCPU = aznew RHI::BufferPool;
  198. RHI::BufferPoolDescriptor bufferPoolDesc;
  199. bufferPoolDesc.m_bindFlags = stagingBufferBindFlags;
  200. bufferPoolDesc.m_heapMemoryLevel = RHI::HeapMemoryLevel::Host;
  201. bufferPoolDesc.m_hostMemoryAccess = RHI::HostMemoryAccess::Read;
  202. bufferPoolDesc.m_deviceMask = m_deviceMask;
  203. if (m_stagingBufferPoolToCPU->Init(bufferPoolDesc) != RHI::ResultCode::Success)
  204. {
  205. AZ_Error("MultiGPUExampleComponent", false, "StagingBufferPoolToCPU was not initialized");
  206. }
  207. }
  208. // Setup main and secondary pipeline
  209. CreateRenderScopeProducer();
  210. CreateCopyToCPUScopeProducer();
  211. CreateCopyToGPUScopeProducer();
  212. CreateCompositeScopeProducer();
  213. RHI::RHISystemNotificationBus::Handler::BusConnect();
  214. }
  215. void MultiGPUExampleComponent::Deactivate()
  216. {
  217. m_inputAssemblyBuffer = nullptr;
  218. m_inputAssemblyBufferPool = nullptr;
  219. m_pipelineState = nullptr;
  220. m_shaderResourceGroupShared = nullptr;
  221. m_stagingBufferPoolToGPU = nullptr;
  222. m_stagingBufferPoolToCPU = nullptr;
  223. m_stagingBufferToGPU = nullptr;
  224. m_stagingBufferToCPU = nullptr;
  225. m_inputAssemblyBufferComposite = nullptr;
  226. m_pipelineStateComposite = nullptr;
  227. m_shaderResourceGroupComposite = nullptr;
  228. m_shaderResourceGroupDataComposite = RHI::ShaderResourceGroupData{};
  229. m_shaderResourceGroupPoolComposite = nullptr;
  230. RHI::RHISystemNotificationBus::Handler::BusDisconnect();
  231. m_windowContext = nullptr;
  232. m_scopeProducers.clear();
  233. m_secondaryScopeProducers.clear();
  234. }
  235. void MultiGPUExampleComponent::CreateRenderScopeProducer()
  236. {
  237. RHI::PipelineStateDescriptorForDraw pipelineStateDescriptor;
  238. {
  239. m_inputAssemblyBufferPool = aznew RHI::BufferPool;
  240. RHI::BufferPoolDescriptor bufferPoolDesc;
  241. bufferPoolDesc.m_bindFlags = RHI::BufferBindFlags::InputAssembly;
  242. bufferPoolDesc.m_heapMemoryLevel = RHI::HeapMemoryLevel::Device;
  243. bufferPoolDesc.m_deviceMask = m_deviceMask;
  244. m_inputAssemblyBufferPool->Init(bufferPoolDesc);
  245. BufferDataTrianglePass bufferData;
  246. SetVertexPosition(bufferData.m_positions.data(), 0, 0.0, 0.5, 0.0);
  247. SetVertexPosition(bufferData.m_positions.data(), 1, -0.5, -0.5, 0.0);
  248. SetVertexPosition(bufferData.m_positions.data(), 2, 0.5, -0.5, 0.0);
  249. SetVertexColor(bufferData.m_colors.data(), 0, 1.0, 0.0, 0.0, 1.0);
  250. SetVertexColor(bufferData.m_colors.data(), 1, 0.0, 1.0, 0.0, 1.0);
  251. SetVertexColor(bufferData.m_colors.data(), 2, 0.0, 0.0, 1.0, 1.0);
  252. SetVertexIndexIncreasing(bufferData.m_indices.data(), bufferData.m_indices.size());
  253. m_inputAssemblyBuffer = aznew RHI::Buffer;
  254. RHI::BufferInitRequest request;
  255. request.m_buffer = m_inputAssemblyBuffer.get();
  256. request.m_descriptor = RHI::BufferDescriptor{ RHI::BufferBindFlags::InputAssembly, sizeof(bufferData) };
  257. request.m_initialData = &bufferData;
  258. m_inputAssemblyBufferPool->InitBuffer(request);
  259. m_geometryView.SetDrawArguments(RHI::DrawIndexed(0, 3, 0));
  260. m_geometryView.SetIndexBufferView({
  261. *m_inputAssemblyBuffer,
  262. offsetof(BufferDataTrianglePass, m_indices),
  263. sizeof(BufferDataTrianglePass::m_indices),
  264. RHI::IndexFormat::Uint16
  265. });
  266. m_geometryView.AddStreamBufferView({
  267. *m_inputAssemblyBuffer,
  268. offsetof(BufferDataTrianglePass, m_positions),
  269. sizeof(BufferDataTrianglePass::m_positions),
  270. sizeof(VertexPosition)
  271. });
  272. m_geometryView.AddStreamBufferView({
  273. *m_inputAssemblyBuffer,
  274. offsetof(BufferDataTrianglePass, m_colors),
  275. sizeof(BufferDataTrianglePass::m_colors),
  276. sizeof(VertexColor)
  277. });
  278. RHI::InputStreamLayoutBuilder layoutBuilder;
  279. layoutBuilder.AddBuffer()->Channel("POSITION", RHI::Format::R32G32B32_FLOAT);
  280. layoutBuilder.AddBuffer()->Channel("COLOR", RHI::Format::R32G32B32A32_FLOAT);
  281. pipelineStateDescriptor.m_inputStreamLayout = layoutBuilder.End();
  282. RHI::ValidateStreamBufferViews(pipelineStateDescriptor.m_inputStreamLayout, m_geometryView, m_geometryView.GetFullStreamBufferIndices());
  283. }
  284. {
  285. const char* triangleShaderFilePath = "Shaders/RHI/triangle.azshader";
  286. const char* sampleName = "MultiGPUExample";
  287. auto shader = LoadShader(triangleShaderFilePath, sampleName);
  288. if (shader == nullptr)
  289. return;
  290. auto shaderOptionGroup = shader->CreateShaderOptionGroup();
  291. shaderOptionGroup.SetUnspecifiedToDefaultValues();
  292. // This is an example of how to set different shader options when searching for the shader variant you want to display
  293. // Searching by id is simple, but suboptimal. Here it's only used to demonstrate the principle,
  294. // but in practice the ShaderOptionIndex and the ShaderOptionValue should be cached for better performance
  295. // You can also try DrawMode::Green, DrawMode::Blue or DrawMode::White. The specified color will appear on top of the triangle.
  296. shaderOptionGroup.SetValue(AZ::Name("o_drawMode"), AZ::Name("DrawMode::Red"));
  297. auto shaderVariant = shader->GetVariant(shaderOptionGroup.GetShaderVariantId());
  298. shaderVariant.ConfigurePipelineState(pipelineStateDescriptor);
  299. RHI::RenderAttachmentLayoutBuilder attachmentsBuilder;
  300. attachmentsBuilder.AddSubpass()
  301. ->RenderTargetAttachment(m_outputFormat);
  302. [[maybe_unused]] RHI::ResultCode result = attachmentsBuilder.End(pipelineStateDescriptor.m_renderAttachmentConfiguration.m_renderAttachmentLayout);
  303. AZ_Assert(result == RHI::ResultCode::Success, "Failed to create render attachment layout");
  304. m_pipelineState = shader->AcquirePipelineState(pipelineStateDescriptor);
  305. if (!m_pipelineState)
  306. {
  307. AZ_Error(sampleName, false, "Failed to acquire default pipeline state for shader '%s'", triangleShaderFilePath);
  308. return;
  309. }
  310. m_shaderResourceGroupShared = CreateShaderResourceGroup(shader, "TriangleInstanceSrg", sampleName);
  311. const Name objectMatrixConstantId{ "m_objectMatrix" };
  312. FindShaderInputIndex(&m_objectMatrixConstantIndex, m_shaderResourceGroupShared, objectMatrixConstantId, sampleName);
  313. // In practice m_shaderResourceGroupShared should be one of the cached SRGs owned by the DrawItem
  314. if (!shaderVariant.IsFullyBaked() && m_shaderResourceGroupShared->HasShaderVariantKeyFallbackEntry())
  315. {
  316. // Normally if the requested variant isn't an exact match we have to set it by SetShaderVariantKeyFallbackValue
  317. // In most cases this should be the preferred behavior:
  318. m_shaderResourceGroupShared->SetShaderVariantKeyFallbackValue(shaderOptionGroup.GetShaderVariantKeyFallbackValue());
  319. AZ_Warning(
  320. sampleName, false, "Check the Triangle.shader file - some program variants haven't been baked ('%s')",
  321. triangleShaderFilePath);
  322. }
  323. }
  324. // Creates two scopes for rendering the halves of the triangle.
  325. {
  326. struct ScopeData
  327. {
  328. bool second{false};
  329. };
  330. const auto prepareFunction = [this](RHI::FrameGraphInterface frameGraph, [[maybe_unused]] ScopeData& scopeData)
  331. {
  332. // Binds the swap chain as a color attachment. Clears it to black.
  333. RHI::ImageScopeAttachmentDescriptor descriptor;
  334. descriptor.m_attachmentId = m_imageAttachmentIds[scopeData.second];
  335. descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Clear;
  336. descriptor.m_loadStoreAction.m_storeAction = RHI::AttachmentStoreAction::Store;
  337. descriptor.m_loadStoreAction.m_clearValue.m_vector4Uint = {0, 0, 0, 0};
  338. frameGraph.UseColorAttachment(descriptor);
  339. // We will submit a single draw item.
  340. frameGraph.SetEstimatedItemCount(1);
  341. };
  342. RHI::EmptyCompileFunction<ScopeData> compileFunction;
  343. const auto executeFunction = [this](const RHI::FrameGraphExecuteContext& context, [[maybe_unused]] const ScopeData& scopeData)
  344. {
  345. RHI::CommandList* commandList = context.GetCommandList();
  346. // Set persistent viewport and scissor state.
  347. commandList->SetViewports(&m_viewport, 1);
  348. commandList->SetScissors(&m_scissors[int(scopeData.second)], 1);
  349. const RHI::DeviceShaderResourceGroup* shaderResourceGroups[] = {
  350. m_shaderResourceGroupShared->GetRHIShaderResourceGroup()->GetDeviceShaderResourceGroup(context.GetDeviceIndex()).get()
  351. };
  352. // Submit the triangle draw item.
  353. RHI::DeviceDrawItem drawItem;
  354. drawItem.m_geometryView = m_geometryView.GetDeviceGeometryView(context.GetDeviceIndex());
  355. drawItem.m_streamIndices = m_geometryView.GetFullStreamBufferIndices();
  356. drawItem.m_pipelineState = m_pipelineState->GetDevicePipelineState(context.GetDeviceIndex()).get();
  357. drawItem.m_shaderResourceGroupCount = static_cast<uint8_t>(RHI::ArraySize(shaderResourceGroups));
  358. drawItem.m_shaderResourceGroups = shaderResourceGroups;
  359. commandList->Submit(drawItem);
  360. };
  361. m_scopeProducers.emplace_back(
  362. aznew
  363. RHI::ScopeProducerFunction<ScopeData, decltype(prepareFunction), decltype(compileFunction), decltype(executeFunction)>(
  364. RHI::ScopeId{ "MultiGPUTriangle0" }, ScopeData{}, prepareFunction, compileFunction, executeFunction, 0));
  365. m_scopeProducers.emplace_back(
  366. aznew
  367. RHI::ScopeProducerFunction<ScopeData, decltype(prepareFunction), decltype(compileFunction), decltype(executeFunction)>(
  368. RHI::ScopeId{ "MultiGPUTriangle1" }, ScopeData{true}, prepareFunction, compileFunction, executeFunction, 1));
  369. }
  370. }
  371. void MultiGPUExampleComponent::CreateCompositeScopeProducer()
  372. {
  373. BufferDataCompositePass bufferData;
  374. RHI::PipelineStateDescriptorForDraw pipelineStateDescriptor;
  375. // Setup input assembly for fullscreen pass
  376. {
  377. SetFullScreenRect(bufferData.m_positions.data(), bufferData.m_uvs.data(), bufferData.m_indices.data());
  378. m_inputAssemblyBufferComposite = aznew RHI::Buffer;
  379. RHI::BufferInitRequest request;
  380. request.m_buffer = m_inputAssemblyBufferComposite.get();
  381. request.m_descriptor = RHI::BufferDescriptor{ RHI::BufferBindFlags::InputAssembly, sizeof(bufferData) };
  382. request.m_initialData = &bufferData;
  383. request.m_deviceMask = m_deviceMask_1;
  384. m_inputAssemblyBufferPool->InitBuffer(request);
  385. m_geometryViewComposite.SetDrawArguments(RHI::DrawIndexed(0, 6, 0));
  386. m_geometryViewComposite.SetIndexBufferView({
  387. *m_inputAssemblyBufferComposite,
  388. offsetof(BufferDataCompositePass, m_indices),
  389. sizeof(BufferDataCompositePass::m_indices),
  390. RHI::IndexFormat::Uint16
  391. });
  392. m_geometryViewComposite.AddStreamBufferView({
  393. *m_inputAssemblyBufferComposite,
  394. offsetof(BufferDataCompositePass, m_positions),
  395. sizeof(BufferDataCompositePass::m_positions),
  396. sizeof(VertexPosition)
  397. });
  398. m_geometryViewComposite.AddStreamBufferView({
  399. *m_inputAssemblyBufferComposite,
  400. offsetof(BufferDataCompositePass, m_uvs),
  401. sizeof(BufferDataCompositePass::m_uvs),
  402. sizeof(VertexUV)
  403. });
  404. RHI::InputStreamLayoutBuilder layoutBuilder;
  405. layoutBuilder.AddBuffer()->Channel("POSITION", RHI::Format::R32G32B32_FLOAT);
  406. layoutBuilder.AddBuffer()->Channel("UV", RHI::Format::R32G32_FLOAT);
  407. pipelineStateDescriptor.m_inputStreamLayout = layoutBuilder.End();
  408. RHI::ValidateStreamBufferViews(pipelineStateDescriptor.m_inputStreamLayout, m_geometryViewComposite, m_geometryViewComposite.GetFullStreamBufferIndices());
  409. }
  410. // Load shader and connect inputs
  411. {
  412. const char* compositeShaderFilePath = "Shaders/RHI/multigpucomposite.azshader";
  413. const char* sampleName = "MultiGPUExample";
  414. auto shader = LoadShader(compositeShaderFilePath, sampleName);
  415. if (shader == nullptr)
  416. {
  417. AZ_Error("MultiGPUExampleComponent", false, "Could not load shader");
  418. return;
  419. }
  420. auto shaderVariant = shader->GetVariant(RPI::ShaderAsset::RootShaderVariantStableId);
  421. shaderVariant.ConfigurePipelineState(pipelineStateDescriptor);
  422. RHI::RenderAttachmentLayoutBuilder attachmentsBuilder;
  423. attachmentsBuilder.AddSubpass()->RenderTargetAttachment(m_outputFormat);
  424. [[maybe_unused]] RHI::ResultCode result =
  425. attachmentsBuilder.End(pipelineStateDescriptor.m_renderAttachmentConfiguration.m_renderAttachmentLayout);
  426. AZ_Assert(result == RHI::ResultCode::Success, "Failed to create render attachment layout");
  427. m_pipelineStateComposite = shader->AcquirePipelineState(pipelineStateDescriptor);
  428. if (!m_pipelineStateComposite)
  429. {
  430. AZ_Error(sampleName, false, "Failed to acquire default pipeline state for shader '%s'", compositeShaderFilePath);
  431. return;
  432. }
  433. RHI::ShaderResourceGroupPoolDescriptor srgPoolDescriptor{};
  434. srgPoolDescriptor.m_layout = shader->GetAsset()->FindShaderResourceGroupLayout(AZ::Name { "CompositeSrg" }, shader->GetSupervariantIndex()).get();
  435. srgPoolDescriptor.m_deviceMask = m_deviceMask_1;
  436. m_shaderResourceGroupPoolComposite = aznew RHI::ShaderResourceGroupPool;
  437. m_shaderResourceGroupPoolComposite->Init(srgPoolDescriptor);
  438. m_shaderResourceGroupComposite = aznew RHI::ShaderResourceGroup;
  439. m_shaderResourceGroupPoolComposite->InitGroup(*m_shaderResourceGroupComposite);
  440. m_shaderResourceGroupDataComposite = RHI::ShaderResourceGroupData{*m_shaderResourceGroupPoolComposite};
  441. {
  442. const AZ::Name inputTextureShaderInput{ "m_inputTextureLeft" };
  443. m_textureInputIndices[0] = srgPoolDescriptor.m_layout->FindShaderInputImageIndex(inputTextureShaderInput);
  444. }
  445. {
  446. const AZ::Name inputTextureShaderInput{ "m_inputTextureRight" };
  447. m_textureInputIndices[1] = srgPoolDescriptor.m_layout->FindShaderInputImageIndex(inputTextureShaderInput);
  448. }
  449. }
  450. // Setup ScopeProducer
  451. {
  452. struct ScopeData
  453. {
  454. };
  455. const auto prepareFunction = [this](RHI::FrameGraphInterface frameGraph, [[maybe_unused]] ScopeData& scopeData)
  456. {
  457. {
  458. RHI::ImageScopeAttachmentDescriptor descriptor{};
  459. descriptor.m_attachmentId = m_imageAttachmentIds[0];
  460. descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Load;
  461. descriptor.m_loadStoreAction.m_storeAction = RHI::AttachmentStoreAction::DontCare;
  462. frameGraph.UseShaderAttachment(descriptor, RHI::ScopeAttachmentAccess::Read, RHI::ScopeAttachmentStage::FragmentShader);
  463. }
  464. {
  465. RHI::ImageScopeAttachmentDescriptor descriptor{};
  466. descriptor.m_attachmentId = m_imageAttachmentIds[1];
  467. descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Load;
  468. descriptor.m_loadStoreAction.m_storeAction = RHI::AttachmentStoreAction::DontCare;
  469. frameGraph.UseShaderAttachment(descriptor, RHI::ScopeAttachmentAccess::Read, RHI::ScopeAttachmentStage::FragmentShader);
  470. }
  471. {
  472. RHI::ImageScopeAttachmentDescriptor desc{};
  473. desc.m_attachmentId = m_outputAttachmentId;
  474. frameGraph.UseColorAttachment(desc);
  475. }
  476. frameGraph.SetEstimatedItemCount(1);
  477. };
  478. const auto compileFunction = [this](const RHI::FrameGraphCompileContext& context, [[maybe_unused]] const ScopeData& scopeData)
  479. {
  480. m_shaderResourceGroupDataComposite.SetImageView(m_textureInputIndices[0], context.GetImageView(m_imageAttachmentIds[0]));
  481. m_shaderResourceGroupDataComposite.SetImageView(m_textureInputIndices[1], context.GetImageView(m_imageAttachmentIds[1]));
  482. m_shaderResourceGroupComposite->Compile(m_shaderResourceGroupDataComposite);
  483. };
  484. const auto executeFunction = [this](const RHI::FrameGraphExecuteContext& context, [[maybe_unused]] const ScopeData& scopeData)
  485. {
  486. RHI::CommandList* commandList = context.GetCommandList();
  487. commandList->SetViewports(&m_viewport, 1);
  488. commandList->SetScissors(&m_scissor, 1);
  489. const RHI::DeviceShaderResourceGroup* shaderResourceGroups[] = {
  490. m_shaderResourceGroupComposite->GetDeviceShaderResourceGroup(context.GetDeviceIndex()).get()
  491. };
  492. RHI::DeviceDrawItem drawItem;
  493. drawItem.m_geometryView = m_geometryViewComposite.GetDeviceGeometryView(context.GetDeviceIndex());
  494. drawItem.m_streamIndices = m_geometryViewComposite.GetFullStreamBufferIndices();
  495. drawItem.m_pipelineState = m_pipelineStateComposite->GetDevicePipelineState(context.GetDeviceIndex()).get();
  496. drawItem.m_shaderResourceGroupCount = static_cast<uint8_t>(RHI::ArraySize(shaderResourceGroups));
  497. drawItem.m_shaderResourceGroups = shaderResourceGroups;
  498. commandList->Submit(drawItem);
  499. };
  500. m_scopeProducers.emplace_back(
  501. aznew
  502. RHI::ScopeProducerFunction<ScopeData, decltype(prepareFunction), decltype(compileFunction), decltype(executeFunction)>(
  503. RHI::ScopeId{ "MultiGPUComposite" }, ScopeData{}, prepareFunction, compileFunction, executeFunction));
  504. }
  505. }
  506. void MultiGPUExampleComponent::CreateCopyToGPUScopeProducer()
  507. {
  508. struct ScopeData
  509. {
  510. };
  511. const auto prepareFunction = [this]([[maybe_unused]] RHI::FrameGraphInterface frameGraph, [[maybe_unused]] ScopeData& scopeData)
  512. {
  513. {
  514. RHI::ImageScopeAttachmentDescriptor descriptor{};
  515. descriptor.m_attachmentId = m_imageAttachmentIds[1];
  516. descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::DontCare;
  517. descriptor.m_loadStoreAction.m_storeAction = RHI::AttachmentStoreAction::Store;
  518. frameGraph.UseCopyAttachment(descriptor, RHI::ScopeAttachmentAccess::Write);
  519. }
  520. };
  521. const auto compileFunction = []([[maybe_unused]] const RHI::FrameGraphCompileContext& context, [[maybe_unused]] const ScopeData& scopeData)
  522. {
  523. };
  524. const auto executeFunction = [this](const RHI::FrameGraphExecuteContext& context, [[maybe_unused]] const ScopeData& scopeData)
  525. {
  526. RHI::DeviceCopyBufferToImageDescriptor copyDescriptor{};
  527. copyDescriptor.m_sourceBuffer = m_stagingBufferToGPU->GetDeviceBuffer(context.GetDeviceIndex()).get();
  528. copyDescriptor.m_sourceOffset = 0;
  529. copyDescriptor.m_sourceBytesPerRow = m_imageWidth * sizeof(uint32_t);
  530. copyDescriptor.m_sourceBytesPerImage = static_cast<uint32_t>(m_stagingBufferToGPU->GetDescriptor().m_byteCount);
  531. copyDescriptor.m_sourceSize = RHI::Size{ m_imageWidth, m_imageHeight, 1 };
  532. copyDescriptor.m_sourceFormat = m_images[1]->GetDeviceImage(context.GetDeviceIndex())->GetDescriptor().m_format;
  533. copyDescriptor.m_destinationImage = m_images[1]->GetDeviceImage(context.GetDeviceIndex()).get();
  534. RHI::DeviceCopyItem copyItem(copyDescriptor);
  535. context.GetCommandList()->Submit(copyItem);
  536. };
  537. m_scopeProducers.emplace_back(
  538. aznew RHI::ScopeProducerFunction<ScopeData, decltype(prepareFunction), decltype(compileFunction), decltype(executeFunction)>(
  539. RHI::ScopeId{ "MultiGPUCopyToGPU" }, ScopeData{}, prepareFunction, compileFunction, executeFunction));
  540. }
  541. void MultiGPUExampleComponent::CreateCopyToCPUScopeProducer()
  542. {
  543. struct ScopeData
  544. {
  545. };
  546. const auto prepareFunction = [this]([[maybe_unused]] RHI::FrameGraphInterface frameGraph, [[maybe_unused]] ScopeData& scopeData)
  547. {
  548. {
  549. RHI::ImageScopeAttachmentDescriptor descriptor{};
  550. descriptor.m_attachmentId = m_imageAttachmentIds[1];
  551. descriptor.m_loadStoreAction.m_loadAction = RHI::AttachmentLoadAction::Load;
  552. descriptor.m_loadStoreAction.m_storeAction = RHI::AttachmentStoreAction::DontCare;
  553. frameGraph.UseCopyAttachment(descriptor, RHI::ScopeAttachmentAccess::Read);
  554. }
  555. };
  556. const auto compileFunction = []([[maybe_unused]] const RHI::FrameGraphCompileContext& context, [[maybe_unused]] const ScopeData& scopeData)
  557. {
  558. };
  559. const auto executeFunction = [this](const RHI::FrameGraphExecuteContext& context, [[maybe_unused]] const ScopeData& scopeData)
  560. {
  561. RHI::DeviceCopyImageToBufferDescriptor copyDescriptor{};
  562. copyDescriptor.m_sourceImage = m_images[1]->GetDeviceImage(context.GetDeviceIndex()).get();
  563. copyDescriptor.m_sourceSize = RHI::Size{ m_imageWidth, m_imageHeight, 1 };
  564. copyDescriptor.m_destinationBuffer = m_stagingBufferToCPU->GetDeviceBuffer(context.GetDeviceIndex()).get();
  565. copyDescriptor.m_destinationOffset = 0;
  566. copyDescriptor.m_destinationBytesPerRow = m_imageWidth * sizeof(uint32_t);
  567. copyDescriptor.m_destinationBytesPerImage = static_cast<uint32_t>(m_stagingBufferToCPU->GetDescriptor().m_byteCount);
  568. copyDescriptor.m_destinationFormat = m_outputFormat;
  569. RHI::DeviceCopyItem copyItem(copyDescriptor);
  570. context.GetCommandList()->Submit(copyItem);
  571. };
  572. m_scopeProducers.emplace_back(
  573. aznew RHI::ScopeProducerFunction<ScopeData, decltype(prepareFunction), decltype(compileFunction), decltype(executeFunction)>(
  574. RHI::ScopeId{ "MultiGPUCopyToCPU" }, ScopeData{}, prepareFunction, compileFunction, executeFunction, 1));
  575. }
  576. } // namespace AtomSampleViewer