Skip to content

Commit 1076f41

Browse files
Fixes to multi-device resources (o3de#17638)
Signed-off-by: Martin Winter <[email protected]> Co-authored-by: Joerg H. Mueller <[email protected]>
1 parent fcff71b commit 1076f41

11 files changed

+64
-58
lines changed

Gems/Atom/RHI/Code/Include/Atom/RHI/IndirectArguments.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace AZ::RHI
2020

2121
IndirectArgumentsTemplate(
2222
uint32_t maxSequenceCount,
23-
const IndirectBufferView& indirectBuffer,
23+
const IndirectBufferViewClass& indirectBuffer,
2424
uint64_t indirectBufferByteOffset)
2525
: IndirectArgumentsTemplate(
2626
maxSequenceCount,
@@ -32,9 +32,9 @@ namespace AZ::RHI
3232

3333
IndirectArgumentsTemplate(
3434
uint32_t maxSequenceCount,
35-
const IndirectBufferView& indirectBuffer,
35+
const IndirectBufferViewClass& indirectBuffer,
3636
uint64_t indirectBufferByteOffset,
37-
const Buffer* countBuffer,
37+
const BufferClass* countBuffer,
3838
uint64_t countBufferByteOffset)
3939
: m_maxSequenceCount(maxSequenceCount)
4040
, m_indirectBufferView(&indirectBuffer)

Gems/Atom/RHI/Code/Include/Atom/RHI/MultiDeviceBufferPool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace AZ::RHI
2020
struct MultiDeviceBufferMapResponse
2121
{
2222
//! Will hold the mapped data for each device selected in the MultiDeviceBuffer
23-
AZStd::vector<void*> m_data;
23+
AZStd::unordered_map<int, void*> m_data;
2424
};
2525

2626
using MultiDeviceBufferInitRequest = BufferInitRequestTemplate<MultiDeviceBuffer>;

Gems/Atom/RHI/Code/Include/Atom/RHI/MultiDeviceDispatchItem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ namespace AZ::RHI
5353
case DispatchType::Direct:
5454
return DispatchArguments(m_direct);
5555
case DispatchType::Indirect:
56-
return DispatchArguments(DispatchIndirect{m_mdIndirect.m_maxSequenceCount, m_mdIndirect.m_indirectBufferView->GetDeviceIndirectBufferView(deviceIndex), m_mdIndirect.m_indirectBufferByteOffset, m_mdIndirect.m_countBuffer->GetDeviceBuffer(deviceIndex).get(), m_mdIndirect.m_countBufferByteOffset});
56+
return DispatchArguments(DispatchIndirect{m_mdIndirect.m_maxSequenceCount, m_mdIndirect.m_indirectBufferView->GetDeviceIndirectBufferView(deviceIndex), m_mdIndirect.m_indirectBufferByteOffset, m_mdIndirect.m_countBuffer ? m_mdIndirect.m_countBuffer->GetDeviceBuffer(deviceIndex).get() : nullptr, m_mdIndirect.m_countBufferByteOffset});
5757
default:
5858
return DispatchArguments();
5959
}

Gems/Atom/RHI/Code/Include/Atom/RHI/MultiDeviceIndirectBufferView.h

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,55 @@ namespace AZ::RHI
3232
uint32_t byteCount,
3333
uint32_t byteStride);
3434

35+
//! The mutex stops the default generation
36+
MultiDeviceIndirectBufferView(const MultiDeviceIndirectBufferView& other)
37+
: m_hash{ other.m_hash }
38+
, m_mdBuffer{ other.m_mdBuffer }
39+
, m_mdSignature{ other.m_mdSignature }
40+
, m_byteOffset{ other.m_byteOffset }
41+
, m_byteCount{ other.m_byteCount }
42+
, m_byteStride{ other.m_byteStride }
43+
{
44+
}
45+
46+
MultiDeviceIndirectBufferView& operator=(const MultiDeviceIndirectBufferView& other)
47+
{
48+
this->m_hash = other.m_hash;
49+
this->m_mdBuffer = other.m_mdBuffer;
50+
this->m_mdSignature = other.m_mdSignature;
51+
this->m_byteOffset = other.m_byteOffset;
52+
this->m_byteCount = other.m_byteCount;
53+
this->m_byteStride = other.m_byteStride;
54+
55+
return *this;
56+
}
57+
3558
//! Returns the device-specific IndirectBufferView for the given index
36-
IndirectBufferView GetDeviceIndirectBufferView(int deviceIndex) const
59+
const IndirectBufferView& GetDeviceIndirectBufferView(int deviceIndex) const
3760
{
3861
AZ_Error("MultiDeviceIndirectBufferView", m_mdSignature, "No MultiDeviceIndirectBufferSignature available\n");
3962
AZ_Error("MultiDeviceIndirectBufferView", m_mdBuffer, "No MultiDeviceBuffer available\n");
4063

41-
return IndirectBufferView(
42-
*m_mdBuffer->GetDeviceBuffer(deviceIndex),
43-
*m_mdSignature->GetDeviceIndirectBufferSignature(deviceIndex),
44-
m_byteOffset,
45-
m_byteCount,
46-
m_byteStride);
64+
AZStd::lock_guard lock(m_bufferViewMutex);
65+
auto iterator{ m_cache.find(deviceIndex) };
66+
if (iterator == m_cache.end())
67+
{
68+
//! Buffer view is not yet in the cache
69+
auto [new_iterator, inserted]{ m_cache.insert(AZStd::make_pair(
70+
deviceIndex,
71+
IndirectBufferView(
72+
*m_mdBuffer->GetDeviceBuffer(deviceIndex),
73+
*m_mdSignature->GetDeviceIndirectBufferSignature(deviceIndex),
74+
m_byteOffset,
75+
m_byteCount,
76+
m_byteStride))) };
77+
if (inserted)
78+
{
79+
return new_iterator->second;
80+
}
81+
}
82+
83+
return iterator->second;
4784
}
4885

4986
//! Returns the hash of the view. This hash is precomputed at creation time.
@@ -72,5 +109,9 @@ namespace AZ::RHI
72109
uint32_t m_byteOffset = 0;
73110
uint32_t m_byteCount = 0;
74111
uint32_t m_byteStride = 0;
112+
113+
//! Safe-guard access to IndirectBufferView cache during parallel access
114+
mutable AZStd::mutex m_bufferViewMutex{};
115+
mutable AZStd::unordered_map<int, IndirectBufferView> m_cache;
75116
};
76117
} // namespace AZ::RHI

Gems/Atom/RHI/Code/Include/Atom/RHI/MultiDeviceShaderResourceGroupData.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ namespace AZ::RHI
5454
ShaderInputConstantIndex FindShaderInputConstantIndex(const Name& name) const;
5555

5656
//! Sets one image view for the given shader input index.
57-
bool SetImageView(ShaderInputImageIndex inputIndex, const MultiDeviceImageView* imageView, uint32_t arrayIndex);
57+
bool SetImageView(ShaderInputImageIndex inputIndex, const MultiDeviceImageView* imageView, uint32_t arrayIndex = 0);
5858

5959
//! Sets an array of image view for the given shader input index.
6060
bool SetImageViewArray(

Gems/Atom/RHI/Code/Include/Atom/RHI/MultiDeviceSwapChain.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ namespace AZ::RHI
4040
//! it is explicitly initialized with just a deviceIndex
4141
ResultCode Init(int deviceIndex, const SwapChainDescriptor& descriptor);
4242

43+
Ptr<SwapChain> GetDeviceSwapChain() const;
44+
4345
//! Presents the swap chain to the display, and rotates the images.
4446
void Present();
4547

Gems/Atom/RHI/Code/Source/RHI/MultiDeviceBufferPool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ namespace AZ::RHI
230230
}
231231
else
232232
{
233-
response.m_data.push_back(deviceMapResponse.m_data);
233+
response.m_data[deviceIndex] = deviceMapResponse.m_data;
234234
}
235235

236236
return resultCode;

Gems/Atom/RHI/Code/Source/RHI/MultiDeviceIndirectBufferSignature.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ namespace AZ::RHI
1616
IndirectBufferSignatureDescriptor MultiDeviceIndirectBufferSignatureDescriptor::GetDeviceIndirectBufferSignatureDescriptor(
1717
int deviceIndex) const
1818
{
19-
AZ_Assert(m_pipelineState, "No MultiDevicePipelineState available\n");
20-
2119
IndirectBufferSignatureDescriptor descriptor{ m_layout };
2220

2321
if (m_pipelineState)

Gems/Atom/RHI/Code/Source/RHI/MultiDeviceShaderResourceGroupData.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ namespace AZ::RHI
7575
}
7676

7777
bool MultiDeviceShaderResourceGroupData::SetImageView(
78-
ShaderInputImageIndex inputIndex, const MultiDeviceImageView* imageView, uint32_t arrayIndex = 0)
78+
ShaderInputImageIndex inputIndex, const MultiDeviceImageView* imageView, uint32_t arrayIndex)
7979
{
8080
AZStd::array<const MultiDeviceImageView* const, 1> imageViews = { { imageView } };
8181
return SetImageViewArray(inputIndex, imageViews, arrayIndex);

Gems/Atom/RHI/Code/Source/RHI/MultiDeviceSwapChain.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ namespace AZ::RHI
8585
return resultCode;
8686
}
8787

88+
Ptr<SwapChain> MultiDeviceSwapChain::GetDeviceSwapChain() const
89+
{
90+
// As MultiDeviceSwapChain is always initialized for one single device, the method returns this single item by accessing map.begin()
91+
return AZStd::static_pointer_cast<SwapChain>(m_deviceObjects.begin()->second);
92+
}
93+
8894
void MultiDeviceSwapChain::ShutdownImages()
8995
{
9096
// Shutdown existing set of images.

0 commit comments

Comments
 (0)