forked from vsg-dev/VulkanSceneGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindowResizeHandler.cpp
152 lines (118 loc) · 5.14 KB
/
WindowResizeHandler.cpp
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
/* <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/commands/ClearAttachments.h>
#include <vsg/io/Options.h>
#include <vsg/nodes/StateGroup.h>
#include <vsg/viewer/View.h>
#include <vsg/viewer/WindowResizeHandler.h>
#include <vsg/vk/Context.h>
#include <vsg/vk/State.h>
#include <iostream>
using namespace vsg;
WindowResizeHandler::WindowResizeHandler()
{
}
void WindowResizeHandler::scale_rect(VkRect2D& rect)
{
int32_t edge_x = rect.offset.x + static_cast<int32_t>(rect.extent.width);
int32_t edge_y = rect.offset.y + static_cast<int32_t>(rect.extent.height);
rect.offset.x = scale_parameter(rect.offset.x, previous_extent.width, new_extent.width);
rect.offset.y = scale_parameter(rect.offset.y, previous_extent.height, new_extent.height);
rect.extent.width = static_cast<uint32_t>(scale_parameter(edge_x, previous_extent.width, new_extent.width) - rect.offset.x);
rect.extent.height = static_cast<uint32_t>(scale_parameter(edge_y, previous_extent.height, new_extent.height) - rect.offset.y);
}
bool WindowResizeHandler::visit(const Object* object, uint32_t index)
{
decltype(visited)::value_type objectIndex(object, index);
if (visited.count(objectIndex) != 0) return false;
visited.insert(objectIndex);
return true;
}
void WindowResizeHandler::apply(vsg::BindGraphicsPipeline& bindPipeline)
{
GraphicsPipeline* graphicsPipeline = bindPipeline.pipeline;
if (!visit(graphicsPipeline, context->viewID))
{
return;
}
if (graphicsPipeline)
{
struct ContainsViewport : public ConstVisitor
{
bool foundViewport = false;
void apply(const ViewportState&) override { foundViewport = true; }
bool operator()(const GraphicsPipeline& gp)
{
for (auto& pipelineState : gp.pipelineStates)
{
pipelineState->accept(*this);
}
return foundViewport;
}
} containsViewport;
bool needToRegenerateGraphicsPipeline = !containsViewport(*graphicsPipeline);
if (needToRegenerateGraphicsPipeline)
{
graphicsPipeline->release(context->viewID);
graphicsPipeline->compile(*context);
}
}
}
void WindowResizeHandler::apply(vsg::Object& object)
{
object.traverse(*this);
}
void WindowResizeHandler::apply(vsg::StateGroup& sg)
{
if (!visit(&sg, context->viewID)) return;
for (auto& command : sg.stateCommands)
{
command->accept(*this);
}
sg.traverse(*this);
}
void WindowResizeHandler::apply(ClearAttachments& clearAttachments)
{
if (!visit(&clearAttachments)) return;
for (auto& clearRect : clearAttachments.rects)
{
auto& rect = clearRect.rect;
scale_rect(rect);
}
}
void WindowResizeHandler::apply(vsg::View& view)
{
if (!visit(&view)) return;
context->viewID = view.viewID;
if (!view.camera)
{
view.traverse(*this);
return;
}
view.camera->projectionMatrix->changeExtent(previous_extent, new_extent);
auto viewportState = view.camera->viewportState;
size_t num_viewports = std::min(viewportState->viewports.size(), viewportState->scissors.size());
for (size_t i = 0; i < num_viewports; ++i)
{
auto& viewport = viewportState->viewports[i];
auto& scissor = viewportState->scissors[i];
bool renderAreaMatches = (renderArea.offset.x == scissor.offset.x) && (renderArea.offset.y == scissor.offset.y) &&
(renderArea.extent.width == scissor.extent.width) && (renderArea.extent.height == scissor.extent.height);
scale_rect(scissor);
viewport.x = static_cast<float>(scissor.offset.x);
viewport.y = static_cast<float>(scissor.offset.y);
viewport.width = static_cast<float>(scissor.extent.width);
viewport.height = static_cast<float>(scissor.extent.height);
if (renderAreaMatches)
{
renderArea = scissor;
}
}
context->defaultPipelineStates.emplace_back(viewportState);
view.traverse(*this);
context->defaultPipelineStates.pop_back();
}