Blender V5.3
extract_mesh_vbo_lnor.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 "extract_mesh.hh"
12
13#include "draw_subdivision.hh"
14
15namespace blender::draw {
16
18{
19 return mr.use_simplify_normals ? mr.mesh->vert_normals_true() : mr.mesh->vert_normals();
20}
21
22template<typename GPUType>
24{
26}
27
28template<typename GPUType>
30{
31 const BMesh &bm = *mr.bm;
32 threading::parallel_for(IndexRange(bm.totvert), 4096, [&](const IndexRange range) {
33 for (const int i : range) {
34 const BMVert *vert = BM_vert_at_index(&const_cast<BMesh &>(bm), i);
35 vert_data[i] = gpu::convert_normal<GPUType>(bm_vert_no_get(mr, vert));
36 }
37 });
38}
39
40template<typename GPUType>
42{
44}
45
46template<typename GPUType>
48{
49 const BMesh &bm = *mr.bm;
50 threading::parallel_for(IndexRange(bm.totface), 4096, [&](const IndexRange range) {
51 for (const int i : range) {
52 const BMFace *face = BM_face_at_index(&const_cast<BMesh &>(bm), i);
53 face_data[i] = gpu::convert_normal<GPUType>(bm_face_no_get(mr, face));
54 }
55 });
56}
57
58template<typename GPUType>
60{
61 if (!mr.corner_normals.is_empty()) {
63 return;
64 }
65 /* The corner normals span can still be empty with the "simplify" option. That forces
66 * mixed face and vertex normals. */
67 const OffsetIndices faces = mr.faces;
68 const Span<int> corner_verts = mr.corner_verts;
69 const Span<bool> sharp_faces = mr.sharp_faces;
70 const Span<float3> vert_normals = get_vert_normals(mr);
71 const Span<float3> face_normals = mr.face_normals;
72 threading::parallel_for(faces.index_range(), 2048, [&](const IndexRange range) {
73 for (const int face : range) {
74 if (!sharp_faces.is_empty() && sharp_faces[face]) {
75 normals.slice(faces[face]).fill(gpu::convert_normal<GPUType>(face_normals[face]));
76 }
77 else {
78 for (const int corner : faces[face]) {
79 normals[corner] = gpu::convert_normal<GPUType>(vert_normals[corner_verts[corner]]);
80 }
81 }
82 }
83 });
84}
85
86template<typename GPUType>
88{
89 const BMesh &bm = *mr.bm;
90 if (mr.bm_free_normal_offset_corner != -1) {
91 threading::parallel_for(IndexRange(bm.totface), 2048, [&](const IndexRange range) {
92 for (const int face_index : range) {
93 const BMFace &face = *BM_face_at_index(&const_cast<BMesh &>(bm), face_index);
94 const BMLoop *loop = BM_FACE_FIRST_LOOP(&face);
95 const IndexRange face_range(BM_elem_index_get(loop), face.len);
96 for (const int corner : face_range) {
97 normals[corner] = gpu::convert_normal<GPUType>(
98 BM_ELEM_CD_GET_FLOAT_P(loop, mr.bm_free_normal_offset_corner));
99 loop = loop->next;
100 }
101 }
102 });
103 return;
104 }
105 if (!mr.bm_loop_normals.is_empty()) {
106 gpu::convert_normals(mr.bm_loop_normals, normals);
107 return;
108 }
109 threading::parallel_for(IndexRange(bm.totface), 2048, [&](const IndexRange range) {
110 for (const int face_index : range) {
111 const BMFace &face = *BM_face_at_index(&const_cast<BMesh &>(bm), face_index);
112 const BMLoop *loop = BM_FACE_FIRST_LOOP(&face);
113 const IndexRange face_range(BM_elem_index_get(loop), face.len);
114 if (!BM_elem_flag_test(&face, BM_ELEM_SMOOTH)) {
115 normals.slice(face_range).fill(gpu::convert_normal<GPUType>(bm_face_no_get(mr, &face)));
116 }
117 else {
118 for (const int corner : face_range) {
119 normals[corner] = gpu::convert_normal<GPUType>(bm_vert_no_get(mr, loop->v));
120 loop = loop->next;
121 }
122 }
123 }
124 });
125}
126
127template<typename GPUType>
129 MeshBufferCache &cache,
130 const GPUVertFormat &format)
131{
132 const bool is_mesh = mr.extract_type == MeshExtractType::Mesh;
133 const int size = mr.corners_num + mr.loose_indices_num;
134 const Array<GPUType> loose_data(mr.loose_indices_num, GPUType{});
135
136 switch (mr.normals_domain) {
138 gpu::VertBufPtr vert_vbo = gpu::VertBufPtr(GPU_vertbuf_create_with_format(format));
139 GPU_vertbuf_data_alloc(*vert_vbo, mr.verts_num);
140 if (is_mesh) {
141 extract_vert_normals_mesh(mr, vert_vbo->data<GPUType>());
142 }
143 else {
144 extract_vert_normals_bm(mr, vert_vbo->data<GPUType>());
145 }
147 gather_vert_to_corner_gpu(mr, cache, *vert_vbo, *vbo);
148 loose_data_upload(mr, loose_data.as_span(), *vbo);
149 return vbo;
150 }
152 gpu::VertBufPtr face_vbo = gpu::VertBufPtr(GPU_vertbuf_create_with_format(format));
153 GPU_vertbuf_data_alloc(*face_vbo, mr.faces_num);
154 if (is_mesh) {
155 extract_face_normals_mesh(mr, face_vbo->data<GPUType>());
156 }
157 else {
158 extract_face_normals_bm(mr, face_vbo->data<GPUType>());
159 }
161 scatter_face_to_corner_gpu(mr, cache, *face_vbo, *vbo);
162 loose_data_upload(mr, loose_data.as_span(), *vbo);
163 return vbo;
164 }
166 gpu::VertBufPtr vbo = gpu::VertBufPtr(GPU_vertbuf_create_with_format(format));
167 GPU_vertbuf_data_alloc(*vbo, size);
168 MutableSpan vbo_data = vbo->data<GPUType>();
169 if (is_mesh) {
171 }
172 else {
174 }
175 vbo_data.take_back(mr.loose_indices_num).copy_from(loose_data);
176 return vbo;
177 }
178 }
180 return {};
181}
182
184 MeshBufferCache &cache,
185 const bool use_hq)
186{
187 if (use_hq) {
188 static const GPUVertFormat format = []() {
189 GPUVertFormat format{};
190 GPU_vertformat_attr_add(&format, "nor", gpu::VertAttrType::SNORM_16_16_16_16);
191 GPU_vertformat_alias_add(&format, "lnor");
192 GPU_vertformat_alias_add(&format, "vnor");
193 return format;
194 }();
195 return extract_normals<short4>(mr, cache, format);
196 }
197 static const GPUVertFormat format = []() {
198 GPUVertFormat format{};
199 GPU_vertformat_attr_add(&format, "nor", gpu::VertAttrType::SNORM_10_10_10_2);
200 GPU_vertformat_alias_add(&format, "lnor");
201 GPU_vertformat_alias_add(&format, "vnor");
202 return format;
203 }();
204 return extract_normals<int1010102_norm>(mr, cache, format);
205}
206
208{
209 static const GPUVertFormat format = []() {
210 GPUVertFormat format{};
211 GPU_vertformat_attr_add(&format, "nor", gpu::VertAttrType::SFLOAT_32_32_32);
212 GPU_vertformat_alias_add(&format, "lnor");
213 GPU_vertformat_alias_add(&format, "vnor");
214 return format;
215 }();
216 return format;
217}
218
220 const DRWSubdivCache &subdiv_cache,
221 gpu::VertBuf &lnor)
222{
223 const int vbo_size = subdiv_full_vbo_size(mr, subdiv_cache);
224 const int loose_geom_start = subdiv_cache.num_subdiv_loops;
225
226 /* Push VBO content to the GPU and bind the VBO so that #GPU_vertbuf_update_sub can work. */
227 GPU_vertbuf_use(&lnor);
228
229 /* Default to zeroed attribute. The overlay shader should expect this and render engines should
230 * never draw loose geometry. */
231 const float3 default_normal(0.0f, 0.0f, 0.0f);
232 for (const int i : IndexRange::from_begin_end(loose_geom_start, vbo_size)) {
233 /* TODO(fclem): This has HORRENDOUS performance. Prefer clearing the buffer on device with
234 * something like glClearBufferSubData. */
235 GPU_vertbuf_update_sub(&lnor, i * sizeof(float3), sizeof(float3), &default_normal);
236 }
237}
238
240 const DRWSubdivCache &subdiv_cache,
241 gpu::VertBuf &pos)
242{
243 const int vbo_size = subdiv_full_vbo_size(mr, subdiv_cache);
244
247 if (subdiv_cache.num_subdiv_loops == 0) {
248 update_loose_normals(mr, subdiv_cache, *lnor);
249 return lnor;
250 }
251
252 if (subdiv_cache.use_custom_loop_normals) {
253 const Mesh *coarse_mesh = subdiv_cache.mesh;
254 static GPUVertFormat src_normals_format = GPU_vertformat_from_attribute(
255 "vnor", gpu::VertAttrType::SFLOAT_32_32_32);
256 gpu::VertBufPtr src = gpu::VertBufPtr(GPU_vertbuf_create_with_format(src_normals_format));
257 GPU_vertbuf_data_alloc(*src, coarse_mesh->corners_num);
258 src->data<float3>().copy_from(coarse_mesh->corner_normals());
259 draw_subdiv_interp_corner_normals(subdiv_cache, *src, *lnor);
260
261 update_loose_normals(mr, subdiv_cache, *lnor);
262 return lnor;
263 }
264
266 subdiv_cache.subdiv_loop_subdiv_vert_index, subdiv_cache.num_subdiv_loops));
267
268 /* Calculate vertex normals (stored here per subdivided vertex rather than per subdivided face
269 * corner). The values are used for smooth shaded faces later. */
270 static GPUVertFormat vert_normals_format = GPU_vertformat_from_attribute(
271 "vnor", gpu::VertAttrType::SFLOAT_32_32_32);
272 gpu::VertBufPtr vert_normals = gpu::VertBufPtr(
273 GPU_vertbuf_create_on_device(vert_normals_format, subdiv_cache.num_subdiv_verts));
275 &pos,
277 subdiv_cache.subdiv_vert_face_adjacency,
278 subdiv_corner_verts.get(),
279 vert_normals.get());
280
281 /* Compute final normals for face corners, either using the vertex normal corresponding to the
282 * corner, or by calculating the face normal.
283 *
284 * TODO: Avoid using face normals or vertex normals if possible, using `mr.normals_domain`. */
286 subdiv_cache, &pos, vert_normals.get(), subdiv_corner_verts.get(), lnor.get());
287
288 update_loose_normals(mr, subdiv_cache, *lnor);
289
290 return lnor;
291}
292
293} // namespace blender::draw
#define BLI_assert_unreachable()
Span< T > as_span() const
Definition BLI_array.hh:254
static constexpr IndexRange from_begin_end(const int64_t begin, const int64_t end)
constexpr IndexRange()=default
constexpr MutableSpan take_back(const int64_t n) const
Definition BLI_span.hh:646
constexpr T * data() const
Definition BLI_span.hh:545
constexpr void copy_from(Span< T > values) const
Definition BLI_span.hh:745
constexpr MutableSpan take_front(const int64_t n) const
Definition BLI_span.hh:635
Extraction of Mesh data into VBO to feed to GPU.
void scatter_face_to_corner_gpu(const MeshRenderData &mr, MeshBufferCache &cache, gpu::VertBuf &src, gpu::VertBuf &dst)
void draw_subdiv_build_lnor_buffer(const DRWSubdivCache &cache, gpu::VertBuf *pos, gpu::VertBuf *vert_normals, gpu::VertBuf *subdiv_corner_verts, gpu::VertBuf *lnor)
void draw_subdiv_interp_corner_normals(const DRWSubdivCache &cache, gpu::VertBuf &src_data, gpu::VertBuf &dst_data)
gpu::VertBufPtr extract_normals(const MeshRenderData &mr, MeshBufferCache &cache, bool use_hq)
static void extract_vert_normals_mesh(const MeshRenderData &mr, MutableSpan< GPUType > vert_data)
static void extract_vert_normals_bm(const MeshRenderData &mr, MutableSpan< GPUType > vert_data)
int subdiv_full_vbo_size(const MeshRenderData &mr, const DRWSubdivCache &cache)
static void extract_corner_normals_bm(const MeshRenderData &mr, MutableSpan< GPUType > normals)
static void update_loose_normals(const MeshRenderData &mr, const DRWSubdivCache &subdiv_cache, gpu::VertBuf &lnor)
static void extract_face_normals_bm(const MeshRenderData &mr, MutableSpan< GPUType > face_data)
static void extract_face_normals_mesh(const MeshRenderData &mr, MutableSpan< GPUType > face_data)
static Span< float3 > get_vert_normals(const MeshRenderData &mr)
void gather_vert_to_corner_gpu(const MeshRenderData &mr, MeshBufferCache &cache, gpu::VertBuf &src, gpu::VertBuf &dst)
static const GPUVertFormat & get_normals_format()
gpu::VertBuf * draw_subdiv_build_origindex_buffer(int *vert_origindex, uint num_loops)
static void extract_corner_normals_mesh(const MeshRenderData &mr, MutableSpan< GPUType > normals)
gpu::VertBufPtr extract_normals_subdiv(const MeshRenderData &mr, const DRWSubdivCache &subdiv_cache, gpu::VertBuf &pos)
void draw_subdiv_accumulate_normals(const DRWSubdivCache &cache, gpu::VertBuf *pos, gpu::VertBuf *face_adjacency_offsets, gpu::VertBuf *face_adjacency_lists, gpu::VertBuf *vert_loop_map, gpu::VertBuf *vert_normals)
void loose_data_upload(const MeshRenderData &mr, const Span< T > loose_data, gpu::VertBuf &dst)
void convert_normals(Span< float3 > src, MutableSpan< GPUType > dst)
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 GPU_vertformat_alias_add(GPUVertFormat *, StringRef alias)
gpu::VertBuf * GPU_vertbuf_create_on_device(const GPUVertFormat &format, uint v_len)
GPUVertFormat GPU_vertformat_from_attribute(StringRef name, gpu::VertAttrType type)
void GPU_vertbuf_use(gpu::VertBuf *)
void GPU_vertbuf_update_sub(gpu::VertBuf *verts, uint start, uint len, const void *data)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
VecBase< float, 3 > float3
gpu::VertBuf * subdiv_vert_face_adjacency_offsets
VArraySpan< bool > sharp_faces
OffsetIndices< int > faces
bke::MeshNormalDomain normals_domain
ccl_device_inline bool is_mesh(const ccl_global KernelLightTreeEmitter *kemitter)
Definition tree.h:74