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"
48 #include "swrast_priv.h"
51 #define need_GL_VERSION_1_3
52 #define need_GL_VERSION_1_4
53 #define need_GL_VERSION_1_5
54 #define need_GL_VERSION_2_0
55 #define need_GL_VERSION_2_1
57 /* sw extensions for imaging */
58 #define need_GL_EXT_blend_color
59 #define need_GL_EXT_blend_minmax
60 #define need_GL_EXT_convolution
61 #define need_GL_EXT_histogram
62 #define need_GL_SGI_color_table
64 /* sw extensions not associated with some GL version */
65 #define need_GL_ARB_shader_objects
66 #define need_GL_ARB_vertex_array_object
67 #define need_GL_ARB_vertex_program
68 #define need_GL_ARB_sync
69 #define need_GL_APPLE_vertex_array_object
70 #define need_GL_ATI_fragment_shader
71 #define need_GL_ATI_separate_stencil
72 #define need_GL_EXT_depth_bounds_test
73 #define need_GL_EXT_framebuffer_object
74 #define need_GL_EXT_framebuffer_blit
75 #define need_GL_EXT_gpu_program_parameters
76 #define need_GL_EXT_paletted_texture
77 #define need_GL_EXT_stencil_two_side
78 #define need_GL_MESA_resize_buffers
79 #define need_GL_NV_vertex_program
80 #define need_GL_NV_fragment_program
82 #include "extension_helper.h"
84 const struct dri_extension card_extensions
[] =
86 { "GL_VERSION_1_3", GL_VERSION_1_3_functions
},
87 { "GL_VERSION_1_4", GL_VERSION_1_4_functions
},
88 { "GL_VERSION_1_5", GL_VERSION_1_5_functions
},
89 { "GL_VERSION_2_0", GL_VERSION_2_0_functions
},
90 { "GL_VERSION_2_1", GL_VERSION_2_1_functions
},
92 { "GL_EXT_blend_color", GL_EXT_blend_color_functions
},
93 { "GL_EXT_blend_minmax", GL_EXT_blend_minmax_functions
},
94 { "GL_EXT_convolution", GL_EXT_convolution_functions
},
95 { "GL_EXT_histogram", GL_EXT_histogram_functions
},
96 { "GL_SGI_color_table", GL_SGI_color_table_functions
},
98 { "GL_ARB_shader_objects", GL_ARB_shader_objects_functions
},
99 { "GL_ARB_vertex_array_object", GL_ARB_vertex_array_object_functions
},
100 { "GL_ARB_vertex_program", GL_ARB_vertex_program_functions
},
101 { "GL_ARB_sync", GL_ARB_sync
},
102 { "GL_APPLE_vertex_array_object", GL_APPLE_vertex_array_object_functions
},
103 { "GL_ATI_fragment_shader", GL_ATI_fragment_shader_functions
},
104 { "GL_ATI_separate_stencil", GL_ATI_separate_stencil_functions
},
105 { "GL_EXT_depth_bounds_test", GL_EXT_depth_bounds_test_functions
},
106 { "GL_EXT_framebuffer_object", GL_EXT_framebuffer_object_functions
},
107 { "GL_EXT_framebuffer_blit", GL_EXT_framebuffer_blit_functions
},
108 { "GL_EXT_gpu_program_parameters", GL_EXT_gpu_program_parameters_functions
},
109 { "GL_EXT_paletted_texture", GL_EXT_paletted_texture_functions
},
110 { "GL_EXT_stencil_two_side", GL_EXT_stencil_two_side_functions
},
111 { "GL_MESA_resize_buffers", GL_MESA_resize_buffers_functions
},
112 { "GL_NV_vertex_program", GL_NV_vertex_program_functions
},
113 { "GL_NV_fragment_program", GL_NV_fragment_program_functions
},
119 * Screen and config-related functions
123 setupLoaderExtensions(__DRIscreen
*psp
,
124 const __DRIextension
**extensions
)
128 for (i
= 0; extensions
[i
]; i
++) {
129 if (strcmp(extensions
[i
]->name
, __DRI_SWRAST_LOADER
) == 0)
130 psp
->swrast_loader
= (__DRIswrastLoaderExtension
*) extensions
[i
];
134 static __DRIconfig
**
135 swrastFillInModes(__DRIscreen
*psp
,
136 unsigned pixel_bits
, unsigned depth_bits
,
137 unsigned stencil_bits
, GLboolean have_back_buffer
)
139 __DRIconfig
**configs
;
140 unsigned depth_buffer_factor
;
141 unsigned back_buffer_factor
;
145 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
146 * support pageflipping at all.
148 static const GLenum back_buffer_modes
[] = {
149 GLX_NONE
, GLX_SWAP_UNDEFINED_OML
152 uint8_t depth_bits_array
[4];
153 uint8_t stencil_bits_array
[4];
154 uint8_t msaa_samples_array
[1];
156 depth_bits_array
[0] = 0;
157 depth_bits_array
[1] = 0;
158 depth_bits_array
[2] = depth_bits
;
159 depth_bits_array
[3] = depth_bits
;
161 /* Just like with the accumulation buffer, always provide some modes
162 * with a stencil buffer.
164 stencil_bits_array
[0] = 0;
165 stencil_bits_array
[1] = (stencil_bits
== 0) ? 8 : stencil_bits
;
166 stencil_bits_array
[2] = 0;
167 stencil_bits_array
[3] = (stencil_bits
== 0) ? 8 : stencil_bits
;
169 msaa_samples_array
[0] = 0;
171 depth_buffer_factor
= 4;
172 back_buffer_factor
= 2;
174 switch (pixel_bits
) {
177 fb_type
= GL_UNSIGNED_BYTE_2_3_3_REV
;
181 fb_type
= GL_UNSIGNED_SHORT_5_6_5
;
185 fb_type
= GL_UNSIGNED_INT_8_8_8_8_REV
;
189 fb_type
= GL_UNSIGNED_INT_8_8_8_8_REV
;
192 fprintf(stderr
, "[%s:%u] bad depth %d\n", __func__
, __LINE__
,
197 configs
= driCreateConfigs(fb_format
, fb_type
,
198 depth_bits_array
, stencil_bits_array
,
199 depth_buffer_factor
, back_buffer_modes
,
200 back_buffer_factor
, msaa_samples_array
, 1);
201 if (configs
== NULL
) {
202 fprintf(stderr
, "[%s:%u] Error creating FBConfig!\n", __func__
,
211 driCreateNewScreen(int scrn
, const __DRIextension
**extensions
,
212 const __DRIconfig
***driver_configs
, void *data
)
214 static const __DRIextension
*emptyExtensionList
[] = { NULL
};
216 __DRIconfig
**configs8
, **configs16
, **configs24
, **configs32
;
222 psp
= _mesa_calloc(sizeof(*psp
));
226 setupLoaderExtensions(psp
, extensions
);
229 psp
->extensions
= emptyExtensionList
;
231 configs8
= swrastFillInModes(psp
, 8, 8, 0, 1);
232 configs16
= swrastFillInModes(psp
, 16, 16, 0, 1);
233 configs24
= swrastFillInModes(psp
, 24, 24, 8, 1);
234 configs32
= swrastFillInModes(psp
, 32, 24, 8, 1);
236 configs16
= driConcatConfigs(configs8
, configs16
);
237 configs24
= driConcatConfigs(configs16
, configs24
);
238 *driver_configs
= (const __DRIconfig
**)
239 driConcatConfigs(configs24
, configs32
);
241 driInitExtensions( NULL
, card_extensions
, GL_FALSE
);
246 static void driDestroyScreen(__DRIscreen
*psp
)
255 static const __DRIextension
**driGetExtensions(__DRIscreen
*psp
)
259 return psp
->extensions
;
264 * Framebuffer and renderbuffer-related functions.
268 choose_pixel_format(const GLvisual
*v
)
271 int depth
= v
->rgbBits
;
274 && v
->redMask
== 0xff0000
275 && v
->greenMask
== 0x00ff00
276 && v
->blueMask
== 0x0000ff)
279 && v
->redMask
== 0xff0000
280 && v
->greenMask
== 0x00ff00
281 && v
->blueMask
== 0x0000ff)
284 && v
->redMask
== 0xf800
285 && v
->greenMask
== 0x07e0
286 && v
->blueMask
== 0x001f)
289 && v
->redMask
== 0x07
290 && v
->greenMask
== 0x38
291 && v
->blueMask
== 0xc0)
295 if (v
->indexBits
== 8)
299 _mesa_problem( NULL
, "unexpected format in %s", __FUNCTION__
);
304 swrast_delete_renderbuffer(struct gl_renderbuffer
*rb
)
308 _mesa_free(rb
->Data
);
313 swrast_alloc_front_storage(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
314 GLenum internalFormat
, GLuint width
, GLuint height
)
316 struct swrast_renderbuffer
*xrb
= swrast_renderbuffer(rb
);
317 unsigned mask
= PITCH_ALIGN_BITS
- 1;
325 /* always pad to PITCH_ALIGN_BITS */
326 xrb
->pitch
= ((width
* xrb
->bpp
+ mask
) & ~mask
) / 8;
332 swrast_alloc_back_storage(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
333 GLenum internalFormat
, GLuint width
, GLuint height
)
335 struct swrast_renderbuffer
*xrb
= swrast_renderbuffer(rb
);
339 _mesa_free(rb
->Data
);
341 swrast_alloc_front_storage(ctx
, rb
, internalFormat
, width
, height
);
343 rb
->Data
= _mesa_malloc(height
* xrb
->pitch
);
348 static struct swrast_renderbuffer
*
349 swrast_new_renderbuffer(const GLvisual
*visual
, GLboolean front
)
351 struct swrast_renderbuffer
*xrb
= _mesa_calloc(sizeof *xrb
);
359 _mesa_init_renderbuffer(&xrb
->Base
, 0);
361 pixel_format
= choose_pixel_format(visual
);
363 xrb
->Base
.Delete
= swrast_delete_renderbuffer
;
365 xrb
->Base
.AllocStorage
= swrast_alloc_front_storage
;
366 swrast_set_span_funcs_front(xrb
, pixel_format
);
369 xrb
->Base
.AllocStorage
= swrast_alloc_back_storage
;
370 swrast_set_span_funcs_back(xrb
, pixel_format
);
373 switch (pixel_format
) {
375 xrb
->Base
.InternalFormat
= GL_RGBA
;
376 xrb
->Base
._BaseFormat
= GL_RGBA
;
377 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
378 xrb
->Base
.RedBits
= 8 * sizeof(GLubyte
);
379 xrb
->Base
.GreenBits
= 8 * sizeof(GLubyte
);
380 xrb
->Base
.BlueBits
= 8 * sizeof(GLubyte
);
381 xrb
->Base
.AlphaBits
= 8 * sizeof(GLubyte
);
385 xrb
->Base
.InternalFormat
= GL_RGB
;
386 xrb
->Base
._BaseFormat
= GL_RGB
;
387 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
388 xrb
->Base
.RedBits
= 8 * sizeof(GLubyte
);
389 xrb
->Base
.GreenBits
= 8 * sizeof(GLubyte
);
390 xrb
->Base
.BlueBits
= 8 * sizeof(GLubyte
);
391 xrb
->Base
.AlphaBits
= 0;
395 xrb
->Base
.InternalFormat
= GL_RGB
;
396 xrb
->Base
._BaseFormat
= GL_RGB
;
397 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
398 xrb
->Base
.RedBits
= 5 * sizeof(GLubyte
);
399 xrb
->Base
.GreenBits
= 6 * sizeof(GLubyte
);
400 xrb
->Base
.BlueBits
= 5 * sizeof(GLubyte
);
401 xrb
->Base
.AlphaBits
= 0;
405 xrb
->Base
.InternalFormat
= GL_RGB
;
406 xrb
->Base
._BaseFormat
= GL_RGB
;
407 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
408 xrb
->Base
.RedBits
= 3 * sizeof(GLubyte
);
409 xrb
->Base
.GreenBits
= 3 * sizeof(GLubyte
);
410 xrb
->Base
.BlueBits
= 2 * sizeof(GLubyte
);
411 xrb
->Base
.AlphaBits
= 0;
415 xrb
->Base
.InternalFormat
= GL_COLOR_INDEX8_EXT
;
416 xrb
->Base
._BaseFormat
= GL_COLOR_INDEX
;
417 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
418 xrb
->Base
.IndexBits
= 8 * sizeof(GLubyte
);
428 static __DRIdrawable
*
429 driCreateNewDrawable(__DRIscreen
*screen
,
430 const __DRIconfig
*config
, void *data
)
433 struct swrast_renderbuffer
*frontrb
, *backrb
;
437 buf
= _mesa_calloc(sizeof *buf
);
441 buf
->loaderPrivate
= data
;
443 buf
->driScreenPriv
= screen
;
445 buf
->row
= _mesa_malloc(MAX_WIDTH
* 4);
447 /* basic framebuffer setup */
448 _mesa_initialize_framebuffer(&buf
->Base
, &config
->modes
);
450 /* add front renderbuffer */
451 frontrb
= swrast_new_renderbuffer(&config
->modes
, GL_TRUE
);
452 _mesa_add_renderbuffer(&buf
->Base
, BUFFER_FRONT_LEFT
, &frontrb
->Base
);
454 /* add back renderbuffer */
455 if (config
->modes
.doubleBufferMode
) {
456 backrb
= swrast_new_renderbuffer(&config
->modes
, GL_FALSE
);
457 _mesa_add_renderbuffer(&buf
->Base
, BUFFER_BACK_LEFT
, &backrb
->Base
);
460 /* add software renderbuffers */
461 _mesa_add_soft_renderbuffers(&buf
->Base
,
462 GL_FALSE
, /* color */
463 config
->modes
.haveDepthBuffer
,
464 config
->modes
.haveStencilBuffer
,
465 config
->modes
.haveAccumBuffer
,
466 GL_FALSE
, /* alpha */
467 GL_FALSE
/* aux bufs */);
473 driDestroyDrawable(__DRIdrawable
*buf
)
478 struct gl_framebuffer
*fb
= &buf
->Base
;
480 _mesa_free(buf
->row
);
482 fb
->DeletePending
= GL_TRUE
;
483 _mesa_reference_framebuffer(&fb
, NULL
);
487 static void driSwapBuffers(__DRIdrawable
*buf
)
489 GET_CURRENT_CONTEXT(ctx
);
491 struct swrast_renderbuffer
*frontrb
=
492 swrast_renderbuffer(buf
->Base
.Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
493 struct swrast_renderbuffer
*backrb
=
494 swrast_renderbuffer(buf
->Base
.Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
);
496 __DRIscreen
*screen
= buf
->driScreenPriv
;
500 /* check for signle-buffered */
504 /* check if swapping currently bound buffer */
505 if (ctx
&& ctx
->DrawBuffer
== &(buf
->Base
)) {
506 /* flush pending rendering */
507 _mesa_notifySwapBuffers(ctx
);
510 screen
->swrast_loader
->putImage(buf
, __DRI_SWRAST_IMAGE_OP_SWAP
,
513 frontrb
->Base
.Height
,
520 * General device driver functions.
524 get_window_size( GLframebuffer
*fb
, GLsizei
*w
, GLsizei
*h
)
526 __DRIdrawable
*buf
= swrast_drawable(fb
);
527 __DRIscreen
*screen
= buf
->driScreenPriv
;
530 screen
->swrast_loader
->getDrawableInfo(buf
,
536 swrast_check_and_update_window_size( GLcontext
*ctx
, GLframebuffer
*fb
)
538 GLsizei width
, height
;
540 get_window_size(fb
, &width
, &height
);
541 if (fb
->Width
!= width
|| fb
->Height
!= height
) {
542 _mesa_resize_framebuffer(ctx
, fb
, width
, height
);
546 static const GLubyte
*
547 get_string(GLcontext
*ctx
, GLenum pname
)
552 return (const GLubyte
*) "Mesa Project";
554 return (const GLubyte
*) "Software Rasterizer";
561 update_state( GLcontext
*ctx
, GLuint new_state
)
563 /* not much to do here - pass it on */
564 _swrast_InvalidateState( ctx
, new_state
);
565 _swsetup_InvalidateState( ctx
, new_state
);
566 _vbo_InvalidateState( ctx
, new_state
);
567 _tnl_InvalidateState( ctx
, new_state
);
571 viewport(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
573 GLframebuffer
*draw
= ctx
->WinSysDrawBuffer
;
574 GLframebuffer
*read
= ctx
->WinSysReadBuffer
;
576 swrast_check_and_update_window_size(ctx
, draw
);
577 swrast_check_and_update_window_size(ctx
, read
);
581 swrast_init_driver_functions(struct dd_function_table
*driver
)
583 driver
->GetString
= get_string
;
584 driver
->UpdateState
= update_state
;
585 driver
->GetBufferSize
= NULL
;
586 driver
->Viewport
= viewport
;
591 * Context-related functions.
594 static __DRIcontext
*
595 driCreateNewContext(__DRIscreen
*screen
, const __DRIconfig
*config
,
596 __DRIcontext
*shared
, void *data
)
600 struct dd_function_table functions
;
604 ctx
= _mesa_calloc(sizeof *ctx
);
608 ctx
->loaderPrivate
= data
;
610 ctx
->driScreenPriv
= screen
;
612 /* build table of device driver functions */
613 _mesa_init_driver_functions(&functions
);
614 swrast_init_driver_functions(&functions
);
616 if (!_mesa_initialize_context(&ctx
->Base
, &config
->modes
,
617 shared
? &shared
->Base
: NULL
,
618 &functions
, (void *) ctx
)) {
623 mesaCtx
= &ctx
->Base
;
625 /* do bounds checking to prevent segfaults and server crashes! */
626 mesaCtx
->Const
.CheckArrayBounds
= GL_TRUE
;
628 /* create module contexts */
629 _swrast_CreateContext( mesaCtx
);
630 _vbo_CreateContext( mesaCtx
);
631 _tnl_CreateContext( mesaCtx
);
632 _swsetup_CreateContext( mesaCtx
);
633 _swsetup_Wakeup( mesaCtx
);
635 /* use default TCL pipeline */
637 TNLcontext
*tnl
= TNL_CONTEXT(mesaCtx
);
638 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
641 _mesa_enable_sw_extensions(mesaCtx
);
642 _mesa_enable_1_3_extensions(mesaCtx
);
643 _mesa_enable_1_4_extensions(mesaCtx
);
644 _mesa_enable_1_5_extensions(mesaCtx
);
645 _mesa_enable_2_0_extensions(mesaCtx
);
646 _mesa_enable_2_1_extensions(mesaCtx
);
652 driDestroyContext(__DRIcontext
*ctx
)
658 mesaCtx
= &ctx
->Base
;
659 _swsetup_DestroyContext( mesaCtx
);
660 _swrast_DestroyContext( mesaCtx
);
661 _tnl_DestroyContext( mesaCtx
);
662 _vbo_DestroyContext( mesaCtx
);
663 _mesa_destroy_context( mesaCtx
);
668 driCopyContext(__DRIcontext
*dst
, __DRIcontext
*src
, unsigned long mask
)
672 _mesa_copy_context(&src
->Base
, &dst
->Base
, mask
);
676 static int driBindContext(__DRIcontext
*ctx
,
681 GLframebuffer
*mesaDraw
;
682 GLframebuffer
*mesaRead
;
689 mesaCtx
= &ctx
->Base
;
690 mesaDraw
= &draw
->Base
;
691 mesaRead
= &read
->Base
;
693 /* check for same context and buffer */
694 if (mesaCtx
== _mesa_get_current_context()
695 && mesaCtx
->DrawBuffer
== mesaDraw
696 && mesaCtx
->ReadBuffer
== mesaRead
) {
700 _glapi_check_multithread();
702 swrast_check_and_update_window_size(mesaCtx
, mesaDraw
);
704 swrast_check_and_update_window_size(mesaCtx
, mesaRead
);
706 _mesa_make_current( mesaCtx
,
712 _mesa_make_current( NULL
, NULL
, NULL
);
718 static int driUnbindContext(__DRIcontext
*ctx
)
726 static const __DRIcoreExtension driCoreExtension
= {
727 { __DRI_CORE
, __DRI_CORE_VERSION
},
728 NULL
, /* driCreateNewScreen */
732 driIndexConfigAttrib
,
733 NULL
, /* driCreateNewDrawable */
743 static const __DRIswrastExtension driSWRastExtension
= {
744 { __DRI_SWRAST
, __DRI_SWRAST_VERSION
},
749 /* This is the table of extensions that the loader will dlsym() for. */
750 PUBLIC
const __DRIextension
*__driDriverExtensions
[] = {
751 &driCoreExtension
.base
,
752 &driSWRastExtension
.base
,