
|
 |
|
Michael Abrash's Graphics Programming Black Book, Special Edition
by Michael Abrash
The Coriolis Group
ISBN: 1576101746 Pub Date: 07/01/97
|
Part II
Chapter 23 Bones and Sinew
At the Very Heart of Standard PC Graphics
The VGA is unparalleled in the history of computer graphics, for it is by far the most widely-used graphics standard ever, the closest we may ever come to a lingua franca of computer graphics. No other graphics standard has even come close to the 50,000,000 or so VGAs in use today, and virtually every PC compatible sold today has full VGA compatibility built in. There are, of course, a variety of graphics accelerators that outperform the standard VGA, and indeed, it is becoming hard to find a plain vanilla VGA anymorebut there is no standard for accelerators, and every accelerator contains a true-blue VGA at its core.
What that means is that if you write your programs for the VGA, youll have the largest possible market for your software. In order for graphics-based software to succeed, however, it must perform well. Wringing the best performance from the VGA is no simple task, and its impossible unless you really understand how the VGA worksunless you have the internals down cold. This book is about PC graphics at many levels, but high performance is the foundation for all that is to come, so it is with the inner workings of the VGA that we will begin our exploration of PC graphics.
The first eight chapters of Part II is a guided tour of the heart of the VGA; after youve absorbed what well cover in this and the next seven chapters, youll have the foundation for understanding just about everything the VGA can do, including the fabled Mode X and more. As you read through these first chapters, please keep in mind that the really exciting stuffanimation, 3-D, blurry-fast lines and circles and polygonshas to wait until we have the fundamentals out of the way. So hold on and follow along, and before you know it the fireworks will be well underway.
Well start our exploration with a quick overview of the VGA, and then well dive right in and get a taste of what the VGA can do.
The VGA
The VGA is the baseline adapter for modern IBM PC compatibles, present in virtually every PC sold today or in the last several years. (Note that the VGA is often nothing more than a chip on a motherboard, with some memory, a DAC, and maybe a couple of glue chips; nonetheless, Ill refer to it as an adapter from now on for simplicity.) It guarantees that every PC is capable of documented resolutions up to 640×480 (with 16 possible colors per pixel) and 320×200 (with 256 colors per pixel), as well as undocumentedbut nonetheless thoroughly standardresolutions up to 360×480 in 256-color mode, as well see in Chapters 3134 and 4749. In order for a video adapter to claim VGA compatibility, it must support all the features and code discussed in this book (with a very few minor exceptions that Ill note)and my experience is that just about 100 percent of the video hardware currently shipping or shipped since 1990 is in fact VGA compatible. Therefore, VGA code will run on nearly all of the 50,000,000 or so PC compatibles out there, with the exceptions being almost entirely obsolete machines from the 1980s. This makes good VGA code and VGA programming expertise valuable commodities indeed.
Right off the bat, Id like to make one thing perfectly clear: The VGA is hardsometimes very hardto program for good performance. Hard, but not impossibleand thats why I like this odd board. Its a throwback to an earlier generation of micros, when inventive coding and a solid understanding of the hardware were the best tools for improving performance. Increasingly, faster processors and powerful coprocessors are seen as the solution to the sluggish software produced by high-level languages and layers of interface and driver code, and thats surely a valid approach. However, there are tens of millions of VGAs installed right now, in machines ranging from 6-MHz 286s to 90-MHz Pentiums. Whats more, because the VGAs are generally 8- or at best 16-bit devices, and because of display memory wait states, a faster processor isnt as much of a help as youd expect. The upshot is that only a seasoned performance programmer who understands the VGA through and through can drive the board to its fullest potential.
Throughout this book, Ill explore the VGA by selecting a specific algorithm or feature and implementing code to support it on the VGA, examining aspects of the VGA architecture as they become relevant. Youll get to see VGA features in context, where they are more comprehensible than in IBMs somewhat arcane documentation, and youll get working code to use or to modify to meet your needs.
The prime directive of VGA programming is that theres rarely just one way to program the VGA for a given purpose. Once you understand the tools the VGA provides, youll be able to combine them to generate the particular synergy your application needs. My VGA routines are not intended to be taken as gospel, or to show best implementations, but rather to start you down the road to understanding the VGA.
Lets begin.
An Introduction to VGA Programming
Most discussions of the VGA start out with a traditional Heres a block diagram of the VGA approach, with lists of registers and statistics. Ill get to that eventually, but you can find it in IBMs VGA documentation and several other books. Besides, its numbing to read specifications and explanations, and the VGA is an exciting adapter, the kind that makes you want to get your hands dirty probing under the hood, to write some nifty code just to see what the board can do. Whats more, the best way to understand the VGA is to see it work, so lets jump right into a sample of the VGA in action, getting a feel for the VGAs architecture in the process.
Listing 23.1 is a sample VGA program that pans around an animated 16-color medium-resolution (640×350) playfield. Theres a lot packed into this code; Im going to focus on the VGA-specific aspects so we dont get sidetracked. Im not going to explain how the ball is animated, for example; well get to animation starting in Chapter 42. What I will do is cover each of the VGA features used in this programthe virtual screen, vertical and horizontal panning, color plane manipulation, multi-plane block copying, and page flippingat a conceptual level, letting the code itself demonstrate the implementation details. Well return to many of these concepts in more depth later in this book.
|