Mikktspace tangent calculator optimizations #112256

Open
Eugene-Kuznetsov wants to merge 1 commits from Eugene-Kuznetsov/blender:ek_mikktspace_rework into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
Contributor

The logic in intern/mikktspace is sometimes a major performance bottleneck. On my dual Xeon E5-2699 v4, I get as low as 10 fps in "animation player" in viewport shading mode with only a single 20k face mesh without any fancy modifiers. The bottleneck occurs because, under some circumstances, the method getTangSpace() is executed once per frame for every mesh, even if the mesh stays exactly the same. As far as I can tell, the sufficient conditions are (1) the mesh should be rigged to an armature with keypoints; and (2) at least one material on the mesh should contain a 'normal map' node.

This PR is a rework of tangent calculation logic. Individual steps are the same (and numbers it produces match the old module with reasonable accuracy), but their implementations were tweaked to various degrees. I've benchmarked it on two systems and I observe a 20% to 40% reduction in getTangSpace() execution time.

The logic in intern/mikktspace is sometimes a major performance bottleneck. On my dual Xeon E5-2699 v4, I get as low as 10 fps in "animation player" in viewport shading mode with only a single 20k face mesh without any fancy modifiers. The bottleneck occurs because, under some circumstances, the method getTangSpace() is executed once per frame for every mesh, even if the mesh stays exactly the same. As far as I can tell, the sufficient conditions are (1) the mesh should be rigged to an armature with keypoints; and (2) at least one material on the mesh should contain a 'normal map' node. This PR is a rework of tangent calculation logic. Individual steps are the same (and numbers it produces match the old module with reasonable accuracy), but their implementations were tweaked to various degrees. I've benchmarked it on two systems and I observe a 20% to 40% reduction in getTangSpace() execution time.
Eugene-Kuznetsov force-pushed ek_mikktspace_rework from d536e37748 to 1d3fb1b5a8 2023-09-11 23:09:30 +02:00 Compare
Eugene-Kuznetsov force-pushed ek_mikktspace_rework from 1d3fb1b5a8 to 09be0b062f 2023-09-11 23:17:56 +02:00 Compare
Author
Contributor

Quick update. PR still in development. I made a standalone version that lets me test performance and accuracy using previously dumped meshes and without launching the whole graphical UI. Found a couple of bugs. Added parallelization of build4RuleGroups() (the last major piece that was not parallelized, and sometimes taking over a quarter of total time) for well-behaved meshes only (it seems almost impossible to parallelize it and to have it match the behavior of reference implementation for all meshes).

The biggest obstacle right now is that I can reliably match reference implementation for "good" meshes, but things get complicated for non-manifold meshes and meshes with messed-up UV maps. For example, there's a piece of logic in there that marks triangles as "neighbors" pairwise if they share edges. Problems start when some edge is shared by three or more triangles, because each triangle can only have one neighbor along that edge, and the choice of which triangle is paired with which has an effect on generated tangents down the road.

Out of 20 meshes I'm testing on, I see some mismatches in 7, fewer than 1% of normals affected in each case but they are still mismatches and I need to find out what's causing them.

Quick update. PR still in development. I made a standalone version that lets me test performance and accuracy using previously dumped meshes and without launching the whole graphical UI. Found a couple of bugs. Added parallelization of build4RuleGroups() (the last major piece that was not parallelized, and sometimes taking over a quarter of total time) for well-behaved meshes only (it seems almost impossible to parallelize it and to have it match the behavior of reference implementation for all meshes). The biggest obstacle right now is that I can reliably match reference implementation for "good" meshes, but things get complicated for non-manifold meshes and meshes with messed-up UV maps. For example, there's a piece of logic in there that marks triangles as "neighbors" pairwise if they share edges. Problems start when some edge is shared by three or more triangles, because each triangle can only have one neighbor along that edge, and the choice of which triangle is paired with which has an effect on generated tangents down the road. Out of 20 meshes I'm testing on, I see some mismatches in 7, fewer than 1% of normals affected in each case but they are still mismatches and I need to find out what's causing them.
Eugene-Kuznetsov force-pushed ek_mikktspace_rework from 09be0b062f to 8df23fd1d9 2023-10-05 07:39:57 +02:00 Compare
Eugene-Kuznetsov force-pushed ek_mikktspace_rework from 8df23fd1d9 to 075bd919fa 2023-10-05 17:56:33 +02:00 Compare
Eugene-Kuznetsov force-pushed ek_mikktspace_rework from 075bd919fa to 245fe2c34c 2023-11-21 02:06:09 +01:00 Compare
Eugene-Kuznetsov force-pushed ek_mikktspace_rework from bcc11845be to 09a0c1feed 2023-12-06 18:34:07 +01:00 Compare
Eugene-Kuznetsov changed title from WIP: Mikktspace tangent calculator optimizations to Mikktspace tangent calculator optimizations 2023-12-07 16:05:22 +01:00
Iliya Katushenock added this to the Modeling project 2023-12-07 16:07:15 +01:00
Iliya Katushenock added the
Interest
EEVEE & Viewport
label 2023-12-07 16:07:19 +01:00
Author
Contributor

If anyone wants to test this, would appreciate it. I have resolved all mismatches I could find. On average, the execution time is reduced at least 50%. I can also supply another branch that includes comparison against reference and timing code.

If anyone wants to test this, would appreciate it. I have resolved all mismatches I could find. On average, the execution time is reduced at least 50%. I can also supply another branch that includes comparison against reference and timing code.
Ray molenkamp requested changes 2023-12-07 16:38:54 +01:00
Ray molenkamp left a comment
Member

This will need specialized kernels for the various architectures that are selected at run-time, we just can't raise the blender requirements to avx2 at this point.

This will need specialized kernels for the various architectures that are selected at run-time, we just can't raise the blender requirements to avx2 at this point.
@ -871,3 +871,3 @@
# different results. This will lead to automated test failures. So disable
# this until we support it.
set(PLATFORM_CFLAGS "-pipe -fPIC -funsigned-char -fno-strict-aliasing -ffp-contract=off")
set(PLATFORM_CFLAGS "-pipe -fPIC -funsigned-char -fno-strict-aliasing -ffp-contract=off -march=native")
Member

-march=native is a no-go as it would break blender on lower end systems.

`-march=native` is a no-go as it would break blender on lower end systems.
Author
Contributor

I am already testing for avx2 at runtime and only using the avx2 kernel if it is available. My problem is that I need -march=native or -march=avx2 for the intrinsics to be available in the first place. I see your point though. I need to see if there's some less drastic way to enable the intrinsics.

I am already testing for avx2 at runtime and only using the avx2 kernel if it is available. My problem is that I need -march=native or -march=avx2 for the intrinsics to be available in the first place. I see your point though. I need to see if there's some less drastic way to enable the intrinsics.
Eugene-Kuznetsov marked this conversation as resolved
@ -0,0 +12,4 @@
/* we have to be compiled with -mavx2 or with -march=native on an
* avx2-capable chip (this has not been the case for Blender previously) */
#if __has_builtin(__builtin_ia32_permvarsf256)
#pragma message ("Using AVX2")
Member

left over debug print

left over debug print
Eugene-Kuznetsov marked this conversation as resolved
Eugene-Kuznetsov force-pushed ek_mikktspace_rework from 09a0c1feed to df9da9cf85 2023-12-09 01:08:15 +01:00 Compare
Eugene-Kuznetsov force-pushed ek_mikktspace_rework from df9da9cf85 to 08e3de67a7 2024-02-06 09:39:24 +01:00 Compare
Eugene-Kuznetsov force-pushed ek_mikktspace_rework from 08e3de67a7 to f17af94955 2024-03-04 05:46:43 +01:00 Compare
Eugene-Kuznetsov force-pushed ek_mikktspace_rework from f17af94955 to 85e5366f7f 2024-04-02 07:20:57 +02:00 Compare
Merge conflict checking is in progress. Try again in few moments.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u ek_mikktspace_rework:Eugene-Kuznetsov-ek_mikktspace_rework
git checkout Eugene-Kuznetsov-ek_mikktspace_rework
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
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#112256
No description provided.