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 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
);
347 _mesa_init_renderbuffer(&xrb
->Base
, 0);
349 pixel_format
= choose_pixel_format(visual
);
351 xrb
->Base
.Delete
= swrast_delete_renderbuffer
;
353 xrb
->Base
.AllocStorage
= swrast_alloc_front_storage
;
354 swrast_set_span_funcs_front(xrb
, pixel_format
);
357 xrb
->Base
.AllocStorage
= swrast_alloc_back_storage
;
358 swrast_set_span_funcs_back(xrb
, pixel_format
);
361 switch (pixel_format
) {
363 xrb
->Base
.InternalFormat
= GL_RGBA
;
364 xrb
->Base
._BaseFormat
= GL_RGBA
;
365 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
366 xrb
->Base
.RedBits
= 8 * sizeof(GLubyte
);
367 xrb
->Base
.GreenBits
= 8 * sizeof(GLubyte
);
368 xrb
->Base
.BlueBits
= 8 * sizeof(GLubyte
);
369 xrb
->Base
.AlphaBits
= 8 * sizeof(GLubyte
);
372 xrb
->Base
.InternalFormat
= GL_RGB
;
373 xrb
->Base
._BaseFormat
= GL_RGB
;
374 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
375 xrb
->Base
.RedBits
= 5 * sizeof(GLubyte
);
376 xrb
->Base
.GreenBits
= 6 * sizeof(GLubyte
);
377 xrb
->Base
.BlueBits
= 5 * sizeof(GLubyte
);
378 xrb
->Base
.AlphaBits
= 0;
381 xrb
->Base
.InternalFormat
= GL_RGB
;
382 xrb
->Base
._BaseFormat
= GL_RGB
;
383 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
384 xrb
->Base
.RedBits
= 3 * sizeof(GLubyte
);
385 xrb
->Base
.GreenBits
= 3 * sizeof(GLubyte
);
386 xrb
->Base
.BlueBits
= 2 * sizeof(GLubyte
);
387 xrb
->Base
.AlphaBits
= 0;
390 xrb
->Base
.InternalFormat
= GL_COLOR_INDEX8_EXT
;
391 xrb
->Base
._BaseFormat
= GL_COLOR_INDEX
;
392 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
393 xrb
->Base
.IndexBits
= 8 * sizeof(GLubyte
);
402 static __DRIdrawable
*
403 driCreateNewDrawable(__DRIscreen
*screen
,
404 const __DRIconfig
*config
, void *data
)
407 struct swrast_renderbuffer
*frontrb
, *backrb
;
411 buf
= _mesa_calloc(sizeof *buf
);
415 buf
->loaderPrivate
= data
;
417 buf
->driScreenPriv
= screen
;
419 buf
->row
= _mesa_malloc(MAX_WIDTH
* 4);
421 /* basic framebuffer setup */
422 _mesa_initialize_framebuffer(&buf
->Base
, &config
->modes
);
424 /* add front renderbuffer */
425 frontrb
= swrast_new_renderbuffer(&config
->modes
, GL_TRUE
);
426 _mesa_add_renderbuffer(&buf
->Base
, BUFFER_FRONT_LEFT
, &frontrb
->Base
);
428 /* add back renderbuffer */
429 if (config
->modes
.doubleBufferMode
) {
430 backrb
= swrast_new_renderbuffer(&config
->modes
, GL_FALSE
);
431 _mesa_add_renderbuffer(&buf
->Base
, BUFFER_BACK_LEFT
, &backrb
->Base
);
434 /* add software renderbuffers */
435 _mesa_add_soft_renderbuffers(&buf
->Base
,
436 GL_FALSE
, /* color */
437 config
->modes
.haveDepthBuffer
,
438 config
->modes
.haveStencilBuffer
,
439 config
->modes
.haveAccumBuffer
,
440 GL_FALSE
, /* alpha */
441 GL_FALSE
/* aux bufs */);
447 driDestroyDrawable(__DRIdrawable
*buf
)
452 struct gl_framebuffer
*fb
= &buf
->Base
;
454 _mesa_free(buf
->row
);
456 fb
->DeletePending
= GL_TRUE
;
457 _mesa_unreference_framebuffer(&fb
);
461 static void driSwapBuffers(__DRIdrawable
*buf
)
463 GET_CURRENT_CONTEXT(ctx
);
465 struct swrast_renderbuffer
*frontrb
=
466 swrast_renderbuffer(buf
->Base
.Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
467 struct swrast_renderbuffer
*backrb
=
468 swrast_renderbuffer(buf
->Base
.Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
);
470 __DRIscreen
*screen
= buf
->driScreenPriv
;
474 /* check for signle-buffered */
478 /* check if swapping currently bound buffer */
479 if (ctx
&& ctx
->DrawBuffer
== &(buf
->Base
)) {
480 /* flush pending rendering */
481 _mesa_notifySwapBuffers(ctx
);
484 screen
->swrast_loader
->putImage(buf
, __DRI_SWRAST_IMAGE_OP_SWAP
,
487 frontrb
->Base
.Height
,
494 * General device driver functions.
498 get_window_size( GLframebuffer
*fb
, GLsizei
*w
, GLsizei
*h
)
500 __DRIdrawable
*buf
= swrast_drawable(fb
);
501 __DRIscreen
*screen
= buf
->driScreenPriv
;
504 screen
->swrast_loader
->getDrawableInfo(buf
,
510 swrast_check_and_update_window_size( GLcontext
*ctx
, GLframebuffer
*fb
)
512 GLsizei width
, height
;
514 get_window_size(fb
, &width
, &height
);
515 if (fb
->Width
!= width
|| fb
->Height
!= height
) {
516 _mesa_resize_framebuffer(ctx
, fb
, width
, height
);
520 static const GLubyte
*
521 get_string(GLcontext
*ctx
, GLenum pname
)
526 return (const GLubyte
*) "Mesa Project";
528 return (const GLubyte
*) "Software Rasterizer";
535 update_state( GLcontext
*ctx
, GLuint new_state
)
537 /* not much to do here - pass it on */
538 _swrast_InvalidateState( ctx
, new_state
);
539 _swsetup_InvalidateState( ctx
, new_state
);
540 _vbo_InvalidateState( ctx
, new_state
);
541 _tnl_InvalidateState( ctx
, new_state
);
545 viewport(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
547 GLframebuffer
*draw
= ctx
->WinSysDrawBuffer
;
548 GLframebuffer
*read
= ctx
->WinSysReadBuffer
;
550 swrast_check_and_update_window_size(ctx
, draw
);
551 swrast_check_and_update_window_size(ctx
, read
);
555 swrast_init_driver_functions(struct dd_function_table
*driver
)
557 driver
->GetString
= get_string
;
558 driver
->UpdateState
= update_state
;
559 driver
->GetBufferSize
= NULL
;
560 driver
->Viewport
= viewport
;
565 * Context-related functions.
568 static __DRIcontext
*
569 driCreateNewContext(__DRIscreen
*screen
, const __DRIconfig
*config
,
570 __DRIcontext
*shared
, void *data
)
574 struct dd_function_table functions
;
578 ctx
= _mesa_calloc(sizeof *ctx
);
582 ctx
->loaderPrivate
= data
;
584 ctx
->driScreenPriv
= screen
;
586 /* build table of device driver functions */
587 _mesa_init_driver_functions(&functions
);
588 swrast_init_driver_functions(&functions
);
590 if (!_mesa_initialize_context(&ctx
->Base
, &config
->modes
,
591 shared
? &shared
->Base
: NULL
,
592 &functions
, (void *) ctx
)) {
597 mesaCtx
= &ctx
->Base
;
599 /* do bounds checking to prevent segfaults and server crashes! */
600 mesaCtx
->Const
.CheckArrayBounds
= GL_TRUE
;
602 /* create module contexts */
603 _swrast_CreateContext( mesaCtx
);
604 _vbo_CreateContext( mesaCtx
);
605 _tnl_CreateContext( mesaCtx
);
606 _swsetup_CreateContext( mesaCtx
);
607 _swsetup_Wakeup( mesaCtx
);
609 /* use default TCL pipeline */
611 TNLcontext
*tnl
= TNL_CONTEXT(mesaCtx
);
612 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
615 _mesa_enable_sw_extensions(mesaCtx
);
616 _mesa_enable_1_3_extensions(mesaCtx
);
617 _mesa_enable_1_4_extensions(mesaCtx
);
618 _mesa_enable_1_5_extensions(mesaCtx
);
619 _mesa_enable_2_0_extensions(mesaCtx
);
620 _mesa_enable_2_1_extensions(mesaCtx
);
626 driDestroyContext(__DRIcontext
*ctx
)
632 mesaCtx
= &ctx
->Base
;
633 _swsetup_DestroyContext( mesaCtx
);
634 _swrast_DestroyContext( mesaCtx
);
635 _tnl_DestroyContext( mesaCtx
);
636 _vbo_DestroyContext( mesaCtx
);
637 _mesa_destroy_context( mesaCtx
);
642 driCopyContext(__DRIcontext
*dst
, __DRIcontext
*src
, unsigned long mask
)
646 _mesa_copy_context(&src
->Base
, &dst
->Base
, mask
);
650 static int driBindContext(__DRIcontext
*ctx
,
655 GLframebuffer
*mesaDraw
;
656 GLframebuffer
*mesaRead
;
663 mesaCtx
= &ctx
->Base
;
664 mesaDraw
= &draw
->Base
;
665 mesaRead
= &read
->Base
;
667 /* check for same context and buffer */
668 if (mesaCtx
== _mesa_get_current_context()
669 && mesaCtx
->DrawBuffer
== mesaDraw
670 && mesaCtx
->ReadBuffer
== mesaRead
) {
674 _glapi_check_multithread();
676 swrast_check_and_update_window_size(mesaCtx
, mesaDraw
);
678 swrast_check_and_update_window_size(mesaCtx
, mesaRead
);
680 _mesa_make_current( mesaCtx
,
686 _mesa_make_current( NULL
, NULL
, NULL
);
692 static int driUnbindContext(__DRIcontext
*ctx
)
696 _mesa_make_current(NULL
, NULL
, NULL
);
701 static const __DRIcoreExtension driCoreExtension
= {
702 { __DRI_CORE
, __DRI_CORE_VERSION
},
703 NULL
, /* driCreateNewScreen */
707 driIndexConfigAttrib
,
708 NULL
, /* driCreateNewDrawable */
718 static const __DRIswrastExtension driSWRastExtension
= {
719 { __DRI_SWRAST
, __DRI_SWRAST_VERSION
},
724 /* This is the table of extensions that the loader will dlsym() for. */
725 PUBLIC
const __DRIextension
*__driDriverExtensions
[] = {
726 &driCoreExtension
.base
,
727 &driSWRastExtension
.base
,