RenderTargetTexture renderList crash

Hi there :slight_smile:

I have a problem with the RenderTargetTexture class, starting at line 61 : https://github.com/BabylonJS/Babylon.js/blob/08dec3cf9c83b61c468de3f0aee0efbd2a72851b/packages/dev/core/src/Materials/Textures/renderTargetTexture.ts#L61

The setter for the renderList field calls a method _hookArray, which changes the behavior of the passed array value. Specifically, the push and splice methods are replaced by custom functions.

This causes a problem in our application, because we need to switch between 2 different renderList-s every frame, and since we don’t want to deep-copy the list each time, the reference of the array stays the same between 2 calls.

The problem can be seen in this Playground (starting at line 30) : https://playground.babylonjs.com/#XA2GK6#1

As you can see, the push functions are stacked upon each other, so that calling push() causes a Maximum call stack size exceeded error to be thrown.

We found a fix by resetting the push and splice methods before setting renderList, but others may encounter this issue in the future.

Personally, I think changing the behavior of a base Javascript type such as Array is a bad practice and violates the principle of least astonishment. It is quite surprising for the user, who has no way of knowing that this is happening unless they read Babylon.js source code. Maybe we could find another way to mark the meshes as light dirty?

If we really need to keep this behavior (e.g., for backwards compatibility), I think it would be great to add some sort of check at the beginning of _hookArray to avoid exceeding the call stack size.

I kind of like this approach and probably the least breaking one. Would you like to try smthg out ?

Sorry for the late reply! Here is my solution: Avoid hooking rtt renderList multiple times by fchoisy · Pull Request #12843 · BabylonJS/Babylon.js · GitHub

Any suggestions are welcome of course

1 Like