r300g: fix HiZ memory size computation and deciding when to use HiZ
[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_texture.h"
26 #include "r300_winsys.h"
27
28 #include "util/u_format.h"
29 #include "util/u_pack_color.h"
30
31 enum r300_blitter_op /* bitmask */
32 {
33 R300_CLEAR = 1,
34 R300_CLEAR_SURFACE = 2,
35 R300_COPY = 4
36 };
37
38 static void r300_blitter_begin(struct r300_context* r300, enum r300_blitter_op op)
39 {
40 if (r300->query_current) {
41 r300->blitter_saved_query = r300->query_current;
42 r300_stop_query(r300);
43 }
44
45 /* Yeah we have to save all those states to ensure the blitter operation
46 * is really transparent. The states will be restored by the blitter once
47 * copying is done. */
48 util_blitter_save_blend(r300->blitter, r300->blend_state.state);
49 util_blitter_save_depth_stencil_alpha(r300->blitter, r300->dsa_state.state);
50 util_blitter_save_stencil_ref(r300->blitter, &(r300->stencil_ref));
51 util_blitter_save_rasterizer(r300->blitter, r300->rs_state.state);
52 util_blitter_save_fragment_shader(r300->blitter, r300->fs.state);
53 util_blitter_save_vertex_shader(r300->blitter, r300->vs_state.state);
54 util_blitter_save_viewport(r300->blitter, &r300->viewport);
55 util_blitter_save_clip(r300->blitter, (struct pipe_clip_state*)r300->clip_state.state);
56 util_blitter_save_vertex_elements(r300->blitter, r300->velems);
57 util_blitter_save_vertex_buffers(r300->blitter, r300->vbuf_mgr->nr_vertex_buffers,
58 r300->vbuf_mgr->vertex_buffer);
59
60 if (op & (R300_CLEAR_SURFACE | R300_COPY)) {
61 util_blitter_save_framebuffer(r300->blitter, r300->fb_state.state);
62 }
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 boolean r300_fast_zclear_allowed(struct r300_context *r300)
112 {
113 struct pipe_framebuffer_state *fb =
114 (struct pipe_framebuffer_state*)r300->fb_state.state;
115
116 return r300_resource(fb->zsbuf->texture)->tex.zmask_dwords[fb->zsbuf->u.tex.level];
117 }
118
119 static boolean r300_hiz_clear_allowed(struct r300_context *r300)
120 {
121 struct pipe_framebuffer_state *fb =
122 (struct pipe_framebuffer_state*)r300->fb_state.state;
123
124 return r300_resource(fb->zsbuf->texture)->tex.hiz_dwords[fb->zsbuf->u.tex.level];
125 }
126
127 static uint32_t r300_depth_clear_value(enum pipe_format format,
128 double depth, unsigned stencil)
129 {
130 switch (format) {
131 case PIPE_FORMAT_Z16_UNORM:
132 case PIPE_FORMAT_X8Z24_UNORM:
133 return util_pack_z(format, depth);
134
135 case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
136 return util_pack_z_stencil(format, depth, stencil);
137
138 default:
139 assert(0);
140 return 0;
141 }
142 }
143
144 /* Clear currently bound buffers. */
145 static void r300_clear(struct pipe_context* pipe,
146 unsigned buffers,
147 const float* rgba,
148 double depth,
149 unsigned stencil)
150 {
151 /* My notes about Zbuffer compression:
152 *
153 * 1) The zbuffer must be micro-tiled and whole microtiles must be
154 * written if compression is enabled. If microtiling is disabled,
155 * it locks up.
156 *
157 * 2) There is ZMASK RAM which contains a compressed zbuffer.
158 * Each dword of the Z Mask contains compression information
159 * for 16 4x4 pixel tiles, that is 2 bits for each tile.
160 * On chips with 2 Z pipes, every other dword maps to a different
161 * pipe. On newer chipsets, there is a new compression mode
162 * with 8x8 pixel tiles per 2 bits.
163 *
164 * 3) The FASTFILL bit has nothing to do with filling. It only tells hw
165 * it should look in the ZMASK RAM first before fetching from a real
166 * zbuffer.
167 *
168 * 4) If a pixel is in a cleared state, ZB_DEPTHCLEARVALUE is returned
169 * during zbuffer reads instead of the value that is actually stored
170 * in the zbuffer memory. A pixel is in a cleared state when its ZMASK
171 * is equal to 0. Therefore, if you clear ZMASK with zeros, you may
172 * leave the zbuffer memory uninitialized, but then you must enable
173 * compression, so that the ZMASK RAM is actually used.
174 *
175 * 5) Each 4x4 (or 8x8) tile is automatically decompressed and recompressed
176 * during zbuffer updates. A special decompressing operation should be
177 * used to fully decompress a zbuffer, which basically just stores all
178 * compressed tiles in ZMASK to the zbuffer memory.
179 *
180 * 6) For a 16-bit zbuffer, compression causes a hung with one or
181 * two samples and should not be used.
182 *
183 * 7) FORCE_COMPRESSED_STENCIL_VALUE should be enabled for stencil clears
184 * to avoid needless decompression.
185 *
186 * 8) Fastfill must not be used if reading of compressed Z data is disabled
187 * and writing of compressed Z data is enabled (RD/WR_COMP_ENABLE),
188 * i.e. it cannot be used to compress the zbuffer.
189 *
190 * 9) ZB_CB_CLEAR does not interact with zbuffer compression in any way.
191 *
192 * - Marek
193 */
194
195 struct r300_context* r300 = r300_context(pipe);
196 struct pipe_framebuffer_state *fb =
197 (struct pipe_framebuffer_state*)r300->fb_state.state;
198 struct r300_hyperz_state *hyperz =
199 (struct r300_hyperz_state*)r300->hyperz_state.state;
200 uint32_t width = fb->width;
201 uint32_t height = fb->height;
202 boolean can_hyperz = r300->rws->get_value(r300->rws, R300_CAN_HYPERZ);
203 uint32_t hyperz_dcv = hyperz->zb_depthclearvalue;
204
205 /* Enable fast Z clear.
206 * The zbuffer must be in micro-tiled mode, otherwise it locks up. */
207 if ((buffers & PIPE_CLEAR_DEPTHSTENCIL) && can_hyperz) {
208 if (r300_fast_zclear_allowed(r300)) {
209 hyperz_dcv = hyperz->zb_depthclearvalue =
210 r300_depth_clear_value(fb->zsbuf->format, depth, stencil);
211
212 r300_mark_atom_dirty(r300, &r300->zmask_clear);
213 buffers &= ~PIPE_CLEAR_DEPTHSTENCIL;
214 }
215
216 if (r300_hiz_clear_allowed(r300)) {
217 r300_mark_atom_dirty(r300, &r300->hiz_clear);
218 }
219 }
220
221 /* Enable CBZB clear. */
222 if (r300_cbzb_clear_allowed(r300, buffers)) {
223 struct r300_surface *surf = r300_surface(fb->cbufs[0]);
224
225 hyperz->zb_depthclearvalue =
226 r300_depth_clear_cb_value(surf->base.format, rgba);
227
228 width = surf->cbzb_width;
229 height = surf->cbzb_height;
230
231 r300->cbzb_clear = TRUE;
232 r300_mark_fb_state_dirty(r300, R300_CHANGED_HYPERZ_FLAG);
233 }
234
235 /* Clear. */
236 if (buffers) {
237 /* Clear using the blitter. */
238 r300_blitter_begin(r300, R300_CLEAR);
239 util_blitter_clear(r300->blitter,
240 width,
241 height,
242 fb->nr_cbufs,
243 buffers, rgba, depth, stencil);
244 r300_blitter_end(r300);
245 } else if (r300->zmask_clear.dirty || r300->hiz_clear.dirty) {
246 /* Just clear zmask and hiz now, this does not use the standard draw
247 * procedure. */
248 unsigned dwords;
249
250 /* Calculate zmask_clear and hiz_clear atom sizes. */
251 r300_update_hyperz_state(r300);
252 dwords = (r300->zmask_clear.dirty ? r300->zmask_clear.size : 0) +
253 (r300->hiz_clear.dirty ? r300->hiz_clear.size : 0) +
254 r300_get_num_cs_end_dwords(r300);
255
256 /* Reserve CS space. */
257 if (dwords > (R300_MAX_CMDBUF_DWORDS - r300->cs->cdw)) {
258 r300->context.flush(&r300->context, 0, NULL);
259 }
260
261 /* Emit clear packets. */
262 if (r300->zmask_clear.dirty) {
263 r300_emit_zmask_clear(r300, r300->zmask_clear.size,
264 r300->zmask_clear.state);
265 r300->zmask_clear.dirty = FALSE;
266 }
267 if (r300->hiz_clear.dirty) {
268 r300_emit_hiz_clear(r300, r300->hiz_clear.size,
269 r300->hiz_clear.state);
270 r300->hiz_clear.dirty = FALSE;
271 }
272 } else {
273 assert(0);
274 }
275
276 /* Disable CBZB clear. */
277 if (r300->cbzb_clear) {
278 r300->cbzb_clear = FALSE;
279 hyperz->zb_depthclearvalue = hyperz_dcv;
280 r300_mark_fb_state_dirty(r300, R300_CHANGED_HYPERZ_FLAG);
281 }
282
283 /* Enable fastfill and/or hiz.
284 *
285 * If we cleared zmask/hiz, it's in use now. The Hyper-Z state update
286 * looks if zmask/hiz is in use and programs hardware accordingly. */
287 if (r300->zmask_in_use || r300->hiz_in_use) {
288 r300_mark_atom_dirty(r300, &r300->hyperz_state);
289 }
290 }
291
292 /* Clear a region of a color surface to a constant value. */
293 static void r300_clear_render_target(struct pipe_context *pipe,
294 struct pipe_surface *dst,
295 const float *rgba,
296 unsigned dstx, unsigned dsty,
297 unsigned width, unsigned height)
298 {
299 struct r300_context *r300 = r300_context(pipe);
300
301 r300->hyperz_locked = TRUE;
302 r300_mark_atom_dirty(r300, &r300->hyperz_state);
303
304 r300_blitter_begin(r300, R300_CLEAR_SURFACE);
305 util_blitter_clear_render_target(r300->blitter, dst, rgba,
306 dstx, dsty, width, height);
307 r300_blitter_end(r300);
308
309 r300->hyperz_locked = FALSE;
310 r300_mark_atom_dirty(r300, &r300->hyperz_state);
311 }
312
313 /* Clear a region of a depth stencil surface. */
314 static void r300_clear_depth_stencil(struct pipe_context *pipe,
315 struct pipe_surface *dst,
316 unsigned clear_flags,
317 double depth,
318 unsigned stencil,
319 unsigned dstx, unsigned dsty,
320 unsigned width, unsigned height)
321 {
322 struct r300_context *r300 = r300_context(pipe);
323 struct pipe_framebuffer_state *fb =
324 (struct pipe_framebuffer_state*)r300->fb_state.state;
325
326 if (r300->zmask_in_use && !r300->hyperz_locked) {
327 if (fb->zsbuf->texture == dst->texture) {
328 r300_decompress_zmask(r300);
329 } else {
330 r300->hyperz_locked = TRUE;
331 r300_mark_atom_dirty(r300, &r300->hyperz_state);
332 }
333 }
334
335 r300_blitter_begin(r300, R300_CLEAR_SURFACE);
336 util_blitter_clear_depth_stencil(r300->blitter, dst, clear_flags, depth, stencil,
337 dstx, dsty, width, height);
338 r300_blitter_end(r300);
339
340 if (r300->hyperz_locked) {
341 r300->hyperz_locked = FALSE;
342 r300_mark_atom_dirty(r300, &r300->hyperz_state);
343 }
344 }
345
346 void r300_decompress_zmask(struct r300_context *r300)
347 {
348 struct pipe_framebuffer_state *fb =
349 (struct pipe_framebuffer_state*)r300->fb_state.state;
350
351 if (!r300->zmask_in_use || r300->hyperz_locked)
352 return;
353
354 r300->zmask_decompress = TRUE;
355 r300_mark_atom_dirty(r300, &r300->hyperz_state);
356
357 r300_blitter_begin(r300, R300_CLEAR);
358 util_blitter_clear_depth_custom(r300->blitter, fb->width, fb->height, 0,
359 r300->dsa_decompress_zmask);
360 r300_blitter_end(r300);
361
362 r300->zmask_decompress = FALSE;
363 r300->zmask_in_use = FALSE;
364 r300_mark_atom_dirty(r300, &r300->hyperz_state);
365 }
366
367 void r300_decompress_zmask_locked_unsafe(struct r300_context *r300)
368 {
369 struct pipe_framebuffer_state fb = {0};
370 fb.width = r300->locked_zbuffer->width;
371 fb.height = r300->locked_zbuffer->height;
372 fb.nr_cbufs = 0;
373 fb.zsbuf = r300->locked_zbuffer;
374
375 r300->context.set_framebuffer_state(&r300->context, &fb);
376 r300_decompress_zmask(r300);
377 }
378
379 void r300_decompress_zmask_locked(struct r300_context *r300)
380 {
381 struct pipe_framebuffer_state saved_fb = {0};
382
383 util_copy_framebuffer_state(&saved_fb, r300->fb_state.state);
384 r300_decompress_zmask_locked_unsafe(r300);
385 r300->context.set_framebuffer_state(&r300->context, &saved_fb);
386 util_unreference_framebuffer_state(&saved_fb);
387 }
388
389 /* Copy a block of pixels from one surface to another using HW. */
390 static void r300_hw_copy_region(struct pipe_context* pipe,
391 struct pipe_resource *dst,
392 unsigned dst_level,
393 unsigned dstx, unsigned dsty, unsigned dstz,
394 struct pipe_resource *src,
395 unsigned src_level,
396 const struct pipe_box *src_box)
397 {
398 struct r300_context* r300 = r300_context(pipe);
399
400 r300_blitter_begin(r300, R300_COPY);
401 util_blitter_copy_region(r300->blitter, dst, dst_level, dstx, dsty, dstz,
402 src, src_level, src_box, TRUE);
403 r300_blitter_end(r300);
404 }
405
406 /* Copy a block of pixels from one surface to another. */
407 static void r300_resource_copy_region(struct pipe_context *pipe,
408 struct pipe_resource *dst,
409 unsigned dst_level,
410 unsigned dstx, unsigned dsty, unsigned dstz,
411 struct pipe_resource *src,
412 unsigned src_level,
413 const struct pipe_box *src_box)
414 {
415 struct r300_context *r300 = r300_context(pipe);
416 struct pipe_framebuffer_state *fb =
417 (struct pipe_framebuffer_state*)r300->fb_state.state;
418 struct pipe_resource old_src = *src;
419 struct pipe_resource old_dst = *dst;
420 struct pipe_resource new_src = old_src;
421 struct pipe_resource new_dst = old_dst;
422 const struct util_format_description *desc =
423 util_format_description(dst->format);
424 struct pipe_box box;
425
426 if (r300->zmask_in_use && !r300->hyperz_locked) {
427 if (fb->zsbuf->texture == src ||
428 fb->zsbuf->texture == dst) {
429 r300_decompress_zmask(r300);
430 } else {
431 r300->hyperz_locked = TRUE;
432 r300_mark_atom_dirty(r300, &r300->hyperz_state);
433 }
434 }
435
436 /* Handle non-renderable plain formats. */
437 if (desc->layout == UTIL_FORMAT_LAYOUT_PLAIN &&
438 (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB ||
439 !pipe->screen->is_format_supported(pipe->screen,
440 src->format, src->target,
441 src->nr_samples,
442 PIPE_BIND_SAMPLER_VIEW, 0) ||
443 !pipe->screen->is_format_supported(pipe->screen,
444 dst->format, dst->target,
445 dst->nr_samples,
446 PIPE_BIND_RENDER_TARGET, 0))) {
447 switch (util_format_get_blocksize(old_dst.format)) {
448 case 1:
449 new_dst.format = PIPE_FORMAT_I8_UNORM;
450 break;
451 case 2:
452 new_dst.format = PIPE_FORMAT_B4G4R4A4_UNORM;
453 break;
454 case 4:
455 new_dst.format = PIPE_FORMAT_B8G8R8A8_UNORM;
456 break;
457 case 8:
458 new_dst.format = PIPE_FORMAT_R16G16B16A16_UNORM;
459 break;
460 default:
461 debug_printf("r300: surface_copy: Unhandled format: %s. Falling back to software.\n"
462 "r300: surface_copy: Software fallback doesn't work for tiled textures.\n",
463 util_format_short_name(dst->format));
464 }
465 new_src.format = new_dst.format;
466 }
467
468 /* Handle compressed formats. */
469 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
470 switch (util_format_get_blocksize(old_dst.format)) {
471 case 8:
472 /* 1 pixel = 4 bits,
473 * we set 1 pixel = 2 bytes ===> 4 times larger pixels. */
474 new_dst.format = PIPE_FORMAT_B4G4R4A4_UNORM;
475 break;
476 case 16:
477 /* 1 pixel = 8 bits,
478 * we set 1 pixel = 4 bytes ===> 4 times larger pixels. */
479 new_dst.format = PIPE_FORMAT_B8G8R8A8_UNORM;
480 break;
481 }
482
483 /* Since the pixels are 4 times larger, we must decrease
484 * the image size and the coordinates 4 times. */
485 new_src.format = new_dst.format;
486 new_dst.height0 = (new_dst.height0 + 3) / 4;
487 new_src.height0 = (new_src.height0 + 3) / 4;
488 dsty /= 4;
489 box = *src_box;
490 box.y /= 4;
491 box.height = (box.height + 3) / 4;
492 src_box = &box;
493 }
494
495 if (old_src.format != new_src.format)
496 r300_resource_set_properties(pipe->screen, src, 0, &new_src);
497 if (old_dst.format != new_dst.format)
498 r300_resource_set_properties(pipe->screen, dst, 0, &new_dst);
499
500 r300_hw_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,
501 src, src_level, src_box);
502
503 if (old_src.format != new_src.format)
504 r300_resource_set_properties(pipe->screen, src, 0, &old_src);
505 if (old_dst.format != new_dst.format)
506 r300_resource_set_properties(pipe->screen, dst, 0, &old_dst);
507
508 if (r300->hyperz_locked) {
509 r300->hyperz_locked = FALSE;
510 r300_mark_atom_dirty(r300, &r300->hyperz_state);
511 }
512 }
513
514 void r300_init_blit_functions(struct r300_context *r300)
515 {
516 r300->context.clear = r300_clear;
517 r300->context.clear_render_target = r300_clear_render_target;
518 r300->context.clear_depth_stencil = r300_clear_depth_stencil;
519 r300->context.resource_copy_region = r300_resource_copy_region;
520 }