Blender V5.3
paint_image_3d.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9/* Paint a color made from hash of node pointer. */
10// #define DEBUG_PIXEL_NODES
11
12#include "DNA_brush_types.h"
13#include "DNA_image_types.h"
14#include "DNA_object_types.h"
15#include "DNA_userdef_types.h"
16
17#include "ED_paint.hh"
18
19#include "BLI_bit_vector.hh"
20#include "BLI_bounds.hh"
21#include "BLI_colorspace.hh"
23#include "BLI_listbase.hh"
25#include "BLI_math_color_c.hh"
26#include "BLI_math_geom_c.hh"
28#include "BLI_math_vector.hh"
29#ifdef DEBUG_PIXEL_NODES
30# include "BLI_hash_c.hh"
31#endif
32
33#include "DNA_mesh_types.h"
34
36#include "IMB_imbuf.hh"
37
38#include "BKE_attribute.hh"
39#include "BKE_brush.hh"
40#include "BKE_image_wrappers.hh"
41#include "BKE_object_types.hh"
42#include "BKE_paint_bvh.hh"
44
45#include "image_paint_intern.hh"
46#include "mesh_brush_common.hh"
47#include "sculpt_automask.hh"
48#include "sculpt_intern.hh"
49
50#include <atomic>
51
52namespace blender {
53
55
56using namespace blender::bke::pbvh::pixels;
57using namespace blender::bke::image;
58
60{
61 if (!image) {
62 return;
63 }
64
65 BLI_assert(image_buffers.size() <= image->tiles.count());
66 for (ImBuf *buffer : image_buffers.values()) {
67 BKE_image_release_ibuf(image, buffer, nullptr);
68 }
69 image_buffers.clear();
70}
71std::unique_ptr<ImageData> ImageData::init_active_image(Object &ob, ImagePaintSettings &settings)
72{
73 std::optional<CanvasImageData> canvas_image_data = BKE_paint_canvas_image_get(settings, ob);
74 if (!canvas_image_data) {
75 return nullptr;
76 }
77
78 std::unique_ptr<ImageData> image_data = std::make_unique<ImageData>();
79 image_data->image = canvas_image_data->first;
80 image_data->image_user = canvas_image_data->second;
81
82 BLI_assert(image_data->image);
83
84 return image_data;
85}
86std::unique_ptr<ImageData> ImageData::init_mask_image(ImagePaintSettings &settings)
87{
88 if (!settings.stencil) {
89 return nullptr;
90 }
91
92 std::unique_ptr<ImageData> image_data = std::make_unique<ImageData>();
93 image_data->image = settings.stencil;
94 image_data->image_user = ImageUser{};
95
96 BLI_assert(image_data->image);
97
98 return image_data;
99}
100
101static void fetch_image_buffers(ImageData &image_data,
102 bke::pbvh::Node & /*node*/,
103 PixelNode &pixel_node,
104 const bool is_mask_brush)
105{
107 for (const UDIMTilePixels &tile : pixel_node.tiles) {
108 ImBuf *buffer = image_data.image_buffers.lookup_or_add_cb(tile.tile_number, [&]() {
109 ImageUser tile_user = image_data.image_user_get();
110 tile_user.tile = tile.tile_number;
111
112 ImBuf *ibuf = BKE_image_acquire_ibuf(image_data.image, &tile_user, nullptr);
113 return ibuf;
114 });
115
116 if (buffer) {
117 image_data.undo_tile_pushed.lookup_or_add_cb(tile.tile_number, [&]() {
118 const int64_t tiles_x = (buffer->x + ED_IMAGE_UNDO_TILE_SIZE - 1) >>
119 ED_IMAGE_UNDO_TILE_BITS;
120 const int64_t tiles_y = (buffer->y + ED_IMAGE_UNDO_TILE_SIZE - 1) >>
121 ED_IMAGE_UNDO_TILE_BITS;
122 return Array<uint32_t>(tiles_x * tiles_y, 0);
123 });
124 image_data.seam_tile_modified.lookup_or_add_cb(tile.tile_number, [&]() {
125 const int tiles_x = (buffer->x + SEAM_TILE_SIZE - 1) >> SEAM_TILE_BITS;
126 const int tiles_y = (buffer->y + SEAM_TILE_SIZE - 1) >> SEAM_TILE_BITS;
127 return Array<uint8_t>(int64_t(tiles_x) * tiles_y, 0);
128 });
129 image_data.processors.lookup_or_add_cb(tile.tile_number, [&]() {
130 const ColorSpace &buffer_colorspace = buffer->float_data() ? buffer->float_colorspace() :
131 buffer->byte_colorspace();
132
133 TileColorspaceProcessor processor;
134 if (is_mask_brush) {
135 return processor;
136 }
137
138 /* Fast path for sRGB byte, to avoid overhead of calling into OpenColorIO. */
139 if (!buffer->float_data() && buffer->byte_data() &&
140 IMB_colormanagement_space_is_srgb(&buffer_colorspace))
141 {
142 processor.is_srgb_byte = true;
143 processor.is_noop = false;
144 return processor;
145 }
146
147 ColormanageProcessor buffer_to_linear =
149 if (buffer_to_linear.is_noop()) {
150 return processor;
151 }
152
153 processor.buffer_to_linear_processor = std::move(buffer_to_linear);
154 processor.linear_to_buffer_processor =
156 processor.is_noop = false;
157
158 return processor;
159 });
160 if (buffer->float_data()) {
161 BLI_assert(ELEM(buffer->channels, 0, 4));
162 image_data.data_buffers.add_overwrite(
163 tile.tile_number,
164 MutableSpan(reinterpret_cast<float4 *>(buffer->float_data_for_write()),
165 IMB_get_pixel_count(buffer)));
166 }
167 else {
168 image_data.data_buffers.add_overwrite(
169 tile.tile_number,
170 MutableSpan(reinterpret_cast<uchar4 *>(buffer->byte_data_for_write()),
171 IMB_get_pixel_count(buffer)));
172 }
173 }
174 }
175}
176
177static void calc_pixel_row_positions(const float3 P_start,
178 const float3 P_delta,
179 const MutableSpan<float3> positions,
180 IndexRange range)
181{
183 BLI_assert(range.size() == positions.size());
184
185 const float3 start = P_start + P_delta * range.start();
186 for (const int i : positions.index_range()) {
187 positions[i] = start + P_delta * i;
188 }
189}
190
192 const Span<int> corner_verts,
193 const Span<int3> corner_tris,
194 const Span<int> tri_indices,
195 const Span<float3> positions)
196{
198 const float3 location = ss.cache ? ss.cache->location_symm : ss.cursor_location;
199 const float radius = ss.cache ? ss.cache->radius : ss.cursor_radius;
200 const Bounds<float3> brush_bounds(location - radius, location + radius);
201
202 BitVector<> brush_test(tri_indices.size());
203 for (const int i : tri_indices.index_range()) {
204 const int3 &tri = corner_tris[tri_indices[i]];
205
206 Bounds<float3> tri_bounds(positions[corner_verts[tri[0]]]);
207 math::min_max(positions[corner_verts[tri[1]]], tri_bounds.min, tri_bounds.max);
208 math::min_max(positions[corner_verts[tri[2]]], tri_bounds.min, tri_bounds.max);
209
210 brush_test[i].set(
211 isect_aabb_aabb_v3(brush_bounds.min, brush_bounds.max, tri_bounds.min, tri_bounds.max));
212 }
213 return brush_test;
214}
215
216static void apply_selection_filter(const Span<int> corner_tri_faces,
217 const Span<int> tri_indices,
218 const Span<bool> select_poly,
219 BitVector<> &brush_test)
220{
221 BLI_assert(tri_indices.size() == brush_test.size());
222
223 for (const int i : tri_indices.index_range()) {
224 const bool current = brush_test[i];
225 brush_test[i].set(current && select_poly[corner_tri_faces[tri_indices[i]]]);
226 }
227}
228
245
247{
248 PaintBlendSettings blend_settings;
249 blend_settings.brush_color = float4(invert ? float3(1.0f - brush.weight) : float3(brush.weight),
250 1.0f);
251 blend_settings.brush_alpha = brush.alpha;
252 blend_settings.blend_mode = IMB_BlendMode(brush.blend);
253 return blend_settings;
254}
255
258 const float brush_alpha,
259 const bool is_mix,
260 const IMB_BlendMode blend_mode,
261 const float factor,
262 const float4 color)
263{
264 const float4 paint_color = brush_color * (factor * brush_alpha);
265 /* Many blend modes don't write alpha to the result, so copy it. */
267 /* TODO: try making IMB_blend_color_float inline instead. */
268 if (is_mix) {
269 blend_color_mix_float(result, color, paint_color);
270 }
271 else {
272 IMB_blend_color_float(result, color, paint_color, blend_mode);
273 }
274 return result;
275}
276
277#ifdef DEBUG_PIXEL_NODES
278static float4 paint_debug_color(float4 scene, const int img_y)
279{
280 if ((img_y >> 3) & 1) {
281 scene.x *= 0.5f;
282 scene.y *= 0.5f;
283 scene.z *= 0.5f;
284 }
285 return scene;
286}
287#endif
288
292template<typename PixelT>
294 const PaintBlendSettings &settings,
295 const Span<float> factors,
296 const int start,
297 const int size,
298 const MutableSpan<PixelT> image_pixels,
299 const int img_x,
300 [[maybe_unused]] const int img_y,
301 const TileColorspaceProcessor &processors,
302 Vector<float4> &paint_pixels)
303{
304 constexpr bool is_float = std::is_same_v<typename PixelT::base_type, float>;
306
307 MutableSpan<float4> scene_linear_pixels;
308
309 /* Convert from image colorspace to scene linear. */
310 if constexpr (is_float) {
311 scene_linear_pixels = image_pixels.slice(img_x, size);
312 }
313 else {
314 paint_pixels.resize(size);
315 for (int i = 0; i < size; i++) {
316 rgba_uchar_to_float(paint_pixels[i], image_pixels[img_x + i]);
317 }
318 scene_linear_pixels = paint_pixels;
319 }
320
321 if (!processors.is_noop) {
323 reinterpret_cast<float *>(scene_linear_pixels.data()), size, 1, 4, false);
324 }
325
326 /* Blend. */
327 const bool is_mix = settings.blend_mode == IMB_BLEND_MIX;
328 for (int i = 0; i < size; i++) {
329 float4 scene = scene_linear_pixels[i];
330#ifdef DEBUG_PIXEL_NODES
331 scene = paint_debug_color(scene, img_y);
332#endif
333 scene_linear_pixels[i] = paint_blend_pixel(settings.brush_color,
334 settings.brush_alpha,
335 is_mix,
336 settings.blend_mode,
337 factors[start + i],
338 scene);
339 }
340
341 /* Convert from scene linear to image colorspace. */
342 if (!processors.is_noop) {
344 reinterpret_cast<float *>(scene_linear_pixels.data()), size, 1, 4, false);
345 }
346
347 /* Write out byte, float was already modified in place. */
348 if constexpr (!is_float) {
349 for (int i = 0; i < size; i++) {
350 rgba_float_to_uchar(image_pixels[img_x + i], scene_linear_pixels[i]);
351 }
352 }
353}
354
359template<typename PixelT>
360static void paint_blend_pixels(const PaintBlendSettings &settings,
361 const Span<float> factors,
362 const int start,
363 const int size,
364 const MutableSpan<PixelT> image_pixels,
365 const int img_x,
366 const int img_y,
367 const TileColorspaceProcessor &processors,
368 Vector<float4> &paint_pixels)
369{
370 constexpr bool is_float = std::is_same_v<typename PixelT::base_type, float>;
371 const bool fast_colorspace = is_float ? processors.is_noop : processors.is_srgb_byte;
372 if (!fast_colorspace) {
373 /* Slow path with OpenColorIO. */
375 settings, factors, start, size, image_pixels, img_x, img_y, processors, paint_pixels);
376 return;
377 }
378
380
381 /* Keep variables in registers during the loop. */
382 const float4 brush_color = settings.brush_color;
383 const float brush_alpha = settings.brush_alpha;
384 const bool is_mix = settings.blend_mode == IMB_BLEND_MIX;
385 const IMB_BlendMode blend_mode = settings.blend_mode;
386
387 if constexpr (is_float) {
388 /* Scene linear float. */
389 for (int i = 0; i < size; i++) {
390 float4 scene = image_pixels[img_x + i];
391#ifdef DEBUG_PIXEL_NODES
392 scene = paint_debug_color(scene, img_y);
393#endif
394 image_pixels[img_x + i] = paint_blend_pixel(
395 brush_color, brush_alpha, is_mix, blend_mode, factors[start + i], scene);
396 }
397 }
398 else {
399 /* sRGB byte. */
400 paint_pixels.resize(size);
401 for (int i = 0; i < size; i++) {
402 float4 scene;
403 srgb_to_linearrgb_uchar4(scene, image_pixels[img_x + i]);
405#ifdef DEBUG_PIXEL_NODES
406 scene = paint_debug_color(scene, img_y);
407#endif
408 paint_pixels[i] = paint_blend_pixel(
409 brush_color, brush_alpha, is_mix, blend_mode, factors[start + i], scene);
410 }
411
412 /* SIMD optimized write. */
414 nullptr :
415 reinterpret_cast<const float (*)[3]>(
417 linearrgb_to_srgb_uchar4_n(reinterpret_cast<uchar(*)[4]>(&image_pixels[img_x]),
418 reinterpret_cast<const float (*)[4]>(paint_pixels.data()),
419 size,
420 matrix);
421 }
422}
423
437
439{
440 return {int2(std::numeric_limits<int>::max()), int2(std::numeric_limits<int>::lowest())};
441}
442
447static void push_undo_tiles(ImageData &image_data,
448 const image::TileNumber tile_number,
449 ImBuf &image_buffer,
450 MutableSpan<uint32_t> tile_pushed,
451 const int img_x_start,
452 const int img_x_end,
453 const int img_y)
454{
455 const int tile_y = img_y >> ED_IMAGE_UNDO_TILE_BITS;
456 const int tile_x_start = img_x_start >> ED_IMAGE_UNDO_TILE_BITS;
457 const int tile_x_end = img_x_end >> ED_IMAGE_UNDO_TILE_BITS;
458 const int undo_tiles_x = (image_buffer.x + ED_IMAGE_UNDO_TILE_SIZE - 1) >>
460
461 for (int tile_x = tile_x_start; tile_x <= tile_x_end; tile_x++) {
462 const int tile_index = tile_y * undo_tiles_x + tile_x;
463 std::atomic_ref<uint32_t> pushed(tile_pushed[tile_index]);
464 /* This atomic load is free on x86_64, and cheap on arm64. */
465 if (pushed.load(std::memory_order_acquire)) {
466 continue;
467 }
468 ImageUser tile_user = image_data.image_user_get();
469 tile_user.tile = tile_number;
472 undo_tiles, image_data.image, &image_buffer, &tile_user, tile_x, tile_y, nullptr, nullptr);
473 pushed.store(1, std::memory_order_release);
474 }
475}
476
482 const int tiles_x,
483 const int tiles_y,
484 const int x_start,
485 const int x_end,
486 const int y)
487{
488 const int tile_y = y >> SEAM_TILE_BITS;
489 const int tile_y_min = (tile_y > 0) ? tile_y - 1 : 0;
490 const int tile_y_max = (tile_y + 1 < tiles_y) ? tile_y + 1 : tiles_y - 1;
491
492 const int tile_x_start = x_start >> SEAM_TILE_BITS;
493 const int tile_x_end = x_end >> SEAM_TILE_BITS;
494 const int tile_x_min = (tile_x_start > 0) ? tile_x_start - 1 : 0;
495 const int tile_x_max = (tile_x_end + 1 < tiles_x) ? tile_x_end + 1 : tiles_x - 1;
496
497 for (int dty = tile_y_min; dty <= tile_y_max; dty++) {
498 for (int dtx = tile_x_min; dtx <= tile_x_max; dtx++) {
499 mask[int64_t(dty) * tiles_x + dtx] = 1;
500 }
501 }
502}
503
504static void do_paint_pixels(const PaintBlendSettings &blend_settings,
505 const Brush &brush,
506 Object &object,
507 Span<float3> positions_eval,
508 Span<int> corner_verts,
509 Span<int3> corner_tris,
510 Span<int> corner_tri_faces,
511 Span<bool> select_poly,
512 ImageData &image_data,
513 bke::pbvh::Node & /*node*/,
514 PixelNode &pixel_node)
515{
517 SculptSession &ss = *object.runtime->sculpt_session;
518 const StrokeCache &cache = *ss.cache;
520 ss, corner_verts, corner_tris, pixel_node.uv_primitives.tri_indices, positions_eval);
521
522 if (!select_poly.is_empty()) {
523 BLI_assert(!corner_tri_faces.is_empty());
525 corner_tri_faces, pixel_node.uv_primitives.tri_indices, select_poly, brush_test);
526 }
527
528#ifdef DEBUG_PIXEL_NODES
529 float4 debug_color;
531
532 debug_color[0] = float(hash & 255) / 255.0f;
533 debug_color[1] = float((hash >> 8) & 255) / 255.0f;
534 debug_color[2] = float((hash >> 16) & 255) / 255.0f;
535 debug_color[3] = 1.0f;
536#endif
537
538 bool pixels_updated = false;
539
540 const float3 location = ss.cache ? ss.cache->location_symm : ss.cursor_location;
541 const float radius = ss.cache ? ss.cache->radius : ss.cursor_radius;
542 const Bounds<float3> brush_bounds(location - radius, location + radius);
543
544 for (UDIMTilePixels &tile_data : pixel_node.tiles) {
545 ImBuf *image_buffer = image_data.image_buffers.lookup_default(tile_data.tile_number, nullptr);
546 if (image_buffer == nullptr) {
547 continue;
548 }
549
550 BufferType mutable_buffer = image_data.data_buffers.lookup_default(tile_data.tile_number,
551 BufferType{});
552 if (std::holds_alternative<std::monostate>(mutable_buffer)) {
554 continue;
555 }
556
557 const TileColorspaceProcessor *processors = image_data.processors.lookup_ptr(
558 tile_data.tile_number);
559
560 const MutableSpan<uint32_t> undo_tile_pushed = image_data.undo_tile_pushed.lookup(
561 tile_data.tile_number);
562
564
565 /* Parallelize over contiguous runs containing one or more per-triangle pixel rows. */
566 const int num_runs = tile_data.pixel_row_run_starts.size() - 1;
567 threading::parallel_for(IndexRange(num_runs), 32, [&](const IndexRange task_range) {
568 PaintLocalData &tls = all_factor_tls.local();
569 for (const int run_i : task_range) {
570 const int run_begin = tile_data.pixel_row_run_starts[run_i];
571 const int run_end = tile_data.pixel_row_run_starts[run_i + 1];
572
573 /* Compute run position and size. */
574 const int run_x = int(tile_data.pixel_row_run_start_coords[run_i].x);
575 const int run_y = int(tile_data.pixel_row_run_start_coords[run_i].y);
576 int run_size = 0;
577 for (int k = run_begin; k < run_end; k++) {
578 run_size += int(tile_data.pixel_rows[k].num_pixels);
579 }
580
581 /* Resize temporary data to match run size. */
582 tls.factors.resize(run_size);
583 tls.pixel_positions.resize(run_size);
584 tls.distances.resize(run_size);
585 const MutableSpan<float> factors = tls.factors;
586 const MutableSpan<float3> positions = tls.pixel_positions;
587 const MutableSpan<float> distances = tls.distances;
588
589 /* Tracking for active subset of run with nonzero factors. */
590 int run_active_begin = -1;
591 int run_active_end = -1;
592 int tri_skip_start = -1;
593 int tri_row_offset = 0;
594
595 /* For each triangle pixel row in the run. */
596 for (int k = run_begin; k < run_end; k++) {
597 const PackedPixelRow &tri_row = tile_data.pixel_rows[k];
598 const int tri_row_size = int(tri_row.num_pixels);
599
600 /* Use pixel to position mapping to compute 3D position. */
601 const int tri_row_x = run_x + tri_row_offset;
602 const int uv_prim = tri_row.uv_primitive_index;
603 const float3x3 &pixel_to_position = pixel_node.uv_primitives.pixel_to_position[uv_prim];
604 const float3 P_delta = pixel_to_position[0];
605 const float3 P_start = P_delta * tri_row_x + pixel_to_position[1] * run_y +
606 pixel_to_position[2];
607
608 /* Quick bounds check. */
609 if (brush_test[tri_row.uv_primitive_index] &&
610 brush_bounds.intersects_segment(P_start, P_start + P_delta * tri_row_size))
611 {
612 /* Compute brush factors per triangle. */
613 const MutableSpan<float> tri_factors = factors.slice(tri_row_offset, tri_row_size);
614 const MutableSpan<float3> tri_positions = positions.slice(tri_row_offset,
615 tri_row_size);
616 const MutableSpan<float> tri_distances = distances.slice(tri_row_offset, tri_row_size);
617
618 tri_factors.fill(1.0f);
619 calc_pixel_row_positions(P_start, P_delta, tri_positions, IndexRange(tri_row_size));
621 ss, tri_positions, eBrushFalloffShape(brush.falloff_shape), tri_distances);
622 filter_distances_with_radius(cache.radius, tri_distances, tri_factors);
623 apply_hardness_to_distances(cache, tri_distances);
624 calc_brush_strength_factors(cache, brush, tri_distances, tri_factors);
626 PaintMode::Texture3D, ss, brush, tri_positions, tri_factors);
627 scale_factors(tri_factors, cache.bstrength);
628
629 /* Track which subset of the run has non-zero factors. */
630 int tri_active_begin = -1;
631 int tri_active_end = -1;
632 for (int i = 0; i < tri_row_size; i++) {
633 if (tri_factors[i] != 0.0f) {
634 if (tri_active_begin < 0) {
635 tri_active_begin = i;
636 }
637 tri_active_end = i;
638 }
639 }
640
641 if (tri_active_begin >= 0) {
642 if (run_active_begin < 0) {
643 run_active_begin = tri_row_offset + tri_active_begin;
644 }
645 else if (tri_skip_start >= 0) {
646 factors.slice(tri_skip_start, tri_row_offset - tri_skip_start).fill(0.0f);
647 tri_skip_start = -1;
648 }
649 run_active_end = tri_row_offset + tri_active_end;
650 }
651 }
652 else {
653 /* No intersection, skip this triangle row. */
654 if (run_active_begin >= 0 && tri_skip_start < 0) {
655 tri_skip_start = tri_row_offset;
656 }
657 }
658 tri_row_offset += tri_row_size;
659 }
660
661 if (run_active_begin < 0) {
662 continue;
663 }
664
665 /* Record modified spans. */
666 const int run_active_size = run_active_end - run_active_begin + 1;
667 const int run_active_x = run_x + run_active_begin;
668 tls.modified_spans.append({.x = run_active_x, .y = run_y, .size = run_active_size});
669
670 /* Push undo tiles before we edit the pixels. */
671 push_undo_tiles(image_data,
672 tile_data.tile_number,
673 *image_buffer,
674 undo_tile_pushed,
675 run_active_x,
676 run_active_x + run_active_size - 1,
677 run_y);
678
679 /* Blend pixels with computed factors. */
680 if (std::holds_alternative<MutableSpan<float4>>(mutable_buffer)) {
681 paint_blend_pixels<float4>(blend_settings,
682 factors,
683 run_active_begin,
684 run_active_size,
685 std::get<MutableSpan<float4>>(mutable_buffer),
686 run_y * image_buffer->x + run_active_x,
687 run_y,
688 *processors,
690 }
691 else {
692 paint_blend_pixels<uchar4>(blend_settings,
693 factors,
694 run_active_begin,
695 run_active_size,
696 std::get<MutableSpan<uchar4>>(mutable_buffer),
697 run_y * image_buffer->x + run_active_x,
698 run_y,
699 *processors,
701 }
702 }
703 });
704
705 /* Mark the seam tiles modified and update dirty bounds. */
706 const MutableSpan<uint8_t> seam_tile_modified = image_data.seam_tile_modified.lookup(
707 tile_data.tile_number);
708 const int seam_tiles_x = (image_buffer->x + SEAM_TILE_SIZE - 1) >> SEAM_TILE_BITS;
709 const int seam_tiles_y = (image_buffer->y + SEAM_TILE_SIZE - 1) >> SEAM_TILE_BITS;
710 Bounds<int2> dirty_bounds = negative_bounds();
711 for (PaintLocalData &tls : all_factor_tls) {
712 for (const PaintLocalData::ModifiedSpan span : tls.modified_spans) {
713 mark_seam_tiles_modified(seam_tile_modified,
714 seam_tiles_x,
715 seam_tiles_y,
716 span.x,
717 span.x + span.size - 1,
718 span.y);
719 const int2 start(span.x, span.y);
720 const int2 end = start + int2(span.size, 1);
721 dirty_bounds = bounds::merge(dirty_bounds, Bounds<int2>(start, end));
722 }
723 }
724 if (!dirty_bounds.is_empty()) {
725 tile_data.mark_dirty(dirty_bounds);
726 }
727
728 if (tile_data.flags.dirty) {
729 IMB_mark_dirty(image_buffer);
730 }
731 pixels_updated |= tile_data.flags.dirty;
732 }
733
734 pixel_node.flags.dirty |= pixels_updated;
735}
736
737/* -------------------------------------------------------------------- */
740
742 const IndexMask &node_mask)
743{
744 Vector<image::TileNumber> dirty_tiles;
745 node_mask.foreach_index(
746 [&](const int i) { bke::pbvh::pixels::collect_dirty_tiles(nodes[i], dirty_tiles); });
747 return dirty_tiles;
748}
750 ImageData &image_data,
751 Span<TileNumber> tile_numbers_to_fix)
752{
754 for (image::TileNumber tile_number : tile_numbers_to_fix) {
755 ImBuf *image_buffer = image_data.image_buffers.lookup_default(tile_number, nullptr);
756 if (image_buffer == nullptr) {
757 continue;
758 }
759 const MutableSpan<uint32_t> undo_tile_pushed = image_data.undo_tile_pushed.lookup(tile_number);
760 const MutableSpan<uint8_t> seam_tile_modified = image_data.seam_tile_modified.lookup(
761 tile_number);
762
764 pbvh,
765 image_data.image_buffers,
766 tile_number,
767 seam_tile_modified,
768 [&](const int x_start, const int x_end, const int y) {
769 push_undo_tiles(
770 image_data, tile_number, *image_buffer, undo_tile_pushed, x_start, x_end, y);
771 });
772
773 seam_tile_modified.fill(0);
774 }
775}
776
778 ImageData &image_data,
780 MutableSpan<PixelNode> pixel_nodes,
781 const IndexMask &node_mask)
782{
783 Vector<image::TileNumber> dirty_tiles = collect_dirty_tiles(pixel_nodes, node_mask);
784 fix_non_manifold_seam_bleeding(*bke::object::pbvh_get(ob), image_data, dirty_tiles);
785}
786
788
789void do_3d_image_paint_brush(const Depsgraph &depsgraph,
790 const Paint &paint,
791 const Brush &brush,
792 Object &ob,
793 ImageData &image_data,
794 const IndexMask &node_mask)
795{
797
800 PixelData &pixel_data = *pbvh.pixels_;
801 MutableSpan<PixelNode> pixel_nodes = pixel_data.nodes;
802
803 const bool is_mask_brush = brush.image_brush_type == IMAGE_PAINT_BRUSH_TYPE_MASK;
804 const bool is_inverted = ob.runtime->sculpt_session->cache->toggle_settings.invert;
805 const PaintBlendSettings blend_settings = is_mask_brush ?
806 mask_brush_blend_settings(brush, is_inverted) :
807 PaintBlendSettings(paint, brush, is_inverted);
808
809 node_mask.foreach_index([&](const int i) {
810 fetch_image_buffers(image_data, nodes[i], pixel_nodes[i], is_mask_brush);
811 });
812
813 const Span<float3> positions = bke::pbvh::vert_positions_eval(depsgraph, ob);
814
815 const Mesh &mesh = *id_cast<const Mesh *>(ob.data);
816 const Span<int> corner_verts = mesh.corner_verts();
817 const Span<int3> corner_tris = mesh.corner_tris();
818
819 const bool use_face_sel = (mesh.editflag & ME_EDIT_PAINT_FACE_SEL) != 0;
820 VArraySpan<bool> select_poly;
821 Span<int> corner_tri_faces;
822 if (use_face_sel) {
823 const bke::AttributeAccessor attributes = mesh.attributes();
824 select_poly = *attributes.lookup<bool>(".select_poly", bke::AttrDomain::Face);
825 corner_tri_faces = mesh.corner_tri_faces();
826 }
827
828 node_mask.foreach_index(
829 [&](const int i) {
830 do_paint_pixels(blend_settings,
831 brush,
832 ob,
833 positions,
834 corner_verts,
835 corner_tris,
836 corner_tri_faces,
837 select_poly,
838 image_data,
839 nodes[i],
840 pixel_nodes[i]);
841 },
843
844 fix_non_manifold_seam_bleeding(ob, image_data, nodes, pixel_nodes, node_mask);
845
846 node_mask.foreach_index([&](const int i) {
847 bke::pbvh::pixels::mark_image_dirty(nodes[i], pixel_nodes[i], image_data.image_buffers);
848 });
849}
850} // namespace ed::sculpt_paint::image
851} // namespace blender
A BVH for high poly meshes.
Object is a sort of wrapper for general info.
#define ED_IMAGE_UNDO_TILE_SIZE
Definition ED_paint.hh:116
#define ED_IMAGE_UNDO_TILE_BITS
Definition ED_paint.hh:115
#define PRF_scope(category)
#define BLI_assert_unreachable()
#define POINTER_AS_UINT(i)
#define BLI_INLINE
#define BLI_NOINLINE
#define ELEM(...)
void apply(float *buffer, int width, int height, int channels, bool predivide) const
static ColormanageProcessor colorspace_processor_from_scene_linear_new(const ColorSpace &to_colorspace)
static ColormanageProcessor colorspace_processor_to_scene_linear_new(const ColorSpace &from_colorspace)
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr int64_t start() const
bool add_overwrite(const Key &key, const Value &value)
Definition BLI_map.hh:325
Value lookup_default(const Key &key, const Value &default_value) const
Definition BLI_map.hh:570
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
constexpr T * data() const
Definition BLI_span.hh:545
constexpr void fill(const T &value) const
Definition BLI_span.hh:523
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr bool is_empty() const
Definition BLI_span.hh:264
int64_t size() const
void resize(const int64_t new_size)
GAttributeReader lookup(const StringRef name) const
void foreach_index(Fn &&fn, Mode mode=exec_mode::serial) const
float2x2 invert(float2x2 mat)
const int tile_index
const ccl_global KernelWorkTile * tile
pbvh::Tree * pbvh_get(Object &object)
void collect_dirty_tiles(PixelNode &pixel_node, Vector< image::TileNumber > &r_dirty_tiles)
void copy_pixels(bke::pbvh::Tree &pbvh, Map< image::TileNumber, ImBuf * > &buffers, image::TileNumber tile_number, Span< uint8_t > seam_tiles_modified, FunctionRef< void(int x_start, int x_end, int y)> push_undo_tiles)
void mark_image_dirty(bke::pbvh::Node &node, PixelNode &pixel_node, Map< image::TileNumber, ImBuf * > &buffers)
Span< float3 > vert_positions_eval(const Depsgraph &depsgraph, const Object &object_orig)
Definition pbvh.cc:933
Bounds< T > merge(const Bounds< T > &a, const Bounds< T > &b)
Definition BLI_bounds.hh:27
float3x3 scene_linear_to_rec709
static void do_paint_pixels(const PaintBlendSettings &blend_settings, const Brush &brush, Object &object, Span< float3 > positions_eval, Span< int > corner_verts, Span< int3 > corner_tris, Span< int > corner_tri_faces, Span< bool > select_poly, ImageData &image_data, bke::pbvh::Node &, PixelNode &pixel_node)
static void push_undo_tiles(ImageData &image_data, const image::TileNumber tile_number, ImBuf &image_buffer, MutableSpan< uint32_t > tile_pushed, const int img_x_start, const int img_x_end, const int img_y)
static void paint_blend_pixels(const PaintBlendSettings &settings, const Span< float > factors, const int start, const int size, const MutableSpan< PixelT > image_pixels, const int img_x, const int img_y, const TileColorspaceProcessor &processors, Vector< float4 > &paint_pixels)
static void fix_non_manifold_seam_bleeding(bke::pbvh::Tree &pbvh, ImageData &image_data, Span< TileNumber > tile_numbers_to_fix)
static BitVector init_uv_primitives_brush_test(SculptSession &ss, const Span< int > corner_verts, const Span< int3 > corner_tris, const Span< int > tri_indices, const Span< float3 > positions)
std::variant< std::monostate, MutableSpan< float4 >, MutableSpan< uchar4 > > BufferType
static void fetch_image_buffers(ImageData &image_data, bke::pbvh::Node &, PixelNode &pixel_node, const bool is_mask_brush)
void do_3d_image_paint_brush(const Depsgraph &depsgraph, const Paint &paint, const Brush &brush, Object &ob, ImageData &image_data, const IndexMask &node_mask)
static void calc_pixel_row_positions(const float3 P_start, const float3 P_delta, const MutableSpan< float3 > positions, IndexRange range)
static BLI_NOINLINE void paint_blend_pixels_color_managed(const PaintBlendSettings &settings, const Span< float > factors, const int start, const int size, const MutableSpan< PixelT > image_pixels, const int img_x, const int img_y, const TileColorspaceProcessor &processors, Vector< float4 > &paint_pixels)
static void apply_selection_filter(const Span< int > corner_tri_faces, const Span< int > tri_indices, const Span< bool > select_poly, BitVector<> &brush_test)
static Bounds< int2 > negative_bounds()
BLI_INLINE float4 paint_blend_pixel(const float4 &brush_color, const float brush_alpha, const bool is_mix, const IMB_BlendMode blend_mode, const float factor, const float4 color)
static PaintBlendSettings mask_brush_blend_settings(const Brush &brush, const bool invert)
static void mark_seam_tiles_modified(MutableSpan< uint8_t > mask, const int tiles_x, const int tiles_y, const int x_start, const int x_end, const int y)
void calc_brush_strength_factors(const StrokeCache &cache, const Brush &brush, Span< float > distances, MutableSpan< float > factors)
Definition sculpt.cc:7501
void apply_hardness_to_distances(float radius, float hardness, MutableSpan< float > distances)
Definition sculpt.cc:7473
void filter_distances_with_radius(float radius, Span< float > distances, MutableSpan< float > factors)
Definition sculpt.cc:7422
void scale_factors(MutableSpan< float > factors, float strength)
Definition sculpt.cc:7856
void calc_brush_distances(const SculptSession &ss, Span< float3 > vert_positions, Span< int > vert, eBrushFalloffShape falloff_shape, MutableSpan< float > r_distances)
Definition sculpt.cc:7370
void calc_brush_texture_factors(PaintMode paint_mode, const SculptSession &ss, const Brush &brush, Span< float3 > vert_positions, Span< int > vert, MutableSpan< float > factors)
Definition sculpt.cc:7513
constexpr ParallelGrainSize grain_size(int grain_size)
void min_max(const T &value, T &min, T &max)
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)
VecBase< uint8_t, 4 > uchar4
__int64 int64_t
Definition BKE_text.h:90
MINLINE void rgba_uchar_to_float(float r_col[4], const unsigned char col_ub[4])
unsigned int uint
Definition BKE_text.h:55
MINLINE void blend_color_mix_float(float dst[4], const float src1[4], const float src2[4])
bool IMB_colormanagement_space_is_srgb(const ColorSpace *colorspace)
size_t IMB_get_pixel_count(const ImBuf *ibuf)
Get the length of the data of the given image buffer in pixels.
MINLINE void srgb_to_linearrgb_uchar4(float linear[4], const unsigned char srgb[4])
void IMB_mark_dirty(ImBuf *ibuf)
@ IMB_BLEND_MIX
Definition IMB_imbuf.hh:204
PaintTileMap * ED_image_paint_tile_map_get()
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< int32_t, 3 > int3
BLI_INLINE unsigned int BLI_hash_int(unsigned int k)
Definition BLI_hash_c.hh:89
unsigned char uchar
Definition BKE_text.h:58
@ ME_EDIT_PAINT_FACE_SEL
BLI_INLINE void IMB_colormanagement_rec709_to_scene_linear(float scene_linear[3], const float rec709[3])
@ IMAGE_PAINT_BRUSH_TYPE_MASK
void IMB_blend_color_float(float dst[4], const float src1[4], const float src2[4], IMB_BlendMode mode)
Definition rectop.cc:118
MatBase< float, 3, 3 > float3x3
std::optional< CanvasImageData > BKE_paint_canvas_image_get(const ImagePaintSettings &settings, Object &object)
const ImBuf * ED_image_paint_tile_push(PaintTileMap *paint_tile_map, Image *image, ImBuf *ibuf, ImageUser *iuser, int x_tile, int y_tile, unsigned short **r_mask, bool **r_valid)
float3 BKE_brush_color_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1253
VecBase< float, 3 > float3
bool isect_aabb_aabb_v3(const float min1[3], const float max1[3], const float min2[3], const float max2[3])
void linearrgb_to_srgb_uchar4_n(unsigned char(*srgb)[4], const float(*linear)[4], int size, const float(*matrix)[3]=nullptr)
MINLINE void rgba_float_to_uchar(unsigned char r_col[4], const float col_f[4])
void BKE_image_release_ibuf(Image *ima, ImBuf *ibuf, void *lock)
float3 BKE_brush_secondary_color_get(const Paint *paint, const Brush *brush)
Definition brush.cc:1297
#define hash
Definition noise_c.cc:156
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
bool intersects_segment(const T &start, const T &end) const
bool is_empty() const
const uint8_t * byte_data() const
uint8_t * byte_data_for_write()
Definition allocimbuf.cc:60
const float * float_data() const
float * float_data_for_write()
Definition allocimbuf.cc:79
const c_style_mat & ptr() const
bke::ObjectRuntime * runtime
ed::sculpt_paint::StrokeCache * cache
Definition BKE_paint.hh:409
struct blender::bke::pbvh::pixels::PixelNode::@370321035024016012235212162017206013166215342344 flags
struct blender::bke::pbvh::pixels::PixelNode::@204347010204307204275117222140300103004304270323 uv_primitives
struct blender::bke::pbvh::pixels::UDIMTilePixels::@304110110127162000274075361305170307020300266014 flags
void mark_dirty(const Bounds< int2 > &bounds)
Map< bke::image::TileNumber, Array< uint32_t > > undo_tile_pushed
static std::unique_ptr< ImageData > init_active_image(Object &ob, ImagePaintSettings &settings)
Map< bke::image::TileNumber, BufferType > data_buffers
Map< bke::image::TileNumber, Array< uint8_t > > seam_tile_modified
Map< bke::image::TileNumber, TileColorspaceProcessor > processors
static std::unique_ptr< ImageData > init_mask_image(ImagePaintSettings &settings)
Map< bke::image::TileNumber, ImBuf * > image_buffers
PaintBlendSettings(const Paint &paint, const Brush &brush, const bool invert)
void resize(int new_size)
Definition vector.hh:94