decentraland / unity-renderer

Unity implementation of Decentraland Client
https://play.decentraland.org
Apache License 2.0
137 stars 93 forks source link

Spike: Reduce complexity of Skybox Shader #4823

Closed m3taphysics closed 1 year ago

m3taphysics commented 1 year ago

Description

The skybox in the game is currently being rendered by a separate camera and is using an extremely complex shader-graph based shader and has overheads due to it being on a completely separate camera.

Deliverables

Notes

See unity review: https://drive.google.com/file/d/1hob-9c4MCTZRFKMGj9bMcQTAQyQL8lq7/view

0xD-Fabio commented 1 year ago

Made a test on the average frame-time at 100,100 coordinates, while changing the renderqueue position on the Skybox material. On my laptop with i7-12800H, 16Gb of RAM and an RTX 3070ti, I got the following results:

renderqueue 2445 (at the end of the Opaque pass as Unity suggested in their review) Average frame time: 11.3082 ms renderqueue 2000 (at the beginning of the Opaque pass) Average frame time: 11.5851 ms renderqueue 2998 (as it is now) Average frame time: 12.96443 ms

0xD-Fabio commented 1 year ago

Made another test on the average frame-time at 100,100 coordinates, to calculate the average frametime (120 seconds) difference between dev and the branch where I made small shader improvements. On my laptop with i7-12800H, 16Gb of RAM and an RTX 3070ti, I got the following results:

dev branch: 13.42076 ms shader branch: 12.65271 ms

With this we may be gaining at least ~0.5 ms on frame time.

0xD-Fabio commented 1 year ago

Conclusions about the shader: The skybox shadergraph shader "S_Skybox02" use multiple subshaders, that in turn use other subshaders too. I have split the subshader "SG_Sky03BasicLayer" into two subshaders called "SG_Sky03BasicLayer_CubemapInput" and "SG_Sky03BasicLayer_TexInput". With this split, I stripped out from the main shader and respective subshaders Texture inputs and texture mappings parts that were not used but made anyway some computation.

The shader and subshader need a deep refactoring from an expert. Moving some calculations from inside the shader/subshaders to C# code can be beneficial.

0xD-Fabio commented 1 year ago

Recap and future developments

https://images.zenhubusercontent.com/337227404/15b0b4f1-52eb-47aa-b4ce-c25984db4463/untitled_video___made_with_clipchamp.mp4

Possible future development:

  1. Set the camera draw distance fixed to 3000 (this will permit to draw the infinite floor on the skybox layer up until horizon and blend it correctly with the skybox)

  2. Develop a LayerCullingOverrideController to change the culling distance for any given layer.

  3. Change the "draw distance" option we have in the graphical settings menu to manipulate the culling distance of layer[0] instead of the Camera draw distance of the frustum. (this way, we are setting the draw distance only for the default Layer 0, which contains all the world objects)

The LayerCullingOverrideController should be based on this Unity's Camera.layerCullDistances documented here Unity's documentation is not super explicative but adapting their example to our needs: distances[0] = 500; will set the Default layer 0 to render up to a distance of 500.

Bonus: with that system in place, we can decide on some granular rendering options on different layers for different graphic levels

aixaCode commented 1 year ago

Closing, moved to shape up