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