1 /* $Id: svgamesa.c,v 1.2 2000/01/22 20:08:36 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)
43 #include "GL/svgamesa.h"
51 #include "svgamesa8.h"
52 #include "svgamesa15.h"
53 #include "svgamesa16.h"
54 #include "svgamesa24.h"
55 #include "svgamesa32.h"
57 struct svga_buffer SVGABuffer
;
58 vga_modeinfo
* SVGAInfo
;
59 SVGAMesaContext SVGAMesa
; /* the current context */
63 #include <sys/types.h>
69 void SVGAlog(char * what
)
71 logfile
=fopen("svgamesa.log","a");
73 fprintf(logfile
,"%s\n",what
);
78 /**********************************************************************/
79 /***** Init stuff... *****/
80 /**********************************************************************/
82 int SVGAMesaInit( int GraphMode
)
85 if (!vga_hasmode(GraphMode
))
87 fprintf(stderr
,"GraphMode %d unavailable...",GraphMode
);
89 SVGAlog("SVGAMesaInit: invalid GraphMode (doesn't exist)");
93 SVGAInfo
=vga_getmodeinfo(GraphMode
);
94 if (SVGAInfo
->flags
& IS_MODEX
)
96 fprintf(stderr
,"ModeX not implemented...");
98 SVGAlog("SVGAMesaInit: invalid GraphMode (ModeX)");
102 if (!SVGAInfo
->bytesperpixel
)
104 fprintf(stderr
,"1 / 4 bit color not implemented...");
106 SVGAlog("SVGAMesaInit: invalid GraphMode (1 or 4 bit)");
110 switch (SVGAInfo
->colors
) {
111 case 256: SVGABuffer
.Depth
= 8; break;
112 case 32768: SVGABuffer
.Depth
= 15; break;
113 case 65536: SVGABuffer
.Depth
= 16; break;
114 default: SVGABuffer
.Depth
= SVGAInfo
->bytesperpixel
<<3; break;
116 SVGABuffer
.BufferSize
=SVGAInfo
->linewidth
*SVGAInfo
->height
;
118 sprintf(cbuf
,"SVGAMesaInit: double buffer info.\n" \
124 SVGABuffer
.Depth
,GraphMode
,SVGAInfo
->linewidth
, \
125 SVGAInfo
->height
,SVGABuffer
.BufferSize
);
128 SVGABuffer
.FrontBuffer
=(void*)malloc(SVGABuffer
.BufferSize
+ 4);
129 if (!SVGABuffer
.FrontBuffer
) {
131 fprintf(stderr
,"Not enough RAM for FRONT_LEFT_BUFFER...");
133 SVGAlog("SVGAMesaInit: Not enough RAM (front buffer)");
139 sprintf(cbuf
,"SVGAMesaInit: FrontBuffer - %p",SVGABuffer
.FrontBuffer
);
142 SVGABuffer
.BackBuffer
=(void*)malloc(SVGABuffer
.BufferSize
+ 4);
143 if (!SVGABuffer
.BackBuffer
) {
145 free(SVGABuffer
.FrontBuffer
);
146 fprintf(stderr
,"Not enough RAM for BACK_LEFT_BUFFER...");
148 SVGAlog("SVGAMesaInit: Not enough RAM (back buffer)");
154 sprintf(cbuf
,"SVGAMesaInit: BackBuffer - %p",SVGABuffer
.BackBuffer
);
158 vga_setmode(GraphMode
);
159 SVGABuffer
.VideoRam
=vga_getgraphmem();
161 sprintf(cbuf
,"SVGAMesaInit: VRAM - %p",SVGABuffer
.VideoRam
);
163 sprintf(cbuf
,"SVGAMesaInit: done. (Mode %d)",GraphMode
);
169 int SVGAMesaClose( void )
172 free(SVGABuffer
.FrontBuffer
);
173 free(SVGABuffer
.BackBuffer
);
177 void SVGAMesaSetCI(int ndx
, GLubyte red
, GLubyte green
, GLubyte blue
)
179 if (ndx
<256) vga_setpalette(ndx
, red
>>2, green
>>2, blue
>>2);
182 /**********************************************************************/
183 /***** Miscellaneous functions *****/
184 /**********************************************************************/
186 static void copy_buffer( GLubyte
* buffer
) {
187 int size
= SVGABuffer
.BufferSize
, page
= 0;
190 sprintf(cbuf
,"copy_buffer: copy %p to %p",buffer
,SVGABuffer
.VideoRam
);
197 memcpy(SVGABuffer
.VideoRam
,buffer
,0x10000);
200 memcpy(SVGABuffer
.VideoRam
,buffer
,size
& 0xffff);
206 static void get_buffer_size( GLcontext
*ctx
, GLuint
*width
, GLuint
*height
)
208 *width
= SVGAMesa
->width
= vga_getxdim();
209 *height
= SVGAMesa
->height
= vga_getydim();
212 static GLboolean
set_buffer( GLcontext
*ctx
, GLenum buffer
)
216 if (buffer
== GL_FRONT_LEFT
)
218 /* vga_waitretrace(); */
219 copy_buffer(SVGABuffer
.FrontBuffer
);
220 tmpptr
=SVGABuffer
.BackBuffer
;
221 SVGABuffer
.BackBuffer
=SVGABuffer
.FrontBuffer
;
222 SVGABuffer
.FrontBuffer
=tmpptr
;
225 else if (buffer
== GL_BACK_LEFT
)
227 /* vga_waitretrace(); */
228 copy_buffer(SVGABuffer
.BackBuffer
);
235 /**********************************************************************/
237 /**********************************************************************/
239 static void svgamesa_update_state( GLcontext
*ctx
)
241 /* Initialize all the pointers in the DD struct. Do this whenever */
242 /* a new context is made current or we change buffers via set_buffer! */
244 ctx
->Driver
.UpdateState
= svgamesa_update_state
;
246 ctx
->Driver
.SetBuffer
= set_buffer
;
247 ctx
->Driver
.GetBufferSize
= get_buffer_size
;
249 ctx
->Driver
.PointsFunc
= NULL
;
250 ctx
->Driver
.LineFunc
= NULL
;
251 ctx
->Driver
.TriangleFunc
= NULL
;
253 switch (SVGABuffer
.Depth
) {
254 case 8: ctx
->Driver
.ClearIndex
= __clear_index8
;
255 ctx
->Driver
.Clear
= __clear8
;
256 ctx
->Driver
.Index
= __set_index8
;
258 ctx
->Driver
.ReadCI32Span
= __read_ci32_span8
;
259 ctx
->Driver
.ReadCI32Pixels
= __read_ci32_pixels8
;
260 ctx
->Driver
.WriteCI8Span
= __write_ci8_span8
;
261 ctx
->Driver
.WriteCI32Span
= __write_ci32_span8
;
262 ctx
->Driver
.WriteCI32Pixels
= __write_ci32_pixels8
;
263 ctx
->Driver
.WriteMonoCISpan
= __write_mono_ci_span8
;
264 ctx
->Driver
.WriteMonoCIPixels
= __write_mono_ci_pixels8
;
266 SVGAlog("SVGAUpdateState: 8 bit mode.");
270 case 15: ctx
->Driver
.ClearColor
= __clear_color15
;
271 ctx
->Driver
.Clear
= __clear15
;
272 ctx
->Driver
.Color
= __set_color15
;
274 ctx
->Driver
.ReadRGBASpan
= __read_rgba_span15
;
275 ctx
->Driver
.ReadRGBAPixels
= __read_rgba_pixels15
;
276 ctx
->Driver
.WriteRGBASpan
= __write_rgba_span15
;
277 ctx
->Driver
.WriteRGBAPixels
= __write_rgba_pixels15
;
278 ctx
->Driver
.WriteMonoRGBASpan
= __write_mono_rgba_span15
;
279 ctx
->Driver
.WriteMonoRGBAPixels
= __write_mono_rgba_pixels15
;
281 SVGAlog("SVGAUpdateState: 15 bit mode.");
284 case 16: ctx
->Driver
.ClearColor
= __clear_color16
;
285 ctx
->Driver
.Clear
= __clear16
;
286 ctx
->Driver
.Color
= __set_color16
;
288 ctx
->Driver
.ReadRGBASpan
= __read_rgba_span16
;
289 ctx
->Driver
.ReadRGBAPixels
= __read_rgba_pixels16
;
290 ctx
->Driver
.WriteRGBASpan
= __write_rgba_span16
;
291 ctx
->Driver
.WriteRGBAPixels
= __write_rgba_pixels16
;
292 ctx
->Driver
.WriteMonoRGBASpan
= __write_mono_rgba_span16
;
293 ctx
->Driver
.WriteMonoRGBAPixels
= __write_mono_rgba_pixels16
;
296 SVGAlog("SVGAUpdateState: 16 bit mode.");
298 case 24: ctx
->Driver
.ClearColor
= __clear_color24
;
299 ctx
->Driver
.Clear
= __clear24
;
300 ctx
->Driver
.Color
= __set_color24
;
302 ctx
->Driver
.ReadRGBASpan
= __read_rgba_span24
;
303 ctx
->Driver
.ReadRGBAPixels
= __read_rgba_pixels24
;
304 ctx
->Driver
.WriteRGBASpan
= __write_rgba_span24
;
305 ctx
->Driver
.WriteRGBAPixels
= __write_rgba_pixels24
;
306 ctx
->Driver
.WriteMonoRGBASpan
= __write_mono_rgba_span24
;
307 ctx
->Driver
.WriteMonoRGBAPixels
= __write_mono_rgba_pixels24
;
310 SVGAlog("SVGAUpdateState: 32 bit mode.");
312 case 32: ctx
->Driver
.ClearColor
= __clear_color32
;
313 ctx
->Driver
.Clear
= __clear32
;
314 ctx
->Driver
.Color
= __set_color32
;
316 ctx
->Driver
.ReadRGBASpan
= __read_rgba_span32
;
317 ctx
->Driver
.ReadRGBAPixels
= __read_rgba_pixels32
;
318 ctx
->Driver
.WriteRGBASpan
= __write_rgba_span32
;
319 ctx
->Driver
.WriteRGBAPixels
= __write_rgba_pixels32
;
320 ctx
->Driver
.WriteMonoRGBASpan
= __write_mono_rgba_span32
;
321 ctx
->Driver
.WriteMonoRGBAPixels
= __write_mono_rgba_pixels32
;
326 * Create a new VGA/Mesa context and return a handle to it.
328 SVGAMesaContext
SVGAMesaCreateContext( GLboolean doubleBuffer
)
333 GLfloat redscale
, greenscale
, bluescale
, alphascale
;
334 GLboolean alpha_flag
= GL_FALSE
;
336 GLint redbits
, greenbits
, bluebits
, alphabits
;
337 /* determine if we're in RGB or color index mode */
338 if ((SVGABuffer
.Depth
==32) || (SVGABuffer
.Depth
==24)) {
340 redscale
= greenscale
= bluescale
= alphascale
= 255.0;
341 redbits
= greenbits
= bluebits
= 8;
345 else if (SVGABuffer
.Depth
==8) {
347 redscale
= greenscale
= bluescale
= alphascale
= 0.0;
348 redbits
= greenbits
= bluebits
= alphabits
= 0;
351 else if (SVGABuffer
.Depth
==15) {
353 redscale
= greenscale
= bluescale
= alphascale
= 31.0;
354 redbits
= greenbits
= bluebits
= 5;
358 else if (SVGABuffer
.Depth
==16) {
360 redscale
= bluescale
= alphascale
= 31.0;
362 redbits
= bluebits
= 5;
368 ctx
= (SVGAMesaContext
) calloc( 1, sizeof(struct svgamesa_context
) );
373 ctx
->gl_vis
= gl_create_visual( rgb_flag
,
376 GL_FALSE
, /* stereo */
378 8, /* stencil_size */
382 bluebits
, alphabits
);
384 ctx
->gl_ctx
= gl_create_context( ctx
->gl_vis
,
385 NULL
, /* share list context */
386 (void *) ctx
, GL_TRUE
);
388 ctx
->gl_buffer
= gl_create_framebuffer( ctx
->gl_vis
);
391 ctx
->red
= ctx
->green
= ctx
->blue
= 255;
393 ctx
->width
= ctx
->height
= 0; /* temporary until first "make-current" */
399 * Destroy the given VGA/Mesa context.
401 void SVGAMesaDestroyContext( SVGAMesaContext ctx
)
405 gl_destroy_visual( ctx
->gl_vis
);
406 gl_destroy_context( ctx
->gl_ctx
);
407 gl_destroy_framebuffer( ctx
->gl_buffer
);
417 * Make the specified VGA/Mesa context the current one.
419 void SVGAMesaMakeCurrent( SVGAMesaContext ctx
)
423 svgamesa_update_state( ctx
->gl_ctx
);
424 gl_make_current( ctx
->gl_ctx
, ctx
->gl_buffer
);
426 if (ctx
->width
==0 || ctx
->height
==0) {
427 /* setup initial viewport */
428 ctx
->width
= vga_getxdim();
429 ctx
->height
= vga_getydim();
430 gl_Viewport( ctx
->gl_ctx
, 0, 0, ctx
->width
, ctx
->height
);
436 * Return a handle to the current VGA/Mesa context.
438 SVGAMesaContext
SVGAMesaGetCurrentContext( void )
444 * Swap front/back buffers for current context if double buffered.
446 void SVGAMesaSwapBuffers( void )
450 /* vga_waitretrace(); */
451 copy_buffer(SVGABuffer
.BackBuffer
);
454 FLUSH_VB( SVGAMesa
->gl_ctx
, "swap buffers" );
455 if (SVGAMesa
->gl_vis
->DBflag
)
459 sprintf(cbuf
,"SVGAMesaSwapBuffers : Swapping...");
461 #endif /* SVGA_DEBUG */
462 tmpptr
=SVGABuffer
.BackBuffer
;
463 SVGABuffer
.BackBuffer
=SVGABuffer
.FrontBuffer
;
464 SVGABuffer
.FrontBuffer
=tmpptr
;
466 sprintf(cbuf
,"SVGAMesaSwapBuffers : WriteBuffer : %p\n"
467 " Readbuffer : %p", \
468 SVGABuffer
.BackBuffer
, SVGABuffer
.FrontBuffer
);
470 #endif /* SVGA_DEBUG */
477 * Need this to provide at least one external definition when SVGA is
478 * not defined on the compiler command line.
481 int gl_svga_dummy_function(void)