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