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