Merge branch '7.8'
[mesa.git] / src / gallium / drivers / llvmpipe / lp_surface.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "util/u_rect.h"
29 #include "lp_context.h"
30 #include "lp_flush.h"
31 #include "lp_limits.h"
32 #include "lp_surface.h"
33 #include "lp_texture.h"
34
35
36 /**
37 * Adjust x, y, width, height to lie on tile bounds.
38 */
39 static void
40 adjust_to_tile_bounds(unsigned x, unsigned y, unsigned width, unsigned height,
41 unsigned *x_tile, unsigned *y_tile,
42 unsigned *w_tile, unsigned *h_tile)
43 {
44 *x_tile = x & ~(TILE_SIZE - 1);
45 *y_tile = y & ~(TILE_SIZE - 1);
46 *w_tile = ((x + width + TILE_SIZE - 1) & ~(TILE_SIZE - 1)) - *x_tile;
47 *h_tile = ((y + height + TILE_SIZE - 1) & ~(TILE_SIZE - 1)) - *y_tile;
48 }
49
50
51
52 static void
53 lp_surface_copy(struct pipe_context *pipe,
54 struct pipe_surface *dst, unsigned dstx, unsigned dsty,
55 struct pipe_surface *src, unsigned srcx, unsigned srcy,
56 unsigned width, unsigned height)
57 {
58 struct llvmpipe_resource *src_tex = llvmpipe_resource(src->texture);
59 struct llvmpipe_resource *dst_tex = llvmpipe_resource(dst->texture);
60 const enum pipe_format format = src_tex->base.format;
61
62 llvmpipe_flush_resource(pipe,
63 dst->texture, dst->face, dst->level,
64 0, /* flush_flags */
65 FALSE, /* read_only */
66 FALSE, /* cpu_access */
67 FALSE); /* do_not_block */
68
69 llvmpipe_flush_resource(pipe,
70 src->texture, src->face, src->level,
71 0, /* flush_flags */
72 TRUE, /* read_only */
73 FALSE, /* cpu_access */
74 FALSE); /* do_not_block */
75
76 /*
77 printf("surface copy from %u to %u: %u,%u to %u,%u %u x %u\n",
78 src_tex->id, dst_tex->id,
79 srcx, srcy, dstx, dsty, width, height);
80 */
81
82 /* set src tiles to linear layout */
83 {
84 unsigned tx, ty, tw, th;
85 unsigned x, y;
86
87 adjust_to_tile_bounds(srcx, srcy, width, height, &tx, &ty, &tw, &th);
88
89 for (y = 0; y < th; y += TILE_SIZE) {
90 for (x = 0; x < tw; x += TILE_SIZE) {
91 (void) llvmpipe_get_texture_tile_linear(src_tex,
92 src->face, src->level,
93 LP_TEX_USAGE_READ,
94 tx + x, ty + y);
95 }
96 }
97 }
98
99 /* set dst tiles to linear layout */
100 {
101 unsigned tx, ty, tw, th;
102 unsigned x, y;
103 enum lp_texture_usage usage;
104
105 /* XXX for the tiles which are completely contained by the
106 * dest rectangle, we could set the usage mode to WRITE_ALL.
107 * Just test for the case of replacing the whole dest region for now.
108 */
109 if (width == dst_tex->base.width0 && height == dst_tex->base.height0)
110 usage = LP_TEX_USAGE_WRITE_ALL;
111 else
112 usage = LP_TEX_USAGE_READ_WRITE;
113
114 adjust_to_tile_bounds(dstx, dsty, width, height, &tx, &ty, &tw, &th);
115
116 for (y = 0; y < th; y += TILE_SIZE) {
117 for (x = 0; x < tw; x += TILE_SIZE) {
118 (void) llvmpipe_get_texture_tile_linear(dst_tex,
119 dst->face, dst->level,
120 usage,
121 tx + x, ty + y);
122 }
123 }
124 }
125
126 /* copy */
127 {
128 const ubyte *src_linear_ptr
129 = llvmpipe_get_texture_image_address(src_tex, src->face,
130 src->level,
131 LP_TEX_LAYOUT_LINEAR);
132 ubyte *dst_linear_ptr
133 = llvmpipe_get_texture_image_address(dst_tex, dst->face,
134 dst->level,
135 LP_TEX_LAYOUT_LINEAR);
136
137 util_copy_rect(dst_linear_ptr, format,
138 llvmpipe_resource_stride(&dst_tex->base, dst->level),
139 dstx, dsty,
140 width, height,
141 src_linear_ptr,
142 llvmpipe_resource_stride(&src_tex->base, src->level),
143 srcx, srcy);
144 }
145 }
146
147
148 void
149 lp_init_surface_functions(struct llvmpipe_context *lp)
150 {
151 lp->pipe.surface_copy = lp_surface_copy;
152 lp->pipe.surface_fill = util_surface_fill;
153 }