• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

Shader with memory leak?

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,414
I'm not sure what is happening here but everything works fine on my Samsung phone but after 2-3 minutes this happens? It gets slower and slower. I don't see any of this happening on web or desktop btw. I'm not sure if it happens on all Android devices or just some unique ones.

Can you spot a memory leak or something?

Code:
Import mojo2
Function Main()
    New MyApp
End

Class MyApp Extends App

    Field sourceImage : Image
    Field targetImage : Image
    Field canvas : Canvas
    Field effect : ShaderEffect
    Field value : Float
    Field timer:Float = 0
     
    Method OnCreate()
        sourceImage = Image.Load("graphics.png",0,0,0)
        targetImage = New Image(sourceImage.Width,sourceImage.Height)
        effect = New ShaderEffect
        canvas = New Canvas
          SetUpdateRate 0
    End
   
    Method OnUpdate:Int()
         timer = Millisecs()/100.0
'         timer = MouseX()/100.0 ' Test using mouse
        Return 0
    End
   
    Method OnRender()
        canvas.Clear 0, 0.5, 1, 1  
        effect.SetValue value
        value = value + 0.01        
        effect.SetValue timer ' Pass the varialel timer to shader
        effect.Render sourceImage, sourceImage,targetImage
        canvas.DrawRect 0,0, 256*1,256*1,targetImage,0,0,256,256
        canvas.Flush
    End

End

Class myShader Extends Shader

    Method New()
        Build(LoadString("shader.glsl"))
    End  
   
    Method OnInitMaterial:Void(material:Material)
        material.SetTexture "ColorTexture",Texture.White()
        material.SetScalar "Timer",1
    End
   
    Function Instance:myShader()
        If Not _instance _instance=New myShader
        Return _instance
    End
   
    Private
    Global _instance:myShader
End

Class ShaderEffect

    Private
    Global _canvas:Canvas
    Field _material:Material
       
    Method New()
        If Not _canvas _canvas=New Canvas
        _material=New Material( myShader.Instance() )
    End
   
    Method SetValue:Void( value:Float )
        _material.SetScalar "Timer",value ' Timer should be 0-1
    End
   
    Method Render:Void(source:Image,source2:Image,target:Image)
        _material.SetTexture "ColorTexture",source.Material.ColorTexture
        _canvas.Clear 0, 0, 0, 0
        _canvas.SetRenderTarget target
        _canvas.SetViewport 0,0,target.Width,target.Height
        _canvas.SetProjection2d 0,target.Width,0,target.Height
        _canvas.DrawRect 0,0,target.Width,target.Height,_material
        _canvas.Flush
    End
   
End

Screenshot 2021-06-12 at 12.56.05.png
 

Attachments

  • Waterhader.zip
    144.9 KB · Views: 79
I can't confirm a memory leak. On GLFW and HTML5 it just runs fine forever.
But on Android, after a while you can see that the shader changes slower.
The framerate stays consistantly at 60 FPS but the changing of the image isn't that smooth like in the beginning.

Sadly I wouldn't know if it is your usage of the shader, your shader code, your usage of Globals in objects, or the shader compiler on Android.
Could also be a problem with the StringMap handling the storage the scalars.
 
Okay thanks for trying it out, I will check into it more and get back. I can't rule out anything at this point so I have to experiment a lot.
 
I think I got it. Others with shader slowdown on Android despite 60 fps.

It's probably trigonometry inside the shader, I feed it an increasing number so that's why it goes slower.
Sin & Cos in glsl wants a normal range of numbers and I didn't chop the timer when it grows. Problem solved!

EDIT tested and confirmed it works fine on all Android with no performance hit any more.
Code:
timer = ( Millisecs()/100.0) Mod 100 ' for instance
 
Last edited:
Back
Top Bottom