b2e7df34e8273b073c497c2eb8c8fc72b5bae05c
[mesa.git] / src / mesa / drivers / dri / swrast / swrast.c
1 /*
2 * Copyright (C) 2008 George Sapountzis <gsap7@yahoo.gr>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22 /*
23 * DRI software rasterizer
24 *
25 * This is the mesa swrast module packaged into a DRI driver structure.
26 *
27 * The front-buffer is allocated by the loader. The loader provides read/write
28 * callbacks for access to the front-buffer. The driver uses a scratch row for
29 * front-buffer rendering to avoid repeated calls to the loader.
30 *
31 * The back-buffer is allocated by the driver and is private.
32 */
33
34 #include "main/context.h"
35 #include "main/extensions.h"
36 #include "main/formats.h"
37 #include "main/framebuffer.h"
38 #include "main/imports.h"
39 #include "main/renderbuffer.h"
40 #include "swrast/swrast.h"
41 #include "swrast_setup/swrast_setup.h"
42 #include "tnl/tnl.h"
43 #include "tnl/t_context.h"
44 #include "tnl/t_pipeline.h"
45 #include "vbo/vbo.h"
46 #include "drivers/common/driverfuncs.h"
47 #include "drivers/common/meta.h"
48 #include "utils.h"
49
50 #include "swrast_priv.h"
51
52
53 /**
54 * Screen and config-related functions
55 */
56
57 static void
58 setupLoaderExtensions(__DRIscreen *psp,
59 const __DRIextension **extensions)
60 {
61 int i;
62
63 for (i = 0; extensions[i]; i++) {
64 if (strcmp(extensions[i]->name, __DRI_SWRAST_LOADER) == 0)
65 psp->swrast_loader = (__DRIswrastLoaderExtension *) extensions[i];
66 }
67 }
68
69 static __DRIconfig **
70 swrastFillInModes(__DRIscreen *psp,
71 unsigned pixel_bits, unsigned depth_bits,
72 unsigned stencil_bits, GLboolean have_back_buffer)
73 {
74 __DRIconfig **configs;
75 unsigned depth_buffer_factor;
76 unsigned back_buffer_factor;
77 GLenum fb_format;
78 GLenum fb_type;
79
80 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
81 * support pageflipping at all.
82 */
83 static const GLenum back_buffer_modes[] = {
84 GLX_NONE, GLX_SWAP_UNDEFINED_OML
85 };
86
87 uint8_t depth_bits_array[4];
88 uint8_t stencil_bits_array[4];
89 uint8_t msaa_samples_array[1];
90
91 depth_bits_array[0] = 0;
92 depth_bits_array[1] = 0;
93 depth_bits_array[2] = depth_bits;
94 depth_bits_array[3] = depth_bits;
95
96 /* Just like with the accumulation buffer, always provide some modes
97 * with a stencil buffer.
98 */
99 stencil_bits_array[0] = 0;
100 stencil_bits_array[1] = (stencil_bits == 0) ? 8 : stencil_bits;
101 stencil_bits_array[2] = 0;
102 stencil_bits_array[3] = (stencil_bits == 0) ? 8 : stencil_bits;
103
104 msaa_samples_array[0] = 0;
105
106 depth_buffer_factor = 4;
107 back_buffer_factor = 2;
108
109 switch (pixel_bits) {
110 case 8:
111 fb_format = GL_RGB;
112 fb_type = GL_UNSIGNED_BYTE_2_3_3_REV;
113 break;
114 case 16:
115 fb_format = GL_RGB;
116 fb_type = GL_UNSIGNED_SHORT_5_6_5;
117 break;
118 case 24:
119 fb_format = GL_BGR;
120 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
121 break;
122 case 32:
123 fb_format = GL_BGRA;
124 fb_type = GL_UNSIGNED_INT_8_8_8_8_REV;
125 break;
126 default:
127 fprintf(stderr, "[%s:%u] bad depth %d\n", __func__, __LINE__,
128 pixel_bits);
129 return NULL;
130 }
131
132 configs = driCreateConfigs(fb_format, fb_type,
133 depth_bits_array, stencil_bits_array,
134 depth_buffer_factor, back_buffer_modes,
135 back_buffer_factor, msaa_samples_array, 1,
136 GL_TRUE);
137 if (configs == NULL) {
138 fprintf(stderr, "[%s:%u] Error creating FBConfig!\n", __func__,
139 __LINE__);
140 return NULL;
141 }
142
143 return configs;
144 }
145
146 static __DRIscreen *
147 driCreateNewScreen(int scrn, const __DRIextension **extensions,
148 const __DRIconfig ***driver_configs, void *data)
149 {
150 static const __DRIextension *emptyExtensionList[] = { NULL };
151 __DRIscreen *psp;
152 __DRIconfig **configs8, **configs16, **configs24, **configs32;
153
154 (void) data;
155
156 TRACE;
157
158 psp = calloc(1, sizeof(*psp));
159 if (!psp)
160 return NULL;
161
162 setupLoaderExtensions(psp, extensions);
163
164 psp->num = scrn;
165 psp->extensions = emptyExtensionList;
166
167 configs8 = swrastFillInModes(psp, 8, 8, 0, 1);
168 configs16 = swrastFillInModes(psp, 16, 16, 0, 1);
169 configs24 = swrastFillInModes(psp, 24, 24, 8, 1);
170 configs32 = swrastFillInModes(psp, 32, 24, 8, 1);
171
172 configs16 = driConcatConfigs(configs8, configs16);
173 configs24 = driConcatConfigs(configs16, configs24);
174 *driver_configs = (const __DRIconfig **)
175 driConcatConfigs(configs24, configs32);
176
177 driInitExtensions( NULL, NULL, GL_FALSE );
178
179 return psp;
180 }
181
182 static void driDestroyScreen(__DRIscreen *psp)
183 {
184 TRACE;
185
186 if (psp) {
187 free(psp);
188 }
189 }
190
191 static const __DRIextension **driGetExtensions(__DRIscreen *psp)
192 {
193 TRACE;
194
195 return psp->extensions;
196 }
197
198
199 /**
200 * Framebuffer and renderbuffer-related functions.
201 */
202
203 static GLuint
204 choose_pixel_format(const GLvisual *v)
205 {
206 if (v->rgbMode) {
207 int depth = v->rgbBits;
208
209 if (depth == 32
210 && v->redMask == 0xff0000
211 && v->greenMask == 0x00ff00
212 && v->blueMask == 0x0000ff)
213 return PF_A8R8G8B8;
214 else if (depth == 24
215 && v->redMask == 0xff0000
216 && v->greenMask == 0x00ff00
217 && v->blueMask == 0x0000ff)
218 return PF_X8R8G8B8;
219 else if (depth == 16
220 && v->redMask == 0xf800
221 && v->greenMask == 0x07e0
222 && v->blueMask == 0x001f)
223 return PF_R5G6B5;
224 else if (depth == 8
225 && v->redMask == 0x07
226 && v->greenMask == 0x38
227 && v->blueMask == 0xc0)
228 return PF_R3G3B2;
229 }
230 else {
231 if (v->indexBits == 8)
232 return PF_CI8;
233 }
234
235 _mesa_problem( NULL, "unexpected format in %s", __FUNCTION__ );
236 return 0;
237 }
238
239 static void
240 swrast_delete_renderbuffer(struct gl_renderbuffer *rb)
241 {
242 TRACE;
243
244 free(rb->Data);
245 free(rb);
246 }
247
248 static GLboolean
249 swrast_alloc_front_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
250 GLenum internalFormat, GLuint width, GLuint height)
251 {
252 struct swrast_renderbuffer *xrb = swrast_renderbuffer(rb);
253 unsigned mask = PITCH_ALIGN_BITS - 1;
254
255 TRACE;
256
257 rb->Data = NULL;
258 rb->Width = width;
259 rb->Height = height;
260
261 /* always pad to PITCH_ALIGN_BITS */
262 xrb->pitch = ((width * xrb->bpp + mask) & ~mask) / 8;
263
264 return GL_TRUE;
265 }
266
267 static GLboolean
268 swrast_alloc_back_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
269 GLenum internalFormat, GLuint width, GLuint height)
270 {
271 struct swrast_renderbuffer *xrb = swrast_renderbuffer(rb);
272
273 TRACE;
274
275 free(rb->Data);
276
277 swrast_alloc_front_storage(ctx, rb, internalFormat, width, height);
278
279 rb->Data = malloc(height * xrb->pitch);
280
281 return GL_TRUE;
282 }
283
284 static struct swrast_renderbuffer *
285 swrast_new_renderbuffer(const GLvisual *visual, GLboolean front)
286 {
287 struct swrast_renderbuffer *xrb = calloc(1, sizeof *xrb);
288 GLuint pixel_format;
289
290 TRACE;
291
292 if (!xrb)
293 return NULL;
294
295 _mesa_init_renderbuffer(&xrb->Base, 0);
296
297 pixel_format = choose_pixel_format(visual);
298
299 xrb->Base.Delete = swrast_delete_renderbuffer;
300 if (front) {
301 xrb->Base.AllocStorage = swrast_alloc_front_storage;
302 swrast_set_span_funcs_front(xrb, pixel_format);
303 }
304 else {
305 xrb->Base.AllocStorage = swrast_alloc_back_storage;
306 swrast_set_span_funcs_back(xrb, pixel_format);
307 }
308
309 switch (pixel_format) {
310 case PF_A8R8G8B8:
311 xrb->Base.Format = MESA_FORMAT_ARGB8888;
312 xrb->Base.InternalFormat = GL_RGBA;
313 xrb->Base._BaseFormat = GL_RGBA;
314 xrb->Base.DataType = GL_UNSIGNED_BYTE;
315 xrb->bpp = 32;
316 break;
317 case PF_X8R8G8B8:
318 xrb->Base.Format = MESA_FORMAT_ARGB8888; /* XXX */
319 xrb->Base.InternalFormat = GL_RGB;
320 xrb->Base._BaseFormat = GL_RGB;
321 xrb->Base.DataType = GL_UNSIGNED_BYTE;
322 xrb->bpp = 32;
323 break;
324 case PF_R5G6B5:
325 xrb->Base.Format = MESA_FORMAT_RGB565;
326 xrb->Base.InternalFormat = GL_RGB;
327 xrb->Base._BaseFormat = GL_RGB;
328 xrb->Base.DataType = GL_UNSIGNED_BYTE;
329 xrb->bpp = 16;
330 break;
331 case PF_R3G3B2:
332 xrb->Base.Format = MESA_FORMAT_RGB332;
333 xrb->Base.InternalFormat = GL_RGB;
334 xrb->Base._BaseFormat = GL_RGB;
335 xrb->Base.DataType = GL_UNSIGNED_BYTE;
336 xrb->bpp = 8;
337 break;
338 case PF_CI8:
339 xrb->Base.Format = MESA_FORMAT_CI8;
340 xrb->Base.InternalFormat = GL_COLOR_INDEX8_EXT;
341 xrb->Base._BaseFormat = GL_COLOR_INDEX;
342 xrb->Base.DataType = GL_UNSIGNED_BYTE;
343 xrb->bpp = 8;
344 break;
345 default:
346 return NULL;
347 }
348
349 return xrb;
350 }
351
352 static __DRIdrawable *
353 driCreateNewDrawable(__DRIscreen *screen,
354 const __DRIconfig *config, void *data)
355 {
356 __DRIdrawable *buf;
357 struct swrast_renderbuffer *frontrb, *backrb;
358
359 TRACE;
360
361 buf = calloc(1, sizeof *buf);
362 if (!buf)
363 return NULL;
364
365 buf->loaderPrivate = data;
366
367 buf->driScreenPriv = screen;
368
369 buf->row = malloc(MAX_WIDTH * 4);
370
371 /* basic framebuffer setup */
372 _mesa_initialize_window_framebuffer(&buf->Base, &config->modes);
373
374 /* add front renderbuffer */
375 frontrb = swrast_new_renderbuffer(&config->modes, GL_TRUE);
376 _mesa_add_renderbuffer(&buf->Base, BUFFER_FRONT_LEFT, &frontrb->Base);
377
378 /* add back renderbuffer */
379 if (config->modes.doubleBufferMode) {
380 backrb = swrast_new_renderbuffer(&config->modes, GL_FALSE);
381 _mesa_add_renderbuffer(&buf->Base, BUFFER_BACK_LEFT, &backrb->Base);
382 }
383
384 /* add software renderbuffers */
385 _mesa_add_soft_renderbuffers(&buf->Base,
386 GL_FALSE, /* color */
387 config->modes.haveDepthBuffer,
388 config->modes.haveStencilBuffer,
389 config->modes.haveAccumBuffer,
390 GL_FALSE, /* alpha */
391 GL_FALSE /* aux bufs */);
392
393 return buf;
394 }
395
396 static void
397 driDestroyDrawable(__DRIdrawable *buf)
398 {
399 TRACE;
400
401 if (buf) {
402 struct gl_framebuffer *fb = &buf->Base;
403
404 free(buf->row);
405
406 fb->DeletePending = GL_TRUE;
407 _mesa_reference_framebuffer(&fb, NULL);
408 }
409 }
410
411 static void driSwapBuffers(__DRIdrawable *buf)
412 {
413 GET_CURRENT_CONTEXT(ctx);
414
415 struct swrast_renderbuffer *frontrb =
416 swrast_renderbuffer(buf->Base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
417 struct swrast_renderbuffer *backrb =
418 swrast_renderbuffer(buf->Base.Attachment[BUFFER_BACK_LEFT].Renderbuffer);
419
420 __DRIscreen *screen = buf->driScreenPriv;
421
422 TRACE;
423
424 /* check for signle-buffered */
425 if (backrb == NULL)
426 return;
427
428 /* check if swapping currently bound buffer */
429 if (ctx && ctx->DrawBuffer == &(buf->Base)) {
430 /* flush pending rendering */
431 _mesa_notifySwapBuffers(ctx);
432 }
433
434 screen->swrast_loader->putImage(buf, __DRI_SWRAST_IMAGE_OP_SWAP,
435 0, 0,
436 frontrb->Base.Width,
437 frontrb->Base.Height,
438 backrb->Base.Data,
439 buf->loaderPrivate);
440 }
441
442
443 /**
444 * General device driver functions.
445 */
446
447 static void
448 get_window_size( GLframebuffer *fb, GLsizei *w, GLsizei *h )
449 {
450 __DRIdrawable *buf = swrast_drawable(fb);
451 __DRIscreen *screen = buf->driScreenPriv;
452 int x, y;
453
454 screen->swrast_loader->getDrawableInfo(buf,
455 &x, &y, w, h,
456 buf->loaderPrivate);
457 }
458
459 static void
460 swrast_check_and_update_window_size( GLcontext *ctx, GLframebuffer *fb )
461 {
462 GLsizei width, height;
463
464 get_window_size(fb, &width, &height);
465 if (fb->Width != width || fb->Height != height) {
466 _mesa_resize_framebuffer(ctx, fb, width, height);
467 }
468 }
469
470 static const GLubyte *
471 get_string(GLcontext *ctx, GLenum pname)
472 {
473 (void) ctx;
474 switch (pname) {
475 case GL_VENDOR:
476 return (const GLubyte *) "Mesa Project";
477 case GL_RENDERER:
478 return (const GLubyte *) "Software Rasterizer";
479 default:
480 return NULL;
481 }
482 }
483
484 static void
485 update_state( GLcontext *ctx, GLuint new_state )
486 {
487 /* not much to do here - pass it on */
488 _swrast_InvalidateState( ctx, new_state );
489 _swsetup_InvalidateState( ctx, new_state );
490 _vbo_InvalidateState( ctx, new_state );
491 _tnl_InvalidateState( ctx, new_state );
492 }
493
494 static void
495 viewport(GLcontext *ctx, GLint x, GLint y, GLsizei w, GLsizei h)
496 {
497 GLframebuffer *draw = ctx->WinSysDrawBuffer;
498 GLframebuffer *read = ctx->WinSysReadBuffer;
499
500 swrast_check_and_update_window_size(ctx, draw);
501 swrast_check_and_update_window_size(ctx, read);
502 }
503
504 static void
505 swrast_init_driver_functions(struct dd_function_table *driver)
506 {
507 driver->GetString = get_string;
508 driver->UpdateState = update_state;
509 driver->GetBufferSize = NULL;
510 driver->Viewport = viewport;
511 }
512
513
514 /**
515 * Context-related functions.
516 */
517
518 static __DRIcontext *
519 driCreateNewContext(__DRIscreen *screen, const __DRIconfig *config,
520 __DRIcontext *shared, void *data)
521 {
522 __DRIcontext *ctx;
523 GLcontext *mesaCtx;
524 struct dd_function_table functions;
525
526 TRACE;
527
528 ctx = calloc(1, sizeof *ctx);
529 if (!ctx)
530 return NULL;
531
532 ctx->loaderPrivate = data;
533
534 ctx->driScreenPriv = screen;
535
536 /* build table of device driver functions */
537 _mesa_init_driver_functions(&functions);
538 swrast_init_driver_functions(&functions);
539
540 if (!_mesa_initialize_context(&ctx->Base, &config->modes,
541 shared ? &shared->Base : NULL,
542 &functions, (void *) ctx)) {
543 free(ctx);
544 return NULL;
545 }
546
547 mesaCtx = &ctx->Base;
548
549 /* do bounds checking to prevent segfaults and server crashes! */
550 mesaCtx->Const.CheckArrayBounds = GL_TRUE;
551
552 /* create module contexts */
553 _swrast_CreateContext( mesaCtx );
554 _vbo_CreateContext( mesaCtx );
555 _tnl_CreateContext( mesaCtx );
556 _swsetup_CreateContext( mesaCtx );
557 _swsetup_Wakeup( mesaCtx );
558
559 /* use default TCL pipeline */
560 {
561 TNLcontext *tnl = TNL_CONTEXT(mesaCtx);
562 tnl->Driver.RunPipeline = _tnl_run_pipeline;
563 }
564
565 _mesa_enable_sw_extensions(mesaCtx);
566 _mesa_enable_1_3_extensions(mesaCtx);
567 _mesa_enable_1_4_extensions(mesaCtx);
568 _mesa_enable_1_5_extensions(mesaCtx);
569 _mesa_enable_2_0_extensions(mesaCtx);
570 _mesa_enable_2_1_extensions(mesaCtx);
571
572 _mesa_meta_init(mesaCtx);
573
574 return ctx;
575 }
576
577 static void
578 driDestroyContext(__DRIcontext *ctx)
579 {
580 GLcontext *mesaCtx;
581 TRACE;
582
583 if (ctx) {
584 mesaCtx = &ctx->Base;
585 _mesa_meta_free(mesaCtx);
586 _swsetup_DestroyContext( mesaCtx );
587 _swrast_DestroyContext( mesaCtx );
588 _tnl_DestroyContext( mesaCtx );
589 _vbo_DestroyContext( mesaCtx );
590 _mesa_destroy_context( mesaCtx );
591 }
592 }
593
594 static int
595 driCopyContext(__DRIcontext *dst, __DRIcontext *src, unsigned long mask)
596 {
597 TRACE;
598
599 _mesa_copy_context(&src->Base, &dst->Base, mask);
600 return GL_TRUE;
601 }
602
603 static int driBindContext(__DRIcontext *ctx,
604 __DRIdrawable *draw,
605 __DRIdrawable *read)
606 {
607 GLcontext *mesaCtx;
608 GLframebuffer *mesaDraw;
609 GLframebuffer *mesaRead;
610 TRACE;
611
612 if (ctx) {
613 if (!draw || !read)
614 return GL_FALSE;
615
616 mesaCtx = &ctx->Base;
617 mesaDraw = &draw->Base;
618 mesaRead = &read->Base;
619
620 /* check for same context and buffer */
621 if (mesaCtx == _mesa_get_current_context()
622 && mesaCtx->DrawBuffer == mesaDraw
623 && mesaCtx->ReadBuffer == mesaRead) {
624 return GL_TRUE;
625 }
626
627 _glapi_check_multithread();
628
629 swrast_check_and_update_window_size(mesaCtx, mesaDraw);
630 if (read != draw)
631 swrast_check_and_update_window_size(mesaCtx, mesaRead);
632
633 _mesa_make_current( mesaCtx,
634 mesaDraw,
635 mesaRead );
636 }
637 else {
638 /* unbind */
639 _mesa_make_current( NULL, NULL, NULL );
640 }
641
642 return GL_TRUE;
643 }
644
645 static int driUnbindContext(__DRIcontext *ctx)
646 {
647 TRACE;
648 (void) ctx;
649 return GL_TRUE;
650 }
651
652
653 static const __DRIcoreExtension driCoreExtension = {
654 { __DRI_CORE, __DRI_CORE_VERSION },
655 NULL, /* driCreateNewScreen */
656 driDestroyScreen,
657 driGetExtensions,
658 driGetConfigAttrib,
659 driIndexConfigAttrib,
660 NULL, /* driCreateNewDrawable */
661 driDestroyDrawable,
662 driSwapBuffers,
663 driCreateNewContext,
664 driCopyContext,
665 driDestroyContext,
666 driBindContext,
667 driUnbindContext
668 };
669
670 static const __DRIswrastExtension driSWRastExtension = {
671 { __DRI_SWRAST, __DRI_SWRAST_VERSION },
672 driCreateNewScreen,
673 driCreateNewDrawable
674 };
675
676 /* This is the table of extensions that the loader will dlsym() for. */
677 PUBLIC const __DRIextension *__driDriverExtensions[] = {
678 &driCoreExtension.base,
679 &driSWRastExtension.base,
680 NULL
681 };