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