Inaccurate vertical blank timing (VBL)

This forum is for general developer support questions.
User avatar
jostein_aarbakk
Beta Tester
Beta Tester
Posts: 37
Joined: Thu Aug 04, 2011 11:08 pm
Location: Norway

Re: Inaccurate vertical blank timing (VBL)

Post by jostein_aarbakk »

@Niels:
nbache wrote: Is LimpidClock showing seconds? Analog or digital clock (or even both)? Could be interesting to see if anything changes if you turn off seconds in LC.
- Not showing seconds
- Both analog and digital
- Task priority of LC: -1
- CPU usage during the tests was constantly only 3-5%
- Switching off seconds did not make any visual difference to my scroller
- Digital only gave less disturbances to my scroller than analog+digital
User avatar
jostein_aarbakk
Beta Tester
Beta Tester
Posts: 37
Joined: Thu Aug 04, 2011 11:08 pm
Location: Norway

Re: Inaccurate vertical blank timing (VBL)

Post by jostein_aarbakk »

@Hans:
Thanks for your feedback.

Regarding CPU-hogging:
- Only Limpid clock and CPUInfo were running in the background.
- Low CPU usage: CPUInfo showed an idle CPU usage during all my tests of only 3-5%.
- Switching off 3D effects seemed to help a bit, but it might be a coincidence.
- All drives uses DMA (although, there were no disk activity while running the tests)
- Limpid clock had priority -1 in all my tests, -5 in some. It didn't remove the issues.
- I played around with task priorities on the scroller, and tried up to +30. It got better (solved the issue completely in many cases, but not all).

A theory could be that the running tasks not necessarily uses much CPU, but accidently runs in the time-critical period when the vertical blanking occurs, which lead to a slight delay.
I don't know.


Idea:
My personal opinion is that operations that has to do with graphics and audio should never be disturbed in a way that makes the disturbance visible/audible for the user.
And all this should be handled automatically by the OS.
This is one of the things I like so much about the Classic Amiga system (both the HW DMA channel priorities + software interrupt priorities).

F.ex.: What if exec was fine-tuned to always prioritize vertical blank interrupts before anything else?
So, if the CPU is working on a task, and such an interrupt occurs, that task should be "paused", and the blanking task should start immediatly.
When that interrupt task is finished, the "paused" task should start again.

This way, we don't risk that tasks that accidently are running while the screen needs to be refreshed, prevents timing-critical tasks like vertical blanks.

Could this be doable?
Or maybe it works like this already, and just needs some "fine tuning"?
(It would be very cool if it could).

Hans wrote: Do you adapt your animation to the frame-rate? If not, it's worth doing....
...it's worth reading this physics tutorial.
Interesting read. I will have this in mind when I reach a bottleneck in my game.
Until now, my strategy has been to render each frame fast enough to make it ready for each vertical blank.

Hans wrote: Composite3DDemo
This is cool! Impressive that's possible to achieve 3D effects by using 2D graphics operations.

Hans wrote: Lack of examples for smooth animation.. Something simpler and in plain C would definitely be worth having in the SDK.
Agreed.
Working examples is a very good thing, I think; Both simple and advanced ones.
And books also, which explains the conceptual ideas, how things plays together, and recommended practices.
Examples have helped me a lot, as well as books like the Amiga ROM Kernel reference manuals, which helps to see the "big picture" and get a better understanding.
If only there existed such books updated for OS4 on NG Amigas..
And of course; Examples and articles all those nice people on the internet makes available for others.

Actually, one of the triggers that made me start working on my game (in addition to really get to know how to program OS4), was to exploit the possibilities for making smooth, hardware accellerated 2D operations with almost zero CPU usage.
And I wanted to program towards native Amiga libraries, instead of using frameworks like SDL (which I have no interest in).
This is the only way I could be sure I got the most out of the hardware (better control, less overhead).
Well, not 100% sure, as "hitting the hardware" directly isn't allowed anymore (although it would have been really, really cool just to have tried it on my Sam, just to getting to know its limitations better and how the hardware works "under the hood").

On my Sam, I have seen several simple 2D-scrolltexts consuming 100% CPU, which I found very strange.
I wanted to find out if this happened when using "classic" native OS4 library calls like graphics.library also.
I got really motivated when I saw that you could do blitting operations without consuming CPU at all, just like on the classic.
Actually; It was possible to blit the whole screen several times, so those OS-procedures seems to work quite good.
This proved to me that those specific 2D graphics procedures were HW accelerated.
User avatar
tonyw
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 1479
Joined: Wed Mar 09, 2011 1:36 pm
Location: Sydney, Australia

Re: Inaccurate vertical blank timing (VBL)

Post by tonyw »

What if exec was fine-tuned to always prioritize vertical blank interrupts before anything else?
So, if the CPU is working on a task, and such an interrupt occurs, that task should be "paused", and the blanking task should start immediatly.
When that interrupt task is finished, the "paused" task should start again.
That's more or less what happens now. To simplify the description, the interrupt is serviced by Exec checkpointing whatever is running and calling the Interrupt Service Routine (ISR). The ISR does whatever it has to do at the priority set by the background program (the program that did the AddInterrupt() call). If there are several interrupts waiting, they are serviced in order of priority. That's interrupt priority, not task priority, the two are independent.

You may consider vertical blank interrupts more imperative than others, but then so are interrupts from HDDs, USB or Ethernet HW. It depends which hat you've got on at the time.
cheers
tony
User avatar
Hans
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 703
Joined: Tue Dec 21, 2010 9:25 pm
Location: New Zealand
Contact:

Re: Inaccurate vertical blank timing (VBL)

Post by Hans »

@jostein_aarbakk
jostein_aarbakk wrote:@Hans:
Thanks for your feedback.

Regarding CPU-hogging:
- Only Limpid clock and CPUInfo were running in the background.
- Low CPU usage: CPUInfo showed an idle CPU usage during all my tests of only 3-5%.
- Switching off 3D effects seemed to help a bit, but it might be a coincidence.
- All drives uses DMA (although, there were no disk activity while running the tests)
- Limpid clock had priority -1 in all my tests, -5 in some. It didn't remove the issues.
- I played around with task priorities on the scroller, and tried up to +30. It got better (solved the issue completely in many cases, but not all).
Did you try disabling both Limpid clock and CPUInfo? What else does Ranger list in running tasks/processes?

Don't increase the scroller's priority to +30; that's way too high, and may result in you interfering with more important stuff.
F.ex.: What if exec was fine-tuned to always prioritize vertical blank interrupts before anything else?
So, if the CPU is working on a task, and such an interrupt occurs, that task should be "paused", and the blanking task should start immediatly.
When that interrupt task is finished, the "paused" task should start again.
Like Tony said, it already works that way. There may still be a way to improve it, but it's working just fine on my machines (which admittedly, are faster tham a Sam440, but that shouldn't matter).
On my Sam, I have seen several simple 2D-scrolltexts consuming 100% CPU, which I found very strange.
I wanted to find out if this happened when using "classic" native OS4 library calls like graphics.library also.
I got really motivated when I saw that you could do blitting operations without consuming CPU at all, just like on the classic.
Actually; It was possible to blit the whole screen several times, so those OS-procedures seems to work quite good.
This proved to me that those specific 2D graphics procedures were HW accelerated.
100% CPU usage for simple scrollers generally means that it's "busy waiting" instead of using OS signals or messages.

I still suspect that something is occasionally delaying your scroller's ChangeScreenBuffer() call till after the VBI. Do you still have any WaitTOF()s or WaitBOVP()s in your main loop? Without seeing your code, it's hard to tell if you've done something the "wrong" way (e.g., you're trying to do all of your rendering within the blanking period).

Hans
http://hdrlab.org.nz/ - Amiga OS 4 projects, programming articles and more. Home of the RadeonHD driver for Amiga OS 4.x project.
User avatar
jostein_aarbakk
Beta Tester
Beta Tester
Posts: 37
Joined: Thu Aug 04, 2011 11:08 pm
Location: Norway

Re: Inaccurate vertical blank timing (VBL)

Post by jostein_aarbakk »

@tonyw:
Ok. thanks for your reply.

Question:
Given that an interrupt occur; Are there any risk that running tasks aren't "set on hold" fast enough (a kind of delay)?
Last edited by jostein_aarbakk on Fri Apr 20, 2012 1:18 am, edited 2 times in total.
User avatar
jostein_aarbakk
Beta Tester
Beta Tester
Posts: 37
Joined: Thu Aug 04, 2011 11:08 pm
Location: Norway

Re: Inaccurate vertical blank timing (VBL)

Post by jostein_aarbakk »

@Hans:
Hans wrote: Did you try disabling both Limpid clock and CPUInfo?
Yes.
Hans wrote: What else does Ranger list in running tasks/processes?
GUI Objects:
-----------
Maindock


Exec tasks:
-----------
application.library messageserver
console.device
EHCI Controller Task Unit 0
hub.usbfd
hub.usbfd
hub.usbfd
idle.task
input.device
OHCI Controller Task Unit 0
OHCI Controller Task Unit 1
page_sweep
sii3114ide.device - chip 0 port 0
sii3114ide.device - chip 0 port 1
USB stack

DOS Processes:
--------------
MUI imagespace screen notify
Workbench -> FKey
Workbench -> ContextMenus -> ContextMenus Command Dispatcher
Workbench -> ContextMenus -> CON
Workbench -> ClickToFront
Workbench -> RAWBInfo
Workbench -> DefIcons -> CON
Workbench -> ASYNCWB
Workbench -> AmiDock -> CON
Workbench -> Workbench DosList Notify
Workbench -> Workbench Clipboard Server
ConClip
ppc440ep_eth Unit 0
TCP/IP Control
TCP/IP Superserver
TCP/IP Configuration
TCP/IP Log
AUDIO
TEXTCLIP
URL
hid.usbfd -> HID Keyboard
hid.usbfd -> HID Mouse
USB stack Process
RexxMaster
IPrefs -> datatypes.library
IPrefs -> DST watcher
Picasso96
ramlib -> BetterString.mcc clipboard server
ramlib -> texteditor.gadget Clipboard server
ramlib -> string.gadget server
ramlib -> ENV
ramlib.support
pager
reaper.task
CON
RAM
RAW
CON
JXF:DH3
DH2
DH1
DH6
DH5
DH4
CD0
DH0 -> SFS DosList handler
dos_appdir_server
dos_filedir_notify
dos_signal_server
dos_nbmd_process
dos_lock_handler
User avatar
Hans
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 703
Joined: Tue Dec 21, 2010 9:25 pm
Location: New Zealand
Contact:

Re: Inaccurate vertical blank timing (VBL)

Post by Hans »

jostein_aarbakk wrote:Question:
Given that an interrupt occur; Are there any risk that running tasks aren't "set on hold" fast enough (a kind of delay)?
No. When an interrupt occurs, any task that is running is immediately interrupted by the CPU. That's how the CPU is designed. The only thing that could delay an interrupt handler from, is another interrupt handler. However, interrupt handler's are for time-critical code (i.e., there's a reason why they keep running), and are supposed to be really short.

BTW, the VBI is only used to send the message that the buffer swap has occurred; the graphics card is instructed to do the swap immediately, and does so in the next VBlank period. The only way to delay the buffer swap is to delay the ChangeScreenBuffer() call.

Hans
http://hdrlab.org.nz/ - Amiga OS 4 projects, programming articles and more. Home of the RadeonHD driver for Amiga OS 4.x project.
Post Reply