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

Rendering compositor identifies is_opengl API; minor optimisation #102302

Merged
merged 1 commit into from
Mar 13, 2025

Conversation

stuartcarnie
Copy link
Contributor

A couple of minor improvements in the rendering code.

  1. Added is_opengl to the RendererCompositor, given it has a gl_end_frame, allowing it to determine whether gl_end_frame should be called rather than RendererRD blit. Faster than performing string comparisons every frame.
  2. Minor optimisation to reduce double lookup in hash table when rendering frames

@stuartcarnie stuartcarnie requested a review from a team as a code owner February 1, 2025 20:39
Comment on lines -70 to +71
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, render_target_descriptors[rd_texture], 0);
RD::get_singleton()->draw_list_bind_uniform_set(draw_list, it->value, 0);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replaces a 3rd hash table lookup

Comment on lines -53 to +54
if (!render_target_descriptors.has(rd_texture) || !RD::get_singleton()->uniform_set_is_valid(render_target_descriptors[rd_texture])) {
HashMap<RID, RID>::Iterator it = render_target_descriptors.find(rd_texture);
if (it == render_target_descriptors.end() || !RD::get_singleton()->uniform_set_is_valid(it->value)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replaces potentially two hash table lookups

Comment on lines -846 to +860
if (OS::get_singleton()->get_current_rendering_driver_name().begins_with("opengl3")) {
if (RSG::rasterizer->is_opengl()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ultimately, the compositor determines if it requires gl_end_frame be called, so it can determine that by returning true for is_opengl.

More efficient, as the string comparison performs increment / decrement of the COW string buffer to perform the begins_with.

Comment on lines -881 to +896
if (OS::get_singleton()->get_current_rendering_driver_name().begins_with("opengl3")) {
Vector<BlitToScreen> blit_to_screen_vec;
blit_to_screen_vec.push_back(blit);
RSG::rasterizer->blit_render_targets_to_screen(vp->viewport_to_screen, blit_to_screen_vec.ptr(), 1);
if (RSG::rasterizer->is_opengl()) {
RSG::rasterizer->blit_render_targets_to_screen(vp->viewport_to_screen, &blit, 1);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replaced a Vector heap allocation here too.

Comment on lines -830 to +846
viewport_set_force_motion_vectors(vp->self, true);
_viewport_set_force_motion_vectors(vp, true);
} else {
viewport_set_force_motion_vectors(vp->self, false);
_viewport_set_force_motion_vectors(vp, false);
Copy link
Contributor Author

@stuartcarnie stuartcarnie Feb 1, 2025

Choose a reason for hiding this comment

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

Follow same pattern for private implementations that already have a reference to a Viewport *, which avoids looking it up again in the hash map.

Comment on lines -1397 to +1409
if (viewport->force_motion_vectors == p_force_motion_vectors) {
_viewport_set_force_motion_vectors(viewport, p_force_motion_vectors);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replace with private function that takes a Viewport * rather than RID, to avoid performing the lookup and check.

@@ -208,6 +208,7 @@ class RendererViewport {
Vector<Viewport *> _sort_active_viewports();
void _viewport_set_size(Viewport *p_viewport, int p_width, int p_height, uint32_t p_view_count);
bool _viewport_requires_motion_vectors(Viewport *p_viewport);
void _viewport_set_force_motion_vectors(Viewport *p_viewport, bool p_force_motion_vectors);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just like the other _viewport_ functions, this takes a Viewport * as the first argument, rather than RID

@fire fire changed the title rendering: compositor identifies is_opengl API; minor optimisation Rendering compositor identifies is_opengl API; minor optimisation Feb 2, 2025
@clayjohn clayjohn added this to the 4.5 milestone Feb 2, 2025
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 good to me! Should probably be rebased before merging since it has been so long and CI might be out of date

@stuartcarnie
Copy link
Contributor Author

Good call – I've rebased 👍🏻

@Repiteo Repiteo merged commit e2c6d86 into godotengine:master Mar 13, 2025
20 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Mar 13, 2025

Thanks!

@stuartcarnie stuartcarnie deleted the render_opt branch March 13, 2025 18:49
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.

3 participants