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