[solved] GET and PUT ( graphics ) commands slow down my program[solved]

New to FreeBASIC? Post your questions here.
Daniel Ouellette
Posts: 47
Joined: Apr 17, 2011 17:43
Location: Montréal, Canada
Contact:

Re: GET and PUT ( graphics ) commands slow down my program

Postby Daniel Ouellette » Aug 08, 2020 23:34

Thank you very much, I guess everything you just mentioned can't be done by a beginner like me, right?
xlucas
Posts: 287
Joined: May 09, 2014 21:19
Location: Argentina

Re: GET and PUT ( graphics ) commands slow down my program

Postby xlucas » Aug 08, 2020 23:37

If I understood correctly, you have a program that draws to a 1024x768 viewport area and you're trying two things. One is to set up a true 1024x768 full screen mode which, being your monitor 16:9, results in a stretched view of the program, but runs fast enough. The other is setting a full screen with the native resolution of your monitor (maybe 1366x768 or 1920x1080), filling most of the screen with one plain colour and PUTting each frame manually more or less in the centre of the viewport, thus getting a well-proportioned view, but much slower.

The first case is much faster because it's not FB stretching the screen. It is your graphics chip that's emulating 1024x768 or more likely, your monitor itself doing just that, so there's no extra work for the CPU. On the other hand, getting and putting each image means that you're transfering 16bit times 1024x768 twice for each frame. That's a lot of data coming and going all the time and the CPU is doing all the work because your original image has been "rendered" on the original viewport at a different resolution. Games achieve this easily because the rendering itself is made skipping the left and right margins and the pixels are only transfered once.

If you are able to access and modify the function that generates the original image, you could change it so that it skips the margins and you won't need to do the getting and putting, but I really don't know how simpler or complex that'd be for you. It depends on the code and you're saying you're not experienced with high level programming. Anyway, if you could do that, that would help a lot.

Another thing I'm thinking is.... if you're first doing a GET, how is the original image being placed in the buffer that you're GETting it from? Are you using FB primitives or accessing the buffer directly? All this could also have to do with the speed. As said before, it's hard to tell without knowing the very nature of the program and how it works.

On a sidenote, that's very interesting about your programming microcontrollers. I've been interested in them, but I'm not very skilled. I wanted to use a PIC32 to make a computer. It'll be lots of work, but lots of fun :)
BasicCoder2
Posts: 3577
Joined: Jan 01, 2009 7:03
Location: Australia

Re: GET and PUT ( graphics ) commands slow down my program

Postby BasicCoder2 » Aug 08, 2020 23:44

xlucas wrote:On a sidenote, that's very interesting about your programming microcontrollers. I've been interested in them, but I'm not very skilled. I wanted to use a PIC32 to make a computer. It'll be lots of work, but lots of fun :)

I used to use assembler all the time. Today you can program microprocessors using C (or even tiny Python) with the Arduino system.
My first computer was a kit using a simple microcomputer which you programmed in binary numbers via toggle switches with LED to display the binary numbers.
xlucas
Posts: 287
Joined: May 09, 2014 21:19
Location: Argentina

Re: GET and PUT ( graphics ) commands slow down my program

Postby xlucas » Aug 08, 2020 23:51

Ah, yeah! Well, the thing is... I don't know if it's your case or Daniel's, but I find assembly language beautiful and I'd take every chance to be able to do the programming in it. PCs nowadays have gone so far from low level programming! And that's precisely what made me interested in PICs, because I had been a PC only programmer for a long time. I would program the PIC32 in assembly and then, perhaps, make my own simpler higher level compiler. Zero 3rd party software :) That really is passion. Programming nowadays in the PC world has turned into a task of putting pieces together instead of creating them. That's boring!
Daniel Ouellette
Posts: 47
Joined: Apr 17, 2011 17:43
Location: Montréal, Canada
Contact:

Re: GET and PUT ( graphics ) commands slow down my program

Postby Daniel Ouellette » Aug 09, 2020 0:00

I always work on the same page throughout the program and only on a part that corresponds to 1024 X 768. At the end, I take this part and I put it in the middle, obviously the total resolution can only be higher or equal to 1024 X 768. You surely understood it, unless it is me who did not understand you. The program is too long and too complex to offset each section.
Daniel Ouellette
Posts: 47
Joined: Apr 17, 2011 17:43
Location: Montréal, Canada
Contact:

Re: GET and PUT ( graphics ) commands slow down my program

Postby Daniel Ouellette » Aug 09, 2020 0:04

I am an industrial electronics designer and therefore I also program microcontrollers in assembler language.
Daniel Ouellette
Posts: 47
Joined: Apr 17, 2011 17:43
Location: Montréal, Canada
Contact:

Re: GET and PUT ( graphics ) commands slow down my program

Postby Daniel Ouellette » Aug 09, 2020 0:09

A microcontroller is much simpler than a PC.
Daniel Ouellette
Posts: 47
Joined: Apr 17, 2011 17:43
Location: Montréal, Canada
Contact:

Re: GET and PUT ( graphics ) commands slow down my program

Postby Daniel Ouellette » Aug 09, 2020 0:23

I only program one type of microcontroller in particular and have never worked with the others. My skills are limited to electronics and assembler language because of the applications that require it.

Coming back to Freebasic, do you think my method with GET and PUT is correct to keep it simple?
BasicCoder2
Posts: 3577
Joined: Jan 01, 2009 7:03
Location: Australia

Re: GET and PUT ( graphics ) commands slow down my program

Postby BasicCoder2 » Aug 09, 2020 0:33

@xlucas,

Sure I loved assembler (Z80, 6502, 68000 and so on) and wrote a simple editor/assembler for the c64 but you no longer have direct access to the hardware on a modern computer thus no speed advantage you are just calling high level routines from assembler.

Unfortunately PIC32 is simply unable to do certain things so it depends what you want your programs to do. Clearly Daniel Ouellette can't program his project on a PIC32.

I am sure if Daniel Ouellette could provide a high level (human language) description of his desired outcome there would be those here that could provide a solution.
BasicCoder2
Posts: 3577
Joined: Jan 01, 2009 7:03
Location: Australia

Re: GET and PUT ( graphics ) commands slow down my program

Postby BasicCoder2 » Aug 09, 2020 0:36

Daniel Ouellette wrote:I only program one type of microcontroller in particular and have never worked with the others. My skills are limited to electronics and assembler language because of the applications that require it.

But what is this application that requires a PC?
Coming back to Freebasic, do you think my method with GET and PUT is correct to keep it simple?

A high level description (human language, diagrams and so on) of what the project is would make it easy to answer that one.
Daniel Ouellette
Posts: 47
Joined: Apr 17, 2011 17:43
Location: Montréal, Canada
Contact:

Re: GET and PUT ( graphics ) commands slow down my program

Postby Daniel Ouellette » Aug 09, 2020 0:55

In fact, the PC is only used by me to diagnose one of the products I designed. I've never used GET before with such a large surface (1024 X 768) and it slows it down a lot and bothers me a bit. If you think that to be faster, I need a lot more complexity, I'll settle for that. It was just to know if I did it correctly.
xlucas
Posts: 287
Joined: May 09, 2014 21:19
Location: Argentina

Re: GET and PUT ( graphics ) commands slow down my program

Postby xlucas » Aug 09, 2020 2:16

This is the thing. You're first reading with GET from the screen or from a buffer?

The previous part of the program is somehow writing to the upper-left-most corner of the screen or buffer, then using a GET and a PUT after that is moving a lot of bytes twice. If you were to do it only once, that'd be fast, but because you do it for every frame, it's a lot. There is no way around that as long as you perform this move operation. What you should try to do is have the previous code somehow target that part of the screen directly, but I don't know if it's going via the screen or via a buffer and if it's being written using PSet, Line, Circle, etc., o via direct accesses to memory, like buffer[100 + 32 * x] = c. Whatever the case, the idea is adding an offset to the X part and, if it's direct access to memory, you also have to watch out for screen width to add the corresponding Y offset. This way, it would only be one transfer.

Also, I don't know the actual case, so I don't understand why you have to use full screen. Maybe it'd be easier if you could simply create a Window of size 1024x768, so you wouldn't have to move the image (you can just move the window) and there wouldn't be any additional transfer. In GNU/Linux, there exists another solution, so maybe this same thing can be done in Windows too, but I don't know. Anyway, the solution is not simple. X11 (the window managing server in GNU/Linux) allows to create windows within windows. This is rarely seen, but can be done. If you create a borderless window inside the full screen window, you place it where you need it and you don't need to copy and paste anymore. I wonder if something similar can be done in Windows. But even if you can do this, you have to call system functions that I don't know. It's not just FreeBasic.
BasicCoder2
Posts: 3577
Joined: Jan 01, 2009 7:03
Location: Australia

Re: GET and PUT ( graphics ) commands slow down my program

Postby BasicCoder2 » Aug 09, 2020 4:07

xlucas wrote:This is the thing. You're first reading with GET from the screen or from a buffer?

The code snippet shows GET as getting it from the screen and then placing it back on the screen.
Here is a working version of the code snippet provided.

Code: Select all

#Include "fbgfx.bi"
Using fb

dim as integer w,h,depth

ScreenInfo w,h,depth

ScreenRes w,h,16,2,GFX_NO_FRAME

ScreenSet 1,0

'fill image with stuff
dim as integer x,y,r
for i as integer = 0 to 50
    x = int(rnd(1)*w)
    y = int(rnd(1)*h)
    r = int(rnd(1)*50)+20
    circle (x,y),r,rgb( int(Rnd(1)*256), int(rnd(1)*256), int(rnd(1)*256)),,,,f
next i
line (1,1)-(1023,767),rgb(255,255,255),b

flip 1,0

sleep

dim shared as any ptr img5
img5 = imagecreate(1024,768)

Get (1,1)-(1023,767),img5
Cls
Put ((w-1024)/2,(h-768)/2), img5, Pset
Flip 1,0

sleep
badidea
Posts: 2126
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: GET and PUT ( graphics ) commands slow down my program

Postby badidea » Aug 09, 2020 8:38

Daniel Ouellette wrote:Get (1,1)-(1023,767),img5

Small remark, I think you want:
Get (0,0) - (1023,767), img5

With the code below I get ~200 fames/sec (~300 without the sleep 1).
Which is ~5 ms and not so bad considering the amount to bytes that are copied around.

Code: Select all

#include "fbgfx.bi"
using FB

dim as integer wDesk, hDesk, dDesk
screenInfo wDesk, hDesk, dDesk
screenRes wDesk, hDesk, 16, 2, GFX_NO_FRAME
width wDesk \ 8, hDesk \ 16 'larger font
screenSet 1,0

const wWin = 1024, hWin = 768
dim as any ptr pImg = imageCreate(wWin, hWin)

dim as integer count = 0
dim as double tStart = timer
while inkey = ""
   locate 1, 1
   print "Desktop: " & wDesk & " x " & hDesk & " x " &  dDesk
   print "Count: " & count
   print "Time: "& timer - tStart
   print "Frames/sec: " & cint(count / (timer - tStart))
   'copy part of screen to buffer
   get(0, 0) - (wWin-1, hWin-1), pImg
   'erase the entire screen
   cls
   'copy buffer to center of screen
   put((wDesk - wWin) \ 2, (hDesk - hWin) \ 2), pImg, pset
   'copy to visible page (same as pcopy 1, 0)
   flip 1, 0
   count +=1
   sleep 1
wend

If your code looks like this ten be aware that there is a lot of data copied / manipulated at multiple steps:
* cls clears the entire desktop sized area. This can be optimized; only a part (left and top) need to be cleared.
* flip does not do a 'hardware flip', but does the same as pcopy (if I understand the documentation well).
* The copy to a from the image buffer (pImg) can be omitted, with an algorithm still to be written. Just copy pixels from top-left to center (in the right order!). I'll see if I can write that...
* Also using 16 bits might slows things down. Less memory is using, but conversion to 32 bit colors is done somewhere.

Edit, this works, but is even slower (moving data by 16 bits in reverse direction is not efficient):

Code: Select all

#include "fbgfx.bi"
using FB

'only for moving in down+right direction, 16-bit
sub move16(xSrc as integer, ySrc as integer, xDst as integer, yDst as integer, wSect as integer, hSect as integer)
   dim as ushort ptr pSrc, pDst, pScrn = screenPtr()
   dim as integer wScrn, hScrn, dScrn, bpp, pitch
   screenInfo wScrn, hScrn, dScrn, bpp, pitch
   pitch \= 2
   pDst = pScrn + pitch * (yDst + hSect - 1) + (xDst + wSect - 1)
   pSrc = pScrn + pitch * (ySrc + hSect - 1) + (xSrc + wSect - 1)
   for y as integer = 0 to hSect - 1
      for x as integer = 0 to wSect - 1
         *pDst = *pSrc
         pDst -= 1
         pSrc -= 1
      next
      pDst -= (pitch - wSect)
      pSrc -= (pitch - wSect)
   next
end sub

'-------------------------------------------------------------------------------

dim as integer wDesk, hDesk, dDesk
screenInfo wDesk, hDesk, dDesk
screenRes wDesk, hDesk, 16, 2, GFX_NO_FRAME
width wDesk \ 8, hDesk \ 16 'larger font
screenSet 1,0

const wWin = 1024, hWin = 768
dim as any ptr pImg = imageCreate(wWin, hWin)

dim as integer count = 0
dim as double tStart = timer
while inkey = ""
   locate 1, 1
   print "Desktop: " & wDesk & " x " & hDesk & " x " &  dDesk
   print "Count: " & count
   print "Time: "& timer - tStart
   print "Frames/sec: " & cint(count / (timer - tStart))
   'copy part of screen to buffer
   'get(0, 0) - (wWin-1, hWin-1), pImg
   'erase the entire screen
   'cls
   'copy buffer to center of screen
   'put((wDesk - wWin) \ 2, (hDesk - hWin) \ 2), pImg, pset

   move16(0, 0, (wDesk - wWin) \ 2, (hDesk - hWin) \ 2, wWin, hWin)
   'copy to visible page (same as pcopy 1, 0)
   flip 1, 0
   count +=1
   sleep 1
wend
Daniel Ouellette
Posts: 47
Joined: Apr 17, 2011 17:43
Location: Montréal, Canada
Contact:

Re: GET and PUT ( graphics ) commands slow down my program

Postby Daniel Ouellette » Aug 09, 2020 13:53

Hi badidea, thanks for all the work. If I understood so well, did you mention that you tried another method, but it's even slower?

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 3 guests