Skip to content

Detect more pipeline settings at load time to avoid pipeline stutters #105175

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

Merged
merged 1 commit into from
Apr 10, 2025

Conversation

clayjohn
Copy link
Member

@clayjohn clayjohn commented Apr 9, 2025

This fixes an unreported bug that created pipeline stutters at run time.

Background

We can't compile every pipeline variant at load time for the ubershader. There are too many. So we detect which ones we know we need and then only compile those. This has the bonus of reducing load times for simple games that only need a few variants.

However, when a feature that needs a new variant is added at run time, we have to go back and compile the missing variant for all existing pipelines. In bigger games this can take a long time.

This is why the docs say to avoid adding things like VoxelGI nodes to the SceneTree at run time.

The problem

Using SSS or the normal_roughness_texture can trigger a new variant being added. What makes these features unique is that they were only being checked if an object that survived culling made it into the render pass. This means that you can trigger pipeline recompilations by looking around.

The solution

We can set the pipeline state when the material is assigned to the object instead of setting it when the object is first visible. This moves the cost to load time and the stutter is gone. Now the usual advice applies, just ensure that any material that uses SSS or the normal_roughness_texture is present in the SceneTree at load time and you will be fine.

Edit

The new approach is basically the same, but now checks the screen textures as well and allocates textures up front too to avoid stuttering on texture creation. On desktops, texture creation isn't really noticeable, but on mobile devices it can lead to noticeable stutter.

Fixes: #101554

@clayjohn clayjohn added bug topic:rendering cherrypick:4.4 Considered for cherry-picking into a future 4.4.x release labels Apr 9, 2025
@clayjohn clayjohn added this to the 4.5 milestone Apr 9, 2025
@clayjohn clayjohn requested a review from DarioSamo April 9, 2025 07:02
@clayjohn clayjohn requested a review from a team as a code owner April 9, 2025 07:02
DarioSamo
DarioSamo previously approved these changes Apr 9, 2025
Copy link
Contributor

@DarioSamo DarioSamo left a comment

Choose a reason for hiding this comment

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

Makes sense!

@DarioSamo DarioSamo self-requested a review April 9, 2025 12:56
@DarioSamo
Copy link
Contributor

DarioSamo commented Apr 9, 2025

I changed my mind slightly about how we should handle this. While this solves pipeline stutters, it won't solve any other stutters caused by screen buffer creation when the element suddenly appears. Something about where this was placed breaks the regular flow of where this behavior is usually handled.

I think what we need is some sort of way to build a default scene state based on tracking what geometry surfaces were created (and also deleted). Said default values would be grabbed here instead of always false:

scene_state.used_sss = false;
scene_state.used_screen_texture = false;
scene_state.used_normal_texture = false;
scene_state.used_depth_texture = false;
scene_state.used_lightmap = false;

That way, we can avoid the current stutters that are caused by the sudden creation of resources when something that uses the effect comes into view.

@DarioSamo
Copy link
Contributor

DarioSamo commented Apr 9, 2025

The idea for handling this in a way that is based of the surface geometry is pretty straightforward. Both surface creation and surface deletion must call the same function with an argument to increase or decrease counters.

// adjust_surface_counters must be called after creating a surface and before deleting one.

void adjust_surface_counters(Surface *p_surface, bool p_increase) {
  int counter_change = p_increase ? 1 : -1;
  if (p_surface->flags & USES_SSS) {
    surfaces_state.subsurface_scattering += counter_change;
  }

  if (p_surface->flags & USES_NORMAL_TEXTURE) {
    surfaces_state.normal_textures += counter_change;
  }
}

Then the above code can simply do:

scene_state.used_sss = surfaces_state.subsurface_scattering > 0; 
 scene_state.used_normal_texture = surfaces_state.normal_textures > 0; 

And consequently, this will also enable the pipeline data and create the textures necessary, eliminating both sources of stutter that can happen if either of these elements are out of view.

@clayjohn
Copy link
Member Author

clayjohn commented Apr 9, 2025

@DarioSamo That's a good idea. It will solve #101554 as well

@clayjohn
Copy link
Member Author

clayjohn commented Apr 9, 2025

Then the above code can simply do:

scene_state.used_sss = surfaces_state.subsurface_scattering > 0; 
 scene_state.used_normal_texture = surfaces_state.normal_textures > 0; 

And consequently, this will also enable the pipeline data and create the textures necessary, eliminating both sources of stutter that can happen if either of these elements are out of view.

It turned out not to be this simple lol. These can trigger behaviour that we only want in frames where it is necessary (like resolving the depth texture etc.). Instead we need to trigger resource + pipeline creation based on the surface data and then still check what is visible per frame for the scene_state. A bit more complex unfortunately.

@clayjohn clayjohn force-pushed the RD-pipeline-sss-roughness branch from a9c7578 to 6225e25 Compare April 9, 2025 20:17
@clayjohn
Copy link
Member Author

clayjohn commented Apr 9, 2025

Updated! In the end it didn't make sense to track the absolute numbers as all of these resources are created when needed and not freed.

I originally incremented the counter for each usage and then used the counter to initialize the scene_state variables, e.g.

scene_state.used_sss = surfaces_state.subsurface_scattering > 0; 

However, that broke rendering in a couple of subtle ways because we were doing things like texture copies unnecessarily.

What I ended up with I think is actually a little bit simpler and should scale well if other resources need this treatment

…cate needed resources at load time.

This allows us to avoid a class of pipeline compilation stutters and
stutters from allocating screen textures.
Copy link
Contributor

@DarioSamo DarioSamo 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 now!

Would it be a good idea to perhaps fail or assert for the existence of the screen and depth textures respectively in the copy() methods now that the "ensure" behavior was moved out of it? Just to catch any errors that could happen from calling one function without having called the ensure previously.

No more suggestions from me besides that.

Repiteo added a commit that referenced this pull request Apr 10, 2025
Detect more pipeline settings at load time to avoid pipeline stutters
@Repiteo
Copy link
Contributor

Repiteo commented Apr 10, 2025

Wait, it didn't count this as merged? That's… Weird. Oh well, for all intents and purposes it is merged, so I'll pretend it worked without a hitch

Thanks!

@Repiteo Repiteo closed this Apr 10, 2025
@AThousandShips AThousandShips removed this from the 4.5 milestone Apr 10, 2025
@AThousandShips AThousandShips added archived cherrypick:4.4 Considered for cherry-picking into a future 4.4.x release and removed cherrypick:4.4 Considered for cherry-picking into a future 4.4.x release archived labels Apr 10, 2025
@AThousandShips AThousandShips added this to the 4.5 milestone Apr 10, 2025
@clayjohn
Copy link
Member Author

clayjohn commented Apr 10, 2025

@Repiteo This didn't get merged, an old commit that is out of date is somehow merged

See the difference between this PR and the commit that got merged

665bdf4 (merged)

bf47c42 (actual commit)

@Repiteo
Copy link
Contributor

Repiteo commented Apr 10, 2025

First time I've seen something like that happen! I'll revert that specific commit and explicitly merge this one after

@Repiteo Repiteo reopened this Apr 10, 2025
Repiteo added a commit that referenced this pull request Apr 10, 2025
@Repiteo Repiteo merged commit 06c71fb into godotengine:master Apr 10, 2025
38 of 41 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Apr 10, 2025

Thanks! (again!)

@clayjohn clayjohn deleted the RD-pipeline-sss-roughness branch April 11, 2025 06:49
jamesminardi pushed a commit to jamesminardi/godot that referenced this pull request Apr 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug cherrypick:4.4 Considered for cherry-picking into a future 4.4.x release topic:rendering
Projects
None yet
Development

Successfully merging this pull request may close these issues.

First use of shader with refraction_enabled=true causes stutter
4 participants