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