-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmbientOcclusion.cpp
More file actions
756 lines (592 loc) · 28 KB
/
AmbientOcclusion.cpp
File metadata and controls
756 lines (592 loc) · 28 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
#include "GLUIComponent.h"
#include "Scene/Scene.h"
#include "Scene/SceneHelper.h"
#include "Skybox.h"
#include "imgui.h"
#include <GL/glew.h>
#include <GLSample.h>
#include <GLSampleWindow.h>
#include <ImageImport.h>
#include <ImportHelper.h>
#include <Math/NormalDistribution.h>
#include <ModelImporter.h>
#include <ShaderLoader.h>
#include <glm/geometric.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <random>
namespace glsample {
/**
* Screen Space Ambient Occlusion post processing effect. Both World and Depth Space.
* TODO: Downscale.
*/
class ScreenSpaceAmbientOcclusion : public GLSampleWindow {
public:
ScreenSpaceAmbientOcclusion() : GLSampleWindow() {
this->setTitle("ScreenSpace AmbientOcclusion");
/* Setting Window. */
this->ambientOcclusionSettingComponent = std::make_shared<AmbientOcclusionSettingComponent>(*this);
this->addUIComponent(this->ambientOcclusionSettingComponent);
/* Default camera position and orientation. */
this->camera.setPosition(glm::vec3(-2.5f));
this->camera.lookAt(glm::vec3(0.f));
}
static const int maxKernels = 64;
struct uniform_buffer_block {
glm::mat4 model{};
glm::mat4 view{};
glm::mat4 proj{};
glm::mat4 modelView{};
glm::mat4 modelViewProjection{};
/* Light source. */
DirectionalLightData directional_light;
/* Material settings. */
glm::vec4 specularColor{};
glm::vec4 ambientColor{};
glm::vec4 viewDir{};
float shininess{};
} uniformStageBlock;
struct UniformSSAOBufferBlock {
glm::mat4 proj{};
/* */
int samples = 64;
float radius = 10.5f;
float intensity = 1.0f;
float bias = 0.025;
/* */
glm::vec4 kernel[maxKernels]{};
glm::vec4 color{};
glm::vec2 screen{};
} uniformStageBlockSSAO;
/* */
MeshObject plan;
Skybox skybox;
Scene *scene{};
/* G-Buffer */
unsigned int multipass_framebuffer{};
unsigned int multipass_texture_width{};
unsigned int multipass_texture_height{};
std::vector<unsigned int> multipass_textures;
unsigned int world_position_sampler = 0;
unsigned int depthTexture{};
unsigned int ssao_framebuffer{};
unsigned int ssaoTexture{};
unsigned int ssao_down_sample_index = 2;
/* White texture for each object. */
unsigned int white_texture{};
/* Random direction texture. */
unsigned int random_texture{};
/* */
unsigned int multipass_program{};
unsigned int ssao_world_program{};
unsigned int ssao_depth_program{};
unsigned int texture_program{};
/* Uniform buffer. */
unsigned int uniform_buffer_binding = 0;
unsigned int uniform_ssao_buffer_binding = 1;
unsigned int uniform_buffer{};
unsigned int uniform_ssao_buffer{};
const size_t nrUniformBuffer = 3;
/* */
size_t uniformBufferAlignedSize = sizeof(uniform_buffer_block);
size_t uniformSSAOBufferAlignSize = sizeof(UniformSSAOBufferBlock);
CameraController camera;
class AmbientOcclusionSettingComponent : public GLUIComponent<ScreenSpaceAmbientOcclusion> {
public:
AmbientOcclusionSettingComponent(ScreenSpaceAmbientOcclusion &base)
: GLUIComponent<ScreenSpaceAmbientOcclusion>(base, "Ambient Occlusion Settings"),
uniform_ssao(this->getRefSample().uniformStageBlockSSAO) {}
void draw() override {
ImGui::TextUnformatted("Ambient Occlusion Settings");
ImGui::DragFloat("Intensity", &uniform_ssao.intensity, 0.1f, 0.0f);
ImGui::DragFloat("Radius", &uniform_ssao.radius, 0.35f, 0.0f);
ImGui::DragInt("Sample", &uniform_ssao.samples, 1, 0);
ImGui::DragFloat("Bias", &uniform_ssao.bias, 0.01f, 0, 1);
ImGui::Checkbox("DownSample", &this->downScale);
ImGui::Checkbox("Use Depth Only", &this->useDepthOnly);
ImGui::TextUnformatted("Light Settings");
ImGui::ColorEdit4("Light", &this->getRefSample().uniformStageBlock.directional_light.lightColor[0],
ImGuiColorEditFlags_Float | ImGuiColorEditFlags_HDR);
ImGui::DragFloat3("Direction",
&this->getRefSample().uniformStageBlock.directional_light.lightDirection[0]);
ImGui::TextUnformatted("Debugging");
ImGui::Checkbox("Show Only AO", &this->showAOOnly);
ImGui::Checkbox("Show GBuffer", &this->showGBuffers);
ImGui::Checkbox("Show Wireframe", &this->showWireframe);
ImGui::Checkbox("Use AO", &this->useAO);
this->drawCameraController(this->getRefSample().camera);
}
bool showWireframe = false;
bool downScale = false;
bool useDepthOnly = false;
bool showAOOnly = true;
bool showGBuffers = false;
bool useAO = true;
private:
struct UniformSSAOBufferBlock &uniform_ssao;
};
std::shared_ptr<AmbientOcclusionSettingComponent> ambientOcclusionSettingComponent;
/* Shader to extract values. */
const std::string vertexMultiPassShaderPath = "Shaders/multipass/multipass.vert.spv";
const std::string fragmentMultiPassShaderPath = "Shaders/multipass/multipass.frag.spv";
/* */
const std::string vertexSSAOShaderPath = "Shaders/ambientocclusion/ambientocclusion.vert.spv";
const std::string fragmentSSAOShaderPath = "Shaders/ambientocclusion/ambientocclusion.frag.spv";
/* */
const std::string vertexSSAODepthOnlyShaderPath = "Shaders/ambientocclusion/ambientocclusion.vert.spv";
const std::string fragmentSSAODepthOnlyShaderPath =
"Shaders/ambientocclusion/ambientocclusion_depthonly.frag.spv";
/* */
const std::string vertexOverlayShaderPath = "Shaders/postprocessingeffects/overlay.vert.spv";
const std::string fragmentOverlayTextureShaderPath = "Shaders/postprocessingeffects/overlay.frag.spv";
void Release() override {
this->scene->release();
/* Delete graphic pipelines. */
glDeleteProgram(this->ssao_world_program);
glDeleteProgram(this->ssao_depth_program);
glDeleteProgram(this->multipass_program);
glDeleteProgram(this->texture_program);
/* */
glDeleteFramebuffers(1, &this->multipass_framebuffer);
glDeleteFramebuffers(1, &this->ssao_framebuffer);
/* Delete textures. */
glDeleteTextures(1, &this->depthTexture);
glDeleteTextures(this->multipass_textures.size(), this->multipass_textures.data());
glDeleteTextures(1, &this->random_texture);
glDeleteTextures(1, &this->white_texture);
glDeleteTextures(1, &this->ssaoTexture);
/* Delete uniform buffer. */
glDeleteBuffers(1, &this->uniform_buffer);
/* Delete geometry data. */
glDeleteVertexArrays(1, &this->plan.vao);
glDeleteBuffers(1, &this->plan.vbo);
glDeleteBuffers(1, &this->plan.ibo);
}
void Initialize() override {
const std::string modelPath = this->getResult()["model"].as<std::string>();
const std::string skyboxPath = this->getResult()["skybox"].as<std::string>();
{
fragcore::ShaderCompiler::CompilerConvertOption compilerOptions;
compilerOptions.target = fragcore::ShaderLanguage::GLSL;
compilerOptions.glslVersion = this->getShaderVersion();
/* Load shader source. */
const std::vector<uint32_t> vertex_ssao_binary =
IOUtil::readFileData<uint32_t>(this->vertexSSAOShaderPath, this->getFileSystem());
const std::vector<uint32_t> fragment_ssao_binary =
IOUtil::readFileData<uint32_t>(this->fragmentSSAOShaderPath, this->getFileSystem());
/* Load shader source. */
const std::vector<uint32_t> vertex_ssao_depth_only_binary =
IOUtil::readFileData<uint32_t>(this->vertexSSAODepthOnlyShaderPath, this->getFileSystem());
const std::vector<uint32_t> fragment_ssao_depth_onlysource =
IOUtil::readFileData<uint32_t>(this->fragmentSSAODepthOnlyShaderPath, this->getFileSystem());
const std::vector<uint32_t> vertex_multi_pass_binary =
IOUtil::readFileData<uint32_t>(this->vertexMultiPassShaderPath, this->getFileSystem());
const std::vector<uint32_t> fragment_multi_pass_binary =
IOUtil::readFileData<uint32_t>(this->fragmentMultiPassShaderPath, this->getFileSystem());
/* */
const std::vector<uint32_t> texture_vertex_binary =
IOUtil::readFileData<uint32_t>(this->vertexOverlayShaderPath, this->getFileSystem());
const std::vector<uint32_t> texture_fragment_binary =
IOUtil::readFileData<uint32_t>(this->fragmentOverlayTextureShaderPath, this->getFileSystem());
/* Load shader */
this->ssao_world_program =
ShaderLoader::loadGraphicProgram(compilerOptions, &vertex_ssao_binary, &fragment_ssao_binary);
/* Load shader */
this->ssao_depth_program = ShaderLoader::loadGraphicProgram(
compilerOptions, &vertex_ssao_depth_only_binary, &fragment_ssao_depth_onlysource);
/* Load shader */
this->multipass_program = ShaderLoader::loadGraphicProgram(compilerOptions, &vertex_multi_pass_binary,
&fragment_multi_pass_binary);
/* Load shader */
this->texture_program =
ShaderLoader::loadGraphicProgram(compilerOptions, &texture_vertex_binary, &texture_fragment_binary);
}
/* Setup graphic ambient occlusion pipeline. */
glUseProgram(this->ssao_world_program);
int uniform_ssao_world_buffer_index =
glGetUniformBlockIndex(this->ssao_world_program, "UniformBufferBlock");
glUniform1iARB(glGetUniformLocation(this->ssao_world_program, "WorldTexture"), 1);
glUniform1iARB(glGetUniformLocation(this->ssao_world_program, "NormalTexture"), 3);
glUniform1iARB(glGetUniformLocation(this->ssao_world_program, "DepthTexture"), 4);
glUniform1iARB(glGetUniformLocation(this->ssao_world_program, "NormalRandomize"), 5);
glUniformBlockBinding(this->ssao_world_program, uniform_ssao_world_buffer_index,
this->uniform_ssao_buffer_binding);
glUseProgram(0);
/* Setup graphic ambient occlusion pipeline. */
glUseProgram(this->ssao_depth_program);
int uniform_ssao_depth_buffer_index =
glGetUniformBlockIndex(this->ssao_depth_program, "UniformBufferBlock");
glUniform1iARB(glGetUniformLocation(this->ssao_depth_program, "WorldTexture"), 1);
glUniform1iARB(glGetUniformLocation(this->ssao_depth_program, "NormalTexture"), 3);
glUniform1iARB(glGetUniformLocation(this->ssao_depth_program, "DepthTexture"), 4);
glUniform1iARB(glGetUniformLocation(this->ssao_depth_program, "NormalRandomize"), 5);
glUniformBlockBinding(this->ssao_depth_program, uniform_ssao_depth_buffer_index,
this->uniform_ssao_buffer_binding);
glUseProgram(0);
/* Setup graphic multipass pipeline. */
glUseProgram(this->multipass_program);
int uniform_buffer_index = glGetUniformBlockIndex(this->multipass_program, "UniformBufferBlock");
glUniform1iARB(glGetUniformLocation(this->multipass_program, "DiffuseTexture"), 0);
glUniform1iARB(glGetUniformLocation(this->multipass_program, "NormalTexture"), 1);
glUniform1iARB(glGetUniformLocation(this->multipass_program, "AlphaMaskedTexture"), 2);
glUniformBlockBinding(this->multipass_program, uniform_buffer_index, this->uniform_buffer_binding);
glUseProgram(0);
/* Setup graphic program. */
glUseProgram(this->texture_program);
glUniform1i(glGetUniformLocation(this->texture_program, "ColorTexture"), 0);
glUseProgram(0);
/* Align uniform buffer in respect to driver requirement. */
GLint minMapBufferSize = 0;
glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &minMapBufferSize);
this->uniformBufferAlignedSize =
fragcore::Math::align<size_t>(this->uniformBufferAlignedSize, (size_t)minMapBufferSize);
this->uniformSSAOBufferAlignSize =
fragcore::Math::align<size_t>(this->uniformSSAOBufferAlignSize, (size_t)minMapBufferSize);
/* */
glGenBuffers(1, &this->uniform_buffer);
glBindBufferARB(GL_UNIFORM_BUFFER, this->uniform_buffer);
glBufferData(GL_UNIFORM_BUFFER, this->uniformBufferAlignedSize * this->nrUniformBuffer, nullptr,
GL_DYNAMIC_DRAW);
glBindBufferARB(GL_UNIFORM_BUFFER, 0);
/* */
glGenBuffers(1, &this->uniform_ssao_buffer);
glBindBufferARB(GL_UNIFORM_BUFFER, this->uniform_ssao_buffer);
glBufferData(GL_UNIFORM_BUFFER, this->uniformSSAOBufferAlignSize * this->nrUniformBuffer, nullptr,
GL_DYNAMIC_DRAW);
glBindBufferARB(GL_UNIFORM_BUFFER, 0);
/* */
ModelImporter modelLoader(FileSystem::getFileSystem());
modelLoader.loadContent(modelPath, 0);
/* load Textures */
TextureImporter textureImporter(this->getFileSystem());
unsigned int skytexture = textureImporter.loadImage2D(skyboxPath);
skybox.init(skytexture, Skybox::loadDefaultPanoramicProgram(this->getFileSystem()));
this->scene = SceneHelper::loadFrom(modelLoader);
/* Load geometry. */
CommonUtil::loadPlan(this->plan, 1, 1, 1);
/* FIXME: improve vectors. */
{
/* Create random vector. */
std::uniform_real_distribution<float> randomFloats(0.0, 1.0); // random floats between [0.0, 1.0]
std::default_random_engine generator;
for (size_t i = 0; i < maxKernels; ++i) {
glm::vec3 sample((randomFloats(generator) * 2.0) - 1.0, (randomFloats(generator) * 2.0) - 1.0,
randomFloats(generator));
sample = glm::normalize(sample);
sample *= randomFloats(generator);
float scale = (float)i / static_cast<float>(maxKernels);
scale = fragcore::Math::lerp(0.1f, 1.0f, scale * scale);
sample *= scale;
this->uniformStageBlockSSAO.kernel[i] = glm::vec4(sample, 0);
}
/* Create white texture. */
this->white_texture = CommonUtil::createColorTexture(1, 1, Color::white());
/* Create noise normalMap. */
const size_t noiseW = 4;
const size_t noiseH = 4;
std::vector<glm::vec3> ssaoRandomNoise(noiseW * noiseH);
for (size_t i = 0; i < ssaoRandomNoise.size(); i++) {
ssaoRandomNoise[i].r = randomFloats(generator) * 2.0 - 1.0;
ssaoRandomNoise[i].g = randomFloats(generator) * 2.0 - 1.0;
ssaoRandomNoise[i].b = 0.0f;
/* */
ssaoRandomNoise[i] = glm::normalize(ssaoRandomNoise[i]);
}
/* Create random texture. */
glGenTextures(1, &this->random_texture);
glBindTexture(GL_TEXTURE_2D, this->random_texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, noiseW, noiseH, 0, GL_RGB, GL_FLOAT, ssaoRandomNoise.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
/* Border clamped to max value, it makes the outside area. */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
FVALIDATE_GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 0));
FVALIDATE_GL_CALL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, 0.0f));
FVALIDATE_GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0));
glBindTexture(GL_TEXTURE_2D, 0);
}
/* Create sampler. */
glCreateSamplers(1, &this->world_position_sampler);
glSamplerParameteri(this->world_position_sampler, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glSamplerParameteri(this->world_position_sampler, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glSamplerParameteri(this->world_position_sampler, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glSamplerParameteri(this->world_position_sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glSamplerParameteri(this->world_position_sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glSamplerParameterf(this->world_position_sampler, GL_TEXTURE_LOD_BIAS, 0.0f);
glSamplerParameteri(this->world_position_sampler, GL_TEXTURE_MAX_LOD, 0);
glSamplerParameteri(this->world_position_sampler, GL_TEXTURE_MIN_LOD, 0);
/* Create multipass framebuffer. */
{
glGenFramebuffers(1, &this->multipass_framebuffer);
glGenFramebuffers(1, &this->ssao_framebuffer);
/* Setup framebuffer textures. */
this->multipass_textures.resize(4);
glGenTextures(this->multipass_textures.size(), this->multipass_textures.data());
glGenTextures(1, &this->depthTexture);
glGenTextures(1, &this->ssaoTexture);
this->onResize(this->width(), this->height());
}
this->camera.setNear(0.15f);
this->camera.setFar(1500.0f);
}
void onResize(int width, int height) override {
this->multipass_texture_width = width;
this->multipass_texture_height = height;
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, this->multipass_framebuffer);
/* Resize the image. */
std::vector<GLenum> drawAttach(multipass_textures.size());
for (size_t i = 0; i < multipass_textures.size(); i++) {
/* */
glBindTexture(GL_TEXTURE_2D, this->multipass_textures[i]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, this->multipass_texture_width,
this->multipass_texture_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
/* Border clamped to max value, it makes the outside area. */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
FVALIDATE_GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 0));
FVALIDATE_GL_CALL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, 0.0f));
FVALIDATE_GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0));
glBindTexture(GL_TEXTURE_2D, 0);
/* */
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D,
this->multipass_textures[i], 0);
drawAttach[i] = GL_COLOR_ATTACHMENT0 + i;
}
/* Create depth buffer texture. */
glBindTexture(GL_TEXTURE_2D, this->depthTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, this->multipass_texture_width,
this->multipass_texture_height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);
FVALIDATE_GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 0));
FVALIDATE_GL_CALL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, 0.0f));
FVALIDATE_GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0));
/* */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
/* Border clamped to max value, it makes the outside area. */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
const float borderColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0);
glDrawBuffers(drawAttach.size(), drawAttach.data());
/* Validate if created properly.*/
const int frameStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if (frameStatus != GL_FRAMEBUFFER_COMPLETE) {
throw RuntimeException("Failed to create framebuffer, {}", frameStatus);
}
{
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, this->ssao_framebuffer);
glBindTexture(GL_TEXTURE_2D, this->ssaoTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, this->multipass_texture_width, this->multipass_texture_height, 0,
GL_RED, GL_FLOAT, nullptr);
GLint swizzleMask[] = {GL_RED, GL_RED, GL_RED, GL_ONE};
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
FVALIDATE_GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_LOD, 0));
FVALIDATE_GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 4));
FVALIDATE_GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 4));
FVALIDATE_GL_CALL(glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, 0.0f));
FVALIDATE_GL_CALL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0));
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->ssaoTexture, 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, this->getDefaultFramebuffer());
}
/* Update camera aspect. */
this->camera.setAspect((float)width / (float)height);
this->camera.setFar(1500.0f);
this->camera.setNear(0.5f);
}
void draw() override {
this->update();
size_t width = 0, height = 0;
this->getCurrentFrameBufferSize(&width, &height);
/* */
this->uniformStageBlockSSAO.screen = glm::vec2(width, height);
/* G-Buffer extraction. */
{
/* */
glBindBufferRange(GL_UNIFORM_BUFFER, this->uniform_buffer_binding, this->uniform_buffer,
(this->getFrameCount() % this->nrUniformBuffer) * this->uniformBufferAlignedSize,
this->uniformBufferAlignedSize);
glBindFramebuffer(GL_FRAMEBUFFER, this->multipass_framebuffer);
glViewport(0, 0, this->multipass_texture_width, this->multipass_texture_height);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* */
glUseProgram(this->multipass_program);
glDisable(GL_CULL_FACE);
this->scene->render();
glUseProgram(0);
skybox.render(this->camera);
}
/* Draw post processing effect - Screen Space Ambient Occlusion. */
if (this->ambientOcclusionSettingComponent->useAO) {
glBindBufferRange(GL_UNIFORM_BUFFER, this->uniform_ssao_buffer_binding, this->uniform_ssao_buffer,
(this->getFrameCount() % this->nrUniformBuffer) * this->uniformSSAOBufferAlignSize,
this->uniformSSAOBufferAlignSize);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, this->ssao_framebuffer);
glViewport(0, 0, width, height);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_DEPTH_BUFFER_BIT);
if (this->ambientOcclusionSettingComponent->useDepthOnly) {
glUseProgram(this->ssao_depth_program);
} else {
glUseProgram(this->ssao_world_program);
}
glDisable(GL_CULL_FACE);
for (size_t i = 0; i < this->multipass_textures.size(); i++) {
/* */
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, this->multipass_textures[i]);
glBindSampler(i, this->world_position_sampler);
}
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, this->depthTexture);
glActiveTexture(GL_TEXTURE5);
glBindTexture(GL_TEXTURE_2D, this->random_texture);
/* Draw. */
glBindVertexArray(this->plan.vao);
glDrawElements(GL_TRIANGLES, this->plan.nrIndicesElements, GL_UNSIGNED_INT, nullptr);
glBindVertexArray(0);
glUseProgram(0);
/* Downscale for creating a cheap blurred version, using the pixel bilinear filtering for each power
* of 2. */
// TODO fix downscale.
if (this->ambientOcclusionSettingComponent->downScale) {
/* Downscale the image. */
glBindFramebuffer(GL_READ_FRAMEBUFFER, this->ssao_framebuffer);
glReadBuffer(GL_COLOR_ATTACHMENT0);
// TODO: check if a better way for downsampling.
glBindTexture(GL_TEXTURE_2D, this->ssaoTexture);
glGenerateMipmap(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
}
}
/* Render final result. */
{
/* Show only ambient Occlusion. */
if (this->ambientOcclusionSettingComponent->showAOOnly) {
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, this->getDefaultFramebuffer());
glBindFramebuffer(GL_READ_FRAMEBUFFER, this->ssao_framebuffer);
glViewport(0, 0, width, height);
// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->ssaoTexture,
// ssao_down_sample_index);
glBlitFramebuffer(0, 0, this->multipass_texture_width, this->multipass_texture_height, 0, 0, width,
height, GL_COLOR_BUFFER_BIT, GL_LINEAR);
// glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->ssaoTexture,
// 0);
} else { /* Blend with final result. */
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, this->getDefaultFramebuffer());
glBindFramebuffer(GL_READ_FRAMEBUFFER, this->multipass_framebuffer);
glReadBuffer(GL_COLOR_ATTACHMENT0);
glViewport(0, 0, width, height);
glBlitFramebuffer(0, 0, this->multipass_texture_width, this->multipass_texture_height, 0, 0, width,
height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
glBindFramebuffer(GL_FRAMEBUFFER, this->getDefaultFramebuffer());
glDisable(GL_CULL_FACE);
glCullFace(GL_FRONT_AND_BACK);
glDisable(GL_DEPTH_TEST);
/* graphic pipeline. - texture */
glUseProgram(this->texture_program);
/* Draw. */
glBindVertexArray(this->plan.vao);
glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_DST_COLOR, GL_ZERO);
// Blend to color layer.
if (this->ambientOcclusionSettingComponent->useAO) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, this->ssaoTexture);
if (this->ambientOcclusionSettingComponent->downScale) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, this->ssao_down_sample_index);
}
/* */
glDrawElements(GL_TRIANGLES, this->plan.nrIndicesElements, GL_UNSIGNED_INT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glBindTexture(GL_TEXTURE_2D, 0);
}
glBindVertexArray(0);
glUseProgram(0);
}
}
if (this->ambientOcclusionSettingComponent->showGBuffers) {
/* Blit image targets to screen. */
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, this->getDefaultFramebuffer());
glBindFramebuffer(GL_READ_FRAMEBUFFER, this->multipass_framebuffer);
const size_t widthDivior = 2;
const size_t heightDivior = 2;
/* Transfer each target to default framebuffer. */
const float halfW = (width / widthDivior) * 0.5f;
const float halfH = (height / heightDivior) * 0.5f;
for (size_t i = 0; i < this->multipass_textures.size(); i++) {
glReadBuffer(GL_COLOR_ATTACHMENT0 + i);
glBlitFramebuffer(0, 0, this->multipass_texture_width, this->multipass_texture_height,
(i % 2) * (halfW), (i / 2) * halfH, halfW + ((i % 2) * halfW),
halfH + ((i / 2) * halfH), GL_COLOR_BUFFER_BIT, GL_LINEAR);
}
}
glBindFramebuffer(GL_FRAMEBUFFER, this->getDefaultFramebuffer());
}
void update() override {
/* Update Camera and Scene. */
this->camera.update(this->getTimer().deltaTime<float>());
this->scene->update(this->getTimer().deltaTime<float>());
this->uniformStageBlock.proj = this->camera.getProjectionMatrix();
this->uniformStageBlockSSAO.proj = this->camera.getProjectionMatrix();
/* */
{
this->uniformStageBlock.model = glm::mat4(1.0f);
this->uniformStageBlock.model = glm::scale(this->uniformStageBlock.model, glm::vec3(1.f));
this->uniformStageBlock.view = this->camera.getViewMatrix();
this->uniformStageBlock.modelView = (this->uniformStageBlock.view * this->uniformStageBlock.model);
this->uniformStageBlock.modelViewProjection =
this->uniformStageBlock.proj * this->uniformStageBlock.view * this->uniformStageBlock.model;
}
/* Update uniform buffers. */
{
glBindBufferARB(GL_UNIFORM_BUFFER, this->uniform_buffer);
void *uniformPointer = glMapBufferRange(
GL_UNIFORM_BUFFER,
((this->getFrameCount() + 1) % this->nrUniformBuffer) * this->uniformBufferAlignedSize,
this->uniformBufferAlignedSize,
GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_UNSYNCHRONIZED_BIT);
memcpy(uniformPointer, &this->uniformStageBlock, sizeof(uniformStageBlock));
glUnmapBufferARB(GL_UNIFORM_BUFFER);
/* */
glBindBufferARB(GL_UNIFORM_BUFFER, this->uniform_ssao_buffer);
void *uniformSSAOPointer = glMapBufferRange(
GL_UNIFORM_BUFFER,
((this->getFrameCount() + 1) % this->nrUniformBuffer) * this->uniformSSAOBufferAlignSize,
this->uniformSSAOBufferAlignSize,
GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT | GL_MAP_UNSYNCHRONIZED_BIT);
memcpy(uniformSSAOPointer, &this->uniformStageBlockSSAO, sizeof(uniformStageBlockSSAO));
glUnmapBufferARB(GL_UNIFORM_BUFFER);
}
}
};
/* */
class AmbientOcclusionGLSample : public GLSample<ScreenSpaceAmbientOcclusion> {
public:
AmbientOcclusionGLSample() : GLSample<ScreenSpaceAmbientOcclusion>() {}
void customOptions(cxxopts::OptionAdder &options) override {
options("M,model", "Model Path", cxxopts::value<std::string>()->default_value("asset/sponza/sponza.obj"))(
"S,skybox", "Skybox Texture File Path",
cxxopts::value<std::string>()->default_value("asset/snowy_forest_4k.exr"));
}
};
} // namespace glsample
int main(int argc, const char **argv) {
try {
glsample::AmbientOcclusionGLSample sample;
sample.run(argc, argv);
} catch (const std::exception &ex) {
std::cerr << cxxexcept::getStackMessage(ex) << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}