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