148d7402e2e452053157ea60e35fa299dc9f84b2
[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.CopyTexImage1D = _swrast_copy_teximage1d;
653 ctx->Driver.CopyTexImage2D = _swrast_copy_teximage2d;
654 ctx->Driver.CopyTexSubImage1D = _swrast_copy_texsubimage1d;
655 ctx->Driver.CopyTexSubImage2D = _swrast_copy_texsubimage2d;
656 ctx->Driver.CopyTexSubImage3D = _swrast_copy_texsubimage3d;
657
658 ctx->Driver.BaseCompressedTexFormat = _mesa_base_compressed_texformat;
659 ctx->Driver.CompressedTextureSize = _mesa_compressed_texture_size;
660 ctx->Driver.GetCompressedTexImage = _mesa_get_compressed_teximage;
661
662 /* Swrast hooks for imaging extensions:
663 */
664 ctx->Driver.CopyColorTable = _swrast_CopyColorTable;
665 ctx->Driver.CopyColorSubTable = _swrast_CopyColorSubTable;
666 ctx->Driver.CopyConvolutionFilter1D = _swrast_CopyConvolutionFilter1D;
667 ctx->Driver.CopyConvolutionFilter2D = _swrast_CopyConvolutionFilter2D;
668
669 /* Statechange callbacks:
670 */
671 ctx->Driver.SetDrawBuffer = set_draw_buffer;
672 ctx->Driver.ClearColor = clear_color;
673
674 /* Initialize the TNL driver interface:
675 */
676 tnl = TNL_CONTEXT(ctx);
677 tnl->Driver.RunPipeline = _tnl_run_pipeline;
678
679 /* Install swsetup for tnl->Driver.Render.*:
680 */
681 _swsetup_Wakeup(ctx);
682 }
683
684
685
686 static void dmesa_update_state (GLcontext *ctx, GLuint new_state)
687 {
688 DMesaContext c = (DMesaContext)ctx->DriverCtx;
689 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
690
691 /* Initialize all the pointers in the DD struct. Do this whenever */
692 /* a new context is made current or we change buffers via set_buffer! */
693
694 _swrast_InvalidateState(ctx, new_state);
695 _swsetup_InvalidateState(ctx, new_state);
696 _ac_InvalidateState(ctx, new_state);
697 _tnl_InvalidateState(ctx, new_state);
698
699 swdd->SetReadBuffer = set_read_buffer;
700
701 /* RGB(A) span/pixel functions */
702 swdd->WriteRGBASpan = write_rgba_span;
703 swdd->WriteRGBSpan = write_rgb_span;
704 swdd->WriteMonoRGBASpan = write_mono_rgba_span;
705 swdd->WriteRGBAPixels = write_rgba_pixels;
706 swdd->WriteMonoRGBAPixels = write_mono_rgba_pixels;
707 swdd->ReadRGBASpan = read_rgba_span;
708 swdd->ReadRGBAPixels = read_rgba_pixels;
709 }
710
711
712
713 /**********************************************************************/
714 /***** DMesa Public API Functions *****/
715 /**********************************************************************/
716
717
718
719 /*
720 * The exact arguments to this function will depend on your window system
721 */
722 DMesaVisual DMesaCreateVisual (GLint width, GLint height, GLint colDepth,
723 GLboolean dbFlag, GLint depthSize,
724 GLint stencilSize,
725 GLint accumSize)
726 {
727 DMesaVisual v;
728 GLint redBits, greenBits, blueBits, alphaBits;
729
730 int refresh;
731 char *var = getenv("DMESA_REFRESH");
732 if ((var == NULL) || ((refresh=atoi(var)) == 0)) {
733 refresh = 60;
734 }
735
736 if (!dbFlag) {
737 return NULL;
738 }
739 alphaBits = 0;
740 switch (colDepth) {
741 case 15:
742 redBits = 5;
743 greenBits = 5;
744 blueBits = 5;
745 break;
746 case 16:
747 redBits = 5;
748 greenBits = 6;
749 blueBits = 5;
750 break;
751 case 32:
752 alphaBits = 8;
753 case 24:
754 redBits = 8;
755 greenBits = 8;
756 blueBits = 8;
757 break;
758 default:
759 return NULL;
760 }
761
762 if (vl_video_init(width, height, colDepth, refresh) != 0) {
763 return NULL;
764 }
765
766 if ((v=(DMesaVisual)calloc(1, sizeof(struct dmesa_visual)))!=NULL) {
767 /* Create core visual */
768 v->gl_visual = _mesa_create_visual(colDepth>8, /* rgb */
769 dbFlag,
770 GL_FALSE, /* stereo */
771 redBits,
772 greenBits,
773 blueBits,
774 alphaBits,
775 0, /* indexBits */
776 depthSize,
777 stencilSize,
778 accumSize, /* accumRed */
779 accumSize, /* accumGreen */
780 accumSize, /* accumBlue */
781 alphaBits?accumSize:0, /* accumAlpha */
782 1); /* numSamples */
783
784 v->depth = colDepth;
785 v->db_flag = dbFlag;
786 }
787
788 return v;
789 }
790
791
792
793 void DMesaDestroyVisual (DMesaVisual v)
794 {
795 vl_video_exit();
796 _mesa_destroy_visual(v->gl_visual);
797 free(v);
798 }
799
800
801
802 DMesaBuffer DMesaCreateBuffer (DMesaVisual visual,
803 GLint xpos, GLint ypos,
804 GLint width, GLint height)
805 {
806 DMesaBuffer b;
807
808 if ((b=(DMesaBuffer)calloc(1, sizeof(struct dmesa_buffer)))!=NULL) {
809
810 _mesa_initialize_framebuffer(&b->gl_buffer,
811 visual->gl_visual,
812 visual->gl_visual->depthBits > 0,
813 visual->gl_visual->stencilBits > 0,
814 visual->gl_visual->accumRedBits > 0,
815 visual->gl_visual->alphaBits > 0);
816 b->xpos = xpos;
817 b->ypos = ypos;
818 b->width = width;
819 b->height = height;
820 b->bypp = (visual->depth+7)/8;
821 }
822
823 return b;
824 }
825
826
827
828 void DMesaDestroyBuffer (DMesaBuffer b)
829 {
830 free(b->the_window);
831 _mesa_free_framebuffer_data(&b->gl_buffer);
832 free(b);
833 }
834
835
836
837 DMesaContext DMesaCreateContext (DMesaVisual visual,
838 DMesaContext share)
839 {
840 DMesaContext c;
841 GLboolean direct = GL_FALSE;
842
843 if ((c=(DMesaContext)calloc(1, sizeof(struct dmesa_context)))!=NULL) {
844 c->gl_ctx = _mesa_create_context(visual->gl_visual,
845 share ? share->gl_ctx : NULL,
846 (void *)c, direct);
847
848 _mesa_enable_sw_extensions(c->gl_ctx);
849 _mesa_enable_1_3_extensions(c->gl_ctx);
850
851 /* you probably have to do a bunch of other initializations here. */
852 c->visual = visual;
853
854 /* Initialize the software rasterizer and helper modules.
855 */
856 _swrast_CreateContext(c->gl_ctx);
857 _ac_CreateContext(c->gl_ctx);
858 _tnl_CreateContext(c->gl_ctx);
859 _swsetup_CreateContext(c->gl_ctx);
860 dmesa_init_pointers(c->gl_ctx);
861 dmesa_register_swrast_functions(c->gl_ctx);
862 }
863
864 return c;
865 }
866
867
868
869 void DMesaDestroyContext (DMesaContext c)
870 {
871 _mesa_destroy_context(c->gl_ctx);
872 free(c);
873 }
874
875
876
877 GLboolean DMesaViewport (DMesaBuffer b,
878 GLint xpos, GLint ypos,
879 GLint width, GLint height)
880 {
881 void *new_window;
882
883 if ((new_window=vl_sync_buffer(b->the_window, xpos, ypos, width, height))==NULL) {
884 return GL_FALSE;
885 } else {
886 b->the_window = new_window;
887 b->xpos = xpos;
888 b->ypos = ypos;
889 b->width = width;
890 b->height = height;
891 b->stride = width * b->bypp;
892 b->bytes = b->stride * height;
893 return GL_TRUE;
894 }
895 }
896
897
898
899 /*
900 * Make the specified context and buffer the current one.
901 */
902 GLboolean DMesaMakeCurrent (DMesaContext c, DMesaBuffer b)
903 {
904 if (c&&b) {
905 if (!DMesaViewport(b, b->xpos, b->ypos, b->width, b->height)) {
906 return GL_FALSE;
907 }
908
909 c->Buffer = b;
910
911 dmesa_update_state(c->gl_ctx, 0);
912 _mesa_make_current(c->gl_ctx, &b->gl_buffer);
913 if (c->gl_ctx->Viewport.Width==0) {
914 /* initialize viewport to window size */
915 _mesa_Viewport(0, 0, b->width, b->height);
916 }
917 } else {
918 /* Detach */
919 _mesa_make_current(NULL, NULL);
920 }
921
922 return GL_TRUE;
923 }
924
925
926
927 void DMesaSwapBuffers (DMesaBuffer b)
928 {
929 /* copy/swap back buffer to front if applicable */
930 GET_CURRENT_CONTEXT(ctx);
931 _mesa_swapbuffers(ctx);
932 vl_flip(b->the_window, b->stride, b->height);
933 }