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