mesa: Prefix main includes with dir to avoid conflicts.
[mesa.git] / src / mesa / state_tracker / st_texture.c
index a5582c31c0bde0994aaf8cf1c5a23581559295b5..289b78b38b340696a3fe84a58492e04274827e27 100644 (file)
 #include "st_context.h"
 #include "st_format.h"
 #include "st_texture.h"
-#include "enums.h"
+#include "main/enums.h"
+
+#undef Elements  /* fix re-defined macro warning */
 
 #include "pipe/p_state.h"
 #include "pipe/p_context.h"
 #include "pipe/p_defines.h"
+#include "pipe/p_inlines.h"
 #include "pipe/p_util.h"
 #include "pipe/p_inlines.h"
-#include "pipe/p_winsys.h"
 
 
 #define DBG if(0) printf
@@ -58,67 +60,73 @@ target_to_target(GLenum target)
 }
 #endif
 
+
+/**
+ * Allocate a new pipe_texture object
+ * width0, height0, depth0 are the dimensions of the level 0 image
+ * (the highest resolution).  last_level indicates how many mipmap levels
+ * to allocate storage for.  For non-mipmapped textures, this will be zero.
+ */
 struct pipe_texture *
 st_texture_create(struct st_context *st,
-                  unsigned target,
-                 unsigned format,
-                 GLenum internal_format,
-                 GLuint first_level,
+                  enum pipe_texture_target target,
+                 enum pipe_format format,
                  GLuint last_level,
                  GLuint width0,
                  GLuint height0,
                  GLuint depth0,
-                 GLuint compress_byte)
+                 GLuint compress_byte,
+                  GLuint usage )
 {
-   struct pipe_texture *pt = CALLOC_STRUCT(pipe_texture);
+   struct pipe_texture pt, *newtex;
+   struct pipe_screen *screen = st->pipe->screen;
 
    assert(target <= PIPE_TEXTURE_CUBE);
 
-   DBG("%s target %s format %s level %d..%d\n", __FUNCTION__,
+   DBG("%s target %s format %s last_level %d\n", __FUNCTION__,
        _mesa_lookup_enum_by_nr(target),
-       _mesa_lookup_enum_by_nr(internal_format), first_level, last_level);
-
-   if (!pt)
-      return NULL;
+       _mesa_lookup_enum_by_nr(format), last_level);
 
    assert(format);
+   assert(screen->is_format_supported(screen, format, target, 
+                                      PIPE_TEXTURE_USAGE_SAMPLER, 0));
 
-   pt->target = target;
-   pt->format = format;
-   pt->internal_format = internal_format;
-   pt->first_level = first_level;
-   pt->last_level = last_level;
-   pt->width[0] = width0;
-   pt->height[0] = height0;
-   pt->depth[0] = depth0;
-   pt->compressed = compress_byte ? 1 : 0;
-   pt->cpp = pt->compressed ? compress_byte : st_sizeof_format(format);
-   pt->refcount = 1; 
-
-   st->pipe->texture_create(st->pipe, &pt);
-
-   return pt;
-}
+   memset(&pt, 0, sizeof(pt));
+   pt.target = target;
+   pt.format = format;
+   pt.last_level = last_level;
+   pt.width[0] = width0;
+   pt.height[0] = height0;
+   pt.depth[0] = depth0;
+   pt.compressed = compress_byte ? 1 : 0;
+   pf_get_block(format, &pt.block);
+   pt.tex_usage = usage;
 
+   newtex = screen->texture_create(screen, &pt);
 
+   assert(!newtex || newtex->refcount == 1);
 
+   return newtex;
+}
 
-/* Can the image be pulled into a unified mipmap texture.  This mirrors
- * the completeness test in a lot of ways.
+
+/**
+ * Check if a texture image be pulled into a unified mipmap texture.
+ * This mirrors the completeness test in a lot of ways.
  *
  * Not sure whether I want to pass gl_texture_image here.
  */
 GLboolean
-st_texture_match_image(struct pipe_texture *pt,
-                          struct gl_texture_image *image,
-                          GLuint face, GLuint level)
+st_texture_match_image(const struct pipe_texture *pt,
+                       const struct gl_texture_image *image,
+                       GLuint face, GLuint level)
 {
    /* Images with borders are never pulled into mipmap textures. 
     */
    if (image->Border) 
       return GL_FALSE;
 
-   if (image->InternalFormat != pt->internal_format ||
+   if (st_mesa_format_to_pipe_format(image->TexFormat->MesaFormat) != pt->format ||
        image->IsCompressed != pt->compressed)
       return GL_FALSE;
 
@@ -179,31 +187,69 @@ st_texture_image_offset(const struct pipe_texture * pt,
  */
 GLubyte *
 st_texture_image_map(struct st_context *st, struct st_texture_image *stImage,
-                    GLuint zoffset)
+                    GLuint zoffset,
+                     GLuint flags )
 {
+   struct pipe_screen *screen = st->pipe->screen;
    struct pipe_texture *pt = stImage->pt;
    DBG("%s \n", __FUNCTION__);
 
-   stImage->surface = st->pipe->get_tex_surface(st->pipe, pt, stImage->face,
-                                               stImage->level, zoffset);
+   stImage->surface = screen->get_tex_surface(screen, pt, stImage->face,
+                                              stImage->level, zoffset, 
+                                              flags);
 
-   (void) st->pipe->region_map(st->pipe, stImage->surface->region);
-
-   return stImage->surface->region->map + stImage->surface->offset;
+   if (stImage->surface)
+      return screen->surface_map(screen, stImage->surface, flags);
+   else
+      return NULL;
 }
 
+
 void
-st_texture_image_unmap(struct st_context *st, struct st_texture_image *stImage)
+st_texture_image_unmap(struct st_context *st,
+                       struct st_texture_image *stImage)
 {
+   struct pipe_screen *screen = st->pipe->screen;
+
    DBG("%s\n", __FUNCTION__);
 
-   st->pipe->region_unmap(st->pipe, stImage->surface->region);
+   screen->surface_unmap(screen, stImage->surface);
 
    pipe_surface_reference(&stImage->surface, NULL);
 }
 
 
 
+/**
+ * Upload data to a rectangular sub-region.  Lots of choices how to do this:
+ *
+ * - memcpy by span to current destination
+ * - upload data as new buffer and blit
+ *
+ * Currently always memcpy.
+ */
+static void
+st_surface_data(struct pipe_context *pipe,
+               struct pipe_surface *dst,
+               unsigned dstx, unsigned dsty,
+               const void *src, unsigned src_stride,
+               unsigned srcx, unsigned srcy, unsigned width, unsigned height)
+{
+   struct pipe_screen *screen = pipe->screen;
+   void *map = screen->surface_map(screen, dst, PIPE_BUFFER_USAGE_CPU_WRITE);
+
+   pipe_copy_rect(map,
+                  &dst->block,
+                  dst->stride,
+                  dstx, dsty, 
+                  width, height, 
+                  src, src_stride, 
+                  srcx, srcy);
+
+   screen->surface_unmap(screen, dst);
+}
+
+
 /* Upload data for a particular image.
  */
 void
@@ -212,65 +258,96 @@ st_texture_image_data(struct pipe_context *pipe,
                       GLuint face,
                       GLuint level,
                       void *src,
-                      GLuint src_row_pitch, GLuint src_image_pitch)
+                      GLuint src_row_stride, GLuint src_image_stride)
 {
+   struct pipe_screen *screen = pipe->screen;
    GLuint depth = dst->depth[level];
    GLuint i;
-   GLuint height = 0;
    const GLubyte *srcUB = src;
    struct pipe_surface *dst_surface;
 
    DBG("%s\n", __FUNCTION__);
    for (i = 0; i < depth; i++) {
-      height = dst->height[level];
-      if(dst->compressed)
-        height /= 4;
-
-      dst_surface = pipe->get_tex_surface(pipe, dst, face, level, i);
+      dst_surface = screen->get_tex_surface(screen, dst, face, level, i,
+                                            PIPE_BUFFER_USAGE_CPU_WRITE);
 
-      pipe->surface_data(pipe, dst_surface,
-                        0, 0,                             /* dstx, dsty */
-                        srcUB,
-                        src_row_pitch,
-                        0, 0,                             /* source x, y */
-                        dst->width[level], height); /* width, height */
+      st_surface_data(pipe, dst_surface,
+                     0, 0,                             /* dstx, dsty */
+                     srcUB,
+                     src_row_stride,
+                     0, 0,                             /* source x, y */
+                     dst->width[level], dst->height[level]);       /* width, height */
 
-      pipe_surface_reference(&dst_surface, NULL);
+      screen->tex_surface_release(screen, &dst_surface);
 
-      srcUB += src_image_pitch * dst->cpp;
+      srcUB += src_image_stride;
    }
 }
 
+
 /* Copy mipmap image between textures
  */
 void
 st_texture_image_copy(struct pipe_context *pipe,
-                         struct pipe_texture *dst,
-                         GLuint face, GLuint level,
-                         struct pipe_texture *src)
+                      struct pipe_texture *dst, GLuint dstLevel,
+                      struct pipe_texture *src,
+                      GLuint face)
 {
-   GLuint width = src->width[level];
-   GLuint height = src->height[level];
-   GLuint depth = src->depth[level];
+   struct pipe_screen *screen = pipe->screen;
+   GLuint width = dst->width[dstLevel];
+   GLuint height = dst->height[dstLevel];
+   GLuint depth = dst->depth[dstLevel];
    struct pipe_surface *src_surface;
    struct pipe_surface *dst_surface;
    GLuint i;
 
-   if (dst->compressed)
-      height /= 4;
+   /* XXX this is a hack */
+   const GLuint copyHeight = dst->compressed ? height / 4 : height;
+
    for (i = 0; i < depth; i++) {
-      dst_surface = pipe->get_tex_surface(pipe, dst, face, level, i);
-      src_surface = pipe->get_tex_surface(pipe, src, face, level, i);
+      GLuint srcLevel;
+
+      /* find src texture level of needed size */
+      for (srcLevel = 0; srcLevel <= src->last_level; srcLevel++) {
+         if (src->width[srcLevel] == width &&
+             src->height[srcLevel] == height) {
+            break;
+         }
+      }
+      assert(src->width[srcLevel] == width);
+      assert(src->height[srcLevel] == height);
+
+#if 0
+      {
+         src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i,
+                                               PIPE_BUFFER_USAGE_CPU_READ);
+         ubyte *map = screen->surface_map(screen, src_surface, PIPE_BUFFER_USAGE_CPU_READ);
+         map += src_surface->width * src_surface->height * 4 / 2;
+         printf("%s center pixel: %d %d %d %d (pt %p[%d] -> %p[%d])\n",
+                __FUNCTION__,
+                map[0], map[1], map[2], map[3],
+                src, srcLevel, dst, dstLevel);
+
+         screen->surface_unmap(screen, src_surface);
+         pipe_surface_reference(&src_surface, NULL);
+      }
+#endif
+
+      dst_surface = screen->get_tex_surface(screen, dst, face, dstLevel, i,
+                                            PIPE_BUFFER_USAGE_GPU_WRITE);
+
+      src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i,
+                                            PIPE_BUFFER_USAGE_GPU_READ);
 
       pipe->surface_copy(pipe,
+                         FALSE,
                         dst_surface,
                         0, 0, /* destX, Y */
                         src_surface,
                         0, 0, /* srcX, Y */
-                        width, height);
+                        width, copyHeight);
 
-      pipe_surface_reference(&dst_surface, NULL);
-      pipe_surface_reference(&src_surface, NULL);
+      screen->tex_surface_release(screen, &src_surface);
+      screen->tex_surface_release(screen, &dst_surface);
    }
-
 }