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
, *rs_state_scissor
, *rs_discard_state
;
128 /* Destination surface dimensions. */
132 bool has_geometry_shader
;
133 bool has_tessellation
;
136 bool has_stencil_export
;
137 bool has_texture_multisample
;
140 bool cube_as_2darray
;
141 bool cached_all_shaders
;
143 /* The Draw module overrides these functions.
144 * Always create the blitter before Draw. */
145 void (*bind_fs_state
)(struct pipe_context
*, void *);
146 void (*delete_fs_state
)(struct pipe_context
*, void *);
149 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
)
151 struct blitter_context_priv
*ctx
;
152 struct pipe_blend_state blend
;
153 struct pipe_depth_stencil_alpha_state dsa
;
154 struct pipe_rasterizer_state rs_state
;
155 struct pipe_sampler_state sampler_state
;
156 struct pipe_vertex_element velem
[2];
159 ctx
= CALLOC_STRUCT(blitter_context_priv
);
163 ctx
->base
.pipe
= pipe
;
164 ctx
->base
.draw_rectangle
= util_blitter_draw_rectangle
;
166 ctx
->bind_fs_state
= pipe
->bind_fs_state
;
167 ctx
->delete_fs_state
= pipe
->delete_fs_state
;
169 /* init state objects for them to be considered invalid */
170 ctx
->base
.saved_blend_state
= INVALID_PTR
;
171 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
172 ctx
->base
.saved_rs_state
= INVALID_PTR
;
173 ctx
->base
.saved_fs
= INVALID_PTR
;
174 ctx
->base
.saved_vs
= INVALID_PTR
;
175 ctx
->base
.saved_gs
= INVALID_PTR
;
176 ctx
->base
.saved_velem_state
= INVALID_PTR
;
177 ctx
->base
.saved_fb_state
.nr_cbufs
= ~0;
178 ctx
->base
.saved_num_sampler_views
= ~0;
179 ctx
->base
.saved_num_sampler_states
= ~0;
180 ctx
->base
.saved_num_so_targets
= ~0;
182 ctx
->has_geometry_shader
=
183 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
184 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
186 ctx
->has_tessellation
=
187 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_TESS_CTRL
,
188 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
190 ctx
->has_stream_out
=
191 pipe
->screen
->get_param(pipe
->screen
,
192 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0;
194 ctx
->has_stencil_export
=
195 pipe
->screen
->get_param(pipe
->screen
,
196 PIPE_CAP_SHADER_STENCIL_EXPORT
);
198 ctx
->has_texture_multisample
=
199 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TEXTURE_MULTISAMPLE
);
201 ctx
->has_tex_lz
= pipe
->screen
->get_param(pipe
->screen
,
202 PIPE_CAP_TGSI_TEX_TXF_LZ
);
203 ctx
->has_txf
= pipe
->screen
->get_param(pipe
->screen
,
204 PIPE_CAP_GLSL_FEATURE_LEVEL
) > 130;
205 ctx
->cube_as_2darray
= pipe
->screen
->get_param(pipe
->screen
,
206 PIPE_CAP_SAMPLER_VIEW_TARGET
);
208 /* blend state objects */
209 memset(&blend
, 0, sizeof(blend
));
211 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
212 for (j
= 0; j
< 2; j
++) {
213 memset(&blend
.rt
[0], 0, sizeof(blend
.rt
[0]));
214 blend
.rt
[0].colormask
= i
;
216 blend
.rt
[0].blend_enable
= 1;
217 blend
.rt
[0].rgb_func
= PIPE_BLEND_ADD
;
218 blend
.rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_SRC_ALPHA
;
219 blend
.rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
220 blend
.rt
[0].alpha_func
= PIPE_BLEND_ADD
;
221 blend
.rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_SRC_ALPHA
;
222 blend
.rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
224 ctx
->blend
[i
][j
] = pipe
->create_blend_state(pipe
, &blend
);
228 /* depth stencil alpha state objects */
229 memset(&dsa
, 0, sizeof(dsa
));
230 ctx
->dsa_keep_depth_stencil
=
231 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
233 dsa
.depth
.enabled
= 1;
234 dsa
.depth
.writemask
= 1;
235 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
236 ctx
->dsa_write_depth_keep_stencil
=
237 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
239 dsa
.stencil
[0].enabled
= 1;
240 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
241 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
242 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
243 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
244 dsa
.stencil
[0].valuemask
= 0xff;
245 dsa
.stencil
[0].writemask
= 0xff;
246 ctx
->dsa_write_depth_stencil
=
247 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
249 dsa
.depth
.enabled
= 0;
250 dsa
.depth
.writemask
= 0;
251 ctx
->dsa_keep_depth_write_stencil
=
252 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
255 memset(&sampler_state
, 0, sizeof(sampler_state
));
256 sampler_state
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
257 sampler_state
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
258 sampler_state
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
259 sampler_state
.normalized_coords
= 1;
260 ctx
->sampler_state
= pipe
->create_sampler_state(pipe
, &sampler_state
);
261 sampler_state
.normalized_coords
= 0;
262 ctx
->sampler_state_rect
= pipe
->create_sampler_state(pipe
, &sampler_state
);
264 sampler_state
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
265 sampler_state
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
266 sampler_state
.normalized_coords
= 1;
267 ctx
->sampler_state_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
268 sampler_state
.normalized_coords
= 0;
269 ctx
->sampler_state_rect_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
271 /* rasterizer state */
272 memset(&rs_state
, 0, sizeof(rs_state
));
273 rs_state
.cull_face
= PIPE_FACE_NONE
;
274 rs_state
.half_pixel_center
= 1;
275 rs_state
.bottom_edge_rule
= 1;
276 rs_state
.flatshade
= 1;
277 rs_state
.depth_clip
= 1;
278 ctx
->rs_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
280 rs_state
.scissor
= 1;
281 ctx
->rs_state_scissor
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
283 if (ctx
->has_stream_out
) {
284 rs_state
.scissor
= 0;
285 rs_state
.rasterizer_discard
= 1;
286 ctx
->rs_discard_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
289 ctx
->base
.cb_slot
= 0; /* 0 for now */
290 ctx
->base
.vb_slot
= 0; /* 0 for now */
292 /* vertex elements states */
293 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
294 for (i
= 0; i
< 2; i
++) {
295 velem
[i
].src_offset
= i
* 4 * sizeof(float);
296 velem
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
297 velem
[i
].vertex_buffer_index
= ctx
->base
.vb_slot
;
299 ctx
->velem_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
301 if (ctx
->has_stream_out
) {
302 static enum pipe_format formats
[4] = {
303 PIPE_FORMAT_R32_UINT
,
304 PIPE_FORMAT_R32G32_UINT
,
305 PIPE_FORMAT_R32G32B32_UINT
,
306 PIPE_FORMAT_R32G32B32A32_UINT
309 for (i
= 0; i
< 4; i
++) {
310 velem
[0].src_format
= formats
[i
];
311 velem
[0].vertex_buffer_index
= ctx
->base
.vb_slot
;
312 ctx
->velem_state_readbuf
[i
] =
313 pipe
->create_vertex_elements_state(pipe
, 1, &velem
[0]);
318 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_INSTANCEID
) &&
319 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT
);
321 /* set invariant vertex coordinates */
322 for (i
= 0; i
< 4; i
++)
323 ctx
->vertices
[i
][0][3] = 1; /*v.w*/
328 static void bind_vs_pos_only(struct blitter_context_priv
*ctx
,
329 unsigned num_so_channels
)
331 struct pipe_context
*pipe
= ctx
->base
.pipe
;
332 int index
= num_so_channels
? num_so_channels
- 1 : 0;
334 if (!ctx
->vs_pos_only
[index
]) {
335 struct pipe_stream_output_info so
;
336 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
};
337 const uint semantic_indices
[] = { 0 };
339 memset(&so
, 0, sizeof(so
));
341 so
.output
[0].num_components
= num_so_channels
;
342 so
.stride
[0] = num_so_channels
;
344 ctx
->vs_pos_only
[index
] =
345 util_make_vertex_passthrough_shader_with_so(pipe
, 1, semantic_names
,
346 semantic_indices
, false,
350 pipe
->bind_vs_state(pipe
, ctx
->vs_pos_only
[index
]);
353 static void bind_vs_passthrough_pos_generic(struct blitter_context_priv
*ctx
)
355 struct pipe_context
*pipe
= ctx
->base
.pipe
;
358 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
359 TGSI_SEMANTIC_GENERIC
};
360 const uint semantic_indices
[] = { 0, 0 };
362 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
363 semantic_indices
, false);
366 pipe
->bind_vs_state(pipe
, ctx
->vs
);
369 static void bind_vs_passthrough_pos(struct blitter_context_priv
*ctx
)
371 struct pipe_context
*pipe
= ctx
->base
.pipe
;
373 if (!ctx
->vs_nogeneric
) {
374 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
};
375 const uint semantic_indices
[] = { 0 };
378 util_make_vertex_passthrough_shader(pipe
, 1,
380 semantic_indices
, false);
383 pipe
->bind_vs_state(pipe
, ctx
->vs_nogeneric
);
386 static void bind_vs_layered(struct blitter_context_priv
*ctx
)
388 struct pipe_context
*pipe
= ctx
->base
.pipe
;
390 if (!ctx
->vs_layered
) {
391 ctx
->vs_layered
= util_make_layered_clear_vertex_shader(pipe
);
394 pipe
->bind_vs_state(pipe
, ctx
->vs_layered
);
397 static void bind_fs_empty(struct blitter_context_priv
*ctx
)
399 struct pipe_context
*pipe
= ctx
->base
.pipe
;
401 if (!ctx
->fs_empty
) {
402 assert(!ctx
->cached_all_shaders
);
403 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
406 ctx
->bind_fs_state(pipe
, ctx
->fs_empty
);
409 static void bind_fs_write_one_cbuf(struct blitter_context_priv
*ctx
)
411 struct pipe_context
*pipe
= ctx
->base
.pipe
;
413 if (!ctx
->fs_write_one_cbuf
) {
414 assert(!ctx
->cached_all_shaders
);
415 ctx
->fs_write_one_cbuf
=
416 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
417 TGSI_INTERPOLATE_CONSTANT
, false);
420 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
423 static void bind_fs_write_all_cbufs(struct blitter_context_priv
*ctx
)
425 struct pipe_context
*pipe
= ctx
->base
.pipe
;
427 if (!ctx
->fs_write_all_cbufs
) {
428 assert(!ctx
->cached_all_shaders
);
429 ctx
->fs_write_all_cbufs
=
430 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
431 TGSI_INTERPOLATE_CONSTANT
, true);
434 ctx
->bind_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
437 void util_blitter_destroy(struct blitter_context
*blitter
)
439 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
440 struct pipe_context
*pipe
= blitter
->pipe
;
443 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++)
444 for (j
= 0; j
< 2; j
++)
445 pipe
->delete_blend_state(pipe
, ctx
->blend
[i
][j
]);
447 for (i
= 0; i
< ARRAY_SIZE(ctx
->blend_clear
); i
++) {
448 if (ctx
->blend_clear
[i
])
449 pipe
->delete_blend_state(pipe
, ctx
->blend_clear
[i
]);
451 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
452 pipe
->delete_depth_stencil_alpha_state(pipe
,
453 ctx
->dsa_write_depth_keep_stencil
);
454 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
455 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
457 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
);
458 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state_scissor
);
459 if (ctx
->rs_discard_state
)
460 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_discard_state
);
462 pipe
->delete_vs_state(pipe
, ctx
->vs
);
463 if (ctx
->vs_nogeneric
)
464 pipe
->delete_vs_state(pipe
, ctx
->vs_nogeneric
);
465 for (i
= 0; i
< 4; i
++)
466 if (ctx
->vs_pos_only
[i
])
467 pipe
->delete_vs_state(pipe
, ctx
->vs_pos_only
[i
]);
469 pipe
->delete_vs_state(pipe
, ctx
->vs_layered
);
470 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state
);
471 for (i
= 0; i
< 4; i
++) {
472 if (ctx
->velem_state_readbuf
[i
]) {
473 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[i
]);
477 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
478 for (unsigned type
= 0; type
< ARRAY_SIZE(ctx
->fs_texfetch_col
); ++type
) {
479 for (unsigned inst
= 0; inst
< 2; inst
++) {
480 if (ctx
->fs_texfetch_col
[type
][i
][inst
])
481 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[type
][i
][inst
]);
483 if (ctx
->fs_texfetch_col_msaa
[type
][i
])
484 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col_msaa
[type
][i
]);
487 for (unsigned inst
= 0; inst
< 2; inst
++) {
488 if (ctx
->fs_texfetch_depth
[i
][inst
])
489 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
][inst
]);
490 if (ctx
->fs_texfetch_depthstencil
[i
][inst
])
491 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil
[i
][inst
]);
492 if (ctx
->fs_texfetch_stencil
[i
][inst
])
493 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil
[i
][inst
]);
496 if (ctx
->fs_texfetch_depth_msaa
[i
])
497 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth_msaa
[i
]);
498 if (ctx
->fs_texfetch_depthstencil_msaa
[i
])
499 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil_msaa
[i
]);
500 if (ctx
->fs_texfetch_stencil_msaa
[i
])
501 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil_msaa
[i
]);
503 for (j
= 0; j
< ARRAY_SIZE(ctx
->fs_resolve
[i
]); j
++)
504 for (f
= 0; f
< 2; f
++)
505 if (ctx
->fs_resolve
[i
][j
][f
])
506 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve
[i
][j
][f
]);
510 ctx
->delete_fs_state(pipe
, ctx
->fs_empty
);
511 if (ctx
->fs_write_one_cbuf
)
512 ctx
->delete_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
513 if (ctx
->fs_write_all_cbufs
)
514 ctx
->delete_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
516 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect_linear
);
517 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect
);
518 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_linear
);
519 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
);
523 void util_blitter_set_texture_multisample(struct blitter_context
*blitter
,
526 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
528 ctx
->has_texture_multisample
= supported
;
531 void util_blitter_set_running_flag(struct blitter_context
*blitter
)
533 if (blitter
->running
) {
534 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
537 blitter
->running
= true;
539 blitter
->pipe
->set_active_query_state(blitter
->pipe
, false);
542 void util_blitter_unset_running_flag(struct blitter_context
*blitter
)
544 if (!blitter
->running
) {
545 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
548 blitter
->running
= false;
550 blitter
->pipe
->set_active_query_state(blitter
->pipe
, true);
553 static void blitter_check_saved_vertex_states(struct blitter_context_priv
*ctx
)
555 assert(ctx
->base
.saved_velem_state
!= INVALID_PTR
);
556 assert(ctx
->base
.saved_vs
!= INVALID_PTR
);
557 assert(!ctx
->has_geometry_shader
|| ctx
->base
.saved_gs
!= INVALID_PTR
);
558 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tcs
!= INVALID_PTR
);
559 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tes
!= INVALID_PTR
);
560 assert(!ctx
->has_stream_out
|| ctx
->base
.saved_num_so_targets
!= ~0u);
561 assert(ctx
->base
.saved_rs_state
!= INVALID_PTR
);
564 void util_blitter_restore_vertex_states(struct blitter_context
*blitter
)
566 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
567 struct pipe_context
*pipe
= ctx
->base
.pipe
;
571 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1,
572 &ctx
->base
.saved_vertex_buffer
);
573 pipe_vertex_buffer_unreference(&ctx
->base
.saved_vertex_buffer
);
575 /* Vertex elements. */
576 pipe
->bind_vertex_elements_state(pipe
, ctx
->base
.saved_velem_state
);
577 ctx
->base
.saved_velem_state
= INVALID_PTR
;
580 pipe
->bind_vs_state(pipe
, ctx
->base
.saved_vs
);
581 ctx
->base
.saved_vs
= INVALID_PTR
;
583 /* Geometry shader. */
584 if (ctx
->has_geometry_shader
) {
585 pipe
->bind_gs_state(pipe
, ctx
->base
.saved_gs
);
586 ctx
->base
.saved_gs
= INVALID_PTR
;
589 if (ctx
->has_tessellation
) {
590 pipe
->bind_tcs_state(pipe
, ctx
->base
.saved_tcs
);
591 pipe
->bind_tes_state(pipe
, ctx
->base
.saved_tes
);
592 ctx
->base
.saved_tcs
= INVALID_PTR
;
593 ctx
->base
.saved_tes
= INVALID_PTR
;
596 /* Stream outputs. */
597 if (ctx
->has_stream_out
) {
598 unsigned offsets
[PIPE_MAX_SO_BUFFERS
];
599 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
600 offsets
[i
] = (unsigned)-1;
601 pipe
->set_stream_output_targets(pipe
,
602 ctx
->base
.saved_num_so_targets
,
603 ctx
->base
.saved_so_targets
, offsets
);
605 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
606 pipe_so_target_reference(&ctx
->base
.saved_so_targets
[i
], NULL
);
608 ctx
->base
.saved_num_so_targets
= ~0;
612 pipe
->bind_rasterizer_state(pipe
, ctx
->base
.saved_rs_state
);
613 ctx
->base
.saved_rs_state
= INVALID_PTR
;
616 static void blitter_check_saved_fragment_states(struct blitter_context_priv
*ctx
)
618 assert(ctx
->base
.saved_fs
!= INVALID_PTR
);
619 assert(ctx
->base
.saved_dsa_state
!= INVALID_PTR
);
620 assert(ctx
->base
.saved_blend_state
!= INVALID_PTR
);
623 void util_blitter_restore_fragment_states(struct blitter_context
*blitter
)
625 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
626 struct pipe_context
*pipe
= ctx
->base
.pipe
;
628 /* Fragment shader. */
629 ctx
->bind_fs_state(pipe
, ctx
->base
.saved_fs
);
630 ctx
->base
.saved_fs
= INVALID_PTR
;
632 /* Depth, stencil, alpha. */
633 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->base
.saved_dsa_state
);
634 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
637 pipe
->bind_blend_state(pipe
, ctx
->base
.saved_blend_state
);
638 ctx
->base
.saved_blend_state
= INVALID_PTR
;
641 if (ctx
->base
.is_sample_mask_saved
) {
642 pipe
->set_sample_mask(pipe
, ctx
->base
.saved_sample_mask
);
643 ctx
->base
.is_sample_mask_saved
= false;
646 /* Miscellaneous states. */
647 /* XXX check whether these are saved and whether they need to be restored
648 * (depending on the operation) */
649 pipe
->set_stencil_ref(pipe
, &ctx
->base
.saved_stencil_ref
);
650 pipe
->set_viewport_states(pipe
, 0, 1, &ctx
->base
.saved_viewport
);
653 static void blitter_check_saved_fb_state(struct blitter_context_priv
*ctx
)
655 assert(ctx
->base
.saved_fb_state
.nr_cbufs
!= ~0u);
658 static void blitter_disable_render_cond(struct blitter_context_priv
*ctx
)
660 struct pipe_context
*pipe
= ctx
->base
.pipe
;
662 if (ctx
->base
.saved_render_cond_query
) {
663 pipe
->render_condition(pipe
, NULL
, false, 0);
667 void util_blitter_restore_render_cond(struct blitter_context
*blitter
)
669 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
670 struct pipe_context
*pipe
= ctx
->base
.pipe
;
672 if (ctx
->base
.saved_render_cond_query
) {
673 pipe
->render_condition(pipe
, ctx
->base
.saved_render_cond_query
,
674 ctx
->base
.saved_render_cond_cond
,
675 ctx
->base
.saved_render_cond_mode
);
676 ctx
->base
.saved_render_cond_query
= NULL
;
680 void util_blitter_restore_fb_state(struct blitter_context
*blitter
)
682 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
683 struct pipe_context
*pipe
= ctx
->base
.pipe
;
685 pipe
->set_framebuffer_state(pipe
, &ctx
->base
.saved_fb_state
);
686 util_unreference_framebuffer_state(&ctx
->base
.saved_fb_state
);
689 static void blitter_check_saved_textures(struct blitter_context_priv
*ctx
)
691 assert(ctx
->base
.saved_num_sampler_states
!= ~0u);
692 assert(ctx
->base
.saved_num_sampler_views
!= ~0u);
695 void util_blitter_restore_textures(struct blitter_context
*blitter
)
697 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
698 struct pipe_context
*pipe
= ctx
->base
.pipe
;
701 /* Fragment sampler states. */
702 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0,
703 ctx
->base
.saved_num_sampler_states
,
704 ctx
->base
.saved_sampler_states
);
706 ctx
->base
.saved_num_sampler_states
= ~0;
708 /* Fragment sampler views. */
709 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
710 ctx
->base
.saved_num_sampler_views
,
711 ctx
->base
.saved_sampler_views
);
713 for (i
= 0; i
< ctx
->base
.saved_num_sampler_views
; i
++)
714 pipe_sampler_view_reference(&ctx
->base
.saved_sampler_views
[i
], NULL
);
716 ctx
->base
.saved_num_sampler_views
= ~0;
719 void util_blitter_restore_constant_buffer_state(struct blitter_context
*blitter
)
721 struct pipe_context
*pipe
= blitter
->pipe
;
723 pipe
->set_constant_buffer(pipe
, PIPE_SHADER_FRAGMENT
, blitter
->cb_slot
,
724 &blitter
->saved_fs_constant_buffer
);
725 pipe_resource_reference(&blitter
->saved_fs_constant_buffer
.buffer
, NULL
);
728 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
729 int x1
, int y1
, int x2
, int y2
,
734 /* set vertex positions */
735 ctx
->vertices
[0][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v0.x*/
736 ctx
->vertices
[0][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v0.y*/
738 ctx
->vertices
[1][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v1.x*/
739 ctx
->vertices
[1][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v1.y*/
741 ctx
->vertices
[2][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v2.x*/
742 ctx
->vertices
[2][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v2.y*/
744 ctx
->vertices
[3][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v3.x*/
745 ctx
->vertices
[3][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v3.y*/
747 for (i
= 0; i
< 4; i
++)
748 ctx
->vertices
[i
][0][2] = depth
; /*z*/
751 struct pipe_viewport_state viewport
;
752 viewport
.scale
[0] = 0.5f
* ctx
->dst_width
;
753 viewport
.scale
[1] = 0.5f
* ctx
->dst_height
;
754 viewport
.scale
[2] = 1.0f
;
755 viewport
.translate
[0] = 0.5f
* ctx
->dst_width
;
756 viewport
.translate
[1] = 0.5f
* ctx
->dst_height
;
757 viewport
.translate
[2] = 0.0f
;
758 ctx
->base
.pipe
->set_viewport_states(ctx
->base
.pipe
, 0, 1, &viewport
);
761 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
762 const float color
[4])
767 for (i
= 0; i
< 4; i
++)
768 memcpy(&ctx
->vertices
[i
][1][0], color
, sizeof(uint32_t) * 4);
770 for (i
= 0; i
< 4; i
++)
771 memset(&ctx
->vertices
[i
][1][0], 0, sizeof(uint32_t) * 4);
775 static void get_texcoords(struct pipe_sampler_view
*src
,
776 unsigned src_width0
, unsigned src_height0
,
777 int x1
, int y1
, int x2
, int y2
,
778 float layer
, unsigned sample
,
779 bool uses_txf
, union blitter_attrib
*out
)
781 unsigned level
= src
->u
.tex
.first_level
;
782 bool normalized
= !uses_txf
&&
783 src
->target
!= PIPE_TEXTURE_RECT
&&
784 src
->texture
->nr_samples
<= 1;
787 out
->texcoord
.x1
= x1
/ (float)u_minify(src_width0
, level
);
788 out
->texcoord
.y1
= y1
/ (float)u_minify(src_height0
, level
);
789 out
->texcoord
.x2
= x2
/ (float)u_minify(src_width0
, level
);
790 out
->texcoord
.y2
= y2
/ (float)u_minify(src_height0
, level
);
792 out
->texcoord
.x1
= x1
;
793 out
->texcoord
.y1
= y1
;
794 out
->texcoord
.x2
= x2
;
795 out
->texcoord
.y2
= y2
;
802 switch (src
->target
) {
803 case PIPE_TEXTURE_3D
:
808 r
/= u_minify(src
->texture
->depth0
, src
->u
.tex
.first_level
);
814 case PIPE_TEXTURE_1D_ARRAY
:
815 out
->texcoord
.y1
= out
->texcoord
.y2
= layer
;
818 case PIPE_TEXTURE_2D_ARRAY
:
819 out
->texcoord
.z
= layer
;
820 out
->texcoord
.w
= sample
;
823 case PIPE_TEXTURE_CUBE_ARRAY
:
824 out
->texcoord
.w
= (unsigned)layer
/ 6;
827 case PIPE_TEXTURE_2D
:
828 out
->texcoord
.w
= sample
;
835 static void blitter_set_dst_dimensions(struct blitter_context_priv
*ctx
,
836 unsigned width
, unsigned height
)
838 ctx
->dst_width
= width
;
839 ctx
->dst_height
= height
;
842 static void set_texcoords_in_vertices(const union blitter_attrib
*attrib
,
843 float *out
, unsigned stride
)
845 out
[0] = attrib
->texcoord
.x1
;
846 out
[1] = attrib
->texcoord
.y1
;
848 out
[0] = attrib
->texcoord
.x2
;
849 out
[1] = attrib
->texcoord
.y1
;
851 out
[0] = attrib
->texcoord
.x2
;
852 out
[1] = attrib
->texcoord
.y2
;
854 out
[0] = attrib
->texcoord
.x1
;
855 out
[1] = attrib
->texcoord
.y2
;
858 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
859 enum pipe_format src_format
,
860 enum pipe_format dst_format
,
861 enum pipe_texture_target target
,
862 unsigned src_nr_samples
,
863 unsigned dst_nr_samples
,
867 struct pipe_context
*pipe
= ctx
->base
.pipe
;
868 enum tgsi_texture_type tgsi_tex
=
869 util_pipe_tex_to_tgsi_tex(target
, src_nr_samples
);
870 enum tgsi_return_type stype
;
871 enum tgsi_return_type dtype
;
874 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
876 if (util_format_is_pure_uint(src_format
)) {
877 stype
= TGSI_RETURN_TYPE_UINT
;
878 if (util_format_is_pure_uint(dst_format
)) {
879 dtype
= TGSI_RETURN_TYPE_UINT
;
882 assert(util_format_is_pure_sint(dst_format
));
883 dtype
= TGSI_RETURN_TYPE_SINT
;
886 } else if (util_format_is_pure_sint(src_format
)) {
887 stype
= TGSI_RETURN_TYPE_SINT
;
888 if (util_format_is_pure_sint(dst_format
)) {
889 dtype
= TGSI_RETURN_TYPE_SINT
;
892 assert(util_format_is_pure_uint(dst_format
));
893 dtype
= TGSI_RETURN_TYPE_UINT
;
897 assert(!util_format_is_pure_uint(dst_format
) &&
898 !util_format_is_pure_sint(dst_format
));
899 dtype
= stype
= TGSI_RETURN_TYPE_FLOAT
;
903 if (src_nr_samples
> 1) {
906 /* OpenGL requires that integer textures just copy 1 sample instead
909 if (dst_nr_samples
<= 1 &&
910 stype
!= TGSI_RETURN_TYPE_UINT
&&
911 stype
!= TGSI_RETURN_TYPE_SINT
) {
912 /* The destination has one sample, so we'll do color resolve. */
913 unsigned index
= GET_MSAA_RESOLVE_FS_IDX(src_nr_samples
);
917 shader
= &ctx
->fs_resolve
[target
][index
][filter
];
920 assert(!ctx
->cached_all_shaders
);
921 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
922 *shader
= util_make_fs_msaa_resolve_bilinear(pipe
, tgsi_tex
,
927 *shader
= util_make_fs_msaa_resolve(pipe
, tgsi_tex
,
934 /* The destination has multiple samples, we'll do
935 * an MSAA->MSAA copy.
937 shader
= &ctx
->fs_texfetch_col_msaa
[type
][target
];
939 /* Create the fragment shader on-demand. */
941 assert(!ctx
->cached_all_shaders
);
942 *shader
= util_make_fs_blit_msaa_color(pipe
, tgsi_tex
, stype
, dtype
);
951 shader
= &ctx
->fs_texfetch_col
[type
][target
][1];
953 shader
= &ctx
->fs_texfetch_col
[type
][target
][0];
955 /* Create the fragment shader on-demand. */
957 assert(!ctx
->cached_all_shaders
);
958 *shader
= util_make_fragment_tex_shader(pipe
, tgsi_tex
,
959 TGSI_INTERPOLATE_LINEAR
,
961 ctx
->has_tex_lz
, use_txf
);
969 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
970 enum pipe_texture_target target
,
974 struct pipe_context
*pipe
= ctx
->base
.pipe
;
976 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
978 if (nr_samples
> 1) {
979 void **shader
= &ctx
->fs_texfetch_depth_msaa
[target
];
981 /* Create the fragment shader on-demand. */
983 enum tgsi_texture_type tgsi_tex
;
984 assert(!ctx
->cached_all_shaders
);
985 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
986 *shader
= util_make_fs_blit_msaa_depth(pipe
, tgsi_tex
);
994 shader
= &ctx
->fs_texfetch_depth
[target
][1];
996 shader
= &ctx
->fs_texfetch_depth
[target
][0];
998 /* Create the fragment shader on-demand. */
1000 enum tgsi_texture_type tgsi_tex
;
1001 assert(!ctx
->cached_all_shaders
);
1002 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1004 util_make_fragment_tex_shader_writedepth(pipe
, tgsi_tex
,
1005 TGSI_INTERPOLATE_LINEAR
,
1006 ctx
->has_tex_lz
, use_txf
);
1014 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv
*ctx
,
1015 enum pipe_texture_target target
,
1016 unsigned nr_samples
,
1019 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1021 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1023 if (nr_samples
> 1) {
1024 void **shader
= &ctx
->fs_texfetch_depthstencil_msaa
[target
];
1026 /* Create the fragment shader on-demand. */
1028 enum tgsi_texture_type tgsi_tex
;
1029 assert(!ctx
->cached_all_shaders
);
1030 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1031 *shader
= util_make_fs_blit_msaa_depthstencil(pipe
, tgsi_tex
);
1039 shader
= &ctx
->fs_texfetch_depthstencil
[target
][1];
1041 shader
= &ctx
->fs_texfetch_depthstencil
[target
][0];
1043 /* Create the fragment shader on-demand. */
1045 enum tgsi_texture_type tgsi_tex
;
1046 assert(!ctx
->cached_all_shaders
);
1047 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1049 util_make_fragment_tex_shader_writedepthstencil(pipe
, tgsi_tex
,
1050 TGSI_INTERPOLATE_LINEAR
,
1060 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv
*ctx
,
1061 enum pipe_texture_target target
,
1062 unsigned nr_samples
,
1065 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1067 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1069 if (nr_samples
> 1) {
1070 void **shader
= &ctx
->fs_texfetch_stencil_msaa
[target
];
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
, nr_samples
);
1077 *shader
= util_make_fs_blit_msaa_stencil(pipe
, tgsi_tex
);
1085 shader
= &ctx
->fs_texfetch_stencil
[target
][1];
1087 shader
= &ctx
->fs_texfetch_stencil
[target
][0];
1089 /* Create the fragment shader on-demand. */
1091 enum tgsi_texture_type tgsi_tex
;
1092 assert(!ctx
->cached_all_shaders
);
1093 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1095 util_make_fragment_tex_shader_writestencil(pipe
, tgsi_tex
,
1096 TGSI_INTERPOLATE_LINEAR
,
1097 ctx
->has_tex_lz
, use_txf
);
1106 * Generate and save all fragment shaders that we will ever need for
1107 * blitting. Drivers which use the 'draw' fallbacks will typically use
1108 * this to make sure we generate/use shaders that don't go through the
1109 * draw module's wrapper functions.
1111 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
)
1113 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1114 struct pipe_context
*pipe
= blitter
->pipe
;
1115 struct pipe_screen
*screen
= pipe
->screen
;
1116 unsigned samples
, j
, f
, target
, max_samples
, use_txf
;
1117 bool has_arraytex
, has_cubearraytex
;
1119 max_samples
= ctx
->has_texture_multisample
? 2 : 1;
1120 has_arraytex
= screen
->get_param(screen
,
1121 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
) != 0;
1122 has_cubearraytex
= screen
->get_param(screen
,
1123 PIPE_CAP_CUBE_MAP_ARRAY
) != 0;
1125 /* It only matters if i <= 1 or > 1. */
1126 for (samples
= 1; samples
<= max_samples
; samples
++) {
1127 for (target
= PIPE_TEXTURE_1D
; target
< PIPE_MAX_TEXTURE_TYPES
; target
++) {
1128 for (use_txf
= 0; use_txf
<= ctx
->has_txf
; use_txf
++) {
1129 if (!has_arraytex
&&
1130 (target
== PIPE_TEXTURE_1D_ARRAY
||
1131 target
== PIPE_TEXTURE_2D_ARRAY
)) {
1134 if (!has_cubearraytex
&&
1135 (target
== PIPE_TEXTURE_CUBE_ARRAY
))
1139 (target
!= PIPE_TEXTURE_2D
&&
1140 target
!= PIPE_TEXTURE_2D_ARRAY
))
1143 if (samples
> 1 && use_txf
)
1144 continue; /* TXF is the only option, use_txf has no effect */
1146 /* If samples == 1, the shaders read one texel. If samples >= 1,
1147 * they read one sample.
1149 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
,
1150 PIPE_FORMAT_R32_FLOAT
, target
,
1151 samples
, samples
, 0, use_txf
);
1152 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1153 PIPE_FORMAT_R32_UINT
, target
,
1154 samples
, samples
, 0, use_txf
);
1155 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1156 PIPE_FORMAT_R32_SINT
, target
,
1157 samples
, samples
, 0, use_txf
);
1158 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1159 PIPE_FORMAT_R32_SINT
, target
,
1160 samples
, samples
, 0, use_txf
);
1161 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1162 PIPE_FORMAT_R32_UINT
, target
,
1163 samples
, samples
, 0, use_txf
);
1164 blitter_get_fs_texfetch_depth(ctx
, target
, samples
, use_txf
);
1165 if (ctx
->has_stencil_export
) {
1166 blitter_get_fs_texfetch_depthstencil(ctx
, target
, samples
, use_txf
);
1167 blitter_get_fs_texfetch_stencil(ctx
, target
, samples
, use_txf
);
1173 /* MSAA resolve shaders. */
1174 for (j
= 2; j
< 32; j
++) {
1175 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R32_FLOAT
,
1177 PIPE_BIND_SAMPLER_VIEW
)) {
1181 for (f
= 0; f
< 2; f
++) {
1182 if (f
!= PIPE_TEX_FILTER_NEAREST
&& use_txf
)
1185 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
,
1186 PIPE_FORMAT_R32_FLOAT
, target
,
1188 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1189 PIPE_FORMAT_R32_UINT
, target
,
1191 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1192 PIPE_FORMAT_R32_SINT
, target
,
1200 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
1202 ctx
->fs_write_one_cbuf
=
1203 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1204 TGSI_INTERPOLATE_CONSTANT
, false);
1206 ctx
->fs_write_all_cbufs
=
1207 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1208 TGSI_INTERPOLATE_CONSTANT
, true);
1210 ctx
->cached_all_shaders
= true;
1213 static void blitter_set_common_draw_rect_state(struct blitter_context_priv
*ctx
,
1216 bool vs_pass_generic
)
1218 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1220 pipe
->bind_rasterizer_state(pipe
, scissor
? ctx
->rs_state_scissor
1223 bind_vs_layered(ctx
);
1224 else if (vs_pass_generic
)
1225 bind_vs_passthrough_pos_generic(ctx
);
1227 bind_vs_passthrough_pos(ctx
);
1229 if (ctx
->has_geometry_shader
)
1230 pipe
->bind_gs_state(pipe
, NULL
);
1231 if (ctx
->has_tessellation
) {
1232 pipe
->bind_tcs_state(pipe
, NULL
);
1233 pipe
->bind_tes_state(pipe
, NULL
);
1235 if (ctx
->has_stream_out
)
1236 pipe
->set_stream_output_targets(pipe
, 0, NULL
, NULL
);
1239 static void blitter_draw(struct blitter_context_priv
*ctx
,
1240 void *vertex_elements_cso
,
1241 int x1
, int y1
, int x2
, int y2
, float depth
,
1242 unsigned num_instances
)
1244 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1245 struct pipe_vertex_buffer vb
= {0};
1247 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
1249 vb
.stride
= 8 * sizeof(float);
1251 u_upload_data(pipe
->stream_uploader
, 0, sizeof(ctx
->vertices
), 4, ctx
->vertices
,
1252 &vb
.buffer_offset
, &vb
.buffer
.resource
);
1253 if (!vb
.buffer
.resource
)
1255 u_upload_unmap(pipe
->stream_uploader
);
1257 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1258 pipe
->bind_vertex_elements_state(pipe
, vertex_elements_cso
);
1259 util_draw_arrays_instanced(pipe
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
1261 pipe_resource_reference(&vb
.buffer
.resource
, NULL
);
1264 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
1265 void *vertex_elements_cso
,
1266 int x1
, int y1
, int x2
, int y2
,
1267 float depth
, unsigned num_instances
,
1268 enum blitter_attrib_type type
,
1269 const union blitter_attrib
*attrib
)
1271 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1275 case UTIL_BLITTER_ATTRIB_COLOR
:
1276 blitter_set_clear_color(ctx
, attrib
->color
);
1279 case UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW
:
1280 for (i
= 0; i
< 4; i
++) {
1281 ctx
->vertices
[i
][1][2] = attrib
->texcoord
.z
;
1282 ctx
->vertices
[i
][1][3] = attrib
->texcoord
.w
;
1285 case UTIL_BLITTER_ATTRIB_TEXCOORD_XY
:
1286 set_texcoords_in_vertices(attrib
, &ctx
->vertices
[0][1][0], 8);
1292 blitter_draw(ctx
, vertex_elements_cso
, x1
, y1
, x2
, y2
, depth
, num_instances
);
1295 static void *get_clear_blend_state(struct blitter_context_priv
*ctx
,
1296 unsigned clear_buffers
)
1298 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1301 clear_buffers
&= PIPE_CLEAR_COLOR
;
1303 /* Return an existing blend state. */
1305 return ctx
->blend
[0][0];
1307 index
= GET_CLEAR_BLEND_STATE_IDX(clear_buffers
);
1309 if (ctx
->blend_clear
[index
])
1310 return ctx
->blend_clear
[index
];
1312 /* Create a new one. */
1314 struct pipe_blend_state blend
= {0};
1317 blend
.independent_blend_enable
= 1;
1319 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1320 if (clear_buffers
& (PIPE_CLEAR_COLOR0
<< i
)) {
1321 blend
.rt
[i
].colormask
= PIPE_MASK_RGBA
;
1325 ctx
->blend_clear
[index
] = pipe
->create_blend_state(pipe
, &blend
);
1327 return ctx
->blend_clear
[index
];
1330 void util_blitter_common_clear_setup(struct blitter_context
*blitter
,
1331 unsigned width
, unsigned height
,
1332 unsigned clear_buffers
,
1333 void *custom_blend
, void *custom_dsa
)
1335 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1336 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1338 util_blitter_set_running_flag(blitter
);
1339 blitter_check_saved_vertex_states(ctx
);
1340 blitter_check_saved_fragment_states(ctx
);
1341 blitter_disable_render_cond(ctx
);
1345 pipe
->bind_blend_state(pipe
, custom_blend
);
1347 pipe
->bind_blend_state(pipe
, get_clear_blend_state(ctx
, clear_buffers
));
1351 pipe
->bind_depth_stencil_alpha_state(pipe
, custom_dsa
);
1352 } else if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1353 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1354 } else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
1355 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1356 } else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
1357 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1359 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1362 pipe
->set_sample_mask(pipe
, ~0);
1363 blitter_set_dst_dimensions(ctx
, width
, height
);
1366 static void util_blitter_clear_custom(struct blitter_context
*blitter
,
1367 unsigned width
, unsigned height
,
1368 unsigned num_layers
,
1369 unsigned clear_buffers
,
1370 const union pipe_color_union
*color
,
1371 double depth
, unsigned stencil
,
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
;
1376 struct pipe_stencil_ref sr
= { { 0 } };
1378 assert(ctx
->has_layered
|| num_layers
<= 1);
1380 util_blitter_common_clear_setup(blitter
, width
, height
, clear_buffers
,
1381 custom_blend
, custom_dsa
);
1383 sr
.ref_value
[0] = stencil
& 0xff;
1384 pipe
->set_stencil_ref(pipe
, &sr
);
1386 bind_fs_write_all_cbufs(ctx
);
1388 union blitter_attrib attrib
;
1389 memcpy(attrib
.color
, color
->ui
, sizeof(color
->ui
));
1391 bool pass_generic
= (clear_buffers
& PIPE_CLEAR_COLOR
) != 0;
1392 enum blitter_attrib_type type
= pass_generic
? UTIL_BLITTER_ATTRIB_COLOR
:
1393 UTIL_BLITTER_ATTRIB_NONE
;
1395 if (num_layers
> 1 && ctx
->has_layered
) {
1396 blitter_set_common_draw_rect_state(ctx
, false, true, pass_generic
);
1397 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, 0, 0, width
, height
,
1398 (float) depth
, num_layers
, type
, &attrib
);
1400 blitter_set_common_draw_rect_state(ctx
, false, false, pass_generic
);
1401 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, 0, 0, width
, height
,
1402 (float) depth
, 1, type
, &attrib
);
1405 util_blitter_restore_vertex_states(blitter
);
1406 util_blitter_restore_fragment_states(blitter
);
1407 util_blitter_restore_render_cond(blitter
);
1408 util_blitter_unset_running_flag(blitter
);
1411 void util_blitter_clear(struct blitter_context
*blitter
,
1412 unsigned width
, unsigned height
, unsigned num_layers
,
1413 unsigned clear_buffers
,
1414 const union pipe_color_union
*color
,
1415 double depth
, unsigned stencil
)
1417 util_blitter_clear_custom(blitter
, width
, height
, num_layers
,
1418 clear_buffers
, color
, depth
, stencil
,
1422 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1423 unsigned width
, unsigned height
,
1424 double depth
, void *custom_dsa
)
1426 static const union pipe_color_union color
;
1427 util_blitter_clear_custom(blitter
, width
, height
, 0, 0, &color
, depth
, 0,
1431 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1432 struct pipe_resource
*dst
,
1436 memset(dst_templ
, 0, sizeof(*dst_templ
));
1437 dst_templ
->format
= util_format_linear(dst
->format
);
1438 dst_templ
->u
.tex
.level
= dstlevel
;
1439 dst_templ
->u
.tex
.first_layer
= dstz
;
1440 dst_templ
->u
.tex
.last_layer
= dstz
;
1443 static struct pipe_surface
*
1444 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
1445 struct pipe_surface
*surf
)
1447 struct pipe_surface dst_templ
;
1449 memset(&dst_templ
, 0, sizeof(dst_templ
));
1450 dst_templ
.format
= surf
->format
;
1451 dst_templ
.u
.tex
.level
= surf
->u
.tex
.level
;
1452 dst_templ
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
+ 1;
1453 dst_templ
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
+ 1;
1455 return pipe
->create_surface(pipe
, surf
->texture
, &dst_templ
);
1458 void util_blitter_default_src_texture(struct blitter_context
*blitter
,
1459 struct pipe_sampler_view
*src_templ
,
1460 struct pipe_resource
*src
,
1463 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1465 memset(src_templ
, 0, sizeof(*src_templ
));
1467 if (ctx
->cube_as_2darray
&&
1468 (src
->target
== PIPE_TEXTURE_CUBE
||
1469 src
->target
== PIPE_TEXTURE_CUBE_ARRAY
))
1470 src_templ
->target
= PIPE_TEXTURE_2D_ARRAY
;
1472 src_templ
->target
= src
->target
;
1474 src_templ
->format
= util_format_linear(src
->format
);
1475 src_templ
->u
.tex
.first_level
= srclevel
;
1476 src_templ
->u
.tex
.last_level
= srclevel
;
1477 src_templ
->u
.tex
.first_layer
= 0;
1478 src_templ
->u
.tex
.last_layer
=
1479 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1480 : src
->array_size
- 1;
1481 src_templ
->swizzle_r
= PIPE_SWIZZLE_X
;
1482 src_templ
->swizzle_g
= PIPE_SWIZZLE_Y
;
1483 src_templ
->swizzle_b
= PIPE_SWIZZLE_Z
;
1484 src_templ
->swizzle_a
= PIPE_SWIZZLE_W
;
1487 static bool is_blit_generic_supported(struct blitter_context
*blitter
,
1488 const struct pipe_resource
*dst
,
1489 enum pipe_format dst_format
,
1490 const struct pipe_resource
*src
,
1491 enum pipe_format src_format
,
1494 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1495 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1499 const struct util_format_description
*desc
=
1500 util_format_description(dst_format
);
1501 bool dst_has_stencil
= util_format_has_stencil(desc
);
1503 /* Stencil export must be supported for stencil copy. */
1504 if ((mask
& PIPE_MASK_S
) && dst_has_stencil
&&
1505 !ctx
->has_stencil_export
) {
1509 if (dst_has_stencil
|| util_format_has_depth(desc
))
1510 bind
= PIPE_BIND_DEPTH_STENCIL
;
1512 bind
= PIPE_BIND_RENDER_TARGET
;
1514 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1515 dst
->nr_samples
, bind
)) {
1521 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1525 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1526 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
)) {
1530 /* Check stencil sampler support for stencil copy. */
1531 if (mask
& PIPE_MASK_S
) {
1532 if (util_format_has_stencil(util_format_description(src_format
))) {
1533 enum pipe_format stencil_format
=
1534 util_format_stencil_only(src_format
);
1535 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1537 if (stencil_format
!= src_format
&&
1538 !screen
->is_format_supported(screen
, stencil_format
,
1539 src
->target
, src
->nr_samples
,
1540 PIPE_BIND_SAMPLER_VIEW
)) {
1550 bool util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1551 const struct pipe_resource
*dst
,
1552 const struct pipe_resource
*src
)
1554 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1555 src
, src
->format
, PIPE_MASK_RGBAZS
);
1558 bool util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1559 const struct pipe_blit_info
*info
)
1561 return is_blit_generic_supported(blitter
,
1562 info
->dst
.resource
, info
->dst
.format
,
1563 info
->src
.resource
, info
->src
.format
,
1567 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1568 struct pipe_resource
*dst
,
1570 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1571 struct pipe_resource
*src
,
1573 const struct pipe_box
*srcbox
)
1575 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1576 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1577 struct pipe_surface
*dst_view
, dst_templ
;
1578 struct pipe_sampler_view src_templ
, *src_view
;
1579 struct pipe_box dstbox
;
1582 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1584 u_box_3d(dstx
, dsty
, dstz
, abs(srcbox
->width
), abs(srcbox
->height
),
1585 abs(srcbox
->depth
), &dstbox
);
1587 /* Initialize the surface. */
1588 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1589 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1591 /* Initialize the sampler view. */
1592 util_blitter_default_src_texture(blitter
, &src_templ
, src
, src_level
);
1593 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1596 util_blitter_blit_generic(blitter
, dst_view
, &dstbox
,
1597 src_view
, srcbox
, src
->width0
, src
->height0
,
1598 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
,
1601 pipe_surface_reference(&dst_view
, NULL
);
1602 pipe_sampler_view_reference(&src_view
, NULL
);
1606 blitter_draw_tex(struct blitter_context_priv
*ctx
,
1607 int dst_x1
, int dst_y1
, int dst_x2
, int dst_y2
,
1608 struct pipe_sampler_view
*src
,
1609 unsigned src_width0
, unsigned src_height0
,
1610 int src_x1
, int src_y1
, int src_x2
, int src_y2
,
1611 float layer
, unsigned sample
,
1612 bool uses_txf
, enum blitter_attrib_type type
)
1614 union blitter_attrib coord
;
1616 get_texcoords(src
, src_width0
, src_height0
,
1617 src_x1
, src_y1
, src_x2
, src_y2
, layer
, sample
,
1620 if (src
->target
== PIPE_TEXTURE_CUBE
||
1621 src
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
1622 float face_coord
[4][2];
1624 set_texcoords_in_vertices(&coord
, &face_coord
[0][0], 2);
1625 util_map_texcoords2d_onto_cubemap((unsigned)layer
% 6,
1626 /* pointer, stride in floats */
1627 &face_coord
[0][0], 2,
1628 &ctx
->vertices
[0][1][0], 8,
1630 for (unsigned i
= 0; i
< 4; i
++)
1631 ctx
->vertices
[i
][1][3] = coord
.texcoord
.w
;
1633 /* Cubemaps don't use draw_rectangle. */
1634 blitter_draw(ctx
, ctx
->velem_state
,
1635 dst_x1
, dst_y1
, dst_x2
, dst_y2
, 0, 1);
1637 ctx
->base
.draw_rectangle(&ctx
->base
, ctx
->velem_state
,
1638 dst_x1
, dst_y1
, dst_x2
, dst_y2
,
1639 0, 1, type
, &coord
);
1643 static void do_blits(struct blitter_context_priv
*ctx
,
1644 struct pipe_surface
*dst
,
1645 const struct pipe_box
*dstbox
,
1646 struct pipe_sampler_view
*src
,
1647 unsigned src_width0
,
1648 unsigned src_height0
,
1649 const struct pipe_box
*srcbox
,
1653 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1654 unsigned src_samples
= src
->texture
->nr_samples
;
1655 unsigned dst_samples
= dst
->texture
->nr_samples
;
1656 enum pipe_texture_target src_target
= src
->target
;
1657 struct pipe_framebuffer_state fb_state
= {0};
1659 /* Initialize framebuffer state. */
1660 fb_state
.width
= dst
->width
;
1661 fb_state
.height
= dst
->height
;
1662 fb_state
.nr_cbufs
= is_zsbuf
? 0 : 1;
1664 blitter_set_dst_dimensions(ctx
, fb_state
.width
, fb_state
.height
);
1666 if ((src_target
== PIPE_TEXTURE_1D
||
1667 src_target
== PIPE_TEXTURE_2D
||
1668 src_target
== PIPE_TEXTURE_RECT
) &&
1670 /* Set framebuffer state. */
1672 fb_state
.zsbuf
= dst
;
1674 fb_state
.cbufs
[0] = dst
;
1676 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1679 pipe
->set_sample_mask(pipe
, ~0);
1680 blitter_draw_tex(ctx
, dstbox
->x
, dstbox
->y
,
1681 dstbox
->x
+ dstbox
->width
,
1682 dstbox
->y
+ dstbox
->height
,
1683 src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1684 srcbox
->x
+ srcbox
->width
, srcbox
->y
+ srcbox
->height
,
1685 0, 0, uses_txf
, UTIL_BLITTER_ATTRIB_TEXCOORD_XY
);
1687 /* Draw the quad with the generic codepath. */
1689 for (dst_z
= 0; dst_z
< dstbox
->depth
; dst_z
++) {
1690 struct pipe_surface
*old
;
1691 float dst2src_scale
= srcbox
->depth
/ (float)dstbox
->depth
;
1693 /* Scale Z properly if the blit is scaled.
1695 * When downscaling, we want the coordinates centered, so that
1696 * mipmapping works for 3D textures. For example, when generating
1697 * a 4x4x4 level, this wouldn't average the pixels:
1699 * src Z: 0 1 2 3 4 5 6 7
1702 * Because the pixels are not centered below the pixels of the higher
1703 * level. Therefore, we want this:
1704 * src Z: 0 1 2 3 4 5 6 7
1707 * dst_offset defines the offset needed for centering the pixels and
1708 * it works with any scaling (not just 2x).
1710 float dst_offset
= ((srcbox
->depth
- 1) -
1711 (dstbox
->depth
- 1) * dst2src_scale
) * 0.5;
1712 float src_z
= (dst_z
+ dst_offset
) * dst2src_scale
;
1714 /* Set framebuffer state. */
1716 fb_state
.zsbuf
= dst
;
1718 fb_state
.cbufs
[0] = dst
;
1720 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1722 /* See if we need to blit a multisample or singlesample buffer. */
1723 if (src_samples
== dst_samples
&& dst_samples
> 1) {
1725 unsigned i
, max_sample
= dst_samples
- 1;
1727 for (i
= 0; i
<= max_sample
; i
++) {
1728 pipe
->set_sample_mask(pipe
, 1 << i
);
1729 blitter_draw_tex(ctx
, dstbox
->x
, dstbox
->y
,
1730 dstbox
->x
+ dstbox
->width
,
1731 dstbox
->y
+ dstbox
->height
,
1732 src
, src_width0
, src_height0
,
1733 srcbox
->x
, srcbox
->y
,
1734 srcbox
->x
+ srcbox
->width
,
1735 srcbox
->y
+ srcbox
->height
,
1736 srcbox
->z
+ src_z
, i
, uses_txf
,
1737 UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW
);
1740 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1741 pipe
->set_sample_mask(pipe
, ~0);
1742 blitter_draw_tex(ctx
, dstbox
->x
, dstbox
->y
,
1743 dstbox
->x
+ dstbox
->width
,
1744 dstbox
->y
+ dstbox
->height
,
1745 src
, src_width0
, src_height0
,
1746 srcbox
->x
, srcbox
->y
,
1747 srcbox
->x
+ srcbox
->width
,
1748 srcbox
->y
+ srcbox
->height
,
1749 srcbox
->z
+ src_z
, 0, uses_txf
,
1750 UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW
);
1753 /* Get the next surface or (if this is the last iteration)
1754 * just unreference the last one. */
1756 if (dst_z
< dstbox
->depth
-1) {
1757 dst
= util_blitter_get_next_surface_layer(ctx
->base
.pipe
, dst
);
1760 pipe_surface_reference(&old
, NULL
);
1766 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1767 struct pipe_surface
*dst
,
1768 const struct pipe_box
*dstbox
,
1769 struct pipe_sampler_view
*src
,
1770 const struct pipe_box
*srcbox
,
1771 unsigned src_width0
, unsigned src_height0
,
1772 unsigned mask
, unsigned filter
,
1773 const struct pipe_scissor_state
*scissor
,
1776 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1777 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1778 enum pipe_texture_target src_target
= src
->target
;
1779 unsigned src_samples
= src
->texture
->nr_samples
;
1780 unsigned dst_samples
= dst
->texture
->nr_samples
;
1781 bool has_depth
, has_stencil
, has_color
;
1782 bool blit_stencil
, blit_depth
, blit_color
;
1783 void *sampler_state
;
1784 const struct util_format_description
*src_desc
=
1785 util_format_description(src
->format
);
1786 const struct util_format_description
*dst_desc
=
1787 util_format_description(dst
->format
);
1789 has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
&&
1790 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1791 has_depth
= util_format_has_depth(src_desc
) &&
1792 util_format_has_depth(dst_desc
);
1793 has_stencil
= util_format_has_stencil(src_desc
) &&
1794 util_format_has_stencil(dst_desc
);
1796 blit_color
= has_color
&& (mask
& PIPE_MASK_RGBA
);
1797 blit_depth
= has_depth
&& (mask
& PIPE_MASK_Z
);
1798 blit_stencil
= has_stencil
&& (mask
& PIPE_MASK_S
) &&
1799 ctx
->has_stencil_export
;
1801 if (!blit_stencil
&& !blit_depth
&& !blit_color
) {
1805 bool is_scaled
= dstbox
->width
!= abs(srcbox
->width
) ||
1806 dstbox
->height
!= abs(srcbox
->height
);
1808 if (blit_stencil
|| !is_scaled
)
1809 filter
= PIPE_TEX_FILTER_NEAREST
;
1811 bool use_txf
= false;
1813 /* Don't support scaled blits. The TXF shader uses F2I for rounding. */
1816 filter
== PIPE_TEX_FILTER_NEAREST
&&
1817 src
->target
!= PIPE_TEXTURE_CUBE
&&
1818 src
->target
!= PIPE_TEXTURE_CUBE_ARRAY
) {
1819 int src_width
= u_minify(src_width0
, src
->u
.tex
.first_level
);
1820 int src_height
= u_minify(src_height0
, src
->u
.tex
.first_level
);
1821 int src_depth
= src
->u
.tex
.last_layer
+ 1;
1822 struct pipe_box box
= *srcbox
;
1824 /* Eliminate negative width/height/depth. */
1825 if (box
.width
< 0) {
1829 if (box
.height
< 0) {
1830 box
.y
+= box
.height
;
1833 if (box
.depth
< 0) {
1838 /* See if srcbox is in bounds. TXF doesn't clamp the coordinates. */
1840 box
.x
>= 0 && box
.x
< src_width
&&
1841 box
.y
>= 0 && box
.y
< src_height
&&
1842 box
.z
>= 0 && box
.z
< src_depth
&&
1843 box
.x
+ box
.width
> 0 && box
.x
+ box
.width
<= src_width
&&
1844 box
.y
+ box
.height
> 0 && box
.y
+ box
.height
<= src_height
&&
1845 box
.z
+ box
.depth
> 0 && box
.z
+ box
.depth
<= src_depth
;
1848 /* Check whether the states are properly saved. */
1849 util_blitter_set_running_flag(blitter
);
1850 blitter_check_saved_vertex_states(ctx
);
1851 blitter_check_saved_fragment_states(ctx
);
1852 blitter_check_saved_textures(ctx
);
1853 blitter_check_saved_fb_state(ctx
);
1854 blitter_disable_render_cond(ctx
);
1856 if (blit_depth
|| blit_stencil
) {
1857 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
1859 if (blit_depth
&& blit_stencil
) {
1860 pipe
->bind_depth_stencil_alpha_state(pipe
,
1861 ctx
->dsa_write_depth_stencil
);
1862 ctx
->bind_fs_state(pipe
,
1863 blitter_get_fs_texfetch_depthstencil(ctx
, src_target
,
1864 src_samples
, use_txf
));
1865 } else if (blit_depth
) {
1866 pipe
->bind_depth_stencil_alpha_state(pipe
,
1867 ctx
->dsa_write_depth_keep_stencil
);
1868 ctx
->bind_fs_state(pipe
,
1869 blitter_get_fs_texfetch_depth(ctx
, src_target
,
1870 src_samples
, use_txf
));
1871 } else { /* is_stencil */
1872 pipe
->bind_depth_stencil_alpha_state(pipe
,
1873 ctx
->dsa_keep_depth_write_stencil
);
1874 ctx
->bind_fs_state(pipe
,
1875 blitter_get_fs_texfetch_stencil(ctx
, src_target
,
1876 src_samples
, use_txf
));
1880 unsigned colormask
= mask
& PIPE_MASK_RGBA
;
1882 pipe
->bind_blend_state(pipe
, ctx
->blend
[colormask
][alpha_blend
]);
1883 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1884 ctx
->bind_fs_state(pipe
,
1885 blitter_get_fs_texfetch_col(ctx
, src
->format
, dst
->format
, src_target
,
1886 src_samples
, dst_samples
, filter
,
1890 /* Set the linear filter only for scaled color non-MSAA blits. */
1891 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
1892 if (src_target
== PIPE_TEXTURE_RECT
) {
1893 sampler_state
= ctx
->sampler_state_rect_linear
;
1895 sampler_state
= ctx
->sampler_state_linear
;
1898 if (src_target
== PIPE_TEXTURE_RECT
) {
1899 sampler_state
= ctx
->sampler_state_rect
;
1901 sampler_state
= ctx
->sampler_state
;
1906 if (blit_depth
&& blit_stencil
) {
1907 /* Setup two samplers, one for depth and the other one for stencil. */
1908 struct pipe_sampler_view templ
;
1909 struct pipe_sampler_view
*views
[2];
1910 void *samplers
[2] = {sampler_state
, sampler_state
};
1913 templ
.format
= util_format_stencil_only(templ
.format
);
1914 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1917 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1919 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, views
);
1920 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, samplers
);
1922 pipe_sampler_view_reference(&views
[1], NULL
);
1923 } else if (blit_stencil
) {
1924 /* Set a stencil-only sampler view for it not to sample depth instead. */
1925 struct pipe_sampler_view templ
;
1926 struct pipe_sampler_view
*view
;
1929 templ
.format
= util_format_stencil_only(templ
.format
);
1930 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1932 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1934 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &view
);
1935 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1936 0, 1, &sampler_state
);
1938 pipe_sampler_view_reference(&view
, NULL
);
1940 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src
);
1941 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1942 0, 1, &sampler_state
);
1946 pipe
->set_scissor_states(pipe
, 0, 1, scissor
);
1949 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
, false, true);
1951 do_blits(ctx
, dst
, dstbox
, src
, src_width0
, src_height0
,
1952 srcbox
, blit_depth
|| blit_stencil
, use_txf
);
1954 util_blitter_restore_vertex_states(blitter
);
1955 util_blitter_restore_fragment_states(blitter
);
1956 util_blitter_restore_textures(blitter
);
1957 util_blitter_restore_fb_state(blitter
);
1959 pipe
->set_scissor_states(pipe
, 0, 1, &ctx
->base
.saved_scissor
);
1961 util_blitter_restore_render_cond(blitter
);
1962 util_blitter_unset_running_flag(blitter
);
1966 util_blitter_blit(struct blitter_context
*blitter
,
1967 const struct pipe_blit_info
*info
)
1969 struct pipe_resource
*dst
= info
->dst
.resource
;
1970 struct pipe_resource
*src
= info
->src
.resource
;
1971 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1972 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1973 struct pipe_surface
*dst_view
, dst_templ
;
1974 struct pipe_sampler_view src_templ
, *src_view
;
1976 /* Initialize the surface. */
1977 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
1979 dst_templ
.format
= info
->dst
.format
;
1980 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1982 /* Initialize the sampler view. */
1983 util_blitter_default_src_texture(blitter
, &src_templ
, src
, info
->src
.level
);
1984 src_templ
.format
= info
->src
.format
;
1985 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1988 util_blitter_blit_generic(blitter
, dst_view
, &info
->dst
.box
,
1989 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
1990 info
->mask
, info
->filter
,
1991 info
->scissor_enable
? &info
->scissor
: NULL
,
1994 pipe_surface_reference(&dst_view
, NULL
);
1995 pipe_sampler_view_reference(&src_view
, NULL
);
1998 void util_blitter_generate_mipmap(struct blitter_context
*blitter
,
1999 struct pipe_resource
*tex
,
2000 enum pipe_format format
,
2001 unsigned base_level
, unsigned last_level
,
2002 unsigned first_layer
, unsigned last_layer
)
2004 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2005 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2006 struct pipe_surface dst_templ
, *dst_view
;
2007 struct pipe_sampler_view src_templ
, *src_view
;
2009 void *sampler_state
;
2010 const struct util_format_description
*desc
=
2011 util_format_description(format
);
2013 unsigned target
= tex
->target
;
2015 if (ctx
->cube_as_2darray
&&
2016 (target
== PIPE_TEXTURE_CUBE
|| target
== PIPE_TEXTURE_CUBE_ARRAY
))
2017 target
= PIPE_TEXTURE_2D_ARRAY
;
2019 assert(tex
->nr_samples
<= 1);
2020 assert(!util_format_has_stencil(desc
));
2022 is_depth
= desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
;
2024 /* Check whether the states are properly saved. */
2025 util_blitter_set_running_flag(blitter
);
2026 blitter_check_saved_vertex_states(ctx
);
2027 blitter_check_saved_fragment_states(ctx
);
2028 blitter_check_saved_textures(ctx
);
2029 blitter_check_saved_fb_state(ctx
);
2030 blitter_disable_render_cond(ctx
);
2034 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
2035 pipe
->bind_depth_stencil_alpha_state(pipe
,
2036 ctx
->dsa_write_depth_keep_stencil
);
2037 ctx
->bind_fs_state(pipe
,
2038 blitter_get_fs_texfetch_depth(ctx
, target
, 1, false));
2040 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2041 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2042 ctx
->bind_fs_state(pipe
,
2043 blitter_get_fs_texfetch_col(ctx
, tex
->format
, tex
->format
, target
,
2044 1, 1, PIPE_TEX_FILTER_LINEAR
, false));
2047 if (target
== PIPE_TEXTURE_RECT
) {
2048 sampler_state
= ctx
->sampler_state_rect_linear
;
2050 sampler_state
= ctx
->sampler_state_linear
;
2052 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
2053 0, 1, &sampler_state
);
2055 blitter_set_common_draw_rect_state(ctx
, false, false, true);
2057 for (src_level
= base_level
; src_level
< last_level
; src_level
++) {
2058 struct pipe_box dstbox
= {0}, srcbox
= {0};
2059 unsigned dst_level
= src_level
+ 1;
2061 dstbox
.width
= u_minify(tex
->width0
, dst_level
);
2062 dstbox
.height
= u_minify(tex
->height0
, dst_level
);
2064 srcbox
.width
= u_minify(tex
->width0
, src_level
);
2065 srcbox
.height
= u_minify(tex
->height0
, src_level
);
2067 if (target
== PIPE_TEXTURE_3D
) {
2068 dstbox
.depth
= util_max_layer(tex
, dst_level
) + 1;
2069 srcbox
.depth
= util_max_layer(tex
, src_level
) + 1;
2071 dstbox
.z
= srcbox
.z
= first_layer
;
2072 dstbox
.depth
= srcbox
.depth
= last_layer
- first_layer
+ 1;
2075 /* Initialize the surface. */
2076 util_blitter_default_dst_texture(&dst_templ
, tex
, dst_level
,
2078 dst_templ
.format
= format
;
2079 dst_view
= pipe
->create_surface(pipe
, tex
, &dst_templ
);
2081 /* Initialize the sampler view. */
2082 util_blitter_default_src_texture(blitter
, &src_templ
, tex
, src_level
);
2083 src_templ
.format
= format
;
2084 src_view
= pipe
->create_sampler_view(pipe
, tex
, &src_templ
);
2086 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src_view
);
2088 do_blits(ctx
, dst_view
, &dstbox
, src_view
, tex
->width0
, tex
->height0
,
2089 &srcbox
, is_depth
, false);
2091 pipe_surface_reference(&dst_view
, NULL
);
2092 pipe_sampler_view_reference(&src_view
, NULL
);
2095 util_blitter_restore_vertex_states(blitter
);
2096 util_blitter_restore_fragment_states(blitter
);
2097 util_blitter_restore_textures(blitter
);
2098 util_blitter_restore_fb_state(blitter
);
2099 util_blitter_restore_render_cond(blitter
);
2100 util_blitter_unset_running_flag(blitter
);
2103 /* Clear a region of a color surface to a constant value. */
2104 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
2105 struct pipe_surface
*dstsurf
,
2106 const union pipe_color_union
*color
,
2107 unsigned dstx
, unsigned dsty
,
2108 unsigned width
, unsigned height
)
2110 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2111 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2112 struct pipe_framebuffer_state fb_state
;
2113 unsigned num_layers
;
2115 assert(dstsurf
->texture
);
2116 if (!dstsurf
->texture
)
2119 /* check the saved state */
2120 util_blitter_set_running_flag(blitter
);
2121 blitter_check_saved_vertex_states(ctx
);
2122 blitter_check_saved_fragment_states(ctx
);
2123 blitter_check_saved_fb_state(ctx
);
2124 blitter_disable_render_cond(ctx
);
2127 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2128 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2129 bind_fs_write_one_cbuf(ctx
);
2131 /* set a framebuffer state */
2132 fb_state
.width
= dstsurf
->width
;
2133 fb_state
.height
= dstsurf
->height
;
2134 fb_state
.nr_cbufs
= 1;
2135 fb_state
.cbufs
[0] = dstsurf
;
2137 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2138 pipe
->set_sample_mask(pipe
, ~0);
2140 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2142 union blitter_attrib attrib
;
2143 memcpy(attrib
.color
, color
->ui
, sizeof(color
->ui
));
2145 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
2146 if (num_layers
> 1 && ctx
->has_layered
) {
2147 blitter_set_common_draw_rect_state(ctx
, false, true, true);
2148 blitter
->draw_rectangle(blitter
, ctx
->velem_state
,
2149 dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
2150 num_layers
, UTIL_BLITTER_ATTRIB_COLOR
, &attrib
);
2152 blitter_set_common_draw_rect_state(ctx
, false, false, true);
2153 blitter
->draw_rectangle(blitter
, ctx
->velem_state
,
2154 dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
2155 1, UTIL_BLITTER_ATTRIB_COLOR
, &attrib
);
2158 util_blitter_restore_vertex_states(blitter
);
2159 util_blitter_restore_fragment_states(blitter
);
2160 util_blitter_restore_fb_state(blitter
);
2161 util_blitter_restore_render_cond(blitter
);
2162 util_blitter_unset_running_flag(blitter
);
2165 /* Clear a region of a depth stencil surface. */
2166 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
2167 struct pipe_surface
*dstsurf
,
2168 unsigned clear_flags
,
2171 unsigned dstx
, unsigned dsty
,
2172 unsigned width
, unsigned height
)
2174 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2175 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2176 struct pipe_framebuffer_state fb_state
;
2177 struct pipe_stencil_ref sr
= { { 0 } };
2178 unsigned num_layers
;
2180 assert(dstsurf
->texture
);
2181 if (!dstsurf
->texture
)
2184 /* check the saved state */
2185 util_blitter_set_running_flag(blitter
);
2186 blitter_check_saved_vertex_states(ctx
);
2187 blitter_check_saved_fragment_states(ctx
);
2188 blitter_check_saved_fb_state(ctx
);
2189 blitter_disable_render_cond(ctx
);
2192 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
2193 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
2194 sr
.ref_value
[0] = stencil
& 0xff;
2195 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
2196 pipe
->set_stencil_ref(pipe
, &sr
);
2198 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
2199 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
2201 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
2202 sr
.ref_value
[0] = stencil
& 0xff;
2203 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
2204 pipe
->set_stencil_ref(pipe
, &sr
);
2207 /* hmm that should be illegal probably, or make it a no-op somewhere */
2208 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2212 /* set a framebuffer state */
2213 fb_state
.width
= dstsurf
->width
;
2214 fb_state
.height
= dstsurf
->height
;
2215 fb_state
.nr_cbufs
= 0;
2216 fb_state
.cbufs
[0] = 0;
2217 fb_state
.zsbuf
= dstsurf
;
2218 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2219 pipe
->set_sample_mask(pipe
, ~0);
2221 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2223 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
2224 if (num_layers
> 1 && ctx
->has_layered
) {
2225 blitter_set_common_draw_rect_state(ctx
, false, true, false);
2226 blitter
->draw_rectangle(blitter
, ctx
->velem_state
,
2227 dstx
, dsty
, dstx
+width
, dsty
+height
, depth
,
2228 num_layers
, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2230 blitter_set_common_draw_rect_state(ctx
, false, false, false);
2231 blitter
->draw_rectangle(blitter
, ctx
->velem_state
,
2232 dstx
, dsty
, dstx
+width
, dsty
+height
, depth
, 1,
2233 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2236 util_blitter_restore_vertex_states(blitter
);
2237 util_blitter_restore_fragment_states(blitter
);
2238 util_blitter_restore_fb_state(blitter
);
2239 util_blitter_restore_render_cond(blitter
);
2240 util_blitter_unset_running_flag(blitter
);
2243 /* draw a rectangle across a region using a custom dsa stage - for r600g */
2244 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
2245 struct pipe_surface
*zsurf
,
2246 struct pipe_surface
*cbsurf
,
2247 unsigned sample_mask
,
2248 void *dsa_stage
, float depth
)
2250 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2251 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2252 struct pipe_framebuffer_state fb_state
;
2254 assert(zsurf
->texture
);
2255 if (!zsurf
->texture
)
2258 /* check the saved state */
2259 util_blitter_set_running_flag(blitter
);
2260 blitter_check_saved_vertex_states(ctx
);
2261 blitter_check_saved_fragment_states(ctx
);
2262 blitter_check_saved_fb_state(ctx
);
2263 blitter_disable_render_cond(ctx
);
2266 pipe
->bind_blend_state(pipe
, cbsurf
? ctx
->blend
[PIPE_MASK_RGBA
][0] :
2268 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
2270 bind_fs_write_one_cbuf(ctx
);
2274 /* set a framebuffer state */
2275 fb_state
.width
= zsurf
->width
;
2276 fb_state
.height
= zsurf
->height
;
2277 fb_state
.nr_cbufs
= 1;
2279 fb_state
.cbufs
[0] = cbsurf
;
2280 fb_state
.nr_cbufs
= 1;
2282 fb_state
.cbufs
[0] = NULL
;
2283 fb_state
.nr_cbufs
= 0;
2285 fb_state
.zsbuf
= zsurf
;
2286 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2287 pipe
->set_sample_mask(pipe
, sample_mask
);
2289 blitter_set_common_draw_rect_state(ctx
, false, false, false);
2290 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
2291 blitter
->draw_rectangle(blitter
, ctx
->velem_state
, 0, 0,
2292 zsurf
->width
, zsurf
->height
, depth
,
2293 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2295 util_blitter_restore_vertex_states(blitter
);
2296 util_blitter_restore_fragment_states(blitter
);
2297 util_blitter_restore_fb_state(blitter
);
2298 util_blitter_restore_render_cond(blitter
);
2299 util_blitter_unset_running_flag(blitter
);
2302 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
2303 struct pipe_resource
*dst
,
2305 struct pipe_resource
*src
,
2309 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2310 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2311 struct pipe_vertex_buffer vb
;
2312 struct pipe_stream_output_target
*so_target
;
2313 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2315 if (srcx
>= src
->width0
||
2316 dstx
>= dst
->width0
) {
2319 if (srcx
+ size
> src
->width0
) {
2320 size
= src
->width0
- srcx
;
2322 if (dstx
+ size
> dst
->width0
) {
2323 size
= dst
->width0
- dstx
;
2326 /* Drivers not capable of Stream Out should not call this function
2327 * in the first place. */
2328 assert(ctx
->has_stream_out
);
2330 /* Some alignment is required. */
2331 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
2332 !ctx
->has_stream_out
) {
2333 struct pipe_box box
;
2334 u_box_1d(srcx
, size
, &box
);
2335 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
2339 util_blitter_set_running_flag(blitter
);
2340 blitter_check_saved_vertex_states(ctx
);
2341 blitter_disable_render_cond(ctx
);
2343 vb
.is_user_buffer
= false;
2344 vb
.buffer
.resource
= src
;
2345 vb
.buffer_offset
= srcx
;
2348 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2349 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[0]);
2350 bind_vs_pos_only(ctx
, 1);
2351 if (ctx
->has_geometry_shader
)
2352 pipe
->bind_gs_state(pipe
, NULL
);
2353 if (ctx
->has_tessellation
) {
2354 pipe
->bind_tcs_state(pipe
, NULL
);
2355 pipe
->bind_tes_state(pipe
, NULL
);
2357 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2359 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
2360 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2362 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2364 util_blitter_restore_vertex_states(blitter
);
2365 util_blitter_restore_render_cond(blitter
);
2366 util_blitter_unset_running_flag(blitter
);
2367 pipe_so_target_reference(&so_target
, NULL
);
2370 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
2371 struct pipe_resource
*dst
,
2372 unsigned offset
, unsigned size
,
2373 unsigned num_channels
,
2374 const union pipe_color_union
*clear_value
)
2376 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2377 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2378 struct pipe_vertex_buffer vb
= {0};
2379 struct pipe_stream_output_target
*so_target
= NULL
;
2380 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2382 assert(num_channels
>= 1);
2383 assert(num_channels
<= 4);
2385 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
2387 * R600 uses this to initialize texture resources, so width0 might not be
2388 * what you think it is.
2391 /* Streamout is required. */
2392 if (!ctx
->has_stream_out
) {
2393 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2397 /* Some alignment is required. */
2398 if (offset
% 4 != 0 || size
% 4 != 0) {
2399 assert(!"Bad alignment in util_blitter_clear_buffer()");
2403 u_upload_data(pipe
->stream_uploader
, 0, num_channels
*4, 4, clear_value
,
2404 &vb
.buffer_offset
, &vb
.buffer
.resource
);
2405 if (!vb
.buffer
.resource
)
2410 util_blitter_set_running_flag(blitter
);
2411 blitter_check_saved_vertex_states(ctx
);
2412 blitter_disable_render_cond(ctx
);
2414 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2415 pipe
->bind_vertex_elements_state(pipe
,
2416 ctx
->velem_state_readbuf
[num_channels
-1]);
2417 bind_vs_pos_only(ctx
, num_channels
);
2418 if (ctx
->has_geometry_shader
)
2419 pipe
->bind_gs_state(pipe
, NULL
);
2420 if (ctx
->has_tessellation
) {
2421 pipe
->bind_tcs_state(pipe
, NULL
);
2422 pipe
->bind_tes_state(pipe
, NULL
);
2424 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2426 so_target
= pipe
->create_stream_output_target(pipe
, dst
, offset
, size
);
2427 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2429 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2432 util_blitter_restore_vertex_states(blitter
);
2433 util_blitter_restore_render_cond(blitter
);
2434 util_blitter_unset_running_flag(blitter
);
2435 pipe_so_target_reference(&so_target
, NULL
);
2436 pipe_resource_reference(&vb
.buffer
.resource
, NULL
);
2439 /* probably radeon specific */
2440 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
2441 struct pipe_resource
*dst
,
2444 struct pipe_resource
*src
,
2446 unsigned sample_mask
,
2448 enum pipe_format format
)
2450 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2451 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2452 struct pipe_framebuffer_state fb_state
;
2453 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
2455 util_blitter_set_running_flag(blitter
);
2456 blitter_check_saved_vertex_states(ctx
);
2457 blitter_check_saved_fragment_states(ctx
);
2458 blitter_disable_render_cond(ctx
);
2461 pipe
->bind_blend_state(pipe
, custom_blend
);
2462 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2463 bind_fs_write_one_cbuf(ctx
);
2464 pipe
->set_sample_mask(pipe
, sample_mask
);
2466 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
2467 surf_tmpl
.format
= format
;
2468 surf_tmpl
.u
.tex
.level
= dst_level
;
2469 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
2470 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
2472 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
2474 surf_tmpl
.u
.tex
.level
= 0;
2475 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
2476 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
2478 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
2480 /* set a framebuffer state */
2481 fb_state
.width
= src
->width0
;
2482 fb_state
.height
= src
->height0
;
2483 fb_state
.nr_cbufs
= 2;
2484 fb_state
.cbufs
[0] = srcsurf
;
2485 fb_state
.cbufs
[1] = dstsurf
;
2486 fb_state
.zsbuf
= NULL
;
2487 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2489 blitter_set_common_draw_rect_state(ctx
, false, false, false);
2490 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
2491 blitter
->draw_rectangle(blitter
, ctx
->velem_state
,
2492 0, 0, src
->width0
, src
->height0
,
2493 0, 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2494 util_blitter_restore_fb_state(blitter
);
2495 util_blitter_restore_vertex_states(blitter
);
2496 util_blitter_restore_fragment_states(blitter
);
2497 util_blitter_restore_render_cond(blitter
);
2498 util_blitter_unset_running_flag(blitter
);
2500 pipe_surface_reference(&srcsurf
, NULL
);
2501 pipe_surface_reference(&dstsurf
, NULL
);
2504 void util_blitter_custom_color(struct blitter_context
*blitter
,
2505 struct pipe_surface
*dstsurf
,
2508 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2509 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2510 struct pipe_framebuffer_state fb_state
;
2512 assert(dstsurf
->texture
);
2513 if (!dstsurf
->texture
)
2516 /* check the saved state */
2517 util_blitter_set_running_flag(blitter
);
2518 blitter_check_saved_vertex_states(ctx
);
2519 blitter_check_saved_fragment_states(ctx
);
2520 blitter_check_saved_fb_state(ctx
);
2521 blitter_disable_render_cond(ctx
);
2524 pipe
->bind_blend_state(pipe
, custom_blend
? custom_blend
2525 : ctx
->blend
[PIPE_MASK_RGBA
][0]);
2526 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2527 bind_fs_write_one_cbuf(ctx
);
2528 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2530 /* set a framebuffer state */
2531 fb_state
.width
= dstsurf
->width
;
2532 fb_state
.height
= dstsurf
->height
;
2533 fb_state
.nr_cbufs
= 1;
2534 fb_state
.cbufs
[0] = dstsurf
;
2536 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2537 pipe
->set_sample_mask(pipe
, ~0);
2539 blitter_set_common_draw_rect_state(ctx
, false, false, false);
2540 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2541 blitter
->draw_rectangle(blitter
, ctx
->velem_state
,
2542 0, 0, dstsurf
->width
, dstsurf
->height
,
2543 0, 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2545 util_blitter_restore_vertex_states(blitter
);
2546 util_blitter_restore_fragment_states(blitter
);
2547 util_blitter_restore_fb_state(blitter
);
2548 util_blitter_restore_render_cond(blitter
);
2549 util_blitter_unset_running_flag(blitter
);