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 VMWARE 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, clear_render_target,
30 * clear_depth_stencil, resource_copy_region, and blit functions.
35 #include "pipe/p_context.h"
36 #include "pipe/p_defines.h"
37 #include "util/u_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_sampler.h"
47 #include "util/u_simple_shaders.h"
48 #include "util/u_surface.h"
49 #include "util/u_texture.h"
50 #include "util/u_upload_mgr.h"
52 #define INVALID_PTR ((void*)~0)
54 #define GET_CLEAR_BLEND_STATE_IDX(clear_buffers) \
55 ((clear_buffers) / PIPE_CLEAR_COLOR0)
57 #define NUM_RESOLVE_FRAG_SHADERS 5 /* MSAA 2x, 4x, 8x, 16x, 32x */
58 #define GET_MSAA_RESOLVE_FS_IDX(nr_samples) (util_logbase2(nr_samples)-1)
60 struct blitter_context_priv
62 struct blitter_context base
;
64 struct u_upload_mgr
*upload
;
66 float vertices
[4][2][4]; /**< {pos, color} or {pos, texcoord} */
68 /* Templates for various state objects. */
70 /* Constant state objects. */
72 void *vs
; /**< Vertex shader which passes {pos, generic} to the output.*/
73 void *vs_pos_only
[4]; /**< Vertex shader which passes pos to the output.*/
74 void *vs_layered
; /**< Vertex shader which sets LAYER = INSTANCEID. */
76 /* Fragment shaders. */
78 void *fs_write_one_cbuf
;
79 void *fs_write_all_cbufs
;
81 /* FS which outputs a color from a texture,
82 where the index is PIPE_TEXTURE_* to be sampled. */
83 void *fs_texfetch_col
[PIPE_MAX_TEXTURE_TYPES
];
84 void *fs_texfetch_col_uint
[PIPE_MAX_TEXTURE_TYPES
];
85 void *fs_texfetch_col_sint
[PIPE_MAX_TEXTURE_TYPES
];
87 /* FS which outputs a depth from a texture,
88 where the index is PIPE_TEXTURE_* to be sampled. */
89 void *fs_texfetch_depth
[PIPE_MAX_TEXTURE_TYPES
];
90 void *fs_texfetch_depthstencil
[PIPE_MAX_TEXTURE_TYPES
];
91 void *fs_texfetch_stencil
[PIPE_MAX_TEXTURE_TYPES
];
93 /* FS which outputs one sample from a multisample texture. */
94 void *fs_texfetch_col_msaa
[PIPE_MAX_TEXTURE_TYPES
];
95 void *fs_texfetch_col_msaa_uint
[PIPE_MAX_TEXTURE_TYPES
];
96 void *fs_texfetch_col_msaa_sint
[PIPE_MAX_TEXTURE_TYPES
];
97 void *fs_texfetch_depth_msaa
[PIPE_MAX_TEXTURE_TYPES
];
98 void *fs_texfetch_depthstencil_msaa
[PIPE_MAX_TEXTURE_TYPES
];
99 void *fs_texfetch_stencil_msaa
[PIPE_MAX_TEXTURE_TYPES
];
101 /* FS which outputs an average of all samples. */
102 void *fs_resolve
[PIPE_MAX_TEXTURE_TYPES
][NUM_RESOLVE_FRAG_SHADERS
][2];
105 void *blend
[PIPE_MASK_RGBA
+1][2]; /**< blend state with writemask */
106 void *blend_clear
[GET_CLEAR_BLEND_STATE_IDX(PIPE_CLEAR_COLOR
)+1];
108 /* Depth stencil alpha state. */
109 void *dsa_write_depth_stencil
;
110 void *dsa_write_depth_keep_stencil
;
111 void *dsa_keep_depth_stencil
;
112 void *dsa_keep_depth_write_stencil
;
114 /* Vertex elements states. */
116 void *velem_state_readbuf
[4]; /**< X, XY, XYZ, XYZW */
120 void *sampler_state_linear
;
121 void *sampler_state_rect
;
122 void *sampler_state_rect_linear
;
124 /* Rasterizer state. */
125 void *rs_state
, *rs_state_scissor
, *rs_discard_state
;
127 /* Viewport state. */
128 struct pipe_viewport_state viewport
;
130 /* Destination surface dimensions. */
134 boolean has_geometry_shader
;
135 boolean has_tessellation
;
137 boolean has_stream_out
;
138 boolean has_stencil_export
;
139 boolean has_texture_multisample
;
140 boolean cached_all_shaders
;
142 /* The Draw module overrides these functions.
143 * Always create the blitter before Draw. */
144 void (*bind_fs_state
)(struct pipe_context
*, void *);
145 void (*delete_fs_state
)(struct pipe_context
*, void *);
148 static struct pipe_surface
*
149 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
150 struct pipe_surface
*surf
);
152 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
)
154 struct blitter_context_priv
*ctx
;
155 struct pipe_blend_state blend
;
156 struct pipe_depth_stencil_alpha_state dsa
;
157 struct pipe_rasterizer_state rs_state
;
158 struct pipe_sampler_state sampler_state
;
159 struct pipe_vertex_element velem
[2];
162 ctx
= CALLOC_STRUCT(blitter_context_priv
);
166 ctx
->base
.pipe
= pipe
;
167 ctx
->base
.draw_rectangle
= util_blitter_draw_rectangle
;
168 ctx
->base
.get_next_surface_layer
= util_blitter_get_next_surface_layer
;
170 ctx
->bind_fs_state
= pipe
->bind_fs_state
;
171 ctx
->delete_fs_state
= pipe
->delete_fs_state
;
173 /* init state objects for them to be considered invalid */
174 ctx
->base
.saved_blend_state
= INVALID_PTR
;
175 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
176 ctx
->base
.saved_rs_state
= INVALID_PTR
;
177 ctx
->base
.saved_fs
= INVALID_PTR
;
178 ctx
->base
.saved_vs
= INVALID_PTR
;
179 ctx
->base
.saved_gs
= INVALID_PTR
;
180 ctx
->base
.saved_velem_state
= INVALID_PTR
;
181 ctx
->base
.saved_fb_state
.nr_cbufs
= ~0;
182 ctx
->base
.saved_num_sampler_views
= ~0;
183 ctx
->base
.saved_num_sampler_states
= ~0;
184 ctx
->base
.saved_num_so_targets
= ~0;
186 ctx
->has_geometry_shader
=
187 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
188 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
190 ctx
->has_tessellation
=
191 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_TESS_CTRL
,
192 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
194 ctx
->has_stream_out
=
195 pipe
->screen
->get_param(pipe
->screen
,
196 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0;
198 ctx
->has_stencil_export
=
199 pipe
->screen
->get_param(pipe
->screen
,
200 PIPE_CAP_SHADER_STENCIL_EXPORT
);
202 ctx
->has_texture_multisample
=
203 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TEXTURE_MULTISAMPLE
);
205 /* blend state objects */
206 memset(&blend
, 0, sizeof(blend
));
208 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
209 for (j
= 0; j
< 2; j
++) {
210 memset(&blend
.rt
[0], 0, sizeof(blend
.rt
[0]));
211 blend
.rt
[0].colormask
= i
;
213 blend
.rt
[0].blend_enable
= 1;
214 blend
.rt
[0].rgb_func
= PIPE_BLEND_ADD
;
215 blend
.rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_SRC_ALPHA
;
216 blend
.rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
217 blend
.rt
[0].alpha_func
= PIPE_BLEND_ADD
;
218 blend
.rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_SRC_ALPHA
;
219 blend
.rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
221 ctx
->blend
[i
][j
] = pipe
->create_blend_state(pipe
, &blend
);
225 /* depth stencil alpha state objects */
226 memset(&dsa
, 0, sizeof(dsa
));
227 ctx
->dsa_keep_depth_stencil
=
228 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
230 dsa
.depth
.enabled
= 1;
231 dsa
.depth
.writemask
= 1;
232 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
233 ctx
->dsa_write_depth_keep_stencil
=
234 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
236 dsa
.stencil
[0].enabled
= 1;
237 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
238 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
239 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
240 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
241 dsa
.stencil
[0].valuemask
= 0xff;
242 dsa
.stencil
[0].writemask
= 0xff;
243 ctx
->dsa_write_depth_stencil
=
244 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
246 dsa
.depth
.enabled
= 0;
247 dsa
.depth
.writemask
= 0;
248 ctx
->dsa_keep_depth_write_stencil
=
249 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
252 memset(&sampler_state
, 0, sizeof(sampler_state
));
253 sampler_state
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
254 sampler_state
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
255 sampler_state
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
256 sampler_state
.normalized_coords
= 1;
257 ctx
->sampler_state
= pipe
->create_sampler_state(pipe
, &sampler_state
);
258 sampler_state
.normalized_coords
= 0;
259 ctx
->sampler_state_rect
= pipe
->create_sampler_state(pipe
, &sampler_state
);
261 sampler_state
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
262 sampler_state
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
263 sampler_state
.normalized_coords
= 1;
264 ctx
->sampler_state_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
265 sampler_state
.normalized_coords
= 0;
266 ctx
->sampler_state_rect_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
268 /* rasterizer state */
269 memset(&rs_state
, 0, sizeof(rs_state
));
270 rs_state
.cull_face
= PIPE_FACE_NONE
;
271 rs_state
.half_pixel_center
= 1;
272 rs_state
.bottom_edge_rule
= 1;
273 rs_state
.flatshade
= 1;
274 rs_state
.depth_clip
= 1;
275 ctx
->rs_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
277 rs_state
.scissor
= 1;
278 ctx
->rs_state_scissor
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
280 if (ctx
->has_stream_out
) {
281 rs_state
.scissor
= 0;
282 rs_state
.rasterizer_discard
= 1;
283 ctx
->rs_discard_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
286 ctx
->base
.vb_slot
= 0; /* 0 for now */
288 /* vertex elements states */
289 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
290 for (i
= 0; i
< 2; i
++) {
291 velem
[i
].src_offset
= i
* 4 * sizeof(float);
292 velem
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
293 velem
[i
].vertex_buffer_index
= ctx
->base
.vb_slot
;
295 ctx
->velem_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
297 if (ctx
->has_stream_out
) {
298 static enum pipe_format formats
[4] = {
299 PIPE_FORMAT_R32_UINT
,
300 PIPE_FORMAT_R32G32_UINT
,
301 PIPE_FORMAT_R32G32B32_UINT
,
302 PIPE_FORMAT_R32G32B32A32_UINT
305 for (i
= 0; i
< 4; i
++) {
306 velem
[0].src_format
= formats
[i
];
307 velem
[0].vertex_buffer_index
= ctx
->base
.vb_slot
;
308 ctx
->velem_state_readbuf
[i
] =
309 pipe
->create_vertex_elements_state(pipe
, 1, &velem
[0]);
314 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_INSTANCEID
) &&
315 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT
);
317 /* set invariant vertex coordinates */
318 for (i
= 0; i
< 4; i
++)
319 ctx
->vertices
[i
][0][3] = 1; /*v.w*/
321 ctx
->upload
= u_upload_create(pipe
, 65536, PIPE_BIND_VERTEX_BUFFER
,
327 static void bind_vs_pos_only(struct blitter_context_priv
*ctx
,
328 unsigned num_so_channels
)
330 struct pipe_context
*pipe
= ctx
->base
.pipe
;
331 int index
= num_so_channels
? num_so_channels
- 1 : 0;
333 if (!ctx
->vs_pos_only
[index
]) {
334 struct pipe_stream_output_info so
;
335 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
};
336 const uint semantic_indices
[] = { 0 };
338 memset(&so
, 0, sizeof(so
));
340 so
.output
[0].num_components
= num_so_channels
;
341 so
.stride
[0] = num_so_channels
;
343 ctx
->vs_pos_only
[index
] =
344 util_make_vertex_passthrough_shader_with_so(pipe
, 1, semantic_names
,
345 semantic_indices
, FALSE
,
349 pipe
->bind_vs_state(pipe
, ctx
->vs_pos_only
[index
]);
352 static void bind_vs_passthrough(struct blitter_context_priv
*ctx
)
354 struct pipe_context
*pipe
= ctx
->base
.pipe
;
357 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
358 TGSI_SEMANTIC_GENERIC
};
359 const uint semantic_indices
[] = { 0, 0 };
361 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
362 semantic_indices
, FALSE
);
365 pipe
->bind_vs_state(pipe
, ctx
->vs
);
368 static void bind_vs_layered(struct blitter_context_priv
*ctx
)
370 struct pipe_context
*pipe
= ctx
->base
.pipe
;
372 if (!ctx
->vs_layered
) {
373 ctx
->vs_layered
= util_make_layered_clear_vertex_shader(pipe
);
376 pipe
->bind_vs_state(pipe
, ctx
->vs_layered
);
379 static void bind_fs_empty(struct blitter_context_priv
*ctx
)
381 struct pipe_context
*pipe
= ctx
->base
.pipe
;
383 if (!ctx
->fs_empty
) {
384 assert(!ctx
->cached_all_shaders
);
385 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
388 ctx
->bind_fs_state(pipe
, ctx
->fs_empty
);
391 static void bind_fs_write_one_cbuf(struct blitter_context_priv
*ctx
)
393 struct pipe_context
*pipe
= ctx
->base
.pipe
;
395 if (!ctx
->fs_write_one_cbuf
) {
396 assert(!ctx
->cached_all_shaders
);
397 ctx
->fs_write_one_cbuf
=
398 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
399 TGSI_INTERPOLATE_CONSTANT
, FALSE
);
402 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
405 static void bind_fs_write_all_cbufs(struct blitter_context_priv
*ctx
)
407 struct pipe_context
*pipe
= ctx
->base
.pipe
;
409 if (!ctx
->fs_write_all_cbufs
) {
410 assert(!ctx
->cached_all_shaders
);
411 ctx
->fs_write_all_cbufs
=
412 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
413 TGSI_INTERPOLATE_CONSTANT
, TRUE
);
416 ctx
->bind_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
419 void util_blitter_destroy(struct blitter_context
*blitter
)
421 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
422 struct pipe_context
*pipe
= blitter
->pipe
;
425 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++)
426 for (j
= 0; j
< 2; j
++)
427 pipe
->delete_blend_state(pipe
, ctx
->blend
[i
][j
]);
429 for (i
= 0; i
< ARRAY_SIZE(ctx
->blend_clear
); i
++) {
430 if (ctx
->blend_clear
[i
])
431 pipe
->delete_blend_state(pipe
, ctx
->blend_clear
[i
]);
433 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
434 pipe
->delete_depth_stencil_alpha_state(pipe
,
435 ctx
->dsa_write_depth_keep_stencil
);
436 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
437 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
439 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
);
440 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state_scissor
);
441 if (ctx
->rs_discard_state
)
442 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_discard_state
);
444 pipe
->delete_vs_state(pipe
, ctx
->vs
);
445 for (i
= 0; i
< 4; i
++)
446 if (ctx
->vs_pos_only
[i
])
447 pipe
->delete_vs_state(pipe
, ctx
->vs_pos_only
[i
]);
449 pipe
->delete_vs_state(pipe
, ctx
->vs_layered
);
450 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state
);
451 for (i
= 0; i
< 4; i
++) {
452 if (ctx
->velem_state_readbuf
[i
]) {
453 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[i
]);
457 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
458 if (ctx
->fs_texfetch_col
[i
])
459 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[i
]);
460 if (ctx
->fs_texfetch_col_sint
[i
])
461 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col_sint
[i
]);
462 if (ctx
->fs_texfetch_col_uint
[i
])
463 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col_uint
[i
]);
464 if (ctx
->fs_texfetch_depth
[i
])
465 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
]);
466 if (ctx
->fs_texfetch_depthstencil
[i
])
467 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil
[i
]);
468 if (ctx
->fs_texfetch_stencil
[i
])
469 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil
[i
]);
471 if (ctx
->fs_texfetch_col_msaa
[i
])
472 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col_msaa
[i
]);
473 if (ctx
->fs_texfetch_col_msaa_sint
[i
])
474 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col_msaa_sint
[i
]);
475 if (ctx
->fs_texfetch_col_msaa_uint
[i
])
476 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col_msaa_uint
[i
]);
477 if (ctx
->fs_texfetch_depth_msaa
[i
])
478 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth_msaa
[i
]);
479 if (ctx
->fs_texfetch_depthstencil_msaa
[i
])
480 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil_msaa
[i
]);
481 if (ctx
->fs_texfetch_stencil_msaa
[i
])
482 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil_msaa
[i
]);
484 for (j
= 0; j
< ARRAY_SIZE(ctx
->fs_resolve
[i
]); j
++)
485 for (f
= 0; f
< 2; f
++)
486 if (ctx
->fs_resolve
[i
][j
][f
])
487 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve
[i
][j
][f
]);
491 ctx
->delete_fs_state(pipe
, ctx
->fs_empty
);
492 if (ctx
->fs_write_one_cbuf
)
493 ctx
->delete_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
494 if (ctx
->fs_write_all_cbufs
)
495 ctx
->delete_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
497 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect_linear
);
498 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect
);
499 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_linear
);
500 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
);
501 u_upload_destroy(ctx
->upload
);
505 void util_blitter_set_texture_multisample(struct blitter_context
*blitter
,
508 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
510 ctx
->has_texture_multisample
= supported
;
513 static void blitter_set_running_flag(struct blitter_context_priv
*ctx
)
515 if (ctx
->base
.running
) {
516 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
519 ctx
->base
.running
= TRUE
;
521 ctx
->base
.pipe
->set_active_query_state(ctx
->base
.pipe
, false);
524 static void blitter_unset_running_flag(struct blitter_context_priv
*ctx
)
526 if (!ctx
->base
.running
) {
527 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
530 ctx
->base
.running
= FALSE
;
532 ctx
->base
.pipe
->set_active_query_state(ctx
->base
.pipe
, true);
535 static void blitter_check_saved_vertex_states(struct blitter_context_priv
*ctx
)
537 assert(ctx
->base
.saved_velem_state
!= INVALID_PTR
);
538 assert(ctx
->base
.saved_vs
!= INVALID_PTR
);
539 assert(!ctx
->has_geometry_shader
|| ctx
->base
.saved_gs
!= INVALID_PTR
);
540 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tcs
!= INVALID_PTR
);
541 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tes
!= INVALID_PTR
);
542 assert(!ctx
->has_stream_out
|| ctx
->base
.saved_num_so_targets
!= ~0u);
543 assert(ctx
->base
.saved_rs_state
!= INVALID_PTR
);
546 static void blitter_restore_vertex_states(struct blitter_context_priv
*ctx
)
548 struct pipe_context
*pipe
= ctx
->base
.pipe
;
552 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1,
553 &ctx
->base
.saved_vertex_buffer
);
554 pipe_resource_reference(&ctx
->base
.saved_vertex_buffer
.buffer
, NULL
);
556 /* Vertex elements. */
557 pipe
->bind_vertex_elements_state(pipe
, ctx
->base
.saved_velem_state
);
558 ctx
->base
.saved_velem_state
= INVALID_PTR
;
561 pipe
->bind_vs_state(pipe
, ctx
->base
.saved_vs
);
562 ctx
->base
.saved_vs
= INVALID_PTR
;
564 /* Geometry shader. */
565 if (ctx
->has_geometry_shader
) {
566 pipe
->bind_gs_state(pipe
, ctx
->base
.saved_gs
);
567 ctx
->base
.saved_gs
= INVALID_PTR
;
570 if (ctx
->has_tessellation
) {
571 pipe
->bind_tcs_state(pipe
, ctx
->base
.saved_tcs
);
572 pipe
->bind_tes_state(pipe
, ctx
->base
.saved_tes
);
573 ctx
->base
.saved_tcs
= INVALID_PTR
;
574 ctx
->base
.saved_tes
= INVALID_PTR
;
577 /* Stream outputs. */
578 if (ctx
->has_stream_out
) {
579 unsigned offsets
[PIPE_MAX_SO_BUFFERS
];
580 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
581 offsets
[i
] = (unsigned)-1;
582 pipe
->set_stream_output_targets(pipe
,
583 ctx
->base
.saved_num_so_targets
,
584 ctx
->base
.saved_so_targets
, offsets
);
586 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
587 pipe_so_target_reference(&ctx
->base
.saved_so_targets
[i
], NULL
);
589 ctx
->base
.saved_num_so_targets
= ~0;
593 pipe
->bind_rasterizer_state(pipe
, ctx
->base
.saved_rs_state
);
594 ctx
->base
.saved_rs_state
= INVALID_PTR
;
597 static void blitter_check_saved_fragment_states(struct blitter_context_priv
*ctx
)
599 assert(ctx
->base
.saved_fs
!= INVALID_PTR
);
600 assert(ctx
->base
.saved_dsa_state
!= INVALID_PTR
);
601 assert(ctx
->base
.saved_blend_state
!= INVALID_PTR
);
604 static void blitter_restore_fragment_states(struct blitter_context_priv
*ctx
)
606 struct pipe_context
*pipe
= ctx
->base
.pipe
;
608 /* Fragment shader. */
609 ctx
->bind_fs_state(pipe
, ctx
->base
.saved_fs
);
610 ctx
->base
.saved_fs
= INVALID_PTR
;
612 /* Depth, stencil, alpha. */
613 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->base
.saved_dsa_state
);
614 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
617 pipe
->bind_blend_state(pipe
, ctx
->base
.saved_blend_state
);
618 ctx
->base
.saved_blend_state
= INVALID_PTR
;
621 if (ctx
->base
.is_sample_mask_saved
) {
622 pipe
->set_sample_mask(pipe
, ctx
->base
.saved_sample_mask
);
623 ctx
->base
.is_sample_mask_saved
= FALSE
;
626 /* Miscellaneous states. */
627 /* XXX check whether these are saved and whether they need to be restored
628 * (depending on the operation) */
629 pipe
->set_stencil_ref(pipe
, &ctx
->base
.saved_stencil_ref
);
630 pipe
->set_viewport_states(pipe
, 0, 1, &ctx
->base
.saved_viewport
);
633 static void blitter_check_saved_fb_state(struct blitter_context_priv
*ctx
)
635 assert(ctx
->base
.saved_fb_state
.nr_cbufs
!= ~0u);
638 static void blitter_disable_render_cond(struct blitter_context_priv
*ctx
)
640 struct pipe_context
*pipe
= ctx
->base
.pipe
;
642 if (ctx
->base
.saved_render_cond_query
) {
643 pipe
->render_condition(pipe
, NULL
, FALSE
, 0);
647 static void blitter_restore_render_cond(struct blitter_context_priv
*ctx
)
649 struct pipe_context
*pipe
= ctx
->base
.pipe
;
651 if (ctx
->base
.saved_render_cond_query
) {
652 pipe
->render_condition(pipe
, ctx
->base
.saved_render_cond_query
,
653 ctx
->base
.saved_render_cond_cond
,
654 ctx
->base
.saved_render_cond_mode
);
655 ctx
->base
.saved_render_cond_query
= NULL
;
659 static void blitter_restore_fb_state(struct blitter_context_priv
*ctx
)
661 struct pipe_context
*pipe
= ctx
->base
.pipe
;
663 pipe
->set_framebuffer_state(pipe
, &ctx
->base
.saved_fb_state
);
664 util_unreference_framebuffer_state(&ctx
->base
.saved_fb_state
);
667 static void blitter_check_saved_textures(struct blitter_context_priv
*ctx
)
669 assert(ctx
->base
.saved_num_sampler_states
!= ~0u);
670 assert(ctx
->base
.saved_num_sampler_views
!= ~0u);
673 static void blitter_restore_textures(struct blitter_context_priv
*ctx
)
675 struct pipe_context
*pipe
= ctx
->base
.pipe
;
678 /* Fragment sampler states. */
679 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0,
680 ctx
->base
.saved_num_sampler_states
,
681 ctx
->base
.saved_sampler_states
);
683 ctx
->base
.saved_num_sampler_states
= ~0;
685 /* Fragment sampler views. */
686 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
687 ctx
->base
.saved_num_sampler_views
,
688 ctx
->base
.saved_sampler_views
);
690 for (i
= 0; i
< ctx
->base
.saved_num_sampler_views
; i
++)
691 pipe_sampler_view_reference(&ctx
->base
.saved_sampler_views
[i
], NULL
);
693 ctx
->base
.saved_num_sampler_views
= ~0;
696 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
697 int x1
, int y1
, int x2
, int y2
,
702 /* set vertex positions */
703 ctx
->vertices
[0][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v0.x*/
704 ctx
->vertices
[0][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v0.y*/
706 ctx
->vertices
[1][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v1.x*/
707 ctx
->vertices
[1][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v1.y*/
709 ctx
->vertices
[2][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v2.x*/
710 ctx
->vertices
[2][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v2.y*/
712 ctx
->vertices
[3][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v3.x*/
713 ctx
->vertices
[3][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v3.y*/
715 for (i
= 0; i
< 4; i
++)
716 ctx
->vertices
[i
][0][2] = depth
; /*z*/
719 ctx
->viewport
.scale
[0] = 0.5f
* ctx
->dst_width
;
720 ctx
->viewport
.scale
[1] = 0.5f
* ctx
->dst_height
;
721 ctx
->viewport
.scale
[2] = 1.0f
;
722 ctx
->viewport
.translate
[0] = 0.5f
* ctx
->dst_width
;
723 ctx
->viewport
.translate
[1] = 0.5f
* ctx
->dst_height
;
724 ctx
->viewport
.translate
[2] = 0.0f
;
725 ctx
->base
.pipe
->set_viewport_states(ctx
->base
.pipe
, 0, 1, &ctx
->viewport
);
728 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
729 const union pipe_color_union
*color
)
734 for (i
= 0; i
< 4; i
++) {
735 uint32_t *uiverts
= (uint32_t *)ctx
->vertices
[i
][1];
736 uiverts
[0] = color
->ui
[0];
737 uiverts
[1] = color
->ui
[1];
738 uiverts
[2] = color
->ui
[2];
739 uiverts
[3] = color
->ui
[3];
742 for (i
= 0; i
< 4; i
++) {
743 ctx
->vertices
[i
][1][0] = 0;
744 ctx
->vertices
[i
][1][1] = 0;
745 ctx
->vertices
[i
][1][2] = 0;
746 ctx
->vertices
[i
][1][3] = 0;
751 static void get_texcoords(struct pipe_sampler_view
*src
,
752 unsigned src_width0
, unsigned src_height0
,
753 int x1
, int y1
, int x2
, int y2
,
756 struct pipe_resource
*tex
= src
->texture
;
757 unsigned level
= src
->u
.tex
.first_level
;
758 boolean normalized
= tex
->target
!= PIPE_TEXTURE_RECT
&&
759 tex
->nr_samples
<= 1;
762 out
[0] = x1
/ (float)u_minify(src_width0
, level
);
763 out
[1] = y1
/ (float)u_minify(src_height0
, level
);
764 out
[2] = x2
/ (float)u_minify(src_width0
, level
);
765 out
[3] = y2
/ (float)u_minify(src_height0
, level
);
774 static void set_texcoords_in_vertices(const float coord
[4],
775 float *out
, unsigned stride
)
777 out
[0] = coord
[0]; /*t0.s*/
778 out
[1] = coord
[1]; /*t0.t*/
780 out
[0] = coord
[2]; /*t1.s*/
781 out
[1] = coord
[1]; /*t1.t*/
783 out
[0] = coord
[2]; /*t2.s*/
784 out
[1] = coord
[3]; /*t2.t*/
786 out
[0] = coord
[0]; /*t3.s*/
787 out
[1] = coord
[3]; /*t3.t*/
790 static void blitter_set_texcoords(struct blitter_context_priv
*ctx
,
791 struct pipe_sampler_view
*src
,
792 unsigned src_width0
, unsigned src_height0
,
793 float layer
, unsigned sample
,
794 int x1
, int y1
, int x2
, int y2
)
798 float face_coord
[4][2];
800 get_texcoords(src
, src_width0
, src_height0
, x1
, y1
, x2
, y2
, coord
);
802 if (src
->texture
->target
== PIPE_TEXTURE_CUBE
||
803 src
->texture
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
804 set_texcoords_in_vertices(coord
, &face_coord
[0][0], 2);
805 util_map_texcoords2d_onto_cubemap((unsigned)layer
% 6,
806 /* pointer, stride in floats */
807 &face_coord
[0][0], 2,
808 &ctx
->vertices
[0][1][0], 8,
811 set_texcoords_in_vertices(coord
, &ctx
->vertices
[0][1][0], 8);
815 switch (src
->texture
->target
) {
816 case PIPE_TEXTURE_3D
:
818 float r
= layer
/ (float)u_minify(src
->texture
->depth0
,
819 src
->u
.tex
.first_level
);
820 for (i
= 0; i
< 4; i
++)
821 ctx
->vertices
[i
][1][2] = r
; /*r*/
825 case PIPE_TEXTURE_1D_ARRAY
:
826 for (i
= 0; i
< 4; i
++)
827 ctx
->vertices
[i
][1][1] = (float) layer
; /*t*/
830 case PIPE_TEXTURE_2D_ARRAY
:
831 for (i
= 0; i
< 4; i
++) {
832 ctx
->vertices
[i
][1][2] = (float) layer
; /*r*/
833 ctx
->vertices
[i
][1][3] = (float) sample
; /*q*/
837 case PIPE_TEXTURE_CUBE_ARRAY
:
838 for (i
= 0; i
< 4; i
++)
839 ctx
->vertices
[i
][1][3] = (float) ((unsigned)layer
/ 6); /*w*/
842 case PIPE_TEXTURE_2D
:
843 for (i
= 0; i
< 4; i
++) {
844 ctx
->vertices
[i
][1][3] = (float) sample
; /*r*/
852 static void blitter_set_dst_dimensions(struct blitter_context_priv
*ctx
,
853 unsigned width
, unsigned height
)
855 ctx
->dst_width
= width
;
856 ctx
->dst_height
= height
;
859 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
860 enum pipe_format format
,
861 enum pipe_texture_target target
,
862 unsigned src_nr_samples
,
863 unsigned dst_nr_samples
,
866 struct pipe_context
*pipe
= ctx
->base
.pipe
;
867 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, src_nr_samples
);
868 enum tgsi_return_type stype
;
870 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
872 if (util_format_is_pure_uint(format
))
873 stype
= TGSI_RETURN_TYPE_UINT
;
874 else if (util_format_is_pure_sint(format
))
875 stype
= TGSI_RETURN_TYPE_SINT
;
877 stype
= TGSI_RETURN_TYPE_FLOAT
;
879 if (src_nr_samples
> 1) {
882 /* OpenGL requires that integer textures just copy 1 sample instead
885 if (dst_nr_samples
<= 1 &&
886 stype
!= TGSI_RETURN_TYPE_UINT
&&
887 stype
!= TGSI_RETURN_TYPE_SINT
) {
888 /* The destination has one sample, so we'll do color resolve. */
889 unsigned index
= GET_MSAA_RESOLVE_FS_IDX(src_nr_samples
);
893 shader
= &ctx
->fs_resolve
[target
][index
][filter
];
896 assert(!ctx
->cached_all_shaders
);
897 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
898 *shader
= util_make_fs_msaa_resolve_bilinear(pipe
, tgsi_tex
,
903 *shader
= util_make_fs_msaa_resolve(pipe
, tgsi_tex
,
910 /* The destination has multiple samples, we'll do
911 * an MSAA->MSAA copy.
913 if (stype
== TGSI_RETURN_TYPE_UINT
)
914 shader
= &ctx
->fs_texfetch_col_msaa_uint
[target
];
915 else if (stype
== TGSI_RETURN_TYPE_SINT
)
916 shader
= &ctx
->fs_texfetch_col_msaa_sint
[target
];
918 shader
= &ctx
->fs_texfetch_col_msaa
[target
];
920 /* Create the fragment shader on-demand. */
922 assert(!ctx
->cached_all_shaders
);
923 *shader
= util_make_fs_blit_msaa_color(pipe
, tgsi_tex
, stype
);
931 if (stype
== TGSI_RETURN_TYPE_UINT
)
932 shader
= &ctx
->fs_texfetch_col_uint
[target
];
933 else if (stype
== TGSI_RETURN_TYPE_SINT
)
934 shader
= &ctx
->fs_texfetch_col_sint
[target
];
936 shader
= &ctx
->fs_texfetch_col
[target
];
938 /* Create the fragment shader on-demand. */
940 assert(!ctx
->cached_all_shaders
);
941 *shader
= util_make_fragment_tex_shader(pipe
, tgsi_tex
,
942 TGSI_INTERPOLATE_LINEAR
,
951 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
952 enum pipe_texture_target target
,
955 struct pipe_context
*pipe
= ctx
->base
.pipe
;
957 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
959 if (nr_samples
> 1) {
960 void **shader
= &ctx
->fs_texfetch_depth_msaa
[target
];
962 /* Create the fragment shader on-demand. */
965 assert(!ctx
->cached_all_shaders
);
966 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
967 *shader
= util_make_fs_blit_msaa_depth(pipe
, tgsi_tex
);
972 void **shader
= &ctx
->fs_texfetch_depth
[target
];
974 /* Create the fragment shader on-demand. */
977 assert(!ctx
->cached_all_shaders
);
978 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
980 util_make_fragment_tex_shader_writedepth(pipe
, tgsi_tex
,
981 TGSI_INTERPOLATE_LINEAR
);
989 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv
*ctx
,
990 enum pipe_texture_target target
,
993 struct pipe_context
*pipe
= ctx
->base
.pipe
;
995 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
997 if (nr_samples
> 1) {
998 void **shader
= &ctx
->fs_texfetch_depthstencil_msaa
[target
];
1000 /* Create the fragment shader on-demand. */
1003 assert(!ctx
->cached_all_shaders
);
1004 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1005 *shader
= util_make_fs_blit_msaa_depthstencil(pipe
, tgsi_tex
);
1010 void **shader
= &ctx
->fs_texfetch_depthstencil
[target
];
1012 /* Create the fragment shader on-demand. */
1015 assert(!ctx
->cached_all_shaders
);
1016 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1018 util_make_fragment_tex_shader_writedepthstencil(pipe
, tgsi_tex
,
1019 TGSI_INTERPOLATE_LINEAR
);
1027 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv
*ctx
,
1028 enum pipe_texture_target target
,
1029 unsigned nr_samples
)
1031 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1033 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1035 if (nr_samples
> 1) {
1036 void **shader
= &ctx
->fs_texfetch_stencil_msaa
[target
];
1038 /* Create the fragment shader on-demand. */
1041 assert(!ctx
->cached_all_shaders
);
1042 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1043 *shader
= util_make_fs_blit_msaa_stencil(pipe
, tgsi_tex
);
1048 void **shader
= &ctx
->fs_texfetch_stencil
[target
];
1050 /* Create the fragment shader on-demand. */
1053 assert(!ctx
->cached_all_shaders
);
1054 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1056 util_make_fragment_tex_shader_writestencil(pipe
, tgsi_tex
,
1057 TGSI_INTERPOLATE_LINEAR
);
1066 * Generate and save all fragment shaders that we will ever need for
1067 * blitting. Drivers which use the 'draw' fallbacks will typically use
1068 * this to make sure we generate/use shaders that don't go through the
1069 * draw module's wrapper functions.
1071 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
)
1073 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1074 struct pipe_context
*pipe
= blitter
->pipe
;
1075 struct pipe_screen
*screen
= pipe
->screen
;
1076 unsigned samples
, j
, f
, target
, max_samples
;
1077 boolean has_arraytex
, has_cubearraytex
;
1079 max_samples
= ctx
->has_texture_multisample
? 2 : 1;
1080 has_arraytex
= screen
->get_param(screen
,
1081 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
) != 0;
1082 has_cubearraytex
= screen
->get_param(screen
,
1083 PIPE_CAP_CUBE_MAP_ARRAY
) != 0;
1085 /* It only matters if i <= 1 or > 1. */
1086 for (samples
= 1; samples
<= max_samples
; samples
++) {
1087 for (target
= PIPE_TEXTURE_1D
; target
< PIPE_MAX_TEXTURE_TYPES
; target
++) {
1088 if (!has_arraytex
&&
1089 (target
== PIPE_TEXTURE_1D_ARRAY
||
1090 target
== PIPE_TEXTURE_2D_ARRAY
)) {
1093 if (!has_cubearraytex
&&
1094 (target
== PIPE_TEXTURE_CUBE_ARRAY
))
1098 (target
!= PIPE_TEXTURE_2D
&&
1099 target
!= PIPE_TEXTURE_2D_ARRAY
))
1102 /* If samples == 1, the shaders read one texel. If samples >= 1,
1103 * they read one sample.
1105 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
, target
,
1106 samples
, samples
, 0);
1107 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
, target
,
1108 samples
, samples
, 0);
1109 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
, target
,
1110 samples
, samples
, 0);
1111 blitter_get_fs_texfetch_depth(ctx
, target
, samples
);
1112 if (ctx
->has_stencil_export
) {
1113 blitter_get_fs_texfetch_depthstencil(ctx
, target
, samples
);
1114 blitter_get_fs_texfetch_stencil(ctx
, target
, samples
);
1120 /* MSAA resolve shaders. */
1121 for (j
= 2; j
< 32; j
++) {
1122 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R32_FLOAT
,
1124 PIPE_BIND_SAMPLER_VIEW
)) {
1128 for (f
= 0; f
< 2; f
++) {
1129 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
, target
,
1131 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
, target
,
1133 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
, target
,
1140 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
1142 ctx
->fs_write_one_cbuf
=
1143 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1144 TGSI_INTERPOLATE_CONSTANT
, FALSE
);
1146 ctx
->fs_write_all_cbufs
=
1147 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1148 TGSI_INTERPOLATE_CONSTANT
, TRUE
);
1150 ctx
->cached_all_shaders
= TRUE
;
1153 static void blitter_set_common_draw_rect_state(struct blitter_context_priv
*ctx
,
1157 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1159 pipe
->bind_rasterizer_state(pipe
, scissor
? ctx
->rs_state_scissor
1162 bind_vs_layered(ctx
);
1164 bind_vs_passthrough(ctx
);
1166 if (ctx
->has_geometry_shader
)
1167 pipe
->bind_gs_state(pipe
, NULL
);
1168 if (ctx
->has_tessellation
) {
1169 pipe
->bind_tcs_state(pipe
, NULL
);
1170 pipe
->bind_tes_state(pipe
, NULL
);
1172 if (ctx
->has_stream_out
)
1173 pipe
->set_stream_output_targets(pipe
, 0, NULL
, NULL
);
1176 static void blitter_draw(struct blitter_context_priv
*ctx
,
1177 int x1
, int y1
, int x2
, int y2
, float depth
,
1178 unsigned num_instances
)
1180 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1181 struct pipe_vertex_buffer vb
= {0};
1183 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
1185 vb
.stride
= 8 * sizeof(float);
1187 u_upload_data(ctx
->upload
, 0, sizeof(ctx
->vertices
), 4, ctx
->vertices
,
1188 &vb
.buffer_offset
, &vb
.buffer
);
1191 u_upload_unmap(ctx
->upload
);
1193 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1194 util_draw_arrays_instanced(pipe
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
1196 pipe_resource_reference(&vb
.buffer
, NULL
);
1199 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
1200 int x1
, int y1
, int x2
, int y2
, float depth
,
1201 enum blitter_attrib_type type
,
1202 const union pipe_color_union
*attrib
)
1204 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1207 case UTIL_BLITTER_ATTRIB_COLOR
:
1208 blitter_set_clear_color(ctx
, attrib
);
1211 case UTIL_BLITTER_ATTRIB_TEXCOORD
:
1212 set_texcoords_in_vertices(attrib
->f
, &ctx
->vertices
[0][1][0], 8);
1218 blitter_draw(ctx
, x1
, y1
, x2
, y2
, depth
, 1);
1221 static void *get_clear_blend_state(struct blitter_context_priv
*ctx
,
1222 unsigned clear_buffers
)
1224 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1227 clear_buffers
&= PIPE_CLEAR_COLOR
;
1229 /* Return an existing blend state. */
1231 return ctx
->blend
[0][0];
1233 index
= GET_CLEAR_BLEND_STATE_IDX(clear_buffers
);
1235 if (ctx
->blend_clear
[index
])
1236 return ctx
->blend_clear
[index
];
1238 /* Create a new one. */
1240 struct pipe_blend_state blend
= {0};
1243 blend
.independent_blend_enable
= 1;
1245 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1246 if (clear_buffers
& (PIPE_CLEAR_COLOR0
<< i
)) {
1247 blend
.rt
[i
].colormask
= PIPE_MASK_RGBA
;
1251 ctx
->blend_clear
[index
] = pipe
->create_blend_state(pipe
, &blend
);
1253 return ctx
->blend_clear
[index
];
1256 static void util_blitter_clear_custom(struct blitter_context
*blitter
,
1257 unsigned width
, unsigned height
,
1258 unsigned num_layers
,
1259 unsigned clear_buffers
,
1260 const union pipe_color_union
*color
,
1261 double depth
, unsigned stencil
,
1262 void *custom_blend
, void *custom_dsa
)
1264 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1265 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1266 struct pipe_stencil_ref sr
= { { 0 } };
1268 assert(ctx
->has_layered
|| num_layers
<= 1);
1270 blitter_set_running_flag(ctx
);
1271 blitter_check_saved_vertex_states(ctx
);
1272 blitter_check_saved_fragment_states(ctx
);
1273 blitter_disable_render_cond(ctx
);
1277 pipe
->bind_blend_state(pipe
, custom_blend
);
1279 pipe
->bind_blend_state(pipe
, get_clear_blend_state(ctx
, clear_buffers
));
1283 pipe
->bind_depth_stencil_alpha_state(pipe
, custom_dsa
);
1284 } else if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1285 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1286 } else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
1287 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1288 } else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
1289 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1291 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1294 sr
.ref_value
[0] = stencil
& 0xff;
1295 pipe
->set_stencil_ref(pipe
, &sr
);
1297 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1298 bind_fs_write_all_cbufs(ctx
);
1299 pipe
->set_sample_mask(pipe
, ~0);
1301 blitter_set_dst_dimensions(ctx
, width
, height
);
1303 if (num_layers
> 1 && ctx
->has_layered
) {
1304 blitter_set_common_draw_rect_state(ctx
, FALSE
, TRUE
);
1305 blitter_set_clear_color(ctx
, color
);
1306 blitter_draw(ctx
, 0, 0, width
, height
, depth
, num_layers
);
1309 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1310 blitter
->draw_rectangle(blitter
, 0, 0, width
, height
, (float) depth
,
1311 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1314 blitter_restore_vertex_states(ctx
);
1315 blitter_restore_fragment_states(ctx
);
1316 blitter_restore_render_cond(ctx
);
1317 blitter_unset_running_flag(ctx
);
1320 void util_blitter_clear(struct blitter_context
*blitter
,
1321 unsigned width
, unsigned height
, unsigned num_layers
,
1322 unsigned clear_buffers
,
1323 const union pipe_color_union
*color
,
1324 double depth
, unsigned stencil
)
1326 util_blitter_clear_custom(blitter
, width
, height
, num_layers
,
1327 clear_buffers
, color
, depth
, stencil
,
1331 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1332 unsigned width
, unsigned height
,
1333 double depth
, void *custom_dsa
)
1335 static const union pipe_color_union color
;
1336 util_blitter_clear_custom(blitter
, width
, height
, 0, 0, &color
, depth
, 0,
1340 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1341 struct pipe_resource
*dst
,
1345 memset(dst_templ
, 0, sizeof(*dst_templ
));
1346 dst_templ
->format
= util_format_linear(dst
->format
);
1347 dst_templ
->u
.tex
.level
= dstlevel
;
1348 dst_templ
->u
.tex
.first_layer
= dstz
;
1349 dst_templ
->u
.tex
.last_layer
= dstz
;
1352 static struct pipe_surface
*
1353 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
1354 struct pipe_surface
*surf
)
1356 struct pipe_surface dst_templ
;
1358 memset(&dst_templ
, 0, sizeof(dst_templ
));
1359 dst_templ
.format
= surf
->format
;
1360 dst_templ
.u
.tex
.level
= surf
->u
.tex
.level
;
1361 dst_templ
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
+ 1;
1362 dst_templ
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
+ 1;
1364 return pipe
->create_surface(pipe
, surf
->texture
, &dst_templ
);
1367 void util_blitter_default_src_texture(struct pipe_sampler_view
*src_templ
,
1368 struct pipe_resource
*src
,
1371 memset(src_templ
, 0, sizeof(*src_templ
));
1372 src_templ
->target
= src
->target
;
1373 src_templ
->format
= util_format_linear(src
->format
);
1374 src_templ
->u
.tex
.first_level
= srclevel
;
1375 src_templ
->u
.tex
.last_level
= srclevel
;
1376 src_templ
->u
.tex
.first_layer
= 0;
1377 src_templ
->u
.tex
.last_layer
=
1378 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1379 : src
->array_size
- 1;
1380 src_templ
->swizzle_r
= PIPE_SWIZZLE_X
;
1381 src_templ
->swizzle_g
= PIPE_SWIZZLE_Y
;
1382 src_templ
->swizzle_b
= PIPE_SWIZZLE_Z
;
1383 src_templ
->swizzle_a
= PIPE_SWIZZLE_W
;
1386 static boolean
is_blit_generic_supported(struct blitter_context
*blitter
,
1387 const struct pipe_resource
*dst
,
1388 enum pipe_format dst_format
,
1389 const struct pipe_resource
*src
,
1390 enum pipe_format src_format
,
1393 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1394 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1398 const struct util_format_description
*desc
=
1399 util_format_description(dst_format
);
1400 boolean dst_has_stencil
= util_format_has_stencil(desc
);
1402 /* Stencil export must be supported for stencil copy. */
1403 if ((mask
& PIPE_MASK_S
) && dst_has_stencil
&&
1404 !ctx
->has_stencil_export
) {
1408 if (dst_has_stencil
|| util_format_has_depth(desc
))
1409 bind
= PIPE_BIND_DEPTH_STENCIL
;
1411 bind
= PIPE_BIND_RENDER_TARGET
;
1413 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1414 dst
->nr_samples
, bind
)) {
1420 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1424 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1425 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
)) {
1429 /* Check stencil sampler support for stencil copy. */
1430 if (mask
& PIPE_MASK_S
) {
1431 if (util_format_has_stencil(util_format_description(src_format
))) {
1432 enum pipe_format stencil_format
=
1433 util_format_stencil_only(src_format
);
1434 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1436 if (stencil_format
!= src_format
&&
1437 !screen
->is_format_supported(screen
, stencil_format
,
1438 src
->target
, src
->nr_samples
,
1439 PIPE_BIND_SAMPLER_VIEW
)) {
1449 boolean
util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1450 const struct pipe_resource
*dst
,
1451 const struct pipe_resource
*src
)
1453 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1454 src
, src
->format
, PIPE_MASK_RGBAZS
);
1457 boolean
util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1458 const struct pipe_blit_info
*info
)
1460 return is_blit_generic_supported(blitter
,
1461 info
->dst
.resource
, info
->dst
.format
,
1462 info
->src
.resource
, info
->src
.format
,
1466 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1467 struct pipe_resource
*dst
,
1469 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1470 struct pipe_resource
*src
,
1472 const struct pipe_box
*srcbox
)
1474 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1475 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1476 struct pipe_surface
*dst_view
, dst_templ
;
1477 struct pipe_sampler_view src_templ
, *src_view
;
1478 struct pipe_box dstbox
;
1481 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1483 u_box_3d(dstx
, dsty
, dstz
, abs(srcbox
->width
), abs(srcbox
->height
),
1484 abs(srcbox
->depth
), &dstbox
);
1486 /* Initialize the surface. */
1487 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1488 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1490 /* Initialize the sampler view. */
1491 util_blitter_default_src_texture(&src_templ
, src
, src_level
);
1492 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1495 util_blitter_blit_generic(blitter
, dst_view
, &dstbox
,
1496 src_view
, srcbox
, src
->width0
, src
->height0
,
1497 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
,
1500 pipe_surface_reference(&dst_view
, NULL
);
1501 pipe_sampler_view_reference(&src_view
, NULL
);
1504 static void do_blits(struct blitter_context_priv
*ctx
,
1505 struct pipe_surface
*dst
,
1506 const struct pipe_box
*dstbox
,
1507 struct pipe_sampler_view
*src
,
1508 unsigned src_width0
,
1509 unsigned src_height0
,
1510 const struct pipe_box
*srcbox
,
1513 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1514 unsigned src_samples
= src
->texture
->nr_samples
;
1515 unsigned dst_samples
= dst
->texture
->nr_samples
;
1516 enum pipe_texture_target src_target
= src
->texture
->target
;
1517 struct pipe_framebuffer_state fb_state
= {0};
1519 /* Initialize framebuffer state. */
1520 fb_state
.width
= dst
->width
;
1521 fb_state
.height
= dst
->height
;
1522 fb_state
.nr_cbufs
= is_zsbuf
? 0 : 1;
1524 blitter_set_dst_dimensions(ctx
, fb_state
.width
, fb_state
.height
);
1526 if ((src_target
== PIPE_TEXTURE_1D
||
1527 src_target
== PIPE_TEXTURE_2D
||
1528 src_target
== PIPE_TEXTURE_RECT
) &&
1530 /* Draw the quad with the draw_rectangle callback. */
1532 /* Set texture coordinates. - use a pipe color union
1533 * for interface purposes.
1534 * XXX pipe_color_union is a wrong name since we use that to set
1535 * texture coordinates too.
1537 union pipe_color_union coord
;
1538 get_texcoords(src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1539 srcbox
->x
+srcbox
->width
, srcbox
->y
+srcbox
->height
, coord
.f
);
1541 /* Set framebuffer state. */
1543 fb_state
.zsbuf
= dst
;
1545 fb_state
.cbufs
[0] = dst
;
1547 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1550 pipe
->set_sample_mask(pipe
, ~0);
1551 ctx
->base
.draw_rectangle(&ctx
->base
, dstbox
->x
, dstbox
->y
,
1552 dstbox
->x
+ dstbox
->width
,
1553 dstbox
->y
+ dstbox
->height
, 0,
1554 UTIL_BLITTER_ATTRIB_TEXCOORD
, &coord
);
1556 /* Draw the quad with the generic codepath. */
1558 for (dst_z
= 0; dst_z
< dstbox
->depth
; dst_z
++) {
1559 struct pipe_surface
*old
;
1560 float dst2src_scale
= srcbox
->depth
/ (float)dstbox
->depth
;
1562 /* Scale Z properly if the blit is scaled.
1564 * When downscaling, we want the coordinates centered, so that
1565 * mipmapping works for 3D textures. For example, when generating
1566 * a 4x4x4 level, this wouldn't average the pixels:
1568 * src Z: 0 1 2 3 4 5 6 7
1571 * Because the pixels are not centered below the pixels of the higher
1572 * level. Therefore, we want this:
1573 * src Z: 0 1 2 3 4 5 6 7
1576 * dst_offset defines the offset needed for centering the pixels and
1577 * it works with any scaling (not just 2x).
1579 float dst_offset
= ((srcbox
->depth
- 1) -
1580 (dstbox
->depth
- 1) * dst2src_scale
) * 0.5;
1581 float src_z
= (dst_z
+ dst_offset
) * dst2src_scale
;
1583 /* Set framebuffer state. */
1585 fb_state
.zsbuf
= dst
;
1587 fb_state
.cbufs
[0] = dst
;
1589 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1591 /* See if we need to blit a multisample or singlesample buffer. */
1592 if (src_samples
== dst_samples
&& dst_samples
> 1) {
1594 unsigned i
, max_sample
= dst_samples
- 1;
1596 for (i
= 0; i
<= max_sample
; i
++) {
1597 pipe
->set_sample_mask(pipe
, 1 << i
);
1598 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1600 i
, srcbox
->x
, srcbox
->y
,
1601 srcbox
->x
+ srcbox
->width
,
1602 srcbox
->y
+ srcbox
->height
);
1603 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1604 dstbox
->x
+ dstbox
->width
,
1605 dstbox
->y
+ dstbox
->height
, 0, 1);
1608 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1609 pipe
->set_sample_mask(pipe
, ~0);
1610 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1611 srcbox
->z
+ src_z
, 0,
1612 srcbox
->x
, srcbox
->y
,
1613 srcbox
->x
+ srcbox
->width
,
1614 srcbox
->y
+ srcbox
->height
);
1615 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1616 dstbox
->x
+ dstbox
->width
,
1617 dstbox
->y
+ dstbox
->height
, 0, 1);
1620 /* Get the next surface or (if this is the last iteration)
1621 * just unreference the last one. */
1623 if (dst_z
< dstbox
->depth
-1) {
1624 dst
= ctx
->base
.get_next_surface_layer(ctx
->base
.pipe
, dst
);
1627 pipe_surface_reference(&old
, NULL
);
1633 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1634 struct pipe_surface
*dst
,
1635 const struct pipe_box
*dstbox
,
1636 struct pipe_sampler_view
*src
,
1637 const struct pipe_box
*srcbox
,
1638 unsigned src_width0
, unsigned src_height0
,
1639 unsigned mask
, unsigned filter
,
1640 const struct pipe_scissor_state
*scissor
,
1641 boolean alpha_blend
)
1643 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1644 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1645 enum pipe_texture_target src_target
= src
->texture
->target
;
1646 unsigned src_samples
= src
->texture
->nr_samples
;
1647 unsigned dst_samples
= dst
->texture
->nr_samples
;
1648 boolean has_depth
, has_stencil
, has_color
;
1649 boolean blit_stencil
, blit_depth
, blit_color
;
1650 void *sampler_state
;
1651 const struct util_format_description
*src_desc
=
1652 util_format_description(src
->format
);
1653 const struct util_format_description
*dst_desc
=
1654 util_format_description(dst
->format
);
1656 has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
&&
1657 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1658 has_depth
= util_format_has_depth(src_desc
) &&
1659 util_format_has_depth(dst_desc
);
1660 has_stencil
= util_format_has_stencil(src_desc
) &&
1661 util_format_has_stencil(dst_desc
);
1663 blit_color
= has_color
&& (mask
& PIPE_MASK_RGBA
);
1664 blit_depth
= has_depth
&& (mask
& PIPE_MASK_Z
);
1665 blit_stencil
= has_stencil
&& (mask
& PIPE_MASK_S
) &&
1666 ctx
->has_stencil_export
;
1668 if (!blit_stencil
&& !blit_depth
&& !blit_color
) {
1673 (dstbox
->width
== abs(srcbox
->width
) &&
1674 dstbox
->height
== abs(srcbox
->height
))) {
1675 filter
= PIPE_TEX_FILTER_NEAREST
;
1678 /* Check whether the states are properly saved. */
1679 blitter_set_running_flag(ctx
);
1680 blitter_check_saved_vertex_states(ctx
);
1681 blitter_check_saved_fragment_states(ctx
);
1682 blitter_check_saved_textures(ctx
);
1683 blitter_check_saved_fb_state(ctx
);
1684 blitter_disable_render_cond(ctx
);
1686 if (blit_depth
|| blit_stencil
) {
1687 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
1689 if (blit_depth
&& blit_stencil
) {
1690 pipe
->bind_depth_stencil_alpha_state(pipe
,
1691 ctx
->dsa_write_depth_stencil
);
1692 ctx
->bind_fs_state(pipe
,
1693 blitter_get_fs_texfetch_depthstencil(ctx
, src_target
,
1695 } else if (blit_depth
) {
1696 pipe
->bind_depth_stencil_alpha_state(pipe
,
1697 ctx
->dsa_write_depth_keep_stencil
);
1698 ctx
->bind_fs_state(pipe
,
1699 blitter_get_fs_texfetch_depth(ctx
, src_target
,
1701 } else { /* is_stencil */
1702 pipe
->bind_depth_stencil_alpha_state(pipe
,
1703 ctx
->dsa_keep_depth_write_stencil
);
1704 ctx
->bind_fs_state(pipe
,
1705 blitter_get_fs_texfetch_stencil(ctx
, src_target
,
1710 unsigned colormask
= mask
& PIPE_MASK_RGBA
;
1712 pipe
->bind_blend_state(pipe
, ctx
->blend
[colormask
][alpha_blend
]);
1713 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1714 ctx
->bind_fs_state(pipe
,
1715 blitter_get_fs_texfetch_col(ctx
, src
->format
, src_target
,
1716 src_samples
, dst_samples
, filter
));
1719 /* Set the linear filter only for scaled color non-MSAA blits. */
1720 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
1721 if (src_target
== PIPE_TEXTURE_RECT
) {
1722 sampler_state
= ctx
->sampler_state_rect_linear
;
1724 sampler_state
= ctx
->sampler_state_linear
;
1727 if (src_target
== PIPE_TEXTURE_RECT
) {
1728 sampler_state
= ctx
->sampler_state_rect
;
1730 sampler_state
= ctx
->sampler_state
;
1735 if (blit_depth
&& blit_stencil
) {
1736 /* Setup two samplers, one for depth and the other one for stencil. */
1737 struct pipe_sampler_view templ
;
1738 struct pipe_sampler_view
*views
[2];
1739 void *samplers
[2] = {sampler_state
, sampler_state
};
1742 templ
.format
= util_format_stencil_only(templ
.format
);
1743 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1746 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1748 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, views
);
1749 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, samplers
);
1751 pipe_sampler_view_reference(&views
[1], NULL
);
1752 } else if (blit_stencil
) {
1753 /* Set a stencil-only sampler view for it not to sample depth instead. */
1754 struct pipe_sampler_view templ
;
1755 struct pipe_sampler_view
*view
;
1758 templ
.format
= util_format_stencil_only(templ
.format
);
1759 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1761 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1763 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &view
);
1764 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1765 0, 1, &sampler_state
);
1767 pipe_sampler_view_reference(&view
, NULL
);
1769 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src
);
1770 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1771 0, 1, &sampler_state
);
1774 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1776 pipe
->set_scissor_states(pipe
, 0, 1, scissor
);
1779 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
, FALSE
);
1781 do_blits(ctx
, dst
, dstbox
, src
, src_width0
, src_height0
,
1782 srcbox
, blit_depth
|| blit_stencil
);
1784 blitter_restore_vertex_states(ctx
);
1785 blitter_restore_fragment_states(ctx
);
1786 blitter_restore_textures(ctx
);
1787 blitter_restore_fb_state(ctx
);
1789 pipe
->set_scissor_states(pipe
, 0, 1, &ctx
->base
.saved_scissor
);
1791 blitter_restore_render_cond(ctx
);
1792 blitter_unset_running_flag(ctx
);
1796 util_blitter_blit(struct blitter_context
*blitter
,
1797 const struct pipe_blit_info
*info
)
1799 struct pipe_resource
*dst
= info
->dst
.resource
;
1800 struct pipe_resource
*src
= info
->src
.resource
;
1801 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1802 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1803 struct pipe_surface
*dst_view
, dst_templ
;
1804 struct pipe_sampler_view src_templ
, *src_view
;
1806 /* Initialize the surface. */
1807 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
1809 dst_templ
.format
= info
->dst
.format
;
1810 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1812 /* Initialize the sampler view. */
1813 util_blitter_default_src_texture(&src_templ
, src
, info
->src
.level
);
1814 src_templ
.format
= info
->src
.format
;
1815 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1818 util_blitter_blit_generic(blitter
, dst_view
, &info
->dst
.box
,
1819 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
1820 info
->mask
, info
->filter
,
1821 info
->scissor_enable
? &info
->scissor
: NULL
,
1824 pipe_surface_reference(&dst_view
, NULL
);
1825 pipe_sampler_view_reference(&src_view
, NULL
);
1828 void util_blitter_generate_mipmap(struct blitter_context
*blitter
,
1829 struct pipe_resource
*tex
,
1830 enum pipe_format format
,
1831 unsigned base_level
, unsigned last_level
,
1832 unsigned first_layer
, unsigned last_layer
)
1834 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1835 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1836 struct pipe_surface dst_templ
, *dst_view
;
1837 struct pipe_sampler_view src_templ
, *src_view
;
1839 void *sampler_state
;
1840 const struct util_format_description
*desc
=
1841 util_format_description(format
);
1844 assert(tex
->nr_samples
<= 1);
1845 assert(!util_format_has_stencil(desc
));
1847 is_depth
= desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
;
1849 /* Check whether the states are properly saved. */
1850 blitter_set_running_flag(ctx
);
1851 blitter_check_saved_vertex_states(ctx
);
1852 blitter_check_saved_fragment_states(ctx
);
1853 blitter_check_saved_textures(ctx
);
1854 blitter_check_saved_fb_state(ctx
);
1855 blitter_disable_render_cond(ctx
);
1859 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
1860 pipe
->bind_depth_stencil_alpha_state(pipe
,
1861 ctx
->dsa_write_depth_keep_stencil
);
1862 ctx
->bind_fs_state(pipe
,
1863 blitter_get_fs_texfetch_depth(ctx
, tex
->target
, 1));
1865 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
1866 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1867 ctx
->bind_fs_state(pipe
,
1868 blitter_get_fs_texfetch_col(ctx
, tex
->format
, tex
->target
,
1869 1, 1, PIPE_TEX_FILTER_LINEAR
));
1872 if (tex
->target
== PIPE_TEXTURE_RECT
) {
1873 sampler_state
= ctx
->sampler_state_rect_linear
;
1875 sampler_state
= ctx
->sampler_state_linear
;
1877 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1878 0, 1, &sampler_state
);
1880 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1881 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1883 for (src_level
= base_level
; src_level
< last_level
; src_level
++) {
1884 struct pipe_box dstbox
= {0}, srcbox
= {0};
1885 unsigned dst_level
= src_level
+ 1;
1887 dstbox
.width
= u_minify(tex
->width0
, dst_level
);
1888 dstbox
.height
= u_minify(tex
->height0
, dst_level
);
1890 srcbox
.width
= u_minify(tex
->width0
, src_level
);
1891 srcbox
.height
= u_minify(tex
->height0
, src_level
);
1893 if (tex
->target
== PIPE_TEXTURE_3D
) {
1894 dstbox
.depth
= util_max_layer(tex
, dst_level
) + 1;
1895 srcbox
.depth
= util_max_layer(tex
, src_level
) + 1;
1897 dstbox
.z
= srcbox
.z
= first_layer
;
1898 dstbox
.depth
= srcbox
.depth
= last_layer
- first_layer
+ 1;
1901 /* Initialize the surface. */
1902 util_blitter_default_dst_texture(&dst_templ
, tex
, dst_level
,
1904 dst_templ
.format
= format
;
1905 dst_view
= pipe
->create_surface(pipe
, tex
, &dst_templ
);
1907 /* Initialize the sampler view. */
1908 util_blitter_default_src_texture(&src_templ
, tex
, src_level
);
1909 src_templ
.format
= format
;
1910 src_view
= pipe
->create_sampler_view(pipe
, tex
, &src_templ
);
1912 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src_view
);
1914 do_blits(ctx
, dst_view
, &dstbox
, src_view
, tex
->width0
, tex
->height0
,
1917 pipe_surface_reference(&dst_view
, NULL
);
1918 pipe_sampler_view_reference(&src_view
, NULL
);
1921 blitter_restore_vertex_states(ctx
);
1922 blitter_restore_fragment_states(ctx
);
1923 blitter_restore_textures(ctx
);
1924 blitter_restore_fb_state(ctx
);
1925 blitter_restore_render_cond(ctx
);
1926 blitter_unset_running_flag(ctx
);
1929 /* Clear a region of a color surface to a constant value. */
1930 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
1931 struct pipe_surface
*dstsurf
,
1932 const union pipe_color_union
*color
,
1933 unsigned dstx
, unsigned dsty
,
1934 unsigned width
, unsigned height
)
1936 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1937 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1938 struct pipe_framebuffer_state fb_state
;
1939 unsigned num_layers
;
1941 assert(dstsurf
->texture
);
1942 if (!dstsurf
->texture
)
1945 /* check the saved state */
1946 blitter_set_running_flag(ctx
);
1947 blitter_check_saved_vertex_states(ctx
);
1948 blitter_check_saved_fragment_states(ctx
);
1949 blitter_check_saved_fb_state(ctx
);
1950 blitter_disable_render_cond(ctx
);
1953 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
1954 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1955 bind_fs_write_one_cbuf(ctx
);
1956 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1958 /* set a framebuffer state */
1959 fb_state
.width
= dstsurf
->width
;
1960 fb_state
.height
= dstsurf
->height
;
1961 fb_state
.nr_cbufs
= 1;
1962 fb_state
.cbufs
[0] = dstsurf
;
1964 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1965 pipe
->set_sample_mask(pipe
, ~0);
1967 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1969 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
1970 if (num_layers
> 1 && ctx
->has_layered
) {
1971 blitter_set_common_draw_rect_state(ctx
, FALSE
, TRUE
);
1972 blitter_set_clear_color(ctx
, color
);
1973 blitter_draw(ctx
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0, num_layers
);
1976 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1977 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
1978 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1981 blitter_restore_vertex_states(ctx
);
1982 blitter_restore_fragment_states(ctx
);
1983 blitter_restore_fb_state(ctx
);
1984 blitter_restore_render_cond(ctx
);
1985 blitter_unset_running_flag(ctx
);
1988 /* Clear a region of a depth stencil surface. */
1989 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
1990 struct pipe_surface
*dstsurf
,
1991 unsigned clear_flags
,
1994 unsigned dstx
, unsigned dsty
,
1995 unsigned width
, unsigned height
)
1997 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1998 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1999 struct pipe_framebuffer_state fb_state
;
2000 struct pipe_stencil_ref sr
= { { 0 } };
2001 unsigned num_layers
;
2003 assert(dstsurf
->texture
);
2004 if (!dstsurf
->texture
)
2007 /* check the saved state */
2008 blitter_set_running_flag(ctx
);
2009 blitter_check_saved_vertex_states(ctx
);
2010 blitter_check_saved_fragment_states(ctx
);
2011 blitter_check_saved_fb_state(ctx
);
2012 blitter_disable_render_cond(ctx
);
2015 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
2016 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
2017 sr
.ref_value
[0] = stencil
& 0xff;
2018 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
2019 pipe
->set_stencil_ref(pipe
, &sr
);
2021 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
2022 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
2024 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
2025 sr
.ref_value
[0] = stencil
& 0xff;
2026 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
2027 pipe
->set_stencil_ref(pipe
, &sr
);
2030 /* hmm that should be illegal probably, or make it a no-op somewhere */
2031 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2034 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2036 /* set a framebuffer state */
2037 fb_state
.width
= dstsurf
->width
;
2038 fb_state
.height
= dstsurf
->height
;
2039 fb_state
.nr_cbufs
= 0;
2040 fb_state
.cbufs
[0] = 0;
2041 fb_state
.zsbuf
= dstsurf
;
2042 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2043 pipe
->set_sample_mask(pipe
, ~0);
2045 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2047 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
2048 if (num_layers
> 1 && ctx
->has_layered
) {
2049 blitter_set_common_draw_rect_state(ctx
, FALSE
, TRUE
);
2050 blitter_draw(ctx
, dstx
, dsty
, dstx
+width
, dsty
+height
, (float) depth
, num_layers
);
2053 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2054 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
,
2056 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2059 blitter_restore_vertex_states(ctx
);
2060 blitter_restore_fragment_states(ctx
);
2061 blitter_restore_fb_state(ctx
);
2062 blitter_restore_render_cond(ctx
);
2063 blitter_unset_running_flag(ctx
);
2066 /* draw a rectangle across a region using a custom dsa stage - for r600g */
2067 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
2068 struct pipe_surface
*zsurf
,
2069 struct pipe_surface
*cbsurf
,
2070 unsigned sample_mask
,
2071 void *dsa_stage
, float depth
)
2073 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2074 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2075 struct pipe_framebuffer_state fb_state
;
2077 assert(zsurf
->texture
);
2078 if (!zsurf
->texture
)
2081 /* check the saved state */
2082 blitter_set_running_flag(ctx
);
2083 blitter_check_saved_vertex_states(ctx
);
2084 blitter_check_saved_fragment_states(ctx
);
2085 blitter_check_saved_fb_state(ctx
);
2086 blitter_disable_render_cond(ctx
);
2089 pipe
->bind_blend_state(pipe
, cbsurf
? ctx
->blend
[PIPE_MASK_RGBA
][0] :
2091 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
2093 bind_fs_write_one_cbuf(ctx
);
2096 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2098 /* set a framebuffer state */
2099 fb_state
.width
= zsurf
->width
;
2100 fb_state
.height
= zsurf
->height
;
2101 fb_state
.nr_cbufs
= 1;
2103 fb_state
.cbufs
[0] = cbsurf
;
2104 fb_state
.nr_cbufs
= 1;
2106 fb_state
.cbufs
[0] = NULL
;
2107 fb_state
.nr_cbufs
= 0;
2109 fb_state
.zsbuf
= zsurf
;
2110 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2111 pipe
->set_sample_mask(pipe
, sample_mask
);
2113 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2114 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
2115 blitter
->draw_rectangle(blitter
, 0, 0, zsurf
->width
, zsurf
->height
, depth
,
2116 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2118 blitter_restore_vertex_states(ctx
);
2119 blitter_restore_fragment_states(ctx
);
2120 blitter_restore_fb_state(ctx
);
2121 blitter_restore_render_cond(ctx
);
2122 blitter_unset_running_flag(ctx
);
2125 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
2126 struct pipe_resource
*dst
,
2128 struct pipe_resource
*src
,
2132 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2133 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2134 struct pipe_vertex_buffer vb
;
2135 struct pipe_stream_output_target
*so_target
;
2136 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2138 if (srcx
>= src
->width0
||
2139 dstx
>= dst
->width0
) {
2142 if (srcx
+ size
> src
->width0
) {
2143 size
= src
->width0
- srcx
;
2145 if (dstx
+ size
> dst
->width0
) {
2146 size
= dst
->width0
- dstx
;
2149 /* Drivers not capable of Stream Out should not call this function
2150 * in the first place. */
2151 assert(ctx
->has_stream_out
);
2153 /* Some alignment is required. */
2154 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
2155 !ctx
->has_stream_out
) {
2156 struct pipe_box box
;
2157 u_box_1d(srcx
, size
, &box
);
2158 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
2162 blitter_set_running_flag(ctx
);
2163 blitter_check_saved_vertex_states(ctx
);
2164 blitter_disable_render_cond(ctx
);
2167 vb
.buffer_offset
= srcx
;
2170 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2171 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[0]);
2172 bind_vs_pos_only(ctx
, 1);
2173 if (ctx
->has_geometry_shader
)
2174 pipe
->bind_gs_state(pipe
, NULL
);
2175 if (ctx
->has_tessellation
) {
2176 pipe
->bind_tcs_state(pipe
, NULL
);
2177 pipe
->bind_tes_state(pipe
, NULL
);
2179 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2181 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
2182 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2184 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2186 blitter_restore_vertex_states(ctx
);
2187 blitter_restore_render_cond(ctx
);
2188 blitter_unset_running_flag(ctx
);
2189 pipe_so_target_reference(&so_target
, NULL
);
2192 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
2193 struct pipe_resource
*dst
,
2194 unsigned offset
, unsigned size
,
2195 unsigned num_channels
,
2196 const union pipe_color_union
*clear_value
)
2198 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2199 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2200 struct pipe_vertex_buffer vb
= {0};
2201 struct pipe_stream_output_target
*so_target
= NULL
;
2202 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2204 assert(num_channels
>= 1);
2205 assert(num_channels
<= 4);
2207 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
2209 * R600 uses this to initialize texture resources, so width0 might not be
2210 * what you think it is.
2213 /* Streamout is required. */
2214 if (!ctx
->has_stream_out
) {
2215 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2219 /* Some alignment is required. */
2220 if (offset
% 4 != 0 || size
% 4 != 0) {
2221 assert(!"Bad alignment in util_blitter_clear_buffer()");
2225 u_upload_data(ctx
->upload
, 0, num_channels
*4, 4, clear_value
,
2226 &vb
.buffer_offset
, &vb
.buffer
);
2232 blitter_set_running_flag(ctx
);
2233 blitter_check_saved_vertex_states(ctx
);
2234 blitter_disable_render_cond(ctx
);
2236 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2237 pipe
->bind_vertex_elements_state(pipe
,
2238 ctx
->velem_state_readbuf
[num_channels
-1]);
2239 bind_vs_pos_only(ctx
, num_channels
);
2240 if (ctx
->has_geometry_shader
)
2241 pipe
->bind_gs_state(pipe
, NULL
);
2242 if (ctx
->has_tessellation
) {
2243 pipe
->bind_tcs_state(pipe
, NULL
);
2244 pipe
->bind_tes_state(pipe
, NULL
);
2246 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2248 so_target
= pipe
->create_stream_output_target(pipe
, dst
, offset
, size
);
2249 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2251 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2254 blitter_restore_vertex_states(ctx
);
2255 blitter_restore_render_cond(ctx
);
2256 blitter_unset_running_flag(ctx
);
2257 pipe_so_target_reference(&so_target
, NULL
);
2258 pipe_resource_reference(&vb
.buffer
, NULL
);
2261 /* probably radeon specific */
2262 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
2263 struct pipe_resource
*dst
,
2266 struct pipe_resource
*src
,
2268 unsigned sample_mask
,
2270 enum pipe_format format
)
2272 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2273 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2274 struct pipe_framebuffer_state fb_state
;
2275 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
2277 blitter_set_running_flag(ctx
);
2278 blitter_check_saved_vertex_states(ctx
);
2279 blitter_check_saved_fragment_states(ctx
);
2280 blitter_disable_render_cond(ctx
);
2283 pipe
->bind_blend_state(pipe
, custom_blend
);
2284 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2285 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2286 bind_fs_write_one_cbuf(ctx
);
2287 pipe
->set_sample_mask(pipe
, sample_mask
);
2289 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
2290 surf_tmpl
.format
= format
;
2291 surf_tmpl
.u
.tex
.level
= dst_level
;
2292 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
2293 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
2295 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
2297 surf_tmpl
.u
.tex
.level
= 0;
2298 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
2299 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
2301 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
2303 /* set a framebuffer state */
2304 fb_state
.width
= src
->width0
;
2305 fb_state
.height
= src
->height0
;
2306 fb_state
.nr_cbufs
= 2;
2307 fb_state
.cbufs
[0] = srcsurf
;
2308 fb_state
.cbufs
[1] = dstsurf
;
2309 fb_state
.zsbuf
= NULL
;
2310 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2312 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2313 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
2314 blitter
->draw_rectangle(blitter
, 0, 0, src
->width0
, src
->height0
,
2316 blitter_restore_fb_state(ctx
);
2317 blitter_restore_vertex_states(ctx
);
2318 blitter_restore_fragment_states(ctx
);
2319 blitter_restore_render_cond(ctx
);
2320 blitter_unset_running_flag(ctx
);
2322 pipe_surface_reference(&srcsurf
, NULL
);
2323 pipe_surface_reference(&dstsurf
, NULL
);
2326 void util_blitter_custom_color(struct blitter_context
*blitter
,
2327 struct pipe_surface
*dstsurf
,
2330 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2331 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2332 struct pipe_framebuffer_state fb_state
;
2334 assert(dstsurf
->texture
);
2335 if (!dstsurf
->texture
)
2338 /* check the saved state */
2339 blitter_set_running_flag(ctx
);
2340 blitter_check_saved_vertex_states(ctx
);
2341 blitter_check_saved_fragment_states(ctx
);
2342 blitter_check_saved_fb_state(ctx
);
2343 blitter_disable_render_cond(ctx
);
2346 pipe
->bind_blend_state(pipe
, custom_blend
? custom_blend
2347 : ctx
->blend
[PIPE_MASK_RGBA
][0]);
2348 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2349 bind_fs_write_one_cbuf(ctx
);
2350 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2351 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2353 /* set a framebuffer state */
2354 fb_state
.width
= dstsurf
->width
;
2355 fb_state
.height
= dstsurf
->height
;
2356 fb_state
.nr_cbufs
= 1;
2357 fb_state
.cbufs
[0] = dstsurf
;
2359 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2360 pipe
->set_sample_mask(pipe
, ~0);
2362 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2363 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2364 blitter
->draw_rectangle(blitter
, 0, 0, dstsurf
->width
, dstsurf
->height
,
2367 blitter_restore_vertex_states(ctx
);
2368 blitter_restore_fragment_states(ctx
);
2369 blitter_restore_fb_state(ctx
);
2370 blitter_restore_render_cond(ctx
);
2371 blitter_unset_running_flag(ctx
);