Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom AABBs within MultiMesh resources #79833

Merged
merged 2 commits into from
Feb 16, 2024

Conversation

puchik
Copy link
Contributor

@puchik puchik commented Jul 23, 2023

  • Supporting custom AABBs on MultiMesh resources themselves allows us to prevent costly runtime AABB recalculations.
  • May also help improve CPUParticle performance.

Fixes #79573

@puchik puchik requested review from a team as code owners July 23, 2023 17:50
@puchik puchik force-pushed the multimesh-custom-aabb branch 4 times, most recently from 1d13f11 to ecd6f35 Compare July 23, 2023 18:19
@AThousandShips AThousandShips added this to the 4.x milestone Jul 23, 2023
Copy link
Member

@clayjohn clayjohn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I think this also needs to be exposed to CPUParticles so that they can benefit from the change as well.

GPUParticles3D have a visibility_aabb property that maps onto particles_custom_aabb. I think we can implement visibility aabb for CPUParticles as well so the API matches (we could also update the "convert to CPUParticles" code to keep the custom_aabb as well

@mohsenph69
Copy link

That is great, I can not believe you already did something to solve this problem
So fast
My grass system depend on this, As it change multi-mesh buffer in each grass update
MTerrain_Grass

@puchik puchik force-pushed the multimesh-custom-aabb branch from ecd6f35 to cde9d10 Compare July 29, 2023 00:33
@puchik puchik requested review from a team as code owners July 29, 2023 00:33
@puchik puchik force-pushed the multimesh-custom-aabb branch 2 times, most recently from f78527c to 5bc7e06 Compare July 29, 2023 01:28
@puchik
Copy link
Contributor Author

puchik commented Jul 29, 2023

Thanks for the starting point! I added the visibility_aabb property to CPUParticles3D which just passes it forward to the MultiMesh. The converter is also updated and I added the same AABB generator. Seems complete.

@puchik puchik requested a review from clayjohn July 29, 2023 02:16
@puchik
Copy link
Contributor Author

puchik commented Sep 1, 2023

Not sure why the godot-cpp test is failing with an insufficient storage error. Is it a problem with the workflow? Does it need to try running again?

@akien-mga
Copy link
Member

Yes, you need to rebase to get workflow changes that fixed that.

@puchik puchik force-pushed the multimesh-custom-aabb branch from 5bc7e06 to 8d61adf Compare September 1, 2023 06:55
@puchik
Copy link
Contributor Author

puchik commented Sep 1, 2023

Okay thanks, just rebased and pushed 👍

drivers/gles3/storage/mesh_storage.cpp Outdated Show resolved Hide resolved
drivers/gles3/storage/mesh_storage.cpp Outdated Show resolved Hide resolved
servers/rendering/renderer_rd/storage_rd/mesh_storage.cpp Outdated Show resolved Hide resolved
servers/rendering/renderer_rd/storage_rd/mesh_storage.cpp Outdated Show resolved Hide resolved
@puchik puchik force-pushed the multimesh-custom-aabb branch from 29b5cf2 to e442382 Compare February 10, 2024 15:52
@clayjohn
Copy link
Member

Just one final little change. We need to ensure that we aren't notifying dependencies that the AABB has changed when a custom AABB is being used:

diff --git a/drivers/gles3/storage/mesh_storage.cpp b/drivers/gles3/storage/mesh_storage.cpp
index a276b73c44..9ed1b29225 100644
--- a/drivers/gles3/storage/mesh_storage.cpp
+++ b/drivers/gles3/storage/mesh_storage.cpp
@@ -1961,9 +1961,10 @@ void MeshStorage::multimesh_set_buffer(RID p_multimesh, const Vector<float> &p_b
        } else if (multimesh->mesh.is_valid()) {
                //if we have a mesh set, we need to re-generate the AABB from the new data
                const float *data = p_buffer.ptr();
-
-               _multimesh_re_create_aabb(multimesh, data, multimesh->instances);
-               multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
+               if (multimesh->custom_aabb != AABB()) {
+                       _multimesh_re_create_aabb(multimesh, data, multimesh->instances);
+                       multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
+               }
        }
 }
 
@@ -2110,9 +2111,11 @@ void MeshStorage::_update_dirty_multimeshes() {
                        }
 
                        if (multimesh->aabb_dirty && multimesh->mesh.is_valid()) {
-                               _multimesh_re_create_aabb(multimesh, data, visible_instances);
                                multimesh->aabb_dirty = false;
-                               multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
+                               if (multimesh->custom_aabb != AABB()) {
+                                       _multimesh_re_create_aabb(multimesh, data, visible_instances);
+                                       multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
+                               }
                        }
                }
 
diff --git a/servers/rendering/renderer_rd/storage_rd/mesh_storage.cpp b/servers/rendering/renderer_rd/storage_rd/mesh_storage.cpp
index 0451a66066..2f348838b0 100644
--- a/servers/rendering/renderer_rd/storage_rd/mesh_storage.cpp
+++ b/servers/rendering/renderer_rd/storage_rd/mesh_storage.cpp
@@ -1962,9 +1962,10 @@ void MeshStorage::multimesh_set_buffer(RID p_multimesh, const Vector<float> &p_b
        } else if (multimesh->mesh.is_valid()) {
                //if we have a mesh set, we need to re-generate the AABB from the new data
                const float *data = p_buffer.ptr();
-
-               _multimesh_re_create_aabb(multimesh, data, multimesh->instances);
-               multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
+               if (multimesh->custom_aabb != AABB()) {
+                       _multimesh_re_create_aabb(multimesh, data, multimesh->instances);
+                       multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
+               }
        }
 }
 
@@ -2083,10 +2084,11 @@ void MeshStorage::_update_dirty_multimeshes() {
}
 
                        if (multimesh->aabb_dirty) {
-                               //aabb is dirty..
-                               _multimesh_re_create_aabb(multimesh, data, visible_instances);
                                multimesh->aabb_dirty = false;
-                               multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
+                               if (multimesh->custom_aabb != AABB()) {
+                                       _multimesh_re_create_aabb(multimesh, data, visible_instances);
+                                       multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
+                               }
                        }
                }

This saves us having to do a bunch of updates in the RenderingServer

@puchik puchik force-pushed the multimesh-custom-aabb branch 2 times, most recently from b82e9cf to 6131112 Compare February 15, 2024 23:49
- Supporting custom AABB on the MultiMesh resource itself allows us to prevent costly runtime AABB recalculations.
- Should also help improve CPU Particle performance.
- Improves performance by reducing time spent on AABB generation.
- Also adds an option to generate the AABB manually in the CPUParticles3D dropdown.
@puchik puchik force-pushed the multimesh-custom-aabb branch from 6131112 to ec6518d Compare February 16, 2024 06:37
@puchik
Copy link
Contributor Author

puchik commented Feb 16, 2024

Updated!

Copy link
Member

@clayjohn clayjohn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks for responding quickly to my feedback

@akien-mga akien-mga merged commit 7b152de into godotengine:master Feb 16, 2024
16 checks passed
@akien-mga
Copy link
Member

Thanks!

@Bimbam360
Copy link

Bimbam360 commented Mar 1, 2024

Assume I should comment here, I just tried applying this in 4.3dev4 and get the following odd behaviour:

Base and when set to (-1000, -1000, -1000, 2000, 2000, 2000) manually in inspector
image

When set in GDScript prior to (or after) adding to the scene with MMInst.multimesh.custom_aabb = AABB(Vector3(-1000, -1000, -1000), Vector3(2000, 2000, 2000))

image

@clayjohn
Copy link
Member

clayjohn commented Mar 1, 2024

Assume I should comment here, I just tried applying this in 4.3dev4 and get the following odd behaviour:

Base and when set to (-1000, -1000, -1000, 2000, 2000, 2000) manually in inspector image

When set in GDScript prior to (or after) adding to the scene with MMInst.multimesh.custom_aabb = AABB(Vector3(-1000, -1000, -1000), Vector3(2000, 2000, 2000))

image

Could you make a new issue with an MRP? That looks super strange and I can't figure out what the issue is just from looking at it. The fact that the cubes are cut in half doesn't make any sense haha

@Bimbam360
Copy link

Bimbam360 commented Mar 1, 2024

Could you make a new issue with an MRP? That looks super strange and I can't figure out what the issue is just from looking at it. The fact that the cubes are cut in half doesn't make any sense haha

Done, see #89028

Also note this seems to be specific to mobile renderer with an imported mesh (which has me worried I've somehow made a borked cube and it's my fault lol).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AABB for multimesh will calculated even with custom AABB instance_set_custom_aabb
7 participants