PageRenderTime 60ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/Psychtoolbox/PsychDocumentation/SyncTrouble.m

https://github.com/theOfficialJuan/Psychtoolbox-3
Objective C | 405 lines | 404 code | 1 blank | 0 comment | 73 complexity | 6ae0323247529c6da920986bad923bf9 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0, LGPL-2.0, GPL-2.0, AGPL-1.0
  1. % SyncTrouble -- Causes and solutions for synchronization problems.
  2. %
  3. % You most probably arrived at this help page because Psychtoolbox
  4. % aborted with "SYNCHRONIZATION FAILURE" or a similar error message and
  5. % asked you to read this page.
  6. %
  7. % BACKGROUND: Why proper synchronization to retrace is important.
  8. %
  9. % When executing Screen('OpenWindow'), Psychtoolbox executes different
  10. % calibration routines to find out if back- and frontbuffer swaps
  11. % (what Screen('Flip') does) are properly synchronized to the vertical
  12. % retrace signal (also known as VBL) of your display. At the same time,
  13. % it measures the real monitor video refresh interval - the elapsed time
  14. % between two VBL signals. It is crucial for flicker free, tear free,
  15. % properly timed visual stimulus presentation that buffer swaps only
  16. % happen during the VBL period of the display. The VBL (vertical blank) is
  17. % the small gap in time that occurs when the display has updated its last scanline
  18. % and before it starts redrawing its display surface starting at the first
  19. % scanline again. This small gap is a neccessity for CRT displays and it
  20. % is preserved for compatibility reasons or other technical reasons on flat
  21. % panels and video beamers. After issuing the Screen('Flip') command, the
  22. % graphics hardware finalizes all pending drawing- and imageprocessing
  23. % operations in the backbuffer of an onscreen window to make sure that the
  24. % final stimulus image is ready in the backbuffer for presentation. Then
  25. % it waits for onset of the next VBL interval before flipp'ing the back-
  26. % and frontbuffer of the onscreen window: The previous backbuffer with your
  27. % newly drawn stimulus becomes the frontbuffer, so it will get scanned out
  28. % and displayed to the subject, starting with the next refresh cycle of your
  29. % display device and on all consecutive refresh cycles, until you draw a new
  30. % stimulus to the onscreen window and update the display again via Screen('Flip').
  31. %
  32. % On a properly working system, this double buffer swap happens in less than
  33. % a microsecond, synchronized to VBL onset with an accuracy of better than a
  34. % microsecond. All change of visual content therefore only happens during
  35. % the VBL period when the display is not updating, thereby avoiding any kind of
  36. % visual flicker or tearing that would be caused by a mixup of an old stimulus and
  37. % a new (incompletely drawn) stimulus when changing image content during the
  38. % scanout cycle of the display. The exact point in time when this
  39. % buffer swap happened, is returned as timestamp by the Screen('Flip') command.
  40. % It is the most well defined timestamp of visual stimulus onset, and it allows to
  41. % define stimulus onset of future stims relative to this accurate baseline,
  42. % using the 'when' argument of Screen('Flip').
  43. %
  44. % Without proper synchronization, you would see very strong visual flicker and
  45. % tearing artifacts in animated (movie / moving) stimuli, you would not have any
  46. % well defined stimulus onset for sequences of static stimuli or rapid stimulus
  47. % presentation, and no means of synchronizing visual stimulus presentation to any
  48. % external stimulation- or acquisition devices like fMRI, EEG, sound, ... You also
  49. % would not have any accurate way of getting a stimulus onset timestamp.
  50. %
  51. % However, if you have very special needs, you can disable either Matlabs / Octaves
  52. % synchronization of execution to the vertical retrace or you can disable synchronization
  53. % of stimulus onset to the vertical retrace completely by setting the 'dontsync' flag
  54. % of Screen('Flip') accordingly.
  55. %
  56. % For more infos about tearing, see Wikipedia articles about "Tearing", "Double buffering",
  57. % "Vertical Synchronization" and the info pages on www.psychtoolbox.org
  58. %
  59. % TESTS: How Psychtoolbox tests for proper synchronization to retrace.
  60. %
  61. % After opening an onscreen window, Psychtoolbox executes a measurement loop,
  62. % where it issues Screen('Flip') commands and measures the time elapsed between
  63. % execution of two consecutive flip commands, getting one refresh sample per
  64. % loop iteration. Each sample is checked for validity: Duration must be longer than
  65. % 4 milliseconds and shorter than 40 milliseconds, because we assume that none of
  66. % the available display devices updates slower than 25 Hz or faster than 250 Hz. Each
  67. % sample is also tested against the expected value provided by the operating system, e.g.,
  68. % if the operating system reports a nominal refresh rate of 100 Hz, then a sample should have
  69. % a duration of roughly 1000 ms / 100 Hz == 10 milliseconds. We accept any sample in a
  70. % range of +/- 20% around this expected value as valid, because timing jitter present in
  71. % any computer system can cause some deviation from the expected value. Samples that don't
  72. % pass this basic test are rejected. Valid samples are used to update a mean value, standard
  73. % deviation of the mean is also calculated: The measurement loop ends when at least 50 valid
  74. % samples have been taken and the standard deviation from the mean is less than 200 microseconds.
  75. % If it is not possible to satisfy this criteria during a five second measurement interval, then the
  76. % calibration is aborted and repeated for up to three times. Failure to get a valid measurement
  77. % during up to three calibration runs is indicating massive timing problems or the inability
  78. % of the gfx-hardware to properly synchronize buffer swaps to the vertical retrace. This leads
  79. % to abortion with the "SYNCHRONIZATION FAILURE" error message.
  80. % Assuming that this calibration loop did provide a valid mean measurement of monitor refresh,
  81. % the value is checked against the value reported by the operating system and - on MacOS-X - against
  82. % the result of an independent measurement loop that uses direct queries of rasterbeam positions
  83. % to measure the monitor refresh interval. Only if all available measurements yield similar results,
  84. % the test is finally rated as PASSED, Psychtoolbox continues execution and the computed monitor
  85. % refresh interval is used internally for all built-in timing checks and for properly timed
  86. % stimulus presentation.
  87. %
  88. % REASONS FOR FAILING THE SYNC TESTS AND HOW TO FIX THEM:
  89. %
  90. % There are multiple classes of possible causes for sync failure. Work down this
  91. % list of causes and solutions down until your problem is resolved or you
  92. % hit the bottom of the list:
  93. %
  94. % 1. Wrong configuration settings: This usually only affects MS-Windows
  95. % systems, where the display settings control panel for your graphics card
  96. % allows to customize a couple of graphics driver parameters. Some of these
  97. % settings can cause sync failure if they are wrong:
  98. %
  99. % -> Make sure the "Synchronize bufferswaps to the vertical retrace" option
  100. % is set to "Application controlled" or "Application controlled, default to
  101. % on". The wording of the option differs between different graphics cards,
  102. % search for something like that. Examples of other names: "Wait for
  103. % vertical sync", "Wait for vertical refresh" ...
  104. % If this setting is forced to off and *not* application controlled, then
  105. % the sync tests will fail because the hardware doesn't synchronize its
  106. % image onset (bufferswap) to the video refresh cycle of your display.
  107. %
  108. % -> Make sure the "Triple buffering" setting is off, or if you can select
  109. % some "Multibuffering" setting, that it is set to "double buffering" or
  110. % "wait for 1 video refresh" or "swap every refresh". This option may not
  111. % exist, but if it does, any other setting will cause the sync tests to
  112. % possibly succeed, but later stimulus onset timestamping to fail with
  113. % errors.
  114. %
  115. % -> If there is an option "Buffer swap mode" or "Bufferswap strategy", it
  116. % should be set to "Auto select" or "Page flipping" or "Exchange buffers".
  117. % The so called "Copy buffers" or "Blitting" option would result in lower
  118. % performance and inaccurate timing.
  119. %
  120. % -> On dual/multi display setups MS-Windows allows you to assign one
  121. % monitor the role of the "primary monitor" or "primary display". It is
  122. % important that the display device which you use for stimulus presentation
  123. % is the "primary display", otherwise random things may go wrong wrt. sync
  124. % tests and timing.
  125. %
  126. % -> If you have the choice to set your multi-monitor configuration to
  127. % either "dual display mode"/"dual display performance mode"/"separate
  128. % displays" or instead to "extended desktop mode" or "horizontal spanning",
  129. % you should choose "extended desktop mode" or "horizontal spanning" modes
  130. % for best timing and stimulus quality. Please note that this choice
  131. % doesn't exist anymore on Windows-Vista and later.
  132. %
  133. % -> On all operating systems in dual display or multi display mode it is
  134. % important that you configure both displays for exactly the same color
  135. % depths, resolution and refresh rate. If there is some option you can
  136. % choose for "genlocked modes" or "genlocked modes only", choose or enable
  137. % that one. Failing to configure dual display setups like this will cause
  138. % massive timing problems or tearing artifacts on one of the display if you
  139. % do dual display stimulation. It may also cause failures in timetamping.
  140. %
  141. %
  142. % 2. Temporary timing glitches or system malfunction: It may help to
  143. % restart Matlab/Octave, or to reboot your machine. Sometimes this resolves
  144. % intermittent problems on your system, especially after the system was
  145. % running without reboot for a long time, on high load, or if display
  146. % settings or display configuration has been changed frequently.
  147. %
  148. %
  149. % 3. Driver bugs: Many graphics card device drivers have bugs that cause
  150. % synchronization to fail. If none of the above steps resolves your
  151. % problems, check the website of your computer vendor or graphics card
  152. % vendor, or use the "Check for driver updates" function of some operating
  153. % systems to find out if new, more recent graphics drivers have been
  154. % released for your graphics card. If so, update to them. A tremendeously
  155. % large number of problems can be resolved by a simple driver update!
  156. %
  157. % 4. Driver/Hardware limitations:
  158. %
  159. % Many systems can't provide reliable research grade timing if you don't
  160. % display your stimuli in fullscreen windows, but use windowed mode
  161. % instead. This can lead to sync failures, problems with timestamping and
  162. % other performance problems. Only use non-fullscreen windows for
  163. % development, debugging and leisure, not for running your studies!
  164. %
  165. % Some systems have serious problems if more than one graphics card is
  166. % connected and enabled on the computer. They only work well in
  167. % single-display mode or dual display mode from a single dual-output
  168. % graphics card.
  169. %
  170. % Microsoft Windows Vista and Windows-7 may provide poor performance on
  171. % dual display setups if you present on both displays simultaneously,
  172. % although your mileage may vary widely depending on exact setup.
  173. %
  174. % On Vista and Windows-7, you may run into drastic timing and performance
  175. % problems if the stimulus presentation window loses the "keyboard focus".
  176. % The window with the "keyboard focus" is the one which is in the
  177. % foreground (in front of all other windows), has its titlebar highlighted
  178. % instead of shaded (assuming it has a titlebar) and receives all keyboard
  179. % input, i.e., key presses. Therefore we assign "keyboard focus" to our
  180. % onscreen windows automatically. However, if the user clicks into windows
  181. % other than our window with the mouse, or onto the desktop background, or
  182. % uses key combos like ALT+TAB or Windows+TAB to switch between windows,
  183. % our window will "lose" the keyboard focus and severe timing and
  184. % performance problems may occur. Obviously if any window on the screen is
  185. % highlighted, this means it *has stolen* the keyboard focus from our
  186. % window. This weird keyboard focus problem is an unfortunate design
  187. % decision (or rather design flaw) of the Windows Vista/Win-7 graphics
  188. % subsystem. There isn't anything we or the graphics cards vendors could do
  189. % about it, so you'll have to accept it and work-around it. Of course this
  190. % becomes mostly a problem on dual-display setups where one display shows
  191. % the desktop and GUI, so avoid such configurations if you can.
  192. %
  193. % Further examples:
  194. %
  195. % On most systems, graphics adapters are only capable of properly timed
  196. % bufferswaps and proper visual stimulus onset timestamps syncing to
  197. % retrace if the onscreen window is a full-screen window. Synchronization
  198. % fails if the onscreen window only covers part of the screen (i.e., when
  199. % providing a 'rect' argument to Screen('OpenWindow') other than the
  200. % default full-screen rect). Solution is to only use fullscreen windows for
  201. % stimulus presentation. On Windows, Linux and MacOS-X, some graphics cards
  202. % (e.g., many - if not all - mobile graphics cards built into Laptops) are
  203. % only capable of synchronizing to the retrace of one display. On a single
  204. % display setup, this will simply work. On a dual display setup, e.g.,
  205. % Laptop connected to external video beamer or CRT, the driver/hardware can
  206. % sync to the wrong output device. A simple, although inconvenient solution
  207. % is to disable the internal flat panel of a Laptop while running your
  208. % study, so the hardware is guaranteed to sync to the external display.
  209. % Depending on the hardware it may also help to try dual display output
  210. % with either: Non-mirror mode, running both displays at different refresh
  211. % rates, mirror mode running both displays at different rates, mirror mode
  212. % running both displays at exactly the same resolution, color depth and
  213. % refresh rate. You'll have to try, as it has been found to be highly
  214. % dependent on hardware, driver and operating system, which combinations
  215. % work and which don't.
  216. %
  217. % 5. Graphics system overload: If you ask too much from your poor graphics
  218. % hardware, the system may enter a state where the electronics is not
  219. % capable of performing drawing operations in hardware, either because it
  220. % runs out of video memory ressources, or because it is lacking the
  221. % neccessary features. In that case, some drivers (e.g., on Microsoft
  222. % Windows or MacOS-X) may activate a software rendering fallback-path: The
  223. % graphics engine is switched off, all rendering is performed by slow
  224. % software in system memory on the cpu and the final image is copied to the
  225. % onscreen framebuffer. While this produces visually correct stimuli,
  226. % presentation timing is completely screwed and not synchronized to the
  227. % monitors refresh at all. On Microsoft Windows, Psychtoolbox will detect
  228. % this case and output some warnings to the Matlab window.
  229. %
  230. % Possible causes of such an overload: Running with Anti-Aliasing enabled
  231. % at a setting that is too high for the given screen resolution (see 'help
  232. % AntiAliasing'), or running at a display resolution that is too high,
  233. % given the amount of video memory installed on your graphics adapter.
  234. % There may be other cases, although we didn't encounter any of them up to
  235. % now. The same could happen if you run a dual display setup that is not
  236. % switched to mirror-mode (or clone mode), so you take up twice the amount
  237. % of video memory for two separate framebuffers.
  238. %
  239. % Troubleshooting: Try lower display resolutions and multisampling levels,
  240. % switch dual display setups into mirror-mode if possible, or buy a
  241. % graphics adapter with more onboard memory.
  242. %
  243. % 6. General system overload: If you run too many applications on your
  244. % system in parallel to your Psychtoolbox+Matlab/Octave session, then these
  245. % applications may cause significant timing jitter in your system, so the
  246. % execution of Psychtoolbox - and its measurement loops - becomes
  247. % non-deterministic up to the point of being unuseable.
  248. %
  249. % Troubleshooting: Quit and disable all applications and services not
  250. % needed for your study, then retry. The usual suspects are: Virus
  251. % scanners, applications accessing the network or the harddiscs,
  252. % applications like iTunes, system software update...
  253. %
  254. % 7. Bad drivers or hardware in your system that interferes with general
  255. % system timing: This is difficult to diagnose. At least on MS-Windows, you
  256. % can download a free tool "dpclat.exe" from the internet (Use Google to
  257. % find it). If you run it, it will tell you if there are potential problems
  258. % with your systems timing and give hints on how to resolve them.
  259. %
  260. % 8. Other: Search the FAQ pages on the www.psychtoolbox.org Wiki and (via
  261. % Google search) the Psychtoolbox forum for other problems and solutions.
  262. %
  263. % 9. If everything else fails, post on the forum for help, but read our
  264. % instructions on how to ask questions on the forum properly. You can find
  265. % these instructions on the "Forum" and "Bugs" pages of our Wiki. If we
  266. % find that you didn't read the instructions and you're basically wasting
  267. % our time due to your omissions, we will simply ignore your request for
  268. % help.
  269. %
  270. %
  271. % HOW TO OVERRIDE THE SYNC TESTS:
  272. %
  273. % That all said, there may be occassions where you do not care about
  274. % perfect sync to retrace or millisecond accurate stimulus presentation
  275. % timing, but you do care about listening to iTunes or getting your
  276. % stimulus running quickly, e.g., during development and debugging of your
  277. % experiment or when showing a quick & dirty online demo of your stimulus
  278. % during a presentation. In these situations you can add the command
  279. % Screen('Preference','SkipSyncTests', 1); at the top of your script,
  280. % before the first call to Screen('OpenWindow'). This will shorten the
  281. % maximum duration of the sync tests to 3 seconds worst case and it will
  282. % force Psychtoolbox to continue with execution of your script, even if the
  283. % sync tests failed completely. Psychtoolbox will still print error
  284. % messages to the Matlab/Octave command window and it will nag about the
  285. % issue by showing the red flashing warning sign for one second. You can
  286. % disable all visual alerts via Screen('Preference','VisualDebugLevel', 0);
  287. % You can disable all output to the command window via Screen('Preference',
  288. % 'SuppressAllWarnings', 1);
  289. %
  290. % If your graphics system basically works, but your computer has just very
  291. % noisy timing you can adjust the threshold settings we use for our tests
  292. % via the setting:
  293. %
  294. % Screen('Preference','SyncTestSettings' [, maxStddev=0.001 secs][, minSamples=50][, maxDeviation=0.1][, maxDuration=5 secs]);
  295. %
  296. % 'maxStddev' selects the amount of tolerable noisyness, the standard
  297. % deviation of measured timing samples from the computed mean. We default
  298. % to 0.001, ie., 1 msec.
  299. %
  300. % 'minSamples' controls the minimum amount of valid measurements to be
  301. % taken for successfull tests: We require at least 50 valid samples by
  302. % default.
  303. %
  304. % 'maxDeviation' sets a tolerance threshold for the maximum percentual
  305. % deviation of the measured video refresh interval duration from the
  306. % duration suggested by the operating system (the nominal value). Our
  307. % default setting of 0.1 allows for +/- 10% of tolerance between
  308. % measurement and expectation before we fail our tests.
  309. %
  310. % 'maxDuration' Controls the maximum duration of a single test run in
  311. % seconds. We default to 5 seconds per run, with 3 repetitions if
  312. % neccessary. A well working system will complete the tests in less than 1
  313. % second though.
  314. %
  315. % Empirically we've found that especially Microsoft Windows Vista and
  316. % Windows-7 may need some tweaking of these parameters, as some of those
  317. % setups do have rather noisy timing.
  318. %
  319. %
  320. %
  321. % MORE WAYS TO TEST:
  322. %
  323. % The script VBLSyncTest() allows you to assess the timing of Psychtoolbox
  324. % on your specific setup in a variety of conditions. It expects many
  325. % parameters and displays a couple of plots at the end, so there is no way
  326. % around reading the 'help VBLSyncTest' if you want to use it.
  327. %
  328. % The script PerceptualVBLSyncTest() shows some flickering stimulus on the
  329. % screen and allows you to assess visually, if synchronization works
  330. % properly.
  331. %
  332. % Both tests are for the really cautious: The built-in test of
  333. % Screen('OpenWindow') should be able to catch about 99% of all conceivable
  334. % synchronization problems.
  335. %
  336. % MORE READING: See the help for 'help MirrorMode' and 'help
  337. % BeampositionQueries' for more info about display issues.
  338. %
  339. %
  340. % LINUX specific tips:
  341. %
  342. % 1. Just as on all other operating systems, timed visual stimulus onset
  343. % and stimulus onset timestamping is not reliable on regular (non
  344. % fullscreen) windows, windows that don't cover the complete desktop of a
  345. % Psychtoolbox screen (also known as X-Screen) or transparent windows,
  346. % e.g., when the PsychDebugWindowConfiguration() command was used. Use of
  347. % rotated display output (90, 180, 270 degrees etc.) will also prevent
  348. % proper timing.
  349. %
  350. % Your windows must be non-transparent, decoration/borderless, fullscreen
  351. % and cover a complete X-Window system screen. On a multi-display setup
  352. % that means that either your window must cover all connected displays, or
  353. % you need to setup separate X-Screens in the graphics driver control panel
  354. % GUI or the /etc/X11/xorg.conf file for different displays, so that all
  355. % stimulus displays are grouped in one (or multiple) X-Screen which are
  356. % fully covered by your PTB onscreen window, and other displays, e.g.,
  357. % operator GUI displays, are grouped into a different X-Screen.
  358. %
  359. % 2. A major source of timing trouble can be 3D desktop compositors. Either
  360. % use a user interface that doesn't employ a desktop compositor, or
  361. % configure your desktop compositor of choice to
  362. % "unredirect_fullscreen_windows", which will disable the compositor for
  363. % Psychtoolbox fullscreen windows. With most versions of compiz, the
  364. % command PsychGPUControl('FullScreenWindowDisablesCompositor', 1); can do
  365. % this setup step for you. If you use a different desktop compositor, and
  366. % on a few versions of compiz, the command doesn't yet work, so you need to
  367. % check the manuals/help of your system on how to enable the option
  368. % "unredirect_fullscreen_windows" manually. If in doubt, just use a desktop
  369. % session without 3D compositor for running the actual data collection of
  370. % your studies. Examples of desktops which use a compositor: Ubuntu Unity,
  371. % GNOME-3, GNOME-2, KWin. Examples which don't use a compositor: GNOME-2
  372. % classic, Mate desktop, XFCE.
  373. %
  374. % If you use the NVidia or AMD proprietary graphics drivers, frequent
  375. % synchronization failures, or other sync related warnings, or unsteady,
  376. % irregularly timed flicker during PerceptualVBLSyncTest indicate that a
  377. % desktop compositor is in use. On Linux with the free and open-source
  378. % graphics drivers radeon, nouveau or intel, PTB will output warnings about
  379. % non-pageflipped flips in such a case.
  380. %
  381. % 3. Another reason for timestamping problems can be the use of
  382. % triple-buffering if your system has an X-Server older than version 1.12,
  383. % or if you have an old system with an outdated graphics driver.
  384. % Psychtoolbox would warn you at least when the free graphics drivers are
  385. % in use, about some error in "PsychOSGetSwapCompletionTimestamp" and some
  386. % system configuration problems.
  387. % On such setups, triple-buffering can be disabled with driver specific
  388. % options in xorg.conf:
  389. %
  390. % On Intel graphics drivers, add the options:
  391. %
  392. % Option "TripleBuffer" "off"
  393. % Option "SwapbuffersWait" "on"
  394. %
  395. % On the nouveau driver for NVidia cards, add:
  396. % Option "SwapLimit" "1"
  397. % Option "GLXVBlank" "on"
  398. %
  399. % On Linux with the open-source intel, nouveau and radeon graphics drivers,
  400. % the script OMLBasicTest allows some additional correctness checks.
  401. %
  402. % History:
  403. % 17.06.2006 written (MK).
  404. % 09.10.2012 Add Linux-specific section (MK).