fbobject.c revision 8829e063aa87ade63c49d3df27a7edd0c63cf160
1/*
2 * Mesa 3-D graphics library
3 * Version:  7.1
4 *
5 * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
6 * Copyright (C) 1999-2009  VMware, Inc.  All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26
27/*
28 * GL_EXT/ARB_framebuffer_object extensions
29 *
30 * Authors:
31 *   Brian Paul
32 */
33
34
35#include "buffers.h"
36#include "context.h"
37#include "enums.h"
38#include "fbobject.h"
39#include "formats.h"
40#include "framebuffer.h"
41#include "hash.h"
42#include "macros.h"
43#include "renderbuffer.h"
44#include "state.h"
45#include "teximage.h"
46#include "texobj.h"
47
48
49/** Set this to 1 to help debug FBO incompleteness problems */
50#define DEBUG_FBO 0
51
52/** Set this to 1 to debug/log glBlitFramebuffer() calls */
53#define DEBUG_BLIT 0
54
55
56/**
57 * Notes:
58 *
59 * None of the GL_EXT_framebuffer_object functions are compiled into
60 * display lists.
61 */
62
63
64
65/*
66 * When glGenRender/FramebuffersEXT() is called we insert pointers to
67 * these placeholder objects into the hash table.
68 * Later, when the object ID is first bound, we replace the placeholder
69 * with the real frame/renderbuffer.
70 */
71static struct gl_framebuffer DummyFramebuffer;
72static struct gl_renderbuffer DummyRenderbuffer;
73
74
75#define IS_CUBE_FACE(TARGET) \
76   ((TARGET) >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && \
77    (TARGET) <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
78
79
80static void
81delete_dummy_renderbuffer(struct gl_renderbuffer *rb)
82{
83   /* no op */
84}
85
86static void
87delete_dummy_framebuffer(struct gl_framebuffer *fb)
88{
89   /* no op */
90}
91
92
93void
94_mesa_init_fbobjects(GLcontext *ctx)
95{
96   DummyFramebuffer.Delete = delete_dummy_framebuffer;
97   DummyRenderbuffer.Delete = delete_dummy_renderbuffer;
98}
99
100
101/**
102 * Helper routine for getting a gl_renderbuffer.
103 */
104struct gl_renderbuffer *
105_mesa_lookup_renderbuffer(GLcontext *ctx, GLuint id)
106{
107   struct gl_renderbuffer *rb;
108
109   if (id == 0)
110      return NULL;
111
112   rb = (struct gl_renderbuffer *)
113      _mesa_HashLookup(ctx->Shared->RenderBuffers, id);
114   return rb;
115}
116
117
118/**
119 * Helper routine for getting a gl_framebuffer.
120 */
121struct gl_framebuffer *
122_mesa_lookup_framebuffer(GLcontext *ctx, GLuint id)
123{
124   struct gl_framebuffer *fb;
125
126   if (id == 0)
127      return NULL;
128
129   fb = (struct gl_framebuffer *)
130      _mesa_HashLookup(ctx->Shared->FrameBuffers, id);
131   return fb;
132}
133
134
135/**
136 * Mark the given framebuffer as invalid.  This will force the
137 * test for framebuffer completeness to be done before the framebuffer
138 * is used.
139 */
140static void
141invalidate_framebuffer(struct gl_framebuffer *fb)
142{
143   fb->_Status = 0; /* "indeterminate" */
144}
145
146
147/**
148 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
149 * gl_renderbuffer_attachment object.
150 * If \p attachment is GL_DEPTH_STENCIL_ATTACHMENT, return a pointer to
151 * the depth buffer attachment point.
152 */
153struct gl_renderbuffer_attachment *
154_mesa_get_attachment(GLcontext *ctx, struct gl_framebuffer *fb,
155                     GLenum attachment)
156{
157   GLuint i;
158
159   switch (attachment) {
160   case GL_COLOR_ATTACHMENT0_EXT:
161   case GL_COLOR_ATTACHMENT1_EXT:
162   case GL_COLOR_ATTACHMENT2_EXT:
163   case GL_COLOR_ATTACHMENT3_EXT:
164   case GL_COLOR_ATTACHMENT4_EXT:
165   case GL_COLOR_ATTACHMENT5_EXT:
166   case GL_COLOR_ATTACHMENT6_EXT:
167   case GL_COLOR_ATTACHMENT7_EXT:
168   case GL_COLOR_ATTACHMENT8_EXT:
169   case GL_COLOR_ATTACHMENT9_EXT:
170   case GL_COLOR_ATTACHMENT10_EXT:
171   case GL_COLOR_ATTACHMENT11_EXT:
172   case GL_COLOR_ATTACHMENT12_EXT:
173   case GL_COLOR_ATTACHMENT13_EXT:
174   case GL_COLOR_ATTACHMENT14_EXT:
175   case GL_COLOR_ATTACHMENT15_EXT:
176      i = attachment - GL_COLOR_ATTACHMENT0_EXT;
177      if (i >= ctx->Const.MaxColorAttachments) {
178	 return NULL;
179      }
180      return &fb->Attachment[BUFFER_COLOR0 + i];
181   case GL_DEPTH_STENCIL_ATTACHMENT:
182      /* fall-through */
183   case GL_DEPTH_BUFFER:
184      /* fall-through / new in GL 3.0 */
185   case GL_DEPTH_ATTACHMENT_EXT:
186      return &fb->Attachment[BUFFER_DEPTH];
187   case GL_STENCIL_BUFFER:
188      /* fall-through / new in GL 3.0 */
189   case GL_STENCIL_ATTACHMENT_EXT:
190      return &fb->Attachment[BUFFER_STENCIL];
191   default:
192      return NULL;
193   }
194}
195
196
197/**
198 * Remove any texture or renderbuffer attached to the given attachment
199 * point.  Update reference counts, etc.
200 */
201void
202_mesa_remove_attachment(GLcontext *ctx, struct gl_renderbuffer_attachment *att)
203{
204   if (att->Type == GL_TEXTURE) {
205      ASSERT(att->Texture);
206      if (ctx->Driver.FinishRenderTexture) {
207         /* tell driver that we're done rendering to this texture. */
208         ctx->Driver.FinishRenderTexture(ctx, att);
209      }
210      _mesa_reference_texobj(&att->Texture, NULL); /* unbind */
211      ASSERT(!att->Texture);
212   }
213   if (att->Type == GL_TEXTURE || att->Type == GL_RENDERBUFFER_EXT) {
214      ASSERT(!att->Texture);
215      _mesa_reference_renderbuffer(&att->Renderbuffer, NULL); /* unbind */
216      ASSERT(!att->Renderbuffer);
217   }
218   att->Type = GL_NONE;
219   att->Complete = GL_TRUE;
220}
221
222
223/**
224 * Bind a texture object to an attachment point.
225 * The previous binding, if any, will be removed first.
226 */
227void
228_mesa_set_texture_attachment(GLcontext *ctx,
229                             struct gl_framebuffer *fb,
230                             struct gl_renderbuffer_attachment *att,
231                             struct gl_texture_object *texObj,
232                             GLenum texTarget, GLuint level, GLuint zoffset)
233{
234   if (att->Texture == texObj) {
235      /* re-attaching same texture */
236      ASSERT(att->Type == GL_TEXTURE);
237      if (ctx->Driver.FinishRenderTexture)
238	 ctx->Driver.FinishRenderTexture(ctx, att);
239   }
240   else {
241      /* new attachment */
242      if (ctx->Driver.FinishRenderTexture && att->Texture)
243	 ctx->Driver.FinishRenderTexture(ctx, att);
244      _mesa_remove_attachment(ctx, att);
245      att->Type = GL_TEXTURE;
246      assert(!att->Texture);
247      _mesa_reference_texobj(&att->Texture, texObj);
248   }
249
250   /* always update these fields */
251   att->TextureLevel = level;
252   att->CubeMapFace = _mesa_tex_target_to_face(texTarget);
253   att->Zoffset = zoffset;
254   att->Complete = GL_FALSE;
255
256   if (att->Texture->Image[att->CubeMapFace][att->TextureLevel]) {
257      ctx->Driver.RenderTexture(ctx, fb, att);
258   }
259
260   invalidate_framebuffer(fb);
261}
262
263
264/**
265 * Bind a renderbuffer to an attachment point.
266 * The previous binding, if any, will be removed first.
267 */
268void
269_mesa_set_renderbuffer_attachment(GLcontext *ctx,
270                                  struct gl_renderbuffer_attachment *att,
271                                  struct gl_renderbuffer *rb)
272{
273   /* XXX check if re-doing same attachment, exit early */
274   _mesa_remove_attachment(ctx, att);
275   att->Type = GL_RENDERBUFFER_EXT;
276   att->Texture = NULL; /* just to be safe */
277   att->Complete = GL_FALSE;
278   _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
279}
280
281
282/**
283 * Fallback for ctx->Driver.FramebufferRenderbuffer()
284 * Attach a renderbuffer object to a framebuffer object.
285 */
286void
287_mesa_framebuffer_renderbuffer(GLcontext *ctx, struct gl_framebuffer *fb,
288                               GLenum attachment, struct gl_renderbuffer *rb)
289{
290   struct gl_renderbuffer_attachment *att;
291
292   _glthread_LOCK_MUTEX(fb->Mutex);
293
294   att = _mesa_get_attachment(ctx, fb, attachment);
295   ASSERT(att);
296   if (rb) {
297      _mesa_set_renderbuffer_attachment(ctx, att, rb);
298      if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
299         /* do stencil attachment here (depth already done above) */
300         att = _mesa_get_attachment(ctx, fb, GL_STENCIL_ATTACHMENT_EXT);
301         assert(att);
302         _mesa_set_renderbuffer_attachment(ctx, att, rb);
303      }
304   }
305   else {
306      _mesa_remove_attachment(ctx, att);
307   }
308
309   invalidate_framebuffer(fb);
310
311   _glthread_UNLOCK_MUTEX(fb->Mutex);
312}
313
314
315/**
316 * For debug only.
317 */
318static void
319att_incomplete(const char *msg)
320{
321#if DEBUG_FBO
322   _mesa_debug(NULL, "attachment incomplete: %s\n", msg);
323#else
324   (void) msg;
325#endif
326}
327
328
329/**
330 * For debug only.
331 */
332static void
333fbo_incomplete(const char *msg, int index)
334{
335#if DEBUG_FBO
336   _mesa_debug(NULL, "FBO Incomplete: %s [%d]\n", msg, index);
337#else
338   (void) msg;
339   (void) index;
340#endif
341}
342
343
344
345
346/**
347 * Test if an attachment point is complete and update its Complete field.
348 * \param format if GL_COLOR, this is a color attachment point,
349 *               if GL_DEPTH, this is a depth component attachment point,
350 *               if GL_STENCIL, this is a stencil component attachment point.
351 */
352static void
353test_attachment_completeness(const GLcontext *ctx, GLenum format,
354                             struct gl_renderbuffer_attachment *att)
355{
356   assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
357
358   /* assume complete */
359   att->Complete = GL_TRUE;
360
361   /* Look for reasons why the attachment might be incomplete */
362   if (att->Type == GL_TEXTURE) {
363      const struct gl_texture_object *texObj = att->Texture;
364      struct gl_texture_image *texImage;
365      GLenum baseFormat;
366
367      if (!texObj) {
368         att_incomplete("no texobj");
369         att->Complete = GL_FALSE;
370         return;
371      }
372
373      texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
374      if (!texImage) {
375         att_incomplete("no teximage");
376         att->Complete = GL_FALSE;
377         return;
378      }
379      if (texImage->Width < 1 || texImage->Height < 1) {
380         att_incomplete("teximage width/height=0");
381         printf("texobj = %u\n", texObj->Name);
382         printf("level = %d\n", att->TextureLevel);
383         att->Complete = GL_FALSE;
384         return;
385      }
386      if (texObj->Target == GL_TEXTURE_3D && att->Zoffset >= texImage->Depth) {
387         att_incomplete("bad z offset");
388         att->Complete = GL_FALSE;
389         return;
390      }
391
392      baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
393
394      if (format == GL_COLOR) {
395         if (baseFormat != GL_RGB &&
396             baseFormat != GL_RGBA) {
397            att_incomplete("bad format");
398            att->Complete = GL_FALSE;
399            return;
400         }
401         if (_mesa_is_format_compressed(texImage->TexFormat)) {
402            att_incomplete("compressed internalformat");
403            att->Complete = GL_FALSE;
404            return;
405         }
406      }
407      else if (format == GL_DEPTH) {
408         if (baseFormat == GL_DEPTH_COMPONENT) {
409            /* OK */
410         }
411         else if (ctx->Extensions.EXT_packed_depth_stencil &&
412                  ctx->Extensions.ARB_depth_texture &&
413                  baseFormat == GL_DEPTH_STENCIL_EXT) {
414            /* OK */
415         }
416         else {
417            att->Complete = GL_FALSE;
418            att_incomplete("bad depth format");
419            return;
420         }
421      }
422      else {
423         ASSERT(format == GL_STENCIL);
424         if (ctx->Extensions.EXT_packed_depth_stencil &&
425             ctx->Extensions.ARB_depth_texture &&
426             baseFormat == GL_DEPTH_STENCIL_EXT) {
427            /* OK */
428         }
429         else {
430            /* no such thing as stencil-only textures */
431            att_incomplete("illegal stencil texture");
432            att->Complete = GL_FALSE;
433            return;
434         }
435      }
436   }
437   else if (att->Type == GL_RENDERBUFFER_EXT) {
438      const GLenum baseFormat =
439         _mesa_get_format_base_format(att->Renderbuffer->Format);
440
441      ASSERT(att->Renderbuffer);
442      if (!att->Renderbuffer->InternalFormat ||
443          att->Renderbuffer->Width < 1 ||
444          att->Renderbuffer->Height < 1) {
445         att_incomplete("0x0 renderbuffer");
446         att->Complete = GL_FALSE;
447         return;
448      }
449      if (format == GL_COLOR) {
450         if (baseFormat != GL_RGB &&
451             baseFormat != GL_RGBA) {
452            att_incomplete("bad renderbuffer color format");
453            att->Complete = GL_FALSE;
454            return;
455         }
456      }
457      else if (format == GL_DEPTH) {
458         if (baseFormat == GL_DEPTH_COMPONENT) {
459            /* OK */
460         }
461         else if (ctx->Extensions.EXT_packed_depth_stencil &&
462                  baseFormat == GL_DEPTH_STENCIL_EXT) {
463            /* OK */
464         }
465         else {
466            att_incomplete("bad renderbuffer depth format");
467            att->Complete = GL_FALSE;
468            return;
469         }
470      }
471      else {
472         assert(format == GL_STENCIL);
473         if (baseFormat == GL_STENCIL_INDEX) {
474            /* OK */
475         }
476         else if (ctx->Extensions.EXT_packed_depth_stencil &&
477                  baseFormat == GL_DEPTH_STENCIL_EXT) {
478            /* OK */
479         }
480         else {
481            att->Complete = GL_FALSE;
482            att_incomplete("bad renderbuffer stencil format");
483            return;
484         }
485      }
486   }
487   else {
488      ASSERT(att->Type == GL_NONE);
489      /* complete */
490      return;
491   }
492}
493
494
495/**
496 * Test if the given framebuffer object is complete and update its
497 * Status field with the results.
498 * Calls the ctx->Driver.ValidateFramebuffer() function to allow the
499 * driver to make hardware-specific validation/completeness checks.
500 * Also update the framebuffer's Width and Height fields if the
501 * framebuffer is complete.
502 */
503void
504_mesa_test_framebuffer_completeness(GLcontext *ctx, struct gl_framebuffer *fb)
505{
506   GLuint numImages;
507   GLenum intFormat = GL_NONE; /* color buffers' internal format */
508   GLuint minWidth = ~0, minHeight = ~0, maxWidth = 0, maxHeight = 0;
509   GLint numSamples = -1;
510   GLint i;
511   GLuint j;
512
513   assert(fb->Name != 0);
514
515   numImages = 0;
516   fb->Width = 0;
517   fb->Height = 0;
518
519   /* Start at -2 to more easily loop over all attachment points.
520    *  -2: depth buffer
521    *  -1: stencil buffer
522    * >=0: color buffer
523    */
524   for (i = -2; i < (GLint) ctx->Const.MaxColorAttachments; i++) {
525      struct gl_renderbuffer_attachment *att;
526      GLenum f;
527
528      /*
529       * XXX for ARB_fbo, only check color buffers that are named by
530       * GL_READ_BUFFER and GL_DRAW_BUFFERi.
531       */
532
533      /* check for attachment completeness
534       */
535      if (i == -2) {
536         att = &fb->Attachment[BUFFER_DEPTH];
537         test_attachment_completeness(ctx, GL_DEPTH, att);
538         if (!att->Complete) {
539            fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
540            fbo_incomplete("depth attachment incomplete", -1);
541            return;
542         }
543      }
544      else if (i == -1) {
545         att = &fb->Attachment[BUFFER_STENCIL];
546         test_attachment_completeness(ctx, GL_STENCIL, att);
547         if (!att->Complete) {
548            fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
549            fbo_incomplete("stencil attachment incomplete", -1);
550            return;
551         }
552      }
553      else {
554         att = &fb->Attachment[BUFFER_COLOR0 + i];
555         test_attachment_completeness(ctx, GL_COLOR, att);
556         if (!att->Complete) {
557            fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
558            fbo_incomplete("color attachment incomplete", i);
559            return;
560         }
561      }
562
563      /* get width, height, format of the renderbuffer/texture
564       */
565      if (att->Type == GL_TEXTURE) {
566         const struct gl_texture_image *texImg
567            = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
568         minWidth = MIN2(minWidth, texImg->Width);
569         maxWidth = MAX2(maxWidth, texImg->Width);
570         minHeight = MIN2(minHeight, texImg->Height);
571         maxHeight = MAX2(maxHeight, texImg->Height);
572         f = texImg->_BaseFormat;
573         numImages++;
574         if (f != GL_RGB && f != GL_RGBA && f != GL_DEPTH_COMPONENT
575             && f != GL_DEPTH_STENCIL_EXT) {
576            fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
577            fbo_incomplete("texture attachment incomplete", -1);
578            return;
579         }
580      }
581      else if (att->Type == GL_RENDERBUFFER_EXT) {
582         minWidth = MIN2(minWidth, att->Renderbuffer->Width);
583         maxWidth = MAX2(minWidth, att->Renderbuffer->Width);
584         minHeight = MIN2(minHeight, att->Renderbuffer->Height);
585         maxHeight = MAX2(minHeight, att->Renderbuffer->Height);
586         f = att->Renderbuffer->InternalFormat;
587         numImages++;
588      }
589      else {
590         assert(att->Type == GL_NONE);
591         continue;
592      }
593
594      if (numSamples < 0) {
595         /* first buffer */
596         numSamples = att->Renderbuffer->NumSamples;
597      }
598
599      /* Error-check width, height, format, samples
600       */
601      if (numImages == 1) {
602         /* save format, num samples */
603         if (i >= 0) {
604            intFormat = f;
605         }
606      }
607      else {
608         if (!ctx->Extensions.ARB_framebuffer_object) {
609            /* check that width, height, format are same */
610            if (minWidth != maxWidth || minHeight != maxHeight) {
611               fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
612               fbo_incomplete("width or height mismatch", -1);
613               return;
614            }
615            /* check that all color buffer have same format */
616            if (intFormat != GL_NONE && f != intFormat) {
617               fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
618               fbo_incomplete("format mismatch", -1);
619               return;
620            }
621         }
622         if (att->Renderbuffer &&
623             att->Renderbuffer->NumSamples != numSamples) {
624            fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE;
625            fbo_incomplete("inconsistant number of samples", i);
626            return;
627         }
628
629      }
630   }
631
632#ifndef FEATURE_OES_framebuffer_object
633   /* Check that all DrawBuffers are present */
634   for (j = 0; j < ctx->Const.MaxDrawBuffers; j++) {
635      if (fb->ColorDrawBuffer[j] != GL_NONE) {
636         const struct gl_renderbuffer_attachment *att
637            = _mesa_get_attachment(ctx, fb, fb->ColorDrawBuffer[j]);
638         assert(att);
639         if (att->Type == GL_NONE) {
640            fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
641            fbo_incomplete("missing drawbuffer", j);
642            return;
643         }
644      }
645   }
646
647   /* Check that the ReadBuffer is present */
648   if (fb->ColorReadBuffer != GL_NONE) {
649      const struct gl_renderbuffer_attachment *att
650         = _mesa_get_attachment(ctx, fb, fb->ColorReadBuffer);
651      assert(att);
652      if (att->Type == GL_NONE) {
653         fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
654            fbo_incomplete("missing readbuffer", -1);
655         return;
656      }
657   }
658#else
659   (void) j;
660#endif
661
662   if (numImages == 0) {
663      fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
664      fbo_incomplete("no attachments", -1);
665      return;
666   }
667
668   /* Provisionally set status = COMPLETE ... */
669   fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
670
671   /* ... but the driver may say the FB is incomplete.
672    * Drivers will most likely set the status to GL_FRAMEBUFFER_UNSUPPORTED
673    * if anything.
674    */
675   if (ctx->Driver.ValidateFramebuffer) {
676      ctx->Driver.ValidateFramebuffer(ctx, fb);
677      if (fb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
678         fbo_incomplete("driver marked FBO as incomplete", -1);
679      }
680   }
681
682   if (fb->_Status == GL_FRAMEBUFFER_COMPLETE_EXT) {
683      /*
684       * Note that if ARB_framebuffer_object is supported and the attached
685       * renderbuffers/textures are different sizes, the framebuffer
686       * width/height will be set to the smallest width/height.
687       */
688      fb->Width = minWidth;
689      fb->Height = minHeight;
690
691      /* finally, update the visual info for the framebuffer */
692      _mesa_update_framebuffer_visual(fb);
693   }
694}
695
696
697GLboolean GLAPIENTRY
698_mesa_IsRenderbufferEXT(GLuint renderbuffer)
699{
700   GET_CURRENT_CONTEXT(ctx);
701   ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
702   if (renderbuffer) {
703      struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
704      if (rb != NULL && rb != &DummyRenderbuffer)
705         return GL_TRUE;
706   }
707   return GL_FALSE;
708}
709
710
711void GLAPIENTRY
712_mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
713{
714   struct gl_renderbuffer *newRb;
715   GET_CURRENT_CONTEXT(ctx);
716
717   ASSERT_OUTSIDE_BEGIN_END(ctx);
718
719   if (target != GL_RENDERBUFFER_EXT) {
720      _mesa_error(ctx, GL_INVALID_ENUM, "glBindRenderbufferEXT(target)");
721      return;
722   }
723
724   /* No need to flush here since the render buffer binding has no
725    * effect on rendering state.
726    */
727
728   if (renderbuffer) {
729      newRb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
730      if (newRb == &DummyRenderbuffer) {
731         /* ID was reserved, but no real renderbuffer object made yet */
732         newRb = NULL;
733      }
734      else if (!newRb && ctx->Extensions.ARB_framebuffer_object) {
735         /* All RB IDs must be Gen'd */
736         _mesa_error(ctx, GL_INVALID_OPERATION, "glBindRenderbuffer(buffer)");
737         return;
738      }
739
740      if (!newRb) {
741	 /* create new renderbuffer object */
742	 newRb = ctx->Driver.NewRenderbuffer(ctx, renderbuffer);
743	 if (!newRb) {
744	    _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindRenderbufferEXT");
745	    return;
746	 }
747         ASSERT(newRb->AllocStorage);
748         _mesa_HashInsert(ctx->Shared->RenderBuffers, renderbuffer, newRb);
749         newRb->RefCount = 1; /* referenced by hash table */
750      }
751   }
752   else {
753      newRb = NULL;
754   }
755
756   ASSERT(newRb != &DummyRenderbuffer);
757
758   _mesa_reference_renderbuffer(&ctx->CurrentRenderbuffer, newRb);
759}
760
761
762/**
763 * If the given renderbuffer is anywhere attached to the framebuffer, detach
764 * the renderbuffer.
765 * This is used when a renderbuffer object is deleted.
766 * The spec calls for unbinding.
767 */
768static void
769detach_renderbuffer(GLcontext *ctx,
770                    struct gl_framebuffer *fb,
771                    struct gl_renderbuffer *rb)
772{
773   GLuint i;
774   for (i = 0; i < BUFFER_COUNT; i++) {
775      if (fb->Attachment[i].Renderbuffer == rb) {
776         _mesa_remove_attachment(ctx, &fb->Attachment[i]);
777      }
778   }
779   invalidate_framebuffer(fb);
780}
781
782
783void GLAPIENTRY
784_mesa_DeleteRenderbuffersEXT(GLsizei n, const GLuint *renderbuffers)
785{
786   GLint i;
787   GET_CURRENT_CONTEXT(ctx);
788
789   ASSERT_OUTSIDE_BEGIN_END(ctx);
790   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
791
792   for (i = 0; i < n; i++) {
793      if (renderbuffers[i] > 0) {
794	 struct gl_renderbuffer *rb;
795	 rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
796	 if (rb) {
797            /* check if deleting currently bound renderbuffer object */
798            if (rb == ctx->CurrentRenderbuffer) {
799               /* bind default */
800               ASSERT(rb->RefCount >= 2);
801               _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
802            }
803
804            if (ctx->DrawBuffer->Name) {
805               detach_renderbuffer(ctx, ctx->DrawBuffer, rb);
806            }
807            if (ctx->ReadBuffer->Name && ctx->ReadBuffer != ctx->DrawBuffer) {
808               detach_renderbuffer(ctx, ctx->ReadBuffer, rb);
809            }
810
811	    /* Remove from hash table immediately, to free the ID.
812             * But the object will not be freed until it's no longer
813             * referenced anywhere else.
814             */
815	    _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
816
817            if (rb != &DummyRenderbuffer) {
818               /* no longer referenced by hash table */
819               _mesa_reference_renderbuffer(&rb, NULL);
820	    }
821	 }
822      }
823   }
824}
825
826
827void GLAPIENTRY
828_mesa_GenRenderbuffersEXT(GLsizei n, GLuint *renderbuffers)
829{
830   GET_CURRENT_CONTEXT(ctx);
831   GLuint first;
832   GLint i;
833
834   ASSERT_OUTSIDE_BEGIN_END(ctx);
835
836   if (n < 0) {
837      _mesa_error(ctx, GL_INVALID_VALUE, "glGenRenderbuffersEXT(n)");
838      return;
839   }
840
841   if (!renderbuffers)
842      return;
843
844   first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
845
846   for (i = 0; i < n; i++) {
847      GLuint name = first + i;
848      renderbuffers[i] = name;
849      /* insert dummy placeholder into hash table */
850      _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
851      _mesa_HashInsert(ctx->Shared->RenderBuffers, name, &DummyRenderbuffer);
852      _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
853   }
854}
855
856
857/**
858 * Given an internal format token for a render buffer, return the
859 * corresponding base format.
860 * This is very similar to _mesa_base_tex_format() but the set of valid
861 * internal formats is somewhat different.
862 *
863 * \return one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT
864 *  GL_DEPTH_STENCIL_EXT or zero if error.
865 *
866 * XXX in the future when we support red-only and red-green formats
867 * we'll also return GL_RED and GL_RG.
868 */
869GLenum
870_mesa_base_fbo_format(GLcontext *ctx, GLenum internalFormat)
871{
872   switch (internalFormat) {
873   case GL_RGB:
874   case GL_R3_G3_B2:
875   case GL_RGB4:
876   case GL_RGB5:
877   case GL_RGB8:
878   case GL_RGB10:
879   case GL_RGB12:
880   case GL_RGB16:
881      return GL_RGB;
882   case GL_RGBA:
883   case GL_RGBA2:
884   case GL_RGBA4:
885   case GL_RGB5_A1:
886   case GL_RGBA8:
887   case GL_RGB10_A2:
888   case GL_RGBA12:
889   case GL_RGBA16:
890      return GL_RGBA;
891   case GL_STENCIL_INDEX:
892   case GL_STENCIL_INDEX1_EXT:
893   case GL_STENCIL_INDEX4_EXT:
894   case GL_STENCIL_INDEX8_EXT:
895   case GL_STENCIL_INDEX16_EXT:
896      return GL_STENCIL_INDEX;
897   case GL_DEPTH_COMPONENT:
898   case GL_DEPTH_COMPONENT16:
899   case GL_DEPTH_COMPONENT24:
900   case GL_DEPTH_COMPONENT32:
901      return GL_DEPTH_COMPONENT;
902   case GL_DEPTH_STENCIL_EXT:
903   case GL_DEPTH24_STENCIL8_EXT:
904      if (ctx->Extensions.EXT_packed_depth_stencil)
905         return GL_DEPTH_STENCIL_EXT;
906      else
907         return 0;
908   /* XXX add floating point formats eventually */
909   default:
910      return 0;
911   }
912}
913
914
915/** sentinal value, see below */
916#define NO_SAMPLES 1000
917
918
919/**
920 * Helper function used by _mesa_RenderbufferStorageEXT() and
921 * _mesa_RenderbufferStorageMultisample().
922 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
923 */
924static void
925renderbuffer_storage(GLenum target, GLenum internalFormat,
926                     GLsizei width, GLsizei height, GLsizei samples)
927{
928   const char *func = samples == NO_SAMPLES ?
929      "glRenderbufferStorage" : "RenderbufferStorageMultisample";
930   struct gl_renderbuffer *rb;
931   GLenum baseFormat;
932   GET_CURRENT_CONTEXT(ctx);
933
934   ASSERT_OUTSIDE_BEGIN_END(ctx);
935
936   if (target != GL_RENDERBUFFER_EXT) {
937      _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
938      return;
939   }
940
941   baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
942   if (baseFormat == 0) {
943      _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat)", func);
944      return;
945   }
946
947   if (width < 1 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
948      _mesa_error(ctx, GL_INVALID_VALUE, "%s(width)", func);
949      return;
950   }
951
952   if (height < 1 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
953      _mesa_error(ctx, GL_INVALID_VALUE, "%s(height)", func);
954      return;
955   }
956
957   if (samples == NO_SAMPLES) {
958      /* NumSamples == 0 indicates non-multisampling */
959      samples = 0;
960   }
961   else if (samples > (GLsizei) ctx->Const.MaxSamples) {
962      /* note: driver may choose to use more samples than what's requested */
963      _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples)", func);
964      return;
965   }
966
967   rb = ctx->CurrentRenderbuffer;
968   if (!rb) {
969      _mesa_error(ctx, GL_INVALID_OPERATION, func);
970      return;
971   }
972
973   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
974
975   if (rb->InternalFormat == internalFormat &&
976       rb->Width == (GLuint) width &&
977       rb->Height == (GLuint) height) {
978      /* no change in allocation needed */
979      return;
980   }
981
982   /* These MUST get set by the AllocStorage func */
983   rb->Format = MESA_FORMAT_NONE;
984   rb->NumSamples = samples;
985
986   /* Now allocate the storage */
987   ASSERT(rb->AllocStorage);
988   if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
989      /* No error - check/set fields now */
990      assert(rb->Format != MESA_FORMAT_NONE);
991      assert(rb->Width == (GLuint) width);
992      assert(rb->Height == (GLuint) height);
993      rb->InternalFormat = internalFormat;
994      rb->_BaseFormat = baseFormat;
995      assert(rb->_BaseFormat != 0);
996   }
997   else {
998      /* Probably ran out of memory - clear the fields */
999      rb->Width = 0;
1000      rb->Height = 0;
1001      rb->Format = MESA_FORMAT_NONE;
1002      rb->InternalFormat = GL_NONE;
1003      rb->_BaseFormat = GL_NONE;
1004      rb->NumSamples = 0;
1005   }
1006
1007   /*
1008   test_framebuffer_completeness(ctx, fb);
1009   */
1010   /* XXX if this renderbuffer is attached anywhere, invalidate attachment
1011    * points???
1012    */
1013}
1014
1015#if FEATURE_OES_EGL_image
1016void GLAPIENTRY
1017_mesa_EGLImageTargetRenderbufferStorageOES (GLenum target, GLeglImageOES image)
1018{
1019   struct gl_renderbuffer *rb;
1020   GET_CURRENT_CONTEXT(ctx);
1021   ASSERT_OUTSIDE_BEGIN_END(ctx);
1022
1023   if (target != GL_RENDERBUFFER) {
1024      _mesa_error(ctx, GL_INVALID_ENUM, "EGLImageTargetRenderbufferStorageOES");
1025      return;
1026   }
1027
1028   rb = ctx->CurrentRenderbuffer;
1029   if (!rb) {
1030      _mesa_error(ctx, GL_INVALID_OPERATION, "EGLImageTargetRenderbufferStorageOES");
1031      return;
1032   }
1033
1034   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1035
1036   ctx->Driver.EGLImageTargetRenderbufferStorage(ctx, rb, image);
1037}
1038#endif
1039
1040/**
1041 * Helper function for _mesa_GetRenderbufferParameterivEXT() and
1042 * _mesa_GetFramebufferAttachmentParameterivEXT()
1043 * We have to be careful to respect the base format.  For example, if a
1044 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1045 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1046 * we need to return zero.
1047 */
1048static GLint
1049get_component_bits(GLenum pname, GLenum baseFormat, gl_format format)
1050{
1051   switch (pname) {
1052   case GL_RENDERBUFFER_RED_SIZE_EXT:
1053   case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
1054   case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1055   case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
1056   case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1057   case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
1058      if (baseFormat == GL_RGB || baseFormat == GL_RGBA)
1059         return _mesa_get_format_bits(format, pname);
1060      else
1061         return 0;
1062   case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1063   case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
1064      if (baseFormat == GL_RGBA || baseFormat == GL_ALPHA)
1065         return _mesa_get_format_bits(format, pname);
1066      else
1067         return 0;
1068   case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1069   case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
1070      if (baseFormat == GL_DEPTH_COMPONENT || baseFormat == GL_DEPTH_STENCIL)
1071         return _mesa_get_format_bits(format, pname);
1072      else
1073         return 0;
1074   case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1075   case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
1076      if (baseFormat == GL_STENCIL_INDEX || baseFormat == GL_DEPTH_STENCIL)
1077         return _mesa_get_format_bits(format, pname);
1078      else
1079         return 0;
1080   default:
1081      return 0;
1082   }
1083}
1084
1085
1086
1087void GLAPIENTRY
1088_mesa_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1089                             GLsizei width, GLsizei height)
1090{
1091   /* GL_ARB_fbo says calling this function is equivalent to calling
1092    * glRenderbufferStorageMultisample() with samples=0.  We pass in
1093    * a token value here just for error reporting purposes.
1094    */
1095   renderbuffer_storage(target, internalFormat, width, height, NO_SAMPLES);
1096}
1097
1098
1099void GLAPIENTRY
1100_mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
1101                                     GLenum internalFormat,
1102                                     GLsizei width, GLsizei height)
1103{
1104   renderbuffer_storage(target, internalFormat, width, height, samples);
1105}
1106
1107
1108
1109void GLAPIENTRY
1110_mesa_GetRenderbufferParameterivEXT(GLenum target, GLenum pname, GLint *params)
1111{
1112   struct gl_renderbuffer *rb;
1113   GET_CURRENT_CONTEXT(ctx);
1114
1115   ASSERT_OUTSIDE_BEGIN_END(ctx);
1116
1117   if (target != GL_RENDERBUFFER_EXT) {
1118      _mesa_error(ctx, GL_INVALID_ENUM,
1119                  "glGetRenderbufferParameterivEXT(target)");
1120      return;
1121   }
1122
1123   rb = ctx->CurrentRenderbuffer;
1124   if (!rb) {
1125      _mesa_error(ctx, GL_INVALID_OPERATION,
1126                  "glGetRenderbufferParameterivEXT");
1127      return;
1128   }
1129
1130   /* No need to flush here since we're just quering state which is
1131    * not effected by rendering.
1132    */
1133
1134   switch (pname) {
1135   case GL_RENDERBUFFER_WIDTH_EXT:
1136      *params = rb->Width;
1137      return;
1138   case GL_RENDERBUFFER_HEIGHT_EXT:
1139      *params = rb->Height;
1140      return;
1141   case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
1142      *params = rb->InternalFormat;
1143      return;
1144   case GL_RENDERBUFFER_RED_SIZE_EXT:
1145   case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1146   case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1147   case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1148   case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1149   case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1150      *params = get_component_bits(pname, rb->_BaseFormat, rb->Format);
1151      break;
1152   case GL_RENDERBUFFER_SAMPLES:
1153      if (ctx->Extensions.ARB_framebuffer_object) {
1154         *params = rb->NumSamples;
1155         break;
1156      }
1157      /* fallthrough */
1158   default:
1159      _mesa_error(ctx, GL_INVALID_ENUM,
1160                  "glGetRenderbufferParameterivEXT(target)");
1161      return;
1162   }
1163}
1164
1165
1166GLboolean GLAPIENTRY
1167_mesa_IsFramebufferEXT(GLuint framebuffer)
1168{
1169   GET_CURRENT_CONTEXT(ctx);
1170   ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1171   if (framebuffer) {
1172      struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
1173      if (rb != NULL && rb != &DummyFramebuffer)
1174         return GL_TRUE;
1175   }
1176   return GL_FALSE;
1177}
1178
1179
1180/**
1181 * Check if any of the attachments of the given framebuffer are textures
1182 * (render to texture).  Call ctx->Driver.RenderTexture() for such
1183 * attachments.
1184 */
1185static void
1186check_begin_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
1187{
1188   GLuint i;
1189   ASSERT(ctx->Driver.RenderTexture);
1190
1191   if (fb->Name == 0)
1192      return; /* can't render to texture with winsys framebuffers */
1193
1194   for (i = 0; i < BUFFER_COUNT; i++) {
1195      struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1196      struct gl_texture_object *texObj = att->Texture;
1197      if (texObj
1198          && texObj->Image[att->CubeMapFace][att->TextureLevel]) {
1199         ctx->Driver.RenderTexture(ctx, fb, att);
1200      }
1201   }
1202}
1203
1204
1205/**
1206 * Examine all the framebuffer's attachments to see if any are textures.
1207 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1208 * notify the device driver that the texture image may have changed.
1209 */
1210static void
1211check_end_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
1212{
1213   if (fb->Name == 0)
1214      return; /* can't render to texture with winsys framebuffers */
1215
1216   if (ctx->Driver.FinishRenderTexture) {
1217      GLuint i;
1218      for (i = 0; i < BUFFER_COUNT; i++) {
1219         struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1220         if (att->Texture && att->Renderbuffer) {
1221            ctx->Driver.FinishRenderTexture(ctx, att);
1222         }
1223      }
1224   }
1225}
1226
1227
1228void GLAPIENTRY
1229_mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
1230{
1231   struct gl_framebuffer *newDrawFb, *newReadFb;
1232   struct gl_framebuffer *oldDrawFb, *oldReadFb;
1233   GLboolean bindReadBuf, bindDrawBuf;
1234   GET_CURRENT_CONTEXT(ctx);
1235
1236#ifdef DEBUG
1237   if (ctx->Extensions.ARB_framebuffer_object) {
1238      ASSERT(ctx->Extensions.EXT_framebuffer_object);
1239      ASSERT(ctx->Extensions.EXT_framebuffer_blit);
1240   }
1241#endif
1242
1243   ASSERT_OUTSIDE_BEGIN_END(ctx);
1244
1245   if (!ctx->Extensions.EXT_framebuffer_object) {
1246      _mesa_error(ctx, GL_INVALID_OPERATION,
1247                  "glBindFramebufferEXT(unsupported)");
1248      return;
1249   }
1250
1251   switch (target) {
1252#if FEATURE_EXT_framebuffer_blit
1253   case GL_DRAW_FRAMEBUFFER_EXT:
1254      if (!ctx->Extensions.EXT_framebuffer_blit) {
1255         _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1256         return;
1257      }
1258      bindDrawBuf = GL_TRUE;
1259      bindReadBuf = GL_FALSE;
1260      break;
1261   case GL_READ_FRAMEBUFFER_EXT:
1262      if (!ctx->Extensions.EXT_framebuffer_blit) {
1263         _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1264         return;
1265      }
1266      bindDrawBuf = GL_FALSE;
1267      bindReadBuf = GL_TRUE;
1268      break;
1269#endif
1270   case GL_FRAMEBUFFER_EXT:
1271      bindDrawBuf = GL_TRUE;
1272      bindReadBuf = GL_TRUE;
1273      break;
1274   default:
1275      _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1276      return;
1277   }
1278
1279   if (framebuffer) {
1280      /* Binding a user-created framebuffer object */
1281      newDrawFb = _mesa_lookup_framebuffer(ctx, framebuffer);
1282      if (newDrawFb == &DummyFramebuffer) {
1283         /* ID was reserved, but no real framebuffer object made yet */
1284         newDrawFb = NULL;
1285      }
1286      else if (!newDrawFb && ctx->Extensions.ARB_framebuffer_object) {
1287         /* All FBO IDs must be Gen'd */
1288         _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
1289         return;
1290      }
1291
1292      if (!newDrawFb) {
1293	 /* create new framebuffer object */
1294	 newDrawFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
1295	 if (!newDrawFb) {
1296	    _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
1297	    return;
1298	 }
1299         _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newDrawFb);
1300      }
1301      newReadFb = newDrawFb;
1302   }
1303   else {
1304      /* Binding the window system framebuffer (which was originally set
1305       * with MakeCurrent).
1306       */
1307      newDrawFb = ctx->WinSysDrawBuffer;
1308      newReadFb = ctx->WinSysReadBuffer;
1309   }
1310
1311   ASSERT(newDrawFb);
1312   ASSERT(newDrawFb != &DummyFramebuffer);
1313
1314   /* save pointers to current/old framebuffers */
1315   oldDrawFb = ctx->DrawBuffer;
1316   oldReadFb = ctx->ReadBuffer;
1317
1318   /* check if really changing bindings */
1319   if (oldDrawFb == newDrawFb)
1320      bindDrawBuf = GL_FALSE;
1321   if (oldReadFb == newReadFb)
1322      bindReadBuf = GL_FALSE;
1323
1324   /*
1325    * OK, now bind the new Draw/Read framebuffers, if they're changing.
1326    *
1327    * We also check if we're beginning and/or ending render-to-texture.
1328    * When a framebuffer with texture attachments is unbound, call
1329    * ctx->Driver.FinishRenderTexture().
1330    * When a framebuffer with texture attachments is bound, call
1331    * ctx->Driver.RenderTexture().
1332    *
1333    * Note that if the ReadBuffer has texture attachments we don't consider
1334    * that a render-to-texture case.
1335    */
1336   if (bindReadBuf) {
1337      FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1338
1339      /* check if old readbuffer was render-to-texture */
1340      check_end_texture_render(ctx, oldReadFb);
1341
1342      _mesa_reference_framebuffer(&ctx->ReadBuffer, newReadFb);
1343   }
1344
1345   if (bindDrawBuf) {
1346      FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1347
1348      /* check if old read/draw buffers were render-to-texture */
1349      if (!bindReadBuf)
1350         check_end_texture_render(ctx, oldReadFb);
1351
1352      if (oldDrawFb != oldReadFb)
1353         check_end_texture_render(ctx, oldDrawFb);
1354
1355      /* check if newly bound framebuffer has any texture attachments */
1356      check_begin_texture_render(ctx, newDrawFb);
1357
1358      _mesa_reference_framebuffer(&ctx->DrawBuffer, newDrawFb);
1359   }
1360
1361   if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
1362      ctx->Driver.BindFramebuffer(ctx, target, newDrawFb, newReadFb);
1363   }
1364}
1365
1366
1367void GLAPIENTRY
1368_mesa_DeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
1369{
1370   GLint i;
1371   GET_CURRENT_CONTEXT(ctx);
1372
1373   ASSERT_OUTSIDE_BEGIN_END(ctx);
1374   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1375
1376   for (i = 0; i < n; i++) {
1377      if (framebuffers[i] > 0) {
1378	 struct gl_framebuffer *fb;
1379	 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
1380	 if (fb) {
1381            ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
1382
1383            /* check if deleting currently bound framebuffer object */
1384            if (ctx->Extensions.EXT_framebuffer_blit) {
1385               /* separate draw/read binding points */
1386               if (fb == ctx->DrawBuffer) {
1387                  /* bind default */
1388                  ASSERT(fb->RefCount >= 2);
1389                  _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
1390               }
1391               if (fb == ctx->ReadBuffer) {
1392                  /* bind default */
1393                  ASSERT(fb->RefCount >= 2);
1394                  _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
1395               }
1396            }
1397            else {
1398               /* only one binding point for read/draw buffers */
1399               if (fb == ctx->DrawBuffer || fb == ctx->ReadBuffer) {
1400                  /* bind default */
1401                  ASSERT(fb->RefCount >= 2);
1402                  _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
1403               }
1404            }
1405
1406	    /* remove from hash table immediately, to free the ID */
1407	    _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
1408
1409            if (fb != &DummyFramebuffer) {
1410               /* But the object will not be freed until it's no longer
1411                * bound in any context.
1412                */
1413               _mesa_reference_framebuffer(&fb, NULL);
1414	    }
1415	 }
1416      }
1417   }
1418}
1419
1420
1421void GLAPIENTRY
1422_mesa_GenFramebuffersEXT(GLsizei n, GLuint *framebuffers)
1423{
1424   GET_CURRENT_CONTEXT(ctx);
1425   GLuint first;
1426   GLint i;
1427
1428   ASSERT_OUTSIDE_BEGIN_END(ctx);
1429
1430   if (n < 0) {
1431      _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
1432      return;
1433   }
1434
1435   if (!framebuffers)
1436      return;
1437
1438   first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
1439
1440   for (i = 0; i < n; i++) {
1441      GLuint name = first + i;
1442      framebuffers[i] = name;
1443      /* insert dummy placeholder into hash table */
1444      _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1445      _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
1446      _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1447   }
1448}
1449
1450
1451
1452GLenum GLAPIENTRY
1453_mesa_CheckFramebufferStatusEXT(GLenum target)
1454{
1455   struct gl_framebuffer *buffer;
1456   GET_CURRENT_CONTEXT(ctx);
1457
1458   ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1459
1460   switch (target) {
1461#if FEATURE_EXT_framebuffer_blit
1462   case GL_DRAW_FRAMEBUFFER_EXT:
1463      if (!ctx->Extensions.EXT_framebuffer_blit) {
1464         _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1465         return 0;
1466      }
1467      buffer = ctx->DrawBuffer;
1468      break;
1469   case GL_READ_FRAMEBUFFER_EXT:
1470      if (!ctx->Extensions.EXT_framebuffer_blit) {
1471         _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1472         return 0;
1473      }
1474      buffer = ctx->ReadBuffer;
1475      break;
1476#endif
1477   case GL_FRAMEBUFFER_EXT:
1478      buffer = ctx->DrawBuffer;
1479      break;
1480   default:
1481      _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1482      return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1483   }
1484
1485   if (buffer->Name == 0) {
1486      /* The window system / default framebuffer is always complete */
1487      return GL_FRAMEBUFFER_COMPLETE_EXT;
1488   }
1489
1490   /* No need to flush here */
1491
1492   if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
1493      _mesa_test_framebuffer_completeness(ctx, buffer);
1494   }
1495
1496   return buffer->_Status;
1497}
1498
1499
1500
1501/**
1502 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1503 */
1504static void
1505framebuffer_texture(GLcontext *ctx, const char *caller, GLenum target,
1506                    GLenum attachment, GLenum textarget, GLuint texture,
1507                    GLint level, GLint zoffset)
1508{
1509   struct gl_renderbuffer_attachment *att;
1510   struct gl_texture_object *texObj = NULL;
1511   struct gl_framebuffer *fb;
1512   GLboolean error = GL_FALSE;
1513
1514   ASSERT_OUTSIDE_BEGIN_END(ctx);
1515
1516   switch (target) {
1517   case GL_READ_FRAMEBUFFER_EXT:
1518      error = !ctx->Extensions.EXT_framebuffer_blit;
1519      fb = ctx->ReadBuffer;
1520      break;
1521   case GL_DRAW_FRAMEBUFFER_EXT:
1522      error = !ctx->Extensions.EXT_framebuffer_blit;
1523      /* fall-through */
1524   case GL_FRAMEBUFFER_EXT:
1525      fb = ctx->DrawBuffer;
1526      break;
1527   default:
1528      error = GL_TRUE;
1529   }
1530
1531   if (error) {
1532      _mesa_error(ctx, GL_INVALID_ENUM,
1533                  "glFramebufferTexture%sEXT(target=0x%x)", caller, target);
1534      return;
1535   }
1536
1537   ASSERT(fb);
1538
1539   /* check framebuffer binding */
1540   if (fb->Name == 0) {
1541      _mesa_error(ctx, GL_INVALID_OPERATION,
1542                  "glFramebufferTexture%sEXT", caller);
1543      return;
1544   }
1545
1546
1547   /* The textarget, level, and zoffset parameters are only validated if
1548    * texture is non-zero.
1549    */
1550   if (texture) {
1551      GLboolean err = GL_TRUE;
1552
1553      texObj = _mesa_lookup_texture(ctx, texture);
1554      if (texObj != NULL) {
1555         if (textarget == 0) {
1556            /* XXX what's the purpose of this? */
1557            err = (texObj->Target != GL_TEXTURE_3D) &&
1558                (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
1559                (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT);
1560         }
1561         else {
1562            err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
1563                ? !IS_CUBE_FACE(textarget)
1564                : (texObj->Target != textarget);
1565         }
1566      }
1567      else {
1568         /* can't render to a non-existant texture */
1569         _mesa_error(ctx, GL_INVALID_OPERATION,
1570                     "glFramebufferTexture%sEXT(non existant texture)",
1571                     caller);
1572         return;
1573      }
1574
1575      if (err) {
1576         _mesa_error(ctx, GL_INVALID_OPERATION,
1577                     "glFramebufferTexture%sEXT(texture target mismatch)",
1578                     caller);
1579         return;
1580      }
1581
1582      if (texObj->Target == GL_TEXTURE_3D) {
1583         const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1584         if (zoffset < 0 || zoffset >= maxSize) {
1585            _mesa_error(ctx, GL_INVALID_VALUE,
1586                        "glFramebufferTexture%sEXT(zoffset)", caller);
1587            return;
1588         }
1589      }
1590      else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
1591               (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
1592         if (zoffset < 0 || zoffset >= ctx->Const.MaxArrayTextureLayers) {
1593            _mesa_error(ctx, GL_INVALID_VALUE,
1594                        "glFramebufferTexture%sEXT(layer)", caller);
1595            return;
1596         }
1597      }
1598
1599      if ((level < 0) ||
1600          (level >= _mesa_max_texture_levels(ctx, texObj->Target))) {
1601         _mesa_error(ctx, GL_INVALID_VALUE,
1602                     "glFramebufferTexture%sEXT(level)", caller);
1603         return;
1604      }
1605   }
1606
1607   att = _mesa_get_attachment(ctx, fb, attachment);
1608   if (att == NULL) {
1609      _mesa_error(ctx, GL_INVALID_ENUM,
1610                  "glFramebufferTexture%sEXT(attachment)", caller);
1611      return;
1612   }
1613
1614   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1615
1616   _glthread_LOCK_MUTEX(fb->Mutex);
1617   if (texObj) {
1618      _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
1619                                   level, zoffset);
1620      /* Set the render-to-texture flag.  We'll check this flag in
1621       * glTexImage() and friends to determine if we need to revalidate
1622       * any FBOs that might be rendering into this texture.
1623       * This flag never gets cleared since it's non-trivial to determine
1624       * when all FBOs might be done rendering to this texture.  That's OK
1625       * though since it's uncommon to render to a texture then repeatedly
1626       * call glTexImage() to change images in the texture.
1627       */
1628      texObj->_RenderToTexture = GL_TRUE;
1629   }
1630   else {
1631      _mesa_remove_attachment(ctx, att);
1632   }
1633
1634   invalidate_framebuffer(fb);
1635
1636   _glthread_UNLOCK_MUTEX(fb->Mutex);
1637}
1638
1639
1640
1641void GLAPIENTRY
1642_mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
1643                              GLenum textarget, GLuint texture, GLint level)
1644{
1645   GET_CURRENT_CONTEXT(ctx);
1646
1647   if ((texture != 0) && (textarget != GL_TEXTURE_1D)) {
1648      _mesa_error(ctx, GL_INVALID_ENUM,
1649                  "glFramebufferTexture1DEXT(textarget)");
1650      return;
1651   }
1652
1653   framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
1654                       level, 0);
1655}
1656
1657
1658void GLAPIENTRY
1659_mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
1660                              GLenum textarget, GLuint texture, GLint level)
1661{
1662   GET_CURRENT_CONTEXT(ctx);
1663
1664   if ((texture != 0) &&
1665       (textarget != GL_TEXTURE_2D) &&
1666       (textarget != GL_TEXTURE_RECTANGLE_ARB) &&
1667       (!IS_CUBE_FACE(textarget))) {
1668      _mesa_error(ctx, GL_INVALID_OPERATION,
1669                  "glFramebufferTexture2DEXT(textarget=0x%x)", textarget);
1670      return;
1671   }
1672
1673   framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
1674                       level, 0);
1675}
1676
1677
1678void GLAPIENTRY
1679_mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
1680                              GLenum textarget, GLuint texture,
1681                              GLint level, GLint zoffset)
1682{
1683   GET_CURRENT_CONTEXT(ctx);
1684
1685   if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
1686      _mesa_error(ctx, GL_INVALID_ENUM,
1687                  "glFramebufferTexture3DEXT(textarget)");
1688      return;
1689   }
1690
1691   framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
1692                       level, zoffset);
1693}
1694
1695
1696void GLAPIENTRY
1697_mesa_FramebufferTextureLayerEXT(GLenum target, GLenum attachment,
1698                                 GLuint texture, GLint level, GLint layer)
1699{
1700   GET_CURRENT_CONTEXT(ctx);
1701
1702   framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
1703                       level, layer);
1704}
1705
1706
1707void GLAPIENTRY
1708_mesa_FramebufferRenderbufferEXT(GLenum target, GLenum attachment,
1709                                 GLenum renderbufferTarget,
1710                                 GLuint renderbuffer)
1711{
1712   struct gl_renderbuffer_attachment *att;
1713   struct gl_framebuffer *fb;
1714   struct gl_renderbuffer *rb;
1715   GET_CURRENT_CONTEXT(ctx);
1716
1717   ASSERT_OUTSIDE_BEGIN_END(ctx);
1718
1719   switch (target) {
1720#if FEATURE_EXT_framebuffer_blit
1721   case GL_DRAW_FRAMEBUFFER_EXT:
1722      if (!ctx->Extensions.EXT_framebuffer_blit) {
1723         _mesa_error(ctx, GL_INVALID_ENUM,
1724                     "glFramebufferRenderbufferEXT(target)");
1725         return;
1726      }
1727      fb = ctx->DrawBuffer;
1728      break;
1729   case GL_READ_FRAMEBUFFER_EXT:
1730      if (!ctx->Extensions.EXT_framebuffer_blit) {
1731         _mesa_error(ctx, GL_INVALID_ENUM,
1732                     "glFramebufferRenderbufferEXT(target)");
1733         return;
1734      }
1735      fb = ctx->ReadBuffer;
1736      break;
1737#endif
1738   case GL_FRAMEBUFFER_EXT:
1739      fb = ctx->DrawBuffer;
1740      break;
1741   default:
1742      _mesa_error(ctx, GL_INVALID_ENUM,
1743                  "glFramebufferRenderbufferEXT(target)");
1744      return;
1745   }
1746
1747   if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
1748      _mesa_error(ctx, GL_INVALID_ENUM,
1749                  "glFramebufferRenderbufferEXT(renderbufferTarget)");
1750      return;
1751   }
1752
1753   if (fb->Name == 0) {
1754      /* Can't attach new renderbuffers to a window system framebuffer */
1755      _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
1756      return;
1757   }
1758
1759   att = _mesa_get_attachment(ctx, fb, attachment);
1760   if (att == NULL) {
1761      _mesa_error(ctx, GL_INVALID_ENUM,
1762                  "glFramebufferRenderbufferEXT(invalid attachment %s)",
1763                  _mesa_lookup_enum_by_nr(attachment));
1764      return;
1765   }
1766
1767   if (renderbuffer) {
1768      rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
1769      if (!rb) {
1770	 _mesa_error(ctx, GL_INVALID_OPERATION,
1771		     "glFramebufferRenderbufferEXT(non-existant"
1772                     " renderbuffer %u)", renderbuffer);
1773	 return;
1774      }
1775   }
1776   else {
1777      /* remove renderbuffer attachment */
1778      rb = NULL;
1779   }
1780
1781   if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1782      /* make sure the renderbuffer is a depth/stencil format */
1783      const GLenum baseFormat =
1784         _mesa_get_format_base_format(att->Renderbuffer->Format);
1785      if (baseFormat != GL_DEPTH_STENCIL) {
1786         _mesa_error(ctx, GL_INVALID_OPERATION,
1787                     "glFramebufferRenderbufferEXT(renderbuffer"
1788                     " is not DEPTH_STENCIL format)");
1789         return;
1790      }
1791   }
1792
1793
1794   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1795
1796   assert(ctx->Driver.FramebufferRenderbuffer);
1797   ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
1798
1799   /* Some subsequent GL commands may depend on the framebuffer's visual
1800    * after the binding is updated.  Update visual info now.
1801    */
1802   _mesa_update_framebuffer_visual(fb);
1803}
1804
1805
1806void GLAPIENTRY
1807_mesa_GetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment,
1808                                             GLenum pname, GLint *params)
1809{
1810   const struct gl_renderbuffer_attachment *att;
1811   struct gl_framebuffer *buffer;
1812   GET_CURRENT_CONTEXT(ctx);
1813
1814   ASSERT_OUTSIDE_BEGIN_END(ctx);
1815
1816   switch (target) {
1817#if FEATURE_EXT_framebuffer_blit
1818   case GL_DRAW_FRAMEBUFFER_EXT:
1819      if (!ctx->Extensions.EXT_framebuffer_blit) {
1820         _mesa_error(ctx, GL_INVALID_ENUM,
1821                     "glGetFramebufferAttachmentParameterivEXT(target)");
1822         return;
1823      }
1824      buffer = ctx->DrawBuffer;
1825      break;
1826   case GL_READ_FRAMEBUFFER_EXT:
1827      if (!ctx->Extensions.EXT_framebuffer_blit) {
1828         _mesa_error(ctx, GL_INVALID_ENUM,
1829                     "glGetFramebufferAttachmentParameterivEXT(target)");
1830         return;
1831      }
1832      buffer = ctx->ReadBuffer;
1833      break;
1834#endif
1835   case GL_FRAMEBUFFER_EXT:
1836      buffer = ctx->DrawBuffer;
1837      break;
1838   default:
1839      _mesa_error(ctx, GL_INVALID_ENUM,
1840                  "glGetFramebufferAttachmentParameterivEXT(target)");
1841      return;
1842   }
1843
1844   if (buffer->Name == 0) {
1845      _mesa_error(ctx, GL_INVALID_OPERATION,
1846                  "glGetFramebufferAttachmentParameterivEXT");
1847      return;
1848   }
1849
1850   att = _mesa_get_attachment(ctx, buffer, attachment);
1851   if (att == NULL) {
1852      _mesa_error(ctx, GL_INVALID_ENUM,
1853                  "glGetFramebufferAttachmentParameterivEXT(attachment)");
1854      return;
1855   }
1856
1857   if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1858      /* the depth and stencil attachments must point to the same buffer */
1859      const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
1860      depthAtt = _mesa_get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
1861      stencilAtt = _mesa_get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
1862      if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
1863         _mesa_error(ctx, GL_INVALID_OPERATION,
1864                     "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
1865                     " attachments differ)");
1866         return;
1867      }
1868   }
1869
1870   /* No need to flush here */
1871
1872   switch (pname) {
1873   case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
1874      *params = att->Type;
1875      return;
1876   case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
1877      if (att->Type == GL_RENDERBUFFER_EXT) {
1878	 *params = att->Renderbuffer->Name;
1879      }
1880      else if (att->Type == GL_TEXTURE) {
1881	 *params = att->Texture->Name;
1882      }
1883      else {
1884	 _mesa_error(ctx, GL_INVALID_ENUM,
1885		     "glGetFramebufferAttachmentParameterivEXT(pname)");
1886      }
1887      return;
1888   case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
1889      if (att->Type == GL_TEXTURE) {
1890	 *params = att->TextureLevel;
1891      }
1892      else {
1893	 _mesa_error(ctx, GL_INVALID_ENUM,
1894		     "glGetFramebufferAttachmentParameterivEXT(pname)");
1895      }
1896      return;
1897   case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
1898      if (att->Type == GL_TEXTURE) {
1899         if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
1900            *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
1901         }
1902         else {
1903            *params = 0;
1904         }
1905      }
1906      else {
1907	 _mesa_error(ctx, GL_INVALID_ENUM,
1908		     "glGetFramebufferAttachmentParameterivEXT(pname)");
1909      }
1910      return;
1911   case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
1912      if (att->Type == GL_TEXTURE) {
1913         if (att->Texture && att->Texture->Target == GL_TEXTURE_3D) {
1914            *params = att->Zoffset;
1915         }
1916         else {
1917            *params = 0;
1918         }
1919      }
1920      else {
1921	 _mesa_error(ctx, GL_INVALID_ENUM,
1922		     "glGetFramebufferAttachmentParameterivEXT(pname)");
1923      }
1924      return;
1925   case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
1926      if (!ctx->Extensions.ARB_framebuffer_object) {
1927         _mesa_error(ctx, GL_INVALID_ENUM,
1928                     "glGetFramebufferAttachmentParameterivEXT(pname)");
1929      }
1930      else {
1931         *params = _mesa_get_format_color_encoding(att->Renderbuffer->Format);
1932      }
1933      return;
1934   case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
1935      if (!ctx->Extensions.ARB_framebuffer_object) {
1936         _mesa_error(ctx, GL_INVALID_ENUM,
1937                     "glGetFramebufferAttachmentParameterivEXT(pname)");
1938         return;
1939      }
1940      else {
1941         gl_format format = att->Renderbuffer->Format;
1942         if (format == MESA_FORMAT_CI8 || format == MESA_FORMAT_S8) {
1943            /* special cases */
1944            *params = GL_INDEX;
1945         }
1946         else {
1947            *params = _mesa_get_format_datatype(format);
1948         }
1949      }
1950      return;
1951   case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
1952   case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
1953   case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
1954   case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
1955   case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
1956   case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
1957      if (!ctx->Extensions.ARB_framebuffer_object) {
1958         _mesa_error(ctx, GL_INVALID_ENUM,
1959                     "glGetFramebufferAttachmentParameterivEXT(pname)");
1960      }
1961      else if (att->Texture) {
1962         const struct gl_texture_image *texImage =
1963            _mesa_select_tex_image(ctx, att->Texture, att->Texture->Target,
1964                                   att->TextureLevel);
1965         if (texImage) {
1966            *params = get_component_bits(pname, texImage->_BaseFormat,
1967                                         texImage->TexFormat);
1968         }
1969         else {
1970            *params = 0;
1971         }
1972      }
1973      else if (att->Renderbuffer) {
1974         *params = get_component_bits(pname, att->Renderbuffer->_BaseFormat,
1975                                      att->Renderbuffer->Format);
1976      }
1977      else {
1978         *params = 0;
1979      }
1980      return;
1981   default:
1982      _mesa_error(ctx, GL_INVALID_ENUM,
1983                  "glGetFramebufferAttachmentParameterivEXT(pname)");
1984      return;
1985   }
1986}
1987
1988
1989void GLAPIENTRY
1990_mesa_GenerateMipmapEXT(GLenum target)
1991{
1992   struct gl_texture_object *texObj;
1993   GET_CURRENT_CONTEXT(ctx);
1994
1995   ASSERT_OUTSIDE_BEGIN_END(ctx);
1996   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1997
1998   switch (target) {
1999   case GL_TEXTURE_1D:
2000   case GL_TEXTURE_2D:
2001   case GL_TEXTURE_3D:
2002   case GL_TEXTURE_CUBE_MAP:
2003      /* OK, legal value */
2004      break;
2005   default:
2006      _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target)");
2007      return;
2008   }
2009
2010   texObj = _mesa_get_current_tex_object(ctx, target);
2011
2012   if (texObj->BaseLevel >= texObj->MaxLevel) {
2013      /* nothing to do */
2014      return;
2015   }
2016
2017   _mesa_lock_texture(ctx, texObj);
2018   if (target == GL_TEXTURE_CUBE_MAP) {
2019      GLuint face;
2020      for (face = 0; face < 6; face++)
2021	 ctx->Driver.GenerateMipmap(ctx,
2022				    GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + face,
2023				    texObj);
2024   }
2025   else {
2026      ctx->Driver.GenerateMipmap(ctx, target, texObj);
2027   }
2028   _mesa_unlock_texture(ctx, texObj);
2029}
2030
2031
2032#if FEATURE_EXT_framebuffer_blit
2033
2034static const struct gl_renderbuffer_attachment *
2035find_attachment(const struct gl_framebuffer *fb, const struct gl_renderbuffer *rb)
2036{
2037   GLuint i;
2038   for (i = 0; i < Elements(fb->Attachment); i++) {
2039      if (fb->Attachment[i].Renderbuffer == rb)
2040         return &fb->Attachment[i];
2041   }
2042   return NULL;
2043}
2044
2045
2046
2047/**
2048 * Blit rectangular region, optionally from one framebuffer to another.
2049 *
2050 * Note, if the src buffer is multisampled and the dest is not, this is
2051 * when the samples must be resolved to a single color.
2052 */
2053void GLAPIENTRY
2054_mesa_BlitFramebufferEXT(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
2055                         GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
2056                         GLbitfield mask, GLenum filter)
2057{
2058   const GLbitfield legalMaskBits = (GL_COLOR_BUFFER_BIT |
2059                                     GL_DEPTH_BUFFER_BIT |
2060                                     GL_STENCIL_BUFFER_BIT);
2061   const struct gl_framebuffer *readFb, *drawFb;
2062   const struct gl_renderbuffer *colorReadRb, *colorDrawRb;
2063   GET_CURRENT_CONTEXT(ctx);
2064
2065   ASSERT_OUTSIDE_BEGIN_END(ctx);
2066   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2067
2068   if (ctx->NewState) {
2069      _mesa_update_state(ctx);
2070   }
2071
2072   readFb = ctx->ReadBuffer;
2073   drawFb = ctx->DrawBuffer;
2074
2075   if (!readFb || !drawFb) {
2076      /* This will normally never happen but someday we may want to
2077       * support MakeCurrent() with no drawables.
2078       */
2079      return;
2080   }
2081
2082   /* check for complete framebuffers */
2083   if (drawFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
2084       readFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2085      _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2086                  "glBlitFramebufferEXT(incomplete draw/read buffers)");
2087      return;
2088   }
2089
2090   if (filter != GL_NEAREST && filter != GL_LINEAR) {
2091      _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(filter)");
2092      return;
2093   }
2094
2095   if (mask & ~legalMaskBits) {
2096      _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
2097      return;
2098   }
2099
2100   /* depth/stencil must be blitted with nearest filtering */
2101   if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
2102        && filter != GL_NEAREST) {
2103      _mesa_error(ctx, GL_INVALID_OPERATION,
2104             "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
2105      return;
2106   }
2107
2108   /* get color read/draw renderbuffers */
2109   if (mask & GL_COLOR_BUFFER_BIT) {
2110      colorReadRb = readFb->_ColorReadBuffer;
2111      colorDrawRb = drawFb->_ColorDrawBuffers[0];
2112   }
2113   else {
2114      colorReadRb = colorDrawRb = NULL;
2115   }
2116
2117   if (mask & GL_STENCIL_BUFFER_BIT) {
2118      struct gl_renderbuffer *readRb = readFb->_StencilBuffer;
2119      struct gl_renderbuffer *drawRb = drawFb->_StencilBuffer;
2120      if (!readRb ||
2121          !drawRb ||
2122          _mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS) !=
2123          _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS)) {
2124         _mesa_error(ctx, GL_INVALID_OPERATION,
2125                     "glBlitFramebufferEXT(stencil buffer size mismatch");
2126         return;
2127      }
2128   }
2129
2130   if (mask & GL_DEPTH_BUFFER_BIT) {
2131      struct gl_renderbuffer *readRb = readFb->_DepthBuffer;
2132      struct gl_renderbuffer *drawRb = drawFb->_DepthBuffer;
2133      if (!readRb ||
2134          !drawRb ||
2135          _mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS) !=
2136          _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS)) {
2137         _mesa_error(ctx, GL_INVALID_OPERATION,
2138                     "glBlitFramebufferEXT(depth buffer size mismatch");
2139         return;
2140      }
2141   }
2142
2143   if (readFb->Visual.samples > 0 &&
2144       drawFb->Visual.samples > 0 &&
2145       readFb->Visual.samples != drawFb->Visual.samples) {
2146      _mesa_error(ctx, GL_INVALID_OPERATION,
2147                  "glBlitFramebufferEXT(mismatched samples");
2148      return;
2149   }
2150
2151   /* extra checks for multisample copies... */
2152   if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
2153      /* src and dest region sizes must be the same */
2154      if (srcX1 - srcX0 != dstX1 - dstX0 ||
2155          srcY1 - srcY0 != dstY1 - dstY0) {
2156         _mesa_error(ctx, GL_INVALID_OPERATION,
2157                "glBlitFramebufferEXT(bad src/dst multisample region sizes");
2158         return;
2159      }
2160
2161      /* color formats must match */
2162      if (colorReadRb &&
2163          colorDrawRb &&
2164          colorReadRb->Format != colorDrawRb->Format) {
2165         _mesa_error(ctx, GL_INVALID_OPERATION,
2166                "glBlitFramebufferEXT(bad src/dst multisample pixel formats");
2167         return;
2168      }
2169   }
2170
2171   if (!ctx->Extensions.EXT_framebuffer_blit) {
2172      _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
2173      return;
2174   }
2175
2176   /* Debug code */
2177   if (DEBUG_BLIT) {
2178      printf("glBlitFramebuffer(%d, %d, %d, %d,  %d, %d, %d, %d,"
2179	     " 0x%x, 0x%x)\n",
2180	     srcX0, srcY0, srcX1, srcY1,
2181	     dstX0, dstY0, dstX1, dstY1,
2182	     mask, filter);
2183      if (colorReadRb) {
2184         const struct gl_renderbuffer_attachment *att;
2185
2186         att = find_attachment(readFb, colorReadRb);
2187         printf("  Src FBO %u  RB %u (%dx%d)  ",
2188		readFb->Name, colorReadRb->Name,
2189		colorReadRb->Width, colorReadRb->Height);
2190         if (att && att->Texture) {
2191            printf("Tex %u  tgt 0x%x  level %u  face %u",
2192		   att->Texture->Name,
2193		   att->Texture->Target,
2194		   att->TextureLevel,
2195		   att->CubeMapFace);
2196         }
2197         printf("\n");
2198
2199         att = find_attachment(drawFb, colorDrawRb);
2200         printf("  Dst FBO %u  RB %u (%dx%d)  ",
2201		drawFb->Name, colorDrawRb->Name,
2202		colorDrawRb->Width, colorDrawRb->Height);
2203         if (att && att->Texture) {
2204            printf("Tex %u  tgt 0x%x  level %u  face %u",
2205		   att->Texture->Name,
2206		   att->Texture->Target,
2207		   att->TextureLevel,
2208		   att->CubeMapFace);
2209         }
2210         printf("\n");
2211      }
2212   }
2213
2214   ASSERT(ctx->Driver.BlitFramebuffer);
2215   ctx->Driver.BlitFramebuffer(ctx,
2216                               srcX0, srcY0, srcX1, srcY1,
2217                               dstX0, dstY0, dstX1, dstY1,
2218                               mask, filter);
2219}
2220#endif /* FEATURE_EXT_framebuffer_blit */
2221