Blender V5.3
extract_mesh_vbo_tan.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 <climits>
10
11#include "BLI_string.hh"
12
14
15#include "BKE_attribute.hh"
16#include "BKE_editmesh.hh"
18#include "BKE_mesh.hh"
19#include "BKE_mesh_tangent.hh"
20
21#include "extract_mesh.hh"
22
23#include "draw_subdivision.hh"
24
25namespace blender::draw {
26
28 const MeshBatchCache &cache,
29 GPUVertFormat *format,
30 gpu::VertAttrType gpu_attr_type)
31{
33
34 VectorSet<std::string> tan_layers = cache.cd_used.tan;
35 bool use_orco_tan = cache.cd_used.tan_orco;
36
37 const StringRef active_name = mr.mesh->active_uv_map_name();
38 const StringRef default_name = mr.mesh->default_uv_map_name();
39
40 /* FIXME(#91838): This is to avoid a crash when orco tangent was requested but there are valid
41 * uv layers. It would be better to fix the root cause. */
42 if (tan_layers.is_empty() && use_orco_tan && !default_name.is_empty()) {
43 tan_layers.add(default_name);
44 use_orco_tan = false;
45 }
46
47 if (use_orco_tan) {
48 Array<float4> tangents;
51 BKE_mesh_orco_verts_transform(const_cast<Mesh *>(mr.mesh), positions, false);
53 mr.bm, mr.edit_bmesh->looptris, mr.bm_face_normals, mr.bm_loop_normals, positions);
54 }
55 else {
56 Span<float3> orco;
57 Array<float3> orco_allocated;
58 if (const float3 *orco_ptr = static_cast<const float3 *>(
60 {
61 orco = Span(orco_ptr, mr.verts_num);
62 }
63 else {
64 orco_allocated = mr.vert_positions;
65 /* TODO: This is not thread-safe. Draw extraction should not modify the mesh. */
66 BKE_mesh_orco_verts_transform(const_cast<Mesh *>(mr.mesh), orco_allocated, false);
67 orco = orco_allocated;
68 }
70 mr.faces,
71 mr.corner_verts,
72 mr.mesh->corner_tris(),
73 mr.mesh->corner_tri_faces(),
74 mr.sharp_faces,
75 mr.mesh->vert_normals(),
76 mr.face_normals,
78 orco);
79 }
80
81 char attr_name[32], attr_safe_name[GPU_MAX_SAFE_ATTR_NAME];
82 GPU_vertformat_safe_attr_name("orco", attr_safe_name, GPU_MAX_SAFE_ATTR_NAME);
83 SNPRINTF(attr_name, "t%s", attr_safe_name);
84 GPU_vertformat_attr_add(format, attr_name, gpu_attr_type);
85 GPU_vertformat_alias_add(format, "t");
86 GPU_vertformat_alias_add(format, "at");
87
88 return {std::move(tangents)};
89 }
90
91 Vector<StringRef> uv_names;
92 for (const StringRef name : tan_layers.as_span().take_front(MAX_MTFACE)) {
93 if (tan_layers.contains(name)) {
94 char attr_name[32], attr_safe_name[GPU_MAX_SAFE_ATTR_NAME];
95 GPU_vertformat_safe_attr_name(name, attr_safe_name, GPU_MAX_SAFE_ATTR_NAME);
96 /* Tangent layer name. */
97 SNPRINTF(attr_name, "t%s", attr_safe_name);
98 GPU_vertformat_attr_add(format, attr_name, gpu_attr_type);
99 /* Active render layer name. */
100 if (name == default_name) {
101 GPU_vertformat_alias_add(format, "t");
102 }
103 /* Active display layer name. */
104 if (name == active_name) {
105 GPU_vertformat_alias_add(format, "at");
106 }
107
108 uv_names.append(name);
109 }
110 }
111
112 if (uv_names.is_empty()) {
113 GPU_vertformat_attr_add(format, "dummy", gpu::VertAttrType::SFLOAT_32);
114 return {};
115 }
116
117 Array<Array<float4>> results;
120 mr.bm, mr.edit_bmesh->looptris, mr.bm_face_normals, mr.bm_loop_normals, uv_names);
121 }
122 else {
123 Array<VArraySpan<float2>> uv_maps(uv_names.size());
124 Array<Span<float2>> uv_map_spans(uv_names.size());
125 const bke::AttributeAccessor attributes = mr.mesh->attributes();
126 for (const int i : uv_names.index_range()) {
127 uv_maps[i] = *attributes.lookup<float2>(uv_names[i], bke::AttrDomain::Corner);
128 uv_map_spans[i] = uv_maps[i];
129 }
131 mr.faces,
132 mr.corner_verts,
133 mr.mesh->corner_tris(),
134 mr.mesh->corner_tri_faces(),
135 mr.sharp_faces,
136 mr.mesh->vert_normals(),
137 mr.face_normals,
139 uv_map_spans);
140 }
141
142 if (format->attr_len == 0) {
143 GPU_vertformat_attr_add(format, "dummy", gpu::VertAttrType::SFLOAT_32);
144 }
145
146 return results;
147}
148
150 const MeshBatchCache &cache,
151 const bool use_hq)
152{
153 gpu::VertAttrType gpu_attr_type = use_hq ? gpu::VertAttrType::SNORM_16_16_16_16 :
154 gpu::VertAttrType::SNORM_10_10_10_2;
155
156 GPUVertFormat format = {0};
157 const Array<Array<float4>> tangents = extract_tan_init_common(mr, cache, &format, gpu_attr_type);
158
159 gpu::VertBufPtr vbo = gpu::VertBufPtr(GPU_vertbuf_create_with_format(format));
161
162 if (use_hq) {
163 MutableSpan tan_data = vbo->data<short4>();
164 int vbo_index = 0;
165 for (const int i : tangents.index_range()) {
166 const Span<float4> layer_data = tangents[i];
167 for (int corner = 0; corner < mr.corners_num; corner++) {
168 tan_data[vbo_index] = gpu::convert_normal<short4>(float3(layer_data[corner]));
169 tan_data[vbo_index].w = (layer_data[corner][3] > 0.0f) ? SHRT_MAX : SHRT_MIN;
170 vbo_index++;
171 }
172 }
173 BLI_assert(vbo_index == tan_data.size());
174 }
175 else {
176 MutableSpan tan_data = vbo->data<int1010102_norm>();
177 int vbo_index = 0;
178 for (const int i : tangents.index_range()) {
179 const Span<float4> layer_data = tangents[i];
180 for (int corner = 0; corner < mr.corners_num; corner++) {
181 tan_data[vbo_index] = gpu::convert_normal<int1010102_norm>(float3(layer_data[corner]));
182 tan_data[vbo_index].w = (layer_data[corner][3] > 0.0f) ? 1 : -2;
183 vbo_index++;
184 }
185 }
186 BLI_assert(vbo_index == tan_data.size());
187 }
188
189 return vbo;
190}
191
193{
195 "tan", gpu::VertAttrType::SFLOAT_32_32_32_32);
196 return format;
197}
198
200 const DRWSubdivCache &subdiv_cache,
201 const MeshBatchCache &cache)
202{
203 gpu::VertAttrType gpu_attr_type = gpu::VertAttrType::SFLOAT_32_32_32_32;
204 GPUVertFormat format = {0};
205 const Array<Array<float4>> tangents = extract_tan_init_common(mr, cache, &format, gpu_attr_type);
206
208 GPU_vertbuf_create_on_device(format, subdiv_cache.num_subdiv_loops));
209
210 gpu::VertBuf *coarse_vbo = GPU_vertbuf_calloc();
211 /* Dynamic as we upload and interpolate layers one at a time. */
213 GPU_vertbuf_data_alloc(*coarse_vbo, mr.corners_num);
214
215 /* Index of the tangent layer in the compact buffer. Used layers are stored in a single buffer.
216 */
217 int pack_layer_index = 0;
218 for (const int i : tangents.index_range()) {
219 float4 *tan_data = coarse_vbo->data<float4>().data();
220 const Span<float4> values = tangents[i];
221 for (int corner = 0; corner < mr.corners_num; corner++) {
222 *tan_data = values[corner];
223 (*tan_data)[3] = (values[corner][3] > 0.0f) ? 1.0f : -1.0f;
224 tan_data++;
225 }
226
227 /* Ensure data is uploaded properly. */
228 GPU_vertbuf_tag_dirty(coarse_vbo);
229 /* Include stride in offset. */
230 const int dst_offset = int(subdiv_cache.num_subdiv_loops) * 4 * pack_layer_index++;
231 draw_subdiv_interp_custom_data(subdiv_cache, *coarse_vbo, *vbo, GPU_COMP_F32, 4, dst_offset);
232 }
233
234 GPU_vertbuf_discard(coarse_vbo);
235 return vbo;
236}
237
238} // namespace blender::draw
#define SNPRINTF(dst, format,...)
#define MAX_MTFACE
IndexRange index_range() const
Definition BLI_array.hh:371
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr T * data() const
Definition BLI_span.hh:545
constexpr Span()=default
bool add(const Key &key)
bool contains(const Key &key) const
Span< Key > as_span() const
int64_t size() const
void append(const T &value)
bool is_empty() const
IndexRange index_range() const
GAttributeReader lookup(const StringRef name) const
constexpr bool is_empty() const
MutableSpan< T > data()
Extraction of Mesh data into VBO to feed to GPU.
#define SHRT_MAX
Array< float4 > calc_orco_tangents(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, Span< int3 > corner_tris, Span< int > corner_tri_faces, Span< bool > sharp_faces, Span< float3 > vert_normals, Span< float3 > face_normals, Span< float3 > corner_normals, Span< float3 > vert_orco)
Array< Array< float4 > > calc_uv_tangents(Span< float3 > vert_positions, OffsetIndices< int > faces, Span< int > corner_verts, Span< int3 > corner_tris, Span< int > corner_tri_faces, Span< bool > sharp_faces, Span< float3 > vert_normals, Span< float3 > face_normals, Span< float3 > corner_normals, Span< Span< float2 > > uv_maps)
gpu::VertBufPtr extract_tangents_subdiv(const MeshRenderData &mr, const DRWSubdivCache &subdiv_cache, const MeshBatchCache &cache)
void draw_subdiv_interp_custom_data(const DRWSubdivCache &cache, gpu::VertBuf &src_data, gpu::VertBuf &dst_data, GPUVertCompType comp_type, int dimensions, int dst_offset)
gpu::VertBufPtr extract_tangents(const MeshRenderData &mr, const MeshBatchCache &cache, bool use_hq)
static Array< Array< float4 > > extract_tan_init_common(const MeshRenderData &mr, const MeshBatchCache &cache, GPUVertFormat *format, gpu::VertAttrType gpu_attr_type)
static const GPUVertFormat & get_coarse_tan_format()
GPUType convert_normal(const float3 &src)
std::unique_ptr< gpu::VertBuf, gpu::VertBufDeleter > VertBufPtr
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void GPU_vertbuf_tag_dirty(gpu::VertBuf *verts)
Array< Array< float4 > > BKE_editmesh_uv_tangents_calc(BMesh *bm, Span< std::array< BMLoop *, 3 > > looptris, Span< float3 > face_normals, Span< float3 > corner_normals, Span< StringRef > uv_names)
void GPU_vertformat_alias_add(GPUVertFormat *, StringRef alias)
void GPU_vertbuf_init_with_format_ex(gpu::VertBuf &verts, const GPUVertFormat &format, GPUUsageType)
NormalizedIntVecBase< int32_t, 4, 10, 10, 10, 2 > int1010102_norm
gpu::VertBuf * GPU_vertbuf_create_on_device(const GPUVertFormat &format, uint v_len)
void GPU_vertformat_deinterleave(GPUVertFormat *format)
GPUVertFormat GPU_vertformat_from_attribute(StringRef name, gpu::VertAttrType type)
VecBase< float, 4 > float4
Array< float4 > BKE_editmesh_orco_tangents_calc(BMesh *bm, Span< std::array< BMLoop *, 3 > > looptris, Span< float3 > face_normals, Span< float3 > corner_normals, Span< float3 > vert_orco)
VecBase< int16_t, 4 > short4
VecBase< float, 2 > float2
void GPU_vertformat_safe_attr_name(StringRef attr_name, char *r_safe_name, uint max_len)
BMesh const char void * data
gpu::VertBuf * GPU_vertbuf_calloc()
Array< float3 > BM_mesh_vert_coords_alloc(const BMesh *bm)
const void * CustomData_get_layer(const CustomData *data, eCustomDataType type)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
void BKE_mesh_orco_verts_transform(Mesh *mesh, MutableSpan< float3 > orco, bool invert)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
VecBase< float, 3 > float3
void GPU_vertbuf_discard(gpu::VertBuf *)
Array< std::array< BMLoop *, 3 > > looptris
CustomData vert_data
VectorSet< std::string > tan
VArraySpan< bool > sharp_faces
OffsetIndices< int > faces