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
;
100 struct r300_surface
*surf
= r300_surface(fb
->cbufs
[0]);
103 /* Only color clear allowed, and only one colorbuffer. */
104 if (clear_buffers
!= PIPE_CLEAR_COLOR
|| fb
->nr_cbufs
!= 1)
107 /* The colorbuffer must be point-sampled. */
108 if (surf
->base
.texture
->nr_samples
> 1)
111 bpp
= util_format_get_blocksizebits(surf
->base
.format
);
113 /* ZB can only work with the two pixel sizes. */
114 if (bpp
!= 16 && bpp
!= 32)
117 /* If the midpoint ZB offset is not aligned to 2048, it returns garbage
118 * with certain texture sizes. Macrotiling ensures the alignment. */
119 if (!r300_texture(surf
->base
.texture
)->mip_macrotile
[surf
->base
.level
])
125 /* Clear currently bound buffers. */
126 static void r300_clear(struct pipe_context
* pipe
,
132 /* My notes about fastfill:
134 * 1) Only the zbuffer is cleared.
136 * 2) The zbuffer must be micro-tiled and whole microtiles must be
137 * written. If microtiling is disabled, it locks up.
139 * 3) There is Z Mask RAM which contains a compressed zbuffer and
140 * it interacts with fastfill. We should figure out how to use it
141 * to get more performance.
142 * This is what we know about the Z Mask:
144 * Each dword of the Z Mask contains compression information
145 * for 16 4x4 pixel blocks, that is 2 bits for each block.
146 * On chips with 2 Z pipes, every other dword maps to a different
149 * 4) ZB_DEPTHCLEARVALUE is used to clear the zbuffer and the Z Mask must
150 * be equal to 0. (clear the Z Mask RAM with zeros)
152 * 5) For 16-bit zbuffer, compression causes a hung with one or
153 * two samples and should not be used.
155 * 6) FORCE_COMPRESSED_STENCIL_VALUE should be enabled for stencil clears
156 * to avoid needless decompression.
158 * 7) Fastfill must not be used if reading of compressed Z data is disabled
159 * and writing of compressed Z data is enabled (RD/WR_COMP_ENABLE),
160 * i.e. it cannot be used to compress the zbuffer.
162 * 8) ZB_CB_CLEAR does not interact with fastfill in any way.
167 struct r300_context
* r300
= r300_context(pipe
);
168 struct pipe_framebuffer_state
*fb
=
169 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
170 struct r300_hyperz_state
*hyperz
=
171 (struct r300_hyperz_state
*)r300
->hyperz_state
.state
;
172 uint32_t width
= fb
->width
;
173 uint32_t height
= fb
->height
;
175 /* Enable CBZB clear. */
176 if (r300_cbzb_clear_allowed(r300
, buffers
)) {
177 struct r300_surface
*surf
= r300_surface(fb
->cbufs
[0]);
179 hyperz
->zb_depthclearvalue
=
180 r300_depth_clear_cb_value(surf
->base
.format
, rgba
);
182 width
= surf
->cbzb_width
;
183 height
= surf
->cbzb_height
;
185 r300
->cbzb_clear
= TRUE
;
186 r300_mark_fb_state_dirty(r300
, R300_CHANGED_CBZB_FLAG
);
190 r300_blitter_begin(r300
, R300_CLEAR
);
191 util_blitter_clear(r300
->blitter
,
195 buffers
, rgba
, depth
, stencil
);
196 r300_blitter_end(r300
);
198 /* Disable CBZB clear. */
199 if (r300
->cbzb_clear
) {
200 r300
->cbzb_clear
= FALSE
;
201 r300_mark_fb_state_dirty(r300
, R300_CHANGED_CBZB_FLAG
);
204 /* XXX this flush "fixes" a hardlock in the cubestorm xscreensaver */
205 if (r300
->flush_counter
== 0)
206 pipe
->flush(pipe
, 0, NULL
);
209 /* Clear a region of a color surface to a constant value. */
210 static void r300_clear_render_target(struct pipe_context
*pipe
,
211 struct pipe_surface
*dst
,
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_render_target(r300
->blitter
, dst
, rgba
,
220 dstx
, dsty
, width
, height
);
221 r300_blitter_end(r300
);
224 /* Clear a region of a depth stencil surface. */
225 static void r300_clear_depth_stencil(struct pipe_context
*pipe
,
226 struct pipe_surface
*dst
,
227 unsigned clear_flags
,
230 unsigned dstx
, unsigned dsty
,
231 unsigned width
, unsigned height
)
233 struct r300_context
*r300
= r300_context(pipe
);
235 r300_blitter_begin(r300
, R300_CLEAR_SURFACE
);
236 util_blitter_clear_depth_stencil(r300
->blitter
, dst
, clear_flags
, depth
, stencil
,
237 dstx
, dsty
, width
, height
);
238 r300_blitter_end(r300
);
241 /* Copy a block of pixels from one surface to another using HW. */
242 static void r300_hw_copy_region(struct pipe_context
* pipe
,
243 struct pipe_resource
*dst
,
244 struct pipe_subresource subdst
,
245 unsigned dstx
, unsigned dsty
, unsigned dstz
,
246 struct pipe_resource
*src
,
247 struct pipe_subresource subsrc
,
248 unsigned srcx
, unsigned srcy
, unsigned srcz
,
249 unsigned width
, unsigned height
)
251 struct r300_context
* r300
= r300_context(pipe
);
253 r300_blitter_begin(r300
, R300_COPY
);
254 util_blitter_copy_region(r300
->blitter
, dst
, subdst
, dstx
, dsty
, dstz
,
255 src
, subsrc
, srcx
, srcy
, srcz
, width
, height
,
257 r300_blitter_end(r300
);
260 /* Copy a block of pixels from one surface to another. */
261 static void r300_resource_copy_region(struct pipe_context
*pipe
,
262 struct pipe_resource
*dst
,
263 struct pipe_subresource subdst
,
264 unsigned dstx
, unsigned dsty
, unsigned dstz
,
265 struct pipe_resource
*src
,
266 struct pipe_subresource subsrc
,
267 unsigned srcx
, unsigned srcy
, unsigned srcz
,
268 unsigned width
, unsigned height
)
270 enum pipe_format old_format
= dst
->format
;
271 enum pipe_format new_format
= old_format
;
273 if (!pipe
->screen
->is_format_supported(pipe
->screen
,
274 old_format
, src
->target
,
276 PIPE_BIND_RENDER_TARGET
|
277 PIPE_BIND_SAMPLER_VIEW
, 0) &&
278 util_format_is_plain(old_format
)) {
279 switch (util_format_get_blocksize(old_format
)) {
281 new_format
= PIPE_FORMAT_I8_UNORM
;
284 new_format
= PIPE_FORMAT_B4G4R4A4_UNORM
;
287 new_format
= PIPE_FORMAT_B8G8R8A8_UNORM
;
290 new_format
= PIPE_FORMAT_R16G16B16A16_UNORM
;
293 debug_printf("r300: surface_copy: Unhandled format: %s. Falling back to software.\n"
294 "r300: surface_copy: Software fallback doesn't work for tiled textures.\n",
295 util_format_short_name(old_format
));
299 if (old_format
!= new_format
) {
300 dst
->format
= new_format
;
301 src
->format
= new_format
;
303 r300_texture_reinterpret_format(pipe
->screen
,
305 r300_texture_reinterpret_format(pipe
->screen
,
309 r300_hw_copy_region(pipe
, dst
, subdst
, dstx
, dsty
, dstz
,
310 src
, subsrc
, srcx
, srcy
, srcz
, width
, height
);
312 if (old_format
!= new_format
) {
313 dst
->format
= old_format
;
314 src
->format
= old_format
;
316 r300_texture_reinterpret_format(pipe
->screen
,
318 r300_texture_reinterpret_format(pipe
->screen
,
323 void r300_init_blit_functions(struct r300_context
*r300
)
325 r300
->context
.clear
= r300_clear
;
326 r300
->context
.clear_render_target
= r300_clear_render_target
;
327 r300
->context
.clear_depth_stencil
= r300_clear_depth_stencil
;
328 r300
->context
.resource_copy_region
= r300_resource_copy_region
;