r600g: move CB_SHADER_MASK setup into cb_misc_state
[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 rctx->nr_vertex_buffers,
64 rctx->vertex_buffer);
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, rctx->ps_samplers.n_views,
78 (struct pipe_sampler_view**)rctx->ps_samplers.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_num_layers(struct pipe_resource *r, unsigned level)
102 {
103 switch (r->target) {
104 case PIPE_TEXTURE_CUBE:
105 return 6;
106 case PIPE_TEXTURE_3D:
107 return u_minify(r->depth0, level);
108 case PIPE_TEXTURE_1D_ARRAY:
109 return r->array_size;
110 case PIPE_TEXTURE_2D_ARRAY:
111 return r->array_size;
112 default:
113 return 1;
114 }
115 }
116
117 void r600_blit_uncompress_depth(struct pipe_context *ctx,
118 struct r600_resource_texture *texture,
119 struct r600_resource_texture *staging)
120 {
121 struct r600_context *rctx = (struct r600_context *)ctx;
122 unsigned layer, level;
123 float depth = 1.0f;
124 struct r600_resource_texture *flushed_depth_texture = staging ?
125 staging : texture->flushed_depth_texture;
126
127 if (!staging && !texture->dirty_db)
128 return;
129
130 if (rctx->family == CHIP_RV610 || rctx->family == CHIP_RV630 ||
131 rctx->family == CHIP_RV620 || rctx->family == CHIP_RV635)
132 depth = 0.0f;
133
134 for (level = 0; level <= texture->resource.b.b.last_level; level++) {
135 unsigned num_layers = u_num_layers(&texture->resource.b.b, level);
136
137 for (layer = 0; layer < num_layers; layer++) {
138 struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
139
140 surf_tmpl.format = texture->real_format;
141 surf_tmpl.u.tex.level = level;
142 surf_tmpl.u.tex.first_layer = layer;
143 surf_tmpl.u.tex.last_layer = layer;
144 surf_tmpl.usage = PIPE_BIND_DEPTH_STENCIL;
145
146 zsurf = ctx->create_surface(ctx, &texture->resource.b.b, &surf_tmpl);
147
148 surf_tmpl.format = flushed_depth_texture->real_format;
149 surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
150 cbsurf = ctx->create_surface(ctx,
151 (struct pipe_resource*)flushed_depth_texture, &surf_tmpl);
152
153 r600_blitter_begin(ctx, R600_DECOMPRESS);
154 util_blitter_custom_depth_stencil(rctx->blitter, zsurf, cbsurf, rctx->custom_dsa_flush, depth);
155 r600_blitter_end(ctx);
156
157 pipe_surface_reference(&zsurf, NULL);
158 pipe_surface_reference(&cbsurf, NULL);
159 }
160 }
161
162 if (!staging)
163 texture->dirty_db = FALSE;
164 }
165
166 void r600_flush_depth_textures(struct r600_context *rctx)
167 {
168 unsigned int i;
169
170 /* XXX: This handles fragment shader textures only. */
171
172 for (i = 0; i < rctx->ps_samplers.n_views; ++i) {
173 struct r600_pipe_sampler_view *view;
174 struct r600_resource_texture *tex;
175
176 view = rctx->ps_samplers.views[i];
177 if (!view) continue;
178
179 tex = (struct r600_resource_texture *)view->base.texture;
180 if (!tex->is_depth)
181 continue;
182
183 if (tex->is_flushing_texture)
184 continue;
185
186 r600_blit_uncompress_depth(&rctx->context, tex, NULL);
187 }
188
189 /* also check CB here */
190 for (i = 0; i < rctx->framebuffer.nr_cbufs; i++) {
191 struct r600_resource_texture *tex;
192 tex = (struct r600_resource_texture *)rctx->framebuffer.cbufs[i]->texture;
193
194 if (!tex->is_depth)
195 continue;
196
197 if (tex->is_flushing_texture)
198 continue;
199
200 r600_blit_uncompress_depth(&rctx->context, tex, NULL);
201 }
202 }
203
204 static void r600_clear(struct pipe_context *ctx, unsigned buffers,
205 const union pipe_color_union *color,
206 double depth, unsigned stencil)
207 {
208 struct r600_context *rctx = (struct r600_context *)ctx;
209 struct pipe_framebuffer_state *fb = &rctx->framebuffer;
210
211 r600_blitter_begin(ctx, R600_CLEAR);
212 util_blitter_clear(rctx->blitter, fb->width, fb->height,
213 fb->nr_cbufs, buffers, fb->nr_cbufs ? fb->cbufs[0]->format : PIPE_FORMAT_NONE,
214 color, depth, stencil);
215 r600_blitter_end(ctx);
216 }
217
218 static void r600_clear_render_target(struct pipe_context *ctx,
219 struct pipe_surface *dst,
220 const union pipe_color_union *color,
221 unsigned dstx, unsigned dsty,
222 unsigned width, unsigned height)
223 {
224 struct r600_context *rctx = (struct r600_context *)ctx;
225
226 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
227 util_blitter_clear_render_target(rctx->blitter, dst, color,
228 dstx, dsty, width, height);
229 r600_blitter_end(ctx);
230 }
231
232 static void r600_clear_depth_stencil(struct pipe_context *ctx,
233 struct pipe_surface *dst,
234 unsigned clear_flags,
235 double depth,
236 unsigned stencil,
237 unsigned dstx, unsigned dsty,
238 unsigned width, unsigned height)
239 {
240 struct r600_context *rctx = (struct r600_context *)ctx;
241
242 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
243 util_blitter_clear_depth_stencil(rctx->blitter, dst, clear_flags, depth, stencil,
244 dstx, dsty, width, height);
245 r600_blitter_end(ctx);
246 }
247
248 struct texture_orig_info {
249 unsigned format;
250 unsigned width0;
251 unsigned height0;
252 unsigned npix_x;
253 unsigned npix_y;
254 unsigned npix0_x;
255 unsigned npix0_y;
256 };
257
258 static void r600_compressed_to_blittable(struct pipe_resource *tex,
259 unsigned level,
260 struct texture_orig_info *orig)
261 {
262 struct r600_resource_texture *rtex = (struct r600_resource_texture*)tex;
263 unsigned pixsize = util_format_get_blocksize(rtex->real_format);
264 int new_format;
265 int new_height, new_width;
266
267 orig->format = tex->format;
268 orig->width0 = tex->width0;
269 orig->height0 = tex->height0;
270 orig->npix0_x = rtex->surface.level[0].npix_x;
271 orig->npix0_y = rtex->surface.level[0].npix_y;
272 orig->npix_x = rtex->surface.level[level].npix_x;
273 orig->npix_y = rtex->surface.level[level].npix_y;
274
275 if (pixsize == 8)
276 new_format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */
277 else
278 new_format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */
279
280 new_width = util_format_get_nblocksx(tex->format, orig->width0);
281 new_height = util_format_get_nblocksy(tex->format, orig->height0);
282
283 tex->width0 = new_width;
284 tex->height0 = new_height;
285 tex->format = new_format;
286 rtex->surface.level[0].npix_x = util_format_get_nblocksx(orig->format, orig->npix0_x);
287 rtex->surface.level[0].npix_y = util_format_get_nblocksy(orig->format, orig->npix0_y);
288 rtex->surface.level[level].npix_x = util_format_get_nblocksx(orig->format, orig->npix_x);
289 rtex->surface.level[level].npix_y = util_format_get_nblocksy(orig->format, orig->npix_y);
290 }
291
292 static void r600_reset_blittable_to_compressed(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
298 tex->format = orig->format;
299 tex->width0 = orig->width0;
300 tex->height0 = orig->height0;
301 rtex->surface.level[0].npix_x = orig->npix0_x;
302 rtex->surface.level[0].npix_y = orig->npix0_y;
303 rtex->surface.level[level].npix_x = orig->npix_x;
304 rtex->surface.level[level].npix_y = orig->npix_y;
305 }
306
307 static void r600_resource_copy_region(struct pipe_context *ctx,
308 struct pipe_resource *dst,
309 unsigned dst_level,
310 unsigned dstx, unsigned dsty, unsigned dstz,
311 struct pipe_resource *src,
312 unsigned src_level,
313 const struct pipe_box *src_box)
314 {
315 struct r600_context *rctx = (struct r600_context *)ctx;
316 struct r600_resource_texture *rsrc = (struct r600_resource_texture*)src;
317 struct texture_orig_info orig_info[2];
318 struct pipe_box sbox;
319 const struct pipe_box *psbox;
320 boolean restore_orig[2];
321
322 memset(orig_info, 0, sizeof(orig_info));
323
324 /* Fallback for buffers. */
325 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
326 util_resource_copy_region(ctx, dst, dst_level, dstx, dsty, dstz,
327 src, src_level, src_box);
328 return;
329 }
330
331 if (rsrc->is_depth && !rsrc->is_flushing_texture)
332 r600_texture_depth_flush(ctx, src, NULL);
333
334 restore_orig[0] = restore_orig[1] = FALSE;
335
336 if (util_format_is_compressed(src->format)) {
337 r600_compressed_to_blittable(src, src_level, &orig_info[0]);
338 restore_orig[0] = TRUE;
339 sbox.x = util_format_get_nblocksx(orig_info[0].format, src_box->x);
340 sbox.y = util_format_get_nblocksy(orig_info[0].format, src_box->y);
341 sbox.z = src_box->z;
342 sbox.width = util_format_get_nblocksx(orig_info[0].format, src_box->width);
343 sbox.height = util_format_get_nblocksy(orig_info[0].format, src_box->height);
344 sbox.depth = src_box->depth;
345 psbox=&sbox;
346 } else
347 psbox=src_box;
348
349 if (util_format_is_compressed(dst->format)) {
350 r600_compressed_to_blittable(dst, dst_level, &orig_info[1]);
351 restore_orig[1] = TRUE;
352 /* translate the dst box as well */
353 dstx = util_format_get_nblocksx(orig_info[1].format, dstx);
354 dsty = util_format_get_nblocksy(orig_info[1].format, dsty);
355 }
356
357 r600_blitter_begin(ctx, R600_COPY);
358 util_blitter_copy_texture(rctx->blitter, dst, dst_level, dstx, dsty, dstz,
359 src, src_level, psbox, TRUE);
360 r600_blitter_end(ctx);
361
362 if (restore_orig[0])
363 r600_reset_blittable_to_compressed(src, src_level, &orig_info[0]);
364
365 if (restore_orig[1])
366 r600_reset_blittable_to_compressed(dst, dst_level, &orig_info[1]);
367 }
368
369 void r600_init_blit_functions(struct r600_context *rctx)
370 {
371 rctx->context.clear = r600_clear;
372 rctx->context.clear_render_target = r600_clear_render_target;
373 rctx->context.clear_depth_stencil = r600_clear_depth_stencil;
374 rctx->context.resource_copy_region = r600_resource_copy_region;
375 }