[Bf-blender-cvs] [74b19336af2] temp-3d-texturing-brush-b: Split up pixel extraction in multiple files.

Jeroen Bakker noreply at git.blender.org
Wed Mar 9 13:19:02 CET 2022


Commit: 74b19336af2391996b9c0d79292b73951487b76d
Author: Jeroen Bakker
Date:   Wed Mar 9 13:18:53 2022 +0100
Branches: temp-3d-texturing-brush-b
https://developer.blender.org/rB74b19336af2391996b9c0d79292b73951487b76d

Split up pixel extraction in multiple files.

===================================================================

M	source/blender/editors/sculpt_paint/CMakeLists.txt
M	source/blender/editors/sculpt_paint/sculpt_intern.h
M	source/blender/editors/sculpt_paint/sculpt_texture_paint_b.cc
A	source/blender/editors/sculpt_paint/sculpt_texture_paint_intern.hh
A	source/blender/editors/sculpt_paint/sculpt_texture_paint_pixel_extraction_a.cc
A	source/blender/editors/sculpt_paint/sculpt_texture_paint_pixel_extraction_b.cc

===================================================================

diff --git a/source/blender/editors/sculpt_paint/CMakeLists.txt b/source/blender/editors/sculpt_paint/CMakeLists.txt
index 55c90214ac1..7f044ca405c 100644
--- a/source/blender/editors/sculpt_paint/CMakeLists.txt
+++ b/source/blender/editors/sculpt_paint/CMakeLists.txt
@@ -66,6 +66,7 @@ set(SRC
   sculpt_pose.c
   sculpt_smooth.c
   sculpt_texture_paint_b.cc
+  sculpt_texture_paint_pixel_extraction_b.cc
   sculpt_transform.c
   sculpt_undo.c
   sculpt_uv.c
diff --git a/source/blender/editors/sculpt_paint/sculpt_intern.h b/source/blender/editors/sculpt_paint/sculpt_intern.h
index 93bb8fb4fe3..1776235e590 100644
--- a/source/blender/editors/sculpt_paint/sculpt_intern.h
+++ b/source/blender/editors/sculpt_paint/sculpt_intern.h
@@ -1616,6 +1616,7 @@ void SCULPT_do_paint_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode
 void SCULPT_do_texture_paint_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode);
 void SCULPT_init_texture_paint(Object *ob);
 void SCULPT_flush_texture_paint(Object *ob);
+void SCULPT_extract_pixels(Object *ob, PBVHNode **nodes, int totnode);
 
 /* Smear Brush. */
 void SCULPT_do_smear_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode);
diff --git a/source/blender/editors/sculpt_paint/sculpt_texture_paint_b.cc b/source/blender/editors/sculpt_paint/sculpt_texture_paint_b.cc
index 81e5767cf6b..7bb27b65803 100644
--- a/source/blender/editors/sculpt_paint/sculpt_texture_paint_b.cc
+++ b/source/blender/editors/sculpt_paint/sculpt_texture_paint_b.cc
@@ -34,259 +34,9 @@
 #include "ED_uvedit.h"
 
 #include "sculpt_intern.h"
+#include "sculpt_texture_paint_intern.hh"
 
 namespace blender::ed::sculpt_paint::texture_paint {
-
-struct PixelData {
-  struct {
-    bool dirty : 1;
-  } flags;
-  int2 pixel_pos;
-  float3 local_pos;
-  float4 content;
-};
-
-struct NodeData {
-  struct {
-    bool dirty : 1;
-  } flags;
-
-  Vector<PixelData> pixels;
-  rcti dirty_region;
-  rctf uv_region;
-
-  NodeData()
-  {
-    flags.dirty = false;
-    BLI_rcti_init_minmax(&dirty_region);
-  }
-
-  void init_pixels_rasterization(Object *ob, PBVHNode *node, ImBuf *image_buffer);
-
-  void flush(ImBuf &image_buffer)
-  {
-    flags.dirty = false;
-    for (PixelData &pixel : pixels) {
-      if (pixel.flags.dirty) {
-        const int pixel_offset = (pixel.pixel_pos[1] * image_buffer.x + pixel.pixel_pos[0]);
-        copy_v4_v4(&image_buffer.rect_float[pixel_offset * 4], pixel.content);
-        pixel.flags.dirty = false;
-      }
-    }
-  }
-
-  void mark_region(Image &image, ImBuf &image_buffer)
-  {
-    printf("%s", __func__);
-    print_rcti_id(&dirty_region);
-    BKE_image_partial_update_mark_region(
-        &image, static_cast<ImageTile *>(image.tiles.first), &image_buffer, &dirty_region);
-    BLI_rcti_init_minmax(&dirty_region);
-  }
-
-  static void free_func(void *instance)
-  {
-    NodeData *node_data = static_cast<NodeData *>(instance);
-    MEM_delete(node_data);
-  }
-};
-
-struct TexturePaintingUserData {
-  Object *ob;
-  Brush *brush;
-  PBVHNode **nodes;
-};
-
-namespace rasterization {
-
-using namespace imbuf::rasterizer;
-
-struct VertexInput {
-  float3 pos;
-  float2 uv;
-
-  VertexInput(float3 pos, float2 uv) : pos(pos), uv(uv)
-  {
-  }
-};
-
-class VertexShader : public AbstractVertexShader<VertexInput, float3> {
- public:
-  float2 image_size;
-  void vertex(const VertexInputType &input, VertexOutputType *r_output) override
-  {
-    r_output->coord = input.uv * image_size;
-    r_output->data = input.pos;
-  }
-};
-
-struct FragmentOutput {
-  float3 local_pos;
-};
-
-class FragmentShader : public AbstractFragmentShader<float3, FragmentOutput> {
- public:
-  ImBuf *image_buffer;
-
- public:
-  void fragment(const FragmentInputType &input, FragmentOutputType *r_output) override
-  {
-    r_output->local_pos = input;
-  }
-};
-
-struct NodeDataPair {
-  ImBuf *image_buffer;
-  NodeData *node_data;
-
-  struct {
-    /* Rasterizer doesn't support glCoord yet, so for now we just store them in a runtime section.
-     */
-    int2 last_known_pixel_pos;
-  } runtime;
-};
-
-class AddPixel : public AbstractBlendMode<FragmentOutput, NodeDataPair> {
- public:
-  void blend(NodeDataPair *dest, const FragmentOutput &source) const override
-  {
-    PixelData new_pixel;
-    new_pixel.local_pos = source.local_pos;
-    new_pixel.pixel_pos = dest->runtime.last_known_pixel_pos;
-    const int pixel_offset = new_pixel.pixel_pos[1] * dest->image_buffer->x +
-                             new_pixel.pixel_pos[0];
-    new_pixel.content = float4(dest->image_buffer->rect_float[pixel_offset * 4]);
-    new_pixel.flags.dirty = false;
-
-    dest->node_data->pixels.append(new_pixel);
-    dest->runtime.last_known_pixel_pos[0] += 1;
-  }
-};
-
-class NodeDataDrawingTarget : public AbstractDrawingTarget<NodeDataPair, NodeDataPair> {
- private:
-  NodeDataPair *active_ = nullptr;
-
- public:
-  uint64_t get_width() const
-  {
-    return active_->image_buffer->x;
-  }
-  uint64_t get_height() const
-  {
-    return active_->image_buffer->y;
-  };
-  NodeDataPair *get_pixel_ptr(uint64_t x, uint64_t y)
-  {
-    active_->runtime.last_known_pixel_pos = int2(x, y);
-    return active_;
-  };
-  int64_t get_pixel_stride() const
-  {
-    return 0;
-  };
-  bool has_active_target() const
-  {
-    return active_ != nullptr;
-  }
-  void activate(NodeDataPair *instance)
-  {
-    active_ = instance;
-  };
-  void deactivate()
-  {
-    active_ = nullptr;
-  }
-};
-
-using RasterizerType = Rasterizer<VertexShader, FragmentShader, AddPixel, NodeDataDrawingTarget>;
-
-static void init_rasterization_task_cb_ex(void *__restrict userdata,
-                                          const int n,
-                                          const TaskParallelTLS *__restrict UNUSED(tls))
-{
-  TexturePaintingUserData *data = static_cast<TexturePaintingUserData *>(userdata);
-  Object *ob = data->ob;
-  SculptSession *ss = ob->sculpt;
-  PBVHNode *node = data->nodes[n];
-
-  NodeData *node_data = static_cast<NodeData *>(BKE_pbvh_node_texture_paint_data_get(node));
-  // TODO: reinit when texturing on different image?
-  if (node_data != nullptr) {
-    return;
-  }
-
-  TIMEIT_START(init_texture_paint_for_node);
-  node_data = MEM_new<NodeData>(__func__);
-  node_data->init_pixels_rasterization(ob, node, ss->mode.texture_paint.drawing_target);
-  BKE_pbvh_node_texture_paint_data_set(node, node_data, NodeData::free_func);
-  TIMEIT_END(init_texture_paint_for_node);
-}
-
-static void init_using_rasterization(Object *ob, int totnode, PBVHNode **nodes)
-{
-  TIMEIT_START(init_using_rasterization);
-  TexturePaintingUserData data = {nullptr};
-  data.ob = ob;
-  data.nodes = nodes;
-
-  TaskParallelSettings settings;
-  BKE_pbvh_parallel_range_settings(&settings, true, totnode);
-
-  BLI_task_parallel_range(0, totnode, &data, init_rasterization_task_cb_ex, &settings);
-  TIMEIT_END(init_using_rasterization);
-}
-
-}  // namespace rasterization
-void NodeData::init_pixels_rasterization(Object *ob, PBVHNode *node, ImBuf *image_buffer)
-{
-  using namespace rasterization;
-  Mesh *mesh = static_cast<Mesh *>(ob->data);
-  MLoopUV *ldata_uv = static_cast<MLoopUV *>(CustomData_get_layer(&mesh->ldata, CD_MLOOPUV));
-  if (ldata_uv == nullptr) {
-    return;
-  }
-
-  RasterizerType rasterizer;
-  NodeDataPair node_data_pair;
-  rasterizer.vertex_shader().image_size = float2(image_buffer->x, image_buffer->y);
-  rasterizer.fragment_shader().image_buffer = image_buffer;
-  node_data_pair.node_data = this;
-  node_data_pair.image_buffer = image_buffer;
-  rasterizer.activate_drawing_target(&node_data_pair);
-
-  SculptSession *ss = ob->sculpt;
-  MVert *mvert = SCULPT_mesh_deformed_mverts_get(ss);
-
-  PBVHVertexIter vd;
-  BKE_pbvh_vertex_iter_begin (ss->pbvh, node, vd, PBVH_ITER_UNIQUE) {
-    MeshElemMap *vert_map = &ss->pmap[vd.index];
-    for (int j = 0; j < ss->pmap[vd.index].count; j++) {
-      const MPoly *p = &ss->mpoly[vert_map->indices[j]];
-      if (p->totloop < 3) {
-        continue;
-      }
-
-      const MLoop *loopstart = &ss->mloop[p->loopstart];
-      for (int triangle = 0; triangle < p->totloop - 2; triangle++) {
-        const int v1_index = loopstart[0].v;
-        const int v2_index = loopstart[triangle + 1].v;
-        const int v3_index = loopstart[triangle + 2].v;
-        const int v1_loop_index = p->loopstart;
-        const int v2_loop_index = p->loopstart + triangle + 1;
-        const int v3_loop_index = p->loopstart + triangle + 2;
-
-        VertexInput v1(mvert[v1_index].co, ldata_uv[v1_loop_index].uv);
-        VertexInput v2(mvert[v2_index].co, ldata_uv[v2_loop_index].uv);
-        VertexInput v3(mvert[v3_index].co, ldata_uv[v3_loop_index].uv);
-        rasterizer.draw_triangle(v1, v2, v3);
-      }
-    }
-  }
-  BKE_pbvh_vertex_iter_end;
-  rasterizer.deactivate_drawing_target();
-}
-
 namespace painting {
 static void do_task_cb_ex(void *__restrict userdata,
                           const int n,
@@ -324,239 +74,6 @@ static void do_task_cb_ex(void *__restrict userdata,
 }
 }  // namespace painting
 
-struct BucketEntry {
-  PBVHNode *node;
-  const MPoly *poly;
-  rctf uv_bounds;
-};
-struct Bucket {
-  static const int Size = 16;
-  Vector<BucketEntry> entries;
-  rctf bounds;
-};
-
-static bool init_using_intersection(SculptSession *ss,
-                                    Bucket &bucket,
-                                    ImBuf *image_buffer,
-                                    MVert *mvert,
-                                    MLoopUV *ldata_uv,
-                                    float2 uv,
-                                    int2 xy)
-{
-  const int pixel_offset = xy[1] * image_buffer->x + xy[0];
-  for (BucketEntry &entry : bucket.entries) {
-    if (!BLI_rctf_isect_pt_v(&entry.uv_bounds, uv)) {
-      continue;
-    }
-    const MPoly *p = entry.poly;
-
-    const MLoop *loopstart = &ss->mloop[p->loopstart];
-    for (int triangle = 0; triangle < p->totloop - 2; triangle++) {
-      const int v1_loop_index = p->loopstart;
-      const int v2_loop_index = p->loopstart + triangle + 1;
-      const int v3_loop_index = p->loopstart + triangle + 2;
-      const float2 v1_uv = ldata_uv[v1_loop_index].uv;
-  

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list