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