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