diff options
author | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2013-07-14 16:51:41 +0400 |
---|---|---|
committer | Brecht Van Lommel <brechtvanlommel@pandora.be> | 2013-07-14 16:51:41 +0400 |
commit | cad3406c812bf2b2c8d2994a68db0e9a68071268 (patch) | |
tree | d9efe606514e6def8ef60ae0421b34eaf654147f /intern/cycles/blender | |
parent | ce6cfeca408213a83e0e4c9bb1d7e00c42ea52d8 (diff) |
Fix some unnecessary memory allocation slowness in cycles mesh export.
Diffstat (limited to 'intern/cycles/blender')
-rw-r--r-- | intern/cycles/blender/blender_mesh.cpp | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/intern/cycles/blender/blender_mesh.cpp b/intern/cycles/blender/blender_mesh.cpp index 90278f215c0..9fa9e126756 100644 --- a/intern/cycles/blender/blender_mesh.cpp +++ b/intern/cycles/blender/blender_mesh.cpp @@ -212,13 +212,27 @@ static void mikk_compute_tangents(BL::Mesh b_mesh, BL::MeshTextureFaceLayer b_la static void create_mesh(Scene *scene, Mesh *mesh, BL::Mesh b_mesh, const vector<uint>& used_shaders) { - /* create vertices */ + /* count vertices and faces */ + int numverts = b_mesh.vertices.length(); + int numfaces = b_mesh.tessfaces.length(); + int numtris = 0; + BL::Mesh::vertices_iterator v; + BL::Mesh::tessfaces_iterator f; - for(b_mesh.vertices.begin(v); v != b_mesh.vertices.end(); ++v) - mesh->verts.push_back(get_float3(v->co())); + for(b_mesh.tessfaces.begin(f); f != b_mesh.tessfaces.end(); ++f) { + int4 vi = get_int4(f->vertices_raw()); + numtris += (vi[3] == 0)? 1: 2; + } + + /* reserve memory */ + mesh->reserve(numverts, numtris, 0, 0); + + /* create vertex coordinates and normals */ + int i = 0; + for(b_mesh.vertices.begin(v); v != b_mesh.vertices.end(); ++v, ++i) + mesh->verts[i] = get_float3(v->co()); - /* create vertex normals */ Attribute *attr_N = mesh->attributes.add(ATTR_STD_VERTEX_NORMAL); float3 *N = attr_N->data_float3(); @@ -226,10 +240,10 @@ static void create_mesh(Scene *scene, Mesh *mesh, BL::Mesh b_mesh, const vector< *N = get_float3(v->normal()); /* create faces */ - BL::Mesh::tessfaces_iterator f; - vector<int> nverts; + vector<int> nverts(numfaces); + int fi = 0, ti = 0; - for(b_mesh.tessfaces.begin(f); f != b_mesh.tessfaces.end(); ++f) { + for(b_mesh.tessfaces.begin(f); f != b_mesh.tessfaces.end(); ++f, ++fi) { int4 vi = get_int4(f->vertices_raw()); int n = (vi[3] == 0)? 3: 4; int mi = clamp(f->material_index(), 0, used_shaders.size()-1); @@ -239,18 +253,18 @@ static void create_mesh(Scene *scene, Mesh *mesh, BL::Mesh b_mesh, const vector< if(n == 4) { if(len_squared(cross(mesh->verts[vi[1]] - mesh->verts[vi[0]], mesh->verts[vi[2]] - mesh->verts[vi[0]])) == 0.0f || len_squared(cross(mesh->verts[vi[2]] - mesh->verts[vi[0]], mesh->verts[vi[3]] - mesh->verts[vi[0]])) == 0.0f) { - mesh->add_triangle(vi[0], vi[1], vi[3], shader, smooth); - mesh->add_triangle(vi[2], vi[3], vi[1], shader, smooth); + mesh->set_triangle(ti++, vi[0], vi[1], vi[3], shader, smooth); + mesh->set_triangle(ti++, vi[2], vi[3], vi[1], shader, smooth); } else { - mesh->add_triangle(vi[0], vi[1], vi[2], shader, smooth); - mesh->add_triangle(vi[0], vi[2], vi[3], shader, smooth); + mesh->set_triangle(ti++, vi[0], vi[1], vi[2], shader, smooth); + mesh->set_triangle(ti++, vi[0], vi[2], vi[3], shader, smooth); } } else - mesh->add_triangle(vi[0], vi[1], vi[2], shader, smooth); + mesh->set_triangle(ti++, vi[0], vi[1], vi[2], shader, smooth); - nverts.push_back(n); + nverts[fi] = n; } /* create vertex color attributes */ |