2376e7f1a54c28cfda1f2cbca94ec8b3efefc586
[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 "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 */
71 static struct gl_framebuffer DummyFramebuffer;
72 static 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
80 static void
81 delete_dummy_renderbuffer(struct gl_renderbuffer *rb)
82 {
83 /* no op */
84 }
85
86 static void
87 delete_dummy_framebuffer(struct gl_framebuffer *fb)
88 {
89 /* no op */
90 }
91
92
93 void
94 _mesa_init_fbobjects(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 */
104 struct 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 */
121 struct 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 */
140 static void
141 invalidate_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 */
153 struct 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 */
201 void
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 */
227 void
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 */
268 void
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 */
286 void
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 */
318 static void
319 att_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 */
332 static void
333 fbo_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 */
352 static void
353 test_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 */
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 #if !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
697 GLboolean 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
711 void 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 */
768 static void
769 detach_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
783 void 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
827 void 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 */
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 case GL_RGBA16_SNORM:
891 return GL_RGBA;
892 case GL_STENCIL_INDEX:
893 case GL_STENCIL_INDEX1_EXT:
894 case GL_STENCIL_INDEX4_EXT:
895 case GL_STENCIL_INDEX8_EXT:
896 case GL_STENCIL_INDEX16_EXT:
897 return GL_STENCIL_INDEX;
898 case GL_DEPTH_COMPONENT:
899 case GL_DEPTH_COMPONENT16:
900 case GL_DEPTH_COMPONENT24:
901 case GL_DEPTH_COMPONENT32:
902 return GL_DEPTH_COMPONENT;
903 case GL_DEPTH_STENCIL_EXT:
904 case GL_DEPTH24_STENCIL8_EXT:
905 if (ctx->Extensions.EXT_packed_depth_stencil)
906 return GL_DEPTH_STENCIL_EXT;
907 else
908 return 0;
909 /* XXX add floating point formats eventually */
910 default:
911 return 0;
912 }
913 }
914
915
916 /** sentinal value, see below */
917 #define NO_SAMPLES 1000
918
919
920 /**
921 * Helper function used by _mesa_RenderbufferStorageEXT() and
922 * _mesa_RenderbufferStorageMultisample().
923 * samples will be NO_SAMPLES if called by _mesa_RenderbufferStorageEXT().
924 */
925 static void
926 renderbuffer_storage(GLenum target, GLenum internalFormat,
927 GLsizei width, GLsizei height, GLsizei samples)
928 {
929 const char *func = samples == NO_SAMPLES ?
930 "glRenderbufferStorage" : "RenderbufferStorageMultisample";
931 struct gl_renderbuffer *rb;
932 GLenum baseFormat;
933 GET_CURRENT_CONTEXT(ctx);
934
935 ASSERT_OUTSIDE_BEGIN_END(ctx);
936
937 if (target != GL_RENDERBUFFER_EXT) {
938 _mesa_error(ctx, GL_INVALID_ENUM, "%s(target)", func);
939 return;
940 }
941
942 baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
943 if (baseFormat == 0) {
944 _mesa_error(ctx, GL_INVALID_ENUM, "%s(internalFormat)", func);
945 return;
946 }
947
948 if (width < 1 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
949 _mesa_error(ctx, GL_INVALID_VALUE, "%s(width)", func);
950 return;
951 }
952
953 if (height < 1 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
954 _mesa_error(ctx, GL_INVALID_VALUE, "%s(height)", func);
955 return;
956 }
957
958 if (samples == NO_SAMPLES) {
959 /* NumSamples == 0 indicates non-multisampling */
960 samples = 0;
961 }
962 else if (samples > (GLsizei) ctx->Const.MaxSamples) {
963 /* note: driver may choose to use more samples than what's requested */
964 _mesa_error(ctx, GL_INVALID_VALUE, "%s(samples)", func);
965 return;
966 }
967
968 rb = ctx->CurrentRenderbuffer;
969 if (!rb) {
970 _mesa_error(ctx, GL_INVALID_OPERATION, func);
971 return;
972 }
973
974 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
975
976 if (rb->InternalFormat == internalFormat &&
977 rb->Width == (GLuint) width &&
978 rb->Height == (GLuint) height) {
979 /* no change in allocation needed */
980 return;
981 }
982
983 /* These MUST get set by the AllocStorage func */
984 rb->Format = MESA_FORMAT_NONE;
985 rb->NumSamples = samples;
986
987 /* Now allocate the storage */
988 ASSERT(rb->AllocStorage);
989 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
990 /* No error - check/set fields now */
991 assert(rb->Format != MESA_FORMAT_NONE);
992 assert(rb->Width == (GLuint) width);
993 assert(rb->Height == (GLuint) height);
994 rb->InternalFormat = internalFormat;
995 rb->_BaseFormat = baseFormat;
996 assert(rb->_BaseFormat != 0);
997 }
998 else {
999 /* Probably ran out of memory - clear the fields */
1000 rb->Width = 0;
1001 rb->Height = 0;
1002 rb->Format = MESA_FORMAT_NONE;
1003 rb->InternalFormat = GL_NONE;
1004 rb->_BaseFormat = GL_NONE;
1005 rb->NumSamples = 0;
1006 }
1007
1008 /*
1009 test_framebuffer_completeness(ctx, fb);
1010 */
1011 /* XXX if this renderbuffer is attached anywhere, invalidate attachment
1012 * points???
1013 */
1014 }
1015
1016 #if FEATURE_OES_EGL_image
1017 void GLAPIENTRY
1018 _mesa_EGLImageTargetRenderbufferStorageOES (GLenum target, GLeglImageOES image)
1019 {
1020 struct gl_renderbuffer *rb;
1021 GET_CURRENT_CONTEXT(ctx);
1022 ASSERT_OUTSIDE_BEGIN_END(ctx);
1023
1024 if (!ctx->Extensions.OES_EGL_image) {
1025 _mesa_error(ctx, GL_INVALID_OPERATION,
1026 "glEGLImageTargetRenderbufferStorageOES(unsupported)");
1027 return;
1028 }
1029
1030 if (target != GL_RENDERBUFFER) {
1031 _mesa_error(ctx, GL_INVALID_ENUM, "EGLImageTargetRenderbufferStorageOES");
1032 return;
1033 }
1034
1035 rb = ctx->CurrentRenderbuffer;
1036 if (!rb) {
1037 _mesa_error(ctx, GL_INVALID_OPERATION, "EGLImageTargetRenderbufferStorageOES");
1038 return;
1039 }
1040
1041 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1042
1043 ctx->Driver.EGLImageTargetRenderbufferStorage(ctx, rb, image);
1044 }
1045 #endif
1046
1047 /**
1048 * Helper function for _mesa_GetRenderbufferParameterivEXT() and
1049 * _mesa_GetFramebufferAttachmentParameterivEXT()
1050 * We have to be careful to respect the base format. For example, if a
1051 * renderbuffer/texture was created with internalFormat=GL_RGB but the
1052 * driver actually chose a GL_RGBA format, when the user queries ALPHA_SIZE
1053 * we need to return zero.
1054 */
1055 static GLint
1056 get_component_bits(GLenum pname, GLenum baseFormat, gl_format format)
1057 {
1058 switch (pname) {
1059 case GL_RENDERBUFFER_RED_SIZE_EXT:
1060 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
1061 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1062 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
1063 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1064 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
1065 if (baseFormat == GL_RGB || baseFormat == GL_RGBA)
1066 return _mesa_get_format_bits(format, pname);
1067 else
1068 return 0;
1069 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1070 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
1071 if (baseFormat == GL_RGBA || baseFormat == GL_ALPHA)
1072 return _mesa_get_format_bits(format, pname);
1073 else
1074 return 0;
1075 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1076 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
1077 if (baseFormat == GL_DEPTH_COMPONENT || baseFormat == GL_DEPTH_STENCIL)
1078 return _mesa_get_format_bits(format, pname);
1079 else
1080 return 0;
1081 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1082 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
1083 if (baseFormat == GL_STENCIL_INDEX || baseFormat == GL_DEPTH_STENCIL)
1084 return _mesa_get_format_bits(format, pname);
1085 else
1086 return 0;
1087 default:
1088 return 0;
1089 }
1090 }
1091
1092
1093
1094 void GLAPIENTRY
1095 _mesa_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1096 GLsizei width, GLsizei height)
1097 {
1098 /* GL_ARB_fbo says calling this function is equivalent to calling
1099 * glRenderbufferStorageMultisample() with samples=0. We pass in
1100 * a token value here just for error reporting purposes.
1101 */
1102 renderbuffer_storage(target, internalFormat, width, height, NO_SAMPLES);
1103 }
1104
1105
1106 void GLAPIENTRY
1107 _mesa_RenderbufferStorageMultisample(GLenum target, GLsizei samples,
1108 GLenum internalFormat,
1109 GLsizei width, GLsizei height)
1110 {
1111 renderbuffer_storage(target, internalFormat, width, height, samples);
1112 }
1113
1114 void GLAPIENTRY
1115 _es_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
1116 GLsizei width, GLsizei height)
1117 {
1118 switch (internalFormat) {
1119 case GL_RGB565:
1120 /* XXX this confuses GL_RENDERBUFFER_INTERNAL_FORMAT_OES */
1121 /* choose a closest format */
1122 internalFormat = GL_RGB5;
1123 break;
1124 default:
1125 break;
1126 }
1127
1128 renderbuffer_storage(target, internalFormat, width, height, 0);
1129 }
1130
1131 void GLAPIENTRY
1132 _mesa_GetRenderbufferParameterivEXT(GLenum target, GLenum pname, GLint *params)
1133 {
1134 struct gl_renderbuffer *rb;
1135 GET_CURRENT_CONTEXT(ctx);
1136
1137 ASSERT_OUTSIDE_BEGIN_END(ctx);
1138
1139 if (target != GL_RENDERBUFFER_EXT) {
1140 _mesa_error(ctx, GL_INVALID_ENUM,
1141 "glGetRenderbufferParameterivEXT(target)");
1142 return;
1143 }
1144
1145 rb = ctx->CurrentRenderbuffer;
1146 if (!rb) {
1147 _mesa_error(ctx, GL_INVALID_OPERATION,
1148 "glGetRenderbufferParameterivEXT");
1149 return;
1150 }
1151
1152 /* No need to flush here since we're just quering state which is
1153 * not effected by rendering.
1154 */
1155
1156 switch (pname) {
1157 case GL_RENDERBUFFER_WIDTH_EXT:
1158 *params = rb->Width;
1159 return;
1160 case GL_RENDERBUFFER_HEIGHT_EXT:
1161 *params = rb->Height;
1162 return;
1163 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
1164 *params = rb->InternalFormat;
1165 return;
1166 case GL_RENDERBUFFER_RED_SIZE_EXT:
1167 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
1168 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
1169 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
1170 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
1171 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
1172 *params = get_component_bits(pname, rb->_BaseFormat, rb->Format);
1173 break;
1174 case GL_RENDERBUFFER_SAMPLES:
1175 if (ctx->Extensions.ARB_framebuffer_object) {
1176 *params = rb->NumSamples;
1177 break;
1178 }
1179 /* fallthrough */
1180 default:
1181 _mesa_error(ctx, GL_INVALID_ENUM,
1182 "glGetRenderbufferParameterivEXT(target)");
1183 return;
1184 }
1185 }
1186
1187
1188 GLboolean GLAPIENTRY
1189 _mesa_IsFramebufferEXT(GLuint framebuffer)
1190 {
1191 GET_CURRENT_CONTEXT(ctx);
1192 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
1193 if (framebuffer) {
1194 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
1195 if (rb != NULL && rb != &DummyFramebuffer)
1196 return GL_TRUE;
1197 }
1198 return GL_FALSE;
1199 }
1200
1201
1202 /**
1203 * Check if any of the attachments of the given framebuffer are textures
1204 * (render to texture). Call ctx->Driver.RenderTexture() for such
1205 * attachments.
1206 */
1207 static void
1208 check_begin_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
1209 {
1210 GLuint i;
1211 ASSERT(ctx->Driver.RenderTexture);
1212
1213 if (fb->Name == 0)
1214 return; /* can't render to texture with winsys framebuffers */
1215
1216 for (i = 0; i < BUFFER_COUNT; i++) {
1217 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1218 struct gl_texture_object *texObj = att->Texture;
1219 if (texObj
1220 && texObj->Image[att->CubeMapFace][att->TextureLevel]) {
1221 ctx->Driver.RenderTexture(ctx, fb, att);
1222 }
1223 }
1224 }
1225
1226
1227 /**
1228 * Examine all the framebuffer's attachments to see if any are textures.
1229 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
1230 * notify the device driver that the texture image may have changed.
1231 */
1232 static void
1233 check_end_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
1234 {
1235 if (fb->Name == 0)
1236 return; /* can't render to texture with winsys framebuffers */
1237
1238 if (ctx->Driver.FinishRenderTexture) {
1239 GLuint i;
1240 for (i = 0; i < BUFFER_COUNT; i++) {
1241 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
1242 if (att->Texture && att->Renderbuffer) {
1243 ctx->Driver.FinishRenderTexture(ctx, att);
1244 }
1245 }
1246 }
1247 }
1248
1249
1250 void GLAPIENTRY
1251 _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
1252 {
1253 struct gl_framebuffer *newDrawFb, *newReadFb;
1254 struct gl_framebuffer *oldDrawFb, *oldReadFb;
1255 GLboolean bindReadBuf, bindDrawBuf;
1256 GET_CURRENT_CONTEXT(ctx);
1257
1258 #ifdef DEBUG
1259 if (ctx->Extensions.ARB_framebuffer_object) {
1260 ASSERT(ctx->Extensions.EXT_framebuffer_object);
1261 ASSERT(ctx->Extensions.EXT_framebuffer_blit);
1262 }
1263 #endif
1264
1265 ASSERT_OUTSIDE_BEGIN_END(ctx);
1266
1267 if (!ctx->Extensions.EXT_framebuffer_object) {
1268 _mesa_error(ctx, GL_INVALID_OPERATION,
1269 "glBindFramebufferEXT(unsupported)");
1270 return;
1271 }
1272
1273 switch (target) {
1274 #if FEATURE_EXT_framebuffer_blit
1275 case GL_DRAW_FRAMEBUFFER_EXT:
1276 if (!ctx->Extensions.EXT_framebuffer_blit) {
1277 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1278 return;
1279 }
1280 bindDrawBuf = GL_TRUE;
1281 bindReadBuf = GL_FALSE;
1282 break;
1283 case GL_READ_FRAMEBUFFER_EXT:
1284 if (!ctx->Extensions.EXT_framebuffer_blit) {
1285 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1286 return;
1287 }
1288 bindDrawBuf = GL_FALSE;
1289 bindReadBuf = GL_TRUE;
1290 break;
1291 #endif
1292 case GL_FRAMEBUFFER_EXT:
1293 bindDrawBuf = GL_TRUE;
1294 bindReadBuf = GL_TRUE;
1295 break;
1296 default:
1297 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
1298 return;
1299 }
1300
1301 if (framebuffer) {
1302 /* Binding a user-created framebuffer object */
1303 newDrawFb = _mesa_lookup_framebuffer(ctx, framebuffer);
1304 if (newDrawFb == &DummyFramebuffer) {
1305 /* ID was reserved, but no real framebuffer object made yet */
1306 newDrawFb = NULL;
1307 }
1308 else if (!newDrawFb && ctx->Extensions.ARB_framebuffer_object) {
1309 /* All FBO IDs must be Gen'd */
1310 _mesa_error(ctx, GL_INVALID_OPERATION, "glBindFramebuffer(buffer)");
1311 return;
1312 }
1313
1314 if (!newDrawFb) {
1315 /* create new framebuffer object */
1316 newDrawFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
1317 if (!newDrawFb) {
1318 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
1319 return;
1320 }
1321 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newDrawFb);
1322 }
1323 newReadFb = newDrawFb;
1324 }
1325 else {
1326 /* Binding the window system framebuffer (which was originally set
1327 * with MakeCurrent).
1328 */
1329 newDrawFb = ctx->WinSysDrawBuffer;
1330 newReadFb = ctx->WinSysReadBuffer;
1331 }
1332
1333 ASSERT(newDrawFb);
1334 ASSERT(newDrawFb != &DummyFramebuffer);
1335
1336 /* save pointers to current/old framebuffers */
1337 oldDrawFb = ctx->DrawBuffer;
1338 oldReadFb = ctx->ReadBuffer;
1339
1340 /* check if really changing bindings */
1341 if (oldDrawFb == newDrawFb)
1342 bindDrawBuf = GL_FALSE;
1343 if (oldReadFb == newReadFb)
1344 bindReadBuf = GL_FALSE;
1345
1346 /*
1347 * OK, now bind the new Draw/Read framebuffers, if they're changing.
1348 *
1349 * We also check if we're beginning and/or ending render-to-texture.
1350 * When a framebuffer with texture attachments is unbound, call
1351 * ctx->Driver.FinishRenderTexture().
1352 * When a framebuffer with texture attachments is bound, call
1353 * ctx->Driver.RenderTexture().
1354 *
1355 * Note that if the ReadBuffer has texture attachments we don't consider
1356 * that a render-to-texture case.
1357 */
1358 if (bindReadBuf) {
1359 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1360
1361 /* check if old readbuffer was render-to-texture */
1362 check_end_texture_render(ctx, oldReadFb);
1363
1364 _mesa_reference_framebuffer(&ctx->ReadBuffer, newReadFb);
1365 }
1366
1367 if (bindDrawBuf) {
1368 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1369
1370 /* check if old read/draw buffers were render-to-texture */
1371 if (!bindReadBuf)
1372 check_end_texture_render(ctx, oldReadFb);
1373
1374 if (oldDrawFb != oldReadFb)
1375 check_end_texture_render(ctx, oldDrawFb);
1376
1377 /* check if newly bound framebuffer has any texture attachments */
1378 check_begin_texture_render(ctx, newDrawFb);
1379
1380 _mesa_reference_framebuffer(&ctx->DrawBuffer, newDrawFb);
1381 }
1382
1383 if ((bindDrawBuf || bindReadBuf) && ctx->Driver.BindFramebuffer) {
1384 ctx->Driver.BindFramebuffer(ctx, target, newDrawFb, newReadFb);
1385 }
1386 }
1387
1388
1389 void GLAPIENTRY
1390 _mesa_DeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
1391 {
1392 GLint i;
1393 GET_CURRENT_CONTEXT(ctx);
1394
1395 ASSERT_OUTSIDE_BEGIN_END(ctx);
1396 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1397
1398 for (i = 0; i < n; i++) {
1399 if (framebuffers[i] > 0) {
1400 struct gl_framebuffer *fb;
1401 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
1402 if (fb) {
1403 ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
1404
1405 /* check if deleting currently bound framebuffer object */
1406 if (ctx->Extensions.EXT_framebuffer_blit) {
1407 /* separate draw/read binding points */
1408 if (fb == ctx->DrawBuffer) {
1409 /* bind default */
1410 ASSERT(fb->RefCount >= 2);
1411 _mesa_BindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, 0);
1412 }
1413 if (fb == ctx->ReadBuffer) {
1414 /* bind default */
1415 ASSERT(fb->RefCount >= 2);
1416 _mesa_BindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, 0);
1417 }
1418 }
1419 else {
1420 /* only one binding point for read/draw buffers */
1421 if (fb == ctx->DrawBuffer || fb == ctx->ReadBuffer) {
1422 /* bind default */
1423 ASSERT(fb->RefCount >= 2);
1424 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
1425 }
1426 }
1427
1428 /* remove from hash table immediately, to free the ID */
1429 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
1430
1431 if (fb != &DummyFramebuffer) {
1432 /* But the object will not be freed until it's no longer
1433 * bound in any context.
1434 */
1435 _mesa_reference_framebuffer(&fb, NULL);
1436 }
1437 }
1438 }
1439 }
1440 }
1441
1442
1443 void GLAPIENTRY
1444 _mesa_GenFramebuffersEXT(GLsizei n, GLuint *framebuffers)
1445 {
1446 GET_CURRENT_CONTEXT(ctx);
1447 GLuint first;
1448 GLint i;
1449
1450 ASSERT_OUTSIDE_BEGIN_END(ctx);
1451
1452 if (n < 0) {
1453 _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
1454 return;
1455 }
1456
1457 if (!framebuffers)
1458 return;
1459
1460 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
1461
1462 for (i = 0; i < n; i++) {
1463 GLuint name = first + i;
1464 framebuffers[i] = name;
1465 /* insert dummy placeholder into hash table */
1466 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1467 _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
1468 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1469 }
1470 }
1471
1472
1473
1474 GLenum GLAPIENTRY
1475 _mesa_CheckFramebufferStatusEXT(GLenum target)
1476 {
1477 struct gl_framebuffer *buffer;
1478 GET_CURRENT_CONTEXT(ctx);
1479
1480 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1481
1482 switch (target) {
1483 #if FEATURE_EXT_framebuffer_blit
1484 case GL_DRAW_FRAMEBUFFER_EXT:
1485 if (!ctx->Extensions.EXT_framebuffer_blit) {
1486 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1487 return 0;
1488 }
1489 buffer = ctx->DrawBuffer;
1490 break;
1491 case GL_READ_FRAMEBUFFER_EXT:
1492 if (!ctx->Extensions.EXT_framebuffer_blit) {
1493 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1494 return 0;
1495 }
1496 buffer = ctx->ReadBuffer;
1497 break;
1498 #endif
1499 case GL_FRAMEBUFFER_EXT:
1500 buffer = ctx->DrawBuffer;
1501 break;
1502 default:
1503 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1504 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1505 }
1506
1507 if (buffer->Name == 0) {
1508 /* The window system / default framebuffer is always complete */
1509 return GL_FRAMEBUFFER_COMPLETE_EXT;
1510 }
1511
1512 /* No need to flush here */
1513
1514 if (buffer->_Status != GL_FRAMEBUFFER_COMPLETE) {
1515 _mesa_test_framebuffer_completeness(ctx, buffer);
1516 }
1517
1518 return buffer->_Status;
1519 }
1520
1521
1522
1523 /**
1524 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1525 */
1526 static void
1527 framebuffer_texture(GLcontext *ctx, const char *caller, GLenum target,
1528 GLenum attachment, GLenum textarget, GLuint texture,
1529 GLint level, GLint zoffset)
1530 {
1531 struct gl_renderbuffer_attachment *att;
1532 struct gl_texture_object *texObj = NULL;
1533 struct gl_framebuffer *fb;
1534 GLboolean error = GL_FALSE;
1535
1536 ASSERT_OUTSIDE_BEGIN_END(ctx);
1537
1538 switch (target) {
1539 case GL_READ_FRAMEBUFFER_EXT:
1540 error = !ctx->Extensions.EXT_framebuffer_blit;
1541 fb = ctx->ReadBuffer;
1542 break;
1543 case GL_DRAW_FRAMEBUFFER_EXT:
1544 error = !ctx->Extensions.EXT_framebuffer_blit;
1545 /* fall-through */
1546 case GL_FRAMEBUFFER_EXT:
1547 fb = ctx->DrawBuffer;
1548 break;
1549 default:
1550 error = GL_TRUE;
1551 }
1552
1553 if (error) {
1554 _mesa_error(ctx, GL_INVALID_ENUM,
1555 "glFramebufferTexture%sEXT(target=0x%x)", caller, target);
1556 return;
1557 }
1558
1559 ASSERT(fb);
1560
1561 /* check framebuffer binding */
1562 if (fb->Name == 0) {
1563 _mesa_error(ctx, GL_INVALID_OPERATION,
1564 "glFramebufferTexture%sEXT", caller);
1565 return;
1566 }
1567
1568
1569 /* The textarget, level, and zoffset parameters are only validated if
1570 * texture is non-zero.
1571 */
1572 if (texture) {
1573 GLboolean err = GL_TRUE;
1574
1575 texObj = _mesa_lookup_texture(ctx, texture);
1576 if (texObj != NULL) {
1577 if (textarget == 0) {
1578 /* XXX what's the purpose of this? */
1579 err = (texObj->Target != GL_TEXTURE_3D) &&
1580 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
1581 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT);
1582 }
1583 else {
1584 err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
1585 ? !IS_CUBE_FACE(textarget)
1586 : (texObj->Target != textarget);
1587 }
1588 }
1589 else {
1590 /* can't render to a non-existant texture */
1591 _mesa_error(ctx, GL_INVALID_OPERATION,
1592 "glFramebufferTexture%sEXT(non existant texture)",
1593 caller);
1594 return;
1595 }
1596
1597 if (err) {
1598 _mesa_error(ctx, GL_INVALID_OPERATION,
1599 "glFramebufferTexture%sEXT(texture target mismatch)",
1600 caller);
1601 return;
1602 }
1603
1604 if (texObj->Target == GL_TEXTURE_3D) {
1605 const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1606 if (zoffset < 0 || zoffset >= maxSize) {
1607 _mesa_error(ctx, GL_INVALID_VALUE,
1608 "glFramebufferTexture%sEXT(zoffset)", caller);
1609 return;
1610 }
1611 }
1612 else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
1613 (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
1614 if (zoffset < 0 || zoffset >= ctx->Const.MaxArrayTextureLayers) {
1615 _mesa_error(ctx, GL_INVALID_VALUE,
1616 "glFramebufferTexture%sEXT(layer)", caller);
1617 return;
1618 }
1619 }
1620
1621 if ((level < 0) ||
1622 (level >= _mesa_max_texture_levels(ctx, texObj->Target))) {
1623 _mesa_error(ctx, GL_INVALID_VALUE,
1624 "glFramebufferTexture%sEXT(level)", caller);
1625 return;
1626 }
1627 }
1628
1629 att = _mesa_get_attachment(ctx, fb, attachment);
1630 if (att == NULL) {
1631 _mesa_error(ctx, GL_INVALID_ENUM,
1632 "glFramebufferTexture%sEXT(attachment)", caller);
1633 return;
1634 }
1635
1636 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1637
1638 _glthread_LOCK_MUTEX(fb->Mutex);
1639 if (texObj) {
1640 _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
1641 level, zoffset);
1642 /* Set the render-to-texture flag. We'll check this flag in
1643 * glTexImage() and friends to determine if we need to revalidate
1644 * any FBOs that might be rendering into this texture.
1645 * This flag never gets cleared since it's non-trivial to determine
1646 * when all FBOs might be done rendering to this texture. That's OK
1647 * though since it's uncommon to render to a texture then repeatedly
1648 * call glTexImage() to change images in the texture.
1649 */
1650 texObj->_RenderToTexture = GL_TRUE;
1651 }
1652 else {
1653 _mesa_remove_attachment(ctx, att);
1654 }
1655
1656 invalidate_framebuffer(fb);
1657
1658 _glthread_UNLOCK_MUTEX(fb->Mutex);
1659 }
1660
1661
1662
1663 void GLAPIENTRY
1664 _mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
1665 GLenum textarget, GLuint texture, GLint level)
1666 {
1667 GET_CURRENT_CONTEXT(ctx);
1668
1669 if ((texture != 0) && (textarget != GL_TEXTURE_1D)) {
1670 _mesa_error(ctx, GL_INVALID_ENUM,
1671 "glFramebufferTexture1DEXT(textarget)");
1672 return;
1673 }
1674
1675 framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
1676 level, 0);
1677 }
1678
1679
1680 void GLAPIENTRY
1681 _mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
1682 GLenum textarget, GLuint texture, GLint level)
1683 {
1684 GET_CURRENT_CONTEXT(ctx);
1685
1686 if ((texture != 0) &&
1687 (textarget != GL_TEXTURE_2D) &&
1688 (textarget != GL_TEXTURE_RECTANGLE_ARB) &&
1689 (!IS_CUBE_FACE(textarget))) {
1690 _mesa_error(ctx, GL_INVALID_OPERATION,
1691 "glFramebufferTexture2DEXT(textarget=0x%x)", textarget);
1692 return;
1693 }
1694
1695 framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
1696 level, 0);
1697 }
1698
1699
1700 void GLAPIENTRY
1701 _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
1702 GLenum textarget, GLuint texture,
1703 GLint level, GLint zoffset)
1704 {
1705 GET_CURRENT_CONTEXT(ctx);
1706
1707 if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
1708 _mesa_error(ctx, GL_INVALID_ENUM,
1709 "glFramebufferTexture3DEXT(textarget)");
1710 return;
1711 }
1712
1713 framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
1714 level, zoffset);
1715 }
1716
1717
1718 void GLAPIENTRY
1719 _mesa_FramebufferTextureLayerEXT(GLenum target, GLenum attachment,
1720 GLuint texture, GLint level, GLint layer)
1721 {
1722 GET_CURRENT_CONTEXT(ctx);
1723
1724 framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
1725 level, layer);
1726 }
1727
1728
1729 void GLAPIENTRY
1730 _mesa_FramebufferRenderbufferEXT(GLenum target, GLenum attachment,
1731 GLenum renderbufferTarget,
1732 GLuint renderbuffer)
1733 {
1734 struct gl_renderbuffer_attachment *att;
1735 struct gl_framebuffer *fb;
1736 struct gl_renderbuffer *rb;
1737 GET_CURRENT_CONTEXT(ctx);
1738
1739 ASSERT_OUTSIDE_BEGIN_END(ctx);
1740
1741 switch (target) {
1742 #if FEATURE_EXT_framebuffer_blit
1743 case GL_DRAW_FRAMEBUFFER_EXT:
1744 if (!ctx->Extensions.EXT_framebuffer_blit) {
1745 _mesa_error(ctx, GL_INVALID_ENUM,
1746 "glFramebufferRenderbufferEXT(target)");
1747 return;
1748 }
1749 fb = ctx->DrawBuffer;
1750 break;
1751 case GL_READ_FRAMEBUFFER_EXT:
1752 if (!ctx->Extensions.EXT_framebuffer_blit) {
1753 _mesa_error(ctx, GL_INVALID_ENUM,
1754 "glFramebufferRenderbufferEXT(target)");
1755 return;
1756 }
1757 fb = ctx->ReadBuffer;
1758 break;
1759 #endif
1760 case GL_FRAMEBUFFER_EXT:
1761 fb = ctx->DrawBuffer;
1762 break;
1763 default:
1764 _mesa_error(ctx, GL_INVALID_ENUM,
1765 "glFramebufferRenderbufferEXT(target)");
1766 return;
1767 }
1768
1769 if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
1770 _mesa_error(ctx, GL_INVALID_ENUM,
1771 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1772 return;
1773 }
1774
1775 if (fb->Name == 0) {
1776 /* Can't attach new renderbuffers to a window system framebuffer */
1777 _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
1778 return;
1779 }
1780
1781 att = _mesa_get_attachment(ctx, fb, attachment);
1782 if (att == NULL) {
1783 _mesa_error(ctx, GL_INVALID_ENUM,
1784 "glFramebufferRenderbufferEXT(invalid attachment %s)",
1785 _mesa_lookup_enum_by_nr(attachment));
1786 return;
1787 }
1788
1789 if (renderbuffer) {
1790 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
1791 if (!rb) {
1792 _mesa_error(ctx, GL_INVALID_OPERATION,
1793 "glFramebufferRenderbufferEXT(non-existant"
1794 " renderbuffer %u)", renderbuffer);
1795 return;
1796 }
1797 }
1798 else {
1799 /* remove renderbuffer attachment */
1800 rb = NULL;
1801 }
1802
1803 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1804 /* make sure the renderbuffer is a depth/stencil format */
1805 const GLenum baseFormat =
1806 _mesa_get_format_base_format(att->Renderbuffer->Format);
1807 if (baseFormat != GL_DEPTH_STENCIL) {
1808 _mesa_error(ctx, GL_INVALID_OPERATION,
1809 "glFramebufferRenderbufferEXT(renderbuffer"
1810 " is not DEPTH_STENCIL format)");
1811 return;
1812 }
1813 }
1814
1815
1816 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1817
1818 assert(ctx->Driver.FramebufferRenderbuffer);
1819 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
1820
1821 /* Some subsequent GL commands may depend on the framebuffer's visual
1822 * after the binding is updated. Update visual info now.
1823 */
1824 _mesa_update_framebuffer_visual(fb);
1825 }
1826
1827
1828 void GLAPIENTRY
1829 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment,
1830 GLenum pname, GLint *params)
1831 {
1832 const struct gl_renderbuffer_attachment *att;
1833 struct gl_framebuffer *buffer;
1834 GET_CURRENT_CONTEXT(ctx);
1835
1836 ASSERT_OUTSIDE_BEGIN_END(ctx);
1837
1838 switch (target) {
1839 #if FEATURE_EXT_framebuffer_blit
1840 case GL_DRAW_FRAMEBUFFER_EXT:
1841 if (!ctx->Extensions.EXT_framebuffer_blit) {
1842 _mesa_error(ctx, GL_INVALID_ENUM,
1843 "glGetFramebufferAttachmentParameterivEXT(target)");
1844 return;
1845 }
1846 buffer = ctx->DrawBuffer;
1847 break;
1848 case GL_READ_FRAMEBUFFER_EXT:
1849 if (!ctx->Extensions.EXT_framebuffer_blit) {
1850 _mesa_error(ctx, GL_INVALID_ENUM,
1851 "glGetFramebufferAttachmentParameterivEXT(target)");
1852 return;
1853 }
1854 buffer = ctx->ReadBuffer;
1855 break;
1856 #endif
1857 case GL_FRAMEBUFFER_EXT:
1858 buffer = ctx->DrawBuffer;
1859 break;
1860 default:
1861 _mesa_error(ctx, GL_INVALID_ENUM,
1862 "glGetFramebufferAttachmentParameterivEXT(target)");
1863 return;
1864 }
1865
1866 if (buffer->Name == 0) {
1867 _mesa_error(ctx, GL_INVALID_OPERATION,
1868 "glGetFramebufferAttachmentParameterivEXT");
1869 return;
1870 }
1871
1872 att = _mesa_get_attachment(ctx, buffer, attachment);
1873 if (att == NULL) {
1874 _mesa_error(ctx, GL_INVALID_ENUM,
1875 "glGetFramebufferAttachmentParameterivEXT(attachment)");
1876 return;
1877 }
1878
1879 if (attachment == GL_DEPTH_STENCIL_ATTACHMENT) {
1880 /* the depth and stencil attachments must point to the same buffer */
1881 const struct gl_renderbuffer_attachment *depthAtt, *stencilAtt;
1882 depthAtt = _mesa_get_attachment(ctx, buffer, GL_DEPTH_ATTACHMENT);
1883 stencilAtt = _mesa_get_attachment(ctx, buffer, GL_STENCIL_ATTACHMENT);
1884 if (depthAtt->Renderbuffer != stencilAtt->Renderbuffer) {
1885 _mesa_error(ctx, GL_INVALID_OPERATION,
1886 "glGetFramebufferAttachmentParameterivEXT(DEPTH/STENCIL"
1887 " attachments differ)");
1888 return;
1889 }
1890 }
1891
1892 /* No need to flush here */
1893
1894 switch (pname) {
1895 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
1896 *params = att->Type;
1897 return;
1898 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
1899 if (att->Type == GL_RENDERBUFFER_EXT) {
1900 *params = att->Renderbuffer->Name;
1901 }
1902 else if (att->Type == GL_TEXTURE) {
1903 *params = att->Texture->Name;
1904 }
1905 else {
1906 _mesa_error(ctx, GL_INVALID_ENUM,
1907 "glGetFramebufferAttachmentParameterivEXT(pname)");
1908 }
1909 return;
1910 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
1911 if (att->Type == GL_TEXTURE) {
1912 *params = att->TextureLevel;
1913 }
1914 else {
1915 _mesa_error(ctx, GL_INVALID_ENUM,
1916 "glGetFramebufferAttachmentParameterivEXT(pname)");
1917 }
1918 return;
1919 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
1920 if (att->Type == GL_TEXTURE) {
1921 if (att->Texture && att->Texture->Target == GL_TEXTURE_CUBE_MAP) {
1922 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
1923 }
1924 else {
1925 *params = 0;
1926 }
1927 }
1928 else {
1929 _mesa_error(ctx, GL_INVALID_ENUM,
1930 "glGetFramebufferAttachmentParameterivEXT(pname)");
1931 }
1932 return;
1933 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
1934 if (att->Type == GL_TEXTURE) {
1935 if (att->Texture && att->Texture->Target == GL_TEXTURE_3D) {
1936 *params = att->Zoffset;
1937 }
1938 else {
1939 *params = 0;
1940 }
1941 }
1942 else {
1943 _mesa_error(ctx, GL_INVALID_ENUM,
1944 "glGetFramebufferAttachmentParameterivEXT(pname)");
1945 }
1946 return;
1947 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING:
1948 if (!ctx->Extensions.ARB_framebuffer_object) {
1949 _mesa_error(ctx, GL_INVALID_ENUM,
1950 "glGetFramebufferAttachmentParameterivEXT(pname)");
1951 }
1952 else {
1953 *params = _mesa_get_format_color_encoding(att->Renderbuffer->Format);
1954 }
1955 return;
1956 case GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE:
1957 if (!ctx->Extensions.ARB_framebuffer_object) {
1958 _mesa_error(ctx, GL_INVALID_ENUM,
1959 "glGetFramebufferAttachmentParameterivEXT(pname)");
1960 return;
1961 }
1962 else {
1963 gl_format format = att->Renderbuffer->Format;
1964 if (format == MESA_FORMAT_CI8 || format == MESA_FORMAT_S8) {
1965 /* special cases */
1966 *params = GL_INDEX;
1967 }
1968 else {
1969 *params = _mesa_get_format_datatype(format);
1970 }
1971 }
1972 return;
1973 case GL_FRAMEBUFFER_ATTACHMENT_RED_SIZE:
1974 case GL_FRAMEBUFFER_ATTACHMENT_GREEN_SIZE:
1975 case GL_FRAMEBUFFER_ATTACHMENT_BLUE_SIZE:
1976 case GL_FRAMEBUFFER_ATTACHMENT_ALPHA_SIZE:
1977 case GL_FRAMEBUFFER_ATTACHMENT_DEPTH_SIZE:
1978 case GL_FRAMEBUFFER_ATTACHMENT_STENCIL_SIZE:
1979 if (!ctx->Extensions.ARB_framebuffer_object) {
1980 _mesa_error(ctx, GL_INVALID_ENUM,
1981 "glGetFramebufferAttachmentParameterivEXT(pname)");
1982 }
1983 else if (att->Texture) {
1984 const struct gl_texture_image *texImage =
1985 _mesa_select_tex_image(ctx, att->Texture, att->Texture->Target,
1986 att->TextureLevel);
1987 if (texImage) {
1988 *params = get_component_bits(pname, texImage->_BaseFormat,
1989 texImage->TexFormat);
1990 }
1991 else {
1992 *params = 0;
1993 }
1994 }
1995 else if (att->Renderbuffer) {
1996 *params = get_component_bits(pname, att->Renderbuffer->_BaseFormat,
1997 att->Renderbuffer->Format);
1998 }
1999 else {
2000 *params = 0;
2001 }
2002 return;
2003 default:
2004 _mesa_error(ctx, GL_INVALID_ENUM,
2005 "glGetFramebufferAttachmentParameterivEXT(pname)");
2006 return;
2007 }
2008 }
2009
2010
2011 void GLAPIENTRY
2012 _mesa_GenerateMipmapEXT(GLenum target)
2013 {
2014 struct gl_texture_object *texObj;
2015 GET_CURRENT_CONTEXT(ctx);
2016
2017 ASSERT_OUTSIDE_BEGIN_END(ctx);
2018 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2019
2020 switch (target) {
2021 case GL_TEXTURE_1D:
2022 case GL_TEXTURE_2D:
2023 case GL_TEXTURE_3D:
2024 case GL_TEXTURE_CUBE_MAP:
2025 /* OK, legal value */
2026 break;
2027 default:
2028 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target)");
2029 return;
2030 }
2031
2032 texObj = _mesa_get_current_tex_object(ctx, target);
2033
2034 if (texObj->BaseLevel >= texObj->MaxLevel) {
2035 /* nothing to do */
2036 return;
2037 }
2038
2039 _mesa_lock_texture(ctx, texObj);
2040 if (target == GL_TEXTURE_CUBE_MAP) {
2041 GLuint face;
2042 for (face = 0; face < 6; face++)
2043 ctx->Driver.GenerateMipmap(ctx,
2044 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + face,
2045 texObj);
2046 }
2047 else {
2048 ctx->Driver.GenerateMipmap(ctx, target, texObj);
2049 }
2050 _mesa_unlock_texture(ctx, texObj);
2051 }
2052
2053
2054 #if FEATURE_EXT_framebuffer_blit
2055
2056 static const struct gl_renderbuffer_attachment *
2057 find_attachment(const struct gl_framebuffer *fb, const struct gl_renderbuffer *rb)
2058 {
2059 GLuint i;
2060 for (i = 0; i < Elements(fb->Attachment); i++) {
2061 if (fb->Attachment[i].Renderbuffer == rb)
2062 return &fb->Attachment[i];
2063 }
2064 return NULL;
2065 }
2066
2067
2068
2069 /**
2070 * Blit rectangular region, optionally from one framebuffer to another.
2071 *
2072 * Note, if the src buffer is multisampled and the dest is not, this is
2073 * when the samples must be resolved to a single color.
2074 */
2075 void GLAPIENTRY
2076 _mesa_BlitFramebufferEXT(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
2077 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
2078 GLbitfield mask, GLenum filter)
2079 {
2080 const GLbitfield legalMaskBits = (GL_COLOR_BUFFER_BIT |
2081 GL_DEPTH_BUFFER_BIT |
2082 GL_STENCIL_BUFFER_BIT);
2083 const struct gl_framebuffer *readFb, *drawFb;
2084 const struct gl_renderbuffer *colorReadRb, *colorDrawRb;
2085 GET_CURRENT_CONTEXT(ctx);
2086
2087 ASSERT_OUTSIDE_BEGIN_END(ctx);
2088 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
2089
2090 if (ctx->NewState) {
2091 _mesa_update_state(ctx);
2092 }
2093
2094 readFb = ctx->ReadBuffer;
2095 drawFb = ctx->DrawBuffer;
2096
2097 if (!readFb || !drawFb) {
2098 /* This will normally never happen but someday we may want to
2099 * support MakeCurrent() with no drawables.
2100 */
2101 return;
2102 }
2103
2104 /* check for complete framebuffers */
2105 if (drawFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
2106 readFb->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
2107 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
2108 "glBlitFramebufferEXT(incomplete draw/read buffers)");
2109 return;
2110 }
2111
2112 if (filter != GL_NEAREST && filter != GL_LINEAR) {
2113 _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(filter)");
2114 return;
2115 }
2116
2117 if (mask & ~legalMaskBits) {
2118 _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
2119 return;
2120 }
2121
2122 /* depth/stencil must be blitted with nearest filtering */
2123 if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
2124 && filter != GL_NEAREST) {
2125 _mesa_error(ctx, GL_INVALID_OPERATION,
2126 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
2127 return;
2128 }
2129
2130 /* get color read/draw renderbuffers */
2131 if (mask & GL_COLOR_BUFFER_BIT) {
2132 colorReadRb = readFb->_ColorReadBuffer;
2133 colorDrawRb = drawFb->_ColorDrawBuffers[0];
2134 }
2135 else {
2136 colorReadRb = colorDrawRb = NULL;
2137 }
2138
2139 if (mask & GL_STENCIL_BUFFER_BIT) {
2140 struct gl_renderbuffer *readRb = readFb->_StencilBuffer;
2141 struct gl_renderbuffer *drawRb = drawFb->_StencilBuffer;
2142 if (!readRb ||
2143 !drawRb ||
2144 _mesa_get_format_bits(readRb->Format, GL_STENCIL_BITS) !=
2145 _mesa_get_format_bits(drawRb->Format, GL_STENCIL_BITS)) {
2146 _mesa_error(ctx, GL_INVALID_OPERATION,
2147 "glBlitFramebufferEXT(stencil buffer size mismatch");
2148 return;
2149 }
2150 }
2151
2152 if (mask & GL_DEPTH_BUFFER_BIT) {
2153 struct gl_renderbuffer *readRb = readFb->_DepthBuffer;
2154 struct gl_renderbuffer *drawRb = drawFb->_DepthBuffer;
2155 if (!readRb ||
2156 !drawRb ||
2157 _mesa_get_format_bits(readRb->Format, GL_DEPTH_BITS) !=
2158 _mesa_get_format_bits(drawRb->Format, GL_DEPTH_BITS)) {
2159 _mesa_error(ctx, GL_INVALID_OPERATION,
2160 "glBlitFramebufferEXT(depth buffer size mismatch");
2161 return;
2162 }
2163 }
2164
2165 if (readFb->Visual.samples > 0 &&
2166 drawFb->Visual.samples > 0 &&
2167 readFb->Visual.samples != drawFb->Visual.samples) {
2168 _mesa_error(ctx, GL_INVALID_OPERATION,
2169 "glBlitFramebufferEXT(mismatched samples");
2170 return;
2171 }
2172
2173 /* extra checks for multisample copies... */
2174 if (readFb->Visual.samples > 0 || drawFb->Visual.samples > 0) {
2175 /* src and dest region sizes must be the same */
2176 if (srcX1 - srcX0 != dstX1 - dstX0 ||
2177 srcY1 - srcY0 != dstY1 - dstY0) {
2178 _mesa_error(ctx, GL_INVALID_OPERATION,
2179 "glBlitFramebufferEXT(bad src/dst multisample region sizes");
2180 return;
2181 }
2182
2183 /* color formats must match */
2184 if (colorReadRb &&
2185 colorDrawRb &&
2186 colorReadRb->Format != colorDrawRb->Format) {
2187 _mesa_error(ctx, GL_INVALID_OPERATION,
2188 "glBlitFramebufferEXT(bad src/dst multisample pixel formats");
2189 return;
2190 }
2191 }
2192
2193 if (!ctx->Extensions.EXT_framebuffer_blit) {
2194 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
2195 return;
2196 }
2197
2198 /* Debug code */
2199 if (DEBUG_BLIT) {
2200 printf("glBlitFramebuffer(%d, %d, %d, %d, %d, %d, %d, %d,"
2201 " 0x%x, 0x%x)\n",
2202 srcX0, srcY0, srcX1, srcY1,
2203 dstX0, dstY0, dstX1, dstY1,
2204 mask, filter);
2205 if (colorReadRb) {
2206 const struct gl_renderbuffer_attachment *att;
2207
2208 att = find_attachment(readFb, colorReadRb);
2209 printf(" Src FBO %u RB %u (%dx%d) ",
2210 readFb->Name, colorReadRb->Name,
2211 colorReadRb->Width, colorReadRb->Height);
2212 if (att && att->Texture) {
2213 printf("Tex %u tgt 0x%x level %u face %u",
2214 att->Texture->Name,
2215 att->Texture->Target,
2216 att->TextureLevel,
2217 att->CubeMapFace);
2218 }
2219 printf("\n");
2220
2221 att = find_attachment(drawFb, colorDrawRb);
2222 printf(" Dst FBO %u RB %u (%dx%d) ",
2223 drawFb->Name, colorDrawRb->Name,
2224 colorDrawRb->Width, colorDrawRb->Height);
2225 if (att && att->Texture) {
2226 printf("Tex %u tgt 0x%x level %u face %u",
2227 att->Texture->Name,
2228 att->Texture->Target,
2229 att->TextureLevel,
2230 att->CubeMapFace);
2231 }
2232 printf("\n");
2233 }
2234 }
2235
2236 ASSERT(ctx->Driver.BlitFramebuffer);
2237 ctx->Driver.BlitFramebuffer(ctx,
2238 srcX0, srcY0, srcX1, srcY1,
2239 dstX0, dstY0, dstX1, dstY1,
2240 mask, filter);
2241 }
2242 #endif /* FEATURE_EXT_framebuffer_blit */