9e9912c6e4679f211d0f0e4d34c0d9caa800c1e9
[mesa.git] / src / gallium / auxiliary / util / u_blit.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * Copy/blit pixel rect between surfaces
31 *
32 * @author Brian Paul
33 */
34
35
36 #include "pipe/p_context.h"
37 #include "pipe/p_debug.h"
38 #include "pipe/p_defines.h"
39 #include "pipe/p_inlines.h"
40 #include "pipe/p_util.h"
41 #include "pipe/p_winsys.h"
42 #include "pipe/p_shader_tokens.h"
43
44 #include "util/u_draw_quad.h"
45 #include "util/u_blit.h"
46 #include "util/u_simple_shaders.h"
47
48 #include "cso_cache/cso_context.h"
49
50
51 struct blit_state
52 {
53 struct pipe_context *pipe;
54 struct cso_context *cso;
55
56 struct pipe_blend_state blend;
57 struct pipe_depth_stencil_alpha_state depthstencil;
58 struct pipe_rasterizer_state rasterizer;
59 struct pipe_sampler_state sampler;
60 struct pipe_viewport_state viewport;
61
62 struct pipe_shader_state vert_shader;
63 struct pipe_shader_state frag_shader;
64 void *vs;
65 void *fs;
66
67 struct pipe_buffer *vbuf; /**< quad vertices */
68 float vertices[4][2][4]; /**< vertex/texcoords for quad */
69 };
70
71
72 /**
73 * Create state object for blit.
74 * Intended to be created once and re-used for many blit() calls.
75 */
76 struct blit_state *
77 util_create_blit(struct pipe_context *pipe, struct cso_context *cso)
78 {
79 struct blit_state *ctx;
80 uint i;
81
82 ctx = CALLOC_STRUCT(blit_state);
83 if (!ctx)
84 return NULL;
85
86 ctx->pipe = pipe;
87 ctx->cso = cso;
88
89 /* disabled blending/masking */
90 memset(&ctx->blend, 0, sizeof(ctx->blend));
91 ctx->blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
92 ctx->blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
93 ctx->blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
94 ctx->blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
95 ctx->blend.colormask = PIPE_MASK_RGBA;
96
97 /* no-op depth/stencil/alpha */
98 memset(&ctx->depthstencil, 0, sizeof(ctx->depthstencil));
99
100 /* rasterizer */
101 memset(&ctx->rasterizer, 0, sizeof(ctx->rasterizer));
102 ctx->rasterizer.front_winding = PIPE_WINDING_CW;
103 ctx->rasterizer.cull_mode = PIPE_WINDING_NONE;
104 ctx->rasterizer.bypass_clipping = 1;
105 /*ctx->rasterizer.bypass_vs = 1;*/
106
107 /* samplers */
108 memset(&ctx->sampler, 0, sizeof(ctx->sampler));
109 ctx->sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
110 ctx->sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
111 ctx->sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
112 ctx->sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
113 ctx->sampler.min_img_filter = 0; /* set later */
114 ctx->sampler.mag_img_filter = 0; /* set later */
115 ctx->sampler.normalized_coords = 1;
116
117 /* viewport (identity, we setup vertices in wincoords) */
118 ctx->viewport.scale[0] = 1.0;
119 ctx->viewport.scale[1] = 1.0;
120 ctx->viewport.scale[2] = 1.0;
121 ctx->viewport.scale[3] = 1.0;
122 ctx->viewport.translate[0] = 0.0;
123 ctx->viewport.translate[1] = 0.0;
124 ctx->viewport.translate[2] = 0.0;
125 ctx->viewport.translate[3] = 0.0;
126
127 /* vertex shader */
128 {
129 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
130 TGSI_SEMANTIC_GENERIC };
131 const uint semantic_indexes[] = { 0, 0 };
132 ctx->vs = util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
133 semantic_indexes,
134 &ctx->vert_shader);
135 }
136
137 /* fragment shader */
138 ctx->fs = util_make_fragment_tex_shader(pipe, &ctx->frag_shader);
139
140 ctx->vbuf = pipe->winsys->buffer_create(pipe->winsys,
141 32,
142 PIPE_BUFFER_USAGE_VERTEX,
143 sizeof(ctx->vertices));
144 if (!ctx->vbuf) {
145 FREE(ctx);
146 ctx->pipe->delete_fs_state(ctx->pipe, ctx->fs);
147 ctx->pipe->delete_vs_state(ctx->pipe, ctx->vs);
148 return NULL;
149 }
150
151 /* init vertex data that doesn't change */
152 for (i = 0; i < 4; i++) {
153 ctx->vertices[i][0][3] = 1.0f; /* w */
154 ctx->vertices[i][1][2] = 0.0f; /* r */
155 ctx->vertices[i][1][3] = 1.0f; /* q */
156 }
157
158 return ctx;
159 }
160
161
162 /**
163 * Destroy a blit context
164 */
165 void
166 util_destroy_blit(struct blit_state *ctx)
167 {
168 struct pipe_context *pipe = ctx->pipe;
169
170 pipe->delete_vs_state(pipe, ctx->vs);
171 pipe->delete_fs_state(pipe, ctx->fs);
172
173 FREE((void*) ctx->vert_shader.tokens);
174 FREE((void*) ctx->frag_shader.tokens);
175
176 pipe->winsys->buffer_destroy(pipe->winsys, ctx->vbuf);
177
178 FREE(ctx);
179 }
180
181
182 /**
183 * Setup vertex data for the textured quad we'll draw.
184 * Note: y=0=top
185 */
186 static void
187 setup_vertex_data(struct blit_state *ctx,
188 float x0, float y0, float x1, float y1, float z)
189 {
190 void *buf;
191
192 ctx->vertices[0][0][0] = x0;
193 ctx->vertices[0][0][1] = y0;
194 ctx->vertices[0][0][2] = z;
195 ctx->vertices[0][1][0] = 0.0f; /*s*/
196 ctx->vertices[0][1][1] = 0.0f; /*t*/
197
198 ctx->vertices[1][0][0] = x1;
199 ctx->vertices[1][0][1] = y0;
200 ctx->vertices[1][0][2] = z;
201 ctx->vertices[1][1][0] = 1.0f; /*s*/
202 ctx->vertices[1][1][1] = 0.0f; /*t*/
203
204 ctx->vertices[2][0][0] = x1;
205 ctx->vertices[2][0][1] = y1;
206 ctx->vertices[2][0][2] = z;
207 ctx->vertices[2][1][0] = 1.0f;
208 ctx->vertices[2][1][1] = 1.0f;
209
210 ctx->vertices[3][0][0] = x0;
211 ctx->vertices[3][0][1] = y1;
212 ctx->vertices[3][0][2] = z;
213 ctx->vertices[3][1][0] = 0.0f;
214 ctx->vertices[3][1][1] = 1.0f;
215
216 buf = ctx->pipe->winsys->buffer_map(ctx->pipe->winsys, ctx->vbuf,
217 PIPE_BUFFER_USAGE_CPU_WRITE);
218
219 memcpy(buf, ctx->vertices, sizeof(ctx->vertices));
220
221 ctx->pipe->winsys->buffer_unmap(ctx->pipe->winsys, ctx->vbuf);
222 }
223
224
225 /**
226 * Copy pixel block from src surface to dst surface.
227 * Overlapping regions are acceptable.
228 * XXX need some control over blitting Z and/or stencil.
229 */
230 void
231 util_blit_pixels(struct blit_state *ctx,
232 struct pipe_surface *src,
233 int srcX0, int srcY0,
234 int srcX1, int srcY1,
235 struct pipe_surface *dst,
236 int dstX0, int dstY0,
237 int dstX1, int dstY1,
238 float z, uint filter)
239 {
240 struct pipe_context *pipe = ctx->pipe;
241 struct pipe_screen *screen = pipe->screen;
242 struct pipe_texture texTemp, *tex;
243 struct pipe_surface *texSurf;
244 struct pipe_framebuffer_state fb;
245 const int srcW = abs(srcX1 - srcX0);
246 const int srcH = abs(srcY1 - srcY0);
247 const int srcLeft = MIN2(srcX0, srcX1);
248 const int srcTop = MIN2(srcY0, srcY1);
249
250 assert(filter == PIPE_TEX_MIPFILTER_NEAREST ||
251 filter == PIPE_TEX_MIPFILTER_LINEAR);
252
253 if (srcLeft != srcX0) {
254 /* left-right flip */
255 int tmp = dstX0;
256 dstX0 = dstX1;
257 dstX1 = tmp;
258 }
259
260 if (srcTop != srcY0) {
261 /* up-down flip */
262 int tmp = dstY0;
263 dstY0 = dstY1;
264 dstY1 = tmp;
265 }
266
267 assert(screen->is_format_supported(screen, src->format, PIPE_TEXTURE));
268 assert(screen->is_format_supported(screen, dst->format, PIPE_SURFACE));
269
270 /*
271 * XXX for now we're always creating a temporary texture.
272 * Strictly speaking that's not always needed.
273 */
274
275 /* create temp texture */
276 memset(&texTemp, 0, sizeof(texTemp));
277 texTemp.target = PIPE_TEXTURE_2D;
278 texTemp.format = src->format;
279 texTemp.last_level = 0;
280 texTemp.width[0] = srcW;
281 texTemp.height[0] = srcH;
282 texTemp.depth[0] = 1;
283 texTemp.compressed = 0;
284 texTemp.cpp = pf_get_bits(src->format) / 8;
285
286 tex = screen->texture_create(screen, &texTemp);
287 if (!tex)
288 return;
289
290 texSurf = screen->get_tex_surface(screen, tex, 0, 0, 0);
291
292 /* load temp texture */
293 pipe->surface_copy(pipe, FALSE,
294 texSurf, 0, 0, /* dest */
295 src, srcLeft, srcTop, /* src */
296 srcW, srcH); /* size */
297
298 /* save state (restored below) */
299 cso_save_blend(ctx->cso);
300 cso_save_depth_stencil_alpha(ctx->cso);
301 cso_save_rasterizer(ctx->cso);
302 cso_save_samplers(ctx->cso);
303 cso_save_sampler_textures(ctx->cso);
304 cso_save_framebuffer(ctx->cso);
305 cso_save_fragment_shader(ctx->cso);
306 cso_save_vertex_shader(ctx->cso);
307 cso_save_viewport(ctx->cso);
308
309 /* set misc state we care about */
310 cso_set_blend(ctx->cso, &ctx->blend);
311 cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil);
312 cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
313 cso_set_viewport(ctx->cso, &ctx->viewport);
314
315 /* sampler */
316 ctx->sampler.min_img_filter = filter;
317 ctx->sampler.mag_img_filter = filter;
318 cso_single_sampler(ctx->cso, 0, &ctx->sampler);
319 cso_single_sampler_done(ctx->cso);
320
321 /* texture */
322 cso_set_sampler_textures(ctx->cso, 1, &tex);
323
324 /* shaders */
325 cso_set_fragment_shader_handle(ctx->cso, ctx->fs);
326 cso_set_vertex_shader_handle(ctx->cso, ctx->vs);
327
328 /* drawing dest */
329 memset(&fb, 0, sizeof(fb));
330 fb.width = dst->width;
331 fb.height = dst->height;
332 fb.num_cbufs = 1;
333 fb.cbufs[0] = dst;
334 cso_set_framebuffer(ctx->cso, &fb);
335
336 /* draw quad */
337 setup_vertex_data(ctx,
338 (float) dstX0, (float) dstY0,
339 (float) dstX1, (float) dstY1, z);
340
341 util_draw_vertex_buffer(ctx->pipe, ctx->vbuf,
342 PIPE_PRIM_TRIANGLE_FAN,
343 4, /* verts */
344 2); /* attribs/vert */
345
346 /* restore state we changed */
347 cso_restore_blend(ctx->cso);
348 cso_restore_depth_stencil_alpha(ctx->cso);
349 cso_restore_rasterizer(ctx->cso);
350 cso_restore_samplers(ctx->cso);
351 cso_restore_sampler_textures(ctx->cso);
352 cso_restore_framebuffer(ctx->cso);
353 cso_restore_fragment_shader(ctx->cso);
354 cso_restore_vertex_shader(ctx->cso);
355 cso_restore_viewport(ctx->cso);
356
357 /* free the texture */
358 pipe_surface_reference(&texSurf, NULL);
359 screen->texture_release(screen, &tex);
360 }
361