nngn
Loading...
Searching...
No Matches
post.h
Go to the documentation of this file.
1#ifndef NNGN_GRAPHICS_OPENGL_POST_H
2#define NNGN_GRAPHICS_OPENGL_POST_H
3
4#include "graphics/graphics.h"
5#include "utils/flags.h"
6#include "utils/log.h"
7
8#include "prog.h"
9#include "resource.h"
10#include "utils.h"
11#include "vao.h"
12
13namespace nngn {
14
16class GLPost {
17public:
19 bool init();
21 bool destroy();
23 void resize(uvec2 size);
24 bool set_automatic_exposure(bool b);
25 void set_exposure(float e) { this->exposure = e; }
26 void set_bloom_downscale(std::size_t d);
27 void set_bloom_threshold(float t) { this->bloom_threshold = t; }
28 void set_bloom_blur_size(float n) { this->bloom_blur_size = n; }
29 void set_bloom_blur_passes(std::size_t n) { this->bloom_blur_passes = n; }
30 void set_bloom_amount(float a) { this->bloom_amount = a; }
31 void set_blur_downscale(std::size_t d);
32 void set_blur_size(float n) { this->blur_size = n; }
33 void set_blur_passes(std::size_t n) { this->blur_passes = n; }
34 void set_HDR_mix(float m) { this->HDR_mix = m; }
36 bool update();
42 bool bind_frame_buffer();
44 bool render();
45private:
46 enum Flag : u8 {
48 SIZE_UPDATED = 1u << 1,
51 };
52 bool enabled() const;
53 bool HDR_enabled() const;
54 bool bloom_enabled() const;
55 bool blur_enabled() const;
57 bool calc_avg_luminance();
58 bool bloom() const;
59 bool blur() const;
60 bool HDR() const;
64 1.0f / static_cast<float>(Graphics::DEFAULT_BLOOM_DOWNSCALE);
68 float bloom_amount = 0;
69 float blur_scale =
70 1.0f / static_cast<float>(Graphics::DEFAULT_BLUR_DOWNSCALE);
71 float blur_size = 0;
72 std::size_t blur_passes = 0;
73 float HDR_mix = 0;
79 color_tex = {}, depth_tex = {},
82 blur_tex0 = {}, blur_tex1 = {},
84 GLFrameBuffer
87 blur_fb0 = {}, blur_fb1 = {},
92 GLBuffer vbo = {}, ebo = {};
94};
95
96inline void GLPost::resize(uvec2 s) {
97 this->size = s;
98 this->half_size = s / 2u;
99 this->rounded_half_size = {
100 std::bit_floor(s.x) >> 1,
101 std::bit_floor(s.y) >> 1,
102 };
103 this->flags.set(Flag::SIZE_UPDATED);
104}
105
106inline bool GLPost::enabled() const {
107 return this->HDR_enabled() || this->bloom_enabled() || this->blur_enabled();
108}
109
110inline bool GLPost::HDR_enabled() const {
111 return this->HDR_mix != 0;
112}
113
114inline bool GLPost::bloom_enabled() const {
115 return this->bloom_amount != 0;
116}
117
118inline bool GLPost::blur_enabled() const {
119 return this->blur_passes && this->blur_size != 0;
120}
121
122inline void GLPost::set_bloom_downscale(std::size_t d) {
123 this->bloom_scale = 1.0f / static_cast<float>(d);
124 this->flags.set(Flag::BLOOM_SCALE_UPDATED);
125}
126
127inline void GLPost::set_blur_downscale(std::size_t d) {
128 this->blur_scale = 1.0f / static_cast<float>(d);
129 this->flags.set(Flag::BLUR_SCALE_UPDATED);
130}
131
134 return LOG_RESULT(glBindFramebuffer,
135 GL_FRAMEBUFFER, this->enabled() ? this->color_fb.id() : 0);
136}
137
138}
139
140#endif
Post-processing operations.
Definition post.h:16
GLTexArray luminance_res_tex
Definition post.h:81
GLFrameBuffer luminance_fb1
Definition post.h:86
int HDR_mix_loc
Definition post.h:90
bool bloom() const
Definition post.cpp:260
uvec2 half_size
Definition post.h:93
bool init()
Single-step, swap-chain-independent initialization.
Definition post.cpp:19
float bloom_blur_size
Definition post.h:66
VAO blur_vao
Definition post.h:91
int bloom_threshold_loc
Definition post.h:89
float bloom_threshold
Definition post.h:65
uvec2 size
Definition post.h:93
float bloom_scale
Definition post.h:63
void set_HDR_mix(float m)
Definition post.h:34
void set_bloom_blur_size(float n)
Definition post.h:28
VAO hdr_vao
Definition post.h:91
float exposure
Definition post.h:62
GLProgram hdr_prog
Definition post.h:77
int exposure_scale_loc
Definition post.h:90
GLTexArray luminance_last_tex
Definition post.h:81
GLFrameBuffer luminance_res_fb
Definition post.h:86
bool render()
Renders all enabled steps.
Definition post.cpp:204
bool bind_frame_buffer()
Binds the appropriate frame buffer according to the configuration.
Definition post.h:132
GLBuffer vbo
Definition post.h:92
GLFrameBuffer blur_fb1
Definition post.h:87
bool blur_enabled() const
Definition post.h:118
GLFrameBuffer blur_fb0
Definition post.h:87
bool blur() const
Definition post.cpp:296
int bloom_amount_loc
Definition post.h:89
GLTexArray blur_tex0
Definition post.h:82
bool update()
Updates resources as necessary.
Definition post.cpp:127
VAO luminance_vao
Definition post.h:91
float blur_size
Definition post.h:71
void set_bloom_blur_passes(std::size_t n)
Definition post.h:29
Flag
Definition post.h:46
@ BLUR_SCALE_UPDATED
Definition post.h:50
@ SIZE_UPDATED
Definition post.h:48
@ AUTOMATIC_EXPOSURE
Definition post.h:47
@ BLOOM_SCALE_UPDATED
Definition post.h:49
float blur_scale
Definition post.h:69
uvec2 rounded_half_size
Definition post.h:93
void set_blur_size(float n)
Definition post.h:32
GLProgram luminance_prog
Definition post.h:74
GLTexArray blur_tex1
Definition post.h:82
bool set_automatic_exposure(bool b)
Definition post.cpp:190
bool enabled() const
Definition post.h:106
void set_bloom_downscale(std::size_t d)
Definition post.h:122
GLTexArray bloom_blur_tex0
Definition post.h:83
GLBuffer ebo
Definition post.h:92
GLTexArray depth_tex
Definition post.h:79
GLProgram blur_prog
Definition post.h:76
int blur_dir_loc
Definition post.h:89
std::size_t bloom_blur_passes
Definition post.h:67
void set_bloom_threshold(float t)
Definition post.h:27
VAO bloom_filter_vao
Definition post.h:91
GLFrameBuffer bloom_blur_fb0
Definition post.h:88
GLTexArray luminance_tex1
Definition post.h:80
bool bloom_enabled() const
Definition post.h:114
void set_exposure(float e)
Definition post.h:25
bool destroy()
Releases all resources.
Definition post.cpp:103
GLFrameBuffer luminance_fb0
Definition post.h:86
GLFrameBuffer color_fb
Definition post.h:85
void set_bloom_amount(float a)
Definition post.h:30
GLFrameBuffer bloom_blur_fb1
Definition post.h:88
GLProgram bloom_prog
Definition post.h:75
float HDR_mix
Definition post.h:73
GLTexArray color_tex
Definition post.h:79
GLTexArray bloom_blur_tex1
Definition post.h:83
void resize(uvec2 size)
Signals that the frame buffer has been resized.
Definition post.h:96
void set_blur_downscale(std::size_t d)
Definition post.h:127
bool HDR() const
Definition post.cpp:335
GLTexArray luminance_tex0
Definition post.h:80
std::size_t blur_passes
Definition post.h:72
bool calc_avg_luminance()
Definition post.cpp:211
bool HDR_enabled() const
Definition post.h:110
Flags< Flag > flags
Definition post.h:61
float bloom_amount
Definition post.h:68
bool resize_frame_buffers()
void set_blur_passes(std::size_t n)
Definition post.h:33
u32 id() const
Definition handle.h:24
local n
Definition dump_lights.lua:5
#define NNGN_LOG_CONTEXT_CF(c)
Definition log.h:11
Definition audio.cpp:7
std::uint8_t u8
Definition def.h:12
#define LOG_RESULT(f,...)
Definition opencl.cpp:264
local a
Definition input.lua:35
local m
Definition input.lua:28
Wrapper for a small unsigned integral representing flags.
Definition flags.h:18
constexpr Flags & set(AT a)
Definition flags.h:47
Definition resource.h:13
Definition prog.h:19
Definition resource.h:24
static constexpr std::size_t DEFAULT_BLOOM_DOWNSCALE
Definition graphics.h:273
static constexpr float DEFAULT_BLOOM_THRESHOLD
Definition graphics.h:270
static constexpr float DEFAULT_BLOOM_BLUR_SIZE
Definition graphics.h:271
static constexpr float DEFAULT_EXPOSURE
Definition graphics.h:269
static constexpr std::size_t DEFAULT_BLOOM_BLUR_PASSES
Definition graphics.h:275
static constexpr std::size_t DEFAULT_BLUR_DOWNSCALE
Definition graphics.h:274
Definition vao.h:11
local e
Definition math.lua:4
std::chrono::seconds s
Definition timing.cpp:6