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 "util/u_surface.h"
24 #include "util/u_blitter.h"
25 #include "util/u_format.h"
26 #include "radeonsi_pipe.h"
27 #include "si_state.h"
28
29 enum r600_blitter_op /* bitmask */
30 {
31 R600_SAVE_TEXTURES = 1,
32 R600_SAVE_FRAMEBUFFER = 2,
33 R600_DISABLE_RENDER_COND = 4,
34
35 R600_CLEAR = 0,
36
37 R600_CLEAR_SURFACE = R600_SAVE_FRAMEBUFFER,
38
39 R600_COPY = R600_SAVE_FRAMEBUFFER | R600_SAVE_TEXTURES |
40 R600_DISABLE_RENDER_COND,
41
42 R600_DECOMPRESS = R600_SAVE_FRAMEBUFFER | R600_DISABLE_RENDER_COND,
43 };
44
r600_blitter_begin(struct pipe_context * ctx,enum r600_blitter_op op)45 static void r600_blitter_begin(struct pipe_context *ctx, enum r600_blitter_op op)
46 {
47 struct r600_context *rctx = (struct r600_context *)ctx;
48
49 r600_context_queries_suspend(rctx);
50
51 util_blitter_save_blend(rctx->blitter, rctx->queued.named.blend);
52 util_blitter_save_depth_stencil_alpha(rctx->blitter, rctx->queued.named.dsa);
53 util_blitter_save_stencil_ref(rctx->blitter, &rctx->stencil_ref);
54 util_blitter_save_rasterizer(rctx->blitter, rctx->queued.named.rasterizer);
55 util_blitter_save_fragment_shader(rctx->blitter, rctx->ps_shader);
56 util_blitter_save_vertex_shader(rctx->blitter, rctx->vs_shader);
57 util_blitter_save_vertex_elements(rctx->blitter, rctx->vertex_elements);
58 if (rctx->queued.named.viewport) {
59 util_blitter_save_viewport(rctx->blitter, &rctx->queued.named.viewport->viewport);
60 }
61 util_blitter_save_vertex_buffers(rctx->blitter,
62 rctx->nr_vertex_buffers,
63 rctx->vertex_buffer);
64 util_blitter_save_so_targets(rctx->blitter, rctx->num_so_targets,
65 (struct pipe_stream_output_target**)rctx->so_targets);
66
67 if (op & R600_SAVE_FRAMEBUFFER)
68 util_blitter_save_framebuffer(rctx->blitter, &rctx->framebuffer);
69
70 if (op & R600_SAVE_TEXTURES) {
71 util_blitter_save_fragment_sampler_states(
72 rctx->blitter, rctx->ps_samplers.n_samplers,
73 (void**)rctx->ps_samplers.samplers);
74
75 util_blitter_save_fragment_sampler_views(
76 rctx->blitter, rctx->ps_samplers.n_views,
77 (struct pipe_sampler_view**)rctx->ps_samplers.views);
78 }
79
80 if ((op & R600_DISABLE_RENDER_COND) && rctx->current_render_cond) {
81 rctx->saved_render_cond = rctx->current_render_cond;
82 rctx->saved_render_cond_mode = rctx->current_render_cond_mode;
83 rctx->context.render_condition(&rctx->context, NULL, 0);
84 }
85
86 }
87
r600_blitter_end(struct pipe_context * ctx)88 static void r600_blitter_end(struct pipe_context *ctx)
89 {
90 struct r600_context *rctx = (struct r600_context *)ctx;
91 if (rctx->saved_render_cond) {
92 rctx->context.render_condition(&rctx->context,
93 rctx->saved_render_cond,
94 rctx->saved_render_cond_mode);
95 rctx->saved_render_cond = NULL;
96 }
97 r600_context_queries_resume(rctx);
98 }
99
u_num_layers(struct pipe_resource * r,unsigned level)100 static unsigned u_num_layers(struct pipe_resource *r, unsigned level)
101 {
102 switch (r->target) {
103 case PIPE_TEXTURE_CUBE:
104 return 6;
105 case PIPE_TEXTURE_3D:
106 return u_minify(r->depth0, level);
107 case PIPE_TEXTURE_1D_ARRAY:
108 return r->array_size;
109 case PIPE_TEXTURE_2D_ARRAY:
110 return r->array_size;
111 default:
112 return 1;
113 }
114 }
115
si_blit_uncompress_depth(struct pipe_context * ctx,struct r600_resource_texture * texture)116 void si_blit_uncompress_depth(struct pipe_context *ctx, struct r600_resource_texture *texture)
117 {
118 struct r600_context *rctx = (struct r600_context *)ctx;
119 unsigned layer, level;
120 float depth = 1.0f;
121
122 if (!texture->dirty_db)
123 return;
124
125 for (level = 0; level <= texture->resource.b.b.last_level; level++) {
126 unsigned num_layers = u_num_layers(&texture->resource.b.b, level);
127
128 for (layer = 0; layer < num_layers; layer++) {
129 struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
130
131 surf_tmpl.format = texture->real_format;
132 surf_tmpl.u.tex.level = level;
133 surf_tmpl.u.tex.first_layer = layer;
134 surf_tmpl.u.tex.last_layer = layer;
135 surf_tmpl.usage = PIPE_BIND_DEPTH_STENCIL;
136
137 zsurf = ctx->create_surface(ctx, &texture->resource.b.b, &surf_tmpl);
138
139 surf_tmpl.format = texture->flushed_depth_texture->real_format;
140 surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
141 cbsurf = ctx->create_surface(ctx,
142 (struct pipe_resource*)texture->flushed_depth_texture, &surf_tmpl);
143
144 r600_blitter_begin(ctx, R600_DECOMPRESS);
145 util_blitter_custom_depth_stencil(rctx->blitter, zsurf, cbsurf, ~0, rctx->custom_dsa_flush, depth);
146 r600_blitter_end(ctx);
147
148 pipe_surface_reference(&zsurf, NULL);
149 pipe_surface_reference(&cbsurf, NULL);
150 }
151 }
152
153 texture->dirty_db = FALSE;
154 }
155
si_flush_depth_textures(struct r600_context * rctx)156 void si_flush_depth_textures(struct r600_context *rctx)
157 {
158 unsigned int i;
159
160 /* FIXME: This handles fragment shader textures only. */
161
162 for (i = 0; i < rctx->ps_samplers.n_views; ++i) {
163 struct si_pipe_sampler_view *view;
164 struct r600_resource_texture *tex;
165
166 view = rctx->ps_samplers.views[i];
167 if (!view) continue;
168
169 tex = (struct r600_resource_texture *)view->base.texture;
170 if (!tex->depth)
171 continue;
172
173 if (tex->is_flushing_texture)
174 continue;
175
176 si_blit_uncompress_depth(&rctx->context, tex);
177 }
178
179 /* also check CB here */
180 for (i = 0; i < rctx->framebuffer.nr_cbufs; i++) {
181 struct r600_resource_texture *tex;
182 tex = (struct r600_resource_texture *)rctx->framebuffer.cbufs[i]->texture;
183
184 if (!tex->depth)
185 continue;
186
187 if (tex->is_flushing_texture)
188 continue;
189
190 si_blit_uncompress_depth(&rctx->context, tex);
191 }
192 }
193
r600_clear(struct pipe_context * ctx,unsigned buffers,const union pipe_color_union * color,double depth,unsigned stencil)194 static void r600_clear(struct pipe_context *ctx, unsigned buffers,
195 const union pipe_color_union *color,
196 double depth, unsigned stencil)
197 {
198 struct r600_context *rctx = (struct r600_context *)ctx;
199 struct pipe_framebuffer_state *fb = &rctx->framebuffer;
200
201 r600_blitter_begin(ctx, R600_CLEAR);
202 util_blitter_clear(rctx->blitter, fb->width, fb->height,
203 fb->nr_cbufs, buffers, fb->nr_cbufs ? fb->cbufs[0]->format : PIPE_FORMAT_NONE,
204 color, depth, stencil);
205 r600_blitter_end(ctx);
206 }
207
r600_clear_render_target(struct pipe_context * ctx,struct pipe_surface * dst,const union pipe_color_union * color,unsigned dstx,unsigned dsty,unsigned width,unsigned height)208 static void r600_clear_render_target(struct pipe_context *ctx,
209 struct pipe_surface *dst,
210 const union pipe_color_union *color,
211 unsigned dstx, unsigned dsty,
212 unsigned width, unsigned height)
213 {
214 struct r600_context *rctx = (struct r600_context *)ctx;
215
216 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
217 util_blitter_clear_render_target(rctx->blitter, dst, color,
218 dstx, dsty, width, height);
219 r600_blitter_end(ctx);
220 }
221
r600_clear_depth_stencil(struct pipe_context * ctx,struct pipe_surface * dst,unsigned clear_flags,double depth,unsigned stencil,unsigned dstx,unsigned dsty,unsigned width,unsigned height)222 static void r600_clear_depth_stencil(struct pipe_context *ctx,
223 struct pipe_surface *dst,
224 unsigned clear_flags,
225 double depth,
226 unsigned stencil,
227 unsigned dstx, unsigned dsty,
228 unsigned width, unsigned height)
229 {
230 struct r600_context *rctx = (struct r600_context *)ctx;
231
232 r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
233 util_blitter_clear_depth_stencil(rctx->blitter, dst, clear_flags, depth, stencil,
234 dstx, dsty, width, height);
235 r600_blitter_end(ctx);
236 }
237
238
239
240 /* Copy a block of pixels from one surface to another using HW. */
r600_hw_copy_region(struct pipe_context * ctx,struct pipe_resource * dst,unsigned dst_level,unsigned dstx,unsigned dsty,unsigned dstz,struct pipe_resource * src,unsigned src_level,const struct pipe_box * src_box)241 static void r600_hw_copy_region(struct pipe_context *ctx,
242 struct pipe_resource *dst,
243 unsigned dst_level,
244 unsigned dstx, unsigned dsty, unsigned dstz,
245 struct pipe_resource *src,
246 unsigned src_level,
247 const struct pipe_box *src_box)
248 {
249 struct r600_context *rctx = (struct r600_context *)ctx;
250
251 r600_blitter_begin(ctx, R600_COPY);
252 util_blitter_copy_texture(rctx->blitter, dst, dst_level, ~0, dstx, dsty, dstz,
253 src, src_level, 0, src_box);
254 r600_blitter_end(ctx);
255 }
256
257 struct texture_orig_info {
258 unsigned format;
259 unsigned width0;
260 unsigned height0;
261 unsigned npix_x;
262 unsigned npix_y;
263 unsigned npix0_x;
264 unsigned npix0_y;
265 };
266
r600_compressed_to_blittable(struct pipe_resource * tex,unsigned level,struct texture_orig_info * orig)267 static void r600_compressed_to_blittable(struct pipe_resource *tex,
268 unsigned level,
269 struct texture_orig_info *orig)
270 {
271 struct r600_resource_texture *rtex = (struct r600_resource_texture*)tex;
272 struct r600_screen *rscreen = (struct r600_screen *)tex->screen;
273 unsigned pixsize = util_format_get_blocksize(rtex->real_format);
274 int new_format;
275 int new_height, new_width;
276
277 orig->format = tex->format;
278 orig->width0 = tex->width0;
279 orig->height0 = tex->height0;
280 orig->npix0_x = rtex->surface.level[0].npix_x;
281 orig->npix0_y = rtex->surface.level[0].npix_y;
282 orig->npix_x = rtex->surface.level[level].npix_x;
283 orig->npix_y = rtex->surface.level[level].npix_y;
284
285 if (pixsize == 8)
286 new_format = PIPE_FORMAT_R16G16B16A16_UINT; /* 64-bit block */
287 else
288 new_format = PIPE_FORMAT_R32G32B32A32_UINT; /* 128-bit block */
289
290 new_width = util_format_get_nblocksx(tex->format, orig->width0);
291 new_height = util_format_get_nblocksy(tex->format, orig->height0);
292
293 tex->width0 = new_width;
294 tex->height0 = new_height;
295 tex->format = new_format;
296 rtex->surface.level[0].npix_x = util_format_get_nblocksx(orig->format, orig->npix0_x);
297 rtex->surface.level[0].npix_y = util_format_get_nblocksy(orig->format, orig->npix0_y);
298 rtex->surface.level[level].npix_x = util_format_get_nblocksx(orig->format, orig->npix_x);
299 rtex->surface.level[level].npix_y = util_format_get_nblocksy(orig->format, orig->npix_y);
300 }
301
r600_reset_blittable_to_compressed(struct pipe_resource * tex,unsigned level,struct texture_orig_info * orig)302 static void r600_reset_blittable_to_compressed(struct pipe_resource *tex,
303 unsigned level,
304 struct texture_orig_info *orig)
305 {
306 struct r600_resource_texture *rtex = (struct r600_resource_texture*)tex;
307 struct r600_screen *rscreen = (struct r600_screen *)tex->screen;
308
309 tex->format = orig->format;
310 tex->width0 = orig->width0;
311 tex->height0 = orig->height0;
312 rtex->surface.level[0].npix_x = orig->npix0_x;
313 rtex->surface.level[0].npix_y = orig->npix0_y;
314 rtex->surface.level[level].npix_x = orig->npix_x;
315 rtex->surface.level[level].npix_y = orig->npix_y;
316 }
317
r600_resource_copy_region(struct pipe_context * ctx,struct pipe_resource * dst,unsigned dst_level,unsigned dstx,unsigned dsty,unsigned dstz,struct pipe_resource * src,unsigned src_level,const struct pipe_box * src_box)318 static void r600_resource_copy_region(struct pipe_context *ctx,
319 struct pipe_resource *dst,
320 unsigned dst_level,
321 unsigned dstx, unsigned dsty, unsigned dstz,
322 struct pipe_resource *src,
323 unsigned src_level,
324 const struct pipe_box *src_box)
325 {
326 struct r600_resource_texture *rsrc = (struct r600_resource_texture*)src;
327 struct texture_orig_info orig_info[2];
328 struct pipe_box sbox;
329 const struct pipe_box *psbox;
330 boolean restore_orig[2];
331
332 memset(orig_info, 0, sizeof(orig_info));
333
334 /* Fallback for buffers. */
335 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
336 util_resource_copy_region(ctx, dst, dst_level, dstx, dsty, dstz,
337 src, src_level, src_box);
338 return;
339 }
340
341 if (rsrc->depth && !rsrc->is_flushing_texture)
342 r600_texture_depth_flush(ctx, src, FALSE);
343
344 restore_orig[0] = restore_orig[1] = FALSE;
345
346 if (util_format_is_compressed(src->format)) {
347 r600_compressed_to_blittable(src, src_level, &orig_info[0]);
348 restore_orig[0] = TRUE;
349 sbox.x = util_format_get_nblocksx(orig_info[0].format, src_box->x);
350 sbox.y = util_format_get_nblocksy(orig_info[0].format, src_box->y);
351 sbox.z = src_box->z;
352 sbox.width = util_format_get_nblocksx(orig_info[0].format, src_box->width);
353 sbox.height = util_format_get_nblocksy(orig_info[0].format, src_box->height);
354 sbox.depth = src_box->depth;
355 psbox=&sbox;
356 } else
357 psbox=src_box;
358
359 if (util_format_is_compressed(dst->format)) {
360 r600_compressed_to_blittable(dst, dst_level, &orig_info[1]);
361 restore_orig[1] = TRUE;
362 /* translate the dst box as well */
363 dstx = util_format_get_nblocksx(orig_info[1].format, dstx);
364 dsty = util_format_get_nblocksy(orig_info[1].format, dsty);
365 }
366
367 r600_hw_copy_region(ctx, dst, dst_level, dstx, dsty, dstz,
368 src, src_level, psbox);
369
370 if (restore_orig[0])
371 r600_reset_blittable_to_compressed(src, src_level, &orig_info[0]);
372
373 if (restore_orig[1])
374 r600_reset_blittable_to_compressed(dst, dst_level, &orig_info[1]);
375 }
376
si_init_blit_functions(struct r600_context * rctx)377 void si_init_blit_functions(struct r600_context *rctx)
378 {
379 rctx->context.clear = r600_clear;
380 rctx->context.clear_render_target = r600_clear_render_target;
381 rctx->context.clear_depth_stencil = r600_clear_depth_stencil;
382 rctx->context.resource_copy_region = r600_resource_copy_region;
383 }
384
r600_blit_push_depth(struct pipe_context * ctx,struct r600_resource_texture * texture)385 void r600_blit_push_depth(struct pipe_context *ctx, struct r600_resource_texture *texture)
386 {
387 struct pipe_box sbox;
388
389 sbox.x = sbox.y = sbox.z = 0;
390 sbox.width = texture->resource.b.b.width0;
391 sbox.height = texture->resource.b.b.height0;
392 /* XXX that might be wrong */
393 sbox.depth = 1;
394
395 r600_hw_copy_region(ctx, (struct pipe_resource *)texture, 0,
396 0, 0, 0,
397 (struct pipe_resource *)texture->flushed_depth_texture, 0,
398 &sbox);
399 }
400