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 * Note that this function may fail.
65 static INLINE
struct st_framebuffer
*
66 st_framebuffer(GLframebuffer
*fb
)
68 /* FBO cannot be casted. See st_new_framebuffer */
69 return (struct st_framebuffer
*) ((fb
&& !fb
->Name
) ? fb
: NULL
);
73 * Map an attachment to a buffer index.
75 static INLINE gl_buffer_index
76 attachment_to_buffer_index(enum st_attachment_type statt
)
78 gl_buffer_index index
;
81 case ST_ATTACHMENT_FRONT_LEFT
:
82 index
= BUFFER_FRONT_LEFT
;
84 case ST_ATTACHMENT_BACK_LEFT
:
85 index
= BUFFER_BACK_LEFT
;
87 case ST_ATTACHMENT_FRONT_RIGHT
:
88 index
= BUFFER_FRONT_RIGHT
;
90 case ST_ATTACHMENT_BACK_RIGHT
:
91 index
= BUFFER_BACK_RIGHT
;
93 case ST_ATTACHMENT_DEPTH_STENCIL
:
96 case ST_ATTACHMENT_ACCUM
:
99 case ST_ATTACHMENT_SAMPLE
:
101 index
= BUFFER_COUNT
;
109 * Map a buffer index to an attachment.
111 static INLINE
enum st_attachment_type
112 buffer_index_to_attachment(gl_buffer_index index
)
114 enum st_attachment_type statt
;
117 case BUFFER_FRONT_LEFT
:
118 statt
= ST_ATTACHMENT_FRONT_LEFT
;
120 case BUFFER_BACK_LEFT
:
121 statt
= ST_ATTACHMENT_BACK_LEFT
;
123 case BUFFER_FRONT_RIGHT
:
124 statt
= ST_ATTACHMENT_FRONT_RIGHT
;
126 case BUFFER_BACK_RIGHT
:
127 statt
= ST_ATTACHMENT_BACK_RIGHT
;
130 statt
= ST_ATTACHMENT_DEPTH_STENCIL
;
133 statt
= ST_ATTACHMENT_ACCUM
;
136 statt
= ST_ATTACHMENT_INVALID
;
144 * Validate a framebuffer and update the states of the context.
147 st_framebuffer_validate(struct st_framebuffer
*stfb
, struct st_context
*st
)
149 struct pipe_screen
*screen
= st
->pipe
->screen
;
150 struct pipe_texture
*textures
[ST_ATTACHMENT_COUNT
];
153 boolean changed
= FALSE
;
155 if (!p_atomic_read(&stfb
->revalidate
))
158 /* validate the fb */
159 if (!stfb
->iface
->validate(stfb
->iface
, stfb
->statts
, stfb
->num_statts
, textures
))
162 width
= stfb
->Base
.Width
;
163 height
= stfb
->Base
.Height
;
165 for (i
= 0; i
< stfb
->num_statts
; i
++) {
166 struct st_renderbuffer
*strb
;
167 struct pipe_surface
*ps
;
173 idx
= attachment_to_buffer_index(stfb
->statts
[i
]);
174 if (idx
>= BUFFER_COUNT
) {
175 pipe_texture_reference(&textures
[i
], NULL
);
179 strb
= st_renderbuffer(stfb
->Base
.Attachment
[idx
].Renderbuffer
);
181 if (strb
->texture
== textures
[i
]) {
182 pipe_texture_reference(&textures
[i
], NULL
);
186 ps
= screen
->get_tex_surface(screen
, textures
[i
], 0, 0, 0,
187 PIPE_BUFFER_USAGE_GPU_READ
| PIPE_BUFFER_USAGE_GPU_WRITE
);
189 pipe_surface_reference(&strb
->surface
, ps
);
190 pipe_texture_reference(&strb
->texture
, ps
->texture
);
191 /* ownership transfered */
192 pipe_surface_reference(&ps
, NULL
);
196 strb
->Base
.Width
= strb
->surface
->width
;
197 strb
->Base
.Height
= strb
->surface
->height
;
199 width
= strb
->Base
.Width
;
200 height
= strb
->Base
.Height
;
203 pipe_texture_reference(&textures
[i
], NULL
);
207 st
->dirty
.st
|= ST_NEW_FRAMEBUFFER
;
208 _mesa_resize_framebuffer(st
->ctx
, &stfb
->Base
, width
, height
);
210 assert(stfb
->Base
.Width
== width
);
211 assert(stfb
->Base
.Height
== height
);
214 p_atomic_set(&stfb
->revalidate
, FALSE
);
218 * Update the attachments to validate.
221 st_framebuffer_update_attachments(struct st_framebuffer
*stfb
)
225 stfb
->num_statts
= 0;
226 for (idx
= 0; idx
< BUFFER_COUNT
; idx
++) {
227 struct st_renderbuffer
*strb
;
228 enum st_attachment_type statt
;
230 strb
= st_renderbuffer(stfb
->Base
.Attachment
[idx
].Renderbuffer
);
231 if (!strb
|| strb
->software
)
234 statt
= buffer_index_to_attachment(idx
);
235 if (statt
!= ST_ATTACHMENT_INVALID
&&
236 st_visual_have_buffers(stfb
->iface
->visual
, 1 << statt
))
237 stfb
->statts
[stfb
->num_statts
++] = statt
;
240 p_atomic_set(&stfb
->revalidate
, TRUE
);
244 * Add a renderbuffer to the framebuffer.
247 st_framebuffer_add_renderbuffer(struct st_framebuffer
*stfb
,
250 struct gl_renderbuffer
*rb
;
251 enum pipe_format format
;
255 /* do not distinguish depth/stencil buffers */
256 if (idx
== BUFFER_STENCIL
)
261 format
= stfb
->iface
->visual
->depth_stencil_format
;
265 format
= stfb
->iface
->visual
->accum_format
;
269 format
= stfb
->iface
->visual
->color_format
;
274 if (format
== PIPE_FORMAT_NONE
)
277 samples
= stfb
->iface
->visual
->samples
;
279 samples
= st_get_msaa();
281 rb
= st_new_renderbuffer_fb(format
, samples
, sw
);
285 if (idx
!= BUFFER_DEPTH
) {
286 _mesa_add_renderbuffer(&stfb
->Base
, idx
, rb
);
289 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_ZS
, 0))
290 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, rb
);
291 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_ZS
, 1))
292 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_STENCIL
, rb
);
299 * Intialize a __GLcontextModes from a visual.
302 st_visual_to_context_mode(const struct st_visual
*visual
,
303 __GLcontextModes
*mode
)
305 memset(mode
, 0, sizeof(*mode
));
307 if (st_visual_have_buffers(visual
, ST_ATTACHMENT_BACK_LEFT_MASK
))
308 mode
->doubleBufferMode
= GL_TRUE
;
309 if (st_visual_have_buffers(visual
,
310 ST_ATTACHMENT_FRONT_RIGHT_MASK
| ST_ATTACHMENT_BACK_RIGHT_MASK
))
311 mode
->stereoMode
= GL_TRUE
;
313 if (visual
->color_format
!= PIPE_FORMAT_NONE
) {
314 mode
->rgbMode
= GL_TRUE
;
317 util_format_get_component_bits(visual
->color_format
,
318 UTIL_FORMAT_COLORSPACE_RGB
, 0);
320 util_format_get_component_bits(visual
->color_format
,
321 UTIL_FORMAT_COLORSPACE_RGB
, 1);
323 util_format_get_component_bits(visual
->color_format
,
324 UTIL_FORMAT_COLORSPACE_RGB
, 2);
326 util_format_get_component_bits(visual
->color_format
,
327 UTIL_FORMAT_COLORSPACE_RGB
, 3);
329 mode
->rgbBits
= mode
->redBits
+
330 mode
->greenBits
+ mode
->blueBits
+ mode
->alphaBits
;
333 if (visual
->depth_stencil_format
!= PIPE_FORMAT_NONE
) {
334 mode
->haveDepthBuffer
= GL_TRUE
;
335 mode
->haveStencilBuffer
= GL_TRUE
;
338 util_format_get_component_bits(visual
->depth_stencil_format
,
339 UTIL_FORMAT_COLORSPACE_ZS
, 0);
341 util_format_get_component_bits(visual
->depth_stencil_format
,
342 UTIL_FORMAT_COLORSPACE_ZS
, 1);
345 if (visual
->accum_format
!= PIPE_FORMAT_NONE
) {
346 mode
->haveAccumBuffer
= GL_TRUE
;
349 util_format_get_component_bits(visual
->accum_format
,
350 UTIL_FORMAT_COLORSPACE_RGB
, 0);
351 mode
->accumGreenBits
=
352 util_format_get_component_bits(visual
->accum_format
,
353 UTIL_FORMAT_COLORSPACE_RGB
, 1);
354 mode
->accumBlueBits
=
355 util_format_get_component_bits(visual
->accum_format
,
356 UTIL_FORMAT_COLORSPACE_RGB
, 2);
357 mode
->accumAlphaBits
=
358 util_format_get_component_bits(visual
->accum_format
,
359 UTIL_FORMAT_COLORSPACE_RGB
, 3);
362 if (visual
->samples
) {
363 mode
->sampleBuffers
= 1;
364 mode
->samples
= visual
->samples
;
369 * Determine the default draw or read buffer from a visual.
372 st_visual_to_default_buffer(const struct st_visual
*visual
,
373 GLenum
*buffer
, GLint
*index
)
375 enum st_attachment_type statt
;
379 statt
= visual
->render_buffer
;
380 /* do nothing if an invalid render buffer is specified */
381 if (statt
== ST_ATTACHMENT_INVALID
||
382 !st_visual_have_buffers(visual
, 1 << statt
))
386 case ST_ATTACHMENT_FRONT_LEFT
:
388 idx
= BUFFER_FRONT_LEFT
;
390 case ST_ATTACHMENT_BACK_LEFT
:
392 idx
= BUFFER_BACK_LEFT
;
394 case ST_ATTACHMENT_FRONT_RIGHT
:
395 buf
= GL_FRONT_RIGHT
;
396 idx
= BUFFER_FRONT_RIGHT
;
398 case ST_ATTACHMENT_BACK_RIGHT
:
400 idx
= BUFFER_BACK_RIGHT
;
408 if (buf
!= GL_NONE
) {
417 * Create a framebuffer from a manager interface.
419 static struct st_framebuffer
*
420 st_framebuffer_create(struct st_framebuffer_iface
*stfbi
)
422 struct st_framebuffer
*stfb
;
423 __GLcontextModes mode
;
426 stfb
= CALLOC_STRUCT(st_framebuffer
);
430 st_visual_to_context_mode(stfbi
->visual
, &mode
);
431 _mesa_initialize_window_framebuffer(&stfb
->Base
, &mode
);
433 /* modify the draw/read buffers of the fb */
434 st_visual_to_default_buffer(stfbi
->visual
, &stfb
->Base
.ColorDrawBuffer
[0],
435 &stfb
->Base
._ColorDrawBufferIndexes
[0]);
436 st_visual_to_default_buffer(stfbi
->visual
, &stfb
->Base
.ColorReadBuffer
,
437 &stfb
->Base
._ColorReadBufferIndex
);
441 /* add the color buffer */
442 idx
= stfb
->Base
._ColorDrawBufferIndexes
[0];
443 if (!st_framebuffer_add_renderbuffer(stfb
, idx
)) {
448 st_framebuffer_add_renderbuffer(stfb
, BUFFER_DEPTH
);
449 st_framebuffer_add_renderbuffer(stfb
, BUFFER_ACCUM
);
451 st_framebuffer_update_attachments(stfb
);
453 stfb
->Base
.Initialized
= GL_TRUE
;
459 * Reference a framebuffer.
462 st_framebuffer_reference(struct st_framebuffer
**ptr
,
463 struct st_framebuffer
*stfb
)
465 GLframebuffer
*fb
= &stfb
->Base
;
466 _mesa_reference_framebuffer((GLframebuffer
**) ptr
, fb
);
470 st_context_notify_invalid_framebuffer(struct st_context_iface
*stctxi
,
471 struct st_framebuffer_iface
*stfbi
)
473 struct st_context
*st
= (struct st_context
*) stctxi
;
474 struct st_framebuffer
*stfb
;
476 /* either draw or read winsys fb */
477 stfb
= st_framebuffer(st
->ctx
->WinSysDrawBuffer
);
478 if (!stfb
|| stfb
->iface
!= stfbi
)
479 stfb
= st_framebuffer(st
->ctx
->WinSysReadBuffer
);
480 assert(stfb
&& stfb
->iface
== stfbi
);
482 p_atomic_set(&stfb
->revalidate
, TRUE
);
486 st_context_flush(struct st_context_iface
*stctxi
, unsigned flags
,
487 struct pipe_fence_handle
**fence
)
489 struct st_context
*st
= (struct st_context
*) stctxi
;
490 st_flush(st
, flags
, fence
);
491 if (flags
& PIPE_FLUSH_RENDER_CACHE
)
492 st_manager_flush_frontbuffer(st
);
496 st_context_teximage(struct st_context_iface
*stctxi
, enum st_texture_type target
,
497 int level
, enum pipe_format internal_format
,
498 struct pipe_texture
*tex
, boolean mipmap
)
500 struct st_context
*st
= (struct st_context
*) stctxi
;
501 GLcontext
*ctx
= st
->ctx
;
502 struct gl_texture_unit
*texUnit
= _mesa_get_current_tex_unit(ctx
);
503 struct gl_texture_object
*texObj
;
504 struct gl_texture_image
*texImage
;
505 struct st_texture_object
*stObj
;
506 struct st_texture_image
*stImage
;
507 GLenum internalFormat
;
511 target
= GL_TEXTURE_1D
;
514 target
= GL_TEXTURE_2D
;
517 target
= GL_TEXTURE_3D
;
519 case ST_TEXTURE_RECT
:
520 target
= GL_TEXTURE_RECTANGLE_ARB
;
527 if (util_format_get_component_bits(internal_format
,
528 UTIL_FORMAT_COLORSPACE_RGB
, 3) > 0)
529 internalFormat
= GL_RGBA
;
531 internalFormat
= GL_RGB
;
533 texObj
= _mesa_select_tex_object(ctx
, texUnit
, target
);
534 _mesa_lock_texture(ctx
, texObj
);
536 stObj
= st_texture_object(texObj
);
537 /* switch to surface based */
538 if (!stObj
->surface_based
) {
539 _mesa_clear_texture_object(ctx
, texObj
);
540 stObj
->surface_based
= GL_TRUE
;
543 texImage
= _mesa_get_tex_image(ctx
, texObj
, target
, level
);
544 stImage
= st_texture_image(texImage
);
546 _mesa_init_teximage_fields(ctx
, target
, texImage
,
547 tex
->width0
, tex
->height0
, 1, 0, internalFormat
);
548 texImage
->TexFormat
= st_ChooseTextureFormat(ctx
, internalFormat
,
549 GL_RGBA
, GL_UNSIGNED_BYTE
);
550 _mesa_set_fetch_functions(texImage
, 2);
553 _mesa_clear_texture_image(ctx
, texImage
);
556 pipe_texture_reference(&stImage
->pt
, tex
);
558 _mesa_dirty_texobj(ctx
, texObj
, GL_TRUE
);
559 _mesa_unlock_texture(ctx
, texObj
);
565 st_context_destroy(struct st_context_iface
*stctxi
)
567 struct st_context
*st
= (struct st_context
*) stctxi
;
568 st_destroy_context(st
);
571 static struct st_context_iface
*
572 st_api_create_context(struct st_api
*stapi
, struct st_manager
*smapi
,
573 const struct st_visual
*visual
,
574 struct st_context_iface
*shared_stctxi
)
576 struct st_context
*shared_ctx
= (struct st_context
*) shared_stctxi
;
577 struct st_context
*st
;
578 struct pipe_context
*pipe
;
579 __GLcontextModes mode
;
581 pipe
= smapi
->screen
->context_create(smapi
->screen
, NULL
);
585 st_visual_to_context_mode(visual
, &mode
);
586 st
= st_create_context(pipe
, &mode
, shared_ctx
);
592 st
->iface
.destroy
= st_context_destroy
;
594 st
->iface
.notify_invalid_framebuffer
=
595 st_context_notify_invalid_framebuffer
;
596 st
->iface
.flush
= st_context_flush
;
598 st
->iface
.teximage
= st_context_teximage
;
599 st
->iface
.copy
= NULL
;
601 st
->iface
.st_context_private
= (void *) smapi
;
607 st_api_make_current(struct st_api
*stapi
, struct st_context_iface
*stctxi
,
608 struct st_framebuffer_iface
*stdrawi
,
609 struct st_framebuffer_iface
*streadi
)
611 struct st_context
*st
= (struct st_context
*) stctxi
;
612 struct st_framebuffer
*stdraw
, *stread
, *stfb
;
615 _glapi_check_multithread();
618 /* reuse/create the draw fb */
619 stfb
= st_framebuffer(st
->ctx
->WinSysDrawBuffer
);
620 if (stfb
&& stfb
->iface
== stdrawi
) {
622 st_framebuffer_reference(&stdraw
, stfb
);
625 stdraw
= st_framebuffer_create(stdrawi
);
628 /* reuse/create the read fb */
629 stfb
= st_framebuffer(st
->ctx
->WinSysReadBuffer
);
630 if (!stfb
|| stfb
->iface
!= streadi
)
632 if (stfb
&& stfb
->iface
== streadi
) {
634 st_framebuffer_reference(&stread
, stfb
);
637 stread
= st_framebuffer_create(streadi
);
640 if (stdraw
&& stread
) {
641 st_framebuffer_validate(stdraw
, st
);
642 if (stread
!= stdraw
)
643 st_framebuffer_validate(stread
, st
);
645 /* modify the draw/read buffers of the context */
646 st_visual_to_default_buffer(stdraw
->iface
->visual
,
647 &st
->ctx
->Color
.DrawBuffer
[0], NULL
);
648 st_visual_to_default_buffer(stread
->iface
->visual
,
649 &st
->ctx
->Pixel
.ReadBuffer
, NULL
);
651 ret
= _mesa_make_current(st
->ctx
, &stdraw
->Base
, &stread
->Base
);
657 st_framebuffer_reference(&stdraw
, NULL
);
658 st_framebuffer_reference(&stread
, NULL
);
661 ret
= _mesa_make_current(NULL
, NULL
, NULL
);
667 static struct st_context_iface
*
668 st_api_get_current(struct st_api
*stapi
)
670 GET_CURRENT_CONTEXT(ctx
);
671 struct st_context
*st
= (ctx
) ? ctx
->st
: NULL
;
673 return (st
) ? &st
->iface
: NULL
;
677 st_api_is_visual_supported(struct st_api
*stapi
,
678 const struct st_visual
*visual
)
684 st_api_get_proc_address(struct st_api
*stapi
, const char *procname
)
686 return (st_proc_t
) _glapi_get_proc_address(procname
);
690 st_api_destroy(struct st_api
*stapi
)
696 * Flush the front buffer if the current context renders to the front buffer.
699 st_manager_flush_frontbuffer(struct st_context
*st
)
701 struct st_framebuffer
*stfb
= st_framebuffer(st
->ctx
->DrawBuffer
);
702 struct st_renderbuffer
*strb
= NULL
;
705 strb
= st_renderbuffer(stfb
->Base
.Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
);
711 struct pipe_surface
*front_surf
= strb
->surface
;
712 st
->pipe
->screen
->flush_frontbuffer(st
->pipe
->screen
,
713 front_surf
, st
->winsys_drawable_handle
);
717 stfb
->iface
->flush_front(stfb
->iface
, ST_ATTACHMENT_FRONT_LEFT
);
721 * Re-validate the framebuffer.
724 st_manager_validate_framebuffers(struct st_context
*st
)
726 struct st_framebuffer
*stdraw
= st_framebuffer(st
->ctx
->DrawBuffer
);
727 struct st_framebuffer
*stread
= st_framebuffer(st
->ctx
->ReadBuffer
);
730 if ((stdraw
&& !stdraw
->iface
) || (stread
&& !stread
->iface
)) {
731 struct pipe_screen
*screen
= st
->pipe
->screen
;
732 if (screen
->update_buffer
)
733 screen
->update_buffer(screen
, st
->pipe
->priv
);
738 st_framebuffer_validate(stdraw
, st
);
739 if (stread
&& stread
!= stdraw
)
740 st_framebuffer_validate(stread
, st
);
744 * Add a color buffer on demand.
747 st_manager_add_color_renderbuffer(struct st_context
*st
, GLframebuffer
*fb
,
750 struct st_framebuffer
*stfb
= st_framebuffer(fb
);
752 /* FBO or st_public.h */
753 if (!stfb
|| !stfb
->iface
)
756 if (stfb
->Base
.Attachment
[idx
].Renderbuffer
)
760 case BUFFER_FRONT_LEFT
:
761 case BUFFER_BACK_LEFT
:
762 case BUFFER_FRONT_RIGHT
:
763 case BUFFER_BACK_RIGHT
:
770 if (!st_framebuffer_add_renderbuffer(stfb
, idx
))
773 st_framebuffer_update_attachments(stfb
);
774 st_invalidate_state(st
->ctx
, _NEW_BUFFERS
);
780 st_manager_create_api(void)
782 struct st_api
*stapi
;
784 stapi
= CALLOC_STRUCT(st_api
);
786 stapi
->destroy
= st_api_destroy
;
787 stapi
->get_proc_address
= st_api_get_proc_address
;
788 stapi
->is_visual_supported
= st_api_is_visual_supported
;
790 stapi
->create_context
= st_api_create_context
;
791 stapi
->make_current
= st_api_make_current
;
792 stapi
->get_current
= st_api_get_current
;