r300g: adapt to interface changes
[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_blit.h"
24 #include "r300_context.h"
25 #include "r300_texture.h"
26
27 #include "util/u_format.h"
28
29 static void r300_blitter_save_states(struct r300_context* r300)
30 {
31 util_blitter_save_blend(r300->blitter, r300->blend_state.state);
32 util_blitter_save_depth_stencil_alpha(r300->blitter, r300->dsa_state.state);
33 util_blitter_save_stencil_ref(r300->blitter, &(r300->stencil_ref));
34 util_blitter_save_rasterizer(r300->blitter, r300->rs_state.state);
35 util_blitter_save_fragment_shader(r300->blitter, r300->fs.state);
36 util_blitter_save_vertex_shader(r300->blitter, r300->vs_state.state);
37 util_blitter_save_viewport(r300->blitter, &r300->viewport);
38 util_blitter_save_clip(r300->blitter, &r300->clip);
39 util_blitter_save_vertex_elements(r300->blitter, r300->velems);
40 /* XXX this crashes the driver
41 util_blitter_save_vertex_buffers(r300->blitter, r300->vertex_buffer_count,
42 r300->vertex_buffer); */
43 }
44
45 /* Clear currently bound buffers. */
46 void r300_clear(struct pipe_context* pipe,
47 unsigned buffers,
48 const float* rgba,
49 double depth,
50 unsigned stencil)
51 {
52 /* XXX Implement fastfill.
53 *
54 * If fastfill is enabled, a few facts should be considered:
55 *
56 * 1) Zbuffer must be micro-tiled and whole microtiles must be
57 * written.
58 *
59 * 2) ZB_DEPTHCLEARVALUE is used to clear a zbuffer and Z Mask must be
60 * equal to 0.
61 *
62 * 3) For 16-bit integer buffering, compression causes a hung with one or
63 * two samples and should not be used.
64 *
65 * 4) Fastfill must not be used if reading of compressed Z data is disabled
66 * and writing of compressed Z data is enabled (RD/WR_COMP_ENABLE),
67 * i.e. it cannot be used to compress the zbuffer.
68 * (what the hell does that mean and how does it fit in clearing
69 * the buffers?)
70 *
71 * - Marek
72 */
73
74 struct r300_context* r300 = r300_context(pipe);
75 struct pipe_framebuffer_state* fb =
76 (struct pipe_framebuffer_state*)r300->fb_state.state;
77
78 r300_blitter_save_states(r300);
79
80 util_blitter_clear(r300->blitter,
81 fb->width,
82 fb->height,
83 fb->nr_cbufs,
84 buffers, rgba, depth, stencil);
85 }
86
87 /* Copy a block of pixels from one surface to another using HW. */
88 static void r300_hw_copy(struct pipe_context* pipe,
89 struct pipe_surface* dst,
90 unsigned dstx, unsigned dsty,
91 struct pipe_surface* src,
92 unsigned srcx, unsigned srcy,
93 unsigned width, unsigned height)
94 {
95 struct r300_context* r300 = r300_context(pipe);
96 struct r300_textures_state* state =
97 (struct r300_textures_state*)r300->textures_state.state;
98
99 /* Yeah we have to save all those states to ensure this blitter operation
100 * is really transparent. The states will be restored by the blitter once
101 * copying is done. */
102 r300_blitter_save_states(r300);
103 util_blitter_save_framebuffer(r300->blitter, r300->fb_state.state);
104
105 util_blitter_save_fragment_sampler_states(
106 r300->blitter, state->sampler_state_count,
107 (void**)state->sampler_states);
108
109 util_blitter_save_fragment_sampler_views(
110 r300->blitter, state->sampler_view_count,
111 (struct pipe_sampler_view**)state->sampler_views);
112
113 /* Do a copy */
114 util_blitter_copy(r300->blitter,
115 dst, dstx, dsty, src, srcx, srcy, width, height, TRUE);
116 }
117
118 /* Copy a block of pixels from one surface to another. */
119 void r300_surface_copy(struct pipe_context* pipe,
120 struct pipe_resource* dst,
121 struct pipe_subresource subdst,
122 unsigned dstx, unsigned dsty, unsigned dstz,
123 struct pipe_resource* src,
124 struct pipe_subresource subsrc,
125 unsigned srcx, unsigned srcy, unsigned srcz,
126 unsigned width, unsigned height)
127 {
128 struct pipe_screen *screen = pipe->screen;
129 enum pipe_format old_format = dst->format;
130 enum pipe_format new_format = old_format;
131 struct pipe_surface *srcsurf, *dstsurf;
132 unsigned bind;
133
134 if (util_format_is_depth_or_stencil(dst->format))
135 bind = PIPE_BIND_DEPTH_STENCIL;
136 else
137 bind = PIPE_BIND_RENDER_TARGET;
138
139 if (dst->format != src->format) {
140 debug_printf("r300: Implementation error: Format mismatch in %s\n"
141 " : src: %s dst: %s\n", __FUNCTION__,
142 util_format_short_name(src->format),
143 util_format_short_name(dst->format));
144 debug_assert(0);
145 }
146
147 if (!pipe->screen->is_format_supported(pipe->screen,
148 old_format, src->target,
149 src->nr_samples,
150 PIPE_BIND_RENDER_TARGET |
151 PIPE_BIND_SAMPLER_VIEW, 0) &&
152 util_format_is_plain(old_format)) {
153 switch (util_format_get_blocksize(old_format)) {
154 case 1:
155 new_format = PIPE_FORMAT_I8_UNORM;
156 break;
157 case 2:
158 new_format = PIPE_FORMAT_B4G4R4A4_UNORM;
159 break;
160 case 4:
161 new_format = PIPE_FORMAT_B8G8R8A8_UNORM;
162 break;
163 case 8:
164 new_format = PIPE_FORMAT_R16G16B16A16_UNORM;
165 break;
166 default:
167 debug_printf("r300: surface_copy: Unhandled format: %s. Falling back to software.\n"
168 "r300: surface_copy: Software fallback doesn't work for tiled textures.\n",
169 util_format_short_name(old_format));
170 }
171 }
172
173 if (old_format != new_format) {
174 dst->format = new_format;
175 src->format = new_format;
176
177 r300_texture_reinterpret_format(pipe->screen,
178 dst, new_format);
179 r300_texture_reinterpret_format(pipe->screen,
180 src, new_format);
181 }
182
183 srcsurf = screen->get_tex_surface(screen, src,
184 subsrc.face, subsrc.level, srcz,
185 PIPE_BIND_SAMPLER_VIEW);
186
187 dstsurf = screen->get_tex_surface(screen, dst,
188 subdst.face, subdst.level, dstz,
189 bind);
190
191 r300_hw_copy(pipe, dstsurf, dstx, dsty, srcsurf, srcx, srcy, width, height);
192
193 pipe_surface_reference(&srcsurf, NULL);
194 pipe_surface_reference(&dstsurf, NULL);
195
196 if (old_format != new_format) {
197 dst->format = old_format;
198 src->format = old_format;
199
200 r300_texture_reinterpret_format(pipe->screen,
201 dst, old_format);
202 r300_texture_reinterpret_format(pipe->screen,
203 src, old_format);
204 }
205 }
206
207 /* Fill a region of a surface with a constant value. */
208 void r300_surface_fill(struct pipe_context* pipe,
209 struct pipe_resource* dst,
210 struct pipe_subresource subdst,
211 unsigned dstx, unsigned dsty, unsigned dstz,
212 unsigned width, unsigned height,
213 unsigned value)
214 {
215 struct pipe_screen *screen = pipe->screen;
216 struct r300_context* r300 = r300_context(pipe);
217 struct pipe_surface *dstsurf;
218 unsigned bind;
219
220 if (util_format_is_depth_or_stencil(dst->format))
221 bind = PIPE_BIND_DEPTH_STENCIL;
222 else
223 bind = PIPE_BIND_RENDER_TARGET;
224
225 dstsurf = screen->get_tex_surface(screen, dst,
226 subdst.face,
227 subdst.level,
228 dstz,
229 bind);
230
231 r300_blitter_save_states(r300);
232 util_blitter_save_framebuffer(r300->blitter, r300->fb_state.state);
233
234 util_blitter_fill(r300->blitter,
235 dstsurf, dstx, dsty, width, height, value);
236
237 pipe_surface_reference(&dstsurf, NULL);
238 }