mesa: print number of samples in renderbuffer_storage error msg
[mesa.git] / src / mesa / main / fbobject.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (C) 1999-2009 VMware, Inc. All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27 /*
28 * GL_EXT/ARB_framebuffer_object extensions
29 *
30 * Authors:
31 * Brian Paul
32 */
33
34 #include <stdbool.h>
35
36 #include "buffers.h"
37 #include "context.h"
38 #include "enums.h"
39 #include "fbobject.h"
40 #include "formats.h"
41 #include "framebuffer.h"
42 #include "glformats.h"
43 #include "hash.h"
44 #include "macros.h"
45 #include "multisample.h"
46 #include "mtypes.h"
47 #include "renderbuffer.h"
48 #include "state.h"
49 #include "teximage.h"
50 #include "texobj.h"
51
52
53 /**
54 * Notes:
55 *
56 * None of the GL_EXT_framebuffer_object functions are compiled into
57 * display lists.
58 */
59
60
61
62 /*
63 * When glGenRender/FramebuffersEXT() is called we insert pointers to
64 * these placeholder objects into the hash table.
65 * Later, when the object ID is first bound, we replace the placeholder
66 * with the real frame/renderbuffer.
67 */
68 static struct gl_framebuffer DummyFramebuffer;
69 static struct gl_renderbuffer DummyRenderbuffer;
70
71 /* We bind this framebuffer when applications pass a NULL
72 * drawable/surface in make current. */
73 static struct gl_framebuffer IncompleteFramebuffer;
74
75
76 static void
77 delete_dummy_renderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb)
78 {
79 /* no op */
80 }
81
82 static void
83 delete_dummy_framebuffer(struct gl_framebuffer *fb)
84 {
85 /* no op */
86 }
87
88
89 void
90 _mesa_init_fbobjects(struct gl_context *ctx)
91 {
92 mtx_init(&DummyFramebuffer.Mutex, mtx_plain);
93 mtx_init(&DummyRenderbuffer.Mutex, mtx_plain);
94 mtx_init(&IncompleteFramebuffer.Mutex, mtx_plain);
95 DummyFramebuffer.Delete = delete_dummy_framebuffer;
96 DummyRenderbuffer.Delete = delete_dummy_renderbuffer;
97 IncompleteFramebuffer.Delete = delete_dummy_framebuffer;
98 }
99
100 struct gl_framebuffer *
101 _mesa_get_incomplete_framebuffer(void)
102 {
103 return &IncompleteFramebuffer;
104 }
105
106 /**
107 * Helper routine for getting a gl_renderbuffer.
108 */
109 struct gl_renderbuffer *
110 _mesa_lookup_renderbuffer(struct gl_context *ctx, GLuint id)
111 {
112 struct gl_renderbuffer *rb;
113
114 if (id == 0)
115 return NULL;
116
117 rb = (struct gl_renderbuffer *)
118 _mesa_HashLookup(ctx->Shared->RenderBuffers, id);
119 return rb;
120 }
121
122
123 /**
124 * A convenience function for direct state access that throws
125 * GL_INVALID_OPERATION if the renderbuffer doesn't exist.
126 */
127 struct gl_renderbuffer *
128 _mesa_lookup_renderbuffer_err(struct gl_context *ctx, GLuint id,
129 const char *func)
130 {
131 struct gl_renderbuffer *rb;
132
133 rb = _mesa_lookup_renderbuffer(ctx, id);
134 if (!rb || rb == &DummyRenderbuffer) {
135 _mesa_error(ctx, GL_INVALID_OPERATION,
136 "%s(non-existent renderbuffer %u)", func, id);
137 return NULL;
138 }
139
140 return rb;
141 }
142
143
144 /**
145 * Helper routine for getting a gl_framebuffer.
146 */
147 struct gl_framebuffer *
148 _mesa_lookup_framebuffer(struct gl_context *ctx, GLuint id)
149 {
150 struct gl_framebuffer *fb;
151
152 if (id == 0)
153 return NULL;
154
155 fb = (struct gl_framebuffer *)
156 _mesa_HashLookup(ctx->Shared->FrameBuffers, id);
157 return fb;
158 }
159
160
161 /**
162 * A convenience function for direct state access that throws
163 * GL_INVALID_OPERATION if the framebuffer doesn't exist.
164 */
165 struct gl_framebuffer *
166 _mesa_lookup_framebuffer_err(struct gl_context *ctx, GLuint id,
167 const char *func)
168 {
169 struct gl_framebuffer *fb;
170
171 fb = _mesa_lookup_framebuffer(ctx, id);
172 if (!fb || fb == &DummyFramebuffer) {
173 _mesa_error(ctx, GL_INVALID_OPERATION,
174 "%s(non-existent framebuffer %u)", func, id);
175 return NULL;
176 }
177
178 return fb;
179 }
180
181
182 /**
183 * Mark the given framebuffer as invalid. This will force the
184 * test for framebuffer completeness to be done before the framebuffer
185 * is used.
186 */
187 static void
188 invalidate_framebuffer(struct gl_framebuffer *fb)
189 {
190 fb->_Status = 0; /* "indeterminate" */
191 }
192
193
194 /**
195 * Return the gl_framebuffer object which corresponds to the given
196 * framebuffer target, such as GL_DRAW_FRAMEBUFFER.
197 * Check support for GL_EXT_framebuffer_blit to determine if certain
198 * targets are legal.
199 * \return gl_framebuffer pointer or NULL if target is illegal
200 */
201 static struct gl_framebuffer *
202 get_framebuffer_target(struct gl_context *ctx, GLenum target)
203 {
204 bool have_fb_blit = _mesa_is_gles3(ctx) || _mesa_is_desktop_gl(ctx);
205 switch (target) {
206 case GL_DRAW_FRAMEBUFFER:
207 return have_fb_blit ? ctx->DrawBuffer : NULL;
208 case GL_READ_FRAMEBUFFER:
209 return have_fb_blit ? ctx->ReadBuffer : NULL;
210 case GL_FRAMEBUFFER_EXT:
211 return ctx->DrawBuffer;
212 default:
213 return NULL;
214 }
215 }
216
217
218 /**
219 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
220 * gl_renderbuffer_attachment object.
221 * This function is only used for user-created FB objects, not the
222 * default / window-system FB object.
223 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
224 * the depth buffer attachment point.
225 */
226 static struct gl_renderbuffer_attachment *
227 get_attachment(struct gl_context *ctx, struct gl_framebuffer *fb,
228 GLenum attachment)
229 {
230 GLuint i;
231
232 assert(_mesa_is_user_fbo(fb));
233
234 switch (attachment) {
235 case GL_COLOR_ATTACHMENT0_EXT:
236 case GL_COLOR_ATTACHMENT1_EXT:
237 case GL_COLOR_ATTACHMENT2_EXT:
238 case GL_COLOR_ATTACHMENT3_EXT:
239 case GL_COLOR_ATTACHMENT4_EXT:
240 case GL_COLOR_ATTACHMENT5_EXT:
241 case GL_COLOR_ATTACHMENT6_EXT:
242 case GL_COLOR_ATTACHMENT7_EXT:
243 case GL_COLOR_ATTACHMENT8_EXT:
244 case GL_COLOR_ATTACHMENT9_EXT:
245 case GL_COLOR_ATTACHMENT10_EXT:
246 case GL_COLOR_ATTACHMENT11_EXT:
247 case GL_COLOR_ATTACHMENT12_EXT:
248 case GL_COLOR_ATTACHMENT13_EXT:
249 case GL_COLOR_ATTACHMENT14_EXT:
250 case GL_COLOR_ATTACHMENT15_EXT:
251 /* Only OpenGL ES 1.x forbids color attachments other than
252 * GL_COLOR_ATTACHMENT0. For all other APIs the limit set by the
253 * hardware is used.
254 */
255 i = attachment - GL_COLOR_ATTACHMENT0_EXT;
256 if (i >= ctx->Const.MaxColorAttachments
257 || (i > 0 && ctx->API == API_OPENGLES)) {
258 return NULL;
259 }
260 return &fb->Attachment[BUFFER_COLOR0 + i];
261 case GL_DEPTH_STENCIL_ATTACHMENT:
262 if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
263 return NULL;
264 /* fall-through */
265 case GL_DEPTH_ATTACHMENT_EXT:
266 return &fb->Attachment[BUFFER_DEPTH];
267 case GL_STENCIL_ATTACHMENT_EXT:
268 return &fb->Attachment[BUFFER_STENCIL];
269 default:
270 return NULL;
271 }
272 }
273
274
275 /**
276 * As above, but only used for getting attachments of the default /
277 * window-system framebuffer (not user-created framebuffer objects).
278 */
279 static struct gl_renderbuffer_attachment *
280 _mesa_get_fb0_attachment(struct gl_context *ctx, struct gl_framebuffer *fb,
281 GLenum attachment)
282 {
283 assert(_mesa_is_winsys_fbo(fb));
284
285 if (_mesa_is_gles3(ctx)) {
286 assert(attachment == GL_BACK ||
287 attachment == GL_DEPTH ||
288 attachment == GL_STENCIL);
289 switch (attachment) {
290 case GL_BACK:
291 /* Since there is no stereo rendering in ES 3.0, only return the
292 * LEFT bits.
293 */
294 if (ctx->DrawBuffer->Visual.doubleBufferMode)
295 return &fb->Attachment[BUFFER_BACK_LEFT];
296 return &fb->Attachment[BUFFER_FRONT_LEFT];
297 case GL_DEPTH:
298 return &fb->Attachment[BUFFER_DEPTH];
299 case GL_STENCIL:
300 return &fb->Attachment[BUFFER_STENCIL];
301 }
302 }
303
304 switch (attachment) {
305 case GL_FRONT_LEFT:
306 return &fb->Attachment[BUFFER_FRONT_LEFT];
307 case GL_FRONT_RIGHT:
308 return &fb->Attachment[BUFFER_FRONT_RIGHT];
309 case GL_BACK_LEFT:
310 return &fb->Attachment[BUFFER_BACK_LEFT];
311 case GL_BACK_RIGHT:
312 return &fb->Attachment[BUFFER_BACK_RIGHT];
313 case GL_AUX0:
314 if (fb->Visual.numAuxBuffers == 1) {
315 return &fb->Attachment[BUFFER_AUX0];
316 }
317 return NULL;
318
319 /* Page 336 (page 352 of the PDF) of the OpenGL 3.0 spec says:
320 *
321 * "If the default framebuffer is bound to target, then attachment must
322 * be one of FRONT LEFT, FRONT RIGHT, BACK LEFT, BACK RIGHT, or AUXi,
323 * identifying a color buffer; DEPTH, identifying the depth buffer; or
324 * STENCIL, identifying the stencil buffer."
325 *
326 * Revision #34 of the ARB_framebuffer_object spec has essentially the same
327 * language. However, revision #33 of the ARB_framebuffer_object spec
328 * says:
329 *
330 * "If the default framebuffer is bound to <target>, then <attachment>
331 * must be one of FRONT_LEFT, FRONT_RIGHT, BACK_LEFT, BACK_RIGHT, AUXi,
332 * DEPTH_BUFFER, or STENCIL_BUFFER, identifying a color buffer, the
333 * depth buffer, or the stencil buffer, and <pname> may be
334 * FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE or
335 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME."
336 *
337 * The enum values for DEPTH_BUFFER and STENCIL_BUFFER have been removed
338 * from glext.h, so shipping apps should not use those values.
339 *
340 * Note that neither EXT_framebuffer_object nor OES_framebuffer_object
341 * support queries of the window system FBO.
342 */
343 case GL_DEPTH:
344 return &fb->Attachment[BUFFER_DEPTH];
345 case GL_STENCIL:
346 return &fb->Attachment[BUFFER_STENCIL];
347 default:
348 return NULL;
349 }
350 }
351
352
353
354 /**
355 * Remove any texture or renderbuffer attached to the given attachment
356 * point. Update reference counts, etc.
357 */
358 static void
359 remove_attachment(struct gl_context *ctx,
360 struct gl_renderbuffer_attachment *att)
361 {
362 struct gl_renderbuffer *rb = att->Renderbuffer;
363
364 /* tell driver that we're done rendering to this texture. */
365 if (rb && rb->NeedsFinishRenderTexture)
366 ctx->Driver.FinishRenderTexture(ctx, rb);
367
368 if (att->Type == GL_TEXTURE) {
369 assert(att->Texture);
370 _mesa_reference_texobj(&att->Texture, NULL); /* unbind */
371 assert(!att->Texture);
372 }
373 if (att->Type == GL_TEXTURE || att->Type == GL_RENDERBUFFER_EXT) {
374 assert(!att->Texture);
375 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL); /* unbind */
376 assert(!att->Renderbuffer);
377 }
378 att->Type = GL_NONE;
379 att->Complete = GL_TRUE;
380 }
381
382 /**
383 * Verify a couple error conditions that will lead to an incomplete FBO and
384 * may cause problems for the driver's RenderTexture path.
385 */
386 static bool
387 driver_RenderTexture_is_safe(const struct gl_renderbuffer_attachment *att)
388 {
389 const struct gl_texture_image *const texImage =
390 att->Texture->Image[att->CubeMapFace][att->TextureLevel];
391
392 if (!texImage ||
393 texImage->Width == 0 || texImage->Height == 0 || texImage->Depth == 0)
394 return false;
395
396 if ((texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY
397 && att->Zoffset >= texImage->Height)
398 || (texImage->TexObject->Target != GL_TEXTURE_1D_ARRAY
399 && att->Zoffset >= texImage->Depth))
400 return false;
401
402 return true;
403 }
404
405 /**
406 * Create a renderbuffer which will be set up by the driver to wrap the
407 * texture image slice.
408 *
409 * By using a gl_renderbuffer (like user-allocated renderbuffers), drivers get
410 * to share most of their framebuffer rendering code between winsys,
411 * renderbuffer, and texture attachments.
412 *
413 * The allocated renderbuffer uses a non-zero Name so that drivers can check
414 * it for determining vertical orientation, but we use ~0 to make it fairly
415 * unambiguous with actual user (non-texture) renderbuffers.
416 */
417 void
418 _mesa_update_texture_renderbuffer(struct gl_context *ctx,
419 struct gl_framebuffer *fb,
420 struct gl_renderbuffer_attachment *att)
421 {
422 struct gl_texture_image *texImage;
423 struct gl_renderbuffer *rb;
424
425 texImage = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
426
427 rb = att->Renderbuffer;
428 if (!rb) {
429 rb = ctx->Driver.NewRenderbuffer(ctx, ~0);
430 if (!rb) {
431 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture()");
432 return;
433 }
434 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
435
436 /* This can't get called on a texture renderbuffer, so set it to NULL
437 * for clarity compared to user renderbuffers.
438 */
439 rb->AllocStorage = NULL;
440
441 rb->NeedsFinishRenderTexture = ctx->Driver.FinishRenderTexture != NULL;
442 }
443
444 if (!texImage)
445 return;
446
447 rb->_BaseFormat = texImage->_BaseFormat;
448 rb->Format = texImage->TexFormat;
449 rb->InternalFormat = texImage->InternalFormat;
450 rb->Width = texImage->Width2;
451 rb->Height = texImage->Height2;
452 rb->Depth = texImage->Depth2;
453 rb->NumSamples = texImage->NumSamples;
454 rb->TexImage = texImage;
455
456 if (driver_RenderTexture_is_safe(att))
457 ctx->Driver.RenderTexture(ctx, fb, att);
458 }
459
460 /**
461 * Bind a texture object to an attachment point.
462 * The previous binding, if any, will be removed first.
463 */
464 static void
465 set_texture_attachment(struct gl_context *ctx,
466 struct gl_framebuffer *fb,
467 struct gl_renderbuffer_attachment *att,
468 struct gl_texture_object *texObj,
469 GLenum texTarget, GLuint level, GLuint layer,
470 GLboolean layered)
471 {
472 struct gl_renderbuffer *rb = att->Renderbuffer;
473
474 if (rb && rb->NeedsFinishRenderTexture)
475 ctx->Driver.FinishRenderTexture(ctx, rb);
476
477 if (att->Texture == texObj) {
478 /* re-attaching same texture */
479 assert(att->Type == GL_TEXTURE);
480 }
481 else {
482 /* new attachment */
483 remove_attachment(ctx, att);
484 att->Type = GL_TEXTURE;
485 assert(!att->Texture);
486 _mesa_reference_texobj(&att->Texture, texObj);
487 }
488 invalidate_framebuffer(fb);
489
490 /* always update these fields */
491 att->TextureLevel = level;
492 att->CubeMapFace = _mesa_tex_target_to_face(texTarget);
493 att->Zoffset = layer;
494 att->Layered = layered;
495 att->Complete = GL_FALSE;
496
497 _mesa_update_texture_renderbuffer(ctx, fb, att);
498 }
499
500
501 /**
502 * Bind a renderbuffer to an attachment point.
503 * The previous binding, if any, will be removed first.
504 */
505 static void
506 set_renderbuffer_attachment(struct gl_context *ctx,
507 struct gl_renderbuffer_attachment *att,
508 struct gl_renderbuffer *rb)
509 {
510 /* XXX check if re-doing same attachment, exit early */
511 remove_attachment(ctx, att);
512 att->Type = GL_RENDERBUFFER_EXT;
513 att->Texture = NULL; /* just to be safe */
514 att->Layered = GL_FALSE;
515 att->Complete = GL_FALSE;
516 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
517 }
518
519
520 /**
521 * Fallback for ctx->Driver.FramebufferRenderbuffer()
522 * Attach a renderbuffer object to a framebuffer object.
523 */
524 void
525 _mesa_FramebufferRenderbuffer_sw(struct gl_context *ctx,
526 struct gl_framebuffer *fb,
527 GLenum attachment,
528 struct gl_renderbuffer *rb)
529 {
530 struct gl_renderbuffer_attachment *att;
531
532 mtx_lock(&fb->Mutex);
533
534 att = get_attachment(ctx, fb, attachment);
535 assert(att);
536 if (rb) {
537 set_renderbuffer_attachment(ctx, att, rb);
538 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
539 /* do stencil attachment here (depth already done above) */
540 att = get_attachment(ctx, fb, GL_STENCIL_ATTACHMENT_EXT);
541 assert(att);
542 set_renderbuffer_attachment(ctx, att, rb);
543 }
544 rb->AttachedAnytime = GL_TRUE;
545 }
546 else {
547 remove_attachment(ctx, att);
548 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
549 /* detach stencil (depth was detached above) */
550 att = get_attachment(ctx, fb, GL_STENCIL_ATTACHMENT_EXT);
551 assert(att);
552 remove_attachment(ctx, att);
553 }
554 }
555
556 invalidate_framebuffer(fb);
557
558 mtx_unlock(&fb->Mutex);
559 }
560
561
562 /**
563 * Fallback for ctx->Driver.ValidateFramebuffer()
564 * Check if the renderbuffer's formats are supported by the software
565 * renderer.
566 * Drivers should probably override this.
567 */
568 void
569 _mesa_validate_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
570 {
571 gl_buffer_index buf;
572 for (buf = 0; buf < BUFFER_COUNT; buf++) {
573 const struct gl_renderbuffer *rb = fb->Attachment[buf].Renderbuffer;
574 if (rb) {
575 switch (rb->_BaseFormat) {
576 case GL_ALPHA:
577 case GL_LUMINANCE_ALPHA:
578 case GL_LUMINANCE:
579 case GL_INTENSITY:
580 case GL_RED:
581 case GL_RG:
582 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
583 return;
584
585 default:
586 switch (rb->Format) {
587 /* XXX This list is likely incomplete. */
588 case MESA_FORMAT_R9G9B9E5_FLOAT:
589 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
590 return;
591 default:;
592 /* render buffer format is supported by software rendering */
593 }
594 }
595 }
596 }
597 }
598
599
600 /**
601 * Return true if the framebuffer has a combined depth/stencil
602 * renderbuffer attached.
603 */
604 GLboolean
605 _mesa_has_depthstencil_combined(const struct gl_framebuffer *fb)
606 {
607 const struct gl_renderbuffer_attachment *depth =
608 &fb->Attachment[BUFFER_DEPTH];
609 const struct gl_renderbuffer_attachment *stencil =
610 &fb->Attachment[BUFFER_STENCIL];
611
612 if (depth->Type == stencil->Type) {
613 if (depth->Type == GL_RENDERBUFFER_EXT &&
614 depth->Renderbuffer == stencil->Renderbuffer)
615 return GL_TRUE;
616
617 if (depth->Type == GL_TEXTURE &&
618 depth->Texture == stencil->Texture)
619 return GL_TRUE;
620 }
621
622 return GL_FALSE;
623 }
624
625
626 /**
627 * For debug only.
628 */
629 static void
630 att_incomplete(const char *msg)
631 {
632 if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_FBO) {
633 _mesa_debug(NULL, "attachment incomplete: %s\n", msg);
634 }
635 }
636
637
638 /**
639 * For debug only.
640 */
641 static void
642 fbo_incomplete(struct gl_context *ctx, const char *msg, int index)
643 {
644 static GLuint msg_id;
645
646 _mesa_gl_debug(ctx, &msg_id,
647 MESA_DEBUG_SOURCE_API,
648 MESA_DEBUG_TYPE_OTHER,
649 MESA_DEBUG_SEVERITY_MEDIUM,
650 "FBO incomplete: %s [%d]\n", msg, index);
651
652 if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_FBO) {
653 _mesa_debug(NULL, "FBO Incomplete: %s [%d]\n", msg, index);
654 }
655 }
656
657
658 /**
659 * Is the given base format a legal format for a color renderbuffer?
660 */
661 GLboolean
662 _mesa_is_legal_color_format(const struct gl_context *ctx, GLenum baseFormat)
663 {
664 switch (baseFormat) {
665 case GL_RGB:
666 case GL_RGBA:
667 return GL_TRUE;
668 case GL_LUMINANCE:
669 case GL_LUMINANCE_ALPHA:
670 case GL_INTENSITY:
671 case GL_ALPHA:
672 return ctx->API == API_OPENGL_COMPAT &&
673 ctx->Extensions.ARB_framebuffer_object;
674 case GL_RED:
675 case GL_RG:
676 return ctx->Extensions.ARB_texture_rg;
677 default:
678 return GL_FALSE;
679 }
680 }
681
682
683 /**
684 * Is the given base format a legal format for a color renderbuffer?
685 */
686 static GLboolean
687 is_format_color_renderable(const struct gl_context *ctx, mesa_format format,
688 GLenum internalFormat)
689 {
690 const GLenum baseFormat =
691 _mesa_get_format_base_format(format);
692 GLboolean valid;
693
694 valid = _mesa_is_legal_color_format(ctx, baseFormat);
695 if (!valid || _mesa_is_desktop_gl(ctx)) {
696 return valid;
697 }
698
699 /* Reject additional cases for GLES */
700 switch (internalFormat) {
701 case GL_RGBA8_SNORM:
702 case GL_RGB32F:
703 case GL_RGB32I:
704 case GL_RGB32UI:
705 case GL_RGB16F:
706 case GL_RGB16I:
707 case GL_RGB16UI:
708 case GL_RGB8_SNORM:
709 case GL_RGB8I:
710 case GL_RGB8UI:
711 case GL_SRGB8:
712 case GL_RGB9_E5:
713 case GL_RG8_SNORM:
714 case GL_R8_SNORM:
715 return GL_FALSE;
716 default:
717 break;
718 }
719
720 if (format == MESA_FORMAT_B10G10R10A2_UNORM &&
721 internalFormat != GL_RGB10_A2) {
722 return GL_FALSE;
723 }
724
725 return GL_TRUE;
726 }
727
728
729 /**
730 * Is the given base format a legal format for a depth/stencil renderbuffer?
731 */
732 static GLboolean
733 is_legal_depth_format(const struct gl_context *ctx, GLenum baseFormat)
734 {
735 switch (baseFormat) {
736 case GL_DEPTH_COMPONENT:
737 case GL_DEPTH_STENCIL_EXT:
738 return GL_TRUE;
739 default:
740 return GL_FALSE;
741 }
742 }
743
744
745 /**
746 * Test if an attachment point is complete and update its Complete field.
747 * \param format if GL_COLOR, this is a color attachment point,
748 * if GL_DEPTH, this is a depth component attachment point,
749 * if GL_STENCIL, this is a stencil component attachment point.
750 */
751 static void
752 test_attachment_completeness(const struct gl_context *ctx, GLenum format,
753 struct gl_renderbuffer_attachment *att)
754 {
755 assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
756
757 /* assume complete */
758 att->Complete = GL_TRUE;
759
760 /* Look for reasons why the attachment might be incomplete */
761 if (att->Type == GL_TEXTURE) {
762 const struct gl_texture_object *texObj = att->Texture;
763 struct gl_texture_image *texImage;
764 GLenum baseFormat;
765
766 if (!texObj) {
767 att_incomplete("no texobj");
768 att->Complete = GL_FALSE;
769 return;
770 }
771
772 texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
773 if (!texImage) {
774 att_incomplete("no teximage");
775 att->Complete = GL_FALSE;
776 return;
777 }
778 if (texImage->Width < 1 || texImage->Height < 1) {
779 att_incomplete("teximage width/height=0");
780 att->Complete = GL_FALSE;
781 return;
782 }
783
784 switch (texObj->Target) {
785 case GL_TEXTURE_3D:
786 if (att->Zoffset >= texImage->Depth) {
787 att_incomplete("bad z offset");
788 att->Complete = GL_FALSE;
789 return;
790 }
791 break;
792 case GL_TEXTURE_1D_ARRAY:
793 if (att->Zoffset >= texImage->Height) {
794 att_incomplete("bad 1D-array layer");
795 att->Complete = GL_FALSE;
796 return;
797 }
798 break;
799 case GL_TEXTURE_2D_ARRAY:
800 if (att->Zoffset >= texImage->Depth) {
801 att_incomplete("bad 2D-array layer");
802 att->Complete = GL_FALSE;
803 return;
804 }
805 break;
806 case GL_TEXTURE_CUBE_MAP_ARRAY:
807 if (att->Zoffset >= texImage->Depth) {
808 att_incomplete("bad cube-array layer");
809 att->Complete = GL_FALSE;
810 return;
811 }
812 break;
813 }
814
815 baseFormat = texImage->_BaseFormat;
816
817 if (format == GL_COLOR) {
818 if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
819 att_incomplete("bad format");
820 att->Complete = GL_FALSE;
821 return;
822 }
823 if (_mesa_is_format_compressed(texImage->TexFormat)) {
824 att_incomplete("compressed internalformat");
825 att->Complete = GL_FALSE;
826 return;
827 }
828
829 /* OES_texture_float allows creation and use of floating point
830 * textures with GL_FLOAT, GL_HALF_FLOAT but it does not allow
831 * these textures to be used as a render target, this is done via
832 * GL_EXT_color_buffer(_half)_float with set of new sized types.
833 */
834 if (_mesa_is_gles(ctx) && (texImage->TexObject->_IsFloat ||
835 texImage->TexObject->_IsHalfFloat)) {
836 att_incomplete("bad internal format");
837 att->Complete = GL_FALSE;
838 return;
839 }
840 }
841 else if (format == GL_DEPTH) {
842 if (baseFormat == GL_DEPTH_COMPONENT) {
843 /* OK */
844 }
845 else if (ctx->Extensions.ARB_depth_texture &&
846 baseFormat == GL_DEPTH_STENCIL) {
847 /* OK */
848 }
849 else {
850 att->Complete = GL_FALSE;
851 att_incomplete("bad depth format");
852 return;
853 }
854 }
855 else {
856 assert(format == GL_STENCIL);
857 if (ctx->Extensions.ARB_depth_texture &&
858 baseFormat == GL_DEPTH_STENCIL) {
859 /* OK */
860 } else if (ctx->Extensions.ARB_texture_stencil8 &&
861 baseFormat == GL_STENCIL_INDEX) {
862 /* OK */
863 } else {
864 /* no such thing as stencil-only textures */
865 att_incomplete("illegal stencil texture");
866 att->Complete = GL_FALSE;
867 return;
868 }
869 }
870 }
871 else if (att->Type == GL_RENDERBUFFER_EXT) {
872 const GLenum baseFormat = att->Renderbuffer->_BaseFormat;
873
874 assert(att->Renderbuffer);
875 if (!att->Renderbuffer->InternalFormat ||
876 att->Renderbuffer->Width < 1 ||
877 att->Renderbuffer->Height < 1) {
878 att_incomplete("0x0 renderbuffer");
879 att->Complete = GL_FALSE;
880 return;
881 }
882 if (format == GL_COLOR) {
883 if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
884 att_incomplete("bad renderbuffer color format");
885 att->Complete = GL_FALSE;
886 return;
887 }
888 }
889 else if (format == GL_DEPTH) {
890 if (baseFormat == GL_DEPTH_COMPONENT) {
891 /* OK */
892 }
893 else if (baseFormat == GL_DEPTH_STENCIL) {
894 /* OK */
895 }
896 else {
897 att_incomplete("bad renderbuffer depth format");
898 att->Complete = GL_FALSE;
899 return;
900 }
901 }
902 else {
903 assert(format == GL_STENCIL);
904 if (baseFormat == GL_STENCIL_INDEX ||
905 baseFormat == GL_DEPTH_STENCIL) {
906 /* OK */
907 }
908 else {
909 att->Complete = GL_FALSE;
910 att_incomplete("bad renderbuffer stencil format");
911 return;
912 }
913 }
914 }
915 else {
916 assert(att->Type == GL_NONE);
917 /* complete */
918 return;
919 }
920 }
921
922
923 /**
924 * Test if the given framebuffer object is complete and update its
925 * Status field with the results.
926 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
927 * driver to make hardware-specific validation/completeness checks.
928 * Also update the framebuffer's Width and Height fields if the
929 * framebuffer is complete.
930 */
931 void
932 _mesa_test_framebuffer_completeness(struct gl_context *ctx,
933 struct gl_framebuffer *fb)
934 {
935 GLuint numImages;
936 GLenum intFormat = GL_NONE; /* color buffers' internal format */
937 GLuint minWidth = ~0, minHeight = ~0, maxWidth = 0, maxHeight = 0;
938 GLint numSamples = -1;
939 GLint fixedSampleLocations = -1;
940 GLint i;
941 GLuint j;
942 /* Covers max_layer_count, is_layered, and layer_tex_target */
943 bool layer_info_valid = false;
944 GLuint max_layer_count = 0, att_layer_count;
945 bool is_layered = false;
946 GLenum layer_tex_target = 0;
947 bool has_depth_attachment = false;
948 bool has_stencil_attachment = false;
949
950 assert(_mesa_is_user_fbo(fb));
951
952 /* we're changing framebuffer fields here */
953 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
954
955 numImages = 0;
956 fb->Width = 0;
957 fb->Height = 0;
958 fb->_AllColorBuffersFixedPoint = GL_TRUE;
959 fb->_HasSNormOrFloatColorBuffer = GL_FALSE;
960 fb->_HasAttachments = true;
961
962 /* Start at -2 to more easily loop over all attachment points.
963 * -2: depth buffer
964 * -1: stencil buffer
965 * >=0: color buffer
966 */
967 for (i = -2; i < (GLint) ctx->Const.MaxColorAttachments; i++) {
968 struct gl_renderbuffer_attachment *att;
969 GLenum f;
970 mesa_format attFormat;
971 GLenum att_tex_target = GL_NONE;
972
973 /*
974 * XXX for ARB_fbo, only check color buffers that are named by
975 * GL_READ_BUFFER and GL_DRAW_BUFFERi.
976 */
977
978 /* check for attachment completeness
979 */
980 if (i == -2) {
981 att = &fb->Attachment[BUFFER_DEPTH];
982 test_attachment_completeness(ctx, GL_DEPTH, att);
983 if (!att->Complete) {
984 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
985 fbo_incomplete(ctx, "depth attachment incomplete", -1);
986 return;
987 } else if (att->Type != GL_NONE) {
988 has_depth_attachment = true;
989 }
990 }
991 else if (i == -1) {
992 att = &fb->Attachment[BUFFER_STENCIL];
993 test_attachment_completeness(ctx, GL_STENCIL, att);
994 if (!att->Complete) {
995 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
996 fbo_incomplete(ctx, "stencil attachment incomplete", -1);
997 return;
998 } else if (att->Type != GL_NONE) {
999 has_stencil_attachment = true;
1000 }
1001 }
1002 else {
1003 att = &fb->Attachment[BUFFER_COLOR0 + i];
1004 test_attachment_completeness(ctx, GL_COLOR, att);
1005 if (!att->Complete) {
1006 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
1007 fbo_incomplete(ctx, "color attachment incomplete", i);
1008 return;
1009 }
1010 }
1011
1012 /* get width, height, format of the renderbuffer/texture
1013 */
1014 if (att->Type == GL_TEXTURE) {
1015 const struct gl_texture_image *texImg = att->Renderbuffer->TexImage;
1016 att_tex_target = att->Texture->Target;
1017 minWidth = MIN2(minWidth, texImg->Width);
1018 maxWidth = MAX2(maxWidth, texImg->Width);
1019 minHeight = MIN2(minHeight, texImg->Height);
1020 maxHeight = MAX2(maxHeight, texImg->Height);
1021 f = texImg->_BaseFormat;
1022 attFormat = texImg->TexFormat;
1023 numImages++;
1024
1025 if (!is_format_color_renderable(ctx, attFormat,
1026 texImg->InternalFormat) &&
1027 !is_legal_depth_format(ctx, f) &&
1028 f != GL_STENCIL_INDEX) {
1029 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
1030 fbo_incomplete(ctx, "texture attachment incomplete", -1);
1031 return;
1032 }
1033
1034 if (numSamples < 0)
1035 numSamples = texImg->NumSamples;
1036 else if (numSamples != texImg->NumSamples) {
1037 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
1038 fbo_incomplete(ctx, "inconsistent sample count", -1);
1039 return;
1040 }
1041
1042 if (fixedSampleLocations < 0)
1043 fixedSampleLocations = texImg->FixedSampleLocations;
1044 else if (fixedSampleLocations != texImg->FixedSampleLocations) {
1045 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
1046 fbo_incomplete(ctx, "inconsistent fixed sample locations", -1);
1047 return;
1048 }
1049 }
1050 else if (att->Type == GL_RENDERBUFFER_EXT) {
1051 minWidth = MIN2(minWidth, att->Renderbuffer->Width);
1052 maxWidth = MAX2(minWidth, att->Renderbuffer->Width);
1053 minHeight = MIN2(minHeight, att->Renderbuffer->Height);
1054 maxHeight = MAX2(minHeight, att->Renderbuffer->Height);
1055 f = att->Renderbuffer->InternalFormat;
1056 attFormat = att->Renderbuffer->Format;
1057 numImages++;
1058
1059 if (numSamples < 0)
1060 numSamples = att->Renderbuffer->NumSamples;
1061 else if (numSamples != att->Renderbuffer->NumSamples) {
1062 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
1063 fbo_incomplete(ctx, "inconsistent sample count", -1);
1064 return;
1065 }
1066
1067 /* RENDERBUFFER has fixedSampleLocations implicitly true */
1068 if (fixedSampleLocations < 0)
1069 fixedSampleLocations = GL_TRUE;
1070 else if (fixedSampleLocations != GL_TRUE) {
1071 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
1072 fbo_incomplete(ctx, "inconsistent fixed sample locations", -1);
1073 return;
1074 }
1075 }
1076 else {
1077 assert(att->Type == GL_NONE);
1078 continue;
1079 }
1080
1081 /* check if integer color */
1082 fb->_IntegerColor = _mesa_is_format_integer_color(attFormat);
1083
1084 /* Update _AllColorBuffersFixedPoint and _HasSNormOrFloatColorBuffer. */
1085 if (i >= 0) {
1086 GLenum type = _mesa_get_format_datatype(attFormat);
1087
1088 fb->_AllColorBuffersFixedPoint =
1089 fb->_AllColorBuffersFixedPoint &&
1090 (type == GL_UNSIGNED_NORMALIZED || type == GL_SIGNED_NORMALIZED);
1091
1092 fb->_HasSNormOrFloatColorBuffer =
1093 fb->_HasSNormOrFloatColorBuffer ||
1094 type == GL_SIGNED_NORMALIZED || type == GL_FLOAT;
1095 }
1096
1097 /* Error-check width, height, format */
1098 if (numImages == 1) {
1099 /* save format */
1100 if (i >= 0) {
1101 intFormat = f;
1102 }
1103 }
1104 else {
1105 if (!ctx->Extensions.ARB_framebuffer_object) {
1106 /* check that width, height, format are same */
1107 if (minWidth != maxWidth || minHeight != maxHeight) {
1108 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
1109 fbo_incomplete(ctx, "width or height mismatch", -1);
1110 return;
1111 }
1112 /* check that all color buffers are the same format */
1113 if (intFormat != GL_NONE && f != intFormat) {
1114 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
1115 fbo_incomplete(ctx, "format mismatch", -1);
1116 return;
1117 }
1118 }
1119 }
1120
1121 /* Check that the format is valid. (MESA_FORMAT_NONE means unsupported)
1122 */
1123 if (att->Type == GL_RENDERBUFFER &&
1124 att->Renderbuffer->Format == MESA_FORMAT_NONE) {
1125 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
1126 fbo_incomplete(ctx, "unsupported renderbuffer format", i);
1127 return;
1128 }
1129
1130 /* Check that layered rendering is consistent. */
1131 if (att->Layered) {
1132 if (att_tex_target == GL_TEXTURE_CUBE_MAP)
1133 att_layer_count = 6;
1134 else if (att_tex_target == GL_TEXTURE_1D_ARRAY)
1135 att_layer_count = att->Renderbuffer->Height;
1136 else
1137 att_layer_count = att->Renderbuffer->Depth;
1138 } else {
1139 att_layer_count = 0;
1140 }
1141 if (!layer_info_valid) {
1142 is_layered = att->Layered;
1143 max_layer_count = att_layer_count;
1144 layer_tex_target = att_tex_target;
1145 layer_info_valid = true;
1146 } else if (max_layer_count > 0 && layer_tex_target != att_tex_target) {
1147 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS;
1148 fbo_incomplete(ctx, "layered framebuffer has mismatched targets", i);
1149 return;
1150 } else if (is_layered != att->Layered) {
1151 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS;
1152 fbo_incomplete(ctx,
1153 "framebuffer attachment layer mode is inconsistent",
1154 i);
1155 return;
1156 } else if (att_layer_count > max_layer_count) {
1157 max_layer_count = att_layer_count;
1158 }
1159
1160 /*
1161 * The extension GL_ARB_framebuffer_no_attachments places additional
1162 * requirement on each attachment. Those additional requirements are
1163 * tighter that those of previous versions of GL. In interest of better
1164 * compatibility, we will not enforce these restrictions. For the record
1165 * those additional restrictions are quoted below:
1166 *
1167 * "The width and height of image are greater than zero and less than or
1168 * equal to the values of the implementation-dependent limits
1169 * MAX_FRAMEBUFFER_WIDTH and MAX_FRAMEBUFFER_HEIGHT, respectively."
1170 *
1171 * "If <image> is a three-dimensional texture or a one- or two-dimensional
1172 * array texture and the attachment is layered, the depth or layer count
1173 * of the texture is less than or equal to the implementation-dependent
1174 * limit MAX_FRAMEBUFFER_LAYERS."
1175 *
1176 * "If image has multiple samples, its sample count is less than or equal
1177 * to the value of the implementation-dependent limit
1178 * MAX_FRAMEBUFFER_SAMPLES."
1179 *
1180 * The same requirements are also in place for GL 4.5,
1181 * Section 9.4.1 "Framebuffer Attachment Completeness", pg 310-311
1182 */
1183 }
1184
1185 fb->MaxNumLayers = max_layer_count;
1186
1187 if (numImages == 0) {
1188 fb->_HasAttachments = false;
1189
1190 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
1191 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
1192 fbo_incomplete(ctx, "no attachments", -1);
1193 return;
1194 }
1195
1196 if (fb->DefaultGeometry.Width == 0 || fb->DefaultGeometry.Height == 0) {
1197 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
1198 fbo_incomplete(ctx, "no attachments and default width or height is 0", -1);
1199 return;
1200 }
1201 }
1202
1203 if (_mesa_is_desktop_gl(ctx) && !ctx->Extensions.ARB_ES2_compatibility) {
1204 /* Check that all DrawBuffers are present */
1205 for (j = 0; j < ctx->Const.MaxDrawBuffers; j++) {
1206 if (fb->ColorDrawBuffer[j] != GL_NONE) {
1207 const struct gl_renderbuffer_attachment *att
1208 = get_attachment(ctx, fb, fb->ColorDrawBuffer[j]);
1209 assert(att);
1210 if (att->Type == GL_NONE) {
1211 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
1212 fbo_incomplete(ctx, "missing drawbuffer", j);
1213 return;
1214 }
1215 }
1216 }
1217
1218 /* Check that the ReadBuffer is present */
1219 if (fb->ColorReadBuffer != GL_NONE) {
1220 const struct gl_renderbuffer_attachment *att
1221 = get_attachment(ctx, fb, fb->ColorReadBuffer);
1222 assert(att);
1223 if (att->Type == GL_NONE) {
1224 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
1225 fbo_incomplete(ctx, "missing readbuffer", -1);
1226 return;
1227 }
1228 }
1229 }
1230
1231 /* The OpenGL ES3 spec, in chapter 9.4. FRAMEBUFFER COMPLETENESS, says:
1232 *
1233 * "Depth and stencil attachments, if present, are the same image."
1234 *
1235 * This restriction is not present in the OpenGL ES2 spec.
1236 */
1237 if (_mesa_is_gles3(ctx) &&
1238 has_stencil_attachment && has_depth_attachment &&
1239 !_mesa_has_depthstencil_combined(fb)) {
1240 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
1241 fbo_incomplete(ctx, "Depth and stencil attachments must be the same image", -1);
1242 return;
1243 }
1244
1245 /* Provisionally set status = COMPLETE ... */
1246 fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
1247
1248 /* ... but the driver may say the FB is incomplete.
1249 * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
1250 * if anything.
1251 */
1252 if (ctx->Driver.ValidateFramebuffer) {
1253 ctx->Driver.ValidateFramebuffer(ctx, fb);
1254 if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
1255 fbo_incomplete(ctx, "driver marked FBO as incomplete", -1);
1256 return;
1257 }
1258 }
1259
1260 /*
1261 * Note that if ARB_framebuffer_object is supported and the attached
1262 * renderbuffers/textures are different sizes, the framebuffer
1263 * width/height will be set to the smallest width/height.
1264 */
1265 if (numImages != 0) {
1266 fb->Width = minWidth;
1267 fb->Height = minHeight;
1268 }
1269
1270 /* finally, update the visual info for the framebuffer */
1271 _mesa_update_framebuffer_visual(ctx, fb);
1272 }
1273
1274
1275 GLboolean GLAPIENTRY
1276 _mesa_IsRenderbuffer(GLuint renderbuffer)
1277 {
1278 GET_CURRENT_CONTEXT(ctx);
1279 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1280 if (renderbuffer) {
1281 struct gl_renderbuffer *rb =
1282 _mesa_lookup_renderbuffer(ctx, renderbuffer);
1283 if (rb != NULL && rb != &DummyRenderbuffer)
1284 return GL_TRUE;
1285 }
1286 return GL_FALSE;
1287 }
1288
1289
1290 static struct gl_renderbuffer *
1291 allocate_renderbuffer_locked(struct gl_context *ctx, GLuint renderbuffer,
1292 const char *func)
1293 {
1294 struct gl_renderbuffer *newRb;
1295
1296 /* create new renderbuffer object */
1297 newRb = ctx->Driver.NewRenderbuffer(ctx, renderbuffer);
1298 if (!newRb) {
1299 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
1300 return NULL;
1301 }
1302 assert(newRb->AllocStorage);
1303 _mesa_HashInsertLocked(ctx->Shared->RenderBuffers, renderbuffer, newRb);
1304 newRb->RefCount = 1; /* referenced by hash table */
1305
1306 return newRb;
1307 }
1308
1309
1310 static void
1311 bind_renderbuffer(GLenum target, GLuint renderbuffer, bool allow_user_names)
1312 {
1313 struct gl_renderbuffer *newRb;
1314 GET_CURRENT_CONTEXT(ctx);
1315
1316 if (target != GL_RENDERBUFFER_EXT) {
1317 _mesa_error(ctx, GL_INVALID_ENUM, "glBindRenderbufferEXT(target)");
1318 return;
1319 }
1320
1321 /* No need to flush here since the render buffer binding has no
1322 * effect on rendering state.
1323 */
1324
1325 if (renderbuffer) {
1326 newRb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
1327 if (newRb == &DummyRenderbuffer) {
1328 /* ID was reserved, but no real renderbuffer object made yet */
1329 newRb = NULL;
1330 }
1331 else if (!newRb && !allow_user_names) {
1332 /* All RB IDs must be Gen'd */
1333 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindRenderbuffer(buffer)");
1334 return;
1335 }
1336
1337 if (!newRb) {
1338 _mesa_HashLockMutex(ctx->Shared->RenderBuffers);
1339 newRb = allocate_renderbuffer_locked(ctx, renderbuffer,
1340 "glBindRenderbufferEXT");
1341 _mesa_HashUnlockMutex(ctx->Shared->RenderBuffers);
1342 }
1343 }
1344 else {
1345 newRb = NULL;
1346 }
1347
1348 assert(newRb != &DummyRenderbuffer);
1349
1350 _mesa_reference_renderbuffer(&ctx->CurrentRenderbuffer, newRb);
1351 }
1352
1353 void GLAPIENTRY
1354 _mesa_BindRenderbuffer(GLenum target, GLuint renderbuffer)
1355 {
1356 GET_CURRENT_CONTEXT(ctx);
1357
1358 /* OpenGL ES glBindRenderbuffer and glBindRenderbufferOES use this same
1359 * entry point, but they allow the use of user-generated names.
1360 */
1361 bind_renderbuffer(target, renderbuffer, _mesa_is_gles(ctx));
1362 }
1363
1364 void GLAPIENTRY
1365 _mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
1366 {
1367 /* This function should not be in the dispatch table for core profile /
1368 * OpenGL 3.1, so execution should never get here in those cases -- no
1369 * need for an explicit test.
1370 */
1371 bind_renderbuffer(target, renderbuffer, true);
1372 }
1373
1374 /**
1375 * ARB_framebuffer_no_attachment - Application passes requested param's
1376 * here. NOTE: NumSamples requested need not be _NumSamples which is
1377 * what the hw supports.
1378 */
1379 static void
1380 framebuffer_parameteri(struct gl_context *ctx, struct gl_framebuffer *fb,
1381 GLenum pname, GLint param, const char *func)
1382 {
1383 switch (pname) {
1384 case GL_FRAMEBUFFER_DEFAULT_WIDTH:
1385 if (param < 0 || param > ctx->Const.MaxFramebufferWidth)
1386 _mesa_error(ctx, GL_INVALID_VALUE, "%s", func);
1387 else
1388 fb->DefaultGeometry.Width = param;
1389 break;
1390 case GL_FRAMEBUFFER_DEFAULT_HEIGHT:
1391 if (param < 0 || param > ctx->Const.MaxFramebufferHeight)
1392 _mesa_error(ctx, GL_INVALID_VALUE, "%s", func);
1393 else
1394 fb->DefaultGeometry.Height = param;
1395 break;
1396 case GL_FRAMEBUFFER_DEFAULT_LAYERS:
1397 /*
1398 * According to the OpenGL ES 3.1 specification section 9.2.1, the
1399 * GL_FRAMEBUFFER_DEFAULT_LAYERS parameter name is not supported.
1400 */
1401 if (_mesa_is_gles31(ctx) && !ctx->Extensions.OES_geometry_shader) {
1402 _mesa_error(ctx, GL_INVALID_ENUM, "%s(pname=0x%x)", func, pname);
1403 break;
1404 }
1405 if (param < 0 || param > ctx->Const.MaxFramebufferLayers)
1406 _mesa_error(ctx, GL_INVALID_VALUE, "%s", func);
1407 else
1408 fb->DefaultGeometry.Layers = param;
1409 break;
1410 case GL_FRAMEBUFFER_DEFAULT_SAMPLES:
1411 if (param < 0 || param > ctx->Const.MaxFramebufferSamples)
1412 _mesa_error(ctx, GL_INVALID_VALUE, "%s", func);
1413 else
1414 fb->DefaultGeometry.NumSamples = param;
1415 break;
1416 case GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS:
1417 fb->DefaultGeometry.FixedSampleLocations = param;
1418 break;
1419 default:
1420 _mesa_error(ctx, GL_INVALID_ENUM,
1421 "%s(pname=0x%x)", func, pname);
1422 }
1423
1424 invalidate_framebuffer(fb);
1425 ctx->NewState |= _NEW_BUFFERS;
1426 }
1427
1428 void GLAPIENTRY
1429 _mesa_FramebufferParameteri(GLenum target, GLenum pname, GLint param)
1430 {
1431 GET_CURRENT_CONTEXT(ctx);
1432 struct gl_framebuffer *fb;
1433
1434 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
1435 _mesa_error(ctx, GL_INVALID_OPERATION,
1436 "glFramebufferParameteriv not supported "
1437 "(ARB_framebuffer_no_attachments not implemented)");
1438 return;
1439 }
1440
1441 fb = get_framebuffer_target(ctx, target);
1442 if (!fb) {
1443 _mesa_error(ctx, GL_INVALID_ENUM,
1444 "glFramebufferParameteri(target=0x%x)", target);
1445 return;
1446 }
1447
1448 /* check framebuffer binding */
1449 if (_mesa_is_winsys_fbo(fb)) {
1450 _mesa_error(ctx, GL_INVALID_OPERATION,
1451 "glFramebufferParameteri");
1452 return;
1453 }
1454
1455 framebuffer_parameteri(ctx, fb, pname, param, "glFramebufferParameteri");
1456 }
1457
1458 static void
1459 get_framebuffer_parameteriv(struct gl_context *ctx, struct gl_framebuffer *fb,
1460 GLenum pname, GLint *params, const char *func)
1461 {
1462 switch (pname) {
1463 case GL_FRAMEBUFFER_DEFAULT_WIDTH:
1464 *params = fb->DefaultGeometry.Width;
1465 break;
1466 case GL_FRAMEBUFFER_DEFAULT_HEIGHT:
1467 *params = fb->DefaultGeometry.Height;
1468 break;
1469 case GL_FRAMEBUFFER_DEFAULT_LAYERS:
1470 /*
1471 * According to the OpenGL ES 3.1 specification section 9.2.3, the
1472 * GL_FRAMEBUFFER_LAYERS parameter name is not supported.
1473 */
1474 if (_mesa_is_gles31(ctx) && !ctx->Extensions.OES_geometry_shader) {
1475 _mesa_error(ctx, GL_INVALID_ENUM, "%s(pname=0x%x)", func, pname);
1476 break;
1477 }
1478 *params = fb->DefaultGeometry.Layers;
1479 break;
1480 case GL_FRAMEBUFFER_DEFAULT_SAMPLES:
1481 *params = fb->DefaultGeometry.NumSamples;
1482 break;
1483 case GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS:
1484 *params = fb->DefaultGeometry.FixedSampleLocations;
1485 break;
1486 default:
1487 _mesa_error(ctx, GL_INVALID_ENUM,
1488 "%s(pname=0x%x)", func, pname);
1489 }
1490 }
1491
1492 void GLAPIENTRY
1493 _mesa_GetFramebufferParameteriv(GLenum target, GLenum pname, GLint *params)
1494 {
1495 GET_CURRENT_CONTEXT(ctx);
1496 struct gl_framebuffer *fb;
1497
1498 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
1499 _mesa_error(ctx, GL_INVALID_OPERATION,
1500 "glGetFramebufferParameteriv not supported "
1501 "(ARB_framebuffer_no_attachments not implemented)");
1502 return;
1503 }
1504
1505 fb = get_framebuffer_target(ctx, target);
1506 if (!fb) {
1507 _mesa_error(ctx, GL_INVALID_ENUM,
1508 "glGetFramebufferParameteriv(target=0x%x)", target);
1509 return;
1510 }
1511
1512 /* check framebuffer binding */
1513 if (_mesa_is_winsys_fbo(fb)) {
1514 _mesa_error(ctx, GL_INVALID_OPERATION,
1515 "glGetFramebufferParameteriv");
1516 return;
1517 }
1518
1519 get_framebuffer_parameteriv(ctx, fb, pname, params,
1520 "glGetFramebufferParameteriv");
1521 }
1522
1523
1524 /**
1525 * Remove the specified renderbuffer or texture from any attachment point in
1526 * the framebuffer.
1527 *
1528 * \returns
1529 * \c true if the renderbuffer was detached from an attachment point. \c
1530 * false otherwise.
1531 */
1532 bool
1533 _mesa_detach_renderbuffer(struct gl_context *ctx,
1534 struct gl_framebuffer *fb,
1535 const void *att)
1536 {
1537 unsigned i;
1538 bool progress = false;
1539
1540 for (i = 0; i < BUFFER_COUNT; i++) {
1541 if (fb->Attachment[i].Texture == att
1542 || fb->Attachment[i].Renderbuffer == att) {
1543 remove_attachment(ctx, &fb->Attachment[i]);
1544 progress = true;
1545 }
1546 }
1547
1548 /* Section 4.4.4 (Framebuffer Completeness), subsection "Whole Framebuffer
1549 * Completeness," of the OpenGL 3.1 spec says:
1550 *
1551 * "Performing any of the following actions may change whether the
1552 * framebuffer is considered complete or incomplete:
1553 *
1554 * ...
1555 *
1556 * - Deleting, with DeleteTextures or DeleteRenderbuffers, an object
1557 * containing an image that is attached to a framebuffer object
1558 * that is bound to the framebuffer."
1559 */
1560 if (progress)
1561 invalidate_framebuffer(fb);
1562
1563 return progress;
1564 }
1565
1566
1567 void GLAPIENTRY
1568 _mesa_DeleteRenderbuffers(GLsizei n, const GLuint *renderbuffers)
1569 {
1570 GLint i;
1571 GET_CURRENT_CONTEXT(ctx);
1572
1573 if (n < 0) {
1574 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteRenderbuffers(n < 0)");
1575 return;
1576 }
1577
1578 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1579
1580 for (i = 0; i < n; i++) {
1581 if (renderbuffers[i] > 0) {
1582 struct gl_renderbuffer *rb;
1583 rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
1584 if (rb) {
1585 /* check if deleting currently bound renderbuffer object */
1586 if (rb == ctx->CurrentRenderbuffer) {
1587 /* bind default */
1588 assert(rb->RefCount >= 2);
1589 _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
1590 }
1591
1592 /* Section 4.4.2 (Attaching Images to Framebuffer Objects),
1593 * subsection "Attaching Renderbuffer Images to a Framebuffer,"
1594 * of the OpenGL 3.1 spec says:
1595 *
1596 * "If a renderbuffer object is deleted while its image is
1597 * attached to one or more attachment points in the currently
1598 * bound framebuffer, then it is as if FramebufferRenderbuffer
1599 * had been called, with a renderbuffer of 0, for each
1600 * attachment point to which this image was attached in the
1601 * currently bound framebuffer. In other words, this
1602 * renderbuffer image is first detached from all attachment
1603 * points in the currently bound framebuffer. Note that the
1604 * renderbuffer image is specifically not detached from any
1605 * non-bound framebuffers. Detaching the image from any
1606 * non-bound framebuffers is the responsibility of the
1607 * application.
1608 */
1609 if (_mesa_is_user_fbo(ctx->DrawBuffer)) {
1610 _mesa_detach_renderbuffer(ctx, ctx->DrawBuffer, rb);
1611 }
1612 if (_mesa_is_user_fbo(ctx->ReadBuffer)
1613 && ctx->ReadBuffer != ctx->DrawBuffer) {
1614 _mesa_detach_renderbuffer(ctx, ctx->ReadBuffer, rb);
1615 }
1616
1617 /* Remove from hash table immediately, to free the ID.
1618 * But the object will not be freed until it's no longer
1619 * referenced anywhere else.
1620 */
1621 _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
1622
1623 if (rb != &DummyRenderbuffer) {
1624 /* no longer referenced by hash table */
1625 _mesa_reference_renderbuffer(&rb, NULL);
1626 }
1627 }
1628 }
1629 }
1630 }
1631
1632 static void
1633 create_render_buffers(struct gl_context *ctx, GLsizei n, GLuint *renderbuffers,
1634 bool dsa)
1635 {
1636 const char *func = dsa ? "glCreateRenderbuffers" : "glGenRenderbuffers";
1637 GLuint first;
1638 GLint i;
1639
1640 if (n < 0) {
1641 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n<0)", func);
1642 return;
1643 }
1644
1645 if (!renderbuffers)
1646 return;
1647
1648 _mesa_HashLockMutex(ctx->Shared->RenderBuffers);
1649
1650 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
1651
1652 for (i = 0; i < n; i++) {
1653 GLuint name = first + i;
1654 renderbuffers[i] = name;
1655
1656 if (dsa) {
1657 allocate_renderbuffer_locked(ctx, name, func);
1658 } else {
1659 /* insert a dummy renderbuffer into the hash table */
1660 _mesa_HashInsertLocked(ctx->Shared->RenderBuffers, name,
1661 &DummyRenderbuffer);
1662 }
1663 }
1664
1665 _mesa_HashUnlockMutex(ctx->Shared->RenderBuffers);
1666 }
1667
1668
1669 void GLAPIENTRY
1670 _mesa_GenRenderbuffers(GLsizei n, GLuint *renderbuffers)
1671 {
1672 GET_CURRENT_CONTEXT(ctx);
1673 create_render_buffers(ctx, n, renderbuffers, false);
1674 }
1675
1676
1677 void GLAPIENTRY
1678 _mesa_CreateRenderbuffers(GLsizei n, GLuint *renderbuffers)
1679 {
1680 GET_CURRENT_CONTEXT(ctx);
1681 create_render_buffers(ctx, n, renderbuffers, true);
1682 }
1683
1684
1685 /**
1686 * Given an internal format token for a render buffer, return the
1687 * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1688 * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1689 * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1690 *
1691 * This is similar to _mesa_base_tex_format() but the set of valid
1692 * internal formats is different.
1693 *
1694 * Note that even if a format is determined to be legal here, validation
1695 * of the FBO may fail if the format is not supported by the driver/GPU.
1696 *
1697 * \param internalFormat as passed to glRenderbufferStorage()
1698 * \return the base internal format, or 0 if internalFormat is illegal
1699 */
1700 GLenum
1701 _mesa_base_fbo_format(struct gl_context *ctx, GLenum internalFormat)
1702 {
1703 /*
1704 * Notes: some formats such as alpha, luminance, etc. were added
1705 * with GL_ARB_framebuffer_object.
1706 */
1707 switch (internalFormat) {
1708 case GL_ALPHA:
1709 case GL_ALPHA4:
1710 case GL_ALPHA8:
1711 case GL_ALPHA12:
1712 case GL_ALPHA16:
1713 return (ctx->API == API_OPENGL_COMPAT &&
1714 ctx->Extensions.ARB_framebuffer_object) ? GL_ALPHA : 0;
1715 case GL_LUMINANCE:
1716 case GL_LUMINANCE4:
1717 case GL_LUMINANCE8:
1718 case GL_LUMINANCE12:
1719 case GL_LUMINANCE16:
1720 return (ctx->API == API_OPENGL_COMPAT &&
1721 ctx->Extensions.ARB_framebuffer_object) ? GL_LUMINANCE : 0;
1722 case GL_LUMINANCE_ALPHA:
1723 case GL_LUMINANCE4_ALPHA4:
1724 case GL_LUMINANCE6_ALPHA2:
1725 case GL_LUMINANCE8_ALPHA8:
1726 case GL_LUMINANCE12_ALPHA4:
1727 case GL_LUMINANCE12_ALPHA12:
1728 case GL_LUMINANCE16_ALPHA16:
1729 return (ctx->API == API_OPENGL_COMPAT &&
1730 ctx->Extensions.ARB_framebuffer_object) ? GL_LUMINANCE_ALPHA : 0;
1731 case GL_INTENSITY:
1732 case GL_INTENSITY4:
1733 case GL_INTENSITY8:
1734 case GL_INTENSITY12:
1735 case GL_INTENSITY16:
1736 return (ctx->API == API_OPENGL_COMPAT &&
1737 ctx->Extensions.ARB_framebuffer_object) ? GL_INTENSITY : 0;
1738 case GL_RGB8:
1739 return GL_RGB;
1740 case GL_RGB:
1741 case GL_R3_G3_B2:
1742 case GL_RGB4:
1743 case GL_RGB5:
1744 case GL_RGB10:
1745 case GL_RGB12:
1746 case GL_RGB16:
1747 return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
1748 case GL_SRGB8_EXT:
1749 return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
1750 case GL_RGBA4:
1751 case GL_RGB5_A1:
1752 case GL_RGBA8:
1753 return GL_RGBA;
1754 case GL_RGBA:
1755 case GL_RGBA2:
1756 case GL_RGBA12:
1757 case GL_RGBA16:
1758 return _mesa_is_desktop_gl(ctx) ? GL_RGBA : 0;
1759 case GL_RGB10_A2:
1760 case GL_SRGB8_ALPHA8_EXT:
1761 return _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
1762 case GL_STENCIL_INDEX:
1763 case GL_STENCIL_INDEX1_EXT:
1764 case GL_STENCIL_INDEX4_EXT:
1765 case GL_STENCIL_INDEX16_EXT:
1766 /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1767 * OpenGL ES, but Mesa does not currently support them.
1768 */
1769 return _mesa_is_desktop_gl(ctx) ? GL_STENCIL_INDEX : 0;
1770 case GL_STENCIL_INDEX8_EXT:
1771 return GL_STENCIL_INDEX;
1772 case GL_DEPTH_COMPONENT:
1773 case GL_DEPTH_COMPONENT32:
1774 return _mesa_is_desktop_gl(ctx) ? GL_DEPTH_COMPONENT : 0;
1775 case GL_DEPTH_COMPONENT16:
1776 case GL_DEPTH_COMPONENT24:
1777 return GL_DEPTH_COMPONENT;
1778 case GL_DEPTH_STENCIL:
1779 return _mesa_is_desktop_gl(ctx) ? GL_DEPTH_STENCIL : 0;
1780 case GL_DEPTH24_STENCIL8:
1781 return GL_DEPTH_STENCIL;
1782 case GL_DEPTH_COMPONENT32F:
1783 return ctx->Version >= 30
1784 || (ctx->API == API_OPENGL_COMPAT &&
1785 ctx->Extensions.ARB_depth_buffer_float)
1786 ? GL_DEPTH_COMPONENT : 0;
1787 case GL_DEPTH32F_STENCIL8:
1788 return ctx->Version >= 30
1789 || (ctx->API == API_OPENGL_COMPAT &&
1790 ctx->Extensions.ARB_depth_buffer_float)
1791 ? GL_DEPTH_STENCIL : 0;
1792 case GL_RED:
1793 case GL_R16:
1794 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1795 ? GL_RED : 0;
1796 case GL_R8:
1797 return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1798 ? GL_RED : 0;
1799 case GL_RG:
1800 case GL_RG16:
1801 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1802 ? GL_RG : 0;
1803 case GL_RG8:
1804 return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1805 ? GL_RG : 0;
1806 /* signed normalized texture formats */
1807 case GL_RED_SNORM:
1808 case GL_R8_SNORM:
1809 case GL_R16_SNORM:
1810 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1811 ? GL_RED : 0;
1812 case GL_RG_SNORM:
1813 case GL_RG8_SNORM:
1814 case GL_RG16_SNORM:
1815 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1816 ? GL_RG : 0;
1817 case GL_RGB_SNORM:
1818 case GL_RGB8_SNORM:
1819 case GL_RGB16_SNORM:
1820 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1821 ? GL_RGB : 0;
1822 case GL_RGBA_SNORM:
1823 case GL_RGBA8_SNORM:
1824 case GL_RGBA16_SNORM:
1825 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1826 ? GL_RGBA : 0;
1827 case GL_ALPHA_SNORM:
1828 case GL_ALPHA8_SNORM:
1829 case GL_ALPHA16_SNORM:
1830 return ctx->API == API_OPENGL_COMPAT &&
1831 ctx->Extensions.EXT_texture_snorm &&
1832 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1833 case GL_LUMINANCE_SNORM:
1834 case GL_LUMINANCE8_SNORM:
1835 case GL_LUMINANCE16_SNORM:
1836 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1837 ? GL_LUMINANCE : 0;
1838 case GL_LUMINANCE_ALPHA_SNORM:
1839 case GL_LUMINANCE8_ALPHA8_SNORM:
1840 case GL_LUMINANCE16_ALPHA16_SNORM:
1841 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1842 ? GL_LUMINANCE_ALPHA : 0;
1843 case GL_INTENSITY_SNORM:
1844 case GL_INTENSITY8_SNORM:
1845 case GL_INTENSITY16_SNORM:
1846 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1847 ? GL_INTENSITY : 0;
1848
1849 case GL_R16F:
1850 case GL_R32F:
1851 return ((_mesa_is_desktop_gl(ctx) &&
1852 ctx->Extensions.ARB_texture_rg &&
1853 ctx->Extensions.ARB_texture_float) ||
1854 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1855 ? GL_RED : 0;
1856 case GL_RG16F:
1857 case GL_RG32F:
1858 return ((_mesa_is_desktop_gl(ctx) &&
1859 ctx->Extensions.ARB_texture_rg &&
1860 ctx->Extensions.ARB_texture_float) ||
1861 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1862 ? GL_RG : 0;
1863 case GL_RGB16F:
1864 case GL_RGB32F:
1865 return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_float)
1866 ? GL_RGB : 0;
1867 case GL_RGBA16F:
1868 case GL_RGBA32F:
1869 return ((_mesa_is_desktop_gl(ctx) &&
1870 ctx->Extensions.ARB_texture_float) ||
1871 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1872 ? GL_RGBA : 0;
1873 case GL_ALPHA16F_ARB:
1874 case GL_ALPHA32F_ARB:
1875 return ctx->API == API_OPENGL_COMPAT &&
1876 ctx->Extensions.ARB_texture_float &&
1877 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1878 case GL_LUMINANCE16F_ARB:
1879 case GL_LUMINANCE32F_ARB:
1880 return ctx->API == API_OPENGL_COMPAT &&
1881 ctx->Extensions.ARB_texture_float &&
1882 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1883 case GL_LUMINANCE_ALPHA16F_ARB:
1884 case GL_LUMINANCE_ALPHA32F_ARB:
1885 return ctx->API == API_OPENGL_COMPAT &&
1886 ctx->Extensions.ARB_texture_float &&
1887 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1888 case GL_INTENSITY16F_ARB:
1889 case GL_INTENSITY32F_ARB:
1890 return ctx->API == API_OPENGL_COMPAT &&
1891 ctx->Extensions.ARB_texture_float &&
1892 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1893 case GL_R11F_G11F_B10F:
1894 return ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_packed_float) ||
1895 _mesa_is_gles3(ctx) /* EXT_color_buffer_float */ )
1896 ? GL_RGB : 0;
1897
1898 case GL_RGBA8UI_EXT:
1899 case GL_RGBA16UI_EXT:
1900 case GL_RGBA32UI_EXT:
1901 case GL_RGBA8I_EXT:
1902 case GL_RGBA16I_EXT:
1903 case GL_RGBA32I_EXT:
1904 return ctx->Version >= 30
1905 || (_mesa_is_desktop_gl(ctx) &&
1906 ctx->Extensions.EXT_texture_integer) ? GL_RGBA : 0;
1907
1908 case GL_RGB8UI_EXT:
1909 case GL_RGB16UI_EXT:
1910 case GL_RGB32UI_EXT:
1911 case GL_RGB8I_EXT:
1912 case GL_RGB16I_EXT:
1913 case GL_RGB32I_EXT:
1914 return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_integer
1915 ? GL_RGB : 0;
1916 case GL_R8UI:
1917 case GL_R8I:
1918 case GL_R16UI:
1919 case GL_R16I:
1920 case GL_R32UI:
1921 case GL_R32I:
1922 return ctx->Version >= 30
1923 || (_mesa_is_desktop_gl(ctx) &&
1924 ctx->Extensions.ARB_texture_rg &&
1925 ctx->Extensions.EXT_texture_integer) ? GL_RED : 0;
1926
1927 case GL_RG8UI:
1928 case GL_RG8I:
1929 case GL_RG16UI:
1930 case GL_RG16I:
1931 case GL_RG32UI:
1932 case GL_RG32I:
1933 return ctx->Version >= 30
1934 || (_mesa_is_desktop_gl(ctx) &&
1935 ctx->Extensions.ARB_texture_rg &&
1936 ctx->Extensions.EXT_texture_integer) ? GL_RG : 0;
1937
1938 case GL_INTENSITY8I_EXT:
1939 case GL_INTENSITY8UI_EXT:
1940 case GL_INTENSITY16I_EXT:
1941 case GL_INTENSITY16UI_EXT:
1942 case GL_INTENSITY32I_EXT:
1943 case GL_INTENSITY32UI_EXT:
1944 return ctx->API == API_OPENGL_COMPAT &&
1945 ctx->Extensions.EXT_texture_integer &&
1946 ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1947
1948 case GL_LUMINANCE8I_EXT:
1949 case GL_LUMINANCE8UI_EXT:
1950 case GL_LUMINANCE16I_EXT:
1951 case GL_LUMINANCE16UI_EXT:
1952 case GL_LUMINANCE32I_EXT:
1953 case GL_LUMINANCE32UI_EXT:
1954 return ctx->API == API_OPENGL_COMPAT &&
1955 ctx->Extensions.EXT_texture_integer &&
1956 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1957
1958 case GL_LUMINANCE_ALPHA8I_EXT:
1959 case GL_LUMINANCE_ALPHA8UI_EXT:
1960 case GL_LUMINANCE_ALPHA16I_EXT:
1961 case GL_LUMINANCE_ALPHA16UI_EXT:
1962 case GL_LUMINANCE_ALPHA32I_EXT:
1963 case GL_LUMINANCE_ALPHA32UI_EXT:
1964 return ctx->API == API_OPENGL_COMPAT &&
1965 ctx->Extensions.EXT_texture_integer &&
1966 ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1967
1968 case GL_ALPHA8I_EXT:
1969 case GL_ALPHA8UI_EXT:
1970 case GL_ALPHA16I_EXT:
1971 case GL_ALPHA16UI_EXT:
1972 case GL_ALPHA32I_EXT:
1973 case GL_ALPHA32UI_EXT:
1974 return ctx->API == API_OPENGL_COMPAT &&
1975 ctx->Extensions.EXT_texture_integer &&
1976 ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1977
1978 case GL_RGB10_A2UI:
1979 return (_mesa_is_desktop_gl(ctx) &&
1980 ctx->Extensions.ARB_texture_rgb10_a2ui)
1981 || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
1982
1983 case GL_RGB565:
1984 return _mesa_is_gles(ctx) || ctx->Extensions.ARB_ES2_compatibility
1985 ? GL_RGB : 0;
1986 default:
1987 return 0;
1988 }
1989 }
1990
1991
1992 /**
1993 * Invalidate a renderbuffer attachment. Called from _mesa_HashWalk().
1994 */
1995 static void
1996 invalidate_rb(GLuint key, void *data, void *userData)
1997 {
1998 struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
1999 struct gl_renderbuffer *rb = (struct gl_renderbuffer *) userData;
2000
2001 /* If this is a user-created FBO */
2002 if (_mesa_is_user_fbo(fb)) {
2003 GLuint i;
2004 for (i = 0; i < BUFFER_COUNT; i++) {
2005 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2006 if (att->Type == GL_RENDERBUFFER &&
2007 att->Renderbuffer == rb) {
2008 /* Mark fb status as indeterminate to force re-validation */
2009 fb->_Status = 0;
2010 return;
2011 }
2012 }
2013 }
2014 }
2015
2016
2017 /** sentinal value, see below */
2018 #define NO_SAMPLES 1000
2019
2020 void
2021 _mesa_renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
2022 GLenum internalFormat, GLsizei width,
2023 GLsizei height, GLsizei samples)
2024 {
2025 const GLenum baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
2026
2027 assert(baseFormat != 0);
2028 assert(width >= 0 && width <= (GLsizei) ctx->Const.MaxRenderbufferSize);
2029 assert(height >= 0 && height <= (GLsizei) ctx->Const.MaxRenderbufferSize);
2030 assert(samples != NO_SAMPLES);
2031 if (samples != 0) {
2032 assert(samples > 0);
2033 assert(_mesa_check_sample_count(ctx, GL_RENDERBUFFER,
2034 internalFormat, samples) == GL_NO_ERROR);
2035 }
2036
2037 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2038
2039 if (rb->InternalFormat == internalFormat &&
2040 rb->Width == (GLuint) width &&
2041 rb->Height == (GLuint) height &&
2042 rb->NumSamples == samples) {
2043 /* no change in allocation needed */
2044 return;
2045 }
2046
2047 /* These MUST get set by the AllocStorage func */
2048 rb->Format = MESA_FORMAT_NONE;
2049 rb->NumSamples = samples;
2050
2051 /* Now allocate the storage */
2052 assert(rb->AllocStorage);
2053 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
2054 /* No error - check/set fields now */
2055 /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
2056 assert(rb->Width == (GLuint) width);
2057 assert(rb->Height == (GLuint) height);
2058 rb->InternalFormat = internalFormat;
2059 rb->_BaseFormat = baseFormat;
2060 assert(rb->_BaseFormat != 0);
2061 }
2062 else {
2063 /* Probably ran out of memory - clear the fields */
2064 rb->Width = 0;
2065 rb->Height = 0;
2066 rb->Format = MESA_FORMAT_NONE;
2067 rb->InternalFormat = GL_NONE;
2068 rb->_BaseFormat = GL_NONE;
2069 rb->NumSamples = 0;
2070 }
2071
2072 /* Invalidate the framebuffers the renderbuffer is attached in. */
2073 if (rb->AttachedAnytime) {
2074 _mesa_HashWalk(ctx->Shared->FrameBuffers, invalidate_rb, rb);
2075 }
2076 }
2077
2078 /**
2079 * Helper function used by renderbuffer_storage_direct() and
2080 * renderbuffer_storage_target().
2081 * samples will be NO_SAMPLES if called by a non-multisample function.
2082 */
2083 static void
2084 renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
2085 GLenum internalFormat, GLsizei width,
2086 GLsizei height, GLsizei samples, const char *func)
2087 {
2088 GLenum baseFormat;
2089 GLenum sample_count_error;
2090
2091 baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
2092 if (baseFormat == 0) {
2093 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat=%s)",
2094 func, _mesa_enum_to_string(internalFormat));
2095 return;
2096 }
2097
2098 if (width < 0 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
2099 _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid width %d)", func,
2100 width);
2101 return;
2102 }
2103
2104 if (height < 0 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
2105 _mesa_error(ctx, GL_INVALID_VALUE, "%s(invalid height %d)", func,
2106 height);
2107 return;
2108 }
2109
2110 if (samples == NO_SAMPLES) {
2111 /* NumSamples == 0 indicates non-multisampling */
2112 samples = 0;
2113 }
2114 else {
2115 /* check the sample count;
2116 * note: driver may choose to use more samples than what's requested
2117 */
2118 sample_count_error = _mesa_check_sample_count(ctx, GL_RENDERBUFFER,
2119 internalFormat, samples);
2120
2121 /* Section 2.5 (GL Errors) of OpenGL 3.0 specification, page 16:
2122 *
2123 * "If a negative number is provided where an argument of type sizei or
2124 * sizeiptr is specified, the error INVALID VALUE is generated."
2125 */
2126 if (samples < 0) {
2127 sample_count_error = GL_INVALID_VALUE;
2128 }
2129
2130 if (sample_count_error != GL_NO_ERROR) {
2131 _mesa_error(ctx, sample_count_error, "%s(samples=%d)", func, samples);
2132 return;
2133 }
2134 }
2135
2136 _mesa_renderbuffer_storage(ctx, rb, internalFormat, width, height, samples);
2137 }
2138
2139 /**
2140 * Helper function used by _mesa_NamedRenderbufferStorage*().
2141 * samples will be NO_SAMPLES if called by a non-multisample function.
2142 */
2143 static void
2144 renderbuffer_storage_named(GLuint renderbuffer, GLenum internalFormat,
2145 GLsizei width, GLsizei height, GLsizei samples,
2146 const char *func)
2147 {
2148 GET_CURRENT_CONTEXT(ctx);
2149
2150 if (MESA_VERBOSE & VERBOSE_API) {
2151 if (samples == NO_SAMPLES)
2152 _mesa_debug(ctx, "%s(%u, %s, %d, %d)\n",
2153 func, renderbuffer,
2154 _mesa_enum_to_string(internalFormat),
2155 width, height);
2156 else
2157 _mesa_debug(ctx, "%s(%u, %s, %d, %d, %d)\n",
2158 func, renderbuffer,
2159 _mesa_enum_to_string(internalFormat),
2160 width, height, samples);
2161 }
2162
2163 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
2164 if (!rb || rb == &DummyRenderbuffer) {
2165 /* ID was reserved, but no real renderbuffer object made yet */
2166 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(invalid renderbuffer %u)",
2167 func, renderbuffer);
2168 return;
2169 }
2170
2171 renderbuffer_storage(ctx, rb, internalFormat, width, height, samples, func);
2172 }
2173
2174 /**
2175 * Helper function used by _mesa_RenderbufferStorage() and
2176 * _mesa_RenderbufferStorageMultisample().
2177 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
2178 */
2179 static void
2180 renderbuffer_storage_target(GLenum target, GLenum internalFormat,
2181 GLsizei width, GLsizei height, GLsizei samples,
2182 const char *func)
2183 {
2184 GET_CURRENT_CONTEXT(ctx);
2185
2186 if (MESA_VERBOSE & VERBOSE_API) {
2187 if (samples == NO_SAMPLES)
2188 _mesa_debug(ctx, "%s(%s, %s, %d, %d)\n",
2189 func,
2190 _mesa_enum_to_string(target),
2191 _mesa_enum_to_string(internalFormat),
2192 width, height);
2193 else
2194 _mesa_debug(ctx, "%s(%s, %s, %d, %d, %d)\n",
2195 func,
2196 _mesa_enum_to_string(target),
2197 _mesa_enum_to_string(internalFormat),
2198 width, height, samples);
2199 }
2200
2201 if (target != GL_RENDERBUFFER_EXT) {
2202 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
2203 return;
2204 }
2205
2206 if (!ctx->CurrentRenderbuffer) {
2207 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(no renderbuffer bound)",
2208 func);
2209 return;
2210 }
2211
2212 renderbuffer_storage(ctx, ctx->CurrentRenderbuffer, internalFormat, width,
2213 height, samples, func);
2214 }
2215
2216
2217 void GLAPIENTRY
2218 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target, GLeglImageOES image)
2219 {
2220 struct gl_renderbuffer *rb;
2221 GET_CURRENT_CONTEXT(ctx);
2222
2223 if (!ctx->Extensions.OES_EGL_image) {
2224 _mesa_error(ctx, GL_INVALID_OPERATION,
2225 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
2226 return;
2227 }
2228
2229 if (target != GL_RENDERBUFFER) {
2230 _mesa_error(ctx, GL_INVALID_ENUM,
2231 "EGLImageTargetRenderbufferStorageOES");
2232 return;
2233 }
2234
2235 rb = ctx->CurrentRenderbuffer;
2236 if (!rb) {
2237 _mesa_error(ctx, GL_INVALID_OPERATION,
2238 "EGLImageTargetRenderbufferStorageOES");
2239 return;
2240 }
2241
2242 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2243
2244 ctx->Driver.EGLImageTargetRenderbufferStorage(ctx, rb, image);
2245 }
2246
2247
2248 /**
2249 * Helper function for _mesa_GetRenderbufferParameteriv() and
2250 * _mesa_GetFramebufferAttachmentParameteriv()
2251 * We have to be careful to respect the base format. For example, if a
2252 * renderbuffer/texture was created with internalFormat=GL_RGB but the
2253 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
2254 * we need to return zero.
2255 */
2256 static GLint
2257 get_component_bits(GLenum pname, GLenum baseFormat, mesa_format format)
2258 {
2259 if (_mesa_base_format_has_channel(baseFormat, pname))
2260 return _mesa_get_format_bits(format, pname);
2261 else
2262 return 0;
2263 }
2264
2265
2266
2267 void GLAPIENTRY
2268 _mesa_RenderbufferStorage(GLenum target, GLenum internalFormat,
2269 GLsizei width, GLsizei height)
2270 {
2271 /* GL_ARB_fbo says calling this function is equivalent to calling
2272 * glRenderbufferStorageMultisample() with samples=0. We pass in
2273 * a token value here just for error reporting purposes.
2274 */
2275 renderbuffer_storage_target(target, internalFormat, width, height,
2276 NO_SAMPLES, "glRenderbufferStorage");
2277 }
2278
2279
2280 void GLAPIENTRY
2281 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
2282 GLenum internalFormat,
2283 GLsizei width, GLsizei height)
2284 {
2285 renderbuffer_storage_target(target, internalFormat, width, height,
2286 samples, "glRenderbufferStorageMultisample");
2287 }
2288
2289
2290 /**
2291 * OpenGL ES version of glRenderBufferStorage.
2292 */
2293 void GLAPIENTRY
2294 _es_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
2295 GLsizei width, GLsizei height)
2296 {
2297 switch (internalFormat) {
2298 case GL_RGB565:
2299 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
2300 /* choose a closest format */
2301 internalFormat = GL_RGB5;
2302 break;
2303 default:
2304 break;
2305 }
2306
2307 renderbuffer_storage_target(target, internalFormat, width, height, 0,
2308 "glRenderbufferStorageEXT");
2309 }
2310
2311 void GLAPIENTRY
2312 _mesa_NamedRenderbufferStorage(GLuint renderbuffer, GLenum internalformat,
2313 GLsizei width, GLsizei height)
2314 {
2315 /* GL_ARB_fbo says calling this function is equivalent to calling
2316 * glRenderbufferStorageMultisample() with samples=0. We pass in
2317 * a token value here just for error reporting purposes.
2318 */
2319 renderbuffer_storage_named(renderbuffer, internalformat, width, height,
2320 NO_SAMPLES, "glNamedRenderbufferStorage");
2321 }
2322
2323 void GLAPIENTRY
2324 _mesa_NamedRenderbufferStorageMultisample(GLuint renderbuffer, GLsizei samples,
2325 GLenum internalformat,
2326 GLsizei width, GLsizei height)
2327 {
2328 renderbuffer_storage_named(renderbuffer, internalformat, width, height,
2329 samples,
2330 "glNamedRenderbufferStorageMultisample");
2331 }
2332
2333
2334 static void
2335 get_render_buffer_parameteriv(struct gl_context *ctx,
2336 struct gl_renderbuffer *rb, GLenum pname,
2337 GLint *params, const char *func)
2338 {
2339 /* No need to flush here since we're just quering state which is
2340 * not effected by rendering.
2341 */
2342
2343 switch (pname) {
2344 case GL_RENDERBUFFER_WIDTH_EXT:
2345 *params = rb->Width;
2346 return;
2347 case GL_RENDERBUFFER_HEIGHT_EXT:
2348 *params = rb->Height;
2349 return;
2350 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
2351 *params = rb->InternalFormat;
2352 return;
2353 case GL_RENDERBUFFER_RED_SIZE_EXT:
2354 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
2355 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
2356 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
2357 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
2358 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
2359 *params = get_component_bits(pname, rb->_BaseFormat, rb->Format);
2360 break;
2361 case GL_RENDERBUFFER_SAMPLES:
2362 if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_framebuffer_object)
2363 || _mesa_is_gles3(ctx)) {
2364 *params = rb->NumSamples;
2365 break;
2366 }
2367 /* fallthrough */
2368 default:
2369 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid pname=%s)", func,
2370 _mesa_enum_to_string(pname));
2371 return;
2372 }
2373 }
2374
2375
2376 void GLAPIENTRY
2377 _mesa_GetRenderbufferParameteriv(GLenum target, GLenum pname, GLint *params)
2378 {
2379 GET_CURRENT_CONTEXT(ctx);
2380
2381 if (target != GL_RENDERBUFFER_EXT) {
2382 _mesa_error(ctx, GL_INVALID_ENUM,
2383 "glGetRenderbufferParameterivEXT(target)");
2384 return;
2385 }
2386
2387 if (!ctx->CurrentRenderbuffer) {
2388 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetRenderbufferParameterivEXT"
2389 "(no renderbuffer bound)");
2390 return;
2391 }
2392
2393 get_render_buffer_parameteriv(ctx, ctx->CurrentRenderbuffer, pname,
2394 params, "glGetRenderbufferParameteriv");
2395 }
2396
2397
2398 void GLAPIENTRY
2399 _mesa_GetNamedRenderbufferParameteriv(GLuint renderbuffer, GLenum pname,
2400 GLint *params)
2401 {
2402 GET_CURRENT_CONTEXT(ctx);
2403
2404 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
2405 if (!rb || rb == &DummyRenderbuffer) {
2406 /* ID was reserved, but no real renderbuffer object made yet */
2407 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetNamedRenderbufferParameteriv"
2408 "(invalid renderbuffer %i)", renderbuffer);
2409 return;
2410 }
2411
2412 get_render_buffer_parameteriv(ctx, rb, pname, params,
2413 "glGetNamedRenderbufferParameteriv");
2414 }
2415
2416
2417 GLboolean GLAPIENTRY
2418 _mesa_IsFramebuffer(GLuint framebuffer)
2419 {
2420 GET_CURRENT_CONTEXT(ctx);
2421 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
2422 if (framebuffer) {
2423 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
2424 if (rb != NULL && rb != &DummyFramebuffer)
2425 return GL_TRUE;
2426 }
2427 return GL_FALSE;
2428 }
2429
2430
2431 /**
2432 * Check if any of the attachments of the given framebuffer are textures
2433 * (render to texture). Call ctx->Driver.RenderTexture() for such
2434 * attachments.
2435 */
2436 static void
2437 check_begin_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
2438 {
2439 GLuint i;
2440 assert(ctx->Driver.RenderTexture);
2441
2442 if (_mesa_is_winsys_fbo(fb))
2443 return; /* can't render to texture with winsys framebuffers */
2444
2445 for (i = 0; i < BUFFER_COUNT; i++) {
2446 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2447 if (att->Texture && att->Renderbuffer->TexImage
2448 && driver_RenderTexture_is_safe(att)) {
2449 ctx->Driver.RenderTexture(ctx, fb, att);
2450 }
2451 }
2452 }
2453
2454
2455 /**
2456 * Examine all the framebuffer's attachments to see if any are textures.
2457 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
2458 * notify the device driver that the texture image may have changed.
2459 */
2460 static void
2461 check_end_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
2462 {
2463 /* Skip if we know NeedsFinishRenderTexture won't be set. */
2464 if (_mesa_is_winsys_fbo(fb) && !ctx->Driver.BindRenderbufferTexImage)
2465 return;
2466
2467 if (ctx->Driver.FinishRenderTexture) {
2468 GLuint i;
2469 for (i = 0; i < BUFFER_COUNT; i++) {
2470 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
2471 struct gl_renderbuffer *rb = att->Renderbuffer;
2472 if (rb && rb->NeedsFinishRenderTexture) {
2473 ctx->Driver.FinishRenderTexture(ctx, rb);
2474 }
2475 }
2476 }
2477 }
2478
2479
2480 static void
2481 bind_framebuffer(GLenum target, GLuint framebuffer, bool allow_user_names)
2482 {
2483 struct gl_framebuffer *newDrawFb, *newReadFb;
2484 GLboolean bindReadBuf, bindDrawBuf;
2485 GET_CURRENT_CONTEXT(ctx);
2486
2487 switch (target) {
2488 case GL_DRAW_FRAMEBUFFER_EXT:
2489 bindDrawBuf = GL_TRUE;
2490 bindReadBuf = GL_FALSE;
2491 break;
2492 case GL_READ_FRAMEBUFFER_EXT:
2493 bindDrawBuf = GL_FALSE;
2494 bindReadBuf = GL_TRUE;
2495 break;
2496 case GL_FRAMEBUFFER_EXT:
2497 bindDrawBuf = GL_TRUE;
2498 bindReadBuf = GL_TRUE;
2499 break;
2500 default:
2501 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
2502 return;
2503 }
2504
2505 if (framebuffer) {
2506 /* Binding a user-created framebuffer object */
2507 newDrawFb = _mesa_lookup_framebuffer(ctx, framebuffer);
2508 if (newDrawFb == &DummyFramebuffer) {
2509 /* ID was reserved, but no real framebuffer object made yet */
2510 newDrawFb = NULL;
2511 }
2512 else if (!newDrawFb && !allow_user_names) {
2513 /* All FBO IDs must be Gen'd */
2514 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
2515 return;
2516 }
2517
2518 if (!newDrawFb) {
2519 /* create new framebuffer object */
2520 newDrawFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
2521 if (!newDrawFb) {
2522 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
2523 return;
2524 }
2525 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newDrawFb);
2526 }
2527 newReadFb = newDrawFb;
2528 }
2529 else {
2530 /* Binding the window system framebuffer (which was originally set
2531 * with MakeCurrent).
2532 */
2533 newDrawFb = ctx->WinSysDrawBuffer;
2534 newReadFb = ctx->WinSysReadBuffer;
2535 }
2536
2537 _mesa_bind_framebuffers(ctx,
2538 bindDrawBuf ? newDrawFb : ctx->DrawBuffer,
2539 bindReadBuf ? newReadFb : ctx->ReadBuffer);
2540 }
2541
2542 void
2543 _mesa_bind_framebuffers(struct gl_context *ctx,
2544 struct gl_framebuffer *newDrawFb,
2545 struct gl_framebuffer *newReadFb)
2546 {
2547 struct gl_framebuffer *const oldDrawFb = ctx->DrawBuffer;
2548 struct gl_framebuffer *const oldReadFb = ctx->ReadBuffer;
2549 const bool bindDrawBuf = oldDrawFb != newDrawFb;
2550 const bool bindReadBuf = oldReadFb != newReadFb;
2551
2552 assert(newDrawFb);
2553 assert(newDrawFb != &DummyFramebuffer);
2554
2555 /*
2556 * OK, now bind the new Draw/Read framebuffers, if they're changing.
2557 *
2558 * We also check if we're beginning and/or ending render-to-texture.
2559 * When a framebuffer with texture attachments is unbound, call
2560 * ctx->Driver.FinishRenderTexture().
2561 * When a framebuffer with texture attachments is bound, call
2562 * ctx->Driver.RenderTexture().
2563 *
2564 * Note that if the ReadBuffer has texture attachments we don't consider
2565 * that a render-to-texture case.
2566 */
2567 if (bindReadBuf) {
2568 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2569
2570 /* check if old readbuffer was render-to-texture */
2571 check_end_texture_render(ctx, oldReadFb);
2572
2573 _mesa_reference_framebuffer(&ctx->ReadBuffer, newReadFb);
2574 }
2575
2576 if (bindDrawBuf) {
2577 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2578
2579 /* check if old framebuffer had any texture attachments */
2580 if (oldDrawFb)
2581 check_end_texture_render(ctx, oldDrawFb);
2582
2583 /* check if newly bound framebuffer has any texture attachments */
2584 check_begin_texture_render(ctx, newDrawFb);
2585
2586 _mesa_reference_framebuffer(&ctx->DrawBuffer, newDrawFb);
2587 }
2588
2589 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
2590 /* The few classic drivers that actually hook this function really only
2591 * want to know if the draw framebuffer changed.
2592 */
2593 ctx->Driver.BindFramebuffer(ctx,
2594 bindDrawBuf ? GL_FRAMEBUFFER : GL_READ_FRAMEBUFFER,
2595 newDrawFb, newReadFb);
2596 }
2597 }
2598
2599 void GLAPIENTRY
2600 _mesa_BindFramebuffer(GLenum target, GLuint framebuffer)
2601 {
2602 GET_CURRENT_CONTEXT(ctx);
2603
2604 /* OpenGL ES glBindFramebuffer and glBindFramebufferOES use this same entry
2605 * point, but they allow the use of user-generated names.
2606 */
2607 bind_framebuffer(target, framebuffer, _mesa_is_gles(ctx));
2608 }
2609
2610
2611 void GLAPIENTRY
2612 _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
2613 {
2614 /* This function should not be in the dispatch table for core profile /
2615 * OpenGL 3.1, so execution should never get here in those cases -- no
2616 * need for an explicit test.
2617 */
2618 bind_framebuffer(target, framebuffer, true);
2619 }
2620
2621
2622 void GLAPIENTRY
2623 _mesa_DeleteFramebuffers(GLsizei n, const GLuint *framebuffers)
2624 {
2625 GLint i;
2626 GET_CURRENT_CONTEXT(ctx);
2627
2628 if (n < 0) {
2629 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteFramebuffers(n < 0)");
2630 return;
2631 }
2632
2633 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2634
2635 for (i = 0; i < n; i++) {
2636 if (framebuffers[i] > 0) {
2637 struct gl_framebuffer *fb;
2638 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
2639 if (fb) {
2640 assert(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
2641
2642 /* check if deleting currently bound framebuffer object */
2643 if (fb == ctx->DrawBuffer) {
2644 /* bind default */
2645 assert(fb->RefCount >= 2);
2646 _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
2647 }
2648 if (fb == ctx->ReadBuffer) {
2649 /* bind default */
2650 assert(fb->RefCount >= 2);
2651 _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER, 0);
2652 }
2653
2654 /* remove from hash table immediately, to free the ID */
2655 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
2656
2657 if (fb != &DummyFramebuffer) {
2658 /* But the object will not be freed until it's no longer
2659 * bound in any context.
2660 */
2661 _mesa_reference_framebuffer(&fb, NULL);
2662 }
2663 }
2664 }
2665 }
2666 }
2667
2668
2669 /**
2670 * This is the implementation for glGenFramebuffers and glCreateFramebuffers.
2671 * It is not exposed to the rest of Mesa to encourage the use of
2672 * nameless buffers in driver internals.
2673 */
2674 static void
2675 create_framebuffers(GLsizei n, GLuint *framebuffers, bool dsa)
2676 {
2677 GET_CURRENT_CONTEXT(ctx);
2678 GLuint first;
2679 GLint i;
2680 struct gl_framebuffer *fb;
2681
2682 const char *func = dsa ? "glCreateFramebuffers" : "glGenFramebuffers";
2683
2684 if (n < 0) {
2685 _mesa_error(ctx, GL_INVALID_VALUE, "%s(n < 0)", func);
2686 return;
2687 }
2688
2689 if (!framebuffers)
2690 return;
2691
2692 _mesa_HashLockMutex(ctx->Shared->FrameBuffers);
2693
2694 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
2695
2696 for (i = 0; i < n; i++) {
2697 GLuint name = first + i;
2698 framebuffers[i] = name;
2699
2700 if (dsa) {
2701 fb = ctx->Driver.NewFramebuffer(ctx, framebuffers[i]);
2702 if (!fb) {
2703 _mesa_HashUnlockMutex(ctx->Shared->FrameBuffers);
2704 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", func);
2705 return;
2706 }
2707 }
2708 else
2709 fb = &DummyFramebuffer;
2710
2711 _mesa_HashInsertLocked(ctx->Shared->FrameBuffers, name, fb);
2712 }
2713
2714 _mesa_HashUnlockMutex(ctx->Shared->FrameBuffers);
2715 }
2716
2717
2718 void GLAPIENTRY
2719 _mesa_GenFramebuffers(GLsizei n, GLuint *framebuffers)
2720 {
2721 create_framebuffers(n, framebuffers, false);
2722 }
2723
2724
2725 void GLAPIENTRY
2726 _mesa_CreateFramebuffers(GLsizei n, GLuint *framebuffers)
2727 {
2728 create_framebuffers(n, framebuffers, true);
2729 }
2730
2731
2732 GLenum
2733 _mesa_check_framebuffer_status(struct gl_context *ctx,
2734 struct gl_framebuffer *buffer)
2735 {
2736 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
2737
2738 if (_mesa_is_winsys_fbo(buffer)) {
2739 /* EGL_KHR_surfaceless_context allows the winsys FBO to be incomplete. */
2740 if (buffer != &IncompleteFramebuffer) {
2741 return GL_FRAMEBUFFER_COMPLETE_EXT;
2742 } else {
2743 return GL_FRAMEBUFFER_UNDEFINED;
2744 }
2745 }
2746
2747 /* No need to flush here */
2748
2749 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
2750 _mesa_test_framebuffer_completeness(ctx, buffer);
2751 }
2752
2753 return buffer->_Status;
2754 }
2755
2756
2757 GLenum GLAPIENTRY
2758 _mesa_CheckFramebufferStatus(GLenum target)
2759 {
2760 struct gl_framebuffer *fb;
2761 GET_CURRENT_CONTEXT(ctx);
2762
2763 if (MESA_VERBOSE & VERBOSE_API)
2764 _mesa_debug(ctx, "glCheckFramebufferStatus(%s)\n",
2765 _mesa_enum_to_string(target));
2766
2767 fb = get_framebuffer_target(ctx, target);
2768 if (!fb) {
2769 _mesa_error(ctx, GL_INVALID_ENUM,
2770 "glCheckFramebufferStatus(invalid target %s)",
2771 _mesa_enum_to_string(target));
2772 return 0;
2773 }
2774
2775 return _mesa_check_framebuffer_status(ctx, fb);
2776 }
2777
2778
2779 GLenum GLAPIENTRY
2780 _mesa_CheckNamedFramebufferStatus(GLuint framebuffer, GLenum target)
2781 {
2782 struct gl_framebuffer *fb;
2783 GET_CURRENT_CONTEXT(ctx);
2784
2785 /* Validate the target (for conformance's sake) and grab a reference to the
2786 * default framebuffer in case framebuffer = 0.
2787 * Section 9.4 Framebuffer Completeness of the OpenGL 4.5 core spec
2788 * (30.10.2014, PDF page 336) says:
2789 * "If framebuffer is zero, then the status of the default read or
2790 * draw framebuffer (as determined by target) is returned."
2791 */
2792 switch (target) {
2793 case GL_DRAW_FRAMEBUFFER:
2794 case GL_FRAMEBUFFER:
2795 fb = ctx->WinSysDrawBuffer;
2796 break;
2797 case GL_READ_FRAMEBUFFER:
2798 fb = ctx->WinSysReadBuffer;
2799 break;
2800 default:
2801 _mesa_error(ctx, GL_INVALID_ENUM,
2802 "glCheckNamedFramebufferStatus(invalid target %s)",
2803 _mesa_enum_to_string(target));
2804 return 0;
2805 }
2806
2807 if (framebuffer) {
2808 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
2809 "glCheckNamedFramebufferStatus");
2810 if (!fb)
2811 return 0;
2812 }
2813
2814 return _mesa_check_framebuffer_status(ctx, fb);
2815 }
2816
2817
2818 /**
2819 * Replicate the src attachment point. Used by framebuffer_texture() when
2820 * the same texture is attached at GL_DEPTH_ATTACHMENT and
2821 * GL_STENCIL_ATTACHMENT.
2822 */
2823 static void
2824 reuse_framebuffer_texture_attachment(struct gl_framebuffer *fb,
2825 gl_buffer_index dst,
2826 gl_buffer_index src)
2827 {
2828 struct gl_renderbuffer_attachment *dst_att = &fb->Attachment[dst];
2829 struct gl_renderbuffer_attachment *src_att = &fb->Attachment[src];
2830
2831 assert(src_att->Texture != NULL);
2832 assert(src_att->Renderbuffer != NULL);
2833
2834 _mesa_reference_texobj(&dst_att->Texture, src_att->Texture);
2835 _mesa_reference_renderbuffer(&dst_att->Renderbuffer, src_att->Renderbuffer);
2836 dst_att->Type = src_att->Type;
2837 dst_att->Complete = src_att->Complete;
2838 dst_att->TextureLevel = src_att->TextureLevel;
2839 dst_att->Zoffset = src_att->Zoffset;
2840 dst_att->Layered = src_att->Layered;
2841 }
2842
2843
2844 /**
2845 * Common code called by gl*FramebufferTexture*() to retrieve the correct
2846 * texture object pointer.
2847 *
2848 * \param texObj where the pointer to the texture object is returned. Note
2849 * that a successful call may return texObj = NULL.
2850 *
2851 * \return true if no errors, false if errors
2852 */
2853 static bool
2854 get_texture_for_framebuffer(struct gl_context *ctx, GLuint texture,
2855 bool layered, const char *caller,
2856 struct gl_texture_object **texObj)
2857 {
2858 *texObj = NULL; /* This will get returned if texture = 0. */
2859
2860 if (!texture)
2861 return true;
2862
2863 *texObj = _mesa_lookup_texture(ctx, texture);
2864 if (*texObj == NULL || (*texObj)->Target == 0) {
2865 /* Can't render to a non-existent texture object.
2866 *
2867 * The OpenGL 4.5 core spec (02.02.2015) in Section 9.2 Binding and
2868 * Managing Framebuffer Objects specifies a different error
2869 * depending upon the calling function (PDF pages 325-328).
2870 * *FramebufferTexture (where layered = GL_TRUE) throws invalid
2871 * value, while the other commands throw invalid operation (where
2872 * layered = GL_FALSE).
2873 */
2874 const GLenum error = layered ? GL_INVALID_VALUE :
2875 GL_INVALID_OPERATION;
2876 _mesa_error(ctx, error,
2877 "%s(non-existent texture %u)", caller, texture);
2878 return false;
2879 }
2880
2881 return true;
2882 }
2883
2884
2885 /**
2886 * Common code called by gl*FramebufferTexture() to verify the texture target
2887 * and decide whether or not the attachment should truly be considered
2888 * layered.
2889 *
2890 * \param layered true if attachment should be considered layered, false if
2891 * not
2892 *
2893 * \return true if no errors, false if errors
2894 */
2895 static bool
2896 check_layered_texture_target(struct gl_context *ctx, GLenum target,
2897 const char *caller, GLboolean *layered)
2898 {
2899 *layered = GL_TRUE;
2900
2901 switch (target) {
2902 case GL_TEXTURE_3D:
2903 case GL_TEXTURE_1D_ARRAY_EXT:
2904 case GL_TEXTURE_2D_ARRAY_EXT:
2905 case GL_TEXTURE_CUBE_MAP:
2906 case GL_TEXTURE_CUBE_MAP_ARRAY:
2907 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2908 return true;
2909 case GL_TEXTURE_1D:
2910 case GL_TEXTURE_2D:
2911 case GL_TEXTURE_RECTANGLE:
2912 case GL_TEXTURE_2D_MULTISAMPLE:
2913 /* These texture types are valid to pass to
2914 * glFramebufferTexture(), but since they aren't layered, it
2915 * is equivalent to calling glFramebufferTexture{1D,2D}().
2916 */
2917 *layered = GL_FALSE;
2918 return true;
2919 }
2920
2921 _mesa_error(ctx, GL_INVALID_OPERATION,
2922 "%s(invalid texture target %s)", caller,
2923 _mesa_enum_to_string(target));
2924 return false;
2925 }
2926
2927
2928 /**
2929 * Common code called by gl*FramebufferTextureLayer() to verify the texture
2930 * target.
2931 *
2932 * \return true if no errors, false if errors
2933 */
2934 static bool
2935 check_texture_target(struct gl_context *ctx, GLenum target,
2936 const char *caller)
2937 {
2938 /* We're being called by glFramebufferTextureLayer().
2939 * The only legal texture types for that function are 3D,
2940 * cube-map, and 1D/2D/cube-map array textures.
2941 *
2942 * We don't need to check for GL_ARB_texture_cube_map_array because the
2943 * application wouldn't have been able to create a texture with a
2944 * GL_TEXTURE_CUBE_MAP_ARRAY target if the extension were not enabled.
2945 */
2946 switch (target) {
2947 case GL_TEXTURE_3D:
2948 case GL_TEXTURE_1D_ARRAY:
2949 case GL_TEXTURE_2D_ARRAY:
2950 case GL_TEXTURE_CUBE_MAP_ARRAY:
2951 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
2952 return true;
2953 case GL_TEXTURE_CUBE_MAP:
2954 /* We don't need to check the extension (GL_ARB_direct_state_access) or
2955 * GL version (4.5) for GL_TEXTURE_CUBE_MAP because DSA is always
2956 * enabled in core profile. This can be called from
2957 * _mesa_FramebufferTextureLayer in compatibility profile (OpenGL 3.0),
2958 * so we do have to check the profile.
2959 */
2960 return ctx->API == API_OPENGL_CORE;
2961 }
2962
2963 _mesa_error(ctx, GL_INVALID_OPERATION,
2964 "%s(invalid texture target %s)", caller,
2965 _mesa_enum_to_string(target));
2966 return false;
2967 }
2968
2969
2970 /**
2971 * Common code called by glFramebufferTexture*D() to verify the texture
2972 * target.
2973 *
2974 * \return true if no errors, false if errors
2975 */
2976 static bool
2977 check_textarget(struct gl_context *ctx, int dims, GLenum target,
2978 GLenum textarget, const char *caller)
2979 {
2980 bool err = false;
2981
2982 switch (dims) {
2983 case 1:
2984 switch (textarget) {
2985 case GL_TEXTURE_1D:
2986 break;
2987 case GL_TEXTURE_1D_ARRAY:
2988 err = !ctx->Extensions.EXT_texture_array;
2989 break;
2990 default:
2991 err = true;
2992 }
2993 break;
2994 case 2:
2995 switch (textarget) {
2996 case GL_TEXTURE_2D:
2997 break;
2998 case GL_TEXTURE_RECTANGLE:
2999 err = _mesa_is_gles(ctx)
3000 || !ctx->Extensions.NV_texture_rectangle;
3001 break;
3002 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
3003 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
3004 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
3005 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
3006 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
3007 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
3008 err = !ctx->Extensions.ARB_texture_cube_map;
3009 break;
3010 case GL_TEXTURE_2D_ARRAY:
3011 err = (_mesa_is_gles(ctx) && ctx->Version < 30)
3012 || !ctx->Extensions.EXT_texture_array;
3013 break;
3014 case GL_TEXTURE_2D_MULTISAMPLE:
3015 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
3016 err = (_mesa_is_gles(ctx) ||
3017 !ctx->Extensions.ARB_texture_multisample) &&
3018 !_mesa_is_gles31(ctx);
3019 break;
3020 default:
3021 err = true;
3022 }
3023 break;
3024 case 3:
3025 if (textarget != GL_TEXTURE_3D)
3026 err = true;
3027 break;
3028 default:
3029 err = true;
3030 }
3031
3032 if (err) {
3033 _mesa_error(ctx, GL_INVALID_OPERATION,
3034 "%s(invalid textarget %s)",
3035 caller, _mesa_enum_to_string(textarget));
3036 return false;
3037 }
3038
3039 /* Make sure textarget is consistent with the texture's type */
3040 err = (target == GL_TEXTURE_CUBE_MAP) ?
3041 !_mesa_is_cube_face(textarget): (target != textarget);
3042
3043 if (err) {
3044 _mesa_error(ctx, GL_INVALID_OPERATION,
3045 "%s(mismatched texture target)", caller);
3046 return false;
3047 }
3048
3049 return true;
3050 }
3051
3052
3053 /**
3054 * Common code called by gl*FramebufferTextureLayer() and
3055 * glFramebufferTexture3D() to validate the layer.
3056 *
3057 * \return true if no errors, false if errors
3058 */
3059 static bool
3060 check_layer(struct gl_context *ctx, GLenum target, GLint layer,
3061 const char *caller)
3062 {
3063 /* Page 306 (page 328 of the PDF) of the OpenGL 4.5 (Core Profile)
3064 * spec says:
3065 *
3066 * "An INVALID_VALUE error is generated if texture is non-zero
3067 * and layer is negative."
3068 */
3069 if (layer < 0) {
3070 _mesa_error(ctx, GL_INVALID_VALUE,
3071 "%s(layer %u < 0)", caller, layer);
3072 return false;
3073 }
3074
3075 if (target == GL_TEXTURE_3D) {
3076 const GLuint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
3077 if (layer >= maxSize) {
3078 _mesa_error(ctx, GL_INVALID_VALUE,
3079 "%s(invalid layer %u)", caller, layer);
3080 return false;
3081 }
3082 }
3083 else if ((target == GL_TEXTURE_1D_ARRAY) ||
3084 (target == GL_TEXTURE_2D_ARRAY) ||
3085 (target == GL_TEXTURE_CUBE_MAP_ARRAY) ||
3086 (target == GL_TEXTURE_2D_MULTISAMPLE_ARRAY)) {
3087 if (layer >= ctx->Const.MaxArrayTextureLayers) {
3088 _mesa_error(ctx, GL_INVALID_VALUE,
3089 "%s(layer %u >= GL_MAX_ARRAY_TEXTURE_LAYERS)",
3090 caller, layer);
3091 return false;
3092 }
3093 }
3094 else if (target == GL_TEXTURE_CUBE_MAP) {
3095 if (layer >= 6) {
3096 _mesa_error(ctx, GL_INVALID_VALUE,
3097 "%s(layer %u >= 6)", caller, layer);
3098 return false;
3099 }
3100 }
3101
3102 return true;
3103 }
3104
3105
3106 /**
3107 * Common code called by all gl*FramebufferTexture*() entry points to verify
3108 * the level.
3109 *
3110 * \return true if no errors, false if errors
3111 */
3112 static bool
3113 check_level(struct gl_context *ctx, GLenum target, GLint level,
3114 const char *caller)
3115 {
3116 if ((level < 0) ||
3117 (level >= _mesa_max_texture_levels(ctx, target))) {
3118 _mesa_error(ctx, GL_INVALID_VALUE,
3119 "%s(invalid level %d)", caller, level);
3120 return false;
3121 }
3122
3123 return true;
3124 }
3125
3126
3127 void
3128 _mesa_framebuffer_texture(struct gl_context *ctx, struct gl_framebuffer *fb,
3129 GLenum attachment,
3130 struct gl_texture_object *texObj, GLenum textarget,
3131 GLint level, GLuint layer, GLboolean layered,
3132 const char *caller)
3133 {
3134 struct gl_renderbuffer_attachment *att;
3135
3136 /* The window-system framebuffer object is immutable */
3137 if (_mesa_is_winsys_fbo(fb)) {
3138 _mesa_error(ctx, GL_INVALID_OPERATION, "%s(window-system framebuffer)",
3139 caller);
3140 return;
3141 }
3142
3143 /* Not a hash lookup, so we can afford to get the attachment here. */
3144 att = get_attachment(ctx, fb, attachment);
3145 if (att == NULL) {
3146 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid attachment %s)", caller,
3147 _mesa_enum_to_string(attachment));
3148 return;
3149 }
3150
3151 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
3152
3153 mtx_lock(&fb->Mutex);
3154 if (texObj) {
3155 if (attachment == GL_DEPTH_ATTACHMENT &&
3156 texObj == fb->Attachment[BUFFER_STENCIL].Texture &&
3157 level == fb->Attachment[BUFFER_STENCIL].TextureLevel &&
3158 _mesa_tex_target_to_face(textarget) ==
3159 fb->Attachment[BUFFER_STENCIL].CubeMapFace &&
3160 layer == fb->Attachment[BUFFER_STENCIL].Zoffset) {
3161 /* The texture object is already attached to the stencil attachment
3162 * point. Don't create a new renderbuffer; just reuse the stencil
3163 * attachment's. This is required to prevent a GL error in
3164 * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
3165 */
3166 reuse_framebuffer_texture_attachment(fb, BUFFER_DEPTH,
3167 BUFFER_STENCIL);
3168 } else if (attachment == GL_STENCIL_ATTACHMENT &&
3169 texObj == fb->Attachment[BUFFER_DEPTH].Texture &&
3170 level == fb->Attachment[BUFFER_DEPTH].TextureLevel &&
3171 _mesa_tex_target_to_face(textarget) ==
3172 fb->Attachment[BUFFER_DEPTH].CubeMapFace &&
3173 layer == fb->Attachment[BUFFER_DEPTH].Zoffset) {
3174 /* As above, but with depth and stencil transposed. */
3175 reuse_framebuffer_texture_attachment(fb, BUFFER_STENCIL,
3176 BUFFER_DEPTH);
3177 } else {
3178 set_texture_attachment(ctx, fb, att, texObj, textarget,
3179 level, layer, layered);
3180
3181 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
3182 /* Above we created a new renderbuffer and attached it to the
3183 * depth attachment point. Now attach it to the stencil attachment
3184 * point too.
3185 */
3186 assert(att == &fb->Attachment[BUFFER_DEPTH]);
3187 reuse_framebuffer_texture_attachment(fb,BUFFER_STENCIL,
3188 BUFFER_DEPTH);
3189 }
3190 }
3191
3192 /* Set the render-to-texture flag. We'll check this flag in
3193 * glTexImage() and friends to determine if we need to revalidate
3194 * any FBOs that might be rendering into this texture.
3195 * This flag never gets cleared since it's non-trivial to determine
3196 * when all FBOs might be done rendering to this texture. That's OK
3197 * though since it's uncommon to render to a texture then repeatedly
3198 * call glTexImage() to change images in the texture.
3199 */
3200 texObj->_RenderToTexture = GL_TRUE;
3201 }
3202 else {
3203 remove_attachment(ctx, att);
3204 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
3205 assert(att == &fb->Attachment[BUFFER_DEPTH]);
3206 remove_attachment(ctx, &fb->Attachment[BUFFER_STENCIL]);
3207 }
3208 }
3209
3210 invalidate_framebuffer(fb);
3211
3212 mtx_unlock(&fb->Mutex);
3213 }
3214
3215
3216 static void
3217 framebuffer_texture_with_dims(int dims, GLenum target,
3218 GLenum attachment, GLenum textarget,
3219 GLuint texture, GLint level, GLint layer,
3220 const char *caller)
3221 {
3222 GET_CURRENT_CONTEXT(ctx);
3223 struct gl_framebuffer *fb;
3224 struct gl_texture_object *texObj;
3225
3226 /* Get the framebuffer object */
3227 fb = get_framebuffer_target(ctx, target);
3228 if (!fb) {
3229 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid target %s)", caller,
3230 _mesa_enum_to_string(target));
3231 return;
3232 }
3233
3234 /* Get the texture object */
3235 if (!get_texture_for_framebuffer(ctx, texture, false, caller, &texObj))
3236 return;
3237
3238 if (texObj) {
3239 if (!check_textarget(ctx, dims, texObj->Target, textarget, caller))
3240 return;
3241
3242 if ((dims == 3) && !check_layer(ctx, texObj->Target, layer, caller))
3243 return;
3244
3245 if (!check_level(ctx, textarget, level, caller))
3246 return;
3247 }
3248
3249 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, textarget, level,
3250 layer, GL_FALSE, caller);
3251 }
3252
3253
3254 void GLAPIENTRY
3255 _mesa_FramebufferTexture1D(GLenum target, GLenum attachment,
3256 GLenum textarget, GLuint texture, GLint level)
3257 {
3258 framebuffer_texture_with_dims(1, target, attachment, textarget, texture,
3259 level, 0, "glFramebufferTexture1D");
3260 }
3261
3262
3263 void GLAPIENTRY
3264 _mesa_FramebufferTexture2D(GLenum target, GLenum attachment,
3265 GLenum textarget, GLuint texture, GLint level)
3266 {
3267 framebuffer_texture_with_dims(2, target, attachment, textarget, texture,
3268 level, 0, "glFramebufferTexture2D");
3269 }
3270
3271
3272 void GLAPIENTRY
3273 _mesa_FramebufferTexture3D(GLenum target, GLenum attachment,
3274 GLenum textarget, GLuint texture,
3275 GLint level, GLint layer)
3276 {
3277 framebuffer_texture_with_dims(3, target, attachment, textarget, texture,
3278 level, layer, "glFramebufferTexture3D");
3279 }
3280
3281
3282 void GLAPIENTRY
3283 _mesa_FramebufferTextureLayer(GLenum target, GLenum attachment,
3284 GLuint texture, GLint level, GLint layer)
3285 {
3286 GET_CURRENT_CONTEXT(ctx);
3287 struct gl_framebuffer *fb;
3288 struct gl_texture_object *texObj;
3289 GLenum textarget = 0;
3290
3291 const char *func = "glFramebufferTextureLayer";
3292
3293 /* Get the framebuffer object */
3294 fb = get_framebuffer_target(ctx, target);
3295 if (!fb) {
3296 _mesa_error(ctx, GL_INVALID_ENUM,
3297 "glFramebufferTextureLayer(invalid target %s)",
3298 _mesa_enum_to_string(target));
3299 return;
3300 }
3301
3302 /* Get the texture object */
3303 if (!get_texture_for_framebuffer(ctx, texture, false, func, &texObj))
3304 return;
3305
3306 if (texObj) {
3307 if (!check_texture_target(ctx, texObj->Target, func))
3308 return;
3309
3310 if (!check_layer(ctx, texObj->Target, layer, func))
3311 return;
3312
3313 if (!check_level(ctx, texObj->Target, level, func))
3314 return;
3315
3316 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
3317 assert(layer >= 0 && layer < 6);
3318 textarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer;
3319 layer = 0;
3320 }
3321 }
3322
3323 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, textarget, level,
3324 layer, GL_FALSE, func);
3325 }
3326
3327
3328 void GLAPIENTRY
3329 _mesa_NamedFramebufferTextureLayer(GLuint framebuffer, GLenum attachment,
3330 GLuint texture, GLint level, GLint layer)
3331 {
3332 GET_CURRENT_CONTEXT(ctx);
3333 struct gl_framebuffer *fb;
3334 struct gl_texture_object *texObj;
3335 GLenum textarget = 0;
3336
3337 const char *func = "glNamedFramebufferTextureLayer";
3338
3339 /* Get the framebuffer object */
3340 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer, func);
3341 if (!fb)
3342 return;
3343
3344 /* Get the texture object */
3345 if (!get_texture_for_framebuffer(ctx, texture, false, func, &texObj))
3346 return;
3347
3348 if (texObj) {
3349 if (!check_texture_target(ctx, texObj->Target, func))
3350 return;
3351
3352 if (!check_layer(ctx, texObj->Target, layer, func))
3353 return;
3354
3355 if (!check_level(ctx, texObj->Target, level, func))
3356 return;
3357
3358 if (texObj->Target == GL_TEXTURE_CUBE_MAP) {
3359 assert(layer >= 0 && layer < 6);
3360 textarget = GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer;
3361 layer = 0;
3362 }
3363 }
3364
3365 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, textarget, level,
3366 layer, GL_FALSE, func);
3367 }
3368
3369
3370 void GLAPIENTRY
3371 _mesa_FramebufferTexture(GLenum target, GLenum attachment,
3372 GLuint texture, GLint level)
3373 {
3374 GET_CURRENT_CONTEXT(ctx);
3375 struct gl_framebuffer *fb;
3376 struct gl_texture_object *texObj;
3377 GLboolean layered = GL_FALSE;
3378
3379 const char *func = "FramebufferTexture";
3380
3381 if (!_mesa_has_geometry_shaders(ctx)) {
3382 _mesa_error(ctx, GL_INVALID_OPERATION,
3383 "unsupported function (glFramebufferTexture) called");
3384 return;
3385 }
3386
3387 /* Get the framebuffer object */
3388 fb = get_framebuffer_target(ctx, target);
3389 if (!fb) {
3390 _mesa_error(ctx, GL_INVALID_ENUM,
3391 "glFramebufferTexture(invalid target %s)",
3392 _mesa_enum_to_string(target));
3393 return;
3394 }
3395
3396 /* Get the texture object */
3397 if (!get_texture_for_framebuffer(ctx, texture, true, func, &texObj))
3398 return;
3399
3400 if (texObj) {
3401 if (!check_layered_texture_target(ctx, texObj->Target, func, &layered))
3402 return;
3403
3404 if (!check_level(ctx, texObj->Target, level, func))
3405 return;
3406 }
3407
3408 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, 0, level,
3409 0, layered, func);
3410 }
3411
3412
3413 void GLAPIENTRY
3414 _mesa_NamedFramebufferTexture(GLuint framebuffer, GLenum attachment,
3415 GLuint texture, GLint level)
3416 {
3417 GET_CURRENT_CONTEXT(ctx);
3418 struct gl_framebuffer *fb;
3419 struct gl_texture_object *texObj;
3420 GLboolean layered = GL_FALSE;
3421
3422 const char *func = "glNamedFramebufferTexture";
3423
3424 if (!_mesa_has_geometry_shaders(ctx)) {
3425 _mesa_error(ctx, GL_INVALID_OPERATION,
3426 "unsupported function (glNamedFramebufferTexture) called");
3427 return;
3428 }
3429
3430 /* Get the framebuffer object */
3431 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer, func);
3432 if (!fb)
3433 return;
3434
3435 /* Get the texture object */
3436 if (!get_texture_for_framebuffer(ctx, texture, true, func, &texObj))
3437 return;
3438
3439 if (texObj) {
3440 if (!check_layered_texture_target(ctx, texObj->Target, func,
3441 &layered))
3442 return;
3443
3444 if (!check_level(ctx, texObj->Target, level, func))
3445 return;
3446 }
3447
3448 _mesa_framebuffer_texture(ctx, fb, attachment, texObj, 0, level,
3449 0, layered, func);
3450 }
3451
3452
3453 void
3454 _mesa_framebuffer_renderbuffer(struct gl_context *ctx,
3455 struct gl_framebuffer *fb,
3456 GLenum attachment,
3457 struct gl_renderbuffer *rb)
3458 {
3459 assert(!_mesa_is_winsys_fbo(fb));
3460
3461 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
3462
3463 assert(ctx->Driver.FramebufferRenderbuffer);
3464 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
3465
3466 /* Some subsequent GL commands may depend on the framebuffer's visual
3467 * after the binding is updated. Update visual info now.
3468 */
3469 _mesa_update_framebuffer_visual(ctx, fb);
3470 }
3471
3472 static void
3473 framebuffer_renderbuffer(struct gl_context *ctx,
3474 struct gl_framebuffer *fb,
3475 GLenum attachment,
3476 struct gl_renderbuffer *rb,
3477 const char *func)
3478 {
3479 struct gl_renderbuffer_attachment *att;
3480
3481 if (_mesa_is_winsys_fbo(fb)) {
3482 /* Can't attach new renderbuffers to a window system framebuffer */
3483 _mesa_error(ctx, GL_INVALID_OPERATION,
3484 "%s(window-system framebuffer)", func);
3485 return;
3486 }
3487
3488 att = get_attachment(ctx, fb, attachment);
3489 if (att == NULL) {
3490 _mesa_error(ctx, GL_INVALID_ENUM,
3491 "%s(invalid attachment %s)", func,
3492 _mesa_enum_to_string(attachment));
3493 return;
3494 }
3495
3496 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT &&
3497 rb && rb->Format != MESA_FORMAT_NONE) {
3498 /* make sure the renderbuffer is a depth/stencil format */
3499 const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
3500 if (baseFormat != GL_DEPTH_STENCIL) {
3501 _mesa_error(ctx, GL_INVALID_OPERATION,
3502 "%s(renderbuffer is not DEPTH_STENCIL format)", func);
3503 return;
3504 }
3505 }
3506
3507 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
3508 }
3509
3510 void GLAPIENTRY
3511 _mesa_FramebufferRenderbuffer(GLenum target, GLenum attachment,
3512 GLenum renderbuffertarget,
3513 GLuint renderbuffer)
3514 {
3515 struct gl_framebuffer *fb;
3516 struct gl_renderbuffer *rb;
3517 GET_CURRENT_CONTEXT(ctx);
3518
3519 fb = get_framebuffer_target(ctx, target);
3520 if (!fb) {
3521 _mesa_error(ctx, GL_INVALID_ENUM,
3522 "glFramebufferRenderbuffer(invalid target %s)",
3523 _mesa_enum_to_string(target));
3524 return;
3525 }
3526
3527 if (renderbuffertarget != GL_RENDERBUFFER) {
3528 _mesa_error(ctx, GL_INVALID_ENUM,
3529 "glFramebufferRenderbuffer(renderbuffertarget is not "
3530 "GL_RENDERBUFFER)");
3531 return;
3532 }
3533
3534 if (renderbuffer) {
3535 rb = _mesa_lookup_renderbuffer_err(ctx, renderbuffer,
3536 "glFramebufferRenderbuffer");
3537 if (!rb)
3538 return;
3539 }
3540 else {
3541 /* remove renderbuffer attachment */
3542 rb = NULL;
3543 }
3544
3545 framebuffer_renderbuffer(ctx, fb, attachment, rb,
3546 "glFramebufferRenderbuffer");
3547 }
3548
3549
3550 void GLAPIENTRY
3551 _mesa_NamedFramebufferRenderbuffer(GLuint framebuffer, GLenum attachment,
3552 GLenum renderbuffertarget,
3553 GLuint renderbuffer)
3554 {
3555 struct gl_framebuffer *fb;
3556 struct gl_renderbuffer *rb;
3557 GET_CURRENT_CONTEXT(ctx);
3558
3559 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
3560 "glNamedFramebufferRenderbuffer");
3561 if (!fb)
3562 return;
3563
3564 if (renderbuffertarget != GL_RENDERBUFFER) {
3565 _mesa_error(ctx, GL_INVALID_ENUM,
3566 "glNamedFramebufferRenderbuffer(renderbuffertarget is not "
3567 "GL_RENDERBUFFER)");
3568 return;
3569 }
3570
3571 if (renderbuffer) {
3572 rb = _mesa_lookup_renderbuffer_err(ctx, renderbuffer,
3573 "glNamedFramebufferRenderbuffer");
3574 if (!rb)
3575 return;
3576 }
3577 else {
3578 /* remove renderbuffer attachment */
3579 rb = NULL;
3580 }
3581
3582 framebuffer_renderbuffer(ctx, fb, attachment, rb,
3583 "glNamedFramebufferRenderbuffer");
3584 }
3585
3586
3587 void
3588 _mesa_get_framebuffer_attachment_parameter(struct gl_context *ctx,
3589 struct gl_framebuffer *buffer,
3590 GLenum attachment, GLenum pname,
3591 GLint *params, const char *caller)
3592 {
3593 const struct gl_renderbuffer_attachment *att;
3594 GLenum err;
3595
3596 /* The error code for an attachment type of GL_NONE differs between APIs.
3597 *
3598 * From the ES 2.0.25 specification, page 127:
3599 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, then
3600 * querying any other pname will generate INVALID_ENUM."
3601 *
3602 * From the OpenGL 3.0 specification, page 337, or identically,
3603 * the OpenGL ES 3.0.4 specification, page 240:
3604 *
3605 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, no
3606 * framebuffer is bound to target. In this case querying pname
3607 * FRAMEBUFFER_ATTACHMENT_OBJECT_NAME will return zero, and all other
3608 * queries will generate an INVALID_OPERATION error."
3609 */
3610 err = ctx->API == API_OPENGLES2 && ctx->Version < 30 ?
3611 GL_INVALID_ENUM : GL_INVALID_OPERATION;
3612
3613 if (_mesa_is_winsys_fbo(buffer)) {
3614 /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
3615 * says:
3616 *
3617 * "If the framebuffer currently bound to target is zero, then
3618 * INVALID_OPERATION is generated."
3619 *
3620 * The EXT_framebuffer_object spec has the same wording, and the
3621 * OES_framebuffer_object spec refers to the EXT_framebuffer_object
3622 * spec.
3623 */
3624 if ((!_mesa_is_desktop_gl(ctx) ||
3625 !ctx->Extensions.ARB_framebuffer_object)
3626 && !_mesa_is_gles3(ctx)) {
3627 _mesa_error(ctx, GL_INVALID_OPERATION,
3628 "%s(window-system framebuffer)", caller);
3629 return;
3630 }
3631
3632 if (_mesa_is_gles3(ctx) && attachment != GL_BACK &&
3633 attachment != GL_DEPTH && attachment != GL_STENCIL) {
3634 _mesa_error(ctx, GL_INVALID_ENUM,
3635 "%s(invalid attachment %s)", caller,
3636 _mesa_enum_to_string(attachment));
3637 return;
3638 }
3639
3640 /* The specs are not clear about how to handle
3641 * GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME with the default framebuffer,
3642 * but dEQP-GLES3 expects an INVALID_ENUM error. This has also been
3643 * discussed in:
3644 *
3645 * https://cvs.khronos.org/bugzilla/show_bug.cgi?id=12928#c1
3646 * and https://bugs.freedesktop.org/show_bug.cgi?id=31947
3647 */
3648 if (pname == GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME) {
3649 _mesa_error(ctx, GL_INVALID_ENUM,
3650 "%s(requesting GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME "
3651 "when GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is "
3652 "GL_FRAMEBUFFER_DEFAULT is not allowed)", caller);
3653 return;
3654 }
3655
3656 /* the default / window-system FBO */
3657 att = _mesa_get_fb0_attachment(ctx, buffer, attachment);
3658 }
3659 else {
3660 /* user-created framebuffer FBO */
3661 att = get_attachment(ctx, buffer, attachment);
3662 }
3663
3664 if (att == NULL) {
3665 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid attachment %s)", caller,
3666 _mesa_enum_to_string(attachment));
3667 return;
3668 }
3669
3670 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
3671 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
3672 if (pname == GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE) {
3673 /* This behavior is first specified in OpenGL 4.4 specification.
3674 *
3675 * From the OpenGL 4.4 spec page 275:
3676 * "This query cannot be performed for a combined depth+stencil
3677 * attachment, since it does not have a single format."
3678 */
3679 _mesa_error(ctx, GL_INVALID_OPERATION,
3680 "%s(GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE"
3681 " is invalid for depth+stencil attachment)", caller);
3682 return;
3683 }
3684 /* the depth and stencil attachments must point to the same buffer */
3685 depthAtt = get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
3686 stencilAtt = get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
3687 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
3688 _mesa_error(ctx, GL_INVALID_OPERATION,
3689 "%s(DEPTH/STENCIL attachments differ)", caller);
3690 return;
3691 }
3692 }
3693
3694 /* No need to flush here */
3695
3696 switch (pname) {
3697 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
3698 /* From the OpenGL spec, 9.2. Binding and Managing Framebuffer Objects:
3699 *
3700 * "If the value of FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE is NONE, then
3701 * either no framebuffer is bound to target; or the default framebuffer
3702 * is bound, attachment is DEPTH or STENCIL, and the number of depth or
3703 * stencil bits, respectively, is zero."
3704 */
3705 *params = (_mesa_is_winsys_fbo(buffer) &&
3706 ((attachment != GL_DEPTH && attachment != GL_STENCIL) ||
3707 (att->Type != GL_NONE)))
3708 ? GL_FRAMEBUFFER_DEFAULT : att->Type;
3709 return;
3710 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
3711 if (att->Type == GL_RENDERBUFFER_EXT) {
3712 *params = att->Renderbuffer->Name;
3713 }
3714 else if (att->Type == GL_TEXTURE) {
3715 *params = att->Texture->Name;
3716 }
3717 else {
3718 assert(att->Type == GL_NONE);
3719 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
3720 *params = 0;
3721 } else {
3722 goto invalid_pname_enum;
3723 }
3724 }
3725 return;
3726 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
3727 if (att->Type == GL_TEXTURE) {
3728 *params = att->TextureLevel;
3729 }
3730 else if (att->Type == GL_NONE) {
3731 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3732 _mesa_enum_to_string(pname));
3733 }
3734 else {
3735 goto invalid_pname_enum;
3736 }
3737 return;
3738 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
3739 if (att->Type == GL_TEXTURE) {
3740 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
3741 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
3742 }
3743 else {
3744 *params = 0;
3745 }
3746 }
3747 else if (att->Type == GL_NONE) {
3748 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3749 _mesa_enum_to_string(pname));
3750 }
3751 else {
3752 goto invalid_pname_enum;
3753 }
3754 return;
3755 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
3756 if (ctx->API == API_OPENGLES) {
3757 goto invalid_pname_enum;
3758 } else if (att->Type == GL_NONE) {
3759 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3760 _mesa_enum_to_string(pname));
3761 } else if (att->Type == GL_TEXTURE) {
3762 if (att->Texture && (att->Texture->Target == GL_TEXTURE_3D ||
3763 att->Texture->Target == GL_TEXTURE_2D_ARRAY)) {
3764 *params = att->Zoffset;
3765 }
3766 else {
3767 *params = 0;
3768 }
3769 }
3770 else {
3771 goto invalid_pname_enum;
3772 }
3773 return;
3774 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
3775 if ((!_mesa_is_desktop_gl(ctx) ||
3776 !ctx->Extensions.ARB_framebuffer_object)
3777 && !_mesa_is_gles3(ctx)) {
3778 goto invalid_pname_enum;
3779 }
3780 else if (att->Type == GL_NONE) {
3781 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3782 _mesa_enum_to_string(pname));
3783 }
3784 else {
3785 if (ctx->Extensions.EXT_framebuffer_sRGB) {
3786 *params =
3787 _mesa_get_format_color_encoding(att->Renderbuffer->Format);
3788 }
3789 else {
3790 /* According to ARB_framebuffer_sRGB, we should return LINEAR
3791 * if the sRGB conversion is unsupported. */
3792 *params = GL_LINEAR;
3793 }
3794 }
3795 return;
3796 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
3797 if ((ctx->API != API_OPENGL_COMPAT ||
3798 !ctx->Extensions.ARB_framebuffer_object)
3799 && ctx->API != API_OPENGL_CORE
3800 && !_mesa_is_gles3(ctx)) {
3801 goto invalid_pname_enum;
3802 }
3803 else if (att->Type == GL_NONE) {
3804 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3805 _mesa_enum_to_string(pname));
3806 }
3807 else {
3808 mesa_format format = att->Renderbuffer->Format;
3809
3810 /* Page 235 (page 247 of the PDF) in section 6.1.13 of the OpenGL ES
3811 * 3.0.1 spec says:
3812 *
3813 * "If pname is FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE.... If
3814 * attachment is DEPTH_STENCIL_ATTACHMENT the query will fail and
3815 * generate an INVALID_OPERATION error.
3816 */
3817 if (_mesa_is_gles3(ctx) &&
3818 attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
3819 _mesa_error(ctx, GL_INVALID_OPERATION,
3820 "%s(cannot query "
3821 "GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE of "
3822 "GL_DEPTH_STENCIL_ATTACHMENT)", caller);
3823 return;
3824 }
3825
3826 if (format == MESA_FORMAT_S_UINT8) {
3827 /* special cases */
3828 *params = GL_INDEX;
3829 }
3830 else if (format == MESA_FORMAT_Z32_FLOAT_S8X24_UINT) {
3831 /* depends on the attachment parameter */
3832 if (attachment == GL_STENCIL_ATTACHMENT) {
3833 *params = GL_INDEX;
3834 }
3835 else {
3836 *params = GL_FLOAT;
3837 }
3838 }
3839 else {
3840 *params = _mesa_get_format_datatype(format);
3841 }
3842 }
3843 return;
3844 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
3845 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
3846 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
3847 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
3848 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
3849 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
3850 if ((!_mesa_is_desktop_gl(ctx) ||
3851 !ctx->Extensions.ARB_framebuffer_object)
3852 && !_mesa_is_gles3(ctx)) {
3853 goto invalid_pname_enum;
3854 }
3855 else if (att->Type == GL_NONE) {
3856 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3857 _mesa_enum_to_string(pname));
3858 }
3859 else if (att->Texture) {
3860 const struct gl_texture_image *texImage =
3861 _mesa_select_tex_image(att->Texture, att->Texture->Target,
3862 att->TextureLevel);
3863 if (texImage) {
3864 *params = get_component_bits(pname, texImage->_BaseFormat,
3865 texImage->TexFormat);
3866 }
3867 else {
3868 *params = 0;
3869 }
3870 }
3871 else if (att->Renderbuffer) {
3872 *params = get_component_bits(pname, att->Renderbuffer->_BaseFormat,
3873 att->Renderbuffer->Format);
3874 }
3875 else {
3876 _mesa_problem(ctx, "%s: invalid FBO attachment structure", caller);
3877 }
3878 return;
3879 case GL_FRAMEBUFFER_ATTACHMENT_LAYERED:
3880 if (!_mesa_has_geometry_shaders(ctx)) {
3881 goto invalid_pname_enum;
3882 } else if (att->Type == GL_TEXTURE) {
3883 *params = att->Layered;
3884 } else if (att->Type == GL_NONE) {
3885 _mesa_error(ctx, err, "%s(invalid pname %s)", caller,
3886 _mesa_enum_to_string(pname));
3887 } else {
3888 goto invalid_pname_enum;
3889 }
3890 return;
3891 default:
3892 goto invalid_pname_enum;
3893 }
3894
3895 return;
3896
3897 invalid_pname_enum:
3898 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid pname %s)", caller,
3899 _mesa_enum_to_string(pname));
3900 return;
3901 }
3902
3903
3904 void GLAPIENTRY
3905 _mesa_GetFramebufferAttachmentParameteriv(GLenum target, GLenum attachment,
3906 GLenum pname, GLint *params)
3907 {
3908 GET_CURRENT_CONTEXT(ctx);
3909 struct gl_framebuffer *buffer;
3910
3911 buffer = get_framebuffer_target(ctx, target);
3912 if (!buffer) {
3913 _mesa_error(ctx, GL_INVALID_ENUM,
3914 "glGetFramebufferAttachmentParameteriv(invalid target %s)",
3915 _mesa_enum_to_string(target));
3916 return;
3917 }
3918
3919 _mesa_get_framebuffer_attachment_parameter(ctx, buffer, attachment, pname,
3920 params,
3921 "glGetFramebufferAttachmentParameteriv");
3922 }
3923
3924
3925 void GLAPIENTRY
3926 _mesa_GetNamedFramebufferAttachmentParameteriv(GLuint framebuffer,
3927 GLenum attachment,
3928 GLenum pname, GLint *params)
3929 {
3930 GET_CURRENT_CONTEXT(ctx);
3931 struct gl_framebuffer *buffer;
3932
3933 if (framebuffer) {
3934 buffer = _mesa_lookup_framebuffer_err(ctx, framebuffer,
3935 "glGetNamedFramebufferAttachmentParameteriv");
3936 if (!buffer)
3937 return;
3938 }
3939 else {
3940 /*
3941 * Section 9.2 Binding and Managing Framebuffer Objects of the OpenGL
3942 * 4.5 core spec (30.10.2014, PDF page 314):
3943 * "If framebuffer is zero, then the default draw framebuffer is
3944 * queried."
3945 */
3946 buffer = ctx->WinSysDrawBuffer;
3947 }
3948
3949 _mesa_get_framebuffer_attachment_parameter(ctx, buffer, attachment, pname,
3950 params,
3951 "glGetNamedFramebufferAttachmentParameteriv");
3952 }
3953
3954
3955 void GLAPIENTRY
3956 _mesa_NamedFramebufferParameteri(GLuint framebuffer, GLenum pname,
3957 GLint param)
3958 {
3959 GET_CURRENT_CONTEXT(ctx);
3960 struct gl_framebuffer *fb = NULL;
3961
3962 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
3963 _mesa_error(ctx, GL_INVALID_OPERATION,
3964 "glNamedFramebufferParameteri("
3965 "ARB_framebuffer_no_attachments not implemented)");
3966 return;
3967 }
3968
3969 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
3970 "glNamedFramebufferParameteri");
3971
3972 if (fb) {
3973 framebuffer_parameteri(ctx, fb, pname, param,
3974 "glNamedFramebufferParameteriv");
3975 }
3976 }
3977
3978
3979 void GLAPIENTRY
3980 _mesa_GetNamedFramebufferParameteriv(GLuint framebuffer, GLenum pname,
3981 GLint *param)
3982 {
3983 GET_CURRENT_CONTEXT(ctx);
3984 struct gl_framebuffer *fb;
3985
3986 if (!ctx->Extensions.ARB_framebuffer_no_attachments) {
3987 _mesa_error(ctx, GL_INVALID_OPERATION,
3988 "glNamedFramebufferParameteriv("
3989 "ARB_framebuffer_no_attachments not implemented)");
3990 return;
3991 }
3992
3993 if (framebuffer) {
3994 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
3995 "glGetNamedFramebufferParameteriv");
3996 } else {
3997 fb = ctx->WinSysDrawBuffer;
3998 }
3999
4000 if (fb) {
4001 get_framebuffer_parameteriv(ctx, fb, pname, param,
4002 "glGetNamedFramebufferParameteriv");
4003 }
4004 }
4005
4006
4007 static void
4008 invalidate_framebuffer_storage(struct gl_context *ctx,
4009 struct gl_framebuffer *fb,
4010 GLsizei numAttachments,
4011 const GLenum *attachments, GLint x, GLint y,
4012 GLsizei width, GLsizei height, const char *name)
4013 {
4014 int i;
4015
4016 /* Section 17.4 Whole Framebuffer Operations of the OpenGL 4.5 Core
4017 * Spec (2.2.2015, PDF page 522) says:
4018 * "An INVALID_VALUE error is generated if numAttachments, width, or
4019 * height is negative."
4020 */
4021 if (numAttachments < 0) {
4022 _mesa_error(ctx, GL_INVALID_VALUE,
4023 "%s(numAttachments < 0)", name);
4024 return;
4025 }
4026
4027 if (width < 0) {
4028 _mesa_error(ctx, GL_INVALID_VALUE,
4029 "%s(width < 0)", name);
4030 return;
4031 }
4032
4033 if (height < 0) {
4034 _mesa_error(ctx, GL_INVALID_VALUE,
4035 "%s(height < 0)", name);
4036 return;
4037 }
4038
4039 /* The GL_ARB_invalidate_subdata spec says:
4040 *
4041 * "If an attachment is specified that does not exist in the
4042 * framebuffer bound to <target>, it is ignored."
4043 *
4044 * It also says:
4045 *
4046 * "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
4047 * or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
4048 * INVALID_OPERATION is generated."
4049 *
4050 * No mention is made of GL_AUXi being out of range. Therefore, we allow
4051 * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
4052 * set of retrictions).
4053 */
4054 for (i = 0; i < numAttachments; i++) {
4055 if (_mesa_is_winsys_fbo(fb)) {
4056 switch (attachments[i]) {
4057 case GL_ACCUM:
4058 case GL_AUX0:
4059 case GL_AUX1:
4060 case GL_AUX2:
4061 case GL_AUX3:
4062 /* Accumulation buffers and auxilary buffers were removed in
4063 * OpenGL 3.1, and they never existed in OpenGL ES.
4064 */
4065 if (ctx->API != API_OPENGL_COMPAT)
4066 goto invalid_enum;
4067 break;
4068 case GL_COLOR:
4069 case GL_DEPTH:
4070 case GL_STENCIL:
4071 break;
4072 case GL_BACK_LEFT:
4073 case GL_BACK_RIGHT:
4074 case GL_FRONT_LEFT:
4075 case GL_FRONT_RIGHT:
4076 if (!_mesa_is_desktop_gl(ctx))
4077 goto invalid_enum;
4078 break;
4079 default:
4080 goto invalid_enum;
4081 }
4082 } else {
4083 switch (attachments[i]) {
4084 case GL_DEPTH_ATTACHMENT:
4085 case GL_STENCIL_ATTACHMENT:
4086 break;
4087 case GL_DEPTH_STENCIL_ATTACHMENT:
4088 /* GL_DEPTH_STENCIL_ATTACHMENT is a valid attachment point only
4089 * in desktop and ES 3.0 profiles. Note that OES_packed_depth_stencil
4090 * extension does not make this attachment point valid on ES 2.0.
4091 */
4092 if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx))
4093 break;
4094 /* fallthrough */
4095 case GL_COLOR_ATTACHMENT0:
4096 case GL_COLOR_ATTACHMENT1:
4097 case GL_COLOR_ATTACHMENT2:
4098 case GL_COLOR_ATTACHMENT3:
4099 case GL_COLOR_ATTACHMENT4:
4100 case GL_COLOR_ATTACHMENT5:
4101 case GL_COLOR_ATTACHMENT6:
4102 case GL_COLOR_ATTACHMENT7:
4103 case GL_COLOR_ATTACHMENT8:
4104 case GL_COLOR_ATTACHMENT9:
4105 case GL_COLOR_ATTACHMENT10:
4106 case GL_COLOR_ATTACHMENT11:
4107 case GL_COLOR_ATTACHMENT12:
4108 case GL_COLOR_ATTACHMENT13:
4109 case GL_COLOR_ATTACHMENT14:
4110 case GL_COLOR_ATTACHMENT15: {
4111 unsigned k = attachments[i] - GL_COLOR_ATTACHMENT0;
4112 if (k >= ctx->Const.MaxColorAttachments) {
4113 _mesa_error(ctx, GL_INVALID_OPERATION,
4114 "%s(attachment >= max. color attachments)", name);
4115 return;
4116 }
4117 break;
4118 }
4119 default:
4120 goto invalid_enum;
4121 }
4122 }
4123 }
4124
4125 /* We don't actually do anything for this yet. Just return after
4126 * validating the parameters and generating the required errors.
4127 */
4128 return;
4129
4130 invalid_enum:
4131 _mesa_error(ctx, GL_INVALID_ENUM, "%s(invalid attachment %s)", name,
4132 _mesa_enum_to_string(attachments[i]));
4133 return;
4134 }
4135
4136
4137 void GLAPIENTRY
4138 _mesa_InvalidateSubFramebuffer(GLenum target, GLsizei numAttachments,
4139 const GLenum *attachments, GLint x, GLint y,
4140 GLsizei width, GLsizei height)
4141 {
4142 struct gl_framebuffer *fb;
4143 GET_CURRENT_CONTEXT(ctx);
4144
4145 fb = get_framebuffer_target(ctx, target);
4146 if (!fb) {
4147 _mesa_error(ctx, GL_INVALID_ENUM,
4148 "glInvalidateSubFramebuffer(invalid target %s)",
4149 _mesa_enum_to_string(target));
4150 return;
4151 }
4152
4153 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
4154 x, y, width, height,
4155 "glInvalidateSubFramebuffer");
4156 }
4157
4158
4159 void GLAPIENTRY
4160 _mesa_InvalidateNamedFramebufferSubData(GLuint framebuffer,
4161 GLsizei numAttachments,
4162 const GLenum *attachments,
4163 GLint x, GLint y,
4164 GLsizei width, GLsizei height)
4165 {
4166 struct gl_framebuffer *fb;
4167 GET_CURRENT_CONTEXT(ctx);
4168
4169 /* The OpenGL 4.5 core spec (02.02.2015) says (in Section 17.4 Whole
4170 * Framebuffer Operations, PDF page 522): "If framebuffer is zero, the
4171 * default draw framebuffer is affected."
4172 */
4173 if (framebuffer) {
4174 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
4175 "glInvalidateNamedFramebufferSubData");
4176 if (!fb)
4177 return;
4178 }
4179 else
4180 fb = ctx->WinSysDrawBuffer;
4181
4182 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
4183 x, y, width, height,
4184 "glInvalidateNamedFramebufferSubData");
4185 }
4186
4187
4188 void GLAPIENTRY
4189 _mesa_InvalidateFramebuffer(GLenum target, GLsizei numAttachments,
4190 const GLenum *attachments)
4191 {
4192 struct gl_framebuffer *fb;
4193 GET_CURRENT_CONTEXT(ctx);
4194
4195 fb = get_framebuffer_target(ctx, target);
4196 if (!fb) {
4197 _mesa_error(ctx, GL_INVALID_ENUM,
4198 "glInvalidateFramebuffer(invalid target %s)",
4199 _mesa_enum_to_string(target));
4200 return;
4201 }
4202
4203 /* The GL_ARB_invalidate_subdata spec says:
4204 *
4205 * "The command
4206 *
4207 * void InvalidateFramebuffer(enum target,
4208 * sizei numAttachments,
4209 * const enum *attachments);
4210 *
4211 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
4212 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
4213 * <MAX_VIEWPORT_DIMS[1]> respectively."
4214 */
4215 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
4216 0, 0,
4217 ctx->Const.MaxViewportWidth,
4218 ctx->Const.MaxViewportHeight,
4219 "glInvalidateFramebuffer");
4220 }
4221
4222
4223 void GLAPIENTRY
4224 _mesa_InvalidateNamedFramebufferData(GLuint framebuffer,
4225 GLsizei numAttachments,
4226 const GLenum *attachments)
4227 {
4228 struct gl_framebuffer *fb;
4229 GET_CURRENT_CONTEXT(ctx);
4230
4231 /* The OpenGL 4.5 core spec (02.02.2015) says (in Section 17.4 Whole
4232 * Framebuffer Operations, PDF page 522): "If framebuffer is zero, the
4233 * default draw framebuffer is affected."
4234 */
4235 if (framebuffer) {
4236 fb = _mesa_lookup_framebuffer_err(ctx, framebuffer,
4237 "glInvalidateNamedFramebufferData");
4238 if (!fb)
4239 return;
4240 }
4241 else
4242 fb = ctx->WinSysDrawBuffer;
4243
4244 /* The GL_ARB_invalidate_subdata spec says:
4245 *
4246 * "The command
4247 *
4248 * void InvalidateFramebuffer(enum target,
4249 * sizei numAttachments,
4250 * const enum *attachments);
4251 *
4252 * is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
4253 * <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
4254 * <MAX_VIEWPORT_DIMS[1]> respectively."
4255 */
4256 invalidate_framebuffer_storage(ctx, fb, numAttachments, attachments,
4257 0, 0,
4258 ctx->Const.MaxViewportWidth,
4259 ctx->Const.MaxViewportHeight,
4260 "glInvalidateNamedFramebufferData");
4261 }
4262
4263
4264 void GLAPIENTRY
4265 _mesa_DiscardFramebufferEXT(GLenum target, GLsizei numAttachments,
4266 const GLenum *attachments)
4267 {
4268 struct gl_framebuffer *fb;
4269 GLint i;
4270
4271 GET_CURRENT_CONTEXT(ctx);
4272
4273 fb = get_framebuffer_target(ctx, target);
4274 if (!fb) {
4275 _mesa_error(ctx, GL_INVALID_ENUM,
4276 "glDiscardFramebufferEXT(target %s)",
4277 _mesa_enum_to_string(target));
4278 return;
4279 }
4280
4281 if (numAttachments < 0) {
4282 _mesa_error(ctx, GL_INVALID_VALUE,
4283 "glDiscardFramebufferEXT(numAttachments < 0)");
4284 return;
4285 }
4286
4287 for (i = 0; i < numAttachments; i++) {
4288 switch (attachments[i]) {
4289 case GL_COLOR:
4290 case GL_DEPTH:
4291 case GL_STENCIL:
4292 if (_mesa_is_user_fbo(fb))
4293 goto invalid_enum;
4294 break;
4295 case GL_COLOR_ATTACHMENT0:
4296 case GL_DEPTH_ATTACHMENT:
4297 case GL_STENCIL_ATTACHMENT:
4298 if (_mesa_is_winsys_fbo(fb))
4299 goto invalid_enum;
4300 break;
4301 default:
4302 goto invalid_enum;
4303 }
4304 }
4305
4306 if (ctx->Driver.DiscardFramebuffer)
4307 ctx->Driver.DiscardFramebuffer(ctx, target, numAttachments, attachments);
4308
4309 return;
4310
4311 invalid_enum:
4312 _mesa_error(ctx, GL_INVALID_ENUM,
4313 "glDiscardFramebufferEXT(attachment %s)",
4314 _mesa_enum_to_string(attachments[i]));
4315 }