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/framebuffer.h"
33 #include "main/glthread.h"
34 #include "main/texobj.h"
35 #include "main/teximage.h"
36 #include "main/texstate.h"
37 #include "main/errors.h"
38 #include "main/framebuffer.h"
39 #include "main/fbobject.h"
40 #include "main/renderbuffer.h"
41 #include "main/version.h"
42 #include "util/hash_table.h"
43 #include "st_texture.h"
45 #include "st_context.h"
47 #include "st_extensions.h"
48 #include "st_format.h"
49 #include "st_cb_bitmap.h"
50 #include "st_cb_flush.h"
51 #include "st_manager.h"
52 #include "st_sampler_view.h"
55 #include "pipe/p_context.h"
56 #include "pipe/p_screen.h"
57 #include "util/format/u_format.h"
58 #include "util/u_helpers.h"
59 #include "util/u_pointer.h"
60 #include "util/u_inlines.h"
61 #include "util/u_atomic.h"
62 #include "util/u_surface.h"
63 #include "util/list.h"
64 #include "util/u_memory.h"
67 struct st_manager_private
69 struct hash_table *stfbi_ht; /* framebuffer iface objects hash table */
70 simple_mtx_t st_mutex;
74 * Cast wrapper to convert a struct gl_framebuffer to an gl_framebuffer.
75 * Return NULL if the struct gl_framebuffer is a user-created framebuffer.
76 * We'll only return non-null for window system framebuffers.
77 * Note that this function may fail.
79 static inline struct gl_framebuffer *
80 st_ws_framebuffer(struct gl_framebuffer *fb)
82 /* FBO cannot be casted. See st_new_framebuffer */
83 if (fb && _mesa_is_winsys_fbo(fb) &&
84 fb != _mesa_get_incomplete_framebuffer())
90 * Map an attachment to a buffer index.
92 static inline gl_buffer_index
93 attachment_to_buffer_index(enum st_attachment_type statt)
95 gl_buffer_index index;
98 case ST_ATTACHMENT_FRONT_LEFT:
99 index = BUFFER_FRONT_LEFT;
101 case ST_ATTACHMENT_BACK_LEFT:
102 index = BUFFER_BACK_LEFT;
104 case ST_ATTACHMENT_FRONT_RIGHT:
105 index = BUFFER_FRONT_RIGHT;
107 case ST_ATTACHMENT_BACK_RIGHT:
108 index = BUFFER_BACK_RIGHT;
110 case ST_ATTACHMENT_DEPTH_STENCIL:
111 index = BUFFER_DEPTH;
113 case ST_ATTACHMENT_ACCUM:
114 index = BUFFER_ACCUM;
117 index = BUFFER_COUNT;
126 * Map a buffer index to an attachment.
128 static inline enum st_attachment_type
129 buffer_index_to_attachment(gl_buffer_index index)
131 enum st_attachment_type statt;
134 case BUFFER_FRONT_LEFT:
135 statt = ST_ATTACHMENT_FRONT_LEFT;
137 case BUFFER_BACK_LEFT:
138 statt = ST_ATTACHMENT_BACK_LEFT;
140 case BUFFER_FRONT_RIGHT:
141 statt = ST_ATTACHMENT_FRONT_RIGHT;
143 case BUFFER_BACK_RIGHT:
144 statt = ST_ATTACHMENT_BACK_RIGHT;
147 statt = ST_ATTACHMENT_DEPTH_STENCIL;
150 statt = ST_ATTACHMENT_ACCUM;
153 statt = ST_ATTACHMENT_INVALID;
162 * Make sure a context picks up the latest cached state of the
163 * drawables it binds to.
166 st_context_validate(struct st_context *st,
167 struct gl_framebuffer *stdraw,
168 struct gl_framebuffer *stread)
170 if (stdraw && stdraw->stamp != st->draw_stamp) {
171 st->dirty |= ST_NEW_FRAMEBUFFER;
172 _mesa_resize_framebuffer(st->ctx, stdraw,
175 st->draw_stamp = stdraw->stamp;
178 if (stread && stread->stamp != st->read_stamp) {
179 if (stread != stdraw) {
180 st->dirty |= ST_NEW_FRAMEBUFFER;
181 _mesa_resize_framebuffer(st->ctx, stread,
185 st->read_stamp = stread->stamp;
191 st_set_ws_renderbuffer_surface(struct gl_renderbuffer *rb,
192 struct pipe_surface *surf)
194 pipe_surface_reference(&rb->surface_srgb, NULL);
195 pipe_surface_reference(&rb->surface_linear, NULL);
197 if (util_format_is_srgb(surf->format))
198 pipe_surface_reference(&rb->surface_srgb, surf);
200 pipe_surface_reference(&rb->surface_linear, surf);
202 rb->surface = surf; /* just assign, don't ref */
203 pipe_resource_reference(&rb->texture, surf->texture);
205 rb->Width = surf->width;
206 rb->Height = surf->height;
211 * Validate a framebuffer to make sure up-to-date pipe_textures are used.
212 * The context is only used for creating pipe surfaces and for calling
213 * _mesa_resize_framebuffer().
214 * (That should probably be rethought, since those surfaces become
215 * drawable state, not context state, and can be freed by another pipe
219 st_framebuffer_validate(struct gl_framebuffer *stfb,
220 struct st_context *st)
222 struct pipe_resource *textures[ST_ATTACHMENT_COUNT];
225 bool changed = false;
228 new_stamp = p_atomic_read(&stfb->iface->stamp);
229 if (stfb->iface_stamp == new_stamp)
232 memset(textures, 0, stfb->num_statts * sizeof(textures[0]));
234 /* validate the fb */
236 if (!stfb->iface->validate(&st->iface, stfb->iface, stfb->statts,
237 stfb->num_statts, textures))
240 stfb->iface_stamp = new_stamp;
241 new_stamp = p_atomic_read(&stfb->iface->stamp);
242 } while(stfb->iface_stamp != new_stamp);
245 height = stfb->Height;
247 for (i = 0; i < stfb->num_statts; i++) {
248 struct gl_renderbuffer *rb;
249 struct pipe_surface *ps, surf_tmpl;
255 idx = attachment_to_buffer_index(stfb->statts[i]);
256 if (idx >= BUFFER_COUNT) {
257 pipe_resource_reference(&textures[i], NULL);
261 rb = stfb->Attachment[idx].Renderbuffer;
263 if (rb->texture == textures[i] &&
264 rb->Width == textures[i]->width0 &&
265 rb->Height == textures[i]->height0) {
266 pipe_resource_reference(&textures[i], NULL);
270 u_surface_default_template(&surf_tmpl, textures[i]);
271 ps = st->pipe->create_surface(st->pipe, textures[i], &surf_tmpl);
273 st_set_ws_renderbuffer_surface(rb, ps);
274 pipe_surface_reference(&ps, NULL);
282 pipe_resource_reference(&textures[i], NULL);
287 _mesa_resize_framebuffer(st->ctx, stfb, width, height);
292 * Return true if the visual has the specified buffers.
295 st_visual_have_buffers(const struct st_visual *visual, unsigned mask)
297 return ((visual->buffer_mask & mask) == mask);
301 * Update the attachments to validate by looping the existing renderbuffers.
304 st_framebuffer_update_attachments(struct gl_framebuffer *stfb)
308 stfb->num_statts = 0;
310 for (enum st_attachment_type i = 0; i < ST_ATTACHMENT_COUNT; i++)
311 stfb->statts[i] = ST_ATTACHMENT_INVALID;
313 for (idx = 0; idx < BUFFER_COUNT; idx++) {
314 struct gl_renderbuffer *rb;
315 enum st_attachment_type statt;
317 rb = stfb->Attachment[idx].Renderbuffer;
318 if (!rb || rb->software)
321 statt = buffer_index_to_attachment(idx);
322 if (statt != ST_ATTACHMENT_INVALID &&
323 st_visual_have_buffers(stfb->iface->visual, 1 << statt))
324 stfb->statts[stfb->num_statts++] = statt;
330 * Allocate a renderbuffer for an on-screen window (not a user-created
331 * renderbuffer). The window system code determines the format.
333 static struct gl_renderbuffer *
334 st_new_renderbuffer_fb(enum pipe_format format, unsigned samples, boolean sw)
336 struct gl_renderbuffer *rb;
338 rb = CALLOC_STRUCT(gl_renderbuffer);
340 _mesa_error(NULL, GL_OUT_OF_MEMORY, "creating renderbuffer");
344 _mesa_init_renderbuffer(rb, 0);
345 rb->ClassID = 0x4242; /* just a unique value */
346 rb->NumSamples = samples;
347 rb->NumStorageSamples = samples;
348 rb->Format = st_pipe_format_to_mesa_format(format);
349 rb->_BaseFormat = _mesa_get_format_base_format(rb->Format);
353 case PIPE_FORMAT_B10G10R10A2_UNORM:
354 case PIPE_FORMAT_R10G10B10A2_UNORM:
355 rb->InternalFormat = GL_RGB10_A2;
357 case PIPE_FORMAT_R10G10B10X2_UNORM:
358 case PIPE_FORMAT_B10G10R10X2_UNORM:
359 rb->InternalFormat = GL_RGB10;
361 case PIPE_FORMAT_R8G8B8A8_UNORM:
362 case PIPE_FORMAT_B8G8R8A8_UNORM:
363 case PIPE_FORMAT_A8R8G8B8_UNORM:
364 rb->InternalFormat = GL_RGBA8;
366 case PIPE_FORMAT_R8G8B8X8_UNORM:
367 case PIPE_FORMAT_B8G8R8X8_UNORM:
368 case PIPE_FORMAT_X8R8G8B8_UNORM:
369 case PIPE_FORMAT_R8G8B8_UNORM:
370 rb->InternalFormat = GL_RGB8;
372 case PIPE_FORMAT_R8G8B8A8_SRGB:
373 case PIPE_FORMAT_B8G8R8A8_SRGB:
374 case PIPE_FORMAT_A8R8G8B8_SRGB:
375 rb->InternalFormat = GL_SRGB8_ALPHA8;
377 case PIPE_FORMAT_R8G8B8X8_SRGB:
378 case PIPE_FORMAT_B8G8R8X8_SRGB:
379 case PIPE_FORMAT_X8R8G8B8_SRGB:
380 rb->InternalFormat = GL_SRGB8;
382 case PIPE_FORMAT_B5G5R5A1_UNORM:
383 rb->InternalFormat = GL_RGB5_A1;
385 case PIPE_FORMAT_B4G4R4A4_UNORM:
386 rb->InternalFormat = GL_RGBA4;
388 case PIPE_FORMAT_B5G6R5_UNORM:
389 rb->InternalFormat = GL_RGB565;
391 case PIPE_FORMAT_Z16_UNORM:
392 rb->InternalFormat = GL_DEPTH_COMPONENT16;
394 case PIPE_FORMAT_Z32_UNORM:
395 rb->InternalFormat = GL_DEPTH_COMPONENT32;
397 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
398 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
399 rb->InternalFormat = GL_DEPTH24_STENCIL8_EXT;
401 case PIPE_FORMAT_Z24X8_UNORM:
402 case PIPE_FORMAT_X8Z24_UNORM:
403 rb->InternalFormat = GL_DEPTH_COMPONENT24;
405 case PIPE_FORMAT_S8_UINT:
406 rb->InternalFormat = GL_STENCIL_INDEX8_EXT;
408 case PIPE_FORMAT_R16G16B16A16_SNORM:
410 rb->InternalFormat = GL_RGBA16_SNORM;
412 case PIPE_FORMAT_R16G16B16A16_UNORM:
413 rb->InternalFormat = GL_RGBA16;
415 case PIPE_FORMAT_R16G16B16_UNORM:
416 rb->InternalFormat = GL_RGB16;
418 case PIPE_FORMAT_R8_UNORM:
419 rb->InternalFormat = GL_R8;
421 case PIPE_FORMAT_R8G8_UNORM:
422 rb->InternalFormat = GL_RG8;
424 case PIPE_FORMAT_R16_UNORM:
425 rb->InternalFormat = GL_R16;
427 case PIPE_FORMAT_R16G16_UNORM:
428 rb->InternalFormat = GL_RG16;
430 case PIPE_FORMAT_R32G32B32A32_FLOAT:
431 rb->InternalFormat = GL_RGBA32F;
433 case PIPE_FORMAT_R32G32B32X32_FLOAT:
434 case PIPE_FORMAT_R32G32B32_FLOAT:
435 rb->InternalFormat = GL_RGB32F;
437 case PIPE_FORMAT_R16G16B16A16_FLOAT:
438 rb->InternalFormat = GL_RGBA16F;
440 case PIPE_FORMAT_R16G16B16X16_FLOAT:
441 rb->InternalFormat = GL_RGB16F;
445 "Unexpected format %s in st_new_renderbuffer_fb",
446 util_format_name(format));
457 * Add a renderbuffer to the framebuffer. The framebuffer is one that
458 * corresponds to a window and is not a user-created FBO.
461 st_framebuffer_add_renderbuffer(struct gl_framebuffer *stfb,
462 gl_buffer_index idx, bool prefer_srgb)
464 struct gl_renderbuffer *rb;
465 enum pipe_format format;
468 assert(_mesa_is_winsys_fbo(stfb));
470 /* do not distinguish depth/stencil buffers */
471 if (idx == BUFFER_STENCIL)
476 format = stfb->iface->visual->depth_stencil_format;
480 format = stfb->iface->visual->accum_format;
484 format = stfb->iface->visual->color_format;
486 format = util_format_srgb(format);
491 if (format == PIPE_FORMAT_NONE)
494 rb = st_new_renderbuffer_fb(format, stfb->iface->visual->samples, sw);
498 if (idx != BUFFER_DEPTH) {
499 _mesa_attach_and_own_rb(stfb, idx, rb);
503 bool rb_ownership_taken = false;
504 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 0)) {
505 _mesa_attach_and_own_rb(stfb, BUFFER_DEPTH, rb);
506 rb_ownership_taken = true;
509 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 1)) {
510 if (rb_ownership_taken)
511 _mesa_attach_and_reference_rb(stfb, BUFFER_STENCIL, rb);
513 _mesa_attach_and_own_rb(stfb, BUFFER_STENCIL, rb);
521 * Intialize a struct gl_config from a visual.
524 st_visual_to_context_mode(const struct st_visual *visual,
525 struct gl_config *mode)
527 memset(mode, 0, sizeof(*mode));
529 if (st_visual_have_buffers(visual, ST_ATTACHMENT_BACK_LEFT_MASK))
530 mode->doubleBufferMode = GL_TRUE;
532 if (st_visual_have_buffers(visual,
533 ST_ATTACHMENT_FRONT_RIGHT_MASK | ST_ATTACHMENT_BACK_RIGHT_MASK))
534 mode->stereoMode = GL_TRUE;
536 if (visual->color_format != PIPE_FORMAT_NONE) {
538 util_format_get_component_bits(visual->color_format,
539 UTIL_FORMAT_COLORSPACE_RGB, 0);
541 util_format_get_component_bits(visual->color_format,
542 UTIL_FORMAT_COLORSPACE_RGB, 1);
544 util_format_get_component_bits(visual->color_format,
545 UTIL_FORMAT_COLORSPACE_RGB, 2);
547 util_format_get_component_bits(visual->color_format,
548 UTIL_FORMAT_COLORSPACE_RGB, 3);
550 mode->rgbBits = mode->redBits +
551 mode->greenBits + mode->blueBits + mode->alphaBits;
552 mode->sRGBCapable = util_format_is_srgb(visual->color_format);
555 if (visual->depth_stencil_format != PIPE_FORMAT_NONE) {
557 util_format_get_component_bits(visual->depth_stencil_format,
558 UTIL_FORMAT_COLORSPACE_ZS, 0);
560 util_format_get_component_bits(visual->depth_stencil_format,
561 UTIL_FORMAT_COLORSPACE_ZS, 1);
564 if (visual->accum_format != PIPE_FORMAT_NONE) {
566 util_format_get_component_bits(visual->accum_format,
567 UTIL_FORMAT_COLORSPACE_RGB, 0);
568 mode->accumGreenBits =
569 util_format_get_component_bits(visual->accum_format,
570 UTIL_FORMAT_COLORSPACE_RGB, 1);
571 mode->accumBlueBits =
572 util_format_get_component_bits(visual->accum_format,
573 UTIL_FORMAT_COLORSPACE_RGB, 2);
574 mode->accumAlphaBits =
575 util_format_get_component_bits(visual->accum_format,
576 UTIL_FORMAT_COLORSPACE_RGB, 3);
579 if (visual->samples > 1) {
580 mode->samples = visual->samples;
586 * Create a framebuffer from a manager interface.
588 static struct gl_framebuffer *
589 st_framebuffer_create(struct st_context *st,
590 struct st_framebuffer_iface *stfbi)
592 struct gl_framebuffer *stfb;
593 struct gl_config mode;
595 bool prefer_srgb = false;
600 stfb = CALLOC_STRUCT(gl_framebuffer);
604 st_visual_to_context_mode(stfbi->visual, &mode);
607 * For desktop GL, sRGB framebuffer write is controlled by both the
608 * capability of the framebuffer and GL_FRAMEBUFFER_SRGB. We should
609 * advertise the capability when the pipe driver (and core Mesa) supports
610 * it so that applications can enable sRGB write when they want to.
612 * This is not to be confused with GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB. When
613 * the attribute is GLX_TRUE, it tells the st manager to pick a color
614 * format such that util_format_srgb(visual->color_format) can be supported
615 * by the pipe driver. We still need to advertise the capability here.
617 * For GLES, however, sRGB framebuffer write is initially only controlled
618 * by the capability of the framebuffer, with GL_EXT_sRGB_write_control
619 * control is given back to the applications, but GL_FRAMEBUFFER_SRGB is
620 * still enabled by default since this is the behaviour when
621 * EXT_sRGB_write_control is not available. Since GL_EXT_sRGB_write_control
622 * brings GLES on par with desktop GLs EXT_framebuffer_sRGB, in mesa this
623 * is also expressed by using the same extension flag
625 if (_mesa_has_EXT_framebuffer_sRGB(st->ctx)) {
626 struct pipe_screen *screen = st->screen;
627 const enum pipe_format srgb_format =
628 util_format_srgb(stfbi->visual->color_format);
630 if (srgb_format != PIPE_FORMAT_NONE &&
631 st_pipe_format_to_mesa_format(srgb_format) != MESA_FORMAT_NONE &&
632 screen->is_format_supported(screen, srgb_format,
633 PIPE_TEXTURE_2D, stfbi->visual->samples,
634 stfbi->visual->samples,
635 (PIPE_BIND_DISPLAY_TARGET |
636 PIPE_BIND_RENDER_TARGET))) {
637 mode.sRGBCapable = GL_TRUE;
638 /* Since GL_FRAMEBUFFER_SRGB is enabled by default on GLES we must not
639 * create renderbuffers with an sRGB format derived from the
640 * visual->color_format, but we still want sRGB for desktop GL.
642 prefer_srgb = _mesa_is_desktop_gl(st->ctx);
646 _mesa_initialize_window_framebuffer(stfb, &mode);
649 stfb->iface_ID = stfbi->ID;
650 stfb->iface_stamp = p_atomic_read(&stfbi->stamp) - 1;
652 /* add the color buffer */
653 idx = stfb->_ColorDrawBufferIndexes[0];
654 if (!st_framebuffer_add_renderbuffer(stfb, idx, prefer_srgb)) {
659 st_framebuffer_add_renderbuffer(stfb, BUFFER_DEPTH, false);
660 st_framebuffer_add_renderbuffer(stfb, BUFFER_ACCUM, false);
663 st_framebuffer_update_attachments(stfb);
670 st_framebuffer_iface_hash(const void *key)
672 return (uintptr_t)key;
677 st_framebuffer_iface_equal(const void *a, const void *b)
679 return (struct st_framebuffer_iface *)a == (struct st_framebuffer_iface *)b;
684 st_framebuffer_iface_lookup(struct st_manager *smapi,
685 const struct st_framebuffer_iface *stfbi)
687 struct st_manager_private *smPriv =
688 (struct st_manager_private *)smapi->st_manager_private;
689 struct hash_entry *entry;
692 assert(smPriv->stfbi_ht);
694 simple_mtx_lock(&smPriv->st_mutex);
695 entry = _mesa_hash_table_search(smPriv->stfbi_ht, stfbi);
696 simple_mtx_unlock(&smPriv->st_mutex);
698 return entry != NULL;
703 st_framebuffer_iface_insert(struct st_manager *smapi,
704 struct st_framebuffer_iface *stfbi)
706 struct st_manager_private *smPriv =
707 (struct st_manager_private *)smapi->st_manager_private;
708 struct hash_entry *entry;
711 assert(smPriv->stfbi_ht);
713 simple_mtx_lock(&smPriv->st_mutex);
714 entry = _mesa_hash_table_insert(smPriv->stfbi_ht, stfbi, stfbi);
715 simple_mtx_unlock(&smPriv->st_mutex);
717 return entry != NULL;
722 st_framebuffer_iface_remove(struct st_manager *smapi,
723 struct st_framebuffer_iface *stfbi)
725 struct st_manager_private *smPriv =
726 (struct st_manager_private *)smapi->st_manager_private;
727 struct hash_entry *entry;
729 if (!smPriv || !smPriv->stfbi_ht)
732 simple_mtx_lock(&smPriv->st_mutex);
733 entry = _mesa_hash_table_search(smPriv->stfbi_ht, stfbi);
737 _mesa_hash_table_remove(smPriv->stfbi_ht, entry);
740 simple_mtx_unlock(&smPriv->st_mutex);
745 * The framebuffer interface object is no longer valid.
746 * Remove the object from the framebuffer interface hash table.
749 st_api_destroy_drawable(struct st_framebuffer_iface *stfbi)
754 st_framebuffer_iface_remove(stfbi->state_manager, stfbi);
759 * Purge the winsys buffers list to remove any references to
760 * non-existing framebuffer interface objects.
763 st_framebuffers_purge(struct st_context *st)
765 struct st_context_iface *st_iface = &st->iface;
766 struct st_manager *smapi = st_iface->state_manager;
767 struct gl_framebuffer *stfb, *next;
771 LIST_FOR_EACH_ENTRY_SAFE_REV(stfb, next, &st->winsys_buffers, head) {
772 struct st_framebuffer_iface *stfbi = stfb->iface;
777 * If the corresponding framebuffer interface object no longer exists,
778 * remove the framebuffer object from the context's winsys buffers list,
779 * and unreference the framebuffer object, so its resources can be
782 if (!st_framebuffer_iface_lookup(smapi, stfbi)) {
783 list_del(&stfb->head);
784 _mesa_reference_framebuffer(&stfb, NULL);
791 st_context_flush(struct st_context_iface *stctxi, unsigned flags,
792 struct pipe_fence_handle **fence,
793 void (*before_flush_cb) (void*),
796 struct st_context *st = (struct st_context *) stctxi;
797 unsigned pipe_flags = 0;
799 if (flags & ST_FLUSH_END_OF_FRAME)
800 pipe_flags |= PIPE_FLUSH_END_OF_FRAME;
801 if (flags & ST_FLUSH_FENCE_FD)
802 pipe_flags |= PIPE_FLUSH_FENCE_FD;
804 /* We can do these in any order because FLUSH_VERTICES will also flush
805 * the bitmap cache if there are any unflushed vertices.
807 st_flush_bitmap_cache(st);
808 FLUSH_VERTICES(st->ctx, 0, 0);
810 /* Notify the caller that we're ready to flush */
812 before_flush_cb(args);
813 st_flush(st, fence, pipe_flags);
815 if ((flags & ST_FLUSH_WAIT) && fence && *fence) {
816 st->screen->fence_finish(st->screen, NULL, *fence,
817 PIPE_TIMEOUT_INFINITE);
818 st->screen->fence_reference(st->screen, fence, NULL);
821 if (flags & ST_FLUSH_FRONT)
822 st_manager_flush_frontbuffer(st);
824 /* DRI3 changes the framebuffer after SwapBuffers, but we need to invoke
825 * st_manager_validate_framebuffers to notice that.
827 * Set gfx_shaders_may_be_dirty to invoke st_validate_state in the next
828 * draw call, which will invoke st_manager_validate_framebuffers, but it
829 * won't dirty states if there is no change.
831 if (flags & ST_FLUSH_END_OF_FRAME)
832 st->gfx_shaders_may_be_dirty = true;
835 /* This is only for GLX_EXT_texture_from_pixmap and equivalent features
839 st_context_teximage(struct st_context_iface *stctxi,
840 enum st_texture_type tex_type,
841 int level, enum pipe_format pipe_format,
842 struct pipe_resource *tex, bool mipmap)
844 struct st_context *st = (struct st_context *) stctxi;
845 struct gl_context *ctx = st->ctx;
846 struct gl_texture_object *texObj;
847 struct gl_texture_image *texImage;
848 GLenum internalFormat;
849 GLuint width, height, depth;
854 target = GL_TEXTURE_1D;
857 target = GL_TEXTURE_2D;
860 target = GL_TEXTURE_3D;
862 case ST_TEXTURE_RECT:
863 target = GL_TEXTURE_RECTANGLE_ARB;
869 texObj = _mesa_get_current_tex_object(ctx, target);
871 _mesa_lock_texture(ctx, texObj);
873 /* switch to surface based */
874 if (!texObj->surface_based) {
875 _mesa_clear_texture_object(ctx, texObj, NULL);
876 texObj->surface_based = GL_TRUE;
879 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
881 mesa_format texFormat = st_pipe_format_to_mesa_format(pipe_format);
883 if (util_format_has_alpha(tex->format))
884 internalFormat = GL_RGBA;
886 internalFormat = GL_RGB;
888 _mesa_init_teximage_fields(ctx, texImage,
889 tex->width0, tex->height0, 1, 0,
890 internalFormat, texFormat);
893 height = tex->height0;
896 /* grow the image size until we hit level = 0 */
908 _mesa_clear_texture_image(ctx, texImage);
909 width = height = depth = 0;
911 _mesa_update_texture_object_swizzle(ctx, texObj);
913 pipe_resource_reference(&texObj->pt, tex);
914 st_texture_release_all_sampler_views(st, texObj);
915 pipe_resource_reference(&texImage->pt, tex);
916 texObj->surface_format = pipe_format;
918 texObj->needs_validation = true;
920 _mesa_dirty_texobj(ctx, texObj);
921 ctx->Shared->HasExternallySharedImages = true;
922 _mesa_unlock_texture(ctx, texObj);
929 st_context_copy(struct st_context_iface *stctxi,
930 struct st_context_iface *stsrci, unsigned mask)
932 struct st_context *st = (struct st_context *) stctxi;
933 struct st_context *src = (struct st_context *) stsrci;
935 _mesa_copy_context(src->ctx, st->ctx, mask);
940 st_context_share(struct st_context_iface *stctxi,
941 struct st_context_iface *stsrci)
943 struct st_context *st = (struct st_context *) stctxi;
944 struct st_context *src = (struct st_context *) stsrci;
946 return _mesa_share_state(st->ctx, src->ctx);
951 st_context_destroy(struct st_context_iface *stctxi)
953 struct st_context *st = (struct st_context *) stctxi;
954 st_destroy_context(st);
959 st_start_thread(struct st_context_iface *stctxi)
961 struct st_context *st = (struct st_context *) stctxi;
963 _mesa_glthread_init(st->ctx);
968 st_thread_finish(struct st_context_iface *stctxi)
970 struct st_context *st = (struct st_context *) stctxi;
972 _mesa_glthread_finish(st->ctx);
977 st_context_invalidate_state(struct st_context_iface *stctxi,
980 struct st_context *st = (struct st_context *) stctxi;
982 if (flags & ST_INVALIDATE_FS_SAMPLER_VIEWS)
983 st->dirty |= ST_NEW_FS_SAMPLER_VIEWS;
984 if (flags & ST_INVALIDATE_FS_CONSTBUF0)
985 st->dirty |= ST_NEW_FS_CONSTANTS;
986 if (flags & ST_INVALIDATE_VS_CONSTBUF0)
987 st->dirty |= ST_NEW_VS_CONSTANTS;
988 if (flags & ST_INVALIDATE_VERTEX_BUFFERS) {
989 st->ctx->Array.NewVertexElements = true;
990 st->dirty |= ST_NEW_VERTEX_ARRAYS;
996 st_manager_destroy(struct st_manager *smapi)
998 struct st_manager_private *smPriv = smapi->st_manager_private;
1000 if (smPriv && smPriv->stfbi_ht) {
1001 _mesa_hash_table_destroy(smPriv->stfbi_ht, NULL);
1002 simple_mtx_destroy(&smPriv->st_mutex);
1004 smapi->st_manager_private = NULL;
1009 struct st_context_iface *
1010 st_api_create_context(struct st_manager *smapi,
1011 const struct st_context_attribs *attribs,
1012 enum st_context_error *error,
1013 struct st_context_iface *shared_stctxi)
1015 struct st_context *shared_ctx = (struct st_context *) shared_stctxi;
1016 struct st_context *st;
1017 struct pipe_context *pipe;
1018 struct gl_config mode, *mode_ptr = &mode;
1020 bool no_error = false;
1021 unsigned ctx_flags = PIPE_CONTEXT_PREFER_THREADED;
1023 if (!(ST_PROFILE_ALL_MASK & (1 << attribs->profile)))
1026 switch (attribs->profile) {
1027 case ST_PROFILE_DEFAULT:
1028 api = API_OPENGL_COMPAT;
1030 case ST_PROFILE_OPENGL_ES1:
1033 case ST_PROFILE_OPENGL_ES2:
1034 api = API_OPENGLES2;
1036 case ST_PROFILE_OPENGL_CORE:
1037 api = API_OPENGL_CORE;
1040 *error = ST_CONTEXT_ERROR_BAD_API;
1044 _mesa_initialize(attribs->options.mesa_extension_override);
1046 /* Create a hash table for the framebuffer interface objects
1047 * if it has not been created for this st manager.
1049 if (smapi->st_manager_private == NULL) {
1050 struct st_manager_private *smPriv;
1052 smPriv = CALLOC_STRUCT(st_manager_private);
1053 simple_mtx_init(&smPriv->st_mutex, mtx_plain);
1054 smPriv->stfbi_ht = _mesa_hash_table_create(NULL,
1055 st_framebuffer_iface_hash,
1056 st_framebuffer_iface_equal);
1057 smapi->st_manager_private = smPriv;
1058 smapi->destroy = st_manager_destroy;
1061 if (attribs->flags & ST_CONTEXT_FLAG_ROBUST_ACCESS)
1062 ctx_flags |= PIPE_CONTEXT_ROBUST_BUFFER_ACCESS;
1064 if (attribs->flags & ST_CONTEXT_FLAG_NO_ERROR)
1067 if (attribs->flags & ST_CONTEXT_FLAG_LOW_PRIORITY)
1068 ctx_flags |= PIPE_CONTEXT_LOW_PRIORITY;
1069 else if (attribs->flags & ST_CONTEXT_FLAG_HIGH_PRIORITY)
1070 ctx_flags |= PIPE_CONTEXT_HIGH_PRIORITY;
1072 if (attribs->flags & ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED)
1073 ctx_flags |= PIPE_CONTEXT_LOSE_CONTEXT_ON_RESET;
1075 if (attribs->flags & ST_CONTEXT_FLAG_PROTECTED)
1076 ctx_flags |= PIPE_CONTEXT_PROTECTED;
1078 pipe = smapi->screen->context_create(smapi->screen, NULL, ctx_flags);
1080 *error = ST_CONTEXT_ERROR_NO_MEMORY;
1084 st_visual_to_context_mode(&attribs->visual, &mode);
1085 if (attribs->visual.color_format == PIPE_FORMAT_NONE)
1087 st = st_create_context(api, pipe, mode_ptr, shared_ctx,
1088 &attribs->options, no_error,
1089 !!smapi->validate_egl_image);
1091 *error = ST_CONTEXT_ERROR_NO_MEMORY;
1092 pipe->destroy(pipe);
1096 if (attribs->flags & ST_CONTEXT_FLAG_DEBUG) {
1097 if (!_mesa_set_debug_state_int(st->ctx, GL_DEBUG_OUTPUT, GL_TRUE)) {
1098 *error = ST_CONTEXT_ERROR_NO_MEMORY;
1102 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_DEBUG_BIT;
1105 if (st->ctx->Const.ContextFlags & GL_CONTEXT_FLAG_DEBUG_BIT) {
1106 _mesa_update_debug_callback(st->ctx);
1109 if (attribs->flags & ST_CONTEXT_FLAG_FORWARD_COMPATIBLE)
1110 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
1111 if (attribs->flags & ST_CONTEXT_FLAG_ROBUST_ACCESS) {
1112 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB;
1113 st->ctx->Const.RobustAccess = GL_TRUE;
1115 if (attribs->flags & ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED) {
1116 st->ctx->Const.ResetStrategy = GL_LOSE_CONTEXT_ON_RESET_ARB;
1117 st_install_device_reset_callback(st);
1120 if (attribs->flags & ST_CONTEXT_FLAG_RELEASE_NONE)
1121 st->ctx->Const.ContextReleaseBehavior = GL_NONE;
1123 /* need to perform version check */
1124 if (attribs->major > 1 || attribs->minor > 0) {
1125 /* Is the actual version less than the requested version?
1127 if (st->ctx->Version < attribs->major * 10U + attribs->minor) {
1128 *error = ST_CONTEXT_ERROR_BAD_VERSION;
1129 st_destroy_context(st);
1134 st->can_scissor_clear = !!st->screen->get_param(st->screen, PIPE_CAP_CLEAR_SCISSORED);
1136 st->ctx->invalidate_on_gl_viewport =
1137 smapi->get_param(smapi, ST_MANAGER_BROKEN_INVALIDATE);
1139 st->iface.destroy = st_context_destroy;
1140 st->iface.flush = st_context_flush;
1141 st->iface.teximage = st_context_teximage;
1142 st->iface.copy = st_context_copy;
1143 st->iface.share = st_context_share;
1144 st->iface.start_thread = st_start_thread;
1145 st->iface.thread_finish = st_thread_finish;
1146 st->iface.invalidate_state = st_context_invalidate_state;
1147 st->iface.st_context_private = (void *) smapi;
1148 st->iface.cso_context = st->cso_context;
1149 st->iface.pipe = st->pipe;
1150 st->iface.state_manager = smapi;
1152 if (st->ctx->IntelBlackholeRender &&
1153 st->screen->get_param(st->screen, PIPE_CAP_FRONTEND_NOOP))
1154 st->pipe->set_frontend_noop(st->pipe, st->ctx->IntelBlackholeRender);
1156 *error = ST_CONTEXT_SUCCESS;
1161 struct st_context_iface *
1162 st_api_get_current(void)
1164 GET_CURRENT_CONTEXT(ctx);
1165 struct st_context *st = ctx ? ctx->st : NULL;
1167 return st ? &st->iface : NULL;
1171 static struct gl_framebuffer *
1172 st_framebuffer_reuse_or_create(struct st_context *st,
1173 struct gl_framebuffer *fb,
1174 struct st_framebuffer_iface *stfbi)
1176 struct gl_framebuffer *cur = NULL, *stfb = NULL;
1181 /* Check if there is already a framebuffer object for the specified
1182 * framebuffer interface in this context. If there is one, use it.
1184 LIST_FOR_EACH_ENTRY(cur, &st->winsys_buffers, head) {
1185 if (cur->iface_ID == stfbi->ID) {
1186 _mesa_reference_framebuffer(&stfb, cur);
1191 /* If there is not already a framebuffer object, create one */
1193 cur = st_framebuffer_create(st, stfbi);
1196 /* add the referenced framebuffer interface object to
1197 * the framebuffer interface object hash table.
1199 if (!st_framebuffer_iface_insert(stfbi->state_manager, stfbi)) {
1200 _mesa_reference_framebuffer(&cur, NULL);
1204 /* add to the context's winsys buffers list */
1205 list_add(&cur->head, &st->winsys_buffers);
1207 _mesa_reference_framebuffer(&stfb, cur);
1216 st_api_make_current(struct st_context_iface *stctxi,
1217 struct st_framebuffer_iface *stdrawi,
1218 struct st_framebuffer_iface *streadi)
1220 struct st_context *st = (struct st_context *) stctxi;
1221 struct gl_framebuffer *stdraw, *stread;
1225 /* reuse or create the draw fb */
1226 stdraw = st_framebuffer_reuse_or_create(st,
1227 st->ctx->WinSysDrawBuffer, stdrawi);
1228 if (streadi != stdrawi) {
1229 /* do the same for the read fb */
1230 stread = st_framebuffer_reuse_or_create(st,
1231 st->ctx->WinSysReadBuffer, streadi);
1235 /* reuse the draw fb for the read fb */
1237 _mesa_reference_framebuffer(&stread, stdraw);
1240 /* If framebuffers were asked for, we'd better have allocated them */
1241 if ((stdrawi && !stdraw) || (streadi && !stread))
1244 if (stdraw && stread) {
1245 st_framebuffer_validate(stdraw, st);
1246 if (stread != stdraw)
1247 st_framebuffer_validate(stread, st);
1249 ret = _mesa_make_current(st->ctx, stdraw, stread);
1251 st->draw_stamp = stdraw->stamp - 1;
1252 st->read_stamp = stread->stamp - 1;
1253 st_context_validate(st, stdraw, stread);
1256 struct gl_framebuffer *incomplete = _mesa_get_incomplete_framebuffer();
1257 ret = _mesa_make_current(st->ctx, incomplete, incomplete);
1260 _mesa_reference_framebuffer(&stdraw, NULL);
1261 _mesa_reference_framebuffer(&stread, NULL);
1263 /* Purge the context's winsys_buffers list in case any
1264 * of the referenced drawables no longer exist.
1266 st_framebuffers_purge(st);
1269 GET_CURRENT_CONTEXT(ctx);
1272 /* Before releasing the context, release its associated
1273 * winsys buffers first. Then purge the context's winsys buffers list
1274 * to free the resources of any winsys buffers that no longer have
1275 * an existing drawable.
1277 ret = _mesa_make_current(ctx, NULL, NULL);
1278 st_framebuffers_purge(ctx->st);
1281 ret = _mesa_make_current(NULL, NULL, NULL);
1289 * Flush the front buffer if the current context renders to the front buffer.
1292 st_manager_flush_frontbuffer(struct st_context *st)
1294 struct gl_framebuffer *stfb = st_ws_framebuffer(st->ctx->DrawBuffer);
1295 struct gl_renderbuffer *rb = NULL;
1300 /* If the context uses a doublebuffered visual, but the buffer is
1301 * single-buffered, guess that it's a pbuffer, which doesn't need
1304 if (st->ctx->Visual.doubleBufferMode &&
1305 !stfb->Visual.doubleBufferMode)
1308 /* Check front buffer used at the GL API level. */
1309 enum st_attachment_type statt = ST_ATTACHMENT_FRONT_LEFT;
1310 rb = stfb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer;
1312 /* Check back buffer redirected by EGL_KHR_mutable_render_buffer. */
1313 statt = ST_ATTACHMENT_BACK_LEFT;
1314 rb = stfb->Attachment[BUFFER_BACK_LEFT].Renderbuffer;
1317 /* Do we have a front color buffer and has it been drawn to since last
1318 * frontbuffer flush?
1320 if (rb && rb->defined &&
1321 stfb->iface->flush_front(&st->iface, stfb->iface, statt)) {
1322 rb->defined = GL_FALSE;
1324 /* Trigger an update of rb->defined on next draw */
1325 st->dirty |= ST_NEW_FB_STATE;
1331 * Re-validate the framebuffers.
1334 st_manager_validate_framebuffers(struct st_context *st)
1336 struct gl_framebuffer *stdraw = st_ws_framebuffer(st->ctx->DrawBuffer);
1337 struct gl_framebuffer *stread = st_ws_framebuffer(st->ctx->ReadBuffer);
1340 st_framebuffer_validate(stdraw, st);
1341 if (stread && stread != stdraw)
1342 st_framebuffer_validate(stread, st);
1344 st_context_validate(st, stdraw, stread);
1349 * Flush any outstanding swapbuffers on the current draw framebuffer.
1352 st_manager_flush_swapbuffers(void)
1354 GET_CURRENT_CONTEXT(ctx);
1355 struct st_context *st = (ctx) ? ctx->st : NULL;
1356 struct gl_framebuffer *stfb;
1361 stfb = st_ws_framebuffer(ctx->DrawBuffer);
1362 if (!stfb || !stfb->iface->flush_swapbuffers)
1365 stfb->iface->flush_swapbuffers(&st->iface, stfb->iface);
1370 * Add a color renderbuffer on demand. The FBO must correspond to a window,
1371 * not a user-created FBO.
1374 st_manager_add_color_renderbuffer(struct gl_context *ctx,
1375 struct gl_framebuffer *fb,
1376 gl_buffer_index idx)
1378 struct gl_framebuffer *stfb = st_ws_framebuffer(fb);
1384 assert(_mesa_is_winsys_fbo(fb));
1386 if (stfb->Attachment[idx].Renderbuffer)
1390 case BUFFER_FRONT_LEFT:
1391 case BUFFER_BACK_LEFT:
1392 case BUFFER_FRONT_RIGHT:
1393 case BUFFER_BACK_RIGHT:
1399 if (!st_framebuffer_add_renderbuffer(stfb, idx,
1400 stfb->Visual.sRGBCapable))
1403 st_framebuffer_update_attachments(stfb);
1406 * Force a call to the frontend manager to validate the
1407 * new renderbuffer. It might be that there is a window system
1408 * renderbuffer available.
1411 stfb->iface_stamp = p_atomic_read(&stfb->iface->stamp) - 1;
1413 st_invalidate_buffers(st_context(ctx));
1420 get_version(struct pipe_screen *screen,
1421 struct st_config_options *options, gl_api api)
1423 struct gl_constants consts = {0};
1424 struct gl_extensions extensions = {0};
1427 if (_mesa_override_gl_version_contextless(&consts, &api, &version)) {
1431 _mesa_init_constants(&consts, api);
1432 _mesa_init_extensions(&extensions);
1434 st_init_limits(screen, &consts, &extensions);
1435 st_init_extensions(screen, &consts, &extensions, options, api);
1436 version = _mesa_get_version(&extensions, &consts, api);
1437 free(consts.SpirVExtensions);
1443 st_api_query_versions(struct st_manager *sm,
1444 struct st_config_options *options,
1445 int *gl_core_version,
1446 int *gl_compat_version,
1447 int *gl_es1_version,
1448 int *gl_es2_version)
1450 *gl_core_version = get_version(sm->screen, options, API_OPENGL_CORE);
1451 *gl_compat_version = get_version(sm->screen, options, API_OPENGL_COMPAT);
1452 *gl_es1_version = get_version(sm->screen, options, API_OPENGLES);
1453 *gl_es2_version = get_version(sm->screen, options, API_OPENGLES2);
1458 st_manager_invalidate_drawables(struct gl_context *ctx)
1460 struct gl_framebuffer *stdraw;
1461 struct gl_framebuffer *stread;
1464 * Normally we'd want the frontend manager to mark the drawables
1465 * invalid only when needed. This will force the frontend manager
1466 * to revalidate the drawable, rather than just update the context with
1467 * the latest cached drawable info.
1470 stdraw = st_ws_framebuffer(ctx->DrawBuffer);
1471 stread = st_ws_framebuffer(ctx->ReadBuffer);
1474 stdraw->iface_stamp = p_atomic_read(&stdraw->iface->stamp) - 1;
1475 if (stread && stread != stdraw)
1476 stread->iface_stamp = p_atomic_read(&stread->iface->stamp) - 1;