-
Notifications
You must be signed in to change notification settings - Fork 2
/
pre_compute.h
291 lines (237 loc) · 7.2 KB
/
pre_compute.h
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
#ifndef __PRE_COMPUTE_H__
#define __PRE_COMPUTE_H__
#include <thread>
#include <future>
#include <vector>
#include <string>
#include <iomanip>
float radical_inverse(unsigned int bits) {
bits = (bits << 16u) | (bits >> 16u);
bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u);
bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u);
bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u);
bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u);
return float(bits) * 2.3283064365386963e-10f; // / 0x100000000
}
void hammersley(unsigned int i, unsigned int N, float &e1, float &e2)
{
e1 = 1.0f * i / N;
e2 = radical_inverse(i);
}
vector3_t hemisphere_sample_uniform(float u, float v)
{
float phi = v * 2.0f * cPI;
float cosTheta = 1.0f - u;
float sinTheta = sqrt(1.0f - cosTheta * cosTheta);
return vector3_t(cos(phi) * sinTheta, sin(phi) * sinTheta, cosTheta);
}
// [ tan_x ]
// | tan_y |
// [ n ]
// makeup a rotation matrix
void makeup_rot(const vector3_t& n, vector3_t& tan_x, vector3_t& tan_y)
{
vector3_t up = fabs(n.z) < 0.999f ? vector3_t(0.0f, 0.0f, 1.0f) : vector3_t(1.0f, 0.0f, 0.0f);
tan_x = cross(up, n);
tan_x.normalize();
tan_y = cross(n, tan_x);
tan_y.normalize();
}
void generate_irradiance_map(const std::string& src_texpath, const std::string& dst_texpath)
{
cube_texture_t src_cube_tex;
src_cube_tex.load_tex(src_texpath, false);
int siz = src_cube_tex.size();
cube_texture_t dst_cube_tex(siz);
auto face_filter_func = [&](texture2d_t& face, int face_id) {
for (int i = 0; i < siz; ++i)
{
for (int j = 0; j < siz; ++j)
{
texcoord_t tc;
tc.u = 1.0f * i / (siz - 1);
tc.v = 1.0f * j / (siz - 1);
vector3_t n;
cube_uv_to_direction(face_id, tc, n);
vector3_t tan_x, tan_y;
makeup_rot(n, tan_x, tan_y);
vector3_t irradiance(0, 0, 0);
const int sample_num = 8192;
for (int k = 0; k < sample_num; ++k)
{
float e1, e2;
hammersley(k, sample_num, e1, e2);
// h is hemisphere direction in tangent space
vector3_t h = hemisphere_sample_uniform(e1, e2);
// convert h to world space
vector3_t l = tan_x * h.x + tan_y * h.y + n * h.z;
l.normalize();
vector3_t color = src_cube_tex.sample(l).to_vec3();
float NoL = max(dot(n, l), 0);
irradiance += color * NoL;
}
irradiance = irradiance * 2.0f / float(sample_num);
face.write_at(i, j, irradiance);
}
}
};
std::vector<std::future<void>> futures;
for (int face_id = 0; face_id < 6; ++face_id) {
texture2d_t& face = dst_cube_tex.get_face(face_id);
futures.push_back(std::move(std::async(std::launch::async, [&face_filter_func, &face, face_id]() {
face_filter_func(face, face_id);
})));
}
for (auto& future : futures) {
future.get();
}
dst_cube_tex.save_tex(dst_texpath, false);
}
vector3_t importance_sample_GGX(float e1, float e2, float roughness)
{
float a = roughness * roughness;
float a2 = a * a;
float phi = 2 * cPI * e1;
float cos_theta = sqrt((1 - e2) / (1 + (a2 - 1) * e2));
float sin_theta = sqrt(1 - cos_theta* cos_theta);
vector3_t h;
h.x = sin_theta * cos(phi);
h.y = sin_theta * sin(phi);
h.z = cos_theta;
return vector3_t(h.x, h.y, h.z);
}
// reference to "Real Shading in Unreal Engine 4. by Brian Karis, Epic Games"
void generate_prefilter_envmap(const std::string& src_texpath, const std::string& dst_texpath)
{
cube_texture_t src_cube_tex;
src_cube_tex.load_tex(src_texpath, false);
auto face_filter_func = [&](float roughness, int siz, texture2d_t& face, int face_id) {
for (int i = 0; i < siz; ++i)
{
for (int j = 0; j < siz; ++j)
{
texcoord_t tc;
tc.u = 1.0f * i / (siz - 1);
tc.v = 1.0f * j / (siz - 1);
vector3_t n;
cube_uv_to_direction(face_id, tc, n);
vector3_t tan_x, tan_y;
makeup_rot(n, tan_x, tan_y);
vector3_t v = n; // specular is dependent on view direction, so assume v == n == r for precompute!
vector3_t filtered_color(0, 0, 0);
float weight = 0;
const int sample_num = 1024;
for (int k = 0; k < sample_num; ++k)
{
float e1, e2;
hammersley(k, sample_num, e1, e2);
vector3_t s = importance_sample_GGX(e1, e2, roughness);
// convert normal direction s to world space
vector3_t h = tan_x * s.x + tan_y * s.y + n * s.z;
h.normalize();
vector3_t l = reflect(h, v);
l.normalize();
float NoL = max(dot(n, l), 0.0f);
if (NoL > 0)
{
vector3_t color = src_cube_tex.sample(l).to_vec3();
filtered_color += color * NoL; // here the weight NoL is not present in Equation
weight += NoL;
}
}
filtered_color /= max(weight, 0.001f);
face.write_at(i, j, filtered_color);
}
}
};
int max_siz = src_cube_tex.size();
int mip_num = (int)ceil(std::log2(max_siz)) + 1;
for (int i = 0; i < mip_num; ++i)
{
int siz = (max_siz >> i);
if (siz <= 0) {
break;
}
cube_texture_t dst_cube_tex(siz);
float roughness = 1.0f * i / (mip_num - 1);
std::vector<std::future<void>> futures;
for (int face_id = 0; face_id < 6; ++face_id) {
texture2d_t& face = dst_cube_tex.get_face(face_id);
futures.push_back(std::move(std::async(std::launch::async, [&face_filter_func, roughness, siz, &face, face_id]() {
face_filter_func(roughness, siz, face, face_id);
})));
}
for (auto& future : futures) {
future.get();
}
const char* ext = ::strrchr(src_texpath.c_str(), '.');
dst_cube_tex.save_tex(dst_texpath + "_mip_" + std::to_string(i) + ext, false);
}
}
float schlickGGX_geometry_ibl(float n_dot_v, float roughness)
{
float k = roughness * roughness / 2.0f;
return n_dot_v / (n_dot_v * (1 - k) + k);
}
float geometry_smith_ibl(float n_dot_v, float n_dot_l, float roughness)
{
float g1 = schlickGGX_geometry_ibl(n_dot_v, roughness);
float g2 = schlickGGX_geometry_ibl(n_dot_l, roughness);
return g1 * g2;
}
vector3_t IntegrateBRDF(float NdotV, float roughness)
{
vector3_t v;
v.x = sqrt(1.0f - NdotV * NdotV);
v.y = 0.0f;
v.z = NdotV;
float A = 0.0f;
float B = 0.0f;
vector3_t n(0.0f, 0.0f, 1.0f);
vector3_t tan_x, tan_y;
makeup_rot(n, tan_x, tan_y);
const int SAMPLE_COUNT = 1024;
for (int i = 0u; i < SAMPLE_COUNT; ++i)
{
float e1, e2;
hammersley(i, SAMPLE_COUNT, e1, e2);
vector3_t s = importance_sample_GGX(e1, e2, roughness);
// convert h to world space
vector3_t h = tan_x * s.x + tan_y * s.y + n * s.z;
h.normalize();
vector3_t l = reflect(h, v);
l.normalize();
float NoL = max(l.z, 0.0f);
float NoV = max(v.z, 0.0f);
float NoH = max(h.z, 0.0f);
float VoH = max(dot(v, h), 0.0f);
if (NoL > 0.0)
{
float G = geometry_smith_ibl(NoV, NoL, roughness);
float G_Vis = (G * VoH) / (NoH * NoV);
float Fc = pow(1.0f - VoH, 5.0f);
A += (1.0f - Fc) * G_Vis;
B += Fc * G_Vis;
}
}
A /= float(SAMPLE_COUNT);
B /= float(SAMPLE_COUNT);
return vector3_t(A, B, 0.0f);
}
void generate_BRDF_LUT(const std::string& dst_texpath, int siz = 256)
{
texture2d_t lut;
lut.init(siz, siz);
for (int i = 0; i < siz; ++i)
{
float NoV = 1.0f * i / (siz - 1.0f);
for (int j = 0; j < siz; ++j)
{
float roughness = 1.0f * j / (siz - 1.0f);
vector3_t c = IntegrateBRDF(NoV, roughness);
lut.write_at(i, j, c);
}
}
lut.save_tex(dst_texpath.c_str(), false);
}
#endif // __PRE_COMPUTE_H__