1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Copy/blit pixel rect between surfaces
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"
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_sampler.h"
49 #include "util/u_simple_shaders.h"
50 #include "util/u_surface.h"
52 #include "cso_cache/cso_context.h"
57 struct pipe_context
*pipe
;
58 struct cso_context
*cso
;
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_clip_state clip
;
66 struct pipe_vertex_element velem
[2];
69 void *fs
[TGSI_WRITEMASK_XYZW
+ 1];
71 struct pipe_resource
*vbuf
; /**< quad vertices */
74 float vertices
[4][2][4]; /**< vertex/texcoords for quad */
79 * Create state object for blit.
80 * Intended to be created once and re-used for many blit() calls.
83 util_create_blit(struct pipe_context
*pipe
, struct cso_context
*cso
)
85 struct blit_state
*ctx
;
88 ctx
= CALLOC_STRUCT(blit_state
);
95 /* disabled blending/masking */
96 memset(&ctx
->blend
, 0, sizeof(ctx
->blend
));
97 ctx
->blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
99 /* no-op depth/stencil/alpha */
100 memset(&ctx
->depthstencil
, 0, sizeof(ctx
->depthstencil
));
103 memset(&ctx
->rasterizer
, 0, sizeof(ctx
->rasterizer
));
104 ctx
->rasterizer
.front_winding
= PIPE_WINDING_CW
;
105 ctx
->rasterizer
.cull_mode
= PIPE_WINDING_NONE
;
106 ctx
->rasterizer
.gl_rasterization_rules
= 1;
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;
118 /* vertex elements state */
119 memset(&ctx
->velem
[0], 0, sizeof(ctx
->velem
[0]) * 2);
120 for (i
= 0; i
< 2; i
++) {
121 ctx
->velem
[i
].src_offset
= i
* 4 * sizeof(float);
122 ctx
->velem
[i
].instance_divisor
= 0;
123 ctx
->velem
[i
].vertex_buffer_index
= 0;
124 ctx
->velem
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
127 /* vertex shader - still required to provide the linkage between
128 * fragment shader input semantics and vertex_element/buffers.
131 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
132 TGSI_SEMANTIC_GENERIC
};
133 const uint semantic_indexes
[] = { 0, 0 };
134 ctx
->vs
= util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
138 /* fragment shader */
139 ctx
->fs
[TGSI_WRITEMASK_XYZW
] =
140 util_make_fragment_tex_shader(pipe
, TGSI_TEXTURE_2D
);
143 /* init vertex data that doesn't change */
144 for (i
= 0; i
< 4; i
++) {
145 ctx
->vertices
[i
][0][3] = 1.0f
; /* w */
146 ctx
->vertices
[i
][1][2] = 0.0f
; /* r */
147 ctx
->vertices
[i
][1][3] = 1.0f
; /* q */
155 * Destroy a blit context
158 util_destroy_blit(struct blit_state
*ctx
)
160 struct pipe_context
*pipe
= ctx
->pipe
;
163 pipe
->delete_vs_state(pipe
, ctx
->vs
);
165 for (i
= 0; i
< Elements(ctx
->fs
); i
++)
167 pipe
->delete_fs_state(pipe
, ctx
->fs
[i
]);
169 pipe_resource_reference(&ctx
->vbuf
, NULL
);
176 * Get offset of next free slot in vertex buffer for quad vertices.
179 get_next_slot( struct blit_state
*ctx
)
181 const unsigned max_slots
= 4096 / sizeof ctx
->vertices
;
183 if (ctx
->vbuf_slot
>= max_slots
)
184 util_blit_flush( ctx
);
187 ctx
->vbuf
= pipe_buffer_create(ctx
->pipe
->screen
,
188 PIPE_BIND_VERTEX_BUFFER
,
189 max_slots
* sizeof ctx
->vertices
);
192 return ctx
->vbuf_slot
++ * sizeof ctx
->vertices
;
200 * Setup vertex data for the textured quad we'll draw.
204 setup_vertex_data_tex(struct blit_state
*ctx
,
205 float x0
, float y0
, float x1
, float y1
,
206 float s0
, float t0
, float s1
, float t1
,
211 ctx
->vertices
[0][0][0] = x0
;
212 ctx
->vertices
[0][0][1] = y0
;
213 ctx
->vertices
[0][0][2] = z
;
214 ctx
->vertices
[0][1][0] = s0
; /*s*/
215 ctx
->vertices
[0][1][1] = t0
; /*t*/
217 ctx
->vertices
[1][0][0] = x1
;
218 ctx
->vertices
[1][0][1] = y0
;
219 ctx
->vertices
[1][0][2] = z
;
220 ctx
->vertices
[1][1][0] = s1
; /*s*/
221 ctx
->vertices
[1][1][1] = t0
; /*t*/
223 ctx
->vertices
[2][0][0] = x1
;
224 ctx
->vertices
[2][0][1] = y1
;
225 ctx
->vertices
[2][0][2] = z
;
226 ctx
->vertices
[2][1][0] = s1
;
227 ctx
->vertices
[2][1][1] = t1
;
229 ctx
->vertices
[3][0][0] = x0
;
230 ctx
->vertices
[3][0][1] = y1
;
231 ctx
->vertices
[3][0][2] = z
;
232 ctx
->vertices
[3][1][0] = s0
;
233 ctx
->vertices
[3][1][1] = t1
;
235 offset
= get_next_slot( ctx
);
237 pipe_buffer_write_nooverlap(ctx
->pipe
, ctx
->vbuf
,
238 offset
, sizeof(ctx
->vertices
), ctx
->vertices
);
245 * \return TRUE if two regions overlap, FALSE otherwise
248 regions_overlap(int srcX0
, int srcY0
,
249 int srcX1
, int srcY1
,
250 int dstX0
, int dstY0
,
251 int dstX1
, int dstY1
)
253 if (MAX2(srcX0
, srcX1
) < MIN2(dstX0
, dstX1
))
254 return FALSE
; /* src completely left of dst */
256 if (MAX2(dstX0
, dstX1
) < MIN2(srcX0
, srcX1
))
257 return FALSE
; /* dst completely left of src */
259 if (MAX2(srcY0
, srcY1
) < MIN2(dstY0
, dstY1
))
260 return FALSE
; /* src completely above dst */
262 if (MAX2(dstY0
, dstY1
) < MIN2(srcY0
, srcY1
))
263 return FALSE
; /* dst completely above src */
265 return TRUE
; /* some overlap */
270 * Copy pixel block from src surface to dst surface.
271 * Overlapping regions are acceptable.
272 * Flipping and stretching are supported.
273 * \param filter one of PIPE_TEX_MIPFILTER_NEAREST/LINEAR
274 * \param writemask controls which channels in the dest surface are sourced
275 * from the src surface. Disabled channels are sourced
277 * XXX need some control over blitting Z and/or stencil.
280 util_blit_pixels_writemask(struct blit_state
*ctx
,
281 struct pipe_surface
*src
,
282 struct pipe_sampler_view
*src_sampler_view
,
283 int srcX0
, int srcY0
,
284 int srcX1
, int srcY1
,
285 struct pipe_surface
*dst
,
286 int dstX0
, int dstY0
,
287 int dstX1
, int dstY1
,
288 float z
, uint filter
,
291 struct pipe_context
*pipe
= ctx
->pipe
;
292 struct pipe_screen
*screen
= pipe
->screen
;
293 struct pipe_sampler_view
*sampler_view
= NULL
;
294 struct pipe_framebuffer_state fb
;
295 const int srcW
= abs(srcX1
- srcX0
);
296 const int srcH
= abs(srcY1
- srcY0
);
299 float s0
, t0
, s1
, t1
;
301 assert(filter
== PIPE_TEX_MIPFILTER_NEAREST
||
302 filter
== PIPE_TEX_MIPFILTER_LINEAR
);
304 assert(screen
->is_format_supported(screen
, src
->format
, PIPE_TEXTURE_2D
,
305 PIPE_BIND_SAMPLER_VIEW
, 0));
306 assert(screen
->is_format_supported(screen
, dst
->format
, PIPE_TEXTURE_2D
,
307 PIPE_BIND_RENDER_TARGET
, 0));
309 /* do the regions overlap? */
310 overlap
= util_same_surface(src
, dst
) &&
311 regions_overlap(srcX0
, srcY0
, srcX1
, srcY1
,
312 dstX0
, dstY0
, dstX1
, dstY1
);
315 * Check for simple case: no format conversion, no flipping, no stretching,
317 * Filter mode should not matter since there's no stretching.
319 if (pipe
->surface_copy
&&
320 dst
->format
== src
->format
&&
325 (dstX1
- dstX0
) == (srcX1
- srcX0
) &&
326 (dstY1
- dstY0
) == (srcY1
- srcY0
) &&
328 pipe
->surface_copy(pipe
,
329 dst
, dstX0
, dstY0
, /* dest */
330 src
, srcX0
, srcY0
, /* src */
331 srcW
, srcH
); /* size */
335 assert(screen
->is_format_supported(screen
, dst
->format
, PIPE_TEXTURE_2D
,
336 PIPE_BIND_RENDER_TARGET
, 0));
338 /* Create a temporary texture when src and dest alias or when src
339 * is anything other than a single-level 2d texture.
341 * This can still be improved upon.
343 if (util_same_surface(src
, dst
) ||
344 src
->texture
->target
!= PIPE_TEXTURE_2D
||
345 src
->texture
->last_level
!= 0)
347 struct pipe_resource texTemp
;
348 struct pipe_resource
*tex
;
349 struct pipe_sampler_view sv_templ
;
350 struct pipe_surface
*texSurf
;
351 const int srcLeft
= MIN2(srcX0
, srcX1
);
352 const int srcTop
= MIN2(srcY0
, srcY1
);
354 if (srcLeft
!= srcX0
) {
355 /* left-right flip */
361 if (srcTop
!= srcY0
) {
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
;
376 texTemp
.bind
= PIPE_BIND_SAMPLER_VIEW
;
378 tex
= screen
->resource_create(screen
, &texTemp
);
382 u_sampler_view_default_template(&sv_templ
, tex
, tex
->format
);
384 sampler_view
= ctx
->pipe
->create_sampler_view(ctx
->pipe
, tex
, &sv_templ
);
386 pipe_resource_reference(&tex
, NULL
);
390 texSurf
= screen
->get_tex_surface(screen
, tex
, 0, 0, 0,
391 PIPE_BIND_BLIT_DESTINATION
);
393 /* load temp texture */
394 if (pipe
->surface_copy
) {
395 pipe
->surface_copy(pipe
,
396 texSurf
, 0, 0, /* dest */
397 src
, srcLeft
, srcTop
, /* src */
398 srcW
, srcH
); /* size */
400 util_surface_copy(pipe
, FALSE
,
401 texSurf
, 0, 0, /* dest */
402 src
, srcLeft
, srcTop
, /* src */
403 srcW
, srcH
); /* size */
406 /* free the surface, update the texture if necessary.
408 pipe_surface_reference(&texSurf
, NULL
);
414 pipe_resource_reference(&tex
, NULL
);
417 pipe_sampler_view_reference(&sampler_view
, src_sampler_view
);
418 s0
= srcX0
/ (float)src
->texture
->width0
;
419 s1
= srcX1
/ (float)src
->texture
->width0
;
420 t0
= srcY0
/ (float)src
->texture
->height0
;
421 t1
= srcY1
/ (float)src
->texture
->height0
;
426 /* save state (restored below) */
427 cso_save_blend(ctx
->cso
);
428 cso_save_depth_stencil_alpha(ctx
->cso
);
429 cso_save_rasterizer(ctx
->cso
);
430 cso_save_samplers(ctx
->cso
);
431 cso_save_fragment_sampler_views(ctx
->cso
);
432 cso_save_viewport(ctx
->cso
);
433 cso_save_framebuffer(ctx
->cso
);
434 cso_save_fragment_shader(ctx
->cso
);
435 cso_save_vertex_shader(ctx
->cso
);
436 cso_save_clip(ctx
->cso
);
437 cso_save_vertex_elements(ctx
->cso
);
439 /* set misc state we care about */
440 cso_set_blend(ctx
->cso
, &ctx
->blend
);
441 cso_set_depth_stencil_alpha(ctx
->cso
, &ctx
->depthstencil
);
442 cso_set_rasterizer(ctx
->cso
, &ctx
->rasterizer
);
443 cso_set_clip(ctx
->cso
, &ctx
->clip
);
444 cso_set_vertex_elements(ctx
->cso
, 2, ctx
->velem
);
447 ctx
->sampler
.min_img_filter
= filter
;
448 ctx
->sampler
.mag_img_filter
= filter
;
449 cso_single_sampler(ctx
->cso
, 0, &ctx
->sampler
);
450 cso_single_sampler_done(ctx
->cso
);
453 ctx
->viewport
.scale
[0] = 0.5f
* dst
->width
;
454 ctx
->viewport
.scale
[1] = 0.5f
* dst
->height
;
455 ctx
->viewport
.scale
[2] = 0.5f
;
456 ctx
->viewport
.scale
[3] = 1.0f
;
457 ctx
->viewport
.translate
[0] = 0.5f
* dst
->width
;
458 ctx
->viewport
.translate
[1] = 0.5f
* dst
->height
;
459 ctx
->viewport
.translate
[2] = 0.5f
;
460 ctx
->viewport
.translate
[3] = 0.0f
;
461 cso_set_viewport(ctx
->cso
, &ctx
->viewport
);
464 cso_set_fragment_sampler_views(ctx
->cso
, 1, &sampler_view
);
466 if (ctx
->fs
[writemask
] == NULL
)
468 util_make_fragment_tex_shader_writemask(pipe
, TGSI_TEXTURE_2D
,
472 cso_set_fragment_shader_handle(ctx
->cso
, ctx
->fs
[writemask
]);
473 cso_set_vertex_shader_handle(ctx
->cso
, ctx
->vs
);
476 memset(&fb
, 0, sizeof(fb
));
477 fb
.width
= dst
->width
;
478 fb
.height
= dst
->height
;
481 cso_set_framebuffer(ctx
->cso
, &fb
);
484 offset
= setup_vertex_data_tex(ctx
,
485 (float) dstX0
/ dst
->width
* 2.0f
- 1.0f
,
486 (float) dstY0
/ dst
->height
* 2.0f
- 1.0f
,
487 (float) dstX1
/ dst
->width
* 2.0f
- 1.0f
,
488 (float) dstY1
/ dst
->height
* 2.0f
- 1.0f
,
493 util_draw_vertex_buffer(ctx
->pipe
, ctx
->vbuf
, offset
,
494 PIPE_PRIM_TRIANGLE_FAN
,
496 2); /* attribs/vert */
498 /* restore state we changed */
499 cso_restore_blend(ctx
->cso
);
500 cso_restore_depth_stencil_alpha(ctx
->cso
);
501 cso_restore_rasterizer(ctx
->cso
);
502 cso_restore_samplers(ctx
->cso
);
503 cso_restore_fragment_sampler_views(ctx
->cso
);
504 cso_restore_viewport(ctx
->cso
);
505 cso_restore_framebuffer(ctx
->cso
);
506 cso_restore_fragment_shader(ctx
->cso
);
507 cso_restore_vertex_shader(ctx
->cso
);
508 cso_restore_clip(ctx
->cso
);
509 cso_restore_vertex_elements(ctx
->cso
);
511 pipe_sampler_view_reference(&sampler_view
, NULL
);
516 util_blit_pixels(struct blit_state
*ctx
,
517 struct pipe_surface
*src
,
518 struct pipe_sampler_view
*src_sampler_view
,
519 int srcX0
, int srcY0
,
520 int srcX1
, int srcY1
,
521 struct pipe_surface
*dst
,
522 int dstX0
, int dstY0
,
523 int dstX1
, int dstY1
,
524 float z
, uint filter
)
526 util_blit_pixels_writemask( ctx
, src
, src_sampler_view
,
533 TGSI_WRITEMASK_XYZW
);
537 /* Release vertex buffer at end of frame to avoid synchronous
540 void util_blit_flush( struct blit_state
*ctx
)
542 pipe_resource_reference(&ctx
->vbuf
, NULL
);
549 * Copy pixel block from src texture to dst surface.
550 * Overlapping regions are acceptable.
552 * XXX Should support selection of level.
553 * XXX need some control over blitting Z and/or stencil.
556 util_blit_pixels_tex(struct blit_state
*ctx
,
557 struct pipe_sampler_view
*src_sampler_view
,
558 int srcX0
, int srcY0
,
559 int srcX1
, int srcY1
,
560 struct pipe_surface
*dst
,
561 int dstX0
, int dstY0
,
562 int dstX1
, int dstY1
,
563 float z
, uint filter
)
565 struct pipe_framebuffer_state fb
;
566 float s0
, t0
, s1
, t1
;
568 struct pipe_resource
*tex
= src_sampler_view
->texture
;
570 assert(filter
== PIPE_TEX_MIPFILTER_NEAREST
||
571 filter
== PIPE_TEX_MIPFILTER_LINEAR
);
574 assert(tex
->width0
!= 0);
575 assert(tex
->height0
!= 0);
577 s0
= srcX0
/ (float)tex
->width0
;
578 s1
= srcX1
/ (float)tex
->width0
;
579 t0
= srcY0
/ (float)tex
->height0
;
580 t1
= srcY1
/ (float)tex
->height0
;
582 assert(ctx
->pipe
->screen
->is_format_supported(ctx
->pipe
->screen
, dst
->format
,
584 PIPE_BIND_RENDER_TARGET
,
587 /* save state (restored below) */
588 cso_save_blend(ctx
->cso
);
589 cso_save_depth_stencil_alpha(ctx
->cso
);
590 cso_save_rasterizer(ctx
->cso
);
591 cso_save_samplers(ctx
->cso
);
592 cso_save_fragment_sampler_views(ctx
->cso
);
593 cso_save_framebuffer(ctx
->cso
);
594 cso_save_fragment_shader(ctx
->cso
);
595 cso_save_vertex_shader(ctx
->cso
);
596 cso_save_clip(ctx
->cso
);
597 cso_save_vertex_elements(ctx
->cso
);
599 /* set misc state we care about */
600 cso_set_blend(ctx
->cso
, &ctx
->blend
);
601 cso_set_depth_stencil_alpha(ctx
->cso
, &ctx
->depthstencil
);
602 cso_set_rasterizer(ctx
->cso
, &ctx
->rasterizer
);
603 cso_set_clip(ctx
->cso
, &ctx
->clip
);
604 cso_set_vertex_elements(ctx
->cso
, 2, ctx
->velem
);
607 ctx
->sampler
.min_img_filter
= filter
;
608 ctx
->sampler
.mag_img_filter
= filter
;
609 cso_single_sampler(ctx
->cso
, 0, &ctx
->sampler
);
610 cso_single_sampler_done(ctx
->cso
);
613 ctx
->viewport
.scale
[0] = 0.5f
* dst
->width
;
614 ctx
->viewport
.scale
[1] = 0.5f
* dst
->height
;
615 ctx
->viewport
.scale
[2] = 0.5f
;
616 ctx
->viewport
.scale
[3] = 1.0f
;
617 ctx
->viewport
.translate
[0] = 0.5f
* dst
->width
;
618 ctx
->viewport
.translate
[1] = 0.5f
* dst
->height
;
619 ctx
->viewport
.translate
[2] = 0.5f
;
620 ctx
->viewport
.translate
[3] = 0.0f
;
621 cso_set_viewport(ctx
->cso
, &ctx
->viewport
);
624 cso_set_fragment_sampler_views(ctx
->cso
, 1, &src_sampler_view
);
627 cso_set_fragment_shader_handle(ctx
->cso
, ctx
->fs
[TGSI_WRITEMASK_XYZW
]);
628 cso_set_vertex_shader_handle(ctx
->cso
, ctx
->vs
);
631 memset(&fb
, 0, sizeof(fb
));
632 fb
.width
= dst
->width
;
633 fb
.height
= dst
->height
;
636 cso_set_framebuffer(ctx
->cso
, &fb
);
639 offset
= setup_vertex_data_tex(ctx
,
640 (float) dstX0
/ dst
->width
* 2.0f
- 1.0f
,
641 (float) dstY0
/ dst
->height
* 2.0f
- 1.0f
,
642 (float) dstX1
/ dst
->width
* 2.0f
- 1.0f
,
643 (float) dstY1
/ dst
->height
* 2.0f
- 1.0f
,
647 util_draw_vertex_buffer(ctx
->pipe
,
649 PIPE_PRIM_TRIANGLE_FAN
,
651 2); /* attribs/vert */
653 /* restore state we changed */
654 cso_restore_blend(ctx
->cso
);
655 cso_restore_depth_stencil_alpha(ctx
->cso
);
656 cso_restore_rasterizer(ctx
->cso
);
657 cso_restore_samplers(ctx
->cso
);
658 cso_restore_fragment_sampler_views(ctx
->cso
);
659 cso_restore_framebuffer(ctx
->cso
);
660 cso_restore_fragment_shader(ctx
->cso
);
661 cso_restore_vertex_shader(ctx
->cso
);
662 cso_restore_clip(ctx
->cso
);
663 cso_restore_vertex_elements(ctx
->cso
);