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"
54 #include "main/texobj.h"
56 #include "swrast_priv.h"
60 * Screen and config-related functions
63 static void swrastSetTexBuffer2(__DRIcontext
*pDRICtx
, GLint target
,
64 GLint texture_format
, __DRIdrawable
*dPriv
)
66 struct dri_context
*dri_ctx
;
68 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
69 struct gl_texture_unit
*texUnit
;
70 struct gl_texture_object
*texObj
;
71 struct gl_texture_image
*texImage
;
72 uint32_t internalFormat
;
74 dri_ctx
= pDRICtx
->driverPrivate
;
76 internalFormat
= (texture_format
== __DRI_TEXTURE_FORMAT_RGB
? 3 : 4);
78 texUnit
= _mesa_get_current_tex_unit(&dri_ctx
->Base
);
79 texObj
= _mesa_select_tex_object(&dri_ctx
->Base
, texUnit
, target
);
80 texImage
= _mesa_get_tex_image(&dri_ctx
->Base
, texObj
, target
, 0);
82 _mesa_lock_texture(&dri_ctx
->Base
, texObj
);
84 sPriv
->swrast_loader
->getDrawableInfo(dPriv
, &x
, &y
, &w
, &h
, dPriv
->loaderPrivate
);
86 _mesa_init_teximage_fields(&dri_ctx
->Base
, target
, texImage
,
87 w
, h
, 1, 0, internalFormat
);
89 if (texture_format
== __DRI_TEXTURE_FORMAT_RGB
)
90 texImage
->TexFormat
= MESA_FORMAT_XRGB8888
;
92 texImage
->TexFormat
= MESA_FORMAT_ARGB8888
;
94 _mesa_set_fetch_functions(texImage
, 2);
96 sPriv
->swrast_loader
->getImage(dPriv
, x
, y
, w
, h
, (char *)texImage
->Data
,
97 dPriv
->loaderPrivate
);
99 _mesa_unlock_texture(&dri_ctx
->Base
, texObj
);
102 static void swrastSetTexBuffer(__DRIcontext
*pDRICtx
, GLint target
,
103 __DRIdrawable
*dPriv
)
105 swrastSetTexBuffer2(pDRICtx
, target
, __DRI_TEXTURE_FORMAT_RGBA
, dPriv
);
108 static const __DRItexBufferExtension swrastTexBufferExtension
= {
109 { __DRI_TEX_BUFFER
, __DRI_TEX_BUFFER_VERSION
},
114 static const __DRIextension
*dri_screen_extensions
[] = {
115 &swrastTexBufferExtension
.base
,
119 static __DRIconfig
**
120 swrastFillInModes(__DRIscreen
*psp
,
121 unsigned pixel_bits
, unsigned depth_bits
,
122 unsigned stencil_bits
, GLboolean have_back_buffer
)
124 __DRIconfig
**configs
;
125 unsigned depth_buffer_factor
;
126 unsigned back_buffer_factor
;
130 /* GLX_SWAP_COPY_OML is only supported because the Intel driver doesn't
131 * support pageflipping at all.
133 static const GLenum back_buffer_modes
[] = {
134 GLX_NONE
, GLX_SWAP_UNDEFINED_OML
137 uint8_t depth_bits_array
[4];
138 uint8_t stencil_bits_array
[4];
139 uint8_t msaa_samples_array
[1];
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
)
225 * Framebuffer and renderbuffer-related functions.
229 choose_pixel_format(const GLvisual
*v
)
231 int depth
= v
->rgbBits
;
234 && v
->redMask
== 0xff0000
235 && v
->greenMask
== 0x00ff00
236 && v
->blueMask
== 0x0000ff)
239 && v
->redMask
== 0xff0000
240 && v
->greenMask
== 0x00ff00
241 && v
->blueMask
== 0x0000ff)
244 && v
->redMask
== 0xf800
245 && v
->greenMask
== 0x07e0
246 && v
->blueMask
== 0x001f)
249 && v
->redMask
== 0x07
250 && v
->greenMask
== 0x38
251 && v
->blueMask
== 0xc0)
254 _mesa_problem( NULL
, "unexpected format in %s", __FUNCTION__
);
259 swrast_delete_renderbuffer(struct gl_renderbuffer
*rb
)
267 /* see bytes_per_line in libGL */
269 bytes_per_line(unsigned pitch_bits
, unsigned mul
)
271 unsigned mask
= mul
- 1;
273 return ((pitch_bits
+ mask
) & ~mask
) / 8;
277 swrast_alloc_front_storage(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
278 GLenum internalFormat
, GLuint width
, GLuint height
)
280 struct swrast_renderbuffer
*xrb
= swrast_renderbuffer(rb
);
288 xrb
->pitch
= bytes_per_line(width
* xrb
->bpp
, 32);
294 swrast_alloc_back_storage(GLcontext
*ctx
, struct gl_renderbuffer
*rb
,
295 GLenum internalFormat
, GLuint width
, GLuint height
)
297 struct swrast_renderbuffer
*xrb
= swrast_renderbuffer(rb
);
303 swrast_alloc_front_storage(ctx
, rb
, internalFormat
, width
, height
);
305 rb
->Data
= malloc(height
* xrb
->pitch
);
310 static struct swrast_renderbuffer
*
311 swrast_new_renderbuffer(const GLvisual
*visual
, GLboolean front
)
313 struct swrast_renderbuffer
*xrb
= calloc(1, sizeof *xrb
);
321 _mesa_init_renderbuffer(&xrb
->Base
, 0);
323 pixel_format
= choose_pixel_format(visual
);
325 xrb
->Base
.Delete
= swrast_delete_renderbuffer
;
327 xrb
->Base
.AllocStorage
= swrast_alloc_front_storage
;
328 swrast_set_span_funcs_front(xrb
, pixel_format
);
331 xrb
->Base
.AllocStorage
= swrast_alloc_back_storage
;
332 swrast_set_span_funcs_back(xrb
, pixel_format
);
335 switch (pixel_format
) {
337 xrb
->Base
.Format
= MESA_FORMAT_ARGB8888
;
338 xrb
->Base
.InternalFormat
= GL_RGBA
;
339 xrb
->Base
._BaseFormat
= GL_RGBA
;
340 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
344 xrb
->Base
.Format
= MESA_FORMAT_ARGB8888
; /* XXX */
345 xrb
->Base
.InternalFormat
= GL_RGB
;
346 xrb
->Base
._BaseFormat
= GL_RGB
;
347 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
351 xrb
->Base
.Format
= MESA_FORMAT_RGB565
;
352 xrb
->Base
.InternalFormat
= GL_RGB
;
353 xrb
->Base
._BaseFormat
= GL_RGB
;
354 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
358 xrb
->Base
.Format
= MESA_FORMAT_RGB332
;
359 xrb
->Base
.InternalFormat
= GL_RGB
;
360 xrb
->Base
._BaseFormat
= GL_RGB
;
361 xrb
->Base
.DataType
= GL_UNSIGNED_BYTE
;
372 dri_create_buffer(__DRIscreen
* sPriv
,
373 __DRIdrawable
* dPriv
,
374 const __GLcontextModes
* visual
, GLboolean isPixmap
)
376 struct dri_drawable
*drawable
= NULL
;
378 struct swrast_renderbuffer
*frontrb
, *backrb
;
382 drawable
= CALLOC_STRUCT(dri_drawable
);
383 if (drawable
== NULL
)
386 dPriv
->driverPrivate
= drawable
;
387 drawable
->dPriv
= dPriv
;
389 drawable
->row
= malloc(MAX_WIDTH
* 4);
390 if (drawable
->row
== NULL
)
393 fb
= &drawable
->Base
;
395 /* basic framebuffer setup */
396 _mesa_initialize_window_framebuffer(fb
, visual
);
398 /* add front renderbuffer */
399 frontrb
= swrast_new_renderbuffer(visual
, GL_TRUE
);
400 _mesa_add_renderbuffer(fb
, BUFFER_FRONT_LEFT
, &frontrb
->Base
);
402 /* add back renderbuffer */
403 if (visual
->doubleBufferMode
) {
404 backrb
= swrast_new_renderbuffer(visual
, GL_FALSE
);
405 _mesa_add_renderbuffer(fb
, BUFFER_BACK_LEFT
, &backrb
->Base
);
408 /* add software renderbuffers */
409 _mesa_add_soft_renderbuffers(fb
,
410 GL_FALSE
, /* color */
411 visual
->haveDepthBuffer
,
412 visual
->haveStencilBuffer
,
413 visual
->haveAccumBuffer
,
414 GL_FALSE
, /* alpha */
415 GL_FALSE
/* aux bufs */);
430 dri_destroy_buffer(__DRIdrawable
* dPriv
)
435 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
440 fb
= &drawable
->Base
;
442 fb
->DeletePending
= GL_TRUE
;
443 _mesa_reference_framebuffer(&fb
, NULL
);
448 dri_swap_buffers(__DRIdrawable
* dPriv
)
450 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
452 GET_CURRENT_CONTEXT(ctx
);
454 struct dri_drawable
*drawable
= dri_drawable(dPriv
);
456 struct swrast_renderbuffer
*frontrb
, *backrb
;
460 fb
= &drawable
->Base
;
463 swrast_renderbuffer(fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
465 swrast_renderbuffer(fb
->Attachment
[BUFFER_BACK_LEFT
].Renderbuffer
);
467 /* check for signle-buffered */
471 /* check if swapping currently bound buffer */
472 if (ctx
&& ctx
->DrawBuffer
== fb
) {
473 /* flush pending rendering */
474 _mesa_notifySwapBuffers(ctx
);
477 sPriv
->swrast_loader
->putImage(dPriv
, __DRI_SWRAST_IMAGE_OP_SWAP
,
480 frontrb
->Base
.Height
,
482 dPriv
->loaderPrivate
);
487 * General device driver functions.
491 get_window_size( GLframebuffer
*fb
, GLsizei
*w
, GLsizei
*h
)
493 __DRIdrawable
*dPriv
= swrast_drawable(fb
)->dPriv
;
494 __DRIscreen
*sPriv
= dPriv
->driScreenPriv
;
497 sPriv
->swrast_loader
->getDrawableInfo(dPriv
,
499 dPriv
->loaderPrivate
);
503 swrast_check_and_update_window_size( GLcontext
*ctx
, GLframebuffer
*fb
)
505 GLsizei width
, height
;
507 get_window_size(fb
, &width
, &height
);
508 if (fb
->Width
!= width
|| fb
->Height
!= height
) {
509 _mesa_resize_framebuffer(ctx
, fb
, width
, height
);
513 static const GLubyte
*
514 get_string(GLcontext
*ctx
, GLenum pname
)
519 return (const GLubyte
*) "Mesa Project";
521 return (const GLubyte
*) "Software Rasterizer";
528 update_state( GLcontext
*ctx
, GLuint new_state
)
530 /* not much to do here - pass it on */
531 _swrast_InvalidateState( ctx
, new_state
);
532 _swsetup_InvalidateState( ctx
, new_state
);
533 _vbo_InvalidateState( ctx
, new_state
);
534 _tnl_InvalidateState( ctx
, new_state
);
538 viewport(GLcontext
*ctx
, GLint x
, GLint y
, GLsizei w
, GLsizei h
)
540 GLframebuffer
*draw
= ctx
->WinSysDrawBuffer
;
541 GLframebuffer
*read
= ctx
->WinSysReadBuffer
;
543 swrast_check_and_update_window_size(ctx
, draw
);
544 swrast_check_and_update_window_size(ctx
, read
);
547 static gl_format
swrastChooseTextureFormat(GLcontext
* ctx
,
548 GLint internalFormat
,
552 if (internalFormat
== GL_RGB
)
553 return MESA_FORMAT_XRGB8888
;
554 return _mesa_choose_tex_format(ctx
, internalFormat
, format
, type
);
558 swrast_init_driver_functions(struct dd_function_table
*driver
)
560 driver
->GetString
= get_string
;
561 driver
->UpdateState
= update_state
;
562 driver
->GetBufferSize
= NULL
;
563 driver
->Viewport
= viewport
;
564 driver
->ChooseTextureFormat
= swrastChooseTextureFormat
;
569 * Context-related functions.
573 dri_create_context(gl_api api
,
574 const __GLcontextModes
* visual
,
575 __DRIcontext
* cPriv
, void *sharedContextPrivate
)
577 struct dri_context
*ctx
= NULL
;
578 struct dri_context
*share
= (struct dri_context
*)sharedContextPrivate
;
579 GLcontext
*mesaCtx
= NULL
;
580 GLcontext
*sharedCtx
= NULL
;
581 struct dd_function_table functions
;
585 ctx
= CALLOC_STRUCT(dri_context
);
589 cPriv
->driverPrivate
= ctx
;
592 /* build table of device driver functions */
593 _mesa_init_driver_functions(&functions
);
594 swrast_init_driver_functions(&functions
);
597 sharedCtx
= &share
->Base
;
600 mesaCtx
= &ctx
->Base
;
602 /* basic context setup */
603 if (!_mesa_initialize_context(mesaCtx
, visual
, sharedCtx
, &functions
, (void *) cPriv
)) {
607 /* do bounds checking to prevent segfaults and server crashes! */
608 mesaCtx
->Const
.CheckArrayBounds
= GL_TRUE
;
610 /* create module contexts */
611 _swrast_CreateContext( mesaCtx
);
612 _vbo_CreateContext( mesaCtx
);
613 _tnl_CreateContext( mesaCtx
);
614 _swsetup_CreateContext( mesaCtx
);
615 _swsetup_Wakeup( mesaCtx
);
617 /* use default TCL pipeline */
619 TNLcontext
*tnl
= TNL_CONTEXT(mesaCtx
);
620 tnl
->Driver
.RunPipeline
= _tnl_run_pipeline
;
623 _mesa_enable_sw_extensions(mesaCtx
);
624 _mesa_enable_1_3_extensions(mesaCtx
);
625 _mesa_enable_1_4_extensions(mesaCtx
);
626 _mesa_enable_1_5_extensions(mesaCtx
);
627 _mesa_enable_2_0_extensions(mesaCtx
);
628 _mesa_enable_2_1_extensions(mesaCtx
);
630 _mesa_meta_init(mesaCtx
);
632 driInitExtensions( mesaCtx
, NULL
, GL_FALSE
);
644 dri_destroy_context(__DRIcontext
* cPriv
)
649 struct dri_context
*ctx
= dri_context(cPriv
);
652 mesaCtx
= &ctx
->Base
;
654 _mesa_meta_free(mesaCtx
);
655 _swsetup_DestroyContext( mesaCtx
);
656 _swrast_DestroyContext( mesaCtx
);
657 _tnl_DestroyContext( mesaCtx
);
658 _vbo_DestroyContext( mesaCtx
);
659 _mesa_destroy_context( mesaCtx
);
664 dri_make_current(__DRIcontext
* cPriv
,
665 __DRIdrawable
* driDrawPriv
,
666 __DRIdrawable
* driReadPriv
)
669 GLframebuffer
*mesaDraw
;
670 GLframebuffer
*mesaRead
;
674 struct dri_context
*ctx
= dri_context(cPriv
);
675 struct dri_drawable
*draw
;
676 struct dri_drawable
*read
;
678 if (!driDrawPriv
|| !driReadPriv
)
681 draw
= dri_drawable(driDrawPriv
);
682 read
= dri_drawable(driReadPriv
);
683 mesaCtx
= &ctx
->Base
;
684 mesaDraw
= &draw
->Base
;
685 mesaRead
= &read
->Base
;
687 /* check for same context and buffer */
688 if (mesaCtx
== _mesa_get_current_context()
689 && mesaCtx
->DrawBuffer
== mesaDraw
690 && mesaCtx
->ReadBuffer
== mesaRead
) {
694 _glapi_check_multithread();
696 swrast_check_and_update_window_size(mesaCtx
, mesaDraw
);
697 if (mesaRead
!= mesaDraw
)
698 swrast_check_and_update_window_size(mesaCtx
, mesaRead
);
700 _mesa_make_current( mesaCtx
,
706 _mesa_make_current( NULL
, NULL
, NULL
);
713 dri_unbind_context(__DRIcontext
* cPriv
)
721 const struct __DriverAPIRec driDriverAPI
= {
722 .InitScreen
= dri_init_screen
,
723 .DestroyScreen
= dri_destroy_screen
,
724 .CreateContext
= dri_create_context
,
725 .DestroyContext
= dri_destroy_context
,
726 .CreateBuffer
= dri_create_buffer
,
727 .DestroyBuffer
= dri_destroy_buffer
,
728 .SwapBuffers
= dri_swap_buffers
,
729 .MakeCurrent
= dri_make_current
,
730 .UnbindContext
= dri_unbind_context
,
733 /* This is the table of extensions that the loader will dlsym() for. */
734 PUBLIC
const __DRIextension
*__driDriverExtensions
[] = {
735 &driCoreExtension
.base
,
736 &driSWRastExtension
.base
,