home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 5 / MA_Cover_5.iso / ppc / mesa / src / svgamesa.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-31  |  14.3 KB  |  557 lines

  1. /* $Id: svgamesa.c,v 1.6 1997/07/24 01:45:24 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  2.4
  6.  * Copyright (C) 1995-1997  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: svgamesa.c,v $
  26.  * Revision 1.6  1997/07/24 01:45:24  brianp
  27.  * added doubleBuffer flag to SVGAMesaCreateContext()
  28.  *
  29.  * Revision 1.5  1997/07/24 01:21:56  brianp
  30.  * changed precompiled header symbol from PCH to PC_HEADER
  31.  *
  32.  * Revision 1.4  1997/05/28 03:26:29  brianp
  33.  * added precompiled header (PCH) support
  34.  *
  35.  * Revision 1.3  1997/05/26 21:15:37  brianp
  36.  * now pass red/green/blue/alpha bits to gl_create_visual()
  37.  *
  38.  * Revision 1.2  1996/10/15 00:23:51  brianp
  39.  * cleaned up code to work with Mesa 2.0
  40.  *
  41.  * Revision 1.1  1996/09/13 01:38:16  brianp
  42.  * Initial revision
  43.  *
  44.  */
  45.  
  46.  
  47. /*
  48.  * Linux SVGA/Mesa interface.
  49.  *
  50.  * This interface is not finished!  Still have to implement pixel
  51.  * reading functions and double buffering.  Then, look into accelerated
  52.  * line and polygon rendering.  And, clean up a bunch of other stuff.
  53.  * Any volunteers?
  54.  */
  55.  
  56.  
  57. #ifdef SVGA
  58.  
  59.  
  60. #ifdef PC_HEADER
  61. #include "all.h"
  62. #else
  63. #include <stdio.h>
  64. #include <stdlib.h>
  65. #include <vga.h>
  66. #include "GL/svgamesa.h"
  67. #include "context.h"
  68. #include "matrix.h"
  69. #include "types.h"
  70. #endif
  71.  
  72.  
  73. struct svgamesa_context {
  74.    GLcontext *gl_ctx;        /* the core Mesa context */
  75.    GLvisual *gl_vis;        /* describes the color buffer */
  76.    GLframebuffer *gl_buffer;    /* the ancillary buffers */
  77.    GLuint index;        /* current color index */
  78.    GLint red, green, blue;    /* current rgb color */
  79.    GLint width, height;        /* size of color buffer */
  80.    GLint depth;            /* bits per pixel (8,16,24 or 32) */
  81. };
  82.  
  83.  
  84. static SVGAMesaContext SVGAMesa = NULL;    /* the current context */
  85.  
  86.  
  87.  
  88. /*
  89.  * Convert Mesa window Y coordinate to VGA screen Y coordinate:
  90.  */
  91. #define FLIP(Y)  (SVGAMesa->height-(Y)-1)
  92.  
  93.  
  94.  
  95. /**********************************************************************/
  96. /*****                 Miscellaneous functions                    *****/
  97. /**********************************************************************/
  98.  
  99.  
  100. static void get_buffer_size( GLcontext *ctx, GLuint *width, GLuint *height )
  101. {
  102.    *width = SVGAMesa->width = vga_getxdim();
  103.    *height = SVGAMesa->height = vga_getydim();
  104. }
  105.  
  106.  
  107. /* Set current color index */
  108. static void set_index( GLcontext *ctx, GLuint index )
  109. {
  110.    SVGAMesa->index = index;
  111.    vga_setcolor( index );
  112. }
  113.  
  114.  
  115. /* Set current drawing color */
  116. static void set_color( GLcontext *ctx,
  117.                        GLubyte red, GLubyte green,
  118.                        GLubyte blue, GLubyte alpha )
  119. {
  120.    SVGAMesa->red = red;
  121.    SVGAMesa->green = green;
  122.    SVGAMesa->blue = blue;
  123.    vga_setrgbcolor( red, green, blue );
  124. }
  125.  
  126.  
  127. static void clear_index( GLcontext *ctx, GLuint index )
  128. {
  129.    /* TODO: Implements glClearIndex() */
  130. }
  131.  
  132.  
  133. static void clear_color( GLcontext *ctx,
  134.                          GLubyte red, GLubyte green,
  135.                          GLubyte blue, GLubyte alpha )
  136. {
  137.    /* TODO: Implements glClearColor() */
  138. }
  139.  
  140.  
  141. static void clear( GLcontext *ctx,
  142.                    GLboolean all,
  143.                    GLint x, GLint y, GLint width, GLint height )
  144. {
  145.    vga_clear();
  146. }
  147.  
  148.  
  149. static GLboolean set_buffer( GLcontext *ctx,
  150.                              GLenum mode )
  151. {
  152.    /* TODO: implement double buffering and use this function to select */
  153.    /* between front and back buffers. */
  154.    return GL_TRUE;
  155. }
  156.  
  157.  
  158.  
  159.  
  160. /**********************************************************************/
  161. /*****            Write spans of pixels                           *****/
  162. /**********************************************************************/
  163.  
  164.  
  165. static void write_index_span( GLcontext *ctx,
  166.                               GLuint n, GLint x, GLint y,
  167.                               const GLuint index[],
  168.                               const GLubyte mask[] )
  169. {
  170.    int i;
  171.    y = FLIP(y);
  172.    for (i=0;i<n;i++,x++) {
  173.       if (mask[i]) {
  174.          vga_setcolor( index[i] );
  175.          vga_drawpixel( x, y );
  176.       }
  177.    }
  178. }
  179.  
  180.  
  181.  
  182. static void write_monoindex_span( GLcontext *ctx,
  183.                                   GLuint n, GLint x, GLint y,
  184.                                   const GLubyte mask[] )
  185. {
  186.    int i;
  187.    y = FLIP(y);
  188.    /* use current color index */
  189.    vga_setcolor( SVGAMesa->index );
  190.    for (i=0;i<n;i++,x++) {
  191.       if (mask[i]) {
  192.          vga_drawpixel( x, y );
  193.       }
  194.    }
  195. }
  196.  
  197.  
  198.  
  199. static void write_color_span( GLcontext *ctx,
  200.                               GLuint n, GLint x, GLint y,
  201.                               const GLubyte red[], const GLubyte green[],
  202.                               const GLubyte blue[], const GLubyte alpha[],
  203.                               const GLubyte mask[] )
  204. {
  205.    int i;
  206.    y=FLIP(y);
  207.    if (mask) {
  208.       /* draw some pixels */
  209.       for (i=0; i<n; i++, x++) {
  210.          if (mask[i]) {
  211.             vga_setrgbcolor( red[i], green[i], blue[i] );
  212.             vga_drawpixel( x, y );
  213.          }
  214.       }
  215.    }
  216.    else {
  217.       /* draw all pixels */
  218.       for (i=0; i<n; i++, x++) {
  219.          vga_setrgbcolor( red[i], green[i], blue[i] );
  220.          vga_drawpixel( x, y );
  221.       }
  222.    }
  223. }
  224.  
  225.  
  226.  
  227. static void write_monocolor_span( GLcontext *ctx,
  228.                                   GLuint n, GLint x, GLint y,
  229.                                   const GLubyte mask[])
  230. {
  231.    int i;
  232.    y=FLIP(y);
  233.    /* use current rgb color */
  234.    vga_setrgbcolor( SVGAMesa->red, SVGAMesa->green, SVGAMesa->blue );
  235.    for (i=0; i<n; i++, x++) {
  236.       if (mask[i]) {
  237.          vga_drawpixel( x, y );
  238.       }
  239.    }
  240. }
  241.  
  242.  
  243.  
  244. /**********************************************************************/
  245. /*****                 Read spans of pixels                       *****/
  246. /**********************************************************************/
  247.  
  248.  
  249. static void read_index_span( GLcontext *ctx,
  250.                              GLuint n, GLint x, GLint y, GLuint index[])
  251. {
  252.    int i;
  253.    y = FLIP(y);
  254.    for (i=0; i<n; i++,x++) {
  255.       index[i] = vga_getpixel( x, y );
  256.    }
  257. }
  258.  
  259.  
  260.  
  261. static void read_color_span( GLcontext *ctx,
  262.                              GLuint n, GLint x, GLint y,
  263.                              GLubyte red[], GLubyte green[],
  264.                              GLubyte blue[], GLubyte alpha[] )
  265. {
  266.    int i;
  267.    for (i=0; i<n; i++, x++) {
  268.       /* TODO */
  269.    }
  270. }
  271.  
  272.  
  273.  
  274. /**********************************************************************/
  275. /*****                  Write arrays of pixels                    *****/
  276. /**********************************************************************/
  277.  
  278.  
  279. static void write_index_pixels( GLcontext *ctx,
  280.                                 GLuint n, const GLint x[], const GLint y[],
  281.                                 const GLuint index[], const GLubyte mask[] )
  282. {
  283.    int i;
  284.    for (i=0; i<n; i++) {
  285.       if (mask[i]) {
  286.          vga_setcolor( index[i] );
  287.          vga_drawpixel( x[i], FLIP(y[i]) );
  288.       }
  289.    }
  290. }
  291.  
  292.  
  293.  
  294. static void write_monoindex_pixels( GLcontext *ctx,
  295.                                     GLuint n,
  296.                                     const GLint x[], const GLint y[],
  297.                                     const GLubyte mask[] )
  298. {
  299.    int i;
  300.    /* use current color index */
  301.    vga_setcolor( SVGAMesa->index );
  302.    for (i=0; i<n; i++) {
  303.       if (mask[i]) {
  304.          vga_drawpixel( x[i], FLIP(y[i]) );
  305.       }
  306.    }
  307. }
  308.  
  309.  
  310.  
  311. static void write_color_pixels( GLcontext *ctx,
  312.                                 GLuint n, const GLint x[], const GLint y[],
  313.                                 const GLubyte r[], const GLubyte g[],
  314.                                 const GLubyte b[], const GLubyte a[],
  315.                                 const GLubyte mask[] )
  316. {
  317.    int i;
  318.    for (i=0; i<n; i++) {
  319.       if (mask[i]) {
  320.          vga_setrgbcolor( r[i], g[i], b[i] );
  321.          vga_drawpixel( x[i], FLIP(y[i]) );
  322.       }
  323.    }
  324. }
  325.  
  326.  
  327.  
  328. static void write_monocolor_pixels( GLcontext *ctx,
  329.                                     GLuint n,
  330.                                     const GLint x[], const GLint y[],
  331.                                     const GLubyte mask[] )
  332. {
  333.    int i;
  334.    /* use current rgb color */
  335.    vga_setrgbcolor( SVGAMesa->red, SVGAMesa->green, SVGAMesa->blue );
  336.    for (i=0; i<n; i++) {
  337.       if (mask[i]) {
  338.          vga_drawpixel( x[i], FLIP(y[i]) );
  339.       }
  340.    }
  341. }
  342.  
  343.  
  344.  
  345.  
  346. /**********************************************************************/
  347. /*****                   Read arrays of pixels                    *****/
  348. /**********************************************************************/
  349.  
  350. /* Read an array of color index pixels. */
  351. static void read_index_pixels( GLcontext *ctx,
  352.                                GLuint n, const GLint x[], const GLint y[],
  353.                                GLuint index[], const GLubyte mask[] )
  354. {
  355.    int i;
  356.    for (i=0; i<n; i++,x++) {
  357.       index[i] = vga_getpixel( x[i], FLIP(y[i]) );
  358.    }
  359. }
  360.  
  361.  
  362.  
  363. static void read_color_pixels( GLcontext *ctx,
  364.                                GLuint n, const GLint x[], const GLint y[],
  365.                                GLubyte red[], GLubyte green[],
  366.                                GLubyte blue[], GLubyte alpha[],
  367.                                const GLubyte mask[] )
  368. {
  369.    /* TODO */
  370. }
  371.  
  372.  
  373.  
  374. static void svgamesa_setup_DD_pointers( GLcontext *ctx )
  375. {
  376.    /* Initialize all the pointers in the DD struct.  Do this whenever */
  377.    /* a new context is made current or we change buffers via set_buffer! */
  378.  
  379.    ctx->Driver.UpdateState = svgamesa_setup_DD_pointers;
  380.  
  381.    ctx->Driver.ClearIndex = clear_index;
  382.    ctx->Driver.ClearColor = clear_color;
  383.    ctx->Driver.Clear = clear;
  384.  
  385.    ctx->Driver.Index = set_index;
  386.    ctx->Driver.Color = set_color;
  387.  
  388.    ctx->Driver.SetBuffer = set_buffer;
  389.    ctx->Driver.GetBufferSize = get_buffer_size;
  390.  
  391.    ctx->Driver.PointsFunc = NULL;
  392.    ctx->Driver.LineFunc = NULL;
  393.    ctx->Driver.TriangleFunc = NULL;
  394.  
  395.    /* Pixel/span writing functions: */
  396.    /* TODO: use different funcs for 8, 16, 32-bit depths */
  397.    ctx->Driver.WriteColorSpan       = write_color_span;
  398.    ctx->Driver.WriteMonocolorSpan   = write_monocolor_span;
  399.    ctx->Driver.WriteColorPixels     = write_color_pixels;
  400.    ctx->Driver.WriteMonocolorPixels = write_monocolor_pixels;
  401.    ctx->Driver.WriteIndexSpan       = write_index_span;
  402.    ctx->Driver.WriteMonoindexSpan   = write_monoindex_span;
  403.    ctx->Driver.WriteIndexPixels     = write_index_pixels;
  404.    ctx->Driver.WriteMonoindexPixels = write_monoindex_pixels;
  405.  
  406.    /* Pixel/span reading functions: */
  407.    /* TODO: use different funcs for 8, 16, 32-bit depths */
  408.    ctx->Driver.ReadIndexSpan = read_index_span;
  409.    ctx->Driver.ReadColorSpan = read_color_span;
  410.    ctx->Driver.ReadIndexPixels = read_index_pixels;
  411.    ctx->Driver.ReadColorPixels = read_color_pixels;
  412. }
  413.  
  414.  
  415.  
  416. /*
  417.  * Create a new VGA/Mesa context and return a handle to it.
  418.  */
  419. SVGAMesaContext SVGAMesaCreateContext( GLboolean doubleBuffer )
  420. {
  421.    SVGAMesaContext ctx;
  422.    GLboolean rgb_flag;
  423.    GLfloat redscale, greenscale, bluescale, alphascale;
  424.    GLboolean alpha_flag = GL_FALSE;
  425.    int colors;
  426.    GLint index_bits;
  427.    GLint redbits, greenbits, bluebits, alphabits;
  428.  
  429.    /* determine if we're in RGB or color index mode */
  430.    colors = vga_getcolors();
  431.    if (colors==32768) {
  432.       rgb_flag = GL_TRUE;
  433.       redscale = greenscale = bluescale = alphascale = 255.0;
  434.       redbits = greenbits = bluebits = 8;
  435.       alphabits = 0;
  436.       index_bits = 0;
  437.    }
  438.    else if (colors==256) {
  439.       rgb_flag = GL_FALSE;
  440.       redscale = greenscale = bluescale = alphascale = 0.0;
  441.       redbits = greenbits = bluebits = alphabits = 0;
  442.       index_bits = 8;
  443.    }
  444.    else {
  445.       printf(">16 bit color not implemented yet!\n");
  446.       return NULL;
  447.    }
  448.  
  449.    ctx = (SVGAMesaContext) calloc( 1, sizeof(struct svgamesa_context) );
  450.    if (!ctx) {
  451.       return NULL;
  452.    }
  453.  
  454.    ctx->gl_vis = gl_create_visual( rgb_flag,
  455.                                    alpha_flag,
  456.                                    doubleBuffer,
  457.                                    16,   /* depth_size */
  458.                                    8,    /* stencil_size */
  459.                                    16,   /* accum_size */
  460.                                    index_bits,
  461.                                    redscale,
  462.                                    greenscale,
  463.                                    bluescale,
  464.                                    alphascale,
  465.                                    redbits, greenbits,
  466.                                    bluebits, alphabits);
  467.  
  468.    ctx->gl_ctx = gl_create_context( ctx->gl_vis,
  469.                                     NULL,  /* share list context */
  470.                                     (void *) ctx
  471.                                   );
  472.  
  473.    ctx->gl_buffer = gl_create_framebuffer( ctx->gl_vis );
  474.  
  475.    ctx->index = 1;
  476.    ctx->red = ctx->green = ctx->blue = 255;
  477.  
  478.    ctx->width = ctx->height = 0;  /* temporary until first "make-current" */
  479.  
  480.    return ctx;
  481. }
  482.  
  483.  
  484.  
  485.  
  486. /*
  487.  * Destroy the given VGA/Mesa context.
  488.  */
  489. void SVGAMesaDestroyContext( SVGAMesaContext ctx )
  490. {
  491.    if (ctx) {
  492.       gl_destroy_visual( ctx->gl_vis );
  493.       gl_destroy_context( ctx->gl_ctx );
  494.       gl_destroy_framebuffer( ctx->gl_buffer );
  495.       free( ctx );
  496.       if (ctx==SVGAMesa) {
  497.          SVGAMesa = NULL;
  498.       }
  499.    }
  500. }
  501.  
  502.  
  503.  
  504. /*
  505.  * Make the specified VGA/Mesa context the current one.
  506.  */
  507. void SVGAMesaMakeCurrent( SVGAMesaContext ctx )
  508. {
  509.    SVGAMesa = ctx;
  510.    gl_make_current( ctx->gl_ctx, ctx->gl_buffer );
  511.    svgamesa_setup_DD_pointers( ctx->gl_ctx );
  512.  
  513.    if (ctx->width==0 || ctx->height==0) {
  514.       /* setup initial viewport */
  515.       ctx->width = vga_getxdim();
  516.       ctx->height = vga_getydim();
  517.       gl_Viewport( ctx->gl_ctx, 0, 0, ctx->width, ctx->height );
  518.    }
  519. }
  520.  
  521.  
  522.  
  523. /*
  524.  * Return a handle to the current VGA/Mesa context.
  525.  */
  526. SVGAMesaContext SVGAMesaGetCurrentContext( void )
  527. {
  528.    return SVGAMesa;
  529. }
  530.  
  531.  
  532. /*
  533.  * Swap front/back buffers for current context if double buffered.
  534.  */
  535. void SVGAMesaSwapBuffers( void )
  536. {
  537.    if (SVGAMesa->gl_vis->DBflag) {
  538.       vga_flip();
  539.    }
  540. }
  541.  
  542.  
  543. #else
  544.  
  545. /*
  546.  * Need this to provide at least one external definition when SVGA is
  547.  * not defined on the compiler command line.
  548.  */
  549.  
  550. int gl_svga_dummy_function(void)
  551. {
  552.    return 0;
  553. }
  554.  
  555. #endif  /*SVGA*/
  556.  
  557.