76 lines
4 KiB
Markdown
76 lines
4 KiB
Markdown
Chapter 53\
|
|
Raw Speed and More {#Heading1}
|
|
-------------------
|
|
|
|
### The Naked Truth About Speed in 3-D Animation {#Heading2}
|
|
|
|
Years ago, this friend of mine—let's call him Bert—went to Hawaii with
|
|
three other fellows to celebrate their graduation from high school. This
|
|
was an unchaperoned trip, and they behaved pretty much as responsibly as
|
|
you'd expect four teenagers to behave, which is to say, not; there's a
|
|
story about a rental car that, to this day, Bert can't bring himself to
|
|
tell. They had a good time, though, save for one thing: no girls.
|
|
|
|
By and by, they met a group of girls by the pool, but the boys couldn't
|
|
get past the hi-howya-doin stage, so they retired to their hotel room to
|
|
plot a better approach. This being the early '70s, and them being
|
|
slightly tipsy teenagers with raging hormones and the effective combined
|
|
IQ of four eggplants, it took them no time at all to come up with a
|
|
brilliant plan: streaking. The girls had mentioned their room number, so
|
|
the boys piled into the elevator, pushed the button for the girls'
|
|
floor, shucked their clothes as fast as they could, and sprinted to the
|
|
girls' door. They knocked on the door and ran on down the hall. As the
|
|
girls opened their door, Bert and his crew raced past, toward the
|
|
elevator, laughing hysterically.
|
|
|
|
Bert was by far the fastest of them all. He whisked between the elevator
|
|
doors just as they started to close; by the time his friends got there,
|
|
it was too late, and the doors slid shut in their faces. As the elevator
|
|
began to move, Bert could hear the frantic pounding of six fists
|
|
thudding on the closed doors. As Bert stood among the clothes littering
|
|
the elevator floor, the thought of his friends stuck in the hall, naked
|
|
as jaybirds, was just too much, and he doubled over with helpless
|
|
laughter, tears streaming down his face. The universe had blessed him
|
|
with one of those exceedingly rare moments of perfect timing and
|
|
execution.
|
|
|
|
The universe wasn't done with Bert quite yet, though. He was still
|
|
contorted with laughter—and still quite thoroughly undressed—when the
|
|
elevator doors opened again. On the lobby.
|
|
|
|
And with that, we come to this chapter's topics: raw speed and hidden
|
|
surfaces.
|
|
|
|
### Raw Speed, Part 1: Assembly Language {#Heading3}
|
|
|
|
I would like to state, here and for the record, that I am not an
|
|
assembly language fanatic. Frankly, I prefer programming in C; assembly
|
|
language is hard work, and I can get a whole lot more done with fewer
|
|
hassles in C. However, I *am* a performance fanatic, performance being
|
|
defined as having programs be as nimble as possible in those areas where
|
|
the user wants fast response. And, in the course of pursuing
|
|
performance, there are times when a little assembly language goes a long
|
|
way.
|
|
|
|
We're now four chapters into development of the X-Sharp 3-D animation
|
|
package. In realtime animation, performance is *sine qua non* (Latin for
|
|
"Make it fast or find another line of work"), so some judiciously
|
|
applied assembly language is in order. In the previous chapter, we got
|
|
up to a serviceable performance level by switching to fixed-point math,
|
|
then implementing the fixed-point multiplication and division functions
|
|
in assembly in order to take advantage of the 386's 32-bit capabilities.
|
|
There's another area of the program that fairly cries out for assembly
|
|
language: matrix math. The function to multiply a matrix by a vector
|
|
(**XformVec()**) and the function to concatenate matrices
|
|
(**ConcatXforms()**) both loop heavily around calls to **FixedMul();** a
|
|
lot of calling and looping can be eliminated by converting these
|
|
functions to pure assembly language.
|
|
|
|
Listing 53.1 is the module FIXED.ASM from this chapter's iteration of
|
|
X-Sharp, with **XformVec()** and **ConcatXforms()** implemented in
|
|
assembly language. The code is heavily optimized, to the extent of
|
|
completely unrolling the loops via macros so that looping is eliminated
|
|
altogether. FIXED.ASM is highly effective; the time taken for matrix
|
|
math is now down to the point where it's a fairly minor component of
|
|
execution time, representing less than ten percent of the total. It's
|
|
time to turn our optimization sights elsewhere.
|