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