Merge branch 'mesa_7_6_branch' into mesa_7_7_branch
[mesa.git] / src / mesa / state_tracker / st_cb_fbo.c
index c249f3b35781f12f6c4d3304aa1a24642d84b001..65ce12ccd4f8081aabbf3462073a2c29564d2d35 100644 (file)
@@ -49,6 +49,7 @@
 #include "st_public.h"
 #include "st_texture.h"
 
+#include "util/u_rect.h"
 
 
 /**
@@ -64,13 +65,7 @@ init_renderbuffer_bits(struct st_renderbuffer *strb,
       assert( 0 );
    }
 
-   strb->Base._ActualFormat = info.base_format;
-   strb->Base.RedBits = info.red_bits;
-   strb->Base.GreenBits = info.green_bits;
-   strb->Base.BlueBits = info.blue_bits;
-   strb->Base.AlphaBits = info.alpha_bits;
-   strb->Base.DepthBits = info.depth_bits;
-   strb->Base.StencilBits = info.stencil_bits;
+   strb->Base.Format = info.mesa_format;
    strb->Base.DataType = st_format_datatype(pipeFormat);
 
    return info.size;
@@ -88,93 +83,92 @@ st_renderbuffer_alloc_storage(GLcontext * ctx, struct gl_renderbuffer *rb,
 {
    struct pipe_context *pipe = ctx->st->pipe;
    struct st_renderbuffer *strb = st_renderbuffer(rb);
-   struct pipe_texture template;
-   unsigned surface_usage;
-
-   /* Free the old surface and texture
-    */
-   pipe_surface_reference( &strb->surface, NULL );
-   pipe_texture_reference( &strb->texture, NULL );
-
-   /* Setup new texture template.
-    */
-   memset(&template, 0, sizeof(template));
-   template.target = PIPE_TEXTURE_2D;
-   if (strb->format != PIPE_FORMAT_NONE) {
-      template.format = strb->format;
-   }
-   else {
-      template.format = st_choose_renderbuffer_format(pipe, internalFormat);
-   }
-   pf_get_block(template.format, &template.block);
-   template.width[0] = width;
-   template.height[0] = height;
-   template.depth[0] = 1;
-   template.last_level = 0;
-   template.nr_samples = rb->NumSamples;
-   if (pf_is_depth_stencil(template.format)) {
-      template.tex_usage = PIPE_TEXTURE_USAGE_DEPTH_STENCIL;
-   }
-   else {
-      template.tex_usage = (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
-                            PIPE_TEXTURE_USAGE_RENDER_TARGET);
-   }
+   enum pipe_format format;
 
+   if (strb->format != PIPE_FORMAT_NONE)
+      format = strb->format;
+   else
+      format = st_choose_renderbuffer_format(pipe->screen, internalFormat);
+      
    /* init renderbuffer fields */
    strb->Base.Width  = width;
    strb->Base.Height = height;
-   init_renderbuffer_bits(strb, template.format);
+   init_renderbuffer_bits(strb, format);
 
    strb->defined = GL_FALSE;  /* undefined contents now */
 
-   /* Probably need dedicated flags for surface usage too: 
-    */
-   surface_usage = (PIPE_BUFFER_USAGE_GPU_READ |
-                    PIPE_BUFFER_USAGE_GPU_WRITE);
-#if 0
-                    PIPE_BUFFER_USAGE_CPU_READ |
-                    PIPE_BUFFER_USAGE_CPU_WRITE);
-#endif
+   if(strb->software) {
+      struct pipe_format_block block;
+      size_t size;
+      
+      _mesa_free(strb->data);
+
+      assert(strb->format != PIPE_FORMAT_NONE);
+      pf_get_block(strb->format, &block);
+      
+      strb->stride = pf_get_stride(&block, width);
+      size = pf_get_2d_size(&block, strb->stride, height);
+      
+      strb->data = _mesa_malloc(size);
+      
+      return strb->data != NULL;
+   }
+   else {
+      struct pipe_texture template;
+      unsigned surface_usage;
+    
+      /* Free the old surface and texture
+       */
+      pipe_surface_reference( &strb->surface, NULL );
+      pipe_texture_reference( &strb->texture, NULL );
 
-   strb->texture = pipe->screen->texture_create( pipe->screen,
-                                                 &template );
+      /* Setup new texture template.
+       */
+      memset(&template, 0, sizeof(template));
+      template.target = PIPE_TEXTURE_2D;
+      template.format = format;
+      pf_get_block(format, &template.block);
+      template.width[0] = width;
+      template.height[0] = height;
+      template.depth[0] = 1;
+      template.last_level = 0;
+      template.nr_samples = rb->NumSamples;
+      if (pf_is_depth_stencil(format)) {
+         template.tex_usage = PIPE_TEXTURE_USAGE_DEPTH_STENCIL;
+      }
+      else {
+         template.tex_usage = (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
+                               PIPE_TEXTURE_USAGE_RENDER_TARGET);
+      }
 
-   /* Special path for accum buffers.  
-    *
-    * Try a different surface format.  Since accum buffers are s/w
-    * only for now, the surface pixel format doesn't really matter,
-    * only that the buffer is large enough.
-    */
-   if (!strb->texture && template.format == DEFAULT_ACCUM_PIPE_FORMAT) 
-   {
-      /* Actually, just setting this usage value should be sufficient
-       * to tell the driver to go ahead and allocate the buffer, even
-       * if HW doesn't support the format.
+      /* Probably need dedicated flags for surface usage too: 
        */
-      template.tex_usage = 0;
-      surface_usage = (PIPE_BUFFER_USAGE_CPU_READ |
+      surface_usage = (PIPE_BUFFER_USAGE_GPU_READ |
+                       PIPE_BUFFER_USAGE_GPU_WRITE);
+#if 0
+                       PIPE_BUFFER_USAGE_CPU_READ |
                        PIPE_BUFFER_USAGE_CPU_WRITE);
+#endif
 
       strb->texture = pipe->screen->texture_create( pipe->screen,
                                                     &template );
 
-   }
-
-   if (!strb->texture) 
-      return FALSE;
-
-   strb->surface = pipe->screen->get_tex_surface( pipe->screen,
-                                                  strb->texture,
-                                                  0, 0, 0,
-                                                  surface_usage );
-
-   assert(strb->surface->texture);
-   assert(strb->surface->format);
-   assert(strb->surface->width == width);
-   assert(strb->surface->height == height);
-
+      if (!strb->texture) 
+         return FALSE;
+
+      strb->surface = pipe->screen->get_tex_surface( pipe->screen,
+                                                     strb->texture,
+                                                     0, 0, 0,
+                                                     surface_usage );
+      if (strb->surface) {
+         assert(strb->surface->texture);
+         assert(strb->surface->format);
+         assert(strb->surface->width == width);
+         assert(strb->surface->height == height);
+      }
 
-   return strb->surface != NULL;
+      return strb->surface != NULL;
+   }
 }
 
 
@@ -188,6 +182,7 @@ st_renderbuffer_delete(struct gl_renderbuffer *rb)
    ASSERT(strb);
    pipe_surface_reference(&strb->surface, NULL);
    pipe_texture_reference(&strb->texture, NULL);
+   _mesa_free(strb->data);
    _mesa_free(strb);
 }
 
@@ -244,7 +239,7 @@ st_new_renderbuffer(GLcontext *ctx, GLuint name)
  * renderbuffer).  The window system code determines the format.
  */
 struct gl_renderbuffer *
-st_new_renderbuffer_fb(enum pipe_format format, int samples)
+st_new_renderbuffer_fb(enum pipe_format format, int samples, boolean sw)
 {
    struct st_renderbuffer *strb;
 
@@ -258,7 +253,9 @@ st_new_renderbuffer_fb(enum pipe_format format, int samples)
    strb->Base.ClassID = 0x4242; /* just a unique value */
    strb->Base.NumSamples = samples;
    strb->format = format;
-
+   init_renderbuffer_bits(strb, format);
+   strb->software = sw;
+   
    switch (format) {
    case PIPE_FORMAT_A8R8G8B8_UNORM:
    case PIPE_FORMAT_B8G8R8A8_UNORM:
@@ -268,34 +265,29 @@ st_new_renderbuffer_fb(enum pipe_format format, int samples)
    case PIPE_FORMAT_A4R4G4B4_UNORM:
    case PIPE_FORMAT_R5G6B5_UNORM:
       strb->Base.InternalFormat = GL_RGBA;
-      strb->Base._BaseFormat = GL_RGBA;
       break;
    case PIPE_FORMAT_Z16_UNORM:
       strb->Base.InternalFormat = GL_DEPTH_COMPONENT16;
-      strb->Base._BaseFormat = GL_DEPTH_COMPONENT;
       break;
    case PIPE_FORMAT_Z32_UNORM:
       strb->Base.InternalFormat = GL_DEPTH_COMPONENT32;
-      strb->Base._BaseFormat = GL_DEPTH_COMPONENT;
       break;
    case PIPE_FORMAT_S8Z24_UNORM:
    case PIPE_FORMAT_Z24S8_UNORM:
    case PIPE_FORMAT_X8Z24_UNORM:
    case PIPE_FORMAT_Z24X8_UNORM:
       strb->Base.InternalFormat = GL_DEPTH24_STENCIL8_EXT;
-      strb->Base._BaseFormat = GL_DEPTH_STENCIL_EXT;
       break;
    case PIPE_FORMAT_S8_UNORM:
       strb->Base.InternalFormat = GL_STENCIL_INDEX8_EXT;
-      strb->Base._BaseFormat = GL_STENCIL_INDEX;
       break;
-   case DEFAULT_ACCUM_PIPE_FORMAT: /*PIPE_FORMAT_R16G16B16A16_SNORM*/
+   case PIPE_FORMAT_R16G16B16A16_SNORM:
       strb->Base.InternalFormat = GL_RGBA16;
-      strb->Base._BaseFormat = GL_RGBA;
       break;
    default:
       _mesa_problem(NULL,
                    "Unexpected format in st_new_renderbuffer_fb");
+      _mesa_free(strb);
       return NULL;
    }
 
@@ -381,6 +373,7 @@ st_render_texture(GLcontext *ctx,
 
    rb->Width = texImage->Width2;
    rb->Height = texImage->Height2;
+   rb->_BaseFormat = texImage->_BaseFormat;
    /*printf("***** render to texture level %d: %d x %d\n", att->TextureLevel, rb->Width, rb->Height);*/
 
    /*printf("***** pipe texture %d x %d\n", pt->width[0], pt->height[0]);*/
@@ -444,6 +437,35 @@ st_finish_render_texture(GLcontext *ctx,
 }
 
 
+/**
+ * Validate a renderbuffer attachment for a particular usage.
+ */
+
+static GLboolean
+st_validate_attachment(struct pipe_screen *screen,
+                      const struct gl_renderbuffer_attachment *att,
+                      GLuint usage)
+{
+   const struct st_texture_object *stObj =
+      st_texture_object(att->Texture);
+
+   /**
+    * Only validate texture attachments for now, since
+    * st_renderbuffer_alloc_storage makes sure that
+    * the format is supported.
+    */
+
+   if (att->Type != GL_TEXTURE)
+      return GL_TRUE;
+
+   if (!stObj)
+      return GL_FALSE;
+
+   return screen->is_format_supported(screen, stObj->pt->format,
+                                     PIPE_TEXTURE_2D,
+                                     usage, 0);
+}
+
 /**
  * Check that the framebuffer configuration is valid in terms of what
  * the driver can support.
@@ -453,13 +475,37 @@ st_finish_render_texture(GLcontext *ctx,
 static void
 st_validate_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
 {
+   struct pipe_screen *screen = ctx->st->pipe->screen;
    const struct gl_renderbuffer *depthRb =
       fb->Attachment[BUFFER_DEPTH].Renderbuffer;
    const struct gl_renderbuffer *stencilRb =
       fb->Attachment[BUFFER_STENCIL].Renderbuffer;
+   GLuint i;
 
    if (stencilRb && depthRb && stencilRb != depthRb) {
       fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
+      return;
+   }
+
+   if (!st_validate_attachment(screen,
+                              &fb->Attachment[BUFFER_DEPTH],
+                              PIPE_TEXTURE_USAGE_DEPTH_STENCIL)) {
+      fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
+      return;
+   }
+   if (!st_validate_attachment(screen,
+                              &fb->Attachment[BUFFER_STENCIL],
+                              PIPE_TEXTURE_USAGE_DEPTH_STENCIL)) {
+      fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
+      return;
+   }
+   for (i = 0; i < ctx->Const.MaxColorAttachments; i++) {
+      if (!st_validate_attachment(screen,
+                                 &fb->Attachment[BUFFER_COLOR0 + i],
+                                 PIPE_TEXTURE_USAGE_RENDER_TARGET)) {
+        fb->_Status = GL_FRAMEBUFFER_UNSUPPORTED_EXT;
+        return;
+      }
    }
 }
 
@@ -481,10 +527,17 @@ copy_back_to_front(struct st_context *st,
    (void) st_get_framebuffer_surface(stfb, backIndex, &surf_back);
 
    if (surf_front && surf_back) {
-      st->pipe->surface_copy(st->pipe,
-                             surf_front, 0, 0,  /* dest */
-                             surf_back, 0, 0,   /* src */
-                             fb->Width, fb->Height);
+      if (st->pipe->surface_copy) {
+         st->pipe->surface_copy(st->pipe,
+                                surf_front, 0, 0,  /* dest */
+                                surf_back, 0, 0,   /* src */
+                                fb->Width, fb->Height);
+      } else {
+         util_surface_copy(st->pipe, FALSE,
+                           surf_front, 0, 0,
+                           surf_back, 0, 0,
+                           fb->Width, fb->Height);
+      }
    }
 }
 
@@ -536,7 +589,7 @@ check_create_front_buffer(GLcontext *ctx, struct gl_framebuffer *fb,
          samples = back->Base.NumSamples;
 
          /* create front renderbuffer */
-         front = st_new_renderbuffer_fb(colorFormat, samples);
+         front = st_new_renderbuffer_fb(colorFormat, samples, FALSE);
          _mesa_add_renderbuffer(fb, frontIndex, front);
 
          /* alloc texture/surface for new front buffer */