Blender V4.3
geometry_mesh.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 "bvh/bvh.h"
6#include "bvh/bvh2.h"
7
8#include "device/device.h"
9
10#include "scene/attribute.h"
11#include "scene/camera.h"
12#include "scene/geometry.h"
13#include "scene/hair.h"
14#include "scene/light.h"
15#include "scene/mesh.h"
16#include "scene/object.h"
17#include "scene/osl.h"
18#include "scene/pointcloud.h"
19#include "scene/scene.h"
20#include "scene/shader.h"
21#include "scene/shader_nodes.h"
22#include "scene/stats.h"
23#include "scene/volume.h"
24
25#include "subd/patch_table.h"
26#include "subd/split.h"
27
28#ifdef WITH_OSL
29# include "kernel/osl/globals.h"
30#endif
31
32#include "util/foreach.h"
33#include "util/log.h"
34#include "util/progress.h"
35#include "util/task.h"
36
38
40 DeviceScene *dscene,
41 Scene *scene,
42 Progress &progress)
43{
44 /* Count. */
45 size_t vert_size = 0;
46 size_t tri_size = 0;
47
48 size_t curve_key_size = 0;
49 size_t curve_size = 0;
50 size_t curve_segment_size = 0;
51
52 size_t point_size = 0;
53
54 size_t patch_size = 0;
55
56 foreach (Geometry *geom, scene->geometry) {
58 Mesh *mesh = static_cast<Mesh *>(geom);
59
60 vert_size += mesh->verts.size();
61 tri_size += mesh->num_triangles();
62
63 if (mesh->get_num_subd_faces()) {
64 Mesh::SubdFace last = mesh->get_subd_face(mesh->get_num_subd_faces() - 1);
65 patch_size += (last.ptex_offset + last.num_ptex_faces()) * 8;
66
67 /* patch tables are stored in same array so include them in patch_size */
68 if (mesh->patch_table) {
69 mesh->patch_table_offset = patch_size;
70 patch_size += mesh->patch_table->total_size();
71 }
72 }
73 }
74 else if (geom->is_hair()) {
75 Hair *hair = static_cast<Hair *>(geom);
76
77 curve_key_size += hair->get_curve_keys().size();
78 curve_size += hair->num_curves();
79 curve_segment_size += hair->num_segments();
80 }
81 else if (geom->is_pointcloud()) {
82 PointCloud *pointcloud = static_cast<PointCloud *>(geom);
83 point_size += pointcloud->num_points();
84 }
85 }
86
87 /* Fill in all the arrays. */
88 if (tri_size != 0) {
89 /* normals */
90 progress.set_status("Updating Mesh", "Computing normals");
91
92 packed_float3 *tri_verts = dscene->tri_verts.alloc(vert_size);
93 uint *tri_shader = dscene->tri_shader.alloc(tri_size);
94 packed_float3 *vnormal = dscene->tri_vnormal.alloc(vert_size);
95 packed_uint3 *tri_vindex = dscene->tri_vindex.alloc(tri_size);
96 uint *tri_patch = dscene->tri_patch.alloc(tri_size);
97 float2 *tri_patch_uv = dscene->tri_patch_uv.alloc(vert_size);
98
99 const bool copy_all_data = dscene->tri_shader.need_realloc() ||
100 dscene->tri_vindex.need_realloc() ||
101 dscene->tri_vnormal.need_realloc() ||
102 dscene->tri_patch.need_realloc() ||
103 dscene->tri_patch_uv.need_realloc();
104
105 foreach (Geometry *geom, scene->geometry) {
107 Mesh *mesh = static_cast<Mesh *>(geom);
108
109 if (mesh->shader_is_modified() || mesh->smooth_is_modified() ||
110 mesh->triangles_is_modified() || copy_all_data)
111 {
112 mesh->pack_shaders(scene, &tri_shader[mesh->prim_offset]);
113 }
114
115 if (mesh->verts_is_modified() || copy_all_data) {
116 mesh->pack_normals(&vnormal[mesh->vert_offset]);
117 }
118
119 if (mesh->verts_is_modified() || mesh->triangles_is_modified() ||
120 mesh->vert_patch_uv_is_modified() || copy_all_data)
121 {
122 mesh->pack_verts(&tri_verts[mesh->vert_offset],
123 &tri_vindex[mesh->prim_offset],
124 &tri_patch[mesh->prim_offset],
125 &tri_patch_uv[mesh->vert_offset]);
126 }
127
128 if (progress.get_cancel()) {
129 return;
130 }
131 }
132 }
133
134 /* vertex coordinates */
135 progress.set_status("Updating Mesh", "Copying Mesh to device");
136
143 }
144
145 if (curve_segment_size != 0) {
146 progress.set_status("Updating Mesh", "Copying Curves to device");
147
148 float4 *curve_keys = dscene->curve_keys.alloc(curve_key_size);
149 KernelCurve *curves = dscene->curves.alloc(curve_size);
150 KernelCurveSegment *curve_segments = dscene->curve_segments.alloc(curve_segment_size);
151
152 const bool copy_all_data = dscene->curve_keys.need_realloc() ||
153 dscene->curves.need_realloc() ||
155
156 foreach (Geometry *geom, scene->geometry) {
157 if (geom->is_hair()) {
158 Hair *hair = static_cast<Hair *>(geom);
159
160 bool curve_keys_co_modified = hair->curve_radius_is_modified() ||
161 hair->curve_keys_is_modified();
162 bool curve_data_modified = hair->curve_shader_is_modified() ||
163 hair->curve_first_key_is_modified();
164
165 if (!curve_keys_co_modified && !curve_data_modified && !copy_all_data) {
166 continue;
167 }
168
169 hair->pack_curves(scene,
170 &curve_keys[hair->curve_key_offset],
171 &curves[hair->prim_offset],
172 &curve_segments[hair->curve_segment_offset]);
173 if (progress.get_cancel()) {
174 return;
175 }
176 }
177 }
178
182 }
183
184 if (point_size != 0) {
185 progress.set_status("Updating Mesh", "Copying Point clouds to device");
186
187 float4 *points = dscene->points.alloc(point_size);
188 uint *points_shader = dscene->points_shader.alloc(point_size);
189
190 foreach (Geometry *geom, scene->geometry) {
191 if (geom->is_pointcloud()) {
192 PointCloud *pointcloud = static_cast<PointCloud *>(geom);
193 pointcloud->pack(
194 scene, &points[pointcloud->prim_offset], &points_shader[pointcloud->prim_offset]);
195 if (progress.get_cancel()) {
196 return;
197 }
198 }
199 }
200
201 dscene->points.copy_to_device();
203 }
204
205 if (patch_size != 0 && dscene->patches.need_realloc()) {
206 progress.set_status("Updating Mesh", "Copying Patches to device");
207
208 uint *patch_data = dscene->patches.alloc(patch_size);
209
210 foreach (Geometry *geom, scene->geometry) {
211 if (geom->is_mesh()) {
212 Mesh *mesh = static_cast<Mesh *>(geom);
213 mesh->pack_patches(&patch_data[mesh->patch_offset]);
214
215 if (mesh->patch_table) {
216 mesh->patch_table->copy_adjusting_offsets(&patch_data[mesh->patch_table_offset],
217 mesh->patch_table_offset);
218 }
219
220 if (progress.get_cancel()) {
221 return;
222 }
223 }
224 }
225
226 dscene->patches.copy_to_device();
227 }
228}
229
unsigned int uint
device_vector< uint > points_shader
Definition devicescene.h:44
device_vector< uint > patches
Definition devicescene.h:40
device_vector< float4 > points
Definition devicescene.h:43
device_vector< KernelCurveSegment > curve_segments
Definition devicescene.h:38
device_vector< KernelCurve > curves
Definition devicescene.h:36
device_vector< packed_uint3 > tri_vindex
Definition devicescene.h:32
device_vector< float4 > curve_keys
Definition devicescene.h:37
device_vector< packed_float3 > tri_vnormal
Definition devicescene.h:31
device_vector< uint > tri_shader
Definition devicescene.h:30
device_vector< packed_float3 > tri_verts
Definition devicescene.h:29
device_vector< float2 > tri_patch_uv
Definition devicescene.h:34
device_vector< uint > tri_patch
Definition devicescene.h:33
void device_update_mesh(Device *device, DeviceScene *dscene, Scene *scene, Progress &progress)
Type geometry_type
bool is_pointcloud() const
bool is_hair() const
size_t prim_offset
bool is_mesh() const
Definition hair.h:14
size_t num_curves() const
Definition hair.h:126
void pack_curves(Scene *scene, float4 *curve_key_co, KernelCurve *curve, KernelCurveSegment *curve_segments)
Definition hair.cpp:505
bool get_cancel() const
Definition progress.h:93
void set_status(const string &status_, const string &substatus_="")
Definition progress.h:263
T * alloc(size_t width, size_t height=0, size_t depth=0)
#define CCL_NAMESPACE_END
int num_ptex_faces() const
Definition scene/mesh.h:98
void pack_patches(uint *patch_data)
size_t num_triangles() const
Definition scene/mesh.h:80
void pack_shaders(Scene *scene, uint *shader)
void pack(Scene *scene, float4 *packed_points, uint *packed_shader)
size_t num_points() const