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"
26 #include "util/u_format.h"
27 #include "util/u_pack_color.h"
29 enum r300_blitter_op
/* bitmask */
32 R300_CLEAR_SURFACE
= 2,
36 static void r300_blitter_begin(struct r300_context
* r300
, enum r300_blitter_op op
)
38 if (r300
->query_current
) {
39 r300
->blitter_saved_query
= r300
->query_current
;
40 r300_stop_query(r300
);
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
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_clip(r300
->blitter
, (struct pipe_clip_state
*)r300
->clip_state
.state
);
54 util_blitter_save_vertex_elements(r300
->blitter
, r300
->velems
);
55 util_blitter_save_vertex_buffers(r300
->blitter
, r300
->vertex_buffer_count
,
58 if (op
& (R300_CLEAR_SURFACE
| R300_COPY
))
59 util_blitter_save_framebuffer(r300
->blitter
, r300
->fb_state
.state
);
62 struct r300_textures_state
* state
=
63 (struct r300_textures_state
*)r300
->textures_state
.state
;
65 util_blitter_save_fragment_sampler_states(
66 r300
->blitter
, state
->sampler_state_count
,
67 (void**)state
->sampler_states
);
69 util_blitter_save_fragment_sampler_views(
70 r300
->blitter
, state
->sampler_view_count
,
71 (struct pipe_sampler_view
**)state
->sampler_views
);
75 static void r300_blitter_end(struct r300_context
*r300
)
77 if (r300
->blitter_saved_query
) {
78 r300_resume_query(r300
, r300
->blitter_saved_query
);
79 r300
->blitter_saved_query
= NULL
;
83 static uint32_t r300_depth_clear_cb_value(enum pipe_format format
,
87 util_pack_color(rgba
, format
, &uc
);
89 if (util_format_get_blocksizebits(format
) == 32)
92 return uc
.us
| (uc
.us
<< 16);
95 static boolean
r300_cbzb_clear_allowed(struct r300_context
*r300
,
96 unsigned clear_buffers
)
98 struct pipe_framebuffer_state
*fb
=
99 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
101 /* Only color clear allowed, and only one colorbuffer. */
102 if (clear_buffers
!= PIPE_CLEAR_COLOR
|| fb
->nr_cbufs
!= 1)
105 return r300_surface(fb
->cbufs
[0])->cbzb_allowed
;
108 /* Clear currently bound buffers. */
109 static void r300_clear(struct pipe_context
* pipe
,
115 /* My notes about fastfill:
117 * 1) Only the zbuffer is cleared.
119 * 2) The zbuffer must be micro-tiled and whole microtiles must be
120 * written. If microtiling is disabled, it locks up.
122 * 3) There is Z Mask RAM which contains a compressed zbuffer and
123 * it interacts with fastfill. We should figure out how to use it
124 * to get more performance.
125 * This is what we know about the Z Mask:
127 * Each dword of the Z Mask contains compression information
128 * for 16 4x4 pixel blocks, that is 2 bits for each block.
129 * On chips with 2 Z pipes, every other dword maps to a different
132 * 4) ZB_DEPTHCLEARVALUE is used to clear the zbuffer and the Z Mask must
133 * be equal to 0. (clear the Z Mask RAM with zeros)
135 * 5) For 16-bit zbuffer, compression causes a hung with one or
136 * two samples and should not be used.
138 * 6) FORCE_COMPRESSED_STENCIL_VALUE should be enabled for stencil clears
139 * to avoid needless decompression.
141 * 7) Fastfill must not be used if reading of compressed Z data is disabled
142 * and writing of compressed Z data is enabled (RD/WR_COMP_ENABLE),
143 * i.e. it cannot be used to compress the zbuffer.
145 * 8) ZB_CB_CLEAR does not interact with fastfill in any way.
150 struct r300_context
* r300
= r300_context(pipe
);
151 struct pipe_framebuffer_state
*fb
=
152 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
153 struct r300_hyperz_state
*hyperz
=
154 (struct r300_hyperz_state
*)r300
->hyperz_state
.state
;
155 uint32_t width
= fb
->width
;
156 uint32_t height
= fb
->height
;
158 /* Enable CBZB clear. */
159 if (r300_cbzb_clear_allowed(r300
, buffers
)) {
160 struct r300_surface
*surf
= r300_surface(fb
->cbufs
[0]);
162 hyperz
->zb_depthclearvalue
=
163 r300_depth_clear_cb_value(surf
->base
.format
, rgba
);
165 width
= surf
->cbzb_width
;
166 height
= surf
->cbzb_height
;
168 r300
->cbzb_clear
= TRUE
;
169 r300_mark_fb_state_dirty(r300
, R300_CHANGED_CBZB_FLAG
);
173 r300_blitter_begin(r300
, R300_CLEAR
);
174 util_blitter_clear(r300
->blitter
,
178 buffers
, rgba
, depth
, stencil
);
179 r300_blitter_end(r300
);
181 /* Disable CBZB clear. */
182 if (r300
->cbzb_clear
) {
183 r300
->cbzb_clear
= FALSE
;
184 r300_mark_fb_state_dirty(r300
, R300_CHANGED_CBZB_FLAG
);
187 /* XXX this flush "fixes" a hardlock in the cubestorm xscreensaver */
188 if (r300
->flush_counter
== 0)
189 pipe
->flush(pipe
, 0, NULL
);
192 /* Clear a region of a color surface to a constant value. */
193 static void r300_clear_render_target(struct pipe_context
*pipe
,
194 struct pipe_surface
*dst
,
196 unsigned dstx
, unsigned dsty
,
197 unsigned width
, unsigned height
)
199 struct r300_context
*r300
= r300_context(pipe
);
201 r300_blitter_begin(r300
, R300_CLEAR_SURFACE
);
202 util_blitter_clear_render_target(r300
->blitter
, dst
, rgba
,
203 dstx
, dsty
, width
, height
);
204 r300_blitter_end(r300
);
207 /* Clear a region of a depth stencil surface. */
208 static void r300_clear_depth_stencil(struct pipe_context
*pipe
,
209 struct pipe_surface
*dst
,
210 unsigned clear_flags
,
213 unsigned dstx
, unsigned dsty
,
214 unsigned width
, unsigned height
)
216 struct r300_context
*r300
= r300_context(pipe
);
218 r300_blitter_begin(r300
, R300_CLEAR_SURFACE
);
219 util_blitter_clear_depth_stencil(r300
->blitter
, dst
, clear_flags
, depth
, stencil
,
220 dstx
, dsty
, width
, height
);
221 r300_blitter_end(r300
);
224 /* Copy a block of pixels from one surface to another using HW. */
225 static void r300_hw_copy_region(struct pipe_context
* pipe
,
226 struct pipe_resource
*dst
,
227 struct pipe_subresource subdst
,
228 unsigned dstx
, unsigned dsty
, unsigned dstz
,
229 struct pipe_resource
*src
,
230 struct pipe_subresource subsrc
,
231 unsigned srcx
, unsigned srcy
, unsigned srcz
,
232 unsigned width
, unsigned height
)
234 struct r300_context
* r300
= r300_context(pipe
);
236 r300_blitter_begin(r300
, R300_COPY
);
237 util_blitter_copy_region(r300
->blitter
, dst
, subdst
, dstx
, dsty
, dstz
,
238 src
, subsrc
, srcx
, srcy
, srcz
, width
, height
,
240 r300_blitter_end(r300
);
243 /* Copy a block of pixels from one surface to another. */
244 static void r300_resource_copy_region(struct pipe_context
*pipe
,
245 struct pipe_resource
*dst
,
246 struct pipe_subresource subdst
,
247 unsigned dstx
, unsigned dsty
, unsigned dstz
,
248 struct pipe_resource
*src
,
249 struct pipe_subresource subsrc
,
250 unsigned srcx
, unsigned srcy
, unsigned srcz
,
251 unsigned width
, unsigned height
)
253 enum pipe_format old_format
= dst
->format
;
254 enum pipe_format new_format
= old_format
;
256 if (!pipe
->screen
->is_format_supported(pipe
->screen
,
257 old_format
, src
->target
,
259 PIPE_BIND_RENDER_TARGET
|
260 PIPE_BIND_SAMPLER_VIEW
, 0) &&
261 util_format_is_plain(old_format
)) {
262 switch (util_format_get_blocksize(old_format
)) {
264 new_format
= PIPE_FORMAT_I8_UNORM
;
267 new_format
= PIPE_FORMAT_B4G4R4A4_UNORM
;
270 new_format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
273 new_format
= PIPE_FORMAT_R16G16B16A16_UNORM
;
276 debug_printf("r300: surface_copy: Unhandled format: %s. Falling back to software.\n"
277 "r300: surface_copy: Software fallback doesn't work for tiled textures.\n",
278 util_format_short_name(old_format
));
282 if (old_format
!= new_format
) {
283 dst
->format
= new_format
;
284 src
->format
= new_format
;
286 r300_texture_reinterpret_format(pipe
->screen
,
288 r300_texture_reinterpret_format(pipe
->screen
,
292 r300_hw_copy_region(pipe
, dst
, subdst
, dstx
, dsty
, dstz
,
293 src
, subsrc
, srcx
, srcy
, srcz
, width
, height
);
295 if (old_format
!= new_format
) {
296 dst
->format
= old_format
;
297 src
->format
= old_format
;
299 r300_texture_reinterpret_format(pipe
->screen
,
301 r300_texture_reinterpret_format(pipe
->screen
,
306 void r300_init_blit_functions(struct r300_context
*r300
)
308 r300
->context
.clear
= r300_clear
;
309 r300
->context
.clear_render_target
= r300_clear_render_target
;
310 r300
->context
.clear_depth_stencil
= r300_clear_depth_stencil
;
311 r300
->context
.resource_copy_region
= r300_resource_copy_region
;