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