diff options
author | Hans Goudey <h.goudey@me.com> | 2022-09-05 19:56:34 +0300 |
---|---|---|
committer | Hans Goudey <h.goudey@me.com> | 2022-09-05 19:56:34 +0300 |
commit | 05952aa94d33eeb504fa63618ba35c2bcc8bd19b (patch) | |
tree | c9ec37adf20c3c37ccaab44869220dcbe8e987a3 /source/blender/blenkernel/intern/dynamicpaint.c | |
parent | 63cfc8f9f6d623f33b50c5c07976af2b22845713 (diff) |
Mesh: Remove redundant custom data pointers
For copy-on-write, we want to share attribute arrays between meshes
where possible. Mutable pointers like `Mesh.mvert` make that difficult
by making ownership vague. They also make code more complex by adding
redundancy.
The simplest solution is just removing them and retrieving layers from
`CustomData` as needed. Similar changes have already been applied to
curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of
the pointers generally makes code more obvious and more reusable.
Mesh data is now accessed with a C++ API (`Mesh::edges()` or
`Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`).
The CoW changes this commit makes possible are described in T95845
and T95842, and started in D14139 and D14140. The change also simplifies
the ongoing mesh struct-of-array refactors from T95965.
**RNA/Python Access Performance**
Theoretically, accessing mesh elements with the RNA API may become
slower, since the layer needs to be found on every random access.
However, overhead is already high enough that this doesn't make a
noticible differenc, and performance is actually improved in some
cases. Random access can be up to 10% faster, but other situations
might be a bit slower. Generally using `foreach_get/set` are the best
way to improve performance. See the differential revision for more
discussion about Python performance.
Cycles has been updated to use raw pointers and the internal Blender
mesh types, mostly because there is no sense in having this overhead
when it's already compiled with Blender. In my tests this roughly
halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million
face grid).
Differential Revision: https://developer.blender.org/D15488
Diffstat (limited to 'source/blender/blenkernel/intern/dynamicpaint.c')
-rw-r--r-- | source/blender/blenkernel/intern/dynamicpaint.c | 59 |
1 files changed, 29 insertions, 30 deletions
diff --git a/source/blender/blenkernel/intern/dynamicpaint.c b/source/blender/blenkernel/intern/dynamicpaint.c index 8a41b2294f5..9c09ebb6973 100644 --- a/source/blender/blenkernel/intern/dynamicpaint.c +++ b/source/blender/blenkernel/intern/dynamicpaint.c @@ -1402,24 +1402,24 @@ static void dynamicPaint_initAdjacencyData(DynamicPaintSurface *surface, const b /* For vertex format, count every vertex that is connected by an edge */ int numOfEdges = mesh->totedge; int numOfPolys = mesh->totpoly; - struct MEdge *edge = mesh->medge; - struct MPoly *mpoly = mesh->mpoly; - struct MLoop *mloop = mesh->mloop; + const MEdge *edges = BKE_mesh_edges(mesh); + const MPoly *polys = BKE_mesh_polygons(mesh); + const MLoop *loops = BKE_mesh_loops(mesh); /* count number of edges per vertex */ for (int i = 0; i < numOfEdges; i++) { - ad->n_num[edge[i].v1]++; - ad->n_num[edge[i].v2]++; + ad->n_num[edges[i].v1]++; + ad->n_num[edges[i].v2]++; - temp_data[edge[i].v1]++; - temp_data[edge[i].v2]++; + temp_data[edges[i].v1]++; + temp_data[edges[i].v2]++; } /* also add number of vertices to temp_data * to locate points on "mesh edge" */ for (int i = 0; i < numOfPolys; i++) { - for (int j = 0; j < mpoly[i].totloop; j++) { - temp_data[mloop[mpoly[i].loopstart + j].v]++; + for (int j = 0; j < polys[i].totloop; j++) { + temp_data[loops[polys[i].loopstart + j].v]++; } } @@ -1444,15 +1444,15 @@ static void dynamicPaint_initAdjacencyData(DynamicPaintSurface *surface, const b /* and now add neighbor data using that info */ for (int i = 0; i < numOfEdges; i++) { /* first vertex */ - int index = edge[i].v1; + int index = edges[i].v1; n_pos = ad->n_index[index] + temp_data[index]; - ad->n_target[n_pos] = edge[i].v2; + ad->n_target[n_pos] = edges[i].v2; temp_data[index]++; /* second vertex */ - index = edge[i].v2; + index = edges[i].v2; n_pos = ad->n_index[index] + temp_data[index]; - ad->n_target[n_pos] = edge[i].v1; + ad->n_target[n_pos] = edges[i].v1; temp_data[index]++; } } @@ -1604,7 +1604,7 @@ static void dynamicPaint_setInitialColor(const Scene *scene, DynamicPaintSurface else if (surface->init_color_type == MOD_DPAINT_INITIAL_TEXTURE) { Tex *tex = surface->init_texture; - const MLoop *mloop = mesh->mloop; + const MLoop *mloop = BKE_mesh_loops(mesh); const MLoopTri *mlooptri = BKE_mesh_runtime_looptri_ensure(mesh); const int tottri = BKE_mesh_runtime_looptri_len(mesh); @@ -1660,7 +1660,7 @@ static void dynamicPaint_setInitialColor(const Scene *scene, DynamicPaintSurface /* for vertex surface, just copy colors from mcol */ if (surface->format == MOD_DPAINT_SURFACE_F_VERTEX) { - const MLoop *mloop = mesh->mloop; + const MLoop *mloop = BKE_mesh_loops(mesh); const int totloop = mesh->totloop; const MLoopCol *col = CustomData_get_layer_named( &mesh->ldata, CD_PROP_BYTE_COLOR, surface->init_layername); @@ -1811,7 +1811,7 @@ static void dynamicPaint_applySurfaceDisplace(DynamicPaintSurface *surface, Mesh /* displace paint */ if (surface->type == MOD_DPAINT_SURFACE_T_DISPLACE) { - MVert *mvert = result->mvert; + MVert *mvert = BKE_mesh_vertices_for_write(result); DynamicPaintModifierApplyData data = { .surface = surface, @@ -1913,9 +1913,9 @@ static Mesh *dynamicPaint_Modifier_apply(DynamicPaintModifierData *pmd, Object * /* vertex color paint */ if (surface->type == MOD_DPAINT_SURFACE_T_PAINT) { - MLoop *mloop = result->mloop; + const MLoop *mloop = BKE_mesh_loops(result); const int totloop = result->totloop; - MPoly *mpoly = result->mpoly; + const MPoly *mpoly = BKE_mesh_polygons(result); const int totpoly = result->totpoly; /* paint is stored on dry and wet layers, so mix final color first */ @@ -1989,8 +1989,6 @@ static Mesh *dynamicPaint_Modifier_apply(DynamicPaintModifierData *pmd, Object * if (defgrp_index != -1 && !dvert && (surface->output_name[0] != '\0')) { dvert = CustomData_add_layer( &result->vdata, CD_MDEFORMVERT, CD_SET_DEFAULT, NULL, sData->total_points); - /* Make the dvert layer easily accessible from the mesh data. */ - result->dvert = dvert; } if (defgrp_index != -1 && dvert) { for (int i = 0; i < sData->total_points; i++) { @@ -2012,7 +2010,7 @@ static Mesh *dynamicPaint_Modifier_apply(DynamicPaintModifierData *pmd, Object * } /* wave simulation */ else if (surface->type == MOD_DPAINT_SURFACE_T_WAVE) { - MVert *mvert = result->mvert; + MVert *mvert = BKE_mesh_vertices_for_write(result); DynamicPaintModifierApplyData data = { .surface = surface, @@ -2823,7 +2821,7 @@ int dynamicPaint_createUVSurface(Scene *scene, return setError(canvas, N_("Cannot bake non-'image sequence' formats")); } - mloop = mesh->mloop; + mloop = BKE_mesh_loops(mesh); mlooptri = BKE_mesh_runtime_looptri_ensure(mesh); const int tottri = BKE_mesh_runtime_looptri_len(mesh); @@ -2963,7 +2961,7 @@ int dynamicPaint_createUVSurface(Scene *scene, BKE_mesh_vert_looptri_map_create(&vert_to_looptri_map, &vert_to_looptri_map_mem, - mesh->mvert, + BKE_mesh_vertices_for_write(mesh), mesh->totvert, mlooptri, tottri, @@ -3783,7 +3781,8 @@ static void dynamicPaint_brushMeshCalculateVelocity(Depsgraph *depsgraph, eModifierType_DynamicPaint); mesh_p = BKE_mesh_copy_for_eval(dynamicPaint_brush_mesh_get(brush), false); numOfVerts_p = mesh_p->totvert; - mvert_p = mesh_p->mvert; + + mvert_p = BKE_mesh_vertices_for_write(mesh_p); copy_m4_m4(prev_obmat, ob->obmat); /* current frame mesh */ @@ -3799,7 +3798,7 @@ static void dynamicPaint_brushMeshCalculateVelocity(Depsgraph *depsgraph, eModifierType_DynamicPaint); mesh_c = dynamicPaint_brush_mesh_get(brush); numOfVerts_c = mesh_c->totvert; - mvert_c = mesh_c->mvert; + mvert_c = BKE_mesh_vertices_for_write(mesh_c); (*brushVel) = (struct Vec3f *)MEM_mallocN(numOfVerts_c * sizeof(Vec3f), "Dynamic Paint brush velocity"); @@ -4270,10 +4269,10 @@ static bool dynamicPaint_paintMesh(Depsgraph *depsgraph, VolumeGrid *grid = bData->grid; mesh = BKE_mesh_copy_for_eval(brush_mesh, false); - mvert = mesh->mvert; + mvert = BKE_mesh_vertices_for_write(mesh); const float(*vert_normals)[3] = BKE_mesh_vertex_normals_ensure(mesh); mlooptri = BKE_mesh_runtime_looptri_ensure(mesh); - mloop = mesh->mloop; + mloop = BKE_mesh_loops(mesh); numOfVerts = mesh->totvert; /* Transform collider vertices to global space @@ -4759,7 +4758,7 @@ static bool dynamicPaint_paintSinglePoint( } const Mesh *brush_mesh = dynamicPaint_brush_mesh_get(brush); - const MVert *mvert = brush_mesh->mvert; + const MVert *mvert = BKE_mesh_vertices(brush_mesh); /* * Loop through every surface point @@ -5862,7 +5861,7 @@ static bool dynamicPaint_surfaceHasMoved(DynamicPaintSurface *surface, Object *o PaintSurfaceData *sData = surface->data; PaintBakeData *bData = sData->bData; Mesh *mesh = dynamicPaint_canvas_mesh_get(surface->canvas); - MVert *mvert = mesh->mvert; + const MVert *mvert = BKE_mesh_vertices(mesh); int numOfVerts = mesh->totvert; @@ -6022,7 +6021,7 @@ static bool dynamicPaint_generateBakeData(DynamicPaintSurface *surface, const bool do_accel_data = (surface->effect & MOD_DPAINT_EFFECT_DO_DRIP) != 0; int canvasNumOfVerts = mesh->totvert; - MVert *mvert = mesh->mvert; + const MVert *mvert = BKE_mesh_vertices(mesh); Vec3f *canvas_verts; if (bData) { |