2 * Mesa 3-D graphics library
5 * Copyright (C) 2010 LunarG Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 * Chia-I Wu <olv@lunarg.com>
28 #include "state_tracker/st_api.h"
30 #include "pipe/p_context.h"
31 #include "pipe/p_screen.h"
32 #include "util/u_format.h"
33 #include "util/u_pointer.h"
34 #include "util/u_inlines.h"
35 #include "util/u_atomic.h"
37 #include "main/mtypes.h"
38 #include "main/context.h"
39 #include "main/texobj.h"
40 #include "main/teximage.h"
41 #include "main/texstate.h"
42 #include "main/texfetch.h"
43 #include "main/fbobject.h"
44 #include "main/framebuffer.h"
45 #include "main/renderbuffer.h"
46 #include "st_texture.h"
48 #include "st_context.h"
49 #include "st_format.h"
50 #include "st_cb_fbo.h"
51 #include "st_manager.h"
53 /* these functions are defined in st_context.c */
55 st_create_context(struct pipe_context
*pipe
,
56 const __GLcontextModes
*visual
,
57 struct st_context
*share
);
58 void st_destroy_context(struct st_context
*st
);
59 void st_flush(struct st_context
*st
, uint pipeFlushFlags
,
60 struct pipe_fence_handle
**fence
);
63 * Map an attachment to a buffer index.
65 static INLINE gl_buffer_index
66 attachment_to_buffer_index(enum st_attachment_type statt
)
68 gl_buffer_index index
;
71 case ST_ATTACHMENT_FRONT_LEFT
:
72 index
= BUFFER_FRONT_LEFT
;
74 case ST_ATTACHMENT_BACK_LEFT
:
75 index
= BUFFER_BACK_LEFT
;
77 case ST_ATTACHMENT_FRONT_RIGHT
:
78 index
= BUFFER_FRONT_RIGHT
;
80 case ST_ATTACHMENT_BACK_RIGHT
:
81 index
= BUFFER_BACK_RIGHT
;
83 case ST_ATTACHMENT_DEPTH_STENCIL
:
86 case ST_ATTACHMENT_ACCUM
:
89 case ST_ATTACHMENT_SAMPLE
:
99 * Map a buffer index to an attachment.
101 static INLINE
enum st_attachment_type
102 buffer_index_to_attachment(gl_buffer_index index
)
104 enum st_attachment_type statt
;
107 case BUFFER_FRONT_LEFT
:
108 statt
= ST_ATTACHMENT_FRONT_LEFT
;
110 case BUFFER_BACK_LEFT
:
111 statt
= ST_ATTACHMENT_BACK_LEFT
;
113 case BUFFER_FRONT_RIGHT
:
114 statt
= ST_ATTACHMENT_FRONT_RIGHT
;
116 case BUFFER_BACK_RIGHT
:
117 statt
= ST_ATTACHMENT_BACK_RIGHT
;
120 statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
123 statt
= ST_ATTACHMENT_ACCUM
;
126 statt
= ST_ATTACHMENT_INVALID
;
134 * Validate a framebuffer and update the states of the context.
137 st_framebuffer_validate(struct st_framebuffer
*stfb
, struct st_context
*st
)
139 struct pipe_screen
*screen
= st
->pipe
->screen
;
140 struct pipe_texture
*textures
[ST_ATTACHMENT_COUNT
];
143 boolean changed
= FALSE
;
145 if (!p_atomic_read(&stfb
->revalidate
))
148 /* validate the fb */
149 if (!stfb
->iface
->validate(stfb
->iface
, stfb
->statts
, stfb
->num_statts
, textures
))
152 width
= stfb
->Base
.Width
;
153 height
= stfb
->Base
.Height
;
155 for (i
= 0; i
< stfb
->num_statts
; i
++) {
156 struct st_renderbuffer
*strb
;
157 struct pipe_surface
*ps
;
163 idx
= attachment_to_buffer_index(stfb
->statts
[i
]);
164 if (idx
>= BUFFER_COUNT
) {
165 pipe_texture_reference(&textures
[i
], NULL
);
169 strb
= st_renderbuffer(stfb
->Base
.Attachment
[idx
].Renderbuffer
);
171 if (strb
->texture
== textures
[i
]) {
172 pipe_texture_reference(&textures
[i
], NULL
);
176 ps
= screen
->get_tex_surface(screen
, textures
[i
], 0, 0, 0,
177 PIPE_BUFFER_USAGE_GPU_READ
| PIPE_BUFFER_USAGE_GPU_WRITE
);
179 pipe_surface_reference(&strb
->surface
, ps
);
180 pipe_texture_reference(&strb
->texture
, ps
->texture
);
181 /* ownership transfered */
182 pipe_surface_reference(&ps
, NULL
);
186 strb
->Base
.Width
= strb
->surface
->width
;
187 strb
->Base
.Height
= strb
->surface
->height
;
189 width
= strb
->Base
.Width
;
190 height
= strb
->Base
.Height
;
193 pipe_texture_reference(&textures
[i
], NULL
);
197 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
198 _mesa_resize_framebuffer(st
->ctx
, &stfb
->Base
, width
, height
);
200 assert(stfb
->Base
.Width
== width
);
201 assert(stfb
->Base
.Height
== height
);
204 p_atomic_set(&stfb
->revalidate
, FALSE
);
208 * Update the attachments to validate.
211 st_framebuffer_update_attachments(struct st_framebuffer
*stfb
)
215 stfb
->num_statts
= 0;
216 for (idx
= 0; idx
< BUFFER_COUNT
; idx
++) {
217 struct st_renderbuffer
*strb
;
218 enum st_attachment_type statt
;
220 strb
= st_renderbuffer(stfb
->Base
.Attachment
[idx
].Renderbuffer
);
221 if (!strb
|| strb
->software
)
224 statt
= buffer_index_to_attachment(idx
);
225 if (statt
!= ST_ATTACHMENT_INVALID
&&
226 st_visual_have_buffers(stfb
->iface
->visual
, 1 << statt
))
227 stfb
->statts
[stfb
->num_statts
++] = statt
;
232 * Add a renderbuffer to the framebuffer.
235 st_framebuffer_add_renderbuffer(struct st_framebuffer
*stfb
,
238 struct gl_renderbuffer
*rb
;
239 enum pipe_format format
;
243 /* do not distinguish depth/stencil buffers */
244 if (idx
== BUFFER_STENCIL
)
249 format
= stfb
->iface
->visual
->depth_stencil_format
;
253 format
= stfb
->iface
->visual
->accum_format
;
257 format
= stfb
->iface
->visual
->color_format
;
262 if (format
== PIPE_FORMAT_NONE
)
265 samples
= stfb
->iface
->visual
->samples
;
267 samples
= st_get_msaa();
269 rb
= st_new_renderbuffer_fb(format
, samples
, sw
);
273 _mesa_add_renderbuffer(&stfb
->Base
, idx
, rb
);
274 if (idx
== BUFFER_DEPTH
)
275 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_STENCIL
, rb
);
281 * Intialize a __GLcontextModes from a visual.
284 st_visual_to_context_mode(const struct st_visual
*visual
,
285 __GLcontextModes
*mode
)
287 memset(mode
, 0, sizeof(*mode
));
289 if (st_visual_have_buffers(visual
, ST_ATTACHMENT_BACK_LEFT_MASK
))
290 mode
->doubleBufferMode
= GL_TRUE
;
291 if (st_visual_have_buffers(visual
,
292 ST_ATTACHMENT_FRONT_RIGHT_MASK
| ST_ATTACHMENT_BACK_RIGHT_MASK
))
293 mode
->stereoMode
= GL_TRUE
;
295 if (visual
->color_format
!= PIPE_FORMAT_NONE
) {
296 mode
->rgbMode
= GL_TRUE
;
299 util_format_get_component_bits(visual
->color_format
,
300 UTIL_FORMAT_COLORSPACE_RGB
, 0);
302 util_format_get_component_bits(visual
->color_format
,
303 UTIL_FORMAT_COLORSPACE_RGB
, 1);
305 util_format_get_component_bits(visual
->color_format
,
306 UTIL_FORMAT_COLORSPACE_RGB
, 2);
308 util_format_get_component_bits(visual
->color_format
,
309 UTIL_FORMAT_COLORSPACE_RGB
, 3);
311 mode
->rgbBits
= mode
->redBits
+
312 mode
->greenBits
+ mode
->blueBits
+ mode
->alphaBits
;
315 if (visual
->depth_stencil_format
!= PIPE_FORMAT_NONE
) {
316 mode
->haveDepthBuffer
= GL_TRUE
;
317 mode
->haveStencilBuffer
= GL_TRUE
;
320 util_format_get_component_bits(visual
->depth_stencil_format
,
321 UTIL_FORMAT_COLORSPACE_ZS
, 0);
323 util_format_get_component_bits(visual
->depth_stencil_format
,
324 UTIL_FORMAT_COLORSPACE_ZS
, 1);
327 if (visual
->accum_format
!= PIPE_FORMAT_NONE
) {
328 mode
->haveAccumBuffer
= GL_TRUE
;
331 util_format_get_component_bits(visual
->accum_format
,
332 UTIL_FORMAT_COLORSPACE_RGB
, 0);
333 mode
->accumGreenBits
=
334 util_format_get_component_bits(visual
->accum_format
,
335 UTIL_FORMAT_COLORSPACE_RGB
, 1);
336 mode
->accumBlueBits
=
337 util_format_get_component_bits(visual
->accum_format
,
338 UTIL_FORMAT_COLORSPACE_RGB
, 2);
339 mode
->accumAlphaBits
=
340 util_format_get_component_bits(visual
->accum_format
,
341 UTIL_FORMAT_COLORSPACE_RGB
, 3);
344 if (visual
->samples
) {
345 mode
->sampleBuffers
= 1;
346 mode
->samples
= visual
->samples
;
351 * Determine the default draw or read buffer from a visual.
354 st_visual_to_default_buffer(const struct st_visual
*visual
,
355 GLenum
*buffer
, GLint
*index
)
357 enum st_attachment_type statt
;
361 statt
= visual
->render_buffer
;
362 /* do nothing if an invalid render buffer is specified */
363 if (statt
== ST_ATTACHMENT_INVALID
||
364 !st_visual_have_buffers(visual
, 1 << statt
))
368 case ST_ATTACHMENT_FRONT_LEFT
:
370 idx
= BUFFER_FRONT_LEFT
;
372 case ST_ATTACHMENT_BACK_LEFT
:
374 idx
= BUFFER_BACK_LEFT
;
376 case ST_ATTACHMENT_FRONT_RIGHT
:
377 buf
= GL_FRONT_RIGHT
;
378 idx
= BUFFER_FRONT_RIGHT
;
380 case ST_ATTACHMENT_BACK_RIGHT
:
382 idx
= BUFFER_BACK_RIGHT
;
390 if (buf
!= GL_NONE
) {
399 * Create a framebuffer from a manager interface.
401 static struct st_framebuffer
*
402 st_framebuffer_create(struct st_framebuffer_iface
*stfbi
)
404 struct st_framebuffer
*stfb
;
405 __GLcontextModes mode
;
408 stfb
= CALLOC_STRUCT(st_framebuffer
);
412 st_visual_to_context_mode(stfbi
->visual
, &mode
);
413 _mesa_initialize_window_framebuffer(&stfb
->Base
, &mode
);
415 /* modify the draw/read buffers of the fb */
416 st_visual_to_default_buffer(stfbi
->visual
, &stfb
->Base
.ColorDrawBuffer
[0],
417 &stfb
->Base
._ColorDrawBufferIndexes
[0]);
418 st_visual_to_default_buffer(stfbi
->visual
, &stfb
->Base
.ColorReadBuffer
,
419 &stfb
->Base
._ColorReadBufferIndex
);
423 /* add the color buffer */
424 idx
= stfb
->Base
._ColorDrawBufferIndexes
[0];
425 if (!st_framebuffer_add_renderbuffer(stfb
, idx
)) {
430 st_framebuffer_add_renderbuffer(stfb
, BUFFER_DEPTH
);
431 st_framebuffer_add_renderbuffer(stfb
, BUFFER_ACCUM
);
433 st_framebuffer_update_attachments(stfb
);
435 p_atomic_set(&stfb
->revalidate
, TRUE
);
436 stfb
->Base
.Initialized
= GL_TRUE
;
442 * Reference a framebuffer.
445 st_framebuffer_reference(struct st_framebuffer
**ptr
,
446 struct st_framebuffer
*stfb
)
448 GLframebuffer
*fb
= &stfb
->Base
;
449 _mesa_reference_framebuffer((GLframebuffer
**) ptr
, fb
);
453 st_context_notify_invalid_framebuffer(struct st_context_iface
*stctxi
,
454 struct st_framebuffer_iface
*stfbi
)
456 struct st_context
*st
= (struct st_context
*) stctxi
;
457 struct st_framebuffer
*stfb
;
459 /* either draw or read winsys fb */
460 stfb
= (struct st_framebuffer
*) st
->ctx
->WinSysDrawBuffer
;
461 if (!stfb
|| stfb
->iface
!= stfbi
)
462 stfb
= (struct st_framebuffer
*) st
->ctx
->WinSysReadBuffer
;
463 assert(stfb
&& stfb
->iface
== stfbi
);
465 p_atomic_set(&stfb
->revalidate
, TRUE
);
469 st_context_flush(struct st_context_iface
*stctxi
, unsigned flags
,
470 struct pipe_fence_handle
**fence
)
472 struct st_context
*st
= (struct st_context
*) stctxi
;
473 st_flush(st
, flags
, fence
);
474 if (flags
& PIPE_FLUSH_RENDER_CACHE
)
475 st_manager_flush_frontbuffer(st
);
479 st_context_teximage(struct st_context_iface
*stctxi
, enum st_texture_type target
,
480 int level
, enum pipe_format internal_format
,
481 struct pipe_texture
*tex
, boolean mipmap
)
483 struct st_context
*st
= (struct st_context
*) stctxi
;
484 GLcontext
*ctx
= st
->ctx
;
485 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
486 struct gl_texture_object
*texObj
;
487 struct gl_texture_image
*texImage
;
488 struct st_texture_object
*stObj
;
489 struct st_texture_image
*stImage
;
490 GLenum internalFormat
;
494 target
= GL_TEXTURE_1D
;
497 target
= GL_TEXTURE_2D
;
500 target
= GL_TEXTURE_3D
;
502 case ST_TEXTURE_RECT
:
503 target
= GL_TEXTURE_RECTANGLE_ARB
;
510 if (util_format_get_component_bits(internal_format
,
511 UTIL_FORMAT_COLORSPACE_RGB
, 3) > 0)
512 internalFormat
= GL_RGBA
;
514 internalFormat
= GL_RGB
;
516 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
517 _mesa_lock_texture(ctx
, texObj
);
519 stObj
= st_texture_object(texObj
);
520 /* switch to surface based */
521 if (!stObj
->surface_based
) {
522 _mesa_clear_texture_object(ctx
, texObj
);
523 stObj
->surface_based
= GL_TRUE
;
526 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
527 stImage
= st_texture_image(texImage
);
529 _mesa_init_teximage_fields(ctx
, target
, texImage
,
530 tex
->width0
, tex
->height0
, 1, 0, internalFormat
);
531 texImage
->TexFormat
= st_ChooseTextureFormat(ctx
, internalFormat
,
532 GL_RGBA
, GL_UNSIGNED_BYTE
);
533 _mesa_set_fetch_functions(texImage
, 2);
536 _mesa_clear_texture_image(ctx
, texImage
);
539 pipe_texture_reference(&stImage
->pt
, tex
);
541 _mesa_dirty_texobj(ctx
, texObj
, GL_TRUE
);
542 _mesa_unlock_texture(ctx
, texObj
);
548 st_context_destroy(struct st_context_iface
*stctxi
)
550 struct st_context
*st
= (struct st_context
*) stctxi
;
551 st_destroy_context(st
);
554 static struct st_context_iface
*
555 st_api_create_context(struct st_api
*stapi
, struct st_manager
*smapi
,
556 const struct st_visual
*visual
,
557 struct st_context_iface
*shared_stctxi
)
559 struct st_context
*shared_ctx
= (struct st_context
*) shared_stctxi
;
560 struct st_context
*st
;
561 struct pipe_context
*pipe
;
562 __GLcontextModes mode
;
564 pipe
= smapi
->screen
->context_create(smapi
->screen
, NULL
);
568 st_visual_to_context_mode(visual
, &mode
);
569 st
= st_create_context(pipe
, &mode
, shared_ctx
);
575 st
->iface
.destroy
= st_context_destroy
;
577 st
->iface
.notify_invalid_framebuffer
=
578 st_context_notify_invalid_framebuffer
;
579 st
->iface
.flush
= st_context_flush
;
581 st
->iface
.teximage
= st_context_teximage
;
582 st
->iface
.copy
= NULL
;
584 st
->iface
.st_context_private
= (void *) smapi
;
590 st_api_make_current(struct st_api
*stapi
, struct st_context_iface
*stctxi
,
591 struct st_framebuffer_iface
*stdrawi
,
592 struct st_framebuffer_iface
*streadi
)
594 struct st_context
*st
= (struct st_context
*) stctxi
;
595 struct st_framebuffer
*stdraw
, *stread
, *stfb
;
598 _glapi_check_multithread();
601 /* reuse/create the draw fb */
602 stfb
= (struct st_framebuffer
* ) st
->ctx
->DrawBuffer
;
603 if (stfb
&& stfb
->iface
== stdrawi
) {
605 st_framebuffer_reference(&stdraw
, stfb
);
608 stdraw
= st_framebuffer_create(stdrawi
);
611 /* reuse/create the read fb */
612 stfb
= (struct st_framebuffer
* ) st
->ctx
->ReadBuffer
;
613 if (!stfb
|| stfb
->iface
!= streadi
)
615 if (stfb
&& stfb
->iface
== streadi
) {
617 st_framebuffer_reference(&stread
, stfb
);
620 stread
= st_framebuffer_create(streadi
);
623 if (stdraw
&& stread
) {
624 st_framebuffer_validate(stdraw
, st
);
625 if (stread
!= stdraw
)
626 st_framebuffer_validate(stread
, st
);
628 /* modify the draw/read buffers of the context */
629 st_visual_to_default_buffer(stdraw
->iface
->visual
,
630 &st
->ctx
->Color
.DrawBuffer
[0], NULL
);
631 st_visual_to_default_buffer(stread
->iface
->visual
,
632 &st
->ctx
->Pixel
.ReadBuffer
, NULL
);
634 ret
= _mesa_make_current(st
->ctx
, &stdraw
->Base
, &stread
->Base
);
640 st_framebuffer_reference(&stdraw
, NULL
);
641 st_framebuffer_reference(&stread
, NULL
);
644 ret
= _mesa_make_current(NULL
, NULL
, NULL
);
650 static struct st_context_iface
*
651 st_api_get_current(struct st_api
*stapi
)
653 GET_CURRENT_CONTEXT(ctx
);
654 struct st_context
*st
= (ctx
) ? ctx
->st
: NULL
;
656 return (st
) ? &st
->iface
: NULL
;
660 st_api_is_visual_supported(struct st_api
*stapi
,
661 const struct st_visual
*visual
)
667 st_api_get_proc_address(struct st_api
*stapi
, const char *procname
)
669 return (st_proc_t
) _glapi_get_proc_address(procname
);
673 st_api_destroy(struct st_api
*stapi
)
679 * Flush the front buffer if the current context renders to the front buffer.
682 st_manager_flush_frontbuffer(struct st_context
*st
)
684 struct st_framebuffer
*stfb
= (struct st_framebuffer
*) st
->ctx
->DrawBuffer
;
685 struct st_renderbuffer
*strb
= NULL
;
688 strb
= st_renderbuffer(stfb
->Base
.Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
692 /* st_public.h or FBO */
694 struct pipe_surface
*front_surf
= strb
->surface
;
695 st
->pipe
->screen
->flush_frontbuffer(st
->pipe
->screen
,
696 front_surf
, st
->winsys_drawable_handle
);
700 stfb
->iface
->flush_front(stfb
->iface
, ST_ATTACHMENT_FRONT_LEFT
);
704 * Re-validate the framebuffer.
707 st_manager_validate_framebuffers(struct st_context
*st
)
709 struct st_framebuffer
*stdraw
, *stread
;
711 stdraw
= (struct st_framebuffer
*) st
->ctx
->DrawBuffer
;
712 stread
= (struct st_framebuffer
*) st
->ctx
->ReadBuffer
;
714 /* st_public.h or FBO */
715 if ((stdraw
&& !stdraw
->iface
) || (stread
&& !stread
->iface
)) {
716 struct pipe_screen
*screen
= st
->pipe
->screen
;
717 if (screen
->update_buffer
)
718 screen
->update_buffer(screen
, st
->pipe
->priv
);
723 st_framebuffer_validate(stdraw
, st
);
724 if (stread
&& stread
!= stdraw
)
725 st_framebuffer_validate(stread
, st
);
729 * Add a color buffer on demand.
732 st_manager_add_color_renderbuffer(struct st_context
*st
, GLframebuffer
*fb
,
735 struct st_framebuffer
*stfb
= (struct st_framebuffer
*) fb
;
737 if (stfb
->Base
.Attachment
[idx
].Renderbuffer
)
740 /* st_public.h or FBO */
745 case BUFFER_FRONT_LEFT
:
746 case BUFFER_BACK_LEFT
:
747 case BUFFER_FRONT_RIGHT
:
748 case BUFFER_BACK_RIGHT
:
755 if (!st_framebuffer_add_renderbuffer(stfb
, idx
))
758 st_framebuffer_update_attachments(stfb
);
759 st_invalidate_state(st
->ctx
, _NEW_BUFFERS
);
765 st_manager_create_api(void)
767 struct st_api
*stapi
;
769 stapi
= CALLOC_STRUCT(st_api
);
771 stapi
->destroy
= st_api_destroy
;
772 stapi
->get_proc_address
= st_api_get_proc_address
;
773 stapi
->is_visual_supported
= st_api_is_visual_supported
;
775 stapi
->create_context
= st_api_create_context
;
776 stapi
->make_current
= st_api_make_current
;
777 stapi
->get_current
= st_api_get_current
;