Merge branch 'mesa_7_6_branch'
[mesa.git] / src / mesa / main / framebuffer.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.2
4 *
5 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /**
27 * Functions for allocating/managing framebuffers and renderbuffers.
28 * Also, routines for reading/writing renderbuffer data as ubytes,
29 * ushorts, uints, etc.
30 */
31
32
33 #include "glheader.h"
34 #include "imports.h"
35 #include "buffers.h"
36 #include "context.h"
37 #include "depthstencil.h"
38 #include "macros.h"
39 #include "mtypes.h"
40 #include "fbobject.h"
41 #include "framebuffer.h"
42 #include "renderbuffer.h"
43 #include "texobj.h"
44
45
46
47 /**
48 * Compute/set the _DepthMax field for the given framebuffer.
49 * This value depends on the Z buffer resolution.
50 */
51 static void
52 compute_depth_max(struct gl_framebuffer *fb)
53 {
54 if (fb->Visual.depthBits == 0) {
55 /* Special case. Even if we don't have a depth buffer we need
56 * good values for DepthMax for Z vertex transformation purposes
57 * and for per-fragment fog computation.
58 */
59 fb->_DepthMax = (1 << 16) - 1;
60 }
61 else if (fb->Visual.depthBits < 32) {
62 fb->_DepthMax = (1 << fb->Visual.depthBits) - 1;
63 }
64 else {
65 /* Special case since shift values greater than or equal to the
66 * number of bits in the left hand expression's type are undefined.
67 */
68 fb->_DepthMax = 0xffffffff;
69 }
70 fb->_DepthMaxF = (GLfloat) fb->_DepthMax;
71
72 /* Minimum resolvable depth value, for polygon offset */
73 fb->_MRD = (GLfloat)1.0 / fb->_DepthMaxF;
74 }
75
76
77 /**
78 * Create and initialize a gl_framebuffer object.
79 * This is intended for creating _window_system_ framebuffers, not generic
80 * framebuffer objects ala GL_EXT_framebuffer_object.
81 *
82 * \sa _mesa_new_framebuffer
83 */
84 struct gl_framebuffer *
85 _mesa_create_framebuffer(const GLvisual *visual)
86 {
87 struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
88 assert(visual);
89 if (fb) {
90 _mesa_initialize_framebuffer(fb, visual);
91 }
92 return fb;
93 }
94
95
96 /**
97 * Allocate a new gl_framebuffer object.
98 * This is the default function for ctx->Driver.NewFramebuffer().
99 * This is for allocating user-created framebuffers, not window-system
100 * framebuffers!
101 * \sa _mesa_create_framebuffer
102 */
103 struct gl_framebuffer *
104 _mesa_new_framebuffer(GLcontext *ctx, GLuint name)
105 {
106 struct gl_framebuffer *fb;
107 (void) ctx;
108 assert(name != 0);
109 fb = CALLOC_STRUCT(gl_framebuffer);
110 if (fb) {
111 fb->Name = name;
112 fb->RefCount = 1;
113 fb->_NumColorDrawBuffers = 1;
114 fb->ColorDrawBuffer[0] = GL_COLOR_ATTACHMENT0_EXT;
115 fb->_ColorDrawBufferIndexes[0] = BUFFER_COLOR0;
116 fb->ColorReadBuffer = GL_COLOR_ATTACHMENT0_EXT;
117 fb->_ColorReadBufferIndex = BUFFER_COLOR0;
118 fb->Delete = _mesa_destroy_framebuffer;
119 _glthread_INIT_MUTEX(fb->Mutex);
120 }
121 return fb;
122 }
123
124
125 /**
126 * Initialize a gl_framebuffer object. Typically used to initialize
127 * window system-created framebuffers, not user-created framebuffers.
128 * \sa _mesa_create_framebuffer
129 */
130 void
131 _mesa_initialize_framebuffer(struct gl_framebuffer *fb, const GLvisual *visual)
132 {
133 assert(fb);
134 assert(visual);
135
136 _mesa_bzero(fb, sizeof(struct gl_framebuffer));
137
138 _glthread_INIT_MUTEX(fb->Mutex);
139
140 fb->RefCount = 1;
141
142 /* save the visual */
143 fb->Visual = *visual;
144
145 /* Init read/draw renderbuffer state */
146 if (visual->doubleBufferMode) {
147 fb->_NumColorDrawBuffers = 1;
148 fb->ColorDrawBuffer[0] = GL_BACK;
149 fb->_ColorDrawBufferIndexes[0] = BUFFER_BACK_LEFT;
150 fb->ColorReadBuffer = GL_BACK;
151 fb->_ColorReadBufferIndex = BUFFER_BACK_LEFT;
152 }
153 else {
154 fb->_NumColorDrawBuffers = 1;
155 fb->ColorDrawBuffer[0] = GL_FRONT;
156 fb->_ColorDrawBufferIndexes[0] = BUFFER_FRONT_LEFT;
157 fb->ColorReadBuffer = GL_FRONT;
158 fb->_ColorReadBufferIndex = BUFFER_FRONT_LEFT;
159 }
160
161 fb->Delete = _mesa_destroy_framebuffer;
162 fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
163
164 compute_depth_max(fb);
165 }
166
167
168 /**
169 * Deallocate buffer and everything attached to it.
170 * Typically called via the gl_framebuffer->Delete() method.
171 */
172 void
173 _mesa_destroy_framebuffer(struct gl_framebuffer *fb)
174 {
175 if (fb) {
176 _mesa_free_framebuffer_data(fb);
177 _mesa_free(fb);
178 }
179 }
180
181
182 /**
183 * Free all the data hanging off the given gl_framebuffer, but don't free
184 * the gl_framebuffer object itself.
185 */
186 void
187 _mesa_free_framebuffer_data(struct gl_framebuffer *fb)
188 {
189 GLuint i;
190
191 assert(fb);
192 assert(fb->RefCount == 0);
193
194 _glthread_DESTROY_MUTEX(fb->Mutex);
195
196 for (i = 0; i < BUFFER_COUNT; i++) {
197 struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
198 if (att->Renderbuffer) {
199 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
200 }
201 if (att->Texture) {
202 _mesa_reference_texobj(&att->Texture, NULL);
203 }
204 ASSERT(!att->Renderbuffer);
205 ASSERT(!att->Texture);
206 att->Type = GL_NONE;
207 }
208
209 /* unbind _Depth/_StencilBuffer to decr ref counts */
210 _mesa_reference_renderbuffer(&fb->_DepthBuffer, NULL);
211 _mesa_reference_renderbuffer(&fb->_StencilBuffer, NULL);
212 }
213
214
215 /**
216 * Set *ptr to point to fb, with refcounting and locking.
217 */
218 void
219 _mesa_reference_framebuffer(struct gl_framebuffer **ptr,
220 struct gl_framebuffer *fb)
221 {
222 assert(ptr);
223 if (*ptr == fb) {
224 /* no change */
225 return;
226 }
227
228 if (*ptr) {
229 /* unreference old renderbuffer */
230 GLboolean deleteFlag = GL_FALSE;
231 struct gl_framebuffer *oldFb = *ptr;
232
233 _glthread_LOCK_MUTEX(oldFb->Mutex);
234 ASSERT(oldFb->RefCount > 0);
235 oldFb->RefCount--;
236 deleteFlag = (oldFb->RefCount == 0);
237 _glthread_UNLOCK_MUTEX(oldFb->Mutex);
238
239 if (deleteFlag)
240 oldFb->Delete(oldFb);
241
242 *ptr = NULL;
243 }
244 assert(!*ptr);
245
246 if (fb) {
247 _glthread_LOCK_MUTEX(fb->Mutex);
248 fb->RefCount++;
249 _glthread_UNLOCK_MUTEX(fb->Mutex);
250 *ptr = fb;
251 }
252 }
253
254
255 /**
256 * Resize the given framebuffer's renderbuffers to the new width and height.
257 * This should only be used for window-system framebuffers, not
258 * user-created renderbuffers (i.e. made with GL_EXT_framebuffer_object).
259 * This will typically be called via ctx->Driver.ResizeBuffers() or directly
260 * from a device driver.
261 *
262 * \note it's possible for ctx to be null since a window can be resized
263 * without a currently bound rendering context.
264 */
265 void
266 _mesa_resize_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb,
267 GLuint width, GLuint height)
268 {
269 GLuint i;
270
271 /* XXX I think we could check if the size is not changing
272 * and return early.
273 */
274
275 /* For window system framebuffers, Name is zero */
276 assert(fb->Name == 0);
277
278 for (i = 0; i < BUFFER_COUNT; i++) {
279 struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
280 if (att->Type == GL_RENDERBUFFER_EXT && att->Renderbuffer) {
281 struct gl_renderbuffer *rb = att->Renderbuffer;
282 /* only resize if size is changing */
283 if (rb->Width != width || rb->Height != height) {
284 /* could just as well pass rb->_ActualFormat here */
285 if (rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height)) {
286 ASSERT(rb->Width == width);
287 ASSERT(rb->Height == height);
288 }
289 else {
290 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Resizing framebuffer");
291 /* no return */
292 }
293 }
294 }
295 }
296
297 if (fb->_DepthBuffer) {
298 struct gl_renderbuffer *rb = fb->_DepthBuffer;
299 if (rb->Width != width || rb->Height != height) {
300 if (!rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height)) {
301 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Resizing framebuffer");
302 }
303 }
304 }
305
306 if (fb->_StencilBuffer) {
307 struct gl_renderbuffer *rb = fb->_StencilBuffer;
308 if (rb->Width != width || rb->Height != height) {
309 if (!rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height)) {
310 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Resizing framebuffer");
311 }
312 }
313 }
314
315 fb->Width = width;
316 fb->Height = height;
317
318 if (ctx) {
319 /* update scissor / window bounds */
320 _mesa_update_draw_buffer_bounds(ctx);
321 /* Signal new buffer state so that swrast will update its clipping
322 * info (the CLIP_BIT flag).
323 */
324 ctx->NewState |= _NEW_BUFFERS;
325 }
326 }
327
328
329
330 /**
331 * XXX THIS IS OBSOLETE - drivers should take care of detecting window
332 * size changes and act accordingly, likely calling _mesa_resize_framebuffer().
333 *
334 * GL_MESA_resize_buffers extension.
335 *
336 * When this function is called, we'll ask the window system how large
337 * the current window is. If it's a new size, we'll call the driver's
338 * ResizeBuffers function. The driver will then resize its color buffers
339 * as needed, and maybe call the swrast's routine for reallocating
340 * swrast-managed depth/stencil/accum/etc buffers.
341 * \note This function should only be called through the GL API, not
342 * from device drivers (as was done in the past).
343 */
344 void
345 _mesa_resizebuffers( GLcontext *ctx )
346 {
347 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx );
348
349 if (MESA_VERBOSE & VERBOSE_API)
350 _mesa_debug(ctx, "glResizeBuffersMESA\n");
351
352 if (!ctx->Driver.GetBufferSize) {
353 return;
354 }
355
356 if (ctx->WinSysDrawBuffer) {
357 GLuint newWidth, newHeight;
358 GLframebuffer *buffer = ctx->WinSysDrawBuffer;
359
360 assert(buffer->Name == 0);
361
362 /* ask device driver for size of output buffer */
363 ctx->Driver.GetBufferSize( buffer, &newWidth, &newHeight );
364
365 /* see if size of device driver's color buffer (window) has changed */
366 if (buffer->Width != newWidth || buffer->Height != newHeight) {
367 if (ctx->Driver.ResizeBuffers)
368 ctx->Driver.ResizeBuffers(ctx, buffer, newWidth, newHeight );
369 }
370 }
371
372 if (ctx->WinSysReadBuffer
373 && ctx->WinSysReadBuffer != ctx->WinSysDrawBuffer) {
374 GLuint newWidth, newHeight;
375 GLframebuffer *buffer = ctx->WinSysReadBuffer;
376
377 assert(buffer->Name == 0);
378
379 /* ask device driver for size of read buffer */
380 ctx->Driver.GetBufferSize( buffer, &newWidth, &newHeight );
381
382 /* see if size of device driver's color buffer (window) has changed */
383 if (buffer->Width != newWidth || buffer->Height != newHeight) {
384 if (ctx->Driver.ResizeBuffers)
385 ctx->Driver.ResizeBuffers(ctx, buffer, newWidth, newHeight );
386 }
387 }
388
389 ctx->NewState |= _NEW_BUFFERS; /* to update scissor / window bounds */
390 }
391
392
393 /*
394 * XXX THIS IS OBSOLETE
395 */
396 void GLAPIENTRY
397 _mesa_ResizeBuffersMESA( void )
398 {
399 GET_CURRENT_CONTEXT(ctx);
400
401 if (ctx->Extensions.MESA_resize_buffers)
402 _mesa_resizebuffers( ctx );
403 }
404
405
406
407 /**
408 * Examine all the framebuffer's renderbuffers to update the Width/Height
409 * fields of the framebuffer. If we have renderbuffers with different
410 * sizes, set the framebuffer's width and height to the min size.
411 * Note: this is only intended for user-created framebuffers, not
412 * window-system framebuffes.
413 */
414 static void
415 update_framebuffer_size(GLcontext *ctx, struct gl_framebuffer *fb)
416 {
417 GLuint minWidth = ~0, minHeight = ~0;
418 GLuint i;
419
420 /* user-created framebuffers only */
421 assert(fb->Name);
422
423 for (i = 0; i < BUFFER_COUNT; i++) {
424 struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
425 const struct gl_renderbuffer *rb = att->Renderbuffer;
426 if (rb) {
427 minWidth = MIN2(minWidth, rb->Width);
428 minHeight = MIN2(minHeight, rb->Height);
429 }
430 }
431
432 if (minWidth != ~0) {
433 fb->Width = minWidth;
434 fb->Height = minHeight;
435 }
436 else {
437 fb->Width = 0;
438 fb->Height = 0;
439 }
440 }
441
442
443 /**
444 * Update the context's current drawing buffer's Xmin, Xmax, Ymin, Ymax fields.
445 * These values are computed from the buffer's width and height and
446 * the scissor box, if it's enabled.
447 * \param ctx the GL context.
448 */
449 void
450 _mesa_update_draw_buffer_bounds(GLcontext *ctx)
451 {
452 struct gl_framebuffer *buffer = ctx->DrawBuffer;
453
454 if (!buffer)
455 return;
456
457 if (buffer->Name) {
458 /* user-created framebuffer size depends on the renderbuffers */
459 update_framebuffer_size(ctx, buffer);
460 }
461
462 buffer->_Xmin = 0;
463 buffer->_Ymin = 0;
464 buffer->_Xmax = buffer->Width;
465 buffer->_Ymax = buffer->Height;
466
467 if (ctx->Scissor.Enabled) {
468 if (ctx->Scissor.X > buffer->_Xmin) {
469 buffer->_Xmin = ctx->Scissor.X;
470 }
471 if (ctx->Scissor.Y > buffer->_Ymin) {
472 buffer->_Ymin = ctx->Scissor.Y;
473 }
474 if (ctx->Scissor.X + ctx->Scissor.Width < buffer->_Xmax) {
475 buffer->_Xmax = ctx->Scissor.X + ctx->Scissor.Width;
476 }
477 if (ctx->Scissor.Y + ctx->Scissor.Height < buffer->_Ymax) {
478 buffer->_Ymax = ctx->Scissor.Y + ctx->Scissor.Height;
479 }
480 /* finally, check for empty region */
481 if (buffer->_Xmin > buffer->_Xmax) {
482 buffer->_Xmin = buffer->_Xmax;
483 }
484 if (buffer->_Ymin > buffer->_Ymax) {
485 buffer->_Ymin = buffer->_Ymax;
486 }
487 }
488
489 ASSERT(buffer->_Xmin <= buffer->_Xmax);
490 ASSERT(buffer->_Ymin <= buffer->_Ymax);
491 }
492
493
494 /**
495 * The glGet queries of the framebuffer red/green/blue size, stencil size,
496 * etc. are satisfied by the fields of ctx->DrawBuffer->Visual. These can
497 * change depending on the renderbuffer bindings. This function updates
498 * the given framebuffer's Visual from the current renderbuffer bindings.
499 *
500 * This may apply to user-created framebuffers or window system framebuffers.
501 *
502 * Also note: ctx->DrawBuffer->Visual.depthBits might not equal
503 * ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer.DepthBits.
504 * The former one is used to convert floating point depth values into
505 * integer Z values.
506 */
507 void
508 _mesa_update_framebuffer_visual(struct gl_framebuffer *fb)
509 {
510 GLuint i;
511
512 _mesa_bzero(&fb->Visual, sizeof(fb->Visual));
513 fb->Visual.rgbMode = GL_TRUE; /* assume this */
514
515 #if 0 /* this _might_ be needed */
516 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
517 /* leave visual fields zero'd */
518 return;
519 }
520 #endif
521
522 /* find first RGB or CI renderbuffer */
523 for (i = 0; i < BUFFER_COUNT; i++) {
524 if (fb->Attachment[i].Renderbuffer) {
525 const struct gl_renderbuffer *rb = fb->Attachment[i].Renderbuffer;
526 if (rb->_BaseFormat == GL_RGBA || rb->_BaseFormat == GL_RGB) {
527 fb->Visual.redBits = rb->RedBits;
528 fb->Visual.greenBits = rb->GreenBits;
529 fb->Visual.blueBits = rb->BlueBits;
530 fb->Visual.alphaBits = rb->AlphaBits;
531 fb->Visual.rgbBits = fb->Visual.redBits
532 + fb->Visual.greenBits + fb->Visual.blueBits;
533 fb->Visual.floatMode = GL_FALSE;
534 fb->Visual.samples = rb->NumSamples;
535 break;
536 }
537 else if (rb->_BaseFormat == GL_COLOR_INDEX) {
538 fb->Visual.indexBits = rb->IndexBits;
539 fb->Visual.rgbMode = GL_FALSE;
540 break;
541 }
542 }
543 }
544
545 if (fb->Attachment[BUFFER_DEPTH].Renderbuffer) {
546 fb->Visual.haveDepthBuffer = GL_TRUE;
547 fb->Visual.depthBits
548 = fb->Attachment[BUFFER_DEPTH].Renderbuffer->DepthBits;
549 }
550
551 if (fb->Attachment[BUFFER_STENCIL].Renderbuffer) {
552 fb->Visual.haveStencilBuffer = GL_TRUE;
553 fb->Visual.stencilBits
554 = fb->Attachment[BUFFER_STENCIL].Renderbuffer->StencilBits;
555 }
556
557 if (fb->Attachment[BUFFER_ACCUM].Renderbuffer) {
558 fb->Visual.haveAccumBuffer = GL_TRUE;
559 fb->Visual.accumRedBits
560 = fb->Attachment[BUFFER_ACCUM].Renderbuffer->RedBits;
561 fb->Visual.accumGreenBits
562 = fb->Attachment[BUFFER_ACCUM].Renderbuffer->GreenBits;
563 fb->Visual.accumBlueBits
564 = fb->Attachment[BUFFER_ACCUM].Renderbuffer->BlueBits;
565 fb->Visual.accumAlphaBits
566 = fb->Attachment[BUFFER_ACCUM].Renderbuffer->AlphaBits;
567 }
568
569 compute_depth_max(fb);
570 }
571
572
573 /**
574 * Update the framebuffer's _DepthBuffer field using the renderbuffer
575 * found at the given attachment index.
576 *
577 * If that attachment points to a combined GL_DEPTH_STENCIL renderbuffer,
578 * create and install a depth wrapper/adaptor.
579 *
580 * \param fb the framebuffer whose _DepthBuffer field to update
581 * \param attIndex indicates the renderbuffer to possibly wrap
582 */
583 void
584 _mesa_update_depth_buffer(GLcontext *ctx,
585 struct gl_framebuffer *fb,
586 GLuint attIndex)
587 {
588 struct gl_renderbuffer *depthRb;
589
590 /* only one possiblity for now */
591 ASSERT(attIndex == BUFFER_DEPTH);
592
593 depthRb = fb->Attachment[attIndex].Renderbuffer;
594
595 if (depthRb && depthRb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT) {
596 /* The attached depth buffer is a GL_DEPTH_STENCIL renderbuffer */
597 if (!fb->_DepthBuffer
598 || fb->_DepthBuffer->Wrapped != depthRb
599 || fb->_DepthBuffer->_BaseFormat != GL_DEPTH_COMPONENT) {
600 /* need to update wrapper */
601 struct gl_renderbuffer *wrapper
602 = _mesa_new_z24_renderbuffer_wrapper(ctx, depthRb);
603 _mesa_reference_renderbuffer(&fb->_DepthBuffer, wrapper);
604 ASSERT(fb->_DepthBuffer->Wrapped == depthRb);
605 }
606 }
607 else {
608 /* depthRb may be null */
609 _mesa_reference_renderbuffer(&fb->_DepthBuffer, depthRb);
610 }
611 }
612
613
614 /**
615 * Update the framebuffer's _StencilBuffer field using the renderbuffer
616 * found at the given attachment index.
617 *
618 * If that attachment points to a combined GL_DEPTH_STENCIL renderbuffer,
619 * create and install a stencil wrapper/adaptor.
620 *
621 * \param fb the framebuffer whose _StencilBuffer field to update
622 * \param attIndex indicates the renderbuffer to possibly wrap
623 */
624 void
625 _mesa_update_stencil_buffer(GLcontext *ctx,
626 struct gl_framebuffer *fb,
627 GLuint attIndex)
628 {
629 struct gl_renderbuffer *stencilRb;
630
631 ASSERT(attIndex == BUFFER_DEPTH ||
632 attIndex == BUFFER_STENCIL);
633
634 stencilRb = fb->Attachment[attIndex].Renderbuffer;
635
636 if (stencilRb && stencilRb->_ActualFormat == GL_DEPTH24_STENCIL8_EXT) {
637 /* The attached stencil buffer is a GL_DEPTH_STENCIL renderbuffer */
638 if (!fb->_StencilBuffer
639 || fb->_StencilBuffer->Wrapped != stencilRb
640 || fb->_StencilBuffer->_BaseFormat != GL_STENCIL_INDEX) {
641 /* need to update wrapper */
642 struct gl_renderbuffer *wrapper
643 = _mesa_new_s8_renderbuffer_wrapper(ctx, stencilRb);
644 _mesa_reference_renderbuffer(&fb->_StencilBuffer, wrapper);
645 ASSERT(fb->_StencilBuffer->Wrapped == stencilRb);
646 }
647 }
648 else {
649 /* stencilRb may be null */
650 _mesa_reference_renderbuffer(&fb->_StencilBuffer, stencilRb);
651 }
652 }
653
654
655 /*
656 * Example DrawBuffers scenarios:
657 *
658 * 1. glDrawBuffer(GL_FRONT_AND_BACK), fixed-func or shader writes to
659 * "gl_FragColor" or program writes to the "result.color" register:
660 *
661 * fragment color output renderbuffer
662 * --------------------- ---------------
663 * color[0] Front, Back
664 *
665 *
666 * 2. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]), shader writes to
667 * gl_FragData[i] or program writes to result.color[i] registers:
668 *
669 * fragment color output renderbuffer
670 * --------------------- ---------------
671 * color[0] Front
672 * color[1] Aux0
673 * color[3] Aux1
674 *
675 *
676 * 3. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]) and shader writes to
677 * gl_FragColor, or fixed function:
678 *
679 * fragment color output renderbuffer
680 * --------------------- ---------------
681 * color[0] Front, Aux0, Aux1
682 *
683 *
684 * In either case, the list of renderbuffers is stored in the
685 * framebuffer->_ColorDrawBuffers[] array and
686 * framebuffer->_NumColorDrawBuffers indicates the number of buffers.
687 * The renderer (like swrast) has to look at the current fragment shader
688 * to see if it writes to gl_FragColor vs. gl_FragData[i] to determine
689 * how to map color outputs to renderbuffers.
690 *
691 * Note that these two calls are equivalent (for fixed function fragment
692 * shading anyway):
693 * a) glDrawBuffer(GL_FRONT_AND_BACK); (assuming non-stereo framebuffer)
694 * b) glDrawBuffers(2, [GL_FRONT_LEFT, GL_BACK_LEFT]);
695 */
696
697
698
699
700 /**
701 * Update the (derived) list of color drawing renderbuffer pointers.
702 * Later, when we're rendering we'll loop from 0 to _NumColorDrawBuffers
703 * writing colors.
704 */
705 static void
706 update_color_draw_buffers(GLcontext *ctx, struct gl_framebuffer *fb)
707 {
708 GLuint output;
709
710 /* set 0th buffer to NULL now in case _NumColorDrawBuffers is zero */
711 fb->_ColorDrawBuffers[0] = NULL;
712
713 for (output = 0; output < fb->_NumColorDrawBuffers; output++) {
714 GLint buf = fb->_ColorDrawBufferIndexes[output];
715 if (buf >= 0) {
716 fb->_ColorDrawBuffers[output] = fb->Attachment[buf].Renderbuffer;
717 }
718 else {
719 fb->_ColorDrawBuffers[output] = NULL;
720 }
721 }
722 }
723
724
725 /**
726 * Update the (derived) color read renderbuffer pointer.
727 * Unlike the DrawBuffer, we can only read from one (or zero) color buffers.
728 */
729 static void
730 update_color_read_buffer(GLcontext *ctx, struct gl_framebuffer *fb)
731 {
732 (void) ctx;
733 if (fb->_ColorReadBufferIndex == -1 ||
734 fb->DeletePending ||
735 fb->Width == 0 ||
736 fb->Height == 0) {
737 fb->_ColorReadBuffer = NULL; /* legal! */
738 }
739 else {
740 ASSERT(fb->_ColorReadBufferIndex >= 0);
741 ASSERT(fb->_ColorReadBufferIndex < BUFFER_COUNT);
742 fb->_ColorReadBuffer
743 = fb->Attachment[fb->_ColorReadBufferIndex].Renderbuffer;
744 }
745 }
746
747
748 /**
749 * Update a gl_framebuffer's derived state.
750 *
751 * Specifically, update these framebuffer fields:
752 * _ColorDrawBuffers
753 * _NumColorDrawBuffers
754 * _ColorReadBuffer
755 * _DepthBuffer
756 * _StencilBuffer
757 *
758 * If the framebuffer is user-created, make sure it's complete.
759 *
760 * The following functions (at least) can effect framebuffer state:
761 * glReadBuffer, glDrawBuffer, glDrawBuffersARB, glFramebufferRenderbufferEXT,
762 * glRenderbufferStorageEXT.
763 */
764 static void
765 update_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
766 {
767 if (fb->Name == 0) {
768 /* This is a window-system framebuffer */
769 /* Need to update the FB's GL_DRAW_BUFFER state to match the
770 * context state (GL_READ_BUFFER too).
771 */
772 if (fb->ColorDrawBuffer[0] != ctx->Color.DrawBuffer[0]) {
773 _mesa_drawbuffers(ctx, ctx->Const.MaxDrawBuffers,
774 ctx->Color.DrawBuffer, NULL);
775 }
776 if (fb->ColorReadBuffer != ctx->Pixel.ReadBuffer) {
777
778 }
779 }
780 else {
781 /* This is a user-created framebuffer.
782 * Completeness only matters for user-created framebuffers.
783 */
784 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE) {
785 _mesa_test_framebuffer_completeness(ctx, fb);
786 }
787 }
788
789 /* Strictly speaking, we don't need to update the draw-state
790 * if this FB is bound as ctx->ReadBuffer (and conversely, the
791 * read-state if this FB is bound as ctx->DrawBuffer), but no
792 * harm.
793 */
794 update_color_draw_buffers(ctx, fb);
795 update_color_read_buffer(ctx, fb);
796 _mesa_update_depth_buffer(ctx, fb, BUFFER_DEPTH);
797 _mesa_update_stencil_buffer(ctx, fb, BUFFER_STENCIL);
798
799 compute_depth_max(fb);
800 }
801
802
803 /**
804 * Update state related to the current draw/read framebuffers.
805 */
806 void
807 _mesa_update_framebuffer(GLcontext *ctx)
808 {
809 struct gl_framebuffer *drawFb = ctx->DrawBuffer;
810 struct gl_framebuffer *readFb = ctx->ReadBuffer;
811
812 update_framebuffer(ctx, drawFb);
813 if (readFb != drawFb)
814 update_framebuffer(ctx, readFb);
815 }
816
817
818 /**
819 * Check if the renderbuffer for a read operation (glReadPixels, glCopyPixels,
820 * glCopyTex[Sub]Image, etc) exists.
821 * \param format a basic image format such as GL_RGB, GL_RGBA, GL_ALPHA,
822 * GL_DEPTH_COMPONENT, etc. or GL_COLOR, GL_DEPTH, GL_STENCIL.
823 * \return GL_TRUE if buffer exists, GL_FALSE otherwise
824 */
825 GLboolean
826 _mesa_source_buffer_exists(GLcontext *ctx, GLenum format)
827 {
828 const struct gl_renderbuffer_attachment *att = ctx->ReadBuffer->Attachment;
829
830 /* If we don't know the framebuffer status, update it now */
831 if (ctx->ReadBuffer->_Status == 0) {
832 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
833 }
834
835 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
836 return GL_FALSE;
837 }
838
839 switch (format) {
840 case GL_COLOR:
841 case GL_RED:
842 case GL_GREEN:
843 case GL_BLUE:
844 case GL_ALPHA:
845 case GL_LUMINANCE:
846 case GL_LUMINANCE_ALPHA:
847 case GL_INTENSITY:
848 case GL_RGB:
849 case GL_BGR:
850 case GL_RGBA:
851 case GL_BGRA:
852 case GL_ABGR_EXT:
853 case GL_COLOR_INDEX:
854 if (ctx->ReadBuffer->_ColorReadBuffer == NULL) {
855 return GL_FALSE;
856 }
857 ASSERT(ctx->ReadBuffer->_ColorReadBuffer->RedBits > 0 ||
858 ctx->ReadBuffer->_ColorReadBuffer->IndexBits > 0);
859 break;
860 case GL_DEPTH:
861 case GL_DEPTH_COMPONENT:
862 if (!att[BUFFER_DEPTH].Renderbuffer) {
863 return GL_FALSE;
864 }
865 ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0);
866 break;
867 case GL_STENCIL:
868 case GL_STENCIL_INDEX:
869 if (!att[BUFFER_STENCIL].Renderbuffer) {
870 return GL_FALSE;
871 }
872 ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0);
873 break;
874 case GL_DEPTH_STENCIL_EXT:
875 if (!att[BUFFER_DEPTH].Renderbuffer ||
876 !att[BUFFER_STENCIL].Renderbuffer) {
877 return GL_FALSE;
878 }
879 ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0);
880 ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0);
881 break;
882 default:
883 _mesa_problem(ctx,
884 "Unexpected format 0x%x in _mesa_source_buffer_exists",
885 format);
886 return GL_FALSE;
887 }
888
889 /* OK */
890 return GL_TRUE;
891 }
892
893
894 /**
895 * As above, but for drawing operations.
896 * XXX could do some code merging w/ above function.
897 */
898 GLboolean
899 _mesa_dest_buffer_exists(GLcontext *ctx, GLenum format)
900 {
901 const struct gl_renderbuffer_attachment *att = ctx->DrawBuffer->Attachment;
902
903 /* If we don't know the framebuffer status, update it now */
904 if (ctx->DrawBuffer->_Status == 0) {
905 _mesa_test_framebuffer_completeness(ctx, ctx->DrawBuffer);
906 }
907
908 if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
909 return GL_FALSE;
910 }
911
912 switch (format) {
913 case GL_COLOR:
914 case GL_RED:
915 case GL_GREEN:
916 case GL_BLUE:
917 case GL_ALPHA:
918 case GL_LUMINANCE:
919 case GL_LUMINANCE_ALPHA:
920 case GL_INTENSITY:
921 case GL_RGB:
922 case GL_BGR:
923 case GL_RGBA:
924 case GL_BGRA:
925 case GL_ABGR_EXT:
926 case GL_COLOR_INDEX:
927 /* Nothing special since GL_DRAW_BUFFER could be GL_NONE. */
928 /* Could assert that colorbuffer has RedBits > 0 */
929 break;
930 case GL_DEPTH:
931 case GL_DEPTH_COMPONENT:
932 if (!att[BUFFER_DEPTH].Renderbuffer) {
933 return GL_FALSE;
934 }
935 ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0);
936 break;
937 case GL_STENCIL:
938 case GL_STENCIL_INDEX:
939 if (!att[BUFFER_STENCIL].Renderbuffer) {
940 return GL_FALSE;
941 }
942 ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0);
943 break;
944 case GL_DEPTH_STENCIL_EXT:
945 if (!att[BUFFER_DEPTH].Renderbuffer ||
946 !att[BUFFER_STENCIL].Renderbuffer) {
947 return GL_FALSE;
948 }
949 ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0);
950 ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0);
951 break;
952 default:
953 _mesa_problem(ctx,
954 "Unexpected format 0x%x in _mesa_dest_buffer_exists",
955 format);
956 return GL_FALSE;
957 }
958
959 /* OK */
960 return GL_TRUE;
961 }