I’ve finalized my PR and I’m sharing it here for anyone interested in testing it out.
You can find the details at:
jMonkeyEngine:master
← jMonkeyEngine:accellbaker
opened 03:40PM - 01 Sep 19 UTC
This PR improves the performances and user friendliness of environment baking.
…
# Accelerated Baking
This PR adds 3 accelerated bakers that can be used in place of the current CPU baker.
The enhancements brought by these bakers is very remarkable, as they allow the environment baking process to be completed in <1s, effectively rendering the need for prebaked environments obsolete.
## IBLGLEnvBaker
This is the most high-performing baker. It run entirely on the GPU, and can generate prefiltered, irradiance, and BRDF maps.
This is the classic env baker implementation that is currently not used in jme.
## IBLHybridEnvBakerLight and IBLGLEnvBakerLight
These bakers generate the prefiltered map and spherical harmonics currently used in the latest PBR shader in jme.
Although this method reduces the VRAM consumption, this approach doesn't allow for as much optimization as the IBLGLEnvBaker. Nonetheless, the following accelerated baking methods are available:
- **IBLHybridEnvBakerLight**: A hybrid baker that creates the prefiltered environmental map using GPU while performing spheric harmonics calculations on the CPU.
- **IBLGLEnvBakerLight**: Similar to the IBLHybridEnvBakerLight, but it leverages the GPU to handle spheric harmonics generation as well.
# New APIs
The LightProbeFactory has now been deprecated in favor of the LightProbeFactory2, which seamlessly integrates the accelerated baking methods. This transition serves as a dedicated migration path for legacy code.
The new API `EnvironmentProbeControl` is designed to streamline and simplify the entire baking and rebaking process.
Here's how it works:
- *Integration*: Developers are only required to instantiate an EnvironmentProbeControl and attach it to the root node of their targeted scene (e.g., the rootNode).
- *Geometry Tagging*: By employing the EnvironmentProbeControl.tag(Spatial) method, you can mark which geometries are part of the environment and should be baked (e.g., the skybox).
- *Profit*: Once the tagging is in place, the control takes care of the rest. On the next frame, it automatically initiates baking for the tagged geometries within its node and completes it before the scene is rendered. No more waiting for the processes to conclude or resorting to convoluted culling tricks.
- *Update the baking*: Every time the baking needs to be updated (eg. day-night cycles) the rebake() method of the control can be invoked.
This new system operates directly within the same scene. It uses RenderManager.setRenderFilter() (added by this PR) to filter out non tagged geometries, avoiding cloning, extra viewports, or other convoluted approaces.
## Example
This is a fully functional remake of TestPBRLighting using the new API
```java
package jme3test.light.pbr;
import com.jme3.app.SimpleApplication;
import com.jme3.environment.EnvironmentProbeControl;
import com.jme3.input.ChaseCamera;
import com.jme3.material.Material;
import com.jme3.math.FastMath;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.util.SkyFactory;
import com.jme3.util.mikktspace.MikktspaceTangentGenerator;
/**
* TestPBRSimple
*/
public class TestPBRSimple extends SimpleApplication {
public static void main(String[] args) {
new TestPBRSimple().start();
}
@Override
public void simpleInitApp() {
Geometry model = (Geometry) assetManager.loadModel("Models/Tank/tank.j3o");
MikktspaceTangentGenerator.generate(model);
Material pbrMat = assetManager.loadMaterial("Models/Tank/tank.j3m");
model.setMaterial(pbrMat);
rootNode.attachChild(model);
ChaseCamera chaseCam = new ChaseCamera(cam, model, inputManager);
chaseCam.setDragToRotate(true);
chaseCam.setMinVerticalRotation(-FastMath.HALF_PI);
chaseCam.setMaxDistance(1000);
chaseCam.setSmoothMotion(true);
chaseCam.setRotationSensitivity(10);
chaseCam.setZoomSensitivity(5);
flyCam.setEnabled(false);
Spatial sky = SkyFactory.createSky(assetManager, "Textures/Sky/Path.hdr", SkyFactory.EnvMapType.EquirectMap);
rootNode.attachChild(sky);
// Create baker control
EnvironmentProbeControl envProbe = new EnvironmentProbeControl(renderManager, assetManager, 256);
rootNode.addControl(envProbe);
// Tag the sky, only the tagged spatials will be rendered in the env map
EnvironmentProbeControl.tag(sky);
}
}
```
As you can see, much simpler and cleaner.
## Knowing issues
The difference in roughness levels in the prefiltered envmap generated by the accelerated baker is less prominent than the current one, i am not sure why, you can test it out by toggling USE_ACCELERATED_BAKING in the TestPBRLighting class included in this PR.
I am not sure if this is an issue or which one is the correct behavior.
This makes PBR env baking ∞% faster. Let me know if you can try it with your models and if they look right.
11 Likes
pspeed
August 8, 2023, 6:52pm
2
Neat.
Do you have a more specific estimate than “infinitely faster”?
1 Like
Yes, the accelerated baker is 10 to 30 times faster on my machine (ubuntu, Ryzen 5 3600 , rtx 2060s)
Eg. for a 256x envmap i measured 100 ms for the accelerated baker vs 2700ms for the CPU baker.
7 Likes