f49d74c89f043f195537c4fa972d77222e08247b
[mesa.git] / src / mesa / main / framebuffer.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * 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 #include <stdio.h>
33 #include "glheader.h"
34 #include "imports.h"
35 #include "blend.h"
36 #include "buffers.h"
37 #include "context.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 #include "glformats.h"
47
48
49
50 /**
51 * Compute/set the _DepthMax field for the given framebuffer.
52 * This value depends on the Z buffer resolution.
53 */
54 static void
55 compute_depth_max(struct gl_framebuffer *fb)
56 {
57 if (fb->Visual.depthBits == 0) {
58 /* Special case. Even if we don't have a depth buffer we need
59 * good values for DepthMax for Z vertex transformation purposes
60 * and for per-fragment fog computation.
61 */
62 fb->_DepthMax = (1 << 16) - 1;
63 }
64 else if (fb->Visual.depthBits < 32) {
65 fb->_DepthMax = (1 << fb->Visual.depthBits) - 1;
66 }
67 else {
68 /* Special case since shift values greater than or equal to the
69 * number of bits in the left hand expression's type are undefined.
70 */
71 fb->_DepthMax = 0xffffffff;
72 }
73 fb->_DepthMaxF = (GLfloat) fb->_DepthMax;
74
75 /* Minimum resolvable depth value, for polygon offset */
76 fb->_MRD = (GLfloat)1.0 / fb->_DepthMaxF;
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 struct gl_config *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(struct gl_context *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 struct gl_config *visual)
127 {
128 assert(fb);
129 assert(visual);
130
131 memset(fb, 0, sizeof(struct gl_framebuffer));
132
133 mtx_init(&fb->Mutex, mtx_plain);
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 fb->_AllColorBuffersFixedPoint = !visual->floatMode;
159 fb->_HasSNormOrFloatColorBuffer = visual->floatMode;
160 fb->_HasAttachments = true;
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 mtx_init(&fb->Mutex, mtx_plain);
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->Label);
200 free(fb);
201 }
202 }
203
204
205 /**
206 * Free all the data hanging off the given gl_framebuffer, but don't free
207 * the gl_framebuffer object itself.
208 */
209 void
210 _mesa_free_framebuffer_data(struct gl_framebuffer *fb)
211 {
212 GLuint i;
213
214 assert(fb);
215 assert(fb->RefCount == 0);
216
217 mtx_destroy(&fb->Mutex);
218
219 for (i = 0; i < BUFFER_COUNT; i++) {
220 struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
221 if (att->Renderbuffer) {
222 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
223 }
224 if (att->Texture) {
225 _mesa_reference_texobj(&att->Texture, NULL);
226 }
227 assert(!att->Renderbuffer);
228 assert(!att->Texture);
229 att->Type = GL_NONE;
230 }
231 }
232
233
234 /**
235 * Set *ptr to point to fb, with refcounting and locking.
236 * This is normally only called from the _mesa_reference_framebuffer() macro
237 * when there's a real pointer change.
238 */
239 void
240 _mesa_reference_framebuffer_(struct gl_framebuffer **ptr,
241 struct gl_framebuffer *fb)
242 {
243 if (*ptr) {
244 /* unreference old renderbuffer */
245 GLboolean deleteFlag = GL_FALSE;
246 struct gl_framebuffer *oldFb = *ptr;
247
248 mtx_lock(&oldFb->Mutex);
249 assert(oldFb->RefCount > 0);
250 oldFb->RefCount--;
251 deleteFlag = (oldFb->RefCount == 0);
252 mtx_unlock(&oldFb->Mutex);
253
254 if (deleteFlag)
255 oldFb->Delete(oldFb);
256
257 *ptr = NULL;
258 }
259 assert(!*ptr);
260
261 if (fb) {
262 mtx_lock(&fb->Mutex);
263 fb->RefCount++;
264 mtx_unlock(&fb->Mutex);
265 *ptr = fb;
266 }
267 }
268
269
270 /**
271 * Resize the given framebuffer's renderbuffers to the new width and height.
272 * This should only be used for window-system framebuffers, not
273 * user-created renderbuffers (i.e. made with GL_EXT_framebuffer_object).
274 * This will typically be called via ctx->Driver.ResizeBuffers() or directly
275 * from a device driver.
276 *
277 * \note it's possible for ctx to be null since a window can be resized
278 * without a currently bound rendering context.
279 */
280 void
281 _mesa_resize_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
282 GLuint width, GLuint height)
283 {
284 GLuint i;
285
286 /* XXX I think we could check if the size is not changing
287 * and return early.
288 */
289
290 /* Can only resize win-sys framebuffer objects */
291 assert(_mesa_is_winsys_fbo(fb));
292
293 for (i = 0; i < BUFFER_COUNT; i++) {
294 struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
295 if (att->Type == GL_RENDERBUFFER_EXT && att->Renderbuffer) {
296 struct gl_renderbuffer *rb = att->Renderbuffer;
297 /* only resize if size is changing */
298 if (rb->Width != width || rb->Height != height) {
299 if (rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height)) {
300 assert(rb->Width == width);
301 assert(rb->Height == height);
302 }
303 else {
304 _mesa_error(ctx, GL_OUT_OF_MEMORY, "Resizing framebuffer");
305 /* no return */
306 }
307 }
308 }
309 }
310
311 fb->Width = width;
312 fb->Height = height;
313
314 if (ctx) {
315 /* update scissor / window bounds */
316 _mesa_update_draw_buffer_bounds(ctx, ctx->DrawBuffer);
317 /* Signal new buffer state so that swrast will update its clipping
318 * info (the CLIP_BIT flag).
319 */
320 ctx->NewState |= _NEW_BUFFERS;
321 }
322 }
323
324 /**
325 * Examine all the framebuffer's renderbuffers to update the Width/Height
326 * fields of the framebuffer. If we have renderbuffers with different
327 * sizes, set the framebuffer's width and height to the min size.
328 * Note: this is only intended for user-created framebuffers, not
329 * window-system framebuffes.
330 */
331 static void
332 update_framebuffer_size(struct gl_context *ctx, struct gl_framebuffer *fb)
333 {
334 GLuint minWidth = ~0, minHeight = ~0;
335 GLuint i;
336
337 /* user-created framebuffers only */
338 assert(_mesa_is_user_fbo(fb));
339
340 for (i = 0; i < BUFFER_COUNT; i++) {
341 struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
342 const struct gl_renderbuffer *rb = att->Renderbuffer;
343 if (rb) {
344 minWidth = MIN2(minWidth, rb->Width);
345 minHeight = MIN2(minHeight, rb->Height);
346 }
347 }
348
349 if (minWidth != ~0U) {
350 fb->Width = minWidth;
351 fb->Height = minHeight;
352 }
353 else {
354 fb->Width = 0;
355 fb->Height = 0;
356 }
357 }
358
359
360 /**
361 * Calculate the inclusive bounding box for the scissor of a specific viewport
362 *
363 * \param ctx GL context.
364 * \param buffer Framebuffer to be checked against
365 * \param idx Index of the desired viewport
366 * \param bbox Bounding box for the scissored viewport. Stored as xmin,
367 * xmax, ymin, ymax.
368 *
369 * \warning This function assumes that the framebuffer dimensions are up to
370 * date (e.g., update_framebuffer_size has been recently called on \c buffer).
371 *
372 * \sa _mesa_clip_to_region
373 */
374 void
375 _mesa_scissor_bounding_box(const struct gl_context *ctx,
376 const struct gl_framebuffer *buffer,
377 unsigned idx, int *bbox)
378 {
379 bbox[0] = 0;
380 bbox[2] = 0;
381 bbox[1] = buffer->Width;
382 bbox[3] = buffer->Height;
383
384 if (ctx->Scissor.EnableFlags & (1u << idx)) {
385 if (ctx->Scissor.ScissorArray[idx].X > bbox[0]) {
386 bbox[0] = ctx->Scissor.ScissorArray[idx].X;
387 }
388 if (ctx->Scissor.ScissorArray[idx].Y > bbox[2]) {
389 bbox[2] = ctx->Scissor.ScissorArray[idx].Y;
390 }
391 if (ctx->Scissor.ScissorArray[idx].X + ctx->Scissor.ScissorArray[idx].Width < bbox[1]) {
392 bbox[1] = ctx->Scissor.ScissorArray[idx].X + ctx->Scissor.ScissorArray[idx].Width;
393 }
394 if (ctx->Scissor.ScissorArray[idx].Y + ctx->Scissor.ScissorArray[idx].Height < bbox[3]) {
395 bbox[3] = ctx->Scissor.ScissorArray[idx].Y + ctx->Scissor.ScissorArray[idx].Height;
396 }
397 /* finally, check for empty region */
398 if (bbox[0] > bbox[1]) {
399 bbox[0] = bbox[1];
400 }
401 if (bbox[2] > bbox[3]) {
402 bbox[2] = bbox[3];
403 }
404 }
405
406 assert(bbox[0] <= bbox[1]);
407 assert(bbox[2] <= bbox[3]);
408 }
409
410 /**
411 * Update the context's current drawing buffer's Xmin, Xmax, Ymin, Ymax fields.
412 * These values are computed from the buffer's width and height and
413 * the scissor box, if it's enabled.
414 * \param ctx the GL context.
415 */
416 void
417 _mesa_update_draw_buffer_bounds(struct gl_context *ctx,
418 struct gl_framebuffer *buffer)
419 {
420 int bbox[4];
421
422 if (!buffer)
423 return;
424
425 if (_mesa_is_user_fbo(buffer)) {
426 /* user-created framebuffer size depends on the renderbuffers */
427 update_framebuffer_size(ctx, buffer);
428 }
429
430 /* Default to the first scissor as that's always valid */
431 _mesa_scissor_bounding_box(ctx, buffer, 0, bbox);
432 buffer->_Xmin = bbox[0];
433 buffer->_Ymin = bbox[2];
434 buffer->_Xmax = bbox[1];
435 buffer->_Ymax = bbox[3];
436 }
437
438
439 /**
440 * The glGet queries of the framebuffer red/green/blue size, stencil size,
441 * etc. are satisfied by the fields of ctx->DrawBuffer->Visual. These can
442 * change depending on the renderbuffer bindings. This function updates
443 * the given framebuffer's Visual from the current renderbuffer bindings.
444 *
445 * This may apply to user-created framebuffers or window system framebuffers.
446 *
447 * Also note: ctx->DrawBuffer->Visual.depthBits might not equal
448 * ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer.DepthBits.
449 * The former one is used to convert floating point depth values into
450 * integer Z values.
451 */
452 void
453 _mesa_update_framebuffer_visual(struct gl_context *ctx,
454 struct gl_framebuffer *fb)
455 {
456 GLuint i;
457
458 memset(&fb->Visual, 0, sizeof(fb->Visual));
459 fb->Visual.rgbMode = GL_TRUE; /* assume this */
460
461 #if 0 /* this _might_ be needed */
462 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
463 /* leave visual fields zero'd */
464 return;
465 }
466 #endif
467
468 /* find first RGB renderbuffer */
469 for (i = 0; i < BUFFER_COUNT; i++) {
470 if (fb->Attachment[i].Renderbuffer) {
471 const struct gl_renderbuffer *rb = fb->Attachment[i].Renderbuffer;
472 const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
473 const mesa_format fmt = rb->Format;
474
475 /* Grab samples and sampleBuffers from any attachment point (assuming
476 * the framebuffer is complete, we'll get the same answer from all
477 * attachments).
478 */
479 fb->Visual.samples = rb->NumSamples;
480 fb->Visual.sampleBuffers = rb->NumSamples > 0 ? 1 : 0;
481
482 if (_mesa_is_legal_color_format(ctx, baseFormat)) {
483 fb->Visual.redBits = _mesa_get_format_bits(fmt, GL_RED_BITS);
484 fb->Visual.greenBits = _mesa_get_format_bits(fmt, GL_GREEN_BITS);
485 fb->Visual.blueBits = _mesa_get_format_bits(fmt, GL_BLUE_BITS);
486 fb->Visual.alphaBits = _mesa_get_format_bits(fmt, GL_ALPHA_BITS);
487 fb->Visual.rgbBits = fb->Visual.redBits
488 + fb->Visual.greenBits + fb->Visual.blueBits;
489 if (_mesa_get_format_color_encoding(fmt) == GL_SRGB)
490 fb->Visual.sRGBCapable = ctx->Extensions.EXT_framebuffer_sRGB;
491 break;
492 }
493 }
494 }
495
496 fb->Visual.floatMode = GL_FALSE;
497 for (i = 0; i < BUFFER_COUNT; i++) {
498 if (fb->Attachment[i].Renderbuffer) {
499 const struct gl_renderbuffer *rb = fb->Attachment[i].Renderbuffer;
500 const mesa_format fmt = rb->Format;
501
502 if (_mesa_get_format_datatype(fmt) == GL_FLOAT) {
503 fb->Visual.floatMode = GL_TRUE;
504 break;
505 }
506 }
507 }
508
509 if (fb->Attachment[BUFFER_DEPTH].Renderbuffer) {
510 const struct gl_renderbuffer *rb =
511 fb->Attachment[BUFFER_DEPTH].Renderbuffer;
512 const mesa_format fmt = rb->Format;
513 fb->Visual.haveDepthBuffer = GL_TRUE;
514 fb->Visual.depthBits = _mesa_get_format_bits(fmt, GL_DEPTH_BITS);
515 }
516
517 if (fb->Attachment[BUFFER_STENCIL].Renderbuffer) {
518 const struct gl_renderbuffer *rb =
519 fb->Attachment[BUFFER_STENCIL].Renderbuffer;
520 const mesa_format fmt = rb->Format;
521 fb->Visual.haveStencilBuffer = GL_TRUE;
522 fb->Visual.stencilBits = _mesa_get_format_bits(fmt, GL_STENCIL_BITS);
523 }
524
525 if (fb->Attachment[BUFFER_ACCUM].Renderbuffer) {
526 const struct gl_renderbuffer *rb =
527 fb->Attachment[BUFFER_ACCUM].Renderbuffer;
528 const mesa_format fmt = rb->Format;
529 fb->Visual.haveAccumBuffer = GL_TRUE;
530 fb->Visual.accumRedBits = _mesa_get_format_bits(fmt, GL_RED_BITS);
531 fb->Visual.accumGreenBits = _mesa_get_format_bits(fmt, GL_GREEN_BITS);
532 fb->Visual.accumBlueBits = _mesa_get_format_bits(fmt, GL_BLUE_BITS);
533 fb->Visual.accumAlphaBits = _mesa_get_format_bits(fmt, GL_ALPHA_BITS);
534 }
535
536 compute_depth_max(fb);
537 }
538
539
540 /*
541 * Example DrawBuffers scenarios:
542 *
543 * 1. glDrawBuffer(GL_FRONT_AND_BACK), fixed-func or shader writes to
544 * "gl_FragColor" or program writes to the "result.color" register:
545 *
546 * fragment color output renderbuffer
547 * --------------------- ---------------
548 * color[0] Front, Back
549 *
550 *
551 * 2. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]), shader writes to
552 * gl_FragData[i] or program writes to result.color[i] registers:
553 *
554 * fragment color output renderbuffer
555 * --------------------- ---------------
556 * color[0] Front
557 * color[1] Aux0
558 * color[3] Aux1
559 *
560 *
561 * 3. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]) and shader writes to
562 * gl_FragColor, or fixed function:
563 *
564 * fragment color output renderbuffer
565 * --------------------- ---------------
566 * color[0] Front, Aux0, Aux1
567 *
568 *
569 * In either case, the list of renderbuffers is stored in the
570 * framebuffer->_ColorDrawBuffers[] array and
571 * framebuffer->_NumColorDrawBuffers indicates the number of buffers.
572 * The renderer (like swrast) has to look at the current fragment shader
573 * to see if it writes to gl_FragColor vs. gl_FragData[i] to determine
574 * how to map color outputs to renderbuffers.
575 *
576 * Note that these two calls are equivalent (for fixed function fragment
577 * shading anyway):
578 * a) glDrawBuffer(GL_FRONT_AND_BACK); (assuming non-stereo framebuffer)
579 * b) glDrawBuffers(2, [GL_FRONT_LEFT, GL_BACK_LEFT]);
580 */
581
582
583
584
585 /**
586 * Update the (derived) list of color drawing renderbuffer pointers.
587 * Later, when we're rendering we'll loop from 0 to _NumColorDrawBuffers
588 * writing colors.
589 */
590 static void
591 update_color_draw_buffers(struct gl_context *ctx, struct gl_framebuffer *fb)
592 {
593 GLuint output;
594
595 /* set 0th buffer to NULL now in case _NumColorDrawBuffers is zero */
596 fb->_ColorDrawBuffers[0] = NULL;
597
598 for (output = 0; output < fb->_NumColorDrawBuffers; output++) {
599 GLint buf = fb->_ColorDrawBufferIndexes[output];
600 if (buf >= 0) {
601 fb->_ColorDrawBuffers[output] = fb->Attachment[buf].Renderbuffer;
602 }
603 else {
604 fb->_ColorDrawBuffers[output] = NULL;
605 }
606 }
607 }
608
609
610 /**
611 * Update the (derived) color read renderbuffer pointer.
612 * Unlike the DrawBuffer, we can only read from one (or zero) color buffers.
613 */
614 static void
615 update_color_read_buffer(struct gl_context *ctx, struct gl_framebuffer *fb)
616 {
617 (void) ctx;
618 if (fb->_ColorReadBufferIndex == -1 ||
619 fb->DeletePending ||
620 fb->Width == 0 ||
621 fb->Height == 0) {
622 fb->_ColorReadBuffer = NULL; /* legal! */
623 }
624 else {
625 assert(fb->_ColorReadBufferIndex >= 0);
626 assert(fb->_ColorReadBufferIndex < BUFFER_COUNT);
627 fb->_ColorReadBuffer
628 = fb->Attachment[fb->_ColorReadBufferIndex].Renderbuffer;
629 }
630 }
631
632
633 /**
634 * Update a gl_framebuffer's derived state.
635 *
636 * Specifically, update these framebuffer fields:
637 * _ColorDrawBuffers
638 * _NumColorDrawBuffers
639 * _ColorReadBuffer
640 *
641 * If the framebuffer is user-created, make sure it's complete.
642 *
643 * The following functions (at least) can effect framebuffer state:
644 * glReadBuffer, glDrawBuffer, glDrawBuffersARB, glFramebufferRenderbufferEXT,
645 * glRenderbufferStorageEXT.
646 */
647 static void
648 update_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
649 {
650 if (_mesa_is_winsys_fbo(fb)) {
651 /* This is a window-system framebuffer */
652 /* Need to update the FB's GL_DRAW_BUFFER state to match the
653 * context state (GL_READ_BUFFER too).
654 */
655 if (fb->ColorDrawBuffer[0] != ctx->Color.DrawBuffer[0]) {
656 _mesa_drawbuffers(ctx, fb, ctx->Const.MaxDrawBuffers,
657 ctx->Color.DrawBuffer, NULL);
658 }
659 }
660 else {
661 /* This is a user-created framebuffer.
662 * Completeness only matters for user-created framebuffers.
663 */
664 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE) {
665 _mesa_test_framebuffer_completeness(ctx, fb);
666 }
667 }
668
669 /* Strictly speaking, we don't need to update the draw-state
670 * if this FB is bound as ctx->ReadBuffer (and conversely, the
671 * read-state if this FB is bound as ctx->DrawBuffer), but no
672 * harm.
673 */
674 update_color_draw_buffers(ctx, fb);
675 update_color_read_buffer(ctx, fb);
676
677 compute_depth_max(fb);
678 }
679
680
681 /**
682 * Update state related to the draw/read framebuffers.
683 */
684 void
685 _mesa_update_framebuffer(struct gl_context *ctx,
686 struct gl_framebuffer *readFb,
687 struct gl_framebuffer *drawFb)
688 {
689 assert(ctx);
690
691 update_framebuffer(ctx, drawFb);
692 if (readFb != drawFb)
693 update_framebuffer(ctx, readFb);
694
695 _mesa_update_clamp_vertex_color(ctx, drawFb);
696 _mesa_update_clamp_fragment_color(ctx, drawFb);
697 }
698
699
700 /**
701 * Check if the renderbuffer for a read/draw operation exists.
702 * \param format a basic image format such as GL_RGB, GL_RGBA, GL_ALPHA,
703 * GL_DEPTH_COMPONENT, etc. or GL_COLOR, GL_DEPTH, GL_STENCIL.
704 * \param reading if TRUE, we're going to read from the buffer,
705 if FALSE, we're going to write to the buffer.
706 * \return GL_TRUE if buffer exists, GL_FALSE otherwise
707 */
708 static GLboolean
709 renderbuffer_exists(struct gl_context *ctx,
710 struct gl_framebuffer *fb,
711 GLenum format,
712 GLboolean reading)
713 {
714 const struct gl_renderbuffer_attachment *att = fb->Attachment;
715
716 /* If we don't know the framebuffer status, update it now */
717 if (fb->_Status == 0) {
718 _mesa_test_framebuffer_completeness(ctx, fb);
719 }
720
721 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
722 return GL_FALSE;
723 }
724
725 switch (format) {
726 case GL_COLOR:
727 case GL_RED:
728 case GL_GREEN:
729 case GL_BLUE:
730 case GL_ALPHA:
731 case GL_LUMINANCE:
732 case GL_LUMINANCE_ALPHA:
733 case GL_INTENSITY:
734 case GL_RG:
735 case GL_RGB:
736 case GL_BGR:
737 case GL_RGBA:
738 case GL_BGRA:
739 case GL_ABGR_EXT:
740 case GL_RED_INTEGER_EXT:
741 case GL_RG_INTEGER:
742 case GL_GREEN_INTEGER_EXT:
743 case GL_BLUE_INTEGER_EXT:
744 case GL_ALPHA_INTEGER_EXT:
745 case GL_RGB_INTEGER_EXT:
746 case GL_RGBA_INTEGER_EXT:
747 case GL_BGR_INTEGER_EXT:
748 case GL_BGRA_INTEGER_EXT:
749 case GL_LUMINANCE_INTEGER_EXT:
750 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
751 if (reading) {
752 /* about to read from a color buffer */
753 const struct gl_renderbuffer *readBuf = fb->_ColorReadBuffer;
754 if (!readBuf) {
755 return GL_FALSE;
756 }
757 assert(_mesa_get_format_bits(readBuf->Format, GL_RED_BITS) > 0 ||
758 _mesa_get_format_bits(readBuf->Format, GL_ALPHA_BITS) > 0 ||
759 _mesa_get_format_bits(readBuf->Format, GL_TEXTURE_LUMINANCE_SIZE) > 0 ||
760 _mesa_get_format_bits(readBuf->Format, GL_TEXTURE_INTENSITY_SIZE) > 0 ||
761 _mesa_get_format_bits(readBuf->Format, GL_INDEX_BITS) > 0);
762 }
763 else {
764 /* about to draw to zero or more color buffers (none is OK) */
765 return GL_TRUE;
766 }
767 break;
768 case GL_DEPTH:
769 case GL_DEPTH_COMPONENT:
770 if (att[BUFFER_DEPTH].Type == GL_NONE) {
771 return GL_FALSE;
772 }
773 break;
774 case GL_STENCIL:
775 case GL_STENCIL_INDEX:
776 if (att[BUFFER_STENCIL].Type == GL_NONE) {
777 return GL_FALSE;
778 }
779 break;
780 case GL_DEPTH_STENCIL_EXT:
781 if (att[BUFFER_DEPTH].Type == GL_NONE ||
782 att[BUFFER_STENCIL].Type == GL_NONE) {
783 return GL_FALSE;
784 }
785 break;
786 default:
787 _mesa_problem(ctx,
788 "Unexpected format 0x%x in renderbuffer_exists",
789 format);
790 return GL_FALSE;
791 }
792
793 /* OK */
794 return GL_TRUE;
795 }
796
797
798 /**
799 * Check if the renderbuffer for a read operation (glReadPixels, glCopyPixels,
800 * glCopyTex[Sub]Image, etc) exists.
801 * \param format a basic image format such as GL_RGB, GL_RGBA, GL_ALPHA,
802 * GL_DEPTH_COMPONENT, etc. or GL_COLOR, GL_DEPTH, GL_STENCIL.
803 * \return GL_TRUE if buffer exists, GL_FALSE otherwise
804 */
805 GLboolean
806 _mesa_source_buffer_exists(struct gl_context *ctx, GLenum format)
807 {
808 return renderbuffer_exists(ctx, ctx->ReadBuffer, format, GL_TRUE);
809 }
810
811
812 /**
813 * As above, but for drawing operations.
814 */
815 GLboolean
816 _mesa_dest_buffer_exists(struct gl_context *ctx, GLenum format)
817 {
818 return renderbuffer_exists(ctx, ctx->DrawBuffer, format, GL_FALSE);
819 }
820
821
822 /**
823 * Used to answer the GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES query.
824 */
825 GLenum
826 _mesa_get_color_read_format(struct gl_context *ctx)
827 {
828 if (!ctx->ReadBuffer || !ctx->ReadBuffer->_ColorReadBuffer) {
829 /* The spec is unclear how to handle this case, but NVIDIA's
830 * driver generates GL_INVALID_OPERATION.
831 */
832 _mesa_error(ctx, GL_INVALID_OPERATION,
833 "glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT: "
834 "no GL_READ_BUFFER)");
835 return GL_NONE;
836 }
837 else {
838 const GLenum format = ctx->ReadBuffer->_ColorReadBuffer->Format;
839 const GLenum data_type = _mesa_get_format_datatype(format);
840
841 if (format == MESA_FORMAT_B8G8R8A8_UNORM)
842 return GL_BGRA;
843 else if (format == MESA_FORMAT_B5G6R5_UNORM)
844 return GL_BGR;
845
846 switch (data_type) {
847 case GL_UNSIGNED_INT:
848 case GL_INT:
849 return GL_RGBA_INTEGER;
850 default:
851 return GL_RGBA;
852 }
853 }
854 }
855
856
857 /**
858 * Used to answer the GL_IMPLEMENTATION_COLOR_READ_TYPE_OES query.
859 */
860 GLenum
861 _mesa_get_color_read_type(struct gl_context *ctx)
862 {
863 if (!ctx->ReadBuffer || !ctx->ReadBuffer->_ColorReadBuffer) {
864 /* The spec is unclear how to handle this case, but NVIDIA's
865 * driver generates GL_INVALID_OPERATION.
866 */
867 _mesa_error(ctx, GL_INVALID_OPERATION,
868 "glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE: "
869 "no GL_READ_BUFFER)");
870 return GL_NONE;
871 }
872 else {
873 const GLenum format = ctx->ReadBuffer->_ColorReadBuffer->Format;
874 const GLenum data_type = _mesa_get_format_datatype(format);
875
876 if (format == MESA_FORMAT_B5G6R5_UNORM)
877 return GL_UNSIGNED_SHORT_5_6_5_REV;
878
879 switch (data_type) {
880 case GL_SIGNED_NORMALIZED:
881 return GL_BYTE;
882 case GL_UNSIGNED_INT:
883 case GL_INT:
884 case GL_FLOAT:
885 return data_type;
886 case GL_UNSIGNED_NORMALIZED:
887 default:
888 return GL_UNSIGNED_BYTE;
889 }
890 }
891 }
892
893
894 /**
895 * Returns the read renderbuffer for the specified format.
896 */
897 struct gl_renderbuffer *
898 _mesa_get_read_renderbuffer_for_format(const struct gl_context *ctx,
899 GLenum format)
900 {
901 const struct gl_framebuffer *rfb = ctx->ReadBuffer;
902
903 if (_mesa_is_color_format(format)) {
904 return rfb->Attachment[rfb->_ColorReadBufferIndex].Renderbuffer;
905 } else if (_mesa_is_depth_format(format) ||
906 _mesa_is_depthstencil_format(format)) {
907 return rfb->Attachment[BUFFER_DEPTH].Renderbuffer;
908 } else {
909 return rfb->Attachment[BUFFER_STENCIL].Renderbuffer;
910 }
911 }
912
913
914 /**
915 * Print framebuffer info to stderr, for debugging.
916 */
917 void
918 _mesa_print_framebuffer(const struct gl_framebuffer *fb)
919 {
920 GLuint i;
921
922 fprintf(stderr, "Mesa Framebuffer %u at %p\n", fb->Name, (void *) fb);
923 fprintf(stderr, " Size: %u x %u Status: %s\n", fb->Width, fb->Height,
924 _mesa_lookup_enum_by_nr(fb->_Status));
925 fprintf(stderr, " Attachments:\n");
926
927 for (i = 0; i < BUFFER_COUNT; i++) {
928 const struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
929 if (att->Type == GL_TEXTURE) {
930 const struct gl_texture_image *texImage = att->Renderbuffer->TexImage;
931 fprintf(stderr,
932 " %2d: Texture %u, level %u, face %u, slice %u, complete %d\n",
933 i, att->Texture->Name, att->TextureLevel, att->CubeMapFace,
934 att->Zoffset, att->Complete);
935 fprintf(stderr, " Size: %u x %u x %u Format %s\n",
936 texImage->Width, texImage->Height, texImage->Depth,
937 _mesa_get_format_name(texImage->TexFormat));
938 }
939 else if (att->Type == GL_RENDERBUFFER) {
940 fprintf(stderr, " %2d: Renderbuffer %u, complete %d\n",
941 i, att->Renderbuffer->Name, att->Complete);
942 fprintf(stderr, " Size: %u x %u Format %s\n",
943 att->Renderbuffer->Width, att->Renderbuffer->Height,
944 _mesa_get_format_name(att->Renderbuffer->Format));
945 }
946 else {
947 fprintf(stderr, " %2d: none\n", i);
948 }
949 }
950 }