blob: d150cdca5acfbf6277773e884204bfeb1ccbb34f [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 */
Brian Paul18a285a2002-03-16 00:53:15 +0000543static void get_buffer_size (GLframebuffer *buffer, GLuint *width, GLuint *height)
Brian Pauldb41d2e2002-02-12 03:24:56 +0000544{
Brian Paul18a285a2002-03-16 00:53:15 +0000545 /* XXX this may not be right. We should query the size of the DOS window
546 * associated with <buffer>. This function should work whether or
547 * not there is a current context.
548 */
549 GET_CURRENT_CONTEXT(ctx);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000550 DMesaContext c = (DMesaContext)ctx->DriverCtx;
551
552 *width = c->Buffer->width;
553 *height = c->Buffer->height;
554}
555
556
557
558static const GLubyte* get_string (GLcontext *ctx, GLenum name)
559{
560 switch (name) {
561 case GL_RENDERER:
562 return (const GLubyte *)"DOS Mesa";
563 default:
564 return NULL;
565 }
566}
567
568
569
570/**********************************************************************/
571/***** Miscellaneous device driver funcs *****/
572/***** Note that these functions are mandatory *****/
573/**********************************************************************/
574
575
576
577/* OPTIONAL FUNCTION: implements glFinish if possible */
578static void finish (GLcontext *ctx)
579{
580/*
581 DMesaContext c = (DMesaContext)ctx->DriverCtx;
582*/
583}
584
585
586
587/* OPTIONAL FUNCTION: implements glFlush if possible */
588static void flush (GLcontext *ctx)
589{
590/*
591 DMesaContext c = (DMesaContext)ctx->DriverCtx;
592*/
593}
594
595
596
597/**********************************************************************/
598/**********************************************************************/
599
600
601
Brian Paule0193a92002-02-23 17:11:27 +0000602#define DMESA_NEW_TRIANGLE (_NEW_POLYGON | \
603 _NEW_TEXTURE | \
604 _NEW_LIGHT | \
605 _NEW_DEPTH | \
606 _NEW_RENDERMODE | \
607 _SWRAST_NEW_RASTERMASK)
608
609
610
611/* Extend the software rasterizer with our line and triangle
612 * functions.
613 */
614static void dmesa_register_swrast_functions (GLcontext *ctx)
615{
616 SWcontext *swrast = SWRAST_CONTEXT(ctx);
617
618 swrast->choose_triangle = dmesa_choose_tri;
619
620 swrast->invalidate_triangle |= DMESA_NEW_TRIANGLE;
621}
622
623
624
Brian Pauldb41d2e2002-02-12 03:24:56 +0000625/* Setup pointers and other driver state that is constant for the life
626 * of a context.
627 */
628void dmesa_init_pointers (GLcontext *ctx)
629{
630 TNLcontext *tnl;
631
632 ctx->Driver.UpdateState = dmesa_update_state;
633
634 ctx->Driver.GetString = get_string;
635 ctx->Driver.GetBufferSize = get_buffer_size;
636 ctx->Driver.Flush = flush;
637 ctx->Driver.Finish = finish;
638
639 /* Software rasterizer pixel paths:
640 */
641 ctx->Driver.Accum = _swrast_Accum;
642 ctx->Driver.Bitmap = _swrast_Bitmap;
643 ctx->Driver.Clear = clear;
Brian Paul18a285a2002-03-16 00:53:15 +0000644 ctx->Driver.ResizeBuffers = _swrast_alloc_buffers;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000645 ctx->Driver.CopyPixels = _swrast_CopyPixels;
646 ctx->Driver.DrawPixels = _swrast_DrawPixels;
647 ctx->Driver.ReadPixels = _swrast_ReadPixels;
648
649 /* Software texture functions:
650 */
651 ctx->Driver.ChooseTextureFormat = _mesa_choose_tex_format;
652 ctx->Driver.TexImage1D = _mesa_store_teximage1d;
653 ctx->Driver.TexImage2D = _mesa_store_teximage2d;
654 ctx->Driver.TexImage3D = _mesa_store_teximage3d;
655 ctx->Driver.TexSubImage1D = _mesa_store_texsubimage1d;
656 ctx->Driver.TexSubImage2D = _mesa_store_texsubimage2d;
657 ctx->Driver.TexSubImage3D = _mesa_store_texsubimage3d;
658 ctx->Driver.TestProxyTexImage = _mesa_test_proxy_teximage;
659
660 ctx->Driver.CopyTexImage1D = _swrast_copy_teximage1d;
661 ctx->Driver.CopyTexImage2D = _swrast_copy_teximage2d;
662 ctx->Driver.CopyTexSubImage1D = _swrast_copy_texsubimage1d;
663 ctx->Driver.CopyTexSubImage2D = _swrast_copy_texsubimage2d;
664 ctx->Driver.CopyTexSubImage3D = _swrast_copy_texsubimage3d;
665
666 ctx->Driver.BaseCompressedTexFormat = _mesa_base_compressed_texformat;
667 ctx->Driver.CompressedTextureSize = _mesa_compressed_texture_size;
668 ctx->Driver.GetCompressedTexImage = _mesa_get_compressed_teximage;
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
694static 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 */
Brian Paule0193a92002-02-23 17:11:27 +0000710 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;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000717}
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 */
Brian Paule0193a92002-02-23 17:11:27 +0000730DMesaVisual DMesaCreateVisual (GLint width, GLint height, GLint colDepth,
731 GLboolean dbFlag, GLint depthSize,
732 GLint stencilSize,
Brian Pauldb41d2e2002-02-12 03:24:56 +0000733 GLint accumSize)
734{
735 DMesaVisual v;
736 GLint redBits, greenBits, blueBits, alphaBits;
737
Brian Paulb43a8282002-03-08 19:27:17 +0000738 if (!dbFlag) {
739 return NULL;
740 }
Brian Paule0193a92002-02-23 17:11:27 +0000741 alphaBits = 0;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000742 switch (colDepth) {
743 case 15:
744 redBits = 5;
745 greenBits = 5;
746 blueBits = 5;
747 break;
748 case 16:
749 redBits = 5;
750 greenBits = 6;
751 blueBits = 5;
752 break;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000753 case 32:
Brian Paule0193a92002-02-23 17:11:27 +0000754 alphaBits = 8;
755 case 24:
Brian Pauldb41d2e2002-02-12 03:24:56 +0000756 redBits = 8;
757 greenBits = 8;
758 blueBits = 8;
759 break;
760 default:
761 return NULL;
762 }
Brian Paule0193a92002-02-23 17:11:27 +0000763
Brian Paulb43a8282002-03-08 19:27:17 +0000764 if (vl_video_init(width, height, colDepth)!=0) {
Brian Paule0193a92002-02-23 17:11:27 +0000765 return NULL;
766 }
Brian Pauldb41d2e2002-02-12 03:24:56 +0000767
768 if ((v=(DMesaVisual)calloc(1, sizeof(struct dmesa_visual)))!=NULL) {
769 /* Create core visual */
770 v->gl_visual = _mesa_create_visual(colDepth>8, /* rgb */
771 dbFlag,
772 GL_FALSE, /* stereo */
773 redBits,
774 greenBits,
775 blueBits,
776 alphaBits,
777 0, /* indexBits */
778 depthSize,
779 stencilSize,
780 accumSize, /* accumRed */
781 accumSize, /* accumGreen */
782 accumSize, /* accumBlue */
783 alphaBits?accumSize:0, /* accumAlpha */
784 1); /* numSamples */
785
786 v->depth = colDepth;
Brian Paulb43a8282002-03-08 19:27:17 +0000787 v->db_flag = dbFlag;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000788 }
789
790 return v;
791}
792
793
794
795void DMesaDestroyVisual (DMesaVisual v)
796{
Brian Paulb43a8282002-03-08 19:27:17 +0000797 vl_video_exit(!0);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000798 _mesa_destroy_visual(v->gl_visual);
799 free(v);
800}
801
802
803
804DMesaBuffer DMesaCreateBuffer (DMesaVisual visual,
Brian Paule0193a92002-02-23 17:11:27 +0000805 GLint xpos, GLint ypos,
806 GLint width, GLint height)
Brian Pauldb41d2e2002-02-12 03:24:56 +0000807{
808 DMesaBuffer b;
809
810 if ((b=(DMesaBuffer)calloc(1, sizeof(struct dmesa_buffer)))!=NULL) {
Brian Pauldb41d2e2002-02-12 03:24:56 +0000811
812 b->gl_buffer = _mesa_create_framebuffer(visual->gl_visual,
813 visual->gl_visual->depthBits > 0,
814 visual->gl_visual->stencilBits > 0,
815 visual->gl_visual->accumRedBits > 0,
816 visual->gl_visual->alphaBits > 0);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000817 b->xpos = xpos;
818 b->ypos = ypos;
Brian Paule0193a92002-02-23 17:11:27 +0000819 b->width = width;
Brian Paulb43a8282002-03-08 19:27:17 +0000820 b->bwidth = width * ((visual->depth+7)/8);
Brian Paule0193a92002-02-23 17:11:27 +0000821 b->height = height;
Brian Paulb43a8282002-03-08 19:27:17 +0000822 b->len = b->bwidth * b->height;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000823 }
824
825 return b;
826}
827
828
829
830void DMesaDestroyBuffer (DMesaBuffer b)
831{
Brian Paulb43a8282002-03-08 19:27:17 +0000832 free(b->the_window);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000833 _mesa_destroy_framebuffer(b->gl_buffer);
834 free(b);
835}
836
837
838
839DMesaContext DMesaCreateContext (DMesaVisual visual,
840 DMesaContext share)
841{
842 DMesaContext c;
843 GLboolean direct = GL_FALSE;
844
845 if ((c=(DMesaContext)calloc(1, sizeof(struct dmesa_context)))!=NULL) {
846 c->gl_ctx = _mesa_create_context(visual->gl_visual,
847 share ? share->gl_ctx : NULL,
848 (void *)c, direct);
849
Brian Paule0193a92002-02-23 17:11:27 +0000850 _mesa_enable_sw_extensions(c->gl_ctx);
851 _mesa_enable_1_3_extensions(c->gl_ctx);
852
Brian Pauldb41d2e2002-02-12 03:24:56 +0000853 /* you probably have to do a bunch of other initializations here. */
854 c->visual = visual;
855
856 /* Initialize the software rasterizer and helper modules.
857 */
858 _swrast_CreateContext(c->gl_ctx);
859 _ac_CreateContext(c->gl_ctx);
860 _tnl_CreateContext(c->gl_ctx);
861 _swsetup_CreateContext(c->gl_ctx);
862 dmesa_init_pointers(c->gl_ctx);
Brian Paule0193a92002-02-23 17:11:27 +0000863 dmesa_register_swrast_functions(c->gl_ctx);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000864 }
865
866 return c;
867}
868
869
870
871void DMesaDestroyContext (DMesaContext c)
872{
873 _mesa_destroy_context(c->gl_ctx);
874 free(c);
875}
876
877
878
879/*
880 * Make the specified context and buffer the current one.
881 */
882GLboolean DMesaMakeCurrent (DMesaContext c, DMesaBuffer b)
883{
884 if (c&&b) {
Brian Paulb43a8282002-03-08 19:27:17 +0000885 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 +0000886 return GL_FALSE;
887 }
888
Brian Paule0193a92002-02-23 17:11:27 +0000889 c->Buffer = b;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000890
891 dmesa_update_state(c->gl_ctx, 0);
892 _mesa_make_current(c->gl_ctx, b->gl_buffer);
893 if (c->gl_ctx->Viewport.Width==0) {
894 /* initialize viewport to window size */
895 _mesa_Viewport(0, 0, c->Buffer->width, c->Buffer->height);
896 }
897 } else {
898 /* Detach */
899 _mesa_make_current(NULL, NULL);
900 }
901
902 return GL_TRUE;
903}
904
905
906
907void DMesaSwapBuffers (DMesaBuffer b)
908{
909 /* copy/swap back buffer to front if applicable */
Brian Paulb43a8282002-03-08 19:27:17 +0000910 vl_flip(b->the_window, b->bwidth, b->height);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000911}