3cc054788bc3b0df42e06a4c4d5d374803d7b648
[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
28 enum r300_blitter_op /* bitmask */
29 {
30 R300_CLEAR = 1,
31 R300_CLEAR_SURFACE = 2,
32 R300_COPY = 4
33 };
34
35 static void r300_blitter_begin(struct r300_context* r300, enum r300_blitter_op op)
36 {
37 if (r300->query_current) {
38 r300->blitter_saved_query = r300->query_current;
39 r300_stop_query(r300);
40 }
41
42 /* Yeah we have to save all those states to ensure the blitter operation
43 * is really transparent. The states will be restored by the blitter once
44 * copying is done. */
45 util_blitter_save_blend(r300->blitter, r300->blend_state.state);
46 util_blitter_save_depth_stencil_alpha(r300->blitter, r300->dsa_state.state);
47 util_blitter_save_stencil_ref(r300->blitter, &(r300->stencil_ref));
48 util_blitter_save_rasterizer(r300->blitter, r300->rs_state.state);
49 util_blitter_save_fragment_shader(r300->blitter, r300->fs.state);
50 util_blitter_save_vertex_shader(r300->blitter, r300->vs_state.state);
51 util_blitter_save_viewport(r300->blitter, &r300->viewport);
52 util_blitter_save_clip(r300->blitter, (struct pipe_clip_state*)r300->clip_state.state);
53 util_blitter_save_vertex_elements(r300->blitter, r300->velems);
54 util_blitter_save_vertex_buffers(r300->blitter, r300->vertex_buffer_count,
55 r300->vertex_buffer);
56
57 if (op & (R300_CLEAR_SURFACE | R300_COPY))
58 util_blitter_save_framebuffer(r300->blitter, r300->fb_state.state);
59
60 if (op & R300_COPY) {
61 struct r300_textures_state* state =
62 (struct r300_textures_state*)r300->textures_state.state;
63
64 util_blitter_save_fragment_sampler_states(
65 r300->blitter, state->sampler_state_count,
66 (void**)state->sampler_states);
67
68 util_blitter_save_fragment_sampler_views(
69 r300->blitter, state->sampler_view_count,
70 (struct pipe_sampler_view**)state->sampler_views);
71 }
72 }
73
74 static void r300_blitter_end(struct r300_context *r300)
75 {
76 if (r300->blitter_saved_query) {
77 r300_resume_query(r300, r300->blitter_saved_query);
78 r300->blitter_saved_query = NULL;
79 }
80 }
81
82 /* Clear currently bound buffers. */
83 static void r300_clear(struct pipe_context* pipe,
84 unsigned buffers,
85 const float* rgba,
86 double depth,
87 unsigned stencil)
88 {
89 /* My notes about fastfill:
90 *
91 * 1) Only the zbuffer is cleared.
92 *
93 * 2) The zbuffer must be micro-tiled and whole microtiles must be
94 * written. If microtiling is disabled, it locks up.
95 *
96 * 3) There is Z Mask RAM which contains a compressed zbuffer and
97 * it interacts with fastfill. We should figure out how to use it
98 * to get more performance.
99 * This is what we know about the Z Mask:
100 *
101 * Each dword of the Z Mask contains compression information
102 * for 16 4x4 pixel blocks, that is 2 bits for each block.
103 * On chips with 2 Z pipes, every other dword maps to a different
104 * pipe.
105 *
106 * 4) ZB_DEPTHCLEARVALUE is used to clear the zbuffer and the Z Mask must
107 * be equal to 0. (clear the Z Mask RAM with zeros)
108 *
109 * 5) For 16-bit zbuffer, compression causes a hung with one or
110 * two samples and should not be used.
111 *
112 * 6) FORCE_COMPRESSED_STENCIL_VALUE should be enabled for stencil clears
113 * to avoid needless decompression.
114 *
115 * 7) Fastfill must not be used if reading of compressed Z data is disabled
116 * and writing of compressed Z data is enabled (RD/WR_COMP_ENABLE),
117 * i.e. it cannot be used to compress the zbuffer.
118 *
119 * 8) ZB_CB_CLEAR does not interact with fastfill in any way.
120 *
121 * - Marek
122 */
123
124 struct r300_context* r300 = r300_context(pipe);
125 struct pipe_framebuffer_state *fb =
126 (struct pipe_framebuffer_state*)r300->fb_state.state;
127
128 /* Clear. */
129 r300_blitter_begin(r300, R300_CLEAR);
130 util_blitter_clear(r300->blitter,
131 fb->width,
132 fb->height,
133 fb->nr_cbufs,
134 buffers, rgba, depth, stencil);
135 r300_blitter_end(r300);
136
137 /* XXX this flush "fixes" a hardlock in the cubestorm xscreensaver */
138 if (r300->flush_counter == 0)
139 pipe->flush(pipe, 0, NULL);
140 }
141
142 /* Clear a region of a color surface to a constant value. */
143 static void r300_clear_render_target(struct pipe_context *pipe,
144 struct pipe_surface *dst,
145 const float *rgba,
146 unsigned dstx, unsigned dsty,
147 unsigned width, unsigned height)
148 {
149 struct r300_context *r300 = r300_context(pipe);
150
151 r300_blitter_begin(r300, R300_CLEAR_SURFACE);
152 util_blitter_clear_render_target(r300->blitter, dst, rgba,
153 dstx, dsty, width, height);
154 r300_blitter_end(r300);
155 }
156
157 /* Clear a region of a depth stencil surface. */
158 static void r300_clear_depth_stencil(struct pipe_context *pipe,
159 struct pipe_surface *dst,
160 unsigned clear_flags,
161 double depth,
162 unsigned stencil,
163 unsigned dstx, unsigned dsty,
164 unsigned width, unsigned height)
165 {
166 struct r300_context *r300 = r300_context(pipe);
167
168 r300_blitter_begin(r300, R300_CLEAR_SURFACE);
169 util_blitter_clear_depth_stencil(r300->blitter, dst, clear_flags, depth, stencil,
170 dstx, dsty, width, height);
171 r300_blitter_end(r300);
172 }
173
174 /* Copy a block of pixels from one surface to another using HW. */
175 static void r300_hw_copy_region(struct pipe_context* pipe,
176 struct pipe_resource *dst,
177 struct pipe_subresource subdst,
178 unsigned dstx, unsigned dsty, unsigned dstz,
179 struct pipe_resource *src,
180 struct pipe_subresource subsrc,
181 unsigned srcx, unsigned srcy, unsigned srcz,
182 unsigned width, unsigned height)
183 {
184 struct r300_context* r300 = r300_context(pipe);
185
186 r300_blitter_begin(r300, R300_COPY);
187 util_blitter_copy_region(r300->blitter, dst, subdst, dstx, dsty, dstz,
188 src, subsrc, srcx, srcy, srcz, width, height,
189 TRUE);
190 r300_blitter_end(r300);
191 }
192
193 /* Copy a block of pixels from one surface to another. */
194 static void r300_resource_copy_region(struct pipe_context *pipe,
195 struct pipe_resource *dst,
196 struct pipe_subresource subdst,
197 unsigned dstx, unsigned dsty, unsigned dstz,
198 struct pipe_resource *src,
199 struct pipe_subresource subsrc,
200 unsigned srcx, unsigned srcy, unsigned srcz,
201 unsigned width, unsigned height)
202 {
203 enum pipe_format old_format = dst->format;
204 enum pipe_format new_format = old_format;
205
206 if (!pipe->screen->is_format_supported(pipe->screen,
207 old_format, src->target,
208 src->nr_samples,
209 PIPE_BIND_RENDER_TARGET |
210 PIPE_BIND_SAMPLER_VIEW, 0) &&
211 util_format_is_plain(old_format)) {
212 switch (util_format_get_blocksize(old_format)) {
213 case 1:
214 new_format = PIPE_FORMAT_I8_UNORM;
215 break;
216 case 2:
217 new_format = PIPE_FORMAT_B4G4R4A4_UNORM;
218 break;
219 case 4:
220 new_format = PIPE_FORMAT_B8G8R8A8_UNORM;
221 break;
222 case 8:
223 new_format = PIPE_FORMAT_R16G16B16A16_UNORM;
224 break;
225 default:
226 debug_printf("r300: surface_copy: Unhandled format: %s. Falling back to software.\n"
227 "r300: surface_copy: Software fallback doesn't work for tiled textures.\n",
228 util_format_short_name(old_format));
229 }
230 }
231
232 if (old_format != new_format) {
233 dst->format = new_format;
234 src->format = new_format;
235
236 r300_texture_reinterpret_format(pipe->screen,
237 dst, new_format);
238 r300_texture_reinterpret_format(pipe->screen,
239 src, new_format);
240 }
241
242 r300_hw_copy_region(pipe, dst, subdst, dstx, dsty, dstz,
243 src, subsrc, srcx, srcy, srcz, width, height);
244
245 if (old_format != new_format) {
246 dst->format = old_format;
247 src->format = old_format;
248
249 r300_texture_reinterpret_format(pipe->screen,
250 dst, old_format);
251 r300_texture_reinterpret_format(pipe->screen,
252 src, old_format);
253 }
254 }
255
256 void r300_init_blit_functions(struct r300_context *r300)
257 {
258 r300->context.clear = r300_clear;
259 r300->context.clear_render_target = r300_clear_render_target;
260 r300->context.clear_depth_stencil = r300_clear_depth_stencil;
261 r300->context.resource_copy_region = r300_resource_copy_region;
262 }