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_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
.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(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 * XXX what about clipping???
266 * XXX need some control over blitting Z and/or stencil.
269 util_blit_pixels_writemask(struct blit_state
*ctx
,
270 struct pipe_surface
*src
,
271 int srcX0
, int srcY0
,
272 int srcX1
, int srcY1
,
273 struct pipe_surface
*dst
,
274 int dstX0
, int dstY0
,
275 int dstX1
, int dstY1
,
276 float z
, uint filter
,
279 struct pipe_context
*pipe
= ctx
->pipe
;
280 struct pipe_screen
*screen
= pipe
->screen
;
281 struct pipe_texture
*tex
= NULL
;
282 struct pipe_framebuffer_state fb
;
283 const int srcW
= abs(srcX1
- srcX0
);
284 const int srcH
= abs(srcY1
- srcY0
);
287 float s0
, t0
, s1
, t1
;
289 assert(filter
== PIPE_TEX_MIPFILTER_NEAREST
||
290 filter
== PIPE_TEX_MIPFILTER_LINEAR
);
292 assert(screen
->is_format_supported(screen
, src
->format
, PIPE_TEXTURE_2D
,
293 PIPE_TEXTURE_USAGE_SAMPLER
, 0));
294 assert(screen
->is_format_supported(screen
, dst
->format
, PIPE_TEXTURE_2D
,
295 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0));
297 /* do the regions overlap? */
298 overlap
= util_same_surface(src
, dst
) &&
299 regions_overlap(srcX0
, srcY0
, srcX1
, srcY1
,
300 dstX0
, dstY0
, dstX1
, dstY1
);
303 * Check for simple case: no format conversion, no flipping, no stretching,
305 * Filter mode should not matter since there's no stretching.
307 if (pipe
->surface_copy
&&
308 dst
->format
== src
->format
&&
313 (dstX1
- dstX0
) == (srcX1
- srcX0
) &&
314 (dstY1
- dstY0
) == (srcY1
- srcY0
) &&
316 pipe
->surface_copy(pipe
,
317 dst
, dstX0
, dstY0
, /* dest */
318 src
, srcX0
, srcY0
, /* src */
319 srcW
, srcH
); /* size */
323 assert(screen
->is_format_supported(screen
, dst
->format
, PIPE_TEXTURE_2D
,
324 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0));
326 /* Create a temporary texture when src and dest alias or when src
327 * is anything other than a single-level 2d texture.
329 * This can still be improved upon.
331 if (util_same_surface(src
, dst
) ||
332 src
->texture
->target
!= PIPE_TEXTURE_2D
||
333 src
->texture
->last_level
!= 0)
335 struct pipe_texture texTemp
;
336 struct pipe_surface
*texSurf
;
337 const int srcLeft
= MIN2(srcX0
, srcX1
);
338 const int srcTop
= MIN2(srcY0
, srcY1
);
340 if (srcLeft
!= srcX0
) {
341 /* left-right flip */
347 if (srcTop
!= srcY0
) {
354 /* create temp texture */
355 memset(&texTemp
, 0, sizeof(texTemp
));
356 texTemp
.target
= PIPE_TEXTURE_2D
;
357 texTemp
.format
= src
->format
;
358 texTemp
.last_level
= 0;
359 texTemp
.width0
= srcW
;
360 texTemp
.height0
= srcH
;
363 tex
= screen
->texture_create(screen
, &texTemp
);
367 texSurf
= screen
->get_tex_surface(screen
, tex
, 0, 0, 0,
368 PIPE_BUFFER_USAGE_GPU_WRITE
);
370 /* load temp texture */
371 if (pipe
->surface_copy
) {
372 pipe
->surface_copy(pipe
,
373 texSurf
, 0, 0, /* dest */
374 src
, srcLeft
, srcTop
, /* src */
375 srcW
, srcH
); /* size */
377 util_surface_copy(pipe
, FALSE
,
378 texSurf
, 0, 0, /* dest */
379 src
, srcLeft
, srcTop
, /* src */
380 srcW
, srcH
); /* size */
383 /* free the surface, update the texture if necessary.
385 pipe_surface_reference(&texSurf
, NULL
);
392 pipe_texture_reference(&tex
, src
->texture
);
393 s0
= srcX0
/ (float)tex
->width0
;
394 s1
= srcX1
/ (float)tex
->width0
;
395 t0
= srcY0
/ (float)tex
->height0
;
396 t1
= srcY1
/ (float)tex
->height0
;
400 /* save state (restored below) */
401 cso_save_blend(ctx
->cso
);
402 cso_save_depth_stencil_alpha(ctx
->cso
);
403 cso_save_rasterizer(ctx
->cso
);
404 cso_save_samplers(ctx
->cso
);
405 cso_save_sampler_textures(ctx
->cso
);
406 cso_save_framebuffer(ctx
->cso
);
407 cso_save_fragment_shader(ctx
->cso
);
408 cso_save_vertex_shader(ctx
->cso
);
410 /* set misc state we care about */
411 cso_set_blend(ctx
->cso
, &ctx
->blend
);
412 cso_set_depth_stencil_alpha(ctx
->cso
, &ctx
->depthstencil
);
413 cso_set_rasterizer(ctx
->cso
, &ctx
->rasterizer
);
416 ctx
->sampler
.min_img_filter
= filter
;
417 ctx
->sampler
.mag_img_filter
= filter
;
418 cso_single_sampler(ctx
->cso
, 0, &ctx
->sampler
);
419 cso_single_sampler_done(ctx
->cso
);
422 cso_set_sampler_textures(ctx
->cso
, 1, &tex
);
424 if (ctx
->fs
[writemask
] == NULL
)
426 util_make_fragment_tex_shader_writemask(pipe
, TGSI_TEXTURE_2D
,
430 cso_set_fragment_shader_handle(ctx
->cso
, ctx
->fs
[writemask
]);
431 cso_set_vertex_shader_handle(ctx
->cso
, ctx
->vs
);
434 memset(&fb
, 0, sizeof(fb
));
435 fb
.width
= dst
->width
;
436 fb
.height
= dst
->height
;
439 cso_set_framebuffer(ctx
->cso
, &fb
);
442 offset
= setup_vertex_data_tex(ctx
,
443 (float) dstX0
, (float) dstY0
,
444 (float) dstX1
, (float) dstY1
,
449 util_draw_vertex_buffer(ctx
->pipe
, ctx
->vbuf
, offset
,
450 PIPE_PRIM_TRIANGLE_FAN
,
452 2); /* attribs/vert */
454 /* restore state we changed */
455 cso_restore_blend(ctx
->cso
);
456 cso_restore_depth_stencil_alpha(ctx
->cso
);
457 cso_restore_rasterizer(ctx
->cso
);
458 cso_restore_samplers(ctx
->cso
);
459 cso_restore_sampler_textures(ctx
->cso
);
460 cso_restore_framebuffer(ctx
->cso
);
461 cso_restore_fragment_shader(ctx
->cso
);
462 cso_restore_vertex_shader(ctx
->cso
);
464 pipe_texture_reference(&tex
, NULL
);
469 util_blit_pixels(struct blit_state
*ctx
,
470 struct pipe_surface
*src
,
471 int srcX0
, int srcY0
,
472 int srcX1
, int srcY1
,
473 struct pipe_surface
*dst
,
474 int dstX0
, int dstY0
,
475 int dstX1
, int dstY1
,
476 float z
, uint filter
)
478 util_blit_pixels_writemask( ctx
, src
,
485 TGSI_WRITEMASK_XYZW
);
489 /* Release vertex buffer at end of frame to avoid synchronous
492 void util_blit_flush( struct blit_state
*ctx
)
494 pipe_buffer_reference(&ctx
->vbuf
, NULL
);
501 * Copy pixel block from src texture to dst surface.
502 * Overlapping regions are acceptable.
504 * XXX Should support selection of level.
505 * XXX need some control over blitting Z and/or stencil.
508 util_blit_pixels_tex(struct blit_state
*ctx
,
509 struct pipe_texture
*tex
,
510 int srcX0
, int srcY0
,
511 int srcX1
, int srcY1
,
512 struct pipe_surface
*dst
,
513 int dstX0
, int dstY0
,
514 int dstX1
, int dstY1
,
515 float z
, uint filter
)
517 struct pipe_framebuffer_state fb
;
518 float s0
, t0
, s1
, t1
;
521 assert(filter
== PIPE_TEX_MIPFILTER_NEAREST
||
522 filter
== PIPE_TEX_MIPFILTER_LINEAR
);
524 assert(tex
->width0
!= 0);
525 assert(tex
->height0
!= 0);
527 s0
= srcX0
/ (float)tex
->width0
;
528 s1
= srcX1
/ (float)tex
->width0
;
529 t0
= srcY0
/ (float)tex
->height0
;
530 t1
= srcY1
/ (float)tex
->height0
;
532 assert(ctx
->pipe
->screen
->is_format_supported(ctx
->pipe
->screen
, dst
->format
,
534 PIPE_TEXTURE_USAGE_RENDER_TARGET
,
537 /* save state (restored below) */
538 cso_save_blend(ctx
->cso
);
539 cso_save_depth_stencil_alpha(ctx
->cso
);
540 cso_save_rasterizer(ctx
->cso
);
541 cso_save_samplers(ctx
->cso
);
542 cso_save_sampler_textures(ctx
->cso
);
543 cso_save_framebuffer(ctx
->cso
);
544 cso_save_fragment_shader(ctx
->cso
);
545 cso_save_vertex_shader(ctx
->cso
);
547 /* set misc state we care about */
548 cso_set_blend(ctx
->cso
, &ctx
->blend
);
549 cso_set_depth_stencil_alpha(ctx
->cso
, &ctx
->depthstencil
);
550 cso_set_rasterizer(ctx
->cso
, &ctx
->rasterizer
);
553 ctx
->sampler
.min_img_filter
= filter
;
554 ctx
->sampler
.mag_img_filter
= filter
;
555 cso_single_sampler(ctx
->cso
, 0, &ctx
->sampler
);
556 cso_single_sampler_done(ctx
->cso
);
559 cso_set_sampler_textures(ctx
->cso
, 1, &tex
);
562 cso_set_fragment_shader_handle(ctx
->cso
, ctx
->fs
[TGSI_WRITEMASK_XYZW
]);
563 cso_set_vertex_shader_handle(ctx
->cso
, ctx
->vs
);
566 memset(&fb
, 0, sizeof(fb
));
567 fb
.width
= dst
->width
;
568 fb
.height
= dst
->height
;
571 cso_set_framebuffer(ctx
->cso
, &fb
);
574 offset
= setup_vertex_data_tex(ctx
,
575 (float) dstX0
, (float) dstY0
,
576 (float) dstX1
, (float) dstY1
,
580 util_draw_vertex_buffer(ctx
->pipe
,
582 PIPE_PRIM_TRIANGLE_FAN
,
584 2); /* attribs/vert */
586 /* restore state we changed */
587 cso_restore_blend(ctx
->cso
);
588 cso_restore_depth_stencil_alpha(ctx
->cso
);
589 cso_restore_rasterizer(ctx
->cso
);
590 cso_restore_samplers(ctx
->cso
);
591 cso_restore_sampler_textures(ctx
->cso
);
592 cso_restore_framebuffer(ctx
->cso
);
593 cso_restore_fragment_shader(ctx
->cso
);
594 cso_restore_vertex_shader(ctx
->cso
);