forked from BachiLi/redner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexture.h
More file actions
498 lines (476 loc) · 20.8 KB
/
texture.h
File metadata and controls
498 lines (476 loc) · 20.8 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
#pragma once
#include "redner.h"
#include "vector.h"
#include "ptr.h"
#include "atomic.h"
struct Texture1 {
Texture1() {}
Texture1(ptr<float> texels,
int width,
int height,
int num_levels,
ptr<float> uv_scale)
: texels(texels.get()),
width(width), height(height),
num_levels(num_levels),
uv_scale(uv_scale.get()) {}
float *texels;
int width;
int height;
int num_levels;
float *uv_scale;
};
struct Texture3 {
Texture3() {}
Texture3(ptr<float> texels,
int width,
int height,
int num_levels,
ptr<float> uv_scale)
: texels(texels.get()),
width(width), height(height),
num_levels(num_levels),
uv_scale(uv_scale.get()) {}
float *texels;
int width;
int height;
int num_levels;
float *uv_scale;
};
DEVICE
inline Vector3 bilinear_interp(const Texture3 &tex,
int xfi, int yfi,
int xci, int yci,
Real u, Real v,
int level) {
auto texels = tex.texels + level * tex.width * tex.height * 3;
auto color_ff = Vector3f{texels[3 * (yfi * tex.width + xfi) + 0],
texels[3 * (yfi * tex.width + xfi) + 1],
texels[3 * (yfi * tex.width + xfi) + 2]};
auto color_cf = Vector3f{texels[3 * (yfi * tex.width + xci) + 0],
texels[3 * (yfi * tex.width + xci) + 1],
texels[3 * (yfi * tex.width + xci) + 2]};
auto color_fc = Vector3f{texels[3 * (yci * tex.width + xfi) + 0],
texels[3 * (yci * tex.width + xfi) + 1],
texels[3 * (yci * tex.width + xfi) + 2]};
auto color_cc = Vector3f{texels[3 * (yci * tex.width + xci) + 0],
texels[3 * (yci * tex.width + xci) + 1],
texels[3 * (yci * tex.width + xci) + 2]};
auto color = color_ff * (1.f - u) * (1.f - v) +
color_fc * (1.f - u) * v +
color_cf * u * (1.f - v) +
color_cc * u * v;
return color;
}
DEVICE
inline void d_bilinear_interp(const Texture3 &tex,
int xfi, int yfi,
int xci, int yci,
Real u, Real v,
int level,
const Vector3 &d_output,
Vector3f &d_color_ff, Vector3f &d_color_cf,
Vector3f &d_color_fc, Vector3f &d_color_cc,
Real &d_u, Real &d_v) {
auto texels = tex.texels + level * tex.width * tex.height * 3;
auto color_ff = Vector3f{texels[3 * (yfi * tex.width + xfi) + 0],
texels[3 * (yfi * tex.width + xfi) + 1],
texels[3 * (yfi * tex.width + xfi) + 2]};
auto color_cf = Vector3f{texels[3 * (yfi * tex.width + xci) + 0],
texels[3 * (yfi * tex.width + xci) + 1],
texels[3 * (yfi * tex.width + xci) + 2]};
auto color_fc = Vector3f{texels[3 * (yci * tex.width + xfi) + 0],
texels[3 * (yci * tex.width + xfi) + 1],
texels[3 * (yci * tex.width + xfi) + 2]};
auto color_cc = Vector3f{texels[3 * (yci * tex.width + xci) + 0],
texels[3 * (yci * tex.width + xci) + 1],
texels[3 * (yci * tex.width + xci) + 2]};
// color = color_ff * (1.f - u) * (1.f - v) +
// color_fc * (1.f - u) * v +
// color_cf * u * (1.f - v) +
// color_cc * u * v;
d_color_ff = d_output * (1.f - u) * (1.f - v);
d_color_cf = d_output * u * (1.f - v);
d_color_fc = d_output * (1.f - u) * v ;
d_color_cc = d_output * u * v ;
d_u += sum(d_output * (-color_ff * (1.f - v) +
color_cf * (1.f - v) +
-color_fc * v +
color_cc * v));
d_v += sum(d_output * (-color_ff * (1.f - u) +
-color_cf * u +
color_fc * (1.f - u) +
color_cc * u));
}
DEVICE
inline void d_bilinear_interp(const Texture3 &tex,
int xfi, int yfi,
int xci, int yci,
Real u, Real v,
int level,
const Vector3 &d_output,
Texture3 &d_tex,
Real &d_u, Real &d_v) {
auto texels = tex.texels + level * tex.width * tex.height * 3;
auto color_ff = Vector3f{texels[3 * (yfi * tex.width + xfi) + 0],
texels[3 * (yfi * tex.width + xfi) + 1],
texels[3 * (yfi * tex.width + xfi) + 2]};
auto color_cf = Vector3f{texels[3 * (yfi * tex.width + xci) + 0],
texels[3 * (yfi * tex.width + xci) + 1],
texels[3 * (yfi * tex.width + xci) + 2]};
auto color_fc = Vector3f{texels[3 * (yci * tex.width + xfi) + 0],
texels[3 * (yci * tex.width + xfi) + 1],
texels[3 * (yci * tex.width + xfi) + 2]};
auto color_cc = Vector3f{texels[3 * (yci * tex.width + xci) + 0],
texels[3 * (yci * tex.width + xci) + 1],
texels[3 * (yci * tex.width + xci) + 2]};
// color = color_ff * (1.f - u) * (1.f - v) +
// color_fc * (1.f - u) * v +
// color_cf * u * (1.f - v) +
// color_cc * u * v;
auto d_texels = d_tex.texels + level * tex.width * tex.height * 3;
// d_color_ff
atomic_add(&d_texels[3 * (yfi * tex.width + xfi)], d_output * (1.f - u) * (1.f - v));
// d_color_fc
atomic_add(&d_texels[3 * (yfi * tex.width + xci)], d_output * u * (1.f - v));
// d_color_cf
atomic_add(&d_texels[3 * (yci * tex.width + xfi)], d_output * (1.f - u) * v );
// d_color_cc
atomic_add(&d_texels[3 * (yci * tex.width + xci)], d_output * u * v );
d_u += sum(d_output * (-color_ff * (1.f - v) +
color_cf * (1.f - v) +
-color_fc * v +
color_cc * v));
d_v += sum(d_output * (-color_ff * (1.f - u) +
-color_cf * u +
color_fc * (1.f - u) +
color_cc * u));
}
DEVICE
inline Real bilinear_interp(const Texture1 &tex,
int xfi, int yfi,
int xci, int yci,
Real u, Real v,
int level) {
auto texels = tex.texels + level * tex.width * tex.height;
auto value_ff = texels[yfi * tex.width + xfi];
auto value_cf = texels[yfi * tex.width + xci];
auto value_fc = texels[yci * tex.width + xfi];
auto value_cc = texels[yci * tex.width + xci];
auto value = value_ff * (1.f - u) * (1.f - v) +
value_fc * (1.f - u) * v +
value_cf * u * (1.f - v) +
value_cc * u * v;
return value;
}
DEVICE
inline void d_bilinear_interp(const Texture1 &tex,
int xfi, int yfi,
int xci, int yci,
Real u, Real v,
int level,
const Real d_output,
Real &d_value_ff, Real &d_value_cf,
Real &d_value_fc, Real &d_value_cc,
Real &d_u, Real &d_v) {
auto texels = tex.texels + level * tex.width * tex.height;
auto value_ff = texels[yfi * tex.width + xfi];
auto value_cf = texels[yfi * tex.width + xci];
auto value_fc = texels[yci * tex.width + xfi];
auto value_cc = texels[yci * tex.width + xci];
// value = value_ff * (1.f - u) * (1.f - v) +
// value_fc * (1.f - u) * v +
// value_cf * u * (1.f - v) +
// value_cc * u * v;
d_value_ff = d_output * (1.f - u) * (1.f - v);
d_value_cf = d_output * u * (1.f - v);
d_value_fc = d_output * (1.f - u) * v ;
d_value_cc = d_output * u * v ;
d_u += d_output * (-value_ff * (1.f - v) +
value_cf * (1.f - v) +
-value_fc * v +
value_cc * v);
d_v += d_output * (-value_ff * (1.f - u) +
-value_cf * u +
value_fc * (1.f - u) +
value_cc * u);
}
DEVICE
inline void d_bilinear_interp(const Texture1 &tex,
int xfi, int yfi,
int xci, int yci,
Real u, Real v,
int level,
const Real d_output,
Texture1 &d_tex,
Real &d_u, Real &d_v) {
auto texels = tex.texels + level * tex.width * tex.height;
auto value_ff = texels[yfi * tex.width + xfi];
auto value_cf = texels[yfi * tex.width + xci];
auto value_fc = texels[yci * tex.width + xfi];
auto value_cc = texels[yci * tex.width + xci];
// value = value_ff * (1.f - u) * (1.f - v) +
// value_fc * (1.f - u) * v +
// value_cf * u * (1.f - v) +
// value_cc * u * v;
auto d_texels = d_tex.texels + level * tex.width * tex.height;
// d_value_ff
atomic_add(&d_texels[yfi * tex.width + xfi], d_output * (1.f - u) * (1.f - v));
// d_value_cf
atomic_add(&d_texels[yfi * tex.width + xci], d_output * (1.f - u) * v );
// d_value_fc
atomic_add(&d_texels[yci * tex.width + xfi], d_output * u * (1.f - v));
// d_value_cc
atomic_add(&d_texels[yci * tex.width + xci], d_output * u * v );
d_u += d_output * (-value_ff * (1.f - v) +
value_cf * (1.f - v) +
-value_fc * v +
value_cc * v);
d_v += d_output * (-value_ff * (1.f - u) +
-value_cf * u +
value_fc * (1.f - u) +
value_cc * u);
}
DEVICE
inline Vector3 get_texture_value_constant(const Texture3 &tex) {
return Vector3{tex.texels[0], tex.texels[1], tex.texels[2]};
}
DEVICE
inline Real get_texture_value_constant(const Texture1 &tex) {
return tex.texels[0];
}
template <typename TextureType>
DEVICE
inline auto get_texture_value(const TextureType &tex,
const Vector2 &uv_,
const Vector2 &du_dxy_,
const Vector2 &dv_dxy_) {
if (tex.num_levels <= 0) {
// Constant texture
return get_texture_value_constant(tex);
} else {
// Trilinear interpolation
auto uv_scale = Vector2f{tex.uv_scale[0], tex.uv_scale[1]};
auto uv = uv_ * uv_scale;
auto du_dxy = du_dxy_ * uv_scale[0];
auto dv_dxy = dv_dxy_ * uv_scale[1];
auto x = uv[0] * tex.width - 0.5f;
auto y = uv[1] * tex.height - 0.5f;
auto xf = (int)floor(x);
auto yf = (int)floor(y);
auto xc = xf + 1;
auto yc = yf + 1;
auto u = x - xf;
auto v = y - yf;
auto xfi = modulo(xf, tex.width);
auto yfi = modulo(yf, tex.height);
auto xci = modulo(xc, tex.width);
auto yci = modulo(yc, tex.height);
auto max_footprint = max(length(du_dxy) * tex.width, length(dv_dxy) * tex.height);
auto level = log2(max(max_footprint, Real(1e-8f)));
if (level <= 0) {
return bilinear_interp(tex, xfi, yfi, xci, yci, u, v, 0);
} else if (level >= tex.num_levels - 1) {
return bilinear_interp(tex, xfi, yfi, xci, yci, u, v, tex.num_levels - 1);
} else {
auto li = (int)floor(level);
auto ld = level - li;
return (1 - ld) * bilinear_interp(tex, xfi, yfi, xci, yci, u, v, li) +
ld * bilinear_interp(tex, xfi, yfi, xci, yci, u, v, li + 1);
}
}
}
template <typename TextureType, typename OutputType, typename DTextureType>
DEVICE
inline void d_get_texture_value(const TextureType &tex,
const Vector2 &uv_,
const Vector2 &du_dxy_,
const Vector2 &dv_dxy_,
const OutputType &d_output,
DTextureType &d_tex,
Vector2 &d_uv_,
Vector2 &d_du_dxy_,
Vector2 &d_dv_dxy_) {
if (tex.width <= 0 && tex.height <= 0) {
// Constant texture
// output = Vector3{tex.texels[0], tex.texels[1], tex.texels[2]};
d_tex.t000 = d_output;
} else {
// Trilinear interpolation
auto uv_scale = Vector2f{tex.uv_scale[0], tex.uv_scale[1]};
auto uv = uv_ * uv_scale;
auto du_dxy = du_dxy_ * uv_scale[0];
auto dv_dxy = dv_dxy_ * uv_scale[1];
auto x = uv[0] * tex.width - 0.5f;
auto y = uv[1] * tex.height - 0.5f;
auto xf = (int)floor(x);
auto yf = (int)floor(y);
auto xc = xf + 1;
auto yc = yf + 1;
auto u = x - xf;
auto v = y - yf;
auto xfi = modulo(xf, tex.width);
auto yfi = modulo(yf, tex.height);
auto xci = modulo(xc, tex.width);
auto yci = modulo(yc, tex.height);
auto u_footprint = length(du_dxy) * tex.width;
auto v_footprint = length(dv_dxy) * tex.height;
bool is_u_max = true;
auto max_footprint = u_footprint;
if (v_footprint > u_footprint) {
is_u_max = false;
max_footprint = v_footprint;
}
auto level = log2(max(max_footprint, Real(1e-8f)));
d_tex.xi = xfi;
d_tex.yi = yfi;
auto d_u = Real(0);
auto d_v = Real(0);
auto d_max_footprint = Real(0);
if (level <= 0) {
d_tex.li = -1;
d_bilinear_interp(tex, xfi, yfi, xci, yci, u, v, 0, d_output,
d_tex.t000, d_tex.t100, d_tex.t010, d_tex.t110, d_u, d_v);
} else if (level >= tex.num_levels - 1) {
d_tex.li = tex.num_levels - 1;
d_bilinear_interp(tex, xfi, yfi, xci, yci, u, v, tex.num_levels - 1, d_output,
d_tex.t000, d_tex.t100, d_tex.t010, d_tex.t110, d_u, d_v);
} else {
auto li = (int)floor(level);
d_tex.li = li;
auto ld = level - li;
// (1 - ld) * bilinear_interp(tex, xfi, yfi, xci, yci, u, v, li) +
// ld * bilinear_interp(tex, xfi, yfi, xci, yci, u, v, li + 1);
auto l0 = bilinear_interp(tex, xfi, yfi, xci, yci, u, v, li);
auto l1 = bilinear_interp(tex, xfi, yfi, xci, yci, u, v, li + 1);
auto d_l0 = (1 - ld) * d_output;
auto d_l1 = ld * d_output;
d_bilinear_interp(tex, xfi, yfi, xci, yci, u, v, li, d_l0,
d_tex.t000, d_tex.t100, d_tex.t010, d_tex.t110, d_u, d_v);
d_bilinear_interp(tex, xfi, yfi, xci, yci, u, v, li + 1, d_l1,
d_tex.t001, d_tex.t101, d_tex.t011, d_tex.t111, d_u, d_v);
auto d_ld = sum(d_output * (l1 - l0));
// level = log2(max(max_footprint, Real(1e-8f)))
if (max_footprint > Real(1e-8f)) {
d_max_footprint += d_ld / (max_footprint * log(Real(2)));
}
}
// max_footprint = max(length(du_dxy) * tex.width, length(dv_dxy) * tex.height)
auto d_uv = Vector2{0, 0};
auto d_du_dxy = Vector2{0, 0};
auto d_dv_dxy = Vector2{0, 0};
if (max_footprint > Real(1e-8f)) {
if (is_u_max) {
d_du_dxy += d_length(du_dxy, d_max_footprint) * tex.width;
} else {
d_dv_dxy += d_length(dv_dxy, d_max_footprint) * tex.height;
}
}
// du = dx, dv = dy
// x = uv[0] * tex.width - 0.5f
// y = uv[1] * tex.height - 0.5f
d_uv[0] += d_u * tex.width;
d_uv[1] += d_v * tex.height;
// uv = uv_ * uv_scale
// du_dxy = du_dxy_ * uv_scale[0]
// dv_dxy = dv_dxy_ * uv_scale[1]
d_uv_ += d_uv * uv_scale;
d_du_dxy_ += d_du_dxy * uv_scale[0];
d_dv_dxy_ += d_dv_dxy * uv_scale[1];
}
}
template <typename TextureType, typename OutputType>
DEVICE
inline void d_get_texture_value(const TextureType &tex,
const Vector2 &uv_,
const Vector2 &du_dxy_,
const Vector2 &dv_dxy_,
const OutputType &d_output,
TextureType &d_tex,
Vector2 &d_uv_,
Vector2 &d_du_dxy_,
Vector2 &d_dv_dxy_) {
if (tex.width <= 0 && tex.height <= 0) {
// Constant texture
// output = Vector3{tex.texels[0], tex.texels[1], tex.texels[2]};
atomic_add(d_tex.texels, d_output);
} else {
// Trilinear interpolation
auto uv_scale = Vector2f{tex.uv_scale[0], tex.uv_scale[1]};
auto uv = uv_ * uv_scale;
auto du_dxy = du_dxy_ * uv_scale[0];
auto dv_dxy = dv_dxy_ * uv_scale[1];
auto x = uv[0] * tex.width - 0.5f;
auto y = uv[1] * tex.height - 0.5f;
auto xf = (int)floor(x);
auto yf = (int)floor(y);
auto xc = xf + 1;
auto yc = yf + 1;
auto u = x - xf;
auto v = y - yf;
auto xfi = modulo(xf, tex.width);
auto yfi = modulo(yf, tex.height);
auto xci = modulo(xc, tex.width);
auto yci = modulo(yc, tex.height);
auto u_footprint = length(du_dxy) * tex.width;
auto v_footprint = length(dv_dxy) * tex.height;
bool is_u_max = true;
auto max_footprint = u_footprint;
if (v_footprint > u_footprint) {
is_u_max = false;
max_footprint = v_footprint;
}
auto level = log2(max(max_footprint, Real(1e-8f)));
auto d_u = Real(0);
auto d_v = Real(0);
auto d_max_footprint = Real(0);
if (level <= 0) {
d_bilinear_interp(tex, xfi, yfi, xci, yci, u, v, 0, d_output, d_tex, d_u, d_v);
} else if (level >= tex.num_levels - 1) {
d_bilinear_interp(tex, xfi, yfi, xci, yci, u, v, tex.num_levels - 1, d_output,
d_tex, d_u, d_v);
} else {
auto li = (int)floor(level);
auto ld = level - li;
// (1 - ld) * bilinear_interp(tex, xfi, yfi, xci, yci, u, v, li) +
// ld * bilinear_interp(tex, xfi, yfi, xci, yci, u, v, li + 1);
auto l0 = bilinear_interp(tex, xfi, yfi, xci, yci, u, v, li);
auto l1 = bilinear_interp(tex, xfi, yfi, xci, yci, u, v, li + 1);
auto d_l0 = (1 - ld) * d_output;
auto d_l1 = ld * d_output;
d_bilinear_interp(tex, xfi, yfi, xci, yci, u, v, li, d_l0,
d_tex, d_u, d_v);
d_bilinear_interp(tex, xfi, yfi, xci, yci, u, v, li + 1, d_l1,
d_tex, d_u, d_v);
auto d_ld = sum(d_output * (l1 - l0));
// level = log2(max(max_footprint, Real(1e-8f)))
if (max_footprint > Real(1e-8f)) {
d_max_footprint += d_ld / (max_footprint * log(Real(2)));
}
}
// max_footprint = max(length(du_dxy) * tex.width, length(dv_dxy) * tex.height)
auto d_uv = Vector2{0, 0};
auto d_du_dxy = Vector2{0, 0};
auto d_dv_dxy = Vector2{0, 0};
if (max_footprint > Real(1e-8f)) {
if (is_u_max) {
d_du_dxy += d_length(du_dxy, d_max_footprint) * tex.width;
} else {
d_dv_dxy += d_length(dv_dxy, d_max_footprint) * tex.height;
}
}
// du = dx, dv = dy
// x = uv[0] * tex.width - 0.5f
// y = uv[1] * tex.height - 0.5f
d_uv[0] += d_u * tex.width;
d_uv[1] += d_v * tex.height;
// uv = uv_ * uv_scale
// du_dxy = du_dxy_ * uv_scale[0]
// dv_dxy = dv_dxy_ * uv_scale[1]
d_uv_ += d_uv * uv_scale;
d_du_dxy_ += d_du_dxy * uv_scale[0];
d_dv_dxy_ += d_dv_dxy * uv_scale[1];
atomic_add(d_tex.uv_scale,
d_uv * uv_ + Vector2{sum(d_du_dxy * du_dxy_), sum(d_dv_dxy * dv_dxy_)});
}
}