Merge branch 'mesa_7_6_branch'
[mesa.git] / src / mesa / state_tracker / st_texture.c
index 19eb7e2f69c6e31623c0b9533989ea8072039af3..bbc2830e69413069b19244f11c0d3b20afc96fd0 100644 (file)
@@ -30,7 +30,9 @@
 #include "st_public.h"
 #include "st_texture.h"
 #include "st_cb_fbo.h"
+#include "st_inlines.h"
 #include "main/enums.h"
+#include "main/texobj.h"
 #include "main/teximage.h"
 #include "main/texstore.h"
 
@@ -188,13 +190,15 @@ st_texture_image_map(struct st_context *st, struct st_texture_image *stImage,
                     GLuint zoffset, enum pipe_transfer_usage usage,
                      GLuint x, GLuint y, GLuint w, GLuint h)
 {
-   struct pipe_screen *screen = st->pipe->screen;
+   struct pipe_context *pipe = st->pipe;
+   struct pipe_screen *screen = pipe->screen;
    struct pipe_texture *pt = stImage->pt;
+
    DBG("%s \n", __FUNCTION__);
 
-   stImage->transfer = screen->get_tex_transfer(screen, pt, stImage->face,
-                                                stImage->level, zoffset, 
-                                                usage, x, y, w, h);
+   stImage->transfer = st_no_flush_get_tex_transfer(st, pt, stImage->face,
+                                                   stImage->level, zoffset,
+                                                   usage, x, y, w, h);
 
    if (stImage->transfer)
       return screen->transfer_map(screen, stImage->transfer);
@@ -236,7 +240,7 @@ st_surface_data(struct pipe_context *pipe,
    struct pipe_screen *screen = pipe->screen;
    void *map = screen->transfer_map(screen, dst);
 
-   pipe_copy_rect(map,
+   util_copy_rect(map,
                   &dst->block,
                   dst->stride,
                   dstx, dsty, 
@@ -251,13 +255,14 @@ st_surface_data(struct pipe_context *pipe,
 /* Upload data for a particular image.
  */
 void
-st_texture_image_data(struct pipe_context *pipe,
+st_texture_image_data(struct st_context *st,
                       struct pipe_texture *dst,
                       GLuint face,
                       GLuint level,
                       void *src,
                       GLuint src_row_stride, GLuint src_image_stride)
 {
+   struct pipe_context *pipe = st->pipe;
    struct pipe_screen *screen = pipe->screen;
    GLuint depth = dst->depth[level];
    GLuint i;
@@ -265,11 +270,12 @@ st_texture_image_data(struct pipe_context *pipe,
    struct pipe_transfer *dst_transfer;
 
    DBG("%s\n", __FUNCTION__);
+
    for (i = 0; i < depth; i++) {
-      dst_transfer = screen->get_tex_transfer(screen, dst, face, level, i,
-                                              PIPE_TRANSFER_WRITE, 0, 0,
-                                              dst->width[level],
-                                              dst->height[level]);
+      dst_transfer = st_no_flush_get_tex_transfer(st, dst, face, level, i,
+                                                 PIPE_TRANSFER_WRITE, 0, 0,
+                                                 dst->width[level],
+                                                 dst->height[level]);
 
       st_surface_data(pipe, dst_transfer,
                      0, 0,                             /* dstx, dsty */
@@ -348,25 +354,95 @@ st_texture_image_copy(struct pipe_context *pipe,
    }
 }
 
-/** Bind a pipe surface for use as a texture image */
+
+/**
+ * Bind a pipe surface to a texture object.  After the call,
+ * the texture object is marked dirty and will be (re-)validated.
+ *
+ * If this is the first surface bound, the texture object is said to
+ * switch from normal to surface based.  It will be cleared first in
+ * this case.
+ *
+ * \param ps      pipe surface to be unbound
+ * \param target  texture target
+ * \param level   image level
+ * \param format  internal format of the texture
+ */
 int
-st_set_teximage(struct pipe_texture *pt, int target)
+st_bind_texture_surface(struct pipe_surface *ps, int target, int level,
+                        enum pipe_format format)
 {
    GET_CURRENT_CONTEXT(ctx);
    const GLuint unit = ctx->Texture.CurrentUnit;
    struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
    struct gl_texture_object *texObj;
    struct gl_texture_image *texImage;
+   struct st_texture_object *stObj;
    struct st_texture_image *stImage;
-   int internalFormat;
+   GLenum internalFormat;
 
-   switch (pt->format) {
-   case PIPE_FORMAT_A8R8G8B8_UNORM:
-      internalFormat = GL_RGBA8;
+   switch (target) {
+   case ST_TEXTURE_2D:
+      target = GL_TEXTURE_2D;
+      break;
+   case ST_TEXTURE_RECT:
+      target = GL_TEXTURE_RECTANGLE_ARB;
       break;
    default:
       return 0;
-   };
+   }
+
+   /* map pipe format to base format for now */
+   if (pf_get_component_bits(format, PIPE_FORMAT_COMP_A) > 0)
+      internalFormat = GL_RGBA;
+   else
+      internalFormat = GL_RGB;
+
+   texObj = _mesa_select_tex_object(ctx, texUnit, target);
+   _mesa_lock_texture(ctx, texObj);
+
+   stObj = st_texture_object(texObj);
+   /* switch to surface based */
+   if (!stObj->surface_based) {
+      _mesa_clear_texture_object(ctx, texObj);
+      stObj->surface_based = GL_TRUE;
+   }
+
+   texImage = _mesa_get_tex_image(ctx, texObj, target, level);
+   stImage = st_texture_image(texImage);
+
+   _mesa_init_teximage_fields(ctx, target, texImage,
+                              ps->width, ps->height, 1, 0, internalFormat);
+   texImage->TexFormat = st_ChooseTextureFormat(ctx, internalFormat,
+                                                GL_RGBA, GL_UNSIGNED_BYTE);
+   _mesa_set_fetch_functions(texImage, 2);
+   pipe_texture_reference(&stImage->pt, ps->texture);
+
+   _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
+   _mesa_unlock_texture(ctx, texObj);
+   
+   return 1;
+}
+
+
+/**
+ * Unbind a pipe surface from a texture object.  After the call,
+ * the texture object is marked dirty and will be (re-)validated.
+ *
+ * \param ps      pipe surface to be unbound
+ * \param target  texture target
+ * \param level   image level
+ */
+int
+st_unbind_texture_surface(struct pipe_surface *ps, int target, int level)
+{
+   GET_CURRENT_CONTEXT(ctx);
+   const GLuint unit = ctx->Texture.CurrentUnit;
+   struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
+   struct gl_texture_object *texObj;
+   struct gl_texture_image *texImage;
+   struct st_texture_object *stObj;
+   struct st_texture_image *stImage;
 
    switch (target) {
    case ST_TEXTURE_2D:
@@ -380,21 +456,28 @@ st_set_teximage(struct pipe_texture *pt, int target)
    }
 
    texObj = _mesa_select_tex_object(ctx, texUnit, target);
-   texImage = _mesa_get_tex_image(ctx, texObj, target, 0);
+
+   _mesa_lock_texture(ctx, texObj);
+
+   texImage = _mesa_get_tex_image(ctx, texObj, target, level);
+   stObj = st_texture_object(texObj);
    stImage = st_texture_image(texImage);
-   
-   _mesa_init_teximage_fields(ctx, GL_TEXTURE_2D, texImage, pt->width[0],
-                              pt->height[0], 1, 0, internalFormat);
 
-   texImage->TexFormat = st_ChooseTextureFormat(ctx, internalFormat, GL_RGBA,
-                                                GL_UNSIGNED_BYTE);
-   _mesa_set_fetch_functions(texImage, 2);
+   /* Make sure the pipe surface is still bound.  The texture object is still
+    * considered surface based even if this is the last bound surface. */
+   if (stImage->pt == ps->texture) {
+      pipe_texture_reference(&stImage->pt, NULL);
+      _mesa_clear_texture_image(ctx, texImage);
 
-   pipe_texture_reference(&stImage->pt, pt);
+      _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
+   }
 
+   _mesa_unlock_texture(ctx, texObj);
+   
    return 1;
 }
 
+
 /** Redirect rendering into stfb's surface to a texture image */
 int
 st_bind_teximage(struct st_framebuffer *stfb, uint surfIndex,
@@ -481,3 +564,20 @@ st_release_teximage(struct st_framebuffer *stfb, uint surfIndex,
 
    return 1;
 }
+
+void
+st_teximage_flush_before_map(struct st_context *st,
+                            struct pipe_texture *pt,
+                            unsigned int face,
+                            unsigned int level,
+                            enum pipe_transfer_usage usage)
+{
+   struct pipe_context *pipe = st->pipe;
+   unsigned referenced =
+      pipe->is_texture_referenced(pipe, pt, face, level);
+
+   if (referenced && ((referenced & PIPE_REFERENCED_FOR_WRITE) ||
+                     usage == PIPE_TRANSFER_WRITE ||
+                     usage == PIPE_TRANSFER_READ_WRITE))
+      st_flush(st, PIPE_FLUSH_RENDER_CACHE, NULL);
+}