Merge branch 'mesa_7_6_branch'
[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 "main/viewport.h"
37 #include "st_context.h"
38 #include "st_cb_fbo.h"
39 #include "st_public.h"
40 #include "pipe/p_defines.h"
41 #include "pipe/p_context.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 = ST_CALLOC_STRUCT(st_framebuffer);
53 if (stfb) {
54 int samples = st_get_msaa();
55 int i;
56
57 if (visual->sampleBuffers)
58 samples = visual->samples;
59
60 _mesa_initialize_framebuffer(&stfb->Base, visual);
61
62 if (visual->doubleBufferMode) {
63 struct gl_renderbuffer *rb
64 = st_new_renderbuffer_fb(colorFormat, samples, FALSE);
65 _mesa_add_renderbuffer(&stfb->Base, BUFFER_BACK_LEFT, rb);
66 }
67 else {
68 /* Only allocate front buffer right now if we're single buffered.
69 * If double-buffered, allocate front buffer on demand later.
70 * See check_create_front_buffers() and st_set_framebuffer_surface().
71 */
72 struct gl_renderbuffer *rb
73 = st_new_renderbuffer_fb(colorFormat, samples, FALSE);
74 _mesa_add_renderbuffer(&stfb->Base, BUFFER_FRONT_LEFT, rb);
75 }
76
77 if (depthFormat == stencilFormat && depthFormat != PIPE_FORMAT_NONE) {
78 /* combined depth/stencil buffer */
79 struct gl_renderbuffer *depthStencilRb
80 = st_new_renderbuffer_fb(depthFormat, samples, FALSE);
81 /* note: bind RB to two attachment points */
82 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthStencilRb);
83 _mesa_add_renderbuffer(&stfb->Base, BUFFER_STENCIL, depthStencilRb);
84 }
85 else {
86 /* separate depth and/or stencil */
87
88 if (visual->depthBits == 32) {
89 /* 32-bit depth buffer */
90 struct gl_renderbuffer *depthRb
91 = st_new_renderbuffer_fb(depthFormat, samples, FALSE);
92 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthRb);
93 }
94 else if (visual->depthBits == 24) {
95 /* 24-bit depth buffer, ignore stencil bits */
96 struct gl_renderbuffer *depthRb
97 = st_new_renderbuffer_fb(depthFormat, samples, FALSE);
98 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthRb);
99 }
100 else if (visual->depthBits > 0) {
101 /* 16-bit depth buffer */
102 struct gl_renderbuffer *depthRb
103 = st_new_renderbuffer_fb(depthFormat, samples, FALSE);
104 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, depthRb);
105 }
106
107 if (visual->stencilBits > 0) {
108 /* 8-bit stencil */
109 struct gl_renderbuffer *stencilRb
110 = st_new_renderbuffer_fb(stencilFormat, samples, FALSE);
111 _mesa_add_renderbuffer(&stfb->Base, BUFFER_STENCIL, stencilRb);
112 }
113 }
114
115 if (visual->accumRedBits > 0) {
116 /* 16-bit/channel accum */
117 /* TODO: query the pipe screen for accumulation buffer format support */
118 struct gl_renderbuffer *accumRb
119 = st_new_renderbuffer_fb(PIPE_FORMAT_R16G16B16A16_SNORM, 0, TRUE);
120 _mesa_add_renderbuffer(&stfb->Base, BUFFER_ACCUM, accumRb);
121 }
122
123 for (i = 0; i < visual->numAuxBuffers; i++) {
124 struct gl_renderbuffer *aux
125 = st_new_renderbuffer_fb(colorFormat, 0, FALSE);
126 _mesa_add_renderbuffer(&stfb->Base, BUFFER_AUX0 + i, aux);
127 }
128
129 stfb->Base.Initialized = GL_TRUE;
130 stfb->InitWidth = width;
131 stfb->InitHeight = height;
132 stfb->Private = private;
133 }
134 return stfb;
135 }
136
137
138 void st_resize_framebuffer( struct st_framebuffer *stfb,
139 uint width, uint height )
140 {
141 if (stfb->Base.Width != width || stfb->Base.Height != height) {
142 GET_CURRENT_CONTEXT(ctx);
143 if (ctx) {
144 _mesa_check_init_viewport(ctx, width, height);
145
146 _mesa_resize_framebuffer(ctx, &stfb->Base, width, height);
147
148 assert(stfb->Base.Width == width);
149 assert(stfb->Base.Height == height);
150 }
151 }
152 }
153
154
155 void st_unreference_framebuffer( struct st_framebuffer *stfb )
156 {
157 _mesa_reference_framebuffer((struct gl_framebuffer **) &stfb, NULL);
158 }
159
160
161
162 /**
163 * Set/replace a framebuffer surface.
164 * The user of the state tracker can use this instead of
165 * st_resize_framebuffer() to provide new surfaces when a window is resized.
166 */
167 void
168 st_set_framebuffer_surface(struct st_framebuffer *stfb,
169 uint surfIndex, struct pipe_surface *surf)
170 {
171 GET_CURRENT_CONTEXT(ctx);
172 static const GLuint invalid_size = 9999999;
173 struct st_renderbuffer *strb;
174 GLuint width, height, i;
175
176 assert(surfIndex < BUFFER_COUNT);
177
178 strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
179
180 if (!strb) {
181 if (surfIndex == ST_SURFACE_FRONT_LEFT) {
182 /* Delayed creation when the window system supplies a fake front buffer */
183 struct st_renderbuffer *strb_back
184 = st_renderbuffer(stfb->Base.Attachment[ST_SURFACE_BACK_LEFT].Renderbuffer);
185 struct gl_renderbuffer *rb
186 = st_new_renderbuffer_fb(surf->format, strb_back->Base.NumSamples, FALSE);
187 _mesa_add_renderbuffer(&stfb->Base, BUFFER_FRONT_LEFT, rb);
188 strb = st_renderbuffer(rb);
189 } else {
190 /* fail */
191 return;
192 }
193 }
194
195 /* replace the renderbuffer's surface/texture pointers */
196 pipe_surface_reference( &strb->surface, surf );
197 pipe_texture_reference( &strb->texture, surf->texture );
198
199 if (ctx) {
200 /* If ctx isn't set, we've likely not made current yet.
201 * But when we do, we need to start setting this dirty bit
202 * to ensure the renderbuffer attachements are up-to-date
203 * via update_framebuffer.
204 */
205 ctx->st->dirty.st |= ST_NEW_FRAMEBUFFER;
206 }
207
208 /* update renderbuffer's width/height */
209 strb->Base.Width = surf->width;
210 strb->Base.Height = surf->height;
211
212 /* Try to update the framebuffer's width/height from the renderbuffer
213 * sizes. Before we start drawing, all the rbs _should_ be the same size.
214 */
215 width = height = invalid_size;
216 for (i = 0; i < BUFFER_COUNT; i++) {
217 if (stfb->Base.Attachment[i].Renderbuffer) {
218 if (width == invalid_size) {
219 width = stfb->Base.Attachment[i].Renderbuffer->Width;
220 height = stfb->Base.Attachment[i].Renderbuffer->Height;
221 }
222 else if (width != stfb->Base.Attachment[i].Renderbuffer->Width ||
223 height != stfb->Base.Attachment[i].Renderbuffer->Height) {
224 /* inconsistant renderbuffer sizes, bail out */
225 return;
226 }
227 }
228 }
229
230 if (width != invalid_size) {
231 /* OK, the renderbuffers are of a consistant size, so update the
232 * parent framebuffer's size.
233 */
234 stfb->Base.Width = width;
235 stfb->Base.Height = height;
236 }
237 }
238
239
240
241 /**
242 * Return the pipe_surface for the given renderbuffer.
243 */
244 int
245 st_get_framebuffer_surface(struct st_framebuffer *stfb, uint surfIndex, struct pipe_surface **surface)
246 {
247 struct st_renderbuffer *strb;
248
249 assert(surfIndex <= ST_SURFACE_DEPTH);
250
251 /* sanity checks, ST tokens should match Mesa tokens */
252 assert(ST_SURFACE_FRONT_LEFT == BUFFER_FRONT_LEFT);
253 assert(ST_SURFACE_BACK_RIGHT == BUFFER_BACK_RIGHT);
254
255 strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
256 if (strb) {
257 *surface = strb->surface;
258 return GL_TRUE;
259 }
260
261 *surface = NULL;
262 return GL_FALSE;
263 }
264
265 int
266 st_get_framebuffer_texture(struct st_framebuffer *stfb, uint surfIndex, struct pipe_texture **texture)
267 {
268 struct st_renderbuffer *strb;
269
270 assert(surfIndex <= ST_SURFACE_DEPTH);
271
272 /* sanity checks, ST tokens should match Mesa tokens */
273 assert(ST_SURFACE_FRONT_LEFT == BUFFER_FRONT_LEFT);
274 assert(ST_SURFACE_BACK_RIGHT == BUFFER_BACK_RIGHT);
275
276 strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
277 if (strb) {
278 *texture = strb->texture;
279 return GL_TRUE;
280 }
281
282 *texture = NULL;
283 return GL_FALSE;
284 }
285
286 /**
287 * This function is to be called prior to SwapBuffers on the given
288 * framebuffer. It checks if the current context is bound to the framebuffer
289 * and flushes rendering if needed.
290 */
291 void
292 st_notify_swapbuffers(struct st_framebuffer *stfb)
293 {
294 GET_CURRENT_CONTEXT(ctx);
295
296 if (ctx && ctx->DrawBuffer == &stfb->Base) {
297 st_flush( ctx->st,
298 PIPE_FLUSH_RENDER_CACHE |
299 PIPE_FLUSH_SWAPBUFFERS |
300 PIPE_FLUSH_FRAME,
301 NULL );
302 if (st_renderbuffer(stfb->Base.Attachment[BUFFER_BACK_LEFT].Renderbuffer))
303 ctx->st->frontbuffer_status = FRONT_STATUS_COPY_OF_BACK;
304 }
305 }
306
307
308 /**
309 * Swap the front/back color buffers. Exchange the front/back pointers
310 * and update some derived state.
311 * No need to call st_notify_swapbuffers() first.
312 *
313 * For a single-buffered framebuffer, no swap occurs, but we still return
314 * the pointer(s) to the front color buffer(s).
315 *
316 * \param front_left returns pointer to front-left renderbuffer after swap
317 * \param front_right returns pointer to front-right renderbuffer after swap
318 */
319 void
320 st_swapbuffers(struct st_framebuffer *stfb,
321 struct pipe_surface **front_left,
322 struct pipe_surface **front_right)
323 {
324 struct gl_framebuffer *fb = &stfb->Base;
325
326 GET_CURRENT_CONTEXT(ctx);
327
328 if (ctx && ctx->DrawBuffer == &stfb->Base) {
329 st_flush( ctx->st,
330 PIPE_FLUSH_RENDER_CACHE |
331 PIPE_FLUSH_SWAPBUFFERS |
332 PIPE_FLUSH_FRAME,
333 NULL );
334 }
335
336 if (!fb->Visual.doubleBufferMode) {
337 /* single buffer mode - return pointers to front surfaces */
338 if (front_left) {
339 struct st_renderbuffer *strb =
340 st_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
341 *front_left = strb->surface;
342 }
343 if (front_right) {
344 struct st_renderbuffer *strb =
345 st_renderbuffer(fb->Attachment[BUFFER_FRONT_RIGHT].Renderbuffer);
346 *front_right = strb ? strb->surface : NULL;
347 }
348 return;
349 }
350
351 /* swap left buffers */
352 if (fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer &&
353 fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer) {
354 struct gl_renderbuffer *rbTemp;
355 rbTemp = fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer;
356 fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer =
357 fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer;
358 fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer = rbTemp;
359 if (front_left) {
360 struct st_renderbuffer *strb =
361 st_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
362 *front_left = strb->surface;
363 }
364 /* mark back buffer contents as undefined */
365 {
366 struct st_renderbuffer *back =
367 st_renderbuffer(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
368 back->defined = GL_FALSE;
369 }
370 }
371 else {
372 /* no front buffer, display the back buffer */
373 if (front_left) {
374 struct st_renderbuffer *strb =
375 st_renderbuffer(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
376 *front_left = strb->surface;
377 }
378 }
379
380 /* swap right buffers (for stereo) */
381 if (fb->Attachment[BUFFER_FRONT_RIGHT].Renderbuffer &&
382 fb->Attachment[BUFFER_BACK_RIGHT].Renderbuffer) {
383 struct gl_renderbuffer *rbTemp;
384 rbTemp = fb->Attachment[BUFFER_FRONT_RIGHT].Renderbuffer;
385 fb->Attachment[BUFFER_FRONT_RIGHT].Renderbuffer =
386 fb->Attachment[BUFFER_BACK_RIGHT].Renderbuffer;
387 fb->Attachment[BUFFER_BACK_RIGHT].Renderbuffer = rbTemp;
388 if (front_right) {
389 struct st_renderbuffer *strb =
390 st_renderbuffer(fb->Attachment[BUFFER_FRONT_RIGHT].Renderbuffer);
391 *front_right = strb->surface;
392 }
393 /* mark back buffer contents as undefined */
394 {
395 struct st_renderbuffer *back =
396 st_renderbuffer(fb->Attachment[BUFFER_BACK_RIGHT].Renderbuffer);
397 back->defined = GL_FALSE;
398 }
399 }
400 else {
401 /* no front right buffer, display back right buffer (if exists) */
402 if (front_right) {
403 struct st_renderbuffer *strb =
404 st_renderbuffer(fb->Attachment[BUFFER_BACK_RIGHT].Renderbuffer);
405 *front_right = strb ? strb->surface : NULL;
406 }
407 }
408
409 /* Update the _ColorDrawBuffers[] array and _ColorReadBuffer pointer */
410 _mesa_update_framebuffer(ctx);
411
412 /* Make sure we draw into the new back surface */
413 st_invalidate_state(ctx, _NEW_BUFFERS);
414 }
415
416
417 void *st_framebuffer_private( struct st_framebuffer *stfb )
418 {
419 return stfb->Private;
420 }
421
422 void st_get_framebuffer_dimensions( struct st_framebuffer *stfb,
423 uint *width,
424 uint *height)
425 {
426 *width = stfb->Base.Width;
427 *height = stfb->Base.Height;
428 }