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