2 * Mesa 3-D graphics library
4 * Copyright (C) 2010 LunarG Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
25 * Chia-I Wu <olv@lunarg.com>
28 #include "main/mtypes.h"
29 #include "main/extensions.h"
30 #include "main/context.h"
31 #include "main/debug_output.h"
32 #include "main/glthread.h"
33 #include "main/texobj.h"
34 #include "main/teximage.h"
35 #include "main/texstate.h"
36 #include "main/errors.h"
37 #include "main/framebuffer.h"
38 #include "main/fbobject.h"
39 #include "main/renderbuffer.h"
40 #include "main/version.h"
41 #include "util/hash_table.h"
42 #include "st_texture.h"
44 #include "st_context.h"
46 #include "st_extensions.h"
47 #include "st_format.h"
48 #include "st_cb_bitmap.h"
49 #include "st_cb_fbo.h"
50 #include "st_cb_flush.h"
51 #include "st_manager.h"
52 #include "st_sampler_view.h"
54 #include "state_tracker/st_gl_api.h"
56 #include "pipe/p_context.h"
57 #include "pipe/p_screen.h"
58 #include "util/u_format.h"
59 #include "util/u_pointer.h"
60 #include "util/u_inlines.h"
61 #include "util/u_atomic.h"
62 #include "util/u_surface.h"
63 #include "util/list.h"
66 struct st_manager_private
68 struct hash_table
*stfbi_ht
; /* framebuffer iface objects hash table */
74 * Map an attachment to a buffer index.
76 static inline gl_buffer_index
77 attachment_to_buffer_index(enum st_attachment_type statt
)
79 gl_buffer_index index
;
82 case ST_ATTACHMENT_FRONT_LEFT
:
83 index
= BUFFER_FRONT_LEFT
;
85 case ST_ATTACHMENT_BACK_LEFT
:
86 index
= BUFFER_BACK_LEFT
;
88 case ST_ATTACHMENT_FRONT_RIGHT
:
89 index
= BUFFER_FRONT_RIGHT
;
91 case ST_ATTACHMENT_BACK_RIGHT
:
92 index
= BUFFER_BACK_RIGHT
;
94 case ST_ATTACHMENT_DEPTH_STENCIL
:
97 case ST_ATTACHMENT_ACCUM
:
100 case ST_ATTACHMENT_SAMPLE
:
102 index
= BUFFER_COUNT
;
111 * Map a buffer index to an attachment.
113 static inline enum st_attachment_type
114 buffer_index_to_attachment(gl_buffer_index index
)
116 enum st_attachment_type statt
;
119 case BUFFER_FRONT_LEFT
:
120 statt
= ST_ATTACHMENT_FRONT_LEFT
;
122 case BUFFER_BACK_LEFT
:
123 statt
= ST_ATTACHMENT_BACK_LEFT
;
125 case BUFFER_FRONT_RIGHT
:
126 statt
= ST_ATTACHMENT_FRONT_RIGHT
;
128 case BUFFER_BACK_RIGHT
:
129 statt
= ST_ATTACHMENT_BACK_RIGHT
;
132 statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
135 statt
= ST_ATTACHMENT_ACCUM
;
138 statt
= ST_ATTACHMENT_INVALID
;
147 * Make sure a context picks up the latest cached state of the
148 * drawables it binds to.
151 st_context_validate(struct st_context
*st
,
152 struct st_framebuffer
*stdraw
,
153 struct st_framebuffer
*stread
)
155 if (stdraw
&& stdraw
->stamp
!= st
->draw_stamp
) {
156 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
157 _mesa_resize_framebuffer(st
->ctx
, &stdraw
->Base
,
159 stdraw
->Base
.Height
);
160 st
->draw_stamp
= stdraw
->stamp
;
163 if (stread
&& stread
->stamp
!= st
->read_stamp
) {
164 if (stread
!= stdraw
) {
165 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
166 _mesa_resize_framebuffer(st
->ctx
, &stread
->Base
,
168 stread
->Base
.Height
);
170 st
->read_stamp
= stread
->stamp
;
176 * Validate a framebuffer to make sure up-to-date pipe_textures are used.
177 * The context is only used for creating pipe surfaces and for calling
178 * _mesa_resize_framebuffer().
179 * (That should probably be rethought, since those surfaces become
180 * drawable state, not context state, and can be freed by another pipe
184 st_framebuffer_validate(struct st_framebuffer
*stfb
,
185 struct st_context
*st
)
187 struct pipe_resource
*textures
[ST_ATTACHMENT_COUNT
];
190 boolean changed
= FALSE
;
193 new_stamp
= p_atomic_read(&stfb
->iface
->stamp
);
194 if (stfb
->iface_stamp
== new_stamp
)
197 memset(textures
, 0, stfb
->num_statts
* sizeof(textures
[0]));
199 /* validate the fb */
201 if (!stfb
->iface
->validate(&st
->iface
, stfb
->iface
, stfb
->statts
,
202 stfb
->num_statts
, textures
))
205 stfb
->iface_stamp
= new_stamp
;
206 new_stamp
= p_atomic_read(&stfb
->iface
->stamp
);
207 } while(stfb
->iface_stamp
!= new_stamp
);
209 width
= stfb
->Base
.Width
;
210 height
= stfb
->Base
.Height
;
212 for (i
= 0; i
< stfb
->num_statts
; i
++) {
213 struct st_renderbuffer
*strb
;
214 struct pipe_surface
*ps
, surf_tmpl
;
220 idx
= attachment_to_buffer_index(stfb
->statts
[i
]);
221 if (idx
>= BUFFER_COUNT
) {
222 pipe_resource_reference(&textures
[i
], NULL
);
226 strb
= st_renderbuffer(stfb
->Base
.Attachment
[idx
].Renderbuffer
);
228 if (strb
->texture
== textures
[i
]) {
229 pipe_resource_reference(&textures
[i
], NULL
);
233 u_surface_default_template(&surf_tmpl
, textures
[i
]);
234 ps
= st
->pipe
->create_surface(st
->pipe
, textures
[i
], &surf_tmpl
);
236 struct pipe_surface
**psurf
=
237 util_format_is_srgb(ps
->format
) ? &strb
->surface_srgb
:
238 &strb
->surface_linear
;
240 pipe_surface_reference(psurf
, ps
);
241 strb
->surface
= *psurf
;
242 pipe_resource_reference(&strb
->texture
, ps
->texture
);
243 /* ownership transfered */
244 pipe_surface_reference(&ps
, NULL
);
248 strb
->Base
.Width
= strb
->surface
->width
;
249 strb
->Base
.Height
= strb
->surface
->height
;
251 width
= strb
->Base
.Width
;
252 height
= strb
->Base
.Height
;
255 pipe_resource_reference(&textures
[i
], NULL
);
260 _mesa_resize_framebuffer(st
->ctx
, &stfb
->Base
, width
, height
);
266 * Update the attachments to validate by looping the existing renderbuffers.
269 st_framebuffer_update_attachments(struct st_framebuffer
*stfb
)
273 stfb
->num_statts
= 0;
274 for (idx
= 0; idx
< BUFFER_COUNT
; idx
++) {
275 struct st_renderbuffer
*strb
;
276 enum st_attachment_type statt
;
278 strb
= st_renderbuffer(stfb
->Base
.Attachment
[idx
].Renderbuffer
);
279 if (!strb
|| strb
->software
)
282 statt
= buffer_index_to_attachment(idx
);
283 if (statt
!= ST_ATTACHMENT_INVALID
&&
284 st_visual_have_buffers(stfb
->iface
->visual
, 1 << statt
))
285 stfb
->statts
[stfb
->num_statts
++] = statt
;
292 * Add a renderbuffer to the framebuffer. The framebuffer is one that
293 * corresponds to a window and is not a user-created FBO.
296 st_framebuffer_add_renderbuffer(struct st_framebuffer
*stfb
,
299 struct gl_renderbuffer
*rb
;
300 enum pipe_format format
;
303 assert(_mesa_is_winsys_fbo(&stfb
->Base
));
305 /* do not distinguish depth/stencil buffers */
306 if (idx
== BUFFER_STENCIL
)
311 format
= stfb
->iface
->visual
->depth_stencil_format
;
315 format
= stfb
->iface
->visual
->accum_format
;
319 format
= stfb
->iface
->visual
->color_format
;
320 if (stfb
->Base
.Visual
.sRGBCapable
)
321 format
= util_format_srgb(format
);
326 if (format
== PIPE_FORMAT_NONE
)
329 rb
= st_new_renderbuffer_fb(format
, stfb
->iface
->visual
->samples
, sw
);
333 if (idx
!= BUFFER_DEPTH
) {
334 _mesa_attach_and_own_rb(&stfb
->Base
, idx
, rb
);
338 bool rb_ownership_taken
= false;
339 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_ZS
, 0)) {
340 _mesa_attach_and_own_rb(&stfb
->Base
, BUFFER_DEPTH
, rb
);
341 rb_ownership_taken
= true;
344 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_ZS
, 1)) {
345 if (rb_ownership_taken
)
346 _mesa_attach_and_reference_rb(&stfb
->Base
, BUFFER_STENCIL
, rb
);
348 _mesa_attach_and_own_rb(&stfb
->Base
, BUFFER_STENCIL
, rb
);
356 * Intialize a struct gl_config from a visual.
359 st_visual_to_context_mode(const struct st_visual
*visual
,
360 struct gl_config
*mode
)
362 memset(mode
, 0, sizeof(*mode
));
364 if (st_visual_have_buffers(visual
, ST_ATTACHMENT_BACK_LEFT_MASK
))
365 mode
->doubleBufferMode
= GL_TRUE
;
367 if (st_visual_have_buffers(visual
,
368 ST_ATTACHMENT_FRONT_RIGHT_MASK
| ST_ATTACHMENT_BACK_RIGHT_MASK
))
369 mode
->stereoMode
= GL_TRUE
;
371 if (visual
->color_format
!= PIPE_FORMAT_NONE
) {
372 mode
->rgbMode
= GL_TRUE
;
375 util_format_get_component_bits(visual
->color_format
,
376 UTIL_FORMAT_COLORSPACE_RGB
, 0);
378 util_format_get_component_bits(visual
->color_format
,
379 UTIL_FORMAT_COLORSPACE_RGB
, 1);
381 util_format_get_component_bits(visual
->color_format
,
382 UTIL_FORMAT_COLORSPACE_RGB
, 2);
384 util_format_get_component_bits(visual
->color_format
,
385 UTIL_FORMAT_COLORSPACE_RGB
, 3);
387 mode
->rgbBits
= mode
->redBits
+
388 mode
->greenBits
+ mode
->blueBits
+ mode
->alphaBits
;
389 mode
->sRGBCapable
= util_format_is_srgb(visual
->color_format
);
392 if (visual
->depth_stencil_format
!= PIPE_FORMAT_NONE
) {
394 util_format_get_component_bits(visual
->depth_stencil_format
,
395 UTIL_FORMAT_COLORSPACE_ZS
, 0);
397 util_format_get_component_bits(visual
->depth_stencil_format
,
398 UTIL_FORMAT_COLORSPACE_ZS
, 1);
400 mode
->haveDepthBuffer
= mode
->depthBits
> 0;
401 mode
->haveStencilBuffer
= mode
->stencilBits
> 0;
404 if (visual
->accum_format
!= PIPE_FORMAT_NONE
) {
405 mode
->haveAccumBuffer
= GL_TRUE
;
408 util_format_get_component_bits(visual
->accum_format
,
409 UTIL_FORMAT_COLORSPACE_RGB
, 0);
410 mode
->accumGreenBits
=
411 util_format_get_component_bits(visual
->accum_format
,
412 UTIL_FORMAT_COLORSPACE_RGB
, 1);
413 mode
->accumBlueBits
=
414 util_format_get_component_bits(visual
->accum_format
,
415 UTIL_FORMAT_COLORSPACE_RGB
, 2);
416 mode
->accumAlphaBits
=
417 util_format_get_component_bits(visual
->accum_format
,
418 UTIL_FORMAT_COLORSPACE_RGB
, 3);
421 if (visual
->samples
> 1) {
422 mode
->sampleBuffers
= 1;
423 mode
->samples
= visual
->samples
;
429 * Create a framebuffer from a manager interface.
431 static struct st_framebuffer
*
432 st_framebuffer_create(struct st_context
*st
,
433 struct st_framebuffer_iface
*stfbi
)
435 struct st_framebuffer
*stfb
;
436 struct gl_config mode
;
442 stfb
= CALLOC_STRUCT(st_framebuffer
);
446 st_visual_to_context_mode(stfbi
->visual
, &mode
);
449 * For desktop GL, sRGB framebuffer write is controlled by both the
450 * capability of the framebuffer and GL_FRAMEBUFFER_SRGB. We should
451 * advertise the capability when the pipe driver (and core Mesa) supports
452 * it so that applications can enable sRGB write when they want to.
454 * This is not to be confused with GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB. When
455 * the attribute is GLX_TRUE, it tells the st manager to pick a color
456 * format such that util_format_srgb(visual->color_format) can be supported
457 * by the pipe driver. We still need to advertise the capability here.
459 * For GLES, however, sRGB framebuffer write is controlled only by the
460 * capability of the framebuffer. There is GL_EXT_sRGB_write_control to
461 * give applications the control back, but sRGB write is still enabled by
462 * default. To avoid unexpected results, we should not advertise the
463 * capability. This could change when we add support for
464 * EGL_KHR_gl_colorspace.
466 if (_mesa_is_desktop_gl(st
->ctx
)) {
467 struct pipe_screen
*screen
= st
->pipe
->screen
;
468 const enum pipe_format srgb_format
=
469 util_format_srgb(stfbi
->visual
->color_format
);
471 if (srgb_format
!= PIPE_FORMAT_NONE
&&
472 st_pipe_format_to_mesa_format(srgb_format
) != MESA_FORMAT_NONE
&&
473 screen
->is_format_supported(screen
, srgb_format
,
474 PIPE_TEXTURE_2D
, stfbi
->visual
->samples
,
475 (PIPE_BIND_DISPLAY_TARGET
|
476 PIPE_BIND_RENDER_TARGET
)))
477 mode
.sRGBCapable
= GL_TRUE
;
480 _mesa_initialize_window_framebuffer(&stfb
->Base
, &mode
);
483 stfb
->iface_ID
= stfbi
->ID
;
484 stfb
->iface_stamp
= p_atomic_read(&stfbi
->stamp
) - 1;
486 /* add the color buffer */
487 idx
= stfb
->Base
._ColorDrawBufferIndexes
[0];
488 if (!st_framebuffer_add_renderbuffer(stfb
, idx
)) {
493 st_framebuffer_add_renderbuffer(stfb
, BUFFER_DEPTH
);
494 st_framebuffer_add_renderbuffer(stfb
, BUFFER_ACCUM
);
497 st_framebuffer_update_attachments(stfb
);
504 * Reference a framebuffer.
507 st_framebuffer_reference(struct st_framebuffer
**ptr
,
508 struct st_framebuffer
*stfb
)
510 struct gl_framebuffer
*fb
= &stfb
->Base
;
511 _mesa_reference_framebuffer((struct gl_framebuffer
**) ptr
, fb
);
516 st_framebuffer_iface_hash(const void *key
)
518 return (uintptr_t)key
;
523 st_framebuffer_iface_equal(const void *a
, const void *b
)
525 return (struct st_framebuffer_iface
*)a
== (struct st_framebuffer_iface
*)b
;
530 st_framebuffer_iface_lookup(struct st_manager
*smapi
,
531 const struct st_framebuffer_iface
*stfbi
)
533 struct st_manager_private
*smPriv
=
534 (struct st_manager_private
*)smapi
->st_manager_private
;
535 struct hash_entry
*entry
;
538 assert(smPriv
->stfbi_ht
);
540 mtx_lock(&smPriv
->st_mutex
);
541 entry
= _mesa_hash_table_search(smPriv
->stfbi_ht
, stfbi
);
542 mtx_unlock(&smPriv
->st_mutex
);
544 return entry
!= NULL
;
549 st_framebuffer_iface_insert(struct st_manager
*smapi
,
550 struct st_framebuffer_iface
*stfbi
)
552 struct st_manager_private
*smPriv
=
553 (struct st_manager_private
*)smapi
->st_manager_private
;
554 struct hash_entry
*entry
;
557 assert(smPriv
->stfbi_ht
);
559 mtx_lock(&smPriv
->st_mutex
);
560 entry
= _mesa_hash_table_insert(smPriv
->stfbi_ht
, stfbi
, stfbi
);
561 mtx_unlock(&smPriv
->st_mutex
);
563 return entry
!= NULL
;
568 st_framebuffer_iface_remove(struct st_manager
*smapi
,
569 struct st_framebuffer_iface
*stfbi
)
571 struct st_manager_private
*smPriv
=
572 (struct st_manager_private
*)smapi
->st_manager_private
;
573 struct hash_entry
*entry
;
575 if (!smPriv
|| !smPriv
->stfbi_ht
)
578 mtx_lock(&smPriv
->st_mutex
);
579 entry
= _mesa_hash_table_search(smPriv
->stfbi_ht
, stfbi
);
583 _mesa_hash_table_remove(smPriv
->stfbi_ht
, entry
);
586 mtx_unlock(&smPriv
->st_mutex
);
591 * The framebuffer interface object is no longer valid.
592 * Remove the object from the framebuffer interface hash table.
595 st_api_destroy_drawable(struct st_api
*stapi
,
596 struct st_framebuffer_iface
*stfbi
)
601 st_framebuffer_iface_remove(stfbi
->state_manager
, stfbi
);
606 * Purge the winsys buffers list to remove any references to
607 * non-existing framebuffer interface objects.
610 st_framebuffers_purge(struct st_context
*st
)
612 struct st_context_iface
*st_iface
= &st
->iface
;
613 struct st_manager
*smapi
= st_iface
->state_manager
;
614 struct st_framebuffer
*stfb
, *next
;
618 LIST_FOR_EACH_ENTRY_SAFE_REV(stfb
, next
, &st
->winsys_buffers
, head
) {
619 struct st_framebuffer_iface
*stfbi
= stfb
->iface
;
624 * If the corresponding framebuffer interface object no longer exists,
625 * remove the framebuffer object from the context's winsys buffers list,
626 * and unreference the framebuffer object, so its resources can be
629 if (!st_framebuffer_iface_lookup(smapi
, stfbi
)) {
630 LIST_DEL(&stfb
->head
);
631 st_framebuffer_reference(&stfb
, NULL
);
638 st_context_flush(struct st_context_iface
*stctxi
, unsigned flags
,
639 struct pipe_fence_handle
**fence
)
641 struct st_context
*st
= (struct st_context
*) stctxi
;
642 unsigned pipe_flags
= 0;
644 if (flags
& ST_FLUSH_END_OF_FRAME
)
645 pipe_flags
|= PIPE_FLUSH_END_OF_FRAME
;
646 if (flags
& ST_FLUSH_FENCE_FD
)
647 pipe_flags
|= PIPE_FLUSH_FENCE_FD
;
649 FLUSH_VERTICES(st
->ctx
, 0);
650 FLUSH_CURRENT(st
->ctx
, 0);
651 st_flush(st
, fence
, pipe_flags
);
653 if ((flags
& ST_FLUSH_WAIT
) && fence
&& *fence
) {
654 st
->pipe
->screen
->fence_finish(st
->pipe
->screen
, NULL
, *fence
,
655 PIPE_TIMEOUT_INFINITE
);
656 st
->pipe
->screen
->fence_reference(st
->pipe
->screen
, fence
, NULL
);
659 if (flags
& ST_FLUSH_FRONT
)
660 st_manager_flush_frontbuffer(st
);
662 /* DRI3 changes the framebuffer after SwapBuffers, but we need to invoke
663 * st_manager_validate_framebuffers to notice that.
665 * Set gfx_shaders_may_be_dirty to invoke st_validate_state in the next
666 * draw call, which will invoke st_manager_validate_framebuffers, but it
667 * won't dirty states if there is no change.
669 if (flags
& ST_FLUSH_END_OF_FRAME
)
670 st
->gfx_shaders_may_be_dirty
= true;
674 st_context_teximage(struct st_context_iface
*stctxi
,
675 enum st_texture_type tex_type
,
676 int level
, enum pipe_format pipe_format
,
677 struct pipe_resource
*tex
, boolean mipmap
)
679 struct st_context
*st
= (struct st_context
*) stctxi
;
680 struct gl_context
*ctx
= st
->ctx
;
681 struct gl_texture_object
*texObj
;
682 struct gl_texture_image
*texImage
;
683 struct st_texture_object
*stObj
;
684 struct st_texture_image
*stImage
;
685 GLenum internalFormat
;
686 GLuint width
, height
, depth
;
691 target
= GL_TEXTURE_1D
;
694 target
= GL_TEXTURE_2D
;
697 target
= GL_TEXTURE_3D
;
699 case ST_TEXTURE_RECT
:
700 target
= GL_TEXTURE_RECTANGLE_ARB
;
706 texObj
= _mesa_get_current_tex_object(ctx
, target
);
708 _mesa_lock_texture(ctx
, texObj
);
710 stObj
= st_texture_object(texObj
);
711 /* switch to surface based */
712 if (!stObj
->surface_based
) {
713 _mesa_clear_texture_object(ctx
, texObj
, NULL
);
714 stObj
->surface_based
= GL_TRUE
;
717 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
718 stImage
= st_texture_image(texImage
);
720 mesa_format texFormat
= st_pipe_format_to_mesa_format(pipe_format
);
722 if (util_format_has_alpha(tex
->format
))
723 internalFormat
= GL_RGBA
;
725 internalFormat
= GL_RGB
;
727 _mesa_init_teximage_fields(ctx
, texImage
,
728 tex
->width0
, tex
->height0
, 1, 0,
729 internalFormat
, texFormat
);
732 height
= tex
->height0
;
735 /* grow the image size until we hit level = 0 */
747 _mesa_clear_texture_image(ctx
, texImage
);
748 width
= height
= depth
= 0;
751 pipe_resource_reference(&stObj
->pt
, tex
);
752 st_texture_release_all_sampler_views(st
, stObj
);
753 pipe_resource_reference(&stImage
->pt
, tex
);
754 stObj
->surface_format
= pipe_format
;
756 stObj
->needs_validation
= true;
758 _mesa_dirty_texobj(ctx
, texObj
);
759 _mesa_unlock_texture(ctx
, texObj
);
766 st_context_copy(struct st_context_iface
*stctxi
,
767 struct st_context_iface
*stsrci
, unsigned mask
)
769 struct st_context
*st
= (struct st_context
*) stctxi
;
770 struct st_context
*src
= (struct st_context
*) stsrci
;
772 _mesa_copy_context(src
->ctx
, st
->ctx
, mask
);
777 st_context_share(struct st_context_iface
*stctxi
,
778 struct st_context_iface
*stsrci
)
780 struct st_context
*st
= (struct st_context
*) stctxi
;
781 struct st_context
*src
= (struct st_context
*) stsrci
;
783 return _mesa_share_state(st
->ctx
, src
->ctx
);
788 st_context_destroy(struct st_context_iface
*stctxi
)
790 struct st_context
*st
= (struct st_context
*) stctxi
;
791 st_destroy_context(st
);
796 st_start_thread(struct st_context_iface
*stctxi
)
798 struct st_context
*st
= (struct st_context
*) stctxi
;
800 _mesa_glthread_init(st
->ctx
);
805 st_thread_finish(struct st_context_iface
*stctxi
)
807 struct st_context
*st
= (struct st_context
*) stctxi
;
809 _mesa_glthread_finish(st
->ctx
);
814 st_manager_destroy(struct st_manager
*smapi
)
816 struct st_manager_private
*smPriv
= smapi
->st_manager_private
;
818 if (smPriv
&& smPriv
->stfbi_ht
) {
819 _mesa_hash_table_destroy(smPriv
->stfbi_ht
, NULL
);
820 mtx_destroy(&smPriv
->st_mutex
);
822 smapi
->st_manager_private
= NULL
;
827 static struct st_context_iface
*
828 st_api_create_context(struct st_api
*stapi
, struct st_manager
*smapi
,
829 const struct st_context_attribs
*attribs
,
830 enum st_context_error
*error
,
831 struct st_context_iface
*shared_stctxi
)
833 struct st_context
*shared_ctx
= (struct st_context
*) shared_stctxi
;
834 struct st_context
*st
;
835 struct pipe_context
*pipe
;
836 struct gl_config mode
;
838 bool no_error
= false;
839 unsigned ctx_flags
= PIPE_CONTEXT_PREFER_THREADED
;
841 if (!(stapi
->profile_mask
& (1 << attribs
->profile
)))
844 switch (attribs
->profile
) {
845 case ST_PROFILE_DEFAULT
:
846 api
= API_OPENGL_COMPAT
;
848 case ST_PROFILE_OPENGL_ES1
:
851 case ST_PROFILE_OPENGL_ES2
:
854 case ST_PROFILE_OPENGL_CORE
:
855 api
= API_OPENGL_CORE
;
858 *error
= ST_CONTEXT_ERROR_BAD_API
;
862 /* Create a hash table for the framebuffer interface objects
863 * if it has not been created for this st manager.
865 if (smapi
->st_manager_private
== NULL
) {
866 struct st_manager_private
*smPriv
;
868 smPriv
= CALLOC_STRUCT(st_manager_private
);
869 mtx_init(&smPriv
->st_mutex
, mtx_plain
);
870 smPriv
->stfbi_ht
= _mesa_hash_table_create(NULL
,
871 st_framebuffer_iface_hash
,
872 st_framebuffer_iface_equal
);
873 smapi
->st_manager_private
= smPriv
;
874 smapi
->destroy
= st_manager_destroy
;
877 if (attribs
->flags
& ST_CONTEXT_FLAG_ROBUST_ACCESS
)
878 ctx_flags
|= PIPE_CONTEXT_ROBUST_BUFFER_ACCESS
;
880 if (attribs
->flags
& ST_CONTEXT_FLAG_NO_ERROR
)
883 if (attribs
->flags
& ST_CONTEXT_FLAG_LOW_PRIORITY
)
884 ctx_flags
|= PIPE_CONTEXT_LOW_PRIORITY
;
885 else if (attribs
->flags
& ST_CONTEXT_FLAG_HIGH_PRIORITY
)
886 ctx_flags
|= PIPE_CONTEXT_HIGH_PRIORITY
;
888 pipe
= smapi
->screen
->context_create(smapi
->screen
, NULL
, ctx_flags
);
890 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
894 st_visual_to_context_mode(&attribs
->visual
, &mode
);
895 st
= st_create_context(api
, pipe
, &mode
, shared_ctx
,
896 &attribs
->options
, no_error
);
898 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
903 if (attribs
->flags
& ST_CONTEXT_FLAG_DEBUG
) {
904 if (!_mesa_set_debug_state_int(st
->ctx
, GL_DEBUG_OUTPUT
, GL_TRUE
)) {
905 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
909 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_DEBUG_BIT
;
912 if (st
->ctx
->Const
.ContextFlags
& GL_CONTEXT_FLAG_DEBUG_BIT
) {
913 st_update_debug_callback(st
);
916 if (attribs
->flags
& ST_CONTEXT_FLAG_FORWARD_COMPATIBLE
)
917 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT
;
918 if (attribs
->flags
& ST_CONTEXT_FLAG_ROBUST_ACCESS
) {
919 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB
;
920 st
->ctx
->Const
.RobustAccess
= GL_TRUE
;
922 if (attribs
->flags
& ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED
) {
923 st
->ctx
->Const
.ResetStrategy
= GL_LOSE_CONTEXT_ON_RESET_ARB
;
924 st_install_device_reset_callback(st
);
927 if (attribs
->flags
& ST_CONTEXT_FLAG_RELEASE_NONE
)
928 st
->ctx
->Const
.ContextReleaseBehavior
= GL_NONE
;
930 /* need to perform version check */
931 if (attribs
->major
> 1 || attribs
->minor
> 0) {
932 /* Is the actual version less than the requested version?
934 if (st
->ctx
->Version
< attribs
->major
* 10U + attribs
->minor
) {
935 *error
= ST_CONTEXT_ERROR_BAD_VERSION
;
936 st_destroy_context(st
);
941 st
->invalidate_on_gl_viewport
=
942 smapi
->get_param(smapi
, ST_MANAGER_BROKEN_INVALIDATE
);
944 st
->iface
.destroy
= st_context_destroy
;
945 st
->iface
.flush
= st_context_flush
;
946 st
->iface
.teximage
= st_context_teximage
;
947 st
->iface
.copy
= st_context_copy
;
948 st
->iface
.share
= st_context_share
;
949 st
->iface
.start_thread
= st_start_thread
;
950 st
->iface
.thread_finish
= st_thread_finish
;
951 st
->iface
.st_context_private
= (void *) smapi
;
952 st
->iface
.cso_context
= st
->cso_context
;
953 st
->iface
.pipe
= st
->pipe
;
954 st
->iface
.state_manager
= smapi
;
956 *error
= ST_CONTEXT_SUCCESS
;
961 static struct st_context_iface
*
962 st_api_get_current(struct st_api
*stapi
)
964 GET_CURRENT_CONTEXT(ctx
);
965 struct st_context
*st
= ctx
? ctx
->st
: NULL
;
967 return st
? &st
->iface
: NULL
;
971 static struct st_framebuffer
*
972 st_framebuffer_reuse_or_create(struct st_context
*st
,
973 struct gl_framebuffer
*fb
,
974 struct st_framebuffer_iface
*stfbi
)
976 struct st_framebuffer
*cur
= NULL
, *stfb
= NULL
;
981 /* Check if there is already a framebuffer object for the specified
982 * framebuffer interface in this context. If there is one, use it.
984 LIST_FOR_EACH_ENTRY(cur
, &st
->winsys_buffers
, head
) {
985 if (cur
->iface_ID
== stfbi
->ID
) {
986 st_framebuffer_reference(&stfb
, cur
);
991 /* If there is not already a framebuffer object, create one */
993 cur
= st_framebuffer_create(st
, stfbi
);
996 /* add the referenced framebuffer interface object to
997 * the framebuffer interface object hash table.
999 if (!st_framebuffer_iface_insert(stfbi
->state_manager
, stfbi
)) {
1000 st_framebuffer_reference(&cur
, NULL
);
1004 /* add to the context's winsys buffers list */
1005 LIST_ADD(&cur
->head
, &st
->winsys_buffers
);
1007 st_framebuffer_reference(&stfb
, cur
);
1016 st_api_make_current(struct st_api
*stapi
, struct st_context_iface
*stctxi
,
1017 struct st_framebuffer_iface
*stdrawi
,
1018 struct st_framebuffer_iface
*streadi
)
1020 struct st_context
*st
= (struct st_context
*) stctxi
;
1021 struct st_framebuffer
*stdraw
, *stread
;
1024 _glapi_check_multithread();
1027 /* reuse or create the draw fb */
1028 stdraw
= st_framebuffer_reuse_or_create(st
,
1029 st
->ctx
->WinSysDrawBuffer
, stdrawi
);
1030 if (streadi
!= stdrawi
) {
1031 /* do the same for the read fb */
1032 stread
= st_framebuffer_reuse_or_create(st
,
1033 st
->ctx
->WinSysReadBuffer
, streadi
);
1037 /* reuse the draw fb for the read fb */
1039 st_framebuffer_reference(&stread
, stdraw
);
1042 if (stdraw
&& stread
) {
1043 st_framebuffer_validate(stdraw
, st
);
1044 if (stread
!= stdraw
)
1045 st_framebuffer_validate(stread
, st
);
1047 ret
= _mesa_make_current(st
->ctx
, &stdraw
->Base
, &stread
->Base
);
1049 st
->draw_stamp
= stdraw
->stamp
- 1;
1050 st
->read_stamp
= stread
->stamp
- 1;
1051 st_context_validate(st
, stdraw
, stread
);
1054 struct gl_framebuffer
*incomplete
= _mesa_get_incomplete_framebuffer();
1055 ret
= _mesa_make_current(st
->ctx
, incomplete
, incomplete
);
1058 st_framebuffer_reference(&stdraw
, NULL
);
1059 st_framebuffer_reference(&stread
, NULL
);
1061 /* Purge the context's winsys_buffers list in case any
1062 * of the referenced drawables no longer exist.
1064 st_framebuffers_purge(st
);
1067 ret
= _mesa_make_current(NULL
, NULL
, NULL
);
1075 st_api_destroy(struct st_api
*stapi
)
1081 * Flush the front buffer if the current context renders to the front buffer.
1084 st_manager_flush_frontbuffer(struct st_context
*st
)
1086 struct st_framebuffer
*stfb
= st_ws_framebuffer(st
->ctx
->DrawBuffer
);
1087 struct st_renderbuffer
*strb
= NULL
;
1090 strb
= st_renderbuffer(stfb
->Base
.Attachment
[BUFFER_FRONT_LEFT
].
1093 /* Do we have a front color buffer and has it been drawn to since last
1094 * frontbuffer flush?
1096 if (strb
&& strb
->defined
) {
1097 stfb
->iface
->flush_front(&st
->iface
, stfb
->iface
,
1098 ST_ATTACHMENT_FRONT_LEFT
);
1099 strb
->defined
= GL_FALSE
;
1101 /* Trigger an update of strb->defined on next draw */
1102 st
->dirty
|= ST_NEW_FB_STATE
;
1108 * Re-validate the framebuffers.
1111 st_manager_validate_framebuffers(struct st_context
*st
)
1113 struct st_framebuffer
*stdraw
= st_ws_framebuffer(st
->ctx
->DrawBuffer
);
1114 struct st_framebuffer
*stread
= st_ws_framebuffer(st
->ctx
->ReadBuffer
);
1117 st_framebuffer_validate(stdraw
, st
);
1118 if (stread
&& stread
!= stdraw
)
1119 st_framebuffer_validate(stread
, st
);
1121 st_context_validate(st
, stdraw
, stread
);
1126 * Flush any outstanding swapbuffers on the current draw framebuffer.
1129 st_manager_flush_swapbuffers(void)
1131 GET_CURRENT_CONTEXT(ctx
);
1132 struct st_context
*st
= (ctx
) ? ctx
->st
: NULL
;
1133 struct st_framebuffer
*stfb
;
1138 stfb
= st_ws_framebuffer(ctx
->DrawBuffer
);
1139 if (!stfb
|| !stfb
->iface
->flush_swapbuffers
)
1142 stfb
->iface
->flush_swapbuffers(&st
->iface
, stfb
->iface
);
1147 * Add a color renderbuffer on demand. The FBO must correspond to a window,
1148 * not a user-created FBO.
1151 st_manager_add_color_renderbuffer(struct st_context
*st
,
1152 struct gl_framebuffer
*fb
,
1153 gl_buffer_index idx
)
1155 struct st_framebuffer
*stfb
= st_ws_framebuffer(fb
);
1161 assert(_mesa_is_winsys_fbo(fb
));
1163 if (stfb
->Base
.Attachment
[idx
].Renderbuffer
)
1167 case BUFFER_FRONT_LEFT
:
1168 case BUFFER_BACK_LEFT
:
1169 case BUFFER_FRONT_RIGHT
:
1170 case BUFFER_BACK_RIGHT
:
1176 if (!st_framebuffer_add_renderbuffer(stfb
, idx
))
1179 st_framebuffer_update_attachments(stfb
);
1182 * Force a call to the state tracker manager to validate the
1183 * new renderbuffer. It might be that there is a window system
1184 * renderbuffer available.
1187 stfb
->iface_stamp
= p_atomic_read(&stfb
->iface
->stamp
) - 1;
1189 st_invalidate_buffers(st
);
1196 get_version(struct pipe_screen
*screen
,
1197 struct st_config_options
*options
, gl_api api
)
1199 struct gl_constants consts
= {0};
1200 struct gl_extensions extensions
= {0};
1203 if (_mesa_override_gl_version_contextless(&consts
, &api
, &version
)) {
1207 _mesa_init_constants(&consts
, api
);
1208 _mesa_init_extensions(&extensions
);
1210 st_init_limits(screen
, &consts
, &extensions
, api
);
1211 st_init_extensions(screen
, &consts
, &extensions
, options
, api
);
1213 return _mesa_get_version(&extensions
, &consts
, api
);
1218 st_api_query_versions(struct st_api
*stapi
, struct st_manager
*sm
,
1219 struct st_config_options
*options
,
1220 int *gl_core_version
,
1221 int *gl_compat_version
,
1222 int *gl_es1_version
,
1223 int *gl_es2_version
)
1225 *gl_core_version
= get_version(sm
->screen
, options
, API_OPENGL_CORE
);
1226 *gl_compat_version
= get_version(sm
->screen
, options
, API_OPENGL_COMPAT
);
1227 *gl_es1_version
= get_version(sm
->screen
, options
, API_OPENGLES
);
1228 *gl_es2_version
= get_version(sm
->screen
, options
, API_OPENGLES2
);
1232 static const struct st_api st_gl_api
= {
1233 .name
= "Mesa " PACKAGE_VERSION
,
1234 .api
= ST_API_OPENGL
,
1235 .profile_mask
= ST_PROFILE_DEFAULT_MASK
|
1236 ST_PROFILE_OPENGL_CORE_MASK
|
1237 ST_PROFILE_OPENGL_ES1_MASK
|
1238 ST_PROFILE_OPENGL_ES2_MASK
|
1240 .feature_mask
= ST_API_FEATURE_MS_VISUALS_MASK
,
1241 .destroy
= st_api_destroy
,
1242 .query_versions
= st_api_query_versions
,
1243 .create_context
= st_api_create_context
,
1244 .make_current
= st_api_make_current
,
1245 .get_current
= st_api_get_current
,
1246 .destroy_drawable
= st_api_destroy_drawable
,
1251 st_gl_api_create(void)
1253 return (struct st_api
*) &st_gl_api
;