Apparently AGK is very slow for 2d drawing :/

Started by Xaron, August 20, 2017, 14:54:38

Previous topic - Next topic

Xaron

Hey there,

just tried to draw some pixels on my good ol' Macbook Air but hell, AGK is SLOW!

Filling a 512*512 texture with random pixels (so roughly 260k pixels) takes about 200-400ms on my older MacBook Air (2011, core i7) using AppGameKit, where Monkey 2 takes 40ms and Monkey 1 aka Cerberus even takes only 10ms.

I've tested 3 possibilities:

1) Using DrawBox:
On my system it takes 320ms for drawing

2) Using SetMemblockByte 4 times:
On my system it takes 440ms for drawing

3) Using SetMemblockInt:
On my system it takes 280ms for drawing



// Project: test
// Created: 2017-08-20

// show all errors
SetErrorMode(2)

// set window properties
SetWindowTitle( "test" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window

// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts

XCreateMemblockForImageRGBA( 1, 512, 512 )
CreateImageFromMemblock( 1, 1 )
CreateSprite( 1, 1 )

do
  Local y, x As Integer
  Local t1, t2, t3 As Integer
  t1 = GetMilliseconds()
  Local col As Integer
  col = MakeColor(255,0,0)
  For y = 0 To 511
    For x = 0 To 511
      //DrawBox( x, y, x+1, y+1, col, col, col, col, 1 )       
      XDrawPixel( 1, x, y, 512, 255, 0, 0, 255 )
      //XDrawPixel2( 1, x, y, 512, 4278190335 )
    Next x
  Next y
  t2 = GetMilliseconds()
  CreateImageFromMemblock( 1, 1 )
  SetSpriteImage( 1, 1 )
  t3 = GetMilliseconds()
  Print( "FPS: " + Str( ScreenFPS() ) )
  Print( "Draw Pixel Time: " + Str( t2 - t1 ) + "ms" )
  Print( "Create Image Time: " + Str( t3 - t2 ) + "ms" )
  Sync()
loop

Function XCreateMemblockForImageRGBA( memId As Integer, width As Integer, height As Integer )
  // memblock 'size' is summary of image width (integer, 4 bytes),
  // image height (integer, 4 bytes), image depth (integer, 4 bytes)
  // that is 12 bytes - and raw image data (the number of pixels multiplied by 4 bytes)
  Local size As Integer
  size = 12 + width * height * 4
  CreateMemblock( memId, size )

  SetMemblockInt( memId, 0, width )  // writing starts with offset 0
  SetMemblockInt( memId, 4, height ) // variable 'width' occupies 4 bytes, so now offset 0 + 4 = 4
  SetMemblockInt( memId, 8, 32 )     // variable 'height' occupies 4 bytes, so now offset 4 + 4 = 8

  Local i As Integer
  For i = 12 To size-1 Step 4
    SetMemblockByte( memId, i, Random( 0, 255 ) )
    SetMemblockByte( memId, i+1, Random( 0, 255 ) )
    SetMemblockByte( memId, i+2, Random( 0, 255 ) )
    SetMemblockByte( memId, i+3, Random( 0, 1 ) )
  Next i
EndFunction

Function XDrawPixel( memId As Integer, x As Integer, y As Integer, w As Integer, r As Integer, g As Integer, b As Integer, a As Integer )
  Local dst As Integer
  dst = 12+4*(y*w+x)
  SetMemblockByte( memId, dst, r )
  SetMemblockByte( memId, dst+1, g )
  SetMemblockByte( memId, dst+2, b )
  SetMemblockByte( memId, dst+3, a )
EndFunction

Function XDrawPixel2( memId As Integer, x As Integer, y As Integer, w As Integer, rgba As Integer )
  Local dst As Integer
  dst = 12+4*(y*w+x)
  SetMemblockInt( memId, dst, rgba )
EndFunction

Xaron

Did another test with C++ and wow, it only takes 20ms now! So that Basic stuff seems to be good until you do some heavy stuff. ;)

Steve Elliott

#2
So you mean you tested the AGK Tier 2 (C++) and it was much faster for heavy lifting?
Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

MikeHart


Steve Elliott

Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

Rick Nasher

Luv the code anyhow Xaron. This example might come in handy sometime for non-time-critical functions, so thanks for sharing.
_______________________________________
B3D + physics + shaders + X-platform = AGK!
:D ..ALIENBREED *LIVES* (thanks to Qube).. :D
_______________________________________

Steve Elliott

#6
Yes thanks for sharing.

I'm learning AGK too and will be starting a small game soon now I know the basics, and got it working nicely in C++.
Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

Xaron

Haha thanks,

never thought that this piece of crap would be interesting for anyone.  :o

Anyway, now that I know there is a fast way of doing this by just using C++ this might be indeed useful. I think I might make a game with AGK. It's a nice toolset, I just love C++ (doing this by profession for ages now) and love Visual Studio as IDE anyway, especially together with Visual Assist.

Steve Elliott

#8
I agree, started my game today using the C++/AGK Combination.  Look forward to see what you produce  :)
Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

MikeHart

#9
When MX was dead i was preaching AgK to Xaron constantly. "Look into this. It is awesome". Typical complain from him: "It can't do OOP".  :D

Steve Elliott

lol, talking of games, how's your AGK Game coming Mike?
Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

MikeHart

No real progress atm. I spending my time on CX related stuff and preparing myself for quite some artwork for a game Xaron and I want to create.

Steve Elliott

A shame, but at least you're working on a game.
Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

MikeHart

Quote from: Steve Elliott on August 24, 2017, 11:33:28
A shame, but at least you're working on a game.

I am not worried. That game will be finished.

Steve Elliott

Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb