2 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
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:
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
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. */
23 #include "r300_context.h"
24 #include "r300_emit.h"
25 #include "r300_texture.h"
28 #include "util/u_format.h"
29 #include "util/u_half.h"
30 #include "util/u_pack_color.h"
31 #include "util/u_surface.h"
33 enum r300_blitter_op
/* bitmask */
36 R300_SAVE_TEXTURES
= 2,
37 R300_SAVE_FRAMEBUFFER
= 4,
38 R300_IGNORE_RENDER_COND
= 8,
40 R300_CLEAR
= R300_STOP_QUERY
,
42 R300_CLEAR_SURFACE
= R300_STOP_QUERY
| R300_SAVE_FRAMEBUFFER
,
44 R300_COPY
= R300_STOP_QUERY
| R300_SAVE_FRAMEBUFFER
|
45 R300_SAVE_TEXTURES
| R300_IGNORE_RENDER_COND
,
47 R300_BLIT
= R300_STOP_QUERY
| R300_SAVE_FRAMEBUFFER
|
50 R300_DECOMPRESS
= R300_STOP_QUERY
| R300_IGNORE_RENDER_COND
,
53 static void r300_blitter_begin(struct r300_context
* r300
, enum r300_blitter_op op
)
55 if ((op
& R300_STOP_QUERY
) && r300
->query_current
) {
56 r300
->blitter_saved_query
= r300
->query_current
;
57 r300_stop_query(r300
);
60 /* Yeah we have to save all those states to ensure the blitter operation
61 * is really transparent. The states will be restored by the blitter once
63 util_blitter_save_blend(r300
->blitter
, r300
->blend_state
.state
);
64 util_blitter_save_depth_stencil_alpha(r300
->blitter
, r300
->dsa_state
.state
);
65 util_blitter_save_stencil_ref(r300
->blitter
, &(r300
->stencil_ref
));
66 util_blitter_save_rasterizer(r300
->blitter
, r300
->rs_state
.state
);
67 util_blitter_save_fragment_shader(r300
->blitter
, r300
->fs
.state
);
68 util_blitter_save_vertex_shader(r300
->blitter
, r300
->vs_state
.state
);
69 util_blitter_save_viewport(r300
->blitter
, &r300
->viewport
);
70 util_blitter_save_scissor(r300
->blitter
, r300
->scissor_state
.state
);
71 util_blitter_save_sample_mask(r300
->blitter
, *(unsigned*)r300
->sample_mask
.state
);
72 util_blitter_save_vertex_buffer_slot(r300
->blitter
, r300
->vertex_buffer
);
73 util_blitter_save_vertex_elements(r300
->blitter
, r300
->velems
);
75 if (op
& R300_SAVE_FRAMEBUFFER
) {
76 util_blitter_save_framebuffer(r300
->blitter
, r300
->fb_state
.state
);
79 if (op
& R300_SAVE_TEXTURES
) {
80 struct r300_textures_state
* state
=
81 (struct r300_textures_state
*)r300
->textures_state
.state
;
83 util_blitter_save_fragment_sampler_states(
84 r300
->blitter
, state
->sampler_state_count
,
85 (void**)state
->sampler_states
);
87 util_blitter_save_fragment_sampler_views(
88 r300
->blitter
, state
->sampler_view_count
,
89 (struct pipe_sampler_view
**)state
->sampler_views
);
92 if (op
& R300_IGNORE_RENDER_COND
) {
94 r300
->blitter_saved_skip_rendering
= r300
->skip_rendering
+1;
95 r300
->skip_rendering
= FALSE
;
97 r300
->blitter_saved_skip_rendering
= 0;
101 static void r300_blitter_end(struct r300_context
*r300
)
103 if (r300
->blitter_saved_query
) {
104 r300_resume_query(r300
, r300
->blitter_saved_query
);
105 r300
->blitter_saved_query
= NULL
;
108 if (r300
->blitter_saved_skip_rendering
) {
109 /* Restore the flag. */
110 r300
->skip_rendering
= r300
->blitter_saved_skip_rendering
-1;
114 static uint32_t r300_depth_clear_cb_value(enum pipe_format format
,
118 util_pack_color(rgba
, format
, &uc
);
120 if (util_format_get_blocksizebits(format
) == 32)
123 return uc
.us
| (uc
.us
<< 16);
126 static boolean
r300_cbzb_clear_allowed(struct r300_context
*r300
,
127 unsigned clear_buffers
)
129 struct pipe_framebuffer_state
*fb
=
130 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
132 /* Only color clear allowed, and only one colorbuffer. */
133 if ((clear_buffers
& ~PIPE_CLEAR_COLOR
) != 0 || fb
->nr_cbufs
!= 1 || !fb
->cbufs
[0])
136 return r300_surface(fb
->cbufs
[0])->cbzb_allowed
;
139 static boolean
r300_fast_zclear_allowed(struct r300_context
*r300
,
140 unsigned clear_buffers
)
142 struct pipe_framebuffer_state
*fb
=
143 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
145 return r300_resource(fb
->zsbuf
->texture
)->tex
.zmask_dwords
[fb
->zsbuf
->u
.tex
.level
] != 0;
148 static boolean
r300_hiz_clear_allowed(struct r300_context
*r300
)
150 struct pipe_framebuffer_state
*fb
=
151 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
153 return r300_resource(fb
->zsbuf
->texture
)->tex
.hiz_dwords
[fb
->zsbuf
->u
.tex
.level
] != 0;
156 static uint32_t r300_depth_clear_value(enum pipe_format format
,
157 double depth
, unsigned stencil
)
160 case PIPE_FORMAT_Z16_UNORM
:
161 case PIPE_FORMAT_X8Z24_UNORM
:
162 return util_pack_z(format
, depth
);
164 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
165 return util_pack_z_stencil(format
, depth
, stencil
);
173 static uint32_t r300_hiz_clear_value(double depth
)
175 uint32_t r
= (uint32_t)(CLAMP(depth
, 0, 1) * 255.5);
177 return r
| (r
<< 8) | (r
<< 16) | (r
<< 24);
180 static void r300_set_clear_color(struct r300_context
*r300
,
181 const union pipe_color_union
*color
)
183 struct pipe_framebuffer_state
*fb
=
184 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
187 memset(&uc
, 0, sizeof(uc
));
188 util_pack_color(color
->f
, fb
->cbufs
[0]->format
, &uc
);
190 if (fb
->cbufs
[0]->format
== PIPE_FORMAT_R16G16B16A16_FLOAT
||
191 fb
->cbufs
[0]->format
== PIPE_FORMAT_R16G16B16X16_FLOAT
) {
192 /* (0,1,2,3) maps to (B,G,R,A) */
193 r300
->color_clear_value_gb
= uc
.h
[0] | ((uint32_t)uc
.h
[1] << 16);
194 r300
->color_clear_value_ar
= uc
.h
[2] | ((uint32_t)uc
.h
[3] << 16);
196 r300
->color_clear_value
= uc
.ui
[0];
200 DEBUG_GET_ONCE_BOOL_OPTION(hyperz
, "RADEON_HYPERZ", FALSE
)
202 /* Clear currently bound buffers. */
203 static void r300_clear(struct pipe_context
* pipe
,
205 const union pipe_color_union
*color
,
209 /* My notes about Zbuffer compression:
211 * 1) The zbuffer must be micro-tiled and whole microtiles must be
212 * written if compression is enabled. If microtiling is disabled,
215 * 2) There is ZMASK RAM which contains a compressed zbuffer.
216 * Each dword of the Z Mask contains compression information
217 * for 16 4x4 pixel tiles, that is 2 bits for each tile.
218 * On chips with 2 Z pipes, every other dword maps to a different
219 * pipe. On newer chipsets, there is a new compression mode
220 * with 8x8 pixel tiles per 2 bits.
222 * 3) The FASTFILL bit has nothing to do with filling. It only tells hw
223 * it should look in the ZMASK RAM first before fetching from a real
226 * 4) If a pixel is in a cleared state, ZB_DEPTHCLEARVALUE is returned
227 * during zbuffer reads instead of the value that is actually stored
228 * in the zbuffer memory. A pixel is in a cleared state when its ZMASK
229 * is equal to 0. Therefore, if you clear ZMASK with zeros, you may
230 * leave the zbuffer memory uninitialized, but then you must enable
231 * compression, so that the ZMASK RAM is actually used.
233 * 5) Each 4x4 (or 8x8) tile is automatically decompressed and recompressed
234 * during zbuffer updates. A special decompressing operation should be
235 * used to fully decompress a zbuffer, which basically just stores all
236 * compressed tiles in ZMASK to the zbuffer memory.
238 * 6) For a 16-bit zbuffer, compression causes a hung with one or
239 * two samples and should not be used.
241 * 7) FORCE_COMPRESSED_STENCIL_VALUE should be enabled for stencil clears
242 * to avoid needless decompression.
244 * 8) Fastfill must not be used if reading of compressed Z data is disabled
245 * and writing of compressed Z data is enabled (RD/WR_COMP_ENABLE),
246 * i.e. it cannot be used to compress the zbuffer.
248 * 9) ZB_CB_CLEAR does not interact with zbuffer compression in any way.
253 struct r300_context
* r300
= r300_context(pipe
);
254 struct pipe_framebuffer_state
*fb
=
255 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
256 struct r300_hyperz_state
*hyperz
=
257 (struct r300_hyperz_state
*)r300
->hyperz_state
.state
;
258 uint32_t width
= fb
->width
;
259 uint32_t height
= fb
->height
;
260 uint32_t hyperz_dcv
= hyperz
->zb_depthclearvalue
;
263 * The zbuffer must be in micro-tiled mode, otherwise it locks up. */
264 if (buffers
& PIPE_CLEAR_DEPTHSTENCIL
) {
265 boolean zmask_clear
, hiz_clear
;
267 /* If both depth and stencil are present, they must be cleared together. */
268 if (fb
->zsbuf
->texture
->format
== PIPE_FORMAT_S8_UINT_Z24_UNORM
&&
269 (buffers
& PIPE_CLEAR_DEPTHSTENCIL
) != PIPE_CLEAR_DEPTHSTENCIL
) {
273 zmask_clear
= r300_fast_zclear_allowed(r300
, buffers
);
274 hiz_clear
= r300_hiz_clear_allowed(r300
);
277 /* If we need Hyper-Z. */
278 if (zmask_clear
|| hiz_clear
) {
279 /* Try to obtain the access to Hyper-Z buffers if we don't have one. */
280 if (!r300
->hyperz_enabled
&&
281 (r300
->screen
->caps
.is_r500
|| debug_get_option_hyperz())) {
282 r300
->hyperz_enabled
=
283 r300
->rws
->cs_request_feature(r300
->cs
,
284 RADEON_FID_R300_HYPERZ_ACCESS
,
286 if (r300
->hyperz_enabled
) {
287 /* Need to emit HyperZ buffer regs for the first time. */
288 r300_mark_fb_state_dirty(r300
, R300_CHANGED_HYPERZ_FLAG
);
292 /* Setup Hyper-Z clears. */
293 if (r300
->hyperz_enabled
) {
295 hyperz_dcv
= hyperz
->zb_depthclearvalue
=
296 r300_depth_clear_value(fb
->zsbuf
->format
, depth
, stencil
);
298 r300_mark_atom_dirty(r300
, &r300
->zmask_clear
);
299 r300_mark_atom_dirty(r300
, &r300
->gpu_flush
);
300 buffers
&= ~PIPE_CLEAR_DEPTHSTENCIL
;
304 r300
->hiz_clear_value
= r300_hiz_clear_value(depth
);
305 r300_mark_atom_dirty(r300
, &r300
->hiz_clear
);
306 r300_mark_atom_dirty(r300
, &r300
->gpu_flush
);
308 r300
->num_z_clears
++;
313 /* Use fast color clear for an AA colorbuffer.
314 * The CMASK is shared between all colorbuffers, so we use it
315 * if there is only one colorbuffer bound. */
316 if ((buffers
& PIPE_CLEAR_COLOR
) && fb
->nr_cbufs
== 1 && fb
->cbufs
[0] &&
317 r300_resource(fb
->cbufs
[0]->texture
)->tex
.cmask_dwords
) {
318 /* Try to obtain the access to the CMASK if we don't have one. */
319 if (!r300
->cmask_access
) {
321 r300
->rws
->cs_request_feature(r300
->cs
,
322 RADEON_FID_R300_CMASK_ACCESS
,
326 /* Setup the clear. */
327 if (r300
->cmask_access
) {
328 /* Pair the resource with the CMASK to avoid other resources
330 if (!r300
->screen
->cmask_resource
) {
331 mtx_lock(&r300
->screen
->cmask_mutex
);
332 /* Double checking (first unlocked, then locked). */
333 if (!r300
->screen
->cmask_resource
) {
334 /* Don't reference this, so that the texture can be
335 * destroyed while set in cmask_resource.
336 * Then in texture_destroy, we set cmask_resource to NULL. */
337 r300
->screen
->cmask_resource
= fb
->cbufs
[0]->texture
;
339 mtx_unlock(&r300
->screen
->cmask_mutex
);
342 if (r300
->screen
->cmask_resource
== fb
->cbufs
[0]->texture
) {
343 r300_set_clear_color(r300
, color
);
344 r300_mark_atom_dirty(r300
, &r300
->cmask_clear
);
345 r300_mark_atom_dirty(r300
, &r300
->gpu_flush
);
346 buffers
&= ~PIPE_CLEAR_COLOR
;
350 /* Enable CBZB clear. */
351 else if (r300_cbzb_clear_allowed(r300
, buffers
)) {
352 struct r300_surface
*surf
= r300_surface(fb
->cbufs
[0]);
354 hyperz
->zb_depthclearvalue
=
355 r300_depth_clear_cb_value(surf
->base
.format
, color
->f
);
357 width
= surf
->cbzb_width
;
358 height
= surf
->cbzb_height
;
360 r300
->cbzb_clear
= TRUE
;
361 r300_mark_fb_state_dirty(r300
, R300_CHANGED_HYPERZ_FLAG
);
366 /* Clear using the blitter. */
367 r300_blitter_begin(r300
, R300_CLEAR
);
368 util_blitter_clear(r300
->blitter
, width
, height
, 1,
369 buffers
, color
, depth
, stencil
,
370 util_framebuffer_get_num_samples(fb
) > 1);
371 r300_blitter_end(r300
);
372 } else if (r300
->zmask_clear
.dirty
||
373 r300
->hiz_clear
.dirty
||
374 r300
->cmask_clear
.dirty
) {
375 /* Just clear zmask and hiz now, this does not use the standard draw
377 /* Calculate zmask_clear and hiz_clear atom sizes. */
379 r300
->gpu_flush
.size
+
380 (r300
->zmask_clear
.dirty
? r300
->zmask_clear
.size
: 0) +
381 (r300
->hiz_clear
.dirty
? r300
->hiz_clear
.size
: 0) +
382 (r300
->cmask_clear
.dirty
? r300
->cmask_clear
.size
: 0) +
383 r300_get_num_cs_end_dwords(r300
);
385 /* Reserve CS space. */
386 if (!r300
->rws
->cs_check_space(r300
->cs
, dwords
, false)) {
387 r300_flush(&r300
->context
, PIPE_FLUSH_ASYNC
, NULL
);
390 /* Emit clear packets. */
391 r300_emit_gpu_flush(r300
, r300
->gpu_flush
.size
, r300
->gpu_flush
.state
);
392 r300
->gpu_flush
.dirty
= FALSE
;
394 if (r300
->zmask_clear
.dirty
) {
395 r300_emit_zmask_clear(r300
, r300
->zmask_clear
.size
,
396 r300
->zmask_clear
.state
);
397 r300
->zmask_clear
.dirty
= FALSE
;
399 if (r300
->hiz_clear
.dirty
) {
400 r300_emit_hiz_clear(r300
, r300
->hiz_clear
.size
,
401 r300
->hiz_clear
.state
);
402 r300
->hiz_clear
.dirty
= FALSE
;
404 if (r300
->cmask_clear
.dirty
) {
405 r300_emit_cmask_clear(r300
, r300
->cmask_clear
.size
,
406 r300
->cmask_clear
.state
);
407 r300
->cmask_clear
.dirty
= FALSE
;
413 /* Disable CBZB clear. */
414 if (r300
->cbzb_clear
) {
415 r300
->cbzb_clear
= FALSE
;
416 hyperz
->zb_depthclearvalue
= hyperz_dcv
;
417 r300_mark_fb_state_dirty(r300
, R300_CHANGED_HYPERZ_FLAG
);
420 /* Enable fastfill and/or hiz.
422 * If we cleared zmask/hiz, it's in use now. The Hyper-Z state update
423 * looks if zmask/hiz is in use and programs hardware accordingly. */
424 if (r300
->zmask_in_use
|| r300
->hiz_in_use
) {
425 r300_mark_atom_dirty(r300
, &r300
->hyperz_state
);
429 /* Clear a region of a color surface to a constant value. */
430 static void r300_clear_render_target(struct pipe_context
*pipe
,
431 struct pipe_surface
*dst
,
432 const union pipe_color_union
*color
,
433 unsigned dstx
, unsigned dsty
,
434 unsigned width
, unsigned height
,
435 bool render_condition_enabled
)
437 struct r300_context
*r300
= r300_context(pipe
);
439 r300_blitter_begin(r300
, R300_CLEAR_SURFACE
|
440 (render_condition_enabled
? 0 : R300_IGNORE_RENDER_COND
));
441 util_blitter_clear_render_target(r300
->blitter
, dst
, color
,
442 dstx
, dsty
, width
, height
);
443 r300_blitter_end(r300
);
446 /* Clear a region of a depth stencil surface. */
447 static void r300_clear_depth_stencil(struct pipe_context
*pipe
,
448 struct pipe_surface
*dst
,
449 unsigned clear_flags
,
452 unsigned dstx
, unsigned dsty
,
453 unsigned width
, unsigned height
,
454 bool render_condition_enabled
)
456 struct r300_context
*r300
= r300_context(pipe
);
457 struct pipe_framebuffer_state
*fb
=
458 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
460 if (r300
->zmask_in_use
&& !r300
->locked_zbuffer
) {
461 if (fb
->zsbuf
->texture
== dst
->texture
) {
462 r300_decompress_zmask(r300
);
466 /* XXX Do not decompress ZMask of the currently-set zbuffer. */
467 r300_blitter_begin(r300
, R300_CLEAR_SURFACE
|
468 (render_condition_enabled
? 0 : R300_IGNORE_RENDER_COND
));
469 util_blitter_clear_depth_stencil(r300
->blitter
, dst
, clear_flags
, depth
, stencil
,
470 dstx
, dsty
, width
, height
);
471 r300_blitter_end(r300
);
474 void r300_decompress_zmask(struct r300_context
*r300
)
476 struct pipe_framebuffer_state
*fb
=
477 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
479 if (!r300
->zmask_in_use
|| r300
->locked_zbuffer
)
482 r300
->zmask_decompress
= TRUE
;
483 r300_mark_atom_dirty(r300
, &r300
->hyperz_state
);
485 r300_blitter_begin(r300
, R300_DECOMPRESS
);
486 util_blitter_custom_clear_depth(r300
->blitter
, fb
->width
, fb
->height
, 0,
487 r300
->dsa_decompress_zmask
);
488 r300_blitter_end(r300
);
490 r300
->zmask_decompress
= FALSE
;
491 r300
->zmask_in_use
= FALSE
;
492 r300_mark_atom_dirty(r300
, &r300
->hyperz_state
);
495 void r300_decompress_zmask_locked_unsafe(struct r300_context
*r300
)
497 struct pipe_framebuffer_state fb
;
499 memset(&fb
, 0, sizeof(fb
));
500 fb
.width
= r300
->locked_zbuffer
->width
;
501 fb
.height
= r300
->locked_zbuffer
->height
;
502 fb
.zsbuf
= r300
->locked_zbuffer
;
504 r300
->context
.set_framebuffer_state(&r300
->context
, &fb
);
505 r300_decompress_zmask(r300
);
508 void r300_decompress_zmask_locked(struct r300_context
*r300
)
510 struct pipe_framebuffer_state saved_fb
;
512 memset(&saved_fb
, 0, sizeof(saved_fb
));
513 util_copy_framebuffer_state(&saved_fb
, r300
->fb_state
.state
);
514 r300_decompress_zmask_locked_unsafe(r300
);
515 r300
->context
.set_framebuffer_state(&r300
->context
, &saved_fb
);
516 util_unreference_framebuffer_state(&saved_fb
);
518 pipe_surface_reference(&r300
->locked_zbuffer
, NULL
);
521 bool r300_is_blit_supported(enum pipe_format format
)
523 const struct util_format_description
*desc
=
524 util_format_description(format
);
526 return desc
->layout
== UTIL_FORMAT_LAYOUT_PLAIN
||
527 desc
->layout
== UTIL_FORMAT_LAYOUT_S3TC
||
528 desc
->layout
== UTIL_FORMAT_LAYOUT_RGTC
;
531 /* Copy a block of pixels from one surface to another. */
532 static void r300_resource_copy_region(struct pipe_context
*pipe
,
533 struct pipe_resource
*dst
,
535 unsigned dstx
, unsigned dsty
, unsigned dstz
,
536 struct pipe_resource
*src
,
538 const struct pipe_box
*src_box
)
540 struct pipe_screen
*screen
= pipe
->screen
;
541 struct r300_context
*r300
= r300_context(pipe
);
542 struct pipe_framebuffer_state
*fb
=
543 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
544 unsigned src_width0
= r300_resource(src
)->tex
.width0
;
545 unsigned src_height0
= r300_resource(src
)->tex
.height0
;
546 unsigned dst_width0
= r300_resource(dst
)->tex
.width0
;
547 unsigned dst_height0
= r300_resource(dst
)->tex
.height0
;
549 struct pipe_box box
, dstbox
;
550 struct pipe_sampler_view src_templ
, *src_view
;
551 struct pipe_surface dst_templ
, *dst_view
;
553 /* Fallback for buffers. */
554 if ((dst
->target
== PIPE_BUFFER
&& src
->target
== PIPE_BUFFER
) ||
555 !r300_is_blit_supported(dst
->format
)) {
556 util_resource_copy_region(pipe
, dst
, dst_level
, dstx
, dsty
, dstz
,
557 src
, src_level
, src_box
);
561 /* Can't read MSAA textures. */
562 if (src
->nr_samples
> 1 || dst
->nr_samples
> 1) {
566 /* The code below changes the texture format so that the copy can be done
567 * on hardware. E.g. depth-stencil surfaces are copied as RGBA
570 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
571 util_blitter_default_src_texture(r300
->blitter
, &src_templ
, src
, src_level
);
573 layout
= util_format_description(dst_templ
.format
)->layout
;
575 /* Handle non-renderable plain formats. */
576 if (layout
== UTIL_FORMAT_LAYOUT_PLAIN
&&
577 (!screen
->is_format_supported(screen
, src_templ
.format
, src
->target
,
578 src
->nr_samples
, src
->nr_storage_samples
,
579 PIPE_BIND_SAMPLER_VIEW
) ||
580 !screen
->is_format_supported(screen
, dst_templ
.format
, dst
->target
,
581 dst
->nr_samples
, dst
->nr_storage_samples
,
582 PIPE_BIND_RENDER_TARGET
))) {
583 switch (util_format_get_blocksize(dst_templ
.format
)) {
585 dst_templ
.format
= PIPE_FORMAT_I8_UNORM
;
588 dst_templ
.format
= PIPE_FORMAT_B4G4R4A4_UNORM
;
591 dst_templ
.format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
594 dst_templ
.format
= PIPE_FORMAT_R16G16B16A16_UNORM
;
597 debug_printf("r300: copy_region: Unhandled format: %s. Falling back to software.\n"
598 "r300: copy_region: Software fallback doesn't work for tiled textures.\n",
599 util_format_short_name(dst_templ
.format
));
601 src_templ
.format
= dst_templ
.format
;
604 /* Handle compressed formats. */
605 if (layout
== UTIL_FORMAT_LAYOUT_S3TC
||
606 layout
== UTIL_FORMAT_LAYOUT_RGTC
) {
607 assert(src_templ
.format
== dst_templ
.format
);
612 dst_width0
= align(dst_width0
, 4);
613 dst_height0
= align(dst_height0
, 4);
614 src_width0
= align(src_width0
, 4);
615 src_height0
= align(src_height0
, 4);
616 box
.width
= align(box
.width
, 4);
617 box
.height
= align(box
.height
, 4);
619 switch (util_format_get_blocksize(dst_templ
.format
)) {
621 /* one 4x4 pixel block has 8 bytes.
622 * we set 1 pixel = 4 bytes ===> 1 block corrensponds to 2 pixels. */
623 dst_templ
.format
= PIPE_FORMAT_R8G8B8A8_UNORM
;
624 dst_width0
= dst_width0
/ 2;
625 src_width0
= src_width0
/ 2;
631 /* one 4x4 pixel block has 16 bytes.
632 * we set 1 pixel = 4 bytes ===> 1 block corresponds to 4 pixels. */
633 dst_templ
.format
= PIPE_FORMAT_R8G8B8A8_UNORM
;
636 src_templ
.format
= dst_templ
.format
;
638 dst_height0
= dst_height0
/ 4;
639 src_height0
= src_height0
/ 4;
645 /* Fallback for textures. */
646 if (!screen
->is_format_supported(screen
, dst_templ
.format
,
647 dst
->target
, dst
->nr_samples
,
648 dst
->nr_storage_samples
,
649 PIPE_BIND_RENDER_TARGET
) ||
650 !screen
->is_format_supported(screen
, src_templ
.format
,
651 src
->target
, src
->nr_samples
,
652 src
->nr_storage_samples
,
653 PIPE_BIND_SAMPLER_VIEW
)) {
654 assert(0 && "this shouldn't happen, update r300_is_blit_supported");
655 util_resource_copy_region(pipe
, dst
, dst_level
, dstx
, dsty
, dstz
,
656 src
, src_level
, src_box
);
660 /* Decompress ZMASK. */
661 if (r300
->zmask_in_use
&& !r300
->locked_zbuffer
) {
662 if (fb
->zsbuf
->texture
== src
||
663 fb
->zsbuf
->texture
== dst
) {
664 r300_decompress_zmask(r300
);
668 dst_view
= r300_create_surface_custom(pipe
, dst
, &dst_templ
, dst_width0
, dst_height0
);
669 src_view
= r300_create_sampler_view_custom(pipe
, src
, &src_templ
, src_width0
, src_height0
);
671 u_box_3d(dstx
, dsty
, dstz
, abs(src_box
->width
), abs(src_box
->height
),
672 abs(src_box
->depth
), &dstbox
);
674 r300_blitter_begin(r300
, R300_COPY
);
675 util_blitter_blit_generic(r300
->blitter
, dst_view
, &dstbox
,
676 src_view
, src_box
, src_width0
, src_height0
,
677 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
,
679 r300_blitter_end(r300
);
681 pipe_surface_reference(&dst_view
, NULL
);
682 pipe_sampler_view_reference(&src_view
, NULL
);
685 static boolean
r300_is_simple_msaa_resolve(const struct pipe_blit_info
*info
)
687 unsigned dst_width
= u_minify(info
->dst
.resource
->width0
, info
->dst
.level
);
688 unsigned dst_height
= u_minify(info
->dst
.resource
->height0
, info
->dst
.level
);
690 return info
->src
.resource
->nr_samples
> 1 &&
691 info
->dst
.resource
->nr_samples
<= 1 &&
692 info
->dst
.resource
->format
== info
->src
.resource
->format
&&
693 info
->dst
.resource
->format
== info
->dst
.format
&&
694 info
->src
.resource
->format
== info
->src
.format
&&
695 !info
->scissor_enable
&&
696 info
->mask
== PIPE_MASK_RGBA
&&
697 dst_width
== info
->src
.resource
->width0
&&
698 dst_height
== info
->src
.resource
->height0
&&
699 info
->dst
.box
.x
== 0 &&
700 info
->dst
.box
.y
== 0 &&
701 info
->dst
.box
.width
== dst_width
&&
702 info
->dst
.box
.height
== dst_height
&&
703 info
->src
.box
.x
== 0 &&
704 info
->src
.box
.y
== 0 &&
705 info
->src
.box
.width
== dst_width
&&
706 info
->src
.box
.height
== dst_height
&&
707 (r300_resource(info
->dst
.resource
)->tex
.microtile
!= RADEON_LAYOUT_LINEAR
||
708 r300_resource(info
->dst
.resource
)->tex
.macrotile
[info
->dst
.level
] != RADEON_LAYOUT_LINEAR
);
711 static void r300_simple_msaa_resolve(struct pipe_context
*pipe
,
712 struct pipe_resource
*dst
,
715 struct pipe_resource
*src
,
716 enum pipe_format format
)
718 struct r300_context
*r300
= r300_context(pipe
);
719 struct r300_surface
*srcsurf
, *dstsurf
;
720 struct pipe_surface surf_tmpl
;
721 struct r300_aa_state
*aa
= (struct r300_aa_state
*)r300
->aa_state
.state
;
723 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
724 surf_tmpl
.format
= format
;
725 srcsurf
= r300_surface(pipe
->create_surface(pipe
, src
, &surf_tmpl
));
727 surf_tmpl
.format
= format
;
728 surf_tmpl
.u
.tex
.level
= dst_level
;
729 surf_tmpl
.u
.tex
.first_layer
=
730 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
731 dstsurf
= r300_surface(pipe
->create_surface(pipe
, dst
, &surf_tmpl
));
733 /* COLORPITCH should contain the tiling info of the resolve buffer.
734 * The tiling of the AA buffer isn't programmable anyway. */
735 srcsurf
->pitch
&= ~(R300_COLOR_TILE(1) | R300_COLOR_MICROTILE(3));
736 srcsurf
->pitch
|= dstsurf
->pitch
& (R300_COLOR_TILE(1) | R300_COLOR_MICROTILE(3));
738 /* Enable AA resolve. */
740 r300
->aa_state
.size
= 8;
741 r300_mark_atom_dirty(r300
, &r300
->aa_state
);
743 /* Resolve the surface. */
744 r300_blitter_begin(r300
, R300_CLEAR_SURFACE
);
745 util_blitter_custom_color(r300
->blitter
, &srcsurf
->base
, NULL
);
746 r300_blitter_end(r300
);
748 /* Disable AA resolve. */
750 r300
->aa_state
.size
= 4;
751 r300_mark_atom_dirty(r300
, &r300
->aa_state
);
753 pipe_surface_reference((struct pipe_surface
**)&srcsurf
, NULL
);
754 pipe_surface_reference((struct pipe_surface
**)&dstsurf
, NULL
);
757 static void r300_msaa_resolve(struct pipe_context
*pipe
,
758 const struct pipe_blit_info
*info
)
760 struct r300_context
*r300
= r300_context(pipe
);
761 struct pipe_screen
*screen
= pipe
->screen
;
762 struct pipe_resource
*tmp
, templ
;
763 struct pipe_blit_info blit
;
765 assert(info
->src
.level
== 0);
766 assert(info
->src
.box
.z
== 0);
767 assert(info
->src
.box
.depth
== 1);
768 assert(info
->dst
.box
.depth
== 1);
770 if (r300_is_simple_msaa_resolve(info
)) {
771 r300_simple_msaa_resolve(pipe
, info
->dst
.resource
, info
->dst
.level
,
772 info
->dst
.box
.z
, info
->src
.resource
,
777 /* resolve into a temporary texture, then blit */
778 memset(&templ
, 0, sizeof(templ
));
779 templ
.target
= PIPE_TEXTURE_2D
;
780 templ
.format
= info
->src
.resource
->format
;
781 templ
.width0
= info
->src
.resource
->width0
;
782 templ
.height0
= info
->src
.resource
->height0
;
784 templ
.array_size
= 1;
785 templ
.usage
= PIPE_USAGE_DEFAULT
;
786 templ
.flags
= R300_RESOURCE_FORCE_MICROTILING
;
788 tmp
= screen
->resource_create(screen
, &templ
);
791 r300_simple_msaa_resolve(pipe
, tmp
, 0, 0, info
->src
.resource
,
796 blit
.src
.resource
= tmp
;
799 r300_blitter_begin(r300
, R300_BLIT
| R300_IGNORE_RENDER_COND
);
800 util_blitter_blit(r300
->blitter
, &blit
);
801 r300_blitter_end(r300
);
803 pipe_resource_reference(&tmp
, NULL
);
806 static void r300_blit(struct pipe_context
*pipe
,
807 const struct pipe_blit_info
*blit
)
809 struct r300_context
*r300
= r300_context(pipe
);
810 struct pipe_framebuffer_state
*fb
=
811 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
812 struct pipe_blit_info info
= *blit
;
814 /* The driver supports sRGB textures but not framebuffers. Blitting
815 * from sRGB to sRGB should be the same as blitting from linear
816 * to linear, so use that, This avoids incorrect linearization.
818 if (util_format_is_srgb(info
.src
.format
)) {
819 info
.src
.format
= util_format_linear(info
.src
.format
);
820 info
.dst
.format
= util_format_linear(info
.dst
.format
);
824 if (info
.src
.resource
->nr_samples
> 1 &&
825 !util_format_is_depth_or_stencil(info
.src
.resource
->format
)) {
826 r300_msaa_resolve(pipe
, &info
);
830 /* Can't read MSAA textures. */
831 if (info
.src
.resource
->nr_samples
> 1) {
835 /* Blit a combined depth-stencil resource as color.
836 * S8Z24 is the only supported stencil format. */
837 if ((info
.mask
& PIPE_MASK_S
) &&
838 info
.src
.format
== PIPE_FORMAT_S8_UINT_Z24_UNORM
&&
839 info
.dst
.format
== PIPE_FORMAT_S8_UINT_Z24_UNORM
) {
840 if (info
.dst
.resource
->nr_samples
> 1) {
841 /* Cannot do that with MSAA buffers. */
842 info
.mask
&= ~PIPE_MASK_S
;
843 if (!(info
.mask
& PIPE_MASK_Z
)) {
847 /* Single-sample buffer. */
848 info
.src
.format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
849 info
.dst
.format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
850 if (info
.mask
& PIPE_MASK_Z
) {
851 info
.mask
= PIPE_MASK_RGBA
; /* depth+stencil */
853 info
.mask
= PIPE_MASK_B
; /* stencil only */
858 /* Decompress ZMASK. */
859 if (r300
->zmask_in_use
&& !r300
->locked_zbuffer
) {
860 if (fb
->zsbuf
->texture
== info
.src
.resource
||
861 fb
->zsbuf
->texture
== info
.dst
.resource
) {
862 r300_decompress_zmask(r300
);
866 r300_blitter_begin(r300
, R300_BLIT
|
867 (info
.render_condition_enable
? 0 : R300_IGNORE_RENDER_COND
));
868 util_blitter_blit(r300
->blitter
, &info
);
869 r300_blitter_end(r300
);
872 static void r300_flush_resource(struct pipe_context
*ctx
,
873 struct pipe_resource
*resource
)
877 void r300_init_blit_functions(struct r300_context
*r300
)
879 r300
->context
.clear
= r300_clear
;
880 r300
->context
.clear_render_target
= r300_clear_render_target
;
881 r300
->context
.clear_depth_stencil
= r300_clear_depth_stencil
;
882 r300
->context
.resource_copy_region
= r300_resource_copy_region
;
883 r300
->context
.blit
= r300_blit
;
884 r300
->context
.flush_resource
= r300_flush_resource
;