gallium: rename clearRT / clearDS to clear_render_target / clear_depth_stencil
[profile/ivi/mesa.git] / src / gallium / drivers / r300 / r300_blit.c
1 /*
2  * Copyright 2009 Marek Olšák <maraeo@gmail.com>
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 "r300_context.h"
24 #include "r300_texture.h"
25
26 #include "util/u_format.h"
27
28 static void r300_blitter_save_states(struct r300_context* r300)
29 {
30     util_blitter_save_blend(r300->blitter, r300->blend_state.state);
31     util_blitter_save_depth_stencil_alpha(r300->blitter, r300->dsa_state.state);
32     util_blitter_save_stencil_ref(r300->blitter, &(r300->stencil_ref));
33     util_blitter_save_rasterizer(r300->blitter, r300->rs_state.state);
34     util_blitter_save_fragment_shader(r300->blitter, r300->fs.state);
35     util_blitter_save_vertex_shader(r300->blitter, r300->vs_state.state);
36     util_blitter_save_viewport(r300->blitter, &r300->viewport);
37     util_blitter_save_clip(r300->blitter, &r300->clip);
38     util_blitter_save_vertex_elements(r300->blitter, r300->velems);
39     /* XXX this crashes the driver
40     util_blitter_save_vertex_buffers(r300->blitter, r300->vertex_buffer_count,
41                                      r300->vertex_buffer); */
42 }
43
44 /* Clear currently bound buffers. */
45 static void r300_clear(struct pipe_context* pipe,
46                        unsigned buffers,
47                        const float* rgba,
48                        double depth,
49                        unsigned stencil)
50 {
51     /* XXX Implement fastfill.
52      *
53      * If fastfill is enabled, a few facts should be considered:
54      *
55      * 1) Zbuffer must be micro-tiled and whole microtiles must be
56      *    written.
57      *
58      * 2) ZB_DEPTHCLEARVALUE is used to clear a zbuffer and Z Mask must be
59      *    equal to 0.
60      *
61      * 3) For 16-bit integer buffering, compression causes a hung with one or
62      *    two samples and should not be used.
63      *
64      * 4) Fastfill must not be used if reading of compressed Z data is disabled
65      *    and writing of compressed Z data is enabled (RD/WR_COMP_ENABLE),
66      *    i.e. it cannot be used to compress the zbuffer.
67      *    (what the hell does that mean and how does it fit in clearing
68      *    the buffers?)
69      *
70      * - Marek
71      */
72
73     struct r300_context* r300 = r300_context(pipe);
74     struct pipe_framebuffer_state* fb =
75         (struct pipe_framebuffer_state*)r300->fb_state.state;
76
77     r300_blitter_save_states(r300);
78
79     util_blitter_clear(r300->blitter,
80                        fb->width,
81                        fb->height,
82                        fb->nr_cbufs,
83                        buffers, rgba, depth, stencil);
84 }
85
86 /* Copy a block of pixels from one surface to another using HW. */
87 static void r300_hw_copy_region(struct pipe_context* pipe,
88                                 struct pipe_resource *dst,
89                                 struct pipe_subresource subdst,
90                                 unsigned dstx, unsigned dsty, unsigned dstz,
91                                 struct pipe_resource *src,
92                                 struct pipe_subresource subsrc,
93                                 unsigned srcx, unsigned srcy, unsigned srcz,
94                                 unsigned width, unsigned height)
95 {
96     struct r300_context* r300 = r300_context(pipe);
97     struct r300_textures_state* state =
98         (struct r300_textures_state*)r300->textures_state.state;
99
100     /* Yeah we have to save all those states to ensure this blitter operation
101      * is really transparent. The states will be restored by the blitter once
102      * copying is done. */
103     r300_blitter_save_states(r300);
104     util_blitter_save_framebuffer(r300->blitter, r300->fb_state.state);
105
106     util_blitter_save_fragment_sampler_states(
107         r300->blitter, state->sampler_state_count,
108         (void**)state->sampler_states);
109
110     util_blitter_save_fragment_sampler_views(
111         r300->blitter, state->sampler_view_count,
112         (struct pipe_sampler_view**)state->sampler_views);
113
114     /* Do a copy */
115     util_blitter_copy_region(r300->blitter, dst, subdst, dstx, dsty, dstz,
116                              src, subsrc, srcx, srcy, srcz, width, height,
117                              TRUE);
118 }
119
120 /* Copy a block of pixels from one surface to another. */
121 static void r300_resource_copy_region(struct pipe_context *pipe,
122                                       struct pipe_resource *dst,
123                                       struct pipe_subresource subdst,
124                                       unsigned dstx, unsigned dsty, unsigned dstz,
125                                       struct pipe_resource *src,
126                                       struct pipe_subresource subsrc,
127                                       unsigned srcx, unsigned srcy, unsigned srcz,
128                                       unsigned width, unsigned height)
129 {
130     enum pipe_format old_format = dst->format;
131     enum pipe_format new_format = old_format;
132
133     if (dst->format != src->format) {
134         debug_printf("r300: Implementation error: Format mismatch in %s\n"
135             "    : src: %s dst: %s\n", __FUNCTION__,
136             util_format_short_name(src->format),
137             util_format_short_name(dst->format));
138         debug_assert(0);
139     }
140
141     if (!pipe->screen->is_format_supported(pipe->screen,
142                                            old_format, src->target,
143                                            src->nr_samples,
144                                            PIPE_BIND_RENDER_TARGET |
145                                            PIPE_BIND_SAMPLER_VIEW, 0) &&
146         util_format_is_plain(old_format)) {
147         switch (util_format_get_blocksize(old_format)) {
148             case 1:
149                 new_format = PIPE_FORMAT_I8_UNORM;
150                 break;
151             case 2:
152                 new_format = PIPE_FORMAT_B4G4R4A4_UNORM;
153                 break;
154             case 4:
155                 new_format = PIPE_FORMAT_B8G8R8A8_UNORM;
156                 break;
157             case 8:
158                 new_format = PIPE_FORMAT_R16G16B16A16_UNORM;
159                 break;
160             default:
161                 debug_printf("r300: surface_copy: Unhandled format: %s. Falling back to software.\n"
162                              "r300: surface_copy: Software fallback doesn't work for tiled textures.\n",
163                              util_format_short_name(old_format));
164         }
165     }
166
167     if (old_format != new_format) {
168         dst->format = new_format;
169         src->format = new_format;
170
171         r300_texture_reinterpret_format(pipe->screen,
172                                         dst, new_format);
173         r300_texture_reinterpret_format(pipe->screen,
174                                         src, new_format);
175     }
176
177     r300_hw_copy_region(pipe, dst, subdst, dstx, dsty, dstz,
178                         src, subsrc, srcx, srcy, srcz, width, height);
179
180     if (old_format != new_format) {
181         dst->format = old_format;
182         src->format = old_format;
183
184         r300_texture_reinterpret_format(pipe->screen,
185                                         dst, old_format);
186         r300_texture_reinterpret_format(pipe->screen,
187                                         src, old_format);
188     }
189 }
190
191 /* Clear a region of a color surface to a constant value. */
192 static void r300_clear_render_target(struct pipe_context *pipe,
193                                      struct pipe_surface *dst,
194                                      const float *rgba,
195                                      unsigned dstx, unsigned dsty,
196                                      unsigned width, unsigned height)
197 {
198     struct r300_context *r300 = r300_context(pipe);
199
200     r300_blitter_save_states(r300);
201     util_blitter_save_framebuffer(r300->blitter, r300->fb_state.state);
202
203     util_blitter_clear_render_target(r300->blitter, dst, rgba,
204                                      dstx, dsty, width, height);
205 }
206
207 /* Clear a region of a depth stencil surface. */
208 static void r300_clear_depth_stencil(struct pipe_context *pipe,
209                                      struct pipe_surface *dst,
210                                      unsigned clear_flags,
211                                      double depth,
212                                      unsigned stencil,
213                                      unsigned dstx, unsigned dsty,
214                                      unsigned width, unsigned height)
215 {
216     struct r300_context *r300 = r300_context(pipe);
217
218     r300_blitter_save_states(r300);
219     util_blitter_save_framebuffer(r300->blitter, r300->fb_state.state);
220
221     util_blitter_clear_depth_stencil(r300->blitter, dst, clear_flags, depth, stencil,
222                                      dstx, dsty, width, height);
223 }
224
225 void r300_init_blit_functions(struct r300_context *r300)
226 {
227     r300->context.clear = r300_clear;
228     r300->context.clear_render_target = r300_clear_render_target;
229     r300->context.clear_depth_stencil = r300_clear_depth_stencil;
230     r300->context.resource_copy_region = r300_resource_copy_region;
231 }