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.*/
71 void *vs_pos_only
[4]; /**< Vertex shader which passes pos to the output.*/
72 void *vs_layered
; /**< Vertex shader which sets LAYER = INSTANCEID. */
74 /* Fragment shaders. */
76 void *fs_write_one_cbuf
;
77 void *fs_write_all_cbufs
;
79 /* FS which outputs a color from a texture where
80 * the 1st index indicates the texture type / destination type,
81 * the 2nd index is the PIPE_TEXTURE_* to be sampled,
82 * the 3rd index is 0 = use TEX, 1 = use TXF.
84 void *fs_texfetch_col
[5][PIPE_MAX_TEXTURE_TYPES
][2];
86 /* FS which outputs a depth from a texture, where
87 * the 1st index is the PIPE_TEXTURE_* to be sampled,
88 * the 2nd index is 0 = use TEX, 1 = use TXF.
90 void *fs_texfetch_depth
[PIPE_MAX_TEXTURE_TYPES
][2];
91 void *fs_texfetch_depthstencil
[PIPE_MAX_TEXTURE_TYPES
][2];
92 void *fs_texfetch_stencil
[PIPE_MAX_TEXTURE_TYPES
][2];
94 /* FS which outputs one sample from a multisample texture. */
95 void *fs_texfetch_col_msaa
[5][PIPE_MAX_TEXTURE_TYPES
];
96 void *fs_texfetch_depth_msaa
[PIPE_MAX_TEXTURE_TYPES
];
97 void *fs_texfetch_depthstencil_msaa
[PIPE_MAX_TEXTURE_TYPES
];
98 void *fs_texfetch_stencil_msaa
[PIPE_MAX_TEXTURE_TYPES
];
100 /* FS which outputs an average of all samples. */
101 void *fs_resolve
[PIPE_MAX_TEXTURE_TYPES
][NUM_RESOLVE_FRAG_SHADERS
][2];
104 void *blend
[PIPE_MASK_RGBA
+1][2]; /**< blend state with writemask */
105 void *blend_clear
[GET_CLEAR_BLEND_STATE_IDX(PIPE_CLEAR_COLOR
)+1];
107 /* Depth stencil alpha state. */
108 void *dsa_write_depth_stencil
;
109 void *dsa_write_depth_keep_stencil
;
110 void *dsa_keep_depth_stencil
;
111 void *dsa_keep_depth_write_stencil
;
113 /* Vertex elements states. */
115 void *velem_state_readbuf
[4]; /**< X, XY, XYZ, XYZW */
119 void *sampler_state_linear
;
120 void *sampler_state_rect
;
121 void *sampler_state_rect_linear
;
123 /* Rasterizer state. */
124 void *rs_state
, *rs_state_scissor
, *rs_discard_state
;
126 /* Viewport state. */
127 struct pipe_viewport_state viewport
;
129 /* Destination surface dimensions. */
133 bool has_geometry_shader
;
134 bool has_tessellation
;
137 bool has_stencil_export
;
138 bool has_texture_multisample
;
141 bool cube_as_2darray
;
142 bool cached_all_shaders
;
144 /* The Draw module overrides these functions.
145 * Always create the blitter before Draw. */
146 void (*bind_fs_state
)(struct pipe_context
*, void *);
147 void (*delete_fs_state
)(struct pipe_context
*, void *);
150 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
)
152 struct blitter_context_priv
*ctx
;
153 struct pipe_blend_state blend
;
154 struct pipe_depth_stencil_alpha_state dsa
;
155 struct pipe_rasterizer_state rs_state
;
156 struct pipe_sampler_state sampler_state
;
157 struct pipe_vertex_element velem
[2];
160 ctx
= CALLOC_STRUCT(blitter_context_priv
);
164 ctx
->base
.pipe
= pipe
;
165 ctx
->base
.draw_rectangle
= util_blitter_draw_rectangle
;
167 ctx
->bind_fs_state
= pipe
->bind_fs_state
;
168 ctx
->delete_fs_state
= pipe
->delete_fs_state
;
170 /* init state objects for them to be considered invalid */
171 ctx
->base
.saved_blend_state
= INVALID_PTR
;
172 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
173 ctx
->base
.saved_rs_state
= INVALID_PTR
;
174 ctx
->base
.saved_fs
= INVALID_PTR
;
175 ctx
->base
.saved_vs
= INVALID_PTR
;
176 ctx
->base
.saved_gs
= INVALID_PTR
;
177 ctx
->base
.saved_velem_state
= INVALID_PTR
;
178 ctx
->base
.saved_fb_state
.nr_cbufs
= ~0;
179 ctx
->base
.saved_num_sampler_views
= ~0;
180 ctx
->base
.saved_num_sampler_states
= ~0;
181 ctx
->base
.saved_num_so_targets
= ~0;
183 ctx
->has_geometry_shader
=
184 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
185 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
187 ctx
->has_tessellation
=
188 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_TESS_CTRL
,
189 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
191 ctx
->has_stream_out
=
192 pipe
->screen
->get_param(pipe
->screen
,
193 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0;
195 ctx
->has_stencil_export
=
196 pipe
->screen
->get_param(pipe
->screen
,
197 PIPE_CAP_SHADER_STENCIL_EXPORT
);
199 ctx
->has_texture_multisample
=
200 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TEXTURE_MULTISAMPLE
);
202 ctx
->has_tex_lz
= pipe
->screen
->get_param(pipe
->screen
,
203 PIPE_CAP_TGSI_TEX_TXF_LZ
);
204 ctx
->has_txf
= pipe
->screen
->get_param(pipe
->screen
,
205 PIPE_CAP_GLSL_FEATURE_LEVEL
) > 130;
206 ctx
->cube_as_2darray
= pipe
->screen
->get_param(pipe
->screen
,
207 PIPE_CAP_SAMPLER_VIEW_TARGET
);
209 /* blend state objects */
210 memset(&blend
, 0, sizeof(blend
));
212 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
213 for (j
= 0; j
< 2; j
++) {
214 memset(&blend
.rt
[0], 0, sizeof(blend
.rt
[0]));
215 blend
.rt
[0].colormask
= i
;
217 blend
.rt
[0].blend_enable
= 1;
218 blend
.rt
[0].rgb_func
= PIPE_BLEND_ADD
;
219 blend
.rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_SRC_ALPHA
;
220 blend
.rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
221 blend
.rt
[0].alpha_func
= PIPE_BLEND_ADD
;
222 blend
.rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_SRC_ALPHA
;
223 blend
.rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
225 ctx
->blend
[i
][j
] = pipe
->create_blend_state(pipe
, &blend
);
229 /* depth stencil alpha state objects */
230 memset(&dsa
, 0, sizeof(dsa
));
231 ctx
->dsa_keep_depth_stencil
=
232 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
234 dsa
.depth
.enabled
= 1;
235 dsa
.depth
.writemask
= 1;
236 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
237 ctx
->dsa_write_depth_keep_stencil
=
238 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
240 dsa
.stencil
[0].enabled
= 1;
241 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
242 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
243 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
244 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
245 dsa
.stencil
[0].valuemask
= 0xff;
246 dsa
.stencil
[0].writemask
= 0xff;
247 ctx
->dsa_write_depth_stencil
=
248 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
250 dsa
.depth
.enabled
= 0;
251 dsa
.depth
.writemask
= 0;
252 ctx
->dsa_keep_depth_write_stencil
=
253 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
256 memset(&sampler_state
, 0, sizeof(sampler_state
));
257 sampler_state
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
258 sampler_state
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
259 sampler_state
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
260 sampler_state
.normalized_coords
= 1;
261 ctx
->sampler_state
= pipe
->create_sampler_state(pipe
, &sampler_state
);
262 sampler_state
.normalized_coords
= 0;
263 ctx
->sampler_state_rect
= pipe
->create_sampler_state(pipe
, &sampler_state
);
265 sampler_state
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
266 sampler_state
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
267 sampler_state
.normalized_coords
= 1;
268 ctx
->sampler_state_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
269 sampler_state
.normalized_coords
= 0;
270 ctx
->sampler_state_rect_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
272 /* rasterizer state */
273 memset(&rs_state
, 0, sizeof(rs_state
));
274 rs_state
.cull_face
= PIPE_FACE_NONE
;
275 rs_state
.half_pixel_center
= 1;
276 rs_state
.bottom_edge_rule
= 1;
277 rs_state
.flatshade
= 1;
278 rs_state
.depth_clip
= 1;
279 ctx
->rs_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
281 rs_state
.scissor
= 1;
282 ctx
->rs_state_scissor
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
284 if (ctx
->has_stream_out
) {
285 rs_state
.scissor
= 0;
286 rs_state
.rasterizer_discard
= 1;
287 ctx
->rs_discard_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
290 ctx
->base
.cb_slot
= 0; /* 0 for now */
291 ctx
->base
.vb_slot
= 0; /* 0 for now */
293 /* vertex elements states */
294 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
295 for (i
= 0; i
< 2; i
++) {
296 velem
[i
].src_offset
= i
* 4 * sizeof(float);
297 velem
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
298 velem
[i
].vertex_buffer_index
= ctx
->base
.vb_slot
;
300 ctx
->velem_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
302 if (ctx
->has_stream_out
) {
303 static enum pipe_format formats
[4] = {
304 PIPE_FORMAT_R32_UINT
,
305 PIPE_FORMAT_R32G32_UINT
,
306 PIPE_FORMAT_R32G32B32_UINT
,
307 PIPE_FORMAT_R32G32B32A32_UINT
310 for (i
= 0; i
< 4; i
++) {
311 velem
[0].src_format
= formats
[i
];
312 velem
[0].vertex_buffer_index
= ctx
->base
.vb_slot
;
313 ctx
->velem_state_readbuf
[i
] =
314 pipe
->create_vertex_elements_state(pipe
, 1, &velem
[0]);
319 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_INSTANCEID
) &&
320 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT
);
322 /* set invariant vertex coordinates */
323 for (i
= 0; i
< 4; i
++)
324 ctx
->vertices
[i
][0][3] = 1; /*v.w*/
329 static void bind_vs_pos_only(struct blitter_context_priv
*ctx
,
330 unsigned num_so_channels
)
332 struct pipe_context
*pipe
= ctx
->base
.pipe
;
333 int index
= num_so_channels
? num_so_channels
- 1 : 0;
335 if (!ctx
->vs_pos_only
[index
]) {
336 struct pipe_stream_output_info so
;
337 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
};
338 const uint semantic_indices
[] = { 0 };
340 memset(&so
, 0, sizeof(so
));
342 so
.output
[0].num_components
= num_so_channels
;
343 so
.stride
[0] = num_so_channels
;
345 ctx
->vs_pos_only
[index
] =
346 util_make_vertex_passthrough_shader_with_so(pipe
, 1, semantic_names
,
347 semantic_indices
, false,
351 pipe
->bind_vs_state(pipe
, ctx
->vs_pos_only
[index
]);
354 static void bind_vs_passthrough(struct blitter_context_priv
*ctx
)
356 struct pipe_context
*pipe
= ctx
->base
.pipe
;
359 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
360 TGSI_SEMANTIC_GENERIC
};
361 const uint semantic_indices
[] = { 0, 0 };
363 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
364 semantic_indices
, false);
367 pipe
->bind_vs_state(pipe
, ctx
->vs
);
370 static void bind_vs_layered(struct blitter_context_priv
*ctx
)
372 struct pipe_context
*pipe
= ctx
->base
.pipe
;
374 if (!ctx
->vs_layered
) {
375 ctx
->vs_layered
= util_make_layered_clear_vertex_shader(pipe
);
378 pipe
->bind_vs_state(pipe
, ctx
->vs_layered
);
381 static void bind_fs_empty(struct blitter_context_priv
*ctx
)
383 struct pipe_context
*pipe
= ctx
->base
.pipe
;
385 if (!ctx
->fs_empty
) {
386 assert(!ctx
->cached_all_shaders
);
387 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
390 ctx
->bind_fs_state(pipe
, ctx
->fs_empty
);
393 static void bind_fs_write_one_cbuf(struct blitter_context_priv
*ctx
)
395 struct pipe_context
*pipe
= ctx
->base
.pipe
;
397 if (!ctx
->fs_write_one_cbuf
) {
398 assert(!ctx
->cached_all_shaders
);
399 ctx
->fs_write_one_cbuf
=
400 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
401 TGSI_INTERPOLATE_CONSTANT
, false);
404 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
407 static void bind_fs_write_all_cbufs(struct blitter_context_priv
*ctx
)
409 struct pipe_context
*pipe
= ctx
->base
.pipe
;
411 if (!ctx
->fs_write_all_cbufs
) {
412 assert(!ctx
->cached_all_shaders
);
413 ctx
->fs_write_all_cbufs
=
414 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
415 TGSI_INTERPOLATE_CONSTANT
, true);
418 ctx
->bind_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
421 void util_blitter_destroy(struct blitter_context
*blitter
)
423 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
424 struct pipe_context
*pipe
= blitter
->pipe
;
427 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++)
428 for (j
= 0; j
< 2; j
++)
429 pipe
->delete_blend_state(pipe
, ctx
->blend
[i
][j
]);
431 for (i
= 0; i
< ARRAY_SIZE(ctx
->blend_clear
); i
++) {
432 if (ctx
->blend_clear
[i
])
433 pipe
->delete_blend_state(pipe
, ctx
->blend_clear
[i
]);
435 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
436 pipe
->delete_depth_stencil_alpha_state(pipe
,
437 ctx
->dsa_write_depth_keep_stencil
);
438 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
439 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
441 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
);
442 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state_scissor
);
443 if (ctx
->rs_discard_state
)
444 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_discard_state
);
446 pipe
->delete_vs_state(pipe
, ctx
->vs
);
447 for (i
= 0; i
< 4; i
++)
448 if (ctx
->vs_pos_only
[i
])
449 pipe
->delete_vs_state(pipe
, ctx
->vs_pos_only
[i
]);
451 pipe
->delete_vs_state(pipe
, ctx
->vs_layered
);
452 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state
);
453 for (i
= 0; i
< 4; i
++) {
454 if (ctx
->velem_state_readbuf
[i
]) {
455 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[i
]);
459 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
460 for (unsigned type
= 0; type
< ARRAY_SIZE(ctx
->fs_texfetch_col
); ++type
) {
461 for (unsigned inst
= 0; inst
< 2; inst
++) {
462 if (ctx
->fs_texfetch_col
[type
][i
][inst
])
463 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[type
][i
][inst
]);
465 if (ctx
->fs_texfetch_col_msaa
[type
][i
])
466 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col_msaa
[type
][i
]);
469 for (unsigned inst
= 0; inst
< 2; inst
++) {
470 if (ctx
->fs_texfetch_depth
[i
][inst
])
471 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
][inst
]);
472 if (ctx
->fs_texfetch_depthstencil
[i
][inst
])
473 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil
[i
][inst
]);
474 if (ctx
->fs_texfetch_stencil
[i
][inst
])
475 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil
[i
][inst
]);
478 if (ctx
->fs_texfetch_depth_msaa
[i
])
479 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth_msaa
[i
]);
480 if (ctx
->fs_texfetch_depthstencil_msaa
[i
])
481 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil_msaa
[i
]);
482 if (ctx
->fs_texfetch_stencil_msaa
[i
])
483 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil_msaa
[i
]);
485 for (j
= 0; j
< ARRAY_SIZE(ctx
->fs_resolve
[i
]); j
++)
486 for (f
= 0; f
< 2; f
++)
487 if (ctx
->fs_resolve
[i
][j
][f
])
488 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve
[i
][j
][f
]);
492 ctx
->delete_fs_state(pipe
, ctx
->fs_empty
);
493 if (ctx
->fs_write_one_cbuf
)
494 ctx
->delete_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
495 if (ctx
->fs_write_all_cbufs
)
496 ctx
->delete_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
498 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect_linear
);
499 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect
);
500 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_linear
);
501 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
);
505 void util_blitter_set_texture_multisample(struct blitter_context
*blitter
,
508 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
510 ctx
->has_texture_multisample
= supported
;
513 void util_blitter_set_running_flag(struct blitter_context
*blitter
)
515 if (blitter
->running
) {
516 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
519 blitter
->running
= true;
521 blitter
->pipe
->set_active_query_state(blitter
->pipe
, false);
524 void util_blitter_unset_running_flag(struct blitter_context
*blitter
)
526 if (!blitter
->running
) {
527 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
530 blitter
->running
= false;
532 blitter
->pipe
->set_active_query_state(blitter
->pipe
, true);
535 static void blitter_check_saved_vertex_states(struct blitter_context_priv
*ctx
)
537 assert(ctx
->base
.saved_velem_state
!= INVALID_PTR
);
538 assert(ctx
->base
.saved_vs
!= INVALID_PTR
);
539 assert(!ctx
->has_geometry_shader
|| ctx
->base
.saved_gs
!= INVALID_PTR
);
540 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tcs
!= INVALID_PTR
);
541 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tes
!= INVALID_PTR
);
542 assert(!ctx
->has_stream_out
|| ctx
->base
.saved_num_so_targets
!= ~0u);
543 assert(ctx
->base
.saved_rs_state
!= INVALID_PTR
);
546 void util_blitter_restore_vertex_states(struct blitter_context
*blitter
)
548 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
549 struct pipe_context
*pipe
= ctx
->base
.pipe
;
553 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1,
554 &ctx
->base
.saved_vertex_buffer
);
555 pipe_vertex_buffer_unreference(&ctx
->base
.saved_vertex_buffer
);
557 /* Vertex elements. */
558 pipe
->bind_vertex_elements_state(pipe
, ctx
->base
.saved_velem_state
);
559 ctx
->base
.saved_velem_state
= INVALID_PTR
;
562 pipe
->bind_vs_state(pipe
, ctx
->base
.saved_vs
);
563 ctx
->base
.saved_vs
= INVALID_PTR
;
565 /* Geometry shader. */
566 if (ctx
->has_geometry_shader
) {
567 pipe
->bind_gs_state(pipe
, ctx
->base
.saved_gs
);
568 ctx
->base
.saved_gs
= INVALID_PTR
;
571 if (ctx
->has_tessellation
) {
572 pipe
->bind_tcs_state(pipe
, ctx
->base
.saved_tcs
);
573 pipe
->bind_tes_state(pipe
, ctx
->base
.saved_tes
);
574 ctx
->base
.saved_tcs
= INVALID_PTR
;
575 ctx
->base
.saved_tes
= INVALID_PTR
;
578 /* Stream outputs. */
579 if (ctx
->has_stream_out
) {
580 unsigned offsets
[PIPE_MAX_SO_BUFFERS
];
581 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
582 offsets
[i
] = (unsigned)-1;
583 pipe
->set_stream_output_targets(pipe
,
584 ctx
->base
.saved_num_so_targets
,
585 ctx
->base
.saved_so_targets
, offsets
);
587 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
588 pipe_so_target_reference(&ctx
->base
.saved_so_targets
[i
], NULL
);
590 ctx
->base
.saved_num_so_targets
= ~0;
594 pipe
->bind_rasterizer_state(pipe
, ctx
->base
.saved_rs_state
);
595 ctx
->base
.saved_rs_state
= INVALID_PTR
;
598 static void blitter_check_saved_fragment_states(struct blitter_context_priv
*ctx
)
600 assert(ctx
->base
.saved_fs
!= INVALID_PTR
);
601 assert(ctx
->base
.saved_dsa_state
!= INVALID_PTR
);
602 assert(ctx
->base
.saved_blend_state
!= INVALID_PTR
);
605 void util_blitter_restore_fragment_states(struct blitter_context
*blitter
)
607 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
608 struct pipe_context
*pipe
= ctx
->base
.pipe
;
610 /* Fragment shader. */
611 ctx
->bind_fs_state(pipe
, ctx
->base
.saved_fs
);
612 ctx
->base
.saved_fs
= INVALID_PTR
;
614 /* Depth, stencil, alpha. */
615 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->base
.saved_dsa_state
);
616 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
619 pipe
->bind_blend_state(pipe
, ctx
->base
.saved_blend_state
);
620 ctx
->base
.saved_blend_state
= INVALID_PTR
;
623 if (ctx
->base
.is_sample_mask_saved
) {
624 pipe
->set_sample_mask(pipe
, ctx
->base
.saved_sample_mask
);
625 ctx
->base
.is_sample_mask_saved
= false;
628 /* Miscellaneous states. */
629 /* XXX check whether these are saved and whether they need to be restored
630 * (depending on the operation) */
631 pipe
->set_stencil_ref(pipe
, &ctx
->base
.saved_stencil_ref
);
632 pipe
->set_viewport_states(pipe
, 0, 1, &ctx
->base
.saved_viewport
);
635 static void blitter_check_saved_fb_state(struct blitter_context_priv
*ctx
)
637 assert(ctx
->base
.saved_fb_state
.nr_cbufs
!= ~0u);
640 static void blitter_disable_render_cond(struct blitter_context_priv
*ctx
)
642 struct pipe_context
*pipe
= ctx
->base
.pipe
;
644 if (ctx
->base
.saved_render_cond_query
) {
645 pipe
->render_condition(pipe
, NULL
, false, 0);
649 void util_blitter_restore_render_cond(struct blitter_context
*blitter
)
651 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
652 struct pipe_context
*pipe
= ctx
->base
.pipe
;
654 if (ctx
->base
.saved_render_cond_query
) {
655 pipe
->render_condition(pipe
, ctx
->base
.saved_render_cond_query
,
656 ctx
->base
.saved_render_cond_cond
,
657 ctx
->base
.saved_render_cond_mode
);
658 ctx
->base
.saved_render_cond_query
= NULL
;
662 void util_blitter_restore_fb_state(struct blitter_context
*blitter
)
664 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
665 struct pipe_context
*pipe
= ctx
->base
.pipe
;
667 pipe
->set_framebuffer_state(pipe
, &ctx
->base
.saved_fb_state
);
668 util_unreference_framebuffer_state(&ctx
->base
.saved_fb_state
);
671 static void blitter_check_saved_textures(struct blitter_context_priv
*ctx
)
673 assert(ctx
->base
.saved_num_sampler_states
!= ~0u);
674 assert(ctx
->base
.saved_num_sampler_views
!= ~0u);
677 void util_blitter_restore_textures(struct blitter_context
*blitter
)
679 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
680 struct pipe_context
*pipe
= ctx
->base
.pipe
;
683 /* Fragment sampler states. */
684 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0,
685 ctx
->base
.saved_num_sampler_states
,
686 ctx
->base
.saved_sampler_states
);
688 ctx
->base
.saved_num_sampler_states
= ~0;
690 /* Fragment sampler views. */
691 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
692 ctx
->base
.saved_num_sampler_views
,
693 ctx
->base
.saved_sampler_views
);
695 for (i
= 0; i
< ctx
->base
.saved_num_sampler_views
; i
++)
696 pipe_sampler_view_reference(&ctx
->base
.saved_sampler_views
[i
], NULL
);
698 ctx
->base
.saved_num_sampler_views
= ~0;
701 void util_blitter_restore_constant_buffer_state(struct blitter_context
*blitter
)
703 struct pipe_context
*pipe
= blitter
->pipe
;
705 pipe
->set_constant_buffer(pipe
, PIPE_SHADER_FRAGMENT
, blitter
->cb_slot
,
706 &blitter
->saved_fs_constant_buffer
);
707 pipe_resource_reference(&blitter
->saved_fs_constant_buffer
.buffer
, NULL
);
710 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
711 int x1
, int y1
, int x2
, int y2
,
716 /* set vertex positions */
717 ctx
->vertices
[0][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v0.x*/
718 ctx
->vertices
[0][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v0.y*/
720 ctx
->vertices
[1][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v1.x*/
721 ctx
->vertices
[1][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v1.y*/
723 ctx
->vertices
[2][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v2.x*/
724 ctx
->vertices
[2][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v2.y*/
726 ctx
->vertices
[3][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v3.x*/
727 ctx
->vertices
[3][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v3.y*/
729 for (i
= 0; i
< 4; i
++)
730 ctx
->vertices
[i
][0][2] = depth
; /*z*/
733 ctx
->viewport
.scale
[0] = 0.5f
* ctx
->dst_width
;
734 ctx
->viewport
.scale
[1] = 0.5f
* ctx
->dst_height
;
735 ctx
->viewport
.scale
[2] = 1.0f
;
736 ctx
->viewport
.translate
[0] = 0.5f
* ctx
->dst_width
;
737 ctx
->viewport
.translate
[1] = 0.5f
* ctx
->dst_height
;
738 ctx
->viewport
.translate
[2] = 0.0f
;
739 ctx
->base
.pipe
->set_viewport_states(ctx
->base
.pipe
, 0, 1, &ctx
->viewport
);
742 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
743 const float color
[4])
748 for (i
= 0; i
< 4; i
++)
749 memcpy(&ctx
->vertices
[i
][1][0], color
, sizeof(uint32_t) * 4);
751 for (i
= 0; i
< 4; i
++)
752 memset(&ctx
->vertices
[i
][1][0], 0, sizeof(uint32_t) * 4);
756 static void get_texcoords(struct pipe_sampler_view
*src
,
757 unsigned src_width0
, unsigned src_height0
,
758 int x1
, int y1
, int x2
, int y2
,
759 float layer
, unsigned sample
,
760 bool uses_txf
, union blitter_attrib
*out
)
762 unsigned level
= src
->u
.tex
.first_level
;
763 bool normalized
= !uses_txf
&&
764 src
->target
!= PIPE_TEXTURE_RECT
&&
765 src
->texture
->nr_samples
<= 1;
768 out
->texcoord
.x1
= x1
/ (float)u_minify(src_width0
, level
);
769 out
->texcoord
.y1
= y1
/ (float)u_minify(src_height0
, level
);
770 out
->texcoord
.x2
= x2
/ (float)u_minify(src_width0
, level
);
771 out
->texcoord
.y2
= y2
/ (float)u_minify(src_height0
, level
);
773 out
->texcoord
.x1
= x1
;
774 out
->texcoord
.y1
= y1
;
775 out
->texcoord
.x2
= x2
;
776 out
->texcoord
.y2
= y2
;
783 switch (src
->target
) {
784 case PIPE_TEXTURE_3D
:
789 r
/= u_minify(src
->texture
->depth0
, src
->u
.tex
.first_level
);
795 case PIPE_TEXTURE_1D_ARRAY
:
796 out
->texcoord
.y1
= out
->texcoord
.y2
= layer
;
799 case PIPE_TEXTURE_2D_ARRAY
:
800 out
->texcoord
.z
= layer
;
801 out
->texcoord
.w
= sample
;
804 case PIPE_TEXTURE_CUBE_ARRAY
:
805 out
->texcoord
.w
= (unsigned)layer
/ 6;
808 case PIPE_TEXTURE_2D
:
809 out
->texcoord
.w
= sample
;
816 static void blitter_set_dst_dimensions(struct blitter_context_priv
*ctx
,
817 unsigned width
, unsigned height
)
819 ctx
->dst_width
= width
;
820 ctx
->dst_height
= height
;
823 static void set_texcoords_in_vertices(const union blitter_attrib
*attrib
,
824 float *out
, unsigned stride
)
826 out
[0] = attrib
->texcoord
.x1
;
827 out
[1] = attrib
->texcoord
.y1
;
829 out
[0] = attrib
->texcoord
.x2
;
830 out
[1] = attrib
->texcoord
.y1
;
832 out
[0] = attrib
->texcoord
.x2
;
833 out
[1] = attrib
->texcoord
.y2
;
835 out
[0] = attrib
->texcoord
.x1
;
836 out
[1] = attrib
->texcoord
.y2
;
839 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
840 enum pipe_format src_format
,
841 enum pipe_format dst_format
,
842 enum pipe_texture_target target
,
843 unsigned src_nr_samples
,
844 unsigned dst_nr_samples
,
848 struct pipe_context
*pipe
= ctx
->base
.pipe
;
849 enum tgsi_texture_type tgsi_tex
=
850 util_pipe_tex_to_tgsi_tex(target
, src_nr_samples
);
851 enum tgsi_return_type stype
;
852 enum tgsi_return_type dtype
;
855 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
857 if (util_format_is_pure_uint(src_format
)) {
858 stype
= TGSI_RETURN_TYPE_UINT
;
859 if (util_format_is_pure_uint(dst_format
)) {
860 dtype
= TGSI_RETURN_TYPE_UINT
;
863 assert(util_format_is_pure_sint(dst_format
));
864 dtype
= TGSI_RETURN_TYPE_SINT
;
867 } else if (util_format_is_pure_sint(src_format
)) {
868 stype
= TGSI_RETURN_TYPE_SINT
;
869 if (util_format_is_pure_sint(dst_format
)) {
870 dtype
= TGSI_RETURN_TYPE_SINT
;
873 assert(util_format_is_pure_uint(dst_format
));
874 dtype
= TGSI_RETURN_TYPE_UINT
;
878 assert(!util_format_is_pure_uint(dst_format
) &&
879 !util_format_is_pure_sint(dst_format
));
880 dtype
= stype
= TGSI_RETURN_TYPE_FLOAT
;
884 if (src_nr_samples
> 1) {
887 /* OpenGL requires that integer textures just copy 1 sample instead
890 if (dst_nr_samples
<= 1 &&
891 stype
!= TGSI_RETURN_TYPE_UINT
&&
892 stype
!= TGSI_RETURN_TYPE_SINT
) {
893 /* The destination has one sample, so we'll do color resolve. */
894 unsigned index
= GET_MSAA_RESOLVE_FS_IDX(src_nr_samples
);
898 shader
= &ctx
->fs_resolve
[target
][index
][filter
];
901 assert(!ctx
->cached_all_shaders
);
902 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
903 *shader
= util_make_fs_msaa_resolve_bilinear(pipe
, tgsi_tex
,
908 *shader
= util_make_fs_msaa_resolve(pipe
, tgsi_tex
,
915 /* The destination has multiple samples, we'll do
916 * an MSAA->MSAA copy.
918 shader
= &ctx
->fs_texfetch_col_msaa
[type
][target
];
920 /* Create the fragment shader on-demand. */
922 assert(!ctx
->cached_all_shaders
);
923 *shader
= util_make_fs_blit_msaa_color(pipe
, tgsi_tex
, stype
, dtype
);
932 shader
= &ctx
->fs_texfetch_col
[type
][target
][1];
934 shader
= &ctx
->fs_texfetch_col
[type
][target
][0];
936 /* Create the fragment shader on-demand. */
938 assert(!ctx
->cached_all_shaders
);
939 *shader
= util_make_fragment_tex_shader(pipe
, tgsi_tex
,
940 TGSI_INTERPOLATE_LINEAR
,
942 ctx
->has_tex_lz
, use_txf
);
950 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
951 enum pipe_texture_target target
,
955 struct pipe_context
*pipe
= ctx
->base
.pipe
;
957 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
959 if (nr_samples
> 1) {
960 void **shader
= &ctx
->fs_texfetch_depth_msaa
[target
];
962 /* Create the fragment shader on-demand. */
964 enum tgsi_texture_type tgsi_tex
;
965 assert(!ctx
->cached_all_shaders
);
966 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
967 *shader
= util_make_fs_blit_msaa_depth(pipe
, tgsi_tex
);
975 shader
= &ctx
->fs_texfetch_depth
[target
][1];
977 shader
= &ctx
->fs_texfetch_depth
[target
][0];
979 /* Create the fragment shader on-demand. */
981 enum tgsi_texture_type tgsi_tex
;
982 assert(!ctx
->cached_all_shaders
);
983 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
985 util_make_fragment_tex_shader_writedepth(pipe
, tgsi_tex
,
986 TGSI_INTERPOLATE_LINEAR
,
987 ctx
->has_tex_lz
, use_txf
);
995 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv
*ctx
,
996 enum pipe_texture_target target
,
1000 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1002 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1004 if (nr_samples
> 1) {
1005 void **shader
= &ctx
->fs_texfetch_depthstencil_msaa
[target
];
1007 /* Create the fragment shader on-demand. */
1009 enum tgsi_texture_type tgsi_tex
;
1010 assert(!ctx
->cached_all_shaders
);
1011 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1012 *shader
= util_make_fs_blit_msaa_depthstencil(pipe
, tgsi_tex
);
1020 shader
= &ctx
->fs_texfetch_depthstencil
[target
][1];
1022 shader
= &ctx
->fs_texfetch_depthstencil
[target
][0];
1024 /* Create the fragment shader on-demand. */
1026 enum tgsi_texture_type tgsi_tex
;
1027 assert(!ctx
->cached_all_shaders
);
1028 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1030 util_make_fragment_tex_shader_writedepthstencil(pipe
, tgsi_tex
,
1031 TGSI_INTERPOLATE_LINEAR
,
1041 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv
*ctx
,
1042 enum pipe_texture_target target
,
1043 unsigned nr_samples
,
1046 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1048 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1050 if (nr_samples
> 1) {
1051 void **shader
= &ctx
->fs_texfetch_stencil_msaa
[target
];
1053 /* Create the fragment shader on-demand. */
1055 enum tgsi_texture_type tgsi_tex
;
1056 assert(!ctx
->cached_all_shaders
);
1057 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1058 *shader
= util_make_fs_blit_msaa_stencil(pipe
, tgsi_tex
);
1066 shader
= &ctx
->fs_texfetch_stencil
[target
][1];
1068 shader
= &ctx
->fs_texfetch_stencil
[target
][0];
1070 /* Create the fragment shader on-demand. */
1072 enum tgsi_texture_type tgsi_tex
;
1073 assert(!ctx
->cached_all_shaders
);
1074 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1076 util_make_fragment_tex_shader_writestencil(pipe
, tgsi_tex
,
1077 TGSI_INTERPOLATE_LINEAR
,
1078 ctx
->has_tex_lz
, use_txf
);
1087 * Generate and save all fragment shaders that we will ever need for
1088 * blitting. Drivers which use the 'draw' fallbacks will typically use
1089 * this to make sure we generate/use shaders that don't go through the
1090 * draw module's wrapper functions.
1092 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
)
1094 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1095 struct pipe_context
*pipe
= blitter
->pipe
;
1096 struct pipe_screen
*screen
= pipe
->screen
;
1097 unsigned samples
, j
, f
, target
, max_samples
, use_txf
;
1098 bool has_arraytex
, has_cubearraytex
;
1100 max_samples
= ctx
->has_texture_multisample
? 2 : 1;
1101 has_arraytex
= screen
->get_param(screen
,
1102 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
) != 0;
1103 has_cubearraytex
= screen
->get_param(screen
,
1104 PIPE_CAP_CUBE_MAP_ARRAY
) != 0;
1106 /* It only matters if i <= 1 or > 1. */
1107 for (samples
= 1; samples
<= max_samples
; samples
++) {
1108 for (target
= PIPE_TEXTURE_1D
; target
< PIPE_MAX_TEXTURE_TYPES
; target
++) {
1109 for (use_txf
= 0; use_txf
<= ctx
->has_txf
; use_txf
++) {
1110 if (!has_arraytex
&&
1111 (target
== PIPE_TEXTURE_1D_ARRAY
||
1112 target
== PIPE_TEXTURE_2D_ARRAY
)) {
1115 if (!has_cubearraytex
&&
1116 (target
== PIPE_TEXTURE_CUBE_ARRAY
))
1120 (target
!= PIPE_TEXTURE_2D
&&
1121 target
!= PIPE_TEXTURE_2D_ARRAY
))
1124 if (samples
> 1 && use_txf
)
1125 continue; /* TXF is the only option, use_txf has no effect */
1127 /* If samples == 1, the shaders read one texel. If samples >= 1,
1128 * they read one sample.
1130 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
,
1131 PIPE_FORMAT_R32_FLOAT
, target
,
1132 samples
, samples
, 0, use_txf
);
1133 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1134 PIPE_FORMAT_R32_UINT
, target
,
1135 samples
, samples
, 0, use_txf
);
1136 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1137 PIPE_FORMAT_R32_SINT
, target
,
1138 samples
, samples
, 0, use_txf
);
1139 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1140 PIPE_FORMAT_R32_SINT
, target
,
1141 samples
, samples
, 0, use_txf
);
1142 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1143 PIPE_FORMAT_R32_UINT
, target
,
1144 samples
, samples
, 0, use_txf
);
1145 blitter_get_fs_texfetch_depth(ctx
, target
, samples
, use_txf
);
1146 if (ctx
->has_stencil_export
) {
1147 blitter_get_fs_texfetch_depthstencil(ctx
, target
, samples
, use_txf
);
1148 blitter_get_fs_texfetch_stencil(ctx
, target
, samples
, use_txf
);
1154 /* MSAA resolve shaders. */
1155 for (j
= 2; j
< 32; j
++) {
1156 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R32_FLOAT
,
1158 PIPE_BIND_SAMPLER_VIEW
)) {
1162 for (f
= 0; f
< 2; f
++) {
1163 if (f
!= PIPE_TEX_FILTER_NEAREST
&& use_txf
)
1166 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
,
1167 PIPE_FORMAT_R32_FLOAT
, target
,
1169 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1170 PIPE_FORMAT_R32_UINT
, target
,
1172 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1173 PIPE_FORMAT_R32_SINT
, target
,
1181 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
1183 ctx
->fs_write_one_cbuf
=
1184 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1185 TGSI_INTERPOLATE_CONSTANT
, false);
1187 ctx
->fs_write_all_cbufs
=
1188 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1189 TGSI_INTERPOLATE_CONSTANT
, true);
1191 ctx
->cached_all_shaders
= true;
1194 static void blitter_set_common_draw_rect_state(struct blitter_context_priv
*ctx
,
1198 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1200 pipe
->bind_rasterizer_state(pipe
, scissor
? ctx
->rs_state_scissor
1203 bind_vs_layered(ctx
);
1205 bind_vs_passthrough(ctx
);
1207 if (ctx
->has_geometry_shader
)
1208 pipe
->bind_gs_state(pipe
, NULL
);
1209 if (ctx
->has_tessellation
) {
1210 pipe
->bind_tcs_state(pipe
, NULL
);
1211 pipe
->bind_tes_state(pipe
, NULL
);
1213 if (ctx
->has_stream_out
)
1214 pipe
->set_stream_output_targets(pipe
, 0, NULL
, NULL
);
1217 static void blitter_draw(struct blitter_context_priv
*ctx
,
1218 int x1
, int y1
, int x2
, int y2
, float depth
,
1219 unsigned num_instances
)
1221 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1222 struct pipe_vertex_buffer vb
= {0};
1224 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
1226 vb
.stride
= 8 * sizeof(float);
1228 u_upload_data(pipe
->stream_uploader
, 0, sizeof(ctx
->vertices
), 4, ctx
->vertices
,
1229 &vb
.buffer_offset
, &vb
.buffer
.resource
);
1230 if (!vb
.buffer
.resource
)
1232 u_upload_unmap(pipe
->stream_uploader
);
1234 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1235 util_draw_arrays_instanced(pipe
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
1237 pipe_resource_reference(&vb
.buffer
.resource
, NULL
);
1240 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
1241 int x1
, int y1
, int x2
, int y2
,
1242 float depth
, unsigned num_instances
,
1243 enum blitter_attrib_type type
,
1244 const union blitter_attrib
*attrib
)
1246 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1250 case UTIL_BLITTER_ATTRIB_COLOR
:
1251 blitter_set_clear_color(ctx
, attrib
->color
);
1254 case UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW
:
1255 for (i
= 0; i
< 4; i
++) {
1256 ctx
->vertices
[i
][1][2] = attrib
->texcoord
.z
;
1257 ctx
->vertices
[i
][1][3] = attrib
->texcoord
.w
;
1260 case UTIL_BLITTER_ATTRIB_TEXCOORD_XY
:
1261 set_texcoords_in_vertices(attrib
, &ctx
->vertices
[0][1][0], 8);
1267 blitter_draw(ctx
, x1
, y1
, x2
, y2
, depth
, num_instances
);
1270 static void *get_clear_blend_state(struct blitter_context_priv
*ctx
,
1271 unsigned clear_buffers
)
1273 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1276 clear_buffers
&= PIPE_CLEAR_COLOR
;
1278 /* Return an existing blend state. */
1280 return ctx
->blend
[0][0];
1282 index
= GET_CLEAR_BLEND_STATE_IDX(clear_buffers
);
1284 if (ctx
->blend_clear
[index
])
1285 return ctx
->blend_clear
[index
];
1287 /* Create a new one. */
1289 struct pipe_blend_state blend
= {0};
1292 blend
.independent_blend_enable
= 1;
1294 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1295 if (clear_buffers
& (PIPE_CLEAR_COLOR0
<< i
)) {
1296 blend
.rt
[i
].colormask
= PIPE_MASK_RGBA
;
1300 ctx
->blend_clear
[index
] = pipe
->create_blend_state(pipe
, &blend
);
1302 return ctx
->blend_clear
[index
];
1305 void util_blitter_common_clear_setup(struct blitter_context
*blitter
,
1306 unsigned width
, unsigned height
,
1307 unsigned clear_buffers
,
1308 void *custom_blend
, void *custom_dsa
)
1310 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1311 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1313 util_blitter_set_running_flag(blitter
);
1314 blitter_check_saved_vertex_states(ctx
);
1315 blitter_check_saved_fragment_states(ctx
);
1316 blitter_disable_render_cond(ctx
);
1320 pipe
->bind_blend_state(pipe
, custom_blend
);
1322 pipe
->bind_blend_state(pipe
, get_clear_blend_state(ctx
, clear_buffers
));
1326 pipe
->bind_depth_stencil_alpha_state(pipe
, custom_dsa
);
1327 } else if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1328 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1329 } else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
1330 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1331 } else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
1332 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1334 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1337 pipe
->set_sample_mask(pipe
, ~0);
1338 blitter_set_dst_dimensions(ctx
, width
, height
);
1341 static void util_blitter_clear_custom(struct blitter_context
*blitter
,
1342 unsigned width
, unsigned height
,
1343 unsigned num_layers
,
1344 unsigned clear_buffers
,
1345 const union pipe_color_union
*color
,
1346 double depth
, unsigned stencil
,
1347 void *custom_blend
, void *custom_dsa
)
1349 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1350 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1351 struct pipe_stencil_ref sr
= { { 0 } };
1353 assert(ctx
->has_layered
|| num_layers
<= 1);
1355 util_blitter_common_clear_setup(blitter
, width
, height
, clear_buffers
,
1356 custom_blend
, custom_dsa
);
1358 sr
.ref_value
[0] = stencil
& 0xff;
1359 pipe
->set_stencil_ref(pipe
, &sr
);
1361 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1362 bind_fs_write_all_cbufs(ctx
);
1364 union blitter_attrib attrib
;
1365 memcpy(attrib
.color
, color
->ui
, sizeof(color
->ui
));
1367 if (num_layers
> 1 && ctx
->has_layered
) {
1368 blitter_set_common_draw_rect_state(ctx
, false, true);
1369 blitter
->draw_rectangle(blitter
, 0, 0, width
, height
, (float) depth
,
1370 num_layers
, UTIL_BLITTER_ATTRIB_COLOR
, &attrib
);
1372 blitter_set_common_draw_rect_state(ctx
, false, false);
1373 blitter
->draw_rectangle(blitter
, 0, 0, width
, height
, (float) depth
,
1374 1, UTIL_BLITTER_ATTRIB_COLOR
, &attrib
);
1377 util_blitter_restore_vertex_states(blitter
);
1378 util_blitter_restore_fragment_states(blitter
);
1379 util_blitter_restore_render_cond(blitter
);
1380 util_blitter_unset_running_flag(blitter
);
1383 void util_blitter_clear(struct blitter_context
*blitter
,
1384 unsigned width
, unsigned height
, unsigned num_layers
,
1385 unsigned clear_buffers
,
1386 const union pipe_color_union
*color
,
1387 double depth
, unsigned stencil
)
1389 util_blitter_clear_custom(blitter
, width
, height
, num_layers
,
1390 clear_buffers
, color
, depth
, stencil
,
1394 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1395 unsigned width
, unsigned height
,
1396 double depth
, void *custom_dsa
)
1398 static const union pipe_color_union color
;
1399 util_blitter_clear_custom(blitter
, width
, height
, 0, 0, &color
, depth
, 0,
1403 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1404 struct pipe_resource
*dst
,
1408 memset(dst_templ
, 0, sizeof(*dst_templ
));
1409 dst_templ
->format
= util_format_linear(dst
->format
);
1410 dst_templ
->u
.tex
.level
= dstlevel
;
1411 dst_templ
->u
.tex
.first_layer
= dstz
;
1412 dst_templ
->u
.tex
.last_layer
= dstz
;
1415 static struct pipe_surface
*
1416 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
1417 struct pipe_surface
*surf
)
1419 struct pipe_surface dst_templ
;
1421 memset(&dst_templ
, 0, sizeof(dst_templ
));
1422 dst_templ
.format
= surf
->format
;
1423 dst_templ
.u
.tex
.level
= surf
->u
.tex
.level
;
1424 dst_templ
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
+ 1;
1425 dst_templ
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
+ 1;
1427 return pipe
->create_surface(pipe
, surf
->texture
, &dst_templ
);
1430 void util_blitter_default_src_texture(struct blitter_context
*blitter
,
1431 struct pipe_sampler_view
*src_templ
,
1432 struct pipe_resource
*src
,
1435 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1437 memset(src_templ
, 0, sizeof(*src_templ
));
1439 if (ctx
->cube_as_2darray
&&
1440 (src
->target
== PIPE_TEXTURE_CUBE
||
1441 src
->target
== PIPE_TEXTURE_CUBE_ARRAY
))
1442 src_templ
->target
= PIPE_TEXTURE_2D_ARRAY
;
1444 src_templ
->target
= src
->target
;
1446 src_templ
->format
= util_format_linear(src
->format
);
1447 src_templ
->u
.tex
.first_level
= srclevel
;
1448 src_templ
->u
.tex
.last_level
= srclevel
;
1449 src_templ
->u
.tex
.first_layer
= 0;
1450 src_templ
->u
.tex
.last_layer
=
1451 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1452 : src
->array_size
- 1;
1453 src_templ
->swizzle_r
= PIPE_SWIZZLE_X
;
1454 src_templ
->swizzle_g
= PIPE_SWIZZLE_Y
;
1455 src_templ
->swizzle_b
= PIPE_SWIZZLE_Z
;
1456 src_templ
->swizzle_a
= PIPE_SWIZZLE_W
;
1459 static bool is_blit_generic_supported(struct blitter_context
*blitter
,
1460 const struct pipe_resource
*dst
,
1461 enum pipe_format dst_format
,
1462 const struct pipe_resource
*src
,
1463 enum pipe_format src_format
,
1466 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1467 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1471 const struct util_format_description
*desc
=
1472 util_format_description(dst_format
);
1473 bool dst_has_stencil
= util_format_has_stencil(desc
);
1475 /* Stencil export must be supported for stencil copy. */
1476 if ((mask
& PIPE_MASK_S
) && dst_has_stencil
&&
1477 !ctx
->has_stencil_export
) {
1481 if (dst_has_stencil
|| util_format_has_depth(desc
))
1482 bind
= PIPE_BIND_DEPTH_STENCIL
;
1484 bind
= PIPE_BIND_RENDER_TARGET
;
1486 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1487 dst
->nr_samples
, bind
)) {
1493 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1497 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1498 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
)) {
1502 /* Check stencil sampler support for stencil copy. */
1503 if (mask
& PIPE_MASK_S
) {
1504 if (util_format_has_stencil(util_format_description(src_format
))) {
1505 enum pipe_format stencil_format
=
1506 util_format_stencil_only(src_format
);
1507 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1509 if (stencil_format
!= src_format
&&
1510 !screen
->is_format_supported(screen
, stencil_format
,
1511 src
->target
, src
->nr_samples
,
1512 PIPE_BIND_SAMPLER_VIEW
)) {
1522 bool util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1523 const struct pipe_resource
*dst
,
1524 const struct pipe_resource
*src
)
1526 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1527 src
, src
->format
, PIPE_MASK_RGBAZS
);
1530 bool util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1531 const struct pipe_blit_info
*info
)
1533 return is_blit_generic_supported(blitter
,
1534 info
->dst
.resource
, info
->dst
.format
,
1535 info
->src
.resource
, info
->src
.format
,
1539 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1540 struct pipe_resource
*dst
,
1542 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1543 struct pipe_resource
*src
,
1545 const struct pipe_box
*srcbox
)
1547 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1548 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1549 struct pipe_surface
*dst_view
, dst_templ
;
1550 struct pipe_sampler_view src_templ
, *src_view
;
1551 struct pipe_box dstbox
;
1554 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1556 u_box_3d(dstx
, dsty
, dstz
, abs(srcbox
->width
), abs(srcbox
->height
),
1557 abs(srcbox
->depth
), &dstbox
);
1559 /* Initialize the surface. */
1560 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1561 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1563 /* Initialize the sampler view. */
1564 util_blitter_default_src_texture(blitter
, &src_templ
, src
, src_level
);
1565 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1568 util_blitter_blit_generic(blitter
, dst_view
, &dstbox
,
1569 src_view
, srcbox
, src
->width0
, src
->height0
,
1570 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
,
1573 pipe_surface_reference(&dst_view
, NULL
);
1574 pipe_sampler_view_reference(&src_view
, NULL
);
1578 blitter_draw_tex(struct blitter_context_priv
*ctx
,
1579 int dst_x1
, int dst_y1
, int dst_x2
, int dst_y2
,
1580 struct pipe_sampler_view
*src
,
1581 unsigned src_width0
, unsigned src_height0
,
1582 int src_x1
, int src_y1
, int src_x2
, int src_y2
,
1583 float layer
, unsigned sample
,
1584 bool uses_txf
, enum blitter_attrib_type type
)
1586 union blitter_attrib coord
;
1588 get_texcoords(src
, src_width0
, src_height0
,
1589 src_x1
, src_y1
, src_x2
, src_y2
, layer
, sample
,
1592 if (src
->target
== PIPE_TEXTURE_CUBE
||
1593 src
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
1594 float face_coord
[4][2];
1596 set_texcoords_in_vertices(&coord
, &face_coord
[0][0], 2);
1597 util_map_texcoords2d_onto_cubemap((unsigned)layer
% 6,
1598 /* pointer, stride in floats */
1599 &face_coord
[0][0], 2,
1600 &ctx
->vertices
[0][1][0], 8,
1602 for (unsigned i
= 0; i
< 4; i
++)
1603 ctx
->vertices
[i
][1][3] = coord
.texcoord
.w
;
1605 /* Cubemaps don't use draw_rectangle. */
1606 blitter_draw(ctx
, dst_x1
, dst_y1
, dst_x2
, dst_y2
, 0, 1);
1608 ctx
->base
.draw_rectangle(&ctx
->base
, dst_x1
, dst_y1
, dst_x2
, dst_y2
, 0,
1613 static void do_blits(struct blitter_context_priv
*ctx
,
1614 struct pipe_surface
*dst
,
1615 const struct pipe_box
*dstbox
,
1616 struct pipe_sampler_view
*src
,
1617 unsigned src_width0
,
1618 unsigned src_height0
,
1619 const struct pipe_box
*srcbox
,
1623 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1624 unsigned src_samples
= src
->texture
->nr_samples
;
1625 unsigned dst_samples
= dst
->texture
->nr_samples
;
1626 enum pipe_texture_target src_target
= src
->target
;
1627 struct pipe_framebuffer_state fb_state
= {0};
1629 /* Initialize framebuffer state. */
1630 fb_state
.width
= dst
->width
;
1631 fb_state
.height
= dst
->height
;
1632 fb_state
.nr_cbufs
= is_zsbuf
? 0 : 1;
1634 blitter_set_dst_dimensions(ctx
, fb_state
.width
, fb_state
.height
);
1636 if ((src_target
== PIPE_TEXTURE_1D
||
1637 src_target
== PIPE_TEXTURE_2D
||
1638 src_target
== PIPE_TEXTURE_RECT
) &&
1640 /* Set framebuffer state. */
1642 fb_state
.zsbuf
= dst
;
1644 fb_state
.cbufs
[0] = dst
;
1646 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1649 pipe
->set_sample_mask(pipe
, ~0);
1650 blitter_draw_tex(ctx
, dstbox
->x
, dstbox
->y
,
1651 dstbox
->x
+ dstbox
->width
,
1652 dstbox
->y
+ dstbox
->height
,
1653 src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1654 srcbox
->x
+ srcbox
->width
, srcbox
->y
+ srcbox
->height
,
1655 0, 0, uses_txf
, UTIL_BLITTER_ATTRIB_TEXCOORD_XY
);
1657 /* Draw the quad with the generic codepath. */
1659 for (dst_z
= 0; dst_z
< dstbox
->depth
; dst_z
++) {
1660 struct pipe_surface
*old
;
1661 float dst2src_scale
= srcbox
->depth
/ (float)dstbox
->depth
;
1663 /* Scale Z properly if the blit is scaled.
1665 * When downscaling, we want the coordinates centered, so that
1666 * mipmapping works for 3D textures. For example, when generating
1667 * a 4x4x4 level, this wouldn't average the pixels:
1669 * src Z: 0 1 2 3 4 5 6 7
1672 * Because the pixels are not centered below the pixels of the higher
1673 * level. Therefore, we want this:
1674 * src Z: 0 1 2 3 4 5 6 7
1677 * dst_offset defines the offset needed for centering the pixels and
1678 * it works with any scaling (not just 2x).
1680 float dst_offset
= ((srcbox
->depth
- 1) -
1681 (dstbox
->depth
- 1) * dst2src_scale
) * 0.5;
1682 float src_z
= (dst_z
+ dst_offset
) * dst2src_scale
;
1684 /* Set framebuffer state. */
1686 fb_state
.zsbuf
= dst
;
1688 fb_state
.cbufs
[0] = dst
;
1690 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1692 /* See if we need to blit a multisample or singlesample buffer. */
1693 if (src_samples
== dst_samples
&& dst_samples
> 1) {
1695 unsigned i
, max_sample
= dst_samples
- 1;
1697 for (i
= 0; i
<= max_sample
; i
++) {
1698 pipe
->set_sample_mask(pipe
, 1 << i
);
1699 blitter_draw_tex(ctx
, dstbox
->x
, dstbox
->y
,
1700 dstbox
->x
+ dstbox
->width
,
1701 dstbox
->y
+ dstbox
->height
,
1702 src
, src_width0
, src_height0
,
1703 srcbox
->x
, srcbox
->y
,
1704 srcbox
->x
+ srcbox
->width
,
1705 srcbox
->y
+ srcbox
->height
,
1706 srcbox
->z
+ src_z
, i
, uses_txf
,
1707 UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW
);
1710 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1711 pipe
->set_sample_mask(pipe
, ~0);
1712 blitter_draw_tex(ctx
, dstbox
->x
, dstbox
->y
,
1713 dstbox
->x
+ dstbox
->width
,
1714 dstbox
->y
+ dstbox
->height
,
1715 src
, src_width0
, src_height0
,
1716 srcbox
->x
, srcbox
->y
,
1717 srcbox
->x
+ srcbox
->width
,
1718 srcbox
->y
+ srcbox
->height
,
1719 srcbox
->z
+ src_z
, 0, uses_txf
,
1720 UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW
);
1723 /* Get the next surface or (if this is the last iteration)
1724 * just unreference the last one. */
1726 if (dst_z
< dstbox
->depth
-1) {
1727 dst
= util_blitter_get_next_surface_layer(ctx
->base
.pipe
, dst
);
1730 pipe_surface_reference(&old
, NULL
);
1736 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1737 struct pipe_surface
*dst
,
1738 const struct pipe_box
*dstbox
,
1739 struct pipe_sampler_view
*src
,
1740 const struct pipe_box
*srcbox
,
1741 unsigned src_width0
, unsigned src_height0
,
1742 unsigned mask
, unsigned filter
,
1743 const struct pipe_scissor_state
*scissor
,
1746 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1747 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1748 enum pipe_texture_target src_target
= src
->target
;
1749 unsigned src_samples
= src
->texture
->nr_samples
;
1750 unsigned dst_samples
= dst
->texture
->nr_samples
;
1751 bool has_depth
, has_stencil
, has_color
;
1752 bool blit_stencil
, blit_depth
, blit_color
;
1753 void *sampler_state
;
1754 const struct util_format_description
*src_desc
=
1755 util_format_description(src
->format
);
1756 const struct util_format_description
*dst_desc
=
1757 util_format_description(dst
->format
);
1759 has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
&&
1760 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1761 has_depth
= util_format_has_depth(src_desc
) &&
1762 util_format_has_depth(dst_desc
);
1763 has_stencil
= util_format_has_stencil(src_desc
) &&
1764 util_format_has_stencil(dst_desc
);
1766 blit_color
= has_color
&& (mask
& PIPE_MASK_RGBA
);
1767 blit_depth
= has_depth
&& (mask
& PIPE_MASK_Z
);
1768 blit_stencil
= has_stencil
&& (mask
& PIPE_MASK_S
) &&
1769 ctx
->has_stencil_export
;
1771 if (!blit_stencil
&& !blit_depth
&& !blit_color
) {
1775 bool is_scaled
= dstbox
->width
!= abs(srcbox
->width
) ||
1776 dstbox
->height
!= abs(srcbox
->height
);
1778 if (blit_stencil
|| !is_scaled
)
1779 filter
= PIPE_TEX_FILTER_NEAREST
;
1781 bool use_txf
= false;
1783 /* Don't support scaled blits. The TXF shader uses F2I for rounding. */
1786 filter
== PIPE_TEX_FILTER_NEAREST
&&
1787 src
->target
!= PIPE_TEXTURE_CUBE
&&
1788 src
->target
!= PIPE_TEXTURE_CUBE_ARRAY
) {
1789 int src_width
= u_minify(src_width0
, src
->u
.tex
.first_level
);
1790 int src_height
= u_minify(src_height0
, src
->u
.tex
.first_level
);
1791 int src_depth
= src
->u
.tex
.last_layer
+ 1;
1792 struct pipe_box box
= *srcbox
;
1794 /* Eliminate negative width/height/depth. */
1795 if (box
.width
< 0) {
1799 if (box
.height
< 0) {
1800 box
.y
+= box
.height
;
1803 if (box
.depth
< 0) {
1808 /* See if srcbox is in bounds. TXF doesn't clamp the coordinates. */
1810 box
.x
>= 0 && box
.x
< src_width
&&
1811 box
.y
>= 0 && box
.y
< src_height
&&
1812 box
.z
>= 0 && box
.z
< src_depth
&&
1813 box
.x
+ box
.width
> 0 && box
.x
+ box
.width
<= src_width
&&
1814 box
.y
+ box
.height
> 0 && box
.y
+ box
.height
<= src_height
&&
1815 box
.z
+ box
.depth
> 0 && box
.z
+ box
.depth
<= src_depth
;
1818 /* Check whether the states are properly saved. */
1819 util_blitter_set_running_flag(blitter
);
1820 blitter_check_saved_vertex_states(ctx
);
1821 blitter_check_saved_fragment_states(ctx
);
1822 blitter_check_saved_textures(ctx
);
1823 blitter_check_saved_fb_state(ctx
);
1824 blitter_disable_render_cond(ctx
);
1826 if (blit_depth
|| blit_stencil
) {
1827 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
1829 if (blit_depth
&& blit_stencil
) {
1830 pipe
->bind_depth_stencil_alpha_state(pipe
,
1831 ctx
->dsa_write_depth_stencil
);
1832 ctx
->bind_fs_state(pipe
,
1833 blitter_get_fs_texfetch_depthstencil(ctx
, src_target
,
1834 src_samples
, use_txf
));
1835 } else if (blit_depth
) {
1836 pipe
->bind_depth_stencil_alpha_state(pipe
,
1837 ctx
->dsa_write_depth_keep_stencil
);
1838 ctx
->bind_fs_state(pipe
,
1839 blitter_get_fs_texfetch_depth(ctx
, src_target
,
1840 src_samples
, use_txf
));
1841 } else { /* is_stencil */
1842 pipe
->bind_depth_stencil_alpha_state(pipe
,
1843 ctx
->dsa_keep_depth_write_stencil
);
1844 ctx
->bind_fs_state(pipe
,
1845 blitter_get_fs_texfetch_stencil(ctx
, src_target
,
1846 src_samples
, use_txf
));
1850 unsigned colormask
= mask
& PIPE_MASK_RGBA
;
1852 pipe
->bind_blend_state(pipe
, ctx
->blend
[colormask
][alpha_blend
]);
1853 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1854 ctx
->bind_fs_state(pipe
,
1855 blitter_get_fs_texfetch_col(ctx
, src
->format
, dst
->format
, src_target
,
1856 src_samples
, dst_samples
, filter
,
1860 /* Set the linear filter only for scaled color non-MSAA blits. */
1861 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
1862 if (src_target
== PIPE_TEXTURE_RECT
) {
1863 sampler_state
= ctx
->sampler_state_rect_linear
;
1865 sampler_state
= ctx
->sampler_state_linear
;
1868 if (src_target
== PIPE_TEXTURE_RECT
) {
1869 sampler_state
= ctx
->sampler_state_rect
;
1871 sampler_state
= ctx
->sampler_state
;
1876 if (blit_depth
&& blit_stencil
) {
1877 /* Setup two samplers, one for depth and the other one for stencil. */
1878 struct pipe_sampler_view templ
;
1879 struct pipe_sampler_view
*views
[2];
1880 void *samplers
[2] = {sampler_state
, sampler_state
};
1883 templ
.format
= util_format_stencil_only(templ
.format
);
1884 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1887 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1889 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, views
);
1890 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, samplers
);
1892 pipe_sampler_view_reference(&views
[1], NULL
);
1893 } else if (blit_stencil
) {
1894 /* Set a stencil-only sampler view for it not to sample depth instead. */
1895 struct pipe_sampler_view templ
;
1896 struct pipe_sampler_view
*view
;
1899 templ
.format
= util_format_stencil_only(templ
.format
);
1900 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1902 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1904 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &view
);
1905 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1906 0, 1, &sampler_state
);
1908 pipe_sampler_view_reference(&view
, NULL
);
1910 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src
);
1911 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1912 0, 1, &sampler_state
);
1915 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1917 pipe
->set_scissor_states(pipe
, 0, 1, scissor
);
1920 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
, false);
1922 do_blits(ctx
, dst
, dstbox
, src
, src_width0
, src_height0
,
1923 srcbox
, blit_depth
|| blit_stencil
, use_txf
);
1925 util_blitter_restore_vertex_states(blitter
);
1926 util_blitter_restore_fragment_states(blitter
);
1927 util_blitter_restore_textures(blitter
);
1928 util_blitter_restore_fb_state(blitter
);
1930 pipe
->set_scissor_states(pipe
, 0, 1, &ctx
->base
.saved_scissor
);
1932 util_blitter_restore_render_cond(blitter
);
1933 util_blitter_unset_running_flag(blitter
);
1937 util_blitter_blit(struct blitter_context
*blitter
,
1938 const struct pipe_blit_info
*info
)
1940 struct pipe_resource
*dst
= info
->dst
.resource
;
1941 struct pipe_resource
*src
= info
->src
.resource
;
1942 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1943 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1944 struct pipe_surface
*dst_view
, dst_templ
;
1945 struct pipe_sampler_view src_templ
, *src_view
;
1947 /* Initialize the surface. */
1948 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
1950 dst_templ
.format
= info
->dst
.format
;
1951 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1953 /* Initialize the sampler view. */
1954 util_blitter_default_src_texture(blitter
, &src_templ
, src
, info
->src
.level
);
1955 src_templ
.format
= info
->src
.format
;
1956 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1959 util_blitter_blit_generic(blitter
, dst_view
, &info
->dst
.box
,
1960 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
1961 info
->mask
, info
->filter
,
1962 info
->scissor_enable
? &info
->scissor
: NULL
,
1965 pipe_surface_reference(&dst_view
, NULL
);
1966 pipe_sampler_view_reference(&src_view
, NULL
);
1969 void util_blitter_generate_mipmap(struct blitter_context
*blitter
,
1970 struct pipe_resource
*tex
,
1971 enum pipe_format format
,
1972 unsigned base_level
, unsigned last_level
,
1973 unsigned first_layer
, unsigned last_layer
)
1975 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1976 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1977 struct pipe_surface dst_templ
, *dst_view
;
1978 struct pipe_sampler_view src_templ
, *src_view
;
1980 void *sampler_state
;
1981 const struct util_format_description
*desc
=
1982 util_format_description(format
);
1984 unsigned target
= tex
->target
;
1986 if (ctx
->cube_as_2darray
&&
1987 (target
== PIPE_TEXTURE_CUBE
|| target
== PIPE_TEXTURE_CUBE_ARRAY
))
1988 target
= PIPE_TEXTURE_2D_ARRAY
;
1990 assert(tex
->nr_samples
<= 1);
1991 assert(!util_format_has_stencil(desc
));
1993 is_depth
= desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
;
1995 /* Check whether the states are properly saved. */
1996 util_blitter_set_running_flag(blitter
);
1997 blitter_check_saved_vertex_states(ctx
);
1998 blitter_check_saved_fragment_states(ctx
);
1999 blitter_check_saved_textures(ctx
);
2000 blitter_check_saved_fb_state(ctx
);
2001 blitter_disable_render_cond(ctx
);
2005 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
2006 pipe
->bind_depth_stencil_alpha_state(pipe
,
2007 ctx
->dsa_write_depth_keep_stencil
);
2008 ctx
->bind_fs_state(pipe
,
2009 blitter_get_fs_texfetch_depth(ctx
, target
, 1, false));
2011 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2012 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2013 ctx
->bind_fs_state(pipe
,
2014 blitter_get_fs_texfetch_col(ctx
, tex
->format
, tex
->format
, target
,
2015 1, 1, PIPE_TEX_FILTER_LINEAR
, false));
2018 if (target
== PIPE_TEXTURE_RECT
) {
2019 sampler_state
= ctx
->sampler_state_rect_linear
;
2021 sampler_state
= ctx
->sampler_state_linear
;
2023 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
2024 0, 1, &sampler_state
);
2026 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2027 blitter_set_common_draw_rect_state(ctx
, false, false);
2029 for (src_level
= base_level
; src_level
< last_level
; src_level
++) {
2030 struct pipe_box dstbox
= {0}, srcbox
= {0};
2031 unsigned dst_level
= src_level
+ 1;
2033 dstbox
.width
= u_minify(tex
->width0
, dst_level
);
2034 dstbox
.height
= u_minify(tex
->height0
, dst_level
);
2036 srcbox
.width
= u_minify(tex
->width0
, src_level
);
2037 srcbox
.height
= u_minify(tex
->height0
, src_level
);
2039 if (target
== PIPE_TEXTURE_3D
) {
2040 dstbox
.depth
= util_max_layer(tex
, dst_level
) + 1;
2041 srcbox
.depth
= util_max_layer(tex
, src_level
) + 1;
2043 dstbox
.z
= srcbox
.z
= first_layer
;
2044 dstbox
.depth
= srcbox
.depth
= last_layer
- first_layer
+ 1;
2047 /* Initialize the surface. */
2048 util_blitter_default_dst_texture(&dst_templ
, tex
, dst_level
,
2050 dst_templ
.format
= format
;
2051 dst_view
= pipe
->create_surface(pipe
, tex
, &dst_templ
);
2053 /* Initialize the sampler view. */
2054 util_blitter_default_src_texture(blitter
, &src_templ
, tex
, src_level
);
2055 src_templ
.format
= format
;
2056 src_view
= pipe
->create_sampler_view(pipe
, tex
, &src_templ
);
2058 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src_view
);
2060 do_blits(ctx
, dst_view
, &dstbox
, src_view
, tex
->width0
, tex
->height0
,
2061 &srcbox
, is_depth
, false);
2063 pipe_surface_reference(&dst_view
, NULL
);
2064 pipe_sampler_view_reference(&src_view
, NULL
);
2067 util_blitter_restore_vertex_states(blitter
);
2068 util_blitter_restore_fragment_states(blitter
);
2069 util_blitter_restore_textures(blitter
);
2070 util_blitter_restore_fb_state(blitter
);
2071 util_blitter_restore_render_cond(blitter
);
2072 util_blitter_unset_running_flag(blitter
);
2075 /* Clear a region of a color surface to a constant value. */
2076 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
2077 struct pipe_surface
*dstsurf
,
2078 const union pipe_color_union
*color
,
2079 unsigned dstx
, unsigned dsty
,
2080 unsigned width
, unsigned height
)
2082 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2083 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2084 struct pipe_framebuffer_state fb_state
;
2085 unsigned num_layers
;
2087 assert(dstsurf
->texture
);
2088 if (!dstsurf
->texture
)
2091 /* check the saved state */
2092 util_blitter_set_running_flag(blitter
);
2093 blitter_check_saved_vertex_states(ctx
);
2094 blitter_check_saved_fragment_states(ctx
);
2095 blitter_check_saved_fb_state(ctx
);
2096 blitter_disable_render_cond(ctx
);
2099 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2100 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2101 bind_fs_write_one_cbuf(ctx
);
2102 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2104 /* set a framebuffer state */
2105 fb_state
.width
= dstsurf
->width
;
2106 fb_state
.height
= dstsurf
->height
;
2107 fb_state
.nr_cbufs
= 1;
2108 fb_state
.cbufs
[0] = dstsurf
;
2110 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2111 pipe
->set_sample_mask(pipe
, ~0);
2113 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2115 union blitter_attrib attrib
;
2116 memcpy(attrib
.color
, color
->ui
, sizeof(color
->ui
));
2118 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
2119 if (num_layers
> 1 && ctx
->has_layered
) {
2120 blitter_set_common_draw_rect_state(ctx
, false, true);
2121 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
2122 num_layers
, UTIL_BLITTER_ATTRIB_COLOR
, &attrib
);
2124 blitter_set_common_draw_rect_state(ctx
, false, false);
2125 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
2126 1, UTIL_BLITTER_ATTRIB_COLOR
, &attrib
);
2129 util_blitter_restore_vertex_states(blitter
);
2130 util_blitter_restore_fragment_states(blitter
);
2131 util_blitter_restore_fb_state(blitter
);
2132 util_blitter_restore_render_cond(blitter
);
2133 util_blitter_unset_running_flag(blitter
);
2136 /* Clear a region of a depth stencil surface. */
2137 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
2138 struct pipe_surface
*dstsurf
,
2139 unsigned clear_flags
,
2142 unsigned dstx
, unsigned dsty
,
2143 unsigned width
, unsigned height
)
2145 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2146 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2147 struct pipe_framebuffer_state fb_state
;
2148 struct pipe_stencil_ref sr
= { { 0 } };
2149 unsigned num_layers
;
2151 assert(dstsurf
->texture
);
2152 if (!dstsurf
->texture
)
2155 /* check the saved state */
2156 util_blitter_set_running_flag(blitter
);
2157 blitter_check_saved_vertex_states(ctx
);
2158 blitter_check_saved_fragment_states(ctx
);
2159 blitter_check_saved_fb_state(ctx
);
2160 blitter_disable_render_cond(ctx
);
2163 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
2164 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
2165 sr
.ref_value
[0] = stencil
& 0xff;
2166 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
2167 pipe
->set_stencil_ref(pipe
, &sr
);
2169 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
2170 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
2172 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
2173 sr
.ref_value
[0] = stencil
& 0xff;
2174 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
2175 pipe
->set_stencil_ref(pipe
, &sr
);
2178 /* hmm that should be illegal probably, or make it a no-op somewhere */
2179 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2182 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2184 /* set a framebuffer state */
2185 fb_state
.width
= dstsurf
->width
;
2186 fb_state
.height
= dstsurf
->height
;
2187 fb_state
.nr_cbufs
= 0;
2188 fb_state
.cbufs
[0] = 0;
2189 fb_state
.zsbuf
= dstsurf
;
2190 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2191 pipe
->set_sample_mask(pipe
, ~0);
2193 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2195 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
2196 if (num_layers
> 1 && ctx
->has_layered
) {
2197 blitter_set_common_draw_rect_state(ctx
, false, true);
2198 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
,
2200 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2202 blitter_set_common_draw_rect_state(ctx
, false, false);
2203 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
,
2205 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2208 util_blitter_restore_vertex_states(blitter
);
2209 util_blitter_restore_fragment_states(blitter
);
2210 util_blitter_restore_fb_state(blitter
);
2211 util_blitter_restore_render_cond(blitter
);
2212 util_blitter_unset_running_flag(blitter
);
2215 /* draw a rectangle across a region using a custom dsa stage - for r600g */
2216 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
2217 struct pipe_surface
*zsurf
,
2218 struct pipe_surface
*cbsurf
,
2219 unsigned sample_mask
,
2220 void *dsa_stage
, float depth
)
2222 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2223 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2224 struct pipe_framebuffer_state fb_state
;
2226 assert(zsurf
->texture
);
2227 if (!zsurf
->texture
)
2230 /* check the saved state */
2231 util_blitter_set_running_flag(blitter
);
2232 blitter_check_saved_vertex_states(ctx
);
2233 blitter_check_saved_fragment_states(ctx
);
2234 blitter_check_saved_fb_state(ctx
);
2235 blitter_disable_render_cond(ctx
);
2238 pipe
->bind_blend_state(pipe
, cbsurf
? ctx
->blend
[PIPE_MASK_RGBA
][0] :
2240 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
2242 bind_fs_write_one_cbuf(ctx
);
2245 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2247 /* set a framebuffer state */
2248 fb_state
.width
= zsurf
->width
;
2249 fb_state
.height
= zsurf
->height
;
2250 fb_state
.nr_cbufs
= 1;
2252 fb_state
.cbufs
[0] = cbsurf
;
2253 fb_state
.nr_cbufs
= 1;
2255 fb_state
.cbufs
[0] = NULL
;
2256 fb_state
.nr_cbufs
= 0;
2258 fb_state
.zsbuf
= zsurf
;
2259 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2260 pipe
->set_sample_mask(pipe
, sample_mask
);
2262 blitter_set_common_draw_rect_state(ctx
, false, false);
2263 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
2264 blitter
->draw_rectangle(blitter
, 0, 0, zsurf
->width
, zsurf
->height
, depth
,
2265 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2267 util_blitter_restore_vertex_states(blitter
);
2268 util_blitter_restore_fragment_states(blitter
);
2269 util_blitter_restore_fb_state(blitter
);
2270 util_blitter_restore_render_cond(blitter
);
2271 util_blitter_unset_running_flag(blitter
);
2274 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
2275 struct pipe_resource
*dst
,
2277 struct pipe_resource
*src
,
2281 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2282 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2283 struct pipe_vertex_buffer vb
;
2284 struct pipe_stream_output_target
*so_target
;
2285 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2287 if (srcx
>= src
->width0
||
2288 dstx
>= dst
->width0
) {
2291 if (srcx
+ size
> src
->width0
) {
2292 size
= src
->width0
- srcx
;
2294 if (dstx
+ size
> dst
->width0
) {
2295 size
= dst
->width0
- dstx
;
2298 /* Drivers not capable of Stream Out should not call this function
2299 * in the first place. */
2300 assert(ctx
->has_stream_out
);
2302 /* Some alignment is required. */
2303 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
2304 !ctx
->has_stream_out
) {
2305 struct pipe_box box
;
2306 u_box_1d(srcx
, size
, &box
);
2307 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
2311 util_blitter_set_running_flag(blitter
);
2312 blitter_check_saved_vertex_states(ctx
);
2313 blitter_disable_render_cond(ctx
);
2315 vb
.is_user_buffer
= false;
2316 vb
.buffer
.resource
= src
;
2317 vb
.buffer_offset
= srcx
;
2320 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2321 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[0]);
2322 bind_vs_pos_only(ctx
, 1);
2323 if (ctx
->has_geometry_shader
)
2324 pipe
->bind_gs_state(pipe
, NULL
);
2325 if (ctx
->has_tessellation
) {
2326 pipe
->bind_tcs_state(pipe
, NULL
);
2327 pipe
->bind_tes_state(pipe
, NULL
);
2329 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2331 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
2332 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2334 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2336 util_blitter_restore_vertex_states(blitter
);
2337 util_blitter_restore_render_cond(blitter
);
2338 util_blitter_unset_running_flag(blitter
);
2339 pipe_so_target_reference(&so_target
, NULL
);
2342 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
2343 struct pipe_resource
*dst
,
2344 unsigned offset
, unsigned size
,
2345 unsigned num_channels
,
2346 const union pipe_color_union
*clear_value
)
2348 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2349 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2350 struct pipe_vertex_buffer vb
= {0};
2351 struct pipe_stream_output_target
*so_target
= NULL
;
2352 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2354 assert(num_channels
>= 1);
2355 assert(num_channels
<= 4);
2357 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
2359 * R600 uses this to initialize texture resources, so width0 might not be
2360 * what you think it is.
2363 /* Streamout is required. */
2364 if (!ctx
->has_stream_out
) {
2365 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2369 /* Some alignment is required. */
2370 if (offset
% 4 != 0 || size
% 4 != 0) {
2371 assert(!"Bad alignment in util_blitter_clear_buffer()");
2375 u_upload_data(pipe
->stream_uploader
, 0, num_channels
*4, 4, clear_value
,
2376 &vb
.buffer_offset
, &vb
.buffer
.resource
);
2377 if (!vb
.buffer
.resource
)
2382 util_blitter_set_running_flag(blitter
);
2383 blitter_check_saved_vertex_states(ctx
);
2384 blitter_disable_render_cond(ctx
);
2386 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2387 pipe
->bind_vertex_elements_state(pipe
,
2388 ctx
->velem_state_readbuf
[num_channels
-1]);
2389 bind_vs_pos_only(ctx
, num_channels
);
2390 if (ctx
->has_geometry_shader
)
2391 pipe
->bind_gs_state(pipe
, NULL
);
2392 if (ctx
->has_tessellation
) {
2393 pipe
->bind_tcs_state(pipe
, NULL
);
2394 pipe
->bind_tes_state(pipe
, NULL
);
2396 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2398 so_target
= pipe
->create_stream_output_target(pipe
, dst
, offset
, size
);
2399 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2401 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2404 util_blitter_restore_vertex_states(blitter
);
2405 util_blitter_restore_render_cond(blitter
);
2406 util_blitter_unset_running_flag(blitter
);
2407 pipe_so_target_reference(&so_target
, NULL
);
2408 pipe_resource_reference(&vb
.buffer
.resource
, NULL
);
2411 /* probably radeon specific */
2412 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
2413 struct pipe_resource
*dst
,
2416 struct pipe_resource
*src
,
2418 unsigned sample_mask
,
2420 enum pipe_format format
)
2422 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2423 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2424 struct pipe_framebuffer_state fb_state
;
2425 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
2427 util_blitter_set_running_flag(blitter
);
2428 blitter_check_saved_vertex_states(ctx
);
2429 blitter_check_saved_fragment_states(ctx
);
2430 blitter_disable_render_cond(ctx
);
2433 pipe
->bind_blend_state(pipe
, custom_blend
);
2434 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2435 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2436 bind_fs_write_one_cbuf(ctx
);
2437 pipe
->set_sample_mask(pipe
, sample_mask
);
2439 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
2440 surf_tmpl
.format
= format
;
2441 surf_tmpl
.u
.tex
.level
= dst_level
;
2442 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
2443 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
2445 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
2447 surf_tmpl
.u
.tex
.level
= 0;
2448 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
2449 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
2451 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
2453 /* set a framebuffer state */
2454 fb_state
.width
= src
->width0
;
2455 fb_state
.height
= src
->height0
;
2456 fb_state
.nr_cbufs
= 2;
2457 fb_state
.cbufs
[0] = srcsurf
;
2458 fb_state
.cbufs
[1] = dstsurf
;
2459 fb_state
.zsbuf
= NULL
;
2460 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2462 blitter_set_common_draw_rect_state(ctx
, false, false);
2463 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
2464 blitter
->draw_rectangle(blitter
, 0, 0, src
->width0
, src
->height0
,
2466 util_blitter_restore_fb_state(blitter
);
2467 util_blitter_restore_vertex_states(blitter
);
2468 util_blitter_restore_fragment_states(blitter
);
2469 util_blitter_restore_render_cond(blitter
);
2470 util_blitter_unset_running_flag(blitter
);
2472 pipe_surface_reference(&srcsurf
, NULL
);
2473 pipe_surface_reference(&dstsurf
, NULL
);
2476 void util_blitter_custom_color(struct blitter_context
*blitter
,
2477 struct pipe_surface
*dstsurf
,
2480 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2481 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2482 struct pipe_framebuffer_state fb_state
;
2484 assert(dstsurf
->texture
);
2485 if (!dstsurf
->texture
)
2488 /* check the saved state */
2489 util_blitter_set_running_flag(blitter
);
2490 blitter_check_saved_vertex_states(ctx
);
2491 blitter_check_saved_fragment_states(ctx
);
2492 blitter_check_saved_fb_state(ctx
);
2493 blitter_disable_render_cond(ctx
);
2496 pipe
->bind_blend_state(pipe
, custom_blend
? custom_blend
2497 : ctx
->blend
[PIPE_MASK_RGBA
][0]);
2498 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2499 bind_fs_write_one_cbuf(ctx
);
2500 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2501 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2503 /* set a framebuffer state */
2504 fb_state
.width
= dstsurf
->width
;
2505 fb_state
.height
= dstsurf
->height
;
2506 fb_state
.nr_cbufs
= 1;
2507 fb_state
.cbufs
[0] = dstsurf
;
2509 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2510 pipe
->set_sample_mask(pipe
, ~0);
2512 blitter_set_common_draw_rect_state(ctx
, false, false);
2513 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2514 blitter
->draw_rectangle(blitter
, 0, 0, dstsurf
->width
, dstsurf
->height
,
2517 util_blitter_restore_vertex_states(blitter
);
2518 util_blitter_restore_fragment_states(blitter
);
2519 util_blitter_restore_fb_state(blitter
);
2520 util_blitter_restore_render_cond(blitter
);
2521 util_blitter_unset_running_flag(blitter
);