u_blit.c revision 6df42d80234d13676fc3207cf44f0e371e3372b5
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_format.h"
46#include "util/u_math.h"
47#include "util/u_memory.h"
48#include "util/u_simple_shaders.h"
49#include "util/u_surface.h"
50#include "util/u_rect.h"
51
52#include "cso_cache/cso_context.h"
53
54
55struct blit_state
56{
57   struct pipe_context *pipe;
58   struct cso_context *cso;
59
60   struct pipe_blend_state blend;
61   struct pipe_depth_stencil_alpha_state depthstencil;
62   struct pipe_rasterizer_state rasterizer;
63   struct pipe_sampler_state sampler;
64   struct pipe_viewport_state viewport;
65
66   void *vs;
67   void *fs[TGSI_WRITEMASK_XYZW + 1];
68
69   struct pipe_buffer *vbuf;  /**< quad vertices */
70   unsigned vbuf_slot;
71
72   float vertices[4][2][4];   /**< vertex/texcoords for quad */
73};
74
75
76/**
77 * Create state object for blit.
78 * Intended to be created once and re-used for many blit() calls.
79 */
80struct blit_state *
81util_create_blit(struct pipe_context *pipe, struct cso_context *cso)
82{
83   struct blit_state *ctx;
84   uint i;
85
86   ctx = CALLOC_STRUCT(blit_state);
87   if (!ctx)
88      return NULL;
89
90   ctx->pipe = pipe;
91   ctx->cso = cso;
92
93   /* disabled blending/masking */
94   memset(&ctx->blend, 0, sizeof(ctx->blend));
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_vs_clip_and_viewport = 1;
105   ctx->rasterizer.gl_rasterization_rules = 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
118   /* vertex shader - still required to provide the linkage between
119    * fragment shader input semantics and vertex_element/buffers.
120    */
121   {
122      const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
123                                      TGSI_SEMANTIC_GENERIC };
124      const uint semantic_indexes[] = { 0, 0 };
125      ctx->vs = util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
126                                                    semantic_indexes);
127   }
128
129   /* fragment shader */
130   ctx->fs[TGSI_WRITEMASK_XYZW] = util_make_fragment_tex_shader(pipe);
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      util_format_get_block(src->format, &texTemp.block);
362
363      tex = screen->texture_create(screen, &texTemp);
364      if (!tex)
365         return;
366
367      texSurf = screen->get_tex_surface(screen, tex, 0, 0, 0,
368                                        PIPE_BUFFER_USAGE_GPU_WRITE);
369
370      /* load temp texture */
371      if (pipe->surface_copy) {
372         pipe->surface_copy(pipe,
373                            texSurf, 0, 0,   /* dest */
374                            src, srcLeft, srcTop, /* src */
375                            srcW, srcH);     /* size */
376      } else {
377         util_surface_copy(pipe, FALSE,
378                           texSurf, 0, 0,   /* dest */
379                           src, srcLeft, srcTop, /* src */
380                           srcW, srcH);     /* size */
381      }
382
383      /* free the surface, update the texture if necessary.
384       */
385      pipe_surface_reference(&texSurf, NULL);
386      s0 = 0.0f;
387      s1 = 1.0f;
388      t0 = 0.0f;
389      t1 = 1.0f;
390   }
391   else {
392      pipe_texture_reference(&tex, src->texture);
393      s0 = srcX0 / (float)tex->width0;
394      s1 = srcX1 / (float)tex->width0;
395      t0 = srcY0 / (float)tex->height0;
396      t1 = srcY1 / (float)tex->height0;
397   }
398
399
400   /* save state (restored below) */
401   cso_save_blend(ctx->cso);
402   cso_save_depth_stencil_alpha(ctx->cso);
403   cso_save_rasterizer(ctx->cso);
404   cso_save_samplers(ctx->cso);
405   cso_save_sampler_textures(ctx->cso);
406   cso_save_framebuffer(ctx->cso);
407   cso_save_fragment_shader(ctx->cso);
408   cso_save_vertex_shader(ctx->cso);
409
410   /* set misc state we care about */
411   cso_set_blend(ctx->cso, &ctx->blend);
412   cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil);
413   cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
414
415   /* sampler */
416   ctx->sampler.min_img_filter = filter;
417   ctx->sampler.mag_img_filter = filter;
418   cso_single_sampler(ctx->cso, 0, &ctx->sampler);
419   cso_single_sampler_done(ctx->cso);
420
421   /* texture */
422   cso_set_sampler_textures(ctx->cso, 1, &tex);
423
424   if (ctx->fs[writemask] == NULL)
425      ctx->fs[writemask] = util_make_fragment_tex_shader_writemask(pipe, writemask);
426
427   /* shaders */
428   cso_set_fragment_shader_handle(ctx->cso, ctx->fs[writemask]);
429   cso_set_vertex_shader_handle(ctx->cso, ctx->vs);
430
431   /* drawing dest */
432   memset(&fb, 0, sizeof(fb));
433   fb.width = dst->width;
434   fb.height = dst->height;
435   fb.nr_cbufs = 1;
436   fb.cbufs[0] = dst;
437   cso_set_framebuffer(ctx->cso, &fb);
438
439   /* draw quad */
440   offset = setup_vertex_data_tex(ctx,
441                                  (float) dstX0, (float) dstY0,
442                                  (float) dstX1, (float) dstY1,
443                                  s0, t0,
444                                  s1, t1,
445                                  z);
446
447   util_draw_vertex_buffer(ctx->pipe, ctx->vbuf, offset,
448                           PIPE_PRIM_TRIANGLE_FAN,
449                           4,  /* verts */
450                           2); /* attribs/vert */
451
452   /* restore state we changed */
453   cso_restore_blend(ctx->cso);
454   cso_restore_depth_stencil_alpha(ctx->cso);
455   cso_restore_rasterizer(ctx->cso);
456   cso_restore_samplers(ctx->cso);
457   cso_restore_sampler_textures(ctx->cso);
458   cso_restore_framebuffer(ctx->cso);
459   cso_restore_fragment_shader(ctx->cso);
460   cso_restore_vertex_shader(ctx->cso);
461
462   pipe_texture_reference(&tex, NULL);
463}
464
465
466void
467util_blit_pixels(struct blit_state *ctx,
468                 struct pipe_surface *src,
469                 int srcX0, int srcY0,
470                 int srcX1, int srcY1,
471                 struct pipe_surface *dst,
472                 int dstX0, int dstY0,
473                 int dstX1, int dstY1,
474                 float z, uint filter )
475{
476   util_blit_pixels_writemask( ctx, src,
477                               srcX0, srcY0,
478                               srcX1, srcY1,
479                               dst,
480                               dstX0, dstY0,
481                               dstX1, dstY1,
482                               z, filter,
483                               TGSI_WRITEMASK_XYZW );
484}
485
486
487/* Release vertex buffer at end of frame to avoid synchronous
488 * rendering.
489 */
490void util_blit_flush( struct blit_state *ctx )
491{
492   pipe_buffer_reference(&ctx->vbuf, NULL);
493   ctx->vbuf_slot = 0;
494}
495
496
497
498/**
499 * Copy pixel block from src texture to dst surface.
500 * Overlapping regions are acceptable.
501 *
502 * XXX Should support selection of level.
503 * XXX need some control over blitting Z and/or stencil.
504 */
505void
506util_blit_pixels_tex(struct blit_state *ctx,
507                 struct pipe_texture *tex,
508                 int srcX0, int srcY0,
509                 int srcX1, int srcY1,
510                 struct pipe_surface *dst,
511                 int dstX0, int dstY0,
512                 int dstX1, int dstY1,
513                 float z, uint filter)
514{
515   struct pipe_framebuffer_state fb;
516   float s0, t0, s1, t1;
517   unsigned offset;
518
519   assert(filter == PIPE_TEX_MIPFILTER_NEAREST ||
520          filter == PIPE_TEX_MIPFILTER_LINEAR);
521
522   assert(tex->width0 != 0);
523   assert(tex->height0 != 0);
524
525   s0 = srcX0 / (float)tex->width0;
526   s1 = srcX1 / (float)tex->width0;
527   t0 = srcY0 / (float)tex->height0;
528   t1 = srcY1 / (float)tex->height0;
529
530   assert(ctx->pipe->screen->is_format_supported(ctx->pipe->screen, dst->format,
531                                                 PIPE_TEXTURE_2D,
532                                                 PIPE_TEXTURE_USAGE_RENDER_TARGET,
533                                                 0));
534
535   /* save state (restored below) */
536   cso_save_blend(ctx->cso);
537   cso_save_depth_stencil_alpha(ctx->cso);
538   cso_save_rasterizer(ctx->cso);
539   cso_save_samplers(ctx->cso);
540   cso_save_sampler_textures(ctx->cso);
541   cso_save_framebuffer(ctx->cso);
542   cso_save_fragment_shader(ctx->cso);
543   cso_save_vertex_shader(ctx->cso);
544
545   /* set misc state we care about */
546   cso_set_blend(ctx->cso, &ctx->blend);
547   cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil);
548   cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
549
550   /* sampler */
551   ctx->sampler.min_img_filter = filter;
552   ctx->sampler.mag_img_filter = filter;
553   cso_single_sampler(ctx->cso, 0, &ctx->sampler);
554   cso_single_sampler_done(ctx->cso);
555
556   /* texture */
557   cso_set_sampler_textures(ctx->cso, 1, &tex);
558
559   /* shaders */
560   cso_set_fragment_shader_handle(ctx->cso, ctx->fs[TGSI_WRITEMASK_XYZW]);
561   cso_set_vertex_shader_handle(ctx->cso, ctx->vs);
562
563   /* drawing dest */
564   memset(&fb, 0, sizeof(fb));
565   fb.width = dst->width;
566   fb.height = dst->height;
567   fb.nr_cbufs = 1;
568   fb.cbufs[0] = dst;
569   cso_set_framebuffer(ctx->cso, &fb);
570
571   /* draw quad */
572   offset = setup_vertex_data_tex(ctx,
573                                  (float) dstX0, (float) dstY0,
574                                  (float) dstX1, (float) dstY1,
575                                  s0, t0, s1, t1,
576                                  z);
577
578   util_draw_vertex_buffer(ctx->pipe,
579                           ctx->vbuf, offset,
580                           PIPE_PRIM_TRIANGLE_FAN,
581                           4,  /* verts */
582                           2); /* attribs/vert */
583
584   /* restore state we changed */
585   cso_restore_blend(ctx->cso);
586   cso_restore_depth_stencil_alpha(ctx->cso);
587   cso_restore_rasterizer(ctx->cso);
588   cso_restore_samplers(ctx->cso);
589   cso_restore_sampler_textures(ctx->cso);
590   cso_restore_framebuffer(ctx->cso);
591   cso_restore_fragment_shader(ctx->cso);
592   cso_restore_vertex_shader(ctx->cso);
593}
594