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.
34 #include "main/context.h"
35 #include "main/extensions.h"
36 #include "main/framebuffer.h"
37 #include "main/imports.h"
38 #include "main/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"
46 #include "drivers/common/meta.h"
49 #include "swrast_priv.h"
52 #define need_GL_VERSION_1_3
53 #define need_GL_VERSION_1_4
54 #define need_GL_VERSION_1_5
55 #define need_GL_VERSION_2_0
56 #define need_GL_VERSION_2_1
58 /* sw extensions for imaging */
59 #define need_GL_EXT_blend_color
60 #define need_GL_EXT_blend_minmax
61 #define need_GL_EXT_convolution
62 #define need_GL_EXT_histogram
63 #define need_GL_SGI_color_table
65 /* sw extensions not associated with some GL version */
66 #define need_GL_ARB_draw_elements_base_vertex
67 #define need_GL_ARB_shader_objects
68 #define need_GL_ARB_vertex_array_object
69 #define need_GL_ARB_vertex_program
70 #define need_GL_ARB_sync
71 #define need_GL_APPLE_vertex_array_object
72 #define need_GL_ATI_fragment_shader
73 #define need_GL_ATI_separate_stencil
74 #define need_GL_EXT_depth_bounds_test
75 #define need_GL_EXT_framebuffer_object
76 #define need_GL_EXT_framebuffer_blit
77 #define need_GL_EXT_gpu_program_parameters
78 #define need_GL_EXT_paletted_texture
79 #define need_GL_EXT_stencil_two_side
80 #define need_GL_MESA_resize_buffers
81 #define need_GL_NV_vertex_program
82 #define need_GL_NV_fragment_program
84 #include "extension_helper.h"
86 const struct dri_extension card_extensions
[] =
88 { "GL_VERSION_1_3", GL_VERSION_1_3_functions
},
89 { "GL_VERSION_1_4", GL_VERSION_1_4_functions
},
90 { "GL_VERSION_1_5", GL_VERSION_1_5_functions
},
91 { "GL_VERSION_2_0", GL_VERSION_2_0_functions
},
92 { "GL_VERSION_2_1", GL_VERSION_2_1_functions
},
94 { "GL_EXT_blend_color", GL_EXT_blend_color_functions
},
95 { "GL_EXT_blend_minmax", GL_EXT_blend_minmax_functions
},
96 { "GL_EXT_convolution", GL_EXT_convolution_functions
},
97 { "GL_EXT_histogram", GL_EXT_histogram_functions
},
98 { "GL_SGI_color_table", GL_SGI_color_table_functions
},
100 { "GL_ARB_depth_clamp", NULL
},
101 { "GL_ARB_draw_elements_base_vertex", GL_ARB_draw_elements_base_vertex_functions
},
102 { "GL_ARB_shader_objects", GL_ARB_shader_objects_functions
},
103 { "GL_ARB_vertex_array_object", GL_ARB_vertex_array_object_functions
},
104 { "GL_ARB_vertex_program", GL_ARB_vertex_program_functions
},
105 { "GL_ARB_sync", GL_ARB_sync_functions
},
106 { "GL_APPLE_vertex_array_object", GL_APPLE_vertex_array_object_functions
},
107 { "GL_ATI_fragment_shader", GL_ATI_fragment_shader_functions
},
108 { "GL_ATI_separate_stencil", GL_ATI_separate_stencil_functions
},
109 { "GL_EXT_depth_bounds_test", GL_EXT_depth_bounds_test_functions
},
110 { "GL_EXT_framebuffer_object", GL_EXT_framebuffer_object_functions
},
111 { "GL_EXT_framebuffer_blit", GL_EXT_framebuffer_blit_functions
},
112 { "GL_EXT_gpu_program_parameters", GL_EXT_gpu_program_parameters_functions
},
113 { "GL_EXT_paletted_texture", GL_EXT_paletted_texture_functions
},
114 { "GL_EXT_stencil_two_side", GL_EXT_stencil_two_side_functions
},
115 { "GL_MESA_resize_buffers", GL_MESA_resize_buffers_functions
},
116 { "GL_NV_depth_clamp", NULL
},
117 { "GL_NV_vertex_program", GL_NV_vertex_program_functions
},
118 { "GL_NV_fragment_program", GL_NV_fragment_program_functions
},
119 { "GL_NV_fragment_program_option", NULL
},
125 * Screen and config-related functions
129 setupLoaderExtensions(__DRIscreen
*psp
,
130 const __DRIextension
**extensions
)
134 for (i
= 0; extensions
[i
]; i
++) {
135 if (strcmp(extensions
[i
]->name
, __DRI_SWRAST_LOADER
) == 0)
136 psp
->swrast_loader
= (__DRIswrastLoaderExtension
*) extensions
[i
];
140 static __DRIconfig
**
141 swrastFillInModes(__DRIscreen
*psp
,
142 unsigned pixel_bits
, unsigned depth_bits
,
143 unsigned stencil_bits
, GLboolean have_back_buffer
)
145 __DRIconfig
**configs
;
146 unsigned depth_buffer_factor
;
147 unsigned back_buffer_factor
;
151 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
152 * support pageflipping at all.
154 static const GLenum back_buffer_modes
[] = {
155 GLX_NONE
, GLX_SWAP_UNDEFINED_OML
158 uint8_t depth_bits_array
[4];
159 uint8_t stencil_bits_array
[4];
160 uint8_t msaa_samples_array
[1];
162 depth_bits_array
[0] = 0;
163 depth_bits_array
[1] = 0;
164 depth_bits_array
[2] = depth_bits
;
165 depth_bits_array
[3] = depth_bits
;
167 /* Just like with the accumulation buffer, always provide some modes
168 * with a stencil buffer.
170 stencil_bits_array
[0] = 0;
171 stencil_bits_array
[1] = (stencil_bits
== 0) ? 8 : stencil_bits
;
172 stencil_bits_array
[2] = 0;
173 stencil_bits_array
[3] = (stencil_bits
== 0) ? 8 : stencil_bits
;
175 msaa_samples_array
[0] = 0;
177 depth_buffer_factor
= 4;
178 back_buffer_factor
= 2;
180 switch (pixel_bits
) {
183 fb_type
= GL_UNSIGNED_BYTE_2_3_3_REV
;
187 fb_type
= GL_UNSIGNED_SHORT_5_6_5
;
191 fb_type
= GL_UNSIGNED_INT_8_8_8_8_REV
;
195 fb_type
= GL_UNSIGNED_INT_8_8_8_8_REV
;
198 fprintf(stderr
, "[%s:%u] bad depth %d\n", __func__
, __LINE__
,
203 configs
= driCreateConfigs(fb_format
, fb_type
,
204 depth_bits_array
, stencil_bits_array
,
205 depth_buffer_factor
, back_buffer_modes
,
206 back_buffer_factor
, msaa_samples_array
, 1);
207 if (configs
== NULL
) {
208 fprintf(stderr
, "[%s:%u] Error creating FBConfig!\n", __func__
,
217 driCreateNewScreen(int scrn
, const __DRIextension
**extensions
,
218 const __DRIconfig
***driver_configs
, void *data
)
220 static const __DRIextension
*emptyExtensionList
[] = { NULL
};
222 __DRIconfig
**configs8
, **configs16
, **configs24
, **configs32
;
228 psp
= _mesa_calloc(sizeof(*psp
));
232 setupLoaderExtensions(psp
, extensions
);
235 psp
->extensions
= emptyExtensionList
;
237 configs8
= swrastFillInModes(psp
, 8, 8, 0, 1);
238 configs16
= swrastFillInModes(psp
, 16, 16, 0, 1);
239 configs24
= swrastFillInModes(psp
, 24, 24, 8, 1);
240 configs32
= swrastFillInModes(psp
, 32, 24, 8, 1);
242 configs16
= driConcatConfigs(configs8
, configs16
);
243 configs24
= driConcatConfigs(configs16
, configs24
);
244 *driver_configs
= (const __DRIconfig
**)
245 driConcatConfigs(configs24
, configs32
);
247 driInitExtensions( NULL
, card_extensions
, GL_FALSE
);
252 static void driDestroyScreen(__DRIscreen
*psp
)
261 static const __DRIextension
**driGetExtensions(__DRIscreen
*psp
)
265 return psp
->extensions
;
270 * Framebuffer and renderbuffer-related functions.
274 choose_pixel_format(const GLvisual
*v
)
277 int depth
= v
->rgbBits
;
280 && v
->redMask
== 0xff0000
281 && v
->greenMask
== 0x00ff00
282 && v
->blueMask
== 0x0000ff)
285 && v
->redMask
== 0xff0000
286 && v
->greenMask
== 0x00ff00
287 && v
->blueMask
== 0x0000ff)
290 && v
->redMask
== 0xf800
291 && v
->greenMask
== 0x07e0
292 && v
->blueMask
== 0x001f)
295 && v
->redMask
== 0x07
296 && v
->greenMask
== 0x38
297 && v
->blueMask
== 0xc0)
301 if (v
->indexBits
== 8)
305 _mesa_problem( NULL
, "unexpected format in %s", __FUNCTION__
);
310 swrast_delete_renderbuffer(struct gl_renderbuffer
*rb
)
314 _mesa_free(rb
->Data
);
319 swrast_alloc_front_storage(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
320 GLenum internalFormat
, GLuint width
, GLuint height
)
322 struct swrast_renderbuffer
*xrb
= swrast_renderbuffer(rb
);
323 unsigned mask
= PITCH_ALIGN_BITS
- 1;
331 /* always pad to PITCH_ALIGN_BITS */
332 xrb
->pitch
= ((width
* xrb
->bpp
+ mask
) & ~mask
) / 8;
338 swrast_alloc_back_storage(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
339 GLenum internalFormat
, GLuint width
, GLuint height
)
341 struct swrast_renderbuffer
*xrb
= swrast_renderbuffer(rb
);
345 _mesa_free(rb
->Data
);
347 swrast_alloc_front_storage(ctx
, rb
, internalFormat
, width
, height
);
349 rb
->Data
= _mesa_malloc(height
* xrb
->pitch
);
354 static struct swrast_renderbuffer
*
355 swrast_new_renderbuffer(const GLvisual
*visual
, GLboolean front
)
357 struct swrast_renderbuffer
*xrb
= _mesa_calloc(sizeof *xrb
);
365 _mesa_init_renderbuffer(&xrb
->Base
, 0);
367 pixel_format
= choose_pixel_format(visual
);
369 xrb
->Base
.Delete
= swrast_delete_renderbuffer
;
371 xrb
->Base
.AllocStorage
= swrast_alloc_front_storage
;
372 swrast_set_span_funcs_front(xrb
, pixel_format
);
375 xrb
->Base
.AllocStorage
= swrast_alloc_back_storage
;
376 swrast_set_span_funcs_back(xrb
, pixel_format
);
379 switch (pixel_format
) {
381 xrb
->Base
.InternalFormat
= GL_RGBA
;
382 xrb
->Base
._BaseFormat
= GL_RGBA
;
383 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
384 xrb
->Base
.RedBits
= 8 * sizeof(GLubyte
);
385 xrb
->Base
.GreenBits
= 8 * sizeof(GLubyte
);
386 xrb
->Base
.BlueBits
= 8 * sizeof(GLubyte
);
387 xrb
->Base
.AlphaBits
= 8 * sizeof(GLubyte
);
391 xrb
->Base
.InternalFormat
= GL_RGB
;
392 xrb
->Base
._BaseFormat
= GL_RGB
;
393 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
394 xrb
->Base
.RedBits
= 8 * sizeof(GLubyte
);
395 xrb
->Base
.GreenBits
= 8 * sizeof(GLubyte
);
396 xrb
->Base
.BlueBits
= 8 * sizeof(GLubyte
);
397 xrb
->Base
.AlphaBits
= 0;
401 xrb
->Base
.InternalFormat
= GL_RGB
;
402 xrb
->Base
._BaseFormat
= GL_RGB
;
403 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
404 xrb
->Base
.RedBits
= 5 * sizeof(GLubyte
);
405 xrb
->Base
.GreenBits
= 6 * sizeof(GLubyte
);
406 xrb
->Base
.BlueBits
= 5 * sizeof(GLubyte
);
407 xrb
->Base
.AlphaBits
= 0;
411 xrb
->Base
.InternalFormat
= GL_RGB
;
412 xrb
->Base
._BaseFormat
= GL_RGB
;
413 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
414 xrb
->Base
.RedBits
= 3 * sizeof(GLubyte
);
415 xrb
->Base
.GreenBits
= 3 * sizeof(GLubyte
);
416 xrb
->Base
.BlueBits
= 2 * sizeof(GLubyte
);
417 xrb
->Base
.AlphaBits
= 0;
421 xrb
->Base
.InternalFormat
= GL_COLOR_INDEX8_EXT
;
422 xrb
->Base
._BaseFormat
= GL_COLOR_INDEX
;
423 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
424 xrb
->Base
.IndexBits
= 8 * sizeof(GLubyte
);
434 static __DRIdrawable
*
435 driCreateNewDrawable(__DRIscreen
*screen
,
436 const __DRIconfig
*config
, void *data
)
439 struct swrast_renderbuffer
*frontrb
, *backrb
;
443 buf
= _mesa_calloc(sizeof *buf
);
447 buf
->loaderPrivate
= data
;
449 buf
->driScreenPriv
= screen
;
451 buf
->row
= _mesa_malloc(MAX_WIDTH
* 4);
453 /* basic framebuffer setup */
454 _mesa_initialize_framebuffer(&buf
->Base
, &config
->modes
);
456 /* add front renderbuffer */
457 frontrb
= swrast_new_renderbuffer(&config
->modes
, GL_TRUE
);
458 _mesa_add_renderbuffer(&buf
->Base
, BUFFER_FRONT_LEFT
, &frontrb
->Base
);
460 /* add back renderbuffer */
461 if (config
->modes
.doubleBufferMode
) {
462 backrb
= swrast_new_renderbuffer(&config
->modes
, GL_FALSE
);
463 _mesa_add_renderbuffer(&buf
->Base
, BUFFER_BACK_LEFT
, &backrb
->Base
);
466 /* add software renderbuffers */
467 _mesa_add_soft_renderbuffers(&buf
->Base
,
468 GL_FALSE
, /* color */
469 config
->modes
.haveDepthBuffer
,
470 config
->modes
.haveStencilBuffer
,
471 config
->modes
.haveAccumBuffer
,
472 GL_FALSE
, /* alpha */
473 GL_FALSE
/* aux bufs */);
479 driDestroyDrawable(__DRIdrawable
*buf
)
484 struct gl_framebuffer
*fb
= &buf
->Base
;
486 _mesa_free(buf
->row
);
488 fb
->DeletePending
= GL_TRUE
;
489 _mesa_reference_framebuffer(&fb
, NULL
);
493 static void driSwapBuffers(__DRIdrawable
*buf
)
495 GET_CURRENT_CONTEXT(ctx
);
497 struct swrast_renderbuffer
*frontrb
=
498 swrast_renderbuffer(buf
->Base
.Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
499 struct swrast_renderbuffer
*backrb
=
500 swrast_renderbuffer(buf
->Base
.Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
);
502 __DRIscreen
*screen
= buf
->driScreenPriv
;
506 /* check for signle-buffered */
510 /* check if swapping currently bound buffer */
511 if (ctx
&& ctx
->DrawBuffer
== &(buf
->Base
)) {
512 /* flush pending rendering */
513 _mesa_notifySwapBuffers(ctx
);
516 screen
->swrast_loader
->putImage(buf
, __DRI_SWRAST_IMAGE_OP_SWAP
,
519 frontrb
->Base
.Height
,
526 * General device driver functions.
530 get_window_size( GLframebuffer
*fb
, GLsizei
*w
, GLsizei
*h
)
532 __DRIdrawable
*buf
= swrast_drawable(fb
);
533 __DRIscreen
*screen
= buf
->driScreenPriv
;
536 screen
->swrast_loader
->getDrawableInfo(buf
,
542 swrast_check_and_update_window_size( GLcontext
*ctx
, GLframebuffer
*fb
)
544 GLsizei width
, height
;
546 get_window_size(fb
, &width
, &height
);
547 if (fb
->Width
!= width
|| fb
->Height
!= height
) {
548 _mesa_resize_framebuffer(ctx
, fb
, width
, height
);
552 static const GLubyte
*
553 get_string(GLcontext
*ctx
, GLenum pname
)
558 return (const GLubyte
*) "Mesa Project";
560 return (const GLubyte
*) "Software Rasterizer";
567 update_state( GLcontext
*ctx
, GLuint new_state
)
569 /* not much to do here - pass it on */
570 _swrast_InvalidateState( ctx
, new_state
);
571 _swsetup_InvalidateState( ctx
, new_state
);
572 _vbo_InvalidateState( ctx
, new_state
);
573 _tnl_InvalidateState( ctx
, new_state
);
577 viewport(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
579 GLframebuffer
*draw
= ctx
->WinSysDrawBuffer
;
580 GLframebuffer
*read
= ctx
->WinSysReadBuffer
;
582 swrast_check_and_update_window_size(ctx
, draw
);
583 swrast_check_and_update_window_size(ctx
, read
);
587 swrast_init_driver_functions(struct dd_function_table
*driver
)
589 driver
->GetString
= get_string
;
590 driver
->UpdateState
= update_state
;
591 driver
->GetBufferSize
= NULL
;
592 driver
->Viewport
= viewport
;
597 * Context-related functions.
600 static __DRIcontext
*
601 driCreateNewContext(__DRIscreen
*screen
, const __DRIconfig
*config
,
602 __DRIcontext
*shared
, void *data
)
606 struct dd_function_table functions
;
610 ctx
= _mesa_calloc(sizeof *ctx
);
614 ctx
->loaderPrivate
= data
;
616 ctx
->driScreenPriv
= screen
;
618 /* build table of device driver functions */
619 _mesa_init_driver_functions(&functions
);
620 swrast_init_driver_functions(&functions
);
622 if (!_mesa_initialize_context(&ctx
->Base
, &config
->modes
,
623 shared
? &shared
->Base
: NULL
,
624 &functions
, (void *) ctx
)) {
629 mesaCtx
= &ctx
->Base
;
631 /* do bounds checking to prevent segfaults and server crashes! */
632 mesaCtx
->Const
.CheckArrayBounds
= GL_TRUE
;
634 /* create module contexts */
635 _swrast_CreateContext( mesaCtx
);
636 _vbo_CreateContext( mesaCtx
);
637 _tnl_CreateContext( mesaCtx
);
638 _swsetup_CreateContext( mesaCtx
);
639 _swsetup_Wakeup( mesaCtx
);
641 /* use default TCL pipeline */
643 TNLcontext
*tnl
= TNL_CONTEXT(mesaCtx
);
644 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
647 _mesa_enable_sw_extensions(mesaCtx
);
648 _mesa_enable_1_3_extensions(mesaCtx
);
649 _mesa_enable_1_4_extensions(mesaCtx
);
650 _mesa_enable_1_5_extensions(mesaCtx
);
651 _mesa_enable_2_0_extensions(mesaCtx
);
652 _mesa_enable_2_1_extensions(mesaCtx
);
654 _mesa_meta_init(mesaCtx
);
660 driDestroyContext(__DRIcontext
*ctx
)
666 mesaCtx
= &ctx
->Base
;
667 _mesa_meta_free(mesaCtx
);
668 _swsetup_DestroyContext( mesaCtx
);
669 _swrast_DestroyContext( mesaCtx
);
670 _tnl_DestroyContext( mesaCtx
);
671 _vbo_DestroyContext( mesaCtx
);
672 _mesa_destroy_context( mesaCtx
);
677 driCopyContext(__DRIcontext
*dst
, __DRIcontext
*src
, unsigned long mask
)
681 _mesa_copy_context(&src
->Base
, &dst
->Base
, mask
);
685 static int driBindContext(__DRIcontext
*ctx
,
690 GLframebuffer
*mesaDraw
;
691 GLframebuffer
*mesaRead
;
698 mesaCtx
= &ctx
->Base
;
699 mesaDraw
= &draw
->Base
;
700 mesaRead
= &read
->Base
;
702 /* check for same context and buffer */
703 if (mesaCtx
== _mesa_get_current_context()
704 && mesaCtx
->DrawBuffer
== mesaDraw
705 && mesaCtx
->ReadBuffer
== mesaRead
) {
709 _glapi_check_multithread();
711 swrast_check_and_update_window_size(mesaCtx
, mesaDraw
);
713 swrast_check_and_update_window_size(mesaCtx
, mesaRead
);
715 _mesa_make_current( mesaCtx
,
721 _mesa_make_current( NULL
, NULL
, NULL
);
727 static int driUnbindContext(__DRIcontext
*ctx
)
735 static const __DRIcoreExtension driCoreExtension
= {
736 { __DRI_CORE
, __DRI_CORE_VERSION
},
737 NULL
, /* driCreateNewScreen */
741 driIndexConfigAttrib
,
742 NULL
, /* driCreateNewDrawable */
752 static const __DRIswrastExtension driSWRastExtension
= {
753 { __DRI_SWRAST
, __DRI_SWRAST_VERSION
},
758 /* This is the table of extensions that the loader will dlsym() for. */
759 PUBLIC
const __DRIextension
*__driDriverExtensions
[] = {
760 &driCoreExtension
.base
,
761 &driSWRastExtension
.base
,