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/format/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 float vertices
[4][2][4]; /**< {pos, color} or {pos, texcoord} */
66 /* Templates for various state objects. */
68 /* Constant state objects. */
70 void *vs
; /**< Vertex shader which passes {pos, generic} to the output.*/
72 void *vs_pos_only
[4]; /**< Vertex shader which passes pos to the output
73 for clear_buffer/copy_buffer.*/
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 where
82 * the 1st index indicates the texture type / destination type,
83 * the 2nd index is the PIPE_TEXTURE_* to be sampled,
84 * the 3rd index is 0 = use TEX, 1 = use TXF.
86 void *fs_texfetch_col
[5][PIPE_MAX_TEXTURE_TYPES
][2];
88 /* FS which outputs a depth from a texture, where
89 * the 1st index is the PIPE_TEXTURE_* to be sampled,
90 * the 2nd index is 0 = use TEX, 1 = use TXF.
92 void *fs_texfetch_depth
[PIPE_MAX_TEXTURE_TYPES
][2];
93 void *fs_texfetch_depthstencil
[PIPE_MAX_TEXTURE_TYPES
][2];
94 void *fs_texfetch_stencil
[PIPE_MAX_TEXTURE_TYPES
][2];
96 /* FS which outputs one sample from a multisample texture. */
97 void *fs_texfetch_col_msaa
[5][PIPE_MAX_TEXTURE_TYPES
];
98 void *fs_texfetch_depth_msaa
[PIPE_MAX_TEXTURE_TYPES
];
99 void *fs_texfetch_depthstencil_msaa
[PIPE_MAX_TEXTURE_TYPES
];
100 void *fs_texfetch_stencil_msaa
[PIPE_MAX_TEXTURE_TYPES
];
102 /* FS which outputs an average of all samples. */
103 void *fs_resolve
[PIPE_MAX_TEXTURE_TYPES
][NUM_RESOLVE_FRAG_SHADERS
][2];
105 /* FS which unpacks color to ZS or packs ZS to color, matching
106 * the ZS format. See util_blitter_get_color_format_for_zs().
108 void *fs_pack_color_zs
[TGSI_TEXTURE_COUNT
][10];
111 void *blend
[PIPE_MASK_RGBA
+1][2]; /**< blend state with writemask */
112 void *blend_clear
[GET_CLEAR_BLEND_STATE_IDX(PIPE_CLEAR_COLOR
)+1];
114 /* Depth stencil alpha state. */
115 void *dsa_write_depth_stencil
;
116 void *dsa_write_depth_keep_stencil
;
117 void *dsa_keep_depth_stencil
;
118 void *dsa_keep_depth_write_stencil
;
120 /* Vertex elements states. */
122 void *velem_state_readbuf
[4]; /**< X, XY, XYZ, XYZW */
126 void *sampler_state_linear
;
127 void *sampler_state_rect
;
128 void *sampler_state_rect_linear
;
130 /* Rasterizer state. */
131 void *rs_state
[2][2]; /**< [scissor][msaa] */
132 void *rs_discard_state
;
134 /* Destination surface dimensions. */
140 bool has_geometry_shader
;
141 bool has_tessellation
;
144 bool has_stencil_export
;
145 bool has_texture_multisample
;
148 bool cube_as_2darray
;
149 bool cached_all_shaders
;
151 /* The Draw module overrides these functions.
152 * Always create the blitter before Draw. */
153 void (*bind_fs_state
)(struct pipe_context
*, void *);
154 void (*delete_fs_state
)(struct pipe_context
*, void *);
157 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
)
159 struct blitter_context_priv
*ctx
;
160 struct pipe_blend_state blend
;
161 struct pipe_depth_stencil_alpha_state dsa
;
162 struct pipe_rasterizer_state rs_state
;
163 struct pipe_sampler_state sampler_state
;
164 struct pipe_vertex_element velem
[2];
167 ctx
= CALLOC_STRUCT(blitter_context_priv
);
171 ctx
->base
.pipe
= pipe
;
172 ctx
->base
.draw_rectangle
= util_blitter_draw_rectangle
;
174 ctx
->bind_fs_state
= pipe
->bind_fs_state
;
175 ctx
->delete_fs_state
= pipe
->delete_fs_state
;
177 /* init state objects for them to be considered invalid */
178 ctx
->base
.saved_blend_state
= INVALID_PTR
;
179 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
180 ctx
->base
.saved_rs_state
= INVALID_PTR
;
181 ctx
->base
.saved_fs
= INVALID_PTR
;
182 ctx
->base
.saved_vs
= INVALID_PTR
;
183 ctx
->base
.saved_gs
= INVALID_PTR
;
184 ctx
->base
.saved_velem_state
= INVALID_PTR
;
185 ctx
->base
.saved_fb_state
.nr_cbufs
= ~0;
186 ctx
->base
.saved_num_sampler_views
= ~0;
187 ctx
->base
.saved_num_sampler_states
= ~0;
188 ctx
->base
.saved_num_so_targets
= ~0;
190 ctx
->has_geometry_shader
=
191 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
192 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
194 ctx
->has_tessellation
=
195 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_TESS_CTRL
,
196 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
198 ctx
->has_stream_out
=
199 pipe
->screen
->get_param(pipe
->screen
,
200 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0;
202 ctx
->has_stencil_export
=
203 pipe
->screen
->get_param(pipe
->screen
,
204 PIPE_CAP_SHADER_STENCIL_EXPORT
);
206 ctx
->has_texture_multisample
=
207 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TEXTURE_MULTISAMPLE
);
209 ctx
->has_tex_lz
= pipe
->screen
->get_param(pipe
->screen
,
210 PIPE_CAP_TGSI_TEX_TXF_LZ
);
211 ctx
->has_txf
= pipe
->screen
->get_param(pipe
->screen
,
212 PIPE_CAP_GLSL_FEATURE_LEVEL
) > 130;
213 ctx
->cube_as_2darray
= pipe
->screen
->get_param(pipe
->screen
,
214 PIPE_CAP_SAMPLER_VIEW_TARGET
);
216 /* blend state objects */
217 memset(&blend
, 0, sizeof(blend
));
219 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
220 for (j
= 0; j
< 2; j
++) {
221 memset(&blend
.rt
[0], 0, sizeof(blend
.rt
[0]));
222 blend
.rt
[0].colormask
= i
;
224 blend
.rt
[0].blend_enable
= 1;
225 blend
.rt
[0].rgb_func
= PIPE_BLEND_ADD
;
226 blend
.rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_SRC_ALPHA
;
227 blend
.rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
228 blend
.rt
[0].alpha_func
= PIPE_BLEND_ADD
;
229 blend
.rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_SRC_ALPHA
;
230 blend
.rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
232 ctx
->blend
[i
][j
] = pipe
->create_blend_state(pipe
, &blend
);
236 /* depth stencil alpha state objects */
237 memset(&dsa
, 0, sizeof(dsa
));
238 ctx
->dsa_keep_depth_stencil
=
239 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
241 dsa
.depth
.enabled
= 1;
242 dsa
.depth
.writemask
= 1;
243 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
244 ctx
->dsa_write_depth_keep_stencil
=
245 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
247 dsa
.stencil
[0].enabled
= 1;
248 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
249 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
250 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
251 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
252 dsa
.stencil
[0].valuemask
= 0xff;
253 dsa
.stencil
[0].writemask
= 0xff;
254 ctx
->dsa_write_depth_stencil
=
255 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
257 dsa
.depth
.enabled
= 0;
258 dsa
.depth
.writemask
= 0;
259 ctx
->dsa_keep_depth_write_stencil
=
260 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
263 memset(&sampler_state
, 0, sizeof(sampler_state
));
264 sampler_state
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
265 sampler_state
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
266 sampler_state
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
267 sampler_state
.normalized_coords
= 1;
268 ctx
->sampler_state
= pipe
->create_sampler_state(pipe
, &sampler_state
);
269 sampler_state
.normalized_coords
= 0;
270 ctx
->sampler_state_rect
= pipe
->create_sampler_state(pipe
, &sampler_state
);
272 sampler_state
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
273 sampler_state
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
274 sampler_state
.normalized_coords
= 1;
275 ctx
->sampler_state_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
276 sampler_state
.normalized_coords
= 0;
277 ctx
->sampler_state_rect_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
279 /* rasterizer state */
280 memset(&rs_state
, 0, sizeof(rs_state
));
281 rs_state
.cull_face
= PIPE_FACE_NONE
;
282 rs_state
.half_pixel_center
= 1;
283 rs_state
.bottom_edge_rule
= 1;
284 rs_state
.flatshade
= 1;
285 rs_state
.depth_clip_near
= 1;
286 rs_state
.depth_clip_far
= 1;
288 unsigned scissor
, msaa
;
289 for (scissor
= 0; scissor
< 2; scissor
++) {
290 for (msaa
= 0; msaa
< 2; msaa
++) {
291 rs_state
.scissor
= scissor
;
292 rs_state
.multisample
= msaa
;
293 ctx
->rs_state
[scissor
][msaa
] =
294 pipe
->create_rasterizer_state(pipe
, &rs_state
);
298 if (ctx
->has_stream_out
) {
299 rs_state
.scissor
= rs_state
.multisample
= 0;
300 rs_state
.rasterizer_discard
= 1;
301 ctx
->rs_discard_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
304 ctx
->base
.cb_slot
= 0; /* 0 for now */
305 ctx
->base
.vb_slot
= 0; /* 0 for now */
307 /* vertex elements states */
308 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
309 for (i
= 0; i
< 2; i
++) {
310 velem
[i
].src_offset
= i
* 4 * sizeof(float);
311 velem
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
312 velem
[i
].vertex_buffer_index
= ctx
->base
.vb_slot
;
314 ctx
->velem_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
316 if (ctx
->has_stream_out
) {
317 static enum pipe_format formats
[4] = {
318 PIPE_FORMAT_R32_UINT
,
319 PIPE_FORMAT_R32G32_UINT
,
320 PIPE_FORMAT_R32G32B32_UINT
,
321 PIPE_FORMAT_R32G32B32A32_UINT
324 for (i
= 0; i
< 4; i
++) {
325 velem
[0].src_format
= formats
[i
];
326 velem
[0].vertex_buffer_index
= ctx
->base
.vb_slot
;
327 ctx
->velem_state_readbuf
[i
] =
328 pipe
->create_vertex_elements_state(pipe
, 1, &velem
[0]);
333 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_INSTANCEID
) &&
334 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT
);
336 /* set invariant vertex coordinates */
337 for (i
= 0; i
< 4; i
++) {
338 ctx
->vertices
[i
][0][2] = 0; /*v.z*/
339 ctx
->vertices
[i
][0][3] = 1; /*v.w*/
345 void *util_blitter_get_noop_blend_state(struct blitter_context
*blitter
)
347 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
349 return ctx
->blend
[0][0];
352 void *util_blitter_get_noop_dsa_state(struct blitter_context
*blitter
)
354 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
356 return ctx
->dsa_keep_depth_stencil
;
359 void *util_blitter_get_discard_rasterizer_state(struct blitter_context
*blitter
)
361 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
363 return ctx
->rs_discard_state
;
366 static void bind_vs_pos_only(struct blitter_context_priv
*ctx
,
367 unsigned num_so_channels
)
369 struct pipe_context
*pipe
= ctx
->base
.pipe
;
370 int index
= num_so_channels
? num_so_channels
- 1 : 0;
372 if (!ctx
->vs_pos_only
[index
]) {
373 struct pipe_stream_output_info so
;
374 static const enum tgsi_semantic semantic_names
[] =
375 { TGSI_SEMANTIC_POSITION
};
376 const uint semantic_indices
[] = { 0 };
378 memset(&so
, 0, sizeof(so
));
380 so
.output
[0].num_components
= num_so_channels
;
381 so
.stride
[0] = num_so_channels
;
383 ctx
->vs_pos_only
[index
] =
384 util_make_vertex_passthrough_shader_with_so(pipe
, 1, semantic_names
,
385 semantic_indices
, false,
389 pipe
->bind_vs_state(pipe
, ctx
->vs_pos_only
[index
]);
392 static void *get_vs_passthrough_pos_generic(struct blitter_context
*blitter
)
394 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
395 struct pipe_context
*pipe
= ctx
->base
.pipe
;
398 static const enum tgsi_semantic semantic_names
[] =
399 { TGSI_SEMANTIC_POSITION
, TGSI_SEMANTIC_GENERIC
};
400 const uint semantic_indices
[] = { 0, 0 };
402 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
403 semantic_indices
, false);
408 static void *get_vs_passthrough_pos(struct blitter_context
*blitter
)
410 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
411 struct pipe_context
*pipe
= ctx
->base
.pipe
;
413 if (!ctx
->vs_nogeneric
) {
414 static const enum tgsi_semantic semantic_names
[] =
415 { TGSI_SEMANTIC_POSITION
};
416 const uint semantic_indices
[] = { 0 };
419 util_make_vertex_passthrough_shader(pipe
, 1,
421 semantic_indices
, false);
423 return ctx
->vs_nogeneric
;
426 static void *get_vs_layered(struct blitter_context
*blitter
)
428 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
429 struct pipe_context
*pipe
= ctx
->base
.pipe
;
431 if (!ctx
->vs_layered
) {
432 ctx
->vs_layered
= util_make_layered_clear_vertex_shader(pipe
);
434 return ctx
->vs_layered
;
437 static void bind_fs_empty(struct blitter_context_priv
*ctx
)
439 struct pipe_context
*pipe
= ctx
->base
.pipe
;
441 if (!ctx
->fs_empty
) {
442 assert(!ctx
->cached_all_shaders
);
443 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
446 ctx
->bind_fs_state(pipe
, ctx
->fs_empty
);
449 static void bind_fs_write_one_cbuf(struct blitter_context_priv
*ctx
)
451 struct pipe_context
*pipe
= ctx
->base
.pipe
;
453 if (!ctx
->fs_write_one_cbuf
) {
454 assert(!ctx
->cached_all_shaders
);
455 ctx
->fs_write_one_cbuf
=
456 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
457 TGSI_INTERPOLATE_CONSTANT
, false);
460 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
463 static void bind_fs_write_all_cbufs(struct blitter_context_priv
*ctx
)
465 struct pipe_context
*pipe
= ctx
->base
.pipe
;
467 if (!ctx
->fs_write_all_cbufs
) {
468 assert(!ctx
->cached_all_shaders
);
469 ctx
->fs_write_all_cbufs
=
470 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
471 TGSI_INTERPOLATE_CONSTANT
, true);
474 ctx
->bind_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
477 void util_blitter_destroy(struct blitter_context
*blitter
)
479 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
480 struct pipe_context
*pipe
= blitter
->pipe
;
483 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++)
484 for (j
= 0; j
< 2; j
++)
485 pipe
->delete_blend_state(pipe
, ctx
->blend
[i
][j
]);
487 for (i
= 0; i
< ARRAY_SIZE(ctx
->blend_clear
); i
++) {
488 if (ctx
->blend_clear
[i
])
489 pipe
->delete_blend_state(pipe
, ctx
->blend_clear
[i
]);
491 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
492 pipe
->delete_depth_stencil_alpha_state(pipe
,
493 ctx
->dsa_write_depth_keep_stencil
);
494 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
495 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
497 unsigned scissor
, msaa
;
498 for (scissor
= 0; scissor
< 2; scissor
++) {
499 for (msaa
= 0; msaa
< 2; msaa
++) {
500 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
[scissor
][msaa
]);
504 if (ctx
->rs_discard_state
)
505 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_discard_state
);
507 pipe
->delete_vs_state(pipe
, ctx
->vs
);
508 if (ctx
->vs_nogeneric
)
509 pipe
->delete_vs_state(pipe
, ctx
->vs_nogeneric
);
510 for (i
= 0; i
< 4; i
++)
511 if (ctx
->vs_pos_only
[i
])
512 pipe
->delete_vs_state(pipe
, ctx
->vs_pos_only
[i
]);
514 pipe
->delete_vs_state(pipe
, ctx
->vs_layered
);
515 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state
);
516 for (i
= 0; i
< 4; i
++) {
517 if (ctx
->velem_state_readbuf
[i
]) {
518 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[i
]);
522 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
523 for (unsigned type
= 0; type
< ARRAY_SIZE(ctx
->fs_texfetch_col
); ++type
) {
524 for (unsigned inst
= 0; inst
< 2; inst
++) {
525 if (ctx
->fs_texfetch_col
[type
][i
][inst
])
526 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[type
][i
][inst
]);
528 if (ctx
->fs_texfetch_col_msaa
[type
][i
])
529 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col_msaa
[type
][i
]);
532 for (unsigned inst
= 0; inst
< 2; inst
++) {
533 if (ctx
->fs_texfetch_depth
[i
][inst
])
534 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
][inst
]);
535 if (ctx
->fs_texfetch_depthstencil
[i
][inst
])
536 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil
[i
][inst
]);
537 if (ctx
->fs_texfetch_stencil
[i
][inst
])
538 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil
[i
][inst
]);
541 if (ctx
->fs_texfetch_depth_msaa
[i
])
542 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth_msaa
[i
]);
543 if (ctx
->fs_texfetch_depthstencil_msaa
[i
])
544 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil_msaa
[i
]);
545 if (ctx
->fs_texfetch_stencil_msaa
[i
])
546 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil_msaa
[i
]);
548 for (j
= 0; j
< ARRAY_SIZE(ctx
->fs_resolve
[i
]); j
++)
549 for (f
= 0; f
< 2; f
++)
550 if (ctx
->fs_resolve
[i
][j
][f
])
551 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve
[i
][j
][f
]);
554 for (i
= 0; i
< ARRAY_SIZE(ctx
->fs_pack_color_zs
); i
++) {
555 for (j
= 0; j
< ARRAY_SIZE(ctx
->fs_pack_color_zs
[0]); j
++) {
556 if (ctx
->fs_pack_color_zs
[i
][j
])
557 ctx
->delete_fs_state(pipe
, ctx
->fs_pack_color_zs
[i
][j
]);
562 ctx
->delete_fs_state(pipe
, ctx
->fs_empty
);
563 if (ctx
->fs_write_one_cbuf
)
564 ctx
->delete_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
565 if (ctx
->fs_write_all_cbufs
)
566 ctx
->delete_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
568 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect_linear
);
569 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect
);
570 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_linear
);
571 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
);
575 void util_blitter_set_texture_multisample(struct blitter_context
*blitter
,
578 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
580 ctx
->has_texture_multisample
= supported
;
583 void util_blitter_set_running_flag(struct blitter_context
*blitter
)
585 if (blitter
->running
) {
586 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
589 blitter
->running
= true;
591 blitter
->pipe
->set_active_query_state(blitter
->pipe
, false);
594 void util_blitter_unset_running_flag(struct blitter_context
*blitter
)
596 if (!blitter
->running
) {
597 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
600 blitter
->running
= false;
602 blitter
->pipe
->set_active_query_state(blitter
->pipe
, true);
605 static void blitter_check_saved_vertex_states(ASSERTED
struct blitter_context_priv
*ctx
)
607 assert(ctx
->base
.saved_vs
!= INVALID_PTR
);
608 assert(!ctx
->has_geometry_shader
|| ctx
->base
.saved_gs
!= INVALID_PTR
);
609 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tcs
!= INVALID_PTR
);
610 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tes
!= INVALID_PTR
);
611 assert(!ctx
->has_stream_out
|| ctx
->base
.saved_num_so_targets
!= ~0u);
612 assert(ctx
->base
.saved_rs_state
!= INVALID_PTR
);
615 void util_blitter_restore_vertex_states(struct blitter_context
*blitter
)
617 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
618 struct pipe_context
*pipe
= ctx
->base
.pipe
;
622 if (ctx
->base
.saved_vertex_buffer
.buffer
.resource
) {
623 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1,
624 &ctx
->base
.saved_vertex_buffer
);
625 pipe_vertex_buffer_unreference(&ctx
->base
.saved_vertex_buffer
);
628 /* Vertex elements. */
629 if (ctx
->base
.saved_velem_state
!= INVALID_PTR
) {
630 pipe
->bind_vertex_elements_state(pipe
, ctx
->base
.saved_velem_state
);
631 ctx
->base
.saved_velem_state
= INVALID_PTR
;
635 pipe
->bind_vs_state(pipe
, ctx
->base
.saved_vs
);
636 ctx
->base
.saved_vs
= INVALID_PTR
;
638 /* Geometry shader. */
639 if (ctx
->has_geometry_shader
) {
640 pipe
->bind_gs_state(pipe
, ctx
->base
.saved_gs
);
641 ctx
->base
.saved_gs
= INVALID_PTR
;
644 if (ctx
->has_tessellation
) {
645 pipe
->bind_tcs_state(pipe
, ctx
->base
.saved_tcs
);
646 pipe
->bind_tes_state(pipe
, ctx
->base
.saved_tes
);
647 ctx
->base
.saved_tcs
= INVALID_PTR
;
648 ctx
->base
.saved_tes
= INVALID_PTR
;
651 /* Stream outputs. */
652 if (ctx
->has_stream_out
) {
653 unsigned offsets
[PIPE_MAX_SO_BUFFERS
];
654 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
655 offsets
[i
] = (unsigned)-1;
656 pipe
->set_stream_output_targets(pipe
,
657 ctx
->base
.saved_num_so_targets
,
658 ctx
->base
.saved_so_targets
, offsets
);
660 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
661 pipe_so_target_reference(&ctx
->base
.saved_so_targets
[i
], NULL
);
663 ctx
->base
.saved_num_so_targets
= ~0;
667 pipe
->bind_rasterizer_state(pipe
, ctx
->base
.saved_rs_state
);
668 ctx
->base
.saved_rs_state
= INVALID_PTR
;
671 static void blitter_check_saved_fragment_states(ASSERTED
struct blitter_context_priv
*ctx
)
673 assert(ctx
->base
.saved_fs
!= INVALID_PTR
);
674 assert(ctx
->base
.saved_dsa_state
!= INVALID_PTR
);
675 assert(ctx
->base
.saved_blend_state
!= INVALID_PTR
);
678 void util_blitter_restore_fragment_states(struct blitter_context
*blitter
)
680 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
681 struct pipe_context
*pipe
= ctx
->base
.pipe
;
683 /* Fragment shader. */
684 ctx
->bind_fs_state(pipe
, ctx
->base
.saved_fs
);
685 ctx
->base
.saved_fs
= INVALID_PTR
;
687 /* Depth, stencil, alpha. */
688 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->base
.saved_dsa_state
);
689 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
692 pipe
->bind_blend_state(pipe
, ctx
->base
.saved_blend_state
);
693 ctx
->base
.saved_blend_state
= INVALID_PTR
;
696 if (ctx
->base
.is_sample_mask_saved
) {
697 pipe
->set_sample_mask(pipe
, ctx
->base
.saved_sample_mask
);
698 ctx
->base
.is_sample_mask_saved
= false;
701 /* Miscellaneous states. */
702 /* XXX check whether these are saved and whether they need to be restored
703 * (depending on the operation) */
704 pipe
->set_stencil_ref(pipe
, &ctx
->base
.saved_stencil_ref
);
706 if (!blitter
->skip_viewport_restore
)
707 pipe
->set_viewport_states(pipe
, 0, 1, &ctx
->base
.saved_viewport
);
709 if (blitter
->saved_num_window_rectangles
) {
710 pipe
->set_window_rectangles(pipe
,
711 blitter
->saved_window_rectangles_include
,
712 blitter
->saved_num_window_rectangles
,
713 blitter
->saved_window_rectangles
);
717 static void blitter_check_saved_fb_state(ASSERTED
struct blitter_context_priv
*ctx
)
719 assert(ctx
->base
.saved_fb_state
.nr_cbufs
!= (ubyte
) ~0);
722 static void blitter_disable_render_cond(struct blitter_context_priv
*ctx
)
724 struct pipe_context
*pipe
= ctx
->base
.pipe
;
726 if (ctx
->base
.saved_render_cond_query
) {
727 pipe
->render_condition(pipe
, NULL
, false, 0);
731 void util_blitter_restore_render_cond(struct blitter_context
*blitter
)
733 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
734 struct pipe_context
*pipe
= ctx
->base
.pipe
;
736 if (ctx
->base
.saved_render_cond_query
) {
737 pipe
->render_condition(pipe
, ctx
->base
.saved_render_cond_query
,
738 ctx
->base
.saved_render_cond_cond
,
739 ctx
->base
.saved_render_cond_mode
);
740 ctx
->base
.saved_render_cond_query
= NULL
;
744 void util_blitter_restore_fb_state(struct blitter_context
*blitter
)
746 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
747 struct pipe_context
*pipe
= ctx
->base
.pipe
;
749 pipe
->set_framebuffer_state(pipe
, &ctx
->base
.saved_fb_state
);
750 util_unreference_framebuffer_state(&ctx
->base
.saved_fb_state
);
753 static void blitter_check_saved_textures(ASSERTED
struct blitter_context_priv
*ctx
)
755 assert(ctx
->base
.saved_num_sampler_states
!= ~0u);
756 assert(ctx
->base
.saved_num_sampler_views
!= ~0u);
759 void util_blitter_restore_textures(struct blitter_context
*blitter
)
761 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
762 struct pipe_context
*pipe
= ctx
->base
.pipe
;
765 /* Fragment sampler states. */
766 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0,
767 ctx
->base
.saved_num_sampler_states
,
768 ctx
->base
.saved_sampler_states
);
770 ctx
->base
.saved_num_sampler_states
= ~0;
772 /* Fragment sampler views. */
773 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
774 ctx
->base
.saved_num_sampler_views
,
775 ctx
->base
.saved_sampler_views
);
777 for (i
= 0; i
< ctx
->base
.saved_num_sampler_views
; i
++)
778 pipe_sampler_view_reference(&ctx
->base
.saved_sampler_views
[i
], NULL
);
780 ctx
->base
.saved_num_sampler_views
= ~0;
783 void util_blitter_restore_constant_buffer_state(struct blitter_context
*blitter
)
785 struct pipe_context
*pipe
= blitter
->pipe
;
787 pipe
->set_constant_buffer(pipe
, PIPE_SHADER_FRAGMENT
, blitter
->cb_slot
,
788 &blitter
->saved_fs_constant_buffer
);
789 pipe_resource_reference(&blitter
->saved_fs_constant_buffer
.buffer
, NULL
);
792 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
793 int x1
, int y1
, int x2
, int y2
,
796 /* set vertex positions */
797 ctx
->vertices
[0][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v0.x*/
798 ctx
->vertices
[0][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v0.y*/
800 ctx
->vertices
[1][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v1.x*/
801 ctx
->vertices
[1][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v1.y*/
803 ctx
->vertices
[2][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v2.x*/
804 ctx
->vertices
[2][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v2.y*/
806 ctx
->vertices
[3][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v3.x*/
807 ctx
->vertices
[3][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v3.y*/
810 struct pipe_viewport_state viewport
;
811 viewport
.scale
[0] = 0.5f
* ctx
->dst_width
;
812 viewport
.scale
[1] = 0.5f
* ctx
->dst_height
;
813 viewport
.scale
[2] = 0.0f
;
814 viewport
.translate
[0] = 0.5f
* ctx
->dst_width
;
815 viewport
.translate
[1] = 0.5f
* ctx
->dst_height
;
816 viewport
.translate
[2] = depth
;
817 ctx
->base
.pipe
->set_viewport_states(ctx
->base
.pipe
, 0, 1, &viewport
);
820 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
821 const float color
[4])
826 for (i
= 0; i
< 4; i
++)
827 memcpy(&ctx
->vertices
[i
][1][0], color
, sizeof(uint32_t) * 4);
829 for (i
= 0; i
< 4; i
++)
830 memset(&ctx
->vertices
[i
][1][0], 0, sizeof(uint32_t) * 4);
834 static void get_texcoords(struct pipe_sampler_view
*src
,
835 unsigned src_width0
, unsigned src_height0
,
836 int x1
, int y1
, int x2
, int y2
,
837 float layer
, unsigned sample
,
838 bool uses_txf
, union blitter_attrib
*out
)
840 unsigned level
= src
->u
.tex
.first_level
;
841 bool normalized
= !uses_txf
&&
842 src
->target
!= PIPE_TEXTURE_RECT
&&
843 src
->texture
->nr_samples
<= 1;
846 out
->texcoord
.x1
= x1
/ (float)u_minify(src_width0
, level
);
847 out
->texcoord
.y1
= y1
/ (float)u_minify(src_height0
, level
);
848 out
->texcoord
.x2
= x2
/ (float)u_minify(src_width0
, level
);
849 out
->texcoord
.y2
= y2
/ (float)u_minify(src_height0
, level
);
851 out
->texcoord
.x1
= x1
;
852 out
->texcoord
.y1
= y1
;
853 out
->texcoord
.x2
= x2
;
854 out
->texcoord
.y2
= y2
;
861 switch (src
->target
) {
862 case PIPE_TEXTURE_3D
:
867 r
/= u_minify(src
->texture
->depth0
, src
->u
.tex
.first_level
);
873 case PIPE_TEXTURE_1D_ARRAY
:
874 out
->texcoord
.y1
= out
->texcoord
.y2
= layer
;
877 case PIPE_TEXTURE_2D_ARRAY
:
878 out
->texcoord
.z
= layer
;
879 out
->texcoord
.w
= sample
;
882 case PIPE_TEXTURE_CUBE_ARRAY
:
883 out
->texcoord
.w
= (unsigned)layer
/ 6;
886 case PIPE_TEXTURE_2D
:
887 out
->texcoord
.w
= sample
;
894 static void blitter_set_dst_dimensions(struct blitter_context_priv
*ctx
,
895 unsigned width
, unsigned height
)
897 ctx
->dst_width
= width
;
898 ctx
->dst_height
= height
;
901 static void set_texcoords_in_vertices(const union blitter_attrib
*attrib
,
902 float *out
, unsigned stride
)
904 out
[0] = attrib
->texcoord
.x1
;
905 out
[1] = attrib
->texcoord
.y1
;
907 out
[0] = attrib
->texcoord
.x2
;
908 out
[1] = attrib
->texcoord
.y1
;
910 out
[0] = attrib
->texcoord
.x2
;
911 out
[1] = attrib
->texcoord
.y2
;
913 out
[0] = attrib
->texcoord
.x1
;
914 out
[1] = attrib
->texcoord
.y2
;
917 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
918 enum pipe_format src_format
,
919 enum pipe_format dst_format
,
920 enum pipe_texture_target target
,
921 unsigned src_nr_samples
,
922 unsigned dst_nr_samples
,
926 struct pipe_context
*pipe
= ctx
->base
.pipe
;
927 enum tgsi_texture_type tgsi_tex
=
928 util_pipe_tex_to_tgsi_tex(target
, src_nr_samples
);
929 enum tgsi_return_type stype
;
930 enum tgsi_return_type dtype
;
933 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
935 if (util_format_is_pure_uint(src_format
)) {
936 stype
= TGSI_RETURN_TYPE_UINT
;
937 if (util_format_is_pure_uint(dst_format
)) {
938 dtype
= TGSI_RETURN_TYPE_UINT
;
941 assert(util_format_is_pure_sint(dst_format
));
942 dtype
= TGSI_RETURN_TYPE_SINT
;
945 } else if (util_format_is_pure_sint(src_format
)) {
946 stype
= TGSI_RETURN_TYPE_SINT
;
947 if (util_format_is_pure_sint(dst_format
)) {
948 dtype
= TGSI_RETURN_TYPE_SINT
;
951 assert(util_format_is_pure_uint(dst_format
));
952 dtype
= TGSI_RETURN_TYPE_UINT
;
956 assert(!util_format_is_pure_uint(dst_format
) &&
957 !util_format_is_pure_sint(dst_format
));
958 dtype
= stype
= TGSI_RETURN_TYPE_FLOAT
;
962 if (src_nr_samples
> 1) {
965 /* OpenGL requires that integer textures just copy 1 sample instead
968 if (dst_nr_samples
<= 1 &&
969 stype
!= TGSI_RETURN_TYPE_UINT
&&
970 stype
!= TGSI_RETURN_TYPE_SINT
) {
971 /* The destination has one sample, so we'll do color resolve. */
972 unsigned index
= GET_MSAA_RESOLVE_FS_IDX(src_nr_samples
);
976 shader
= &ctx
->fs_resolve
[target
][index
][filter
];
979 assert(!ctx
->cached_all_shaders
);
980 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
981 *shader
= util_make_fs_msaa_resolve_bilinear(pipe
, tgsi_tex
,
986 *shader
= util_make_fs_msaa_resolve(pipe
, tgsi_tex
,
993 /* The destination has multiple samples, we'll do
994 * an MSAA->MSAA copy.
996 shader
= &ctx
->fs_texfetch_col_msaa
[type
][target
];
998 /* Create the fragment shader on-demand. */
1000 assert(!ctx
->cached_all_shaders
);
1001 *shader
= util_make_fs_blit_msaa_color(pipe
, tgsi_tex
, stype
, dtype
);
1010 shader
= &ctx
->fs_texfetch_col
[type
][target
][1];
1012 shader
= &ctx
->fs_texfetch_col
[type
][target
][0];
1014 /* Create the fragment shader on-demand. */
1016 assert(!ctx
->cached_all_shaders
);
1017 *shader
= util_make_fragment_tex_shader(pipe
, tgsi_tex
,
1018 TGSI_INTERPOLATE_LINEAR
,
1020 ctx
->has_tex_lz
, use_txf
);
1028 void *blitter_get_fs_pack_color_zs(struct blitter_context_priv
*ctx
,
1029 enum pipe_texture_target target
,
1030 unsigned nr_samples
,
1031 enum pipe_format zs_format
,
1034 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1035 enum tgsi_texture_type tgsi_tex
=
1036 util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1037 int format_index
= zs_format
== PIPE_FORMAT_Z24_UNORM_S8_UINT
? 0 :
1038 zs_format
== PIPE_FORMAT_S8_UINT_Z24_UNORM
? 1 :
1039 zs_format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
? 2 :
1040 zs_format
== PIPE_FORMAT_Z24X8_UNORM
? 3 :
1041 zs_format
== PIPE_FORMAT_X8Z24_UNORM
? 4 : -1;
1043 if (format_index
== -1) {
1048 /* The first 5 shaders pack ZS to color, the last 5 shaders unpack color
1054 void **shader
= &ctx
->fs_pack_color_zs
[tgsi_tex
][format_index
];
1056 /* Create the fragment shader on-demand. */
1058 assert(!ctx
->cached_all_shaders
);
1059 *shader
= util_make_fs_pack_color_zs(pipe
, tgsi_tex
, zs_format
,
1066 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
1067 enum pipe_texture_target target
,
1068 unsigned nr_samples
,
1071 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1073 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1075 if (nr_samples
> 1) {
1076 void **shader
= &ctx
->fs_texfetch_depth_msaa
[target
];
1078 /* Create the fragment shader on-demand. */
1080 enum tgsi_texture_type tgsi_tex
;
1081 assert(!ctx
->cached_all_shaders
);
1082 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1083 *shader
= util_make_fs_blit_msaa_depth(pipe
, tgsi_tex
);
1091 shader
= &ctx
->fs_texfetch_depth
[target
][1];
1093 shader
= &ctx
->fs_texfetch_depth
[target
][0];
1095 /* Create the fragment shader on-demand. */
1097 enum tgsi_texture_type tgsi_tex
;
1098 assert(!ctx
->cached_all_shaders
);
1099 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1100 *shader
= util_make_fs_blit_zs(pipe
, PIPE_MASK_Z
, tgsi_tex
,
1101 ctx
->has_tex_lz
, use_txf
);
1109 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv
*ctx
,
1110 enum pipe_texture_target target
,
1111 unsigned nr_samples
,
1114 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1116 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1118 if (nr_samples
> 1) {
1119 void **shader
= &ctx
->fs_texfetch_depthstencil_msaa
[target
];
1121 /* Create the fragment shader on-demand. */
1123 enum tgsi_texture_type tgsi_tex
;
1124 assert(!ctx
->cached_all_shaders
);
1125 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1126 *shader
= util_make_fs_blit_msaa_depthstencil(pipe
, tgsi_tex
);
1134 shader
= &ctx
->fs_texfetch_depthstencil
[target
][1];
1136 shader
= &ctx
->fs_texfetch_depthstencil
[target
][0];
1138 /* Create the fragment shader on-demand. */
1140 enum tgsi_texture_type tgsi_tex
;
1141 assert(!ctx
->cached_all_shaders
);
1142 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1143 *shader
= util_make_fs_blit_zs(pipe
, PIPE_MASK_ZS
, tgsi_tex
,
1144 ctx
->has_tex_lz
, use_txf
);
1152 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv
*ctx
,
1153 enum pipe_texture_target target
,
1154 unsigned nr_samples
,
1157 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1159 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1161 if (nr_samples
> 1) {
1162 void **shader
= &ctx
->fs_texfetch_stencil_msaa
[target
];
1164 /* Create the fragment shader on-demand. */
1166 enum tgsi_texture_type tgsi_tex
;
1167 assert(!ctx
->cached_all_shaders
);
1168 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1169 *shader
= util_make_fs_blit_msaa_stencil(pipe
, tgsi_tex
);
1177 shader
= &ctx
->fs_texfetch_stencil
[target
][1];
1179 shader
= &ctx
->fs_texfetch_stencil
[target
][0];
1181 /* Create the fragment shader on-demand. */
1183 enum tgsi_texture_type tgsi_tex
;
1184 assert(!ctx
->cached_all_shaders
);
1185 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1186 *shader
= util_make_fs_blit_zs(pipe
, PIPE_MASK_S
, tgsi_tex
,
1187 ctx
->has_tex_lz
, use_txf
);
1196 * Generate and save all fragment shaders that we will ever need for
1197 * blitting. Drivers which use the 'draw' fallbacks will typically use
1198 * this to make sure we generate/use shaders that don't go through the
1199 * draw module's wrapper functions.
1201 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
)
1203 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1204 struct pipe_context
*pipe
= blitter
->pipe
;
1205 struct pipe_screen
*screen
= pipe
->screen
;
1206 unsigned samples
, j
, f
, target
, max_samples
, use_txf
;
1207 bool has_arraytex
, has_cubearraytex
;
1209 max_samples
= ctx
->has_texture_multisample
? 2 : 1;
1210 has_arraytex
= screen
->get_param(screen
,
1211 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
) != 0;
1212 has_cubearraytex
= screen
->get_param(screen
,
1213 PIPE_CAP_CUBE_MAP_ARRAY
) != 0;
1215 /* It only matters if i <= 1 or > 1. */
1216 for (samples
= 1; samples
<= max_samples
; samples
++) {
1217 for (target
= PIPE_TEXTURE_1D
; target
< PIPE_MAX_TEXTURE_TYPES
; target
++) {
1218 for (use_txf
= 0; use_txf
<= ctx
->has_txf
; use_txf
++) {
1219 if (!has_arraytex
&&
1220 (target
== PIPE_TEXTURE_1D_ARRAY
||
1221 target
== PIPE_TEXTURE_2D_ARRAY
)) {
1224 if (!has_cubearraytex
&&
1225 (target
== PIPE_TEXTURE_CUBE_ARRAY
))
1229 (target
!= PIPE_TEXTURE_2D
&&
1230 target
!= PIPE_TEXTURE_2D_ARRAY
))
1233 if (samples
> 1 && use_txf
)
1234 continue; /* TXF is the only option, use_txf has no effect */
1236 /* If samples == 1, the shaders read one texel. If samples >= 1,
1237 * they read one sample.
1239 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
,
1240 PIPE_FORMAT_R32_FLOAT
, target
,
1241 samples
, samples
, 0, use_txf
);
1242 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1243 PIPE_FORMAT_R32_UINT
, target
,
1244 samples
, samples
, 0, use_txf
);
1245 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1246 PIPE_FORMAT_R32_SINT
, target
,
1247 samples
, samples
, 0, use_txf
);
1248 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1249 PIPE_FORMAT_R32_SINT
, target
,
1250 samples
, samples
, 0, use_txf
);
1251 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1252 PIPE_FORMAT_R32_UINT
, target
,
1253 samples
, samples
, 0, use_txf
);
1254 blitter_get_fs_texfetch_depth(ctx
, target
, samples
, use_txf
);
1255 if (ctx
->has_stencil_export
) {
1256 blitter_get_fs_texfetch_depthstencil(ctx
, target
, samples
, use_txf
);
1257 blitter_get_fs_texfetch_stencil(ctx
, target
, samples
, use_txf
);
1263 /* MSAA resolve shaders. */
1264 for (j
= 2; j
< 32; j
++) {
1265 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R32_FLOAT
,
1267 PIPE_BIND_SAMPLER_VIEW
)) {
1271 for (f
= 0; f
< 2; f
++) {
1272 if (f
!= PIPE_TEX_FILTER_NEAREST
&& use_txf
)
1275 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
,
1276 PIPE_FORMAT_R32_FLOAT
, target
,
1278 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1279 PIPE_FORMAT_R32_UINT
, target
,
1281 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1282 PIPE_FORMAT_R32_SINT
, target
,
1290 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
1292 ctx
->fs_write_one_cbuf
=
1293 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1294 TGSI_INTERPOLATE_CONSTANT
, false);
1296 ctx
->fs_write_all_cbufs
=
1297 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1298 TGSI_INTERPOLATE_CONSTANT
, true);
1300 ctx
->cached_all_shaders
= true;
1303 static void blitter_set_common_draw_rect_state(struct blitter_context_priv
*ctx
,
1304 bool scissor
, bool msaa
)
1306 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1308 if (ctx
->base
.saved_num_window_rectangles
)
1309 pipe
->set_window_rectangles(pipe
, false, 0, NULL
);
1311 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_state
[scissor
][msaa
]);
1313 if (ctx
->has_geometry_shader
)
1314 pipe
->bind_gs_state(pipe
, NULL
);
1315 if (ctx
->has_tessellation
) {
1316 pipe
->bind_tcs_state(pipe
, NULL
);
1317 pipe
->bind_tes_state(pipe
, NULL
);
1319 if (ctx
->has_stream_out
)
1320 pipe
->set_stream_output_targets(pipe
, 0, NULL
, NULL
);
1323 static void blitter_draw(struct blitter_context_priv
*ctx
,
1324 void *vertex_elements_cso
,
1325 blitter_get_vs_func get_vs
,
1326 int x1
, int y1
, int x2
, int y2
, float depth
,
1327 unsigned num_instances
)
1329 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1330 struct pipe_vertex_buffer vb
= {0};
1332 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
1334 vb
.stride
= 8 * sizeof(float);
1336 u_upload_data(pipe
->stream_uploader
, 0, sizeof(ctx
->vertices
), 4, ctx
->vertices
,
1337 &vb
.buffer_offset
, &vb
.buffer
.resource
);
1338 if (!vb
.buffer
.resource
)
1340 u_upload_unmap(pipe
->stream_uploader
);
1342 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1343 pipe
->bind_vertex_elements_state(pipe
, vertex_elements_cso
);
1344 pipe
->bind_vs_state(pipe
, get_vs(&ctx
->base
));
1346 if (ctx
->base
.use_index_buffer
) {
1347 /* Note that for V3D,
1348 * dEQP-GLES3.functional.fbo.blit.rect.nearest_consistency_* require
1349 * that the last vert of the two tris be the same.
1351 static uint8_t indices
[6] = { 0, 1, 2, 0, 3, 2 };
1352 util_draw_elements_instanced(pipe
, indices
, 1, 0,
1353 PIPE_PRIM_TRIANGLES
, 0, 6,
1356 util_draw_arrays_instanced(pipe
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
1359 pipe_resource_reference(&vb
.buffer
.resource
, NULL
);
1362 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
1363 void *vertex_elements_cso
,
1364 blitter_get_vs_func get_vs
,
1365 int x1
, int y1
, int x2
, int y2
,
1366 float depth
, unsigned num_instances
,
1367 enum blitter_attrib_type type
,
1368 const union blitter_attrib
*attrib
)
1370 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1374 case UTIL_BLITTER_ATTRIB_COLOR
:
1375 blitter_set_clear_color(ctx
, attrib
->color
);
1378 case UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW
:
1379 for (i
= 0; i
< 4; i
++) {
1380 ctx
->vertices
[i
][1][2] = attrib
->texcoord
.z
;
1381 ctx
->vertices
[i
][1][3] = attrib
->texcoord
.w
;
1384 case UTIL_BLITTER_ATTRIB_TEXCOORD_XY
:
1385 set_texcoords_in_vertices(attrib
, &ctx
->vertices
[0][1][0], 8);
1391 blitter_draw(ctx
, vertex_elements_cso
, get_vs
, x1
, y1
, x2
, y2
, depth
,
1395 static void *get_clear_blend_state(struct blitter_context_priv
*ctx
,
1396 unsigned clear_buffers
)
1398 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1401 clear_buffers
&= PIPE_CLEAR_COLOR
;
1403 /* Return an existing blend state. */
1405 return ctx
->blend
[0][0];
1407 index
= GET_CLEAR_BLEND_STATE_IDX(clear_buffers
);
1409 if (ctx
->blend_clear
[index
])
1410 return ctx
->blend_clear
[index
];
1412 /* Create a new one. */
1414 struct pipe_blend_state blend
= {0};
1417 blend
.independent_blend_enable
= 1;
1419 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1420 if (clear_buffers
& (PIPE_CLEAR_COLOR0
<< i
)) {
1421 blend
.rt
[i
].colormask
= PIPE_MASK_RGBA
;
1426 ctx
->blend_clear
[index
] = pipe
->create_blend_state(pipe
, &blend
);
1428 return ctx
->blend_clear
[index
];
1431 void util_blitter_common_clear_setup(struct blitter_context
*blitter
,
1432 unsigned width
, unsigned height
,
1433 unsigned clear_buffers
,
1434 void *custom_blend
, void *custom_dsa
)
1436 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1437 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1439 util_blitter_set_running_flag(blitter
);
1440 blitter_check_saved_vertex_states(ctx
);
1441 blitter_check_saved_fragment_states(ctx
);
1442 blitter_disable_render_cond(ctx
);
1446 pipe
->bind_blend_state(pipe
, custom_blend
);
1448 pipe
->bind_blend_state(pipe
, get_clear_blend_state(ctx
, clear_buffers
));
1452 pipe
->bind_depth_stencil_alpha_state(pipe
, custom_dsa
);
1453 } else if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1454 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1455 } else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
1456 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1457 } else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
1458 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1460 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1463 pipe
->set_sample_mask(pipe
, ~0);
1464 blitter_set_dst_dimensions(ctx
, width
, height
);
1467 static void util_blitter_clear_custom(struct blitter_context
*blitter
,
1468 unsigned width
, unsigned height
,
1469 unsigned num_layers
,
1470 unsigned clear_buffers
,
1471 const union pipe_color_union
*color
,
1472 double depth
, unsigned stencil
,
1473 void *custom_blend
, void *custom_dsa
,
1476 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1477 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1478 struct pipe_stencil_ref sr
= { { 0 } };
1480 assert(ctx
->has_layered
|| num_layers
<= 1);
1482 util_blitter_common_clear_setup(blitter
, width
, height
, clear_buffers
,
1483 custom_blend
, custom_dsa
);
1485 sr
.ref_value
[0] = stencil
& 0xff;
1486 pipe
->set_stencil_ref(pipe
, &sr
);
1488 bind_fs_write_all_cbufs(ctx
);
1490 union blitter_attrib attrib
;
1491 memcpy(attrib
.color
, color
->ui
, sizeof(color
->ui
));
1493 bool pass_generic
= (clear_buffers
& PIPE_CLEAR_COLOR
) != 0;
1494 enum blitter_attrib_type type
= pass_generic
? UTIL_BLITTER_ATTRIB_COLOR
:
1495 UTIL_BLITTER_ATTRIB_NONE
;
1497 if (num_layers
> 1 && ctx
->has_layered
) {
1498 blitter_get_vs_func get_vs
= get_vs_layered
;
1500 blitter_set_common_draw_rect_state(ctx
, false, msaa
);
1501 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs
,
1502 0, 0, width
, height
,
1503 (float) depth
, num_layers
, type
, &attrib
);
1505 blitter_get_vs_func get_vs
;
1508 get_vs
= get_vs_passthrough_pos_generic
;
1510 get_vs
= get_vs_passthrough_pos
;
1512 blitter_set_common_draw_rect_state(ctx
, false, msaa
);
1513 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs
,
1514 0, 0, width
, height
,
1515 (float) depth
, 1, type
, &attrib
);
1518 util_blitter_restore_vertex_states(blitter
);
1519 util_blitter_restore_fragment_states(blitter
);
1520 util_blitter_restore_render_cond(blitter
);
1521 util_blitter_unset_running_flag(blitter
);
1524 void util_blitter_clear(struct blitter_context
*blitter
,
1525 unsigned width
, unsigned height
, unsigned num_layers
,
1526 unsigned clear_buffers
,
1527 const union pipe_color_union
*color
,
1528 double depth
, unsigned stencil
,
1531 util_blitter_clear_custom(blitter
, width
, height
, num_layers
,
1532 clear_buffers
, color
, depth
, stencil
,
1536 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1537 unsigned width
, unsigned height
,
1538 double depth
, void *custom_dsa
)
1540 static const union pipe_color_union color
;
1541 util_blitter_clear_custom(blitter
, width
, height
, 0, 0, &color
, depth
, 0,
1542 NULL
, custom_dsa
, false);
1545 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1546 struct pipe_resource
*dst
,
1550 memset(dst_templ
, 0, sizeof(*dst_templ
));
1551 dst_templ
->format
= util_format_linear(dst
->format
);
1552 dst_templ
->u
.tex
.level
= dstlevel
;
1553 dst_templ
->u
.tex
.first_layer
= dstz
;
1554 dst_templ
->u
.tex
.last_layer
= dstz
;
1557 static struct pipe_surface
*
1558 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
1559 struct pipe_surface
*surf
)
1561 struct pipe_surface dst_templ
;
1563 memset(&dst_templ
, 0, sizeof(dst_templ
));
1564 dst_templ
.format
= surf
->format
;
1565 dst_templ
.u
.tex
.level
= surf
->u
.tex
.level
;
1566 dst_templ
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
+ 1;
1567 dst_templ
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
+ 1;
1569 return pipe
->create_surface(pipe
, surf
->texture
, &dst_templ
);
1572 void util_blitter_default_src_texture(struct blitter_context
*blitter
,
1573 struct pipe_sampler_view
*src_templ
,
1574 struct pipe_resource
*src
,
1577 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1579 memset(src_templ
, 0, sizeof(*src_templ
));
1581 if (ctx
->cube_as_2darray
&&
1582 (src
->target
== PIPE_TEXTURE_CUBE
||
1583 src
->target
== PIPE_TEXTURE_CUBE_ARRAY
))
1584 src_templ
->target
= PIPE_TEXTURE_2D_ARRAY
;
1586 src_templ
->target
= src
->target
;
1588 src_templ
->format
= util_format_linear(src
->format
);
1589 src_templ
->u
.tex
.first_level
= srclevel
;
1590 src_templ
->u
.tex
.last_level
= srclevel
;
1591 src_templ
->u
.tex
.first_layer
= 0;
1592 src_templ
->u
.tex
.last_layer
=
1593 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1594 : (unsigned)(src
->array_size
- 1);
1595 src_templ
->swizzle_r
= PIPE_SWIZZLE_X
;
1596 src_templ
->swizzle_g
= PIPE_SWIZZLE_Y
;
1597 src_templ
->swizzle_b
= PIPE_SWIZZLE_Z
;
1598 src_templ
->swizzle_a
= PIPE_SWIZZLE_W
;
1601 static bool is_blit_generic_supported(struct blitter_context
*blitter
,
1602 const struct pipe_resource
*dst
,
1603 enum pipe_format dst_format
,
1604 const struct pipe_resource
*src
,
1605 enum pipe_format src_format
,
1608 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1609 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1613 const struct util_format_description
*desc
=
1614 util_format_description(dst_format
);
1615 bool dst_has_stencil
= util_format_has_stencil(desc
);
1617 /* Stencil export must be supported for stencil copy. */
1618 if ((mask
& PIPE_MASK_S
) && dst_has_stencil
&&
1619 !ctx
->has_stencil_export
) {
1623 if (dst_has_stencil
|| util_format_has_depth(desc
))
1624 bind
= PIPE_BIND_DEPTH_STENCIL
;
1626 bind
= PIPE_BIND_RENDER_TARGET
;
1628 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1629 dst
->nr_samples
, dst
->nr_storage_samples
,
1636 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1640 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1641 src
->nr_samples
, src
->nr_storage_samples
,
1642 PIPE_BIND_SAMPLER_VIEW
)) {
1646 /* Check stencil sampler support for stencil copy. */
1647 if (mask
& PIPE_MASK_S
) {
1648 if (util_format_has_stencil(util_format_description(src_format
))) {
1649 enum pipe_format stencil_format
=
1650 util_format_stencil_only(src_format
);
1651 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1653 if (stencil_format
!= src_format
&&
1654 !screen
->is_format_supported(screen
, stencil_format
,
1655 src
->target
, src
->nr_samples
,
1656 src
->nr_storage_samples
,
1657 PIPE_BIND_SAMPLER_VIEW
)) {
1667 bool util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1668 const struct pipe_resource
*dst
,
1669 const struct pipe_resource
*src
)
1671 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1672 src
, src
->format
, PIPE_MASK_RGBAZS
);
1675 bool util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1676 const struct pipe_blit_info
*info
)
1678 return is_blit_generic_supported(blitter
,
1679 info
->dst
.resource
, info
->dst
.format
,
1680 info
->src
.resource
, info
->src
.format
,
1684 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1685 struct pipe_resource
*dst
,
1687 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1688 struct pipe_resource
*src
,
1690 const struct pipe_box
*srcbox
)
1692 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1693 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1694 struct pipe_surface
*dst_view
, dst_templ
;
1695 struct pipe_sampler_view src_templ
, *src_view
;
1696 struct pipe_box dstbox
;
1699 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1701 u_box_3d(dstx
, dsty
, dstz
, abs(srcbox
->width
), abs(srcbox
->height
),
1702 abs(srcbox
->depth
), &dstbox
);
1704 /* Initialize the surface. */
1705 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1706 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1708 /* Initialize the sampler view. */
1709 util_blitter_default_src_texture(blitter
, &src_templ
, src
, src_level
);
1710 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1713 util_blitter_blit_generic(blitter
, dst_view
, &dstbox
,
1714 src_view
, srcbox
, src
->width0
, src
->height0
,
1715 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
,
1718 pipe_surface_reference(&dst_view
, NULL
);
1719 pipe_sampler_view_reference(&src_view
, NULL
);
1723 blitter_draw_tex(struct blitter_context_priv
*ctx
,
1724 int dst_x1
, int dst_y1
, int dst_x2
, int dst_y2
,
1725 struct pipe_sampler_view
*src
,
1726 unsigned src_width0
, unsigned src_height0
,
1727 int src_x1
, int src_y1
, int src_x2
, int src_y2
,
1728 float layer
, unsigned sample
,
1729 bool uses_txf
, enum blitter_attrib_type type
)
1731 union blitter_attrib coord
;
1732 blitter_get_vs_func get_vs
= get_vs_passthrough_pos_generic
;
1734 get_texcoords(src
, src_width0
, src_height0
,
1735 src_x1
, src_y1
, src_x2
, src_y2
, layer
, sample
,
1738 if (src
->target
== PIPE_TEXTURE_CUBE
||
1739 src
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
1740 float face_coord
[4][2];
1742 set_texcoords_in_vertices(&coord
, &face_coord
[0][0], 2);
1743 util_map_texcoords2d_onto_cubemap((unsigned)layer
% 6,
1744 /* pointer, stride in floats */
1745 &face_coord
[0][0], 2,
1746 &ctx
->vertices
[0][1][0], 8,
1748 for (unsigned i
= 0; i
< 4; i
++)
1749 ctx
->vertices
[i
][1][3] = coord
.texcoord
.w
;
1751 /* Cubemaps don't use draw_rectangle. */
1752 blitter_draw(ctx
, ctx
->velem_state
, get_vs
,
1753 dst_x1
, dst_y1
, dst_x2
, dst_y2
, 0, 1);
1755 ctx
->base
.draw_rectangle(&ctx
->base
, ctx
->velem_state
, get_vs
,
1756 dst_x1
, dst_y1
, dst_x2
, dst_y2
,
1757 0, 1, type
, &coord
);
1761 static void do_blits(struct blitter_context_priv
*ctx
,
1762 struct pipe_surface
*dst
,
1763 const struct pipe_box
*dstbox
,
1764 struct pipe_sampler_view
*src
,
1765 unsigned src_width0
,
1766 unsigned src_height0
,
1767 const struct pipe_box
*srcbox
,
1771 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1772 unsigned src_samples
= src
->texture
->nr_samples
;
1773 unsigned dst_samples
= dst
->texture
->nr_samples
;
1774 enum pipe_texture_target src_target
= src
->target
;
1775 struct pipe_framebuffer_state fb_state
= {0};
1777 /* Initialize framebuffer state. */
1778 fb_state
.width
= dst
->width
;
1779 fb_state
.height
= dst
->height
;
1780 fb_state
.nr_cbufs
= is_zsbuf
? 0 : 1;
1782 blitter_set_dst_dimensions(ctx
, fb_state
.width
, fb_state
.height
);
1784 if ((src_target
== PIPE_TEXTURE_1D
||
1785 src_target
== PIPE_TEXTURE_2D
||
1786 src_target
== PIPE_TEXTURE_RECT
) &&
1788 /* Set framebuffer state. */
1790 fb_state
.zsbuf
= dst
;
1792 fb_state
.cbufs
[0] = dst
;
1794 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1797 pipe
->set_sample_mask(pipe
, ~0);
1798 blitter_draw_tex(ctx
, dstbox
->x
, dstbox
->y
,
1799 dstbox
->x
+ dstbox
->width
,
1800 dstbox
->y
+ dstbox
->height
,
1801 src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1802 srcbox
->x
+ srcbox
->width
, srcbox
->y
+ srcbox
->height
,
1803 0, 0, uses_txf
, UTIL_BLITTER_ATTRIB_TEXCOORD_XY
);
1805 /* Draw the quad with the generic codepath. */
1807 for (dst_z
= 0; dst_z
< dstbox
->depth
; dst_z
++) {
1808 struct pipe_surface
*old
;
1809 float dst2src_scale
= srcbox
->depth
/ (float)dstbox
->depth
;
1811 /* Scale Z properly if the blit is scaled.
1813 * When downscaling, we want the coordinates centered, so that
1814 * mipmapping works for 3D textures. For example, when generating
1815 * a 4x4x4 level, this wouldn't average the pixels:
1817 * src Z: 0 1 2 3 4 5 6 7
1820 * Because the pixels are not centered below the pixels of the higher
1821 * level. Therefore, we want this:
1822 * src Z: 0 1 2 3 4 5 6 7
1825 * dst_offset defines the offset needed for centering the pixels and
1826 * it works with any scaling (not just 2x).
1828 float dst_offset
= ((srcbox
->depth
- 1) -
1829 (dstbox
->depth
- 1) * dst2src_scale
) * 0.5;
1830 float src_z
= (dst_z
+ dst_offset
) * dst2src_scale
;
1832 /* Set framebuffer state. */
1834 fb_state
.zsbuf
= dst
;
1836 fb_state
.cbufs
[0] = dst
;
1838 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1840 /* See if we need to blit a multisample or singlesample buffer. */
1841 if (src_samples
== dst_samples
&& dst_samples
> 1) {
1843 unsigned i
, max_sample
= dst_samples
- 1;
1845 for (i
= 0; i
<= max_sample
; i
++) {
1846 pipe
->set_sample_mask(pipe
, 1 << i
);
1847 blitter_draw_tex(ctx
, dstbox
->x
, dstbox
->y
,
1848 dstbox
->x
+ dstbox
->width
,
1849 dstbox
->y
+ dstbox
->height
,
1850 src
, src_width0
, src_height0
,
1851 srcbox
->x
, srcbox
->y
,
1852 srcbox
->x
+ srcbox
->width
,
1853 srcbox
->y
+ srcbox
->height
,
1854 srcbox
->z
+ src_z
, i
, uses_txf
,
1855 UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW
);
1858 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1859 pipe
->set_sample_mask(pipe
, ~0);
1860 blitter_draw_tex(ctx
, dstbox
->x
, dstbox
->y
,
1861 dstbox
->x
+ dstbox
->width
,
1862 dstbox
->y
+ dstbox
->height
,
1863 src
, src_width0
, src_height0
,
1864 srcbox
->x
, srcbox
->y
,
1865 srcbox
->x
+ srcbox
->width
,
1866 srcbox
->y
+ srcbox
->height
,
1867 srcbox
->z
+ src_z
, 0, uses_txf
,
1868 UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW
);
1871 /* Get the next surface or (if this is the last iteration)
1872 * just unreference the last one. */
1874 if (dst_z
< dstbox
->depth
-1) {
1875 dst
= util_blitter_get_next_surface_layer(ctx
->base
.pipe
, dst
);
1878 pipe_surface_reference(&old
, NULL
);
1884 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1885 struct pipe_surface
*dst
,
1886 const struct pipe_box
*dstbox
,
1887 struct pipe_sampler_view
*src
,
1888 const struct pipe_box
*srcbox
,
1889 unsigned src_width0
, unsigned src_height0
,
1890 unsigned mask
, unsigned filter
,
1891 const struct pipe_scissor_state
*scissor
,
1894 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1895 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1896 enum pipe_texture_target src_target
= src
->target
;
1897 unsigned src_samples
= src
->texture
->nr_samples
;
1898 unsigned dst_samples
= dst
->texture
->nr_samples
;
1899 void *sampler_state
;
1900 const struct util_format_description
*src_desc
=
1901 util_format_description(src
->format
);
1902 const struct util_format_description
*dst_desc
=
1903 util_format_description(dst
->format
);
1905 bool src_has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1906 bool src_has_depth
= util_format_has_depth(src_desc
);
1907 bool src_has_stencil
= util_format_has_stencil(src_desc
);
1909 bool dst_has_color
= mask
& PIPE_MASK_RGBA
&&
1910 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1911 bool dst_has_depth
= mask
& PIPE_MASK_Z
&&
1912 util_format_has_depth(dst_desc
);
1913 bool dst_has_stencil
= ctx
->has_stencil_export
&&
1914 mask
& PIPE_MASK_S
&&
1915 util_format_has_stencil(dst_desc
);
1917 /* Return if there is nothing to do. */
1918 if (!dst_has_color
&& !dst_has_depth
&& !dst_has_stencil
) {
1922 bool is_scaled
= dstbox
->width
!= abs(srcbox
->width
) ||
1923 dstbox
->height
!= abs(srcbox
->height
);
1925 if (src_has_stencil
|| !is_scaled
)
1926 filter
= PIPE_TEX_FILTER_NEAREST
;
1928 bool use_txf
= false;
1930 /* Don't support scaled blits. The TXF shader uses F2I for rounding. */
1933 filter
== PIPE_TEX_FILTER_NEAREST
&&
1934 src
->target
!= PIPE_TEXTURE_CUBE
&&
1935 src
->target
!= PIPE_TEXTURE_CUBE_ARRAY
) {
1936 int src_width
= u_minify(src_width0
, src
->u
.tex
.first_level
);
1937 int src_height
= u_minify(src_height0
, src
->u
.tex
.first_level
);
1938 int src_depth
= src
->u
.tex
.last_layer
+ 1;
1939 struct pipe_box box
= *srcbox
;
1941 /* Eliminate negative width/height/depth. */
1942 if (box
.width
< 0) {
1946 if (box
.height
< 0) {
1947 box
.y
+= box
.height
;
1950 if (box
.depth
< 0) {
1955 /* See if srcbox is in bounds. TXF doesn't clamp the coordinates. */
1957 box
.x
>= 0 && box
.x
< src_width
&&
1958 box
.y
>= 0 && box
.y
< src_height
&&
1959 box
.z
>= 0 && box
.z
< src_depth
&&
1960 box
.x
+ box
.width
> 0 && box
.x
+ box
.width
<= src_width
&&
1961 box
.y
+ box
.height
> 0 && box
.y
+ box
.height
<= src_height
&&
1962 box
.z
+ box
.depth
> 0 && box
.z
+ box
.depth
<= src_depth
;
1965 /* Check whether the states are properly saved. */
1966 util_blitter_set_running_flag(blitter
);
1967 blitter_check_saved_vertex_states(ctx
);
1968 blitter_check_saved_fragment_states(ctx
);
1969 blitter_check_saved_textures(ctx
);
1970 blitter_check_saved_fb_state(ctx
);
1971 blitter_disable_render_cond(ctx
);
1973 /* Blend, DSA, fragment shader. */
1974 if (dst_has_depth
&& dst_has_stencil
) {
1975 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
1976 pipe
->bind_depth_stencil_alpha_state(pipe
,
1977 ctx
->dsa_write_depth_stencil
);
1978 if (src_has_color
) {
1980 ctx
->bind_fs_state(pipe
,
1981 blitter_get_fs_pack_color_zs(ctx
, src_target
,
1982 src_samples
, dst
->format
, false));
1984 ctx
->bind_fs_state(pipe
,
1985 blitter_get_fs_texfetch_depthstencil(ctx
, src_target
,
1986 src_samples
, use_txf
));
1988 } else if (dst_has_depth
) {
1989 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
1990 pipe
->bind_depth_stencil_alpha_state(pipe
,
1991 ctx
->dsa_write_depth_keep_stencil
);
1992 if (src_has_color
&&
1993 (src
->format
== PIPE_FORMAT_R32_UINT
||
1994 src
->format
== PIPE_FORMAT_R32G32_UINT
)) {
1996 ctx
->bind_fs_state(pipe
,
1997 blitter_get_fs_pack_color_zs(ctx
, src_target
,
1998 src_samples
, dst
->format
, false));
2000 ctx
->bind_fs_state(pipe
,
2001 blitter_get_fs_texfetch_depth(ctx
, src_target
,
2002 src_samples
, use_txf
));
2004 } else if (dst_has_stencil
) {
2005 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
2006 pipe
->bind_depth_stencil_alpha_state(pipe
,
2007 ctx
->dsa_keep_depth_write_stencil
);
2009 assert(src_has_stencil
); /* unpacking from color is unsupported */
2010 ctx
->bind_fs_state(pipe
,
2011 blitter_get_fs_texfetch_stencil(ctx
, src_target
,
2012 src_samples
, use_txf
));
2014 unsigned colormask
= mask
& PIPE_MASK_RGBA
;
2016 pipe
->bind_blend_state(pipe
, ctx
->blend
[colormask
][alpha_blend
]);
2017 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2019 if (src_has_depth
&&
2020 (dst
->format
== PIPE_FORMAT_R32_UINT
||
2021 dst
->format
== PIPE_FORMAT_R32G32_UINT
)) {
2023 ctx
->bind_fs_state(pipe
,
2024 blitter_get_fs_pack_color_zs(ctx
, src_target
,
2025 src_samples
, src
->format
, true));
2027 ctx
->bind_fs_state(pipe
,
2028 blitter_get_fs_texfetch_col(ctx
, src
->format
, dst
->format
, src_target
,
2029 src_samples
, dst_samples
, filter
,
2034 /* Set the linear filter only for scaled color non-MSAA blits. */
2035 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
2036 if (src_target
== PIPE_TEXTURE_RECT
) {
2037 sampler_state
= ctx
->sampler_state_rect_linear
;
2039 sampler_state
= ctx
->sampler_state_linear
;
2042 if (src_target
== PIPE_TEXTURE_RECT
) {
2043 sampler_state
= ctx
->sampler_state_rect
;
2045 sampler_state
= ctx
->sampler_state
;
2050 if (src_has_depth
&& src_has_stencil
&&
2051 (dst_has_color
|| (dst_has_depth
&& dst_has_stencil
))) {
2052 /* Setup two samplers, one for depth and the other one for stencil. */
2053 struct pipe_sampler_view templ
;
2054 struct pipe_sampler_view
*views
[2];
2055 void *samplers
[2] = {sampler_state
, sampler_state
};
2058 templ
.format
= util_format_stencil_only(templ
.format
);
2059 assert(templ
.format
!= PIPE_FORMAT_NONE
);
2062 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
2064 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, views
);
2065 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, samplers
);
2067 pipe_sampler_view_reference(&views
[1], NULL
);
2068 } else if (src_has_stencil
&& dst_has_stencil
) {
2069 /* Set a stencil-only sampler view for it not to sample depth instead. */
2070 struct pipe_sampler_view templ
;
2071 struct pipe_sampler_view
*view
;
2074 templ
.format
= util_format_stencil_only(templ
.format
);
2075 assert(templ
.format
!= PIPE_FORMAT_NONE
);
2077 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
2079 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &view
);
2080 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
2081 0, 1, &sampler_state
);
2083 pipe_sampler_view_reference(&view
, NULL
);
2085 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src
);
2086 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
2087 0, 1, &sampler_state
);
2091 pipe
->set_scissor_states(pipe
, 0, 1, scissor
);
2094 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
, dst_samples
> 1);
2096 do_blits(ctx
, dst
, dstbox
, src
, src_width0
, src_height0
,
2097 srcbox
, dst_has_depth
|| dst_has_stencil
, use_txf
);
2099 util_blitter_restore_vertex_states(blitter
);
2100 util_blitter_restore_fragment_states(blitter
);
2101 util_blitter_restore_textures(blitter
);
2102 util_blitter_restore_fb_state(blitter
);
2104 pipe
->set_scissor_states(pipe
, 0, 1, &ctx
->base
.saved_scissor
);
2106 util_blitter_restore_render_cond(blitter
);
2107 util_blitter_unset_running_flag(blitter
);
2111 util_blitter_blit(struct blitter_context
*blitter
,
2112 const struct pipe_blit_info
*info
)
2114 struct pipe_resource
*dst
= info
->dst
.resource
;
2115 struct pipe_resource
*src
= info
->src
.resource
;
2116 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2117 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2118 struct pipe_surface
*dst_view
, dst_templ
;
2119 struct pipe_sampler_view src_templ
, *src_view
;
2121 /* Initialize the surface. */
2122 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
2124 dst_templ
.format
= info
->dst
.format
;
2125 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
2127 /* Initialize the sampler view. */
2128 util_blitter_default_src_texture(blitter
, &src_templ
, src
, info
->src
.level
);
2129 src_templ
.format
= info
->src
.format
;
2130 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
2133 util_blitter_blit_generic(blitter
, dst_view
, &info
->dst
.box
,
2134 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
2135 info
->mask
, info
->filter
,
2136 info
->scissor_enable
? &info
->scissor
: NULL
,
2139 pipe_surface_reference(&dst_view
, NULL
);
2140 pipe_sampler_view_reference(&src_view
, NULL
);
2143 void util_blitter_generate_mipmap(struct blitter_context
*blitter
,
2144 struct pipe_resource
*tex
,
2145 enum pipe_format format
,
2146 unsigned base_level
, unsigned last_level
,
2147 unsigned first_layer
, unsigned last_layer
)
2149 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2150 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2151 struct pipe_surface dst_templ
, *dst_view
;
2152 struct pipe_sampler_view src_templ
, *src_view
;
2154 void *sampler_state
;
2155 const struct util_format_description
*desc
=
2156 util_format_description(format
);
2158 unsigned target
= tex
->target
;
2160 if (ctx
->cube_as_2darray
&&
2161 (target
== PIPE_TEXTURE_CUBE
|| target
== PIPE_TEXTURE_CUBE_ARRAY
))
2162 target
= PIPE_TEXTURE_2D_ARRAY
;
2164 assert(tex
->nr_samples
<= 1);
2165 /* Disallow stencil formats without depth. */
2166 assert(!util_format_has_stencil(desc
) || util_format_has_depth(desc
));
2168 is_depth
= desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
;
2170 /* Check whether the states are properly saved. */
2171 util_blitter_set_running_flag(blitter
);
2172 blitter_check_saved_vertex_states(ctx
);
2173 blitter_check_saved_fragment_states(ctx
);
2174 blitter_check_saved_textures(ctx
);
2175 blitter_check_saved_fb_state(ctx
);
2176 blitter_disable_render_cond(ctx
);
2180 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
2181 pipe
->bind_depth_stencil_alpha_state(pipe
,
2182 ctx
->dsa_write_depth_keep_stencil
);
2183 ctx
->bind_fs_state(pipe
,
2184 blitter_get_fs_texfetch_depth(ctx
, target
, 1, false));
2186 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2187 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2188 ctx
->bind_fs_state(pipe
,
2189 blitter_get_fs_texfetch_col(ctx
, tex
->format
, tex
->format
, target
,
2190 1, 1, PIPE_TEX_FILTER_LINEAR
, false));
2193 if (target
== PIPE_TEXTURE_RECT
) {
2194 sampler_state
= ctx
->sampler_state_rect_linear
;
2196 sampler_state
= ctx
->sampler_state_linear
;
2198 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
2199 0, 1, &sampler_state
);
2201 blitter_set_common_draw_rect_state(ctx
, false, false);
2203 for (src_level
= base_level
; src_level
< last_level
; src_level
++) {
2204 struct pipe_box dstbox
= {0}, srcbox
= {0};
2205 unsigned dst_level
= src_level
+ 1;
2207 dstbox
.width
= u_minify(tex
->width0
, dst_level
);
2208 dstbox
.height
= u_minify(tex
->height0
, dst_level
);
2210 srcbox
.width
= u_minify(tex
->width0
, src_level
);
2211 srcbox
.height
= u_minify(tex
->height0
, src_level
);
2213 if (target
== PIPE_TEXTURE_3D
) {
2214 dstbox
.depth
= util_num_layers(tex
, dst_level
);
2215 srcbox
.depth
= util_num_layers(tex
, src_level
);
2217 dstbox
.z
= srcbox
.z
= first_layer
;
2218 dstbox
.depth
= srcbox
.depth
= last_layer
- first_layer
+ 1;
2221 /* Initialize the surface. */
2222 util_blitter_default_dst_texture(&dst_templ
, tex
, dst_level
,
2224 dst_templ
.format
= format
;
2225 dst_view
= pipe
->create_surface(pipe
, tex
, &dst_templ
);
2227 /* Initialize the sampler view. */
2228 util_blitter_default_src_texture(blitter
, &src_templ
, tex
, src_level
);
2229 src_templ
.format
= format
;
2230 src_view
= pipe
->create_sampler_view(pipe
, tex
, &src_templ
);
2232 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src_view
);
2234 do_blits(ctx
, dst_view
, &dstbox
, src_view
, tex
->width0
, tex
->height0
,
2235 &srcbox
, is_depth
, false);
2237 pipe_surface_reference(&dst_view
, NULL
);
2238 pipe_sampler_view_reference(&src_view
, NULL
);
2241 util_blitter_restore_vertex_states(blitter
);
2242 util_blitter_restore_fragment_states(blitter
);
2243 util_blitter_restore_textures(blitter
);
2244 util_blitter_restore_fb_state(blitter
);
2245 util_blitter_restore_render_cond(blitter
);
2246 util_blitter_unset_running_flag(blitter
);
2249 /* Clear a region of a color surface to a constant value. */
2250 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
2251 struct pipe_surface
*dstsurf
,
2252 const union pipe_color_union
*color
,
2253 unsigned dstx
, unsigned dsty
,
2254 unsigned width
, unsigned height
)
2256 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2257 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2258 struct pipe_framebuffer_state fb_state
;
2260 unsigned num_layers
;
2262 assert(dstsurf
->texture
);
2263 if (!dstsurf
->texture
)
2266 /* check the saved state */
2267 util_blitter_set_running_flag(blitter
);
2268 blitter_check_saved_vertex_states(ctx
);
2269 blitter_check_saved_fragment_states(ctx
);
2270 blitter_check_saved_fb_state(ctx
);
2271 blitter_disable_render_cond(ctx
);
2274 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2275 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2276 bind_fs_write_one_cbuf(ctx
);
2278 /* set a framebuffer state */
2279 fb_state
.width
= dstsurf
->width
;
2280 fb_state
.height
= dstsurf
->height
;
2281 fb_state
.nr_cbufs
= 1;
2282 fb_state
.cbufs
[0] = dstsurf
;
2284 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2285 pipe
->set_sample_mask(pipe
, ~0);
2286 msaa
= util_framebuffer_get_num_samples(&fb_state
) > 1;
2288 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2290 union blitter_attrib attrib
;
2291 memcpy(attrib
.color
, color
->ui
, sizeof(color
->ui
));
2293 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
2294 if (num_layers
> 1 && ctx
->has_layered
) {
2295 blitter_set_common_draw_rect_state(ctx
, false, msaa
);
2296 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs_layered
,
2297 dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
2298 num_layers
, UTIL_BLITTER_ATTRIB_COLOR
, &attrib
);
2300 blitter_set_common_draw_rect_state(ctx
, false, msaa
);
2301 blitter
->draw_rectangle(blitter
, ctx
->velem_state
,
2302 get_vs_passthrough_pos_generic
,
2303 dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
2304 1, UTIL_BLITTER_ATTRIB_COLOR
, &attrib
);
2307 util_blitter_restore_vertex_states(blitter
);
2308 util_blitter_restore_fragment_states(blitter
);
2309 util_blitter_restore_fb_state(blitter
);
2310 util_blitter_restore_render_cond(blitter
);
2311 util_blitter_unset_running_flag(blitter
);
2314 /* Clear a region of a depth stencil surface. */
2315 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
2316 struct pipe_surface
*dstsurf
,
2317 unsigned clear_flags
,
2320 unsigned dstx
, unsigned dsty
,
2321 unsigned width
, unsigned height
)
2323 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2324 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2325 struct pipe_framebuffer_state fb_state
;
2326 struct pipe_stencil_ref sr
= { { 0 } };
2327 unsigned num_layers
;
2329 assert(dstsurf
->texture
);
2330 if (!dstsurf
->texture
)
2333 /* check the saved state */
2334 util_blitter_set_running_flag(blitter
);
2335 blitter_check_saved_vertex_states(ctx
);
2336 blitter_check_saved_fragment_states(ctx
);
2337 blitter_check_saved_fb_state(ctx
);
2338 blitter_disable_render_cond(ctx
);
2341 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
2342 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
2343 sr
.ref_value
[0] = stencil
& 0xff;
2344 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
2345 pipe
->set_stencil_ref(pipe
, &sr
);
2347 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
2348 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
2350 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
2351 sr
.ref_value
[0] = stencil
& 0xff;
2352 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
2353 pipe
->set_stencil_ref(pipe
, &sr
);
2356 /* hmm that should be illegal probably, or make it a no-op somewhere */
2357 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2361 /* set a framebuffer state */
2362 fb_state
.width
= dstsurf
->width
;
2363 fb_state
.height
= dstsurf
->height
;
2364 fb_state
.nr_cbufs
= 0;
2365 fb_state
.cbufs
[0] = 0;
2366 fb_state
.zsbuf
= dstsurf
;
2367 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2368 pipe
->set_sample_mask(pipe
, ~0);
2370 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2372 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
2373 if (num_layers
> 1 && ctx
->has_layered
) {
2374 blitter_set_common_draw_rect_state(ctx
, false, false);
2375 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs_layered
,
2376 dstx
, dsty
, dstx
+width
, dsty
+height
, depth
,
2377 num_layers
, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2379 blitter_set_common_draw_rect_state(ctx
, false, false);
2380 blitter
->draw_rectangle(blitter
, ctx
->velem_state
,
2381 get_vs_passthrough_pos
,
2382 dstx
, dsty
, dstx
+width
, dsty
+height
, depth
, 1,
2383 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2386 util_blitter_restore_vertex_states(blitter
);
2387 util_blitter_restore_fragment_states(blitter
);
2388 util_blitter_restore_fb_state(blitter
);
2389 util_blitter_restore_render_cond(blitter
);
2390 util_blitter_unset_running_flag(blitter
);
2393 /* draw a rectangle across a region using a custom dsa stage - for r600g */
2394 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
2395 struct pipe_surface
*zsurf
,
2396 struct pipe_surface
*cbsurf
,
2397 unsigned sample_mask
,
2398 void *dsa_stage
, float depth
)
2400 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2401 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2402 struct pipe_framebuffer_state fb_state
;
2404 assert(zsurf
->texture
);
2405 if (!zsurf
->texture
)
2408 /* check the saved state */
2409 util_blitter_set_running_flag(blitter
);
2410 blitter_check_saved_vertex_states(ctx
);
2411 blitter_check_saved_fragment_states(ctx
);
2412 blitter_check_saved_fb_state(ctx
);
2413 blitter_disable_render_cond(ctx
);
2416 pipe
->bind_blend_state(pipe
, cbsurf
? ctx
->blend
[PIPE_MASK_RGBA
][0] :
2418 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
2420 bind_fs_write_one_cbuf(ctx
);
2424 /* set a framebuffer state */
2425 fb_state
.width
= zsurf
->width
;
2426 fb_state
.height
= zsurf
->height
;
2427 fb_state
.nr_cbufs
= 1;
2429 fb_state
.cbufs
[0] = cbsurf
;
2430 fb_state
.nr_cbufs
= 1;
2432 fb_state
.cbufs
[0] = NULL
;
2433 fb_state
.nr_cbufs
= 0;
2435 fb_state
.zsbuf
= zsurf
;
2436 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2437 pipe
->set_sample_mask(pipe
, sample_mask
);
2439 blitter_set_common_draw_rect_state(ctx
, false,
2440 util_framebuffer_get_num_samples(&fb_state
) > 1);
2441 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
2442 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs_passthrough_pos
,
2443 0, 0, zsurf
->width
, zsurf
->height
, depth
,
2444 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2446 util_blitter_restore_vertex_states(blitter
);
2447 util_blitter_restore_fragment_states(blitter
);
2448 util_blitter_restore_fb_state(blitter
);
2449 util_blitter_restore_render_cond(blitter
);
2450 util_blitter_unset_running_flag(blitter
);
2453 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
2454 struct pipe_resource
*dst
,
2456 struct pipe_resource
*src
,
2460 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2461 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2462 struct pipe_vertex_buffer vb
;
2463 struct pipe_stream_output_target
*so_target
;
2464 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2466 if (srcx
>= src
->width0
||
2467 dstx
>= dst
->width0
) {
2470 if (srcx
+ size
> src
->width0
) {
2471 size
= src
->width0
- srcx
;
2473 if (dstx
+ size
> dst
->width0
) {
2474 size
= dst
->width0
- dstx
;
2477 /* Drivers not capable of Stream Out should not call this function
2478 * in the first place. */
2479 assert(ctx
->has_stream_out
);
2481 /* Some alignment is required. */
2482 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
2483 !ctx
->has_stream_out
) {
2484 struct pipe_box box
;
2485 u_box_1d(srcx
, size
, &box
);
2486 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
2490 util_blitter_set_running_flag(blitter
);
2491 blitter_check_saved_vertex_states(ctx
);
2492 blitter_disable_render_cond(ctx
);
2494 vb
.is_user_buffer
= false;
2495 vb
.buffer
.resource
= src
;
2496 vb
.buffer_offset
= srcx
;
2499 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2500 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[0]);
2501 bind_vs_pos_only(ctx
, 1);
2502 if (ctx
->has_geometry_shader
)
2503 pipe
->bind_gs_state(pipe
, NULL
);
2504 if (ctx
->has_tessellation
) {
2505 pipe
->bind_tcs_state(pipe
, NULL
);
2506 pipe
->bind_tes_state(pipe
, NULL
);
2508 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2510 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
2511 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2513 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2515 util_blitter_restore_vertex_states(blitter
);
2516 util_blitter_restore_render_cond(blitter
);
2517 util_blitter_unset_running_flag(blitter
);
2518 pipe_so_target_reference(&so_target
, NULL
);
2521 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
2522 struct pipe_resource
*dst
,
2523 unsigned offset
, unsigned size
,
2524 unsigned num_channels
,
2525 const union pipe_color_union
*clear_value
)
2527 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2528 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2529 struct pipe_vertex_buffer vb
= {0};
2530 struct pipe_stream_output_target
*so_target
= NULL
;
2531 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2533 assert(num_channels
>= 1);
2534 assert(num_channels
<= 4);
2536 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
2538 * R600 uses this to initialize texture resources, so width0 might not be
2539 * what you think it is.
2542 /* Streamout is required. */
2543 if (!ctx
->has_stream_out
) {
2544 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2548 /* Some alignment is required. */
2549 if (offset
% 4 != 0 || size
% 4 != 0) {
2550 assert(!"Bad alignment in util_blitter_clear_buffer()");
2554 u_upload_data(pipe
->stream_uploader
, 0, num_channels
*4, 4, clear_value
,
2555 &vb
.buffer_offset
, &vb
.buffer
.resource
);
2556 if (!vb
.buffer
.resource
)
2561 util_blitter_set_running_flag(blitter
);
2562 blitter_check_saved_vertex_states(ctx
);
2563 blitter_disable_render_cond(ctx
);
2565 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2566 pipe
->bind_vertex_elements_state(pipe
,
2567 ctx
->velem_state_readbuf
[num_channels
-1]);
2568 bind_vs_pos_only(ctx
, num_channels
);
2569 if (ctx
->has_geometry_shader
)
2570 pipe
->bind_gs_state(pipe
, NULL
);
2571 if (ctx
->has_tessellation
) {
2572 pipe
->bind_tcs_state(pipe
, NULL
);
2573 pipe
->bind_tes_state(pipe
, NULL
);
2575 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2577 so_target
= pipe
->create_stream_output_target(pipe
, dst
, offset
, size
);
2578 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2580 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2583 util_blitter_restore_vertex_states(blitter
);
2584 util_blitter_restore_render_cond(blitter
);
2585 util_blitter_unset_running_flag(blitter
);
2586 pipe_so_target_reference(&so_target
, NULL
);
2587 pipe_resource_reference(&vb
.buffer
.resource
, NULL
);
2590 /* probably radeon specific */
2591 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
2592 struct pipe_resource
*dst
,
2595 struct pipe_resource
*src
,
2597 unsigned sample_mask
,
2599 enum pipe_format format
)
2601 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2602 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2603 struct pipe_framebuffer_state fb_state
;
2604 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
2606 util_blitter_set_running_flag(blitter
);
2607 blitter_check_saved_vertex_states(ctx
);
2608 blitter_check_saved_fragment_states(ctx
);
2609 blitter_disable_render_cond(ctx
);
2612 pipe
->bind_blend_state(pipe
, custom_blend
);
2613 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2614 bind_fs_write_one_cbuf(ctx
);
2615 pipe
->set_sample_mask(pipe
, sample_mask
);
2617 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
2618 surf_tmpl
.format
= format
;
2619 surf_tmpl
.u
.tex
.level
= dst_level
;
2620 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
2621 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
2623 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
2625 surf_tmpl
.u
.tex
.level
= 0;
2626 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
2627 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
2629 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
2631 /* set a framebuffer state */
2632 fb_state
.width
= src
->width0
;
2633 fb_state
.height
= src
->height0
;
2634 fb_state
.nr_cbufs
= 2;
2635 fb_state
.cbufs
[0] = srcsurf
;
2636 fb_state
.cbufs
[1] = dstsurf
;
2637 fb_state
.zsbuf
= NULL
;
2638 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2640 blitter_set_common_draw_rect_state(ctx
, false,
2641 util_framebuffer_get_num_samples(&fb_state
) > 1);
2642 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
2643 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs_passthrough_pos
,
2644 0, 0, src
->width0
, src
->height0
,
2645 0, 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2646 util_blitter_restore_fb_state(blitter
);
2647 util_blitter_restore_vertex_states(blitter
);
2648 util_blitter_restore_fragment_states(blitter
);
2649 util_blitter_restore_render_cond(blitter
);
2650 util_blitter_unset_running_flag(blitter
);
2652 pipe_surface_reference(&srcsurf
, NULL
);
2653 pipe_surface_reference(&dstsurf
, NULL
);
2656 void util_blitter_custom_color(struct blitter_context
*blitter
,
2657 struct pipe_surface
*dstsurf
,
2660 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2661 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2662 struct pipe_framebuffer_state fb_state
;
2664 assert(dstsurf
->texture
);
2665 if (!dstsurf
->texture
)
2668 /* check the saved state */
2669 util_blitter_set_running_flag(blitter
);
2670 blitter_check_saved_vertex_states(ctx
);
2671 blitter_check_saved_fragment_states(ctx
);
2672 blitter_check_saved_fb_state(ctx
);
2673 blitter_disable_render_cond(ctx
);
2676 pipe
->bind_blend_state(pipe
, custom_blend
? custom_blend
2677 : ctx
->blend
[PIPE_MASK_RGBA
][0]);
2678 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2679 bind_fs_write_one_cbuf(ctx
);
2680 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2682 /* set a framebuffer state */
2683 fb_state
.width
= dstsurf
->width
;
2684 fb_state
.height
= dstsurf
->height
;
2685 fb_state
.nr_cbufs
= 1;
2686 fb_state
.cbufs
[0] = dstsurf
;
2688 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2689 pipe
->set_sample_mask(pipe
, ~0);
2691 blitter_set_common_draw_rect_state(ctx
, false,
2692 util_framebuffer_get_num_samples(&fb_state
) > 1);
2693 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2694 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs_passthrough_pos
,
2695 0, 0, dstsurf
->width
, dstsurf
->height
,
2696 0, 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2698 util_blitter_restore_vertex_states(blitter
);
2699 util_blitter_restore_fragment_states(blitter
);
2700 util_blitter_restore_fb_state(blitter
);
2701 util_blitter_restore_render_cond(blitter
);
2702 util_blitter_unset_running_flag(blitter
);
2705 static void *get_custom_vs(struct blitter_context
*blitter
)
2707 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2709 return ctx
->custom_vs
;
2713 * Performs a custom blit to the destination surface, using the VS and FS
2716 * Used by vc4 for the 8-bit linear-to-tiled blit.
2718 void util_blitter_custom_shader(struct blitter_context
*blitter
,
2719 struct pipe_surface
*dstsurf
,
2720 void *custom_vs
, void *custom_fs
)
2722 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2723 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2724 struct pipe_framebuffer_state fb_state
;
2726 ctx
->custom_vs
= custom_vs
;
2728 assert(dstsurf
->texture
);
2729 if (!dstsurf
->texture
)
2732 /* check the saved state */
2733 util_blitter_set_running_flag(blitter
);
2734 blitter_check_saved_vertex_states(ctx
);
2735 blitter_check_saved_fragment_states(ctx
);
2736 blitter_check_saved_fb_state(ctx
);
2737 blitter_disable_render_cond(ctx
);
2740 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2741 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2742 pipe
->bind_fs_state(pipe
, custom_fs
);
2743 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2745 /* set a framebuffer state */
2746 fb_state
.width
= dstsurf
->width
;
2747 fb_state
.height
= dstsurf
->height
;
2748 fb_state
.nr_cbufs
= 1;
2749 fb_state
.cbufs
[0] = dstsurf
;
2751 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2752 pipe
->set_sample_mask(pipe
, ~0);
2754 blitter_set_common_draw_rect_state(ctx
, false,
2755 util_framebuffer_get_num_samples(&fb_state
) > 1);
2756 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2757 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_custom_vs
,
2758 0, 0, dstsurf
->width
, dstsurf
->height
,
2759 0, 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2761 util_blitter_restore_vertex_states(blitter
);
2762 util_blitter_restore_fragment_states(blitter
);
2763 util_blitter_restore_fb_state(blitter
);
2764 util_blitter_restore_render_cond(blitter
);
2765 util_blitter_unset_running_flag(blitter
);