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