svga: fix src/dst typo in can_blit_via_copy_region_vgpu10()
[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_context.h"
27 #include "svga_debug.h"
28 #include "svga_cmd.h"
29 #include "svga_resource_buffer.h"
30 #include "svga_resource_texture.h"
31 #include "svga_surface.h"
32
33 //#include "util/u_blit_sw.h"
34 #include "util/u_format.h"
35 #include "util/u_surface.h"
36
37 #define FILE_DEBUG_FLAG DEBUG_BLIT
38
39
40 /**
41 * Copy an image between textures with the vgpu10 CopyRegion command.
42 */
43 static void
44 copy_region_vgpu10(struct svga_context *svga, struct pipe_resource *src_tex,
45 unsigned src_x, unsigned src_y, unsigned src_z,
46 unsigned src_level, unsigned src_face,
47 struct pipe_resource *dst_tex,
48 unsigned dst_x, unsigned dst_y, unsigned dst_z,
49 unsigned dst_level, unsigned dst_face,
50 unsigned width, unsigned height, unsigned depth)
51 {
52 enum pipe_error ret;
53 uint32 srcSubResource, dstSubResource;
54 struct svga_texture *dtex, *stex;
55 SVGA3dCopyBox box;
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 srcSubResource = src_face * (src_tex->last_level + 1) + src_level;
71 dstSubResource = dst_face * (dst_tex->last_level + 1) + dst_level;
72
73 ret = SVGA3D_vgpu10_PredCopyRegion(svga->swc,
74 dtex->handle, dstSubResource,
75 stex->handle, srcSubResource, &box);
76 if (ret != PIPE_OK) {
77 svga_context_flush(svga, NULL);
78 ret = SVGA3D_vgpu10_PredCopyRegion(svga->swc,
79 dtex->handle, dstSubResource,
80 stex->handle, srcSubResource, &box);
81 assert(ret == PIPE_OK);
82 }
83
84 svga_define_texture_level(dtex, dst_face, dst_level);
85 }
86
87
88 static void
89 svga_resource_copy_region(struct pipe_context *pipe,
90 struct pipe_resource *dst_tex,
91 unsigned dst_level,
92 unsigned dstx, unsigned dsty, unsigned dstz,
93 struct pipe_resource *src_tex,
94 unsigned src_level,
95 const struct pipe_box *src_box)
96 {
97 struct svga_context *svga = svga_context(pipe);
98 struct svga_texture *stex, *dtex;
99 unsigned dst_face_layer, dst_z, src_face_layer, src_z;
100
101 /* Emit buffered drawing commands, and any back copies.
102 */
103 svga_surfaces_flush( svga );
104
105 if (dst_tex->target == PIPE_BUFFER && src_tex->target == PIPE_BUFFER) {
106 /* can't copy within the same buffer, unfortunately */
107 if (svga_have_vgpu10(svga) && src_tex != dst_tex) {
108 enum pipe_error ret;
109 struct svga_winsys_surface *src_surf;
110 struct svga_winsys_surface *dst_surf;
111 struct svga_buffer *dbuffer = svga_buffer(dst_tex);
112
113 src_surf = svga_buffer_handle(svga, src_tex);
114 dst_surf = svga_buffer_handle(svga, dst_tex);
115
116 ret = SVGA3D_vgpu10_BufferCopy(svga->swc, src_surf, dst_surf,
117 src_box->x, dstx, src_box->width);
118 if (ret != PIPE_OK) {
119 svga_context_flush(svga, NULL);
120 ret = SVGA3D_vgpu10_BufferCopy(svga->swc, src_surf, dst_surf,
121 src_box->x, dstx, src_box->width);
122 assert(ret == PIPE_OK);
123 }
124
125 dbuffer->dirty = TRUE;
126 }
127 else {
128 /* use map/memcpy fallback */
129 util_resource_copy_region(pipe, dst_tex, dst_level, dstx,
130 dsty, dstz, src_tex, src_level, src_box);
131 }
132 return;
133 }
134
135 stex = svga_texture(src_tex);
136 dtex = svga_texture(dst_tex);
137
138 if (src_tex->target == PIPE_TEXTURE_CUBE ||
139 src_tex->target == PIPE_TEXTURE_2D_ARRAY ||
140 src_tex->target == PIPE_TEXTURE_1D_ARRAY) {
141 src_face_layer = src_box->z;
142 src_z = 0;
143 assert(src_box->depth == 1);
144 }
145 else {
146 src_face_layer = 0;
147 src_z = src_box->z;
148 }
149
150 if (dst_tex->target == PIPE_TEXTURE_CUBE ||
151 dst_tex->target == PIPE_TEXTURE_2D_ARRAY ||
152 dst_tex->target == PIPE_TEXTURE_1D_ARRAY) {
153 dst_face_layer = dstz;
154 dst_z = 0;
155 assert(src_box->depth == 1);
156 }
157 else {
158 dst_face_layer = 0;
159 dst_z = dstz;
160 }
161
162 stex = svga_texture(src_tex);
163 dtex = svga_texture(dst_tex);
164
165 if (svga_have_vgpu10(svga)) {
166 /* vgpu10 */
167 if (util_format_is_compressed(src_tex->format) ==
168 util_format_is_compressed(dst_tex->format) &&
169 !util_format_is_depth_and_stencil(src_tex->format) &&
170 stex->handle != dtex->handle &&
171 src_tex->target == dst_tex->target) {
172 copy_region_vgpu10(svga,
173 src_tex,
174 src_box->x, src_box->y, src_z,
175 src_level, src_face_layer,
176 dst_tex,
177 dstx, dsty, dst_z,
178 dst_level, dst_face_layer,
179 src_box->width, src_box->height, src_box->depth);
180 }
181 else {
182 util_resource_copy_region(pipe, dst_tex, dst_level, dstx, dsty, dstz,
183 src_tex, src_level, src_box);
184 }
185 }
186 else {
187 /* vgpu9 */
188 if (src_tex->format == dst_tex->format) {
189 svga_texture_copy_handle(svga,
190 stex->handle,
191 src_box->x, src_box->y, src_z,
192 src_level, src_face_layer,
193 dtex->handle,
194 dstx, dsty, dst_z,
195 dst_level, dst_face_layer,
196 src_box->width, src_box->height,
197 src_box->depth);
198 }
199 else {
200 util_resource_copy_region(pipe, dst_tex, dst_level, dstx, dsty, dstz,
201 src_tex, src_level, src_box);
202 }
203 }
204
205 /* Mark the destination image as being defined */
206 svga_define_texture_level(dtex, dst_face_layer, dst_level);
207 }
208
209
210 /**
211 * The state tracker implements some resource copies with blits (for
212 * GL_ARB_copy_image). This function checks if we should really do the blit
213 * with a VGPU10 CopyRegion command or software fallback (for incompatible
214 * src/dst formats).
215 */
216 static bool
217 can_blit_via_copy_region_vgpu10(struct svga_context *svga,
218 const struct pipe_blit_info *blit_info)
219 {
220 struct svga_texture *dtex, *stex;
221
222 if (!svga_have_vgpu10(svga))
223 return false;
224
225 stex = svga_texture(blit_info->src.resource);
226 dtex = svga_texture(blit_info->dst.resource);
227
228 // can't copy within one resource
229 if (stex->handle == dtex->handle)
230 return false;
231
232 // can't copy between different resource types
233 if (blit_info->src.resource->target != blit_info->dst.resource->target)
234 return false;
235
236 // check that the blit src/dst regions are same size, no flipping, etc.
237 if (blit_info->src.box.width != blit_info->dst.box.width ||
238 blit_info->src.box.height != blit_info->dst.box.height)
239 return false;
240
241 // depth/stencil copies not supported at this time
242 if (blit_info->mask != PIPE_MASK_RGBA)
243 return false;
244
245 if (blit_info->alpha_blend || blit_info->render_condition_enable ||
246 blit_info->scissor_enable)
247 return false;
248
249 // check that src/dst surface formats are compatible for the VGPU device.
250 return util_is_format_compatible(
251 util_format_description(blit_info->src.resource->format),
252 util_format_description(blit_info->dst.resource->format));
253 }
254
255
256 static void
257 svga_blit(struct pipe_context *pipe,
258 const struct pipe_blit_info *blit_info)
259 {
260 struct svga_context *svga = svga_context(pipe);
261
262 if (!svga_have_vgpu10(svga) &&
263 blit_info->src.resource->nr_samples > 1 &&
264 blit_info->dst.resource->nr_samples <= 1 &&
265 !util_format_is_depth_or_stencil(blit_info->src.resource->format) &&
266 !util_format_is_pure_integer(blit_info->src.resource->format)) {
267 debug_printf("svga: color resolve unimplemented\n");
268 return;
269 }
270
271 if (can_blit_via_copy_region_vgpu10(svga, blit_info)) {
272 unsigned src_face, src_z, dst_face, dst_z;
273
274 if (blit_info->src.resource->target == PIPE_TEXTURE_CUBE) {
275 src_face = blit_info->src.box.z;
276 src_z = 0;
277 assert(blit_info->src.box.depth == 1);
278 }
279 else {
280 src_face = 0;
281 src_z = blit_info->src.box.z;
282 }
283
284 if (blit_info->dst.resource->target == PIPE_TEXTURE_CUBE) {
285 dst_face = blit_info->dst.box.z;
286 dst_z = 0;
287 assert(blit_info->src.box.depth == 1);
288 }
289 else {
290 dst_face = 0;
291 dst_z = blit_info->dst.box.z;
292 }
293
294 copy_region_vgpu10(svga,
295 blit_info->src.resource,
296 blit_info->src.box.x, blit_info->src.box.y, src_z,
297 blit_info->src.level, src_face,
298 blit_info->dst.resource,
299 blit_info->dst.box.x, blit_info->dst.box.y, dst_z,
300 blit_info->dst.level, dst_face,
301 blit_info->src.box.width, blit_info->src.box.height,
302 blit_info->src.box.depth);
303 return;
304 }
305
306 if (util_can_blit_via_copy_region(blit_info, TRUE) ||
307 util_can_blit_via_copy_region(blit_info, FALSE)) {
308 util_resource_copy_region(pipe, blit_info->dst.resource,
309 blit_info->dst.level,
310 blit_info->dst.box.x, blit_info->dst.box.y,
311 blit_info->dst.box.z, blit_info->src.resource,
312 blit_info->src.level, &blit_info->src.box);
313 return; /* done */
314 }
315
316 if ((blit_info->mask & PIPE_MASK_S) ||
317 !util_blitter_is_blit_supported(svga->blitter, blit_info)) {
318 debug_printf("svga: blit unsupported %s -> %s\n",
319 util_format_short_name(blit_info->src.resource->format),
320 util_format_short_name(blit_info->dst.resource->format));
321 return;
322 }
323
324 /* XXX turn off occlusion and streamout queries */
325
326 util_blitter_save_vertex_buffer_slot(svga->blitter, svga->curr.vb);
327 util_blitter_save_vertex_elements(svga->blitter, (void*)svga->curr.velems);
328 util_blitter_save_vertex_shader(svga->blitter, svga->curr.vs);
329 util_blitter_save_geometry_shader(svga->blitter, svga->curr.user_gs);
330 util_blitter_save_so_targets(svga->blitter, svga->num_so_targets,
331 (struct pipe_stream_output_target**)svga->so_targets);
332 util_blitter_save_rasterizer(svga->blitter, (void*)svga->curr.rast);
333 util_blitter_save_viewport(svga->blitter, &svga->curr.viewport);
334 util_blitter_save_scissor(svga->blitter, &svga->curr.scissor);
335 util_blitter_save_fragment_shader(svga->blitter, svga->curr.fs);
336 util_blitter_save_blend(svga->blitter, (void*)svga->curr.blend);
337 util_blitter_save_depth_stencil_alpha(svga->blitter,
338 (void*)svga->curr.depth);
339 util_blitter_save_stencil_ref(svga->blitter, &svga->curr.stencil_ref);
340 util_blitter_save_sample_mask(svga->blitter, svga->curr.sample_mask);
341 util_blitter_save_framebuffer(svga->blitter, &svga->curr.framebuffer);
342 util_blitter_save_fragment_sampler_states(svga->blitter,
343 svga->curr.num_samplers[PIPE_SHADER_FRAGMENT],
344 (void**)svga->curr.sampler[PIPE_SHADER_FRAGMENT]);
345 util_blitter_save_fragment_sampler_views(svga->blitter,
346 svga->curr.num_sampler_views[PIPE_SHADER_FRAGMENT],
347 svga->curr.sampler_views[PIPE_SHADER_FRAGMENT]);
348 /*util_blitter_save_render_condition(svga->blitter, svga->render_cond_query,
349 svga->render_cond_cond, svga->render_cond_mode);*/
350 util_blitter_blit(svga->blitter, blit_info);
351 }
352
353
354 static void
355 svga_flush_resource(struct pipe_context *pipe,
356 struct pipe_resource *resource)
357 {
358 }
359
360
361 void
362 svga_init_blit_functions(struct svga_context *svga)
363 {
364 svga->pipe.resource_copy_region = svga_resource_copy_region;
365 svga->pipe.blit = svga_blit;
366 svga->pipe.flush_resource = svga_flush_resource;
367 }