blob: 559204fc53d2642d043d43424a457bd53f9d5d1c [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 Paul6c921af2002-04-01 17:01:33 +000026 * DOS/DJGPP device driver v1.0 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 {
Brian Paul6c921af2002-04-01 17:01:33 +000079 GLframebuffer gl_buffer; /* The depth, stencil, accum, etc buffers */
Brian Pauldb41d2e2002-02-12 03:24:56 +000080 void *the_window; /* your window handle, etc */
81
Brian Paul6c921af2002-04-01 17:01:33 +000082 int bypp; /* bytes per pixel */
Brian Paule0193a92002-02-23 17:11:27 +000083 int xpos, ypos; /* position */
Brian Pauldb41d2e2002-02-12 03:24:56 +000084 int width, height; /* size in pixels */
Brian Paulb43a8282002-03-08 19:27:17 +000085 int bwidth, len; /* bytes in a line, then total */
Brian Pauldb41d2e2002-02-12 03:24:56 +000086};
87
88/*
89 * In C++ terms, this class derives from the GLcontext class.
90 * Add system-specific fields to it.
91 */
92struct dmesa_context {
93 GLcontext *gl_ctx; /* the core library context */
94 DMesaVisual visual;
95 DMesaBuffer Buffer;
96 GLuint ClearColor;
97 /* etc... */
98};
99
100
101
102static void dmesa_update_state (GLcontext *ctx, GLuint new_state);
103
104
105
106/**********************************************************************/
107/***** Read/Write pixels *****/
108/**********************************************************************/
109
110
111
Brian Paule0193a92002-02-23 17:11:27 +0000112#define FLIP(y) (c->Buffer->height - (y) - 1)
113#define FLIP2(y) (h - (y) - 1)
Brian Pauldb41d2e2002-02-12 03:24:56 +0000114
Brian Pauldb41d2e2002-02-12 03:24:56 +0000115
Brian Pauldb41d2e2002-02-12 03:24:56 +0000116
Brian Paule0193a92002-02-23 17:11:27 +0000117static void write_rgba_span (const GLcontext *ctx, GLuint n, GLint x, GLint y,
118 const GLubyte rgba[][4], const GLubyte mask[])
119{
120 DMesaContext c = (DMesaContext)ctx->DriverCtx;
121 void *b = c->Buffer->the_window;
122 GLuint i, offset;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000123
Brian Paulb43a8282002-03-08 19:27:17 +0000124 offset = c->Buffer->width * FLIP(y) + x;
Brian Paule0193a92002-02-23 17:11:27 +0000125 if (mask) {
126 /* draw some pixels */
127 for (i=0; i<n; i++, offset++) {
128 if (mask[i]) {
129 vl_putpixel(b, offset, vl_mixrgba(rgba[i]));
130 }
131 }
132 } else {
133 /* draw all pixels */
134 for (i=0; i<n; i++, offset++) {
135 vl_putpixel(b, offset, vl_mixrgba(rgba[i]));
136 }
137 }
138}
Brian Pauldb41d2e2002-02-12 03:24:56 +0000139
Brian Paule0193a92002-02-23 17:11:27 +0000140static void write_rgb_span (const GLcontext *ctx, GLuint n, GLint x, GLint y,
141 const GLubyte rgb[][3], const GLubyte mask[])
142{
143 DMesaContext c = (DMesaContext)ctx->DriverCtx;
144 void *b = c->Buffer->the_window;
145 GLuint i, offset;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000146
Brian Paulb43a8282002-03-08 19:27:17 +0000147 offset = c->Buffer->width * FLIP(y) + x;
Brian Paule0193a92002-02-23 17:11:27 +0000148 if (mask) {
149 /* draw some pixels */
150 for (i=0; i<n; i++, offset++) {
151 if (mask[i]) {
152 vl_putpixel(b, offset, vl_mixrgb(rgb[i]));
153 }
154 }
155 } else {
156 /* draw all pixels */
157 for (i=0; i<n; i++, offset++) {
158 vl_putpixel(b, offset, vl_mixrgb(rgb[i]));
159 }
160 }
161}
162
163static void write_mono_rgba_span (const GLcontext *ctx,
164 GLuint n, GLint x, GLint y,
165 const GLchan color[4], const GLubyte mask[])
166{
167 DMesaContext c = (DMesaContext)ctx->DriverCtx;
168 void *b = c->Buffer->the_window;
169 GLuint i, offset, rgba = vl_mixrgba(color);
170
Brian Paulb43a8282002-03-08 19:27:17 +0000171 offset = c->Buffer->width * FLIP(y) + x;
Brian Paule0193a92002-02-23 17:11:27 +0000172 if (mask) {
173 /* draw some pixels */
174 for (i=0; i<n; i++, offset++) {
175 if (mask[i]) {
176 vl_putpixel(b, offset, rgba);
177 }
178 }
179 } else {
180 /* draw all pixels */
181 for (i=0; i<n; i++, offset++) {
182 vl_putpixel(b, offset, rgba);
183 }
184 }
185}
186
187static void read_rgba_span (const GLcontext *ctx, GLuint n, GLint x, GLint y,
188 GLubyte rgba[][4])
189{
190 DMesaContext c = (DMesaContext)ctx->DriverCtx;
191 void *b = c->Buffer->the_window;
192 GLuint i, offset;
193
Brian Paulb43a8282002-03-08 19:27:17 +0000194 offset = c->Buffer->width * FLIP(y) + x;
Brian Paule0193a92002-02-23 17:11:27 +0000195 /* read all pixels */
196 for (i=0; i<n; i++, offset++) {
197 vl_getrgba(b, offset, rgba[i]);
198 }
199}
200
201static void write_rgba_pixels (const GLcontext *ctx,
202 GLuint n, const GLint x[], const GLint y[],
203 const GLubyte rgba[][4], const GLubyte mask[])
204{
205 DMesaContext c = (DMesaContext)ctx->DriverCtx;
206 void *b = c->Buffer->the_window;
Brian Paulb43a8282002-03-08 19:27:17 +0000207 GLuint i, w = c->Buffer->width, h = c->Buffer->height;
Brian Paule0193a92002-02-23 17:11:27 +0000208
209 if (mask) {
210 /* draw some pixels */
211 for (i=0; i<n; i++) {
212 if (mask[i]) {
213 vl_putpixel(b, FLIP2(y[i])*w + x[i], vl_mixrgba(rgba[i]));
214 }
215 }
216 } else {
217 /* draw all pixels */
218 for (i=0; i<n; i++) {
219 vl_putpixel(b, FLIP2(y[i])*w + x[i], vl_mixrgba(rgba[i]));
220 }
221 }
222}
223
224static void write_mono_rgba_pixels (const GLcontext *ctx,
225 GLuint n, const GLint x[], const GLint y[],
226 const GLchan color[4], const GLubyte mask[])
227{
228 DMesaContext c = (DMesaContext)ctx->DriverCtx;
229 void *b = c->Buffer->the_window;
Brian Paulb43a8282002-03-08 19:27:17 +0000230 GLuint i, w = c->Buffer->width, h = c->Buffer->height, rgba = vl_mixrgba(color);
Brian Paule0193a92002-02-23 17:11:27 +0000231
232 if (mask) {
233 /* draw some pixels */
234 for (i=0; i<n; i++) {
235 if (mask[i]) {
236 vl_putpixel(b, FLIP2(y[i])*w + x[i], rgba);
237 }
238 }
239 } else {
240 /* draw all pixels */
241 for (i=0; i<n; i++) {
242 vl_putpixel(b, FLIP2(y[i])*w + x[i], rgba);
243 }
244 }
245}
246
247static void read_rgba_pixels (const GLcontext *ctx,
248 GLuint n, const GLint x[], const GLint y[],
249 GLubyte rgba[][4], const GLubyte mask[])
250{
251 DMesaContext c = (DMesaContext)ctx->DriverCtx;
252 void *b = c->Buffer->the_window;
Brian Paulb43a8282002-03-08 19:27:17 +0000253 GLuint i, w = c->Buffer->width, h = c->Buffer->height;
Brian Paule0193a92002-02-23 17:11:27 +0000254
255 if (mask) {
256 /* read some pixels */
257 for (i=0; i<n; i++) {
258 if (mask[i]) {
259 vl_getrgba(b, FLIP2(y[i])*w + x[i], rgba[i]);
260 }
261 }
262 } else {
263 /* read all pixels */
264 for (i=0; i<n; i++) {
265 vl_getrgba(b, FLIP2(y[i])*w + x[i], rgba[i]);
266 }
267 }
268}
269
270
271
272/**********************************************************************/
273/***** Optimized triangle rendering *****/
274/**********************************************************************/
275
276
277
278/*
279 * flat, NON-depth-buffered, triangle.
280 */
281static void tri_rgb_flat (GLcontext *ctx,
282 const SWvertex *v0,
283 const SWvertex *v1,
284 const SWvertex *v2)
285{
286 DMesaContext c = (DMesaContext)ctx->DriverCtx;
287 void *b = c->Buffer->the_window;
Brian Paulb43a8282002-03-08 19:27:17 +0000288 GLuint w = c->Buffer->width, h = c->Buffer->height;
Brian Paule0193a92002-02-23 17:11:27 +0000289
Brian Paule0193a92002-02-23 17:11:27 +0000290#define SETUP_CODE GLuint rgb = vl_mixrgb(v2->color);
291
292#define RENDER_SPAN(span) \
Brian Paulb43a8282002-03-08 19:27:17 +0000293 GLuint i, offset = FLIP2(span.y)*w + span.x; \
294 for (i = 0; i < span.count; i++, offset++) { \
295 vl_putpixel(b, offset, rgb); \
296 }
Brian Paule0193a92002-02-23 17:11:27 +0000297
298#include "swrast/s_tritemp.h"
Brian Paule0193a92002-02-23 17:11:27 +0000299}
300
301
302
303/*
304 * flat, depth-buffered, triangle.
305 */
306static void tri_rgb_flat_z (GLcontext *ctx,
307 const SWvertex *v0,
308 const SWvertex *v1,
309 const SWvertex *v2)
310{
311 DMesaContext c = (DMesaContext)ctx->DriverCtx;
312 void *b = c->Buffer->the_window;
Brian Paulb43a8282002-03-08 19:27:17 +0000313 GLuint w = c->Buffer->width, h = c->Buffer->height;
Brian Paule0193a92002-02-23 17:11:27 +0000314
315#define INTERP_Z 1
316#define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
317#define SETUP_CODE GLuint rgb = vl_mixrgb(v2->color);
318
319#define RENDER_SPAN(span) \
Brian Paulb43a8282002-03-08 19:27:17 +0000320 GLuint i, offset = FLIP2(span.y)*w + span.x; \
321 for (i = 0; i < span.count; i++, offset++) { \
322 const DEPTH_TYPE z = FixedToDepth(span.z); \
323 if (z < zRow[i]) { \
324 vl_putpixel(b, offset, rgb); \
325 zRow[i] = z; \
326 } \
327 span.z += span.zStep; \
328 }
Brian Paule0193a92002-02-23 17:11:27 +0000329
330#include "swrast/s_tritemp.h"
331}
332
333
334
335/*
336 * smooth, NON-depth-buffered, triangle.
337 */
338static void tri_rgb_smooth (GLcontext *ctx,
339 const SWvertex *v0,
340 const SWvertex *v1,
341 const SWvertex *v2)
342{
343 DMesaContext c = (DMesaContext)ctx->DriverCtx;
344 void *b = c->Buffer->the_window;
Brian Paulb43a8282002-03-08 19:27:17 +0000345 GLuint w = c->Buffer->width, h = c->Buffer->height;
Brian Paule0193a92002-02-23 17:11:27 +0000346
347#define INTERP_RGB 1
348#define RENDER_SPAN(span) \
Brian Paulb43a8282002-03-08 19:27:17 +0000349 GLuint i, offset = FLIP2(span.y)*w + span.x; \
350 for (i = 0; i < span.count; i++, offset++) { \
351 unsigned char rgb[3]; \
352 rgb[0] = FixedToInt(span.red); \
353 rgb[1] = FixedToInt(span.green); \
354 rgb[2] = FixedToInt(span.blue); \
355 vl_putpixel(b, offset, vl_mixrgb(rgb)); \
356 span.red += span.redStep; \
357 span.green += span.greenStep; \
358 span.blue += span.blueStep; \
359 }
Brian Paule0193a92002-02-23 17:11:27 +0000360
361#include "swrast/s_tritemp.h"
362}
363
364
365
366/*
367 * smooth, depth-buffered, triangle.
368 */
369static void tri_rgb_smooth_z (GLcontext *ctx,
370 const SWvertex *v0,
371 const SWvertex *v1,
372 const SWvertex *v2)
373{
374 DMesaContext c = (DMesaContext)ctx->DriverCtx;
375 void *b = c->Buffer->the_window;
Brian Paulb43a8282002-03-08 19:27:17 +0000376 GLuint w = c->Buffer->width, h = c->Buffer->height;
Brian Paule0193a92002-02-23 17:11:27 +0000377
378#define INTERP_Z 1
379#define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
380#define INTERP_RGB 1
381
382#define RENDER_SPAN(span) \
Brian Paulb43a8282002-03-08 19:27:17 +0000383 GLuint i, offset = FLIP2(span.y)*w + span.x; \
384 for (i = 0; i < span.count; i++, offset++) { \
385 const DEPTH_TYPE z = FixedToDepth(span.z); \
386 if (z < zRow[i]) { \
387 unsigned char rgb[3]; \
388 rgb[0] = FixedToInt(span.red); \
389 rgb[1] = FixedToInt(span.green); \
390 rgb[2] = FixedToInt(span.blue); \
391 vl_putpixel(b, offset, vl_mixrgb(rgb)); \
392 zRow[i] = z; \
393 } \
394 span.red += span.redStep; \
395 span.green += span.greenStep; \
396 span.blue += span.blueStep; \
397 span.z += span.zStep; \
398 }
Brian Paule0193a92002-02-23 17:11:27 +0000399
400#include "swrast/s_tritemp.h"
401}
402
403
404
405/*
406 * Analyze context state to see if we can provide a fast triangle function
407 * Otherwise, return NULL.
408 */
409static swrast_tri_func dmesa_choose_tri_function (GLcontext *ctx)
410{
411 const SWcontext *swrast = SWRAST_CONTEXT(ctx);
412
413 if (ctx->RenderMode != GL_RENDER) return (swrast_tri_func) NULL;
414 if (ctx->Polygon.SmoothFlag) return (swrast_tri_func) NULL;
415 if (ctx->Texture._ReallyEnabled) return (swrast_tri_func) NULL;
416
417 if (ctx->Light.ShadeModel==GL_SMOOTH
418 && swrast->_RasterMask==DEPTH_BIT
419 && ctx->Depth.Func==GL_LESS
420 && ctx->Depth.Mask==GL_TRUE
421 && ctx->Visual.depthBits == DEFAULT_SOFTWARE_DEPTH_BITS
422 && ctx->Polygon.StippleFlag==GL_FALSE) {
423 return tri_rgb_smooth_z;
424 }
425 if (ctx->Light.ShadeModel==GL_FLAT
426 && swrast->_RasterMask==DEPTH_BIT
427 && ctx->Depth.Func==GL_LESS
428 && ctx->Depth.Mask==GL_TRUE
429 && ctx->Visual.depthBits == DEFAULT_SOFTWARE_DEPTH_BITS
430 && ctx->Polygon.StippleFlag==GL_FALSE) {
431 return tri_rgb_flat_z;
432 }
433 if (swrast->_RasterMask==0 /* no depth test */
434 && ctx->Light.ShadeModel==GL_SMOOTH
435 && ctx->Polygon.StippleFlag==GL_FALSE) {
436 return tri_rgb_smooth;
437 }
438 if (swrast->_RasterMask==0 /* no depth test */
439 && ctx->Light.ShadeModel==GL_FLAT
440 && ctx->Polygon.StippleFlag==GL_FALSE) {
441 return tri_rgb_flat;
442 }
443
444 return (swrast_tri_func)NULL;
445}
446
447
448
449/* Override for the swrast triangle-selection function. Try to use one
450 * of our internal line functions, otherwise fall back to the
451 * standard swrast functions.
452 */
453static void dmesa_choose_tri (GLcontext *ctx)
454{
455 SWcontext *swrast = SWRAST_CONTEXT(ctx);
456
457 if (!(swrast->Triangle=dmesa_choose_tri_function(ctx)))
458 _swrast_choose_triangle(ctx);
459}
Brian Pauldb41d2e2002-02-12 03:24:56 +0000460
461
462
463/**********************************************************************/
464/***** Miscellaneous device driver funcs *****/
465/**********************************************************************/
466
467
468
469static void clear_color (GLcontext *ctx, const GLchan color[4])
470{
471 DMesaContext c = (DMesaContext)ctx->DriverCtx;
Brian Paule0193a92002-02-23 17:11:27 +0000472 c->ClearColor = vl_mixrgba(color);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000473}
474
475
476
477static void clear (GLcontext *ctx, GLbitfield mask, GLboolean all,
478 GLint x, GLint y, GLint width, GLint height)
479{
480 DMesaContext c = (DMesaContext)ctx->DriverCtx;
481 const GLuint *colorMask = (GLuint *)&ctx->Color.ColorMask;
482 DMesaBuffer b = c->Buffer;
483
484/*
485 * Clear the specified region of the buffers indicated by 'mask'
486 * using the clear color or index as specified by one of the two
487 * functions above.
488 * If all==GL_TRUE, clear whole buffer, else just clear region defined
489 * by x,y,width,height
490 */
491
492 /* we can't handle color or index masking */
Brian Paule0193a92002-02-23 17:11:27 +0000493 if (*colorMask==0xffffffff) {
494 if (mask & DD_BACK_LEFT_BIT) {
Brian Pauldb41d2e2002-02-12 03:24:56 +0000495 if (all) {
Brian Paulb43a8282002-03-08 19:27:17 +0000496 vl_clear(b->the_window, b->len, c->ClearColor);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000497 } else {
Brian Paulb43a8282002-03-08 19:27:17 +0000498 vl_rect(b->the_window, x, y, width, height, c->ClearColor);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000499 }
500 mask &= ~DD_BACK_LEFT_BIT;
501 }
502 }
503
504 if (mask) {
505 _swrast_Clear(ctx, mask, all, x, y, width, height);
506 }
507}
508
509
510
511/*
512 * Set the current reading buffer.
513 */
514static void set_read_buffer (GLcontext *ctx, GLframebuffer *buffer,
515 GLenum mode)
516{
517/*
518 DMesaContext c = (DMesaContext)ctx->DriverCtx;
519 dmesa_update_state(ctx);
520*/
521}
522
523
524
525/*
526 * Set the destination/draw buffer.
527 */
528static GLboolean set_draw_buffer (GLcontext *ctx, GLenum mode)
529{
Brian Paulb43a8282002-03-08 19:27:17 +0000530 if (mode==GL_BACK_LEFT) {
Brian Pauldb41d2e2002-02-12 03:24:56 +0000531 return GL_TRUE;
532 } else {
533 return GL_FALSE;
534 }
535}
536
537
538
539/*
540 * Return the width and height of the current buffer.
541 * If anything special has to been done when the buffer/window is
542 * resized, do it now.
543 */
Brian Paul6c921af2002-04-01 17:01:33 +0000544static void get_buffer_size (GLframebuffer *buffer, GLuint *width, GLuint *height)
Brian Pauldb41d2e2002-02-12 03:24:56 +0000545{
Brian Paul6c921af2002-04-01 17:01:33 +0000546 DMesaBuffer b = (DMesaBuffer)buffer;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000547
Brian Paul6c921af2002-04-01 17:01:33 +0000548 *width = b->width;
549 *height = b->height;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000550}
551
552
553
554static const GLubyte* get_string (GLcontext *ctx, GLenum name)
555{
556 switch (name) {
557 case GL_RENDERER:
Brian Paul6c921af2002-04-01 17:01:33 +0000558 return (const GLubyte *)"Mesa DOS\0DJGPP port (c) Borca Daniel 31-mar-2002";
Brian Pauldb41d2e2002-02-12 03:24:56 +0000559 default:
560 return NULL;
561 }
562}
563
564
565
566/**********************************************************************/
567/***** Miscellaneous device driver funcs *****/
568/***** Note that these functions are mandatory *****/
569/**********************************************************************/
570
571
572
573/* OPTIONAL FUNCTION: implements glFinish if possible */
574static void finish (GLcontext *ctx)
575{
576/*
577 DMesaContext c = (DMesaContext)ctx->DriverCtx;
578*/
579}
580
581
582
583/* OPTIONAL FUNCTION: implements glFlush if possible */
584static void flush (GLcontext *ctx)
585{
586/*
587 DMesaContext c = (DMesaContext)ctx->DriverCtx;
588*/
589}
590
591
592
593/**********************************************************************/
594/**********************************************************************/
595
596
597
Brian Paule0193a92002-02-23 17:11:27 +0000598#define DMESA_NEW_TRIANGLE (_NEW_POLYGON | \
599 _NEW_TEXTURE | \
600 _NEW_LIGHT | \
601 _NEW_DEPTH | \
602 _NEW_RENDERMODE | \
603 _SWRAST_NEW_RASTERMASK)
604
605
606
607/* Extend the software rasterizer with our line and triangle
608 * functions.
609 */
610static void dmesa_register_swrast_functions (GLcontext *ctx)
611{
612 SWcontext *swrast = SWRAST_CONTEXT(ctx);
613
614 swrast->choose_triangle = dmesa_choose_tri;
615
616 swrast->invalidate_triangle |= DMESA_NEW_TRIANGLE;
617}
618
619
620
Brian Pauldb41d2e2002-02-12 03:24:56 +0000621/* Setup pointers and other driver state that is constant for the life
622 * of a context.
623 */
624void dmesa_init_pointers (GLcontext *ctx)
625{
626 TNLcontext *tnl;
627
628 ctx->Driver.UpdateState = dmesa_update_state;
629
630 ctx->Driver.GetString = get_string;
631 ctx->Driver.GetBufferSize = get_buffer_size;
632 ctx->Driver.Flush = flush;
633 ctx->Driver.Finish = finish;
634
635 /* Software rasterizer pixel paths:
636 */
637 ctx->Driver.Accum = _swrast_Accum;
638 ctx->Driver.Bitmap = _swrast_Bitmap;
639 ctx->Driver.Clear = clear;
Brian Paul6c921af2002-04-01 17:01:33 +0000640 ctx->Driver.ResizeBuffers = _swrast_alloc_buffers;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000641 ctx->Driver.CopyPixels = _swrast_CopyPixels;
642 ctx->Driver.DrawPixels = _swrast_DrawPixels;
643 ctx->Driver.ReadPixels = _swrast_ReadPixels;
644
645 /* Software texture functions:
646 */
647 ctx->Driver.ChooseTextureFormat = _mesa_choose_tex_format;
648 ctx->Driver.TexImage1D = _mesa_store_teximage1d;
649 ctx->Driver.TexImage2D = _mesa_store_teximage2d;
650 ctx->Driver.TexImage3D = _mesa_store_teximage3d;
651 ctx->Driver.TexSubImage1D = _mesa_store_texsubimage1d;
652 ctx->Driver.TexSubImage2D = _mesa_store_texsubimage2d;
653 ctx->Driver.TexSubImage3D = _mesa_store_texsubimage3d;
654 ctx->Driver.TestProxyTexImage = _mesa_test_proxy_teximage;
655
656 ctx->Driver.CopyTexImage1D = _swrast_copy_teximage1d;
657 ctx->Driver.CopyTexImage2D = _swrast_copy_teximage2d;
658 ctx->Driver.CopyTexSubImage1D = _swrast_copy_texsubimage1d;
659 ctx->Driver.CopyTexSubImage2D = _swrast_copy_texsubimage2d;
660 ctx->Driver.CopyTexSubImage3D = _swrast_copy_texsubimage3d;
661
662 ctx->Driver.BaseCompressedTexFormat = _mesa_base_compressed_texformat;
663 ctx->Driver.CompressedTextureSize = _mesa_compressed_texture_size;
664 ctx->Driver.GetCompressedTexImage = _mesa_get_compressed_teximage;
665
666 /* Swrast hooks for imaging extensions:
667 */
668 ctx->Driver.CopyColorTable = _swrast_CopyColorTable;
669 ctx->Driver.CopyColorSubTable = _swrast_CopyColorSubTable;
670 ctx->Driver.CopyConvolutionFilter1D = _swrast_CopyConvolutionFilter1D;
671 ctx->Driver.CopyConvolutionFilter2D = _swrast_CopyConvolutionFilter2D;
672
673 /* Statechange callbacks:
674 */
675 ctx->Driver.SetDrawBuffer = set_draw_buffer;
676 ctx->Driver.ClearColor = clear_color;
677
678 /* Initialize the TNL driver interface:
679 */
680 tnl = TNL_CONTEXT(ctx);
681 tnl->Driver.RunPipeline = _tnl_run_pipeline;
682
683 /* Install swsetup for tnl->Driver.Render.*:
684 */
685 _swsetup_Wakeup(ctx);
686}
687
688
689
690static void dmesa_update_state (GLcontext *ctx, GLuint new_state)
691{
692 DMesaContext c = (DMesaContext)ctx->DriverCtx;
693 struct swrast_device_driver *swdd = _swrast_GetDeviceDriverReference(ctx);
694
695 /* Initialize all the pointers in the DD struct. Do this whenever */
696 /* a new context is made current or we change buffers via set_buffer! */
697
698 _swrast_InvalidateState(ctx, new_state);
699 _swsetup_InvalidateState(ctx, new_state);
700 _ac_InvalidateState(ctx, new_state);
701 _tnl_InvalidateState(ctx, new_state);
702
703 swdd->SetReadBuffer = set_read_buffer;
704
705 /* RGB(A) span/pixel functions */
Brian Paule0193a92002-02-23 17:11:27 +0000706 swdd->WriteRGBASpan = write_rgba_span;
707 swdd->WriteRGBSpan = write_rgb_span;
708 swdd->WriteMonoRGBASpan = write_mono_rgba_span;
709 swdd->WriteRGBAPixels = write_rgba_pixels;
710 swdd->WriteMonoRGBAPixels = write_mono_rgba_pixels;
711 swdd->ReadRGBASpan = read_rgba_span;
712 swdd->ReadRGBAPixels = read_rgba_pixels;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000713}
714
715
716
717/**********************************************************************/
718/***** DMesa Public API Functions *****/
719/**********************************************************************/
720
721
722
723/*
724 * The exact arguments to this function will depend on your window system
725 */
Brian Paule0193a92002-02-23 17:11:27 +0000726DMesaVisual DMesaCreateVisual (GLint width, GLint height, GLint colDepth,
727 GLboolean dbFlag, GLint depthSize,
728 GLint stencilSize,
Brian Pauldb41d2e2002-02-12 03:24:56 +0000729 GLint accumSize)
730{
731 DMesaVisual v;
732 GLint redBits, greenBits, blueBits, alphaBits;
733
Brian Paulb43a8282002-03-08 19:27:17 +0000734 if (!dbFlag) {
735 return NULL;
736 }
Brian Paule0193a92002-02-23 17:11:27 +0000737 alphaBits = 0;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000738 switch (colDepth) {
739 case 15:
740 redBits = 5;
741 greenBits = 5;
742 blueBits = 5;
743 break;
744 case 16:
745 redBits = 5;
746 greenBits = 6;
747 blueBits = 5;
748 break;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000749 case 32:
Brian Paule0193a92002-02-23 17:11:27 +0000750 alphaBits = 8;
751 case 24:
Brian Pauldb41d2e2002-02-12 03:24:56 +0000752 redBits = 8;
753 greenBits = 8;
754 blueBits = 8;
755 break;
756 default:
757 return NULL;
758 }
Brian Paule0193a92002-02-23 17:11:27 +0000759
Brian Paulb43a8282002-03-08 19:27:17 +0000760 if (vl_video_init(width, height, colDepth)!=0) {
Brian Paule0193a92002-02-23 17:11:27 +0000761 return NULL;
762 }
Brian Pauldb41d2e2002-02-12 03:24:56 +0000763
764 if ((v=(DMesaVisual)calloc(1, sizeof(struct dmesa_visual)))!=NULL) {
765 /* Create core visual */
766 v->gl_visual = _mesa_create_visual(colDepth>8, /* rgb */
767 dbFlag,
768 GL_FALSE, /* stereo */
769 redBits,
770 greenBits,
771 blueBits,
772 alphaBits,
773 0, /* indexBits */
774 depthSize,
775 stencilSize,
776 accumSize, /* accumRed */
777 accumSize, /* accumGreen */
778 accumSize, /* accumBlue */
779 alphaBits?accumSize:0, /* accumAlpha */
780 1); /* numSamples */
781
782 v->depth = colDepth;
Brian Paulb43a8282002-03-08 19:27:17 +0000783 v->db_flag = dbFlag;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000784 }
785
786 return v;
787}
788
789
790
791void DMesaDestroyVisual (DMesaVisual v)
792{
Brian Paulb43a8282002-03-08 19:27:17 +0000793 vl_video_exit(!0);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000794 _mesa_destroy_visual(v->gl_visual);
795 free(v);
796}
797
798
799
800DMesaBuffer DMesaCreateBuffer (DMesaVisual visual,
Brian Paule0193a92002-02-23 17:11:27 +0000801 GLint xpos, GLint ypos,
802 GLint width, GLint height)
Brian Pauldb41d2e2002-02-12 03:24:56 +0000803{
804 DMesaBuffer b;
805
806 if ((b=(DMesaBuffer)calloc(1, sizeof(struct dmesa_buffer)))!=NULL) {
Brian Pauldb41d2e2002-02-12 03:24:56 +0000807
Brian Paul6c921af2002-04-01 17:01:33 +0000808 _mesa_initialize_framebuffer(&b->gl_buffer,
809 visual->gl_visual,
810 visual->gl_visual->depthBits > 0,
811 visual->gl_visual->stencilBits > 0,
812 visual->gl_visual->accumRedBits > 0,
813 visual->gl_visual->alphaBits > 0);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000814 b->xpos = xpos;
815 b->ypos = ypos;
Brian Paule0193a92002-02-23 17:11:27 +0000816 b->width = width;
817 b->height = height;
Brian Paul6c921af2002-04-01 17:01:33 +0000818 b->bypp = (visual->depth+7)/8;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000819 }
820
821 return b;
822}
823
824
825
826void DMesaDestroyBuffer (DMesaBuffer b)
827{
Brian Paulb43a8282002-03-08 19:27:17 +0000828 free(b->the_window);
Brian Paul6c921af2002-04-01 17:01:33 +0000829 _mesa_free_framebuffer_data(&b->gl_buffer);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000830 free(b);
831}
832
833
834
835DMesaContext DMesaCreateContext (DMesaVisual visual,
836 DMesaContext share)
837{
838 DMesaContext c;
839 GLboolean direct = GL_FALSE;
840
841 if ((c=(DMesaContext)calloc(1, sizeof(struct dmesa_context)))!=NULL) {
842 c->gl_ctx = _mesa_create_context(visual->gl_visual,
843 share ? share->gl_ctx : NULL,
844 (void *)c, direct);
845
Brian Paule0193a92002-02-23 17:11:27 +0000846 _mesa_enable_sw_extensions(c->gl_ctx);
847 _mesa_enable_1_3_extensions(c->gl_ctx);
848
Brian Pauldb41d2e2002-02-12 03:24:56 +0000849 /* you probably have to do a bunch of other initializations here. */
850 c->visual = visual;
851
852 /* Initialize the software rasterizer and helper modules.
853 */
854 _swrast_CreateContext(c->gl_ctx);
855 _ac_CreateContext(c->gl_ctx);
856 _tnl_CreateContext(c->gl_ctx);
857 _swsetup_CreateContext(c->gl_ctx);
858 dmesa_init_pointers(c->gl_ctx);
Brian Paule0193a92002-02-23 17:11:27 +0000859 dmesa_register_swrast_functions(c->gl_ctx);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000860 }
861
862 return c;
863}
864
865
866
867void DMesaDestroyContext (DMesaContext c)
868{
869 _mesa_destroy_context(c->gl_ctx);
870 free(c);
871}
872
873
874
Brian Paul6c921af2002-04-01 17:01:33 +0000875GLboolean DMesaViewport (DMesaBuffer b,
876 GLint xpos, GLint ypos,
877 GLint width, GLint height)
878{
879 void *new_window;
880
881 if ((new_window=vl_sync_buffer(b->the_window, xpos, ypos, width, height))==NULL) {
882 return GL_FALSE;
883 } else {
884 b->the_window = new_window;
885 b->xpos = xpos;
886 b->ypos = ypos;
887 b->width = width;
888 b->height = height;
889 b->bwidth = width * b->bypp;
890 b->len = b->bwidth * height;
891 return GL_TRUE;
892 }
893}
894
895
896
Brian Pauldb41d2e2002-02-12 03:24:56 +0000897/*
898 * Make the specified context and buffer the current one.
899 */
900GLboolean DMesaMakeCurrent (DMesaContext c, DMesaBuffer b)
901{
902 if (c&&b) {
Brian Paul6c921af2002-04-01 17:01:33 +0000903 if (!DMesaViewport(b, b->xpos, b->ypos, b->width, b->height)) {
Brian Pauldb41d2e2002-02-12 03:24:56 +0000904 return GL_FALSE;
905 }
906
Brian Paule0193a92002-02-23 17:11:27 +0000907 c->Buffer = b;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000908
909 dmesa_update_state(c->gl_ctx, 0);
Brian Paul6c921af2002-04-01 17:01:33 +0000910 _mesa_make_current(c->gl_ctx, &b->gl_buffer);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000911 if (c->gl_ctx->Viewport.Width==0) {
912 /* initialize viewport to window size */
Brian Paul6c921af2002-04-01 17:01:33 +0000913 _mesa_Viewport(0, 0, b->width, b->height);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000914 }
915 } else {
916 /* Detach */
917 _mesa_make_current(NULL, NULL);
918 }
919
920 return GL_TRUE;
921}
922
923
924
925void DMesaSwapBuffers (DMesaBuffer b)
926{
927 /* copy/swap back buffer to front if applicable */
Brian Paulb43a8282002-03-08 19:27:17 +0000928 vl_flip(b->the_window, b->bwidth, b->height);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000929}