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