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_hyperz.h"
26 #include "r300_texture.h"
27 #include "r300_winsys.h"
29 #include "util/u_format.h"
30 #include "util/u_pack_color.h"
32 enum r300_blitter_op
/* bitmask */
35 R300_CLEAR_SURFACE
= 2,
39 static void r300_blitter_begin(struct r300_context
* r300
, enum r300_blitter_op op
)
41 if (r300
->query_current
) {
42 r300
->blitter_saved_query
= r300
->query_current
;
43 r300_stop_query(r300
);
46 /* Yeah we have to save all those states to ensure the blitter operation
47 * is really transparent. The states will be restored by the blitter once
49 util_blitter_save_blend(r300
->blitter
, r300
->blend_state
.state
);
50 util_blitter_save_depth_stencil_alpha(r300
->blitter
, r300
->dsa_state
.state
);
51 util_blitter_save_stencil_ref(r300
->blitter
, &(r300
->stencil_ref
));
52 util_blitter_save_rasterizer(r300
->blitter
, r300
->rs_state
.state
);
53 util_blitter_save_fragment_shader(r300
->blitter
, r300
->fs
.state
);
54 util_blitter_save_vertex_shader(r300
->blitter
, r300
->vs_state
.state
);
55 util_blitter_save_viewport(r300
->blitter
, &r300
->viewport
);
56 util_blitter_save_clip(r300
->blitter
, (struct pipe_clip_state
*)r300
->clip_state
.state
);
57 util_blitter_save_vertex_elements(r300
->blitter
, r300
->velems
);
58 util_blitter_save_vertex_buffers(r300
->blitter
, r300
->vertex_buffer_count
,
61 if (op
& (R300_CLEAR_SURFACE
| R300_COPY
))
62 util_blitter_save_framebuffer(r300
->blitter
, r300
->fb_state
.state
);
65 struct r300_textures_state
* state
=
66 (struct r300_textures_state
*)r300
->textures_state
.state
;
68 util_blitter_save_fragment_sampler_states(
69 r300
->blitter
, state
->sampler_state_count
,
70 (void**)state
->sampler_states
);
72 util_blitter_save_fragment_sampler_views(
73 r300
->blitter
, state
->sampler_view_count
,
74 (struct pipe_sampler_view
**)state
->sampler_views
);
78 static void r300_blitter_end(struct r300_context
*r300
)
80 if (r300
->blitter_saved_query
) {
81 r300_resume_query(r300
, r300
->blitter_saved_query
);
82 r300
->blitter_saved_query
= NULL
;
86 static uint32_t r300_depth_clear_cb_value(enum pipe_format format
,
90 util_pack_color(rgba
, format
, &uc
);
92 if (util_format_get_blocksizebits(format
) == 32)
95 return uc
.us
| (uc
.us
<< 16);
98 static boolean
r300_cbzb_clear_allowed(struct r300_context
*r300
,
99 unsigned clear_buffers
)
101 struct pipe_framebuffer_state
*fb
=
102 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
104 /* Only color clear allowed, and only one colorbuffer. */
105 if (clear_buffers
!= PIPE_CLEAR_COLOR
|| fb
->nr_cbufs
!= 1)
108 return r300_surface(fb
->cbufs
[0])->cbzb_allowed
;
111 static uint32_t r300_depth_clear_value(enum pipe_format format
,
112 double depth
, unsigned stencil
)
115 case PIPE_FORMAT_Z16_UNORM
:
116 case PIPE_FORMAT_X8Z24_UNORM
:
117 return util_pack_z(format
, depth
);
119 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
120 return util_pack_z_stencil(format
, depth
, stencil
);
128 /* Clear currently bound buffers. */
129 static void r300_clear(struct pipe_context
* pipe
,
135 /* My notes about fastfill:
137 * 1) Only the zbuffer is cleared.
139 * 2) The zbuffer must be micro-tiled and whole microtiles must be
140 * written. If microtiling is disabled, it locks up.
142 * 3) There is Z Mask RAM which contains a compressed zbuffer and
143 * it interacts with fastfill. We should figure out how to use it
144 * to get more performance.
145 * This is what we know about the Z Mask:
147 * Each dword of the Z Mask contains compression information
148 * for 16 4x4 pixel blocks, that is 2 bits for each block.
149 * On chips with 2 Z pipes, every other dword maps to a different
152 * 4) ZB_DEPTHCLEARVALUE is used to clear the zbuffer and the Z Mask must
153 * be equal to 0. (clear the Z Mask RAM with zeros)
155 * 5) For 16-bit zbuffer, compression causes a hung with one or
156 * two samples and should not be used.
158 * 6) FORCE_COMPRESSED_STENCIL_VALUE should be enabled for stencil clears
159 * to avoid needless decompression.
161 * 7) Fastfill must not be used if reading of compressed Z data is disabled
162 * and writing of compressed Z data is enabled (RD/WR_COMP_ENABLE),
163 * i.e. it cannot be used to compress the zbuffer.
165 * 8) ZB_CB_CLEAR does not interact with fastfill in any way.
170 struct r300_context
* r300
= r300_context(pipe
);
171 struct pipe_framebuffer_state
*fb
=
172 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
173 struct r300_hyperz_state
*hyperz
=
174 (struct r300_hyperz_state
*)r300
->hyperz_state
.state
;
175 struct r300_texture
*zstex
=
176 fb
->zsbuf
? r300_texture(fb
->zsbuf
->texture
) : NULL
;
177 uint32_t width
= fb
->width
;
178 uint32_t height
= fb
->height
;
179 boolean can_hyperz
= r300
->rws
->get_value(r300
->rws
, R300_CAN_HYPERZ
);
180 uint32_t hyperz_dcv
= hyperz
->zb_depthclearvalue
;
182 /* Decompress zbuffers that are bound as textures. If we didn't flush here,
183 * it would happen inside the blitter when updating derived state,
184 * causing a blitter operation to be called from inside the blitter,
185 * which would overwrite saved states and they would never get restored. */
186 r300_flush_depth_textures(r300
);
188 /* Enable fast Z clear.
189 * The zbuffer must be in micro-tiled mode, otherwise it locks up. */
190 if ((buffers
& PIPE_CLEAR_DEPTHSTENCIL
) && can_hyperz
) {
191 hyperz_dcv
= hyperz
->zb_depthclearvalue
=
192 r300_depth_clear_value(fb
->zsbuf
->format
, depth
, stencil
);
194 r300_mark_fb_state_dirty(r300
, R300_CHANGED_ZCLEAR_FLAG
);
195 if (zstex
->zmask_mem
[fb
->zsbuf
->u
.tex
.level
]) {
196 r300_mark_atom_dirty(r300
, &r300
->zmask_clear
);
197 buffers
&= ~PIPE_CLEAR_DEPTHSTENCIL
;
199 if (zstex
->hiz_mem
[fb
->zsbuf
->u
.tex
.level
])
200 r300_mark_atom_dirty(r300
, &r300
->hiz_clear
);
203 /* Enable CBZB clear. */
204 if (r300_cbzb_clear_allowed(r300
, buffers
)) {
205 struct r300_surface
*surf
= r300_surface(fb
->cbufs
[0]);
207 hyperz
->zb_depthclearvalue
=
208 r300_depth_clear_cb_value(surf
->base
.format
, rgba
);
210 width
= surf
->cbzb_width
;
211 height
= surf
->cbzb_height
;
213 r300
->cbzb_clear
= TRUE
;
214 r300_mark_fb_state_dirty(r300
, R300_CHANGED_CBZB_FLAG
);
219 /* Clear using the blitter. */
220 r300_blitter_begin(r300
, R300_CLEAR
);
221 util_blitter_clear(r300
->blitter
,
225 buffers
, rgba
, depth
, stencil
);
226 r300_blitter_end(r300
);
227 } else if (r300
->zmask_clear
.dirty
) {
228 /* Just clear zmask and hiz now, this does not use a standard draw
232 /* Calculate zmask_clear and hiz_clear atom sizes. */
233 r300_update_hyperz_state(r300
);
234 dwords
= r300
->zmask_clear
.size
+
235 (r300
->hiz_clear
.dirty
? r300
->hiz_clear
.size
: 0) +
236 r300_get_num_cs_end_dwords(r300
);
238 /* Reserve CS space. */
239 if (dwords
> (R300_MAX_CMDBUF_DWORDS
- r300
->cs
->cdw
)) {
240 r300
->context
.flush(&r300
->context
, 0, NULL
);
243 /* Emit clear packets. */
244 r300_emit_zmask_clear(r300
, r300
->zmask_clear
.size
,
245 r300
->zmask_clear
.state
);
246 r300
->zmask_clear
.dirty
= FALSE
;
247 if (r300
->hiz_clear
.dirty
) {
248 r300_emit_hiz_clear(r300
, r300
->hiz_clear
.size
,
249 r300
->hiz_clear
.state
);
250 r300
->hiz_clear
.dirty
= FALSE
;
256 /* Disable CBZB clear. */
257 if (r300
->cbzb_clear
) {
258 r300
->cbzb_clear
= FALSE
;
259 hyperz
->zb_depthclearvalue
= hyperz_dcv
;
260 r300_mark_fb_state_dirty(r300
, R300_CHANGED_CBZB_FLAG
);
263 /* Enable fastfill and/or hiz.
265 * If we cleared zmask/hiz, it's in use now. The Hyper-Z state update
266 * looks if zmask/hiz is in use and enables fastfill accordingly. */
268 (zstex
->zmask_in_use
[fb
->zsbuf
->u
.tex
.level
] ||
269 zstex
->hiz_in_use
[fb
->zsbuf
->u
.tex
.level
])) {
270 r300_mark_atom_dirty(r300
, &r300
->hyperz_state
);
274 /* Clear a region of a color surface to a constant value. */
275 static void r300_clear_render_target(struct pipe_context
*pipe
,
276 struct pipe_surface
*dst
,
278 unsigned dstx
, unsigned dsty
,
279 unsigned width
, unsigned height
)
281 struct r300_context
*r300
= r300_context(pipe
);
283 /* Decompress zbuffers that are bound as textures. If we didn't flush here,
284 * it would happen inside the blitter when updating derived state,
285 * causing a blitter operation to be called from inside the blitter,
286 * which would overwrite saved states and they would never get restored. */
287 r300_flush_depth_textures(r300
);
289 r300_blitter_begin(r300
, R300_CLEAR_SURFACE
);
290 util_blitter_clear_render_target(r300
->blitter
, dst
, rgba
,
291 dstx
, dsty
, width
, height
);
292 r300_blitter_end(r300
);
295 /* Clear a region of a depth stencil surface. */
296 static void r300_clear_depth_stencil(struct pipe_context
*pipe
,
297 struct pipe_surface
*dst
,
298 unsigned clear_flags
,
301 unsigned dstx
, unsigned dsty
,
302 unsigned width
, unsigned height
)
304 struct r300_context
*r300
= r300_context(pipe
);
306 /* Decompress zbuffers that are bound as textures. If we didn't flush here,
307 * it would happen inside the blitter when updating derived state,
308 * causing a blitter operation to be called from inside the blitter,
309 * which would overwrite saved states and they would never get restored. */
310 r300_flush_depth_textures(r300
);
312 r300_blitter_begin(r300
, R300_CLEAR_SURFACE
);
313 util_blitter_clear_depth_stencil(r300
->blitter
, dst
, clear_flags
, depth
, stencil
,
314 dstx
, dsty
, width
, height
);
315 r300_blitter_end(r300
);
318 /* Flush a depth stencil buffer. */
319 static void r300_flush_depth_stencil(struct pipe_context
*pipe
,
320 struct pipe_resource
*dst
,
324 struct r300_context
*r300
= r300_context(pipe
);
325 struct pipe_surface
*dstsurf
, surf_tmpl
;
326 struct r300_texture
*tex
= r300_texture(dst
);
328 if (!tex
->zmask_mem
[level
])
330 if (!tex
->zmask_in_use
[level
])
333 surf_tmpl
.format
= dst
->format
;
334 surf_tmpl
.usage
= PIPE_BIND_DEPTH_STENCIL
;
335 surf_tmpl
.u
.tex
.level
= level
;
336 surf_tmpl
.u
.tex
.first_layer
= layer
;
337 surf_tmpl
.u
.tex
.last_layer
= layer
;
338 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
340 r300
->z_decomp_rd
= TRUE
;
342 r300_blitter_begin(r300
, R300_CLEAR_SURFACE
);
343 util_blitter_flush_depth_stencil(r300
->blitter
, dstsurf
);
344 r300_blitter_end(r300
);
345 r300
->z_decomp_rd
= FALSE
;
347 tex
->zmask_in_use
[level
] = FALSE
;
348 pipe_surface_reference(&dstsurf
, NULL
);
351 /* We can't use compressed zbuffers as samplers. */
352 void r300_flush_depth_textures(struct r300_context
*r300
)
354 struct r300_textures_state
*state
=
355 (struct r300_textures_state
*)r300
->textures_state
.state
;
357 unsigned count
= MIN2(state
->sampler_view_count
,
358 state
->sampler_state_count
);
360 if (r300
->z_decomp_rd
)
363 for (i
= 0; i
< count
; i
++)
364 if (state
->sampler_views
[i
] && state
->sampler_states
[i
]) {
365 struct pipe_resource
*tex
= state
->sampler_views
[i
]->base
.texture
;
367 if (tex
->target
== PIPE_TEXTURE_3D
||
368 tex
->target
== PIPE_TEXTURE_CUBE
)
371 /* Ignore non-depth textures.
372 * Also ignore reinterpreted depth textures, e.g. resource_copy. */
373 if (!util_format_is_depth_or_stencil(tex
->format
))
376 for (level
= 0; level
<= tex
->last_level
; level
++)
377 if (r300_texture(tex
)->zmask_in_use
[level
]) {
378 /* We don't handle 3D textures and cubemaps yet. */
379 r300_flush_depth_stencil(&r300
->context
, tex
, level
, 0);
384 /* Copy a block of pixels from one surface to another using HW. */
385 static void r300_hw_copy_region(struct pipe_context
* pipe
,
386 struct pipe_resource
*dst
,
388 unsigned dstx
, unsigned dsty
, unsigned dstz
,
389 struct pipe_resource
*src
,
391 const struct pipe_box
*src_box
)
393 struct r300_context
* r300
= r300_context(pipe
);
395 r300_blitter_begin(r300
, R300_COPY
);
398 util_blitter_copy_region(r300
->blitter
, dst
, dst_level
, dstx
, dsty
, dstz
,
399 src
, src_level
, src_box
, TRUE
);
400 r300_blitter_end(r300
);
403 /* Copy a block of pixels from one surface to another. */
404 static void r300_resource_copy_region(struct pipe_context
*pipe
,
405 struct pipe_resource
*dst
,
407 unsigned dstx
, unsigned dsty
, unsigned dstz
,
408 struct pipe_resource
*src
,
410 const struct pipe_box
*src_box
)
412 enum pipe_format old_format
= dst
->format
;
413 enum pipe_format new_format
= old_format
;
416 if (!pipe
->screen
->is_format_supported(pipe
->screen
,
417 old_format
, src
->target
,
419 PIPE_BIND_RENDER_TARGET
|
420 PIPE_BIND_SAMPLER_VIEW
, 0) &&
421 util_format_is_plain(old_format
)) {
422 switch (util_format_get_blocksize(old_format
)) {
424 new_format
= PIPE_FORMAT_I8_UNORM
;
427 new_format
= PIPE_FORMAT_B4G4R4A4_UNORM
;
430 new_format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
433 new_format
= PIPE_FORMAT_R16G16B16A16_UNORM
;
436 debug_printf("r300: surface_copy: Unhandled format: %s. Falling back to software.\n"
437 "r300: surface_copy: Software fallback doesn't work for tiled textures.\n",
438 util_format_short_name(old_format
));
442 is_depth
= util_format_get_component_bits(src
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0;
444 r300_flush_depth_stencil(pipe
, src
, src_level
, src_box
->z
);
447 if (old_format
!= new_format
) {
448 r300_texture_reinterpret_format(pipe
->screen
,
450 r300_texture_reinterpret_format(pipe
->screen
,
454 r300_hw_copy_region(pipe
, dst
, dst_level
, dstx
, dsty
, dstz
,
455 src
, src_level
, src_box
);
457 if (old_format
!= new_format
) {
458 r300_texture_reinterpret_format(pipe
->screen
,
460 r300_texture_reinterpret_format(pipe
->screen
,
465 void r300_init_blit_functions(struct r300_context
*r300
)
467 r300
->context
.clear
= r300_clear
;
468 r300
->context
.clear_render_target
= r300_clear_render_target
;
469 r300
->context
.clear_depth_stencil
= r300_clear_depth_stencil
;
470 r300
->context
.resource_copy_region
= r300_resource_copy_region
;