OSDN Git Service

mesa: Reject texture-only formats as renderbuffer formats in ES 3
[android-x86/external-mesa.git] / src / mesa / main / fbobject.c
1 /*
2  * Mesa 3-D graphics library
3  * Version:  7.1
4  *
5  * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
6  * Copyright (C) 1999-2009  VMware, Inc.  All Rights Reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included
16  * in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR 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 "mfeatures.h"
46 #include "mtypes.h"
47 #include "renderbuffer.h"
48 #include "state.h"
49 #include "teximage.h"
50 #include "texobj.h"
51
52
53 /** Set this to 1 to debug/log glBlitFramebuffer() calls */
54 #define DEBUG_BLIT 0
55
56
57 /**
58  * Notes:
59  *
60  * None of the GL_EXT_framebuffer_object functions are compiled into
61  * display lists.
62  */
63
64
65
66 /*
67  * When glGenRender/FramebuffersEXT() is called we insert pointers to
68  * these placeholder objects into the hash table.
69  * Later, when the object ID is first bound, we replace the placeholder
70  * with the real frame/renderbuffer.
71  */
72 static struct gl_framebuffer DummyFramebuffer;
73 static struct gl_renderbuffer DummyRenderbuffer;
74
75 /* We bind this framebuffer when applications pass a NULL
76  * drawable/surface in make current. */
77 static struct gl_framebuffer IncompleteFramebuffer;
78
79
80 static void
81 delete_dummy_renderbuffer(struct gl_context *ctx, struct gl_renderbuffer *rb)
82 {
83    /* no op */
84 }
85
86 static void
87 delete_dummy_framebuffer(struct gl_framebuffer *fb)
88 {
89    /* no op */
90 }
91
92
93 void
94 _mesa_init_fbobjects(struct gl_context *ctx)
95 {
96    _glthread_INIT_MUTEX(DummyFramebuffer.Mutex);
97    _glthread_INIT_MUTEX(DummyRenderbuffer.Mutex);
98    _glthread_INIT_MUTEX(IncompleteFramebuffer.Mutex);
99    DummyFramebuffer.Delete = delete_dummy_framebuffer;
100    DummyRenderbuffer.Delete = delete_dummy_renderbuffer;
101    IncompleteFramebuffer.Delete = delete_dummy_framebuffer;
102 }
103
104 struct gl_framebuffer *
105 _mesa_get_incomplete_framebuffer(void)
106 {
107    return &IncompleteFramebuffer;
108 }
109
110 /**
111  * Helper routine for getting a gl_renderbuffer.
112  */
113 struct gl_renderbuffer *
114 _mesa_lookup_renderbuffer(struct gl_context *ctx, GLuint id)
115 {
116    struct gl_renderbuffer *rb;
117
118    if (id == 0)
119       return NULL;
120
121    rb = (struct gl_renderbuffer *)
122       _mesa_HashLookup(ctx->Shared->RenderBuffers, id);
123    return rb;
124 }
125
126
127 /**
128  * Helper routine for getting a gl_framebuffer.
129  */
130 struct gl_framebuffer *
131 _mesa_lookup_framebuffer(struct gl_context *ctx, GLuint id)
132 {
133    struct gl_framebuffer *fb;
134
135    if (id == 0)
136       return NULL;
137
138    fb = (struct gl_framebuffer *)
139       _mesa_HashLookup(ctx->Shared->FrameBuffers, id);
140    return fb;
141 }
142
143
144 /**
145  * Mark the given framebuffer as invalid.  This will force the
146  * test for framebuffer completeness to be done before the framebuffer
147  * is used.
148  */
149 static void
150 invalidate_framebuffer(struct gl_framebuffer *fb)
151 {
152    fb->_Status = 0; /* "indeterminate" */
153 }
154
155
156 /**
157  * Return the gl_framebuffer object which corresponds to the given
158  * framebuffer target, such as GL_DRAW_FRAMEBUFFER.
159  * Check support for GL_EXT_framebuffer_blit to determine if certain
160  * targets are legal.
161  * \return gl_framebuffer pointer or NULL if target is illegal
162  */
163 static struct gl_framebuffer *
164 get_framebuffer_target(struct gl_context *ctx, GLenum target)
165 {
166    bool have_fb_blit = _mesa_is_gles3(ctx) ||
167       (ctx->Extensions.EXT_framebuffer_blit && _mesa_is_desktop_gl(ctx));
168    switch (target) {
169    case GL_DRAW_FRAMEBUFFER:
170       return have_fb_blit ? ctx->DrawBuffer : NULL;
171    case GL_READ_FRAMEBUFFER:
172       return have_fb_blit ? ctx->ReadBuffer : NULL;
173    case GL_FRAMEBUFFER_EXT:
174       return ctx->DrawBuffer;
175    default:
176       return NULL;
177    }
178 }
179
180
181 /**
182  * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
183  * gl_renderbuffer_attachment object.
184  * This function is only used for user-created FB objects, not the
185  * default / window-system FB object.
186  * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
187  * the depth buffer attachment point.
188  */
189 struct gl_renderbuffer_attachment *
190 _mesa_get_attachment(struct gl_context *ctx, struct gl_framebuffer *fb,
191                      GLenum attachment)
192 {
193    GLuint i;
194
195    assert(_mesa_is_user_fbo(fb));
196
197    switch (attachment) {
198    case GL_COLOR_ATTACHMENT0_EXT:
199    case GL_COLOR_ATTACHMENT1_EXT:
200    case GL_COLOR_ATTACHMENT2_EXT:
201    case GL_COLOR_ATTACHMENT3_EXT:
202    case GL_COLOR_ATTACHMENT4_EXT:
203    case GL_COLOR_ATTACHMENT5_EXT:
204    case GL_COLOR_ATTACHMENT6_EXT:
205    case GL_COLOR_ATTACHMENT7_EXT:
206    case GL_COLOR_ATTACHMENT8_EXT:
207    case GL_COLOR_ATTACHMENT9_EXT:
208    case GL_COLOR_ATTACHMENT10_EXT:
209    case GL_COLOR_ATTACHMENT11_EXT:
210    case GL_COLOR_ATTACHMENT12_EXT:
211    case GL_COLOR_ATTACHMENT13_EXT:
212    case GL_COLOR_ATTACHMENT14_EXT:
213    case GL_COLOR_ATTACHMENT15_EXT:
214       /* Only OpenGL ES 1.x forbids color attachments other than
215        * GL_COLOR_ATTACHMENT0.  For all other APIs the limit set by the
216        * hardware is used.
217        */
218       i = attachment - GL_COLOR_ATTACHMENT0_EXT;
219       if (i >= ctx->Const.MaxColorAttachments
220           || (i > 0 && ctx->API == API_OPENGLES)) {
221          return NULL;
222       }
223       return &fb->Attachment[BUFFER_COLOR0 + i];
224    case GL_DEPTH_STENCIL_ATTACHMENT:
225       if (!_mesa_is_desktop_gl(ctx) && !_mesa_is_gles3(ctx))
226          return NULL;
227       /* fall-through */
228    case GL_DEPTH_ATTACHMENT_EXT:
229       return &fb->Attachment[BUFFER_DEPTH];
230    case GL_STENCIL_ATTACHMENT_EXT:
231       return &fb->Attachment[BUFFER_STENCIL];
232    default:
233       return NULL;
234    }
235 }
236
237
238 /**
239  * As above, but only used for getting attachments of the default /
240  * window-system framebuffer (not user-created framebuffer objects).
241  */
242 static struct gl_renderbuffer_attachment *
243 _mesa_get_fb0_attachment(struct gl_context *ctx, struct gl_framebuffer *fb,
244                          GLenum attachment)
245 {
246    assert(_mesa_is_winsys_fbo(fb));
247
248    switch (attachment) {
249    case GL_FRONT_LEFT:
250       return &fb->Attachment[BUFFER_FRONT_LEFT];
251    case GL_FRONT_RIGHT:
252       return &fb->Attachment[BUFFER_FRONT_RIGHT];
253    case GL_BACK_LEFT:
254       return &fb->Attachment[BUFFER_BACK_LEFT];
255    case GL_BACK_RIGHT:
256       return &fb->Attachment[BUFFER_BACK_RIGHT];
257    case GL_AUX0:
258       if (fb->Visual.numAuxBuffers == 1) {
259          return &fb->Attachment[BUFFER_AUX0];
260       }
261       return NULL;
262
263    /* Page 336 (page 352 of the PDF) of the OpenGL 3.0 spec says:
264     *
265     *     "If the default framebuffer is bound to target, then attachment must
266     *     be one of FRONT LEFT, FRONT RIGHT, BACK LEFT, BACK RIGHT, or AUXi,
267     *     identifying a color buffer; DEPTH, identifying the depth buffer; or
268     *     STENCIL, identifying the stencil buffer."
269     *
270     * Revision #34 of the ARB_framebuffer_object spec has essentially the same
271     * language.  However, revision #33 of the ARB_framebuffer_object spec
272     * says:
273     *
274     *     "If the default framebuffer is bound to <target>, then <attachment>
275     *     must be one of FRONT_LEFT, FRONT_RIGHT, BACK_LEFT, BACK_RIGHT, AUXi,
276     *     DEPTH_BUFFER, or STENCIL_BUFFER, identifying a color buffer, the
277     *     depth buffer, or the stencil buffer, and <pname> may be
278     *     FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE or
279     *     FRAMEBUFFER_ATTACHMENT_OBJECT_NAME."
280     *
281     * The enum values for DEPTH_BUFFER and STENCIL_BUFFER have been removed
282     * from glext.h, so shipping apps should not use those values.
283     *
284     * Note that neither EXT_framebuffer_object nor OES_framebuffer_object
285     * support queries of the window system FBO.
286     */
287    case GL_DEPTH:
288       return &fb->Attachment[BUFFER_DEPTH];
289    case GL_STENCIL:
290       return &fb->Attachment[BUFFER_STENCIL];
291    default:
292       return NULL;
293    }
294 }
295
296
297
298 /**
299  * Remove any texture or renderbuffer attached to the given attachment
300  * point.  Update reference counts, etc.
301  */
302 void
303 _mesa_remove_attachment(struct gl_context *ctx,
304                         struct gl_renderbuffer_attachment *att)
305 {
306    if (att->Type == GL_TEXTURE) {
307       ASSERT(att->Texture);
308       if (ctx->Driver.FinishRenderTexture) {
309          /* tell driver that we're done rendering to this texture. */
310          ctx->Driver.FinishRenderTexture(ctx, att);
311       }
312       _mesa_reference_texobj(&att->Texture, NULL); /* unbind */
313       ASSERT(!att->Texture);
314    }
315    if (att->Type == GL_TEXTURE || att->Type == GL_RENDERBUFFER_EXT) {
316       ASSERT(!att->Texture);
317       _mesa_reference_renderbuffer(&att->Renderbuffer, NULL); /* unbind */
318       ASSERT(!att->Renderbuffer);
319    }
320    att->Type = GL_NONE;
321    att->Complete = GL_TRUE;
322 }
323
324
325 /**
326  * Bind a texture object to an attachment point.
327  * The previous binding, if any, will be removed first.
328  */
329 void
330 _mesa_set_texture_attachment(struct gl_context *ctx,
331                              struct gl_framebuffer *fb,
332                              struct gl_renderbuffer_attachment *att,
333                              struct gl_texture_object *texObj,
334                              GLenum texTarget, GLuint level, GLuint zoffset)
335 {
336    if (att->Texture == texObj) {
337       /* re-attaching same texture */
338       ASSERT(att->Type == GL_TEXTURE);
339       if (ctx->Driver.FinishRenderTexture)
340          ctx->Driver.FinishRenderTexture(ctx, att);
341    }
342    else {
343       /* new attachment */
344       if (ctx->Driver.FinishRenderTexture && att->Texture)
345          ctx->Driver.FinishRenderTexture(ctx, att);
346       _mesa_remove_attachment(ctx, att);
347       att->Type = GL_TEXTURE;
348       assert(!att->Texture);
349       _mesa_reference_texobj(&att->Texture, texObj);
350    }
351
352    /* always update these fields */
353    att->TextureLevel = level;
354    att->CubeMapFace = _mesa_tex_target_to_face(texTarget);
355    att->Zoffset = zoffset;
356    att->Complete = GL_FALSE;
357
358    if (_mesa_get_attachment_teximage(att)) {
359       ctx->Driver.RenderTexture(ctx, fb, att);
360    }
361
362    invalidate_framebuffer(fb);
363 }
364
365
366 /**
367  * Bind a renderbuffer to an attachment point.
368  * The previous binding, if any, will be removed first.
369  */
370 void
371 _mesa_set_renderbuffer_attachment(struct gl_context *ctx,
372                                   struct gl_renderbuffer_attachment *att,
373                                   struct gl_renderbuffer *rb)
374 {
375    /* XXX check if re-doing same attachment, exit early */
376    _mesa_remove_attachment(ctx, att);
377    att->Type = GL_RENDERBUFFER_EXT;
378    att->Texture = NULL; /* just to be safe */
379    att->Complete = GL_FALSE;
380    _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
381 }
382
383
384 /**
385  * Fallback for ctx->Driver.FramebufferRenderbuffer()
386  * Attach a renderbuffer object to a framebuffer object.
387  */
388 void
389 _mesa_framebuffer_renderbuffer(struct gl_context *ctx,
390                                struct gl_framebuffer *fb,
391                                GLenum attachment, struct gl_renderbuffer *rb)
392 {
393    struct gl_renderbuffer_attachment *att;
394
395    _glthread_LOCK_MUTEX(fb->Mutex);
396
397    att = _mesa_get_attachment(ctx, fb, attachment);
398    ASSERT(att);
399    if (rb) {
400       _mesa_set_renderbuffer_attachment(ctx, att, rb);
401       if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
402          /* do stencil attachment here (depth already done above) */
403          att = _mesa_get_attachment(ctx, fb, GL_STENCIL_ATTACHMENT_EXT);
404          assert(att);
405          _mesa_set_renderbuffer_attachment(ctx, att, rb);
406       }
407       rb->AttachedAnytime = GL_TRUE;
408    }
409    else {
410       _mesa_remove_attachment(ctx, att);
411    }
412
413    invalidate_framebuffer(fb);
414
415    _glthread_UNLOCK_MUTEX(fb->Mutex);
416 }
417
418
419 /**
420  * Fallback for ctx->Driver.ValidateFramebuffer()
421  * Check if the renderbuffer's formats are supported by the software
422  * renderer.
423  * Drivers should probably override this.
424  */
425 void
426 _mesa_validate_framebuffer(struct gl_context *ctx, struct gl_framebuffer *fb)
427 {
428    gl_buffer_index buf;
429    for (buf = 0; buf < BUFFER_COUNT; buf++) {
430       const struct gl_renderbuffer *rb = fb->Attachment[buf].Renderbuffer;
431       if (rb) {
432          switch (rb->_BaseFormat) {
433          case GL_ALPHA:
434          case GL_LUMINANCE_ALPHA:
435          case GL_LUMINANCE:
436          case GL_INTENSITY:
437          case GL_RED:
438          case GL_RG:
439             fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
440             return;
441
442          default:
443             switch (rb->Format) {
444             /* XXX This list is likely incomplete. */
445             case MESA_FORMAT_RGB9_E5_FLOAT:
446                fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
447                return;
448             default:;
449                /* render buffer format is supported by software rendering */
450             }
451          }
452       }
453    }
454 }
455
456
457 /**
458  * For debug only.
459  */
460 static void
461 att_incomplete(const char *msg)
462 {
463    if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_FBO) {
464       _mesa_debug(NULL, "attachment incomplete: %s\n", msg);
465    }
466 }
467
468
469 /**
470  * For debug only.
471  */
472 static void
473 fbo_incomplete(const char *msg, int index)
474 {
475    if (MESA_DEBUG_FLAGS & DEBUG_INCOMPLETE_FBO) {
476       _mesa_debug(NULL, "FBO Incomplete: %s [%d]\n", msg, index);
477    }
478 }
479
480
481 /**
482  * Is the given base format a legal format for a color renderbuffer?
483  */
484 GLboolean
485 _mesa_is_legal_color_format(const struct gl_context *ctx, GLenum baseFormat)
486 {
487    switch (baseFormat) {
488    case GL_RGB:
489    case GL_RGBA:
490       return GL_TRUE;
491    case GL_LUMINANCE:
492    case GL_LUMINANCE_ALPHA:
493    case GL_INTENSITY:
494    case GL_ALPHA:
495       return ctx->API == API_OPENGL_COMPAT &&
496              ctx->Extensions.ARB_framebuffer_object;
497    case GL_RED:
498    case GL_RG:
499       return ctx->Extensions.ARB_texture_rg;
500    default:
501       return GL_FALSE;
502    }
503 }
504
505
506 /**
507  * Is the given base format a legal format for a depth/stencil renderbuffer?
508  */
509 static GLboolean
510 is_legal_depth_format(const struct gl_context *ctx, GLenum baseFormat)
511 {
512    switch (baseFormat) {
513    case GL_DEPTH_COMPONENT:
514    case GL_DEPTH_STENCIL_EXT:
515       return GL_TRUE;
516    default:
517       return GL_FALSE;
518    }
519 }
520
521
522 /**
523  * Test if an attachment point is complete and update its Complete field.
524  * \param format if GL_COLOR, this is a color attachment point,
525  *               if GL_DEPTH, this is a depth component attachment point,
526  *               if GL_STENCIL, this is a stencil component attachment point.
527  */
528 static void
529 test_attachment_completeness(const struct gl_context *ctx, GLenum format,
530                              struct gl_renderbuffer_attachment *att)
531 {
532    assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
533
534    /* assume complete */
535    att->Complete = GL_TRUE;
536
537    /* Look for reasons why the attachment might be incomplete */
538    if (att->Type == GL_TEXTURE) {
539       const struct gl_texture_object *texObj = att->Texture;
540       struct gl_texture_image *texImage;
541       GLenum baseFormat;
542
543       if (!texObj) {
544          att_incomplete("no texobj");
545          att->Complete = GL_FALSE;
546          return;
547       }
548
549       texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
550       if (!texImage) {
551          att_incomplete("no teximage");
552          att->Complete = GL_FALSE;
553          return;
554       }
555       if (texImage->Width < 1 || texImage->Height < 1) {
556          att_incomplete("teximage width/height=0");
557          printf("texobj = %u\n", texObj->Name);
558          printf("level = %d\n", att->TextureLevel);
559          att->Complete = GL_FALSE;
560          return;
561       }
562       if (texObj->Target == GL_TEXTURE_3D && att->Zoffset >= texImage->Depth) {
563          att_incomplete("bad z offset");
564          att->Complete = GL_FALSE;
565          return;
566       }
567
568       baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
569
570       if (format == GL_COLOR) {
571          if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
572             att_incomplete("bad format");
573             att->Complete = GL_FALSE;
574             return;
575          }
576          if (_mesa_is_format_compressed(texImage->TexFormat)) {
577             att_incomplete("compressed internalformat");
578             att->Complete = GL_FALSE;
579             return;
580          }
581       }
582       else if (format == GL_DEPTH) {
583          if (baseFormat == GL_DEPTH_COMPONENT) {
584             /* OK */
585          }
586          else if (ctx->Extensions.EXT_packed_depth_stencil &&
587                   ctx->Extensions.ARB_depth_texture &&
588                   baseFormat == GL_DEPTH_STENCIL_EXT) {
589             /* OK */
590          }
591          else {
592             att->Complete = GL_FALSE;
593             att_incomplete("bad depth format");
594             return;
595          }
596       }
597       else {
598          ASSERT(format == GL_STENCIL);
599          if (ctx->Extensions.EXT_packed_depth_stencil &&
600              ctx->Extensions.ARB_depth_texture &&
601              baseFormat == GL_DEPTH_STENCIL_EXT) {
602             /* OK */
603          }
604          else {
605             /* no such thing as stencil-only textures */
606             att_incomplete("illegal stencil texture");
607             att->Complete = GL_FALSE;
608             return;
609          }
610       }
611    }
612    else if (att->Type == GL_RENDERBUFFER_EXT) {
613       const GLenum baseFormat =
614          _mesa_get_format_base_format(att->Renderbuffer->Format);
615
616       ASSERT(att->Renderbuffer);
617       if (!att->Renderbuffer->InternalFormat ||
618           att->Renderbuffer->Width < 1 ||
619           att->Renderbuffer->Height < 1) {
620          att_incomplete("0x0 renderbuffer");
621          att->Complete = GL_FALSE;
622          return;
623       }
624       if (format == GL_COLOR) {
625          if (!_mesa_is_legal_color_format(ctx, baseFormat)) {
626             att_incomplete("bad renderbuffer color format");
627             att->Complete = GL_FALSE;
628             return;
629          }
630       }
631       else if (format == GL_DEPTH) {
632          if (baseFormat == GL_DEPTH_COMPONENT) {
633             /* OK */
634          }
635          else if (ctx->Extensions.EXT_packed_depth_stencil &&
636                   baseFormat == GL_DEPTH_STENCIL_EXT) {
637             /* OK */
638          }
639          else {
640             att_incomplete("bad renderbuffer depth format");
641             att->Complete = GL_FALSE;
642             return;
643          }
644       }
645       else {
646          assert(format == GL_STENCIL);
647          if (baseFormat == GL_STENCIL_INDEX) {
648             /* OK */
649          }
650          else if (ctx->Extensions.EXT_packed_depth_stencil &&
651                   baseFormat == GL_DEPTH_STENCIL_EXT) {
652             /* OK */
653          }
654          else {
655             att->Complete = GL_FALSE;
656             att_incomplete("bad renderbuffer stencil format");
657             return;
658          }
659       }
660    }
661    else {
662       ASSERT(att->Type == GL_NONE);
663       /* complete */
664       return;
665    }
666 }
667
668
669 /**
670  * Test if the given framebuffer object is complete and update its
671  * Status field with the results.
672  * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
673  * driver to make hardware-specific validation/completeness checks.
674  * Also update the framebuffer's Width and Height fields if the
675  * framebuffer is complete.
676  */
677 void
678 _mesa_test_framebuffer_completeness(struct gl_context *ctx,
679                                     struct gl_framebuffer *fb)
680 {
681    GLuint numImages;
682    GLenum intFormat = GL_NONE; /* color buffers' internal format */
683    GLuint minWidth = ~0, minHeight = ~0, maxWidth = 0, maxHeight = 0;
684    GLint numSamples = -1;
685    GLint i;
686    GLuint j;
687
688    assert(_mesa_is_user_fbo(fb));
689
690    /* we're changing framebuffer fields here */
691    FLUSH_VERTICES(ctx, _NEW_BUFFERS);
692
693    numImages = 0;
694    fb->Width = 0;
695    fb->Height = 0;
696
697    /* Start at -2 to more easily loop over all attachment points.
698     *  -2: depth buffer
699     *  -1: stencil buffer
700     * >=0: color buffer
701     */
702    for (i = -2; i < (GLint) ctx->Const.MaxColorAttachments; i++) {
703       struct gl_renderbuffer_attachment *att;
704       GLenum f;
705       gl_format attFormat;
706
707       /*
708        * XXX for ARB_fbo, only check color buffers that are named by
709        * GL_READ_BUFFER and GL_DRAW_BUFFERi.
710        */
711
712       /* check for attachment completeness
713        */
714       if (i == -2) {
715          att = &fb->Attachment[BUFFER_DEPTH];
716          test_attachment_completeness(ctx, GL_DEPTH, att);
717          if (!att->Complete) {
718             fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
719             fbo_incomplete("depth attachment incomplete", -1);
720             return;
721          }
722       }
723       else if (i == -1) {
724          att = &fb->Attachment[BUFFER_STENCIL];
725          test_attachment_completeness(ctx, GL_STENCIL, att);
726          if (!att->Complete) {
727             fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
728             fbo_incomplete("stencil attachment incomplete", -1);
729             return;
730          }
731       }
732       else {
733          att = &fb->Attachment[BUFFER_COLOR0 + i];
734          test_attachment_completeness(ctx, GL_COLOR, att);
735          if (!att->Complete) {
736             fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
737             fbo_incomplete("color attachment incomplete", i);
738             return;
739          }
740       }
741
742       /* get width, height, format of the renderbuffer/texture
743        */
744       if (att->Type == GL_TEXTURE) {
745          const struct gl_texture_image *texImg =
746             _mesa_get_attachment_teximage(att);
747          minWidth = MIN2(minWidth, texImg->Width);
748          maxWidth = MAX2(maxWidth, texImg->Width);
749          minHeight = MIN2(minHeight, texImg->Height);
750          maxHeight = MAX2(maxHeight, texImg->Height);
751          f = texImg->_BaseFormat;
752          attFormat = texImg->TexFormat;
753          numImages++;
754          if (!_mesa_is_legal_color_format(ctx, f) &&
755              !is_legal_depth_format(ctx, f)) {
756             fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
757             fbo_incomplete("texture attachment incomplete", -1);
758             return;
759          }
760       }
761       else if (att->Type == GL_RENDERBUFFER_EXT) {
762          minWidth = MIN2(minWidth, att->Renderbuffer->Width);
763          maxWidth = MAX2(minWidth, att->Renderbuffer->Width);
764          minHeight = MIN2(minHeight, att->Renderbuffer->Height);
765          maxHeight = MAX2(minHeight, att->Renderbuffer->Height);
766          f = att->Renderbuffer->InternalFormat;
767          attFormat = att->Renderbuffer->Format;
768          numImages++;
769       }
770       else {
771          assert(att->Type == GL_NONE);
772          continue;
773       }
774
775       if (att->Renderbuffer && numSamples < 0) {
776          /* first buffer */
777          numSamples = att->Renderbuffer->NumSamples;
778       }
779
780       /* check if integer color */
781       fb->_IntegerColor = _mesa_is_format_integer_color(attFormat);
782
783       /* Error-check width, height, format, samples
784        */
785       if (numImages == 1) {
786          /* save format, num samples */
787          if (i >= 0) {
788             intFormat = f;
789          }
790       }
791       else {
792          if (!ctx->Extensions.ARB_framebuffer_object) {
793             /* check that width, height, format are same */
794             if (minWidth != maxWidth || minHeight != maxHeight) {
795                fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
796                fbo_incomplete("width or height mismatch", -1);
797                return;
798             }
799             /* check that all color buffers are the same format */
800             if (intFormat != GL_NONE && f != intFormat) {
801                fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
802                fbo_incomplete("format mismatch", -1);
803                return;
804             }
805          }
806          if (att->Renderbuffer &&
807              att->Renderbuffer->NumSamples != numSamples) {
808             fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
809             fbo_incomplete("inconsistant number of samples", i);
810             return;
811          }
812       }
813
814       /* Check that the format is valid. (MESA_FORMAT_NONE means unsupported)
815        */
816       if (att->Type == GL_RENDERBUFFER &&
817           att->Renderbuffer->Format == MESA_FORMAT_NONE) {
818          fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED;
819          fbo_incomplete("unsupported renderbuffer format", i);
820          return;
821       }
822    }
823
824    if (_mesa_is_desktop_gl(ctx) && !ctx->Extensions.ARB_ES2_compatibility) {
825       /* Check that all DrawBuffers are present */
826       for (j = 0; j < ctx->Const.MaxDrawBuffers; j++) {
827          if (fb->ColorDrawBuffer[j] != GL_NONE) {
828             const struct gl_renderbuffer_attachment *att
829                = _mesa_get_attachment(ctx, fb, fb->ColorDrawBuffer[j]);
830             assert(att);
831             if (att->Type == GL_NONE) {
832                fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
833                fbo_incomplete("missing drawbuffer", j);
834                return;
835             }
836          }
837       }
838
839       /* Check that the ReadBuffer is present */
840       if (fb->ColorReadBuffer != GL_NONE) {
841          const struct gl_renderbuffer_attachment *att
842             = _mesa_get_attachment(ctx, fb, fb->ColorReadBuffer);
843          assert(att);
844          if (att->Type == GL_NONE) {
845             fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
846             fbo_incomplete("missing readbuffer", -1);
847             return;
848          }
849       }
850    }
851
852    if (numImages == 0) {
853       fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
854       fbo_incomplete("no attachments", -1);
855       return;
856    }
857
858    /* Provisionally set status = COMPLETE ... */
859    fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
860
861    /* ... but the driver may say the FB is incomplete.
862     * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
863     * if anything.
864     */
865    if (ctx->Driver.ValidateFramebuffer) {
866       ctx->Driver.ValidateFramebuffer(ctx, fb);
867       if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
868          fbo_incomplete("driver marked FBO as incomplete", -1);
869       }
870    }
871
872    if (fb->_Status == GL_FRAMEBUFFER_COMPLETE_EXT) {
873       /*
874        * Note that if ARB_framebuffer_object is supported and the attached
875        * renderbuffers/textures are different sizes, the framebuffer
876        * width/height will be set to the smallest width/height.
877        */
878       fb->Width = minWidth;
879       fb->Height = minHeight;
880
881       /* finally, update the visual info for the framebuffer */
882       _mesa_update_framebuffer_visual(ctx, fb);
883    }
884 }
885
886
887 GLboolean GLAPIENTRY
888 _mesa_IsRenderbuffer(GLuint renderbuffer)
889 {
890    GET_CURRENT_CONTEXT(ctx);
891    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
892    if (renderbuffer) {
893       struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
894       if (rb != NULL && rb != &DummyRenderbuffer)
895          return GL_TRUE;
896    }
897    return GL_FALSE;
898 }
899
900
901 void GLAPIENTRY
902 _mesa_BindRenderbuffer(GLenum target, GLuint renderbuffer)
903 {
904    struct gl_renderbuffer *newRb;
905    GET_CURRENT_CONTEXT(ctx);
906
907    ASSERT_OUTSIDE_BEGIN_END(ctx);
908
909    if (target != GL_RENDERBUFFER_EXT) {
910       _mesa_error(ctx, GL_INVALID_ENUM, "glBindRenderbufferEXT(target)");
911       return;
912    }
913
914    /* No need to flush here since the render buffer binding has no
915     * effect on rendering state.
916     */
917
918    if (renderbuffer) {
919       newRb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
920       if (newRb == &DummyRenderbuffer) {
921          /* ID was reserved, but no real renderbuffer object made yet */
922          newRb = NULL;
923       }
924       else if (!newRb
925                && _mesa_is_desktop_gl(ctx)
926                && ctx->Extensions.ARB_framebuffer_object) {
927          /* All RB IDs must be Gen'd */
928          _mesa_error(ctx, GL_INVALID_OPERATION, "glBindRenderbuffer(buffer)");
929          return;
930       }
931
932       if (!newRb) {
933          /* create new renderbuffer object */
934          newRb = ctx->Driver.NewRenderbuffer(ctx, renderbuffer);
935          if (!newRb) {
936             _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindRenderbufferEXT");
937             return;
938          }
939          ASSERT(newRb->AllocStorage);
940          _mesa_HashInsert(ctx->Shared->RenderBuffers, renderbuffer, newRb);
941          newRb->RefCount = 1; /* referenced by hash table */
942       }
943    }
944    else {
945       newRb = NULL;
946    }
947
948    ASSERT(newRb != &DummyRenderbuffer);
949
950    _mesa_reference_renderbuffer(&ctx->CurrentRenderbuffer, newRb);
951 }
952
953
954 /**
955  * If the given renderbuffer is anywhere attached to the framebuffer, detach
956  * the renderbuffer.
957  * This is used when a renderbuffer object is deleted.
958  * The spec calls for unbinding.
959  */
960 static void
961 detach_renderbuffer(struct gl_context *ctx,
962                     struct gl_framebuffer *fb,
963                     struct gl_renderbuffer *rb)
964 {
965    GLuint i;
966    for (i = 0; i < BUFFER_COUNT; i++) {
967       if (fb->Attachment[i].Renderbuffer == rb) {
968          _mesa_remove_attachment(ctx, &fb->Attachment[i]);
969       }
970    }
971    invalidate_framebuffer(fb);
972 }
973
974
975 void GLAPIENTRY
976 _mesa_DeleteRenderbuffers(GLsizei n, const GLuint *renderbuffers)
977 {
978    GLint i;
979    GET_CURRENT_CONTEXT(ctx);
980
981    ASSERT_OUTSIDE_BEGIN_END(ctx);
982    FLUSH_VERTICES(ctx, _NEW_BUFFERS);
983
984    for (i = 0; i < n; i++) {
985       if (renderbuffers[i] > 0) {
986          struct gl_renderbuffer *rb;
987          rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
988          if (rb) {
989             /* check if deleting currently bound renderbuffer object */
990             if (rb == ctx->CurrentRenderbuffer) {
991                /* bind default */
992                ASSERT(rb->RefCount >= 2);
993                _mesa_BindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
994             }
995
996             if (_mesa_is_user_fbo(ctx->DrawBuffer)) {
997                detach_renderbuffer(ctx, ctx->DrawBuffer, rb);
998             }
999             if (_mesa_is_user_fbo(ctx->ReadBuffer)
1000                 && ctx->ReadBuffer != ctx->DrawBuffer) {
1001                detach_renderbuffer(ctx, ctx->ReadBuffer, rb);
1002             }
1003
1004             /* Remove from hash table immediately, to free the ID.
1005              * But the object will not be freed until it's no longer
1006              * referenced anywhere else.
1007              */
1008             _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
1009
1010             if (rb != &DummyRenderbuffer) {
1011                /* no longer referenced by hash table */
1012                _mesa_reference_renderbuffer(&rb, NULL);
1013             }
1014          }
1015       }
1016    }
1017 }
1018
1019
1020 void GLAPIENTRY
1021 _mesa_GenRenderbuffers(GLsizei n, GLuint *renderbuffers)
1022 {
1023    GET_CURRENT_CONTEXT(ctx);
1024    GLuint first;
1025    GLint i;
1026
1027    ASSERT_OUTSIDE_BEGIN_END(ctx);
1028
1029    if (n < 0) {
1030       _mesa_error(ctx, GL_INVALID_VALUE, "glGenRenderbuffersEXT(n)");
1031       return;
1032    }
1033
1034    if (!renderbuffers)
1035       return;
1036
1037    first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
1038
1039    for (i = 0; i < n; i++) {
1040       GLuint name = first + i;
1041       renderbuffers[i] = name;
1042       /* insert dummy placeholder into hash table */
1043       _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1044       _mesa_HashInsert(ctx->Shared->RenderBuffers, name, &DummyRenderbuffer);
1045       _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1046    }
1047 }
1048
1049
1050 /**
1051  * Given an internal format token for a render buffer, return the
1052  * corresponding base format (one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX,
1053  * GL_DEPTH_COMPONENT, GL_DEPTH_STENCIL_EXT, GL_ALPHA, GL_LUMINANCE,
1054  * GL_LUMINANCE_ALPHA, GL_INTENSITY, etc).
1055  *
1056  * This is similar to _mesa_base_tex_format() but the set of valid
1057  * internal formats is different.
1058  *
1059  * Note that even if a format is determined to be legal here, validation
1060  * of the FBO may fail if the format is not supported by the driver/GPU.
1061  *
1062  * \param internalFormat  as passed to glRenderbufferStorage()
1063  * \return the base internal format, or 0 if internalFormat is illegal
1064  */
1065 GLenum
1066 _mesa_base_fbo_format(struct gl_context *ctx, GLenum internalFormat)
1067 {
1068    /*
1069     * Notes: some formats such as alpha, luminance, etc. were added
1070     * with GL_ARB_framebuffer_object.
1071     */
1072    switch (internalFormat) {
1073    case GL_ALPHA:
1074    case GL_ALPHA4:
1075    case GL_ALPHA8:
1076    case GL_ALPHA12:
1077    case GL_ALPHA16:
1078       return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1079          ? GL_ALPHA : 0;
1080    case GL_LUMINANCE:
1081    case GL_LUMINANCE4:
1082    case GL_LUMINANCE8:
1083    case GL_LUMINANCE12:
1084    case GL_LUMINANCE16:
1085       return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1086          ? GL_LUMINANCE : 0;
1087    case GL_LUMINANCE_ALPHA:
1088    case GL_LUMINANCE4_ALPHA4:
1089    case GL_LUMINANCE6_ALPHA2:
1090    case GL_LUMINANCE8_ALPHA8:
1091    case GL_LUMINANCE12_ALPHA4:
1092    case GL_LUMINANCE12_ALPHA12:
1093    case GL_LUMINANCE16_ALPHA16:
1094       return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1095          ? GL_LUMINANCE_ALPHA : 0;
1096    case GL_INTENSITY:
1097    case GL_INTENSITY4:
1098    case GL_INTENSITY8:
1099    case GL_INTENSITY12:
1100    case GL_INTENSITY16:
1101       return ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_framebuffer_object
1102          ? GL_INTENSITY : 0;
1103    case GL_RGB8:
1104       return GL_RGB;
1105    case GL_RGB:
1106    case GL_R3_G3_B2:
1107    case GL_RGB4:
1108    case GL_RGB5:
1109    case GL_RGB10:
1110    case GL_RGB12:
1111    case GL_RGB16:
1112       return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
1113    case GL_SRGB8_EXT:
1114       return _mesa_is_desktop_gl(ctx) ? GL_RGB : 0;
1115    case GL_RGBA4:
1116    case GL_RGB5_A1:
1117    case GL_RGBA8:
1118       return GL_RGBA;
1119    case GL_RGBA:
1120    case GL_RGBA2:
1121    case GL_RGBA12:
1122    case GL_RGBA16:
1123       return _mesa_is_desktop_gl(ctx) ? GL_RGBA : 0;
1124    case GL_RGB10_A2:
1125    case GL_SRGB8_ALPHA8_EXT:
1126       return _mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
1127    case GL_STENCIL_INDEX:
1128    case GL_STENCIL_INDEX1_EXT:
1129    case GL_STENCIL_INDEX4_EXT:
1130    case GL_STENCIL_INDEX16_EXT:
1131       /* There are extensions for GL_STENCIL_INDEX1 and GL_STENCIL_INDEX4 in
1132        * OpenGL ES, but Mesa does not currently support them.
1133        */
1134       return _mesa_is_desktop_gl(ctx) ? GL_STENCIL_INDEX : 0;
1135    case GL_STENCIL_INDEX8_EXT:
1136       return GL_STENCIL_INDEX;
1137    case GL_DEPTH_COMPONENT:
1138    case GL_DEPTH_COMPONENT32:
1139       return _mesa_is_desktop_gl(ctx) ? GL_DEPTH_COMPONENT : 0;
1140    case GL_DEPTH_COMPONENT16:
1141    case GL_DEPTH_COMPONENT24:
1142       return GL_DEPTH_COMPONENT;
1143    case GL_DEPTH_STENCIL_EXT:
1144       return _mesa_is_desktop_gl(ctx)
1145          && ctx->Extensions.EXT_packed_depth_stencil
1146          ? GL_DEPTH_STENCIL_EXT : 0;
1147    case GL_DEPTH24_STENCIL8_EXT:
1148       return ctx->Extensions.EXT_packed_depth_stencil
1149          ? GL_DEPTH_STENCIL_EXT : 0;
1150    case GL_DEPTH_COMPONENT32F:
1151       return ctx->Version >= 30
1152          || (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_depth_buffer_float)
1153          ? GL_DEPTH_COMPONENT : 0;
1154    case GL_DEPTH32F_STENCIL8:
1155       return ctx->Version >= 30
1156          || (ctx->API == API_OPENGL_COMPAT && ctx->Extensions.ARB_depth_buffer_float)
1157          ? GL_DEPTH_STENCIL : 0;
1158    case GL_RED:
1159    case GL_R16:
1160       return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1161          ? GL_RED : 0;
1162    case GL_R8:
1163       return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1164          ? GL_RED : 0;
1165    case GL_RG:
1166    case GL_RG16:
1167       return _mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_rg
1168          ? GL_RG : 0;
1169    case GL_RG8:
1170       return ctx->API != API_OPENGLES && ctx->Extensions.ARB_texture_rg
1171          ? GL_RG : 0;
1172    /* signed normalized texture formats */
1173    case GL_RED_SNORM:
1174    case GL_R8_SNORM:
1175    case GL_R16_SNORM:
1176       return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1177          ? GL_RED : 0;
1178    case GL_RG_SNORM:
1179    case GL_RG8_SNORM:
1180    case GL_RG16_SNORM:
1181       return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1182          ? GL_RG : 0;
1183    case GL_RGB_SNORM:
1184    case GL_RGB8_SNORM:
1185    case GL_RGB16_SNORM:
1186       return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1187          ? GL_RGB : 0;
1188    case GL_RGBA_SNORM:
1189    case GL_RGBA8_SNORM:
1190    case GL_RGBA16_SNORM:
1191       return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_snorm
1192          ? GL_RGBA : 0;
1193    case GL_ALPHA_SNORM:
1194    case GL_ALPHA8_SNORM:
1195    case GL_ALPHA16_SNORM:
1196       return ctx->API == API_OPENGL_COMPAT &&
1197              ctx->Extensions.EXT_texture_snorm &&
1198              ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1199    case GL_R16F:
1200    case GL_R32F:
1201       return (_mesa_is_desktop_gl(ctx) &&
1202              ctx->Extensions.ARB_texture_rg &&
1203              ctx->Extensions.ARB_texture_float) ? GL_RED : 0;
1204    case GL_RG16F:
1205    case GL_RG32F:
1206       return (_mesa_is_desktop_gl(ctx) &&
1207              ctx->Extensions.ARB_texture_rg &&
1208              ctx->Extensions.ARB_texture_float) ? GL_RG : 0;
1209    case GL_RGB16F:
1210    case GL_RGB32F:
1211       return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_float)
1212          ? GL_RGB : 0;
1213    case GL_RGBA16F:
1214    case GL_RGBA32F:
1215       return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_texture_float)
1216          ? GL_RGBA : 0;
1217    case GL_ALPHA16F_ARB:
1218    case GL_ALPHA32F_ARB:
1219       return ctx->API == API_OPENGL_COMPAT &&
1220              ctx->Extensions.ARB_texture_float &&
1221              ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1222    case GL_LUMINANCE16F_ARB:
1223    case GL_LUMINANCE32F_ARB:
1224       return ctx->API == API_OPENGL_COMPAT &&
1225              ctx->Extensions.ARB_texture_float &&
1226              ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1227    case GL_LUMINANCE_ALPHA16F_ARB:
1228    case GL_LUMINANCE_ALPHA32F_ARB:
1229       return ctx->API == API_OPENGL_COMPAT &&
1230              ctx->Extensions.ARB_texture_float &&
1231              ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1232    case GL_INTENSITY16F_ARB:
1233    case GL_INTENSITY32F_ARB:
1234       return ctx->API == API_OPENGL_COMPAT &&
1235              ctx->Extensions.ARB_texture_float &&
1236              ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1237    case GL_RGB9_E5:
1238       return (_mesa_is_desktop_gl(ctx)
1239               && ctx->Extensions.EXT_texture_shared_exponent)
1240          ? GL_RGB : 0;
1241    case GL_R11F_G11F_B10F:
1242       return (_mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_packed_float)
1243          ? GL_RGB : 0;
1244
1245    case GL_RGBA8UI_EXT:
1246    case GL_RGBA16UI_EXT:
1247    case GL_RGBA32UI_EXT:
1248    case GL_RGBA8I_EXT:
1249    case GL_RGBA16I_EXT:
1250    case GL_RGBA32I_EXT:
1251       return ctx->Version >= 30
1252          || (_mesa_is_desktop_gl(ctx) &&
1253              ctx->Extensions.EXT_texture_integer) ? GL_RGBA : 0;
1254
1255    case GL_RGB8UI_EXT:
1256    case GL_RGB16UI_EXT:
1257    case GL_RGB32UI_EXT:
1258    case GL_RGB8I_EXT:
1259    case GL_RGB16I_EXT:
1260    case GL_RGB32I_EXT:
1261       return _mesa_is_desktop_gl(ctx) && ctx->Extensions.EXT_texture_integer
1262          ? GL_RGB : 0;
1263    case GL_R8UI:
1264    case GL_R8I:
1265    case GL_R16UI:
1266    case GL_R16I:
1267    case GL_R32UI:
1268    case GL_R32I:
1269       return ctx->Version >= 30
1270          || (_mesa_is_desktop_gl(ctx) &&
1271              ctx->Extensions.ARB_texture_rg &&
1272              ctx->Extensions.EXT_texture_integer) ? GL_RED : 0;
1273
1274    case GL_RG8UI:
1275    case GL_RG8I:
1276    case GL_RG16UI:
1277    case GL_RG16I:
1278    case GL_RG32UI:
1279    case GL_RG32I:
1280       return ctx->Version >= 30
1281          || (_mesa_is_desktop_gl(ctx) &&
1282              ctx->Extensions.ARB_texture_rg &&
1283              ctx->Extensions.EXT_texture_integer) ? GL_RG : 0;
1284
1285    case GL_INTENSITY8I_EXT:
1286    case GL_INTENSITY8UI_EXT:
1287    case GL_INTENSITY16I_EXT:
1288    case GL_INTENSITY16UI_EXT:
1289    case GL_INTENSITY32I_EXT:
1290    case GL_INTENSITY32UI_EXT:
1291       return ctx->API == API_OPENGL_COMPAT &&
1292              ctx->Extensions.EXT_texture_integer &&
1293              ctx->Extensions.ARB_framebuffer_object ? GL_INTENSITY : 0;
1294
1295    case GL_LUMINANCE8I_EXT:
1296    case GL_LUMINANCE8UI_EXT:
1297    case GL_LUMINANCE16I_EXT:
1298    case GL_LUMINANCE16UI_EXT:
1299    case GL_LUMINANCE32I_EXT:
1300    case GL_LUMINANCE32UI_EXT:
1301       return ctx->API == API_OPENGL_COMPAT &&
1302              ctx->Extensions.EXT_texture_integer &&
1303              ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE : 0;
1304
1305    case GL_LUMINANCE_ALPHA8I_EXT:
1306    case GL_LUMINANCE_ALPHA8UI_EXT:
1307    case GL_LUMINANCE_ALPHA16I_EXT:
1308    case GL_LUMINANCE_ALPHA16UI_EXT:
1309    case GL_LUMINANCE_ALPHA32I_EXT:
1310    case GL_LUMINANCE_ALPHA32UI_EXT:
1311       return ctx->API == API_OPENGL_COMPAT &&
1312              ctx->Extensions.EXT_texture_integer &&
1313              ctx->Extensions.ARB_framebuffer_object ? GL_LUMINANCE_ALPHA : 0;
1314
1315    case GL_ALPHA8I_EXT:
1316    case GL_ALPHA8UI_EXT:
1317    case GL_ALPHA16I_EXT:
1318    case GL_ALPHA16UI_EXT:
1319    case GL_ALPHA32I_EXT:
1320    case GL_ALPHA32UI_EXT:
1321       return ctx->API == API_OPENGL_COMPAT &&
1322              ctx->Extensions.EXT_texture_integer &&
1323              ctx->Extensions.ARB_framebuffer_object ? GL_ALPHA : 0;
1324
1325    case GL_RGB10_A2UI:
1326       return (_mesa_is_desktop_gl(ctx) &&
1327               ctx->Extensions.ARB_texture_rgb10_a2ui)
1328          || _mesa_is_gles3(ctx) ? GL_RGBA : 0;
1329
1330    case GL_RGB565:
1331       return _mesa_is_gles(ctx) || ctx->Extensions.ARB_ES2_compatibility
1332          ? GL_RGB : 0;
1333    default:
1334       return 0;
1335    }
1336 }
1337
1338
1339 /**
1340  * Invalidate a renderbuffer attachment.  Called from _mesa_HashWalk().
1341  */
1342 static void
1343 invalidate_rb(GLuint key, void *data, void *userData)
1344 {
1345    struct gl_framebuffer *fb = (struct gl_framebuffer *) data;
1346    struct gl_renderbuffer *rb = (struct gl_renderbuffer *) userData;
1347
1348    /* If this is a user-created FBO */
1349    if (_mesa_is_user_fbo(fb)) {
1350       GLuint i;
1351       for (i = 0; i < BUFFER_COUNT; i++) {
1352          struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1353          if (att->Type == GL_RENDERBUFFER &&
1354              att->Renderbuffer == rb) {
1355             /* Mark fb status as indeterminate to force re-validation */
1356             fb->_Status = 0;
1357             return;
1358          }
1359       }
1360    }
1361 }
1362
1363
1364 /** sentinal value, see below */
1365 #define NO_SAMPLES 1000
1366
1367
1368 /**
1369  * Helper function used by _mesa_RenderbufferStorage() and 
1370  * _mesa_RenderbufferStorageMultisample().
1371  * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorage().
1372  */
1373 static void
1374 renderbuffer_storage(GLenum target, GLenum internalFormat,
1375                      GLsizei width, GLsizei height, GLsizei samples)
1376 {
1377    const char *func = samples == NO_SAMPLES ?
1378       "glRenderbufferStorage" : "glRenderbufferStorageMultisample";
1379    struct gl_renderbuffer *rb;
1380    GLenum baseFormat;
1381    GET_CURRENT_CONTEXT(ctx);
1382
1383    ASSERT_OUTSIDE_BEGIN_END(ctx);
1384
1385    if (MESA_VERBOSE & VERBOSE_API) {
1386       if (samples == NO_SAMPLES)
1387          _mesa_debug(ctx, "%s(%s, %s, %d, %d)\n",
1388                      func,
1389                      _mesa_lookup_enum_by_nr(target),
1390                      _mesa_lookup_enum_by_nr(internalFormat),
1391                      width, height);
1392       else
1393          _mesa_debug(ctx, "%s(%s, %s, %d, %d, %d)\n",
1394                      func,
1395                      _mesa_lookup_enum_by_nr(target),
1396                      _mesa_lookup_enum_by_nr(internalFormat),
1397                      width, height, samples);
1398    }
1399
1400    if (target != GL_RENDERBUFFER_EXT) {
1401       _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
1402       return;
1403    }
1404
1405    baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
1406    if (baseFormat == 0) {
1407       _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat)", func);
1408       return;
1409    }
1410
1411    if (width < 0 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1412       _mesa_error(ctx, GL_INVALID_VALUE, "%s(width)", func);
1413       return;
1414    }
1415
1416    if (height < 0 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
1417       _mesa_error(ctx, GL_INVALID_VALUE, "%s(height)", func);
1418       return;
1419    }
1420
1421    if (samples == NO_SAMPLES) {
1422       /* NumSamples == 0 indicates non-multisampling */
1423       samples = 0;
1424    }
1425    else if (samples > (GLsizei) ctx->Const.MaxSamples) {
1426       /* note: driver may choose to use more samples than what's requested */
1427       _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples)", func);
1428       return;
1429    }
1430
1431    rb = ctx->CurrentRenderbuffer;
1432    if (!rb) {
1433       _mesa_error(ctx, GL_INVALID_OPERATION, "%s", func);
1434       return;
1435    }
1436
1437    FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1438
1439    if (rb->InternalFormat == internalFormat &&
1440        rb->Width == (GLuint) width &&
1441        rb->Height == (GLuint) height &&
1442        rb->NumSamples == samples) {
1443       /* no change in allocation needed */
1444       return;
1445    }
1446
1447    /* These MUST get set by the AllocStorage func */
1448    rb->Format = MESA_FORMAT_NONE;
1449    rb->NumSamples = samples;
1450
1451    /* Now allocate the storage */
1452    ASSERT(rb->AllocStorage);
1453    if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
1454       /* No error - check/set fields now */
1455       /* If rb->Format == MESA_FORMAT_NONE, the format is unsupported. */
1456       assert(rb->Width == (GLuint) width);
1457       assert(rb->Height == (GLuint) height);
1458       rb->InternalFormat = internalFormat;
1459       rb->_BaseFormat = baseFormat;
1460       assert(rb->_BaseFormat != 0);
1461    }
1462    else {
1463       /* Probably ran out of memory - clear the fields */
1464       rb->Width = 0;
1465       rb->Height = 0;
1466       rb->Format = MESA_FORMAT_NONE;
1467       rb->InternalFormat = GL_NONE;
1468       rb->_BaseFormat = GL_NONE;
1469       rb->NumSamples = 0;
1470    }
1471
1472    /* Invalidate the framebuffers the renderbuffer is attached in. */
1473    if (rb->AttachedAnytime) {
1474       _mesa_HashWalk(ctx->Shared->FrameBuffers, invalidate_rb, rb);
1475    }
1476 }
1477
1478
1479 void GLAPIENTRY
1480 _mesa_EGLImageTargetRenderbufferStorageOES(GLenum target, GLeglImageOES image)
1481 {
1482    struct gl_renderbuffer *rb;
1483    GET_CURRENT_CONTEXT(ctx);
1484    ASSERT_OUTSIDE_BEGIN_END(ctx);
1485
1486    if (!ctx->Extensions.OES_EGL_image) {
1487       _mesa_error(ctx, GL_INVALID_OPERATION,
1488                   "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1489       return;
1490    }
1491
1492    if (target != GL_RENDERBUFFER) {
1493       _mesa_error(ctx, GL_INVALID_ENUM,
1494                   "EGLImageTargetRenderbufferStorageOES");
1495       return;
1496    }
1497
1498    rb = ctx->CurrentRenderbuffer;
1499    if (!rb) {
1500       _mesa_error(ctx, GL_INVALID_OPERATION,
1501                   "EGLImageTargetRenderbufferStorageOES");
1502       return;
1503    }
1504
1505    FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1506
1507    ctx->Driver.EGLImageTargetRenderbufferStorage(ctx, rb, image);
1508 }
1509
1510
1511 /**
1512  * Helper function for _mesa_GetRenderbufferParameteriv() and
1513  * _mesa_GetFramebufferAttachmentParameteriv()
1514  * We have to be careful to respect the base format.  For example, if a
1515  * renderbuffer/texture was created with internalFormat=GL_RGB but the
1516  * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1517  * we need to return zero.
1518  */
1519 static GLint
1520 get_component_bits(GLenum pname, GLenum baseFormat, gl_format format)
1521 {
1522    if (_mesa_base_format_has_channel(baseFormat, pname))
1523       return _mesa_get_format_bits(format, pname);
1524    else
1525       return 0;
1526 }
1527
1528
1529
1530 void GLAPIENTRY
1531 _mesa_RenderbufferStorage(GLenum target, GLenum internalFormat,
1532                              GLsizei width, GLsizei height)
1533 {
1534    /* GL_ARB_fbo says calling this function is equivalent to calling
1535     * glRenderbufferStorageMultisample() with samples=0.  We pass in
1536     * a token value here just for error reporting purposes.
1537     */
1538    renderbuffer_storage(target, internalFormat, width, height, NO_SAMPLES);
1539 }
1540
1541
1542 void GLAPIENTRY
1543 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
1544                                      GLenum internalFormat,
1545                                      GLsizei width, GLsizei height)
1546 {
1547    renderbuffer_storage(target, internalFormat, width, height, samples);
1548 }
1549
1550
1551 /**
1552  * OpenGL ES version of glRenderBufferStorage.
1553  */
1554 void GLAPIENTRY
1555 _es_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1556                            GLsizei width, GLsizei height)
1557 {
1558    switch (internalFormat) {
1559    case GL_RGB565:
1560       /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1561       /* choose a closest format */
1562       internalFormat = GL_RGB5;
1563       break;
1564    default:
1565       break;
1566    }
1567
1568    renderbuffer_storage(target, internalFormat, width, height, 0);
1569 }
1570
1571
1572 void GLAPIENTRY
1573 _mesa_GetRenderbufferParameteriv(GLenum target, GLenum pname, GLint *params)
1574 {
1575    struct gl_renderbuffer *rb;
1576    GET_CURRENT_CONTEXT(ctx);
1577
1578    ASSERT_OUTSIDE_BEGIN_END(ctx);
1579
1580    if (target != GL_RENDERBUFFER_EXT) {
1581       _mesa_error(ctx, GL_INVALID_ENUM,
1582                   "glGetRenderbufferParameterivEXT(target)");
1583       return;
1584    }
1585
1586    rb = ctx->CurrentRenderbuffer;
1587    if (!rb) {
1588       _mesa_error(ctx, GL_INVALID_OPERATION,
1589                   "glGetRenderbufferParameterivEXT");
1590       return;
1591    }
1592
1593    /* No need to flush here since we're just quering state which is
1594     * not effected by rendering.
1595     */
1596
1597    switch (pname) {
1598    case GL_RENDERBUFFER_WIDTH_EXT:
1599       *params = rb->Width;
1600       return;
1601    case GL_RENDERBUFFER_HEIGHT_EXT:
1602       *params = rb->Height;
1603       return;
1604    case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
1605       *params = rb->InternalFormat;
1606       return;
1607    case GL_RENDERBUFFER_RED_SIZE_EXT:
1608    case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1609    case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1610    case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1611    case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1612    case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1613       *params = get_component_bits(pname, rb->_BaseFormat, rb->Format);
1614       break;
1615    case GL_RENDERBUFFER_SAMPLES:
1616       if ((_mesa_is_desktop_gl(ctx) && ctx->Extensions.ARB_framebuffer_object)
1617           || _mesa_is_gles3(ctx)) {
1618          *params = rb->NumSamples;
1619          break;
1620       }
1621       /* fallthrough */
1622    default:
1623       _mesa_error(ctx, GL_INVALID_ENUM,
1624                   "glGetRenderbufferParameterivEXT(target)");
1625       return;
1626    }
1627 }
1628
1629
1630 GLboolean GLAPIENTRY
1631 _mesa_IsFramebuffer(GLuint framebuffer)
1632 {
1633    GET_CURRENT_CONTEXT(ctx);
1634    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1635    if (framebuffer) {
1636       struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
1637       if (rb != NULL && rb != &DummyFramebuffer)
1638          return GL_TRUE;
1639    }
1640    return GL_FALSE;
1641 }
1642
1643
1644 /**
1645  * Check if any of the attachments of the given framebuffer are textures
1646  * (render to texture).  Call ctx->Driver.RenderTexture() for such
1647  * attachments.
1648  */
1649 static void
1650 check_begin_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
1651 {
1652    GLuint i;
1653    ASSERT(ctx->Driver.RenderTexture);
1654
1655    if (_mesa_is_winsys_fbo(fb))
1656       return; /* can't render to texture with winsys framebuffers */
1657
1658    for (i = 0; i < BUFFER_COUNT; i++) {
1659       struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1660       if (att->Texture && _mesa_get_attachment_teximage(att)) {
1661          ctx->Driver.RenderTexture(ctx, fb, att);
1662       }
1663    }
1664 }
1665
1666
1667 /**
1668  * Examine all the framebuffer's attachments to see if any are textures.
1669  * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1670  * notify the device driver that the texture image may have changed.
1671  */
1672 static void
1673 check_end_texture_render(struct gl_context *ctx, struct gl_framebuffer *fb)
1674 {
1675    if (_mesa_is_winsys_fbo(fb))
1676       return; /* can't render to texture with winsys framebuffers */
1677
1678    if (ctx->Driver.FinishRenderTexture) {
1679       GLuint i;
1680       for (i = 0; i < BUFFER_COUNT; i++) {
1681          struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1682          if (att->Texture && att->Renderbuffer) {
1683             ctx->Driver.FinishRenderTexture(ctx, att);
1684          }
1685       }
1686    }
1687 }
1688
1689
1690 void GLAPIENTRY
1691 _mesa_BindFramebuffer(GLenum target, GLuint framebuffer)
1692 {
1693    struct gl_framebuffer *newDrawFb, *newReadFb;
1694    struct gl_framebuffer *oldDrawFb, *oldReadFb;
1695    GLboolean bindReadBuf, bindDrawBuf;
1696    GET_CURRENT_CONTEXT(ctx);
1697
1698 #ifdef DEBUG
1699    if (ctx->Extensions.ARB_framebuffer_object) {
1700       ASSERT(ctx->Extensions.EXT_framebuffer_object);
1701       ASSERT(ctx->Extensions.EXT_framebuffer_blit);
1702    }
1703 #endif
1704
1705    ASSERT_OUTSIDE_BEGIN_END(ctx);
1706
1707    if (!ctx->Extensions.EXT_framebuffer_object) {
1708       _mesa_error(ctx, GL_INVALID_OPERATION,
1709                   "glBindFramebufferEXT(unsupported)");
1710       return;
1711    }
1712
1713    switch (target) {
1714    case GL_DRAW_FRAMEBUFFER_EXT:
1715       if (!ctx->Extensions.EXT_framebuffer_blit) {
1716          _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1717          return;
1718       }
1719       bindDrawBuf = GL_TRUE;
1720       bindReadBuf = GL_FALSE;
1721       break;
1722    case GL_READ_FRAMEBUFFER_EXT:
1723       if (!ctx->Extensions.EXT_framebuffer_blit) {
1724          _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1725          return;
1726       }
1727       bindDrawBuf = GL_FALSE;
1728       bindReadBuf = GL_TRUE;
1729       break;
1730    case GL_FRAMEBUFFER_EXT:
1731       bindDrawBuf = GL_TRUE;
1732       bindReadBuf = GL_TRUE;
1733       break;
1734    default:
1735       _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1736       return;
1737    }
1738
1739    if (framebuffer) {
1740       /* Binding a user-created framebuffer object */
1741       newDrawFb = _mesa_lookup_framebuffer(ctx, framebuffer);
1742       if (newDrawFb == &DummyFramebuffer) {
1743          /* ID was reserved, but no real framebuffer object made yet */
1744          newDrawFb = NULL;
1745       }
1746       else if (!newDrawFb
1747                && _mesa_is_desktop_gl(ctx)
1748                && ctx->Extensions.ARB_framebuffer_object) {
1749          /* All FBO IDs must be Gen'd */
1750          _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
1751          return;
1752       }
1753
1754       if (!newDrawFb) {
1755          /* create new framebuffer object */
1756          newDrawFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
1757          if (!newDrawFb) {
1758             _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
1759             return;
1760          }
1761          _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newDrawFb);
1762       }
1763       newReadFb = newDrawFb;
1764    }
1765    else {
1766       /* Binding the window system framebuffer (which was originally set
1767        * with MakeCurrent).
1768        */
1769       newDrawFb = ctx->WinSysDrawBuffer;
1770       newReadFb = ctx->WinSysReadBuffer;
1771    }
1772
1773    ASSERT(newDrawFb);
1774    ASSERT(newDrawFb != &DummyFramebuffer);
1775
1776    /* save pointers to current/old framebuffers */
1777    oldDrawFb = ctx->DrawBuffer;
1778    oldReadFb = ctx->ReadBuffer;
1779
1780    /* check if really changing bindings */
1781    if (oldDrawFb == newDrawFb)
1782       bindDrawBuf = GL_FALSE;
1783    if (oldReadFb == newReadFb)
1784       bindReadBuf = GL_FALSE;
1785
1786    /*
1787     * OK, now bind the new Draw/Read framebuffers, if they're changing.
1788     *
1789     * We also check if we're beginning and/or ending render-to-texture.
1790     * When a framebuffer with texture attachments is unbound, call
1791     * ctx->Driver.FinishRenderTexture().
1792     * When a framebuffer with texture attachments is bound, call
1793     * ctx->Driver.RenderTexture().
1794     *
1795     * Note that if the ReadBuffer has texture attachments we don't consider
1796     * that a render-to-texture case.
1797     */
1798    if (bindReadBuf) {
1799       FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1800
1801       /* check if old readbuffer was render-to-texture */
1802       check_end_texture_render(ctx, oldReadFb);
1803
1804       _mesa_reference_framebuffer(&ctx->ReadBuffer, newReadFb);
1805    }
1806
1807    if (bindDrawBuf) {
1808       FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1809
1810       /* check if old framebuffer had any texture attachments */
1811       if (oldDrawFb)
1812          check_end_texture_render(ctx, oldDrawFb);
1813
1814       /* check if newly bound framebuffer has any texture attachments */
1815       check_begin_texture_render(ctx, newDrawFb);
1816
1817       _mesa_reference_framebuffer(&ctx->DrawBuffer, newDrawFb);
1818    }
1819
1820    if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
1821       ctx->Driver.BindFramebuffer(ctx, target, newDrawFb, newReadFb);
1822    }
1823 }
1824
1825
1826 void GLAPIENTRY
1827 _mesa_DeleteFramebuffers(GLsizei n, const GLuint *framebuffers)
1828 {
1829    GLint i;
1830    GET_CURRENT_CONTEXT(ctx);
1831
1832    ASSERT_OUTSIDE_BEGIN_END(ctx);
1833    FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1834
1835    for (i = 0; i < n; i++) {
1836       if (framebuffers[i] > 0) {
1837          struct gl_framebuffer *fb;
1838          fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
1839          if (fb) {
1840             ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
1841
1842             /* check if deleting currently bound framebuffer object */
1843             if (ctx->Extensions.EXT_framebuffer_blit) {
1844                /* separate draw/read binding points */
1845                if (fb == ctx->DrawBuffer) {
1846                   /* bind default */
1847                   ASSERT(fb->RefCount >= 2);
1848                   _mesa_BindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, 0);
1849                }
1850                if (fb == ctx->ReadBuffer) {
1851                   /* bind default */
1852                   ASSERT(fb->RefCount >= 2);
1853                   _mesa_BindFramebuffer(GL_READ_FRAMEBUFFER_EXT, 0);
1854                }
1855             }
1856             else {
1857                /* only one binding point for read/draw buffers */
1858                if (fb == ctx->DrawBuffer || fb == ctx->ReadBuffer) {
1859                   /* bind default */
1860                   ASSERT(fb->RefCount >= 2);
1861                   _mesa_BindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
1862                }
1863             }
1864
1865             /* remove from hash table immediately, to free the ID */
1866             _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
1867
1868             if (fb != &DummyFramebuffer) {
1869                /* But the object will not be freed until it's no longer
1870                 * bound in any context.
1871                 */
1872                _mesa_reference_framebuffer(&fb, NULL);
1873             }
1874          }
1875       }
1876    }
1877 }
1878
1879
1880 void GLAPIENTRY
1881 _mesa_GenFramebuffers(GLsizei n, GLuint *framebuffers)
1882 {
1883    GET_CURRENT_CONTEXT(ctx);
1884    GLuint first;
1885    GLint i;
1886
1887    ASSERT_OUTSIDE_BEGIN_END(ctx);
1888
1889    if (n < 0) {
1890       _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
1891       return;
1892    }
1893
1894    if (!framebuffers)
1895       return;
1896
1897    first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
1898
1899    for (i = 0; i < n; i++) {
1900       GLuint name = first + i;
1901       framebuffers[i] = name;
1902       /* insert dummy placeholder into hash table */
1903       _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1904       _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
1905       _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1906    }
1907 }
1908
1909
1910
1911 GLenum GLAPIENTRY
1912 _mesa_CheckFramebufferStatus(GLenum target)
1913 {
1914    struct gl_framebuffer *buffer;
1915    GET_CURRENT_CONTEXT(ctx);
1916
1917    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1918
1919    if (MESA_VERBOSE & VERBOSE_API)
1920       _mesa_debug(ctx, "glCheckFramebufferStatus(%s)\n",
1921                   _mesa_lookup_enum_by_nr(target));
1922
1923    buffer = get_framebuffer_target(ctx, target);
1924    if (!buffer) {
1925       _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1926       return 0;
1927    }
1928
1929    if (_mesa_is_winsys_fbo(buffer)) {
1930       /* The window system / default framebuffer is always complete */
1931       return GL_FRAMEBUFFER_COMPLETE_EXT;
1932    }
1933
1934    /* No need to flush here */
1935
1936    if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
1937       _mesa_test_framebuffer_completeness(ctx, buffer);
1938    }
1939
1940    return buffer->_Status;
1941 }
1942
1943
1944 /**
1945  * Replicate the src attachment point. Used by framebuffer_texture() when
1946  * the same texture is attached at GL_DEPTH_ATTACHMENT and
1947  * GL_STENCIL_ATTACHMENT.
1948  */
1949 static void
1950 reuse_framebuffer_texture_attachment(struct gl_framebuffer *fb,
1951                                      gl_buffer_index dst,
1952                                      gl_buffer_index src)
1953 {
1954    struct gl_renderbuffer_attachment *dst_att = &fb->Attachment[dst];
1955    struct gl_renderbuffer_attachment *src_att = &fb->Attachment[src];
1956
1957    assert(src_att->Texture != NULL);
1958    assert(src_att->Renderbuffer != NULL);
1959
1960    _mesa_reference_texobj(&dst_att->Texture, src_att->Texture);
1961    _mesa_reference_renderbuffer(&dst_att->Renderbuffer, src_att->Renderbuffer);
1962    dst_att->Type = src_att->Type;
1963    dst_att->Complete = src_att->Complete;
1964    dst_att->TextureLevel = src_att->TextureLevel;
1965    dst_att->Zoffset = src_att->Zoffset;
1966 }
1967
1968
1969 /**
1970  * Common code called by glFramebufferTexture1D/2D/3DEXT() and
1971  * glFramebufferTextureLayerEXT().
1972  * Note: glFramebufferTextureLayerEXT() has no textarget parameter so we'll
1973  * get textarget=0 in that case.
1974  */
1975 static void
1976 framebuffer_texture(struct gl_context *ctx, const char *caller, GLenum target, 
1977                     GLenum attachment, GLenum textarget, GLuint texture,
1978                     GLint level, GLint zoffset)
1979 {
1980    struct gl_renderbuffer_attachment *att;
1981    struct gl_texture_object *texObj = NULL;
1982    struct gl_framebuffer *fb;
1983    GLenum maxLevelsTarget;
1984
1985    ASSERT_OUTSIDE_BEGIN_END(ctx);
1986
1987    fb = get_framebuffer_target(ctx, target);
1988    if (!fb) {
1989       _mesa_error(ctx, GL_INVALID_ENUM,
1990                   "glFramebufferTexture%sEXT(target=0x%x)", caller, target);
1991       return;
1992    }
1993
1994    /* check framebuffer binding */
1995    if (_mesa_is_winsys_fbo(fb)) {
1996       _mesa_error(ctx, GL_INVALID_OPERATION,
1997                   "glFramebufferTexture%sEXT", caller);
1998       return;
1999    }
2000
2001    /* The textarget, level, and zoffset parameters are only validated if
2002     * texture is non-zero.
2003     */
2004    if (texture) {
2005       GLboolean err = GL_TRUE;
2006
2007       texObj = _mesa_lookup_texture(ctx, texture);
2008       if (texObj != NULL) {
2009          if (textarget == 0) {
2010             /* If textarget == 0 it means we're being called by
2011              * glFramebufferTextureLayer() and textarget is not used.
2012              * The only legal texture types for that function are 3D and
2013              * 1D/2D arrays textures.
2014              */
2015             err = (texObj->Target != GL_TEXTURE_3D) &&
2016                 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
2017                 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT) &&
2018                 (texObj->Target != GL_TEXTURE_CUBE_MAP_ARRAY);
2019          }
2020          else {
2021             /* Make sure textarget is consistent with the texture's type */
2022             err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
2023                 ? !_mesa_is_cube_face(textarget)
2024                 : (texObj->Target != textarget);
2025          }
2026       }
2027       else {
2028          /* can't render to a non-existant texture */
2029          _mesa_error(ctx, GL_INVALID_OPERATION,
2030                      "glFramebufferTexture%sEXT(non existant texture)",
2031                      caller);
2032          return;
2033       }
2034
2035       if (err) {
2036          _mesa_error(ctx, GL_INVALID_OPERATION,
2037                      "glFramebufferTexture%sEXT(texture target mismatch)",
2038                      caller);
2039          return;
2040       }
2041
2042       if (texObj->Target == GL_TEXTURE_3D) {
2043          const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
2044          if (zoffset < 0 || zoffset >= maxSize) {
2045             _mesa_error(ctx, GL_INVALID_VALUE,
2046                         "glFramebufferTexture%sEXT(zoffset)", caller);
2047             return;
2048          }
2049       }
2050       else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
2051                (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT) ||
2052                (texObj->Target == GL_TEXTURE_CUBE_MAP_ARRAY)) {
2053          if (zoffset < 0 ||
2054              zoffset >= (GLint) ctx->Const.MaxArrayTextureLayers) {
2055             _mesa_error(ctx, GL_INVALID_VALUE,
2056                         "glFramebufferTexture%sEXT(layer)", caller);
2057             return;
2058          }
2059       }
2060
2061       maxLevelsTarget = textarget ? textarget : texObj->Target;
2062       if ((level < 0) ||
2063           (level >= _mesa_max_texture_levels(ctx, maxLevelsTarget))) {
2064          _mesa_error(ctx, GL_INVALID_VALUE,
2065                      "glFramebufferTexture%sEXT(level)", caller);
2066          return;
2067       }
2068    }
2069
2070    att = _mesa_get_attachment(ctx, fb, attachment);
2071    if (att == NULL) {
2072       _mesa_error(ctx, GL_INVALID_ENUM,
2073                   "glFramebufferTexture%sEXT(attachment)", caller);
2074       return;
2075    }
2076
2077    FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2078
2079    _glthread_LOCK_MUTEX(fb->Mutex);
2080    if (texObj) {
2081       if (attachment == GL_DEPTH_ATTACHMENT &&
2082           texObj == fb->Attachment[BUFFER_STENCIL].Texture &&
2083           level == fb->Attachment[BUFFER_STENCIL].TextureLevel &&
2084           _mesa_tex_target_to_face(textarget) ==
2085           fb->Attachment[BUFFER_STENCIL].CubeMapFace &&
2086           zoffset == fb->Attachment[BUFFER_STENCIL].Zoffset) {
2087          /* The texture object is already attached to the stencil attachment
2088           * point. Don't create a new renderbuffer; just reuse the stencil
2089           * attachment's. This is required to prevent a GL error in
2090           * glGetFramebufferAttachmentParameteriv(GL_DEPTH_STENCIL).
2091           */
2092          reuse_framebuffer_texture_attachment(fb, BUFFER_DEPTH,
2093                                               BUFFER_STENCIL);
2094       } else if (attachment == GL_STENCIL_ATTACHMENT &&
2095                  texObj == fb->Attachment[BUFFER_DEPTH].Texture &&
2096                  level == fb->Attachment[BUFFER_DEPTH].TextureLevel &&
2097                  _mesa_tex_target_to_face(textarget) ==
2098                  fb->Attachment[BUFFER_DEPTH].CubeMapFace &&
2099                  zoffset == fb->Attachment[BUFFER_DEPTH].Zoffset) {
2100          /* As above, but with depth and stencil transposed. */
2101          reuse_framebuffer_texture_attachment(fb, BUFFER_STENCIL,
2102                                               BUFFER_DEPTH);
2103       } else {
2104          _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
2105                                       level, zoffset);
2106          if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2107             /* Above we created a new renderbuffer and attached it to the
2108              * depth attachment point. Now attach it to the stencil attachment
2109              * point too.
2110              */
2111             assert(att == &fb->Attachment[BUFFER_DEPTH]);
2112             reuse_framebuffer_texture_attachment(fb,BUFFER_STENCIL,
2113                                                  BUFFER_DEPTH);
2114          }
2115       }
2116
2117       /* Set the render-to-texture flag.  We'll check this flag in
2118        * glTexImage() and friends to determine if we need to revalidate
2119        * any FBOs that might be rendering into this texture.
2120        * This flag never gets cleared since it's non-trivial to determine
2121        * when all FBOs might be done rendering to this texture.  That's OK
2122        * though since it's uncommon to render to a texture then repeatedly
2123        * call glTexImage() to change images in the texture.
2124        */
2125       texObj->_RenderToTexture = GL_TRUE;
2126    }
2127    else {
2128       _mesa_remove_attachment(ctx, att);
2129       if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2130          assert(att == &fb->Attachment[BUFFER_DEPTH]);
2131          _mesa_remove_attachment(ctx, &fb->Attachment[BUFFER_STENCIL]);
2132       }
2133    }
2134
2135    invalidate_framebuffer(fb);
2136
2137    _glthread_UNLOCK_MUTEX(fb->Mutex);
2138 }
2139
2140
2141
2142 void GLAPIENTRY
2143 _mesa_FramebufferTexture1D(GLenum target, GLenum attachment,
2144                               GLenum textarget, GLuint texture, GLint level)
2145 {
2146    GET_CURRENT_CONTEXT(ctx);
2147
2148    if (texture != 0) {
2149       GLboolean error;
2150
2151       switch (textarget) {
2152       case GL_TEXTURE_1D:
2153          error = GL_FALSE;
2154          break;
2155       case GL_TEXTURE_1D_ARRAY:
2156          error = !ctx->Extensions.EXT_texture_array;
2157          break;
2158       default:
2159          error = GL_TRUE;
2160       }
2161
2162       if (error) {
2163          _mesa_error(ctx, GL_INVALID_OPERATION,
2164                      "glFramebufferTexture1DEXT(textarget=%s)",
2165                      _mesa_lookup_enum_by_nr(textarget));
2166          return;
2167       }
2168    }
2169
2170    framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
2171                        level, 0);
2172 }
2173
2174
2175 void GLAPIENTRY
2176 _mesa_FramebufferTexture2D(GLenum target, GLenum attachment,
2177                               GLenum textarget, GLuint texture, GLint level)
2178 {
2179    GET_CURRENT_CONTEXT(ctx);
2180
2181    if (texture != 0) {
2182       GLboolean error;
2183
2184       switch (textarget) {
2185       case GL_TEXTURE_2D:
2186          error = GL_FALSE;
2187          break;
2188       case GL_TEXTURE_RECTANGLE:
2189          error = _mesa_is_gles(ctx)
2190             || !ctx->Extensions.NV_texture_rectangle;
2191          break;
2192       case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
2193       case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
2194       case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
2195       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
2196       case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
2197       case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
2198          error = !ctx->Extensions.ARB_texture_cube_map;
2199          break;
2200       case GL_TEXTURE_2D_ARRAY:
2201          error = (_mesa_is_gles(ctx) && ctx->Version < 30)
2202             || !ctx->Extensions.EXT_texture_array;
2203          break;
2204       default:
2205          error = GL_TRUE;
2206       }
2207
2208       if (error) {
2209          _mesa_error(ctx, GL_INVALID_OPERATION,
2210                      "glFramebufferTexture2DEXT(textarget=%s)",
2211                      _mesa_lookup_enum_by_nr(textarget));
2212          return;
2213       }
2214    }
2215
2216    framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
2217                        level, 0);
2218 }
2219
2220
2221 void GLAPIENTRY
2222 _mesa_FramebufferTexture3D(GLenum target, GLenum attachment,
2223                               GLenum textarget, GLuint texture,
2224                               GLint level, GLint zoffset)
2225 {
2226    GET_CURRENT_CONTEXT(ctx);
2227
2228    if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
2229       _mesa_error(ctx, GL_INVALID_OPERATION,
2230                   "glFramebufferTexture3DEXT(textarget)");
2231       return;
2232    }
2233
2234    framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
2235                        level, zoffset);
2236 }
2237
2238
2239 void GLAPIENTRY
2240 _mesa_FramebufferTextureLayer(GLenum target, GLenum attachment,
2241                                  GLuint texture, GLint level, GLint layer)
2242 {
2243    GET_CURRENT_CONTEXT(ctx);
2244
2245    framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
2246                        level, layer);
2247 }
2248
2249
2250 void GLAPIENTRY
2251 _mesa_FramebufferRenderbuffer(GLenum target, GLenum attachment,
2252                                  GLenum renderbufferTarget,
2253                                  GLuint renderbuffer)
2254 {
2255    struct gl_renderbuffer_attachment *att;
2256    struct gl_framebuffer *fb;
2257    struct gl_renderbuffer *rb;
2258    GET_CURRENT_CONTEXT(ctx);
2259
2260    ASSERT_OUTSIDE_BEGIN_END(ctx);
2261
2262    fb = get_framebuffer_target(ctx, target);
2263    if (!fb) {
2264       _mesa_error(ctx, GL_INVALID_ENUM, "glFramebufferRenderbufferEXT(target)");
2265       return;
2266    }
2267
2268    if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
2269       _mesa_error(ctx, GL_INVALID_ENUM,
2270                   "glFramebufferRenderbufferEXT(renderbufferTarget)");
2271       return;
2272    }
2273
2274    if (_mesa_is_winsys_fbo(fb)) {
2275       /* Can't attach new renderbuffers to a window system framebuffer */
2276       _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
2277       return;
2278    }
2279
2280    att = _mesa_get_attachment(ctx, fb, attachment);
2281    if (att == NULL) {
2282       _mesa_error(ctx, GL_INVALID_ENUM,
2283                   "glFramebufferRenderbufferEXT(invalid attachment %s)",
2284                   _mesa_lookup_enum_by_nr(attachment));
2285       return;
2286    }
2287
2288    if (renderbuffer) {
2289       rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
2290       if (!rb) {
2291          _mesa_error(ctx, GL_INVALID_OPERATION,
2292                      "glFramebufferRenderbufferEXT(non-existant"
2293                      " renderbuffer %u)", renderbuffer);
2294          return;
2295       }
2296       else if (rb == &DummyRenderbuffer) {
2297          /* This is what NVIDIA does */
2298          _mesa_error(ctx, GL_INVALID_VALUE,
2299                      "glFramebufferRenderbufferEXT(renderbuffer %u)",
2300                      renderbuffer);
2301          return;
2302       }
2303    }
2304    else {
2305       /* remove renderbuffer attachment */
2306       rb = NULL;
2307    }
2308
2309    if (attachment == GL_DEPTH_STENCIL_ATTACHMENT &&
2310        rb && rb->Format != MESA_FORMAT_NONE) {
2311       /* make sure the renderbuffer is a depth/stencil format */
2312       const GLenum baseFormat = _mesa_get_format_base_format(rb->Format);
2313       if (baseFormat != GL_DEPTH_STENCIL) {
2314          _mesa_error(ctx, GL_INVALID_OPERATION,
2315                      "glFramebufferRenderbufferEXT(renderbuffer"
2316                      " is not DEPTH_STENCIL format)");
2317          return;
2318       }
2319    }
2320
2321
2322    FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2323
2324    assert(ctx->Driver.FramebufferRenderbuffer);
2325    ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
2326
2327    /* Some subsequent GL commands may depend on the framebuffer's visual
2328     * after the binding is updated.  Update visual info now.
2329     */
2330    _mesa_update_framebuffer_visual(ctx, fb);
2331 }
2332
2333
2334 void GLAPIENTRY
2335 _mesa_GetFramebufferAttachmentParameteriv(GLenum target, GLenum attachment,
2336                                              GLenum pname, GLint *params)
2337 {
2338    const struct gl_renderbuffer_attachment *att;
2339    struct gl_framebuffer *buffer;
2340    GLenum err;
2341    GET_CURRENT_CONTEXT(ctx);
2342
2343    ASSERT_OUTSIDE_BEGIN_END(ctx);
2344
2345    /* The error differs in GL and GLES. */
2346    err = _mesa_is_desktop_gl(ctx) ? GL_INVALID_OPERATION : GL_INVALID_ENUM;
2347
2348    buffer = get_framebuffer_target(ctx, target);
2349    if (!buffer) {
2350       _mesa_error(ctx, GL_INVALID_ENUM,
2351                   "glGetFramebufferAttachmentParameterivEXT(target)");
2352       return;
2353    }
2354
2355    if (_mesa_is_winsys_fbo(buffer)) {
2356       /* Page 126 (page 136 of the PDF) of the OpenGL ES 2.0.25 spec
2357        * says:
2358        *
2359        *     "If the framebuffer currently bound to target is zero, then
2360        *     INVALID_OPERATION is generated."
2361        *
2362        * The EXT_framebuffer_object spec has the same wording, and the
2363        * OES_framebuffer_object spec refers to the EXT_framebuffer_object
2364        * spec.
2365        */
2366       if (!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_framebuffer_object) {
2367          _mesa_error(ctx, GL_INVALID_OPERATION,
2368                      "glGetFramebufferAttachmentParameteriv(bound FBO = 0)");
2369          return;
2370       }
2371       /* the default / window-system FBO */
2372       att = _mesa_get_fb0_attachment(ctx, buffer, attachment);
2373    }
2374    else {
2375       /* user-created framebuffer FBO */
2376       att = _mesa_get_attachment(ctx, buffer, attachment);
2377    }
2378
2379    if (att == NULL) {
2380       _mesa_error(ctx, GL_INVALID_ENUM,
2381                   "glGetFramebufferAttachmentParameterivEXT(attachment)");
2382       return;
2383    }
2384
2385    if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
2386       /* the depth and stencil attachments must point to the same buffer */
2387       const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
2388       depthAtt = _mesa_get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
2389       stencilAtt = _mesa_get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
2390       if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
2391          _mesa_error(ctx, GL_INVALID_OPERATION,
2392                      "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
2393                      " attachments differ)");
2394          return;
2395       }
2396    }
2397
2398    /* No need to flush here */
2399
2400    switch (pname) {
2401    case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
2402       *params = _mesa_is_winsys_fbo(buffer)
2403          ? GL_FRAMEBUFFER_DEFAULT : att->Type;
2404       return;
2405    case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
2406       if (att->Type == GL_RENDERBUFFER_EXT) {
2407          *params = att->Renderbuffer->Name;
2408       }
2409       else if (att->Type == GL_TEXTURE) {
2410          *params = att->Texture->Name;
2411       }
2412       else {
2413          assert(att->Type == GL_NONE);
2414          if (_mesa_is_desktop_gl(ctx) || _mesa_is_gles3(ctx)) {
2415             *params = 0;
2416          } else {
2417             goto invalid_pname_enum;
2418          }
2419       }
2420       return;
2421    case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
2422       if (att->Type == GL_TEXTURE) {
2423          *params = att->TextureLevel;
2424       }
2425       else if (att->Type == GL_NONE) {
2426          _mesa_error(ctx, err,
2427                      "glGetFramebufferAttachmentParameterivEXT(pname)");
2428       }
2429       else {
2430          goto invalid_pname_enum;
2431       }
2432       return;
2433    case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
2434       if (att->Type == GL_TEXTURE) {
2435          if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
2436             *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
2437          }
2438          else {
2439             *params = 0;
2440          }
2441       }
2442       else if (att->Type == GL_NONE) {
2443          _mesa_error(ctx, err,
2444                      "glGetFramebufferAttachmentParameterivEXT(pname)");
2445       }
2446       else {
2447          goto invalid_pname_enum;
2448       }
2449       return;
2450    case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
2451       if (ctx->API == API_OPENGLES) {
2452          goto invalid_pname_enum;
2453       } else if (att->Type == GL_NONE) {
2454          _mesa_error(ctx, err,
2455                      "glGetFramebufferAttachmentParameterivEXT(pname)");
2456       } else if (att->Type == GL_TEXTURE) {
2457          if (att->Texture && att->Texture->Target == GL_TEXTURE_3D) {
2458             *params = att->Zoffset;
2459          }
2460          else {
2461             *params = 0;
2462          }
2463       }
2464       else {
2465          goto invalid_pname_enum;
2466       }
2467       return;
2468    case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
2469       if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_framebuffer_object)
2470           && !_mesa_is_gles3(ctx)) {
2471          goto invalid_pname_enum;
2472       }
2473       else if (att->Type == GL_NONE) {
2474          _mesa_error(ctx, err,
2475                      "glGetFramebufferAttachmentParameterivEXT(pname)");
2476       }
2477       else {
2478          if (ctx->Extensions.EXT_framebuffer_sRGB) {
2479             *params = _mesa_get_format_color_encoding(att->Renderbuffer->Format);
2480          }
2481          else {
2482             /* According to ARB_framebuffer_sRGB, we should return LINEAR
2483              * if the sRGB conversion is unsupported. */
2484             *params = GL_LINEAR;
2485          }
2486       }
2487       return;
2488    case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
2489       if ((ctx->API != API_OPENGL_COMPAT || !ctx->Extensions.ARB_framebuffer_object)
2490           && ctx->API != API_OPENGL_CORE
2491           && !_mesa_is_gles3(ctx)) {
2492          goto invalid_pname_enum;
2493       }
2494       else if (att->Type == GL_NONE) {
2495          _mesa_error(ctx, err,
2496                      "glGetFramebufferAttachmentParameterivEXT(pname)");
2497       }
2498       else {
2499          gl_format format = att->Renderbuffer->Format;
2500          if (format == MESA_FORMAT_S8) {
2501             /* special cases */
2502             *params = GL_INDEX;
2503          }
2504          else if (format == MESA_FORMAT_Z32_FLOAT_X24S8) {
2505             /* depends on the attachment parameter */
2506             if (attachment == GL_STENCIL_ATTACHMENT) {
2507                *params = GL_INDEX;
2508             }
2509             else {
2510                *params = GL_FLOAT;
2511             }
2512          }
2513          else {
2514             *params = _mesa_get_format_datatype(format);
2515          }
2516       }
2517       return;
2518    case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
2519    case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
2520    case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
2521    case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
2522    case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
2523    case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
2524       if ((!_mesa_is_desktop_gl(ctx) || !ctx->Extensions.ARB_framebuffer_object)
2525           && !_mesa_is_gles3(ctx)) {
2526          goto invalid_pname_enum;
2527       }
2528       else if (att->Type == GL_NONE) {
2529          _mesa_error(ctx, err,
2530                      "glGetFramebufferAttachmentParameterivEXT(pname)");
2531       }
2532       else if (att->Texture) {
2533          const struct gl_texture_image *texImage =
2534             _mesa_select_tex_image(ctx, att->Texture, att->Texture->Target,
2535                                    att->TextureLevel);
2536          if (texImage) {
2537             *params = get_component_bits(pname, texImage->_BaseFormat,
2538                                          texImage->TexFormat);
2539          }
2540          else {
2541             *params = 0;
2542          }
2543       }
2544       else if (att->Renderbuffer) {
2545          *params = get_component_bits(pname, att->Renderbuffer->_BaseFormat,
2546                                       att->Renderbuffer->Format);
2547       }
2548       else {
2549          _mesa_problem(ctx, "glGetFramebufferAttachmentParameterivEXT:"
2550                        " invalid FBO attachment structure");
2551       }
2552       return;
2553    default:
2554       goto invalid_pname_enum;
2555    }
2556
2557    return;
2558
2559 invalid_pname_enum:
2560    _mesa_error(ctx, GL_INVALID_ENUM,
2561                "glGetFramebufferAttachmentParameteriv(pname)");
2562    return;
2563 }
2564
2565
2566 void GLAPIENTRY
2567 _mesa_GenerateMipmap(GLenum target)
2568 {
2569    struct gl_texture_image *srcImage;
2570    struct gl_texture_object *texObj;
2571    GLboolean error;
2572
2573    GET_CURRENT_CONTEXT(ctx);
2574
2575    ASSERT_OUTSIDE_BEGIN_END(ctx);
2576    FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2577
2578    switch (target) {
2579    case GL_TEXTURE_1D:
2580       error = _mesa_is_gles(ctx);
2581       break;
2582    case GL_TEXTURE_2D:
2583       error = GL_FALSE;
2584       break;
2585    case GL_TEXTURE_3D:
2586       error = ctx->API == API_OPENGLES;
2587       break;
2588    case GL_TEXTURE_CUBE_MAP:
2589       error = !ctx->Extensions.ARB_texture_cube_map;
2590       break;
2591    case GL_TEXTURE_1D_ARRAY:
2592       error = _mesa_is_gles(ctx) || !ctx->Extensions.EXT_texture_array;
2593       break;
2594    case GL_TEXTURE_2D_ARRAY:
2595       error = (_mesa_is_gles(ctx) && ctx->Version < 30)
2596          || !ctx->Extensions.EXT_texture_array;
2597       break;
2598    default:
2599       error = GL_TRUE;
2600    }
2601
2602    if (error) {
2603       _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target=%s)",
2604                   _mesa_lookup_enum_by_nr(target));
2605       return;
2606    }
2607
2608    texObj = _mesa_get_current_tex_object(ctx, target);
2609
2610    if (texObj->BaseLevel >= texObj->MaxLevel) {
2611       /* nothing to do */
2612       return;
2613    }
2614
2615    if (texObj->Target == GL_TEXTURE_CUBE_MAP &&
2616        !_mesa_cube_complete(texObj)) {
2617       _mesa_error(ctx, GL_INVALID_OPERATION,
2618                   "glGenerateMipmap(incomplete cube map)");
2619       return;
2620    }
2621
2622    _mesa_lock_texture(ctx, texObj);
2623
2624    srcImage = _mesa_select_tex_image(ctx, texObj, target, texObj->BaseLevel);
2625    if (!srcImage) {
2626       _mesa_unlock_texture(ctx, texObj);
2627       _mesa_error(ctx, GL_INVALID_OPERATION,
2628                   "glGenerateMipmap(zero size base image)");
2629       return;
2630    }
2631
2632    if (_mesa_is_enum_format_integer(srcImage->InternalFormat) ||
2633        _mesa_is_depthstencil_format(srcImage->InternalFormat) ||
2634        _mesa_is_stencil_format(srcImage->InternalFormat)) {
2635       _mesa_unlock_texture(ctx, texObj);
2636       _mesa_error(ctx, GL_INVALID_OPERATION,
2637                   "glGenerateMipmap(invalid internal format)");
2638       return;
2639    }
2640
2641    if (target == GL_TEXTURE_CUBE_MAP) {
2642       GLuint face;
2643       for (face = 0; face < 6; face++)
2644          ctx->Driver.GenerateMipmap(ctx,
2645                                     GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + face,
2646                                     texObj);
2647    }
2648    else {
2649       ctx->Driver.GenerateMipmap(ctx, target, texObj);
2650    }
2651    _mesa_unlock_texture(ctx, texObj);
2652 }
2653
2654
2655 static const struct gl_renderbuffer_attachment *
2656 find_attachment(const struct gl_framebuffer *fb,
2657                 const struct gl_renderbuffer *rb)
2658 {
2659    GLuint i;
2660    for (i = 0; i < Elements(fb->Attachment); i++) {
2661       if (fb->Attachment[i].Renderbuffer == rb)
2662          return &fb->Attachment[i];
2663    }
2664    return NULL;
2665 }
2666
2667
2668 /**
2669  * Helper function for checking if the datatypes of color buffers are
2670  * compatible for glBlitFramebuffer.  From the 3.1 spec, page 198:
2671  *
2672  * "GL_INVALID_OPERATION is generated if mask contains GL_COLOR_BUFFER_BIT
2673  *  and any of the following conditions hold:
2674  *   - The read buffer contains fixed-point or floating-point values and any
2675  *     draw buffer contains neither fixed-point nor floating-point values.
2676  *   - The read buffer contains unsigned integer values and any draw buffer
2677  *     does not contain unsigned integer values.
2678  *   - The read buffer contains signed integer values and any draw buffer
2679  *     does not contain signed integer values."
2680  */
2681 static GLboolean
2682 compatible_color_datatypes(gl_format srcFormat, gl_format dstFormat)
2683 {
2684    GLenum srcType = _mesa_get_format_datatype(srcFormat);
2685    GLenum dstType = _mesa_get_format_datatype(dstFormat);
2686
2687    if (srcType != GL_INT && srcType != GL_UNSIGNED_INT) {
2688       assert(srcType == GL_UNSIGNED_NORMALIZED ||
2689              srcType == GL_SIGNED_NORMALIZED ||
2690              srcType == GL_FLOAT);
2691       /* Boil any of those types down to GL_FLOAT */
2692       srcType = GL_FLOAT;
2693    }
2694
2695    if (dstType != GL_INT && dstType != GL_UNSIGNED_INT) {
2696       assert(dstType == GL_UNSIGNED_NORMALIZED ||
2697              dstType == GL_SIGNED_NORMALIZED ||
2698              dstType == GL_FLOAT);
2699       /* Boil any of those types down to GL_FLOAT */
2700       dstType = GL_FLOAT;
2701    }
2702
2703    return srcType == dstType;
2704 }
2705
2706
2707 static GLboolean
2708 compatible_resolve_formats(const struct gl_renderbuffer *readRb,
2709                            const struct gl_renderbuffer *drawRb)
2710 {
2711    GLenum readFormat, drawFormat;
2712
2713    /* The simple case where we know the backing Mesa formats are the same.
2714     */
2715    if (_mesa_get_srgb_format_linear(readRb->Format) ==
2716        _mesa_get_srgb_format_linear(drawRb->Format)) {
2717       return GL_TRUE;
2718    }
2719
2720    /* The Mesa formats are different, so we must check whether the internal
2721     * formats are compatible.
2722     *
2723     * Under some circumstances, the user may request e.g. two GL_RGBA8
2724     * textures and get two entirely different Mesa formats like RGBA8888 and
2725     * ARGB8888. Drivers behaving like that should be able to cope with
2726     * non-matching formats by themselves, because it's not the user's fault.
2727     *
2728     * Blits between linear and sRGB formats are also allowed.
2729     */
2730    readFormat = _mesa_get_nongeneric_internalformat(readRb->InternalFormat);
2731    drawFormat = _mesa_get_nongeneric_internalformat(drawRb->InternalFormat);
2732    readFormat = _mesa_get_linear_internalformat(readFormat);
2733    drawFormat = _mesa_get_linear_internalformat(drawFormat);
2734
2735    if (readFormat == drawFormat) {
2736       return GL_TRUE;
2737    }
2738
2739    return GL_FALSE;
2740 }
2741
2742
2743 /**
2744  * Blit rectangular region, optionally from one framebuffer to another.
2745  *
2746  * Note, if the src buffer is multisampled and the dest is not, this is
2747  * when the samples must be resolved to a single color.
2748  */
2749 void GLAPIENTRY
2750 _mesa_BlitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
2751                          GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
2752                          GLbitfield mask, GLenum filter)
2753 {
2754    const GLbitfield legalMaskBits = (GL_COLOR_BUFFER_BIT |
2755                                      GL_DEPTH_BUFFER_BIT |
2756                                      GL_STENCIL_BUFFER_BIT);
2757    const struct gl_framebuffer *readFb, *drawFb;
2758    const struct gl_renderbuffer *colorReadRb, *colorDrawRb;
2759    GET_CURRENT_CONTEXT(ctx);
2760
2761    ASSERT_OUTSIDE_BEGIN_END(ctx);
2762    FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2763
2764    if (MESA_VERBOSE & VERBOSE_API)
2765       _mesa_debug(ctx,
2766                   "glBlitFramebuffer(%d, %d, %d, %d,  %d, %d, %d, %d, 0x%x, %s)\n",
2767                   srcX0, srcY0, srcX1, srcY1,
2768                   dstX0, dstY0, dstX1, dstY1,
2769                   mask, _mesa_lookup_enum_by_nr(filter));
2770
2771    if (ctx->NewState) {
2772       _mesa_update_state(ctx);
2773    }
2774
2775    readFb = ctx->ReadBuffer;
2776    drawFb = ctx->DrawBuffer;
2777
2778    if (!readFb || !drawFb) {
2779       /* This will normally never happen but someday we may want to
2780        * support MakeCurrent() with no drawables.
2781        */
2782       return;
2783    }
2784
2785    /* check for complete framebuffers */
2786    if (drawFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
2787        readFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2788       _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2789                   "glBlitFramebufferEXT(incomplete draw/read buffers)");
2790       return;
2791    }
2792
2793    if (filter != GL_NEAREST && filter != GL_LINEAR) {
2794       _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(filter)");
2795       return;
2796    }
2797
2798    if (mask & ~legalMaskBits) {
2799       _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
2800       return;
2801    }
2802
2803    /* depth/stencil must be blitted with nearest filtering */
2804    if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
2805         && filter != GL_NEAREST) {
2806       _mesa_error(ctx, GL_INVALID_OPERATION,
2807              "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter)");
2808       return;
2809    }
2810
2811    /* get color read/draw renderbuffers */
2812    if (mask & GL_COLOR_BUFFER_BIT) {
2813       colorReadRb = readFb->_ColorReadBuffer;
2814       colorDrawRb = drawFb->_ColorDrawBuffers[0];
2815
2816       /* From the EXT_framebuffer_object spec:
2817        *
2818        *     "If a buffer is specified in <mask> and does not exist in both
2819        *     the read and draw framebuffers, the corresponding bit is silently
2820        *     ignored."
2821        */
2822       if ((colorReadRb == NULL) || (colorDrawRb == NULL)) {
2823          colorReadRb = colorDrawRb = NULL;
2824          mask &= ~GL_COLOR_BUFFER_BIT;
2825       }
2826       else if (!compatible_color_datatypes(colorReadRb->Format,
2827                                            colorDrawRb->Format)) {
2828          _mesa_error(ctx, GL_INVALID_OPERATION,
2829                      "glBlitFramebufferEXT(color buffer datatypes mismatch)");
2830          return;
2831       }
2832    }
2833    else {
2834       colorReadRb = colorDrawRb = NULL;
2835    }
2836
2837    if (mask & GL_STENCIL_BUFFER_BIT) {
2838       struct gl_renderbuffer *readRb =
2839          readFb->Attachment[BUFFER_STENCIL].Renderbuffer;
2840       struct gl_renderbuffer *drawRb =
2841          drawFb->Attachment[BUFFER_STENCIL].Renderbuffer;
2842
2843       /* From the EXT_framebuffer_object spec:
2844        *
2845        *     "If a buffer is specified in <mask> and does not exist in both
2846        *     the read and draw framebuffers, the corresponding bit is silently
2847        *     ignored."
2848        */
2849       if ((readRb == NULL) || (drawRb == NULL)) {
2850          mask &= ~GL_STENCIL_BUFFER_BIT;
2851       }
2852       else if (_mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS) !=
2853                _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS)) {
2854          /* There is no need to check the stencil datatype here, because
2855           * there is only one: GL_UNSIGNED_INT.
2856           */
2857          _mesa_error(ctx, GL_INVALID_OPERATION,
2858                      "glBlitFramebufferEXT(stencil buffer size mismatch)");
2859          return;
2860       }
2861    }
2862
2863    if (mask & GL_DEPTH_BUFFER_BIT) {
2864       struct gl_renderbuffer *readRb =
2865          readFb->Attachment[BUFFER_DEPTH].Renderbuffer;
2866       struct gl_renderbuffer *drawRb =
2867          drawFb->Attachment[BUFFER_DEPTH].Renderbuffer;
2868
2869       /* From the EXT_framebuffer_object spec:
2870        *
2871        *     "If a buffer is specified in <mask> and does not exist in both
2872        *     the read and draw framebuffers, the corresponding bit is silently
2873        *     ignored."
2874        */
2875       if ((readRb == NULL) || (drawRb == NULL)) {
2876          mask &= ~GL_DEPTH_BUFFER_BIT;
2877       }
2878       else if ((_mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS) !=
2879                 _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS)) ||
2880                (_mesa_get_format_datatype(readRb->Format) !=
2881                 _mesa_get_format_datatype(drawRb->Format))) {
2882          _mesa_error(ctx, GL_INVALID_OPERATION,
2883                      "glBlitFramebufferEXT(depth buffer format mismatch)");
2884          return;
2885       }
2886    }
2887
2888    if (readFb->Visual.samples > 0 &&
2889        drawFb->Visual.samples > 0 &&
2890        readFb->Visual.samples != drawFb->Visual.samples) {
2891       _mesa_error(ctx, GL_INVALID_OPERATION,
2892                   "glBlitFramebufferEXT(mismatched samples)");
2893       return;
2894    }
2895
2896    /* extra checks for multisample copies... */
2897    if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
2898       /* src and dest region sizes must be the same */
2899       if (abs(srcX1 - srcX0) != abs(dstX1 - dstX0) ||
2900           abs(srcY1 - srcY0) != abs(dstY1 - dstY0)) {
2901          _mesa_error(ctx, GL_INVALID_OPERATION,
2902                 "glBlitFramebufferEXT(bad src/dst multisample region sizes)");
2903          return;
2904       }
2905
2906       /* color formats must match */
2907       if (colorReadRb &&
2908           colorDrawRb &&
2909           !compatible_resolve_formats(colorReadRb, colorDrawRb)) {
2910          _mesa_error(ctx, GL_INVALID_OPERATION,
2911                 "glBlitFramebufferEXT(bad src/dst multisample pixel formats)");
2912          return;
2913       }
2914    }
2915
2916    if (filter == GL_LINEAR && (mask & GL_COLOR_BUFFER_BIT)) {
2917       /* 3.1 spec, page 199:
2918        * "Calling BlitFramebuffer will result in an INVALID_OPERATION error
2919        * if filter is LINEAR and read buffer contains integer data."
2920        */
2921       GLenum type = _mesa_get_format_datatype(colorReadRb->Format);
2922       if (type == GL_INT || type == GL_UNSIGNED_INT) {
2923          _mesa_error(ctx, GL_INVALID_OPERATION,
2924                      "glBlitFramebufferEXT(integer color type)");
2925          return;
2926       }
2927    }
2928
2929    if (!ctx->Extensions.EXT_framebuffer_blit) {
2930       _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
2931       return;
2932    }
2933
2934    /* Debug code */
2935    if (DEBUG_BLIT) {
2936       printf("glBlitFramebuffer(%d, %d, %d, %d,  %d, %d, %d, %d,"
2937              " 0x%x, 0x%x)\n",
2938              srcX0, srcY0, srcX1, srcY1,
2939              dstX0, dstY0, dstX1, dstY1,
2940              mask, filter);
2941       if (colorReadRb) {
2942          const struct gl_renderbuffer_attachment *att;
2943
2944          att = find_attachment(readFb, colorReadRb);
2945          printf("  Src FBO %u  RB %u (%dx%d)  ",
2946                 readFb->Name, colorReadRb->Name,
2947                 colorReadRb->Width, colorReadRb->Height);
2948          if (att && att->Texture) {
2949             printf("Tex %u  tgt 0x%x  level %u  face %u",
2950                    att->Texture->Name,
2951                    att->Texture->Target,
2952                    att->TextureLevel,
2953                    att->CubeMapFace);
2954          }
2955          printf("\n");
2956
2957          att = find_attachment(drawFb, colorDrawRb);
2958          printf("  Dst FBO %u  RB %u (%dx%d)  ",
2959                 drawFb->Name, colorDrawRb->Name,
2960                 colorDrawRb->Width, colorDrawRb->Height);
2961          if (att && att->Texture) {
2962             printf("Tex %u  tgt 0x%x  level %u  face %u",
2963                    att->Texture->Name,
2964                    att->Texture->Target,
2965                    att->TextureLevel,
2966                    att->CubeMapFace);
2967          }
2968          printf("\n");
2969       }
2970    }
2971
2972    if (!mask) {
2973       return;
2974    }
2975
2976    ASSERT(ctx->Driver.BlitFramebuffer);
2977    ctx->Driver.BlitFramebuffer(ctx,
2978                                srcX0, srcY0, srcX1, srcY1,
2979                                dstX0, dstY0, dstX1, dstY1,
2980                                mask, filter);
2981 }
2982
2983
2984 static void
2985 invalidate_framebuffer_storage(GLenum target, GLsizei numAttachments,
2986                                const GLenum *attachments, GLint x, GLint y,
2987                                GLsizei width, GLsizei height, const char *name)
2988 {
2989    int i;
2990    struct gl_framebuffer *fb;
2991    GET_CURRENT_CONTEXT(ctx);
2992
2993    ASSERT_OUTSIDE_BEGIN_END(ctx);
2994
2995    fb = get_framebuffer_target(ctx, target);
2996    if (!fb) {
2997       _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", name);
2998       return;
2999    }
3000
3001    if (numAttachments < 0) {
3002       _mesa_error(ctx, GL_INVALID_VALUE,
3003                   "%s(numAttachments < 0)", name);
3004       return;
3005    }
3006
3007    /* The GL_ARB_invalidate_subdata spec says:
3008     *
3009     *     "If an attachment is specified that does not exist in the
3010     *     framebuffer bound to <target>, it is ignored."
3011     *
3012     * It also says:
3013     *
3014     *     "If <attachments> contains COLOR_ATTACHMENTm and m is greater than
3015     *     or equal to the value of MAX_COLOR_ATTACHMENTS, then the error
3016     *     INVALID_OPERATION is generated."
3017     *
3018     * No mention is made of GL_AUXi being out of range.  Therefore, we allow
3019     * any enum that can be allowed by the API (OpenGL ES 3.0 has a different
3020     * set of retrictions).
3021     */
3022    for (i = 0; i < numAttachments; i++) {
3023       if (_mesa_is_winsys_fbo(fb)) {
3024          switch (attachments[i]) {
3025          case GL_ACCUM:
3026          case GL_AUX0:
3027          case GL_AUX1:
3028          case GL_AUX2:
3029          case GL_AUX3:
3030             /* Accumulation buffers and auxilary buffers were removed in
3031              * OpenGL 3.1, and they never existed in OpenGL ES.
3032              */
3033             if (ctx->API != API_OPENGL_COMPAT)
3034                goto invalid_enum;
3035             break;
3036          case GL_COLOR:
3037          case GL_DEPTH:
3038          case GL_STENCIL:
3039             break;
3040          case GL_BACK_LEFT:
3041          case GL_BACK_RIGHT:
3042          case GL_FRONT_LEFT:
3043          case GL_FRONT_RIGHT:
3044             if (!_mesa_is_desktop_gl(ctx))
3045                goto invalid_enum;
3046             break;
3047          default:
3048             goto invalid_enum;
3049          }
3050       } else {
3051          switch (attachments[i]) {
3052          case GL_DEPTH_ATTACHMENT:
3053          case GL_STENCIL_ATTACHMENT:
3054             break;
3055          case GL_COLOR_ATTACHMENT0:
3056          case GL_COLOR_ATTACHMENT1:
3057          case GL_COLOR_ATTACHMENT2:
3058          case GL_COLOR_ATTACHMENT3:
3059          case GL_COLOR_ATTACHMENT4:
3060          case GL_COLOR_ATTACHMENT5:
3061          case GL_COLOR_ATTACHMENT6:
3062          case GL_COLOR_ATTACHMENT7:
3063          case GL_COLOR_ATTACHMENT8:
3064          case GL_COLOR_ATTACHMENT9:
3065          case GL_COLOR_ATTACHMENT10:
3066          case GL_COLOR_ATTACHMENT11:
3067          case GL_COLOR_ATTACHMENT12:
3068          case GL_COLOR_ATTACHMENT13:
3069          case GL_COLOR_ATTACHMENT14:
3070          case GL_COLOR_ATTACHMENT15: {
3071             unsigned k = attachments[i] - GL_COLOR_ATTACHMENT0;
3072             if (k >= ctx->Const.MaxColorAttachments) {
3073                _mesa_error(ctx, GL_INVALID_OPERATION,
3074                            "%s(attachment >= max. color attachments)", name);
3075                return;
3076             }
3077          }
3078          default:
3079             goto invalid_enum;
3080          }
3081       }
3082    }
3083
3084    /* We don't actually do anything for this yet.  Just return after
3085     * validating the parameters and generating the required errors.
3086     */
3087    return;
3088
3089 invalid_enum:
3090    _mesa_error(ctx, GL_INVALID_ENUM, "%s(attachment)", name);
3091    return;
3092 }
3093
3094 void GLAPIENTRY
3095 _mesa_InvalidateSubFramebuffer(GLenum target, GLsizei numAttachments,
3096                                const GLenum *attachments, GLint x, GLint y,
3097                                GLsizei width, GLsizei height)
3098 {
3099    invalidate_framebuffer_storage(target, numAttachments, attachments,
3100                                   x, y, width, height,
3101                                   "glInvalidateSubFramebuffer");
3102 }
3103
3104 void GLAPIENTRY
3105 _mesa_InvalidateFramebuffer(GLenum target, GLsizei numAttachments,
3106                             const GLenum *attachments)
3107 {
3108    /* The GL_ARB_invalidate_subdata spec says:
3109     *
3110     *     "The command
3111     *
3112     *        void InvalidateFramebuffer(enum target,
3113     *                                   sizei numAttachments,
3114     *                                   const enum *attachments);
3115     *
3116     *     is equivalent to the command InvalidateSubFramebuffer with <x>, <y>,
3117     *     <width>, <height> equal to 0, 0, <MAX_VIEWPORT_DIMS[0]>,
3118     *     <MAX_VIEWPORT_DIMS[1]> respectively."
3119     */
3120    invalidate_framebuffer_storage(target, numAttachments, attachments,
3121                                   0, 0, MAX_VIEWPORT_WIDTH, MAX_VIEWPORT_HEIGHT,
3122                                   "glInvalidateFramebuffer");
3123 }