glhd: Grab resource_copy_region from r300g.
[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
29 {
30 R300_CLEAR,
31 R300_CLEAR_SURFACE,
32 R300_COPY
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 /* XXX Implement fastfill.
90 *
91 * If fastfill is enabled, a few facts should be considered:
92 *
93 * 1) Zbuffer must be micro-tiled and whole microtiles must be
94 * written.
95 *
96 * 2) ZB_DEPTHCLEARVALUE is used to clear a zbuffer and Z Mask must be
97 * equal to 0.
98 *
99 * 3) For 16-bit integer buffering, compression causes a hung with one or
100 * two samples and should not be used.
101 *
102 * 4) Fastfill must not be used if reading of compressed Z data is disabled
103 * and writing of compressed Z data is enabled (RD/WR_COMP_ENABLE),
104 * i.e. it cannot be used to compress the zbuffer.
105 * (what the hell does that mean and how does it fit in clearing
106 * the buffers?)
107 *
108 * - Marek
109 */
110
111 struct r300_context* r300 = r300_context(pipe);
112 struct pipe_framebuffer_state* fb =
113 (struct pipe_framebuffer_state*)r300->fb_state.state;
114
115 r300_blitter_begin(r300, R300_CLEAR);
116 util_blitter_clear(r300->blitter,
117 fb->width,
118 fb->height,
119 fb->nr_cbufs,
120 buffers, rgba, depth, stencil);
121 r300_blitter_end(r300);
122 }
123
124 /* Clear a region of a color surface to a constant value. */
125 static void r300_clear_render_target(struct pipe_context *pipe,
126 struct pipe_surface *dst,
127 const float *rgba,
128 unsigned dstx, unsigned dsty,
129 unsigned width, unsigned height)
130 {
131 struct r300_context *r300 = r300_context(pipe);
132
133 r300_blitter_begin(r300, R300_CLEAR_SURFACE);
134 util_blitter_clear_render_target(r300->blitter, dst, rgba,
135 dstx, dsty, width, height);
136 r300_blitter_end(r300);
137 }
138
139 /* Clear a region of a depth stencil surface. */
140 static void r300_clear_depth_stencil(struct pipe_context *pipe,
141 struct pipe_surface *dst,
142 unsigned clear_flags,
143 double depth,
144 unsigned stencil,
145 unsigned dstx, unsigned dsty,
146 unsigned width, unsigned height)
147 {
148 struct r300_context *r300 = r300_context(pipe);
149
150 r300_blitter_begin(r300, R300_CLEAR_SURFACE);
151 util_blitter_clear_depth_stencil(r300->blitter, dst, clear_flags, depth, stencil,
152 dstx, dsty, width, height);
153 r300_blitter_end(r300);
154 }
155
156 /* Copy a block of pixels from one surface to another using HW. */
157 static void r300_hw_copy_region(struct pipe_context* pipe,
158 struct pipe_resource *dst,
159 struct pipe_subresource subdst,
160 unsigned dstx, unsigned dsty, unsigned dstz,
161 struct pipe_resource *src,
162 struct pipe_subresource subsrc,
163 unsigned srcx, unsigned srcy, unsigned srcz,
164 unsigned width, unsigned height)
165 {
166 struct r300_context* r300 = r300_context(pipe);
167
168 r300_blitter_begin(r300, R300_COPY);
169 util_blitter_copy_region(r300->blitter, dst, subdst, dstx, dsty, dstz,
170 src, subsrc, srcx, srcy, srcz, width, height,
171 TRUE);
172 r300_blitter_end(r300);
173 }
174
175 /* Copy a block of pixels from one surface to another. */
176 static void r300_resource_copy_region(struct pipe_context *pipe,
177 struct pipe_resource *dst,
178 struct pipe_subresource subdst,
179 unsigned dstx, unsigned dsty, unsigned dstz,
180 struct pipe_resource *src,
181 struct pipe_subresource subsrc,
182 unsigned srcx, unsigned srcy, unsigned srcz,
183 unsigned width, unsigned height)
184 {
185 enum pipe_format old_format = dst->format;
186 enum pipe_format new_format = old_format;
187
188 if (!pipe->screen->is_format_supported(pipe->screen,
189 old_format, src->target,
190 src->nr_samples,
191 PIPE_BIND_RENDER_TARGET |
192 PIPE_BIND_SAMPLER_VIEW, 0) &&
193 util_format_is_plain(old_format)) {
194 switch (util_format_get_blocksize(old_format)) {
195 case 1:
196 new_format = PIPE_FORMAT_I8_UNORM;
197 break;
198 case 2:
199 new_format = PIPE_FORMAT_B4G4R4A4_UNORM;
200 break;
201 case 4:
202 new_format = PIPE_FORMAT_B8G8R8A8_UNORM;
203 break;
204 case 8:
205 new_format = PIPE_FORMAT_R16G16B16A16_UNORM;
206 break;
207 default:
208 debug_printf("r300: surface_copy: Unhandled format: %s. Falling back to software.\n"
209 "r300: surface_copy: Software fallback doesn't work for tiled textures.\n",
210 util_format_short_name(old_format));
211 }
212 }
213
214 if (old_format != new_format) {
215 dst->format = new_format;
216 src->format = new_format;
217
218 r300_texture_reinterpret_format(pipe->screen,
219 dst, new_format);
220 r300_texture_reinterpret_format(pipe->screen,
221 src, new_format);
222 }
223
224 r300_hw_copy_region(pipe, dst, subdst, dstx, dsty, dstz,
225 src, subsrc, srcx, srcy, srcz, width, height);
226
227 if (old_format != new_format) {
228 dst->format = old_format;
229 src->format = old_format;
230
231 r300_texture_reinterpret_format(pipe->screen,
232 dst, old_format);
233 r300_texture_reinterpret_format(pipe->screen,
234 src, old_format);
235 }
236 }
237
238 void r300_init_blit_functions(struct r300_context *r300)
239 {
240 r300->context.clear = r300_clear;
241 r300->context.clear_render_target = r300_clear_render_target;
242 r300->context.clear_depth_stencil = r300_clear_depth_stencil;
243 r300->context.resource_copy_region = r300_resource_copy_region;
244 }