Tizen 2.0 Release
[profile/ivi/osmesa.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 <util/u_surface.h>
24 #include <util/u_blitter.h>
25 #include <util/u_format.h>
26 #include "r600_pipe.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_pipe_context *rctx = (struct r600_pipe_context *)ctx;
47
48         rctx->blit = true;
49         r600_context_queries_suspend(&rctx->ctx);
50
51         util_blitter_save_blend(rctx->blitter, rctx->states[R600_PIPE_STATE_BLEND]);
52         util_blitter_save_depth_stencil_alpha(rctx->blitter, rctx->states[R600_PIPE_STATE_DSA]);
53         if (rctx->states[R600_PIPE_STATE_STENCIL_REF]) {
54                 util_blitter_save_stencil_ref(rctx->blitter, &rctx->stencil_ref);
55         }
56         util_blitter_save_rasterizer(rctx->blitter, rctx->states[R600_PIPE_STATE_RASTERIZER]);
57         util_blitter_save_fragment_shader(rctx->blitter, rctx->ps_shader);
58         util_blitter_save_vertex_shader(rctx->blitter, rctx->vs_shader);
59         util_blitter_save_vertex_elements(rctx->blitter, rctx->vertex_elements);
60         if (rctx->states[R600_PIPE_STATE_VIEWPORT]) {
61                 util_blitter_save_viewport(rctx->blitter, &rctx->viewport);
62         }
63         if (rctx->states[R600_PIPE_STATE_CLIP]) {
64                 util_blitter_save_clip(rctx->blitter, &rctx->clip);
65         }
66         util_blitter_save_vertex_buffers(rctx->blitter,
67                                          rctx->vbuf_mgr->nr_vertex_buffers,
68                                          rctx->vbuf_mgr->vertex_buffer);
69
70         if (op & R600_SAVE_FRAMEBUFFER)
71                 util_blitter_save_framebuffer(rctx->blitter, &rctx->framebuffer);
72
73         if (op & R600_SAVE_TEXTURES) {
74                 util_blitter_save_fragment_sampler_states(
75                         rctx->blitter, rctx->ps_samplers.n_samplers,
76                         (void**)rctx->ps_samplers.samplers);
77
78                 util_blitter_save_fragment_sampler_views(
79                         rctx->blitter, rctx->ps_samplers.n_views,
80                         (struct pipe_sampler_view**)rctx->ps_samplers.views);
81         }
82
83         if ((op & R600_DISABLE_RENDER_COND) && rctx->current_render_cond) {
84                 rctx->saved_render_cond = rctx->current_render_cond;
85                 rctx->saved_render_cond_mode = rctx->current_render_cond_mode;
86                 rctx->context.render_condition(&rctx->context, NULL, 0);
87         }
88
89 }
90
91 static void r600_blitter_end(struct pipe_context *ctx)
92 {
93         struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
94         if (rctx->saved_render_cond) {
95                 rctx->context.render_condition(&rctx->context,
96                                                rctx->saved_render_cond,
97                                                rctx->saved_render_cond_mode);
98                 rctx->saved_render_cond = NULL;
99         }
100         r600_context_queries_resume(&rctx->ctx, FALSE);
101         rctx->blit = false;
102 }
103
104 void r600_blit_uncompress_depth(struct pipe_context *ctx, struct r600_resource_texture *texture)
105 {
106         struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
107         struct pipe_surface *zsurf, *cbsurf, surf_tmpl;
108         int level = 0;
109         float depth = 1.0f;
110
111         if (!texture->dirty_db)
112                 return;
113
114         surf_tmpl.format = texture->resource.b.b.b.format;
115         surf_tmpl.u.tex.level = level;
116         surf_tmpl.u.tex.first_layer = 0;
117         surf_tmpl.u.tex.last_layer = 0;
118         surf_tmpl.usage = PIPE_BIND_DEPTH_STENCIL;
119
120         zsurf = ctx->create_surface(ctx, &texture->resource.b.b.b, &surf_tmpl);
121
122         surf_tmpl.format = ((struct pipe_resource*)texture->flushed_depth_texture)->format;
123         surf_tmpl.usage = PIPE_BIND_RENDER_TARGET;
124         cbsurf = ctx->create_surface(ctx,
125                         (struct pipe_resource*)texture->flushed_depth_texture, &surf_tmpl);
126
127         if (rctx->family == CHIP_RV610 || rctx->family == CHIP_RV630 ||
128             rctx->family == CHIP_RV620 || rctx->family == CHIP_RV635)
129                 depth = 0.0f;
130
131         r600_blitter_begin(ctx, R600_DECOMPRESS);
132         util_blitter_custom_depth_stencil(rctx->blitter, zsurf, cbsurf, rctx->custom_dsa_flush, depth);
133         r600_blitter_end(ctx);
134
135         pipe_surface_reference(&zsurf, NULL);
136         pipe_surface_reference(&cbsurf, NULL);
137
138         texture->dirty_db = FALSE;
139 }
140
141 void r600_flush_depth_textures(struct r600_pipe_context *rctx)
142 {
143         unsigned int i;
144
145         /* FIXME: This handles fragment shader textures only. */
146
147         for (i = 0; i < rctx->ps_samplers.n_views; ++i) {
148                 struct r600_pipe_sampler_view *view;
149                 struct r600_resource_texture *tex;
150
151                 view = rctx->ps_samplers.views[i];
152                 if (!view) continue;
153
154                 tex = (struct r600_resource_texture *)view->base.texture;
155                 if (!tex->depth)
156                         continue;
157
158                 if (tex->is_flushing_texture)
159                         continue;
160
161                 r600_blit_uncompress_depth(&rctx->context, tex);
162         }
163
164         /* also check CB here */
165         for (i = 0; i < rctx->framebuffer.nr_cbufs; i++) {
166                 struct r600_resource_texture *tex;
167                 tex = (struct r600_resource_texture *)rctx->framebuffer.cbufs[i]->texture;
168
169                 if (!tex->depth)
170                         continue;
171
172                 if (tex->is_flushing_texture)
173                         continue;
174
175                 r600_blit_uncompress_depth(&rctx->context, tex);
176         }
177 }
178
179 static void r600_clear(struct pipe_context *ctx, unsigned buffers,
180                         const float *rgba, double depth, unsigned stencil)
181 {
182         struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
183         struct pipe_framebuffer_state *fb = &rctx->framebuffer;
184
185         r600_blitter_begin(ctx, R600_CLEAR);
186         util_blitter_clear(rctx->blitter, fb->width, fb->height,
187                                 fb->nr_cbufs, buffers, rgba, depth,
188                                 stencil);
189         r600_blitter_end(ctx);
190 }
191
192 static void r600_clear_render_target(struct pipe_context *ctx,
193                                      struct pipe_surface *dst,
194                                      const float *rgba,
195                                      unsigned dstx, unsigned dsty,
196                                      unsigned width, unsigned height)
197 {
198         struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
199
200         r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
201         util_blitter_clear_render_target(rctx->blitter, dst, rgba,
202                                          dstx, dsty, width, height);
203         r600_blitter_end(ctx);
204 }
205
206 static void r600_clear_depth_stencil(struct pipe_context *ctx,
207                                      struct pipe_surface *dst,
208                                      unsigned clear_flags,
209                                      double depth,
210                                      unsigned stencil,
211                                      unsigned dstx, unsigned dsty,
212                                      unsigned width, unsigned height)
213 {
214         struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
215
216         r600_blitter_begin(ctx, R600_CLEAR_SURFACE);
217         util_blitter_clear_depth_stencil(rctx->blitter, dst, clear_flags, depth, stencil,
218                                          dstx, dsty, width, height);
219         r600_blitter_end(ctx);
220 }
221
222
223
224 /* Copy a block of pixels from one surface to another using HW. */
225 static void r600_hw_copy_region(struct pipe_context *ctx,
226                                 struct pipe_resource *dst,
227                                 unsigned dst_level,
228                                 unsigned dstx, unsigned dsty, unsigned dstz,
229                                 struct pipe_resource *src,
230                                 unsigned src_level,
231                                 const struct pipe_box *src_box)
232 {
233         struct r600_pipe_context *rctx = (struct r600_pipe_context *)ctx;
234
235         r600_blitter_begin(ctx, R600_COPY);
236         util_blitter_copy_region(rctx->blitter, dst, dst_level, dstx, dsty, dstz,
237                                  src, src_level, src_box, TRUE);
238         r600_blitter_end(ctx);
239 }
240
241 struct texture_orig_info {
242         unsigned format;
243         unsigned width0;
244         unsigned height0;
245 };
246
247 static void r600_compressed_to_blittable(struct pipe_resource *tex,
248                                    unsigned level,
249                                    struct texture_orig_info *orig)
250 {
251         struct r600_resource_texture *rtex = (struct r600_resource_texture*)tex;
252         unsigned pixsize = util_format_get_blocksize(tex->format);
253         int new_format;
254         int new_height, new_width;
255
256         orig->format = tex->format;
257         orig->width0 = tex->width0;
258         orig->height0 = tex->height0;
259
260         if (pixsize == 8)
261                 new_format = PIPE_FORMAT_R16G16B16A16_UNORM; /* 64-bit block */
262         else
263                 new_format = PIPE_FORMAT_R32G32B32A32_UNORM; /* 128-bit block */
264
265         new_width = util_format_get_nblocksx(tex->format, orig->width0);
266         new_height = util_format_get_nblocksy(tex->format, orig->height0);
267
268         rtex->force_int_type = true;
269         tex->width0 = new_width;
270         tex->height0 = new_height;
271         tex->format = new_format;
272
273 }
274
275 static void r600_reset_blittable_to_compressed(struct pipe_resource *tex,
276                                          unsigned level,
277                                          struct texture_orig_info *orig)
278 {
279         struct r600_resource_texture *rtex = (struct r600_resource_texture*)tex;
280         rtex->force_int_type = false;
281
282         tex->format = orig->format;
283         tex->width0 = orig->width0;
284         tex->height0 = orig->height0;
285 }
286
287 static void r600_resource_copy_region(struct pipe_context *ctx,
288                                       struct pipe_resource *dst,
289                                       unsigned dst_level,
290                                       unsigned dstx, unsigned dsty, unsigned dstz,
291                                       struct pipe_resource *src,
292                                       unsigned src_level,
293                                       const struct pipe_box *src_box)
294 {
295         struct r600_resource_texture *rsrc = (struct r600_resource_texture*)src;
296         struct texture_orig_info orig_info[2];
297         struct pipe_box sbox;
298         const struct pipe_box *psbox;
299         boolean restore_orig[2];
300
301         /* Fallback for buffers. */
302         if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
303                 util_resource_copy_region(ctx, dst, dst_level, dstx, dsty, dstz,
304                                           src, src_level, src_box);
305                 return;
306         }
307
308         if (rsrc->depth && !rsrc->is_flushing_texture)
309                 r600_texture_depth_flush(ctx, src, FALSE);
310
311         restore_orig[0] = restore_orig[1] = FALSE;
312
313         if (util_format_is_compressed(src->format)) {
314                 r600_compressed_to_blittable(src, src_level, &orig_info[0]);
315                 restore_orig[0] = TRUE;
316                 sbox.x = util_format_get_nblocksx(orig_info[0].format, src_box->x);
317                 sbox.y = util_format_get_nblocksy(orig_info[0].format, src_box->y);
318                 sbox.z = src_box->z;
319                 sbox.width = util_format_get_nblocksx(orig_info[0].format, src_box->width);
320                 sbox.height = util_format_get_nblocksy(orig_info[0].format, src_box->height);
321                 sbox.depth = src_box->depth;
322                 psbox=&sbox;
323         } else
324                 psbox=src_box;
325
326         if (util_format_is_compressed(dst->format)) {
327                 r600_compressed_to_blittable(dst, dst_level, &orig_info[1]);
328                 restore_orig[1] = TRUE;
329                 /* translate the dst box as well */
330                 dstx = util_format_get_nblocksx(orig_info[1].format, dstx);
331                 dsty = util_format_get_nblocksy(orig_info[1].format, dsty);
332         }
333
334         r600_hw_copy_region(ctx, dst, dst_level, dstx, dsty, dstz,
335                             src, src_level, psbox);
336
337         if (restore_orig[0])
338                 r600_reset_blittable_to_compressed(src, src_level, &orig_info[0]);
339
340         if (restore_orig[1])
341                 r600_reset_blittable_to_compressed(dst, dst_level, &orig_info[1]);
342 }
343
344 void r600_init_blit_functions(struct r600_pipe_context *rctx)
345 {
346         rctx->context.clear = r600_clear;
347         rctx->context.clear_render_target = r600_clear_render_target;
348         rctx->context.clear_depth_stencil = r600_clear_depth_stencil;
349         rctx->context.resource_copy_region = r600_resource_copy_region;
350 }
351
352 void r600_blit_push_depth(struct pipe_context *ctx, struct r600_resource_texture *texture)
353 {
354         struct pipe_box sbox;
355
356         sbox.x = sbox.y = sbox.z = 0;
357         sbox.width = texture->resource.b.b.b.width0;
358         sbox.height = texture->resource.b.b.b.height0;
359         /* XXX that might be wrong */
360         sbox.depth = 1;
361
362         r600_hw_copy_region(ctx, (struct pipe_resource *)texture, 0,
363                             0, 0, 0,
364                             (struct pipe_resource *)texture->flushed_depth_texture, 0,
365                             &sbox);
366 }