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
;
186 * Setup vertex data for the textured quad we'll draw.
190 setup_vertex_data(struct blit_state
*ctx
,
191 float x0
, float y0
, float x1
, float y1
, float z
)
195 ctx
->vertices
[0][0][0] = x0
;
196 ctx
->vertices
[0][0][1] = y0
;
197 ctx
->vertices
[0][0][2] = z
;
198 ctx
->vertices
[0][1][0] = 0.0f
; /*s*/
199 ctx
->vertices
[0][1][1] = 0.0f
; /*t*/
201 ctx
->vertices
[1][0][0] = x1
;
202 ctx
->vertices
[1][0][1] = y0
;
203 ctx
->vertices
[1][0][2] = z
;
204 ctx
->vertices
[1][1][0] = 1.0f
; /*s*/
205 ctx
->vertices
[1][1][1] = 0.0f
; /*t*/
207 ctx
->vertices
[2][0][0] = x1
;
208 ctx
->vertices
[2][0][1] = y1
;
209 ctx
->vertices
[2][0][2] = z
;
210 ctx
->vertices
[2][1][0] = 1.0f
;
211 ctx
->vertices
[2][1][1] = 1.0f
;
213 ctx
->vertices
[3][0][0] = x0
;
214 ctx
->vertices
[3][0][1] = y1
;
215 ctx
->vertices
[3][0][2] = z
;
216 ctx
->vertices
[3][1][0] = 0.0f
;
217 ctx
->vertices
[3][1][1] = 1.0f
;
219 offset
= get_next_slot( ctx
);
221 pipe_buffer_write(ctx
->pipe
->screen
, ctx
->vbuf
,
222 offset
, sizeof(ctx
->vertices
), ctx
->vertices
);
229 * Setup vertex data for the textured quad we'll draw.
233 setup_vertex_data_tex(struct blit_state
*ctx
,
234 float x0
, float y0
, float x1
, float y1
,
235 float s0
, float t0
, float s1
, float t1
,
240 ctx
->vertices
[0][0][0] = x0
;
241 ctx
->vertices
[0][0][1] = y0
;
242 ctx
->vertices
[0][0][2] = z
;
243 ctx
->vertices
[0][1][0] = s0
; /*s*/
244 ctx
->vertices
[0][1][1] = t0
; /*t*/
246 ctx
->vertices
[1][0][0] = x1
;
247 ctx
->vertices
[1][0][1] = y0
;
248 ctx
->vertices
[1][0][2] = z
;
249 ctx
->vertices
[1][1][0] = s1
; /*s*/
250 ctx
->vertices
[1][1][1] = t0
; /*t*/
252 ctx
->vertices
[2][0][0] = x1
;
253 ctx
->vertices
[2][0][1] = y1
;
254 ctx
->vertices
[2][0][2] = z
;
255 ctx
->vertices
[2][1][0] = s1
;
256 ctx
->vertices
[2][1][1] = t1
;
258 ctx
->vertices
[3][0][0] = x0
;
259 ctx
->vertices
[3][0][1] = y1
;
260 ctx
->vertices
[3][0][2] = z
;
261 ctx
->vertices
[3][1][0] = s0
;
262 ctx
->vertices
[3][1][1] = t1
;
264 offset
= get_next_slot( ctx
);
266 pipe_buffer_write(ctx
->pipe
->screen
, ctx
->vbuf
,
267 offset
, sizeof(ctx
->vertices
), ctx
->vertices
);
274 * \return TRUE if two regions overlap, FALSE otherwise
277 regions_overlap(int srcX0
, int srcY0
,
278 int srcX1
, int srcY1
,
279 int dstX0
, int dstY0
,
280 int dstX1
, int dstY1
)
282 if (MAX2(srcX0
, srcX1
) < MIN2(dstX0
, dstX1
))
283 return FALSE
; /* src completely left of dst */
285 if (MAX2(dstX0
, dstX1
) < MIN2(srcX0
, srcX1
))
286 return FALSE
; /* dst completely left of src */
288 if (MAX2(srcY0
, srcY1
) < MIN2(dstY0
, dstY1
))
289 return FALSE
; /* src completely above dst */
291 if (MAX2(dstY0
, dstY1
) < MIN2(srcY0
, srcY1
))
292 return FALSE
; /* dst completely above src */
294 return TRUE
; /* some overlap */
299 * Copy pixel block from src surface to dst surface.
300 * Overlapping regions are acceptable.
301 * Flipping and stretching are supported.
302 * XXX what about clipping???
303 * XXX need some control over blitting Z and/or stencil.
306 util_blit_pixels_writemask(struct blit_state
*ctx
,
307 struct pipe_surface
*src
,
308 int srcX0
, int srcY0
,
309 int srcX1
, int srcY1
,
310 struct pipe_surface
*dst
,
311 int dstX0
, int dstY0
,
312 int dstX1
, int dstY1
,
313 float z
, uint filter
,
316 struct pipe_context
*pipe
= ctx
->pipe
;
317 struct pipe_screen
*screen
= pipe
->screen
;
318 struct pipe_texture texTemp
, *tex
;
319 struct pipe_surface
*texSurf
;
320 struct pipe_framebuffer_state fb
;
321 const int srcW
= abs(srcX1
- srcX0
);
322 const int srcH
= abs(srcY1
- srcY0
);
323 const int srcLeft
= MIN2(srcX0
, srcX1
);
324 const int srcTop
= MIN2(srcY0
, srcY1
);
328 assert(filter
== PIPE_TEX_MIPFILTER_NEAREST
||
329 filter
== PIPE_TEX_MIPFILTER_LINEAR
);
331 assert(screen
->is_format_supported(screen
, src
->format
, PIPE_TEXTURE_2D
,
332 PIPE_TEXTURE_USAGE_SAMPLER
, 0));
333 assert(screen
->is_format_supported(screen
, dst
->format
, PIPE_TEXTURE_2D
,
334 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0));
336 /* do the regions overlap? */
337 overlap
= util_same_surface(src
, dst
) &&
338 regions_overlap(srcX0
, srcY0
, srcX1
, srcY1
,
339 dstX0
, dstY0
, dstX1
, dstY1
);
342 * Check for simple case: no format conversion, no flipping, no stretching,
344 * Filter mode should not matter since there's no stretching.
346 if (dst
->format
== src
->format
&&
351 (dstX1
- dstX0
) == (srcX1
- srcX0
) &&
352 (dstY1
- dstY0
) == (srcY1
- srcY0
) &&
354 pipe
->surface_copy(pipe
,
355 dst
, dstX0
, dstY0
, /* dest */
356 src
, srcX0
, srcY0
, /* src */
357 srcW
, srcH
); /* size */
361 if (srcLeft
!= srcX0
) {
362 /* left-right flip */
368 if (srcTop
!= srcY0
) {
375 assert(screen
->is_format_supported(screen
, dst
->format
, PIPE_TEXTURE_2D
,
376 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0));
379 * XXX for now we're always creating a temporary texture.
380 * Strictly speaking that's not always needed.
383 /* create temp texture */
384 memset(&texTemp
, 0, sizeof(texTemp
));
385 texTemp
.target
= PIPE_TEXTURE_2D
;
386 texTemp
.format
= src
->format
;
387 texTemp
.last_level
= 0;
388 texTemp
.width
[0] = srcW
;
389 texTemp
.height
[0] = srcH
;
390 texTemp
.depth
[0] = 1;
391 pf_get_block(src
->format
, &texTemp
.block
);
393 tex
= screen
->texture_create(screen
, &texTemp
);
397 texSurf
= screen
->get_tex_surface(screen
, tex
, 0, 0, 0,
398 PIPE_BUFFER_USAGE_GPU_WRITE
);
400 /* load temp texture */
401 pipe
->surface_copy(pipe
,
402 texSurf
, 0, 0, /* dest */
403 src
, srcLeft
, srcTop
, /* src */
404 srcW
, srcH
); /* size */
406 /* free the surface, update the texture if necessary.
408 pipe_surface_reference(&texSurf
, NULL
);
410 /* save state (restored below) */
411 cso_save_blend(ctx
->cso
);
412 cso_save_depth_stencil_alpha(ctx
->cso
);
413 cso_save_rasterizer(ctx
->cso
);
414 cso_save_samplers(ctx
->cso
);
415 cso_save_sampler_textures(ctx
->cso
);
416 cso_save_framebuffer(ctx
->cso
);
417 cso_save_fragment_shader(ctx
->cso
);
418 cso_save_vertex_shader(ctx
->cso
);
420 /* set misc state we care about */
421 cso_set_blend(ctx
->cso
, &ctx
->blend
);
422 cso_set_depth_stencil_alpha(ctx
->cso
, &ctx
->depthstencil
);
423 cso_set_rasterizer(ctx
->cso
, &ctx
->rasterizer
);
426 ctx
->sampler
.min_img_filter
= filter
;
427 ctx
->sampler
.mag_img_filter
= filter
;
428 cso_single_sampler(ctx
->cso
, 0, &ctx
->sampler
);
429 cso_single_sampler_done(ctx
->cso
);
432 cso_set_sampler_textures(ctx
->cso
, 1, &tex
);
434 if (ctx
->fs
[writemask
] == NULL
)
435 ctx
->fs
[writemask
] = util_make_fragment_tex_shader_writemask(pipe
, writemask
);
438 cso_set_fragment_shader_handle(ctx
->cso
, ctx
->fs
[writemask
]);
439 cso_set_vertex_shader_handle(ctx
->cso
, ctx
->vs
);
442 memset(&fb
, 0, sizeof(fb
));
443 fb
.width
= dst
->width
;
444 fb
.height
= dst
->height
;
447 cso_set_framebuffer(ctx
->cso
, &fb
);
450 offset
= setup_vertex_data(ctx
,
451 (float) dstX0
, (float) dstY0
,
452 (float) dstX1
, (float) dstY1
, z
);
454 util_draw_vertex_buffer(ctx
->pipe
, ctx
->vbuf
, offset
,
455 PIPE_PRIM_TRIANGLE_FAN
,
457 2); /* attribs/vert */
459 /* restore state we changed */
460 cso_restore_blend(ctx
->cso
);
461 cso_restore_depth_stencil_alpha(ctx
->cso
);
462 cso_restore_rasterizer(ctx
->cso
);
463 cso_restore_samplers(ctx
->cso
);
464 cso_restore_sampler_textures(ctx
->cso
);
465 cso_restore_framebuffer(ctx
->cso
);
466 cso_restore_fragment_shader(ctx
->cso
);
467 cso_restore_vertex_shader(ctx
->cso
);
469 pipe_texture_reference(&tex
, NULL
);
474 util_blit_pixels(struct blit_state
*ctx
,
475 struct pipe_surface
*src
,
476 int srcX0
, int srcY0
,
477 int srcX1
, int srcY1
,
478 struct pipe_surface
*dst
,
479 int dstX0
, int dstY0
,
480 int dstX1
, int dstY1
,
481 float z
, uint filter
)
483 util_blit_pixels_writemask( ctx
, src
,
490 TGSI_WRITEMASK_XYZW
);
494 /* Release vertex buffer at end of frame to avoid synchronous
497 void util_blit_flush( struct blit_state
*ctx
)
499 pipe_buffer_reference(&ctx
->vbuf
, NULL
);
506 * Copy pixel block from src texture to dst surface.
507 * Overlapping regions are acceptable.
509 * XXX Should support selection of level.
510 * XXX need some control over blitting Z and/or stencil.
513 util_blit_pixels_tex(struct blit_state
*ctx
,
514 struct pipe_texture
*tex
,
515 int srcX0
, int srcY0
,
516 int srcX1
, int srcY1
,
517 struct pipe_surface
*dst
,
518 int dstX0
, int dstY0
,
519 int dstX1
, int dstY1
,
520 float z
, uint filter
)
522 struct pipe_framebuffer_state fb
;
523 float s0
, t0
, s1
, t1
;
526 assert(filter
== PIPE_TEX_MIPFILTER_NEAREST
||
527 filter
== PIPE_TEX_MIPFILTER_LINEAR
);
529 assert(tex
->width
[0] != 0);
530 assert(tex
->height
[0] != 0);
532 s0
= srcX0
/ (float)tex
->width
[0];
533 s1
= srcX1
/ (float)tex
->width
[0];
534 t0
= srcY0
/ (float)tex
->height
[0];
535 t1
= srcY1
/ (float)tex
->height
[0];
537 assert(ctx
->pipe
->screen
->is_format_supported(ctx
->pipe
->screen
, dst
->format
,
539 PIPE_TEXTURE_USAGE_RENDER_TARGET
,
542 /* save state (restored below) */
543 cso_save_blend(ctx
->cso
);
544 cso_save_depth_stencil_alpha(ctx
->cso
);
545 cso_save_rasterizer(ctx
->cso
);
546 cso_save_samplers(ctx
->cso
);
547 cso_save_sampler_textures(ctx
->cso
);
548 cso_save_framebuffer(ctx
->cso
);
549 cso_save_fragment_shader(ctx
->cso
);
550 cso_save_vertex_shader(ctx
->cso
);
552 /* set misc state we care about */
553 cso_set_blend(ctx
->cso
, &ctx
->blend
);
554 cso_set_depth_stencil_alpha(ctx
->cso
, &ctx
->depthstencil
);
555 cso_set_rasterizer(ctx
->cso
, &ctx
->rasterizer
);
558 ctx
->sampler
.min_img_filter
= filter
;
559 ctx
->sampler
.mag_img_filter
= filter
;
560 cso_single_sampler(ctx
->cso
, 0, &ctx
->sampler
);
561 cso_single_sampler_done(ctx
->cso
);
564 cso_set_sampler_textures(ctx
->cso
, 1, &tex
);
567 cso_set_fragment_shader_handle(ctx
->cso
, ctx
->fs
[TGSI_WRITEMASK_XYZW
]);
568 cso_set_vertex_shader_handle(ctx
->cso
, ctx
->vs
);
571 memset(&fb
, 0, sizeof(fb
));
572 fb
.width
= dst
->width
;
573 fb
.height
= dst
->height
;
576 cso_set_framebuffer(ctx
->cso
, &fb
);
579 offset
= setup_vertex_data_tex(ctx
,
580 (float) dstX0
, (float) dstY0
,
581 (float) dstX1
, (float) dstY1
,
585 util_draw_vertex_buffer(ctx
->pipe
,
587 PIPE_PRIM_TRIANGLE_FAN
,
589 2); /* attribs/vert */
591 /* restore state we changed */
592 cso_restore_blend(ctx
->cso
);
593 cso_restore_depth_stencil_alpha(ctx
->cso
);
594 cso_restore_rasterizer(ctx
->cso
);
595 cso_restore_samplers(ctx
->cso
);
596 cso_restore_sampler_textures(ctx
->cso
);
597 cso_restore_framebuffer(ctx
->cso
);
598 cso_restore_fragment_shader(ctx
->cso
);
599 cso_restore_vertex_shader(ctx
->cso
);