-
Notifications
You must be signed in to change notification settings - Fork 274
Expand file tree
/
Copy pathState.h
More file actions
412 lines (332 loc) · 13 KB
/
State.h
File metadata and controls
412 lines (332 loc) · 13 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#pragma once
/* <editor-fold desc="MIT License">
Copyright(c) 2018 Robert Osfield
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</editor-fold> */
#include <vsg/maths/plane.h>
#include <vsg/maths/transform.h>
#include <vsg/nodes/MatrixTransform.h>
#include <vsg/state/PushConstants.h>
#include <vsg/vk/CommandBuffer.h>
#include <array>
#include <map>
#include <stack>
namespace vsg
{
#define POLYTOPE_SIZE 5
#define STATESTACK_SIZE 16
/// StateStack used internally by vsg::State to manage stack of vsg::StateCommand
template<class T>
class StateStack
{
public:
StateStack() {}
using Stack = std::array<const T*, STATESTACK_SIZE>;
Stack stack;
size_t pos = 0;
inline void reset()
{
pos = 0;
stack[0] = nullptr;
}
inline void dirty()
{
stack[0] = nullptr;
}
inline void push(const T* value)
{
stack[++pos] = value;
}
inline void pop()
{
--pos;
}
bool empty() const { return pos == 0; }
size_t size() const { return pos; }
const T* top() const { return stack[pos]; }
inline void record(CommandBuffer& commandBuffer)
{
const T* current = stack[pos];
if (current != stack[0])
{
current->record(commandBuffer);
stack[0] = current;
}
}
};
/// MatrixStack used internally by vsg::State to manage stack of projection or modelview matrices
class MatrixStack
{
public:
explicit MatrixStack(uint32_t in_offset = 0) :
offset(in_offset)
{
// make sure there is an initial matrix
matrixStack.emplace(dmat4());
dirty = true;
}
using value_type = double;
std::stack<dmat4> matrixStack;
uint32_t offset = 0;
bool dirty = false;
inline void set(const mat4& matrix)
{
matrixStack = {};
matrixStack.emplace(matrix);
dirty = true;
}
inline void set(const dmat4& matrix)
{
matrixStack = {};
matrixStack.emplace(matrix);
dirty = true;
}
inline void push(const mat4& matrix)
{
matrixStack.emplace(matrix);
dirty = true;
}
inline void push(const dmat4& matrix)
{
matrixStack.emplace(matrix);
dirty = true;
}
inline void push(const Transform& transform)
{
matrixStack.emplace(transform.transform(matrixStack.top()));
dirty = true;
}
inline void push(const MatrixTransform& transform)
{
matrixStack.emplace(matrixStack.top() * transform.matrix);
dirty = true;
}
const dmat4& top() const { return matrixStack.top(); }
inline void pop()
{
matrixStack.pop();
dirty = true;
}
inline void record(CommandBuffer& commandBuffer)
{
if (dirty)
{
auto pipeline = commandBuffer.getCurrentPipelineLayout();
auto stageFlags = commandBuffer.getCurrentPushConstantStageFlags();
// don't attempt to push matrices if no pipeline is current or no stages are enabled for push constants
if (pipeline == 0 || stageFlags == 0)
{
return;
}
// make sure matrix is a float matrix.
mat4 newmatrix(matrixStack.top());
vkCmdPushConstants(commandBuffer, pipeline, stageFlags, offset, sizeof(newmatrix), newmatrix.data());
dirty = false;
}
}
};
/// Frustum used internally by vsg::State to manage view fustum culling during vsg::RecordTraversal
struct Frustum
{
using value_type = MatrixStack::value_type;
using Plane = t_plane<value_type>;
using Vector = t_vec4<value_type>;
Plane face[POLYTOPE_SIZE];
Vector lodScale;
Frustum()
{
face[0].set(1.0, 0.0, 0.0, 1.0); // left plane
face[1].set(-1.0, 0.0, 0.0, 1.0); // right plane
face[2].set(0.0, -1.0, 0.0, 1.0); // bottom plane
face[3].set(0.0, 1.0, 0.0, 1.0); // top plane
if constexpr (POLYTOPE_SIZE >= 5) face[4].set(0.0, 0.0, 1.0, 0.0); // far plane
if constexpr (POLYTOPE_SIZE >= 6) face[5].set(0.0, 0.0, -1.0, 1.0); // near plane
}
template<class M>
Frustum(const Frustum& pt, const M& matrix)
{
face[0] = pt.face[0] * matrix;
face[1] = pt.face[1] * matrix;
face[2] = pt.face[2] * matrix;
face[3] = pt.face[3] * matrix;
if constexpr (POLYTOPE_SIZE >= 5) face[4] = pt.face[4] * matrix;
if constexpr (POLYTOPE_SIZE >= 6) face[5] = pt.face[5] * matrix;
}
template<class M>
void set(const Frustum& pt, const M& matrix)
{
face[0] = pt.face[0] * matrix;
face[1] = pt.face[1] * matrix;
face[2] = pt.face[2] * matrix;
face[3] = pt.face[3] * matrix;
if constexpr (POLYTOPE_SIZE >= 5) face[4] = pt.face[4] * matrix;
if constexpr (POLYTOPE_SIZE >= 6) face[5] = pt.face[5] * matrix;
}
template<class M>
void computeLodScale(const M& proj, const M& mv)
{
value_type f = -proj[1][1];
value_type sc = f * std::sqrt(square(mv[0][0]) + square(mv[1][0]) + square(mv[2][0]) + square(mv[0][1]) + square(mv[1][1]) + square(mv[2][1])) * 0.5;
value_type inv_scale = value_type(1.0) / sc;
lodScale.set(mv[0][2] * inv_scale,
mv[1][2] * inv_scale,
mv[2][2] * inv_scale,
mv[3][2] * inv_scale);
}
template<typename T>
bool intersect(const t_sphere<T>& s) const
{
auto negative_radius = -s.radius;
if (distance(face[0], s.center) < negative_radius) return false;
if (distance(face[1], s.center) < negative_radius) return false;
if (distance(face[2], s.center) < negative_radius) return false;
if (distance(face[3], s.center) < negative_radius) return false;
if constexpr (POLYTOPE_SIZE >= 5)
if (distance(face[4], s.center) < negative_radius) return false;
if constexpr (POLYTOPE_SIZE >= 6)
if (distance(face[5], s.center) < negative_radius) return false;
return true;
}
};
/// vsg::State is used by vsg::RecordTraversal to manage state stacks, projection and modelview matrices and frustum stacks.
class State : public Inherit<Object, State>
{
public:
explicit State(const Slots& in_maxSlots);
using StateCommandStack = StateStack<StateCommand>;
using StateStacks = std::vector<StateCommandStack>;
ref_ptr<CommandBuffer> _commandBuffer;
Frustum _frustumUnit;
Frustum _frustumProjected;
using FrustumStack = std::stack<Frustum>;
FrustumStack _frustumStack;
bool dirty = true;
bool inheritViewForLODScaling = false;
dmat4 inheritedProjectionMatrix;
dmat4 inheritedViewMatrix;
dmat4 inheritedViewTransform;
Slots maxSlots;
uint32_t activeMaxStateSlot = 0;
StateStacks stateStacks;
uint32_t viewportStateHint = 0;
MatrixStack projectionMatrixStack{0};
MatrixStack modelviewMatrixStack{64};
void reserve(const Slots& in_maxSlots);
void connect(ref_ptr<CommandBuffer> commandBuffer);
void reset();
inline void dirtyStateStacks()
{
for (auto& stateStack : stateStacks)
{
stateStack.dirty();
}
}
void setInhertiedViewProjectionAndViewMatrix(const dmat4& projMatrix, const dmat4& viewMatrix)
{
inheritedProjectionMatrix = projMatrix;
inheritedViewMatrix = viewMatrix;
}
void setProjectionAndViewMatrix(const dmat4& projMatrix, const dmat4& viewMatrix)
{
projectionMatrixStack.set(projMatrix);
const auto& proj = projectionMatrixStack.top();
_frustumProjected.set(_frustumUnit, proj);
modelviewMatrixStack.set(viewMatrix);
// clear frustum stack
while (!_frustumStack.empty()) _frustumStack.pop();
if (inheritViewForLODScaling)
{
inheritedViewTransform = inheritedViewMatrix * inverse(viewMatrix);
}
// push frustum in world coords
pushFrustum();
}
inline void record()
{
if (dirty)
{
for (uint32_t slot = 0; slot <= activeMaxStateSlot; ++slot)
{
stateStacks[slot].record(*_commandBuffer);
}
// reset the active maxslot to the minimum required
activeMaxStateSlot = maxSlots.state;
projectionMatrixStack.record(*_commandBuffer);
modelviewMatrixStack.record(*_commandBuffer);
dirty = false;
}
}
template<typename Iterator>
inline void push(Iterator begin, Iterator end)
{
for (auto itr = begin; itr != end; ++itr)
{
stateStacks[(*itr)->slot].push((*itr));
}
dirty = true;
}
inline void push(const StateCommands& commands)
{
push(commands.begin(), commands.end());
}
template<typename Iterator>
inline void pop(Iterator begin, Iterator end)
{
for (auto itr = begin; itr != end; ++itr)
{
stateStacks[(*itr)->slot].pop();
}
dirty = true;
}
inline void pop(const StateCommands& commands)
{
pop(commands.begin(), commands.end());
}
inline void push(ref_ptr<StateCommand> command)
{
stateStacks[command->slot].push(command);
dirty = true;
}
inline void pop(ref_ptr<StateCommand> command)
{
stateStacks[command->slot].pop();
dirty = true;
}
void pushView(ref_ptr<StateCommand> command);
void popView(ref_ptr<StateCommand> command);
void pushView(const View& view);
void popView(const View& view);
inline void pushFrustum()
{
_frustumStack.push(Frustum(_frustumProjected, modelviewMatrixStack.top()));
if (inheritViewForLODScaling)
_frustumStack.top().computeLodScale(inheritedProjectionMatrix, inheritedViewTransform * modelviewMatrixStack.top());
else
_frustumStack.top().computeLodScale(projectionMatrixStack.top(), modelviewMatrixStack.top());
}
inline void applyFrustum()
{
_frustumStack.top().set(_frustumProjected, modelviewMatrixStack.top());
_frustumStack.top().computeLodScale(projectionMatrixStack.top(), modelviewMatrixStack.top());
}
inline void popFrustum()
{
_frustumStack.pop();
}
template<typename T>
bool intersect(const t_sphere<T>& s) const
{
return _frustumStack.top().intersect(s);
}
template<typename T>
T lodDistance(const t_sphere<T>& s) const
{
const auto& frustum = _frustumStack.top();
if (!frustum.intersect(s)) return -1.0;
const auto& lodScale = frustum.lodScale;
return std::abs(lodScale[0] * s.x + lodScale[1] * s.y + lodScale[2] * s.z + lodScale[3]);
}
};
} // namespace vsg