Geometry: speedup reverse UV sampler #118772

Merged
Jacques Lucke merged 23 commits from JacquesLucke/blender:reverse-uv-sampler-speedup into main 2024-02-29 16:42:46 +01:00
Member

This implements a new internal data structure for reverse uv sampling. Being better than the previous one was not particularly hard, because it was never really optimized and used a very simple approach. I found the new implementation to be about 10-20x faster in my tests. Simon was able to reproduce an comparable speedups in production files.

The speed-up is mainly achieved by a better memory layout and better multi-threading during construction. The lookup performance is mostly the same as before.

Like the old data structure, the new one also splits the uv space into uniformly sized cells. The size of the cells is based on the number of triangles. Then it sorts all triangles into the rows that they touch. Finally, it creates a flat array for each row that contains the triangle indices contained in each row.

There are still ways to optimize this further, but for now this is a good improvement already.

This implements a new internal data structure for reverse uv sampling. Being better than the previous one was not particularly hard, because it was never really optimized and used a very simple approach. I found the new implementation to be about 10-20x faster in my tests. Simon was able to reproduce an comparable speedups in production files. The speed-up is mainly achieved by a better memory layout and better multi-threading during construction. The lookup performance is mostly the same as before. Like the old data structure, the new one also splits the uv space into uniformly sized cells. The size of the cells is based on the number of triangles. Then it sorts all triangles into the rows that they touch. Finally, it creates a flat array for each row that contains the triangle indices contained in each row. There are still ways to optimize this further, but for now this is a good improvement already.
Jacques Lucke added 9 commits 2024-02-26 23:34:04 +01:00
buildbot/vexp-code-patch-lint Build done. Details
buildbot/vexp-code-patch-linux-x86_64 Build done. Details
buildbot/vexp-code-patch-darwin-arm64 Build done. Details
buildbot/vexp-code-patch-darwin-x86_64 Build done. Details
buildbot/vexp-code-patch-windows-amd64 Build done. Details
buildbot/vexp-code-patch-coordinator Build done. Details
1fe320ca1f
improve grain size
Author
Member

@blender-bot build

@blender-bot build
Hans Goudey reviewed 2024-02-27 03:44:01 +01:00
Hans Goudey left a comment
Member

I found this pretty straightforward to understand. Nice job! Using a separate data structure for storing the triangles in a row while gathering them will help too.

Sorry if I preempted your future cleanup pass, I was just curious to read this anyway.

I found this pretty straightforward to understand. Nice job! Using a separate data structure for storing the triangles in a row while gathering them will help too. Sorry if I preempted your future cleanup pass, I was just curious to read this anyway.
@ -26,3 +32,4 @@
public:
ReverseUVSampler(Span<float2> uv_map, Span<int3> corner_tris);
ReverseUVSampler(Span<float2> uv_map, Span<int3> corner_tris, const IndexMask &corner_tris_mask);
Member

The user will probably give us a faces_mask BTW. Maybe you'll have some fun converting that to a triangle mask efficiently :P Or maybe you'll pass face offsets to this constructor in the end

The user will probably give us a `faces_mask` BTW. Maybe you'll have some fun converting that to a triangle mask efficiently :P Or maybe you'll pass face offsets to this constructor in the end
Author
Member

Good point, think I'll just remove the mask for now. This complexity can be added as part of another patch.

Good point, think I'll just remove the mask for now. This complexity can be added as part of another patch.
JacquesLucke marked this conversation as resolved
@ -15,0 +22,4 @@
struct Row {
int x_min = 0;
int x_max = 0;
Vector<int> offsets;
Member

I'd use Array here. Looks like amortized growth isn't necessary. And why not save 16 bytes per row :)

I'd use `Array` here. Looks like amortized growth isn't necessary. And why not save 16 bytes per row :)
JacquesLucke marked this conversation as resolved
@ -15,0 +33,4 @@
struct TriWithRange {
int tri_index;
IndexRange range;
Member

Feels like it would be worth storing the IndexRange in its 32 bit integer form here, saving at least 8 bytes per triangle

Feels like it would be worth storing the `IndexRange` in its 32 bit integer form here, saving at least 8 bytes per triangle
JacquesLucke marked this conversation as resolved
@ -15,0 +44,4 @@
struct LocalRowData {
TriWithRangeGroup *tris = nullptr;
int x_min = INT32_MAX;
Member

What about storing this as Bounds<int> directly? Same with Row. Just makes the concepts simpler to read maybe

What about storing this as `Bounds<int>` directly? Same with `Row`. Just makes the concepts simpler to read maybe
Author
Member

Wanted to that at first, but I wasn't sure if I'm allowed to initialize Bounds<int> with the same values were the min is larger than the max.

Wanted to that at first, but I wasn't sure if I'm allowed to initialize `Bounds<int>` with the same values were the min is larger than the max.
JacquesLucke marked this conversation as resolved
@ -42,0 +81,4 @@
{
}
BLI_NOINLINE static void sort_into_y_buckets(
Member

Might as well be sort_into_y_rows to avoid adding another term? Just an idea, don't have a strong opinion!

Might as well be `sort_into_y_rows` to avoid adding another term? Just an idea, don't have a strong opinion!
JacquesLucke marked this conversation as resolved
@ -42,0 +98,4 @@
key_bounds.max.x);
const TriWithRange tri_with_range{tri_i, x_range};
for (const int key_y :
Member

What do you think about changing from key to cell in variable names? That naming feels a bit more visual

What do you think about changing from `key` to `cell` in variable names? That naming feels a bit more visual
JacquesLucke marked this conversation as resolved
@ -44,0 +125,4 @@
const Bounds<int> y_bounds,
ReverseUVSampler::LookupGrid &lookup_grid)
{
// SCOPED_TIMER_AVERAGED("fill rows");
Member

If this comment is meant to stay, why not SCOPED_TIMER_AVERAGED(__func__);?

If this comment is meant to stay, why not `SCOPED_TIMER_AVERAGED(__func__);`?
JacquesLucke marked this conversation as resolved
@ -44,0 +189,4 @@
const IndexMask &corner_tris_mask)
: uv_map_(uv_map), corner_tris_(corner_tris), lookup_grid_(std::make_unique<LookupGrid>())
{
resolution_ = std::max<int>(3, std::sqrt(corner_tris.size()) * 3);
Member

It would be nice to have some comment about the resolution heuristic

It would be nice to have some comment about the resolution heuristic
JacquesLucke marked this conversation as resolved
Hans Goudey added this to the Nodes & Physics project 2024-02-27 04:09:50 +01:00
Jacques Lucke added 2 commits 2024-02-27 11:27:23 +01:00
Member

Great job! Getting a solid 8-15x performance improvement in our production files on modifiers that rely heavily on the UV sampling node without any regression in behavior afaict 👍

Great job! Getting a solid 8-15x performance improvement in our production files on modifiers that rely heavily on the UV sampling node without any regression in behavior afaict 👍
Jacques Lucke added 7 commits 2024-02-28 13:25:10 +01:00
Jacques Lucke added 3 commits 2024-02-28 22:48:44 +01:00
Jacques Lucke added 1 commit 2024-02-28 22:57:09 +01:00
Jacques Lucke changed title from WIP: Geometry: speedup reverse uv sampler to Geometry: speedup reverse uv sampler 2024-02-28 22:57:34 +01:00
Jacques Lucke requested review from Hans Goudey 2024-02-28 22:57:46 +01:00

How this works in corner cases which was pretty worst for old implementation? For instance UV scale ~100x..

How this works in corner cases which was pretty worst for old implementation? For instance UV scale ~100x..
Author
Member

The worst case performance is still similarly bad to before, but not worse I think.

The worst case performance is still similarly bad to before, but not worse I think.
Hans Goudey changed title from Geometry: speedup reverse uv sampler to Geometry: speedup reverse UV sampler 2024-02-29 15:06:57 +01:00
Hans Goudey approved these changes 2024-02-29 15:36:49 +01:00
Hans Goudey left a comment
Member

Code is quite nice now! Didn't find any issues in my testing.

Code is quite nice now! Didn't find any issues in my testing.
@ -6,6 +6,7 @@
#include <optional>
#include "BLI_index_mask_fwd.hh"
Member

Unused header now

Unused header now
JacquesLucke marked this conversation as resolved
@ -4,2 +4,4 @@
#include <algorithm>
#include <fmt/format.h>
#include <mutex>
Member

<mutex> is unused

`<mutex>` is unused
JacquesLucke marked this conversation as resolved
Jacques Lucke added 1 commit 2024-02-29 16:39:56 +01:00
Jacques Lucke merged commit 0a430fb8cb into main 2024-02-29 16:42:46 +01:00
Jacques Lucke deleted branch reverse-uv-sampler-speedup 2024-02-29 16:42:48 +01:00
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 Assignees
4 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#118772
No description provided.