Blender V5.3
extract_mesh_vbo_edituv_stretch_area.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
9#include "BLI_math_geom_c.hh"
11
12#include "BKE_attribute.hh"
13#include "BKE_mesh.hh"
14
15#include "extract_mesh.hh"
16
17#include "draw_subdivision.hh"
18
19namespace blender::draw {
20
21BLI_INLINE float area_ratio_get(float area, float uvarea)
22{
23 if (area >= FLT_EPSILON && uvarea >= FLT_EPSILON) {
24 return uvarea / area;
25 }
26 return 0.0f;
27}
28
29BLI_INLINE float area_ratio_to_stretch(float ratio, float tot_ratio)
30{
31 ratio *= tot_ratio;
32 return (ratio > 1.0f) ? (1.0f / ratio) : ratio;
33}
34
35struct AreaInfo {
36 float tot_area = 0.0f;
37 float tot_uv_area = 0.0f;
38};
40{
42 const BMesh &bm = *mr.bm;
43 const StringRef active_name = mr.mesh->active_uv_map_name();
44 const int uv_offset = CustomData_get_offset_named(&bm.ldata, CD_PROP_FLOAT2, active_name);
47 1024,
48 AreaInfo{},
49 [&](const IndexRange range, AreaInfo info) {
50 for (const int face_index : range) {
51 const BMFace &face = *BM_face_at_index(&const_cast<BMesh &>(bm), face_index);
52 const float area = BM_face_calc_area(&face);
53 const float uvarea = BM_face_calc_area_uv(&face, uv_offset);
54 info.tot_area += area;
55 info.tot_uv_area += uvarea;
56 r_area_ratio[face_index] = area_ratio_get(area, uvarea);
57 }
58 return info;
59 },
60 [](const AreaInfo &a, const AreaInfo &b) {
61 return AreaInfo{a.tot_area + b.tot_area, a.tot_uv_area + b.tot_uv_area};
62 });
63 }
64
65 const Span<float3> positions = mr.vert_positions;
66 const OffsetIndices<int> faces = mr.faces;
67 const Span<int> corner_verts = mr.corner_verts;
68 const Mesh &mesh = *mr.mesh;
69 const bke::AttributeAccessor attributes = mesh.attributes();
70 const StringRef name = mesh.active_uv_map_name();
71 const VArraySpan uv_map = *attributes.lookup<float2>(name, bke::AttrDomain::Corner);
72
74 faces.index_range(),
75 1024,
76 AreaInfo{},
77 [&](const IndexRange range, AreaInfo info) {
78 for (const int face_index : range) {
79 const IndexRange face = faces[face_index];
80 const float area = bke::mesh::face_area_calc(positions, corner_verts.slice(face));
81 float uvarea = area_poly_v2(reinterpret_cast<const float (*)[2]>(&uv_map[face.start()]),
82 face.size());
83 info.tot_area += area;
84 info.tot_uv_area += uvarea;
85 r_area_ratio[face_index] = area_ratio_get(area, uvarea);
86 }
87 return info;
88 },
89 [](const AreaInfo &a, const AreaInfo &b) {
90 return AreaInfo{a.tot_area + b.tot_area, a.tot_uv_area + b.tot_uv_area};
91 });
92}
93
95 float &tot_area,
96 float &tot_uv_area)
97{
98 Array<float> area_ratio(mr.faces_num);
99 const AreaInfo info = compute_area_ratio(mr, area_ratio);
100 tot_area = info.tot_area;
101 tot_uv_area = info.tot_uv_area;
102
103 static const GPUVertFormat format = GPU_vertformat_from_attribute("ratio",
104 gpu::VertAttrType::SFLOAT_32);
105 gpu::VertBufPtr vbo = gpu::VertBufPtr(GPU_vertbuf_create_with_format(format));
107 MutableSpan<float> vbo_data = vbo->data<float>();
108
109 const int64_t bytes = area_ratio.as_span().size_in_bytes() + vbo_data.size_in_bytes();
112 const BMesh &bm = *mr.bm;
113 threading::parallel_for(IndexRange(bm.totface), 2048, [&](const IndexRange range) {
114 for (const int face_index : range) {
115 const BMFace &face = *BM_face_at_index(&const_cast<BMesh &>(bm), face_index);
116 const IndexRange face_range(BM_elem_index_get(BM_FACE_FIRST_LOOP(&face)), face.len);
117 vbo_data.slice(face_range).fill(area_ratio[face_index]);
118 }
119 });
120 }
121 else {
123 const OffsetIndices<int> faces = mr.faces;
124 threading::parallel_for(faces.index_range(), 2048, [&](const IndexRange range) {
125 for (const int face : range) {
126 vbo_data.slice(faces[face]).fill(area_ratio[face]);
127 }
128 });
129 }
130 });
131 return vbo;
132}
133
135 const DRWSubdivCache &subdiv_cache,
136 float &tot_area,
137 float &tot_uv_area)
138{
139 static const GPUVertFormat format = GPU_vertformat_from_attribute("ratio",
140 gpu::VertAttrType::SFLOAT_32);
142 GPU_vertbuf_create_on_device(format, subdiv_cache.num_subdiv_loops));
143
144 gpu::VertBuf *coarse_vbo = GPU_vertbuf_calloc();
145 GPU_vertbuf_init_with_format(*coarse_vbo, format);
146 GPU_vertbuf_data_alloc(*coarse_vbo, mr.faces_num);
147 MutableSpan coarse_vbo_data = coarse_vbo->data<float>();
148 const AreaInfo info = compute_area_ratio(mr, coarse_vbo_data);
149 tot_area = info.tot_area;
150 tot_uv_area = info.tot_uv_area;
151
152 draw_subdiv_build_edituv_stretch_area_buffer(subdiv_cache, coarse_vbo, vbo.get());
153
154 GPU_vertbuf_discard(coarse_vbo);
155 return vbo;
156}
157
158} // namespace blender::draw
#define GPU_vertbuf_init_with_format(verts, format)
#define BLI_INLINE
Span< T > as_span() const
Definition BLI_array.hh:254
constexpr IndexRange()=default
constexpr T * data() const
Definition BLI_span.hh:545
constexpr int64_t size_in_bytes() const
Definition BLI_span.hh:507
GAttributeReader lookup(const StringRef name) const
MutableSpan< T > data()
Extraction of Mesh data into VBO to feed to GPU.
#define FLT_EPSILON
BLI_INLINE float area_ratio_to_stretch(float ratio, float tot_ratio)
void draw_subdiv_build_edituv_stretch_area_buffer(const DRWSubdivCache &cache, gpu::VertBuf *coarse_data, gpu::VertBuf *subdiv_data)
gpu::VertBufPtr extract_edituv_stretch_area(const MeshRenderData &mr, float &tot_area, float &tot_uv_area)
static AreaInfo compute_area_ratio(const MeshRenderData &mr, MutableSpan< float > r_area_ratio)
gpu::VertBufPtr extract_edituv_stretch_area_subdiv(const MeshRenderData &mr, const DRWSubdivCache &subdiv_cache, float &tot_area, float &tot_uv_area)
BLI_INLINE float area_ratio_get(float area, float uvarea)
std::unique_ptr< gpu::VertBuf, gpu::VertBufDeleter > VertBufPtr
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
void memory_bandwidth_bound_task(const int64_t approximate_bytes_touched, const Function &function)
Definition BLI_task.hh:251
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
gpu::VertBuf * GPU_vertbuf_create_on_device(const GPUVertFormat &format, uint v_len)
GPUVertFormat GPU_vertformat_from_attribute(StringRef name, gpu::VertAttrType type)
VecBase< float, 2 > float2
gpu::VertBuf * GPU_vertbuf_calloc()
int CustomData_get_offset_named(const CustomData *data, eCustomDataType type, StringRef name)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
void GPU_vertbuf_discard(gpu::VertBuf *)
CustomData ldata
OffsetIndices< int > faces