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