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
, bool uses_txf
,
759 union blitter_attrib
*out
)
761 unsigned level
= src
->u
.tex
.first_level
;
762 bool normalized
= !uses_txf
&&
763 src
->target
!= PIPE_TEXTURE_RECT
&&
764 src
->texture
->nr_samples
<= 1;
767 out
->texcoord
.x1
= x1
/ (float)u_minify(src_width0
, level
);
768 out
->texcoord
.y1
= y1
/ (float)u_minify(src_height0
, level
);
769 out
->texcoord
.x2
= x2
/ (float)u_minify(src_width0
, level
);
770 out
->texcoord
.y2
= y2
/ (float)u_minify(src_height0
, level
);
772 out
->texcoord
.x1
= x1
;
773 out
->texcoord
.y1
= y1
;
774 out
->texcoord
.x2
= x2
;
775 out
->texcoord
.y2
= y2
;
779 static void set_texcoords_in_vertices(const union blitter_attrib
*attrib
,
780 float *out
, unsigned stride
)
782 out
[0] = attrib
->texcoord
.x1
;
783 out
[1] = attrib
->texcoord
.y1
;
785 out
[0] = attrib
->texcoord
.x2
;
786 out
[1] = attrib
->texcoord
.y1
;
788 out
[0] = attrib
->texcoord
.x2
;
789 out
[1] = attrib
->texcoord
.y2
;
791 out
[0] = attrib
->texcoord
.x1
;
792 out
[1] = attrib
->texcoord
.y2
;
795 static void blitter_set_texcoords(struct blitter_context_priv
*ctx
,
796 struct pipe_sampler_view
*src
,
797 unsigned src_width0
, unsigned src_height0
,
798 float layer
, unsigned sample
,
799 int x1
, int y1
, int x2
, int y2
,
803 union blitter_attrib coord
;
804 float face_coord
[4][2];
806 get_texcoords(src
, src_width0
, src_height0
, x1
, y1
, x2
, y2
, uses_txf
,
809 if (src
->target
== PIPE_TEXTURE_CUBE
||
810 src
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
811 set_texcoords_in_vertices(&coord
, &face_coord
[0][0], 2);
812 util_map_texcoords2d_onto_cubemap((unsigned)layer
% 6,
813 /* pointer, stride in floats */
814 &face_coord
[0][0], 2,
815 &ctx
->vertices
[0][1][0], 8,
818 set_texcoords_in_vertices(&coord
, &ctx
->vertices
[0][1][0], 8);
822 switch (src
->target
) {
823 case PIPE_TEXTURE_3D
:
828 r
/= u_minify(src
->texture
->depth0
, src
->u
.tex
.first_level
);
830 for (i
= 0; i
< 4; i
++)
831 ctx
->vertices
[i
][1][2] = r
; /*r*/
835 case PIPE_TEXTURE_1D_ARRAY
:
836 for (i
= 0; i
< 4; i
++)
837 ctx
->vertices
[i
][1][1] = (float) layer
; /*t*/
840 case PIPE_TEXTURE_2D_ARRAY
:
841 for (i
= 0; i
< 4; i
++) {
842 ctx
->vertices
[i
][1][2] = (float) layer
; /*r*/
843 ctx
->vertices
[i
][1][3] = (float) sample
; /*q*/
847 case PIPE_TEXTURE_CUBE_ARRAY
:
848 for (i
= 0; i
< 4; i
++)
849 ctx
->vertices
[i
][1][3] = (float) ((unsigned)layer
/ 6); /*w*/
852 case PIPE_TEXTURE_2D
:
853 for (i
= 0; i
< 4; i
++) {
854 ctx
->vertices
[i
][1][3] = (float) sample
; /*r*/
862 static void blitter_set_dst_dimensions(struct blitter_context_priv
*ctx
,
863 unsigned width
, unsigned height
)
865 ctx
->dst_width
= width
;
866 ctx
->dst_height
= height
;
869 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
870 enum pipe_format src_format
,
871 enum pipe_format dst_format
,
872 enum pipe_texture_target target
,
873 unsigned src_nr_samples
,
874 unsigned dst_nr_samples
,
878 struct pipe_context
*pipe
= ctx
->base
.pipe
;
879 enum tgsi_texture_type tgsi_tex
=
880 util_pipe_tex_to_tgsi_tex(target
, src_nr_samples
);
881 enum tgsi_return_type stype
;
882 enum tgsi_return_type dtype
;
885 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
887 if (util_format_is_pure_uint(src_format
)) {
888 stype
= TGSI_RETURN_TYPE_UINT
;
889 if (util_format_is_pure_uint(dst_format
)) {
890 dtype
= TGSI_RETURN_TYPE_UINT
;
893 assert(util_format_is_pure_sint(dst_format
));
894 dtype
= TGSI_RETURN_TYPE_SINT
;
897 } else if (util_format_is_pure_sint(src_format
)) {
898 stype
= TGSI_RETURN_TYPE_SINT
;
899 if (util_format_is_pure_sint(dst_format
)) {
900 dtype
= TGSI_RETURN_TYPE_SINT
;
903 assert(util_format_is_pure_uint(dst_format
));
904 dtype
= TGSI_RETURN_TYPE_UINT
;
908 assert(!util_format_is_pure_uint(dst_format
) &&
909 !util_format_is_pure_sint(dst_format
));
910 dtype
= stype
= TGSI_RETURN_TYPE_FLOAT
;
914 if (src_nr_samples
> 1) {
917 /* OpenGL requires that integer textures just copy 1 sample instead
920 if (dst_nr_samples
<= 1 &&
921 stype
!= TGSI_RETURN_TYPE_UINT
&&
922 stype
!= TGSI_RETURN_TYPE_SINT
) {
923 /* The destination has one sample, so we'll do color resolve. */
924 unsigned index
= GET_MSAA_RESOLVE_FS_IDX(src_nr_samples
);
928 shader
= &ctx
->fs_resolve
[target
][index
][filter
];
931 assert(!ctx
->cached_all_shaders
);
932 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
933 *shader
= util_make_fs_msaa_resolve_bilinear(pipe
, tgsi_tex
,
938 *shader
= util_make_fs_msaa_resolve(pipe
, tgsi_tex
,
945 /* The destination has multiple samples, we'll do
946 * an MSAA->MSAA copy.
948 shader
= &ctx
->fs_texfetch_col_msaa
[type
][target
];
950 /* Create the fragment shader on-demand. */
952 assert(!ctx
->cached_all_shaders
);
953 *shader
= util_make_fs_blit_msaa_color(pipe
, tgsi_tex
, stype
, dtype
);
962 shader
= &ctx
->fs_texfetch_col
[type
][target
][1];
964 shader
= &ctx
->fs_texfetch_col
[type
][target
][0];
966 /* Create the fragment shader on-demand. */
968 assert(!ctx
->cached_all_shaders
);
969 *shader
= util_make_fragment_tex_shader(pipe
, tgsi_tex
,
970 TGSI_INTERPOLATE_LINEAR
,
972 ctx
->has_tex_lz
, use_txf
);
980 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
981 enum pipe_texture_target target
,
985 struct pipe_context
*pipe
= ctx
->base
.pipe
;
987 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
989 if (nr_samples
> 1) {
990 void **shader
= &ctx
->fs_texfetch_depth_msaa
[target
];
992 /* Create the fragment shader on-demand. */
994 enum tgsi_texture_type tgsi_tex
;
995 assert(!ctx
->cached_all_shaders
);
996 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
997 *shader
= util_make_fs_blit_msaa_depth(pipe
, tgsi_tex
);
1005 shader
= &ctx
->fs_texfetch_depth
[target
][1];
1007 shader
= &ctx
->fs_texfetch_depth
[target
][0];
1009 /* Create the fragment shader on-demand. */
1011 enum tgsi_texture_type tgsi_tex
;
1012 assert(!ctx
->cached_all_shaders
);
1013 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1015 util_make_fragment_tex_shader_writedepth(pipe
, tgsi_tex
,
1016 TGSI_INTERPOLATE_LINEAR
,
1017 ctx
->has_tex_lz
, use_txf
);
1025 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv
*ctx
,
1026 enum pipe_texture_target target
,
1027 unsigned nr_samples
,
1030 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1032 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1034 if (nr_samples
> 1) {
1035 void **shader
= &ctx
->fs_texfetch_depthstencil_msaa
[target
];
1037 /* Create the fragment shader on-demand. */
1039 enum tgsi_texture_type tgsi_tex
;
1040 assert(!ctx
->cached_all_shaders
);
1041 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1042 *shader
= util_make_fs_blit_msaa_depthstencil(pipe
, tgsi_tex
);
1050 shader
= &ctx
->fs_texfetch_depthstencil
[target
][1];
1052 shader
= &ctx
->fs_texfetch_depthstencil
[target
][0];
1054 /* Create the fragment shader on-demand. */
1056 enum tgsi_texture_type tgsi_tex
;
1057 assert(!ctx
->cached_all_shaders
);
1058 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1060 util_make_fragment_tex_shader_writedepthstencil(pipe
, tgsi_tex
,
1061 TGSI_INTERPOLATE_LINEAR
,
1071 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv
*ctx
,
1072 enum pipe_texture_target target
,
1073 unsigned nr_samples
,
1076 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1078 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1080 if (nr_samples
> 1) {
1081 void **shader
= &ctx
->fs_texfetch_stencil_msaa
[target
];
1083 /* Create the fragment shader on-demand. */
1085 enum tgsi_texture_type tgsi_tex
;
1086 assert(!ctx
->cached_all_shaders
);
1087 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1088 *shader
= util_make_fs_blit_msaa_stencil(pipe
, tgsi_tex
);
1096 shader
= &ctx
->fs_texfetch_stencil
[target
][1];
1098 shader
= &ctx
->fs_texfetch_stencil
[target
][0];
1100 /* Create the fragment shader on-demand. */
1102 enum tgsi_texture_type tgsi_tex
;
1103 assert(!ctx
->cached_all_shaders
);
1104 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1106 util_make_fragment_tex_shader_writestencil(pipe
, tgsi_tex
,
1107 TGSI_INTERPOLATE_LINEAR
,
1108 ctx
->has_tex_lz
, use_txf
);
1117 * Generate and save all fragment shaders that we will ever need for
1118 * blitting. Drivers which use the 'draw' fallbacks will typically use
1119 * this to make sure we generate/use shaders that don't go through the
1120 * draw module's wrapper functions.
1122 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
)
1124 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1125 struct pipe_context
*pipe
= blitter
->pipe
;
1126 struct pipe_screen
*screen
= pipe
->screen
;
1127 unsigned samples
, j
, f
, target
, max_samples
, use_txf
;
1128 bool has_arraytex
, has_cubearraytex
;
1130 max_samples
= ctx
->has_texture_multisample
? 2 : 1;
1131 has_arraytex
= screen
->get_param(screen
,
1132 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
) != 0;
1133 has_cubearraytex
= screen
->get_param(screen
,
1134 PIPE_CAP_CUBE_MAP_ARRAY
) != 0;
1136 /* It only matters if i <= 1 or > 1. */
1137 for (samples
= 1; samples
<= max_samples
; samples
++) {
1138 for (target
= PIPE_TEXTURE_1D
; target
< PIPE_MAX_TEXTURE_TYPES
; target
++) {
1139 for (use_txf
= 0; use_txf
<= ctx
->has_txf
; use_txf
++) {
1140 if (!has_arraytex
&&
1141 (target
== PIPE_TEXTURE_1D_ARRAY
||
1142 target
== PIPE_TEXTURE_2D_ARRAY
)) {
1145 if (!has_cubearraytex
&&
1146 (target
== PIPE_TEXTURE_CUBE_ARRAY
))
1150 (target
!= PIPE_TEXTURE_2D
&&
1151 target
!= PIPE_TEXTURE_2D_ARRAY
))
1154 if (samples
> 1 && use_txf
)
1155 continue; /* TXF is the only option, use_txf has no effect */
1157 /* If samples == 1, the shaders read one texel. If samples >= 1,
1158 * they read one sample.
1160 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
,
1161 PIPE_FORMAT_R32_FLOAT
, target
,
1162 samples
, samples
, 0, use_txf
);
1163 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1164 PIPE_FORMAT_R32_UINT
, target
,
1165 samples
, samples
, 0, use_txf
);
1166 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1167 PIPE_FORMAT_R32_SINT
, target
,
1168 samples
, samples
, 0, use_txf
);
1169 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1170 PIPE_FORMAT_R32_SINT
, target
,
1171 samples
, samples
, 0, use_txf
);
1172 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1173 PIPE_FORMAT_R32_UINT
, target
,
1174 samples
, samples
, 0, use_txf
);
1175 blitter_get_fs_texfetch_depth(ctx
, target
, samples
, use_txf
);
1176 if (ctx
->has_stencil_export
) {
1177 blitter_get_fs_texfetch_depthstencil(ctx
, target
, samples
, use_txf
);
1178 blitter_get_fs_texfetch_stencil(ctx
, target
, samples
, use_txf
);
1184 /* MSAA resolve shaders. */
1185 for (j
= 2; j
< 32; j
++) {
1186 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R32_FLOAT
,
1188 PIPE_BIND_SAMPLER_VIEW
)) {
1192 for (f
= 0; f
< 2; f
++) {
1193 if (f
!= PIPE_TEX_FILTER_NEAREST
&& use_txf
)
1196 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
,
1197 PIPE_FORMAT_R32_FLOAT
, target
,
1199 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1200 PIPE_FORMAT_R32_UINT
, target
,
1202 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1203 PIPE_FORMAT_R32_SINT
, target
,
1211 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
1213 ctx
->fs_write_one_cbuf
=
1214 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1215 TGSI_INTERPOLATE_CONSTANT
, false);
1217 ctx
->fs_write_all_cbufs
=
1218 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1219 TGSI_INTERPOLATE_CONSTANT
, true);
1221 ctx
->cached_all_shaders
= true;
1224 static void blitter_set_common_draw_rect_state(struct blitter_context_priv
*ctx
,
1228 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1230 pipe
->bind_rasterizer_state(pipe
, scissor
? ctx
->rs_state_scissor
1233 bind_vs_layered(ctx
);
1235 bind_vs_passthrough(ctx
);
1237 if (ctx
->has_geometry_shader
)
1238 pipe
->bind_gs_state(pipe
, NULL
);
1239 if (ctx
->has_tessellation
) {
1240 pipe
->bind_tcs_state(pipe
, NULL
);
1241 pipe
->bind_tes_state(pipe
, NULL
);
1243 if (ctx
->has_stream_out
)
1244 pipe
->set_stream_output_targets(pipe
, 0, NULL
, NULL
);
1247 static void blitter_draw(struct blitter_context_priv
*ctx
,
1248 int x1
, int y1
, int x2
, int y2
, float depth
,
1249 unsigned num_instances
)
1251 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1252 struct pipe_vertex_buffer vb
= {0};
1254 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
1256 vb
.stride
= 8 * sizeof(float);
1258 u_upload_data(pipe
->stream_uploader
, 0, sizeof(ctx
->vertices
), 4, ctx
->vertices
,
1259 &vb
.buffer_offset
, &vb
.buffer
.resource
);
1260 if (!vb
.buffer
.resource
)
1262 u_upload_unmap(pipe
->stream_uploader
);
1264 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1265 util_draw_arrays_instanced(pipe
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
1267 pipe_resource_reference(&vb
.buffer
.resource
, NULL
);
1270 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
1271 int x1
, int y1
, int x2
, int y2
, float depth
,
1272 enum blitter_attrib_type type
,
1273 const union blitter_attrib
*attrib
)
1275 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1278 case UTIL_BLITTER_ATTRIB_COLOR
:
1279 blitter_set_clear_color(ctx
, attrib
->color
);
1282 case UTIL_BLITTER_ATTRIB_TEXCOORD
:
1283 set_texcoords_in_vertices(attrib
, &ctx
->vertices
[0][1][0], 8);
1289 blitter_draw(ctx
, x1
, y1
, x2
, y2
, depth
, 1);
1292 static void *get_clear_blend_state(struct blitter_context_priv
*ctx
,
1293 unsigned clear_buffers
)
1295 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1298 clear_buffers
&= PIPE_CLEAR_COLOR
;
1300 /* Return an existing blend state. */
1302 return ctx
->blend
[0][0];
1304 index
= GET_CLEAR_BLEND_STATE_IDX(clear_buffers
);
1306 if (ctx
->blend_clear
[index
])
1307 return ctx
->blend_clear
[index
];
1309 /* Create a new one. */
1311 struct pipe_blend_state blend
= {0};
1314 blend
.independent_blend_enable
= 1;
1316 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1317 if (clear_buffers
& (PIPE_CLEAR_COLOR0
<< i
)) {
1318 blend
.rt
[i
].colormask
= PIPE_MASK_RGBA
;
1322 ctx
->blend_clear
[index
] = pipe
->create_blend_state(pipe
, &blend
);
1324 return ctx
->blend_clear
[index
];
1327 void util_blitter_common_clear_setup(struct blitter_context
*blitter
,
1328 unsigned width
, unsigned height
,
1329 unsigned clear_buffers
,
1330 void *custom_blend
, void *custom_dsa
)
1332 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1333 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1335 util_blitter_set_running_flag(blitter
);
1336 blitter_check_saved_vertex_states(ctx
);
1337 blitter_check_saved_fragment_states(ctx
);
1338 blitter_disable_render_cond(ctx
);
1342 pipe
->bind_blend_state(pipe
, custom_blend
);
1344 pipe
->bind_blend_state(pipe
, get_clear_blend_state(ctx
, clear_buffers
));
1348 pipe
->bind_depth_stencil_alpha_state(pipe
, custom_dsa
);
1349 } else if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1350 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1351 } else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
1352 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1353 } else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
1354 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1356 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1359 pipe
->set_sample_mask(pipe
, ~0);
1360 blitter_set_dst_dimensions(ctx
, width
, height
);
1363 static void util_blitter_clear_custom(struct blitter_context
*blitter
,
1364 unsigned width
, unsigned height
,
1365 unsigned num_layers
,
1366 unsigned clear_buffers
,
1367 const union pipe_color_union
*color
,
1368 double depth
, unsigned stencil
,
1369 void *custom_blend
, void *custom_dsa
)
1371 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1372 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1373 struct pipe_stencil_ref sr
= { { 0 } };
1375 assert(ctx
->has_layered
|| num_layers
<= 1);
1377 util_blitter_common_clear_setup(blitter
, width
, height
, clear_buffers
,
1378 custom_blend
, custom_dsa
);
1380 sr
.ref_value
[0] = stencil
& 0xff;
1381 pipe
->set_stencil_ref(pipe
, &sr
);
1383 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1384 bind_fs_write_all_cbufs(ctx
);
1386 if (num_layers
> 1 && ctx
->has_layered
) {
1387 blitter_set_common_draw_rect_state(ctx
, false, true);
1388 blitter_set_clear_color(ctx
, color
->f
);
1389 blitter_draw(ctx
, 0, 0, width
, height
, depth
, num_layers
);
1392 union blitter_attrib attrib
;
1394 memcpy(attrib
.color
, color
->ui
, sizeof(color
->ui
));
1395 blitter_set_common_draw_rect_state(ctx
, false, false);
1396 blitter
->draw_rectangle(blitter
, 0, 0, width
, height
, (float) depth
,
1397 UTIL_BLITTER_ATTRIB_COLOR
, &attrib
);
1400 util_blitter_restore_vertex_states(blitter
);
1401 util_blitter_restore_fragment_states(blitter
);
1402 util_blitter_restore_render_cond(blitter
);
1403 util_blitter_unset_running_flag(blitter
);
1406 void util_blitter_clear(struct blitter_context
*blitter
,
1407 unsigned width
, unsigned height
, unsigned num_layers
,
1408 unsigned clear_buffers
,
1409 const union pipe_color_union
*color
,
1410 double depth
, unsigned stencil
)
1412 util_blitter_clear_custom(blitter
, width
, height
, num_layers
,
1413 clear_buffers
, color
, depth
, stencil
,
1417 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1418 unsigned width
, unsigned height
,
1419 double depth
, void *custom_dsa
)
1421 static const union pipe_color_union color
;
1422 util_blitter_clear_custom(blitter
, width
, height
, 0, 0, &color
, depth
, 0,
1426 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1427 struct pipe_resource
*dst
,
1431 memset(dst_templ
, 0, sizeof(*dst_templ
));
1432 dst_templ
->format
= util_format_linear(dst
->format
);
1433 dst_templ
->u
.tex
.level
= dstlevel
;
1434 dst_templ
->u
.tex
.first_layer
= dstz
;
1435 dst_templ
->u
.tex
.last_layer
= dstz
;
1438 static struct pipe_surface
*
1439 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
1440 struct pipe_surface
*surf
)
1442 struct pipe_surface dst_templ
;
1444 memset(&dst_templ
, 0, sizeof(dst_templ
));
1445 dst_templ
.format
= surf
->format
;
1446 dst_templ
.u
.tex
.level
= surf
->u
.tex
.level
;
1447 dst_templ
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
+ 1;
1448 dst_templ
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
+ 1;
1450 return pipe
->create_surface(pipe
, surf
->texture
, &dst_templ
);
1453 void util_blitter_default_src_texture(struct blitter_context
*blitter
,
1454 struct pipe_sampler_view
*src_templ
,
1455 struct pipe_resource
*src
,
1458 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1460 memset(src_templ
, 0, sizeof(*src_templ
));
1462 if (ctx
->cube_as_2darray
&&
1463 (src
->target
== PIPE_TEXTURE_CUBE
||
1464 src
->target
== PIPE_TEXTURE_CUBE_ARRAY
))
1465 src_templ
->target
= PIPE_TEXTURE_2D_ARRAY
;
1467 src_templ
->target
= src
->target
;
1469 src_templ
->format
= util_format_linear(src
->format
);
1470 src_templ
->u
.tex
.first_level
= srclevel
;
1471 src_templ
->u
.tex
.last_level
= srclevel
;
1472 src_templ
->u
.tex
.first_layer
= 0;
1473 src_templ
->u
.tex
.last_layer
=
1474 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1475 : src
->array_size
- 1;
1476 src_templ
->swizzle_r
= PIPE_SWIZZLE_X
;
1477 src_templ
->swizzle_g
= PIPE_SWIZZLE_Y
;
1478 src_templ
->swizzle_b
= PIPE_SWIZZLE_Z
;
1479 src_templ
->swizzle_a
= PIPE_SWIZZLE_W
;
1482 static bool is_blit_generic_supported(struct blitter_context
*blitter
,
1483 const struct pipe_resource
*dst
,
1484 enum pipe_format dst_format
,
1485 const struct pipe_resource
*src
,
1486 enum pipe_format src_format
,
1489 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1490 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1494 const struct util_format_description
*desc
=
1495 util_format_description(dst_format
);
1496 bool dst_has_stencil
= util_format_has_stencil(desc
);
1498 /* Stencil export must be supported for stencil copy. */
1499 if ((mask
& PIPE_MASK_S
) && dst_has_stencil
&&
1500 !ctx
->has_stencil_export
) {
1504 if (dst_has_stencil
|| util_format_has_depth(desc
))
1505 bind
= PIPE_BIND_DEPTH_STENCIL
;
1507 bind
= PIPE_BIND_RENDER_TARGET
;
1509 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1510 dst
->nr_samples
, bind
)) {
1516 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1520 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1521 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
)) {
1525 /* Check stencil sampler support for stencil copy. */
1526 if (mask
& PIPE_MASK_S
) {
1527 if (util_format_has_stencil(util_format_description(src_format
))) {
1528 enum pipe_format stencil_format
=
1529 util_format_stencil_only(src_format
);
1530 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1532 if (stencil_format
!= src_format
&&
1533 !screen
->is_format_supported(screen
, stencil_format
,
1534 src
->target
, src
->nr_samples
,
1535 PIPE_BIND_SAMPLER_VIEW
)) {
1545 bool util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1546 const struct pipe_resource
*dst
,
1547 const struct pipe_resource
*src
)
1549 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1550 src
, src
->format
, PIPE_MASK_RGBAZS
);
1553 bool util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1554 const struct pipe_blit_info
*info
)
1556 return is_blit_generic_supported(blitter
,
1557 info
->dst
.resource
, info
->dst
.format
,
1558 info
->src
.resource
, info
->src
.format
,
1562 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1563 struct pipe_resource
*dst
,
1565 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1566 struct pipe_resource
*src
,
1568 const struct pipe_box
*srcbox
)
1570 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1571 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1572 struct pipe_surface
*dst_view
, dst_templ
;
1573 struct pipe_sampler_view src_templ
, *src_view
;
1574 struct pipe_box dstbox
;
1577 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1579 u_box_3d(dstx
, dsty
, dstz
, abs(srcbox
->width
), abs(srcbox
->height
),
1580 abs(srcbox
->depth
), &dstbox
);
1582 /* Initialize the surface. */
1583 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1584 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1586 /* Initialize the sampler view. */
1587 util_blitter_default_src_texture(blitter
, &src_templ
, src
, src_level
);
1588 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1591 util_blitter_blit_generic(blitter
, dst_view
, &dstbox
,
1592 src_view
, srcbox
, src
->width0
, src
->height0
,
1593 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
,
1596 pipe_surface_reference(&dst_view
, NULL
);
1597 pipe_sampler_view_reference(&src_view
, NULL
);
1600 static void do_blits(struct blitter_context_priv
*ctx
,
1601 struct pipe_surface
*dst
,
1602 const struct pipe_box
*dstbox
,
1603 struct pipe_sampler_view
*src
,
1604 unsigned src_width0
,
1605 unsigned src_height0
,
1606 const struct pipe_box
*srcbox
,
1610 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1611 unsigned src_samples
= src
->texture
->nr_samples
;
1612 unsigned dst_samples
= dst
->texture
->nr_samples
;
1613 enum pipe_texture_target src_target
= src
->target
;
1614 struct pipe_framebuffer_state fb_state
= {0};
1616 /* Initialize framebuffer state. */
1617 fb_state
.width
= dst
->width
;
1618 fb_state
.height
= dst
->height
;
1619 fb_state
.nr_cbufs
= is_zsbuf
? 0 : 1;
1621 blitter_set_dst_dimensions(ctx
, fb_state
.width
, fb_state
.height
);
1623 if ((src_target
== PIPE_TEXTURE_1D
||
1624 src_target
== PIPE_TEXTURE_2D
||
1625 src_target
== PIPE_TEXTURE_RECT
) &&
1627 /* Draw the quad with the draw_rectangle callback. */
1629 /* Set texture coordinates. */
1630 union blitter_attrib coord
;
1631 get_texcoords(src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1632 srcbox
->x
+srcbox
->width
, srcbox
->y
+srcbox
->height
,
1635 /* Set framebuffer state. */
1637 fb_state
.zsbuf
= dst
;
1639 fb_state
.cbufs
[0] = dst
;
1641 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1644 pipe
->set_sample_mask(pipe
, ~0);
1645 ctx
->base
.draw_rectangle(&ctx
->base
, dstbox
->x
, dstbox
->y
,
1646 dstbox
->x
+ dstbox
->width
,
1647 dstbox
->y
+ dstbox
->height
, 0,
1648 UTIL_BLITTER_ATTRIB_TEXCOORD
, &coord
);
1650 /* Draw the quad with the generic codepath. */
1652 for (dst_z
= 0; dst_z
< dstbox
->depth
; dst_z
++) {
1653 struct pipe_surface
*old
;
1654 float dst2src_scale
= srcbox
->depth
/ (float)dstbox
->depth
;
1656 /* Scale Z properly if the blit is scaled.
1658 * When downscaling, we want the coordinates centered, so that
1659 * mipmapping works for 3D textures. For example, when generating
1660 * a 4x4x4 level, this wouldn't average the pixels:
1662 * src Z: 0 1 2 3 4 5 6 7
1665 * Because the pixels are not centered below the pixels of the higher
1666 * level. Therefore, we want this:
1667 * src Z: 0 1 2 3 4 5 6 7
1670 * dst_offset defines the offset needed for centering the pixels and
1671 * it works with any scaling (not just 2x).
1673 float dst_offset
= ((srcbox
->depth
- 1) -
1674 (dstbox
->depth
- 1) * dst2src_scale
) * 0.5;
1675 float src_z
= (dst_z
+ dst_offset
) * dst2src_scale
;
1677 /* Set framebuffer state. */
1679 fb_state
.zsbuf
= dst
;
1681 fb_state
.cbufs
[0] = dst
;
1683 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1685 /* See if we need to blit a multisample or singlesample buffer. */
1686 if (src_samples
== dst_samples
&& dst_samples
> 1) {
1688 unsigned i
, max_sample
= dst_samples
- 1;
1690 for (i
= 0; i
<= max_sample
; i
++) {
1691 pipe
->set_sample_mask(pipe
, 1 << i
);
1692 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1694 i
, srcbox
->x
, srcbox
->y
,
1695 srcbox
->x
+ srcbox
->width
,
1696 srcbox
->y
+ srcbox
->height
, uses_txf
);
1697 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1698 dstbox
->x
+ dstbox
->width
,
1699 dstbox
->y
+ dstbox
->height
, 0, 1);
1702 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1703 pipe
->set_sample_mask(pipe
, ~0);
1704 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1705 srcbox
->z
+ src_z
, 0,
1706 srcbox
->x
, srcbox
->y
,
1707 srcbox
->x
+ srcbox
->width
,
1708 srcbox
->y
+ srcbox
->height
, uses_txf
);
1709 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1710 dstbox
->x
+ dstbox
->width
,
1711 dstbox
->y
+ dstbox
->height
, 0, 1);
1714 /* Get the next surface or (if this is the last iteration)
1715 * just unreference the last one. */
1717 if (dst_z
< dstbox
->depth
-1) {
1718 dst
= util_blitter_get_next_surface_layer(ctx
->base
.pipe
, dst
);
1721 pipe_surface_reference(&old
, NULL
);
1727 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1728 struct pipe_surface
*dst
,
1729 const struct pipe_box
*dstbox
,
1730 struct pipe_sampler_view
*src
,
1731 const struct pipe_box
*srcbox
,
1732 unsigned src_width0
, unsigned src_height0
,
1733 unsigned mask
, unsigned filter
,
1734 const struct pipe_scissor_state
*scissor
,
1737 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1738 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1739 enum pipe_texture_target src_target
= src
->target
;
1740 unsigned src_samples
= src
->texture
->nr_samples
;
1741 unsigned dst_samples
= dst
->texture
->nr_samples
;
1742 bool has_depth
, has_stencil
, has_color
;
1743 bool blit_stencil
, blit_depth
, blit_color
;
1744 void *sampler_state
;
1745 const struct util_format_description
*src_desc
=
1746 util_format_description(src
->format
);
1747 const struct util_format_description
*dst_desc
=
1748 util_format_description(dst
->format
);
1750 has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
&&
1751 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1752 has_depth
= util_format_has_depth(src_desc
) &&
1753 util_format_has_depth(dst_desc
);
1754 has_stencil
= util_format_has_stencil(src_desc
) &&
1755 util_format_has_stencil(dst_desc
);
1757 blit_color
= has_color
&& (mask
& PIPE_MASK_RGBA
);
1758 blit_depth
= has_depth
&& (mask
& PIPE_MASK_Z
);
1759 blit_stencil
= has_stencil
&& (mask
& PIPE_MASK_S
) &&
1760 ctx
->has_stencil_export
;
1762 if (!blit_stencil
&& !blit_depth
&& !blit_color
) {
1766 bool is_scaled
= dstbox
->width
!= abs(srcbox
->width
) ||
1767 dstbox
->height
!= abs(srcbox
->height
);
1769 if (blit_stencil
|| !is_scaled
)
1770 filter
= PIPE_TEX_FILTER_NEAREST
;
1772 bool use_txf
= false;
1774 /* Don't support scaled blits. The TXF shader uses F2I for rounding. */
1777 filter
== PIPE_TEX_FILTER_NEAREST
&&
1778 src
->target
!= PIPE_TEXTURE_CUBE
&&
1779 src
->target
!= PIPE_TEXTURE_CUBE_ARRAY
) {
1780 int src_width
= u_minify(src_width0
, src
->u
.tex
.first_level
);
1781 int src_height
= u_minify(src_height0
, src
->u
.tex
.first_level
);
1782 int src_depth
= src
->u
.tex
.last_layer
+ 1;
1783 struct pipe_box box
= *srcbox
;
1785 /* Eliminate negative width/height/depth. */
1786 if (box
.width
< 0) {
1790 if (box
.height
< 0) {
1791 box
.y
+= box
.height
;
1794 if (box
.depth
< 0) {
1799 /* See if srcbox is in bounds. TXF doesn't clamp the coordinates. */
1801 box
.x
>= 0 && box
.x
< src_width
&&
1802 box
.y
>= 0 && box
.y
< src_height
&&
1803 box
.z
>= 0 && box
.z
< src_depth
&&
1804 box
.x
+ box
.width
> 0 && box
.x
+ box
.width
<= src_width
&&
1805 box
.y
+ box
.height
> 0 && box
.y
+ box
.height
<= src_height
&&
1806 box
.z
+ box
.depth
> 0 && box
.z
+ box
.depth
<= src_depth
;
1809 /* Check whether the states are properly saved. */
1810 util_blitter_set_running_flag(blitter
);
1811 blitter_check_saved_vertex_states(ctx
);
1812 blitter_check_saved_fragment_states(ctx
);
1813 blitter_check_saved_textures(ctx
);
1814 blitter_check_saved_fb_state(ctx
);
1815 blitter_disable_render_cond(ctx
);
1817 if (blit_depth
|| blit_stencil
) {
1818 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
1820 if (blit_depth
&& blit_stencil
) {
1821 pipe
->bind_depth_stencil_alpha_state(pipe
,
1822 ctx
->dsa_write_depth_stencil
);
1823 ctx
->bind_fs_state(pipe
,
1824 blitter_get_fs_texfetch_depthstencil(ctx
, src_target
,
1825 src_samples
, use_txf
));
1826 } else if (blit_depth
) {
1827 pipe
->bind_depth_stencil_alpha_state(pipe
,
1828 ctx
->dsa_write_depth_keep_stencil
);
1829 ctx
->bind_fs_state(pipe
,
1830 blitter_get_fs_texfetch_depth(ctx
, src_target
,
1831 src_samples
, use_txf
));
1832 } else { /* is_stencil */
1833 pipe
->bind_depth_stencil_alpha_state(pipe
,
1834 ctx
->dsa_keep_depth_write_stencil
);
1835 ctx
->bind_fs_state(pipe
,
1836 blitter_get_fs_texfetch_stencil(ctx
, src_target
,
1837 src_samples
, use_txf
));
1841 unsigned colormask
= mask
& PIPE_MASK_RGBA
;
1843 pipe
->bind_blend_state(pipe
, ctx
->blend
[colormask
][alpha_blend
]);
1844 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1845 ctx
->bind_fs_state(pipe
,
1846 blitter_get_fs_texfetch_col(ctx
, src
->format
, dst
->format
, src_target
,
1847 src_samples
, dst_samples
, filter
,
1851 /* Set the linear filter only for scaled color non-MSAA blits. */
1852 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
1853 if (src_target
== PIPE_TEXTURE_RECT
) {
1854 sampler_state
= ctx
->sampler_state_rect_linear
;
1856 sampler_state
= ctx
->sampler_state_linear
;
1859 if (src_target
== PIPE_TEXTURE_RECT
) {
1860 sampler_state
= ctx
->sampler_state_rect
;
1862 sampler_state
= ctx
->sampler_state
;
1867 if (blit_depth
&& blit_stencil
) {
1868 /* Setup two samplers, one for depth and the other one for stencil. */
1869 struct pipe_sampler_view templ
;
1870 struct pipe_sampler_view
*views
[2];
1871 void *samplers
[2] = {sampler_state
, sampler_state
};
1874 templ
.format
= util_format_stencil_only(templ
.format
);
1875 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1878 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1880 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, views
);
1881 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, samplers
);
1883 pipe_sampler_view_reference(&views
[1], NULL
);
1884 } else if (blit_stencil
) {
1885 /* Set a stencil-only sampler view for it not to sample depth instead. */
1886 struct pipe_sampler_view templ
;
1887 struct pipe_sampler_view
*view
;
1890 templ
.format
= util_format_stencil_only(templ
.format
);
1891 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1893 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1895 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &view
);
1896 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1897 0, 1, &sampler_state
);
1899 pipe_sampler_view_reference(&view
, NULL
);
1901 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src
);
1902 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1903 0, 1, &sampler_state
);
1906 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1908 pipe
->set_scissor_states(pipe
, 0, 1, scissor
);
1911 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
, false);
1913 do_blits(ctx
, dst
, dstbox
, src
, src_width0
, src_height0
,
1914 srcbox
, blit_depth
|| blit_stencil
, use_txf
);
1916 util_blitter_restore_vertex_states(blitter
);
1917 util_blitter_restore_fragment_states(blitter
);
1918 util_blitter_restore_textures(blitter
);
1919 util_blitter_restore_fb_state(blitter
);
1921 pipe
->set_scissor_states(pipe
, 0, 1, &ctx
->base
.saved_scissor
);
1923 util_blitter_restore_render_cond(blitter
);
1924 util_blitter_unset_running_flag(blitter
);
1928 util_blitter_blit(struct blitter_context
*blitter
,
1929 const struct pipe_blit_info
*info
)
1931 struct pipe_resource
*dst
= info
->dst
.resource
;
1932 struct pipe_resource
*src
= info
->src
.resource
;
1933 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1934 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1935 struct pipe_surface
*dst_view
, dst_templ
;
1936 struct pipe_sampler_view src_templ
, *src_view
;
1938 /* Initialize the surface. */
1939 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
1941 dst_templ
.format
= info
->dst
.format
;
1942 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1944 /* Initialize the sampler view. */
1945 util_blitter_default_src_texture(blitter
, &src_templ
, src
, info
->src
.level
);
1946 src_templ
.format
= info
->src
.format
;
1947 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1950 util_blitter_blit_generic(blitter
, dst_view
, &info
->dst
.box
,
1951 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
1952 info
->mask
, info
->filter
,
1953 info
->scissor_enable
? &info
->scissor
: NULL
,
1956 pipe_surface_reference(&dst_view
, NULL
);
1957 pipe_sampler_view_reference(&src_view
, NULL
);
1960 void util_blitter_generate_mipmap(struct blitter_context
*blitter
,
1961 struct pipe_resource
*tex
,
1962 enum pipe_format format
,
1963 unsigned base_level
, unsigned last_level
,
1964 unsigned first_layer
, unsigned last_layer
)
1966 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1967 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1968 struct pipe_surface dst_templ
, *dst_view
;
1969 struct pipe_sampler_view src_templ
, *src_view
;
1971 void *sampler_state
;
1972 const struct util_format_description
*desc
=
1973 util_format_description(format
);
1975 unsigned target
= tex
->target
;
1977 if (ctx
->cube_as_2darray
&&
1978 (target
== PIPE_TEXTURE_CUBE
|| target
== PIPE_TEXTURE_CUBE_ARRAY
))
1979 target
= PIPE_TEXTURE_2D_ARRAY
;
1981 assert(tex
->nr_samples
<= 1);
1982 assert(!util_format_has_stencil(desc
));
1984 is_depth
= desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
;
1986 /* Check whether the states are properly saved. */
1987 util_blitter_set_running_flag(blitter
);
1988 blitter_check_saved_vertex_states(ctx
);
1989 blitter_check_saved_fragment_states(ctx
);
1990 blitter_check_saved_textures(ctx
);
1991 blitter_check_saved_fb_state(ctx
);
1992 blitter_disable_render_cond(ctx
);
1996 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
1997 pipe
->bind_depth_stencil_alpha_state(pipe
,
1998 ctx
->dsa_write_depth_keep_stencil
);
1999 ctx
->bind_fs_state(pipe
,
2000 blitter_get_fs_texfetch_depth(ctx
, target
, 1, false));
2002 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2003 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2004 ctx
->bind_fs_state(pipe
,
2005 blitter_get_fs_texfetch_col(ctx
, tex
->format
, tex
->format
, target
,
2006 1, 1, PIPE_TEX_FILTER_LINEAR
, false));
2009 if (target
== PIPE_TEXTURE_RECT
) {
2010 sampler_state
= ctx
->sampler_state_rect_linear
;
2012 sampler_state
= ctx
->sampler_state_linear
;
2014 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
2015 0, 1, &sampler_state
);
2017 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2018 blitter_set_common_draw_rect_state(ctx
, false, false);
2020 for (src_level
= base_level
; src_level
< last_level
; src_level
++) {
2021 struct pipe_box dstbox
= {0}, srcbox
= {0};
2022 unsigned dst_level
= src_level
+ 1;
2024 dstbox
.width
= u_minify(tex
->width0
, dst_level
);
2025 dstbox
.height
= u_minify(tex
->height0
, dst_level
);
2027 srcbox
.width
= u_minify(tex
->width0
, src_level
);
2028 srcbox
.height
= u_minify(tex
->height0
, src_level
);
2030 if (target
== PIPE_TEXTURE_3D
) {
2031 dstbox
.depth
= util_max_layer(tex
, dst_level
) + 1;
2032 srcbox
.depth
= util_max_layer(tex
, src_level
) + 1;
2034 dstbox
.z
= srcbox
.z
= first_layer
;
2035 dstbox
.depth
= srcbox
.depth
= last_layer
- first_layer
+ 1;
2038 /* Initialize the surface. */
2039 util_blitter_default_dst_texture(&dst_templ
, tex
, dst_level
,
2041 dst_templ
.format
= format
;
2042 dst_view
= pipe
->create_surface(pipe
, tex
, &dst_templ
);
2044 /* Initialize the sampler view. */
2045 util_blitter_default_src_texture(blitter
, &src_templ
, tex
, src_level
);
2046 src_templ
.format
= format
;
2047 src_view
= pipe
->create_sampler_view(pipe
, tex
, &src_templ
);
2049 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src_view
);
2051 do_blits(ctx
, dst_view
, &dstbox
, src_view
, tex
->width0
, tex
->height0
,
2052 &srcbox
, is_depth
, false);
2054 pipe_surface_reference(&dst_view
, NULL
);
2055 pipe_sampler_view_reference(&src_view
, NULL
);
2058 util_blitter_restore_vertex_states(blitter
);
2059 util_blitter_restore_fragment_states(blitter
);
2060 util_blitter_restore_textures(blitter
);
2061 util_blitter_restore_fb_state(blitter
);
2062 util_blitter_restore_render_cond(blitter
);
2063 util_blitter_unset_running_flag(blitter
);
2066 /* Clear a region of a color surface to a constant value. */
2067 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
2068 struct pipe_surface
*dstsurf
,
2069 const union pipe_color_union
*color
,
2070 unsigned dstx
, unsigned dsty
,
2071 unsigned width
, unsigned height
)
2073 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2074 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2075 struct pipe_framebuffer_state fb_state
;
2076 unsigned num_layers
;
2078 assert(dstsurf
->texture
);
2079 if (!dstsurf
->texture
)
2082 /* check the saved state */
2083 util_blitter_set_running_flag(blitter
);
2084 blitter_check_saved_vertex_states(ctx
);
2085 blitter_check_saved_fragment_states(ctx
);
2086 blitter_check_saved_fb_state(ctx
);
2087 blitter_disable_render_cond(ctx
);
2090 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2091 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2092 bind_fs_write_one_cbuf(ctx
);
2093 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2095 /* set a framebuffer state */
2096 fb_state
.width
= dstsurf
->width
;
2097 fb_state
.height
= dstsurf
->height
;
2098 fb_state
.nr_cbufs
= 1;
2099 fb_state
.cbufs
[0] = dstsurf
;
2101 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2102 pipe
->set_sample_mask(pipe
, ~0);
2104 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2106 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
2107 if (num_layers
> 1 && ctx
->has_layered
) {
2108 blitter_set_common_draw_rect_state(ctx
, false, true);
2109 blitter_set_clear_color(ctx
, color
->f
);
2110 blitter_draw(ctx
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0, num_layers
);
2113 union blitter_attrib attrib
;
2115 memcpy(attrib
.color
, color
->ui
, sizeof(color
->ui
));
2116 blitter_set_common_draw_rect_state(ctx
, false, false);
2117 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
2118 UTIL_BLITTER_ATTRIB_COLOR
, &attrib
);
2121 util_blitter_restore_vertex_states(blitter
);
2122 util_blitter_restore_fragment_states(blitter
);
2123 util_blitter_restore_fb_state(blitter
);
2124 util_blitter_restore_render_cond(blitter
);
2125 util_blitter_unset_running_flag(blitter
);
2128 /* Clear a region of a depth stencil surface. */
2129 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
2130 struct pipe_surface
*dstsurf
,
2131 unsigned clear_flags
,
2134 unsigned dstx
, unsigned dsty
,
2135 unsigned width
, unsigned height
)
2137 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2138 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2139 struct pipe_framebuffer_state fb_state
;
2140 struct pipe_stencil_ref sr
= { { 0 } };
2141 unsigned num_layers
;
2143 assert(dstsurf
->texture
);
2144 if (!dstsurf
->texture
)
2147 /* check the saved state */
2148 util_blitter_set_running_flag(blitter
);
2149 blitter_check_saved_vertex_states(ctx
);
2150 blitter_check_saved_fragment_states(ctx
);
2151 blitter_check_saved_fb_state(ctx
);
2152 blitter_disable_render_cond(ctx
);
2155 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
2156 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
2157 sr
.ref_value
[0] = stencil
& 0xff;
2158 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
2159 pipe
->set_stencil_ref(pipe
, &sr
);
2161 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
2162 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
2164 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
2165 sr
.ref_value
[0] = stencil
& 0xff;
2166 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
2167 pipe
->set_stencil_ref(pipe
, &sr
);
2170 /* hmm that should be illegal probably, or make it a no-op somewhere */
2171 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2174 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2176 /* set a framebuffer state */
2177 fb_state
.width
= dstsurf
->width
;
2178 fb_state
.height
= dstsurf
->height
;
2179 fb_state
.nr_cbufs
= 0;
2180 fb_state
.cbufs
[0] = 0;
2181 fb_state
.zsbuf
= dstsurf
;
2182 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2183 pipe
->set_sample_mask(pipe
, ~0);
2185 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2187 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
2188 if (num_layers
> 1 && ctx
->has_layered
) {
2189 blitter_set_common_draw_rect_state(ctx
, false, true);
2190 blitter_draw(ctx
, dstx
, dsty
, dstx
+width
, dsty
+height
, (float) depth
, num_layers
);
2193 blitter_set_common_draw_rect_state(ctx
, false, false);
2194 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
,
2196 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2199 util_blitter_restore_vertex_states(blitter
);
2200 util_blitter_restore_fragment_states(blitter
);
2201 util_blitter_restore_fb_state(blitter
);
2202 util_blitter_restore_render_cond(blitter
);
2203 util_blitter_unset_running_flag(blitter
);
2206 /* draw a rectangle across a region using a custom dsa stage - for r600g */
2207 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
2208 struct pipe_surface
*zsurf
,
2209 struct pipe_surface
*cbsurf
,
2210 unsigned sample_mask
,
2211 void *dsa_stage
, float depth
)
2213 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2214 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2215 struct pipe_framebuffer_state fb_state
;
2217 assert(zsurf
->texture
);
2218 if (!zsurf
->texture
)
2221 /* check the saved state */
2222 util_blitter_set_running_flag(blitter
);
2223 blitter_check_saved_vertex_states(ctx
);
2224 blitter_check_saved_fragment_states(ctx
);
2225 blitter_check_saved_fb_state(ctx
);
2226 blitter_disable_render_cond(ctx
);
2229 pipe
->bind_blend_state(pipe
, cbsurf
? ctx
->blend
[PIPE_MASK_RGBA
][0] :
2231 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
2233 bind_fs_write_one_cbuf(ctx
);
2236 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2238 /* set a framebuffer state */
2239 fb_state
.width
= zsurf
->width
;
2240 fb_state
.height
= zsurf
->height
;
2241 fb_state
.nr_cbufs
= 1;
2243 fb_state
.cbufs
[0] = cbsurf
;
2244 fb_state
.nr_cbufs
= 1;
2246 fb_state
.cbufs
[0] = NULL
;
2247 fb_state
.nr_cbufs
= 0;
2249 fb_state
.zsbuf
= zsurf
;
2250 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2251 pipe
->set_sample_mask(pipe
, sample_mask
);
2253 blitter_set_common_draw_rect_state(ctx
, false, false);
2254 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
2255 blitter
->draw_rectangle(blitter
, 0, 0, zsurf
->width
, zsurf
->height
, depth
,
2256 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2258 util_blitter_restore_vertex_states(blitter
);
2259 util_blitter_restore_fragment_states(blitter
);
2260 util_blitter_restore_fb_state(blitter
);
2261 util_blitter_restore_render_cond(blitter
);
2262 util_blitter_unset_running_flag(blitter
);
2265 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
2266 struct pipe_resource
*dst
,
2268 struct pipe_resource
*src
,
2272 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2273 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2274 struct pipe_vertex_buffer vb
;
2275 struct pipe_stream_output_target
*so_target
;
2276 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2278 if (srcx
>= src
->width0
||
2279 dstx
>= dst
->width0
) {
2282 if (srcx
+ size
> src
->width0
) {
2283 size
= src
->width0
- srcx
;
2285 if (dstx
+ size
> dst
->width0
) {
2286 size
= dst
->width0
- dstx
;
2289 /* Drivers not capable of Stream Out should not call this function
2290 * in the first place. */
2291 assert(ctx
->has_stream_out
);
2293 /* Some alignment is required. */
2294 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
2295 !ctx
->has_stream_out
) {
2296 struct pipe_box box
;
2297 u_box_1d(srcx
, size
, &box
);
2298 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
2302 util_blitter_set_running_flag(blitter
);
2303 blitter_check_saved_vertex_states(ctx
);
2304 blitter_disable_render_cond(ctx
);
2306 vb
.is_user_buffer
= false;
2307 vb
.buffer
.resource
= src
;
2308 vb
.buffer_offset
= srcx
;
2311 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2312 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[0]);
2313 bind_vs_pos_only(ctx
, 1);
2314 if (ctx
->has_geometry_shader
)
2315 pipe
->bind_gs_state(pipe
, NULL
);
2316 if (ctx
->has_tessellation
) {
2317 pipe
->bind_tcs_state(pipe
, NULL
);
2318 pipe
->bind_tes_state(pipe
, NULL
);
2320 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2322 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
2323 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2325 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2327 util_blitter_restore_vertex_states(blitter
);
2328 util_blitter_restore_render_cond(blitter
);
2329 util_blitter_unset_running_flag(blitter
);
2330 pipe_so_target_reference(&so_target
, NULL
);
2333 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
2334 struct pipe_resource
*dst
,
2335 unsigned offset
, unsigned size
,
2336 unsigned num_channels
,
2337 const union pipe_color_union
*clear_value
)
2339 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2340 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2341 struct pipe_vertex_buffer vb
= {0};
2342 struct pipe_stream_output_target
*so_target
= NULL
;
2343 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2345 assert(num_channels
>= 1);
2346 assert(num_channels
<= 4);
2348 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
2350 * R600 uses this to initialize texture resources, so width0 might not be
2351 * what you think it is.
2354 /* Streamout is required. */
2355 if (!ctx
->has_stream_out
) {
2356 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2360 /* Some alignment is required. */
2361 if (offset
% 4 != 0 || size
% 4 != 0) {
2362 assert(!"Bad alignment in util_blitter_clear_buffer()");
2366 u_upload_data(pipe
->stream_uploader
, 0, num_channels
*4, 4, clear_value
,
2367 &vb
.buffer_offset
, &vb
.buffer
.resource
);
2368 if (!vb
.buffer
.resource
)
2373 util_blitter_set_running_flag(blitter
);
2374 blitter_check_saved_vertex_states(ctx
);
2375 blitter_disable_render_cond(ctx
);
2377 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2378 pipe
->bind_vertex_elements_state(pipe
,
2379 ctx
->velem_state_readbuf
[num_channels
-1]);
2380 bind_vs_pos_only(ctx
, num_channels
);
2381 if (ctx
->has_geometry_shader
)
2382 pipe
->bind_gs_state(pipe
, NULL
);
2383 if (ctx
->has_tessellation
) {
2384 pipe
->bind_tcs_state(pipe
, NULL
);
2385 pipe
->bind_tes_state(pipe
, NULL
);
2387 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2389 so_target
= pipe
->create_stream_output_target(pipe
, dst
, offset
, size
);
2390 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2392 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2395 util_blitter_restore_vertex_states(blitter
);
2396 util_blitter_restore_render_cond(blitter
);
2397 util_blitter_unset_running_flag(blitter
);
2398 pipe_so_target_reference(&so_target
, NULL
);
2399 pipe_resource_reference(&vb
.buffer
.resource
, NULL
);
2402 /* probably radeon specific */
2403 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
2404 struct pipe_resource
*dst
,
2407 struct pipe_resource
*src
,
2409 unsigned sample_mask
,
2411 enum pipe_format format
)
2413 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2414 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2415 struct pipe_framebuffer_state fb_state
;
2416 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
2418 util_blitter_set_running_flag(blitter
);
2419 blitter_check_saved_vertex_states(ctx
);
2420 blitter_check_saved_fragment_states(ctx
);
2421 blitter_disable_render_cond(ctx
);
2424 pipe
->bind_blend_state(pipe
, custom_blend
);
2425 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2426 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2427 bind_fs_write_one_cbuf(ctx
);
2428 pipe
->set_sample_mask(pipe
, sample_mask
);
2430 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
2431 surf_tmpl
.format
= format
;
2432 surf_tmpl
.u
.tex
.level
= dst_level
;
2433 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
2434 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
2436 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
2438 surf_tmpl
.u
.tex
.level
= 0;
2439 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
2440 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
2442 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
2444 /* set a framebuffer state */
2445 fb_state
.width
= src
->width0
;
2446 fb_state
.height
= src
->height0
;
2447 fb_state
.nr_cbufs
= 2;
2448 fb_state
.cbufs
[0] = srcsurf
;
2449 fb_state
.cbufs
[1] = dstsurf
;
2450 fb_state
.zsbuf
= NULL
;
2451 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2453 blitter_set_common_draw_rect_state(ctx
, false, false);
2454 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
2455 blitter
->draw_rectangle(blitter
, 0, 0, src
->width0
, src
->height0
,
2457 util_blitter_restore_fb_state(blitter
);
2458 util_blitter_restore_vertex_states(blitter
);
2459 util_blitter_restore_fragment_states(blitter
);
2460 util_blitter_restore_render_cond(blitter
);
2461 util_blitter_unset_running_flag(blitter
);
2463 pipe_surface_reference(&srcsurf
, NULL
);
2464 pipe_surface_reference(&dstsurf
, NULL
);
2467 void util_blitter_custom_color(struct blitter_context
*blitter
,
2468 struct pipe_surface
*dstsurf
,
2471 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2472 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2473 struct pipe_framebuffer_state fb_state
;
2475 assert(dstsurf
->texture
);
2476 if (!dstsurf
->texture
)
2479 /* check the saved state */
2480 util_blitter_set_running_flag(blitter
);
2481 blitter_check_saved_vertex_states(ctx
);
2482 blitter_check_saved_fragment_states(ctx
);
2483 blitter_check_saved_fb_state(ctx
);
2484 blitter_disable_render_cond(ctx
);
2487 pipe
->bind_blend_state(pipe
, custom_blend
? custom_blend
2488 : ctx
->blend
[PIPE_MASK_RGBA
][0]);
2489 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2490 bind_fs_write_one_cbuf(ctx
);
2491 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2492 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2494 /* set a framebuffer state */
2495 fb_state
.width
= dstsurf
->width
;
2496 fb_state
.height
= dstsurf
->height
;
2497 fb_state
.nr_cbufs
= 1;
2498 fb_state
.cbufs
[0] = dstsurf
;
2500 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2501 pipe
->set_sample_mask(pipe
, ~0);
2503 blitter_set_common_draw_rect_state(ctx
, false, false);
2504 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2505 blitter
->draw_rectangle(blitter
, 0, 0, dstsurf
->width
, dstsurf
->height
,
2508 util_blitter_restore_vertex_states(blitter
);
2509 util_blitter_restore_fragment_states(blitter
);
2510 util_blitter_restore_fb_state(blitter
);
2511 util_blitter_restore_render_cond(blitter
);
2512 util_blitter_unset_running_flag(blitter
);