gallium: Add accessor functions to get textures from a st_framebuffer
[mesa.git] / src / mesa / state_tracker / st_framebuffer.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 #include "main/imports.h"
30 #include "main/buffers.h"
31 #include "main/context.h"
32 #include "main/framebuffer.h"
33 #include "main/matrix.h"
34 #include "main/renderbuffer.h"
35 #include "main/scissor.h"
36 #include "st_public.h"
37 #include "st_context.h"
38 #include "st_cb_fbo.h"
39 #include "pipe/p_defines.h"
40 #include "pipe/p_context.h"
41 #include "pipe/p_inlines.h"
42
43
44 struct st_framebuffer *
45 st_create_framebuffer( const __GLcontextModes *visual,
46 enum pipe_format colorFormat,
47 enum pipe_format depthFormat,
48 enum pipe_format stencilFormat,
49 uint width, uint height,
50 void *private)
51 {
52 struct st_framebuffer *stfb = CALLOC_STRUCT(st_framebuffer);
53 if (stfb) {
54 _mesa_initialize_framebuffer(&stfb->Base, visual);
55
56 {
57 /* fake frontbuffer */
58 /* XXX allocation should only happen in the unusual case
59 it's actually needed */
60 struct gl_renderbuffer *rb
61 = st_new_renderbuffer_fb(colorFormat);
62 _mesa_add_renderbuffer(&stfb->Base, BUFFER_FRONT_LEFT, rb);
63 }
64
65 if (visual->doubleBufferMode) {
66 struct gl_renderbuffer *rb
67 = st_new_renderbuffer_fb(colorFormat);
68 _mesa_add_renderbuffer(&stfb->Base, BUFFER_BACK_LEFT, rb);
69 }
70
71 if (visual->depthBits == 24 && visual->stencilBits == 8) {
72 /* combined depth/stencil buffer */
73 struct gl_renderbuffer *depthStencilRb
74 = st_new_renderbuffer_fb(depthFormat);
75 /* note: bind RB to two attachment points */
76 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthStencilRb);
77 _mesa_add_renderbuffer(&stfb->Base, BUFFER_STENCIL, depthStencilRb);
78 }
79 else {
80 /* separate depth and/or stencil */
81
82 if (visual->depthBits == 32) {
83 /* 32-bit depth buffer */
84 struct gl_renderbuffer *depthRb
85 = st_new_renderbuffer_fb(depthFormat);
86 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthRb);
87 }
88 else if (visual->depthBits == 24) {
89 /* 24-bit depth buffer, ignore stencil bits */
90 struct gl_renderbuffer *depthRb
91 = st_new_renderbuffer_fb(depthFormat);
92 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthRb);
93 }
94 else if (visual->depthBits > 0) {
95 /* 16-bit depth buffer */
96 struct gl_renderbuffer *depthRb
97 = st_new_renderbuffer_fb(depthFormat);
98 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthRb);
99 }
100
101 if (visual->stencilBits > 0) {
102 /* 8-bit stencil */
103 struct gl_renderbuffer *stencilRb
104 = st_new_renderbuffer_fb(stencilFormat);
105 _mesa_add_renderbuffer(&stfb->Base, BUFFER_STENCIL, stencilRb);
106 }
107 }
108
109 if (visual->accumRedBits > 0) {
110 /* 16-bit/channel accum */
111 struct gl_renderbuffer *accumRb
112 = st_new_renderbuffer_fb(DEFAULT_ACCUM_PIPE_FORMAT);
113 _mesa_add_renderbuffer(&stfb->Base, BUFFER_ACCUM, accumRb);
114 }
115
116 stfb->Base.Initialized = GL_TRUE;
117 stfb->InitWidth = width;
118 stfb->InitHeight = height;
119 stfb->Private = private;
120 }
121 return stfb;
122 }
123
124
125 void st_resize_framebuffer( struct st_framebuffer *stfb,
126 uint width, uint height )
127 {
128 if (stfb->Base.Width != width || stfb->Base.Height != height) {
129 GET_CURRENT_CONTEXT(ctx);
130 if (ctx) {
131 if (stfb->InitWidth == 0 && stfb->InitHeight == 0) {
132 /* didn't have a valid size until now */
133 stfb->InitWidth = width;
134 stfb->InitHeight = height;
135 if (ctx->Viewport.Width <= 1) {
136 /* set context's initial viewport/scissor size */
137 _mesa_set_viewport(ctx, 0, 0, width, height);
138 _mesa_set_scissor(ctx, 0, 0, width, height);
139 }
140 }
141
142 _mesa_resize_framebuffer(ctx, &stfb->Base, width, height);
143
144 assert(stfb->Base.Width == width);
145 assert(stfb->Base.Height == height);
146 }
147 }
148 }
149
150
151 void st_unreference_framebuffer( struct st_framebuffer **stfb )
152 {
153 _mesa_unreference_framebuffer((struct gl_framebuffer **) stfb);
154 }
155
156
157
158 /**
159 * Set/replace a framebuffer surface.
160 * The user of the state tracker can use this instead of
161 * st_resize_framebuffer() to provide new surfaces when a window is resized.
162 */
163 void
164 st_set_framebuffer_surface(struct st_framebuffer *stfb,
165 uint surfIndex, struct pipe_surface *surf)
166 {
167 static const GLuint invalid_size = 9999999;
168 struct st_renderbuffer *strb;
169 GLuint width, height, i;
170
171 assert(surfIndex < BUFFER_COUNT);
172
173 strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
174 assert(strb);
175
176 /* replace the renderbuffer's surface/texture pointers */
177 pipe_surface_reference( &strb->surface, surf );
178 pipe_texture_reference( &strb->texture, surf->texture );
179
180 /* update renderbuffer's width/height */
181 strb->Base.Width = surf->width;
182 strb->Base.Height = surf->height;
183
184 /* Try to update the framebuffer's width/height from the renderbuffer
185 * sizes. Before we start drawing, all the rbs _should_ be the same size.
186 */
187 width = height = invalid_size;
188 for (i = 0; i < BUFFER_COUNT; i++) {
189 if (stfb->Base.Attachment[i].Renderbuffer) {
190 if (width == invalid_size) {
191 width = stfb->Base.Attachment[i].Renderbuffer->Width;
192 height = stfb->Base.Attachment[i].Renderbuffer->Height;
193 }
194 else if (width != stfb->Base.Attachment[i].Renderbuffer->Width ||
195 height != stfb->Base.Attachment[i].Renderbuffer->Height) {
196 /* inconsistant renderbuffer sizes, bail out */
197 return;
198 }
199 }
200 }
201
202 if (width != invalid_size) {
203 /* OK, the renderbuffers are of a consistant size, so update the
204 * parent framebuffer's size.
205 */
206 stfb->Base.Width = width;
207 stfb->Base.Height = height;
208 }
209 }
210
211
212
213 /**
214 * Return the pipe_surface for the given renderbuffer.
215 */
216 struct pipe_surface *
217 st_get_framebuffer_surface(struct st_framebuffer *stfb, uint surfIndex)
218 {
219 struct st_renderbuffer *strb;
220
221 assert(surfIndex <= ST_SURFACE_DEPTH);
222
223 /* sanity checks, ST tokens should match Mesa tokens */
224 assert(ST_SURFACE_FRONT_LEFT == BUFFER_FRONT_LEFT);
225 assert(ST_SURFACE_BACK_RIGHT == BUFFER_BACK_RIGHT);
226
227 strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
228 if (strb)
229 return strb->surface;
230 return NULL;
231 }
232
233 struct pipe_texture *
234 st_get_framebuffer_texture(struct st_framebuffer *stfb, uint surfIndex)
235 {
236 struct st_renderbuffer *strb;
237
238 assert(surfIndex <= ST_SURFACE_DEPTH);
239
240 /* sanity checks, ST tokens should match Mesa tokens */
241 assert(ST_SURFACE_FRONT_LEFT == BUFFER_FRONT_LEFT);
242 assert(ST_SURFACE_BACK_RIGHT == BUFFER_BACK_RIGHT);
243
244 strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
245 if (strb)
246 return strb->texture;
247 return NULL;
248 }
249
250 /**
251 * This function is to be called prior to SwapBuffers on the given
252 * framebuffer. It checks if the current context is bound to the framebuffer
253 * and flushes rendering if needed.
254 */
255 void
256 st_notify_swapbuffers(struct st_framebuffer *stfb)
257 {
258 GET_CURRENT_CONTEXT(ctx);
259
260 if (ctx && ctx->DrawBuffer == &stfb->Base) {
261 st_flush( ctx->st,
262 PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_SWAPBUFFERS,
263 NULL );
264 ctx->st->frontbuffer_status = FRONT_STATUS_COPY_OF_BACK;
265 }
266 }
267
268
269 /**
270 * Quick hack - allows the winsys to inform the driver that surface
271 * states are now undefined after a glXSwapBuffers or similar.
272 */
273 void
274 st_notify_swapbuffers_complete(struct st_framebuffer *stfb)
275 {
276 GET_CURRENT_CONTEXT(ctx);
277
278 if (ctx && ctx->DrawBuffer == &stfb->Base) {
279 struct st_renderbuffer *strb;
280 int i;
281
282 for (i = 0; i < BUFFER_COUNT; i++) {
283 if (stfb->Base.Attachment[i].Renderbuffer) {
284 strb = st_renderbuffer(stfb->Base.Attachment[i].Renderbuffer);
285 strb->surface->status = PIPE_SURFACE_STATUS_UNDEFINED;
286 }
287 }
288 }
289 }
290
291
292 void *st_framebuffer_private( struct st_framebuffer *stfb )
293 {
294 return stfb->Private;
295 }
296