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"
65 #include "util/u_memory.h"
68 struct st_manager_private
70 struct hash_table
*stfbi_ht
; /* framebuffer iface objects hash table */
71 simple_mtx_t st_mutex
;
76 * Map an attachment to a buffer index.
78 static inline gl_buffer_index
79 attachment_to_buffer_index(enum st_attachment_type statt
)
81 gl_buffer_index index
;
84 case ST_ATTACHMENT_FRONT_LEFT
:
85 index
= BUFFER_FRONT_LEFT
;
87 case ST_ATTACHMENT_BACK_LEFT
:
88 index
= BUFFER_BACK_LEFT
;
90 case ST_ATTACHMENT_FRONT_RIGHT
:
91 index
= BUFFER_FRONT_RIGHT
;
93 case ST_ATTACHMENT_BACK_RIGHT
:
94 index
= BUFFER_BACK_RIGHT
;
96 case ST_ATTACHMENT_DEPTH_STENCIL
:
99 case ST_ATTACHMENT_ACCUM
:
100 index
= BUFFER_ACCUM
;
102 case ST_ATTACHMENT_SAMPLE
:
104 index
= BUFFER_COUNT
;
113 * Map a buffer index to an attachment.
115 static inline enum st_attachment_type
116 buffer_index_to_attachment(gl_buffer_index index
)
118 enum st_attachment_type statt
;
121 case BUFFER_FRONT_LEFT
:
122 statt
= ST_ATTACHMENT_FRONT_LEFT
;
124 case BUFFER_BACK_LEFT
:
125 statt
= ST_ATTACHMENT_BACK_LEFT
;
127 case BUFFER_FRONT_RIGHT
:
128 statt
= ST_ATTACHMENT_FRONT_RIGHT
;
130 case BUFFER_BACK_RIGHT
:
131 statt
= ST_ATTACHMENT_BACK_RIGHT
;
134 statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
137 statt
= ST_ATTACHMENT_ACCUM
;
140 statt
= ST_ATTACHMENT_INVALID
;
149 * Make sure a context picks up the latest cached state of the
150 * drawables it binds to.
153 st_context_validate(struct st_context
*st
,
154 struct st_framebuffer
*stdraw
,
155 struct st_framebuffer
*stread
)
157 if (stdraw
&& stdraw
->stamp
!= st
->draw_stamp
) {
158 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
159 _mesa_resize_framebuffer(st
->ctx
, &stdraw
->Base
,
161 stdraw
->Base
.Height
);
162 st
->draw_stamp
= stdraw
->stamp
;
165 if (stread
&& stread
->stamp
!= st
->read_stamp
) {
166 if (stread
!= stdraw
) {
167 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
168 _mesa_resize_framebuffer(st
->ctx
, &stread
->Base
,
170 stread
->Base
.Height
);
172 st
->read_stamp
= stread
->stamp
;
178 st_set_ws_renderbuffer_surface(struct st_renderbuffer
*strb
,
179 struct pipe_surface
*surf
)
181 pipe_surface_reference(&strb
->surface_srgb
, NULL
);
182 pipe_surface_reference(&strb
->surface_linear
, NULL
);
184 if (util_format_is_srgb(surf
->format
))
185 pipe_surface_reference(&strb
->surface_srgb
, surf
);
187 pipe_surface_reference(&strb
->surface_linear
, surf
);
189 strb
->surface
= surf
; /* just assign, don't ref */
190 pipe_resource_reference(&strb
->texture
, surf
->texture
);
192 strb
->Base
.Width
= surf
->width
;
193 strb
->Base
.Height
= surf
->height
;
198 * Validate a framebuffer to make sure up-to-date pipe_textures are used.
199 * The context is only used for creating pipe surfaces and for calling
200 * _mesa_resize_framebuffer().
201 * (That should probably be rethought, since those surfaces become
202 * drawable state, not context state, and can be freed by another pipe
206 st_framebuffer_validate(struct st_framebuffer
*stfb
,
207 struct st_context
*st
)
209 struct pipe_resource
*textures
[ST_ATTACHMENT_COUNT
];
212 bool changed
= false;
215 new_stamp
= p_atomic_read(&stfb
->iface
->stamp
);
216 if (stfb
->iface_stamp
== new_stamp
)
219 memset(textures
, 0, stfb
->num_statts
* sizeof(textures
[0]));
221 /* validate the fb */
223 if (!stfb
->iface
->validate(&st
->iface
, stfb
->iface
, stfb
->statts
,
224 stfb
->num_statts
, textures
))
227 stfb
->iface_stamp
= new_stamp
;
228 new_stamp
= p_atomic_read(&stfb
->iface
->stamp
);
229 } while(stfb
->iface_stamp
!= new_stamp
);
231 width
= stfb
->Base
.Width
;
232 height
= stfb
->Base
.Height
;
234 for (i
= 0; i
< stfb
->num_statts
; i
++) {
235 struct st_renderbuffer
*strb
;
236 struct pipe_surface
*ps
, surf_tmpl
;
242 idx
= attachment_to_buffer_index(stfb
->statts
[i
]);
243 if (idx
>= BUFFER_COUNT
) {
244 pipe_resource_reference(&textures
[i
], NULL
);
248 strb
= st_renderbuffer(stfb
->Base
.Attachment
[idx
].Renderbuffer
);
250 if (strb
->texture
== textures
[i
]) {
251 pipe_resource_reference(&textures
[i
], NULL
);
255 u_surface_default_template(&surf_tmpl
, textures
[i
]);
256 ps
= st
->pipe
->create_surface(st
->pipe
, textures
[i
], &surf_tmpl
);
258 st_set_ws_renderbuffer_surface(strb
, ps
);
259 pipe_surface_reference(&ps
, NULL
);
263 width
= strb
->Base
.Width
;
264 height
= strb
->Base
.Height
;
267 pipe_resource_reference(&textures
[i
], NULL
);
272 _mesa_resize_framebuffer(st
->ctx
, &stfb
->Base
, width
, height
);
278 * Update the attachments to validate by looping the existing renderbuffers.
281 st_framebuffer_update_attachments(struct st_framebuffer
*stfb
)
285 stfb
->num_statts
= 0;
286 for (idx
= 0; idx
< BUFFER_COUNT
; idx
++) {
287 struct st_renderbuffer
*strb
;
288 enum st_attachment_type statt
;
290 strb
= st_renderbuffer(stfb
->Base
.Attachment
[idx
].Renderbuffer
);
291 if (!strb
|| strb
->software
)
294 statt
= buffer_index_to_attachment(idx
);
295 if (statt
!= ST_ATTACHMENT_INVALID
&&
296 st_visual_have_buffers(stfb
->iface
->visual
, 1 << statt
))
297 stfb
->statts
[stfb
->num_statts
++] = statt
;
304 * Add a renderbuffer to the framebuffer. The framebuffer is one that
305 * corresponds to a window and is not a user-created FBO.
308 st_framebuffer_add_renderbuffer(struct st_framebuffer
*stfb
,
309 gl_buffer_index idx
, bool prefer_srgb
)
311 struct gl_renderbuffer
*rb
;
312 enum pipe_format format
;
315 assert(_mesa_is_winsys_fbo(&stfb
->Base
));
317 /* do not distinguish depth/stencil buffers */
318 if (idx
== BUFFER_STENCIL
)
323 format
= stfb
->iface
->visual
->depth_stencil_format
;
327 format
= stfb
->iface
->visual
->accum_format
;
331 format
= stfb
->iface
->visual
->color_format
;
333 format
= util_format_srgb(format
);
338 if (format
== PIPE_FORMAT_NONE
)
341 rb
= st_new_renderbuffer_fb(format
, stfb
->iface
->visual
->samples
, sw
);
345 if (idx
!= BUFFER_DEPTH
) {
346 _mesa_attach_and_own_rb(&stfb
->Base
, idx
, rb
);
350 bool rb_ownership_taken
= false;
351 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_ZS
, 0)) {
352 _mesa_attach_and_own_rb(&stfb
->Base
, BUFFER_DEPTH
, rb
);
353 rb_ownership_taken
= true;
356 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_ZS
, 1)) {
357 if (rb_ownership_taken
)
358 _mesa_attach_and_reference_rb(&stfb
->Base
, BUFFER_STENCIL
, rb
);
360 _mesa_attach_and_own_rb(&stfb
->Base
, BUFFER_STENCIL
, rb
);
368 * Intialize a struct gl_config from a visual.
371 st_visual_to_context_mode(const struct st_visual
*visual
,
372 struct gl_config
*mode
)
374 memset(mode
, 0, sizeof(*mode
));
376 if (st_visual_have_buffers(visual
, ST_ATTACHMENT_BACK_LEFT_MASK
))
377 mode
->doubleBufferMode
= GL_TRUE
;
379 if (st_visual_have_buffers(visual
,
380 ST_ATTACHMENT_FRONT_RIGHT_MASK
| ST_ATTACHMENT_BACK_RIGHT_MASK
))
381 mode
->stereoMode
= GL_TRUE
;
383 if (visual
->color_format
!= PIPE_FORMAT_NONE
) {
385 util_format_get_component_bits(visual
->color_format
,
386 UTIL_FORMAT_COLORSPACE_RGB
, 0);
388 util_format_get_component_bits(visual
->color_format
,
389 UTIL_FORMAT_COLORSPACE_RGB
, 1);
391 util_format_get_component_bits(visual
->color_format
,
392 UTIL_FORMAT_COLORSPACE_RGB
, 2);
394 util_format_get_component_bits(visual
->color_format
,
395 UTIL_FORMAT_COLORSPACE_RGB
, 3);
397 mode
->rgbBits
= mode
->redBits
+
398 mode
->greenBits
+ mode
->blueBits
+ mode
->alphaBits
;
399 mode
->sRGBCapable
= util_format_is_srgb(visual
->color_format
);
402 if (visual
->depth_stencil_format
!= PIPE_FORMAT_NONE
) {
404 util_format_get_component_bits(visual
->depth_stencil_format
,
405 UTIL_FORMAT_COLORSPACE_ZS
, 0);
407 util_format_get_component_bits(visual
->depth_stencil_format
,
408 UTIL_FORMAT_COLORSPACE_ZS
, 1);
411 if (visual
->accum_format
!= PIPE_FORMAT_NONE
) {
413 util_format_get_component_bits(visual
->accum_format
,
414 UTIL_FORMAT_COLORSPACE_RGB
, 0);
415 mode
->accumGreenBits
=
416 util_format_get_component_bits(visual
->accum_format
,
417 UTIL_FORMAT_COLORSPACE_RGB
, 1);
418 mode
->accumBlueBits
=
419 util_format_get_component_bits(visual
->accum_format
,
420 UTIL_FORMAT_COLORSPACE_RGB
, 2);
421 mode
->accumAlphaBits
=
422 util_format_get_component_bits(visual
->accum_format
,
423 UTIL_FORMAT_COLORSPACE_RGB
, 3);
426 if (visual
->samples
> 1) {
427 mode
->sampleBuffers
= 1;
428 mode
->samples
= visual
->samples
;
434 * Create a framebuffer from a manager interface.
436 static struct st_framebuffer
*
437 st_framebuffer_create(struct st_context
*st
,
438 struct st_framebuffer_iface
*stfbi
)
440 struct st_framebuffer
*stfb
;
441 struct gl_config mode
;
443 bool prefer_srgb
= false;
448 stfb
= CALLOC_STRUCT(st_framebuffer
);
452 st_visual_to_context_mode(stfbi
->visual
, &mode
);
455 * For desktop GL, sRGB framebuffer write is controlled by both the
456 * capability of the framebuffer and GL_FRAMEBUFFER_SRGB. We should
457 * advertise the capability when the pipe driver (and core Mesa) supports
458 * it so that applications can enable sRGB write when they want to.
460 * This is not to be confused with GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB. When
461 * the attribute is GLX_TRUE, it tells the st manager to pick a color
462 * format such that util_format_srgb(visual->color_format) can be supported
463 * by the pipe driver. We still need to advertise the capability here.
465 * For GLES, however, sRGB framebuffer write is initially only controlled
466 * by the capability of the framebuffer, with GL_EXT_sRGB_write_control
467 * control is given back to the applications, but GL_FRAMEBUFFER_SRGB is
468 * still enabled by default since this is the behaviour when
469 * EXT_sRGB_write_control is not available. Since GL_EXT_sRGB_write_control
470 * brings GLES on par with desktop GLs EXT_framebuffer_sRGB, in mesa this
471 * is also expressed by using the same extension flag
473 if (_mesa_has_EXT_framebuffer_sRGB(st
->ctx
)) {
474 struct pipe_screen
*screen
= st
->pipe
->screen
;
475 const enum pipe_format srgb_format
=
476 util_format_srgb(stfbi
->visual
->color_format
);
478 if (srgb_format
!= PIPE_FORMAT_NONE
&&
479 st_pipe_format_to_mesa_format(srgb_format
) != MESA_FORMAT_NONE
&&
480 screen
->is_format_supported(screen
, srgb_format
,
481 PIPE_TEXTURE_2D
, stfbi
->visual
->samples
,
482 stfbi
->visual
->samples
,
483 (PIPE_BIND_DISPLAY_TARGET
|
484 PIPE_BIND_RENDER_TARGET
))) {
485 mode
.sRGBCapable
= GL_TRUE
;
486 /* Since GL_FRAMEBUFFER_SRGB is enabled by default on GLES we must not
487 * create renderbuffers with an sRGB format derived from the
488 * visual->color_format, but we still want sRGB for desktop GL.
490 prefer_srgb
= _mesa_is_desktop_gl(st
->ctx
);
494 _mesa_initialize_window_framebuffer(&stfb
->Base
, &mode
);
497 stfb
->iface_ID
= stfbi
->ID
;
498 stfb
->iface_stamp
= p_atomic_read(&stfbi
->stamp
) - 1;
500 /* add the color buffer */
501 idx
= stfb
->Base
._ColorDrawBufferIndexes
[0];
502 if (!st_framebuffer_add_renderbuffer(stfb
, idx
, prefer_srgb
)) {
507 st_framebuffer_add_renderbuffer(stfb
, BUFFER_DEPTH
, false);
508 st_framebuffer_add_renderbuffer(stfb
, BUFFER_ACCUM
, false);
511 st_framebuffer_update_attachments(stfb
);
518 * Reference a framebuffer.
521 st_framebuffer_reference(struct st_framebuffer
**ptr
,
522 struct st_framebuffer
*stfb
)
524 struct gl_framebuffer
*fb
= stfb
? &stfb
->Base
: NULL
;
525 _mesa_reference_framebuffer((struct gl_framebuffer
**) ptr
, fb
);
530 st_framebuffer_iface_hash(const void *key
)
532 return (uintptr_t)key
;
537 st_framebuffer_iface_equal(const void *a
, const void *b
)
539 return (struct st_framebuffer_iface
*)a
== (struct st_framebuffer_iface
*)b
;
544 st_framebuffer_iface_lookup(struct st_manager
*smapi
,
545 const struct st_framebuffer_iface
*stfbi
)
547 struct st_manager_private
*smPriv
=
548 (struct st_manager_private
*)smapi
->st_manager_private
;
549 struct hash_entry
*entry
;
552 assert(smPriv
->stfbi_ht
);
554 simple_mtx_lock(&smPriv
->st_mutex
);
555 entry
= _mesa_hash_table_search(smPriv
->stfbi_ht
, stfbi
);
556 simple_mtx_unlock(&smPriv
->st_mutex
);
558 return entry
!= NULL
;
563 st_framebuffer_iface_insert(struct st_manager
*smapi
,
564 struct st_framebuffer_iface
*stfbi
)
566 struct st_manager_private
*smPriv
=
567 (struct st_manager_private
*)smapi
->st_manager_private
;
568 struct hash_entry
*entry
;
571 assert(smPriv
->stfbi_ht
);
573 simple_mtx_lock(&smPriv
->st_mutex
);
574 entry
= _mesa_hash_table_insert(smPriv
->stfbi_ht
, stfbi
, stfbi
);
575 simple_mtx_unlock(&smPriv
->st_mutex
);
577 return entry
!= NULL
;
582 st_framebuffer_iface_remove(struct st_manager
*smapi
,
583 struct st_framebuffer_iface
*stfbi
)
585 struct st_manager_private
*smPriv
=
586 (struct st_manager_private
*)smapi
->st_manager_private
;
587 struct hash_entry
*entry
;
589 if (!smPriv
|| !smPriv
->stfbi_ht
)
592 simple_mtx_lock(&smPriv
->st_mutex
);
593 entry
= _mesa_hash_table_search(smPriv
->stfbi_ht
, stfbi
);
597 _mesa_hash_table_remove(smPriv
->stfbi_ht
, entry
);
600 simple_mtx_unlock(&smPriv
->st_mutex
);
605 * The framebuffer interface object is no longer valid.
606 * Remove the object from the framebuffer interface hash table.
609 st_api_destroy_drawable(struct st_api
*stapi
,
610 struct st_framebuffer_iface
*stfbi
)
615 st_framebuffer_iface_remove(stfbi
->state_manager
, stfbi
);
620 * Purge the winsys buffers list to remove any references to
621 * non-existing framebuffer interface objects.
624 st_framebuffers_purge(struct st_context
*st
)
626 struct st_context_iface
*st_iface
= &st
->iface
;
627 struct st_manager
*smapi
= st_iface
->state_manager
;
628 struct st_framebuffer
*stfb
, *next
;
632 LIST_FOR_EACH_ENTRY_SAFE_REV(stfb
, next
, &st
->winsys_buffers
, head
) {
633 struct st_framebuffer_iface
*stfbi
= stfb
->iface
;
638 * If the corresponding framebuffer interface object no longer exists,
639 * remove the framebuffer object from the context's winsys buffers list,
640 * and unreference the framebuffer object, so its resources can be
643 if (!st_framebuffer_iface_lookup(smapi
, stfbi
)) {
644 list_del(&stfb
->head
);
645 st_framebuffer_reference(&stfb
, NULL
);
652 st_context_flush(struct st_context_iface
*stctxi
, unsigned flags
,
653 struct pipe_fence_handle
**fence
,
654 void (*before_flush_cb
) (void*),
657 struct st_context
*st
= (struct st_context
*) stctxi
;
658 unsigned pipe_flags
= 0;
660 if (flags
& ST_FLUSH_END_OF_FRAME
)
661 pipe_flags
|= PIPE_FLUSH_END_OF_FRAME
;
662 if (flags
& ST_FLUSH_FENCE_FD
)
663 pipe_flags
|= PIPE_FLUSH_FENCE_FD
;
665 /* If both the bitmap cache is dirty and there are unflushed vertices,
666 * it means that glBitmap was called first and then glBegin.
668 st_flush_bitmap_cache(st
);
669 FLUSH_VERTICES(st
->ctx
, 0);
671 /* Notify the caller that we're ready to flush */
673 before_flush_cb(args
);
674 st_flush(st
, fence
, pipe_flags
);
676 if ((flags
& ST_FLUSH_WAIT
) && fence
&& *fence
) {
677 st
->pipe
->screen
->fence_finish(st
->pipe
->screen
, NULL
, *fence
,
678 PIPE_TIMEOUT_INFINITE
);
679 st
->pipe
->screen
->fence_reference(st
->pipe
->screen
, fence
, NULL
);
682 if (flags
& ST_FLUSH_FRONT
)
683 st_manager_flush_frontbuffer(st
);
685 /* DRI3 changes the framebuffer after SwapBuffers, but we need to invoke
686 * st_manager_validate_framebuffers to notice that.
688 * Set gfx_shaders_may_be_dirty to invoke st_validate_state in the next
689 * draw call, which will invoke st_manager_validate_framebuffers, but it
690 * won't dirty states if there is no change.
692 if (flags
& ST_FLUSH_END_OF_FRAME
)
693 st
->gfx_shaders_may_be_dirty
= true;
697 st_context_teximage(struct st_context_iface
*stctxi
,
698 enum st_texture_type tex_type
,
699 int level
, enum pipe_format pipe_format
,
700 struct pipe_resource
*tex
, bool mipmap
)
702 struct st_context
*st
= (struct st_context
*) stctxi
;
703 struct gl_context
*ctx
= st
->ctx
;
704 struct gl_texture_object
*texObj
;
705 struct gl_texture_image
*texImage
;
706 struct st_texture_object
*stObj
;
707 struct st_texture_image
*stImage
;
708 GLenum internalFormat
;
709 GLuint width
, height
, depth
;
714 target
= GL_TEXTURE_1D
;
717 target
= GL_TEXTURE_2D
;
720 target
= GL_TEXTURE_3D
;
722 case ST_TEXTURE_RECT
:
723 target
= GL_TEXTURE_RECTANGLE_ARB
;
729 texObj
= _mesa_get_current_tex_object(ctx
, target
);
731 _mesa_lock_texture(ctx
, texObj
);
733 stObj
= st_texture_object(texObj
);
734 /* switch to surface based */
735 if (!stObj
->surface_based
) {
736 _mesa_clear_texture_object(ctx
, texObj
, NULL
);
737 stObj
->surface_based
= GL_TRUE
;
740 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
741 stImage
= st_texture_image(texImage
);
743 mesa_format texFormat
= st_pipe_format_to_mesa_format(pipe_format
);
745 if (util_format_has_alpha(tex
->format
))
746 internalFormat
= GL_RGBA
;
748 internalFormat
= GL_RGB
;
750 _mesa_init_teximage_fields(ctx
, texImage
,
751 tex
->width0
, tex
->height0
, 1, 0,
752 internalFormat
, texFormat
);
755 height
= tex
->height0
;
758 /* grow the image size until we hit level = 0 */
770 _mesa_clear_texture_image(ctx
, texImage
);
771 width
= height
= depth
= 0;
774 pipe_resource_reference(&stObj
->pt
, tex
);
775 st_texture_release_all_sampler_views(st
, stObj
);
776 pipe_resource_reference(&stImage
->pt
, tex
);
777 stObj
->surface_format
= pipe_format
;
779 stObj
->needs_validation
= true;
781 _mesa_dirty_texobj(ctx
, texObj
);
782 _mesa_unlock_texture(ctx
, texObj
);
789 st_context_copy(struct st_context_iface
*stctxi
,
790 struct st_context_iface
*stsrci
, unsigned mask
)
792 struct st_context
*st
= (struct st_context
*) stctxi
;
793 struct st_context
*src
= (struct st_context
*) stsrci
;
795 _mesa_copy_context(src
->ctx
, st
->ctx
, mask
);
800 st_context_share(struct st_context_iface
*stctxi
,
801 struct st_context_iface
*stsrci
)
803 struct st_context
*st
= (struct st_context
*) stctxi
;
804 struct st_context
*src
= (struct st_context
*) stsrci
;
806 return _mesa_share_state(st
->ctx
, src
->ctx
);
811 st_context_destroy(struct st_context_iface
*stctxi
)
813 struct st_context
*st
= (struct st_context
*) stctxi
;
814 st_destroy_context(st
);
819 st_start_thread(struct st_context_iface
*stctxi
)
821 struct st_context
*st
= (struct st_context
*) stctxi
;
823 _mesa_glthread_init(st
->ctx
);
825 /* Pin all driver threads to one L3 cache for optimal performance
826 * on AMD Zen. This is only done if glthread is enabled.
828 * If glthread is disabled, st_draw.c re-pins driver threads regularly
829 * based on the location of the app thread.
831 struct glthread_state
*glthread
= &st
->ctx
->GLThread
;
832 if (glthread
->enabled
&& st
->pipe
->set_context_param
) {
833 util_pin_driver_threads_to_random_L3(st
->pipe
, &glthread
->queue
.threads
[0]);
839 st_thread_finish(struct st_context_iface
*stctxi
)
841 struct st_context
*st
= (struct st_context
*) stctxi
;
843 _mesa_glthread_finish(st
->ctx
);
848 st_manager_destroy(struct st_manager
*smapi
)
850 struct st_manager_private
*smPriv
= smapi
->st_manager_private
;
852 if (smPriv
&& smPriv
->stfbi_ht
) {
853 _mesa_hash_table_destroy(smPriv
->stfbi_ht
, NULL
);
854 simple_mtx_destroy(&smPriv
->st_mutex
);
856 smapi
->st_manager_private
= NULL
;
861 static struct st_context_iface
*
862 st_api_create_context(struct st_api
*stapi
, struct st_manager
*smapi
,
863 const struct st_context_attribs
*attribs
,
864 enum st_context_error
*error
,
865 struct st_context_iface
*shared_stctxi
)
867 struct st_context
*shared_ctx
= (struct st_context
*) shared_stctxi
;
868 struct st_context
*st
;
869 struct pipe_context
*pipe
;
870 struct gl_config
* mode_ptr
;
871 struct gl_config mode
;
873 bool no_error
= false;
874 unsigned ctx_flags
= PIPE_CONTEXT_PREFER_THREADED
;
876 if (!(stapi
->profile_mask
& (1 << attribs
->profile
)))
879 switch (attribs
->profile
) {
880 case ST_PROFILE_DEFAULT
:
881 api
= API_OPENGL_COMPAT
;
883 case ST_PROFILE_OPENGL_ES1
:
886 case ST_PROFILE_OPENGL_ES2
:
889 case ST_PROFILE_OPENGL_CORE
:
890 api
= API_OPENGL_CORE
;
893 *error
= ST_CONTEXT_ERROR_BAD_API
;
899 /* Create a hash table for the framebuffer interface objects
900 * if it has not been created for this st manager.
902 if (smapi
->st_manager_private
== NULL
) {
903 struct st_manager_private
*smPriv
;
905 smPriv
= CALLOC_STRUCT(st_manager_private
);
906 simple_mtx_init(&smPriv
->st_mutex
, mtx_plain
);
907 smPriv
->stfbi_ht
= _mesa_hash_table_create(NULL
,
908 st_framebuffer_iface_hash
,
909 st_framebuffer_iface_equal
);
910 smapi
->st_manager_private
= smPriv
;
911 smapi
->destroy
= st_manager_destroy
;
914 if (attribs
->flags
& ST_CONTEXT_FLAG_ROBUST_ACCESS
)
915 ctx_flags
|= PIPE_CONTEXT_ROBUST_BUFFER_ACCESS
;
917 if (attribs
->flags
& ST_CONTEXT_FLAG_NO_ERROR
)
920 if (attribs
->flags
& ST_CONTEXT_FLAG_LOW_PRIORITY
)
921 ctx_flags
|= PIPE_CONTEXT_LOW_PRIORITY
;
922 else if (attribs
->flags
& ST_CONTEXT_FLAG_HIGH_PRIORITY
)
923 ctx_flags
|= PIPE_CONTEXT_HIGH_PRIORITY
;
925 if (attribs
->flags
& ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED
)
926 ctx_flags
|= PIPE_CONTEXT_LOSE_CONTEXT_ON_RESET
;
928 pipe
= smapi
->screen
->context_create(smapi
->screen
, NULL
, ctx_flags
);
930 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
934 st_visual_to_context_mode(&attribs
->visual
, &mode
);
936 if (attribs
->visual
.no_config
)
941 st
= st_create_context(api
, pipe
, mode_ptr
, shared_ctx
,
942 &attribs
->options
, no_error
);
944 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
949 if (attribs
->flags
& ST_CONTEXT_FLAG_DEBUG
) {
950 if (!_mesa_set_debug_state_int(st
->ctx
, GL_DEBUG_OUTPUT
, GL_TRUE
)) {
951 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
955 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_DEBUG_BIT
;
958 if (st
->ctx
->Const
.ContextFlags
& GL_CONTEXT_FLAG_DEBUG_BIT
) {
959 st_update_debug_callback(st
);
962 if (attribs
->flags
& ST_CONTEXT_FLAG_FORWARD_COMPATIBLE
)
963 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT
;
964 if (attribs
->flags
& ST_CONTEXT_FLAG_ROBUST_ACCESS
) {
965 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB
;
966 st
->ctx
->Const
.RobustAccess
= GL_TRUE
;
968 if (attribs
->flags
& ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED
) {
969 st
->ctx
->Const
.ResetStrategy
= GL_LOSE_CONTEXT_ON_RESET_ARB
;
970 st_install_device_reset_callback(st
);
973 if (attribs
->flags
& ST_CONTEXT_FLAG_RELEASE_NONE
)
974 st
->ctx
->Const
.ContextReleaseBehavior
= GL_NONE
;
976 /* need to perform version check */
977 if (attribs
->major
> 1 || attribs
->minor
> 0) {
978 /* Is the actual version less than the requested version?
980 if (st
->ctx
->Version
< attribs
->major
* 10U + attribs
->minor
) {
981 *error
= ST_CONTEXT_ERROR_BAD_VERSION
;
982 st_destroy_context(st
);
987 st
->can_scissor_clear
= !!st
->pipe
->screen
->get_param(st
->pipe
->screen
, PIPE_CAP_CLEAR_SCISSORED
);
989 st
->invalidate_on_gl_viewport
=
990 smapi
->get_param(smapi
, ST_MANAGER_BROKEN_INVALIDATE
);
992 st
->iface
.destroy
= st_context_destroy
;
993 st
->iface
.flush
= st_context_flush
;
994 st
->iface
.teximage
= st_context_teximage
;
995 st
->iface
.copy
= st_context_copy
;
996 st
->iface
.share
= st_context_share
;
997 st
->iface
.start_thread
= st_start_thread
;
998 st
->iface
.thread_finish
= st_thread_finish
;
999 st
->iface
.st_context_private
= (void *) smapi
;
1000 st
->iface
.cso_context
= st
->cso_context
;
1001 st
->iface
.pipe
= st
->pipe
;
1002 st
->iface
.state_manager
= smapi
;
1004 *error
= ST_CONTEXT_SUCCESS
;
1009 static struct st_context_iface
*
1010 st_api_get_current(struct st_api
*stapi
)
1012 GET_CURRENT_CONTEXT(ctx
);
1013 struct st_context
*st
= ctx
? ctx
->st
: NULL
;
1015 return st
? &st
->iface
: NULL
;
1019 static struct st_framebuffer
*
1020 st_framebuffer_reuse_or_create(struct st_context
*st
,
1021 struct gl_framebuffer
*fb
,
1022 struct st_framebuffer_iface
*stfbi
)
1024 struct st_framebuffer
*cur
= NULL
, *stfb
= NULL
;
1029 /* Check if there is already a framebuffer object for the specified
1030 * framebuffer interface in this context. If there is one, use it.
1032 LIST_FOR_EACH_ENTRY(cur
, &st
->winsys_buffers
, head
) {
1033 if (cur
->iface_ID
== stfbi
->ID
) {
1034 st_framebuffer_reference(&stfb
, cur
);
1039 /* If there is not already a framebuffer object, create one */
1041 cur
= st_framebuffer_create(st
, stfbi
);
1044 /* add the referenced framebuffer interface object to
1045 * the framebuffer interface object hash table.
1047 if (!st_framebuffer_iface_insert(stfbi
->state_manager
, stfbi
)) {
1048 st_framebuffer_reference(&cur
, NULL
);
1052 /* add to the context's winsys buffers list */
1053 list_add(&cur
->head
, &st
->winsys_buffers
);
1055 st_framebuffer_reference(&stfb
, cur
);
1064 st_api_make_current(struct st_api
*stapi
, struct st_context_iface
*stctxi
,
1065 struct st_framebuffer_iface
*stdrawi
,
1066 struct st_framebuffer_iface
*streadi
)
1068 struct st_context
*st
= (struct st_context
*) stctxi
;
1069 struct st_framebuffer
*stdraw
, *stread
;
1073 /* reuse or create the draw fb */
1074 stdraw
= st_framebuffer_reuse_or_create(st
,
1075 st
->ctx
->WinSysDrawBuffer
, stdrawi
);
1076 if (streadi
!= stdrawi
) {
1077 /* do the same for the read fb */
1078 stread
= st_framebuffer_reuse_or_create(st
,
1079 st
->ctx
->WinSysReadBuffer
, streadi
);
1083 /* reuse the draw fb for the read fb */
1085 st_framebuffer_reference(&stread
, stdraw
);
1088 if (stdraw
&& stread
) {
1089 st_framebuffer_validate(stdraw
, st
);
1090 if (stread
!= stdraw
)
1091 st_framebuffer_validate(stread
, st
);
1093 ret
= _mesa_make_current(st
->ctx
, &stdraw
->Base
, &stread
->Base
);
1095 st
->draw_stamp
= stdraw
->stamp
- 1;
1096 st
->read_stamp
= stread
->stamp
- 1;
1097 st_context_validate(st
, stdraw
, stread
);
1100 struct gl_framebuffer
*incomplete
= _mesa_get_incomplete_framebuffer();
1101 ret
= _mesa_make_current(st
->ctx
, incomplete
, incomplete
);
1104 st_framebuffer_reference(&stdraw
, NULL
);
1105 st_framebuffer_reference(&stread
, NULL
);
1107 /* Purge the context's winsys_buffers list in case any
1108 * of the referenced drawables no longer exist.
1110 st_framebuffers_purge(st
);
1113 GET_CURRENT_CONTEXT(ctx
);
1116 /* Before releasing the context, release its associated
1117 * winsys buffers first. Then purge the context's winsys buffers list
1118 * to free the resources of any winsys buffers that no longer have
1119 * an existing drawable.
1121 ret
= _mesa_make_current(ctx
, NULL
, NULL
);
1122 st_framebuffers_purge(ctx
->st
);
1125 ret
= _mesa_make_current(NULL
, NULL
, NULL
);
1133 st_api_destroy(struct st_api
*stapi
)
1139 * Flush the front buffer if the current context renders to the front buffer.
1142 st_manager_flush_frontbuffer(struct st_context
*st
)
1144 struct st_framebuffer
*stfb
= st_ws_framebuffer(st
->ctx
->DrawBuffer
);
1145 struct st_renderbuffer
*strb
= NULL
;
1150 /* If the context uses a doublebuffered visual, but the buffer is
1151 * single-buffered, guess that it's a pbuffer, which doesn't need
1154 if (st
->ctx
->Visual
.doubleBufferMode
&&
1155 !stfb
->Base
.Visual
.doubleBufferMode
)
1158 strb
= st_renderbuffer(stfb
->Base
.Attachment
[BUFFER_FRONT_LEFT
].
1161 /* Do we have a front color buffer and has it been drawn to since last
1162 * frontbuffer flush?
1164 if (strb
&& strb
->defined
) {
1165 stfb
->iface
->flush_front(&st
->iface
, stfb
->iface
,
1166 ST_ATTACHMENT_FRONT_LEFT
);
1167 strb
->defined
= GL_FALSE
;
1169 /* Trigger an update of strb->defined on next draw */
1170 st
->dirty
|= ST_NEW_FB_STATE
;
1176 * Re-validate the framebuffers.
1179 st_manager_validate_framebuffers(struct st_context
*st
)
1181 struct st_framebuffer
*stdraw
= st_ws_framebuffer(st
->ctx
->DrawBuffer
);
1182 struct st_framebuffer
*stread
= st_ws_framebuffer(st
->ctx
->ReadBuffer
);
1185 st_framebuffer_validate(stdraw
, st
);
1186 if (stread
&& stread
!= stdraw
)
1187 st_framebuffer_validate(stread
, st
);
1189 st_context_validate(st
, stdraw
, stread
);
1194 * Flush any outstanding swapbuffers on the current draw framebuffer.
1197 st_manager_flush_swapbuffers(void)
1199 GET_CURRENT_CONTEXT(ctx
);
1200 struct st_context
*st
= (ctx
) ? ctx
->st
: NULL
;
1201 struct st_framebuffer
*stfb
;
1206 stfb
= st_ws_framebuffer(ctx
->DrawBuffer
);
1207 if (!stfb
|| !stfb
->iface
->flush_swapbuffers
)
1210 stfb
->iface
->flush_swapbuffers(&st
->iface
, stfb
->iface
);
1215 * Add a color renderbuffer on demand. The FBO must correspond to a window,
1216 * not a user-created FBO.
1219 st_manager_add_color_renderbuffer(struct st_context
*st
,
1220 struct gl_framebuffer
*fb
,
1221 gl_buffer_index idx
)
1223 struct st_framebuffer
*stfb
= st_ws_framebuffer(fb
);
1229 assert(_mesa_is_winsys_fbo(fb
));
1231 if (stfb
->Base
.Attachment
[idx
].Renderbuffer
)
1235 case BUFFER_FRONT_LEFT
:
1236 case BUFFER_BACK_LEFT
:
1237 case BUFFER_FRONT_RIGHT
:
1238 case BUFFER_BACK_RIGHT
:
1244 if (!st_framebuffer_add_renderbuffer(stfb
, idx
,
1245 stfb
->Base
.Visual
.sRGBCapable
))
1248 st_framebuffer_update_attachments(stfb
);
1251 * Force a call to the frontend manager to validate the
1252 * new renderbuffer. It might be that there is a window system
1253 * renderbuffer available.
1256 stfb
->iface_stamp
= p_atomic_read(&stfb
->iface
->stamp
) - 1;
1258 st_invalidate_buffers(st
);
1265 get_version(struct pipe_screen
*screen
,
1266 struct st_config_options
*options
, gl_api api
)
1268 struct gl_constants consts
= {0};
1269 struct gl_extensions extensions
= {0};
1272 if (_mesa_override_gl_version_contextless(&consts
, &api
, &version
)) {
1276 _mesa_init_constants(&consts
, api
);
1277 _mesa_init_extensions(&extensions
);
1279 st_init_limits(screen
, &consts
, &extensions
);
1280 st_init_extensions(screen
, &consts
, &extensions
, options
, api
);
1281 version
= _mesa_get_version(&extensions
, &consts
, api
);
1282 free(consts
.SpirVExtensions
);
1288 st_api_query_versions(struct st_api
*stapi
, struct st_manager
*sm
,
1289 struct st_config_options
*options
,
1290 int *gl_core_version
,
1291 int *gl_compat_version
,
1292 int *gl_es1_version
,
1293 int *gl_es2_version
)
1295 *gl_core_version
= get_version(sm
->screen
, options
, API_OPENGL_CORE
);
1296 *gl_compat_version
= get_version(sm
->screen
, options
, API_OPENGL_COMPAT
);
1297 *gl_es1_version
= get_version(sm
->screen
, options
, API_OPENGLES
);
1298 *gl_es2_version
= get_version(sm
->screen
, options
, API_OPENGLES2
);
1302 static const struct st_api st_gl_api
= {
1303 .name
= "Mesa " PACKAGE_VERSION
,
1304 .api
= ST_API_OPENGL
,
1305 .profile_mask
= ST_PROFILE_DEFAULT_MASK
|
1306 ST_PROFILE_OPENGL_CORE_MASK
|
1307 ST_PROFILE_OPENGL_ES1_MASK
|
1308 ST_PROFILE_OPENGL_ES2_MASK
|
1310 .feature_mask
= ST_API_FEATURE_MS_VISUALS_MASK
,
1311 .destroy
= st_api_destroy
,
1312 .query_versions
= st_api_query_versions
,
1313 .create_context
= st_api_create_context
,
1314 .make_current
= st_api_make_current
,
1315 .get_current
= st_api_get_current
,
1316 .destroy_drawable
= st_api_destroy_drawable
,
1321 st_gl_api_create(void)
1323 return (struct st_api
*) &st_gl_api
;