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];
106 void *blend
[PIPE_MASK_RGBA
+1][2]; /**< blend state with writemask */
107 void *blend_clear
[GET_CLEAR_BLEND_STATE_IDX(PIPE_CLEAR_COLOR
)+1];
109 /* Depth stencil alpha state. */
110 void *dsa_write_depth_stencil
;
111 void *dsa_write_depth_keep_stencil
;
112 void *dsa_keep_depth_stencil
;
113 void *dsa_keep_depth_write_stencil
;
115 /* Vertex elements states. */
117 void *velem_state_readbuf
[4]; /**< X, XY, XYZ, XYZW */
121 void *sampler_state_linear
;
122 void *sampler_state_rect
;
123 void *sampler_state_rect_linear
;
125 /* Rasterizer state. */
126 void *rs_state
[2][2]; /**< [scissor][msaa] */
127 void *rs_discard_state
;
129 /* Destination surface dimensions. */
135 bool has_geometry_shader
;
136 bool has_tessellation
;
139 bool has_stencil_export
;
140 bool has_texture_multisample
;
143 bool cube_as_2darray
;
144 bool cached_all_shaders
;
146 /* The Draw module overrides these functions.
147 * Always create the blitter before Draw. */
148 void (*bind_fs_state
)(struct pipe_context
*, void *);
149 void (*delete_fs_state
)(struct pipe_context
*, void *);
152 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
)
154 struct blitter_context_priv
*ctx
;
155 struct pipe_blend_state blend
;
156 struct pipe_depth_stencil_alpha_state dsa
;
157 struct pipe_rasterizer_state rs_state
;
158 struct pipe_sampler_state sampler_state
;
159 struct pipe_vertex_element velem
[2];
162 ctx
= CALLOC_STRUCT(blitter_context_priv
);
166 ctx
->base
.pipe
= pipe
;
167 ctx
->base
.draw_rectangle
= util_blitter_draw_rectangle
;
169 ctx
->bind_fs_state
= pipe
->bind_fs_state
;
170 ctx
->delete_fs_state
= pipe
->delete_fs_state
;
172 /* init state objects for them to be considered invalid */
173 ctx
->base
.saved_blend_state
= INVALID_PTR
;
174 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
175 ctx
->base
.saved_rs_state
= INVALID_PTR
;
176 ctx
->base
.saved_fs
= INVALID_PTR
;
177 ctx
->base
.saved_vs
= INVALID_PTR
;
178 ctx
->base
.saved_gs
= INVALID_PTR
;
179 ctx
->base
.saved_velem_state
= INVALID_PTR
;
180 ctx
->base
.saved_fb_state
.nr_cbufs
= ~0;
181 ctx
->base
.saved_num_sampler_views
= ~0;
182 ctx
->base
.saved_num_sampler_states
= ~0;
183 ctx
->base
.saved_num_so_targets
= ~0;
185 ctx
->has_geometry_shader
=
186 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
187 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
189 ctx
->has_tessellation
=
190 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_TESS_CTRL
,
191 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
193 ctx
->has_stream_out
=
194 pipe
->screen
->get_param(pipe
->screen
,
195 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0;
197 ctx
->has_stencil_export
=
198 pipe
->screen
->get_param(pipe
->screen
,
199 PIPE_CAP_SHADER_STENCIL_EXPORT
);
201 ctx
->has_texture_multisample
=
202 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TEXTURE_MULTISAMPLE
);
204 ctx
->has_tex_lz
= pipe
->screen
->get_param(pipe
->screen
,
205 PIPE_CAP_TGSI_TEX_TXF_LZ
);
206 ctx
->has_txf
= pipe
->screen
->get_param(pipe
->screen
,
207 PIPE_CAP_GLSL_FEATURE_LEVEL
) > 130;
208 ctx
->cube_as_2darray
= pipe
->screen
->get_param(pipe
->screen
,
209 PIPE_CAP_SAMPLER_VIEW_TARGET
);
211 /* blend state objects */
212 memset(&blend
, 0, sizeof(blend
));
214 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
215 for (j
= 0; j
< 2; j
++) {
216 memset(&blend
.rt
[0], 0, sizeof(blend
.rt
[0]));
217 blend
.rt
[0].colormask
= i
;
219 blend
.rt
[0].blend_enable
= 1;
220 blend
.rt
[0].rgb_func
= PIPE_BLEND_ADD
;
221 blend
.rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_SRC_ALPHA
;
222 blend
.rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
223 blend
.rt
[0].alpha_func
= PIPE_BLEND_ADD
;
224 blend
.rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_SRC_ALPHA
;
225 blend
.rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
227 ctx
->blend
[i
][j
] = pipe
->create_blend_state(pipe
, &blend
);
231 /* depth stencil alpha state objects */
232 memset(&dsa
, 0, sizeof(dsa
));
233 ctx
->dsa_keep_depth_stencil
=
234 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
236 dsa
.depth
.enabled
= 1;
237 dsa
.depth
.writemask
= 1;
238 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
239 ctx
->dsa_write_depth_keep_stencil
=
240 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
242 dsa
.stencil
[0].enabled
= 1;
243 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
244 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
245 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
246 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
247 dsa
.stencil
[0].valuemask
= 0xff;
248 dsa
.stencil
[0].writemask
= 0xff;
249 ctx
->dsa_write_depth_stencil
=
250 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
252 dsa
.depth
.enabled
= 0;
253 dsa
.depth
.writemask
= 0;
254 ctx
->dsa_keep_depth_write_stencil
=
255 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
258 memset(&sampler_state
, 0, sizeof(sampler_state
));
259 sampler_state
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
260 sampler_state
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
261 sampler_state
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
262 sampler_state
.normalized_coords
= 1;
263 ctx
->sampler_state
= pipe
->create_sampler_state(pipe
, &sampler_state
);
264 sampler_state
.normalized_coords
= 0;
265 ctx
->sampler_state_rect
= pipe
->create_sampler_state(pipe
, &sampler_state
);
267 sampler_state
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
268 sampler_state
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
269 sampler_state
.normalized_coords
= 1;
270 ctx
->sampler_state_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
271 sampler_state
.normalized_coords
= 0;
272 ctx
->sampler_state_rect_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
274 /* rasterizer state */
275 memset(&rs_state
, 0, sizeof(rs_state
));
276 rs_state
.cull_face
= PIPE_FACE_NONE
;
277 rs_state
.half_pixel_center
= 1;
278 rs_state
.bottom_edge_rule
= 1;
279 rs_state
.flatshade
= 1;
280 rs_state
.depth_clip_near
= 1;
281 rs_state
.depth_clip_far
= 1;
283 unsigned scissor
, msaa
;
284 for (scissor
= 0; scissor
< 2; scissor
++) {
285 for (msaa
= 0; msaa
< 2; msaa
++) {
286 rs_state
.scissor
= scissor
;
287 rs_state
.multisample
= msaa
;
288 ctx
->rs_state
[scissor
][msaa
] =
289 pipe
->create_rasterizer_state(pipe
, &rs_state
);
293 if (ctx
->has_stream_out
) {
294 rs_state
.scissor
= rs_state
.multisample
= 0;
295 rs_state
.rasterizer_discard
= 1;
296 ctx
->rs_discard_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
299 ctx
->base
.cb_slot
= 0; /* 0 for now */
300 ctx
->base
.vb_slot
= 0; /* 0 for now */
302 /* vertex elements states */
303 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
304 for (i
= 0; i
< 2; i
++) {
305 velem
[i
].src_offset
= i
* 4 * sizeof(float);
306 velem
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
307 velem
[i
].vertex_buffer_index
= ctx
->base
.vb_slot
;
309 ctx
->velem_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
311 if (ctx
->has_stream_out
) {
312 static enum pipe_format formats
[4] = {
313 PIPE_FORMAT_R32_UINT
,
314 PIPE_FORMAT_R32G32_UINT
,
315 PIPE_FORMAT_R32G32B32_UINT
,
316 PIPE_FORMAT_R32G32B32A32_UINT
319 for (i
= 0; i
< 4; i
++) {
320 velem
[0].src_format
= formats
[i
];
321 velem
[0].vertex_buffer_index
= ctx
->base
.vb_slot
;
322 ctx
->velem_state_readbuf
[i
] =
323 pipe
->create_vertex_elements_state(pipe
, 1, &velem
[0]);
328 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_INSTANCEID
) &&
329 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT
);
331 /* set invariant vertex coordinates */
332 for (i
= 0; i
< 4; i
++)
333 ctx
->vertices
[i
][0][3] = 1; /*v.w*/
338 static void bind_vs_pos_only(struct blitter_context_priv
*ctx
,
339 unsigned num_so_channels
)
341 struct pipe_context
*pipe
= ctx
->base
.pipe
;
342 int index
= num_so_channels
? num_so_channels
- 1 : 0;
344 if (!ctx
->vs_pos_only
[index
]) {
345 struct pipe_stream_output_info so
;
346 static const enum tgsi_semantic semantic_names
[] =
347 { TGSI_SEMANTIC_POSITION
};
348 const uint semantic_indices
[] = { 0 };
350 memset(&so
, 0, sizeof(so
));
352 so
.output
[0].num_components
= num_so_channels
;
353 so
.stride
[0] = num_so_channels
;
355 ctx
->vs_pos_only
[index
] =
356 util_make_vertex_passthrough_shader_with_so(pipe
, 1, semantic_names
,
357 semantic_indices
, false,
361 pipe
->bind_vs_state(pipe
, ctx
->vs_pos_only
[index
]);
364 static void *get_vs_passthrough_pos_generic(struct blitter_context
*blitter
)
366 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
367 struct pipe_context
*pipe
= ctx
->base
.pipe
;
370 static const enum tgsi_semantic semantic_names
[] =
371 { TGSI_SEMANTIC_POSITION
, TGSI_SEMANTIC_GENERIC
};
372 const uint semantic_indices
[] = { 0, 0 };
374 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
375 semantic_indices
, false);
380 static void *get_vs_passthrough_pos(struct blitter_context
*blitter
)
382 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
383 struct pipe_context
*pipe
= ctx
->base
.pipe
;
385 if (!ctx
->vs_nogeneric
) {
386 static const enum tgsi_semantic semantic_names
[] =
387 { TGSI_SEMANTIC_POSITION
};
388 const uint semantic_indices
[] = { 0 };
391 util_make_vertex_passthrough_shader(pipe
, 1,
393 semantic_indices
, false);
395 return ctx
->vs_nogeneric
;
398 static void *get_vs_layered(struct blitter_context
*blitter
)
400 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
401 struct pipe_context
*pipe
= ctx
->base
.pipe
;
403 if (!ctx
->vs_layered
) {
404 ctx
->vs_layered
= util_make_layered_clear_vertex_shader(pipe
);
406 return ctx
->vs_layered
;
409 static void bind_fs_empty(struct blitter_context_priv
*ctx
)
411 struct pipe_context
*pipe
= ctx
->base
.pipe
;
413 if (!ctx
->fs_empty
) {
414 assert(!ctx
->cached_all_shaders
);
415 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
418 ctx
->bind_fs_state(pipe
, ctx
->fs_empty
);
421 static void bind_fs_write_one_cbuf(struct blitter_context_priv
*ctx
)
423 struct pipe_context
*pipe
= ctx
->base
.pipe
;
425 if (!ctx
->fs_write_one_cbuf
) {
426 assert(!ctx
->cached_all_shaders
);
427 ctx
->fs_write_one_cbuf
=
428 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
429 TGSI_INTERPOLATE_CONSTANT
, false);
432 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
435 static void bind_fs_write_all_cbufs(struct blitter_context_priv
*ctx
)
437 struct pipe_context
*pipe
= ctx
->base
.pipe
;
439 if (!ctx
->fs_write_all_cbufs
) {
440 assert(!ctx
->cached_all_shaders
);
441 ctx
->fs_write_all_cbufs
=
442 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
443 TGSI_INTERPOLATE_CONSTANT
, true);
446 ctx
->bind_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
449 void util_blitter_destroy(struct blitter_context
*blitter
)
451 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
452 struct pipe_context
*pipe
= blitter
->pipe
;
455 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++)
456 for (j
= 0; j
< 2; j
++)
457 pipe
->delete_blend_state(pipe
, ctx
->blend
[i
][j
]);
459 for (i
= 0; i
< ARRAY_SIZE(ctx
->blend_clear
); i
++) {
460 if (ctx
->blend_clear
[i
])
461 pipe
->delete_blend_state(pipe
, ctx
->blend_clear
[i
]);
463 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
464 pipe
->delete_depth_stencil_alpha_state(pipe
,
465 ctx
->dsa_write_depth_keep_stencil
);
466 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
467 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
469 unsigned scissor
, msaa
;
470 for (scissor
= 0; scissor
< 2; scissor
++) {
471 for (msaa
= 0; msaa
< 2; msaa
++) {
472 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
[scissor
][msaa
]);
476 if (ctx
->rs_discard_state
)
477 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_discard_state
);
479 pipe
->delete_vs_state(pipe
, ctx
->vs
);
480 if (ctx
->vs_nogeneric
)
481 pipe
->delete_vs_state(pipe
, ctx
->vs_nogeneric
);
482 for (i
= 0; i
< 4; i
++)
483 if (ctx
->vs_pos_only
[i
])
484 pipe
->delete_vs_state(pipe
, ctx
->vs_pos_only
[i
]);
486 pipe
->delete_vs_state(pipe
, ctx
->vs_layered
);
487 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state
);
488 for (i
= 0; i
< 4; i
++) {
489 if (ctx
->velem_state_readbuf
[i
]) {
490 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[i
]);
494 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
495 for (unsigned type
= 0; type
< ARRAY_SIZE(ctx
->fs_texfetch_col
); ++type
) {
496 for (unsigned inst
= 0; inst
< 2; inst
++) {
497 if (ctx
->fs_texfetch_col
[type
][i
][inst
])
498 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[type
][i
][inst
]);
500 if (ctx
->fs_texfetch_col_msaa
[type
][i
])
501 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col_msaa
[type
][i
]);
504 for (unsigned inst
= 0; inst
< 2; inst
++) {
505 if (ctx
->fs_texfetch_depth
[i
][inst
])
506 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
][inst
]);
507 if (ctx
->fs_texfetch_depthstencil
[i
][inst
])
508 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil
[i
][inst
]);
509 if (ctx
->fs_texfetch_stencil
[i
][inst
])
510 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil
[i
][inst
]);
513 if (ctx
->fs_texfetch_depth_msaa
[i
])
514 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth_msaa
[i
]);
515 if (ctx
->fs_texfetch_depthstencil_msaa
[i
])
516 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil_msaa
[i
]);
517 if (ctx
->fs_texfetch_stencil_msaa
[i
])
518 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil_msaa
[i
]);
520 for (j
= 0; j
< ARRAY_SIZE(ctx
->fs_resolve
[i
]); j
++)
521 for (f
= 0; f
< 2; f
++)
522 if (ctx
->fs_resolve
[i
][j
][f
])
523 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve
[i
][j
][f
]);
527 ctx
->delete_fs_state(pipe
, ctx
->fs_empty
);
528 if (ctx
->fs_write_one_cbuf
)
529 ctx
->delete_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
530 if (ctx
->fs_write_all_cbufs
)
531 ctx
->delete_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
533 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect_linear
);
534 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect
);
535 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_linear
);
536 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
);
540 void util_blitter_set_texture_multisample(struct blitter_context
*blitter
,
543 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
545 ctx
->has_texture_multisample
= supported
;
548 void util_blitter_set_running_flag(struct blitter_context
*blitter
)
550 if (blitter
->running
) {
551 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
554 blitter
->running
= true;
556 blitter
->pipe
->set_active_query_state(blitter
->pipe
, false);
559 void util_blitter_unset_running_flag(struct blitter_context
*blitter
)
561 if (!blitter
->running
) {
562 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
565 blitter
->running
= false;
567 blitter
->pipe
->set_active_query_state(blitter
->pipe
, true);
570 static void blitter_check_saved_vertex_states(MAYBE_UNUSED
struct blitter_context_priv
*ctx
)
572 assert(ctx
->base
.saved_vs
!= INVALID_PTR
);
573 assert(!ctx
->has_geometry_shader
|| ctx
->base
.saved_gs
!= INVALID_PTR
);
574 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tcs
!= INVALID_PTR
);
575 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tes
!= INVALID_PTR
);
576 assert(!ctx
->has_stream_out
|| ctx
->base
.saved_num_so_targets
!= ~0u);
577 assert(ctx
->base
.saved_rs_state
!= INVALID_PTR
);
580 void util_blitter_restore_vertex_states(struct blitter_context
*blitter
)
582 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
583 struct pipe_context
*pipe
= ctx
->base
.pipe
;
587 if (ctx
->base
.saved_vertex_buffer
.buffer
.resource
) {
588 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1,
589 &ctx
->base
.saved_vertex_buffer
);
590 pipe_vertex_buffer_unreference(&ctx
->base
.saved_vertex_buffer
);
593 /* Vertex elements. */
594 if (ctx
->base
.saved_velem_state
!= INVALID_PTR
) {
595 pipe
->bind_vertex_elements_state(pipe
, ctx
->base
.saved_velem_state
);
596 ctx
->base
.saved_velem_state
= INVALID_PTR
;
600 pipe
->bind_vs_state(pipe
, ctx
->base
.saved_vs
);
601 ctx
->base
.saved_vs
= INVALID_PTR
;
603 /* Geometry shader. */
604 if (ctx
->has_geometry_shader
) {
605 pipe
->bind_gs_state(pipe
, ctx
->base
.saved_gs
);
606 ctx
->base
.saved_gs
= INVALID_PTR
;
609 if (ctx
->has_tessellation
) {
610 pipe
->bind_tcs_state(pipe
, ctx
->base
.saved_tcs
);
611 pipe
->bind_tes_state(pipe
, ctx
->base
.saved_tes
);
612 ctx
->base
.saved_tcs
= INVALID_PTR
;
613 ctx
->base
.saved_tes
= INVALID_PTR
;
616 /* Stream outputs. */
617 if (ctx
->has_stream_out
) {
618 unsigned offsets
[PIPE_MAX_SO_BUFFERS
];
619 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
620 offsets
[i
] = (unsigned)-1;
621 pipe
->set_stream_output_targets(pipe
,
622 ctx
->base
.saved_num_so_targets
,
623 ctx
->base
.saved_so_targets
, offsets
);
625 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
626 pipe_so_target_reference(&ctx
->base
.saved_so_targets
[i
], NULL
);
628 ctx
->base
.saved_num_so_targets
= ~0;
632 pipe
->bind_rasterizer_state(pipe
, ctx
->base
.saved_rs_state
);
633 ctx
->base
.saved_rs_state
= INVALID_PTR
;
636 static void blitter_check_saved_fragment_states(MAYBE_UNUSED
struct blitter_context_priv
*ctx
)
638 assert(ctx
->base
.saved_fs
!= INVALID_PTR
);
639 assert(ctx
->base
.saved_dsa_state
!= INVALID_PTR
);
640 assert(ctx
->base
.saved_blend_state
!= INVALID_PTR
);
643 void util_blitter_restore_fragment_states(struct blitter_context
*blitter
)
645 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
646 struct pipe_context
*pipe
= ctx
->base
.pipe
;
648 /* Fragment shader. */
649 ctx
->bind_fs_state(pipe
, ctx
->base
.saved_fs
);
650 ctx
->base
.saved_fs
= INVALID_PTR
;
652 /* Depth, stencil, alpha. */
653 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->base
.saved_dsa_state
);
654 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
657 pipe
->bind_blend_state(pipe
, ctx
->base
.saved_blend_state
);
658 ctx
->base
.saved_blend_state
= INVALID_PTR
;
661 if (ctx
->base
.is_sample_mask_saved
) {
662 pipe
->set_sample_mask(pipe
, ctx
->base
.saved_sample_mask
);
663 ctx
->base
.is_sample_mask_saved
= false;
666 /* Miscellaneous states. */
667 /* XXX check whether these are saved and whether they need to be restored
668 * (depending on the operation) */
669 pipe
->set_stencil_ref(pipe
, &ctx
->base
.saved_stencil_ref
);
671 if (!blitter
->skip_viewport_restore
)
672 pipe
->set_viewport_states(pipe
, 0, 1, &ctx
->base
.saved_viewport
);
674 if (blitter
->saved_num_window_rectangles
) {
675 pipe
->set_window_rectangles(pipe
,
676 blitter
->saved_window_rectangles_include
,
677 blitter
->saved_num_window_rectangles
,
678 blitter
->saved_window_rectangles
);
682 static void blitter_check_saved_fb_state(MAYBE_UNUSED
struct blitter_context_priv
*ctx
)
684 assert(ctx
->base
.saved_fb_state
.nr_cbufs
!= (ubyte
) ~0);
687 static void blitter_disable_render_cond(struct blitter_context_priv
*ctx
)
689 struct pipe_context
*pipe
= ctx
->base
.pipe
;
691 if (ctx
->base
.saved_render_cond_query
) {
692 pipe
->render_condition(pipe
, NULL
, false, 0);
696 void util_blitter_restore_render_cond(struct blitter_context
*blitter
)
698 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
699 struct pipe_context
*pipe
= ctx
->base
.pipe
;
701 if (ctx
->base
.saved_render_cond_query
) {
702 pipe
->render_condition(pipe
, ctx
->base
.saved_render_cond_query
,
703 ctx
->base
.saved_render_cond_cond
,
704 ctx
->base
.saved_render_cond_mode
);
705 ctx
->base
.saved_render_cond_query
= NULL
;
709 void util_blitter_restore_fb_state(struct blitter_context
*blitter
)
711 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
712 struct pipe_context
*pipe
= ctx
->base
.pipe
;
714 pipe
->set_framebuffer_state(pipe
, &ctx
->base
.saved_fb_state
);
715 util_unreference_framebuffer_state(&ctx
->base
.saved_fb_state
);
718 static void blitter_check_saved_textures(MAYBE_UNUSED
struct blitter_context_priv
*ctx
)
720 assert(ctx
->base
.saved_num_sampler_states
!= ~0u);
721 assert(ctx
->base
.saved_num_sampler_views
!= ~0u);
724 void util_blitter_restore_textures(struct blitter_context
*blitter
)
726 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
727 struct pipe_context
*pipe
= ctx
->base
.pipe
;
730 /* Fragment sampler states. */
731 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0,
732 ctx
->base
.saved_num_sampler_states
,
733 ctx
->base
.saved_sampler_states
);
735 ctx
->base
.saved_num_sampler_states
= ~0;
737 /* Fragment sampler views. */
738 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
739 ctx
->base
.saved_num_sampler_views
,
740 ctx
->base
.saved_sampler_views
);
742 for (i
= 0; i
< ctx
->base
.saved_num_sampler_views
; i
++)
743 pipe_sampler_view_reference(&ctx
->base
.saved_sampler_views
[i
], NULL
);
745 ctx
->base
.saved_num_sampler_views
= ~0;
748 void util_blitter_restore_constant_buffer_state(struct blitter_context
*blitter
)
750 struct pipe_context
*pipe
= blitter
->pipe
;
752 pipe
->set_constant_buffer(pipe
, PIPE_SHADER_FRAGMENT
, blitter
->cb_slot
,
753 &blitter
->saved_fs_constant_buffer
);
754 pipe_resource_reference(&blitter
->saved_fs_constant_buffer
.buffer
, NULL
);
757 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
758 int x1
, int y1
, int x2
, int y2
,
763 /* set vertex positions */
764 ctx
->vertices
[0][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v0.x*/
765 ctx
->vertices
[0][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v0.y*/
767 ctx
->vertices
[1][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v1.x*/
768 ctx
->vertices
[1][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v1.y*/
770 ctx
->vertices
[2][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v2.x*/
771 ctx
->vertices
[2][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v2.y*/
773 ctx
->vertices
[3][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v3.x*/
774 ctx
->vertices
[3][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v3.y*/
776 for (i
= 0; i
< 4; i
++)
777 ctx
->vertices
[i
][0][2] = depth
; /*z*/
780 struct pipe_viewport_state viewport
;
781 viewport
.scale
[0] = 0.5f
* ctx
->dst_width
;
782 viewport
.scale
[1] = 0.5f
* ctx
->dst_height
;
783 viewport
.scale
[2] = 1.0f
;
784 viewport
.translate
[0] = 0.5f
* ctx
->dst_width
;
785 viewport
.translate
[1] = 0.5f
* ctx
->dst_height
;
786 viewport
.translate
[2] = 0.0f
;
787 ctx
->base
.pipe
->set_viewport_states(ctx
->base
.pipe
, 0, 1, &viewport
);
790 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
791 const float color
[4])
796 for (i
= 0; i
< 4; i
++)
797 memcpy(&ctx
->vertices
[i
][1][0], color
, sizeof(uint32_t) * 4);
799 for (i
= 0; i
< 4; i
++)
800 memset(&ctx
->vertices
[i
][1][0], 0, sizeof(uint32_t) * 4);
804 static void get_texcoords(struct pipe_sampler_view
*src
,
805 unsigned src_width0
, unsigned src_height0
,
806 int x1
, int y1
, int x2
, int y2
,
807 float layer
, unsigned sample
,
808 bool uses_txf
, union blitter_attrib
*out
)
810 unsigned level
= src
->u
.tex
.first_level
;
811 bool normalized
= !uses_txf
&&
812 src
->target
!= PIPE_TEXTURE_RECT
&&
813 src
->texture
->nr_samples
<= 1;
816 out
->texcoord
.x1
= x1
/ (float)u_minify(src_width0
, level
);
817 out
->texcoord
.y1
= y1
/ (float)u_minify(src_height0
, level
);
818 out
->texcoord
.x2
= x2
/ (float)u_minify(src_width0
, level
);
819 out
->texcoord
.y2
= y2
/ (float)u_minify(src_height0
, level
);
821 out
->texcoord
.x1
= x1
;
822 out
->texcoord
.y1
= y1
;
823 out
->texcoord
.x2
= x2
;
824 out
->texcoord
.y2
= y2
;
831 switch (src
->target
) {
832 case PIPE_TEXTURE_3D
:
837 r
/= u_minify(src
->texture
->depth0
, src
->u
.tex
.first_level
);
843 case PIPE_TEXTURE_1D_ARRAY
:
844 out
->texcoord
.y1
= out
->texcoord
.y2
= layer
;
847 case PIPE_TEXTURE_2D_ARRAY
:
848 out
->texcoord
.z
= layer
;
849 out
->texcoord
.w
= sample
;
852 case PIPE_TEXTURE_CUBE_ARRAY
:
853 out
->texcoord
.w
= (unsigned)layer
/ 6;
856 case PIPE_TEXTURE_2D
:
857 out
->texcoord
.w
= sample
;
864 static void blitter_set_dst_dimensions(struct blitter_context_priv
*ctx
,
865 unsigned width
, unsigned height
)
867 ctx
->dst_width
= width
;
868 ctx
->dst_height
= height
;
871 static void set_texcoords_in_vertices(const union blitter_attrib
*attrib
,
872 float *out
, unsigned stride
)
874 out
[0] = attrib
->texcoord
.x1
;
875 out
[1] = attrib
->texcoord
.y1
;
877 out
[0] = attrib
->texcoord
.x2
;
878 out
[1] = attrib
->texcoord
.y1
;
880 out
[0] = attrib
->texcoord
.x2
;
881 out
[1] = attrib
->texcoord
.y2
;
883 out
[0] = attrib
->texcoord
.x1
;
884 out
[1] = attrib
->texcoord
.y2
;
887 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
888 enum pipe_format src_format
,
889 enum pipe_format dst_format
,
890 enum pipe_texture_target target
,
891 unsigned src_nr_samples
,
892 unsigned dst_nr_samples
,
896 struct pipe_context
*pipe
= ctx
->base
.pipe
;
897 enum tgsi_texture_type tgsi_tex
=
898 util_pipe_tex_to_tgsi_tex(target
, src_nr_samples
);
899 enum tgsi_return_type stype
;
900 enum tgsi_return_type dtype
;
903 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
905 if (util_format_is_pure_uint(src_format
)) {
906 stype
= TGSI_RETURN_TYPE_UINT
;
907 if (util_format_is_pure_uint(dst_format
)) {
908 dtype
= TGSI_RETURN_TYPE_UINT
;
911 assert(util_format_is_pure_sint(dst_format
));
912 dtype
= TGSI_RETURN_TYPE_SINT
;
915 } else if (util_format_is_pure_sint(src_format
)) {
916 stype
= TGSI_RETURN_TYPE_SINT
;
917 if (util_format_is_pure_sint(dst_format
)) {
918 dtype
= TGSI_RETURN_TYPE_SINT
;
921 assert(util_format_is_pure_uint(dst_format
));
922 dtype
= TGSI_RETURN_TYPE_UINT
;
926 assert(!util_format_is_pure_uint(dst_format
) &&
927 !util_format_is_pure_sint(dst_format
));
928 dtype
= stype
= TGSI_RETURN_TYPE_FLOAT
;
932 if (src_nr_samples
> 1) {
935 /* OpenGL requires that integer textures just copy 1 sample instead
938 if (dst_nr_samples
<= 1 &&
939 stype
!= TGSI_RETURN_TYPE_UINT
&&
940 stype
!= TGSI_RETURN_TYPE_SINT
) {
941 /* The destination has one sample, so we'll do color resolve. */
942 unsigned index
= GET_MSAA_RESOLVE_FS_IDX(src_nr_samples
);
946 shader
= &ctx
->fs_resolve
[target
][index
][filter
];
949 assert(!ctx
->cached_all_shaders
);
950 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
951 *shader
= util_make_fs_msaa_resolve_bilinear(pipe
, tgsi_tex
,
956 *shader
= util_make_fs_msaa_resolve(pipe
, tgsi_tex
,
963 /* The destination has multiple samples, we'll do
964 * an MSAA->MSAA copy.
966 shader
= &ctx
->fs_texfetch_col_msaa
[type
][target
];
968 /* Create the fragment shader on-demand. */
970 assert(!ctx
->cached_all_shaders
);
971 *shader
= util_make_fs_blit_msaa_color(pipe
, tgsi_tex
, stype
, dtype
);
980 shader
= &ctx
->fs_texfetch_col
[type
][target
][1];
982 shader
= &ctx
->fs_texfetch_col
[type
][target
][0];
984 /* Create the fragment shader on-demand. */
986 assert(!ctx
->cached_all_shaders
);
987 *shader
= util_make_fragment_tex_shader(pipe
, tgsi_tex
,
988 TGSI_INTERPOLATE_LINEAR
,
990 ctx
->has_tex_lz
, use_txf
);
998 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
999 enum pipe_texture_target target
,
1000 unsigned nr_samples
,
1003 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1005 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1007 if (nr_samples
> 1) {
1008 void **shader
= &ctx
->fs_texfetch_depth_msaa
[target
];
1010 /* Create the fragment shader on-demand. */
1012 enum tgsi_texture_type tgsi_tex
;
1013 assert(!ctx
->cached_all_shaders
);
1014 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1015 *shader
= util_make_fs_blit_msaa_depth(pipe
, tgsi_tex
);
1023 shader
= &ctx
->fs_texfetch_depth
[target
][1];
1025 shader
= &ctx
->fs_texfetch_depth
[target
][0];
1027 /* Create the fragment shader on-demand. */
1029 enum tgsi_texture_type tgsi_tex
;
1030 assert(!ctx
->cached_all_shaders
);
1031 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1033 util_make_fragment_tex_shader_writedepth(pipe
, tgsi_tex
,
1034 TGSI_INTERPOLATE_LINEAR
,
1035 ctx
->has_tex_lz
, use_txf
);
1043 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv
*ctx
,
1044 enum pipe_texture_target target
,
1045 unsigned nr_samples
,
1048 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1050 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1052 if (nr_samples
> 1) {
1053 void **shader
= &ctx
->fs_texfetch_depthstencil_msaa
[target
];
1055 /* Create the fragment shader on-demand. */
1057 enum tgsi_texture_type tgsi_tex
;
1058 assert(!ctx
->cached_all_shaders
);
1059 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1060 *shader
= util_make_fs_blit_msaa_depthstencil(pipe
, tgsi_tex
);
1068 shader
= &ctx
->fs_texfetch_depthstencil
[target
][1];
1070 shader
= &ctx
->fs_texfetch_depthstencil
[target
][0];
1072 /* Create the fragment shader on-demand. */
1074 enum tgsi_texture_type tgsi_tex
;
1075 assert(!ctx
->cached_all_shaders
);
1076 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1078 util_make_fragment_tex_shader_writedepthstencil(pipe
, tgsi_tex
,
1079 TGSI_INTERPOLATE_LINEAR
,
1089 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv
*ctx
,
1090 enum pipe_texture_target target
,
1091 unsigned nr_samples
,
1094 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1096 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1098 if (nr_samples
> 1) {
1099 void **shader
= &ctx
->fs_texfetch_stencil_msaa
[target
];
1101 /* Create the fragment shader on-demand. */
1103 enum tgsi_texture_type tgsi_tex
;
1104 assert(!ctx
->cached_all_shaders
);
1105 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1106 *shader
= util_make_fs_blit_msaa_stencil(pipe
, tgsi_tex
);
1114 shader
= &ctx
->fs_texfetch_stencil
[target
][1];
1116 shader
= &ctx
->fs_texfetch_stencil
[target
][0];
1118 /* Create the fragment shader on-demand. */
1120 enum tgsi_texture_type tgsi_tex
;
1121 assert(!ctx
->cached_all_shaders
);
1122 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1124 util_make_fragment_tex_shader_writestencil(pipe
, tgsi_tex
,
1125 TGSI_INTERPOLATE_LINEAR
,
1126 ctx
->has_tex_lz
, use_txf
);
1135 * Generate and save all fragment shaders that we will ever need for
1136 * blitting. Drivers which use the 'draw' fallbacks will typically use
1137 * this to make sure we generate/use shaders that don't go through the
1138 * draw module's wrapper functions.
1140 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
)
1142 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1143 struct pipe_context
*pipe
= blitter
->pipe
;
1144 struct pipe_screen
*screen
= pipe
->screen
;
1145 unsigned samples
, j
, f
, target
, max_samples
, use_txf
;
1146 bool has_arraytex
, has_cubearraytex
;
1148 max_samples
= ctx
->has_texture_multisample
? 2 : 1;
1149 has_arraytex
= screen
->get_param(screen
,
1150 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
) != 0;
1151 has_cubearraytex
= screen
->get_param(screen
,
1152 PIPE_CAP_CUBE_MAP_ARRAY
) != 0;
1154 /* It only matters if i <= 1 or > 1. */
1155 for (samples
= 1; samples
<= max_samples
; samples
++) {
1156 for (target
= PIPE_TEXTURE_1D
; target
< PIPE_MAX_TEXTURE_TYPES
; target
++) {
1157 for (use_txf
= 0; use_txf
<= ctx
->has_txf
; use_txf
++) {
1158 if (!has_arraytex
&&
1159 (target
== PIPE_TEXTURE_1D_ARRAY
||
1160 target
== PIPE_TEXTURE_2D_ARRAY
)) {
1163 if (!has_cubearraytex
&&
1164 (target
== PIPE_TEXTURE_CUBE_ARRAY
))
1168 (target
!= PIPE_TEXTURE_2D
&&
1169 target
!= PIPE_TEXTURE_2D_ARRAY
))
1172 if (samples
> 1 && use_txf
)
1173 continue; /* TXF is the only option, use_txf has no effect */
1175 /* If samples == 1, the shaders read one texel. If samples >= 1,
1176 * they read one sample.
1178 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
,
1179 PIPE_FORMAT_R32_FLOAT
, target
,
1180 samples
, samples
, 0, use_txf
);
1181 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1182 PIPE_FORMAT_R32_UINT
, target
,
1183 samples
, samples
, 0, use_txf
);
1184 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1185 PIPE_FORMAT_R32_SINT
, target
,
1186 samples
, samples
, 0, use_txf
);
1187 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1188 PIPE_FORMAT_R32_SINT
, target
,
1189 samples
, samples
, 0, use_txf
);
1190 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1191 PIPE_FORMAT_R32_UINT
, target
,
1192 samples
, samples
, 0, use_txf
);
1193 blitter_get_fs_texfetch_depth(ctx
, target
, samples
, use_txf
);
1194 if (ctx
->has_stencil_export
) {
1195 blitter_get_fs_texfetch_depthstencil(ctx
, target
, samples
, use_txf
);
1196 blitter_get_fs_texfetch_stencil(ctx
, target
, samples
, use_txf
);
1202 /* MSAA resolve shaders. */
1203 for (j
= 2; j
< 32; j
++) {
1204 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R32_FLOAT
,
1206 PIPE_BIND_SAMPLER_VIEW
)) {
1210 for (f
= 0; f
< 2; f
++) {
1211 if (f
!= PIPE_TEX_FILTER_NEAREST
&& use_txf
)
1214 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
,
1215 PIPE_FORMAT_R32_FLOAT
, target
,
1217 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1218 PIPE_FORMAT_R32_UINT
, target
,
1220 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1221 PIPE_FORMAT_R32_SINT
, target
,
1229 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
1231 ctx
->fs_write_one_cbuf
=
1232 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1233 TGSI_INTERPOLATE_CONSTANT
, false);
1235 ctx
->fs_write_all_cbufs
=
1236 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1237 TGSI_INTERPOLATE_CONSTANT
, true);
1239 ctx
->cached_all_shaders
= true;
1242 static void blitter_set_common_draw_rect_state(struct blitter_context_priv
*ctx
,
1243 bool scissor
, bool msaa
)
1245 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1247 if (ctx
->base
.saved_num_window_rectangles
)
1248 pipe
->set_window_rectangles(pipe
, false, 0, NULL
);
1250 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_state
[scissor
][msaa
]);
1252 if (ctx
->has_geometry_shader
)
1253 pipe
->bind_gs_state(pipe
, NULL
);
1254 if (ctx
->has_tessellation
) {
1255 pipe
->bind_tcs_state(pipe
, NULL
);
1256 pipe
->bind_tes_state(pipe
, NULL
);
1258 if (ctx
->has_stream_out
)
1259 pipe
->set_stream_output_targets(pipe
, 0, NULL
, NULL
);
1262 static void blitter_draw(struct blitter_context_priv
*ctx
,
1263 void *vertex_elements_cso
,
1264 blitter_get_vs_func get_vs
,
1265 int x1
, int y1
, int x2
, int y2
, float depth
,
1266 unsigned num_instances
)
1268 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1269 struct pipe_vertex_buffer vb
= {0};
1271 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
1273 vb
.stride
= 8 * sizeof(float);
1275 u_upload_data(pipe
->stream_uploader
, 0, sizeof(ctx
->vertices
), 4, ctx
->vertices
,
1276 &vb
.buffer_offset
, &vb
.buffer
.resource
);
1277 if (!vb
.buffer
.resource
)
1279 u_upload_unmap(pipe
->stream_uploader
);
1281 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1282 pipe
->bind_vertex_elements_state(pipe
, vertex_elements_cso
);
1283 pipe
->bind_vs_state(pipe
, get_vs(&ctx
->base
));
1285 if (ctx
->base
.use_index_buffer
) {
1286 /* Note that for V3D,
1287 * dEQP-GLES3.functional.fbo.blit.rect.nearest_consistency_* require
1288 * that the last vert of the two tris be the same.
1290 static uint8_t indices
[6] = { 0, 1, 2, 0, 3, 2 };
1291 util_draw_elements_instanced(pipe
, indices
, 1, 0,
1292 PIPE_PRIM_TRIANGLES
, 0, 6,
1295 util_draw_arrays_instanced(pipe
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
1298 pipe_resource_reference(&vb
.buffer
.resource
, NULL
);
1301 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
1302 void *vertex_elements_cso
,
1303 blitter_get_vs_func get_vs
,
1304 int x1
, int y1
, int x2
, int y2
,
1305 float depth
, unsigned num_instances
,
1306 enum blitter_attrib_type type
,
1307 const union blitter_attrib
*attrib
)
1309 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1313 case UTIL_BLITTER_ATTRIB_COLOR
:
1314 blitter_set_clear_color(ctx
, attrib
->color
);
1317 case UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW
:
1318 for (i
= 0; i
< 4; i
++) {
1319 ctx
->vertices
[i
][1][2] = attrib
->texcoord
.z
;
1320 ctx
->vertices
[i
][1][3] = attrib
->texcoord
.w
;
1323 case UTIL_BLITTER_ATTRIB_TEXCOORD_XY
:
1324 set_texcoords_in_vertices(attrib
, &ctx
->vertices
[0][1][0], 8);
1330 blitter_draw(ctx
, vertex_elements_cso
, get_vs
, x1
, y1
, x2
, y2
, depth
,
1334 static void *get_clear_blend_state(struct blitter_context_priv
*ctx
,
1335 unsigned clear_buffers
)
1337 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1340 clear_buffers
&= PIPE_CLEAR_COLOR
;
1342 /* Return an existing blend state. */
1344 return ctx
->blend
[0][0];
1346 index
= GET_CLEAR_BLEND_STATE_IDX(clear_buffers
);
1348 if (ctx
->blend_clear
[index
])
1349 return ctx
->blend_clear
[index
];
1351 /* Create a new one. */
1353 struct pipe_blend_state blend
= {0};
1356 blend
.independent_blend_enable
= 1;
1358 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1359 if (clear_buffers
& (PIPE_CLEAR_COLOR0
<< i
)) {
1360 blend
.rt
[i
].colormask
= PIPE_MASK_RGBA
;
1364 ctx
->blend_clear
[index
] = pipe
->create_blend_state(pipe
, &blend
);
1366 return ctx
->blend_clear
[index
];
1369 void util_blitter_common_clear_setup(struct blitter_context
*blitter
,
1370 unsigned width
, unsigned height
,
1371 unsigned clear_buffers
,
1372 void *custom_blend
, void *custom_dsa
)
1374 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1375 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1377 util_blitter_set_running_flag(blitter
);
1378 blitter_check_saved_vertex_states(ctx
);
1379 blitter_check_saved_fragment_states(ctx
);
1380 blitter_disable_render_cond(ctx
);
1384 pipe
->bind_blend_state(pipe
, custom_blend
);
1386 pipe
->bind_blend_state(pipe
, get_clear_blend_state(ctx
, clear_buffers
));
1390 pipe
->bind_depth_stencil_alpha_state(pipe
, custom_dsa
);
1391 } else if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1392 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1393 } else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
1394 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1395 } else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
1396 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1398 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1401 pipe
->set_sample_mask(pipe
, ~0);
1402 blitter_set_dst_dimensions(ctx
, width
, height
);
1405 static void util_blitter_clear_custom(struct blitter_context
*blitter
,
1406 unsigned width
, unsigned height
,
1407 unsigned num_layers
,
1408 unsigned clear_buffers
,
1409 const union pipe_color_union
*color
,
1410 double depth
, unsigned stencil
,
1411 void *custom_blend
, void *custom_dsa
)
1413 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1414 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1415 struct pipe_stencil_ref sr
= { { 0 } };
1417 assert(ctx
->has_layered
|| num_layers
<= 1);
1419 util_blitter_common_clear_setup(blitter
, width
, height
, clear_buffers
,
1420 custom_blend
, custom_dsa
);
1422 sr
.ref_value
[0] = stencil
& 0xff;
1423 pipe
->set_stencil_ref(pipe
, &sr
);
1425 bind_fs_write_all_cbufs(ctx
);
1427 union blitter_attrib attrib
;
1428 memcpy(attrib
.color
, color
->ui
, sizeof(color
->ui
));
1430 bool pass_generic
= (clear_buffers
& PIPE_CLEAR_COLOR
) != 0;
1431 enum blitter_attrib_type type
= pass_generic
? UTIL_BLITTER_ATTRIB_COLOR
:
1432 UTIL_BLITTER_ATTRIB_NONE
;
1434 if (num_layers
> 1 && ctx
->has_layered
) {
1435 blitter_get_vs_func get_vs
= get_vs_layered
;
1437 blitter_set_common_draw_rect_state(ctx
, false, false);
1438 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs
,
1439 0, 0, width
, height
,
1440 (float) depth
, num_layers
, type
, &attrib
);
1442 blitter_get_vs_func get_vs
;
1445 get_vs
= get_vs_passthrough_pos_generic
;
1447 get_vs
= get_vs_passthrough_pos
;
1449 blitter_set_common_draw_rect_state(ctx
, false, false);
1450 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs
,
1451 0, 0, width
, height
,
1452 (float) depth
, 1, type
, &attrib
);
1455 util_blitter_restore_vertex_states(blitter
);
1456 util_blitter_restore_fragment_states(blitter
);
1457 util_blitter_restore_render_cond(blitter
);
1458 util_blitter_unset_running_flag(blitter
);
1461 void util_blitter_clear(struct blitter_context
*blitter
,
1462 unsigned width
, unsigned height
, unsigned num_layers
,
1463 unsigned clear_buffers
,
1464 const union pipe_color_union
*color
,
1465 double depth
, unsigned stencil
)
1467 util_blitter_clear_custom(blitter
, width
, height
, num_layers
,
1468 clear_buffers
, color
, depth
, stencil
,
1472 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1473 unsigned width
, unsigned height
,
1474 double depth
, void *custom_dsa
)
1476 static const union pipe_color_union color
;
1477 util_blitter_clear_custom(blitter
, width
, height
, 0, 0, &color
, depth
, 0,
1481 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1482 struct pipe_resource
*dst
,
1486 memset(dst_templ
, 0, sizeof(*dst_templ
));
1487 dst_templ
->format
= util_format_linear(dst
->format
);
1488 dst_templ
->u
.tex
.level
= dstlevel
;
1489 dst_templ
->u
.tex
.first_layer
= dstz
;
1490 dst_templ
->u
.tex
.last_layer
= dstz
;
1493 static struct pipe_surface
*
1494 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
1495 struct pipe_surface
*surf
)
1497 struct pipe_surface dst_templ
;
1499 memset(&dst_templ
, 0, sizeof(dst_templ
));
1500 dst_templ
.format
= surf
->format
;
1501 dst_templ
.u
.tex
.level
= surf
->u
.tex
.level
;
1502 dst_templ
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
+ 1;
1503 dst_templ
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
+ 1;
1505 return pipe
->create_surface(pipe
, surf
->texture
, &dst_templ
);
1508 void util_blitter_default_src_texture(struct blitter_context
*blitter
,
1509 struct pipe_sampler_view
*src_templ
,
1510 struct pipe_resource
*src
,
1513 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1515 memset(src_templ
, 0, sizeof(*src_templ
));
1517 if (ctx
->cube_as_2darray
&&
1518 (src
->target
== PIPE_TEXTURE_CUBE
||
1519 src
->target
== PIPE_TEXTURE_CUBE_ARRAY
))
1520 src_templ
->target
= PIPE_TEXTURE_2D_ARRAY
;
1522 src_templ
->target
= src
->target
;
1524 src_templ
->format
= util_format_linear(src
->format
);
1525 src_templ
->u
.tex
.first_level
= srclevel
;
1526 src_templ
->u
.tex
.last_level
= srclevel
;
1527 src_templ
->u
.tex
.first_layer
= 0;
1528 src_templ
->u
.tex
.last_layer
=
1529 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1530 : (unsigned)(src
->array_size
- 1);
1531 src_templ
->swizzle_r
= PIPE_SWIZZLE_X
;
1532 src_templ
->swizzle_g
= PIPE_SWIZZLE_Y
;
1533 src_templ
->swizzle_b
= PIPE_SWIZZLE_Z
;
1534 src_templ
->swizzle_a
= PIPE_SWIZZLE_W
;
1537 static bool is_blit_generic_supported(struct blitter_context
*blitter
,
1538 const struct pipe_resource
*dst
,
1539 enum pipe_format dst_format
,
1540 const struct pipe_resource
*src
,
1541 enum pipe_format src_format
,
1544 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1545 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1549 const struct util_format_description
*desc
=
1550 util_format_description(dst_format
);
1551 bool dst_has_stencil
= util_format_has_stencil(desc
);
1553 /* Stencil export must be supported for stencil copy. */
1554 if ((mask
& PIPE_MASK_S
) && dst_has_stencil
&&
1555 !ctx
->has_stencil_export
) {
1559 if (dst_has_stencil
|| util_format_has_depth(desc
))
1560 bind
= PIPE_BIND_DEPTH_STENCIL
;
1562 bind
= PIPE_BIND_RENDER_TARGET
;
1564 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1565 dst
->nr_samples
, dst
->nr_storage_samples
,
1572 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1576 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1577 src
->nr_samples
, src
->nr_storage_samples
,
1578 PIPE_BIND_SAMPLER_VIEW
)) {
1582 /* Check stencil sampler support for stencil copy. */
1583 if (mask
& PIPE_MASK_S
) {
1584 if (util_format_has_stencil(util_format_description(src_format
))) {
1585 enum pipe_format stencil_format
=
1586 util_format_stencil_only(src_format
);
1587 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1589 if (stencil_format
!= src_format
&&
1590 !screen
->is_format_supported(screen
, stencil_format
,
1591 src
->target
, src
->nr_samples
,
1592 src
->nr_storage_samples
,
1593 PIPE_BIND_SAMPLER_VIEW
)) {
1603 bool util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1604 const struct pipe_resource
*dst
,
1605 const struct pipe_resource
*src
)
1607 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1608 src
, src
->format
, PIPE_MASK_RGBAZS
);
1611 bool util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1612 const struct pipe_blit_info
*info
)
1614 return is_blit_generic_supported(blitter
,
1615 info
->dst
.resource
, info
->dst
.format
,
1616 info
->src
.resource
, info
->src
.format
,
1620 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1621 struct pipe_resource
*dst
,
1623 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1624 struct pipe_resource
*src
,
1626 const struct pipe_box
*srcbox
)
1628 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1629 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1630 struct pipe_surface
*dst_view
, dst_templ
;
1631 struct pipe_sampler_view src_templ
, *src_view
;
1632 struct pipe_box dstbox
;
1635 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1637 u_box_3d(dstx
, dsty
, dstz
, abs(srcbox
->width
), abs(srcbox
->height
),
1638 abs(srcbox
->depth
), &dstbox
);
1640 /* Initialize the surface. */
1641 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1642 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1644 /* Initialize the sampler view. */
1645 util_blitter_default_src_texture(blitter
, &src_templ
, src
, src_level
);
1646 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1649 util_blitter_blit_generic(blitter
, dst_view
, &dstbox
,
1650 src_view
, srcbox
, src
->width0
, src
->height0
,
1651 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
,
1654 pipe_surface_reference(&dst_view
, NULL
);
1655 pipe_sampler_view_reference(&src_view
, NULL
);
1659 blitter_draw_tex(struct blitter_context_priv
*ctx
,
1660 int dst_x1
, int dst_y1
, int dst_x2
, int dst_y2
,
1661 struct pipe_sampler_view
*src
,
1662 unsigned src_width0
, unsigned src_height0
,
1663 int src_x1
, int src_y1
, int src_x2
, int src_y2
,
1664 float layer
, unsigned sample
,
1665 bool uses_txf
, enum blitter_attrib_type type
)
1667 union blitter_attrib coord
;
1668 blitter_get_vs_func get_vs
= get_vs_passthrough_pos_generic
;
1670 get_texcoords(src
, src_width0
, src_height0
,
1671 src_x1
, src_y1
, src_x2
, src_y2
, layer
, sample
,
1674 if (src
->target
== PIPE_TEXTURE_CUBE
||
1675 src
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
1676 float face_coord
[4][2];
1678 set_texcoords_in_vertices(&coord
, &face_coord
[0][0], 2);
1679 util_map_texcoords2d_onto_cubemap((unsigned)layer
% 6,
1680 /* pointer, stride in floats */
1681 &face_coord
[0][0], 2,
1682 &ctx
->vertices
[0][1][0], 8,
1684 for (unsigned i
= 0; i
< 4; i
++)
1685 ctx
->vertices
[i
][1][3] = coord
.texcoord
.w
;
1687 /* Cubemaps don't use draw_rectangle. */
1688 blitter_draw(ctx
, ctx
->velem_state
, get_vs
,
1689 dst_x1
, dst_y1
, dst_x2
, dst_y2
, 0, 1);
1691 ctx
->base
.draw_rectangle(&ctx
->base
, ctx
->velem_state
, get_vs
,
1692 dst_x1
, dst_y1
, dst_x2
, dst_y2
,
1693 0, 1, type
, &coord
);
1697 static void do_blits(struct blitter_context_priv
*ctx
,
1698 struct pipe_surface
*dst
,
1699 const struct pipe_box
*dstbox
,
1700 struct pipe_sampler_view
*src
,
1701 unsigned src_width0
,
1702 unsigned src_height0
,
1703 const struct pipe_box
*srcbox
,
1707 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1708 unsigned src_samples
= src
->texture
->nr_samples
;
1709 unsigned dst_samples
= dst
->texture
->nr_samples
;
1710 enum pipe_texture_target src_target
= src
->target
;
1711 struct pipe_framebuffer_state fb_state
= {0};
1713 /* Initialize framebuffer state. */
1714 fb_state
.width
= dst
->width
;
1715 fb_state
.height
= dst
->height
;
1716 fb_state
.nr_cbufs
= is_zsbuf
? 0 : 1;
1718 blitter_set_dst_dimensions(ctx
, fb_state
.width
, fb_state
.height
);
1720 if ((src_target
== PIPE_TEXTURE_1D
||
1721 src_target
== PIPE_TEXTURE_2D
||
1722 src_target
== PIPE_TEXTURE_RECT
) &&
1724 /* Set framebuffer state. */
1726 fb_state
.zsbuf
= dst
;
1728 fb_state
.cbufs
[0] = dst
;
1730 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1733 pipe
->set_sample_mask(pipe
, ~0);
1734 blitter_draw_tex(ctx
, dstbox
->x
, dstbox
->y
,
1735 dstbox
->x
+ dstbox
->width
,
1736 dstbox
->y
+ dstbox
->height
,
1737 src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1738 srcbox
->x
+ srcbox
->width
, srcbox
->y
+ srcbox
->height
,
1739 0, 0, uses_txf
, UTIL_BLITTER_ATTRIB_TEXCOORD_XY
);
1741 /* Draw the quad with the generic codepath. */
1743 for (dst_z
= 0; dst_z
< dstbox
->depth
; dst_z
++) {
1744 struct pipe_surface
*old
;
1745 float dst2src_scale
= srcbox
->depth
/ (float)dstbox
->depth
;
1747 /* Scale Z properly if the blit is scaled.
1749 * When downscaling, we want the coordinates centered, so that
1750 * mipmapping works for 3D textures. For example, when generating
1751 * a 4x4x4 level, this wouldn't average the pixels:
1753 * src Z: 0 1 2 3 4 5 6 7
1756 * Because the pixels are not centered below the pixels of the higher
1757 * level. Therefore, we want this:
1758 * src Z: 0 1 2 3 4 5 6 7
1761 * dst_offset defines the offset needed for centering the pixels and
1762 * it works with any scaling (not just 2x).
1764 float dst_offset
= ((srcbox
->depth
- 1) -
1765 (dstbox
->depth
- 1) * dst2src_scale
) * 0.5;
1766 float src_z
= (dst_z
+ dst_offset
) * dst2src_scale
;
1768 /* Set framebuffer state. */
1770 fb_state
.zsbuf
= dst
;
1772 fb_state
.cbufs
[0] = dst
;
1774 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1776 /* See if we need to blit a multisample or singlesample buffer. */
1777 if (src_samples
== dst_samples
&& dst_samples
> 1) {
1779 unsigned i
, max_sample
= dst_samples
- 1;
1781 for (i
= 0; i
<= max_sample
; i
++) {
1782 pipe
->set_sample_mask(pipe
, 1 << i
);
1783 blitter_draw_tex(ctx
, dstbox
->x
, dstbox
->y
,
1784 dstbox
->x
+ dstbox
->width
,
1785 dstbox
->y
+ dstbox
->height
,
1786 src
, src_width0
, src_height0
,
1787 srcbox
->x
, srcbox
->y
,
1788 srcbox
->x
+ srcbox
->width
,
1789 srcbox
->y
+ srcbox
->height
,
1790 srcbox
->z
+ src_z
, i
, uses_txf
,
1791 UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW
);
1794 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1795 pipe
->set_sample_mask(pipe
, ~0);
1796 blitter_draw_tex(ctx
, dstbox
->x
, dstbox
->y
,
1797 dstbox
->x
+ dstbox
->width
,
1798 dstbox
->y
+ dstbox
->height
,
1799 src
, src_width0
, src_height0
,
1800 srcbox
->x
, srcbox
->y
,
1801 srcbox
->x
+ srcbox
->width
,
1802 srcbox
->y
+ srcbox
->height
,
1803 srcbox
->z
+ src_z
, 0, uses_txf
,
1804 UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW
);
1807 /* Get the next surface or (if this is the last iteration)
1808 * just unreference the last one. */
1810 if (dst_z
< dstbox
->depth
-1) {
1811 dst
= util_blitter_get_next_surface_layer(ctx
->base
.pipe
, dst
);
1814 pipe_surface_reference(&old
, NULL
);
1820 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1821 struct pipe_surface
*dst
,
1822 const struct pipe_box
*dstbox
,
1823 struct pipe_sampler_view
*src
,
1824 const struct pipe_box
*srcbox
,
1825 unsigned src_width0
, unsigned src_height0
,
1826 unsigned mask
, unsigned filter
,
1827 const struct pipe_scissor_state
*scissor
,
1830 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1831 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1832 enum pipe_texture_target src_target
= src
->target
;
1833 unsigned src_samples
= src
->texture
->nr_samples
;
1834 unsigned dst_samples
= dst
->texture
->nr_samples
;
1835 bool has_depth
, has_stencil
, has_color
;
1836 bool blit_stencil
, blit_depth
, blit_color
;
1837 void *sampler_state
;
1838 const struct util_format_description
*src_desc
=
1839 util_format_description(src
->format
);
1840 const struct util_format_description
*dst_desc
=
1841 util_format_description(dst
->format
);
1843 has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
&&
1844 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1845 has_depth
= util_format_has_depth(src_desc
) &&
1846 util_format_has_depth(dst_desc
);
1847 has_stencil
= util_format_has_stencil(src_desc
) &&
1848 util_format_has_stencil(dst_desc
);
1850 blit_color
= has_color
&& (mask
& PIPE_MASK_RGBA
);
1851 blit_depth
= has_depth
&& (mask
& PIPE_MASK_Z
);
1852 blit_stencil
= has_stencil
&& (mask
& PIPE_MASK_S
) &&
1853 ctx
->has_stencil_export
;
1855 if (!blit_stencil
&& !blit_depth
&& !blit_color
) {
1859 bool is_scaled
= dstbox
->width
!= abs(srcbox
->width
) ||
1860 dstbox
->height
!= abs(srcbox
->height
);
1862 if (blit_stencil
|| !is_scaled
)
1863 filter
= PIPE_TEX_FILTER_NEAREST
;
1865 bool use_txf
= false;
1867 /* Don't support scaled blits. The TXF shader uses F2I for rounding. */
1870 filter
== PIPE_TEX_FILTER_NEAREST
&&
1871 src
->target
!= PIPE_TEXTURE_CUBE
&&
1872 src
->target
!= PIPE_TEXTURE_CUBE_ARRAY
) {
1873 int src_width
= u_minify(src_width0
, src
->u
.tex
.first_level
);
1874 int src_height
= u_minify(src_height0
, src
->u
.tex
.first_level
);
1875 int src_depth
= src
->u
.tex
.last_layer
+ 1;
1876 struct pipe_box box
= *srcbox
;
1878 /* Eliminate negative width/height/depth. */
1879 if (box
.width
< 0) {
1883 if (box
.height
< 0) {
1884 box
.y
+= box
.height
;
1887 if (box
.depth
< 0) {
1892 /* See if srcbox is in bounds. TXF doesn't clamp the coordinates. */
1894 box
.x
>= 0 && box
.x
< src_width
&&
1895 box
.y
>= 0 && box
.y
< src_height
&&
1896 box
.z
>= 0 && box
.z
< src_depth
&&
1897 box
.x
+ box
.width
> 0 && box
.x
+ box
.width
<= src_width
&&
1898 box
.y
+ box
.height
> 0 && box
.y
+ box
.height
<= src_height
&&
1899 box
.z
+ box
.depth
> 0 && box
.z
+ box
.depth
<= src_depth
;
1902 /* Check whether the states are properly saved. */
1903 util_blitter_set_running_flag(blitter
);
1904 blitter_check_saved_vertex_states(ctx
);
1905 blitter_check_saved_fragment_states(ctx
);
1906 blitter_check_saved_textures(ctx
);
1907 blitter_check_saved_fb_state(ctx
);
1908 blitter_disable_render_cond(ctx
);
1910 if (blit_depth
|| blit_stencil
) {
1911 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
1913 if (blit_depth
&& blit_stencil
) {
1914 pipe
->bind_depth_stencil_alpha_state(pipe
,
1915 ctx
->dsa_write_depth_stencil
);
1916 ctx
->bind_fs_state(pipe
,
1917 blitter_get_fs_texfetch_depthstencil(ctx
, src_target
,
1918 src_samples
, use_txf
));
1919 } else if (blit_depth
) {
1920 pipe
->bind_depth_stencil_alpha_state(pipe
,
1921 ctx
->dsa_write_depth_keep_stencil
);
1922 ctx
->bind_fs_state(pipe
,
1923 blitter_get_fs_texfetch_depth(ctx
, src_target
,
1924 src_samples
, use_txf
));
1925 } else { /* is_stencil */
1926 pipe
->bind_depth_stencil_alpha_state(pipe
,
1927 ctx
->dsa_keep_depth_write_stencil
);
1928 ctx
->bind_fs_state(pipe
,
1929 blitter_get_fs_texfetch_stencil(ctx
, src_target
,
1930 src_samples
, use_txf
));
1934 unsigned colormask
= mask
& PIPE_MASK_RGBA
;
1936 pipe
->bind_blend_state(pipe
, ctx
->blend
[colormask
][alpha_blend
]);
1937 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1938 ctx
->bind_fs_state(pipe
,
1939 blitter_get_fs_texfetch_col(ctx
, src
->format
, dst
->format
, src_target
,
1940 src_samples
, dst_samples
, filter
,
1944 /* Set the linear filter only for scaled color non-MSAA blits. */
1945 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
1946 if (src_target
== PIPE_TEXTURE_RECT
) {
1947 sampler_state
= ctx
->sampler_state_rect_linear
;
1949 sampler_state
= ctx
->sampler_state_linear
;
1952 if (src_target
== PIPE_TEXTURE_RECT
) {
1953 sampler_state
= ctx
->sampler_state_rect
;
1955 sampler_state
= ctx
->sampler_state
;
1960 if (blit_depth
&& blit_stencil
) {
1961 /* Setup two samplers, one for depth and the other one for stencil. */
1962 struct pipe_sampler_view templ
;
1963 struct pipe_sampler_view
*views
[2];
1964 void *samplers
[2] = {sampler_state
, sampler_state
};
1967 templ
.format
= util_format_stencil_only(templ
.format
);
1968 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1971 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1973 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, views
);
1974 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, samplers
);
1976 pipe_sampler_view_reference(&views
[1], NULL
);
1977 } else if (blit_stencil
) {
1978 /* Set a stencil-only sampler view for it not to sample depth instead. */
1979 struct pipe_sampler_view templ
;
1980 struct pipe_sampler_view
*view
;
1983 templ
.format
= util_format_stencil_only(templ
.format
);
1984 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1986 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1988 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &view
);
1989 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1990 0, 1, &sampler_state
);
1992 pipe_sampler_view_reference(&view
, NULL
);
1994 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src
);
1995 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1996 0, 1, &sampler_state
);
2000 pipe
->set_scissor_states(pipe
, 0, 1, scissor
);
2003 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
, dst_samples
> 1);
2005 do_blits(ctx
, dst
, dstbox
, src
, src_width0
, src_height0
,
2006 srcbox
, blit_depth
|| blit_stencil
, use_txf
);
2008 util_blitter_restore_vertex_states(blitter
);
2009 util_blitter_restore_fragment_states(blitter
);
2010 util_blitter_restore_textures(blitter
);
2011 util_blitter_restore_fb_state(blitter
);
2013 pipe
->set_scissor_states(pipe
, 0, 1, &ctx
->base
.saved_scissor
);
2015 util_blitter_restore_render_cond(blitter
);
2016 util_blitter_unset_running_flag(blitter
);
2020 util_blitter_blit(struct blitter_context
*blitter
,
2021 const struct pipe_blit_info
*info
)
2023 struct pipe_resource
*dst
= info
->dst
.resource
;
2024 struct pipe_resource
*src
= info
->src
.resource
;
2025 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2026 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2027 struct pipe_surface
*dst_view
, dst_templ
;
2028 struct pipe_sampler_view src_templ
, *src_view
;
2030 /* Initialize the surface. */
2031 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
2033 dst_templ
.format
= info
->dst
.format
;
2034 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
2036 /* Initialize the sampler view. */
2037 util_blitter_default_src_texture(blitter
, &src_templ
, src
, info
->src
.level
);
2038 src_templ
.format
= info
->src
.format
;
2039 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
2042 util_blitter_blit_generic(blitter
, dst_view
, &info
->dst
.box
,
2043 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
2044 info
->mask
, info
->filter
,
2045 info
->scissor_enable
? &info
->scissor
: NULL
,
2048 pipe_surface_reference(&dst_view
, NULL
);
2049 pipe_sampler_view_reference(&src_view
, NULL
);
2052 void util_blitter_generate_mipmap(struct blitter_context
*blitter
,
2053 struct pipe_resource
*tex
,
2054 enum pipe_format format
,
2055 unsigned base_level
, unsigned last_level
,
2056 unsigned first_layer
, unsigned last_layer
)
2058 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2059 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2060 struct pipe_surface dst_templ
, *dst_view
;
2061 struct pipe_sampler_view src_templ
, *src_view
;
2063 void *sampler_state
;
2064 const struct util_format_description
*desc
=
2065 util_format_description(format
);
2067 unsigned target
= tex
->target
;
2069 if (ctx
->cube_as_2darray
&&
2070 (target
== PIPE_TEXTURE_CUBE
|| target
== PIPE_TEXTURE_CUBE_ARRAY
))
2071 target
= PIPE_TEXTURE_2D_ARRAY
;
2073 assert(tex
->nr_samples
<= 1);
2074 /* Disallow stencil formats without depth. */
2075 assert(!util_format_has_stencil(desc
) || util_format_has_depth(desc
));
2077 is_depth
= desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
;
2079 /* Check whether the states are properly saved. */
2080 util_blitter_set_running_flag(blitter
);
2081 blitter_check_saved_vertex_states(ctx
);
2082 blitter_check_saved_fragment_states(ctx
);
2083 blitter_check_saved_textures(ctx
);
2084 blitter_check_saved_fb_state(ctx
);
2085 blitter_disable_render_cond(ctx
);
2089 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
2090 pipe
->bind_depth_stencil_alpha_state(pipe
,
2091 ctx
->dsa_write_depth_keep_stencil
);
2092 ctx
->bind_fs_state(pipe
,
2093 blitter_get_fs_texfetch_depth(ctx
, target
, 1, false));
2095 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2096 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2097 ctx
->bind_fs_state(pipe
,
2098 blitter_get_fs_texfetch_col(ctx
, tex
->format
, tex
->format
, target
,
2099 1, 1, PIPE_TEX_FILTER_LINEAR
, false));
2102 if (target
== PIPE_TEXTURE_RECT
) {
2103 sampler_state
= ctx
->sampler_state_rect_linear
;
2105 sampler_state
= ctx
->sampler_state_linear
;
2107 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
2108 0, 1, &sampler_state
);
2110 blitter_set_common_draw_rect_state(ctx
, false, false);
2112 for (src_level
= base_level
; src_level
< last_level
; src_level
++) {
2113 struct pipe_box dstbox
= {0}, srcbox
= {0};
2114 unsigned dst_level
= src_level
+ 1;
2116 dstbox
.width
= u_minify(tex
->width0
, dst_level
);
2117 dstbox
.height
= u_minify(tex
->height0
, dst_level
);
2119 srcbox
.width
= u_minify(tex
->width0
, src_level
);
2120 srcbox
.height
= u_minify(tex
->height0
, src_level
);
2122 if (target
== PIPE_TEXTURE_3D
) {
2123 dstbox
.depth
= util_num_layers(tex
, dst_level
);
2124 srcbox
.depth
= util_num_layers(tex
, src_level
);
2126 dstbox
.z
= srcbox
.z
= first_layer
;
2127 dstbox
.depth
= srcbox
.depth
= last_layer
- first_layer
+ 1;
2130 /* Initialize the surface. */
2131 util_blitter_default_dst_texture(&dst_templ
, tex
, dst_level
,
2133 dst_templ
.format
= format
;
2134 dst_view
= pipe
->create_surface(pipe
, tex
, &dst_templ
);
2136 /* Initialize the sampler view. */
2137 util_blitter_default_src_texture(blitter
, &src_templ
, tex
, src_level
);
2138 src_templ
.format
= format
;
2139 src_view
= pipe
->create_sampler_view(pipe
, tex
, &src_templ
);
2141 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src_view
);
2143 do_blits(ctx
, dst_view
, &dstbox
, src_view
, tex
->width0
, tex
->height0
,
2144 &srcbox
, is_depth
, false);
2146 pipe_surface_reference(&dst_view
, NULL
);
2147 pipe_sampler_view_reference(&src_view
, NULL
);
2150 util_blitter_restore_vertex_states(blitter
);
2151 util_blitter_restore_fragment_states(blitter
);
2152 util_blitter_restore_textures(blitter
);
2153 util_blitter_restore_fb_state(blitter
);
2154 util_blitter_restore_render_cond(blitter
);
2155 util_blitter_unset_running_flag(blitter
);
2158 /* Clear a region of a color surface to a constant value. */
2159 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
2160 struct pipe_surface
*dstsurf
,
2161 const union pipe_color_union
*color
,
2162 unsigned dstx
, unsigned dsty
,
2163 unsigned width
, unsigned height
)
2165 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2166 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2167 struct pipe_framebuffer_state fb_state
;
2168 unsigned num_layers
;
2170 assert(dstsurf
->texture
);
2171 if (!dstsurf
->texture
)
2174 /* check the saved state */
2175 util_blitter_set_running_flag(blitter
);
2176 blitter_check_saved_vertex_states(ctx
);
2177 blitter_check_saved_fragment_states(ctx
);
2178 blitter_check_saved_fb_state(ctx
);
2179 blitter_disable_render_cond(ctx
);
2182 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2183 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2184 bind_fs_write_one_cbuf(ctx
);
2186 /* set a framebuffer state */
2187 fb_state
.width
= dstsurf
->width
;
2188 fb_state
.height
= dstsurf
->height
;
2189 fb_state
.nr_cbufs
= 1;
2190 fb_state
.cbufs
[0] = dstsurf
;
2192 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2193 pipe
->set_sample_mask(pipe
, ~0);
2195 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2197 union blitter_attrib attrib
;
2198 memcpy(attrib
.color
, color
->ui
, sizeof(color
->ui
));
2200 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
2201 if (num_layers
> 1 && ctx
->has_layered
) {
2202 blitter_set_common_draw_rect_state(ctx
, false, false);
2203 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs_layered
,
2204 dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
2205 num_layers
, UTIL_BLITTER_ATTRIB_COLOR
, &attrib
);
2207 blitter_set_common_draw_rect_state(ctx
, false, false);
2208 blitter
->draw_rectangle(blitter
, ctx
->velem_state
,
2209 get_vs_passthrough_pos_generic
,
2210 dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
2211 1, UTIL_BLITTER_ATTRIB_COLOR
, &attrib
);
2214 util_blitter_restore_vertex_states(blitter
);
2215 util_blitter_restore_fragment_states(blitter
);
2216 util_blitter_restore_fb_state(blitter
);
2217 util_blitter_restore_render_cond(blitter
);
2218 util_blitter_unset_running_flag(blitter
);
2221 /* Clear a region of a depth stencil surface. */
2222 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
2223 struct pipe_surface
*dstsurf
,
2224 unsigned clear_flags
,
2227 unsigned dstx
, unsigned dsty
,
2228 unsigned width
, unsigned height
)
2230 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2231 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2232 struct pipe_framebuffer_state fb_state
;
2233 struct pipe_stencil_ref sr
= { { 0 } };
2234 unsigned num_layers
;
2236 assert(dstsurf
->texture
);
2237 if (!dstsurf
->texture
)
2240 /* check the saved state */
2241 util_blitter_set_running_flag(blitter
);
2242 blitter_check_saved_vertex_states(ctx
);
2243 blitter_check_saved_fragment_states(ctx
);
2244 blitter_check_saved_fb_state(ctx
);
2245 blitter_disable_render_cond(ctx
);
2248 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
2249 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
2250 sr
.ref_value
[0] = stencil
& 0xff;
2251 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
2252 pipe
->set_stencil_ref(pipe
, &sr
);
2254 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
2255 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
2257 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
2258 sr
.ref_value
[0] = stencil
& 0xff;
2259 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
2260 pipe
->set_stencil_ref(pipe
, &sr
);
2263 /* hmm that should be illegal probably, or make it a no-op somewhere */
2264 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2268 /* set a framebuffer state */
2269 fb_state
.width
= dstsurf
->width
;
2270 fb_state
.height
= dstsurf
->height
;
2271 fb_state
.nr_cbufs
= 0;
2272 fb_state
.cbufs
[0] = 0;
2273 fb_state
.zsbuf
= dstsurf
;
2274 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2275 pipe
->set_sample_mask(pipe
, ~0);
2277 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2279 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
2280 if (num_layers
> 1 && ctx
->has_layered
) {
2281 blitter_set_common_draw_rect_state(ctx
, false, false);
2282 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs_layered
,
2283 dstx
, dsty
, dstx
+width
, dsty
+height
, depth
,
2284 num_layers
, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2286 blitter_set_common_draw_rect_state(ctx
, false, false);
2287 blitter
->draw_rectangle(blitter
, ctx
->velem_state
,
2288 get_vs_passthrough_pos
,
2289 dstx
, dsty
, dstx
+width
, dsty
+height
, depth
, 1,
2290 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2293 util_blitter_restore_vertex_states(blitter
);
2294 util_blitter_restore_fragment_states(blitter
);
2295 util_blitter_restore_fb_state(blitter
);
2296 util_blitter_restore_render_cond(blitter
);
2297 util_blitter_unset_running_flag(blitter
);
2300 /* draw a rectangle across a region using a custom dsa stage - for r600g */
2301 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
2302 struct pipe_surface
*zsurf
,
2303 struct pipe_surface
*cbsurf
,
2304 unsigned sample_mask
,
2305 void *dsa_stage
, float depth
)
2307 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2308 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2309 struct pipe_framebuffer_state fb_state
;
2311 assert(zsurf
->texture
);
2312 if (!zsurf
->texture
)
2315 /* check the saved state */
2316 util_blitter_set_running_flag(blitter
);
2317 blitter_check_saved_vertex_states(ctx
);
2318 blitter_check_saved_fragment_states(ctx
);
2319 blitter_check_saved_fb_state(ctx
);
2320 blitter_disable_render_cond(ctx
);
2323 pipe
->bind_blend_state(pipe
, cbsurf
? ctx
->blend
[PIPE_MASK_RGBA
][0] :
2325 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
2327 bind_fs_write_one_cbuf(ctx
);
2331 /* set a framebuffer state */
2332 fb_state
.width
= zsurf
->width
;
2333 fb_state
.height
= zsurf
->height
;
2334 fb_state
.nr_cbufs
= 1;
2336 fb_state
.cbufs
[0] = cbsurf
;
2337 fb_state
.nr_cbufs
= 1;
2339 fb_state
.cbufs
[0] = NULL
;
2340 fb_state
.nr_cbufs
= 0;
2342 fb_state
.zsbuf
= zsurf
;
2343 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2344 pipe
->set_sample_mask(pipe
, sample_mask
);
2346 blitter_set_common_draw_rect_state(ctx
, false, false);
2347 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
2348 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs_passthrough_pos
,
2349 0, 0, zsurf
->width
, zsurf
->height
, depth
,
2350 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2352 util_blitter_restore_vertex_states(blitter
);
2353 util_blitter_restore_fragment_states(blitter
);
2354 util_blitter_restore_fb_state(blitter
);
2355 util_blitter_restore_render_cond(blitter
);
2356 util_blitter_unset_running_flag(blitter
);
2359 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
2360 struct pipe_resource
*dst
,
2362 struct pipe_resource
*src
,
2366 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2367 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2368 struct pipe_vertex_buffer vb
;
2369 struct pipe_stream_output_target
*so_target
;
2370 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2372 if (srcx
>= src
->width0
||
2373 dstx
>= dst
->width0
) {
2376 if (srcx
+ size
> src
->width0
) {
2377 size
= src
->width0
- srcx
;
2379 if (dstx
+ size
> dst
->width0
) {
2380 size
= dst
->width0
- dstx
;
2383 /* Drivers not capable of Stream Out should not call this function
2384 * in the first place. */
2385 assert(ctx
->has_stream_out
);
2387 /* Some alignment is required. */
2388 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
2389 !ctx
->has_stream_out
) {
2390 struct pipe_box box
;
2391 u_box_1d(srcx
, size
, &box
);
2392 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
2396 util_blitter_set_running_flag(blitter
);
2397 blitter_check_saved_vertex_states(ctx
);
2398 blitter_disable_render_cond(ctx
);
2400 vb
.is_user_buffer
= false;
2401 vb
.buffer
.resource
= src
;
2402 vb
.buffer_offset
= srcx
;
2405 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2406 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[0]);
2407 bind_vs_pos_only(ctx
, 1);
2408 if (ctx
->has_geometry_shader
)
2409 pipe
->bind_gs_state(pipe
, NULL
);
2410 if (ctx
->has_tessellation
) {
2411 pipe
->bind_tcs_state(pipe
, NULL
);
2412 pipe
->bind_tes_state(pipe
, NULL
);
2414 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2416 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
2417 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2419 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2421 util_blitter_restore_vertex_states(blitter
);
2422 util_blitter_restore_render_cond(blitter
);
2423 util_blitter_unset_running_flag(blitter
);
2424 pipe_so_target_reference(&so_target
, NULL
);
2427 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
2428 struct pipe_resource
*dst
,
2429 unsigned offset
, unsigned size
,
2430 unsigned num_channels
,
2431 const union pipe_color_union
*clear_value
)
2433 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2434 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2435 struct pipe_vertex_buffer vb
= {0};
2436 struct pipe_stream_output_target
*so_target
= NULL
;
2437 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2439 assert(num_channels
>= 1);
2440 assert(num_channels
<= 4);
2442 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
2444 * R600 uses this to initialize texture resources, so width0 might not be
2445 * what you think it is.
2448 /* Streamout is required. */
2449 if (!ctx
->has_stream_out
) {
2450 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2454 /* Some alignment is required. */
2455 if (offset
% 4 != 0 || size
% 4 != 0) {
2456 assert(!"Bad alignment in util_blitter_clear_buffer()");
2460 u_upload_data(pipe
->stream_uploader
, 0, num_channels
*4, 4, clear_value
,
2461 &vb
.buffer_offset
, &vb
.buffer
.resource
);
2462 if (!vb
.buffer
.resource
)
2467 util_blitter_set_running_flag(blitter
);
2468 blitter_check_saved_vertex_states(ctx
);
2469 blitter_disable_render_cond(ctx
);
2471 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2472 pipe
->bind_vertex_elements_state(pipe
,
2473 ctx
->velem_state_readbuf
[num_channels
-1]);
2474 bind_vs_pos_only(ctx
, num_channels
);
2475 if (ctx
->has_geometry_shader
)
2476 pipe
->bind_gs_state(pipe
, NULL
);
2477 if (ctx
->has_tessellation
) {
2478 pipe
->bind_tcs_state(pipe
, NULL
);
2479 pipe
->bind_tes_state(pipe
, NULL
);
2481 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2483 so_target
= pipe
->create_stream_output_target(pipe
, dst
, offset
, size
);
2484 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2486 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2489 util_blitter_restore_vertex_states(blitter
);
2490 util_blitter_restore_render_cond(blitter
);
2491 util_blitter_unset_running_flag(blitter
);
2492 pipe_so_target_reference(&so_target
, NULL
);
2493 pipe_resource_reference(&vb
.buffer
.resource
, NULL
);
2496 /* probably radeon specific */
2497 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
2498 struct pipe_resource
*dst
,
2501 struct pipe_resource
*src
,
2503 unsigned sample_mask
,
2505 enum pipe_format format
)
2507 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2508 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2509 struct pipe_framebuffer_state fb_state
;
2510 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
2512 util_blitter_set_running_flag(blitter
);
2513 blitter_check_saved_vertex_states(ctx
);
2514 blitter_check_saved_fragment_states(ctx
);
2515 blitter_disable_render_cond(ctx
);
2518 pipe
->bind_blend_state(pipe
, custom_blend
);
2519 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2520 bind_fs_write_one_cbuf(ctx
);
2521 pipe
->set_sample_mask(pipe
, sample_mask
);
2523 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
2524 surf_tmpl
.format
= format
;
2525 surf_tmpl
.u
.tex
.level
= dst_level
;
2526 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
2527 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
2529 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
2531 surf_tmpl
.u
.tex
.level
= 0;
2532 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
2533 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
2535 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
2537 /* set a framebuffer state */
2538 fb_state
.width
= src
->width0
;
2539 fb_state
.height
= src
->height0
;
2540 fb_state
.nr_cbufs
= 2;
2541 fb_state
.cbufs
[0] = srcsurf
;
2542 fb_state
.cbufs
[1] = dstsurf
;
2543 fb_state
.zsbuf
= NULL
;
2544 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2546 blitter_set_common_draw_rect_state(ctx
, false, false);
2547 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
2548 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs_passthrough_pos
,
2549 0, 0, src
->width0
, src
->height0
,
2550 0, 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2551 util_blitter_restore_fb_state(blitter
);
2552 util_blitter_restore_vertex_states(blitter
);
2553 util_blitter_restore_fragment_states(blitter
);
2554 util_blitter_restore_render_cond(blitter
);
2555 util_blitter_unset_running_flag(blitter
);
2557 pipe_surface_reference(&srcsurf
, NULL
);
2558 pipe_surface_reference(&dstsurf
, NULL
);
2561 void util_blitter_custom_color(struct blitter_context
*blitter
,
2562 struct pipe_surface
*dstsurf
,
2565 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2566 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2567 struct pipe_framebuffer_state fb_state
;
2569 assert(dstsurf
->texture
);
2570 if (!dstsurf
->texture
)
2573 /* check the saved state */
2574 util_blitter_set_running_flag(blitter
);
2575 blitter_check_saved_vertex_states(ctx
);
2576 blitter_check_saved_fragment_states(ctx
);
2577 blitter_check_saved_fb_state(ctx
);
2578 blitter_disable_render_cond(ctx
);
2581 pipe
->bind_blend_state(pipe
, custom_blend
? custom_blend
2582 : ctx
->blend
[PIPE_MASK_RGBA
][0]);
2583 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2584 bind_fs_write_one_cbuf(ctx
);
2585 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2587 /* set a framebuffer state */
2588 fb_state
.width
= dstsurf
->width
;
2589 fb_state
.height
= dstsurf
->height
;
2590 fb_state
.nr_cbufs
= 1;
2591 fb_state
.cbufs
[0] = dstsurf
;
2593 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2594 pipe
->set_sample_mask(pipe
, ~0);
2596 blitter_set_common_draw_rect_state(ctx
, false, false);
2597 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2598 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_vs_passthrough_pos
,
2599 0, 0, dstsurf
->width
, dstsurf
->height
,
2600 0, 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2602 util_blitter_restore_vertex_states(blitter
);
2603 util_blitter_restore_fragment_states(blitter
);
2604 util_blitter_restore_fb_state(blitter
);
2605 util_blitter_restore_render_cond(blitter
);
2606 util_blitter_unset_running_flag(blitter
);
2609 static void *get_custom_vs(struct blitter_context
*blitter
)
2611 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2613 return ctx
->custom_vs
;
2617 * Performs a custom blit to the destination surface, using the VS and FS
2620 * Used by vc4 for the 8-bit linear-to-tiled blit.
2622 void util_blitter_custom_shader(struct blitter_context
*blitter
,
2623 struct pipe_surface
*dstsurf
,
2624 void *custom_vs
, void *custom_fs
)
2626 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2627 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2628 struct pipe_framebuffer_state fb_state
;
2630 ctx
->custom_vs
= custom_vs
;
2632 assert(dstsurf
->texture
);
2633 if (!dstsurf
->texture
)
2636 /* check the saved state */
2637 util_blitter_set_running_flag(blitter
);
2638 blitter_check_saved_vertex_states(ctx
);
2639 blitter_check_saved_fragment_states(ctx
);
2640 blitter_check_saved_fb_state(ctx
);
2641 blitter_disable_render_cond(ctx
);
2644 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2645 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2646 pipe
->bind_fs_state(pipe
, custom_fs
);
2647 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2649 /* set a framebuffer state */
2650 fb_state
.width
= dstsurf
->width
;
2651 fb_state
.height
= dstsurf
->height
;
2652 fb_state
.nr_cbufs
= 1;
2653 fb_state
.cbufs
[0] = dstsurf
;
2655 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2656 pipe
->set_sample_mask(pipe
, ~0);
2658 blitter_set_common_draw_rect_state(ctx
, false, false);
2659 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2660 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, get_custom_vs
,
2661 0, 0, dstsurf
->width
, dstsurf
->height
,
2662 0, 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2664 util_blitter_restore_vertex_states(blitter
);
2665 util_blitter_restore_fragment_states(blitter
);
2666 util_blitter_restore_fb_state(blitter
);
2667 util_blitter_restore_render_cond(blitter
);
2668 util_blitter_unset_running_flag(blitter
);