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];
139 (void) have_back_buffer
;
141 depth_bits_array
[0] = 0;
142 depth_bits_array
[1] = 0;
143 depth_bits_array
[2] = depth_bits
;
144 depth_bits_array
[3] = depth_bits
;
146 /* Just like with the accumulation buffer, always provide some modes
147 * with a stencil buffer.
149 stencil_bits_array
[0] = 0;
150 stencil_bits_array
[1] = (stencil_bits
== 0) ? 8 : stencil_bits
;
151 stencil_bits_array
[2] = 0;
152 stencil_bits_array
[3] = (stencil_bits
== 0) ? 8 : stencil_bits
;
154 msaa_samples_array
[0] = 0;
156 depth_buffer_factor
= 4;
157 back_buffer_factor
= 2;
159 switch (pixel_bits
) {
162 fb_type
= GL_UNSIGNED_BYTE_2_3_3_REV
;
166 fb_type
= GL_UNSIGNED_SHORT_5_6_5
;
170 fb_type
= GL_UNSIGNED_INT_8_8_8_8_REV
;
174 fb_type
= GL_UNSIGNED_INT_8_8_8_8_REV
;
177 fprintf(stderr
, "[%s:%u] bad depth %d\n", __func__
, __LINE__
,
182 configs
= driCreateConfigs(fb_format
, fb_type
,
183 depth_bits_array
, stencil_bits_array
,
184 depth_buffer_factor
, back_buffer_modes
,
185 back_buffer_factor
, msaa_samples_array
, 1,
187 if (configs
== NULL
) {
188 fprintf(stderr
, "[%s:%u] Error creating FBConfig!\n", __func__
,
196 static const __DRIconfig
**
197 dri_init_screen(__DRIscreen
* psp
)
199 __DRIconfig
**configs8
, **configs16
, **configs24
, **configs32
;
203 psp
->extensions
= dri_screen_extensions
;
205 configs8
= swrastFillInModes(psp
, 8, 8, 0, 1);
206 configs16
= swrastFillInModes(psp
, 16, 16, 0, 1);
207 configs24
= swrastFillInModes(psp
, 24, 24, 8, 1);
208 configs32
= swrastFillInModes(psp
, 32, 24, 8, 1);
210 configs16
= driConcatConfigs(configs8
, configs16
);
211 configs24
= driConcatConfigs(configs16
, configs24
);
212 configs32
= driConcatConfigs(configs24
, configs32
);
214 return (const __DRIconfig
**)configs32
;
218 dri_destroy_screen(__DRIscreen
* sPriv
)
226 * Framebuffer and renderbuffer-related functions.
230 choose_pixel_format(const struct gl_config
*v
)
232 int depth
= v
->rgbBits
;
235 && v
->redMask
== 0xff0000
236 && v
->greenMask
== 0x00ff00
237 && v
->blueMask
== 0x0000ff)
240 && v
->redMask
== 0xff0000
241 && v
->greenMask
== 0x00ff00
242 && v
->blueMask
== 0x0000ff)
245 && v
->redMask
== 0xf800
246 && v
->greenMask
== 0x07e0
247 && v
->blueMask
== 0x001f)
250 && v
->redMask
== 0x07
251 && v
->greenMask
== 0x38
252 && v
->blueMask
== 0xc0)
255 _mesa_problem( NULL
, "unexpected format in %s", __FUNCTION__
);
260 swrast_delete_renderbuffer(struct gl_renderbuffer
*rb
)
268 /* see bytes_per_line in libGL */
270 bytes_per_line(unsigned pitch_bits
, unsigned mul
)
272 unsigned mask
= mul
- 1;
274 return ((pitch_bits
+ mask
) & ~mask
) / 8;
278 swrast_alloc_front_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
279 GLenum internalFormat
, GLuint width
, GLuint height
)
281 struct swrast_renderbuffer
*xrb
= swrast_renderbuffer(rb
);
286 (void) internalFormat
;
292 xrb
->pitch
= bytes_per_line(width
* xrb
->bpp
, 32);
298 swrast_alloc_back_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
299 GLenum internalFormat
, GLuint width
, GLuint height
)
301 struct swrast_renderbuffer
*xrb
= swrast_renderbuffer(rb
);
307 swrast_alloc_front_storage(ctx
, rb
, internalFormat
, width
, height
);
309 rb
->Data
= malloc(height
* xrb
->pitch
);
314 static struct swrast_renderbuffer
*
315 swrast_new_renderbuffer(const struct gl_config
*visual
, GLboolean front
)
317 struct swrast_renderbuffer
*xrb
= calloc(1, sizeof *xrb
);
325 _mesa_init_renderbuffer(&xrb
->Base
, 0);
327 pixel_format
= choose_pixel_format(visual
);
329 xrb
->Base
.Delete
= swrast_delete_renderbuffer
;
331 xrb
->Base
.AllocStorage
= swrast_alloc_front_storage
;
332 swrast_set_span_funcs_front(xrb
, pixel_format
);
335 xrb
->Base
.AllocStorage
= swrast_alloc_back_storage
;
336 swrast_set_span_funcs_back(xrb
, pixel_format
);
339 switch (pixel_format
) {
341 xrb
->Base
.Format
= MESA_FORMAT_ARGB8888
;
342 xrb
->Base
.InternalFormat
= GL_RGBA
;
343 xrb
->Base
._BaseFormat
= GL_RGBA
;
344 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
348 xrb
->Base
.Format
= MESA_FORMAT_ARGB8888
; /* XXX */
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_RGB565
;
356 xrb
->Base
.InternalFormat
= GL_RGB
;
357 xrb
->Base
._BaseFormat
= GL_RGB
;
358 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
362 xrb
->Base
.Format
= MESA_FORMAT_RGB332
;
363 xrb
->Base
.InternalFormat
= GL_RGB
;
364 xrb
->Base
._BaseFormat
= GL_RGB
;
365 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
376 dri_create_buffer(__DRIscreen
* sPriv
,
377 __DRIdrawable
* dPriv
,
378 const struct gl_config
* visual
, GLboolean isPixmap
)
380 struct dri_drawable
*drawable
= NULL
;
381 struct gl_framebuffer
*fb
;
382 struct swrast_renderbuffer
*frontrb
, *backrb
;
389 drawable
= CALLOC_STRUCT(dri_drawable
);
390 if (drawable
== NULL
)
393 dPriv
->driverPrivate
= drawable
;
394 drawable
->dPriv
= dPriv
;
396 drawable
->row
= malloc(MAX_WIDTH
* 4);
397 if (drawable
->row
== NULL
)
400 fb
= &drawable
->Base
;
402 /* basic framebuffer setup */
403 _mesa_initialize_window_framebuffer(fb
, visual
);
405 /* add front renderbuffer */
406 frontrb
= swrast_new_renderbuffer(visual
, GL_TRUE
);
407 _mesa_add_renderbuffer(fb
, BUFFER_FRONT_LEFT
, &frontrb
->Base
);
409 /* add back renderbuffer */
410 if (visual
->doubleBufferMode
) {
411 backrb
= swrast_new_renderbuffer(visual
, GL_FALSE
);
412 _mesa_add_renderbuffer(fb
, BUFFER_BACK_LEFT
, &backrb
->Base
);
415 /* add software renderbuffers */
416 _mesa_add_soft_renderbuffers(fb
,
417 GL_FALSE
, /* color */
418 visual
->haveDepthBuffer
,
419 visual
->haveStencilBuffer
,
420 visual
->haveAccumBuffer
,
421 GL_FALSE
, /* alpha */
422 GL_FALSE
/* aux bufs */);
437 dri_destroy_buffer(__DRIdrawable
* dPriv
)
442 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
443 struct gl_framebuffer
*fb
;
447 fb
= &drawable
->Base
;
449 fb
->DeletePending
= GL_TRUE
;
450 _mesa_reference_framebuffer(&fb
, NULL
);
455 dri_swap_buffers(__DRIdrawable
* dPriv
)
457 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
459 GET_CURRENT_CONTEXT(ctx
);
461 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
462 struct gl_framebuffer
*fb
;
463 struct swrast_renderbuffer
*frontrb
, *backrb
;
467 fb
= &drawable
->Base
;
470 swrast_renderbuffer(fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
472 swrast_renderbuffer(fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
);
474 /* check for signle-buffered */
478 /* check if swapping currently bound buffer */
479 if (ctx
&& ctx
->DrawBuffer
== fb
) {
480 /* flush pending rendering */
481 _mesa_notifySwapBuffers(ctx
);
484 sPriv
->swrast_loader
->putImage(dPriv
, __DRI_SWRAST_IMAGE_OP_SWAP
,
487 frontrb
->Base
.Height
,
489 dPriv
->loaderPrivate
);
494 * General device driver functions.
498 get_window_size( struct gl_framebuffer
*fb
, GLsizei
*w
, GLsizei
*h
)
500 __DRIdrawable
*dPriv
= swrast_drawable(fb
)->dPriv
;
501 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
504 sPriv
->swrast_loader
->getDrawableInfo(dPriv
,
506 dPriv
->loaderPrivate
);
510 swrast_check_and_update_window_size( struct gl_context
*ctx
, struct gl_framebuffer
*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(struct gl_context
*ctx
, GLenum pname
)
526 return (const GLubyte
*) "Mesa Project";
528 return (const GLubyte
*) "Software Rasterizer";
535 update_state( struct gl_context
*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(struct gl_context
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
547 struct gl_framebuffer
*draw
= ctx
->WinSysDrawBuffer
;
548 struct gl_framebuffer
*read
= ctx
->WinSysReadBuffer
;
554 swrast_check_and_update_window_size(ctx
, draw
);
555 swrast_check_and_update_window_size(ctx
, read
);
558 static gl_format
swrastChooseTextureFormat(struct gl_context
* ctx
,
559 GLint internalFormat
,
563 if (internalFormat
== GL_RGB
)
564 return MESA_FORMAT_XRGB8888
;
565 return _mesa_choose_tex_format(ctx
, internalFormat
, format
, type
);
569 swrast_init_driver_functions(struct dd_function_table
*driver
)
571 driver
->GetString
= get_string
;
572 driver
->UpdateState
= update_state
;
573 driver
->GetBufferSize
= NULL
;
574 driver
->Viewport
= viewport
;
575 driver
->ChooseTextureFormat
= swrastChooseTextureFormat
;
578 static const char *es2_extensions
[] = {
579 /* Used by mesa internally (cf all_mesa_extensions in ../common/utils.c) */
580 "GL_ARB_draw_buffers",
581 "GL_ARB_transpose_matrix",
583 "GL_EXT_blend_func_separate",
584 "GL_EXT_compiled_vertex_array",
585 "GL_EXT_framebuffer_blit",
586 "GL_IBM_multimode_draw_arrays",
587 "GL_MESA_window_pos",
588 "GL_NV_vertex_program",
590 /* Required by GLES2 */
591 "GL_ARB_fragment_program",
592 "GL_ARB_fragment_shader",
593 "GL_ARB_shader_objects",
594 "GL_ARB_texture_cube_map",
595 "GL_ARB_texture_mirrored_repeat",
596 "GL_ARB_texture_non_power_of_two",
597 "GL_ARB_vertex_shader",
598 "GL_EXT_blend_color",
599 "GL_EXT_blend_equation_separate",
600 "GL_EXT_blend_minmax",
603 "GL_ARB_framebuffer_object",
604 "GL_EXT_texture_filter_anisotropic",
605 "GL_ARB_depth_texture",
606 "GL_EXT_packed_depth_stencil",
607 "GL_EXT_framebuffer_object",
612 InitExtensionsES2(struct gl_context
*ctx
)
616 for (i
= 0; es2_extensions
[i
]; i
++)
617 _mesa_enable_extension(ctx
, es2_extensions
[i
]);
621 * Context-related functions.
625 dri_create_context(gl_api api
,
626 const struct gl_config
* visual
,
627 __DRIcontext
* cPriv
, void *sharedContextPrivate
)
629 struct dri_context
*ctx
= NULL
;
630 struct dri_context
*share
= (struct dri_context
*)sharedContextPrivate
;
631 struct gl_context
*mesaCtx
= NULL
;
632 struct gl_context
*sharedCtx
= NULL
;
633 struct dd_function_table functions
;
637 ctx
= CALLOC_STRUCT(dri_context
);
641 cPriv
->driverPrivate
= ctx
;
644 /* build table of device driver functions */
645 _mesa_init_driver_functions(&functions
);
646 swrast_init_driver_functions(&functions
);
649 sharedCtx
= &share
->Base
;
652 mesaCtx
= &ctx
->Base
;
654 /* basic context setup */
655 if (!_mesa_initialize_context(mesaCtx
, api
, visual
, sharedCtx
, &functions
, (void *) cPriv
)) {
659 /* do bounds checking to prevent segfaults and server crashes! */
660 mesaCtx
->Const
.CheckArrayBounds
= GL_TRUE
;
662 /* create module contexts */
663 _swrast_CreateContext( mesaCtx
);
664 _vbo_CreateContext( mesaCtx
);
665 _tnl_CreateContext( mesaCtx
);
666 _swsetup_CreateContext( mesaCtx
);
667 _swsetup_Wakeup( mesaCtx
);
669 /* use default TCL pipeline */
671 TNLcontext
*tnl
= TNL_CONTEXT(mesaCtx
);
672 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
675 _mesa_meta_init(mesaCtx
);
676 _mesa_enable_sw_extensions(mesaCtx
);
680 _mesa_enable_1_3_extensions(mesaCtx
);
681 _mesa_enable_1_4_extensions(mesaCtx
);
682 _mesa_enable_1_5_extensions(mesaCtx
);
683 _mesa_enable_2_0_extensions(mesaCtx
);
684 _mesa_enable_2_1_extensions(mesaCtx
);
687 _mesa_enable_1_3_extensions(mesaCtx
);
688 _mesa_enable_1_4_extensions(mesaCtx
);
689 _mesa_enable_1_5_extensions(mesaCtx
);
693 InitExtensionsES2( mesaCtx
);
707 dri_destroy_context(__DRIcontext
* cPriv
)
712 struct dri_context
*ctx
= dri_context(cPriv
);
713 struct gl_context
*mesaCtx
;
715 mesaCtx
= &ctx
->Base
;
717 _mesa_meta_free(mesaCtx
);
718 _swsetup_DestroyContext( mesaCtx
);
719 _swrast_DestroyContext( mesaCtx
);
720 _tnl_DestroyContext( mesaCtx
);
721 _vbo_DestroyContext( mesaCtx
);
722 _mesa_destroy_context( mesaCtx
);
727 dri_make_current(__DRIcontext
* cPriv
,
728 __DRIdrawable
* driDrawPriv
,
729 __DRIdrawable
* driReadPriv
)
731 struct gl_context
*mesaCtx
;
732 struct gl_framebuffer
*mesaDraw
;
733 struct gl_framebuffer
*mesaRead
;
737 struct dri_context
*ctx
= dri_context(cPriv
);
738 struct dri_drawable
*draw
;
739 struct dri_drawable
*read
;
741 if (!driDrawPriv
|| !driReadPriv
)
744 draw
= dri_drawable(driDrawPriv
);
745 read
= dri_drawable(driReadPriv
);
746 mesaCtx
= &ctx
->Base
;
747 mesaDraw
= &draw
->Base
;
748 mesaRead
= &read
->Base
;
750 /* check for same context and buffer */
751 if (mesaCtx
== _mesa_get_current_context()
752 && mesaCtx
->DrawBuffer
== mesaDraw
753 && mesaCtx
->ReadBuffer
== mesaRead
) {
757 _glapi_check_multithread();
759 swrast_check_and_update_window_size(mesaCtx
, mesaDraw
);
760 if (mesaRead
!= mesaDraw
)
761 swrast_check_and_update_window_size(mesaCtx
, mesaRead
);
763 _mesa_make_current( mesaCtx
,
769 _mesa_make_current( NULL
, NULL
, NULL
);
776 dri_unbind_context(__DRIcontext
* cPriv
)
781 /* Unset current context and dispath table */
782 _mesa_make_current(NULL
, NULL
, NULL
);
788 const struct __DriverAPIRec driDriverAPI
= {
789 .InitScreen
= dri_init_screen
,
790 .DestroyScreen
= dri_destroy_screen
,
791 .CreateContext
= dri_create_context
,
792 .DestroyContext
= dri_destroy_context
,
793 .CreateBuffer
= dri_create_buffer
,
794 .DestroyBuffer
= dri_destroy_buffer
,
795 .SwapBuffers
= dri_swap_buffers
,
796 .MakeCurrent
= dri_make_current
,
797 .UnbindContext
= dri_unbind_context
,
800 /* This is the table of extensions that the loader will dlsym() for. */
801 PUBLIC
const __DRIextension
*__driDriverExtensions
[] = {
802 &driCoreExtension
.base
,
803 &driSWRastExtension
.base
,