Blender V5.3
extract_mesh_vbo_edituv_stretch_angle.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
10
11#include "BKE_attribute.hh"
12#include "BKE_mesh.hh"
13
14#include "extract_mesh.hh"
15
16#include "draw_subdivision.hh"
17
18namespace blender::draw {
19
25
28 const float2 *uv;
29 float auv[2][2], last_auv[2];
30 float av[2][3], last_av[3];
31 int cd_ofs;
32};
33
34static void compute_normalize_edge_vectors(float auv[2][2],
35 float av[2][3],
36 const float uv[2],
37 const float uv_prev[2],
38 const float co[3],
39 const float co_prev[3])
40{
41 /* Move previous edge. */
42 copy_v2_v2(auv[0], auv[1]);
43 copy_v3_v3(av[0], av[1]);
44 /* 2d edge */
45 sub_v2_v2v2(auv[1], uv_prev, uv);
46 normalize_v2(auv[1]);
47 /* 3d edge */
48 sub_v3_v3v3(av[1], co_prev, co);
49 normalize_v3(av[1]);
50}
51
52static short v2_to_short_angle(const float v[2])
53{
54 return atan2f(v[1], v[0]) * float(M_1_PI) * SHRT_MAX;
55}
56
57static void edituv_get_edituv_stretch_angle(float auv[2][2],
58 const float av[2][3],
59 UVStretchAngle *r_stretch)
60{
61 /* Send UVs to the shader and let it compute the aspect corrected angle. */
62 r_stretch->uv_angles[0] = v2_to_short_angle(auv[0]);
63 r_stretch->uv_angles[1] = v2_to_short_angle(auv[1]);
64 /* Compute 3D angle here. */
65 r_stretch->angle = angle_normalized_v3v3(av[0], av[1]) * float(M_1_PI);
66
67#if 0 /* here for reference, this is done in shader now. */
68 float uvang = angle_normalized_v2v2(auv0, auv1);
69 float ang = angle_normalized_v3v3(av0, av1);
70 float stretch = fabsf(uvang - ang) / float(M_PI);
71 return 1.0f - pow2f(1.0f - stretch);
72#endif
73}
74
77{
78 const BMesh &bm = *mr.bm;
79 const StringRef active_name = mr.mesh->active_uv_map_name();
80 const int uv_offset = CustomData_get_offset_named(&bm.ldata, CD_PROP_FLOAT2, active_name);
81
82 float auv[2][2], last_auv[2];
83 float av[2][3], last_av[3];
84
85 const BMFace *face;
86 BMIter f_iter;
87 BM_ITER_MESH (face, &f_iter, &const_cast<BMesh &>(bm), BM_FACES_OF_MESH) {
88 BMLoop *l_iter, *l_first;
89 l_iter = l_first = BM_FACE_FIRST_LOOP(face);
90 do {
91 const int l_index = BM_elem_index_get(l_iter);
92
93 const float (*luv)[2], (*luv_next)[2];
94 BMLoop *l_next = l_iter->next;
95 if (l_iter == BM_FACE_FIRST_LOOP(face)) {
96 /* First loop in face. */
97 BMLoop *l_tmp = l_iter->prev;
98 BMLoop *l_next_tmp = l_iter;
99 luv = BM_ELEM_CD_GET_FLOAT2_P(l_tmp, uv_offset);
100 luv_next = BM_ELEM_CD_GET_FLOAT2_P(l_next_tmp, uv_offset);
102 av,
103 *luv,
104 *luv_next,
105 bm_vert_co_get(mr, l_tmp->v),
106 bm_vert_co_get(mr, l_next_tmp->v));
107 /* Save last edge. */
108 copy_v2_v2(last_auv, auv[1]);
109 copy_v3_v3(last_av, av[1]);
110 }
111 if (l_next == BM_FACE_FIRST_LOOP(face)) {
112 /* Move previous edge. */
113 copy_v2_v2(auv[0], auv[1]);
114 copy_v3_v3(av[0], av[1]);
115 /* Copy already calculated last edge. */
116 copy_v2_v2(auv[1], last_auv);
117 copy_v3_v3(av[1], last_av);
118 }
119 else {
120 luv = BM_ELEM_CD_GET_FLOAT2_P(l_iter, uv_offset);
121 luv_next = BM_ELEM_CD_GET_FLOAT2_P(l_next, uv_offset);
123 av,
124 *luv,
125 *luv_next,
126 bm_vert_co_get(mr, l_iter->v),
127 bm_vert_co_get(mr, l_next->v));
128 }
129 edituv_get_edituv_stretch_angle(auv, av, &vbo_data[l_index]);
130 } while ((l_iter = l_iter->next) != l_first);
131 }
132}
133
136{
137 const Span<float3> positions = mr.vert_positions;
138 const OffsetIndices faces = mr.faces;
139 const Span<int> corner_verts = mr.corner_verts;
140 const Mesh &mesh = *mr.mesh;
141 const bke::AttributeAccessor attributes = mesh.attributes();
142 const StringRef name = mesh.active_uv_map_name();
143 const VArraySpan uv_map = *attributes.lookup<float2>(name, bke::AttrDomain::Corner);
144
145 float auv[2][2], last_auv[2];
146 float av[2][3], last_av[3];
147
148 for (const int face_index : faces.index_range()) {
149 const IndexRange face = faces[face_index];
150 const int corner_end = face.start() + face.size();
151 for (int corner = face.start(); corner < corner_end; corner += 1) {
152 int l_next = corner + 1;
153 if (corner == face.start()) {
154 /* First loop in face. */
155 const int corner_last = corner_end - 1;
156 const int l_next_tmp = face.start();
158 av,
159 uv_map[corner_last],
160 uv_map[l_next_tmp],
161 positions[corner_verts[corner_last]],
162 positions[corner_verts[l_next_tmp]]);
163 /* Save last edge. */
164 copy_v2_v2(last_auv, auv[1]);
165 copy_v3_v3(last_av, av[1]);
166 }
167 if (l_next == corner_end) {
168 l_next = face.start();
169 /* Move previous edge. */
170 copy_v2_v2(auv[0], auv[1]);
171 copy_v3_v3(av[0], av[1]);
172 /* Copy already calculated last edge. */
173 copy_v2_v2(auv[1], last_auv);
174 copy_v3_v3(av[1], last_av);
175 }
176 else {
178 av,
179 uv_map[corner],
180 uv_map[l_next],
181 positions[corner_verts[corner]],
182 positions[corner_verts[l_next]]);
183 }
184 edituv_get_edituv_stretch_angle(auv, av, &vbo_data[corner]);
185 }
186 }
187}
188
190{
191 static const GPUVertFormat format = []() {
192 GPUVertFormat format{};
193 /* Waning: adjust #UVStretchAngle struct accordingly. */
194 GPU_vertformat_attr_add(&format, "angle", gpu::VertAttrType::SFLOAT_32);
195 GPU_vertformat_attr_add(&format, "uv_angles", gpu::VertAttrType::SNORM_16_16);
196 return format;
197 }();
198
199 gpu::VertBufPtr vbo = gpu::VertBufPtr(GPU_vertbuf_create_with_format(format));
201 MutableSpan vbo_data = vbo->data<UVStretchAngle>();
202
204 extract_uv_stretch_angle_bm(mr, vbo_data);
205 }
206 else {
207 extract_uv_stretch_angle_mesh(mr, vbo_data);
208 }
209 return vbo;
210}
211
213{
214 static const GPUVertFormat format = []() {
215 GPUVertFormat format{};
216 /* Waning: adjust #UVStretchAngle struct accordingly. */
217 GPU_vertformat_attr_add(&format, "angle", gpu::VertAttrType::SFLOAT_32);
218 GPU_vertformat_attr_add(&format, "uv_angles", gpu::VertAttrType::SFLOAT_32_32);
219 return format;
220 }();
221 return format;
222}
223
225 const DRWSubdivCache &subdiv_cache,
226 const MeshBatchCache &cache)
227{
230
231 gpu::VertBuf *pos = cache.final.buff.vbos.lookup(VBOType::Position).get();
232 gpu::VertBuf *uvs = cache.final.buff.vbos.lookup(VBOType::UVs).get();
233
234 /* It may happen that the data for the UV editor is requested before (as a separate draw update)
235 * the data for the mesh when switching to the `UV Editing` workspace, and therefore the position
236 * buffer might not be created yet. In this case, create a buffer it locally, the subdivision
237 * data should already be evaluated if we are here. This can happen if the subsurf modifier is
238 * only enabled in edit-mode. See #96338. */
239 if (!pos) {
240 pos = GPU_vertbuf_calloc();
241 static const GPUVertFormat pos_format = GPU_vertformat_from_attribute(
242 "pos", gpu::VertAttrType::SFLOAT_32_32_32);
243 GPU_vertbuf_init_build_on_device(*pos, pos_format, subdiv_full_vbo_size(mr, subdiv_cache));
244 draw_subdiv_extract_pos(subdiv_cache, pos, nullptr);
245 }
246
247 /* UVs are stored contiguously so we need to compute the offset in the UVs buffer for the active
248 * UV layer. */
249
250 VectorSet<std::string> uv_layers = cache.cd_used.uv;
251 /* HACK to fix #68857 */
252 if (mr.extract_type == MeshExtractType::BMesh && cache.cd_used.edit_uv == 1) {
253 const StringRef active_name = mr.mesh->active_uv_map_name();
254 if (!active_name.is_empty()) {
255 uv_layers.add_as(active_name);
256 }
257 }
258
259 int uvs_offset = uv_layers.index_of(mr.mesh->active_uv_map_name());
260
261 /* The data is at `offset * num loops`, and we have 2 values per index. */
262 uvs_offset *= subdiv_cache.num_subdiv_loops * 2;
263
264 draw_subdiv_build_edituv_stretch_angle_buffer(subdiv_cache, pos, uvs, uvs_offset, vbo.get());
265 return vbo;
266}
267
268} // namespace blender::draw
#define M_1_PI
#define M_PI
#define BLI_STATIC_ASSERT_ALIGN(st, align)
#define BM_ELEM_CD_GET_FLOAT2_P(ele, offset)
#define BM_FACE_FIRST_LOOP(p)
#define BM_elem_index_get(ele)
#define BM_ITER_MESH(ele, iter, bm, itype)
constexpr int64_t size() const
constexpr int64_t start() const
constexpr T * data() const
Definition BLI_span.hh:545
bool add_as(ForwardKey &&key)
int64_t index_of(const Key &key) const
GAttributeReader lookup(const StringRef name) const
constexpr bool is_empty() const
Extraction of Mesh data into VBO to feed to GPU.
#define SHRT_MAX
static void compute_normalize_edge_vectors(float auv[2][2], float av[2][3], const float uv[2], const float uv_prev[2], const float co[3], const float co_prev[3])
gpu::VertBufPtr extract_edituv_stretch_angle_subdiv(const MeshRenderData &mr, const DRWSubdivCache &subdiv_cache, const MeshBatchCache &cache)
static void extract_uv_stretch_angle_bm(const MeshRenderData &mr, MutableSpan< UVStretchAngle > vbo_data)
static const GPUVertFormat & get_edituv_stretch_angle_format_subdiv()
static short v2_to_short_angle(const float v[2])
static void extract_uv_stretch_angle_mesh(const MeshRenderData &mr, MutableSpan< UVStretchAngle > vbo_data)
void draw_subdiv_extract_pos(const DRWSubdivCache &cache, gpu::VertBuf *pos, gpu::VertBuf *orco)
static void edituv_get_edituv_stretch_angle(float auv[2][2], const float av[2][3], UVStretchAngle *r_stretch)
int subdiv_full_vbo_size(const MeshRenderData &mr, const DRWSubdivCache &cache)
void draw_subdiv_build_edituv_stretch_angle_buffer(const DRWSubdivCache &cache, gpu::VertBuf *pos, gpu::VertBuf *uvs, int uvs_offset, gpu::VertBuf *stretch_angles)
gpu::VertBufPtr extract_edituv_stretch_angle(const MeshRenderData &mr)
BLI_INLINE const float * bm_vert_co_get(const MeshRenderData &mr, const BMVert *eve)
std::unique_ptr< gpu::VertBuf, gpu::VertBufDeleter > VertBufPtr
float angle_normalized_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void copy_v2_v2(float r[2], const float a[2])
gpu::VertBuf * GPU_vertbuf_create_on_device(const GPUVertFormat &format, uint v_len)
GPUVertFormat GPU_vertformat_from_attribute(StringRef name, gpu::VertAttrType type)
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
MINLINE float normalize_v2(float n[2])
ATTR_WARN_UNUSED_RESULT const BMVert * v
MINLINE float pow2f(float x)
float angle_normalized_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
VecBase< float, 2 > float2
gpu::VertBuf * GPU_vertbuf_calloc()
int CustomData_get_offset_named(const CustomData *data, eCustomDataType type, StringRef name)
void GPU_vertbuf_init_build_on_device(gpu::VertBuf &verts, const GPUVertFormat &format, uint v_len)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
MINLINE float normalize_v3(float n[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
signed short int16_t
Definition BKE_text.h:77
#define fabsf
#define atan2f
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct BMLoop * next
struct BMLoop * prev
struct BMVert * v
CustomData ldata
VectorSet< std::string > uv
Map< VBOType, std::unique_ptr< gpu::VertBuf, gpu::VertBufDeleter > > vbos
OffsetIndices< int > faces