Merge remote-tracking branch 'origin/master' into pipe-video
[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
27 #include "util/u_format.h"
28 #include "util/u_pack_color.h"
29 #include "util/u_surface.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] != 0;
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] != 0;
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 static uint32_t r300_hiz_clear_value(double depth)
145 {
146 uint32_t r = (uint32_t)(CLAMP(depth, 0, 1) * 255.5);
147 assert(r <= 255);
148 return r | (r << 8) | (r << 16) | (r << 24);
149 }
150
151 /* Clear currently bound buffers. */
152 static void r300_clear(struct pipe_context* pipe,
153 unsigned buffers,
154 const float* rgba,
155 double depth,
156 unsigned stencil)
157 {
158 /* My notes about Zbuffer compression:
159 *
160 * 1) The zbuffer must be micro-tiled and whole microtiles must be
161 * written if compression is enabled. If microtiling is disabled,
162 * it locks up.
163 *
164 * 2) There is ZMASK RAM which contains a compressed zbuffer.
165 * Each dword of the Z Mask contains compression information
166 * for 16 4x4 pixel tiles, that is 2 bits for each tile.
167 * On chips with 2 Z pipes, every other dword maps to a different
168 * pipe. On newer chipsets, there is a new compression mode
169 * with 8x8 pixel tiles per 2 bits.
170 *
171 * 3) The FASTFILL bit has nothing to do with filling. It only tells hw
172 * it should look in the ZMASK RAM first before fetching from a real
173 * zbuffer.
174 *
175 * 4) If a pixel is in a cleared state, ZB_DEPTHCLEARVALUE is returned
176 * during zbuffer reads instead of the value that is actually stored
177 * in the zbuffer memory. A pixel is in a cleared state when its ZMASK
178 * is equal to 0. Therefore, if you clear ZMASK with zeros, you may
179 * leave the zbuffer memory uninitialized, but then you must enable
180 * compression, so that the ZMASK RAM is actually used.
181 *
182 * 5) Each 4x4 (or 8x8) tile is automatically decompressed and recompressed
183 * during zbuffer updates. A special decompressing operation should be
184 * used to fully decompress a zbuffer, which basically just stores all
185 * compressed tiles in ZMASK to the zbuffer memory.
186 *
187 * 6) For a 16-bit zbuffer, compression causes a hung with one or
188 * two samples and should not be used.
189 *
190 * 7) FORCE_COMPRESSED_STENCIL_VALUE should be enabled for stencil clears
191 * to avoid needless decompression.
192 *
193 * 8) Fastfill must not be used if reading of compressed Z data is disabled
194 * and writing of compressed Z data is enabled (RD/WR_COMP_ENABLE),
195 * i.e. it cannot be used to compress the zbuffer.
196 *
197 * 9) ZB_CB_CLEAR does not interact with zbuffer compression in any way.
198 *
199 * - Marek
200 */
201
202 struct r300_context* r300 = r300_context(pipe);
203 struct pipe_framebuffer_state *fb =
204 (struct pipe_framebuffer_state*)r300->fb_state.state;
205 struct r300_hyperz_state *hyperz =
206 (struct r300_hyperz_state*)r300->hyperz_state.state;
207 uint32_t width = fb->width;
208 uint32_t height = fb->height;
209 uint32_t hyperz_dcv = hyperz->zb_depthclearvalue;
210
211 /* Enable fast Z clear.
212 * The zbuffer must be in micro-tiled mode, otherwise it locks up. */
213 if (buffers & PIPE_CLEAR_DEPTHSTENCIL) {
214 boolean zmask_clear, hiz_clear;
215
216 zmask_clear = r300_fast_zclear_allowed(r300);
217 hiz_clear = r300_hiz_clear_allowed(r300);
218
219 /* If we need Hyper-Z. */
220 if (zmask_clear || hiz_clear) {
221 r300->num_z_clears++;
222
223 /* Try to obtain the access to Hyper-Z buffers if we don't have one. */
224 if (!r300->hyperz_enabled) {
225 r300->hyperz_enabled =
226 r300->rws->cs_request_feature(r300->cs,
227 RADEON_FID_HYPERZ_RAM_ACCESS,
228 TRUE);
229 if (r300->hyperz_enabled) {
230 /* Need to emit HyperZ buffer regs for the first time. */
231 r300_mark_fb_state_dirty(r300, R300_CHANGED_HYPERZ_FLAG);
232 }
233 }
234
235 /* Setup Hyper-Z clears. */
236 if (r300->hyperz_enabled) {
237 if (zmask_clear) {
238 hyperz_dcv = hyperz->zb_depthclearvalue =
239 r300_depth_clear_value(fb->zsbuf->format, depth, stencil);
240
241 r300_mark_atom_dirty(r300, &r300->zmask_clear);
242 buffers &= ~PIPE_CLEAR_DEPTHSTENCIL;
243 }
244
245 if (hiz_clear) {
246 r300->hiz_clear_value = r300_hiz_clear_value(depth);
247 r300_mark_atom_dirty(r300, &r300->hiz_clear);
248 }
249 }
250 }
251 }
252
253 /* Enable CBZB clear. */
254 if (r300_cbzb_clear_allowed(r300, buffers)) {
255 struct r300_surface *surf = r300_surface(fb->cbufs[0]);
256
257 hyperz->zb_depthclearvalue =
258 r300_depth_clear_cb_value(surf->base.format, rgba);
259
260 width = surf->cbzb_width;
261 height = surf->cbzb_height;
262
263 r300->cbzb_clear = TRUE;
264 r300_mark_fb_state_dirty(r300, R300_CHANGED_HYPERZ_FLAG);
265 }
266
267 /* Clear. */
268 if (buffers) {
269 /* Clear using the blitter. */
270 r300_blitter_begin(r300, R300_CLEAR);
271 util_blitter_clear(r300->blitter,
272 width,
273 height,
274 fb->nr_cbufs,
275 buffers, rgba, depth, stencil);
276 r300_blitter_end(r300);
277 } else if (r300->zmask_clear.dirty || r300->hiz_clear.dirty) {
278 /* Just clear zmask and hiz now, this does not use the standard draw
279 * procedure. */
280 /* Calculate zmask_clear and hiz_clear atom sizes. */
281 unsigned dwords =
282 (r300->zmask_clear.dirty ? r300->zmask_clear.size : 0) +
283 (r300->hiz_clear.dirty ? r300->hiz_clear.size : 0) +
284 r300_get_num_cs_end_dwords(r300);
285
286 /* Reserve CS space. */
287 if (dwords > (RADEON_MAX_CMDBUF_DWORDS - r300->cs->cdw)) {
288 r300_flush(&r300->context, RADEON_FLUSH_ASYNC, NULL);
289 }
290
291 /* Emit clear packets. */
292 if (r300->zmask_clear.dirty) {
293 r300_emit_zmask_clear(r300, r300->zmask_clear.size,
294 r300->zmask_clear.state);
295 r300->zmask_clear.dirty = FALSE;
296 }
297 if (r300->hiz_clear.dirty) {
298 r300_emit_hiz_clear(r300, r300->hiz_clear.size,
299 r300->hiz_clear.state);
300 r300->hiz_clear.dirty = FALSE;
301 }
302 } else {
303 assert(0);
304 }
305
306 /* Disable CBZB clear. */
307 if (r300->cbzb_clear) {
308 r300->cbzb_clear = FALSE;
309 hyperz->zb_depthclearvalue = hyperz_dcv;
310 r300_mark_fb_state_dirty(r300, R300_CHANGED_HYPERZ_FLAG);
311 }
312
313 /* Enable fastfill and/or hiz.
314 *
315 * If we cleared zmask/hiz, it's in use now. The Hyper-Z state update
316 * looks if zmask/hiz is in use and programs hardware accordingly. */
317 if (r300->zmask_in_use || r300->hiz_in_use) {
318 r300_mark_atom_dirty(r300, &r300->hyperz_state);
319 }
320 }
321
322 /* Clear a region of a color surface to a constant value. */
323 static void r300_clear_render_target(struct pipe_context *pipe,
324 struct pipe_surface *dst,
325 const float *rgba,
326 unsigned dstx, unsigned dsty,
327 unsigned width, unsigned height)
328 {
329 struct r300_context *r300 = r300_context(pipe);
330
331 r300_blitter_begin(r300, R300_CLEAR_SURFACE);
332 util_blitter_clear_render_target(r300->blitter, dst, rgba,
333 dstx, dsty, width, height);
334 r300_blitter_end(r300);
335 }
336
337 /* Clear a region of a depth stencil surface. */
338 static void r300_clear_depth_stencil(struct pipe_context *pipe,
339 struct pipe_surface *dst,
340 unsigned clear_flags,
341 double depth,
342 unsigned stencil,
343 unsigned dstx, unsigned dsty,
344 unsigned width, unsigned height)
345 {
346 struct r300_context *r300 = r300_context(pipe);
347 struct pipe_framebuffer_state *fb =
348 (struct pipe_framebuffer_state*)r300->fb_state.state;
349
350 if (r300->zmask_in_use && !r300->locked_zbuffer) {
351 if (fb->zsbuf->texture == dst->texture) {
352 r300_decompress_zmask(r300);
353 }
354 }
355
356 /* XXX Do not decompress ZMask of the currently-set zbuffer. */
357 r300_blitter_begin(r300, R300_CLEAR_SURFACE);
358 util_blitter_clear_depth_stencil(r300->blitter, dst, clear_flags, depth, stencil,
359 dstx, dsty, width, height);
360 r300_blitter_end(r300);
361 }
362
363 void r300_decompress_zmask(struct r300_context *r300)
364 {
365 struct pipe_framebuffer_state *fb =
366 (struct pipe_framebuffer_state*)r300->fb_state.state;
367
368 if (!r300->zmask_in_use || r300->locked_zbuffer)
369 return;
370
371 r300->zmask_decompress = TRUE;
372 r300_mark_atom_dirty(r300, &r300->hyperz_state);
373
374 r300_blitter_begin(r300, R300_CLEAR);
375 util_blitter_clear_depth_custom(r300->blitter, fb->width, fb->height, 0,
376 r300->dsa_decompress_zmask);
377 r300_blitter_end(r300);
378
379 r300->zmask_decompress = FALSE;
380 r300->zmask_in_use = FALSE;
381 r300_mark_atom_dirty(r300, &r300->hyperz_state);
382 }
383
384 void r300_decompress_zmask_locked_unsafe(struct r300_context *r300)
385 {
386 struct pipe_framebuffer_state fb = {0};
387 fb.width = r300->locked_zbuffer->width;
388 fb.height = r300->locked_zbuffer->height;
389 fb.nr_cbufs = 0;
390 fb.zsbuf = r300->locked_zbuffer;
391
392 r300->context.set_framebuffer_state(&r300->context, &fb);
393 r300_decompress_zmask(r300);
394 }
395
396 void r300_decompress_zmask_locked(struct r300_context *r300)
397 {
398 struct pipe_framebuffer_state saved_fb = {0};
399
400 util_copy_framebuffer_state(&saved_fb, r300->fb_state.state);
401 r300_decompress_zmask_locked_unsafe(r300);
402 r300->context.set_framebuffer_state(&r300->context, &saved_fb);
403 util_unreference_framebuffer_state(&saved_fb);
404
405 pipe_surface_reference(&r300->locked_zbuffer, NULL);
406 }
407
408 /* Copy a block of pixels from one surface to another using HW. */
409 static void r300_hw_copy_region(struct pipe_context* pipe,
410 struct pipe_resource *dst,
411 unsigned dst_level,
412 unsigned dstx, unsigned dsty, unsigned dstz,
413 struct pipe_resource *src,
414 unsigned src_level,
415 const struct pipe_box *src_box)
416 {
417 struct r300_context* r300 = r300_context(pipe);
418
419 r300_blitter_begin(r300, R300_COPY);
420 util_blitter_copy_region(r300->blitter, dst, dst_level, dstx, dsty, dstz,
421 src, src_level, src_box, TRUE);
422 r300_blitter_end(r300);
423 }
424
425 /* Copy a block of pixels from one surface to another. */
426 static void r300_resource_copy_region(struct pipe_context *pipe,
427 struct pipe_resource *dst,
428 unsigned dst_level,
429 unsigned dstx, unsigned dsty, unsigned dstz,
430 struct pipe_resource *src,
431 unsigned src_level,
432 const struct pipe_box *src_box)
433 {
434 struct r300_context *r300 = r300_context(pipe);
435 struct pipe_framebuffer_state *fb =
436 (struct pipe_framebuffer_state*)r300->fb_state.state;
437 struct pipe_resource old_src = *src;
438 struct pipe_resource old_dst = *dst;
439 struct pipe_resource new_src = old_src;
440 struct pipe_resource new_dst = old_dst;
441 const struct util_format_description *desc =
442 util_format_description(dst->format);
443 struct pipe_box box;
444
445 /* Fallback for buffers. */
446 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
447 util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,
448 src, src_level, src_box);
449 return;
450 }
451
452 if (r300->zmask_in_use && !r300->locked_zbuffer) {
453 if (fb->zsbuf->texture == src ||
454 fb->zsbuf->texture == dst) {
455 r300_decompress_zmask(r300);
456 }
457 }
458
459 /* Handle non-renderable plain formats. */
460 if (desc->layout == UTIL_FORMAT_LAYOUT_PLAIN &&
461 (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB ||
462 !pipe->screen->is_format_supported(pipe->screen,
463 src->format, src->target,
464 src->nr_samples,
465 PIPE_BIND_SAMPLER_VIEW) ||
466 !pipe->screen->is_format_supported(pipe->screen,
467 dst->format, dst->target,
468 dst->nr_samples,
469 PIPE_BIND_RENDER_TARGET))) {
470 switch (util_format_get_blocksize(old_dst.format)) {
471 case 1:
472 new_dst.format = PIPE_FORMAT_I8_UNORM;
473 break;
474 case 2:
475 new_dst.format = PIPE_FORMAT_B4G4R4A4_UNORM;
476 break;
477 case 4:
478 new_dst.format = PIPE_FORMAT_B8G8R8A8_UNORM;
479 break;
480 case 8:
481 new_dst.format = PIPE_FORMAT_R16G16B16A16_UNORM;
482 break;
483 default:
484 debug_printf("r300: surface_copy: Unhandled format: %s. Falling back to software.\n"
485 "r300: surface_copy: Software fallback doesn't work for tiled textures.\n",
486 util_format_short_name(dst->format));
487 }
488 new_src.format = new_dst.format;
489 }
490
491 /* Handle compressed formats. */
492 if (desc->layout == UTIL_FORMAT_LAYOUT_S3TC ||
493 desc->layout == UTIL_FORMAT_LAYOUT_RGTC) {
494 switch (util_format_get_blocksize(old_dst.format)) {
495 case 8:
496 /* 1 pixel = 4 bits,
497 * we set 1 pixel = 2 bytes ===> 4 times larger pixels. */
498 new_dst.format = PIPE_FORMAT_B4G4R4A4_UNORM;
499 break;
500 case 16:
501 /* 1 pixel = 8 bits,
502 * we set 1 pixel = 4 bytes ===> 4 times larger pixels. */
503 new_dst.format = PIPE_FORMAT_B8G8R8A8_UNORM;
504 break;
505 }
506
507 /* Since the pixels are 4 times larger, we must decrease
508 * the image size and the coordinates 4 times. */
509 new_src.format = new_dst.format;
510 new_dst.height0 = (new_dst.height0 + 3) / 4;
511 new_src.height0 = (new_src.height0 + 3) / 4;
512 dsty /= 4;
513 box = *src_box;
514 box.y /= 4;
515 box.height = (box.height + 3) / 4;
516 src_box = &box;
517 }
518
519 if (old_src.format != new_src.format)
520 r300_resource_set_properties(pipe->screen, src, 0, &new_src);
521 if (old_dst.format != new_dst.format)
522 r300_resource_set_properties(pipe->screen, dst, 0, &new_dst);
523
524 r300_hw_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,
525 src, src_level, src_box);
526
527 if (old_src.format != new_src.format)
528 r300_resource_set_properties(pipe->screen, src, 0, &old_src);
529 if (old_dst.format != new_dst.format)
530 r300_resource_set_properties(pipe->screen, dst, 0, &old_dst);
531 }
532
533 void r300_init_blit_functions(struct r300_context *r300)
534 {
535 r300->context.clear = r300_clear;
536 r300->context.clear_render_target = r300_clear_render_target;
537 r300->context.clear_depth_stencil = r300_clear_depth_stencil;
538 r300->context.resource_copy_region = r300_resource_copy_region;
539 }