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/format/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 */
70 simple_mtx_t st_mutex
;
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 st_set_ws_renderbuffer_surface(struct st_renderbuffer
*strb
,
178 struct pipe_surface
*surf
)
180 pipe_surface_reference(&strb
->surface_srgb
, NULL
);
181 pipe_surface_reference(&strb
->surface_linear
, NULL
);
183 if (util_format_is_srgb(surf
->format
))
184 pipe_surface_reference(&strb
->surface_srgb
, surf
);
186 pipe_surface_reference(&strb
->surface_linear
, surf
);
188 strb
->surface
= surf
; /* just assign, don't ref */
189 pipe_resource_reference(&strb
->texture
, surf
->texture
);
191 strb
->Base
.Width
= surf
->width
;
192 strb
->Base
.Height
= surf
->height
;
197 * Validate a framebuffer to make sure up-to-date pipe_textures are used.
198 * The context is only used for creating pipe surfaces and for calling
199 * _mesa_resize_framebuffer().
200 * (That should probably be rethought, since those surfaces become
201 * drawable state, not context state, and can be freed by another pipe
205 st_framebuffer_validate(struct st_framebuffer
*stfb
,
206 struct st_context
*st
)
208 struct pipe_resource
*textures
[ST_ATTACHMENT_COUNT
];
211 bool changed
= false;
214 new_stamp
= p_atomic_read(&stfb
->iface
->stamp
);
215 if (stfb
->iface_stamp
== new_stamp
)
218 memset(textures
, 0, stfb
->num_statts
* sizeof(textures
[0]));
220 /* validate the fb */
222 if (!stfb
->iface
->validate(&st
->iface
, stfb
->iface
, stfb
->statts
,
223 stfb
->num_statts
, textures
))
226 stfb
->iface_stamp
= new_stamp
;
227 new_stamp
= p_atomic_read(&stfb
->iface
->stamp
);
228 } while(stfb
->iface_stamp
!= new_stamp
);
230 width
= stfb
->Base
.Width
;
231 height
= stfb
->Base
.Height
;
233 for (i
= 0; i
< stfb
->num_statts
; i
++) {
234 struct st_renderbuffer
*strb
;
235 struct pipe_surface
*ps
, surf_tmpl
;
241 idx
= attachment_to_buffer_index(stfb
->statts
[i
]);
242 if (idx
>= BUFFER_COUNT
) {
243 pipe_resource_reference(&textures
[i
], NULL
);
247 strb
= st_renderbuffer(stfb
->Base
.Attachment
[idx
].Renderbuffer
);
249 if (strb
->texture
== textures
[i
]) {
250 pipe_resource_reference(&textures
[i
], NULL
);
254 u_surface_default_template(&surf_tmpl
, textures
[i
]);
255 ps
= st
->pipe
->create_surface(st
->pipe
, textures
[i
], &surf_tmpl
);
257 st_set_ws_renderbuffer_surface(strb
, ps
);
258 pipe_surface_reference(&ps
, NULL
);
262 width
= strb
->Base
.Width
;
263 height
= strb
->Base
.Height
;
266 pipe_resource_reference(&textures
[i
], NULL
);
271 _mesa_resize_framebuffer(st
->ctx
, &stfb
->Base
, width
, height
);
277 * Update the attachments to validate by looping the existing renderbuffers.
280 st_framebuffer_update_attachments(struct st_framebuffer
*stfb
)
284 stfb
->num_statts
= 0;
285 for (idx
= 0; idx
< BUFFER_COUNT
; idx
++) {
286 struct st_renderbuffer
*strb
;
287 enum st_attachment_type statt
;
289 strb
= st_renderbuffer(stfb
->Base
.Attachment
[idx
].Renderbuffer
);
290 if (!strb
|| strb
->software
)
293 statt
= buffer_index_to_attachment(idx
);
294 if (statt
!= ST_ATTACHMENT_INVALID
&&
295 st_visual_have_buffers(stfb
->iface
->visual
, 1 << statt
))
296 stfb
->statts
[stfb
->num_statts
++] = statt
;
303 * Add a renderbuffer to the framebuffer. The framebuffer is one that
304 * corresponds to a window and is not a user-created FBO.
307 st_framebuffer_add_renderbuffer(struct st_framebuffer
*stfb
,
308 gl_buffer_index idx
, bool prefer_srgb
)
310 struct gl_renderbuffer
*rb
;
311 enum pipe_format format
;
314 assert(_mesa_is_winsys_fbo(&stfb
->Base
));
316 /* do not distinguish depth/stencil buffers */
317 if (idx
== BUFFER_STENCIL
)
322 format
= stfb
->iface
->visual
->depth_stencil_format
;
326 format
= stfb
->iface
->visual
->accum_format
;
330 format
= stfb
->iface
->visual
->color_format
;
332 format
= util_format_srgb(format
);
337 if (format
== PIPE_FORMAT_NONE
)
340 rb
= st_new_renderbuffer_fb(format
, stfb
->iface
->visual
->samples
, sw
);
344 if (idx
!= BUFFER_DEPTH
) {
345 _mesa_attach_and_own_rb(&stfb
->Base
, idx
, rb
);
349 bool rb_ownership_taken
= false;
350 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_ZS
, 0)) {
351 _mesa_attach_and_own_rb(&stfb
->Base
, BUFFER_DEPTH
, rb
);
352 rb_ownership_taken
= true;
355 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_ZS
, 1)) {
356 if (rb_ownership_taken
)
357 _mesa_attach_and_reference_rb(&stfb
->Base
, BUFFER_STENCIL
, rb
);
359 _mesa_attach_and_own_rb(&stfb
->Base
, BUFFER_STENCIL
, rb
);
367 * Intialize a struct gl_config from a visual.
370 st_visual_to_context_mode(const struct st_visual
*visual
,
371 struct gl_config
*mode
)
373 memset(mode
, 0, sizeof(*mode
));
375 if (st_visual_have_buffers(visual
, ST_ATTACHMENT_BACK_LEFT_MASK
))
376 mode
->doubleBufferMode
= GL_TRUE
;
378 if (st_visual_have_buffers(visual
,
379 ST_ATTACHMENT_FRONT_RIGHT_MASK
| ST_ATTACHMENT_BACK_RIGHT_MASK
))
380 mode
->stereoMode
= GL_TRUE
;
382 if (visual
->color_format
!= PIPE_FORMAT_NONE
) {
384 util_format_get_component_bits(visual
->color_format
,
385 UTIL_FORMAT_COLORSPACE_RGB
, 0);
387 util_format_get_component_bits(visual
->color_format
,
388 UTIL_FORMAT_COLORSPACE_RGB
, 1);
390 util_format_get_component_bits(visual
->color_format
,
391 UTIL_FORMAT_COLORSPACE_RGB
, 2);
393 util_format_get_component_bits(visual
->color_format
,
394 UTIL_FORMAT_COLORSPACE_RGB
, 3);
396 mode
->rgbBits
= mode
->redBits
+
397 mode
->greenBits
+ mode
->blueBits
+ mode
->alphaBits
;
398 mode
->sRGBCapable
= util_format_is_srgb(visual
->color_format
);
401 if (visual
->depth_stencil_format
!= PIPE_FORMAT_NONE
) {
403 util_format_get_component_bits(visual
->depth_stencil_format
,
404 UTIL_FORMAT_COLORSPACE_ZS
, 0);
406 util_format_get_component_bits(visual
->depth_stencil_format
,
407 UTIL_FORMAT_COLORSPACE_ZS
, 1);
410 if (visual
->accum_format
!= PIPE_FORMAT_NONE
) {
412 util_format_get_component_bits(visual
->accum_format
,
413 UTIL_FORMAT_COLORSPACE_RGB
, 0);
414 mode
->accumGreenBits
=
415 util_format_get_component_bits(visual
->accum_format
,
416 UTIL_FORMAT_COLORSPACE_RGB
, 1);
417 mode
->accumBlueBits
=
418 util_format_get_component_bits(visual
->accum_format
,
419 UTIL_FORMAT_COLORSPACE_RGB
, 2);
420 mode
->accumAlphaBits
=
421 util_format_get_component_bits(visual
->accum_format
,
422 UTIL_FORMAT_COLORSPACE_RGB
, 3);
425 if (visual
->samples
> 1) {
426 mode
->sampleBuffers
= 1;
427 mode
->samples
= visual
->samples
;
433 * Create a framebuffer from a manager interface.
435 static struct st_framebuffer
*
436 st_framebuffer_create(struct st_context
*st
,
437 struct st_framebuffer_iface
*stfbi
)
439 struct st_framebuffer
*stfb
;
440 struct gl_config mode
;
442 bool prefer_srgb
= false;
447 stfb
= CALLOC_STRUCT(st_framebuffer
);
451 st_visual_to_context_mode(stfbi
->visual
, &mode
);
454 * For desktop GL, sRGB framebuffer write is controlled by both the
455 * capability of the framebuffer and GL_FRAMEBUFFER_SRGB. We should
456 * advertise the capability when the pipe driver (and core Mesa) supports
457 * it so that applications can enable sRGB write when they want to.
459 * This is not to be confused with GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB. When
460 * the attribute is GLX_TRUE, it tells the st manager to pick a color
461 * format such that util_format_srgb(visual->color_format) can be supported
462 * by the pipe driver. We still need to advertise the capability here.
464 * For GLES, however, sRGB framebuffer write is initially only controlled
465 * by the capability of the framebuffer, with GL_EXT_sRGB_write_control
466 * control is given back to the applications, but GL_FRAMEBUFFER_SRGB is
467 * still enabled by default since this is the behaviour when
468 * EXT_sRGB_write_control is not available. Since GL_EXT_sRGB_write_control
469 * brings GLES on par with desktop GLs EXT_framebuffer_sRGB, in mesa this
470 * is also expressed by using the same extension flag
472 if (_mesa_has_EXT_framebuffer_sRGB(st
->ctx
)) {
473 struct pipe_screen
*screen
= st
->pipe
->screen
;
474 const enum pipe_format srgb_format
=
475 util_format_srgb(stfbi
->visual
->color_format
);
477 if (srgb_format
!= PIPE_FORMAT_NONE
&&
478 st_pipe_format_to_mesa_format(srgb_format
) != MESA_FORMAT_NONE
&&
479 screen
->is_format_supported(screen
, srgb_format
,
480 PIPE_TEXTURE_2D
, stfbi
->visual
->samples
,
481 stfbi
->visual
->samples
,
482 (PIPE_BIND_DISPLAY_TARGET
|
483 PIPE_BIND_RENDER_TARGET
))) {
484 mode
.sRGBCapable
= GL_TRUE
;
485 /* Since GL_FRAMEBUFFER_SRGB is enabled by default on GLES we must not
486 * create renderbuffers with an sRGB format derived from the
487 * visual->color_format, but we still want sRGB for desktop GL.
489 prefer_srgb
= _mesa_is_desktop_gl(st
->ctx
);
493 _mesa_initialize_window_framebuffer(&stfb
->Base
, &mode
);
496 stfb
->iface_ID
= stfbi
->ID
;
497 stfb
->iface_stamp
= p_atomic_read(&stfbi
->stamp
) - 1;
499 /* add the color buffer */
500 idx
= stfb
->Base
._ColorDrawBufferIndexes
[0];
501 if (!st_framebuffer_add_renderbuffer(stfb
, idx
, prefer_srgb
)) {
506 st_framebuffer_add_renderbuffer(stfb
, BUFFER_DEPTH
, false);
507 st_framebuffer_add_renderbuffer(stfb
, BUFFER_ACCUM
, false);
510 st_framebuffer_update_attachments(stfb
);
517 * Reference a framebuffer.
520 st_framebuffer_reference(struct st_framebuffer
**ptr
,
521 struct st_framebuffer
*stfb
)
523 struct gl_framebuffer
*fb
= stfb
? &stfb
->Base
: NULL
;
524 _mesa_reference_framebuffer((struct gl_framebuffer
**) ptr
, fb
);
529 st_framebuffer_iface_hash(const void *key
)
531 return (uintptr_t)key
;
536 st_framebuffer_iface_equal(const void *a
, const void *b
)
538 return (struct st_framebuffer_iface
*)a
== (struct st_framebuffer_iface
*)b
;
543 st_framebuffer_iface_lookup(struct st_manager
*smapi
,
544 const struct st_framebuffer_iface
*stfbi
)
546 struct st_manager_private
*smPriv
=
547 (struct st_manager_private
*)smapi
->st_manager_private
;
548 struct hash_entry
*entry
;
551 assert(smPriv
->stfbi_ht
);
553 simple_mtx_lock(&smPriv
->st_mutex
);
554 entry
= _mesa_hash_table_search(smPriv
->stfbi_ht
, stfbi
);
555 simple_mtx_unlock(&smPriv
->st_mutex
);
557 return entry
!= NULL
;
562 st_framebuffer_iface_insert(struct st_manager
*smapi
,
563 struct st_framebuffer_iface
*stfbi
)
565 struct st_manager_private
*smPriv
=
566 (struct st_manager_private
*)smapi
->st_manager_private
;
567 struct hash_entry
*entry
;
570 assert(smPriv
->stfbi_ht
);
572 simple_mtx_lock(&smPriv
->st_mutex
);
573 entry
= _mesa_hash_table_insert(smPriv
->stfbi_ht
, stfbi
, stfbi
);
574 simple_mtx_unlock(&smPriv
->st_mutex
);
576 return entry
!= NULL
;
581 st_framebuffer_iface_remove(struct st_manager
*smapi
,
582 struct st_framebuffer_iface
*stfbi
)
584 struct st_manager_private
*smPriv
=
585 (struct st_manager_private
*)smapi
->st_manager_private
;
586 struct hash_entry
*entry
;
588 if (!smPriv
|| !smPriv
->stfbi_ht
)
591 simple_mtx_lock(&smPriv
->st_mutex
);
592 entry
= _mesa_hash_table_search(smPriv
->stfbi_ht
, stfbi
);
596 _mesa_hash_table_remove(smPriv
->stfbi_ht
, entry
);
599 simple_mtx_unlock(&smPriv
->st_mutex
);
604 * The framebuffer interface object is no longer valid.
605 * Remove the object from the framebuffer interface hash table.
608 st_api_destroy_drawable(struct st_api
*stapi
,
609 struct st_framebuffer_iface
*stfbi
)
614 st_framebuffer_iface_remove(stfbi
->state_manager
, stfbi
);
619 * Purge the winsys buffers list to remove any references to
620 * non-existing framebuffer interface objects.
623 st_framebuffers_purge(struct st_context
*st
)
625 struct st_context_iface
*st_iface
= &st
->iface
;
626 struct st_manager
*smapi
= st_iface
->state_manager
;
627 struct st_framebuffer
*stfb
, *next
;
631 LIST_FOR_EACH_ENTRY_SAFE_REV(stfb
, next
, &st
->winsys_buffers
, head
) {
632 struct st_framebuffer_iface
*stfbi
= stfb
->iface
;
637 * If the corresponding framebuffer interface object no longer exists,
638 * remove the framebuffer object from the context's winsys buffers list,
639 * and unreference the framebuffer object, so its resources can be
642 if (!st_framebuffer_iface_lookup(smapi
, stfbi
)) {
643 list_del(&stfb
->head
);
644 st_framebuffer_reference(&stfb
, NULL
);
651 st_context_flush(struct st_context_iface
*stctxi
, unsigned flags
,
652 struct pipe_fence_handle
**fence
,
653 void (*before_flush_cb
) (void*),
656 struct st_context
*st
= (struct st_context
*) stctxi
;
657 unsigned pipe_flags
= 0;
659 if (flags
& ST_FLUSH_END_OF_FRAME
)
660 pipe_flags
|= PIPE_FLUSH_END_OF_FRAME
;
661 if (flags
& ST_FLUSH_FENCE_FD
)
662 pipe_flags
|= PIPE_FLUSH_FENCE_FD
;
664 /* If both the bitmap cache is dirty and there are unflushed vertices,
665 * it means that glBitmap was called first and then glBegin.
667 st_flush_bitmap_cache(st
);
668 FLUSH_VERTICES(st
->ctx
, 0);
670 /* Notify the caller that we're ready to flush */
672 before_flush_cb(args
);
673 st_flush(st
, fence
, pipe_flags
);
675 if ((flags
& ST_FLUSH_WAIT
) && fence
&& *fence
) {
676 st
->pipe
->screen
->fence_finish(st
->pipe
->screen
, NULL
, *fence
,
677 PIPE_TIMEOUT_INFINITE
);
678 st
->pipe
->screen
->fence_reference(st
->pipe
->screen
, fence
, NULL
);
681 if (flags
& ST_FLUSH_FRONT
)
682 st_manager_flush_frontbuffer(st
);
684 /* DRI3 changes the framebuffer after SwapBuffers, but we need to invoke
685 * st_manager_validate_framebuffers to notice that.
687 * Set gfx_shaders_may_be_dirty to invoke st_validate_state in the next
688 * draw call, which will invoke st_manager_validate_framebuffers, but it
689 * won't dirty states if there is no change.
691 if (flags
& ST_FLUSH_END_OF_FRAME
)
692 st
->gfx_shaders_may_be_dirty
= true;
696 st_context_teximage(struct st_context_iface
*stctxi
,
697 enum st_texture_type tex_type
,
698 int level
, enum pipe_format pipe_format
,
699 struct pipe_resource
*tex
, bool mipmap
)
701 struct st_context
*st
= (struct st_context
*) stctxi
;
702 struct gl_context
*ctx
= st
->ctx
;
703 struct gl_texture_object
*texObj
;
704 struct gl_texture_image
*texImage
;
705 struct st_texture_object
*stObj
;
706 struct st_texture_image
*stImage
;
707 GLenum internalFormat
;
708 GLuint width
, height
, depth
;
713 target
= GL_TEXTURE_1D
;
716 target
= GL_TEXTURE_2D
;
719 target
= GL_TEXTURE_3D
;
721 case ST_TEXTURE_RECT
:
722 target
= GL_TEXTURE_RECTANGLE_ARB
;
728 texObj
= _mesa_get_current_tex_object(ctx
, target
);
730 _mesa_lock_texture(ctx
, texObj
);
732 stObj
= st_texture_object(texObj
);
733 /* switch to surface based */
734 if (!stObj
->surface_based
) {
735 _mesa_clear_texture_object(ctx
, texObj
, NULL
);
736 stObj
->surface_based
= GL_TRUE
;
739 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
740 stImage
= st_texture_image(texImage
);
742 mesa_format texFormat
= st_pipe_format_to_mesa_format(pipe_format
);
744 if (util_format_has_alpha(tex
->format
))
745 internalFormat
= GL_RGBA
;
747 internalFormat
= GL_RGB
;
749 _mesa_init_teximage_fields(ctx
, texImage
,
750 tex
->width0
, tex
->height0
, 1, 0,
751 internalFormat
, texFormat
);
754 height
= tex
->height0
;
757 /* grow the image size until we hit level = 0 */
769 _mesa_clear_texture_image(ctx
, texImage
);
770 width
= height
= depth
= 0;
773 pipe_resource_reference(&stObj
->pt
, tex
);
774 st_texture_release_all_sampler_views(st
, stObj
);
775 pipe_resource_reference(&stImage
->pt
, tex
);
776 stObj
->surface_format
= pipe_format
;
778 stObj
->needs_validation
= true;
780 _mesa_dirty_texobj(ctx
, texObj
);
781 _mesa_unlock_texture(ctx
, texObj
);
788 st_context_copy(struct st_context_iface
*stctxi
,
789 struct st_context_iface
*stsrci
, unsigned mask
)
791 struct st_context
*st
= (struct st_context
*) stctxi
;
792 struct st_context
*src
= (struct st_context
*) stsrci
;
794 _mesa_copy_context(src
->ctx
, st
->ctx
, mask
);
799 st_context_share(struct st_context_iface
*stctxi
,
800 struct st_context_iface
*stsrci
)
802 struct st_context
*st
= (struct st_context
*) stctxi
;
803 struct st_context
*src
= (struct st_context
*) stsrci
;
805 return _mesa_share_state(st
->ctx
, src
->ctx
);
810 st_context_destroy(struct st_context_iface
*stctxi
)
812 struct st_context
*st
= (struct st_context
*) stctxi
;
813 st_destroy_context(st
);
818 st_start_thread(struct st_context_iface
*stctxi
)
820 struct st_context
*st
= (struct st_context
*) stctxi
;
822 _mesa_glthread_init(st
->ctx
);
824 /* Pin all driver threads to one L3 cache for optimal performance
825 * on AMD Zen. This is only done if glthread is enabled.
827 * If glthread is disabled, st_draw.c re-pins driver threads regularly
828 * based on the location of the app thread.
830 struct glthread_state
*glthread
= st
->ctx
->GLThread
;
831 if (glthread
&& st
->pipe
->set_context_param
) {
832 util_pin_driver_threads_to_random_L3(st
->pipe
, &glthread
->queue
.threads
[0]);
838 st_thread_finish(struct st_context_iface
*stctxi
)
840 struct st_context
*st
= (struct st_context
*) stctxi
;
842 _mesa_glthread_finish(st
->ctx
);
847 st_manager_destroy(struct st_manager
*smapi
)
849 struct st_manager_private
*smPriv
= smapi
->st_manager_private
;
851 if (smPriv
&& smPriv
->stfbi_ht
) {
852 _mesa_hash_table_destroy(smPriv
->stfbi_ht
, NULL
);
853 simple_mtx_destroy(&smPriv
->st_mutex
);
855 smapi
->st_manager_private
= NULL
;
860 static struct st_context_iface
*
861 st_api_create_context(struct st_api
*stapi
, struct st_manager
*smapi
,
862 const struct st_context_attribs
*attribs
,
863 enum st_context_error
*error
,
864 struct st_context_iface
*shared_stctxi
)
866 struct st_context
*shared_ctx
= (struct st_context
*) shared_stctxi
;
867 struct st_context
*st
;
868 struct pipe_context
*pipe
;
869 struct gl_config
* mode_ptr
;
870 struct gl_config mode
;
872 bool no_error
= false;
873 unsigned ctx_flags
= PIPE_CONTEXT_PREFER_THREADED
;
875 if (!(stapi
->profile_mask
& (1 << attribs
->profile
)))
878 switch (attribs
->profile
) {
879 case ST_PROFILE_DEFAULT
:
880 api
= API_OPENGL_COMPAT
;
882 case ST_PROFILE_OPENGL_ES1
:
885 case ST_PROFILE_OPENGL_ES2
:
888 case ST_PROFILE_OPENGL_CORE
:
889 api
= API_OPENGL_CORE
;
892 *error
= ST_CONTEXT_ERROR_BAD_API
;
896 /* Create a hash table for the framebuffer interface objects
897 * if it has not been created for this st manager.
899 if (smapi
->st_manager_private
== NULL
) {
900 struct st_manager_private
*smPriv
;
902 smPriv
= CALLOC_STRUCT(st_manager_private
);
903 simple_mtx_init(&smPriv
->st_mutex
, mtx_plain
);
904 smPriv
->stfbi_ht
= _mesa_hash_table_create(NULL
,
905 st_framebuffer_iface_hash
,
906 st_framebuffer_iface_equal
);
907 smapi
->st_manager_private
= smPriv
;
908 smapi
->destroy
= st_manager_destroy
;
911 if (attribs
->flags
& ST_CONTEXT_FLAG_ROBUST_ACCESS
)
912 ctx_flags
|= PIPE_CONTEXT_ROBUST_BUFFER_ACCESS
;
914 if (attribs
->flags
& ST_CONTEXT_FLAG_NO_ERROR
)
917 if (attribs
->flags
& ST_CONTEXT_FLAG_LOW_PRIORITY
)
918 ctx_flags
|= PIPE_CONTEXT_LOW_PRIORITY
;
919 else if (attribs
->flags
& ST_CONTEXT_FLAG_HIGH_PRIORITY
)
920 ctx_flags
|= PIPE_CONTEXT_HIGH_PRIORITY
;
922 if (attribs
->flags
& ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED
)
923 ctx_flags
|= PIPE_CONTEXT_LOSE_CONTEXT_ON_RESET
;
925 pipe
= smapi
->screen
->context_create(smapi
->screen
, NULL
, ctx_flags
);
927 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
931 st_visual_to_context_mode(&attribs
->visual
, &mode
);
933 if (attribs
->visual
.no_config
)
938 st
= st_create_context(api
, pipe
, mode_ptr
, shared_ctx
,
939 &attribs
->options
, no_error
);
941 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
946 if (attribs
->flags
& ST_CONTEXT_FLAG_DEBUG
) {
947 if (!_mesa_set_debug_state_int(st
->ctx
, GL_DEBUG_OUTPUT
, GL_TRUE
)) {
948 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
952 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_DEBUG_BIT
;
955 if (st
->ctx
->Const
.ContextFlags
& GL_CONTEXT_FLAG_DEBUG_BIT
) {
956 st_update_debug_callback(st
);
959 if (attribs
->flags
& ST_CONTEXT_FLAG_FORWARD_COMPATIBLE
)
960 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT
;
961 if (attribs
->flags
& ST_CONTEXT_FLAG_ROBUST_ACCESS
) {
962 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB
;
963 st
->ctx
->Const
.RobustAccess
= GL_TRUE
;
965 if (attribs
->flags
& ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED
) {
966 st
->ctx
->Const
.ResetStrategy
= GL_LOSE_CONTEXT_ON_RESET_ARB
;
967 st_install_device_reset_callback(st
);
970 if (attribs
->flags
& ST_CONTEXT_FLAG_RELEASE_NONE
)
971 st
->ctx
->Const
.ContextReleaseBehavior
= GL_NONE
;
973 /* need to perform version check */
974 if (attribs
->major
> 1 || attribs
->minor
> 0) {
975 /* Is the actual version less than the requested version?
977 if (st
->ctx
->Version
< attribs
->major
* 10U + attribs
->minor
) {
978 *error
= ST_CONTEXT_ERROR_BAD_VERSION
;
979 st_destroy_context(st
);
984 st
->invalidate_on_gl_viewport
=
985 smapi
->get_param(smapi
, ST_MANAGER_BROKEN_INVALIDATE
);
987 st
->iface
.destroy
= st_context_destroy
;
988 st
->iface
.flush
= st_context_flush
;
989 st
->iface
.teximage
= st_context_teximage
;
990 st
->iface
.copy
= st_context_copy
;
991 st
->iface
.share
= st_context_share
;
992 st
->iface
.start_thread
= st_start_thread
;
993 st
->iface
.thread_finish
= st_thread_finish
;
994 st
->iface
.st_context_private
= (void *) smapi
;
995 st
->iface
.cso_context
= st
->cso_context
;
996 st
->iface
.pipe
= st
->pipe
;
997 st
->iface
.state_manager
= smapi
;
999 *error
= ST_CONTEXT_SUCCESS
;
1004 static struct st_context_iface
*
1005 st_api_get_current(struct st_api
*stapi
)
1007 GET_CURRENT_CONTEXT(ctx
);
1008 struct st_context
*st
= ctx
? ctx
->st
: NULL
;
1010 return st
? &st
->iface
: NULL
;
1014 static struct st_framebuffer
*
1015 st_framebuffer_reuse_or_create(struct st_context
*st
,
1016 struct gl_framebuffer
*fb
,
1017 struct st_framebuffer_iface
*stfbi
)
1019 struct st_framebuffer
*cur
= NULL
, *stfb
= NULL
;
1024 /* Check if there is already a framebuffer object for the specified
1025 * framebuffer interface in this context. If there is one, use it.
1027 LIST_FOR_EACH_ENTRY(cur
, &st
->winsys_buffers
, head
) {
1028 if (cur
->iface_ID
== stfbi
->ID
) {
1029 st_framebuffer_reference(&stfb
, cur
);
1034 /* If there is not already a framebuffer object, create one */
1036 cur
= st_framebuffer_create(st
, stfbi
);
1039 /* add the referenced framebuffer interface object to
1040 * the framebuffer interface object hash table.
1042 if (!st_framebuffer_iface_insert(stfbi
->state_manager
, stfbi
)) {
1043 st_framebuffer_reference(&cur
, NULL
);
1047 /* add to the context's winsys buffers list */
1048 list_add(&cur
->head
, &st
->winsys_buffers
);
1050 st_framebuffer_reference(&stfb
, cur
);
1059 st_api_make_current(struct st_api
*stapi
, struct st_context_iface
*stctxi
,
1060 struct st_framebuffer_iface
*stdrawi
,
1061 struct st_framebuffer_iface
*streadi
)
1063 struct st_context
*st
= (struct st_context
*) stctxi
;
1064 struct st_framebuffer
*stdraw
, *stread
;
1068 /* reuse or create the draw fb */
1069 stdraw
= st_framebuffer_reuse_or_create(st
,
1070 st
->ctx
->WinSysDrawBuffer
, stdrawi
);
1071 if (streadi
!= stdrawi
) {
1072 /* do the same for the read fb */
1073 stread
= st_framebuffer_reuse_or_create(st
,
1074 st
->ctx
->WinSysReadBuffer
, streadi
);
1078 /* reuse the draw fb for the read fb */
1080 st_framebuffer_reference(&stread
, stdraw
);
1083 if (stdraw
&& stread
) {
1084 st_framebuffer_validate(stdraw
, st
);
1085 if (stread
!= stdraw
)
1086 st_framebuffer_validate(stread
, st
);
1088 ret
= _mesa_make_current(st
->ctx
, &stdraw
->Base
, &stread
->Base
);
1090 st
->draw_stamp
= stdraw
->stamp
- 1;
1091 st
->read_stamp
= stread
->stamp
- 1;
1092 st_context_validate(st
, stdraw
, stread
);
1095 struct gl_framebuffer
*incomplete
= _mesa_get_incomplete_framebuffer();
1096 ret
= _mesa_make_current(st
->ctx
, incomplete
, incomplete
);
1099 st_framebuffer_reference(&stdraw
, NULL
);
1100 st_framebuffer_reference(&stread
, NULL
);
1102 /* Purge the context's winsys_buffers list in case any
1103 * of the referenced drawables no longer exist.
1105 st_framebuffers_purge(st
);
1108 GET_CURRENT_CONTEXT(ctx
);
1111 /* Before releasing the context, release its associated
1112 * winsys buffers first. Then purge the context's winsys buffers list
1113 * to free the resources of any winsys buffers that no longer have
1114 * an existing drawable.
1116 ret
= _mesa_make_current(ctx
, NULL
, NULL
);
1117 st_framebuffers_purge(ctx
->st
);
1120 ret
= _mesa_make_current(NULL
, NULL
, NULL
);
1128 st_api_destroy(struct st_api
*stapi
)
1134 * Flush the front buffer if the current context renders to the front buffer.
1137 st_manager_flush_frontbuffer(struct st_context
*st
)
1139 struct st_framebuffer
*stfb
= st_ws_framebuffer(st
->ctx
->DrawBuffer
);
1140 struct st_renderbuffer
*strb
= NULL
;
1145 /* If the context uses a doublebuffered visual, but the buffer is
1146 * single-buffered, guess that it's a pbuffer, which doesn't need
1149 if (st
->ctx
->Visual
.doubleBufferMode
&&
1150 !stfb
->Base
.Visual
.doubleBufferMode
)
1153 strb
= st_renderbuffer(stfb
->Base
.Attachment
[BUFFER_FRONT_LEFT
].
1156 /* Do we have a front color buffer and has it been drawn to since last
1157 * frontbuffer flush?
1159 if (strb
&& strb
->defined
) {
1160 stfb
->iface
->flush_front(&st
->iface
, stfb
->iface
,
1161 ST_ATTACHMENT_FRONT_LEFT
);
1162 strb
->defined
= GL_FALSE
;
1164 /* Trigger an update of strb->defined on next draw */
1165 st
->dirty
|= ST_NEW_FB_STATE
;
1171 * Re-validate the framebuffers.
1174 st_manager_validate_framebuffers(struct st_context
*st
)
1176 struct st_framebuffer
*stdraw
= st_ws_framebuffer(st
->ctx
->DrawBuffer
);
1177 struct st_framebuffer
*stread
= st_ws_framebuffer(st
->ctx
->ReadBuffer
);
1180 st_framebuffer_validate(stdraw
, st
);
1181 if (stread
&& stread
!= stdraw
)
1182 st_framebuffer_validate(stread
, st
);
1184 st_context_validate(st
, stdraw
, stread
);
1189 * Flush any outstanding swapbuffers on the current draw framebuffer.
1192 st_manager_flush_swapbuffers(void)
1194 GET_CURRENT_CONTEXT(ctx
);
1195 struct st_context
*st
= (ctx
) ? ctx
->st
: NULL
;
1196 struct st_framebuffer
*stfb
;
1201 stfb
= st_ws_framebuffer(ctx
->DrawBuffer
);
1202 if (!stfb
|| !stfb
->iface
->flush_swapbuffers
)
1205 stfb
->iface
->flush_swapbuffers(&st
->iface
, stfb
->iface
);
1210 * Add a color renderbuffer on demand. The FBO must correspond to a window,
1211 * not a user-created FBO.
1214 st_manager_add_color_renderbuffer(struct st_context
*st
,
1215 struct gl_framebuffer
*fb
,
1216 gl_buffer_index idx
)
1218 struct st_framebuffer
*stfb
= st_ws_framebuffer(fb
);
1224 assert(_mesa_is_winsys_fbo(fb
));
1226 if (stfb
->Base
.Attachment
[idx
].Renderbuffer
)
1230 case BUFFER_FRONT_LEFT
:
1231 case BUFFER_BACK_LEFT
:
1232 case BUFFER_FRONT_RIGHT
:
1233 case BUFFER_BACK_RIGHT
:
1239 if (!st_framebuffer_add_renderbuffer(stfb
, idx
,
1240 stfb
->Base
.Visual
.sRGBCapable
))
1243 st_framebuffer_update_attachments(stfb
);
1246 * Force a call to the state tracker manager to validate the
1247 * new renderbuffer. It might be that there is a window system
1248 * renderbuffer available.
1251 stfb
->iface_stamp
= p_atomic_read(&stfb
->iface
->stamp
) - 1;
1253 st_invalidate_buffers(st
);
1260 get_version(struct pipe_screen
*screen
,
1261 struct st_config_options
*options
, gl_api api
)
1263 struct gl_constants consts
= {0};
1264 struct gl_extensions extensions
= {0};
1267 if (_mesa_override_gl_version_contextless(&consts
, &api
, &version
)) {
1271 _mesa_init_constants(&consts
, api
);
1272 _mesa_init_extensions(&extensions
);
1274 st_init_limits(screen
, &consts
, &extensions
);
1275 st_init_extensions(screen
, &consts
, &extensions
, options
, api
);
1276 version
= _mesa_get_version(&extensions
, &consts
, api
);
1277 free(consts
.SpirVExtensions
);
1283 st_api_query_versions(struct st_api
*stapi
, struct st_manager
*sm
,
1284 struct st_config_options
*options
,
1285 int *gl_core_version
,
1286 int *gl_compat_version
,
1287 int *gl_es1_version
,
1288 int *gl_es2_version
)
1290 *gl_core_version
= get_version(sm
->screen
, options
, API_OPENGL_CORE
);
1291 *gl_compat_version
= get_version(sm
->screen
, options
, API_OPENGL_COMPAT
);
1292 *gl_es1_version
= get_version(sm
->screen
, options
, API_OPENGLES
);
1293 *gl_es2_version
= get_version(sm
->screen
, options
, API_OPENGLES2
);
1297 static const struct st_api st_gl_api
= {
1298 .name
= "Mesa " PACKAGE_VERSION
,
1299 .api
= ST_API_OPENGL
,
1300 .profile_mask
= ST_PROFILE_DEFAULT_MASK
|
1301 ST_PROFILE_OPENGL_CORE_MASK
|
1302 ST_PROFILE_OPENGL_ES1_MASK
|
1303 ST_PROFILE_OPENGL_ES2_MASK
|
1305 .feature_mask
= ST_API_FEATURE_MS_VISUALS_MASK
,
1306 .destroy
= st_api_destroy
,
1307 .query_versions
= st_api_query_versions
,
1308 .create_context
= st_api_create_context
,
1309 .make_current
= st_api_make_current
,
1310 .get_current
= st_api_get_current
,
1311 .destroy_drawable
= st_api_destroy_drawable
,
1316 st_gl_api_create(void)
1318 return (struct st_api
*) &st_gl_api
;