second test with Quake3 Arena, this time in 1440x900...
differences from before:
* More performance tweaks / micro-optimization;
* Now the rasterizer supports multiple threads (2 threads were used in this test, with the screen divided in half);
* Inline ASM is used in a few places;
it is still sort of laggy, but I am not really sure how far software rasterization can be pushed on a generic desktop PC.
CPU: Phenom II X4 3.4 GHz;
RAM: 4x4GB PC3-1060
note: it is a fair bit faster at 1024x768 or lower...
basically, lacking much better to do recently, I wrote a basic software rasterizer and put an OpenGL front-end on it, then proceeded to make Quake 2 work on it:
yes, kind of sucks, but there are some limits as to what is likely doable on the CPU. also, it is plain C and single threaded scalar code (no SSE). a rasterizer using multiple threads and/or SSE could potentially do a little more, but I don't know, and don't expect there to really be much practical use for something like this, so alas.
writing something like this though does make one a little more aware what things are involved trying to get from geometry to the final output.
otherwise, may need to try to find something more relevant to do...
ADD: as-is, it basically mimics an "OpenGL Miniport" DLL, which means it exports the usual GL 1.1 calls, along with some WGL calls, and some wrapped GDI calls.
this is loaded up by Quake2, which then goes and uses "GetProcAddress" a bunch of times to fetch the various function pointers.
it has to export pretty much all of the 1.1 calls, though a lot of them are basically no-op stubs (would normally set an error status, currently rigged up to intentionally crash so the debugger can catch it...).
as for calls implemented: simple answer: about 1/4 to 1/2 of them.
as for functionality implemented by the rasterizer: * most stuff related to glBegin/glEnd; * things like glTexImage2D, glTexParameter, ... * various misc things, like glClear, glClearColor, glDepthRange, ... * matrix operations (glPushMatrix, glPopMatrix, ...) * ...
as for functionality not implemented: * texture-coordinate generation stuff; * display lists, selection buffers, accumulation buffer, ... * pretty much everything else where I was like "what is this and what would it be used for?" * currently doesn't do DrawArrays or DrawElements, but this may change. ** would basically be needed for Quake3 to work IIRC. ** partial provisions have been made, but logic isn't written yet. internally, it implements the actual drawing to the screen via CreateDIBSection and BitBlt and similar.
then it has a few buffers, for example, a color-buffer, implemented as an array of 32-bit pixel colors (in 0xAARRGGBB order, AKA, BGRA), as well as a Depth+Stencil buffer in Depth24_Stencil8 format (I was originally going to use Depth16 and no stencil, but then I realized that space for a stencil buffer could be provided "almost for free").
at its core, its main operation is "drawing spans", which look something like:
with functions which in turn use these for drawing triangles, ... (damn near everything is decomposed into triangles).
these functions are basically given raw pointers to the appropriate locations in the respective framebuffers.
the basic strategy for drawing each triangle is to sort the vertex from lowest to highest Y coordinate, then walk from one end of the triangle to the other, drawing each span.
* so: Y0=lowest, Y1=middle, Y2=highest * calculate stepping vectors for left/right sides (Y0 to Y1) * walk from Y0 to Y1, drawing each span. * recalculate vectors for Y1 to Y2 * walk from Y1 to Y2, drawing spans.
dunno if there is a better way.
the actual process rendering goes something like: * build up arrays of vertices via the glBegin/glEnd/glVertex interface; * if needed, decompose into triangles (pretty much everything other than GL_TRIANGLES is decomposed); ** ok, GL_QUADS is also semi-primitive, but in the rasterizer each quad is treated as a triangle pair. * feed vertex data through a combined Projection*ModelView matrix; * subdivide triangles, such that each triangle has a limited screen-area; ** needed or else textures warp all over the place (crazy bad texture deformation); ** each triangle, if sufficiently large, is split into 4 sub-triangles, which may happen recursively; *** Zelda Triforce logo configuration. ** quads are also divided into 4 pieces. * divide all vertex XYZ coordinates by W; * clip all the triangles/quads/... to fit on screen; * convert this into the form used by the rasterizer: ** fixed point XY values with separate Z. ** set magic numbers to indicate which drawing logic will be used. *** flat color? texture? needs fancy blending or tests? ... * hand them off to backend.
backend: * walks lists of triangles/quads, passing each to the appropriate rasterizer function. ** quads basically just invoke the triangle-drawer twice, first for vertices 0/1/2, then for 0/2/3. * there are different triangle-draw functions for different types of triangles (flat, textured, interpolated color, ...) ** in turn, function-pointers are often used to select the appropriate span-drawing functions. * the handling of blending/tests/... is basically done by assembling the blend-and-test logic out of function pointers. ** different functions for the different collections of tests to be performed; ** functions to select each individual operator for a given test *** GL_LESS vs GL_GEQUAL, GL_SRC_COLOR vs GL_DST_ALPHA, ... *** don't want to use switches, as these are *very slow* if done per-pixel. **** wanted to avoid the thing going at glacial speeds at least...