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