mesa: refactor clamping controls, get rid of _ClampReadColor
[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 #include "glformats.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 fb->_AllColorBuffersFixedPoint = !visual->floatMode;
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
229
230 /**
231 * Set *ptr to point to fb, with refcounting and locking.
232 * This is normally only called from the _mesa_reference_framebuffer() macro
233 * when there's a real pointer change.
234 */
235 void
236 _mesa_reference_framebuffer_(struct gl_framebuffer **ptr,
237 struct gl_framebuffer *fb)
238 {
239 if (*ptr) {
240 /* unreference old renderbuffer */
241 GLboolean deleteFlag = GL_FALSE;
242 struct gl_framebuffer *oldFb = *ptr;
243
244 _glthread_LOCK_MUTEX(oldFb->Mutex);
245 ASSERT(oldFb->RefCount > 0);
246 oldFb->RefCount--;
247 deleteFlag = (oldFb->RefCount == 0);
248 _glthread_UNLOCK_MUTEX(oldFb->Mutex);
249
250 if (deleteFlag)
251 oldFb->Delete(oldFb);
252
253 *ptr = NULL;
254 }
255 assert(!*ptr);
256
257 if (fb) {
258 _glthread_LOCK_MUTEX(fb->Mutex);
259 fb->RefCount++;
260 _glthread_UNLOCK_MUTEX(fb->Mutex);
261 *ptr = fb;
262 }
263 }
264
265
266 /**
267 * Resize the given framebuffer's renderbuffers to the new width and height.
268 * This should only be used for window-system framebuffers, not
269 * user-created renderbuffers (i.e. made with GL_EXT_framebuffer_object).
270 * This will typically be called via ctx->Driver.ResizeBuffers() or directly
271 * from a device driver.
272 *
273 * \note it's possible for ctx to be null since a window can be resized
274 * without a currently bound rendering context.
275 */
276 void
277 _mesa_resize_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
278 GLuint width, GLuint height)
279 {
280 GLuint i;
281
282 /* XXX I think we could check if the size is not changing
283 * and return early.
284 */
285
286 /* Can only resize win-sys framebuffer objects */
287 assert(_mesa_is_winsys_fbo(fb));
288
289 for (i = 0; i < BUFFER_COUNT; i++) {
290 struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
291 if (att->Type == GL_RENDERBUFFER_EXT && att->Renderbuffer) {
292 struct gl_renderbuffer *rb = att->Renderbuffer;
293 /* only resize if size is changing */
294 if (rb->Width != width || rb->Height != height) {
295 if (rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height)) {
296 ASSERT(rb->Width == width);
297 ASSERT(rb->Height == height);
298 }
299 else {
300 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Resizing framebuffer");
301 /* no return */
302 }
303 }
304 }
305 }
306
307 fb->Width = width;
308 fb->Height = height;
309
310 if (ctx) {
311 /* update scissor / window bounds */
312 _mesa_update_draw_buffer_bounds(ctx);
313 /* Signal new buffer state so that swrast will update its clipping
314 * info (the CLIP_BIT flag).
315 */
316 ctx->NewState |= _NEW_BUFFERS;
317 }
318 }
319
320
321
322 /**
323 * XXX THIS IS OBSOLETE - drivers should take care of detecting window
324 * size changes and act accordingly, likely calling _mesa_resize_framebuffer().
325 *
326 * GL_MESA_resize_buffers extension.
327 *
328 * When this function is called, we'll ask the window system how large
329 * the current window is. If it's a new size, we'll call the driver's
330 * ResizeBuffers function. The driver will then resize its color buffers
331 * as needed, and maybe call the swrast's routine for reallocating
332 * swrast-managed depth/stencil/accum/etc buffers.
333 * \note This function should only be called through the GL API, not
334 * from device drivers (as was done in the past).
335 */
336 void
337 _mesa_resizebuffers( struct gl_context *ctx )
338 {
339 FLUSH_VERTICES(ctx, 0);
340
341 if (MESA_VERBOSE & VERBOSE_API)
342 _mesa_debug(ctx, "glResizeBuffersMESA\n");
343
344 if (!ctx->Driver.GetBufferSize) {
345 return;
346 }
347
348 if (ctx->WinSysDrawBuffer) {
349 GLuint newWidth, newHeight;
350 struct gl_framebuffer *buffer = ctx->WinSysDrawBuffer;
351
352 assert(_mesa_is_winsys_fbo(buffer));
353
354 /* ask device driver for size of output buffer */
355 ctx->Driver.GetBufferSize( buffer, &newWidth, &newHeight );
356
357 /* see if size of device driver's color buffer (window) has changed */
358 if (buffer->Width != newWidth || buffer->Height != newHeight) {
359 if (ctx->Driver.ResizeBuffers)
360 ctx->Driver.ResizeBuffers(ctx, buffer, newWidth, newHeight );
361 }
362 }
363
364 if (ctx->WinSysReadBuffer
365 && ctx->WinSysReadBuffer != ctx->WinSysDrawBuffer) {
366 GLuint newWidth, newHeight;
367 struct gl_framebuffer *buffer = ctx->WinSysReadBuffer;
368
369 assert(_mesa_is_winsys_fbo(buffer));
370
371 /* ask device driver for size of read buffer */
372 ctx->Driver.GetBufferSize( buffer, &newWidth, &newHeight );
373
374 /* see if size of device driver's color buffer (window) has changed */
375 if (buffer->Width != newWidth || buffer->Height != newHeight) {
376 if (ctx->Driver.ResizeBuffers)
377 ctx->Driver.ResizeBuffers(ctx, buffer, newWidth, newHeight );
378 }
379 }
380
381 ctx->NewState |= _NEW_BUFFERS; /* to update scissor / window bounds */
382 }
383
384
385 /*
386 * XXX THIS IS OBSOLETE
387 */
388 void GLAPIENTRY
389 _mesa_ResizeBuffersMESA( void )
390 {
391 GET_CURRENT_CONTEXT(ctx);
392
393 if (ctx->Extensions.MESA_resize_buffers)
394 _mesa_resizebuffers( ctx );
395 }
396
397
398
399 /**
400 * Examine all the framebuffer's renderbuffers to update the Width/Height
401 * fields of the framebuffer. If we have renderbuffers with different
402 * sizes, set the framebuffer's width and height to the min size.
403 * Note: this is only intended for user-created framebuffers, not
404 * window-system framebuffes.
405 */
406 static void
407 update_framebuffer_size(struct gl_context *ctx, struct gl_framebuffer *fb)
408 {
409 GLuint minWidth = ~0, minHeight = ~0;
410 GLuint i;
411
412 /* user-created framebuffers only */
413 assert(_mesa_is_user_fbo(fb));
414
415 for (i = 0; i < BUFFER_COUNT; i++) {
416 struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
417 const struct gl_renderbuffer *rb = att->Renderbuffer;
418 if (rb) {
419 minWidth = MIN2(minWidth, rb->Width);
420 minHeight = MIN2(minHeight, rb->Height);
421 }
422 }
423
424 if (minWidth != ~0) {
425 fb->Width = minWidth;
426 fb->Height = minHeight;
427 }
428 else {
429 fb->Width = 0;
430 fb->Height = 0;
431 }
432 }
433
434
435 /**
436 * Update the context's current drawing buffer's Xmin, Xmax, Ymin, Ymax fields.
437 * These values are computed from the buffer's width and height and
438 * the scissor box, if it's enabled.
439 * \param ctx the GL context.
440 */
441 void
442 _mesa_update_draw_buffer_bounds(struct gl_context *ctx)
443 {
444 struct gl_framebuffer *buffer = ctx->DrawBuffer;
445
446 if (!buffer)
447 return;
448
449 if (_mesa_is_user_fbo(buffer)) {
450 /* user-created framebuffer size depends on the renderbuffers */
451 update_framebuffer_size(ctx, buffer);
452 }
453
454 buffer->_Xmin = 0;
455 buffer->_Ymin = 0;
456 buffer->_Xmax = buffer->Width;
457 buffer->_Ymax = buffer->Height;
458
459 if (ctx->Scissor.Enabled) {
460 if (ctx->Scissor.X > buffer->_Xmin) {
461 buffer->_Xmin = ctx->Scissor.X;
462 }
463 if (ctx->Scissor.Y > buffer->_Ymin) {
464 buffer->_Ymin = ctx->Scissor.Y;
465 }
466 if (ctx->Scissor.X + ctx->Scissor.Width < buffer->_Xmax) {
467 buffer->_Xmax = ctx->Scissor.X + ctx->Scissor.Width;
468 }
469 if (ctx->Scissor.Y + ctx->Scissor.Height < buffer->_Ymax) {
470 buffer->_Ymax = ctx->Scissor.Y + ctx->Scissor.Height;
471 }
472 /* finally, check for empty region */
473 if (buffer->_Xmin > buffer->_Xmax) {
474 buffer->_Xmin = buffer->_Xmax;
475 }
476 if (buffer->_Ymin > buffer->_Ymax) {
477 buffer->_Ymin = buffer->_Ymax;
478 }
479 }
480
481 ASSERT(buffer->_Xmin <= buffer->_Xmax);
482 ASSERT(buffer->_Ymin <= buffer->_Ymax);
483 }
484
485
486 /**
487 * The glGet queries of the framebuffer red/green/blue size, stencil size,
488 * etc. are satisfied by the fields of ctx->DrawBuffer->Visual. These can
489 * change depending on the renderbuffer bindings. This function updates
490 * the given framebuffer's Visual from the current renderbuffer bindings.
491 *
492 * This may apply to user-created framebuffers or window system framebuffers.
493 *
494 * Also note: ctx->DrawBuffer->Visual.depthBits might not equal
495 * ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer.DepthBits.
496 * The former one is used to convert floating point depth values into
497 * integer Z values.
498 */
499 void
500 _mesa_update_framebuffer_visual(struct gl_context *ctx,
501 struct gl_framebuffer *fb)
502 {
503 GLuint i;
504
505 memset(&fb->Visual, 0, sizeof(fb->Visual));
506 fb->Visual.rgbMode = GL_TRUE; /* assume this */
507
508 #if 0 /* this _might_ be needed */
509 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
510 /* leave visual fields zero'd */
511 return;
512 }
513 #endif
514
515 /* find first RGB renderbuffer */
516 for (i = 0; i < BUFFER_COUNT; i++) {
517 if (fb->Attachment[i].Renderbuffer) {
518 const struct gl_renderbuffer *rb = fb->Attachment[i].Renderbuffer;
519 const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
520 const gl_format fmt = rb->Format;
521
522 /* Grab samples and sampleBuffers from any attachment point (assuming
523 * the framebuffer is complete, we'll get the same answer from all
524 * attachments).
525 */
526 fb->Visual.samples = rb->NumSamples;
527 fb->Visual.sampleBuffers = rb->NumSamples > 0 ? 1 : 0;
528
529 if (_mesa_is_legal_color_format(ctx, baseFormat)) {
530 fb->Visual.redBits = _mesa_get_format_bits(fmt, GL_RED_BITS);
531 fb->Visual.greenBits = _mesa_get_format_bits(fmt, GL_GREEN_BITS);
532 fb->Visual.blueBits = _mesa_get_format_bits(fmt, GL_BLUE_BITS);
533 fb->Visual.alphaBits = _mesa_get_format_bits(fmt, GL_ALPHA_BITS);
534 fb->Visual.rgbBits = fb->Visual.redBits
535 + fb->Visual.greenBits + fb->Visual.blueBits;
536 if (_mesa_get_format_color_encoding(fmt) == GL_SRGB)
537 fb->Visual.sRGBCapable = ctx->Extensions.EXT_framebuffer_sRGB;
538 break;
539 }
540 }
541 }
542
543 fb->Visual.floatMode = GL_FALSE;
544 for (i = 0; i < BUFFER_COUNT; i++) {
545 if (fb->Attachment[i].Renderbuffer) {
546 const struct gl_renderbuffer *rb = fb->Attachment[i].Renderbuffer;
547 const gl_format fmt = rb->Format;
548
549 if (_mesa_get_format_datatype(fmt) == GL_FLOAT) {
550 fb->Visual.floatMode = GL_TRUE;
551 break;
552 }
553 }
554 }
555
556 if (fb->Attachment[BUFFER_DEPTH].Renderbuffer) {
557 const struct gl_renderbuffer *rb =
558 fb->Attachment[BUFFER_DEPTH].Renderbuffer;
559 const gl_format fmt = rb->Format;
560 fb->Visual.haveDepthBuffer = GL_TRUE;
561 fb->Visual.depthBits = _mesa_get_format_bits(fmt, GL_DEPTH_BITS);
562 }
563
564 if (fb->Attachment[BUFFER_STENCIL].Renderbuffer) {
565 const struct gl_renderbuffer *rb =
566 fb->Attachment[BUFFER_STENCIL].Renderbuffer;
567 const gl_format fmt = rb->Format;
568 fb->Visual.haveStencilBuffer = GL_TRUE;
569 fb->Visual.stencilBits = _mesa_get_format_bits(fmt, GL_STENCIL_BITS);
570 }
571
572 if (fb->Attachment[BUFFER_ACCUM].Renderbuffer) {
573 const struct gl_renderbuffer *rb =
574 fb->Attachment[BUFFER_ACCUM].Renderbuffer;
575 const gl_format fmt = rb->Format;
576 fb->Visual.haveAccumBuffer = GL_TRUE;
577 fb->Visual.accumRedBits = _mesa_get_format_bits(fmt, GL_RED_BITS);
578 fb->Visual.accumGreenBits = _mesa_get_format_bits(fmt, GL_GREEN_BITS);
579 fb->Visual.accumBlueBits = _mesa_get_format_bits(fmt, GL_BLUE_BITS);
580 fb->Visual.accumAlphaBits = _mesa_get_format_bits(fmt, GL_ALPHA_BITS);
581 }
582
583 compute_depth_max(fb);
584 }
585
586
587 /*
588 * Example DrawBuffers scenarios:
589 *
590 * 1. glDrawBuffer(GL_FRONT_AND_BACK), fixed-func or shader writes to
591 * "gl_FragColor" or program writes to the "result.color" register:
592 *
593 * fragment color output renderbuffer
594 * --------------------- ---------------
595 * color[0] Front, Back
596 *
597 *
598 * 2. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]), shader writes to
599 * gl_FragData[i] or program writes to result.color[i] registers:
600 *
601 * fragment color output renderbuffer
602 * --------------------- ---------------
603 * color[0] Front
604 * color[1] Aux0
605 * color[3] Aux1
606 *
607 *
608 * 3. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]) and shader writes to
609 * gl_FragColor, or fixed function:
610 *
611 * fragment color output renderbuffer
612 * --------------------- ---------------
613 * color[0] Front, Aux0, Aux1
614 *
615 *
616 * In either case, the list of renderbuffers is stored in the
617 * framebuffer->_ColorDrawBuffers[] array and
618 * framebuffer->_NumColorDrawBuffers indicates the number of buffers.
619 * The renderer (like swrast) has to look at the current fragment shader
620 * to see if it writes to gl_FragColor vs. gl_FragData[i] to determine
621 * how to map color outputs to renderbuffers.
622 *
623 * Note that these two calls are equivalent (for fixed function fragment
624 * shading anyway):
625 * a) glDrawBuffer(GL_FRONT_AND_BACK); (assuming non-stereo framebuffer)
626 * b) glDrawBuffers(2, [GL_FRONT_LEFT, GL_BACK_LEFT]);
627 */
628
629
630
631
632 /**
633 * Update the (derived) list of color drawing renderbuffer pointers.
634 * Later, when we're rendering we'll loop from 0 to _NumColorDrawBuffers
635 * writing colors.
636 */
637 static void
638 update_color_draw_buffers(struct gl_context *ctx, struct gl_framebuffer *fb)
639 {
640 GLuint output;
641
642 /* set 0th buffer to NULL now in case _NumColorDrawBuffers is zero */
643 fb->_ColorDrawBuffers[0] = NULL;
644
645 for (output = 0; output < fb->_NumColorDrawBuffers; output++) {
646 GLint buf = fb->_ColorDrawBufferIndexes[output];
647 if (buf >= 0) {
648 fb->_ColorDrawBuffers[output] = fb->Attachment[buf].Renderbuffer;
649 }
650 else {
651 fb->_ColorDrawBuffers[output] = NULL;
652 }
653 }
654 }
655
656
657 /**
658 * Update the (derived) color read renderbuffer pointer.
659 * Unlike the DrawBuffer, we can only read from one (or zero) color buffers.
660 */
661 static void
662 update_color_read_buffer(struct gl_context *ctx, struct gl_framebuffer *fb)
663 {
664 (void) ctx;
665 if (fb->_ColorReadBufferIndex == -1 ||
666 fb->DeletePending ||
667 fb->Width == 0 ||
668 fb->Height == 0) {
669 fb->_ColorReadBuffer = NULL; /* legal! */
670 }
671 else {
672 ASSERT(fb->_ColorReadBufferIndex >= 0);
673 ASSERT(fb->_ColorReadBufferIndex < BUFFER_COUNT);
674 fb->_ColorReadBuffer
675 = fb->Attachment[fb->_ColorReadBufferIndex].Renderbuffer;
676 }
677 }
678
679
680 /**
681 * Update a gl_framebuffer's derived state.
682 *
683 * Specifically, update these framebuffer fields:
684 * _ColorDrawBuffers
685 * _NumColorDrawBuffers
686 * _ColorReadBuffer
687 *
688 * If the framebuffer is user-created, make sure it's complete.
689 *
690 * The following functions (at least) can effect framebuffer state:
691 * glReadBuffer, glDrawBuffer, glDrawBuffersARB, glFramebufferRenderbufferEXT,
692 * glRenderbufferStorageEXT.
693 */
694 static void
695 update_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
696 {
697 if (_mesa_is_winsys_fbo(fb)) {
698 /* This is a window-system framebuffer */
699 /* Need to update the FB's GL_DRAW_BUFFER state to match the
700 * context state (GL_READ_BUFFER too).
701 */
702 if (fb->ColorDrawBuffer[0] != ctx->Color.DrawBuffer[0]) {
703 _mesa_drawbuffers(ctx, ctx->Const.MaxDrawBuffers,
704 ctx->Color.DrawBuffer, NULL);
705 }
706 }
707 else {
708 /* This is a user-created framebuffer.
709 * Completeness only matters for user-created framebuffers.
710 */
711 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE) {
712 _mesa_test_framebuffer_completeness(ctx, fb);
713 }
714 }
715
716 /* Strictly speaking, we don't need to update the draw-state
717 * if this FB is bound as ctx->ReadBuffer (and conversely, the
718 * read-state if this FB is bound as ctx->DrawBuffer), but no
719 * harm.
720 */
721 update_color_draw_buffers(ctx, fb);
722 update_color_read_buffer(ctx, fb);
723
724 compute_depth_max(fb);
725 }
726
727
728 /**
729 * Update state related to the current draw/read framebuffers.
730 */
731 void
732 _mesa_update_framebuffer(struct gl_context *ctx)
733 {
734 struct gl_framebuffer *drawFb;
735 struct gl_framebuffer *readFb;
736
737 assert(ctx);
738 drawFb = ctx->DrawBuffer;
739 readFb = ctx->ReadBuffer;
740
741 update_framebuffer(ctx, drawFb);
742 if (readFb != drawFb)
743 update_framebuffer(ctx, readFb);
744 }
745
746
747 /**
748 * Check if the renderbuffer for a read/draw operation exists.
749 * \param format a basic image format such as GL_RGB, GL_RGBA, GL_ALPHA,
750 * GL_DEPTH_COMPONENT, etc. or GL_COLOR, GL_DEPTH, GL_STENCIL.
751 * \param reading if TRUE, we're going to read from the buffer,
752 if FALSE, we're going to write to the buffer.
753 * \return GL_TRUE if buffer exists, GL_FALSE otherwise
754 */
755 static GLboolean
756 renderbuffer_exists(struct gl_context *ctx,
757 struct gl_framebuffer *fb,
758 GLenum format,
759 GLboolean reading)
760 {
761 const struct gl_renderbuffer_attachment *att = fb->Attachment;
762
763 /* If we don't know the framebuffer status, update it now */
764 if (fb->_Status == 0) {
765 _mesa_test_framebuffer_completeness(ctx, fb);
766 }
767
768 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
769 return GL_FALSE;
770 }
771
772 switch (format) {
773 case GL_COLOR:
774 case GL_RED:
775 case GL_GREEN:
776 case GL_BLUE:
777 case GL_ALPHA:
778 case GL_LUMINANCE:
779 case GL_LUMINANCE_ALPHA:
780 case GL_INTENSITY:
781 case GL_RG:
782 case GL_RGB:
783 case GL_BGR:
784 case GL_RGBA:
785 case GL_BGRA:
786 case GL_ABGR_EXT:
787 case GL_RED_INTEGER_EXT:
788 case GL_RG_INTEGER:
789 case GL_GREEN_INTEGER_EXT:
790 case GL_BLUE_INTEGER_EXT:
791 case GL_ALPHA_INTEGER_EXT:
792 case GL_RGB_INTEGER_EXT:
793 case GL_RGBA_INTEGER_EXT:
794 case GL_BGR_INTEGER_EXT:
795 case GL_BGRA_INTEGER_EXT:
796 case GL_LUMINANCE_INTEGER_EXT:
797 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
798 if (reading) {
799 /* about to read from a color buffer */
800 const struct gl_renderbuffer *readBuf = fb->_ColorReadBuffer;
801 if (!readBuf) {
802 return GL_FALSE;
803 }
804 ASSERT(_mesa_get_format_bits(readBuf->Format, GL_RED_BITS) > 0 ||
805 _mesa_get_format_bits(readBuf->Format, GL_ALPHA_BITS) > 0 ||
806 _mesa_get_format_bits(readBuf->Format, GL_TEXTURE_LUMINANCE_SIZE) > 0 ||
807 _mesa_get_format_bits(readBuf->Format, GL_TEXTURE_INTENSITY_SIZE) > 0 ||
808 _mesa_get_format_bits(readBuf->Format, GL_INDEX_BITS) > 0);
809 }
810 else {
811 /* about to draw to zero or more color buffers (none is OK) */
812 return GL_TRUE;
813 }
814 break;
815 case GL_DEPTH:
816 case GL_DEPTH_COMPONENT:
817 if (att[BUFFER_DEPTH].Type == GL_NONE) {
818 return GL_FALSE;
819 }
820 break;
821 case GL_STENCIL:
822 case GL_STENCIL_INDEX:
823 if (att[BUFFER_STENCIL].Type == GL_NONE) {
824 return GL_FALSE;
825 }
826 break;
827 case GL_DEPTH_STENCIL_EXT:
828 if (att[BUFFER_DEPTH].Type == GL_NONE ||
829 att[BUFFER_STENCIL].Type == GL_NONE) {
830 return GL_FALSE;
831 }
832 break;
833 default:
834 _mesa_problem(ctx,
835 "Unexpected format 0x%x in renderbuffer_exists",
836 format);
837 return GL_FALSE;
838 }
839
840 /* OK */
841 return GL_TRUE;
842 }
843
844
845 /**
846 * Check if the renderbuffer for a read operation (glReadPixels, glCopyPixels,
847 * glCopyTex[Sub]Image, etc) exists.
848 * \param format a basic image format such as GL_RGB, GL_RGBA, GL_ALPHA,
849 * GL_DEPTH_COMPONENT, etc. or GL_COLOR, GL_DEPTH, GL_STENCIL.
850 * \return GL_TRUE if buffer exists, GL_FALSE otherwise
851 */
852 GLboolean
853 _mesa_source_buffer_exists(struct gl_context *ctx, GLenum format)
854 {
855 return renderbuffer_exists(ctx, ctx->ReadBuffer, format, GL_TRUE);
856 }
857
858
859 /**
860 * As above, but for drawing operations.
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 const GLenum data_type = _mesa_get_format_datatype(
876 ctx->ReadBuffer->_ColorReadBuffer->Format);
877
878 switch (ctx->ReadBuffer->_ColorReadBuffer->Format) {
879 case MESA_FORMAT_ARGB8888:
880 return GL_BGRA;
881 case MESA_FORMAT_RGB565:
882 return GL_BGR;
883 default:
884 if (data_type == GL_UNSIGNED_INT || data_type == GL_INT) {
885 return GL_RGBA_INTEGER;
886 } else {
887 return GL_RGBA;
888 }
889 }
890 }
891
892
893 /**
894 * Used to answer the GL_IMPLEMENTATION_COLOR_READ_TYPE_OES query.
895 */
896 GLenum
897 _mesa_get_color_read_type(struct gl_context *ctx)
898 {
899 const GLenum data_type = _mesa_get_format_datatype(
900 ctx->ReadBuffer->_ColorReadBuffer->Format);
901
902 switch (ctx->ReadBuffer->_ColorReadBuffer->Format) {
903 case MESA_FORMAT_RGB565:
904 return GL_UNSIGNED_SHORT_5_6_5_REV;
905 default:
906 break;
907 }
908
909 switch (data_type) {
910 case GL_SIGNED_NORMALIZED:
911 return GL_BYTE;
912 case GL_UNSIGNED_INT:
913 case GL_INT:
914 case GL_FLOAT:
915 return data_type;
916 case GL_UNSIGNED_NORMALIZED:
917 default:
918 return GL_UNSIGNED_BYTE;
919 }
920 }
921
922
923 /**
924 * Returns the read renderbuffer for the specified format.
925 */
926 struct gl_renderbuffer *
927 _mesa_get_read_renderbuffer_for_format(const struct gl_context *ctx,
928 GLenum format)
929 {
930 const struct gl_framebuffer *rfb = ctx->ReadBuffer;
931
932 if (_mesa_is_color_format(format)) {
933 return rfb->Attachment[rfb->_ColorReadBufferIndex].Renderbuffer;
934 } else if (_mesa_is_depth_format(format) ||
935 _mesa_is_depthstencil_format(format)) {
936 return rfb->Attachment[BUFFER_DEPTH].Renderbuffer;
937 } else {
938 return rfb->Attachment[BUFFER_STENCIL].Renderbuffer;
939 }
940 }
941
942
943 /**
944 * Print framebuffer info to stderr, for debugging.
945 */
946 void
947 _mesa_print_framebuffer(const struct gl_framebuffer *fb)
948 {
949 GLuint i;
950
951 fprintf(stderr, "Mesa Framebuffer %u at %p\n", fb->Name, (void *) fb);
952 fprintf(stderr, " Size: %u x %u Status: %s\n", fb->Width, fb->Height,
953 _mesa_lookup_enum_by_nr(fb->_Status));
954 fprintf(stderr, " Attachments:\n");
955
956 for (i = 0; i < BUFFER_COUNT; i++) {
957 const struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
958 if (att->Type == GL_TEXTURE) {
959 const struct gl_texture_image *texImage =
960 _mesa_get_attachment_teximage_const(att);
961 fprintf(stderr,
962 " %2d: Texture %u, level %u, face %u, slice %u, complete %d\n",
963 i, att->Texture->Name, att->TextureLevel, att->CubeMapFace,
964 att->Zoffset, att->Complete);
965 fprintf(stderr, " Size: %u x %u x %u Format %s\n",
966 texImage->Width, texImage->Height, texImage->Depth,
967 _mesa_get_format_name(texImage->TexFormat));
968 }
969 else if (att->Type == GL_RENDERBUFFER) {
970 fprintf(stderr, " %2d: Renderbuffer %u, complete %d\n",
971 i, att->Renderbuffer->Name, att->Complete);
972 fprintf(stderr, " Size: %u x %u Format %s\n",
973 att->Renderbuffer->Width, att->Renderbuffer->Height,
974 _mesa_get_format_name(att->Renderbuffer->Format));
975 }
976 else {
977 fprintf(stderr, " %2d: none\n", i);
978 }
979 }
980 }