-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cc
More file actions
286 lines (248 loc) · 9.15 KB
/
main.cc
File metadata and controls
286 lines (248 loc) · 9.15 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
/* -------------------------------------------------------------------------- */
//
// 08 - image based lighting
//
// Where we use a HDR envmap to illuminate a 3D model.
//
// Show how to load and display a glTF object manually, redefining the pipeline
// (see sample_10 for how to use the internal renderer system directly).
//
/* -------------------------------------------------------------------------- */
#include "aer/application.h"
#include "aer/core/arcball_controller.h"
namespace shader_interop {
#include "shaders/interop.h"
}
/* -------------------------------------------------------------------------- */
static constexpr uint32_t kMaxNumTextures = 128u;
/* -------------------------------------------------------------------------- */
class SampleApp final : public Application {
public:
using HostData_t = shader_interop::UniformData;
private:
bool setup() final {
wm_->set_title("08 - Es werde Licht");
renderer_.set_clear_color({ 0.55f, 0.65f, 0.75f, 1.0f });
/* Setup the camera. */
{
/* We use the internal camera_ object, on which we give an ArcBallController
* to be able to move it with input events. */
camera_.makePerspective(
lina::radians(60.0f),
viewport_size_.width,
viewport_size_.height,
0.05f,
750.0f
);
camera_.set_controller(&arcball_controller_);
arcball_controller_.set_view(lina::kTwoPi/16.0f, lina::kTwoPi/8.0f);
arcball_controller_.set_dolly(4.0f);
host_data_.scene = {
.projectionMatrix = camera_.proj(),
};
/* Create and upload the uniform values directly. */
if constexpr (true) {
/* Using an internal transient command buffer. */
uniform_buffer_ = context_.transientCreateBuffer(
&host_data_, sizeof(host_data_),
VK_BUFFER_USAGE_2_UNIFORM_BUFFER_BIT
);
} else {
/* Or writing the data from the host via mapping operations. */
uniform_buffer_ = context_.createBuffer(
sizeof(host_data_),
VK_BUFFER_USAGE_2_UNIFORM_BUFFER_BIT,
VMA_MEMORY_USAGE_CPU_TO_GPU
);
context_.writeBuffer(uniform_buffer_, &host_data_, sizeof(host_data_));
}
}
/* Initialize the skybox and setup the HDR diffuse envmap used to calculate
* IBL convolutions. */
auto &skybox = renderer_.skybox();
skybox.setup(ASSETS_DIR "textures/qwantani_dusk_2_2k.hdr");
/* Load glTF Scene / Scene. */
{
std::string const gltf_filename{ASSETS_DIR "models/"
"suzanne.glb"
};
/* Load the model directly on device, as we do not change the model's internal data
* layout we need to specify how to map its attributes to the shader used. */
scene_ = renderer_.loadGLTF(gltf_filename, {
{ Geometry::AttributeType::Position, shader_interop::kAttribLocation_Position },
{ Geometry::AttributeType::Texcoord, shader_interop::kAttribLocation_Texcoord },
{ Geometry::AttributeType::Normal, shader_interop::kAttribLocation_Normal },
});
LOG_CHECK(scene_->device_images.size() <= kMaxNumTextures); //
}
/* Release the temporary staging buffers.
* This is done automatically at the end of setup(). */
context_.clearStagingBuffers();
/* Descriptor set. */
{
descriptor_set_layout_ = context_.createDescriptorSetLayout({
{
.binding = shader_interop::kDescriptorSetBinding_UniformBuffer,
.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.descriptorCount = 1u,
.stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
.bindingFlags = VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT
| VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT
| VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT
},
{
.binding = shader_interop::kDescriptorSetBinding_Sampler,
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.descriptorCount = kMaxNumTextures,
.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
.bindingFlags = VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT,
},
{
.binding = shader_interop::kDescriptorSetBinding_IrradianceEnvMap,
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.descriptorCount = 1u,
.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
.bindingFlags = VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT,
},
});
descriptor_set_ = context_.createDescriptorSet(descriptor_set_layout_, {
{
.binding = shader_interop::kDescriptorSetBinding_UniformBuffer,
.type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
.buffers = { { uniform_buffer_.buffer } },
},
{
.binding = shader_interop::kDescriptorSetBinding_IrradianceEnvMap,
.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.images = {
{
.sampler = context_.default_sampler(),
.imageView = skybox.irradiance_map().view,
.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
}
},
},
});
}
/* Update the Sampler Atlas descriptor with the currently loaded textures. */
context_.updateDescriptorSet(descriptor_set_, {
{
.binding = shader_interop::kDescriptorSetBinding_Sampler,
.type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.images = scene_->buildDescriptorImageInfos()
}
});
auto shaders{context_.createShaderModules(COMPILED_SHADERS_DIR, {
"simple.vert.glsl",
"simple.frag.glsl",
})};
/* Setup the graphics pipeline. */
{
VkPipelineLayout const pipeline_layout = context_.createPipelineLayout({
.setLayouts = { descriptor_set_layout_ },
.pushConstantRanges = {
{
.stageFlags = VK_SHADER_STAGE_VERTEX_BIT
| VK_SHADER_STAGE_FRAGMENT_BIT
,
.size = sizeof(shader_interop::PushConstant),
}
},
});
graphics_pipeline_ = context_.createGraphicsPipeline(pipeline_layout, {
.dynamicStates = {
VK_DYNAMIC_STATE_VERTEX_INPUT_EXT,
VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY,
},
.vertex = {
.module = shaders[0u].module,
},
.fragment = {
.module = shaders[1u].module,
.targets = {
{
.writeMask = VK_COLOR_COMPONENT_R_BIT
| VK_COLOR_COMPONENT_G_BIT
| VK_COLOR_COMPONENT_B_BIT
| VK_COLOR_COMPONENT_A_BIT
,
}
},
},
.depthStencil = {
.depthTestEnable = VK_TRUE,
.depthWriteEnable = VK_TRUE,
.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL,
},
.primitive = {
.cullMode = VK_CULL_MODE_BACK_BIT,
}
});
}
context_.releaseShaderModules(shaders);
return true;
}
void release() final {
context_.destroyResources(
descriptor_set_layout_,
graphics_pipeline_.layout(),
graphics_pipeline_,
uniform_buffer_
);
scene_.reset();
}
void draw_model(RenderPassEncoder const& pass, mat4 const& world_matrix) {
for (auto const& mesh : scene_->meshes) {
pass.setPrimitiveTopology(mesh->vk_primitive_topology());
push_constant_.model.worldMatrix = linalg::mul(
world_matrix,
mesh->world_matrix()
);
for (auto const& submesh : mesh->submeshes) {
auto const& mat = scene_->material_proxy(*submesh.material_ref);
push_constant_.model.albedo_texture_index = mat.bindings.basecolor;
pass.pushConstant(push_constant_, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT); //
pass.draw(submesh.draw_descriptor, scene_->vertex_buffer, scene_->index_buffer);
}
}
}
void update(float const dt) final {
/* We can check if the camera has changed between frame. */
if (camera_.rebuilt()) {
push_constant_.viewMatrix = camera_.view();
}
}
void draw(CommandEncoder const& cmd) final {
mat4 const worldMatrix{
lina::rotation_matrix_axis(
vec3(-0.25f, 1.0f, -0.15f),
frame_time() * 0.4f
)
};
auto pass = cmd.beginRendering();
{
pass.setViewportScissor(viewport_size_);
/* First render the skybox. */
renderer_.skybox().render(pass, camera_);
/* Then the scene / model. */
pass.bindPipeline(graphics_pipeline_);
{
pass.bindDescriptorSet(descriptor_set_, VK_SHADER_STAGE_VERTEX_BIT);
draw_model(pass, worldMatrix);
}
}
cmd.endRendering();
}
private:
HostData_t host_data_{};
backend::Buffer uniform_buffer_{};
VkDescriptorSetLayout descriptor_set_layout_{};
VkDescriptorSet descriptor_set_{};
shader_interop::PushConstant push_constant_{};
Pipeline graphics_pipeline_{};
ArcBallController arcball_controller_{};
GLTFScene scene_{};
};
// ----------------------------------------------------------------------------
ENTRY_POINT(SampleApp)
/* -------------------------------------------------------------------------- */