Welcome to mirror list, hosted at ThFree Co, Russian Federation.

extract_mesh_ibo_lines_paint_mask.cc « mesh_extractors « intern « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 713a533492f3175f8d72e18ec15eb5574d9509f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* SPDX-License-Identifier: GPL-2.0-or-later
 * Copyright 2021 Blender Foundation. All rights reserved. */

/** \file
 * \ingroup draw
 */

#include "BLI_bitmap.h"
#include "BLI_vector.hh"
#include "atomic_ops.h"

#include "MEM_guardedalloc.h"

#include "draw_subdivision.h"
#include "extract_mesh.hh"

namespace blender::draw {
/* ---------------------------------------------------------------------- */
/** \name Extract Paint Mask Line Indices
 * \{ */

struct MeshExtract_LinePaintMask_Data {
  GPUIndexBufBuilder elb;
  /** One bit per edge set if face is selected. */
  BLI_bitmap *select_map;
};

static void extract_lines_paint_mask_init(const MeshRenderData *mr,
                                          struct MeshBatchCache *UNUSED(cache),
                                          void *UNUSED(ibo),
                                          void *tls_data)
{
  MeshExtract_LinePaintMask_Data *data = static_cast<MeshExtract_LinePaintMask_Data *>(tls_data);
  data->select_map = BLI_BITMAP_NEW(mr->edge_len, __func__);
  GPU_indexbuf_init(&data->elb, GPU_PRIM_LINES, mr->edge_len, mr->loop_len);
}

static void extract_lines_paint_mask_iter_poly_mesh(const MeshRenderData *mr,
                                                    const MPoly *mp,
                                                    const int UNUSED(mp_index),
                                                    void *_data)
{
  MeshExtract_LinePaintMask_Data *data = static_cast<MeshExtract_LinePaintMask_Data *>(_data);
  const MLoop *mloop = mr->mloop;
  const int ml_index_end = mp->loopstart + mp->totloop;
  for (int ml_index = mp->loopstart; ml_index < ml_index_end; ml_index += 1) {
    const MLoop *ml = &mloop[ml_index];

    const int e_index = ml->e;
    const MEdge *me = &mr->medge[e_index];
    if (!((mr->use_hide && (me->flag & ME_HIDE)) ||
          ((mr->extract_type == MR_EXTRACT_MAPPED) && (mr->e_origindex) &&
           (mr->e_origindex[e_index] == ORIGINDEX_NONE)))) {

      const int ml_index_last = mp->totloop + mp->loopstart - 1;
      const int ml_index_other = (ml_index == ml_index_last) ? mp->loopstart : (ml_index + 1);
      if (mp->flag & ME_FACE_SEL) {
        if (BLI_BITMAP_TEST_AND_SET_ATOMIC(data->select_map, e_index)) {
          /* Hide edge as it has more than 2 selected loop. */
          GPU_indexbuf_set_line_restart(&data->elb, e_index);
        }
        else {
          /* First selected loop. Set edge visible, overwriting any unselected loop. */
          GPU_indexbuf_set_line_verts(&data->elb, e_index, ml_index, ml_index_other);
        }
      }
      else {
        /* Set these unselected loop only if this edge has no other selected loop. */
        if (!BLI_BITMAP_TEST(data->select_map, e_index)) {
          GPU_indexbuf_set_line_verts(&data->elb, e_index, ml_index, ml_index_other);
        }
      }
    }
    else {
      GPU_indexbuf_set_line_restart(&data->elb, e_index);
    }
  }
}

static void extract_lines_paint_mask_finish(const MeshRenderData *UNUSED(mr),
                                            struct MeshBatchCache *UNUSED(cache),
                                            void *buf,
                                            void *_data)
{
  MeshExtract_LinePaintMask_Data *data = static_cast<MeshExtract_LinePaintMask_Data *>(_data);
  GPUIndexBuf *ibo = static_cast<GPUIndexBuf *>(buf);
  GPU_indexbuf_build_in_place(&data->elb, ibo);
  MEM_freeN(data->select_map);
}

static void extract_lines_paint_mask_init_subdiv(const DRWSubdivCache *subdiv_cache,
                                                 const MeshRenderData *mr,
                                                 MeshBatchCache *UNUSED(cache),
                                                 void *UNUSED(buf),
                                                 void *tls_data)
{
  MeshExtract_LinePaintMask_Data *data = static_cast<MeshExtract_LinePaintMask_Data *>(tls_data);
  data->select_map = BLI_BITMAP_NEW(mr->edge_len, __func__);
  GPU_indexbuf_init(&data->elb,
                    GPU_PRIM_LINES,
                    subdiv_cache->num_subdiv_edges,
                    subdiv_cache->num_subdiv_loops * 2);
}

static void extract_lines_paint_mask_iter_subdiv_mesh(const DRWSubdivCache *subdiv_cache,
                                                      const MeshRenderData *mr,
                                                      void *_data,
                                                      uint subdiv_quad_index,
                                                      const MPoly *coarse_quad)
{
  MeshExtract_LinePaintMask_Data *data = static_cast<MeshExtract_LinePaintMask_Data *>(_data);
  int *subdiv_loop_edge_index = (int *)GPU_vertbuf_get_data(subdiv_cache->edges_orig_index);
  int *subdiv_loop_subdiv_edge_index = subdiv_cache->subdiv_loop_subdiv_edge_index;

  uint start_loop_idx = subdiv_quad_index * 4;
  uint end_loop_idx = (subdiv_quad_index + 1) * 4;
  for (uint loop_idx = start_loop_idx; loop_idx < end_loop_idx; loop_idx++) {
    const uint coarse_edge_index = (uint)subdiv_loop_edge_index[loop_idx];
    const uint subdiv_edge_index = (uint)subdiv_loop_subdiv_edge_index[loop_idx];

    if (coarse_edge_index == -1u) {
      GPU_indexbuf_set_line_restart(&data->elb, subdiv_edge_index);
    }
    else {
      const MEdge *me = &mr->medge[coarse_edge_index];
      if (!((mr->use_hide && (me->flag & ME_HIDE)) ||
            ((mr->extract_type == MR_EXTRACT_MAPPED) && (mr->e_origindex) &&
             (mr->e_origindex[coarse_edge_index] == ORIGINDEX_NONE)))) {
        const uint ml_index_other = (loop_idx == end_loop_idx) ? start_loop_idx : loop_idx + 1;
        if (coarse_quad->flag & ME_FACE_SEL) {
          if (BLI_BITMAP_TEST_AND_SET_ATOMIC(data->select_map, coarse_edge_index)) {
            /* Hide edge as it has more than 2 selected loop. */
            GPU_indexbuf_set_line_restart(&data->elb, subdiv_edge_index);
          }
          else {
            /* First selected loop. Set edge visible, overwriting any unselected loop. */
            GPU_indexbuf_set_line_verts(&data->elb, subdiv_edge_index, loop_idx, ml_index_other);
          }
        }
        else {
          /* Set these unselected loop only if this edge has no other selected loop. */
          if (!BLI_BITMAP_TEST(data->select_map, coarse_edge_index)) {
            GPU_indexbuf_set_line_verts(&data->elb, subdiv_edge_index, loop_idx, ml_index_other);
          }
        }
      }
      else {
        GPU_indexbuf_set_line_restart(&data->elb, subdiv_edge_index);
      }
    }
  }
}

static void extract_lines_paint_mask_finish_subdiv(
    const struct DRWSubdivCache *UNUSED(subdiv_cache),
    const MeshRenderData *mr,
    struct MeshBatchCache *cache,
    void *buf,
    void *_data)
{
  extract_lines_paint_mask_finish(mr, cache, buf, _data);
}

constexpr MeshExtract create_extractor_lines_paint_mask()
{
  MeshExtract extractor = {nullptr};
  extractor.init = extract_lines_paint_mask_init;
  extractor.iter_poly_mesh = extract_lines_paint_mask_iter_poly_mesh;
  extractor.finish = extract_lines_paint_mask_finish;
  extractor.init_subdiv = extract_lines_paint_mask_init_subdiv;
  extractor.iter_subdiv_mesh = extract_lines_paint_mask_iter_subdiv_mesh;
  extractor.finish_subdiv = extract_lines_paint_mask_finish_subdiv;
  extractor.data_type = MR_DATA_NONE;
  extractor.data_size = sizeof(MeshExtract_LinePaintMask_Data);
  extractor.use_threading = false;
  extractor.mesh_buffer_offset = offsetof(MeshBufferList, ibo.lines_paint_mask);
  return extractor;
}

/** \} */

}  // namespace blender::draw

const MeshExtract extract_lines_paint_mask = blender::draw::create_extractor_lines_paint_mask();