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

Improve performance of shader lighting code in Forward renderers. #99136

Merged

Conversation

DarioSamo
Copy link
Contributor

@DarioSamo DarioSamo commented Nov 12, 2024

  • Skip sampling shadows if attenuation is very small.
  • Skip computing diffuse and specular light if attenuation and shadow are very small.

Using the TPS demo from this PR with the Mobile renderer.

Approximate GPU times.

Ryzen 7950 Integrated Graphics
master: ~32.5 ms
light-compute-attenuation-skip: ~27.0 ms

Mali G-78 (Samsung S21)
master: ~27.4 ms
light-compute-attenuation-skip: ~24.6 ms

Mali G-715 (Google Pixel 8)
master: ~19.4 ms
light-compute-attenuation-skip: ~17.3 ms

Behavior should be identical in theory although verification is welcome. Performance improvements are also expected in Forward+ but will be less significant as there's light clustering involved.

Opening as draft for now as I intend to do more research into improving some of the other features by reorganizing the code further.


Contributed by W4 Games. 🍀

- Skip sampling shadows if attenuation is very small.
- Skip computing diffuse and specular light if attenuation and shadow are very small.
@DarioSamo DarioSamo force-pushed the light-compute-attenuation-skip branch from 831f763 to 0ce4c6d Compare November 27, 2024 15:24
@DarioSamo DarioSamo marked this pull request as ready for review November 27, 2024 15:33
@DarioSamo DarioSamo requested a review from a team as a code owner November 27, 2024 15:33
@DarioSamo
Copy link
Contributor Author

Marking as ready for review, benchmarking and testing for regressions is welcome. Differences in projects with only directional lights are unlikely to show any difference whatsoever.

@clayjohn
Copy link
Member

clayjohn commented Dec 4, 2024

Finally tested on an older device (Pixel 4 Adreno 640). This PR roughly reduces the frame time from 285 ms to about 260 ms which lines up with the other mobile devices you tested on.

@clayjohn clayjohn modified the milestones: 4.x, 4.4 Dec 4, 2024
@Repiteo Repiteo merged commit f5d82af into godotengine:master Dec 5, 2024
20 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Dec 5, 2024

Thanks!

#endif
float Dr = D_GGX(ccNdotH, mix(0.001, 0.1, clearcoat_roughness));
float Gr = 0.25 / (cLdotH * cLdotH);
float Fr = mix(.04, 1.0, cLdotH5);
Copy link

@levidavidmurray levidavidmurray Dec 5, 2024

Choose a reason for hiding this comment

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

Pretty sure this is the source of the errors I'm getting on a fresh compile. Opening certain scenes straight up crashes the editor.

  servers\rendering\renderer_rd\shader_rd.cpp:336 - Error compiling Fragment shader, variant #16 ().
  servers\rendering\renderer_rd\shader_rd.cpp:337 - Failed parse:
  WARNING: 0:60: '' : all default precisions are highp; use precision statements to quiet warning, e.g.:
           "precision mediump int; precision highp float;" 
  ERROR: 0:1086: 'cLdotH5' : undeclared identifier 
  ERROR: 0:1086: '' : missing #endif 
  ERROR: 0:1086: '' : compilation terminated 
  ERROR: 3 compilation errors.  No code generated.

cLdotH5 is only defined when !defined(SPECULAR_SCHLICK_GGX) is true. Should this section be moved into the same conditional block, or should cLdotH5 be defined outside of it?

Copy link
Member

Choose a reason for hiding this comment

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

We should remove the !defined(SPECULAR_SCHLICK_GGX) check in the clearcoat section. and then in the specular GGX section add a !defined(LIGHT_CLEARCOAT_USED) and only define cLdotH5 when clearcoat hasn't already defined it

Copy link
Member

Choose a reason for hiding this comment

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

Fixed in #100081

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.

6 participants