Blender V5.3
mesh_displace.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "device/device.h"
6
8
9#include "scene/mesh.h"
10#include "scene/object.h"
11#include "scene/scene.h"
12#include "scene/shader.h"
13
14#include "util/progress.h"
15
17
18/* Fill in coordinates for mesh displacement shader evaluation on device. */
19static int fill_shader_input(const Scene *scene,
20 const Mesh *mesh,
21 const size_t object_index,
23{
24 int d_input_size = 0;
25 KernelShaderEvalInput *d_input_data = d_input.data();
26
27 const array<int> &mesh_shaders = mesh->get_shader();
28 const array<Node *> &mesh_used_shaders = mesh->get_used_shaders();
29 const int num_verts = mesh->num_verts();
30
31 vector<bool> done(num_verts, false);
32
33 const int num_triangles = mesh->num_triangles();
34 for (int i = 0; i < num_triangles; i++) {
35 const Mesh::Triangle t = mesh->get_triangle(i);
36 const int shader_index = mesh_shaders[i];
37 Shader *shader = (shader_index < mesh_used_shaders.size()) ?
38 static_cast<Shader *>(mesh_used_shaders[shader_index]) :
39 scene->default_surface;
40
41 if (!shader->has_displacement || shader->get_displacement_method() == DISPLACE_BUMP) {
42 continue;
43 }
44
45 for (int j = 0; j < 3; j++) {
46 if (done[t.v[j]]) {
47 continue;
48 }
49
50 done[t.v[j]] = true;
51
52 /* set up object, primitive and barycentric coordinates */
53 const int object = object_index;
54 const int prim = mesh->prim_offset + i;
55 float u;
56 float v;
57
58 switch (j) {
59 case 0:
60 u = 0.0f;
61 v = 0.0f;
62 break;
63 case 1:
64 u = 1.0f;
65 v = 0.0f;
66 break;
67 default:
68 u = 0.0f;
69 v = 1.0f;
70 break;
71 }
72
73 /* back */
75 in.object = object;
76 in.prim = prim;
77 in.u = u;
78 in.v = v;
79 d_input_data[d_input_size++] = in;
80 }
81 }
82
83 return d_input_size;
84}
85
86/* Read back mesh displacement shader output. */
87static void read_shader_output(const Scene *scene,
88 Mesh *mesh,
89 const device_vector<float> &d_output)
90{
91 const array<int> &mesh_shaders = mesh->get_shader();
92 const array<Node *> &mesh_used_shaders = mesh->get_used_shaders();
93 packed_float3 *mesh_verts = mesh->get_position_for_write();
94 const int num_verts = mesh->num_verts();
95 const int num_motion_steps = mesh->get_motion_steps();
96 vector<bool> done(num_verts, false);
97
98 const float *d_output_data = d_output.data();
99 int d_output_index = 0;
100
102 if (!attr_P->has_motion()) {
103 attr_P = nullptr;
104 }
105 const int num_triangles = mesh->num_triangles();
106 for (int i = 0; i < num_triangles; i++) {
107 const Mesh::Triangle t = mesh->get_triangle(i);
108 const int shader_index = mesh_shaders[i];
109 Shader *shader = (shader_index < mesh_used_shaders.size()) ?
110 static_cast<Shader *>(mesh_used_shaders[shader_index]) :
111 scene->default_surface;
112
113 if (!shader->has_displacement || shader->get_displacement_method() == DISPLACE_BUMP) {
114 continue;
115 }
116
117 for (int j = 0; j < 3; j++) {
118 if (!done[t.v[j]]) {
119 done[t.v[j]] = true;
120 float3 off = make_float3(d_output_data[d_output_index + 0],
121 d_output_data[d_output_index + 1],
122 d_output_data[d_output_index + 2]);
123 d_output_index += 3;
124
125 /* Avoid illegal vertex coordinates. */
126 off = ensure_finite(off);
127 mesh_verts[t.v[j]] = float3(mesh_verts[t.v[j]]) + off;
128 if (attr_P != nullptr) {
129 for (int step = 1; step < num_motion_steps; step++) {
131 mP[t.v[j]] = float3(mP[t.v[j]]) + off;
132 }
133 }
134 }
135 }
136 }
137}
138
139/* Compute unnormalized vertex normals by accumulating face normals from the
140 * specified triangles. Vertices not touched by any included triangle are left
141 * at zero. */
142static void compute_vertex_normals(const Mesh *mesh,
143 const packed_float3 *verts_data,
144 const vector<bool> &tri_recompute,
145 vector<float3> &vN)
146{
147 const size_t num_triangles = mesh->num_triangles();
148
149 for (size_t i = 0; i < num_triangles; i++) {
150 if (tri_recompute[i]) {
151 const Mesh::Triangle triangle = mesh->get_triangle(i);
152 for (size_t j = 0; j < 3; j++) {
153 vN[triangle.v[j]] = zero_float3();
154 }
155 }
156 }
157
158 for (size_t i = 0; i < num_triangles; i++) {
159 if (tri_recompute[i]) {
160 const Mesh::Triangle triangle = mesh->get_triangle(i);
161 const float3 fN = triangle.compute_normal(verts_data);
162 for (size_t j = 0; j < 3; j++) {
163 vN[triangle.v[j]] += fN;
164 }
165 }
166 }
167}
168
169/* Store normalized vertex normals into a packed_normal attribute, applying
170 * flip for negative-scaled transforms. Only vertices of included triangles
171 * are written. */
172static void store_vertex_normals(const Mesh *mesh,
173 const vector<float3> &vN_float,
174 const vector<bool> &tri_recompute,
175 const bool flip,
176 packed_normal *vN)
177{
178 const size_t num_verts = mesh->num_verts();
179 vector<bool> done(num_verts, false);
180
181 for (size_t i = 0; i < mesh->num_triangles(); i++) {
182 if (tri_recompute[i]) {
183 const Mesh::Triangle triangle = mesh->get_triangle(i);
184 for (size_t j = 0; j < 3; j++) {
185 const int vert = triangle.v[j];
186 if (done[vert]) {
187 continue;
188 }
189
190 float3 N = safe_normalize(vN_float[vert]);
191 if (flip) {
192 N = -N;
193 }
194 vN[vert] = packed_normal(N);
195 done[vert] = true;
196 }
197 }
198 }
199}
200
201/* Apply vertex normal delta from displacement to a set of corner normals.
202 * For flat shaded triangles, use the new face normal directly. */
203static void apply_corner_normal_delta(const Mesh *mesh,
204 const packed_float3 *verts_data,
205 const vector<float3> &post_vN,
206 const float3 *pre_vN,
207 const vector<bool> &tri_recompute,
208 const bool flip,
209 packed_normal *cN)
210{
211 const bool *smooth = mesh->get_smooth().data();
212
213 for (size_t i = 0; i < mesh->num_triangles(); i++) {
214 if (!tri_recompute[i]) {
215 continue;
216 }
217 const Mesh::Triangle triangle = mesh->get_triangle(i);
218 if (smooth && smooth[i]) {
219 for (size_t j = 0; j < 3; j++) {
220 const int vert = triangle.v[j];
221 float3 post = safe_normalize(post_vN[vert]);
222 if (flip) {
223 post = -post;
224 }
225 const float3 delta = post - pre_vN[vert];
226 cN[i * 3 + j] = packed_normal(safe_normalize(cN[i * 3 + j].decode() + delta));
227 }
228 }
229 else {
230 float3 post_fN = triangle.compute_normal(verts_data);
231 if (flip) {
232 post_fN = -post_fN;
233 }
234 for (size_t j = 0; j < 3; j++) {
235 cN[i * 3 + j] = packed_normal(post_fN);
236 }
237 }
238 }
239}
240
241/* Save pre-displacement vertex normals so we can compute the delta after
242 * displacement and apply it to corner normals. Also saves per motion step. */
243static void save_pre_displacement_normals(const Mesh *mesh,
244 array<float3> &pre_displace_vN,
245 vector<array<float3>> &pre_displace_motion_vN)
246{
247 const size_t num_verts = mesh->num_verts();
248 const size_t num_triangles = mesh->num_triangles();
249 const bool flip = mesh->transform_negative_scaled;
250 const vector<bool> all_tris(num_triangles, true);
251
252 auto compute_normals = [&](const packed_float3 *verts_data) {
253 array<float3> result;
254 result.resize(num_verts, zero_float3());
255 vector<float3> vN(num_verts, zero_float3());
256 compute_vertex_normals(mesh, verts_data, all_tris, vN);
257 for (size_t i = 0; i < num_verts; i++) {
258 float3 N = safe_normalize(vN[i]);
259 if (flip) {
260 N = -N;
261 }
262 result[i] = N;
263 }
264 return result;
265 };
266
267 pre_displace_vN = compute_normals(mesh->get_position());
268
269 const Attribute *attr_P = mesh->attributes.find(ATTR_STD_POSITION);
270 const Attribute *attr_cN = mesh->attributes.find(ATTR_STD_CORNER_NORMAL);
271 if (mesh->has_motion_blur() && attr_P->has_motion() && attr_cN && attr_cN->has_motion()) {
272 const int num_steps = mesh->get_motion_steps() - 1;
273 pre_displace_motion_vN.resize(num_steps);
274 for (int attr_step = 1; attr_step <= num_steps; attr_step++) {
275 const packed_float3 *mP = attr_P->data<packed_float3>(attr_step);
276 pre_displace_motion_vN[attr_step - 1] = compute_normals(mP);
277 }
278 }
279}
280
281/* Update corner normals after displacement, including motion blur steps. */
283 const vector<float3> &vN_float,
284 const array<float3> &pre_displace_vN,
285 const vector<array<float3>> &pre_displace_motion_vN,
286 const vector<bool> &tri_recompute,
287 const bool flip)
288{
289 /* Static corner normals. */
292 mesh->get_position(),
293 vN_float,
294 pre_displace_vN.data(),
295 tri_recompute,
296 flip,
297 attr_cN->data_for_write<packed_normal>());
298
299 /* Motion corner normals. */
301
302 if (mesh->has_motion_blur() && attr_P->has_motion() && attr_cN && attr_cN->has_motion()) {
303 const size_t num_verts = mesh->num_verts();
304
305 for (int attr_step = 1; attr_step < mesh->get_motion_steps(); attr_step++) {
306 const packed_float3 *mP = attr_P->data<packed_float3>(attr_step);
307 packed_normal *mcN = attr_cN->data_for_write<packed_normal>(attr_step);
308
309 vector<float3> mN_float(num_verts, zero_float3());
310 compute_vertex_normals(mesh, mP, tri_recompute, mN_float);
311
313 mP,
314 mN_float,
315 pre_displace_motion_vN[attr_step - 1].data(),
316 tri_recompute,
317 flip,
318 mcN);
319 }
320 }
321}
322
323/* Update vertex normals after displacement, including motion blur steps. */
325 const vector<float3> &vN_float,
326 const vector<bool> &tri_recompute,
327 const bool flip)
328{
329 const size_t num_verts = mesh->num_verts();
330
331 /* Static vertex normals. */
334 mesh, vN_float, tri_recompute, flip, attr_vN->data_for_write<packed_normal>());
335
336 /* Motion vertex normals. */
338
339 if (mesh->has_motion_blur() && attr_P->has_motion() && attr_vN && attr_vN->has_motion()) {
340 for (int attr_step = 1; attr_step < mesh->get_motion_steps(); attr_step++) {
341 const packed_float3 *mP = attr_P->data<packed_float3>(attr_step);
342 packed_normal *mN = attr_vN->data_for_write<packed_normal>(attr_step);
343
344 vector<float3> mN_float(num_verts, zero_float3());
345 compute_vertex_normals(mesh, mP, tri_recompute, mN_float);
346 store_vertex_normals(mesh, mN_float, tri_recompute, flip, mN);
347 }
348 }
349}
350
351bool GeometryManager::displace(Device *device, Scene *scene, Mesh *mesh, Progress &progress)
352{
353 /* verify if we have a displacement shader */
354 if (!mesh->has_true_displacement()) {
355 return false;
356 }
357
358 const size_t num_verts = mesh->num_verts();
359 const size_t num_triangles = mesh->num_triangles();
360
361 if (num_triangles == 0) {
362 return false;
363 }
364
365 /* Corner normals for sharp edges and faces should be preserved, but we can not
366 * individually displace corners as the mesh would break apart. Instead we
367 * compute the delta between vertex normals before and after displacement and
368 * apply the delta to corner normals. */
369 bool need_recompute_vertex_normals = false;
370 bool need_recompute_all_vertex_normals = false;
371
372 const bool has_corner_normals = mesh->attributes.find(ATTR_STD_CORNER_NORMAL) != nullptr;
373 array<float3> pre_displace_vN;
374 vector<array<float3>> pre_displace_motion_vN;
375
376 if (has_corner_normals) {
377 need_recompute_vertex_normals = true;
378 need_recompute_all_vertex_normals = true;
379 save_pre_displacement_normals(mesh, pre_displace_vN, pre_displace_motion_vN);
380 }
381
382 /* Add undisplaced attributes right before doing displacement. */
383 mesh->add_undisplaced(scene);
384
385 const string msg = string_printf("Computing Displacement %s", mesh->name.c_str());
386 progress.set_status("Updating Mesh", msg);
387
388 /* find object index. todo: is arbitrary */
389 size_t object_index = OBJECT_NONE;
390
391 for (size_t i = 0; i < scene->objects.size(); i++) {
392 if (scene->objects[i]->get_geometry() == mesh) {
393 object_index = i;
394 break;
395 }
396 }
397
398 /* Evaluate shader on device. */
399 ShaderEval shader_eval(device, progress);
400 if (!shader_eval.eval(
402 num_verts,
403 3,
404 [scene, mesh, object_index](device_vector<KernelShaderEvalInput> &d_input) {
405 return fill_shader_input(scene, mesh, object_index, d_input);
406 },
407 [scene, mesh](const device_vector<float> &d_output) {
408 read_shader_output(scene, mesh, d_output);
409 }))
410 {
411 return false;
412 }
413
414 /* For displacement method both, we don't need to recompute the vertex normals
415 * as bump mapping in the shader will already alter the vertex normal, so we start
416 * from the non-displaced vertex normals to avoid applying the perturbation twice. */
417 for (Node *node : mesh->get_used_shaders()) {
418 Shader *shader = static_cast<Shader *>(node);
419 if (shader->has_displacement && shader->get_displacement_method() == DISPLACE_TRUE) {
420 need_recompute_vertex_normals = true;
421 break;
422 }
423 }
424
425 if (need_recompute_vertex_normals) {
426 const bool flip = mesh->transform_negative_scaled;
427 vector<bool> tri_recompute(num_triangles, need_recompute_all_vertex_normals);
428
429 if (!need_recompute_all_vertex_normals) {
430 for (size_t i = 0; i < num_triangles; i++) {
431 const int shader_index = mesh->shader[i];
432 Shader *shader = (shader_index < mesh->used_shaders.size()) ?
433 static_cast<Shader *>(mesh->used_shaders[shader_index]) :
434 scene->default_surface;
435
436 tri_recompute[i] = shader->has_displacement &&
437 shader->get_displacement_method() == DISPLACE_TRUE;
438 }
439 }
440
441 vector<float3> vN_float(num_verts, zero_float3());
442 compute_vertex_normals(mesh, mesh->get_position(), tri_recompute, vN_float);
443
444 if (has_corner_normals) {
446 mesh, vN_float, pre_displace_vN, pre_displace_motion_vN, tri_recompute, flip);
447 }
448 else {
449 recompute_displaced_vertex_normals(mesh, vN_float, tri_recompute, flip);
450 }
451 }
452
453 mesh->update_tangents(scene, false);
454
455 return true;
456}
457
Attribute * find(ustring name) const
const T * data(const int step=0) const
T * data_for_write(const int step=0)
bool has_motion() const
bool displace(Device *device, Scene *scene, Mesh *mesh, Progress &progress)
bool has_true_displacement() const
const packed_float3 * get_position() const
size_t prim_offset
AttributeSet attributes
bool transform_negative_scaled
packed_float3 * get_position_for_write()
void add_undisplaced(Scene *scene)
NODE_DECLARE size_t num_verts() const
bool has_motion_blur() const override
size_t num_triangles() const
Definition scene/mesh.h:73
Triangle get_triangle(const size_t i) const
Definition scene/mesh.h:67
void update_tangents(Scene *scene, bool undisplaced)
void set_status(const string &status_, const string &substatus_="")
Definition progress.h:248
Definition scene.h:133
Shader * default_surface
Definition scene.h:178
unique_ptr_vector< Object > objects
Definition scene.h:162
bool eval(const ShaderEvalType type, const int max_num_inputs, const int num_channels, const std::function< int(device_vector< KernelShaderEvalInput > &)> &fill_input, const std::function< void(device_vector< float > &)> &read_output)
bool has_displacement
size_t size() const
size_t size() const
static void fill_shader_input(device_vector< KernelShaderEvalInput > &d_input, const Octree *octree, const Object *object, const Shader *shader, const int resolution)
static void read_shader_output(const device_vector< float > &d_output, const Octree *octree, const int num_channels, const int resolution, vector< Extrema< float > > &sigmas)
#define OBJECT_NONE
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define smooth
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
VecT safe_normalize(VecT vector)
@ ATTR_STD_VERTEX_NORMAL
@ ATTR_STD_POSITION
@ ATTR_STD_CORNER_NORMAL
ccl_device_inline float ensure_finite(const float v)
Definition math_base.h:370
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
#define N
static void save_pre_displacement_normals(const Mesh *mesh, array< float3 > &pre_displace_vN, vector< array< float3 > > &pre_displace_motion_vN)
static void apply_corner_normal_delta(const Mesh *mesh, const packed_float3 *verts_data, const vector< float3 > &post_vN, const float3 *pre_vN, const vector< bool > &tri_recompute, const bool flip, packed_normal *cN)
static void compute_vertex_normals(const Mesh *mesh, const packed_float3 *verts_data, const vector< bool > &tri_recompute, vector< float3 > &vN)
static void recompute_displaced_corner_normals(Mesh *mesh, const vector< float3 > &vN_float, const array< float3 > &pre_displace_vN, const vector< array< float3 > > &pre_displace_motion_vN, const vector< bool > &tri_recompute, const bool flip)
static void recompute_displaced_vertex_normals(Mesh *mesh, const vector< float3 > &vN_float, const vector< bool > &tri_recompute, const bool flip)
static void store_vertex_normals(const Mesh *mesh, const vector< float3 > &vN_float, const vector< bool > &tri_recompute, const bool flip, packed_normal *vN)
@ DISPLACE_BUMP
@ DISPLACE_TRUE
@ SHADER_EVAL_DISPLACE
Definition shader_eval.h:19
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:25
float3 compute_normal(const packed_float3 *verts) const
ustring name
Definition graph/node.h:178
packed_normal()=default