/
SurfaceRenderer.h
165 lines (130 loc) · 4.06 KB
/
SurfaceRenderer.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
#pragma once
#include <map>
#include <stdexcept>
#include "irender.h"
#include "isurfacerenderer.h"
#include "igeometrystore.h"
#include "ObjectRenderer.h"
namespace render
{
class SurfaceRenderer :
public ISurfaceRenderer
{
private:
IGeometryStore& _store;
struct SurfaceInfo
{
std::reference_wrapper<IRenderableSurface> surface;
bool surfaceDataChanged;
IGeometryStore::Slot storageHandle;
SurfaceInfo(IRenderableSurface& surface_, IGeometryStore::Slot slot) :
surface(surface_),
surfaceDataChanged(false),
storageHandle(slot)
{}
};
std::map<Slot, SurfaceInfo> _surfaces;
Slot _freeSlotMappingHint;
std::vector<Slot> _surfacesNeedingUpdate;
bool _surfacesNeedUpdate;
public:
SurfaceRenderer(IGeometryStore& store) :
_store(store),
_freeSlotMappingHint(0),
_surfacesNeedUpdate(false)
{}
bool empty() const
{
return _surfaces.empty();
}
Slot addSurface(IRenderableSurface& surface) override
{
// Find a free slot
auto newSlotIndex = getNextFreeSlotIndex();
const auto& vertices = surface.getVertices();
const auto& indices = surface.getIndices();
auto slot = _store.allocateSlot(vertices.size(), indices.size());
_store.updateData(slot, vertices, indices);
_surfaces.emplace(newSlotIndex, SurfaceInfo(surface, slot));
return newSlotIndex;
}
void removeSurface(Slot slot) override
{
auto surface = _surfaces.find(slot);
assert(surface != _surfaces.end());
// Deallocate the storage
_store.deallocateSlot(surface->second.storageHandle);
_surfaces.erase(surface);
if (slot < _freeSlotMappingHint)
{
_freeSlotMappingHint = slot;
}
}
void updateSurface(Slot slot) override
{
_surfaces.at(slot).surfaceDataChanged = true;
_surfacesNeedingUpdate.push_back(slot);
_surfacesNeedUpdate = true;
}
void render(const VolumeTest& view)
{
for (auto& surface : _surfaces)
{
renderSlot(surface.second, &view);
}
}
void renderSurface(Slot slot) override
{
renderSlot(_surfaces.at(slot));
}
IGeometryStore::Slot getSurfaceStorageLocation(ISurfaceRenderer::Slot slot) override
{
return _surfaces.at(slot).storageHandle;
}
// Ensures the data in the IGeometryStore is up to date
void prepareForRendering()
{
if (!_surfacesNeedUpdate) return;
_surfacesNeedUpdate = false;
for (auto slotIndex : _surfacesNeedingUpdate)
{
auto& surfaceInfo = _surfaces.at(slotIndex);
if (surfaceInfo.surfaceDataChanged)
{
surfaceInfo.surfaceDataChanged = false;
auto& surface = surfaceInfo.surface.get();
_store.updateData(surfaceInfo.storageHandle, surface.getVertices(), surface.getIndices());
}
}
}
private:
void renderSlot(SurfaceInfo& slot, const VolumeTest* view = nullptr)
{
auto& surface = slot.surface.get();
// Check if this surface is in view
if (view && view->TestAABB(surface.getObjectBounds(), surface.getObjectTransform()) == VOLUME_OUTSIDE)
{
return;
}
// Update the vertex data now if necessary
if (slot.surfaceDataChanged)
{
slot.surfaceDataChanged = false;
_store.updateData(slot.storageHandle, surface.getVertices(), surface.getIndices());
}
ObjectRenderer::SubmitObject(surface, _store);
}
Slot getNextFreeSlotIndex()
{
for (auto i = _freeSlotMappingHint; i < std::numeric_limits<Slot>::max(); ++i)
{
if (_surfaces.count(i) == 0)
{
_freeSlotMappingHint = i + 1; // start searching here next time
return i;
}
}
throw std::runtime_error("SurfaceRenderer ran out of surface slot numbers");
}
};
}