1 /* $Id: svgamesa.c,v 1.9 2000/11/17 21:01:44 brianp 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"
52 #include "svgamesa8.h"
53 #include "svgamesa15.h"
54 #include "svgamesa16.h"
55 #include "svgamesa24.h"
56 #include "svgamesa32.h"
58 struct svga_buffer SVGABuffer
;
59 vga_modeinfo
* SVGAInfo
;
60 SVGAMesaContext SVGAMesa
; /* the current context */
64 #include <sys/types.h>
70 void SVGAlog(char * what
)
72 logfile
=fopen("svgamesa.log","a");
74 fprintf(logfile
,"%s\n",what
);
79 /**********************************************************************/
80 /***** Init stuff... *****/
81 /**********************************************************************/
83 int SVGAMesaInit( int GraphMode
)
86 if (!vga_hasmode(GraphMode
))
88 fprintf(stderr
,"GraphMode %d unavailable...",GraphMode
);
90 SVGAlog("SVGAMesaInit: invalid GraphMode (doesn't exist)");
94 SVGAInfo
=vga_getmodeinfo(GraphMode
);
95 if (SVGAInfo
->flags
& IS_MODEX
)
97 fprintf(stderr
,"ModeX not implemented...");
99 SVGAlog("SVGAMesaInit: invalid GraphMode (ModeX)");
103 if (!SVGAInfo
->bytesperpixel
)
105 fprintf(stderr
,"1 / 4 bit color not implemented...");
107 SVGAlog("SVGAMesaInit: invalid GraphMode (1 or 4 bit)");
111 switch (SVGAInfo
->colors
) {
112 case 256: SVGABuffer
.Depth
= 8; break;
113 case 32768: SVGABuffer
.Depth
= 15; break;
114 case 65536: SVGABuffer
.Depth
= 16; break;
115 default: SVGABuffer
.Depth
= SVGAInfo
->bytesperpixel
<<3; break;
117 SVGABuffer
.BufferSize
=SVGAInfo
->linewidth
*SVGAInfo
->height
;
119 sprintf(cbuf
,"SVGAMesaInit: double buffer info.\n" \
125 SVGABuffer
.Depth
,GraphMode
,SVGAInfo
->linewidth
, \
126 SVGAInfo
->height
,SVGABuffer
.BufferSize
);
129 SVGABuffer
.FrontBuffer
=(void*)malloc(SVGABuffer
.BufferSize
+ 4);
130 if (!SVGABuffer
.FrontBuffer
) {
132 fprintf(stderr
,"Not enough RAM for FRONT_LEFT_BUFFER...");
134 SVGAlog("SVGAMesaInit: Not enough RAM (front buffer)");
140 sprintf(cbuf
,"SVGAMesaInit: FrontBuffer - %p",SVGABuffer
.FrontBuffer
);
143 SVGABuffer
.BackBuffer
=(void*)malloc(SVGABuffer
.BufferSize
+ 4);
144 if (!SVGABuffer
.BackBuffer
) {
146 free(SVGABuffer
.FrontBuffer
);
147 fprintf(stderr
,"Not enough RAM for BACK_LEFT_BUFFER...");
149 SVGAlog("SVGAMesaInit: Not enough RAM (back buffer)");
155 sprintf(cbuf
,"SVGAMesaInit: BackBuffer - %p",SVGABuffer
.BackBuffer
);
159 vga_setmode(GraphMode
);
160 SVGABuffer
.VideoRam
=vga_getgraphmem();
162 sprintf(cbuf
,"SVGAMesaInit: VRAM - %p",SVGABuffer
.VideoRam
);
164 sprintf(cbuf
,"SVGAMesaInit: done. (Mode %d)",GraphMode
);
168 SVGABuffer
.DrawBuffer
= SVGABuffer
.BackBuffer
;
169 SVGABuffer
.ReadBuffer
= SVGABuffer
.BackBuffer
;
174 int SVGAMesaClose( void )
177 free(SVGABuffer
.FrontBuffer
);
178 free(SVGABuffer
.BackBuffer
);
182 void SVGAMesaSetCI(int ndx
, GLubyte red
, GLubyte green
, GLubyte blue
)
185 vga_setpalette(ndx
, red
>>2, green
>>2, blue
>>2);
188 /**********************************************************************/
189 /***** Miscellaneous functions *****/
190 /**********************************************************************/
192 static void copy_buffer( const GLubyte
* buffer
) {
193 int size
= SVGABuffer
.BufferSize
, page
= 0;
196 sprintf(cbuf
,"copy_buffer: copy %p to %p",buffer
,SVGABuffer
.VideoRam
);
203 memcpy(SVGABuffer
.VideoRam
,buffer
,0x10000);
206 memcpy(SVGABuffer
.VideoRam
,buffer
,size
& 0xffff);
212 static void get_buffer_size( GLcontext
*ctx
, GLuint
*width
, GLuint
*height
)
214 *width
= SVGAMesa
->width
= vga_getxdim();
215 *height
= SVGAMesa
->height
= vga_getydim();
218 static GLboolean
set_draw_buffer( GLcontext
*ctx
, GLenum buffer
)
220 if (buffer
== GL_FRONT_LEFT
) {
221 SVGABuffer
.DrawBuffer
= SVGABuffer
.FrontBuffer
;
223 /* vga_waitretrace(); */
225 copy_buffer(SVGABuffer
.FrontBuffer
);
226 tmpptr
=SVGABuffer
.BackBuffer
;
227 SVGABuffer
.BackBuffer
=SVGABuffer
.FrontBuffer
;
228 SVGABuffer
.FrontBuffer
=tmpptr
;
232 else if (buffer
== GL_BACK_LEFT
) {
233 SVGABuffer
.DrawBuffer
= SVGABuffer
.BackBuffer
;
235 /* vga_waitretrace(); */
236 copy_buffer(SVGABuffer
.BackBuffer
);
245 static void set_read_buffer( GLcontext
*ctx
, GLframebuffer
*colorBuffer
,
248 /* We can ignore colorBuffer since we don't support a MakeCurrentRead()
253 if (buffer
== GL_FRONT_LEFT
) {
254 SVGABuffer
.ReadBuffer
= SVGABuffer
.FrontBuffer
;
257 /* vga_waitretrace(); */
258 copy_buffer(SVGABuffer
.FrontBuffer
);
259 tmpptr
=SVGABuffer
.BackBuffer
;
260 SVGABuffer
.BackBuffer
=SVGABuffer
.FrontBuffer
;
261 SVGABuffer
.FrontBuffer
=tmpptr
;
264 else if (buffer
== GL_BACK_LEFT
) {
265 SVGABuffer
.ReadBuffer
= SVGABuffer
.BackBuffer
;
267 /* vga_waitretrace(); */
268 copy_buffer(SVGABuffer
.BackBuffer
);
273 /**********************************************************************/
275 /**********************************************************************/
277 static void svgamesa_update_state( GLcontext
*ctx
)
279 /* Initialize all the pointers in the DD struct. Do this whenever */
280 /* a new context is made current or we change buffers via set_buffer! */
282 ctx
->Driver
.UpdateState
= svgamesa_update_state
;
284 ctx
->Driver
.GetBufferSize
= get_buffer_size
;
285 ctx
->Driver
.SetDrawBuffer
= set_draw_buffer
;
286 ctx
->Driver
.SetReadBuffer
= set_read_buffer
;
288 ctx
->Driver
.PointsFunc
= NULL
;
289 ctx
->Driver
.LineFunc
= NULL
;
290 ctx
->Driver
.TriangleFunc
= NULL
;
292 switch (SVGABuffer
.Depth
) {
293 case 8: ctx
->Driver
.ClearIndex
= __clear_index8
;
294 ctx
->Driver
.Clear
= __clear8
;
296 ctx
->Driver
.ReadCI32Span
= __read_ci32_span8
;
297 ctx
->Driver
.ReadCI32Pixels
= __read_ci32_pixels8
;
298 ctx
->Driver
.WriteCI8Span
= __write_ci8_span8
;
299 ctx
->Driver
.WriteCI32Span
= __write_ci32_span8
;
300 ctx
->Driver
.WriteCI32Pixels
= __write_ci32_pixels8
;
301 ctx
->Driver
.WriteMonoCISpan
= __write_mono_ci_span8
;
302 ctx
->Driver
.WriteMonoCIPixels
= __write_mono_ci_pixels8
;
304 SVGAlog("SVGAUpdateState: 8 bit mode.");
308 case 15: ctx
->Driver
.ClearColor
= __clear_color15
;
309 ctx
->Driver
.Clear
= __clear15
;
311 ctx
->Driver
.ReadRGBASpan
= __read_rgba_span15
;
312 ctx
->Driver
.ReadRGBAPixels
= __read_rgba_pixels15
;
313 ctx
->Driver
.WriteRGBASpan
= __write_rgba_span15
;
314 ctx
->Driver
.WriteRGBAPixels
= __write_rgba_pixels15
;
315 ctx
->Driver
.WriteMonoRGBASpan
= __write_mono_rgba_span15
;
316 ctx
->Driver
.WriteMonoRGBAPixels
= __write_mono_rgba_pixels15
;
318 SVGAlog("SVGAUpdateState: 15 bit mode.");
321 case 16: ctx
->Driver
.ClearColor
= __clear_color16
;
322 ctx
->Driver
.Clear
= __clear16
;
324 ctx
->Driver
.ReadRGBASpan
= __read_rgba_span16
;
325 ctx
->Driver
.ReadRGBAPixels
= __read_rgba_pixels16
;
326 ctx
->Driver
.WriteRGBASpan
= __write_rgba_span16
;
327 ctx
->Driver
.WriteRGBAPixels
= __write_rgba_pixels16
;
328 ctx
->Driver
.WriteMonoRGBASpan
= __write_mono_rgba_span16
;
329 ctx
->Driver
.WriteMonoRGBAPixels
= __write_mono_rgba_pixels16
;
332 SVGAlog("SVGAUpdateState: 16 bit mode.");
334 case 24: ctx
->Driver
.ClearColor
= __clear_color24
;
335 ctx
->Driver
.Clear
= __clear24
;
337 ctx
->Driver
.ReadRGBASpan
= __read_rgba_span24
;
338 ctx
->Driver
.ReadRGBAPixels
= __read_rgba_pixels24
;
339 ctx
->Driver
.WriteRGBASpan
= __write_rgba_span24
;
340 ctx
->Driver
.WriteRGBAPixels
= __write_rgba_pixels24
;
341 ctx
->Driver
.WriteMonoRGBASpan
= __write_mono_rgba_span24
;
342 ctx
->Driver
.WriteMonoRGBAPixels
= __write_mono_rgba_pixels24
;
345 SVGAlog("SVGAUpdateState: 32 bit mode.");
347 case 32: ctx
->Driver
.ClearColor
= __clear_color32
;
348 ctx
->Driver
.Clear
= __clear32
;
350 ctx
->Driver
.ReadRGBASpan
= __read_rgba_span32
;
351 ctx
->Driver
.ReadRGBAPixels
= __read_rgba_pixels32
;
352 ctx
->Driver
.WriteRGBASpan
= __write_rgba_span32
;
353 ctx
->Driver
.WriteRGBAPixels
= __write_rgba_pixels32
;
354 ctx
->Driver
.WriteMonoRGBASpan
= __write_mono_rgba_span32
;
355 ctx
->Driver
.WriteMonoRGBAPixels
= __write_mono_rgba_pixels32
;
360 * Create a new VGA/Mesa context and return a handle to it.
362 SVGAMesaContext
SVGAMesaCreateContext( GLboolean doubleBuffer
)
367 GLfloat redscale
, greenscale
, bluescale
, alphascale
;
369 GLint redbits
, greenbits
, bluebits
, alphabits
;
370 /* determine if we're in RGB or color index mode */
371 if ((SVGABuffer
.Depth
==32) || (SVGABuffer
.Depth
==24)) {
373 redscale
= greenscale
= bluescale
= alphascale
= 255.0;
374 redbits
= greenbits
= bluebits
= 8;
378 else if (SVGABuffer
.Depth
==8) {
380 redscale
= greenscale
= bluescale
= alphascale
= 0.0;
381 redbits
= greenbits
= bluebits
= alphabits
= 0;
384 else if (SVGABuffer
.Depth
==15) {
386 redscale
= greenscale
= bluescale
= alphascale
= 31.0;
387 redbits
= greenbits
= bluebits
= 5;
391 else if (SVGABuffer
.Depth
==16) {
393 redscale
= bluescale
= alphascale
= 31.0;
395 redbits
= bluebits
= 5;
401 ctx
= (SVGAMesaContext
) calloc( 1, sizeof(struct svgamesa_context
) );
406 ctx
->gl_vis
= _mesa_create_visual( rgb_flag
,
408 GL_FALSE
, /* stereo */
413 8, /* stencil_size */
414 16, 16, 16, 16, /* accum_size */
418 ctx
->gl_ctx
= _mesa_create_context( ctx
->gl_vis
,
419 NULL
, /* share list context */
420 (void *) ctx
, GL_TRUE
);
422 _mesa_enable_sw_extensions(ctx
->gl_ctx
);
424 ctx
->gl_buffer
= _mesa_create_framebuffer( ctx
->gl_vis
,
425 ctx
->gl_vis
->DepthBits
> 0,
426 ctx
->gl_vis
->StencilBits
> 0,
427 ctx
->gl_vis
->AccumRedBits
> 0,
428 ctx
->gl_vis
->AlphaBits
> 0 );
430 ctx
->width
= ctx
->height
= 0; /* temporary until first "make-current" */
436 * Destroy the given VGA/Mesa context.
438 void SVGAMesaDestroyContext( SVGAMesaContext ctx
)
442 _mesa_destroy_visual( ctx
->gl_vis
);
443 _mesa_destroy_context( ctx
->gl_ctx
);
444 _mesa_destroy_framebuffer( ctx
->gl_buffer
);
454 * Make the specified VGA/Mesa context the current one.
456 void SVGAMesaMakeCurrent( SVGAMesaContext ctx
)
460 svgamesa_update_state( ctx
->gl_ctx
);
461 _mesa_make_current( ctx
->gl_ctx
, ctx
->gl_buffer
);
463 if (ctx
->width
==0 || ctx
->height
==0) {
464 /* setup initial viewport */
465 ctx
->width
= vga_getxdim();
466 ctx
->height
= vga_getydim();
467 gl_Viewport( ctx
->gl_ctx
, 0, 0, ctx
->width
, ctx
->height
);
473 * Return a handle to the current VGA/Mesa context.
475 SVGAMesaContext
SVGAMesaGetCurrentContext( void )
481 * Swap front/back buffers for current context if double buffered.
483 void SVGAMesaSwapBuffers( void )
489 /* vga_waitretrace(); */
490 copy_buffer(SVGABuffer
.BackBuffer
);
493 _mesa_swapbuffers( SVGAMesa
->gl_ctx
);
494 if (SVGAMesa
->gl_vis
->DBflag
)
498 sprintf(cbuf
,"SVGAMesaSwapBuffers : Swapping...");
500 #endif /* SVGA_DEBUG */
502 tmpptr
=SVGABuffer
.BackBuffer
;
503 SVGABuffer
.BackBuffer
=SVGABuffer
.FrontBuffer
;
504 SVGABuffer
.FrontBuffer
=tmpptr
;
507 sprintf(cbuf
,"SVGAMesaSwapBuffers : WriteBuffer : %p\n"
508 " Readbuffer : %p", \
509 SVGABuffer
.BackBuffer
, SVGABuffer
.FrontBuffer
);
511 #endif /* SVGA_DEBUG */
518 * Need this to provide at least one external definition when SVGA is
519 * not defined on the compiler command line.
522 int gl_svga_dummy_function(void)