-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathr_ssao.cpp
184 lines (141 loc) · 5.21 KB
/
r_ssao.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
#include "r_ssao.h"
#include "m_trig.h"
#include "u_vector.h"
#include "u_misc.h"
namespace r {
bool ssaoMethod::init(const u::vector<const char *> &defines) {
if (!method::init("screen space ambient occlusion"))
return false;
for (const auto &it : defines)
method::define(it);
if (gl::has(gl::ARB_texture_rectangle))
method::define("HAS_TEXTURE_RECTANGLE");
method::define("kKernelSize", kKernelSize);
method::define("kKernelFactor", m::sin(m::kPi / float(kKernelSize)));
method::define("kKernelOffset", 1.0f / kKernelSize);
if (!addShader(GL_VERTEX_SHADER, "shaders/ssao.vs"))
return false;
if (!addShader(GL_FRAGMENT_SHADER, "shaders/ssao.fs"))
return false;
if (!finalize({ "position" }))
return false;
m_occluderBias = getUniform("gOccluderBias", uniform::kFloat);
m_samplingRadius = getUniform("gSamplingRadius", uniform::kFloat);
m_attenuation = getUniform("gAttenuation", uniform::kVec2);
m_inverse = getUniform("gInverse", uniform::kMat4);
m_WVP = getUniform("gWVP", uniform::kMat4);
m_screenFrustum = getUniform("gScreenFrustum", uniform::kVec2);
m_screenSize = getUniform("gScreenSize", uniform::kVec2);
m_normalTexture = getUniform("gNormalMap", uniform::kSampler);
m_depthTexture = getUniform("gDepthMap", uniform::kSampler);
m_randomTexture = getUniform("gRandomMap", uniform::kSampler);
for (size_t i = 0; i < kKernelSize; i++)
m_kernel[i] = getUniform(u::format("gKernel[%zu]", i), uniform::kVec2);
post();
// Setup the kernel
// Note: This must be changed as well if kKernelSize changes
static const m::vec2 kKernel[kKernelSize] = {
{ 0.0f, 1.0f, },
{ 1.0f, 0.0f, },
{ 0.0f, -1.0f, },
{ -1.0f, 0.0f }
};
enable();
for (const auto &it : kKernel)
m_kernel[&it - kKernel]->set(it);
return true;
}
void ssaoMethod::setOccluderBias(float bias) {
m_occluderBias->set(bias);
}
void ssaoMethod::setSamplingRadius(float radius) {
m_samplingRadius->set(radius);
}
void ssaoMethod::setAttenuation(float constant, float linear) {
m_attenuation->set(m::vec2(constant, linear));
}
void ssaoMethod::setInverse(const m::mat4 &inverse) {
m_inverse->set(inverse);
}
void ssaoMethod::setWVP(const m::mat4 &wvp) {
m_WVP->set(wvp);
}
void ssaoMethod::setPerspective(const m::perspective &p) {
m_screenFrustum->set(m::vec2(p.nearp, p.farp));
m_screenSize->set(m::vec2(p.width, p.height));
}
void ssaoMethod::setNormalTextureUnit(int unit) {
m_normalTexture->set(unit);
}
void ssaoMethod::setDepthTextureUnit(int unit) {
m_depthTexture->set(unit);
}
void ssaoMethod::setRandomTextureUnit(int unit) {
m_randomTexture->set(unit);
}
ssao::ssao()
: m_fbo(0)
, m_width(0)
, m_height(0)
{
m_textures[kBuffer] = 0;
m_textures[kRandom] = 0;
}
ssao::~ssao() {
destroy();
}
bool ssao::init(const m::perspective &p) {
m_width = p.width / 2;
m_height = p.height / 2;
const GLenum format = gl::has(gl::ARB_texture_rectangle) ? GL_TEXTURE_RECTANGLE : GL_TEXTURE_2D;
gl::GenTextures(2, m_textures);
// 16-bit RED
gl::BindTexture(format, m_textures[kBuffer]);
gl::TexParameteri(format, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
gl::TexParameteri(format, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
gl::TexParameteri(format, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gl::TexParameterf(format, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
gl::TexImage2D(format, 0, GL_R16F, m_width, m_height, 0, GL_RED, GL_FLOAT, nullptr);
// 8-bit RED random texture
u::vector<unsigned char> random;
random.reserve(kRandomSize * kRandomSize);
for (size_t i = 0; i < kRandomSize; i++)
for (size_t j = 0; j < kRandomSize; j++)
random.push_back(u::randu() % 0xFF);
gl::BindTexture(format, m_textures[kRandom]);
gl::TexImage2D(format, 0, GL_R8, kRandomSize, kRandomSize, 0, GL_RED, GL_UNSIGNED_BYTE, &random[0]);
gl::GenFramebuffers(1, &m_fbo);
gl::BindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo);
gl::FramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, format, m_textures[kBuffer], 0);
static GLuint drawBuffers[] = { GL_COLOR_ATTACHMENT0 };
gl::DrawBuffers(1, drawBuffers);
const GLenum status = gl::CheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
if (status != GL_FRAMEBUFFER_COMPLETE)
return false;
gl::BindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
return true;
}
void ssao::update(const m::perspective &p) {
const size_t width = p.width / 2;
const size_t height = p.height / 2;
if (m_width == width && m_height == height)
return;
const GLenum format = gl::has(gl::ARB_texture_rectangle) ? GL_TEXTURE_RECTANGLE : GL_TEXTURE_2D;
m_width = width;
m_height = height;
gl::BindTexture(format, m_textures[kBuffer]);
gl::TexImage2D(format, 0, GL_R16F, m_width, m_height, 0, GL_RED, GL_FLOAT, nullptr);
}
void ssao::destroy() {
if (m_fbo)
gl::DeleteFramebuffers(1, &m_fbo);
if (m_textures[0])
gl::DeleteTextures(2, m_textures);
}
void ssao::bindWriting() {
gl::BindFramebuffer(GL_DRAW_FRAMEBUFFER, m_fbo);
}
GLuint ssao::texture(textureType type) const {
return m_textures[type];
}
}