mesa: make _mesa_scissor_bounding_box() static
[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 #include "state.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 mtx_init(&fb->Mutex, mtx_plain);
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 fb->_HasAttachments = true;
162
163 compute_depth_max(fb);
164 }
165
166
167 /**
168 * Initialize a user-created gl_framebuffer object.
169 * \sa _mesa_initialize_window_framebuffer
170 */
171 void
172 _mesa_initialize_user_framebuffer(struct gl_framebuffer *fb, GLuint name)
173 {
174 assert(fb);
175 assert(name);
176
177 memset(fb, 0, sizeof(struct gl_framebuffer));
178
179 fb->Name = name;
180 fb->RefCount = 1;
181 fb->_NumColorDrawBuffers = 1;
182 fb->ColorDrawBuffer[0] = GL_COLOR_ATTACHMENT0_EXT;
183 fb->_ColorDrawBufferIndexes[0] = BUFFER_COLOR0;
184 fb->ColorReadBuffer = GL_COLOR_ATTACHMENT0_EXT;
185 fb->_ColorReadBufferIndex = BUFFER_COLOR0;
186 fb->Delete = _mesa_destroy_framebuffer;
187 mtx_init(&fb->Mutex, mtx_plain);
188 }
189
190
191 /**
192 * Deallocate buffer and everything attached to it.
193 * Typically called via the gl_framebuffer->Delete() method.
194 */
195 void
196 _mesa_destroy_framebuffer(struct gl_framebuffer *fb)
197 {
198 if (fb) {
199 _mesa_free_framebuffer_data(fb);
200 free(fb->Label);
201 free(fb);
202 }
203 }
204
205
206 /**
207 * Free all the data hanging off the given gl_framebuffer, but don't free
208 * the gl_framebuffer object itself.
209 */
210 void
211 _mesa_free_framebuffer_data(struct gl_framebuffer *fb)
212 {
213 GLuint i;
214
215 assert(fb);
216 assert(fb->RefCount == 0);
217
218 mtx_destroy(&fb->Mutex);
219
220 for (i = 0; i < BUFFER_COUNT; i++) {
221 struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
222 if (att->Renderbuffer) {
223 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
224 }
225 if (att->Texture) {
226 _mesa_reference_texobj(&att->Texture, NULL);
227 }
228 assert(!att->Renderbuffer);
229 assert(!att->Texture);
230 att->Type = GL_NONE;
231 }
232 }
233
234
235 /**
236 * Set *ptr to point to fb, with refcounting and locking.
237 * This is normally only called from the _mesa_reference_framebuffer() macro
238 * when there's a real pointer change.
239 */
240 void
241 _mesa_reference_framebuffer_(struct gl_framebuffer **ptr,
242 struct gl_framebuffer *fb)
243 {
244 if (*ptr) {
245 /* unreference old renderbuffer */
246 GLboolean deleteFlag = GL_FALSE;
247 struct gl_framebuffer *oldFb = *ptr;
248
249 mtx_lock(&oldFb->Mutex);
250 assert(oldFb->RefCount > 0);
251 oldFb->RefCount--;
252 deleteFlag = (oldFb->RefCount == 0);
253 mtx_unlock(&oldFb->Mutex);
254
255 if (deleteFlag)
256 oldFb->Delete(oldFb);
257
258 *ptr = NULL;
259 }
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 directly 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, ctx->DrawBuffer);
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 * Examine all the framebuffer's renderbuffers to update the Width/Height
325 * fields of the framebuffer. If we have renderbuffers with different
326 * sizes, set the framebuffer's width and height to the min size.
327 * Note: this is only intended for user-created framebuffers, not
328 * window-system framebuffes.
329 */
330 static void
331 update_framebuffer_size(struct gl_context *ctx, struct gl_framebuffer *fb)
332 {
333 GLuint minWidth = ~0, minHeight = ~0;
334 GLuint i;
335
336 /* user-created framebuffers only */
337 assert(_mesa_is_user_fbo(fb));
338
339 for (i = 0; i < BUFFER_COUNT; i++) {
340 struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
341 const struct gl_renderbuffer *rb = att->Renderbuffer;
342 if (rb) {
343 minWidth = MIN2(minWidth, rb->Width);
344 minHeight = MIN2(minHeight, rb->Height);
345 }
346 }
347
348 if (minWidth != ~0U) {
349 fb->Width = minWidth;
350 fb->Height = minHeight;
351 }
352 else {
353 fb->Width = 0;
354 fb->Height = 0;
355 }
356 }
357
358
359
360 /**
361 * Given a bounding box, intersect the bounding box with the scissor of
362 * a specified vieport.
363 *
364 * \param ctx GL context.
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 void
370 _mesa_intersect_scissor_bounding_box(const struct gl_context *ctx,
371 unsigned idx, int *bbox)
372 {
373 if (ctx->Scissor.EnableFlags & (1u << idx)) {
374 if (ctx->Scissor.ScissorArray[idx].X > bbox[0]) {
375 bbox[0] = ctx->Scissor.ScissorArray[idx].X;
376 }
377 if (ctx->Scissor.ScissorArray[idx].Y > bbox[2]) {
378 bbox[2] = ctx->Scissor.ScissorArray[idx].Y;
379 }
380 if (ctx->Scissor.ScissorArray[idx].X + ctx->Scissor.ScissorArray[idx].Width < bbox[1]) {
381 bbox[1] = ctx->Scissor.ScissorArray[idx].X + ctx->Scissor.ScissorArray[idx].Width;
382 }
383 if (ctx->Scissor.ScissorArray[idx].Y + ctx->Scissor.ScissorArray[idx].Height < bbox[3]) {
384 bbox[3] = ctx->Scissor.ScissorArray[idx].Y + ctx->Scissor.ScissorArray[idx].Height;
385 }
386 /* finally, check for empty region */
387 if (bbox[0] > bbox[1]) {
388 bbox[0] = bbox[1];
389 }
390 if (bbox[2] > bbox[3]) {
391 bbox[2] = bbox[3];
392 }
393 }
394 }
395
396 /**
397 * Calculate the inclusive bounding box for the scissor of a specific viewport
398 *
399 * \param ctx GL context.
400 * \param buffer Framebuffer to be checked against
401 * \param idx Index of the desired viewport
402 * \param bbox Bounding box for the scissored viewport. Stored as xmin,
403 * xmax, ymin, ymax.
404 *
405 * \warning This function assumes that the framebuffer dimensions are up to
406 * date (e.g., update_framebuffer_size has been recently called on \c buffer).
407 *
408 * \sa _mesa_clip_to_region
409 */
410 static void
411 scissor_bounding_box(const struct gl_context *ctx,
412 const struct gl_framebuffer *buffer,
413 unsigned idx, int *bbox)
414 {
415 bbox[0] = 0;
416 bbox[2] = 0;
417 bbox[1] = buffer->Width;
418 bbox[3] = buffer->Height;
419
420 _mesa_intersect_scissor_bounding_box(ctx, idx, bbox);
421
422 assert(bbox[0] <= bbox[1]);
423 assert(bbox[2] <= bbox[3]);
424 }
425
426 /**
427 * Update the context's current drawing buffer's Xmin, Xmax, Ymin, Ymax fields.
428 * These values are computed from the buffer's width and height and
429 * the scissor box, if it's enabled.
430 * \param ctx the GL context.
431 */
432 void
433 _mesa_update_draw_buffer_bounds(struct gl_context *ctx,
434 struct gl_framebuffer *buffer)
435 {
436 int bbox[4];
437
438 if (!buffer)
439 return;
440
441 if (_mesa_is_user_fbo(buffer)) {
442 /* user-created framebuffer size depends on the renderbuffers */
443 update_framebuffer_size(ctx, buffer);
444 }
445
446 /* Default to the first scissor as that's always valid */
447 scissor_bounding_box(ctx, buffer, 0, bbox);
448 buffer->_Xmin = bbox[0];
449 buffer->_Ymin = bbox[2];
450 buffer->_Xmax = bbox[1];
451 buffer->_Ymax = bbox[3];
452 }
453
454
455 /**
456 * The glGet queries of the framebuffer red/green/blue size, stencil size,
457 * etc. are satisfied by the fields of ctx->DrawBuffer->Visual. These can
458 * change depending on the renderbuffer bindings. This function updates
459 * the given framebuffer's Visual from the current renderbuffer bindings.
460 *
461 * This may apply to user-created framebuffers or window system framebuffers.
462 *
463 * Also note: ctx->DrawBuffer->Visual.depthBits might not equal
464 * ctx->DrawBuffer->Attachment[BUFFER_DEPTH].Renderbuffer.DepthBits.
465 * The former one is used to convert floating point depth values into
466 * integer Z values.
467 */
468 void
469 _mesa_update_framebuffer_visual(struct gl_context *ctx,
470 struct gl_framebuffer *fb)
471 {
472 GLuint i;
473
474 memset(&fb->Visual, 0, sizeof(fb->Visual));
475 fb->Visual.rgbMode = GL_TRUE; /* assume this */
476
477 /* find first RGB renderbuffer */
478 for (i = 0; i < BUFFER_COUNT; i++) {
479 if (fb->Attachment[i].Renderbuffer) {
480 const struct gl_renderbuffer *rb = fb->Attachment[i].Renderbuffer;
481 const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
482 const mesa_format fmt = rb->Format;
483
484 /* Grab samples and sampleBuffers from any attachment point (assuming
485 * the framebuffer is complete, we'll get the same answer from all
486 * attachments).
487 */
488 fb->Visual.samples = rb->NumSamples;
489 fb->Visual.sampleBuffers = rb->NumSamples > 0 ? 1 : 0;
490
491 if (_mesa_is_legal_color_format(ctx, baseFormat)) {
492 fb->Visual.redBits = _mesa_get_format_bits(fmt, GL_RED_BITS);
493 fb->Visual.greenBits = _mesa_get_format_bits(fmt, GL_GREEN_BITS);
494 fb->Visual.blueBits = _mesa_get_format_bits(fmt, GL_BLUE_BITS);
495 fb->Visual.alphaBits = _mesa_get_format_bits(fmt, GL_ALPHA_BITS);
496 fb->Visual.rgbBits = fb->Visual.redBits
497 + fb->Visual.greenBits + fb->Visual.blueBits;
498 if (_mesa_get_format_color_encoding(fmt) == GL_SRGB)
499 fb->Visual.sRGBCapable = ctx->Extensions.EXT_framebuffer_sRGB;
500 break;
501 }
502 }
503 }
504
505 fb->Visual.floatMode = GL_FALSE;
506 for (i = 0; i < BUFFER_COUNT; i++) {
507 if (fb->Attachment[i].Renderbuffer) {
508 const struct gl_renderbuffer *rb = fb->Attachment[i].Renderbuffer;
509 const mesa_format fmt = rb->Format;
510
511 if (_mesa_get_format_datatype(fmt) == GL_FLOAT) {
512 fb->Visual.floatMode = GL_TRUE;
513 break;
514 }
515 }
516 }
517
518 if (fb->Attachment[BUFFER_DEPTH].Renderbuffer) {
519 const struct gl_renderbuffer *rb =
520 fb->Attachment[BUFFER_DEPTH].Renderbuffer;
521 const mesa_format fmt = rb->Format;
522 fb->Visual.haveDepthBuffer = GL_TRUE;
523 fb->Visual.depthBits = _mesa_get_format_bits(fmt, GL_DEPTH_BITS);
524 }
525
526 if (fb->Attachment[BUFFER_STENCIL].Renderbuffer) {
527 const struct gl_renderbuffer *rb =
528 fb->Attachment[BUFFER_STENCIL].Renderbuffer;
529 const mesa_format fmt = rb->Format;
530 fb->Visual.haveStencilBuffer = GL_TRUE;
531 fb->Visual.stencilBits = _mesa_get_format_bits(fmt, GL_STENCIL_BITS);
532 }
533
534 if (fb->Attachment[BUFFER_ACCUM].Renderbuffer) {
535 const struct gl_renderbuffer *rb =
536 fb->Attachment[BUFFER_ACCUM].Renderbuffer;
537 const mesa_format fmt = rb->Format;
538 fb->Visual.haveAccumBuffer = GL_TRUE;
539 fb->Visual.accumRedBits = _mesa_get_format_bits(fmt, GL_RED_BITS);
540 fb->Visual.accumGreenBits = _mesa_get_format_bits(fmt, GL_GREEN_BITS);
541 fb->Visual.accumBlueBits = _mesa_get_format_bits(fmt, GL_BLUE_BITS);
542 fb->Visual.accumAlphaBits = _mesa_get_format_bits(fmt, GL_ALPHA_BITS);
543 }
544
545 compute_depth_max(fb);
546 }
547
548
549 /*
550 * Example DrawBuffers scenarios:
551 *
552 * 1. glDrawBuffer(GL_FRONT_AND_BACK), fixed-func or shader writes to
553 * "gl_FragColor" or program writes to the "result.color" register:
554 *
555 * fragment color output renderbuffer
556 * --------------------- ---------------
557 * color[0] Front, Back
558 *
559 *
560 * 2. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]), shader writes to
561 * gl_FragData[i] or program writes to result.color[i] registers:
562 *
563 * fragment color output renderbuffer
564 * --------------------- ---------------
565 * color[0] Front
566 * color[1] Aux0
567 * color[3] Aux1
568 *
569 *
570 * 3. glDrawBuffers(3, [GL_FRONT, GL_AUX0, GL_AUX1]) and shader writes to
571 * gl_FragColor, or fixed function:
572 *
573 * fragment color output renderbuffer
574 * --------------------- ---------------
575 * color[0] Front, Aux0, Aux1
576 *
577 *
578 * In either case, the list of renderbuffers is stored in the
579 * framebuffer->_ColorDrawBuffers[] array and
580 * framebuffer->_NumColorDrawBuffers indicates the number of buffers.
581 * The renderer (like swrast) has to look at the current fragment shader
582 * to see if it writes to gl_FragColor vs. gl_FragData[i] to determine
583 * how to map color outputs to renderbuffers.
584 *
585 * Note that these two calls are equivalent (for fixed function fragment
586 * shading anyway):
587 * a) glDrawBuffer(GL_FRONT_AND_BACK); (assuming non-stereo framebuffer)
588 * b) glDrawBuffers(2, [GL_FRONT_LEFT, GL_BACK_LEFT]);
589 */
590
591
592
593
594 /**
595 * Update the (derived) list of color drawing renderbuffer pointers.
596 * Later, when we're rendering we'll loop from 0 to _NumColorDrawBuffers
597 * writing colors.
598 */
599 static void
600 update_color_draw_buffers(struct gl_context *ctx, struct gl_framebuffer *fb)
601 {
602 GLuint output;
603
604 /* set 0th buffer to NULL now in case _NumColorDrawBuffers is zero */
605 fb->_ColorDrawBuffers[0] = NULL;
606
607 for (output = 0; output < fb->_NumColorDrawBuffers; output++) {
608 GLint buf = fb->_ColorDrawBufferIndexes[output];
609 if (buf >= 0) {
610 fb->_ColorDrawBuffers[output] = fb->Attachment[buf].Renderbuffer;
611 }
612 else {
613 fb->_ColorDrawBuffers[output] = NULL;
614 }
615 }
616 }
617
618
619 /**
620 * Update the (derived) color read renderbuffer pointer.
621 * Unlike the DrawBuffer, we can only read from one (or zero) color buffers.
622 */
623 static void
624 update_color_read_buffer(struct gl_context *ctx, struct gl_framebuffer *fb)
625 {
626 (void) ctx;
627 if (fb->_ColorReadBufferIndex == -1 ||
628 fb->DeletePending ||
629 fb->Width == 0 ||
630 fb->Height == 0) {
631 fb->_ColorReadBuffer = NULL; /* legal! */
632 }
633 else {
634 assert(fb->_ColorReadBufferIndex >= 0);
635 assert(fb->_ColorReadBufferIndex < BUFFER_COUNT);
636 fb->_ColorReadBuffer
637 = fb->Attachment[fb->_ColorReadBufferIndex].Renderbuffer;
638 }
639 }
640
641
642 /**
643 * Update a gl_framebuffer's derived state.
644 *
645 * Specifically, update these framebuffer fields:
646 * _ColorDrawBuffers
647 * _NumColorDrawBuffers
648 * _ColorReadBuffer
649 *
650 * If the framebuffer is user-created, make sure it's complete.
651 *
652 * The following functions (at least) can effect framebuffer state:
653 * glReadBuffer, glDrawBuffer, glDrawBuffersARB, glFramebufferRenderbufferEXT,
654 * glRenderbufferStorageEXT.
655 */
656 static void
657 update_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
658 {
659 if (_mesa_is_winsys_fbo(fb)) {
660 /* This is a window-system framebuffer */
661 /* Need to update the FB's GL_DRAW_BUFFER state to match the
662 * context state (GL_READ_BUFFER too).
663 */
664 if (fb->ColorDrawBuffer[0] != ctx->Color.DrawBuffer[0]) {
665 _mesa_drawbuffers(ctx, fb, ctx->Const.MaxDrawBuffers,
666 ctx->Color.DrawBuffer, NULL);
667 }
668 }
669 else {
670 /* This is a user-created framebuffer.
671 * Completeness only matters for user-created framebuffers.
672 */
673 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE) {
674 _mesa_test_framebuffer_completeness(ctx, fb);
675 }
676 }
677
678 /* Strictly speaking, we don't need to update the draw-state
679 * if this FB is bound as ctx->ReadBuffer (and conversely, the
680 * read-state if this FB is bound as ctx->DrawBuffer), but no
681 * harm.
682 */
683 update_color_draw_buffers(ctx, fb);
684 update_color_read_buffer(ctx, fb);
685
686 compute_depth_max(fb);
687 }
688
689
690 /**
691 * Update state related to the draw/read framebuffers.
692 */
693 void
694 _mesa_update_framebuffer(struct gl_context *ctx,
695 struct gl_framebuffer *readFb,
696 struct gl_framebuffer *drawFb)
697 {
698 assert(ctx);
699
700 update_framebuffer(ctx, drawFb);
701 if (readFb != drawFb)
702 update_framebuffer(ctx, readFb);
703
704 _mesa_update_clamp_vertex_color(ctx, drawFb);
705 _mesa_update_clamp_fragment_color(ctx, drawFb);
706 }
707
708
709 /**
710 * Check if the renderbuffer for a read/draw operation exists.
711 * \param format a basic image format such as GL_RGB, GL_RGBA, GL_ALPHA,
712 * GL_DEPTH_COMPONENT, etc. or GL_COLOR, GL_DEPTH, GL_STENCIL.
713 * \param reading if TRUE, we're going to read from the buffer,
714 if FALSE, we're going to write to the buffer.
715 * \return GL_TRUE if buffer exists, GL_FALSE otherwise
716 */
717 static GLboolean
718 renderbuffer_exists(struct gl_context *ctx,
719 struct gl_framebuffer *fb,
720 GLenum format,
721 GLboolean reading)
722 {
723 const struct gl_renderbuffer_attachment *att = fb->Attachment;
724
725 /* If we don't know the framebuffer status, update it now */
726 if (fb->_Status == 0) {
727 _mesa_test_framebuffer_completeness(ctx, fb);
728 }
729
730 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
731 return GL_FALSE;
732 }
733
734 switch (format) {
735 case GL_COLOR:
736 case GL_RED:
737 case GL_GREEN:
738 case GL_BLUE:
739 case GL_ALPHA:
740 case GL_LUMINANCE:
741 case GL_LUMINANCE_ALPHA:
742 case GL_INTENSITY:
743 case GL_RG:
744 case GL_RGB:
745 case GL_BGR:
746 case GL_RGBA:
747 case GL_BGRA:
748 case GL_ABGR_EXT:
749 case GL_RED_INTEGER_EXT:
750 case GL_RG_INTEGER:
751 case GL_GREEN_INTEGER_EXT:
752 case GL_BLUE_INTEGER_EXT:
753 case GL_ALPHA_INTEGER_EXT:
754 case GL_RGB_INTEGER_EXT:
755 case GL_RGBA_INTEGER_EXT:
756 case GL_BGR_INTEGER_EXT:
757 case GL_BGRA_INTEGER_EXT:
758 case GL_LUMINANCE_INTEGER_EXT:
759 case GL_LUMINANCE_ALPHA_INTEGER_EXT:
760 if (reading) {
761 /* about to read from a color buffer */
762 const struct gl_renderbuffer *readBuf = fb->_ColorReadBuffer;
763 if (!readBuf) {
764 return GL_FALSE;
765 }
766 assert(_mesa_get_format_bits(readBuf->Format, GL_RED_BITS) > 0 ||
767 _mesa_get_format_bits(readBuf->Format, GL_ALPHA_BITS) > 0 ||
768 _mesa_get_format_bits(readBuf->Format, GL_TEXTURE_LUMINANCE_SIZE) > 0 ||
769 _mesa_get_format_bits(readBuf->Format, GL_TEXTURE_INTENSITY_SIZE) > 0 ||
770 _mesa_get_format_bits(readBuf->Format, GL_INDEX_BITS) > 0);
771 }
772 else {
773 /* about to draw to zero or more color buffers (none is OK) */
774 return GL_TRUE;
775 }
776 break;
777 case GL_DEPTH:
778 case GL_DEPTH_COMPONENT:
779 if (att[BUFFER_DEPTH].Type == GL_NONE) {
780 return GL_FALSE;
781 }
782 break;
783 case GL_STENCIL:
784 case GL_STENCIL_INDEX:
785 if (att[BUFFER_STENCIL].Type == GL_NONE) {
786 return GL_FALSE;
787 }
788 break;
789 case GL_DEPTH_STENCIL_EXT:
790 if (att[BUFFER_DEPTH].Type == GL_NONE ||
791 att[BUFFER_STENCIL].Type == GL_NONE) {
792 return GL_FALSE;
793 }
794 break;
795 default:
796 _mesa_problem(ctx,
797 "Unexpected format 0x%x in renderbuffer_exists",
798 format);
799 return GL_FALSE;
800 }
801
802 /* OK */
803 return GL_TRUE;
804 }
805
806
807 /**
808 * Check if the renderbuffer for a read operation (glReadPixels, glCopyPixels,
809 * glCopyTex[Sub]Image, etc) exists.
810 * \param format a basic image format such as GL_RGB, GL_RGBA, GL_ALPHA,
811 * GL_DEPTH_COMPONENT, etc. or GL_COLOR, GL_DEPTH, GL_STENCIL.
812 * \return GL_TRUE if buffer exists, GL_FALSE otherwise
813 */
814 GLboolean
815 _mesa_source_buffer_exists(struct gl_context *ctx, GLenum format)
816 {
817 return renderbuffer_exists(ctx, ctx->ReadBuffer, format, GL_TRUE);
818 }
819
820
821 /**
822 * As above, but for drawing operations.
823 */
824 GLboolean
825 _mesa_dest_buffer_exists(struct gl_context *ctx, GLenum format)
826 {
827 return renderbuffer_exists(ctx, ctx->DrawBuffer, format, GL_FALSE);
828 }
829
830
831 /**
832 * Used to answer the GL_IMPLEMENTATION_COLOR_READ_FORMAT_OES queries (using
833 * GetIntegerv, GetFramebufferParameteriv, etc)
834 *
835 * If @fb is NULL, the method returns the value for the current bound
836 * framebuffer.
837 */
838 GLenum
839 _mesa_get_color_read_format(struct gl_context *ctx,
840 struct gl_framebuffer *fb,
841 const char *caller)
842 {
843 if (ctx->NewState)
844 _mesa_update_state(ctx);
845
846 if (fb == NULL)
847 fb = ctx->ReadBuffer;
848
849 if (!fb || !fb->_ColorReadBuffer) {
850 /*
851 * From OpenGL 4.5 spec, section 18.2.2 "ReadPixels":
852 *
853 * "An INVALID_OPERATION error is generated by GetIntegerv if pname
854 * is IMPLEMENTATION_COLOR_READ_FORMAT or IMPLEMENTATION_COLOR_-
855 * READ_TYPE and any of:
856 * * the read framebuffer is not framebuffer complete.
857 * * the read framebuffer is a framebuffer object, and the selected
858 * read buffer (see section 18.2.1) has no image attached.
859 * * the selected read buffer is NONE."
860 *
861 * There is not equivalent quote for GetFramebufferParameteriv or
862 * GetNamedFramebufferParameteriv, but from section 9.2.3 "Framebuffer
863 * Object Queries":
864 *
865 * "Values of framebuffer-dependent state are identical to those that
866 * would be obtained were the framebuffer object bound and queried
867 * using the simple state queries in that table."
868 *
869 * Where "using the simple state queries" refer to use GetIntegerv. So
870 * we will assume that on that situation the same error should be
871 * triggered too.
872 */
873 _mesa_error(ctx, GL_INVALID_OPERATION,
874 "%s(GL_IMPLEMENTATION_COLOR_READ_FORMAT: no GL_READ_BUFFER)",
875 caller);
876 return GL_NONE;
877 }
878 else {
879 const mesa_format format = fb->_ColorReadBuffer->Format;
880 const GLenum data_type = _mesa_get_format_datatype(format);
881
882 if (format == MESA_FORMAT_B8G8R8A8_UNORM)
883 return GL_BGRA;
884 else if (format == MESA_FORMAT_B5G6R5_UNORM)
885 return GL_RGB;
886 else if (format == MESA_FORMAT_R_UNORM8)
887 return GL_RED;
888
889 switch (data_type) {
890 case GL_UNSIGNED_INT:
891 case GL_INT:
892 return GL_RGBA_INTEGER;
893 default:
894 return GL_RGBA;
895 }
896 }
897 }
898
899
900 /**
901 * Used to answer the GL_IMPLEMENTATION_COLOR_READ_TYPE_OES queries (using
902 * GetIntegerv, GetFramebufferParameteriv, etc)
903 *
904 * If @fb is NULL, the method returns the value for the current bound
905 * framebuffer.
906 */
907 GLenum
908 _mesa_get_color_read_type(struct gl_context *ctx,
909 struct gl_framebuffer *fb,
910 const char *caller)
911 {
912 if (ctx->NewState)
913 _mesa_update_state(ctx);
914
915 if (fb == NULL)
916 fb = ctx->ReadBuffer;
917
918 if (!fb || !fb->_ColorReadBuffer) {
919 /*
920 * See comment on _mesa_get_color_read_format
921 */
922 _mesa_error(ctx, GL_INVALID_OPERATION,
923 "%s(GL_IMPLEMENTATION_COLOR_READ_TYPE: no GL_READ_BUFFER)",
924 caller);
925 return GL_NONE;
926 }
927 else {
928 const GLenum format = fb->_ColorReadBuffer->Format;
929 const GLenum data_type = _mesa_get_format_datatype(format);
930
931 if (format == MESA_FORMAT_B5G6R5_UNORM)
932 return GL_UNSIGNED_SHORT_5_6_5;
933
934 switch (data_type) {
935 case GL_SIGNED_NORMALIZED:
936 return GL_BYTE;
937 case GL_UNSIGNED_INT:
938 case GL_INT:
939 case GL_FLOAT:
940 return data_type;
941 case GL_UNSIGNED_NORMALIZED:
942 default:
943 return GL_UNSIGNED_BYTE;
944 }
945 }
946 }
947
948
949 /**
950 * Returns the read renderbuffer for the specified format.
951 */
952 struct gl_renderbuffer *
953 _mesa_get_read_renderbuffer_for_format(const struct gl_context *ctx,
954 GLenum format)
955 {
956 const struct gl_framebuffer *rfb = ctx->ReadBuffer;
957
958 if (_mesa_is_color_format(format)) {
959 return rfb->Attachment[rfb->_ColorReadBufferIndex].Renderbuffer;
960 } else if (_mesa_is_depth_format(format) ||
961 _mesa_is_depthstencil_format(format)) {
962 return rfb->Attachment[BUFFER_DEPTH].Renderbuffer;
963 } else {
964 return rfb->Attachment[BUFFER_STENCIL].Renderbuffer;
965 }
966 }
967
968
969 /**
970 * Print framebuffer info to stderr, for debugging.
971 */
972 void
973 _mesa_print_framebuffer(const struct gl_framebuffer *fb)
974 {
975 GLuint i;
976
977 fprintf(stderr, "Mesa Framebuffer %u at %p\n", fb->Name, (void *) fb);
978 fprintf(stderr, " Size: %u x %u Status: %s\n", fb->Width, fb->Height,
979 _mesa_enum_to_string(fb->_Status));
980 fprintf(stderr, " Attachments:\n");
981
982 for (i = 0; i < BUFFER_COUNT; i++) {
983 const struct gl_renderbuffer_attachment *att = &fb->Attachment[i];
984 if (att->Type == GL_TEXTURE) {
985 const struct gl_texture_image *texImage = att->Renderbuffer->TexImage;
986 fprintf(stderr,
987 " %2d: Texture %u, level %u, face %u, slice %u, complete %d\n",
988 i, att->Texture->Name, att->TextureLevel, att->CubeMapFace,
989 att->Zoffset, att->Complete);
990 fprintf(stderr, " Size: %u x %u x %u Format %s\n",
991 texImage->Width, texImage->Height, texImage->Depth,
992 _mesa_get_format_name(texImage->TexFormat));
993 }
994 else if (att->Type == GL_RENDERBUFFER) {
995 fprintf(stderr, " %2d: Renderbuffer %u, complete %d\n",
996 i, att->Renderbuffer->Name, att->Complete);
997 fprintf(stderr, " Size: %u x %u Format %s\n",
998 att->Renderbuffer->Width, att->Renderbuffer->Height,
999 _mesa_get_format_name(att->Renderbuffer->Format));
1000 }
1001 else {
1002 fprintf(stderr, " %2d: none\n", i);
1003 }
1004 }
1005 }
1006
1007 bool
1008 _mesa_is_front_buffer_reading(const struct gl_framebuffer *fb)
1009 {
1010 if (!fb || _mesa_is_user_fbo(fb))
1011 return false;
1012
1013 return fb->_ColorReadBufferIndex == BUFFER_FRONT_LEFT;
1014 }
1015
1016 bool
1017 _mesa_is_front_buffer_drawing(const struct gl_framebuffer *fb)
1018 {
1019 if (!fb || _mesa_is_user_fbo(fb))
1020 return false;
1021
1022 return (fb->_NumColorDrawBuffers >= 1 &&
1023 fb->_ColorDrawBufferIndexes[0] == BUFFER_FRONT_LEFT);
1024 }
1025
1026 static inline GLuint
1027 _mesa_geometric_nonvalidated_samples(const struct gl_framebuffer *buffer)
1028 {
1029 return buffer->_HasAttachments ?
1030 buffer->Visual.samples :
1031 buffer->DefaultGeometry.NumSamples;
1032 }
1033
1034 bool
1035 _mesa_is_multisample_enabled(const struct gl_context *ctx)
1036 {
1037 /* The sample count may not be validated by the driver, but when it is set,
1038 * we know that is in a valid range and no driver should ever validate a
1039 * multisampled framebuffer to non-multisampled and vice-versa.
1040 */
1041 return ctx->Multisample.Enabled &&
1042 ctx->DrawBuffer &&
1043 _mesa_geometric_nonvalidated_samples(ctx->DrawBuffer) >= 1;
1044 }
1045
1046 /**
1047 * Is alpha testing enabled and applicable to the currently bound
1048 * framebuffer?
1049 */
1050 bool
1051 _mesa_is_alpha_test_enabled(const struct gl_context *ctx)
1052 {
1053 bool buffer0_is_integer = ctx->DrawBuffer->_IntegerBuffers & 0x1;
1054 return (ctx->Color.AlphaEnabled && !buffer0_is_integer);
1055 }
1056
1057 /**
1058 * Is alpha to coverage enabled and applicable to the currently bound
1059 * framebuffer?
1060 */
1061 bool
1062 _mesa_is_alpha_to_coverage_enabled(const struct gl_context *ctx)
1063 {
1064 bool buffer0_is_integer = ctx->DrawBuffer->_IntegerBuffers & 0x1;
1065 return (ctx->Multisample.SampleAlphaToCoverage &&
1066 _mesa_is_multisample_enabled(ctx) &&
1067 !buffer0_is_integer);
1068 }