Merge branch 'mesa_7_5_branch'
[mesa.git] / src / mesa / drivers / dri / intel / intel_fbo.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * 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
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #include "main/imports.h"
30 #include "main/macros.h"
31 #include "main/mtypes.h"
32 #include "main/fbobject.h"
33 #include "main/framebuffer.h"
34 #include "main/renderbuffer.h"
35 #include "main/context.h"
36 #include "main/texformat.h"
37 #include "main/texrender.h"
38
39 #include "intel_context.h"
40 #include "intel_buffers.h"
41 #include "intel_fbo.h"
42 #include "intel_mipmap_tree.h"
43 #include "intel_regions.h"
44
45
46 #define FILE_DEBUG_FLAG DEBUG_FBO
47
48
49 /**
50 * Create a new framebuffer object.
51 */
52 static struct gl_framebuffer *
53 intel_new_framebuffer(GLcontext * ctx, GLuint name)
54 {
55 /* Only drawable state in intel_framebuffer at this time, just use Mesa's
56 * class
57 */
58 return _mesa_new_framebuffer(ctx, name);
59 }
60
61
62 /** Called by gl_renderbuffer::Delete() */
63 static void
64 intel_delete_renderbuffer(struct gl_renderbuffer *rb)
65 {
66 GET_CURRENT_CONTEXT(ctx);
67 struct intel_context *intel = intel_context(ctx);
68 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
69
70 ASSERT(irb);
71
72 if (irb->span_cache != NULL)
73 _mesa_free(irb->span_cache);
74
75 if (intel && irb->region) {
76 intel_region_release(&irb->region);
77 }
78
79 _mesa_free(irb);
80 }
81
82
83 /**
84 * Return a pointer to a specific pixel in a renderbuffer.
85 */
86 static void *
87 intel_get_pointer(GLcontext * ctx, struct gl_renderbuffer *rb,
88 GLint x, GLint y)
89 {
90 /* By returning NULL we force all software rendering to go through
91 * the span routines.
92 */
93 return NULL;
94 }
95
96
97 /**
98 * Called via glRenderbufferStorageEXT() to set the format and allocate
99 * storage for a user-created renderbuffer.
100 */
101 static GLboolean
102 intel_alloc_renderbuffer_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
103 GLenum internalFormat,
104 GLuint width, GLuint height)
105 {
106 struct intel_context *intel = intel_context(ctx);
107 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
108 GLboolean softwareBuffer = GL_FALSE;
109 int cpp;
110
111 ASSERT(rb->Name != 0);
112
113 switch (internalFormat) {
114 case GL_R3_G3_B2:
115 case GL_RGB4:
116 case GL_RGB5:
117 rb->_ActualFormat = GL_RGB5;
118 rb->DataType = GL_UNSIGNED_BYTE;
119 rb->RedBits = 5;
120 rb->GreenBits = 6;
121 rb->BlueBits = 5;
122 irb->texformat = &_mesa_texformat_rgb565;
123 cpp = 2;
124 break;
125 case GL_RGB:
126 case GL_RGB8:
127 case GL_RGB10:
128 case GL_RGB12:
129 case GL_RGB16:
130 rb->_ActualFormat = GL_RGB8;
131 rb->DataType = GL_UNSIGNED_BYTE;
132 rb->RedBits = 8;
133 rb->GreenBits = 8;
134 rb->BlueBits = 8;
135 rb->AlphaBits = 0;
136 irb->texformat = &_mesa_texformat_argb8888; /* XXX: Need xrgb8888 */
137 cpp = 4;
138 break;
139 case GL_RGBA:
140 case GL_RGBA2:
141 case GL_RGBA4:
142 case GL_RGB5_A1:
143 case GL_RGBA8:
144 case GL_RGB10_A2:
145 case GL_RGBA12:
146 case GL_RGBA16:
147 rb->_ActualFormat = GL_RGBA8;
148 rb->DataType = GL_UNSIGNED_BYTE;
149 rb->RedBits = 8;
150 rb->GreenBits = 8;
151 rb->BlueBits = 8;
152 rb->AlphaBits = 8;
153 irb->texformat = &_mesa_texformat_argb8888;
154 cpp = 4;
155 break;
156 case GL_STENCIL_INDEX:
157 case GL_STENCIL_INDEX1_EXT:
158 case GL_STENCIL_INDEX4_EXT:
159 case GL_STENCIL_INDEX8_EXT:
160 case GL_STENCIL_INDEX16_EXT:
161 /* alloc a depth+stencil buffer */
162 rb->_ActualFormat = GL_DEPTH24_STENCIL8_EXT;
163 rb->DataType = GL_UNSIGNED_INT_24_8_EXT;
164 rb->StencilBits = 8;
165 cpp = 4;
166 irb->texformat = &_mesa_texformat_s8_z24;
167 break;
168 case GL_DEPTH_COMPONENT16:
169 rb->_ActualFormat = GL_DEPTH_COMPONENT16;
170 rb->DataType = GL_UNSIGNED_SHORT;
171 rb->DepthBits = 16;
172 cpp = 2;
173 irb->texformat = &_mesa_texformat_z16;
174 break;
175 case GL_DEPTH_COMPONENT:
176 case GL_DEPTH_COMPONENT24:
177 case GL_DEPTH_COMPONENT32:
178 rb->_ActualFormat = GL_DEPTH24_STENCIL8_EXT;
179 rb->DataType = GL_UNSIGNED_INT_24_8_EXT;
180 rb->DepthBits = 24;
181 cpp = 4;
182 irb->texformat = &_mesa_texformat_s8_z24;
183 break;
184 case GL_DEPTH_STENCIL_EXT:
185 case GL_DEPTH24_STENCIL8_EXT:
186 rb->_ActualFormat = GL_DEPTH24_STENCIL8_EXT;
187 rb->DataType = GL_UNSIGNED_INT_24_8_EXT;
188 rb->DepthBits = 24;
189 rb->StencilBits = 8;
190 cpp = 4;
191 irb->texformat = &_mesa_texformat_s8_z24;
192 break;
193 default:
194 _mesa_problem(ctx,
195 "Unexpected format in intel_alloc_renderbuffer_storage");
196 return GL_FALSE;
197 }
198
199 intelFlush(ctx);
200
201 /* free old region */
202 if (irb->region) {
203 intel_region_release(&irb->region);
204 }
205
206 /* allocate new memory region/renderbuffer */
207 if (softwareBuffer) {
208 return _mesa_soft_renderbuffer_storage(ctx, rb, internalFormat,
209 width, height);
210 }
211 else {
212 /* Choose a pitch to match hardware requirements:
213 */
214 GLuint pitch = ((cpp * width + 63) & ~63) / cpp;
215
216 /* alloc hardware renderbuffer */
217 DBG("Allocating %d x %d Intel RBO (pitch %d)\n", width,
218 height, pitch);
219
220 irb->region = intel_region_alloc(intel, I915_TILING_NONE,
221 cpp, width, height, pitch,
222 GL_TRUE);
223 if (!irb->region)
224 return GL_FALSE; /* out of memory? */
225
226 ASSERT(irb->region->buffer);
227
228 rb->Width = width;
229 rb->Height = height;
230
231 return GL_TRUE;
232 }
233 }
234
235
236 /**
237 * Called for each hardware renderbuffer when a _window_ is resized.
238 * Just update fields.
239 * Not used for user-created renderbuffers!
240 */
241 static GLboolean
242 intel_alloc_window_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
243 GLenum internalFormat, GLuint width, GLuint height)
244 {
245 ASSERT(rb->Name == 0);
246 rb->Width = width;
247 rb->Height = height;
248 rb->_ActualFormat = internalFormat;
249
250 return GL_TRUE;
251 }
252
253
254 static void
255 intel_resize_buffers(GLcontext *ctx, struct gl_framebuffer *fb,
256 GLuint width, GLuint height)
257 {
258 struct intel_framebuffer *intel_fb = (struct intel_framebuffer*)fb;
259 int i;
260
261 _mesa_resize_framebuffer(ctx, fb, width, height);
262
263 fb->Initialized = GL_TRUE; /* XXX remove someday */
264
265 if (fb->Name != 0) {
266 return;
267 }
268
269 /* Make sure all window system renderbuffers are up to date */
270 for (i = 0; i < 2; i++) {
271 struct gl_renderbuffer *rb = &intel_fb->color_rb[i]->Base;
272
273 /* only resize if size is changing */
274 if (rb && (rb->Width != width || rb->Height != height)) {
275 rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height);
276 }
277 }
278 }
279
280
281 /** Dummy function for gl_renderbuffer::AllocStorage() */
282 static GLboolean
283 intel_nop_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
284 GLenum internalFormat, GLuint width, GLuint height)
285 {
286 _mesa_problem(ctx, "intel_op_alloc_storage should never be called.");
287 return GL_FALSE;
288 }
289
290
291 void
292 intel_renderbuffer_set_region(struct intel_renderbuffer *rb,
293 struct intel_region *region)
294 {
295 struct intel_region *old;
296
297 old = rb->region;
298 rb->region = NULL;
299 intel_region_reference(&rb->region, region);
300 intel_region_release(&old);
301 }
302
303
304 /**
305 * Create a new intel_renderbuffer which corresponds to an on-screen window,
306 * not a user-created renderbuffer.
307 */
308 struct intel_renderbuffer *
309 intel_create_renderbuffer(GLenum intFormat)
310 {
311 GET_CURRENT_CONTEXT(ctx);
312
313 struct intel_renderbuffer *irb;
314 const GLuint name = 0;
315
316 irb = CALLOC_STRUCT(intel_renderbuffer);
317 if (!irb) {
318 _mesa_error(ctx, GL_OUT_OF_MEMORY, "creating renderbuffer");
319 return NULL;
320 }
321
322 _mesa_init_renderbuffer(&irb->Base, name);
323 irb->Base.ClassID = INTEL_RB_CLASS;
324
325 switch (intFormat) {
326 case GL_RGB5:
327 irb->Base._ActualFormat = GL_RGB5;
328 irb->Base._BaseFormat = GL_RGBA;
329 irb->Base.RedBits = 5;
330 irb->Base.GreenBits = 6;
331 irb->Base.BlueBits = 5;
332 irb->Base.DataType = GL_UNSIGNED_BYTE;
333 irb->texformat = &_mesa_texformat_rgb565;
334 break;
335 case GL_RGB8:
336 irb->Base._ActualFormat = GL_RGB8;
337 irb->Base._BaseFormat = GL_RGB;
338 irb->Base.RedBits = 8;
339 irb->Base.GreenBits = 8;
340 irb->Base.BlueBits = 8;
341 irb->Base.AlphaBits = 0;
342 irb->Base.DataType = GL_UNSIGNED_BYTE;
343 irb->texformat = &_mesa_texformat_argb8888; /* XXX: Need xrgb8888 */
344 break;
345 case GL_RGBA8:
346 irb->Base._ActualFormat = GL_RGBA8;
347 irb->Base._BaseFormat = GL_RGBA;
348 irb->Base.RedBits = 8;
349 irb->Base.GreenBits = 8;
350 irb->Base.BlueBits = 8;
351 irb->Base.AlphaBits = 8;
352 irb->Base.DataType = GL_UNSIGNED_BYTE;
353 irb->texformat = &_mesa_texformat_argb8888;
354 break;
355 case GL_STENCIL_INDEX8_EXT:
356 irb->Base._ActualFormat = GL_STENCIL_INDEX8_EXT;
357 irb->Base._BaseFormat = GL_STENCIL_INDEX;
358 irb->Base.StencilBits = 8;
359 irb->Base.DataType = GL_UNSIGNED_BYTE;
360 irb->texformat = &_mesa_texformat_s8_z24;
361 break;
362 case GL_DEPTH_COMPONENT16:
363 irb->Base._ActualFormat = GL_DEPTH_COMPONENT16;
364 irb->Base._BaseFormat = GL_DEPTH_COMPONENT;
365 irb->Base.DepthBits = 16;
366 irb->Base.DataType = GL_UNSIGNED_SHORT;
367 irb->texformat = &_mesa_texformat_z16;
368 break;
369 case GL_DEPTH_COMPONENT24:
370 irb->Base._ActualFormat = GL_DEPTH24_STENCIL8_EXT;
371 irb->Base._BaseFormat = GL_DEPTH_COMPONENT;
372 irb->Base.DepthBits = 24;
373 irb->Base.DataType = GL_UNSIGNED_INT;
374 irb->texformat = &_mesa_texformat_s8_z24;
375 break;
376 case GL_DEPTH24_STENCIL8_EXT:
377 irb->Base._ActualFormat = GL_DEPTH24_STENCIL8_EXT;
378 irb->Base._BaseFormat = GL_DEPTH_STENCIL_EXT;
379 irb->Base.DepthBits = 24;
380 irb->Base.StencilBits = 8;
381 irb->Base.DataType = GL_UNSIGNED_INT_24_8_EXT;
382 irb->texformat = &_mesa_texformat_s8_z24;
383 break;
384 default:
385 _mesa_problem(NULL,
386 "Unexpected intFormat in intel_create_renderbuffer");
387 return NULL;
388 }
389
390 irb->Base.InternalFormat = intFormat;
391
392 /* intel-specific methods */
393 irb->Base.Delete = intel_delete_renderbuffer;
394 irb->Base.AllocStorage = intel_alloc_window_storage;
395 irb->Base.GetPointer = intel_get_pointer;
396
397 return irb;
398 }
399
400
401 /**
402 * Create a new renderbuffer object.
403 * Typically called via glBindRenderbufferEXT().
404 */
405 static struct gl_renderbuffer *
406 intel_new_renderbuffer(GLcontext * ctx, GLuint name)
407 {
408 /*struct intel_context *intel = intel_context(ctx); */
409 struct intel_renderbuffer *irb;
410
411 irb = CALLOC_STRUCT(intel_renderbuffer);
412 if (!irb) {
413 _mesa_error(ctx, GL_OUT_OF_MEMORY, "creating renderbuffer");
414 return NULL;
415 }
416
417 _mesa_init_renderbuffer(&irb->Base, name);
418 irb->Base.ClassID = INTEL_RB_CLASS;
419
420 /* intel-specific methods */
421 irb->Base.Delete = intel_delete_renderbuffer;
422 irb->Base.AllocStorage = intel_alloc_renderbuffer_storage;
423 irb->Base.GetPointer = intel_get_pointer;
424 /* span routines set in alloc_storage function */
425
426 return &irb->Base;
427 }
428
429
430 /**
431 * Called via glBindFramebufferEXT().
432 */
433 static void
434 intel_bind_framebuffer(GLcontext * ctx, GLenum target,
435 struct gl_framebuffer *fb, struct gl_framebuffer *fbread)
436 {
437 if (target == GL_FRAMEBUFFER_EXT || target == GL_DRAW_FRAMEBUFFER_EXT) {
438 intel_draw_buffer(ctx, fb);
439 }
440 else {
441 /* don't need to do anything if target == GL_READ_FRAMEBUFFER_EXT */
442 }
443 }
444
445
446 /**
447 * Called via glFramebufferRenderbufferEXT().
448 */
449 static void
450 intel_framebuffer_renderbuffer(GLcontext * ctx,
451 struct gl_framebuffer *fb,
452 GLenum attachment, struct gl_renderbuffer *rb)
453 {
454 DBG("Intel FramebufferRenderbuffer %u %u\n", fb->Name, rb ? rb->Name : 0);
455
456 intelFlush(ctx);
457
458 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb);
459 intel_draw_buffer(ctx, fb);
460 }
461
462
463 static GLboolean
464 intel_update_wrapper(GLcontext *ctx, struct intel_renderbuffer *irb,
465 struct gl_texture_image *texImage)
466 {
467 irb->texformat = texImage->TexFormat;
468
469 if (texImage->TexFormat == &_mesa_texformat_argb8888) {
470 irb->Base._ActualFormat = GL_RGBA8;
471 irb->Base._BaseFormat = GL_RGBA;
472 irb->Base.DataType = GL_UNSIGNED_BYTE;
473 DBG("Render to RGBA8 texture OK\n");
474 }
475 else if (texImage->TexFormat == &_mesa_texformat_rgb565) {
476 irb->Base._ActualFormat = GL_RGB5;
477 irb->Base._BaseFormat = GL_RGB;
478 irb->Base.DataType = GL_UNSIGNED_BYTE;
479 DBG("Render to RGB5 texture OK\n");
480 }
481 else if (texImage->TexFormat == &_mesa_texformat_argb1555) {
482 irb->Base._ActualFormat = GL_RGB5_A1;
483 irb->Base._BaseFormat = GL_RGBA;
484 irb->Base.DataType = GL_UNSIGNED_BYTE;
485 DBG("Render to ARGB1555 texture OK\n");
486 }
487 else if (texImage->TexFormat == &_mesa_texformat_argb4444) {
488 irb->Base._ActualFormat = GL_RGBA4;
489 irb->Base._BaseFormat = GL_RGBA;
490 irb->Base.DataType = GL_UNSIGNED_BYTE;
491 DBG("Render to ARGB4444 texture OK\n");
492 }
493 else if (texImage->TexFormat == &_mesa_texformat_z16) {
494 irb->Base._ActualFormat = GL_DEPTH_COMPONENT16;
495 irb->Base._BaseFormat = GL_DEPTH_COMPONENT;
496 irb->Base.DataType = GL_UNSIGNED_SHORT;
497 DBG("Render to DEPTH16 texture OK\n");
498 }
499 else if (texImage->TexFormat == &_mesa_texformat_s8_z24) {
500 irb->Base._ActualFormat = GL_DEPTH24_STENCIL8_EXT;
501 irb->Base._BaseFormat = GL_DEPTH_STENCIL_EXT;
502 irb->Base.DataType = GL_UNSIGNED_INT_24_8_EXT;
503 DBG("Render to DEPTH_STENCIL texture OK\n");
504 }
505 else {
506 DBG("Render to texture BAD FORMAT %d\n",
507 texImage->TexFormat->MesaFormat);
508 return GL_FALSE;
509 }
510
511 irb->Base.InternalFormat = irb->Base._ActualFormat;
512 irb->Base.Width = texImage->Width;
513 irb->Base.Height = texImage->Height;
514 irb->Base.RedBits = texImage->TexFormat->RedBits;
515 irb->Base.GreenBits = texImage->TexFormat->GreenBits;
516 irb->Base.BlueBits = texImage->TexFormat->BlueBits;
517 irb->Base.AlphaBits = texImage->TexFormat->AlphaBits;
518 irb->Base.DepthBits = texImage->TexFormat->DepthBits;
519 irb->Base.StencilBits = texImage->TexFormat->StencilBits;
520
521 irb->Base.Delete = intel_delete_renderbuffer;
522 irb->Base.AllocStorage = intel_nop_alloc_storage;
523
524 return GL_TRUE;
525 }
526
527
528 /**
529 * When glFramebufferTexture[123]D is called this function sets up the
530 * gl_renderbuffer wrapper around the texture image.
531 * This will have the region info needed for hardware rendering.
532 */
533 static struct intel_renderbuffer *
534 intel_wrap_texture(GLcontext * ctx, struct gl_texture_image *texImage)
535 {
536 const GLuint name = ~0; /* not significant, but distinct for debugging */
537 struct intel_renderbuffer *irb;
538
539 /* make an intel_renderbuffer to wrap the texture image */
540 irb = CALLOC_STRUCT(intel_renderbuffer);
541 if (!irb) {
542 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture");
543 return NULL;
544 }
545
546 _mesa_init_renderbuffer(&irb->Base, name);
547 irb->Base.ClassID = INTEL_RB_CLASS;
548
549 if (!intel_update_wrapper(ctx, irb, texImage)) {
550 _mesa_free(irb);
551 return NULL;
552 }
553
554 return irb;
555 }
556
557
558 /**
559 * Called by glFramebufferTexture[123]DEXT() (and other places) to
560 * prepare for rendering into texture memory. This might be called
561 * many times to choose different texture levels, cube faces, etc
562 * before intel_finish_render_texture() is ever called.
563 */
564 static void
565 intel_render_texture(GLcontext * ctx,
566 struct gl_framebuffer *fb,
567 struct gl_renderbuffer_attachment *att)
568 {
569 struct gl_texture_image *newImage
570 = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
571 struct intel_renderbuffer *irb = intel_renderbuffer(att->Renderbuffer);
572 struct intel_texture_image *intel_image;
573 GLuint imageOffset;
574
575 (void) fb;
576
577 ASSERT(newImage);
578
579 intel_image = intel_texture_image(newImage);
580 if (!intel_image->mt) {
581 /* Fallback on drawing to a texture that doesn't have a miptree
582 * (has a border, width/height 0, etc.)
583 */
584 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
585 _mesa_render_texture(ctx, fb, att);
586 return;
587 }
588 else if (!irb) {
589 irb = intel_wrap_texture(ctx, newImage);
590 if (irb) {
591 /* bind the wrapper to the attachment point */
592 _mesa_reference_renderbuffer(&att->Renderbuffer, &irb->Base);
593 }
594 else {
595 /* fallback to software rendering */
596 _mesa_render_texture(ctx, fb, att);
597 return;
598 }
599 }
600
601 if (!intel_update_wrapper(ctx, irb, newImage)) {
602 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL);
603 _mesa_render_texture(ctx, fb, att);
604 return;
605 }
606
607 DBG("Begin render texture tid %x tex=%u w=%d h=%d refcount=%d\n",
608 _glthread_GetID(),
609 att->Texture->Name, newImage->Width, newImage->Height,
610 irb->Base.RefCount);
611
612 /* point the renderbufer's region to the texture image region */
613 if (irb->region != intel_image->mt->region) {
614 if (irb->region)
615 intel_region_release(&irb->region);
616 intel_region_reference(&irb->region, intel_image->mt->region);
617 }
618
619 /* compute offset of the particular 2D image within the texture region */
620 imageOffset = intel_miptree_image_offset(intel_image->mt,
621 att->CubeMapFace,
622 att->TextureLevel);
623
624 if (att->Texture->Target == GL_TEXTURE_3D) {
625 const GLuint *offsets = intel_miptree_depth_offsets(intel_image->mt,
626 att->TextureLevel);
627 imageOffset += offsets[att->Zoffset];
628 }
629
630 /* store that offset in the region */
631 intel_image->mt->region->draw_offset = imageOffset;
632
633 /* update drawing region, etc */
634 intel_draw_buffer(ctx, fb);
635 }
636
637
638 /**
639 * Called by Mesa when rendering to a texture is done.
640 */
641 static void
642 intel_finish_render_texture(GLcontext * ctx,
643 struct gl_renderbuffer_attachment *att)
644 {
645 /* no-op
646 * Previously we released the renderbuffer's intel_region but
647 * that's not necessary and actually caused problems when trying
648 * to do a glRead/CopyPixels from the renderbuffer later.
649 * The region will be released later if the texture is replaced
650 * or the renderbuffer deleted.
651 *
652 * The intention of this driver hook is more of a "done rendering
653 * to texture, please re-twiddle/etc if necessary".
654 */
655 }
656
657
658 /**
659 * Do additional "completeness" testing of a framebuffer object.
660 */
661 static void
662 intel_validate_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
663 {
664 const struct intel_renderbuffer *depthRb =
665 intel_get_renderbuffer(fb, BUFFER_DEPTH);
666 const struct intel_renderbuffer *stencilRb =
667 intel_get_renderbuffer(fb, BUFFER_STENCIL);
668 int i;
669
670 if (stencilRb && stencilRb != depthRb) {
671 /* we only support combined depth/stencil buffers, not separate
672 * stencil buffers.
673 */
674 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
675 }
676
677 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
678 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[i];
679 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
680
681 if (rb == NULL)
682 continue;
683
684 if (irb == NULL) {
685 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
686 continue;
687 }
688
689 switch (irb->texformat->MesaFormat) {
690 case MESA_FORMAT_ARGB8888:
691 case MESA_FORMAT_RGB565:
692 case MESA_FORMAT_ARGB1555:
693 case MESA_FORMAT_ARGB4444:
694 break;
695 default:
696 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
697 }
698 }
699 }
700
701
702 /**
703 * Called from glBlitFramebuffer().
704 * For now, we're doing an approximation with glCopyPixels().
705 * XXX we need to bypass all the per-fragment operations, except scissor.
706 */
707 static void
708 intel_blit_framebuffer(GLcontext *ctx,
709 GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
710 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
711 GLbitfield mask, GLenum filter)
712 {
713 const GLfloat xZoomSave = ctx->Pixel.ZoomX;
714 const GLfloat yZoomSave = ctx->Pixel.ZoomY;
715 GLsizei width, height;
716 GLfloat xFlip = 1.0F, yFlip = 1.0F;
717
718 if (srcX1 < srcX0) {
719 GLint tmp = srcX1;
720 srcX1 = srcX0;
721 srcX0 = tmp;
722 xFlip = -1.0F;
723 }
724
725 if (srcY1 < srcY0) {
726 GLint tmp = srcY1;
727 srcY1 = srcY0;
728 srcY0 = tmp;
729 yFlip = -1.0F;
730 }
731
732 width = srcX1 - srcX0;
733 height = srcY1 - srcY0;
734
735 ctx->Pixel.ZoomX = xFlip * (dstX1 - dstX0) / (srcX1 - srcY0);
736 ctx->Pixel.ZoomY = yFlip * (dstY1 - dstY0) / (srcY1 - srcY0);
737
738 if (ctx->Pixel.ZoomX < 0.0F) {
739 dstX0 = MAX2(dstX0, dstX1);
740 }
741 else {
742 dstX0 = MIN2(dstX0, dstX1);
743 }
744
745 if (ctx->Pixel.ZoomY < 0.0F) {
746 dstY0 = MAX2(dstY0, dstY1);
747 }
748 else {
749 dstY0 = MIN2(dstY0, dstY1);
750 }
751
752 if (mask & GL_COLOR_BUFFER_BIT) {
753 ctx->Driver.CopyPixels(ctx, srcX0, srcY0, width, height,
754 dstX0, dstY0, GL_COLOR);
755 }
756 if (mask & GL_DEPTH_BUFFER_BIT) {
757 ctx->Driver.CopyPixels(ctx, srcX0, srcY0, width, height,
758 dstX0, dstY0, GL_DEPTH);
759 }
760 if (mask & GL_STENCIL_BUFFER_BIT) {
761 ctx->Driver.CopyPixels(ctx, srcX0, srcY0, width, height,
762 dstX0, dstY0, GL_STENCIL);
763 }
764
765 ctx->Pixel.ZoomX = xZoomSave;
766 ctx->Pixel.ZoomY = yZoomSave;
767 }
768
769
770 /**
771 * Do one-time context initializations related to GL_EXT_framebuffer_object.
772 * Hook in device driver functions.
773 */
774 void
775 intel_fbo_init(struct intel_context *intel)
776 {
777 intel->ctx.Driver.NewFramebuffer = intel_new_framebuffer;
778 intel->ctx.Driver.NewRenderbuffer = intel_new_renderbuffer;
779 intel->ctx.Driver.BindFramebuffer = intel_bind_framebuffer;
780 intel->ctx.Driver.FramebufferRenderbuffer = intel_framebuffer_renderbuffer;
781 intel->ctx.Driver.RenderTexture = intel_render_texture;
782 intel->ctx.Driver.FinishRenderTexture = intel_finish_render_texture;
783 intel->ctx.Driver.ResizeBuffers = intel_resize_buffers;
784 intel->ctx.Driver.ValidateFramebuffer = intel_validate_framebuffer;
785 intel->ctx.Driver.BlitFramebuffer = intel_blit_framebuffer;
786 }