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"
49 #include "util/u_rect.h"
51 #include "cso_cache/cso_context.h"
56 struct pipe_context
*pipe
;
57 struct cso_context
*cso
;
59 struct pipe_blend_state blend
;
60 struct pipe_depth_stencil_alpha_state depthstencil
;
61 struct pipe_rasterizer_state rasterizer
;
62 struct pipe_sampler_state sampler
;
63 struct pipe_viewport_state viewport
;
66 void *fs
[TGSI_WRITEMASK_XYZW
+ 1];
68 struct pipe_buffer
*vbuf
; /**< quad vertices */
71 float vertices
[4][2][4]; /**< vertex/texcoords for quad */
76 * Create state object for blit.
77 * Intended to be created once and re-used for many blit() calls.
80 util_create_blit(struct pipe_context
*pipe
, struct cso_context
*cso
)
82 struct blit_state
*ctx
;
85 ctx
= CALLOC_STRUCT(blit_state
);
92 /* disabled blending/masking */
93 memset(&ctx
->blend
, 0, sizeof(ctx
->blend
));
94 ctx
->blend
.colormask
= PIPE_MASK_RGBA
;
96 /* no-op depth/stencil/alpha */
97 memset(&ctx
->depthstencil
, 0, sizeof(ctx
->depthstencil
));
100 memset(&ctx
->rasterizer
, 0, sizeof(ctx
->rasterizer
));
101 ctx
->rasterizer
.front_winding
= PIPE_WINDING_CW
;
102 ctx
->rasterizer
.cull_mode
= PIPE_WINDING_NONE
;
103 ctx
->rasterizer
.bypass_vs_clip_and_viewport
= 1;
104 ctx
->rasterizer
.gl_rasterization_rules
= 1;
107 memset(&ctx
->sampler
, 0, sizeof(ctx
->sampler
));
108 ctx
->sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
109 ctx
->sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
110 ctx
->sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
111 ctx
->sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
112 ctx
->sampler
.min_img_filter
= 0; /* set later */
113 ctx
->sampler
.mag_img_filter
= 0; /* set later */
114 ctx
->sampler
.normalized_coords
= 1;
117 /* vertex shader - still required to provide the linkage between
118 * fragment shader input semantics and vertex_element/buffers.
121 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
122 TGSI_SEMANTIC_GENERIC
};
123 const uint semantic_indexes
[] = { 0, 0 };
124 ctx
->vs
= util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
128 /* fragment shader */
129 ctx
->fs
[TGSI_WRITEMASK_XYZW
] = util_make_fragment_tex_shader(pipe
);
132 /* init vertex data that doesn't change */
133 for (i
= 0; i
< 4; i
++) {
134 ctx
->vertices
[i
][0][3] = 1.0f
; /* w */
135 ctx
->vertices
[i
][1][2] = 0.0f
; /* r */
136 ctx
->vertices
[i
][1][3] = 1.0f
; /* q */
144 * Destroy a blit context
147 util_destroy_blit(struct blit_state
*ctx
)
149 struct pipe_context
*pipe
= ctx
->pipe
;
152 pipe
->delete_vs_state(pipe
, ctx
->vs
);
154 for (i
= 0; i
< Elements(ctx
->fs
); i
++)
156 pipe
->delete_fs_state(pipe
, ctx
->fs
[i
]);
158 pipe_buffer_reference(&ctx
->vbuf
, NULL
);
165 * Get offset of next free slot in vertex buffer for quad vertices.
168 get_next_slot( struct blit_state
*ctx
)
170 const unsigned max_slots
= 4096 / sizeof ctx
->vertices
;
172 if (ctx
->vbuf_slot
>= max_slots
)
173 util_blit_flush( ctx
);
176 ctx
->vbuf
= pipe_buffer_create(ctx
->pipe
->screen
,
178 PIPE_BUFFER_USAGE_VERTEX
,
179 max_slots
* sizeof ctx
->vertices
);
182 return ctx
->vbuf_slot
++ * sizeof ctx
->vertices
;
190 * Setup vertex data for the textured quad we'll draw.
194 setup_vertex_data_tex(struct blit_state
*ctx
,
195 float x0
, float y0
, float x1
, float y1
,
196 float s0
, float t0
, float s1
, float t1
,
201 ctx
->vertices
[0][0][0] = x0
;
202 ctx
->vertices
[0][0][1] = y0
;
203 ctx
->vertices
[0][0][2] = z
;
204 ctx
->vertices
[0][1][0] = s0
; /*s*/
205 ctx
->vertices
[0][1][1] = t0
; /*t*/
207 ctx
->vertices
[1][0][0] = x1
;
208 ctx
->vertices
[1][0][1] = y0
;
209 ctx
->vertices
[1][0][2] = z
;
210 ctx
->vertices
[1][1][0] = s1
; /*s*/
211 ctx
->vertices
[1][1][1] = t0
; /*t*/
213 ctx
->vertices
[2][0][0] = x1
;
214 ctx
->vertices
[2][0][1] = y1
;
215 ctx
->vertices
[2][0][2] = z
;
216 ctx
->vertices
[2][1][0] = s1
;
217 ctx
->vertices
[2][1][1] = t1
;
219 ctx
->vertices
[3][0][0] = x0
;
220 ctx
->vertices
[3][0][1] = y1
;
221 ctx
->vertices
[3][0][2] = z
;
222 ctx
->vertices
[3][1][0] = s0
;
223 ctx
->vertices
[3][1][1] = t1
;
225 offset
= get_next_slot( ctx
);
227 pipe_buffer_write(ctx
->pipe
->screen
, ctx
->vbuf
,
228 offset
, sizeof(ctx
->vertices
), ctx
->vertices
);
235 * \return TRUE if two regions overlap, FALSE otherwise
238 regions_overlap(int srcX0
, int srcY0
,
239 int srcX1
, int srcY1
,
240 int dstX0
, int dstY0
,
241 int dstX1
, int dstY1
)
243 if (MAX2(srcX0
, srcX1
) < MIN2(dstX0
, dstX1
))
244 return FALSE
; /* src completely left of dst */
246 if (MAX2(dstX0
, dstX1
) < MIN2(srcX0
, srcX1
))
247 return FALSE
; /* dst completely left of src */
249 if (MAX2(srcY0
, srcY1
) < MIN2(dstY0
, dstY1
))
250 return FALSE
; /* src completely above dst */
252 if (MAX2(dstY0
, dstY1
) < MIN2(srcY0
, srcY1
))
253 return FALSE
; /* dst completely above src */
255 return TRUE
; /* some overlap */
260 * Copy pixel block from src surface to dst surface.
261 * Overlapping regions are acceptable.
262 * Flipping and stretching are supported.
263 * XXX what about clipping???
264 * XXX need some control over blitting Z and/or stencil.
267 util_blit_pixels_writemask(struct blit_state
*ctx
,
268 struct pipe_surface
*src
,
269 int srcX0
, int srcY0
,
270 int srcX1
, int srcY1
,
271 struct pipe_surface
*dst
,
272 int dstX0
, int dstY0
,
273 int dstX1
, int dstY1
,
274 float z
, uint filter
,
277 struct pipe_context
*pipe
= ctx
->pipe
;
278 struct pipe_screen
*screen
= pipe
->screen
;
279 struct pipe_texture
*tex
= NULL
;
280 struct pipe_framebuffer_state fb
;
281 const int srcW
= abs(srcX1
- srcX0
);
282 const int srcH
= abs(srcY1
- srcY0
);
285 float s0
, t0
, s1
, t1
;
287 assert(filter
== PIPE_TEX_MIPFILTER_NEAREST
||
288 filter
== PIPE_TEX_MIPFILTER_LINEAR
);
290 assert(screen
->is_format_supported(screen
, src
->format
, PIPE_TEXTURE_2D
,
291 PIPE_TEXTURE_USAGE_SAMPLER
, 0));
292 assert(screen
->is_format_supported(screen
, dst
->format
, PIPE_TEXTURE_2D
,
293 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0));
295 /* do the regions overlap? */
296 overlap
= util_same_surface(src
, dst
) &&
297 regions_overlap(srcX0
, srcY0
, srcX1
, srcY1
,
298 dstX0
, dstY0
, dstX1
, dstY1
);
301 * Check for simple case: no format conversion, no flipping, no stretching,
303 * Filter mode should not matter since there's no stretching.
305 if (pipe
->surface_copy
&&
306 dst
->format
== src
->format
&&
311 (dstX1
- dstX0
) == (srcX1
- srcX0
) &&
312 (dstY1
- dstY0
) == (srcY1
- srcY0
) &&
314 pipe
->surface_copy(pipe
,
315 dst
, dstX0
, dstY0
, /* dest */
316 src
, srcX0
, srcY0
, /* src */
317 srcW
, srcH
); /* size */
321 assert(screen
->is_format_supported(screen
, dst
->format
, PIPE_TEXTURE_2D
,
322 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0));
324 /* Create a temporary texture when src and dest alias or when src
325 * is anything other than a single-level 2d texture.
327 * This can still be improved upon.
329 if (util_same_surface(src
, dst
) ||
330 src
->texture
->target
!= PIPE_TEXTURE_2D
||
331 src
->texture
->last_level
!= 0)
333 struct pipe_texture texTemp
;
334 struct pipe_surface
*texSurf
;
335 const int srcLeft
= MIN2(srcX0
, srcX1
);
336 const int srcTop
= MIN2(srcY0
, srcY1
);
338 if (srcLeft
!= srcX0
) {
339 /* left-right flip */
345 if (srcTop
!= srcY0
) {
352 /* create temp texture */
353 memset(&texTemp
, 0, sizeof(texTemp
));
354 texTemp
.target
= PIPE_TEXTURE_2D
;
355 texTemp
.format
= src
->format
;
356 texTemp
.last_level
= 0;
357 texTemp
.width
[0] = srcW
;
358 texTemp
.height
[0] = srcH
;
359 texTemp
.depth
[0] = 1;
360 pf_get_block(src
->format
, &texTemp
.block
);
362 tex
= screen
->texture_create(screen
, &texTemp
);
366 texSurf
= screen
->get_tex_surface(screen
, tex
, 0, 0, 0,
367 PIPE_BUFFER_USAGE_GPU_WRITE
);
369 /* load temp texture */
370 if (pipe
->surface_copy
) {
371 pipe
->surface_copy(pipe
,
372 texSurf
, 0, 0, /* dest */
373 src
, srcLeft
, srcTop
, /* src */
374 srcW
, srcH
); /* size */
376 util_surface_copy(pipe
, FALSE
,
377 texSurf
, 0, 0, /* dest */
378 src
, srcLeft
, srcTop
, /* src */
379 srcW
, srcH
); /* size */
382 /* free the surface, update the texture if necessary.
384 pipe_surface_reference(&texSurf
, NULL
);
391 pipe_texture_reference(&tex
, src
->texture
);
392 s0
= srcX0
/ (float)tex
->width
[0];
393 s1
= srcX1
/ (float)tex
->width
[0];
394 t0
= srcY0
/ (float)tex
->height
[0];
395 t1
= srcY1
/ (float)tex
->height
[0];
399 /* save state (restored below) */
400 cso_save_blend(ctx
->cso
);
401 cso_save_depth_stencil_alpha(ctx
->cso
);
402 cso_save_rasterizer(ctx
->cso
);
403 cso_save_samplers(ctx
->cso
);
404 cso_save_sampler_textures(ctx
->cso
);
405 cso_save_framebuffer(ctx
->cso
);
406 cso_save_fragment_shader(ctx
->cso
);
407 cso_save_vertex_shader(ctx
->cso
);
409 /* set misc state we care about */
410 cso_set_blend(ctx
->cso
, &ctx
->blend
);
411 cso_set_depth_stencil_alpha(ctx
->cso
, &ctx
->depthstencil
);
412 cso_set_rasterizer(ctx
->cso
, &ctx
->rasterizer
);
415 ctx
->sampler
.min_img_filter
= filter
;
416 ctx
->sampler
.mag_img_filter
= filter
;
417 cso_single_sampler(ctx
->cso
, 0, &ctx
->sampler
);
418 cso_single_sampler_done(ctx
->cso
);
421 cso_set_sampler_textures(ctx
->cso
, 1, &tex
);
423 if (ctx
->fs
[writemask
] == NULL
)
424 ctx
->fs
[writemask
] = util_make_fragment_tex_shader_writemask(pipe
, writemask
);
427 cso_set_fragment_shader_handle(ctx
->cso
, ctx
->fs
[writemask
]);
428 cso_set_vertex_shader_handle(ctx
->cso
, ctx
->vs
);
431 memset(&fb
, 0, sizeof(fb
));
432 fb
.width
= dst
->width
;
433 fb
.height
= dst
->height
;
436 cso_set_framebuffer(ctx
->cso
, &fb
);
439 offset
= setup_vertex_data_tex(ctx
,
440 (float) dstX0
, (float) dstY0
,
441 (float) dstX1
, (float) dstY1
,
446 util_draw_vertex_buffer(ctx
->pipe
, ctx
->vbuf
, offset
,
447 PIPE_PRIM_TRIANGLE_FAN
,
449 2); /* attribs/vert */
451 /* restore state we changed */
452 cso_restore_blend(ctx
->cso
);
453 cso_restore_depth_stencil_alpha(ctx
->cso
);
454 cso_restore_rasterizer(ctx
->cso
);
455 cso_restore_samplers(ctx
->cso
);
456 cso_restore_sampler_textures(ctx
->cso
);
457 cso_restore_framebuffer(ctx
->cso
);
458 cso_restore_fragment_shader(ctx
->cso
);
459 cso_restore_vertex_shader(ctx
->cso
);
461 pipe_texture_reference(&tex
, NULL
);
466 util_blit_pixels(struct blit_state
*ctx
,
467 struct pipe_surface
*src
,
468 int srcX0
, int srcY0
,
469 int srcX1
, int srcY1
,
470 struct pipe_surface
*dst
,
471 int dstX0
, int dstY0
,
472 int dstX1
, int dstY1
,
473 float z
, uint filter
)
475 util_blit_pixels_writemask( ctx
, src
,
482 TGSI_WRITEMASK_XYZW
);
486 /* Release vertex buffer at end of frame to avoid synchronous
489 void util_blit_flush( struct blit_state
*ctx
)
491 pipe_buffer_reference(&ctx
->vbuf
, NULL
);
498 * Copy pixel block from src texture to dst surface.
499 * Overlapping regions are acceptable.
501 * XXX Should support selection of level.
502 * XXX need some control over blitting Z and/or stencil.
505 util_blit_pixels_tex(struct blit_state
*ctx
,
506 struct pipe_texture
*tex
,
507 int srcX0
, int srcY0
,
508 int srcX1
, int srcY1
,
509 struct pipe_surface
*dst
,
510 int dstX0
, int dstY0
,
511 int dstX1
, int dstY1
,
512 float z
, uint filter
)
514 struct pipe_framebuffer_state fb
;
515 float s0
, t0
, s1
, t1
;
518 assert(filter
== PIPE_TEX_MIPFILTER_NEAREST
||
519 filter
== PIPE_TEX_MIPFILTER_LINEAR
);
521 assert(tex
->width
[0] != 0);
522 assert(tex
->height
[0] != 0);
524 s0
= srcX0
/ (float)tex
->width
[0];
525 s1
= srcX1
/ (float)tex
->width
[0];
526 t0
= srcY0
/ (float)tex
->height
[0];
527 t1
= srcY1
/ (float)tex
->height
[0];
529 assert(ctx
->pipe
->screen
->is_format_supported(ctx
->pipe
->screen
, dst
->format
,
531 PIPE_TEXTURE_USAGE_RENDER_TARGET
,
534 /* save state (restored below) */
535 cso_save_blend(ctx
->cso
);
536 cso_save_depth_stencil_alpha(ctx
->cso
);
537 cso_save_rasterizer(ctx
->cso
);
538 cso_save_samplers(ctx
->cso
);
539 cso_save_sampler_textures(ctx
->cso
);
540 cso_save_framebuffer(ctx
->cso
);
541 cso_save_fragment_shader(ctx
->cso
);
542 cso_save_vertex_shader(ctx
->cso
);
544 /* set misc state we care about */
545 cso_set_blend(ctx
->cso
, &ctx
->blend
);
546 cso_set_depth_stencil_alpha(ctx
->cso
, &ctx
->depthstencil
);
547 cso_set_rasterizer(ctx
->cso
, &ctx
->rasterizer
);
550 ctx
->sampler
.min_img_filter
= filter
;
551 ctx
->sampler
.mag_img_filter
= filter
;
552 cso_single_sampler(ctx
->cso
, 0, &ctx
->sampler
);
553 cso_single_sampler_done(ctx
->cso
);
556 cso_set_sampler_textures(ctx
->cso
, 1, &tex
);
559 cso_set_fragment_shader_handle(ctx
->cso
, ctx
->fs
[TGSI_WRITEMASK_XYZW
]);
560 cso_set_vertex_shader_handle(ctx
->cso
, ctx
->vs
);
563 memset(&fb
, 0, sizeof(fb
));
564 fb
.width
= dst
->width
;
565 fb
.height
= dst
->height
;
568 cso_set_framebuffer(ctx
->cso
, &fb
);
571 offset
= setup_vertex_data_tex(ctx
,
572 (float) dstX0
, (float) dstY0
,
573 (float) dstX1
, (float) dstY1
,
577 util_draw_vertex_buffer(ctx
->pipe
,
579 PIPE_PRIM_TRIANGLE_FAN
,
581 2); /* attribs/vert */
583 /* restore state we changed */
584 cso_restore_blend(ctx
->cso
);
585 cso_restore_depth_stencil_alpha(ctx
->cso
);
586 cso_restore_rasterizer(ctx
->cso
);
587 cso_restore_samplers(ctx
->cso
);
588 cso_restore_sampler_textures(ctx
->cso
);
589 cso_restore_framebuffer(ctx
->cso
);
590 cso_restore_fragment_shader(ctx
->cso
);
591 cso_restore_vertex_shader(ctx
->cso
);