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_texture.h"
25 #include "r300_winsys.h"
27 #include "util/u_format.h"
28 #include "util/u_pack_color.h"
30 enum r300_blitter_op
/* bitmask */
33 R300_CLEAR_SURFACE
= 2,
37 static void r300_blitter_begin(struct r300_context
* r300
, enum r300_blitter_op op
)
39 if (r300
->query_current
) {
40 r300
->blitter_saved_query
= r300
->query_current
;
41 r300_stop_query(r300
);
44 /* Yeah we have to save all those states to ensure the blitter operation
45 * is really transparent. The states will be restored by the blitter once
47 util_blitter_save_blend(r300
->blitter
, r300
->blend_state
.state
);
48 util_blitter_save_depth_stencil_alpha(r300
->blitter
, r300
->dsa_state
.state
);
49 util_blitter_save_stencil_ref(r300
->blitter
, &(r300
->stencil_ref
));
50 util_blitter_save_rasterizer(r300
->blitter
, r300
->rs_state
.state
);
51 util_blitter_save_fragment_shader(r300
->blitter
, r300
->fs
.state
);
52 util_blitter_save_vertex_shader(r300
->blitter
, r300
->vs_state
.state
);
53 util_blitter_save_viewport(r300
->blitter
, &r300
->viewport
);
54 util_blitter_save_clip(r300
->blitter
, (struct pipe_clip_state
*)r300
->clip_state
.state
);
55 util_blitter_save_vertex_elements(r300
->blitter
, r300
->velems
);
56 util_blitter_save_vertex_buffers(r300
->blitter
, r300
->vertex_buffer_count
,
59 if (op
& (R300_CLEAR_SURFACE
| R300_COPY
))
60 util_blitter_save_framebuffer(r300
->blitter
, r300
->fb_state
.state
);
63 struct r300_textures_state
* state
=
64 (struct r300_textures_state
*)r300
->textures_state
.state
;
66 util_blitter_save_fragment_sampler_states(
67 r300
->blitter
, state
->sampler_state_count
,
68 (void**)state
->sampler_states
);
70 util_blitter_save_fragment_sampler_views(
71 r300
->blitter
, state
->sampler_view_count
,
72 (struct pipe_sampler_view
**)state
->sampler_views
);
76 static void r300_blitter_end(struct r300_context
*r300
)
78 if (r300
->blitter_saved_query
) {
79 r300_resume_query(r300
, r300
->blitter_saved_query
);
80 r300
->blitter_saved_query
= NULL
;
84 static uint32_t r300_depth_clear_cb_value(enum pipe_format format
,
88 util_pack_color(rgba
, format
, &uc
);
90 if (util_format_get_blocksizebits(format
) == 32)
93 return uc
.us
| (uc
.us
<< 16);
96 static boolean
r300_cbzb_clear_allowed(struct r300_context
*r300
,
97 unsigned clear_buffers
)
99 struct pipe_framebuffer_state
*fb
=
100 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
102 if (r300
->z_fastfill
)
103 clear_buffers
&= ~(PIPE_CLEAR_DEPTH
| PIPE_CLEAR_STENCIL
);
105 /* Only color clear allowed, and only one colorbuffer. */
106 if (clear_buffers
!= PIPE_CLEAR_COLOR
|| fb
->nr_cbufs
!= 1)
109 return r300_surface(fb
->cbufs
[0])->cbzb_allowed
;
112 static uint32_t r300_depth_clear_value(enum pipe_format format
,
113 double depth
, unsigned stencil
)
116 case PIPE_FORMAT_Z16_UNORM
:
117 case PIPE_FORMAT_X8Z24_UNORM
:
118 return util_pack_z(format
, depth
);
120 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
121 return util_pack_z_stencil(format
, depth
, stencil
);
129 /* Clear currently bound buffers. */
130 static void r300_clear(struct pipe_context
* pipe
,
136 /* My notes about fastfill:
138 * 1) Only the zbuffer is cleared.
140 * 2) The zbuffer must be micro-tiled and whole microtiles must be
141 * written. If microtiling is disabled, it locks up.
143 * 3) There is Z Mask RAM which contains a compressed zbuffer and
144 * it interacts with fastfill. We should figure out how to use it
145 * to get more performance.
146 * This is what we know about the Z Mask:
148 * Each dword of the Z Mask contains compression information
149 * for 16 4x4 pixel blocks, that is 2 bits for each block.
150 * On chips with 2 Z pipes, every other dword maps to a different
153 * 4) ZB_DEPTHCLEARVALUE is used to clear the zbuffer and the Z Mask must
154 * be equal to 0. (clear the Z Mask RAM with zeros)
156 * 5) For 16-bit zbuffer, compression causes a hung with one or
157 * two samples and should not be used.
159 * 6) FORCE_COMPRESSED_STENCIL_VALUE should be enabled for stencil clears
160 * to avoid needless decompression.
162 * 7) Fastfill must not be used if reading of compressed Z data is disabled
163 * and writing of compressed Z data is enabled (RD/WR_COMP_ENABLE),
164 * i.e. it cannot be used to compress the zbuffer.
166 * 8) ZB_CB_CLEAR does not interact with fastfill in any way.
171 struct r300_context
* r300
= r300_context(pipe
);
172 struct pipe_framebuffer_state
*fb
=
173 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
174 struct r300_hyperz_state
*hyperz
=
175 (struct r300_hyperz_state
*)r300
->hyperz_state
.state
;
176 uint32_t width
= fb
->width
;
177 uint32_t height
= fb
->height
;
178 boolean has_hyperz
= r300
->rws
->get_value(r300
->rws
, R300_CAN_HYPERZ
);
179 uint32_t hyperz_dcv
= 0;
181 /* Enable fast Z clear.
182 * The zbuffer must be in micro-tiled mode, otherwise it locks up. */
183 if ((buffers
& (PIPE_CLEAR_DEPTH
|PIPE_CLEAR_STENCIL
)) && has_hyperz
) {
185 hyperz_dcv
= hyperz
->zb_depthclearvalue
=
186 r300_depth_clear_value(fb
->zsbuf
->format
, depth
, stencil
);
188 r300_mark_fb_state_dirty(r300
, R300_CHANGED_ZCLEAR_FLAG
);
189 if (r300
->z_compression
|| r300
->z_fastfill
)
190 r300
->zmask_clear
.dirty
= TRUE
;
191 if (r300
->hiz_enable
)
192 r300
->hiz_clear
.dirty
= TRUE
;
195 /* Enable CBZB clear. */
196 if (r300_cbzb_clear_allowed(r300
, buffers
)) {
197 struct r300_surface
*surf
= r300_surface(fb
->cbufs
[0]);
199 hyperz
->zb_depthclearvalue
=
200 r300_depth_clear_cb_value(surf
->base
.format
, rgba
);
202 width
= surf
->cbzb_width
;
203 height
= surf
->cbzb_height
;
205 r300
->cbzb_clear
= TRUE
;
206 r300_mark_fb_state_dirty(r300
, R300_CHANGED_CBZB_FLAG
);
210 r300_blitter_begin(r300
, R300_CLEAR
);
211 util_blitter_clear(r300
->blitter
,
215 buffers
, rgba
, depth
, stencil
);
216 r300_blitter_end(r300
);
218 /* Disable CBZB clear. */
219 if (r300
->cbzb_clear
) {
220 r300
->cbzb_clear
= FALSE
;
221 hyperz
->zb_depthclearvalue
= hyperz_dcv
;
222 r300_mark_fb_state_dirty(r300
, R300_CHANGED_CBZB_FLAG
);
225 /* XXX this flush "fixes" a hardlock in the cubestorm xscreensaver */
226 if (r300
->flush_counter
== 0)
227 pipe
->flush(pipe
, 0, NULL
);
230 /* Clear a region of a color surface to a constant value. */
231 static void r300_clear_render_target(struct pipe_context
*pipe
,
232 struct pipe_surface
*dst
,
234 unsigned dstx
, unsigned dsty
,
235 unsigned width
, unsigned height
)
237 struct r300_context
*r300
= r300_context(pipe
);
239 r300_blitter_begin(r300
, R300_CLEAR_SURFACE
);
240 util_blitter_clear_render_target(r300
->blitter
, dst
, rgba
,
241 dstx
, dsty
, width
, height
);
242 r300_blitter_end(r300
);
245 /* Clear a region of a depth stencil surface. */
246 static void r300_clear_depth_stencil(struct pipe_context
*pipe
,
247 struct pipe_surface
*dst
,
248 unsigned clear_flags
,
251 unsigned dstx
, unsigned dsty
,
252 unsigned width
, unsigned height
)
254 struct r300_context
*r300
= r300_context(pipe
);
256 r300_blitter_begin(r300
, R300_CLEAR_SURFACE
);
257 util_blitter_clear_depth_stencil(r300
->blitter
, dst
, clear_flags
, depth
, stencil
,
258 dstx
, dsty
, width
, height
);
259 r300_blitter_end(r300
);
262 /* Flush a depth stencil buffer. */
263 void r300_flush_depth_stencil(struct pipe_context
*pipe
,
264 struct pipe_resource
*dst
,
265 struct pipe_subresource subdst
,
268 struct r300_context
*r300
= r300_context(pipe
);
269 struct pipe_surface
*dstsurf
;
270 struct r300_texture
*tex
= r300_texture(dst
);
272 if (!tex
->zmask_mem
[subdst
.level
])
274 if (!tex
->dirty_zmask
[subdst
.level
])
277 dstsurf
= pipe
->screen
->get_tex_surface(pipe
->screen
, dst
,
278 subdst
.face
, subdst
.level
, zslice
,
279 PIPE_BIND_DEPTH_STENCIL
);
280 r300
->z_decomp_rd
= TRUE
;
281 r300_blitter_begin(r300
, R300_CLEAR_SURFACE
);
282 util_blitter_clear_depth_stencil(r300
->blitter
, dstsurf
,
283 PIPE_CLEAR_DEPTH
, 0, 0,
284 0, 0, dstsurf
->width
, dstsurf
->height
);
285 r300_blitter_end(r300
);
286 r300
->z_decomp_rd
= FALSE
;
288 tex
->dirty_zmask
[subdst
.level
] = FALSE
;
291 /* Copy a block of pixels from one surface to another using HW. */
292 static void r300_hw_copy_region(struct pipe_context
* pipe
,
293 struct pipe_resource
*dst
,
294 struct pipe_subresource subdst
,
295 unsigned dstx
, unsigned dsty
, unsigned dstz
,
296 struct pipe_resource
*src
,
297 struct pipe_subresource subsrc
,
298 unsigned srcx
, unsigned srcy
, unsigned srcz
,
299 unsigned width
, unsigned height
)
301 struct r300_context
* r300
= r300_context(pipe
);
303 r300_blitter_begin(r300
, R300_COPY
);
304 util_blitter_copy_region(r300
->blitter
, dst
, subdst
, dstx
, dsty
, dstz
,
305 src
, subsrc
, srcx
, srcy
, srcz
, width
, height
,
307 r300_blitter_end(r300
);
310 /* Copy a block of pixels from one surface to another. */
311 static void r300_resource_copy_region(struct pipe_context
*pipe
,
312 struct pipe_resource
*dst
,
313 struct pipe_subresource subdst
,
314 unsigned dstx
, unsigned dsty
, unsigned dstz
,
315 struct pipe_resource
*src
,
316 struct pipe_subresource subsrc
,
317 unsigned srcx
, unsigned srcy
, unsigned srcz
,
318 unsigned width
, unsigned height
)
320 enum pipe_format old_format
= dst
->format
;
321 enum pipe_format new_format
= old_format
;
323 if (!pipe
->screen
->is_format_supported(pipe
->screen
,
324 old_format
, src
->target
,
326 PIPE_BIND_RENDER_TARGET
|
327 PIPE_BIND_SAMPLER_VIEW
, 0) &&
328 util_format_is_plain(old_format
)) {
329 switch (util_format_get_blocksize(old_format
)) {
331 new_format
= PIPE_FORMAT_I8_UNORM
;
334 new_format
= PIPE_FORMAT_B4G4R4A4_UNORM
;
337 new_format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
340 new_format
= PIPE_FORMAT_R16G16B16A16_UNORM
;
343 debug_printf("r300: surface_copy: Unhandled format: %s. Falling back to software.\n"
344 "r300: surface_copy: Software fallback doesn't work for tiled textures.\n",
345 util_format_short_name(old_format
));
349 is_depth
= util_format_get_component_bits(src
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0;
351 r300_flush_depth_stencil(pipe
, src
, subsrc
, srcz
);
353 if (old_format
!= new_format
) {
354 dst
->format
= new_format
;
355 src
->format
= new_format
;
357 r300_texture_reinterpret_format(pipe
->screen
,
359 r300_texture_reinterpret_format(pipe
->screen
,
363 r300_hw_copy_region(pipe
, dst
, subdst
, dstx
, dsty
, dstz
,
364 src
, subsrc
, srcx
, srcy
, srcz
, width
, height
);
366 if (old_format
!= new_format
) {
367 dst
->format
= old_format
;
368 src
->format
= old_format
;
370 r300_texture_reinterpret_format(pipe
->screen
,
372 r300_texture_reinterpret_format(pipe
->screen
,
377 void r300_init_blit_functions(struct r300_context
*r300
)
379 r300
->context
.clear
= r300_clear
;
380 r300
->context
.clear_render_target
= r300_clear_render_target
;
381 r300
->context
.clear_depth_stencil
= r300_clear_depth_stencil
;
382 r300
->context
.resource_copy_region
= r300_resource_copy_region
;