1 /**************************************************************************
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 **************************************************************************/
29 * Blitter utility to facilitate acceleration of the clear, surface_copy,
30 * and surface_fill functions.
35 #include "pipe/p_context.h"
36 #include "pipe/p_defines.h"
37 #include "pipe/p_inlines.h"
38 #include "pipe/p_shader_tokens.h"
39 #include "pipe/p_state.h"
41 #include "util/u_memory.h"
42 #include "util/u_math.h"
43 #include "util/u_blitter.h"
44 #include "util/u_draw_quad.h"
45 #include "util/u_pack_color.h"
46 #include "util/u_rect.h"
47 #include "util/u_simple_shaders.h"
48 #include "util/u_texture.h"
50 struct blitter_context_priv
52 struct blitter_context blitter
;
54 struct pipe_context
*pipe
; /**< pipe context */
55 struct pipe_buffer
*vbuf
; /**< quad */
57 float vertices
[4][2][4]; /**< {pos, color} or {pos, texcoord} */
59 /* Templates for various state objects. */
60 struct pipe_depth_stencil_alpha_state template_dsa
;
61 struct pipe_sampler_state template_sampler_state
;
63 /* Constant state objects. */
65 void *vs_col
; /**< Vertex shader which passes {pos, color} to the output */
66 void *vs_tex
; /**<Vertex shader which passes {pos, texcoord} to the output.*/
68 /* Fragment shaders. */
69 /* FS which outputs a color to multiple color buffers. */
70 void *fs_col
[PIPE_MAX_COLOR_BUFS
];
72 /* FS which outputs a color from a texture,
73 where the index is PIPE_TEXTURE_* to be sampled. */
74 void *fs_texfetch_col
[PIPE_MAX_TEXTURE_TYPES
];
76 /* FS which outputs a depth from a texture,
77 where the index is PIPE_TEXTURE_* to be sampled. */
78 void *fs_texfetch_depth
[PIPE_MAX_TEXTURE_TYPES
];
81 void *blend_write_color
; /**< blend state with writemask of RGBA */
82 void *blend_keep_color
; /**< blend state with writemask of 0 */
84 /* Depth stencil alpha state. */
85 void *dsa_write_depth_stencil
[0xff]; /**< indices are stencil clear values */
86 void *dsa_write_depth_keep_stencil
;
87 void *dsa_keep_depth_stencil
;
89 /* Sampler state for clamping to a miplevel. */
90 void *sampler_state
[PIPE_MAX_TEXTURE_LEVELS
];
92 /* Rasterizer state. */
96 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
)
98 struct blitter_context_priv
*ctx
;
99 struct pipe_blend_state blend
;
100 struct pipe_depth_stencil_alpha_state
*dsa
;
101 struct pipe_rasterizer_state rs_state
;
102 struct pipe_sampler_state
*sampler_state
;
105 ctx
= CALLOC_STRUCT(blitter_context_priv
);
111 /* init state objects for them to be considered invalid */
112 ctx
->blitter
.saved_fb_state
.nr_cbufs
= ~0;
113 ctx
->blitter
.saved_num_textures
= ~0;
114 ctx
->blitter
.saved_num_sampler_states
= ~0;
116 /* blend state objects */
117 memset(&blend
, 0, sizeof(blend
));
118 ctx
->blend_keep_color
= pipe
->create_blend_state(pipe
, &blend
);
120 blend
.colormask
= PIPE_MASK_RGBA
;
121 ctx
->blend_write_color
= pipe
->create_blend_state(pipe
, &blend
);
123 /* depth stencil alpha state objects */
124 dsa
= &ctx
->template_dsa
;
125 ctx
->dsa_keep_depth_stencil
=
126 pipe
->create_depth_stencil_alpha_state(pipe
, dsa
);
128 dsa
->depth
.enabled
= 1;
129 dsa
->depth
.writemask
= 1;
130 dsa
->depth
.func
= PIPE_FUNC_ALWAYS
;
131 ctx
->dsa_write_depth_keep_stencil
=
132 pipe
->create_depth_stencil_alpha_state(pipe
, dsa
);
134 dsa
->stencil
[0].enabled
= 1;
135 dsa
->stencil
[0].func
= PIPE_FUNC_ALWAYS
;
136 dsa
->stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
137 dsa
->stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
138 dsa
->stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
139 dsa
->stencil
[0].valuemask
= 0xff;
140 dsa
->stencil
[0].writemask
= 0xff;
141 /* The DSA state objects which write depth and stencil are created
145 sampler_state
= &ctx
->template_sampler_state
;
146 sampler_state
->wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
147 sampler_state
->wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
148 sampler_state
->wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
149 /* The sampler state objects which sample from a specified mipmap level
150 * are created on-demand. */
152 /* rasterizer state */
153 memset(&rs_state
, 0, sizeof(rs_state
));
154 rs_state
.front_winding
= PIPE_WINDING_CW
;
155 rs_state
.cull_mode
= PIPE_WINDING_NONE
;
156 rs_state
.bypass_vs_clip_and_viewport
= 1;
157 rs_state
.gl_rasterization_rules
= 1;
158 ctx
->rs_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
160 /* fragment shaders are created on-demand */
164 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
165 TGSI_SEMANTIC_COLOR
};
166 const uint semantic_indices
[] = { 0, 0 };
168 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
172 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
173 TGSI_SEMANTIC_GENERIC
};
174 const uint semantic_indices
[] = { 0, 0 };
176 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
180 /* set invariant vertex coordinates */
181 for (i
= 0; i
< 4; i
++)
182 ctx
->vertices
[i
][0][3] = 1; /*v.w*/
184 /* create the vertex buffer */
185 ctx
->vbuf
= pipe_buffer_create(ctx
->pipe
->screen
,
187 PIPE_BUFFER_USAGE_VERTEX
,
188 sizeof(ctx
->vertices
));
190 return &ctx
->blitter
;
193 void util_blitter_destroy(struct blitter_context
*blitter
)
195 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
196 struct pipe_context
*pipe
= ctx
->pipe
;
199 pipe
->delete_blend_state(pipe
, ctx
->blend_write_color
);
200 pipe
->delete_blend_state(pipe
, ctx
->blend_keep_color
);
201 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
202 pipe
->delete_depth_stencil_alpha_state(pipe
,
203 ctx
->dsa_write_depth_keep_stencil
);
205 for (i
= 0; i
< 0xff; i
++)
206 if (ctx
->dsa_write_depth_stencil
[i
])
207 pipe
->delete_depth_stencil_alpha_state(pipe
,
208 ctx
->dsa_write_depth_stencil
[i
]);
210 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
);
211 pipe
->delete_vs_state(pipe
, ctx
->vs_col
);
212 pipe
->delete_vs_state(pipe
, ctx
->vs_tex
);
214 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
215 if (ctx
->fs_texfetch_col
[i
])
216 pipe
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[i
]);
217 if (ctx
->fs_texfetch_depth
[i
])
218 pipe
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
]);
221 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
&& ctx
->fs_col
[i
]; i
++)
223 pipe
->delete_fs_state(pipe
, ctx
->fs_col
[i
]);
225 for (i
= 0; i
< PIPE_MAX_TEXTURE_LEVELS
; i
++)
226 if (ctx
->sampler_state
[i
])
227 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
[i
]);
229 pipe_buffer_reference(&ctx
->vbuf
, NULL
);
233 static void blitter_check_saved_CSOs(struct blitter_context_priv
*ctx
)
235 /* make sure these CSOs have been saved */
236 assert(ctx
->blitter
.saved_blend_state
&&
237 ctx
->blitter
.saved_dsa_state
&&
238 ctx
->blitter
.saved_rs_state
&&
239 ctx
->blitter
.saved_fs
&&
240 ctx
->blitter
.saved_vs
);
243 static void blitter_restore_CSOs(struct blitter_context_priv
*ctx
)
245 struct pipe_context
*pipe
= ctx
->pipe
;
247 /* restore the state objects which are always required to be saved */
248 pipe
->bind_blend_state(pipe
, ctx
->blitter
.saved_blend_state
);
249 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->blitter
.saved_dsa_state
);
250 pipe
->bind_rasterizer_state(pipe
, ctx
->blitter
.saved_rs_state
);
251 pipe
->bind_fs_state(pipe
, ctx
->blitter
.saved_fs
);
252 pipe
->bind_vs_state(pipe
, ctx
->blitter
.saved_vs
);
254 ctx
->blitter
.saved_blend_state
= 0;
255 ctx
->blitter
.saved_dsa_state
= 0;
256 ctx
->blitter
.saved_rs_state
= 0;
257 ctx
->blitter
.saved_fs
= 0;
258 ctx
->blitter
.saved_vs
= 0;
260 /* restore the state objects which are required to be saved before copy/fill
262 if (ctx
->blitter
.saved_fb_state
.nr_cbufs
!= ~0) {
263 pipe
->set_framebuffer_state(pipe
, &ctx
->blitter
.saved_fb_state
);
264 ctx
->blitter
.saved_fb_state
.nr_cbufs
= ~0;
267 if (ctx
->blitter
.saved_num_sampler_states
!= ~0) {
268 pipe
->bind_fragment_sampler_states(pipe
,
269 ctx
->blitter
.saved_num_sampler_states
,
270 ctx
->blitter
.saved_sampler_states
);
271 ctx
->blitter
.saved_num_sampler_states
= ~0;
274 if (ctx
->blitter
.saved_num_textures
!= ~0) {
275 pipe
->set_fragment_sampler_textures(pipe
,
276 ctx
->blitter
.saved_num_textures
,
277 ctx
->blitter
.saved_textures
);
278 ctx
->blitter
.saved_num_textures
= ~0;
282 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
283 unsigned x1
, unsigned y1
,
284 unsigned x2
, unsigned y2
,
289 /* set vertex positions */
290 ctx
->vertices
[0][0][0] = x1
; /*v0.x*/
291 ctx
->vertices
[0][0][1] = y1
; /*v0.y*/
293 ctx
->vertices
[1][0][0] = x2
; /*v1.x*/
294 ctx
->vertices
[1][0][1] = y1
; /*v1.y*/
296 ctx
->vertices
[2][0][0] = x2
; /*v2.x*/
297 ctx
->vertices
[2][0][1] = y2
; /*v2.y*/
299 ctx
->vertices
[3][0][0] = x1
; /*v3.x*/
300 ctx
->vertices
[3][0][1] = y2
; /*v3.y*/
302 for (i
= 0; i
< 4; i
++)
303 ctx
->vertices
[i
][0][2] = depth
; /*z*/
306 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
311 for (i
= 0; i
< 4; i
++) {
312 ctx
->vertices
[i
][1][0] = rgba
[0];
313 ctx
->vertices
[i
][1][1] = rgba
[1];
314 ctx
->vertices
[i
][1][2] = rgba
[2];
315 ctx
->vertices
[i
][1][3] = rgba
[3];
319 static void blitter_set_texcoords_2d(struct blitter_context_priv
*ctx
,
320 struct pipe_surface
*surf
,
321 unsigned x1
, unsigned y1
,
322 unsigned x2
, unsigned y2
)
325 float s1
= x1
/ (float)surf
->width
;
326 float t1
= y1
/ (float)surf
->height
;
327 float s2
= x2
/ (float)surf
->width
;
328 float t2
= y2
/ (float)surf
->height
;
330 ctx
->vertices
[0][1][0] = s1
; /*t0.s*/
331 ctx
->vertices
[0][1][1] = t1
; /*t0.t*/
333 ctx
->vertices
[1][1][0] = s2
; /*t1.s*/
334 ctx
->vertices
[1][1][1] = t1
; /*t1.t*/
336 ctx
->vertices
[2][1][0] = s2
; /*t2.s*/
337 ctx
->vertices
[2][1][1] = t2
; /*t2.t*/
339 ctx
->vertices
[3][1][0] = s1
; /*t3.s*/
340 ctx
->vertices
[3][1][1] = t2
; /*t3.t*/
342 for (i
= 0; i
< 4; i
++) {
343 ctx
->vertices
[i
][1][2] = 0; /*r*/
344 ctx
->vertices
[i
][1][3] = 1; /*q*/
348 static void blitter_set_texcoords_3d(struct blitter_context_priv
*ctx
,
349 struct pipe_surface
*surf
,
350 unsigned x1
, unsigned y1
,
351 unsigned x2
, unsigned y2
)
354 float depth
= u_minify(surf
->texture
->depth0
, surf
->level
);
355 float r
= surf
->zslice
/ depth
;
357 blitter_set_texcoords_2d(ctx
, surf
, x1
, y1
, x2
, y2
);
359 for (i
= 0; i
< 4; i
++)
360 ctx
->vertices
[i
][1][2] = r
; /*r*/
363 static void blitter_set_texcoords_cube(struct blitter_context_priv
*ctx
,
364 struct pipe_surface
*surf
,
365 unsigned x1
, unsigned y1
,
366 unsigned x2
, unsigned y2
)
369 float s1
= x1
/ (float)surf
->width
;
370 float t1
= y1
/ (float)surf
->height
;
371 float s2
= x2
/ (float)surf
->width
;
372 float t2
= y2
/ (float)surf
->height
;
373 const float st
[4][2] = {
374 {s1
, t1
}, {s2
, t1
}, {s2
, t2
}, {s1
, t2
}
377 util_map_texcoords2d_onto_cubemap(surf
->face
,
378 /* pointer, stride in floats */
380 &ctx
->vertices
[0][1][0], 8);
382 for (i
= 0; i
< 4; i
++)
383 ctx
->vertices
[i
][1][3] = 1; /*q*/
386 static void blitter_draw_quad(struct blitter_context_priv
*ctx
)
388 struct pipe_context
*pipe
= ctx
->pipe
;
390 /* write vertices and draw them */
391 pipe_buffer_write(pipe
->screen
, ctx
->vbuf
,
392 0, sizeof(ctx
->vertices
), ctx
->vertices
);
394 util_draw_vertex_buffer(pipe
, ctx
->vbuf
, 0, PIPE_PRIM_TRIANGLE_FAN
,
396 2); /* attribs/vert */
400 void *blitter_get_state_write_depth_stencil(
401 struct blitter_context_priv
*ctx
,
404 struct pipe_context
*pipe
= ctx
->pipe
;
408 /* Create the DSA state on-demand. */
409 if (!ctx
->dsa_write_depth_stencil
[stencil
]) {
410 ctx
->template_dsa
.stencil
[0].ref_value
= stencil
;
412 ctx
->dsa_write_depth_stencil
[stencil
] =
413 pipe
->create_depth_stencil_alpha_state(pipe
, &ctx
->template_dsa
);
416 return ctx
->dsa_write_depth_stencil
[stencil
];
420 void **blitter_get_sampler_state(struct blitter_context_priv
*ctx
,
423 struct pipe_context
*pipe
= ctx
->pipe
;
424 struct pipe_sampler_state
*sampler_state
= &ctx
->template_sampler_state
;
426 assert(miplevel
< PIPE_MAX_TEXTURE_LEVELS
);
428 /* Create the sampler state on-demand. */
429 if (!ctx
->sampler_state
[miplevel
]) {
430 sampler_state
->lod_bias
= miplevel
;
431 sampler_state
->min_lod
= miplevel
;
432 sampler_state
->max_lod
= miplevel
;
434 ctx
->sampler_state
[miplevel
] = pipe
->create_sampler_state(pipe
,
438 /* Return void** so that it can be passed to bind_fragment_sampler_states
440 return &ctx
->sampler_state
[miplevel
];
444 void *blitter_get_fs_col(struct blitter_context_priv
*ctx
, unsigned num_cbufs
)
446 struct pipe_context
*pipe
= ctx
->pipe
;
447 unsigned index
= num_cbufs
? num_cbufs
- 1 : 0;
449 assert(num_cbufs
<= PIPE_MAX_COLOR_BUFS
);
451 if (!ctx
->fs_col
[index
])
453 util_make_fragment_clonecolor_shader(pipe
, num_cbufs
);
455 return ctx
->fs_col
[index
];
459 void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
462 struct pipe_context
*pipe
= ctx
->pipe
;
464 assert(tex_target
< PIPE_MAX_TEXTURE_TYPES
);
466 /* Create the fragment shader on-demand. */
467 if (!ctx
->fs_texfetch_col
[tex_target
]) {
468 switch (tex_target
) {
469 case PIPE_TEXTURE_1D
:
470 ctx
->fs_texfetch_col
[PIPE_TEXTURE_1D
] =
471 util_make_fragment_tex_shader(pipe
, TGSI_TEXTURE_1D
);
473 case PIPE_TEXTURE_2D
:
474 ctx
->fs_texfetch_col
[PIPE_TEXTURE_2D
] =
475 util_make_fragment_tex_shader(pipe
, TGSI_TEXTURE_2D
);
477 case PIPE_TEXTURE_3D
:
478 ctx
->fs_texfetch_col
[PIPE_TEXTURE_3D
] =
479 util_make_fragment_tex_shader(pipe
, TGSI_TEXTURE_3D
);
481 case PIPE_TEXTURE_CUBE
:
482 ctx
->fs_texfetch_col
[PIPE_TEXTURE_CUBE
] =
483 util_make_fragment_tex_shader(pipe
, TGSI_TEXTURE_CUBE
);
489 return ctx
->fs_texfetch_col
[tex_target
];
493 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
496 struct pipe_context
*pipe
= ctx
->pipe
;
498 assert(tex_target
< PIPE_MAX_TEXTURE_TYPES
);
500 /* Create the fragment shader on-demand. */
501 if (!ctx
->fs_texfetch_depth
[tex_target
]) {
502 switch (tex_target
) {
503 case PIPE_TEXTURE_1D
:
504 ctx
->fs_texfetch_depth
[PIPE_TEXTURE_1D
] =
505 util_make_fragment_tex_shader_writedepth(pipe
, TGSI_TEXTURE_1D
);
507 case PIPE_TEXTURE_2D
:
508 ctx
->fs_texfetch_depth
[PIPE_TEXTURE_2D
] =
509 util_make_fragment_tex_shader_writedepth(pipe
, TGSI_TEXTURE_2D
);
511 case PIPE_TEXTURE_3D
:
512 ctx
->fs_texfetch_depth
[PIPE_TEXTURE_3D
] =
513 util_make_fragment_tex_shader_writedepth(pipe
, TGSI_TEXTURE_3D
);
515 case PIPE_TEXTURE_CUBE
:
516 ctx
->fs_texfetch_depth
[PIPE_TEXTURE_CUBE
] =
517 util_make_fragment_tex_shader_writedepth(pipe
,TGSI_TEXTURE_CUBE
);
523 return ctx
->fs_texfetch_depth
[tex_target
];
526 void util_blitter_clear(struct blitter_context
*blitter
,
527 unsigned width
, unsigned height
,
529 unsigned clear_buffers
,
531 double depth
, unsigned stencil
)
533 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
534 struct pipe_context
*pipe
= ctx
->pipe
;
536 assert(num_cbufs
<= PIPE_MAX_COLOR_BUFS
);
538 blitter_check_saved_CSOs(ctx
);
541 if (clear_buffers
& PIPE_CLEAR_COLOR
)
542 pipe
->bind_blend_state(pipe
, ctx
->blend_write_color
);
544 pipe
->bind_blend_state(pipe
, ctx
->blend_keep_color
);
546 if (clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
)
547 pipe
->bind_depth_stencil_alpha_state(pipe
,
548 blitter_get_state_write_depth_stencil(ctx
, stencil
));
550 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
552 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_state
);
553 pipe
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, num_cbufs
));
554 pipe
->bind_vs_state(pipe
, ctx
->vs_col
);
556 blitter_set_clear_color(ctx
, rgba
);
557 blitter_set_rectangle(ctx
, 0, 0, width
, height
, depth
);
558 blitter_draw_quad(ctx
);
559 blitter_restore_CSOs(ctx
);
562 void util_blitter_copy(struct blitter_context
*blitter
,
563 struct pipe_surface
*dst
,
564 unsigned dstx
, unsigned dsty
,
565 struct pipe_surface
*src
,
566 unsigned srcx
, unsigned srcy
,
567 unsigned width
, unsigned height
,
568 boolean ignore_stencil
)
570 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
571 struct pipe_context
*pipe
= ctx
->pipe
;
572 struct pipe_screen
*screen
= pipe
->screen
;
573 struct pipe_framebuffer_state fb_state
;
574 boolean is_stencil
, is_depth
;
575 unsigned dst_tex_usage
;
577 /* give up if textures are not set */
578 assert(dst
->texture
&& src
->texture
);
579 if (!dst
->texture
|| !src
->texture
)
582 is_depth
= util_format_get_component_bits(src
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0;
583 is_stencil
= util_format_get_component_bits(src
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 1) != 0;
584 dst_tex_usage
= is_depth
|| is_stencil
? PIPE_TEXTURE_USAGE_DEPTH_STENCIL
:
585 PIPE_TEXTURE_USAGE_RENDER_TARGET
;
587 /* check if we can sample from and render to the surfaces */
588 /* (assuming copying a stencil buffer is not possible) */
589 if ((!ignore_stencil
&& is_stencil
) ||
590 !screen
->is_format_supported(screen
, dst
->format
, dst
->texture
->target
,
592 !screen
->is_format_supported(screen
, src
->format
, src
->texture
->target
,
593 PIPE_TEXTURE_USAGE_SAMPLER
, 0)) {
594 util_surface_copy(pipe
, FALSE
, dst
, dstx
, dsty
, src
, srcx
, srcy
,
599 /* check whether the states are properly saved */
600 blitter_check_saved_CSOs(ctx
);
601 assert(blitter
->saved_fb_state
.nr_cbufs
!= ~0);
602 assert(blitter
->saved_num_textures
!= ~0);
603 assert(blitter
->saved_num_sampler_states
!= ~0);
604 assert(src
->texture
->target
< PIPE_MAX_TEXTURE_TYPES
);
607 fb_state
.width
= dst
->width
;
608 fb_state
.height
= dst
->height
;
611 pipe
->bind_blend_state(pipe
, ctx
->blend_keep_color
);
612 pipe
->bind_depth_stencil_alpha_state(pipe
,
613 ctx
->dsa_write_depth_keep_stencil
);
614 pipe
->bind_fs_state(pipe
,
615 blitter_get_fs_texfetch_depth(ctx
, src
->texture
->target
));
617 fb_state
.nr_cbufs
= 0;
618 fb_state
.zsbuf
= dst
;
620 pipe
->bind_blend_state(pipe
, ctx
->blend_write_color
);
621 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
622 pipe
->bind_fs_state(pipe
,
623 blitter_get_fs_texfetch_col(ctx
, src
->texture
->target
));
625 fb_state
.nr_cbufs
= 1;
626 fb_state
.cbufs
[0] = dst
;
630 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_state
);
631 pipe
->bind_vs_state(pipe
, ctx
->vs_tex
);
632 pipe
->bind_fragment_sampler_states(pipe
, 1,
633 blitter_get_sampler_state(ctx
, src
->level
));
634 pipe
->set_fragment_sampler_textures(pipe
, 1, &src
->texture
);
635 pipe
->set_framebuffer_state(pipe
, &fb_state
);
637 /* set texture coordinates */
638 switch (src
->texture
->target
) {
639 case PIPE_TEXTURE_1D
:
640 case PIPE_TEXTURE_2D
:
641 blitter_set_texcoords_2d(ctx
, src
, srcx
, srcy
,
642 srcx
+width
, srcy
+height
);
644 case PIPE_TEXTURE_3D
:
645 blitter_set_texcoords_3d(ctx
, src
, srcx
, srcy
,
646 srcx
+width
, srcy
+height
);
648 case PIPE_TEXTURE_CUBE
:
649 blitter_set_texcoords_cube(ctx
, src
, srcx
, srcy
,
650 srcx
+width
, srcy
+height
);
656 blitter_set_rectangle(ctx
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0);
657 blitter_draw_quad(ctx
);
658 blitter_restore_CSOs(ctx
);
661 void util_blitter_fill(struct blitter_context
*blitter
,
662 struct pipe_surface
*dst
,
663 unsigned dstx
, unsigned dsty
,
664 unsigned width
, unsigned height
,
667 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
668 struct pipe_context
*pipe
= ctx
->pipe
;
669 struct pipe_screen
*screen
= pipe
->screen
;
670 struct pipe_framebuffer_state fb_state
;
672 ubyte ub_rgba
[4] = {0};
673 union util_color color
;
676 assert(dst
->texture
);
680 /* check if we can render to the surface */
681 if (pf_is_depth_or_stencil(dst
->format
) || /* unlikely, but you never know */
682 !screen
->is_format_supported(screen
, dst
->format
, dst
->texture
->target
,
683 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0)) {
684 util_surface_fill(pipe
, dst
, dstx
, dsty
, width
, height
, value
);
688 /* unpack the color */
690 util_unpack_color_ub(dst
->format
, &color
,
691 ub_rgba
, ub_rgba
+1, ub_rgba
+2, ub_rgba
+3);
692 for (i
= 0; i
< 4; i
++)
693 rgba
[i
] = ubyte_to_float(ub_rgba
[i
]);
695 /* check the saved state */
696 blitter_check_saved_CSOs(ctx
);
697 assert(blitter
->saved_fb_state
.nr_cbufs
!= ~0);
700 pipe
->bind_blend_state(pipe
, ctx
->blend_write_color
);
701 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
702 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_state
);
703 pipe
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, 1));
704 pipe
->bind_vs_state(pipe
, ctx
->vs_col
);
706 /* set a framebuffer state */
707 fb_state
.width
= dst
->width
;
708 fb_state
.height
= dst
->height
;
709 fb_state
.nr_cbufs
= 1;
710 fb_state
.cbufs
[0] = dst
;
712 pipe
->set_framebuffer_state(pipe
, &fb_state
);
714 blitter_set_clear_color(ctx
, rgba
);
715 blitter_set_rectangle(ctx
, 0, 0, width
, height
, 0);
716 blitter_draw_quad(ctx
);
717 blitter_restore_CSOs(ctx
);