blob: 454b891c08b364b51679911a956efb75f3d2cebb [file] [log] [blame]
Brian Pauldb41d2e2002-02-12 03:24:56 +00001/*
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/*
Brian Paulb43a8282002-03-08 19:27:17 +000026 * DOS/DJGPP device driver v0.3 for Mesa 4.0
Brian Pauldb41d2e2002-02-12 03:24:56 +000027 *
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"
Brian Paule0193a92002-02-23 17:11:27 +000040#include "extensions.h"
41#include "macros.h"
Brian Pauldb41d2e2002-02-12 03:24:56 +000042#include "matrix.h"
Brian Paule0193a92002-02-23 17:11:27 +000043#include "mmath.h"
Brian Pauldb41d2e2002-02-12 03:24:56 +000044#include "texformat.h"
45#include "texstore.h"
46#include "array_cache/acache.h"
Brian Paule0193a92002-02-23 17:11:27 +000047#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"
Brian Pauldb41d2e2002-02-12 03:24:56 +000052#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
Brian Paule0193a92002-02-23 17:11:27 +000059#include "video.h"
Brian Pauldb41d2e2002-02-12 03:24:56 +000060
61
62
63/*
64 * In C++ terms, this class derives from the GLvisual class.
65 * Add system-specific fields to it.
66 */
67struct dmesa_visual {
68 GLvisual *gl_visual;
Brian Paulb43a8282002-03-08 19:27:17 +000069 GLboolean db_flag; /* double buffered? */
Brian Pauldb41d2e2002-02-12 03:24:56 +000070 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 */
78struct dmesa_buffer {
79 GLframebuffer *gl_buffer; /* The depth, stencil, accum, etc buffers */
80 void *the_window; /* your window handle, etc */
81
Brian Paule0193a92002-02-23 17:11:27 +000082 int xpos, ypos; /* position */
Brian Pauldb41d2e2002-02-12 03:24:56 +000083 int width, height; /* size in pixels */
Brian Paulb43a8282002-03-08 19:27:17 +000084 int bwidth, len; /* bytes in a line, then total */
Brian Pauldb41d2e2002-02-12 03:24:56 +000085};
86
87/*
88 * In C++ terms, this class derives from the GLcontext class.
89 * Add system-specific fields to it.
90 */
91struct 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
101static void dmesa_update_state (GLcontext *ctx, GLuint new_state);
102
103
104
105/**********************************************************************/
106/***** Read/Write pixels *****/
107/**********************************************************************/
108
109
110
Brian Paule0193a92002-02-23 17:11:27 +0000111#define FLIP(y) (c->Buffer->height - (y) - 1)
112#define FLIP2(y) (h - (y) - 1)
Brian Pauldb41d2e2002-02-12 03:24:56 +0000113
Brian Pauldb41d2e2002-02-12 03:24:56 +0000114
Brian Pauldb41d2e2002-02-12 03:24:56 +0000115
Brian Paule0193a92002-02-23 17:11:27 +0000116static 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;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000122
Brian Paulb43a8282002-03-08 19:27:17 +0000123 offset = c->Buffer->width * FLIP(y) + x;
Brian Paule0193a92002-02-23 17:11:27 +0000124 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}
Brian Pauldb41d2e2002-02-12 03:24:56 +0000138
Brian Paule0193a92002-02-23 17:11:27 +0000139static 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;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000145
Brian Paulb43a8282002-03-08 19:27:17 +0000146 offset = c->Buffer->width * FLIP(y) + x;
Brian Paule0193a92002-02-23 17:11:27 +0000147 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
162static 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
Brian Paulb43a8282002-03-08 19:27:17 +0000170 offset = c->Buffer->width * FLIP(y) + x;
Brian Paule0193a92002-02-23 17:11:27 +0000171 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
186static 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
Brian Paulb43a8282002-03-08 19:27:17 +0000193 offset = c->Buffer->width * FLIP(y) + x;
Brian Paule0193a92002-02-23 17:11:27 +0000194 /* read all pixels */
195 for (i=0; i<n; i++, offset++) {
196 vl_getrgba(b, offset, rgba[i]);
197 }
198}
199
200static 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;
Brian Paulb43a8282002-03-08 19:27:17 +0000206 GLuint i, w = c->Buffer->width, h = c->Buffer->height;
Brian Paule0193a92002-02-23 17:11:27 +0000207
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
223static 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;
Brian Paulb43a8282002-03-08 19:27:17 +0000229 GLuint i, w = c->Buffer->width, h = c->Buffer->height, rgba = vl_mixrgba(color);
Brian Paule0193a92002-02-23 17:11:27 +0000230
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
246static 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;
Brian Paulb43a8282002-03-08 19:27:17 +0000252 GLuint i, w = c->Buffer->width, h = c->Buffer->height;
Brian Paule0193a92002-02-23 17:11:27 +0000253
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 */
280static 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;
Brian Paulb43a8282002-03-08 19:27:17 +0000287 GLuint w = c->Buffer->width, h = c->Buffer->height;
Brian Paule0193a92002-02-23 17:11:27 +0000288
Brian Paule0193a92002-02-23 17:11:27 +0000289#define SETUP_CODE GLuint rgb = vl_mixrgb(v2->color);
290
291#define RENDER_SPAN(span) \
Brian Paulb43a8282002-03-08 19:27:17 +0000292 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 }
Brian Paule0193a92002-02-23 17:11:27 +0000296
297#include "swrast/s_tritemp.h"
Brian Paule0193a92002-02-23 17:11:27 +0000298}
299
300
301
302/*
303 * flat, depth-buffered, triangle.
304 */
305static 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;
Brian Paulb43a8282002-03-08 19:27:17 +0000312 GLuint w = c->Buffer->width, h = c->Buffer->height;
Brian Paule0193a92002-02-23 17:11:27 +0000313
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) \
Brian Paulb43a8282002-03-08 19:27:17 +0000319 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 }
Brian Paule0193a92002-02-23 17:11:27 +0000328
329#include "swrast/s_tritemp.h"
330}
331
332
333
334/*
335 * smooth, NON-depth-buffered, triangle.
336 */
337static 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;
Brian Paulb43a8282002-03-08 19:27:17 +0000344 GLuint w = c->Buffer->width, h = c->Buffer->height;
Brian Paule0193a92002-02-23 17:11:27 +0000345
346#define INTERP_RGB 1
347#define RENDER_SPAN(span) \
Brian Paulb43a8282002-03-08 19:27:17 +0000348 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 }
Brian Paule0193a92002-02-23 17:11:27 +0000359
360#include "swrast/s_tritemp.h"
361}
362
363
364
365/*
366 * smooth, depth-buffered, triangle.
367 */
368static 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;
Brian Paulb43a8282002-03-08 19:27:17 +0000375 GLuint w = c->Buffer->width, h = c->Buffer->height;
Brian Paule0193a92002-02-23 17:11:27 +0000376
377#define INTERP_Z 1
378#define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
379#define INTERP_RGB 1
380
381#define RENDER_SPAN(span) \
Brian Paulb43a8282002-03-08 19:27:17 +0000382 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 }
Brian Paule0193a92002-02-23 17:11:27 +0000398
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 */
408static 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 */
452static 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}
Brian Pauldb41d2e2002-02-12 03:24:56 +0000459
460
461
462/**********************************************************************/
463/***** Miscellaneous device driver funcs *****/
464/**********************************************************************/
465
466
467
468static void clear_color (GLcontext *ctx, const GLchan color[4])
469{
470 DMesaContext c = (DMesaContext)ctx->DriverCtx;
Brian Paule0193a92002-02-23 17:11:27 +0000471 c->ClearColor = vl_mixrgba(color);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000472}
473
474
475
476static 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 */
Brian Paule0193a92002-02-23 17:11:27 +0000492 if (*colorMask==0xffffffff) {
493 if (mask & DD_BACK_LEFT_BIT) {
Brian Pauldb41d2e2002-02-12 03:24:56 +0000494 if (all) {
Brian Paulb43a8282002-03-08 19:27:17 +0000495 vl_clear(b->the_window, b->len, c->ClearColor);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000496 } else {
Brian Paulb43a8282002-03-08 19:27:17 +0000497 vl_rect(b->the_window, x, y, width, height, c->ClearColor);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000498 }
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 */
513static void set_read_buffer (GLcontext *ctx, GLframebuffer *buffer,
514 GLenum mode)
515{
516/*
517 DMesaContext c = (DMesaContext)ctx->DriverCtx;
518 dmesa_update_state(ctx);
519*/
520}
521
522
523
524/*
525 * Set the destination/draw buffer.
526 */
527static GLboolean set_draw_buffer (GLcontext *ctx, GLenum mode)
528{
Brian Paulb43a8282002-03-08 19:27:17 +0000529 if (mode==GL_BACK_LEFT) {
Brian Pauldb41d2e2002-02-12 03:24:56 +0000530 return GL_TRUE;
531 } else {
532 return GL_FALSE;
533 }
534}
535
536
537
538/*
539 * Return the width and height of the current buffer.
540 * If anything special has to been done when the buffer/window is
541 * resized, do it now.
542 */
543static void get_buffer_size (GLcontext *ctx, GLuint *width, GLuint *height)
544{
545 DMesaContext c = (DMesaContext)ctx->DriverCtx;
546
547 *width = c->Buffer->width;
548 *height = c->Buffer->height;
549}
550
551
552
553static const GLubyte* get_string (GLcontext *ctx, GLenum name)
554{
555 switch (name) {
556 case GL_RENDERER:
557 return (const GLubyte *)"DOS Mesa";
558 default:
559 return NULL;
560 }
561}
562
563
564
565/**********************************************************************/
566/***** Miscellaneous device driver funcs *****/
567/***** Note that these functions are mandatory *****/
568/**********************************************************************/
569
570
571
572/* OPTIONAL FUNCTION: implements glFinish if possible */
573static void finish (GLcontext *ctx)
574{
575/*
576 DMesaContext c = (DMesaContext)ctx->DriverCtx;
577*/
578}
579
580
581
582/* OPTIONAL FUNCTION: implements glFlush if possible */
583static void flush (GLcontext *ctx)
584{
585/*
586 DMesaContext c = (DMesaContext)ctx->DriverCtx;
587*/
588}
589
590
591
592/**********************************************************************/
593/**********************************************************************/
594
595
596
Brian Paule0193a92002-02-23 17:11:27 +0000597#define DMESA_NEW_TRIANGLE (_NEW_POLYGON | \
598 _NEW_TEXTURE | \
599 _NEW_LIGHT | \
600 _NEW_DEPTH | \
601 _NEW_RENDERMODE | \
602 _SWRAST_NEW_RASTERMASK)
603
604
605
606/* Extend the software rasterizer with our line and triangle
607 * functions.
608 */
609static void dmesa_register_swrast_functions (GLcontext *ctx)
610{
611 SWcontext *swrast = SWRAST_CONTEXT(ctx);
612
613 swrast->choose_triangle = dmesa_choose_tri;
614
615 swrast->invalidate_triangle |= DMESA_NEW_TRIANGLE;
616}
617
618
619
Brian Pauldb41d2e2002-02-12 03:24:56 +0000620/* Setup pointers and other driver state that is constant for the life
621 * of a context.
622 */
623void dmesa_init_pointers (GLcontext *ctx)
624{
625 TNLcontext *tnl;
626
627 ctx->Driver.UpdateState = dmesa_update_state;
628
629 ctx->Driver.GetString = get_string;
630 ctx->Driver.GetBufferSize = get_buffer_size;
631 ctx->Driver.Flush = flush;
632 ctx->Driver.Finish = finish;
633
634 /* Software rasterizer pixel paths:
635 */
636 ctx->Driver.Accum = _swrast_Accum;
637 ctx->Driver.Bitmap = _swrast_Bitmap;
638 ctx->Driver.Clear = clear;
639 ctx->Driver.ResizeBuffersMESA = _swrast_alloc_buffers;
640 ctx->Driver.CopyPixels = _swrast_CopyPixels;
641 ctx->Driver.DrawPixels = _swrast_DrawPixels;
642 ctx->Driver.ReadPixels = _swrast_ReadPixels;
643
644 /* Software texture functions:
645 */
646 ctx->Driver.ChooseTextureFormat = _mesa_choose_tex_format;
647 ctx->Driver.TexImage1D = _mesa_store_teximage1d;
648 ctx->Driver.TexImage2D = _mesa_store_teximage2d;
649 ctx->Driver.TexImage3D = _mesa_store_teximage3d;
650 ctx->Driver.TexSubImage1D = _mesa_store_texsubimage1d;
651 ctx->Driver.TexSubImage2D = _mesa_store_texsubimage2d;
652 ctx->Driver.TexSubImage3D = _mesa_store_texsubimage3d;
653 ctx->Driver.TestProxyTexImage = _mesa_test_proxy_teximage;
654
655 ctx->Driver.CopyTexImage1D = _swrast_copy_teximage1d;
656 ctx->Driver.CopyTexImage2D = _swrast_copy_teximage2d;
657 ctx->Driver.CopyTexSubImage1D = _swrast_copy_texsubimage1d;
658 ctx->Driver.CopyTexSubImage2D = _swrast_copy_texsubimage2d;
659 ctx->Driver.CopyTexSubImage3D = _swrast_copy_texsubimage3d;
660
661 ctx->Driver.BaseCompressedTexFormat = _mesa_base_compressed_texformat;
662 ctx->Driver.CompressedTextureSize = _mesa_compressed_texture_size;
663 ctx->Driver.GetCompressedTexImage = _mesa_get_compressed_teximage;
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
689static 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 */
Brian Paule0193a92002-02-23 17:11:27 +0000705 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;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000712}
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 */
Brian Paule0193a92002-02-23 17:11:27 +0000725DMesaVisual DMesaCreateVisual (GLint width, GLint height, GLint colDepth,
726 GLboolean dbFlag, GLint depthSize,
727 GLint stencilSize,
Brian Pauldb41d2e2002-02-12 03:24:56 +0000728 GLint accumSize)
729{
730 DMesaVisual v;
731 GLint redBits, greenBits, blueBits, alphaBits;
732
Brian Paulb43a8282002-03-08 19:27:17 +0000733 if (!dbFlag) {
734 return NULL;
735 }
Brian Paule0193a92002-02-23 17:11:27 +0000736 alphaBits = 0;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000737 switch (colDepth) {
738 case 15:
739 redBits = 5;
740 greenBits = 5;
741 blueBits = 5;
742 break;
743 case 16:
744 redBits = 5;
745 greenBits = 6;
746 blueBits = 5;
747 break;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000748 case 32:
Brian Paule0193a92002-02-23 17:11:27 +0000749 alphaBits = 8;
750 case 24:
Brian Pauldb41d2e2002-02-12 03:24:56 +0000751 redBits = 8;
752 greenBits = 8;
753 blueBits = 8;
754 break;
755 default:
756 return NULL;
757 }
Brian Paule0193a92002-02-23 17:11:27 +0000758
Brian Paulb43a8282002-03-08 19:27:17 +0000759 if (vl_video_init(width, height, colDepth)!=0) {
Brian Paule0193a92002-02-23 17:11:27 +0000760 return NULL;
761 }
Brian Pauldb41d2e2002-02-12 03:24:56 +0000762
763 if ((v=(DMesaVisual)calloc(1, sizeof(struct dmesa_visual)))!=NULL) {
764 /* Create core visual */
765 v->gl_visual = _mesa_create_visual(colDepth>8, /* rgb */
766 dbFlag,
767 GL_FALSE, /* stereo */
768 redBits,
769 greenBits,
770 blueBits,
771 alphaBits,
772 0, /* indexBits */
773 depthSize,
774 stencilSize,
775 accumSize, /* accumRed */
776 accumSize, /* accumGreen */
777 accumSize, /* accumBlue */
778 alphaBits?accumSize:0, /* accumAlpha */
779 1); /* numSamples */
780
781 v->depth = colDepth;
Brian Paulb43a8282002-03-08 19:27:17 +0000782 v->db_flag = dbFlag;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000783 }
784
785 return v;
786}
787
788
789
790void DMesaDestroyVisual (DMesaVisual v)
791{
Brian Paulb43a8282002-03-08 19:27:17 +0000792 vl_video_exit(!0);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000793 _mesa_destroy_visual(v->gl_visual);
794 free(v);
795}
796
797
798
799DMesaBuffer DMesaCreateBuffer (DMesaVisual visual,
Brian Paule0193a92002-02-23 17:11:27 +0000800 GLint xpos, GLint ypos,
801 GLint width, GLint height)
Brian Pauldb41d2e2002-02-12 03:24:56 +0000802{
803 DMesaBuffer b;
804
805 if ((b=(DMesaBuffer)calloc(1, sizeof(struct dmesa_buffer)))!=NULL) {
Brian Pauldb41d2e2002-02-12 03:24:56 +0000806
807 b->gl_buffer = _mesa_create_framebuffer(visual->gl_visual,
808 visual->gl_visual->depthBits > 0,
809 visual->gl_visual->stencilBits > 0,
810 visual->gl_visual->accumRedBits > 0,
811 visual->gl_visual->alphaBits > 0);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000812 b->xpos = xpos;
813 b->ypos = ypos;
Brian Paule0193a92002-02-23 17:11:27 +0000814 b->width = width;
Brian Paulb43a8282002-03-08 19:27:17 +0000815 b->bwidth = width * ((visual->depth+7)/8);
Brian Paule0193a92002-02-23 17:11:27 +0000816 b->height = height;
Brian Paulb43a8282002-03-08 19:27:17 +0000817 b->len = b->bwidth * b->height;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000818 }
819
820 return b;
821}
822
823
824
825void DMesaDestroyBuffer (DMesaBuffer b)
826{
Brian Paulb43a8282002-03-08 19:27:17 +0000827 free(b->the_window);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000828 _mesa_destroy_framebuffer(b->gl_buffer);
829 free(b);
830}
831
832
833
834DMesaContext DMesaCreateContext (DMesaVisual visual,
835 DMesaContext share)
836{
837 DMesaContext c;
838 GLboolean direct = GL_FALSE;
839
840 if ((c=(DMesaContext)calloc(1, sizeof(struct dmesa_context)))!=NULL) {
841 c->gl_ctx = _mesa_create_context(visual->gl_visual,
842 share ? share->gl_ctx : NULL,
843 (void *)c, direct);
844
Brian Paule0193a92002-02-23 17:11:27 +0000845 _mesa_enable_sw_extensions(c->gl_ctx);
846 _mesa_enable_1_3_extensions(c->gl_ctx);
847
Brian Pauldb41d2e2002-02-12 03:24:56 +0000848 /* you probably have to do a bunch of other initializations here. */
849 c->visual = visual;
850
851 /* Initialize the software rasterizer and helper modules.
852 */
853 _swrast_CreateContext(c->gl_ctx);
854 _ac_CreateContext(c->gl_ctx);
855 _tnl_CreateContext(c->gl_ctx);
856 _swsetup_CreateContext(c->gl_ctx);
857 dmesa_init_pointers(c->gl_ctx);
Brian Paule0193a92002-02-23 17:11:27 +0000858 dmesa_register_swrast_functions(c->gl_ctx);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000859 }
860
861 return c;
862}
863
864
865
866void DMesaDestroyContext (DMesaContext c)
867{
868 _mesa_destroy_context(c->gl_ctx);
869 free(c);
870}
871
872
873
874/*
875 * Make the specified context and buffer the current one.
876 */
877GLboolean DMesaMakeCurrent (DMesaContext c, DMesaBuffer b)
878{
879 if (c&&b) {
Brian Paulb43a8282002-03-08 19:27:17 +0000880 if ((b->the_window=vl_sync_buffer(b->the_window, b->xpos, b->ypos, b->width, b->height))==NULL) {
Brian Pauldb41d2e2002-02-12 03:24:56 +0000881 return GL_FALSE;
882 }
883
Brian Paule0193a92002-02-23 17:11:27 +0000884 c->Buffer = b;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000885
886 dmesa_update_state(c->gl_ctx, 0);
887 _mesa_make_current(c->gl_ctx, b->gl_buffer);
888 if (c->gl_ctx->Viewport.Width==0) {
889 /* initialize viewport to window size */
890 _mesa_Viewport(0, 0, c->Buffer->width, c->Buffer->height);
891 }
892 } else {
893 /* Detach */
894 _mesa_make_current(NULL, NULL);
895 }
896
897 return GL_TRUE;
898}
899
900
901
902void DMesaSwapBuffers (DMesaBuffer b)
903{
904 /* copy/swap back buffer to front if applicable */
Brian Paulb43a8282002-03-08 19:27:17 +0000905 vl_flip(b->the_window, b->bwidth, b->height);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000906}