1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
#include "renderer-shared.h"
#include "source/core/slang-basic.h"
namespace gfx
{
template <typename TDevice, typename TBufferResource>
class TransientResourceHeapBaseImpl : public TransientResourceHeapBase
{
public:
void breakStrongReferenceToDevice() { m_device.breakStrongReference(); }
public:
BreakableReference<TDevice> m_device;
Slang::List<Slang::RefPtr<TBufferResource>> m_constantBuffers;
Slang::List<Slang::RefPtr<TBufferResource>> m_stagingBuffers;
Slang::Index m_constantBufferAllocCounter = 0;
size_t m_constantBufferOffsetAllocCounter = 0;
uint32_t m_alignment = 256;
Result init(const ITransientResourceHeap::Desc& desc, uint32_t alignment, TDevice* device)
{
m_device = device;
if (desc.constantBufferSize)
{
Slang::ComPtr<IBufferResource> bufferPtr;
IBufferResource::Desc bufferDesc;
bufferDesc.type = IResource::Type::Buffer;
bufferDesc.defaultState = ResourceState::ConstantBuffer;
bufferDesc.allowedStates =
ResourceStateSet(ResourceState::ConstantBuffer, ResourceState::CopyDestination);
bufferDesc.sizeInBytes = desc.constantBufferSize;
bufferDesc.memoryType = MemoryType::Upload;
SLANG_RETURN_ON_FAIL(
m_device->createBufferResource(bufferDesc, nullptr, bufferPtr.writeRef()));
m_constantBuffers.add(static_cast<TBufferResource*>(bufferPtr.get()));
}
m_version = getVersionCounter();
getVersionCounter()++;
return SLANG_OK;
}
static size_t alignUp(size_t value, uint32_t alignment)
{
return (value + alignment - 1) / alignment * alignment;
}
Result allocateStagingBuffer(size_t size, IBufferResource*& outBufferWeakPtr, ResourceState state)
{
Slang::ComPtr<IBufferResource> bufferPtr;
IBufferResource::Desc bufferDesc;
bufferDesc.type = IResource::Type::Buffer;
bufferDesc.defaultState = state;
bufferDesc.allowedStates =
ResourceStateSet(ResourceState::CopyDestination, ResourceState::CopySource);
if (state == ResourceState::CopySource)
bufferDesc.memoryType = MemoryType::Upload;
else
bufferDesc.memoryType = MemoryType::ReadBack;
bufferDesc.sizeInBytes = size;
SLANG_RETURN_ON_FAIL(
m_device->createBufferResource(bufferDesc, nullptr, bufferPtr.writeRef()));
m_stagingBuffers.add(static_cast<TBufferResource*>(bufferPtr.get()));
outBufferWeakPtr = bufferPtr.get();
return SLANG_OK;
}
Result allocateConstantBuffer(
size_t size,
IBufferResource*& outBufferWeakPtr,
size_t& outOffset)
{
size_t bufferAllocOffset = alignUp(m_constantBufferOffsetAllocCounter, m_alignment);
Slang::Index bufferId = -1;
// Find first constant buffer from `m_constantBufferAllocCounter` that has enough space
// for this allocation.
for (Slang::Index i = m_constantBufferAllocCounter; i < m_constantBuffers.getCount(); i++)
{
auto cb = m_constantBuffers[i].Ptr();
if (bufferAllocOffset + size <= cb->getDesc()->sizeInBytes)
{
bufferId = i;
break;
}
bufferAllocOffset = 0;
}
// If we cannot find an existing constant buffer with sufficient free space,
// create a new constant buffer.
if (bufferId == -1)
{
Slang::ComPtr<IBufferResource> bufferPtr;
IBufferResource::Desc bufferDesc;
bufferDesc.type = IResource::Type::Buffer;
bufferDesc.defaultState = ResourceState::ConstantBuffer;
bufferDesc.allowedStates =
ResourceStateSet(ResourceState::ConstantBuffer, ResourceState::CopyDestination);
bufferDesc.memoryType = MemoryType::Upload;
size_t lastConstantBufferSize = 0;
if (m_constantBuffers.getCount())
{
lastConstantBufferSize = m_constantBuffers.getLast()->getDesc()->sizeInBytes;
}
bufferDesc.sizeInBytes = Slang::Math::Max(
lastConstantBufferSize * 2, Slang::Math::Max(size, size_t(4 << 20)));
SLANG_RETURN_ON_FAIL(
m_device->createBufferResource(bufferDesc, nullptr, bufferPtr.writeRef()));
bufferId = m_constantBuffers.getCount();
bufferAllocOffset = 0;
m_constantBuffers.add(static_cast<TBufferResource*>(bufferPtr.get()));
}
// Sub allocate from current constant buffer.
outBufferWeakPtr = m_constantBuffers[bufferId].Ptr();
outOffset = bufferAllocOffset;
m_constantBufferAllocCounter = bufferId;
m_constantBufferOffsetAllocCounter = bufferAllocOffset + size;
return SLANG_OK;
}
void reset()
{
m_constantBufferAllocCounter = 0;
m_constantBufferOffsetAllocCounter = 0;
for (auto& stagingBuffer : m_stagingBuffers)
stagingBuffer = nullptr;
m_stagingBuffers.clear();
m_version = getVersionCounter();
getVersionCounter()++;
}
};
} // namespace gfx
|