Skip to content

Commit

Permalink
butano: sprite affine mats management cpu usage reduced
Browse files Browse the repository at this point in the history
  • Loading branch information
GValiente committed Oct 31, 2023
1 parent e7b9238 commit 2dba94c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 34 deletions.
3 changes: 2 additions & 1 deletion butano/include/bn_documentation.h
Original file line number Diff line number Diff line change
Expand Up @@ -2158,7 +2158,8 @@
*
* @section changelog_16_2_0 16.2.0 (next release)
*
* bn::color_effect::blend added.
* * bn::color_effect::blend added.
* * Sprite affine mats management CPU usage reduced.
*
*
* @section changelog_16_1_0 16.1.0
Expand Down
57 changes: 46 additions & 11 deletions butano/src/bn_sprite_affine_mats_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ namespace
intrusive_list<sprite_affine_mat_attach_node_type> attached_nodes;
unsigned usages;
bool flipped_identity;
bool update;
bool remove_if_not_needed;

void init()
{
attributes = affine_mat_attributes();
usages = 1;
flipped_identity = true;
update = false;
remove_if_not_needed = false;
}

Expand All @@ -83,6 +85,7 @@ namespace
attributes = new_attributes;
usages = 1;
flipped_identity = attributes.flipped_identity();
update = false;
remove_if_not_needed = false;
}
};
Expand All @@ -95,10 +98,12 @@ namespace
item_type items[max_items];
vector<int8_t, max_items> free_item_indexes;
hw::sprite_affine_mats::handle* handles_ptr = nullptr;
int first_index_to_commit = max_items;
int last_index_to_commit = 0;
int first_index_to_update = max_items;
int last_index_to_update = 0;
int first_index_to_remove_if_not_needed = max_items;
int last_index_to_remove_if_not_needed = 0;
int first_index_to_commit = max_items;
int last_index_to_commit = 0;
};

BN_DATA_EWRAM_BSS static_data data;
Expand Down Expand Up @@ -140,14 +145,9 @@ namespace
void _update(int index)
{
item_type& item = data.items[index];
hw::sprite_affine_mats::setup(item.attributes, data.handles_ptr[index]);
_update_indexes_to_commit(index);

for(sprite_affine_mat_attach_node_type& attached_node : item.attached_nodes)
{
sprites_manager_item& sprite_item = sprites_manager_item::affine_mat_attach_node_item(attached_node);
sprites_manager::update_affine_mat_double_size(&sprite_item, index);
}
item.update = true;
data.first_index_to_update = min(data.first_index_to_update, index);
data.last_index_to_update = max(data.last_index_to_update, index);
}

[[nodiscard]] int _new_item_index()
Expand Down Expand Up @@ -295,6 +295,7 @@ void decrease_usages(int id)

if(! item.usages)
{
item.update = false;
item.remove_if_not_needed = false;
data.free_item_indexes.push_back(int8_t(id));
}
Expand Down Expand Up @@ -696,7 +697,11 @@ void update()
++it;

sprites_manager_item& sprite_item = sprites_manager_item::affine_mat_attach_node_item(attached_node);
sprites_manager::remove_identity_affine_mat_if_not_needed(&sprite_item);

if(sprite_item.remove_affine_mat_when_not_needed)
{
sprites_manager::remove_identity_affine_mat_when_not_needed(&sprite_item);
}
}

decrease_usages(index);
Expand All @@ -706,6 +711,36 @@ void update()
data.first_index_to_remove_if_not_needed = max_items;
data.last_index_to_remove_if_not_needed = 0;
}

int first_index_to_update = data.first_index_to_update;

if(first_index_to_update < max_items)
{
for(int index = first_index_to_update, last = data.last_index_to_update; index <= last; ++index)
{
item_type& item = data.items[index];

if(item.update)
{
hw::sprite_affine_mats::setup(item.attributes, data.handles_ptr[index]);
_update_indexes_to_commit(index);
item.update = false;

for(sprite_affine_mat_attach_node_type& attached_node : item.attached_nodes)
{
sprites_manager_item& sprite_item = sprites_manager_item::affine_mat_attach_node_item(attached_node);

if(sprite_double_size_mode(sprite_item.double_size_mode) == sprite_double_size_mode::AUTO)
{
sprites_manager::update_auto_double_size(&sprite_item, index);
}
}
}
}

data.first_index_to_update = max_items;
data.last_index_to_update = 0;
}
}

commit_data retrieve_commit_data()
Expand Down
30 changes: 11 additions & 19 deletions butano/src/bn_sprites_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1331,36 +1331,28 @@ void update_cameras()
}
}

void remove_identity_affine_mat_if_not_needed(id_type id)
void remove_identity_affine_mat_when_not_needed(id_type id)
{
auto item = static_cast<item_type*>(id);

if(item->remove_affine_mat_when_not_needed)
{
_remove_affine_mat(*item);
}
_remove_affine_mat(*item);
}

void update_affine_mat_double_size(id_type id, int affine_mat_id)
void update_auto_double_size(id_type id, int affine_mat_id)
{
auto item = static_cast<item_type*>(id);
bool new_double_size = sprite_affine_mats_manager::sprite_double_size(
affine_mat_id, hw::sprites::shape_size(item->handle));

if(sprite_double_size_mode(item->double_size_mode) == sprite_double_size_mode::AUTO)
if(item->double_size != new_double_size)
{
bool new_double_size = sprite_affine_mats_manager::sprite_double_size(
affine_mat_id, hw::sprites::shape_size(item->handle));
item->double_size = new_double_size;

if(item->double_size != new_double_size)
if(item->visible)
{
item->double_size = new_double_size;

if(item->visible)
{
hw::sprites::show_affine(new_double_size, item->handle);
}

_update_item_dimensions(*item);
hw::sprites::show_affine(new_double_size, item->handle);
}

_update_item_dimensions(*item);
}
}

Expand Down
4 changes: 2 additions & 2 deletions butano/src/bn_sprites_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ namespace sprites_manager

void update_cameras();

void remove_identity_affine_mat_if_not_needed(id_type id);
void remove_identity_affine_mat_when_not_needed(id_type id);

void update_affine_mat_double_size(id_type id, int affine_mat_id);
void update_auto_double_size(id_type id, int affine_mat_id);

void update();

Expand Down
2 changes: 1 addition & 1 deletion docs/changelog.html

Large diffs are not rendered by default.

0 comments on commit 2dba94c

Please sign in to comment.