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 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][3] = 1; /*v.w*/
343 static void bind_vs_pos_only(struct blitter_context_priv
*ctx
,
344 unsigned num_so_channels
)
346 struct pipe_context
*pipe
= ctx
->base
.pipe
;
347 int index
= num_so_channels
? num_so_channels
- 1 : 0;
349 if (!ctx
->vs_pos_only
[index
]) {
350 struct pipe_stream_output_info so
;
351 static const enum tgsi_semantic semantic_names
[] =
352 { TGSI_SEMANTIC_POSITION
};
353 const uint semantic_indices
[] = { 0 };
355 memset(&so
, 0, sizeof(so
));
357 so
.output
[0].num_components
= num_so_channels
;
358 so
.stride
[0] = num_so_channels
;
360 ctx
->vs_pos_only
[index
] =
361 util_make_vertex_passthrough_shader_with_so(pipe
, 1, semantic_names
,
362 semantic_indices
, false,
366 pipe
->bind_vs_state(pipe
, ctx
->vs_pos_only
[index
]);
369 static void *get_vs_passthrough_pos_generic(struct blitter_context
*blitter
)
371 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
372 struct pipe_context
*pipe
= ctx
->base
.pipe
;
375 static const enum tgsi_semantic semantic_names
[] =
376 { TGSI_SEMANTIC_POSITION
, TGSI_SEMANTIC_GENERIC
};
377 const uint semantic_indices
[] = { 0, 0 };
379 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
380 semantic_indices
, false);
385 static void *get_vs_passthrough_pos(struct blitter_context
*blitter
)
387 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
388 struct pipe_context
*pipe
= ctx
->base
.pipe
;
390 if (!ctx
->vs_nogeneric
) {
391 static const enum tgsi_semantic semantic_names
[] =
392 { TGSI_SEMANTIC_POSITION
};
393 const uint semantic_indices
[] = { 0 };
396 util_make_vertex_passthrough_shader(pipe
, 1,
398 semantic_indices
, false);
400 return ctx
->vs_nogeneric
;
403 static void *get_vs_layered(struct blitter_context
*blitter
)
405 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
406 struct pipe_context
*pipe
= ctx
->base
.pipe
;
408 if (!ctx
->vs_layered
) {
409 ctx
->vs_layered
= util_make_layered_clear_vertex_shader(pipe
);
411 return ctx
->vs_layered
;
414 static void bind_fs_empty(struct blitter_context_priv
*ctx
)
416 struct pipe_context
*pipe
= ctx
->base
.pipe
;
418 if (!ctx
->fs_empty
) {
419 assert(!ctx
->cached_all_shaders
);
420 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
423 ctx
->bind_fs_state(pipe
, ctx
->fs_empty
);
426 static void bind_fs_write_one_cbuf(struct blitter_context_priv
*ctx
)
428 struct pipe_context
*pipe
= ctx
->base
.pipe
;
430 if (!ctx
->fs_write_one_cbuf
) {
431 assert(!ctx
->cached_all_shaders
);
432 ctx
->fs_write_one_cbuf
=
433 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
434 TGSI_INTERPOLATE_CONSTANT
, false);
437 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
440 static void bind_fs_write_all_cbufs(struct blitter_context_priv
*ctx
)
442 struct pipe_context
*pipe
= ctx
->base
.pipe
;
444 if (!ctx
->fs_write_all_cbufs
) {
445 assert(!ctx
->cached_all_shaders
);
446 ctx
->fs_write_all_cbufs
=
447 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
448 TGSI_INTERPOLATE_CONSTANT
, true);
451 ctx
->bind_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
454 void util_blitter_destroy(struct blitter_context
*blitter
)
456 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
457 struct pipe_context
*pipe
= blitter
->pipe
;
460 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++)
461 for (j
= 0; j
< 2; j
++)
462 pipe
->delete_blend_state(pipe
, ctx
->blend
[i
][j
]);
464 for (i
= 0; i
< ARRAY_SIZE(ctx
->blend_clear
); i
++) {
465 if (ctx
->blend_clear
[i
])
466 pipe
->delete_blend_state(pipe
, ctx
->blend_clear
[i
]);
468 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
469 pipe
->delete_depth_stencil_alpha_state(pipe
,
470 ctx
->dsa_write_depth_keep_stencil
);
471 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
472 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
474 unsigned scissor
, msaa
;
475 for (scissor
= 0; scissor
< 2; scissor
++) {
476 for (msaa
= 0; msaa
< 2; msaa
++) {
477 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
[scissor
][msaa
]);
481 if (ctx
->rs_discard_state
)
482 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_discard_state
);
484 pipe
->delete_vs_state(pipe
, ctx
->vs
);
485 if (ctx
->vs_nogeneric
)
486 pipe
->delete_vs_state(pipe
, ctx
->vs_nogeneric
);
487 for (i
= 0; i
< 4; i
++)
488 if (ctx
->vs_pos_only
[i
])
489 pipe
->delete_vs_state(pipe
, ctx
->vs_pos_only
[i
]);
491 pipe
->delete_vs_state(pipe
, ctx
->vs_layered
);
492 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state
);
493 for (i
= 0; i
< 4; i
++) {
494 if (ctx
->velem_state_readbuf
[i
]) {
495 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[i
]);
499 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
500 for (unsigned type
= 0; type
< ARRAY_SIZE(ctx
->fs_texfetch_col
); ++type
) {
501 for (unsigned inst
= 0; inst
< 2; inst
++) {
502 if (ctx
->fs_texfetch_col
[type
][i
][inst
])
503 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[type
][i
][inst
]);
505 if (ctx
->fs_texfetch_col_msaa
[type
][i
])
506 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col_msaa
[type
][i
]);
509 for (unsigned inst
= 0; inst
< 2; inst
++) {
510 if (ctx
->fs_texfetch_depth
[i
][inst
])
511 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
][inst
]);
512 if (ctx
->fs_texfetch_depthstencil
[i
][inst
])
513 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil
[i
][inst
]);
514 if (ctx
->fs_texfetch_stencil
[i
][inst
])
515 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil
[i
][inst
]);
518 if (ctx
->fs_texfetch_depth_msaa
[i
])
519 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth_msaa
[i
]);
520 if (ctx
->fs_texfetch_depthstencil_msaa
[i
])
521 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil_msaa
[i
]);
522 if (ctx
->fs_texfetch_stencil_msaa
[i
])
523 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil_msaa
[i
]);
525 for (j
= 0; j
< ARRAY_SIZE(ctx
->fs_resolve
[i
]); j
++)
526 for (f
= 0; f
< 2; f
++)
527 if (ctx
->fs_resolve
[i
][j
][f
])
528 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve
[i
][j
][f
]);
531 for (i
= 0; i
< ARRAY_SIZE(ctx
->fs_pack_color_zs
); i
++) {
532 for (j
= 0; j
< ARRAY_SIZE(ctx
->fs_pack_color_zs
[0]); j
++) {
533 if (ctx
->fs_pack_color_zs
[i
][j
])
534 ctx
->delete_fs_state(pipe
, ctx
->fs_pack_color_zs
[i
][j
]);
539 ctx
->delete_fs_state(pipe
, ctx
->fs_empty
);
540 if (ctx
->fs_write_one_cbuf
)
541 ctx
->delete_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
542 if (ctx
->fs_write_all_cbufs
)
543 ctx
->delete_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
545 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect_linear
);
546 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect
);
547 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_linear
);
548 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
);
552 void util_blitter_set_texture_multisample(struct blitter_context
*blitter
,
555 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
557 ctx
->has_texture_multisample
= supported
;
560 void util_blitter_set_running_flag(struct blitter_context
*blitter
)
562 if (blitter
->running
) {
563 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
566 blitter
->running
= true;
568 blitter
->pipe
->set_active_query_state(blitter
->pipe
, false);
571 void util_blitter_unset_running_flag(struct blitter_context
*blitter
)
573 if (!blitter
->running
) {
574 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
577 blitter
->running
= false;
579 blitter
->pipe
->set_active_query_state(blitter
->pipe
, true);
582 static void blitter_check_saved_vertex_states(MAYBE_UNUSED
struct blitter_context_priv
*ctx
)
584 assert(ctx
->base
.saved_vs
!= INVALID_PTR
);
585 assert(!ctx
->has_geometry_shader
|| ctx
->base
.saved_gs
!= INVALID_PTR
);
586 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tcs
!= INVALID_PTR
);
587 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tes
!= INVALID_PTR
);
588 assert(!ctx
->has_stream_out
|| ctx
->base
.saved_num_so_targets
!= ~0u);
589 assert(ctx
->base
.saved_rs_state
!= INVALID_PTR
);
592 void util_blitter_restore_vertex_states(struct blitter_context
*blitter
)
594 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
595 struct pipe_context
*pipe
= ctx
->base
.pipe
;
599 if (ctx
->base
.saved_vertex_buffer
.buffer
.resource
) {
600 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1,
601 &ctx
->base
.saved_vertex_buffer
);
602 pipe_vertex_buffer_unreference(&ctx
->base
.saved_vertex_buffer
);
605 /* Vertex elements. */
606 if (ctx
->base
.saved_velem_state
!= INVALID_PTR
) {
607 pipe
->bind_vertex_elements_state(pipe
, ctx
->base
.saved_velem_state
);
608 ctx
->base
.saved_velem_state
= INVALID_PTR
;
612 pipe
->bind_vs_state(pipe
, ctx
->base
.saved_vs
);
613 ctx
->base
.saved_vs
= INVALID_PTR
;
615 /* Geometry shader. */
616 if (ctx
->has_geometry_shader
) {
617 pipe
->bind_gs_state(pipe
, ctx
->base
.saved_gs
);
618 ctx
->base
.saved_gs
= INVALID_PTR
;
621 if (ctx
->has_tessellation
) {
622 pipe
->bind_tcs_state(pipe
, ctx
->base
.saved_tcs
);
623 pipe
->bind_tes_state(pipe
, ctx
->base
.saved_tes
);
624 ctx
->base
.saved_tcs
= INVALID_PTR
;
625 ctx
->base
.saved_tes
= INVALID_PTR
;
628 /* Stream outputs. */
629 if (ctx
->has_stream_out
) {
630 unsigned offsets
[PIPE_MAX_SO_BUFFERS
];
631 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
632 offsets
[i
] = (unsigned)-1;
633 pipe
->set_stream_output_targets(pipe
,
634 ctx
->base
.saved_num_so_targets
,
635 ctx
->base
.saved_so_targets
, offsets
);
637 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
638 pipe_so_target_reference(&ctx
->base
.saved_so_targets
[i
], NULL
);
640 ctx
->base
.saved_num_so_targets
= ~0;
644 pipe
->bind_rasterizer_state(pipe
, ctx
->base
.saved_rs_state
);
645 ctx
->base
.saved_rs_state
= INVALID_PTR
;
648 static void blitter_check_saved_fragment_states(MAYBE_UNUSED
struct blitter_context_priv
*ctx
)
650 assert(ctx
->base
.saved_fs
!= INVALID_PTR
);
651 assert(ctx
->base
.saved_dsa_state
!= INVALID_PTR
);
652 assert(ctx
->base
.saved_blend_state
!= INVALID_PTR
);
655 void util_blitter_restore_fragment_states(struct blitter_context
*blitter
)
657 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
658 struct pipe_context
*pipe
= ctx
->base
.pipe
;
660 /* Fragment shader. */
661 ctx
->bind_fs_state(pipe
, ctx
->base
.saved_fs
);
662 ctx
->base
.saved_fs
= INVALID_PTR
;
664 /* Depth, stencil, alpha. */
665 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->base
.saved_dsa_state
);
666 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
669 pipe
->bind_blend_state(pipe
, ctx
->base
.saved_blend_state
);
670 ctx
->base
.saved_blend_state
= INVALID_PTR
;
673 if (ctx
->base
.is_sample_mask_saved
) {
674 pipe
->set_sample_mask(pipe
, ctx
->base
.saved_sample_mask
);
675 ctx
->base
.is_sample_mask_saved
= false;
678 /* Miscellaneous states. */
679 /* XXX check whether these are saved and whether they need to be restored
680 * (depending on the operation) */
681 pipe
->set_stencil_ref(pipe
, &ctx
->base
.saved_stencil_ref
);
683 if (!blitter
->skip_viewport_restore
)
684 pipe
->set_viewport_states(pipe
, 0, 1, &ctx
->base
.saved_viewport
);
686 if (blitter
->saved_num_window_rectangles
) {
687 pipe
->set_window_rectangles(pipe
,
688 blitter
->saved_window_rectangles_include
,
689 blitter
->saved_num_window_rectangles
,
690 blitter
->saved_window_rectangles
);
694 static void blitter_check_saved_fb_state(MAYBE_UNUSED
struct blitter_context_priv
*ctx
)
696 assert(ctx
->base
.saved_fb_state
.nr_cbufs
!= (ubyte
) ~0);
699 static void blitter_disable_render_cond(struct blitter_context_priv
*ctx
)
701 struct pipe_context
*pipe
= ctx
->base
.pipe
;
703 if (ctx
->base
.saved_render_cond_query
) {
704 pipe
->render_condition(pipe
, NULL
, false, 0);
708 void util_blitter_restore_render_cond(struct blitter_context
*blitter
)
710 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
711 struct pipe_context
*pipe
= ctx
->base
.pipe
;
713 if (ctx
->base
.saved_render_cond_query
) {
714 pipe
->render_condition(pipe
, ctx
->base
.saved_render_cond_query
,
715 ctx
->base
.saved_render_cond_cond
,
716 ctx
->base
.saved_render_cond_mode
);
717 ctx
->base
.saved_render_cond_query
= NULL
;
721 void util_blitter_restore_fb_state(struct blitter_context
*blitter
)
723 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
724 struct pipe_context
*pipe
= ctx
->base
.pipe
;
726 pipe
->set_framebuffer_state(pipe
, &ctx
->base
.saved_fb_state
);
727 util_unreference_framebuffer_state(&ctx
->base
.saved_fb_state
);
730 static void blitter_check_saved_textures(MAYBE_UNUSED
struct blitter_context_priv
*ctx
)
732 assert(ctx
->base
.saved_num_sampler_states
!= ~0u);
733 assert(ctx
->base
.saved_num_sampler_views
!= ~0u);
736 void util_blitter_restore_textures(struct blitter_context
*blitter
)
738 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
739 struct pipe_context
*pipe
= ctx
->base
.pipe
;
742 /* Fragment sampler states. */
743 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0,
744 ctx
->base
.saved_num_sampler_states
,
745 ctx
->base
.saved_sampler_states
);
747 ctx
->base
.saved_num_sampler_states
= ~0;
749 /* Fragment sampler views. */
750 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
751 ctx
->base
.saved_num_sampler_views
,
752 ctx
->base
.saved_sampler_views
);
754 for (i
= 0; i
< ctx
->base
.saved_num_sampler_views
; i
++)
755 pipe_sampler_view_reference(&ctx
->base
.saved_sampler_views
[i
], NULL
);
757 ctx
->base
.saved_num_sampler_views
= ~0;
760 void util_blitter_restore_constant_buffer_state(struct blitter_context
*blitter
)
762 struct pipe_context
*pipe
= blitter
->pipe
;
764 pipe
->set_constant_buffer(pipe
, PIPE_SHADER_FRAGMENT
, blitter
->cb_slot
,
765 &blitter
->saved_fs_constant_buffer
);
766 pipe_resource_reference(&blitter
->saved_fs_constant_buffer
.buffer
, NULL
);
769 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
770 int x1
, int y1
, int x2
, int y2
,
775 /* set vertex positions */
776 ctx
->vertices
[0][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v0.x*/
777 ctx
->vertices
[0][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v0.y*/
779 ctx
->vertices
[1][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v1.x*/
780 ctx
->vertices
[1][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v1.y*/
782 ctx
->vertices
[2][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v2.x*/
783 ctx
->vertices
[2][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v2.y*/
785 ctx
->vertices
[3][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v3.x*/
786 ctx
->vertices
[3][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v3.y*/
788 for (i
= 0; i
< 4; i
++)
789 ctx
->vertices
[i
][0][2] = depth
; /*z*/
792 struct pipe_viewport_state viewport
;
793 viewport
.scale
[0] = 0.5f
* ctx
->dst_width
;
794 viewport
.scale
[1] = 0.5f
* ctx
->dst_height
;
795 viewport
.scale
[2] = 1.0f
;
796 viewport
.translate
[0] = 0.5f
* ctx
->dst_width
;
797 viewport
.translate
[1] = 0.5f
* ctx
->dst_height
;
798 viewport
.translate
[2] = 0.0f
;
799 ctx
->base
.pipe
->set_viewport_states(ctx
->base
.pipe
, 0, 1, &viewport
);
802 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
803 const float color
[4])
808 for (i
= 0; i
< 4; i
++)
809 memcpy(&ctx
->vertices
[i
][1][0], color
, sizeof(uint32_t) * 4);
811 for (i
= 0; i
< 4; i
++)
812 memset(&ctx
->vertices
[i
][1][0], 0, sizeof(uint32_t) * 4);
816 static void get_texcoords(struct pipe_sampler_view
*src
,
817 unsigned src_width0
, unsigned src_height0
,
818 int x1
, int y1
, int x2
, int y2
,
819 float layer
, unsigned sample
,
820 bool uses_txf
, union blitter_attrib
*out
)
822 unsigned level
= src
->u
.tex
.first_level
;
823 bool normalized
= !uses_txf
&&
824 src
->target
!= PIPE_TEXTURE_RECT
&&
825 src
->texture
->nr_samples
<= 1;
828 out
->texcoord
.x1
= x1
/ (float)u_minify(src_width0
, level
);
829 out
->texcoord
.y1
= y1
/ (float)u_minify(src_height0
, level
);
830 out
->texcoord
.x2
= x2
/ (float)u_minify(src_width0
, level
);
831 out
->texcoord
.y2
= y2
/ (float)u_minify(src_height0
, level
);
833 out
->texcoord
.x1
= x1
;
834 out
->texcoord
.y1
= y1
;
835 out
->texcoord
.x2
= x2
;
836 out
->texcoord
.y2
= y2
;
843 switch (src
->target
) {
844 case PIPE_TEXTURE_3D
:
849 r
/= u_minify(src
->texture
->depth0
, src
->u
.tex
.first_level
);
855 case PIPE_TEXTURE_1D_ARRAY
:
856 out
->texcoord
.y1
= out
->texcoord
.y2
= layer
;
859 case PIPE_TEXTURE_2D_ARRAY
:
860 out
->texcoord
.z
= layer
;
861 out
->texcoord
.w
= sample
;
864 case PIPE_TEXTURE_CUBE_ARRAY
:
865 out
->texcoord
.w
= (unsigned)layer
/ 6;
868 case PIPE_TEXTURE_2D
:
869 out
->texcoord
.w
= sample
;
876 static void blitter_set_dst_dimensions(struct blitter_context_priv
*ctx
,
877 unsigned width
, unsigned height
)
879 ctx
->dst_width
= width
;
880 ctx
->dst_height
= height
;
883 static void set_texcoords_in_vertices(const union blitter_attrib
*attrib
,
884 float *out
, unsigned stride
)
886 out
[0] = attrib
->texcoord
.x1
;
887 out
[1] = attrib
->texcoord
.y1
;
889 out
[0] = attrib
->texcoord
.x2
;
890 out
[1] = attrib
->texcoord
.y1
;
892 out
[0] = attrib
->texcoord
.x2
;
893 out
[1] = attrib
->texcoord
.y2
;
895 out
[0] = attrib
->texcoord
.x1
;
896 out
[1] = attrib
->texcoord
.y2
;
899 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
900 enum pipe_format src_format
,
901 enum pipe_format dst_format
,
902 enum pipe_texture_target target
,
903 unsigned src_nr_samples
,
904 unsigned dst_nr_samples
,
908 struct pipe_context
*pipe
= ctx
->base
.pipe
;
909 enum tgsi_texture_type tgsi_tex
=
910 util_pipe_tex_to_tgsi_tex(target
, src_nr_samples
);
911 enum tgsi_return_type stype
;
912 enum tgsi_return_type dtype
;
915 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
917 if (util_format_is_pure_uint(src_format
)) {
918 stype
= TGSI_RETURN_TYPE_UINT
;
919 if (util_format_is_pure_uint(dst_format
)) {
920 dtype
= TGSI_RETURN_TYPE_UINT
;
923 assert(util_format_is_pure_sint(dst_format
));
924 dtype
= TGSI_RETURN_TYPE_SINT
;
927 } else if (util_format_is_pure_sint(src_format
)) {
928 stype
= TGSI_RETURN_TYPE_SINT
;
929 if (util_format_is_pure_sint(dst_format
)) {
930 dtype
= TGSI_RETURN_TYPE_SINT
;
933 assert(util_format_is_pure_uint(dst_format
));
934 dtype
= TGSI_RETURN_TYPE_UINT
;
938 assert(!util_format_is_pure_uint(dst_format
) &&
939 !util_format_is_pure_sint(dst_format
));
940 dtype
= stype
= TGSI_RETURN_TYPE_FLOAT
;
944 if (src_nr_samples
> 1) {
947 /* OpenGL requires that integer textures just copy 1 sample instead
950 if (dst_nr_samples
<= 1 &&
951 stype
!= TGSI_RETURN_TYPE_UINT
&&
952 stype
!= TGSI_RETURN_TYPE_SINT
) {
953 /* The destination has one sample, so we'll do color resolve. */
954 unsigned index
= GET_MSAA_RESOLVE_FS_IDX(src_nr_samples
);
958 shader
= &ctx
->fs_resolve
[target
][index
][filter
];
961 assert(!ctx
->cached_all_shaders
);
962 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
963 *shader
= util_make_fs_msaa_resolve_bilinear(pipe
, tgsi_tex
,
968 *shader
= util_make_fs_msaa_resolve(pipe
, tgsi_tex
,
975 /* The destination has multiple samples, we'll do
976 * an MSAA->MSAA copy.
978 shader
= &ctx
->fs_texfetch_col_msaa
[type
][target
];
980 /* Create the fragment shader on-demand. */
982 assert(!ctx
->cached_all_shaders
);
983 *shader
= util_make_fs_blit_msaa_color(pipe
, tgsi_tex
, stype
, dtype
);
992 shader
= &ctx
->fs_texfetch_col
[type
][target
][1];
994 shader
= &ctx
->fs_texfetch_col
[type
][target
][0];
996 /* Create the fragment shader on-demand. */
998 assert(!ctx
->cached_all_shaders
);
999 *shader
= util_make_fragment_tex_shader(pipe
, tgsi_tex
,
1000 TGSI_INTERPOLATE_LINEAR
,
1002 ctx
->has_tex_lz
, use_txf
);
1010 void *blitter_get_fs_pack_color_zs(struct blitter_context_priv
*ctx
,
1011 enum pipe_texture_target target
,
1012 unsigned nr_samples
,
1013 enum pipe_format zs_format
,
1016 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1017 enum tgsi_texture_type tgsi_tex
=
1018 util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1019 int format_index
= zs_format
== PIPE_FORMAT_Z24_UNORM_S8_UINT
? 0 :
1020 zs_format
== PIPE_FORMAT_S8_UINT_Z24_UNORM
? 1 :
1021 zs_format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
? 2 :
1022 zs_format
== PIPE_FORMAT_Z24X8_UNORM
? 3 :
1023 zs_format
== PIPE_FORMAT_X8Z24_UNORM
? 4 : -1;
1025 if (format_index
== -1) {
1030 /* The first 5 shaders pack ZS to color, the last 5 shaders unpack color
1036 void **shader
= &ctx
->fs_pack_color_zs
[tgsi_tex
][format_index
];
1038 /* Create the fragment shader on-demand. */
1040 assert(!ctx
->cached_all_shaders
);
1041 *shader
= util_make_fs_pack_color_zs(pipe
, tgsi_tex
, zs_format
,
1048 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
1049 enum pipe_texture_target target
,
1050 unsigned nr_samples
,
1053 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1055 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1057 if (nr_samples
> 1) {
1058 void **shader
= &ctx
->fs_texfetch_depth_msaa
[target
];
1060 /* Create the fragment shader on-demand. */
1062 enum tgsi_texture_type tgsi_tex
;
1063 assert(!ctx
->cached_all_shaders
);
1064 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1065 *shader
= util_make_fs_blit_msaa_depth(pipe
, tgsi_tex
);
1073 shader
= &ctx
->fs_texfetch_depth
[target
][1];
1075 shader
= &ctx
->fs_texfetch_depth
[target
][0];
1077 /* Create the fragment shader on-demand. */
1079 enum tgsi_texture_type tgsi_tex
;
1080 assert(!ctx
->cached_all_shaders
);
1081 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1082 *shader
= util_make_fs_blit_zs(pipe
, PIPE_MASK_Z
, tgsi_tex
,
1083 ctx
->has_tex_lz
, use_txf
);
1091 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv
*ctx
,
1092 enum pipe_texture_target target
,
1093 unsigned nr_samples
,
1096 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1098 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1100 if (nr_samples
> 1) {
1101 void **shader
= &ctx
->fs_texfetch_depthstencil_msaa
[target
];
1103 /* Create the fragment shader on-demand. */
1105 enum tgsi_texture_type tgsi_tex
;
1106 assert(!ctx
->cached_all_shaders
);
1107 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1108 *shader
= util_make_fs_blit_msaa_depthstencil(pipe
, tgsi_tex
);
1116 shader
= &ctx
->fs_texfetch_depthstencil
[target
][1];
1118 shader
= &ctx
->fs_texfetch_depthstencil
[target
][0];
1120 /* Create the fragment shader on-demand. */
1122 enum tgsi_texture_type tgsi_tex
;
1123 assert(!ctx
->cached_all_shaders
);
1124 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1125 *shader
= util_make_fs_blit_zs(pipe
, PIPE_MASK_ZS
, tgsi_tex
,
1126 ctx
->has_tex_lz
, use_txf
);
1134 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv
*ctx
,
1135 enum pipe_texture_target target
,
1136 unsigned nr_samples
,
1139 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1141 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1143 if (nr_samples
> 1) {
1144 void **shader
= &ctx
->fs_texfetch_stencil_msaa
[target
];
1146 /* Create the fragment shader on-demand. */
1148 enum tgsi_texture_type tgsi_tex
;
1149 assert(!ctx
->cached_all_shaders
);
1150 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1151 *shader
= util_make_fs_blit_msaa_stencil(pipe
, tgsi_tex
);
1159 shader
= &ctx
->fs_texfetch_stencil
[target
][1];
1161 shader
= &ctx
->fs_texfetch_stencil
[target
][0];
1163 /* Create the fragment shader on-demand. */
1165 enum tgsi_texture_type tgsi_tex
;
1166 assert(!ctx
->cached_all_shaders
);
1167 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1168 *shader
= util_make_fs_blit_zs(pipe
, PIPE_MASK_S
, tgsi_tex
,
1169 ctx
->has_tex_lz
, use_txf
);
1178 * Generate and save all fragment shaders that we will ever need for
1179 * blitting. Drivers which use the 'draw' fallbacks will typically use
1180 * this to make sure we generate/use shaders that don't go through the
1181 * draw module's wrapper functions.
1183 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
)
1185 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1186 struct pipe_context
*pipe
= blitter
->pipe
;
1187 struct pipe_screen
*screen
= pipe
->screen
;
1188 unsigned samples
, j
, f
, target
, max_samples
, use_txf
;
1189 bool has_arraytex
, has_cubearraytex
;
1191 max_samples
= ctx
->has_texture_multisample
? 2 : 1;
1192 has_arraytex
= screen
->get_param(screen
,
1193 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
) != 0;
1194 has_cubearraytex
= screen
->get_param(screen
,
1195 PIPE_CAP_CUBE_MAP_ARRAY
) != 0;
1197 /* It only matters if i <= 1 or > 1. */
1198 for (samples
= 1; samples
<= max_samples
; samples
++) {
1199 for (target
= PIPE_TEXTURE_1D
; target
< PIPE_MAX_TEXTURE_TYPES
; target
++) {
1200 for (use_txf
= 0; use_txf
<= ctx
->has_txf
; use_txf
++) {
1201 if (!has_arraytex
&&
1202 (target
== PIPE_TEXTURE_1D_ARRAY
||
1203 target
== PIPE_TEXTURE_2D_ARRAY
)) {
1206 if (!has_cubearraytex
&&
1207 (target
== PIPE_TEXTURE_CUBE_ARRAY
))
1211 (target
!= PIPE_TEXTURE_2D
&&
1212 target
!= PIPE_TEXTURE_2D_ARRAY
))
1215 if (samples
> 1 && use_txf
)
1216 continue; /* TXF is the only option, use_txf has no effect */
1218 /* If samples == 1, the shaders read one texel. If samples >= 1,
1219 * they read one sample.
1221 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
,
1222 PIPE_FORMAT_R32_FLOAT
, target
,
1223 samples
, samples
, 0, use_txf
);
1224 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1225 PIPE_FORMAT_R32_UINT
, target
,
1226 samples
, samples
, 0, use_txf
);
1227 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1228 PIPE_FORMAT_R32_SINT
, target
,
1229 samples
, samples
, 0, use_txf
);
1230 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1231 PIPE_FORMAT_R32_SINT
, target
,
1232 samples
, samples
, 0, use_txf
);
1233 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1234 PIPE_FORMAT_R32_UINT
, target
,
1235 samples
, samples
, 0, use_txf
);
1236 blitter_get_fs_texfetch_depth(ctx
, target
, samples
, use_txf
);
1237 if (ctx
->has_stencil_export
) {
1238 blitter_get_fs_texfetch_depthstencil(ctx
, target
, samples
, use_txf
);
1239 blitter_get_fs_texfetch_stencil(ctx
, target
, samples
, use_txf
);
1245 /* MSAA resolve shaders. */
1246 for (j
= 2; j
< 32; j
++) {
1247 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R32_FLOAT
,
1249 PIPE_BIND_SAMPLER_VIEW
)) {
1253 for (f
= 0; f
< 2; f
++) {
1254 if (f
!= PIPE_TEX_FILTER_NEAREST
&& use_txf
)
1257 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
,
1258 PIPE_FORMAT_R32_FLOAT
, target
,
1260 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1261 PIPE_FORMAT_R32_UINT
, target
,
1263 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1264 PIPE_FORMAT_R32_SINT
, target
,
1272 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
1274 ctx
->fs_write_one_cbuf
=
1275 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1276 TGSI_INTERPOLATE_CONSTANT
, false);
1278 ctx
->fs_write_all_cbufs
=
1279 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1280 TGSI_INTERPOLATE_CONSTANT
, true);
1282 ctx
->cached_all_shaders
= true;
1285 static void blitter_set_common_draw_rect_state(struct blitter_context_priv
*ctx
,
1286 bool scissor
, bool msaa
)
1288 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1290 if (ctx
->base
.saved_num_window_rectangles
)
1291 pipe
->set_window_rectangles(pipe
, false, 0, NULL
);
1293 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_state
[scissor
][msaa
]);
1295 if (ctx
->has_geometry_shader
)
1296 pipe
->bind_gs_state(pipe
, NULL
);
1297 if (ctx
->has_tessellation
) {
1298 pipe
->bind_tcs_state(pipe
, NULL
);
1299 pipe
->bind_tes_state(pipe
, NULL
);
1301 if (ctx
->has_stream_out
)
1302 pipe
->set_stream_output_targets(pipe
, 0, NULL
, NULL
);
1305 static void blitter_draw(struct blitter_context_priv
*ctx
,
1306 void *vertex_elements_cso
,
1307 blitter_get_vs_func get_vs
,
1308 int x1
, int y1
, int x2
, int y2
, float depth
,
1309 unsigned num_instances
)
1311 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1312 struct pipe_vertex_buffer vb
= {0};
1314 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
1316 vb
.stride
= 8 * sizeof(float);
1318 u_upload_data(pipe
->stream_uploader
, 0, sizeof(ctx
->vertices
), 4, ctx
->vertices
,
1319 &vb
.buffer_offset
, &vb
.buffer
.resource
);
1320 if (!vb
.buffer
.resource
)
1322 u_upload_unmap(pipe
->stream_uploader
);
1324 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1325 pipe
->bind_vertex_elements_state(pipe
, vertex_elements_cso
);
1326 pipe
->bind_vs_state(pipe
, get_vs(&ctx
->base
));
1328 if (ctx
->base
.use_index_buffer
) {
1329 /* Note that for V3D,
1330 * dEQP-GLES3.functional.fbo.blit.rect.nearest_consistency_* require
1331 * that the last vert of the two tris be the same.
1333 static uint8_t indices
[6] = { 0, 1, 2, 0, 3, 2 };
1334 util_draw_elements_instanced(pipe
, indices
, 1, 0,
1335 PIPE_PRIM_TRIANGLES
, 0, 6,
1338 util_draw_arrays_instanced(pipe
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
1341 pipe_resource_reference(&vb
.buffer
.resource
, NULL
);
1344 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
1345 void *vertex_elements_cso
,
1346 blitter_get_vs_func get_vs
,
1347 int x1
, int y1
, int x2
, int y2
,
1348 float depth
, unsigned num_instances
,
1349 enum blitter_attrib_type type
,
1350 const union blitter_attrib
*attrib
)
1352 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1356 case UTIL_BLITTER_ATTRIB_COLOR
:
1357 blitter_set_clear_color(ctx
, attrib
->color
);
1360 case UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW
:
1361 for (i
= 0; i
< 4; i
++) {
1362 ctx
->vertices
[i
][1][2] = attrib
->texcoord
.z
;
1363 ctx
->vertices
[i
][1][3] = attrib
->texcoord
.w
;
1366 case UTIL_BLITTER_ATTRIB_TEXCOORD_XY
:
1367 set_texcoords_in_vertices(attrib
, &ctx
->vertices
[0][1][0], 8);
1373 blitter_draw(ctx
, vertex_elements_cso
, get_vs
, x1
, y1
, x2
, y2
, depth
,
1377 static void *get_clear_blend_state(struct blitter_context_priv
*ctx
,
1378 unsigned clear_buffers
)
1380 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1383 clear_buffers
&= PIPE_CLEAR_COLOR
;
1385 /* Return an existing blend state. */
1387 return ctx
->blend
[0][0];
1389 index
= GET_CLEAR_BLEND_STATE_IDX(clear_buffers
);
1391 if (ctx
->blend_clear
[index
])
1392 return ctx
->blend_clear
[index
];
1394 /* Create a new one. */
1396 struct pipe_blend_state blend
= {0};
1399 blend
.independent_blend_enable
= 1;
1401 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1402 if (clear_buffers
& (PIPE_CLEAR_COLOR0
<< i
)) {
1403 blend
.rt
[i
].colormask
= PIPE_MASK_RGBA
;
1407 ctx
->blend_clear
[index
] = pipe
->create_blend_state(pipe
, &blend
);
1409 return ctx
->blend_clear
[index
];
1412 void util_blitter_common_clear_setup(struct blitter_context
*blitter
,
1413 unsigned width
, unsigned height
,
1414 unsigned clear_buffers
,
1415 void *custom_blend
, void *custom_dsa
)
1417 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1418 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1420 util_blitter_set_running_flag(blitter
);
1421 blitter_check_saved_vertex_states(ctx
);
1422 blitter_check_saved_fragment_states(ctx
);
1423 blitter_disable_render_cond(ctx
);
1427 pipe
->bind_blend_state(pipe
, custom_blend
);
1429 pipe
->bind_blend_state(pipe
, get_clear_blend_state(ctx
, clear_buffers
));
1433 pipe
->bind_depth_stencil_alpha_state(pipe
, custom_dsa
);
1434 } else if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1435 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1436 } else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
1437 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1438 } else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
1439 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1441 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1444 pipe
->set_sample_mask(pipe
, ~0);
1445 blitter_set_dst_dimensions(ctx
, width
, height
);
1448 static void util_blitter_clear_custom(struct blitter_context
*blitter
,
1449 unsigned width
, unsigned height
,
1450 unsigned num_layers
,
1451 unsigned clear_buffers
,
1452 const union pipe_color_union
*color
,
1453 double depth
, unsigned stencil
,
1454 void *custom_blend
, void *custom_dsa
)
1456 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1457 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1458 struct pipe_stencil_ref sr
= { { 0 } };
1460 assert(ctx
->has_layered
|| num_layers
<= 1);
1462 util_blitter_common_clear_setup(blitter
, width
, height
, clear_buffers
,
1463 custom_blend
, custom_dsa
);
1465 sr
.ref_value
[0] = stencil
& 0xff;
1466 pipe
->set_stencil_ref(pipe
, &sr
);
1468 bind_fs_write_all_cbufs(ctx
);
1470 union blitter_attrib attrib
;
1471 memcpy(attrib
.color
, color
->ui
, sizeof(color
->ui
));
1473 bool pass_generic
= (clear_buffers
& PIPE_CLEAR_COLOR
) != 0;
1474 enum blitter_attrib_type type
= pass_generic
? UTIL_BLITTER_ATTRIB_COLOR
:
1475 UTIL_BLITTER_ATTRIB_NONE
;
1477 if (num_layers
> 1 && ctx
->has_layered
) {
1478 blitter_get_vs_func get_vs
= get_vs_layered
;
1480 blitter_set_common_draw_rect_state(ctx
, false, false);
1481 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs
,
1482 0, 0, width
, height
,
1483 (float) depth
, num_layers
, type
, &attrib
);
1485 blitter_get_vs_func get_vs
;
1488 get_vs
= get_vs_passthrough_pos_generic
;
1490 get_vs
= get_vs_passthrough_pos
;
1492 blitter_set_common_draw_rect_state(ctx
, false, false);
1493 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs
,
1494 0, 0, width
, height
,
1495 (float) depth
, 1, type
, &attrib
);
1498 util_blitter_restore_vertex_states(blitter
);
1499 util_blitter_restore_fragment_states(blitter
);
1500 util_blitter_restore_render_cond(blitter
);
1501 util_blitter_unset_running_flag(blitter
);
1504 void util_blitter_clear(struct blitter_context
*blitter
,
1505 unsigned width
, unsigned height
, unsigned num_layers
,
1506 unsigned clear_buffers
,
1507 const union pipe_color_union
*color
,
1508 double depth
, unsigned stencil
)
1510 util_blitter_clear_custom(blitter
, width
, height
, num_layers
,
1511 clear_buffers
, color
, depth
, stencil
,
1515 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1516 unsigned width
, unsigned height
,
1517 double depth
, void *custom_dsa
)
1519 static const union pipe_color_union color
;
1520 util_blitter_clear_custom(blitter
, width
, height
, 0, 0, &color
, depth
, 0,
1524 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1525 struct pipe_resource
*dst
,
1529 memset(dst_templ
, 0, sizeof(*dst_templ
));
1530 dst_templ
->format
= util_format_linear(dst
->format
);
1531 dst_templ
->u
.tex
.level
= dstlevel
;
1532 dst_templ
->u
.tex
.first_layer
= dstz
;
1533 dst_templ
->u
.tex
.last_layer
= dstz
;
1536 static struct pipe_surface
*
1537 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
1538 struct pipe_surface
*surf
)
1540 struct pipe_surface dst_templ
;
1542 memset(&dst_templ
, 0, sizeof(dst_templ
));
1543 dst_templ
.format
= surf
->format
;
1544 dst_templ
.u
.tex
.level
= surf
->u
.tex
.level
;
1545 dst_templ
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
+ 1;
1546 dst_templ
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
+ 1;
1548 return pipe
->create_surface(pipe
, surf
->texture
, &dst_templ
);
1551 void util_blitter_default_src_texture(struct blitter_context
*blitter
,
1552 struct pipe_sampler_view
*src_templ
,
1553 struct pipe_resource
*src
,
1556 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1558 memset(src_templ
, 0, sizeof(*src_templ
));
1560 if (ctx
->cube_as_2darray
&&
1561 (src
->target
== PIPE_TEXTURE_CUBE
||
1562 src
->target
== PIPE_TEXTURE_CUBE_ARRAY
))
1563 src_templ
->target
= PIPE_TEXTURE_2D_ARRAY
;
1565 src_templ
->target
= src
->target
;
1567 src_templ
->format
= util_format_linear(src
->format
);
1568 src_templ
->u
.tex
.first_level
= srclevel
;
1569 src_templ
->u
.tex
.last_level
= srclevel
;
1570 src_templ
->u
.tex
.first_layer
= 0;
1571 src_templ
->u
.tex
.last_layer
=
1572 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1573 : (unsigned)(src
->array_size
- 1);
1574 src_templ
->swizzle_r
= PIPE_SWIZZLE_X
;
1575 src_templ
->swizzle_g
= PIPE_SWIZZLE_Y
;
1576 src_templ
->swizzle_b
= PIPE_SWIZZLE_Z
;
1577 src_templ
->swizzle_a
= PIPE_SWIZZLE_W
;
1580 static bool is_blit_generic_supported(struct blitter_context
*blitter
,
1581 const struct pipe_resource
*dst
,
1582 enum pipe_format dst_format
,
1583 const struct pipe_resource
*src
,
1584 enum pipe_format src_format
,
1587 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1588 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1592 const struct util_format_description
*desc
=
1593 util_format_description(dst_format
);
1594 bool dst_has_stencil
= util_format_has_stencil(desc
);
1596 /* Stencil export must be supported for stencil copy. */
1597 if ((mask
& PIPE_MASK_S
) && dst_has_stencil
&&
1598 !ctx
->has_stencil_export
) {
1602 if (dst_has_stencil
|| util_format_has_depth(desc
))
1603 bind
= PIPE_BIND_DEPTH_STENCIL
;
1605 bind
= PIPE_BIND_RENDER_TARGET
;
1607 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1608 dst
->nr_samples
, dst
->nr_storage_samples
,
1615 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1619 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1620 src
->nr_samples
, src
->nr_storage_samples
,
1621 PIPE_BIND_SAMPLER_VIEW
)) {
1625 /* Check stencil sampler support for stencil copy. */
1626 if (mask
& PIPE_MASK_S
) {
1627 if (util_format_has_stencil(util_format_description(src_format
))) {
1628 enum pipe_format stencil_format
=
1629 util_format_stencil_only(src_format
);
1630 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1632 if (stencil_format
!= src_format
&&
1633 !screen
->is_format_supported(screen
, stencil_format
,
1634 src
->target
, src
->nr_samples
,
1635 src
->nr_storage_samples
,
1636 PIPE_BIND_SAMPLER_VIEW
)) {
1646 bool util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1647 const struct pipe_resource
*dst
,
1648 const struct pipe_resource
*src
)
1650 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1651 src
, src
->format
, PIPE_MASK_RGBAZS
);
1654 bool util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1655 const struct pipe_blit_info
*info
)
1657 return is_blit_generic_supported(blitter
,
1658 info
->dst
.resource
, info
->dst
.format
,
1659 info
->src
.resource
, info
->src
.format
,
1663 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1664 struct pipe_resource
*dst
,
1666 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1667 struct pipe_resource
*src
,
1669 const struct pipe_box
*srcbox
)
1671 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1672 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1673 struct pipe_surface
*dst_view
, dst_templ
;
1674 struct pipe_sampler_view src_templ
, *src_view
;
1675 struct pipe_box dstbox
;
1678 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1680 u_box_3d(dstx
, dsty
, dstz
, abs(srcbox
->width
), abs(srcbox
->height
),
1681 abs(srcbox
->depth
), &dstbox
);
1683 /* Initialize the surface. */
1684 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1685 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1687 /* Initialize the sampler view. */
1688 util_blitter_default_src_texture(blitter
, &src_templ
, src
, src_level
);
1689 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1692 util_blitter_blit_generic(blitter
, dst_view
, &dstbox
,
1693 src_view
, srcbox
, src
->width0
, src
->height0
,
1694 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
,
1697 pipe_surface_reference(&dst_view
, NULL
);
1698 pipe_sampler_view_reference(&src_view
, NULL
);
1702 blitter_draw_tex(struct blitter_context_priv
*ctx
,
1703 int dst_x1
, int dst_y1
, int dst_x2
, int dst_y2
,
1704 struct pipe_sampler_view
*src
,
1705 unsigned src_width0
, unsigned src_height0
,
1706 int src_x1
, int src_y1
, int src_x2
, int src_y2
,
1707 float layer
, unsigned sample
,
1708 bool uses_txf
, enum blitter_attrib_type type
)
1710 union blitter_attrib coord
;
1711 blitter_get_vs_func get_vs
= get_vs_passthrough_pos_generic
;
1713 get_texcoords(src
, src_width0
, src_height0
,
1714 src_x1
, src_y1
, src_x2
, src_y2
, layer
, sample
,
1717 if (src
->target
== PIPE_TEXTURE_CUBE
||
1718 src
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
1719 float face_coord
[4][2];
1721 set_texcoords_in_vertices(&coord
, &face_coord
[0][0], 2);
1722 util_map_texcoords2d_onto_cubemap((unsigned)layer
% 6,
1723 /* pointer, stride in floats */
1724 &face_coord
[0][0], 2,
1725 &ctx
->vertices
[0][1][0], 8,
1727 for (unsigned i
= 0; i
< 4; i
++)
1728 ctx
->vertices
[i
][1][3] = coord
.texcoord
.w
;
1730 /* Cubemaps don't use draw_rectangle. */
1731 blitter_draw(ctx
, ctx
->velem_state
, get_vs
,
1732 dst_x1
, dst_y1
, dst_x2
, dst_y2
, 0, 1);
1734 ctx
->base
.draw_rectangle(&ctx
->base
, ctx
->velem_state
, get_vs
,
1735 dst_x1
, dst_y1
, dst_x2
, dst_y2
,
1736 0, 1, type
, &coord
);
1740 static void do_blits(struct blitter_context_priv
*ctx
,
1741 struct pipe_surface
*dst
,
1742 const struct pipe_box
*dstbox
,
1743 struct pipe_sampler_view
*src
,
1744 unsigned src_width0
,
1745 unsigned src_height0
,
1746 const struct pipe_box
*srcbox
,
1750 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1751 unsigned src_samples
= src
->texture
->nr_samples
;
1752 unsigned dst_samples
= dst
->texture
->nr_samples
;
1753 enum pipe_texture_target src_target
= src
->target
;
1754 struct pipe_framebuffer_state fb_state
= {0};
1756 /* Initialize framebuffer state. */
1757 fb_state
.width
= dst
->width
;
1758 fb_state
.height
= dst
->height
;
1759 fb_state
.nr_cbufs
= is_zsbuf
? 0 : 1;
1761 blitter_set_dst_dimensions(ctx
, fb_state
.width
, fb_state
.height
);
1763 if ((src_target
== PIPE_TEXTURE_1D
||
1764 src_target
== PIPE_TEXTURE_2D
||
1765 src_target
== PIPE_TEXTURE_RECT
) &&
1767 /* Set framebuffer state. */
1769 fb_state
.zsbuf
= dst
;
1771 fb_state
.cbufs
[0] = dst
;
1773 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1776 pipe
->set_sample_mask(pipe
, ~0);
1777 blitter_draw_tex(ctx
, dstbox
->x
, dstbox
->y
,
1778 dstbox
->x
+ dstbox
->width
,
1779 dstbox
->y
+ dstbox
->height
,
1780 src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1781 srcbox
->x
+ srcbox
->width
, srcbox
->y
+ srcbox
->height
,
1782 0, 0, uses_txf
, UTIL_BLITTER_ATTRIB_TEXCOORD_XY
);
1784 /* Draw the quad with the generic codepath. */
1786 for (dst_z
= 0; dst_z
< dstbox
->depth
; dst_z
++) {
1787 struct pipe_surface
*old
;
1788 float dst2src_scale
= srcbox
->depth
/ (float)dstbox
->depth
;
1790 /* Scale Z properly if the blit is scaled.
1792 * When downscaling, we want the coordinates centered, so that
1793 * mipmapping works for 3D textures. For example, when generating
1794 * a 4x4x4 level, this wouldn't average the pixels:
1796 * src Z: 0 1 2 3 4 5 6 7
1799 * Because the pixels are not centered below the pixels of the higher
1800 * level. Therefore, we want this:
1801 * src Z: 0 1 2 3 4 5 6 7
1804 * dst_offset defines the offset needed for centering the pixels and
1805 * it works with any scaling (not just 2x).
1807 float dst_offset
= ((srcbox
->depth
- 1) -
1808 (dstbox
->depth
- 1) * dst2src_scale
) * 0.5;
1809 float src_z
= (dst_z
+ dst_offset
) * dst2src_scale
;
1811 /* Set framebuffer state. */
1813 fb_state
.zsbuf
= dst
;
1815 fb_state
.cbufs
[0] = dst
;
1817 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1819 /* See if we need to blit a multisample or singlesample buffer. */
1820 if (src_samples
== dst_samples
&& dst_samples
> 1) {
1822 unsigned i
, max_sample
= dst_samples
- 1;
1824 for (i
= 0; i
<= max_sample
; i
++) {
1825 pipe
->set_sample_mask(pipe
, 1 << i
);
1826 blitter_draw_tex(ctx
, dstbox
->x
, dstbox
->y
,
1827 dstbox
->x
+ dstbox
->width
,
1828 dstbox
->y
+ dstbox
->height
,
1829 src
, src_width0
, src_height0
,
1830 srcbox
->x
, srcbox
->y
,
1831 srcbox
->x
+ srcbox
->width
,
1832 srcbox
->y
+ srcbox
->height
,
1833 srcbox
->z
+ src_z
, i
, uses_txf
,
1834 UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW
);
1837 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1838 pipe
->set_sample_mask(pipe
, ~0);
1839 blitter_draw_tex(ctx
, dstbox
->x
, dstbox
->y
,
1840 dstbox
->x
+ dstbox
->width
,
1841 dstbox
->y
+ dstbox
->height
,
1842 src
, src_width0
, src_height0
,
1843 srcbox
->x
, srcbox
->y
,
1844 srcbox
->x
+ srcbox
->width
,
1845 srcbox
->y
+ srcbox
->height
,
1846 srcbox
->z
+ src_z
, 0, uses_txf
,
1847 UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW
);
1850 /* Get the next surface or (if this is the last iteration)
1851 * just unreference the last one. */
1853 if (dst_z
< dstbox
->depth
-1) {
1854 dst
= util_blitter_get_next_surface_layer(ctx
->base
.pipe
, dst
);
1857 pipe_surface_reference(&old
, NULL
);
1863 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1864 struct pipe_surface
*dst
,
1865 const struct pipe_box
*dstbox
,
1866 struct pipe_sampler_view
*src
,
1867 const struct pipe_box
*srcbox
,
1868 unsigned src_width0
, unsigned src_height0
,
1869 unsigned mask
, unsigned filter
,
1870 const struct pipe_scissor_state
*scissor
,
1873 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1874 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1875 enum pipe_texture_target src_target
= src
->target
;
1876 unsigned src_samples
= src
->texture
->nr_samples
;
1877 unsigned dst_samples
= dst
->texture
->nr_samples
;
1878 void *sampler_state
;
1879 const struct util_format_description
*src_desc
=
1880 util_format_description(src
->format
);
1881 const struct util_format_description
*dst_desc
=
1882 util_format_description(dst
->format
);
1884 bool src_has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1885 bool src_has_depth
= util_format_has_depth(src_desc
);
1886 bool src_has_stencil
= util_format_has_stencil(src_desc
);
1888 bool dst_has_color
= mask
& PIPE_MASK_RGBA
&&
1889 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1890 bool dst_has_depth
= mask
& PIPE_MASK_Z
&&
1891 util_format_has_depth(dst_desc
);
1892 bool dst_has_stencil
= ctx
->has_stencil_export
&&
1893 mask
& PIPE_MASK_S
&&
1894 util_format_has_stencil(dst_desc
);
1896 /* Return if there is nothing to do. */
1897 if (!dst_has_color
&& !dst_has_depth
&& !dst_has_stencil
) {
1901 bool is_scaled
= dstbox
->width
!= abs(srcbox
->width
) ||
1902 dstbox
->height
!= abs(srcbox
->height
);
1904 if (src_has_stencil
|| !is_scaled
)
1905 filter
= PIPE_TEX_FILTER_NEAREST
;
1907 bool use_txf
= false;
1909 /* Don't support scaled blits. The TXF shader uses F2I for rounding. */
1912 filter
== PIPE_TEX_FILTER_NEAREST
&&
1913 src
->target
!= PIPE_TEXTURE_CUBE
&&
1914 src
->target
!= PIPE_TEXTURE_CUBE_ARRAY
) {
1915 int src_width
= u_minify(src_width0
, src
->u
.tex
.first_level
);
1916 int src_height
= u_minify(src_height0
, src
->u
.tex
.first_level
);
1917 int src_depth
= src
->u
.tex
.last_layer
+ 1;
1918 struct pipe_box box
= *srcbox
;
1920 /* Eliminate negative width/height/depth. */
1921 if (box
.width
< 0) {
1925 if (box
.height
< 0) {
1926 box
.y
+= box
.height
;
1929 if (box
.depth
< 0) {
1934 /* See if srcbox is in bounds. TXF doesn't clamp the coordinates. */
1936 box
.x
>= 0 && box
.x
< src_width
&&
1937 box
.y
>= 0 && box
.y
< src_height
&&
1938 box
.z
>= 0 && box
.z
< src_depth
&&
1939 box
.x
+ box
.width
> 0 && box
.x
+ box
.width
<= src_width
&&
1940 box
.y
+ box
.height
> 0 && box
.y
+ box
.height
<= src_height
&&
1941 box
.z
+ box
.depth
> 0 && box
.z
+ box
.depth
<= src_depth
;
1944 /* Check whether the states are properly saved. */
1945 util_blitter_set_running_flag(blitter
);
1946 blitter_check_saved_vertex_states(ctx
);
1947 blitter_check_saved_fragment_states(ctx
);
1948 blitter_check_saved_textures(ctx
);
1949 blitter_check_saved_fb_state(ctx
);
1950 blitter_disable_render_cond(ctx
);
1952 /* Blend, DSA, fragment shader. */
1953 if (dst_has_depth
&& dst_has_stencil
) {
1954 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
1955 pipe
->bind_depth_stencil_alpha_state(pipe
,
1956 ctx
->dsa_write_depth_stencil
);
1957 if (src_has_color
) {
1959 ctx
->bind_fs_state(pipe
,
1960 blitter_get_fs_pack_color_zs(ctx
, src_target
,
1961 src_samples
, dst
->format
, false));
1963 ctx
->bind_fs_state(pipe
,
1964 blitter_get_fs_texfetch_depthstencil(ctx
, src_target
,
1965 src_samples
, use_txf
));
1967 } else if (dst_has_depth
) {
1968 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
1969 pipe
->bind_depth_stencil_alpha_state(pipe
,
1970 ctx
->dsa_write_depth_keep_stencil
);
1971 if (src_has_color
&&
1972 (src
->format
== PIPE_FORMAT_R32_UINT
||
1973 src
->format
== PIPE_FORMAT_R32G32_UINT
)) {
1975 ctx
->bind_fs_state(pipe
,
1976 blitter_get_fs_pack_color_zs(ctx
, src_target
,
1977 src_samples
, dst
->format
, false));
1979 ctx
->bind_fs_state(pipe
,
1980 blitter_get_fs_texfetch_depth(ctx
, src_target
,
1981 src_samples
, use_txf
));
1983 } else if (dst_has_stencil
) {
1984 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
1985 pipe
->bind_depth_stencil_alpha_state(pipe
,
1986 ctx
->dsa_keep_depth_write_stencil
);
1988 assert(src_has_stencil
); /* unpacking from color is unsupported */
1989 ctx
->bind_fs_state(pipe
,
1990 blitter_get_fs_texfetch_stencil(ctx
, src_target
,
1991 src_samples
, use_txf
));
1993 unsigned colormask
= mask
& PIPE_MASK_RGBA
;
1995 pipe
->bind_blend_state(pipe
, ctx
->blend
[colormask
][alpha_blend
]);
1996 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1998 if (src_has_depth
&&
1999 (dst
->format
== PIPE_FORMAT_R32_UINT
||
2000 dst
->format
== PIPE_FORMAT_R32G32_UINT
)) {
2002 ctx
->bind_fs_state(pipe
,
2003 blitter_get_fs_pack_color_zs(ctx
, src_target
,
2004 src_samples
, src
->format
, true));
2006 ctx
->bind_fs_state(pipe
,
2007 blitter_get_fs_texfetch_col(ctx
, src
->format
, dst
->format
, src_target
,
2008 src_samples
, dst_samples
, filter
,
2013 /* Set the linear filter only for scaled color non-MSAA blits. */
2014 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
2015 if (src_target
== PIPE_TEXTURE_RECT
) {
2016 sampler_state
= ctx
->sampler_state_rect_linear
;
2018 sampler_state
= ctx
->sampler_state_linear
;
2021 if (src_target
== PIPE_TEXTURE_RECT
) {
2022 sampler_state
= ctx
->sampler_state_rect
;
2024 sampler_state
= ctx
->sampler_state
;
2029 if (src_has_depth
&& src_has_stencil
&&
2030 (dst_has_color
|| (dst_has_depth
&& dst_has_stencil
))) {
2031 /* Setup two samplers, one for depth and the other one for stencil. */
2032 struct pipe_sampler_view templ
;
2033 struct pipe_sampler_view
*views
[2];
2034 void *samplers
[2] = {sampler_state
, sampler_state
};
2037 templ
.format
= util_format_stencil_only(templ
.format
);
2038 assert(templ
.format
!= PIPE_FORMAT_NONE
);
2041 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
2043 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, views
);
2044 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, samplers
);
2046 pipe_sampler_view_reference(&views
[1], NULL
);
2047 } else if (src_has_stencil
&& dst_has_stencil
) {
2048 /* Set a stencil-only sampler view for it not to sample depth instead. */
2049 struct pipe_sampler_view templ
;
2050 struct pipe_sampler_view
*view
;
2053 templ
.format
= util_format_stencil_only(templ
.format
);
2054 assert(templ
.format
!= PIPE_FORMAT_NONE
);
2056 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
2058 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &view
);
2059 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
2060 0, 1, &sampler_state
);
2062 pipe_sampler_view_reference(&view
, NULL
);
2064 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src
);
2065 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
2066 0, 1, &sampler_state
);
2070 pipe
->set_scissor_states(pipe
, 0, 1, scissor
);
2073 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
, dst_samples
> 1);
2075 do_blits(ctx
, dst
, dstbox
, src
, src_width0
, src_height0
,
2076 srcbox
, dst_has_depth
|| dst_has_stencil
, use_txf
);
2078 util_blitter_restore_vertex_states(blitter
);
2079 util_blitter_restore_fragment_states(blitter
);
2080 util_blitter_restore_textures(blitter
);
2081 util_blitter_restore_fb_state(blitter
);
2083 pipe
->set_scissor_states(pipe
, 0, 1, &ctx
->base
.saved_scissor
);
2085 util_blitter_restore_render_cond(blitter
);
2086 util_blitter_unset_running_flag(blitter
);
2090 util_blitter_blit(struct blitter_context
*blitter
,
2091 const struct pipe_blit_info
*info
)
2093 struct pipe_resource
*dst
= info
->dst
.resource
;
2094 struct pipe_resource
*src
= info
->src
.resource
;
2095 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2096 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2097 struct pipe_surface
*dst_view
, dst_templ
;
2098 struct pipe_sampler_view src_templ
, *src_view
;
2100 /* Initialize the surface. */
2101 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
2103 dst_templ
.format
= info
->dst
.format
;
2104 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
2106 /* Initialize the sampler view. */
2107 util_blitter_default_src_texture(blitter
, &src_templ
, src
, info
->src
.level
);
2108 src_templ
.format
= info
->src
.format
;
2109 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
2112 util_blitter_blit_generic(blitter
, dst_view
, &info
->dst
.box
,
2113 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
2114 info
->mask
, info
->filter
,
2115 info
->scissor_enable
? &info
->scissor
: NULL
,
2118 pipe_surface_reference(&dst_view
, NULL
);
2119 pipe_sampler_view_reference(&src_view
, NULL
);
2122 void util_blitter_generate_mipmap(struct blitter_context
*blitter
,
2123 struct pipe_resource
*tex
,
2124 enum pipe_format format
,
2125 unsigned base_level
, unsigned last_level
,
2126 unsigned first_layer
, unsigned last_layer
)
2128 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2129 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2130 struct pipe_surface dst_templ
, *dst_view
;
2131 struct pipe_sampler_view src_templ
, *src_view
;
2133 void *sampler_state
;
2134 const struct util_format_description
*desc
=
2135 util_format_description(format
);
2137 unsigned target
= tex
->target
;
2139 if (ctx
->cube_as_2darray
&&
2140 (target
== PIPE_TEXTURE_CUBE
|| target
== PIPE_TEXTURE_CUBE_ARRAY
))
2141 target
= PIPE_TEXTURE_2D_ARRAY
;
2143 assert(tex
->nr_samples
<= 1);
2144 /* Disallow stencil formats without depth. */
2145 assert(!util_format_has_stencil(desc
) || util_format_has_depth(desc
));
2147 is_depth
= desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
;
2149 /* Check whether the states are properly saved. */
2150 util_blitter_set_running_flag(blitter
);
2151 blitter_check_saved_vertex_states(ctx
);
2152 blitter_check_saved_fragment_states(ctx
);
2153 blitter_check_saved_textures(ctx
);
2154 blitter_check_saved_fb_state(ctx
);
2155 blitter_disable_render_cond(ctx
);
2159 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
2160 pipe
->bind_depth_stencil_alpha_state(pipe
,
2161 ctx
->dsa_write_depth_keep_stencil
);
2162 ctx
->bind_fs_state(pipe
,
2163 blitter_get_fs_texfetch_depth(ctx
, target
, 1, false));
2165 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2166 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2167 ctx
->bind_fs_state(pipe
,
2168 blitter_get_fs_texfetch_col(ctx
, tex
->format
, tex
->format
, target
,
2169 1, 1, PIPE_TEX_FILTER_LINEAR
, false));
2172 if (target
== PIPE_TEXTURE_RECT
) {
2173 sampler_state
= ctx
->sampler_state_rect_linear
;
2175 sampler_state
= ctx
->sampler_state_linear
;
2177 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
2178 0, 1, &sampler_state
);
2180 blitter_set_common_draw_rect_state(ctx
, false, false);
2182 for (src_level
= base_level
; src_level
< last_level
; src_level
++) {
2183 struct pipe_box dstbox
= {0}, srcbox
= {0};
2184 unsigned dst_level
= src_level
+ 1;
2186 dstbox
.width
= u_minify(tex
->width0
, dst_level
);
2187 dstbox
.height
= u_minify(tex
->height0
, dst_level
);
2189 srcbox
.width
= u_minify(tex
->width0
, src_level
);
2190 srcbox
.height
= u_minify(tex
->height0
, src_level
);
2192 if (target
== PIPE_TEXTURE_3D
) {
2193 dstbox
.depth
= util_num_layers(tex
, dst_level
);
2194 srcbox
.depth
= util_num_layers(tex
, src_level
);
2196 dstbox
.z
= srcbox
.z
= first_layer
;
2197 dstbox
.depth
= srcbox
.depth
= last_layer
- first_layer
+ 1;
2200 /* Initialize the surface. */
2201 util_blitter_default_dst_texture(&dst_templ
, tex
, dst_level
,
2203 dst_templ
.format
= format
;
2204 dst_view
= pipe
->create_surface(pipe
, tex
, &dst_templ
);
2206 /* Initialize the sampler view. */
2207 util_blitter_default_src_texture(blitter
, &src_templ
, tex
, src_level
);
2208 src_templ
.format
= format
;
2209 src_view
= pipe
->create_sampler_view(pipe
, tex
, &src_templ
);
2211 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src_view
);
2213 do_blits(ctx
, dst_view
, &dstbox
, src_view
, tex
->width0
, tex
->height0
,
2214 &srcbox
, is_depth
, false);
2216 pipe_surface_reference(&dst_view
, NULL
);
2217 pipe_sampler_view_reference(&src_view
, NULL
);
2220 util_blitter_restore_vertex_states(blitter
);
2221 util_blitter_restore_fragment_states(blitter
);
2222 util_blitter_restore_textures(blitter
);
2223 util_blitter_restore_fb_state(blitter
);
2224 util_blitter_restore_render_cond(blitter
);
2225 util_blitter_unset_running_flag(blitter
);
2228 /* Clear a region of a color surface to a constant value. */
2229 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
2230 struct pipe_surface
*dstsurf
,
2231 const union pipe_color_union
*color
,
2232 unsigned dstx
, unsigned dsty
,
2233 unsigned width
, unsigned height
)
2235 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2236 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2237 struct pipe_framebuffer_state fb_state
;
2239 unsigned num_layers
;
2241 assert(dstsurf
->texture
);
2242 if (!dstsurf
->texture
)
2245 /* check the saved state */
2246 util_blitter_set_running_flag(blitter
);
2247 blitter_check_saved_vertex_states(ctx
);
2248 blitter_check_saved_fragment_states(ctx
);
2249 blitter_check_saved_fb_state(ctx
);
2250 blitter_disable_render_cond(ctx
);
2253 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2254 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2255 bind_fs_write_one_cbuf(ctx
);
2257 /* set a framebuffer state */
2258 fb_state
.width
= dstsurf
->width
;
2259 fb_state
.height
= dstsurf
->height
;
2260 fb_state
.nr_cbufs
= 1;
2261 fb_state
.cbufs
[0] = dstsurf
;
2263 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2264 pipe
->set_sample_mask(pipe
, ~0);
2265 msaa
= util_framebuffer_get_num_samples(&fb_state
) > 1;
2267 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2269 union blitter_attrib attrib
;
2270 memcpy(attrib
.color
, color
->ui
, sizeof(color
->ui
));
2272 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
2273 if (num_layers
> 1 && ctx
->has_layered
) {
2274 blitter_set_common_draw_rect_state(ctx
, false, msaa
);
2275 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs_layered
,
2276 dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
2277 num_layers
, UTIL_BLITTER_ATTRIB_COLOR
, &attrib
);
2279 blitter_set_common_draw_rect_state(ctx
, false, msaa
);
2280 blitter
->draw_rectangle(blitter
, ctx
->velem_state
,
2281 get_vs_passthrough_pos_generic
,
2282 dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
2283 1, UTIL_BLITTER_ATTRIB_COLOR
, &attrib
);
2286 util_blitter_restore_vertex_states(blitter
);
2287 util_blitter_restore_fragment_states(blitter
);
2288 util_blitter_restore_fb_state(blitter
);
2289 util_blitter_restore_render_cond(blitter
);
2290 util_blitter_unset_running_flag(blitter
);
2293 /* Clear a region of a depth stencil surface. */
2294 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
2295 struct pipe_surface
*dstsurf
,
2296 unsigned clear_flags
,
2299 unsigned dstx
, unsigned dsty
,
2300 unsigned width
, unsigned height
)
2302 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2303 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2304 struct pipe_framebuffer_state fb_state
;
2305 struct pipe_stencil_ref sr
= { { 0 } };
2306 unsigned num_layers
;
2308 assert(dstsurf
->texture
);
2309 if (!dstsurf
->texture
)
2312 /* check the saved state */
2313 util_blitter_set_running_flag(blitter
);
2314 blitter_check_saved_vertex_states(ctx
);
2315 blitter_check_saved_fragment_states(ctx
);
2316 blitter_check_saved_fb_state(ctx
);
2317 blitter_disable_render_cond(ctx
);
2320 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
2321 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
2322 sr
.ref_value
[0] = stencil
& 0xff;
2323 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
2324 pipe
->set_stencil_ref(pipe
, &sr
);
2326 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
2327 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
2329 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
2330 sr
.ref_value
[0] = stencil
& 0xff;
2331 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
2332 pipe
->set_stencil_ref(pipe
, &sr
);
2335 /* hmm that should be illegal probably, or make it a no-op somewhere */
2336 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2340 /* set a framebuffer state */
2341 fb_state
.width
= dstsurf
->width
;
2342 fb_state
.height
= dstsurf
->height
;
2343 fb_state
.nr_cbufs
= 0;
2344 fb_state
.cbufs
[0] = 0;
2345 fb_state
.zsbuf
= dstsurf
;
2346 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2347 pipe
->set_sample_mask(pipe
, ~0);
2349 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2351 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
2352 if (num_layers
> 1 && ctx
->has_layered
) {
2353 blitter_set_common_draw_rect_state(ctx
, false, false);
2354 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs_layered
,
2355 dstx
, dsty
, dstx
+width
, dsty
+height
, depth
,
2356 num_layers
, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2358 blitter_set_common_draw_rect_state(ctx
, false, false);
2359 blitter
->draw_rectangle(blitter
, ctx
->velem_state
,
2360 get_vs_passthrough_pos
,
2361 dstx
, dsty
, dstx
+width
, dsty
+height
, depth
, 1,
2362 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2365 util_blitter_restore_vertex_states(blitter
);
2366 util_blitter_restore_fragment_states(blitter
);
2367 util_blitter_restore_fb_state(blitter
);
2368 util_blitter_restore_render_cond(blitter
);
2369 util_blitter_unset_running_flag(blitter
);
2372 /* draw a rectangle across a region using a custom dsa stage - for r600g */
2373 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
2374 struct pipe_surface
*zsurf
,
2375 struct pipe_surface
*cbsurf
,
2376 unsigned sample_mask
,
2377 void *dsa_stage
, float depth
)
2379 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2380 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2381 struct pipe_framebuffer_state fb_state
;
2383 assert(zsurf
->texture
);
2384 if (!zsurf
->texture
)
2387 /* check the saved state */
2388 util_blitter_set_running_flag(blitter
);
2389 blitter_check_saved_vertex_states(ctx
);
2390 blitter_check_saved_fragment_states(ctx
);
2391 blitter_check_saved_fb_state(ctx
);
2392 blitter_disable_render_cond(ctx
);
2395 pipe
->bind_blend_state(pipe
, cbsurf
? ctx
->blend
[PIPE_MASK_RGBA
][0] :
2397 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
2399 bind_fs_write_one_cbuf(ctx
);
2403 /* set a framebuffer state */
2404 fb_state
.width
= zsurf
->width
;
2405 fb_state
.height
= zsurf
->height
;
2406 fb_state
.nr_cbufs
= 1;
2408 fb_state
.cbufs
[0] = cbsurf
;
2409 fb_state
.nr_cbufs
= 1;
2411 fb_state
.cbufs
[0] = NULL
;
2412 fb_state
.nr_cbufs
= 0;
2414 fb_state
.zsbuf
= zsurf
;
2415 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2416 pipe
->set_sample_mask(pipe
, sample_mask
);
2418 blitter_set_common_draw_rect_state(ctx
, false,
2419 util_framebuffer_get_num_samples(&fb_state
) > 1);
2420 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
2421 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs_passthrough_pos
,
2422 0, 0, zsurf
->width
, zsurf
->height
, depth
,
2423 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2425 util_blitter_restore_vertex_states(blitter
);
2426 util_blitter_restore_fragment_states(blitter
);
2427 util_blitter_restore_fb_state(blitter
);
2428 util_blitter_restore_render_cond(blitter
);
2429 util_blitter_unset_running_flag(blitter
);
2432 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
2433 struct pipe_resource
*dst
,
2435 struct pipe_resource
*src
,
2439 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2440 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2441 struct pipe_vertex_buffer vb
;
2442 struct pipe_stream_output_target
*so_target
;
2443 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2445 if (srcx
>= src
->width0
||
2446 dstx
>= dst
->width0
) {
2449 if (srcx
+ size
> src
->width0
) {
2450 size
= src
->width0
- srcx
;
2452 if (dstx
+ size
> dst
->width0
) {
2453 size
= dst
->width0
- dstx
;
2456 /* Drivers not capable of Stream Out should not call this function
2457 * in the first place. */
2458 assert(ctx
->has_stream_out
);
2460 /* Some alignment is required. */
2461 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
2462 !ctx
->has_stream_out
) {
2463 struct pipe_box box
;
2464 u_box_1d(srcx
, size
, &box
);
2465 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
2469 util_blitter_set_running_flag(blitter
);
2470 blitter_check_saved_vertex_states(ctx
);
2471 blitter_disable_render_cond(ctx
);
2473 vb
.is_user_buffer
= false;
2474 vb
.buffer
.resource
= src
;
2475 vb
.buffer_offset
= srcx
;
2478 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2479 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[0]);
2480 bind_vs_pos_only(ctx
, 1);
2481 if (ctx
->has_geometry_shader
)
2482 pipe
->bind_gs_state(pipe
, NULL
);
2483 if (ctx
->has_tessellation
) {
2484 pipe
->bind_tcs_state(pipe
, NULL
);
2485 pipe
->bind_tes_state(pipe
, NULL
);
2487 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2489 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
2490 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2492 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2494 util_blitter_restore_vertex_states(blitter
);
2495 util_blitter_restore_render_cond(blitter
);
2496 util_blitter_unset_running_flag(blitter
);
2497 pipe_so_target_reference(&so_target
, NULL
);
2500 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
2501 struct pipe_resource
*dst
,
2502 unsigned offset
, unsigned size
,
2503 unsigned num_channels
,
2504 const union pipe_color_union
*clear_value
)
2506 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2507 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2508 struct pipe_vertex_buffer vb
= {0};
2509 struct pipe_stream_output_target
*so_target
= NULL
;
2510 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2512 assert(num_channels
>= 1);
2513 assert(num_channels
<= 4);
2515 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
2517 * R600 uses this to initialize texture resources, so width0 might not be
2518 * what you think it is.
2521 /* Streamout is required. */
2522 if (!ctx
->has_stream_out
) {
2523 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2527 /* Some alignment is required. */
2528 if (offset
% 4 != 0 || size
% 4 != 0) {
2529 assert(!"Bad alignment in util_blitter_clear_buffer()");
2533 u_upload_data(pipe
->stream_uploader
, 0, num_channels
*4, 4, clear_value
,
2534 &vb
.buffer_offset
, &vb
.buffer
.resource
);
2535 if (!vb
.buffer
.resource
)
2540 util_blitter_set_running_flag(blitter
);
2541 blitter_check_saved_vertex_states(ctx
);
2542 blitter_disable_render_cond(ctx
);
2544 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2545 pipe
->bind_vertex_elements_state(pipe
,
2546 ctx
->velem_state_readbuf
[num_channels
-1]);
2547 bind_vs_pos_only(ctx
, num_channels
);
2548 if (ctx
->has_geometry_shader
)
2549 pipe
->bind_gs_state(pipe
, NULL
);
2550 if (ctx
->has_tessellation
) {
2551 pipe
->bind_tcs_state(pipe
, NULL
);
2552 pipe
->bind_tes_state(pipe
, NULL
);
2554 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2556 so_target
= pipe
->create_stream_output_target(pipe
, dst
, offset
, size
);
2557 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2559 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2562 util_blitter_restore_vertex_states(blitter
);
2563 util_blitter_restore_render_cond(blitter
);
2564 util_blitter_unset_running_flag(blitter
);
2565 pipe_so_target_reference(&so_target
, NULL
);
2566 pipe_resource_reference(&vb
.buffer
.resource
, NULL
);
2569 /* probably radeon specific */
2570 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
2571 struct pipe_resource
*dst
,
2574 struct pipe_resource
*src
,
2576 unsigned sample_mask
,
2578 enum pipe_format format
)
2580 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2581 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2582 struct pipe_framebuffer_state fb_state
;
2583 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
2585 util_blitter_set_running_flag(blitter
);
2586 blitter_check_saved_vertex_states(ctx
);
2587 blitter_check_saved_fragment_states(ctx
);
2588 blitter_disable_render_cond(ctx
);
2591 pipe
->bind_blend_state(pipe
, custom_blend
);
2592 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2593 bind_fs_write_one_cbuf(ctx
);
2594 pipe
->set_sample_mask(pipe
, sample_mask
);
2596 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
2597 surf_tmpl
.format
= format
;
2598 surf_tmpl
.u
.tex
.level
= dst_level
;
2599 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
2600 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
2602 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
2604 surf_tmpl
.u
.tex
.level
= 0;
2605 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
2606 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
2608 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
2610 /* set a framebuffer state */
2611 fb_state
.width
= src
->width0
;
2612 fb_state
.height
= src
->height0
;
2613 fb_state
.nr_cbufs
= 2;
2614 fb_state
.cbufs
[0] = srcsurf
;
2615 fb_state
.cbufs
[1] = dstsurf
;
2616 fb_state
.zsbuf
= NULL
;
2617 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2619 blitter_set_common_draw_rect_state(ctx
, false,
2620 util_framebuffer_get_num_samples(&fb_state
) > 1);
2621 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
2622 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs_passthrough_pos
,
2623 0, 0, src
->width0
, src
->height0
,
2624 0, 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2625 util_blitter_restore_fb_state(blitter
);
2626 util_blitter_restore_vertex_states(blitter
);
2627 util_blitter_restore_fragment_states(blitter
);
2628 util_blitter_restore_render_cond(blitter
);
2629 util_blitter_unset_running_flag(blitter
);
2631 pipe_surface_reference(&srcsurf
, NULL
);
2632 pipe_surface_reference(&dstsurf
, NULL
);
2635 void util_blitter_custom_color(struct blitter_context
*blitter
,
2636 struct pipe_surface
*dstsurf
,
2639 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2640 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2641 struct pipe_framebuffer_state fb_state
;
2643 assert(dstsurf
->texture
);
2644 if (!dstsurf
->texture
)
2647 /* check the saved state */
2648 util_blitter_set_running_flag(blitter
);
2649 blitter_check_saved_vertex_states(ctx
);
2650 blitter_check_saved_fragment_states(ctx
);
2651 blitter_check_saved_fb_state(ctx
);
2652 blitter_disable_render_cond(ctx
);
2655 pipe
->bind_blend_state(pipe
, custom_blend
? custom_blend
2656 : ctx
->blend
[PIPE_MASK_RGBA
][0]);
2657 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2658 bind_fs_write_one_cbuf(ctx
);
2659 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2661 /* set a framebuffer state */
2662 fb_state
.width
= dstsurf
->width
;
2663 fb_state
.height
= dstsurf
->height
;
2664 fb_state
.nr_cbufs
= 1;
2665 fb_state
.cbufs
[0] = dstsurf
;
2667 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2668 pipe
->set_sample_mask(pipe
, ~0);
2670 blitter_set_common_draw_rect_state(ctx
, false,
2671 util_framebuffer_get_num_samples(&fb_state
) > 1);
2672 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2673 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs_passthrough_pos
,
2674 0, 0, dstsurf
->width
, dstsurf
->height
,
2675 0, 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2677 util_blitter_restore_vertex_states(blitter
);
2678 util_blitter_restore_fragment_states(blitter
);
2679 util_blitter_restore_fb_state(blitter
);
2680 util_blitter_restore_render_cond(blitter
);
2681 util_blitter_unset_running_flag(blitter
);
2684 static void *get_custom_vs(struct blitter_context
*blitter
)
2686 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2688 return ctx
->custom_vs
;
2692 * Performs a custom blit to the destination surface, using the VS and FS
2695 * Used by vc4 for the 8-bit linear-to-tiled blit.
2697 void util_blitter_custom_shader(struct blitter_context
*blitter
,
2698 struct pipe_surface
*dstsurf
,
2699 void *custom_vs
, void *custom_fs
)
2701 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2702 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2703 struct pipe_framebuffer_state fb_state
;
2705 ctx
->custom_vs
= custom_vs
;
2707 assert(dstsurf
->texture
);
2708 if (!dstsurf
->texture
)
2711 /* check the saved state */
2712 util_blitter_set_running_flag(blitter
);
2713 blitter_check_saved_vertex_states(ctx
);
2714 blitter_check_saved_fragment_states(ctx
);
2715 blitter_check_saved_fb_state(ctx
);
2716 blitter_disable_render_cond(ctx
);
2719 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2720 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2721 pipe
->bind_fs_state(pipe
, custom_fs
);
2722 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2724 /* set a framebuffer state */
2725 fb_state
.width
= dstsurf
->width
;
2726 fb_state
.height
= dstsurf
->height
;
2727 fb_state
.nr_cbufs
= 1;
2728 fb_state
.cbufs
[0] = dstsurf
;
2730 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2731 pipe
->set_sample_mask(pipe
, ~0);
2733 blitter_set_common_draw_rect_state(ctx
, false,
2734 util_framebuffer_get_num_samples(&fb_state
) > 1);
2735 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2736 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_custom_vs
,
2737 0, 0, dstsurf
->width
, dstsurf
->height
,
2738 0, 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2740 util_blitter_restore_vertex_states(blitter
);
2741 util_blitter_restore_fragment_states(blitter
);
2742 util_blitter_restore_fb_state(blitter
);
2743 util_blitter_restore_render_cond(blitter
);
2744 util_blitter_unset_running_flag(blitter
);