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