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