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_fbo.h"
49 #include "st_cb_flush.h"
50 #include "st_manager.h"
51 #include "st_sampler_view.h"
53 #include "state_tracker/st_gl_api.h"
55 #include "pipe/p_context.h"
56 #include "pipe/p_screen.h"
57 #include "util/u_format.h"
58 #include "util/u_pointer.h"
59 #include "util/u_inlines.h"
60 #include "util/u_atomic.h"
61 #include "util/u_surface.h"
62 #include "util/list.h"
65 struct st_manager_private
67 struct hash_table
*stfbi_ht
; /* framebuffer iface objects hash table */
71 static void st_manager_destroy(struct st_manager
*);
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
;
110 * Map a buffer index to an attachment.
112 static inline enum st_attachment_type
113 buffer_index_to_attachment(gl_buffer_index index
)
115 enum st_attachment_type statt
;
118 case BUFFER_FRONT_LEFT
:
119 statt
= ST_ATTACHMENT_FRONT_LEFT
;
121 case BUFFER_BACK_LEFT
:
122 statt
= ST_ATTACHMENT_BACK_LEFT
;
124 case BUFFER_FRONT_RIGHT
:
125 statt
= ST_ATTACHMENT_FRONT_RIGHT
;
127 case BUFFER_BACK_RIGHT
:
128 statt
= ST_ATTACHMENT_BACK_RIGHT
;
131 statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
134 statt
= ST_ATTACHMENT_ACCUM
;
137 statt
= ST_ATTACHMENT_INVALID
;
145 * Make sure a context picks up the latest cached state of the
146 * drawables it binds to.
149 st_context_validate(struct st_context
*st
,
150 struct st_framebuffer
*stdraw
,
151 struct st_framebuffer
*stread
)
153 if (stdraw
&& stdraw
->stamp
!= st
->draw_stamp
) {
154 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
155 _mesa_resize_framebuffer(st
->ctx
, &stdraw
->Base
,
157 stdraw
->Base
.Height
);
158 st
->draw_stamp
= stdraw
->stamp
;
161 if (stread
&& stread
->stamp
!= st
->read_stamp
) {
162 if (stread
!= stdraw
) {
163 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
164 _mesa_resize_framebuffer(st
->ctx
, &stread
->Base
,
166 stread
->Base
.Height
);
168 st
->read_stamp
= stread
->stamp
;
173 * Validate a framebuffer to make sure up-to-date pipe_textures are used.
174 * The context is only used for creating pipe surfaces and for calling
175 * _mesa_resize_framebuffer().
176 * (That should probably be rethought, since those surfaces become
177 * drawable state, not context state, and can be freed by another pipe
181 st_framebuffer_validate(struct st_framebuffer
*stfb
,
182 struct st_context
*st
)
184 struct pipe_resource
*textures
[ST_ATTACHMENT_COUNT
];
187 boolean changed
= FALSE
;
190 new_stamp
= p_atomic_read(&stfb
->iface
->stamp
);
191 if (stfb
->iface_stamp
== new_stamp
)
194 /* validate the fb */
196 if (!stfb
->iface
->validate(&st
->iface
, stfb
->iface
, stfb
->statts
,
197 stfb
->num_statts
, textures
))
200 stfb
->iface_stamp
= new_stamp
;
201 new_stamp
= p_atomic_read(&stfb
->iface
->stamp
);
202 } while(stfb
->iface_stamp
!= new_stamp
);
204 width
= stfb
->Base
.Width
;
205 height
= stfb
->Base
.Height
;
207 for (i
= 0; i
< stfb
->num_statts
; i
++) {
208 struct st_renderbuffer
*strb
;
209 struct pipe_surface
*ps
, surf_tmpl
;
215 idx
= attachment_to_buffer_index(stfb
->statts
[i
]);
216 if (idx
>= BUFFER_COUNT
) {
217 pipe_resource_reference(&textures
[i
], NULL
);
221 strb
= st_renderbuffer(stfb
->Base
.Attachment
[idx
].Renderbuffer
);
223 if (strb
->texture
== textures
[i
]) {
224 pipe_resource_reference(&textures
[i
], NULL
);
228 u_surface_default_template(&surf_tmpl
, textures
[i
]);
229 ps
= st
->pipe
->create_surface(st
->pipe
, textures
[i
], &surf_tmpl
);
231 struct pipe_surface
**psurf
=
232 util_format_is_srgb(ps
->format
) ? &strb
->surface_srgb
:
233 &strb
->surface_linear
;
235 pipe_surface_reference(psurf
, ps
);
236 strb
->surface
= *psurf
;
237 pipe_resource_reference(&strb
->texture
, ps
->texture
);
238 /* ownership transfered */
239 pipe_surface_reference(&ps
, NULL
);
243 strb
->Base
.Width
= strb
->surface
->width
;
244 strb
->Base
.Height
= strb
->surface
->height
;
246 width
= strb
->Base
.Width
;
247 height
= strb
->Base
.Height
;
250 pipe_resource_reference(&textures
[i
], NULL
);
255 _mesa_resize_framebuffer(st
->ctx
, &stfb
->Base
, width
, height
);
260 * Update the attachments to validate by looping the existing renderbuffers.
263 st_framebuffer_update_attachments(struct st_framebuffer
*stfb
)
267 stfb
->num_statts
= 0;
268 for (idx
= 0; idx
< BUFFER_COUNT
; idx
++) {
269 struct st_renderbuffer
*strb
;
270 enum st_attachment_type statt
;
272 strb
= st_renderbuffer(stfb
->Base
.Attachment
[idx
].Renderbuffer
);
273 if (!strb
|| strb
->software
)
276 statt
= buffer_index_to_attachment(idx
);
277 if (statt
!= ST_ATTACHMENT_INVALID
&&
278 st_visual_have_buffers(stfb
->iface
->visual
, 1 << statt
))
279 stfb
->statts
[stfb
->num_statts
++] = statt
;
285 * Add a renderbuffer to the framebuffer. The framebuffer is one that
286 * corresponds to a window and is not a user-created FBO.
289 st_framebuffer_add_renderbuffer(struct st_framebuffer
*stfb
,
292 struct gl_renderbuffer
*rb
;
293 enum pipe_format format
;
296 assert(_mesa_is_winsys_fbo(&stfb
->Base
));
298 /* do not distinguish depth/stencil buffers */
299 if (idx
== BUFFER_STENCIL
)
304 format
= stfb
->iface
->visual
->depth_stencil_format
;
308 format
= stfb
->iface
->visual
->accum_format
;
312 format
= stfb
->iface
->visual
->color_format
;
313 if (stfb
->Base
.Visual
.sRGBCapable
)
314 format
= util_format_srgb(format
);
319 if (format
== PIPE_FORMAT_NONE
)
322 rb
= st_new_renderbuffer_fb(format
, stfb
->iface
->visual
->samples
, sw
);
326 if (idx
!= BUFFER_DEPTH
) {
327 _mesa_attach_and_own_rb(&stfb
->Base
, idx
, rb
);
331 bool rb_ownership_taken
= false;
332 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_ZS
, 0)) {
333 _mesa_attach_and_own_rb(&stfb
->Base
, BUFFER_DEPTH
, rb
);
334 rb_ownership_taken
= true;
337 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_ZS
, 1)) {
338 if (rb_ownership_taken
)
339 _mesa_attach_and_reference_rb(&stfb
->Base
, BUFFER_STENCIL
, rb
);
341 _mesa_attach_and_own_rb(&stfb
->Base
, BUFFER_STENCIL
, rb
);
348 * Intialize a struct gl_config from a visual.
351 st_visual_to_context_mode(const struct st_visual
*visual
,
352 struct gl_config
*mode
)
354 memset(mode
, 0, sizeof(*mode
));
356 if (st_visual_have_buffers(visual
, ST_ATTACHMENT_BACK_LEFT_MASK
))
357 mode
->doubleBufferMode
= GL_TRUE
;
358 if (st_visual_have_buffers(visual
,
359 ST_ATTACHMENT_FRONT_RIGHT_MASK
| ST_ATTACHMENT_BACK_RIGHT_MASK
))
360 mode
->stereoMode
= GL_TRUE
;
362 if (visual
->color_format
!= PIPE_FORMAT_NONE
) {
363 mode
->rgbMode
= GL_TRUE
;
366 util_format_get_component_bits(visual
->color_format
,
367 UTIL_FORMAT_COLORSPACE_RGB
, 0);
369 util_format_get_component_bits(visual
->color_format
,
370 UTIL_FORMAT_COLORSPACE_RGB
, 1);
372 util_format_get_component_bits(visual
->color_format
,
373 UTIL_FORMAT_COLORSPACE_RGB
, 2);
375 util_format_get_component_bits(visual
->color_format
,
376 UTIL_FORMAT_COLORSPACE_RGB
, 3);
378 mode
->rgbBits
= mode
->redBits
+
379 mode
->greenBits
+ mode
->blueBits
+ mode
->alphaBits
;
380 mode
->sRGBCapable
= util_format_is_srgb(visual
->color_format
);
383 if (visual
->depth_stencil_format
!= PIPE_FORMAT_NONE
) {
385 util_format_get_component_bits(visual
->depth_stencil_format
,
386 UTIL_FORMAT_COLORSPACE_ZS
, 0);
388 util_format_get_component_bits(visual
->depth_stencil_format
,
389 UTIL_FORMAT_COLORSPACE_ZS
, 1);
391 mode
->haveDepthBuffer
= mode
->depthBits
> 0;
392 mode
->haveStencilBuffer
= mode
->stencilBits
> 0;
395 if (visual
->accum_format
!= PIPE_FORMAT_NONE
) {
396 mode
->haveAccumBuffer
= GL_TRUE
;
399 util_format_get_component_bits(visual
->accum_format
,
400 UTIL_FORMAT_COLORSPACE_RGB
, 0);
401 mode
->accumGreenBits
=
402 util_format_get_component_bits(visual
->accum_format
,
403 UTIL_FORMAT_COLORSPACE_RGB
, 1);
404 mode
->accumBlueBits
=
405 util_format_get_component_bits(visual
->accum_format
,
406 UTIL_FORMAT_COLORSPACE_RGB
, 2);
407 mode
->accumAlphaBits
=
408 util_format_get_component_bits(visual
->accum_format
,
409 UTIL_FORMAT_COLORSPACE_RGB
, 3);
412 if (visual
->samples
> 1) {
413 mode
->sampleBuffers
= 1;
414 mode
->samples
= visual
->samples
;
419 * Create a framebuffer from a manager interface.
421 static struct st_framebuffer
*
422 st_framebuffer_create(struct st_context
*st
,
423 struct st_framebuffer_iface
*stfbi
)
425 struct st_framebuffer
*stfb
;
426 struct gl_config mode
;
432 stfb
= CALLOC_STRUCT(st_framebuffer
);
436 st_visual_to_context_mode(stfbi
->visual
, &mode
);
439 * For desktop GL, sRGB framebuffer write is controlled by both the
440 * capability of the framebuffer and GL_FRAMEBUFFER_SRGB. We should
441 * advertise the capability when the pipe driver (and core Mesa) supports
442 * it so that applications can enable sRGB write when they want to.
444 * This is not to be confused with GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB. When
445 * the attribute is GLX_TRUE, it tells the st manager to pick a color
446 * format such that util_format_srgb(visual->color_format) can be supported
447 * by the pipe driver. We still need to advertise the capability here.
449 * For GLES, however, sRGB framebuffer write is controlled only by the
450 * capability of the framebuffer. There is GL_EXT_sRGB_write_control to
451 * give applications the control back, but sRGB write is still enabled by
452 * default. To avoid unexpected results, we should not advertise the
453 * capability. This could change when we add support for
454 * EGL_KHR_gl_colorspace.
456 if (_mesa_is_desktop_gl(st
->ctx
)) {
457 struct pipe_screen
*screen
= st
->pipe
->screen
;
458 const enum pipe_format srgb_format
=
459 util_format_srgb(stfbi
->visual
->color_format
);
461 if (srgb_format
!= PIPE_FORMAT_NONE
&&
462 st_pipe_format_to_mesa_format(srgb_format
) != MESA_FORMAT_NONE
&&
463 screen
->is_format_supported(screen
, srgb_format
,
464 PIPE_TEXTURE_2D
, stfbi
->visual
->samples
,
465 (PIPE_BIND_DISPLAY_TARGET
|
466 PIPE_BIND_RENDER_TARGET
)))
467 mode
.sRGBCapable
= GL_TRUE
;
470 _mesa_initialize_window_framebuffer(&stfb
->Base
, &mode
);
473 stfb
->iface_ID
= stfbi
->ID
;
474 stfb
->iface_stamp
= p_atomic_read(&stfbi
->stamp
) - 1;
476 /* add the color buffer */
477 idx
= stfb
->Base
._ColorDrawBufferIndexes
[0];
478 if (!st_framebuffer_add_renderbuffer(stfb
, idx
)) {
483 st_framebuffer_add_renderbuffer(stfb
, BUFFER_DEPTH
);
484 st_framebuffer_add_renderbuffer(stfb
, BUFFER_ACCUM
);
487 st_framebuffer_update_attachments(stfb
);
493 * Reference a framebuffer.
496 st_framebuffer_reference(struct st_framebuffer
**ptr
,
497 struct st_framebuffer
*stfb
)
499 struct gl_framebuffer
*fb
= &stfb
->Base
;
500 _mesa_reference_framebuffer((struct gl_framebuffer
**) ptr
, fb
);
505 st_framebuffer_iface_hash(const void *key
)
507 return (uintptr_t)key
;
512 st_framebuffer_iface_equal(const void *a
, const void *b
)
514 return (struct st_framebuffer_iface
*)a
== (struct st_framebuffer_iface
*)b
;
519 st_framebuffer_iface_lookup(struct st_manager
*smapi
,
520 const struct st_framebuffer_iface
*stfbi
)
522 struct st_manager_private
*smPriv
=
523 (struct st_manager_private
*)smapi
->st_manager_private
;
524 struct hash_entry
*entry
;
527 assert(smPriv
->stfbi_ht
);
529 mtx_lock(&smPriv
->st_mutex
);
530 entry
= _mesa_hash_table_search(smPriv
->stfbi_ht
, stfbi
);
531 mtx_unlock(&smPriv
->st_mutex
);
533 return entry
!= NULL
;
538 st_framebuffer_iface_insert(struct st_manager
*smapi
,
539 struct st_framebuffer_iface
*stfbi
)
541 struct st_manager_private
*smPriv
=
542 (struct st_manager_private
*)smapi
->st_manager_private
;
543 struct hash_entry
*entry
;
546 assert(smPriv
->stfbi_ht
);
548 mtx_lock(&smPriv
->st_mutex
);
549 entry
= _mesa_hash_table_insert(smPriv
->stfbi_ht
, stfbi
, stfbi
);
550 mtx_unlock(&smPriv
->st_mutex
);
552 return entry
!= NULL
;
557 st_framebuffer_iface_remove(struct st_manager
*smapi
,
558 struct st_framebuffer_iface
*stfbi
)
560 struct st_manager_private
*smPriv
=
561 (struct st_manager_private
*)smapi
->st_manager_private
;
562 struct hash_entry
*entry
;
564 if (!smPriv
|| !smPriv
->stfbi_ht
)
567 mtx_lock(&smPriv
->st_mutex
);
568 entry
= _mesa_hash_table_search(smPriv
->stfbi_ht
, stfbi
);
572 _mesa_hash_table_remove(smPriv
->stfbi_ht
, entry
);
575 mtx_unlock(&smPriv
->st_mutex
);
580 * The framebuffer interface object is no longer valid.
581 * Remove the object from the framebuffer interface hash table.
584 st_api_destroy_drawable(struct st_api
*stapi
,
585 struct st_framebuffer_iface
*stfbi
)
590 st_framebuffer_iface_remove(stfbi
->state_manager
, stfbi
);
595 * Purge the winsys buffers list to remove any references to
596 * non-existing framebuffer interface objects.
599 st_framebuffers_purge(struct st_context
*st
)
601 struct st_context_iface
*st_iface
= &st
->iface
;
602 struct st_manager
*smapi
= st_iface
->state_manager
;
603 struct st_framebuffer
*stfb
, *next
;
607 LIST_FOR_EACH_ENTRY_SAFE_REV(stfb
, next
, &st
->winsys_buffers
, head
) {
608 struct st_framebuffer_iface
*stfbi
= stfb
->iface
;
613 * If the corresponding framebuffer interface object no longer exists,
614 * remove the framebuffer object from the context's winsys buffers list,
615 * and unreference the framebuffer object, so its resources can be
618 if (!st_framebuffer_iface_lookup(smapi
, stfbi
)) {
619 LIST_DEL(&stfb
->head
);
620 st_framebuffer_reference(&stfb
, NULL
);
626 st_context_flush(struct st_context_iface
*stctxi
, unsigned flags
,
627 struct pipe_fence_handle
**fence
)
629 struct st_context
*st
= (struct st_context
*) stctxi
;
630 unsigned pipe_flags
= 0;
632 if (flags
& ST_FLUSH_END_OF_FRAME
) {
633 pipe_flags
|= PIPE_FLUSH_END_OF_FRAME
;
636 st_flush(st
, fence
, pipe_flags
);
638 if ((flags
& ST_FLUSH_WAIT
) && fence
) {
639 st
->pipe
->screen
->fence_finish(st
->pipe
->screen
, NULL
, *fence
,
640 PIPE_TIMEOUT_INFINITE
);
641 st
->pipe
->screen
->fence_reference(st
->pipe
->screen
, fence
, NULL
);
644 if (flags
& ST_FLUSH_FRONT
)
645 st_manager_flush_frontbuffer(st
);
647 /* DRI3 changes the framebuffer after SwapBuffers, but we need to invoke
648 * st_manager_validate_framebuffers to notice that.
650 * Set gfx_shaders_may_be_dirty to invoke st_validate_state in the next
651 * draw call, which will invoke st_manager_validate_framebuffers, but it
652 * won't dirty states if there is no change.
654 if (flags
& ST_FLUSH_END_OF_FRAME
)
655 st
->gfx_shaders_may_be_dirty
= true;
659 st_context_teximage(struct st_context_iface
*stctxi
,
660 enum st_texture_type tex_type
,
661 int level
, enum pipe_format pipe_format
,
662 struct pipe_resource
*tex
, boolean mipmap
)
664 struct st_context
*st
= (struct st_context
*) stctxi
;
665 struct gl_context
*ctx
= st
->ctx
;
666 struct gl_texture_object
*texObj
;
667 struct gl_texture_image
*texImage
;
668 struct st_texture_object
*stObj
;
669 struct st_texture_image
*stImage
;
670 GLenum internalFormat
;
671 GLuint width
, height
, depth
;
676 target
= GL_TEXTURE_1D
;
679 target
= GL_TEXTURE_2D
;
682 target
= GL_TEXTURE_3D
;
684 case ST_TEXTURE_RECT
:
685 target
= GL_TEXTURE_RECTANGLE_ARB
;
691 texObj
= _mesa_get_current_tex_object(ctx
, target
);
693 _mesa_lock_texture(ctx
, texObj
);
695 stObj
= st_texture_object(texObj
);
696 /* switch to surface based */
697 if (!stObj
->surface_based
) {
698 _mesa_clear_texture_object(ctx
, texObj
);
699 stObj
->surface_based
= GL_TRUE
;
702 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
703 stImage
= st_texture_image(texImage
);
705 mesa_format texFormat
= st_pipe_format_to_mesa_format(pipe_format
);
707 if (util_format_has_alpha(tex
->format
))
708 internalFormat
= GL_RGBA
;
710 internalFormat
= GL_RGB
;
712 _mesa_init_teximage_fields(ctx
, texImage
,
713 tex
->width0
, tex
->height0
, 1, 0,
714 internalFormat
, texFormat
);
717 height
= tex
->height0
;
720 /* grow the image size until we hit level = 0 */
732 _mesa_clear_texture_image(ctx
, texImage
);
733 width
= height
= depth
= 0;
736 pipe_resource_reference(&stImage
->pt
, tex
);
737 stObj
->surface_format
= pipe_format
;
739 st_texture_release_all_sampler_views(st
, stObj
);
740 stObj
->needs_validation
= true;
742 _mesa_dirty_texobj(ctx
, texObj
);
743 _mesa_unlock_texture(ctx
, texObj
);
749 st_context_copy(struct st_context_iface
*stctxi
,
750 struct st_context_iface
*stsrci
, unsigned mask
)
752 struct st_context
*st
= (struct st_context
*) stctxi
;
753 struct st_context
*src
= (struct st_context
*) stsrci
;
755 _mesa_copy_context(src
->ctx
, st
->ctx
, mask
);
759 st_context_share(struct st_context_iface
*stctxi
,
760 struct st_context_iface
*stsrci
)
762 struct st_context
*st
= (struct st_context
*) stctxi
;
763 struct st_context
*src
= (struct st_context
*) stsrci
;
765 return _mesa_share_state(st
->ctx
, src
->ctx
);
769 st_context_destroy(struct st_context_iface
*stctxi
)
771 struct st_context
*st
= (struct st_context
*) stctxi
;
772 st_destroy_context(st
);
776 st_start_thread(struct st_context_iface
*stctxi
)
778 struct st_context
*st
= (struct st_context
*) stctxi
;
780 _mesa_glthread_init(st
->ctx
);
784 st_thread_finish(struct st_context_iface
*stctxi
)
786 struct st_context
*st
= (struct st_context
*) stctxi
;
788 _mesa_glthread_finish(st
->ctx
);
791 static struct st_context_iface
*
792 st_api_create_context(struct st_api
*stapi
, struct st_manager
*smapi
,
793 const struct st_context_attribs
*attribs
,
794 enum st_context_error
*error
,
795 struct st_context_iface
*shared_stctxi
)
797 struct st_context
*shared_ctx
= (struct st_context
*) shared_stctxi
;
798 struct st_context
*st
;
799 struct pipe_context
*pipe
;
800 struct gl_config mode
;
802 bool no_error
= false;
803 unsigned ctx_flags
= PIPE_CONTEXT_PREFER_THREADED
;
805 if (!(stapi
->profile_mask
& (1 << attribs
->profile
)))
808 switch (attribs
->profile
) {
809 case ST_PROFILE_DEFAULT
:
810 api
= API_OPENGL_COMPAT
;
812 case ST_PROFILE_OPENGL_ES1
:
815 case ST_PROFILE_OPENGL_ES2
:
818 case ST_PROFILE_OPENGL_CORE
:
819 api
= API_OPENGL_CORE
;
822 *error
= ST_CONTEXT_ERROR_BAD_API
;
826 /* Create a hash table for the framebuffer interface objects
827 * if it has not been created for this st manager.
829 if (smapi
->st_manager_private
== NULL
) {
830 struct st_manager_private
*smPriv
;
832 smPriv
= CALLOC_STRUCT(st_manager_private
);
833 mtx_init(&smPriv
->st_mutex
, mtx_plain
);
834 smPriv
->stfbi_ht
= _mesa_hash_table_create(NULL
,
835 st_framebuffer_iface_hash
,
836 st_framebuffer_iface_equal
);
837 smapi
->st_manager_private
= smPriv
;
838 smapi
->destroy
= st_manager_destroy
;
841 if (attribs
->flags
& ST_CONTEXT_FLAG_ROBUST_ACCESS
)
842 ctx_flags
|= PIPE_CONTEXT_ROBUST_BUFFER_ACCESS
;
844 if (attribs
->flags
& ST_CONTEXT_FLAG_NO_ERROR
)
847 pipe
= smapi
->screen
->context_create(smapi
->screen
, NULL
, ctx_flags
);
849 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
853 st_visual_to_context_mode(&attribs
->visual
, &mode
);
854 st
= st_create_context(api
, pipe
, &mode
, shared_ctx
, &attribs
->options
, no_error
);
856 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
861 if (attribs
->flags
& ST_CONTEXT_FLAG_DEBUG
) {
862 if (!_mesa_set_debug_state_int(st
->ctx
, GL_DEBUG_OUTPUT
, GL_TRUE
)) {
863 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
867 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_DEBUG_BIT
;
870 if (st
->ctx
->Const
.ContextFlags
& GL_CONTEXT_FLAG_DEBUG_BIT
) {
871 st_update_debug_callback(st
);
874 if (attribs
->flags
& ST_CONTEXT_FLAG_FORWARD_COMPATIBLE
)
875 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT
;
876 if (attribs
->flags
& ST_CONTEXT_FLAG_ROBUST_ACCESS
) {
877 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB
;
878 st
->ctx
->Const
.RobustAccess
= GL_TRUE
;
880 if (attribs
->flags
& ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED
) {
881 st
->ctx
->Const
.ResetStrategy
= GL_LOSE_CONTEXT_ON_RESET_ARB
;
882 st_install_device_reset_callback(st
);
885 /* need to perform version check */
886 if (attribs
->major
> 1 || attribs
->minor
> 0) {
887 /* Is the actual version less than the requested version?
889 if (st
->ctx
->Version
< attribs
->major
* 10U + attribs
->minor
) {
890 *error
= ST_CONTEXT_ERROR_BAD_VERSION
;
891 st_destroy_context(st
);
896 st
->invalidate_on_gl_viewport
=
897 smapi
->get_param(smapi
, ST_MANAGER_BROKEN_INVALIDATE
);
899 st
->iface
.destroy
= st_context_destroy
;
900 st
->iface
.flush
= st_context_flush
;
901 st
->iface
.teximage
= st_context_teximage
;
902 st
->iface
.copy
= st_context_copy
;
903 st
->iface
.share
= st_context_share
;
904 st
->iface
.start_thread
= st_start_thread
;
905 st
->iface
.thread_finish
= st_thread_finish
;
906 st
->iface
.st_context_private
= (void *) smapi
;
907 st
->iface
.cso_context
= st
->cso_context
;
908 st
->iface
.pipe
= st
->pipe
;
909 st
->iface
.state_manager
= smapi
;
911 *error
= ST_CONTEXT_SUCCESS
;
915 static struct st_context_iface
*
916 st_api_get_current(struct st_api
*stapi
)
918 GET_CURRENT_CONTEXT(ctx
);
919 struct st_context
*st
= (ctx
) ? ctx
->st
: NULL
;
921 return (st
) ? &st
->iface
: NULL
;
924 static struct st_framebuffer
*
925 st_framebuffer_reuse_or_create(struct st_context
*st
,
926 struct gl_framebuffer
*fb
,
927 struct st_framebuffer_iface
*stfbi
)
929 struct st_framebuffer
*cur
= NULL
, *stfb
= NULL
;
934 /* Check if there is already a framebuffer object for the specified
935 * framebuffer interface in this context. If there is one, use it.
937 LIST_FOR_EACH_ENTRY(cur
, &st
->winsys_buffers
, head
) {
938 if (cur
->iface_ID
== stfbi
->ID
) {
939 st_framebuffer_reference(&stfb
, cur
);
944 /* If there is not already a framebuffer object, create one */
946 cur
= st_framebuffer_create(st
, stfbi
);
949 /* add the referenced framebuffer interface object to
950 * the framebuffer interface object hash table.
952 if (!st_framebuffer_iface_insert(stfbi
->state_manager
, stfbi
)) {
953 st_framebuffer_reference(&cur
, NULL
);
957 /* add to the context's winsys buffers list */
958 LIST_ADD(&cur
->head
, &st
->winsys_buffers
);
960 st_framebuffer_reference(&stfb
, cur
);
968 st_api_make_current(struct st_api
*stapi
, struct st_context_iface
*stctxi
,
969 struct st_framebuffer_iface
*stdrawi
,
970 struct st_framebuffer_iface
*streadi
)
972 struct st_context
*st
= (struct st_context
*) stctxi
;
973 struct st_framebuffer
*stdraw
, *stread
;
976 _glapi_check_multithread();
979 /* reuse or create the draw fb */
980 stdraw
= st_framebuffer_reuse_or_create(st
,
981 st
->ctx
->WinSysDrawBuffer
, stdrawi
);
982 if (streadi
!= stdrawi
) {
983 /* do the same for the read fb */
984 stread
= st_framebuffer_reuse_or_create(st
,
985 st
->ctx
->WinSysReadBuffer
, streadi
);
989 /* reuse the draw fb for the read fb */
991 st_framebuffer_reference(&stread
, stdraw
);
994 if (stdraw
&& stread
) {
995 st_framebuffer_validate(stdraw
, st
);
996 if (stread
!= stdraw
)
997 st_framebuffer_validate(stread
, st
);
999 ret
= _mesa_make_current(st
->ctx
, &stdraw
->Base
, &stread
->Base
);
1001 st
->draw_stamp
= stdraw
->stamp
- 1;
1002 st
->read_stamp
= stread
->stamp
- 1;
1003 st_context_validate(st
, stdraw
, stread
);
1006 struct gl_framebuffer
*incomplete
= _mesa_get_incomplete_framebuffer();
1007 ret
= _mesa_make_current(st
->ctx
, incomplete
, incomplete
);
1010 st_framebuffer_reference(&stdraw
, NULL
);
1011 st_framebuffer_reference(&stread
, NULL
);
1013 /* Purge the context's winsys_buffers list in case any
1014 * of the referenced drawables no longer exist.
1016 st_framebuffers_purge(st
);
1019 ret
= _mesa_make_current(NULL
, NULL
, NULL
);
1026 st_api_destroy(struct st_api
*stapi
)
1031 * Flush the front buffer if the current context renders to the front buffer.
1034 st_manager_flush_frontbuffer(struct st_context
*st
)
1036 struct st_framebuffer
*stfb
= st_ws_framebuffer(st
->ctx
->DrawBuffer
);
1037 struct st_renderbuffer
*strb
= NULL
;
1040 strb
= st_renderbuffer(stfb
->Base
.Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
1044 /* never a dummy fb */
1045 stfb
->iface
->flush_front(&st
->iface
, stfb
->iface
, ST_ATTACHMENT_FRONT_LEFT
);
1049 * Re-validate the framebuffers.
1052 st_manager_validate_framebuffers(struct st_context
*st
)
1054 struct st_framebuffer
*stdraw
= st_ws_framebuffer(st
->ctx
->DrawBuffer
);
1055 struct st_framebuffer
*stread
= st_ws_framebuffer(st
->ctx
->ReadBuffer
);
1058 st_framebuffer_validate(stdraw
, st
);
1059 if (stread
&& stread
!= stdraw
)
1060 st_framebuffer_validate(stread
, st
);
1062 st_context_validate(st
, stdraw
, stread
);
1066 * Add a color renderbuffer on demand. The FBO must correspond to a window,
1067 * not a user-created FBO.
1070 st_manager_add_color_renderbuffer(struct st_context
*st
,
1071 struct gl_framebuffer
*fb
,
1072 gl_buffer_index idx
)
1074 struct st_framebuffer
*stfb
= st_ws_framebuffer(fb
);
1080 assert(_mesa_is_winsys_fbo(fb
));
1082 if (stfb
->Base
.Attachment
[idx
].Renderbuffer
)
1086 case BUFFER_FRONT_LEFT
:
1087 case BUFFER_BACK_LEFT
:
1088 case BUFFER_FRONT_RIGHT
:
1089 case BUFFER_BACK_RIGHT
:
1095 if (!st_framebuffer_add_renderbuffer(stfb
, idx
))
1098 st_framebuffer_update_attachments(stfb
);
1101 * Force a call to the state tracker manager to validate the
1102 * new renderbuffer. It might be that there is a window system
1103 * renderbuffer available.
1106 stfb
->iface_stamp
= p_atomic_read(&stfb
->iface
->stamp
) - 1;
1108 st_invalidate_buffers(st
);
1114 st_manager_destroy(struct st_manager
*smapi
)
1116 struct st_manager_private
*smPriv
= smapi
->st_manager_private
;
1118 if (smPriv
&& smPriv
->stfbi_ht
) {
1119 _mesa_hash_table_destroy(smPriv
->stfbi_ht
, NULL
);
1120 mtx_destroy(&smPriv
->st_mutex
);
1122 smapi
->st_manager_private
= NULL
;
1127 get_version(struct pipe_screen
*screen
,
1128 struct st_config_options
*options
, gl_api api
)
1130 struct gl_constants consts
= {0};
1131 struct gl_extensions extensions
= {0};
1134 if (_mesa_override_gl_version_contextless(&consts
, &api
, &version
)) {
1138 _mesa_init_constants(&consts
, api
);
1139 _mesa_init_extensions(&extensions
);
1141 st_init_limits(screen
, &consts
, &extensions
);
1142 st_init_extensions(screen
, &consts
, &extensions
, options
, GL_TRUE
);
1144 return _mesa_get_version(&extensions
, &consts
, api
);
1148 st_api_query_versions(struct st_api
*stapi
, struct st_manager
*sm
,
1149 struct st_config_options
*options
,
1150 int *gl_core_version
,
1151 int *gl_compat_version
,
1152 int *gl_es1_version
,
1153 int *gl_es2_version
)
1155 *gl_core_version
= get_version(sm
->screen
, options
, API_OPENGL_CORE
);
1156 *gl_compat_version
= get_version(sm
->screen
, options
, API_OPENGL_COMPAT
);
1157 *gl_es1_version
= get_version(sm
->screen
, options
, API_OPENGLES
);
1158 *gl_es2_version
= get_version(sm
->screen
, options
, API_OPENGLES2
);
1161 static const struct st_api st_gl_api
= {
1162 .name
= "Mesa " PACKAGE_VERSION
,
1163 .api
= ST_API_OPENGL
,
1164 .profile_mask
= ST_PROFILE_DEFAULT_MASK
|
1165 ST_PROFILE_OPENGL_CORE_MASK
|
1166 ST_PROFILE_OPENGL_ES1_MASK
|
1167 ST_PROFILE_OPENGL_ES2_MASK
|
1169 .feature_mask
= ST_API_FEATURE_MS_VISUALS_MASK
,
1170 .destroy
= st_api_destroy
,
1171 .query_versions
= st_api_query_versions
,
1172 .create_context
= st_api_create_context
,
1173 .make_current
= st_api_make_current
,
1174 .get_current
= st_api_get_current
,
1175 .destroy_drawable
= st_api_destroy_drawable
,
1179 st_gl_api_create(void)
1181 return (struct st_api
*) &st_gl_api
;