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_sampler.h"
49 #include "util/u_texture.h"
50 #include "util/u_simple_shaders.h"
52 #include "cso_cache/cso_context.h"
57 struct pipe_context
*pipe
;
58 struct cso_context
*cso
;
60 struct pipe_blend_state blend_write_color
, blend_keep_color
;
61 struct pipe_depth_stencil_alpha_state dsa_keep_depthstencil
;
62 struct pipe_depth_stencil_alpha_state dsa_write_depthstencil
;
63 struct pipe_depth_stencil_alpha_state dsa_write_depth
;
64 struct pipe_depth_stencil_alpha_state dsa_write_stencil
;
65 struct pipe_rasterizer_state rasterizer
;
66 struct pipe_sampler_state sampler
;
67 struct pipe_viewport_state viewport
;
68 struct pipe_vertex_element velem
[2];
69 enum pipe_texture_target internal_target
;
72 void *fs
[PIPE_MAX_TEXTURE_TYPES
][TGSI_WRITEMASK_XYZW
+ 1];
73 void *fs_depthstencil
[PIPE_MAX_TEXTURE_TYPES
];
74 void *fs_depth
[PIPE_MAX_TEXTURE_TYPES
];
75 void *fs_stencil
[PIPE_MAX_TEXTURE_TYPES
];
77 struct pipe_resource
*vbuf
; /**< quad vertices */
80 float vertices
[4][2][4]; /**< vertex/texcoords for quad */
82 boolean has_stencil_export
;
87 * Create state object for blit.
88 * Intended to be created once and re-used for many blit() calls.
91 util_create_blit(struct pipe_context
*pipe
, struct cso_context
*cso
)
93 struct blit_state
*ctx
;
96 ctx
= CALLOC_STRUCT(blit_state
);
103 /* disabled blending/masking */
104 ctx
->blend_write_color
.rt
[0].colormask
= PIPE_MASK_RGBA
;
106 /* depth stencil states */
107 ctx
->dsa_write_depth
.depth
.enabled
= 1;
108 ctx
->dsa_write_depth
.depth
.writemask
= 1;
109 ctx
->dsa_write_depth
.depth
.func
= PIPE_FUNC_ALWAYS
;
110 ctx
->dsa_write_stencil
.stencil
[0].enabled
= 1;
111 ctx
->dsa_write_stencil
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
112 ctx
->dsa_write_stencil
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
113 ctx
->dsa_write_stencil
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
114 ctx
->dsa_write_stencil
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
115 ctx
->dsa_write_stencil
.stencil
[0].valuemask
= 0xff;
116 ctx
->dsa_write_stencil
.stencil
[0].writemask
= 0xff;
117 ctx
->dsa_write_depthstencil
.depth
= ctx
->dsa_write_depth
.depth
;
118 ctx
->dsa_write_depthstencil
.stencil
[0] = ctx
->dsa_write_stencil
.stencil
[0];
121 ctx
->rasterizer
.cull_face
= PIPE_FACE_NONE
;
122 ctx
->rasterizer
.half_pixel_center
= 1;
123 ctx
->rasterizer
.bottom_edge_rule
= 1;
124 ctx
->rasterizer
.depth_clip
= 1;
127 ctx
->sampler
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
128 ctx
->sampler
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
129 ctx
->sampler
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
130 ctx
->sampler
.min_mip_filter
= PIPE_TEX_MIPFILTER_NONE
;
131 ctx
->sampler
.min_img_filter
= 0; /* set later */
132 ctx
->sampler
.mag_img_filter
= 0; /* set later */
134 /* vertex elements state */
135 for (i
= 0; i
< 2; i
++) {
136 ctx
->velem
[i
].src_offset
= i
* 4 * sizeof(float);
137 ctx
->velem
[i
].instance_divisor
= 0;
138 ctx
->velem
[i
].vertex_buffer_index
= cso_get_aux_vertex_buffer_slot(cso
);
139 ctx
->velem
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
144 /* init vertex data that doesn't change */
145 for (i
= 0; i
< 4; i
++) {
146 ctx
->vertices
[i
][0][3] = 1.0f
; /* w */
147 ctx
->vertices
[i
][1][3] = 1.0f
; /* q */
150 if(pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_NPOT_TEXTURES
))
151 ctx
->internal_target
= PIPE_TEXTURE_2D
;
153 ctx
->internal_target
= PIPE_TEXTURE_RECT
;
155 ctx
->has_stencil_export
=
156 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_SHADER_STENCIL_EXPORT
);
163 * Destroy a blit context
166 util_destroy_blit(struct blit_state
*ctx
)
168 struct pipe_context
*pipe
= ctx
->pipe
;
172 pipe
->delete_vs_state(pipe
, ctx
->vs
);
174 for (i
= 0; i
< Elements(ctx
->fs
); i
++) {
175 for (j
= 0; j
< Elements(ctx
->fs
[i
]); j
++) {
177 pipe
->delete_fs_state(pipe
, ctx
->fs
[i
][j
]);
181 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
182 if (ctx
->fs_depthstencil
[i
]) {
183 pipe
->delete_fs_state(pipe
, ctx
->fs_depthstencil
[i
]);
185 if (ctx
->fs_depth
[i
]) {
186 pipe
->delete_fs_state(pipe
, ctx
->fs_depth
[i
]);
188 if (ctx
->fs_stencil
[i
]) {
189 pipe
->delete_fs_state(pipe
, ctx
->fs_stencil
[i
]);
193 pipe_resource_reference(&ctx
->vbuf
, NULL
);
200 * Helper function to set the fragment shaders.
203 set_fragment_shader(struct blit_state
*ctx
, uint writemask
,
204 enum pipe_texture_target pipe_tex
)
206 if (!ctx
->fs
[pipe_tex
][writemask
]) {
207 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(pipe_tex
, 0);
209 ctx
->fs
[pipe_tex
][writemask
] =
210 util_make_fragment_tex_shader_writemask(ctx
->pipe
, tgsi_tex
,
211 TGSI_INTERPOLATE_LINEAR
,
215 cso_set_fragment_shader_handle(ctx
->cso
, ctx
->fs
[pipe_tex
][writemask
]);
220 * Helper function to set the shader which writes depth and stencil.
223 set_depthstencil_fragment_shader(struct blit_state
*ctx
,
224 enum pipe_texture_target pipe_tex
)
226 if (!ctx
->fs_depthstencil
[pipe_tex
]) {
227 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(pipe_tex
, 0);
229 ctx
->fs_depthstencil
[pipe_tex
] =
230 util_make_fragment_tex_shader_writedepthstencil(ctx
->pipe
, tgsi_tex
,
231 TGSI_INTERPOLATE_LINEAR
);
234 cso_set_fragment_shader_handle(ctx
->cso
, ctx
->fs_depthstencil
[pipe_tex
]);
239 * Helper function to set the shader which writes depth.
242 set_depth_fragment_shader(struct blit_state
*ctx
,
243 enum pipe_texture_target pipe_tex
)
245 if (!ctx
->fs_depth
[pipe_tex
]) {
246 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(pipe_tex
, 0);
248 ctx
->fs_depth
[pipe_tex
] =
249 util_make_fragment_tex_shader_writedepth(ctx
->pipe
, tgsi_tex
,
250 TGSI_INTERPOLATE_LINEAR
);
253 cso_set_fragment_shader_handle(ctx
->cso
, ctx
->fs_depth
[pipe_tex
]);
258 * Helper function to set the shader which writes stencil.
261 set_stencil_fragment_shader(struct blit_state
*ctx
,
262 enum pipe_texture_target pipe_tex
)
264 if (!ctx
->fs_stencil
[pipe_tex
]) {
265 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(pipe_tex
, 0);
267 ctx
->fs_stencil
[pipe_tex
] =
268 util_make_fragment_tex_shader_writestencil(ctx
->pipe
, tgsi_tex
,
269 TGSI_INTERPOLATE_LINEAR
);
272 cso_set_fragment_shader_handle(ctx
->cso
, ctx
->fs_stencil
[pipe_tex
]);
277 * Helper function to set the vertex shader.
280 set_vertex_shader(struct blit_state
*ctx
)
282 /* vertex shader - still required to provide the linkage between
283 * fragment shader input semantics and vertex_element/buffers.
286 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
287 TGSI_SEMANTIC_GENERIC
};
288 const uint semantic_indexes
[] = { 0, 0 };
289 ctx
->vs
= util_make_vertex_passthrough_shader(ctx
->pipe
, 2,
294 cso_set_vertex_shader_handle(ctx
->cso
, ctx
->vs
);
299 * Get offset of next free slot in vertex buffer for quad vertices.
302 get_next_slot( struct blit_state
*ctx
)
304 const unsigned max_slots
= 4096 / sizeof ctx
->vertices
;
306 if (ctx
->vbuf_slot
>= max_slots
) {
307 pipe_resource_reference(&ctx
->vbuf
, NULL
);
312 ctx
->vbuf
= pipe_buffer_create(ctx
->pipe
->screen
,
313 PIPE_BIND_VERTEX_BUFFER
,
315 max_slots
* sizeof ctx
->vertices
);
318 return ctx
->vbuf_slot
++ * sizeof ctx
->vertices
;
325 * Setup vertex data for the textured quad we'll draw.
329 setup_vertex_data_tex(struct blit_state
*ctx
,
332 float x0
, float y0
, float x1
, float y1
,
333 float s0
, float t0
, float s1
, float t1
,
338 ctx
->vertices
[0][0][0] = x0
;
339 ctx
->vertices
[0][0][1] = y0
;
340 ctx
->vertices
[0][0][2] = z
;
341 ctx
->vertices
[0][1][0] = s0
; /*s*/
342 ctx
->vertices
[0][1][1] = t0
; /*t*/
343 ctx
->vertices
[0][1][2] = 0; /*r*/
345 ctx
->vertices
[1][0][0] = x1
;
346 ctx
->vertices
[1][0][1] = y0
;
347 ctx
->vertices
[1][0][2] = z
;
348 ctx
->vertices
[1][1][0] = s1
; /*s*/
349 ctx
->vertices
[1][1][1] = t0
; /*t*/
350 ctx
->vertices
[1][1][2] = 0; /*r*/
352 ctx
->vertices
[2][0][0] = x1
;
353 ctx
->vertices
[2][0][1] = y1
;
354 ctx
->vertices
[2][0][2] = z
;
355 ctx
->vertices
[2][1][0] = s1
;
356 ctx
->vertices
[2][1][1] = t1
;
357 ctx
->vertices
[3][1][2] = 0;
359 ctx
->vertices
[3][0][0] = x0
;
360 ctx
->vertices
[3][0][1] = y1
;
361 ctx
->vertices
[3][0][2] = z
;
362 ctx
->vertices
[3][1][0] = s0
;
363 ctx
->vertices
[3][1][1] = t1
;
364 ctx
->vertices
[3][1][2] = 0;
366 if (src_target
== PIPE_TEXTURE_CUBE
||
367 src_target
== PIPE_TEXTURE_CUBE_ARRAY
) {
368 /* Map cubemap texture coordinates inplace. */
369 const unsigned stride
= sizeof ctx
->vertices
[0] / sizeof ctx
->vertices
[0][0][0];
370 util_map_texcoords2d_onto_cubemap(src_face
,
371 &ctx
->vertices
[0][1][0], stride
,
372 &ctx
->vertices
[0][1][0], stride
);
375 offset
= get_next_slot( ctx
);
378 pipe_buffer_write_nooverlap(ctx
->pipe
, ctx
->vbuf
,
379 offset
, sizeof(ctx
->vertices
), ctx
->vertices
);
387 * \return TRUE if two regions overlap, FALSE otherwise
390 regions_overlap(int srcX0
, int srcY0
,
391 int srcX1
, int srcY1
,
392 int dstX0
, int dstY0
,
393 int dstX1
, int dstY1
)
395 if (MAX2(srcX0
, srcX1
) < MIN2(dstX0
, dstX1
))
396 return FALSE
; /* src completely left of dst */
398 if (MAX2(dstX0
, dstX1
) < MIN2(srcX0
, srcX1
))
399 return FALSE
; /* dst completely left of src */
401 if (MAX2(srcY0
, srcY1
) < MIN2(dstY0
, dstY1
))
402 return FALSE
; /* src completely above dst */
404 if (MAX2(dstY0
, dstY1
) < MIN2(srcY0
, srcY1
))
405 return FALSE
; /* dst completely above src */
407 return TRUE
; /* some overlap */
412 * Can we blit from src format to dest format with a simple copy?
415 formats_compatible(enum pipe_format src_format
,
416 enum pipe_format dst_format
)
418 if (src_format
== dst_format
) {
422 const struct util_format_description
*src_desc
=
423 util_format_description(src_format
);
424 const struct util_format_description
*dst_desc
=
425 util_format_description(dst_format
);
426 return util_is_format_compatible(src_desc
, dst_desc
);
432 * Copy pixel block from src surface to dst surface.
433 * Overlapping regions are acceptable.
434 * Flipping and stretching are supported.
435 * \param filter one of PIPE_TEX_MIPFILTER_NEAREST/LINEAR
436 * \param writemask controls which channels in the dest surface are sourced
437 * from the src surface. Disabled channels are sourced
441 util_blit_pixels(struct blit_state
*ctx
,
442 struct pipe_resource
*src_tex
,
444 int srcX0
, int srcY0
,
445 int srcX1
, int srcY1
,
447 struct pipe_surface
*dst
,
448 int dstX0
, int dstY0
,
449 int dstX1
, int dstY1
,
450 float z
, uint filter
,
451 uint writemask
, uint zs_writemask
)
453 struct pipe_context
*pipe
= ctx
->pipe
;
454 struct pipe_screen
*screen
= pipe
->screen
;
455 enum pipe_format src_format
, dst_format
;
456 struct pipe_sampler_view
*sampler_view
= NULL
;
457 struct pipe_sampler_view sv_templ
;
458 struct pipe_surface
*dst_surface
;
459 struct pipe_framebuffer_state fb
;
460 const int srcW
= abs(srcX1
- srcX0
);
461 const int srcH
= abs(srcY1
- srcY0
);
464 float s0
, t0
, s1
, t1
;
466 boolean is_stencil
, is_depth
, blit_depth
, blit_stencil
;
467 const struct util_format_description
*src_desc
=
468 util_format_description(src_tex
->format
);
470 assert(filter
== PIPE_TEX_MIPFILTER_NEAREST
||
471 filter
== PIPE_TEX_MIPFILTER_LINEAR
);
473 assert(src_level
<= src_tex
->last_level
);
475 /* do the regions overlap? */
476 overlap
= src_tex
== dst
->texture
&&
477 dst
->u
.tex
.level
== src_level
&&
478 dst
->u
.tex
.first_layer
== srcZ0
&&
479 regions_overlap(srcX0
, srcY0
, srcX1
, srcY1
,
480 dstX0
, dstY0
, dstX1
, dstY1
);
482 src_format
= util_format_linear(src_tex
->format
);
483 dst_format
= util_format_linear(dst
->texture
->format
);
485 /* See whether we will blit depth or stencil. */
486 is_depth
= util_format_has_depth(src_desc
);
487 is_stencil
= util_format_has_stencil(src_desc
);
489 blit_depth
= is_depth
&& (zs_writemask
& BLIT_WRITEMASK_Z
);
490 blit_stencil
= is_stencil
&& (zs_writemask
& BLIT_WRITEMASK_STENCIL
);
492 assert((writemask
&& !zs_writemask
&& !is_depth
&& !is_stencil
) ||
493 (!writemask
&& (blit_depth
|| blit_stencil
)));
496 * Check for simple case: no format conversion, no flipping, no stretching,
497 * no overlapping, same number of samples.
498 * Filter mode should not matter since there's no stretching.
500 if (formats_compatible(src_format
, dst_format
) &&
501 src_tex
->nr_samples
== dst
->texture
->nr_samples
&&
502 is_stencil
== blit_stencil
&&
503 is_depth
== blit_depth
&&
508 (dstX1
- dstX0
) == (srcX1
- srcX0
) &&
509 (dstY1
- dstY0
) == (srcY1
- srcY0
) &&
511 struct pipe_box src_box
;
515 src_box
.width
= srcW
;
516 src_box
.height
= srcH
;
518 pipe
->resource_copy_region(pipe
,
519 dst
->texture
, dst
->u
.tex
.level
,
520 dstX0
, dstY0
, dst
->u
.tex
.first_layer
,/* dest */
526 /* XXX Reading multisample textures is unimplemented. */
527 assert(src_tex
->nr_samples
<= 1);
528 if (src_tex
->nr_samples
> 1) {
532 /* It's a mistake to call this function with a stencil format and
533 * without shader stencil export. We don't do software fallbacks here.
534 * Ignore stencil and only copy depth.
536 if (blit_stencil
&& !ctx
->has_stencil_export
) {
537 blit_stencil
= FALSE
;
543 if (dst_format
== dst
->format
) {
546 struct pipe_surface templ
= *dst
;
547 templ
.format
= dst_format
;
548 dst_surface
= pipe
->create_surface(pipe
, dst
->texture
, &templ
);
551 /* Create a temporary texture when src and dest alias.
553 if (src_tex
== dst_surface
->texture
&&
554 dst_surface
->u
.tex
.level
== src_level
&&
555 dst_surface
->u
.tex
.first_layer
== srcZ0
) {
556 /* Make a temporary texture which contains a copy of the source pixels.
557 * Then we'll sample from the temporary texture.
559 struct pipe_resource texTemp
;
560 struct pipe_resource
*tex
;
561 struct pipe_sampler_view sv_templ
;
562 struct pipe_box src_box
;
563 const int srcLeft
= MIN2(srcX0
, srcX1
);
564 const int srcTop
= MIN2(srcY0
, srcY1
);
566 if (srcLeft
!= srcX0
) {
567 /* left-right flip */
573 if (srcTop
!= srcY0
) {
580 /* create temp texture */
581 memset(&texTemp
, 0, sizeof(texTemp
));
582 texTemp
.target
= ctx
->internal_target
;
583 texTemp
.format
= src_format
;
584 texTemp
.last_level
= 0;
585 texTemp
.width0
= srcW
;
586 texTemp
.height0
= srcH
;
588 texTemp
.array_size
= 1;
589 texTemp
.bind
= PIPE_BIND_SAMPLER_VIEW
;
591 tex
= screen
->resource_create(screen
, &texTemp
);
598 src_box
.width
= srcW
;
599 src_box
.height
= srcH
;
601 /* load temp texture */
602 pipe
->resource_copy_region(pipe
,
603 tex
, 0, 0, 0, 0, /* dest */
604 src_tex
, src_level
, &src_box
);
606 normalized
= tex
->target
!= PIPE_TEXTURE_RECT
;
620 u_sampler_view_default_template(&sv_templ
, tex
, tex
->format
);
621 if (!blit_depth
&& blit_stencil
) {
622 /* set a stencil-only format, e.g. Z24S8 --> X24S8 */
623 sv_templ
.format
= util_format_stencil_only(tex
->format
);
624 assert(sv_templ
.format
!= PIPE_FORMAT_NONE
);
626 sampler_view
= pipe
->create_sampler_view(pipe
, tex
, &sv_templ
);
629 pipe_resource_reference(&tex
, NULL
);
632 pipe_resource_reference(&tex
, NULL
);
635 /* Directly sample from the source resource/texture */
636 u_sampler_view_default_template(&sv_templ
, src_tex
, src_format
);
637 if (!blit_depth
&& blit_stencil
) {
638 /* set a stencil-only format, e.g. Z24S8 --> X24S8 */
639 sv_templ
.format
= util_format_stencil_only(src_format
);
640 assert(sv_templ
.format
!= PIPE_FORMAT_NONE
);
642 sampler_view
= pipe
->create_sampler_view(pipe
, src_tex
, &sv_templ
);
652 normalized
= sampler_view
->texture
->target
!= PIPE_TEXTURE_RECT
;
655 s0
/= (float)(u_minify(sampler_view
->texture
->width0
, src_level
));
656 s1
/= (float)(u_minify(sampler_view
->texture
->width0
, src_level
));
657 t0
/= (float)(u_minify(sampler_view
->texture
->height0
, src_level
));
658 t1
/= (float)(u_minify(sampler_view
->texture
->height0
, src_level
));
662 assert(screen
->is_format_supported(screen
, sampler_view
->format
,
663 ctx
->internal_target
, sampler_view
->texture
->nr_samples
,
664 PIPE_BIND_SAMPLER_VIEW
));
665 assert(screen
->is_format_supported(screen
, dst_format
, ctx
->internal_target
,
666 dst_surface
->texture
->nr_samples
,
667 is_depth
|| is_stencil
? PIPE_BIND_DEPTH_STENCIL
:
668 PIPE_BIND_RENDER_TARGET
));
670 /* save state (restored below) */
671 cso_save_blend(ctx
->cso
);
672 cso_save_depth_stencil_alpha(ctx
->cso
);
673 cso_save_rasterizer(ctx
->cso
);
674 cso_save_sample_mask(ctx
->cso
);
675 cso_save_samplers(ctx
->cso
, PIPE_SHADER_FRAGMENT
);
676 cso_save_sampler_views(ctx
->cso
, PIPE_SHADER_FRAGMENT
);
677 cso_save_stream_outputs(ctx
->cso
);
678 cso_save_viewport(ctx
->cso
);
679 cso_save_framebuffer(ctx
->cso
);
680 cso_save_fragment_shader(ctx
->cso
);
681 cso_save_vertex_shader(ctx
->cso
);
682 cso_save_geometry_shader(ctx
->cso
);
683 cso_save_vertex_elements(ctx
->cso
);
684 cso_save_aux_vertex_buffer_slot(ctx
->cso
);
685 cso_save_render_condition(ctx
->cso
);
687 /* set misc state we care about */
689 cso_set_blend(ctx
->cso
, &ctx
->blend_write_color
);
691 cso_set_blend(ctx
->cso
, &ctx
->blend_keep_color
);
693 cso_set_sample_mask(ctx
->cso
, ~0);
694 cso_set_rasterizer(ctx
->cso
, &ctx
->rasterizer
);
695 cso_set_vertex_elements(ctx
->cso
, 2, ctx
->velem
);
696 cso_set_stream_outputs(ctx
->cso
, 0, NULL
, 0);
697 cso_set_render_condition(ctx
->cso
, NULL
, FALSE
, 0);
699 /* default sampler state */
700 ctx
->sampler
.normalized_coords
= normalized
;
701 ctx
->sampler
.min_img_filter
= filter
;
702 ctx
->sampler
.mag_img_filter
= filter
;
703 ctx
->sampler
.min_lod
= (float) src_level
;
704 ctx
->sampler
.max_lod
= (float) src_level
;
706 /* Depth stencil state, fragment shader and sampler setup depending on what
709 if (blit_depth
&& blit_stencil
) {
710 cso_single_sampler(ctx
->cso
, PIPE_SHADER_FRAGMENT
, 0, &ctx
->sampler
);
711 /* don't filter stencil */
712 ctx
->sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
713 ctx
->sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
714 cso_single_sampler(ctx
->cso
, PIPE_SHADER_FRAGMENT
, 1, &ctx
->sampler
);
716 cso_set_depth_stencil_alpha(ctx
->cso
, &ctx
->dsa_write_depthstencil
);
717 set_depthstencil_fragment_shader(ctx
, sampler_view
->texture
->target
);
719 else if (blit_depth
) {
720 cso_single_sampler(ctx
->cso
, PIPE_SHADER_FRAGMENT
, 0, &ctx
->sampler
);
721 cso_set_depth_stencil_alpha(ctx
->cso
, &ctx
->dsa_write_depth
);
722 set_depth_fragment_shader(ctx
, sampler_view
->texture
->target
);
724 else if (blit_stencil
) {
725 /* don't filter stencil */
726 ctx
->sampler
.min_img_filter
= PIPE_TEX_FILTER_NEAREST
;
727 ctx
->sampler
.mag_img_filter
= PIPE_TEX_FILTER_NEAREST
;
728 cso_single_sampler(ctx
->cso
, PIPE_SHADER_FRAGMENT
, 0, &ctx
->sampler
);
730 cso_set_depth_stencil_alpha(ctx
->cso
, &ctx
->dsa_write_stencil
);
731 set_stencil_fragment_shader(ctx
, sampler_view
->texture
->target
);
734 cso_single_sampler(ctx
->cso
, PIPE_SHADER_FRAGMENT
, 0, &ctx
->sampler
);
735 cso_set_depth_stencil_alpha(ctx
->cso
, &ctx
->dsa_keep_depthstencil
);
736 set_fragment_shader(ctx
, writemask
, sampler_view
->texture
->target
);
738 cso_single_sampler_done(ctx
->cso
, PIPE_SHADER_FRAGMENT
);
741 if (blit_depth
&& blit_stencil
) {
742 /* Setup two samplers, one for depth and the other one for stencil. */
743 struct pipe_sampler_view templ
;
744 struct pipe_sampler_view
*views
[2];
746 templ
= *sampler_view
;
747 templ
.format
= util_format_stencil_only(templ
.format
);
748 assert(templ
.format
!= PIPE_FORMAT_NONE
);
750 views
[0] = sampler_view
;
751 views
[1] = pipe
->create_sampler_view(pipe
, views
[0]->texture
, &templ
);
752 cso_set_sampler_views(ctx
->cso
, PIPE_SHADER_FRAGMENT
, 2, views
);
754 pipe_sampler_view_reference(&views
[1], NULL
);
757 cso_set_sampler_views(ctx
->cso
, PIPE_SHADER_FRAGMENT
, 1, &sampler_view
);
761 ctx
->viewport
.scale
[0] = 0.5f
* dst_surface
->width
;
762 ctx
->viewport
.scale
[1] = 0.5f
* dst_surface
->height
;
763 ctx
->viewport
.scale
[2] = 0.5f
;
764 ctx
->viewport
.scale
[3] = 1.0f
;
765 ctx
->viewport
.translate
[0] = 0.5f
* dst_surface
->width
;
766 ctx
->viewport
.translate
[1] = 0.5f
* dst_surface
->height
;
767 ctx
->viewport
.translate
[2] = 0.5f
;
768 ctx
->viewport
.translate
[3] = 0.0f
;
769 cso_set_viewport(ctx
->cso
, &ctx
->viewport
);
771 set_vertex_shader(ctx
);
772 cso_set_geometry_shader_handle(ctx
->cso
, NULL
);
775 memset(&fb
, 0, sizeof(fb
));
776 fb
.width
= dst_surface
->width
;
777 fb
.height
= dst_surface
->height
;
778 if (blit_depth
|| blit_stencil
) {
779 fb
.zsbuf
= dst_surface
;
782 fb
.cbufs
[0] = dst_surface
;
784 cso_set_framebuffer(ctx
->cso
, &fb
);
787 offset
= setup_vertex_data_tex(ctx
,
788 sampler_view
->texture
->target
,
790 (float) dstX0
/ dst_surface
->width
* 2.0f
- 1.0f
,
791 (float) dstY0
/ dst_surface
->height
* 2.0f
- 1.0f
,
792 (float) dstX1
/ dst_surface
->width
* 2.0f
- 1.0f
,
793 (float) dstY1
/ dst_surface
->height
* 2.0f
- 1.0f
,
799 util_draw_vertex_buffer(ctx
->pipe
, ctx
->cso
, ctx
->vbuf
,
800 cso_get_aux_vertex_buffer_slot(ctx
->cso
),
802 PIPE_PRIM_TRIANGLE_FAN
,
804 2); /* attribs/vert */
807 /* restore state we changed */
808 cso_restore_blend(ctx
->cso
);
809 cso_restore_depth_stencil_alpha(ctx
->cso
);
810 cso_restore_rasterizer(ctx
->cso
);
811 cso_restore_sample_mask(ctx
->cso
);
812 cso_restore_samplers(ctx
->cso
, PIPE_SHADER_FRAGMENT
);
813 cso_restore_sampler_views(ctx
->cso
, PIPE_SHADER_FRAGMENT
);
814 cso_restore_viewport(ctx
->cso
);
815 cso_restore_framebuffer(ctx
->cso
);
816 cso_restore_fragment_shader(ctx
->cso
);
817 cso_restore_vertex_shader(ctx
->cso
);
818 cso_restore_geometry_shader(ctx
->cso
);
819 cso_restore_vertex_elements(ctx
->cso
);
820 cso_restore_aux_vertex_buffer_slot(ctx
->cso
);
821 cso_restore_stream_outputs(ctx
->cso
);
822 cso_restore_render_condition(ctx
->cso
);
824 pipe_sampler_view_reference(&sampler_view
, NULL
);
825 if (dst_surface
!= dst
)
826 pipe_surface_reference(&dst_surface
, NULL
);
831 * Copy pixel block from src sampler view to dst surface.
833 * The sampler view's first_level field indicates the source
834 * mipmap level to use.
836 * The sampler view's first_layer indicate the layer to use, but for
837 * cube maps it must point to the first face. Face is passed in src_face.
839 * The main advantage over util_blit_pixels is that it allows to specify swizzles in
840 * pipe_sampler_view::swizzle_?.
842 * But there is no control over blitting Z and/or stencil.
845 util_blit_pixels_tex(struct blit_state
*ctx
,
846 struct pipe_sampler_view
*src_sampler_view
,
847 int srcX0
, int srcY0
,
848 int srcX1
, int srcY1
,
850 struct pipe_surface
*dst
,
851 int dstX0
, int dstY0
,
852 int dstX1
, int dstY1
,
853 float z
, uint filter
)
855 boolean normalized
= src_sampler_view
->texture
->target
!= PIPE_TEXTURE_RECT
;
856 struct pipe_framebuffer_state fb
;
857 float s0
, t0
, s1
, t1
;
859 struct pipe_resource
*tex
= src_sampler_view
->texture
;
861 assert(filter
== PIPE_TEX_MIPFILTER_NEAREST
||
862 filter
== PIPE_TEX_MIPFILTER_LINEAR
);
865 assert(tex
->width0
!= 0);
866 assert(tex
->height0
!= 0);
875 /* normalize according to the mipmap level's size */
876 int level
= src_sampler_view
->u
.tex
.first_level
;
877 float w
= (float) u_minify(tex
->width0
, level
);
878 float h
= (float) u_minify(tex
->height0
, level
);
885 assert(ctx
->pipe
->screen
->is_format_supported(ctx
->pipe
->screen
, dst
->format
,
887 dst
->texture
->nr_samples
,
888 PIPE_BIND_RENDER_TARGET
));
890 /* save state (restored below) */
891 cso_save_blend(ctx
->cso
);
892 cso_save_depth_stencil_alpha(ctx
->cso
);
893 cso_save_rasterizer(ctx
->cso
);
894 cso_save_sample_mask(ctx
->cso
);
895 cso_save_samplers(ctx
->cso
, PIPE_SHADER_FRAGMENT
);
896 cso_save_sampler_views(ctx
->cso
, PIPE_SHADER_FRAGMENT
);
897 cso_save_stream_outputs(ctx
->cso
);
898 cso_save_viewport(ctx
->cso
);
899 cso_save_framebuffer(ctx
->cso
);
900 cso_save_fragment_shader(ctx
->cso
);
901 cso_save_vertex_shader(ctx
->cso
);
902 cso_save_geometry_shader(ctx
->cso
);
903 cso_save_vertex_elements(ctx
->cso
);
904 cso_save_aux_vertex_buffer_slot(ctx
->cso
);
906 /* set misc state we care about */
907 cso_set_blend(ctx
->cso
, &ctx
->blend_write_color
);
908 cso_set_depth_stencil_alpha(ctx
->cso
, &ctx
->dsa_keep_depthstencil
);
909 cso_set_sample_mask(ctx
->cso
, ~0);
910 cso_set_rasterizer(ctx
->cso
, &ctx
->rasterizer
);
911 cso_set_vertex_elements(ctx
->cso
, 2, ctx
->velem
);
912 cso_set_stream_outputs(ctx
->cso
, 0, NULL
, 0);
915 ctx
->sampler
.normalized_coords
= normalized
;
916 ctx
->sampler
.min_img_filter
= filter
;
917 ctx
->sampler
.mag_img_filter
= filter
;
918 cso_single_sampler(ctx
->cso
, PIPE_SHADER_FRAGMENT
, 0, &ctx
->sampler
);
919 cso_single_sampler_done(ctx
->cso
, PIPE_SHADER_FRAGMENT
);
922 ctx
->viewport
.scale
[0] = 0.5f
* dst
->width
;
923 ctx
->viewport
.scale
[1] = 0.5f
* dst
->height
;
924 ctx
->viewport
.scale
[2] = 0.5f
;
925 ctx
->viewport
.scale
[3] = 1.0f
;
926 ctx
->viewport
.translate
[0] = 0.5f
* dst
->width
;
927 ctx
->viewport
.translate
[1] = 0.5f
* dst
->height
;
928 ctx
->viewport
.translate
[2] = 0.5f
;
929 ctx
->viewport
.translate
[3] = 0.0f
;
930 cso_set_viewport(ctx
->cso
, &ctx
->viewport
);
933 cso_set_sampler_views(ctx
->cso
, PIPE_SHADER_FRAGMENT
, 1, &src_sampler_view
);
936 set_fragment_shader(ctx
, TGSI_WRITEMASK_XYZW
,
937 src_sampler_view
->texture
->target
);
938 set_vertex_shader(ctx
);
939 cso_set_geometry_shader_handle(ctx
->cso
, NULL
);
942 memset(&fb
, 0, sizeof(fb
));
943 fb
.width
= dst
->width
;
944 fb
.height
= dst
->height
;
947 cso_set_framebuffer(ctx
->cso
, &fb
);
950 offset
= setup_vertex_data_tex(ctx
,
951 src_sampler_view
->texture
->target
,
953 (float) dstX0
/ dst
->width
* 2.0f
- 1.0f
,
954 (float) dstY0
/ dst
->height
* 2.0f
- 1.0f
,
955 (float) dstX1
/ dst
->width
* 2.0f
- 1.0f
,
956 (float) dstY1
/ dst
->height
* 2.0f
- 1.0f
,
960 util_draw_vertex_buffer(ctx
->pipe
, ctx
->cso
, ctx
->vbuf
,
961 cso_get_aux_vertex_buffer_slot(ctx
->cso
),
963 PIPE_PRIM_TRIANGLE_FAN
,
965 2); /* attribs/vert */
967 /* restore state we changed */
968 cso_restore_blend(ctx
->cso
);
969 cso_restore_depth_stencil_alpha(ctx
->cso
);
970 cso_restore_rasterizer(ctx
->cso
);
971 cso_restore_sample_mask(ctx
->cso
);
972 cso_restore_samplers(ctx
->cso
, PIPE_SHADER_FRAGMENT
);
973 cso_restore_sampler_views(ctx
->cso
, PIPE_SHADER_FRAGMENT
);
974 cso_restore_viewport(ctx
->cso
);
975 cso_restore_framebuffer(ctx
->cso
);
976 cso_restore_fragment_shader(ctx
->cso
);
977 cso_restore_vertex_shader(ctx
->cso
);
978 cso_restore_geometry_shader(ctx
->cso
);
979 cso_restore_vertex_elements(ctx
->cso
);
980 cso_restore_aux_vertex_buffer_slot(ctx
->cso
);
981 cso_restore_stream_outputs(ctx
->cso
);