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