1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
29 #include "main/imports.h"
30 #include "main/context.h"
31 #include "main/framebuffer.h"
32 #include "main/renderbuffer.h"
33 #include "st_public.h"
34 #include "st_context.h"
35 #include "st_cb_fbo.h"
38 struct st_framebuffer
*st_create_framebuffer( const __GLcontextModes
*visual
,
39 boolean createRenderbuffers
,
42 struct st_framebuffer
*stfb
43 = CALLOC_STRUCT(st_framebuffer
);
45 GLenum rgbFormat
= (visual
->redBits
== 5 ? GL_RGB5
: GL_RGBA8
);
47 _mesa_initialize_framebuffer(&stfb
->Base
, visual
);
49 if (createRenderbuffers
) {
51 /* fake frontbuffer */
52 /* XXX allocation should only happen in the unusual case
53 it's actually needed */
54 struct gl_renderbuffer
*rb
= st_new_renderbuffer_fb(rgbFormat
);
55 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_FRONT_LEFT
, rb
);
58 if (visual
->doubleBufferMode
) {
59 struct gl_renderbuffer
*rb
= st_new_renderbuffer_fb(rgbFormat
);
60 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_BACK_LEFT
, rb
);
63 if (visual
->depthBits
== 24 && visual
->stencilBits
== 8) {
64 /* combined depth/stencil buffer */
65 struct gl_renderbuffer
*depthStencilRb
66 = st_new_renderbuffer_fb(GL_DEPTH24_STENCIL8_EXT
);
67 /* note: bind RB to two attachment points */
68 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthStencilRb
);
69 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_STENCIL
, depthStencilRb
);
72 /* separate depth and/or stencil */
74 if (visual
->depthBits
== 32) {
75 /* 32-bit depth buffer */
76 struct gl_renderbuffer
*depthRb
77 = st_new_renderbuffer_fb(GL_DEPTH_COMPONENT32
);
78 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
80 else if (visual
->depthBits
== 24) {
81 /* 24-bit depth buffer, ignore stencil bits */
82 struct gl_renderbuffer
*depthRb
83 = st_new_renderbuffer_fb(GL_DEPTH24_STENCIL8_EXT
);
84 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
86 else if (visual
->depthBits
> 0) {
87 /* 16-bit depth buffer */
88 struct gl_renderbuffer
*depthRb
89 = st_new_renderbuffer_fb(GL_DEPTH_COMPONENT16
);
90 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_DEPTH
, depthRb
);
93 if (visual
->stencilBits
> 0) {
95 struct gl_renderbuffer
*stencilRb
96 = st_new_renderbuffer_fb(GL_STENCIL_INDEX8_EXT
);
97 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_STENCIL
, stencilRb
);
102 if (visual
->accumRedBits
> 0) {
103 /* 16-bit/channel accum */
104 struct gl_renderbuffer
*accumRb
105 = st_new_renderbuffer_fb(GL_RGBA16
);
106 _mesa_add_renderbuffer(&stfb
->Base
, BUFFER_ACCUM
, accumRb
);
110 /* now add any/all software-based renderbuffers we may need */
111 _mesa_add_soft_renderbuffers(&stfb
->Base
,
112 GL_FALSE
, /* never sw color */
113 GL_FALSE
, /* never sw depth */
114 GL_FALSE
, /* stencil */
115 visual
->accumRedBits
> 0,
116 GL_FALSE
, /* never sw alpha */
117 GL_FALSE
/* never sw aux */ );
120 stfb
->Base
.Initialized
= GL_TRUE
;
122 stfb
->Private
= private;
128 void st_resize_framebuffer( struct st_framebuffer
*stfb
,
129 uint width
, uint height
)
131 if (stfb
->Base
.Width
!= width
|| stfb
->Base
.Height
!= height
) {
132 GET_CURRENT_CONTEXT(ctx
);
134 _mesa_resize_framebuffer(ctx
, &stfb
->Base
, width
, height
);
136 assert(stfb
->Base
.Width
== width
);
137 assert(stfb
->Base
.Height
== height
);
143 void st_unreference_framebuffer( struct st_framebuffer
**stfb
)
145 _mesa_unreference_framebuffer((struct gl_framebuffer
**) stfb
);
151 * Return the pipe_surface for the given renderbuffer.
153 struct pipe_surface
*
154 st_get_framebuffer_surface(struct st_framebuffer
*stfb
, uint surfIndex
)
156 struct st_renderbuffer
*strb
;
158 assert(surfIndex
<= ST_SURFACE_BACK_RIGHT
);
160 /* sanity checks, ST tokens should match Mesa tokens */
161 assert(ST_SURFACE_FRONT_LEFT
== BUFFER_FRONT_LEFT
);
162 assert(ST_SURFACE_BACK_RIGHT
== BUFFER_BACK_RIGHT
);
164 strb
= st_renderbuffer(stfb
->Base
.Attachment
[surfIndex
].Renderbuffer
);
166 return strb
->surface
;
172 * This function is to be called prior to SwapBuffers on the given
173 * framebuffer. It checks if the current context is bound to the framebuffer
174 * and flushes rendering if needed.
177 st_notify_swapbuffers(struct st_framebuffer
*stfb
)
179 GET_CURRENT_CONTEXT(ctx
);
181 if (ctx
&& ctx
->DrawBuffer
== &stfb
->Base
) {
182 st_flush(ctx
->st
, 0x0);
187 void *st_framebuffer_private( struct st_framebuffer
*stfb
)
189 return stfb
->Private
;