intel_fbo.c revision 3078bd136d6ee1d9ad16b4c834cad23b005304a4
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/texrender.h" 37#include "drivers/common/meta.h" 38 39#include "intel_context.h" 40#include "intel_batchbuffer.h" 41#include "intel_buffers.h" 42#include "intel_fbo.h" 43#include "intel_mipmap_tree.h" 44#include "intel_regions.h" 45 46 47#define FILE_DEBUG_FLAG DEBUG_FBO 48 49 50/** 51 * Create a new framebuffer object. 52 */ 53static struct gl_framebuffer * 54intel_new_framebuffer(GLcontext * ctx, GLuint name) 55{ 56 /* Only drawable state in intel_framebuffer at this time, just use Mesa's 57 * class 58 */ 59 return _mesa_new_framebuffer(ctx, name); 60} 61 62 63/** Called by gl_renderbuffer::Delete() */ 64static void 65intel_delete_renderbuffer(struct gl_renderbuffer *rb) 66{ 67 GET_CURRENT_CONTEXT(ctx); 68 struct intel_context *intel = intel_context(ctx); 69 struct intel_renderbuffer *irb = intel_renderbuffer(rb); 70 71 ASSERT(irb); 72 73 if (irb->span_cache != NULL) 74 _mesa_free(irb->span_cache); 75 76 if (intel && irb->region) { 77 intel_region_release(&irb->region); 78 } 79 80 _mesa_free(irb); 81} 82 83 84/** 85 * Return a pointer to a specific pixel in a renderbuffer. 86 */ 87static void * 88intel_get_pointer(GLcontext * ctx, struct gl_renderbuffer *rb, 89 GLint x, GLint y) 90{ 91 /* By returning NULL we force all software rendering to go through 92 * the span routines. 93 */ 94 return NULL; 95} 96 97 98/** 99 * Called via glRenderbufferStorageEXT() to set the format and allocate 100 * storage for a user-created renderbuffer. 101 */ 102static GLboolean 103intel_alloc_renderbuffer_storage(GLcontext * ctx, struct gl_renderbuffer *rb, 104 GLenum internalFormat, 105 GLuint width, GLuint height) 106{ 107 struct intel_context *intel = intel_context(ctx); 108 struct intel_renderbuffer *irb = intel_renderbuffer(rb); 109 GLboolean softwareBuffer = GL_FALSE; 110 int cpp; 111 112 ASSERT(rb->Name != 0); 113 114 switch (internalFormat) { 115 case GL_R3_G3_B2: 116 case GL_RGB4: 117 case GL_RGB5: 118 rb->Format = MESA_FORMAT_RGB565; 119 rb->DataType = GL_UNSIGNED_BYTE; 120 cpp = 2; 121 break; 122 case GL_RGB: 123 case GL_RGB8: 124 case GL_RGB10: 125 case GL_RGB12: 126 case GL_RGB16: 127 rb->Format = MESA_FORMAT_ARGB8888; /* XXX: Need xrgb8888 */ 128 rb->DataType = GL_UNSIGNED_BYTE; 129 cpp = 4; 130 break; 131 case GL_RGBA: 132 case GL_RGBA2: 133 case GL_RGBA4: 134 case GL_RGB5_A1: 135 case GL_RGBA8: 136 case GL_RGB10_A2: 137 case GL_RGBA12: 138 case GL_RGBA16: 139 rb->Format = MESA_FORMAT_ARGB8888; 140 rb->DataType = GL_UNSIGNED_BYTE; 141 cpp = 4; 142 break; 143 case GL_STENCIL_INDEX: 144 case GL_STENCIL_INDEX1_EXT: 145 case GL_STENCIL_INDEX4_EXT: 146 case GL_STENCIL_INDEX8_EXT: 147 case GL_STENCIL_INDEX16_EXT: 148 /* alloc a depth+stencil buffer */ 149 rb->Format = MESA_FORMAT_S8_Z24; 150 rb->DataType = GL_UNSIGNED_INT_24_8_EXT; 151 cpp = 4; 152 break; 153 case GL_DEPTH_COMPONENT16: 154 rb->Format = MESA_FORMAT_Z16; 155 rb->DataType = GL_UNSIGNED_SHORT; 156 cpp = 2; 157 break; 158 case GL_DEPTH_COMPONENT: 159 case GL_DEPTH_COMPONENT24: 160 case GL_DEPTH_COMPONENT32: 161 rb->Format = MESA_FORMAT_S8_Z24; 162 rb->DataType = GL_UNSIGNED_INT_24_8_EXT; 163 cpp = 4; 164 break; 165 case GL_DEPTH_STENCIL_EXT: 166 case GL_DEPTH24_STENCIL8_EXT: 167 rb->Format = MESA_FORMAT_S8_Z24; 168 rb->DataType = GL_UNSIGNED_INT_24_8_EXT; 169 cpp = 4; 170 break; 171 default: 172 _mesa_problem(ctx, 173 "Unexpected format in intel_alloc_renderbuffer_storage"); 174 return GL_FALSE; 175 } 176 177 rb->_BaseFormat = _mesa_base_fbo_format(ctx, internalFormat); 178 179 intelFlush(ctx); 180 181 /* free old region */ 182 if (irb->region) { 183 intel_region_release(&irb->region); 184 } 185 186 /* allocate new memory region/renderbuffer */ 187 if (softwareBuffer) { 188 return _mesa_soft_renderbuffer_storage(ctx, rb, internalFormat, 189 width, height); 190 } 191 else { 192 /* Choose a pitch to match hardware requirements: 193 */ 194 GLuint pitch = ((cpp * width + 63) & ~63) / cpp; 195 196 /* alloc hardware renderbuffer */ 197 DBG("Allocating %d x %d Intel RBO (pitch %d)\n", width, 198 height, pitch); 199 200 irb->region = intel_region_alloc(intel, I915_TILING_NONE, 201 cpp, width, height, pitch, 202 GL_TRUE); 203 if (!irb->region) 204 return GL_FALSE; /* out of memory? */ 205 206 ASSERT(irb->region->buffer); 207 208 rb->Width = width; 209 rb->Height = height; 210 211 return GL_TRUE; 212 } 213} 214 215 216/** 217 * Called for each hardware renderbuffer when a _window_ is resized. 218 * Just update fields. 219 * Not used for user-created renderbuffers! 220 */ 221static GLboolean 222intel_alloc_window_storage(GLcontext * ctx, struct gl_renderbuffer *rb, 223 GLenum internalFormat, GLuint width, GLuint height) 224{ 225 ASSERT(rb->Name == 0); 226 rb->Width = width; 227 rb->Height = height; 228 rb->InternalFormat = internalFormat; 229 230 return GL_TRUE; 231} 232 233 234static void 235intel_resize_buffers(GLcontext *ctx, struct gl_framebuffer *fb, 236 GLuint width, GLuint height) 237{ 238 struct intel_framebuffer *intel_fb = (struct intel_framebuffer*)fb; 239 int i; 240 241 _mesa_resize_framebuffer(ctx, fb, width, height); 242 243 fb->Initialized = GL_TRUE; /* XXX remove someday */ 244 245 if (fb->Name != 0) { 246 return; 247 } 248 249 /* Make sure all window system renderbuffers are up to date */ 250 for (i = 0; i < 2; i++) { 251 struct gl_renderbuffer *rb = &intel_fb->color_rb[i]->Base; 252 253 /* only resize if size is changing */ 254 if (rb && (rb->Width != width || rb->Height != height)) { 255 rb->AllocStorage(ctx, rb, rb->InternalFormat, width, height); 256 } 257 } 258} 259 260 261/** Dummy function for gl_renderbuffer::AllocStorage() */ 262static GLboolean 263intel_nop_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb, 264 GLenum internalFormat, GLuint width, GLuint height) 265{ 266 _mesa_problem(ctx, "intel_op_alloc_storage should never be called."); 267 return GL_FALSE; 268} 269 270 271void 272intel_renderbuffer_set_region(struct intel_renderbuffer *rb, 273 struct intel_region *region) 274{ 275 struct intel_region *old; 276 277 old = rb->region; 278 rb->region = NULL; 279 intel_region_reference(&rb->region, region); 280 intel_region_release(&old); 281} 282 283 284/** 285 * Create a new intel_renderbuffer which corresponds to an on-screen window, 286 * not a user-created renderbuffer. 287 */ 288struct intel_renderbuffer * 289intel_create_renderbuffer(gl_format format) 290{ 291 GET_CURRENT_CONTEXT(ctx); 292 293 struct intel_renderbuffer *irb; 294 const GLuint name = 0; 295 296 irb = CALLOC_STRUCT(intel_renderbuffer); 297 if (!irb) { 298 _mesa_error(ctx, GL_OUT_OF_MEMORY, "creating renderbuffer"); 299 return NULL; 300 } 301 302 _mesa_init_renderbuffer(&irb->Base, name); 303 irb->Base.ClassID = INTEL_RB_CLASS; 304 305 switch (format) { 306 case MESA_FORMAT_RGB565: 307 irb->Base._BaseFormat = GL_RGB; 308 irb->Base.DataType = GL_UNSIGNED_BYTE; 309 break; 310 case MESA_FORMAT_XRGB8888: 311 /* XXX this is a hack since XRGB surfaces don't seem to work 312 * properly yet. Reading the alpha channel returns 0 instead of 1. 313 */ 314 format = MESA_FORMAT_ARGB8888; 315 irb->Base._BaseFormat = GL_RGB; 316 irb->Base.DataType = GL_UNSIGNED_BYTE; 317 break; 318 case MESA_FORMAT_ARGB8888: 319 irb->Base._BaseFormat = GL_RGBA; 320 irb->Base.DataType = GL_UNSIGNED_BYTE; 321 break; 322 case MESA_FORMAT_Z16: 323 irb->Base._BaseFormat = GL_DEPTH_COMPONENT; 324 irb->Base.DataType = GL_UNSIGNED_SHORT; 325 break; 326 case MESA_FORMAT_X8_Z24: 327 irb->Base._BaseFormat = GL_DEPTH_COMPONENT; 328 irb->Base.DataType = GL_UNSIGNED_INT; 329 break; 330 case MESA_FORMAT_S8_Z24: 331 irb->Base._BaseFormat = GL_DEPTH_STENCIL; 332 irb->Base.DataType = GL_UNSIGNED_INT_24_8_EXT; 333 break; 334 default: 335 _mesa_problem(NULL, 336 "Unexpected intFormat in intel_create_renderbuffer"); 337 _mesa_free(irb); 338 return NULL; 339 } 340 341 irb->Base.Format = format; 342 irb->Base.InternalFormat = irb->Base._BaseFormat; 343 344 /* intel-specific methods */ 345 irb->Base.Delete = intel_delete_renderbuffer; 346 irb->Base.AllocStorage = intel_alloc_window_storage; 347 irb->Base.GetPointer = intel_get_pointer; 348 349 return irb; 350} 351 352 353/** 354 * Create a new renderbuffer object. 355 * Typically called via glBindRenderbufferEXT(). 356 */ 357static struct gl_renderbuffer * 358intel_new_renderbuffer(GLcontext * ctx, GLuint name) 359{ 360 /*struct intel_context *intel = intel_context(ctx); */ 361 struct intel_renderbuffer *irb; 362 363 irb = CALLOC_STRUCT(intel_renderbuffer); 364 if (!irb) { 365 _mesa_error(ctx, GL_OUT_OF_MEMORY, "creating renderbuffer"); 366 return NULL; 367 } 368 369 _mesa_init_renderbuffer(&irb->Base, name); 370 irb->Base.ClassID = INTEL_RB_CLASS; 371 372 /* intel-specific methods */ 373 irb->Base.Delete = intel_delete_renderbuffer; 374 irb->Base.AllocStorage = intel_alloc_renderbuffer_storage; 375 irb->Base.GetPointer = intel_get_pointer; 376 /* span routines set in alloc_storage function */ 377 378 return &irb->Base; 379} 380 381 382/** 383 * Called via glBindFramebufferEXT(). 384 */ 385static void 386intel_bind_framebuffer(GLcontext * ctx, GLenum target, 387 struct gl_framebuffer *fb, struct gl_framebuffer *fbread) 388{ 389 if (target == GL_FRAMEBUFFER_EXT || target == GL_DRAW_FRAMEBUFFER_EXT) { 390 intel_draw_buffer(ctx, fb); 391 } 392 else { 393 /* don't need to do anything if target == GL_READ_FRAMEBUFFER_EXT */ 394 } 395} 396 397 398/** 399 * Called via glFramebufferRenderbufferEXT(). 400 */ 401static void 402intel_framebuffer_renderbuffer(GLcontext * ctx, 403 struct gl_framebuffer *fb, 404 GLenum attachment, struct gl_renderbuffer *rb) 405{ 406 DBG("Intel FramebufferRenderbuffer %u %u\n", fb->Name, rb ? rb->Name : 0); 407 408 intelFlush(ctx); 409 410 _mesa_framebuffer_renderbuffer(ctx, fb, attachment, rb); 411 intel_draw_buffer(ctx, fb); 412} 413 414 415static GLboolean 416intel_update_wrapper(GLcontext *ctx, struct intel_renderbuffer *irb, 417 struct gl_texture_image *texImage) 418{ 419 gl_format texFormat; 420 421 if (texImage->TexFormat == MESA_FORMAT_ARGB8888) { 422 irb->Base.DataType = GL_UNSIGNED_BYTE; 423 DBG("Render to RGBA8 texture OK\n"); 424 } 425 else if (texImage->TexFormat == MESA_FORMAT_XRGB8888) { 426 irb->Base.DataType = GL_UNSIGNED_BYTE; 427 DBG("Render to XGBA8 texture OK\n"); 428 } 429 else if (texImage->TexFormat == MESA_FORMAT_RGB565) { 430 irb->Base.DataType = GL_UNSIGNED_BYTE; 431 DBG("Render to RGB5 texture OK\n"); 432 } 433 else if (texImage->TexFormat == MESA_FORMAT_ARGB1555) { 434 irb->Base.DataType = GL_UNSIGNED_BYTE; 435 DBG("Render to ARGB1555 texture OK\n"); 436 } 437 else if (texImage->TexFormat == MESA_FORMAT_ARGB4444) { 438 irb->Base.DataType = GL_UNSIGNED_BYTE; 439 DBG("Render to ARGB4444 texture OK\n"); 440 } 441 else if (texImage->TexFormat == MESA_FORMAT_Z16) { 442 irb->Base.DataType = GL_UNSIGNED_SHORT; 443 DBG("Render to DEPTH16 texture OK\n"); 444 } 445 else if (texImage->TexFormat == MESA_FORMAT_S8_Z24) { 446 irb->Base.DataType = GL_UNSIGNED_INT_24_8_EXT; 447 DBG("Render to DEPTH_STENCIL texture OK\n"); 448 } 449 else { 450 DBG("Render to texture BAD FORMAT %d\n", texImage->TexFormat); 451 return GL_FALSE; 452 } 453 454 irb->Base.Format = texImage->TexFormat; 455 456 texFormat = texImage->TexFormat; 457 458 irb->Base.InternalFormat = texImage->InternalFormat; 459 irb->Base._BaseFormat = _mesa_base_fbo_format(ctx, irb->Base.InternalFormat); 460 irb->Base.Width = texImage->Width; 461 irb->Base.Height = texImage->Height; 462 463 irb->Base.Delete = intel_delete_renderbuffer; 464 irb->Base.AllocStorage = intel_nop_alloc_storage; 465 466 return GL_TRUE; 467} 468 469 470/** 471 * When glFramebufferTexture[123]D is called this function sets up the 472 * gl_renderbuffer wrapper around the texture image. 473 * This will have the region info needed for hardware rendering. 474 */ 475static struct intel_renderbuffer * 476intel_wrap_texture(GLcontext * ctx, struct gl_texture_image *texImage) 477{ 478 const GLuint name = ~0; /* not significant, but distinct for debugging */ 479 struct intel_renderbuffer *irb; 480 481 /* make an intel_renderbuffer to wrap the texture image */ 482 irb = CALLOC_STRUCT(intel_renderbuffer); 483 if (!irb) { 484 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glFramebufferTexture"); 485 return NULL; 486 } 487 488 _mesa_init_renderbuffer(&irb->Base, name); 489 irb->Base.ClassID = INTEL_RB_CLASS; 490 491 if (!intel_update_wrapper(ctx, irb, texImage)) { 492 _mesa_free(irb); 493 return NULL; 494 } 495 496 return irb; 497} 498 499 500/** 501 * Called by glFramebufferTexture[123]DEXT() (and other places) to 502 * prepare for rendering into texture memory. This might be called 503 * many times to choose different texture levels, cube faces, etc 504 * before intel_finish_render_texture() is ever called. 505 */ 506static void 507intel_render_texture(GLcontext * ctx, 508 struct gl_framebuffer *fb, 509 struct gl_renderbuffer_attachment *att) 510{ 511 struct gl_texture_image *newImage 512 = att->Texture->Image[att->CubeMapFace][att->TextureLevel]; 513 struct intel_renderbuffer *irb = intel_renderbuffer(att->Renderbuffer); 514 struct intel_texture_image *intel_image; 515 GLuint dst_x, dst_y; 516 517 (void) fb; 518 519 ASSERT(newImage); 520 521 intel_image = intel_texture_image(newImage); 522 if (!intel_image->mt) { 523 /* Fallback on drawing to a texture that doesn't have a miptree 524 * (has a border, width/height 0, etc.) 525 */ 526 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL); 527 _mesa_render_texture(ctx, fb, att); 528 return; 529 } 530 else if (!irb) { 531 irb = intel_wrap_texture(ctx, newImage); 532 if (irb) { 533 /* bind the wrapper to the attachment point */ 534 _mesa_reference_renderbuffer(&att->Renderbuffer, &irb->Base); 535 } 536 else { 537 /* fallback to software rendering */ 538 _mesa_render_texture(ctx, fb, att); 539 return; 540 } 541 } 542 543 if (!intel_update_wrapper(ctx, irb, newImage)) { 544 _mesa_reference_renderbuffer(&att->Renderbuffer, NULL); 545 _mesa_render_texture(ctx, fb, att); 546 return; 547 } 548 549 DBG("Begin render texture tid %x tex=%u w=%d h=%d refcount=%d\n", 550 _glthread_GetID(), 551 att->Texture->Name, newImage->Width, newImage->Height, 552 irb->Base.RefCount); 553 554 /* point the renderbufer's region to the texture image region */ 555 if (irb->region != intel_image->mt->region) { 556 if (irb->region) 557 intel_region_release(&irb->region); 558 intel_region_reference(&irb->region, intel_image->mt->region); 559 } 560 561 /* compute offset of the particular 2D image within the texture region */ 562 intel_miptree_get_image_offset(intel_image->mt, 563 att->TextureLevel, 564 att->CubeMapFace, 565 att->Zoffset, 566 &dst_x, &dst_y); 567 568 intel_image->mt->region->draw_offset = (dst_y * intel_image->mt->pitch + 569 dst_x) * intel_image->mt->cpp; 570 intel_image->mt->region->draw_x = dst_x; 571 intel_image->mt->region->draw_y = dst_y; 572 intel_image->used_as_render_target = GL_TRUE; 573 574 /* update drawing region, etc */ 575 intel_draw_buffer(ctx, fb); 576} 577 578 579/** 580 * Called by Mesa when rendering to a texture is done. 581 */ 582static void 583intel_finish_render_texture(GLcontext * ctx, 584 struct gl_renderbuffer_attachment *att) 585{ 586 struct intel_context *intel = intel_context(ctx); 587 struct gl_texture_object *tex_obj = att->Texture; 588 struct gl_texture_image *image = 589 tex_obj->Image[att->CubeMapFace][att->TextureLevel]; 590 struct intel_texture_image *intel_image = intel_texture_image(image); 591 592 /* Flag that this image may now be validated into the object's miptree. */ 593 intel_image->used_as_render_target = GL_FALSE; 594 595 /* Since we've (probably) rendered to the texture and will (likely) use 596 * it in the texture domain later on in this batchbuffer, flush the 597 * batch. Once again, we wish for a domain tracker in libdrm to cover 598 * usage inside of a batchbuffer like GEM does in the kernel. 599 */ 600 intel_batchbuffer_emit_mi_flush(intel->batch); 601} 602 603/** 604 * Do additional "completeness" testing of a framebuffer object. 605 */ 606static void 607intel_validate_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb) 608{ 609 const struct intel_renderbuffer *depthRb = 610 intel_get_renderbuffer(fb, BUFFER_DEPTH); 611 const struct intel_renderbuffer *stencilRb = 612 intel_get_renderbuffer(fb, BUFFER_STENCIL); 613 int i; 614 615 if (stencilRb && stencilRb != depthRb) { 616 /* we only support combined depth/stencil buffers, not separate 617 * stencil buffers. 618 */ 619 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT; 620 } 621 622 for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) { 623 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[i]; 624 struct intel_renderbuffer *irb = intel_renderbuffer(rb); 625 626 if (rb == NULL) 627 continue; 628 629 if (irb == NULL) { 630 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT; 631 continue; 632 } 633 634 switch (irb->Base.Format) { 635 case MESA_FORMAT_ARGB8888: 636 case MESA_FORMAT_XRGB8888: 637 case MESA_FORMAT_RGB565: 638 case MESA_FORMAT_ARGB1555: 639 case MESA_FORMAT_ARGB4444: 640 break; 641 default: 642 fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT; 643 } 644 } 645} 646 647 648/** 649 * Do one-time context initializations related to GL_EXT_framebuffer_object. 650 * Hook in device driver functions. 651 */ 652void 653intel_fbo_init(struct intel_context *intel) 654{ 655 intel->ctx.Driver.NewFramebuffer = intel_new_framebuffer; 656 intel->ctx.Driver.NewRenderbuffer = intel_new_renderbuffer; 657 intel->ctx.Driver.BindFramebuffer = intel_bind_framebuffer; 658 intel->ctx.Driver.FramebufferRenderbuffer = intel_framebuffer_renderbuffer; 659 intel->ctx.Driver.RenderTexture = intel_render_texture; 660 intel->ctx.Driver.FinishRenderTexture = intel_finish_render_texture; 661 intel->ctx.Driver.ResizeBuffers = intel_resize_buffers; 662 intel->ctx.Driver.ValidateFramebuffer = intel_validate_framebuffer; 663 intel->ctx.Driver.BlitFramebuffer = _mesa_meta_BlitFramebuffer; 664} 665