svga: use copy_region_vgpu10() for region copies when possible
[mesa.git] / src / gallium / drivers / svga / svga_pipe_blit.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "svga_resource_texture.h"
27 #include "svga_context.h"
28 #include "svga_debug.h"
29 #include "svga_cmd.h"
30 #include "svga_surface.h"
31
32 //#include "util/u_blit_sw.h"
33 #include "util/u_format.h"
34 #include "util/u_surface.h"
35
36 #define FILE_DEBUG_FLAG DEBUG_BLIT
37
38
39 /**
40 * Copy an image between textures with the vgpu10 CopyRegion command.
41 */
42 static void
43 copy_region_vgpu10(struct svga_context *svga, struct pipe_resource *src_tex,
44 unsigned src_x, unsigned src_y, unsigned src_z,
45 unsigned src_level, unsigned src_face,
46 struct pipe_resource *dst_tex,
47 unsigned dst_x, unsigned dst_y, unsigned dst_z,
48 unsigned dst_level, unsigned dst_face,
49 unsigned width, unsigned height, unsigned depth)
50 {
51 enum pipe_error ret;
52 uint32 srcSubResource, dstSubResource;
53 struct svga_texture *dtex, *stex;
54 SVGA3dCopyBox box;
55 int i, num_layers = 1;
56
57 stex = svga_texture(src_tex);
58 dtex = svga_texture(dst_tex);
59
60 box.x = dst_x;
61 box.y = dst_y;
62 box.z = dst_z;
63 box.w = width;
64 box.h = height;
65 box.d = depth;
66 box.srcx = src_x;
67 box.srcy = src_y;
68 box.srcz = src_z;
69
70 if (src_tex->target == PIPE_TEXTURE_1D_ARRAY ||
71 src_tex->target == PIPE_TEXTURE_2D_ARRAY) {
72 /* copy layer by layer */
73 box.z = 0;
74 box.d = 1;
75 box.srcz = 0;
76
77 num_layers = depth;
78 src_face = src_z;
79 dst_face = dst_z;
80 }
81
82 /* loop over array layers */
83 for (i = 0; i < num_layers; i++) {
84 srcSubResource = (src_face + i) * (src_tex->last_level + 1) + src_level;
85 dstSubResource = (dst_face + i) * (dst_tex->last_level + 1) + dst_level;
86
87 ret = SVGA3D_vgpu10_PredCopyRegion(svga->swc,
88 dtex->handle, dstSubResource,
89 stex->handle, srcSubResource, &box);
90 if (ret != PIPE_OK) {
91 svga_context_flush(svga, NULL);
92 ret = SVGA3D_vgpu10_PredCopyRegion(svga->swc,
93 dtex->handle, dstSubResource,
94 stex->handle, srcSubResource, &box);
95 assert(ret == PIPE_OK);
96 }
97
98 svga_define_texture_level(dtex, dst_face + i, dst_level);
99 }
100 }
101
102
103 static void
104 svga_resource_copy_region(struct pipe_context *pipe,
105 struct pipe_resource *dst_tex,
106 unsigned dst_level,
107 unsigned dstx, unsigned dsty, unsigned dstz,
108 struct pipe_resource *src_tex,
109 unsigned src_level,
110 const struct pipe_box *src_box)
111 {
112 struct svga_context *svga = svga_context(pipe);
113 struct svga_texture *stex, *dtex;
114 unsigned dst_face_layer, dst_z, src_face_layer, src_z;
115
116 /* Emit buffered drawing commands, and any back copies.
117 */
118 svga_surfaces_flush( svga );
119
120 /* Fallback for buffers. */
121 if (dst_tex->target == PIPE_BUFFER && src_tex->target == PIPE_BUFFER) {
122 util_resource_copy_region(pipe, dst_tex, dst_level, dstx, dsty, dstz,
123 src_tex, src_level, src_box);
124 return;
125 }
126
127 stex = svga_texture(src_tex);
128 dtex = svga_texture(dst_tex);
129
130 if (src_tex->target == PIPE_TEXTURE_CUBE ||
131 src_tex->target == PIPE_TEXTURE_1D_ARRAY) {
132 src_face_layer = src_box->z;
133 src_z = 0;
134 assert(src_box->depth == 1);
135 }
136 else {
137 src_face_layer = 0;
138 src_z = src_box->z;
139 }
140
141 if (dst_tex->target == PIPE_TEXTURE_CUBE ||
142 dst_tex->target == PIPE_TEXTURE_1D_ARRAY) {
143 dst_face_layer = dstz;
144 dst_z = 0;
145 assert(src_box->depth == 1);
146 }
147 else {
148 dst_face_layer = 0;
149 dst_z = dstz;
150 }
151
152 stex = svga_texture(src_tex);
153 dtex = svga_texture(dst_tex);
154
155 if (svga_have_vgpu10(svga)) {
156 /* vgpu10 */
157 if (util_format_is_compressed(src_tex->format) ==
158 util_format_is_compressed(dst_tex->format) &&
159 !util_format_is_depth_and_stencil(src_tex->format) &&
160 stex->handle != dtex->handle &&
161 src_tex->target == dst_tex->target) {
162 copy_region_vgpu10(svga,
163 src_tex,
164 src_box->x, src_box->y, src_z,
165 src_level, src_face_layer,
166 dst_tex,
167 dstx, dsty, dst_z,
168 dst_level, dst_face_layer,
169 src_box->width, src_box->height, src_box->depth);
170 }
171 else {
172 util_resource_copy_region(pipe, dst_tex, dst_level, dstx, dsty, dstz,
173 src_tex, src_level, src_box);
174 }
175 }
176 else {
177 /* vgpu9 */
178 if (src_tex->format == dst_tex->format) {
179 svga_texture_copy_handle(svga,
180 stex->handle,
181 src_box->x, src_box->y, src_z,
182 src_level, src_face_layer,
183 dtex->handle,
184 dstx, dsty, dst_z,
185 dst_level, dst_face_layer,
186 src_box->width, src_box->height,
187 src_box->depth);
188 }
189 else {
190 util_resource_copy_region(pipe, dst_tex, dst_level, dstx, dsty, dstz,
191 src_tex, src_level, src_box);
192 }
193 }
194
195 /* Mark the destination image as being defined */
196 svga_define_texture_level(dtex, dst_face_layer, dst_level);
197 }
198
199
200 /**
201 * The state tracker implements some resource copies with blits (for
202 * GL_ARB_copy_image). This function checks if we should really do the blit
203 * with a VGPU10 CopyRegion command or software fallback (for incompatible
204 * src/dst formats).
205 */
206 static bool
207 can_blit_via_copy_region_vgpu10(struct svga_context *svga,
208 const struct pipe_blit_info *blit_info)
209 {
210 struct svga_texture *dtex, *stex;
211
212 if (!svga_have_vgpu10(svga))
213 return false;
214
215 stex = svga_texture(blit_info->src.resource);
216 dtex = svga_texture(blit_info->src.resource);
217
218 // can't copy within one resource
219 if (stex->handle == dtex->handle)
220 return false;
221
222 // can't copy between different resource types
223 if (blit_info->src.resource->target != blit_info->dst.resource->target)
224 return false;
225
226 // check that the blit src/dst regions are same size, no flipping, etc.
227 if (blit_info->src.box.width != blit_info->dst.box.width ||
228 blit_info->src.box.height != blit_info->dst.box.height)
229 return false;
230
231 // depth/stencil copies not supported at this time
232 if (blit_info->mask != PIPE_MASK_RGBA)
233 return false;
234
235 if (blit_info->alpha_blend || blit_info->render_condition_enable ||
236 blit_info->scissor_enable)
237 return false;
238
239 // check that src/dst surface formats are compatible for the VGPU device.
240 return util_is_format_compatible(
241 util_format_description(blit_info->src.resource->format),
242 util_format_description(blit_info->dst.resource->format));
243 }
244
245
246 static void
247 svga_blit(struct pipe_context *pipe,
248 const struct pipe_blit_info *blit_info)
249 {
250 struct svga_context *svga = svga_context(pipe);
251
252 if (!svga_have_vgpu10(svga) &&
253 blit_info->src.resource->nr_samples > 1 &&
254 blit_info->dst.resource->nr_samples <= 1 &&
255 !util_format_is_depth_or_stencil(blit_info->src.resource->format) &&
256 !util_format_is_pure_integer(blit_info->src.resource->format)) {
257 debug_printf("svga: color resolve unimplemented\n");
258 return;
259 }
260
261 if (can_blit_via_copy_region_vgpu10(svga, blit_info)) {
262 unsigned src_face, src_z, dst_face, dst_z;
263
264 if (blit_info->src.resource->target == PIPE_TEXTURE_CUBE) {
265 src_face = blit_info->src.box.z;
266 src_z = 0;
267 assert(blit_info->src.box.depth == 1);
268 }
269 else {
270 src_face = 0;
271 src_z = blit_info->src.box.z;
272 }
273
274 if (blit_info->dst.resource->target == PIPE_TEXTURE_CUBE) {
275 dst_face = blit_info->dst.box.z;
276 dst_z = 0;
277 assert(blit_info->src.box.depth == 1);
278 }
279 else {
280 dst_face = 0;
281 dst_z = blit_info->dst.box.z;
282 }
283
284 copy_region_vgpu10(svga,
285 blit_info->src.resource,
286 blit_info->src.box.x, blit_info->src.box.y, src_z,
287 blit_info->src.level, src_face,
288 blit_info->dst.resource,
289 blit_info->dst.box.x, blit_info->dst.box.y, dst_z,
290 blit_info->dst.level, dst_face,
291 blit_info->src.box.width, blit_info->src.box.height,
292 blit_info->src.box.depth);
293 return;
294 }
295
296 if (util_try_blit_via_copy_region(pipe, blit_info)) {
297 return; /* done */
298 }
299
300 if ((blit_info->mask & PIPE_MASK_S) ||
301 !util_blitter_is_blit_supported(svga->blitter, blit_info)) {
302 debug_printf("svga: blit unsupported %s -> %s\n",
303 util_format_short_name(blit_info->src.resource->format),
304 util_format_short_name(blit_info->dst.resource->format));
305 return;
306 }
307
308 /* XXX turn off occlusion and streamout queries */
309
310 util_blitter_save_vertex_buffer_slot(svga->blitter, svga->curr.vb);
311 util_blitter_save_vertex_elements(svga->blitter, (void*)svga->curr.velems);
312 util_blitter_save_vertex_shader(svga->blitter, svga->curr.vs);
313 util_blitter_save_geometry_shader(svga->blitter, svga->curr.user_gs);
314 util_blitter_save_so_targets(svga->blitter, svga->num_so_targets,
315 (struct pipe_stream_output_target**)svga->so_targets);
316 util_blitter_save_rasterizer(svga->blitter, (void*)svga->curr.rast);
317 util_blitter_save_viewport(svga->blitter, &svga->curr.viewport);
318 util_blitter_save_scissor(svga->blitter, &svga->curr.scissor);
319 util_blitter_save_fragment_shader(svga->blitter, svga->curr.fs);
320 util_blitter_save_blend(svga->blitter, (void*)svga->curr.blend);
321 util_blitter_save_depth_stencil_alpha(svga->blitter,
322 (void*)svga->curr.depth);
323 util_blitter_save_stencil_ref(svga->blitter, &svga->curr.stencil_ref);
324 util_blitter_save_sample_mask(svga->blitter, svga->curr.sample_mask);
325 util_blitter_save_framebuffer(svga->blitter, &svga->curr.framebuffer);
326 util_blitter_save_fragment_sampler_states(svga->blitter,
327 svga->curr.num_samplers[PIPE_SHADER_FRAGMENT],
328 (void**)svga->curr.sampler[PIPE_SHADER_FRAGMENT]);
329 util_blitter_save_fragment_sampler_views(svga->blitter,
330 svga->curr.num_sampler_views[PIPE_SHADER_FRAGMENT],
331 svga->curr.sampler_views[PIPE_SHADER_FRAGMENT]);
332 /*util_blitter_save_render_condition(svga->blitter, svga->render_cond_query,
333 svga->render_cond_cond, svga->render_cond_mode);*/
334 util_blitter_blit(svga->blitter, blit_info);
335 }
336
337
338 static void
339 svga_flush_resource(struct pipe_context *pipe,
340 struct pipe_resource *resource)
341 {
342 }
343
344
345 void
346 svga_init_blit_functions(struct svga_context *svga)
347 {
348 svga->pipe.resource_copy_region = svga_resource_copy_region;
349 svga->pipe.blit = svga_blit;
350 svga->pipe.flush_resource = svga_flush_resource;
351 }