/
texture.h
455 lines (420 loc) · 18.2 KB
/
texture.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
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
#pragma once
#include "redner.h"
#include "vector.h"
#include "ptr.h"
#include "atomic.h"
#include "assert.h"
template <int N>
struct Texture {
Texture() {}
Texture(ptr<float> texels,
int width,
int height,
int channels, // ignored if N=-1
int num_levels,
int mesh_colors_resolution,
ptr<float> uv_scale)
: texels(texels.get()),
width(width), height(height), channels(channels),
num_levels(num_levels),
mesh_colors_resolution(mesh_colors_resolution),
uv_scale(uv_scale.get()) {}
float *texels;
int width;
int height;
int channels;
int num_levels;
int mesh_colors_resolution;
float *uv_scale;
};
using TextureN = Texture<-1>;
using Texture3 = Texture<3>;
using Texture1 = Texture<1>;
template <int N>
DEVICE
inline void trilinear_interp(const Texture<N> &tex,
int xfi, int yfi,
int xci, int yci,
Real u, Real v,
Real level,
Real *output) {
// If channels == N, hopefully the constant would propagate and simplify the code.
auto channels = N == -1 ? tex.channels : N;
if (level <= 0 || level >= tex.num_levels - 1) {
auto li = level <= 0 ? 0 : tex.num_levels - 1;
auto texels = tex.texels + li * tex.width * tex.height * channels;
for (int i = 0; i < channels; i++) {
auto value_ff = texels[channels * (yfi * tex.width + xfi) + i];
auto value_cf = texels[channels * (yfi * tex.width + xci) + i];
auto value_fc = texels[channels * (yci * tex.width + xfi) + i];
auto value_cc = texels[channels * (yci * tex.width + xci) + i];
output[i] = value_ff * (1.f - u) * (1.f - v) +
value_fc * (1.f - u) * v +
value_cf * u * (1.f - v) +
value_cc * u * v;
}
} else {
auto li = (int)floor(level);
auto ld = level - li;
auto texels0 = tex.texels + li * tex.width * tex.height * channels;
auto texels1 = tex.texels + (li + 1) * tex.width * tex.height * channels;
for (int i = 0; i < channels; i++) {
auto value_ff0 = texels0[channels * (yfi * tex.width + xfi) + i];
auto value_cf0 = texels0[channels * (yfi * tex.width + xci) + i];
auto value_fc0 = texels0[channels * (yci * tex.width + xfi) + i];
auto value_cc0 = texels0[channels * (yci * tex.width + xci) + i];
auto value_ff1 = texels1[channels * (yfi * tex.width + xfi) + i];
auto value_cf1 = texels1[channels * (yfi * tex.width + xci) + i];
auto value_fc1 = texels1[channels * (yci * tex.width + xfi) + i];
auto value_cc1 = texels1[channels * (yci * tex.width + xci) + i];
auto v0 = value_ff0 * (1.f - u) * (1.f - v) +
value_fc0 * (1.f - u) * v +
value_cf0 * u * (1.f - v) +
value_cc0 * u * v;
auto v1 = value_ff1 * (1.f - u) * (1.f - v) +
value_fc1 * (1.f - u) * v +
value_cf1 * u * (1.f - v) +
value_cc1 * u * v;
output[i] = v0 * (1 - ld) + v1 * ld;
}
}
}
template <int N>
DEVICE
inline void d_trilinear_interp(const Texture<N> &tex,
int xfi, int yfi,
int xci, int yci,
Real u, Real v,
Real level,
const Real *d_output,
Texture<N> &d_tex,
Real &d_u, Real &d_v,
Real &d_level) {
// If channels == N, hopefully the constant would propagate and simplify the code.
auto channels = N == -1 ? tex.channels : N;
if (level <= 0 || level >= tex.num_levels - 1) {
auto li = level <= 0 ? 0 : tex.num_levels - 1;
auto texels = tex.texels + li * tex.width * tex.height * channels;
auto d_texels = d_tex.texels + li * tex.width * tex.height * channels;
for (int i = 0; i < channels; i++) {
auto value_ff = texels[channels * (yfi * tex.width + xfi) + i];
auto value_cf = texels[channels * (yfi * tex.width + xci) + i];
auto value_fc = texels[channels * (yci * tex.width + xfi) + i];
auto value_cc = texels[channels * (yci * tex.width + xci) + i];
// output[i] = 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
atomic_add(&d_texels[channels * (yfi * tex.width + xfi) + i],
d_output[i] * (1.f - u) * (1.f - v));
// d_value_fc
atomic_add(&d_texels[channels * (yfi * tex.width + xci) + i],
d_output[i] * u * (1.f - v));
// d_value_cf
atomic_add(&d_texels[channels * (yci * tex.width + xfi) + i],
d_output[i] * (1.f - u) * v );
// d_value_cc
atomic_add(&d_texels[channels * (yci * tex.width + xci) + i],
d_output[i] * u * v );
d_u += sum(d_output[i] * (-value_ff * (1.f - v) +
value_cf * (1.f - v) +
-value_fc * v +
value_cc * v));
d_v += sum(d_output[i] * (-value_ff * (1.f - u) +
-value_cf * u +
value_fc * (1.f - u) +
value_cc * u));
}
} else {
auto li = (int)floor(level);
auto ld = level - li;
auto texels0 = tex.texels + li * tex.width * tex.height * channels;
auto texels1 = tex.texels + (li + 1) * tex.width * tex.height * channels;
auto d_texels0 = d_tex.texels + li * tex.width * tex.height * channels;
auto d_texels1 = d_tex.texels + (li + 1) * tex.width * tex.height * channels;
for (int i = 0; i < channels; i++) {
auto value_ff0 = texels0[channels * (yfi * tex.width + xfi) + i];
auto value_cf0 = texels0[channels * (yfi * tex.width + xci) + i];
auto value_fc0 = texels0[channels * (yci * tex.width + xfi) + i];
auto value_cc0 = texels0[channels * (yci * tex.width + xci) + i];
auto value_ff1 = texels1[channels * (yfi * tex.width + xfi) + i];
auto value_cf1 = texels1[channels * (yfi * tex.width + xci) + i];
auto value_fc1 = texels1[channels * (yci * tex.width + xfi) + i];
auto value_cc1 = texels1[channels * (yci * tex.width + xci) + i];
auto v0 = value_ff0 * (1.f - u) * (1.f - v) +
value_fc0 * (1.f - u) * v +
value_cf0 * u * (1.f - v) +
value_cc0 * u * v;
auto v1 = value_ff1 * (1.f - u) * (1.f - v) +
value_fc1 * (1.f - u) * v +
value_cf1 * u * (1.f - v) +
value_cc1 * u * v;
// output[i] = v0 * (1 - ld) + v1 * ld;
auto d_v0 = d_output[i] * (1 - ld);
auto d_v1 = d_output[i] * ld;
d_level += d_output[i] * (v1 - v0);
// d_value_ff0
atomic_add(&d_texels0[channels * (yfi * tex.width + xfi) + i],
d_v0 * (1.f - u) * (1.f - v));
// d_value_fc0
atomic_add(&d_texels0[channels * (yfi * tex.width + xci) + i],
d_v0 * u * (1.f - v));
// d_value_cf0
atomic_add(&d_texels0[channels * (yci * tex.width + xfi) + i],
d_v0 * (1.f - u) * v );
// d_value_cc0
atomic_add(&d_texels0[channels * (yci * tex.width + xci) + i],
d_v0 * u * v );
// d_value_ff1
atomic_add(&d_texels1[channels * (yfi * tex.width + xfi) + i],
d_v1 * (1.f - u) * (1.f - v));
// d_value_fc1
atomic_add(&d_texels1[channels * (yfi * tex.width + xci) + i],
d_v1 * u * (1.f - v));
// d_value_cf1
atomic_add(&d_texels1[channels * (yci * tex.width + xfi) + i],
d_v1 * (1.f - u) * v );
// d_value_cc1
atomic_add(&d_texels1[channels * (yci * tex.width + xci) + i],
d_v1 * u * v );
d_u += d_v0 * (-value_ff0 * (1.f - v) +
value_cf0 * (1.f - v) +
-value_fc0 * v +
value_cc0 * v);
d_u += d_v1 * (-value_ff1 * (1.f - v) +
value_cf1 * (1.f - v) +
-value_fc1 * v +
value_cc1 * v);
d_v += d_v0 * (-value_ff0 * (1.f - u) +
-value_cf0 * u +
value_fc0 * (1.f - u) +
value_cc0 * u);
d_v += d_v1 * (-value_ff1 * (1.f - u) +
-value_cf1 * u +
value_fc1 * (1.f - u) +
value_cc1 * u);
}
}
}
const Real epsilon = 0.0001;
DEVICE
inline Real round_local(Real val) {
return floor(0.5f + val);
}
DEVICE
inline int get_mesh_colors_index(const int tri_id, const int res, int i, int j) {
return tri_id * round_local((res + 1) * (res + 2) / 2) + round_local(i * (2 * res - i + 3) / 2) + j;
}
template <int N>
DEVICE
inline void mesh_colors_interp(const Texture<N> &tex,
const int tri_id,
const Vector2 &uv,
Real *output) {
auto channels = N == -1 ? tex.channels : N;
const int r = tex.mesh_colors_resolution;
const int i = floor(r * uv.x);
const int j = floor(r * uv.y);
const Real w = 1.0 - uv.x - uv.y;
const Real w_x = r * uv.x - i;
const Real w_y = r * uv.y - j;
const Real w_z = r * w - floor(r * w);
auto mc_ind0 = get_mesh_colors_index(tri_id, r, i + 1, j);
auto mc_ind1 = get_mesh_colors_index(tri_id, r, i, j + 1);
auto mc_ind2 = get_mesh_colors_index(tri_id, r, i, j);
if (w_x + w_y + w_z < epsilon) {
for (int i = 0; i < channels; i++) {
output[i] = tex.texels[channels * mc_ind2 + i];
assert(isfinite(output[i]));
}
return;
}
if (w_x + w_y + w_z < 2.0 - epsilon) {
for (int i = 0; i < channels; i++) {
auto c_i1j = tex.texels[channels * mc_ind0 + i];
auto c_ij1 = tex.texels[channels * mc_ind1 + i];
auto c_ij = tex.texels[channels * mc_ind2 + i];
output[i] = w_x * c_i1j + w_y * c_ij1 + w_z * c_ij;
assert(isfinite(output[i]));
}
}
else {
mc_ind2 = get_mesh_colors_index(tri_id, r, i + 1, j + 1);
for (int i = 0; i < channels; i++) {
auto c_i1j = tex.texels[channels * mc_ind0 + i];
auto c_ij1 = tex.texels[channels * mc_ind1 + i];
auto c_i1j1 = tex.texels[channels * mc_ind2 + i];
output[i] = (1.0 - w_x) * c_ij1 + (1.0 - w_y) * c_i1j + (1.0 - w_z) * c_i1j1;
assert(isfinite(output[i]));
}
}
}
template <int N>
DEVICE
inline void d_mesh_colors_interp(const Texture<N> &tex,
const int tri_id,
const Vector2 &uv,
const Real *d_output,
Texture<N> &d_tex) {
auto channels = N == -1 ? tex.channels : N;
const int r = tex.mesh_colors_resolution;
const int i = floor(r * uv.x);
const int j = floor(r * uv.y);
const Real w = 1.0 - uv.x - uv.y;
const Real w_x = r * uv.x - i;
const Real w_y = r * uv.y - j;
const Real w_z = r * w - floor(r * w);
if (w_x + w_y + w_z < epsilon) {
// No derivatives here pardner
return;
}
auto mc_ind0 = get_mesh_colors_index(tri_id, r, i + 1, j);
auto mc_ind1 = get_mesh_colors_index(tri_id, r, i, j + 1);
auto mc_ind2 = (w_x + w_y + w_z < 2 - epsilon) ?
get_mesh_colors_index(tri_id, r, i, j) :
get_mesh_colors_index(tri_id, r, i + 1, j + 1);
for (int i = 0; i < channels; i++) {
assert(isfinite(d_output[i] * w_x));
assert(isfinite(d_output[i] * w_y));
assert(isfinite(d_output[i] * w_z));
atomic_add(&d_tex.texels[channels * mc_ind0 + i], d_output[i] * w_x);
atomic_add(&d_tex.texels[channels * mc_ind1 + i], d_output[i] * w_y);
atomic_add(&d_tex.texels[channels * mc_ind2 + i], d_output[i] * w_z);
}
}
template <int N>
DEVICE
inline void get_texture_value_constant(const Texture<N> &tex,
Real *output) {
auto channels = N == -1 ? tex.channels : N;
for (int i = 0; i < channels; i++) {
output[i] = tex.texels[i];
}
}
template <typename TextureType>
DEVICE
inline void get_texture_value(const TextureType &tex,
const int tri_id_,
const Vector2 &uv_,
const Vector2 &tri_uv_,
const Vector2 &du_dxy_,
const Vector2 &dv_dxy_,
Real *output) {
if (tex.num_levels <= 0) {
// Constant texture
get_texture_value_constant(tex, output);
}
else if (tex.mesh_colors_resolution > 0) {
mesh_colors_interp(tex, tri_id_, tri_uv_, 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 max_footprint = max(length(du_dxy) * tex.width, length(dv_dxy) * tex.height);
auto level = log2(max(max_footprint, Real(1e-8f)));
trilinear_interp(tex, xfi, yfi, xci, yci, u, v, level, output);
}
}
template <int N>
DEVICE
inline void d_get_texture_value(const Texture<N> &tex,
const int tri_id_,
const Vector2 &uv_,
const Vector2 &tri_uv_,
const Vector2 &du_dxy_,
const Vector2 &dv_dxy_,
const Real *d_output,
Texture<N> &d_tex,
Vector2 &d_uv_,
Vector2 &d_du_dxy_,
Vector2 &d_dv_dxy_) {
if (tex.num_levels <= 0) {
// Constant texture
// output[i] = tex.texels[i]
auto channels = N == -1 ? tex.channels : N;
for (int i = 0; i < channels; i++) {
atomic_add(d_tex.texels[i], d_output[i]);
}
}
else if (tex.mesh_colors_resolution > 0) {
d_mesh_colors_interp(tex, tri_id_, tri_uv_, d_output, d_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 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_level = Real(0);
d_trilinear_interp(tex, xfi, yfi, xci, yci, u, v, level,
d_output, d_tex, d_u, d_v, d_level);
auto d_max_footprint = Real(0);
// level = log2(max(max_footprint, Real(1e-8f)))
if (max_footprint > Real(1e-8f)) {
d_max_footprint += d_level / (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_)});
}
}