2 * Copyright (C) 2008 George Sapountzis <gsap7@yahoo.gr>
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:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
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.
23 * DRI software rasterizer
25 * This is the mesa swrast module packaged into a DRI driver structure.
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.
31 * The back-buffer is allocated by the driver and is private.
35 #include "extensions.h"
36 #include "framebuffer.h"
38 #include "renderbuffer.h"
39 #include "swrast/swrast.h"
40 #include "swrast_setup/swrast_setup.h"
42 #include "tnl/t_context.h"
43 #include "tnl/t_pipeline.h"
45 #include "drivers/common/driverfuncs.h"
47 #include "swrast_priv.h"
50 #define need_GL_VERSION_1_3
51 #define need_GL_VERSION_1_4
52 #define need_GL_VERSION_1_5
53 #define need_GL_VERSION_2_0
55 /* sw extensions for imaging */
56 #define need_GL_EXT_blend_color
57 #define need_GL_EXT_blend_minmax
58 #define need_GL_EXT_convolution
59 #define need_GL_EXT_histogram
60 #define need_GL_SGI_color_table
62 /* sw extensions not associated with some GL version */
63 #define need_GL_ARB_shader_objects
64 #define need_GL_ARB_vertex_program
65 #define need_GL_APPLE_vertex_array_object
66 #define need_GL_ATI_fragment_shader
67 #define need_GL_EXT_depth_bounds_test
68 #define need_GL_EXT_framebuffer_object
69 #define need_GL_EXT_framebuffer_blit
70 #define need_GL_EXT_gpu_program_parameters
71 #define need_GL_EXT_paletted_texture
72 #define need_GL_IBM_multimode_draw_arrays
73 #define need_GL_MESA_resize_buffers
74 #define need_GL_NV_vertex_program
75 #define need_GL_NV_fragment_program
77 #include "extension_helper.h"
80 const struct dri_extension card_extensions
[] =
82 { "GL_VERSION_1_3", GL_VERSION_1_3_functions
},
83 { "GL_VERSION_1_4", GL_VERSION_1_4_functions
},
84 { "GL_VERSION_1_5", GL_VERSION_1_5_functions
},
85 { "GL_VERSION_2_0", GL_VERSION_2_0_functions
},
87 { "GL_EXT_blend_color", GL_EXT_blend_color_functions
},
88 { "GL_EXT_blend_minmax", GL_EXT_blend_minmax_functions
},
89 { "GL_EXT_convolution", GL_EXT_convolution_functions
},
90 { "GL_EXT_histogram", GL_EXT_histogram_functions
},
91 { "GL_SGI_color_table", GL_SGI_color_table_functions
},
93 { "GL_ARB_shader_objects", GL_ARB_shader_objects_functions
},
94 { "GL_ARB_vertex_program", GL_ARB_vertex_program_functions
},
95 { "GL_APPLE_vertex_array_object", GL_APPLE_vertex_array_object_functions
},
96 { "GL_ATI_fragment_shader", GL_ATI_fragment_shader_functions
},
97 { "GL_EXT_depth_bounds_test", GL_EXT_depth_bounds_test_functions
},
98 { "GL_EXT_framebuffer_object", GL_EXT_framebuffer_object_functions
},
99 { "GL_EXT_framebuffer_blit", GL_EXT_framebuffer_blit_functions
},
100 { "GL_EXT_gpu_program_parameters", GL_EXT_gpu_program_parameters_functions
},
101 { "GL_EXT_paletted_texture", GL_EXT_paletted_texture_functions
},
102 { "GL_IBM_multimode_draw_arrays", GL_IBM_multimode_draw_arrays_functions
},
103 { "GL_MESA_resize_buffers", GL_MESA_resize_buffers_functions
},
104 { "GL_NV_vertex_program", GL_NV_vertex_program_functions
},
105 { "GL_NV_fragment_program", GL_NV_fragment_program_functions
},
111 * Screen and config-related functions
115 setupLoaderExtensions(__DRIscreen
*psp
,
116 const __DRIextension
**extensions
)
120 for (i
= 0; extensions
[i
]; i
++) {
121 if (strcmp(extensions
[i
]->name
, __DRI_SWRAST_LOADER
) == 0)
122 psp
->swrast_loader
= (__DRIswrastLoaderExtension
*) extensions
[i
];
126 static __DRIconfig
**
127 swrastFillInModes(__DRIscreen
*psp
,
128 unsigned pixel_bits
, unsigned depth_bits
,
129 unsigned stencil_bits
, GLboolean have_back_buffer
)
131 __DRIconfig
**configs
;
132 unsigned depth_buffer_factor
;
133 unsigned back_buffer_factor
;
137 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
138 * support pageflipping at all.
140 static const GLenum back_buffer_modes
[] = {
141 GLX_NONE
, GLX_SWAP_UNDEFINED_OML
144 u_int8_t depth_bits_array
[4];
145 u_int8_t stencil_bits_array
[4];
147 depth_bits_array
[0] = 0;
148 depth_bits_array
[1] = 0;
149 depth_bits_array
[2] = depth_bits
;
150 depth_bits_array
[3] = depth_bits
;
152 /* Just like with the accumulation buffer, always provide some modes
153 * with a stencil buffer.
155 stencil_bits_array
[0] = 0;
156 stencil_bits_array
[1] = (stencil_bits
== 0) ? 8 : stencil_bits
;
157 stencil_bits_array
[2] = 0;
158 stencil_bits_array
[3] = (stencil_bits
== 0) ? 8 : stencil_bits
;
160 depth_buffer_factor
= 4;
161 back_buffer_factor
= 2;
163 if (pixel_bits
== 8) {
165 fb_type
= GL_UNSIGNED_BYTE_2_3_3_REV
;
167 else if (pixel_bits
== 16) {
169 fb_type
= GL_UNSIGNED_SHORT_5_6_5
;
173 fb_type
= GL_UNSIGNED_INT_8_8_8_8_REV
;
176 configs
= driCreateConfigs(fb_format
, fb_type
,
177 depth_bits_array
, stencil_bits_array
,
178 depth_buffer_factor
, back_buffer_modes
,
180 if (configs
== NULL
) {
181 fprintf(stderr
, "[%s:%u] Error creating FBConfig!\n", __func__
,
190 driCreateNewScreen(int scrn
, const __DRIextension
**extensions
,
191 const __DRIconfig
***driver_configs
, void *data
)
193 static const __DRIextension
*emptyExtensionList
[] = { NULL
};
195 __DRIconfig
**configs8
, **configs16
, **configs32
;
201 psp
= _mesa_calloc(sizeof(*psp
));
205 setupLoaderExtensions(psp
, extensions
);
208 psp
->extensions
= emptyExtensionList
;
210 configs8
= swrastFillInModes(psp
, 8, 8, 0, 1);
211 configs16
= swrastFillInModes(psp
, 16, 16, 0, 1);
212 configs32
= swrastFillInModes(psp
, 32, 24, 8, 1);
214 configs16
= (__DRIconfig
**)driConcatConfigs(configs8
, configs16
);
216 *driver_configs
= driConcatConfigs(configs16
, configs32
);
218 driInitExtensions( NULL
, card_extensions
, GL_FALSE
);
223 static void driDestroyScreen(__DRIscreen
*psp
)
232 static const __DRIextension
**driGetExtensions(__DRIscreen
*psp
)
236 return psp
->extensions
;
241 * Framebuffer and renderbuffer-related functions.
245 choose_pixel_format(const GLvisual
*v
)
248 int bpp
= v
->rgbBits
;
251 && v
->redMask
== 0xff0000
252 && v
->greenMask
== 0x00ff00
253 && v
->blueMask
== 0x0000ff)
256 && v
->redMask
== 0xf800
257 && v
->greenMask
== 0x07e0
258 && v
->blueMask
== 0x001f)
261 && v
->redMask
== 0x07
262 && v
->greenMask
== 0x38
263 && v
->blueMask
== 0xc0)
267 if (v
->indexBits
== 8)
271 _mesa_problem( NULL
, "unexpected format in %s", __FUNCTION__
);
276 swrast_delete_renderbuffer(struct gl_renderbuffer
*rb
)
280 _mesa_free(rb
->Data
);
285 swrast_alloc_front_storage(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
286 GLenum internalFormat
, GLuint width
, GLuint height
)
288 struct swrast_renderbuffer
*xrb
= swrast_renderbuffer(rb
);
290 unsigned mask
= PITCH_ALIGN_BITS
- 1;
298 switch (internalFormat
) {
300 bpp
= rb
->RedBits
+ rb
->GreenBits
+ rb
->BlueBits
;
303 bpp
= rb
->RedBits
+ rb
->GreenBits
+ rb
->BlueBits
+ rb
->AlphaBits
;
305 case GL_COLOR_INDEX8_EXT
:
309 _mesa_problem( NULL
, "unexpected format in %s", __FUNCTION__
);
313 /* always pad to PITCH_ALIGN_BITS */
314 xrb
->pitch
= ((width
* bpp
+ mask
) & ~mask
) / 8;
320 swrast_alloc_back_storage(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
321 GLenum internalFormat
, GLuint width
, GLuint height
)
323 struct swrast_renderbuffer
*xrb
= swrast_renderbuffer(rb
);
327 _mesa_free(rb
->Data
);
329 (void) swrast_alloc_front_storage(ctx
, rb
, internalFormat
, width
, height
);
331 rb
->Data
= _mesa_malloc(height
* xrb
->pitch
);
336 static struct swrast_renderbuffer
*
337 swrast_new_renderbuffer(const GLvisual
*visual
, GLboolean front
)
339 struct swrast_renderbuffer
*xrb
= _mesa_calloc(sizeof *xrb
);
345 _mesa_init_renderbuffer(&xrb
->Base
, 0);
347 pixel_format
= choose_pixel_format(visual
);
349 xrb
->Base
.Delete
= swrast_delete_renderbuffer
;
351 xrb
->Base
.AllocStorage
= swrast_alloc_front_storage
;
352 swrast_set_span_funcs_front(xrb
, pixel_format
);
355 xrb
->Base
.AllocStorage
= swrast_alloc_back_storage
;
356 swrast_set_span_funcs_back(xrb
, pixel_format
);
359 switch (pixel_format
) {
361 xrb
->Base
.InternalFormat
= GL_RGBA
;
362 xrb
->Base
._BaseFormat
= GL_RGBA
;
363 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
364 xrb
->Base
.RedBits
= 8 * sizeof(GLubyte
);
365 xrb
->Base
.GreenBits
= 8 * sizeof(GLubyte
);
366 xrb
->Base
.BlueBits
= 8 * sizeof(GLubyte
);
367 xrb
->Base
.AlphaBits
= 8 * sizeof(GLubyte
);
370 xrb
->Base
.InternalFormat
= GL_RGB
;
371 xrb
->Base
._BaseFormat
= GL_RGB
;
372 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
373 xrb
->Base
.RedBits
= 5 * sizeof(GLubyte
);
374 xrb
->Base
.GreenBits
= 6 * sizeof(GLubyte
);
375 xrb
->Base
.BlueBits
= 5 * sizeof(GLubyte
);
376 xrb
->Base
.AlphaBits
= 0;
379 xrb
->Base
.InternalFormat
= GL_RGB
;
380 xrb
->Base
._BaseFormat
= GL_RGB
;
381 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
382 xrb
->Base
.RedBits
= 3 * sizeof(GLubyte
);
383 xrb
->Base
.GreenBits
= 3 * sizeof(GLubyte
);
384 xrb
->Base
.BlueBits
= 2 * sizeof(GLubyte
);
385 xrb
->Base
.AlphaBits
= 0;
388 xrb
->Base
.InternalFormat
= GL_COLOR_INDEX8_EXT
;
389 xrb
->Base
._BaseFormat
= GL_COLOR_INDEX
;
390 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
391 xrb
->Base
.IndexBits
= 8 * sizeof(GLubyte
);
400 static __DRIdrawable
*
401 driCreateNewDrawable(__DRIscreen
*screen
,
402 const __DRIconfig
*config
, void *data
)
405 struct swrast_renderbuffer
*frontrb
, *backrb
;
409 buf
= _mesa_calloc(sizeof *buf
);
413 buf
->loaderPrivate
= data
;
415 buf
->driScreenPriv
= screen
;
417 buf
->row
= _mesa_malloc(MAX_WIDTH
* 4);
419 /* basic framebuffer setup */
420 _mesa_initialize_framebuffer(&buf
->Base
, &config
->modes
);
422 /* add front renderbuffer */
423 frontrb
= swrast_new_renderbuffer(&config
->modes
, GL_TRUE
);
424 _mesa_add_renderbuffer(&buf
->Base
, BUFFER_FRONT_LEFT
, &frontrb
->Base
);
426 /* add back renderbuffer */
427 if (config
->modes
.doubleBufferMode
) {
428 backrb
= swrast_new_renderbuffer(&config
->modes
, GL_FALSE
);
429 _mesa_add_renderbuffer(&buf
->Base
, BUFFER_BACK_LEFT
, &backrb
->Base
);
432 /* add software renderbuffers */
433 _mesa_add_soft_renderbuffers(&buf
->Base
,
434 GL_FALSE
, /* color */
435 config
->modes
.haveDepthBuffer
,
436 config
->modes
.haveStencilBuffer
,
437 config
->modes
.haveAccumBuffer
,
438 GL_FALSE
, /* alpha */
439 GL_FALSE
/* aux bufs */);
445 driDestroyDrawable(__DRIdrawable
*buf
)
450 struct gl_framebuffer
*fb
= &buf
->Base
;
452 _mesa_free(buf
->row
);
454 fb
->DeletePending
= GL_TRUE
;
455 _mesa_unreference_framebuffer(&fb
);
459 static void driSwapBuffers(__DRIdrawable
*buf
)
461 GET_CURRENT_CONTEXT(ctx
);
463 struct swrast_renderbuffer
*frontrb
=
464 swrast_renderbuffer(buf
->Base
.Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
465 struct swrast_renderbuffer
*backrb
=
466 swrast_renderbuffer(buf
->Base
.Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
);
468 __DRIscreen
*screen
= buf
->driScreenPriv
;
472 /* check for signle-buffered */
476 /* check if swapping currently bound buffer */
477 if (ctx
&& ctx
->DrawBuffer
== &(buf
->Base
)) {
478 /* flush pending rendering */
479 _mesa_notifySwapBuffers(ctx
);
482 screen
->swrast_loader
->putImage(buf
, __DRI_SWRAST_IMAGE_OP_SWAP
,
485 frontrb
->Base
.Height
,
492 * General device driver functions.
496 get_window_size( GLframebuffer
*fb
, GLsizei
*w
, GLsizei
*h
)
498 __DRIdrawable
*buf
= swrast_drawable(fb
);
499 __DRIscreen
*screen
= buf
->driScreenPriv
;
502 screen
->swrast_loader
->getDrawableInfo(buf
,
508 swrast_check_and_update_window_size( GLcontext
*ctx
, GLframebuffer
*fb
)
510 GLsizei width
, height
;
512 get_window_size(fb
, &width
, &height
);
513 if (fb
->Width
!= width
|| fb
->Height
!= height
) {
514 _mesa_resize_framebuffer(ctx
, fb
, width
, height
);
518 static const GLubyte
*
519 get_string(GLcontext
*ctx
, GLenum pname
)
524 return (const GLubyte
*) "Mesa Project";
526 return (const GLubyte
*) "Software Rasterizer";
533 update_state( GLcontext
*ctx
, GLuint new_state
)
535 /* not much to do here - pass it on */
536 _swrast_InvalidateState( ctx
, new_state
);
537 _swsetup_InvalidateState( ctx
, new_state
);
538 _vbo_InvalidateState( ctx
, new_state
);
539 _tnl_InvalidateState( ctx
, new_state
);
543 viewport(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
545 GLframebuffer
*draw
= ctx
->WinSysDrawBuffer
;
546 GLframebuffer
*read
= ctx
->WinSysReadBuffer
;
548 swrast_check_and_update_window_size(ctx
, draw
);
549 swrast_check_and_update_window_size(ctx
, read
);
553 swrast_init_driver_functions(struct dd_function_table
*driver
)
555 driver
->GetString
= get_string
;
556 driver
->UpdateState
= update_state
;
557 driver
->GetBufferSize
= NULL
;
558 driver
->Viewport
= viewport
;
563 * Context-related functions.
566 static __DRIcontext
*
567 driCreateNewContext(__DRIscreen
*screen
, const __DRIconfig
*config
,
568 __DRIcontext
*shared
, void *data
)
572 struct dd_function_table functions
;
576 ctx
= _mesa_calloc(sizeof *ctx
);
580 ctx
->loaderPrivate
= data
;
582 ctx
->driScreenPriv
= screen
;
584 /* build table of device driver functions */
585 _mesa_init_driver_functions(&functions
);
586 swrast_init_driver_functions(&functions
);
588 if (!_mesa_initialize_context(&ctx
->Base
, &config
->modes
,
589 shared
? &shared
->Base
: NULL
,
590 &functions
, (void *) ctx
)) {
595 mesaCtx
= &ctx
->Base
;
597 /* do bounds checking to prevent segfaults and server crashes! */
598 mesaCtx
->Const
.CheckArrayBounds
= GL_TRUE
;
600 /* create module contexts */
601 _swrast_CreateContext( mesaCtx
);
602 _vbo_CreateContext( mesaCtx
);
603 _tnl_CreateContext( mesaCtx
);
604 _swsetup_CreateContext( mesaCtx
);
605 _swsetup_Wakeup( mesaCtx
);
607 /* use default TCL pipeline */
609 TNLcontext
*tnl
= TNL_CONTEXT(mesaCtx
);
610 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
613 _mesa_enable_sw_extensions(mesaCtx
);
614 _mesa_enable_1_3_extensions(mesaCtx
);
615 _mesa_enable_1_4_extensions(mesaCtx
);
616 _mesa_enable_1_5_extensions(mesaCtx
);
617 _mesa_enable_2_0_extensions(mesaCtx
);
618 _mesa_enable_2_1_extensions(mesaCtx
);
624 driDestroyContext(__DRIcontext
*ctx
)
630 mesaCtx
= &ctx
->Base
;
631 _swsetup_DestroyContext( mesaCtx
);
632 _swrast_DestroyContext( mesaCtx
);
633 _tnl_DestroyContext( mesaCtx
);
634 _vbo_DestroyContext( mesaCtx
);
635 _mesa_destroy_context( mesaCtx
);
640 driCopyContext(__DRIcontext
*dst
, __DRIcontext
*src
, unsigned long mask
)
644 _mesa_copy_context(&src
->Base
, &dst
->Base
, mask
);
648 static int driBindContext(__DRIcontext
*ctx
,
653 GLframebuffer
*mesaDraw
;
654 GLframebuffer
*mesaRead
;
661 mesaCtx
= &ctx
->Base
;
662 mesaDraw
= &draw
->Base
;
663 mesaRead
= &read
->Base
;
665 /* check for same context and buffer */
666 if (mesaCtx
== _mesa_get_current_context()
667 && mesaCtx
->DrawBuffer
== mesaDraw
668 && mesaCtx
->ReadBuffer
== mesaRead
) {
672 _glapi_check_multithread();
674 swrast_check_and_update_window_size(mesaCtx
, mesaDraw
);
676 swrast_check_and_update_window_size(mesaCtx
, mesaRead
);
678 _mesa_make_current( mesaCtx
,
684 _mesa_make_current( NULL
, NULL
, NULL
);
690 static int driUnbindContext(__DRIcontext
*ctx
)
694 _mesa_make_current(NULL
, NULL
, NULL
);
699 static const __DRIcoreExtension driCoreExtension
= {
700 { __DRI_CORE
, __DRI_CORE_VERSION
},
701 NULL
, /* driCreateNewScreen */
705 driIndexConfigAttrib
,
706 NULL
, /* driCreateNewDrawable */
716 static const __DRIswrastExtension driSWRastExtension
= {
717 { __DRI_SWRAST
, __DRI_SWRAST_VERSION
},
722 /* This is the table of extensions that the loader will dlsym() for. */
723 PUBLIC
const __DRIextension
*__driDriverExtensions
[] = {
724 &driCoreExtension
.base
,
725 &driSWRastExtension
.base
,