Debian Codegraph @

debian/external_deps/fetk/sg/src/gl/svgamesa.c

raw
1 /* $Id: svgamesa.c,v 1.2 2000/10/27 15:21:40 mholst Exp $ */
2
3 /*
4 * Mesa 3-D graphics library
5 * Version: 2.0
6 * Copyright (C) 1995-1996 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 * Linux SVGA/Mesa interface.
25 *
26 * This interface is not finished! Still have to implement pixel
27 * reading functions and double buffering. Then, look into accelerated
28 * line and polygon rendering. And, clean up a bunch of other stuff.
29 * Any volunteers?
30 */
31
32
33 #ifdef SVGA
34
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <vga.h>
39 #include "GL/svgamesa.h"
40 #include "context.h"
41 #include "matrix.h"
42 #include "types.h"
43
44
45
46 struct svgamesa_context {
47 GLcontext *gl_ctx; /* the core Mesa context */
48 GLvisual *gl_vis; /* describes the color buffer */
49 GLframebuffer *gl_buffer; /* the ancillary buffers */
50 GLuint index; /* current color index */
51 GLint red, green, blue; /* current rgb color */
52 GLint width, height; /* size of color buffer */
53 GLint depth; /* bits per pixel (8,16,24 or 32) */
54 };
55
56
57 static SVGAMesaContext SVGAMesa = NULL; /* the current context */
58
59
60
61 /*
62 * Convert Mesa window Y coordinate to VGA screen Y coordinate:
63 */
64 #define FLIP(Y) (SVGAMesa->height-(Y)-1)
65
66
67
68 /**********************************************************************/
69 /***** Miscellaneous functions *****/
70 /**********************************************************************/
71
72
73 static void get_buffer_size( GLcontext *ctx, GLuint *width, GLuint *height )
74 {
75 *width = SVGAMesa->width = vga_getxdim();
76 *height = SVGAMesa->height = vga_getydim();
77 }
78
79
80 /* Set current color index */
81 static void set_index( GLcontext *ctx, GLuint index )
82 {
83 SVGAMesa->index = index;
84 vga_setcolor( index );
85 }
86
87
88 /* Set current drawing color */
89 static void set_color( GLcontext *ctx,
90 GLubyte red, GLubyte green,
91 GLubyte blue, GLubyte alpha )
92 {
93 SVGAMesa->red = red;
94 SVGAMesa->green = green;
95 SVGAMesa->blue = blue;
96 vga_setrgbcolor( red, green, blue );
97 }
98
99
100 static void clear_index( GLcontext *ctx, GLuint index )
101 {
102 /* TODO: Implements glClearIndex() */
103 }
104
105
106 static void clear_color( GLcontext *ctx,
107 GLubyte red, GLubyte green,
108 GLubyte blue, GLubyte alpha )
109 {
110 /* TODO: Implements glClearColor() */
111 }
112
113
114 static void clear( GLcontext *ctx,
115 GLboolean all,
116 GLint x, GLint y, GLint width, GLint height )
117 {
118 vga_clear();
119 }
120
121
122 static GLboolean set_buffer( GLcontext *ctx,
123 GLenum mode )
124 {
125 /* TODO: implement double buffering and use this function to select */
126 /* between front and back buffers. */
127 return GL_TRUE;
128 }
129
130
131
132
133 /**********************************************************************/
134 /***** Write spans of pixels *****/
135 /**********************************************************************/
136
137
138 static void write_index_span( GLcontext *ctx,
139 GLuint n, GLint x, GLint y,
140 const GLuint index[],
141 const GLubyte mask[] )
142 {
143 int i;
144 y = FLIP(y);
145 for (i=0;i<n;i++,x++) {
146 if (mask[i]) {
147 vga_setcolor( index[i] );
148 vga_drawpixel( x, y );
149 }
150 }
151 }
152
153
154
155 static void write_monoindex_span( GLcontext *ctx,
156 GLuint n, GLint x, GLint y,
157 const GLubyte mask[] )
158 {
159 int i;
160 y = FLIP(y);
161 /* use current color index */
162 vga_setcolor( SVGAMesa->index );
163 for (i=0;i<n;i++,x++) {
164 if (mask[i]) {
165 vga_drawpixel( x, y );
166 }
167 }
168 }
169
170
171
172 static void write_color_span( GLcontext *ctx,
173 GLuint n, GLint x, GLint y,
174 const GLubyte red[], const GLubyte green[],
175 const GLubyte blue[], const GLubyte alpha[],
176 const GLubyte mask[] )
177 {
178 int i;
179 y=FLIP(y);
180 if (mask) {
181 /* draw some pixels */
182 for (i=0; i<n; i++, x++) {
183 if (mask[i]) {
184 vga_setrgbcolor( red[i], green[i], blue[i] );
185 vga_drawpixel( x, y );
186 }
187 }
188 }
189 else {
190 /* draw all pixels */
191 for (i=0; i<n; i++, x++) {
192 vga_setrgbcolor( red[i], green[i], blue[i] );
193 vga_drawpixel( x, y );
194 }
195 }
196 }
197
198
199
200 static void write_monocolor_span( GLcontext *ctx,
201 GLuint n, GLint x, GLint y,
202 const GLubyte mask[])
203 {
204 int i;
205 y=FLIP(y);
206 /* use current rgb color */
207 vga_setrgbcolor( SVGAMesa->red, SVGAMesa->green, SVGAMesa->blue );
208 for (i=0; i<n; i++, x++) {
209 if (mask[i]) {
210 vga_drawpixel( x, y );
211 }
212 }
213 }
214
215
216
217 /**********************************************************************/
218 /***** Read spans of pixels *****/
219 /**********************************************************************/
220
221
222 static void read_index_span( GLcontext *ctx,
223 GLuint n, GLint x, GLint y, GLuint index[])
224 {
225 int i;
226 y = FLIP(y);
227 for (i=0; i<n; i++,x++) {
228 index[i] = vga_getpixel( x, y );
229 }
230 }
231
232
233
234 static void read_color_span( GLcontext *ctx,
235 GLuint n, GLint x, GLint y,
236 GLubyte red[], GLubyte green[],
237 GLubyte blue[], GLubyte alpha[] )
238 {
239 int i;
240 for (i=0; i<n; i++, x++) {
241 /* TODO */
242 }
243 }
244
245
246
247 /**********************************************************************/
248 /***** Write arrays of pixels *****/
249 /**********************************************************************/
250
251
252 static void write_index_pixels( GLcontext *ctx,
253 GLuint n, const GLint x[], const GLint y[],
254 const GLuint index[], const GLubyte mask[] )
255 {
256 int i;
257 for (i=0; i<n; i++) {
258 if (mask[i]) {
259 vga_setcolor( index[i] );
260 vga_drawpixel( x[i], FLIP(y[i]) );
261 }
262 }
263 }
264
265
266
267 static void write_monoindex_pixels( GLcontext *ctx,
268 GLuint n,
269 const GLint x[], const GLint y[],
270 const GLubyte mask[] )
271 {
272 int i;
273 /* use current color index */
274 vga_setcolor( SVGAMesa->index );
275 for (i=0; i<n; i++) {
276 if (mask[i]) {
277 vga_drawpixel( x[i], FLIP(y[i]) );
278 }
279 }
280 }
281
282
283
284 static void write_color_pixels( GLcontext *ctx,
285 GLuint n, const GLint x[], const GLint y[],
286 const GLubyte r[], const GLubyte g[],
287 const GLubyte b[], const GLubyte a[],
288 const GLubyte mask[] )
289 {
290 int i;
291 for (i=0; i<n; i++) {
292 if (mask[i]) {
293 vga_setrgbcolor( r[i], g[i], b[i] );
294 vga_drawpixel( x[i], FLIP(y[i]) );
295 }
296 }
297 }
298
299
300
301 static void write_monocolor_pixels( GLcontext *ctx,
302 GLuint n,
303 const GLint x[], const GLint y[],
304 const GLubyte mask[] )
305 {
306 int i;
307 /* use current rgb color */
308 vga_setrgbcolor( SVGAMesa->red, SVGAMesa->green, SVGAMesa->blue );
309 for (i=0; i<n; i++) {
310 if (mask[i]) {
311 vga_drawpixel( x[i], FLIP(y[i]) );
312 }
313 }
314 }
315
316
317
318
319 /**********************************************************************/
320 /***** Read arrays of pixels *****/
321 /**********************************************************************/
322
323 /* Read an array of color index pixels. */
324 static void read_index_pixels( GLcontext *ctx,
325 GLuint n, const GLint x[], const GLint y[],
326 GLuint index[], const GLubyte mask[] )
327 {
328 int i;
329 for (i=0; i<n; i++,x++) {
330 index[i] = vga_getpixel( x[i], FLIP(y[i]) );
331 }
332 }
333
334
335
336 static void read_color_pixels( GLcontext *ctx,
337 GLuint n, const GLint x[], const GLint y[],
338 GLubyte red[], GLubyte green[],
339 GLubyte blue[], GLubyte alpha[],
340 const GLubyte mask[] )
341 {
342 /* TODO */
343 }
344
345
346
347 static void svgamesa_setup_DD_pointers( GLcontext *ctx )
348 {
349 /* Initialize all the pointers in the DD struct. Do this whenever */
350 /* a new context is made current or we change buffers via set_buffer! */
351
352 ctx->Driver.UpdateState = svgamesa_setup_DD_pointers;
353
354 ctx->Driver.ClearIndex = clear_index;
355 ctx->Driver.ClearColor = clear_color;
356 ctx->Driver.Clear = clear;
357
358 ctx->Driver.Index = set_index;
359 ctx->Driver.Color = set_color;
360
361 ctx->Driver.SetBuffer = set_buffer;
362 ctx->Driver.GetBufferSize = get_buffer_size;
363
364 ctx->Driver.PointsFunc = NULL;
365 ctx->Driver.LineFunc = NULL;
366 ctx->Driver.TriangleFunc = NULL;
367
368 /* Pixel/span writing functions: */
369 /* TODO: use different funcs for 8, 16, 32-bit depths */
370 ctx->Driver.WriteColorSpan = write_color_span;
371 ctx->Driver.WriteMonocolorSpan = write_monocolor_span;
372 ctx->Driver.WriteColorPixels = write_color_pixels;
373 ctx->Driver.WriteMonocolorPixels = write_monocolor_pixels;
374 ctx->Driver.WriteIndexSpan = write_index_span;
375 ctx->Driver.WriteMonoindexSpan = write_monoindex_span;
376 ctx->Driver.WriteIndexPixels = write_index_pixels;
377 ctx->Driver.WriteMonoindexPixels = write_monoindex_pixels;
378
379 /* Pixel/span reading functions: */
380 /* TODO: use different funcs for 8, 16, 32-bit depths */
381 ctx->Driver.ReadIndexSpan = read_index_span;
382 ctx->Driver.ReadColorSpan = read_color_span;
383 ctx->Driver.ReadIndexPixels = read_index_pixels;
384 ctx->Driver.ReadColorPixels = read_color_pixels;
385 }
386
387
388
389 /*
390 * Create a new VGA/Mesa context and return a handle to it.
391 */
392 SVGAMesaContext SVGAMesaCreateContext( void )
393 {
394 SVGAMesaContext ctx;
395 GLboolean rgb_flag;
396 GLfloat redscale, greenscale, bluescale, alphascale;
397 GLboolean db_flag = GL_FALSE;
398 GLboolean alpha_flag = GL_FALSE;
399 int colors;
400 GLint index_bits;
401
402 /* determine if we're in RGB or color index mode */
403 colors = vga_getcolors();
404 if (colors==32768) {
405 rgb_flag = GL_TRUE;
406 redscale = greenscale = bluescale = alphascale = 255.0;
407 index_bits = 0;
408 }
409 else if (colors==256) {
410 rgb_flag = GL_FALSE;
411 redscale = greenscale = bluescale = alphascale = 0.0;
412 index_bits = 8;
413 }
414 else {
415 printf(">16 bit color not implemented yet!\n");
416 return NULL;
417 }
418
419 ctx = (SVGAMesaContext) calloc( 1, sizeof(struct svgamesa_context) );
420 if (!ctx) {
421 return NULL;
422 }
423
424 ctx->gl_vis = gl_create_visual( rgb_flag,
425 alpha_flag,
426 db_flag,
427 16, /* depth_size */
428 8, /* stencil_size */
429 16, /* accum_size */
430 index_bits,
431 redscale,
432 greenscale,
433 bluescale,
434 alphascale );
435
436 ctx->gl_ctx = gl_create_context( ctx->gl_vis,
437 NULL, /* share list context */
438 (void *) ctx
439 );
440
441 ctx->gl_buffer = gl_create_framebuffer( ctx->gl_vis );
442
443 ctx->index = 1;
444 ctx->red = ctx->green = ctx->blue = 255;
445
446 ctx->width = ctx->height = 0; /* temporary until first "make-current" */
447
448 return ctx;
449 }
450
451
452
453
454 /*
455 * Destroy the given VGA/Mesa context.
456 */
457 void SVGAMesaDestroyContext( SVGAMesaContext ctx )
458 {
459 if (ctx) {
460 gl_destroy_visual( ctx->gl_vis );
461 gl_destroy_context( ctx->gl_ctx );
462 gl_destroy_framebuffer( ctx->gl_buffer );
463 free( ctx );
464 if (ctx==SVGAMesa) {
465 SVGAMesa = NULL;
466 }
467 }
468 }
469
470
471
472 /*
473 * Make the specified VGA/Mesa context the current one.
474 */
475 void SVGAMesaMakeCurrent( SVGAMesaContext ctx )
476 {
477 SVGAMesa = ctx;
478 gl_make_current( ctx->gl_ctx, ctx->gl_buffer );
479 svgamesa_setup_DD_pointers( ctx->gl_ctx );
480
481 if (ctx->width==0 || ctx->height==0) {
482 /* setup initial viewport */
483 ctx->width = vga_getxdim();
484 ctx->height = vga_getydim();
485 gl_Viewport( ctx->gl_ctx, 0, 0, ctx->width, ctx->height );
486 }
487 }
488
489
490
491 /*
492 * Return a handle to the current VGA/Mesa context.
493 */
494 SVGAMesaContext SVGAMesaGetCurrentContext( void )
495 {
496 return SVGAMesa;
497 }
498
499
500 /*
501 * Swap front/back buffers for current context if double buffered.
502 */
503 void SVGAMesaSwapBuffers( void )
504 {
505 if (SVGAMesa->gl_vis->DBflag) {
506 vga_flip();
507 }
508 }
509
510
511 #else
512
513 /*
514 * Need this to provide at least one external definition when SVGA is
515 * not defined on the compiler command line.
516 */
517
518 int gl_svga_dummy_function(void)
519 {
520 return 0;
521 }
522
523 #endif /*SVGA*/
524