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