summaryrefslogtreecommitdiffstats
path: root/tools/gfx/transient-resource-heap-base.h
blob: d99d84697db22f5d2123e48a2616a3961425a877 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "core/slang-basic.h"
#include "renderer-shared.h"

namespace gfx
{
template<typename TDevice, typename TBufferResource>
class StagingBufferPool
{
public:
    struct StagingBufferPage
    {
        Slang::RefPtr<TBufferResource> resource;
        size_t size;
    };

    struct Allocation
    {
        TBufferResource* resource;
        size_t offset;
    };

    TDevice* m_device;
    MemoryType m_memoryType;
    uint32_t m_alignment;
    ResourceStateSet m_allowedStates;

    Slang::List<StagingBufferPage> m_pages;
    Slang::List<Slang::RefPtr<TBufferResource>> m_largeAllocations;

    Slang::Index m_pageAllocCounter = 0;
    size_t m_offsetAllocCounter = 0;

    const size_t kStagingBufferDefaultPageSize = 16 * 1024 * 1024;

    void init(
        TDevice* device,
        MemoryType memoryType,
        uint32_t alignment,
        ResourceStateSet allowedStates)
    {
        m_device = device;
        m_memoryType = memoryType;
        m_alignment = alignment;
        m_allowedStates = allowedStates;
    }

    static size_t alignUp(size_t value, uint32_t alignment)
    {
        return (value + alignment - 1) / alignment * alignment;
    }

    void reset()
    {
        m_pageAllocCounter = 0;
        m_offsetAllocCounter = 0;
        m_largeAllocations.clearAndDeallocate();
    }

    Result newStagingBufferPage()
    {
        StagingBufferPage page;
        size_t pageSize = kStagingBufferDefaultPageSize;

        Slang::ComPtr<IBufferResource> bufferPtr;
        IBufferResource::Desc bufferDesc;
        bufferDesc.type = IResource::Type::Buffer;
        bufferDesc.defaultState = ResourceState::General;
        bufferDesc.allowedStates = m_allowedStates;
        bufferDesc.memoryType = m_memoryType;
        bufferDesc.sizeInBytes = pageSize;
        SLANG_RETURN_ON_FAIL(
            m_device->createBufferResource(bufferDesc, nullptr, bufferPtr.writeRef()));

        page.resource = static_cast<TBufferResource*>(bufferPtr.get());
        page.size = pageSize;
        m_pages.add(page);
        return SLANG_OK;
    }

    Result newLargeBuffer(size_t size)
    {
        Slang::ComPtr<IBufferResource> bufferPtr;
        IBufferResource::Desc bufferDesc;
        bufferDesc.type = IResource::Type::Buffer;
        bufferDesc.defaultState = ResourceState::General;
        bufferDesc.allowedStates = m_allowedStates;
        bufferDesc.memoryType = m_memoryType;
        bufferDesc.sizeInBytes = size;
        SLANG_RETURN_ON_FAIL(
            m_device->createBufferResource(bufferDesc, nullptr, bufferPtr.writeRef()));
        auto bufferImpl = static_cast<TBufferResource*>(bufferPtr.get());
        m_largeAllocations.add(bufferImpl);
        return SLANG_OK;
    }

    Allocation allocate(size_t size, bool forceLargePage)
    {
        if (forceLargePage || size >= (kStagingBufferDefaultPageSize >> 2))
        {
            newLargeBuffer(size);
            Allocation result;
            result.resource = m_largeAllocations.getLast();
            result.offset = 0;
            return result;
        }

        size_t bufferAllocOffset = alignUp(m_offsetAllocCounter, m_alignment);
        Slang::Index bufferId = -1;
        for (Slang::Index i = m_pageAllocCounter; i < m_pages.getCount(); i++)
        {
            auto cb = m_pages[i].resource.Ptr();
            if (bufferAllocOffset + size <= cb->getDesc()->sizeInBytes)
            {
                bufferId = i;
                break;
            }
            bufferAllocOffset = 0;
        }
        // If we cannot find an existing page with sufficient free space,
        // create a new page.
        if (bufferId == -1)
        {
            newStagingBufferPage();
            bufferId = m_pages.getCount() - 1;
        }
        // Sub allocate from current page.
        Allocation result;
        result.resource = m_pages[bufferId].resource.Ptr();
        result.offset = bufferAllocOffset;
        m_pageAllocCounter = bufferId;
        m_offsetAllocCounter = bufferAllocOffset + size;
        return result;
    }
};

template<typename TDevice, typename TBufferResource>
class TransientResourceHeapBaseImpl : public TransientResourceHeapBase
{
public:
    void breakStrongReferenceToDevice() { m_device.breakStrongReference(); }

public:
    BreakableReference<TDevice> m_device;
    StagingBufferPool<TDevice, TBufferResource> m_constantBufferPool;
    StagingBufferPool<TDevice, TBufferResource> m_uploadBufferPool;
    StagingBufferPool<TDevice, TBufferResource> m_readbackBufferPool;

    Result init(const ITransientResourceHeap::Desc& desc, uint32_t alignment, TDevice* device)
    {
        m_device = device;

        m_constantBufferPool.init(
            device,
            MemoryType::Upload,
            256,
            ResourceStateSet(
                ResourceState::ConstantBuffer,
                ResourceState::CopySource,
                ResourceState::CopyDestination));

        m_uploadBufferPool.init(
            device,
            MemoryType::Upload,
            256,
            ResourceStateSet(ResourceState::CopySource, ResourceState::CopyDestination));

        m_readbackBufferPool.init(
            device,
            MemoryType::ReadBack,
            256,
            ResourceStateSet(ResourceState::CopySource, ResourceState::CopyDestination));

        m_version = getVersionCounter();
        getVersionCounter()++;
        return SLANG_OK;
    }

    Result allocateStagingBuffer(
        size_t size,
        IBufferResource*& outBufferWeakPtr,
        size_t& offset,
        MemoryType memoryType,
        bool forceLargePage = false)
    {
        switch (memoryType)
        {
        case MemoryType::ReadBack:
            {
                auto allocation = m_readbackBufferPool.allocate(size, forceLargePage);
                outBufferWeakPtr = allocation.resource;
                offset = allocation.offset;
            }
            break;
        default:
            {
                auto allocation = m_uploadBufferPool.allocate(size, forceLargePage);
                outBufferWeakPtr = allocation.resource;
                offset = allocation.offset;
            }
            break;
        }
        return SLANG_OK;
    }

    Result allocateConstantBuffer(
        size_t size,
        IBufferResource*& outBufferWeakPtr,
        size_t& outOffset)
    {
        auto allocation = m_constantBufferPool.allocate(size, false);
        outBufferWeakPtr = allocation.resource;
        outOffset = allocation.offset;
        return SLANG_OK;
    }

    void reset()
    {
        m_constantBufferPool.reset();
        m_uploadBufferPool.reset();
        m_readbackBufferPool.reset();
        m_version = getVersionCounter();
        getVersionCounter()++;
    }
};

} // namespace gfx