Blender V5.3
multires_bake.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2012-2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
67
68#include "RE_multires_bake.h"
69
70#include <atomic>
71
72#include "DNA_mesh_types.h"
73#include "DNA_modifier_types.h"
74
75#include "BLI_array.hh"
76#include "BLI_listbase.hh"
77#include "BLI_math_base.hh"
78#include "BLI_math_geom_c.hh"
79#include "BLI_math_matrix.hh"
80#include "BLI_math_vector.hh"
81#include "BLI_span.hh"
82#include "BLI_task.hh"
83#include "BLI_threads.hh"
84
85#include "BKE_attribute.hh"
86#include "BKE_customdata.hh"
87#include "BKE_global.hh"
88#include "BKE_image.hh"
89#include "BKE_lib_id.hh"
90#include "BKE_mesh.hh"
91#include "BKE_mesh_tangent.hh"
92#include "BKE_multires.hh"
93#include "BKE_subdiv.hh"
94#include "BKE_subdiv_ccg.hh"
95#include "BKE_subdiv_eval.hh"
96#include "BKE_subdiv_foreach.hh"
97#include "BKE_subdiv_mesh.hh"
98
99#include "IMB_imbuf.hh"
100#include "IMB_imbuf_types.hh"
101#include "IMB_partial_update.hh"
102
103#include "DEG_depsgraph.hh"
104
105#include "RE_texture_margin.h"
106
107namespace blender {
108
109namespace render {
110namespace {
111
112namespace subdiv = bke::subdiv;
113
114/* -------------------------------------------------------------------- */
118
119template<class T> T interp_barycentric_triangle(const T data[3], const float2 &uv)
120{
121 return data[0] * uv.x + data[1] * uv.y + data[2] * (1.0f - uv.x - uv.y);
122}
123
124template<class T>
126 const float u, const float v, const T &p0, const T &p1, const T &p2, const T &p3)
127{
128 const float w0 = (1 - u) * (1 - v);
129 const float w1 = u * (1 - v);
130 const float w2 = u * v;
131 const float w3 = (1 - u) * v;
132
133 return p0 * w0 + p1 * w1 + p2 * w2 + p3 * w3;
134}
135
136float2 resolve_tri_uv(const float2 &st, const float2 &st0, const float2 &st1, const float2 &st2)
137{
138 float2 uv;
139 resolve_tri_uv_v2(uv, st, st0, st1, st2);
140 return uv;
141}
142
144
145/* -------------------------------------------------------------------- */
148
149template<class T> class Grid {
150 Span<T> data_;
151 int side_size_;
152
153 T get_element(const int x, const int y) const
154 {
155 const int64_t index = int64_t(y) * side_size_ + x;
156 return data_[index];
157 }
158
159 public:
160 Grid(const Span<T> data, const int side_size) : data_(data), side_size_(side_size)
161 {
162 BLI_assert(data.size() == side_size_ * side_size_);
163 }
164
165 T sample(const float2 uv) const
166 {
167 const float2 xy = uv * (side_size_ - 1);
168
169 const int x0 = int(xy.x);
170 const int x1 = x0 >= (side_size_ - 1) ? (side_size_ - 1) : (x0 + 1);
171
172 const int y0 = int(xy.y);
173 const int y1 = y0 >= (side_size_ - 1) ? (side_size_ - 1) : (y0 + 1);
174
175 const float u = xy.x - x0;
176 const float v = xy.y - y0;
177
179 u, v, get_element(x0, y0), get_element(x1, y0), get_element(x1, y1), get_element(x0, y1));
180 }
181};
182
183template<class T>
184Grid<T> get_subdiv_ccg_grid(const SubdivCCG &subdiv_ccg, const int grid_index, const Span<T> data)
185{
186 const int64_t offset = int64_t(grid_index) * subdiv_ccg.grid_area;
187 return Grid(Span(data.data() + offset, subdiv_ccg.grid_area), subdiv_ccg.grid_size);
188}
189
190float3 sample_position_on_subdiv_ccg(const SubdivCCG &subdiv_ccg,
191 const int grid_index,
192 const float2 grid_uv)
193{
194 const Grid<float3> grid = get_subdiv_ccg_grid(
195 subdiv_ccg, grid_index, subdiv_ccg.positions.as_span());
196 return grid.sample(grid_uv);
197}
198
199float3 sample_normal_on_subdiv_ccg(const SubdivCCG &subdiv_ccg,
200 const int grid_index,
201 const float2 grid_uv)
202{
203 /* TODO(sergey): Support flat normals.
204 * It seems that the baker always used smooth interpolation for CCG. */
205 const Grid<float3> grid = get_subdiv_ccg_grid(
206 subdiv_ccg, grid_index, subdiv_ccg.normals.as_span());
207 return grid.sample(grid_uv);
208}
209
211
212/* -------------------------------------------------------------------- */
215
216struct MeshArrays {
217 Span<float3> vert_positions;
218 Span<float3> vert_normals;
219
220 Span<int> corner_verts;
221 Span<int3> corner_tris;
222 Span<float3> corner_normals;
223
224 Span<int> tri_faces;
225
226 OffsetIndices<int> faces;
227 Span<float3> face_normals;
228 VArraySpan<bool> sharp_faces;
229
230 VArraySpan<float2> uv_map;
231
232 VArraySpan<int> material_indices;
233
234 MeshArrays() = default;
235
236 explicit MeshArrays(const Mesh &mesh)
237 {
238 bke::AttributeAccessor attributes = mesh.attributes();
239
240 const StringRef active_uv_map = mesh.active_uv_map_name();
241 vert_positions = mesh.vert_positions();
242 vert_normals = mesh.vert_normals();
243
244 corner_verts = mesh.corner_verts();
245 corner_tris = mesh.corner_tris();
246 corner_normals = mesh.corner_normals();
247
248 tri_faces = mesh.corner_tri_faces();
249
250 faces = mesh.faces();
251 face_normals = mesh.face_normals();
252 sharp_faces = *attributes.lookup_or_default<bool>("sharp_face", bke::AttrDomain::Face, false);
253
254 uv_map = *attributes.lookup<float2>(active_uv_map, bke::AttrDomain::Corner);
255
256 material_indices = *attributes.lookup_or_default<int>(
257 "material_index", bke::AttrDomain::Face, 0);
258 }
259};
260
261/* Calculate UV map coordinates at the center of the face (grid coordinates (0, 0)). */
262static float2 face_center_tex_uv_calc(const MeshArrays &mesh_arrays, const int face_index)
263{
264 const IndexRange &face = mesh_arrays.faces[face_index];
265 float2 tex_uv_acc(0.0f, 0.0f);
266 for (const int corner : face) {
267 tex_uv_acc += mesh_arrays.uv_map[corner];
268 }
269 return tex_uv_acc / face.size();
270}
271
272/* Calculate smooth normal coordinates at the center of the face (grid coordinates (0, 0)).
273 * NOTE: The returned value is not normalized to allow linear interpolation with other grid
274 * elements. */
275static float3 face_center_smooth_normal_calc(const MeshArrays &mesh_arrays, const int face_index)
276{
277 const IndexRange &face = mesh_arrays.faces[face_index];
278 float3 normal_acc(0.0f, 0.0f, 0.0f);
279 for (const int corner : face) {
280 normal_acc += mesh_arrays.vert_normals[mesh_arrays.corner_verts[corner]];
281 }
282 /* NOTE: No normalization here: do it after interpolation at the baking point.
283 *
284 * This preserves linearity of operation. If normalization is done here interpolation will go
285 * wrong. */
286 return normal_acc / face.size();
287}
288
289/* Calculate tangent space for the given mesh state. */
290Array<float4> calc_uv_tangents(const MeshArrays &mesh_arrays)
291{
292 Array<Array<float4>> tangent_data = bke::mesh::calc_uv_tangents(mesh_arrays.vert_positions,
293 mesh_arrays.faces,
294 mesh_arrays.corner_verts,
295 mesh_arrays.corner_tris,
296 mesh_arrays.tri_faces,
297 mesh_arrays.sharp_faces,
298 mesh_arrays.vert_normals,
299 mesh_arrays.face_normals,
300 mesh_arrays.corner_normals,
301 {mesh_arrays.uv_map});
302
303 return tangent_data[0];
304}
305
306/* Calculate tangent space at the center of the face (grid coordinates (0, 0)). */
307static float4 face_center_uv_tangent_calc(const MeshArrays &mesh_arrays,
308 const Span<float4> uv_tangents,
309 const int face_index)
310{
311 const IndexRange &face = mesh_arrays.faces[face_index];
312 float4 tex_uv_acc(0.0f, 0.0f, 0.0f, 0.0f);
313 for (const int corner : face) {
314 tex_uv_acc += uv_tangents[corner];
315 }
316 return tex_uv_acc / face.size();
317}
318
320
321/* -------------------------------------------------------------------- */
324
325struct ExtraBuffers {
326 Array<float> displacement_buffer;
327 Array<char> mask_buffer;
328};
329
330struct RasterizeTile {
331 ImBuf *ibuf = nullptr; /* Image buffer of the tile. */
332 ExtraBuffers *extra_buffers = nullptr;
333 float2 uv_offset; /* UV coordinate of the tile origin. */
334};
335
336struct RasterizeTriangle {
337 /* UV coordinates with the CCG. All vertices belong to the same grid. */
338 int grid_index;
339 float2 grid_uvs[3];
340
341 /* UV texture coordinates of the triangle vertices within the tile. */
342 float2 tex_uvs[3];
343
344 /* Positions and normals of the vertices, at the bake level. */
345 float3 positions[3];
346 float3 normals[3];
347
348 /* Triangle is shaded flat: it has the same normal at every point of its surface.
349 * Face normal is stored in all elements of the normals array. */
350 bool is_flat;
351
352 /* Optional tangents.
353 * The uv_tangents might be uninitialized if has_uv_tangents=false. */
354 bool has_uv_tangents;
355 float4 uv_tangents[3];
356
357 float3 get_position(const float2 &uv) const
358 {
359 return interp_barycentric_triangle(positions, uv);
360 }
361
362 float3 get_normal(const float2 &uv) const
363 {
364 if (is_flat) {
365 return normals[0];
366 }
367 return math::normalize(interp_barycentric_triangle(normals, uv));
368 }
369};
370
371struct RasterizeQuad {
372 /* UV coordinates with the CCG. All vertices belong to the same grid. */
373 int grid_index;
374 float2 grid_uvs[4];
375
376 /* UV texture coordinates of the triangle vertices within the tile. */
377 float2 tex_uvs[4];
378
379 /* Positions and normals of the vertices, at the bake level. */
380 float3 positions[4];
381 float3 normals[4];
382
383 /* Quad is shaded flat: it has the same normal at every point of its surface.
384 * Face normal is stored in all elements of the normals array. */
385 bool is_flat;
386
387 /* Optional tangents.
388 * The uv_tangents might be uninitialized if has_uv_tangents=false. */
389 bool has_uv_tangents;
390 float4 uv_tangents[4];
391};
392
393struct RasterizeResult {
394 float height_min = FLT_MAX;
395 float height_max = -FLT_MAX;
396};
397
398struct BakedImBuf {
399 Image *image;
400 ImBuf *ibuf;
401 ExtraBuffers extra_buffers;
402 float2 uv_offset;
403};
404
405struct MultiresBakeResult {
406 Vector<BakedImBuf> baked_ibufs;
407
408 /* Bake-level mesh subdivided to the final multi-resolution level.
409 * It is created by displacement baker that used "Use Low Resolution Mesh" OFF.
410 *
411 * This mesh is to be used to filter baked images. */
412 const Mesh *highres_bake_mesh = nullptr;
413
414 /* Minimum and maximum height during displacement baking. */
415 float height_min = FLT_MAX;
416 float height_max = -FLT_MAX;
417};
418
419class MultiresBaker {
420 public:
421 virtual ~MultiresBaker() = default;
422
423 virtual float3 bake_pixel(const RasterizeTriangle &triangle,
424 const float2 &bary_uv,
425 const float2 &grid_uv,
426 RasterizeResult &result) const = 0;
427
428 virtual void write_pixel(const RasterizeTile &tile,
429 const int2 &coord,
430 const float3 &value) const = 0;
431
432 protected:
433 void write_pixel_to_image_buffer(ImBuf &ibuf, const int2 &coord, const float3 &value) const
434 {
435 const int64_t pixel = int64_t(ibuf.x) * coord.y + coord.x;
436
437 if (ibuf.float_data()) {
438 /* TODO(sergey): Properly tackle ibuf.channels. */
439 BLI_assert(ibuf.channels == 4);
440 float *rrgbf = ibuf.float_data_for_write() + pixel * 4;
441 rrgbf[0] = value[0];
442 rrgbf[1] = value[1];
443 rrgbf[2] = value[2];
444 rrgbf[3] = 1.0f;
445 }
446
447 if (ibuf.byte_data()) {
448 uchar *rrgb = ibuf.byte_data_for_write() + pixel * 4;
449 unit_float_to_uchar_clamp_v3(rrgb, value);
450 rrgb[3] = 255;
451 }
452 }
453};
454
455static bool multiresbake_test_break(const MultiresBakeRender &bake)
456{
457 if (!bake.stop) {
458 /* This means baker is executed outside from job system (for example, from Python API).
459 * In this case there is no need to cancel, as it will be quite strange to cancel out
460 * execution of a script. */
461 return false;
462 }
463 return *bake.stop || G.is_break;
464}
465
466static float2 get_tile_uv(Image &image, ImageTile &tile)
467{
468 float uv_offset[2];
469 BKE_image_get_tile_uv(&image, tile.tile_number, uv_offset);
470 return uv_offset;
471}
472
473static bool need_tangent(const MultiresBakeRender &bake)
474{
475 return (bake.type == R_BAKE_NORMALS) || (bake.type == R_BAKE_VECTOR_DISPLACEMENT &&
476 bake.displacement_space == R_BAKE_SPACE_TANGENT);
477}
478
479/* Get matrix which converts tangent space to object space in the (tangent, bitangent, normal)
480 * convention. */
481static float3x3 get_from_tangent_matrix_tbn(const RasterizeTriangle &triangle,
482 const float2 &bary_uv)
483{
484 if (!triangle.has_uv_tangents) {
485 return float3x3::identity();
486 }
487
488 const float u = bary_uv.x;
489 const float v = bary_uv.y;
490 const float w = 1 - u - v;
491
492 const float3 &no0 = triangle.normals[0];
493 const float3 &no1 = triangle.normals[1];
494 const float3 &no2 = triangle.normals[2];
495
496 const float4 &tang0 = triangle.uv_tangents[0];
497 const float4 &tang1 = triangle.uv_tangents[1];
498 const float4 &tang2 = triangle.uv_tangents[2];
499
500 /* The sign is the same at all face vertices for any non-degenerate face.
501 * Just in case we clamp the interpolated value though. */
502 const float sign = (tang0.w * u + tang1.w * v + tang2.w * w) < 0 ? (-1.0f) : 1.0f;
503
504 /* x - tangent
505 * y - bitangent (B = sign * cross(N, T))
506 * z - normal */
507 float3x3 from_tangent;
508 from_tangent.x = tang0.xyz() * u + tang1.xyz() * v + tang2.xyz() * w;
509 from_tangent.z = no0.xyz() * u + no1.xyz() * v + no2.xyz() * w;
510 from_tangent.y = sign * math::cross(from_tangent.z, from_tangent.x);
511
512 return from_tangent;
513}
514
515/* Get matrix which converts object space to tangent space in the (tangent, bitangent, normal)
516 * convention. */
517static float3x3 get_to_tangent_matrix_tbn(const RasterizeTriangle &triangle, const float2 &bary_uv)
518{
519 const float3x3 from_tangent = get_from_tangent_matrix_tbn(triangle, bary_uv);
520 return math::invert(from_tangent);
521}
522
523/* Get matrix which converts object space to tangent space in the (tangent, normal, bitangent)
524 * convention. */
525static float3x3 get_to_tangent_matrix_tnb(const RasterizeTriangle &triangle, const float2 &bary_uv)
526{
527 float3x3 from_tangent = get_from_tangent_matrix_tbn(triangle, bary_uv);
528 std::swap(from_tangent.y, from_tangent.z);
529 return math::invert(from_tangent);
530}
531
533
534/* -------------------------------------------------------------------- */
537
538static void flush_pixel(const MultiresBaker &baker,
539 const RasterizeTile &tile,
540 const RasterizeTriangle &triangle,
541 const int x,
542 const int y,
543 RasterizeResult &result)
544{
545 const float2 st{(x + 0.5f) / tile.ibuf->x, (y + 0.5f) / tile.ibuf->y};
546
547 const float2 bary_uv = resolve_tri_uv(
548 st, triangle.tex_uvs[0], triangle.tex_uvs[1], triangle.tex_uvs[2]);
549 const float2 grid_uv = interp_barycentric_triangle(triangle.grid_uvs, bary_uv);
550
551 const float3 baked_pixel = baker.bake_pixel(triangle, bary_uv, grid_uv, result);
552 baker.write_pixel(tile, int2(x, y), baked_pixel);
553}
554
555static void set_rast_triangle(const MultiresBaker &baker,
556 const RasterizeTile &tile,
557 const RasterizeTriangle &triangle,
558 const int x,
559 const int y,
560 RasterizeResult &result)
561{
562 const int w = tile.ibuf->x;
563 const int h = tile.ibuf->y;
564
565 if (x >= 0 && x < w && y >= 0 && y < h) {
566 const int64_t pixel = int64_t(y) * w + x;
567 if (tile.extra_buffers->mask_buffer[pixel] == FILTER_MASK_NULL) {
568 tile.extra_buffers->mask_buffer[pixel] = FILTER_MASK_USED;
569 flush_pixel(baker, tile, triangle, x, y, result);
570 }
571 }
572}
573
574static void rasterize_half(const MultiresBaker &baker,
575 const RasterizeTile &tile,
576 const RasterizeTriangle &triangle,
577 const float2 &s0,
578 const float2 &s1,
579 const float2 &l0,
580 const float2 &l1,
581 const int y0_in,
582 const int y1_in,
583 const bool is_mid_right,
584 RasterizeResult &result)
585{
586 const bool s_stable = fabsf(s1.y - s0.y) > FLT_EPSILON;
587 const bool l_stable = fabsf(l1.y - l0.y) > FLT_EPSILON;
588 const int w = tile.ibuf->x;
589 const int h = tile.ibuf->y;
590
591 if (y1_in <= 0 || y0_in >= h) {
592 return;
593 }
594
595 const int y0 = y0_in < 0 ? 0 : y0_in;
596 const int y1 = y1_in >= h ? h : y1_in;
597
598 for (int y = y0; y < y1; y++) {
599 /* `-b(x-x0) + a(y-y0) = 0`. */
600 float x_l = s_stable ? (s0.x + (((s1.x - s0.x) * (y - s0.y)) / (s1.y - s0.y))) : s0.x;
601 float x_r = l_stable ? (l0.x + (((l1.x - l0.x) * (y - l0.y)) / (l1.y - l0.y))) : l0.x;
602 if (is_mid_right) {
603 std::swap(x_l, x_r);
604 }
605
606 int iXl = int(ceilf(x_l));
607 int iXr = int(ceilf(x_r));
608
609 if (iXr > 0 && iXl < w) {
610 iXl = iXl < 0 ? 0 : iXl;
611 iXr = iXr >= w ? w : iXr;
612
613 for (int x = iXl; x < iXr; x++) {
614 set_rast_triangle(baker, tile, triangle, x, y, result);
615 }
616 }
617 }
618}
619
620static void rasterize_triangle(const MultiresBaker &baker,
621 const RasterizeTile &tile,
622 const RasterizeTriangle &triangle,
623 RasterizeResult &result)
624{
625 const float2 ibuf_size(tile.ibuf->x, tile.ibuf->y);
626
627 const float2 &st0_in = triangle.tex_uvs[0];
628 const float2 &st1_in = triangle.tex_uvs[1];
629 const float2 &st2_in = triangle.tex_uvs[2];
630
631 float2 p_low = st0_in * ibuf_size - 0.5f;
632 float2 p_mid = st1_in * ibuf_size - 0.5f;
633 float2 p_high = st2_in * ibuf_size - 0.5f;
634
635 /* Skip degenerates. */
636 if ((p_low.x == p_mid.x && p_low.y == p_mid.y) || (p_low.x == p_mid.x && p_low.y == p_high.y) ||
637 (p_mid.x == p_high.x && p_mid.y == p_high.y))
638 {
639 return;
640 }
641
642 /* Sort by T. */
643 if (p_low.y > p_mid.y && p_low.y > p_high.y) {
644 std::swap(p_high.x, p_low.x);
645 std::swap(p_high.y, p_low.y);
646 }
647 else if (p_mid.y > p_high.y) {
648 std::swap(p_high.x, p_mid.x);
649 std::swap(p_high.y, p_mid.y);
650 }
651
652 if (p_low.y > p_mid.y) {
653 std::swap(p_low.x, p_mid.x);
654 std::swap(p_low.y, p_mid.y);
655 }
656
657 /* Check if mid-point is to the left or to the right of the lo-hi edge. */
658 const bool is_mid_right = math::cross(p_mid - p_high, p_high - p_low) > 0;
659 const int ylo = int(ceilf(p_low.y));
660 const int yhi_beg = int(ceilf(p_mid.y));
661 const int yhi = int(ceilf(p_high.y));
662
663 rasterize_half(
664 baker, tile, triangle, p_low, p_mid, p_low, p_high, ylo, yhi_beg, is_mid_right, result);
665 rasterize_half(
666 baker, tile, triangle, p_mid, p_high, p_low, p_high, yhi_beg, yhi, is_mid_right, result);
667}
668
669static void rasterize_quad(const MultiresBaker &baker,
670 const RasterizeTile &tile,
671 const RasterizeQuad &quad,
672 RasterizeResult &result)
673{
674 RasterizeTriangle triangle;
675 triangle.grid_index = quad.grid_index;
676 triangle.is_flat = quad.is_flat;
677 triangle.has_uv_tangents = quad.has_uv_tangents;
678
679 const int3 quad_split_data[2] = {{0, 1, 2}, {2, 3, 0}};
680 for (const int3 &triangle_idx : Span(quad_split_data, 2)) {
681 triangle.grid_uvs[0] = quad.grid_uvs[triangle_idx.x];
682 triangle.grid_uvs[1] = quad.grid_uvs[triangle_idx.y];
683 triangle.grid_uvs[2] = quad.grid_uvs[triangle_idx.z];
684
685 triangle.tex_uvs[0] = quad.tex_uvs[triangle_idx.x];
686 triangle.tex_uvs[1] = quad.tex_uvs[triangle_idx.y];
687 triangle.tex_uvs[2] = quad.tex_uvs[triangle_idx.z];
688
689 triangle.positions[0] = quad.positions[triangle_idx.x];
690 triangle.positions[1] = quad.positions[triangle_idx.y];
691 triangle.positions[2] = quad.positions[triangle_idx.z];
692
693 triangle.normals[0] = quad.normals[triangle_idx.x];
694 triangle.normals[1] = quad.normals[triangle_idx.y];
695 triangle.normals[2] = quad.normals[triangle_idx.z];
696
697 if (triangle.has_uv_tangents) {
698 triangle.uv_tangents[0] = quad.uv_tangents[triangle_idx.x];
699 triangle.uv_tangents[1] = quad.uv_tangents[triangle_idx.y];
700 triangle.uv_tangents[2] = quad.uv_tangents[triangle_idx.z];
701 }
702
703 rasterize_triangle(baker, tile, triangle, result);
704 }
705}
706
708
709/* -------------------------------------------------------------------- */
712
713class MultiresDisplacementBaker : public MultiresBaker {
714 const SubdivCCG &high_subdiv_ccg_;
715
716 public:
717 MultiresDisplacementBaker(const SubdivCCG &subdiv_ccg,
718 const ImBuf &ibuf,
719 ExtraBuffers &extra_buffers)
720 : high_subdiv_ccg_(subdiv_ccg)
721 {
722 extra_buffers.displacement_buffer.reinitialize(IMB_get_pixel_count(&ibuf));
723 extra_buffers.displacement_buffer.fill(0);
724 }
725
726 float3 bake_pixel(const RasterizeTriangle &triangle,
727 const float2 &bary_uv,
728 const float2 &grid_uv,
729 RasterizeResult &result) const override
730 {
731 const float3 bake_level_position = triangle.get_position(bary_uv);
732 const float3 bake_level_normal = triangle.get_normal(bary_uv);
733 const float3 high_level_position = sample_position_on_subdiv_ccg(
734 high_subdiv_ccg_, triangle.grid_index, grid_uv);
735
736 const float length = math::dot(bake_level_normal, (high_level_position - bake_level_position));
737
738 result.height_min = math::min(result.height_min, length);
739 result.height_max = math::max(result.height_max, length);
740
741 return {length, length, length};
742 }
743
744 void write_pixel(const RasterizeTile &tile,
745 const int2 &coord,
746 const float3 &value) const override
747 {
748 const ImBuf &ibuf = *tile.ibuf;
749
750 const int64_t pixel = int64_t(ibuf.x) * coord.y + coord.x;
751 tile.extra_buffers->displacement_buffer[pixel] = value.x;
752
753 write_pixel_to_image_buffer(*tile.ibuf, coord, value);
754 }
755};
756
757class MultiresVectorDisplacementBaker : public MultiresBaker {
758 const SubdivCCG &high_subdiv_ccg_;
759 eBakeSpace space_;
760
761 public:
762 MultiresVectorDisplacementBaker(const SubdivCCG &subdiv_ccg, const eBakeSpace space)
763 : high_subdiv_ccg_(subdiv_ccg), space_(space)
764 {
765 }
766
767 float3 bake_pixel(const RasterizeTriangle &triangle,
768 const float2 &bary_uv,
769 const float2 &grid_uv,
770 RasterizeResult & /*result*/) const override
771 {
772 const float3 bake_level_position = triangle.get_position(bary_uv);
773
774 const float3 high_level_position = sample_position_on_subdiv_ccg(
775 high_subdiv_ccg_, triangle.grid_index, grid_uv);
776
777 const float3 displacement = high_level_position - bake_level_position;
778
779 if (space_ == R_BAKE_SPACE_TANGENT) {
780 const float3x3 to_tangent = get_to_tangent_matrix_tnb(triangle, bary_uv);
781 return to_tangent * displacement;
782 }
783
784 return displacement;
785 }
786
787 void write_pixel(const RasterizeTile &tile,
788 const int2 &coord,
789 const float3 &value) const override
790 {
791 write_pixel_to_image_buffer(*tile.ibuf, coord, value);
792 }
793};
794
796
797/* -------------------------------------------------------------------- */
800
801class MultiresNormalsBaker : public MultiresBaker {
802 const SubdivCCG &subdiv_ccg_;
803
804 public:
805 explicit MultiresNormalsBaker(const SubdivCCG &subdiv_ccg) : subdiv_ccg_(subdiv_ccg) {}
806
807 float3 bake_pixel(const RasterizeTriangle &triangle,
808 const float2 &bary_uv,
809 const float2 &grid_uv,
810 RasterizeResult & /*result*/) const override
811 {
812 const float3x3 to_tangent = get_to_tangent_matrix_tbn(triangle, bary_uv);
813 const float3 normal = sample_normal_on_subdiv_ccg(subdiv_ccg_, triangle.grid_index, grid_uv);
814 return math::normalize(to_tangent * normal) * 0.5f + float3(0.5f, 0.5f, 0.5f);
815 }
816
817 void write_pixel(const RasterizeTile &tile,
818 const int2 &coord,
819 const float3 &value) const override
820 {
821 write_pixel_to_image_buffer(*tile.ibuf, coord, value);
822 }
823};
824
826
827/* -------------------------------------------------------------------- */
830
831static void initialize_images(MultiresBakeRender &bake)
832{
833 bake.images.clear();
834
835 for (Image *image : bake.ob_image) {
836 if (image) {
837 bake.images.add(image);
838 }
839 }
840}
841
843
844/* -------------------------------------------------------------------- */
847
848static std::unique_ptr<MultiresBaker> create_baker(const MultiresBakeRender &bake,
849 const SubdivCCG &subdiv_ccg,
850 const ImBuf &ibuf,
851 ExtraBuffers &extra_buffers)
852{
853 switch (bake.type) {
854 case R_BAKE_NORMALS:
855 return std::make_unique<MultiresNormalsBaker>(subdiv_ccg);
857 return std::make_unique<MultiresDisplacementBaker>(subdiv_ccg, ibuf, extra_buffers);
859 return std::make_unique<MultiresVectorDisplacementBaker>(subdiv_ccg,
860 bake.displacement_space);
861 case R_BAKE_AO:
862 /* Not implemented, should not be used. */
863 break;
864 }
866 return nullptr;
867}
868
869static void rasterize_base_face(const MultiresBaker &baker,
870 const RasterizeTile &tile,
871 const MeshArrays &mesh_arrays,
872 const Span<float4> uv_tangents,
873 const int face_index,
874 RasterizeResult &result)
875{
876 const IndexRange &face = mesh_arrays.faces[face_index];
877 const Span<int> face_verts = mesh_arrays.corner_verts.slice(face);
878
879 RasterizeQuad quad;
880
881 /* - Grid coordinate (0, 0): face center.
882 * - Grid axis U points from the face center to the middle of the edge connecting corner to
883 * next_corner.
884 * - Grid axis V points from the face center to the middle of the edge connecting prev_corner to
885 * corner. */
886 quad.grid_uvs[0] = float2(0.0f, 0.0f);
887 quad.grid_uvs[1] = float2(1.0f, 0.0f);
888 quad.grid_uvs[2] = float2(1.0f, 1.0f);
889 quad.grid_uvs[3] = float2(0.0f, 1.0f);
890
891 quad.tex_uvs[0] = face_center_tex_uv_calc(mesh_arrays, face_index) - tile.uv_offset;
892 quad.positions[0] = bke::mesh::face_center_calc(mesh_arrays.vert_positions, face_verts);
893
894 /* TODO(sergey): Support corner normals. */
895
896 quad.is_flat = mesh_arrays.sharp_faces[face_index];
897 if (quad.is_flat) {
898 quad.normals[0] = mesh_arrays.face_normals[face_index];
899 }
900 else {
901 quad.normals[0] = face_center_smooth_normal_calc(mesh_arrays, face_index);
902 }
903
904 quad.has_uv_tangents = !uv_tangents.is_empty();
905 if (quad.has_uv_tangents) {
906 quad.uv_tangents[0] = face_center_uv_tangent_calc(mesh_arrays, uv_tangents, face_index);
907 }
908
909 for (const int corner : face) {
910 const int prev_corner = bke::mesh::face_corner_prev(face, corner);
911 const int next_corner = bke::mesh::face_corner_next(face, corner);
912
913 const float3 &position = mesh_arrays.vert_positions[mesh_arrays.corner_verts[corner]];
914 const float3 &next_position =
915 mesh_arrays.vert_positions[mesh_arrays.corner_verts[next_corner]];
916 const float3 &prev_position =
917 mesh_arrays.vert_positions[mesh_arrays.corner_verts[prev_corner]];
918
919 quad.grid_index = corner;
920
921 quad.tex_uvs[1] = (mesh_arrays.uv_map[corner] + mesh_arrays.uv_map[next_corner]) * 0.5f -
922 tile.uv_offset;
923 quad.tex_uvs[2] = mesh_arrays.uv_map[corner] - tile.uv_offset;
924 quad.tex_uvs[3] = (mesh_arrays.uv_map[prev_corner] + mesh_arrays.uv_map[corner]) * 0.5f -
925 tile.uv_offset;
926
927 quad.positions[1] = (position + next_position) * 0.5f;
928 quad.positions[2] = position;
929 quad.positions[3] = (prev_position + position) * 0.5f;
930
931 if (quad.is_flat) {
932 quad.normals[1] = quad.normals[0];
933 quad.normals[2] = quad.normals[0];
934 quad.normals[3] = quad.normals[0];
935 }
936 else {
937 const float3 &normal = mesh_arrays.vert_normals[mesh_arrays.corner_verts[corner]];
938 const float3 &next_normal = mesh_arrays.vert_normals[mesh_arrays.corner_verts[next_corner]];
939 const float3 &prev_normal = mesh_arrays.vert_normals[mesh_arrays.corner_verts[prev_corner]];
940
941 /* NOTE: No normalization here: do it after interpolation at the baking point.
942 *
943 * This preserves linearity of operation. If normalization is done here interpolation will go
944 * wrong. */
945 quad.normals[1] = (normal + next_normal) * 0.5f;
946 quad.normals[2] = normal;
947 quad.normals[3] = (prev_normal + normal) * 0.5f;
948 }
949
950 if (quad.has_uv_tangents) {
951 const float4 &tangent = uv_tangents[corner];
952 const float4 &next_tangent = uv_tangents[next_corner];
953 const float4 &prev_tangent = uv_tangents[prev_corner];
954
955 quad.uv_tangents[1] = (tangent + next_tangent) * 0.5f;
956 quad.uv_tangents[2] = tangent;
957 quad.uv_tangents[3] = (prev_tangent + tangent) * 0.5f;
958 }
959
960 rasterize_quad(baker, tile, quad, result);
961 }
962}
963
964static void bake_single_image_to_base_mesh(MultiresBakeRender &bake,
965 const Mesh &bake_level_mesh,
966 const SubdivCCG &subdiv_ccg,
967 Image &image,
968 ImageTile &image_tile,
969 ImBuf &ibuf,
970 ExtraBuffers &extra_buffers,
971 MultiresBakeResult &result)
972{
973 std::unique_ptr<MultiresBaker> baker = create_baker(bake, subdiv_ccg, ibuf, extra_buffers);
974 if (!baker) {
975 return;
976 }
977
978 MeshArrays mesh_arrays(bake_level_mesh);
979 Array<float4> uv_tangents;
980 if (need_tangent(bake)) {
981 uv_tangents = calc_uv_tangents(mesh_arrays);
982 }
983
984 RasterizeTile tile;
985 tile.ibuf = &ibuf;
986 tile.extra_buffers = &extra_buffers;
987 tile.uv_offset = get_tile_uv(image, image_tile);
988
989 SpinLock spin_lock;
990 BLI_spin_init(&spin_lock);
991 std::atomic<int> num_baked_faces = 0;
992 threading::parallel_for(mesh_arrays.faces.index_range(), 1, [&](const IndexRange range) {
993 for (const int64_t face_index : range) {
994 if (multiresbake_test_break(bake)) {
995 return;
996 }
997
998 /* Check whether the face is to be baked into the current image. */
999 const int mat_nr = mesh_arrays.material_indices[face_index];
1000 const Image *face_image = mat_nr < bake.ob_image.size() ? bake.ob_image[mat_nr] : nullptr;
1001 if (face_image != &image) {
1002 continue;
1003 }
1004
1005 RasterizeResult rasterize_result;
1006 rasterize_base_face(*baker, tile, mesh_arrays, uv_tangents, face_index, rasterize_result);
1007
1008 ++num_baked_faces;
1009 BLI_spin_lock(&spin_lock);
1010 result.height_min = std::min(result.height_min, rasterize_result.height_min);
1011 result.height_max = std::max(result.height_max, rasterize_result.height_max);
1012 if (bake.do_update) {
1013 *bake.do_update = true;
1014 }
1015 if (bake.progress) {
1016 *bake.progress = (float(bake.num_baked_objects) +
1017 float(num_baked_faces) / mesh_arrays.faces.size()) /
1018 bake.num_total_objects;
1019 }
1020 BLI_spin_unlock(&spin_lock);
1021 }
1022 });
1023 BLI_spin_end(&spin_lock);
1024}
1025
1027
1028/* -------------------------------------------------------------------- */
1031
1032struct GridCoord {
1033 int grid_index;
1034 float2 uv;
1035};
1036
1037static Array<GridCoord> get_subdivided_corner_grid_coords(subdiv::Subdiv &subdiv,
1038 const Mesh &coarse_mesh,
1039 const int level)
1040{
1041 struct SubdividedCornerGridCoordData {
1042 MeshArrays coarse_mesh_arrays;
1043 Array<GridCoord> corner_grid_coords;
1044 };
1045
1046 subdiv::ToMeshSettings mesh_settings;
1047 mesh_settings.resolution = (1 << level) + 1;
1048
1049 SubdividedCornerGridCoordData data;
1050 data.coarse_mesh_arrays = MeshArrays(coarse_mesh);
1051
1052 subdiv::ForeachContext foreach_context;
1053 foreach_context.user_data = &data;
1054
1055 foreach_context.topology_info = [](const subdiv::ForeachContext *context,
1056 const int /*num_vertices*/,
1057 const int /*num_edges*/,
1058 const int num_corners,
1059 const int /*num_faces*/,
1060 const Span<int> /*subdiv_face_offset*/) -> bool {
1061 SubdividedCornerGridCoordData *data = static_cast<SubdividedCornerGridCoordData *>(
1062 context->user_data);
1063 data->corner_grid_coords.reinitialize(num_corners);
1064 return true;
1065 };
1066
1067 foreach_context.loop = [](const subdiv::ForeachContext *context,
1068 void * /*tls*/,
1069 const int /*ptex_face_index*/,
1070 const float u,
1071 const float v,
1072 const int /*coarse_corner_index*/,
1073 const int coarse_face_index,
1074 const int coarse_corner,
1075 const int subdiv_corner_index,
1076 const int /*subdiv_vert_index*/,
1077 const int /*subdiv_edge_index*/) {
1078 SubdividedCornerGridCoordData *data = static_cast<SubdividedCornerGridCoordData *>(
1079 context->user_data);
1080
1081 const float2 ptex_uv(u, v);
1082 const IndexRange coarse_face = data->coarse_mesh_arrays.faces[coarse_face_index];
1083
1084 GridCoord &corner_grid_coord = data->corner_grid_coords[subdiv_corner_index];
1085 corner_grid_coord.grid_index = coarse_face.start() + coarse_corner;
1086
1087 if (coarse_face.size() == 4) {
1088 corner_grid_coord.uv = subdiv::ptex_face_uv_to_grid_uv(
1089 subdiv::rotate_quad_to_corner(coarse_corner, ptex_uv));
1090 }
1091 else {
1092 corner_grid_coord.uv = subdiv::ptex_face_uv_to_grid_uv(ptex_uv);
1093 }
1094 };
1095
1096 foreach_subdiv_geometry(&subdiv, &foreach_context, &mesh_settings, &coarse_mesh);
1097
1098 return data.corner_grid_coords;
1099}
1100
1101static void rasterize_subdivided_face(const MultiresBaker &baker,
1102 const RasterizeTile &tile,
1103 const MeshArrays &mesh_arrays,
1104 const Span<GridCoord> &corner_grid_coords,
1105 const Span<float4> uv_tangents,
1106 const int face_index,
1107 RasterizeResult &result)
1108{
1109 const IndexRange &face = mesh_arrays.faces[face_index];
1110
1111 /* This code operates with mesh with at least one subdivision level applied. Such mesh only has
1112 * quad faces as per how subdivision works. */
1113 BLI_assert(face.size() == 4);
1114
1115 RasterizeQuad quad;
1116
1117 /* TODO(sergey): Support corner normals. */
1118
1119 quad.is_flat = mesh_arrays.sharp_faces[face_index];
1120 quad.has_uv_tangents = !uv_tangents.is_empty();
1121 quad.grid_index = corner_grid_coords[face.start()].grid_index;
1122
1123 for (int i = 0; i < 4; ++i) {
1124 const int corner = face[i];
1125 const int vert = mesh_arrays.corner_verts[corner];
1126
1127 BLI_assert(corner_grid_coords[corner].grid_index == quad.grid_index);
1128 quad.grid_uvs[i] = corner_grid_coords[corner].uv;
1129
1130 quad.tex_uvs[i] = mesh_arrays.uv_map[corner] - tile.uv_offset;
1131 quad.positions[i] = mesh_arrays.vert_positions[vert];
1132 if (!quad.is_flat) {
1133 quad.normals[i] = mesh_arrays.vert_normals[vert];
1134 }
1135
1136 if (quad.has_uv_tangents) {
1137 quad.uv_tangents[i] = uv_tangents[corner];
1138 }
1139 }
1140
1141 if (quad.is_flat) {
1142 quad.normals[0] = mesh_arrays.face_normals[face_index];
1143 quad.normals[1] = quad.normals[0];
1144 quad.normals[2] = quad.normals[0];
1145 quad.normals[3] = quad.normals[0];
1146 }
1147
1148 rasterize_quad(baker, tile, quad, result);
1149}
1150
1151static void bake_single_image_to_subdivided_mesh(MultiresBakeRender &bake,
1152 const Mesh &bake_level_mesh,
1153 const SubdivCCG &subdiv_ccg,
1154 Image &image,
1155 ImageTile &image_tile,
1156 ImBuf &ibuf,
1157 ExtraBuffers &extra_buffers,
1158 MultiresBakeResult &result)
1159{
1160 std::unique_ptr<MultiresBaker> baker = create_baker(bake, subdiv_ccg, ibuf, extra_buffers);
1161 if (!baker) {
1162 return;
1163 }
1164
1165 MeshArrays mesh_arrays(bake_level_mesh);
1166 Array<float4> uv_tangents;
1167 if (need_tangent(bake)) {
1168 uv_tangents = calc_uv_tangents(mesh_arrays);
1169 }
1170
1171 RasterizeTile tile;
1172 tile.ibuf = &ibuf;
1173 tile.extra_buffers = &extra_buffers;
1174 tile.uv_offset = get_tile_uv(image, image_tile);
1175
1176 const Array<GridCoord> corner_grid_coords = get_subdivided_corner_grid_coords(
1177 *subdiv_ccg.subdiv, *bake.base_mesh, bake.multires_modifier->lvl);
1178
1179 SpinLock spin_lock;
1180 BLI_spin_init(&spin_lock);
1181 std::atomic<int> num_baked_faces = 0;
1182 threading::parallel_for(mesh_arrays.faces.index_range(), 1, [&](const IndexRange range) {
1183 for (const int64_t face_index : range) {
1184 if (multiresbake_test_break(bake)) {
1185 return;
1186 }
1187
1188 /* Check whether the face is to be baked into the current image. */
1189 const int mat_nr = mesh_arrays.material_indices[face_index];
1190 const Image *face_image = mat_nr < bake.ob_image.size() ? bake.ob_image[mat_nr] : nullptr;
1191 if (face_image != &image) {
1192 continue;
1193 }
1194
1195 RasterizeResult rasterize_result;
1196 rasterize_subdivided_face(*baker,
1197 tile,
1198 mesh_arrays,
1199 corner_grid_coords,
1200 uv_tangents,
1201 face_index,
1202 rasterize_result);
1203
1204 ++num_baked_faces;
1205 BLI_spin_lock(&spin_lock);
1206 result.height_min = std::min(result.height_min, rasterize_result.height_min);
1207 result.height_max = std::max(result.height_max, rasterize_result.height_max);
1208 if (bake.do_update) {
1209 *bake.do_update = true;
1210 }
1211 if (bake.progress) {
1212 *bake.progress = (float(bake.num_baked_objects) +
1213 float(num_baked_faces) / mesh_arrays.faces.size()) /
1214 bake.num_total_objects;
1215 }
1216 BLI_spin_unlock(&spin_lock);
1217 }
1218 });
1219 BLI_spin_end(&spin_lock);
1220}
1221
1223
1224/* -------------------------------------------------------------------- */
1228
1229/* Subdivide bake_level_mesh to the level of `total level - viewport level`.
1230 * Essentially bring the bake_level_mesh to the same resolution level as the top multi-resolution
1231 * level. */
1232static const Mesh *create_highres_mesh(const Mesh &bake_level_mesh,
1233 const MultiresModifierData &multires_modifier)
1234{
1235 const int top_level = multires_modifier.totlvl;
1236 const int bake_level = multires_modifier.lvl;
1237 const int subdivide_level = top_level - bake_level;
1238 if (subdivide_level == 0) {
1239 return &bake_level_mesh;
1240 }
1241
1242 subdiv::Settings subdiv_settings;
1243 BKE_multires_subdiv_settings_init(&subdiv_settings, &multires_modifier);
1244 subdiv::Subdiv *subdiv = subdiv::update_from_mesh(nullptr, &subdiv_settings, &bake_level_mesh);
1245
1246 subdiv::ToMeshSettings mesh_settings;
1247 mesh_settings.resolution = (1 << subdivide_level) + 1;
1248
1249 Mesh *result = subdiv::subdiv_to_mesh(subdiv, &mesh_settings, &bake_level_mesh);
1250
1252
1253 return result;
1254}
1255
1256/* Get grid coordinates for every corner of the highres_bake_mesh. */
1257static Array<GridCoord> get_highres_mesh_loop_grid_coords(
1258 subdiv::Subdiv &subdiv,
1259 const MultiresModifierData &multires_modifier,
1260 const Mesh &base_mesh,
1261 const Mesh &bake_level_mesh,
1262 const Mesh &highres_bake_mesh)
1263{
1264 UNUSED_VARS_NDEBUG(highres_bake_mesh);
1265
1266 if (multires_modifier.lvl == 0) {
1267 /* Simple case: baking from subdivided mesh highres_bake_mesh to the base mesh. */
1268 return get_subdivided_corner_grid_coords(
1269 subdiv, bake_level_mesh, multires_modifier.totlvl - multires_modifier.lvl);
1270 }
1271
1272 /* More tricky case:
1273 * - The base_mesh is first subdivided to the viewport level (bake_level_mesh)
1274 * - The bake_level_mesh is then further subdivided (highres_bake_mesh).
1275 *
1276 * This case needs an extra level of indirection: map loops from the highres_bake_mesh to the
1277 * faces of the bake_level_mesh, and then interpolate the grid coordinates calculated for the
1278 * bake_level_mesh to get grid coordinates.
1279 *
1280 * The coarse mesh here is the same as bake_level_mesh, and the subdiv mesh is the same as
1281 * highres_bake_mesh.
1282 *
1283 * It is possible to optimize the memory usage here by utilizing an implicit knowledge about
1284 * how faces in the high-res mesh are created from the bake level mesh: Since the bake level
1285 * mesh has some amount of subdivisions in this branch all its faces are quads. So all the
1286 * faces in the high-res mesh are also quads, created in the grid pattern from the bake level
1287 * faces. */
1288
1289 struct HighresCornerGridCoordData {
1290 MeshArrays bake_level_mesh_arrays;
1291 Array<GridCoord> bake_level_corner_grid_coords;
1292
1293 Array<GridCoord> corner_grid_coords;
1294 };
1295
1296 const int top_level = multires_modifier.totlvl;
1297 const int bake_level = multires_modifier.lvl;
1298 const int subdivide_level = top_level - bake_level;
1299
1300 subdiv::ToMeshSettings mesh_settings;
1301 mesh_settings.resolution = (1 << subdivide_level) + 1;
1302
1303 HighresCornerGridCoordData data;
1304 data.bake_level_mesh_arrays = MeshArrays(bake_level_mesh);
1305 data.bake_level_corner_grid_coords = get_subdivided_corner_grid_coords(
1306 subdiv, base_mesh, multires_modifier.lvl);
1307
1308 subdiv::ForeachContext foreach_context;
1309 foreach_context.user_data = &data;
1310
1311 foreach_context.topology_info = [](const subdiv::ForeachContext *context,
1312 const int /*num_vertices*/,
1313 const int /*num_edges*/,
1314 const int num_corners,
1315 const int /*num_faces*/,
1316 const Span<int> /*subdiv_face_offset*/) -> bool {
1317 HighresCornerGridCoordData *data = static_cast<HighresCornerGridCoordData *>(
1318 context->user_data);
1319 data->corner_grid_coords.reinitialize(num_corners);
1320 return true;
1321 };
1322
1323 foreach_context.loop = [](const subdiv::ForeachContext *context,
1324 void * /*tls*/,
1325 const int /*ptex_face_index*/,
1326 const float u,
1327 const float v,
1328 const int /*bake_level_corner_index*/,
1329 const int bake_level_face_index,
1330 const int /*bake_level_corner*/,
1331 const int highres_corner_index,
1332 const int /*highres_vert_index*/,
1333 const int /*highres_edge_index*/) {
1334 HighresCornerGridCoordData *data = static_cast<HighresCornerGridCoordData *>(
1335 context->user_data);
1336
1337 const Span<GridCoord> bake_level_corner_grid_coords = data->bake_level_corner_grid_coords;
1338
1339 const IndexRange bake_level_face = data->bake_level_mesh_arrays.faces[bake_level_face_index];
1340 BLI_assert(bake_level_face.size() == 4);
1341
1342 const int bake_level_face_start = bake_level_face.start();
1343
1344 GridCoord &corner_grid_coord = data->corner_grid_coords[highres_corner_index];
1345 corner_grid_coord.grid_index = bake_level_corner_grid_coords[bake_level_face_start].grid_index;
1346 corner_grid_coord.uv = interp_bilinear_quad(
1347 u,
1348 v,
1349 bake_level_corner_grid_coords[bake_level_face_start + 0].uv,
1350 bake_level_corner_grid_coords[bake_level_face_start + 1].uv,
1351 bake_level_corner_grid_coords[bake_level_face_start + 2].uv,
1352 bake_level_corner_grid_coords[bake_level_face_start + 3].uv);
1353
1354 /* Loops of the bake level mesh are supposed to be in the same grid. */
1355 BLI_assert(corner_grid_coord.grid_index ==
1356 bake_level_corner_grid_coords[bake_level_face_start + 1].grid_index);
1357 BLI_assert(corner_grid_coord.grid_index ==
1358 bake_level_corner_grid_coords[bake_level_face_start + 2].grid_index);
1359 BLI_assert(corner_grid_coord.grid_index ==
1360 bake_level_corner_grid_coords[bake_level_face_start + 3].grid_index);
1361 };
1362
1363 foreach_subdiv_geometry(&subdiv, &foreach_context, &mesh_settings, &bake_level_mesh);
1364
1365 BLI_assert(data.corner_grid_coords.size() == highres_bake_mesh.corners_num);
1366 return data.corner_grid_coords;
1367}
1368
1369static void bake_single_image_displacement(MultiresBakeRender &bake,
1370 const Mesh &bake_level_mesh,
1371 const SubdivCCG &subdiv_ccg,
1372 Image &image,
1373 ImageTile &image_tile,
1374 ImBuf &ibuf,
1375 ExtraBuffers &extra_buffers,
1376 MultiresBakeResult &result)
1377{
1378 std::unique_ptr<MultiresBaker> baker = create_baker(bake, subdiv_ccg, ibuf, extra_buffers);
1379 if (!baker) {
1380 return;
1381 }
1382
1383 const Mesh *highres_bake_mesh = result.highres_bake_mesh;
1384 if (!highres_bake_mesh) {
1385 highres_bake_mesh = create_highres_mesh(bake_level_mesh, *bake.multires_modifier);
1386 result.highres_bake_mesh = highres_bake_mesh;
1387 }
1388
1389 const Array<GridCoord> corner_grid_coords = get_highres_mesh_loop_grid_coords(
1390 *subdiv_ccg.subdiv,
1391 *bake.multires_modifier,
1392 *bake.base_mesh,
1393 bake_level_mesh,
1394 *highres_bake_mesh);
1395
1396 MeshArrays mesh_arrays(*highres_bake_mesh);
1397 Array<float4> uv_tangents;
1398 if (need_tangent(bake)) {
1399 uv_tangents = calc_uv_tangents(mesh_arrays);
1400 }
1401
1402 RasterizeTile tile;
1403 tile.ibuf = &ibuf;
1404 tile.extra_buffers = &extra_buffers;
1405 tile.uv_offset = get_tile_uv(image, image_tile);
1406
1407 SpinLock spin_lock;
1408 BLI_spin_init(&spin_lock);
1409 std::atomic<int> num_baked_faces = 0;
1410 threading::parallel_for(mesh_arrays.faces.index_range(), 1, [&](const IndexRange range) {
1411 for (const int64_t face_index : range) {
1412 if (multiresbake_test_break(bake)) {
1413 return;
1414 }
1415
1416 /* Check whether the face is to be baked into the current image. */
1417 const int mat_nr = mesh_arrays.material_indices[face_index];
1418 const Image *face_image = mat_nr < bake.ob_image.size() ? bake.ob_image[mat_nr] : nullptr;
1419 if (face_image != &image) {
1420 continue;
1421 }
1422
1423 RasterizeResult rasterize_result;
1424 rasterize_subdivided_face(*baker,
1425 tile,
1426 mesh_arrays,
1427 corner_grid_coords,
1428 uv_tangents,
1429 face_index,
1430 rasterize_result);
1431
1432 ++num_baked_faces;
1433 BLI_spin_lock(&spin_lock);
1434 result.height_min = std::min(result.height_min, rasterize_result.height_min);
1435 result.height_max = std::max(result.height_max, rasterize_result.height_max);
1436 if (bake.do_update) {
1437 *bake.do_update = true;
1438 }
1439 if (bake.progress) {
1440 *bake.progress = (float(bake.num_baked_objects) +
1441 float(num_baked_faces) / mesh_arrays.faces.size()) /
1442 bake.num_total_objects;
1443 }
1444 BLI_spin_unlock(&spin_lock);
1445 }
1446 });
1447 BLI_spin_end(&spin_lock);
1448}
1449
1451
1452/* -------------------------------------------------------------------- */
1455
1456static void bake_single_image(MultiresBakeRender &bake,
1457 const Mesh &bake_level_mesh,
1458 const SubdivCCG &subdiv_ccg,
1459 Image &image,
1460 ImageTile &image_tile,
1461 ImBuf &ibuf,
1462 ExtraBuffers &extra_buffers,
1463 MultiresBakeResult &result)
1464{
1465 if (ELEM(bake.type, R_BAKE_DISPLACEMENT, R_BAKE_VECTOR_DISPLACEMENT) &&
1466 !bake.use_low_resolution_mesh &&
1467 bake.multires_modifier->lvl != bake.multires_modifier->totlvl)
1468 {
1469 bake_single_image_displacement(
1470 bake, bake_level_mesh, subdiv_ccg, image, image_tile, ibuf, extra_buffers, result);
1471 return;
1472 }
1473
1474 if (bake.multires_modifier->lvl == 0) {
1475 bake_single_image_to_base_mesh(
1476 bake, bake_level_mesh, subdiv_ccg, image, image_tile, ibuf, extra_buffers, result);
1477 return;
1478 }
1479
1480 bake_single_image_to_subdivided_mesh(
1481 bake, bake_level_mesh, subdiv_ccg, image, image_tile, ibuf, extra_buffers, result);
1482}
1483
1484static void bake_images(MultiresBakeRender &bake,
1485 const Mesh &bake_level_mesh,
1486 const SubdivCCG &subdiv_ccg,
1487 MultiresBakeResult &result)
1488{
1489 for (Image *image : bake.images) {
1490 for (ImageTile &image_tile : image->tiles) {
1491 ImageUser iuser;
1492 BKE_imageuser_default(&iuser);
1493 iuser.tile = image_tile.tile_number;
1494
1495 ImBuf *ibuf = BKE_image_acquire_ibuf(image, &iuser, nullptr);
1496 if (ibuf && ibuf->x > 0 && ibuf->y > 0) {
1497 result.baked_ibufs.append({});
1498
1499 BakedImBuf &baked_ibuf = result.baked_ibufs.last();
1500 baked_ibuf.image = image;
1501 baked_ibuf.ibuf = ibuf;
1502 baked_ibuf.uv_offset = get_tile_uv(*image, image_tile);
1503
1504 ExtraBuffers &extra_buffers = baked_ibuf.extra_buffers;
1505 extra_buffers.mask_buffer.reinitialize(int64_t(ibuf->y) * ibuf->x);
1506 extra_buffers.mask_buffer.fill(FILTER_MASK_NULL);
1507
1508 bake_single_image(
1509 bake, bake_level_mesh, subdiv_ccg, *image, image_tile, *ibuf, extra_buffers, result);
1510 }
1511 }
1512 }
1513}
1514
1516
1517/* -------------------------------------------------------------------- */
1520
1521static void bake_ibuf_normalize_displacement(ImBuf &ibuf,
1522 const MutableSpan<float> displacement,
1523 const Span<char> mask,
1524 const float displacement_min,
1525 const float displacement_max)
1526{
1527 const float *current_displacement = displacement.data();
1528 const char *current_mask = mask.data();
1529 const float max_distance = math::max(math::abs(displacement_min), math::abs(displacement_max));
1530
1531 if (max_distance <= 1e-5f) {
1532 const float col[4] = {0.5f, 0.5f, 0.5f, 1.0f};
1533 IMB_rectfill(&ibuf, col);
1534 return;
1535 }
1536
1537 /* TODO(sergey): Look into multi-threading this loop. */
1538 const size_t ibuf_pixel_count = IMB_get_pixel_count(&ibuf);
1539 uchar *byte_data = ibuf.byte_data_for_write();
1540 float *float_data = ibuf.float_data_for_write();
1541 for (size_t i = 0; i < ibuf_pixel_count; i++) {
1542 if (*current_mask == FILTER_MASK_USED) {
1543 const float normalized_displacement = (*current_displacement + max_distance) /
1544 (max_distance * 2);
1545
1546 if (float_data) {
1547 /* TODO(sergey): Properly tackle ibuf.channels. */
1548 BLI_assert(ibuf.channels == 4);
1549 float *fp = float_data + int64_t(i) * 4;
1550 fp[0] = fp[1] = fp[2] = normalized_displacement;
1551 fp[3] = 1.0f;
1552 }
1553
1554 if (byte_data) {
1555 uchar *cp = byte_data + int64_t(i) * 4;
1556 cp[0] = cp[1] = cp[2] = unit_float_to_uchar_clamp(normalized_displacement);
1557 cp[3] = 255;
1558 }
1559 }
1560
1561 current_displacement++;
1562 current_mask++;
1563 }
1564}
1565
1566static void bake_ibuf_filter(ImBuf &ibuf,
1567 const MutableSpan<char> mask,
1568 const Mesh &bake_level_mesh,
1569 const int margin,
1570 const eBakeMarginType margin_type,
1571 const float2 uv_offset)
1572{
1573 /* NOTE: Must check before filtering. */
1574 const bool is_new_alpha = !ibuf.can_contain_alpha() && BKE_imbuf_alpha_test(&ibuf);
1575
1576 if (margin) {
1577 switch (margin_type) {
1578 case R_BAKE_ADJACENT_FACES: {
1580 mask.data(),
1581 margin,
1582 &bake_level_mesh,
1583 bake_level_mesh.active_uv_map_name(),
1584 uv_offset,
1585 false);
1586 break;
1587 }
1588 default:
1589 /* Fall through. */
1590 case R_BAKE_EXTEND:
1591 IMB_filter_extend(&ibuf, mask.data(), margin);
1592 break;
1593 }
1594 }
1595
1596 /* If the bake results in new alpha then change the image setting. */
1597 if (is_new_alpha) {
1598 ibuf.color_mode = ImColorMode::RGBA;
1599 }
1600 else {
1601 if (margin && !ibuf.can_contain_alpha()) {
1602 /* Clear alpha added by filtering. */
1603 IMB_rectfill_alpha(&ibuf, 1.0f);
1604 }
1605 }
1606}
1607
1608static void finish_images(MultiresBakeRender &bake,
1609 const Mesh &bake_level_mesh,
1610 MultiresBakeResult &result)
1611{
1612 const bool use_displacement_buffer = bake.type == R_BAKE_DISPLACEMENT;
1613
1614 for (BakedImBuf &baked_ibuf : result.baked_ibufs) {
1615 Image *image = baked_ibuf.image;
1616 ImBuf *ibuf = baked_ibuf.ibuf;
1617
1618 if (use_displacement_buffer) {
1619 bake_ibuf_normalize_displacement(*ibuf,
1620 baked_ibuf.extra_buffers.displacement_buffer,
1621 baked_ibuf.extra_buffers.mask_buffer,
1622 result.height_min,
1623 result.height_max);
1624 }
1625
1626 bake_ibuf_filter(*ibuf,
1627 baked_ibuf.extra_buffers.mask_buffer,
1628 bake_level_mesh,
1629 bake.bake_margin,
1630 bake.bake_margin_type,
1631 baked_ibuf.uv_offset);
1632
1634 IMB_mark_dirty(ibuf);
1635
1636 BKE_image_release_ibuf(image, ibuf, nullptr);
1637 DEG_id_tag_update(&image->id, 0);
1638 }
1639}
1640
1642
1643/* -------------------------------------------------------------------- */
1646
1647static subdiv::Subdiv *create_subdiv(const Mesh &mesh,
1648 const MultiresModifierData &multires_modifier)
1649{
1650 subdiv::Settings subdiv_settings;
1651 BKE_multires_subdiv_settings_init(&subdiv_settings, &multires_modifier);
1652 subdiv::Subdiv *subdiv = subdiv::update_from_mesh(nullptr, &subdiv_settings, &mesh);
1653 subdiv::displacement_attach_from_multires(subdiv, &mesh, &multires_modifier);
1654
1655 /* Initialization evaluation of the limit surface and the displacement. */
1656 if (!subdiv::eval_begin_from_mesh(subdiv, &mesh, subdiv::SUBDIV_EVALUATOR_TYPE_CPU)) {
1657 subdiv::free(subdiv);
1658 return nullptr;
1659 }
1660 subdiv::eval_init_displacement(subdiv);
1661
1662 return subdiv;
1663}
1664
1665static std::unique_ptr<SubdivCCG> create_subdiv_ccg(const Mesh &mesh,
1666 const MultiresModifierData &multires_modifier)
1667{
1668 subdiv::Subdiv *subdiv = create_subdiv(mesh, multires_modifier);
1669 if (!subdiv) {
1670 return nullptr;
1671 }
1672
1673 SubdivToCCGSettings settings;
1674 settings.resolution = (1 << multires_modifier.totlvl) + 1;
1675 settings.need_normal = true;
1676 settings.need_mask = false;
1677
1678 return BKE_subdiv_to_ccg(*subdiv, settings, mesh);
1679}
1680
1681static Mesh *create_bake_level_mesh(const Mesh &base_mesh,
1682 const MultiresModifierData &multires_modifier)
1683{
1684 subdiv::Subdiv *subdiv = create_subdiv(base_mesh, multires_modifier);
1685 if (!subdiv) {
1686 return nullptr;
1687 }
1688
1689 subdiv::ToMeshSettings mesh_settings;
1690 mesh_settings.resolution = (1 << multires_modifier.lvl) + 1;
1691 subdiv::displacement_attach_from_multires(subdiv, &base_mesh, &multires_modifier);
1692 Mesh *result = subdiv::subdiv_to_mesh(subdiv, &mesh_settings, &base_mesh);
1693 subdiv::free(subdiv);
1694 return result;
1695}
1696
1698
1699} // namespace
1700} // namespace render
1701
1703{
1704 using namespace blender::render;
1705
1706 std::unique_ptr<SubdivCCG> subdiv_ccg = create_subdiv_ccg(*bake.base_mesh,
1707 *bake.multires_modifier);
1708
1709 Mesh *bake_level_mesh = bake.base_mesh;
1710 if (bake.multires_modifier->lvl != 0) {
1711 bake_level_mesh = create_bake_level_mesh(*bake.base_mesh, *bake.multires_modifier);
1712 }
1713
1714 MultiresBakeResult result;
1715 initialize_images(bake);
1716 bake_images(bake, *bake_level_mesh, *subdiv_ccg, result);
1717
1718 const Mesh *filter_mesh = result.highres_bake_mesh ? result.highres_bake_mesh : bake_level_mesh;
1719 finish_images(bake, *filter_mesh, result);
1720
1721 if (result.highres_bake_mesh && result.highres_bake_mesh != bake_level_mesh) {
1722 BKE_id_free(nullptr, const_cast<Mesh *>(result.highres_bake_mesh));
1723 }
1724 if (bake_level_mesh != bake.base_mesh) {
1725 BKE_id_free(nullptr, bake_level_mesh);
1726 }
1727}
1728
1729} // namespace blender
CustomData interface, see also DNA_customdata_types.h.
#define FILTER_MASK_USED
Definition IMB_imbuf.hh:347
#define FILTER_MASK_NULL
Definition IMB_imbuf.hh:345
#define BLI_assert_unreachable()
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert(a)
#define ELEM(...)
long long int int64_t
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
SIMD_FORCE_INLINE btScalar length() const
Return the length of the vector.
Definition btVector3.h:257
AttributeSet attributes
constexpr bool is_empty() const
Definition BLI_span.hh:264
constexpr Span()=default
#define FLT_MAX
#define FLT_EPSILON
#define position
#define image(slot, qualifiers, format)
#define index(i)
constexpr T sign(T) RET
unsigned int uchar
const ccl_global KernelWorkTile * tile
#define unit_float_to_uchar_clamp_v3(v1, v2)
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
#define T
int face_corner_prev(const IndexRange face, const int corner)
Definition BKE_mesh.hh:306
Array< Array< float4 > > calc_uv_tangents(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, Span< int3 > corner_tris, Span< int > corner_tri_faces, Span< bool > sharp_faces, Span< float3 > vert_normals, Span< float3 > face_normals, Span< float3 > corner_normals, Span< Span< float2 > > uv_maps)
float3 face_center_calc(Span< float3 > vert_positions, Span< int > face_verts)
int face_corner_next(const IndexRange face, const int corner)
Definition BKE_mesh.hh:315
void free(Subdiv *subdiv)
Definition subdiv.cc:192
bool foreach_subdiv_geometry(Subdiv *subdiv, const ForeachContext *context, const ToMeshSettings *mesh_settings, const Mesh *coarse_mesh)
BLI_INLINE void ptex_face_uv_to_grid_uv(float ptex_u, float ptex_v, float *r_grid_u, float *r_grid_v)
BLI_INLINE int rotate_quad_to_corner(float quad_u, float quad_v, float *r_corner_u, float *r_corner_v)
Mesh * subdiv_to_mesh(Subdiv *subdiv, const ToMeshSettings *settings, const Mesh *coarse_mesh)
Subdiv * update_from_mesh(Subdiv *subdiv, const Settings *settings, const Mesh *mesh)
Definition subdiv.cc:181
int context(const bContext *C, const char *member, bContextDataResult *result)
VecBase< T, 3 > interp_bilinear_quad(const VecBase< T, 3 > &v0, const VecBase< T, 3 > &v1, const VecBase< T, 3 > &v2, const VecBase< T, 3 > &v3, const T u, const T v)
T dot(const QuaternionBase< T > &a, const QuaternionBase< T > &b)
T min(const T &a, const T &b)
CartesianBasis invert(const CartesianBasis &basis)
AxisSigned cross(const AxisSigned a, const AxisSigned b)
MatBase< T, NumCol, NumRow > normalize(const MatBase< T, NumCol, NumRow > &a)
T max(const T &a, const T &b)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
MINLINE unsigned char unit_float_to_uchar_clamp(float val)
void IMB_rectfill_alpha(ImBuf *ibuf, float value)
Definition rectop.cc:1309
pthread_spinlock_t SpinLock
@ R_BAKE_ADJACENT_FACES
void RE_multires_bake_images(MultiresBakeRender &bake)
size_t IMB_get_pixel_count(const ImBuf *ibuf)
Get the length of the data of the given image buffer in pixels.
void RE_generate_texturemargin_adjacentfaces(ImBuf *ibuf, char *mask, const int margin, const Mesh *mesh, StringRef uv_layer, const float uv_offset[2], const bool conservative)
void IMB_mark_dirty(ImBuf *ibuf)
void IMB_rectfill(ImBuf *drect, const float col[4])
Definition rectop.cc:1165
ATTR_WARN_UNUSED_RESULT const BMVert * v
Global G
Definition blender.cc:57
VecBase< float, 4 > float4
void BKE_multires_subdiv_settings_init(bke::subdiv::Settings *settings, const MultiresModifierData *mmd)
void BLI_spin_end(SpinLock *spin)
Definition threads.cc:448
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
unsigned char uchar
Definition BKE_text.h:58
void resolve_tri_uv_v2(float r_uv[2], const float st[2], const float st0[2], const float st1[2], const float st2[2])
void DEG_id_tag_update(ID *id, unsigned int flags)
BMesh const char void * data
void IMB_partial_update_mark_full(ImBuf *ibuf)
MatBase< float, 3, 3 > float3x3
void BKE_id_free(Main *bmain, void *idv)
ImBuf * BKE_image_acquire_ibuf(Image *ima, ImageUser *iuser, void **r_lock)
@ R_BAKE_VECTOR_DISPLACEMENT
void BLI_spin_init(SpinLock *spin)
Definition threads.cc:394
std::unique_ptr< SubdivCCG > BKE_subdiv_to_ccg(bke::subdiv::Subdiv &subdiv, const SubdivToCCGSettings &settings, const Mesh &coarse_mesh, SubdivCCGMaskEvaluator *mask_evaluator=nullptr)
void IMB_filter_extend(ImBuf *ibuf, char *mask, int filter)
Definition filter.cc:203
VecBase< float, 3 > float3
void BKE_imageuser_default(ImageUser *iuser)
void BKE_image_get_tile_uv(const Image *ima, const int tile_number, float r_uv[2])
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
void vert(const int vert_id, float4 &position)
#define fabsf
#define ceilf
float x
float y
float x
Definition sky_math.h:136
float w
Definition sky_math.h:225