SceneMgr::_renderScene() updates scenegraph AFTER shadows?
Posted: Wed Jan 16, 2013 5:32 am
I encountered a bug where shadows were lagging the camera by a frame. I tracked the problem down to the order of things in SceneManager::_renderScene().
The order was prepareShadowTextures(), then updateSceneGraph().
Here is a sort of pseudocode of the flow to illustrate the bug (each level of indention is a deeper level of the call stack):
It seems pretty obvious that this is the wrong order to do things. Updating the scene graph and determining the final camera position should be done BEFORE preparing the shadow maps which DEPEND on the camera being up to date.
I believe the only reason this bug hasn't been noticed before is that the recursive calls to _renderScene() tend mask the problem in many cases. In my case, this bug was only apparent when camera-relative-rendering mode was on (and the camera had to be parented to a scene node).
I would expect that if there was a weird and obscure reason why updating the scenegraph should come after preparing shadow textures, that there would be a comment in the code explaining it. But there isn't.
So barring any objections, I'll submit a patch for this to v1-8 branch.
The order was prepareShadowTextures(), then updateSceneGraph().
Here is a sort of pseudocode of the flow to illustrate the bug (each level of indention is a deeper level of the call stack):
Code: Select all
SceneManager::_renderScene( main camera )
->prepareShadowTextures()
->set up shadow cameras (shadow space projection matrices are pushed into GPU params)
->render shadow maps
->_renderScene( shadow camera ) (recursive call to render the first shadow map)
->updateSceneGraph() (main camera is moved here because it is attached to a sceneNode, and this is the first time in the frame updateSceneGraph has been called)
->render shadow map
->updateSceneGraph()
->render main camera view (using stale matrices from the previous camera state)
I believe the only reason this bug hasn't been noticed before is that the recursive calls to _renderScene() tend mask the problem in many cases. In my case, this bug was only apparent when camera-relative-rendering mode was on (and the camera had to be parented to a scene node).
I would expect that if there was a weird and obscure reason why updating the scenegraph should come after preparing shadow textures, that there would be a comment in the code explaining it. But there isn't.
So barring any objections, I'll submit a patch for this to v1-8 branch.