1 /*
2 * Copyright 2009 Marek Olšák <[email protected]>
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "r300_context.h"
7 #include "r300_emit.h"
8 #include "r300_texture.h"
9 #include "r300_reg.h"
10
11 #include "util/format/u_format.h"
12 #include "util/half_float.h"
13 #include "util/u_pack_color.h"
14 #include "util/u_surface.h"
15
16 enum r300_blitter_op /* bitmask */
17 {
18 R300_STOP_QUERY = 1,
19 R300_SAVE_TEXTURES = 2,
20 R300_SAVE_FRAMEBUFFER = 4,
21 R300_IGNORE_RENDER_COND = 8,
22
23 R300_CLEAR = R300_STOP_QUERY,
24
25 R300_CLEAR_SURFACE = R300_STOP_QUERY | R300_SAVE_FRAMEBUFFER,
26
27 R300_COPY = R300_STOP_QUERY | R300_SAVE_FRAMEBUFFER |
28 R300_SAVE_TEXTURES | R300_IGNORE_RENDER_COND,
29
30 R300_BLIT = R300_STOP_QUERY | R300_SAVE_FRAMEBUFFER |
31 R300_SAVE_TEXTURES,
32
33 R300_DECOMPRESS = R300_STOP_QUERY | R300_IGNORE_RENDER_COND,
34 };
35
r300_blitter_begin(struct r300_context * r300,enum r300_blitter_op op)36 static void r300_blitter_begin(struct r300_context* r300, enum r300_blitter_op op)
37 {
38 if ((op & R300_STOP_QUERY) && r300->query_current) {
39 r300->blitter_saved_query = r300->query_current;
40 r300_stop_query(r300);
41 }
42
43 /* Yeah we have to save all those states to ensure the blitter operation
44 * is really transparent. The states will be restored by the blitter once
45 * copying is done. */
46 util_blitter_save_blend(r300->blitter, r300->blend_state.state);
47 util_blitter_save_depth_stencil_alpha(r300->blitter, r300->dsa_state.state);
48 util_blitter_save_stencil_ref(r300->blitter, &(r300->stencil_ref));
49 util_blitter_save_rasterizer(r300->blitter, r300->rs_state.state);
50 util_blitter_save_fragment_shader(r300->blitter, r300->fs.state);
51 util_blitter_save_vertex_shader(r300->blitter, r300->vs_state.state);
52 util_blitter_save_viewport(r300->blitter, &r300->viewport);
53 util_blitter_save_scissor(r300->blitter, r300->scissor_state.state);
54 util_blitter_save_sample_mask(r300->blitter, *(unsigned*)r300->sample_mask.state, 0);
55 util_blitter_save_vertex_buffers(r300->blitter, r300->vertex_buffer,
56 r300->nr_vertex_buffers);
57 util_blitter_save_vertex_elements(r300->blitter, r300->velems);
58
59 struct pipe_constant_buffer cb = {
60 /* r300 doesn't use the size for FS at all. The shader determines it.
61 * Set something for blitter.
62 */
63 .buffer_size = 4,
64 .user_buffer = ((struct r300_constant_buffer*)r300->fs_constants.state)->ptr,
65 };
66 util_blitter_save_fragment_constant_buffer_slot(r300->blitter, &cb);
67
68 if (op & R300_SAVE_FRAMEBUFFER) {
69 util_blitter_save_framebuffer(r300->blitter, r300->fb_state.state);
70 }
71
72 if (op & R300_SAVE_TEXTURES) {
73 struct r300_textures_state* state =
74 (struct r300_textures_state*)r300->textures_state.state;
75
76 util_blitter_save_fragment_sampler_states(
77 r300->blitter, state->sampler_state_count,
78 (void**)state->sampler_states);
79
80 util_blitter_save_fragment_sampler_views(
81 r300->blitter, state->sampler_view_count,
82 (struct pipe_sampler_view**)state->sampler_views);
83 }
84
85 if (op & R300_IGNORE_RENDER_COND) {
86 /* Save the flag. */
87 r300->blitter_saved_skip_rendering = r300->skip_rendering+1;
88 r300->skip_rendering = false;
89 } else {
90 r300->blitter_saved_skip_rendering = 0;
91 }
92 }
93
r300_blitter_end(struct r300_context * r300)94 static void r300_blitter_end(struct r300_context *r300)
95 {
96 if (r300->blitter_saved_query) {
97 r300_resume_query(r300, r300->blitter_saved_query);
98 r300->blitter_saved_query = NULL;
99 }
100
101 if (r300->blitter_saved_skip_rendering) {
102 /* Restore the flag. */
103 r300->skip_rendering = r300->blitter_saved_skip_rendering-1;
104 }
105 }
106
r300_depth_clear_cb_value(enum pipe_format format,const float * rgba)107 static uint32_t r300_depth_clear_cb_value(enum pipe_format format,
108 const float* rgba)
109 {
110 union util_color uc;
111 util_pack_color(rgba, format, &uc);
112
113 if (util_format_get_blocksizebits(format) == 32)
114 return uc.ui[0];
115 else
116 return uc.us | (uc.us << 16);
117 }
118
r300_cbzb_clear_allowed(struct r300_context * r300,unsigned clear_buffers)119 static bool r300_cbzb_clear_allowed(struct r300_context *r300,
120 unsigned clear_buffers)
121 {
122 struct pipe_framebuffer_state *fb =
123 (struct pipe_framebuffer_state*)r300->fb_state.state;
124
125 /* Only color clear allowed, and only one colorbuffer. */
126 if ((clear_buffers & ~PIPE_CLEAR_COLOR) != 0 || fb->nr_cbufs != 1 || !fb->cbufs[0])
127 return false;
128
129 return r300_surface(fb->cbufs[0])->cbzb_allowed;
130 }
131
r300_fast_zclear_allowed(struct r300_context * r300,unsigned clear_buffers)132 static bool r300_fast_zclear_allowed(struct r300_context *r300,
133 unsigned clear_buffers)
134 {
135 struct pipe_framebuffer_state *fb =
136 (struct pipe_framebuffer_state*)r300->fb_state.state;
137
138 return r300_resource(fb->zsbuf->texture)->tex.zmask_dwords[fb->zsbuf->u.tex.level] != 0;
139 }
140
r300_hiz_clear_allowed(struct r300_context * r300)141 static bool r300_hiz_clear_allowed(struct r300_context *r300)
142 {
143 struct pipe_framebuffer_state *fb =
144 (struct pipe_framebuffer_state*)r300->fb_state.state;
145
146 return r300_resource(fb->zsbuf->texture)->tex.hiz_dwords[fb->zsbuf->u.tex.level] != 0;
147 }
148
r300_depth_clear_value(enum pipe_format format,double depth,unsigned stencil)149 static uint32_t r300_depth_clear_value(enum pipe_format format,
150 double depth, unsigned stencil)
151 {
152 switch (format) {
153 case PIPE_FORMAT_Z16_UNORM:
154 case PIPE_FORMAT_X8Z24_UNORM:
155 return util_pack_z(format, depth);
156
157 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
158 return util_pack_z_stencil(format, depth, stencil);
159
160 default:
161 assert(0);
162 return 0;
163 }
164 }
165
r300_hiz_clear_value(double depth)166 static uint32_t r300_hiz_clear_value(double depth)
167 {
168 uint32_t r = (uint32_t)(CLAMP(depth, 0, 1) * 255.5);
169 assert(r <= 255);
170 return r | (r << 8) | (r << 16) | (r << 24);
171 }
172
r300_set_clear_color(struct r300_context * r300,const union pipe_color_union * color)173 static void r300_set_clear_color(struct r300_context *r300,
174 const union pipe_color_union *color)
175 {
176 struct pipe_framebuffer_state *fb =
177 (struct pipe_framebuffer_state*)r300->fb_state.state;
178 union util_color uc;
179
180 memset(&uc, 0, sizeof(uc));
181 util_pack_color(color->f, fb->cbufs[0]->format, &uc);
182
183 if (fb->cbufs[0]->format == PIPE_FORMAT_R16G16B16A16_FLOAT ||
184 fb->cbufs[0]->format == PIPE_FORMAT_R16G16B16X16_FLOAT) {
185 /* (0,1,2,3) maps to (B,G,R,A) */
186 r300->color_clear_value_gb = uc.h[0] | ((uint32_t)uc.h[1] << 16);
187 r300->color_clear_value_ar = uc.h[2] | ((uint32_t)uc.h[3] << 16);
188 } else {
189 r300->color_clear_value = uc.ui[0];
190 }
191 }
192
193 DEBUG_GET_ONCE_BOOL_OPTION(hyperz, "RADEON_HYPERZ", false)
194
195 /* Clear currently bound buffers. */
r300_clear(struct pipe_context * pipe,unsigned buffers,const struct pipe_scissor_state * scissor_state,const union pipe_color_union * color,double depth,unsigned stencil)196 static void r300_clear(struct pipe_context* pipe,
197 unsigned buffers,
198 const struct pipe_scissor_state *scissor_state,
199 const union pipe_color_union *color,
200 double depth,
201 unsigned stencil)
202 {
203 /* My notes about Zbuffer compression:
204 *
205 * 1) The zbuffer must be micro-tiled and whole microtiles must be
206 * written if compression is enabled. If microtiling is disabled,
207 * it locks up.
208 *
209 * 2) There is ZMASK RAM which contains a compressed zbuffer.
210 * Each dword of the Z Mask contains compression information
211 * for 16 4x4 pixel tiles, that is 2 bits for each tile.
212 * On chips with 2 Z pipes, every other dword maps to a different
213 * pipe. On newer chipsets, there is a new compression mode
214 * with 8x8 pixel tiles per 2 bits.
215 *
216 * 3) The FASTFILL bit has nothing to do with filling. It only tells hw
217 * it should look in the ZMASK RAM first before fetching from a real
218 * zbuffer.
219 *
220 * 4) If a pixel is in a cleared state, ZB_DEPTHCLEARVALUE is returned
221 * during zbuffer reads instead of the value that is actually stored
222 * in the zbuffer memory. A pixel is in a cleared state when its ZMASK
223 * is equal to 0. Therefore, if you clear ZMASK with zeros, you may
224 * leave the zbuffer memory uninitialized, but then you must enable
225 * compression, so that the ZMASK RAM is actually used.
226 *
227 * 5) Each 4x4 (or 8x8) tile is automatically decompressed and recompressed
228 * during zbuffer updates. A special decompressing operation should be
229 * used to fully decompress a zbuffer, which basically just stores all
230 * compressed tiles in ZMASK to the zbuffer memory.
231 *
232 * 6) For a 16-bit zbuffer, compression causes a hung with one or
233 * two samples and should not be used.
234 *
235 * 7) FORCE_COMPRESSED_STENCIL_VALUE should be enabled for stencil clears
236 * to avoid needless decompression.
237 *
238 * 8) Fastfill must not be used if reading of compressed Z data is disabled
239 * and writing of compressed Z data is enabled (RD/WR_COMP_ENABLE),
240 * i.e. it cannot be used to compress the zbuffer.
241 *
242 * 9) ZB_CB_CLEAR does not interact with zbuffer compression in any way.
243 *
244 * - Marek
245 */
246
247 struct r300_context* r300 = r300_context(pipe);
248 struct pipe_framebuffer_state *fb =
249 (struct pipe_framebuffer_state*)r300->fb_state.state;
250 struct r300_hyperz_state *hyperz =
251 (struct r300_hyperz_state*)r300->hyperz_state.state;
252 uint32_t width = fb->width;
253 uint32_t height = fb->height;
254 uint32_t hyperz_dcv = hyperz->zb_depthclearvalue;
255
256 /* Use fast Z clear.
257 * The zbuffer must be in micro-tiled mode, otherwise it locks up. */
258 if (buffers & PIPE_CLEAR_DEPTHSTENCIL) {
259 bool zmask_clear, hiz_clear;
260
261 /* If both depth and stencil are present, they must be cleared together. */
262 if (fb->zsbuf->texture->format == PIPE_FORMAT_S8_UINT_Z24_UNORM &&
263 (buffers & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) {
264 zmask_clear = false;
265 hiz_clear = false;
266 } else {
267 zmask_clear = r300_fast_zclear_allowed(r300, buffers);
268 hiz_clear = r300_hiz_clear_allowed(r300);
269 }
270
271 /* If we need Hyper-Z. */
272 if (zmask_clear || hiz_clear) {
273 /* Try to obtain the access to Hyper-Z buffers if we don't have one. */
274 if (!r300->hyperz_enabled &&
275 (r300->screen->caps.is_r500 || debug_get_option_hyperz())) {
276 r300->hyperz_enabled =
277 r300->rws->cs_request_feature(&r300->cs,
278 RADEON_FID_R300_HYPERZ_ACCESS,
279 true);
280 if (r300->hyperz_enabled) {
281 /* Need to emit HyperZ buffer regs for the first time. */
282 r300_mark_fb_state_dirty(r300, R300_CHANGED_HYPERZ_FLAG);
283 }
284 }
285
286 /* Setup Hyper-Z clears. */
287 if (r300->hyperz_enabled) {
288 if (zmask_clear) {
289 hyperz_dcv = hyperz->zb_depthclearvalue =
290 r300_depth_clear_value(fb->zsbuf->format, depth, stencil);
291
292 r300_mark_atom_dirty(r300, &r300->zmask_clear);
293 r300_mark_atom_dirty(r300, &r300->gpu_flush);
294 buffers &= ~PIPE_CLEAR_DEPTHSTENCIL;
295 }
296
297 if (hiz_clear) {
298 r300->hiz_clear_value = r300_hiz_clear_value(depth);
299 r300_mark_atom_dirty(r300, &r300->hiz_clear);
300 r300_mark_atom_dirty(r300, &r300->gpu_flush);
301 }
302 r300->num_z_clears++;
303 }
304 }
305 }
306
307 /* Use fast color clear for an AA colorbuffer.
308 * The CMASK is shared between all colorbuffers, so we use it
309 * if there is only one colorbuffer bound. */
310 if ((buffers & PIPE_CLEAR_COLOR) && fb->nr_cbufs == 1 && fb->cbufs[0] &&
311 r300_resource(fb->cbufs[0]->texture)->tex.cmask_dwords) {
312 /* Try to obtain the access to the CMASK if we don't have one. */
313 if (!r300->cmask_access) {
314 r300->cmask_access =
315 r300->rws->cs_request_feature(&r300->cs,
316 RADEON_FID_R300_CMASK_ACCESS,
317 true);
318 }
319
320 /* Setup the clear. */
321 if (r300->cmask_access) {
322 /* Pair the resource with the CMASK to avoid other resources
323 * accessing it. */
324 if (!r300->screen->cmask_resource) {
325 mtx_lock(&r300->screen->cmask_mutex);
326 /* Double checking (first unlocked, then locked). */
327 if (!r300->screen->cmask_resource) {
328 /* Don't reference this, so that the texture can be
329 * destroyed while set in cmask_resource.
330 * Then in texture_destroy, we set cmask_resource to NULL. */
331 r300->screen->cmask_resource = fb->cbufs[0]->texture;
332 }
333 mtx_unlock(&r300->screen->cmask_mutex);
334 }
335
336 if (r300->screen->cmask_resource == fb->cbufs[0]->texture) {
337 r300_set_clear_color(r300, color);
338 r300_mark_atom_dirty(r300, &r300->cmask_clear);
339 r300_mark_atom_dirty(r300, &r300->gpu_flush);
340 buffers &= ~PIPE_CLEAR_COLOR;
341 }
342 }
343 }
344 /* Enable CBZB clear. */
345 else if (r300_cbzb_clear_allowed(r300, buffers)) {
346 struct r300_surface *surf = r300_surface(fb->cbufs[0]);
347
348 hyperz->zb_depthclearvalue =
349 r300_depth_clear_cb_value(surf->base.format, color->f);
350
351 width = surf->cbzb_width;
352 height = surf->cbzb_height;
353
354 r300->cbzb_clear = true;
355 r300_mark_fb_state_dirty(r300, R300_CHANGED_HYPERZ_FLAG);
356 }
357
358 /* Clear. */
359 if (buffers) {
360 /* Clear using the blitter. */
361 r300_blitter_begin(r300, R300_CLEAR);
362 util_blitter_clear(r300->blitter, width, height, 1,
363 buffers, color, depth, stencil,
364 util_framebuffer_get_num_samples(fb) > 1);
365 r300_blitter_end(r300);
366 } else if (r300->zmask_clear.dirty ||
367 r300->hiz_clear.dirty ||
368 r300->cmask_clear.dirty) {
369 /* Just clear zmask and hiz now, this does not use the standard draw
370 * procedure. */
371 /* Calculate zmask_clear and hiz_clear atom sizes. */
372 unsigned dwords =
373 r300->gpu_flush.size +
374 (r300->zmask_clear.dirty ? r300->zmask_clear.size : 0) +
375 (r300->hiz_clear.dirty ? r300->hiz_clear.size : 0) +
376 (r300->cmask_clear.dirty ? r300->cmask_clear.size : 0) +
377 r300_get_num_cs_end_dwords(r300);
378
379 /* Reserve CS space. */
380 if (!r300->rws->cs_check_space(&r300->cs, dwords)) {
381 r300_flush(&r300->context, PIPE_FLUSH_ASYNC, NULL);
382 }
383
384 /* Emit clear packets. */
385 r300_emit_gpu_flush(r300, r300->gpu_flush.size, r300->gpu_flush.state);
386 r300->gpu_flush.dirty = false;
387
388 if (r300->zmask_clear.dirty) {
389 r300_emit_zmask_clear(r300, r300->zmask_clear.size,
390 r300->zmask_clear.state);
391 r300->zmask_clear.dirty = false;
392 }
393 if (r300->hiz_clear.dirty) {
394 r300_emit_hiz_clear(r300, r300->hiz_clear.size,
395 r300->hiz_clear.state);
396 r300->hiz_clear.dirty = false;
397 }
398 if (r300->cmask_clear.dirty) {
399 r300_emit_cmask_clear(r300, r300->cmask_clear.size,
400 r300->cmask_clear.state);
401 r300->cmask_clear.dirty = false;
402 }
403 } else {
404 assert(0);
405 }
406
407 /* Disable CBZB clear. */
408 if (r300->cbzb_clear) {
409 r300->cbzb_clear = false;
410 hyperz->zb_depthclearvalue = hyperz_dcv;
411 r300_mark_fb_state_dirty(r300, R300_CHANGED_HYPERZ_FLAG);
412 }
413
414 /* Enable fastfill and/or hiz.
415 *
416 * If we cleared zmask/hiz, it's in use now. The Hyper-Z state update
417 * looks if zmask/hiz is in use and programs hardware accordingly. */
418 if (r300->zmask_in_use || r300->hiz_in_use) {
419 r300_mark_atom_dirty(r300, &r300->hyperz_state);
420 }
421 }
422
423 /* Clear a region of a color surface to a constant value. */
r300_clear_render_target(struct pipe_context * pipe,struct pipe_surface * dst,const union pipe_color_union * color,unsigned dstx,unsigned dsty,unsigned width,unsigned height,bool render_condition_enabled)424 static void r300_clear_render_target(struct pipe_context *pipe,
425 struct pipe_surface *dst,
426 const union pipe_color_union *color,
427 unsigned dstx, unsigned dsty,
428 unsigned width, unsigned height,
429 bool render_condition_enabled)
430 {
431 struct r300_context *r300 = r300_context(pipe);
432
433 r300_blitter_begin(r300, R300_CLEAR_SURFACE |
434 (render_condition_enabled ? 0 : R300_IGNORE_RENDER_COND));
435 util_blitter_clear_render_target(r300->blitter, dst, color,
436 dstx, dsty, width, height);
437 r300_blitter_end(r300);
438 }
439
440 /* Clear a region of a depth stencil surface. */
r300_clear_depth_stencil(struct pipe_context * pipe,struct pipe_surface * dst,unsigned clear_flags,double depth,unsigned stencil,unsigned dstx,unsigned dsty,unsigned width,unsigned height,bool render_condition_enabled)441 static void r300_clear_depth_stencil(struct pipe_context *pipe,
442 struct pipe_surface *dst,
443 unsigned clear_flags,
444 double depth,
445 unsigned stencil,
446 unsigned dstx, unsigned dsty,
447 unsigned width, unsigned height,
448 bool render_condition_enabled)
449 {
450 struct r300_context *r300 = r300_context(pipe);
451 struct pipe_framebuffer_state *fb =
452 (struct pipe_framebuffer_state*)r300->fb_state.state;
453
454 if (r300->zmask_in_use && !r300->locked_zbuffer) {
455 if (fb->zsbuf->texture == dst->texture) {
456 r300_decompress_zmask(r300);
457 }
458 }
459
460 /* XXX Do not decompress ZMask of the currently-set zbuffer. */
461 r300_blitter_begin(r300, R300_CLEAR_SURFACE |
462 (render_condition_enabled ? 0 : R300_IGNORE_RENDER_COND));
463 util_blitter_clear_depth_stencil(r300->blitter, dst, clear_flags, depth, stencil,
464 dstx, dsty, width, height);
465 r300_blitter_end(r300);
466 }
467
r300_decompress_zmask(struct r300_context * r300)468 void r300_decompress_zmask(struct r300_context *r300)
469 {
470 struct pipe_framebuffer_state *fb =
471 (struct pipe_framebuffer_state*)r300->fb_state.state;
472
473 if (!r300->zmask_in_use || r300->locked_zbuffer)
474 return;
475
476 r300->zmask_decompress = true;
477 r300_mark_atom_dirty(r300, &r300->hyperz_state);
478
479 r300_blitter_begin(r300, R300_DECOMPRESS);
480 util_blitter_custom_clear_depth(r300->blitter, fb->width, fb->height, 0,
481 r300->dsa_decompress_zmask);
482 r300_blitter_end(r300);
483
484 r300->zmask_decompress = false;
485 r300->zmask_in_use = false;
486 r300_mark_atom_dirty(r300, &r300->hyperz_state);
487 }
488
r300_decompress_zmask_locked_unsafe(struct r300_context * r300)489 void r300_decompress_zmask_locked_unsafe(struct r300_context *r300)
490 {
491 struct pipe_framebuffer_state fb;
492
493 memset(&fb, 0, sizeof(fb));
494 fb.width = r300->locked_zbuffer->width;
495 fb.height = r300->locked_zbuffer->height;
496 fb.zsbuf = r300->locked_zbuffer;
497
498 r300->context.set_framebuffer_state(&r300->context, &fb);
499 r300_decompress_zmask(r300);
500 }
501
r300_decompress_zmask_locked(struct r300_context * r300)502 void r300_decompress_zmask_locked(struct r300_context *r300)
503 {
504 struct pipe_framebuffer_state saved_fb;
505
506 memset(&saved_fb, 0, sizeof(saved_fb));
507 util_copy_framebuffer_state(&saved_fb, r300->fb_state.state);
508 r300_decompress_zmask_locked_unsafe(r300);
509 r300->context.set_framebuffer_state(&r300->context, &saved_fb);
510 util_unreference_framebuffer_state(&saved_fb);
511
512 pipe_surface_reference(&r300->locked_zbuffer, NULL);
513 }
514
r300_is_blit_supported(enum pipe_format format)515 bool r300_is_blit_supported(enum pipe_format format)
516 {
517 const struct util_format_description *desc =
518 util_format_description(format);
519
520 return desc->layout == UTIL_FORMAT_LAYOUT_PLAIN ||
521 desc->layout == UTIL_FORMAT_LAYOUT_S3TC ||
522 desc->layout == UTIL_FORMAT_LAYOUT_RGTC;
523 }
524
525 /* Copy a block of pixels from one surface to another. */
r300_resource_copy_region(struct pipe_context * pipe,struct pipe_resource * dst,unsigned dst_level,unsigned dstx,unsigned dsty,unsigned dstz,struct pipe_resource * src,unsigned src_level,const struct pipe_box * src_box)526 static void r300_resource_copy_region(struct pipe_context *pipe,
527 struct pipe_resource *dst,
528 unsigned dst_level,
529 unsigned dstx, unsigned dsty, unsigned dstz,
530 struct pipe_resource *src,
531 unsigned src_level,
532 const struct pipe_box *src_box)
533 {
534 struct pipe_screen *screen = pipe->screen;
535 struct r300_context *r300 = r300_context(pipe);
536 struct pipe_framebuffer_state *fb =
537 (struct pipe_framebuffer_state*)r300->fb_state.state;
538 unsigned src_width0 = r300_resource(src)->tex.width0;
539 unsigned src_height0 = r300_resource(src)->tex.height0;
540 unsigned dst_width0 = r300_resource(dst)->tex.width0;
541 unsigned dst_height0 = r300_resource(dst)->tex.height0;
542 unsigned layout;
543 struct pipe_box box, dstbox;
544 struct pipe_sampler_view src_templ, *src_view;
545 struct pipe_surface dst_templ, *dst_view;
546
547 /* Fallback for buffers. */
548 if ((dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) ||
549 !r300_is_blit_supported(dst->format)) {
550 util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,
551 src, src_level, src_box);
552 return;
553 }
554
555 /* Can't read MSAA textures. */
556 if (src->nr_samples > 1 || dst->nr_samples > 1) {
557 return;
558 }
559
560 /* The code below changes the texture format so that the copy can be done
561 * on hardware. E.g. depth-stencil surfaces are copied as RGBA
562 * colorbuffers. */
563
564 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
565 util_blitter_default_src_texture(r300->blitter, &src_templ, src, src_level);
566
567 layout = util_format_description(dst_templ.format)->layout;
568
569 /* Handle non-renderable plain formats. */
570 if (layout == UTIL_FORMAT_LAYOUT_PLAIN &&
571 (!screen->is_format_supported(screen, src_templ.format, src->target,
572 src->nr_samples, src->nr_storage_samples,
573 PIPE_BIND_SAMPLER_VIEW) ||
574 !screen->is_format_supported(screen, dst_templ.format, dst->target,
575 dst->nr_samples, dst->nr_storage_samples,
576 PIPE_BIND_RENDER_TARGET))) {
577 switch (util_format_get_blocksize(dst_templ.format)) {
578 case 1:
579 dst_templ.format = PIPE_FORMAT_I8_UNORM;
580 break;
581 case 2:
582 dst_templ.format = PIPE_FORMAT_B4G4R4A4_UNORM;
583 break;
584 case 4:
585 dst_templ.format = PIPE_FORMAT_B8G8R8A8_UNORM;
586 break;
587 case 8:
588 dst_templ.format = PIPE_FORMAT_R16G16B16A16_UNORM;
589 break;
590 default:
591 debug_printf("r300: copy_region: Unhandled format: %s. Falling back to software.\n"
592 "r300: copy_region: Software fallback doesn't work for tiled textures.\n",
593 util_format_short_name(dst_templ.format));
594 }
595 src_templ.format = dst_templ.format;
596 }
597
598 /* Handle compressed formats. */
599 if (layout == UTIL_FORMAT_LAYOUT_S3TC ||
600 layout == UTIL_FORMAT_LAYOUT_RGTC) {
601 assert(src_templ.format == dst_templ.format);
602
603 box = *src_box;
604 src_box = &box;
605
606 dst_width0 = align(dst_width0, 4);
607 dst_height0 = align(dst_height0, 4);
608 src_width0 = align(src_width0, 4);
609 src_height0 = align(src_height0, 4);
610 box.width = align(box.width, 4);
611 box.height = align(box.height, 4);
612
613 switch (util_format_get_blocksize(dst_templ.format)) {
614 case 8:
615 /* one 4x4 pixel block has 8 bytes.
616 * we set 1 pixel = 4 bytes ===> 1 block corresponds to 2 pixels. */
617 dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
618 dst_width0 = dst_width0 / 2;
619 src_width0 = src_width0 / 2;
620 dstx /= 2;
621 box.x /= 2;
622 box.width /= 2;
623 break;
624 case 16:
625 /* one 4x4 pixel block has 16 bytes.
626 * we set 1 pixel = 4 bytes ===> 1 block corresponds to 4 pixels. */
627 dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
628 break;
629 }
630 src_templ.format = dst_templ.format;
631
632 dst_height0 = dst_height0 / 4;
633 src_height0 = src_height0 / 4;
634 dsty /= 4;
635 box.y /= 4;
636 box.height /= 4;
637 }
638
639 /* Fallback for textures. */
640 if (!screen->is_format_supported(screen, dst_templ.format,
641 dst->target, dst->nr_samples,
642 dst->nr_storage_samples,
643 PIPE_BIND_RENDER_TARGET) ||
644 !screen->is_format_supported(screen, src_templ.format,
645 src->target, src->nr_samples,
646 src->nr_storage_samples,
647 PIPE_BIND_SAMPLER_VIEW)) {
648 assert(0 && "this shouldn't happen, update r300_is_blit_supported");
649 util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,
650 src, src_level, src_box);
651 return;
652 }
653
654 /* Decompress ZMASK. */
655 if (r300->zmask_in_use && !r300->locked_zbuffer) {
656 if (fb->zsbuf->texture == src ||
657 fb->zsbuf->texture == dst) {
658 r300_decompress_zmask(r300);
659 }
660 }
661
662 dst_view = r300_create_surface_custom(pipe, dst, &dst_templ, dst_width0, dst_height0);
663 src_view = r300_create_sampler_view_custom(pipe, src, &src_templ, src_width0, src_height0);
664
665 u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height),
666 abs(src_box->depth), &dstbox);
667
668 r300_blitter_begin(r300, R300_COPY);
669 util_blitter_blit_generic(r300->blitter, dst_view, &dstbox,
670 src_view, src_box, src_width0, src_height0,
671 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,
672 false, false, 0, NULL);
673 r300_blitter_end(r300);
674
675 pipe_surface_reference(&dst_view, NULL);
676 pipe_sampler_view_reference(&src_view, NULL);
677 }
678
r300_is_simple_msaa_resolve(const struct pipe_blit_info * info)679 static bool r300_is_simple_msaa_resolve(const struct pipe_blit_info *info)
680 {
681 unsigned dst_width = u_minify(info->dst.resource->width0, info->dst.level);
682 unsigned dst_height = u_minify(info->dst.resource->height0, info->dst.level);
683
684 return info->src.resource->nr_samples > 1 &&
685 info->dst.resource->nr_samples <= 1 &&
686 info->dst.resource->format == info->src.resource->format &&
687 info->dst.resource->format == info->dst.format &&
688 info->src.resource->format == info->src.format &&
689 !info->scissor_enable &&
690 info->mask == PIPE_MASK_RGBA &&
691 dst_width == info->src.resource->width0 &&
692 dst_height == info->src.resource->height0 &&
693 info->dst.box.x == 0 &&
694 info->dst.box.y == 0 &&
695 info->dst.box.width == dst_width &&
696 info->dst.box.height == dst_height &&
697 info->src.box.x == 0 &&
698 info->src.box.y == 0 &&
699 info->src.box.width == dst_width &&
700 info->src.box.height == dst_height &&
701 (r300_resource(info->dst.resource)->tex.microtile != RADEON_LAYOUT_LINEAR ||
702 r300_resource(info->dst.resource)->tex.macrotile[info->dst.level] != RADEON_LAYOUT_LINEAR);
703 }
704
r300_simple_msaa_resolve(struct pipe_context * pipe,struct pipe_resource * dst,unsigned dst_level,unsigned dst_layer,struct pipe_resource * src,enum pipe_format format)705 static void r300_simple_msaa_resolve(struct pipe_context *pipe,
706 struct pipe_resource *dst,
707 unsigned dst_level,
708 unsigned dst_layer,
709 struct pipe_resource *src,
710 enum pipe_format format)
711 {
712 struct r300_context *r300 = r300_context(pipe);
713 struct r300_surface *srcsurf, *dstsurf;
714 struct pipe_surface surf_tmpl;
715 struct r300_aa_state *aa = (struct r300_aa_state*)r300->aa_state.state;
716
717 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
718 surf_tmpl.format = format;
719 srcsurf = r300_surface(pipe->create_surface(pipe, src, &surf_tmpl));
720
721 surf_tmpl.format = format;
722 surf_tmpl.u.tex.level = dst_level;
723 surf_tmpl.u.tex.first_layer =
724 surf_tmpl.u.tex.last_layer = dst_layer;
725 dstsurf = r300_surface(pipe->create_surface(pipe, dst, &surf_tmpl));
726
727 /* COLORPITCH should contain the tiling info of the resolve buffer.
728 * The tiling of the AA buffer isn't programmable anyway. */
729 srcsurf->pitch &= ~(R300_COLOR_TILE(1) | R300_COLOR_MICROTILE(3));
730 srcsurf->pitch |= dstsurf->pitch & (R300_COLOR_TILE(1) | R300_COLOR_MICROTILE(3));
731
732 /* Enable AA resolve. */
733 aa->dest = dstsurf;
734 r300->aa_state.size = 8;
735 r300_mark_atom_dirty(r300, &r300->aa_state);
736
737 /* Resolve the surface. */
738 r300_blitter_begin(r300, R300_CLEAR_SURFACE);
739 util_blitter_custom_color(r300->blitter, &srcsurf->base, NULL);
740 r300_blitter_end(r300);
741
742 /* Disable AA resolve. */
743 aa->dest = NULL;
744 r300->aa_state.size = 4;
745 r300_mark_atom_dirty(r300, &r300->aa_state);
746
747 pipe_surface_reference((struct pipe_surface**)&srcsurf, NULL);
748 pipe_surface_reference((struct pipe_surface**)&dstsurf, NULL);
749 }
750
r300_msaa_resolve(struct pipe_context * pipe,const struct pipe_blit_info * info)751 static void r300_msaa_resolve(struct pipe_context *pipe,
752 const struct pipe_blit_info *info)
753 {
754 struct r300_context *r300 = r300_context(pipe);
755 struct pipe_screen *screen = pipe->screen;
756 struct pipe_resource *tmp, templ;
757 struct pipe_blit_info blit;
758
759 assert(info->src.level == 0);
760 assert(info->src.box.z == 0);
761 assert(info->src.box.depth == 1);
762 assert(info->dst.box.depth == 1);
763
764 if (r300_is_simple_msaa_resolve(info)) {
765 r300_simple_msaa_resolve(pipe, info->dst.resource, info->dst.level,
766 info->dst.box.z, info->src.resource,
767 info->src.format);
768 return;
769 }
770
771 /* resolve into a temporary texture, then blit */
772 memset(&templ, 0, sizeof(templ));
773 templ.target = PIPE_TEXTURE_2D;
774 templ.format = info->src.resource->format;
775 templ.width0 = info->src.resource->width0;
776 templ.height0 = info->src.resource->height0;
777 templ.depth0 = 1;
778 templ.array_size = 1;
779 templ.usage = PIPE_USAGE_DEFAULT;
780 templ.flags = R300_RESOURCE_FORCE_MICROTILING;
781
782 tmp = screen->resource_create(screen, &templ);
783
784 /* resolve */
785 r300_simple_msaa_resolve(pipe, tmp, 0, 0, info->src.resource,
786 info->src.format);
787
788 /* blit */
789 blit = *info;
790 blit.src.resource = tmp;
791 blit.src.box.z = 0;
792
793 r300_blitter_begin(r300, R300_BLIT | R300_IGNORE_RENDER_COND);
794 util_blitter_blit(r300->blitter, &blit, NULL);
795 r300_blitter_end(r300);
796
797 pipe_resource_reference(&tmp, NULL);
798 }
799
r300_blit(struct pipe_context * pipe,const struct pipe_blit_info * blit)800 static void r300_blit(struct pipe_context *pipe,
801 const struct pipe_blit_info *blit)
802 {
803 struct r300_context *r300 = r300_context(pipe);
804 struct pipe_framebuffer_state *fb =
805 (struct pipe_framebuffer_state*)r300->fb_state.state;
806 struct pipe_blit_info info = *blit;
807
808 /* The driver supports sRGB textures but not framebuffers. Blitting
809 * from sRGB to sRGB should be the same as blitting from linear
810 * to linear, so use that, This avoids incorrect linearization.
811 */
812 if (util_format_is_srgb(info.src.format)) {
813 info.src.format = util_format_linear(info.src.format);
814 info.dst.format = util_format_linear(info.dst.format);
815 }
816
817 /* MSAA resolve. */
818 if (info.src.resource->nr_samples > 1 &&
819 !util_format_is_depth_or_stencil(info.src.resource->format)) {
820 r300_msaa_resolve(pipe, &info);
821 return;
822 }
823
824 /* Can't read MSAA textures. */
825 if (info.src.resource->nr_samples > 1) {
826 return;
827 }
828
829 /* Blit a combined depth-stencil resource as color.
830 * S8Z24 is the only supported stencil format. */
831 if ((info.mask & PIPE_MASK_S) &&
832 info.src.format == PIPE_FORMAT_S8_UINT_Z24_UNORM &&
833 info.dst.format == PIPE_FORMAT_S8_UINT_Z24_UNORM) {
834 if (info.dst.resource->nr_samples > 1) {
835 /* Cannot do that with MSAA buffers. */
836 info.mask &= ~PIPE_MASK_S;
837 if (!(info.mask & PIPE_MASK_Z)) {
838 return;
839 }
840 } else {
841 /* Single-sample buffer. */
842 info.src.format = PIPE_FORMAT_B8G8R8A8_UNORM;
843 info.dst.format = PIPE_FORMAT_B8G8R8A8_UNORM;
844 if (info.mask & PIPE_MASK_Z) {
845 info.mask = PIPE_MASK_RGBA; /* depth+stencil */
846 } else {
847 info.mask = PIPE_MASK_B; /* stencil only */
848 }
849 }
850 }
851
852 /* Decompress ZMASK. */
853 if (r300->zmask_in_use && !r300->locked_zbuffer) {
854 if (fb->zsbuf->texture == info.src.resource ||
855 fb->zsbuf->texture == info.dst.resource) {
856 r300_decompress_zmask(r300);
857 }
858 }
859
860 r300_blitter_begin(r300, R300_BLIT |
861 (info.render_condition_enable ? 0 : R300_IGNORE_RENDER_COND));
862 util_blitter_blit(r300->blitter, &info, NULL);
863 r300_blitter_end(r300);
864 }
865
r300_flush_resource(struct pipe_context * ctx,struct pipe_resource * resource)866 static void r300_flush_resource(struct pipe_context *ctx,
867 struct pipe_resource *resource)
868 {
869 }
870
r300_init_blit_functions(struct r300_context * r300)871 void r300_init_blit_functions(struct r300_context *r300)
872 {
873 r300->context.clear = r300_clear;
874 r300->context.clear_render_target = r300_clear_render_target;
875 r300->context.clear_depth_stencil = r300_clear_depth_stencil;
876 r300->context.resource_copy_region = r300_resource_copy_region;
877 r300->context.blit = r300_blit;
878 r300->context.flush_resource = r300_flush_resource;
879 }
880