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_simple_shaders.h"
49 #include "util/u_surface.h"
50 #include "util/u_rect.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
;
67 void *fs
[TGSI_WRITEMASK_XYZW
+ 1];
69 struct pipe_buffer
*vbuf
; /**< quad vertices */
72 float vertices
[4][2][4]; /**< vertex/texcoords for quad */
77 * Create state object for blit.
78 * Intended to be created once and re-used for many blit() calls.
81 util_create_blit(struct pipe_context
*pipe
, struct cso_context
*cso
)
83 struct blit_state
*ctx
;
86 ctx
= CALLOC_STRUCT(blit_state
);
93 /* disabled blending/masking */
94 memset(&ctx
->blend
, 0, sizeof(ctx
->blend
));
95 ctx
->blend
.rt
[0].colormask
= PIPE_MASK_RGBA
;
97 /* no-op depth/stencil/alpha */
98 memset(&ctx
->depthstencil
, 0, sizeof(ctx
->depthstencil
));
101 memset(&ctx
->rasterizer
, 0, sizeof(ctx
->rasterizer
));
102 ctx
->rasterizer
.front_winding
= PIPE_WINDING_CW
;
103 ctx
->rasterizer
.cull_mode
= PIPE_WINDING_NONE
;
104 ctx
->rasterizer
.bypass_vs_clip_and_viewport
= 1;
105 ctx
->rasterizer
.gl_rasterization_rules
= 1;
108 memset(&ctx
->sampler
, 0, sizeof(ctx
->sampler
));
109 ctx
->sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
110 ctx
->sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
111 ctx
->sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
112 ctx
->sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
113 ctx
->sampler
.min_img_filter
= 0; /* set later */
114 ctx
->sampler
.mag_img_filter
= 0; /* set later */
115 ctx
->sampler
.normalized_coords
= 1;
118 /* vertex shader - still required to provide the linkage between
119 * fragment shader input semantics and vertex_element/buffers.
122 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
123 TGSI_SEMANTIC_GENERIC
};
124 const uint semantic_indexes
[] = { 0, 0 };
125 ctx
->vs
= util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
129 /* fragment shader */
130 ctx
->fs
[TGSI_WRITEMASK_XYZW
] =
131 util_make_fragment_tex_shader(pipe
, TGSI_TEXTURE_2D
);
134 /* init vertex data that doesn't change */
135 for (i
= 0; i
< 4; i
++) {
136 ctx
->vertices
[i
][0][3] = 1.0f
; /* w */
137 ctx
->vertices
[i
][1][2] = 0.0f
; /* r */
138 ctx
->vertices
[i
][1][3] = 1.0f
; /* q */
146 * Destroy a blit context
149 util_destroy_blit(struct blit_state
*ctx
)
151 struct pipe_context
*pipe
= ctx
->pipe
;
154 pipe
->delete_vs_state(pipe
, ctx
->vs
);
156 for (i
= 0; i
< Elements(ctx
->fs
); i
++)
158 pipe
->delete_fs_state(pipe
, ctx
->fs
[i
]);
160 pipe_buffer_reference(&ctx
->vbuf
, NULL
);
167 * Get offset of next free slot in vertex buffer for quad vertices.
170 get_next_slot( struct blit_state
*ctx
)
172 const unsigned max_slots
= 4096 / sizeof ctx
->vertices
;
174 if (ctx
->vbuf_slot
>= max_slots
)
175 util_blit_flush( ctx
);
178 ctx
->vbuf
= pipe_buffer_create(ctx
->pipe
->screen
,
180 PIPE_BUFFER_USAGE_VERTEX
,
181 max_slots
* sizeof ctx
->vertices
);
184 return ctx
->vbuf_slot
++ * sizeof ctx
->vertices
;
192 * Setup vertex data for the textured quad we'll draw.
196 setup_vertex_data_tex(struct blit_state
*ctx
,
197 float x0
, float y0
, float x1
, float y1
,
198 float s0
, float t0
, float s1
, float t1
,
203 ctx
->vertices
[0][0][0] = x0
;
204 ctx
->vertices
[0][0][1] = y0
;
205 ctx
->vertices
[0][0][2] = z
;
206 ctx
->vertices
[0][1][0] = s0
; /*s*/
207 ctx
->vertices
[0][1][1] = t0
; /*t*/
209 ctx
->vertices
[1][0][0] = x1
;
210 ctx
->vertices
[1][0][1] = y0
;
211 ctx
->vertices
[1][0][2] = z
;
212 ctx
->vertices
[1][1][0] = s1
; /*s*/
213 ctx
->vertices
[1][1][1] = t0
; /*t*/
215 ctx
->vertices
[2][0][0] = x1
;
216 ctx
->vertices
[2][0][1] = y1
;
217 ctx
->vertices
[2][0][2] = z
;
218 ctx
->vertices
[2][1][0] = s1
;
219 ctx
->vertices
[2][1][1] = t1
;
221 ctx
->vertices
[3][0][0] = x0
;
222 ctx
->vertices
[3][0][1] = y1
;
223 ctx
->vertices
[3][0][2] = z
;
224 ctx
->vertices
[3][1][0] = s0
;
225 ctx
->vertices
[3][1][1] = t1
;
227 offset
= get_next_slot( ctx
);
229 pipe_buffer_write_nooverlap(ctx
->pipe
->screen
, ctx
->vbuf
,
230 offset
, sizeof(ctx
->vertices
), ctx
->vertices
);
237 * \return TRUE if two regions overlap, FALSE otherwise
240 regions_overlap(int srcX0
, int srcY0
,
241 int srcX1
, int srcY1
,
242 int dstX0
, int dstY0
,
243 int dstX1
, int dstY1
)
245 if (MAX2(srcX0
, srcX1
) < MIN2(dstX0
, dstX1
))
246 return FALSE
; /* src completely left of dst */
248 if (MAX2(dstX0
, dstX1
) < MIN2(srcX0
, srcX1
))
249 return FALSE
; /* dst completely left of src */
251 if (MAX2(srcY0
, srcY1
) < MIN2(dstY0
, dstY1
))
252 return FALSE
; /* src completely above dst */
254 if (MAX2(dstY0
, dstY1
) < MIN2(srcY0
, srcY1
))
255 return FALSE
; /* dst completely above src */
257 return TRUE
; /* some overlap */
262 * Copy pixel block from src surface to dst surface.
263 * Overlapping regions are acceptable.
264 * Flipping and stretching are supported.
265 * \param filter one of PIPE_TEX_MIPFILTER_NEAREST/LINEAR
266 * \param writemask controls which channels in the dest surface are sourced
267 * from the src surface. Disabled channels are sourced
269 * XXX what about clipping???
270 * XXX need some control over blitting Z and/or stencil.
273 util_blit_pixels_writemask(struct blit_state
*ctx
,
274 struct pipe_surface
*src
,
275 int srcX0
, int srcY0
,
276 int srcX1
, int srcY1
,
277 struct pipe_surface
*dst
,
278 int dstX0
, int dstY0
,
279 int dstX1
, int dstY1
,
280 float z
, uint filter
,
283 struct pipe_context
*pipe
= ctx
->pipe
;
284 struct pipe_screen
*screen
= pipe
->screen
;
285 struct pipe_texture
*tex
= NULL
;
286 struct pipe_framebuffer_state fb
;
287 const int srcW
= abs(srcX1
- srcX0
);
288 const int srcH
= abs(srcY1
- srcY0
);
291 float s0
, t0
, s1
, t1
;
293 assert(filter
== PIPE_TEX_MIPFILTER_NEAREST
||
294 filter
== PIPE_TEX_MIPFILTER_LINEAR
);
296 assert(screen
->is_format_supported(screen
, src
->format
, PIPE_TEXTURE_2D
,
297 PIPE_TEXTURE_USAGE_SAMPLER
, 0));
298 assert(screen
->is_format_supported(screen
, dst
->format
, PIPE_TEXTURE_2D
,
299 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0));
301 /* do the regions overlap? */
302 overlap
= util_same_surface(src
, dst
) &&
303 regions_overlap(srcX0
, srcY0
, srcX1
, srcY1
,
304 dstX0
, dstY0
, dstX1
, dstY1
);
307 * Check for simple case: no format conversion, no flipping, no stretching,
309 * Filter mode should not matter since there's no stretching.
311 if (pipe
->surface_copy
&&
312 dst
->format
== src
->format
&&
317 (dstX1
- dstX0
) == (srcX1
- srcX0
) &&
318 (dstY1
- dstY0
) == (srcY1
- srcY0
) &&
320 pipe
->surface_copy(pipe
,
321 dst
, dstX0
, dstY0
, /* dest */
322 src
, srcX0
, srcY0
, /* src */
323 srcW
, srcH
); /* size */
327 assert(screen
->is_format_supported(screen
, dst
->format
, PIPE_TEXTURE_2D
,
328 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0));
330 /* Create a temporary texture when src and dest alias or when src
331 * is anything other than a single-level 2d texture.
333 * This can still be improved upon.
335 if (util_same_surface(src
, dst
) ||
336 src
->texture
->target
!= PIPE_TEXTURE_2D
||
337 src
->texture
->last_level
!= 0)
339 struct pipe_texture texTemp
;
340 struct pipe_surface
*texSurf
;
341 const int srcLeft
= MIN2(srcX0
, srcX1
);
342 const int srcTop
= MIN2(srcY0
, srcY1
);
344 if (srcLeft
!= srcX0
) {
345 /* left-right flip */
351 if (srcTop
!= srcY0
) {
358 /* create temp texture */
359 memset(&texTemp
, 0, sizeof(texTemp
));
360 texTemp
.target
= PIPE_TEXTURE_2D
;
361 texTemp
.format
= src
->format
;
362 texTemp
.last_level
= 0;
363 texTemp
.width0
= srcW
;
364 texTemp
.height0
= srcH
;
367 tex
= screen
->texture_create(screen
, &texTemp
);
371 texSurf
= screen
->get_tex_surface(screen
, tex
, 0, 0, 0,
372 PIPE_BUFFER_USAGE_GPU_WRITE
);
374 /* load temp texture */
375 if (pipe
->surface_copy
) {
376 pipe
->surface_copy(pipe
,
377 texSurf
, 0, 0, /* dest */
378 src
, srcLeft
, srcTop
, /* src */
379 srcW
, srcH
); /* size */
381 util_surface_copy(pipe
, FALSE
,
382 texSurf
, 0, 0, /* dest */
383 src
, srcLeft
, srcTop
, /* src */
384 srcW
, srcH
); /* size */
387 /* free the surface, update the texture if necessary.
389 pipe_surface_reference(&texSurf
, NULL
);
396 pipe_texture_reference(&tex
, src
->texture
);
397 s0
= srcX0
/ (float)tex
->width0
;
398 s1
= srcX1
/ (float)tex
->width0
;
399 t0
= srcY0
/ (float)tex
->height0
;
400 t1
= srcY1
/ (float)tex
->height0
;
404 /* save state (restored below) */
405 cso_save_blend(ctx
->cso
);
406 cso_save_depth_stencil_alpha(ctx
->cso
);
407 cso_save_rasterizer(ctx
->cso
);
408 cso_save_samplers(ctx
->cso
);
409 cso_save_sampler_textures(ctx
->cso
);
410 cso_save_framebuffer(ctx
->cso
);
411 cso_save_fragment_shader(ctx
->cso
);
412 cso_save_vertex_shader(ctx
->cso
);
414 /* set misc state we care about */
415 cso_set_blend(ctx
->cso
, &ctx
->blend
);
416 cso_set_depth_stencil_alpha(ctx
->cso
, &ctx
->depthstencil
);
417 cso_set_rasterizer(ctx
->cso
, &ctx
->rasterizer
);
420 ctx
->sampler
.min_img_filter
= filter
;
421 ctx
->sampler
.mag_img_filter
= filter
;
422 cso_single_sampler(ctx
->cso
, 0, &ctx
->sampler
);
423 cso_single_sampler_done(ctx
->cso
);
426 cso_set_sampler_textures(ctx
->cso
, 1, &tex
);
428 if (ctx
->fs
[writemask
] == NULL
)
430 util_make_fragment_tex_shader_writemask(pipe
, TGSI_TEXTURE_2D
,
434 cso_set_fragment_shader_handle(ctx
->cso
, ctx
->fs
[writemask
]);
435 cso_set_vertex_shader_handle(ctx
->cso
, ctx
->vs
);
438 memset(&fb
, 0, sizeof(fb
));
439 fb
.width
= dst
->width
;
440 fb
.height
= dst
->height
;
443 cso_set_framebuffer(ctx
->cso
, &fb
);
446 offset
= setup_vertex_data_tex(ctx
,
447 (float) dstX0
, (float) dstY0
,
448 (float) dstX1
, (float) dstY1
,
453 util_draw_vertex_buffer(ctx
->pipe
, ctx
->vbuf
, offset
,
454 PIPE_PRIM_TRIANGLE_FAN
,
456 2); /* attribs/vert */
458 /* restore state we changed */
459 cso_restore_blend(ctx
->cso
);
460 cso_restore_depth_stencil_alpha(ctx
->cso
);
461 cso_restore_rasterizer(ctx
->cso
);
462 cso_restore_samplers(ctx
->cso
);
463 cso_restore_sampler_textures(ctx
->cso
);
464 cso_restore_framebuffer(ctx
->cso
);
465 cso_restore_fragment_shader(ctx
->cso
);
466 cso_restore_vertex_shader(ctx
->cso
);
468 pipe_texture_reference(&tex
, NULL
);
473 util_blit_pixels(struct blit_state
*ctx
,
474 struct pipe_surface
*src
,
475 int srcX0
, int srcY0
,
476 int srcX1
, int srcY1
,
477 struct pipe_surface
*dst
,
478 int dstX0
, int dstY0
,
479 int dstX1
, int dstY1
,
480 float z
, uint filter
)
482 util_blit_pixels_writemask( ctx
, src
,
489 TGSI_WRITEMASK_XYZW
);
493 /* Release vertex buffer at end of frame to avoid synchronous
496 void util_blit_flush( struct blit_state
*ctx
)
498 pipe_buffer_reference(&ctx
->vbuf
, NULL
);
505 * Copy pixel block from src texture to dst surface.
506 * Overlapping regions are acceptable.
508 * XXX Should support selection of level.
509 * XXX need some control over blitting Z and/or stencil.
512 util_blit_pixels_tex(struct blit_state
*ctx
,
513 struct pipe_texture
*tex
,
514 int srcX0
, int srcY0
,
515 int srcX1
, int srcY1
,
516 struct pipe_surface
*dst
,
517 int dstX0
, int dstY0
,
518 int dstX1
, int dstY1
,
519 float z
, uint filter
)
521 struct pipe_framebuffer_state fb
;
522 float s0
, t0
, s1
, t1
;
525 assert(filter
== PIPE_TEX_MIPFILTER_NEAREST
||
526 filter
== PIPE_TEX_MIPFILTER_LINEAR
);
528 assert(tex
->width0
!= 0);
529 assert(tex
->height0
!= 0);
531 s0
= srcX0
/ (float)tex
->width0
;
532 s1
= srcX1
/ (float)tex
->width0
;
533 t0
= srcY0
/ (float)tex
->height0
;
534 t1
= srcY1
/ (float)tex
->height0
;
536 assert(ctx
->pipe
->screen
->is_format_supported(ctx
->pipe
->screen
, dst
->format
,
538 PIPE_TEXTURE_USAGE_RENDER_TARGET
,
541 /* save state (restored below) */
542 cso_save_blend(ctx
->cso
);
543 cso_save_depth_stencil_alpha(ctx
->cso
);
544 cso_save_rasterizer(ctx
->cso
);
545 cso_save_samplers(ctx
->cso
);
546 cso_save_sampler_textures(ctx
->cso
);
547 cso_save_framebuffer(ctx
->cso
);
548 cso_save_fragment_shader(ctx
->cso
);
549 cso_save_vertex_shader(ctx
->cso
);
551 /* set misc state we care about */
552 cso_set_blend(ctx
->cso
, &ctx
->blend
);
553 cso_set_depth_stencil_alpha(ctx
->cso
, &ctx
->depthstencil
);
554 cso_set_rasterizer(ctx
->cso
, &ctx
->rasterizer
);
557 ctx
->sampler
.min_img_filter
= filter
;
558 ctx
->sampler
.mag_img_filter
= filter
;
559 cso_single_sampler(ctx
->cso
, 0, &ctx
->sampler
);
560 cso_single_sampler_done(ctx
->cso
);
563 cso_set_sampler_textures(ctx
->cso
, 1, &tex
);
566 cso_set_fragment_shader_handle(ctx
->cso
, ctx
->fs
[TGSI_WRITEMASK_XYZW
]);
567 cso_set_vertex_shader_handle(ctx
->cso
, ctx
->vs
);
570 memset(&fb
, 0, sizeof(fb
));
571 fb
.width
= dst
->width
;
572 fb
.height
= dst
->height
;
575 cso_set_framebuffer(ctx
->cso
, &fb
);
578 offset
= setup_vertex_data_tex(ctx
,
579 (float) dstX0
, (float) dstY0
,
580 (float) dstX1
, (float) dstY1
,
584 util_draw_vertex_buffer(ctx
->pipe
,
586 PIPE_PRIM_TRIANGLE_FAN
,
588 2); /* attribs/vert */
590 /* restore state we changed */
591 cso_restore_blend(ctx
->cso
);
592 cso_restore_depth_stencil_alpha(ctx
->cso
);
593 cso_restore_rasterizer(ctx
->cso
);
594 cso_restore_samplers(ctx
->cso
);
595 cso_restore_sampler_textures(ctx
->cso
);
596 cso_restore_framebuffer(ctx
->cso
);
597 cso_restore_fragment_shader(ctx
->cso
);
598 cso_restore_vertex_shader(ctx
->cso
);