r600g: implement TXQ_LZ opcode
[mesa.git] / src / gallium / drivers / r600 / r600_blit.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #include "r600_pipe.h"
24 #include "util/u_surface.h"
25 #include "util/u_blitter.h"
26 #include "util/u_format.h"
27
28 enum r600_blitter_op /* bitmask */
29 {
30 R600_SAVE_FRAGMENT_STATE = 1,
31 R600_SAVE_TEXTURES = 2,
32 R600_SAVE_FRAMEBUFFER = 4,
33 R600_DISABLE_RENDER_COND = 8,
34
35 R600_CLEAR = R600_SAVE_FRAGMENT_STATE,
36
37 R600_CLEAR_SURFACE = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER,
38
39 R600_COPY_BUFFER = R600_DISABLE_RENDER_COND,
40
41 R600_COPY_TEXTURE = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_SAVE_TEXTURES |
42 R600_DISABLE_RENDER_COND,
43
44 R600_DECOMPRESS = R600_SAVE_FRAGMENT_STATE | R600_SAVE_FRAMEBUFFER | R600_DISABLE_RENDER_COND,
45 };
46
47 static void r600_blitter_begin(struct pipe_context *ctx, enum r600_blitter_op op)
48 {
49 struct r600_context *rctx = (struct r600_context *)ctx;
50
51 r600_suspend_nontimer_queries(rctx);
52
53 util_blitter_save_vertex_buffers(rctx->blitter,
54 util_last_bit(rctx->vertex_buffer_state.enabled_mask),
55 rctx->vertex_buffer_state.vb);
56 util_blitter_save_vertex_elements(rctx->blitter, rctx->vertex_elements);
57 util_blitter_save_vertex_shader(rctx->blitter, rctx->vs_shader);
58 util_blitter_save_so_targets(rctx->blitter, rctx->num_so_targets,
59 (struct pipe_stream_output_target**)rctx->so_targets);
60 util_blitter_save_rasterizer(rctx->blitter, rctx->states[R600_PIPE_STATE_RASTERIZER]);
61
62 if (op & R600_SAVE_FRAGMENT_STATE) {
63 if (rctx->states[R600_PIPE_STATE_VIEWPORT]) {
64 util_blitter_save_viewport(rctx->blitter, &rctx->viewport);
65 }
66 util_blitter_save_fragment_shader(rctx->blitter, rctx->ps_shader);
67 util_blitter_save_blend(rctx->blitter, rctx->states[R600_PIPE_STATE_BLEND]);
68 util_blitter_save_depth_stencil_alpha(rctx->blitter, rctx->states[R600_PIPE_STATE_DSA]);
69 if (rctx->states[R600_PIPE_STATE_STENCIL_REF]) {
70 util_blitter_save_stencil_ref(rctx->blitter, &rctx->stencil_ref);
71 }
72 util_blitter_save_sample_mask(rctx->blitter, rctx->sample_mask.sample_mask);
73 }
74
75 if (op & R600_SAVE_FRAMEBUFFER)
76 util_blitter_save_framebuffer(rctx->blitter, &rctx->framebuffer);
77
78 if (op & R600_SAVE_TEXTURES) {
79 util_blitter_save_fragment_sampler_states(
80 rctx->blitter, rctx->ps_samplers.n_samplers,
81 (void**)rctx->ps_samplers.samplers);
82
83 util_blitter_save_fragment_sampler_views(
84 rctx->blitter, util_last_bit(rctx->ps_samplers.views.enabled_mask),
85 (struct pipe_sampler_view**)rctx->ps_samplers.views.views);
86 }
87
88 if ((op & R600_DISABLE_RENDER_COND) && rctx->current_render_cond) {
89 rctx->saved_render_cond = rctx->current_render_cond;
90 rctx->saved_render_cond_mode = rctx->current_render_cond_mode;
91 rctx->context.render_condition(&rctx->context, NULL, 0);
92 }
93
94 }
95
96 static void r600_blitter_end(struct pipe_context *ctx)
97 {
98 struct r600_context *rctx = (struct r600_context *)ctx;
99 if (rctx->saved_render_cond) {
100 rctx->context.render_condition(&rctx->context,
101 rctx->saved_render_cond,
102 rctx->saved_render_cond_mode);
103 rctx->saved_render_cond = NULL;
104 }
105 r600_resume_nontimer_queries(rctx);
106 }
107
108 static unsigned u_max_layer(struct pipe_resource *r, unsigned level)
109 {
110 switch (r->target) {
111 case PIPE_TEXTURE_CUBE:
112 return 6 - 1;
113 case PIPE_TEXTURE_3D:
114 return u_minify(r->depth0, level) - 1;
115 case PIPE_TEXTURE_1D_ARRAY:
116 case PIPE_TEXTURE_2D_ARRAY:
117 return r->array_size - 1;
118 default:
119 return 0;
120 }
121 }
122
123 void r600_blit_uncompress_depth(struct pipe_context *ctx,
124 struct r600_resource_texture *texture,
125 struct r600_resource_texture *staging,
126 unsigned first_level, unsigned last_level,
127 unsigned first_layer, unsigned last_layer)
128 {
129 struct r600_context *rctx = (struct r600_context *)ctx;
130 unsigned layer, level, checked_last_layer, max_layer;
131 float depth = 1.0f;
132 struct r600_resource_texture *flushed_depth_texture = staging ?
133 staging : texture->flushed_depth_texture;
134 const struct util_format_description *desc =
135 util_format_description(texture->resource.b.b.format);
136
137 if (!staging && !texture->dirty_db_mask)
138 return;
139
140 if (rctx->family == CHIP_RV610 || rctx->family == CHIP_RV630 ||
141 rctx->family == CHIP_RV620 || rctx->family == CHIP_RV635)
142 depth = 0.0f;
143
144 /* Enable decompression in DB_RENDER_CONTROL */
145 rctx->db_misc_state.flush_depthstencil_through_cb = true;
146 rctx->db_misc_state.copy_depth = util_format_has_depth(desc);
147 rctx->db_misc_state.copy_stencil = util_format_has_stencil(desc);
148 r600_atom_dirty(rctx, &rctx->db_misc_state.atom);
149
150 for (level = first_level; level <= last_level; level++) {
151 if (!staging && !(texture->dirty_db_mask & (1 << level)))
152 continue;
153
154 /* The smaller the mipmap level, the less layers there are
155 * as far as 3D textures are concerned. */
156 max_layer = u_max_layer(&texture->resource.b.b, level);
157 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
158
159 for (layer = first_layer; layer <= checked_last_layer; layer++) {
160 struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
161
162 surf_tmpl.format = texture->real_format;
163 surf_tmpl.u.tex.level = level;
164 surf_tmpl.u.tex.first_layer = layer;
165 surf_tmpl.u.tex.last_layer = layer;
166 surf_tmpl.usage = PIPE_BIND_DEPTH_STENCIL;
167
168 zsurf = ctx->create_surface(ctx, &texture->resource.b.b, &surf_tmpl);
169
170 surf_tmpl.format = flushed_depth_texture->real_format;
171 surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
172 cbsurf = ctx->create_surface(ctx,
173 (struct pipe_resource*)flushed_depth_texture, &surf_tmpl);
174
175 r600_blitter_begin(ctx, R600_DECOMPRESS);
176 util_blitter_custom_depth_stencil(rctx->blitter, zsurf, cbsurf, rctx->custom_dsa_flush, depth);
177 r600_blitter_end(ctx);
178
179 pipe_surface_reference(&zsurf, NULL);
180 pipe_surface_reference(&cbsurf, NULL);
181 }
182
183 /* The texture will always be dirty if some layers aren't flushed.
184 * I don't think this case can occur though. */
185 if (!staging && first_layer == 0 && last_layer == max_layer) {
186 texture->dirty_db_mask &= ~(1 << level);
187 }
188 }
189
190 /* reenable compression in DB_RENDER_CONTROL */
191 rctx->db_misc_state.flush_depthstencil_through_cb = false;
192 r600_atom_dirty(rctx, &rctx->db_misc_state.atom);
193 }
194
195 void r600_flush_depth_textures(struct r600_context *rctx,
196 struct r600_samplerview_state *textures)
197 {
198 unsigned i;
199 unsigned depth_texture_mask = textures->depth_texture_mask;
200
201 while (depth_texture_mask) {
202 struct pipe_sampler_view *view;
203 struct r600_resource_texture *tex;
204
205 i = u_bit_scan(&depth_texture_mask);
206
207 view = &textures->views[i]->base;
208 assert(view);
209
210 tex = (struct r600_resource_texture *)view->texture;
211 assert(tex->is_depth && !tex->is_flushing_texture);
212
213 r600_blit_uncompress_depth(&rctx->context, tex, NULL,
214 view->u.tex.first_level,
215 view->u.tex.last_level,
216 0,
217 u_max_layer(&tex->resource.b.b, view->u.tex.first_level));
218 }
219 }
220
221 static void r600_clear(struct pipe_context *ctx, unsigned buffers,
222 const union pipe_color_union *color,
223 double depth, unsigned stencil)
224 {
225 struct r600_context *rctx = (struct r600_context *)ctx;
226 struct pipe_framebuffer_state *fb = &rctx->framebuffer;
227
228 r600_blitter_begin(ctx, R600_CLEAR);
229 util_blitter_clear(rctx->blitter, fb->width, fb->height,
230 fb->nr_cbufs, buffers, fb->nr_cbufs ? fb->cbufs[0]->format : PIPE_FORMAT_NONE,
231 color, depth, stencil);
232 r600_blitter_end(ctx);
233 }
234
235 static void r600_clear_render_target(struct pipe_context *ctx,
236 struct pipe_surface *dst,
237 const union pipe_color_union *color,
238 unsigned dstx, unsigned dsty,
239 unsigned width, unsigned height)
240 {
241 struct r600_context *rctx = (struct r600_context *)ctx;
242
243 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
244 util_blitter_clear_render_target(rctx->blitter, dst, color,
245 dstx, dsty, width, height);
246 r600_blitter_end(ctx);
247 }
248
249 static void r600_clear_depth_stencil(struct pipe_context *ctx,
250 struct pipe_surface *dst,
251 unsigned clear_flags,
252 double depth,
253 unsigned stencil,
254 unsigned dstx, unsigned dsty,
255 unsigned width, unsigned height)
256 {
257 struct r600_context *rctx = (struct r600_context *)ctx;
258
259 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
260 util_blitter_clear_depth_stencil(rctx->blitter, dst, clear_flags, depth, stencil,
261 dstx, dsty, width, height);
262 r600_blitter_end(ctx);
263 }
264
265 void r600_copy_buffer(struct pipe_context *ctx, struct
266 pipe_resource *dst, unsigned dstx,
267 struct pipe_resource *src, const struct pipe_box *src_box)
268 {
269 struct r600_context *rctx = (struct r600_context*)ctx;
270
271 if (rctx->screen->has_streamout &&
272 /* Require dword alignment. */
273 dstx % 4 == 0 && src_box->x % 4 == 0 && src_box->width % 4 == 0) {
274 r600_blitter_begin(ctx, R600_COPY_BUFFER);
275 util_blitter_copy_buffer(rctx->blitter, dst, dstx, src, src_box->x, src_box->width);
276 r600_blitter_end(ctx);
277 } else {
278 util_resource_copy_region(ctx, dst, 0, dstx, 0, 0, src, 0, src_box);
279 }
280 }
281
282 struct texture_orig_info {
283 unsigned format;
284 unsigned width0;
285 unsigned height0;
286 unsigned npix_x;
287 unsigned npix_y;
288 unsigned npix0_x;
289 unsigned npix0_y;
290 };
291
292 static void r600_compressed_to_blittable(struct pipe_resource *tex,
293 unsigned level,
294 struct texture_orig_info *orig)
295 {
296 struct r600_resource_texture *rtex = (struct r600_resource_texture*)tex;
297 unsigned pixsize = util_format_get_blocksize(rtex->real_format);
298 int new_format;
299 int new_height, new_width;
300
301 orig->format = tex->format;
302 orig->width0 = tex->width0;
303 orig->height0 = tex->height0;
304 orig->npix0_x = rtex->surface.level[0].npix_x;
305 orig->npix0_y = rtex->surface.level[0].npix_y;
306 orig->npix_x = rtex->surface.level[level].npix_x;
307 orig->npix_y = rtex->surface.level[level].npix_y;
308
309 if (pixsize == 8)
310 new_format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */
311 else
312 new_format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */
313
314 new_width = util_format_get_nblocksx(tex->format, orig->width0);
315 new_height = util_format_get_nblocksy(tex->format, orig->height0);
316
317 tex->width0 = new_width;
318 tex->height0 = new_height;
319 tex->format = new_format;
320 rtex->surface.level[0].npix_x = util_format_get_nblocksx(orig->format, orig->npix0_x);
321 rtex->surface.level[0].npix_y = util_format_get_nblocksy(orig->format, orig->npix0_y);
322 rtex->surface.level[level].npix_x = util_format_get_nblocksx(orig->format, orig->npix_x);
323 rtex->surface.level[level].npix_y = util_format_get_nblocksy(orig->format, orig->npix_y);
324 }
325
326 static void r600_change_format(struct pipe_resource *tex,
327 unsigned level,
328 struct texture_orig_info *orig,
329 enum pipe_format format)
330 {
331 struct r600_resource_texture *rtex = (struct r600_resource_texture*)tex;
332
333 orig->format = tex->format;
334 orig->width0 = tex->width0;
335 orig->height0 = tex->height0;
336 orig->npix0_x = rtex->surface.level[0].npix_x;
337 orig->npix0_y = rtex->surface.level[0].npix_y;
338 orig->npix_x = rtex->surface.level[level].npix_x;
339 orig->npix_y = rtex->surface.level[level].npix_y;
340
341 tex->format = format;
342 }
343
344 static void r600_reset_blittable_to_orig(struct pipe_resource *tex,
345 unsigned level,
346 struct texture_orig_info *orig)
347 {
348 struct r600_resource_texture *rtex = (struct r600_resource_texture*)tex;
349
350 tex->format = orig->format;
351 tex->width0 = orig->width0;
352 tex->height0 = orig->height0;
353 rtex->surface.level[0].npix_x = orig->npix0_x;
354 rtex->surface.level[0].npix_y = orig->npix0_y;
355 rtex->surface.level[level].npix_x = orig->npix_x;
356 rtex->surface.level[level].npix_y = orig->npix_y;
357 }
358
359 static void r600_resource_copy_region(struct pipe_context *ctx,
360 struct pipe_resource *dst,
361 unsigned dst_level,
362 unsigned dstx, unsigned dsty, unsigned dstz,
363 struct pipe_resource *src,
364 unsigned src_level,
365 const struct pipe_box *src_box)
366 {
367 struct r600_context *rctx = (struct r600_context *)ctx;
368 struct r600_resource_texture *rsrc = (struct r600_resource_texture*)src;
369 struct texture_orig_info orig_info[2];
370 struct pipe_box sbox;
371 const struct pipe_box *psbox = src_box;
372 boolean restore_orig[2];
373
374 memset(orig_info, 0, sizeof(orig_info));
375
376 /* Handle buffers first. */
377 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
378 r600_copy_buffer(ctx, dst, dstx, src, src_box);
379 return;
380 }
381
382 /* This must be done before entering u_blitter to avoid recursion. */
383 if (rsrc->is_depth && !rsrc->is_flushing_texture) {
384 if (!r600_init_flushed_depth_texture(ctx, src, NULL))
385 return; /* error */
386
387 r600_blit_uncompress_depth(ctx, rsrc, NULL,
388 src_level, src_level,
389 src_box->z, src_box->z + src_box->depth - 1);
390 }
391
392 restore_orig[0] = restore_orig[1] = FALSE;
393
394 if (util_format_is_compressed(src->format) &&
395 util_format_is_compressed(dst->format)) {
396 r600_compressed_to_blittable(src, src_level, &orig_info[0]);
397 restore_orig[0] = TRUE;
398 sbox.x = util_format_get_nblocksx(orig_info[0].format, src_box->x);
399 sbox.y = util_format_get_nblocksy(orig_info[0].format, src_box->y);
400 sbox.z = src_box->z;
401 sbox.width = util_format_get_nblocksx(orig_info[0].format, src_box->width);
402 sbox.height = util_format_get_nblocksy(orig_info[0].format, src_box->height);
403 sbox.depth = src_box->depth;
404 psbox=&sbox;
405
406 r600_compressed_to_blittable(dst, dst_level, &orig_info[1]);
407 restore_orig[1] = TRUE;
408 /* translate the dst box as well */
409 dstx = util_format_get_nblocksx(orig_info[1].format, dstx);
410 dsty = util_format_get_nblocksy(orig_info[1].format, dsty);
411 } else if (!util_blitter_is_copy_supported(rctx->blitter, dst, src,
412 PIPE_MASK_RGBAZS)) {
413 unsigned blocksize = util_format_get_blocksize(src->format);
414
415 switch (blocksize) {
416 case 1:
417 r600_change_format(src, src_level, &orig_info[0],
418 PIPE_FORMAT_R8_UNORM);
419 r600_change_format(dst, dst_level, &orig_info[1],
420 PIPE_FORMAT_R8_UNORM);
421 break;
422 case 4:
423 r600_change_format(src, src_level, &orig_info[0],
424 PIPE_FORMAT_R8G8B8A8_UNORM);
425 r600_change_format(dst, dst_level, &orig_info[1],
426 PIPE_FORMAT_R8G8B8A8_UNORM);
427 break;
428 default:
429 fprintf(stderr, "Unhandled format %s with blocksize %u\n",
430 util_format_short_name(src->format), blocksize);
431 assert(0);
432 }
433 restore_orig[0] = TRUE;
434 restore_orig[1] = TRUE;
435 }
436
437 r600_blitter_begin(ctx, R600_COPY_TEXTURE);
438 util_blitter_copy_texture(rctx->blitter, dst, dst_level, ~0, dstx, dsty, dstz,
439 src, src_level, 0, psbox);
440 r600_blitter_end(ctx);
441
442 if (restore_orig[0])
443 r600_reset_blittable_to_orig(src, src_level, &orig_info[0]);
444
445 if (restore_orig[1])
446 r600_reset_blittable_to_orig(dst, dst_level, &orig_info[1]);
447 }
448
449 void r600_init_blit_functions(struct r600_context *rctx)
450 {
451 rctx->context.clear = r600_clear;
452 rctx->context.clear_render_target = r600_clear_render_target;
453 rctx->context.clear_depth_stencil = r600_clear_depth_stencil;
454 rctx->context.resource_copy_region = r600_resource_copy_region;
455 }