1 /* $Id: svgamesa.c,v 1.15 2001/03/19 02:25:36 keithw Exp $ */
4 * Mesa 3-D graphics library
6 * Copyright (C) 1995-2000 Brian Paul
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.
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.
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.
25 * SVGA driver for Mesa.
26 * Original author: Brian Paul
27 * Additional authors: Slawomir Szczyrba <steev@hot.pl> (Mesa 3.2)
44 #include "GL/svgamesa.h"
46 #include "extensions.h"
49 #include "swrast/swrast.h"
53 #include "svgamesa8.h"
54 #include "svgamesa15.h"
55 #include "svgamesa16.h"
56 #include "svgamesa24.h"
57 #include "svgamesa32.h"
59 struct svga_buffer SVGABuffer
;
60 vga_modeinfo
* SVGAInfo
;
61 SVGAMesaContext SVGAMesa
; /* the current context */
65 #include <sys/types.h>
71 void SVGAlog(char * what
)
73 logfile
=fopen("svgamesa.log","a");
75 fprintf(logfile
,"%s\n",what
);
80 /**********************************************************************/
81 /***** Init stuff... *****/
82 /**********************************************************************/
84 int SVGAMesaInit( int GraphMode
)
87 if (!vga_hasmode(GraphMode
))
89 fprintf(stderr
,"GraphMode %d unavailable...",GraphMode
);
91 SVGAlog("SVGAMesaInit: invalid GraphMode (doesn't exist)");
95 SVGAInfo
=vga_getmodeinfo(GraphMode
);
96 if (SVGAInfo
->flags
& IS_MODEX
)
98 fprintf(stderr
,"ModeX not implemented...");
100 SVGAlog("SVGAMesaInit: invalid GraphMode (ModeX)");
104 if (!SVGAInfo
->bytesperpixel
)
106 fprintf(stderr
,"1 / 4 bit color not implemented...");
108 SVGAlog("SVGAMesaInit: invalid GraphMode (1 or 4 bit)");
112 switch (SVGAInfo
->colors
) {
113 case 256: SVGABuffer
.Depth
= 8; break;
114 case 32768: SVGABuffer
.Depth
= 15; break;
115 case 65536: SVGABuffer
.Depth
= 16; break;
116 default: SVGABuffer
.Depth
= SVGAInfo
->bytesperpixel
<<3; break;
118 SVGABuffer
.BufferSize
=SVGAInfo
->linewidth
*SVGAInfo
->height
;
120 sprintf(cbuf
,"SVGAMesaInit: double buffer info.\n" \
126 SVGABuffer
.Depth
,GraphMode
,SVGAInfo
->linewidth
, \
127 SVGAInfo
->height
,SVGABuffer
.BufferSize
);
130 SVGABuffer
.FrontBuffer
=(void*)malloc(SVGABuffer
.BufferSize
+ 4);
131 if (!SVGABuffer
.FrontBuffer
) {
133 fprintf(stderr
,"Not enough RAM for FRONT_LEFT_BUFFER...");
135 SVGAlog("SVGAMesaInit: Not enough RAM (front buffer)");
141 sprintf(cbuf
,"SVGAMesaInit: FrontBuffer - %p",SVGABuffer
.FrontBuffer
);
144 SVGABuffer
.BackBuffer
=(void*)malloc(SVGABuffer
.BufferSize
+ 4);
145 if (!SVGABuffer
.BackBuffer
) {
147 free(SVGABuffer
.FrontBuffer
);
148 fprintf(stderr
,"Not enough RAM for BACK_LEFT_BUFFER...");
150 SVGAlog("SVGAMesaInit: Not enough RAM (back buffer)");
156 sprintf(cbuf
,"SVGAMesaInit: BackBuffer - %p",SVGABuffer
.BackBuffer
);
160 vga_setmode(GraphMode
);
161 SVGABuffer
.VideoRam
=vga_getgraphmem();
163 sprintf(cbuf
,"SVGAMesaInit: VRAM - %p",SVGABuffer
.VideoRam
);
165 sprintf(cbuf
,"SVGAMesaInit: done. (Mode %d)",GraphMode
);
169 SVGABuffer
.DrawBuffer
= SVGABuffer
.BackBuffer
;
170 SVGABuffer
.ReadBuffer
= SVGABuffer
.BackBuffer
;
175 int SVGAMesaClose( void )
178 free(SVGABuffer
.FrontBuffer
);
179 free(SVGABuffer
.BackBuffer
);
183 void SVGAMesaSetCI(int ndx
, GLubyte red
, GLubyte green
, GLubyte blue
)
186 vga_setpalette(ndx
, red
>>2, green
>>2, blue
>>2);
189 /**********************************************************************/
190 /***** Miscellaneous functions *****/
191 /**********************************************************************/
193 static void copy_buffer( const GLubyte
* buffer
) {
194 int size
= SVGABuffer
.BufferSize
, page
= 0;
197 sprintf(cbuf
,"copy_buffer: copy %p to %p",buffer
,SVGABuffer
.VideoRam
);
204 memcpy(SVGABuffer
.VideoRam
,buffer
,0x10000);
207 memcpy(SVGABuffer
.VideoRam
,buffer
,size
& 0xffff);
213 static void get_buffer_size( GLcontext
*ctx
, GLuint
*width
, GLuint
*height
)
215 *width
= SVGAMesa
->width
= vga_getxdim();
216 *height
= SVGAMesa
->height
= vga_getydim();
219 static GLboolean
set_draw_buffer( GLcontext
*ctx
, GLenum buffer
)
221 if (buffer
== GL_FRONT_LEFT
) {
222 SVGABuffer
.DrawBuffer
= SVGABuffer
.FrontBuffer
;
224 /* vga_waitretrace(); */
226 copy_buffer(SVGABuffer
.FrontBuffer
);
227 tmpptr
=SVGABuffer
.BackBuffer
;
228 SVGABuffer
.BackBuffer
=SVGABuffer
.FrontBuffer
;
229 SVGABuffer
.FrontBuffer
=tmpptr
;
233 else if (buffer
== GL_BACK_LEFT
) {
234 SVGABuffer
.DrawBuffer
= SVGABuffer
.BackBuffer
;
236 /* vga_waitretrace(); */
237 copy_buffer(SVGABuffer
.BackBuffer
);
246 static void set_read_buffer( GLcontext
*ctx
, GLframebuffer
*colorBuffer
,
249 /* We can ignore colorBuffer since we don't support a MakeCurrentRead()
254 if (buffer
== GL_FRONT_LEFT
) {
255 SVGABuffer
.ReadBuffer
= SVGABuffer
.FrontBuffer
;
258 /* vga_waitretrace(); */
259 copy_buffer(SVGABuffer
.FrontBuffer
);
260 tmpptr
=SVGABuffer
.BackBuffer
;
261 SVGABuffer
.BackBuffer
=SVGABuffer
.FrontBuffer
;
262 SVGABuffer
.FrontBuffer
=tmpptr
;
265 else if (buffer
== GL_BACK_LEFT
) {
266 SVGABuffer
.ReadBuffer
= SVGABuffer
.BackBuffer
;
268 /* vga_waitretrace(); */
269 copy_buffer(SVGABuffer
.BackBuffer
);
274 /**********************************************************************/
276 /**********************************************************************/
278 static void svgamesa_update_state( GLcontext
*ctx
, GLuint new_state
)
280 struct swrast_device_driver
*swdd
= _swrast_GetDeviceDriverReference( ctx
);
282 /* Initialize all the pointers in the DD struct. Do this whenever */
283 /* a new context is made current or we change buffers via set_buffer! */
285 ctx
->Driver
.UpdateState
= svgamesa_update_state
;
287 ctx
->Driver
.GetBufferSize
= get_buffer_size
;
288 ctx
->Driver
.SetDrawBuffer
= set_draw_buffer
;
290 /* Software rasterizer pixel paths:
292 ctx
->Driver
.Accum
= _swrast_Accum
;
293 ctx
->Driver
.Bitmap
= _swrast_Bitmap
;
294 ctx
->Driver
.CopyPixels
= _swrast_CopyPixels
;
295 ctx
->Driver
.DrawPixels
= _swrast_DrawPixels
;
296 ctx
->Driver
.ReadPixels
= _swrast_ReadPixels
;
298 /* Fill in the swrast driver interface:
300 swdd
->SetReadBuffer
= set_read_buffer
;
302 switch (SVGABuffer
.Depth
) {
303 case 8: ctx
->Driver
.ClearIndex
= __clear_index8
;
304 ctx
->Driver
.Clear
= __clear8
;
306 swdd
->ReadCI32Span
= __read_ci32_span8
;
307 swdd
->ReadCI32Pixels
= __read_ci32_pixels8
;
308 swdd
->WriteCI8Span
= __write_ci8_span8
;
309 swdd
->WriteCI32Span
= __write_ci32_span8
;
310 swdd
->WriteCI32Pixels
= __write_ci32_pixels8
;
311 swdd
->WriteMonoCISpan
= __write_mono_ci_span8
;
312 swdd
->WriteMonoCIPixels
= __write_mono_ci_pixels8
;
314 SVGAlog("SVGAUpdateState: 8 bit mode.");
318 case 15: ctx
->Driver
.ClearColor
= __clear_color15
;
319 ctx
->Driver
.Clear
= __clear15
;
321 swdd
->ReadRGBASpan
= __read_rgba_span15
;
322 swdd
->ReadRGBAPixels
= __read_rgba_pixels15
;
323 swdd
->WriteRGBASpan
= __write_rgba_span15
;
324 swdd
->WriteRGBAPixels
= __write_rgba_pixels15
;
325 swdd
->WriteMonoRGBASpan
= __write_mono_rgba_span15
;
326 swdd
->WriteMonoRGBAPixels
= __write_mono_rgba_pixels15
;
328 SVGAlog("SVGAUpdateState: 15 bit mode.");
331 case 16: ctx
->Driver
.ClearColor
= __clear_color16
;
332 ctx
->Driver
.Clear
= __clear16
;
334 swdd
->ReadRGBASpan
= __read_rgba_span16
;
335 swdd
->ReadRGBAPixels
= __read_rgba_pixels16
;
336 swdd
->WriteRGBASpan
= __write_rgba_span16
;
337 swdd
->WriteRGBAPixels
= __write_rgba_pixels16
;
338 swdd
->WriteMonoRGBASpan
= __write_mono_rgba_span16
;
339 swdd
->WriteMonoRGBAPixels
= __write_mono_rgba_pixels16
;
342 SVGAlog("SVGAUpdateState: 16 bit mode.");
344 case 24: ctx
->Driver
.ClearColor
= __clear_color24
;
345 ctx
->Driver
.Clear
= __clear24
;
347 swdd
->ReadRGBASpan
= __read_rgba_span24
;
348 swdd
->ReadRGBAPixels
= __read_rgba_pixels24
;
349 swdd
->WriteRGBASpan
= __write_rgba_span24
;
350 swdd
->WriteRGBAPixels
= __write_rgba_pixels24
;
351 swdd
->WriteMonoRGBASpan
= __write_mono_rgba_span24
;
352 swdd
->WriteMonoRGBAPixels
= __write_mono_rgba_pixels24
;
355 SVGAlog("SVGAUpdateState: 32 bit mode.");
357 case 32: ctx
->Driver
.ClearColor
= __clear_color32
;
358 ctx
->Driver
.Clear
= __clear32
;
360 swdd
->ReadRGBASpan
= __read_rgba_span32
;
361 swdd
->ReadRGBAPixels
= __read_rgba_pixels32
;
362 swdd
->WriteRGBASpan
= __write_rgba_span32
;
363 swdd
->WriteRGBAPixels
= __write_rgba_pixels32
;
364 swdd
->WriteMonoRGBASpan
= __write_mono_rgba_span32
;
365 swdd
->WriteMonoRGBAPixels
= __write_mono_rgba_pixels32
;
370 * Create a new VGA/Mesa context and return a handle to it.
372 SVGAMesaContext
SVGAMesaCreateContext( GLboolean doubleBuffer
)
377 GLfloat redscale
, greenscale
, bluescale
, alphascale
;
379 GLint redbits
, greenbits
, bluebits
, alphabits
;
380 /* determine if we're in RGB or color index mode */
381 if ((SVGABuffer
.Depth
==32) || (SVGABuffer
.Depth
==24)) {
383 redscale
= greenscale
= bluescale
= alphascale
= 255.0;
384 redbits
= greenbits
= bluebits
= 8;
388 else if (SVGABuffer
.Depth
==8) {
390 redscale
= greenscale
= bluescale
= alphascale
= 0.0;
391 redbits
= greenbits
= bluebits
= alphabits
= 0;
394 else if (SVGABuffer
.Depth
==15) {
396 redscale
= greenscale
= bluescale
= alphascale
= 31.0;
397 redbits
= greenbits
= bluebits
= 5;
401 else if (SVGABuffer
.Depth
==16) {
403 redscale
= bluescale
= alphascale
= 31.0;
405 redbits
= bluebits
= 5;
411 ctx
= (SVGAMesaContext
) calloc( 1, sizeof(struct svgamesa_context
) );
416 ctx
->gl_vis
= _mesa_create_visual( rgb_flag
,
418 GL_FALSE
, /* stereo */
423 8, /* stencil_size */
424 16, 16, 16, 16, /* accum_size */
428 ctx
->gl_ctx
= _mesa_create_context( ctx
->gl_vis
,
429 NULL
, /* share list context */
430 (void *) ctx
, GL_TRUE
);
432 _mesa_enable_sw_extensions(ctx
->gl_ctx
);
434 ctx
->gl_buffer
= _mesa_create_framebuffer( ctx
->gl_vis
,
435 ctx
->gl_vis
->depthBits
> 0,
436 ctx
->gl_vis
->stencilBits
> 0,
437 ctx
->gl_vis
->accumRedBits
> 0,
438 ctx
->gl_vis
->alphaBits
> 0 );
440 ctx
->width
= ctx
->height
= 0; /* temporary until first "make-current" */
446 * Destroy the given VGA/Mesa context.
448 void SVGAMesaDestroyContext( SVGAMesaContext ctx
)
452 _mesa_destroy_visual( ctx
->gl_vis
);
453 _mesa_destroy_context( ctx
->gl_ctx
);
454 _mesa_destroy_framebuffer( ctx
->gl_buffer
);
464 * Make the specified VGA/Mesa context the current one.
466 void SVGAMesaMakeCurrent( SVGAMesaContext ctx
)
470 svgamesa_update_state( ctx
->gl_ctx
, ~0 );
471 _mesa_make_current( ctx
->gl_ctx
, ctx
->gl_buffer
);
473 if (ctx
->width
==0 || ctx
->height
==0) {
474 /* setup initial viewport */
475 ctx
->width
= vga_getxdim();
476 ctx
->height
= vga_getydim();
477 _mesa_set_viewport( ctx
->gl_ctx
, 0, 0, ctx
->width
, ctx
->height
);
483 * Return a handle to the current VGA/Mesa context.
485 SVGAMesaContext
SVGAMesaGetCurrentContext( void )
491 * Swap front/back buffers for current context if double buffered.
493 void SVGAMesaSwapBuffers( void )
499 /* vga_waitretrace(); */
500 copy_buffer(SVGABuffer
.BackBuffer
);
503 _mesa_swapbuffers( SVGAMesa
->gl_ctx
);
504 if (SVGAMesa
->gl_vis
->doubleBufferMode
)
508 sprintf(cbuf
,"SVGAMesaSwapBuffers : Swapping...");
510 #endif /* SVGA_DEBUG */
512 tmpptr
=SVGABuffer
.BackBuffer
;
513 SVGABuffer
.BackBuffer
=SVGABuffer
.FrontBuffer
;
514 SVGABuffer
.FrontBuffer
=tmpptr
;
517 sprintf(cbuf
,"SVGAMesaSwapBuffers : WriteBuffer : %p\n"
518 " Readbuffer : %p", \
519 SVGABuffer
.BackBuffer
, SVGABuffer
.FrontBuffer
);
521 #endif /* SVGA_DEBUG */
528 * Need this to provide at least one external definition when SVGA is
529 * not defined on the compiler command line.
532 int gl_svga_dummy_function(void)