r300g: fix microtiling on RS6xx
[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 #include "util/u_pack_color.h"
28
29 enum r300_blitter_op /* bitmask */
30 {
31 R300_CLEAR = 1,
32 R300_CLEAR_SURFACE = 2,
33 R300_COPY = 4
34 };
35
36 static void r300_blitter_begin(struct r300_context* r300, enum r300_blitter_op op)
37 {
38 if (r300->query_current) {
39 r300->blitter_saved_query = r300->query_current;
40 r300_stop_query(r300);
41 }
42
43 /* Yeah we have to save all those states to ensure the blitter operation
44 * is really transparent. The states will be restored by the blitter once
45 * copying is done. */
46 util_blitter_save_blend(r300->blitter, r300->blend_state.state);
47 util_blitter_save_depth_stencil_alpha(r300->blitter, r300->dsa_state.state);
48 util_blitter_save_stencil_ref(r300->blitter, &(r300->stencil_ref));
49 util_blitter_save_rasterizer(r300->blitter, r300->rs_state.state);
50 util_blitter_save_fragment_shader(r300->blitter, r300->fs.state);
51 util_blitter_save_vertex_shader(r300->blitter, r300->vs_state.state);
52 util_blitter_save_viewport(r300->blitter, &r300->viewport);
53 util_blitter_save_clip(r300->blitter, (struct pipe_clip_state*)r300->clip_state.state);
54 util_blitter_save_vertex_elements(r300->blitter, r300->velems);
55 util_blitter_save_vertex_buffers(r300->blitter, r300->vertex_buffer_count,
56 r300->vertex_buffer);
57
58 if (op & (R300_CLEAR_SURFACE | R300_COPY))
59 util_blitter_save_framebuffer(r300->blitter, r300->fb_state.state);
60
61 if (op & R300_COPY) {
62 struct r300_textures_state* state =
63 (struct r300_textures_state*)r300->textures_state.state;
64
65 util_blitter_save_fragment_sampler_states(
66 r300->blitter, state->sampler_state_count,
67 (void**)state->sampler_states);
68
69 util_blitter_save_fragment_sampler_views(
70 r300->blitter, state->sampler_view_count,
71 (struct pipe_sampler_view**)state->sampler_views);
72 }
73 }
74
75 static void r300_blitter_end(struct r300_context *r300)
76 {
77 if (r300->blitter_saved_query) {
78 r300_resume_query(r300, r300->blitter_saved_query);
79 r300->blitter_saved_query = NULL;
80 }
81 }
82
83 static uint32_t r300_depth_clear_cb_value(enum pipe_format format,
84 const float* rgba)
85 {
86 union util_color uc;
87 util_pack_color(rgba, format, &uc);
88
89 if (util_format_get_blocksizebits(format) == 32)
90 return uc.ui;
91 else
92 return uc.us | (uc.us << 16);
93 }
94
95 static boolean r300_cbzb_clear_allowed(struct r300_context *r300,
96 unsigned clear_buffers)
97 {
98 struct pipe_framebuffer_state *fb =
99 (struct pipe_framebuffer_state*)r300->fb_state.state;
100
101 /* Only color clear allowed, and only one colorbuffer. */
102 if (clear_buffers != PIPE_CLEAR_COLOR || fb->nr_cbufs != 1)
103 return FALSE;
104
105 return r300_surface(fb->cbufs[0])->cbzb_allowed;
106 }
107
108 /* Clear currently bound buffers. */
109 static void r300_clear(struct pipe_context* pipe,
110 unsigned buffers,
111 const float* rgba,
112 double depth,
113 unsigned stencil)
114 {
115 /* My notes about fastfill:
116 *
117 * 1) Only the zbuffer is cleared.
118 *
119 * 2) The zbuffer must be micro-tiled and whole microtiles must be
120 * written. If microtiling is disabled, it locks up.
121 *
122 * 3) There is Z Mask RAM which contains a compressed zbuffer and
123 * it interacts with fastfill. We should figure out how to use it
124 * to get more performance.
125 * This is what we know about the Z Mask:
126 *
127 * Each dword of the Z Mask contains compression information
128 * for 16 4x4 pixel blocks, that is 2 bits for each block.
129 * On chips with 2 Z pipes, every other dword maps to a different
130 * pipe.
131 *
132 * 4) ZB_DEPTHCLEARVALUE is used to clear the zbuffer and the Z Mask must
133 * be equal to 0. (clear the Z Mask RAM with zeros)
134 *
135 * 5) For 16-bit zbuffer, compression causes a hung with one or
136 * two samples and should not be used.
137 *
138 * 6) FORCE_COMPRESSED_STENCIL_VALUE should be enabled for stencil clears
139 * to avoid needless decompression.
140 *
141 * 7) Fastfill must not be used if reading of compressed Z data is disabled
142 * and writing of compressed Z data is enabled (RD/WR_COMP_ENABLE),
143 * i.e. it cannot be used to compress the zbuffer.
144 *
145 * 8) ZB_CB_CLEAR does not interact with fastfill in any way.
146 *
147 * - Marek
148 */
149
150 struct r300_context* r300 = r300_context(pipe);
151 struct pipe_framebuffer_state *fb =
152 (struct pipe_framebuffer_state*)r300->fb_state.state;
153 struct r300_hyperz_state *hyperz =
154 (struct r300_hyperz_state*)r300->hyperz_state.state;
155 uint32_t width = fb->width;
156 uint32_t height = fb->height;
157
158 /* Enable CBZB clear. */
159 if (r300_cbzb_clear_allowed(r300, buffers)) {
160 struct r300_surface *surf = r300_surface(fb->cbufs[0]);
161
162 hyperz->zb_depthclearvalue =
163 r300_depth_clear_cb_value(surf->base.format, rgba);
164
165 width = surf->cbzb_width;
166 height = surf->cbzb_height;
167
168 r300->cbzb_clear = TRUE;
169 r300_mark_fb_state_dirty(r300, R300_CHANGED_CBZB_FLAG);
170 }
171
172 /* Clear. */
173 r300_blitter_begin(r300, R300_CLEAR);
174 util_blitter_clear(r300->blitter,
175 width,
176 height,
177 fb->nr_cbufs,
178 buffers, rgba, depth, stencil);
179 r300_blitter_end(r300);
180
181 /* Disable CBZB clear. */
182 if (r300->cbzb_clear) {
183 r300->cbzb_clear = FALSE;
184 r300_mark_fb_state_dirty(r300, R300_CHANGED_CBZB_FLAG);
185 }
186
187 /* XXX this flush "fixes" a hardlock in the cubestorm xscreensaver */
188 if (r300->flush_counter == 0)
189 pipe->flush(pipe, 0, NULL);
190 }
191
192 /* Clear a region of a color surface to a constant value. */
193 static void r300_clear_render_target(struct pipe_context *pipe,
194 struct pipe_surface *dst,
195 const float *rgba,
196 unsigned dstx, unsigned dsty,
197 unsigned width, unsigned height)
198 {
199 struct r300_context *r300 = r300_context(pipe);
200
201 r300_blitter_begin(r300, R300_CLEAR_SURFACE);
202 util_blitter_clear_render_target(r300->blitter, dst, rgba,
203 dstx, dsty, width, height);
204 r300_blitter_end(r300);
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_begin(r300, R300_CLEAR_SURFACE);
219 util_blitter_clear_depth_stencil(r300->blitter, dst, clear_flags, depth, stencil,
220 dstx, dsty, width, height);
221 r300_blitter_end(r300);
222 }
223
224 /* Copy a block of pixels from one surface to another using HW. */
225 static void r300_hw_copy_region(struct pipe_context* pipe,
226 struct pipe_resource *dst,
227 struct pipe_subresource subdst,
228 unsigned dstx, unsigned dsty, unsigned dstz,
229 struct pipe_resource *src,
230 struct pipe_subresource subsrc,
231 unsigned srcx, unsigned srcy, unsigned srcz,
232 unsigned width, unsigned height)
233 {
234 struct r300_context* r300 = r300_context(pipe);
235
236 r300_blitter_begin(r300, R300_COPY);
237 util_blitter_copy_region(r300->blitter, dst, subdst, dstx, dsty, dstz,
238 src, subsrc, srcx, srcy, srcz, width, height,
239 TRUE);
240 r300_blitter_end(r300);
241 }
242
243 /* Copy a block of pixels from one surface to another. */
244 static void r300_resource_copy_region(struct pipe_context *pipe,
245 struct pipe_resource *dst,
246 struct pipe_subresource subdst,
247 unsigned dstx, unsigned dsty, unsigned dstz,
248 struct pipe_resource *src,
249 struct pipe_subresource subsrc,
250 unsigned srcx, unsigned srcy, unsigned srcz,
251 unsigned width, unsigned height)
252 {
253 enum pipe_format old_format = dst->format;
254 enum pipe_format new_format = old_format;
255
256 if (!pipe->screen->is_format_supported(pipe->screen,
257 old_format, src->target,
258 src->nr_samples,
259 PIPE_BIND_RENDER_TARGET |
260 PIPE_BIND_SAMPLER_VIEW, 0) &&
261 util_format_is_plain(old_format)) {
262 switch (util_format_get_blocksize(old_format)) {
263 case 1:
264 new_format = PIPE_FORMAT_I8_UNORM;
265 break;
266 case 2:
267 new_format = PIPE_FORMAT_B4G4R4A4_UNORM;
268 break;
269 case 4:
270 new_format = PIPE_FORMAT_B8G8R8A8_UNORM;
271 break;
272 case 8:
273 new_format = PIPE_FORMAT_R16G16B16A16_UNORM;
274 break;
275 default:
276 debug_printf("r300: surface_copy: Unhandled format: %s. Falling back to software.\n"
277 "r300: surface_copy: Software fallback doesn't work for tiled textures.\n",
278 util_format_short_name(old_format));
279 }
280 }
281
282 if (old_format != new_format) {
283 dst->format = new_format;
284 src->format = new_format;
285
286 r300_texture_reinterpret_format(pipe->screen,
287 dst, new_format);
288 r300_texture_reinterpret_format(pipe->screen,
289 src, new_format);
290 }
291
292 r300_hw_copy_region(pipe, dst, subdst, dstx, dsty, dstz,
293 src, subsrc, srcx, srcy, srcz, width, height);
294
295 if (old_format != new_format) {
296 dst->format = old_format;
297 src->format = old_format;
298
299 r300_texture_reinterpret_format(pipe->screen,
300 dst, old_format);
301 r300_texture_reinterpret_format(pipe->screen,
302 src, old_format);
303 }
304 }
305
306 void r300_init_blit_functions(struct r300_context *r300)
307 {
308 r300->context.clear = r300_clear;
309 r300->context.clear_render_target = r300_clear_render_target;
310 r300->context.clear_depth_stencil = r300_clear_depth_stencil;
311 r300->context.resource_copy_region = r300_resource_copy_region;
312 }