new texture compression infrastructure
[mesa.git] / src / mesa / drivers / dos / dmesa.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 4.0
4 *
5 * Copyright (C) 1999 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /*
26 * DOS/DJGPP device driver v1.1 for Mesa 4.0
27 *
28 * Copyright (C) 2002 - Borca Daniel
29 * Email : dborca@yahoo.com
30 * Web : http://www.geocities.com/dborca
31 */
32
33
34 #ifdef PC_HEADER
35 #include "all.h"
36 #else
37 #include "glheader.h"
38 #include "context.h"
39 #include "GL/dmesa.h"
40 #include "extensions.h"
41 #include "macros.h"
42 #include "matrix.h"
43 #include "mmath.h"
44 #include "texformat.h"
45 #include "texstore.h"
46 #include "array_cache/acache.h"
47 #include "swrast/s_context.h"
48 #include "swrast/s_depth.h"
49 #include "swrast/s_lines.h"
50 #include "swrast/s_triangle.h"
51 #include "swrast/s_trispan.h"
52 #include "swrast/swrast.h"
53 #include "swrast_setup/swrast_setup.h"
54 #include "tnl/tnl.h"
55 #include "tnl/t_context.h"
56 #include "tnl/t_pipeline.h"
57 #endif
58
59 #include "video.h"
60
61
62
63 /*
64 * In C++ terms, this class derives from the GLvisual class.
65 * Add system-specific fields to it.
66 */
67 struct dmesa_visual {
68 GLvisual *gl_visual;
69 GLboolean db_flag; /* double buffered? */
70 GLboolean rgb_flag; /* RGB mode? */
71 GLuint depth; /* bits per pixel (1, 8, 24, etc) */
72 };
73
74 /*
75 * In C++ terms, this class derives from the GLframebuffer class.
76 * Add system-specific fields to it.
77 */
78 struct dmesa_buffer {
79 GLframebuffer gl_buffer; /* The depth, stencil, accum, etc buffers */
80 void *the_window; /* your window handle, etc */
81
82 int xpos, ypos; /* position */
83 int width, height; /* size in pixels */
84 int bypp, stride, bytes; /* bytes per pixel, in a line, then total */
85 };
86
87 /*
88 * In C++ terms, this class derives from the GLcontext class.
89 * Add system-specific fields to it.
90 */
91 struct dmesa_context {
92 GLcontext *gl_ctx; /* the core library context */
93 DMesaVisual visual;
94 DMesaBuffer Buffer;
95 GLuint ClearColor;
96 /* etc... */
97 };
98
99
100
101 static void dmesa_update_state (GLcontext *ctx, GLuint new_state);
102
103
104
105 /**********************************************************************/
106 /***** Read/Write pixels *****/
107 /**********************************************************************/
108
109
110
111 #define FLIP(y) (c->Buffer->height - (y) - 1)
112 #define FLIP2(y) (h - (y) - 1)
113
114
115
116 static void write_rgba_span (const GLcontext *ctx, GLuint n, GLint x, GLint y,
117 const GLubyte rgba[][4], const GLubyte mask[])
118 {
119 DMesaContext c = (DMesaContext)ctx->DriverCtx;
120 void *b = c->Buffer->the_window;
121 GLuint i, offset;
122
123 offset = c->Buffer->width * FLIP(y) + x;
124 if (mask) {
125 /* draw some pixels */
126 for (i=0; i<n; i++, offset++) {
127 if (mask[i]) {
128 vl_putpixel(b, offset, vl_mixrgba(rgba[i]));
129 }
130 }
131 } else {
132 /* draw all pixels */
133 for (i=0; i<n; i++, offset++) {
134 vl_putpixel(b, offset, vl_mixrgba(rgba[i]));
135 }
136 }
137 }
138
139 static void write_rgb_span (const GLcontext *ctx, GLuint n, GLint x, GLint y,
140 const GLubyte rgb[][3], const GLubyte mask[])
141 {
142 DMesaContext c = (DMesaContext)ctx->DriverCtx;
143 void *b = c->Buffer->the_window;
144 GLuint i, offset;
145
146 offset = c->Buffer->width * FLIP(y) + x;
147 if (mask) {
148 /* draw some pixels */
149 for (i=0; i<n; i++, offset++) {
150 if (mask[i]) {
151 vl_putpixel(b, offset, vl_mixrgb(rgb[i]));
152 }
153 }
154 } else {
155 /* draw all pixels */
156 for (i=0; i<n; i++, offset++) {
157 vl_putpixel(b, offset, vl_mixrgb(rgb[i]));
158 }
159 }
160 }
161
162 static void write_mono_rgba_span (const GLcontext *ctx,
163 GLuint n, GLint x, GLint y,
164 const GLchan color[4], const GLubyte mask[])
165 {
166 DMesaContext c = (DMesaContext)ctx->DriverCtx;
167 void *b = c->Buffer->the_window;
168 GLuint i, offset, rgba = vl_mixrgba(color);
169
170 offset = c->Buffer->width * FLIP(y) + x;
171 if (mask) {
172 /* draw some pixels */
173 for (i=0; i<n; i++, offset++) {
174 if (mask[i]) {
175 vl_putpixel(b, offset, rgba);
176 }
177 }
178 } else {
179 /* draw all pixels */
180 for (i=0; i<n; i++, offset++) {
181 vl_putpixel(b, offset, rgba);
182 }
183 }
184 }
185
186 static void read_rgba_span (const GLcontext *ctx, GLuint n, GLint x, GLint y,
187 GLubyte rgba[][4])
188 {
189 DMesaContext c = (DMesaContext)ctx->DriverCtx;
190 void *b = c->Buffer->the_window;
191 GLuint i, offset;
192
193 offset = c->Buffer->width * FLIP(y) + x;
194 /* read all pixels */
195 for (i=0; i<n; i++, offset++) {
196 vl_getrgba(b, offset, rgba[i]);
197 }
198 }
199
200 static void write_rgba_pixels (const GLcontext *ctx,
201 GLuint n, const GLint x[], const GLint y[],
202 const GLubyte rgba[][4], const GLubyte mask[])
203 {
204 DMesaContext c = (DMesaContext)ctx->DriverCtx;
205 void *b = c->Buffer->the_window;
206 GLuint i, w = c->Buffer->width, h = c->Buffer->height;
207
208 if (mask) {
209 /* draw some pixels */
210 for (i=0; i<n; i++) {
211 if (mask[i]) {
212 vl_putpixel(b, FLIP2(y[i])*w + x[i], vl_mixrgba(rgba[i]));
213 }
214 }
215 } else {
216 /* draw all pixels */
217 for (i=0; i<n; i++) {
218 vl_putpixel(b, FLIP2(y[i])*w + x[i], vl_mixrgba(rgba[i]));
219 }
220 }
221 }
222
223 static void write_mono_rgba_pixels (const GLcontext *ctx,
224 GLuint n, const GLint x[], const GLint y[],
225 const GLchan color[4], const GLubyte mask[])
226 {
227 DMesaContext c = (DMesaContext)ctx->DriverCtx;
228 void *b = c->Buffer->the_window;
229 GLuint i, w = c->Buffer->width, h = c->Buffer->height, rgba = vl_mixrgba(color);
230
231 if (mask) {
232 /* draw some pixels */
233 for (i=0; i<n; i++) {
234 if (mask[i]) {
235 vl_putpixel(b, FLIP2(y[i])*w + x[i], rgba);
236 }
237 }
238 } else {
239 /* draw all pixels */
240 for (i=0; i<n; i++) {
241 vl_putpixel(b, FLIP2(y[i])*w + x[i], rgba);
242 }
243 }
244 }
245
246 static void read_rgba_pixels (const GLcontext *ctx,
247 GLuint n, const GLint x[], const GLint y[],
248 GLubyte rgba[][4], const GLubyte mask[])
249 {
250 DMesaContext c = (DMesaContext)ctx->DriverCtx;
251 void *b = c->Buffer->the_window;
252 GLuint i, w = c->Buffer->width, h = c->Buffer->height;
253
254 if (mask) {
255 /* read some pixels */
256 for (i=0; i<n; i++) {
257 if (mask[i]) {
258 vl_getrgba(b, FLIP2(y[i])*w + x[i], rgba[i]);
259 }
260 }
261 } else {
262 /* read all pixels */
263 for (i=0; i<n; i++) {
264 vl_getrgba(b, FLIP2(y[i])*w + x[i], rgba[i]);
265 }
266 }
267 }
268
269
270
271 /**********************************************************************/
272 /***** Optimized triangle rendering *****/
273 /**********************************************************************/
274
275
276
277 /*
278 * flat, NON-depth-buffered, triangle.
279 */
280 static void tri_rgb_flat (GLcontext *ctx,
281 const SWvertex *v0,
282 const SWvertex *v1,
283 const SWvertex *v2)
284 {
285 DMesaContext c = (DMesaContext)ctx->DriverCtx;
286 void *b = c->Buffer->the_window;
287 GLuint w = c->Buffer->width, h = c->Buffer->height;
288
289 #define SETUP_CODE GLuint rgb = vl_mixrgb(v2->color);
290
291 #define RENDER_SPAN(span) \
292 GLuint i, offset = FLIP2(span.y)*w + span.x; \
293 for (i = 0; i < span.count; i++, offset++) { \
294 vl_putpixel(b, offset, rgb); \
295 }
296
297 #include "swrast/s_tritemp.h"
298 }
299
300
301
302 /*
303 * flat, depth-buffered, triangle.
304 */
305 static void tri_rgb_flat_z (GLcontext *ctx,
306 const SWvertex *v0,
307 const SWvertex *v1,
308 const SWvertex *v2)
309 {
310 DMesaContext c = (DMesaContext)ctx->DriverCtx;
311 void *b = c->Buffer->the_window;
312 GLuint w = c->Buffer->width, h = c->Buffer->height;
313
314 #define INTERP_Z 1
315 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
316 #define SETUP_CODE GLuint rgb = vl_mixrgb(v2->color);
317
318 #define RENDER_SPAN(span) \
319 GLuint i, offset = FLIP2(span.y)*w + span.x; \
320 for (i = 0; i < span.count; i++, offset++) { \
321 const DEPTH_TYPE z = FixedToDepth(span.z); \
322 if (z < zRow[i]) { \
323 vl_putpixel(b, offset, rgb); \
324 zRow[i] = z; \
325 } \
326 span.z += span.zStep; \
327 }
328
329 #include "swrast/s_tritemp.h"
330 }
331
332
333
334 /*
335 * smooth, NON-depth-buffered, triangle.
336 */
337 static void tri_rgb_smooth (GLcontext *ctx,
338 const SWvertex *v0,
339 const SWvertex *v1,
340 const SWvertex *v2)
341 {
342 DMesaContext c = (DMesaContext)ctx->DriverCtx;
343 void *b = c->Buffer->the_window;
344 GLuint w = c->Buffer->width, h = c->Buffer->height;
345
346 #define INTERP_RGB 1
347 #define RENDER_SPAN(span) \
348 GLuint i, offset = FLIP2(span.y)*w + span.x; \
349 for (i = 0; i < span.count; i++, offset++) { \
350 unsigned char rgb[3]; \
351 rgb[0] = FixedToInt(span.red); \
352 rgb[1] = FixedToInt(span.green); \
353 rgb[2] = FixedToInt(span.blue); \
354 vl_putpixel(b, offset, vl_mixrgb(rgb)); \
355 span.red += span.redStep; \
356 span.green += span.greenStep; \
357 span.blue += span.blueStep; \
358 }
359
360 #include "swrast/s_tritemp.h"
361 }
362
363
364
365 /*
366 * smooth, depth-buffered, triangle.
367 */
368 static void tri_rgb_smooth_z (GLcontext *ctx,
369 const SWvertex *v0,
370 const SWvertex *v1,
371 const SWvertex *v2)
372 {
373 DMesaContext c = (DMesaContext)ctx->DriverCtx;
374 void *b = c->Buffer->the_window;
375 GLuint w = c->Buffer->width, h = c->Buffer->height;
376
377 #define INTERP_Z 1
378 #define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
379 #define INTERP_RGB 1
380
381 #define RENDER_SPAN(span) \
382 GLuint i, offset = FLIP2(span.y)*w + span.x; \
383 for (i = 0; i < span.count; i++, offset++) { \
384 const DEPTH_TYPE z = FixedToDepth(span.z); \
385 if (z < zRow[i]) { \
386 unsigned char rgb[3]; \
387 rgb[0] = FixedToInt(span.red); \
388 rgb[1] = FixedToInt(span.green); \
389 rgb[2] = FixedToInt(span.blue); \
390 vl_putpixel(b, offset, vl_mixrgb(rgb)); \
391 zRow[i] = z; \
392 } \
393 span.red += span.redStep; \
394 span.green += span.greenStep; \
395 span.blue += span.blueStep; \
396 span.z += span.zStep; \
397 }
398
399 #include "swrast/s_tritemp.h"
400 }
401
402
403
404 /*
405 * Analyze context state to see if we can provide a fast triangle function
406 * Otherwise, return NULL.
407 */
408 static swrast_tri_func dmesa_choose_tri_function (GLcontext *ctx)
409 {
410 const SWcontext *swrast = SWRAST_CONTEXT(ctx);
411
412 if (ctx->RenderMode != GL_RENDER) return (swrast_tri_func) NULL;
413 if (ctx->Polygon.SmoothFlag) return (swrast_tri_func) NULL;
414 if (ctx->Texture._ReallyEnabled) return (swrast_tri_func) NULL;
415
416 if (ctx->Light.ShadeModel==GL_SMOOTH
417 && swrast->_RasterMask==DEPTH_BIT
418 && ctx->Depth.Func==GL_LESS
419 && ctx->Depth.Mask==GL_TRUE
420 && ctx->Visual.depthBits == DEFAULT_SOFTWARE_DEPTH_BITS
421 && ctx->Polygon.StippleFlag==GL_FALSE) {
422 return tri_rgb_smooth_z;
423 }
424 if (ctx->Light.ShadeModel==GL_FLAT
425 && swrast->_RasterMask==DEPTH_BIT
426 && ctx->Depth.Func==GL_LESS
427 && ctx->Depth.Mask==GL_TRUE
428 && ctx->Visual.depthBits == DEFAULT_SOFTWARE_DEPTH_BITS
429 && ctx->Polygon.StippleFlag==GL_FALSE) {
430 return tri_rgb_flat_z;
431 }
432 if (swrast->_RasterMask==0 /* no depth test */
433 && ctx->Light.ShadeModel==GL_SMOOTH
434 && ctx->Polygon.StippleFlag==GL_FALSE) {
435 return tri_rgb_smooth;
436 }
437 if (swrast->_RasterMask==0 /* no depth test */
438 && ctx->Light.ShadeModel==GL_FLAT
439 && ctx->Polygon.StippleFlag==GL_FALSE) {
440 return tri_rgb_flat;
441 }
442
443 return (swrast_tri_func)NULL;
444 }
445
446
447
448 /* Override for the swrast triangle-selection function. Try to use one
449 * of our internal line functions, otherwise fall back to the
450 * standard swrast functions.
451 */
452 static void dmesa_choose_tri (GLcontext *ctx)
453 {
454 SWcontext *swrast = SWRAST_CONTEXT(ctx);
455
456 if (!(swrast->Triangle=dmesa_choose_tri_function(ctx)))
457 _swrast_choose_triangle(ctx);
458 }
459
460
461
462 /**********************************************************************/
463 /***** Miscellaneous device driver funcs *****/
464 /**********************************************************************/
465
466
467
468 static void clear_color (GLcontext *ctx, const GLchan color[4])
469 {
470 DMesaContext c = (DMesaContext)ctx->DriverCtx;
471 c->ClearColor = vl_mixrgba(color);
472 }
473
474
475
476 static void clear (GLcontext *ctx, GLbitfield mask, GLboolean all,
477 GLint x, GLint y, GLint width, GLint height)
478 {
479 DMesaContext c = (DMesaContext)ctx->DriverCtx;
480 const GLuint *colorMask = (GLuint *)&ctx->Color.ColorMask;
481 DMesaBuffer b = c->Buffer;
482
483 /*
484 * Clear the specified region of the buffers indicated by 'mask'
485 * using the clear color or index as specified by one of the two
486 * functions above.
487 * If all==GL_TRUE, clear whole buffer, else just clear region defined
488 * by x,y,width,height
489 */
490
491 /* we can't handle color or index masking */
492 if (*colorMask==0xffffffff) {
493 if (mask & DD_BACK_LEFT_BIT) {
494 if (all) {
495 vl_clear(b->the_window, b->bytes, c->ClearColor);
496 } else {
497 vl_rect(b->the_window, x, y, width, height, c->ClearColor);
498 }
499 mask &= ~DD_BACK_LEFT_BIT;
500 }
501 }
502
503 if (mask) {
504 _swrast_Clear(ctx, mask, all, x, y, width, height);
505 }
506 }
507
508
509
510 /*
511 * Set the current reading buffer.
512 */
513 static void set_read_buffer (GLcontext *ctx, GLframebuffer *buffer,
514 GLenum mode)
515 {
516 /*
517 XXX this has to be fixed
518 */
519 }
520
521
522
523 /*
524 * Set the destination/draw buffer.
525 */
526 static void set_draw_buffer (GLcontext *ctx, GLenum mode)
527 {
528 /*
529 XXX this has to be fixed
530 */
531 }
532
533
534
535 /*
536 * Return the width and height of the current buffer.
537 * If anything special has to been done when the buffer/window is
538 * resized, do it now.
539 */
540 static void get_buffer_size (GLframebuffer *buffer, GLuint *width, GLuint *height)
541 {
542 DMesaBuffer b = (DMesaBuffer)buffer;
543
544 *width = b->width;
545 *height = b->height;
546 }
547
548
549
550 static const GLubyte* get_string (GLcontext *ctx, GLenum name)
551 {
552 switch (name) {
553 case GL_RENDERER:
554 return (const GLubyte *)"Mesa DJGPP\0port (c) Borca Daniel 3-sep-2002";
555 default:
556 return NULL;
557 }
558 }
559
560
561
562 /**********************************************************************/
563 /***** Miscellaneous device driver funcs *****/
564 /***** Note that these functions are mandatory *****/
565 /**********************************************************************/
566
567
568
569 /* OPTIONAL FUNCTION: implements glFinish if possible */
570 static void finish (GLcontext *ctx)
571 {
572 /*
573 DMesaContext c = (DMesaContext)ctx->DriverCtx;
574 */
575 }
576
577
578
579 /* OPTIONAL FUNCTION: implements glFlush if possible */
580 static void flush (GLcontext *ctx)
581 {
582 /*
583 DMesaContext c = (DMesaContext)ctx->DriverCtx;
584 */
585 }
586
587
588
589 /**********************************************************************/
590 /**********************************************************************/
591
592
593
594 #define DMESA_NEW_TRIANGLE (_NEW_POLYGON | \
595 _NEW_TEXTURE | \
596 _NEW_LIGHT | \
597 _NEW_DEPTH | \
598 _NEW_RENDERMODE | \
599 _SWRAST_NEW_RASTERMASK)
600
601
602
603 /* Extend the software rasterizer with our line and triangle
604 * functions.
605 */
606 static void dmesa_register_swrast_functions (GLcontext *ctx)
607 {
608 SWcontext *swrast = SWRAST_CONTEXT(ctx);
609
610 swrast->choose_triangle = dmesa_choose_tri;
611
612 swrast->invalidate_triangle |= DMESA_NEW_TRIANGLE;
613 }
614
615
616
617 /* Setup pointers and other driver state that is constant for the life
618 * of a context.
619 */
620 void dmesa_init_pointers (GLcontext *ctx)
621 {
622 TNLcontext *tnl;
623
624 ctx->Driver.UpdateState = dmesa_update_state;
625
626 ctx->Driver.GetString = get_string;
627 ctx->Driver.GetBufferSize = get_buffer_size;
628 ctx->Driver.Flush = flush;
629 ctx->Driver.Finish = finish;
630
631 /* Software rasterizer pixel paths:
632 */
633 ctx->Driver.Accum = _swrast_Accum;
634 ctx->Driver.Bitmap = _swrast_Bitmap;
635 ctx->Driver.Clear = clear;
636 ctx->Driver.ResizeBuffers = _swrast_alloc_buffers;
637 ctx->Driver.CopyPixels = _swrast_CopyPixels;
638 ctx->Driver.DrawPixels = _swrast_DrawPixels;
639 ctx->Driver.ReadPixels = _swrast_ReadPixels;
640
641 /* Software texture functions:
642 */
643 ctx->Driver.ChooseTextureFormat = _mesa_choose_tex_format;
644 ctx->Driver.TexImage1D = _mesa_store_teximage1d;
645 ctx->Driver.TexImage2D = _mesa_store_teximage2d;
646 ctx->Driver.TexImage3D = _mesa_store_teximage3d;
647 ctx->Driver.TexSubImage1D = _mesa_store_texsubimage1d;
648 ctx->Driver.TexSubImage2D = _mesa_store_texsubimage2d;
649 ctx->Driver.TexSubImage3D = _mesa_store_texsubimage3d;
650 ctx->Driver.TestProxyTexImage = _mesa_test_proxy_teximage;
651
652 ctx->Driver.CompressedTexImage1D = _mesa_store_compressed_teximage1d;
653 ctx->Driver.CompressedTexImage2D = _mesa_store_compressed_teximage2d;
654 ctx->Driver.CompressedTexImage3D = _mesa_store_compressed_teximage3d;
655 ctx->Driver.CompressedTexSubImage1D = _mesa_store_compressed_texsubimage1d;
656 ctx->Driver.CompressedTexSubImage2D = _mesa_store_compressed_texsubimage2d;
657 ctx->Driver.CompressedTexSubImage3D = _mesa_store_compressed_texsubimage3d;
658
659 ctx->Driver.CopyTexImage1D = _swrast_copy_teximage1d;
660 ctx->Driver.CopyTexImage2D = _swrast_copy_teximage2d;
661 ctx->Driver.CopyTexSubImage1D = _swrast_copy_texsubimage1d;
662 ctx->Driver.CopyTexSubImage2D = _swrast_copy_texsubimage2d;
663 ctx->Driver.CopyTexSubImage3D = _swrast_copy_texsubimage3d;
664
665 /* Swrast hooks for imaging extensions:
666 */
667 ctx->Driver.CopyColorTable = _swrast_CopyColorTable;
668 ctx->Driver.CopyColorSubTable = _swrast_CopyColorSubTable;
669 ctx->Driver.CopyConvolutionFilter1D = _swrast_CopyConvolutionFilter1D;
670 ctx->Driver.CopyConvolutionFilter2D = _swrast_CopyConvolutionFilter2D;
671
672 /* Statechange callbacks:
673 */
674 ctx->Driver.SetDrawBuffer = set_draw_buffer;
675 ctx->Driver.ClearColor = clear_color;
676
677 /* Initialize the TNL driver interface:
678 */
679 tnl = TNL_CONTEXT(ctx);
680 tnl->Driver.RunPipeline = _tnl_run_pipeline;
681
682 /* Install swsetup for tnl->Driver.Render.*:
683 */
684 _swsetup_Wakeup(ctx);
685 }
686
687
688
689 static void dmesa_update_state (GLcontext *ctx, GLuint new_state)
690 {
691 DMesaContext c = (DMesaContext)ctx->DriverCtx;
692 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
693
694 /* Initialize all the pointers in the DD struct. Do this whenever */
695 /* a new context is made current or we change buffers via set_buffer! */
696
697 _swrast_InvalidateState(ctx, new_state);
698 _swsetup_InvalidateState(ctx, new_state);
699 _ac_InvalidateState(ctx, new_state);
700 _tnl_InvalidateState(ctx, new_state);
701
702 swdd->SetReadBuffer = set_read_buffer;
703
704 /* RGB(A) span/pixel functions */
705 swdd->WriteRGBASpan = write_rgba_span;
706 swdd->WriteRGBSpan = write_rgb_span;
707 swdd->WriteMonoRGBASpan = write_mono_rgba_span;
708 swdd->WriteRGBAPixels = write_rgba_pixels;
709 swdd->WriteMonoRGBAPixels = write_mono_rgba_pixels;
710 swdd->ReadRGBASpan = read_rgba_span;
711 swdd->ReadRGBAPixels = read_rgba_pixels;
712 }
713
714
715
716 /**********************************************************************/
717 /***** DMesa Public API Functions *****/
718 /**********************************************************************/
719
720
721
722 /*
723 * The exact arguments to this function will depend on your window system
724 */
725 DMesaVisual DMesaCreateVisual (GLint width, GLint height, GLint colDepth,
726 GLboolean dbFlag, GLint depthSize,
727 GLint stencilSize,
728 GLint accumSize)
729 {
730 DMesaVisual v;
731 GLint redBits, greenBits, blueBits, alphaBits;
732
733 int refresh;
734 char *var = getenv("DMESA_REFRESH");
735 if ((var == NULL) || ((refresh=atoi(var)) == 0)) {
736 refresh = 60;
737 }
738
739 if (!dbFlag) {
740 return NULL;
741 }
742 alphaBits = 0;
743 switch (colDepth) {
744 case 15:
745 redBits = 5;
746 greenBits = 5;
747 blueBits = 5;
748 break;
749 case 16:
750 redBits = 5;
751 greenBits = 6;
752 blueBits = 5;
753 break;
754 case 32:
755 alphaBits = 8;
756 case 24:
757 redBits = 8;
758 greenBits = 8;
759 blueBits = 8;
760 break;
761 default:
762 return NULL;
763 }
764
765 if (vl_video_init(width, height, colDepth, refresh) != 0) {
766 return NULL;
767 }
768
769 if ((v=(DMesaVisual)calloc(1, sizeof(struct dmesa_visual)))!=NULL) {
770 /* Create core visual */
771 v->gl_visual = _mesa_create_visual(colDepth>8, /* rgb */
772 dbFlag,
773 GL_FALSE, /* stereo */
774 redBits,
775 greenBits,
776 blueBits,
777 alphaBits,
778 0, /* indexBits */
779 depthSize,
780 stencilSize,
781 accumSize, /* accumRed */
782 accumSize, /* accumGreen */
783 accumSize, /* accumBlue */
784 alphaBits?accumSize:0, /* accumAlpha */
785 1); /* numSamples */
786
787 v->depth = colDepth;
788 v->db_flag = dbFlag;
789 }
790
791 return v;
792 }
793
794
795
796 void DMesaDestroyVisual (DMesaVisual v)
797 {
798 vl_video_exit();
799 _mesa_destroy_visual(v->gl_visual);
800 free(v);
801 }
802
803
804
805 DMesaBuffer DMesaCreateBuffer (DMesaVisual visual,
806 GLint xpos, GLint ypos,
807 GLint width, GLint height)
808 {
809 DMesaBuffer b;
810
811 if ((b=(DMesaBuffer)calloc(1, sizeof(struct dmesa_buffer)))!=NULL) {
812
813 _mesa_initialize_framebuffer(&b->gl_buffer,
814 visual->gl_visual,
815 visual->gl_visual->depthBits > 0,
816 visual->gl_visual->stencilBits > 0,
817 visual->gl_visual->accumRedBits > 0,
818 visual->gl_visual->alphaBits > 0);
819 b->xpos = xpos;
820 b->ypos = ypos;
821 b->width = width;
822 b->height = height;
823 b->bypp = (visual->depth+7)/8;
824 }
825
826 return b;
827 }
828
829
830
831 void DMesaDestroyBuffer (DMesaBuffer b)
832 {
833 free(b->the_window);
834 _mesa_free_framebuffer_data(&b->gl_buffer);
835 free(b);
836 }
837
838
839
840 DMesaContext DMesaCreateContext (DMesaVisual visual,
841 DMesaContext share)
842 {
843 DMesaContext c;
844 GLboolean direct = GL_FALSE;
845
846 if ((c=(DMesaContext)calloc(1, sizeof(struct dmesa_context)))!=NULL) {
847 c->gl_ctx = _mesa_create_context(visual->gl_visual,
848 share ? share->gl_ctx : NULL,
849 (void *)c, direct);
850
851 _mesa_enable_sw_extensions(c->gl_ctx);
852 _mesa_enable_1_3_extensions(c->gl_ctx);
853
854 /* you probably have to do a bunch of other initializations here. */
855 c->visual = visual;
856
857 /* Initialize the software rasterizer and helper modules.
858 */
859 _swrast_CreateContext(c->gl_ctx);
860 _ac_CreateContext(c->gl_ctx);
861 _tnl_CreateContext(c->gl_ctx);
862 _swsetup_CreateContext(c->gl_ctx);
863 dmesa_init_pointers(c->gl_ctx);
864 dmesa_register_swrast_functions(c->gl_ctx);
865 }
866
867 return c;
868 }
869
870
871
872 void DMesaDestroyContext (DMesaContext c)
873 {
874 _mesa_destroy_context(c->gl_ctx);
875 free(c);
876 }
877
878
879
880 GLboolean DMesaViewport (DMesaBuffer b,
881 GLint xpos, GLint ypos,
882 GLint width, GLint height)
883 {
884 void *new_window;
885
886 if ((new_window=vl_sync_buffer(b->the_window, xpos, ypos, width, height))==NULL) {
887 return GL_FALSE;
888 } else {
889 b->the_window = new_window;
890 b->xpos = xpos;
891 b->ypos = ypos;
892 b->width = width;
893 b->height = height;
894 b->stride = width * b->bypp;
895 b->bytes = b->stride * height;
896 return GL_TRUE;
897 }
898 }
899
900
901
902 /*
903 * Make the specified context and buffer the current one.
904 */
905 GLboolean DMesaMakeCurrent (DMesaContext c, DMesaBuffer b)
906 {
907 if (c&&b) {
908 if (!DMesaViewport(b, b->xpos, b->ypos, b->width, b->height)) {
909 return GL_FALSE;
910 }
911
912 c->Buffer = b;
913
914 dmesa_update_state(c->gl_ctx, 0);
915 _mesa_make_current(c->gl_ctx, &b->gl_buffer);
916 if (c->gl_ctx->Viewport.Width==0) {
917 /* initialize viewport to window size */
918 _mesa_Viewport(0, 0, b->width, b->height);
919 }
920 } else {
921 /* Detach */
922 _mesa_make_current(NULL, NULL);
923 }
924
925 return GL_TRUE;
926 }
927
928
929
930 void DMesaSwapBuffers (DMesaBuffer b)
931 {
932 /* copy/swap back buffer to front if applicable */
933 GET_CURRENT_CONTEXT(ctx);
934 _mesa_swapbuffers(ctx);
935 vl_flip(b->the_window, b->stride, b->height);
936 }