u_blit.c revision bb567357bc1366df7115e0daa68c2470e3bf6ba6
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] = util_make_fragment_tex_shader(pipe);
130   ctx->vbuf = NULL;
131
132   /* init vertex data that doesn't change */
133   for (i = 0; i < 4; i++) {
134      ctx->vertices[i][0][3] = 1.0f; /* w */
135      ctx->vertices[i][1][2] = 0.0f; /* r */
136      ctx->vertices[i][1][3] = 1.0f; /* q */
137   }
138
139   return ctx;
140}
141
142
143/**
144 * Destroy a blit context
145 */
146void
147util_destroy_blit(struct blit_state *ctx)
148{
149   struct pipe_context *pipe = ctx->pipe;
150   unsigned i;
151
152   pipe->delete_vs_state(pipe, ctx->vs);
153
154   for (i = 0; i < Elements(ctx->fs); i++)
155      if (ctx->fs[i])
156         pipe->delete_fs_state(pipe, ctx->fs[i]);
157
158   pipe_buffer_reference(&ctx->vbuf, NULL);
159
160   FREE(ctx);
161}
162
163
164/**
165 * Get offset of next free slot in vertex buffer for quad vertices.
166 */
167static unsigned
168get_next_slot( struct blit_state *ctx )
169{
170   const unsigned max_slots = 4096 / sizeof ctx->vertices;
171
172   if (ctx->vbuf_slot >= max_slots)
173      util_blit_flush( ctx );
174
175   if (!ctx->vbuf) {
176      ctx->vbuf = pipe_buffer_create(ctx->pipe->screen,
177                                     32,
178                                     PIPE_BUFFER_USAGE_VERTEX,
179                                     max_slots * sizeof ctx->vertices);
180   }
181
182   return ctx->vbuf_slot++ * sizeof ctx->vertices;
183}
184
185
186
187
188
189/**
190 * Setup vertex data for the textured quad we'll draw.
191 * Note: y=0=top
192 */
193static unsigned
194setup_vertex_data_tex(struct blit_state *ctx,
195                      float x0, float y0, float x1, float y1,
196                      float s0, float t0, float s1, float t1,
197                      float z)
198{
199   unsigned offset;
200
201   ctx->vertices[0][0][0] = x0;
202   ctx->vertices[0][0][1] = y0;
203   ctx->vertices[0][0][2] = z;
204   ctx->vertices[0][1][0] = s0; /*s*/
205   ctx->vertices[0][1][1] = t0; /*t*/
206
207   ctx->vertices[1][0][0] = x1;
208   ctx->vertices[1][0][1] = y0;
209   ctx->vertices[1][0][2] = z;
210   ctx->vertices[1][1][0] = s1; /*s*/
211   ctx->vertices[1][1][1] = t0; /*t*/
212
213   ctx->vertices[2][0][0] = x1;
214   ctx->vertices[2][0][1] = y1;
215   ctx->vertices[2][0][2] = z;
216   ctx->vertices[2][1][0] = s1;
217   ctx->vertices[2][1][1] = t1;
218
219   ctx->vertices[3][0][0] = x0;
220   ctx->vertices[3][0][1] = y1;
221   ctx->vertices[3][0][2] = z;
222   ctx->vertices[3][1][0] = s0;
223   ctx->vertices[3][1][1] = t1;
224
225   offset = get_next_slot( ctx );
226
227   pipe_buffer_write(ctx->pipe->screen, ctx->vbuf,
228                     offset, sizeof(ctx->vertices), ctx->vertices);
229
230   return offset;
231}
232
233
234/**
235 * \return TRUE if two regions overlap, FALSE otherwise
236 */
237static boolean
238regions_overlap(int srcX0, int srcY0,
239                int srcX1, int srcY1,
240                int dstX0, int dstY0,
241                int dstX1, int dstY1)
242{
243   if (MAX2(srcX0, srcX1) < MIN2(dstX0, dstX1))
244      return FALSE; /* src completely left of dst */
245
246   if (MAX2(dstX0, dstX1) < MIN2(srcX0, srcX1))
247      return FALSE; /* dst completely left of src */
248
249   if (MAX2(srcY0, srcY1) < MIN2(dstY0, dstY1))
250      return FALSE; /* src completely above dst */
251
252   if (MAX2(dstY0, dstY1) < MIN2(srcY0, srcY1))
253      return FALSE; /* dst completely above src */
254
255   return TRUE; /* some overlap */
256}
257
258
259/**
260 * Copy pixel block from src surface to dst surface.
261 * Overlapping regions are acceptable.
262 * Flipping and stretching are supported.
263 * XXX what about clipping???
264 * XXX need some control over blitting Z and/or stencil.
265 */
266void
267util_blit_pixels_writemask(struct blit_state *ctx,
268                           struct pipe_surface *src,
269                           int srcX0, int srcY0,
270                           int srcX1, int srcY1,
271                           struct pipe_surface *dst,
272                           int dstX0, int dstY0,
273                           int dstX1, int dstY1,
274                           float z, uint filter,
275                           uint writemask)
276{
277   struct pipe_context *pipe = ctx->pipe;
278   struct pipe_screen *screen = pipe->screen;
279   struct pipe_texture *tex = NULL;
280   struct pipe_framebuffer_state fb;
281   const int srcW = abs(srcX1 - srcX0);
282   const int srcH = abs(srcY1 - srcY0);
283   unsigned offset;
284   boolean overlap;
285   float s0, t0, s1, t1;
286
287   assert(filter == PIPE_TEX_MIPFILTER_NEAREST ||
288          filter == PIPE_TEX_MIPFILTER_LINEAR);
289
290   assert(screen->is_format_supported(screen, src->format, PIPE_TEXTURE_2D,
291                                      PIPE_TEXTURE_USAGE_SAMPLER, 0));
292   assert(screen->is_format_supported(screen, dst->format, PIPE_TEXTURE_2D,
293                                      PIPE_TEXTURE_USAGE_RENDER_TARGET, 0));
294
295   /* do the regions overlap? */
296   overlap = util_same_surface(src, dst) &&
297      regions_overlap(srcX0, srcY0, srcX1, srcY1,
298                      dstX0, dstY0, dstX1, dstY1);
299
300   /*
301    * Check for simple case:  no format conversion, no flipping, no stretching,
302    * no overlapping.
303    * Filter mode should not matter since there's no stretching.
304    */
305   if (pipe->surface_copy &&
306       dst->format == src->format &&
307       srcX0 < srcX1 &&
308       dstX0 < dstX1 &&
309       srcY0 < srcY1 &&
310       dstY0 < dstY1 &&
311       (dstX1 - dstX0) == (srcX1 - srcX0) &&
312       (dstY1 - dstY0) == (srcY1 - srcY0) &&
313       !overlap) {
314      pipe->surface_copy(pipe,
315			 dst, dstX0, dstY0, /* dest */
316			 src, srcX0, srcY0, /* src */
317			 srcW, srcH);       /* size */
318      return;
319   }
320
321   assert(screen->is_format_supported(screen, dst->format, PIPE_TEXTURE_2D,
322                                      PIPE_TEXTURE_USAGE_RENDER_TARGET, 0));
323
324   /* Create a temporary texture when src and dest alias or when src
325    * is anything other than a single-level 2d texture.
326    *
327    * This can still be improved upon.
328    */
329   if (util_same_surface(src, dst) ||
330       src->texture->target != PIPE_TEXTURE_2D ||
331       src->texture->last_level != 0)
332   {
333      struct pipe_texture texTemp;
334      struct pipe_surface *texSurf;
335      const int srcLeft = MIN2(srcX0, srcX1);
336      const int srcTop = MIN2(srcY0, srcY1);
337
338      if (srcLeft != srcX0) {
339         /* left-right flip */
340         int tmp = dstX0;
341         dstX0 = dstX1;
342         dstX1 = tmp;
343      }
344
345      if (srcTop != srcY0) {
346         /* up-down flip */
347         int tmp = dstY0;
348         dstY0 = dstY1;
349         dstY1 = tmp;
350      }
351
352      /* create temp texture */
353      memset(&texTemp, 0, sizeof(texTemp));
354      texTemp.target = PIPE_TEXTURE_2D;
355      texTemp.format = src->format;
356      texTemp.last_level = 0;
357      texTemp.width[0] = srcW;
358      texTemp.height[0] = srcH;
359      texTemp.depth[0] = 1;
360      pf_get_block(src->format, &texTemp.block);
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->width[0];
393      s1 = srcX1 / (float)tex->width[0];
394      t0 = srcY0 / (float)tex->height[0];
395      t1 = srcY1 / (float)tex->height[0];
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] = util_make_fragment_tex_shader_writemask(pipe, writemask);
425
426   /* shaders */
427   cso_set_fragment_shader_handle(ctx->cso, ctx->fs[writemask]);
428   cso_set_vertex_shader_handle(ctx->cso, ctx->vs);
429
430   /* drawing dest */
431   memset(&fb, 0, sizeof(fb));
432   fb.width = dst->width;
433   fb.height = dst->height;
434   fb.nr_cbufs = 1;
435   fb.cbufs[0] = dst;
436   cso_set_framebuffer(ctx->cso, &fb);
437
438   /* draw quad */
439   offset = setup_vertex_data_tex(ctx,
440                                  (float) dstX0, (float) dstY0,
441                                  (float) dstX1, (float) dstY1,
442                                  s0, t0,
443                                  s1, t1,
444                                  z);
445
446   util_draw_vertex_buffer(ctx->pipe, ctx->vbuf, offset,
447                           PIPE_PRIM_TRIANGLE_FAN,
448                           4,  /* verts */
449                           2); /* attribs/vert */
450
451   /* restore state we changed */
452   cso_restore_blend(ctx->cso);
453   cso_restore_depth_stencil_alpha(ctx->cso);
454   cso_restore_rasterizer(ctx->cso);
455   cso_restore_samplers(ctx->cso);
456   cso_restore_sampler_textures(ctx->cso);
457   cso_restore_framebuffer(ctx->cso);
458   cso_restore_fragment_shader(ctx->cso);
459   cso_restore_vertex_shader(ctx->cso);
460
461   pipe_texture_reference(&tex, NULL);
462}
463
464
465void
466util_blit_pixels(struct blit_state *ctx,
467                 struct pipe_surface *src,
468                 int srcX0, int srcY0,
469                 int srcX1, int srcY1,
470                 struct pipe_surface *dst,
471                 int dstX0, int dstY0,
472                 int dstX1, int dstY1,
473                 float z, uint filter )
474{
475   util_blit_pixels_writemask( ctx, src,
476                               srcX0, srcY0,
477                               srcX1, srcY1,
478                               dst,
479                               dstX0, dstY0,
480                               dstX1, dstY1,
481                               z, filter,
482                               TGSI_WRITEMASK_XYZW );
483}
484
485
486/* Release vertex buffer at end of frame to avoid synchronous
487 * rendering.
488 */
489void util_blit_flush( struct blit_state *ctx )
490{
491   pipe_buffer_reference(&ctx->vbuf, NULL);
492   ctx->vbuf_slot = 0;
493}
494
495
496
497/**
498 * Copy pixel block from src texture to dst surface.
499 * Overlapping regions are acceptable.
500 *
501 * XXX Should support selection of level.
502 * XXX need some control over blitting Z and/or stencil.
503 */
504void
505util_blit_pixels_tex(struct blit_state *ctx,
506                 struct pipe_texture *tex,
507                 int srcX0, int srcY0,
508                 int srcX1, int srcY1,
509                 struct pipe_surface *dst,
510                 int dstX0, int dstY0,
511                 int dstX1, int dstY1,
512                 float z, uint filter)
513{
514   struct pipe_framebuffer_state fb;
515   float s0, t0, s1, t1;
516   unsigned offset;
517
518   assert(filter == PIPE_TEX_MIPFILTER_NEAREST ||
519          filter == PIPE_TEX_MIPFILTER_LINEAR);
520
521   assert(tex->width[0] != 0);
522   assert(tex->height[0] != 0);
523
524   s0 = srcX0 / (float)tex->width[0];
525   s1 = srcX1 / (float)tex->width[0];
526   t0 = srcY0 / (float)tex->height[0];
527   t1 = srcY1 / (float)tex->height[0];
528
529   assert(ctx->pipe->screen->is_format_supported(ctx->pipe->screen, dst->format,
530                                                 PIPE_TEXTURE_2D,
531                                                 PIPE_TEXTURE_USAGE_RENDER_TARGET,
532                                                 0));
533
534   /* save state (restored below) */
535   cso_save_blend(ctx->cso);
536   cso_save_depth_stencil_alpha(ctx->cso);
537   cso_save_rasterizer(ctx->cso);
538   cso_save_samplers(ctx->cso);
539   cso_save_sampler_textures(ctx->cso);
540   cso_save_framebuffer(ctx->cso);
541   cso_save_fragment_shader(ctx->cso);
542   cso_save_vertex_shader(ctx->cso);
543
544   /* set misc state we care about */
545   cso_set_blend(ctx->cso, &ctx->blend);
546   cso_set_depth_stencil_alpha(ctx->cso, &ctx->depthstencil);
547   cso_set_rasterizer(ctx->cso, &ctx->rasterizer);
548
549   /* sampler */
550   ctx->sampler.min_img_filter = filter;
551   ctx->sampler.mag_img_filter = filter;
552   cso_single_sampler(ctx->cso, 0, &ctx->sampler);
553   cso_single_sampler_done(ctx->cso);
554
555   /* texture */
556   cso_set_sampler_textures(ctx->cso, 1, &tex);
557
558   /* shaders */
559   cso_set_fragment_shader_handle(ctx->cso, ctx->fs[TGSI_WRITEMASK_XYZW]);
560   cso_set_vertex_shader_handle(ctx->cso, ctx->vs);
561
562   /* drawing dest */
563   memset(&fb, 0, sizeof(fb));
564   fb.width = dst->width;
565   fb.height = dst->height;
566   fb.nr_cbufs = 1;
567   fb.cbufs[0] = dst;
568   cso_set_framebuffer(ctx->cso, &fb);
569
570   /* draw quad */
571   offset = setup_vertex_data_tex(ctx,
572                                  (float) dstX0, (float) dstY0,
573                                  (float) dstX1, (float) dstY1,
574                                  s0, t0, s1, t1,
575                                  z);
576
577   util_draw_vertex_buffer(ctx->pipe,
578                           ctx->vbuf, offset,
579                           PIPE_PRIM_TRIANGLE_FAN,
580                           4,  /* verts */
581                           2); /* attribs/vert */
582
583   /* restore state we changed */
584   cso_restore_blend(ctx->cso);
585   cso_restore_depth_stencil_alpha(ctx->cso);
586   cso_restore_rasterizer(ctx->cso);
587   cso_restore_samplers(ctx->cso);
588   cso_restore_sampler_textures(ctx->cso);
589   cso_restore_framebuffer(ctx->cso);
590   cso_restore_fragment_shader(ctx->cso);
591   cso_restore_vertex_shader(ctx->cso);
592}
593