forked from LukasBanana/LLGL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
206 lines (163 loc) · 6.53 KB
/
Copy pathmain.cpp
File metadata and controls
206 lines (163 loc) · 6.53 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
/*
* main.cpp (Tutorial04_Query)
*
* This file is part of the "LLGL" project (Copyright (c) 2015 by Lukas Hermanns)
* See "LICENSE.txt" for license information.
*/
#include "../tutorial.h"
class Tutorial04 : public Tutorial
{
LLGL::ShaderProgram* shaderProgram = nullptr;
LLGL::GraphicsPipeline* occlusionPipeline = nullptr;
LLGL::GraphicsPipeline* scenePipeline = nullptr;
LLGL::Buffer* vertexBuffer = nullptr;
LLGL::Buffer* indexBuffer = nullptr;
LLGL::Buffer* constantBuffer = nullptr;
LLGL::Query* occlusionQuery = nullptr;
LLGL::Query* geometryQuery = nullptr;
bool occlusionCullingEnabled = true;
struct Settings
{
Gs::Matrix4f wvpMatrix;
LLGL::ColorRGBAf color;
}
settings;
public:
Tutorial04() :
Tutorial { L"LLGL Tutorial 04: Query" }
{
// Create all graphics objects
auto vertexFormat = CreateBuffers();
shaderProgram = LoadStandardShaderProgram(vertexFormat);
CreatePipelines();
CreateQueries();
// Print some information
std::cout << "press TAB KEY to enable/disable occlusion culling" << std::endl;
}
LLGL::VertexFormat CreateBuffers()
{
// Specify vertex format
LLGL::VertexFormat vertexFormat;
vertexFormat.AppendAttribute({ "position", LLGL::VectorType::Float3 });
// Create vertex, index, and constant buffer
vertexBuffer = CreateVertexBuffer(GenerateCubeVertices(), vertexFormat);
indexBuffer = CreateIndexBuffer(GenerateCubeTriangleIndices(), LLGL::DataType::UInt32);
constantBuffer = CreateConstantBuffer(settings);
return vertexFormat;
}
void CreatePipelines()
{
// Create graphics pipeline for occlusion query
LLGL::GraphicsPipelineDescriptor pipelineDesc;
{
pipelineDesc.shaderProgram = shaderProgram;
pipelineDesc.depth.testEnabled = true;
pipelineDesc.rasterizer.multiSampling = LLGL::MultiSamplingDescriptor(8);
LLGL::BlendTargetDescriptor blendDesc;
{
blendDesc.colorMask = LLGL::ColorRGBAb(false);
}
pipelineDesc.blend.targets.push_back(blendDesc);
}
occlusionPipeline = renderer->CreateGraphicsPipeline(pipelineDesc);
// Create graphics pipeline for scene rendering
{
pipelineDesc.depth.testEnabled = true;
pipelineDesc.depth.writeEnabled = true;
pipelineDesc.blend.targets[0].colorMask = LLGL::ColorRGBAb(true);
}
scenePipeline = renderer->CreateGraphicsPipeline(pipelineDesc);
}
void CreateQueries()
{
// Create query to determine if any samples passed the depth test (occlusion query)
LLGL::QueryDescriptor queryDesc;
{
queryDesc.type = LLGL::QueryType::AnySamplesPassed;
queryDesc.renderCondition = true;
}
occlusionQuery = renderer->CreateQuery(queryDesc);
// Create query to determine number of primitives that are sent to the rasterizer
{
queryDesc.type = LLGL::QueryType::PrimitivesGenerated;
queryDesc.renderCondition = false;
}
geometryQuery = renderer->CreateQuery(queryDesc);
}
std::uint64_t GetAndSyncQueryResult(LLGL::Query* query)
{
// Wait until query result is available and return result
std::uint64_t result = 0;
while (!commands->QueryResult(*query, result)) { /* wait */ }
return result;
}
void PrintQueryResult()
{
std::cout << "occlusion culling: " << (occlusionCullingEnabled ? "enabled" : "disabled");
std::cout << ", primitives generated: " << GetAndSyncQueryResult(geometryQuery);
if (occlusionCullingEnabled)
std::cout << ", geometry visible: " << (GetAndSyncQueryResult(occlusionQuery) != 0 ? "yes" : "no");
std::cout << " \r";
std::flush(std::cout);
}
void SetBoxColor(const LLGL::ColorRGBAf& color)
{
settings.color = color;
UpdateBuffer(constantBuffer, settings);
}
private:
void OnDrawFrame() override
{
// Update user input
if (input->KeyDown(LLGL::Key::Tab))
occlusionCullingEnabled = !occlusionCullingEnabled;
// Update matrices in constant buffer
static float anim;
anim += 0.01f;
settings.wvpMatrix = projection;
Gs::RotateFree(settings.wvpMatrix, { 0, 1, 0 }, Gs::Deg2Rad(std::sin(anim)*55.0f));
Gs::Translate(settings.wvpMatrix, { 0, 0, 5 });
Gs::RotateFree(settings.wvpMatrix, Gs::Vector3f(1).Normalized(), anim*3);
// Clear color and depth buffers
commands->Clear(LLGL::ClearFlags::Color | LLGL::ClearFlags::Depth);
// Set buffers
commands->SetVertexBuffer(*vertexBuffer);
commands->SetIndexBuffer(*indexBuffer);
commands->SetConstantBuffer(*constantBuffer, 0, LLGL::ShaderStageFlags::VertexStage | LLGL::ShaderStageFlags::FragmentStage);
// Start with qeometry query
commands->BeginQuery(*geometryQuery);
{
if (occlusionCullingEnabled)
{
// Draw box for occlusion query
commands->SetGraphicsPipeline(*occlusionPipeline);
SetBoxColor({ 1, 1, 1 });
commands->BeginQuery(*occlusionQuery);
{
commands->DrawIndexed(36, 0);
}
commands->EndQuery(*occlusionQuery);
// Draw scene
commands->SetGraphicsPipeline(*scenePipeline);
SetBoxColor({ 0, 1, 0 });
commands->BeginRenderCondition(*occlusionQuery, LLGL::RenderConditionMode::Wait);
{
commands->DrawIndexed(36, 0);
}
commands->EndRenderCondition();
}
else
{
// Draw scene without occlusion query
commands->SetGraphicsPipeline(*scenePipeline);
SetBoxColor({ 0, 1, 0 });
commands->DrawIndexed(36, 0);
}
}
commands->EndQuery(*geometryQuery);
PrintQueryResult();
// Present result on the screen
context->Present();
}
};
LLGL_IMPLEMENT_TUTORIAL(Tutorial04);