u_blit.c revision dff4c9ed559ae025d1d8fe7b9d1cea5a973c2225
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 "util/u_debug.h"
38#include "pipe/p_defines.h"
39#include "pipe/p_inlines.h"
40#include "pipe/p_shader_tokens.h"
41#include "pipe/p_state.h"
42
43#include "util/u_blit.h"
44#include "util/u_draw_quad.h"
45#include "util/u_math.h"
46#include "util/u_memory.h"
47#include "util/u_simple_shaders.h"
48#include "util/u_surface.h"
49#include "util/u_rect.h"
50
51#include "cso_cache/cso_context.h"
52
53
54struct blit_state
55{
56   struct pipe_context *pipe;
57   struct cso_context *cso;
58
59   struct pipe_blend_state blend;
60   struct pipe_depth_stencil_alpha_state depthstencil;
61   struct pipe_rasterizer_state rasterizer;
62   struct pipe_sampler_state sampler;
63   struct pipe_viewport_state viewport;
64
65   void *vs;
66   void *fs[TGSI_WRITEMASK_XYZW + 1];
67
68   struct pipe_buffer *vbuf;  /**< quad vertices */
69   unsigned vbuf_slot;
70
71   float vertices[4][2][4];   /**< vertex/texcoords for quad */
72};
73
74
75/**
76 * Create state object for blit.
77 * Intended to be created once and re-used for many blit() calls.
78 */
79struct blit_state *
80util_create_blit(struct pipe_context *pipe, struct cso_context *cso)
81{
82   struct blit_state *ctx;
83   uint i;
84
85   ctx = CALLOC_STRUCT(blit_state);
86   if (!ctx)
87      return NULL;
88
89   ctx->pipe = pipe;
90   ctx->cso = cso;
91
92   /* disabled blending/masking */
93   memset(&ctx->blend, 0, sizeof(ctx->blend));
94   ctx->blend.colormask = PIPE_MASK_RGBA;
95
96   /* no-op depth/stencil/alpha */
97   memset(&ctx->depthstencil, 0, sizeof(ctx->depthstencil));
98
99   /* rasterizer */
100   memset(&ctx->rasterizer, 0, sizeof(ctx->rasterizer));
101   ctx->rasterizer.front_winding = PIPE_WINDING_CW;
102   ctx->rasterizer.cull_mode = PIPE_WINDING_NONE;
103   ctx->rasterizer.bypass_vs_clip_and_viewport = 1;
104   ctx->rasterizer.gl_rasterization_rules = 1;
105
106   /* samplers */
107   memset(&ctx->sampler, 0, sizeof(ctx->sampler));
108   ctx->sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
109   ctx->sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
110   ctx->sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
111   ctx->sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
112   ctx->sampler.min_img_filter = 0; /* set later */
113   ctx->sampler.mag_img_filter = 0; /* set later */
114   ctx->sampler.normalized_coords = 1;
115
116
117   /* vertex shader - still required to provide the linkage between
118    * fragment shader input semantics and vertex_element/buffers.
119    */
120   {
121      const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
122                                      TGSI_SEMANTIC_GENERIC };
123      const uint semantic_indexes[] = { 0, 0 };
124      ctx->vs = util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
125                                                    semantic_indexes);
126   }
127
128   /* fragment shader */
129   ctx->fs[TGSI_WRITEMASK_XYZW] =
130      util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_2D);
131   ctx->vbuf = NULL;
132
133   /* init vertex data that doesn't change */
134   for (i = 0; i < 4; i++) {
135      ctx->vertices[i][0][3] = 1.0f; /* w */
136      ctx->vertices[i][1][2] = 0.0f; /* r */
137      ctx->vertices[i][1][3] = 1.0f; /* q */
138   }
139
140   return ctx;
141}
142
143
144/**
145 * Destroy a blit context
146 */
147void
148util_destroy_blit(struct blit_state *ctx)
149{
150   struct pipe_context *pipe = ctx->pipe;
151   unsigned i;
152
153   pipe->delete_vs_state(pipe, ctx->vs);
154
155   for (i = 0; i < Elements(ctx->fs); i++)
156      if (ctx->fs[i])
157         pipe->delete_fs_state(pipe, ctx->fs[i]);
158
159   pipe_buffer_reference(&ctx->vbuf, NULL);
160
161   FREE(ctx);
162}
163
164
165/**
166 * Get offset of next free slot in vertex buffer for quad vertices.
167 */
168static unsigned
169get_next_slot( struct blit_state *ctx )
170{
171   const unsigned max_slots = 4096 / sizeof ctx->vertices;
172
173   if (ctx->vbuf_slot >= max_slots)
174      util_blit_flush( ctx );
175
176   if (!ctx->vbuf) {
177      ctx->vbuf = pipe_buffer_create(ctx->pipe->screen,
178                                     32,
179                                     PIPE_BUFFER_USAGE_VERTEX,
180                                     max_slots * sizeof ctx->vertices);
181   }
182
183   return ctx->vbuf_slot++ * sizeof ctx->vertices;
184}
185
186
187
188
189
190/**
191 * Setup vertex data for the textured quad we'll draw.
192 * Note: y=0=top
193 */
194static unsigned
195setup_vertex_data_tex(struct blit_state *ctx,
196                      float x0, float y0, float x1, float y1,
197                      float s0, float t0, float s1, float t1,
198                      float z)
199{
200   unsigned offset;
201
202   ctx->vertices[0][0][0] = x0;
203   ctx->vertices[0][0][1] = y0;
204   ctx->vertices[0][0][2] = z;
205   ctx->vertices[0][1][0] = s0; /*s*/
206   ctx->vertices[0][1][1] = t0; /*t*/
207
208   ctx->vertices[1][0][0] = x1;
209   ctx->vertices[1][0][1] = y0;
210   ctx->vertices[1][0][2] = z;
211   ctx->vertices[1][1][0] = s1; /*s*/
212   ctx->vertices[1][1][1] = t0; /*t*/
213
214   ctx->vertices[2][0][0] = x1;
215   ctx->vertices[2][0][1] = y1;
216   ctx->vertices[2][0][2] = z;
217   ctx->vertices[2][1][0] = s1;
218   ctx->vertices[2][1][1] = t1;
219
220   ctx->vertices[3][0][0] = x0;
221   ctx->vertices[3][0][1] = y1;
222   ctx->vertices[3][0][2] = z;
223   ctx->vertices[3][1][0] = s0;
224   ctx->vertices[3][1][1] = t1;
225
226   offset = get_next_slot( ctx );
227
228   pipe_buffer_write(ctx->pipe->screen, ctx->vbuf,
229                     offset, sizeof(ctx->vertices), ctx->vertices);
230
231   return offset;
232}
233
234
235/**
236 * \return TRUE if two regions overlap, FALSE otherwise
237 */
238static boolean
239regions_overlap(int srcX0, int srcY0,
240                int srcX1, int srcY1,
241                int dstX0, int dstY0,
242                int dstX1, int dstY1)
243{
244   if (MAX2(srcX0, srcX1) < MIN2(dstX0, dstX1))
245      return FALSE; /* src completely left of dst */
246
247   if (MAX2(dstX0, dstX1) < MIN2(srcX0, srcX1))
248      return FALSE; /* dst completely left of src */
249
250   if (MAX2(srcY0, srcY1) < MIN2(dstY0, dstY1))
251      return FALSE; /* src completely above dst */
252
253   if (MAX2(dstY0, dstY1) < MIN2(srcY0, srcY1))
254      return FALSE; /* dst completely above src */
255
256   return TRUE; /* some overlap */
257}
258
259
260/**
261 * Copy pixel block from src surface to dst surface.
262 * Overlapping regions are acceptable.
263 * Flipping and stretching are supported.
264 * XXX what about clipping???
265 * XXX need some control over blitting Z and/or stencil.
266 */
267void
268util_blit_pixels_writemask(struct blit_state *ctx,
269                           struct pipe_surface *src,
270                           int srcX0, int srcY0,
271                           int srcX1, int srcY1,
272                           struct pipe_surface *dst,
273                           int dstX0, int dstY0,
274                           int dstX1, int dstY1,
275                           float z, uint filter,
276                           uint writemask)
277{
278   struct pipe_context *pipe = ctx->pipe;
279   struct pipe_screen *screen = pipe->screen;
280   struct pipe_texture *tex = NULL;
281   struct pipe_framebuffer_state fb;
282   const int srcW = abs(srcX1 - srcX0);
283   const int srcH = abs(srcY1 - srcY0);
284   unsigned offset;
285   boolean overlap;
286   float s0, t0, s1, t1;
287
288   assert(filter == PIPE_TEX_MIPFILTER_NEAREST ||
289          filter == PIPE_TEX_MIPFILTER_LINEAR);
290
291   assert(screen->is_format_supported(screen, src->format, PIPE_TEXTURE_2D,
292                                      PIPE_TEXTURE_USAGE_SAMPLER, 0));
293   assert(screen->is_format_supported(screen, dst->format, PIPE_TEXTURE_2D,
294                                      PIPE_TEXTURE_USAGE_RENDER_TARGET, 0));
295
296   /* do the regions overlap? */
297   overlap = util_same_surface(src, dst) &&
298      regions_overlap(srcX0, srcY0, srcX1, srcY1,
299                      dstX0, dstY0, dstX1, dstY1);
300
301   /*
302    * Check for simple case:  no format conversion, no flipping, no stretching,
303    * no overlapping.
304    * Filter mode should not matter since there's no stretching.
305    */
306   if (pipe->surface_copy &&
307       dst->format == src->format &&
308       srcX0 < srcX1 &&
309       dstX0 < dstX1 &&
310       srcY0 < srcY1 &&
311       dstY0 < dstY1 &&
312       (dstX1 - dstX0) == (srcX1 - srcX0) &&
313       (dstY1 - dstY0) == (srcY1 - srcY0) &&
314       !overlap) {
315      pipe->surface_copy(pipe,
316			 dst, dstX0, dstY0, /* dest */
317			 src, srcX0, srcY0, /* src */
318			 srcW, srcH);       /* size */
319      return;
320   }
321
322   assert(screen->is_format_supported(screen, dst->format, PIPE_TEXTURE_2D,
323                                      PIPE_TEXTURE_USAGE_RENDER_TARGET, 0));
324
325   /* Create a temporary texture when src and dest alias or when src
326    * is anything other than a single-level 2d texture.
327    *
328    * This can still be improved upon.
329    */
330   if (util_same_surface(src, dst) ||
331       src->texture->target != PIPE_TEXTURE_2D ||
332       src->texture->last_level != 0)
333   {
334      struct pipe_texture texTemp;
335      struct pipe_surface *texSurf;
336      const int srcLeft = MIN2(srcX0, srcX1);
337      const int srcTop = MIN2(srcY0, srcY1);
338
339      if (srcLeft != srcX0) {
340         /* left-right flip */
341         int tmp = dstX0;
342         dstX0 = dstX1;
343         dstX1 = tmp;
344      }
345
346      if (srcTop != srcY0) {
347         /* up-down flip */
348         int tmp = dstY0;
349         dstY0 = dstY1;
350         dstY1 = tmp;
351      }
352
353      /* create temp texture */
354      memset(&texTemp, 0, sizeof(texTemp));
355      texTemp.target = PIPE_TEXTURE_2D;
356      texTemp.format = src->format;
357      texTemp.last_level = 0;
358      texTemp.width0 = srcW;
359      texTemp.height0 = srcH;
360      texTemp.depth0 = 1;
361
362      tex = screen->texture_create(screen, &texTemp);
363      if (!tex)
364         return;
365
366      texSurf = screen->get_tex_surface(screen, tex, 0, 0, 0,
367                                        PIPE_BUFFER_USAGE_GPU_WRITE);
368
369      /* load temp texture */
370      if (pipe->surface_copy) {
371         pipe->surface_copy(pipe,
372                            texSurf, 0, 0,   /* dest */
373                            src, srcLeft, srcTop, /* src */
374                            srcW, srcH);     /* size */
375      } else {
376         util_surface_copy(pipe, FALSE,
377                           texSurf, 0, 0,   /* dest */
378                           src, srcLeft, srcTop, /* src */
379                           srcW, srcH);     /* size */
380      }
381
382      /* free the surface, update the texture if necessary.
383       */
384      pipe_surface_reference(&texSurf, NULL);
385      s0 = 0.0f;
386      s1 = 1.0f;
387      t0 = 0.0f;
388      t1 = 1.0f;
389   }
390   else {
391      pipe_texture_reference(&tex, src->texture);
392      s0 = srcX0 / (float)tex->width0;
393      s1 = srcX1 / (float)tex->width0;
394      t0 = srcY0 / (float)tex->height0;
395      t1 = srcY1 / (float)tex->height0;
396   }
397
398
399   /* save state (restored below) */
400   cso_save_blend(ctx->cso);
401   cso_save_depth_stencil_alpha(ctx->cso);
402   cso_save_rasterizer(ctx->cso);
403   cso_save_samplers(ctx->cso);
404   cso_save_sampler_textures(ctx->cso);
405   cso_save_framebuffer(ctx->cso);
406   cso_save_fragment_shader(ctx->cso);
407   cso_save_vertex_shader(ctx->cso);
408
409   /* set misc state we care about */
410   cso_set_blend(ctx->cso, &ctx->blend);
411   cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil);
412   cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
413
414   /* sampler */
415   ctx->sampler.min_img_filter = filter;
416   ctx->sampler.mag_img_filter = filter;
417   cso_single_sampler(ctx->cso, 0, &ctx->sampler);
418   cso_single_sampler_done(ctx->cso);
419
420   /* texture */
421   cso_set_sampler_textures(ctx->cso, 1, &tex);
422
423   if (ctx->fs[writemask] == NULL)
424      ctx->fs[writemask] =
425         util_make_fragment_tex_shader_writemask(pipe, TGSI_TEXTURE_2D,
426                                                 writemask);
427
428   /* shaders */
429   cso_set_fragment_shader_handle(ctx->cso, ctx->fs[writemask]);
430   cso_set_vertex_shader_handle(ctx->cso, ctx->vs);
431
432   /* drawing dest */
433   memset(&fb, 0, sizeof(fb));
434   fb.width = dst->width;
435   fb.height = dst->height;
436   fb.nr_cbufs = 1;
437   fb.cbufs[0] = dst;
438   cso_set_framebuffer(ctx->cso, &fb);
439
440   /* draw quad */
441   offset = setup_vertex_data_tex(ctx,
442                                  (float) dstX0, (float) dstY0,
443                                  (float) dstX1, (float) dstY1,
444                                  s0, t0,
445                                  s1, t1,
446                                  z);
447
448   util_draw_vertex_buffer(ctx->pipe, ctx->vbuf, offset,
449                           PIPE_PRIM_TRIANGLE_FAN,
450                           4,  /* verts */
451                           2); /* attribs/vert */
452
453   /* restore state we changed */
454   cso_restore_blend(ctx->cso);
455   cso_restore_depth_stencil_alpha(ctx->cso);
456   cso_restore_rasterizer(ctx->cso);
457   cso_restore_samplers(ctx->cso);
458   cso_restore_sampler_textures(ctx->cso);
459   cso_restore_framebuffer(ctx->cso);
460   cso_restore_fragment_shader(ctx->cso);
461   cso_restore_vertex_shader(ctx->cso);
462
463   pipe_texture_reference(&tex, NULL);
464}
465
466
467void
468util_blit_pixels(struct blit_state *ctx,
469                 struct pipe_surface *src,
470                 int srcX0, int srcY0,
471                 int srcX1, int srcY1,
472                 struct pipe_surface *dst,
473                 int dstX0, int dstY0,
474                 int dstX1, int dstY1,
475                 float z, uint filter )
476{
477   util_blit_pixels_writemask( ctx, src,
478                               srcX0, srcY0,
479                               srcX1, srcY1,
480                               dst,
481                               dstX0, dstY0,
482                               dstX1, dstY1,
483                               z, filter,
484                               TGSI_WRITEMASK_XYZW );
485}
486
487
488/* Release vertex buffer at end of frame to avoid synchronous
489 * rendering.
490 */
491void util_blit_flush( struct blit_state *ctx )
492{
493   pipe_buffer_reference(&ctx->vbuf, NULL);
494   ctx->vbuf_slot = 0;
495}
496
497
498
499/**
500 * Copy pixel block from src texture to dst surface.
501 * Overlapping regions are acceptable.
502 *
503 * XXX Should support selection of level.
504 * XXX need some control over blitting Z and/or stencil.
505 */
506void
507util_blit_pixels_tex(struct blit_state *ctx,
508                 struct pipe_texture *tex,
509                 int srcX0, int srcY0,
510                 int srcX1, int srcY1,
511                 struct pipe_surface *dst,
512                 int dstX0, int dstY0,
513                 int dstX1, int dstY1,
514                 float z, uint filter)
515{
516   struct pipe_framebuffer_state fb;
517   float s0, t0, s1, t1;
518   unsigned offset;
519
520   assert(filter == PIPE_TEX_MIPFILTER_NEAREST ||
521          filter == PIPE_TEX_MIPFILTER_LINEAR);
522
523   assert(tex->width0 != 0);
524   assert(tex->height0 != 0);
525
526   s0 = srcX0 / (float)tex->width0;
527   s1 = srcX1 / (float)tex->width0;
528   t0 = srcY0 / (float)tex->height0;
529   t1 = srcY1 / (float)tex->height0;
530
531   assert(ctx->pipe->screen->is_format_supported(ctx->pipe->screen, dst->format,
532                                                 PIPE_TEXTURE_2D,
533                                                 PIPE_TEXTURE_USAGE_RENDER_TARGET,
534                                                 0));
535
536   /* save state (restored below) */
537   cso_save_blend(ctx->cso);
538   cso_save_depth_stencil_alpha(ctx->cso);
539   cso_save_rasterizer(ctx->cso);
540   cso_save_samplers(ctx->cso);
541   cso_save_sampler_textures(ctx->cso);
542   cso_save_framebuffer(ctx->cso);
543   cso_save_fragment_shader(ctx->cso);
544   cso_save_vertex_shader(ctx->cso);
545
546   /* set misc state we care about */
547   cso_set_blend(ctx->cso, &ctx->blend);
548   cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil);
549   cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
550
551   /* sampler */
552   ctx->sampler.min_img_filter = filter;
553   ctx->sampler.mag_img_filter = filter;
554   cso_single_sampler(ctx->cso, 0, &ctx->sampler);
555   cso_single_sampler_done(ctx->cso);
556
557   /* texture */
558   cso_set_sampler_textures(ctx->cso, 1, &tex);
559
560   /* shaders */
561   cso_set_fragment_shader_handle(ctx->cso, ctx->fs[TGSI_WRITEMASK_XYZW]);
562   cso_set_vertex_shader_handle(ctx->cso, ctx->vs);
563
564   /* drawing dest */
565   memset(&fb, 0, sizeof(fb));
566   fb.width = dst->width;
567   fb.height = dst->height;
568   fb.nr_cbufs = 1;
569   fb.cbufs[0] = dst;
570   cso_set_framebuffer(ctx->cso, &fb);
571
572   /* draw quad */
573   offset = setup_vertex_data_tex(ctx,
574                                  (float) dstX0, (float) dstY0,
575                                  (float) dstX1, (float) dstY1,
576                                  s0, t0, s1, t1,
577                                  z);
578
579   util_draw_vertex_buffer(ctx->pipe,
580                           ctx->vbuf, offset,
581                           PIPE_PRIM_TRIANGLE_FAN,
582                           4,  /* verts */
583                           2); /* attribs/vert */
584
585   /* restore state we changed */
586   cso_restore_blend(ctx->cso);
587   cso_restore_depth_stencil_alpha(ctx->cso);
588   cso_restore_rasterizer(ctx->cso);
589   cso_restore_samplers(ctx->cso);
590   cso_restore_sampler_textures(ctx->cso);
591   cso_restore_framebuffer(ctx->cso);
592   cso_restore_fragment_shader(ctx->cso);
593   cso_restore_vertex_shader(ctx->cso);
594}
595