3 * Copyright (C) 1997-1998 Uwe Maurer - uwe_maurer@t-online.de
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * ---------------------------------------------------------------------
20 * This code was derived from the following source of information:
22 * svgamesa.c and ddsample.c by Brian Paul
30 #include <ggi/mesa/ggimesa_int.h>
31 #include <ggi/mesa/debug.h>
32 #include "extensions.h"
35 #include "swrast/swrast.h"
36 #include "swrast_setup/swrast_setup.h"
38 #include "tnl/t_context.h"
39 #include "tnl/t_pipeline.h"
40 #include "array_cache/acache.h"
41 #include "texformat.h"
44 ggi_extid ggiMesaID
= -1;
45 static int _ggimesaLibIsUp
= 0;
46 static void *_ggimesaConfigHandle
;
48 static char ggimesaconffile
[] = GGIMESACONFFILE
;
50 int _ggimesaDebugSync
= 0;
51 uint32 _ggimesaDebugState
= 0;
53 static void gl_ggiUpdateState(GLcontext
*ctx
, GLuint new_state
);
54 static int changed(ggi_visual_t vis
, int whatchanged
);
57 static int _ggi_error(void)
59 GGIMESADPRINT_CORE("_ggi_error() called\n");
64 static void gl_ggiGetSize(GLframebuffer
*fb
, GLuint
*width
, GLuint
*height
)
66 /* FIXME: this is a hack to work around the new interface */
68 ggi_mesa_context_t ggi_ctx
;
69 ctx
= _mesa_get_current_context();
70 ggi_ctx
= (ggi_mesa_context_t
)ctx
->DriverCtx
;
72 GGIMESADPRINT_CORE("gl_ggiGetSize() called\n");
74 *width
= LIBGGI_MODE(ggi_ctx
->ggi_visual
)->visible
.x
;
75 *height
= LIBGGI_MODE(ggi_ctx
->ggi_visual
)->visible
.y
;
76 printf("returning %d, %d\n", *width
, *height
);
79 static void gl_ggiSetIndex(GLcontext
*ctx
, GLuint ci
)
81 ggi_mesa_context_t ggi_ctx
= (ggi_mesa_context_t
)ctx
->DriverCtx
;
83 GGIMESADPRINT_CORE("gl_ggiSetIndex() called\n");
85 ggiSetGCForeground(ggi_ctx
->ggi_visual
, ci
);
86 ggi_ctx
->color
= (ggi_pixel
)ci
;
89 static void gl_ggiSetClearIndex(GLcontext
*ctx
, GLuint ci
)
91 ggi_mesa_context_t ggi_ctx
= (ggi_mesa_context_t
)ctx
->DriverCtx
;
93 GGIMESADPRINT_CORE("gl_ggiSetClearIndex() called\n");
95 ggiSetGCForeground(ggi_ctx
->ggi_visual
, ci
);
96 ggi_ctx
->clearcolor
= (ggi_pixel
)ci
;
99 static void gl_ggiSetClearColor(GLcontext
*ctx
, const GLfloat color
[4])
101 ggi_mesa_context_t ggi_ctx
= (ggi_mesa_context_t
)ctx
->DriverCtx
;
104 GLubyte byteColor
[3];
106 GGIMESADPRINT_CORE("gl_ggiSetClearColor() called\n");
108 CLAMPED_FLOAT_TO_UBYTE(byteColor
[0], color
[0]);
109 CLAMPED_FLOAT_TO_UBYTE(byteColor
[1], color
[1]);
110 CLAMPED_FLOAT_TO_UBYTE(byteColor
[2], color
[2]);
112 rgb
.r
= (uint16
)byteColor
[0] << SHIFT
;
113 rgb
.g
= (uint16
)byteColor
[1] << SHIFT
;
114 rgb
.b
= (uint16
)byteColor
[2] << SHIFT
;
115 col
= ggiMapColor(ggi_ctx
->ggi_visual
, &rgb
);
116 ggiSetGCForeground(ggi_ctx
->ggi_visual
, col
);
117 ggi_ctx
->clearcolor
= col
;
120 static void gl_ggiClear(GLcontext
*ctx
, GLbitfield mask
, GLboolean all
,
121 GLint x
, GLint y
, GLint width
, GLint height
)
123 ggi_mesa_context_t ggi_ctx
= (ggi_mesa_context_t
)ctx
->DriverCtx
;
125 GGIMESADPRINT_CORE("gl_ggiClear() called\n");
127 if (mask
& (DD_FRONT_LEFT_BIT
| DD_BACK_LEFT_BIT
))
129 ggiSetGCForeground(ggi_ctx
->ggi_visual
, ggi_ctx
->clearcolor
);
134 w
= LIBGGI_MODE(ggi_ctx
->ggi_visual
)->visible
.x
;
135 h
= LIBGGI_MODE(ggi_ctx
->ggi_visual
)->visible
.y
;
136 ggiDrawBox(ggi_ctx
->ggi_visual
, 0, 0, w
, h
);
140 ggiDrawBox(ggi_ctx
->ggi_visual
, x
, y
, //FLIP(y),
143 ggiSetGCForeground(ggi_ctx
->ggi_visual
, ggi_ctx
->color
);
145 mask
&= ~(DD_FRONT_LEFT_BIT
| DD_BACK_LEFT_BIT
);
147 _swrast_Clear(ctx
, mask
, all
, x
, y
, width
, height
);
152 /* Set the buffer used for reading */
153 /* XXX support for separate read/draw buffers hasn't been tested */
154 static GLboolean
gl_ggiSetBuffer(GLcontext
*ctx
, GLframebuffer
*buffer
, GLuint bufferBit
)
156 ggi_mesa_context_t ggi_ctx
= (ggi_mesa_context_t
)ctx
->DriverCtx
;
158 printf("set read %d\n", mode
);
159 GGIMESADPRINT_CORE("gl_ggiSetBuffer() called\n");
161 if (bufferBit
== FRONT_LEFT_BIT
)
163 ggiSetReadFrame(ggi_ctx
->ggi_visual
,
164 ggiGetDisplayFrame(ggi_ctx
->ggi_visual
));
165 ggiSetWriteFrame(ggi_ctx
->ggi_visual
,
166 ggiGetDisplayFrame(ggi_ctx
->ggi_visual
));
169 else if (bufferBit
== BACK_LEFT_BIT
)
171 ggiSetReadFrame(ggi_ctx
->ggi_visual
,
172 ggiGetDisplayFrame(ggi_ctx
->ggi_visual
)?0 : 1);
173 ggiSetWriteFrame(ggi_ctx
->ggi_visual
,
174 ggiGetDisplayFrame(ggi_ctx
->ggi_visual
)?0 : 1);
182 static const GLubyte
* gl_ggiGetString(GLcontext
*ctx
, GLenum name
)
184 GGIMESADPRINT_CORE("gl_ggiGetString() called\n");
186 if (name
== GL_RENDERER
)
187 return (GLubyte
*) "Mesa GGI";
192 static void gl_ggiFlush(GLcontext
*ctx
)
194 ggi_mesa_context_t ggi_ctx
= (ggi_mesa_context_t
)ctx
->DriverCtx
;
196 GGIMESADPRINT_CORE("gl_ggiFlush() called\n");
198 ggiFlush(ggi_ctx
->ggi_visual
);
201 static void gl_ggiIndexMask(GLcontext
*ctx
, GLuint mask
)
203 GGIMESADPRINT_CORE("gl_ggiIndexMask() called\n");
206 static void gl_ggiColorMask(GLcontext
*ctx
, GLboolean rmask
, GLboolean gmask
,
207 GLboolean bmask
, GLboolean amask
)
209 GGIMESADPRINT_CORE("gl_ggiColorMask() called\n");
212 static void gl_ggiEnable(GLcontext
*ctx
, GLenum pname
, GLboolean state
)
214 GGIMESADPRINT_CORE("gl_ggiEnable() called\n");
217 static void gl_ggiSetupPointers(GLcontext
*ctx
)
221 GGIMESADPRINT_CORE("gl_ggiSetupPointers() called\n");
223 /* General information */
224 ctx
->Driver
.GetString
= gl_ggiGetString
;
225 ctx
->Driver
.GetBufferSize
= gl_ggiGetSize
;
226 ctx
->Driver
.Finish
= gl_ggiFlush
;
227 ctx
->Driver
.Flush
= gl_ggiFlush
;
229 /* Software rasterizer pixel paths */
230 ctx
->Driver
.Accum
= _swrast_Accum
;
231 ctx
->Driver
.Bitmap
= _swrast_Bitmap
;
232 ctx
->Driver
.Clear
= gl_ggiClear
;
233 ctx
->Driver
.ResizeBuffers
= _swrast_alloc_buffers
;
234 ctx
->Driver
.CopyPixels
= _swrast_CopyPixels
;
235 ctx
->Driver
.DrawPixels
= _swrast_DrawPixels
;
236 ctx
->Driver
.ReadPixels
= _swrast_ReadPixels
;
237 ctx
->Driver
.DrawBuffer
= _swrast_DrawBuffer
;
239 /* Software texturing */
240 ctx
->Driver
.ChooseTextureFormat
= _mesa_choose_tex_format
;
241 ctx
->Driver
.TexImage1D
= _mesa_store_teximage1d
;
242 ctx
->Driver
.TexImage2D
= _mesa_store_teximage2d
;
243 ctx
->Driver
.TexImage3D
= _mesa_store_teximage3d
;
244 ctx
->Driver
.TexSubImage1D
= _mesa_store_texsubimage1d
;
245 ctx
->Driver
.TexSubImage2D
= _mesa_store_texsubimage2d
;
246 ctx
->Driver
.TexSubImage3D
= _mesa_store_texsubimage3d
;
247 ctx
->Driver
.TestProxyTexImage
= _mesa_test_proxy_teximage
;
249 ctx
->Driver
.CompressedTexImage1D
= _mesa_store_compressed_teximage1d
;
250 ctx
->Driver
.CompressedTexImage2D
= _mesa_store_compressed_teximage2d
;
251 ctx
->Driver
.CompressedTexImage3D
= _mesa_store_compressed_teximage3d
;
252 ctx
->Driver
.CompressedTexSubImage1D
= _mesa_store_compressed_texsubimage1d
;
253 ctx
->Driver
.CompressedTexSubImage2D
= _mesa_store_compressed_texsubimage2d
;
254 ctx
->Driver
.CompressedTexSubImage3D
= _mesa_store_compressed_texsubimage3d
;
256 ctx
->Driver
.CopyTexImage1D
= _swrast_copy_teximage1d
;
257 ctx
->Driver
.CopyTexImage2D
= _swrast_copy_teximage2d
;
258 ctx
->Driver
.CopyTexSubImage1D
= _swrast_copy_texsubimage1d
;
259 ctx
->Driver
.CopyTexSubImage2D
= _swrast_copy_texsubimage2d
;
260 ctx
->Driver
.CopyTexSubImage3D
= _swrast_copy_texsubimage3d
;
262 /* Imaging extensions */
263 ctx
->Driver
.CopyColorTable
= _swrast_CopyColorTable
;
264 ctx
->Driver
.CopyColorSubTable
= _swrast_CopyColorSubTable
;
265 ctx
->Driver
.CopyConvolutionFilter1D
= _swrast_CopyConvolutionFilter1D
;
266 ctx
->Driver
.CopyConvolutionFilter2D
= _swrast_CopyConvolutionFilter2D
;
268 /* State change callbacks */
269 ctx
->Driver
.ClearIndex
= gl_ggiSetClearIndex
;
270 ctx
->Driver
.ClearColor
= gl_ggiSetClearColor
;
271 ctx
->Driver
.IndexMask
= gl_ggiIndexMask
;
272 ctx
->Driver
.ColorMask
= gl_ggiColorMask
;
273 ctx
->Driver
.Enable
= gl_ggiEnable
;
275 ctx
->Driver
.UpdateState
= gl_ggiUpdateState
;
277 /* Initialize TNL driver interface */
278 tnl
= TNL_CONTEXT(ctx
);
279 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
281 /* Install setup for tnl */
282 _swsetup_Wakeup(ctx
);
285 static void get_mode_info(ggi_visual_t vis
, int *r
, int *g
, int *b
,
286 GLboolean
*rgb
, GLboolean
*db
, int *ci
)
294 for(i
= 0; i
< sizeof(ggi_pixel
)*8; ++i
){
296 if(LIBGGI_PIXFMT(vis
)->red_mask
& mask
)
298 if(LIBGGI_PIXFMT(vis
)->green_mask
& mask
)
300 if(LIBGGI_PIXFMT(vis
)->blue_mask
& mask
)
304 *rgb
= GT_SCHEME(LIBGGI_MODE(vis
)->graphtype
) == GT_TRUECOLOR
;
305 *db
= LIBGGI_MODE(vis
)->frames
> 1;
306 *ci
= GT_SIZE(LIBGGI_MODE(vis
)->graphtype
);
308 printf("rgb (%d, %d, %d) db %d, rgb %d ci %d\n",*r
,*g
,*b
,*db
,*rgb
,*ci
);
316 GGIMESADPRINT_CORE("ggiMesaInit() called\n");
318 str
= getenv("GGIMESA_DEBUG");
320 _ggimesaDebugState
= atoi(str
);
321 GGIMESADPRINT_CORE("Debugging=%d\n", _ggimesaDebugState
);
324 str
= getenv("GGIMESA_DEBUGSYNC");
326 _ggimesaDebugSync
= 1;
329 GGIMESADPRINT_CORE("ggiMesaInit()\n");
332 if (_ggimesaLibIsUp
> 1)
333 return 0; /* Initialize only at first call */
335 err
= ggLoadConfig(ggimesaconffile
, &_ggimesaConfigHandle
);
338 GGIMESADPRINT_CORE("GGIMesa: Couldn't open %s\n",
344 ggiMesaID
= ggiExtensionRegister("GGIMesa",
345 sizeof(struct ggi_mesa_ext
), changed
);
349 GGIMESADPRINT_CORE("GGIMesa: failed to register as extension\n");
351 ggFreeConfig(_ggimesaConfigHandle
);
358 int ggiMesaExit(void)
362 GGIMESADPRINT_CORE("ggiMesaExit() called\n");
364 if (!_ggimesaLibIsUp
)
367 if (_ggimesaLibIsUp
> 1)
369 /* Exit only at last call */
374 rc
= ggiExtensionUnregister(ggiMesaID
);
375 ggFreeConfig(_ggimesaConfigHandle
);
382 int ggiMesaAttach(ggi_visual_t vis
)
386 GGIMESADPRINT_CORE("ggiMesaAttach() called\n");
388 rc
= ggiExtensionAttach(vis
, ggiMesaID
);
394 GLframebuffer
*gl_fb
;
396 /* We are creating the primary instance */
397 memset(LIBGGI_MESAEXT(vis
), 0, sizeof(struct ggi_mesa_ext
));
398 LIBGGI_MESAEXT(vis
)->update_state
= (void *)_ggi_error
;
399 LIBGGI_MESAEXT(vis
)->setup_driver
= (void *)_ggi_error
;
401 /* Initialize default mesa visual */
402 get_mode_info(vis
, &r
, &g
, &b
, &rgb
, &db
, &ci
);
403 gl_visual
= &(LIBGGI_MESAEXT(vis
)->mesa_visual
.gl_visual
);
404 _mesa_initialize_visual(gl_visual
,
405 rgb
, db
, 0 /* No stereo */,
406 r
, g
, b
, 0 /* No alpha */, ci
,
407 0 /* No depth */, 0 /* No stencil */,
408 0, 0, 0, 0 /* No accum */, 0);
410 /* Now fake an "API change" so the right libs get loaded */
411 changed(vis
, GGI_CHG_APILIST
);
417 int ggiMesaDetach(ggi_visual_t vis
)
419 GGIMESADPRINT_CORE("ggiMesaDetach() called\n");
421 return ggiExtensionDetach(vis
, ggiMesaID
);
424 int ggiMesaExtendVisual(ggi_visual_t vis
, GLboolean alpha_flag
,
425 GLboolean stereo_flag
, GLint depth_size
,
426 GLint stencil_size
, GLint accum_red_size
,
427 GLint accum_green_size
, GLint accum_blue_size
,
428 GLint accum_alpha_size
, GLint num_samples
)
430 GLvisual
*gl_vis
= &(LIBGGI_MESAEXT(vis
)->mesa_visual
.gl_visual
);
434 get_mode_info(vis
, &r
, &g
, &b
, &rgb
, &db
, &ci
);
436 /* Initialize the visual with the provided information */
437 _mesa_initialize_visual(&(LIBGGI_MESAEXT(vis
)->mesa_visual
.gl_visual
),
438 rgb
, db
, stereo_flag
,
439 r
, g
, b
, 0 /* FIXME */, ci
,
440 depth_size
, stencil_size
,
441 accum_red_size
, accum_green_size
,
442 accum_blue_size
, accum_alpha_size
, 0);
444 /* Now fake an "API change" so the right libs get loaded. After all,
445 extending the visual by all these new buffers could be considered
446 a "mode change" which requires an "API change".
448 changed(vis
, GGI_CHG_APILIST
);
454 ggi_mesa_context_t
ggiMesaCreateContext(ggi_visual_t vis
)
456 ggi_mesa_context_t ctx
;
461 GGIMESADPRINT_CORE("ggiMesaCreateContext() called\n");
463 ctx
= (ggi_mesa_context_t
)malloc(sizeof(struct ggi_mesa_context
));
467 ctx
->ggi_visual
= vis
;
471 _mesa_create_context(&(LIBGGI_MESAEXT(vis
)->mesa_visual
.gl_visual
),
472 NULL
, (void *) ctx
, GL_FALSE
);
476 _mesa_enable_sw_extensions(ctx
->gl_ctx
);
478 _swrast_CreateContext(ctx
->gl_ctx
);
479 _ac_CreateContext(ctx
->gl_ctx
);
480 _tnl_CreateContext(ctx
->gl_ctx
);
481 _swsetup_CreateContext(ctx
->gl_ctx
);
483 gl_ggiSetupPointers(ctx
->gl_ctx
);
485 /* Make sure that an appropriate sublib has been loaded */
486 if (!LIBGGI_MESAEXT(ctx
->ggi_visual
)->setup_driver
){
487 GGIMESADPRINT_CORE("setup_driver==NULL!\n");
488 GGIMESADPRINT_CORE("Please check your config files!\n");
492 /* Set up the sublib driver */
493 err
= LIBGGI_MESAEXT(ctx
->ggi_visual
)->setup_driver(ctx
);
495 GGIMESADPRINT_CORE("setup_driver failed (err = %d)", err
);
496 goto free_gl_context
;
502 _mesa_destroy_context(ctx
->gl_ctx
);
509 void ggiMesaDestroyContext(ggi_mesa_context_t ctx
)
511 GGIMESADPRINT_CORE("ggiMesaDestroyContext() called\n");
516 _mesa_destroy_context(ctx
->gl_ctx
);
520 void ggiMesaMakeCurrent(ggi_mesa_context_t ctx
, ggi_visual_t vis
)
522 GGIMESADPRINT_CORE("ggiMesaMakeCurrent(ctx = %p) called\n", ctx
);
524 /* FIXME: clean up where are ggi_vis */
525 if (ctx
->ggi_visual
!= vis
) {
526 GGIMESADPRINT_CORE("Cannot migrate GL contexts\n");
530 _mesa_make_current(ctx
->gl_ctx
, &LIBGGI_MESAEXT(vis
)->mesa_buffer
);
532 if (ctx
->gl_ctx
->Viewport
.Width
== 0)
535 LIBGGI_MODE(vis
)->visible
.x
,
536 LIBGGI_MODE(vis
)->visible
.y
);
537 ctx
->gl_ctx
->Scissor
.Width
= LIBGGI_MODE(vis
)->visible
.x
;
538 ctx
->gl_ctx
->Scissor
.Height
= LIBGGI_MODE(vis
)->visible
.y
;
544 * Swap front/back buffers for current context if double buffered.
546 void ggiMesaSwapBuffers(void)
549 ggi_mesa_context_t ggi_ctx
;
550 ctx
= _mesa_get_current_context();
551 ggi_ctx
= (ggi_mesa_context_t
)ctx
->DriverCtx
;
553 GGIMESADPRINT_CORE("ggiMesaSwapBuffers() called\n");
555 _mesa_notifySwapBuffers(ctx
);
558 ggiSetDisplayFrame(ggi_ctx
->ggi_visual
,
559 !ggiGetDisplayFrame(ggi_ctx
->ggi_visual
));
560 ggiSetWriteFrame(ggi_ctx
->ggi_visual
,
561 !ggiGetWriteFrame(ggi_ctx
->ggi_visual
));
562 ggiSetReadFrame(ggi_ctx
->ggi_visual
,
563 !ggiGetReadFrame(ggi_ctx
->ggi_visual
));
565 GGIMESADPRINT_CORE("swap disp: %d, write %d\n",
566 ggiGetDisplayFrame(ggi_ctx
->ggi_visual
),
567 ggiGetWriteFrame(ggi_ctx
->ggi_visual
));
570 static void gl_ggiUpdateState(GLcontext
*ctx
, GLuint new_state
)
572 ggi_mesa_context_t ggi_ctx
= (ggi_mesa_context_t
)ctx
->DriverCtx
;
574 GGIMESADPRINT_CORE("gl_ggiUpdateState() called\n");
576 /* Propogate statechange information to swrast and swrast_setup
577 * modules. The GGI driver has no internal GL-dependent state.
579 _swrast_InvalidateState(ctx
, new_state
);
580 _swsetup_InvalidateState(ctx
, new_state
);
581 _tnl_InvalidateState(ctx
, new_state
);
583 if (!LIBGGI_MESAEXT(ggi_ctx
->ggi_visual
)->update_state
) {
584 GGIMESADPRINT_CORE("update_state == NULL!\n");
585 GGIMESADPRINT_CORE("Please check your config files!\n");
589 LIBGGI_MESAEXT(ggi_ctx
->ggi_visual
)->update_state(ggi_ctx
);
592 static int changed(ggi_visual_t vis
, int whatchanged
)
595 ctx
= _mesa_get_current_context();
597 GGIMESADPRINT_CORE("changed() called\n");
601 case GGI_CHG_APILIST
:
608 GLvisual
*gl_vis
=&(LIBGGI_MESAEXT(vis
)->mesa_visual
.gl_visual
);
609 GLframebuffer
*gl_fb
= &(LIBGGI_MESAEXT(vis
)->mesa_buffer
);
611 /* Initialize the framebuffer to provide all necessary
612 buffers in software. The target libraries that are loaded
613 next are free to modify this according to their
616 /* FIXME: if the target changes capabilities we'll leak
617 swrast's memory !!! Need to deallocate first */
618 _mesa_initialize_framebuffer(gl_fb
, gl_vis
,
619 gl_vis
->depthBits
> 0,
620 gl_vis
->stencilBits
> 0,
621 gl_vis
->accumRedBits
> 0,
622 gl_vis
->alphaBits
> 0);
624 for (i
= 0; ggiGetAPI(vis
, i
, api
, args
) == 0; i
++)
626 strcat(api
, "-mesa");
627 fname
= ggMatchConfig(_ggimesaConfigHandle
, api
, NULL
);
630 /* No special implementation for this sublib */
633 lib
= ggiExtensionLoadDL(vis
, fname
, args
, NULL
,
637 /* The targets have cleared everything they can do from
638 the framebuffer structure so we provide the rest in sw
640 _swrast_alloc_buffers(gl_fb
);