1 /* $Id: svgamesa.c,v 1.22 2002/10/24 23:57:23 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)
41 #include "GL/svgamesa.h"
43 #include "extensions.h"
47 #include "swrast/swrast.h"
49 #include "svgamesa8.h"
50 #include "svgamesa15.h"
51 #include "svgamesa16.h"
52 #include "svgamesa24.h"
53 #include "svgamesa32.h"
55 struct svga_buffer SVGABuffer
;
56 vga_modeinfo
* SVGAInfo
;
57 SVGAMesaContext SVGAMesa
; /* the current context */
61 #include <sys/types.h>
67 void SVGAlog(char * what
)
69 logfile
=fopen("svgamesa.log","a");
71 fprintf(logfile
,"%s\n",what
);
76 /**********************************************************************/
77 /***** Init stuff... *****/
78 /**********************************************************************/
80 int SVGAMesaInit( int GraphMode
)
83 if (!vga_hasmode(GraphMode
))
85 fprintf(stderr
,"GraphMode %d unavailable...",GraphMode
);
87 SVGAlog("SVGAMesaInit: invalid GraphMode (doesn't exist)");
91 SVGAInfo
=vga_getmodeinfo(GraphMode
);
92 if (SVGAInfo
->flags
& IS_MODEX
)
94 fprintf(stderr
,"ModeX not implemented...");
96 SVGAlog("SVGAMesaInit: invalid GraphMode (ModeX)");
100 if (!SVGAInfo
->bytesperpixel
)
102 fprintf(stderr
,"1 / 4 bit color not implemented...");
104 SVGAlog("SVGAMesaInit: invalid GraphMode (1 or 4 bit)");
108 switch (SVGAInfo
->colors
) {
109 case 256: SVGABuffer
.Depth
= 8; break;
110 case 32768: SVGABuffer
.Depth
= 15; break;
111 case 65536: SVGABuffer
.Depth
= 16; break;
112 default: SVGABuffer
.Depth
= SVGAInfo
->bytesperpixel
<<3; break;
114 SVGABuffer
.BufferSize
=SVGAInfo
->linewidth
*SVGAInfo
->height
;
116 sprintf(cbuf
,"SVGAMesaInit: double buffer info.\n" \
122 SVGABuffer
.Depth
,GraphMode
,SVGAInfo
->linewidth
, \
123 SVGAInfo
->height
,SVGABuffer
.BufferSize
);
126 SVGABuffer
.FrontBuffer
=(void*)malloc(SVGABuffer
.BufferSize
+ 4);
127 if (!SVGABuffer
.FrontBuffer
) {
129 fprintf(stderr
,"Not enough RAM for FRONT_LEFT_BUFFER...");
131 SVGAlog("SVGAMesaInit: Not enough RAM (front buffer)");
137 sprintf(cbuf
,"SVGAMesaInit: FrontBuffer - %p",SVGABuffer
.FrontBuffer
);
140 SVGABuffer
.BackBuffer
=(void*)malloc(SVGABuffer
.BufferSize
+ 4);
141 if (!SVGABuffer
.BackBuffer
) {
143 free(SVGABuffer
.FrontBuffer
);
144 fprintf(stderr
,"Not enough RAM for BACK_LEFT_BUFFER...");
146 SVGAlog("SVGAMesaInit: Not enough RAM (back buffer)");
152 sprintf(cbuf
,"SVGAMesaInit: BackBuffer - %p",SVGABuffer
.BackBuffer
);
156 vga_setmode(GraphMode
);
157 SVGABuffer
.VideoRam
=vga_getgraphmem();
159 sprintf(cbuf
,"SVGAMesaInit: VRAM - %p",SVGABuffer
.VideoRam
);
161 sprintf(cbuf
,"SVGAMesaInit: done. (Mode %d)",GraphMode
);
165 SVGABuffer
.DrawBuffer
= SVGABuffer
.BackBuffer
;
166 SVGABuffer
.ReadBuffer
= SVGABuffer
.BackBuffer
;
171 int SVGAMesaClose( void )
174 free(SVGABuffer
.FrontBuffer
);
175 free(SVGABuffer
.BackBuffer
);
179 void SVGAMesaSetCI(int ndx
, GLubyte red
, GLubyte green
, GLubyte blue
)
182 vga_setpalette(ndx
, red
>>2, green
>>2, blue
>>2);
185 /**********************************************************************/
186 /***** Miscellaneous functions *****/
187 /**********************************************************************/
189 static void copy_buffer( const GLubyte
* buffer
) {
190 int size
= SVGABuffer
.BufferSize
, page
= 0;
193 sprintf(cbuf
,"copy_buffer: copy %p to %p",buffer
,SVGABuffer
.VideoRam
);
200 memcpy(SVGABuffer
.VideoRam
,buffer
,0x10000);
203 memcpy(SVGABuffer
.VideoRam
,buffer
,size
& 0xffff);
209 static void get_buffer_size( GLframebuffer
*buffer
, GLuint
*width
, GLuint
*height
)
211 *width
= SVGAMesa
->width
= vga_getxdim();
212 *height
= SVGAMesa
->height
= vga_getydim();
216 static void set_buffer( GLcontext
*ctx
, GLframebuffer
*colorBuffer
,
219 /* We can ignore colorBuffer since we don't support a MakeCurrentRead()
224 if (buffer
== GL_FRONT_LEFT
) {
225 SVGABuffer
.ReadBuffer
= SVGABuffer
.FrontBuffer
;
226 SVGABuffer
.DrawBuffer
= SVGABuffer
.FrontBuffer
;
229 /* vga_waitretrace(); */
230 copy_buffer(SVGABuffer
.FrontBuffer
);
231 tmpptr
=SVGABuffer
.BackBuffer
;
232 SVGABuffer
.BackBuffer
=SVGABuffer
.FrontBuffer
;
233 SVGABuffer
.FrontBuffer
=tmpptr
;
236 else if (buffer
== GL_BACK_LEFT
) {
237 SVGABuffer
.ReadBuffer
= SVGABuffer
.BackBuffer
;
238 SVGABuffer
.DrawBuffer
= SVGABuffer
.BackBuffer
;
240 /* vga_waitretrace(); */
241 copy_buffer(SVGABuffer
.BackBuffer
);
246 /**********************************************************************/
248 /**********************************************************************/
250 static void svgamesa_update_state( GLcontext
*ctx
, GLuint new_state
)
252 struct swrast_device_driver
*swdd
= _swrast_GetDeviceDriverReference( ctx
);
254 /* Initialize all the pointers in the DD struct. Do this whenever */
255 /* a new context is made current or we change buffers via set_buffer! */
257 ctx
->Driver
.UpdateState
= svgamesa_update_state
;
259 ctx
->Driver
.GetBufferSize
= get_buffer_size
;
260 ctx
->Driver
.ResizeBuffers
= _swrast_alloc_buffers
;
262 /* Software rasterizer pixel paths:
264 ctx
->Driver
.Accum
= _swrast_Accum
;
265 ctx
->Driver
.Bitmap
= _swrast_Bitmap
;
266 ctx
->Driver
.CopyPixels
= _swrast_CopyPixels
;
267 ctx
->Driver
.DrawPixels
= _swrast_DrawPixels
;
268 ctx
->Driver
.ReadPixels
= _swrast_ReadPixels
;
269 ctx
->Driver
.DrawBuffer
= _swrast_DrawBuffer
;
271 /* Fill in the swrast driver interface:
273 swdd
->SetBuffer
= set_buffer
;
275 switch (SVGABuffer
.Depth
) {
276 case 8: ctx
->Driver
.ClearIndex
= __clear_index8
;
277 ctx
->Driver
.Clear
= __clear8
;
279 swdd
->ReadCI32Span
= __read_ci32_span8
;
280 swdd
->ReadCI32Pixels
= __read_ci32_pixels8
;
281 swdd
->WriteCI8Span
= __write_ci8_span8
;
282 swdd
->WriteCI32Span
= __write_ci32_span8
;
283 swdd
->WriteCI32Pixels
= __write_ci32_pixels8
;
284 swdd
->WriteMonoCISpan
= __write_mono_ci_span8
;
285 swdd
->WriteMonoCIPixels
= __write_mono_ci_pixels8
;
287 SVGAlog("SVGAUpdateState: 8 bit mode.");
291 case 15: ctx
->Driver
.ClearColor
= __clear_color15
;
292 ctx
->Driver
.Clear
= __clear15
;
294 swdd
->ReadRGBASpan
= __read_rgba_span15
;
295 swdd
->ReadRGBAPixels
= __read_rgba_pixels15
;
296 swdd
->WriteRGBASpan
= __write_rgba_span15
;
297 swdd
->WriteRGBAPixels
= __write_rgba_pixels15
;
298 swdd
->WriteMonoRGBASpan
= __write_mono_rgba_span15
;
299 swdd
->WriteMonoRGBAPixels
= __write_mono_rgba_pixels15
;
301 SVGAlog("SVGAUpdateState: 15 bit mode.");
304 case 16: ctx
->Driver
.ClearColor
= __clear_color16
;
305 ctx
->Driver
.Clear
= __clear16
;
307 swdd
->ReadRGBASpan
= __read_rgba_span16
;
308 swdd
->ReadRGBAPixels
= __read_rgba_pixels16
;
309 swdd
->WriteRGBASpan
= __write_rgba_span16
;
310 swdd
->WriteRGBAPixels
= __write_rgba_pixels16
;
311 swdd
->WriteMonoRGBASpan
= __write_mono_rgba_span16
;
312 swdd
->WriteMonoRGBAPixels
= __write_mono_rgba_pixels16
;
315 SVGAlog("SVGAUpdateState: 16 bit mode.");
317 case 24: ctx
->Driver
.ClearColor
= __clear_color24
;
318 ctx
->Driver
.Clear
= __clear24
;
320 swdd
->ReadRGBASpan
= __read_rgba_span24
;
321 swdd
->ReadRGBAPixels
= __read_rgba_pixels24
;
322 swdd
->WriteRGBASpan
= __write_rgba_span24
;
323 swdd
->WriteRGBAPixels
= __write_rgba_pixels24
;
324 swdd
->WriteMonoRGBASpan
= __write_mono_rgba_span24
;
325 swdd
->WriteMonoRGBAPixels
= __write_mono_rgba_pixels24
;
328 SVGAlog("SVGAUpdateState: 32 bit mode.");
330 case 32: ctx
->Driver
.ClearColor
= __clear_color32
;
331 ctx
->Driver
.Clear
= __clear32
;
333 swdd
->ReadRGBASpan
= __read_rgba_span32
;
334 swdd
->ReadRGBAPixels
= __read_rgba_pixels32
;
335 swdd
->WriteRGBASpan
= __write_rgba_span32
;
336 swdd
->WriteRGBAPixels
= __write_rgba_pixels32
;
337 swdd
->WriteMonoRGBASpan
= __write_mono_rgba_span32
;
338 swdd
->WriteMonoRGBAPixels
= __write_mono_rgba_pixels32
;
343 * Create a new VGA/Mesa context and return a handle to it.
345 SVGAMesaContext
SVGAMesaCreateContext( GLboolean doubleBuffer
)
350 GLfloat redscale
, greenscale
, bluescale
, alphascale
;
352 GLint redbits
, greenbits
, bluebits
, alphabits
;
354 /* determine if we're in RGB or color index mode */
355 if ((SVGABuffer
.Depth
==32) || (SVGABuffer
.Depth
==24)) {
357 redscale
= greenscale
= bluescale
= alphascale
= 255.0;
358 redbits
= greenbits
= bluebits
= 8;
362 else if (SVGABuffer
.Depth
==8) {
364 redscale
= greenscale
= bluescale
= alphascale
= 0.0;
365 redbits
= greenbits
= bluebits
= alphabits
= 0;
368 else if (SVGABuffer
.Depth
==15) {
370 redscale
= greenscale
= bluescale
= alphascale
= 31.0;
371 redbits
= greenbits
= bluebits
= 5;
375 else if (SVGABuffer
.Depth
==16) {
377 redscale
= bluescale
= alphascale
= 31.0;
379 redbits
= bluebits
= 5;
385 ctx
= (SVGAMesaContext
) calloc( 1, sizeof(struct svgamesa_context
) );
390 ctx
->gl_vis
= _mesa_create_visual( rgb_flag
,
392 GL_FALSE
, /* stereo */
397 8, /* stencil_size */
398 16, 16, 16, 16, /* accum_size */
402 ctx
->gl_ctx
= _mesa_create_context( ctx
->gl_vis
,
403 NULL
, /* share list context */
404 (void *) ctx
, GL_FALSE
);
406 _mesa_enable_sw_extensions(ctx
->gl_ctx
);
407 _mesa_enable_1_3_extensions(ctx
->gl_ctx
);
409 ctx
->gl_buffer
= _mesa_create_framebuffer( ctx
->gl_vis
,
410 ctx
->gl_vis
->depthBits
> 0,
411 ctx
->gl_vis
->stencilBits
> 0,
412 ctx
->gl_vis
->accumRedBits
> 0,
413 ctx
->gl_vis
->alphaBits
> 0 );
415 ctx
->width
= ctx
->height
= 0; /* temporary until first "make-current" */
421 * Destroy the given VGA/Mesa context.
423 void SVGAMesaDestroyContext( SVGAMesaContext ctx
)
427 _mesa_destroy_visual( ctx
->gl_vis
);
428 _mesa_destroy_context( ctx
->gl_ctx
);
429 _mesa_destroy_framebuffer( ctx
->gl_buffer
);
439 * Make the specified VGA/Mesa context the current one.
441 void SVGAMesaMakeCurrent( SVGAMesaContext ctx
)
445 svgamesa_update_state( ctx
->gl_ctx
, ~0 );
446 _mesa_make_current( ctx
->gl_ctx
, ctx
->gl_buffer
);
448 if (ctx
->width
==0 || ctx
->height
==0) {
449 /* setup initial viewport */
450 ctx
->width
= vga_getxdim();
451 ctx
->height
= vga_getydim();
452 _mesa_set_viewport( ctx
->gl_ctx
, 0, 0, ctx
->width
, ctx
->height
);
458 * Return a handle to the current VGA/Mesa context.
460 SVGAMesaContext
SVGAMesaGetCurrentContext( void )
466 * Swap front/back buffers for current context if double buffered.
468 void SVGAMesaSwapBuffers( void )
474 /* vga_waitretrace(); */
475 copy_buffer(SVGABuffer
.BackBuffer
);
478 _mesa_notifySwapBuffers( SVGAMesa
->gl_ctx
);
479 if (SVGAMesa
->gl_vis
->doubleBufferMode
)
483 sprintf(cbuf
,"SVGAMesaSwapBuffers : Swapping...");
485 #endif /* SVGA_DEBUG */
487 tmpptr
=SVGABuffer
.BackBuffer
;
488 SVGABuffer
.BackBuffer
=SVGABuffer
.FrontBuffer
;
489 SVGABuffer
.FrontBuffer
=tmpptr
;
492 sprintf(cbuf
,"SVGAMesaSwapBuffers : WriteBuffer : %p\n"
493 " Readbuffer : %p", \
494 SVGABuffer
.BackBuffer
, SVGABuffer
.FrontBuffer
);
496 #endif /* SVGA_DEBUG */
503 * Need this to provide at least one external definition when SVGA is
504 * not defined on the compiler command line.
507 int gl_svga_dummy_function(void)