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