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_helpers.h"
60 #include "util/u_pointer.h"
61 #include "util/u_inlines.h"
62 #include "util/u_atomic.h"
63 #include "util/u_surface.h"
64 #include "util/list.h"
67 struct st_manager_private
69 struct hash_table
*stfbi_ht
; /* framebuffer iface objects hash table */
75 * Map an attachment to a buffer index.
77 static inline gl_buffer_index
78 attachment_to_buffer_index(enum st_attachment_type statt
)
80 gl_buffer_index index
;
83 case ST_ATTACHMENT_FRONT_LEFT
:
84 index
= BUFFER_FRONT_LEFT
;
86 case ST_ATTACHMENT_BACK_LEFT
:
87 index
= BUFFER_BACK_LEFT
;
89 case ST_ATTACHMENT_FRONT_RIGHT
:
90 index
= BUFFER_FRONT_RIGHT
;
92 case ST_ATTACHMENT_BACK_RIGHT
:
93 index
= BUFFER_BACK_RIGHT
;
95 case ST_ATTACHMENT_DEPTH_STENCIL
:
98 case ST_ATTACHMENT_ACCUM
:
101 case ST_ATTACHMENT_SAMPLE
:
103 index
= BUFFER_COUNT
;
112 * Map a buffer index to an attachment.
114 static inline enum st_attachment_type
115 buffer_index_to_attachment(gl_buffer_index index
)
117 enum st_attachment_type statt
;
120 case BUFFER_FRONT_LEFT
:
121 statt
= ST_ATTACHMENT_FRONT_LEFT
;
123 case BUFFER_BACK_LEFT
:
124 statt
= ST_ATTACHMENT_BACK_LEFT
;
126 case BUFFER_FRONT_RIGHT
:
127 statt
= ST_ATTACHMENT_FRONT_RIGHT
;
129 case BUFFER_BACK_RIGHT
:
130 statt
= ST_ATTACHMENT_BACK_RIGHT
;
133 statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
136 statt
= ST_ATTACHMENT_ACCUM
;
139 statt
= ST_ATTACHMENT_INVALID
;
148 * Make sure a context picks up the latest cached state of the
149 * drawables it binds to.
152 st_context_validate(struct st_context
*st
,
153 struct st_framebuffer
*stdraw
,
154 struct st_framebuffer
*stread
)
156 if (stdraw
&& stdraw
->stamp
!= st
->draw_stamp
) {
157 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
158 _mesa_resize_framebuffer(st
->ctx
, &stdraw
->Base
,
160 stdraw
->Base
.Height
);
161 st
->draw_stamp
= stdraw
->stamp
;
164 if (stread
&& stread
->stamp
!= st
->read_stamp
) {
165 if (stread
!= stdraw
) {
166 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
167 _mesa_resize_framebuffer(st
->ctx
, &stread
->Base
,
169 stread
->Base
.Height
);
171 st
->read_stamp
= stread
->stamp
;
177 * Validate a framebuffer to make sure up-to-date pipe_textures are used.
178 * The context is only used for creating pipe surfaces and for calling
179 * _mesa_resize_framebuffer().
180 * (That should probably be rethought, since those surfaces become
181 * drawable state, not context state, and can be freed by another pipe
185 st_framebuffer_validate(struct st_framebuffer
*stfb
,
186 struct st_context
*st
)
188 struct pipe_resource
*textures
[ST_ATTACHMENT_COUNT
];
191 boolean changed
= FALSE
;
194 new_stamp
= p_atomic_read(&stfb
->iface
->stamp
);
195 if (stfb
->iface_stamp
== new_stamp
)
198 memset(textures
, 0, stfb
->num_statts
* sizeof(textures
[0]));
200 /* validate the fb */
202 if (!stfb
->iface
->validate(&st
->iface
, stfb
->iface
, stfb
->statts
,
203 stfb
->num_statts
, textures
))
206 stfb
->iface_stamp
= new_stamp
;
207 new_stamp
= p_atomic_read(&stfb
->iface
->stamp
);
208 } while(stfb
->iface_stamp
!= new_stamp
);
210 width
= stfb
->Base
.Width
;
211 height
= stfb
->Base
.Height
;
213 for (i
= 0; i
< stfb
->num_statts
; i
++) {
214 struct st_renderbuffer
*strb
;
215 struct pipe_surface
*ps
, surf_tmpl
;
221 idx
= attachment_to_buffer_index(stfb
->statts
[i
]);
222 if (idx
>= BUFFER_COUNT
) {
223 pipe_resource_reference(&textures
[i
], NULL
);
227 strb
= st_renderbuffer(stfb
->Base
.Attachment
[idx
].Renderbuffer
);
229 if (strb
->texture
== textures
[i
]) {
230 pipe_resource_reference(&textures
[i
], NULL
);
234 u_surface_default_template(&surf_tmpl
, textures
[i
]);
235 ps
= st
->pipe
->create_surface(st
->pipe
, textures
[i
], &surf_tmpl
);
237 struct pipe_surface
**psurf
=
238 util_format_is_srgb(ps
->format
) ? &strb
->surface_srgb
:
239 &strb
->surface_linear
;
241 pipe_surface_reference(psurf
, ps
);
242 strb
->surface
= *psurf
;
243 pipe_resource_reference(&strb
->texture
, ps
->texture
);
244 /* ownership transfered */
245 pipe_surface_reference(&ps
, NULL
);
249 strb
->Base
.Width
= strb
->surface
->width
;
250 strb
->Base
.Height
= strb
->surface
->height
;
252 width
= strb
->Base
.Width
;
253 height
= strb
->Base
.Height
;
256 pipe_resource_reference(&textures
[i
], NULL
);
261 _mesa_resize_framebuffer(st
->ctx
, &stfb
->Base
, width
, height
);
267 * Update the attachments to validate by looping the existing renderbuffers.
270 st_framebuffer_update_attachments(struct st_framebuffer
*stfb
)
274 stfb
->num_statts
= 0;
275 for (idx
= 0; idx
< BUFFER_COUNT
; idx
++) {
276 struct st_renderbuffer
*strb
;
277 enum st_attachment_type statt
;
279 strb
= st_renderbuffer(stfb
->Base
.Attachment
[idx
].Renderbuffer
);
280 if (!strb
|| strb
->software
)
283 statt
= buffer_index_to_attachment(idx
);
284 if (statt
!= ST_ATTACHMENT_INVALID
&&
285 st_visual_have_buffers(stfb
->iface
->visual
, 1 << statt
))
286 stfb
->statts
[stfb
->num_statts
++] = statt
;
293 * Add a renderbuffer to the framebuffer. The framebuffer is one that
294 * corresponds to a window and is not a user-created FBO.
297 st_framebuffer_add_renderbuffer(struct st_framebuffer
*stfb
,
300 struct gl_renderbuffer
*rb
;
301 enum pipe_format format
;
304 assert(_mesa_is_winsys_fbo(&stfb
->Base
));
306 /* do not distinguish depth/stencil buffers */
307 if (idx
== BUFFER_STENCIL
)
312 format
= stfb
->iface
->visual
->depth_stencil_format
;
316 format
= stfb
->iface
->visual
->accum_format
;
320 format
= stfb
->iface
->visual
->color_format
;
321 if (stfb
->Base
.Visual
.sRGBCapable
)
322 format
= util_format_srgb(format
);
327 if (format
== PIPE_FORMAT_NONE
)
330 rb
= st_new_renderbuffer_fb(format
, stfb
->iface
->visual
->samples
, sw
);
334 if (idx
!= BUFFER_DEPTH
) {
335 _mesa_attach_and_own_rb(&stfb
->Base
, idx
, rb
);
339 bool rb_ownership_taken
= false;
340 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_ZS
, 0)) {
341 _mesa_attach_and_own_rb(&stfb
->Base
, BUFFER_DEPTH
, rb
);
342 rb_ownership_taken
= true;
345 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_ZS
, 1)) {
346 if (rb_ownership_taken
)
347 _mesa_attach_and_reference_rb(&stfb
->Base
, BUFFER_STENCIL
, rb
);
349 _mesa_attach_and_own_rb(&stfb
->Base
, BUFFER_STENCIL
, rb
);
357 * Intialize a struct gl_config from a visual.
360 st_visual_to_context_mode(const struct st_visual
*visual
,
361 struct gl_config
*mode
)
363 memset(mode
, 0, sizeof(*mode
));
365 if (st_visual_have_buffers(visual
, ST_ATTACHMENT_BACK_LEFT_MASK
))
366 mode
->doubleBufferMode
= GL_TRUE
;
368 if (st_visual_have_buffers(visual
,
369 ST_ATTACHMENT_FRONT_RIGHT_MASK
| ST_ATTACHMENT_BACK_RIGHT_MASK
))
370 mode
->stereoMode
= GL_TRUE
;
372 if (visual
->color_format
!= PIPE_FORMAT_NONE
) {
373 mode
->rgbMode
= GL_TRUE
;
376 util_format_get_component_bits(visual
->color_format
,
377 UTIL_FORMAT_COLORSPACE_RGB
, 0);
379 util_format_get_component_bits(visual
->color_format
,
380 UTIL_FORMAT_COLORSPACE_RGB
, 1);
382 util_format_get_component_bits(visual
->color_format
,
383 UTIL_FORMAT_COLORSPACE_RGB
, 2);
385 util_format_get_component_bits(visual
->color_format
,
386 UTIL_FORMAT_COLORSPACE_RGB
, 3);
388 mode
->rgbBits
= mode
->redBits
+
389 mode
->greenBits
+ mode
->blueBits
+ mode
->alphaBits
;
390 mode
->sRGBCapable
= util_format_is_srgb(visual
->color_format
);
393 if (visual
->depth_stencil_format
!= PIPE_FORMAT_NONE
) {
395 util_format_get_component_bits(visual
->depth_stencil_format
,
396 UTIL_FORMAT_COLORSPACE_ZS
, 0);
398 util_format_get_component_bits(visual
->depth_stencil_format
,
399 UTIL_FORMAT_COLORSPACE_ZS
, 1);
401 mode
->haveDepthBuffer
= mode
->depthBits
> 0;
402 mode
->haveStencilBuffer
= mode
->stencilBits
> 0;
405 if (visual
->accum_format
!= PIPE_FORMAT_NONE
) {
406 mode
->haveAccumBuffer
= GL_TRUE
;
409 util_format_get_component_bits(visual
->accum_format
,
410 UTIL_FORMAT_COLORSPACE_RGB
, 0);
411 mode
->accumGreenBits
=
412 util_format_get_component_bits(visual
->accum_format
,
413 UTIL_FORMAT_COLORSPACE_RGB
, 1);
414 mode
->accumBlueBits
=
415 util_format_get_component_bits(visual
->accum_format
,
416 UTIL_FORMAT_COLORSPACE_RGB
, 2);
417 mode
->accumAlphaBits
=
418 util_format_get_component_bits(visual
->accum_format
,
419 UTIL_FORMAT_COLORSPACE_RGB
, 3);
422 if (visual
->samples
> 1) {
423 mode
->sampleBuffers
= 1;
424 mode
->samples
= visual
->samples
;
430 * Create a framebuffer from a manager interface.
432 static struct st_framebuffer
*
433 st_framebuffer_create(struct st_context
*st
,
434 struct st_framebuffer_iface
*stfbi
)
436 struct st_framebuffer
*stfb
;
437 struct gl_config mode
;
443 stfb
= CALLOC_STRUCT(st_framebuffer
);
447 st_visual_to_context_mode(stfbi
->visual
, &mode
);
450 * For desktop GL, sRGB framebuffer write is controlled by both the
451 * capability of the framebuffer and GL_FRAMEBUFFER_SRGB. We should
452 * advertise the capability when the pipe driver (and core Mesa) supports
453 * it so that applications can enable sRGB write when they want to.
455 * This is not to be confused with GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB. When
456 * the attribute is GLX_TRUE, it tells the st manager to pick a color
457 * format such that util_format_srgb(visual->color_format) can be supported
458 * by the pipe driver. We still need to advertise the capability here.
460 * For GLES, however, sRGB framebuffer write is controlled only by the
461 * capability of the framebuffer. There is GL_EXT_sRGB_write_control to
462 * give applications the control back, but sRGB write is still enabled by
463 * default. To avoid unexpected results, we should not advertise the
464 * capability. This could change when we add support for
465 * EGL_KHR_gl_colorspace.
467 if (_mesa_is_desktop_gl(st
->ctx
)) {
468 struct pipe_screen
*screen
= st
->pipe
->screen
;
469 const enum pipe_format srgb_format
=
470 util_format_srgb(stfbi
->visual
->color_format
);
472 if (srgb_format
!= PIPE_FORMAT_NONE
&&
473 st_pipe_format_to_mesa_format(srgb_format
) != MESA_FORMAT_NONE
&&
474 screen
->is_format_supported(screen
, srgb_format
,
475 PIPE_TEXTURE_2D
, stfbi
->visual
->samples
,
476 stfbi
->visual
->samples
,
477 (PIPE_BIND_DISPLAY_TARGET
|
478 PIPE_BIND_RENDER_TARGET
)))
479 mode
.sRGBCapable
= GL_TRUE
;
482 _mesa_initialize_window_framebuffer(&stfb
->Base
, &mode
);
485 stfb
->iface_ID
= stfbi
->ID
;
486 stfb
->iface_stamp
= p_atomic_read(&stfbi
->stamp
) - 1;
488 /* add the color buffer */
489 idx
= stfb
->Base
._ColorDrawBufferIndexes
[0];
490 if (!st_framebuffer_add_renderbuffer(stfb
, idx
)) {
495 st_framebuffer_add_renderbuffer(stfb
, BUFFER_DEPTH
);
496 st_framebuffer_add_renderbuffer(stfb
, BUFFER_ACCUM
);
499 st_framebuffer_update_attachments(stfb
);
506 * Reference a framebuffer.
509 st_framebuffer_reference(struct st_framebuffer
**ptr
,
510 struct st_framebuffer
*stfb
)
512 struct gl_framebuffer
*fb
= &stfb
->Base
;
513 _mesa_reference_framebuffer((struct gl_framebuffer
**) ptr
, fb
);
518 st_framebuffer_iface_hash(const void *key
)
520 return (uintptr_t)key
;
525 st_framebuffer_iface_equal(const void *a
, const void *b
)
527 return (struct st_framebuffer_iface
*)a
== (struct st_framebuffer_iface
*)b
;
532 st_framebuffer_iface_lookup(struct st_manager
*smapi
,
533 const struct st_framebuffer_iface
*stfbi
)
535 struct st_manager_private
*smPriv
=
536 (struct st_manager_private
*)smapi
->st_manager_private
;
537 struct hash_entry
*entry
;
540 assert(smPriv
->stfbi_ht
);
542 mtx_lock(&smPriv
->st_mutex
);
543 entry
= _mesa_hash_table_search(smPriv
->stfbi_ht
, stfbi
);
544 mtx_unlock(&smPriv
->st_mutex
);
546 return entry
!= NULL
;
551 st_framebuffer_iface_insert(struct st_manager
*smapi
,
552 struct st_framebuffer_iface
*stfbi
)
554 struct st_manager_private
*smPriv
=
555 (struct st_manager_private
*)smapi
->st_manager_private
;
556 struct hash_entry
*entry
;
559 assert(smPriv
->stfbi_ht
);
561 mtx_lock(&smPriv
->st_mutex
);
562 entry
= _mesa_hash_table_insert(smPriv
->stfbi_ht
, stfbi
, stfbi
);
563 mtx_unlock(&smPriv
->st_mutex
);
565 return entry
!= NULL
;
570 st_framebuffer_iface_remove(struct st_manager
*smapi
,
571 struct st_framebuffer_iface
*stfbi
)
573 struct st_manager_private
*smPriv
=
574 (struct st_manager_private
*)smapi
->st_manager_private
;
575 struct hash_entry
*entry
;
577 if (!smPriv
|| !smPriv
->stfbi_ht
)
580 mtx_lock(&smPriv
->st_mutex
);
581 entry
= _mesa_hash_table_search(smPriv
->stfbi_ht
, stfbi
);
585 _mesa_hash_table_remove(smPriv
->stfbi_ht
, entry
);
588 mtx_unlock(&smPriv
->st_mutex
);
593 * The framebuffer interface object is no longer valid.
594 * Remove the object from the framebuffer interface hash table.
597 st_api_destroy_drawable(struct st_api
*stapi
,
598 struct st_framebuffer_iface
*stfbi
)
603 st_framebuffer_iface_remove(stfbi
->state_manager
, stfbi
);
608 * Purge the winsys buffers list to remove any references to
609 * non-existing framebuffer interface objects.
612 st_framebuffers_purge(struct st_context
*st
)
614 struct st_context_iface
*st_iface
= &st
->iface
;
615 struct st_manager
*smapi
= st_iface
->state_manager
;
616 struct st_framebuffer
*stfb
, *next
;
620 LIST_FOR_EACH_ENTRY_SAFE_REV(stfb
, next
, &st
->winsys_buffers
, head
) {
621 struct st_framebuffer_iface
*stfbi
= stfb
->iface
;
626 * If the corresponding framebuffer interface object no longer exists,
627 * remove the framebuffer object from the context's winsys buffers list,
628 * and unreference the framebuffer object, so its resources can be
631 if (!st_framebuffer_iface_lookup(smapi
, stfbi
)) {
632 LIST_DEL(&stfb
->head
);
633 st_framebuffer_reference(&stfb
, NULL
);
640 st_context_flush(struct st_context_iface
*stctxi
, unsigned flags
,
641 struct pipe_fence_handle
**fence
)
643 struct st_context
*st
= (struct st_context
*) stctxi
;
644 unsigned pipe_flags
= 0;
646 if (flags
& ST_FLUSH_END_OF_FRAME
)
647 pipe_flags
|= PIPE_FLUSH_END_OF_FRAME
;
648 if (flags
& ST_FLUSH_FENCE_FD
)
649 pipe_flags
|= PIPE_FLUSH_FENCE_FD
;
651 FLUSH_VERTICES(st
->ctx
, 0);
652 FLUSH_CURRENT(st
->ctx
, 0);
653 st_flush(st
, fence
, pipe_flags
);
655 if ((flags
& ST_FLUSH_WAIT
) && fence
&& *fence
) {
656 st
->pipe
->screen
->fence_finish(st
->pipe
->screen
, NULL
, *fence
,
657 PIPE_TIMEOUT_INFINITE
);
658 st
->pipe
->screen
->fence_reference(st
->pipe
->screen
, fence
, NULL
);
661 if (flags
& ST_FLUSH_FRONT
)
662 st_manager_flush_frontbuffer(st
);
664 /* DRI3 changes the framebuffer after SwapBuffers, but we need to invoke
665 * st_manager_validate_framebuffers to notice that.
667 * Set gfx_shaders_may_be_dirty to invoke st_validate_state in the next
668 * draw call, which will invoke st_manager_validate_framebuffers, but it
669 * won't dirty states if there is no change.
671 if (flags
& ST_FLUSH_END_OF_FRAME
)
672 st
->gfx_shaders_may_be_dirty
= true;
676 st_context_teximage(struct st_context_iface
*stctxi
,
677 enum st_texture_type tex_type
,
678 int level
, enum pipe_format pipe_format
,
679 struct pipe_resource
*tex
, boolean mipmap
)
681 struct st_context
*st
= (struct st_context
*) stctxi
;
682 struct gl_context
*ctx
= st
->ctx
;
683 struct gl_texture_object
*texObj
;
684 struct gl_texture_image
*texImage
;
685 struct st_texture_object
*stObj
;
686 struct st_texture_image
*stImage
;
687 GLenum internalFormat
;
688 GLuint width
, height
, depth
;
693 target
= GL_TEXTURE_1D
;
696 target
= GL_TEXTURE_2D
;
699 target
= GL_TEXTURE_3D
;
701 case ST_TEXTURE_RECT
:
702 target
= GL_TEXTURE_RECTANGLE_ARB
;
708 texObj
= _mesa_get_current_tex_object(ctx
, target
);
710 _mesa_lock_texture(ctx
, texObj
);
712 stObj
= st_texture_object(texObj
);
713 /* switch to surface based */
714 if (!stObj
->surface_based
) {
715 _mesa_clear_texture_object(ctx
, texObj
, NULL
);
716 stObj
->surface_based
= GL_TRUE
;
719 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
720 stImage
= st_texture_image(texImage
);
722 mesa_format texFormat
= st_pipe_format_to_mesa_format(pipe_format
);
724 if (util_format_has_alpha(tex
->format
))
725 internalFormat
= GL_RGBA
;
727 internalFormat
= GL_RGB
;
729 _mesa_init_teximage_fields(ctx
, texImage
,
730 tex
->width0
, tex
->height0
, 1, 0,
731 internalFormat
, texFormat
);
734 height
= tex
->height0
;
737 /* grow the image size until we hit level = 0 */
749 _mesa_clear_texture_image(ctx
, texImage
);
750 width
= height
= depth
= 0;
753 pipe_resource_reference(&stObj
->pt
, tex
);
754 st_texture_release_all_sampler_views(st
, stObj
);
755 pipe_resource_reference(&stImage
->pt
, tex
);
756 stObj
->surface_format
= pipe_format
;
758 stObj
->needs_validation
= true;
760 _mesa_dirty_texobj(ctx
, texObj
);
761 _mesa_unlock_texture(ctx
, texObj
);
768 st_context_copy(struct st_context_iface
*stctxi
,
769 struct st_context_iface
*stsrci
, unsigned mask
)
771 struct st_context
*st
= (struct st_context
*) stctxi
;
772 struct st_context
*src
= (struct st_context
*) stsrci
;
774 _mesa_copy_context(src
->ctx
, st
->ctx
, mask
);
779 st_context_share(struct st_context_iface
*stctxi
,
780 struct st_context_iface
*stsrci
)
782 struct st_context
*st
= (struct st_context
*) stctxi
;
783 struct st_context
*src
= (struct st_context
*) stsrci
;
785 return _mesa_share_state(st
->ctx
, src
->ctx
);
790 st_context_destroy(struct st_context_iface
*stctxi
)
792 struct st_context
*st
= (struct st_context
*) stctxi
;
793 st_destroy_context(st
);
798 st_start_thread(struct st_context_iface
*stctxi
)
800 struct st_context
*st
= (struct st_context
*) stctxi
;
802 _mesa_glthread_init(st
->ctx
);
804 /* Pin all driver threads to one L3 cache for optimal performance
805 * on AMD Zen. This is only done if glthread is enabled.
807 * If glthread is disabled, st_draw.c re-pins driver threads regularly
808 * based on the location of the app thread.
810 struct glthread_state
*glthread
= st
->ctx
->GLThread
;
811 if (glthread
&& st
->pipe
->set_context_param
) {
812 util_pin_driver_threads_to_random_L3(st
->pipe
, &glthread
->queue
.threads
[0]);
818 st_thread_finish(struct st_context_iface
*stctxi
)
820 struct st_context
*st
= (struct st_context
*) stctxi
;
822 _mesa_glthread_finish(st
->ctx
);
827 st_manager_destroy(struct st_manager
*smapi
)
829 struct st_manager_private
*smPriv
= smapi
->st_manager_private
;
831 if (smPriv
&& smPriv
->stfbi_ht
) {
832 _mesa_hash_table_destroy(smPriv
->stfbi_ht
, NULL
);
833 mtx_destroy(&smPriv
->st_mutex
);
835 smapi
->st_manager_private
= NULL
;
840 static struct st_context_iface
*
841 st_api_create_context(struct st_api
*stapi
, struct st_manager
*smapi
,
842 const struct st_context_attribs
*attribs
,
843 enum st_context_error
*error
,
844 struct st_context_iface
*shared_stctxi
)
846 struct st_context
*shared_ctx
= (struct st_context
*) shared_stctxi
;
847 struct st_context
*st
;
848 struct pipe_context
*pipe
;
849 struct gl_config
* mode_ptr
;
850 struct gl_config mode
;
852 bool no_error
= false;
853 unsigned ctx_flags
= PIPE_CONTEXT_PREFER_THREADED
;
855 if (!(stapi
->profile_mask
& (1 << attribs
->profile
)))
858 switch (attribs
->profile
) {
859 case ST_PROFILE_DEFAULT
:
860 api
= API_OPENGL_COMPAT
;
862 case ST_PROFILE_OPENGL_ES1
:
865 case ST_PROFILE_OPENGL_ES2
:
868 case ST_PROFILE_OPENGL_CORE
:
869 api
= API_OPENGL_CORE
;
872 *error
= ST_CONTEXT_ERROR_BAD_API
;
876 /* Create a hash table for the framebuffer interface objects
877 * if it has not been created for this st manager.
879 if (smapi
->st_manager_private
== NULL
) {
880 struct st_manager_private
*smPriv
;
882 smPriv
= CALLOC_STRUCT(st_manager_private
);
883 mtx_init(&smPriv
->st_mutex
, mtx_plain
);
884 smPriv
->stfbi_ht
= _mesa_hash_table_create(NULL
,
885 st_framebuffer_iface_hash
,
886 st_framebuffer_iface_equal
);
887 smapi
->st_manager_private
= smPriv
;
888 smapi
->destroy
= st_manager_destroy
;
891 if (attribs
->flags
& ST_CONTEXT_FLAG_ROBUST_ACCESS
)
892 ctx_flags
|= PIPE_CONTEXT_ROBUST_BUFFER_ACCESS
;
894 if (attribs
->flags
& ST_CONTEXT_FLAG_NO_ERROR
)
897 if (attribs
->flags
& ST_CONTEXT_FLAG_LOW_PRIORITY
)
898 ctx_flags
|= PIPE_CONTEXT_LOW_PRIORITY
;
899 else if (attribs
->flags
& ST_CONTEXT_FLAG_HIGH_PRIORITY
)
900 ctx_flags
|= PIPE_CONTEXT_HIGH_PRIORITY
;
902 if (attribs
->flags
& ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED
)
903 ctx_flags
|= PIPE_CONTEXT_LOSE_CONTEXT_ON_RESET
;
905 pipe
= smapi
->screen
->context_create(smapi
->screen
, NULL
, ctx_flags
);
907 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
911 st_visual_to_context_mode(&attribs
->visual
, &mode
);
913 if (attribs
->visual
.no_config
)
918 st
= st_create_context(api
, pipe
, mode_ptr
, shared_ctx
,
919 &attribs
->options
, no_error
);
921 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
926 if (attribs
->flags
& ST_CONTEXT_FLAG_DEBUG
) {
927 if (!_mesa_set_debug_state_int(st
->ctx
, GL_DEBUG_OUTPUT
, GL_TRUE
)) {
928 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
932 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_DEBUG_BIT
;
935 if (st
->ctx
->Const
.ContextFlags
& GL_CONTEXT_FLAG_DEBUG_BIT
) {
936 st_update_debug_callback(st
);
939 if (attribs
->flags
& ST_CONTEXT_FLAG_FORWARD_COMPATIBLE
)
940 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT
;
941 if (attribs
->flags
& ST_CONTEXT_FLAG_ROBUST_ACCESS
) {
942 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB
;
943 st
->ctx
->Const
.RobustAccess
= GL_TRUE
;
945 if (attribs
->flags
& ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED
) {
946 st
->ctx
->Const
.ResetStrategy
= GL_LOSE_CONTEXT_ON_RESET_ARB
;
947 st_install_device_reset_callback(st
);
950 if (attribs
->flags
& ST_CONTEXT_FLAG_RELEASE_NONE
)
951 st
->ctx
->Const
.ContextReleaseBehavior
= GL_NONE
;
953 /* need to perform version check */
954 if (attribs
->major
> 1 || attribs
->minor
> 0) {
955 /* Is the actual version less than the requested version?
957 if (st
->ctx
->Version
< attribs
->major
* 10U + attribs
->minor
) {
958 *error
= ST_CONTEXT_ERROR_BAD_VERSION
;
959 st_destroy_context(st
);
964 st
->invalidate_on_gl_viewport
=
965 smapi
->get_param(smapi
, ST_MANAGER_BROKEN_INVALIDATE
);
967 st
->iface
.destroy
= st_context_destroy
;
968 st
->iface
.flush
= st_context_flush
;
969 st
->iface
.teximage
= st_context_teximage
;
970 st
->iface
.copy
= st_context_copy
;
971 st
->iface
.share
= st_context_share
;
972 st
->iface
.start_thread
= st_start_thread
;
973 st
->iface
.thread_finish
= st_thread_finish
;
974 st
->iface
.st_context_private
= (void *) smapi
;
975 st
->iface
.cso_context
= st
->cso_context
;
976 st
->iface
.pipe
= st
->pipe
;
977 st
->iface
.state_manager
= smapi
;
979 *error
= ST_CONTEXT_SUCCESS
;
984 static struct st_context_iface
*
985 st_api_get_current(struct st_api
*stapi
)
987 GET_CURRENT_CONTEXT(ctx
);
988 struct st_context
*st
= ctx
? ctx
->st
: NULL
;
990 return st
? &st
->iface
: NULL
;
994 static struct st_framebuffer
*
995 st_framebuffer_reuse_or_create(struct st_context
*st
,
996 struct gl_framebuffer
*fb
,
997 struct st_framebuffer_iface
*stfbi
)
999 struct st_framebuffer
*cur
= NULL
, *stfb
= NULL
;
1004 /* Check if there is already a framebuffer object for the specified
1005 * framebuffer interface in this context. If there is one, use it.
1007 LIST_FOR_EACH_ENTRY(cur
, &st
->winsys_buffers
, head
) {
1008 if (cur
->iface_ID
== stfbi
->ID
) {
1009 st_framebuffer_reference(&stfb
, cur
);
1014 /* If there is not already a framebuffer object, create one */
1016 cur
= st_framebuffer_create(st
, stfbi
);
1019 /* add the referenced framebuffer interface object to
1020 * the framebuffer interface object hash table.
1022 if (!st_framebuffer_iface_insert(stfbi
->state_manager
, stfbi
)) {
1023 st_framebuffer_reference(&cur
, NULL
);
1027 /* add to the context's winsys buffers list */
1028 LIST_ADD(&cur
->head
, &st
->winsys_buffers
);
1030 st_framebuffer_reference(&stfb
, cur
);
1039 st_api_make_current(struct st_api
*stapi
, struct st_context_iface
*stctxi
,
1040 struct st_framebuffer_iface
*stdrawi
,
1041 struct st_framebuffer_iface
*streadi
)
1043 struct st_context
*st
= (struct st_context
*) stctxi
;
1044 struct st_framebuffer
*stdraw
, *stread
;
1048 /* reuse or create the draw fb */
1049 stdraw
= st_framebuffer_reuse_or_create(st
,
1050 st
->ctx
->WinSysDrawBuffer
, stdrawi
);
1051 if (streadi
!= stdrawi
) {
1052 /* do the same for the read fb */
1053 stread
= st_framebuffer_reuse_or_create(st
,
1054 st
->ctx
->WinSysReadBuffer
, streadi
);
1058 /* reuse the draw fb for the read fb */
1060 st_framebuffer_reference(&stread
, stdraw
);
1063 if (stdraw
&& stread
) {
1064 st_framebuffer_validate(stdraw
, st
);
1065 if (stread
!= stdraw
)
1066 st_framebuffer_validate(stread
, st
);
1068 ret
= _mesa_make_current(st
->ctx
, &stdraw
->Base
, &stread
->Base
);
1070 st
->draw_stamp
= stdraw
->stamp
- 1;
1071 st
->read_stamp
= stread
->stamp
- 1;
1072 st_context_validate(st
, stdraw
, stread
);
1075 struct gl_framebuffer
*incomplete
= _mesa_get_incomplete_framebuffer();
1076 ret
= _mesa_make_current(st
->ctx
, incomplete
, incomplete
);
1079 st_framebuffer_reference(&stdraw
, NULL
);
1080 st_framebuffer_reference(&stread
, NULL
);
1082 /* Purge the context's winsys_buffers list in case any
1083 * of the referenced drawables no longer exist.
1085 st_framebuffers_purge(st
);
1088 ret
= _mesa_make_current(NULL
, NULL
, NULL
);
1096 st_api_destroy(struct st_api
*stapi
)
1102 * Flush the front buffer if the current context renders to the front buffer.
1105 st_manager_flush_frontbuffer(struct st_context
*st
)
1107 struct st_framebuffer
*stfb
= st_ws_framebuffer(st
->ctx
->DrawBuffer
);
1108 struct st_renderbuffer
*strb
= NULL
;
1111 strb
= st_renderbuffer(stfb
->Base
.Attachment
[BUFFER_FRONT_LEFT
].
1114 /* Do we have a front color buffer and has it been drawn to since last
1115 * frontbuffer flush?
1117 if (strb
&& strb
->defined
) {
1118 stfb
->iface
->flush_front(&st
->iface
, stfb
->iface
,
1119 ST_ATTACHMENT_FRONT_LEFT
);
1120 strb
->defined
= GL_FALSE
;
1122 /* Trigger an update of strb->defined on next draw */
1123 st
->dirty
|= ST_NEW_FB_STATE
;
1129 * Re-validate the framebuffers.
1132 st_manager_validate_framebuffers(struct st_context
*st
)
1134 struct st_framebuffer
*stdraw
= st_ws_framebuffer(st
->ctx
->DrawBuffer
);
1135 struct st_framebuffer
*stread
= st_ws_framebuffer(st
->ctx
->ReadBuffer
);
1138 st_framebuffer_validate(stdraw
, st
);
1139 if (stread
&& stread
!= stdraw
)
1140 st_framebuffer_validate(stread
, st
);
1142 st_context_validate(st
, stdraw
, stread
);
1147 * Flush any outstanding swapbuffers on the current draw framebuffer.
1150 st_manager_flush_swapbuffers(void)
1152 GET_CURRENT_CONTEXT(ctx
);
1153 struct st_context
*st
= (ctx
) ? ctx
->st
: NULL
;
1154 struct st_framebuffer
*stfb
;
1159 stfb
= st_ws_framebuffer(ctx
->DrawBuffer
);
1160 if (!stfb
|| !stfb
->iface
->flush_swapbuffers
)
1163 stfb
->iface
->flush_swapbuffers(&st
->iface
, stfb
->iface
);
1168 * Add a color renderbuffer on demand. The FBO must correspond to a window,
1169 * not a user-created FBO.
1172 st_manager_add_color_renderbuffer(struct st_context
*st
,
1173 struct gl_framebuffer
*fb
,
1174 gl_buffer_index idx
)
1176 struct st_framebuffer
*stfb
= st_ws_framebuffer(fb
);
1182 assert(_mesa_is_winsys_fbo(fb
));
1184 if (stfb
->Base
.Attachment
[idx
].Renderbuffer
)
1188 case BUFFER_FRONT_LEFT
:
1189 case BUFFER_BACK_LEFT
:
1190 case BUFFER_FRONT_RIGHT
:
1191 case BUFFER_BACK_RIGHT
:
1197 if (!st_framebuffer_add_renderbuffer(stfb
, idx
))
1200 st_framebuffer_update_attachments(stfb
);
1203 * Force a call to the state tracker manager to validate the
1204 * new renderbuffer. It might be that there is a window system
1205 * renderbuffer available.
1208 stfb
->iface_stamp
= p_atomic_read(&stfb
->iface
->stamp
) - 1;
1210 st_invalidate_buffers(st
);
1217 get_version(struct pipe_screen
*screen
,
1218 struct st_config_options
*options
, gl_api api
)
1220 struct gl_constants consts
= {0};
1221 struct gl_extensions extensions
= {0};
1224 if (_mesa_override_gl_version_contextless(&consts
, &api
, &version
)) {
1228 _mesa_init_constants(&consts
, api
);
1229 _mesa_init_extensions(&extensions
);
1231 st_init_limits(screen
, &consts
, &extensions
, api
);
1232 st_init_extensions(screen
, &consts
, &extensions
, options
, api
);
1234 return _mesa_get_version(&extensions
, &consts
, api
);
1239 st_api_query_versions(struct st_api
*stapi
, struct st_manager
*sm
,
1240 struct st_config_options
*options
,
1241 int *gl_core_version
,
1242 int *gl_compat_version
,
1243 int *gl_es1_version
,
1244 int *gl_es2_version
)
1246 *gl_core_version
= get_version(sm
->screen
, options
, API_OPENGL_CORE
);
1247 *gl_compat_version
= get_version(sm
->screen
, options
, API_OPENGL_COMPAT
);
1248 *gl_es1_version
= get_version(sm
->screen
, options
, API_OPENGLES
);
1249 *gl_es2_version
= get_version(sm
->screen
, options
, API_OPENGLES2
);
1253 static const struct st_api st_gl_api
= {
1254 .name
= "Mesa " PACKAGE_VERSION
,
1255 .api
= ST_API_OPENGL
,
1256 .profile_mask
= ST_PROFILE_DEFAULT_MASK
|
1257 ST_PROFILE_OPENGL_CORE_MASK
|
1258 ST_PROFILE_OPENGL_ES1_MASK
|
1259 ST_PROFILE_OPENGL_ES2_MASK
|
1261 .feature_mask
= ST_API_FEATURE_MS_VISUALS_MASK
,
1262 .destroy
= st_api_destroy
,
1263 .query_versions
= st_api_query_versions
,
1264 .create_context
= st_api_create_context
,
1265 .make_current
= st_api_make_current
,
1266 .get_current
= st_api_get_current
,
1267 .destroy_drawable
= st_api_destroy_drawable
,
1272 st_gl_api_create(void)
1274 return (struct st_api
*) &st_gl_api
;