Fix #102843: Add UV Packer with O(nlogn) performance #105393

Closed
Chris Blackbourn wants to merge 4 commits from Chris_Blackbourn:uv-alpaca-packer into main

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

Adds a novel "Alpaca" UV packing strategy for fast packing
of UV island AABBs without rotation.

Adds a novel "Alpaca" UV packing strategy for fast packing of UV island AABBs without rotation.
Chris Blackbourn added 1 commit 2023-03-03 05:18:05 +01:00
7926531aab Fix #102843: Add UV Packer with O(nlogn) performance.
Adds a novel "Alpaca" UV packing strategy for fast packing
of UV island AABBs without rotation.
Chris Blackbourn added this to the Modeling project 2023-03-03 05:18:20 +01:00
Chris Blackbourn requested review from Campbell Barton 2023-03-03 05:18:36 +01:00
Hans Goudey requested changes 2023-03-03 23:01:13 +01:00
Hans Goudey left a comment
Member

The name is a bit fun, but I'm a bit skeptical of adding a pun in the code just for the sake of it. The fact that it's a "novel strategy" doesn't really help IMO. It would be better to just give it a straightforward name like L-packer or something. And the "turbo" because "it's fast" seems a bit strange too-- I haven't seen that done anywhere else. It's all relative anyway.

Other than that, I took a look at general C++ stuff.

The name is a bit fun, but I'm a bit skeptical of adding a pun in the code just for the sake of it. The fact that it's a "novel strategy" doesn't really help IMO. It would be better to just give it a straightforward name like L-packer or something. And the "turbo" because "it's fast" seems a bit strange too-- I haven't seen that done anywhere else. It's all relative anyway. Other than that, I took a look at general C++ stuff.
@ -23,0 +27,4 @@
public:
float2 uv_diagonal;
float2 uv_placement;
int64_t index;
Member

Looks like the index value isn't actually used. Best to remove it IMO, since the index should be known from the context during iteration, etc.

Looks like the `index` value isn't actually used. Best to remove it IMO, since the index should be known from the context during iteration, etc.
Author
Member

The aabb_vector get sorted on line 121:

  /* Sort from "biggest" to "smallest". */
  std::stable_sort(aabb_vector.begin(), aabb_vector.end(), UVAABBSorterNoRotate());

We need to remember the original position in the island_vector so that the call to BLI_box_pack_2d will write back to the correct place.

The `aabb_vector` get sorted on line 121: ``` /* Sort from "biggest" to "smallest". */ std::stable_sort(aabb_vector.begin(), aabb_vector.end(), UVAABBSorterNoRotate()); ``` We need to remember the original position in the `island_vector` so that the call to `BLI_box_pack_2d` will write back to the correct place.
Member

Ah, I missed that in my search of the file, sorry about that!

Ah, I missed that in my search of the file, sorry about that!
Author
Member

All good.. there's a lot going on here, so maybe it needs to be renamed. input_index or source_index or something?

I'll post a new version shortly, hopefully that will be a bit cleaner

All good.. there's a lot going on here, so maybe it needs to be renamed. `input_index` or `source_index` or something? I'll post a new version shortly, hopefully that will be a bit cleaner
@ -23,0 +45,4 @@
* Each box is packed into an "L" shaped region, gradually filling up space.
* "Alpaca" is a pun, as it's pronounced the same as "L-Packer" in English.
* In theory, alpaca_turbo should be the fastest non-trivial packer, hence the "turbo" suffix.
*/
Member

island_vector is a span here, not a vector. A simpler name might just be islands-- there's no real need to put the data structure in the name, since we see both all the time.

`island_vector` is a span here, not a vector. A simpler name might just be `islands`-- there's no real need to put the data structure in the name, since we see both all the time.
Chris_Blackbourn marked this conversation as resolved
@ -23,0 +46,4 @@
* "Alpaca" is a pun, as it's pronounced the same as "L-Packer" in English.
* In theory, alpaca_turbo should be the fastest non-trivial packer, hence the "turbo" suffix.
*/
static void pack_islands_alpaca_turbo(const Span<UVAABBIsland *> &island_vector,
Member

Span is a small struct and should be passed by value, otherwise it's semantically like a pointer to a pointer

`Span` is a small struct and should be passed by value, otherwise it's semantically like a pointer to a pointer
Chris_Blackbourn marked this conversation as resolved
@ -23,0 +47,4 @@
* In theory, alpaca_turbo should be the fastest non-trivial packer, hence the "turbo" suffix.
*/
static void pack_islands_alpaca_turbo(const Span<UVAABBIsland *> &island_vector,
BoxPack *box_array,
Member

Use Span or MutableSpan for this box_array variable too. That makes it clearer that it's actually an array and not a pointer to a single value, and gives better safety and some utility functions.

Actually.. looks like it's unused here

Use `Span` or `MutableSpan` for this `box_array` variable too. That makes it clearer that it's actually an array and not a pointer to a single value, and gives better safety and some utility functions. Actually.. looks like it's unused here
Author
Member

box_array is somewhat historical, and I'm a little reluctant to change it piecemeal.

At some point, I'll refactor it out of the public interface and some of this stuff will make more sense.

:( I think we're stuck with it just a little longer..

`box_array` is somewhat historical, and I'm a little reluctant to change it piecemeal. At some point, I'll refactor it out of the public interface and some of this stuff will make more sense. :( I think we're stuck with it just a little longer..
@ -23,0 +60,4 @@
float v0 = zigzag ? 0.0f : next_v1;
/* Visit every island in order. */
for (auto island : island_vector) {
Member

Convention in Blender code is to spell out the type name rather than using auto in such situations, since it can be helpful to the reader.

Convention in Blender code is to spell out the type name rather than using `auto` in such situations, since it can be helpful to the reader.
Chris_Blackbourn marked this conversation as resolved
@ -26,2 +106,4 @@
const float margin)
{
/* First, copy information from our input into the AABB structure. */
Vector<UVAABBIsland *> aabb_vector;
Member

Looks like this should be an Array rather than Vector, since it doesn't need to grow-- it can be with the known size directly

Looks like this should be an `Array` rather than `Vector`, since it doesn't need to grow-- it can be with the known size directly
Chris_Blackbourn marked this conversation as resolved
@ -28,2 +110,3 @@
for (const int64_t index : island_vector.index_range()) {
PackIsland *island = island_vector[index];
PackIsland *pack_island = island_vector[index];
UVAABBIsland *aabb = new UVAABBIsland();
Member

Use std::make_unique.

OR! Even better, avoid allocating each struct individually! They're only 24 bytes each

Use `std::make_unique`. OR! Even better, avoid allocating each struct individually! They're only 24 bytes each
Author
Member

The "Alpaca" strategy also serves as an introduction to the other packing strategies, which use larger structures and combine them in more complicated ways than a simple array.

I think it's more important to have consistency (with the code that's not in this PR) than to make this particular PR cleaner.

The "Alpaca" strategy also serves as an introduction to the other packing strategies, which use larger structures and combine them in more complicated ways than a simple array. I think it's more important to have consistency (with the code that's not in this PR) than to make this particular PR cleaner.
@ -30,0 +119,4 @@
}
/* Sort from "biggest" to "smallest". */
std::stable_sort(aabb_vector.begin(), aabb_vector.end(), UVAABBSorterNoRotate());
Member

Any particular reason not to use a lambda here? That's more common. Like this here:

  std::stable_sort(
      aabb_vector.begin(), aabb_vector.end(), [](const UVAABBIsland *a, const UVAABBIsland *b) {
        /* Just choose the AABB with smaller rectangular area. */
        return b->uv_diagonal.x * b->uv_diagonal.y < a->uv_diagonal.x * a->uv_diagonal.y;
      });
Any particular reason not to use a lambda here? That's more common. Like this here: ``` std::stable_sort( aabb_vector.begin(), aabb_vector.end(), [](const UVAABBIsland *a, const UVAABBIsland *b) { /* Just choose the AABB with smaller rectangular area. */ return b->uv_diagonal.x * b->uv_diagonal.y < a->uv_diagonal.x * a->uv_diagonal.y; });
Author
Member

Nice!

Nice!
Chris_Blackbourn marked this conversation as resolved
@ -30,0 +126,4 @@
int64_t max_box_pack = std::min(alpaca_cutoff, island_vector.size());
/* Prepare for box_pack. */
for (int64_t index = 0; index < island_vector.size(); index++) {
Member

for (const int64_t i : island_vector.index_range())

Some reasoning:

  • There's no real benefit of spelling out index rather than i
  • Making the iterator variable const makes it clearer that it's only changed in the iteration
  • Much less visual noise
`for (const int64_t i : island_vector.index_range())` Some reasoning: - There's no real benefit of spelling out `index` rather than `i` - Making the iterator variable `const` makes it clearer that it's only changed in the iteration - Much less visual noise
Chris_Blackbourn marked this conversation as resolved
@ -38,0 +137,4 @@
/* Call box_pack (slow for large N.) */
float max_box_u = 0.0f;
float max_box_v = 0.0f;
BLI_box_pack_2d(box_array, (int)max_box_pack, &max_box_u, &max_box_v);
Member

Use functional casts in C++: int(max_box_pack) https://wiki.blender.org/wiki/Style_Guide/C_Cpp#C.2B.2B_Type_Cast

Use functional casts in C++: `int(max_box_pack)` https://wiki.blender.org/wiki/Style_Guide/C_Cpp#C.2B.2B_Type_Cast
Chris_Blackbourn marked this conversation as resolved
@ -38,0 +143,4 @@
/* Call Alpaca. */
pack_islands_alpaca_turbo(
Span<UVAABBIsland *>(aabb_vector.data() + max_box_pack, aabb_vector.size() - max_box_pack),
Member

Looks like the perfect place to use some of the helper methods like aabb_vector.as_span().drop_front(max_box_pack)

Looks like the perfect place to use some of the helper methods like `aabb_vector.as_span().drop_front(max_box_pack)`
Author
Member

Nice!

Nice!
Chris_Blackbourn marked this conversation as resolved
@ -38,0 +163,4 @@
delete aabb;
}
return max_ff(max_box_u, max_box_v);
Member

Use blenders C++ math namespace or std::max instead

Use blenders C++ `math` namespace or `std::max` instead
Chris_Blackbourn marked this conversation as resolved
Chris Blackbourn added 1 commit 2023-03-03 23:48:33 +01:00
Chris Blackbourn added 1 commit 2023-03-04 23:47:30 +01:00
Hans Goudey changed title from Fix #102843: Add UV Packer with O(nlogn) performance. to Fix #102843: Add UV Packer with O(nlogn) performance 2023-03-06 14:10:55 +01:00
Campbell Barton approved these changes 2023-03-08 01:14:58 +01:00
Campbell Barton reviewed 2023-03-08 01:25:51 +01:00
@ -30,0 +123,4 @@
});
/* Partition island_vector, largest will go to box_pack, the rest alpaca_turbo. */
const int64_t alpaca_cutoff = int64_t(1024); /* TODO: Tune constant. */

It would be good to give some context for whats going on here.

/* Avoid unreasonably high `O(nlogn)` complexity, causing poor
 * performance for many islands at the expense of some efficiency, see: #102843. */
It would be good to give some context for whats going on here. ``` /* Avoid unreasonably high `O(nlogn)` complexity, causing poor * performance for many islands at the expense of some efficiency, see: #102843. */ ```
Chris_Blackbourn marked this conversation as resolved
Chris Blackbourn added 1 commit 2023-03-08 06:30:55 +01:00
Chris Blackbourn closed this pull request 2023-03-08 08:54:13 +01:00
Author
Member
b1185da40341bf1fb6deda37449857facc48ee6e
Chris Blackbourn deleted branch uv-alpaca-packer 2023-03-08 08:54:48 +01:00

Pull request closed

Sign in to join this conversation.
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
3 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#105393
No description provided.