Skip to content

Commit 73505ff

Browse files
committed
Refactored the State::stateStack so it handles both the view specific and general state stacks
1 parent cefc978 commit 73505ff

3 files changed

Lines changed: 26 additions & 32 deletions

File tree

include/vsg/vk/State.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ namespace vsg
226226
class State : public Inherit<Object, State>
227227
{
228228
public:
229-
explicit State(uint32_t maxStateSlot, uint32_t maxViewSlot);
229+
explicit State(uint32_t in_maxStateSlot, uint32_t in_maxViewSlot);
230230

231231
using StateCommandStack = StateStack<StateCommand>;
232232
using StateStacks = std::vector<StateCommandStack>;
@@ -246,15 +246,18 @@ namespace vsg
246246
dmat4 inheritedViewMatrix;
247247
dmat4 inheritedViewTransform;
248248

249+
uint32_t activeMaxStateSlot = 0;
250+
uint32_t maxStateSlot = 0;
251+
uint32_t maxViewSlot = 0;
252+
249253
StateStacks stateStacks;
250254

251255
uint32_t viewportStateHint = 0;
252-
StateStacks viewStateStacks;
253256

254257
MatrixStack projectionMatrixStack{0};
255258
MatrixStack modelviewMatrixStack{64};
256259

257-
void reserve(uint32_t maxStateSlot, uint32_t maxViewSlot);
260+
void reserve(uint32_t in_maxStateSlot, uint32_t in_maxViewSlot);
258261

259262
void reset();
260263

@@ -290,11 +293,14 @@ namespace vsg
290293
{
291294
if (dirty)
292295
{
293-
for (auto& stateStack : stateStacks)
296+
for(uint32_t slot = 0; slot <= activeMaxStateSlot; ++slot)
294297
{
295-
stateStack.record(*_commandBuffer);
298+
stateStacks[slot].record(*_commandBuffer);
296299
}
297300

301+
// reset the active maxslot to the minimum required
302+
activeMaxStateSlot = maxStateSlot;
303+
298304
projectionMatrixStack.record(*_commandBuffer);
299305
modelviewMatrixStack.record(*_commandBuffer);
300306

@@ -352,8 +358,6 @@ namespace vsg
352358

353359
void popView(const View& view);
354360

355-
void recordView();
356-
357361
inline void pushFrustum()
358362
{
359363
_frustumStack.push(Frustum(_frustumProjected, modelviewMatrixStack.top()));

src/vsg/state/ViewportState.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ using namespace vsg;
1818

1919
ViewportState::ViewportState()
2020
{
21+
slot = 8;
2122
}
2223

2324
ViewportState::ViewportState(const ViewportState& vs) :
@@ -140,6 +141,7 @@ void ViewportState::apply(Context& context, VkGraphicsPipelineCreateInfo& pipeli
140141

141142
void ViewportState::record(CommandBuffer& commandBuffer) const
142143
{
144+
// info("ViewportState::record() ", &commandBuffer);
143145
vkCmdSetScissor(commandBuffer, 0, static_cast<uint32_t>(scissors.size()), scissors.data());
144146
vkCmdSetViewport(commandBuffer, 0, static_cast<uint32_t>(viewports.size()), viewports.data());
145147
}

src/vsg/vk/State.cpp

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,22 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
1616

1717
using namespace vsg;
1818

19-
State::State(uint32_t maxStateSlot, uint32_t maxViewSlot) :
19+
State::State(uint32_t in_maxStateSlot, uint32_t in_maxViewSlot) :
2020
dirty(false)
2121
{
22-
reserve(maxStateSlot, maxViewSlot);
22+
reserve(in_maxStateSlot, in_maxViewSlot);
2323
}
2424

25-
void State::reserve(uint32_t maxStateSlot, uint32_t maxViewSlot)
25+
void State::reserve(uint32_t in_maxStateSlot, uint32_t in_maxViewSlot)
2626
{
27-
//info("State::reserve(", maxStateSlot, ", ", maxViewSlot, ")");
27+
maxStateSlot = in_maxStateSlot;
28+
maxViewSlot = in_maxViewSlot;
29+
activeMaxStateSlot = std::max(maxViewSlot, maxStateSlot);
2830

29-
size_t required_size = static_cast<size_t>(maxStateSlot) + 1;
31+
size_t required_size = static_cast<size_t>(activeMaxStateSlot) + 1;
3032
if (required_size > stateStacks.size()) stateStacks.resize(required_size);
3133

32-
required_size = static_cast<size_t>(maxViewSlot) + 1;
33-
if (required_size > viewStateStacks.size()) viewStateStacks.resize(required_size);
34+
// info("State::reserve(", maxStateSlot, ", ", maxViewSlot, ")");
3435
}
3536

3637
void State::reset()
@@ -41,22 +42,19 @@ void State::reset()
4142
stateStack.reset();
4243
}
4344

44-
for (auto& stateStack : viewStateStacks)
45-
{
46-
stateStack.reset();
47-
}
45+
activeMaxStateSlot = std::max(maxViewSlot, maxStateSlot);
4846
}
4947

5048
void State::pushView(ref_ptr<StateCommand> command)
5149
{
52-
viewStateStacks[command->slot].push(command);
53-
recordView();
50+
stateStacks[command->slot].push(command);
51+
activeMaxStateSlot = std::max(maxViewSlot, maxStateSlot);
5452
}
5553

5654
void State::popView(ref_ptr<StateCommand> command)
5755
{
58-
viewStateStacks[command->slot].pop();
59-
recordView();
56+
stateStacks[command->slot].pop();
57+
activeMaxStateSlot = std::max(maxViewSlot, maxStateSlot);
6058
}
6159

6260
void State::pushView(const View& view)
@@ -70,13 +68,3 @@ void State::popView(const View& view)
7068
//info("State::popView(View&, ", &view, ")");
7169
if ((viewportStateHint & DYNAMIC_VIEWPORTSTATE) && view.camera && view.camera->viewportState) popView(view.camera->viewportState);
7270
}
73-
74-
void State::recordView()
75-
{
76-
//info("State::recordView(..)");
77-
78-
for (auto& stateStack : viewStateStacks)
79-
{
80-
if (!stateStack.empty()) stateStack.record(*_commandBuffer);
81-
}
82-
}

0 commit comments

Comments
 (0)