Heavy lags in 6.21 Beta

Just starting out? Need help? Post your questions and find answers here.
Joubarbe
Enthusiast
Enthusiast
Posts: 711
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Heavy lags in 6.21 Beta

Post by Joubarbe »

EDIT: Bug report added: viewtopic.php?t=86886
-----------------------------------------------------------------

Following the little discussion I've started in the announcement channel, I wanted to just show you the part of the code that lags. It takes 20 ms to process in 6.20, and more than 130ms in 6.21. Also note that in 6.21, the CPU fan goes crazy, and I cannot ALT-F4 the game.

My game is drawn on a canvas, and I have implemented "views" which are panels that can be manipulated independently from each other; but the whole screen (terminal) is always updated in full (because one view can affect another one). If you look at screenshots of my game from my signature, you'll see that it doesn't need fancy graphics, that's why I chose to only use canvases (and it's theoretically portable to SB).

Also, the "terminal" is an array of symbol, and each cell depends on the font size. If you increase the font size, everything gets bigger.

The part that is laggy:

Code: Select all

StartDrawing(CanvasOutput(#CANVAS_MAIN))
DrawingFont(FontID(#FONT_MAIN))
; Clear the terminal.
Box(0, 0, OutputWidth(), OutputHeight(), settings\default_colors\terminal_background)
If \shade_opacity > 0
  DrawingMode(#PB_2DDrawing_AlphaBlend)
  Box(0, 0, OutputWidth(), OutputHeight(), RGBA(0, 0, 0, \shade_opacity))
  DrawingMode(#PB_2DDrawing_Default)
EndIf
ForEach \views()
  If \views()\visible = #True
    DrawView(\views())
  EndIf
Next
DrawView() function:

Code: Select all

Procedure DrawView(*view._View) : With *view
  Define fw = settings\font\width
  Define fh = settings\font\height
  Define width_px = \size\width * fw
  Define height_px = \size\height * fh
  Define origin_x_px = Riva::GetViewPosXPx(*view)
  Define origin_y_px = Riva::GetViewPosYPx(*view)
  Define view_bg
  Define font_alt_used.b
  
  ; Background.
  If \background_color = #DEFAULT
    view_bg = settings\default_colors\view_background
  Else
    view_bg = \background_color
  EndIf
  
  ; Clear the view, either with an image or the view background colour.
  If \image_alias$ = ""
    If \frame_rounded_px = 0
      Box(origin_x_px, origin_y_px, width_px, height_px, view_bg)
    Else
      RoundBox(origin_x_px, origin_y_px, width_px, height_px, \frame_rounded_px, \frame_rounded_px, view_bg)
    EndIf
  Else
    Define image = terminal\image_store(\image_alias$)\handle
    If *view\image_resize = #True And (ImageWidth(image) <> width_px Or ImageHeight(image) <> height_px)
      ResizeImage(image, width_px, height_px, terminal\image_store()\resize_mode)
    EndIf
    DrawAlphaImage(ImageID(image), origin_x_px, origin_y_px, \image_opacity)
  EndIf
  
  Define x, y
  Define pos._XY
  Define *cell._Cell
  
  If \transparent_control_bg = #True
    DrawingMode(#PB_2DDrawing_Transparent)
  EndIf
  
  For y = 0 To ArraySize(\pages()\grid(), 2)
    For x = 0 To ArraySize(\pages()\grid(), 1)
      *cell = \pages()\grid(x, y)
      If *cell\char$ <> ""
        pos\x = (x + Riva::GetViewPosX(*view)) * fw
        pos\y = (y + Riva::GetViewPosY(*view)) * fh
        
        If x = \margins\left And *cell\char$ = "─"
          ; Draw a separator.
          ; FIXME Will also draw a separator if the first character of any line of any control is "─".
          Define color
          If *view\frame_color > -1
            color = *view\frame_color
          Else
            If *view = GetActiveView()
              color = settings\default_colors\frame_active
            Else
              color = settings\default_colors\frame_inactive
            EndIf
          EndIf
          Define padding_left = *cell\color_fg
          Define padding_right = *cell\color_bg
          LineXY(pos\x - (\margins\left * fw) + (padding_left * fw), pos\y + fh/2, (Riva::GetViewPosX(*view) + \size\width - padding_right) * fw - 1, pos\y + fh/2, color)
          Continue
        EndIf
        
        ; EXPERIMENTAL Alternative font.
        If font_alt_used = #True And *cell\use_alt_font = #False
          DrawingFont(FontID(#FONT_MAIN))
          font_alt_used = #False
        ElseIf *cell\use_alt_font = #True And font_alt_used = #False
          DrawingFont(FontID(#FONT_ALT))
          font_alt_used = #True
        EndIf
        
        Define cf = *cell\color_fg
        Define cb = *cell\color_bg
        DrawText(pos\x, pos\y, *cell\char$, cf, cb)
        If settings\misc\shadowing_amount > 0
          DrawingMode(#PB_2DDrawing_AlphaBlend)
          DrawText(pos\x - settings\misc\shadowing_offset, pos\y, *cell\char$, RGBA(Red(cf), Green(cf), Blue(cf), settings\misc\shadowing_amount), 0)
          If \transparent_control_bg = #False
            DrawingMode(#PB_2DDrawing_Default)
          Else
            DrawingMode(#PB_2DDrawing_Transparent)
          EndIf
        EndIf
        If *cell\underlined = #True
          LineXY(pos\x, pos\y + fh - 1, pos\x + fw - 1, pos\y + fh - 1, cf)
        EndIf
      EndIf
    Next x
  Next y
  
  If *view\frame_visible = #True
    DrawFrame(*view)
  ElseIf ListSize(\pages()) > 1 And IsRequesterOpen() = #False ; Does not draw page numbers when a pop-up is opened.
    DrawPaging(*view)
  EndIf
  
  ; Shade opacity.
  If *view\shade_opacity > 0
    DrawingMode(#PB_2DDrawing_AlphaBlend)
    If *view\shade_opacity_bg = #False
      Box(origin_x_px, origin_y_px, width_px, height_px, RGBA(0, 0, 0, *view\shade_opacity))
    Else
      Box(origin_x_px, origin_y_px, width_px, height_px, RGBA(Red(view_bg), Green(view_bg), Blue(view_bg), *view\shade_opacity))
    EndIf
    DrawingMode(#PB_2DDrawing_Default)
  EndIf
  
  ;     DBG_DrawGrid(*view) ; DEBUG DrawGrid()
EndWith : EndProcedure
DrawFrame() and DrawPaging() have no effect, because even if I comment them out, the lag remains the same.

The problem here is I can comment everything out, and things will disappear, so I'm not really able to know what the cause is. If you only leave what is truly essential, which is DrawText(), then it still takes about 70ms to process. So there's definitely something internal that has changed.

I don't know what to do to further test this.
Last edited by Joubarbe on Thu May 08, 2025 9:26 am, edited 2 times in total.
pjay
Enthusiast
Enthusiast
Posts: 252
Joined: Thu Mar 30, 2006 11:14 am

Re: Heavy lags in 6.21 Beta

Post by pjay »

For sure the Drawtext() is a lot slower than before here in Windows 6.21 x64.

The below code runs in ~25ms per frame in v6.20, but ~155ms in v6.21b8.

Code: Select all

#width = 1024 : #Height = 768 : #TextCount = 1000
OpenWindow(0,0,0,#width / DesktopResolutionX(), #Height / DesktopResolutionX(),"Drawtext test",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
CanvasGadget(0,0,0,#width, #height)
OpenConsole()

Repeat
  
  Repeat
    event = WindowEvent()
    If event = #PB_Event_CloseWindow : exit = #True : EndIf
  Until event = 0
  
  time = ElapsedMilliseconds()
  StartDrawing(CanvasOutput(0))
    Box(0,0,OutputWidth(),OutputHeight(),RGBA(30,40,50,255))
     For x = 0 To #textCount
       DrawText(Random(#width),Random(#height),"Test text...")
     Next
  StopDrawing()
  time = ElapsedMilliseconds() - time
  PrintN("Time to draw: "+Str(#textCount)+" strings: "+Str(time)+"ms")
  
Until exit = #True
Joubarbe
Enthusiast
Enthusiast
Posts: 711
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Heavy lags in 6.21 Beta

Post by Joubarbe »

Thank you pjay for making a working code.

Bug report added: viewtopic.php?t=86886
Post Reply