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/texfetch.h"
52 #include "main/texformat.h"
53 #include "main/texstate.h"
55 #include "swrast_priv.h"
59 * Screen and config-related functions
62 static void swrastSetTexBuffer2(__DRIcontext
*pDRICtx
, GLint target
,
63 GLint texture_format
, __DRIdrawable
*dPriv
)
65 struct dri_context
*dri_ctx
;
67 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
68 struct gl_texture_unit
*texUnit
;
69 struct gl_texture_object
*texObj
;
70 struct gl_texture_image
*texImage
;
71 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 _mesa_init_teximage_fields(&dri_ctx
->Base
, target
, texImage
,
86 w
, h
, 1, 0, internalFormat
);
88 if (texture_format
== __DRI_TEXTURE_FORMAT_RGB
)
89 texImage
->TexFormat
= MESA_FORMAT_XRGB8888
;
91 texImage
->TexFormat
= MESA_FORMAT_ARGB8888
;
93 _mesa_set_fetch_functions(texImage
, 2);
95 sPriv
->swrast_loader
->getImage(dPriv
, x
, y
, w
, h
, (char *)texImage
->Data
,
96 dPriv
->loaderPrivate
);
98 _mesa_unlock_texture(&dri_ctx
->Base
, texObj
);
101 static void swrastSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
,
102 __DRIdrawable
*dPriv
)
104 swrastSetTexBuffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
107 static const __DRItexBufferExtension swrastTexBufferExtension
= {
108 { __DRI_TEX_BUFFER
, __DRI_TEX_BUFFER_VERSION
},
113 static const __DRIextension
*dri_screen_extensions
[] = {
114 &swrastTexBufferExtension
.base
,
118 static __DRIconfig
**
119 swrastFillInModes(__DRIscreen
*psp
,
120 unsigned pixel_bits
, unsigned depth_bits
,
121 unsigned stencil_bits
, GLboolean have_back_buffer
)
123 __DRIconfig
**configs
;
124 unsigned depth_buffer_factor
;
125 unsigned back_buffer_factor
;
129 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
130 * support pageflipping at all.
132 static const GLenum back_buffer_modes
[] = {
133 GLX_NONE
, GLX_SWAP_UNDEFINED_OML
136 uint8_t depth_bits_array
[4];
137 uint8_t stencil_bits_array
[4];
138 uint8_t msaa_samples_array
[1];
140 depth_bits_array
[0] = 0;
141 depth_bits_array
[1] = 0;
142 depth_bits_array
[2] = depth_bits
;
143 depth_bits_array
[3] = depth_bits
;
145 /* Just like with the accumulation buffer, always provide some modes
146 * with a stencil buffer.
148 stencil_bits_array
[0] = 0;
149 stencil_bits_array
[1] = (stencil_bits
== 0) ? 8 : stencil_bits
;
150 stencil_bits_array
[2] = 0;
151 stencil_bits_array
[3] = (stencil_bits
== 0) ? 8 : stencil_bits
;
153 msaa_samples_array
[0] = 0;
155 depth_buffer_factor
= 4;
156 back_buffer_factor
= 2;
158 switch (pixel_bits
) {
161 fb_type
= GL_UNSIGNED_BYTE_2_3_3_REV
;
165 fb_type
= GL_UNSIGNED_SHORT_5_6_5
;
169 fb_type
= GL_UNSIGNED_INT_8_8_8_8_REV
;
173 fb_type
= GL_UNSIGNED_INT_8_8_8_8_REV
;
176 fprintf(stderr
, "[%s:%u] bad depth %d\n", __func__
, __LINE__
,
181 configs
= driCreateConfigs(fb_format
, fb_type
,
182 depth_bits_array
, stencil_bits_array
,
183 depth_buffer_factor
, back_buffer_modes
,
184 back_buffer_factor
, msaa_samples_array
, 1,
186 if (configs
== NULL
) {
187 fprintf(stderr
, "[%s:%u] Error creating FBConfig!\n", __func__
,
195 static const __DRIconfig
**
196 dri_init_screen(__DRIscreen
* psp
)
198 __DRIconfig
**configs8
, **configs16
, **configs24
, **configs32
;
202 psp
->extensions
= dri_screen_extensions
;
204 configs8
= swrastFillInModes(psp
, 8, 8, 0, 1);
205 configs16
= swrastFillInModes(psp
, 16, 16, 0, 1);
206 configs24
= swrastFillInModes(psp
, 24, 24, 8, 1);
207 configs32
= swrastFillInModes(psp
, 32, 24, 8, 1);
209 configs16
= driConcatConfigs(configs8
, configs16
);
210 configs24
= driConcatConfigs(configs16
, configs24
);
211 configs32
= driConcatConfigs(configs24
, configs32
);
213 return (const __DRIconfig
**)configs32
;
217 dri_destroy_screen(__DRIscreen
* sPriv
)
224 * Framebuffer and renderbuffer-related functions.
228 choose_pixel_format(const struct gl_config
*v
)
230 int depth
= v
->rgbBits
;
233 && v
->redMask
== 0xff0000
234 && v
->greenMask
== 0x00ff00
235 && v
->blueMask
== 0x0000ff)
238 && v
->redMask
== 0xff0000
239 && v
->greenMask
== 0x00ff00
240 && v
->blueMask
== 0x0000ff)
243 && v
->redMask
== 0xf800
244 && v
->greenMask
== 0x07e0
245 && v
->blueMask
== 0x001f)
248 && v
->redMask
== 0x07
249 && v
->greenMask
== 0x38
250 && v
->blueMask
== 0xc0)
253 _mesa_problem( NULL
, "unexpected format in %s", __FUNCTION__
);
258 swrast_delete_renderbuffer(struct gl_renderbuffer
*rb
)
266 /* see bytes_per_line in libGL */
268 bytes_per_line(unsigned pitch_bits
, unsigned mul
)
270 unsigned mask
= mul
- 1;
272 return ((pitch_bits
+ mask
) & ~mask
) / 8;
276 swrast_alloc_front_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
277 GLenum internalFormat
, GLuint width
, GLuint height
)
279 struct swrast_renderbuffer
*xrb
= swrast_renderbuffer(rb
);
287 xrb
->pitch
= bytes_per_line(width
* xrb
->bpp
, 32);
293 swrast_alloc_back_storage(struct gl_context
*ctx
, struct gl_renderbuffer
*rb
,
294 GLenum internalFormat
, GLuint width
, GLuint height
)
296 struct swrast_renderbuffer
*xrb
= swrast_renderbuffer(rb
);
302 swrast_alloc_front_storage(ctx
, rb
, internalFormat
, width
, height
);
304 rb
->Data
= malloc(height
* xrb
->pitch
);
309 static struct swrast_renderbuffer
*
310 swrast_new_renderbuffer(const struct gl_config
*visual
, GLboolean front
)
312 struct swrast_renderbuffer
*xrb
= calloc(1, sizeof *xrb
);
320 _mesa_init_renderbuffer(&xrb
->Base
, 0);
322 pixel_format
= choose_pixel_format(visual
);
324 xrb
->Base
.Delete
= swrast_delete_renderbuffer
;
326 xrb
->Base
.AllocStorage
= swrast_alloc_front_storage
;
327 swrast_set_span_funcs_front(xrb
, pixel_format
);
330 xrb
->Base
.AllocStorage
= swrast_alloc_back_storage
;
331 swrast_set_span_funcs_back(xrb
, pixel_format
);
334 switch (pixel_format
) {
336 xrb
->Base
.Format
= MESA_FORMAT_ARGB8888
;
337 xrb
->Base
.InternalFormat
= GL_RGBA
;
338 xrb
->Base
._BaseFormat
= GL_RGBA
;
339 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
343 xrb
->Base
.Format
= MESA_FORMAT_ARGB8888
; /* XXX */
344 xrb
->Base
.InternalFormat
= GL_RGB
;
345 xrb
->Base
._BaseFormat
= GL_RGB
;
346 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
350 xrb
->Base
.Format
= MESA_FORMAT_RGB565
;
351 xrb
->Base
.InternalFormat
= GL_RGB
;
352 xrb
->Base
._BaseFormat
= GL_RGB
;
353 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
357 xrb
->Base
.Format
= MESA_FORMAT_RGB332
;
358 xrb
->Base
.InternalFormat
= GL_RGB
;
359 xrb
->Base
._BaseFormat
= GL_RGB
;
360 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
371 dri_create_buffer(__DRIscreen
* sPriv
,
372 __DRIdrawable
* dPriv
,
373 const struct gl_config
* visual
, GLboolean isPixmap
)
375 struct dri_drawable
*drawable
= NULL
;
376 struct gl_framebuffer
*fb
;
377 struct swrast_renderbuffer
*frontrb
, *backrb
;
381 drawable
= CALLOC_STRUCT(dri_drawable
);
382 if (drawable
== NULL
)
385 dPriv
->driverPrivate
= drawable
;
386 drawable
->dPriv
= dPriv
;
388 drawable
->row
= malloc(MAX_WIDTH
* 4);
389 if (drawable
->row
== NULL
)
392 fb
= &drawable
->Base
;
394 /* basic framebuffer setup */
395 _mesa_initialize_window_framebuffer(fb
, visual
);
397 /* add front renderbuffer */
398 frontrb
= swrast_new_renderbuffer(visual
, GL_TRUE
);
399 _mesa_add_renderbuffer(fb
, BUFFER_FRONT_LEFT
, &frontrb
->Base
);
401 /* add back renderbuffer */
402 if (visual
->doubleBufferMode
) {
403 backrb
= swrast_new_renderbuffer(visual
, GL_FALSE
);
404 _mesa_add_renderbuffer(fb
, BUFFER_BACK_LEFT
, &backrb
->Base
);
407 /* add software renderbuffers */
408 _mesa_add_soft_renderbuffers(fb
,
409 GL_FALSE
, /* color */
410 visual
->haveDepthBuffer
,
411 visual
->haveStencilBuffer
,
412 visual
->haveAccumBuffer
,
413 GL_FALSE
, /* alpha */
414 GL_FALSE
/* aux bufs */);
429 dri_destroy_buffer(__DRIdrawable
* dPriv
)
434 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
435 struct gl_framebuffer
*fb
;
439 fb
= &drawable
->Base
;
441 fb
->DeletePending
= GL_TRUE
;
442 _mesa_reference_framebuffer(&fb
, NULL
);
447 dri_swap_buffers(__DRIdrawable
* dPriv
)
449 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
451 GET_CURRENT_CONTEXT(ctx
);
453 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
454 struct gl_framebuffer
*fb
;
455 struct swrast_renderbuffer
*frontrb
, *backrb
;
459 fb
= &drawable
->Base
;
462 swrast_renderbuffer(fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
464 swrast_renderbuffer(fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
);
466 /* check for signle-buffered */
470 /* check if swapping currently bound buffer */
471 if (ctx
&& ctx
->DrawBuffer
== fb
) {
472 /* flush pending rendering */
473 _mesa_notifySwapBuffers(ctx
);
476 sPriv
->swrast_loader
->putImage(dPriv
, __DRI_SWRAST_IMAGE_OP_SWAP
,
479 frontrb
->Base
.Height
,
481 dPriv
->loaderPrivate
);
486 * General device driver functions.
490 get_window_size( struct gl_framebuffer
*fb
, GLsizei
*w
, GLsizei
*h
)
492 __DRIdrawable
*dPriv
= swrast_drawable(fb
)->dPriv
;
493 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
496 sPriv
->swrast_loader
->getDrawableInfo(dPriv
,
498 dPriv
->loaderPrivate
);
502 swrast_check_and_update_window_size( struct gl_context
*ctx
, struct gl_framebuffer
*fb
)
504 GLsizei width
, height
;
506 get_window_size(fb
, &width
, &height
);
507 if (fb
->Width
!= width
|| fb
->Height
!= height
) {
508 _mesa_resize_framebuffer(ctx
, fb
, width
, height
);
512 static const GLubyte
*
513 get_string(struct gl_context
*ctx
, GLenum pname
)
518 return (const GLubyte
*) "Mesa Project";
520 return (const GLubyte
*) "Software Rasterizer";
527 update_state( struct gl_context
*ctx
, GLuint new_state
)
529 /* not much to do here - pass it on */
530 _swrast_InvalidateState( ctx
, new_state
);
531 _swsetup_InvalidateState( ctx
, new_state
);
532 _vbo_InvalidateState( ctx
, new_state
);
533 _tnl_InvalidateState( ctx
, new_state
);
537 viewport(struct gl_context
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
539 struct gl_framebuffer
*draw
= ctx
->WinSysDrawBuffer
;
540 struct gl_framebuffer
*read
= ctx
->WinSysReadBuffer
;
542 swrast_check_and_update_window_size(ctx
, draw
);
543 swrast_check_and_update_window_size(ctx
, read
);
546 static gl_format
swrastChooseTextureFormat(struct gl_context
* ctx
,
547 GLint internalFormat
,
551 if (internalFormat
== GL_RGB
)
552 return MESA_FORMAT_XRGB8888
;
553 return _mesa_choose_tex_format(ctx
, internalFormat
, format
, type
);
557 swrast_init_driver_functions(struct dd_function_table
*driver
)
559 driver
->GetString
= get_string
;
560 driver
->UpdateState
= update_state
;
561 driver
->GetBufferSize
= NULL
;
562 driver
->Viewport
= viewport
;
563 driver
->ChooseTextureFormat
= swrastChooseTextureFormat
;
568 * Context-related functions.
572 dri_create_context(gl_api api
,
573 const struct gl_config
* visual
,
574 __DRIcontext
* cPriv
, void *sharedContextPrivate
)
576 struct dri_context
*ctx
= NULL
;
577 struct dri_context
*share
= (struct dri_context
*)sharedContextPrivate
;
578 struct gl_context
*mesaCtx
= NULL
;
579 struct gl_context
*sharedCtx
= NULL
;
580 struct dd_function_table functions
;
584 ctx
= CALLOC_STRUCT(dri_context
);
588 cPriv
->driverPrivate
= ctx
;
591 /* build table of device driver functions */
592 _mesa_init_driver_functions(&functions
);
593 swrast_init_driver_functions(&functions
);
596 sharedCtx
= &share
->Base
;
599 mesaCtx
= &ctx
->Base
;
601 /* basic context setup */
602 if (!_mesa_initialize_context(mesaCtx
, visual
, sharedCtx
, &functions
, (void *) cPriv
)) {
606 /* do bounds checking to prevent segfaults and server crashes! */
607 mesaCtx
->Const
.CheckArrayBounds
= GL_TRUE
;
609 /* create module contexts */
610 _swrast_CreateContext( mesaCtx
);
611 _vbo_CreateContext( mesaCtx
);
612 _tnl_CreateContext( mesaCtx
);
613 _swsetup_CreateContext( mesaCtx
);
614 _swsetup_Wakeup( mesaCtx
);
616 /* use default TCL pipeline */
618 TNLcontext
*tnl
= TNL_CONTEXT(mesaCtx
);
619 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
622 _mesa_enable_sw_extensions(mesaCtx
);
623 _mesa_enable_1_3_extensions(mesaCtx
);
624 _mesa_enable_1_4_extensions(mesaCtx
);
625 _mesa_enable_1_5_extensions(mesaCtx
);
626 _mesa_enable_2_0_extensions(mesaCtx
);
627 _mesa_enable_2_1_extensions(mesaCtx
);
629 _mesa_meta_init(mesaCtx
);
631 driInitExtensions( mesaCtx
, NULL
, GL_FALSE
);
643 dri_destroy_context(__DRIcontext
* cPriv
)
648 struct dri_context
*ctx
= dri_context(cPriv
);
649 struct gl_context
*mesaCtx
;
651 mesaCtx
= &ctx
->Base
;
653 _mesa_meta_free(mesaCtx
);
654 _swsetup_DestroyContext( mesaCtx
);
655 _swrast_DestroyContext( mesaCtx
);
656 _tnl_DestroyContext( mesaCtx
);
657 _vbo_DestroyContext( mesaCtx
);
658 _mesa_destroy_context( mesaCtx
);
663 dri_make_current(__DRIcontext
* cPriv
,
664 __DRIdrawable
* driDrawPriv
,
665 __DRIdrawable
* driReadPriv
)
667 struct gl_context
*mesaCtx
;
668 struct gl_framebuffer
*mesaDraw
;
669 struct gl_framebuffer
*mesaRead
;
673 struct dri_context
*ctx
= dri_context(cPriv
);
674 struct dri_drawable
*draw
;
675 struct dri_drawable
*read
;
677 if (!driDrawPriv
|| !driReadPriv
)
680 draw
= dri_drawable(driDrawPriv
);
681 read
= dri_drawable(driReadPriv
);
682 mesaCtx
= &ctx
->Base
;
683 mesaDraw
= &draw
->Base
;
684 mesaRead
= &read
->Base
;
686 /* check for same context and buffer */
687 if (mesaCtx
== _mesa_get_current_context()
688 && mesaCtx
->DrawBuffer
== mesaDraw
689 && mesaCtx
->ReadBuffer
== mesaRead
) {
693 _glapi_check_multithread();
695 swrast_check_and_update_window_size(mesaCtx
, mesaDraw
);
696 if (mesaRead
!= mesaDraw
)
697 swrast_check_and_update_window_size(mesaCtx
, mesaRead
);
699 _mesa_make_current( mesaCtx
,
705 _mesa_make_current( NULL
, NULL
, NULL
);
712 dri_unbind_context(__DRIcontext
* cPriv
)
717 /* Unset current context and dispath table */
718 _mesa_make_current(NULL
, NULL
, NULL
);
724 const struct __DriverAPIRec driDriverAPI
= {
725 .InitScreen
= dri_init_screen
,
726 .DestroyScreen
= dri_destroy_screen
,
727 .CreateContext
= dri_create_context
,
728 .DestroyContext
= dri_destroy_context
,
729 .CreateBuffer
= dri_create_buffer
,
730 .DestroyBuffer
= dri_destroy_buffer
,
731 .SwapBuffers
= dri_swap_buffers
,
732 .MakeCurrent
= dri_make_current
,
733 .UnbindContext
= dri_unbind_context
,
736 /* This is the table of extensions that the loader will dlsym() for. */
737 PUBLIC
const __DRIextension
*__driDriverExtensions
[] = {
738 &driCoreExtension
.base
,
739 &driSWRastExtension
.base
,