1 /* $Id: svgamesa.c,v 1.25 2004/11/29 17:30:21 brianp Exp $ */
4 * Mesa 3-D graphics library
6 * Copyright (C) 1995-2002 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)
41 #include "GL/svgamesa.h"
44 #include "extensions.h"
48 #include "swrast/swrast.h"
50 #include "svgamesa8.h"
51 #include "svgamesa15.h"
52 #include "svgamesa16.h"
53 #include "svgamesa24.h"
54 #include "svgamesa32.h"
56 struct svga_buffer SVGABuffer
;
57 vga_modeinfo
* SVGAInfo
;
58 SVGAMesaContext SVGAMesa
; /* the current context */
62 #include <sys/types.h>
68 void SVGAlog(char * what
)
70 logfile
=fopen("svgamesa.log","a");
72 fprintf(logfile
,"%s\n",what
);
77 /**********************************************************************/
78 /***** Init stuff... *****/
79 /**********************************************************************/
81 int SVGAMesaInit( int GraphMode
)
84 if (!vga_hasmode(GraphMode
))
86 fprintf(stderr
,"GraphMode %d unavailable...",GraphMode
);
88 SVGAlog("SVGAMesaInit: invalid GraphMode (doesn't exist)");
92 SVGAInfo
=vga_getmodeinfo(GraphMode
);
93 if (SVGAInfo
->flags
& IS_MODEX
)
95 fprintf(stderr
,"ModeX not implemented...");
97 SVGAlog("SVGAMesaInit: invalid GraphMode (ModeX)");
101 if (!SVGAInfo
->bytesperpixel
)
103 fprintf(stderr
,"1 / 4 bit color not implemented...");
105 SVGAlog("SVGAMesaInit: invalid GraphMode (1 or 4 bit)");
109 switch (SVGAInfo
->colors
) {
110 case 256: SVGABuffer
.Depth
= 8; break;
111 case 32768: SVGABuffer
.Depth
= 15; break;
112 case 65536: SVGABuffer
.Depth
= 16; break;
113 default: SVGABuffer
.Depth
= SVGAInfo
->bytesperpixel
<<3; break;
115 SVGABuffer
.BufferSize
=SVGAInfo
->linewidth
*SVGAInfo
->height
;
117 sprintf(cbuf
,"SVGAMesaInit: double buffer info.\n" \
123 SVGABuffer
.Depth
,GraphMode
,SVGAInfo
->linewidth
, \
124 SVGAInfo
->height
,SVGABuffer
.BufferSize
);
127 SVGABuffer
.FrontBuffer
=(void*)malloc(SVGABuffer
.BufferSize
+ 4);
128 if (!SVGABuffer
.FrontBuffer
) {
130 fprintf(stderr
,"Not enough RAM for FRONT_LEFT_BUFFER...");
132 SVGAlog("SVGAMesaInit: Not enough RAM (front buffer)");
138 sprintf(cbuf
,"SVGAMesaInit: FrontBuffer - %p",SVGABuffer
.FrontBuffer
);
141 SVGABuffer
.BackBuffer
=(void*)malloc(SVGABuffer
.BufferSize
+ 4);
142 if (!SVGABuffer
.BackBuffer
) {
144 free(SVGABuffer
.FrontBuffer
);
145 fprintf(stderr
,"Not enough RAM for BACK_LEFT_BUFFER...");
147 SVGAlog("SVGAMesaInit: Not enough RAM (back buffer)");
153 sprintf(cbuf
,"SVGAMesaInit: BackBuffer - %p",SVGABuffer
.BackBuffer
);
157 vga_setmode(GraphMode
);
158 SVGABuffer
.VideoRam
=vga_getgraphmem();
160 sprintf(cbuf
,"SVGAMesaInit: VRAM - %p",SVGABuffer
.VideoRam
);
162 sprintf(cbuf
,"SVGAMesaInit: done. (Mode %d)",GraphMode
);
166 SVGABuffer
.DrawBuffer
= SVGABuffer
.BackBuffer
;
167 SVGABuffer
.ReadBuffer
= SVGABuffer
.BackBuffer
;
172 int SVGAMesaClose( void )
175 free(SVGABuffer
.FrontBuffer
);
176 free(SVGABuffer
.BackBuffer
);
180 void SVGAMesaSetCI(int ndx
, GLubyte red
, GLubyte green
, GLubyte blue
)
183 vga_setpalette(ndx
, red
>>2, green
>>2, blue
>>2);
186 /**********************************************************************/
187 /***** Miscellaneous functions *****/
188 /**********************************************************************/
190 static void copy_buffer( const GLubyte
* buffer
) {
191 int size
= SVGABuffer
.BufferSize
, page
= 0;
194 sprintf(cbuf
,"copy_buffer: copy %p to %p",buffer
,SVGABuffer
.VideoRam
);
201 memcpy(SVGABuffer
.VideoRam
,buffer
,0x10000);
204 memcpy(SVGABuffer
.VideoRam
,buffer
,size
& 0xffff);
210 static void get_buffer_size( GLframebuffer
*buffer
, GLuint
*width
, GLuint
*height
)
212 *width
= SVGAMesa
->width
= vga_getxdim();
213 *height
= SVGAMesa
->height
= vga_getydim();
216 static void viewport(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
218 /* poll for window size change and realloc software Z/stencil/etc if needed */
219 _mesa_ResizeBuffersMESA();
222 static void set_buffer( GLcontext
*ctx
, GLframebuffer
*colorBuffer
,
225 /* We can ignore colorBuffer since we don't support a MakeCurrentRead()
230 if (buffer
== GL_FRONT_LEFT
) {
231 SVGABuffer
.ReadBuffer
= SVGABuffer
.FrontBuffer
;
232 SVGABuffer
.DrawBuffer
= SVGABuffer
.FrontBuffer
;
235 /* vga_waitretrace(); */
236 copy_buffer(SVGABuffer
.FrontBuffer
);
237 tmpptr
=SVGABuffer
.BackBuffer
;
238 SVGABuffer
.BackBuffer
=SVGABuffer
.FrontBuffer
;
239 SVGABuffer
.FrontBuffer
=tmpptr
;
242 else if (buffer
== GL_BACK_LEFT
) {
243 SVGABuffer
.ReadBuffer
= SVGABuffer
.BackBuffer
;
244 SVGABuffer
.DrawBuffer
= SVGABuffer
.BackBuffer
;
246 /* vga_waitretrace(); */
247 copy_buffer(SVGABuffer
.BackBuffer
);
252 /**********************************************************************/
254 /**********************************************************************/
256 static void svgamesa_update_state( GLcontext
*ctx
, GLuint new_state
)
258 struct swrast_device_driver
*swdd
= _swrast_GetDeviceDriverReference( ctx
);
260 /* Initialize all the pointers in the DD struct. Do this whenever */
261 /* a new context is made current or we change buffers via set_buffer! */
263 ctx
->Driver
.UpdateState
= svgamesa_update_state
;
265 ctx
->Driver
.GetBufferSize
= get_buffer_size
;
266 ctx
->Driver
.ResizeBuffers
= _swrast_alloc_buffers
;
267 ctx
->Driver
.Viewport
= viewport
;
269 /* Software rasterizer pixel paths:
271 ctx
->Driver
.Accum
= _swrast_Accum
;
272 ctx
->Driver
.Bitmap
= _swrast_Bitmap
;
273 ctx
->Driver
.CopyPixels
= _swrast_CopyPixels
;
274 ctx
->Driver
.DrawPixels
= _swrast_DrawPixels
;
275 ctx
->Driver
.ReadPixels
= _swrast_ReadPixels
;
276 ctx
->Driver
.DrawBuffer
= _swrast_DrawBuffer
;
278 /* Fill in the swrast driver interface:
280 swdd
->SetBuffer
= set_buffer
;
282 switch (SVGABuffer
.Depth
) {
283 case 8: ctx
->Driver
.ClearIndex
= __clear_index8
;
284 ctx
->Driver
.Clear
= __clear8
;
286 swdd
->ReadCI32Span
= __read_ci32_span8
;
287 swdd
->ReadCI32Pixels
= __read_ci32_pixels8
;
288 swdd
->WriteCI8Span
= __write_ci8_span8
;
289 swdd
->WriteCI32Span
= __write_ci32_span8
;
290 swdd
->WriteCI32Pixels
= __write_ci32_pixels8
;
291 swdd
->WriteMonoCISpan
= __write_mono_ci_span8
;
292 swdd
->WriteMonoCIPixels
= __write_mono_ci_pixels8
;
294 SVGAlog("SVGAUpdateState: 8 bit mode.");
298 case 15: ctx
->Driver
.ClearColor
= __clear_color15
;
299 ctx
->Driver
.Clear
= __clear15
;
301 swdd
->ReadRGBASpan
= __read_rgba_span15
;
302 swdd
->ReadRGBAPixels
= __read_rgba_pixels15
;
303 swdd
->WriteRGBASpan
= __write_rgba_span15
;
304 swdd
->WriteRGBAPixels
= __write_rgba_pixels15
;
305 swdd
->WriteMonoRGBASpan
= __write_mono_rgba_span15
;
306 swdd
->WriteMonoRGBAPixels
= __write_mono_rgba_pixels15
;
308 SVGAlog("SVGAUpdateState: 15 bit mode.");
311 case 16: ctx
->Driver
.ClearColor
= __clear_color16
;
312 ctx
->Driver
.Clear
= __clear16
;
314 swdd
->ReadRGBASpan
= __read_rgba_span16
;
315 swdd
->ReadRGBAPixels
= __read_rgba_pixels16
;
316 swdd
->WriteRGBASpan
= __write_rgba_span16
;
317 swdd
->WriteRGBAPixels
= __write_rgba_pixels16
;
318 swdd
->WriteMonoRGBASpan
= __write_mono_rgba_span16
;
319 swdd
->WriteMonoRGBAPixels
= __write_mono_rgba_pixels16
;
322 SVGAlog("SVGAUpdateState: 16 bit mode.");
324 case 24: ctx
->Driver
.ClearColor
= __clear_color24
;
325 ctx
->Driver
.Clear
= __clear24
;
327 swdd
->ReadRGBASpan
= __read_rgba_span24
;
328 swdd
->ReadRGBAPixels
= __read_rgba_pixels24
;
329 swdd
->WriteRGBASpan
= __write_rgba_span24
;
330 swdd
->WriteRGBAPixels
= __write_rgba_pixels24
;
331 swdd
->WriteMonoRGBASpan
= __write_mono_rgba_span24
;
332 swdd
->WriteMonoRGBAPixels
= __write_mono_rgba_pixels24
;
335 SVGAlog("SVGAUpdateState: 32 bit mode.");
337 case 32: ctx
->Driver
.ClearColor
= __clear_color32
;
338 ctx
->Driver
.Clear
= __clear32
;
340 swdd
->ReadRGBASpan
= __read_rgba_span32
;
341 swdd
->ReadRGBAPixels
= __read_rgba_pixels32
;
342 swdd
->WriteRGBASpan
= __write_rgba_span32
;
343 swdd
->WriteRGBAPixels
= __write_rgba_pixels32
;
344 swdd
->WriteMonoRGBASpan
= __write_mono_rgba_span32
;
345 swdd
->WriteMonoRGBAPixels
= __write_mono_rgba_pixels32
;
350 * Create a new VGA/Mesa context and return a handle to it.
352 SVGAMesaContext
SVGAMesaCreateContext( GLboolean doubleBuffer
)
357 GLfloat redscale
, greenscale
, bluescale
, alphascale
;
359 GLint redbits
, greenbits
, bluebits
, alphabits
;
361 /* determine if we're in RGB or color index mode */
362 if ((SVGABuffer
.Depth
==32) || (SVGABuffer
.Depth
==24)) {
364 redscale
= greenscale
= bluescale
= alphascale
= 255.0;
365 redbits
= greenbits
= bluebits
= 8;
369 else if (SVGABuffer
.Depth
==8) {
371 redscale
= greenscale
= bluescale
= alphascale
= 0.0;
372 redbits
= greenbits
= bluebits
= alphabits
= 0;
375 else if (SVGABuffer
.Depth
==15) {
377 redscale
= greenscale
= bluescale
= alphascale
= 31.0;
378 redbits
= greenbits
= bluebits
= 5;
382 else if (SVGABuffer
.Depth
==16) {
384 redscale
= bluescale
= alphascale
= 31.0;
386 redbits
= bluebits
= 5;
392 ctx
= (SVGAMesaContext
) calloc( 1, sizeof(struct svgamesa_context
) );
397 ctx
->gl_vis
= _mesa_create_visual( rgb_flag
,
399 GL_FALSE
, /* stereo */
404 8, /* stencil_size */
405 16, 16, 16, 16, /* accum_size */
409 ctx
->gl_ctx
= _mesa_create_context( ctx
->gl_vis
,
410 NULL
, /* share list context */
411 (void *) ctx
, GL_FALSE
);
413 _mesa_enable_sw_extensions(ctx
->gl_ctx
);
414 _mesa_enable_1_3_extensions(ctx
->gl_ctx
);
416 ctx
->gl_buffer
= _mesa_create_framebuffer( ctx
->gl_vis
,
417 ctx
->gl_vis
->depthBits
> 0,
418 ctx
->gl_vis
->stencilBits
> 0,
419 ctx
->gl_vis
->accumRedBits
> 0,
420 ctx
->gl_vis
->alphaBits
> 0 );
422 ctx
->width
= ctx
->height
= 0; /* temporary until first "make-current" */
428 * Destroy the given VGA/Mesa context.
430 void SVGAMesaDestroyContext( SVGAMesaContext ctx
)
434 _mesa_destroy_visual( ctx
->gl_vis
);
435 _mesa_destroy_context( ctx
->gl_ctx
);
436 _mesa_destroy_framebuffer( ctx
->gl_buffer
);
446 * Make the specified VGA/Mesa context the current one.
448 void SVGAMesaMakeCurrent( SVGAMesaContext ctx
)
452 svgamesa_update_state( ctx
->gl_ctx
, ~0 );
453 _mesa_make_current( ctx
->gl_ctx
, ctx
->gl_buffer
);
455 if (ctx
->width
==0 || ctx
->height
==0) {
456 ctx
->width
= vga_getxdim();
457 ctx
->height
= vga_getydim();
463 * Return a handle to the current VGA/Mesa context.
465 SVGAMesaContext
SVGAMesaGetCurrentContext( void )
471 * Swap front/back buffers for current context if double buffered.
473 void SVGAMesaSwapBuffers( void )
479 /* vga_waitretrace(); */
480 copy_buffer(SVGABuffer
.BackBuffer
);
483 _mesa_notifySwapBuffers( SVGAMesa
->gl_ctx
);
484 if (SVGAMesa
->gl_vis
->doubleBufferMode
)
488 sprintf(cbuf
,"SVGAMesaSwapBuffers : Swapping...");
490 #endif /* SVGA_DEBUG */
492 tmpptr
=SVGABuffer
.BackBuffer
;
493 SVGABuffer
.BackBuffer
=SVGABuffer
.FrontBuffer
;
494 SVGABuffer
.FrontBuffer
=tmpptr
;
497 sprintf(cbuf
,"SVGAMesaSwapBuffers : WriteBuffer : %p\n"
498 " Readbuffer : %p", \
499 SVGABuffer
.BackBuffer
, SVGABuffer
.FrontBuffer
);
501 #endif /* SVGA_DEBUG */
508 * Need this to provide at least one external definition when SVGA is
509 * not defined on the compiler command line.
511 extern int gl_svga_dummy_function(void);
512 int gl_svga_dummy_function(void)