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

DoF optimisation #8057

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft

Conversation

kaychang-unity
Copy link
Contributor

@kaychang-unity kaychang-unity commented Apr 8, 2024

Purpose of this PR

The main optimisation is to dynamically select the number of samples to generate the bokeh, depending on the max amount of bokeh around neighbouring pixels.
This makes the performance of the effect scales depending on the number of blur on screen. The performance is improved by 40% for usual DoF setup when using KERNEL_VERY_LARGE option (4 rings, 71 samples).
This requires an extra step to downsample the CoC texture a few mip levels. The cost of the extra step is compensated by the performance gain when generating the bokeh.

The shader code was also further optimised:

  • moved some calculations outside the loop generating the bokeh samples as uniform, moved some temp variables to half, replaced some divisions by multiplications: 5% improvement.
  • Manual unrolling of the loop generating the bokeh samples: 10% improvement

Testing status

DoF output is mostly changed.
A slight difference is the blending between foreground out-of-focus pixels into in-focus pixels. The original code uses a solution which generate different results depending on the number of kernel samples. As the number of kernel samples is now dynamic, I implemented a slightly different blending strategy but which gives very close results.

Note that the original solution to decide the blending is flawed, so there was no possible clean fix for it.

TODO: test on many platforms


Comments to reviewers

@BenGraterUnity
Copy link
Contributor

Thanks for this. Could you please provide some more detail on what testing has been done and on what plaforms?

@kaychang-unity kaychang-unity marked this pull request as draft April 12, 2024 02:08
@kaychang-unity kaychang-unity self-assigned this Apr 12, 2024
@kaychang-unity
Copy link
Contributor Author

I'm still experimenting with various optimisations before testing more platforms. Once ready, I will likely test on Android (GLES - Vulkan), iOS (Metal, iPhone 8), macOS, Windows (D3D11, D3D12, Vulkan, GL), PS5, Switch. Unfortunately I don't have access to XR devices, so will do on emulator instead (virtual HMD).

@BenGraterUnity
Copy link
Contributor

That sounds great, thank you! 👍

@kaychang-unity kaychang-unity changed the title First pass DoF optimisation. DoF optimisation. Apr 19, 2024
@kaychang-unity kaychang-unity changed the title DoF optimisation. DoF optimisation Apr 19, 2024
The original blending formula between DoF texture and source image is flawed but this fix tries to keep the new results as similar as possible.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants