gallium: add flag PIPE_TRANSFER_MAP_PERMANENTLY
[mesa.git] / src / gallium / drivers / r300 / r300_transfer.c
index ec89681a3c00a013d58afc63120148e8731db99f..91e861a4a73327531c45499d3bd8cfc8a05e0264 100644 (file)
  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  * USE OR OTHER DEALINGS IN THE SOFTWARE. */
 
-#include "r300_context.h"
 #include "r300_transfer.h"
-#include "r300_texture.h"
-#include "r300_screen.h"
+#include "r300_texture_desc.h"
+#include "r300_screen_buffer.h"
 
 #include "util/u_memory.h"
 #include "util/u_format.h"
+#include "util/u_box.h"
 
 struct r300_transfer {
     /* Parent class */
     struct pipe_transfer transfer;
 
-    /* Pipe context. */
-    struct pipe_context *ctx;
-
-    /* Parameters of get_tex_transfer. */
-    unsigned x, y, level, zslice, face;
-
     /* Offset from start of buffer. */
     unsigned offset;
 
-    /* Detiled texture. */
-    struct r300_texture *detiled_texture;
-
-    /* Transfer and format flags. */
-    unsigned buffer_usage, render_target_usage;
+    /* Linear texture. */
+    struct r300_resource *linear_texture;
 };
 
 /* Convenience cast wrapper. */
@@ -60,211 +51,223 @@ r300_transfer(struct pipe_transfer* transfer)
 static void r300_copy_from_tiled_texture(struct pipe_context *ctx,
                                          struct r300_transfer *r300transfer)
 {
-    struct pipe_screen *screen = ctx->screen;
     struct pipe_transfer *transfer = (struct pipe_transfer*)r300transfer;
-    struct pipe_texture *tex = transfer->texture;
-    struct pipe_surface *src, *dst;
-
-    src = screen->get_tex_surface(screen, tex, r300transfer->face,
-                                  r300transfer->level, r300transfer->zslice,
-                                  PIPE_BUFFER_USAGE_GPU_READ |
-                                  PIPE_BUFFER_USAGE_PIXEL);
+    struct pipe_resource *tex = transfer->resource;
 
-    dst = screen->get_tex_surface(screen, &r300transfer->detiled_texture->tex,
-                                  0, 0, 0,
-                                  PIPE_BUFFER_USAGE_GPU_WRITE |
-                                  PIPE_BUFFER_USAGE_PIXEL |
-                                  r300transfer->buffer_usage);
-
-    ctx->surface_copy(ctx, dst, 0, 0, src, r300transfer->x, r300transfer->y,
-                      transfer->width, transfer->height);
-
-    pipe_surface_reference(&src, NULL);
-    pipe_surface_reference(&dst, NULL);
+    ctx->resource_copy_region(ctx, &r300transfer->linear_texture->b.b.b, 0,
+                              0, 0, 0,
+                              tex, transfer->level, &transfer->box);
 }
 
 /* Copy a detiled texture to a tiled one. */
 static void r300_copy_into_tiled_texture(struct pipe_context *ctx,
                                          struct r300_transfer *r300transfer)
 {
-    struct pipe_screen *screen = ctx->screen;
     struct pipe_transfer *transfer = (struct pipe_transfer*)r300transfer;
-    struct pipe_texture *tex = transfer->texture;
-    struct pipe_surface *src, *dst;
-
-    src = screen->get_tex_surface(screen, &r300transfer->detiled_texture->tex,
-                                  0, 0, 0,
-                                  PIPE_BUFFER_USAGE_GPU_READ |
-                                  PIPE_BUFFER_USAGE_PIXEL);
-
-    dst = screen->get_tex_surface(screen, tex, r300transfer->face,
-                                  r300transfer->level, r300transfer->zslice,
-                                  PIPE_BUFFER_USAGE_GPU_WRITE |
-                                  PIPE_BUFFER_USAGE_PIXEL);
+    struct pipe_resource *tex = transfer->resource;
+    struct pipe_box src_box;
+    u_box_origin_2d(transfer->box.width, transfer->box.height, &src_box);
 
-    /* XXX this flush prevents the following DRM error from occuring:
-     * [drm:radeon_cs_ioctl] *ERROR* Failed to parse relocation !
-     * Reproducible with perf/copytex. */
-    ctx->flush(ctx, 0, NULL);
+    ctx->resource_copy_region(ctx, tex, transfer->level,
+                              transfer->box.x, transfer->box.y, transfer->box.z,
+                              &r300transfer->linear_texture->b.b.b, 0, &src_box);
 
-    ctx->surface_copy(ctx, dst, r300transfer->x, r300transfer->y, src, 0, 0,
-                      transfer->width, transfer->height);
-
-    /* XXX this flush fixes a few piglit tests (e.g. glean/pixelFormats). */
-    ctx->flush(ctx, 0, NULL);
-
-    pipe_surface_reference(&src, NULL);
-    pipe_surface_reference(&dst, NULL);
+    /* XXX remove this. */
+    r300_flush(ctx, 0, NULL);
 }
 
-static struct pipe_transfer*
-r300_get_tex_transfer(struct pipe_screen *screen,
-                      struct pipe_texture *texture,
-                      unsigned face, unsigned level, unsigned zslice,
-                      enum pipe_transfer_usage usage, unsigned x, unsigned y,
-                      unsigned w, unsigned h)
+struct pipe_transfer*
+r300_texture_get_transfer(struct pipe_context *ctx,
+                          struct pipe_resource *texture,
+                          unsigned level,
+                          unsigned usage,
+                          const struct pipe_box *box)
 {
-    struct r300_texture *tex = (struct r300_texture *)texture;
+    struct r300_context *r300 = r300_context(ctx);
+    struct r300_resource *tex = r300_resource(texture);
     struct r300_transfer *trans;
-    struct r300_screen *r300screen = r300_screen(screen);
-    struct pipe_texture template;
+    struct pipe_resource base;
+    boolean referenced_cs, referenced_hw;
+
+    if (usage & (PIPE_TRANSFER_MAP_DIRECTLY | PIPE_TRANSFER_MAP_PERMANENTLY)) {
+        return NULL;
+    }
+
+    referenced_cs =
+        r300->rws->cs_is_buffer_referenced(r300->cs, tex->cs_buf);
+    if (referenced_cs) {
+        referenced_hw = TRUE;
+    } else {
+        referenced_hw =
+            r300->rws->buffer_is_busy(tex->buf, RADEON_USAGE_READWRITE);
+    }
 
     trans = CALLOC_STRUCT(r300_transfer);
     if (trans) {
         /* Initialize the transfer object. */
-        pipe_texture_reference(&trans->transfer.texture, texture);
+        pipe_resource_reference(&trans->transfer.resource, texture);
+        trans->transfer.level = level;
         trans->transfer.usage = usage;
-        trans->transfer.width = w;
-        trans->transfer.height = h;
-        trans->ctx = r300screen->ctx;
-        trans->x = x;
-        trans->y = y;
-        trans->level = level;
-        trans->zslice = zslice;
-        trans->face = face;
+        trans->transfer.box = *box;
 
         /* If the texture is tiled, we must create a temporary detiled texture
-         * for this transfer. */
-        if (tex->microtile || tex->macrotile) {
-            trans->buffer_usage = pipe_transfer_buffer_flags(&trans->transfer);
-            trans->render_target_usage =
-                util_format_is_depth_or_stencil(texture->format) ?
-                PIPE_TEXTURE_USAGE_DEPTH_STENCIL :
-                PIPE_TEXTURE_USAGE_RENDER_TARGET;
-
-            template.target = PIPE_TEXTURE_2D;
-            template.format = texture->format;
-            template.width0 = w;
-            template.height0 = h;
-            template.depth0 = 0;
-            template.last_level = 0;
-            template.nr_samples = 0;
-            template.tex_usage = PIPE_TEXTURE_USAGE_DYNAMIC |
-                                 R300_TEXTURE_USAGE_TRANSFER;
+         * for this transfer.
+         * Also make write transfers pipelined. */
+        if (tex->tex.microtile || tex->tex.macrotile[level] ||
+            (referenced_hw && !(usage & PIPE_TRANSFER_READ) &&
+             r300_is_blit_supported(texture->format))) {
+            if (r300->blitter->running) {
+                fprintf(stderr, "r300: ERROR: Blitter recursion in texture_get_transfer.\n");
+                os_break();
+            }
+
+            base.target = PIPE_TEXTURE_2D;
+            base.format = texture->format;
+            base.width0 = box->width;
+            base.height0 = box->height;
+            base.depth0 = 1;
+            base.array_size = 1;
+            base.last_level = 0;
+            base.nr_samples = 0;
+            base.usage = PIPE_USAGE_STAGING;
+            base.bind = 0;
+            if (usage & PIPE_TRANSFER_READ) {
+                base.bind |= PIPE_BIND_SAMPLER_VIEW;
+            }
+            if (usage & PIPE_TRANSFER_WRITE) {
+                base.bind |= PIPE_BIND_RENDER_TARGET;
+            }
+            base.flags = R300_RESOURCE_FLAG_TRANSFER;
 
             /* For texture reading, the temporary (detiled) texture is used as
              * a render target when blitting from a tiled texture. */
             if (usage & PIPE_TRANSFER_READ) {
-                template.tex_usage |= trans->render_target_usage;
+                base.bind |= PIPE_BIND_RENDER_TARGET;
             }
             /* For texture writing, the temporary texture is used as a sampler
              * when blitting into a tiled texture. */
             if (usage & PIPE_TRANSFER_WRITE) {
-                template.tex_usage |= PIPE_TEXTURE_USAGE_SAMPLER;
+                base.bind |= PIPE_BIND_SAMPLER_VIEW;
             }
 
             /* Create the temporary texture. */
-            trans->detiled_texture =
-                (struct r300_texture*)screen->texture_create(screen, &template);
-            assert(!trans->detiled_texture->microtile &&
-                   !trans->detiled_texture->macrotile);
+            trans->linear_texture = r300_resource(
+               ctx->screen->resource_create(ctx->screen,
+                                            &base));
+
+            if (!trans->linear_texture) {
+                /* Oh crap, the thing can't create the texture.
+                 * Let's flush and try again. */
+                r300_flush(ctx, 0, NULL);
+
+                trans->linear_texture = r300_resource(
+                   ctx->screen->resource_create(ctx->screen,
+                                                &base));
+
+                if (!trans->linear_texture) {
+                    /* For linear textures, it's safe to fallback to
+                     * an unpipelined transfer. */
+                    if (!tex->tex.microtile && !tex->tex.macrotile[level]) {
+                        goto unpipelined;
+                    }
+
+                    /* Otherwise, go to hell. */
+                    fprintf(stderr,
+                        "r300: Failed to create a transfer object, praise.\n");
+                    FREE(trans);
+                    return NULL;
+                }
+            }
+
+            assert(!trans->linear_texture->tex.microtile &&
+                   !trans->linear_texture->tex.macrotile[0]);
 
-            /* Set the stride.
-             * Parameters x, y, level, zslice, and face remain zero. */
+            /* Set the stride. */
             trans->transfer.stride =
-                r300_texture_get_stride(r300screen, trans->detiled_texture, 0);
+                    trans->linear_texture->tex.stride_in_bytes[0];
 
             if (usage & PIPE_TRANSFER_READ) {
                 /* We cannot map a tiled texture directly because the data is
                  * in a different order, therefore we do detiling using a blit. */
-                r300_copy_from_tiled_texture(r300screen->ctx, trans);
+                r300_copy_from_tiled_texture(ctx, trans);
+
+                /* Always referenced in the blit. */
+                r300_flush(ctx, 0, NULL);
             }
-        } else {
-            trans->transfer.x = x;
-            trans->transfer.y = y;
-            trans->transfer.stride =
-                r300_texture_get_stride(r300screen, tex, level);
-            trans->transfer.level = level;
-            trans->transfer.zslice = zslice;
-            trans->transfer.face = face;
-            trans->offset = r300_texture_get_offset(tex, level, zslice, face);
+            return &trans->transfer;
         }
+
+    unpipelined:
+        /* Unpipelined transfer. */
+        trans->transfer.stride = tex->tex.stride_in_bytes[level];
+        trans->offset = r300_texture_get_offset(tex, level, box->z);
+
+        if (referenced_cs &&
+            !(usage & PIPE_TRANSFER_UNSYNCHRONIZED))
+            r300_flush(ctx, 0, NULL);
+        return &trans->transfer;
     }
-    return &trans->transfer;
+    return NULL;
 }
 
-static void r300_tex_transfer_destroy(struct pipe_transfer *trans)
+void r300_texture_transfer_destroy(struct pipe_context *ctx,
+                                  struct pipe_transfer *trans)
 {
     struct r300_transfer *r300transfer = r300_transfer(trans);
 
-    if (r300transfer->detiled_texture) {
+    if (r300transfer->linear_texture) {
         if (trans->usage & PIPE_TRANSFER_WRITE) {
-            r300_copy_into_tiled_texture(r300transfer->ctx, r300transfer);
+            r300_copy_into_tiled_texture(ctx, r300transfer);
         }
 
-        pipe_texture_reference(
-            (struct pipe_texture**)&r300transfer->detiled_texture, NULL);
+        pipe_resource_reference(
+            (struct pipe_resource**)&r300transfer->linear_texture, NULL);
     }
-    pipe_texture_reference(&trans->texture, NULL);
+    pipe_resource_reference(&trans->resource, NULL);
     FREE(trans);
 }
 
-static void* r300_transfer_map(struct pipe_screen *screen,
-                               struct pipe_transfer *transfer)
+void* r300_texture_transfer_map(struct pipe_context *ctx,
+                               struct pipe_transfer *transfer)
 {
+    struct r300_context *r300 = r300_context(ctx);
+    struct radeon_winsys *rws = (struct radeon_winsys *)ctx->winsys;
     struct r300_transfer *r300transfer = r300_transfer(transfer);
-    struct r300_texture *tex = (struct r300_texture*)transfer->texture;
+    struct r300_resource *tex = r300_resource(transfer->resource);
     char *map;
-    enum pipe_format format = tex->tex.format;
+    enum pipe_format format = tex->b.b.b.format;
 
-    if (r300transfer->detiled_texture) {
+    if (r300transfer->linear_texture) {
         /* The detiled texture is of the same size as the region being mapped
          * (no offset needed). */
-        return pipe_buffer_map(screen,
-                               r300transfer->detiled_texture->buffer,
-                               pipe_transfer_buffer_flags(transfer));
+        return rws->buffer_map(r300transfer->linear_texture->buf,
+                               r300->cs,
+                               transfer->usage);
     } else {
         /* Tiling is disabled. */
-        map = pipe_buffer_map(screen, tex->buffer,
-                              pipe_transfer_buffer_flags(transfer));
+        map = rws->buffer_map(tex->buf, r300->cs,
+                              transfer->usage);
 
         if (!map) {
             return NULL;
         }
 
         return map + r300_transfer(transfer)->offset +
-            transfer->y / util_format_get_blockheight(format) * transfer->stride +
-            transfer->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
+            transfer->box.y / util_format_get_blockheight(format) * transfer->stride +
+            transfer->box.x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
     }
 }
 
-static void r300_transfer_unmap(struct pipe_screen *screen,
-                                struct pipe_transfer *transfer)
+void r300_texture_transfer_unmap(struct pipe_context *ctx,
+                                struct pipe_transfer *transfer)
 {
+    struct radeon_winsys *rws = (struct radeon_winsys *)ctx->winsys;
     struct r300_transfer *r300transfer = r300_transfer(transfer);
-    struct r300_texture *tex = (struct r300_texture*)transfer->texture;
+    struct r300_resource *tex = r300_resource(transfer->resource);
 
-    if (r300transfer->detiled_texture) {
-        pipe_buffer_unmap(screen, r300transfer->detiled_texture->buffer);
+    if (r300transfer->linear_texture) {
+        rws->buffer_unmap(r300transfer->linear_texture->buf);
     } else {
-        pipe_buffer_unmap(screen, tex->buffer);
+        rws->buffer_unmap(tex->buf);
     }
 }
-
-void r300_init_screen_transfer_functions(struct pipe_screen *screen)
-{
-    screen->get_tex_transfer = r300_get_tex_transfer;
-    screen->tex_transfer_destroy = r300_tex_transfer_destroy;
-    screen->transfer_map = r300_transfer_map;
-    screen->transfer_unmap = r300_transfer_unmap;
-}