Merge pull request #38503 from lawnjelly/kessel_reorder_bug

GLES2 Batching - fix item reordering bug
This commit is contained in:
Rémi Verschelde 2020-05-06 14:37:09 +02:00 committed by GitHub
commit 7b56111c29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 6 deletions

View File

@ -746,7 +746,9 @@ void RasterizerCanvasGLES2::diagnose_batches(Item::Command *const *p_commands) {
bdata.frame_string += "R "; bdata.frame_string += "R ";
bdata.frame_string += itos(batch.first_command) + "-"; bdata.frame_string += itos(batch.first_command) + "-";
bdata.frame_string += itos(batch.num_commands); bdata.frame_string += itos(batch.num_commands);
bdata.frame_string += " [" + itos(batch.batch_texture_id) + "]";
int tex_id = (int)bdata.batch_textures[batch.batch_texture_id].RID_texture.get_id();
bdata.frame_string += " [" + itos(batch.batch_texture_id) + " - " + itos(tex_id) + "]";
bdata.frame_string += " " + batch.color.to_string(); bdata.frame_string += " " + batch.color.to_string();
@ -1751,7 +1753,7 @@ void RasterizerCanvasGLES2::sort_items() {
return; return;
} }
for (int s = 0; s < bdata.sort_items.size() - 1; s++) { for (int s = 0; s < bdata.sort_items.size() - 2; s++) {
if (sort_items_from(s)) { if (sort_items_from(s)) {
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
bdata.stats_items_sorted++; bdata.stats_items_sorted++;
@ -1789,8 +1791,11 @@ bool RasterizerCanvasGLES2::sort_items_from(int p_start) {
return false; return false;
} }
// local cached aabb
Rect2 second_AABB = second.item->global_rect_cache;
// if the start and 2nd items overlap, can do no more // if the start and 2nd items overlap, can do no more
if (start.item->global_rect_cache.intersects(second.item->global_rect_cache)) { if (start.item->global_rect_cache.intersects(second_AABB)) {
return false; return false;
} }
@ -1811,17 +1816,26 @@ bool RasterizerCanvasGLES2::sort_items_from(int p_start) {
return false; return false;
} }
Item *test_item = test_sort_item->item;
// if the test item overlaps the second item, we can't swap, AT ALL
// because swapping an item OVER this one would cause artefacts
if (second_AABB.intersects(test_item->global_rect_cache)) {
return false;
}
// do they match? // do they match?
if (!_sort_items_match(start, *test_sort_item)) // order is crucial, start first if (!_sort_items_match(start, *test_sort_item)) // order is crucial, start first
{ {
continue; continue;
} }
Item *test_item = test_sort_item->item;
// we can only swap if there are no AABB overlaps with sandwiched neighbours // we can only swap if there are no AABB overlaps with sandwiched neighbours
bool ok = true; bool ok = true;
for (int sn = 1; sn < test; sn++) {
// start from 2, no need to check 1 as the second has already been checked against this item
// in the intersection test above
for (int sn = 2; sn < test; sn++) {
BSortItem *sandwich_neighbour = &bdata.sort_items[p_start + sn]; BSortItem *sandwich_neighbour = &bdata.sort_items[p_start + sn];
if (test_item->global_rect_cache.intersects(sandwich_neighbour->item->global_rect_cache)) { if (test_item->global_rect_cache.intersects(sandwich_neighbour->item->global_rect_cache)) {
ok = false; ok = false;