r300g: cleanup texture debug logging
[mesa.git] / src / gallium / drivers / r300 / r300_blit.c
1 /*
2 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
3 *
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:
10 *
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
13 * Software.
14 *
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. */
22
23 #include "r300_context.h"
24 #include "r300_texture.h"
25
26 #include "util/u_format.h"
27 #include "util/u_pack_color.h"
28
29 enum r300_blitter_op /* bitmask */
30 {
31 R300_CLEAR = 1,
32 R300_CLEAR_SURFACE = 2,
33 R300_COPY = 4
34 };
35
36 static void r300_blitter_begin(struct r300_context* r300, enum r300_blitter_op op)
37 {
38 if (r300->query_current) {
39 r300->blitter_saved_query = r300->query_current;
40 r300_stop_query(r300);
41 }
42
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
45 * copying is done. */
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,
56 r300->vertex_buffer);
57
58 if (op & (R300_CLEAR_SURFACE | R300_COPY))
59 util_blitter_save_framebuffer(r300->blitter, r300->fb_state.state);
60
61 if (op & R300_COPY) {
62 struct r300_textures_state* state =
63 (struct r300_textures_state*)r300->textures_state.state;
64
65 util_blitter_save_fragment_sampler_states(
66 r300->blitter, state->sampler_state_count,
67 (void**)state->sampler_states);
68
69 util_blitter_save_fragment_sampler_views(
70 r300->blitter, state->sampler_view_count,
71 (struct pipe_sampler_view**)state->sampler_views);
72 }
73 }
74
75 static void r300_blitter_end(struct r300_context *r300)
76 {
77 if (r300->blitter_saved_query) {
78 r300_resume_query(r300, r300->blitter_saved_query);
79 r300->blitter_saved_query = NULL;
80 }
81 }
82
83 static uint32_t r300_depth_clear_cb_value(enum pipe_format format,
84 const float* rgba)
85 {
86 union util_color uc;
87 util_pack_color(rgba, format, &uc);
88
89 if (util_format_get_blocksizebits(format) == 32)
90 return uc.ui;
91 else
92 return uc.us | (uc.us << 16);
93 }
94
95 static boolean r300_cbzb_clear_allowed(struct r300_context *r300,
96 unsigned clear_buffers)
97 {
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]);
101 unsigned bpp;
102
103 /* Only color clear allowed, and only one colorbuffer. */
104 if (clear_buffers != PIPE_CLEAR_COLOR || fb->nr_cbufs != 1)
105 return FALSE;
106
107 /* The colorbuffer must be point-sampled. */
108 if (surf->base.texture->nr_samples > 1)
109 return FALSE;
110
111 bpp = util_format_get_blocksizebits(surf->base.format);
112
113 /* ZB can only work with the two pixel sizes. */
114 if (bpp != 16 && bpp != 32)
115 return FALSE;
116
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])
120 return FALSE;
121
122 return TRUE;
123 }
124
125 /* Clear currently bound buffers. */
126 static void r300_clear(struct pipe_context* pipe,
127 unsigned buffers,
128 const float* rgba,
129 double depth,
130 unsigned stencil)
131 {
132 /* My notes about fastfill:
133 *
134 * 1) Only the zbuffer is cleared.
135 *
136 * 2) The zbuffer must be micro-tiled and whole microtiles must be
137 * written. If microtiling is disabled, it locks up.
138 *
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:
143 *
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
147 * pipe.
148 *
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)
151 *
152 * 5) For 16-bit zbuffer, compression causes a hung with one or
153 * two samples and should not be used.
154 *
155 * 6) FORCE_COMPRESSED_STENCIL_VALUE should be enabled for stencil clears
156 * to avoid needless decompression.
157 *
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.
161 *
162 * 8) ZB_CB_CLEAR does not interact with fastfill in any way.
163 *
164 * - Marek
165 */
166
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;
174
175 /* Enable CBZB clear. */
176 if (r300_cbzb_clear_allowed(r300, buffers)) {
177 struct r300_surface *surf = r300_surface(fb->cbufs[0]);
178
179 hyperz->zb_depthclearvalue =
180 r300_depth_clear_cb_value(surf->base.format, rgba);
181
182 width = surf->cbzb_width;
183 height = surf->cbzb_height;
184
185 r300->cbzb_clear = TRUE;
186 r300_mark_fb_state_dirty(r300, R300_CHANGED_CBZB_FLAG);
187 }
188
189 /* Clear. */
190 r300_blitter_begin(r300, R300_CLEAR);
191 util_blitter_clear(r300->blitter,
192 width,
193 height,
194 fb->nr_cbufs,
195 buffers, rgba, depth, stencil);
196 r300_blitter_end(r300);
197
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);
202 }
203
204 /* XXX this flush "fixes" a hardlock in the cubestorm xscreensaver */
205 if (r300->flush_counter == 0)
206 pipe->flush(pipe, 0, NULL);
207 }
208
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,
212 const float *rgba,
213 unsigned dstx, unsigned dsty,
214 unsigned width, unsigned height)
215 {
216 struct r300_context *r300 = r300_context(pipe);
217
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);
222 }
223
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,
228 double depth,
229 unsigned stencil,
230 unsigned dstx, unsigned dsty,
231 unsigned width, unsigned height)
232 {
233 struct r300_context *r300 = r300_context(pipe);
234
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);
239 }
240
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)
250 {
251 struct r300_context* r300 = r300_context(pipe);
252
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,
256 TRUE);
257 r300_blitter_end(r300);
258 }
259
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)
269 {
270 enum pipe_format old_format = dst->format;
271 enum pipe_format new_format = old_format;
272
273 if (!pipe->screen->is_format_supported(pipe->screen,
274 old_format, src->target,
275 src->nr_samples,
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)) {
280 case 1:
281 new_format = PIPE_FORMAT_I8_UNORM;
282 break;
283 case 2:
284 new_format = PIPE_FORMAT_B4G4R4A4_UNORM;
285 break;
286 case 4:
287 new_format = PIPE_FORMAT_B8G8R8A8_UNORM;
288 break;
289 case 8:
290 new_format = PIPE_FORMAT_R16G16B16A16_UNORM;
291 break;
292 default:
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));
296 }
297 }
298
299 if (old_format != new_format) {
300 dst->format = new_format;
301 src->format = new_format;
302
303 r300_texture_reinterpret_format(pipe->screen,
304 dst, new_format);
305 r300_texture_reinterpret_format(pipe->screen,
306 src, new_format);
307 }
308
309 r300_hw_copy_region(pipe, dst, subdst, dstx, dsty, dstz,
310 src, subsrc, srcx, srcy, srcz, width, height);
311
312 if (old_format != new_format) {
313 dst->format = old_format;
314 src->format = old_format;
315
316 r300_texture_reinterpret_format(pipe->screen,
317 dst, old_format);
318 r300_texture_reinterpret_format(pipe->screen,
319 src, old_format);
320 }
321 }
322
323 void r300_init_blit_functions(struct r300_context *r300)
324 {
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;
329 }