2 * Copyright 2008, 2010 George Sapountzis <gsapountzis@gmail.com>
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/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"
43 #include "tnl/t_context.h"
44 #include "tnl/t_pipeline.h"
46 #include "drivers/common/driverfuncs.h"
47 #include "drivers/common/meta.h"
50 #include "main/teximage.h"
51 #include "main/texformat.h"
52 #include "main/texstate.h"
54 #include "swrast_priv.h"
58 * Screen and config-related functions
61 static void swrastSetTexBuffer2(__DRIcontext
*pDRICtx
, GLint target
,
62 GLint texture_format
, __DRIdrawable
*dPriv
)
64 struct dri_context
*dri_ctx
;
66 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
67 struct gl_texture_unit
*texUnit
;
68 struct gl_texture_object
*texObj
;
69 struct gl_texture_image
*texImage
;
70 uint32_t internalFormat
;
73 dri_ctx
= pDRICtx
->driverPrivate
;
75 internalFormat
= (texture_format
== __DRI_TEXTURE_FORMAT_RGB
? 3 : 4);
77 texUnit
= _mesa_get_current_tex_unit(&dri_ctx
->Base
);
78 texObj
= _mesa_select_tex_object(&dri_ctx
->Base
, texUnit
, target
);
79 texImage
= _mesa_get_tex_image(&dri_ctx
->Base
, texObj
, target
, 0);
81 _mesa_lock_texture(&dri_ctx
->Base
, texObj
);
83 sPriv
->swrast_loader
->getDrawableInfo(dPriv
, &x
, &y
, &w
, &h
, dPriv
->loaderPrivate
);
85 if (texture_format
== __DRI_TEXTURE_FORMAT_RGB
)
86 texFormat
= MESA_FORMAT_XRGB8888
;
88 texFormat
= MESA_FORMAT_ARGB8888
;
90 _mesa_init_teximage_fields(&dri_ctx
->Base
, target
, texImage
,
91 w
, h
, 1, 0, internalFormat
, texFormat
);
93 sPriv
->swrast_loader
->getImage(dPriv
, x
, y
, w
, h
, (char *)texImage
->Data
,
94 dPriv
->loaderPrivate
);
96 _mesa_unlock_texture(&dri_ctx
->Base
, texObj
);
99 static void swrastSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
,
100 __DRIdrawable
*dPriv
)
102 swrastSetTexBuffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
105 static const __DRItexBufferExtension swrastTexBufferExtension
= {
106 { __DRI_TEX_BUFFER
, __DRI_TEX_BUFFER_VERSION
},
111 static const __DRIextension
*dri_screen_extensions
[] = {
112 &swrastTexBufferExtension
.base
,
116 static __DRIconfig
**
117 swrastFillInModes(__DRIscreen
*psp
,
118 unsigned pixel_bits
, unsigned depth_bits
,
119 unsigned stencil_bits
, GLboolean have_back_buffer
)
121 __DRIconfig
**configs
;
122 unsigned depth_buffer_factor
;
123 unsigned back_buffer_factor
;
127 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
128 * support pageflipping at all.
130 static const GLenum back_buffer_modes
[] = {
131 GLX_NONE
, GLX_SWAP_UNDEFINED_OML
134 uint8_t depth_bits_array
[4];
135 uint8_t stencil_bits_array
[4];
136 uint8_t msaa_samples_array
[1];
138 depth_bits_array
[0] = 0;
139 depth_bits_array
[1] = 0;
140 depth_bits_array
[2] = depth_bits
;
141 depth_bits_array
[3] = depth_bits
;
143 /* Just like with the accumulation buffer, always provide some modes
144 * with a stencil buffer.
146 stencil_bits_array
[0] = 0;
147 stencil_bits_array
[1] = (stencil_bits
== 0) ? 8 : stencil_bits
;
148 stencil_bits_array
[2] = 0;
149 stencil_bits_array
[3] = (stencil_bits
== 0) ? 8 : stencil_bits
;
151 msaa_samples_array
[0] = 0;
153 depth_buffer_factor
= 4;
154 back_buffer_factor
= 2;
156 switch (pixel_bits
) {
159 fb_type
= GL_UNSIGNED_BYTE_2_3_3_REV
;
163 fb_type
= GL_UNSIGNED_SHORT_5_6_5
;
167 fb_type
= GL_UNSIGNED_INT_8_8_8_8_REV
;
171 fb_type
= GL_UNSIGNED_INT_8_8_8_8_REV
;
174 fprintf(stderr
, "[%s:%u] bad depth %d\n", __func__
, __LINE__
,
179 configs
= driCreateConfigs(fb_format
, fb_type
,
180 depth_bits_array
, stencil_bits_array
,
181 depth_buffer_factor
, back_buffer_modes
,
182 back_buffer_factor
, msaa_samples_array
, 1,
184 if (configs
== NULL
) {
185 fprintf(stderr
, "[%s:%u] Error creating FBConfig!\n", __func__
,
193 static const __DRIconfig
**
194 dri_init_screen(__DRIscreen
* psp
)
196 __DRIconfig
**configs8
, **configs16
, **configs24
, **configs32
;
200 psp
->extensions
= dri_screen_extensions
;
202 configs8
= swrastFillInModes(psp
, 8, 8, 0, 1);
203 configs16
= swrastFillInModes(psp
, 16, 16, 0, 1);
204 configs24
= swrastFillInModes(psp
, 24, 24, 8, 1);
205 configs32
= swrastFillInModes(psp
, 32, 24, 8, 1);
207 configs16
= driConcatConfigs(configs8
, configs16
);
208 configs24
= driConcatConfigs(configs16
, configs24
);
209 configs32
= driConcatConfigs(configs24
, configs32
);
211 return (const __DRIconfig
**)configs32
;
215 dri_destroy_screen(__DRIscreen
* sPriv
)
222 * Framebuffer and renderbuffer-related functions.
226 choose_pixel_format(const struct gl_config
*v
)
228 int depth
= v
->rgbBits
;
231 && v
->redMask
== 0xff0000
232 && v
->greenMask
== 0x00ff00
233 && v
->blueMask
== 0x0000ff)
236 && v
->redMask
== 0xff0000
237 && v
->greenMask
== 0x00ff00
238 && v
->blueMask
== 0x0000ff)
241 && v
->redMask
== 0xf800
242 && v
->greenMask
== 0x07e0
243 && v
->blueMask
== 0x001f)
246 && v
->redMask
== 0x07
247 && v
->greenMask
== 0x38
248 && v
->blueMask
== 0xc0)
251 _mesa_problem( NULL
, "unexpected format in %s", __FUNCTION__
);
256 swrast_delete_renderbuffer(struct gl_renderbuffer
*rb
)
264 /* see bytes_per_line in libGL */
266 bytes_per_line(unsigned pitch_bits
, unsigned mul
)
268 unsigned mask
= mul
- 1;
270 return ((pitch_bits
+ mask
) & ~mask
) / 8;
274 swrast_alloc_front_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
275 GLenum internalFormat
, GLuint width
, GLuint height
)
277 struct swrast_renderbuffer
*xrb
= swrast_renderbuffer(rb
);
285 xrb
->pitch
= bytes_per_line(width
* xrb
->bpp
, 32);
291 swrast_alloc_back_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
292 GLenum internalFormat
, GLuint width
, GLuint height
)
294 struct swrast_renderbuffer
*xrb
= swrast_renderbuffer(rb
);
300 swrast_alloc_front_storage(ctx
, rb
, internalFormat
, width
, height
);
302 rb
->Data
= malloc(height
* xrb
->pitch
);
307 static struct swrast_renderbuffer
*
308 swrast_new_renderbuffer(const struct gl_config
*visual
, GLboolean front
)
310 struct swrast_renderbuffer
*xrb
= calloc(1, sizeof *xrb
);
318 _mesa_init_renderbuffer(&xrb
->Base
, 0);
320 pixel_format
= choose_pixel_format(visual
);
322 xrb
->Base
.Delete
= swrast_delete_renderbuffer
;
324 xrb
->Base
.AllocStorage
= swrast_alloc_front_storage
;
325 swrast_set_span_funcs_front(xrb
, pixel_format
);
328 xrb
->Base
.AllocStorage
= swrast_alloc_back_storage
;
329 swrast_set_span_funcs_back(xrb
, pixel_format
);
332 switch (pixel_format
) {
334 xrb
->Base
.Format
= MESA_FORMAT_ARGB8888
;
335 xrb
->Base
.InternalFormat
= GL_RGBA
;
336 xrb
->Base
._BaseFormat
= GL_RGBA
;
337 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
341 xrb
->Base
.Format
= MESA_FORMAT_ARGB8888
; /* XXX */
342 xrb
->Base
.InternalFormat
= GL_RGB
;
343 xrb
->Base
._BaseFormat
= GL_RGB
;
344 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
348 xrb
->Base
.Format
= MESA_FORMAT_RGB565
;
349 xrb
->Base
.InternalFormat
= GL_RGB
;
350 xrb
->Base
._BaseFormat
= GL_RGB
;
351 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
355 xrb
->Base
.Format
= MESA_FORMAT_RGB332
;
356 xrb
->Base
.InternalFormat
= GL_RGB
;
357 xrb
->Base
._BaseFormat
= GL_RGB
;
358 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
369 dri_create_buffer(__DRIscreen
* sPriv
,
370 __DRIdrawable
* dPriv
,
371 const struct gl_config
* visual
, GLboolean isPixmap
)
373 struct dri_drawable
*drawable
= NULL
;
374 struct gl_framebuffer
*fb
;
375 struct swrast_renderbuffer
*frontrb
, *backrb
;
379 drawable
= CALLOC_STRUCT(dri_drawable
);
380 if (drawable
== NULL
)
383 dPriv
->driverPrivate
= drawable
;
384 drawable
->dPriv
= dPriv
;
386 drawable
->row
= malloc(MAX_WIDTH
* 4);
387 if (drawable
->row
== NULL
)
390 fb
= &drawable
->Base
;
392 /* basic framebuffer setup */
393 _mesa_initialize_window_framebuffer(fb
, visual
);
395 /* add front renderbuffer */
396 frontrb
= swrast_new_renderbuffer(visual
, GL_TRUE
);
397 _mesa_add_renderbuffer(fb
, BUFFER_FRONT_LEFT
, &frontrb
->Base
);
399 /* add back renderbuffer */
400 if (visual
->doubleBufferMode
) {
401 backrb
= swrast_new_renderbuffer(visual
, GL_FALSE
);
402 _mesa_add_renderbuffer(fb
, BUFFER_BACK_LEFT
, &backrb
->Base
);
405 /* add software renderbuffers */
406 _mesa_add_soft_renderbuffers(fb
,
407 GL_FALSE
, /* color */
408 visual
->haveDepthBuffer
,
409 visual
->haveStencilBuffer
,
410 visual
->haveAccumBuffer
,
411 GL_FALSE
, /* alpha */
412 GL_FALSE
/* aux bufs */);
427 dri_destroy_buffer(__DRIdrawable
* dPriv
)
432 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
433 struct gl_framebuffer
*fb
;
437 fb
= &drawable
->Base
;
439 fb
->DeletePending
= GL_TRUE
;
440 _mesa_reference_framebuffer(&fb
, NULL
);
445 dri_swap_buffers(__DRIdrawable
* dPriv
)
447 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
449 GET_CURRENT_CONTEXT(ctx
);
451 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
452 struct gl_framebuffer
*fb
;
453 struct swrast_renderbuffer
*frontrb
, *backrb
;
457 fb
= &drawable
->Base
;
460 swrast_renderbuffer(fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
462 swrast_renderbuffer(fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
);
464 /* check for signle-buffered */
468 /* check if swapping currently bound buffer */
469 if (ctx
&& ctx
->DrawBuffer
== fb
) {
470 /* flush pending rendering */
471 _mesa_notifySwapBuffers(ctx
);
474 sPriv
->swrast_loader
->putImage(dPriv
, __DRI_SWRAST_IMAGE_OP_SWAP
,
477 frontrb
->Base
.Height
,
479 dPriv
->loaderPrivate
);
484 * General device driver functions.
488 get_window_size( struct gl_framebuffer
*fb
, GLsizei
*w
, GLsizei
*h
)
490 __DRIdrawable
*dPriv
= swrast_drawable(fb
)->dPriv
;
491 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
494 sPriv
->swrast_loader
->getDrawableInfo(dPriv
,
496 dPriv
->loaderPrivate
);
500 swrast_check_and_update_window_size( struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
502 GLsizei width
, height
;
504 get_window_size(fb
, &width
, &height
);
505 if (fb
->Width
!= width
|| fb
->Height
!= height
) {
506 _mesa_resize_framebuffer(ctx
, fb
, width
, height
);
510 static const GLubyte
*
511 get_string(struct gl_context
*ctx
, GLenum pname
)
516 return (const GLubyte
*) "Mesa Project";
518 return (const GLubyte
*) "Software Rasterizer";
525 update_state( struct gl_context
*ctx
, GLuint new_state
)
527 /* not much to do here - pass it on */
528 _swrast_InvalidateState( ctx
, new_state
);
529 _swsetup_InvalidateState( ctx
, new_state
);
530 _vbo_InvalidateState( ctx
, new_state
);
531 _tnl_InvalidateState( ctx
, new_state
);
535 viewport(struct gl_context
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
537 struct gl_framebuffer
*draw
= ctx
->WinSysDrawBuffer
;
538 struct gl_framebuffer
*read
= ctx
->WinSysReadBuffer
;
540 swrast_check_and_update_window_size(ctx
, draw
);
541 swrast_check_and_update_window_size(ctx
, read
);
544 static gl_format
swrastChooseTextureFormat(struct gl_context
* ctx
,
545 GLint internalFormat
,
549 if (internalFormat
== GL_RGB
)
550 return MESA_FORMAT_XRGB8888
;
551 return _mesa_choose_tex_format(ctx
, internalFormat
, format
, type
);
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
;
561 driver
->ChooseTextureFormat
= swrastChooseTextureFormat
;
564 static const char *es2_extensions
[] = {
565 /* Used by mesa internally (cf all_mesa_extensions in ../common/utils.c) */
566 "GL_ARB_draw_buffers",
567 "GL_ARB_multisample",
568 "GL_ARB_texture_compression",
569 "GL_ARB_transpose_matrix",
570 "GL_ARB_vertex_buffer_object",
572 "GL_EXT_blend_func_separate",
573 "GL_EXT_compiled_vertex_array",
574 "GL_EXT_framebuffer_blit",
575 "GL_EXT_multi_draw_arrays",
576 "GL_EXT_polygon_offset",
577 "GL_EXT_texture_object",
578 "GL_EXT_vertex_array",
579 "GL_IBM_multimode_draw_arrays",
580 "GL_MESA_window_pos",
581 "GL_NV_vertex_program",
583 /* Required by GLES2 */
584 "GL_ARB_fragment_program",
585 "GL_ARB_fragment_shader",
586 "GL_ARB_multitexture",
587 "GL_ARB_shader_objects",
588 "GL_ARB_texture_cube_map",
589 "GL_ARB_texture_mirrored_repeat",
590 "GL_ARB_texture_non_power_of_two",
591 "GL_ARB_vertex_shader",
592 "GL_EXT_blend_color",
593 "GL_EXT_blend_equation_separate",
594 "GL_EXT_blend_minmax",
595 "GL_EXT_blend_subtract",
596 "GL_EXT_stencil_wrap",
599 "GL_ARB_framebuffer_object",
600 "GL_EXT_texture_filter_anisotropic",
601 "GL_ARB_depth_texture",
602 "GL_EXT_packed_depth_stencil",
603 "GL_EXT_framebuffer_object",
608 InitExtensionsES2(struct gl_context
*ctx
)
612 /* Can't use driInitExtensions() since it uses extensions from
613 * main/remap_helper.h when called the first time. */
615 for (i
= 0; es2_extensions
[i
]; i
++)
616 _mesa_enable_extension(ctx
, es2_extensions
[i
]);
620 * Context-related functions.
624 dri_create_context(gl_api api
,
625 const struct gl_config
* visual
,
626 __DRIcontext
* cPriv
, void *sharedContextPrivate
)
628 struct dri_context
*ctx
= NULL
;
629 struct dri_context
*share
= (struct dri_context
*)sharedContextPrivate
;
630 struct gl_context
*mesaCtx
= NULL
;
631 struct gl_context
*sharedCtx
= NULL
;
632 struct dd_function_table functions
;
636 ctx
= CALLOC_STRUCT(dri_context
);
640 cPriv
->driverPrivate
= ctx
;
643 /* build table of device driver functions */
644 _mesa_init_driver_functions(&functions
);
645 swrast_init_driver_functions(&functions
);
648 sharedCtx
= &share
->Base
;
651 mesaCtx
= &ctx
->Base
;
653 /* basic context setup */
654 if (!_mesa_initialize_context(mesaCtx
, api
, visual
, sharedCtx
, &functions
, (void *) cPriv
)) {
658 /* do bounds checking to prevent segfaults and server crashes! */
659 mesaCtx
->Const
.CheckArrayBounds
= GL_TRUE
;
661 /* create module contexts */
662 _swrast_CreateContext( mesaCtx
);
663 _vbo_CreateContext( mesaCtx
);
664 _tnl_CreateContext( mesaCtx
);
665 _swsetup_CreateContext( mesaCtx
);
666 _swsetup_Wakeup( mesaCtx
);
668 /* use default TCL pipeline */
670 TNLcontext
*tnl
= TNL_CONTEXT(mesaCtx
);
671 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
674 _mesa_meta_init(mesaCtx
);
675 _mesa_enable_sw_extensions(mesaCtx
);
679 _mesa_enable_1_3_extensions(mesaCtx
);
680 _mesa_enable_1_4_extensions(mesaCtx
);
681 _mesa_enable_1_5_extensions(mesaCtx
);
682 _mesa_enable_2_0_extensions(mesaCtx
);
683 _mesa_enable_2_1_extensions(mesaCtx
);
685 driInitExtensions( mesaCtx
, NULL
, GL_FALSE
);
688 _mesa_enable_1_3_extensions(mesaCtx
);
689 _mesa_enable_1_4_extensions(mesaCtx
);
690 _mesa_enable_1_5_extensions(mesaCtx
);
694 InitExtensionsES2( mesaCtx
);
708 dri_destroy_context(__DRIcontext
* cPriv
)
713 struct dri_context
*ctx
= dri_context(cPriv
);
714 struct gl_context
*mesaCtx
;
716 mesaCtx
= &ctx
->Base
;
718 _mesa_meta_free(mesaCtx
);
719 _swsetup_DestroyContext( mesaCtx
);
720 _swrast_DestroyContext( mesaCtx
);
721 _tnl_DestroyContext( mesaCtx
);
722 _vbo_DestroyContext( mesaCtx
);
723 _mesa_destroy_context( mesaCtx
);
728 dri_make_current(__DRIcontext
* cPriv
,
729 __DRIdrawable
* driDrawPriv
,
730 __DRIdrawable
* driReadPriv
)
732 struct gl_context
*mesaCtx
;
733 struct gl_framebuffer
*mesaDraw
;
734 struct gl_framebuffer
*mesaRead
;
738 struct dri_context
*ctx
= dri_context(cPriv
);
739 struct dri_drawable
*draw
;
740 struct dri_drawable
*read
;
742 if (!driDrawPriv
|| !driReadPriv
)
745 draw
= dri_drawable(driDrawPriv
);
746 read
= dri_drawable(driReadPriv
);
747 mesaCtx
= &ctx
->Base
;
748 mesaDraw
= &draw
->Base
;
749 mesaRead
= &read
->Base
;
751 /* check for same context and buffer */
752 if (mesaCtx
== _mesa_get_current_context()
753 && mesaCtx
->DrawBuffer
== mesaDraw
754 && mesaCtx
->ReadBuffer
== mesaRead
) {
758 _glapi_check_multithread();
760 swrast_check_and_update_window_size(mesaCtx
, mesaDraw
);
761 if (mesaRead
!= mesaDraw
)
762 swrast_check_and_update_window_size(mesaCtx
, mesaRead
);
764 _mesa_make_current( mesaCtx
,
770 _mesa_make_current( NULL
, NULL
, NULL
);
777 dri_unbind_context(__DRIcontext
* cPriv
)
782 /* Unset current context and dispath table */
783 _mesa_make_current(NULL
, NULL
, NULL
);
789 const struct __DriverAPIRec driDriverAPI
= {
790 .InitScreen
= dri_init_screen
,
791 .DestroyScreen
= dri_destroy_screen
,
792 .CreateContext
= dri_create_context
,
793 .DestroyContext
= dri_destroy_context
,
794 .CreateBuffer
= dri_create_buffer
,
795 .DestroyBuffer
= dri_destroy_buffer
,
796 .SwapBuffers
= dri_swap_buffers
,
797 .MakeCurrent
= dri_make_current
,
798 .UnbindContext
= dri_unbind_context
,
801 /* This is the table of extensions that the loader will dlsym() for. */
802 PUBLIC
const __DRIextension
*__driDriverExtensions
[] = {
803 &driCoreExtension
.base
,
804 &driSWRastExtension
.base
,