blob: d05d436c1e202da86438ad904e3a6c220836ddee [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"
Brian Paul9a33a112002-06-13 04:28:29 +000041#inlcude "imports.h"
Brian Paule0193a92002-02-23 17:11:27 +000042#include "macros.h"
Brian Pauldb41d2e2002-02-12 03:24:56 +000043#include "matrix.h"
Brian Paule0193a92002-02-23 17:11:27 +000044#include "mmath.h"
Brian Pauldb41d2e2002-02-12 03:24:56 +000045#include "texformat.h"
46#include "texstore.h"
47#include "array_cache/acache.h"
Brian Paule0193a92002-02-23 17:11:27 +000048#include "swrast/s_context.h"
49#include "swrast/s_depth.h"
50#include "swrast/s_lines.h"
51#include "swrast/s_triangle.h"
52#include "swrast/s_trispan.h"
Brian Pauldb41d2e2002-02-12 03:24:56 +000053#include "swrast/swrast.h"
54#include "swrast_setup/swrast_setup.h"
55#include "tnl/tnl.h"
56#include "tnl/t_context.h"
57#include "tnl/t_pipeline.h"
58#endif
59
Brian Paule0193a92002-02-23 17:11:27 +000060#include "video.h"
Brian Pauldb41d2e2002-02-12 03:24:56 +000061
62
63
64/*
65 * In C++ terms, this class derives from the GLvisual class.
66 * Add system-specific fields to it.
67 */
68struct dmesa_visual {
69 GLvisual *gl_visual;
Brian Paulb43a8282002-03-08 19:27:17 +000070 GLboolean db_flag; /* double buffered? */
Brian Pauldb41d2e2002-02-12 03:24:56 +000071 GLboolean rgb_flag; /* RGB mode? */
72 GLuint depth; /* bits per pixel (1, 8, 24, etc) */
73};
74
75/*
76 * In C++ terms, this class derives from the GLframebuffer class.
77 * Add system-specific fields to it.
78 */
79struct dmesa_buffer {
Brian Paul6c921af2002-04-01 17:01:33 +000080 GLframebuffer gl_buffer; /* The depth, stencil, accum, etc buffers */
Brian Pauldb41d2e2002-02-12 03:24:56 +000081 void *the_window; /* your window handle, etc */
82
Brian Paul6c921af2002-04-01 17:01:33 +000083 int bypp; /* bytes per pixel */
Brian Paule0193a92002-02-23 17:11:27 +000084 int xpos, ypos; /* position */
Brian Pauldb41d2e2002-02-12 03:24:56 +000085 int width, height; /* size in pixels */
Brian Paulb43a8282002-03-08 19:27:17 +000086 int bwidth, len; /* bytes in a line, then total */
Brian Pauldb41d2e2002-02-12 03:24:56 +000087};
88
89/*
90 * In C++ terms, this class derives from the GLcontext class.
91 * Add system-specific fields to it.
92 */
93struct dmesa_context {
94 GLcontext *gl_ctx; /* the core library context */
95 DMesaVisual visual;
96 DMesaBuffer Buffer;
97 GLuint ClearColor;
98 /* etc... */
99};
100
101
102
103static void dmesa_update_state (GLcontext *ctx, GLuint new_state);
104
105
106
107/**********************************************************************/
108/***** Read/Write pixels *****/
109/**********************************************************************/
110
111
112
Brian Paule0193a92002-02-23 17:11:27 +0000113#define FLIP(y) (c->Buffer->height - (y) - 1)
114#define FLIP2(y) (h - (y) - 1)
Brian Pauldb41d2e2002-02-12 03:24:56 +0000115
Brian Pauldb41d2e2002-02-12 03:24:56 +0000116
Brian Pauldb41d2e2002-02-12 03:24:56 +0000117
Brian Paule0193a92002-02-23 17:11:27 +0000118static void write_rgba_span (const GLcontext *ctx, GLuint n, GLint x, GLint y,
119 const GLubyte rgba[][4], const GLubyte mask[])
120{
121 DMesaContext c = (DMesaContext)ctx->DriverCtx;
122 void *b = c->Buffer->the_window;
123 GLuint i, offset;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000124
Brian Paulb43a8282002-03-08 19:27:17 +0000125 offset = c->Buffer->width * FLIP(y) + x;
Brian Paule0193a92002-02-23 17:11:27 +0000126 if (mask) {
127 /* draw some pixels */
128 for (i=0; i<n; i++, offset++) {
129 if (mask[i]) {
130 vl_putpixel(b, offset, vl_mixrgba(rgba[i]));
131 }
132 }
133 } else {
134 /* draw all pixels */
135 for (i=0; i<n; i++, offset++) {
136 vl_putpixel(b, offset, vl_mixrgba(rgba[i]));
137 }
138 }
139}
Brian Pauldb41d2e2002-02-12 03:24:56 +0000140
Brian Paule0193a92002-02-23 17:11:27 +0000141static void write_rgb_span (const GLcontext *ctx, GLuint n, GLint x, GLint y,
142 const GLubyte rgb[][3], const GLubyte mask[])
143{
144 DMesaContext c = (DMesaContext)ctx->DriverCtx;
145 void *b = c->Buffer->the_window;
146 GLuint i, offset;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000147
Brian Paulb43a8282002-03-08 19:27:17 +0000148 offset = c->Buffer->width * FLIP(y) + x;
Brian Paule0193a92002-02-23 17:11:27 +0000149 if (mask) {
150 /* draw some pixels */
151 for (i=0; i<n; i++, offset++) {
152 if (mask[i]) {
153 vl_putpixel(b, offset, vl_mixrgb(rgb[i]));
154 }
155 }
156 } else {
157 /* draw all pixels */
158 for (i=0; i<n; i++, offset++) {
159 vl_putpixel(b, offset, vl_mixrgb(rgb[i]));
160 }
161 }
162}
163
164static void write_mono_rgba_span (const GLcontext *ctx,
165 GLuint n, GLint x, GLint y,
166 const GLchan color[4], const GLubyte mask[])
167{
168 DMesaContext c = (DMesaContext)ctx->DriverCtx;
169 void *b = c->Buffer->the_window;
170 GLuint i, offset, rgba = vl_mixrgba(color);
171
Brian Paulb43a8282002-03-08 19:27:17 +0000172 offset = c->Buffer->width * FLIP(y) + x;
Brian Paule0193a92002-02-23 17:11:27 +0000173 if (mask) {
174 /* draw some pixels */
175 for (i=0; i<n; i++, offset++) {
176 if (mask[i]) {
177 vl_putpixel(b, offset, rgba);
178 }
179 }
180 } else {
181 /* draw all pixels */
182 for (i=0; i<n; i++, offset++) {
183 vl_putpixel(b, offset, rgba);
184 }
185 }
186}
187
188static void read_rgba_span (const GLcontext *ctx, GLuint n, GLint x, GLint y,
189 GLubyte rgba[][4])
190{
191 DMesaContext c = (DMesaContext)ctx->DriverCtx;
192 void *b = c->Buffer->the_window;
193 GLuint i, offset;
194
Brian Paulb43a8282002-03-08 19:27:17 +0000195 offset = c->Buffer->width * FLIP(y) + x;
Brian Paule0193a92002-02-23 17:11:27 +0000196 /* read all pixels */
197 for (i=0; i<n; i++, offset++) {
198 vl_getrgba(b, offset, rgba[i]);
199 }
200}
201
202static void write_rgba_pixels (const GLcontext *ctx,
203 GLuint n, const GLint x[], const GLint y[],
204 const GLubyte rgba[][4], const GLubyte mask[])
205{
206 DMesaContext c = (DMesaContext)ctx->DriverCtx;
207 void *b = c->Buffer->the_window;
Brian Paulb43a8282002-03-08 19:27:17 +0000208 GLuint i, w = c->Buffer->width, h = c->Buffer->height;
Brian Paule0193a92002-02-23 17:11:27 +0000209
210 if (mask) {
211 /* draw some pixels */
212 for (i=0; i<n; i++) {
213 if (mask[i]) {
214 vl_putpixel(b, FLIP2(y[i])*w + x[i], vl_mixrgba(rgba[i]));
215 }
216 }
217 } else {
218 /* draw all pixels */
219 for (i=0; i<n; i++) {
220 vl_putpixel(b, FLIP2(y[i])*w + x[i], vl_mixrgba(rgba[i]));
221 }
222 }
223}
224
225static void write_mono_rgba_pixels (const GLcontext *ctx,
226 GLuint n, const GLint x[], const GLint y[],
227 const GLchan color[4], const GLubyte mask[])
228{
229 DMesaContext c = (DMesaContext)ctx->DriverCtx;
230 void *b = c->Buffer->the_window;
Brian Paulb43a8282002-03-08 19:27:17 +0000231 GLuint i, w = c->Buffer->width, h = c->Buffer->height, rgba = vl_mixrgba(color);
Brian Paule0193a92002-02-23 17:11:27 +0000232
233 if (mask) {
234 /* draw some pixels */
235 for (i=0; i<n; i++) {
236 if (mask[i]) {
237 vl_putpixel(b, FLIP2(y[i])*w + x[i], rgba);
238 }
239 }
240 } else {
241 /* draw all pixels */
242 for (i=0; i<n; i++) {
243 vl_putpixel(b, FLIP2(y[i])*w + x[i], rgba);
244 }
245 }
246}
247
248static void read_rgba_pixels (const GLcontext *ctx,
249 GLuint n, const GLint x[], const GLint y[],
250 GLubyte rgba[][4], const GLubyte mask[])
251{
252 DMesaContext c = (DMesaContext)ctx->DriverCtx;
253 void *b = c->Buffer->the_window;
Brian Paulb43a8282002-03-08 19:27:17 +0000254 GLuint i, w = c->Buffer->width, h = c->Buffer->height;
Brian Paule0193a92002-02-23 17:11:27 +0000255
256 if (mask) {
257 /* read some pixels */
258 for (i=0; i<n; i++) {
259 if (mask[i]) {
260 vl_getrgba(b, FLIP2(y[i])*w + x[i], rgba[i]);
261 }
262 }
263 } else {
264 /* read all pixels */
265 for (i=0; i<n; i++) {
266 vl_getrgba(b, FLIP2(y[i])*w + x[i], rgba[i]);
267 }
268 }
269}
270
271
272
273/**********************************************************************/
274/***** Optimized triangle rendering *****/
275/**********************************************************************/
276
277
278
279/*
280 * flat, NON-depth-buffered, triangle.
281 */
282static void tri_rgb_flat (GLcontext *ctx,
283 const SWvertex *v0,
284 const SWvertex *v1,
285 const SWvertex *v2)
286{
287 DMesaContext c = (DMesaContext)ctx->DriverCtx;
288 void *b = c->Buffer->the_window;
Brian Paulb43a8282002-03-08 19:27:17 +0000289 GLuint w = c->Buffer->width, h = c->Buffer->height;
Brian Paule0193a92002-02-23 17:11:27 +0000290
Brian Paule0193a92002-02-23 17:11:27 +0000291#define SETUP_CODE GLuint rgb = vl_mixrgb(v2->color);
292
293#define RENDER_SPAN(span) \
Brian Paulb43a8282002-03-08 19:27:17 +0000294 GLuint i, offset = FLIP2(span.y)*w + span.x; \
295 for (i = 0; i < span.count; i++, offset++) { \
296 vl_putpixel(b, offset, rgb); \
297 }
Brian Paule0193a92002-02-23 17:11:27 +0000298
299#include "swrast/s_tritemp.h"
Brian Paule0193a92002-02-23 17:11:27 +0000300}
301
302
303
304/*
305 * flat, depth-buffered, triangle.
306 */
307static void tri_rgb_flat_z (GLcontext *ctx,
308 const SWvertex *v0,
309 const SWvertex *v1,
310 const SWvertex *v2)
311{
312 DMesaContext c = (DMesaContext)ctx->DriverCtx;
313 void *b = c->Buffer->the_window;
Brian Paulb43a8282002-03-08 19:27:17 +0000314 GLuint w = c->Buffer->width, h = c->Buffer->height;
Brian Paule0193a92002-02-23 17:11:27 +0000315
316#define INTERP_Z 1
317#define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
318#define SETUP_CODE GLuint rgb = vl_mixrgb(v2->color);
319
320#define RENDER_SPAN(span) \
Brian Paulb43a8282002-03-08 19:27:17 +0000321 GLuint i, offset = FLIP2(span.y)*w + span.x; \
322 for (i = 0; i < span.count; i++, offset++) { \
323 const DEPTH_TYPE z = FixedToDepth(span.z); \
324 if (z < zRow[i]) { \
325 vl_putpixel(b, offset, rgb); \
326 zRow[i] = z; \
327 } \
328 span.z += span.zStep; \
329 }
Brian Paule0193a92002-02-23 17:11:27 +0000330
331#include "swrast/s_tritemp.h"
332}
333
334
335
336/*
337 * smooth, NON-depth-buffered, triangle.
338 */
339static void tri_rgb_smooth (GLcontext *ctx,
340 const SWvertex *v0,
341 const SWvertex *v1,
342 const SWvertex *v2)
343{
344 DMesaContext c = (DMesaContext)ctx->DriverCtx;
345 void *b = c->Buffer->the_window;
Brian Paulb43a8282002-03-08 19:27:17 +0000346 GLuint w = c->Buffer->width, h = c->Buffer->height;
Brian Paule0193a92002-02-23 17:11:27 +0000347
348#define INTERP_RGB 1
349#define RENDER_SPAN(span) \
Brian Paulb43a8282002-03-08 19:27:17 +0000350 GLuint i, offset = FLIP2(span.y)*w + span.x; \
351 for (i = 0; i < span.count; i++, offset++) { \
352 unsigned char rgb[3]; \
353 rgb[0] = FixedToInt(span.red); \
354 rgb[1] = FixedToInt(span.green); \
355 rgb[2] = FixedToInt(span.blue); \
356 vl_putpixel(b, offset, vl_mixrgb(rgb)); \
357 span.red += span.redStep; \
358 span.green += span.greenStep; \
359 span.blue += span.blueStep; \
360 }
Brian Paule0193a92002-02-23 17:11:27 +0000361
362#include "swrast/s_tritemp.h"
363}
364
365
366
367/*
368 * smooth, depth-buffered, triangle.
369 */
370static void tri_rgb_smooth_z (GLcontext *ctx,
371 const SWvertex *v0,
372 const SWvertex *v1,
373 const SWvertex *v2)
374{
375 DMesaContext c = (DMesaContext)ctx->DriverCtx;
376 void *b = c->Buffer->the_window;
Brian Paulb43a8282002-03-08 19:27:17 +0000377 GLuint w = c->Buffer->width, h = c->Buffer->height;
Brian Paule0193a92002-02-23 17:11:27 +0000378
379#define INTERP_Z 1
380#define DEPTH_TYPE DEFAULT_SOFTWARE_DEPTH_TYPE
381#define INTERP_RGB 1
382
383#define RENDER_SPAN(span) \
Brian Paulb43a8282002-03-08 19:27:17 +0000384 GLuint i, offset = FLIP2(span.y)*w + span.x; \
385 for (i = 0; i < span.count; i++, offset++) { \
386 const DEPTH_TYPE z = FixedToDepth(span.z); \
387 if (z < zRow[i]) { \
388 unsigned char rgb[3]; \
389 rgb[0] = FixedToInt(span.red); \
390 rgb[1] = FixedToInt(span.green); \
391 rgb[2] = FixedToInt(span.blue); \
392 vl_putpixel(b, offset, vl_mixrgb(rgb)); \
393 zRow[i] = z; \
394 } \
395 span.red += span.redStep; \
396 span.green += span.greenStep; \
397 span.blue += span.blueStep; \
398 span.z += span.zStep; \
399 }
Brian Paule0193a92002-02-23 17:11:27 +0000400
401#include "swrast/s_tritemp.h"
402}
403
404
405
406/*
407 * Analyze context state to see if we can provide a fast triangle function
408 * Otherwise, return NULL.
409 */
410static swrast_tri_func dmesa_choose_tri_function (GLcontext *ctx)
411{
412 const SWcontext *swrast = SWRAST_CONTEXT(ctx);
413
414 if (ctx->RenderMode != GL_RENDER) return (swrast_tri_func) NULL;
415 if (ctx->Polygon.SmoothFlag) return (swrast_tri_func) NULL;
416 if (ctx->Texture._ReallyEnabled) return (swrast_tri_func) NULL;
417
418 if (ctx->Light.ShadeModel==GL_SMOOTH
419 && swrast->_RasterMask==DEPTH_BIT
420 && ctx->Depth.Func==GL_LESS
421 && ctx->Depth.Mask==GL_TRUE
422 && ctx->Visual.depthBits == DEFAULT_SOFTWARE_DEPTH_BITS
423 && ctx->Polygon.StippleFlag==GL_FALSE) {
424 return tri_rgb_smooth_z;
425 }
426 if (ctx->Light.ShadeModel==GL_FLAT
427 && swrast->_RasterMask==DEPTH_BIT
428 && ctx->Depth.Func==GL_LESS
429 && ctx->Depth.Mask==GL_TRUE
430 && ctx->Visual.depthBits == DEFAULT_SOFTWARE_DEPTH_BITS
431 && ctx->Polygon.StippleFlag==GL_FALSE) {
432 return tri_rgb_flat_z;
433 }
434 if (swrast->_RasterMask==0 /* no depth test */
435 && ctx->Light.ShadeModel==GL_SMOOTH
436 && ctx->Polygon.StippleFlag==GL_FALSE) {
437 return tri_rgb_smooth;
438 }
439 if (swrast->_RasterMask==0 /* no depth test */
440 && ctx->Light.ShadeModel==GL_FLAT
441 && ctx->Polygon.StippleFlag==GL_FALSE) {
442 return tri_rgb_flat;
443 }
444
445 return (swrast_tri_func)NULL;
446}
447
448
449
450/* Override for the swrast triangle-selection function. Try to use one
451 * of our internal line functions, otherwise fall back to the
452 * standard swrast functions.
453 */
454static void dmesa_choose_tri (GLcontext *ctx)
455{
456 SWcontext *swrast = SWRAST_CONTEXT(ctx);
457
458 if (!(swrast->Triangle=dmesa_choose_tri_function(ctx)))
459 _swrast_choose_triangle(ctx);
460}
Brian Pauldb41d2e2002-02-12 03:24:56 +0000461
462
463
464/**********************************************************************/
465/***** Miscellaneous device driver funcs *****/
466/**********************************************************************/
467
468
469
470static void clear_color (GLcontext *ctx, const GLchan color[4])
471{
472 DMesaContext c = (DMesaContext)ctx->DriverCtx;
Brian Paule0193a92002-02-23 17:11:27 +0000473 c->ClearColor = vl_mixrgba(color);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000474}
475
476
477
478static void clear (GLcontext *ctx, GLbitfield mask, GLboolean all,
479 GLint x, GLint y, GLint width, GLint height)
480{
481 DMesaContext c = (DMesaContext)ctx->DriverCtx;
482 const GLuint *colorMask = (GLuint *)&ctx->Color.ColorMask;
483 DMesaBuffer b = c->Buffer;
484
485/*
486 * Clear the specified region of the buffers indicated by 'mask'
487 * using the clear color or index as specified by one of the two
488 * functions above.
489 * If all==GL_TRUE, clear whole buffer, else just clear region defined
490 * by x,y,width,height
491 */
492
493 /* we can't handle color or index masking */
Brian Paule0193a92002-02-23 17:11:27 +0000494 if (*colorMask==0xffffffff) {
495 if (mask & DD_BACK_LEFT_BIT) {
Brian Pauldb41d2e2002-02-12 03:24:56 +0000496 if (all) {
Brian Paulb43a8282002-03-08 19:27:17 +0000497 vl_clear(b->the_window, b->len, c->ClearColor);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000498 } else {
Brian Paulb43a8282002-03-08 19:27:17 +0000499 vl_rect(b->the_window, x, y, width, height, c->ClearColor);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000500 }
501 mask &= ~DD_BACK_LEFT_BIT;
502 }
503 }
504
505 if (mask) {
506 _swrast_Clear(ctx, mask, all, x, y, width, height);
507 }
508}
509
510
511
512/*
513 * Set the current reading buffer.
514 */
515static void set_read_buffer (GLcontext *ctx, GLframebuffer *buffer,
516 GLenum mode)
517{
518/*
519 DMesaContext c = (DMesaContext)ctx->DriverCtx;
520 dmesa_update_state(ctx);
521*/
522}
523
524
525
526/*
527 * Set the destination/draw buffer.
528 */
Brian Paul4753d602002-06-15 02:38:15 +0000529static void set_draw_buffer (GLcontext *ctx, GLenum mode)
Brian Pauldb41d2e2002-02-12 03:24:56 +0000530{
Brian Paul4753d602002-06-15 02:38:15 +0000531 /*
532 XXX this has to be fixed
533 */
Brian Pauldb41d2e2002-02-12 03:24:56 +0000534}
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 Paul6c921af2002-04-01 17:01:33 +0000543static void get_buffer_size (GLframebuffer *buffer, GLuint *width, GLuint *height)
Brian Pauldb41d2e2002-02-12 03:24:56 +0000544{
Brian Paul6c921af2002-04-01 17:01:33 +0000545 DMesaBuffer b = (DMesaBuffer)buffer;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000546
Brian Paul6c921af2002-04-01 17:01:33 +0000547 *width = b->width;
548 *height = b->height;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000549}
550
551
552
553static const GLubyte* get_string (GLcontext *ctx, GLenum name)
554{
555 switch (name) {
556 case GL_RENDERER:
Brian Paul6c921af2002-04-01 17:01:33 +0000557 return (const GLubyte *)"Mesa DOS\0DJGPP port (c) Borca Daniel 31-mar-2002";
Brian Pauldb41d2e2002-02-12 03:24:56 +0000558 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;
Brian Paul6c921af2002-04-01 17:01:33 +0000639 ctx->Driver.ResizeBuffers = _swrast_alloc_buffers;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000640 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
Brian Paul6c921af2002-04-01 17:01:33 +0000807 _mesa_initialize_framebuffer(&b->gl_buffer,
808 visual->gl_visual,
809 visual->gl_visual->depthBits > 0,
810 visual->gl_visual->stencilBits > 0,
811 visual->gl_visual->accumRedBits > 0,
812 visual->gl_visual->alphaBits > 0);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000813 b->xpos = xpos;
814 b->ypos = ypos;
Brian Paule0193a92002-02-23 17:11:27 +0000815 b->width = width;
816 b->height = height;
Brian Paul6c921af2002-04-01 17:01:33 +0000817 b->bypp = (visual->depth+7)/8;
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 Paul6c921af2002-04-01 17:01:33 +0000828 _mesa_free_framebuffer_data(&b->gl_buffer);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000829 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) {
Brian Paul9a33a112002-06-13 04:28:29 +0000841 __GLimports imports;
842 _mesa_init_default_imports( &imports, (void *) c);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000843 c->gl_ctx = _mesa_create_context(visual->gl_visual,
844 share ? share->gl_ctx : NULL,
Brian Paul9a33a112002-06-13 04:28:29 +0000845 &imports);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000846
Brian Paule0193a92002-02-23 17:11:27 +0000847 _mesa_enable_sw_extensions(c->gl_ctx);
848 _mesa_enable_1_3_extensions(c->gl_ctx);
849
Brian Pauldb41d2e2002-02-12 03:24:56 +0000850 /* you probably have to do a bunch of other initializations here. */
851 c->visual = visual;
852
853 /* Initialize the software rasterizer and helper modules.
854 */
855 _swrast_CreateContext(c->gl_ctx);
856 _ac_CreateContext(c->gl_ctx);
857 _tnl_CreateContext(c->gl_ctx);
858 _swsetup_CreateContext(c->gl_ctx);
859 dmesa_init_pointers(c->gl_ctx);
Brian Paule0193a92002-02-23 17:11:27 +0000860 dmesa_register_swrast_functions(c->gl_ctx);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000861 }
862
863 return c;
864}
865
866
867
868void DMesaDestroyContext (DMesaContext c)
869{
870 _mesa_destroy_context(c->gl_ctx);
871 free(c);
872}
873
874
875
Brian Paul6c921af2002-04-01 17:01:33 +0000876GLboolean DMesaViewport (DMesaBuffer b,
877 GLint xpos, GLint ypos,
878 GLint width, GLint height)
879{
880 void *new_window;
881
882 if ((new_window=vl_sync_buffer(b->the_window, xpos, ypos, width, height))==NULL) {
883 return GL_FALSE;
884 } else {
885 b->the_window = new_window;
886 b->xpos = xpos;
887 b->ypos = ypos;
888 b->width = width;
889 b->height = height;
890 b->bwidth = width * b->bypp;
891 b->len = b->bwidth * height;
892 return GL_TRUE;
893 }
894}
895
896
897
Brian Pauldb41d2e2002-02-12 03:24:56 +0000898/*
899 * Make the specified context and buffer the current one.
900 */
901GLboolean DMesaMakeCurrent (DMesaContext c, DMesaBuffer b)
902{
903 if (c&&b) {
Brian Paul6c921af2002-04-01 17:01:33 +0000904 if (!DMesaViewport(b, b->xpos, b->ypos, b->width, b->height)) {
Brian Pauldb41d2e2002-02-12 03:24:56 +0000905 return GL_FALSE;
906 }
907
Brian Paule0193a92002-02-23 17:11:27 +0000908 c->Buffer = b;
Brian Pauldb41d2e2002-02-12 03:24:56 +0000909
910 dmesa_update_state(c->gl_ctx, 0);
Brian Paul6c921af2002-04-01 17:01:33 +0000911 _mesa_make_current(c->gl_ctx, &b->gl_buffer);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000912 if (c->gl_ctx->Viewport.Width==0) {
913 /* initialize viewport to window size */
Brian Paul6c921af2002-04-01 17:01:33 +0000914 _mesa_Viewport(0, 0, b->width, b->height);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000915 }
916 } else {
917 /* Detach */
918 _mesa_make_current(NULL, NULL);
919 }
920
921 return GL_TRUE;
922}
923
924
925
926void DMesaSwapBuffers (DMesaBuffer b)
927{
928 /* copy/swap back buffer to front if applicable */
Brian Paulb43a8282002-03-08 19:27:17 +0000929 vl_flip(b->the_window, b->bwidth, b->height);
Brian Pauldb41d2e2002-02-12 03:24:56 +0000930}