Updating shader parameters causes memory leak

Hello,
I’m currently making a sailboat game using JME.
I have made shaders in order to animate some parts of the scene like flags and boat sails.
As I wanted the flags and the sails to have different behavior when the wind changes, I modify some material parameters in the update loop (in a custom control to be more precise).
I have carefully followed the shaders video tutorial and my shaders are working well until all the memory is exhausted…
I realized that the problem was also present in the video tutorials.
In the video, some parameters are passed to the shader via the material in the update loop in order to control the shader.
If you carefully look at the video, you will see that the stats show a growing number of OpenGL memory objects (the Shaders (M) line). The problem is visible at 12m41s.
[video]http://youtu.be/XO8y6cmGiFM?t=12m41s[/video]
I have the same problem, and when the number of shaders in memory is around 35000 then the memory usage is about 2GB for the process and the game stops.
I understand that modifying material parameters in the update loop is causing a recompilation of the shader and it’s slow and causes memory problems, but after searching the forum and the documentation, I couldn’t find a solution.

So my question is: how can I modify variables of the shaders in the update loop like the engine does with attributes instead of modifying uniform variables.

Thanks in advance.
Eric.

The original post goes into some details about it:

http://hub.jmonkeyengine.org/forum/topic/video-tutorials-introduction-to-shaders/#post-184089

Basically just change it from a float to a reference type (like Vec2), and update that.

I can’t see your material but you also need to make sure that material parameters that you change every frame aren’t associated with a define. Otherwise, the shader gets recompiled every time.

Edit: I mean, wezrule is kind of right but if your parameters are associated with a define then presumably that’s for a reason and using a vec that you modify outside of normal parameter setting will break that. And if it’s not for a reason then don’t do it and you won’t have a shader leak anymore.

1 Like

Thank you very much, this works very well.
I have used vectors and removed the defines and it’s working.
You can see the result in this short video: http://youtu.be/_wIW1OtT3QU
and the sources are on GitHub
Thank you for your support, for all the awesome work and tutorials, this is great, JME is really great.
Eric.

1 Like

That’s nice!

Good looking game that and kind of you to share the code :slight_smile: