Use the faster span read/write template for 16bpp
[mesa.git] / src / mesa / drivers / dos / dmesa.c
index 4541559e2cafab94de87d2e72cb83c37d5b7cc73..9688dd95b26fb772051d03580392618b8880b6bc 100644 (file)
-/*\r
- * Mesa 3-D graphics library\r
- * Version:  5.0\r
- * \r
- * Copyright (C) 1999  Brian Paul   All Rights Reserved.\r
- * \r
- * Permission is hereby granted, free of charge, to any person obtaining a\r
- * copy of this software and associated documentation files (the "Software"),\r
- * to deal in the Software without restriction, including without limitation\r
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,\r
- * and/or sell copies of the Software, and to permit persons to whom the\r
- * Software is furnished to do so, subject to the following conditions:\r
- * \r
- * The above copyright notice and this permission notice shall be included\r
- * in all copies or substantial portions of the Software.\r
- * \r
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS\r
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL\r
- * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN\r
- * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
- */\r
-\r
-/*\r
- * DOS/DJGPP device driver v1.3 for Mesa 5.0\r
- *\r
- *  Copyright (C) 2002 - Borca Daniel\r
- *  Email : dborca@yahoo.com\r
- *  Web   : http://www.geocities.com/dborca\r
- */\r
-\r
-\r
-#ifndef FX\r
-\r
-#include "glheader.h"\r
-#include "context.h"\r
-#include "GL/dmesa.h"\r
-#include "extensions.h"\r
-#include "macros.h"\r
-#include "matrix.h"\r
-#include "mmath.h"\r
-#include "texformat.h"\r
-#include "texstore.h"\r
-#include "array_cache/acache.h"\r
-#include "swrast/s_context.h"\r
-#include "swrast/s_depth.h"\r
-#include "swrast/s_lines.h"\r
-#include "swrast/s_triangle.h"\r
-#include "swrast/s_trispan.h"\r
-#include "swrast/swrast.h"\r
-#include "swrast_setup/swrast_setup.h"\r
-#include "tnl/tnl.h"\r
-#include "tnl/t_context.h"\r
-#include "tnl/t_pipeline.h"\r
-\r
-#include "video.h"\r
-\r
-#else\r
-\r
-#include "../FX/fxdrv.h"\r
-#include "GL/dmesa.h"\r
-\r
-#endif\r
-\r
-\r
-\r
-/*\r
- * In C++ terms, this class derives from the GLvisual class.\r
- * Add system-specific fields to it.\r
- */\r
-struct dmesa_visual {\r
-   GLvisual *gl_visual;\r
-   GLboolean db_flag;           /* double buffered? */\r
-   GLboolean rgb_flag;          /* RGB mode? */\r
-   GLuint depth;                /* bits per pixel (1, 8, 24, etc) */\r
-};\r
-\r
-/*\r
- * In C++ terms, this class derives from the GLframebuffer class.\r
- * Add system-specific fields to it.\r
- */\r
-struct dmesa_buffer {\r
-   GLframebuffer gl_buffer;     /* The depth, stencil, accum, etc buffers */\r
-   void *the_window;            /* your window handle, etc */\r
-\r
-   int xpos, ypos;              /* position */\r
-   int width, height;           /* size in pixels */\r
-};\r
-\r
-/*\r
- * In C++ terms, this class derives from the GLcontext class.\r
- * Add system-specific fields to it.\r
- */\r
-struct dmesa_context {\r
-   GLcontext *gl_ctx;           /* the core library context */\r
-   DMesaVisual visual;\r
-   DMesaBuffer Buffer;\r
-   GLuint ClearColor;\r
-   GLuint ClearIndex;\r
-   /* etc... */\r
-};\r
-\r
-\r
-\r
-#ifndef FX\r
-/****************************************************************************\r
- * Read/Write pixels\r
- ***************************************************************************/\r
-#define FLIP(y)  (c->Buffer->height - (y) - 1)\r
-#define FLIP2(y) (b - (y))\r
-\r
-/****************************************************************************\r
- * RGB[A]\r
- ***************************************************************************/\r
-static void write_rgba_span (const GLcontext *ctx, GLuint n, GLint x, GLint y,\r
-                             const GLubyte rgba[][4], const GLubyte mask[])\r
-{\r
- const DMesaContext c = (DMesaContext)ctx->DriverCtx;\r
- GLuint i, offset;\r
-\r
- offset = c->Buffer->width * FLIP(y) + x;\r
- if (mask) {\r
-    /* draw some pixels */\r
-    for (i=0; i<n; i++, offset++) {\r
-        if (mask[i]) {\r
-           vl_putpixel(offset, vl_mixrgba(rgba[i]));\r
-        }\r
-    }\r
- } else {\r
-    /* draw all pixels */\r
-    for (i=0; i<n; i++, offset++) {\r
-        vl_putpixel(offset, vl_mixrgba(rgba[i]));\r
-    }\r
- }\r
-}\r
-\r
-\r
-\r
-static void write_rgb_span (const GLcontext *ctx, GLuint n, GLint x, GLint y,\r
-                            const GLubyte rgb[][3], const GLubyte mask[])\r
-{\r
- const DMesaContext c = (DMesaContext)ctx->DriverCtx;\r
- GLuint i, offset;\r
-\r
- offset = c->Buffer->width * FLIP(y) + x;\r
- if (mask) {\r
-    /* draw some pixels */\r
-    for (i=0; i<n; i++, offset++) {\r
-        if (mask[i]) {\r
-           vl_putpixel(offset, vl_mixrgb(rgb[i]));\r
-        }\r
-    }\r
- } else {\r
-    /* draw all pixels */\r
-    for (i=0; i<n; i++, offset++) {\r
-        vl_putpixel(offset, vl_mixrgb(rgb[i]));\r
-    }\r
- }\r
-}\r
-\r
-\r
-\r
-static void write_mono_rgba_span (const GLcontext *ctx,\r
-                                  GLuint n, GLint x, GLint y,\r
-                                  const GLchan color[4], const GLubyte mask[])\r
-{\r
- const DMesaContext c = (DMesaContext)ctx->DriverCtx;\r
- GLuint i, offset, rgba = vl_mixrgba(color);\r
-\r
- offset = c->Buffer->width * FLIP(y) + x;\r
- if (mask) {\r
-    /* draw some pixels */\r
-    for (i=0; i<n; i++, offset++) {\r
-        if (mask[i]) {\r
-           vl_putpixel(offset, rgba);\r
-        }\r
-    }\r
- } else {\r
-    /* draw all pixels */\r
-    for (i=0; i<n; i++, offset++) {\r
-        vl_putpixel(offset, rgba);\r
-    }\r
- }\r
-}\r
-\r
-\r
-\r
-static void read_rgba_span (const GLcontext *ctx, GLuint n, GLint x, GLint y,\r
-                            GLubyte rgba[][4])\r
-{\r
- const DMesaContext c = (DMesaContext)ctx->DriverCtx;\r
- GLuint i, offset;\r
-\r
- offset = c->Buffer->width * FLIP(y) + x;\r
- /* read all pixels */\r
- for (i=0; i<n; i++, offset++) {\r
-     vl_getrgba(offset, rgba[i]);\r
- }\r
-}\r
-\r
-\r
-\r
-static void write_rgba_pixels (const GLcontext *ctx,\r
-                               GLuint n, const GLint x[], const GLint y[],\r
-                               const GLubyte rgba[][4], const GLubyte mask[])\r
-{\r
- const DMesaContext c = (DMesaContext)ctx->DriverCtx;\r
- GLuint i, w = c->Buffer->width, b = c->Buffer->height - 1;\r
-\r
- if (mask) {\r
-    /* draw some pixels */\r
-    for (i=0; i<n; i++) {\r
-        if (mask[i]) {\r
-           vl_putpixel(FLIP2(y[i])*w + x[i], vl_mixrgba(rgba[i]));\r
-        }\r
-    }\r
- } else {\r
-    /* draw all pixels */\r
-    for (i=0; i<n; i++) {\r
-        vl_putpixel(FLIP2(y[i])*w + x[i], vl_mixrgba(rgba[i]));\r
-    }\r
- }\r
-}\r
-\r
-\r
-\r
-static void write_mono_rgba_pixels (const GLcontext *ctx,\r
-                                    GLuint n, const GLint x[], const GLint y[],\r
-                                    const GLchan color[4], const GLubyte mask[])\r
-{\r
- const DMesaContext c = (DMesaContext)ctx->DriverCtx;\r
- GLuint i, w = c->Buffer->width, b = c->Buffer->height - 1, rgba = vl_mixrgba(color);\r
-\r
- if (mask) {\r
-    /* draw some pixels */\r
-    for (i=0; i<n; i++) {\r
-        if (mask[i]) {\r
-           vl_putpixel(FLIP2(y[i])*w + x[i], rgba);\r
-        }\r
-    }\r
- } else {\r
-    /* draw all pixels */\r
-    for (i=0; i<n; i++) {\r
-        vl_putpixel(FLIP2(y[i])*w + x[i], rgba);\r
-    }\r
- }\r
-}\r
-\r
-\r
-\r
-static void read_rgba_pixels (const GLcontext *ctx,\r
-                              GLuint n, const GLint x[], const GLint y[],\r
-                              GLubyte rgba[][4], const GLubyte mask[])\r
-{\r
- const DMesaContext c = (DMesaContext)ctx->DriverCtx;\r
- GLuint i, w = c->Buffer->width, b = c->Buffer->height - 1;\r
-\r
- if (mask) {\r
-    /* read some pixels */\r
-    for (i=0; i<n; i++) {\r
-        if (mask[i]) {\r
-           vl_getrgba(FLIP2(y[i])*w + x[i], rgba[i]);\r
-        }\r
-    }\r
- } else {\r
-    /* read all pixels */\r
-    for (i=0; i<n; i++) {\r
-        vl_getrgba(FLIP2(y[i])*w + x[i], rgba[i]);\r
-    }\r
- }\r
-}\r
-\r
-/****************************************************************************\r
- * Index\r
- ***************************************************************************/\r
-static void write_index_span (const GLcontext *ctx, GLuint n, GLint x, GLint y,\r
-                              const GLuint index[], const GLubyte mask[])\r
-{\r
- const DMesaContext c = (DMesaContext)ctx->DriverCtx;\r
- GLuint i, offset;\r
-\r
- offset = c->Buffer->width * FLIP(y) + x;\r
- if (mask) {\r
-    /* draw some pixels */\r
-    for (i=0; i<n; i++, offset++) {\r
-        if (mask[i]) {\r
-           vl_putpixel(offset, index[i]);\r
-        }\r
-    }\r
- } else {\r
-    /* draw all pixels */\r
-    for (i=0; i<n; i++, offset++) {\r
-        vl_putpixel(offset, index[i]);\r
-    }\r
- }\r
-}\r
-\r
-\r
-\r
-static void write_index8_span (const GLcontext *ctx, GLuint n, GLint x, GLint y,\r
-                               const GLubyte index[], const GLubyte mask[])\r
-{\r
- const DMesaContext c = (DMesaContext)ctx->DriverCtx;\r
- GLuint i, offset;\r
-\r
- offset = c->Buffer->width * FLIP(y) + x;\r
- if (mask) {\r
-    /* draw some pixels */\r
-    for (i=0; i<n; i++, offset++) {\r
-        if (mask[i]) {\r
-           vl_putpixel(offset, index[i]);\r
-        }\r
-    }\r
- } else {\r
-    /* draw all pixels */\r
-    for (i=0; i<n; i++, offset++) {\r
-        vl_putpixel(offset, index[i]);\r
-    }\r
- }\r
-}\r
-\r
-\r
-\r
-static void write_mono_index_span (const GLcontext *ctx,\r
-                                   GLuint n, GLint x, GLint y,\r
-                                   GLuint colorIndex, const GLubyte mask[])\r
-{\r
- const DMesaContext c = (DMesaContext)ctx->DriverCtx;\r
- GLuint i, offset;\r
-\r
- offset = c->Buffer->width * FLIP(y) + x;\r
- if (mask) {\r
-    /* draw some pixels */\r
-    for (i=0; i<n; i++, offset++) {\r
-        if (mask[i]) {\r
-           vl_putpixel(offset, colorIndex);\r
-        }\r
-    }\r
- } else {\r
-    /* draw all pixels */\r
-    for (i=0; i<n; i++, offset++) {\r
-        vl_putpixel(offset, colorIndex);\r
-    }\r
- }\r
-}\r
-\r
-\r
-\r
-static void read_index_span (const GLcontext *ctx, GLuint n, GLint x, GLint y,\r
-                             GLuint index[])\r
-{\r
- const DMesaContext c = (DMesaContext)ctx->DriverCtx;\r
- GLuint i, offset;\r
-\r
- offset = c->Buffer->width * FLIP(y) + x;\r
- /* read all pixels */\r
- for (i=0; i<n; i++, offset++) {\r
-     index[i] = vl_getCIpixel(offset);\r
- }\r
-}\r
-\r
-\r
-\r
-static void write_index_pixels (const GLcontext *ctx,\r
-                                GLuint n, const GLint x[], const GLint y[],\r
-                                const GLuint index[], const GLubyte mask[])\r
-{\r
- const DMesaContext c = (DMesaContext)ctx->DriverCtx;\r
- GLuint i, w = c->Buffer->width, b = c->Buffer->height - 1;\r
-\r
- if (mask) {\r
-    /* draw some pixels */\r
-    for (i=0; i<n; i++) {\r
-        if (mask[i]) {\r
-           vl_putpixel(FLIP2(y[i])*w + x[i], index[i]);\r
-        }\r
-    }\r
- } else {\r
-    /* draw all pixels */\r
-    for (i=0; i<n; i++) {\r
-        vl_putpixel(FLIP2(y[i])*w + x[i], index[i]);\r
-    }\r
- }\r
-}\r
-\r
-\r
-\r
-static void write_mono_index_pixels (const GLcontext *ctx,\r
-                                     GLuint n, const GLint x[], const GLint y[],\r
-                                     GLuint colorIndex, const GLubyte mask[])\r
-{\r
- const DMesaContext c = (DMesaContext)ctx->DriverCtx;\r
- GLuint i, w = c->Buffer->width, b = c->Buffer->height - 1;\r
-\r
- if (mask) {\r
-    /* draw some pixels */\r
-    for (i=0; i<n; i++) {\r
-        if (mask[i]) {\r
-           vl_putpixel(FLIP2(y[i])*w + x[i], colorIndex);\r
-        }\r
-    }\r
- } else {\r
-    /* draw all pixels */\r
-    for (i=0; i<n; i++) {\r
-        vl_putpixel(FLIP2(y[i])*w + x[i], colorIndex);\r
-    }\r
- }\r
-}\r
-\r
-\r
-\r
-static void read_index_pixels (const GLcontext *ctx,\r
-                               GLuint n, const GLint x[], const GLint y[],\r
-                               GLuint index[], const GLubyte mask[])\r
-{\r
- const DMesaContext c = (DMesaContext)ctx->DriverCtx;\r
- GLuint i, w = c->Buffer->width, b = c->Buffer->height - 1;\r
-\r
- if (mask) {\r
-    /* read some pixels */\r
-    for (i=0; i<n; i++) {\r
-        if (mask[i]) {\r
-           index[i] = vl_getCIpixel(FLIP2(y[i])*w + x[i]);\r
-        }\r
-    }\r
- } else {\r
-    /* read all pixels */\r
-    for (i=0; i<n; i++) {\r
-        index[i] = vl_getCIpixel(FLIP2(y[i])*w + x[i]);\r
-    }\r
- }\r
-}\r
-\r
-\r
-\r
-/****************************************************************************\r
- * Optimized triangle rendering\r
- ***************************************************************************/\r
-\r
-/*\r
- * flat, NON-depth-buffered, triangle.\r
- */\r
-static void tri_rgb_flat (GLcontext *ctx,\r
-                          const SWvertex *v0,\r
-                          const SWvertex *v1,\r
-                          const SWvertex *v2)\r
-{\r
- const DMesaContext c = (DMesaContext)ctx->DriverCtx;\r
- GLuint w = c->Buffer->width, b = c->Buffer->height - 1;\r
-\r
-#define SETUP_CODE GLuint rgb = vl_mixrgb(v2->color);\r
-\r
-#define RENDER_SPAN(span)                                      \\r
- GLuint i, offset = FLIP2(span.y)*w + span.x;                  \\r
- for (i = 0; i < span.end; i++, offset++) {                    \\r
-     vl_putpixel(offset, rgb);                                 \\r
- }\r
-\r
-#include "swrast/s_tritemp.h"\r
-}\r
-\r
-\r
-\r
-/*\r
- * flat, depth-buffered, triangle.\r
- */\r
-static void tri_rgb_flat_z (GLcontext *ctx,\r
-                            const SWvertex *v0,\r
-                            const SWvertex *v1,\r
-                            const SWvertex *v2)\r
-{\r
- const DMesaContext c = (DMesaContext)ctx->DriverCtx;\r
- GLuint w = c->Buffer->width, b = c->Buffer->height - 1;\r
-\r
-#define INTERP_Z 1\r
-#define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE\r
-#define SETUP_CODE GLuint rgb = vl_mixrgb(v2->color);\r
-\r
-#define RENDER_SPAN(span)                                      \\r
- GLuint i, offset = FLIP2(span.y)*w + span.x;                  \\r
- for (i = 0; i < span.end; i++, offset++) {                    \\r
-     const DEPTH_TYPE z = FixedToDepth(span.z);                        \\r
-     if (z < zRow[i]) {                                                \\r
-        vl_putpixel(offset, rgb);                              \\r
-        zRow[i] = z;                                           \\r
-     }                                                         \\r
-     span.z += span.zStep;                                     \\r
- }\r
-\r
-#include "swrast/s_tritemp.h"\r
-}\r
-\r
-\r
-\r
-/*\r
- * smooth, NON-depth-buffered, triangle.\r
- */\r
-static void tri_rgb_smooth (GLcontext *ctx,\r
-                            const SWvertex *v0,\r
-                            const SWvertex *v1,\r
-                            const SWvertex *v2)\r
-{\r
- const DMesaContext c = (DMesaContext)ctx->DriverCtx;\r
- GLuint w = c->Buffer->width, b = c->Buffer->height - 1;\r
-\r
-#define INTERP_RGB 1\r
-#define RENDER_SPAN(span)                                              \\r
- GLuint i, offset = FLIP2(span.y)*w + span.x;                          \\r
- for (i = 0; i < span.end; i++, offset++) {                            \\r
-     vl_putpixel(offset, vl_mixfix(span.red, span.green, span.blue));  \\r
-     span.red += span.redStep;                                         \\r
-     span.green += span.greenStep;                                     \\r
-     span.blue += span.blueStep;                                       \\r
- }\r
-\r
-#include "swrast/s_tritemp.h"\r
-}\r
-\r
-\r
-\r
-/*\r
- * smooth, depth-buffered, triangle.\r
- */\r
-static void tri_rgb_smooth_z (GLcontext *ctx,\r
-                              const SWvertex *v0,\r
-                              const SWvertex *v1,\r
-                              const SWvertex *v2)\r
-{\r
- const DMesaContext c = (DMesaContext)ctx->DriverCtx;\r
- GLuint w = c->Buffer->width, b = c->Buffer->height - 1;\r
-\r
-#define INTERP_Z 1\r
-#define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE\r
-#define INTERP_RGB 1\r
-\r
-#define RENDER_SPAN(span)                                              \\r
- GLuint i, offset = FLIP2(span.y)*w + span.x;                          \\r
- for (i = 0; i < span.end; i++, offset++) {                            \\r
-     const DEPTH_TYPE z = FixedToDepth(span.z);                                \\r
-     if (z < zRow[i]) {                                                        \\r
-        vl_putpixel(offset, vl_mixfix(span.red, span.green, span.blue));\\r
-        zRow[i] = z;                                                   \\r
-     }                                                                 \\r
-     span.red += span.redStep;                                         \\r
-     span.green += span.greenStep;                                     \\r
-     span.blue += span.blueStep;                                       \\r
-     span.z += span.zStep;                                             \\r
- }\r
-\r
-#include "swrast/s_tritemp.h"\r
-}\r
-\r
-\r
-\r
-/*\r
- * Analyze context state to see if we can provide a fast triangle function\r
- * Otherwise, return NULL.\r
- */\r
-static swrast_tri_func dmesa_choose_tri_function (GLcontext *ctx)\r
-{\r
- const SWcontext *swrast = SWRAST_CONTEXT(ctx);\r
-\r
- if ((ctx->RenderMode != GL_RENDER)\r
-     || (ctx->Polygon.SmoothFlag)\r
-     || (ctx->Polygon.StippleFlag)\r
-     || (ctx->Texture._EnabledUnits)\r
-     || (swrast->_RasterMask & MULTI_DRAW_BIT)\r
-     || ((ctx->Polygon.CullFlag && ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK))) {\r
-    return (swrast_tri_func)NULL;\r
- }\r
-\r
- if (swrast->_RasterMask==DEPTH_BIT\r
-     && ctx->Depth.Func==GL_LESS\r
-     && ctx->Depth.Mask==GL_TRUE\r
-     && ctx->Visual.depthBits == DEFAULT_SOFTWARE_DEPTH_BITS) {\r
-    return (ctx->Light.ShadeModel==GL_SMOOTH) ? tri_rgb_smooth_z : tri_rgb_flat_z;\r
- }\r
-\r
- if (swrast->_RasterMask==0) { /* no depth test */\r
-    return (ctx->Light.ShadeModel==GL_SMOOTH) ? tri_rgb_smooth : tri_rgb_flat;\r
- }\r
-\r
- return (swrast_tri_func)NULL;\r
-}\r
-\r
-\r
-\r
-/* Override for the swrast triangle-selection function.  Try to use one\r
- * of our internal triangle functions, otherwise fall back to the\r
- * standard swrast functions.\r
- */\r
-static void dmesa_choose_tri (GLcontext *ctx)\r
-{\r
- SWcontext *swrast = SWRAST_CONTEXT(ctx);\r
-\r
- if (!(swrast->Triangle=dmesa_choose_tri_function(ctx)))\r
-    _swrast_choose_triangle(ctx);\r
-}\r
-\r
-\r
-\r
-/****************************************************************************\r
- * Miscellaneous device driver funcs\r
- ***************************************************************************/\r
-\r
-static void clear_index (GLcontext *ctx, GLuint index)\r
-{\r
- DMesaContext c = (DMesaContext)ctx->DriverCtx;\r
-\r
- c->ClearIndex = index;\r
-}\r
-\r
-static void clear_color (GLcontext *ctx, const GLfloat color[4])\r
-{\r
- GLubyte col[4];\r
- DMesaContext c = (DMesaContext)ctx->DriverCtx;\r
- CLAMPED_FLOAT_TO_UBYTE(col[0], color[0]);\r
- CLAMPED_FLOAT_TO_UBYTE(col[1], color[1]);\r
- CLAMPED_FLOAT_TO_UBYTE(col[2], color[2]);\r
- CLAMPED_FLOAT_TO_UBYTE(col[3], color[3]);\r
- c->ClearColor = vl_mixrgba(col);\r
-}\r
-\r
-\r
-\r
-static void clear (GLcontext *ctx, GLbitfield mask, GLboolean all,\r
-                   GLint x, GLint y, GLint width, GLint height)\r
-{\r
- const DMesaContext c = (DMesaContext)ctx->DriverCtx;\r
- const GLuint *colorMask = (GLuint *)&ctx->Color.ColorMask;\r
-\r
-/*\r
- * Clear the specified region of the buffers indicated by 'mask'\r
- * using the clear color or index as specified by one of the two\r
- * functions above.\r
- * If all==GL_TRUE, clear whole buffer, else just clear region defined\r
- * by x,y,width,height\r
- */\r
-\r
- /* we can't handle color or index masking */\r
- if ((*colorMask == 0xffffffff) && (ctx->Color.IndexMask == 0xffffffff)) {\r
-    if (mask & DD_BACK_LEFT_BIT) {\r
-       int color = c->visual->rgb_flag ? c->ClearColor : c->ClearIndex;\r
-\r
-       if (all) {\r
-          vl_clear(color);\r
-       } else {\r
-          vl_rect(x, y, width, height, color);\r
-       }\r
-\r
-       mask &= ~DD_BACK_LEFT_BIT;\r
-    }\r
- }\r
-\r
- if (mask) {\r
-    _swrast_Clear(ctx, mask, all, x, y, width, height);\r
- }\r
-}\r
-\r
-\r
-\r
-static void set_buffer (GLcontext *ctx, GLframebuffer *colorBuffer, GLuint bufferBit)\r
-{\r
- /*\r
-  * XXX todo - examine bufferBit and set read/write pointers\r
-  */\r
-}\r
-\r
-\r
-\r
-/*\r
- * Return the width and height of the current buffer.\r
- * If anything special has to been done when the buffer/window is\r
- * resized, do it now.\r
- */\r
-static void get_buffer_size (GLframebuffer *buffer, GLuint *width, GLuint *height)\r
-{\r
- DMesaBuffer b = (DMesaBuffer)buffer;\r
-\r
- *width  = b->width;\r
- *height = b->height;\r
-}\r
-\r
-\r
-\r
-static const GLubyte* get_string (GLcontext *ctx, GLenum name)\r
-{\r
- switch (name) {\r
-        case GL_RENDERER:\r
-             return (const GLubyte *)"Mesa DJGPP\0port (c) Borca Daniel dec-2002";\r
-        default:\r
-             return NULL;\r
- }\r
-}\r
-\r
-\r
-\r
-static void finish (GLcontext *ctx)\r
-{\r
- /*\r
-  * XXX todo - OPTIONAL FUNCTION: implements glFinish if possible\r
-  */\r
-}\r
-\r
-\r
-\r
-static void flush (GLcontext *ctx)\r
-{\r
- /*\r
-  * XXX todo - OPTIONAL FUNCTION: implements glFlush if possible\r
-  */\r
-}\r
-\r
-\r
-\r
-/****************************************************************************\r
- * State\r
- ***************************************************************************/\r
-#define DMESA_NEW_TRIANGLE (_NEW_POLYGON | \\r
-                            _NEW_TEXTURE | \\r
-                            _NEW_LIGHT | \\r
-                            _NEW_DEPTH | \\r
-                            _NEW_RENDERMODE | \\r
-                            _SWRAST_NEW_RASTERMASK)\r
-\r
-/* Extend the software rasterizer with our line and triangle\r
- * functions.\r
- */\r
-static void dmesa_register_swrast_functions (GLcontext *ctx)\r
-{\r
- SWcontext *swrast = SWRAST_CONTEXT(ctx);\r
-\r
- swrast->choose_triangle = dmesa_choose_tri;\r
-\r
- swrast->invalidate_triangle |= DMESA_NEW_TRIANGLE;\r
-}\r
-\r
-\r
-\r
-/* Setup pointers and other driver state that is constant for the life\r
- * of a context.\r
- */\r
-static void dmesa_init_pointers (GLcontext *ctx)\r
-{\r
- TNLcontext *tnl;\r
- struct swrast_device_driver *dd = _swrast_GetDeviceDriverReference(ctx);\r
-\r
- ctx->Driver.GetString = get_string;\r
- ctx->Driver.GetBufferSize = get_buffer_size;\r
- ctx->Driver.Flush = flush;\r
- ctx->Driver.Finish = finish;\r
-    \r
- /* Software rasterizer pixel paths:\r
-  */\r
- ctx->Driver.Accum = _swrast_Accum;\r
- ctx->Driver.Bitmap = _swrast_Bitmap;\r
- ctx->Driver.Clear = clear;\r
- ctx->Driver.ResizeBuffers = _swrast_alloc_buffers;\r
- ctx->Driver.CopyPixels = _swrast_CopyPixels;\r
- ctx->Driver.DrawPixels = _swrast_DrawPixels;\r
- ctx->Driver.ReadPixels = _swrast_ReadPixels;\r
- ctx->Driver.DrawBuffer = _swrast_DrawBuffer;\r
-\r
- /* Software texture functions:\r
-  */\r
- ctx->Driver.ChooseTextureFormat = _mesa_choose_tex_format;\r
- ctx->Driver.TexImage1D = _mesa_store_teximage1d;\r
- ctx->Driver.TexImage2D = _mesa_store_teximage2d;\r
- ctx->Driver.TexImage3D = _mesa_store_teximage3d;\r
- ctx->Driver.TexSubImage1D = _mesa_store_texsubimage1d;\r
- ctx->Driver.TexSubImage2D = _mesa_store_texsubimage2d;\r
- ctx->Driver.TexSubImage3D = _mesa_store_texsubimage3d;\r
- ctx->Driver.TestProxyTexImage = _mesa_test_proxy_teximage;\r
-\r
- ctx->Driver.CopyTexImage1D = _swrast_copy_teximage1d;\r
- ctx->Driver.CopyTexImage2D = _swrast_copy_teximage2d;\r
- ctx->Driver.CopyTexSubImage1D = _swrast_copy_texsubimage1d;\r
- ctx->Driver.CopyTexSubImage2D = _swrast_copy_texsubimage2d;\r
- ctx->Driver.CopyTexSubImage3D = _swrast_copy_texsubimage3d;\r
-\r
- ctx->Driver.CompressedTexImage1D = _mesa_store_compressed_teximage1d;\r
- ctx->Driver.CompressedTexImage2D = _mesa_store_compressed_teximage2d;\r
- ctx->Driver.CompressedTexImage3D = _mesa_store_compressed_teximage3d;\r
- ctx->Driver.CompressedTexSubImage1D = _mesa_store_compressed_texsubimage1d;\r
- ctx->Driver.CompressedTexSubImage2D = _mesa_store_compressed_texsubimage2d;\r
- ctx->Driver.CompressedTexSubImage3D = _mesa_store_compressed_texsubimage3d;\r
-\r
- /* Swrast hooks for imaging extensions:\r
-  */\r
- ctx->Driver.CopyColorTable = _swrast_CopyColorTable;\r
- ctx->Driver.CopyColorSubTable = _swrast_CopyColorSubTable;\r
- ctx->Driver.CopyConvolutionFilter1D = _swrast_CopyConvolutionFilter1D;\r
- ctx->Driver.CopyConvolutionFilter2D = _swrast_CopyConvolutionFilter2D;\r
-\r
- /* Statechange callbacks:\r
-  */\r
- ctx->Driver.ClearColor = clear_color;\r
- ctx->Driver.ClearIndex = clear_index;\r
-\r
- /* Initialize the TNL driver interface:\r
-  */\r
- tnl = TNL_CONTEXT(ctx);\r
- tnl->Driver.RunPipeline = _tnl_run_pipeline;\r
-\r
- dd->SetBuffer = set_buffer;\r
-   \r
- /* Install swsetup for tnl->Driver.Render.*:\r
-  */\r
- _swsetup_Wakeup(ctx);\r
-}\r
-\r
-\r
-\r
-static void dmesa_update_state (GLcontext *ctx, GLuint new_state)\r
-{\r
- struct swrast_device_driver *dd = _swrast_GetDeviceDriverReference(ctx);\r
-\r
- /* Propogate statechange information to swrast and swrast_setup\r
-  * modules. The DMesa driver has no internal GL-dependent state.\r
-  */\r
- _swrast_InvalidateState( ctx, new_state );\r
- _ac_InvalidateState( ctx, new_state );\r
- _tnl_InvalidateState( ctx, new_state );\r
- _swsetup_InvalidateState( ctx, new_state );\r
-\r
- /* Index span/pixel functions */\r
- dd->WriteCI32Span = write_index_span;\r
- dd->WriteCI8Span = write_index8_span;\r
- dd->WriteMonoCISpan = write_mono_index_span;\r
- dd->WriteCI32Pixels = write_index_pixels;\r
- dd->WriteMonoCIPixels = write_mono_index_pixels;\r
- dd->ReadCI32Span = read_index_span;\r
- dd->ReadCI32Pixels = read_index_pixels;\r
-\r
- /* RGB(A) span/pixel functions */\r
- dd->WriteRGBASpan = write_rgba_span;\r
- dd->WriteRGBSpan = write_rgb_span;\r
- dd->WriteMonoRGBASpan = write_mono_rgba_span;\r
- dd->WriteRGBAPixels = write_rgba_pixels;\r
- dd->WriteMonoRGBAPixels = write_mono_rgba_pixels;\r
- dd->ReadRGBASpan = read_rgba_span;\r
- dd->ReadRGBAPixels = read_rgba_pixels;\r
-}\r
-#endif\r
-\r
-\r
-\r
-/****************************************************************************\r
- * DMesa Public API Functions\r
- ***************************************************************************/\r
-\r
-/*\r
- * The exact arguments to this function will depend on your window system\r
- */\r
-DMesaVisual DMesaCreateVisual (GLint width,\r
-                               GLint height,\r
-                               GLint colDepth,\r
-                               GLint refresh,\r
-                               GLboolean dbFlag,\r
-                               GLboolean rgbFlag,\r
-                               GLboolean alphaFlag,\r
-                               GLint depthSize,\r
-                               GLint stencilSize,\r
-                               GLint accumSize)\r
-{\r
-#ifndef FX\r
- DMesaVisual v;\r
- GLint redBits, greenBits, blueBits, alphaBits, indexBits;\r
-\r
- if (!dbFlag) {\r
-    return NULL;\r
- }\r
-\r
- alphaBits = 0;\r
-\r
- if (!rgbFlag) {\r
-    indexBits = 8;\r
-    redBits = 0;\r
-    greenBits = 0;\r
-    blueBits = 0;\r
- } else {\r
-    indexBits = 0;\r
-    switch (colDepth) {\r
-           case 8:\r
-                redBits = 8;\r
-                greenBits = 8;\r
-                blueBits = 8;\r
-                break;\r
-           case 15:\r
-                redBits = 5;\r
-                greenBits = 5;\r
-                blueBits = 5;\r
-                break;\r
-           case 16:\r
-                redBits = 5;\r
-                greenBits = 6;\r
-                blueBits = 5;\r
-                break;\r
-           case 32:\r
-                alphaBits = 8;\r
-           case 24:\r
-                redBits = 8;\r
-                greenBits = 8;\r
-                blueBits = 8;\r
-                break;\r
-           default:\r
-                return NULL;\r
-    }\r
- }\r
-\r
- if ((colDepth=vl_video_init(width, height, colDepth, rgbFlag, refresh)) <= 0) {\r
-    return NULL;\r
- }\r
-\r
- if (alphaFlag && (alphaBits==0)) {\r
-    alphaBits = 8;\r
- }\r
-\r
- if ((v=(DMesaVisual)calloc(1, sizeof(struct dmesa_visual))) != NULL) {\r
-    /* Create core visual */\r
-    v->gl_visual = _mesa_create_visual(rgbFlag,                        /* rgb */\r
-                                       dbFlag,\r
-                                       GL_FALSE,               /* stereo */\r
-                                       redBits,\r
-                                       greenBits,\r
-                                       blueBits,\r
-                                       alphaBits,\r
-                                       indexBits,              /* indexBits */\r
-                                       depthSize,\r
-                                       stencilSize,\r
-                                       accumSize,              /* accumRed */\r
-                                       accumSize,              /* accumGreen */\r
-                                       accumSize,              /* accumBlue */\r
-                                       alphaFlag?accumSize:0,  /* accumAlpha */\r
-                                       1);                     /* numSamples */\r
-\r
-    v->depth = colDepth;\r
-    v->db_flag = dbFlag;\r
-    v->rgb_flag = rgbFlag;\r
- }\r
-\r
- return v;\r
-\r
-#else\r
-\r
- int i = 0, fx_attrib[32];\r
-\r
- if (!rgbFlag) {\r
-    return NULL;\r
- }\r
-\r
- if (dbFlag) fx_attrib[i++] = FXMESA_DOUBLEBUFFER;\r
- if (depthSize > 0) { fx_attrib[i++] = FXMESA_DEPTH_SIZE; fx_attrib[i++] = depthSize; }\r
- if (stencilSize > 0) { fx_attrib[i++] = FXMESA_STENCIL_SIZE; fx_attrib[i++] = stencilSize; }\r
- if (accumSize > 0) { fx_attrib[i++] = FXMESA_ACCUM_SIZE; fx_attrib[i++] = accumSize; }\r
- if (alphaFlag) { fx_attrib[i++] = FXMESA_ALPHA_SIZE; fx_attrib[i++] = 1; }\r
- fx_attrib[i] = FXMESA_NONE;\r
-\r
- return (DMesaVisual)fxMesaCreateBestContext(-1, width, height, fx_attrib);\r
-#endif\r
-}\r
-\r
-\r
-\r
-void DMesaDestroyVisual (DMesaVisual v)\r
-{\r
-#ifndef FX\r
- _mesa_destroy_visual(v->gl_visual);\r
- free(v);\r
-\r
- vl_video_exit();\r
-#else\r
- fxMesaDestroyContext((fxMesaContext)v);\r
-#endif\r
-}\r
-\r
-\r
-\r
-DMesaBuffer DMesaCreateBuffer (DMesaVisual visual,\r
-                               GLint xpos, GLint ypos,\r
-                               GLint width, GLint height)\r
-{\r
-#ifndef FX\r
- DMesaBuffer b;\r
-\r
- if ((b=(DMesaBuffer)calloc(1, sizeof(struct dmesa_buffer))) != NULL) {\r
-\r
-    _mesa_initialize_framebuffer(&b->gl_buffer,\r
-                                 visual->gl_visual,\r
-                                 visual->gl_visual->depthBits > 0,\r
-                                 visual->gl_visual->stencilBits > 0,\r
-                                 visual->gl_visual->accumRedBits > 0,\r
-                                 visual->gl_visual->alphaBits > 0);\r
-    b->xpos = xpos;\r
-    b->ypos = ypos;\r
-    b->width = width;\r
-    b->height = height;\r
- }\r
-\r
- return b;\r
-#else\r
- return (DMesaBuffer)visual;\r
-#endif\r
-}\r
-\r
-\r
-\r
-void DMesaDestroyBuffer (DMesaBuffer b)\r
-{\r
-#ifndef FX\r
- free(b->the_window);\r
- _mesa_free_framebuffer_data(&b->gl_buffer);\r
- free(b);\r
-#endif\r
-}\r
-\r
-\r
-\r
-DMesaContext DMesaCreateContext (DMesaVisual visual,\r
-                                 DMesaContext share)\r
-{\r
-#ifndef FX\r
- DMesaContext c;\r
- GLboolean direct = GL_FALSE;\r
-\r
- if ((c=(DMesaContext)calloc(1, sizeof(struct dmesa_context))) != NULL) {\r
-    c->gl_ctx = _mesa_create_context(visual->gl_visual,\r
-                                     share ? share->gl_ctx : NULL,\r
-                                     (void *)c, direct);\r
-\r
-    _mesa_enable_sw_extensions(c->gl_ctx);\r
-    _mesa_enable_1_3_extensions(c->gl_ctx);\r
-    _mesa_enable_1_4_extensions(c->gl_ctx);\r
-\r
-    /* you probably have to do a bunch of other initializations here. */\r
-    c->visual = visual;\r
-\r
-    c->gl_ctx->Driver.UpdateState = dmesa_update_state;\r
-\r
-    /* Initialize the software rasterizer and helper modules.\r
-     */\r
-    _swrast_CreateContext(c->gl_ctx);\r
-    _ac_CreateContext(c->gl_ctx);\r
-    _tnl_CreateContext(c->gl_ctx);\r
-    _swsetup_CreateContext(c->gl_ctx);\r
-    if (visual->rgb_flag) dmesa_register_swrast_functions(c->gl_ctx);\r
-    dmesa_init_pointers(c->gl_ctx);\r
- }\r
-\r
- return c;\r
-\r
-#else\r
-\r
- return (DMesaContext)visual;\r
-#endif\r
-}\r
-\r
-\r
-\r
-void DMesaDestroyContext (DMesaContext c)\r
-{\r
-#ifndef FX\r
- if (c->gl_ctx) {\r
-    _swsetup_DestroyContext(c->gl_ctx);\r
-    _swrast_DestroyContext(c->gl_ctx);\r
-    _tnl_DestroyContext(c->gl_ctx);\r
-    _ac_DestroyContext(c->gl_ctx);\r
-    _mesa_destroy_context(c->gl_ctx);\r
- }\r
- free(c);\r
-#endif\r
-}\r
-\r
-\r
-\r
-GLboolean DMesaViewport (DMesaBuffer b,\r
-                         GLint xpos, GLint ypos,\r
-                         GLint width, GLint height)\r
-{\r
-#ifndef FX\r
- void *new_window;\r
-\r
- if ((new_window=vl_sync_buffer(b->the_window, xpos, ypos, width, height)) == NULL) {\r
-    return GL_FALSE;\r
- } else {\r
-    b->the_window = new_window;\r
-    b->xpos = xpos;\r
-    b->ypos = ypos;\r
-    b->width = width;\r
-    b->height = height;\r
-    return GL_TRUE;\r
- }\r
-\r
-#else\r
-\r
- return GL_FALSE;\r
-#endif\r
-}\r
-\r
-\r
-\r
-/*\r
- * Make the specified context and buffer the current one.\r
- */\r
-GLboolean DMesaMakeCurrent (DMesaContext c, DMesaBuffer b)\r
-{\r
-#ifndef FX\r
- if ((c != NULL) && (b != NULL)) {\r
-    if (!DMesaViewport(b, b->xpos, b->ypos, b->width, b->height)) {\r
-       return GL_FALSE;\r
-    }\r
-\r
-    c->Buffer = b;\r
-\r
-    _mesa_make_current(c->gl_ctx, &b->gl_buffer);\r
-    if (c->gl_ctx->Viewport.Width == 0) {\r
-       /* initialize viewport to window size */\r
-       _mesa_Viewport(0, 0, b->width, b->height);\r
-    }\r
- } else {\r
-    /* Detach */\r
-    _mesa_make_current(NULL, NULL);\r
- }\r
-\r
-#else\r
-\r
- fxMesaMakeCurrent((fxMesaContext)c);\r
-#endif\r
-\r
- return GL_TRUE;\r
-}\r
-\r
-\r
-\r
-void DMesaSwapBuffers (DMesaBuffer b)\r
-{\r
- /* copy/swap back buffer to front if applicable */\r
-#ifndef FX\r
- GET_CURRENT_CONTEXT(ctx);\r
- _mesa_notifySwapBuffers(ctx);\r
- vl_flip();\r
-#else\r
- fxMesaSwapBuffers();\r
-#endif\r
-}\r
-\r
-\r
-\r
-void DMesaSetCI (int ndx, GLfloat red, GLfloat green, GLfloat blue)\r
-{\r
-#ifndef FX\r
- vl_setCI(ndx, red, green, blue);\r
-#endif\r
-}\r
+/*
+ * Mesa 3-D graphics library
+ * Version:  6.3
+ * 
+ * Copyright (C) 1999-2004  Brian Paul   All Rights Reserved.
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included
+ * in all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
+ * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+/*
+ * DOS/DJGPP device driver v1.7 for Mesa
+ *
+ *  Copyright (c) 2003 - Daniel Borca
+ *  Email : dborca@users.sourceforge.net
+ *  Web   : http://www.geocities.com/dborca
+ */
+
+
+#include "glheader.h"
+#include "context.h"
+#include "imports.h"
+#ifndef FX
+#include "bufferobj.h"
+#include "buffers.h"
+#include "extensions.h"
+#include "macros.h"
+#include "matrix.h"
+#include "mtypes.h"
+#include "texformat.h"
+#include "teximage.h"
+#include "texstore.h"
+#include "array_cache/acache.h"
+#include "swrast/s_context.h"
+#include "swrast/s_depth.h"
+#include "swrast/s_lines.h"
+#include "swrast/s_triangle.h"
+#include "swrast/swrast.h"
+#include "swrast_setup/swrast_setup.h"
+#include "tnl/tnl.h"
+#include "tnl/t_context.h"
+#include "tnl/t_pipeline.h"
+#include "drivers/common/driverfuncs.h"
+#include "video.h"
+#else  /* FX */
+#include "GL/fxmesa.h"
+#endif /* FX */
+
+#include "GL/dmesa.h"
+
+
+#define SWTC 0 /* SW texture compression */
+
+
+/*
+ * In C++ terms, this class derives from the GLvisual class.
+ * Add system-specific fields to it.
+ */
+struct dmesa_visual {
+   GLvisual gl_visual;
+   GLboolean sw_alpha;          /* use Mesa's alpha buffer? */
+   int z_buffer;                /* Z=buffer: 0=no, 1=SW, -1=HW */
+};
+
+/*
+ * In C++ terms, this class derives from the GLframebuffer class.
+ * Add system-specific fields to it.
+ */
+struct dmesa_buffer {
+   GLframebuffer gl_buffer;     /* The depth, stencil, accum, etc buffers */
+   void *the_window;            /* your window handle, etc */
+
+   int xpos, ypos;              /* position */
+   int width, height;           /* size in pixels */
+};
+
+/*
+ * In C++ terms, this class derives from the GLcontext class.
+ * Add system-specific fields to it.
+ */
+struct dmesa_context {
+   GLcontext gl_ctx;            /* the core library context */
+   DMesaVisual visual;
+   DMesaBuffer buffer;
+   GLuint ClearColor;
+   GLuint ClearIndex;
+   /* etc... */
+};
+
+
+#ifndef FX
+/****************************************************************************
+ * Read/Write pixels
+ ***************************************************************************/
+#define FLIP(y)  (dmesa->buffer->height - (y) - 1)
+#define FLIP2(y) (_b_ - (y))
+
+#define DSTRIDE dmesa->buffer->width
+
+/****************************************************************************
+ * RGB[A]
+ ***************************************************************************/
+static void
+write_rgba_span (const GLcontext *ctx, GLuint n, GLint x, GLint y,
+                 const GLubyte rgba[][4], const GLubyte mask[])
+{
+   const DMesaContext dmesa = (DMesaContext)ctx;
+   GLuint i, offset;
+
+   offset = DSTRIDE * FLIP(y) + x;
+   if (mask) {
+      /* draw some pixels */
+      for (i = 0; i < n; i++, offset++) {
+         if (mask[i]) {
+            vl_putpixel(offset, vl_mixrgba(rgba[i]));
+         }
+      }
+   } else {
+      /* draw all pixels */
+      for (i = 0; i < n; i++, offset++) {
+         vl_putpixel(offset, vl_mixrgba(rgba[i]));
+      }
+   }
+}
+
+
+static void
+write_rgb_span (const GLcontext *ctx, GLuint n, GLint x, GLint y,
+                const GLubyte rgb[][3], const GLubyte mask[])
+{
+   const DMesaContext dmesa = (DMesaContext)ctx;
+   GLuint i, offset;
+
+   offset = DSTRIDE * FLIP(y) + x;
+   if (mask) {
+      /* draw some pixels */
+      for (i = 0; i < n; i++, offset++) {
+         if (mask[i]) {
+            vl_putpixel(offset, vl_mixrgb(rgb[i]));
+         }
+      }
+   } else {
+      /* draw all pixels */
+      for (i = 0; i < n; i++, offset++) {
+         vl_putpixel(offset, vl_mixrgb(rgb[i]));
+      }
+   }
+}
+
+
+static void
+write_mono_rgba_span (const GLcontext *ctx,
+                      GLuint n, GLint x, GLint y,
+                      const GLchan color[4], const GLubyte mask[])
+{
+   const DMesaContext dmesa = (DMesaContext)ctx;
+   GLuint i, offset, rgba = vl_mixrgba(color);
+
+   offset = DSTRIDE * FLIP(y) + x;
+   if (mask) {
+      /* draw some pixels */
+      for (i = 0; i < n; i++, offset++) {
+         if (mask[i]) {
+            vl_putpixel(offset, rgba);
+         }
+      }
+   } else {
+      /* draw all pixels */
+      for (i = 0; i < n; i++, offset++) {
+         vl_putpixel(offset, rgba);
+      }
+   }
+}
+
+
+static void
+read_rgba_span (const GLcontext *ctx, GLuint n, GLint x, GLint y,
+                GLubyte rgba[][4])
+{
+   const DMesaContext dmesa = (DMesaContext)ctx;
+   GLuint i, offset;
+
+   offset = DSTRIDE * FLIP(y) + x;
+   /* read all pixels */
+   for (i = 0; i < n; i++, offset++) {
+      vl_getrgba(offset, rgba[i]);
+   }
+}
+
+
+static void
+write_rgba_pixels (const GLcontext *ctx,
+                   GLuint n, const GLint x[], const GLint y[],
+                   const GLubyte rgba[][4], const GLubyte mask[])
+{
+   const DMesaContext dmesa = (DMesaContext)ctx;
+   GLuint i, _w_ = DSTRIDE, _b_ = dmesa->buffer->height - 1;
+
+   if (mask) {
+      /* draw some pixels */
+      for (i = 0; i < n; i++) {
+         if (mask[i]) {
+            vl_putpixel(FLIP2(y[i])*_w_ + x[i], vl_mixrgba(rgba[i]));
+         }
+      }
+   } else {
+      /* draw all pixels */
+      for (i = 0; i < n; i++) {
+         vl_putpixel(FLIP2(y[i])*_w_ + x[i], vl_mixrgba(rgba[i]));
+      }
+   }
+}
+
+
+static void
+write_mono_rgba_pixels (const GLcontext *ctx,
+                        GLuint n, const GLint x[], const GLint y[],
+                                    const GLchan color[4], const GLubyte mask[])
+{
+   const DMesaContext dmesa = (DMesaContext)ctx;
+   GLuint i, _w_ = DSTRIDE, _b_ = dmesa->buffer->height - 1, rgba = vl_mixrgba(color);
+
+   if (mask) {
+      /* draw some pixels */
+      for (i = 0; i < n; i++) {
+         if (mask[i]) {
+            vl_putpixel(FLIP2(y[i])*_w_ + x[i], rgba);
+         }
+      }
+   } else {
+      /* draw all pixels */
+      for (i = 0; i < n; i++) {
+         vl_putpixel(FLIP2(y[i])*_w_ + x[i], rgba);
+      }
+   }
+}
+
+
+static void
+read_rgba_pixels (const GLcontext *ctx,
+                  GLuint n, const GLint x[], const GLint y[],
+                  GLubyte rgba[][4], const GLubyte mask[])
+{
+   const DMesaContext dmesa = (DMesaContext)ctx;
+   GLuint i, _w_ = DSTRIDE, _b_ = dmesa->buffer->height - 1;
+
+   if (mask) {
+      /* read some pixels */
+      for (i = 0; i < n; i++) {
+         if (mask[i]) {
+            vl_getrgba(FLIP2(y[i])*_w_ + x[i], rgba[i]);
+         }
+      }
+   } else {
+      /* read all pixels */
+      for (i = 0; i < n; i++) {
+         vl_getrgba(FLIP2(y[i])*_w_ + x[i], rgba[i]);
+      }
+   }
+}
+
+
+/****************************************************************************
+ * Index
+ ***************************************************************************/
+static void
+write_index_span (const GLcontext *ctx, GLuint n, GLint x, GLint y,
+                  const GLuint index[], const GLubyte mask[])
+{
+   const DMesaContext dmesa = (DMesaContext)ctx;
+   GLuint i, offset;
+
+   offset = DSTRIDE * FLIP(y) + x;
+   if (mask) {
+      /* draw some pixels */
+      for (i = 0; i < n; i++, offset++) {
+         if (mask[i]) {
+            vl_putpixel(offset, index[i]);
+         }
+      }
+   } else {
+      /* draw all pixels */
+      for (i = 0; i < n; i++, offset++) {
+         vl_putpixel(offset, index[i]);
+      }
+   }
+}
+
+
+static void
+write_index8_span (const GLcontext *ctx, GLuint n, GLint x, GLint y,
+                   const GLubyte index[], const GLubyte mask[])
+{
+   const DMesaContext dmesa = (DMesaContext)ctx;
+   GLuint i, offset;
+
+   offset = DSTRIDE * FLIP(y) + x;
+   if (mask) {
+      /* draw some pixels */
+      for (i = 0; i < n; i++, offset++) {
+         if (mask[i]) {
+            vl_putpixel(offset, index[i]);
+         }
+      }
+   } else {
+      /* draw all pixels */
+      for (i = 0; i < n; i++, offset++) {
+         vl_putpixel(offset, index[i]);
+      }
+   }
+}
+
+
+static void
+write_mono_index_span (const GLcontext *ctx,
+                       GLuint n, GLint x, GLint y,
+                       GLuint colorIndex, const GLubyte mask[])
+{
+   const DMesaContext dmesa = (DMesaContext)ctx;
+   GLuint i, offset;
+
+   offset = DSTRIDE * FLIP(y) + x;
+   if (mask) {
+      /* draw some pixels */
+      for (i = 0; i < n; i++, offset++) {
+         if (mask[i]) {
+            vl_putpixel(offset, colorIndex);
+         }
+      }
+   } else {
+      /* draw all pixels */
+      for (i = 0; i < n; i++, offset++) {
+         vl_putpixel(offset, colorIndex);
+      }
+   }
+}
+
+
+static void
+read_index_span (const GLcontext *ctx, GLuint n, GLint x, GLint y,
+                 GLuint index[])
+{
+   const DMesaContext dmesa = (DMesaContext)ctx;
+   GLuint i, offset;
+
+   offset = DSTRIDE * FLIP(y) + x;
+   /* read all pixels */
+   for (i = 0; i < n; i++, offset++) {
+      index[i] = vl_getpixel(offset);
+   }
+}
+
+
+static void
+write_index_pixels (const GLcontext *ctx,
+                    GLuint n, const GLint x[], const GLint y[],
+                    const GLuint index[], const GLubyte mask[])
+{
+   const DMesaContext dmesa = (DMesaContext)ctx;
+   GLuint i, _w_ = DSTRIDE, _b_ = dmesa->buffer->height - 1;
+
+   if (mask) {
+      /* draw some pixels */
+      for (i = 0; i < n; i++) {
+         if (mask[i]) {
+            vl_putpixel(FLIP2(y[i])*_w_ + x[i], index[i]);
+         }
+      }
+   } else {
+      /* draw all pixels */
+      for (i = 0; i < n; i++) {
+         vl_putpixel(FLIP2(y[i])*_w_ + x[i], index[i]);
+      }
+   }
+}
+
+
+static void
+write_mono_index_pixels (const GLcontext *ctx,
+                         GLuint n, const GLint x[], const GLint y[],
+                         GLuint colorIndex, const GLubyte mask[])
+{
+   const DMesaContext dmesa = (DMesaContext)ctx;
+   GLuint i, _w_ = DSTRIDE, _b_ = dmesa->buffer->height - 1;
+
+   if (mask) {
+      /* draw some pixels */
+      for (i = 0; i < n; i++) {
+         if (mask[i]) {
+            vl_putpixel(FLIP2(y[i])*_w_ + x[i], colorIndex);
+         }
+      }
+   } else {
+      /* draw all pixels */
+      for (i = 0; i < n; i++) {
+         vl_putpixel(FLIP2(y[i])*_w_ + x[i], colorIndex);
+      }
+   }
+}
+
+
+static void
+read_index_pixels (const GLcontext *ctx,
+                   GLuint n, const GLint x[], const GLint y[],
+                               GLuint index[], const GLubyte mask[])
+{
+   const DMesaContext dmesa = (DMesaContext)ctx;
+   GLuint i, _w_ = DSTRIDE, _b_ = dmesa->buffer->height - 1;
+
+   if (mask) {
+      /* read some pixels */
+      for (i = 0; i < n; i++) {
+         if (mask[i]) {
+            index[i] = vl_getpixel(FLIP2(y[i])*_w_ + x[i]);
+         }
+      }
+   } else {
+      /* read all pixels */
+      for (i = 0; i < n; i++) {
+         index[i] = vl_getpixel(FLIP2(y[i])*_w_ + x[i]);
+      }
+   }
+}
+
+
+/****************************************************************************
+ * Z-buffer
+ ***************************************************************************/
+
+
+/****************************************************************************
+ * Optimized triangle rendering
+ ***************************************************************************/
+
+/*
+ * NON-depth-buffered flat triangle.
+ */
+#define NAME tri_rgb_flat
+
+#define SETUP_CODE \
+   const DMesaContext dmesa = (DMesaContext)ctx;\
+   GLuint _b_ = dmesa->buffer->height - 1;     \
+   GLuint _w_ = dmesa->buffer->width;          \
+   GLuint rgb = vl_mixrgb(v2->color);
+
+#define RENDER_SPAN(span) \
+   GLuint i, offset = FLIP2(span.y)*_w_ + span.x;\
+   for (i = 0; i < span.end; i++, offset++) {  \
+      vl_putpixel(offset, rgb);                        \
+   }
+
+#include "swrast/s_tritemp.h"
+
+
+/*
+ * Z-less flat triangle.
+ */
+#define NAME tri_rgb_flat_zless
+
+#define INTERP_Z 1
+#define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
+
+#define SETUP_CODE \
+   const DMesaContext dmesa = (DMesaContext)ctx; \
+   GLuint _b_ = dmesa->buffer->height - 1;     \
+   GLuint _w_ = dmesa->buffer->width;          \
+   GLuint rgb = vl_mixrgb(v2->color);
+
+#define RENDER_SPAN(span) \
+   GLuint i, offset = FLIP2(span.y)*_w_ + span.x;\
+   for (i = 0; i < span.end; i++, offset++) {  \
+      const DEPTH_TYPE z = FixedToDepth(span.z);\
+      if (z < zRow[i]) {                       \
+         vl_putpixel(offset, rgb);             \
+         zRow[i] = z;                          \
+      }                                                \
+      span.z += span.zStep;                    \
+   }
+
+#include "swrast/s_tritemp.h"
+
+
+/*
+ * NON-depth-buffered iterated triangle.
+ */
+#define NAME tri_rgb_iter
+
+#define INTERP_RGB 1
+
+#define SETUP_CODE \
+   const DMesaContext dmesa = (DMesaContext)ctx;\
+   GLuint _b_ = dmesa->buffer->height - 1;     \
+   GLuint _w_ = dmesa->buffer->width;
+
+#define RENDER_SPAN(span) \
+   GLuint i, offset = FLIP2(span.y)*_w_ + span.x;                      \
+   for (i = 0; i < span.end; i++, offset++) {                          \
+      vl_putpixel(offset, vl_mixfix(span.red, span.green, span.blue)); \
+      span.red += span.redStep;                                                \
+      span.green += span.greenStep;                                    \
+      span.blue += span.blueStep;                                      \
+   }
+
+#include "swrast/s_tritemp.h"
+
+
+/*
+ * Z-less iterated triangle.
+ */
+#define NAME tri_rgb_iter_zless
+
+#define INTERP_Z 1
+#define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
+#define INTERP_RGB 1
+
+#define SETUP_CODE \
+   const DMesaContext dmesa = (DMesaContext)ctx;\
+   GLuint _b_ = dmesa->buffer->height - 1;     \
+   GLuint _w_ = dmesa->buffer->width;
+
+#define RENDER_SPAN(span) \
+   GLuint i, offset = FLIP2(span.y)*_w_ + span.x;                      \
+   for (i = 0; i < span.end; i++, offset++) {                          \
+      const DEPTH_TYPE z = FixedToDepth(span.z);                       \
+      if (z < zRow[i]) {                                               \
+         vl_putpixel(offset, vl_mixfix(span.red, span.green, span.blue));\
+         zRow[i] = z;                                                  \
+      }                                                                        \
+      span.red += span.redStep;                                                \
+      span.green += span.greenStep;                                    \
+      span.blue += span.blueStep;                                      \
+      span.z += span.zStep;                                            \
+   }
+
+#include "swrast/s_tritemp.h"
+
+
+/*
+ * Analyze context state to see if we can provide a fast triangle function
+ * Otherwise, return NULL.
+ */
+static swrast_tri_func
+dmesa_choose_tri_function (GLcontext *ctx)
+{
+   const SWcontext *swrast = SWRAST_CONTEXT(ctx);
+
+   if ((ctx->RenderMode != GL_RENDER)
+       || (ctx->Polygon.SmoothFlag)
+       || (ctx->Polygon.StippleFlag)
+       || (ctx->Texture._EnabledUnits)
+       || (swrast->_RasterMask & MULTI_DRAW_BIT)
+       || (ctx->Polygon.CullFlag && ctx->Polygon.CullFaceMode == GL_FRONT_AND_BACK)) {
+      return (swrast_tri_func)NULL;
+   }
+
+   if (swrast->_RasterMask==DEPTH_BIT
+       && ctx->Depth.Func==GL_LESS
+       && ctx->Depth.Mask==GL_TRUE
+       && ctx->Visual.depthBits == DEFAULT_SOFTWARE_DEPTH_BITS) {
+      return (ctx->Light.ShadeModel==GL_SMOOTH) ? tri_rgb_iter_zless : tri_rgb_flat_zless;
+   }
+
+   if (swrast->_RasterMask==0) { /* no depth test */
+      return (ctx->Light.ShadeModel==GL_SMOOTH) ? tri_rgb_iter : tri_rgb_flat;
+   }
+
+   return (swrast_tri_func)NULL;
+}
+
+
+/* Override for the swrast triangle-selection function.  Try to use one
+ * of our internal triangle functions, otherwise fall back to the
+ * standard swrast functions.
+ */
+static void
+dmesa_choose_tri (GLcontext *ctx)
+{
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
+
+   if (!(swrast->Triangle=dmesa_choose_tri_function(ctx))) {
+      _swrast_choose_triangle(ctx);
+   }
+}
+
+
+/****************************************************************************
+ * Optimized line rendering
+ ***************************************************************************/
+
+/*
+ * NON-depth-buffered flat line.
+ */
+#define NAME line_rgb_flat
+
+#define INTERP_XY 1
+#define CLIP_HACK 1
+
+#define SETUP_CODE \
+   const DMesaContext dmesa = (DMesaContext)ctx;\
+   GLuint _b_ = dmesa->buffer->height - 1;     \
+   GLuint _w_ = dmesa->buffer->width;          \
+   GLuint rgb = vl_mixrgb(vert1->color);
+
+#define PLOT(X,Y) vl_putpixel(FLIP2(Y) * _w_ + X, rgb);
+
+#include "swrast/s_linetemp.h"
+
+
+/*
+ * Z-less flat line.
+ */
+#define NAME line_rgb_flat_zless
+
+#define INTERP_XY 1
+#define INTERP_Z 1
+#define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
+#define CLIP_HACK 1
+
+#define SETUP_CODE \
+   const DMesaContext dmesa = (DMesaContext)ctx;\
+   GLuint _b_ = dmesa->buffer->height - 1;     \
+   GLuint _w_ = dmesa->buffer->width;          \
+   GLuint rgb = vl_mixrgb(vert1->color);
+
+#define PLOT(X,Y) \
+   if (Z < *zPtr) {                            \
+      *zPtr = Z;                               \
+      vl_putpixel(FLIP2(Y) * _w_ + X, rgb);    \
+   }
+
+#include "swrast/s_linetemp.h"
+
+
+/*
+ * NON-depth-buffered iterated line.
+ */
+#define line_rgb_iter NULL
+
+
+/*
+ * Z-less iterated line.
+ */
+#define line_rgb_iter_zless NULL
+
+
+/*
+ * Analyze context state to see if we can provide a fast line function
+ * Otherwise, return NULL.
+ */
+static swrast_line_func
+dmesa_choose_line_function (GLcontext *ctx)
+{
+   const SWcontext *swrast = SWRAST_CONTEXT(ctx);
+
+   if ((ctx->RenderMode != GL_RENDER)
+       || (ctx->Line.SmoothFlag)
+       || (ctx->Texture._EnabledUnits)
+       || (ctx->Line.StippleFlag)
+       || (swrast->_RasterMask & MULTI_DRAW_BIT)
+       || (ctx->Line.Width!=1.0F)) {
+      return (swrast_line_func)NULL;
+   }
+
+   if (swrast->_RasterMask==DEPTH_BIT
+       && ctx->Depth.Func==GL_LESS
+       && ctx->Depth.Mask==GL_TRUE
+       && ctx->Visual.depthBits == DEFAULT_SOFTWARE_DEPTH_BITS) {
+      return (ctx->Light.ShadeModel==GL_SMOOTH) ? line_rgb_iter_zless : line_rgb_flat_zless;
+   }
+
+   if (swrast->_RasterMask==0) { /* no depth test */
+      return (ctx->Light.ShadeModel==GL_SMOOTH) ? line_rgb_iter : line_rgb_flat;
+   }
+
+   return (swrast_line_func)NULL;
+}
+
+
+/* Override for the swrast line-selection function.  Try to use one
+ * of our internal line functions, otherwise fall back to the
+ * standard swrast functions.
+ */
+static void
+dmesa_choose_line (GLcontext *ctx)
+{
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
+
+   if (!(swrast->Line=dmesa_choose_line_function(ctx))) {
+      _swrast_choose_line(ctx);
+   }
+}
+
+
+/****************************************************************************
+ * Miscellaneous device driver funcs
+ ***************************************************************************/
+static const struct gl_texture_format *
+choose_tex_format (GLcontext *ctx, GLint internalFormat,
+                   GLenum format, GLenum type)
+{
+   switch (internalFormat) {
+      case GL_COMPRESSED_RGB_ARB:
+         return &_mesa_texformat_rgb;
+      case GL_COMPRESSED_RGBA_ARB:
+         return &_mesa_texformat_rgba;
+      default:
+         return _mesa_choose_tex_format(ctx, internalFormat, format, type);
+   }
+}
+
+
+static void
+clear_index (GLcontext *ctx, GLuint index)
+{
+   ((DMesaContext)ctx)->ClearIndex = index;
+}
+
+
+static void
+clear_color (GLcontext *ctx, const GLfloat color[4])
+{
+   GLubyte col[4];
+   CLAMPED_FLOAT_TO_UBYTE(col[0], color[0]);
+   CLAMPED_FLOAT_TO_UBYTE(col[1], color[1]);
+   CLAMPED_FLOAT_TO_UBYTE(col[2], color[2]);
+   CLAMPED_FLOAT_TO_UBYTE(col[3], color[3]);
+   ((DMesaContext)ctx)->ClearColor = vl_mixrgba(col);
+}
+
+
+static void
+clear (GLcontext *ctx, GLbitfield mask, GLboolean all,
+       GLint x, GLint y, GLint width, GLint height)
+{
+   const DMesaContext c = (DMesaContext)ctx;
+   const GLuint *colorMask = (GLuint *)&ctx->Color.ColorMask;
+
+   /*
+    * Clear the specified region of the buffers indicated by 'mask'
+    * using the clear color or index as specified by one of the two
+    * functions above.
+    * If all==GL_TRUE, clear whole buffer, else just clear region defined
+    * by x,y,width,height
+    */
+
+   /* we can't handle color or index masking */
+   if ((*colorMask == 0xffffffff) && (ctx->Color.IndexMask == 0xffffffff)) {
+      if (mask & DD_BACK_LEFT_BIT) {
+         int color = ((GLvisual *)(c->visual))->rgbMode ? c->ClearColor : c->ClearIndex;
+
+         if (all) {
+            vl_clear(color);
+         } else {
+            vl_rect(x, c->buffer->height - y - height, width, height, color);
+         }
+
+         mask &= ~DD_BACK_LEFT_BIT;
+      }
+   }
+
+   if (mask) {
+      _swrast_Clear(ctx, mask, all, x, y, width, height);
+   }
+}
+
+
+/*
+ * This function is called to specify which buffer to read and write
+ * for software rasterization (swrast) fallbacks.  This doesn't necessarily
+ * correspond to glDrawBuffer() or glReadBuffer() calls.
+ */
+static void
+set_buffer (GLcontext *ctx, GLframebuffer *colorBuffer, GLuint bufferBit)
+{
+   /*
+    * XXX todo - examine bufferBit and set read/write pointers
+    */
+   /* Normally, we would have
+    *    ctx->Driver.ReadBuffer == set_read_buffer
+    *    ctx->Driver.DrawBuffer == set_draw_buffer
+    * and make sure set_draw_buffer calls _swrast_DrawBuffer,
+    * which in turn will call this routine via dd->SetBuffer.
+    */
+}
+
+
+/*
+ * Return the width and height of the current buffer.
+ * If anything special has to been done when the buffer/window is
+ * resized, do it now.
+ */
+static void
+get_buffer_size (GLframebuffer *buffer, GLuint *width, GLuint *height)
+{
+   DMesaBuffer b = (DMesaBuffer)buffer;
+
+   *width  = b->width;
+   *height = b->height;
+}
+
+
+static void
+viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
+{
+   /* poll for window size change and realloc software Z/stencil/etc if needed */
+   _mesa_ResizeBuffersMESA();
+}
+
+
+static const GLubyte *
+get_string (GLcontext *ctx, GLenum name)
+{
+   switch (name) {
+      case GL_RENDERER:
+         return (const GLubyte *)"Mesa DJGPP";
+      default:
+         return NULL;
+   }
+}
+
+
+static void
+finish (GLcontext *ctx)
+{
+   /*
+    * XXX todo - OPTIONAL FUNCTION: implements glFinish if possible
+    */
+}
+
+
+static void
+flush (GLcontext *ctx)
+{
+   /*
+    * XXX todo - OPTIONAL FUNCTION: implements glFlush if possible
+    */
+}
+
+
+/****************************************************************************
+ * State
+ ***************************************************************************/
+#define DMESA_NEW_LINE (_NEW_LINE | \
+                        _NEW_TEXTURE | \
+                        _NEW_LIGHT | \
+                        _NEW_DEPTH | \
+                        _NEW_RENDERMODE | \
+                        _SWRAST_NEW_RASTERMASK)
+
+#define DMESA_NEW_TRIANGLE (_NEW_POLYGON | \
+                            _NEW_TEXTURE | \
+                            _NEW_LIGHT | \
+                            _NEW_DEPTH | \
+                            _NEW_RENDERMODE | \
+                            _SWRAST_NEW_RASTERMASK)
+
+/* Extend the software rasterizer with our line and triangle
+ * functions.
+ */
+static void
+dmesa_register_swrast_functions (GLcontext *ctx)
+{
+   SWcontext *swrast = SWRAST_CONTEXT(ctx);
+
+   swrast->choose_line = dmesa_choose_line;
+   swrast->choose_triangle = dmesa_choose_tri;
+
+   swrast->invalidate_line |= DMESA_NEW_LINE;
+   swrast->invalidate_triangle |= DMESA_NEW_TRIANGLE;
+}
+
+
+static void
+dmesa_update_state (GLcontext *ctx, GLuint new_state)
+{
+   /* Propagate statechange information to swrast and swrast_setup
+    * modules. The DMesa driver has no internal GL-dependent state.
+    */
+   _swrast_InvalidateState( ctx, new_state );
+   _ac_InvalidateState( ctx, new_state );
+   _tnl_InvalidateState( ctx, new_state );
+   _swsetup_InvalidateState( ctx, new_state );
+}
+
+
+/* Initialize the device driver function table with the functions
+ * we implement in this driver.
+ */
+static void
+dmesa_init_driver_functions (DMesaVisual visual,
+                             struct dd_function_table *driver)
+{
+   driver->UpdateState = dmesa_update_state;
+   driver->GetString = get_string;
+   driver->GetBufferSize = get_buffer_size;
+   driver->Viewport = viewport;
+   driver->Flush = flush;
+   driver->Finish = finish;
+   driver->Clear = clear;
+   driver->ClearColor = clear_color;
+   driver->ClearIndex = clear_index;
+#if SWTC
+   driver->ChooseTextureFormat = choose_tex_format;
+#endif
+}
+
+
+/* Setup pointers and other driver state that is constant for the life
+ * of a context.
+ */
+static void
+dmesa_init_pointers (GLcontext *ctx)
+{
+   struct swrast_device_driver *dd = _swrast_GetDeviceDriverReference(ctx);
+
+   dd->SetBuffer = set_buffer;
+
+   /* The span functions should be in `dmesa_update_state', but I'm
+    * pretty sure they will never change during the life of the Visual
+    */
+
+   /* Index span/pixel functions */
+   dd->WriteCI32Span = write_index_span;
+   dd->WriteCI8Span = write_index8_span;
+   dd->WriteMonoCISpan = write_mono_index_span;
+   dd->WriteCI32Pixels = write_index_pixels;
+   dd->WriteMonoCIPixels = write_mono_index_pixels;
+   dd->ReadCI32Span = read_index_span;
+   dd->ReadCI32Pixels = read_index_pixels;
+
+   /* RGB(A) span/pixel functions */
+   dd->WriteRGBASpan = write_rgba_span;
+   dd->WriteRGBSpan = write_rgb_span;
+   dd->WriteMonoRGBASpan = write_mono_rgba_span;
+   dd->WriteRGBAPixels = write_rgba_pixels;
+   dd->WriteMonoRGBAPixels = write_mono_rgba_pixels;
+   dd->ReadRGBASpan = read_rgba_span;
+   dd->ReadRGBAPixels = read_rgba_pixels;
+}
+#endif /* FX */
+
+
+/****************************************************************************
+ * DMesa Public API Functions
+ ***************************************************************************/
+
+/*
+ * The exact arguments to this function will depend on your window system
+ */
+DMesaVisual
+DMesaCreateVisual (GLint width,
+                   GLint height,
+                   GLint colDepth,
+                   GLint refresh,
+                   GLboolean dbFlag,
+                   GLboolean rgbFlag,
+                   GLint alphaSize,
+                   GLint depthSize,
+                   GLint stencilSize,
+                   GLint accumSize)
+{
+   DMesaVisual v;
+   GLint redBits, greenBits, blueBits, alphaBits, indexBits;
+   GLboolean sw_alpha;
+
+   alphaBits = 0;
+
+   if (!rgbFlag) {
+      indexBits = 8;
+      redBits = 0;
+      greenBits = 0;
+      blueBits = 0;
+   } else {
+      indexBits = 0;
+      switch (colDepth) {
+         case 8:
+            redBits = 8;
+            greenBits = 8;
+            blueBits = 8;
+            break;
+         case 15:
+            alphaBits = 1;
+            redBits = 5;
+            greenBits = 5;
+            blueBits = 5;
+            break;
+         case 16:
+            redBits = 5;
+            greenBits = 6;
+            blueBits = 5;
+            break;
+         case 32:
+            alphaBits = 8;
+         case 24:
+            redBits = 8;
+            greenBits = 8;
+            blueBits = 8;
+            break;
+         default:
+            return NULL;
+      }
+   }
+
+   /* Okay,
+    * `alphaBits' is what we can provide
+    * `alphaSize' is what app requests
+    *
+    * Note that alpha buffering is required only if destination alpha is used
+    * in alpha blending; alpha blending modes that do not use destination alpha
+    * can be used w/o alpha buffer.
+    *
+    * We will use whatever ALPHA app requests. Later, in `CreateBuffer' we'll
+    * instruct Mesa to use its own ALPHA buffer, by passing a non-FALSE value
+    * for ALPHA to `_mesa_initialize_framebuffer'.
+    *
+    * Basically, 32bit modes provide ALPHA storage, but can we rely on this?
+    */
+   alphaBits = alphaSize;
+   sw_alpha = (alphaBits > 0);
+
+#ifndef FX
+   if (!dbFlag) {
+      return NULL;
+   }
+   if ((colDepth=vl_video_init(width, height, colDepth, rgbFlag, refresh)) <= 0) {
+      return NULL;
+   }
+#else  /* FX */
+   if (!rgbFlag) {
+      return NULL;
+   } else {
+      char *env;
+
+      if ((env = getenv("MESA_FX_INFO")) && (env[0] == 'r')) {
+         freopen("MESA.LOG", "w", stderr);
+      }
+
+      if (refresh && (((env = getenv("FX_GLIDE_REFRESH")) == NULL) || !atoi(env))) {
+         /* if we are passed non-zero value for refresh, we need to override
+          * default refresh rate. However, if FX_GLIDE_REFRESH is already set
+          * to 0, we won't override it, because it has a special meaning for
+          * DJGPP Glide3x (switch via VESA, using BIOS default refresh).
+          */
+         char tmp[32];
+         sprintf(tmp, "FX_GLIDE_REFRESH=%u", refresh);
+         putenv(tmp);
+      }
+   }
+#endif /* FX */
+
+   if ((v=(DMesaVisual)CALLOC_STRUCT(dmesa_visual)) != NULL) {
+      /* Create core visual */
+      _mesa_initialize_visual((GLvisual *)v,
+                              rgbFlag,         /* rgb */
+                              dbFlag,
+                              GL_FALSE,                /* stereo */
+                              redBits,
+                              greenBits,
+                              blueBits,
+                              alphaBits,
+                              indexBits,               /* indexBits */
+                              depthSize,
+                              stencilSize,
+                              accumSize,               /* accumRed */
+                              accumSize,               /* accumGreen */
+                              accumSize,               /* accumBlue */
+                              alphaBits?accumSize:0,   /* accumAlpha */
+                              1);                      /* numSamples */
+
+#ifndef FX
+      v->sw_alpha = sw_alpha;
+      v->z_buffer = (depthSize > 0) ? 1 : 0;
+#endif
+   }
+
+   return v;
+}
+
+
+void
+DMesaDestroyVisual (DMesaVisual v)
+{
+#ifndef FX
+   vl_video_exit();
+#endif
+   _mesa_destroy_visual((GLvisual *)v);
+}
+
+
+DMesaBuffer
+DMesaCreateBuffer (DMesaVisual visual,
+                   GLint xpos, GLint ypos,
+                   GLint width, GLint height)
+{
+#ifndef FX
+   DMesaBuffer b;
+
+   if ((b=(DMesaBuffer)CALLOC_STRUCT(dmesa_buffer)) != NULL) {
+      _mesa_initialize_framebuffer((GLframebuffer *)b,
+                                   (GLvisual *)visual,
+                                   visual->z_buffer == 1,
+                                   ((GLvisual *)visual)->stencilBits > 0,
+                                   ((GLvisual *)visual)->accumRedBits > 0,
+                                   visual->sw_alpha);
+      b->xpos = xpos;
+      b->ypos = ypos;
+      b->width = width;
+      b->height = height;
+   }
+
+   return b;
+#else  /* FX */
+
+   GLvisual *v = (GLvisual *)visual;
+   int i = 0, fx_attrib[32];
+
+   if (v->doubleBufferMode) fx_attrib[i++] = FXMESA_DOUBLEBUFFER;
+   if (v->depthBits > 0) { fx_attrib[i++] = FXMESA_DEPTH_SIZE; fx_attrib[i++] = v->depthBits; }
+   if (v->stencilBits > 0) { fx_attrib[i++] = FXMESA_STENCIL_SIZE; fx_attrib[i++] = v->stencilBits; }
+   if (v->accumRedBits > 0) { fx_attrib[i++] = FXMESA_ACCUM_SIZE; fx_attrib[i++] = v->accumRedBits; }
+   if (v->alphaBits) { fx_attrib[i++] = FXMESA_ALPHA_SIZE; fx_attrib[i++] = v->alphaBits; }
+   fx_attrib[i++] = FXMESA_COLORDEPTH;
+   fx_attrib[i++] = v->redBits + v->greenBits + v->blueBits;
+   fx_attrib[i] = FXMESA_NONE;
+
+   return (DMesaBuffer)fxMesaCreateBestContext(-1, width, height, fx_attrib);
+#endif /* FX */
+}
+
+
+void
+DMesaDestroyBuffer (DMesaBuffer b)
+{
+#ifndef FX
+   if (b->the_window != NULL) {
+      free(b->the_window);
+   }
+   _mesa_destroy_framebuffer((GLframebuffer *)b);
+#else
+   fxMesaDestroyContext((fxMesaContext)b);
+#endif
+}
+
+
+DMesaContext
+DMesaCreateContext (DMesaVisual visual, DMesaContext share)
+{
+   GLcontext *c;
+#ifndef FX
+   TNLcontext *tnl;
+   struct dd_function_table functions;
+
+   if ((c=(GLcontext *)CALLOC_STRUCT(dmesa_context)) != NULL) {
+      /* Initialize device driver function table */
+      _mesa_init_driver_functions(&functions);
+      /* override with our functions */
+      dmesa_init_driver_functions(visual, &functions);
+
+      _mesa_initialize_context(c,
+                               (GLvisual *)visual,
+                               (GLcontext *)share,
+                               &functions,
+                               (void *)c);
+      _mesa_enable_sw_extensions(c);
+      _mesa_enable_1_3_extensions(c);
+      _mesa_enable_1_4_extensions(c);
+      _mesa_enable_1_5_extensions(c);
+      _mesa_enable_2_0_extensions(c);
+#if SWTC
+      if (c->Mesa_DXTn) {
+         _mesa_enable_extension(c, "GL_EXT_texture_compression_s3tc");
+         _mesa_enable_extension(c, "GL_S3_s3tc");
+      }
+      _mesa_enable_extension(c, "GL_3DFX_texture_compression_FXT1");
+#endif
+
+      /* you probably have to do a bunch of other initializations here. */
+      ((DMesaContext)c)->visual = visual;
+
+      /* Initialize the software rasterizer and helper modules.
+       */
+      _swrast_CreateContext(c);
+      _ac_CreateContext(c);
+      _tnl_CreateContext(c);
+      _swsetup_CreateContext(c);
+      /* tnl setup */
+      tnl = TNL_CONTEXT(c);
+      tnl->Driver.RunPipeline = _tnl_run_pipeline;
+      /* swrast setup */
+      if (((GLvisual *)visual)->rgbMode) dmesa_register_swrast_functions(c);
+      dmesa_init_pointers(c);
+      _swsetup_Wakeup(c);
+   }
+
+#else  /* FX */
+   c = (GLcontext *)0xdeadbeef;
+#endif /* FX */
+
+   return (DMesaContext)c;
+}
+
+
+void
+DMesaDestroyContext (DMesaContext c)
+{
+#ifndef FX
+   if (c) {
+      _swsetup_DestroyContext((GLcontext *)c);
+      _swrast_DestroyContext((GLcontext *)c);
+      _tnl_DestroyContext((GLcontext *)c);
+      _ac_DestroyContext((GLcontext *)c);
+      _mesa_destroy_context((GLcontext *)c);
+   }
+#endif
+}
+
+
+GLboolean
+DMesaMoveBuffer (GLint xpos, GLint ypos)
+{
+#ifndef FX
+   GET_CURRENT_CONTEXT(ctx);
+   DMesaBuffer b = ((DMesaContext)ctx)->buffer;
+
+   if (vl_sync_buffer(&b->the_window, xpos, ypos, b->width, b->height) == 0) {
+      b->xpos = xpos;
+      b->ypos = ypos;
+      return GL_TRUE;
+   }
+#endif
+
+   return GL_FALSE;
+}
+
+
+GLboolean
+DMesaResizeBuffer (GLint width, GLint height)
+{
+#ifndef FX
+   GET_CURRENT_CONTEXT(ctx);
+   DMesaBuffer b = ((DMesaContext)ctx)->buffer;
+
+   if (vl_sync_buffer(&b->the_window, b->xpos, b->ypos, width, height) == 0) {
+      b->width = width;
+      b->height = height;
+      return GL_TRUE;
+   }
+#endif
+
+   return GL_FALSE;
+}
+
+
+/*
+ * Make the specified context and buffer the current one.
+ */
+GLboolean
+DMesaMakeCurrent (DMesaContext c, DMesaBuffer b)
+{
+#ifndef FX
+   if ((c != NULL) && (b != NULL)) {
+      if (vl_sync_buffer(&b->the_window, b->xpos, b->ypos, b->width, b->height) != 0) {
+         return GL_FALSE;
+      }
+
+      c->buffer = b;
+
+      _mesa_make_current((GLcontext *)c, (GLframebuffer *)b);
+   }
+   else {
+      /* Detach */
+      _mesa_make_current(NULL, NULL);
+   }
+
+#else
+   fxMesaMakeCurrent((fxMesaContext)b);
+#endif
+
+   return GL_TRUE;
+}
+
+
+void
+DMesaSwapBuffers (DMesaBuffer b)
+{
+   /* copy/swap back buffer to front if applicable */
+#ifndef FX
+   GET_CURRENT_CONTEXT(ctx);
+   _mesa_notifySwapBuffers(ctx);
+   vl_flip();
+#else
+   fxMesaSwapBuffers();
+#endif
+}
+
+
+void
+DMesaSetCI (int ndx, GLfloat red, GLfloat green, GLfloat blue)
+{
+#ifndef FX
+   vl_setCI(ndx, red, green, blue);
+#endif
+}
+
+
+DMesaContext
+DMesaGetCurrentContext (void)
+{
+   GET_CURRENT_CONTEXT(ctx);
+
+#ifndef FX
+#else
+   if (ctx != NULL) {
+      ctx = (GLcontext *)0xdeadbeef;
+   }
+#endif
+
+   return (DMesaContext)ctx;
+}
+
+
+DMesaBuffer
+DMesaGetCurrentBuffer (void)
+{
+   const DMesaContext dmesa = DMesaGetCurrentContext();
+
+   if (dmesa == NULL) {
+      return NULL;
+   }
+
+#ifndef FX
+   return dmesa->buffer;
+#else
+   return (DMesaBuffer)fxMesaGetCurrentContext();
+#endif
+}
+
+
+DMesaProc
+DMesaGetProcAddress (const char *name)
+{
+   DMesaProc p = (DMesaProc)_glapi_get_proc_address(name);
+
+   /* TODO: handle DMesa* namespace
+   if (p == NULL) {
+   }
+   */
+
+   return p;
+}
+
+
+int
+DMesaGetIntegerv (GLenum pname, GLint *params)
+{
+   switch (pname) {
+      case DMESA_GET_SCREEN_SIZE:
+         #ifndef FX
+         vl_get(VL_GET_SCREEN_SIZE, params);
+         #else
+         fxGetScreenGeometry(&params[0], &params[1]);
+         #endif
+         break;
+      case DMESA_GET_DRIVER_CAPS:
+         #ifndef FX
+         params[0] = DMESA_DRIVER_SWDB_BIT;
+         #else
+         params[0] = DMESA_DRIVER_LLWO_BIT;
+         #endif
+         break;
+      case DMESA_GET_VIDEO_MODES:
+         #ifndef FX
+         return vl_get(VL_GET_VIDEO_MODES, params);
+         #else
+         return -1; /* TODO */
+         #endif
+      case DMESA_GET_BUFFER_ADDR: {
+         #ifndef FX
+         DMesaContext c = (DMesaContext)DMesaGetCurrentContext();
+         if (c != NULL) {
+            DMesaBuffer b = c->buffer;
+            if (b != NULL) {
+               params[0] = (GLint)b->the_window;
+            }
+         }
+         break;
+         #else
+         return -1;
+         #endif
+      }
+      default:
+         return -1;
+   }
+
+   return 0;
+}
+
+
+#if SWTC && (((__DJGPP__ << 8) | __DJGPP_MINOR__) >= 0x204)
+#include <sys/dxe.h>
+
+extern_asm(___dj_assert);
+extern_asm(_free);
+extern_asm(_malloc);
+extern_asm(_memset);
+
+DXE_EXPORT_TABLE_AUTO (___dxe_eta___dxtn)
+   DXE_EXPORT_ASM (___dj_assert)
+   DXE_EXPORT_ASM (_free)
+   DXE_EXPORT_ASM (_malloc)
+   DXE_EXPORT_ASM (_memset)
+DXE_EXPORT_END
+#endif