u_blit.c revision 8aafc03b260ab8923f1b373f7effa75bcdb40a72
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
51struct 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 */
76struct blit_state *
77util_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 */
165void
166util_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 */
186static void
187setup_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 * Setup vertex data for the textured quad we'll draw.
227 * Note: y=0=top
228 */
229static void
230setup_vertex_data_tex(struct blit_state *ctx,
231                      float x0, float y0, float x1, float y1,
232                      float s0, float t0, float s1, float t1,
233                      float z)
234{
235   void *buf;
236
237   ctx->vertices[0][0][0] = x0;
238   ctx->vertices[0][0][1] = y0;
239   ctx->vertices[0][0][2] = z;
240   ctx->vertices[0][1][0] = s0; /*s*/
241   ctx->vertices[0][1][1] = t0; /*t*/
242
243   ctx->vertices[1][0][0] = x1;
244   ctx->vertices[1][0][1] = y0;
245   ctx->vertices[1][0][2] = z;
246   ctx->vertices[1][1][0] = s1; /*s*/
247   ctx->vertices[1][1][1] = t0; /*t*/
248
249   ctx->vertices[2][0][0] = x1;
250   ctx->vertices[2][0][1] = y1;
251   ctx->vertices[2][0][2] = z;
252   ctx->vertices[2][1][0] = s1;
253   ctx->vertices[2][1][1] = t1;
254
255   ctx->vertices[3][0][0] = x0;
256   ctx->vertices[3][0][1] = y1;
257   ctx->vertices[3][0][2] = z;
258   ctx->vertices[3][1][0] = s0;
259   ctx->vertices[3][1][1] = t1;
260
261   buf = ctx->pipe->winsys->buffer_map(ctx->pipe->winsys, ctx->vbuf,
262                                       PIPE_BUFFER_USAGE_CPU_WRITE);
263
264   memcpy(buf, ctx->vertices, sizeof(ctx->vertices));
265
266   ctx->pipe->winsys->buffer_unmap(ctx->pipe->winsys, ctx->vbuf);
267}
268/**
269 * Copy pixel block from src surface to dst surface.
270 * Overlapping regions are acceptable.
271 * XXX need some control over blitting Z and/or stencil.
272 */
273void
274util_blit_pixels(struct blit_state *ctx,
275                 struct pipe_surface *src,
276                 int srcX0, int srcY0,
277                 int srcX1, int srcY1,
278                 struct pipe_surface *dst,
279                 int dstX0, int dstY0,
280                 int dstX1, int dstY1,
281                 float z, uint filter)
282{
283   struct pipe_context *pipe = ctx->pipe;
284   struct pipe_screen *screen = pipe->screen;
285   struct pipe_texture texTemp, *tex;
286   struct pipe_surface *texSurf;
287   struct pipe_framebuffer_state fb;
288   const int srcW = abs(srcX1 - srcX0);
289   const int srcH = abs(srcY1 - srcY0);
290   const int srcLeft = MIN2(srcX0, srcX1);
291   const int srcTop = MIN2(srcY0, srcY1);
292
293   assert(filter == PIPE_TEX_MIPFILTER_NEAREST ||
294          filter == PIPE_TEX_MIPFILTER_LINEAR);
295
296   if (srcLeft != srcX0) {
297      /* left-right flip */
298      int tmp = dstX0;
299      dstX0 = dstX1;
300      dstX1 = tmp;
301   }
302
303   if (srcTop != srcY0) {
304      /* up-down flip */
305      int tmp = dstY0;
306      dstY0 = dstY1;
307      dstY1 = tmp;
308   }
309
310   assert(screen->is_format_supported(screen, src->format, PIPE_TEXTURE_2D,
311                                      PIPE_TEXTURE_USAGE_SAMPLER, 0));
312   assert(screen->is_format_supported(screen, dst->format, PIPE_TEXTURE_2D,
313                                      PIPE_TEXTURE_USAGE_SAMPLER, 0));
314
315   if(dst->format == src->format && (dstX1 - dstX0) == srcW && (dstY1 - dstY0) == srcH) {
316      /* FIXME: this will most surely fail for overlapping rectangles */
317      pipe->surface_copy(pipe, FALSE,
318			 dst, dstX0, dstY0,   /* dest */
319			 src, srcX0, srcY0, /* src */
320			 srcW, srcH);     /* size */
321      return;
322   }
323
324   assert(screen->is_format_supported(screen, dst->format, PIPE_TEXTURE_2D,
325                                      PIPE_TEXTURE_USAGE_RENDER_TARGET, 0));
326
327   /*
328    * XXX for now we're always creating a temporary texture.
329    * Strictly speaking that's not always needed.
330    */
331
332   /* create temp texture */
333   memset(&texTemp, 0, sizeof(texTemp));
334   texTemp.target = PIPE_TEXTURE_2D;
335   texTemp.format = src->format;
336   texTemp.last_level = 0;
337   texTemp.width[0] = srcW;
338   texTemp.height[0] = srcH;
339   texTemp.depth[0] = 1;
340   texTemp.compressed = 0;
341   pf_get_block(src->format, &texTemp.block);
342
343   tex = screen->texture_create(screen, &texTemp);
344   if (!tex)
345      return;
346
347   texSurf = screen->get_tex_surface(screen, tex, 0, 0, 0,
348                                     PIPE_BUFFER_USAGE_GPU_WRITE);
349
350   /* load temp texture */
351   pipe->surface_copy(pipe, FALSE,
352                      texSurf, 0, 0,   /* dest */
353                      src, srcLeft, srcTop, /* src */
354                      srcW, srcH);     /* size */
355
356   /* free the surface, update the texture if necessary.
357    */
358   screen->tex_surface_release(screen, &texSurf);
359
360   /* save state (restored below) */
361   cso_save_blend(ctx->cso);
362   cso_save_depth_stencil_alpha(ctx->cso);
363   cso_save_rasterizer(ctx->cso);
364   cso_save_samplers(ctx->cso);
365   cso_save_sampler_textures(ctx->cso);
366   cso_save_framebuffer(ctx->cso);
367   cso_save_fragment_shader(ctx->cso);
368   cso_save_vertex_shader(ctx->cso);
369   cso_save_viewport(ctx->cso);
370
371   /* set misc state we care about */
372   cso_set_blend(ctx->cso, &ctx->blend);
373   cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil);
374   cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
375   cso_set_viewport(ctx->cso, &ctx->viewport);
376
377   /* sampler */
378   ctx->sampler.min_img_filter = filter;
379   ctx->sampler.mag_img_filter = filter;
380   cso_single_sampler(ctx->cso, 0, &ctx->sampler);
381   cso_single_sampler_done(ctx->cso);
382
383   /* texture */
384   cso_set_sampler_textures(ctx->cso, 1, &tex);
385
386   /* shaders */
387   cso_set_fragment_shader_handle(ctx->cso, ctx->fs);
388   cso_set_vertex_shader_handle(ctx->cso, ctx->vs);
389
390   /* drawing dest */
391   memset(&fb, 0, sizeof(fb));
392   fb.width = dst->width;
393   fb.height = dst->height;
394   fb.num_cbufs = 1;
395   fb.cbufs[0] = dst;
396   cso_set_framebuffer(ctx->cso, &fb);
397
398   /* draw quad */
399   setup_vertex_data(ctx,
400                     (float) dstX0, (float) dstY0,
401                     (float) dstX1, (float) dstY1, z);
402
403   util_draw_vertex_buffer(ctx->pipe, ctx->vbuf,
404                           PIPE_PRIM_TRIANGLE_FAN,
405                           4,  /* verts */
406                           2); /* attribs/vert */
407
408   /* restore state we changed */
409   cso_restore_blend(ctx->cso);
410   cso_restore_depth_stencil_alpha(ctx->cso);
411   cso_restore_rasterizer(ctx->cso);
412   cso_restore_samplers(ctx->cso);
413   cso_restore_sampler_textures(ctx->cso);
414   cso_restore_framebuffer(ctx->cso);
415   cso_restore_fragment_shader(ctx->cso);
416   cso_restore_vertex_shader(ctx->cso);
417   cso_restore_viewport(ctx->cso);
418
419   screen->texture_release(screen, &tex);
420}
421
422/**
423 * Copy pixel block from src texture to dst surface.
424 * Overlapping regions are acceptable.
425 *
426 * XXX Should support selection of level.
427 * XXX need some control over blitting Z and/or stencil.
428 */
429void
430util_blit_pixels_tex(struct blit_state *ctx,
431                 struct pipe_texture *tex,
432                 int srcX0, int srcY0,
433                 int srcX1, int srcY1,
434                 struct pipe_surface *dst,
435                 int dstX0, int dstY0,
436                 int dstX1, int dstY1,
437                 float z, uint filter)
438{
439   struct pipe_context *pipe = ctx->pipe;
440   struct pipe_screen *screen = pipe->screen;
441   struct pipe_framebuffer_state fb;
442   float s0, t0, s1, t1;
443
444   assert(filter == PIPE_TEX_MIPFILTER_NEAREST ||
445          filter == PIPE_TEX_MIPFILTER_LINEAR);
446
447   assert(tex->width[0] != 0);
448   assert(tex->height[0] != 0);
449
450   s0 = srcX0 / (float)tex->width[0];
451   s1 = srcX1 / (float)tex->width[0];
452   t0 = srcY0 / (float)tex->height[0];
453   t1 = srcY1 / (float)tex->height[0];
454
455   assert(screen->is_format_supported(screen, dst->format, PIPE_TEXTURE_2D,
456                                      PIPE_TEXTURE_USAGE_RENDER_TARGET, 0));
457
458   /* save state (restored below) */
459   cso_save_blend(ctx->cso);
460   cso_save_depth_stencil_alpha(ctx->cso);
461   cso_save_rasterizer(ctx->cso);
462   cso_save_samplers(ctx->cso);
463   cso_save_sampler_textures(ctx->cso);
464   cso_save_framebuffer(ctx->cso);
465   cso_save_fragment_shader(ctx->cso);
466   cso_save_vertex_shader(ctx->cso);
467   cso_save_viewport(ctx->cso);
468
469   /* set misc state we care about */
470   cso_set_blend(ctx->cso, &ctx->blend);
471   cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil);
472   cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
473   cso_set_viewport(ctx->cso, &ctx->viewport);
474
475   /* sampler */
476   ctx->sampler.min_img_filter = filter;
477   ctx->sampler.mag_img_filter = filter;
478   cso_single_sampler(ctx->cso, 0, &ctx->sampler);
479   cso_single_sampler_done(ctx->cso);
480
481   /* texture */
482   cso_set_sampler_textures(ctx->cso, 1, &tex);
483
484   /* shaders */
485   cso_set_fragment_shader_handle(ctx->cso, ctx->fs);
486   cso_set_vertex_shader_handle(ctx->cso, ctx->vs);
487
488   /* drawing dest */
489   memset(&fb, 0, sizeof(fb));
490   fb.width = dst->width;
491   fb.height = dst->height;
492   fb.num_cbufs = 1;
493   fb.cbufs[0] = dst;
494   cso_set_framebuffer(ctx->cso, &fb);
495
496   /* draw quad */
497   setup_vertex_data_tex(ctx,
498                     (float) dstX0, (float) dstY0,
499                     (float) dstX1, (float) dstY1,
500                     s0, t0, s1, t1,
501                     z);
502
503   util_draw_vertex_buffer(ctx->pipe, ctx->vbuf,
504                           PIPE_PRIM_TRIANGLE_FAN,
505                           4,  /* verts */
506                           2); /* attribs/vert */
507
508   /* restore state we changed */
509   cso_restore_blend(ctx->cso);
510   cso_restore_depth_stencil_alpha(ctx->cso);
511   cso_restore_rasterizer(ctx->cso);
512   cso_restore_samplers(ctx->cso);
513   cso_restore_sampler_textures(ctx->cso);
514   cso_restore_framebuffer(ctx->cso);
515   cso_restore_fragment_shader(ctx->cso);
516   cso_restore_vertex_shader(ctx->cso);
517   cso_restore_viewport(ctx->cso);
518}
519