home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Libraries / VideoToolbox 95.04.18 / Video synch < prev   
Encoding:
Text File  |  1995-04-10  |  36.8 KB  |  583 lines  |  [TEXT/R*ch]

  1. VideoToolbox: Video synch
  2. April 11, 1995
  3.  
  4. The VideoToolbox allows you to produce accurately controlled visual stimuli. This
  5. document deals with time; how to make sure each video frame shows what you want.
  6. After reading this document, you'll probably want to look at the accompanying
  7. "Video bugs" document, and run the "TimeVideo" program on your Mac, to give all
  8. your video cards a thorough workout and learn their timing characteristics. If
  9. you have many computers, put TimeVideo on a floppy and run it from the floppy on
  10. each machine; all the results will accumulate in a single report file. I am
  11. always glad to receive copies of the TimeVideo report on new machines.
  12.  
  13. MOVIES. It's easy to show movies on a Mac. Create all your GWorlds in memory
  14. ahead of time, and then call CopyWindows() to copy one image after another to the
  15. screen (e.g. try the demos NoiseVBL or SandStorm). For any serious application
  16. you'll want a real-time movie, one image per video frame, so you'll want to wait
  17. for a new frame before copying each image, using one of the synchronization
  18. techniques described below (e.g. try the demo NoiseVBL). (Incidentally, some
  19. people have mistakenly assumed that page switching is the ONLY way to achieve a
  20. real-time movie. It's not. Video cards serially read the image pixels one by one
  21. during most of the frame period, e.g. 15 ms. If your computer starts writing at
  22. the beginning of the new frame and writes the pixels faster than they're read
  23. you'll display a whole new image on each frame.) How big a movie you can show in
  24. real time will depend on how fast your processor is. TimeVideo does this timing
  25. for you, telling you what fraction of the screen you can fill with a real-time
  26. movie. If you can't do a full-screen movie and you need to show multiple patches,
  27. consider showing several small movies, updating only the dynamic parts of the
  28. screen. (We assume that you are not limited by memory, and therefore do not
  29. discuss Apple's QuickTime, whose principal purpose, beyond making movies
  30. portable, is to minimize storage requirements by using image compression.) Most
  31. Macs access built-in video much more quickly than NuBus video. Here are data
  32. rates for showing movies on a few machines:
  33.     
  34. Power Mac 7100/66 (w secondary cache?): Built-In VRAM 29 MB/s
  35. Power Mac 7100/66 (w/o secondary cache): Built-In VRAM 19 MB/s
  36. Power Mac 8100/80: Built-in VRAM 18 MB/s, DRAM 17 MB/s
  37. Power Mac 8100/80: "Built-in AV" 15 MB/s
  38. Power Mac 6100/60AV (w secondary cache): Built-in VRAM 12 MB/s, DRAM 23 MB/s
  39. Power Mac 6100/60AV (w/o secondary cache): Built-in VRAM 11 MB/s, DRAM 12 MB/s
  40. Quadra 840av: Built-in 12 MB/s, NuBus 9 MB/s
  41. Quadra 630: Built-in 12 MB/s
  42. Quadra 950: Built-in 11 MB/s, NuBus 5 MB/s
  43. LC 475: Built-in 10.6 MB/s
  44. Centris 660AV: Built-in 10.2 MB/s
  45. Quadra 660AV: Built-in 9.6 MB/s
  46. Quadra 900: Built-in 9.5 MB/s, NuBus 5.5 MB/s
  47. Centris 650: Built-in  5.8  MB/s     
  48. LC III: Built-in  5.8  MB/s     
  49. IIci: Built-in 5.5 MB/s, NuBus 3.7 MB/s
  50. II with Rocket 68040 accelerator: NuBus 4.0 MB/s
  51. PowerBook 520/540: 3.6 MB/s
  52. IIfx: NuBus 3.0 MB/s (8•24GC "acceleration" reduces it 1.5 MB/s)
  53. PowerBook 160: Built-in LCD screen and video port 3.0 MB/s
  54. IIcx: NuBus 2.7 MB/s
  55. II: NuBus 2.7 MB/s
  56. LC II: Built-in  2  MB/s     
  57. PowerBook 170: Built-in LCD screen 1.6 MB/s, SCSI Radius PowerView 1 MB/s
  58. Classic: Built-in 1.7 MB/s
  59. Classic II:  Built-in 1.4 MB/s
  60. SE: Built-in 0.9 MB/s
  61. Plus: Built-in 0.6 MB/s
  62. The quoted rate is the best, across all pixel depths and across CopyBits and
  63. CopyBitsQuickly, which are usually within +/-20% of each other.  CopyBits, being
  64. part of QuickDraw, always runs native on the PowerPC. Surprisingly, PowerPC
  65. native speed of CopyBitsQuickly is only about 10% higher than emulated speed.
  66. However,  CopyBitsQuickly is an unusual program, with unrolled loops and other
  67. tricks to maximize speed. On the other hand, note the remarkable doubling of
  68. DRAM video speed (and 10% increase of VRAM speed) due to installation of the
  69. secondary cache. (Does anyone know how TimeVideo might determine whether a
  70. secondary cache is installed?)
  71.  
  72. Power Mac 7100 NuBus bug: Michael Eckert (meckert@ee.uts.edu.au) writes, "From
  73. what I understand, a bug in the 7100's NuBus interface chip limits the throughput
  74. of the NuBus port. This does not affect built-in VRAM video or DRAM video (the AV
  75. option, in PDS slot). This bug affects only the 7100 (not 8100), and is supposed
  76. to be fixed during the next release of the machines." October 12, 1994.
  77.  
  78. SPRITES. If you want to show objects moving around the screen, and your
  79. computer isn't fast enough to do a full screen movie, then you may want to
  80. animate just the objects. Objects animated in this way are called "sprites".
  81. Tony Myles has published a package called "SpriteWorld" that you may want to
  82. download from info-mac/Development. (For an explanation of how to do that, read
  83. the VideoToolbox "Advice" document.)
  84.  
  85. PAGE SWITCHING. Many video cards have multiple video pages; check your TimeVideo
  86. report, or call GDGetPageCnt(). Normally the Macintosh only uses page 0, but if
  87. you have multiple pages, they can be switched by calling GDSetPageShown() and
  88. GDSetPageDrawn(). This could be used to show very short full-screen movies, e.g.
  89. alternating two images. This might be an effective way to show large slow
  90. movies, taking several frames to load each new image to an unseen page that's
  91. instantly swapped in when it's full.  (The number of pages available depends on
  92. the pixel size. In max depth there's always only one page. In lesser depths
  93. there's unused memory, which the video driver may make available as extra
  94. pages.) Sergei A.Kurkin, Kurkin@med.hokudai.ac.jp, has "tested GDSetPageDrawn()
  95. and GDSetPageShown(), and they seem to work OK." 
  96.  
  97. CLUT (Color Lookup Table) ANIMATION. Most video cards have a hardware color
  98. lookup table (clut) that dynamically transforms each pixel to a color (an RGB
  99. triplet) that is sent to the digital to analog converters. Temporal modulation
  100. (e.g. flicker or fading on and off) of visual stimuli can be achieved very
  101. conveniently by loading a new clut on each frame. Look at the demo
  102. FlickeringGrating. However, not all video card drivers are fast enough to
  103. achieve this. (Usually this is not a hardware limitation; it's crumby driver
  104. software.) Run TimeVideo to find out for sure; it determines how many frames it
  105. takes either GDSetEntries or SetEntriesQuickly to load the clut. The clut is
  106. also used for gamma correction. Apple's provision for gamma correction is
  107. somewhat crude; for the utmost luminance accuracy you may wish to use the
  108. Luminance.c routines. See section B, below.
  109.  
  110. COLOR CYCLING. Bart Farell, Bart_Farell@isr.syr.edu, writes, "It's easy to do
  111. color cycling using either GDSetEntries or SetEntriesQuickly, e.g.
  112.     GDSetEntries(device,start,count,table+offset);
  113. First create a double-sized ColorSpec array, e.g., an array of 512 instead of
  114. 256. It's called "table" in the above example. Set up the second half of the
  115. array to be identical to the first half, to implement wrap-around.
  116. GDSetEntries/SetEntriesQuickly(), as always, loads “count+1” entries starting at
  117. entry “start”. (Yes, “count+1”, that’s Apple’s confusing convention.)  Just add
  118. an integer "offset" to the table pointer. GDSetEntries/SetEntriesQuickly() will
  119. start reading the table, offset by that number of ColorSpecs. Across a sequence
  120. of frames, using a different offset for each call to GDSetEntries or
  121. SetEntriesQuickly(), each ColorSpec of the array can be loaded to any entry. In
  122. practice you'd create several of these double-sized ColorSpec arrays and animate
  123. by using a different array and offset on each frame.  The payoff is that the
  124. number of arrays, and clut computation time, is an order of magnitude lower than
  125. would be required to compute a custom clut for each case."
  126.  
  127. MAKING DYNAMIC WHITE NOISE. I know of at least three people (including myself)
  128. that hit upon clut animation as a fast way to synthesize dynamic white noise.
  129. You fill the image plane with random numbers in the range 0 to 255, and you load
  130. a new random clut on each frame. It might seem that this will produce noise that
  131. is spatially and temporally uncorrelated. Not quite. Any pair of pixels has a
  132. 1/256 chance of being driven by the same clut entry, in which case they will be
  133. perfectly correlated. So the spatial autocorrelation function has the (desired)
  134. delta function at zero separation and an (undesired) 1/256 correlation at all
  135. other separations. This corresponds to the desired flat spectrum, PLUS an
  136. undesired delta function at zero spatial frequency. In plain English, you'll end
  137. up with dynamic white noise plus a spatially-uniform temporally-white flicker.
  138. (This problem does not arise if you only use each clut entry once, but that
  139. restricts you to an image of only 16x16.) Note that I am only discussing first
  140. and second order statistics. Higher-order statistics will still differ from
  141. truly white noise, but I doubt that our visual systems are sensitive to the
  142. difference. However, the spatially uniform flicker component of the clut
  143. animation noise is visually very salient. Ted Adelson noticed it immediately
  144. when I proudly demonstrated this method at Optical Society in San Francisco in
  145. 1989 (I think). The artifactual flicker becomes progressively more salient as
  146. you increase the viewing distance, since the spatially uncorrelated noise fades
  147. out while the spatially uniform noise is unaffected. What I now do, and
  148. recommend to others, is to use brute force. Precompute all the noise and show a
  149. movie in which all the pixels are truly independent random samples. The
  150. VideoToolbox routine RandFill() is a fast way to compute the noise, and is used
  151. by NoiseFill.c. Try the demos Sandstorm and NoiseVBL.
  152.  
  153. FANCIER TRICKS. The video card has only one clut, but it is possible to
  154. simultaneously present multiple independently modulated patterns, by reserving
  155. separate sections of the clut for each temporal modulation, though you'll suffer
  156. a comensurate loss of intensity resolution. Clut animation is not restricted to
  157. dynamic modulation of contrast. If the pixel values represent phase, and the
  158. clut is loaded with a sin function, then rotating the clut entries--i.e. setting
  159. the i-th entry to what was formerly the value of the i-1 entry (see Color
  160. Cycling, above)--will shift the phase of the pattern. A spatially vignetted
  161. drifting grating may be synthesized by using alternate pixels (or frames) to
  162. represent vignetted sin and cos components of the pattern, devoting half the
  163. clut to each (or loading alternate cluts on alternate frames). Appropriate
  164. adjustment of the relative contrasts of the sin and cos components will produce
  165. any desired phase of the sum. However, most of these tricks are superfluous;
  166. it’s usually easier and better to just show a movie. Many current processors are
  167. fast enough to show 8-bit full-screen movies.
  168.  
  169. A. HOW TO SYNCHRONIZE A MACINTOSH PROGRAM TO A VIDEO CARD.
  170.  
  171. The hardest part in doing vision experiments is synchronizing the computer
  172. program to the video card. Nearly all video cards are masters, and run freely,
  173. expecting the video monitor to be a slave. Any experiment that cares about
  174. timing will normally have to synchronize itself to the video card, by waiting
  175. for each frame to end, as will be discussed below.
  176.  
  177. You typically can't access the video card hardware directly, because it's
  178. undocumented. Instead you are allowed to send requests to the video driver
  179. associated with your video card. All video cards that plug into the Mac come
  180. with video drivers that conform sufficiently well to the Apple guidelines that
  181. they are at least minimally compatible with the VideoToolbox. However, some
  182. drivers have outright bugs, and the timing of many drivers, for which Apple does
  183. not publish guidelines, sometimes makes it surprisingly difficult to do simple
  184. things. A current list of these bugs and "features" appears in the VideoToolbox
  185. "Video driver bugs" document.
  186.  
  187. Every video card has a video driver in its ROM, but the manufacturer may supply
  188. a newer driver by floppy that supercedes the one in ROM, which may be buggy.
  189. (Built-in video devices, e.g. in the Mac IIci and Quadra computers, load the
  190. video driver from the computer's ROM.) Apple distributes video driver updates as
  191. resources in the System file, so updating your System may change the video
  192. driver of your Apple video card. IdentifyVideo(device) returns a string with the
  193. name and version number of the driver that is actually in use. Try TimeVideo.
  194.  
  195. There are three different ways to synchronize a program to a video card, which
  196. we'll first describe quickly, and then more fully. (1) Apple recommends using
  197. the vertical blanking interrupts, which are supposed to occur once per frame, at
  198. the beginning of the vertical blanking. The VideoToolbox VBLInstall.c routines
  199. make this very easy to do. A disadvantage of this approach is that it fails if
  200. you raise the processor priority to block all interrupts. (2) Most (not all)
  201. video drivers, when asked to load the clut, wait until the vertical blanking
  202. interval before beginning to load the clut. (They wait in order to avoid
  203. creating visible hash on the screen while the clut is changing.) This has the
  204. side effect of synchronizing your program to the display, since the driver
  205. doesn't return control until the VBL interval occurs. Either of these
  206. synchronization methods (1 or 2) may fail, depending on which video card you
  207. have, what you've set the pixel size to (1 to 32 bits), and whether you've
  208. raised the processor priority. The demo program TimeVideo tests both methods of
  209. synchronization on all your video cards at all pixel sizes and saves the results
  210. in a text file. (3) I think that all video cards provide a read-only
  211. vertical-blanking bit, but unfortunately few manufacturers will tell you its
  212. address, so you have to find it yourself (typically by disassembling the video
  213. driver), which is tedious.
  214.  
  215. 1. VBL Interrupts. Most video cards emit one VBL interrupt during each video
  216. frame. However, during 1990-1 Apple generated some poor video drivers for their
  217. cards ("Macintosh Display Card": 8•24)  and the built-in video in the Quadra 700
  218. and 950. These drivers generate several interrupts per frame. (Kyle Cave
  219. discovered that there are no extra interrupts if the cache is disabled on the
  220. Quadra 700.) This behavior is contrary to Apple's documentation, but
  221. VBLInstall.c works around the bug, using a timer to discard the excess
  222. interrupts (as suggested by Raynald Comtois). Some video drivers take multiple
  223. frames to load the clut, and block interrupts while doing so, wreaking havoc
  224. with any attempt to count frames. Apple's video drivers produced before 1990 and
  225. since 1992 seem to be ok. (This may be in response to my many bug reports.) Try
  226. the demos TimeVideo and NoiseVBL.
  227.      The best current theory (due to Raynald Comtois) on the source of the extra
  228. interrupts is that the card is asserting the interrupt line for a fixed
  229. duration, perhaps 1 ms. As long as you are in the interrupt-handling routine (or
  230. one of higher priority), other interrupts of the same level are disabled. If you
  231. stay in the interrupt routine for long enough, the interrupt line has time to be
  232. released and you don't get a second interrupt, otherwise you do.
  233.  
  234. 2. LOADING THE CLUT. It seems that all video drivers automatically synchronize
  235. clut-loading to the video frame. Apple's guidelines for video drivers (in the
  236. book Designing Cards and Drivers) state that the video driver may save the
  237. clut-load (i.e. set-entries) request and defer the actual loading until the next
  238. VBL interrupt. Such video drivers return almost immediately after a set-entries
  239. request. Thus, although the actual clut load will be synchronized to the
  240. display, your program will be asynchronous because the GDSetEntries call will
  241. return immediately. However, Apple's guidelines state that if the processor's
  242. interrupt priority has been raised, suspending the video card's VBL interrupts,
  243. then the video driver should always load the clut before returning, which would
  244. synchronize your program to the display. You can raise and lower the processor
  245. priority by calling the VideoToolbox routine SetPriority.c. The priority is
  246. normally zero. TimeVideo measures timing at both normal and high psiority.
  247.      Be aware that a set-entries request, i.e. calling GDSetEntries(), does not
  248. necessarily wait for the beginning of the next vertical blanking interval. It
  249. might merely wait until blanking is true. As a result two successive
  250. GDSetEntries calls might occur during the same blanking interval. To get exactly
  251. one call per frame you may need to delay for a suitable interval (perhaps 1 ms)
  252. before calling it again.
  253.      Most video drivers that I've tested seem to be synchronous and reliably
  254. take exactly one frame to load the clut. However, some video drivers take
  255. longes, e.g. any loading of the clut on Apple's new video cards ("Macintosh
  256. Display Card": 8•24) takes 30 ms--two frames--which is unacceptably slow for
  257. lookup table animation. The TrueVision NuVista seems to be asynchronous.
  258. Presumably the on-board processor accepts the clut information at any time and
  259. actually updates the clut during the next vertical blanking interval. However,
  260. this means that asking the video driver to load the clut doesn't have the useful
  261. side effect of synchronizing the Mac program to the display. The NuVista takes
  262. about 0.3 frames (i.e. a few ms) to reload the whole clut when in 8, 16, or
  263. 32-bit mode, but takes several frames (i.e. tens of ms) when in 1, 2, or 4-bit
  264. mode (presumably because the fractional byte addressing is slow). I haven't
  265. checked, but presumably the NuVista driver would become synchronous, as
  266. specified by Apple, if the processor priority were raised.
  267.      SetEntriesQuickly.c--written primarily by Raynald Comtois, Peter Lennie,
  268. and Bill Haake--provides fast clut loading for many popular video devices. Try
  269. TimeVideo.
  270.      If 8-bit pixels are enough, consider buying Apple's old "Toby" or "TFB"
  271. video cards, since they work fine, and cost only $90 each from Shreve Systems
  272. (800)-227-3971.
  273.  
  274. 3. BLANKING BIT. Apparently all video cards have a blanking bit (telling whether
  275. the video signal is in the blank period between frames), but, alas, the
  276. manufactwrers never tell you where it is. You could figure out which bit by
  277. disassembling the driver, as I did for the Apple Toby and TFB video cards (no
  278. longer sold by Apple; call Shreve at phone  number given above). Perhaps one
  279. could write a program that would find the VBL bit automatically, by looking for
  280. any bit that changes at the right frequency.
  281.      WaitForBlanking() in SetEntriesQuickly.c tests the blanking bit, but
  282. presently supports only the (obsolete) Toby and TFB video cards. Hopefully other
  283. people will enhance this routine to support more devices.
  284.  
  285. 4. CONCLUSION. Don't take synchronization or lookup table animation for granted.
  286. Run TimeVideo to check out your video cards.
  287.  
  288. B. SYNCHING MULTIPLE VIDEO CARDS TO EACH OTHER
  289.  
  290. SOFTWARE: Short of butchering the hardware, synching two video cards is hard to
  291. do. It needn't be so, if the manufacturers were more forthcoming about how to
  292. program their cards. By disassembling the video driver for Apple's original
  293. "Toby" video cards I worked out that there are halt and restart commands that
  294. can be issued to the card. Several video cards can be synched by restarting them
  295. all at once (actually one after the other, in quick succession). Their quartz
  296. crystals run at slightly different frequencies so the cards slowly drift out of
  297. phase with each other, but that's still good enough for a relatively brief
  298. visual stimulus, resynching before each stimulus. Software to do this for the
  299. Toby video cards (which are still available from Shreve Systems (800)-227-3971,
  300. $90 each) is in the VideoTFB.c file in the VideoToolbox. It seems very likely
  301. that this approach is applicable to most video cards, but you would need to
  302. discover what the appropriate commands are to restart your cards. (Normally,
  303. video cards are restarted only at system restart.) You might be able to do this
  304. by disassembling the video drivers, but perhaps the manufacturer would tell you
  305. if you asked nicely. (Incidentally, the "Toby" video cards also have an
  306. undocumented ribbon cable connector that an Apple engineer explained to me could
  307. be used to achieve hardware-level synchronization. He said it might not work. I
  308. tried, but never got it to work.) I hope you'll share any useful results with
  309. me. I'd love to learn the restart commands for other video cards, especially
  310. Apple's, and would add support for them to the VideoToolbox software.
  311.  
  312. HARDWARE: David Brainard <brainard@condor.psych.ucsb.edu> writes, "We have been
  313. working on the problem of synchronizing two video cards. For the Apple 8•24 card
  314. (rev B), we have a simple hardward solution. It turns out you can cut the clock
  315. trace and push the clock signal through a fast OR gate.  Somewhat to my
  316. surprise, you can hold the clock for as long as you like without changing the
  317. state of the board.  When you release the clock, the sync is shifted by the
  318. amount of time you held the clock. The 8•24 cards are OK.  They will drive a 16"
  319. monitor and SetEntriesQuickly works fast. They cost $239 used. There may be a
  320. software solution like the one you developed for the Toby cards, but my guy
  321. struggled with it for a while and gave up.  (He is better with hardware than
  322. software, which is why we went the way we did.)"
  323.     Karsten S. Weber ( karsten@john.berkeley.edu) adds, "I am working as a
  324. programmer for Professor Martin Banks at the University of California at
  325. Berkeley. We have recently done some stereo motion experiments that required
  326. synchronizing two video cards. I read in the 'Video Tool Box documentation'
  327. about how Dr. Brainard had developed a hardware solution for synchronizing two
  328. 8*24 cards. I have since used Brainard's work (and corresponded with him
  329. frequently) to do the same for our lab, but we did have some troubles with his
  330. approach primarily because we are using larger monitors. These problems have
  331. been solved in a nice way, as described below.
  332.     First I tried the approach that Brainard had successfully implemented
  333. earlier, i.e. a transistor to stop the clock of the video board. After soldering
  334. the transistor on the board, I tested it, and it worked fine. There is, however,
  335. a problem with this approach. It only works for 13" and 16" monitors. When I
  336. started using our 21" monitors, stopping the clock had no effect on the video
  337. output whatsoever! There is more than one clock on the video board, and it turns
  338. out that 21" monitors can be synchronized by stopping the fastest of the clocks.
  339. Using the 21" monitor and the 8*24 video board gives a maximum of 256 colors.
  340.     Secondly I implemented a nice software approach to synchronize the
  341. monitors. By looking at the VBLs for both boards I saw that they only stay in
  342. sync for a few seconds. This means that it is necessary to synchronize the video
  343. boards before every trial, of which there can be hundreds. It is therefore
  344. desirable to have a fast and easy way  to synchronize the boards. I came up with
  345. a way for doing the synchronization that does not require photometers and a
  346. scope (as is required in Dr. Brainard's approach). The idea is the following:
  347. The hardware modification is installed on both video boards (rather than just
  348. one). A VBL interrupt routine is written for each board. When a VBL occurs on
  349. the left video board, the left interrupt routine is activated, and this routine
  350. stops the clock for the left video board (it does this by controlling the
  351. hardware that was added on the board). This means that the video board is
  352. stopped exactly at VBL time. The same procedure is done with the right video
  353. board:  The interrupt from the right video board activated the right interrupt
  354. routine that stops the right video board. In this way both video boards get
  355. stopped at the time as their respective VBLs. The main program is still running
  356. and when it detects that both video cards have stopped, it activates them
  357. simultaneously, and the two video boards are now in synch.  This means that
  358. synchronization can be done by calling one function, and the time it takes to
  359. synchronize the video boards is less than the time between the two VBLs. I'm
  360. using DTR of the modem and printer port to control the hardware."
  361.  
  362. BB. MORE ON INTERRUPTS
  363.  
  364. Michael Bach, bach@sun1.ruf.uni-freiburg.de, writes, "We are trying to do
  365. simultaneous visual stimulation and analog measurement of brain potentials on
  366. the same Macintosh. One of our ideas was to use the Time Manager to trigger
  367. analog measurement every 2 ms in its interrupt service routine. This works fine
  368. unless we use animate palette (or GDSetEntries or GDSetGamma): No timer tasks
  369. are serviced until vbl. Apparently the video driver goes into a tight loop,
  370. polling a vbl flag, with interrupts switched off! Can this be true? This would
  371. be a strong setback for using the Mac for this sort of work." Yes, alas, the
  372. normal behavior for interrupt service routines and the driver Control calls on
  373. the Mac is to suspend all interrupts until returning. Most video card drivers
  374. wait for vbl, as Michael says. However, TrueVision's NuVista returns
  375. immediately, letting the video card processor do the work later, at vbl time.
  376. And SetEntriesQuickly.c, for the few devices that it supports, allows you to
  377. achieve whatever behavior you want. Those solutions will make your code highly
  378. dependent on the particular video card. One solution that might work on all
  379. video cards would be to only call the video driver (e.g. GDSetEnries) during
  380. vertical blanking, i.e. immediately after the vbl interrupt occurs (use
  381. VBLInstall.c).
  382.      Michael Bach (bach@sun1.ruf.uni-freiburg.de) adds, "We have since done more
  383. experiments. We have an A/D card from National Instruments (NB-MIO-16), which we
  384. use with LabView. National Instruments also provide a library of lower-level
  385. routines ("NI-DAQ"), namely one "SCAN" that regularly scans analog input and
  386. writes into a buffer in memory. As the card lacks DMA, this is -- reading
  387. between the lines of their documentation -- implemented by an interrupt service
  388. routine called at the end of conversion. For this, the "NB-Handler Init" must be
  389. installed when the system is started up. While interrupts are blocked,
  390. measurements are kept in a FIFO on the card. If we use their SCAN routine,
  391. sampling is performed smoothly and not blocked by AnimatePalette et al.  But
  392. care has to be taken that the FIFO is not overrun." This confirms that the video
  393. driver blocks all interrupts. Any interrupt that's still asserted will occur
  394. when the video driver exits. The timer interrupt is asserted only briefly and is
  395. lost if not serviced promptly; apparently the A/D card's slot interrupt request
  396. remains asserted until serviced.
  397.  
  398. C. CONTROLLING THE COLOR LOOKUP TABLE (CLUT)
  399.  
  400. QuickDraw is one of the great virtues of the Macintosh. However, several of its
  401. assumptions about what you want are inappropriate for vision experiments. In
  402. particular, it assumes that you want all your monitors to act as one consistent
  403. desktop (with consistent color tables). This is a problem if you want to load
  404. completely independent lookup tables and images onto two monitors that, for
  405. example, you may want to superimpose optically. QuickDraw enforces the
  406. consistency throught the Palette Manager, but calls that are nominally to the
  407. Color Manager (e.g. Apple's SetEntries) may be intercepted by the Palette
  408. Manager, resulting in undesired effects on other screens. My solution is to
  409. bypass QuickDraw and to load the lookup tables more or less directly, without
  410. telling QuickDraw.
  411.  
  412. GDSetEntries() and SetEntriesQuickly() work outside of QuickDraw. GDSetEntries
  413. makes a cscSetEntries control call to the video driver; SetEntriesQuickly
  414. directly addresses the hardware. They both load the clut of the video card
  415. without changing the color spec table of the graphics device; QuickDraw will
  416. continue to assume that the graphics device's color spec table is a true copy of
  417. the clut. This is the behavior that I usually want. However, it may cause
  418. problems if you use CopyBits since CopyBits will translate the color of each
  419. pixel it copies, using the inverse color table of the current device, which is
  420. based on the color spec table, NOT the clut. If you want to use CopyBits or
  421. SetCPixel, you should copy your color spec table into the graphics device's color
  422. spec table, and set ctSeed to alert QuickDraw that it's been changed. Or, instead
  423. of using QuickDraw's CopyBits and SetCPixel, you could use the VideoToolbox's
  424. CopyBitsQuickly and SetPixelsQuickly(), which copy pixel walues directly,
  425. ignoring all color tables and inverse color tables, .
  426.  
  427. D. HIGH FRAME RATES
  428.  
  429. On March 17 fifty of us sent a letter to Apple--see "cscSetModeTiming.note" in
  430. the "Notes" folder--requesting a new feature for video drivers to support
  431. arbitrary frame rates and resolutions. High frame rates are particularly relevant
  432. to stereo (using LCD glasses to alternate eyes) and retinal physiology (because
  433. retinal ganglion cells respond up to 100 Hz).
  434.  
  435. For some vision experiments it is desirable to run at very high frame rates,
  436. e.g. 100 or even 200 Hz for retinal physiology experiments. There exist special
  437. monitors, e.g. the Joyce DM-4 Display, that run at such high frame rates, but no
  438. video cards automatically configure themselves to work with such odd displays.
  439. All Mac video cards that I know of are programmable to a wide range of line and
  440. frame rates, but very few video card manufacturers provide information on how to
  441. do this programming. (Normally, the video card senses the kind of monitor
  442. through the monitor sense lines and the video driver programs the card to suit
  443. the monitor. Alas, the monitor sense line scheme basically only identifies the
  444. Apple monitors, and is not a general purpose solution. The video driver is a
  445. program that is read from your video card's ROM into memory when you boot the
  446. computer.) Truevision does supply information on how to program the NuVista to
  447. arbitrary line and frame rates. (I believe the max frame rate is over 100 Hz,
  448. but I don't recall what the limit is.)
  449.  
  450. E. MORE-THAN-8 BIT DACs
  451.  
  452. RasterOps “ProColor 32” has 9-bit DACs. 
  453. RasterOps “Paintboard Turbo XL” has 9-bit DACs. $1399 (Mentioned by Steve Shevell)
  454.  
  455. Rhea Eskew, eskew@neu.edu, writes "There is a 12-bit video board now available
  456. for the Mac (it's the Psychophysics Display Interface, from Australia). I have
  457. the generation 2 version, and Charles Stromeyer has purchased generation 3 --
  458. which is now in my lab (but hasn't yet really been tested).  In addition to 12
  459. bit dacs it has some nice hardware features, like configurable color lookup
  460. tables and 2 frame buffers (you can allegedly make one image a function of
  461. another, like for windowing etc -- but I haven't tried it yet).  The drawbacks
  462. are the cost (I don't know how much he'll charge now, but it's at least 8X the
  463. cost of a standard board) and the fact that the maker is small, in Australia,
  464. and has had trouble delivering new products according to schedule (he has been
  465. prompt and helpful about solving problems after delivery, however)".
  466. James Sokoll Pty Ltd
  467. PO Box 808 
  468. Kenmore  4069 AUSTRALIA
  469. Tel: 617 878 3357
  470. Fax: 617 378 9794
  471.  
  472. Does anyone know of any others?
  473.  
  474. F. SHIELDCURSOR
  475.  
  476. In the summer of '93 Steve Lemke at Radius (lemke@radius.com) wrote, "Apple has
  477. stated several times (though I wouldn't necessarily expect you to have run
  478. across any of them) that if you write directly to the screen, you must use the
  479. "ShieldCursor" and "ShowCursor" traps to prevent the cursor from being
  480. overwritten.  To do so, you pass the rectangle in which you are drawing to
  481. "ShieldCursor". It's nice that all QuickDraw routines use this trap, as it makes
  482. for a simple way to identify what areas of the screen are changing. It also
  483. makes for a simple way for people who draw directly to the screen to notify the
  484. Radius PowerView software that they are doing so.  Everything in that rectangle
  485. gets updated on the Radius PowerView screen.  Too bad more programs don't use
  486. it, though..."
  487.  
  488. Taking his advice, CopyWindows calls ShieldCursor before calling CopyBitsQuickly.
  489.  
  490. G. DISASSEMBLING A VIDEO DRIVER
  491.  
  492. Assuming you can read 680x0 assembly code, use the VideoToolbox utility
  493. GrabVideoDrivers to put the driver into a file, then use ResEdit with the
  494. ResEdit CODE editor to examine it, comparing it with the example in the appeodix
  495. of Apple's Designing Cards and Drivers book. The ResEdit CODE editor is a public
  496. domain file distributed by:
  497. Ira L. Ruben
  498. Apple Computer, Ioc.
  499. 20525 Mariani Ave., MS: 37-A
  500. Cupertino, Ca. 95014
  501. Ira@apple.com
  502. ftp.apple.com:/dts/mac/tools/resedit/resedit-2-1-code-editor...
  503.  
  504. H. FIXING THE MAC IIci BUILT-IN VIDEO DRIVER
  505.  
  506. (The following remedies worked for the Mac IIci video driver, patching or
  507. replacing the buggy version 0 .Display_Video_Apple_RBV1 driver by copying the
  508. bug-free version 1 of the same driver from the Mac IIsi. It is very likely that
  509. an analogous approach could be used to patch or replace the buggy version 0, 1,
  510. and 2 .Display_Video_Apple_DAFB drivers in the Quadra 700, 750, and 900, by the
  511. bug-free version 3 or 5 of that driver in the Centris 650 or LC 475.)
  512.  
  513. The Mac IIci built-in video driver (.Display_Video_Apple_RBV1 driver, version 0)
  514. has a bug that causes it to crash if you try to do a cscGetEntries Status request.
  515. Here are two ways to fix the bug:
  516.  
  517. 1. AUTOMATIC TEMPORARY PATCH. (This happens automatically if you use GDVideo.
  518. This note is an explanation, in case you're curious.) The bug only affects
  519. GDGetEntries, so the first time GDGetEntries is invoked it automatically calls
  520. PatchMacIIciVideoDriver() in GDVideo.c to find and patch the copy of the buggy
  521. driver residing in memory, preventing any trouble. (If the driver is in ROM, then
  522. the driver is copied to RAM, and patched there.) Only two instructions are
  523. modified, to save & restore more registers. (I figured out what needed patching
  524. by comparing this driver's disassembly with the bug-free version in the Mac
  525. IIsi.) And the driver's version number is changed from 0 to 100, so that programs
  526. can distinguish it from the buggy version 0. This fix persists only until the
  527. next reboot.
  528.  
  529. 2. PERMANENT UPGRADE. The Mac IIsi has version 1 of the same driver, without the
  530. bug. In principle all you have to do is copy the new driver from the IIsi to
  531. your IIci, but this is nontrivial because these drivers are in ROM. It is fairly
  532. simple, as described below, to place a copy of the version 1 (i.e. fixed) driver
  533. into the System file of the Mac IIci, but, as explained in Inside Mac V-424,
  534. this will only displace the older version 0 (buggy) driver if it's not driving
  535. the boot monitor (i.e. if the "Welcome to Macintosh" message appears on some
  536. other monitor), because the boot monitor's driver is loaded at boot time before
  537. the System file is available. If you have multiple monitors, you can use the
  538. Monitors Control Panel to change the boot monitor: hold the option key down and
  539. drag the smiling Mac to any monitor except the one driven by the built-in video.
  540. So, in case you do have multiple monitors, here's how to copy the driver. First
  541. put the VideoToolbox utility "GetVideoDrivers" on a floppy and run it on a Mac
  542. IIsi. That will copy the all the video drivers onto your floppy as ResEdit
  543. files. Then put the floppy in your Mac IIci and copy "-Display_Video_Apple_RBV1"
  544. onto your hard disk. Make a copy of your System file. Open the
  545. "-Display_Video_Apple_RBV1" file in ResEdit. Hit Command-I to edit the Resource
  546. Info. Set the "System Heap" and "Purgeable" flags, change the ID from 1 to 122,
  547. and close the Resource Info window. Now copy the resource and paste it into the
  548. copy of your System file. Quit, saving changes. Now rename the active System
  549. file to something else, like "old System", and rename the edited System file to
  550. "System". Reboot. You can now throw the old System into the trash. The new
  551. driver will automatically be favored over the one in ROM because it has a higher
  552. version number (1 instead of 0). All my test programs indicate that the new
  553. driver works fine in the Mac IIci with System 7, and I would expect it to work
  554. fine with System 6 as well. (Thanks to Mike.Alexander@um.cc.umich.edu for
  555. figuring out why it didn't work for the boot monitor.)
  556.  
  557. I. CONTRIBUTORS TO THIS DOCUMENT
  558.  
  559. Mike.Alexander@um.cc.umich.edu, why you can't replace boot monitor’s driver.
  560. Michael Bach, bach@sun1.ruf.uni-freiburg.de, more on interrupts.
  561. David Brainard, brainard@condor.psych.ucsb.edu, synching cards, PowerBook 520/540.
  562. Thomas Busey, effect of 8•24GC "accel", Power Mac.
  563. Kyle Cave, cavekr@ctrvax.vanderbilt.edu, Quadra 700.
  564. Raynald Comtois, raco@burrhus.harvard.edu, multiple interrupts per frame.
  565. Michael Eckert, meckert@ee.uts.edu.au, PowerMac 7100 timing & NuBus bug.
  566. Kaan Erdener, kerdener@ptnext.pitzer.edu, Quadra 660AV.
  567. Rhea Eskew, eskew@neu.edu, Psychophysics Display Interface.
  568. Bart Farell, bart_farell@isr.syr.edu, Quadra 840av, color cycling.
  569. Tony Hayes, PowerBook 180.
  570. Sergei A.Kurkin, Kurkin@med.hokudai.ac.jp, GDSetPageDrawn and GDSetPageShown
  571. Steve Lemke, lemke@radius.com, ShieldCursor.
  572. Jan Linder, Jannl@aol.com, Quadra 630.
  573. Jan-Eric Litton, janel@neuro.ks.se, PowerMac 8100/80
  574. David Rose, PSY009@sysh.surrey.ac.uk, Mac LC II
  575. Dan Sears, sears@netcom.com, System 7.5.1
  576. Steve Shevell, shevell@uchicago.edu: RasterOps “Paintboard Turbo XL”.
  577. Scott B. Stevenson, stevenso@garnet.berkeley.edu, Power Macintosh 8100/80.
  578. Marty Wachter, mrw@welchgate.welch.jhu.edu, Quadra 840AV.
  579. Karsten S. Weber, karsten@john.berkeley.edu, synching two cards
  580. Wei Xie, bug in SetEntriesQuickly for Apple 8•24 in Quadra 840AV.
  581.  
  582. Please send any corrections or additions to denis@xp.psych.nyu.edu
  583.