1946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca/**************************************************************************
2946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca *
3946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * All Rights Reserved.
5946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca *
6946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * Permission is hereby granted, free of charge, to any person obtaining a
7946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * copy of this software and associated documentation files (the
8946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * "Software"), to deal in the Software without restriction, including
9946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * without limitation the rights to use, copy, modify, merge, publish,
10946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * distribute, sub license, and/or sell copies of the Software, and to
11946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * permit persons to whom the Software is furnished to do so, subject to
12946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * the following conditions:
13946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca *
14946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * The above copyright notice and this permission notice (including the
15946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * next paragraph) shall be included in all copies or substantial portions
16946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * of the Software.
17946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca *
18946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca *
26946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca **************************************************************************/
27946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca
28946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca#include "util/u_rect.h"
298b0c217f2bc123bffd25cc4977d6abb1b3fa8186Brian Paul#include "util/u_surface.h"
30946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca#include "lp_context.h"
31bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca#include "lp_flush.h"
322de31f2bf2b9c68aaa76a63fa0d0d3e0731ccfb5Brian Paul#include "lp_limits.h"
33946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca#include "lp_surface.h"
340639765b2850739af1678f10fc0c5706d5827776Brian Paul#include "lp_texture.h"
350639765b2850739af1678f10fc0c5706d5827776Brian Paul
360639765b2850739af1678f10fc0c5706d5827776Brian Paul
370639765b2850739af1678f10fc0c5706d5827776Brian Paul/**
380639765b2850739af1678f10fc0c5706d5827776Brian Paul * Adjust x, y, width, height to lie on tile bounds.
390639765b2850739af1678f10fc0c5706d5827776Brian Paul */
400639765b2850739af1678f10fc0c5706d5827776Brian Paulstatic void
410639765b2850739af1678f10fc0c5706d5827776Brian Pauladjust_to_tile_bounds(unsigned x, unsigned y, unsigned width, unsigned height,
420639765b2850739af1678f10fc0c5706d5827776Brian Paul                      unsigned *x_tile, unsigned *y_tile,
430639765b2850739af1678f10fc0c5706d5827776Brian Paul                      unsigned *w_tile, unsigned *h_tile)
440639765b2850739af1678f10fc0c5706d5827776Brian Paul{
450639765b2850739af1678f10fc0c5706d5827776Brian Paul   *x_tile = x & ~(TILE_SIZE - 1);
460639765b2850739af1678f10fc0c5706d5827776Brian Paul   *y_tile = y & ~(TILE_SIZE - 1);
470639765b2850739af1678f10fc0c5706d5827776Brian Paul   *w_tile = ((x + width + TILE_SIZE - 1) & ~(TILE_SIZE - 1)) - *x_tile;
480639765b2850739af1678f10fc0c5706d5827776Brian Paul   *h_tile = ((y + height + TILE_SIZE - 1) & ~(TILE_SIZE - 1)) - *y_tile;
490639765b2850739af1678f10fc0c5706d5827776Brian Paul}
500639765b2850739af1678f10fc0c5706d5827776Brian Paul
51946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca
52946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca
53946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonsecastatic void
54e121a73d629856240a9676a4f0dff832d95c90c3Roland Scheideggerlp_resource_copy(struct pipe_context *pipe,
554c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger                 struct pipe_resource *dst, unsigned dst_level,
56e121a73d629856240a9676a4f0dff832d95c90c3Roland Scheidegger                 unsigned dstx, unsigned dsty, unsigned dstz,
574c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger                 struct pipe_resource *src, unsigned src_level,
584c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger                 const struct pipe_box *src_box)
59946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca{
604c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger   /* XXX this used to ignore srcz/dstz
614c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger    * assume it works the same for cube and 3d
624c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger    */
63e121a73d629856240a9676a4f0dff832d95c90c3Roland Scheidegger   struct llvmpipe_resource *src_tex = llvmpipe_resource(src);
64e121a73d629856240a9676a4f0dff832d95c90c3Roland Scheidegger   struct llvmpipe_resource *dst_tex = llvmpipe_resource(dst);
651a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul   const enum pipe_format format = src_tex->base.format;
664c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger   unsigned width = src_box->width;
674c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger   unsigned height = src_box->height;
684c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger   assert(src_box->depth == 1);
691a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul
70883d8a0b449b53d83cc5970d2ce50df536aef55fMarek Olšák   /* Fallback for buffers. */
71883d8a0b449b53d83cc5970d2ce50df536aef55fMarek Olšák   if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
72883d8a0b449b53d83cc5970d2ce50df536aef55fMarek Olšák      util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,
73883d8a0b449b53d83cc5970d2ce50df536aef55fMarek Olšák                                src, src_level, src_box);
74883d8a0b449b53d83cc5970d2ce50df536aef55fMarek Olšák      return;
75883d8a0b449b53d83cc5970d2ce50df536aef55fMarek Olšák   }
76883d8a0b449b53d83cc5970d2ce50df536aef55fMarek Olšák
777951630d0877fc3c293151ad6476bec7288e63c6José Fonseca   llvmpipe_flush_resource(pipe,
784c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger                           dst, dst_level, dstz,
7943b85af56efbe6eb06f4e62d23e9f6f583c5ec2eJosé Fonseca                           FALSE, /* read_only */
80a9e0fda070f3d8cb656e6891b1be56aaa8ed705aJosé Fonseca                           TRUE, /* cpu_access */
819f6e8e1d6b8696a3ee96cba01b2466ba7a1a8ef6Keith Whitwell                           FALSE, /* do_not_block */
829f6e8e1d6b8696a3ee96cba01b2466ba7a1a8ef6Keith Whitwell                           "blit dest");
83bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca
847951630d0877fc3c293151ad6476bec7288e63c6José Fonseca   llvmpipe_flush_resource(pipe,
854c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger                           src, src_level, src_box->z,
8643b85af56efbe6eb06f4e62d23e9f6f583c5ec2eJosé Fonseca                           TRUE, /* read_only */
87a9e0fda070f3d8cb656e6891b1be56aaa8ed705aJosé Fonseca                           TRUE, /* cpu_access */
889f6e8e1d6b8696a3ee96cba01b2466ba7a1a8ef6Keith Whitwell                           FALSE, /* do_not_block */
899f6e8e1d6b8696a3ee96cba01b2466ba7a1a8ef6Keith Whitwell                           "blit src");
90bf40c346637325862d6d9cdbc9838c5726abc0c0José Fonseca
911a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul   /*
924c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger   printf("surface copy from %u lvl %u to %u lvl %u: %u,%u,%u to %u,%u,%u %u x %u x %u\n",
934c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger          src_tex->id, src_level, dst_tex->id, dst_level,
944c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger          src_box->x, src_box->y, src_box->z, dstx, dsty, dstz,
954c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger          src_box->width, src_box->height, src_box->depth);
961a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul   */
971a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul
981a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul   /* set src tiles to linear layout */
990639765b2850739af1678f10fc0c5706d5827776Brian Paul   {
1001a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul      unsigned tx, ty, tw, th;
1011a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul      unsigned x, y;
1021a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul
1034c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger      adjust_to_tile_bounds(src_box->x, src_box->y, width, height,
1044c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger                            &tx, &ty, &tw, &th);
1051a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul
1061a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul      for (y = 0; y < th; y += TILE_SIZE) {
1071a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul         for (x = 0; x < tw; x += TILE_SIZE) {
1081a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul            (void) llvmpipe_get_texture_tile_linear(src_tex,
1094c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger                                                    src_box->z, src_level,
110e121a73d629856240a9676a4f0dff832d95c90c3Roland Scheidegger                                                    LP_TEX_USAGE_READ,
1111a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul                                                    tx + x, ty + y);
1120639765b2850739af1678f10fc0c5706d5827776Brian Paul         }
1130639765b2850739af1678f10fc0c5706d5827776Brian Paul      }
1141a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul   }
1150639765b2850739af1678f10fc0c5706d5827776Brian Paul
1161a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul   /* set dst tiles to linear layout */
1171a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul   {
1181a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul      unsigned tx, ty, tw, th;
1191a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul      unsigned x, y;
1201a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul      enum lp_texture_usage usage;
1211a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul
1221a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul      adjust_to_tile_bounds(dstx, dsty, width, height, &tx, &ty, &tw, &th);
1231a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul
1241a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul      for (y = 0; y < th; y += TILE_SIZE) {
125250b92f3bb4fc4a53f3150b0e8ff1e121a5adbc7José Fonseca         boolean contained_y = ty + y >= dsty &&
126250b92f3bb4fc4a53f3150b0e8ff1e121a5adbc7José Fonseca                               ty + y + TILE_SIZE <= dsty + height ?
127250b92f3bb4fc4a53f3150b0e8ff1e121a5adbc7José Fonseca                               TRUE : FALSE;
128250b92f3bb4fc4a53f3150b0e8ff1e121a5adbc7José Fonseca
1291a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul         for (x = 0; x < tw; x += TILE_SIZE) {
130250b92f3bb4fc4a53f3150b0e8ff1e121a5adbc7José Fonseca            boolean contained_x = tx + x >= dstx &&
131250b92f3bb4fc4a53f3150b0e8ff1e121a5adbc7José Fonseca                                  tx + x + TILE_SIZE <= dstx + width ?
132250b92f3bb4fc4a53f3150b0e8ff1e121a5adbc7José Fonseca                                  TRUE : FALSE;
133250b92f3bb4fc4a53f3150b0e8ff1e121a5adbc7José Fonseca
134250b92f3bb4fc4a53f3150b0e8ff1e121a5adbc7José Fonseca            /*
135250b92f3bb4fc4a53f3150b0e8ff1e121a5adbc7José Fonseca             * Set the usage mode to WRITE_ALL for the tiles which are
136250b92f3bb4fc4a53f3150b0e8ff1e121a5adbc7José Fonseca             * completely contained by the dest rectangle.
137250b92f3bb4fc4a53f3150b0e8ff1e121a5adbc7José Fonseca             */
138250b92f3bb4fc4a53f3150b0e8ff1e121a5adbc7José Fonseca            if (contained_y && contained_x)
139250b92f3bb4fc4a53f3150b0e8ff1e121a5adbc7José Fonseca               usage = LP_TEX_USAGE_WRITE_ALL;
140250b92f3bb4fc4a53f3150b0e8ff1e121a5adbc7José Fonseca            else
141250b92f3bb4fc4a53f3150b0e8ff1e121a5adbc7José Fonseca               usage = LP_TEX_USAGE_READ_WRITE;
142250b92f3bb4fc4a53f3150b0e8ff1e121a5adbc7José Fonseca
1431a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul            (void) llvmpipe_get_texture_tile_linear(dst_tex,
1444c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger                                                    dstz, dst_level,
1451a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul                                                    usage,
1461a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul                                                    tx + x, ty + y);
1470639765b2850739af1678f10fc0c5706d5827776Brian Paul         }
1480639765b2850739af1678f10fc0c5706d5827776Brian Paul      }
1490639765b2850739af1678f10fc0c5706d5827776Brian Paul   }
1500639765b2850739af1678f10fc0c5706d5827776Brian Paul
1511a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul   /* copy */
1521a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul   {
1531a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul      const ubyte *src_linear_ptr
1544c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger         = llvmpipe_get_texture_image_address(src_tex, src_box->z,
1554c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger                                              src_level,
1561a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul                                              LP_TEX_LAYOUT_LINEAR);
1571a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul      ubyte *dst_linear_ptr
1584c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger         = llvmpipe_get_texture_image_address(dst_tex, dstz,
1594c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger                                              dst_level,
1601a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul                                              LP_TEX_LAYOUT_LINEAR);
1611a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul
1623d6101245b2726721a26931e0491c61286ca29c6Brian Paul      if (dst_linear_ptr && src_linear_ptr) {
1633d6101245b2726721a26931e0491c61286ca29c6Brian Paul         util_copy_rect(dst_linear_ptr, format,
1644c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger                        llvmpipe_resource_stride(&dst_tex->base, dst_level),
1653d6101245b2726721a26931e0491c61286ca29c6Brian Paul                        dstx, dsty,
1663d6101245b2726721a26931e0491c61286ca29c6Brian Paul                        width, height,
1673d6101245b2726721a26931e0491c61286ca29c6Brian Paul                        src_linear_ptr,
1684c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger                        llvmpipe_resource_stride(&src_tex->base, src_level),
1694c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger                        src_box->x, src_box->y);
1703d6101245b2726721a26931e0491c61286ca29c6Brian Paul      }
1711a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul   }
172946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca}
173946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca
1741a44ec5e36c75e7cb75e3d777977658c39b4c182Brian Paul
175946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonsecavoid
1763a749b5c094cca558b5d03efbf2ea6ee6d62c4b5Brian Paulllvmpipe_init_surface_functions(struct llvmpipe_context *lp)
177946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca{
178e121a73d629856240a9676a4f0dff832d95c90c3Roland Scheidegger   lp->pipe.resource_copy_region = lp_resource_copy;
179f92879576e9b899bb6ac84f1009322c1c7e5ed51Roland Scheidegger   lp->pipe.clear_render_target = util_clear_render_target;
180f92879576e9b899bb6ac84f1009322c1c7e5ed51Roland Scheidegger   lp->pipe.clear_depth_stencil = util_clear_depth_stencil;
181946f432a08112148d743eb9faf6b27bb8cc7fa76José Fonseca}
182