Speed up FBX export of normals/tangents/bitangents with numpy #104449

Merged
Bastien Montagne merged 1 commits from Mysteryem/blender-addons:fbx_numpy_normals_tangents_bitangents_pr into main 2023-03-06 15:05:26 +01:00
Member

Use buffers matching the C types of the data in foreach_get to avoid having to iterate and cast every single element in the C foreach_getset function.

Replace nors_transformed_gen mesh transform helper with numpy version nors_transformed.

This patch can slightly change the exported normals/tangent/bitangents when geom_mat_no is set by skipping a cast from double to single precision before casting as float64 (usually double precision):
The original code would multiply mathutils.Matrix (single precision) and mathutils.Vector (single precision) together, which casts the Matrix elements to double precision, performs the multiplication, and then casts the result back to single precision. These single precision multiplied vectors would then be cast to float64 to be exported.
The new code performs the same cast of the matrix to double precision, but skips the step of casting back to single precision, instead casting directly to float64.
Even if the new code were to cast back to single precision float and then to float64 like the original code, there does tend to be a small difference in the result, presumably due to precision error.
In most cases, however, it seems that there is no change because geom_mat_no is usually an identity matrix, so both the original and new code end up at the same result.

For subdivided default cubes with 1538 to 1572864 vertices:
~5-8 times faster (geom_mat_no is None and tangents are exported)
~8-11 times faster (geom_mat_no is set and tangents are exported)
~13-16 times faster (geom_mat_no is None and no tangents are exported)
~18-21 times faster (geom_mat_no is set and no tangents are exported)


mathutils column vector multiplication for reference, note the use of double precision math:

int column_vector_multiplication(float r_vec[MAX_DIMENSIONS], VectorObject *vec, MatrixObject *mat)

This patch depends on
#104447
I wasn't sure if I should include its commit in this PR, I have included it for now.

Use buffers matching the C types of the data in foreach_get to avoid having to iterate and cast every single element in the C foreach_getset function. Replace nors_transformed_gen mesh transform helper with numpy version nors_transformed. This patch can slightly change the exported normals/tangent/bitangents when geom_mat_no is set by skipping a cast from double to single precision before casting as float64 (usually double precision): The original code would multiply mathutils.Matrix (single precision) and mathutils.Vector (single precision) together, which casts the Matrix elements to double precision, performs the multiplication, and then casts the result back to single precision. These single precision multiplied vectors would then be cast to float64 to be exported. The new code performs the same cast of the matrix to double precision, but skips the step of casting back to single precision, instead casting directly to float64. Even if the new code were to cast back to single precision float and then to float64 like the original code, there does tend to be a small difference in the result, presumably due to precision error. In most cases, however, it seems that there is no change because geom_mat_no is usually an identity matrix, so both the original and new code end up at the same result. For subdivided default cubes with 1538 to 1572864 vertices: ~5-8 times faster (geom_mat_no is None and tangents are exported) ~8-11 times faster (geom_mat_no is set and tangents are exported) ~13-16 times faster (geom_mat_no is None and no tangents are exported) ~18-21 times faster (geom_mat_no is set and no tangents are exported) ----- mathutils column vector multiplication for reference, note the use of double precision math: https://projects.blender.org/blender/blender/src/commit/4675ee3c7342c151311a1a2d74acacecd64c4545/source/blender/python/mathutils/mathutils_Vector.c#L2047 This patch depends on https://projects.blender.org/blender/blender-addons/pulls/104447 I wasn't sure if I should include its commit in this PR, I have included it for now.
Thomas Barlow requested review from Bastien Montagne 2023-02-28 01:21:12 +01:00
Bastien Montagne force-pushed fbx_numpy_normals_tangents_bitangents_pr from fa121d0bd1 to 202540d3d7 2023-02-28 18:05:40 +01:00 Compare
Bastien Montagne force-pushed fbx_numpy_normals_tangents_bitangents_pr from 202540d3d7 to b1fa311570 2023-03-06 14:32:55 +01:00 Compare
Bastien Montagne merged commit aa7d1d71e0 into main 2023-03-06 15:05:26 +01:00
Bastien Montagne deleted branch fbx_numpy_normals_tangents_bitangents_pr 2023-03-06 15:05:26 +01:00
Sign in to join this conversation.
No reviewers
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender-addons#104449
No description provided.