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/texobj.h"
33 #include "main/teximage.h"
34 #include "main/texstate.h"
35 #include "main/errors.h"
36 #include "main/framebuffer.h"
37 #include "main/fbobject.h"
38 #include "main/renderbuffer.h"
39 #include "main/version.h"
40 #include "st_texture.h"
42 #include "st_context.h"
44 #include "st_extensions.h"
45 #include "st_format.h"
46 #include "st_cb_fbo.h"
47 #include "st_cb_flush.h"
48 #include "st_manager.h"
50 #include "state_tracker/st_gl_api.h"
52 #include "pipe/p_context.h"
53 #include "pipe/p_screen.h"
54 #include "util/u_format.h"
55 #include "util/u_pointer.h"
56 #include "util/u_inlines.h"
57 #include "util/u_atomic.h"
58 #include "util/u_surface.h"
61 * Cast wrapper to convert a struct gl_framebuffer to an st_framebuffer.
62 * Return NULL if the struct gl_framebuffer is a user-created framebuffer.
63 * We'll only return non-null for window system framebuffers.
64 * Note that this function may fail.
66 static inline struct st_framebuffer
*
67 st_ws_framebuffer(struct gl_framebuffer
*fb
)
69 /* FBO cannot be casted. See st_new_framebuffer */
70 if (fb
&& _mesa_is_winsys_fbo(fb
))
71 return (struct st_framebuffer
*) fb
;
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
;
112 * Map a buffer index to an attachment.
114 static inline enum st_attachment_type
115 buffer_index_to_attachment(gl_buffer_index index
)
117 enum st_attachment_type statt
;
120 case BUFFER_FRONT_LEFT
:
121 statt
= ST_ATTACHMENT_FRONT_LEFT
;
123 case BUFFER_BACK_LEFT
:
124 statt
= ST_ATTACHMENT_BACK_LEFT
;
126 case BUFFER_FRONT_RIGHT
:
127 statt
= ST_ATTACHMENT_FRONT_RIGHT
;
129 case BUFFER_BACK_RIGHT
:
130 statt
= ST_ATTACHMENT_BACK_RIGHT
;
133 statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
136 statt
= ST_ATTACHMENT_ACCUM
;
139 statt
= ST_ATTACHMENT_INVALID
;
147 * Make sure a context picks up the latest cached state of the
148 * drawables it binds to.
151 st_context_validate(struct st_context
*st
,
152 struct st_framebuffer
*stdraw
,
153 struct st_framebuffer
*stread
)
155 if (stdraw
&& stdraw
->stamp
!= st
->draw_stamp
) {
156 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
157 _mesa_resize_framebuffer(st
->ctx
, &stdraw
->Base
,
159 stdraw
->Base
.Height
);
160 st
->draw_stamp
= stdraw
->stamp
;
163 if (stread
&& stread
->stamp
!= st
->read_stamp
) {
164 if (stread
!= stdraw
) {
165 st
->dirty
|= ST_NEW_FRAMEBUFFER
;
166 _mesa_resize_framebuffer(st
->ctx
, &stread
->Base
,
168 stread
->Base
.Height
);
170 st
->read_stamp
= stread
->stamp
;
175 * Validate a framebuffer to make sure up-to-date pipe_textures are used.
176 * The context is only used for creating pipe surfaces and for calling
177 * _mesa_resize_framebuffer().
178 * (That should probably be rethought, since those surfaces become
179 * drawable state, not context state, and can be freed by another pipe
183 st_framebuffer_validate(struct st_framebuffer
*stfb
,
184 struct st_context
*st
)
186 struct pipe_resource
*textures
[ST_ATTACHMENT_COUNT
];
189 boolean changed
= FALSE
;
192 /* Check for incomplete framebuffers (e.g. EGL_KHR_surfaceless_context) */
196 new_stamp
= p_atomic_read(&stfb
->iface
->stamp
);
197 if (stfb
->iface_stamp
== new_stamp
)
200 /* validate the fb */
202 if (!stfb
->iface
->validate(&st
->iface
, stfb
->iface
, stfb
->statts
,
203 stfb
->num_statts
, textures
))
206 stfb
->iface_stamp
= new_stamp
;
207 new_stamp
= p_atomic_read(&stfb
->iface
->stamp
);
208 } while(stfb
->iface_stamp
!= new_stamp
);
210 width
= stfb
->Base
.Width
;
211 height
= stfb
->Base
.Height
;
213 for (i
= 0; i
< stfb
->num_statts
; i
++) {
214 struct st_renderbuffer
*strb
;
215 struct pipe_surface
*ps
, surf_tmpl
;
221 idx
= attachment_to_buffer_index(stfb
->statts
[i
]);
222 if (idx
>= BUFFER_COUNT
) {
223 pipe_resource_reference(&textures
[i
], NULL
);
227 strb
= st_renderbuffer(stfb
->Base
.Attachment
[idx
].Renderbuffer
);
229 if (strb
->texture
== textures
[i
]) {
230 pipe_resource_reference(&textures
[i
], NULL
);
234 u_surface_default_template(&surf_tmpl
, textures
[i
]);
235 ps
= st
->pipe
->create_surface(st
->pipe
, textures
[i
], &surf_tmpl
);
237 pipe_surface_reference(&strb
->surface
, ps
);
238 pipe_resource_reference(&strb
->texture
, ps
->texture
);
239 /* ownership transfered */
240 pipe_surface_reference(&ps
, NULL
);
244 strb
->Base
.Width
= strb
->surface
->width
;
245 strb
->Base
.Height
= strb
->surface
->height
;
247 width
= strb
->Base
.Width
;
248 height
= strb
->Base
.Height
;
251 pipe_resource_reference(&textures
[i
], NULL
);
256 _mesa_resize_framebuffer(st
->ctx
, &stfb
->Base
, width
, height
);
261 * Update the attachments to validate by looping the existing renderbuffers.
264 st_framebuffer_update_attachments(struct st_framebuffer
*stfb
)
268 stfb
->num_statts
= 0;
269 for (idx
= 0; idx
< BUFFER_COUNT
; idx
++) {
270 struct st_renderbuffer
*strb
;
271 enum st_attachment_type statt
;
273 strb
= st_renderbuffer(stfb
->Base
.Attachment
[idx
].Renderbuffer
);
274 if (!strb
|| strb
->software
)
277 statt
= buffer_index_to_attachment(idx
);
278 if (statt
!= ST_ATTACHMENT_INVALID
&&
279 st_visual_have_buffers(stfb
->iface
->visual
, 1 << statt
))
280 stfb
->statts
[stfb
->num_statts
++] = statt
;
286 * Add a renderbuffer to the framebuffer.
289 st_framebuffer_add_renderbuffer(struct st_framebuffer
*stfb
,
292 struct gl_renderbuffer
*rb
;
293 enum pipe_format format
;
299 /* do not distinguish depth/stencil buffers */
300 if (idx
== BUFFER_STENCIL
)
305 format
= stfb
->iface
->visual
->depth_stencil_format
;
309 format
= stfb
->iface
->visual
->accum_format
;
313 format
= stfb
->iface
->visual
->color_format
;
314 if (stfb
->Base
.Visual
.sRGBCapable
)
315 format
= util_format_srgb(format
);
320 if (format
== PIPE_FORMAT_NONE
)
323 rb
= st_new_renderbuffer_fb(format
, stfb
->iface
->visual
->samples
, sw
);
327 if (idx
!= BUFFER_DEPTH
) {
328 _mesa_add_renderbuffer(&stfb
->Base
, idx
, rb
);
331 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_ZS
, 0))
332 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, rb
);
333 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_ZS
, 1))
334 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_STENCIL
, rb
);
341 * Intialize a struct gl_config from a visual.
344 st_visual_to_context_mode(const struct st_visual
*visual
,
345 struct gl_config
*mode
)
347 memset(mode
, 0, sizeof(*mode
));
349 if (st_visual_have_buffers(visual
, ST_ATTACHMENT_BACK_LEFT_MASK
))
350 mode
->doubleBufferMode
= GL_TRUE
;
351 if (st_visual_have_buffers(visual
,
352 ST_ATTACHMENT_FRONT_RIGHT_MASK
| ST_ATTACHMENT_BACK_RIGHT_MASK
))
353 mode
->stereoMode
= GL_TRUE
;
355 if (visual
->color_format
!= PIPE_FORMAT_NONE
) {
356 mode
->rgbMode
= GL_TRUE
;
359 util_format_get_component_bits(visual
->color_format
,
360 UTIL_FORMAT_COLORSPACE_RGB
, 0);
362 util_format_get_component_bits(visual
->color_format
,
363 UTIL_FORMAT_COLORSPACE_RGB
, 1);
365 util_format_get_component_bits(visual
->color_format
,
366 UTIL_FORMAT_COLORSPACE_RGB
, 2);
368 util_format_get_component_bits(visual
->color_format
,
369 UTIL_FORMAT_COLORSPACE_RGB
, 3);
371 mode
->rgbBits
= mode
->redBits
+
372 mode
->greenBits
+ mode
->blueBits
+ mode
->alphaBits
;
373 mode
->sRGBCapable
= util_format_is_srgb(visual
->color_format
);
376 if (visual
->depth_stencil_format
!= PIPE_FORMAT_NONE
) {
378 util_format_get_component_bits(visual
->depth_stencil_format
,
379 UTIL_FORMAT_COLORSPACE_ZS
, 0);
381 util_format_get_component_bits(visual
->depth_stencil_format
,
382 UTIL_FORMAT_COLORSPACE_ZS
, 1);
384 mode
->haveDepthBuffer
= mode
->depthBits
> 0;
385 mode
->haveStencilBuffer
= mode
->stencilBits
> 0;
388 if (visual
->accum_format
!= PIPE_FORMAT_NONE
) {
389 mode
->haveAccumBuffer
= GL_TRUE
;
392 util_format_get_component_bits(visual
->accum_format
,
393 UTIL_FORMAT_COLORSPACE_RGB
, 0);
394 mode
->accumGreenBits
=
395 util_format_get_component_bits(visual
->accum_format
,
396 UTIL_FORMAT_COLORSPACE_RGB
, 1);
397 mode
->accumBlueBits
=
398 util_format_get_component_bits(visual
->accum_format
,
399 UTIL_FORMAT_COLORSPACE_RGB
, 2);
400 mode
->accumAlphaBits
=
401 util_format_get_component_bits(visual
->accum_format
,
402 UTIL_FORMAT_COLORSPACE_RGB
, 3);
405 if (visual
->samples
> 1) {
406 mode
->sampleBuffers
= 1;
407 mode
->samples
= visual
->samples
;
412 * Create a framebuffer from a manager interface.
414 static struct st_framebuffer
*
415 st_framebuffer_create(struct st_context
*st
,
416 struct st_framebuffer_iface
*stfbi
)
418 struct st_framebuffer
*stfb
;
419 struct gl_config mode
;
425 stfb
= CALLOC_STRUCT(st_framebuffer
);
429 st_visual_to_context_mode(stfbi
->visual
, &mode
);
432 * For desktop GL, sRGB framebuffer write is controlled by both the
433 * capability of the framebuffer and GL_FRAMEBUFFER_SRGB. We should
434 * advertise the capability when the pipe driver (and core Mesa) supports
435 * it so that applications can enable sRGB write when they want to.
437 * This is not to be confused with GLX_FRAMEBUFFER_SRGB_CAPABLE_ARB. When
438 * the attribute is GLX_TRUE, it tells the st manager to pick a color
439 * format such that util_format_srgb(visual->color_format) can be supported
440 * by the pipe driver. We still need to advertise the capability here.
442 * For GLES, however, sRGB framebuffer write is controlled only by the
443 * capability of the framebuffer. There is GL_EXT_sRGB_write_control to
444 * give applications the control back, but sRGB write is still enabled by
445 * default. To avoid unexpected results, we should not advertise the
446 * capability. This could change when we add support for
447 * EGL_KHR_gl_colorspace.
449 if (_mesa_is_desktop_gl(st
->ctx
)) {
450 struct pipe_screen
*screen
= st
->pipe
->screen
;
451 const enum pipe_format srgb_format
=
452 util_format_srgb(stfbi
->visual
->color_format
);
454 if (srgb_format
!= PIPE_FORMAT_NONE
&&
455 st_pipe_format_to_mesa_format(srgb_format
) != MESA_FORMAT_NONE
&&
456 screen
->is_format_supported(screen
, srgb_format
,
457 PIPE_TEXTURE_2D
, stfbi
->visual
->samples
,
458 (PIPE_BIND_DISPLAY_TARGET
|
459 PIPE_BIND_RENDER_TARGET
)))
460 mode
.sRGBCapable
= GL_TRUE
;
463 _mesa_initialize_window_framebuffer(&stfb
->Base
, &mode
);
466 stfb
->iface_stamp
= p_atomic_read(&stfbi
->stamp
) - 1;
468 /* add the color buffer */
469 idx
= stfb
->Base
._ColorDrawBufferIndexes
[0];
470 if (!st_framebuffer_add_renderbuffer(stfb
, idx
)) {
475 st_framebuffer_add_renderbuffer(stfb
, BUFFER_DEPTH
);
476 st_framebuffer_add_renderbuffer(stfb
, BUFFER_ACCUM
);
479 st_framebuffer_update_attachments(stfb
);
485 * Reference a framebuffer.
488 st_framebuffer_reference(struct st_framebuffer
**ptr
,
489 struct st_framebuffer
*stfb
)
491 struct gl_framebuffer
*fb
= &stfb
->Base
;
492 _mesa_reference_framebuffer((struct gl_framebuffer
**) ptr
, fb
);
496 st_context_flush(struct st_context_iface
*stctxi
, unsigned flags
,
497 struct pipe_fence_handle
**fence
)
499 struct st_context
*st
= (struct st_context
*) stctxi
;
500 unsigned pipe_flags
= 0;
502 if (flags
& ST_FLUSH_END_OF_FRAME
) {
503 pipe_flags
|= PIPE_FLUSH_END_OF_FRAME
;
506 st_flush(st
, fence
, pipe_flags
);
507 if (flags
& ST_FLUSH_FRONT
)
508 st_manager_flush_frontbuffer(st
);
512 st_context_teximage(struct st_context_iface
*stctxi
,
513 enum st_texture_type tex_type
,
514 int level
, enum pipe_format pipe_format
,
515 struct pipe_resource
*tex
, boolean mipmap
)
517 struct st_context
*st
= (struct st_context
*) stctxi
;
518 struct gl_context
*ctx
= st
->ctx
;
519 struct gl_texture_object
*texObj
;
520 struct gl_texture_image
*texImage
;
521 struct st_texture_object
*stObj
;
522 struct st_texture_image
*stImage
;
523 GLenum internalFormat
;
524 GLuint width
, height
, depth
;
529 target
= GL_TEXTURE_1D
;
532 target
= GL_TEXTURE_2D
;
535 target
= GL_TEXTURE_3D
;
537 case ST_TEXTURE_RECT
:
538 target
= GL_TEXTURE_RECTANGLE_ARB
;
544 texObj
= _mesa_get_current_tex_object(ctx
, target
);
546 _mesa_lock_texture(ctx
, texObj
);
548 stObj
= st_texture_object(texObj
);
549 /* switch to surface based */
550 if (!stObj
->surface_based
) {
551 _mesa_clear_texture_object(ctx
, texObj
);
552 stObj
->surface_based
= GL_TRUE
;
555 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
556 stImage
= st_texture_image(texImage
);
558 mesa_format texFormat
= st_pipe_format_to_mesa_format(pipe_format
);
560 if (util_format_has_alpha(tex
->format
))
561 internalFormat
= GL_RGBA
;
563 internalFormat
= GL_RGB
;
565 _mesa_init_teximage_fields(ctx
, texImage
,
566 tex
->width0
, tex
->height0
, 1, 0,
567 internalFormat
, texFormat
);
570 height
= tex
->height0
;
573 /* grow the image size until we hit level = 0 */
585 _mesa_clear_texture_image(ctx
, texImage
);
586 width
= height
= depth
= 0;
589 pipe_resource_reference(&stImage
->pt
, tex
);
590 stObj
->surface_format
= pipe_format
;
592 _mesa_dirty_texobj(ctx
, texObj
);
593 _mesa_unlock_texture(ctx
, texObj
);
599 st_context_copy(struct st_context_iface
*stctxi
,
600 struct st_context_iface
*stsrci
, unsigned mask
)
602 struct st_context
*st
= (struct st_context
*) stctxi
;
603 struct st_context
*src
= (struct st_context
*) stsrci
;
605 _mesa_copy_context(src
->ctx
, st
->ctx
, mask
);
609 st_context_share(struct st_context_iface
*stctxi
,
610 struct st_context_iface
*stsrci
)
612 struct st_context
*st
= (struct st_context
*) stctxi
;
613 struct st_context
*src
= (struct st_context
*) stsrci
;
615 return _mesa_share_state(st
->ctx
, src
->ctx
);
619 st_context_destroy(struct st_context_iface
*stctxi
)
621 struct st_context
*st
= (struct st_context
*) stctxi
;
622 st_destroy_context(st
);
625 static struct st_context_iface
*
626 st_api_create_context(struct st_api
*stapi
, struct st_manager
*smapi
,
627 const struct st_context_attribs
*attribs
,
628 enum st_context_error
*error
,
629 struct st_context_iface
*shared_stctxi
)
631 struct st_context
*shared_ctx
= (struct st_context
*) shared_stctxi
;
632 struct st_context
*st
;
633 struct pipe_context
*pipe
;
634 struct gl_config mode
;
636 unsigned ctx_flags
= 0;
638 if (!(stapi
->profile_mask
& (1 << attribs
->profile
)))
641 switch (attribs
->profile
) {
642 case ST_PROFILE_DEFAULT
:
643 api
= API_OPENGL_COMPAT
;
645 case ST_PROFILE_OPENGL_ES1
:
648 case ST_PROFILE_OPENGL_ES2
:
651 case ST_PROFILE_OPENGL_CORE
:
652 api
= API_OPENGL_CORE
;
655 *error
= ST_CONTEXT_ERROR_BAD_API
;
659 if (attribs
->flags
& ST_CONTEXT_FLAG_ROBUST_ACCESS
)
660 ctx_flags
|= PIPE_CONTEXT_ROBUST_BUFFER_ACCESS
;
662 pipe
= smapi
->screen
->context_create(smapi
->screen
, NULL
, ctx_flags
);
664 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
668 st_visual_to_context_mode(&attribs
->visual
, &mode
);
669 st
= st_create_context(api
, pipe
, &mode
, shared_ctx
, &attribs
->options
);
671 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
676 if (attribs
->flags
& ST_CONTEXT_FLAG_DEBUG
) {
677 if (!_mesa_set_debug_state_int(st
->ctx
, GL_DEBUG_OUTPUT
, GL_TRUE
)) {
678 *error
= ST_CONTEXT_ERROR_NO_MEMORY
;
682 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_DEBUG_BIT
;
684 st_update_debug_callback(st
);
687 if (attribs
->flags
& ST_CONTEXT_FLAG_FORWARD_COMPATIBLE
)
688 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT
;
689 if (attribs
->flags
& ST_CONTEXT_FLAG_ROBUST_ACCESS
) {
690 st
->ctx
->Const
.ContextFlags
|= GL_CONTEXT_FLAG_ROBUST_ACCESS_BIT_ARB
;
691 st
->ctx
->Const
.RobustAccess
= GL_TRUE
;
693 if (attribs
->flags
& ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED
) {
694 st
->ctx
->Const
.ResetStrategy
= GL_LOSE_CONTEXT_ON_RESET_ARB
;
695 st_install_device_reset_callback(st
);
698 /* need to perform version check */
699 if (attribs
->major
> 1 || attribs
->minor
> 0) {
700 /* Is the actual version less than the requested version?
702 if (st
->ctx
->Version
< attribs
->major
* 10U + attribs
->minor
) {
703 *error
= ST_CONTEXT_ERROR_BAD_VERSION
;
704 st_destroy_context(st
);
709 st
->invalidate_on_gl_viewport
=
710 smapi
->get_param(smapi
, ST_MANAGER_BROKEN_INVALIDATE
);
712 st
->iface
.destroy
= st_context_destroy
;
713 st
->iface
.flush
= st_context_flush
;
714 st
->iface
.teximage
= st_context_teximage
;
715 st
->iface
.copy
= st_context_copy
;
716 st
->iface
.share
= st_context_share
;
717 st
->iface
.st_context_private
= (void *) smapi
;
718 st
->iface
.cso_context
= st
->cso_context
;
719 st
->iface
.pipe
= st
->pipe
;
721 *error
= ST_CONTEXT_SUCCESS
;
725 static struct st_context_iface
*
726 st_api_get_current(struct st_api
*stapi
)
728 GET_CURRENT_CONTEXT(ctx
);
729 struct st_context
*st
= (ctx
) ? ctx
->st
: NULL
;
731 return (st
) ? &st
->iface
: NULL
;
734 static struct st_framebuffer
*
735 st_framebuffer_reuse_or_create(struct st_context
*st
,
736 struct gl_framebuffer
*fb
,
737 struct st_framebuffer_iface
*stfbi
)
739 struct st_framebuffer
*cur
= st_ws_framebuffer(fb
), *stfb
= NULL
;
741 /* dummy framebuffers cant be used as st_framebuffer */
742 if (cur
&& &cur
->Base
!= _mesa_get_incomplete_framebuffer() &&
743 cur
->iface
== stfbi
) {
744 /* reuse the current stfb */
745 st_framebuffer_reference(&stfb
, cur
);
748 /* create a new one */
749 stfb
= st_framebuffer_create(st
, stfbi
);
756 st_api_make_current(struct st_api
*stapi
, struct st_context_iface
*stctxi
,
757 struct st_framebuffer_iface
*stdrawi
,
758 struct st_framebuffer_iface
*streadi
)
760 struct st_context
*st
= (struct st_context
*) stctxi
;
761 struct st_framebuffer
*stdraw
, *stread
;
764 _glapi_check_multithread();
767 /* reuse or create the draw fb */
768 stdraw
= st_framebuffer_reuse_or_create(st
,
769 st
->ctx
->WinSysDrawBuffer
, stdrawi
);
770 if (streadi
!= stdrawi
) {
771 /* do the same for the read fb */
772 stread
= st_framebuffer_reuse_or_create(st
,
773 st
->ctx
->WinSysReadBuffer
, streadi
);
777 /* reuse the draw fb for the read fb */
779 st_framebuffer_reference(&stread
, stdraw
);
782 if (stdraw
&& stread
) {
783 st_framebuffer_validate(stdraw
, st
);
784 if (stread
!= stdraw
)
785 st_framebuffer_validate(stread
, st
);
787 ret
= _mesa_make_current(st
->ctx
, &stdraw
->Base
, &stread
->Base
);
789 st
->draw_stamp
= stdraw
->stamp
- 1;
790 st
->read_stamp
= stread
->stamp
- 1;
791 st_context_validate(st
, stdraw
, stread
);
794 struct gl_framebuffer
*incomplete
= _mesa_get_incomplete_framebuffer();
795 ret
= _mesa_make_current(st
->ctx
, incomplete
, incomplete
);
798 st_framebuffer_reference(&stdraw
, NULL
);
799 st_framebuffer_reference(&stread
, NULL
);
802 ret
= _mesa_make_current(NULL
, NULL
, NULL
);
809 st_api_destroy(struct st_api
*stapi
)
814 * Flush the front buffer if the current context renders to the front buffer.
817 st_manager_flush_frontbuffer(struct st_context
*st
)
819 struct st_framebuffer
*stfb
= st_ws_framebuffer(st
->ctx
->DrawBuffer
);
820 struct st_renderbuffer
*strb
= NULL
;
823 strb
= st_renderbuffer(stfb
->Base
.Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
827 /* never a dummy fb */
828 assert(&stfb
->Base
!= _mesa_get_incomplete_framebuffer());
829 stfb
->iface
->flush_front(&st
->iface
, stfb
->iface
, ST_ATTACHMENT_FRONT_LEFT
);
833 * Return the surface of an EGLImage.
834 * FIXME: I think this should operate on resources, not surfaces
836 struct pipe_surface
*
837 st_manager_get_egl_image_surface(struct st_context
*st
, void *eglimg
)
839 struct st_manager
*smapi
=
840 (struct st_manager
*) st
->iface
.st_context_private
;
841 struct st_egl_image stimg
;
842 struct pipe_surface
*ps
, surf_tmpl
;
844 if (!smapi
|| !smapi
->get_egl_image
)
847 memset(&stimg
, 0, sizeof(stimg
));
848 if (!smapi
->get_egl_image(smapi
, eglimg
, &stimg
))
851 u_surface_default_template(&surf_tmpl
, stimg
.texture
);
852 surf_tmpl
.format
= stimg
.format
;
853 surf_tmpl
.u
.tex
.level
= stimg
.level
;
854 surf_tmpl
.u
.tex
.first_layer
= stimg
.layer
;
855 surf_tmpl
.u
.tex
.last_layer
= stimg
.layer
;
856 ps
= st
->pipe
->create_surface(st
->pipe
, stimg
.texture
, &surf_tmpl
);
857 pipe_resource_reference(&stimg
.texture
, NULL
);
863 * Re-validate the framebuffers.
866 st_manager_validate_framebuffers(struct st_context
*st
)
868 struct st_framebuffer
*stdraw
= st_ws_framebuffer(st
->ctx
->DrawBuffer
);
869 struct st_framebuffer
*stread
= st_ws_framebuffer(st
->ctx
->ReadBuffer
);
872 st_framebuffer_validate(stdraw
, st
);
873 if (stread
&& stread
!= stdraw
)
874 st_framebuffer_validate(stread
, st
);
876 st_context_validate(st
, stdraw
, stread
);
880 * Add a color renderbuffer on demand.
883 st_manager_add_color_renderbuffer(struct st_context
*st
,
884 struct gl_framebuffer
*fb
,
887 struct st_framebuffer
*stfb
= st_ws_framebuffer(fb
);
893 if (stfb
->Base
.Attachment
[idx
].Renderbuffer
)
897 case BUFFER_FRONT_LEFT
:
898 case BUFFER_BACK_LEFT
:
899 case BUFFER_FRONT_RIGHT
:
900 case BUFFER_BACK_RIGHT
:
906 if (!st_framebuffer_add_renderbuffer(stfb
, idx
))
909 st_framebuffer_update_attachments(stfb
);
912 * Force a call to the state tracker manager to validate the
913 * new renderbuffer. It might be that there is a window system
914 * renderbuffer available.
917 stfb
->iface_stamp
= p_atomic_read(&stfb
->iface
->stamp
) - 1;
919 st_invalidate_state(st
->ctx
, _NEW_BUFFERS
);
924 static unsigned get_version(struct pipe_screen
*screen
,
925 struct st_config_options
*options
, gl_api api
)
927 struct gl_constants consts
= {0};
928 struct gl_extensions extensions
= {0};
931 if (_mesa_override_gl_version_contextless(&consts
, &api
, &version
)) {
935 _mesa_init_constants(&consts
, api
);
936 _mesa_init_extensions(&extensions
);
938 st_init_limits(screen
, &consts
, &extensions
);
939 st_init_extensions(screen
, &consts
, &extensions
, options
, GL_TRUE
);
941 return _mesa_get_version(&extensions
, &consts
, api
);
945 st_api_query_versions(struct st_api
*stapi
, struct st_manager
*sm
,
946 struct st_config_options
*options
,
947 int *gl_core_version
,
948 int *gl_compat_version
,
952 *gl_core_version
= get_version(sm
->screen
, options
, API_OPENGL_CORE
);
953 *gl_compat_version
= get_version(sm
->screen
, options
, API_OPENGL_COMPAT
);
954 *gl_es1_version
= get_version(sm
->screen
, options
, API_OPENGLES
);
955 *gl_es2_version
= get_version(sm
->screen
, options
, API_OPENGLES2
);
958 static const struct st_api st_gl_api
= {
959 .name
= "Mesa " PACKAGE_VERSION
,
960 .api
= ST_API_OPENGL
,
961 .profile_mask
= ST_PROFILE_DEFAULT_MASK
|
962 ST_PROFILE_OPENGL_CORE_MASK
|
963 ST_PROFILE_OPENGL_ES1_MASK
|
964 ST_PROFILE_OPENGL_ES2_MASK
|
966 .feature_mask
= ST_API_FEATURE_MS_VISUALS_MASK
,
967 .destroy
= st_api_destroy
,
968 .query_versions
= st_api_query_versions
,
969 .create_context
= st_api_create_context
,
970 .make_current
= st_api_make_current
,
971 .get_current
= st_api_get_current
,
975 st_gl_api_create(void)
977 return (struct st_api
*) &st_gl_api
;