|
1 | 1 | /* |
2 | | - * Copyright 2019-2025 Diligent Graphics LLC |
| 2 | + * Copyright 2019-2026 Diligent Graphics LLC |
3 | 3 | * Copyright 2015-2019 Egor Yusov |
4 | 4 | * |
5 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
@@ -40,10 +40,10 @@ namespace Diligent |
40 | 40 |
|
41 | 41 | // All resources are stored in the continuous memory using the following layout: |
42 | 42 | // |
43 | | -// | Cached UBs | Cached Textures | Cached Images | Cached Storage Blocks | |
44 | | -// |----------------------------------------------------|--------------------------|---------------------------| |
45 | | -// | 0 | 1 | ... | UBCount-1 | 0 | 1 | ...| SmpCount-1 | 0 | 1 | ... | ImgCount-1 | 0 | 1 | ... | SBOCount-1 | |
46 | | -// ----------------------------------------------------------------------------------------------------------- |
| 43 | +// | Cached UBs | Cached Textures | Cached Images | Cached Storage Blocks | Inline Constant Data | |
| 44 | +// |----------------------------------------------------|--------------------------|---------------------------|-------------------------| |
| 45 | +// | 0 | 1 | ... | UBCount-1 | 0 | 1 | ...| SmpCount-1 | 0 | 1 | ... | ImgCount-1 | 0 | 1 | ... | SBOCount-1 | Uint32[] (tail) | |
| 46 | +// -------------------------------------------------------------------------------------------------------------------------------------- |
47 | 47 | // |
48 | 48 | class ShaderResourceCacheGL : public ShaderResourceCacheBase |
49 | 49 | { |
@@ -71,13 +71,27 @@ class ShaderResourceCacheGL : public ShaderResourceCacheBase |
71 | 71 | Uint32 RangeSize = 0; |
72 | 72 | Uint32 DynamicOffset = 0; |
73 | 73 |
|
| 74 | + // Pointer to inline constant data |
| 75 | + void* pInlineConstantData = nullptr; |
| 76 | + |
74 | 77 | // In OpenGL dynamic buffers are only those that are not bound as a whole and |
75 | 78 | // can use a dynamic offset, irrespective of the variable type or whether the |
76 | 79 | // buffer is USAGE_DYNAMIC or not. |
77 | 80 | bool IsDynamic() const |
78 | 81 | { |
79 | 82 | return pBuffer && RangeSize < pBuffer->GetDesc().Size; |
80 | 83 | } |
| 84 | + |
| 85 | + void SetInlineConstants(const void* pSrcConstants, Uint32 FirstConstant, Uint32 NumConstants) |
| 86 | + { |
| 87 | + VERIFY(pSrcConstants != nullptr, "Source constant data pointer is null"); |
| 88 | + VERIFY(FirstConstant + NumConstants <= RangeSize / sizeof(Uint32), |
| 89 | + "Too many constants (", FirstConstant + NumConstants, ") for the allocated space (", RangeSize / sizeof(Uint32), ")"); |
| 90 | + VERIFY(pInlineConstantData != nullptr, "Inline constant data pointer is null"); |
| 91 | + memcpy(reinterpret_cast<Uint8*>(pInlineConstantData) + FirstConstant * sizeof(Uint32), |
| 92 | + pSrcConstants, |
| 93 | + NumConstants * sizeof(Uint32)); |
| 94 | + } |
81 | 95 | }; |
82 | 96 |
|
83 | 97 | /// Describes a resource bound to a sampler or an image slot |
@@ -142,9 +156,9 @@ class ShaderResourceCacheGL : public ShaderResourceCacheBase |
142 | 156 | }; |
143 | 157 |
|
144 | 158 | using TResourceCount = std::array<Uint16, 4>; // same as PipelineResourceSignatureGLImpl::TBindings. |
145 | | - static size_t GetRequiredMemorySize(const TResourceCount& ResCount); |
| 159 | + static size_t GetRequiredMemorySize(const TResourceCount& ResCount, Uint32 TotalInlineConstants = 0); |
146 | 160 |
|
147 | | - void Initialize(const TResourceCount& Count, IMemoryAllocator& MemAllocator, Uint64 DynamicUBOSlotMask, Uint64 DynamicSSBOSlotMask); |
| 161 | + void Initialize(const TResourceCount& Count, IMemoryAllocator& MemAllocator, Uint64 DynamicUBOSlotMask, Uint64 DynamicSSBOSlotMask, Uint32 TotalInlineConstants = 0); |
148 | 162 |
|
149 | 163 | void SetUniformBuffer(Uint32 CacheOffset, RefCntAutoPtr<BufferGLImpl>&& pBuff, Uint64 BaseOffset, Uint64 RangeSize) |
150 | 164 | { |
@@ -347,7 +361,56 @@ class ShaderResourceCacheGL : public ShaderResourceCacheBase |
347 | 361 |
|
348 | 362 | bool HasInlineConstants() const |
349 | 363 | { |
350 | | - return false; |
| 364 | + return m_HasInlineConstants; |
| 365 | + } |
| 366 | + |
| 367 | + void InitInlineConstantBuffer(Uint32 CacheOffset, |
| 368 | + RefCntAutoPtr<BufferGLImpl>&& pBuffer, |
| 369 | + Uint32 NumConstants, |
| 370 | + void* pInlineConstantData) |
| 371 | + { |
| 372 | + VERIFY_EXPR(pBuffer); |
| 373 | + VERIFY_EXPR(pInlineConstantData); |
| 374 | + |
| 375 | + CachedUB& UB = GetUB(CacheOffset); |
| 376 | + UB.pBuffer = std::move(pBuffer); |
| 377 | + UB.BaseOffset = 0; |
| 378 | + UB.RangeSize = NumConstants * sizeof(Uint32); |
| 379 | + UB.DynamicOffset = 0; |
| 380 | + UB.pInlineConstantData = pInlineConstantData; |
| 381 | + |
| 382 | + UpdateRevision(); |
| 383 | + } |
| 384 | + |
| 385 | + void SetInlineConstants(Uint32 CacheOffset, |
| 386 | + const void* pConstants, |
| 387 | + Uint32 FirstConstant, |
| 388 | + Uint32 NumConstants) |
| 389 | + { |
| 390 | + VERIFY(CacheOffset < GetUBCount(), "Cache offset is out of range"); |
| 391 | + CachedUB& UB = GetUB(CacheOffset); |
| 392 | + UB.SetInlineConstants(pConstants, FirstConstant, NumConstants); |
| 393 | + UpdateRevision(); |
| 394 | + } |
| 395 | + |
| 396 | + void CopyInlineConstants(const ShaderResourceCacheGL& SrcCache, |
| 397 | + Uint32 CacheOffset, |
| 398 | + Uint32 NumConstants) |
| 399 | + { |
| 400 | + VERIFY(CacheOffset < GetUBCount(), "Destination index is out of range"); |
| 401 | + VERIFY(CacheOffset < SrcCache.GetUBCount(), "Source index is out of range"); |
| 402 | + |
| 403 | + const CachedUB& SrcUB = SrcCache.GetConstUB(CacheOffset); |
| 404 | + CachedUB& DstUB = GetUB(CacheOffset); |
| 405 | + |
| 406 | + VERIFY(SrcUB.pInlineConstantData != nullptr, "Source inline constant data is null"); |
| 407 | + VERIFY(DstUB.pInlineConstantData != nullptr, "Destination inline constant data is null"); |
| 408 | + VERIFY(SrcUB.RangeSize == NumConstants * sizeof(Uint32), "Source inline constant buffer size mismatch"); |
| 409 | + VERIFY(DstUB.RangeSize == NumConstants * sizeof(Uint32), "Destination inline constant buffer size mismatch"); |
| 410 | + |
| 411 | + memcpy(DstUB.pInlineConstantData, |
| 412 | + SrcUB.pInlineConstantData, |
| 413 | + NumConstants * sizeof(Uint32)); |
351 | 414 | } |
352 | 415 |
|
353 | 416 | #ifdef DILIGENT_DEBUG |
@@ -397,6 +460,9 @@ class ShaderResourceCacheGL : public ShaderResourceCacheBase |
397 | 460 | // Indicates what types of resources are stored in the cache |
398 | 461 | const ResourceCacheContentType m_ContentType; |
399 | 462 |
|
| 463 | + // Indicates whether this cache has inline constants |
| 464 | + bool m_HasInlineConstants = false; |
| 465 | + |
400 | 466 | #ifdef DILIGENT_DEVELOPMENT |
401 | 467 | bool m_bStaticResourcesInitialized = false; |
402 | 468 | #endif |
|
0 commit comments