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
,
1457 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1458 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1459 struct pipe_stencil_ref sr
= { { 0 } };
1461 assert(ctx
->has_layered
|| num_layers
<= 1);
1463 util_blitter_common_clear_setup(blitter
, width
, height
, clear_buffers
,
1464 custom_blend
, custom_dsa
);
1466 sr
.ref_value
[0] = stencil
& 0xff;
1467 pipe
->set_stencil_ref(pipe
, &sr
);
1469 bind_fs_write_all_cbufs(ctx
);
1471 union blitter_attrib attrib
;
1472 memcpy(attrib
.color
, color
->ui
, sizeof(color
->ui
));
1474 bool pass_generic
= (clear_buffers
& PIPE_CLEAR_COLOR
) != 0;
1475 enum blitter_attrib_type type
= pass_generic
? UTIL_BLITTER_ATTRIB_COLOR
:
1476 UTIL_BLITTER_ATTRIB_NONE
;
1478 if (num_layers
> 1 && ctx
->has_layered
) {
1479 blitter_get_vs_func get_vs
= get_vs_layered
;
1481 blitter_set_common_draw_rect_state(ctx
, false, msaa
);
1482 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs
,
1483 0, 0, width
, height
,
1484 (float) depth
, num_layers
, type
, &attrib
);
1486 blitter_get_vs_func get_vs
;
1489 get_vs
= get_vs_passthrough_pos_generic
;
1491 get_vs
= get_vs_passthrough_pos
;
1493 blitter_set_common_draw_rect_state(ctx
, false, msaa
);
1494 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs
,
1495 0, 0, width
, height
,
1496 (float) depth
, 1, type
, &attrib
);
1499 util_blitter_restore_vertex_states(blitter
);
1500 util_blitter_restore_fragment_states(blitter
);
1501 util_blitter_restore_render_cond(blitter
);
1502 util_blitter_unset_running_flag(blitter
);
1505 void util_blitter_clear(struct blitter_context
*blitter
,
1506 unsigned width
, unsigned height
, unsigned num_layers
,
1507 unsigned clear_buffers
,
1508 const union pipe_color_union
*color
,
1509 double depth
, unsigned stencil
,
1512 util_blitter_clear_custom(blitter
, width
, height
, num_layers
,
1513 clear_buffers
, color
, depth
, stencil
,
1517 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1518 unsigned width
, unsigned height
,
1519 double depth
, void *custom_dsa
)
1521 static const union pipe_color_union color
;
1522 util_blitter_clear_custom(blitter
, width
, height
, 0, 0, &color
, depth
, 0,
1523 NULL
, custom_dsa
, false);
1526 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1527 struct pipe_resource
*dst
,
1531 memset(dst_templ
, 0, sizeof(*dst_templ
));
1532 dst_templ
->format
= util_format_linear(dst
->format
);
1533 dst_templ
->u
.tex
.level
= dstlevel
;
1534 dst_templ
->u
.tex
.first_layer
= dstz
;
1535 dst_templ
->u
.tex
.last_layer
= dstz
;
1538 static struct pipe_surface
*
1539 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
1540 struct pipe_surface
*surf
)
1542 struct pipe_surface dst_templ
;
1544 memset(&dst_templ
, 0, sizeof(dst_templ
));
1545 dst_templ
.format
= surf
->format
;
1546 dst_templ
.u
.tex
.level
= surf
->u
.tex
.level
;
1547 dst_templ
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
+ 1;
1548 dst_templ
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
+ 1;
1550 return pipe
->create_surface(pipe
, surf
->texture
, &dst_templ
);
1553 void util_blitter_default_src_texture(struct blitter_context
*blitter
,
1554 struct pipe_sampler_view
*src_templ
,
1555 struct pipe_resource
*src
,
1558 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1560 memset(src_templ
, 0, sizeof(*src_templ
));
1562 if (ctx
->cube_as_2darray
&&
1563 (src
->target
== PIPE_TEXTURE_CUBE
||
1564 src
->target
== PIPE_TEXTURE_CUBE_ARRAY
))
1565 src_templ
->target
= PIPE_TEXTURE_2D_ARRAY
;
1567 src_templ
->target
= src
->target
;
1569 src_templ
->format
= util_format_linear(src
->format
);
1570 src_templ
->u
.tex
.first_level
= srclevel
;
1571 src_templ
->u
.tex
.last_level
= srclevel
;
1572 src_templ
->u
.tex
.first_layer
= 0;
1573 src_templ
->u
.tex
.last_layer
=
1574 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1575 : (unsigned)(src
->array_size
- 1);
1576 src_templ
->swizzle_r
= PIPE_SWIZZLE_X
;
1577 src_templ
->swizzle_g
= PIPE_SWIZZLE_Y
;
1578 src_templ
->swizzle_b
= PIPE_SWIZZLE_Z
;
1579 src_templ
->swizzle_a
= PIPE_SWIZZLE_W
;
1582 static bool is_blit_generic_supported(struct blitter_context
*blitter
,
1583 const struct pipe_resource
*dst
,
1584 enum pipe_format dst_format
,
1585 const struct pipe_resource
*src
,
1586 enum pipe_format src_format
,
1589 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1590 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1594 const struct util_format_description
*desc
=
1595 util_format_description(dst_format
);
1596 bool dst_has_stencil
= util_format_has_stencil(desc
);
1598 /* Stencil export must be supported for stencil copy. */
1599 if ((mask
& PIPE_MASK_S
) && dst_has_stencil
&&
1600 !ctx
->has_stencil_export
) {
1604 if (dst_has_stencil
|| util_format_has_depth(desc
))
1605 bind
= PIPE_BIND_DEPTH_STENCIL
;
1607 bind
= PIPE_BIND_RENDER_TARGET
;
1609 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1610 dst
->nr_samples
, dst
->nr_storage_samples
,
1617 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1621 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1622 src
->nr_samples
, src
->nr_storage_samples
,
1623 PIPE_BIND_SAMPLER_VIEW
)) {
1627 /* Check stencil sampler support for stencil copy. */
1628 if (mask
& PIPE_MASK_S
) {
1629 if (util_format_has_stencil(util_format_description(src_format
))) {
1630 enum pipe_format stencil_format
=
1631 util_format_stencil_only(src_format
);
1632 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1634 if (stencil_format
!= src_format
&&
1635 !screen
->is_format_supported(screen
, stencil_format
,
1636 src
->target
, src
->nr_samples
,
1637 src
->nr_storage_samples
,
1638 PIPE_BIND_SAMPLER_VIEW
)) {
1648 bool util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1649 const struct pipe_resource
*dst
,
1650 const struct pipe_resource
*src
)
1652 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1653 src
, src
->format
, PIPE_MASK_RGBAZS
);
1656 bool util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1657 const struct pipe_blit_info
*info
)
1659 return is_blit_generic_supported(blitter
,
1660 info
->dst
.resource
, info
->dst
.format
,
1661 info
->src
.resource
, info
->src
.format
,
1665 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1666 struct pipe_resource
*dst
,
1668 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1669 struct pipe_resource
*src
,
1671 const struct pipe_box
*srcbox
)
1673 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1674 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1675 struct pipe_surface
*dst_view
, dst_templ
;
1676 struct pipe_sampler_view src_templ
, *src_view
;
1677 struct pipe_box dstbox
;
1680 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1682 u_box_3d(dstx
, dsty
, dstz
, abs(srcbox
->width
), abs(srcbox
->height
),
1683 abs(srcbox
->depth
), &dstbox
);
1685 /* Initialize the surface. */
1686 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1687 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1689 /* Initialize the sampler view. */
1690 util_blitter_default_src_texture(blitter
, &src_templ
, src
, src_level
);
1691 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1694 util_blitter_blit_generic(blitter
, dst_view
, &dstbox
,
1695 src_view
, srcbox
, src
->width0
, src
->height0
,
1696 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
,
1699 pipe_surface_reference(&dst_view
, NULL
);
1700 pipe_sampler_view_reference(&src_view
, NULL
);
1704 blitter_draw_tex(struct blitter_context_priv
*ctx
,
1705 int dst_x1
, int dst_y1
, int dst_x2
, int dst_y2
,
1706 struct pipe_sampler_view
*src
,
1707 unsigned src_width0
, unsigned src_height0
,
1708 int src_x1
, int src_y1
, int src_x2
, int src_y2
,
1709 float layer
, unsigned sample
,
1710 bool uses_txf
, enum blitter_attrib_type type
)
1712 union blitter_attrib coord
;
1713 blitter_get_vs_func get_vs
= get_vs_passthrough_pos_generic
;
1715 get_texcoords(src
, src_width0
, src_height0
,
1716 src_x1
, src_y1
, src_x2
, src_y2
, layer
, sample
,
1719 if (src
->target
== PIPE_TEXTURE_CUBE
||
1720 src
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
1721 float face_coord
[4][2];
1723 set_texcoords_in_vertices(&coord
, &face_coord
[0][0], 2);
1724 util_map_texcoords2d_onto_cubemap((unsigned)layer
% 6,
1725 /* pointer, stride in floats */
1726 &face_coord
[0][0], 2,
1727 &ctx
->vertices
[0][1][0], 8,
1729 for (unsigned i
= 0; i
< 4; i
++)
1730 ctx
->vertices
[i
][1][3] = coord
.texcoord
.w
;
1732 /* Cubemaps don't use draw_rectangle. */
1733 blitter_draw(ctx
, ctx
->velem_state
, get_vs
,
1734 dst_x1
, dst_y1
, dst_x2
, dst_y2
, 0, 1);
1736 ctx
->base
.draw_rectangle(&ctx
->base
, ctx
->velem_state
, get_vs
,
1737 dst_x1
, dst_y1
, dst_x2
, dst_y2
,
1738 0, 1, type
, &coord
);
1742 static void do_blits(struct blitter_context_priv
*ctx
,
1743 struct pipe_surface
*dst
,
1744 const struct pipe_box
*dstbox
,
1745 struct pipe_sampler_view
*src
,
1746 unsigned src_width0
,
1747 unsigned src_height0
,
1748 const struct pipe_box
*srcbox
,
1752 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1753 unsigned src_samples
= src
->texture
->nr_samples
;
1754 unsigned dst_samples
= dst
->texture
->nr_samples
;
1755 enum pipe_texture_target src_target
= src
->target
;
1756 struct pipe_framebuffer_state fb_state
= {0};
1758 /* Initialize framebuffer state. */
1759 fb_state
.width
= dst
->width
;
1760 fb_state
.height
= dst
->height
;
1761 fb_state
.nr_cbufs
= is_zsbuf
? 0 : 1;
1763 blitter_set_dst_dimensions(ctx
, fb_state
.width
, fb_state
.height
);
1765 if ((src_target
== PIPE_TEXTURE_1D
||
1766 src_target
== PIPE_TEXTURE_2D
||
1767 src_target
== PIPE_TEXTURE_RECT
) &&
1769 /* Set framebuffer state. */
1771 fb_state
.zsbuf
= dst
;
1773 fb_state
.cbufs
[0] = dst
;
1775 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1778 pipe
->set_sample_mask(pipe
, ~0);
1779 blitter_draw_tex(ctx
, dstbox
->x
, dstbox
->y
,
1780 dstbox
->x
+ dstbox
->width
,
1781 dstbox
->y
+ dstbox
->height
,
1782 src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1783 srcbox
->x
+ srcbox
->width
, srcbox
->y
+ srcbox
->height
,
1784 0, 0, uses_txf
, UTIL_BLITTER_ATTRIB_TEXCOORD_XY
);
1786 /* Draw the quad with the generic codepath. */
1788 for (dst_z
= 0; dst_z
< dstbox
->depth
; dst_z
++) {
1789 struct pipe_surface
*old
;
1790 float dst2src_scale
= srcbox
->depth
/ (float)dstbox
->depth
;
1792 /* Scale Z properly if the blit is scaled.
1794 * When downscaling, we want the coordinates centered, so that
1795 * mipmapping works for 3D textures. For example, when generating
1796 * a 4x4x4 level, this wouldn't average the pixels:
1798 * src Z: 0 1 2 3 4 5 6 7
1801 * Because the pixels are not centered below the pixels of the higher
1802 * level. Therefore, we want this:
1803 * src Z: 0 1 2 3 4 5 6 7
1806 * dst_offset defines the offset needed for centering the pixels and
1807 * it works with any scaling (not just 2x).
1809 float dst_offset
= ((srcbox
->depth
- 1) -
1810 (dstbox
->depth
- 1) * dst2src_scale
) * 0.5;
1811 float src_z
= (dst_z
+ dst_offset
) * dst2src_scale
;
1813 /* Set framebuffer state. */
1815 fb_state
.zsbuf
= dst
;
1817 fb_state
.cbufs
[0] = dst
;
1819 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1821 /* See if we need to blit a multisample or singlesample buffer. */
1822 if (src_samples
== dst_samples
&& dst_samples
> 1) {
1824 unsigned i
, max_sample
= dst_samples
- 1;
1826 for (i
= 0; i
<= max_sample
; i
++) {
1827 pipe
->set_sample_mask(pipe
, 1 << i
);
1828 blitter_draw_tex(ctx
, dstbox
->x
, dstbox
->y
,
1829 dstbox
->x
+ dstbox
->width
,
1830 dstbox
->y
+ dstbox
->height
,
1831 src
, src_width0
, src_height0
,
1832 srcbox
->x
, srcbox
->y
,
1833 srcbox
->x
+ srcbox
->width
,
1834 srcbox
->y
+ srcbox
->height
,
1835 srcbox
->z
+ src_z
, i
, uses_txf
,
1836 UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW
);
1839 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1840 pipe
->set_sample_mask(pipe
, ~0);
1841 blitter_draw_tex(ctx
, dstbox
->x
, dstbox
->y
,
1842 dstbox
->x
+ dstbox
->width
,
1843 dstbox
->y
+ dstbox
->height
,
1844 src
, src_width0
, src_height0
,
1845 srcbox
->x
, srcbox
->y
,
1846 srcbox
->x
+ srcbox
->width
,
1847 srcbox
->y
+ srcbox
->height
,
1848 srcbox
->z
+ src_z
, 0, uses_txf
,
1849 UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW
);
1852 /* Get the next surface or (if this is the last iteration)
1853 * just unreference the last one. */
1855 if (dst_z
< dstbox
->depth
-1) {
1856 dst
= util_blitter_get_next_surface_layer(ctx
->base
.pipe
, dst
);
1859 pipe_surface_reference(&old
, NULL
);
1865 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1866 struct pipe_surface
*dst
,
1867 const struct pipe_box
*dstbox
,
1868 struct pipe_sampler_view
*src
,
1869 const struct pipe_box
*srcbox
,
1870 unsigned src_width0
, unsigned src_height0
,
1871 unsigned mask
, unsigned filter
,
1872 const struct pipe_scissor_state
*scissor
,
1875 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1876 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1877 enum pipe_texture_target src_target
= src
->target
;
1878 unsigned src_samples
= src
->texture
->nr_samples
;
1879 unsigned dst_samples
= dst
->texture
->nr_samples
;
1880 void *sampler_state
;
1881 const struct util_format_description
*src_desc
=
1882 util_format_description(src
->format
);
1883 const struct util_format_description
*dst_desc
=
1884 util_format_description(dst
->format
);
1886 bool src_has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1887 bool src_has_depth
= util_format_has_depth(src_desc
);
1888 bool src_has_stencil
= util_format_has_stencil(src_desc
);
1890 bool dst_has_color
= mask
& PIPE_MASK_RGBA
&&
1891 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1892 bool dst_has_depth
= mask
& PIPE_MASK_Z
&&
1893 util_format_has_depth(dst_desc
);
1894 bool dst_has_stencil
= ctx
->has_stencil_export
&&
1895 mask
& PIPE_MASK_S
&&
1896 util_format_has_stencil(dst_desc
);
1898 /* Return if there is nothing to do. */
1899 if (!dst_has_color
&& !dst_has_depth
&& !dst_has_stencil
) {
1903 bool is_scaled
= dstbox
->width
!= abs(srcbox
->width
) ||
1904 dstbox
->height
!= abs(srcbox
->height
);
1906 if (src_has_stencil
|| !is_scaled
)
1907 filter
= PIPE_TEX_FILTER_NEAREST
;
1909 bool use_txf
= false;
1911 /* Don't support scaled blits. The TXF shader uses F2I for rounding. */
1914 filter
== PIPE_TEX_FILTER_NEAREST
&&
1915 src
->target
!= PIPE_TEXTURE_CUBE
&&
1916 src
->target
!= PIPE_TEXTURE_CUBE_ARRAY
) {
1917 int src_width
= u_minify(src_width0
, src
->u
.tex
.first_level
);
1918 int src_height
= u_minify(src_height0
, src
->u
.tex
.first_level
);
1919 int src_depth
= src
->u
.tex
.last_layer
+ 1;
1920 struct pipe_box box
= *srcbox
;
1922 /* Eliminate negative width/height/depth. */
1923 if (box
.width
< 0) {
1927 if (box
.height
< 0) {
1928 box
.y
+= box
.height
;
1931 if (box
.depth
< 0) {
1936 /* See if srcbox is in bounds. TXF doesn't clamp the coordinates. */
1938 box
.x
>= 0 && box
.x
< src_width
&&
1939 box
.y
>= 0 && box
.y
< src_height
&&
1940 box
.z
>= 0 && box
.z
< src_depth
&&
1941 box
.x
+ box
.width
> 0 && box
.x
+ box
.width
<= src_width
&&
1942 box
.y
+ box
.height
> 0 && box
.y
+ box
.height
<= src_height
&&
1943 box
.z
+ box
.depth
> 0 && box
.z
+ box
.depth
<= src_depth
;
1946 /* Check whether the states are properly saved. */
1947 util_blitter_set_running_flag(blitter
);
1948 blitter_check_saved_vertex_states(ctx
);
1949 blitter_check_saved_fragment_states(ctx
);
1950 blitter_check_saved_textures(ctx
);
1951 blitter_check_saved_fb_state(ctx
);
1952 blitter_disable_render_cond(ctx
);
1954 /* Blend, DSA, fragment shader. */
1955 if (dst_has_depth
&& dst_has_stencil
) {
1956 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
1957 pipe
->bind_depth_stencil_alpha_state(pipe
,
1958 ctx
->dsa_write_depth_stencil
);
1959 if (src_has_color
) {
1961 ctx
->bind_fs_state(pipe
,
1962 blitter_get_fs_pack_color_zs(ctx
, src_target
,
1963 src_samples
, dst
->format
, false));
1965 ctx
->bind_fs_state(pipe
,
1966 blitter_get_fs_texfetch_depthstencil(ctx
, src_target
,
1967 src_samples
, use_txf
));
1969 } else if (dst_has_depth
) {
1970 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
1971 pipe
->bind_depth_stencil_alpha_state(pipe
,
1972 ctx
->dsa_write_depth_keep_stencil
);
1973 if (src_has_color
&&
1974 (src
->format
== PIPE_FORMAT_R32_UINT
||
1975 src
->format
== PIPE_FORMAT_R32G32_UINT
)) {
1977 ctx
->bind_fs_state(pipe
,
1978 blitter_get_fs_pack_color_zs(ctx
, src_target
,
1979 src_samples
, dst
->format
, false));
1981 ctx
->bind_fs_state(pipe
,
1982 blitter_get_fs_texfetch_depth(ctx
, src_target
,
1983 src_samples
, use_txf
));
1985 } else if (dst_has_stencil
) {
1986 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
1987 pipe
->bind_depth_stencil_alpha_state(pipe
,
1988 ctx
->dsa_keep_depth_write_stencil
);
1990 assert(src_has_stencil
); /* unpacking from color is unsupported */
1991 ctx
->bind_fs_state(pipe
,
1992 blitter_get_fs_texfetch_stencil(ctx
, src_target
,
1993 src_samples
, use_txf
));
1995 unsigned colormask
= mask
& PIPE_MASK_RGBA
;
1997 pipe
->bind_blend_state(pipe
, ctx
->blend
[colormask
][alpha_blend
]);
1998 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2000 if (src_has_depth
&&
2001 (dst
->format
== PIPE_FORMAT_R32_UINT
||
2002 dst
->format
== PIPE_FORMAT_R32G32_UINT
)) {
2004 ctx
->bind_fs_state(pipe
,
2005 blitter_get_fs_pack_color_zs(ctx
, src_target
,
2006 src_samples
, src
->format
, true));
2008 ctx
->bind_fs_state(pipe
,
2009 blitter_get_fs_texfetch_col(ctx
, src
->format
, dst
->format
, src_target
,
2010 src_samples
, dst_samples
, filter
,
2015 /* Set the linear filter only for scaled color non-MSAA blits. */
2016 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
2017 if (src_target
== PIPE_TEXTURE_RECT
) {
2018 sampler_state
= ctx
->sampler_state_rect_linear
;
2020 sampler_state
= ctx
->sampler_state_linear
;
2023 if (src_target
== PIPE_TEXTURE_RECT
) {
2024 sampler_state
= ctx
->sampler_state_rect
;
2026 sampler_state
= ctx
->sampler_state
;
2031 if (src_has_depth
&& src_has_stencil
&&
2032 (dst_has_color
|| (dst_has_depth
&& dst_has_stencil
))) {
2033 /* Setup two samplers, one for depth and the other one for stencil. */
2034 struct pipe_sampler_view templ
;
2035 struct pipe_sampler_view
*views
[2];
2036 void *samplers
[2] = {sampler_state
, sampler_state
};
2039 templ
.format
= util_format_stencil_only(templ
.format
);
2040 assert(templ
.format
!= PIPE_FORMAT_NONE
);
2043 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
2045 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, views
);
2046 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, samplers
);
2048 pipe_sampler_view_reference(&views
[1], NULL
);
2049 } else if (src_has_stencil
&& dst_has_stencil
) {
2050 /* Set a stencil-only sampler view for it not to sample depth instead. */
2051 struct pipe_sampler_view templ
;
2052 struct pipe_sampler_view
*view
;
2055 templ
.format
= util_format_stencil_only(templ
.format
);
2056 assert(templ
.format
!= PIPE_FORMAT_NONE
);
2058 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
2060 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &view
);
2061 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
2062 0, 1, &sampler_state
);
2064 pipe_sampler_view_reference(&view
, NULL
);
2066 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src
);
2067 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
2068 0, 1, &sampler_state
);
2072 pipe
->set_scissor_states(pipe
, 0, 1, scissor
);
2075 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
, dst_samples
> 1);
2077 do_blits(ctx
, dst
, dstbox
, src
, src_width0
, src_height0
,
2078 srcbox
, dst_has_depth
|| dst_has_stencil
, use_txf
);
2080 util_blitter_restore_vertex_states(blitter
);
2081 util_blitter_restore_fragment_states(blitter
);
2082 util_blitter_restore_textures(blitter
);
2083 util_blitter_restore_fb_state(blitter
);
2085 pipe
->set_scissor_states(pipe
, 0, 1, &ctx
->base
.saved_scissor
);
2087 util_blitter_restore_render_cond(blitter
);
2088 util_blitter_unset_running_flag(blitter
);
2092 util_blitter_blit(struct blitter_context
*blitter
,
2093 const struct pipe_blit_info
*info
)
2095 struct pipe_resource
*dst
= info
->dst
.resource
;
2096 struct pipe_resource
*src
= info
->src
.resource
;
2097 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2098 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2099 struct pipe_surface
*dst_view
, dst_templ
;
2100 struct pipe_sampler_view src_templ
, *src_view
;
2102 /* Initialize the surface. */
2103 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
2105 dst_templ
.format
= info
->dst
.format
;
2106 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
2108 /* Initialize the sampler view. */
2109 util_blitter_default_src_texture(blitter
, &src_templ
, src
, info
->src
.level
);
2110 src_templ
.format
= info
->src
.format
;
2111 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
2114 util_blitter_blit_generic(blitter
, dst_view
, &info
->dst
.box
,
2115 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
2116 info
->mask
, info
->filter
,
2117 info
->scissor_enable
? &info
->scissor
: NULL
,
2120 pipe_surface_reference(&dst_view
, NULL
);
2121 pipe_sampler_view_reference(&src_view
, NULL
);
2124 void util_blitter_generate_mipmap(struct blitter_context
*blitter
,
2125 struct pipe_resource
*tex
,
2126 enum pipe_format format
,
2127 unsigned base_level
, unsigned last_level
,
2128 unsigned first_layer
, unsigned last_layer
)
2130 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2131 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2132 struct pipe_surface dst_templ
, *dst_view
;
2133 struct pipe_sampler_view src_templ
, *src_view
;
2135 void *sampler_state
;
2136 const struct util_format_description
*desc
=
2137 util_format_description(format
);
2139 unsigned target
= tex
->target
;
2141 if (ctx
->cube_as_2darray
&&
2142 (target
== PIPE_TEXTURE_CUBE
|| target
== PIPE_TEXTURE_CUBE_ARRAY
))
2143 target
= PIPE_TEXTURE_2D_ARRAY
;
2145 assert(tex
->nr_samples
<= 1);
2146 /* Disallow stencil formats without depth. */
2147 assert(!util_format_has_stencil(desc
) || util_format_has_depth(desc
));
2149 is_depth
= desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
;
2151 /* Check whether the states are properly saved. */
2152 util_blitter_set_running_flag(blitter
);
2153 blitter_check_saved_vertex_states(ctx
);
2154 blitter_check_saved_fragment_states(ctx
);
2155 blitter_check_saved_textures(ctx
);
2156 blitter_check_saved_fb_state(ctx
);
2157 blitter_disable_render_cond(ctx
);
2161 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
2162 pipe
->bind_depth_stencil_alpha_state(pipe
,
2163 ctx
->dsa_write_depth_keep_stencil
);
2164 ctx
->bind_fs_state(pipe
,
2165 blitter_get_fs_texfetch_depth(ctx
, target
, 1, false));
2167 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2168 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2169 ctx
->bind_fs_state(pipe
,
2170 blitter_get_fs_texfetch_col(ctx
, tex
->format
, tex
->format
, target
,
2171 1, 1, PIPE_TEX_FILTER_LINEAR
, false));
2174 if (target
== PIPE_TEXTURE_RECT
) {
2175 sampler_state
= ctx
->sampler_state_rect_linear
;
2177 sampler_state
= ctx
->sampler_state_linear
;
2179 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
2180 0, 1, &sampler_state
);
2182 blitter_set_common_draw_rect_state(ctx
, false, false);
2184 for (src_level
= base_level
; src_level
< last_level
; src_level
++) {
2185 struct pipe_box dstbox
= {0}, srcbox
= {0};
2186 unsigned dst_level
= src_level
+ 1;
2188 dstbox
.width
= u_minify(tex
->width0
, dst_level
);
2189 dstbox
.height
= u_minify(tex
->height0
, dst_level
);
2191 srcbox
.width
= u_minify(tex
->width0
, src_level
);
2192 srcbox
.height
= u_minify(tex
->height0
, src_level
);
2194 if (target
== PIPE_TEXTURE_3D
) {
2195 dstbox
.depth
= util_num_layers(tex
, dst_level
);
2196 srcbox
.depth
= util_num_layers(tex
, src_level
);
2198 dstbox
.z
= srcbox
.z
= first_layer
;
2199 dstbox
.depth
= srcbox
.depth
= last_layer
- first_layer
+ 1;
2202 /* Initialize the surface. */
2203 util_blitter_default_dst_texture(&dst_templ
, tex
, dst_level
,
2205 dst_templ
.format
= format
;
2206 dst_view
= pipe
->create_surface(pipe
, tex
, &dst_templ
);
2208 /* Initialize the sampler view. */
2209 util_blitter_default_src_texture(blitter
, &src_templ
, tex
, src_level
);
2210 src_templ
.format
= format
;
2211 src_view
= pipe
->create_sampler_view(pipe
, tex
, &src_templ
);
2213 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src_view
);
2215 do_blits(ctx
, dst_view
, &dstbox
, src_view
, tex
->width0
, tex
->height0
,
2216 &srcbox
, is_depth
, false);
2218 pipe_surface_reference(&dst_view
, NULL
);
2219 pipe_sampler_view_reference(&src_view
, NULL
);
2222 util_blitter_restore_vertex_states(blitter
);
2223 util_blitter_restore_fragment_states(blitter
);
2224 util_blitter_restore_textures(blitter
);
2225 util_blitter_restore_fb_state(blitter
);
2226 util_blitter_restore_render_cond(blitter
);
2227 util_blitter_unset_running_flag(blitter
);
2230 /* Clear a region of a color surface to a constant value. */
2231 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
2232 struct pipe_surface
*dstsurf
,
2233 const union pipe_color_union
*color
,
2234 unsigned dstx
, unsigned dsty
,
2235 unsigned width
, unsigned height
)
2237 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2238 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2239 struct pipe_framebuffer_state fb_state
;
2241 unsigned num_layers
;
2243 assert(dstsurf
->texture
);
2244 if (!dstsurf
->texture
)
2247 /* check the saved state */
2248 util_blitter_set_running_flag(blitter
);
2249 blitter_check_saved_vertex_states(ctx
);
2250 blitter_check_saved_fragment_states(ctx
);
2251 blitter_check_saved_fb_state(ctx
);
2252 blitter_disable_render_cond(ctx
);
2255 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2256 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2257 bind_fs_write_one_cbuf(ctx
);
2259 /* set a framebuffer state */
2260 fb_state
.width
= dstsurf
->width
;
2261 fb_state
.height
= dstsurf
->height
;
2262 fb_state
.nr_cbufs
= 1;
2263 fb_state
.cbufs
[0] = dstsurf
;
2265 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2266 pipe
->set_sample_mask(pipe
, ~0);
2267 msaa
= util_framebuffer_get_num_samples(&fb_state
) > 1;
2269 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2271 union blitter_attrib attrib
;
2272 memcpy(attrib
.color
, color
->ui
, sizeof(color
->ui
));
2274 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
2275 if (num_layers
> 1 && ctx
->has_layered
) {
2276 blitter_set_common_draw_rect_state(ctx
, false, msaa
);
2277 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs_layered
,
2278 dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
2279 num_layers
, UTIL_BLITTER_ATTRIB_COLOR
, &attrib
);
2281 blitter_set_common_draw_rect_state(ctx
, false, msaa
);
2282 blitter
->draw_rectangle(blitter
, ctx
->velem_state
,
2283 get_vs_passthrough_pos_generic
,
2284 dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
2285 1, UTIL_BLITTER_ATTRIB_COLOR
, &attrib
);
2288 util_blitter_restore_vertex_states(blitter
);
2289 util_blitter_restore_fragment_states(blitter
);
2290 util_blitter_restore_fb_state(blitter
);
2291 util_blitter_restore_render_cond(blitter
);
2292 util_blitter_unset_running_flag(blitter
);
2295 /* Clear a region of a depth stencil surface. */
2296 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
2297 struct pipe_surface
*dstsurf
,
2298 unsigned clear_flags
,
2301 unsigned dstx
, unsigned dsty
,
2302 unsigned width
, unsigned height
)
2304 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2305 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2306 struct pipe_framebuffer_state fb_state
;
2307 struct pipe_stencil_ref sr
= { { 0 } };
2308 unsigned num_layers
;
2310 assert(dstsurf
->texture
);
2311 if (!dstsurf
->texture
)
2314 /* check the saved state */
2315 util_blitter_set_running_flag(blitter
);
2316 blitter_check_saved_vertex_states(ctx
);
2317 blitter_check_saved_fragment_states(ctx
);
2318 blitter_check_saved_fb_state(ctx
);
2319 blitter_disable_render_cond(ctx
);
2322 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
2323 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
2324 sr
.ref_value
[0] = stencil
& 0xff;
2325 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
2326 pipe
->set_stencil_ref(pipe
, &sr
);
2328 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
2329 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
2331 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
2332 sr
.ref_value
[0] = stencil
& 0xff;
2333 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
2334 pipe
->set_stencil_ref(pipe
, &sr
);
2337 /* hmm that should be illegal probably, or make it a no-op somewhere */
2338 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2342 /* set a framebuffer state */
2343 fb_state
.width
= dstsurf
->width
;
2344 fb_state
.height
= dstsurf
->height
;
2345 fb_state
.nr_cbufs
= 0;
2346 fb_state
.cbufs
[0] = 0;
2347 fb_state
.zsbuf
= dstsurf
;
2348 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2349 pipe
->set_sample_mask(pipe
, ~0);
2351 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2353 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
2354 if (num_layers
> 1 && ctx
->has_layered
) {
2355 blitter_set_common_draw_rect_state(ctx
, false, false);
2356 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs_layered
,
2357 dstx
, dsty
, dstx
+width
, dsty
+height
, depth
,
2358 num_layers
, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2360 blitter_set_common_draw_rect_state(ctx
, false, false);
2361 blitter
->draw_rectangle(blitter
, ctx
->velem_state
,
2362 get_vs_passthrough_pos
,
2363 dstx
, dsty
, dstx
+width
, dsty
+height
, depth
, 1,
2364 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2367 util_blitter_restore_vertex_states(blitter
);
2368 util_blitter_restore_fragment_states(blitter
);
2369 util_blitter_restore_fb_state(blitter
);
2370 util_blitter_restore_render_cond(blitter
);
2371 util_blitter_unset_running_flag(blitter
);
2374 /* draw a rectangle across a region using a custom dsa stage - for r600g */
2375 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
2376 struct pipe_surface
*zsurf
,
2377 struct pipe_surface
*cbsurf
,
2378 unsigned sample_mask
,
2379 void *dsa_stage
, float depth
)
2381 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2382 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2383 struct pipe_framebuffer_state fb_state
;
2385 assert(zsurf
->texture
);
2386 if (!zsurf
->texture
)
2389 /* check the saved state */
2390 util_blitter_set_running_flag(blitter
);
2391 blitter_check_saved_vertex_states(ctx
);
2392 blitter_check_saved_fragment_states(ctx
);
2393 blitter_check_saved_fb_state(ctx
);
2394 blitter_disable_render_cond(ctx
);
2397 pipe
->bind_blend_state(pipe
, cbsurf
? ctx
->blend
[PIPE_MASK_RGBA
][0] :
2399 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
2401 bind_fs_write_one_cbuf(ctx
);
2405 /* set a framebuffer state */
2406 fb_state
.width
= zsurf
->width
;
2407 fb_state
.height
= zsurf
->height
;
2408 fb_state
.nr_cbufs
= 1;
2410 fb_state
.cbufs
[0] = cbsurf
;
2411 fb_state
.nr_cbufs
= 1;
2413 fb_state
.cbufs
[0] = NULL
;
2414 fb_state
.nr_cbufs
= 0;
2416 fb_state
.zsbuf
= zsurf
;
2417 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2418 pipe
->set_sample_mask(pipe
, sample_mask
);
2420 blitter_set_common_draw_rect_state(ctx
, false,
2421 util_framebuffer_get_num_samples(&fb_state
) > 1);
2422 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
2423 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs_passthrough_pos
,
2424 0, 0, zsurf
->width
, zsurf
->height
, depth
,
2425 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2427 util_blitter_restore_vertex_states(blitter
);
2428 util_blitter_restore_fragment_states(blitter
);
2429 util_blitter_restore_fb_state(blitter
);
2430 util_blitter_restore_render_cond(blitter
);
2431 util_blitter_unset_running_flag(blitter
);
2434 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
2435 struct pipe_resource
*dst
,
2437 struct pipe_resource
*src
,
2441 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2442 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2443 struct pipe_vertex_buffer vb
;
2444 struct pipe_stream_output_target
*so_target
;
2445 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2447 if (srcx
>= src
->width0
||
2448 dstx
>= dst
->width0
) {
2451 if (srcx
+ size
> src
->width0
) {
2452 size
= src
->width0
- srcx
;
2454 if (dstx
+ size
> dst
->width0
) {
2455 size
= dst
->width0
- dstx
;
2458 /* Drivers not capable of Stream Out should not call this function
2459 * in the first place. */
2460 assert(ctx
->has_stream_out
);
2462 /* Some alignment is required. */
2463 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
2464 !ctx
->has_stream_out
) {
2465 struct pipe_box box
;
2466 u_box_1d(srcx
, size
, &box
);
2467 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
2471 util_blitter_set_running_flag(blitter
);
2472 blitter_check_saved_vertex_states(ctx
);
2473 blitter_disable_render_cond(ctx
);
2475 vb
.is_user_buffer
= false;
2476 vb
.buffer
.resource
= src
;
2477 vb
.buffer_offset
= srcx
;
2480 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2481 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[0]);
2482 bind_vs_pos_only(ctx
, 1);
2483 if (ctx
->has_geometry_shader
)
2484 pipe
->bind_gs_state(pipe
, NULL
);
2485 if (ctx
->has_tessellation
) {
2486 pipe
->bind_tcs_state(pipe
, NULL
);
2487 pipe
->bind_tes_state(pipe
, NULL
);
2489 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2491 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
2492 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2494 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2496 util_blitter_restore_vertex_states(blitter
);
2497 util_blitter_restore_render_cond(blitter
);
2498 util_blitter_unset_running_flag(blitter
);
2499 pipe_so_target_reference(&so_target
, NULL
);
2502 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
2503 struct pipe_resource
*dst
,
2504 unsigned offset
, unsigned size
,
2505 unsigned num_channels
,
2506 const union pipe_color_union
*clear_value
)
2508 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2509 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2510 struct pipe_vertex_buffer vb
= {0};
2511 struct pipe_stream_output_target
*so_target
= NULL
;
2512 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2514 assert(num_channels
>= 1);
2515 assert(num_channels
<= 4);
2517 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
2519 * R600 uses this to initialize texture resources, so width0 might not be
2520 * what you think it is.
2523 /* Streamout is required. */
2524 if (!ctx
->has_stream_out
) {
2525 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2529 /* Some alignment is required. */
2530 if (offset
% 4 != 0 || size
% 4 != 0) {
2531 assert(!"Bad alignment in util_blitter_clear_buffer()");
2535 u_upload_data(pipe
->stream_uploader
, 0, num_channels
*4, 4, clear_value
,
2536 &vb
.buffer_offset
, &vb
.buffer
.resource
);
2537 if (!vb
.buffer
.resource
)
2542 util_blitter_set_running_flag(blitter
);
2543 blitter_check_saved_vertex_states(ctx
);
2544 blitter_disable_render_cond(ctx
);
2546 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2547 pipe
->bind_vertex_elements_state(pipe
,
2548 ctx
->velem_state_readbuf
[num_channels
-1]);
2549 bind_vs_pos_only(ctx
, num_channels
);
2550 if (ctx
->has_geometry_shader
)
2551 pipe
->bind_gs_state(pipe
, NULL
);
2552 if (ctx
->has_tessellation
) {
2553 pipe
->bind_tcs_state(pipe
, NULL
);
2554 pipe
->bind_tes_state(pipe
, NULL
);
2556 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2558 so_target
= pipe
->create_stream_output_target(pipe
, dst
, offset
, size
);
2559 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2561 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2564 util_blitter_restore_vertex_states(blitter
);
2565 util_blitter_restore_render_cond(blitter
);
2566 util_blitter_unset_running_flag(blitter
);
2567 pipe_so_target_reference(&so_target
, NULL
);
2568 pipe_resource_reference(&vb
.buffer
.resource
, NULL
);
2571 /* probably radeon specific */
2572 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
2573 struct pipe_resource
*dst
,
2576 struct pipe_resource
*src
,
2578 unsigned sample_mask
,
2580 enum pipe_format format
)
2582 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2583 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2584 struct pipe_framebuffer_state fb_state
;
2585 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
2587 util_blitter_set_running_flag(blitter
);
2588 blitter_check_saved_vertex_states(ctx
);
2589 blitter_check_saved_fragment_states(ctx
);
2590 blitter_disable_render_cond(ctx
);
2593 pipe
->bind_blend_state(pipe
, custom_blend
);
2594 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2595 bind_fs_write_one_cbuf(ctx
);
2596 pipe
->set_sample_mask(pipe
, sample_mask
);
2598 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
2599 surf_tmpl
.format
= format
;
2600 surf_tmpl
.u
.tex
.level
= dst_level
;
2601 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
2602 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
2604 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
2606 surf_tmpl
.u
.tex
.level
= 0;
2607 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
2608 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
2610 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
2612 /* set a framebuffer state */
2613 fb_state
.width
= src
->width0
;
2614 fb_state
.height
= src
->height0
;
2615 fb_state
.nr_cbufs
= 2;
2616 fb_state
.cbufs
[0] = srcsurf
;
2617 fb_state
.cbufs
[1] = dstsurf
;
2618 fb_state
.zsbuf
= NULL
;
2619 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2621 blitter_set_common_draw_rect_state(ctx
, false,
2622 util_framebuffer_get_num_samples(&fb_state
) > 1);
2623 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
2624 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs_passthrough_pos
,
2625 0, 0, src
->width0
, src
->height0
,
2626 0, 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2627 util_blitter_restore_fb_state(blitter
);
2628 util_blitter_restore_vertex_states(blitter
);
2629 util_blitter_restore_fragment_states(blitter
);
2630 util_blitter_restore_render_cond(blitter
);
2631 util_blitter_unset_running_flag(blitter
);
2633 pipe_surface_reference(&srcsurf
, NULL
);
2634 pipe_surface_reference(&dstsurf
, NULL
);
2637 void util_blitter_custom_color(struct blitter_context
*blitter
,
2638 struct pipe_surface
*dstsurf
,
2641 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2642 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2643 struct pipe_framebuffer_state fb_state
;
2645 assert(dstsurf
->texture
);
2646 if (!dstsurf
->texture
)
2649 /* check the saved state */
2650 util_blitter_set_running_flag(blitter
);
2651 blitter_check_saved_vertex_states(ctx
);
2652 blitter_check_saved_fragment_states(ctx
);
2653 blitter_check_saved_fb_state(ctx
);
2654 blitter_disable_render_cond(ctx
);
2657 pipe
->bind_blend_state(pipe
, custom_blend
? custom_blend
2658 : ctx
->blend
[PIPE_MASK_RGBA
][0]);
2659 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2660 bind_fs_write_one_cbuf(ctx
);
2661 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2663 /* set a framebuffer state */
2664 fb_state
.width
= dstsurf
->width
;
2665 fb_state
.height
= dstsurf
->height
;
2666 fb_state
.nr_cbufs
= 1;
2667 fb_state
.cbufs
[0] = dstsurf
;
2669 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2670 pipe
->set_sample_mask(pipe
, ~0);
2672 blitter_set_common_draw_rect_state(ctx
, false,
2673 util_framebuffer_get_num_samples(&fb_state
) > 1);
2674 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2675 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs_passthrough_pos
,
2676 0, 0, dstsurf
->width
, dstsurf
->height
,
2677 0, 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2679 util_blitter_restore_vertex_states(blitter
);
2680 util_blitter_restore_fragment_states(blitter
);
2681 util_blitter_restore_fb_state(blitter
);
2682 util_blitter_restore_render_cond(blitter
);
2683 util_blitter_unset_running_flag(blitter
);
2686 static void *get_custom_vs(struct blitter_context
*blitter
)
2688 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2690 return ctx
->custom_vs
;
2694 * Performs a custom blit to the destination surface, using the VS and FS
2697 * Used by vc4 for the 8-bit linear-to-tiled blit.
2699 void util_blitter_custom_shader(struct blitter_context
*blitter
,
2700 struct pipe_surface
*dstsurf
,
2701 void *custom_vs
, void *custom_fs
)
2703 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2704 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2705 struct pipe_framebuffer_state fb_state
;
2707 ctx
->custom_vs
= custom_vs
;
2709 assert(dstsurf
->texture
);
2710 if (!dstsurf
->texture
)
2713 /* check the saved state */
2714 util_blitter_set_running_flag(blitter
);
2715 blitter_check_saved_vertex_states(ctx
);
2716 blitter_check_saved_fragment_states(ctx
);
2717 blitter_check_saved_fb_state(ctx
);
2718 blitter_disable_render_cond(ctx
);
2721 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2722 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2723 pipe
->bind_fs_state(pipe
, custom_fs
);
2724 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2726 /* set a framebuffer state */
2727 fb_state
.width
= dstsurf
->width
;
2728 fb_state
.height
= dstsurf
->height
;
2729 fb_state
.nr_cbufs
= 1;
2730 fb_state
.cbufs
[0] = dstsurf
;
2732 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2733 pipe
->set_sample_mask(pipe
, ~0);
2735 blitter_set_common_draw_rect_state(ctx
, false,
2736 util_framebuffer_get_num_samples(&fb_state
) > 1);
2737 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2738 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_custom_vs
,
2739 0, 0, dstsurf
->width
, dstsurf
->height
,
2740 0, 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2742 util_blitter_restore_vertex_states(blitter
);
2743 util_blitter_restore_fragment_states(blitter
);
2744 util_blitter_restore_fb_state(blitter
);
2745 util_blitter_restore_render_cond(blitter
);
2746 util_blitter_unset_running_flag(blitter
);