jMonkeyEngine / jmonkeyengine

A complete 3-D game development suite written in Java.
http://jmonkeyengine.org
BSD 3-Clause "New" or "Revised" License
3.75k stars 1.12k forks source link

Refactor shaders to utilize shared .glslibs between multiple rendering engines #2122

Open yaRnMcDonuts opened 9 months ago

yaRnMcDonuts commented 9 months ago

Following up on this discussion:

https://hub.jmonkeyengine.org/t/optimize-terrain-shaders-for-tri-planar-mode-per-layer-remove-fog-defines-to-make-room/46816/43

After this PR: https://github.com/jMonkeyEngine/jmonkeyengine/pull/2090 introducing a new rendering engine is merged into jme, we will have a lot of identical shader code that can be refactored into .glslibs to make things easier to maintain.

For example, much of the code of PBRLighting.frag and PBRLightingGBufferPack.frag is the same code doing the same texture reads prior to the lighting calculations. So this could be put into something like PBRLighting.glslib. And same for Lighting.frag and all of the terrain frag shaders with their GBufferPack equivalent in the new rendering engine.

This will also make it easier for jme devs to fork the existing stock jme shaders without having to constantly update local code when the shader is updated on master. Instead of making changes to PBRLighting.frag and everyone having to update their local fork, we can just make changes to PBRLighting.glslib and then most forks using the glslib will automatically adopt the changes with no extra effort.

JohnLKkk commented 9 months ago

Following up on this discussion:

https://hub.jmonkeyengine.org/t/optimize-terrain-shaders-for-tri-planar-mode-per-layer-remove-fog-defines-to-make-room/46816/42

After this PR: #2090 introducing a new rendering engine is merged into jme, we will have a lot of identical shader code that can be refactored into .glslibs to make things easier to maintain.

For example, much of the code of PBRLighting.frag and PBRLightingGBufferPack.frag is the same code doing the same texture reads prior to the lighting calculations. So this could be put into something like PBRLighting.glslib. And same for Lighting.frag and all of the terrain frag shaders with their GBufferPack equivalent in the new rendering engine.

This will also make it easier for jme devs to fork the existing stock jme shaders without having to constantly update local code when the shader is updated on master. Instead of making changes to PBRLighting.frag and everyone having to update their local fork, we can just make changes to PBRLighting.glslib and then most forks using the glslib will automatically adopt the changes with no extra effort.

I agree with this suggestion. JME3 already has the concept of glsllib, so encapsulating reusable code can help better maintain shaders.