-
Notifications
You must be signed in to change notification settings - Fork 2
/
11-occlusion-query.cpp
235 lines (217 loc) · 9.07 KB
/
11-occlusion-query.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
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
#include "../framework/vulkanApp.h"
#include "quadric/include/plane.h"
#include "quadric/include/teapot.h"
#define NON_COHERENT_UNIFORM_BUFFER 0
template<class Type> using DynamicUniformBuffer =
#if NON_COHERENT_UNIFORM_BUFFER
magma::NonCoherentDynamicUniformBuffer<Type>;
#else
magma::DynamicUniformBuffer<Type>;
#endif
// Use L button + mouse to rotate scene
class OcclusionQueryApp : public VulkanApp
{
struct TransformSetTable : magma::DescriptorSetTable
{
magma::descriptor::DynamicUniformBuffer worldViewProj = 0;
MAGMA_REFLECT(worldViewProj)
} setTable0;
struct ColorSetTable : magma::DescriptorSetTable
{
magma::descriptor::DynamicUniformBuffer color = 0;
MAGMA_REFLECT(color)
} setTable1;
std::unique_ptr<quadric::Plane> plane;
std::unique_ptr<quadric::Teapot> teapot;
std::shared_ptr<magma::OcclusionQuery> occlusionQuery;
std::shared_ptr<DynamicUniformBuffer<rapid::matrix>> transformUniforms;
std::shared_ptr<DynamicUniformBuffer<rapid::vector4>> colorUniforms;
std::shared_ptr<magma::DescriptorSet> descriptorSets[2];
std::shared_ptr<magma::PipelineLayout> sharedLayout;
std::shared_ptr<magma::GraphicsPipeline> teapotPipeline;
std::shared_ptr<magma::GraphicsPipeline> planePipeline;
rapid::matrix viewProj;
public:
OcclusionQueryApp(const AppEntry& entry):
VulkanApp(entry, TEXT("11 - Occlusion query"), 512, 512, true)
{
initialize();
setupView();
createOcclusionQuery();
createMeshes();
createUniformBuffers();
setupDescriptorSet();
setupPipeline();
recordCommandBuffer(Buffer::Front);
recordCommandBuffer(Buffer::Back);
}
void render(uint32_t bufferIndex) override
{
updatePerspectiveTransform();
submitCommandBuffer(bufferIndex);
constexpr bool waitForResult = true;
showOcclusionResult(waitForResult);
}
void showOcclusionResult(bool waitForResult)
{
uint64_t sampleCount = 0;
if (waitForResult)
sampleCount = occlusionQuery->getResults<uint64_t>(0, 1, true).front();
else
{
const magma::QueryPool::Result<uint64_t, uint64_t> result = occlusionQuery->getResultsWithAvailability<uint64_t>(0, 1).front();
if (result.availability > 0)
sampleCount = result.result;
std::cout << "Query result: ";
if (result.availability > 0)
std::cout << sampleCount << std::endl;
else // Not ready
std::cout << "---" << std::endl;
}
const std::tstring caption = TEXT("11 - Occlusion query samples: ") + std::to_tstring(sampleCount);
setWindowCaption(caption);
}
void setupView()
{
const rapid::vector3 eye(0.f, 0.f, 10.f);
const rapid::vector3 center(0.f, 0.f, 0.f);
const rapid::vector3 up(0.f, 1.f, 0.f);
constexpr float fov = rapid::radians(60.f);
const float aspect = width/(float)height;
constexpr float zn = 1.f, zf = 100.f;
const rapid::matrix view = rapid::lookAtRH(eye, center, up);
const rapid::matrix proj = rapid::perspectiveFovRH(fov, aspect, zn, zf);
viewProj = view * proj;
}
void updatePerspectiveTransform()
{
const rapid::matrix pitch = rapid::rotationX(rapid::radians(spinY/2.f));
const rapid::matrix yaw = rapid::rotationY(rapid::radians(spinX/2.f));
const rapid::matrix transPlane = rapid::translation(0.f, 0.f, 2.f);
const rapid::matrix transMesh = rapid::translation(0.f, -2.f, 0.f);
const rapid::matrix worldPlane = rapid::rotationX(rapid::radians(90.f)) * transPlane * pitch * yaw;
const rapid::matrix worldMesh = transMesh * pitch * yaw;
magma::helpers::mapScoped<rapid::matrix>(transformUniforms,
[this, &worldPlane, &worldMesh](auto& transforms)
{
transforms[0] = worldPlane * viewProj;
transforms[1] = worldMesh * viewProj;
});
}
void createOcclusionQuery()
{
/* Not setting precise bit may be more efficient on some implementations,
and should be used where it is sufficient to know a boolean result
on whether any samples passed the per-fragment tests.
In this case, some implementations may only return zero or one,
indifferent to the actual number of samples passing the per-fragment tests. */
constexpr bool precise = false;
occlusionQuery = std::make_shared<magma::OcclusionQuery>(device, 1, precise);
}
void createMeshes()
{
constexpr bool twoSided = true;
plane = std::make_unique<quadric::Plane>(6.f, 6.f, twoSided, cmdBufferCopy);
constexpr uint16_t subdivisionDegree = 16;
teapot = std::make_unique<quadric::Teapot>(subdivisionDegree, cmdBufferCopy);
}
void createUniformBuffers()
{
#if NON_COHERENT_UNIFORM_BUFFER
constexpr bool ubFlag = true; // mappedPersistently
#else
const bool ubFlag = device->getFeatures()->supportsDeviceLocalHostVisibleMemory(); // stagedPool
#endif
transformUniforms = std::make_shared<DynamicUniformBuffer<rapid::matrix>>(device, 2, ubFlag);
colorUniforms = std::make_shared<DynamicUniformBuffer<rapid::vector4>>(device, 2, ubFlag);
magma::helpers::mapScoped<rapid::vector4>(colorUniforms,
[](auto& colors)
{ // Update only once
colors[0] = rapid::vector4(0.f, 0.f, 1.f, 1.f);
colors[1] = rapid::vector4(1.f, 0.f, 0.f, 1.f);
});
updatePerspectiveTransform();
}
void setupDescriptorSet()
{
setTable0.worldViewProj = transformUniforms;
descriptorSets[0] = std::make_shared<magma::DescriptorSet>(descriptorPool,
setTable0, VK_SHADER_STAGE_VERTEX_BIT,
nullptr, 0, shaderReflectionFactory, "transform", 0);
setTable1.color = colorUniforms;
descriptorSets[1] = std::make_shared<magma::DescriptorSet>(descriptorPool,
setTable1, VK_SHADER_STAGE_VERTEX_BIT,
nullptr, 0, shaderReflectionFactory, "transform", 1);
}
void setupPipeline()
{
sharedLayout = std::make_shared<magma::PipelineLayout>(
std::initializer_list<std::shared_ptr<const magma::DescriptorSetLayout>>{
descriptorSets[0]->getLayout(),
descriptorSets[1]->getLayout()
});
teapotPipeline = std::make_shared<GraphicsPipeline>(device,
"transform", "fill",
teapot->getVertexInput(),
magma::renderstate::triangleList,
negateViewport ? magma::renderstate::fillCullBackCcw
: magma::renderstate::fillCullBackCw,
magma::renderstate::dontMultisample,
magma::renderstate::depthLessOrEqual,
magma::renderstate::dontBlendRgb,
sharedLayout,
renderPass, 0,
pipelineCache);
planePipeline = std::make_shared<GraphicsPipeline>(device,
"transform", "fill",
plane->getVertexInput(),
magma::renderstate::triangleList,
negateViewport ? magma::renderstate::fillCullBackCcw
: magma::renderstate::fillCullBackCw,
magma::renderstate::dontMultisample,
magma::renderstate::depthLessOrEqual,
magma::renderstate::dontBlendRgb,
sharedLayout,
renderPass, 0,
pipelineCache);
}
void recordCommandBuffer(uint32_t index)
{
auto& cmdBuffer = commandBuffers[index];
cmdBuffer->begin();
{
cmdBuffer->resetQueryPool(occlusionQuery, 0, occlusionQuery->getQueryCount());
cmdBuffer->beginRenderPass(renderPass, framebuffers[index],
{
magma::clear::gray,
magma::clear::depthOne
});
{
cmdBuffer->setViewport(0, 0, width, negateViewport ? -height : height);
cmdBuffer->setScissor(0, 0, width, height);
// Occluder
cmdBuffer->bindPipeline(planePipeline);
cmdBuffer->bindDescriptorSets(planePipeline, 0, {descriptorSets[0], descriptorSets[1]}, {0, 0});
plane->draw(cmdBuffer);
// Occludee
cmdBuffer->bindPipeline(teapotPipeline);
cmdBuffer->bindDescriptorSets(teapotPipeline, 0, {descriptorSets[0], descriptorSets[1]},
{
transformUniforms->getDynamicOffset(1),
colorUniforms->getDynamicOffset(1)
});
cmdBuffer->beginQuery(occlusionQuery, 0);
{
teapot->draw(cmdBuffer);
}
cmdBuffer->endQuery(occlusionQuery, 0);
}
cmdBuffer->endRenderPass();
}
cmdBuffer->end();
}
};
std::unique_ptr<IApplication> appFactory(const AppEntry& entry)
{
return std::unique_ptr<OcclusionQueryApp>(new OcclusionQueryApp(entry));
}