fix mesa's handling of fbo's / window fb (again)
[mesa.git] / src / mesa / main / fbobject.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 6.5.1
4 *
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25
26 /*
27 * Authors:
28 * Brian Paul
29 */
30
31
32 #include "buffers.h"
33 #include "context.h"
34 #include "fbobject.h"
35 #include "framebuffer.h"
36 #include "hash.h"
37 #include "mipmap.h"
38 #include "renderbuffer.h"
39 #include "state.h"
40 #include "teximage.h"
41 #include "texobj.h"
42 #include "texstore.h"
43
44
45 /**
46 * Notes:
47 *
48 * None of the GL_EXT_framebuffer_object functions are compiled into
49 * display lists.
50 */
51
52
53
54 /*
55 * When glGenRender/FramebuffersEXT() is called we insert pointers to
56 * these placeholder objects into the hash table.
57 * Later, when the object ID is first bound, we replace the placeholder
58 * with the real frame/renderbuffer.
59 */
60 static struct gl_framebuffer DummyFramebuffer;
61 static struct gl_renderbuffer DummyRenderbuffer;
62
63
64 #define IS_CUBE_FACE(TARGET) \
65 ((TARGET) >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && \
66 (TARGET) <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
67
68
69 /**
70 * Helper routine for getting a gl_renderbuffer.
71 */
72 struct gl_renderbuffer *
73 _mesa_lookup_renderbuffer(GLcontext *ctx, GLuint id)
74 {
75 struct gl_renderbuffer *rb;
76
77 if (id == 0)
78 return NULL;
79
80 rb = (struct gl_renderbuffer *)
81 _mesa_HashLookup(ctx->Shared->RenderBuffers, id);
82 return rb;
83 }
84
85
86 /**
87 * Helper routine for getting a gl_framebuffer.
88 */
89 struct gl_framebuffer *
90 _mesa_lookup_framebuffer(GLcontext *ctx, GLuint id)
91 {
92 struct gl_framebuffer *fb;
93
94 if (id == 0)
95 return NULL;
96
97 fb = (struct gl_framebuffer *)
98 _mesa_HashLookup(ctx->Shared->FrameBuffers, id);
99 return fb;
100 }
101
102
103 /**
104 * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
105 * gl_renderbuffer_attachment object.
106 */
107 struct gl_renderbuffer_attachment *
108 _mesa_get_attachment(GLcontext *ctx, struct gl_framebuffer *fb,
109 GLenum attachment)
110 {
111 GLuint i;
112
113 switch (attachment) {
114 case GL_COLOR_ATTACHMENT0_EXT:
115 case GL_COLOR_ATTACHMENT1_EXT:
116 case GL_COLOR_ATTACHMENT2_EXT:
117 case GL_COLOR_ATTACHMENT3_EXT:
118 case GL_COLOR_ATTACHMENT4_EXT:
119 case GL_COLOR_ATTACHMENT5_EXT:
120 case GL_COLOR_ATTACHMENT6_EXT:
121 case GL_COLOR_ATTACHMENT7_EXT:
122 case GL_COLOR_ATTACHMENT8_EXT:
123 case GL_COLOR_ATTACHMENT9_EXT:
124 case GL_COLOR_ATTACHMENT10_EXT:
125 case GL_COLOR_ATTACHMENT11_EXT:
126 case GL_COLOR_ATTACHMENT12_EXT:
127 case GL_COLOR_ATTACHMENT13_EXT:
128 case GL_COLOR_ATTACHMENT14_EXT:
129 case GL_COLOR_ATTACHMENT15_EXT:
130 i = attachment - GL_COLOR_ATTACHMENT0_EXT;
131 if (i >= ctx->Const.MaxColorAttachments) {
132 return NULL;
133 }
134 return &fb->Attachment[BUFFER_COLOR0 + i];
135 case GL_DEPTH_ATTACHMENT_EXT:
136 return &fb->Attachment[BUFFER_DEPTH];
137 case GL_STENCIL_ATTACHMENT_EXT:
138 return &fb->Attachment[BUFFER_STENCIL];
139 default:
140 return NULL;
141 }
142 }
143
144
145 /**
146 * Remove any texture or renderbuffer attached to the given attachment
147 * point. Update reference counts, etc.
148 */
149 void
150 _mesa_remove_attachment(GLcontext *ctx, struct gl_renderbuffer_attachment *att)
151 {
152 if (att->Type == GL_TEXTURE) {
153 ASSERT(att->Texture);
154 att->Texture->RefCount--;
155 if (att->Texture->RefCount == 0) {
156 ctx->Driver.DeleteTexture(ctx, att->Texture);
157 }
158 else {
159 /* tell driver that we're done rendering to this texture. */
160 if (ctx->Driver.FinishRenderTexture) {
161 ctx->Driver.FinishRenderTexture(ctx, att);
162 }
163 }
164 att->Texture = NULL;
165 }
166 if (att->Type == GL_TEXTURE || att->Type == GL_RENDERBUFFER_EXT) {
167 ASSERT(att->Renderbuffer);
168 ASSERT(!att->Texture);
169 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
170 }
171 att->Type = GL_NONE;
172 att->Complete = GL_TRUE;
173 }
174
175
176 /**
177 * Bind a texture object to an attachment point.
178 * The previous binding, if any, will be removed first.
179 */
180 void
181 _mesa_set_texture_attachment(GLcontext *ctx,
182 struct gl_framebuffer *fb,
183 struct gl_renderbuffer_attachment *att,
184 struct gl_texture_object *texObj,
185 GLenum texTarget, GLuint level, GLuint zoffset)
186 {
187 if (att->Texture == texObj) {
188 /* re-attaching same texture */
189 ASSERT(att->Type == GL_TEXTURE);
190 }
191 else {
192 /* new attachment */
193 _mesa_remove_attachment(ctx, att);
194 att->Type = GL_TEXTURE;
195 att->Texture = texObj;
196 texObj->RefCount++;
197 }
198
199 /* always update these fields */
200 att->TextureLevel = level;
201 if (IS_CUBE_FACE(texTarget)) {
202 att->CubeMapFace = texTarget - GL_TEXTURE_CUBE_MAP_POSITIVE_X;
203 }
204 else {
205 att->CubeMapFace = 0;
206 }
207 att->Zoffset = zoffset;
208 att->Complete = GL_FALSE;
209
210 if (att->Texture->Image[att->CubeMapFace][att->TextureLevel]) {
211 ctx->Driver.RenderTexture(ctx, fb, att);
212 }
213 }
214
215
216 /**
217 * Bind a renderbuffer to an attachment point.
218 * The previous binding, if any, will be removed first.
219 */
220 void
221 _mesa_set_renderbuffer_attachment(GLcontext *ctx,
222 struct gl_renderbuffer_attachment *att,
223 struct gl_renderbuffer *rb)
224 {
225 /* XXX check if re-doing same attachment, exit early */
226 _mesa_remove_attachment(ctx, att);
227 att->Type = GL_RENDERBUFFER_EXT;
228 att->Texture = NULL; /* just to be safe */
229 att->Complete = GL_FALSE;
230 _mesa_reference_renderbuffer(&att->Renderbuffer, rb);
231 }
232
233
234 /**
235 * Fallback for ctx->Driver.FramebufferRenderbuffer()
236 * Attach a renderbuffer object to a framebuffer object.
237 */
238 void
239 _mesa_framebuffer_renderbuffer(GLcontext *ctx, struct gl_framebuffer *fb,
240 GLenum attachment, struct gl_renderbuffer *rb)
241 {
242 struct gl_renderbuffer_attachment *att;
243
244 _glthread_LOCK_MUTEX(fb->Mutex);
245
246 att = _mesa_get_attachment(ctx, fb, attachment);
247 ASSERT(att);
248 if (rb) {
249 _mesa_set_renderbuffer_attachment(ctx, att, rb);
250 }
251 else {
252 _mesa_remove_attachment(ctx, att);
253 }
254
255 _glthread_UNLOCK_MUTEX(fb->Mutex);
256 }
257
258
259 /**
260 * Test if an attachment point is complete and update its Complete field.
261 * \param format if GL_COLOR, this is a color attachment point,
262 * if GL_DEPTH, this is a depth component attachment point,
263 * if GL_STENCIL, this is a stencil component attachment point.
264 */
265 static void
266 test_attachment_completeness(const GLcontext *ctx, GLenum format,
267 struct gl_renderbuffer_attachment *att)
268 {
269 assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
270
271 /* assume complete */
272 att->Complete = GL_TRUE;
273
274 /* Look for reasons why the attachment might be incomplete */
275 if (att->Type == GL_TEXTURE) {
276 const struct gl_texture_object *texObj = att->Texture;
277 struct gl_texture_image *texImage;
278
279 if (!texObj) {
280 att->Complete = GL_FALSE;
281 return;
282 }
283
284 texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
285 if (!texImage) {
286 att->Complete = GL_FALSE;
287 return;
288 }
289 if (texImage->Width < 1 || texImage->Height < 1) {
290 att->Complete = GL_FALSE;
291 return;
292 }
293 if (texObj->Target == GL_TEXTURE_3D && att->Zoffset >= texImage->Depth) {
294 att->Complete = GL_FALSE;
295 return;
296 }
297
298 if (format == GL_COLOR) {
299 if (texImage->TexFormat->BaseFormat != GL_RGB &&
300 texImage->TexFormat->BaseFormat != GL_RGBA) {
301 att->Complete = GL_FALSE;
302 return;
303 }
304 }
305 else if (format == GL_DEPTH) {
306 if (texImage->TexFormat->BaseFormat == GL_DEPTH_COMPONENT) {
307 /* OK */
308 }
309 else if (ctx->Extensions.EXT_packed_depth_stencil &&
310 att->Renderbuffer->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
311 /* OK */
312 }
313 else {
314 att->Complete = GL_FALSE;
315 return;
316 }
317 }
318 else {
319 /* no such thing as stencil textures */
320 att->Complete = GL_FALSE;
321 return;
322 }
323 }
324 else if (att->Type == GL_RENDERBUFFER_EXT) {
325 ASSERT(att->Renderbuffer);
326 if (!att->Renderbuffer->InternalFormat ||
327 att->Renderbuffer->Width < 1 ||
328 att->Renderbuffer->Height < 1) {
329 att->Complete = GL_FALSE;
330 return;
331 }
332 if (format == GL_COLOR) {
333 if (att->Renderbuffer->_BaseFormat != GL_RGB &&
334 att->Renderbuffer->_BaseFormat != GL_RGBA) {
335 ASSERT(att->Renderbuffer->RedBits);
336 ASSERT(att->Renderbuffer->GreenBits);
337 ASSERT(att->Renderbuffer->BlueBits);
338 att->Complete = GL_FALSE;
339 return;
340 }
341 }
342 else if (format == GL_DEPTH) {
343 ASSERT(att->Renderbuffer->DepthBits);
344 if (att->Renderbuffer->_BaseFormat == GL_DEPTH_COMPONENT) {
345 /* OK */
346 }
347 else if (ctx->Extensions.EXT_packed_depth_stencil &&
348 att->Renderbuffer->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
349 /* OK */
350 }
351 else {
352 att->Complete = GL_FALSE;
353 return;
354 }
355 }
356 else {
357 assert(format == GL_STENCIL);
358 ASSERT(att->Renderbuffer->StencilBits);
359 if (att->Renderbuffer->_BaseFormat == GL_STENCIL_INDEX) {
360 /* OK */
361 }
362 else if (ctx->Extensions.EXT_packed_depth_stencil &&
363 att->Renderbuffer->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
364 /* OK */
365 }
366 else {
367 att->Complete = GL_FALSE;
368 return;
369 }
370 }
371 }
372 else {
373 ASSERT(att->Type == GL_NONE);
374 /* complete */
375 return;
376 }
377 }
378
379
380 /**
381 * Helpful for debugging
382 */
383 static void
384 fbo_incomplete(const char *msg, int index)
385 {
386 (void) msg;
387 (void) index;
388 /*
389 _mesa_debug(NULL, "FBO Incomplete: %s [%d]\n", msg, index);
390 */
391 }
392
393
394 /**
395 * Test if the given framebuffer object is complete and update its
396 * Status field with the results.
397 * Also update the framebuffer's Width and Height fields if the
398 * framebuffer is complete.
399 */
400 void
401 _mesa_test_framebuffer_completeness(GLcontext *ctx, struct gl_framebuffer *fb)
402 {
403 GLuint numImages, width = 0, height = 0;
404 GLenum intFormat = GL_NONE;
405 GLuint w = 0, h = 0;
406 GLint i;
407 GLuint j;
408
409 assert(fb->Name != 0);
410
411 numImages = 0;
412 fb->Width = 0;
413 fb->Height = 0;
414
415 /* Start at -2 to more easily loop over all attachment points */
416 for (i = -2; i < (GLint) ctx->Const.MaxColorAttachments; i++) {
417 struct gl_renderbuffer_attachment *att;
418 GLenum f;
419
420 if (i == -2) {
421 att = &fb->Attachment[BUFFER_DEPTH];
422 test_attachment_completeness(ctx, GL_DEPTH, att);
423 if (!att->Complete) {
424 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
425 fbo_incomplete("depth attachment incomplete", -1);
426 return;
427 }
428 }
429 else if (i == -1) {
430 att = &fb->Attachment[BUFFER_STENCIL];
431 test_attachment_completeness(ctx, GL_STENCIL, att);
432 if (!att->Complete) {
433 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
434 fbo_incomplete("stencil attachment incomplete", -1);
435 return;
436 }
437 }
438 else {
439 att = &fb->Attachment[BUFFER_COLOR0 + i];
440 test_attachment_completeness(ctx, GL_COLOR, att);
441 if (!att->Complete) {
442 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
443 fbo_incomplete("color attachment incomplete", i);
444 return;
445 }
446 }
447
448 if (att->Type == GL_TEXTURE) {
449 const struct gl_texture_image *texImg
450 = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
451 w = texImg->Width;
452 h = texImg->Height;
453 f = texImg->_BaseFormat;
454 numImages++;
455 if (f != GL_RGB && f != GL_RGBA && f != GL_DEPTH_COMPONENT
456 && f != GL_DEPTH_STENCIL_EXT) {
457 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
458 fbo_incomplete("texture attachment incomplete", -1);
459 return;
460 }
461 }
462 else if (att->Type == GL_RENDERBUFFER_EXT) {
463 w = att->Renderbuffer->Width;
464 h = att->Renderbuffer->Height;
465 f = att->Renderbuffer->InternalFormat;
466 numImages++;
467 }
468 else {
469 assert(att->Type == GL_NONE);
470 continue;
471 }
472
473 if (numImages == 1) {
474 /* set required width, height and format */
475 width = w;
476 height = h;
477 if (i >= 0)
478 intFormat = f;
479 }
480 else {
481 /* check that width, height, format are same */
482 if (w != width || h != height) {
483 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
484 fbo_incomplete("width or height mismatch", -1);
485 return;
486 }
487 if (intFormat != GL_NONE && f != intFormat) {
488 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
489 fbo_incomplete("format mismatch", -1);
490 return;
491 }
492 }
493 }
494
495 /* Check that all DrawBuffers are present */
496 for (j = 0; j < ctx->Const.MaxDrawBuffers; j++) {
497 if (fb->ColorDrawBuffer[j] != GL_NONE) {
498 const struct gl_renderbuffer_attachment *att
499 = _mesa_get_attachment(ctx, fb, fb->ColorDrawBuffer[j]);
500 assert(att);
501 if (att->Type == GL_NONE) {
502 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
503 fbo_incomplete("missing drawbuffer", j);
504 return;
505 }
506 }
507 }
508
509 /* Check that the ReadBuffer is present */
510 if (fb->ColorReadBuffer != GL_NONE) {
511 const struct gl_renderbuffer_attachment *att
512 = _mesa_get_attachment(ctx, fb, fb->ColorReadBuffer);
513 assert(att);
514 if (att->Type == GL_NONE) {
515 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
516 fbo_incomplete("missing readbuffer", -1);
517 return;
518 }
519 }
520
521 if (numImages == 0) {
522 fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
523 fbo_incomplete("no attachments", -1);
524 return;
525 }
526
527 /*
528 * If we get here, the framebuffer is complete!
529 */
530 fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
531 fb->Width = w;
532 fb->Height = h;
533 }
534
535
536 GLboolean GLAPIENTRY
537 _mesa_IsRenderbufferEXT(GLuint renderbuffer)
538 {
539 GET_CURRENT_CONTEXT(ctx);
540 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
541 if (renderbuffer) {
542 struct gl_renderbuffer *rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
543 if (rb != NULL && rb != &DummyRenderbuffer)
544 return GL_TRUE;
545 }
546 return GL_FALSE;
547 }
548
549
550 void GLAPIENTRY
551 _mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
552 {
553 struct gl_renderbuffer *newRb;
554 GET_CURRENT_CONTEXT(ctx);
555
556 ASSERT_OUTSIDE_BEGIN_END(ctx);
557
558 if (target != GL_RENDERBUFFER_EXT) {
559 _mesa_error(ctx, GL_INVALID_ENUM,
560 "glBindRenderbufferEXT(target)");
561 return;
562 }
563
564 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
565 /* The above doesn't fully flush the drivers in the way that a
566 * glFlush does, but that is required here:
567 */
568 if (ctx->Driver.Flush)
569 ctx->Driver.Flush(ctx);
570
571
572 if (renderbuffer) {
573 newRb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
574 if (newRb == &DummyRenderbuffer) {
575 /* ID was reserved, but no real renderbuffer object made yet */
576 newRb = NULL;
577 }
578 if (!newRb) {
579 /* create new renderbuffer object */
580 newRb = ctx->Driver.NewRenderbuffer(ctx, renderbuffer);
581 if (!newRb) {
582 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindRenderbufferEXT");
583 return;
584 }
585 ASSERT(newRb->AllocStorage);
586 _mesa_HashInsert(ctx->Shared->RenderBuffers, renderbuffer, newRb);
587 newRb->RefCount = 1; /* referenced by hash table */
588 }
589 }
590 else {
591 newRb = NULL;
592 }
593
594 ASSERT(newRb != &DummyRenderbuffer);
595
596 _mesa_reference_renderbuffer(&ctx->CurrentRenderbuffer, newRb);
597 }
598
599
600 void GLAPIENTRY
601 _mesa_DeleteRenderbuffersEXT(GLsizei n, const GLuint *renderbuffers)
602 {
603 GLint i;
604 GET_CURRENT_CONTEXT(ctx);
605
606 ASSERT_OUTSIDE_BEGIN_END(ctx);
607 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
608
609 for (i = 0; i < n; i++) {
610 if (renderbuffers[i] > 0) {
611 struct gl_renderbuffer *rb;
612 rb = _mesa_lookup_renderbuffer(ctx, renderbuffers[i]);
613 if (rb) {
614 /* check if deleting currently bound renderbuffer object */
615 if (rb == ctx->CurrentRenderbuffer) {
616 /* bind default */
617 ASSERT(rb->RefCount >= 2);
618 _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
619 }
620
621 /* Remove from hash table immediately, to free the ID.
622 * But the object will not be freed until it's no longer
623 * referenced anywhere else.
624 */
625 _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
626
627 if (rb != &DummyRenderbuffer) {
628 /* no longer referenced by hash table */
629 _mesa_reference_renderbuffer(&rb, NULL);
630 }
631 }
632 }
633 }
634 }
635
636
637 void GLAPIENTRY
638 _mesa_GenRenderbuffersEXT(GLsizei n, GLuint *renderbuffers)
639 {
640 GET_CURRENT_CONTEXT(ctx);
641 GLuint first;
642 GLint i;
643
644 ASSERT_OUTSIDE_BEGIN_END(ctx);
645
646 if (n < 0) {
647 _mesa_error(ctx, GL_INVALID_VALUE, "glGenRenderbuffersEXT(n)");
648 return;
649 }
650
651 if (!renderbuffers)
652 return;
653
654 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
655
656 for (i = 0; i < n; i++) {
657 GLuint name = first + i;
658 renderbuffers[i] = name;
659 /* insert dummy placeholder into hash table */
660 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
661 _mesa_HashInsert(ctx->Shared->RenderBuffers, name, &DummyRenderbuffer);
662 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
663 }
664 }
665
666
667 /**
668 * Given an internal format token for a render buffer, return the
669 * corresponding base format.
670 * This is very similar to _mesa_base_tex_format() but the set of valid
671 * internal formats is somewhat different.
672 *
673 * \return one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT
674 * GL_DEPTH_STENCIL_EXT or zero if error.
675 */
676 GLenum
677 _mesa_base_fbo_format(GLcontext *ctx, GLenum internalFormat)
678 {
679 switch (internalFormat) {
680 case GL_RGB:
681 case GL_R3_G3_B2:
682 case GL_RGB4:
683 case GL_RGB5:
684 case GL_RGB8:
685 case GL_RGB10:
686 case GL_RGB12:
687 case GL_RGB16:
688 return GL_RGB;
689 case GL_RGBA:
690 case GL_RGBA2:
691 case GL_RGBA4:
692 case GL_RGB5_A1:
693 case GL_RGBA8:
694 case GL_RGB10_A2:
695 case GL_RGBA12:
696 case GL_RGBA16:
697 return GL_RGBA;
698 case GL_STENCIL_INDEX:
699 case GL_STENCIL_INDEX1_EXT:
700 case GL_STENCIL_INDEX4_EXT:
701 case GL_STENCIL_INDEX8_EXT:
702 case GL_STENCIL_INDEX16_EXT:
703 return GL_STENCIL_INDEX;
704 case GL_DEPTH_COMPONENT:
705 case GL_DEPTH_COMPONENT16:
706 case GL_DEPTH_COMPONENT24:
707 case GL_DEPTH_COMPONENT32:
708 return GL_DEPTH_COMPONENT;
709 case GL_DEPTH_STENCIL_EXT:
710 case GL_DEPTH24_STENCIL8_EXT:
711 if (ctx->Extensions.EXT_packed_depth_stencil)
712 return GL_DEPTH_STENCIL_EXT;
713 else
714 return 0;
715 /* XXX add floating point formats eventually */
716 default:
717 return 0;
718 }
719 }
720
721
722 void GLAPIENTRY
723 _mesa_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
724 GLsizei width, GLsizei height)
725 {
726 struct gl_renderbuffer *rb;
727 GLenum baseFormat;
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, "glRenderbufferStorageEXT(target)");
734 return;
735 }
736
737 baseFormat = _mesa_base_fbo_format(ctx, internalFormat);
738 if (baseFormat == 0) {
739 _mesa_error(ctx, GL_INVALID_ENUM,
740 "glRenderbufferStorageEXT(internalFormat)");
741 return;
742 }
743
744 if (width < 1 || width > (GLsizei) ctx->Const.MaxRenderbufferSize) {
745 _mesa_error(ctx, GL_INVALID_VALUE, "glRenderbufferStorageEXT(width)");
746 return;
747 }
748
749 if (height < 1 || height > (GLsizei) ctx->Const.MaxRenderbufferSize) {
750 _mesa_error(ctx, GL_INVALID_VALUE, "glRenderbufferStorageEXT(height)");
751 return;
752 }
753
754 rb = ctx->CurrentRenderbuffer;
755
756 if (!rb) {
757 _mesa_error(ctx, GL_INVALID_OPERATION, "glRenderbufferStorageEXT");
758 return;
759 }
760
761 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
762
763 if (rb->InternalFormat == internalFormat &&
764 rb->Width == (GLuint) width &&
765 rb->Height == (GLuint) height) {
766 /* no change in allocation needed */
767 return;
768 }
769
770 /* These MUST get set by the AllocStorage func */
771 rb->_ActualFormat = 0;
772 rb->RedBits =
773 rb->GreenBits =
774 rb->BlueBits =
775 rb->AlphaBits =
776 rb->IndexBits =
777 rb->DepthBits =
778 rb->StencilBits = 0;
779
780 /* Now allocate the storage */
781 ASSERT(rb->AllocStorage);
782 if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
783 /* No error - check/set fields now */
784 assert(rb->_ActualFormat);
785 assert(rb->Width == (GLuint) width);
786 assert(rb->Height == (GLuint) height);
787 assert(rb->RedBits || rb->GreenBits || rb->BlueBits || rb->AlphaBits ||
788 rb->DepthBits || rb->StencilBits || rb->IndexBits);
789 rb->InternalFormat = internalFormat;
790 rb->_BaseFormat = baseFormat;
791 }
792 else {
793 /* Probably ran out of memory - clear the fields */
794 rb->Width = 0;
795 rb->Height = 0;
796 rb->InternalFormat = GL_NONE;
797 rb->_ActualFormat = GL_NONE;
798 rb->_BaseFormat = GL_NONE;
799 rb->RedBits =
800 rb->GreenBits =
801 rb->BlueBits =
802 rb->AlphaBits =
803 rb->IndexBits =
804 rb->DepthBits =
805 rb->StencilBits = 0;
806 }
807
808 /*
809 test_framebuffer_completeness(ctx, fb);
810 */
811 /* XXX if this renderbuffer is attached anywhere, invalidate attachment
812 * points???
813 */
814 }
815
816
817 void GLAPIENTRY
818 _mesa_GetRenderbufferParameterivEXT(GLenum target, GLenum pname, GLint *params)
819 {
820 GET_CURRENT_CONTEXT(ctx);
821
822 ASSERT_OUTSIDE_BEGIN_END(ctx);
823
824 if (target != GL_RENDERBUFFER_EXT) {
825 _mesa_error(ctx, GL_INVALID_ENUM,
826 "glGetRenderbufferParameterivEXT(target)");
827 return;
828 }
829
830 if (!ctx->CurrentRenderbuffer) {
831 _mesa_error(ctx, GL_INVALID_OPERATION,
832 "glGetRenderbufferParameterivEXT");
833 return;
834 }
835
836 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
837
838 switch (pname) {
839 case GL_RENDERBUFFER_WIDTH_EXT:
840 *params = ctx->CurrentRenderbuffer->Width;
841 return;
842 case GL_RENDERBUFFER_HEIGHT_EXT:
843 *params = ctx->CurrentRenderbuffer->Height;
844 return;
845 case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
846 *params = ctx->CurrentRenderbuffer->InternalFormat;
847 return;
848 case GL_RENDERBUFFER_RED_SIZE_EXT:
849 *params = ctx->CurrentRenderbuffer->RedBits;
850 break;
851 case GL_RENDERBUFFER_GREEN_SIZE_EXT:
852 *params = ctx->CurrentRenderbuffer->GreenBits;
853 break;
854 case GL_RENDERBUFFER_BLUE_SIZE_EXT:
855 *params = ctx->CurrentRenderbuffer->BlueBits;
856 break;
857 case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
858 *params = ctx->CurrentRenderbuffer->AlphaBits;
859 break;
860 case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
861 *params = ctx->CurrentRenderbuffer->DepthBits;
862 break;
863 case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
864 *params = ctx->CurrentRenderbuffer->StencilBits;
865 break;
866 default:
867 _mesa_error(ctx, GL_INVALID_ENUM,
868 "glGetRenderbufferParameterivEXT(target)");
869 return;
870 }
871 }
872
873
874 GLboolean GLAPIENTRY
875 _mesa_IsFramebufferEXT(GLuint framebuffer)
876 {
877 GET_CURRENT_CONTEXT(ctx);
878 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
879 if (framebuffer) {
880 struct gl_framebuffer *rb = _mesa_lookup_framebuffer(ctx, framebuffer);
881 if (rb != NULL && rb != &DummyFramebuffer)
882 return GL_TRUE;
883 }
884 return GL_FALSE;
885 }
886
887
888 static void
889 check_begin_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
890 {
891 GLuint i;
892 ASSERT(ctx->Driver.RenderTexture);
893 for (i = 0; i < BUFFER_COUNT; i++) {
894 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
895 struct gl_texture_object *texObj = att->Texture;
896 if (texObj
897 && att->Texture->Image[att->CubeMapFace][att->TextureLevel]) {
898 ctx->Driver.RenderTexture(ctx, fb, att);
899 }
900 }
901 }
902
903
904 /**
905 * Examine all the framebuffer's attachments to see if any are textures.
906 * If so, call ctx->Driver.FinishRenderTexture() for each texture to
907 * notify the device driver that the texture image may have changed.
908 */
909 static void
910 check_end_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
911 {
912 if (ctx->Driver.FinishRenderTexture) {
913 GLuint i;
914 for (i = 0; i < BUFFER_COUNT; i++) {
915 struct gl_renderbuffer_attachment *att = fb->Attachment + i;
916 struct gl_texture_object *texObj = att->Texture;
917 if (texObj) {
918 ctx->Driver.FinishRenderTexture(ctx, att);
919 }
920 }
921 }
922 }
923
924
925 void GLAPIENTRY
926 _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
927 {
928 struct gl_framebuffer *newFb, *newFbread;
929 GLboolean bindReadBuf, bindDrawBuf;
930 GET_CURRENT_CONTEXT(ctx);
931
932 ASSERT_OUTSIDE_BEGIN_END(ctx);
933
934 if (!ctx->Extensions.EXT_framebuffer_object) {
935 _mesa_error(ctx, GL_INVALID_OPERATION,
936 "glBindFramebufferEXT(unsupported)");
937 return;
938 }
939
940 switch (target) {
941 #if FEATURE_EXT_framebuffer_blit
942 case GL_DRAW_FRAMEBUFFER_EXT:
943 if (!ctx->Extensions.EXT_framebuffer_blit) {
944 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
945 return;
946 }
947 bindDrawBuf = GL_TRUE;
948 bindReadBuf = GL_FALSE;
949 break;
950 case GL_READ_FRAMEBUFFER_EXT:
951 if (!ctx->Extensions.EXT_framebuffer_blit) {
952 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
953 return;
954 }
955 bindDrawBuf = GL_FALSE;
956 bindReadBuf = GL_TRUE;
957 break;
958 #endif
959 case GL_FRAMEBUFFER_EXT:
960 bindDrawBuf = GL_TRUE;
961 bindReadBuf = GL_TRUE;
962 break;
963 default:
964 _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
965 return;
966 }
967
968 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
969 if (ctx->Driver.Flush) {
970 ctx->Driver.Flush(ctx);
971 }
972 if (framebuffer) {
973 /* Binding a user-created framebuffer object */
974 newFb = _mesa_lookup_framebuffer(ctx, framebuffer);
975 if (newFb == &DummyFramebuffer) {
976 /* ID was reserved, but no real framebuffer object made yet */
977 newFb = NULL;
978 }
979 if (!newFb) {
980 /* create new framebuffer object */
981 newFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
982 if (!newFb) {
983 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
984 return;
985 }
986 _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newFb);
987 }
988 newFbread = newFb;
989 }
990 else {
991 /* Binding the window system framebuffer (which was originally set
992 * with MakeCurrent).
993 */
994 newFb = ctx->WinSysDrawBuffer;
995 newFbread = ctx->WinSysReadBuffer;
996 }
997
998 ASSERT(newFb);
999 ASSERT(newFb != &DummyFramebuffer);
1000
1001 /*
1002 * XXX check if re-binding same buffer and skip some of this code.
1003 */
1004
1005 /* for window-framebuffers, re-initialize the fbo values, as they
1006 could be wrong (makecurrent with a new drawable while still a fbo
1007 was bound will lead to default init fbo values).
1008 note that therefore the context ReadBuffer/DrawBuffer values are not
1009 valid while fbo's are bound!!! */
1010 if (bindReadBuf) {
1011 _mesa_reference_framebuffer(&ctx->ReadBuffer, newFbread);
1012 if (!newFbread->Name) {
1013 _mesa_readbuffer_update_fields(ctx, ctx->Pixel.ReadBuffer);
1014 }
1015 }
1016
1017 if (bindDrawBuf) {
1018 /* check if old FB had any texture attachments */
1019 check_end_texture_render(ctx, ctx->DrawBuffer);
1020 /* check if time to delete this framebuffer */
1021 _mesa_reference_framebuffer(&ctx->DrawBuffer, newFb);
1022 if (!newFb->Name) {
1023 GLuint i;
1024 GLenum buffers[MAX_DRAW_BUFFERS];
1025 for(i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
1026 buffers[i] = ctx->Color.DrawBuffer[i];
1027 }
1028 _mesa_drawbuffers(ctx, ctx->Const.MaxDrawBuffers, buffers, NULL);
1029 }
1030 else {
1031 /* check if newly bound framebuffer has any texture attachments */
1032 check_begin_texture_render(ctx, newFb);
1033 }
1034 }
1035
1036 if (ctx->Driver.BindFramebuffer) {
1037 ctx->Driver.BindFramebuffer(ctx, target, newFb, newFbread);
1038 }
1039 }
1040
1041
1042 void GLAPIENTRY
1043 _mesa_DeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
1044 {
1045 GLint i;
1046 GET_CURRENT_CONTEXT(ctx);
1047
1048 ASSERT_OUTSIDE_BEGIN_END(ctx);
1049 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1050 /* The above doesn't fully flush the drivers in the way that a
1051 * glFlush does, but that is required here:
1052 */
1053 if (ctx->Driver.Flush)
1054 ctx->Driver.Flush(ctx);
1055
1056 for (i = 0; i < n; i++) {
1057 if (framebuffers[i] > 0) {
1058 struct gl_framebuffer *fb;
1059 fb = _mesa_lookup_framebuffer(ctx, framebuffers[i]);
1060 if (fb) {
1061 ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
1062
1063 /* check if deleting currently bound framebuffer object */
1064 if (fb == ctx->DrawBuffer) {
1065 /* bind default */
1066 ASSERT(fb->RefCount >= 2);
1067 _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
1068 }
1069
1070 /* remove from hash table immediately, to free the ID */
1071 _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
1072
1073 if (fb != &DummyFramebuffer) {
1074 /* But the object will not be freed until it's no longer
1075 * bound in any context.
1076 */
1077 _mesa_unreference_framebuffer(&fb);
1078 }
1079 }
1080 }
1081 }
1082 }
1083
1084
1085 void GLAPIENTRY
1086 _mesa_GenFramebuffersEXT(GLsizei n, GLuint *framebuffers)
1087 {
1088 GET_CURRENT_CONTEXT(ctx);
1089 GLuint first;
1090 GLint i;
1091
1092 ASSERT_OUTSIDE_BEGIN_END(ctx);
1093
1094 if (n < 0) {
1095 _mesa_error(ctx, GL_INVALID_VALUE, "glGenFramebuffersEXT(n)");
1096 return;
1097 }
1098
1099 if (!framebuffers)
1100 return;
1101
1102 first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
1103
1104 for (i = 0; i < n; i++) {
1105 GLuint name = first + i;
1106 framebuffers[i] = name;
1107 /* insert dummy placeholder into hash table */
1108 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
1109 _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
1110 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
1111 }
1112 }
1113
1114
1115
1116 GLenum GLAPIENTRY
1117 _mesa_CheckFramebufferStatusEXT(GLenum target)
1118 {
1119 struct gl_framebuffer *buffer;
1120 GET_CURRENT_CONTEXT(ctx);
1121
1122 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
1123
1124 switch (target) {
1125 #if FEATURE_EXT_framebuffer_blit
1126 case GL_DRAW_FRAMEBUFFER_EXT:
1127 if (!ctx->Extensions.EXT_framebuffer_blit) {
1128 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1129 return 0;
1130 }
1131 buffer = ctx->DrawBuffer;
1132 break;
1133 case GL_READ_FRAMEBUFFER_EXT:
1134 if (!ctx->Extensions.EXT_framebuffer_blit) {
1135 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1136 return 0;
1137 }
1138 buffer = ctx->ReadBuffer;
1139 break;
1140 #endif
1141 case GL_FRAMEBUFFER_EXT:
1142 buffer = ctx->DrawBuffer;
1143 break;
1144 default:
1145 _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
1146 return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
1147 }
1148
1149 if (buffer->Name == 0) {
1150 /* The window system / default framebuffer is always complete */
1151 return GL_FRAMEBUFFER_COMPLETE_EXT;
1152 }
1153
1154 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1155
1156 _mesa_test_framebuffer_completeness(ctx, buffer);
1157 return buffer->_Status;
1158 }
1159
1160
1161
1162 /**
1163 * Common code called by glFramebufferTexture1D/2D/3DEXT().
1164 */
1165 static void
1166 framebuffer_texture(GLcontext *ctx, const char *caller, GLenum target,
1167 GLenum attachment, GLenum textarget, GLuint texture,
1168 GLint level, GLint zoffset)
1169 {
1170 struct gl_renderbuffer_attachment *att;
1171 struct gl_texture_object *texObj = NULL;
1172 struct gl_framebuffer *fb;
1173
1174 ASSERT_OUTSIDE_BEGIN_END(ctx);
1175
1176 if (target != GL_FRAMEBUFFER_EXT) {
1177 _mesa_error(ctx, GL_INVALID_ENUM,
1178 "glFramebufferTexture%sEXT(target)", caller);
1179 return;
1180 }
1181
1182 fb = ctx->DrawBuffer;
1183 ASSERT(fb);
1184
1185 /* check framebuffer binding */
1186 if (fb->Name == 0) {
1187 _mesa_error(ctx, GL_INVALID_OPERATION,
1188 "glFramebufferTexture%sEXT", caller);
1189 return;
1190 }
1191
1192
1193 /* The textarget, level, and zoffset parameters are only validated if
1194 * texture is non-zero.
1195 */
1196 if (texture) {
1197 GLboolean err = GL_TRUE;
1198
1199 texObj = _mesa_lookup_texture(ctx, texture);
1200 if (texObj != NULL) {
1201 if (textarget == 0) {
1202 err = (texObj->Target != GL_TEXTURE_3D) &&
1203 (texObj->Target != GL_TEXTURE_1D_ARRAY_EXT) &&
1204 (texObj->Target != GL_TEXTURE_2D_ARRAY_EXT);
1205 }
1206 else {
1207 err = (texObj->Target == GL_TEXTURE_CUBE_MAP)
1208 ? !IS_CUBE_FACE(textarget)
1209 : (texObj->Target != textarget);
1210 }
1211 }
1212
1213 if (err) {
1214 _mesa_error(ctx, GL_INVALID_OPERATION,
1215 "glFramebufferTexture%sEXT(texture target mismatch)",
1216 caller);
1217 return;
1218 }
1219
1220 if (texObj->Target == GL_TEXTURE_3D) {
1221 const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
1222 if (zoffset < 0 || zoffset >= maxSize) {
1223 _mesa_error(ctx, GL_INVALID_VALUE,
1224 "glFramebufferTexture%sEXT(zoffset)", caller);
1225 return;
1226 }
1227 }
1228 else if ((texObj->Target == GL_TEXTURE_1D_ARRAY_EXT) ||
1229 (texObj->Target == GL_TEXTURE_2D_ARRAY_EXT)) {
1230 if (zoffset < 0 || zoffset >= ctx->Const.MaxArrayTextureLayers) {
1231 _mesa_error(ctx, GL_INVALID_VALUE,
1232 "glFramebufferTexture%sEXT(layer)", caller);
1233 return;
1234 }
1235 }
1236
1237
1238 if ((level < 0) ||
1239 (level >= _mesa_max_texture_levels(ctx, texObj->Target))) {
1240 _mesa_error(ctx, GL_INVALID_VALUE,
1241 "glFramebufferTexture%sEXT(level)", caller);
1242 return;
1243 }
1244 }
1245
1246 att = _mesa_get_attachment(ctx, fb, attachment);
1247 if (att == NULL) {
1248 _mesa_error(ctx, GL_INVALID_ENUM,
1249 "glFramebufferTexture%sEXT(attachment)", caller);
1250 return;
1251 }
1252
1253 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1254 /* The above doesn't fully flush the drivers in the way that a
1255 * glFlush does, but that is required here:
1256 */
1257 if (ctx->Driver.Flush)
1258 ctx->Driver.Flush(ctx);
1259
1260 _glthread_LOCK_MUTEX(fb->Mutex);
1261 if (texObj) {
1262 _mesa_set_texture_attachment(ctx, fb, att, texObj, textarget,
1263 level, zoffset);
1264 }
1265 else {
1266 _mesa_remove_attachment(ctx, att);
1267 }
1268 _glthread_UNLOCK_MUTEX(fb->Mutex);
1269 }
1270
1271
1272
1273 void GLAPIENTRY
1274 _mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
1275 GLenum textarget, GLuint texture, GLint level)
1276 {
1277 GET_CURRENT_CONTEXT(ctx);
1278
1279 if ((texture != 0) && (textarget != GL_TEXTURE_1D)) {
1280 _mesa_error(ctx, GL_INVALID_ENUM,
1281 "glFramebufferTexture1DEXT(textarget)");
1282 return;
1283 }
1284
1285 framebuffer_texture(ctx, "1D", target, attachment, textarget, texture,
1286 level, 0);
1287 }
1288
1289
1290 void GLAPIENTRY
1291 _mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
1292 GLenum textarget, GLuint texture, GLint level)
1293 {
1294 GET_CURRENT_CONTEXT(ctx);
1295
1296 if ((texture != 0) &&
1297 (textarget != GL_TEXTURE_2D) &&
1298 (textarget != GL_TEXTURE_RECTANGLE_ARB) &&
1299 (!IS_CUBE_FACE(textarget))) {
1300 _mesa_error(ctx, GL_INVALID_OPERATION,
1301 "glFramebufferTexture2DEXT(textarget)");
1302 return;
1303 }
1304
1305 framebuffer_texture(ctx, "2D", target, attachment, textarget, texture,
1306 level, 0);
1307 }
1308
1309
1310 void GLAPIENTRY
1311 _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
1312 GLenum textarget, GLuint texture,
1313 GLint level, GLint zoffset)
1314 {
1315 GET_CURRENT_CONTEXT(ctx);
1316
1317 if ((texture != 0) && (textarget != GL_TEXTURE_3D)) {
1318 _mesa_error(ctx, GL_INVALID_ENUM,
1319 "glFramebufferTexture3DEXT(textarget)");
1320 return;
1321 }
1322
1323 framebuffer_texture(ctx, "3D", target, attachment, textarget, texture,
1324 level, zoffset);
1325 }
1326
1327
1328 void GLAPIENTRY
1329 _mesa_FramebufferTextureLayerEXT(GLenum target, GLenum attachment,
1330 GLuint texture, GLint level, GLint layer)
1331 {
1332 GET_CURRENT_CONTEXT(ctx);
1333
1334 framebuffer_texture(ctx, "Layer", target, attachment, 0, texture,
1335 level, layer);
1336 }
1337
1338
1339 void GLAPIENTRY
1340 _mesa_FramebufferRenderbufferEXT(GLenum target, GLenum attachment,
1341 GLenum renderbufferTarget,
1342 GLuint renderbuffer)
1343 {
1344 struct gl_renderbuffer_attachment *att;
1345 struct gl_framebuffer *fb;
1346 struct gl_renderbuffer *rb;
1347 GET_CURRENT_CONTEXT(ctx);
1348
1349 ASSERT_OUTSIDE_BEGIN_END(ctx);
1350
1351 switch (target) {
1352 #if FEATURE_EXT_framebuffer_blit
1353 case GL_DRAW_FRAMEBUFFER_EXT:
1354 if (!ctx->Extensions.EXT_framebuffer_blit) {
1355 _mesa_error(ctx, GL_INVALID_ENUM,
1356 "glFramebufferRenderbufferEXT(target)");
1357 return;
1358 }
1359 fb = ctx->DrawBuffer;
1360 break;
1361 case GL_READ_FRAMEBUFFER_EXT:
1362 if (!ctx->Extensions.EXT_framebuffer_blit) {
1363 _mesa_error(ctx, GL_INVALID_ENUM,
1364 "glFramebufferRenderbufferEXT(target)");
1365 return;
1366 }
1367 fb = ctx->ReadBuffer;
1368 break;
1369 #endif
1370 case GL_FRAMEBUFFER_EXT:
1371 fb = ctx->DrawBuffer;
1372 break;
1373 default:
1374 _mesa_error(ctx, GL_INVALID_ENUM,
1375 "glFramebufferRenderbufferEXT(target)");
1376 return;
1377 }
1378
1379 if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
1380 _mesa_error(ctx, GL_INVALID_ENUM,
1381 "glFramebufferRenderbufferEXT(renderbufferTarget)");
1382 return;
1383 }
1384
1385 if (fb->Name == 0) {
1386 /* Can't attach new renderbuffers to a window system framebuffer */
1387 _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
1388 return;
1389 }
1390
1391 att = _mesa_get_attachment(ctx, fb, attachment);
1392 if (att == NULL) {
1393 _mesa_error(ctx, GL_INVALID_ENUM,
1394 "glFramebufferRenderbufferEXT(attachment)");
1395 return;
1396 }
1397
1398 if (renderbuffer) {
1399 rb = _mesa_lookup_renderbuffer(ctx, renderbuffer);
1400 if (!rb) {
1401 _mesa_error(ctx, GL_INVALID_OPERATION,
1402 "glFramebufferRenderbufferEXT(renderbuffer)");
1403 return;
1404 }
1405 }
1406 else {
1407 /* remove renderbuffer attachment */
1408 rb = NULL;
1409 }
1410
1411 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1412 /* The above doesn't fully flush the drivers in the way that a
1413 * glFlush does, but that is required here:
1414 */
1415 if (ctx->Driver.Flush)
1416 ctx->Driver.Flush(ctx);
1417
1418 assert(ctx->Driver.FramebufferRenderbuffer);
1419 ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
1420
1421 /* Some subsequent GL commands may depend on the framebuffer's visual
1422 * after the binding is updated. Update visual info now.
1423 */
1424 _mesa_update_framebuffer_visual(fb);
1425 }
1426
1427
1428 void GLAPIENTRY
1429 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment,
1430 GLenum pname, GLint *params)
1431 {
1432 const struct gl_renderbuffer_attachment *att;
1433 struct gl_framebuffer *buffer;
1434 GET_CURRENT_CONTEXT(ctx);
1435
1436 ASSERT_OUTSIDE_BEGIN_END(ctx);
1437
1438 switch (target) {
1439 #if FEATURE_EXT_framebuffer_blit
1440 case GL_DRAW_FRAMEBUFFER_EXT:
1441 if (!ctx->Extensions.EXT_framebuffer_blit) {
1442 _mesa_error(ctx, GL_INVALID_ENUM,
1443 "glGetFramebufferAttachmentParameterivEXT(target)");
1444 return;
1445 }
1446 buffer = ctx->DrawBuffer;
1447 break;
1448 case GL_READ_FRAMEBUFFER_EXT:
1449 if (!ctx->Extensions.EXT_framebuffer_blit) {
1450 _mesa_error(ctx, GL_INVALID_ENUM,
1451 "glGetFramebufferAttachmentParameterivEXT(target)");
1452 return;
1453 }
1454 buffer = ctx->ReadBuffer;
1455 break;
1456 #endif
1457 case GL_FRAMEBUFFER_EXT:
1458 buffer = ctx->DrawBuffer;
1459 break;
1460 default:
1461 _mesa_error(ctx, GL_INVALID_ENUM,
1462 "glGetFramebufferAttachmentParameterivEXT(target)");
1463 return;
1464 }
1465
1466 if (buffer->Name == 0) {
1467 _mesa_error(ctx, GL_INVALID_OPERATION,
1468 "glGetFramebufferAttachmentParameterivEXT");
1469 return;
1470 }
1471
1472 att = _mesa_get_attachment(ctx, buffer, attachment);
1473 if (att == NULL) {
1474 _mesa_error(ctx, GL_INVALID_ENUM,
1475 "glGetFramebufferAttachmentParameterivEXT(attachment)");
1476 return;
1477 }
1478
1479 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1480 /* The above doesn't fully flush the drivers in the way that a
1481 * glFlush does, but that is required here:
1482 */
1483 if (ctx->Driver.Flush)
1484 ctx->Driver.Flush(ctx);
1485
1486 switch (pname) {
1487 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
1488 *params = att->Type;
1489 return;
1490 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT:
1491 if (att->Type == GL_RENDERBUFFER_EXT) {
1492 *params = att->Renderbuffer->Name;
1493 }
1494 else if (att->Type == GL_TEXTURE) {
1495 *params = att->Texture->Name;
1496 }
1497 else {
1498 _mesa_error(ctx, GL_INVALID_ENUM,
1499 "glGetFramebufferAttachmentParameterivEXT(pname)");
1500 }
1501 return;
1502 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT:
1503 if (att->Type == GL_TEXTURE) {
1504 *params = att->TextureLevel;
1505 }
1506 else {
1507 _mesa_error(ctx, GL_INVALID_ENUM,
1508 "glGetFramebufferAttachmentParameterivEXT(pname)");
1509 }
1510 return;
1511 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT:
1512 if (att->Type == GL_TEXTURE) {
1513 *params = GL_TEXTURE_CUBE_MAP_POSITIVE_X + att->CubeMapFace;
1514 }
1515 else {
1516 _mesa_error(ctx, GL_INVALID_ENUM,
1517 "glGetFramebufferAttachmentParameterivEXT(pname)");
1518 }
1519 return;
1520 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT:
1521 if (att->Type == GL_TEXTURE) {
1522 *params = att->Zoffset;
1523 }
1524 else {
1525 _mesa_error(ctx, GL_INVALID_ENUM,
1526 "glGetFramebufferAttachmentParameterivEXT(pname)");
1527 }
1528 return;
1529 default:
1530 _mesa_error(ctx, GL_INVALID_ENUM,
1531 "glGetFramebufferAttachmentParameterivEXT(pname)");
1532 return;
1533 }
1534 }
1535
1536
1537 void GLAPIENTRY
1538 _mesa_GenerateMipmapEXT(GLenum target)
1539 {
1540 struct gl_texture_unit *texUnit;
1541 struct gl_texture_object *texObj;
1542 GET_CURRENT_CONTEXT(ctx);
1543
1544 ASSERT_OUTSIDE_BEGIN_END(ctx);
1545 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1546
1547 switch (target) {
1548 case GL_TEXTURE_1D:
1549 case GL_TEXTURE_2D:
1550 case GL_TEXTURE_3D:
1551 case GL_TEXTURE_CUBE_MAP:
1552 /* OK, legal value */
1553 break;
1554 default:
1555 _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target)");
1556 return;
1557 }
1558
1559 texUnit = &ctx->Texture.Unit[ctx->Texture.CurrentUnit];
1560 texObj = _mesa_select_tex_object(ctx, texUnit, target);
1561
1562 /* XXX this might not handle cube maps correctly */
1563 _mesa_lock_texture(ctx, texObj);
1564 _mesa_generate_mipmap(ctx, target, texUnit, texObj);
1565 _mesa_unlock_texture(ctx, texObj);
1566 }
1567
1568
1569 #if FEATURE_EXT_framebuffer_blit
1570 void GLAPIENTRY
1571 _mesa_BlitFramebufferEXT(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
1572 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
1573 GLbitfield mask, GLenum filter)
1574 {
1575 GET_CURRENT_CONTEXT(ctx);
1576
1577 ASSERT_OUTSIDE_BEGIN_END(ctx);
1578 FLUSH_VERTICES(ctx, _NEW_BUFFERS);
1579
1580 if (ctx->NewState) {
1581 _mesa_update_state(ctx);
1582 }
1583
1584 if (!ctx->ReadBuffer) {
1585 /* XXX */
1586 }
1587
1588 /* check for complete framebuffers */
1589 if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
1590 ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
1591 _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
1592 "glBlitFramebufferEXT(incomplete draw/read buffers)");
1593 return;
1594 }
1595
1596 if (filter != GL_NEAREST && filter != GL_LINEAR) {
1597 _mesa_error(ctx, GL_INVALID_ENUM, "glBlitFramebufferEXT(filter)");
1598 return;
1599 }
1600
1601 if (mask & ~(GL_COLOR_BUFFER_BIT |
1602 GL_DEPTH_BUFFER_BIT |
1603 GL_STENCIL_BUFFER_BIT)) {
1604 _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
1605 return;
1606 }
1607
1608 /* depth/stencil must be blitted with nearest filtering */
1609 if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
1610 && filter != GL_NEAREST) {
1611 _mesa_error(ctx, GL_INVALID_OPERATION,
1612 "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
1613 return;
1614 }
1615
1616 if (mask & GL_STENCIL_BUFFER_BIT) {
1617 struct gl_renderbuffer *readRb = ctx->ReadBuffer->_StencilBuffer;
1618 struct gl_renderbuffer *drawRb = ctx->DrawBuffer->_StencilBuffer;
1619 if (readRb->StencilBits != drawRb->StencilBits) {
1620 _mesa_error(ctx, GL_INVALID_OPERATION,
1621 "glBlitFramebufferEXT(stencil buffer size mismatch");
1622 return;
1623 }
1624 }
1625
1626 if (mask & GL_DEPTH_BUFFER_BIT) {
1627 struct gl_renderbuffer *readRb = ctx->ReadBuffer->_DepthBuffer;
1628 struct gl_renderbuffer *drawRb = ctx->DrawBuffer->_DepthBuffer;
1629 if (readRb->DepthBits != drawRb->DepthBits) {
1630 _mesa_error(ctx, GL_INVALID_OPERATION,
1631 "glBlitFramebufferEXT(depth buffer size mismatch");
1632 return;
1633 }
1634 }
1635
1636 if (!ctx->Extensions.EXT_framebuffer_blit) {
1637 _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
1638 return;
1639 }
1640
1641 ASSERT(ctx->Driver.BlitFramebuffer);
1642 ctx->Driver.BlitFramebuffer(ctx,
1643 srcX0, srcY0, srcX1, srcY1,
1644 dstX0, dstY0, dstX1, dstY1,
1645 mask, filter);
1646 }
1647 #endif /* FEATURE_EXT_framebuffer_blit */