/
GeometryStore.h
216 lines (168 loc) · 6.3 KB
/
GeometryStore.h
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
#pragma once
#include <stdexcept>
#include <limits>
#include "igeometrystore.h"
#include "ContinuousBuffer.h"
namespace render
{
class GeometryStore :
public IGeometryStore
{
public:
// Slot ID handed out to client code
using Slot = std::uint64_t;
private:
static constexpr auto NumFrameBuffers = 2;
// Keep track of modified slots as long as a single buffer is in use
std::vector<detail::BufferTransaction> _transactionLog;
// Represents the storage for a single frame
struct FrameBuffer
{
ContinuousBuffer<MeshVertex> vertices;
ContinuousBuffer<unsigned int> indices;
ISyncObject::Ptr syncObject;
void applyTransactions(const std::vector<detail::BufferTransaction>& transactions, const FrameBuffer& other)
{
vertices.applyTransactions(transactions, other.vertices, GetVertexSlot);
indices.applyTransactions(transactions, other.indices, GetIndexSlot);
}
};
// We keep a fixed number of frame buffers
std::vector<FrameBuffer> _frameBuffers;
unsigned int _currentBuffer;
ISyncObjectProvider& _syncObjectProvider;
public:
GeometryStore(ISyncObjectProvider& syncObjectProvider) :
_currentBuffer(0),
_syncObjectProvider(syncObjectProvider)
{
_frameBuffers.resize(NumFrameBuffers);
}
// Marks the beginning of a frame, switches to the next writing buffers
void onFrameStart()
{
// Switch to the next frame
auto& previous = getCurrentBuffer();
_currentBuffer = (_currentBuffer + 1) % NumFrameBuffers;
auto& current = getCurrentBuffer();
// Wait for this buffer to become available
if (current.syncObject)
{
current.syncObject->wait();
current.syncObject.reset();
}
// Replay any modifications to the new buffer
current.applyTransactions(_transactionLog, previous);
_transactionLog.clear();
}
// Completes the currently writing frame, creates sync objects
void onFrameFinished()
{
auto& current = getCurrentBuffer();
current.syncObject = _syncObjectProvider.createSyncObject();
}
Slot allocateSlot(std::size_t numVertices, std::size_t numIndices) override
{
assert(numVertices > 0);
assert(numIndices > 0);
auto& current = getCurrentBuffer();
auto vertexSlot = current.vertices.allocate(numVertices);
auto indexSlot = current.indices.allocate(numIndices);
auto slot = GetSlot(vertexSlot, indexSlot);
_transactionLog.emplace_back(detail::BufferTransaction{
slot, detail::BufferTransaction::Type::Allocate
});
return slot;
}
Slot allocateIndexSlot(Slot slotContainingVertexData, std::size_t numIndices) override
{
return std::numeric_limits<Slot>::max();
}
void updateData(Slot slot, const std::vector<MeshVertex>& vertices,
const std::vector<unsigned int>& indices) override
{
assert(!vertices.empty());
assert(!indices.empty());
auto& current = getCurrentBuffer();
current.vertices.setData(GetVertexSlot(slot), vertices);
current.indices.setData(GetIndexSlot(slot), indices);
_transactionLog.emplace_back(detail::BufferTransaction{
slot, detail::BufferTransaction::Type::Update
});
}
void updateSubData(Slot slot, std::size_t vertexOffset, const std::vector<MeshVertex>& vertices,
std::size_t indexOffset, const std::vector<unsigned int>& indices) override
{
assert(!vertices.empty());
assert(!indices.empty());
auto& current = getCurrentBuffer();
current.vertices.setSubData(GetVertexSlot(slot), vertexOffset, vertices);
current.indices.setSubData(GetIndexSlot(slot), indexOffset, indices);
_transactionLog.emplace_back(detail::BufferTransaction{
slot, detail::BufferTransaction::Type::Update
});
}
void resizeData(Slot slot, std::size_t vertexSize, std::size_t indexSize) override
{
auto& current = getCurrentBuffer();
current.vertices.resizeData(GetVertexSlot(slot), vertexSize);
current.indices.resizeData(GetIndexSlot(slot), indexSize);
_transactionLog.emplace_back(detail::BufferTransaction{
slot, detail::BufferTransaction::Type::Update
});
}
void deallocateSlot(Slot slot) override
{
auto& current = getCurrentBuffer();
current.vertices.deallocate(GetVertexSlot(slot));
current.indices.deallocate(GetIndexSlot(slot));
_transactionLog.emplace_back(detail::BufferTransaction{
slot, detail::BufferTransaction::Type::Deallocate
});
}
RenderParameters getRenderParameters(Slot slot) override
{
auto vertexSlot = GetVertexSlot(slot);
auto indexSlot = GetIndexSlot(slot);
auto& current = getCurrentBuffer();
return RenderParameters
{
current.vertices.getBufferStart(),
current.indices.getBufferStart() + current.indices.getOffset(indexSlot), // pointer to first index
current.indices.getNumUsedElements(indexSlot), // index count of the given geometry
current.vertices.getOffset(vertexSlot) // offset to the first vertex
};
}
AABB getBounds(Slot slot) override
{
auto vertexSlot = GetVertexSlot(slot);
auto& current = getCurrentBuffer();
auto vertex = current.vertices.getBufferStart() + current.vertices.getOffset(vertexSlot);
auto numVertices = current.vertices.getSize(vertexSlot);
AABB bounds;
for (auto i = 0; i < numVertices; ++i, ++vertex)
{
bounds.includePoint(vertex->vertex);
}
return bounds;
}
private:
FrameBuffer& getCurrentBuffer()
{
return _frameBuffers[_currentBuffer];
}
// Higher 4 bytes will hold the vertex buffer slot
static Slot GetSlot(std::uint32_t vertexSlot, std::uint32_t indexSlot)
{
return (static_cast<Slot>(vertexSlot) << 32) + indexSlot;
}
static std::uint32_t GetVertexSlot(Slot slot)
{
return static_cast<std::uint32_t>(slot >> 32);
}
static std::uint32_t GetIndexSlot(Slot slot)
{
return static_cast<std::uint32_t>((slot << 32) >> 32);
}
};
}