llvmpipe: fence debugging, add llvmpipe_finish
[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 "util/u_surface.h"
30 #include "lp_context.h"
31 #include "lp_flush.h"
32 #include "lp_limits.h"
33 #include "lp_surface.h"
34 #include "lp_texture.h"
35
36
37 /**
38 * Adjust x, y, width, height to lie on tile bounds.
39 */
40 static void
41 adjust_to_tile_bounds(unsigned x, unsigned y, unsigned width, unsigned height,
42 unsigned *x_tile, unsigned *y_tile,
43 unsigned *w_tile, unsigned *h_tile)
44 {
45 *x_tile = x & ~(TILE_SIZE - 1);
46 *y_tile = y & ~(TILE_SIZE - 1);
47 *w_tile = ((x + width + TILE_SIZE - 1) & ~(TILE_SIZE - 1)) - *x_tile;
48 *h_tile = ((y + height + TILE_SIZE - 1) & ~(TILE_SIZE - 1)) - *y_tile;
49 }
50
51
52
53 static void
54 lp_resource_copy(struct pipe_context *pipe,
55 struct pipe_resource *dst, struct pipe_subresource subdst,
56 unsigned dstx, unsigned dsty, unsigned dstz,
57 struct pipe_resource *src, struct pipe_subresource subsrc,
58 unsigned srcx, unsigned srcy, unsigned srcz,
59 unsigned width, unsigned height)
60 {
61 /* XXX what about the dstz/srcz parameters - zslice wasn't used... */
62 struct llvmpipe_resource *src_tex = llvmpipe_resource(src);
63 struct llvmpipe_resource *dst_tex = llvmpipe_resource(dst);
64 const enum pipe_format format = src_tex->base.format;
65
66 llvmpipe_flush_resource(pipe,
67 dst, subdst.face, subdst.level,
68 0, /* flush_flags */
69 FALSE, /* read_only */
70 TRUE, /* cpu_access */
71 FALSE,
72 "blit dst"); /* do_not_block */
73
74 llvmpipe_flush_resource(pipe,
75 src, subsrc.face, subsrc.level,
76 0, /* flush_flags */
77 TRUE, /* read_only */
78 TRUE, /* cpu_access */
79 FALSE,
80 "blit src"); /* do_not_block */
81
82 /*
83 printf("surface copy from %u to %u: %u,%u to %u,%u %u x %u\n",
84 src_tex->id, dst_tex->id,
85 srcx, srcy, dstx, dsty, width, height);
86 */
87
88 /* set src tiles to linear layout */
89 {
90 unsigned tx, ty, tw, th;
91 unsigned x, y;
92
93 adjust_to_tile_bounds(srcx, srcy, width, height, &tx, &ty, &tw, &th);
94
95 for (y = 0; y < th; y += TILE_SIZE) {
96 for (x = 0; x < tw; x += TILE_SIZE) {
97 (void) llvmpipe_get_texture_tile_linear(src_tex,
98 subsrc.face, subsrc.level,
99 LP_TEX_USAGE_READ,
100 tx + x, ty + y);
101 }
102 }
103 }
104
105 /* set dst tiles to linear layout */
106 {
107 unsigned tx, ty, tw, th;
108 unsigned x, y;
109 enum lp_texture_usage usage;
110
111 adjust_to_tile_bounds(dstx, dsty, width, height, &tx, &ty, &tw, &th);
112
113 for (y = 0; y < th; y += TILE_SIZE) {
114 boolean contained_y = ty + y >= dsty &&
115 ty + y + TILE_SIZE <= dsty + height ?
116 TRUE : FALSE;
117
118 for (x = 0; x < tw; x += TILE_SIZE) {
119 boolean contained_x = tx + x >= dstx &&
120 tx + x + TILE_SIZE <= dstx + width ?
121 TRUE : FALSE;
122
123 /*
124 * Set the usage mode to WRITE_ALL for the tiles which are
125 * completely contained by the dest rectangle.
126 */
127 if (contained_y && contained_x)
128 usage = LP_TEX_USAGE_WRITE_ALL;
129 else
130 usage = LP_TEX_USAGE_READ_WRITE;
131
132 (void) llvmpipe_get_texture_tile_linear(dst_tex,
133 subdst.face, subdst.level,
134 usage,
135 tx + x, ty + y);
136 }
137 }
138 }
139
140 /* copy */
141 {
142 const ubyte *src_linear_ptr
143 = llvmpipe_get_texture_image_address(src_tex, subsrc.face,
144 subsrc.level,
145 LP_TEX_LAYOUT_LINEAR);
146 ubyte *dst_linear_ptr
147 = llvmpipe_get_texture_image_address(dst_tex, subdst.face,
148 subdst.level,
149 LP_TEX_LAYOUT_LINEAR);
150
151 if (dst_linear_ptr && src_linear_ptr) {
152 util_copy_rect(dst_linear_ptr, format,
153 llvmpipe_resource_stride(&dst_tex->base, subdst.level),
154 dstx, dsty,
155 width, height,
156 src_linear_ptr,
157 llvmpipe_resource_stride(&src_tex->base, subsrc.level),
158 srcx, srcy);
159 }
160 }
161 }
162
163
164 void
165 llvmpipe_init_surface_functions(struct llvmpipe_context *lp)
166 {
167 lp->pipe.resource_copy_region = lp_resource_copy;
168 lp->pipe.clear_render_target = util_clear_render_target;
169 lp->pipe.clear_depth_stencil = util_clear_depth_stencil;
170 }