gallium: fix incorrect types for shaders
[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
61 void *vs;
62 void *fs;
63
64 struct pipe_buffer *vbuf; /**< quad vertices */
65 float vertices[4][2][4]; /**< vertex/texcoords for quad */
66 };
67
68
69 /**
70 * Create state object for blit.
71 * Intended to be created once and re-used for many blit() calls.
72 */
73 struct blit_state *
74 util_create_blit(struct pipe_context *pipe, struct cso_context *cso)
75 {
76 struct blit_state *ctx;
77 uint i;
78
79 ctx = CALLOC_STRUCT(blit_state);
80 if (!ctx)
81 return NULL;
82
83 ctx->pipe = pipe;
84 ctx->cso = cso;
85
86 /* disabled blending/masking */
87 memset(&ctx->blend, 0, sizeof(ctx->blend));
88 ctx->blend.rgb_src_factor = PIPE_BLENDFACTOR_ONE;
89 ctx->blend.alpha_src_factor = PIPE_BLENDFACTOR_ONE;
90 ctx->blend.rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
91 ctx->blend.alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
92 ctx->blend.colormask = PIPE_MASK_RGBA;
93
94 /* no-op depth/stencil/alpha */
95 memset(&ctx->depthstencil, 0, sizeof(ctx->depthstencil));
96
97 /* rasterizer */
98 memset(&ctx->rasterizer, 0, sizeof(ctx->rasterizer));
99 ctx->rasterizer.front_winding = PIPE_WINDING_CW;
100 ctx->rasterizer.cull_mode = PIPE_WINDING_NONE;
101 ctx->rasterizer.bypass_clipping = 1; /* bypasses viewport too */
102 /*ctx->rasterizer.bypass_vs = 1;*/
103
104 /* samplers */
105 memset(&ctx->sampler, 0, sizeof(ctx->sampler));
106 ctx->sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
107 ctx->sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
108 ctx->sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
109 ctx->sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
110 ctx->sampler.min_img_filter = 0; /* set later */
111 ctx->sampler.mag_img_filter = 0; /* set later */
112 ctx->sampler.normalized_coords = 1;
113
114 #if 0
115 /* viewport */
116 ctx->viewport.scale[0] = 1.0;
117 ctx->viewport.scale[1] = 1.0;
118 ctx->viewport.scale[2] = 1.0;
119 ctx->viewport.scale[3] = 1.0;
120 ctx->viewport.translate[0] = 0.0;
121 ctx->viewport.translate[1] = 0.0;
122 ctx->viewport.translate[2] = 0.0;
123 ctx->viewport.translate[3] = 0.0;
124 #endif
125
126 /* vertex shader */
127 {
128 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
129 TGSI_SEMANTIC_GENERIC };
130 const uint semantic_indexes[] = { 0, 0 };
131 ctx->vs = util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
132 semantic_indexes);
133 }
134
135 /* fragment shader */
136 ctx->fs = util_make_fragment_tex_shader(pipe);
137
138 ctx->vbuf = pipe->winsys->buffer_create(pipe->winsys,
139 32,
140 PIPE_BUFFER_USAGE_VERTEX,
141 sizeof(ctx->vertices));
142 if (!ctx->vbuf) {
143 FREE(ctx);
144 ctx->pipe->delete_fs_state(ctx->pipe, ctx->fs);
145 ctx->pipe->delete_vs_state(ctx->pipe, ctx->vs);
146 return NULL;
147 }
148
149 /* init vertex data that doesn't change */
150 for (i = 0; i < 4; i++) {
151 ctx->vertices[i][0][3] = 1.0f; /* w */
152 ctx->vertices[i][1][2] = 0.0f; /* r */
153 ctx->vertices[i][1][3] = 1.0f; /* q */
154 }
155
156 return ctx;
157 }
158
159
160 /**
161 * Destroy a blit context
162 */
163 void
164 util_destroy_blit(struct blit_state *ctx)
165 {
166 struct pipe_context *pipe = ctx->pipe;
167
168 pipe->delete_vs_state(pipe, ctx->vs);
169 pipe->delete_fs_state(pipe, ctx->fs);
170
171 pipe->winsys->buffer_destroy(pipe->winsys, ctx->vbuf);
172
173 FREE(ctx);
174 }
175
176
177 /**
178 * Setup vertex data for the textured quad we'll draw.
179 * Note: y=0=top
180 */
181 static void
182 setup_vertex_data(struct blit_state *ctx,
183 float x0, float y0, float x1, float y1, float z)
184 {
185 void *buf;
186
187 ctx->vertices[0][0][0] = x0;
188 ctx->vertices[0][0][1] = y0;
189 ctx->vertices[0][0][2] = z;
190 ctx->vertices[0][1][0] = 0.0f; /*s*/
191 ctx->vertices[0][1][1] = 0.0f; /*t*/
192
193 ctx->vertices[1][0][0] = x1;
194 ctx->vertices[1][0][1] = y0;
195 ctx->vertices[1][0][2] = z;
196 ctx->vertices[1][1][0] = 1.0f; /*s*/
197 ctx->vertices[1][1][1] = 0.0f; /*t*/
198
199 ctx->vertices[2][0][0] = x1;
200 ctx->vertices[2][0][1] = y1;
201 ctx->vertices[2][0][2] = z;
202 ctx->vertices[2][1][0] = 1.0f;
203 ctx->vertices[2][1][1] = 1.0f;
204
205 ctx->vertices[3][0][0] = x0;
206 ctx->vertices[3][0][1] = y1;
207 ctx->vertices[3][0][2] = z;
208 ctx->vertices[3][1][0] = 0.0f;
209 ctx->vertices[3][1][1] = 1.0f;
210
211 buf = ctx->pipe->winsys->buffer_map(ctx->pipe->winsys, ctx->vbuf,
212 PIPE_BUFFER_USAGE_CPU_WRITE);
213
214 memcpy(buf, ctx->vertices, sizeof(ctx->vertices));
215
216 ctx->pipe->winsys->buffer_unmap(ctx->pipe->winsys, ctx->vbuf);
217 }
218
219
220 /**
221 * Copy pixel block from src surface to dst surface.
222 * Overlapping regions are acceptable.
223 * XXX need some control over blitting Z and/or stencil.
224 */
225 void
226 util_blit_pixels(struct blit_state *ctx,
227 struct pipe_surface *src,
228 int srcX0, int srcY0,
229 int srcX1, int srcY1,
230 struct pipe_surface *dst,
231 int dstX0, int dstY0,
232 int dstX1, int dstY1,
233 float z, uint filter)
234 {
235 struct pipe_context *pipe = ctx->pipe;
236 struct pipe_screen *screen = pipe->screen;
237 struct pipe_texture texTemp, *tex;
238 struct pipe_surface *texSurf;
239 struct pipe_framebuffer_state fb;
240 const int srcW = abs(srcX1 - srcX0);
241 const int srcH = abs(srcY1 - srcY0);
242 const int srcLeft = MIN2(srcX0, srcX1);
243 const int srcTop = MIN2(srcY0, srcY1);
244
245 assert(filter == PIPE_TEX_MIPFILTER_NEAREST ||
246 filter == PIPE_TEX_MIPFILTER_LINEAR);
247
248 if (srcLeft != srcX0) {
249 /* left-right flip */
250 int tmp = dstX0;
251 dstX0 = dstX1;
252 dstX1 = tmp;
253 }
254
255 if (srcTop != srcY0) {
256 /* up-down flip */
257 int tmp = dstY0;
258 dstY0 = dstY1;
259 dstY1 = tmp;
260 }
261
262 /*
263 * XXX for now we're always creating a temporary texture.
264 * Strictly speaking that's not always needed.
265 */
266
267 /* create temp texture */
268 memset(&texTemp, 0, sizeof(texTemp));
269 texTemp.target = PIPE_TEXTURE_2D;
270 texTemp.format = src->format;
271 texTemp.last_level = 0;
272 texTemp.width[0] = srcW;
273 texTemp.height[0] = srcH;
274 texTemp.depth[0] = 1;
275 texTemp.compressed = 0;
276 texTemp.cpp = pf_get_bits(src->format) / 8;
277
278 tex = screen->texture_create(screen, &texTemp);
279 if (!tex)
280 return;
281
282 texSurf = screen->get_tex_surface(screen, tex, 0, 0, 0);
283
284 /* load temp texture */
285 pipe->surface_copy(pipe, FALSE,
286 texSurf, 0, 0, /* dest */
287 src, srcLeft, srcTop, /* src */
288 srcW, srcH); /* size */
289
290 /* save state (restored below) */
291 cso_save_blend(ctx->cso);
292 cso_save_depth_stencil_alpha(ctx->cso);
293 cso_save_rasterizer(ctx->cso);
294 cso_save_samplers(ctx->cso);
295 cso_save_sampler_textures(ctx->cso);
296 cso_save_framebuffer(ctx->cso);
297
298 /* set misc state we care about */
299 cso_set_blend(ctx->cso, &ctx->blend);
300 cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil);
301 cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
302
303 /* sampler */
304 ctx->sampler.min_img_filter = filter;
305 ctx->sampler.mag_img_filter = filter;
306 cso_single_sampler(ctx->cso, 0, &ctx->sampler);
307 cso_single_sampler_done(ctx->cso);
308
309 /* texture */
310 pipe->set_sampler_textures(pipe, 1, &tex);
311
312 /* shaders */
313 pipe->bind_fs_state(pipe, ctx->fs);
314 pipe->bind_vs_state(pipe, ctx->vs);
315
316 /* drawing dest */
317 memset(&fb, 0, sizeof(fb));
318 fb.width = dst->width;
319 fb.height = dst->height;
320 fb.num_cbufs = 1;
321 fb.cbufs[0] = dst;
322 cso_set_framebuffer(ctx->cso, &fb);
323
324 /* draw quad */
325 setup_vertex_data(ctx,
326 (float) dstX0, (float) dstY0,
327 (float) dstX1, (float) dstY1, z);
328
329 util_draw_vertex_buffer(ctx->pipe, ctx->vbuf,
330 PIPE_PRIM_TRIANGLE_FAN,
331 4, /* verts */
332 2); /* attribs/vert */
333
334 /* restore state we changed */
335 cso_restore_blend(ctx->cso);
336 cso_restore_depth_stencil_alpha(ctx->cso);
337 cso_restore_rasterizer(ctx->cso);
338 cso_restore_samplers(ctx->cso);
339 cso_restore_sampler_textures(ctx->cso);
340 cso_restore_framebuffer(ctx->cso);
341
342 /* free the texture */
343 pipe_surface_reference(&texSurf, NULL);
344 screen->texture_release(screen, &tex);
345 }
346