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