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 "st_texture.h"
43 #include "st_context.h"
45 #include "st_extensions.h"
46 #include "st_format.h"
47 #include "st_cb_fbo.h"
48 #include "st_cb_flush.h"
49 #include "st_manager.h"
51 #include "state_tracker/st_gl_api.h"
53 #include "pipe/p_context.h"
54 #include "pipe/p_screen.h"
55 #include "util/u_format.h"
56 #include "util/u_pointer.h"
57 #include "util/u_inlines.h"
58 #include "util/u_atomic.h"
59 #include "util/u_surface.h"
60 #include "util/list.h"
64 * Map an attachment to a buffer index.
66 static inline gl_buffer_index
67 attachment_to_buffer_index(enum st_attachment_type statt
)
69 gl_buffer_index index
;
72 case ST_ATTACHMENT_FRONT_LEFT
:
73 index
= BUFFER_FRONT_LEFT
;
75 case ST_ATTACHMENT_BACK_LEFT
:
76 index
= BUFFER_BACK_LEFT
;
78 case ST_ATTACHMENT_FRONT_RIGHT
:
79 index
= BUFFER_FRONT_RIGHT
;
81 case ST_ATTACHMENT_BACK_RIGHT
:
82 index
= BUFFER_BACK_RIGHT
;
84 case ST_ATTACHMENT_DEPTH_STENCIL
:
87 case ST_ATTACHMENT_ACCUM
:
90 case ST_ATTACHMENT_SAMPLE
:
100 * Map a buffer index to an attachment.
102 static inline enum st_attachment_type
103 buffer_index_to_attachment(gl_buffer_index index
)
105 enum st_attachment_type statt
;
108 case BUFFER_FRONT_LEFT
:
109 statt
= ST_ATTACHMENT_FRONT_LEFT
;
111 case BUFFER_BACK_LEFT
:
112 statt
= ST_ATTACHMENT_BACK_LEFT
;
114 case BUFFER_FRONT_RIGHT
:
115 statt
= ST_ATTACHMENT_FRONT_RIGHT
;
117 case BUFFER_BACK_RIGHT
:
118 statt
= ST_ATTACHMENT_BACK_RIGHT
;
121 statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
124 statt
= ST_ATTACHMENT_ACCUM
;
127 statt
= ST_ATTACHMENT_INVALID
;
135 * Make sure a context picks up the latest cached state of the
136 * drawables it binds to.
139 st_context_validate(struct st_context
*st
,
140 struct st_framebuffer
*stdraw
,
141 struct st_framebuffer
*stread
)
143 if (stdraw
&& stdraw
->stamp
!= st
->draw_stamp
) {
144 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
145 _mesa_resize_framebuffer(st
->ctx
, &stdraw
->Base
,
147 stdraw
->Base
.Height
);
148 st
->draw_stamp
= stdraw
->stamp
;
151 if (stread
&& stread
->stamp
!= st
->read_stamp
) {
152 if (stread
!= stdraw
) {
153 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
154 _mesa_resize_framebuffer(st
->ctx
, &stread
->Base
,
156 stread
->Base
.Height
);
158 st
->read_stamp
= stread
->stamp
;
163 * Validate a framebuffer to make sure up-to-date pipe_textures are used.
164 * The context is only used for creating pipe surfaces and for calling
165 * _mesa_resize_framebuffer().
166 * (That should probably be rethought, since those surfaces become
167 * drawable state, not context state, and can be freed by another pipe
171 st_framebuffer_validate(struct st_framebuffer
*stfb
,
172 struct st_context
*st
)
174 struct pipe_resource
*textures
[ST_ATTACHMENT_COUNT
];
177 boolean changed
= FALSE
;
180 new_stamp
= p_atomic_read(&stfb
->iface
->stamp
);
181 if (stfb
->iface_stamp
== new_stamp
)
184 /* validate the fb */
186 if (!stfb
->iface
->validate(&st
->iface
, stfb
->iface
, stfb
->statts
,
187 stfb
->num_statts
, textures
))
190 stfb
->iface_stamp
= new_stamp
;
191 new_stamp
= p_atomic_read(&stfb
->iface
->stamp
);
192 } while(stfb
->iface_stamp
!= new_stamp
);
194 width
= stfb
->Base
.Width
;
195 height
= stfb
->Base
.Height
;
197 for (i
= 0; i
< stfb
->num_statts
; i
++) {
198 struct st_renderbuffer
*strb
;
199 struct pipe_surface
*ps
, surf_tmpl
;
205 idx
= attachment_to_buffer_index(stfb
->statts
[i
]);
206 if (idx
>= BUFFER_COUNT
) {
207 pipe_resource_reference(&textures
[i
], NULL
);
211 strb
= st_renderbuffer(stfb
->Base
.Attachment
[idx
].Renderbuffer
);
213 if (strb
->texture
== textures
[i
]) {
214 pipe_resource_reference(&textures
[i
], NULL
);
218 u_surface_default_template(&surf_tmpl
, textures
[i
]);
219 ps
= st
->pipe
->create_surface(st
->pipe
, textures
[i
], &surf_tmpl
);
221 struct pipe_surface
**psurf
=
222 util_format_is_srgb(ps
->format
) ? &strb
->surface_srgb
:
223 &strb
->surface_linear
;
225 pipe_surface_reference(psurf
, ps
);
226 strb
->surface
= *psurf
;
227 pipe_resource_reference(&strb
->texture
, ps
->texture
);
228 /* ownership transfered */
229 pipe_surface_reference(&ps
, NULL
);
233 strb
->Base
.Width
= strb
->surface
->width
;
234 strb
->Base
.Height
= strb
->surface
->height
;
236 width
= strb
->Base
.Width
;
237 height
= strb
->Base
.Height
;
240 pipe_resource_reference(&textures
[i
], NULL
);
245 _mesa_resize_framebuffer(st
->ctx
, &stfb
->Base
, width
, height
);
250 * Update the attachments to validate by looping the existing renderbuffers.
253 st_framebuffer_update_attachments(struct st_framebuffer
*stfb
)
257 stfb
->num_statts
= 0;
258 for (idx
= 0; idx
< BUFFER_COUNT
; idx
++) {
259 struct st_renderbuffer
*strb
;
260 enum st_attachment_type statt
;
262 strb
= st_renderbuffer(stfb
->Base
.Attachment
[idx
].Renderbuffer
);
263 if (!strb
|| strb
->software
)
266 statt
= buffer_index_to_attachment(idx
);
267 if (statt
!= ST_ATTACHMENT_INVALID
&&
268 st_visual_have_buffers(stfb
->iface
->visual
, 1 << statt
))
269 stfb
->statts
[stfb
->num_statts
++] = statt
;
275 * Add a renderbuffer to the framebuffer. The framebuffer is one that
276 * corresponds to a window and is not a user-created FBO.
279 st_framebuffer_add_renderbuffer(struct st_framebuffer
*stfb
,
282 struct gl_renderbuffer
*rb
;
283 enum pipe_format format
;
286 assert(_mesa_is_winsys_fbo(&stfb
->Base
));
288 /* do not distinguish depth/stencil buffers */
289 if (idx
== BUFFER_STENCIL
)
294 format
= stfb
->iface
->visual
->depth_stencil_format
;
298 format
= stfb
->iface
->visual
->accum_format
;
302 format
= stfb
->iface
->visual
->color_format
;
303 if (stfb
->Base
.Visual
.sRGBCapable
)
304 format
= util_format_srgb(format
);
309 if (format
== PIPE_FORMAT_NONE
)
312 rb
= st_new_renderbuffer_fb(format
, stfb
->iface
->visual
->samples
, sw
);
316 if (idx
!= BUFFER_DEPTH
) {
317 _mesa_attach_and_own_rb(&stfb
->Base
, idx
, rb
);
321 bool rb_ownership_taken
= false;
322 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_ZS
, 0)) {
323 _mesa_attach_and_own_rb(&stfb
->Base
, BUFFER_DEPTH
, rb
);
324 rb_ownership_taken
= true;
327 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_ZS
, 1)) {
328 if (rb_ownership_taken
)
329 _mesa_attach_and_reference_rb(&stfb
->Base
, BUFFER_STENCIL
, rb
);
331 _mesa_attach_and_own_rb(&stfb
->Base
, BUFFER_STENCIL
, rb
);
338 * Intialize a struct gl_config from a visual.
341 st_visual_to_context_mode(const struct st_visual
*visual
,
342 struct gl_config
*mode
)
344 memset(mode
, 0, sizeof(*mode
));
346 if (st_visual_have_buffers(visual
, ST_ATTACHMENT_BACK_LEFT_MASK
))
347 mode
->doubleBufferMode
= GL_TRUE
;
348 if (st_visual_have_buffers(visual
,
349 ST_ATTACHMENT_FRONT_RIGHT_MASK
| ST_ATTACHMENT_BACK_RIGHT_MASK
))
350 mode
->stereoMode
= GL_TRUE
;
352 if (visual
->color_format
!= PIPE_FORMAT_NONE
) {
353 mode
->rgbMode
= GL_TRUE
;
356 util_format_get_component_bits(visual
->color_format
,
357 UTIL_FORMAT_COLORSPACE_RGB
, 0);
359 util_format_get_component_bits(visual
->color_format
,
360 UTIL_FORMAT_COLORSPACE_RGB
, 1);
362 util_format_get_component_bits(visual
->color_format
,
363 UTIL_FORMAT_COLORSPACE_RGB
, 2);
365 util_format_get_component_bits(visual
->color_format
,
366 UTIL_FORMAT_COLORSPACE_RGB
, 3);
368 mode
->rgbBits
= mode
->redBits
+
369 mode
->greenBits
+ mode
->blueBits
+ mode
->alphaBits
;
370 mode
->sRGBCapable
= util_format_is_srgb(visual
->color_format
);
373 if (visual
->depth_stencil_format
!= PIPE_FORMAT_NONE
) {
375 util_format_get_component_bits(visual
->depth_stencil_format
,
376 UTIL_FORMAT_COLORSPACE_ZS
, 0);
378 util_format_get_component_bits(visual
->depth_stencil_format
,
379 UTIL_FORMAT_COLORSPACE_ZS
, 1);
381 mode
->haveDepthBuffer
= mode
->depthBits
> 0;
382 mode
->haveStencilBuffer
= mode
->stencilBits
> 0;
385 if (visual
->accum_format
!= PIPE_FORMAT_NONE
) {
386 mode
->haveAccumBuffer
= GL_TRUE
;
389 util_format_get_component_bits(visual
->accum_format
,
390 UTIL_FORMAT_COLORSPACE_RGB
, 0);
391 mode
->accumGreenBits
=
392 util_format_get_component_bits(visual
->accum_format
,
393 UTIL_FORMAT_COLORSPACE_RGB
, 1);
394 mode
->accumBlueBits
=
395 util_format_get_component_bits(visual
->accum_format
,
396 UTIL_FORMAT_COLORSPACE_RGB
, 2);
397 mode
->accumAlphaBits
=
398 util_format_get_component_bits(visual
->accum_format
,
399 UTIL_FORMAT_COLORSPACE_RGB
, 3);
402 if (visual
->samples
> 1) {
403 mode
->sampleBuffers
= 1;
404 mode
->samples
= visual
->samples
;
409 * Create a framebuffer from a manager interface.
411 static struct st_framebuffer
*
412 st_framebuffer_create(struct st_context
*st
,
413 struct st_framebuffer_iface
*stfbi
)
415 struct st_framebuffer
*stfb
;
416 struct gl_config mode
;
422 stfb
= CALLOC_STRUCT(st_framebuffer
);
426 st_visual_to_context_mode(stfbi
->visual
, &mode
);
429 * For desktop GL, sRGB framebuffer write is controlled by both the
430 * capability of the framebuffer and GL_FRAMEBUFFER_SRGB. We should
431 * advertise the capability when the pipe driver (and core Mesa) supports
432 * it so that applications can enable sRGB write when they want to.
434 * This is not to be confused with GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB. When
435 * the attribute is GLX_TRUE, it tells the st manager to pick a color
436 * format such that util_format_srgb(visual->color_format) can be supported
437 * by the pipe driver. We still need to advertise the capability here.
439 * For GLES, however, sRGB framebuffer write is controlled only by the
440 * capability of the framebuffer. There is GL_EXT_sRGB_write_control to
441 * give applications the control back, but sRGB write is still enabled by
442 * default. To avoid unexpected results, we should not advertise the
443 * capability. This could change when we add support for
444 * EGL_KHR_gl_colorspace.
446 if (_mesa_is_desktop_gl(st
->ctx
)) {
447 struct pipe_screen
*screen
= st
->pipe
->screen
;
448 const enum pipe_format srgb_format
=
449 util_format_srgb(stfbi
->visual
->color_format
);
451 if (srgb_format
!= PIPE_FORMAT_NONE
&&
452 st_pipe_format_to_mesa_format(srgb_format
) != MESA_FORMAT_NONE
&&
453 screen
->is_format_supported(screen
, srgb_format
,
454 PIPE_TEXTURE_2D
, stfbi
->visual
->samples
,
455 (PIPE_BIND_DISPLAY_TARGET
|
456 PIPE_BIND_RENDER_TARGET
)))
457 mode
.sRGBCapable
= GL_TRUE
;
460 _mesa_initialize_window_framebuffer(&stfb
->Base
, &mode
);
463 stfb
->iface_ID
= stfbi
->ID
;
464 stfb
->iface_stamp
= p_atomic_read(&stfbi
->stamp
) - 1;
466 /* add the color buffer */
467 idx
= stfb
->Base
._ColorDrawBufferIndexes
[0];
468 if (!st_framebuffer_add_renderbuffer(stfb
, idx
)) {
473 st_framebuffer_add_renderbuffer(stfb
, BUFFER_DEPTH
);
474 st_framebuffer_add_renderbuffer(stfb
, BUFFER_ACCUM
);
477 st_framebuffer_update_attachments(stfb
);
483 * Reference a framebuffer.
486 st_framebuffer_reference(struct st_framebuffer
**ptr
,
487 struct st_framebuffer
*stfb
)
489 struct gl_framebuffer
*fb
= &stfb
->Base
;
490 _mesa_reference_framebuffer((struct gl_framebuffer
**) ptr
, fb
);
494 * Purge the winsys buffers list to remove any references to
495 * non-existing framebuffer interface objects.
498 st_framebuffers_purge(struct st_context
*st
)
500 struct st_framebuffer
*stfb
, *next
;
502 LIST_FOR_EACH_ENTRY_SAFE_REV(stfb
, next
, &st
->winsys_buffers
, head
) {
504 * If the corresponding framebuffer interface object no longer exists,
505 * remove the framebuffer object from the context's winsys buffers list,
506 * and unreference the framebuffer object, so its resources can be
509 if (stfb
->iface
->ID
!= stfb
->iface_ID
) {
510 LIST_DEL(&stfb
->head
);
511 st_framebuffer_reference(&stfb
, NULL
);
517 st_context_flush(struct st_context_iface
*stctxi
, unsigned flags
,
518 struct pipe_fence_handle
**fence
)
520 struct st_context
*st
= (struct st_context
*) stctxi
;
521 unsigned pipe_flags
= 0;
523 if (flags
& ST_FLUSH_END_OF_FRAME
) {
524 pipe_flags
|= PIPE_FLUSH_END_OF_FRAME
;
527 st_flush(st
, fence
, pipe_flags
);
529 if ((flags
& ST_FLUSH_WAIT
) && fence
) {
530 st
->pipe
->screen
->fence_finish(st
->pipe
->screen
, NULL
, *fence
,
531 PIPE_TIMEOUT_INFINITE
);
532 st
->pipe
->screen
->fence_reference(st
->pipe
->screen
, fence
, NULL
);
535 if (flags
& ST_FLUSH_FRONT
)
536 st_manager_flush_frontbuffer(st
);
540 st_context_teximage(struct st_context_iface
*stctxi
,
541 enum st_texture_type tex_type
,
542 int level
, enum pipe_format pipe_format
,
543 struct pipe_resource
*tex
, boolean mipmap
)
545 struct st_context
*st
= (struct st_context
*) stctxi
;
546 struct gl_context
*ctx
= st
->ctx
;
547 struct gl_texture_object
*texObj
;
548 struct gl_texture_image
*texImage
;
549 struct st_texture_object
*stObj
;
550 struct st_texture_image
*stImage
;
551 GLenum internalFormat
;
552 GLuint width
, height
, depth
;
557 target
= GL_TEXTURE_1D
;
560 target
= GL_TEXTURE_2D
;
563 target
= GL_TEXTURE_3D
;
565 case ST_TEXTURE_RECT
:
566 target
= GL_TEXTURE_RECTANGLE_ARB
;
572 texObj
= _mesa_get_current_tex_object(ctx
, target
);
574 _mesa_lock_texture(ctx
, texObj
);
576 stObj
= st_texture_object(texObj
);
577 /* switch to surface based */
578 if (!stObj
->surface_based
) {
579 _mesa_clear_texture_object(ctx
, texObj
);
580 stObj
->surface_based
= GL_TRUE
;
583 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
584 stImage
= st_texture_image(texImage
);
586 mesa_format texFormat
= st_pipe_format_to_mesa_format(pipe_format
);
588 if (util_format_has_alpha(tex
->format
))
589 internalFormat
= GL_RGBA
;
591 internalFormat
= GL_RGB
;
593 _mesa_init_teximage_fields(ctx
, texImage
,
594 tex
->width0
, tex
->height0
, 1, 0,
595 internalFormat
, texFormat
);
598 height
= tex
->height0
;
601 /* grow the image size until we hit level = 0 */
613 _mesa_clear_texture_image(ctx
, texImage
);
614 width
= height
= depth
= 0;
617 pipe_resource_reference(&stImage
->pt
, tex
);
618 stObj
->surface_format
= pipe_format
;
620 stObj
->needs_validation
= true;
622 _mesa_dirty_texobj(ctx
, texObj
);
623 _mesa_unlock_texture(ctx
, texObj
);
629 st_context_copy(struct st_context_iface
*stctxi
,
630 struct st_context_iface
*stsrci
, unsigned mask
)
632 struct st_context
*st
= (struct st_context
*) stctxi
;
633 struct st_context
*src
= (struct st_context
*) stsrci
;
635 _mesa_copy_context(src
->ctx
, st
->ctx
, mask
);
639 st_context_share(struct st_context_iface
*stctxi
,
640 struct st_context_iface
*stsrci
)
642 struct st_context
*st
= (struct st_context
*) stctxi
;
643 struct st_context
*src
= (struct st_context
*) stsrci
;
645 return _mesa_share_state(st
->ctx
, src
->ctx
);
649 st_context_destroy(struct st_context_iface
*stctxi
)
651 struct st_context
*st
= (struct st_context
*) stctxi
;
652 st_destroy_context(st
);
656 st_start_thread(struct st_context_iface
*stctxi
)
658 struct st_context
*st
= (struct st_context
*) stctxi
;
660 _mesa_glthread_init(st
->ctx
);
664 st_thread_finish(struct st_context_iface
*stctxi
)
666 struct st_context
*st
= (struct st_context
*) stctxi
;
668 _mesa_glthread_finish(st
->ctx
);
671 static struct st_context_iface
*
672 st_api_create_context(struct st_api
*stapi
, struct st_manager
*smapi
,
673 const struct st_context_attribs
*attribs
,
674 enum st_context_error
*error
,
675 struct st_context_iface
*shared_stctxi
)
677 struct st_context
*shared_ctx
= (struct st_context
*) shared_stctxi
;
678 struct st_context
*st
;
679 struct pipe_context
*pipe
;
680 struct gl_config mode
;
682 bool no_error
= false;
683 unsigned ctx_flags
= PIPE_CONTEXT_PREFER_THREADED
;
685 if (!(stapi
->profile_mask
& (1 << attribs
->profile
)))
688 switch (attribs
->profile
) {
689 case ST_PROFILE_DEFAULT
:
690 api
= API_OPENGL_COMPAT
;
692 case ST_PROFILE_OPENGL_ES1
:
695 case ST_PROFILE_OPENGL_ES2
:
698 case ST_PROFILE_OPENGL_CORE
:
699 api
= API_OPENGL_CORE
;
702 *error
= ST_CONTEXT_ERROR_BAD_API
;
706 if (attribs
->flags
& ST_CONTEXT_FLAG_ROBUST_ACCESS
)
707 ctx_flags
|= PIPE_CONTEXT_ROBUST_BUFFER_ACCESS
;
709 if (attribs
->flags
& ST_CONTEXT_FLAG_NO_ERROR
)
712 pipe
= smapi
->screen
->context_create(smapi
->screen
, NULL
, ctx_flags
);
714 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
718 st_visual_to_context_mode(&attribs
->visual
, &mode
);
719 st
= st_create_context(api
, pipe
, &mode
, shared_ctx
, &attribs
->options
, no_error
);
721 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
726 if (attribs
->flags
& ST_CONTEXT_FLAG_DEBUG
) {
727 if (!_mesa_set_debug_state_int(st
->ctx
, GL_DEBUG_OUTPUT
, GL_TRUE
)) {
728 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
732 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_DEBUG_BIT
;
735 if (st
->ctx
->Const
.ContextFlags
& GL_CONTEXT_FLAG_DEBUG_BIT
) {
736 st_update_debug_callback(st
);
739 if (attribs
->flags
& ST_CONTEXT_FLAG_FORWARD_COMPATIBLE
)
740 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT
;
741 if (attribs
->flags
& ST_CONTEXT_FLAG_ROBUST_ACCESS
) {
742 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB
;
743 st
->ctx
->Const
.RobustAccess
= GL_TRUE
;
745 if (attribs
->flags
& ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED
) {
746 st
->ctx
->Const
.ResetStrategy
= GL_LOSE_CONTEXT_ON_RESET_ARB
;
747 st_install_device_reset_callback(st
);
750 /* need to perform version check */
751 if (attribs
->major
> 1 || attribs
->minor
> 0) {
752 /* Is the actual version less than the requested version?
754 if (st
->ctx
->Version
< attribs
->major
* 10U + attribs
->minor
) {
755 *error
= ST_CONTEXT_ERROR_BAD_VERSION
;
756 st_destroy_context(st
);
761 st
->invalidate_on_gl_viewport
=
762 smapi
->get_param(smapi
, ST_MANAGER_BROKEN_INVALIDATE
);
764 st
->iface
.destroy
= st_context_destroy
;
765 st
->iface
.flush
= st_context_flush
;
766 st
->iface
.teximage
= st_context_teximage
;
767 st
->iface
.copy
= st_context_copy
;
768 st
->iface
.share
= st_context_share
;
769 st
->iface
.start_thread
= st_start_thread
;
770 st
->iface
.thread_finish
= st_thread_finish
;
771 st
->iface
.st_context_private
= (void *) smapi
;
772 st
->iface
.cso_context
= st
->cso_context
;
773 st
->iface
.pipe
= st
->pipe
;
775 *error
= ST_CONTEXT_SUCCESS
;
779 static struct st_context_iface
*
780 st_api_get_current(struct st_api
*stapi
)
782 GET_CURRENT_CONTEXT(ctx
);
783 struct st_context
*st
= (ctx
) ? ctx
->st
: NULL
;
785 return (st
) ? &st
->iface
: NULL
;
788 static struct st_framebuffer
*
789 st_framebuffer_reuse_or_create(struct st_context
*st
,
790 struct gl_framebuffer
*fb
,
791 struct st_framebuffer_iface
*stfbi
)
793 struct st_framebuffer
*cur
= NULL
, *stfb
= NULL
;
798 /* Check if there is already a framebuffer object for the specified
799 * framebuffer interface in this context. If there is one, use it.
801 LIST_FOR_EACH_ENTRY(cur
, &st
->winsys_buffers
, head
) {
802 if (cur
->iface_ID
== stfbi
->ID
) {
803 st_framebuffer_reference(&stfb
, cur
);
808 /* If there is not already a framebuffer object, create one */
810 cur
= st_framebuffer_create(st
, stfbi
);
813 /* add to the context's winsys buffers list */
814 LIST_ADD(&cur
->head
, &st
->winsys_buffers
);
816 st_framebuffer_reference(&stfb
, cur
);
824 st_api_make_current(struct st_api
*stapi
, struct st_context_iface
*stctxi
,
825 struct st_framebuffer_iface
*stdrawi
,
826 struct st_framebuffer_iface
*streadi
)
828 struct st_context
*st
= (struct st_context
*) stctxi
;
829 struct st_framebuffer
*stdraw
, *stread
;
832 _glapi_check_multithread();
835 /* reuse or create the draw fb */
836 stdraw
= st_framebuffer_reuse_or_create(st
,
837 st
->ctx
->WinSysDrawBuffer
, stdrawi
);
838 if (streadi
!= stdrawi
) {
839 /* do the same for the read fb */
840 stread
= st_framebuffer_reuse_or_create(st
,
841 st
->ctx
->WinSysReadBuffer
, streadi
);
845 /* reuse the draw fb for the read fb */
847 st_framebuffer_reference(&stread
, stdraw
);
850 if (stdraw
&& stread
) {
851 st_framebuffer_validate(stdraw
, st
);
852 if (stread
!= stdraw
)
853 st_framebuffer_validate(stread
, st
);
855 ret
= _mesa_make_current(st
->ctx
, &stdraw
->Base
, &stread
->Base
);
857 st
->draw_stamp
= stdraw
->stamp
- 1;
858 st
->read_stamp
= stread
->stamp
- 1;
859 st_context_validate(st
, stdraw
, stread
);
862 struct gl_framebuffer
*incomplete
= _mesa_get_incomplete_framebuffer();
863 ret
= _mesa_make_current(st
->ctx
, incomplete
, incomplete
);
866 st_framebuffer_reference(&stdraw
, NULL
);
867 st_framebuffer_reference(&stread
, NULL
);
869 /* Purge the context's winsys_buffers list in case any
870 * of the referenced drawables no longer exist.
872 st_framebuffers_purge(st
);
875 ret
= _mesa_make_current(NULL
, NULL
, NULL
);
882 st_api_destroy(struct st_api
*stapi
)
887 * Flush the front buffer if the current context renders to the front buffer.
890 st_manager_flush_frontbuffer(struct st_context
*st
)
892 struct st_framebuffer
*stfb
= st_ws_framebuffer(st
->ctx
->DrawBuffer
);
893 struct st_renderbuffer
*strb
= NULL
;
896 strb
= st_renderbuffer(stfb
->Base
.Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
900 /* never a dummy fb */
901 stfb
->iface
->flush_front(&st
->iface
, stfb
->iface
, ST_ATTACHMENT_FRONT_LEFT
);
905 * Re-validate the framebuffers.
908 st_manager_validate_framebuffers(struct st_context
*st
)
910 struct st_framebuffer
*stdraw
= st_ws_framebuffer(st
->ctx
->DrawBuffer
);
911 struct st_framebuffer
*stread
= st_ws_framebuffer(st
->ctx
->ReadBuffer
);
914 st_framebuffer_validate(stdraw
, st
);
915 if (stread
&& stread
!= stdraw
)
916 st_framebuffer_validate(stread
, st
);
918 st_context_validate(st
, stdraw
, stread
);
922 * Add a color renderbuffer on demand. The FBO must correspond to a window,
923 * not a user-created FBO.
926 st_manager_add_color_renderbuffer(struct st_context
*st
,
927 struct gl_framebuffer
*fb
,
930 struct st_framebuffer
*stfb
= st_ws_framebuffer(fb
);
936 assert(_mesa_is_winsys_fbo(fb
));
938 if (stfb
->Base
.Attachment
[idx
].Renderbuffer
)
942 case BUFFER_FRONT_LEFT
:
943 case BUFFER_BACK_LEFT
:
944 case BUFFER_FRONT_RIGHT
:
945 case BUFFER_BACK_RIGHT
:
951 if (!st_framebuffer_add_renderbuffer(stfb
, idx
))
954 st_framebuffer_update_attachments(stfb
);
957 * Force a call to the state tracker manager to validate the
958 * new renderbuffer. It might be that there is a window system
959 * renderbuffer available.
962 stfb
->iface_stamp
= p_atomic_read(&stfb
->iface
->stamp
) - 1;
964 st_invalidate_buffers(st
);
970 get_version(struct pipe_screen
*screen
,
971 struct st_config_options
*options
, gl_api api
)
973 struct gl_constants consts
= {0};
974 struct gl_extensions extensions
= {0};
977 if (_mesa_override_gl_version_contextless(&consts
, &api
, &version
)) {
981 _mesa_init_constants(&consts
, api
);
982 _mesa_init_extensions(&extensions
);
984 st_init_limits(screen
, &consts
, &extensions
);
985 st_init_extensions(screen
, &consts
, &extensions
, options
, GL_TRUE
);
987 return _mesa_get_version(&extensions
, &consts
, api
);
991 st_api_query_versions(struct st_api
*stapi
, struct st_manager
*sm
,
992 struct st_config_options
*options
,
993 int *gl_core_version
,
994 int *gl_compat_version
,
998 *gl_core_version
= get_version(sm
->screen
, options
, API_OPENGL_CORE
);
999 *gl_compat_version
= get_version(sm
->screen
, options
, API_OPENGL_COMPAT
);
1000 *gl_es1_version
= get_version(sm
->screen
, options
, API_OPENGLES
);
1001 *gl_es2_version
= get_version(sm
->screen
, options
, API_OPENGLES2
);
1004 static const struct st_api st_gl_api
= {
1005 .name
= "Mesa " PACKAGE_VERSION
,
1006 .api
= ST_API_OPENGL
,
1007 .profile_mask
= ST_PROFILE_DEFAULT_MASK
|
1008 ST_PROFILE_OPENGL_CORE_MASK
|
1009 ST_PROFILE_OPENGL_ES1_MASK
|
1010 ST_PROFILE_OPENGL_ES2_MASK
|
1012 .feature_mask
= ST_API_FEATURE_MS_VISUALS_MASK
,
1013 .destroy
= st_api_destroy
,
1014 .query_versions
= st_api_query_versions
,
1015 .create_context
= st_api_create_context
,
1016 .make_current
= st_api_make_current
,
1017 .get_current
= st_api_get_current
,
1021 st_gl_api_create(void)
1023 return (struct st_api
*) &st_gl_api
;