a40fe1be99af1e525015cb66e280744fa5e957dc
[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, unsigned dst_level,
56 unsigned dstx, unsigned dsty, unsigned dstz,
57 struct pipe_resource *src, unsigned src_level,
58 const struct pipe_box *src_box)
59 {
60 struct llvmpipe_resource *src_tex = llvmpipe_resource(src);
61 struct llvmpipe_resource *dst_tex = llvmpipe_resource(dst);
62 const enum pipe_format format = src_tex->base.format;
63 unsigned width = src_box->width;
64 unsigned height = src_box->height;
65 unsigned depth = src_box->depth;
66 unsigned z;
67
68 llvmpipe_flush_resource(pipe,
69 dst, dst_level,
70 FALSE, /* read_only */
71 TRUE, /* cpu_access */
72 FALSE, /* do_not_block */
73 "blit dest");
74
75 llvmpipe_flush_resource(pipe,
76 src, src_level,
77 TRUE, /* read_only */
78 TRUE, /* cpu_access */
79 FALSE, /* do_not_block */
80 "blit src");
81
82 /* Fallback for buffers. */
83 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
84 util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,
85 src, src_level, src_box);
86 return;
87 }
88
89 /*
90 printf("surface copy from %u lvl %u to %u lvl %u: %u,%u,%u to %u,%u,%u %u x %u x %u\n",
91 src_tex->id, src_level, dst_tex->id, dst_level,
92 src_box->x, src_box->y, src_box->z, dstx, dsty, dstz,
93 src_box->width, src_box->height, src_box->depth);
94 */
95
96 for (z = 0; z < src_box->depth; z++){
97
98 /* set src tiles to linear layout */
99 {
100 unsigned tx, ty, tw, th;
101 unsigned x, y;
102
103 adjust_to_tile_bounds(src_box->x, src_box->y, width, height,
104 &tx, &ty, &tw, &th);
105
106 for (y = 0; y < th; y += TILE_SIZE) {
107 for (x = 0; x < tw; x += TILE_SIZE) {
108 (void) llvmpipe_get_texture_tile_linear(src_tex,
109 src_box->z + z, src_level,
110 LP_TEX_USAGE_READ,
111 tx + x, ty + y);
112 }
113 }
114 }
115
116 /* set dst tiles to linear layout */
117 {
118 unsigned tx, ty, tw, th;
119 unsigned x, y;
120 enum lp_texture_usage usage;
121
122 adjust_to_tile_bounds(dstx, dsty, width, height, &tx, &ty, &tw, &th);
123
124 for (y = 0; y < th; y += TILE_SIZE) {
125 boolean contained_y = ty + y >= dsty &&
126 ty + y + TILE_SIZE <= dsty + height ?
127 TRUE : FALSE;
128
129 for (x = 0; x < tw; x += TILE_SIZE) {
130 boolean contained_x = tx + x >= dstx &&
131 tx + x + TILE_SIZE <= dstx + width ?
132 TRUE : FALSE;
133
134 /*
135 * Set the usage mode to WRITE_ALL for the tiles which are
136 * completely contained by the dest rectangle.
137 */
138 if (contained_y && contained_x)
139 usage = LP_TEX_USAGE_WRITE_ALL;
140 else
141 usage = LP_TEX_USAGE_READ_WRITE;
142
143 (void) llvmpipe_get_texture_tile_linear(dst_tex,
144 dstz + z, dst_level,
145 usage,
146 tx + x, ty + y);
147 }
148 }
149 }
150 }
151
152 /* copy */
153 {
154 const ubyte *src_linear_ptr
155 = llvmpipe_get_texture_image_address(src_tex, src_box->z,
156 src_level);
157 ubyte *dst_linear_ptr
158 = llvmpipe_get_texture_image_address(dst_tex, dstz,
159 dst_level);
160
161 if (dst_linear_ptr && src_linear_ptr) {
162 util_copy_box(dst_linear_ptr, format,
163 llvmpipe_resource_stride(&dst_tex->base, dst_level),
164 dst_tex->img_stride[dst_level],
165 dstx, dsty, 0,
166 width, height, depth,
167 src_linear_ptr,
168 llvmpipe_resource_stride(&src_tex->base, src_level),
169 src_tex->img_stride[src_level],
170 src_box->x, src_box->y, 0);
171 }
172 }
173 }
174
175
176 static void lp_blit(struct pipe_context *pipe,
177 const struct pipe_blit_info *blit_info)
178 {
179 struct llvmpipe_context *lp = llvmpipe_context(pipe);
180 struct pipe_blit_info info = *blit_info;
181
182 if (info.src.resource->nr_samples > 1 &&
183 info.dst.resource->nr_samples <= 1 &&
184 !util_format_is_depth_or_stencil(info.src.resource->format) &&
185 !util_format_is_pure_integer(info.src.resource->format)) {
186 debug_printf("llvmpipe: color resolve unimplemented\n");
187 return;
188 }
189
190 if (util_try_blit_via_copy_region(pipe, &info)) {
191 return; /* done */
192 }
193
194 if (info.mask & PIPE_MASK_S) {
195 debug_printf("llvmpipe: cannot blit stencil, skipping\n");
196 info.mask &= ~PIPE_MASK_S;
197 }
198
199 if (!util_blitter_is_blit_supported(lp->blitter, &info)) {
200 debug_printf("llvmpipe: blit unsupported %s -> %s\n",
201 util_format_short_name(info.src.resource->format),
202 util_format_short_name(info.dst.resource->format));
203 return;
204 }
205
206 /* XXX turn off occlusion and streamout queries */
207
208 util_blitter_save_vertex_buffer_slot(lp->blitter, lp->vertex_buffer);
209 util_blitter_save_vertex_elements(lp->blitter, (void*)lp->velems);
210 util_blitter_save_vertex_shader(lp->blitter, (void*)lp->vs);
211 util_blitter_save_geometry_shader(lp->blitter, (void*)lp->gs);
212 util_blitter_save_so_targets(lp->blitter, lp->num_so_targets,
213 (struct pipe_stream_output_target**)lp->so_targets);
214 util_blitter_save_rasterizer(lp->blitter, (void*)lp->rasterizer);
215 util_blitter_save_viewport(lp->blitter, &lp->viewports[0]);
216 util_blitter_save_scissor(lp->blitter, &lp->scissors[0]);
217 util_blitter_save_fragment_shader(lp->blitter, lp->fs);
218 util_blitter_save_blend(lp->blitter, (void*)lp->blend);
219 util_blitter_save_depth_stencil_alpha(lp->blitter, (void*)lp->depth_stencil);
220 util_blitter_save_stencil_ref(lp->blitter, &lp->stencil_ref);
221 /*util_blitter_save_sample_mask(sp->blitter, lp->sample_mask);*/
222 util_blitter_save_framebuffer(lp->blitter, &lp->framebuffer);
223 util_blitter_save_fragment_sampler_states(lp->blitter,
224 lp->num_samplers[PIPE_SHADER_FRAGMENT],
225 (void**)lp->samplers[PIPE_SHADER_FRAGMENT]);
226 util_blitter_save_fragment_sampler_views(lp->blitter,
227 lp->num_sampler_views[PIPE_SHADER_FRAGMENT],
228 lp->sampler_views[PIPE_SHADER_FRAGMENT]);
229 util_blitter_save_render_condition(lp->blitter, lp->render_cond_query,
230 lp->render_cond_mode);
231 util_blitter_blit(lp->blitter, &info);
232 }
233
234
235 static struct pipe_surface *
236 llvmpipe_create_surface(struct pipe_context *pipe,
237 struct pipe_resource *pt,
238 const struct pipe_surface *surf_tmpl)
239 {
240 struct pipe_surface *ps;
241
242 if (!(pt->bind & (PIPE_BIND_DEPTH_STENCIL | PIPE_BIND_RENDER_TARGET)))
243 debug_printf("Illegal surface creation without bind flag\n");
244
245 ps = CALLOC_STRUCT(pipe_surface);
246 if (ps) {
247 pipe_reference_init(&ps->reference, 1);
248 pipe_resource_reference(&ps->texture, pt);
249 ps->context = pipe;
250 ps->format = surf_tmpl->format;
251 if (llvmpipe_resource_is_texture(pt)) {
252 assert(surf_tmpl->u.tex.level <= pt->last_level);
253 assert(surf_tmpl->u.tex.first_layer <= surf_tmpl->u.tex.last_layer);
254 ps->width = u_minify(pt->width0, surf_tmpl->u.tex.level);
255 ps->height = u_minify(pt->height0, surf_tmpl->u.tex.level);
256 ps->u.tex.level = surf_tmpl->u.tex.level;
257 ps->u.tex.first_layer = surf_tmpl->u.tex.first_layer;
258 ps->u.tex.last_layer = surf_tmpl->u.tex.last_layer;
259 }
260 else {
261 /* setting width as number of elements should get us correct renderbuffer width */
262 ps->width = surf_tmpl->u.buf.last_element - surf_tmpl->u.buf.first_element + 1;
263 ps->height = pt->height0;
264 ps->u.buf.first_element = surf_tmpl->u.buf.first_element;
265 ps->u.buf.last_element = surf_tmpl->u.buf.last_element;
266 assert(ps->u.buf.first_element <= ps->u.buf.last_element);
267 assert(util_format_get_blocksize(surf_tmpl->format) *
268 (ps->u.buf.last_element + 1) <= pt->width0);
269 }
270 }
271 return ps;
272 }
273
274
275 static void
276 llvmpipe_surface_destroy(struct pipe_context *pipe,
277 struct pipe_surface *surf)
278 {
279 /* Effectively do the texture_update work here - if texture images
280 * needed post-processing to put them into hardware layout, this is
281 * where it would happen. For llvmpipe, nothing to do.
282 */
283 assert(surf->texture);
284 pipe_resource_reference(&surf->texture, NULL);
285 FREE(surf);
286 }
287
288
289 void
290 llvmpipe_init_surface_functions(struct llvmpipe_context *lp)
291 {
292 lp->pipe.clear_render_target = util_clear_render_target;
293 lp->pipe.clear_depth_stencil = util_clear_depth_stencil;
294 lp->pipe.create_surface = llvmpipe_create_surface;
295 lp->pipe.surface_destroy = llvmpipe_surface_destroy;
296 /* These two are not actually functions dealing with surfaces */
297 lp->pipe.resource_copy_region = lp_resource_copy;
298 lp->pipe.blit = lp_blit;
299 }