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 _mesa_check_init_viewport(ctx, width, height);
138
139 _mesa_resize_framebuffer(ctx, &stfb->Base, width, height);
140
141 assert(stfb->Base.Width == width);
142 assert(stfb->Base.Height == height);
143 }
144 }
145 }
146
147
148 void st_unreference_framebuffer( struct st_framebuffer *stfb )
149 {
150 _mesa_reference_framebuffer((struct gl_framebuffer **) &stfb, NULL);
151 }
152
153
154
155 /**
156 * Set/replace a framebuffer surface.
157 * The user of the state tracker can use this instead of
158 * st_resize_framebuffer() to provide new surfaces when a window is resized.
159 */
160 void
161 st_set_framebuffer_surface(struct st_framebuffer *stfb,
162 uint surfIndex, struct pipe_surface *surf)
163 {
164 GET_CURRENT_CONTEXT(ctx);
165 static const GLuint invalid_size = 9999999;
166 struct st_renderbuffer *strb;
167 GLuint width, height, i;
168
169 assert(surfIndex < BUFFER_COUNT);
170
171 strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
172
173 /* fail */
174 if (!strb) return;
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 if (ctx) {
181 /* If ctx isn't set, we've likely not made current yet.
182 * But when we do, we need to start setting this dirty bit
183 * to ensure the renderbuffer attachements are up-to-date
184 * via update_framebuffer.
185 */
186 ctx->st->dirty.st |= ST_NEW_FRAMEBUFFER;
187 }
188
189 /* update renderbuffer's width/height */
190 strb->Base.Width = surf->width;
191 strb->Base.Height = surf->height;
192
193 /* Try to update the framebuffer's width/height from the renderbuffer
194 * sizes. Before we start drawing, all the rbs _should_ be the same size.
195 */
196 width = height = invalid_size;
197 for (i = 0; i < BUFFER_COUNT; i++) {
198 if (stfb->Base.Attachment[i].Renderbuffer) {
199 if (width == invalid_size) {
200 width = stfb->Base.Attachment[i].Renderbuffer->Width;
201 height = stfb->Base.Attachment[i].Renderbuffer->Height;
202 }
203 else if (width != stfb->Base.Attachment[i].Renderbuffer->Width ||
204 height != stfb->Base.Attachment[i].Renderbuffer->Height) {
205 /* inconsistant renderbuffer sizes, bail out */
206 return;
207 }
208 }
209 }
210
211 if (width != invalid_size) {
212 /* OK, the renderbuffers are of a consistant size, so update the
213 * parent framebuffer's size.
214 */
215 stfb->Base.Width = width;
216 stfb->Base.Height = height;
217 }
218 }
219
220
221
222 /**
223 * Return the pipe_surface for the given renderbuffer.
224 */
225 int
226 st_get_framebuffer_surface(struct st_framebuffer *stfb, uint surfIndex, struct pipe_surface **surface)
227 {
228 struct st_renderbuffer *strb;
229
230 assert(surfIndex <= ST_SURFACE_DEPTH);
231
232 /* sanity checks, ST tokens should match Mesa tokens */
233 assert(ST_SURFACE_FRONT_LEFT == BUFFER_FRONT_LEFT);
234 assert(ST_SURFACE_BACK_RIGHT == BUFFER_BACK_RIGHT);
235
236 strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
237 if (strb) {
238 *surface = strb->surface;
239 return GL_TRUE;
240 }
241
242 *surface = NULL;
243 return GL_FALSE;
244 }
245
246 int
247 st_get_framebuffer_texture(struct st_framebuffer *stfb, uint surfIndex, struct pipe_texture **texture)
248 {
249 struct st_renderbuffer *strb;
250
251 assert(surfIndex <= ST_SURFACE_DEPTH);
252
253 /* sanity checks, ST tokens should match Mesa tokens */
254 assert(ST_SURFACE_FRONT_LEFT == BUFFER_FRONT_LEFT);
255 assert(ST_SURFACE_BACK_RIGHT == BUFFER_BACK_RIGHT);
256
257 strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer);
258 if (strb) {
259 *texture = strb->texture;
260 return GL_TRUE;
261 }
262
263 *texture = NULL;
264 return GL_FALSE;
265 }
266
267 /**
268 * This function is to be called prior to SwapBuffers on the given
269 * framebuffer. It checks if the current context is bound to the framebuffer
270 * and flushes rendering if needed.
271 */
272 void
273 st_notify_swapbuffers(struct st_framebuffer *stfb)
274 {
275 GET_CURRENT_CONTEXT(ctx);
276
277 if (ctx && ctx->DrawBuffer == &stfb->Base) {
278 st_flush( ctx->st,
279 PIPE_FLUSH_RENDER_CACHE |
280 PIPE_FLUSH_SWAPBUFFERS |
281 PIPE_FLUSH_FRAME,
282 NULL );
283 ctx->st->frontbuffer_status = FRONT_STATUS_COPY_OF_BACK;
284 }
285 }
286
287
288 /**
289 * Swap the front/back color buffers. Exchange the front/back pointers
290 * and update some derived state.
291 * No need to call st_notify_swapbuffers() first.
292 *
293 * For a single-buffered framebuffer, no swap occurs, but we still return
294 * the pointer(s) to the front color buffer(s).
295 *
296 * \param front_left returns pointer to front-left renderbuffer after swap
297 * \param front_right returns pointer to front-right renderbuffer after swap
298 */
299 void
300 st_swapbuffers(struct st_framebuffer *stfb,
301 struct pipe_surface **front_left,
302 struct pipe_surface **front_right)
303 {
304 struct gl_framebuffer *fb = &stfb->Base;
305
306 GET_CURRENT_CONTEXT(ctx);
307
308 if (ctx && ctx->DrawBuffer == &stfb->Base) {
309 st_flush( ctx->st,
310 PIPE_FLUSH_RENDER_CACHE |
311 PIPE_FLUSH_SWAPBUFFERS |
312 PIPE_FLUSH_FRAME,
313 NULL );
314 }
315
316 if (!fb->Visual.doubleBufferMode) {
317 /* single buffer mode - return pointers to front surfaces */
318 if (front_left) {
319 struct st_renderbuffer *strb =
320 st_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
321 *front_left = strb->surface;
322 }
323 if (front_right) {
324 struct st_renderbuffer *strb =
325 st_renderbuffer(fb->Attachment[BUFFER_FRONT_RIGHT].Renderbuffer);
326 *front_right = strb ? strb->surface : NULL;
327 }
328 return;
329 }
330
331 /* swap left buffers */
332 if (fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer &&
333 fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer) {
334 struct gl_renderbuffer *rbTemp;
335 rbTemp = fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer;
336 fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer =
337 fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer;
338 fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer = rbTemp;
339 if (front_left) {
340 struct st_renderbuffer *strb =
341 st_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
342 *front_left = strb->surface;
343 }
344 /* mark back buffer contents as undefined */
345 {
346 struct st_renderbuffer *back =
347 st_renderbuffer(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
348 back->defined = GL_FALSE;
349 }
350 }
351 else {
352 /* no front buffer, display the back buffer */
353 if (front_left) {
354 struct st_renderbuffer *strb =
355 st_renderbuffer(fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
356 *front_left = strb->surface;
357 }
358 }
359
360 /* swap right buffers (for stereo) */
361 if (fb->Attachment[BUFFER_FRONT_RIGHT].Renderbuffer &&
362 fb->Attachment[BUFFER_BACK_RIGHT].Renderbuffer) {
363 struct gl_renderbuffer *rbTemp;
364 rbTemp = fb->Attachment[BUFFER_FRONT_RIGHT].Renderbuffer;
365 fb->Attachment[BUFFER_FRONT_RIGHT].Renderbuffer =
366 fb->Attachment[BUFFER_BACK_RIGHT].Renderbuffer;
367 fb->Attachment[BUFFER_BACK_RIGHT].Renderbuffer = rbTemp;
368 if (front_right) {
369 struct st_renderbuffer *strb =
370 st_renderbuffer(fb->Attachment[BUFFER_FRONT_RIGHT].Renderbuffer);
371 *front_right = strb->surface;
372 }
373 /* mark back buffer contents as undefined */
374 {
375 struct st_renderbuffer *back =
376 st_renderbuffer(fb->Attachment[BUFFER_BACK_RIGHT].Renderbuffer);
377 back->defined = GL_FALSE;
378 }
379 }
380 else {
381 /* no front right buffer, display back right buffer (if exists) */
382 if (front_right) {
383 struct st_renderbuffer *strb =
384 st_renderbuffer(fb->Attachment[BUFFER_BACK_RIGHT].Renderbuffer);
385 *front_right = strb ? strb->surface : NULL;
386 }
387 }
388
389 /* Update the _ColorDrawBuffers[] array and _ColorReadBuffer pointer */
390 _mesa_update_framebuffer(ctx);
391
392 /* Make sure we draw into the new back surface */
393 st_invalidate_state(ctx, _NEW_BUFFERS);
394 }
395
396
397 void *st_framebuffer_private( struct st_framebuffer *stfb )
398 {
399 return stfb->Private;
400 }
401
402 void st_get_framebuffer_dimensions( struct st_framebuffer *stfb,
403 uint *width,
404 uint *height)
405 {
406 *width = stfb->Base.Width;
407 *height = stfb->Base.Height;
408 }