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_format.h"
42 #include "util/u_memory.h"
43 #include "util/u_math.h"
44 #include "util/u_blitter.h"
45 #include "util/u_draw_quad.h"
46 #include "util/u_pack_color.h"
47 #include "util/u_rect.h"
48 #include "util/u_simple_shaders.h"
49 #include "util/u_texture.h"
51 struct blitter_context_priv
53 struct blitter_context blitter
;
55 struct pipe_context
*pipe
; /**< pipe context */
56 struct pipe_buffer
*vbuf
; /**< quad */
58 float vertices
[4][2][4]; /**< {pos, color} or {pos, texcoord} */
60 /* Templates for various state objects. */
61 struct pipe_depth_stencil_alpha_state template_dsa
;
62 struct pipe_sampler_state template_sampler_state
;
64 /* Constant state objects. */
66 void *vs_col
; /**< Vertex shader which passes {pos, color} to the output */
67 void *vs_tex
; /**<Vertex shader which passes {pos, texcoord} to the output.*/
69 /* Fragment shaders. */
70 /* FS which outputs a color to multiple color buffers. */
71 void *fs_col
[PIPE_MAX_COLOR_BUFS
];
73 /* FS which outputs a color from a texture,
74 where the index is PIPE_TEXTURE_* to be sampled. */
75 void *fs_texfetch_col
[PIPE_MAX_TEXTURE_TYPES
];
77 /* FS which outputs a depth from a texture,
78 where the index is PIPE_TEXTURE_* to be sampled. */
79 void *fs_texfetch_depth
[PIPE_MAX_TEXTURE_TYPES
];
82 void *blend_write_color
; /**< blend state with writemask of RGBA */
83 void *blend_keep_color
; /**< blend state with writemask of 0 */
85 /* Depth stencil alpha state. */
86 void *dsa_write_depth_stencil
[0xff]; /**< indices are stencil clear values */
87 void *dsa_write_depth_keep_stencil
;
88 void *dsa_keep_depth_stencil
;
90 /* Sampler state for clamping to a miplevel. */
91 void *sampler_state
[PIPE_MAX_TEXTURE_LEVELS
];
93 /* Rasterizer state. */
97 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
)
99 struct blitter_context_priv
*ctx
;
100 struct pipe_blend_state blend
;
101 struct pipe_depth_stencil_alpha_state
*dsa
;
102 struct pipe_rasterizer_state rs_state
;
103 struct pipe_sampler_state
*sampler_state
;
106 ctx
= CALLOC_STRUCT(blitter_context_priv
);
112 /* init state objects for them to be considered invalid */
113 ctx
->blitter
.saved_fb_state
.nr_cbufs
= ~0;
114 ctx
->blitter
.saved_num_textures
= ~0;
115 ctx
->blitter
.saved_num_sampler_states
= ~0;
117 /* blend state objects */
118 memset(&blend
, 0, sizeof(blend
));
119 ctx
->blend_keep_color
= pipe
->create_blend_state(pipe
, &blend
);
121 blend
.colormask
= PIPE_MASK_RGBA
;
122 ctx
->blend_write_color
= pipe
->create_blend_state(pipe
, &blend
);
124 /* depth stencil alpha state objects */
125 dsa
= &ctx
->template_dsa
;
126 ctx
->dsa_keep_depth_stencil
=
127 pipe
->create_depth_stencil_alpha_state(pipe
, dsa
);
129 dsa
->depth
.enabled
= 1;
130 dsa
->depth
.writemask
= 1;
131 dsa
->depth
.func
= PIPE_FUNC_ALWAYS
;
132 ctx
->dsa_write_depth_keep_stencil
=
133 pipe
->create_depth_stencil_alpha_state(pipe
, dsa
);
135 dsa
->stencil
[0].enabled
= 1;
136 dsa
->stencil
[0].func
= PIPE_FUNC_ALWAYS
;
137 dsa
->stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
138 dsa
->stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
139 dsa
->stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
140 dsa
->stencil
[0].valuemask
= 0xff;
141 dsa
->stencil
[0].writemask
= 0xff;
142 /* The DSA state objects which write depth and stencil are created
146 sampler_state
= &ctx
->template_sampler_state
;
147 sampler_state
->wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
148 sampler_state
->wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
149 sampler_state
->wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
150 /* The sampler state objects which sample from a specified mipmap level
151 * are created on-demand. */
153 /* rasterizer state */
154 memset(&rs_state
, 0, sizeof(rs_state
));
155 rs_state
.front_winding
= PIPE_WINDING_CW
;
156 rs_state
.cull_mode
= PIPE_WINDING_NONE
;
157 rs_state
.bypass_vs_clip_and_viewport
= 1;
158 rs_state
.gl_rasterization_rules
= 1;
159 ctx
->rs_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
161 /* fragment shaders are created on-demand */
165 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
166 TGSI_SEMANTIC_COLOR
};
167 const uint semantic_indices
[] = { 0, 0 };
169 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
173 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
174 TGSI_SEMANTIC_GENERIC
};
175 const uint semantic_indices
[] = { 0, 0 };
177 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
181 /* set invariant vertex coordinates */
182 for (i
= 0; i
< 4; i
++)
183 ctx
->vertices
[i
][0][3] = 1; /*v.w*/
185 /* create the vertex buffer */
186 ctx
->vbuf
= pipe_buffer_create(ctx
->pipe
->screen
,
188 PIPE_BUFFER_USAGE_VERTEX
,
189 sizeof(ctx
->vertices
));
191 return &ctx
->blitter
;
194 void util_blitter_destroy(struct blitter_context
*blitter
)
196 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
197 struct pipe_context
*pipe
= ctx
->pipe
;
200 pipe
->delete_blend_state(pipe
, ctx
->blend_write_color
);
201 pipe
->delete_blend_state(pipe
, ctx
->blend_keep_color
);
202 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
203 pipe
->delete_depth_stencil_alpha_state(pipe
,
204 ctx
->dsa_write_depth_keep_stencil
);
206 for (i
= 0; i
< 0xff; i
++)
207 if (ctx
->dsa_write_depth_stencil
[i
])
208 pipe
->delete_depth_stencil_alpha_state(pipe
,
209 ctx
->dsa_write_depth_stencil
[i
]);
211 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
);
212 pipe
->delete_vs_state(pipe
, ctx
->vs_col
);
213 pipe
->delete_vs_state(pipe
, ctx
->vs_tex
);
215 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
216 if (ctx
->fs_texfetch_col
[i
])
217 pipe
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[i
]);
218 if (ctx
->fs_texfetch_depth
[i
])
219 pipe
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
]);
222 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
&& ctx
->fs_col
[i
]; i
++)
224 pipe
->delete_fs_state(pipe
, ctx
->fs_col
[i
]);
226 for (i
= 0; i
< PIPE_MAX_TEXTURE_LEVELS
; i
++)
227 if (ctx
->sampler_state
[i
])
228 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
[i
]);
230 pipe_buffer_reference(&ctx
->vbuf
, NULL
);
234 static void blitter_check_saved_CSOs(struct blitter_context_priv
*ctx
)
236 /* make sure these CSOs have been saved */
237 assert(ctx
->blitter
.saved_blend_state
&&
238 ctx
->blitter
.saved_dsa_state
&&
239 ctx
->blitter
.saved_rs_state
&&
240 ctx
->blitter
.saved_fs
&&
241 ctx
->blitter
.saved_vs
);
244 static void blitter_restore_CSOs(struct blitter_context_priv
*ctx
)
246 struct pipe_context
*pipe
= ctx
->pipe
;
248 /* restore the state objects which are always required to be saved */
249 pipe
->bind_blend_state(pipe
, ctx
->blitter
.saved_blend_state
);
250 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->blitter
.saved_dsa_state
);
251 pipe
->bind_rasterizer_state(pipe
, ctx
->blitter
.saved_rs_state
);
252 pipe
->bind_fs_state(pipe
, ctx
->blitter
.saved_fs
);
253 pipe
->bind_vs_state(pipe
, ctx
->blitter
.saved_vs
);
255 ctx
->blitter
.saved_blend_state
= 0;
256 ctx
->blitter
.saved_dsa_state
= 0;
257 ctx
->blitter
.saved_rs_state
= 0;
258 ctx
->blitter
.saved_fs
= 0;
259 ctx
->blitter
.saved_vs
= 0;
261 /* restore the state objects which are required to be saved before copy/fill
263 if (ctx
->blitter
.saved_fb_state
.nr_cbufs
!= ~0) {
264 pipe
->set_framebuffer_state(pipe
, &ctx
->blitter
.saved_fb_state
);
265 ctx
->blitter
.saved_fb_state
.nr_cbufs
= ~0;
268 if (ctx
->blitter
.saved_num_sampler_states
!= ~0) {
269 pipe
->bind_fragment_sampler_states(pipe
,
270 ctx
->blitter
.saved_num_sampler_states
,
271 ctx
->blitter
.saved_sampler_states
);
272 ctx
->blitter
.saved_num_sampler_states
= ~0;
275 if (ctx
->blitter
.saved_num_textures
!= ~0) {
276 pipe
->set_fragment_sampler_textures(pipe
,
277 ctx
->blitter
.saved_num_textures
,
278 ctx
->blitter
.saved_textures
);
279 ctx
->blitter
.saved_num_textures
= ~0;
283 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
284 unsigned x1
, unsigned y1
,
285 unsigned x2
, unsigned y2
,
290 /* set vertex positions */
291 ctx
->vertices
[0][0][0] = x1
; /*v0.x*/
292 ctx
->vertices
[0][0][1] = y1
; /*v0.y*/
294 ctx
->vertices
[1][0][0] = x2
; /*v1.x*/
295 ctx
->vertices
[1][0][1] = y1
; /*v1.y*/
297 ctx
->vertices
[2][0][0] = x2
; /*v2.x*/
298 ctx
->vertices
[2][0][1] = y2
; /*v2.y*/
300 ctx
->vertices
[3][0][0] = x1
; /*v3.x*/
301 ctx
->vertices
[3][0][1] = y2
; /*v3.y*/
303 for (i
= 0; i
< 4; i
++)
304 ctx
->vertices
[i
][0][2] = depth
; /*z*/
307 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
312 for (i
= 0; i
< 4; i
++) {
313 ctx
->vertices
[i
][1][0] = rgba
[0];
314 ctx
->vertices
[i
][1][1] = rgba
[1];
315 ctx
->vertices
[i
][1][2] = rgba
[2];
316 ctx
->vertices
[i
][1][3] = rgba
[3];
320 static void blitter_set_texcoords_2d(struct blitter_context_priv
*ctx
,
321 struct pipe_surface
*surf
,
322 unsigned x1
, unsigned y1
,
323 unsigned x2
, unsigned y2
)
326 float s1
= x1
/ (float)surf
->width
;
327 float t1
= y1
/ (float)surf
->height
;
328 float s2
= x2
/ (float)surf
->width
;
329 float t2
= y2
/ (float)surf
->height
;
331 ctx
->vertices
[0][1][0] = s1
; /*t0.s*/
332 ctx
->vertices
[0][1][1] = t1
; /*t0.t*/
334 ctx
->vertices
[1][1][0] = s2
; /*t1.s*/
335 ctx
->vertices
[1][1][1] = t1
; /*t1.t*/
337 ctx
->vertices
[2][1][0] = s2
; /*t2.s*/
338 ctx
->vertices
[2][1][1] = t2
; /*t2.t*/
340 ctx
->vertices
[3][1][0] = s1
; /*t3.s*/
341 ctx
->vertices
[3][1][1] = t2
; /*t3.t*/
343 for (i
= 0; i
< 4; i
++) {
344 ctx
->vertices
[i
][1][2] = 0; /*r*/
345 ctx
->vertices
[i
][1][3] = 1; /*q*/
349 static void blitter_set_texcoords_3d(struct blitter_context_priv
*ctx
,
350 struct pipe_surface
*surf
,
351 unsigned x1
, unsigned y1
,
352 unsigned x2
, unsigned y2
)
355 float depth
= u_minify(surf
->texture
->depth0
, surf
->level
);
356 float r
= surf
->zslice
/ depth
;
358 blitter_set_texcoords_2d(ctx
, surf
, x1
, y1
, x2
, y2
);
360 for (i
= 0; i
< 4; i
++)
361 ctx
->vertices
[i
][1][2] = r
; /*r*/
364 static void blitter_set_texcoords_cube(struct blitter_context_priv
*ctx
,
365 struct pipe_surface
*surf
,
366 unsigned x1
, unsigned y1
,
367 unsigned x2
, unsigned y2
)
370 float s1
= x1
/ (float)surf
->width
;
371 float t1
= y1
/ (float)surf
->height
;
372 float s2
= x2
/ (float)surf
->width
;
373 float t2
= y2
/ (float)surf
->height
;
374 const float st
[4][2] = {
375 {s1
, t1
}, {s2
, t1
}, {s2
, t2
}, {s1
, t2
}
378 util_map_texcoords2d_onto_cubemap(surf
->face
,
379 /* pointer, stride in floats */
381 &ctx
->vertices
[0][1][0], 8);
383 for (i
= 0; i
< 4; i
++)
384 ctx
->vertices
[i
][1][3] = 1; /*q*/
387 static void blitter_draw_quad(struct blitter_context_priv
*ctx
)
389 struct pipe_context
*pipe
= ctx
->pipe
;
391 /* write vertices and draw them */
392 pipe_buffer_write(pipe
->screen
, ctx
->vbuf
,
393 0, sizeof(ctx
->vertices
), ctx
->vertices
);
395 util_draw_vertex_buffer(pipe
, ctx
->vbuf
, 0, PIPE_PRIM_TRIANGLE_FAN
,
397 2); /* attribs/vert */
401 void *blitter_get_state_write_depth_stencil(
402 struct blitter_context_priv
*ctx
,
405 struct pipe_context
*pipe
= ctx
->pipe
;
409 /* Create the DSA state on-demand. */
410 if (!ctx
->dsa_write_depth_stencil
[stencil
]) {
411 ctx
->template_dsa
.stencil
[0].ref_value
= stencil
;
413 ctx
->dsa_write_depth_stencil
[stencil
] =
414 pipe
->create_depth_stencil_alpha_state(pipe
, &ctx
->template_dsa
);
417 return ctx
->dsa_write_depth_stencil
[stencil
];
421 void **blitter_get_sampler_state(struct blitter_context_priv
*ctx
,
424 struct pipe_context
*pipe
= ctx
->pipe
;
425 struct pipe_sampler_state
*sampler_state
= &ctx
->template_sampler_state
;
427 assert(miplevel
< PIPE_MAX_TEXTURE_LEVELS
);
429 /* Create the sampler state on-demand. */
430 if (!ctx
->sampler_state
[miplevel
]) {
431 sampler_state
->lod_bias
= miplevel
;
432 sampler_state
->min_lod
= miplevel
;
433 sampler_state
->max_lod
= miplevel
;
435 ctx
->sampler_state
[miplevel
] = pipe
->create_sampler_state(pipe
,
439 /* Return void** so that it can be passed to bind_fragment_sampler_states
441 return &ctx
->sampler_state
[miplevel
];
445 void *blitter_get_fs_col(struct blitter_context_priv
*ctx
, unsigned num_cbufs
)
447 struct pipe_context
*pipe
= ctx
->pipe
;
448 unsigned index
= num_cbufs
? num_cbufs
- 1 : 0;
450 assert(num_cbufs
<= PIPE_MAX_COLOR_BUFS
);
452 if (!ctx
->fs_col
[index
])
454 util_make_fragment_clonecolor_shader(pipe
, num_cbufs
);
456 return ctx
->fs_col
[index
];
460 void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
463 struct pipe_context
*pipe
= ctx
->pipe
;
465 assert(tex_target
< PIPE_MAX_TEXTURE_TYPES
);
467 /* Create the fragment shader on-demand. */
468 if (!ctx
->fs_texfetch_col
[tex_target
]) {
469 switch (tex_target
) {
470 case PIPE_TEXTURE_1D
:
471 ctx
->fs_texfetch_col
[PIPE_TEXTURE_1D
] =
472 util_make_fragment_tex_shader(pipe
, TGSI_TEXTURE_1D
);
474 case PIPE_TEXTURE_2D
:
475 ctx
->fs_texfetch_col
[PIPE_TEXTURE_2D
] =
476 util_make_fragment_tex_shader(pipe
, TGSI_TEXTURE_2D
);
478 case PIPE_TEXTURE_3D
:
479 ctx
->fs_texfetch_col
[PIPE_TEXTURE_3D
] =
480 util_make_fragment_tex_shader(pipe
, TGSI_TEXTURE_3D
);
482 case PIPE_TEXTURE_CUBE
:
483 ctx
->fs_texfetch_col
[PIPE_TEXTURE_CUBE
] =
484 util_make_fragment_tex_shader(pipe
, TGSI_TEXTURE_CUBE
);
490 return ctx
->fs_texfetch_col
[tex_target
];
494 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
497 struct pipe_context
*pipe
= ctx
->pipe
;
499 assert(tex_target
< PIPE_MAX_TEXTURE_TYPES
);
501 /* Create the fragment shader on-demand. */
502 if (!ctx
->fs_texfetch_depth
[tex_target
]) {
503 switch (tex_target
) {
504 case PIPE_TEXTURE_1D
:
505 ctx
->fs_texfetch_depth
[PIPE_TEXTURE_1D
] =
506 util_make_fragment_tex_shader_writedepth(pipe
, TGSI_TEXTURE_1D
);
508 case PIPE_TEXTURE_2D
:
509 ctx
->fs_texfetch_depth
[PIPE_TEXTURE_2D
] =
510 util_make_fragment_tex_shader_writedepth(pipe
, TGSI_TEXTURE_2D
);
512 case PIPE_TEXTURE_3D
:
513 ctx
->fs_texfetch_depth
[PIPE_TEXTURE_3D
] =
514 util_make_fragment_tex_shader_writedepth(pipe
, TGSI_TEXTURE_3D
);
516 case PIPE_TEXTURE_CUBE
:
517 ctx
->fs_texfetch_depth
[PIPE_TEXTURE_CUBE
] =
518 util_make_fragment_tex_shader_writedepth(pipe
,TGSI_TEXTURE_CUBE
);
524 return ctx
->fs_texfetch_depth
[tex_target
];
527 void util_blitter_clear(struct blitter_context
*blitter
,
528 unsigned width
, unsigned height
,
530 unsigned clear_buffers
,
532 double depth
, unsigned stencil
)
534 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
535 struct pipe_context
*pipe
= ctx
->pipe
;
537 assert(num_cbufs
<= PIPE_MAX_COLOR_BUFS
);
539 blitter_check_saved_CSOs(ctx
);
542 if (clear_buffers
& PIPE_CLEAR_COLOR
)
543 pipe
->bind_blend_state(pipe
, ctx
->blend_write_color
);
545 pipe
->bind_blend_state(pipe
, ctx
->blend_keep_color
);
547 if (clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
)
548 pipe
->bind_depth_stencil_alpha_state(pipe
,
549 blitter_get_state_write_depth_stencil(ctx
, stencil
));
551 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
553 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_state
);
554 pipe
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, num_cbufs
));
555 pipe
->bind_vs_state(pipe
, ctx
->vs_col
);
557 blitter_set_clear_color(ctx
, rgba
);
558 blitter_set_rectangle(ctx
, 0, 0, width
, height
, depth
);
559 blitter_draw_quad(ctx
);
560 blitter_restore_CSOs(ctx
);
563 void util_blitter_copy(struct blitter_context
*blitter
,
564 struct pipe_surface
*dst
,
565 unsigned dstx
, unsigned dsty
,
566 struct pipe_surface
*src
,
567 unsigned srcx
, unsigned srcy
,
568 unsigned width
, unsigned height
,
569 boolean ignore_stencil
)
571 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
572 struct pipe_context
*pipe
= ctx
->pipe
;
573 struct pipe_screen
*screen
= pipe
->screen
;
574 struct pipe_framebuffer_state fb_state
;
575 boolean is_stencil
, is_depth
;
576 unsigned dst_tex_usage
;
578 /* give up if textures are not set */
579 assert(dst
->texture
&& src
->texture
);
580 if (!dst
->texture
|| !src
->texture
)
583 is_depth
= util_format_get_component_bits(src
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 0) != 0;
584 is_stencil
= util_format_get_component_bits(src
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 1) != 0;
585 dst_tex_usage
= is_depth
|| is_stencil
? PIPE_TEXTURE_USAGE_DEPTH_STENCIL
:
586 PIPE_TEXTURE_USAGE_RENDER_TARGET
;
588 /* check if we can sample from and render to the surfaces */
589 /* (assuming copying a stencil buffer is not possible) */
590 if ((!ignore_stencil
&& is_stencil
) ||
591 !screen
->is_format_supported(screen
, dst
->format
, dst
->texture
->target
,
593 !screen
->is_format_supported(screen
, src
->format
, src
->texture
->target
,
594 PIPE_TEXTURE_USAGE_SAMPLER
, 0)) {
595 util_surface_copy(pipe
, FALSE
, dst
, dstx
, dsty
, src
, srcx
, srcy
,
600 /* check whether the states are properly saved */
601 blitter_check_saved_CSOs(ctx
);
602 assert(blitter
->saved_fb_state
.nr_cbufs
!= ~0);
603 assert(blitter
->saved_num_textures
!= ~0);
604 assert(blitter
->saved_num_sampler_states
!= ~0);
605 assert(src
->texture
->target
< PIPE_MAX_TEXTURE_TYPES
);
608 fb_state
.width
= dst
->width
;
609 fb_state
.height
= dst
->height
;
612 pipe
->bind_blend_state(pipe
, ctx
->blend_keep_color
);
613 pipe
->bind_depth_stencil_alpha_state(pipe
,
614 ctx
->dsa_write_depth_keep_stencil
);
615 pipe
->bind_fs_state(pipe
,
616 blitter_get_fs_texfetch_depth(ctx
, src
->texture
->target
));
618 fb_state
.nr_cbufs
= 0;
619 fb_state
.zsbuf
= dst
;
621 pipe
->bind_blend_state(pipe
, ctx
->blend_write_color
);
622 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
623 pipe
->bind_fs_state(pipe
,
624 blitter_get_fs_texfetch_col(ctx
, src
->texture
->target
));
626 fb_state
.nr_cbufs
= 1;
627 fb_state
.cbufs
[0] = dst
;
631 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_state
);
632 pipe
->bind_vs_state(pipe
, ctx
->vs_tex
);
633 pipe
->bind_fragment_sampler_states(pipe
, 1,
634 blitter_get_sampler_state(ctx
, src
->level
));
635 pipe
->set_fragment_sampler_textures(pipe
, 1, &src
->texture
);
636 pipe
->set_framebuffer_state(pipe
, &fb_state
);
638 /* set texture coordinates */
639 switch (src
->texture
->target
) {
640 case PIPE_TEXTURE_1D
:
641 case PIPE_TEXTURE_2D
:
642 blitter_set_texcoords_2d(ctx
, src
, srcx
, srcy
,
643 srcx
+width
, srcy
+height
);
645 case PIPE_TEXTURE_3D
:
646 blitter_set_texcoords_3d(ctx
, src
, srcx
, srcy
,
647 srcx
+width
, srcy
+height
);
649 case PIPE_TEXTURE_CUBE
:
650 blitter_set_texcoords_cube(ctx
, src
, srcx
, srcy
,
651 srcx
+width
, srcy
+height
);
657 blitter_set_rectangle(ctx
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0);
658 blitter_draw_quad(ctx
);
659 blitter_restore_CSOs(ctx
);
662 void util_blitter_fill(struct blitter_context
*blitter
,
663 struct pipe_surface
*dst
,
664 unsigned dstx
, unsigned dsty
,
665 unsigned width
, unsigned height
,
668 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
669 struct pipe_context
*pipe
= ctx
->pipe
;
670 struct pipe_screen
*screen
= pipe
->screen
;
671 struct pipe_framebuffer_state fb_state
;
673 ubyte ub_rgba
[4] = {0};
674 union util_color color
;
677 assert(dst
->texture
);
681 /* check if we can render to the surface */
682 if (util_format_is_depth_or_stencil(dst
->format
) || /* unlikely, but you never know */
683 !screen
->is_format_supported(screen
, dst
->format
, dst
->texture
->target
,
684 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0)) {
685 util_surface_fill(pipe
, dst
, dstx
, dsty
, width
, height
, value
);
689 /* unpack the color */
691 util_unpack_color_ub(dst
->format
, &color
,
692 ub_rgba
, ub_rgba
+1, ub_rgba
+2, ub_rgba
+3);
693 for (i
= 0; i
< 4; i
++)
694 rgba
[i
] = ubyte_to_float(ub_rgba
[i
]);
696 /* check the saved state */
697 blitter_check_saved_CSOs(ctx
);
698 assert(blitter
->saved_fb_state
.nr_cbufs
!= ~0);
701 pipe
->bind_blend_state(pipe
, ctx
->blend_write_color
);
702 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
703 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_state
);
704 pipe
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, 1));
705 pipe
->bind_vs_state(pipe
, ctx
->vs_col
);
707 /* set a framebuffer state */
708 fb_state
.width
= dst
->width
;
709 fb_state
.height
= dst
->height
;
710 fb_state
.nr_cbufs
= 1;
711 fb_state
.cbufs
[0] = dst
;
713 pipe
->set_framebuffer_state(pipe
, &fb_state
);
715 blitter_set_clear_color(ctx
, rgba
);
716 blitter_set_rectangle(ctx
, 0, 0, width
, height
, 0);
717 blitter_draw_quad(ctx
);
718 blitter_restore_CSOs(ctx
);