finally get rid of ctx->Texture._ReallyEnabled field
[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._EnabledUnits) 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 const GLubyte col[4];
471 DMesaContext c = (DMesaContext)ctx->DriverCtx;
472 CLAMPED_FLOAT_TO_UBYTE(col[0], color[0]);
473 CLAMPED_FLOAT_TO_UBYTE(col[1], color[1]);
474 CLAMPED_FLOAT_TO_UBYTE(col[2], color[2]);
475 CLAMPED_FLOAT_TO_UBYTE(col[3], color[3]);
476 c->ClearColor = vl_mixrgba(col);
477 }
478
479
480
481 static void clear (GLcontext *ctx, GLbitfield mask, GLboolean all,
482 GLint x, GLint y, GLint width, GLint height)
483 {
484 DMesaContext c = (DMesaContext)ctx->DriverCtx;
485 const GLuint *colorMask = (GLuint *)&ctx->Color.ColorMask;
486 DMesaBuffer b = c->Buffer;
487
488 /*
489 * Clear the specified region of the buffers indicated by 'mask'
490 * using the clear color or index as specified by one of the two
491 * functions above.
492 * If all==GL_TRUE, clear whole buffer, else just clear region defined
493 * by x,y,width,height
494 */
495
496 /* we can't handle color or index masking */
497 if (*colorMask==0xffffffff) {
498 if (mask & DD_BACK_LEFT_BIT) {
499 if (all) {
500 vl_clear(b->the_window, b->bytes, c->ClearColor);
501 } else {
502 vl_rect(b->the_window, x, y, width, height, c->ClearColor);
503 }
504 mask &= ~DD_BACK_LEFT_BIT;
505 }
506 }
507
508 if (mask) {
509 _swrast_Clear(ctx, mask, all, x, y, width, height);
510 }
511 }
512
513
514
515 /*
516 * Set the current reading buffer.
517 */
518 static void set_read_buffer (GLcontext *ctx, GLframebuffer *buffer,
519 GLenum mode)
520 {
521 /*
522 XXX this has to be fixed
523 */
524 }
525
526
527
528 /*
529 * Set the destination/draw buffer.
530 */
531 static void set_draw_buffer (GLcontext *ctx, GLenum mode)
532 {
533 /*
534 XXX this has to be fixed
535 */
536 }
537
538
539
540 /*
541 * Return the width and height of the current buffer.
542 * If anything special has to been done when the buffer/window is
543 * resized, do it now.
544 */
545 static void get_buffer_size (GLframebuffer *buffer, GLuint *width, GLuint *height)
546 {
547 DMesaBuffer b = (DMesaBuffer)buffer;
548
549 *width = b->width;
550 *height = b->height;
551 }
552
553
554
555 static const GLubyte* get_string (GLcontext *ctx, GLenum name)
556 {
557 switch (name) {
558 case GL_RENDERER:
559 return (const GLubyte *)"Mesa DJGPP\0port (c) Borca Daniel 3-sep-2002";
560 default:
561 return NULL;
562 }
563 }
564
565
566
567 /**********************************************************************/
568 /***** Miscellaneous device driver funcs *****/
569 /***** Note that these functions are mandatory *****/
570 /**********************************************************************/
571
572
573
574 /* OPTIONAL FUNCTION: implements glFinish if possible */
575 static void finish (GLcontext *ctx)
576 {
577 /*
578 DMesaContext c = (DMesaContext)ctx->DriverCtx;
579 */
580 }
581
582
583
584 /* OPTIONAL FUNCTION: implements glFlush if possible */
585 static void flush (GLcontext *ctx)
586 {
587 /*
588 DMesaContext c = (DMesaContext)ctx->DriverCtx;
589 */
590 }
591
592
593
594 /**********************************************************************/
595 /**********************************************************************/
596
597
598
599 #define DMESA_NEW_TRIANGLE (_NEW_POLYGON | \
600 _NEW_TEXTURE | \
601 _NEW_LIGHT | \
602 _NEW_DEPTH | \
603 _NEW_RENDERMODE | \
604 _SWRAST_NEW_RASTERMASK)
605
606
607
608 /* Extend the software rasterizer with our line and triangle
609 * functions.
610 */
611 static void dmesa_register_swrast_functions (GLcontext *ctx)
612 {
613 SWcontext *swrast = SWRAST_CONTEXT(ctx);
614
615 swrast->choose_triangle = dmesa_choose_tri;
616
617 swrast->invalidate_triangle |= DMESA_NEW_TRIANGLE;
618 }
619
620
621
622 /* Setup pointers and other driver state that is constant for the life
623 * of a context.
624 */
625 void dmesa_init_pointers (GLcontext *ctx)
626 {
627 TNLcontext *tnl;
628
629 ctx->Driver.UpdateState = dmesa_update_state;
630
631 ctx->Driver.GetString = get_string;
632 ctx->Driver.GetBufferSize = get_buffer_size;
633 ctx->Driver.Flush = flush;
634 ctx->Driver.Finish = finish;
635
636 /* Software rasterizer pixel paths:
637 */
638 ctx->Driver.Accum = _swrast_Accum;
639 ctx->Driver.Bitmap = _swrast_Bitmap;
640 ctx->Driver.Clear = clear;
641 ctx->Driver.ResizeBuffers = _swrast_alloc_buffers;
642 ctx->Driver.CopyPixels = _swrast_CopyPixels;
643 ctx->Driver.DrawPixels = _swrast_DrawPixels;
644 ctx->Driver.ReadPixels = _swrast_ReadPixels;
645
646 /* Software texture functions:
647 */
648 ctx->Driver.ChooseTextureFormat = _mesa_choose_tex_format;
649 ctx->Driver.TexImage1D = _mesa_store_teximage1d;
650 ctx->Driver.TexImage2D = _mesa_store_teximage2d;
651 ctx->Driver.TexImage3D = _mesa_store_teximage3d;
652 ctx->Driver.TexSubImage1D = _mesa_store_texsubimage1d;
653 ctx->Driver.TexSubImage2D = _mesa_store_texsubimage2d;
654 ctx->Driver.TexSubImage3D = _mesa_store_texsubimage3d;
655 ctx->Driver.TestProxyTexImage = _mesa_test_proxy_teximage;
656
657 ctx->Driver.CompressedTexImage1D = _mesa_store_compressed_teximage1d;
658 ctx->Driver.CompressedTexImage2D = _mesa_store_compressed_teximage2d;
659 ctx->Driver.CompressedTexImage3D = _mesa_store_compressed_teximage3d;
660 ctx->Driver.CompressedTexSubImage1D = _mesa_store_compressed_texsubimage1d;
661 ctx->Driver.CompressedTexSubImage2D = _mesa_store_compressed_texsubimage2d;
662 ctx->Driver.CompressedTexSubImage3D = _mesa_store_compressed_texsubimage3d;
663
664 ctx->Driver.CopyTexImage1D = _swrast_copy_teximage1d;
665 ctx->Driver.CopyTexImage2D = _swrast_copy_teximage2d;
666 ctx->Driver.CopyTexSubImage1D = _swrast_copy_texsubimage1d;
667 ctx->Driver.CopyTexSubImage2D = _swrast_copy_texsubimage2d;
668 ctx->Driver.CopyTexSubImage3D = _swrast_copy_texsubimage3d;
669
670 /* Swrast hooks for imaging extensions:
671 */
672 ctx->Driver.CopyColorTable = _swrast_CopyColorTable;
673 ctx->Driver.CopyColorSubTable = _swrast_CopyColorSubTable;
674 ctx->Driver.CopyConvolutionFilter1D = _swrast_CopyConvolutionFilter1D;
675 ctx->Driver.CopyConvolutionFilter2D = _swrast_CopyConvolutionFilter2D;
676
677 /* Statechange callbacks:
678 */
679 ctx->Driver.SetDrawBuffer = set_draw_buffer;
680 ctx->Driver.ClearColor = clear_color;
681
682 /* Initialize the TNL driver interface:
683 */
684 tnl = TNL_CONTEXT(ctx);
685 tnl->Driver.RunPipeline = _tnl_run_pipeline;
686
687 /* Install swsetup for tnl->Driver.Render.*:
688 */
689 _swsetup_Wakeup(ctx);
690 }
691
692
693
694 static void dmesa_update_state (GLcontext *ctx, GLuint new_state)
695 {
696 DMesaContext c = (DMesaContext)ctx->DriverCtx;
697 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
698
699 /* Initialize all the pointers in the DD struct. Do this whenever */
700 /* a new context is made current or we change buffers via set_buffer! */
701
702 _swrast_InvalidateState(ctx, new_state);
703 _swsetup_InvalidateState(ctx, new_state);
704 _ac_InvalidateState(ctx, new_state);
705 _tnl_InvalidateState(ctx, new_state);
706
707 swdd->SetReadBuffer = set_read_buffer;
708
709 /* RGB(A) span/pixel functions */
710 swdd->WriteRGBASpan = write_rgba_span;
711 swdd->WriteRGBSpan = write_rgb_span;
712 swdd->WriteMonoRGBASpan = write_mono_rgba_span;
713 swdd->WriteRGBAPixels = write_rgba_pixels;
714 swdd->WriteMonoRGBAPixels = write_mono_rgba_pixels;
715 swdd->ReadRGBASpan = read_rgba_span;
716 swdd->ReadRGBAPixels = read_rgba_pixels;
717 }
718
719
720
721 /**********************************************************************/
722 /***** DMesa Public API Functions *****/
723 /**********************************************************************/
724
725
726
727 /*
728 * The exact arguments to this function will depend on your window system
729 */
730 DMesaVisual DMesaCreateVisual (GLint width, GLint height, GLint colDepth,
731 GLboolean dbFlag, GLint depthSize,
732 GLint stencilSize,
733 GLint accumSize)
734 {
735 DMesaVisual v;
736 GLint redBits, greenBits, blueBits, alphaBits;
737
738 int refresh;
739 char *var = getenv("DMESA_REFRESH");
740 if ((var == NULL) || ((refresh=atoi(var)) == 0)) {
741 refresh = 60;
742 }
743
744 if (!dbFlag) {
745 return NULL;
746 }
747 alphaBits = 0;
748 switch (colDepth) {
749 case 15:
750 redBits = 5;
751 greenBits = 5;
752 blueBits = 5;
753 break;
754 case 16:
755 redBits = 5;
756 greenBits = 6;
757 blueBits = 5;
758 break;
759 case 32:
760 alphaBits = 8;
761 case 24:
762 redBits = 8;
763 greenBits = 8;
764 blueBits = 8;
765 break;
766 default:
767 return NULL;
768 }
769
770 if (vl_video_init(width, height, colDepth, refresh) != 0) {
771 return NULL;
772 }
773
774 if ((v=(DMesaVisual)calloc(1, sizeof(struct dmesa_visual)))!=NULL) {
775 /* Create core visual */
776 v->gl_visual = _mesa_create_visual(colDepth>8, /* rgb */
777 dbFlag,
778 GL_FALSE, /* stereo */
779 redBits,
780 greenBits,
781 blueBits,
782 alphaBits,
783 0, /* indexBits */
784 depthSize,
785 stencilSize,
786 accumSize, /* accumRed */
787 accumSize, /* accumGreen */
788 accumSize, /* accumBlue */
789 alphaBits?accumSize:0, /* accumAlpha */
790 1); /* numSamples */
791
792 v->depth = colDepth;
793 v->db_flag = dbFlag;
794 }
795
796 return v;
797 }
798
799
800
801 void DMesaDestroyVisual (DMesaVisual v)
802 {
803 vl_video_exit();
804 _mesa_destroy_visual(v->gl_visual);
805 free(v);
806 }
807
808
809
810 DMesaBuffer DMesaCreateBuffer (DMesaVisual visual,
811 GLint xpos, GLint ypos,
812 GLint width, GLint height)
813 {
814 DMesaBuffer b;
815
816 if ((b=(DMesaBuffer)calloc(1, sizeof(struct dmesa_buffer)))!=NULL) {
817
818 _mesa_initialize_framebuffer(&b->gl_buffer,
819 visual->gl_visual,
820 visual->gl_visual->depthBits > 0,
821 visual->gl_visual->stencilBits > 0,
822 visual->gl_visual->accumRedBits > 0,
823 visual->gl_visual->alphaBits > 0);
824 b->xpos = xpos;
825 b->ypos = ypos;
826 b->width = width;
827 b->height = height;
828 b->bypp = (visual->depth+7)/8;
829 }
830
831 return b;
832 }
833
834
835
836 void DMesaDestroyBuffer (DMesaBuffer b)
837 {
838 free(b->the_window);
839 _mesa_free_framebuffer_data(&b->gl_buffer);
840 free(b);
841 }
842
843
844
845 DMesaContext DMesaCreateContext (DMesaVisual visual,
846 DMesaContext share)
847 {
848 DMesaContext c;
849 GLboolean direct = GL_FALSE;
850
851 if ((c=(DMesaContext)calloc(1, sizeof(struct dmesa_context)))!=NULL) {
852 c->gl_ctx = _mesa_create_context(visual->gl_visual,
853 share ? share->gl_ctx : NULL,
854 (void *)c, direct);
855
856 _mesa_enable_sw_extensions(c->gl_ctx);
857 _mesa_enable_1_3_extensions(c->gl_ctx);
858
859 /* you probably have to do a bunch of other initializations here. */
860 c->visual = visual;
861
862 /* Initialize the software rasterizer and helper modules.
863 */
864 _swrast_CreateContext(c->gl_ctx);
865 _ac_CreateContext(c->gl_ctx);
866 _tnl_CreateContext(c->gl_ctx);
867 _swsetup_CreateContext(c->gl_ctx);
868 dmesa_init_pointers(c->gl_ctx);
869 dmesa_register_swrast_functions(c->gl_ctx);
870 }
871
872 return c;
873 }
874
875
876
877 void DMesaDestroyContext (DMesaContext c)
878 {
879 _mesa_destroy_context(c->gl_ctx);
880 free(c);
881 }
882
883
884
885 GLboolean DMesaViewport (DMesaBuffer b,
886 GLint xpos, GLint ypos,
887 GLint width, GLint height)
888 {
889 void *new_window;
890
891 if ((new_window=vl_sync_buffer(b->the_window, xpos, ypos, width, height))==NULL) {
892 return GL_FALSE;
893 } else {
894 b->the_window = new_window;
895 b->xpos = xpos;
896 b->ypos = ypos;
897 b->width = width;
898 b->height = height;
899 b->stride = width * b->bypp;
900 b->bytes = b->stride * height;
901 return GL_TRUE;
902 }
903 }
904
905
906
907 /*
908 * Make the specified context and buffer the current one.
909 */
910 GLboolean DMesaMakeCurrent (DMesaContext c, DMesaBuffer b)
911 {
912 if (c&&b) {
913 if (!DMesaViewport(b, b->xpos, b->ypos, b->width, b->height)) {
914 return GL_FALSE;
915 }
916
917 c->Buffer = b;
918
919 dmesa_update_state(c->gl_ctx, 0);
920 _mesa_make_current(c->gl_ctx, &b->gl_buffer);
921 if (c->gl_ctx->Viewport.Width==0) {
922 /* initialize viewport to window size */
923 _mesa_Viewport(0, 0, b->width, b->height);
924 }
925 } else {
926 /* Detach */
927 _mesa_make_current(NULL, NULL);
928 }
929
930 return GL_TRUE;
931 }
932
933
934
935 void DMesaSwapBuffers (DMesaBuffer b)
936 {
937 /* copy/swap back buffer to front if applicable */
938 GET_CURRENT_CONTEXT(ctx);
939 _mesa_swapbuffers(ctx);
940 vl_flip(b->the_window, b->stride, b->height);
941 }