Bad performance using surfaces

Discussion in 'Programming' started by TomatoFantasyStudio, Feb 9, 2019.

  1. TomatoFantasyStudio

    TomatoFantasyStudio Member

    Joined:
    Dec 5, 2017
    Posts:
    32
    Please, can anyone tell me, maybe I use surfaces the wrong way?Hey, everyone!
    Can anyone help me? :D

    I create surfaces like in code below.

    Code:
    surface_set_target(application_surface);
    //Draw blood of wounds, fullscreen animation of glitches etc.
    surface_reset_target();
    
    //If don't need to use colour displacement shader (hero is alright). Use ONLY shader 1
    {
    shader_set(<shader1_name>); //Draw shader 1
    draw_surface(application_surface,0,0);  
    shader_reset();
    }
    else
    //If needed to draw colour displacement shader in addition (when hero were wounded). Use shader 1 AND shader 2
    {
        if !surface_exists(surfa)
        {
            surfa = surface_create(gui_w, gui_h);
        }
    
        shader_set(<shader1_name>); //Draw shader 1
        surface_set_target(surfa);
        draw_surface(application_surface,0,0);  
        surface_reset_target();
        shader_reset();  
       
        shader_set(<shader2_name>); //Shader 2 - colour displacement
        draw_surface(surfa,0,0);  
        shader_reset();
    }
    
    And createtion code.
    Code:
    surfa = -1;
    
    gui_w = display_get_gui_width();
    gui_h = display_get_gui_height();
    
    surface_resize(application_surface, gui_w, gui_h);
    application_surface_draw_enable(false);
    

    And get VERY bad perfomance. Even with low resolution.
    Please, can anyone tell me, maybe I use sufaces the wrong way? :(
     
    Last edited: Feb 9, 2019
  2. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    735
    How big is the surface you are drawing? i.e. what is the value of gui_w and gui_h? Is it still slow when you remove the shader processes?
     
    TomatoFantasyStudio likes this.
  3. SoVes

    SoVes Member

    Joined:
    May 17, 2017
    Posts:
    108
    check what is taking time with the debugger. I have had experience of getting bad performance with suing many surface_reset_target.
     
    TomatoFantasyStudio likes this.
  4. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,280
    Make sure you have "synchronization to avoid tearing" or whatever the option is called now disabled. Some computers can't handle more than 1 or 2 surfaces.
     
    TomatoFantasyStudio likes this.
  5. TomatoFantasyStudio

    TomatoFantasyStudio Member

    Joined:
    Dec 5, 2017
    Posts:
    32
    Surfaces are equal to resolution you choose in game. It can be 960x540, 1280x720 or 1920x1080. And when I remove shaders it doesn't effects fps much (maybe about 1-2 fps).


    I have only one surface_reset_target. Debugger says this function takes about 0.063 ms.


    "Synchronization to avoid tearing" was disabled.

    One more interesting thing. Many people launched the game one time and get very bad performance. But after reloading PC sometimes it was fine. And after some work time on PC againg bad. Not the only one man, so I think there problem in game.
    Maybe someone came across this? :confused:
     
    Last edited: Feb 10, 2019
  6. Pfap

    Pfap Member

    Joined:
    Apr 30, 2017
    Posts:
    440
    Are you calling surface_free(your_surface); when you are finished with it?
    What event is your code located in?


    I copied and pasted your code below:
    Code:
    surface_set_target(application_surface);
    //Draw blood of wounds, fullscreen animation of glitches etc.
    surface_reset_target();
    
    //If don't need to use colour displacement shader (hero is alright). Use ONLY shader 1
    {
    shader_set(<shader1_name>); //Draw shader 1
    draw_surface(application_surface,0,0);  
    shader_reset();
    }
    else
    //If needed to draw colour displacement shader in addition (when hero were wounded). Use shader 1 AND shader 2
    {
        if !surface_exists(surfa)
        {
            surfa = surface_create(gui_w, gui_h);
        }
    
    /******************************
    I added this else block
    *******************************/
    else{
    surface_free(surfa);
    }
    
        shader_set(<shader1_name>); //Draw shader 1
        surface_set_target(surfa);
        draw_surface(application_surface,0,0);  
        surface_reset_target();
        shader_reset();  
       
        shader_set(<shader2_name>); //Shader 2 - colour displacement
        draw_surface(surfa,0,0);  
        shader_reset();
    }
    

    I think you might need that else block to free the surface existing in the previous step.
     
    TomatoFantasyStudio likes this.
  7. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,280
    But why would he free the surface every step if you just going to recreate it again The next step anyway? If you are using it from step to step to step, then there's no point in freeing it.
     
  8. Pfap

    Pfap Member

    Joined:
    Apr 30, 2017
    Posts:
    440
    Otherwise, your surfaces will start to pile up creating a surface on top of surface effect. If you are drawing something non-static and creating the surface in the draw event you want to free it and draw a clear alpha on the newly created one.


    Edit:

    To the op

    I forgot to mention the draw clear alpha part.
    Code:
    surface_free(surface);
    
    
    surface = surface_create(camera_get_view_width(view_camera[1]), camera_get_view_height(view_camera[1]));
    
    
    surface_set_target(surface);
    draw_clear_alpha(c_black, 0);
    surface_reset_target();
    
    Depending upon what you're drawing you may not need to always draw the clear alpha.
     
    Last edited: Feb 10, 2019
    TomatoFantasyStudio likes this.
  9. jonjons

    jonjons Member

    Joined:
    May 10, 2017
    Posts:
    275
    TomatoFantasyStudio likes this.
  10. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,280
    Unless he's using room_restart or game_restart elsewhere in his project, where is he repeatedly creating a surface on top of another surface? It's code specifically states to create a new service only if there is no corresponding surface to the variable that holds the surface ID.
    in the code that he posted, I see no memory leak. Nor do I see anywhere where he declares the variable surf a with the command var, at which point freeing the surface every step is just a waste of the program's time. Surfaces are not meant to be destroyed every step.
     
    TomatoFantasyStudio likes this.
  11. Pfap

    Pfap Member

    Joined:
    Apr 30, 2017
    Posts:
    440
    Yea you're right, I missed that for some reason.
     
    TomatoFantasyStudio likes this.
  12. TomatoFantasyStudio

    TomatoFantasyStudio Member

    Joined:
    Dec 5, 2017
    Posts:
    32

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice