Skip to content

Commit ad9e968

Browse files
ShaderResourceCacheWebGPU: remove m_pInlineConstantData
1 parent 620a594 commit ad9e968

2 files changed

Lines changed: 37 additions & 28 deletions

File tree

Graphics/GraphicsEngineWebGPU/include/ShaderResourceCacheWebGPU.hpp

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class ShaderResourceCacheWebGPU : public ShaderResourceCacheBase
9595
// For uniform and storage buffers only
9696
/*16 */ Uint64 BufferBaseOffset = 0;
9797
/*24 */ Uint64 BufferRangeSize = 0;
98-
// For inline constant buffers only - points to staging data in cache tail
98+
// For inline constant buffers only - pointer to the staging data
9999
/*32 */ void* const pInlineConstantData = nullptr;
100100
// clang-format on
101101

@@ -108,7 +108,7 @@ class ShaderResourceCacheWebGPU : public ShaderResourceCacheBase
108108
{
109109
VERIFY(pInlineConstantData != nullptr, "Inline constant data pointer is not initialized");
110110
VERIFY(pData != nullptr, "Source data is null");
111-
memcpy(static_cast<Uint32*>(pInlineConstantData) + FirstConstant, pData, NumConstants * sizeof(Uint32));
111+
memcpy(static_cast<Uint8*>(pInlineConstantData) + FirstConstant * sizeof(Uint32), pData, NumConstants * sizeof(Uint32));
112112
}
113113

114114
template <typename ResType>
@@ -196,7 +196,7 @@ class ShaderResourceCacheWebGPU : public ShaderResourceCacheBase
196196

197197
bool HasInlineConstants() const
198198
{
199-
return m_pInlineConstantData != nullptr;
199+
return m_HasInlineConstants != 0;
200200
}
201201

202202
ResourceCacheContentType GetContentType() const { return static_cast<ResourceCacheContentType>(m_ContentType); }
@@ -208,6 +208,16 @@ class ShaderResourceCacheWebGPU : public ShaderResourceCacheBase
208208
std::vector<uint32_t>& Offsets,
209209
Uint32 GroupIdx) const;
210210

211+
// Writes inline constant data to the staging buffer
212+
void SetInlineConstants(Uint32 BindGroupIdx, Uint32 CacheOffset, const void* pConstants, Uint32 FirstConstant, Uint32 NumConstants);
213+
214+
// Copies inline constant data from source cache to this cache
215+
void CopyInlineConstants(const ShaderResourceCacheWebGPU& SrcCache,
216+
Uint32 BindGroupIdx,
217+
Uint32 SrcCacheOffset,
218+
Uint32 DstCacheOffset,
219+
Uint32 NumConstants);
220+
211221
#ifdef DILIGENT_DEBUG
212222
// For debug purposes only
213223
void DbgVerifyResourceInitialization() const;
@@ -221,9 +231,25 @@ class ShaderResourceCacheWebGPU : public ShaderResourceCacheBase
221231
return reinterpret_cast<const Resource*>(reinterpret_cast<const BindGroup*>(m_pMemory.get()) + m_NumBindGroups);
222232
}
223233
#endif
234+
BindGroup* GetFirstBindGroupPtr()
235+
{
236+
return reinterpret_cast<BindGroup*>(m_pMemory.get());
237+
}
224238
Resource* GetFirstResourcePtr()
225239
{
226-
return reinterpret_cast<Resource*>(reinterpret_cast<BindGroup*>(m_pMemory.get()) + m_NumBindGroups);
240+
static_assert(alignof(BindGroup) >= alignof(Resource),
241+
"BindGroup alignment is less than Resource alignment, Resource pointer may be misaligned");
242+
return reinterpret_cast<Resource*>(GetFirstBindGroupPtr() + m_NumBindGroups);
243+
}
244+
WGPUBindGroupEntry* GetFirstWGPUEntryPtr()
245+
{
246+
static_assert(alignof(Resource) >= alignof(WGPUBindGroupEntry),
247+
"Resource alignment is less than WGPUBindGroupEntry alignment, WGPUBindGroupEntry pointer may be misaligned");
248+
return reinterpret_cast<WGPUBindGroupEntry*>(GetFirstResourcePtr() + m_TotalResources);
249+
}
250+
Uint8* GetInlineConstantDataPtr(Uint32 Offset = 0)
251+
{
252+
return reinterpret_cast<Uint8*>(GetFirstWGPUEntryPtr() + m_TotalResources) + Offset * sizeof(Uint32);
227253
}
228254

229255
BindGroup& GetBindGroup(Uint32 Index)
@@ -240,26 +266,14 @@ class ShaderResourceCacheWebGPU : public ShaderResourceCacheBase
240266
// The total actual number of dynamic buffers (that were created with USAGE_DYNAMIC) bound in the resource cache
241267
// regardless of the variable type.
242268
Uint16 m_NumDynamicBuffers = 0;
243-
Uint32 m_TotalResources : 31;
269+
Uint32 m_TotalResources : 30;
244270

245271
// Indicates what types of resources are stored in the cache
246272
const Uint32 m_ContentType : 1;
247273

248-
// Pointer to the start of inline constant staging data in the memory tail
249-
Uint32* m_pInlineConstantData = nullptr;
250-
251-
public:
252-
// Writes inline constant data to the staging buffer
253-
void SetInlineConstants(Uint32 BindGroupIdx, Uint32 CacheOffset, const void* pConstants, Uint32 FirstConstant, Uint32 NumConstants);
254-
255-
// Copies inline constant data from source cache to this cache
256-
void CopyInlineConstants(const ShaderResourceCacheWebGPU& SrcCache,
257-
Uint32 BindGroupIdx,
258-
Uint32 SrcCacheOffset,
259-
Uint32 DstCacheOffset,
260-
Uint32 NumConstants);
274+
// Indicates whether the cache has inline constants
275+
Uint32 m_HasInlineConstants : 1;
261276

262-
private:
263277
#ifdef DILIGENT_DEBUG
264278
// Debug array that stores flags indicating if resources in the cache have been initialized
265279
std::vector<std::vector<bool>> m_DbgInitializedResources;

Graphics/GraphicsEngineWebGPU/src/ShaderResourceCacheWebGPU.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,8 @@ void ShaderResourceCacheWebGPU::InitializeGroups(IMemoryAllocator& MemAllocator,
112112
};
113113
memset(m_pMemory.get(), 0, MemorySize);
114114

115-
BindGroup* pGroups = reinterpret_cast<BindGroup*>(m_pMemory.get());
116-
Resource* pCurrResPtr = reinterpret_cast<Resource*>(pGroups + m_NumBindGroups);
117-
WGPUBindGroupEntry* pCurrWGPUEntryPtr = reinterpret_cast<WGPUBindGroupEntry*>(pCurrResPtr + m_TotalResources);
115+
Resource* pCurrResPtr = GetFirstResourcePtr();
116+
WGPUBindGroupEntry* pCurrWGPUEntryPtr = GetFirstWGPUEntryPtr();
118117
for (Uint32 t = 0; t < NumGroups; ++t)
119118
{
120119
const Uint32 GroupSize = GroupSizes[t];
@@ -138,11 +137,7 @@ void ShaderResourceCacheWebGPU::InitializeGroups(IMemoryAllocator& MemAllocator,
138137
#endif
139138
}
140139

141-
// Set pointer to inline constant staging data at the tail of the memory block
142-
if (TotalInlineConstants > 0)
143-
{
144-
m_pInlineConstantData = reinterpret_cast<Uint32*>(pCurrWGPUEntryPtr);
145-
}
140+
m_HasInlineConstants = (TotalInlineConstants > 0) ? 1u : 0u;
146141
}
147142
}
148143

@@ -275,7 +270,7 @@ void ShaderResourceCacheWebGPU::InitializeResources(Uint32 GroupIdx,
275270
new (&Group.GetResource(Offset + res)) Resource{
276271
Type,
277272
HasImmutableSampler,
278-
InlineConstantOffset != ~0u ? m_pInlineConstantData + InlineConstantOffset : nullptr,
273+
InlineConstantOffset != ~0u ? GetInlineConstantDataPtr(InlineConstantOffset) : nullptr,
279274
};
280275
#ifdef DILIGENT_DEBUG
281276
m_DbgInitializedResources[GroupIdx][size_t{Offset} + res] = true;

0 commit comments

Comments
 (0)