bcdf950df9cfcba70332c7d5871174218a3c6707
[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_blit.h"
24 #include "r300_context.h"
25 #include "r300_texture.h"
26
27 #include "util/u_format.h"
28
29 static void r300_blitter_save_states(struct r300_context* r300)
30 {
31     util_blitter_save_blend(r300->blitter, r300->blend_state.state);
32     util_blitter_save_depth_stencil_alpha(r300->blitter, r300->dsa_state.state);
33     util_blitter_save_stencil_ref(r300->blitter, &(r300->stencil_ref));
34     util_blitter_save_rasterizer(r300->blitter, r300->rs_state.state);
35     util_blitter_save_fragment_shader(r300->blitter, r300->fs);
36     util_blitter_save_vertex_shader(r300->blitter, r300->vs_state.state);
37     util_blitter_save_viewport(r300->blitter, &r300->viewport);
38     util_blitter_save_clip(r300->blitter, &r300->clip);
39     util_blitter_save_vertex_elements(r300->blitter, r300->velems);
40 }
41
42 /* Clear currently bound buffers. */
43 void r300_clear(struct pipe_context* pipe,
44                 unsigned buffers,
45                 const float* rgba,
46                 double depth,
47                 unsigned stencil)
48 {
49     /* XXX Implement fastfill.
50      *
51      * If fastfill is enabled, a few facts should be considered:
52      *
53      * 1) Zbuffer must be micro-tiled and whole microtiles must be
54      *    written.
55      *
56      * 2) ZB_DEPTHCLEARVALUE is used to clear a zbuffer and Z Mask must be
57      *    equal to 0.
58      *
59      * 3) RB3D_COLOR_CLEAR_VALUE is used to clear a colorbuffer and
60      *    RB3D_COLOR_CHANNEL_MASK must be equal to 0.
61      *
62      * 4) ZB_CB_CLEAR can be used to make the ZB units help in clearing
63      *    the colorbuffer. The color clear value is supplied through both
64      *    RB3D_COLOR_CLEAR_VALUE and ZB_DEPTHCLEARVALUE, and the colorbuffer
65      *    must be set in ZB_DEPTHOFFSET and ZB_DEPTHPITCH in addition to
66      *    RB3D_COLOROFFSET and RB3D_COLORPITCH. It's obvious that the zbuffer
67      *    will not be cleared and multiple render targets cannot be cleared
68      *    this way either.
69      *
70      * 5) For 16-bit integer buffering, compression causes a hung with one or
71      *    two samples and should not be used.
72      *
73      * 6) Fastfill must not be used if reading of compressed Z data is disabled
74      *    and writing of compressed Z data is enabled (RD/WR_COMP_ENABLE),
75      *    i.e. it cannot be used to compress the zbuffer.
76      *    (what the hell does that mean and how does it fit in clearing
77      *    the buffers?)
78      *
79      * - Marek
80      */
81
82     struct r300_context* r300 = r300_context(pipe);
83     struct pipe_framebuffer_state* fb =
84         (struct pipe_framebuffer_state*)r300->fb_state.state;
85
86     r300_blitter_save_states(r300);
87
88     util_blitter_clear(r300->blitter,
89                        fb->width,
90                        fb->height,
91                        fb->nr_cbufs,
92                        buffers, rgba, depth, stencil);
93 }
94
95 /* Copy a block of pixels from one surface to another using HW. */
96 static void r300_hw_copy(struct pipe_context* pipe,
97                          struct pipe_surface* dst,
98                          unsigned dstx, unsigned dsty,
99                          struct pipe_surface* src,
100                          unsigned srcx, unsigned srcy,
101                          unsigned width, unsigned height)
102 {
103     struct r300_context* r300 = r300_context(pipe);
104     struct r300_textures_state* state =
105         (struct r300_textures_state*)r300->textures_state.state;
106
107     /* Yeah we have to save all those states to ensure this blitter operation
108      * is really transparent. The states will be restored by the blitter once
109      * copying is done. */
110     r300_blitter_save_states(r300);
111     util_blitter_save_framebuffer(r300->blitter, r300->fb_state.state);
112
113     util_blitter_save_fragment_sampler_states(
114         r300->blitter, state->sampler_count, (void**)state->sampler_states);
115
116     util_blitter_save_fragment_sampler_views(
117         r300->blitter, state->texture_count,
118         state->fragment_sampler_views);
119
120     /* Do a copy */
121     util_blitter_copy(r300->blitter,
122                       dst, dstx, dsty, src, srcx, srcy, width, height, TRUE);
123 }
124
125 /* Copy a block of pixels from one surface to another. */
126 void r300_surface_copy(struct pipe_context* pipe,
127                        struct pipe_surface* dst,
128                        unsigned dstx, unsigned dsty,
129                        struct pipe_surface* src,
130                        unsigned srcx, unsigned srcy,
131                        unsigned width, unsigned height)
132 {
133     enum pipe_format old_format = dst->texture->format;
134     enum pipe_format new_format = old_format;
135
136     assert(dst->texture->format == src->texture->format);
137
138     if (!pipe->screen->is_format_supported(pipe->screen,
139                                            old_format, src->texture->target,
140                                            PIPE_TEXTURE_USAGE_RENDER_TARGET |
141                                            PIPE_TEXTURE_USAGE_SAMPLER, 0)) {
142         switch (util_format_get_blocksize(old_format)) {
143             case 1:
144                 new_format = PIPE_FORMAT_I8_UNORM;
145                 break;
146             case 2:
147                 new_format = PIPE_FORMAT_B4G4R4A4_UNORM;
148                 break;
149             case 4:
150                 new_format = PIPE_FORMAT_B8G8R8A8_UNORM;
151                 break;
152             case 8:
153                 new_format = PIPE_FORMAT_R16G16B16A16_UNORM;
154                 break;
155             default:
156                 debug_printf("r300: surface_copy: Unhandled format: %s. Falling back to software.\n"
157                              "r300: surface_copy: Software fallback doesn't work for tiled textures.\n",
158                              util_format_name(old_format));
159         }
160     }
161
162     if (old_format != new_format) {
163         dst->format = new_format;
164         src->format = new_format;
165
166         r300_texture_reinterpret_format(pipe->screen,
167                                         dst->texture, new_format);
168         r300_texture_reinterpret_format(pipe->screen,
169                                         src->texture, new_format);
170     }
171
172     r300_hw_copy(pipe, dst, dstx, dsty, src, srcx, srcy, width, height);
173
174     if (old_format != new_format) {
175         dst->format = old_format;
176         src->format = old_format;
177
178         r300_texture_reinterpret_format(pipe->screen,
179                                         dst->texture, old_format);
180         r300_texture_reinterpret_format(pipe->screen,
181                                         src->texture, old_format);
182     }
183 }
184
185 /* Fill a region of a surface with a constant value. */
186 void r300_surface_fill(struct pipe_context* pipe,
187                        struct pipe_surface* dst,
188                        unsigned dstx, unsigned dsty,
189                        unsigned width, unsigned height,
190                        unsigned value)
191 {
192     struct r300_context* r300 = r300_context(pipe);
193
194     r300_blitter_save_states(r300);
195     util_blitter_save_framebuffer(r300->blitter, r300->fb_state.state);
196
197     util_blitter_fill(r300->blitter,
198                       dst, dstx, dsty, width, height, value);
199 }