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