r600g: only set the index type if drawing is indexed
[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_TEXTURES = 1,
31 R600_SAVE_FRAMEBUFFER = 2,
32 R600_DISABLE_RENDER_COND = 4,
33
34 R600_CLEAR = 0,
35
36 R600_CLEAR_SURFACE = R600_SAVE_FRAMEBUFFER,
37
38 R600_COPY = R600_SAVE_FRAMEBUFFER | R600_SAVE_TEXTURES |
39 R600_DISABLE_RENDER_COND,
40
41 R600_DECOMPRESS = R600_SAVE_FRAMEBUFFER | R600_DISABLE_RENDER_COND,
42 };
43
44 static void r600_blitter_begin(struct pipe_context *ctx, enum r600_blitter_op op)
45 {
46 struct r600_context *rctx = (struct r600_context *)ctx;
47
48 r600_suspend_nontimer_queries(rctx);
49
50 util_blitter_save_blend(rctx->blitter, rctx->states[R600_PIPE_STATE_BLEND]);
51 util_blitter_save_depth_stencil_alpha(rctx->blitter, rctx->states[R600_PIPE_STATE_DSA]);
52 if (rctx->states[R600_PIPE_STATE_STENCIL_REF]) {
53 util_blitter_save_stencil_ref(rctx->blitter, &rctx->stencil_ref);
54 }
55 util_blitter_save_rasterizer(rctx->blitter, rctx->states[R600_PIPE_STATE_RASTERIZER]);
56 util_blitter_save_fragment_shader(rctx->blitter, rctx->ps_shader);
57 util_blitter_save_vertex_shader(rctx->blitter, rctx->vs_shader);
58 util_blitter_save_vertex_elements(rctx->blitter, rctx->vertex_elements);
59 if (rctx->states[R600_PIPE_STATE_VIEWPORT]) {
60 util_blitter_save_viewport(rctx->blitter, &rctx->viewport);
61 }
62 util_blitter_save_vertex_buffers(rctx->blitter,
63 util_last_bit(rctx->vertex_buffer_state.enabled_mask),
64 rctx->vertex_buffer_state.vb);
65 util_blitter_save_so_targets(rctx->blitter, rctx->num_so_targets,
66 (struct pipe_stream_output_target**)rctx->so_targets);
67
68 if (op & R600_SAVE_FRAMEBUFFER)
69 util_blitter_save_framebuffer(rctx->blitter, &rctx->framebuffer);
70
71 if (op & R600_SAVE_TEXTURES) {
72 util_blitter_save_fragment_sampler_states(
73 rctx->blitter, rctx->ps_samplers.n_samplers,
74 (void**)rctx->ps_samplers.samplers);
75
76 util_blitter_save_fragment_sampler_views(
77 rctx->blitter, util_last_bit(rctx->ps_samplers.views.enabled_mask),
78 (struct pipe_sampler_view**)rctx->ps_samplers.views.views);
79 }
80
81 if ((op & R600_DISABLE_RENDER_COND) && rctx->current_render_cond) {
82 rctx->saved_render_cond = rctx->current_render_cond;
83 rctx->saved_render_cond_mode = rctx->current_render_cond_mode;
84 rctx->context.render_condition(&rctx->context, NULL, 0);
85 }
86
87 }
88
89 static void r600_blitter_end(struct pipe_context *ctx)
90 {
91 struct r600_context *rctx = (struct r600_context *)ctx;
92 if (rctx->saved_render_cond) {
93 rctx->context.render_condition(&rctx->context,
94 rctx->saved_render_cond,
95 rctx->saved_render_cond_mode);
96 rctx->saved_render_cond = NULL;
97 }
98 r600_resume_nontimer_queries(rctx);
99 }
100
101 static unsigned u_max_layer(struct pipe_resource *r, unsigned level)
102 {
103 switch (r->target) {
104 case PIPE_TEXTURE_CUBE:
105 return 6 - 1;
106 case PIPE_TEXTURE_3D:
107 return u_minify(r->depth0, level) - 1;
108 case PIPE_TEXTURE_1D_ARRAY:
109 case PIPE_TEXTURE_2D_ARRAY:
110 return r->array_size - 1;
111 default:
112 return 0;
113 }
114 }
115
116 void r600_blit_uncompress_depth(struct pipe_context *ctx,
117 struct r600_resource_texture *texture,
118 struct r600_resource_texture *staging,
119 unsigned first_level, unsigned last_level,
120 unsigned first_layer, unsigned last_layer)
121 {
122 struct r600_context *rctx = (struct r600_context *)ctx;
123 unsigned layer, level, checked_last_layer, max_layer;
124 float depth = 1.0f;
125 struct r600_resource_texture *flushed_depth_texture = staging ?
126 staging : texture->flushed_depth_texture;
127
128 if (!staging && !texture->dirty_db_mask)
129 return;
130
131 if (rctx->family == CHIP_RV610 || rctx->family == CHIP_RV630 ||
132 rctx->family == CHIP_RV620 || rctx->family == CHIP_RV635)
133 depth = 0.0f;
134
135 if (!rctx->db_misc_state.flush_depthstencil_through_cb) {
136 /* Enable decompression in DB_RENDER_CONTROL */
137 rctx->db_misc_state.flush_depthstencil_through_cb = true;
138 r600_atom_dirty(rctx, &rctx->db_misc_state.atom);
139 }
140
141 for (level = first_level; level <= last_level; level++) {
142 if (!staging && !(texture->dirty_db_mask & (1 << level)))
143 continue;
144
145 /* The smaller the mipmap level, the less layers there are
146 * as far as 3D textures are concerned. */
147 max_layer = u_max_layer(&texture->resource.b.b, level);
148 checked_last_layer = last_layer < max_layer ? last_layer : max_layer;
149
150 for (layer = first_layer; layer <= checked_last_layer; layer++) {
151 struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
152
153 surf_tmpl.format = texture->real_format;
154 surf_tmpl.u.tex.level = level;
155 surf_tmpl.u.tex.first_layer = layer;
156 surf_tmpl.u.tex.last_layer = layer;
157 surf_tmpl.usage = PIPE_BIND_DEPTH_STENCIL;
158
159 zsurf = ctx->create_surface(ctx, &texture->resource.b.b, &surf_tmpl);
160
161 surf_tmpl.format = flushed_depth_texture->real_format;
162 surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
163 cbsurf = ctx->create_surface(ctx,
164 (struct pipe_resource*)flushed_depth_texture, &surf_tmpl);
165
166 r600_blitter_begin(ctx, R600_DECOMPRESS);
167 util_blitter_custom_depth_stencil(rctx->blitter, zsurf, cbsurf, rctx->custom_dsa_flush, depth);
168 r600_blitter_end(ctx);
169
170 pipe_surface_reference(&zsurf, NULL);
171 pipe_surface_reference(&cbsurf, NULL);
172 }
173
174 /* The texture will always be dirty if some layers aren't flushed.
175 * I don't think this case can occur though. */
176 if (!staging && first_layer == 0 && last_layer == max_layer) {
177 texture->dirty_db_mask &= ~(1 << level);
178 }
179 }
180
181 /* reenable compression in DB_RENDER_CONTROL */
182 rctx->db_misc_state.flush_depthstencil_through_cb = false;
183 r600_atom_dirty(rctx, &rctx->db_misc_state.atom);
184 }
185
186 void r600_flush_depth_textures(struct r600_context *rctx,
187 struct r600_samplerview_state *textures)
188 {
189 unsigned i;
190 unsigned depth_texture_mask = textures->depth_texture_mask;
191
192 while (depth_texture_mask) {
193 struct pipe_sampler_view *view;
194 struct r600_resource_texture *tex;
195
196 i = u_bit_scan(&depth_texture_mask);
197
198 view = &textures->views[i]->base;
199 assert(view);
200
201 tex = (struct r600_resource_texture *)view->texture;
202 assert(tex->is_depth && !tex->is_flushing_texture);
203
204 r600_blit_uncompress_depth(&rctx->context, tex, NULL,
205 view->u.tex.first_level,
206 view->u.tex.last_level,
207 0,
208 u_max_layer(&tex->resource.b.b, view->u.tex.first_level));
209 }
210 }
211
212 static void r600_clear(struct pipe_context *ctx, unsigned buffers,
213 const union pipe_color_union *color,
214 double depth, unsigned stencil)
215 {
216 struct r600_context *rctx = (struct r600_context *)ctx;
217 struct pipe_framebuffer_state *fb = &rctx->framebuffer;
218
219 r600_blitter_begin(ctx, R600_CLEAR);
220 util_blitter_clear(rctx->blitter, fb->width, fb->height,
221 fb->nr_cbufs, buffers, fb->nr_cbufs ? fb->cbufs[0]->format : PIPE_FORMAT_NONE,
222 color, depth, stencil);
223 r600_blitter_end(ctx);
224 }
225
226 static void r600_clear_render_target(struct pipe_context *ctx,
227 struct pipe_surface *dst,
228 const union pipe_color_union *color,
229 unsigned dstx, unsigned dsty,
230 unsigned width, unsigned height)
231 {
232 struct r600_context *rctx = (struct r600_context *)ctx;
233
234 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
235 util_blitter_clear_render_target(rctx->blitter, dst, color,
236 dstx, dsty, width, height);
237 r600_blitter_end(ctx);
238 }
239
240 static void r600_clear_depth_stencil(struct pipe_context *ctx,
241 struct pipe_surface *dst,
242 unsigned clear_flags,
243 double depth,
244 unsigned stencil,
245 unsigned dstx, unsigned dsty,
246 unsigned width, unsigned height)
247 {
248 struct r600_context *rctx = (struct r600_context *)ctx;
249
250 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
251 util_blitter_clear_depth_stencil(rctx->blitter, dst, clear_flags, depth, stencil,
252 dstx, dsty, width, height);
253 r600_blitter_end(ctx);
254 }
255
256 struct texture_orig_info {
257 unsigned format;
258 unsigned width0;
259 unsigned height0;
260 unsigned npix_x;
261 unsigned npix_y;
262 unsigned npix0_x;
263 unsigned npix0_y;
264 };
265
266 static void r600_compressed_to_blittable(struct pipe_resource *tex,
267 unsigned level,
268 struct texture_orig_info *orig)
269 {
270 struct r600_resource_texture *rtex = (struct r600_resource_texture*)tex;
271 unsigned pixsize = util_format_get_blocksize(rtex->real_format);
272 int new_format;
273 int new_height, new_width;
274
275 orig->format = tex->format;
276 orig->width0 = tex->width0;
277 orig->height0 = tex->height0;
278 orig->npix0_x = rtex->surface.level[0].npix_x;
279 orig->npix0_y = rtex->surface.level[0].npix_y;
280 orig->npix_x = rtex->surface.level[level].npix_x;
281 orig->npix_y = rtex->surface.level[level].npix_y;
282
283 if (pixsize == 8)
284 new_format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */
285 else
286 new_format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */
287
288 new_width = util_format_get_nblocksx(tex->format, orig->width0);
289 new_height = util_format_get_nblocksy(tex->format, orig->height0);
290
291 tex->width0 = new_width;
292 tex->height0 = new_height;
293 tex->format = new_format;
294 rtex->surface.level[0].npix_x = util_format_get_nblocksx(orig->format, orig->npix0_x);
295 rtex->surface.level[0].npix_y = util_format_get_nblocksy(orig->format, orig->npix0_y);
296 rtex->surface.level[level].npix_x = util_format_get_nblocksx(orig->format, orig->npix_x);
297 rtex->surface.level[level].npix_y = util_format_get_nblocksy(orig->format, orig->npix_y);
298 }
299
300 static void r600_reset_blittable_to_compressed(struct pipe_resource *tex,
301 unsigned level,
302 struct texture_orig_info *orig)
303 {
304 struct r600_resource_texture *rtex = (struct r600_resource_texture*)tex;
305
306 tex->format = orig->format;
307 tex->width0 = orig->width0;
308 tex->height0 = orig->height0;
309 rtex->surface.level[0].npix_x = orig->npix0_x;
310 rtex->surface.level[0].npix_y = orig->npix0_y;
311 rtex->surface.level[level].npix_x = orig->npix_x;
312 rtex->surface.level[level].npix_y = orig->npix_y;
313 }
314
315 static void r600_resource_copy_region(struct pipe_context *ctx,
316 struct pipe_resource *dst,
317 unsigned dst_level,
318 unsigned dstx, unsigned dsty, unsigned dstz,
319 struct pipe_resource *src,
320 unsigned src_level,
321 const struct pipe_box *src_box)
322 {
323 struct r600_context *rctx = (struct r600_context *)ctx;
324 struct r600_resource_texture *rsrc = (struct r600_resource_texture*)src;
325 struct texture_orig_info orig_info[2];
326 struct pipe_box sbox;
327 const struct pipe_box *psbox;
328 boolean restore_orig[2];
329
330 memset(orig_info, 0, sizeof(orig_info));
331
332 /* Fallback for buffers. */
333 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
334 util_resource_copy_region(ctx, dst, dst_level, dstx, dsty, dstz,
335 src, src_level, src_box);
336 return;
337 }
338
339 /* This must be done before entering u_blitter to avoid recursion. */
340 if (rsrc->is_depth && !rsrc->is_flushing_texture) {
341 if (!r600_init_flushed_depth_texture(ctx, src, NULL))
342 return; /* error */
343
344 r600_blit_uncompress_depth(ctx, rsrc, NULL,
345 src_level, src_level,
346 src_box->z, src_box->z + src_box->depth - 1);
347 }
348
349 restore_orig[0] = restore_orig[1] = FALSE;
350
351 if (util_format_is_compressed(src->format)) {
352 r600_compressed_to_blittable(src, src_level, &orig_info[0]);
353 restore_orig[0] = TRUE;
354 sbox.x = util_format_get_nblocksx(orig_info[0].format, src_box->x);
355 sbox.y = util_format_get_nblocksy(orig_info[0].format, src_box->y);
356 sbox.z = src_box->z;
357 sbox.width = util_format_get_nblocksx(orig_info[0].format, src_box->width);
358 sbox.height = util_format_get_nblocksy(orig_info[0].format, src_box->height);
359 sbox.depth = src_box->depth;
360 psbox=&sbox;
361 } else
362 psbox=src_box;
363
364 if (util_format_is_compressed(dst->format)) {
365 r600_compressed_to_blittable(dst, dst_level, &orig_info[1]);
366 restore_orig[1] = TRUE;
367 /* translate the dst box as well */
368 dstx = util_format_get_nblocksx(orig_info[1].format, dstx);
369 dsty = util_format_get_nblocksy(orig_info[1].format, dsty);
370 }
371
372 r600_blitter_begin(ctx, R600_COPY);
373 util_blitter_copy_texture(rctx->blitter, dst, dst_level, dstx, dsty, dstz,
374 src, src_level, psbox, TRUE);
375 r600_blitter_end(ctx);
376
377 if (restore_orig[0])
378 r600_reset_blittable_to_compressed(src, src_level, &orig_info[0]);
379
380 if (restore_orig[1])
381 r600_reset_blittable_to_compressed(dst, dst_level, &orig_info[1]);
382 }
383
384 void r600_init_blit_functions(struct r600_context *rctx)
385 {
386 rctx->context.clear = r600_clear;
387 rctx->context.clear_render_target = r600_clear_render_target;
388 rctx->context.clear_depth_stencil = r600_clear_depth_stencil;
389 rctx->context.resource_copy_region = r600_resource_copy_region;
390 }