Merge branch 'gallium-polygon-stipple'
[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 "enums.h"
39 #include "formats.h"
40 #include "macros.h"
41 #include "mtypes.h"
42 #include "fbobject.h"
43 #include "framebuffer.h"
44 #include "renderbuffer.h"
45 #include "texobj.h"
46
47
48
49 /**
50 * Compute/set the _DepthMax field for the given framebuffer.
51 * This value depends on the Z buffer resolution.
52 */
53 static void
54 compute_depth_max(struct gl_framebuffer *fb)
55 {
56 if (fb->Visual.depthBits == 0) {
57 /* Special case. Even if we don't have a depth buffer we need
58 * good values for DepthMax for Z vertex transformation purposes
59 * and for per-fragment fog computation.
60 */
61 fb->_DepthMax = (1 << 16) - 1;
62 }
63 else if (fb->Visual.depthBits < 32) {
64 fb->_DepthMax = (1 << fb->Visual.depthBits) - 1;
65 }
66 else {
67 /* Special case since shift values greater than or equal to the
68 * number of bits in the left hand expression's type are undefined.
69 */
70 fb->_DepthMax = 0xffffffff;
71 }
72 fb->_DepthMaxF = (GLfloat) fb->_DepthMax;
73
74 /* Minimum resolvable depth value, for polygon offset */
75 fb->_MRD = (GLfloat)1.0 / fb->_DepthMaxF;
76 }
77
78 /**
79 * Create and initialize a gl_framebuffer object.
80 * This is intended for creating _window_system_ framebuffers, not generic
81 * framebuffer objects ala GL_EXT_framebuffer_object.
82 *
83 * \sa _mesa_new_framebuffer
84 */
85 struct gl_framebuffer *
86 _mesa_create_framebuffer(const struct gl_config *visual)
87 {
88 struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
89 assert(visual);
90 if (fb) {
91 _mesa_initialize_window_framebuffer(fb, visual);
92 }
93 return fb;
94 }
95
96
97 /**
98 * Allocate a new gl_framebuffer object.
99 * This is the default function for ctx->Driver.NewFramebuffer().
100 * This is for allocating user-created framebuffers, not window-system
101 * framebuffers!
102 * \sa _mesa_create_framebuffer
103 */
104 struct gl_framebuffer *
105 _mesa_new_framebuffer(struct gl_context *ctx, GLuint name)
106 {
107 struct gl_framebuffer *fb;
108 (void) ctx;
109 assert(name != 0);
110 fb = CALLOC_STRUCT(gl_framebuffer);
111 if (fb) {
112 _mesa_initialize_user_framebuffer(fb, name);
113 }
114 return fb;
115 }
116
117
118 /**
119 * Initialize a gl_framebuffer object. Typically used to initialize
120 * window system-created framebuffers, not user-created framebuffers.
121 * \sa _mesa_initialize_user_framebuffer
122 */
123 void
124 _mesa_initialize_window_framebuffer(struct gl_framebuffer *fb,
125 const struct gl_config *visual)
126 {
127 assert(fb);
128 assert(visual);
129
130 memset(fb, 0, sizeof(struct gl_framebuffer));
131
132 _glthread_INIT_MUTEX(fb->Mutex);
133
134 fb->RefCount = 1;
135
136 /* save the visual */
137 fb->Visual = *visual;
138
139 /* Init read/draw renderbuffer state */
140 if (visual->doubleBufferMode) {
141 fb->_NumColorDrawBuffers = 1;
142 fb->ColorDrawBuffer[0] = GL_BACK;
143 fb->_ColorDrawBufferIndexes[0] = BUFFER_BACK_LEFT;
144 fb->ColorReadBuffer = GL_BACK;
145 fb->_ColorReadBufferIndex = BUFFER_BACK_LEFT;
146 }
147 else {
148 fb->_NumColorDrawBuffers = 1;
149 fb->ColorDrawBuffer[0] = GL_FRONT;
150 fb->_ColorDrawBufferIndexes[0] = BUFFER_FRONT_LEFT;
151 fb->ColorReadBuffer = GL_FRONT;
152 fb->_ColorReadBufferIndex = BUFFER_FRONT_LEFT;
153 }
154
155 fb->Delete = _mesa_destroy_framebuffer;
156 fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
157
158 compute_depth_max(fb);
159 }
160
161
162 /**
163 * Initialize a user-created gl_framebuffer object.
164 * \sa _mesa_initialize_window_framebuffer
165 */
166 void
167 _mesa_initialize_user_framebuffer(struct gl_framebuffer *fb, GLuint name)
168 {
169 assert(fb);
170 assert(name);
171
172 memset(fb, 0, sizeof(struct gl_framebuffer));
173
174 fb->Name = name;
175 fb->RefCount = 1;
176 fb->_NumColorDrawBuffers = 1;
177 fb->ColorDrawBuffer[0] = GL_COLOR_ATTACHMENT0_EXT;
178 fb->_ColorDrawBufferIndexes[0] = BUFFER_COLOR0;
179 fb->ColorReadBuffer = GL_COLOR_ATTACHMENT0_EXT;
180 fb->_ColorReadBufferIndex = BUFFER_COLOR0;
181 fb->Delete = _mesa_destroy_framebuffer;
182 _glthread_INIT_MUTEX(fb->Mutex);
183 }
184
185
186 /**
187 * Deallocate buffer and everything attached to it.
188 * Typically called via the gl_framebuffer->Delete() method.
189 */
190 void
191 _mesa_destroy_framebuffer(struct gl_framebuffer *fb)
192 {
193 if (fb) {
194 _mesa_free_framebuffer_data(fb);
195 free(fb);
196 }
197 }
198
199
200 /**
201 * Free all the data hanging off the given gl_framebuffer, but don't free
202 * the gl_framebuffer object itself.
203 */
204 void
205 _mesa_free_framebuffer_data(struct gl_framebuffer *fb)
206 {
207 GLuint i;
208
209 assert(fb);
210 assert(fb->RefCount == 0);
211
212 _glthread_DESTROY_MUTEX(fb->Mutex);
213
214 for (i = 0; i < BUFFER_COUNT; i++) {
215 struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
216 if (att->Renderbuffer) {
217 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
218 }
219 if (att->Texture) {
220 _mesa_reference_texobj(&att->Texture, NULL);
221 }
222 ASSERT(!att->Renderbuffer);
223 ASSERT(!att->Texture);
224 att->Type = GL_NONE;
225 }
226
227 /* unbind _Depth/_StencilBuffer to decr ref counts */
228 _mesa_reference_renderbuffer(&fb->_DepthBuffer, NULL);
229 _mesa_reference_renderbuffer(&fb->_StencilBuffer, NULL);
230 }
231
232
233 /**
234 * Set *ptr to point to fb, with refcounting and locking.
235 * This is normally only called from the _mesa_reference_framebuffer() macro
236 * when there's a real pointer change.
237 */
238 void
239 _mesa_reference_framebuffer_(struct gl_framebuffer **ptr,
240 struct gl_framebuffer *fb)
241 {
242 if (*ptr) {
243 /* unreference old renderbuffer */
244 GLboolean deleteFlag = GL_FALSE;
245 struct gl_framebuffer *oldFb = *ptr;
246
247 _glthread_LOCK_MUTEX(oldFb->Mutex);
248 ASSERT(oldFb->RefCount > 0);
249 oldFb->RefCount--;
250 deleteFlag = (oldFb->RefCount == 0);
251 _glthread_UNLOCK_MUTEX(oldFb->Mutex);
252
253 if (deleteFlag)
254 oldFb->Delete(oldFb);
255
256 *ptr = NULL;
257 }
258 assert(!*ptr);
259
260 if (fb) {
261 _glthread_LOCK_MUTEX(fb->Mutex);
262 fb->RefCount++;
263 _glthread_UNLOCK_MUTEX(fb->Mutex);
264 *ptr = fb;
265 }
266 }
267
268
269 /**
270 * Resize the given framebuffer's renderbuffers to the new width and height.
271 * This should only be used for window-system framebuffers, not
272 * user-created renderbuffers (i.e. made with GL_EXT_framebuffer_object).
273 * This will typically be called via ctx->Driver.ResizeBuffers() or directly
274 * from a device driver.
275 *
276 * \note it's possible for ctx to be null since a window can be resized
277 * without a currently bound rendering context.
278 */
279 void
280 _mesa_resize_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
281 GLuint width, GLuint height)
282 {
283 GLuint i;
284
285 /* XXX I think we could check if the size is not changing
286 * and return early.
287 */
288
289 /* For window system framebuffers, Name is zero */
290 assert(fb->Name == 0);
291
292 for (i = 0; i < BUFFER_COUNT; i++) {
293 struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
294 if (att->Type == GL_RENDERBUFFER_EXT && att->Renderbuffer) {
295 struct gl_renderbuffer *rb = att->Renderbuffer;
296 /* only resize if size is changing */
297 if (rb->Width != width || rb->Height != height) {
298 if (rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height)) {
299 ASSERT(rb->Width == width);
300 ASSERT(rb->Height == height);
301 }
302 else {
303 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Resizing framebuffer");
304 /* no return */
305 }
306 }
307 }
308 }
309
310 if (fb->_DepthBuffer) {
311 struct gl_renderbuffer *rb = fb->_DepthBuffer;
312 if (rb->Width != width || rb->Height != height) {
313 if (!rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height)) {
314 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Resizing framebuffer");
315 }
316 }
317 }
318
319 if (fb->_StencilBuffer) {
320 struct gl_renderbuffer *rb = fb->_StencilBuffer;
321 if (rb->Width != width || rb->Height != height) {
322 if (!rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height)) {
323 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Resizing framebuffer");
324 }
325 }
326 }
327
328 fb->Width = width;
329 fb->Height = height;
330
331 if (ctx) {
332 /* update scissor / window bounds */
333 _mesa_update_draw_buffer_bounds(ctx);
334 /* Signal new buffer state so that swrast will update its clipping
335 * info (the CLIP_BIT flag).
336 */
337 ctx->NewState |= _NEW_BUFFERS;
338 }
339 }
340
341
342
343 /**
344 * XXX THIS IS OBSOLETE - drivers should take care of detecting window
345 * size changes and act accordingly, likely calling _mesa_resize_framebuffer().
346 *
347 * GL_MESA_resize_buffers extension.
348 *
349 * When this function is called, we'll ask the window system how large
350 * the current window is. If it's a new size, we'll call the driver's
351 * ResizeBuffers function. The driver will then resize its color buffers
352 * as needed, and maybe call the swrast's routine for reallocating
353 * swrast-managed depth/stencil/accum/etc buffers.
354 * \note This function should only be called through the GL API, not
355 * from device drivers (as was done in the past).
356 */
357 void
358 _mesa_resizebuffers( struct gl_context *ctx )
359 {
360 ASSERT_OUTSIDE_BEGIN_END_AND_FLUSH( ctx );
361
362 if (MESA_VERBOSE & VERBOSE_API)
363 _mesa_debug(ctx, "glResizeBuffersMESA\n");
364
365 if (!ctx->Driver.GetBufferSize) {
366 return;
367 }
368
369 if (ctx->WinSysDrawBuffer) {
370 GLuint newWidth, newHeight;
371 struct gl_framebuffer *buffer = ctx->WinSysDrawBuffer;
372
373 assert(buffer->Name == 0);
374
375 /* ask device driver for size of output buffer */
376 ctx->Driver.GetBufferSize( buffer, &newWidth, &newHeight );
377
378 /* see if size of device driver's color buffer (window) has changed */
379 if (buffer->Width != newWidth || buffer->Height != newHeight) {
380 if (ctx->Driver.ResizeBuffers)
381 ctx->Driver.ResizeBuffers(ctx, buffer, newWidth, newHeight );
382 }
383 }
384
385 if (ctx->WinSysReadBuffer
386 && ctx->WinSysReadBuffer != ctx->WinSysDrawBuffer) {
387 GLuint newWidth, newHeight;
388 struct gl_framebuffer *buffer = ctx->WinSysReadBuffer;
389
390 assert(buffer->Name == 0);
391
392 /* ask device driver for size of read buffer */
393 ctx->Driver.GetBufferSize( buffer, &newWidth, &newHeight );
394
395 /* see if size of device driver's color buffer (window) has changed */
396 if (buffer->Width != newWidth || buffer->Height != newHeight) {
397 if (ctx->Driver.ResizeBuffers)
398 ctx->Driver.ResizeBuffers(ctx, buffer, newWidth, newHeight );
399 }
400 }
401
402 ctx->NewState |= _NEW_BUFFERS; /* to update scissor / window bounds */
403 }
404
405
406 /*
407 * XXX THIS IS OBSOLETE
408 */
409 void GLAPIENTRY
410 _mesa_ResizeBuffersMESA( void )
411 {
412 GET_CURRENT_CONTEXT(ctx);
413
414 if (ctx->Extensions.MESA_resize_buffers)
415 _mesa_resizebuffers( ctx );
416 }
417
418
419
420 /**
421 * Examine all the framebuffer's renderbuffers to update the Width/Height
422 * fields of the framebuffer. If we have renderbuffers with different
423 * sizes, set the framebuffer's width and height to the min size.
424 * Note: this is only intended for user-created framebuffers, not
425 * window-system framebuffes.
426 */
427 static void
428 update_framebuffer_size(struct gl_context *ctx, struct gl_framebuffer *fb)
429 {
430 GLuint minWidth = ~0, minHeight = ~0;
431 GLuint i;
432
433 /* user-created framebuffers only */
434 assert(fb->Name);
435
436 for (i = 0; i < BUFFER_COUNT; i++) {
437 struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
438 const struct gl_renderbuffer *rb = att->Renderbuffer;
439 if (rb) {
440 minWidth = MIN2(minWidth, rb->Width);
441 minHeight = MIN2(minHeight, rb->Height);
442 }
443 }
444
445 if (minWidth != ~0) {
446 fb->Width = minWidth;
447 fb->Height = minHeight;
448 }
449 else {
450 fb->Width = 0;
451 fb->Height = 0;
452 }
453 }
454
455
456 /**
457 * Update the context's current drawing buffer's Xmin, Xmax, Ymin, Ymax fields.
458 * These values are computed from the buffer's width and height and
459 * the scissor box, if it's enabled.
460 * \param ctx the GL context.
461 */
462 void
463 _mesa_update_draw_buffer_bounds(struct gl_context *ctx)
464 {
465 struct gl_framebuffer *buffer = ctx->DrawBuffer;
466
467 if (!buffer)
468 return;
469
470 if (buffer->Name) {
471 /* user-created framebuffer size depends on the renderbuffers */
472 update_framebuffer_size(ctx, buffer);
473 }
474
475 buffer->_Xmin = 0;
476 buffer->_Ymin = 0;
477 buffer->_Xmax = buffer->Width;
478 buffer->_Ymax = buffer->Height;
479
480 if (ctx->Scissor.Enabled) {
481 if (ctx->Scissor.X > buffer->_Xmin) {
482 buffer->_Xmin = ctx->Scissor.X;
483 }
484 if (ctx->Scissor.Y > buffer->_Ymin) {
485 buffer->_Ymin = ctx->Scissor.Y;
486 }
487 if (ctx->Scissor.X + ctx->Scissor.Width < buffer->_Xmax) {
488 buffer->_Xmax = ctx->Scissor.X + ctx->Scissor.Width;
489 }
490 if (ctx->Scissor.Y + ctx->Scissor.Height < buffer->_Ymax) {
491 buffer->_Ymax = ctx->Scissor.Y + ctx->Scissor.Height;
492 }
493 /* finally, check for empty region */
494 if (buffer->_Xmin > buffer->_Xmax) {
495 buffer->_Xmin = buffer->_Xmax;
496 }
497 if (buffer->_Ymin > buffer->_Ymax) {
498 buffer->_Ymin = buffer->_Ymax;
499 }
500 }
501
502 ASSERT(buffer->_Xmin <= buffer->_Xmax);
503 ASSERT(buffer->_Ymin <= buffer->_Ymax);
504 }
505
506
507 /**
508 * The glGet queries of the framebuffer red/green/blue size, stencil size,
509 * etc. are satisfied by the fields of ctx->DrawBuffer->Visual. These can
510 * change depending on the renderbuffer bindings. This function updates
511 * the given framebuffer's Visual from the current renderbuffer bindings.
512 *
513 * This may apply to user-created framebuffers or window system framebuffers.
514 *
515 * Also note: ctx->DrawBuffer->Visual.depthBits might not equal
516 * ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer.DepthBits.
517 * The former one is used to convert floating point depth values into
518 * integer Z values.
519 */
520 void
521 _mesa_update_framebuffer_visual(struct gl_context *ctx,
522 struct gl_framebuffer *fb)
523 {
524 GLuint i;
525
526 memset(&fb->Visual, 0, sizeof(fb->Visual));
527 fb->Visual.rgbMode = GL_TRUE; /* assume this */
528
529 #if 0 /* this _might_ be needed */
530 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
531 /* leave visual fields zero'd */
532 return;
533 }
534 #endif
535
536 /* find first RGB renderbuffer */
537 for (i = 0; i < BUFFER_COUNT; i++) {
538 if (fb->Attachment[i].Renderbuffer) {
539 const struct gl_renderbuffer *rb = fb->Attachment[i].Renderbuffer;
540 const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
541 const gl_format fmt = rb->Format;
542
543 if (_mesa_is_legal_color_format(ctx, baseFormat)) {
544 fb->Visual.redBits = _mesa_get_format_bits(fmt, GL_RED_BITS);
545 fb->Visual.greenBits = _mesa_get_format_bits(fmt, GL_GREEN_BITS);
546 fb->Visual.blueBits = _mesa_get_format_bits(fmt, GL_BLUE_BITS);
547 fb->Visual.alphaBits = _mesa_get_format_bits(fmt, GL_ALPHA_BITS);
548 fb->Visual.rgbBits = fb->Visual.redBits
549 + fb->Visual.greenBits + fb->Visual.blueBits;
550 fb->Visual.samples = rb->NumSamples;
551 if (_mesa_get_format_color_encoding(fmt) == GL_SRGB)
552 fb->Visual.sRGBCapable = ctx->Const.sRGBCapable;
553 break;
554 }
555 }
556 }
557
558 fb->Visual.floatMode = GL_FALSE;
559 for (i = 0; i < BUFFER_COUNT; i++) {
560 if (fb->Attachment[i].Renderbuffer) {
561 const struct gl_renderbuffer *rb = fb->Attachment[i].Renderbuffer;
562 const gl_format fmt = rb->Format;
563
564 if (_mesa_get_format_datatype(fmt) == GL_FLOAT) {
565 fb->Visual.floatMode = GL_TRUE;
566 break;
567 }
568 }
569 }
570
571 if (fb->Attachment[BUFFER_DEPTH].Renderbuffer) {
572 const struct gl_renderbuffer *rb =
573 fb->Attachment[BUFFER_DEPTH].Renderbuffer;
574 const gl_format fmt = rb->Format;
575 fb->Visual.haveDepthBuffer = GL_TRUE;
576 fb->Visual.depthBits = _mesa_get_format_bits(fmt, GL_DEPTH_BITS);
577 }
578
579 if (fb->Attachment[BUFFER_STENCIL].Renderbuffer) {
580 const struct gl_renderbuffer *rb =
581 fb->Attachment[BUFFER_STENCIL].Renderbuffer;
582 const gl_format fmt = rb->Format;
583 fb->Visual.haveStencilBuffer = GL_TRUE;
584 fb->Visual.stencilBits = _mesa_get_format_bits(fmt, GL_STENCIL_BITS);
585 }
586
587 if (fb->Attachment[BUFFER_ACCUM].Renderbuffer) {
588 const struct gl_renderbuffer *rb =
589 fb->Attachment[BUFFER_ACCUM].Renderbuffer;
590 const gl_format fmt = rb->Format;
591 fb->Visual.haveAccumBuffer = GL_TRUE;
592 fb->Visual.accumRedBits = _mesa_get_format_bits(fmt, GL_RED_BITS);
593 fb->Visual.accumGreenBits = _mesa_get_format_bits(fmt, GL_GREEN_BITS);
594 fb->Visual.accumBlueBits = _mesa_get_format_bits(fmt, GL_BLUE_BITS);
595 fb->Visual.accumAlphaBits = _mesa_get_format_bits(fmt, GL_ALPHA_BITS);
596 }
597
598 compute_depth_max(fb);
599 }
600
601
602 /**
603 * Update the framebuffer's _DepthBuffer field using the renderbuffer
604 * found at the given attachment index.
605 *
606 * If that attachment points to a combined GL_DEPTH_STENCIL renderbuffer,
607 * create and install a depth wrapper/adaptor.
608 *
609 * \param fb the framebuffer whose _DepthBuffer field to update
610 * \param attIndex indicates the renderbuffer to possibly wrap
611 */
612 void
613 _mesa_update_depth_buffer(struct gl_context *ctx,
614 struct gl_framebuffer *fb,
615 GLuint attIndex)
616 {
617 struct gl_renderbuffer *depthRb;
618
619 /* only one possiblity for now */
620 ASSERT(attIndex == BUFFER_DEPTH);
621
622 depthRb = fb->Attachment[attIndex].Renderbuffer;
623
624 if (depthRb && _mesa_is_format_packed_depth_stencil(depthRb->Format)) {
625 /* The attached depth buffer is a GL_DEPTH_STENCIL renderbuffer */
626 if (!fb->_DepthBuffer
627 || fb->_DepthBuffer->Wrapped != depthRb
628 || _mesa_get_format_base_format(fb->_DepthBuffer->Format) != GL_DEPTH_COMPONENT) {
629 /* need to update wrapper */
630 struct gl_renderbuffer *wrapper;
631
632 if (depthRb->Format == MESA_FORMAT_Z32_FLOAT_X24S8) {
633 wrapper = _mesa_new_z32f_renderbuffer_wrapper(ctx, depthRb);
634 }
635 else {
636 wrapper = _mesa_new_z24_renderbuffer_wrapper(ctx, depthRb);
637 }
638 _mesa_reference_renderbuffer(&fb->_DepthBuffer, wrapper);
639 ASSERT(fb->_DepthBuffer->Wrapped == depthRb);
640 }
641 }
642 else {
643 /* depthRb may be null */
644 _mesa_reference_renderbuffer(&fb->_DepthBuffer, depthRb);
645 }
646 }
647
648
649 /**
650 * Update the framebuffer's _StencilBuffer field using the renderbuffer
651 * found at the given attachment index.
652 *
653 * If that attachment points to a combined GL_DEPTH_STENCIL renderbuffer,
654 * create and install a stencil wrapper/adaptor.
655 *
656 * \param fb the framebuffer whose _StencilBuffer field to update
657 * \param attIndex indicates the renderbuffer to possibly wrap
658 */
659 void
660 _mesa_update_stencil_buffer(struct gl_context *ctx,
661 struct gl_framebuffer *fb,
662 GLuint attIndex)
663 {
664 struct gl_renderbuffer *stencilRb;
665
666 ASSERT(attIndex == BUFFER_DEPTH ||
667 attIndex == BUFFER_STENCIL);
668
669 stencilRb = fb->Attachment[attIndex].Renderbuffer;
670
671 if (stencilRb && _mesa_is_format_packed_depth_stencil(stencilRb->Format)) {
672 /* The attached stencil buffer is a GL_DEPTH_STENCIL renderbuffer */
673 if (!fb->_StencilBuffer
674 || fb->_StencilBuffer->Wrapped != stencilRb
675 || _mesa_get_format_base_format(fb->_StencilBuffer->Format) != GL_STENCIL_INDEX) {
676 /* need to update wrapper */
677 struct gl_renderbuffer *wrapper
678 = _mesa_new_s8_renderbuffer_wrapper(ctx, stencilRb);
679 _mesa_reference_renderbuffer(&fb->_StencilBuffer, wrapper);
680 ASSERT(fb->_StencilBuffer->Wrapped == stencilRb);
681 }
682 }
683 else {
684 /* stencilRb may be null */
685 _mesa_reference_renderbuffer(&fb->_StencilBuffer, stencilRb);
686 }
687 }
688
689
690 /*
691 * Example DrawBuffers scenarios:
692 *
693 * 1. glDrawBuffer(GL_FRONT_AND_BACK), fixed-func or shader writes to
694 * "gl_FragColor" or program writes to the "result.color" register:
695 *
696 * fragment color output renderbuffer
697 * --------------------- ---------------
698 * color[0] Front, Back
699 *
700 *
701 * 2. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]), shader writes to
702 * gl_FragData[i] or program writes to result.color[i] registers:
703 *
704 * fragment color output renderbuffer
705 * --------------------- ---------------
706 * color[0] Front
707 * color[1] Aux0
708 * color[3] Aux1
709 *
710 *
711 * 3. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]) and shader writes to
712 * gl_FragColor, or fixed function:
713 *
714 * fragment color output renderbuffer
715 * --------------------- ---------------
716 * color[0] Front, Aux0, Aux1
717 *
718 *
719 * In either case, the list of renderbuffers is stored in the
720 * framebuffer->_ColorDrawBuffers[] array and
721 * framebuffer->_NumColorDrawBuffers indicates the number of buffers.
722 * The renderer (like swrast) has to look at the current fragment shader
723 * to see if it writes to gl_FragColor vs. gl_FragData[i] to determine
724 * how to map color outputs to renderbuffers.
725 *
726 * Note that these two calls are equivalent (for fixed function fragment
727 * shading anyway):
728 * a) glDrawBuffer(GL_FRONT_AND_BACK); (assuming non-stereo framebuffer)
729 * b) glDrawBuffers(2, [GL_FRONT_LEFT, GL_BACK_LEFT]);
730 */
731
732
733
734
735 /**
736 * Update the (derived) list of color drawing renderbuffer pointers.
737 * Later, when we're rendering we'll loop from 0 to _NumColorDrawBuffers
738 * writing colors.
739 */
740 static void
741 update_color_draw_buffers(struct gl_context *ctx, struct gl_framebuffer *fb)
742 {
743 GLuint output;
744
745 /* set 0th buffer to NULL now in case _NumColorDrawBuffers is zero */
746 fb->_ColorDrawBuffers[0] = NULL;
747
748 for (output = 0; output < fb->_NumColorDrawBuffers; output++) {
749 GLint buf = fb->_ColorDrawBufferIndexes[output];
750 if (buf >= 0) {
751 fb->_ColorDrawBuffers[output] = fb->Attachment[buf].Renderbuffer;
752 }
753 else {
754 fb->_ColorDrawBuffers[output] = NULL;
755 }
756 }
757 }
758
759
760 /**
761 * Update the (derived) color read renderbuffer pointer.
762 * Unlike the DrawBuffer, we can only read from one (or zero) color buffers.
763 */
764 static void
765 update_color_read_buffer(struct gl_context *ctx, struct gl_framebuffer *fb)
766 {
767 (void) ctx;
768 if (fb->_ColorReadBufferIndex == -1 ||
769 fb->DeletePending ||
770 fb->Width == 0 ||
771 fb->Height == 0) {
772 fb->_ColorReadBuffer = NULL; /* legal! */
773 }
774 else {
775 ASSERT(fb->_ColorReadBufferIndex >= 0);
776 ASSERT(fb->_ColorReadBufferIndex < BUFFER_COUNT);
777 fb->_ColorReadBuffer
778 = fb->Attachment[fb->_ColorReadBufferIndex].Renderbuffer;
779 }
780 }
781
782
783 /**
784 * Update a gl_framebuffer's derived state.
785 *
786 * Specifically, update these framebuffer fields:
787 * _ColorDrawBuffers
788 * _NumColorDrawBuffers
789 * _ColorReadBuffer
790 * _DepthBuffer
791 * _StencilBuffer
792 *
793 * If the framebuffer is user-created, make sure it's complete.
794 *
795 * The following functions (at least) can effect framebuffer state:
796 * glReadBuffer, glDrawBuffer, glDrawBuffersARB, glFramebufferRenderbufferEXT,
797 * glRenderbufferStorageEXT.
798 */
799 static void
800 update_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
801 {
802 if (fb->Name == 0) {
803 /* This is a window-system framebuffer */
804 /* Need to update the FB's GL_DRAW_BUFFER state to match the
805 * context state (GL_READ_BUFFER too).
806 */
807 if (fb->ColorDrawBuffer[0] != ctx->Color.DrawBuffer[0]) {
808 _mesa_drawbuffers(ctx, ctx->Const.MaxDrawBuffers,
809 ctx->Color.DrawBuffer, NULL);
810 }
811 }
812 else {
813 /* This is a user-created framebuffer.
814 * Completeness only matters for user-created framebuffers.
815 */
816 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE) {
817 _mesa_test_framebuffer_completeness(ctx, fb);
818 }
819 }
820
821 /* Strictly speaking, we don't need to update the draw-state
822 * if this FB is bound as ctx->ReadBuffer (and conversely, the
823 * read-state if this FB is bound as ctx->DrawBuffer), but no
824 * harm.
825 */
826 update_color_draw_buffers(ctx, fb);
827 update_color_read_buffer(ctx, fb);
828 _mesa_update_depth_buffer(ctx, fb, BUFFER_DEPTH);
829 _mesa_update_stencil_buffer(ctx, fb, BUFFER_STENCIL);
830
831 compute_depth_max(fb);
832 }
833
834
835 /**
836 * Update state related to the current draw/read framebuffers.
837 */
838 void
839 _mesa_update_framebuffer(struct gl_context *ctx)
840 {
841 struct gl_framebuffer *drawFb;
842 struct gl_framebuffer *readFb;
843
844 assert(ctx);
845 drawFb = ctx->DrawBuffer;
846 readFb = ctx->ReadBuffer;
847
848 update_framebuffer(ctx, drawFb);
849 if (readFb != drawFb)
850 update_framebuffer(ctx, readFb);
851 }
852
853
854 /**
855 * Check if the renderbuffer for a read operation (glReadPixels, glCopyPixels,
856 * glCopyTex[Sub]Image, etc) exists.
857 * \param format a basic image format such as GL_RGB, GL_RGBA, GL_ALPHA,
858 * GL_DEPTH_COMPONENT, etc. or GL_COLOR, GL_DEPTH, GL_STENCIL.
859 * \return GL_TRUE if buffer exists, GL_FALSE otherwise
860 */
861 GLboolean
862 _mesa_source_buffer_exists(struct gl_context *ctx, GLenum format)
863 {
864 const struct gl_renderbuffer_attachment *att = ctx->ReadBuffer->Attachment;
865
866 /* If we don't know the framebuffer status, update it now */
867 if (ctx->ReadBuffer->_Status == 0) {
868 _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
869 }
870
871 if (ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
872 return GL_FALSE;
873 }
874
875 switch (format) {
876 case GL_COLOR:
877 case GL_RED:
878 case GL_GREEN:
879 case GL_BLUE:
880 case GL_ALPHA:
881 case GL_LUMINANCE:
882 case GL_LUMINANCE_ALPHA:
883 case GL_INTENSITY:
884 case GL_RG:
885 case GL_RGB:
886 case GL_BGR:
887 case GL_RGBA:
888 case GL_BGRA:
889 case GL_ABGR_EXT:
890 case GL_COLOR_INDEX:
891 case GL_RED_INTEGER_EXT:
892 case GL_GREEN_INTEGER_EXT:
893 case GL_BLUE_INTEGER_EXT:
894 case GL_ALPHA_INTEGER_EXT:
895 case GL_RGB_INTEGER_EXT:
896 case GL_RGBA_INTEGER_EXT:
897 case GL_BGR_INTEGER_EXT:
898 case GL_BGRA_INTEGER_EXT:
899 case GL_LUMINANCE_INTEGER_EXT:
900 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
901 if (ctx->ReadBuffer->_ColorReadBuffer == NULL) {
902 return GL_FALSE;
903 }
904 ASSERT(_mesa_get_format_bits(ctx->ReadBuffer->_ColorReadBuffer->Format, GL_RED_BITS) > 0 ||
905 _mesa_get_format_bits(ctx->ReadBuffer->_ColorReadBuffer->Format, GL_ALPHA_BITS) > 0 ||
906 _mesa_get_format_bits(ctx->ReadBuffer->_ColorReadBuffer->Format, GL_TEXTURE_LUMINANCE_SIZE) > 0 ||
907 _mesa_get_format_bits(ctx->ReadBuffer->_ColorReadBuffer->Format, GL_TEXTURE_INTENSITY_SIZE) > 0 ||
908 _mesa_get_format_bits(ctx->ReadBuffer->_ColorReadBuffer->Format, GL_INDEX_BITS) > 0);
909 break;
910 case GL_DEPTH:
911 case GL_DEPTH_COMPONENT:
912 if (!att[BUFFER_DEPTH].Renderbuffer) {
913 return GL_FALSE;
914 }
915 /*ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0);*/
916 break;
917 case GL_STENCIL:
918 case GL_STENCIL_INDEX:
919 if (!att[BUFFER_STENCIL].Renderbuffer) {
920 return GL_FALSE;
921 }
922 /*ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0);*/
923 break;
924 case GL_DEPTH_STENCIL_EXT:
925 if (!att[BUFFER_DEPTH].Renderbuffer ||
926 !att[BUFFER_STENCIL].Renderbuffer) {
927 return GL_FALSE;
928 }
929 /*
930 ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0);
931 ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0);
932 */
933 break;
934 default:
935 _mesa_problem(ctx,
936 "Unexpected format 0x%x in _mesa_source_buffer_exists",
937 format);
938 return GL_FALSE;
939 }
940
941 /* OK */
942 return GL_TRUE;
943 }
944
945
946 /**
947 * As above, but for drawing operations.
948 * XXX could do some code merging w/ above function.
949 */
950 GLboolean
951 _mesa_dest_buffer_exists(struct gl_context *ctx, GLenum format)
952 {
953 const struct gl_renderbuffer_attachment *att = ctx->DrawBuffer->Attachment;
954
955 /* If we don't know the framebuffer status, update it now */
956 if (ctx->DrawBuffer->_Status == 0) {
957 _mesa_test_framebuffer_completeness(ctx, ctx->DrawBuffer);
958 }
959
960 if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
961 return GL_FALSE;
962 }
963
964 switch (format) {
965 case GL_COLOR:
966 case GL_RED:
967 case GL_GREEN:
968 case GL_BLUE:
969 case GL_ALPHA:
970 case GL_LUMINANCE:
971 case GL_LUMINANCE_ALPHA:
972 case GL_INTENSITY:
973 case GL_RGB:
974 case GL_BGR:
975 case GL_RGBA:
976 case GL_BGRA:
977 case GL_ABGR_EXT:
978 case GL_COLOR_INDEX:
979 case GL_RED_INTEGER_EXT:
980 case GL_GREEN_INTEGER_EXT:
981 case GL_BLUE_INTEGER_EXT:
982 case GL_ALPHA_INTEGER_EXT:
983 case GL_RGB_INTEGER_EXT:
984 case GL_RGBA_INTEGER_EXT:
985 case GL_BGR_INTEGER_EXT:
986 case GL_BGRA_INTEGER_EXT:
987 case GL_LUMINANCE_INTEGER_EXT:
988 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
989 /* Nothing special since GL_DRAW_BUFFER could be GL_NONE. */
990 /* Could assert that colorbuffer has RedBits > 0 */
991 break;
992 case GL_DEPTH:
993 case GL_DEPTH_COMPONENT:
994 if (!att[BUFFER_DEPTH].Renderbuffer) {
995 return GL_FALSE;
996 }
997 /*ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0);*/
998 break;
999 case GL_STENCIL:
1000 case GL_STENCIL_INDEX:
1001 if (!att[BUFFER_STENCIL].Renderbuffer) {
1002 return GL_FALSE;
1003 }
1004 /*ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0);*/
1005 break;
1006 case GL_DEPTH_STENCIL_EXT:
1007 if (!att[BUFFER_DEPTH].Renderbuffer ||
1008 !att[BUFFER_STENCIL].Renderbuffer) {
1009 return GL_FALSE;
1010 }
1011 /*
1012 ASSERT(att[BUFFER_DEPTH].Renderbuffer->DepthBits > 0);
1013 ASSERT(att[BUFFER_STENCIL].Renderbuffer->StencilBits > 0);
1014 */
1015 break;
1016 default:
1017 _mesa_problem(ctx,
1018 "Unexpected format 0x%x in _mesa_dest_buffer_exists",
1019 format);
1020 return GL_FALSE;
1021 }
1022
1023 /* OK */
1024 return GL_TRUE;
1025 }
1026
1027
1028 /**
1029 * Used to answer the GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES query.
1030 */
1031 GLenum
1032 _mesa_get_color_read_format(struct gl_context *ctx)
1033 {
1034 switch (ctx->ReadBuffer->_ColorReadBuffer->Format) {
1035 case MESA_FORMAT_ARGB8888:
1036 return GL_BGRA;
1037 case MESA_FORMAT_RGB565:
1038 return GL_BGR;
1039 default:
1040 return GL_RGBA;
1041 }
1042 }
1043
1044
1045 /**
1046 * Used to answer the GL_IMPLEMENTATION_COLOR_READ_TYPE_OES query.
1047 */
1048 GLenum
1049 _mesa_get_color_read_type(struct gl_context *ctx)
1050 {
1051 switch (ctx->ReadBuffer->_ColorReadBuffer->Format) {
1052 case MESA_FORMAT_ARGB8888:
1053 return GL_UNSIGNED_BYTE;
1054 case MESA_FORMAT_RGB565:
1055 return GL_UNSIGNED_SHORT_5_6_5_REV;
1056 default:
1057 return GL_UNSIGNED_BYTE;
1058 }
1059 }
1060
1061
1062 /**
1063 * Print framebuffer info to stderr, for debugging.
1064 */
1065 void
1066 _mesa_print_framebuffer(const struct gl_framebuffer *fb)
1067 {
1068 GLuint i;
1069
1070 fprintf(stderr, "Mesa Framebuffer %u at %p\n", fb->Name, (void *) fb);
1071 fprintf(stderr, " Size: %u x %u Status: %s\n", fb->Width, fb->Height,
1072 _mesa_lookup_enum_by_nr(fb->_Status));
1073 fprintf(stderr, " Attachments:\n");
1074
1075 for (i = 0; i < BUFFER_COUNT; i++) {
1076 const struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
1077 if (att->Type == GL_TEXTURE) {
1078 const struct gl_texture_image *texImage =
1079 _mesa_get_attachment_teximage_const(att);
1080 fprintf(stderr,
1081 " %2d: Texture %u, level %u, face %u, slice %u, complete %d\n",
1082 i, att->Texture->Name, att->TextureLevel, att->CubeMapFace,
1083 att->Zoffset, att->Complete);
1084 fprintf(stderr, " Size: %u x %u x %u Format %s\n",
1085 texImage->Width, texImage->Height, texImage->Depth,
1086 _mesa_get_format_name(texImage->TexFormat));
1087 }
1088 else if (att->Type == GL_RENDERBUFFER) {
1089 fprintf(stderr, " %2d: Renderbuffer %u, complete %d\n",
1090 i, att->Renderbuffer->Name, att->Complete);
1091 fprintf(stderr, " Size: %u x %u Format %s\n",
1092 att->Renderbuffer->Width, att->Renderbuffer->Height,
1093 _mesa_get_format_name(att->Renderbuffer->Format));
1094 }
1095 else {
1096 fprintf(stderr, " %2d: none\n", i);
1097 }
1098 }
1099 }