r300g: consolidate buffers and textures to r300_resource
[mesa.git] / src / gallium / drivers / r300 / r300_transfer.c
index 3cc4c8c958279f3cb9b3dd52b5e7a1c7e8878f30..7265fa733e942c9fc0cb58a88caf153e82f71113 100644 (file)
  * USE OR OTHER DEALINGS IN THE SOFTWARE. */
 
 #include "r300_transfer.h"
-#include "r300_texture.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 */
@@ -35,8 +36,8 @@ struct r300_transfer {
     /* Offset from start of buffer. */
     unsigned offset;
 
-    /* Detiled texture. */
-    struct r300_texture *detiled_texture;
+    /* Linear texture. */
+    struct r300_resource *linear_texture;
 };
 
 /* Convenience cast wrapper. */
@@ -52,16 +53,10 @@ static void r300_copy_from_tiled_texture(struct pipe_context *ctx,
 {
     struct pipe_transfer *transfer = (struct pipe_transfer*)r300transfer;
     struct pipe_resource *tex = transfer->resource;
-    struct pipe_subresource subdst;
 
-    subdst.face = 0;
-    subdst.level = 0;
-
-    ctx->resource_copy_region(ctx, &r300transfer->detiled_texture->b.b, subdst,
-                             0, 0, 0,
-                             tex, transfer->sr,
-                             transfer->box.x, transfer->box.y, transfer->box.z,
-                             transfer->box.width, transfer->box.height);
+    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. */
@@ -70,43 +65,38 @@ static void r300_copy_into_tiled_texture(struct pipe_context *ctx,
 {
     struct pipe_transfer *transfer = (struct pipe_transfer*)r300transfer;
     struct pipe_resource *tex = transfer->resource;
-    struct pipe_subresource subsrc;
-
-    subsrc.face = 0;
-    subsrc.level = 0;
+    struct pipe_box src_box;
+    u_box_origin_2d(transfer->box.width, transfer->box.height, &src_box);
 
-    ctx->resource_copy_region(ctx, tex, transfer->sr,
-                             transfer->box.x, transfer->box.y, transfer->box.z,
-                             &r300transfer->detiled_texture->b.b, subsrc,
-                             0, 0, 0,
-                             transfer->box.width, transfer->box.height);
+    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->flush(ctx, 0, NULL);
 }
 
 struct pipe_transfer*
 r300_texture_get_transfer(struct pipe_context *ctx,
-                         struct pipe_resource *texture,
-                         struct pipe_subresource sr,
-                         unsigned usage,
-                         const struct pipe_box *box)
+                          struct pipe_resource *texture,
+                          unsigned level,
+                          unsigned usage,
+                          const struct pipe_box *box)
 {
     struct r300_context *r300 = r300_context(ctx);
-    struct r300_texture *tex = r300_texture(texture);
-    struct r300_screen *r300screen = r300_screen(ctx->screen);
+    struct r300_resource *tex = r300_resource(texture);
     struct r300_transfer *trans;
     struct pipe_resource base;
     boolean referenced_cs, referenced_hw, blittable;
 
     referenced_cs =
         r300->rws->cs_is_buffer_referenced(r300->cs,
-                                           tex->buffer, R300_REF_CS);
+                                           tex->cs_buf, R300_REF_CS);
     if (referenced_cs) {
         referenced_hw = TRUE;
     } else {
         referenced_hw =
             r300->rws->cs_is_buffer_referenced(r300->cs,
-                                               tex->buffer, R300_REF_HW);
+                                               tex->cs_buf, R300_REF_HW);
     }
 
     blittable = ctx->screen->is_format_supported(
@@ -117,25 +107,26 @@ r300_texture_get_transfer(struct pipe_context *ctx,
     if (trans) {
         /* Initialize the transfer object. */
         pipe_resource_reference(&trans->transfer.resource, texture);
-        trans->transfer.sr = sr;
+        trans->transfer.level = level;
         trans->transfer.usage = usage;
         trans->transfer.box = *box;
 
         /* If the texture is tiled, we must create a temporary detiled texture
          * for this transfer.
          * Also make write transfers pipelined. */
-        if (tex->microtile || tex->macrotile ||
+        if (tex->tex.microtile || tex->tex.macrotile[level] ||
             ((referenced_hw & !(usage & PIPE_TRANSFER_READ)) && blittable)) {
             base.target = PIPE_TEXTURE_2D;
             base.format = texture->format;
             base.width0 = box->width;
             base.height0 = box->height;
-            base.depth0 = 0;
+            base.depth0 = 1;
+            base.array_size = 1;
             base.last_level = 0;
             base.nr_samples = 0;
             base.usage = PIPE_USAGE_DYNAMIC;
             base.bind = 0;
-           base.flags = R300_RESOURCE_FLAG_TRANSFER;
+            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. */
@@ -149,23 +140,23 @@ r300_texture_get_transfer(struct pipe_context *ctx,
             }
 
             /* Create the temporary texture. */
-            trans->detiled_texture = r300_texture(
+            trans->linear_texture = r300_resource(
                ctx->screen->resource_create(ctx->screen,
                                             &base));
 
-            if (!trans->detiled_texture) {
+            if (!trans->linear_texture) {
                 /* Oh crap, the thing can't create the texture.
                  * Let's flush and try again. */
                 ctx->flush(ctx, 0, NULL);
 
-                trans->detiled_texture = r300_texture(
+                trans->linear_texture = r300_resource(
                    ctx->screen->resource_create(ctx->screen,
                                                 &base));
 
-                if (!trans->detiled_texture) {
+                if (!trans->linear_texture) {
                     /* For linear textures, it's safe to fallback to
                      * an unpipelined transfer. */
-                    if (!tex->microtile && !tex->macrotile) {
+                    if (!tex->tex.microtile && !tex->tex.macrotile[level]) {
                         goto unpipelined;
                     }
 
@@ -177,18 +168,18 @@ r300_texture_get_transfer(struct pipe_context *ctx,
                 }
             }
 
-            assert(!trans->detiled_texture->microtile &&
-                   !trans->detiled_texture->macrotile);
+            assert(!trans->linear_texture->tex.microtile &&
+                   !trans->linear_texture->tex.macrotile[0]);
 
             /* Set the stride.
             *
             * Even though we are using an internal texture for this,
-            * the transfer sr, box and usage parameters still reflect
+            * the transfer level, box and usage parameters still reflect
             * the arguments received to get_transfer.  We just do the
             * right thing internally.
             */
             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
@@ -203,9 +194,8 @@ r300_texture_get_transfer(struct pipe_context *ctx,
 
     unpipelined:
         /* Unpipelined transfer. */
-        trans->transfer.stride =
-                r300_texture_get_stride(r300screen, tex, sr.level);
-        trans->offset = r300_texture_get_offset(tex, sr.level, box->z, sr.face);
+        trans->transfer.stride = tex->tex.stride_in_bytes[level];
+        trans->offset = r300_texture_get_offset(tex, level, box->z);
 
         if (referenced_cs)
             ctx->flush(ctx, PIPE_FLUSH_RENDER_CACHE, NULL);
@@ -219,13 +209,13 @@ void r300_texture_transfer_destroy(struct pipe_context *ctx,
 {
     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(ctx, r300transfer);
         }
 
         pipe_resource_reference(
-            (struct pipe_resource**)&r300transfer->detiled_texture, NULL);
+            (struct pipe_resource**)&r300transfer->linear_texture, NULL);
     }
     pipe_resource_reference(&trans->resource, NULL);
     FREE(trans);
@@ -237,20 +227,20 @@ void* r300_texture_transfer_map(struct pipe_context *ctx,
     struct r300_context *r300 = r300_context(ctx);
     struct r300_winsys_screen *rws = (struct r300_winsys_screen *)ctx->winsys;
     struct r300_transfer *r300transfer = r300_transfer(transfer);
-    struct r300_texture *tex = r300_texture(transfer->resource);
+    struct r300_resource *tex = r300_resource(transfer->resource);
     char *map;
-    enum pipe_format format = tex->b.b.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 rws->buffer_map(rws,
-                               r300transfer->detiled_texture->buffer,
+                               r300transfer->linear_texture->buf,
                                r300->cs,
                                transfer->usage);
     } else {
         /* Tiling is disabled. */
-        map = rws->buffer_map(rws, tex->buffer, r300->cs,
+        map = rws->buffer_map(rws, tex->buf, r300->cs,
                               transfer->usage);
 
         if (!map) {
@@ -268,11 +258,11 @@ void r300_texture_transfer_unmap(struct pipe_context *ctx,
 {
     struct r300_winsys_screen *rws = (struct r300_winsys_screen *)ctx->winsys;
     struct r300_transfer *r300transfer = r300_transfer(transfer);
-    struct r300_texture *tex = r300_texture(transfer->resource);
+    struct r300_resource *tex = r300_resource(transfer->resource);
 
-    if (r300transfer->detiled_texture) {
-       rws->buffer_unmap(rws, r300transfer->detiled_texture->buffer);
+    if (r300transfer->linear_texture) {
+        rws->buffer_unmap(rws, r300transfer->linear_texture->buf);
     } else {
-        rws->buffer_unmap(rws, tex->buffer);
+        rws->buffer_unmap(rws, tex->buf);
     }
 }