Small compile fix
[mesa.git] / src / mesa / drivers / svga / svgamesa.c
1 /* $Id: svgamesa.c,v 1.7 2000/11/14 17:40:14 brianp Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 3.3
6 * Copyright (C) 1995-2000 Brian Paul
7 *
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.
12 *
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.
17 *
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.
21 */
22
23
24 /*
25 * SVGA driver for Mesa.
26 * Original author: Brian Paul
27 * Additional authors: Slawomir Szczyrba <steev@hot.pl> (Mesa 3.2)
28 */
29
30
31 #ifdef HAVE_CONFIG_H
32 #include "conf.h"
33 #endif
34
35 #ifdef SVGA
36
37 #ifdef PC_HEADER
38 #include "all.h"
39 #else
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <vga.h>
43 #include "GL/svgamesa.h"
44 #include "context.h"
45 #include "matrix.h"
46 #include "types.h"
47 #include <string.h>
48 #endif
49
50 #include "svgapix.h"
51 #include "svgamesa8.h"
52 #include "svgamesa15.h"
53 #include "svgamesa16.h"
54 #include "svgamesa24.h"
55 #include "svgamesa32.h"
56
57 struct svga_buffer SVGABuffer;
58 vga_modeinfo * SVGAInfo;
59 SVGAMesaContext SVGAMesa; /* the current context */
60
61 #ifdef SVGA_DEBUG
62
63 #include <sys/types.h>
64 #include <signal.h>
65
66 FILE * logfile;
67 char cbuf[1024]={0};
68
69 void SVGAlog(char * what)
70 {
71 logfile=fopen("svgamesa.log","a");
72 if (!logfile) return;
73 fprintf(logfile,"%s\n",what);
74 fclose(logfile);
75 }
76 #endif
77
78 /**********************************************************************/
79 /***** Init stuff... *****/
80 /**********************************************************************/
81
82 int SVGAMesaInit( int GraphMode )
83 {
84 vga_init();
85 if (!vga_hasmode(GraphMode))
86 {
87 fprintf(stderr,"GraphMode %d unavailable...",GraphMode);
88 #ifdef SVGA_DEBUG
89 SVGAlog("SVGAMesaInit: invalid GraphMode (doesn't exist)");
90 #endif
91 return(1);
92 }
93 SVGAInfo=vga_getmodeinfo(GraphMode);
94 if (SVGAInfo->flags & IS_MODEX)
95 {
96 fprintf(stderr,"ModeX not implemented...");
97 #ifdef SVGA_DEBUG
98 SVGAlog("SVGAMesaInit: invalid GraphMode (ModeX)");
99 #endif
100 return(2);
101 }
102 if (!SVGAInfo->bytesperpixel)
103 {
104 fprintf(stderr,"1 / 4 bit color not implemented...");
105 #ifdef SVGA_DEBUG
106 SVGAlog("SVGAMesaInit: invalid GraphMode (1 or 4 bit)");
107 #endif
108 return(3);
109 }
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;
115 }
116 SVGABuffer.BufferSize=SVGAInfo->linewidth*SVGAInfo->height;
117 #ifdef SVGA_DEBUG
118 sprintf(cbuf,"SVGAMesaInit: double buffer info.\n" \
119 " depth : %d\n" \
120 " mode : %d\n" \
121 " width : %d\n" \
122 " height : %d\n" \
123 " bufsize: %d\n", \
124 SVGABuffer.Depth,GraphMode,SVGAInfo->linewidth, \
125 SVGAInfo->height,SVGABuffer.BufferSize);
126 SVGAlog(cbuf);
127 #endif
128 SVGABuffer.FrontBuffer=(void*)malloc(SVGABuffer.BufferSize + 4);
129 if (!SVGABuffer.FrontBuffer) {
130 {
131 fprintf(stderr,"Not enough RAM for FRONT_LEFT_BUFFER...");
132 #ifdef SVGA_DEBUG
133 SVGAlog("SVGAMesaInit: Not enough RAM (front buffer)");
134 #endif
135 return(4);
136 }
137 }
138 #ifdef SVGA_DEBUG
139 sprintf(cbuf,"SVGAMesaInit: FrontBuffer - %p",SVGABuffer.FrontBuffer);
140 SVGAlog(cbuf);
141 #endif
142 SVGABuffer.BackBuffer=(void*)malloc(SVGABuffer.BufferSize + 4);
143 if (!SVGABuffer.BackBuffer) {
144 {
145 free(SVGABuffer.FrontBuffer);
146 fprintf(stderr,"Not enough RAM for BACK_LEFT_BUFFER...");
147 #ifdef SVGA_DEBUG
148 SVGAlog("SVGAMesaInit: Not enough RAM (back buffer)");
149 #endif
150 return(5);
151 }
152 }
153 #ifdef SVGA_DEBUG
154 sprintf(cbuf,"SVGAMesaInit: BackBuffer - %p",SVGABuffer.BackBuffer);
155 SVGAlog(cbuf);
156 #endif
157
158 vga_setmode(GraphMode);
159 SVGABuffer.VideoRam=vga_getgraphmem();
160 #ifdef SVGA_DEBUG
161 sprintf(cbuf,"SVGAMesaInit: VRAM - %p",SVGABuffer.VideoRam);
162 SVGAlog(cbuf);
163 sprintf(cbuf,"SVGAMesaInit: done. (Mode %d)",GraphMode);
164 SVGAlog(cbuf);
165 #endif
166
167 SVGABuffer.DrawBuffer = SVGABuffer.BackBuffer;
168 SVGABuffer.ReadBuffer = SVGABuffer.BackBuffer;
169
170 return 0;
171 }
172
173 int SVGAMesaClose( void )
174 {
175 vga_setmode(TEXT);
176 free(SVGABuffer.FrontBuffer);
177 free(SVGABuffer.BackBuffer);
178 return 0;
179 }
180
181 void SVGAMesaSetCI(int ndx, GLubyte red, GLubyte green, GLubyte blue)
182 {
183 if (ndx<256)
184 vga_setpalette(ndx, red>>2, green>>2, blue>>2);
185 }
186
187 /**********************************************************************/
188 /***** Miscellaneous functions *****/
189 /**********************************************************************/
190
191 static void copy_buffer( const GLubyte * buffer) {
192 int size = SVGABuffer.BufferSize, page = 0;
193
194 #ifdef SVGA_DEBUG
195 sprintf(cbuf,"copy_buffer: copy %p to %p",buffer,SVGABuffer.VideoRam);
196 SVGAlog(cbuf);
197 #endif
198
199 while(size>0) {
200 vga_setpage(page++);
201 if (size>>16) {
202 memcpy(SVGABuffer.VideoRam,buffer,0x10000);
203 buffer+=0x10000;
204 }else{
205 memcpy(SVGABuffer.VideoRam,buffer,size & 0xffff);
206 }
207 size-=0xffff;
208 }
209 }
210
211 static void get_buffer_size( GLcontext *ctx, GLuint *width, GLuint *height )
212 {
213 *width = SVGAMesa->width = vga_getxdim();
214 *height = SVGAMesa->height = vga_getydim();
215 }
216
217 static GLboolean set_draw_buffer( GLcontext *ctx, GLenum buffer )
218 {
219 if (buffer == GL_FRONT_LEFT) {
220 SVGABuffer.DrawBuffer = SVGABuffer.FrontBuffer;
221 #if 0
222 /* vga_waitretrace(); */
223 void * tmpptr;
224 copy_buffer(SVGABuffer.FrontBuffer);
225 tmpptr=SVGABuffer.BackBuffer;
226 SVGABuffer.BackBuffer=SVGABuffer.FrontBuffer;
227 SVGABuffer.FrontBuffer=tmpptr;
228 #endif
229 return GL_TRUE;
230 }
231 else if (buffer == GL_BACK_LEFT) {
232 SVGABuffer.DrawBuffer = SVGABuffer.BackBuffer;
233 #if 0
234 /* vga_waitretrace(); */
235 copy_buffer(SVGABuffer.BackBuffer);
236 #endif
237 return GL_TRUE;
238 }
239 else
240 return GL_FALSE;
241 }
242
243
244 static void set_read_buffer( GLcontext *ctx, GLframebuffer *colorBuffer,
245 GLenum buffer )
246 {
247 /* We can ignore colorBuffer since we don't support a MakeCurrentRead()
248 * function.
249 */
250 (void) colorBuffer;
251
252 if (buffer == GL_FRONT_LEFT) {
253 SVGABuffer.ReadBuffer = SVGABuffer.FrontBuffer;
254 #if 0
255 void * tmpptr;
256 /* vga_waitretrace(); */
257 copy_buffer(SVGABuffer.FrontBuffer);
258 tmpptr=SVGABuffer.BackBuffer;
259 SVGABuffer.BackBuffer=SVGABuffer.FrontBuffer;
260 SVGABuffer.FrontBuffer=tmpptr;
261 #endif
262 }
263 else if (buffer == GL_BACK_LEFT) {
264 SVGABuffer.ReadBuffer = SVGABuffer.BackBuffer;
265 #if 0
266 /* vga_waitretrace(); */
267 copy_buffer(SVGABuffer.BackBuffer);
268 #endif
269 }
270 }
271
272 /**********************************************************************/
273 /***** *****/
274 /**********************************************************************/
275
276 static void svgamesa_update_state( GLcontext *ctx )
277 {
278 /* Initialize all the pointers in the DD struct. Do this whenever */
279 /* a new context is made current or we change buffers via set_buffer! */
280
281 ctx->Driver.UpdateState = svgamesa_update_state;
282
283 ctx->Driver.GetBufferSize = get_buffer_size;
284 ctx->Driver.SetDrawBuffer = set_draw_buffer;
285 ctx->Driver.SetReadBuffer = set_read_buffer;
286
287 ctx->Driver.PointsFunc = NULL;
288 ctx->Driver.LineFunc = NULL;
289 ctx->Driver.TriangleFunc = NULL;
290
291 switch (SVGABuffer.Depth) {
292 case 8: ctx->Driver.ClearIndex = __clear_index8;
293 ctx->Driver.Clear = __clear8;
294
295 ctx->Driver.ReadCI32Span = __read_ci32_span8;
296 ctx->Driver.ReadCI32Pixels = __read_ci32_pixels8;
297 ctx->Driver.WriteCI8Span = __write_ci8_span8;
298 ctx->Driver.WriteCI32Span = __write_ci32_span8;
299 ctx->Driver.WriteCI32Pixels = __write_ci32_pixels8;
300 ctx->Driver.WriteMonoCISpan = __write_mono_ci_span8;
301 ctx->Driver.WriteMonoCIPixels = __write_mono_ci_pixels8;
302 #ifdef SVGA_DEBUG
303 SVGAlog("SVGAUpdateState: 8 bit mode.");
304 #endif
305
306 break;
307 case 15: ctx->Driver.ClearColor = __clear_color15;
308 ctx->Driver.Clear = __clear15;
309
310 ctx->Driver.ReadRGBASpan = __read_rgba_span15;
311 ctx->Driver.ReadRGBAPixels = __read_rgba_pixels15;
312 ctx->Driver.WriteRGBASpan = __write_rgba_span15;
313 ctx->Driver.WriteRGBAPixels = __write_rgba_pixels15;
314 ctx->Driver.WriteMonoRGBASpan = __write_mono_rgba_span15;
315 ctx->Driver.WriteMonoRGBAPixels = __write_mono_rgba_pixels15;
316 #ifdef SVGA_DEBUG
317 SVGAlog("SVGAUpdateState: 15 bit mode.");
318 #endif
319 break;
320 case 16: ctx->Driver.ClearColor = __clear_color16;
321 ctx->Driver.Clear = __clear16;
322
323 ctx->Driver.ReadRGBASpan = __read_rgba_span16;
324 ctx->Driver.ReadRGBAPixels = __read_rgba_pixels16;
325 ctx->Driver.WriteRGBASpan = __write_rgba_span16;
326 ctx->Driver.WriteRGBAPixels = __write_rgba_pixels16;
327 ctx->Driver.WriteMonoRGBASpan = __write_mono_rgba_span16;
328 ctx->Driver.WriteMonoRGBAPixels = __write_mono_rgba_pixels16;
329 break;
330 #ifdef SVGA_DEBUG
331 SVGAlog("SVGAUpdateState: 16 bit mode.");
332 #endif
333 case 24: ctx->Driver.ClearColor = __clear_color24;
334 ctx->Driver.Clear = __clear24;
335
336 ctx->Driver.ReadRGBASpan = __read_rgba_span24;
337 ctx->Driver.ReadRGBAPixels = __read_rgba_pixels24;
338 ctx->Driver.WriteRGBASpan = __write_rgba_span24;
339 ctx->Driver.WriteRGBAPixels = __write_rgba_pixels24;
340 ctx->Driver.WriteMonoRGBASpan = __write_mono_rgba_span24;
341 ctx->Driver.WriteMonoRGBAPixels = __write_mono_rgba_pixels24;
342 break;
343 #ifdef SVGA_DEBUG
344 SVGAlog("SVGAUpdateState: 32 bit mode.");
345 #endif
346 case 32: ctx->Driver.ClearColor = __clear_color32;
347 ctx->Driver.Clear = __clear32;
348
349 ctx->Driver.ReadRGBASpan = __read_rgba_span32;
350 ctx->Driver.ReadRGBAPixels = __read_rgba_pixels32;
351 ctx->Driver.WriteRGBASpan = __write_rgba_span32;
352 ctx->Driver.WriteRGBAPixels = __write_rgba_pixels32;
353 ctx->Driver.WriteMonoRGBASpan = __write_mono_rgba_span32;
354 ctx->Driver.WriteMonoRGBAPixels = __write_mono_rgba_pixels32;
355 }
356 }
357
358 /*
359 * Create a new VGA/Mesa context and return a handle to it.
360 */
361 SVGAMesaContext SVGAMesaCreateContext( GLboolean doubleBuffer )
362 {
363 SVGAMesaContext ctx;
364 #ifndef DEV
365 GLboolean rgb_flag;
366 GLfloat redscale, greenscale, bluescale, alphascale;
367 GLint index_bits;
368 GLint redbits, greenbits, bluebits, alphabits;
369 /* determine if we're in RGB or color index mode */
370 if ((SVGABuffer.Depth==32) || (SVGABuffer.Depth==24)) {
371 rgb_flag = GL_TRUE;
372 redscale = greenscale = bluescale = alphascale = 255.0;
373 redbits = greenbits = bluebits = 8;
374 alphabits = 0;
375 index_bits = 0;
376 }
377 else if (SVGABuffer.Depth==8) {
378 rgb_flag = GL_FALSE;
379 redscale = greenscale = bluescale = alphascale = 0.0;
380 redbits = greenbits = bluebits = alphabits = 0;
381 index_bits = 8;
382 }
383 else if (SVGABuffer.Depth==15) {
384 rgb_flag = GL_TRUE;
385 redscale = greenscale = bluescale = alphascale = 31.0;
386 redbits = greenbits = bluebits = 5;
387 alphabits = 0;
388 index_bits = 0;
389 }
390 else if (SVGABuffer.Depth==16) {
391 rgb_flag = GL_TRUE;
392 redscale = bluescale = alphascale = 31.0;
393 greenscale = 63.0;
394 redbits = bluebits = 5;
395 greenbits = 6;
396 alphabits = 0;
397 index_bits = 0;
398 }
399
400 ctx = (SVGAMesaContext) calloc( 1, sizeof(struct svgamesa_context) );
401 if (!ctx) {
402 return NULL;
403 }
404
405 ctx->gl_vis = _mesa_create_visual( rgb_flag,
406 doubleBuffer,
407 GL_FALSE, /* stereo */
408 redbits, greenbits,
409 bluebits, alphabits,
410 index_bits,
411 16, /* depth_size */
412 8, /* stencil_size */
413 16, 16, 16, 16, /* accum_size */
414 1 /* samples */
415 );
416
417 ctx->gl_ctx = _mesa_create_context( ctx->gl_vis,
418 NULL, /* share list context */
419 (void *) ctx, GL_TRUE );
420
421 ctx->gl_buffer = _mesa_create_framebuffer( ctx->gl_vis,
422 ctx->gl_vis->DepthBits > 0,
423 ctx->gl_vis->StencilBits > 0,
424 ctx->gl_vis->AccumRedBits > 0,
425 ctx->gl_vis->AlphaBits > 0 );
426
427 ctx->width = ctx->height = 0; /* temporary until first "make-current" */
428 #endif
429 return ctx;
430 }
431
432 /*
433 * Destroy the given VGA/Mesa context.
434 */
435 void SVGAMesaDestroyContext( SVGAMesaContext ctx )
436 {
437 #ifndef DEV
438 if (ctx) {
439 _mesa_destroy_visual( ctx->gl_vis );
440 _mesa_destroy_context( ctx->gl_ctx );
441 _mesa_destroy_framebuffer( ctx->gl_buffer );
442 free( ctx );
443 if (ctx==SVGAMesa) {
444 SVGAMesa = NULL;
445 }
446 }
447 #endif
448 }
449
450 /*
451 * Make the specified VGA/Mesa context the current one.
452 */
453 void SVGAMesaMakeCurrent( SVGAMesaContext ctx )
454 {
455 #ifndef DEV
456 SVGAMesa = ctx;
457 svgamesa_update_state( ctx->gl_ctx );
458 _mesa_make_current( ctx->gl_ctx, ctx->gl_buffer );
459
460 if (ctx->width==0 || ctx->height==0) {
461 /* setup initial viewport */
462 ctx->width = vga_getxdim();
463 ctx->height = vga_getydim();
464 gl_Viewport( ctx->gl_ctx, 0, 0, ctx->width, ctx->height );
465 }
466 #endif
467 }
468
469 /*
470 * Return a handle to the current VGA/Mesa context.
471 */
472 SVGAMesaContext SVGAMesaGetCurrentContext( void )
473 {
474 return SVGAMesa;
475 }
476
477 /*
478 * Swap front/back buffers for current context if double buffered.
479 */
480 void SVGAMesaSwapBuffers( void )
481 {
482 #if 000
483 void * tmpptr;
484 #endif
485
486 /* vga_waitretrace(); */
487 copy_buffer(SVGABuffer.BackBuffer);
488
489 #ifndef DEV
490 FLUSH_VB( SVGAMesa->gl_ctx, "swap buffers" );
491 if (SVGAMesa->gl_vis->DBflag)
492 #endif /* DEV */
493 {
494 #ifdef SVGA_DEBUG
495 sprintf(cbuf,"SVGAMesaSwapBuffers : Swapping...");
496 SVGAlog(cbuf);
497 #endif /* SVGA_DEBUG */
498 #if 000
499 tmpptr=SVGABuffer.BackBuffer;
500 SVGABuffer.BackBuffer=SVGABuffer.FrontBuffer;
501 SVGABuffer.FrontBuffer=tmpptr;
502 #endif
503 #ifdef SVGA_DEBUG
504 sprintf(cbuf,"SVGAMesaSwapBuffers : WriteBuffer : %p\n"
505 " Readbuffer : %p", \
506 SVGABuffer.BackBuffer, SVGABuffer.FrontBuffer );
507 SVGAlog(cbuf);
508 #endif /* SVGA_DEBUG */
509 }
510 }
511
512 #else /*SVGA*/
513
514 /*
515 * Need this to provide at least one external definition when SVGA is
516 * not defined on the compiler command line.
517 */
518
519 int gl_svga_dummy_function(void)
520 {
521 return 0;
522 }
523
524 #endif /*SVGA*/
525