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 "pipe/p_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_math.h"
46 #include "util/u_memory.h"
47 #include "util/u_simple_shaders.h"
48 #include "util/u_surface.h"
50 #include "cso_cache/cso_context.h"
55 struct pipe_context
*pipe
;
56 struct cso_context
*cso
;
58 struct pipe_blend_state blend
;
59 struct pipe_depth_stencil_alpha_state depthstencil
;
60 struct pipe_rasterizer_state rasterizer
;
61 struct pipe_sampler_state sampler
;
62 struct pipe_viewport_state viewport
;
65 void *fs
[TGSI_WRITEMASK_XYZW
+ 1];
67 struct pipe_buffer
*vbuf
; /**< quad vertices */
70 float vertices
[4][2][4]; /**< vertex/texcoords for quad */
75 * Create state object for blit.
76 * Intended to be created once and re-used for many blit() calls.
79 util_create_blit(struct pipe_context
*pipe
, struct cso_context
*cso
)
81 struct blit_state
*ctx
;
84 ctx
= CALLOC_STRUCT(blit_state
);
91 /* disabled blending/masking */
92 memset(&ctx
->blend
, 0, sizeof(ctx
->blend
));
93 ctx
->blend
.colormask
= PIPE_MASK_RGBA
;
95 /* no-op depth/stencil/alpha */
96 memset(&ctx
->depthstencil
, 0, sizeof(ctx
->depthstencil
));
99 memset(&ctx
->rasterizer
, 0, sizeof(ctx
->rasterizer
));
100 ctx
->rasterizer
.front_winding
= PIPE_WINDING_CW
;
101 ctx
->rasterizer
.cull_mode
= PIPE_WINDING_NONE
;
102 ctx
->rasterizer
.bypass_vs_clip_and_viewport
= 1;
103 ctx
->rasterizer
.gl_rasterization_rules
= 1;
106 memset(&ctx
->sampler
, 0, sizeof(ctx
->sampler
));
107 ctx
->sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
108 ctx
->sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
109 ctx
->sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
110 ctx
->sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
111 ctx
->sampler
.min_img_filter
= 0; /* set later */
112 ctx
->sampler
.mag_img_filter
= 0; /* set later */
113 ctx
->sampler
.normalized_coords
= 1;
116 /* vertex shader - still required to provide the linkage between
117 * fragment shader input semantics and vertex_element/buffers.
120 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
121 TGSI_SEMANTIC_GENERIC
};
122 const uint semantic_indexes
[] = { 0, 0 };
123 ctx
->vs
= util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
127 /* fragment shader */
128 ctx
->fs
[TGSI_WRITEMASK_XYZW
] = util_make_fragment_tex_shader(pipe
);
131 /* init vertex data that doesn't change */
132 for (i
= 0; i
< 4; i
++) {
133 ctx
->vertices
[i
][0][3] = 1.0f
; /* w */
134 ctx
->vertices
[i
][1][2] = 0.0f
; /* r */
135 ctx
->vertices
[i
][1][3] = 1.0f
; /* q */
143 * Destroy a blit context
146 util_destroy_blit(struct blit_state
*ctx
)
148 struct pipe_context
*pipe
= ctx
->pipe
;
151 pipe
->delete_vs_state(pipe
, ctx
->vs
);
153 for (i
= 0; i
< Elements(ctx
->fs
); i
++)
155 pipe
->delete_fs_state(pipe
, ctx
->fs
[i
]);
157 pipe_buffer_reference(&ctx
->vbuf
, NULL
);
164 * Get offset of next free slot in vertex buffer for quad vertices.
167 get_next_slot( struct blit_state
*ctx
)
169 const unsigned max_slots
= 4096 / sizeof ctx
->vertices
;
171 if (ctx
->vbuf_slot
>= max_slots
)
172 util_blit_flush( ctx
);
175 ctx
->vbuf
= pipe_buffer_create(ctx
->pipe
->screen
,
177 PIPE_BUFFER_USAGE_VERTEX
,
178 max_slots
* sizeof ctx
->vertices
);
181 return ctx
->vbuf_slot
++ * sizeof ctx
->vertices
;
189 * Setup vertex data for the textured quad we'll draw.
193 setup_vertex_data_tex(struct blit_state
*ctx
,
194 float x0
, float y0
, float x1
, float y1
,
195 float s0
, float t0
, float s1
, float t1
,
200 ctx
->vertices
[0][0][0] = x0
;
201 ctx
->vertices
[0][0][1] = y0
;
202 ctx
->vertices
[0][0][2] = z
;
203 ctx
->vertices
[0][1][0] = s0
; /*s*/
204 ctx
->vertices
[0][1][1] = t0
; /*t*/
206 ctx
->vertices
[1][0][0] = x1
;
207 ctx
->vertices
[1][0][1] = y0
;
208 ctx
->vertices
[1][0][2] = z
;
209 ctx
->vertices
[1][1][0] = s1
; /*s*/
210 ctx
->vertices
[1][1][1] = t0
; /*t*/
212 ctx
->vertices
[2][0][0] = x1
;
213 ctx
->vertices
[2][0][1] = y1
;
214 ctx
->vertices
[2][0][2] = z
;
215 ctx
->vertices
[2][1][0] = s1
;
216 ctx
->vertices
[2][1][1] = t1
;
218 ctx
->vertices
[3][0][0] = x0
;
219 ctx
->vertices
[3][0][1] = y1
;
220 ctx
->vertices
[3][0][2] = z
;
221 ctx
->vertices
[3][1][0] = s0
;
222 ctx
->vertices
[3][1][1] = t1
;
224 offset
= get_next_slot( ctx
);
226 pipe_buffer_write(ctx
->pipe
->screen
, ctx
->vbuf
,
227 offset
, sizeof(ctx
->vertices
), ctx
->vertices
);
234 * \return TRUE if two regions overlap, FALSE otherwise
237 regions_overlap(int srcX0
, int srcY0
,
238 int srcX1
, int srcY1
,
239 int dstX0
, int dstY0
,
240 int dstX1
, int dstY1
)
242 if (MAX2(srcX0
, srcX1
) < MIN2(dstX0
, dstX1
))
243 return FALSE
; /* src completely left of dst */
245 if (MAX2(dstX0
, dstX1
) < MIN2(srcX0
, srcX1
))
246 return FALSE
; /* dst completely left of src */
248 if (MAX2(srcY0
, srcY1
) < MIN2(dstY0
, dstY1
))
249 return FALSE
; /* src completely above dst */
251 if (MAX2(dstY0
, dstY1
) < MIN2(srcY0
, srcY1
))
252 return FALSE
; /* dst completely above src */
254 return TRUE
; /* some overlap */
259 * Copy pixel block from src surface to dst surface.
260 * Overlapping regions are acceptable.
261 * Flipping and stretching are supported.
262 * XXX what about clipping???
263 * XXX need some control over blitting Z and/or stencil.
266 util_blit_pixels_writemask(struct blit_state
*ctx
,
267 struct pipe_surface
*src
,
268 int srcX0
, int srcY0
,
269 int srcX1
, int srcY1
,
270 struct pipe_surface
*dst
,
271 int dstX0
, int dstY0
,
272 int dstX1
, int dstY1
,
273 float z
, uint filter
,
276 struct pipe_context
*pipe
= ctx
->pipe
;
277 struct pipe_screen
*screen
= pipe
->screen
;
278 struct pipe_texture
*tex
= NULL
;
279 struct pipe_framebuffer_state fb
;
280 const int srcW
= abs(srcX1
- srcX0
);
281 const int srcH
= abs(srcY1
- srcY0
);
284 float s0
, t0
, s1
, t1
;
286 assert(filter
== PIPE_TEX_MIPFILTER_NEAREST
||
287 filter
== PIPE_TEX_MIPFILTER_LINEAR
);
289 assert(screen
->is_format_supported(screen
, src
->format
, PIPE_TEXTURE_2D
,
290 PIPE_TEXTURE_USAGE_SAMPLER
, 0));
291 assert(screen
->is_format_supported(screen
, dst
->format
, PIPE_TEXTURE_2D
,
292 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0));
294 /* do the regions overlap? */
295 overlap
= util_same_surface(src
, dst
) &&
296 regions_overlap(srcX0
, srcY0
, srcX1
, srcY1
,
297 dstX0
, dstY0
, dstX1
, dstY1
);
300 * Check for simple case: no format conversion, no flipping, no stretching,
302 * Filter mode should not matter since there's no stretching.
304 if (dst
->format
== src
->format
&&
309 (dstX1
- dstX0
) == (srcX1
- srcX0
) &&
310 (dstY1
- dstY0
) == (srcY1
- srcY0
) &&
312 pipe
->surface_copy(pipe
,
313 dst
, dstX0
, dstY0
, /* dest */
314 src
, srcX0
, srcY0
, /* src */
315 srcW
, srcH
); /* size */
319 assert(screen
->is_format_supported(screen
, dst
->format
, PIPE_TEXTURE_2D
,
320 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0));
322 /* Create a temporary texture when src and dest alias or when src
323 * is anything other than a single-level 2d texture.
325 * This can still be improved upon.
327 if (util_same_surface(src
, dst
) ||
328 src
->texture
->target
!= PIPE_TEXTURE_2D
||
329 src
->texture
->last_level
!= 0)
331 struct pipe_texture texTemp
;
332 struct pipe_surface
*texSurf
;
333 const int srcLeft
= MIN2(srcX0
, srcX1
);
334 const int srcTop
= MIN2(srcY0
, srcY1
);
336 if (srcLeft
!= srcX0
) {
337 /* left-right flip */
343 if (srcTop
!= srcY0
) {
350 /* create temp texture */
351 memset(&texTemp
, 0, sizeof(texTemp
));
352 texTemp
.target
= PIPE_TEXTURE_2D
;
353 texTemp
.format
= src
->format
;
354 texTemp
.last_level
= 0;
355 texTemp
.width
[0] = srcW
;
356 texTemp
.height
[0] = srcH
;
357 texTemp
.depth
[0] = 1;
358 pf_get_block(src
->format
, &texTemp
.block
);
360 tex
= screen
->texture_create(screen
, &texTemp
);
364 texSurf
= screen
->get_tex_surface(screen
, tex
, 0, 0, 0,
365 PIPE_BUFFER_USAGE_GPU_WRITE
);
367 /* load temp texture */
368 pipe
->surface_copy(pipe
,
369 texSurf
, 0, 0, /* dest */
370 src
, srcLeft
, srcTop
, /* src */
371 srcW
, srcH
); /* size */
373 /* free the surface, update the texture if necessary.
375 pipe_surface_reference(&texSurf
, NULL
);
382 pipe_texture_reference(&tex
, src
->texture
);
383 s0
= srcX0
/ (float)tex
->width
[0];
384 s1
= srcX1
/ (float)tex
->width
[0];
385 t0
= srcY0
/ (float)tex
->height
[0];
386 t1
= srcY1
/ (float)tex
->height
[0];
390 /* save state (restored below) */
391 cso_save_blend(ctx
->cso
);
392 cso_save_depth_stencil_alpha(ctx
->cso
);
393 cso_save_rasterizer(ctx
->cso
);
394 cso_save_samplers(ctx
->cso
);
395 cso_save_sampler_textures(ctx
->cso
);
396 cso_save_framebuffer(ctx
->cso
);
397 cso_save_fragment_shader(ctx
->cso
);
398 cso_save_vertex_shader(ctx
->cso
);
400 /* set misc state we care about */
401 cso_set_blend(ctx
->cso
, &ctx
->blend
);
402 cso_set_depth_stencil_alpha(ctx
->cso
, &ctx
->depthstencil
);
403 cso_set_rasterizer(ctx
->cso
, &ctx
->rasterizer
);
406 ctx
->sampler
.min_img_filter
= filter
;
407 ctx
->sampler
.mag_img_filter
= filter
;
408 cso_single_sampler(ctx
->cso
, 0, &ctx
->sampler
);
409 cso_single_sampler_done(ctx
->cso
);
412 cso_set_sampler_textures(ctx
->cso
, 1, &tex
);
414 if (ctx
->fs
[writemask
] == NULL
)
415 ctx
->fs
[writemask
] = util_make_fragment_tex_shader_writemask(pipe
, writemask
);
418 cso_set_fragment_shader_handle(ctx
->cso
, ctx
->fs
[writemask
]);
419 cso_set_vertex_shader_handle(ctx
->cso
, ctx
->vs
);
422 memset(&fb
, 0, sizeof(fb
));
423 fb
.width
= dst
->width
;
424 fb
.height
= dst
->height
;
427 cso_set_framebuffer(ctx
->cso
, &fb
);
430 offset
= setup_vertex_data_tex(ctx
,
431 (float) dstX0
, (float) dstY0
,
432 (float) dstX1
, (float) dstY1
,
437 util_draw_vertex_buffer(ctx
->pipe
, ctx
->vbuf
, offset
,
438 PIPE_PRIM_TRIANGLE_FAN
,
440 2); /* attribs/vert */
442 /* restore state we changed */
443 cso_restore_blend(ctx
->cso
);
444 cso_restore_depth_stencil_alpha(ctx
->cso
);
445 cso_restore_rasterizer(ctx
->cso
);
446 cso_restore_samplers(ctx
->cso
);
447 cso_restore_sampler_textures(ctx
->cso
);
448 cso_restore_framebuffer(ctx
->cso
);
449 cso_restore_fragment_shader(ctx
->cso
);
450 cso_restore_vertex_shader(ctx
->cso
);
452 pipe_texture_reference(&tex
, NULL
);
457 util_blit_pixels(struct blit_state
*ctx
,
458 struct pipe_surface
*src
,
459 int srcX0
, int srcY0
,
460 int srcX1
, int srcY1
,
461 struct pipe_surface
*dst
,
462 int dstX0
, int dstY0
,
463 int dstX1
, int dstY1
,
464 float z
, uint filter
)
466 util_blit_pixels_writemask( ctx
, src
,
473 TGSI_WRITEMASK_XYZW
);
477 /* Release vertex buffer at end of frame to avoid synchronous
480 void util_blit_flush( struct blit_state
*ctx
)
482 pipe_buffer_reference(&ctx
->vbuf
, NULL
);
489 * Copy pixel block from src texture to dst surface.
490 * Overlapping regions are acceptable.
492 * XXX Should support selection of level.
493 * XXX need some control over blitting Z and/or stencil.
496 util_blit_pixels_tex(struct blit_state
*ctx
,
497 struct pipe_texture
*tex
,
498 int srcX0
, int srcY0
,
499 int srcX1
, int srcY1
,
500 struct pipe_surface
*dst
,
501 int dstX0
, int dstY0
,
502 int dstX1
, int dstY1
,
503 float z
, uint filter
)
505 struct pipe_framebuffer_state fb
;
506 float s0
, t0
, s1
, t1
;
509 assert(filter
== PIPE_TEX_MIPFILTER_NEAREST
||
510 filter
== PIPE_TEX_MIPFILTER_LINEAR
);
512 assert(tex
->width
[0] != 0);
513 assert(tex
->height
[0] != 0);
515 s0
= srcX0
/ (float)tex
->width
[0];
516 s1
= srcX1
/ (float)tex
->width
[0];
517 t0
= srcY0
/ (float)tex
->height
[0];
518 t1
= srcY1
/ (float)tex
->height
[0];
520 assert(ctx
->pipe
->screen
->is_format_supported(ctx
->pipe
->screen
, dst
->format
,
522 PIPE_TEXTURE_USAGE_RENDER_TARGET
,
525 /* save state (restored below) */
526 cso_save_blend(ctx
->cso
);
527 cso_save_depth_stencil_alpha(ctx
->cso
);
528 cso_save_rasterizer(ctx
->cso
);
529 cso_save_samplers(ctx
->cso
);
530 cso_save_sampler_textures(ctx
->cso
);
531 cso_save_framebuffer(ctx
->cso
);
532 cso_save_fragment_shader(ctx
->cso
);
533 cso_save_vertex_shader(ctx
->cso
);
535 /* set misc state we care about */
536 cso_set_blend(ctx
->cso
, &ctx
->blend
);
537 cso_set_depth_stencil_alpha(ctx
->cso
, &ctx
->depthstencil
);
538 cso_set_rasterizer(ctx
->cso
, &ctx
->rasterizer
);
541 ctx
->sampler
.min_img_filter
= filter
;
542 ctx
->sampler
.mag_img_filter
= filter
;
543 cso_single_sampler(ctx
->cso
, 0, &ctx
->sampler
);
544 cso_single_sampler_done(ctx
->cso
);
547 cso_set_sampler_textures(ctx
->cso
, 1, &tex
);
550 cso_set_fragment_shader_handle(ctx
->cso
, ctx
->fs
[TGSI_WRITEMASK_XYZW
]);
551 cso_set_vertex_shader_handle(ctx
->cso
, ctx
->vs
);
554 memset(&fb
, 0, sizeof(fb
));
555 fb
.width
= dst
->width
;
556 fb
.height
= dst
->height
;
559 cso_set_framebuffer(ctx
->cso
, &fb
);
562 offset
= setup_vertex_data_tex(ctx
,
563 (float) dstX0
, (float) dstY0
,
564 (float) dstX1
, (float) dstY1
,
568 util_draw_vertex_buffer(ctx
->pipe
,
570 PIPE_PRIM_TRIANGLE_FAN
,
572 2); /* attribs/vert */
574 /* restore state we changed */
575 cso_restore_blend(ctx
->cso
);
576 cso_restore_depth_stencil_alpha(ctx
->cso
);
577 cso_restore_rasterizer(ctx
->cso
);
578 cso_restore_samplers(ctx
->cso
);
579 cso_restore_sampler_textures(ctx
->cso
);
580 cso_restore_framebuffer(ctx
->cso
);
581 cso_restore_fragment_shader(ctx
->cso
);
582 cso_restore_vertex_shader(ctx
->cso
);