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 boolean has_geometry_shader
;
134 boolean has_tessellation
;
136 boolean has_stream_out
;
137 boolean has_stencil_export
;
138 boolean has_texture_multisample
;
141 boolean cube_as_2darray
;
142 boolean 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 static struct pipe_surface
*
151 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
152 struct pipe_surface
*surf
);
154 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
)
156 struct blitter_context_priv
*ctx
;
157 struct pipe_blend_state blend
;
158 struct pipe_depth_stencil_alpha_state dsa
;
159 struct pipe_rasterizer_state rs_state
;
160 struct pipe_sampler_state sampler_state
;
161 struct pipe_vertex_element velem
[2];
164 ctx
= CALLOC_STRUCT(blitter_context_priv
);
168 ctx
->base
.pipe
= pipe
;
169 ctx
->base
.draw_rectangle
= util_blitter_draw_rectangle
;
170 ctx
->base
.get_next_surface_layer
= util_blitter_get_next_surface_layer
;
172 ctx
->bind_fs_state
= pipe
->bind_fs_state
;
173 ctx
->delete_fs_state
= pipe
->delete_fs_state
;
175 /* init state objects for them to be considered invalid */
176 ctx
->base
.saved_blend_state
= INVALID_PTR
;
177 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
178 ctx
->base
.saved_rs_state
= INVALID_PTR
;
179 ctx
->base
.saved_fs
= INVALID_PTR
;
180 ctx
->base
.saved_vs
= INVALID_PTR
;
181 ctx
->base
.saved_gs
= INVALID_PTR
;
182 ctx
->base
.saved_velem_state
= INVALID_PTR
;
183 ctx
->base
.saved_fb_state
.nr_cbufs
= ~0;
184 ctx
->base
.saved_num_sampler_views
= ~0;
185 ctx
->base
.saved_num_sampler_states
= ~0;
186 ctx
->base
.saved_num_so_targets
= ~0;
188 ctx
->has_geometry_shader
=
189 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
190 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
192 ctx
->has_tessellation
=
193 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_TESS_CTRL
,
194 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
196 ctx
->has_stream_out
=
197 pipe
->screen
->get_param(pipe
->screen
,
198 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0;
200 ctx
->has_stencil_export
=
201 pipe
->screen
->get_param(pipe
->screen
,
202 PIPE_CAP_SHADER_STENCIL_EXPORT
);
204 ctx
->has_texture_multisample
=
205 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TEXTURE_MULTISAMPLE
);
207 ctx
->has_tex_lz
= pipe
->screen
->get_param(pipe
->screen
,
208 PIPE_CAP_TGSI_TEX_TXF_LZ
);
209 ctx
->has_txf
= pipe
->screen
->get_param(pipe
->screen
,
210 PIPE_CAP_GLSL_FEATURE_LEVEL
) > 130;
211 ctx
->cube_as_2darray
= pipe
->screen
->get_param(pipe
->screen
,
212 PIPE_CAP_SAMPLER_VIEW_TARGET
);
214 /* blend state objects */
215 memset(&blend
, 0, sizeof(blend
));
217 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
218 for (j
= 0; j
< 2; j
++) {
219 memset(&blend
.rt
[0], 0, sizeof(blend
.rt
[0]));
220 blend
.rt
[0].colormask
= i
;
222 blend
.rt
[0].blend_enable
= 1;
223 blend
.rt
[0].rgb_func
= PIPE_BLEND_ADD
;
224 blend
.rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_SRC_ALPHA
;
225 blend
.rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
226 blend
.rt
[0].alpha_func
= PIPE_BLEND_ADD
;
227 blend
.rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_SRC_ALPHA
;
228 blend
.rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
230 ctx
->blend
[i
][j
] = pipe
->create_blend_state(pipe
, &blend
);
234 /* depth stencil alpha state objects */
235 memset(&dsa
, 0, sizeof(dsa
));
236 ctx
->dsa_keep_depth_stencil
=
237 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
239 dsa
.depth
.enabled
= 1;
240 dsa
.depth
.writemask
= 1;
241 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
242 ctx
->dsa_write_depth_keep_stencil
=
243 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
245 dsa
.stencil
[0].enabled
= 1;
246 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
247 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
248 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
249 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
250 dsa
.stencil
[0].valuemask
= 0xff;
251 dsa
.stencil
[0].writemask
= 0xff;
252 ctx
->dsa_write_depth_stencil
=
253 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
255 dsa
.depth
.enabled
= 0;
256 dsa
.depth
.writemask
= 0;
257 ctx
->dsa_keep_depth_write_stencil
=
258 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
261 memset(&sampler_state
, 0, sizeof(sampler_state
));
262 sampler_state
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
263 sampler_state
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
264 sampler_state
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
265 sampler_state
.normalized_coords
= 1;
266 ctx
->sampler_state
= pipe
->create_sampler_state(pipe
, &sampler_state
);
267 sampler_state
.normalized_coords
= 0;
268 ctx
->sampler_state_rect
= pipe
->create_sampler_state(pipe
, &sampler_state
);
270 sampler_state
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
271 sampler_state
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
272 sampler_state
.normalized_coords
= 1;
273 ctx
->sampler_state_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
274 sampler_state
.normalized_coords
= 0;
275 ctx
->sampler_state_rect_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
277 /* rasterizer state */
278 memset(&rs_state
, 0, sizeof(rs_state
));
279 rs_state
.cull_face
= PIPE_FACE_NONE
;
280 rs_state
.half_pixel_center
= 1;
281 rs_state
.bottom_edge_rule
= 1;
282 rs_state
.flatshade
= 1;
283 rs_state
.depth_clip
= 1;
284 ctx
->rs_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
286 rs_state
.scissor
= 1;
287 ctx
->rs_state_scissor
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
289 if (ctx
->has_stream_out
) {
290 rs_state
.scissor
= 0;
291 rs_state
.rasterizer_discard
= 1;
292 ctx
->rs_discard_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
295 ctx
->base
.cb_slot
= 0; /* 0 for now */
296 ctx
->base
.vb_slot
= 0; /* 0 for now */
298 /* vertex elements states */
299 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
300 for (i
= 0; i
< 2; i
++) {
301 velem
[i
].src_offset
= i
* 4 * sizeof(float);
302 velem
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
303 velem
[i
].vertex_buffer_index
= ctx
->base
.vb_slot
;
305 ctx
->velem_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
307 if (ctx
->has_stream_out
) {
308 static enum pipe_format formats
[4] = {
309 PIPE_FORMAT_R32_UINT
,
310 PIPE_FORMAT_R32G32_UINT
,
311 PIPE_FORMAT_R32G32B32_UINT
,
312 PIPE_FORMAT_R32G32B32A32_UINT
315 for (i
= 0; i
< 4; i
++) {
316 velem
[0].src_format
= formats
[i
];
317 velem
[0].vertex_buffer_index
= ctx
->base
.vb_slot
;
318 ctx
->velem_state_readbuf
[i
] =
319 pipe
->create_vertex_elements_state(pipe
, 1, &velem
[0]);
324 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_INSTANCEID
) &&
325 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT
);
327 /* set invariant vertex coordinates */
328 for (i
= 0; i
< 4; i
++)
329 ctx
->vertices
[i
][0][3] = 1; /*v.w*/
334 static void bind_vs_pos_only(struct blitter_context_priv
*ctx
,
335 unsigned num_so_channels
)
337 struct pipe_context
*pipe
= ctx
->base
.pipe
;
338 int index
= num_so_channels
? num_so_channels
- 1 : 0;
340 if (!ctx
->vs_pos_only
[index
]) {
341 struct pipe_stream_output_info so
;
342 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
};
343 const uint semantic_indices
[] = { 0 };
345 memset(&so
, 0, sizeof(so
));
347 so
.output
[0].num_components
= num_so_channels
;
348 so
.stride
[0] = num_so_channels
;
350 ctx
->vs_pos_only
[index
] =
351 util_make_vertex_passthrough_shader_with_so(pipe
, 1, semantic_names
,
352 semantic_indices
, FALSE
,
356 pipe
->bind_vs_state(pipe
, ctx
->vs_pos_only
[index
]);
359 static void bind_vs_passthrough(struct blitter_context_priv
*ctx
)
361 struct pipe_context
*pipe
= ctx
->base
.pipe
;
364 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
365 TGSI_SEMANTIC_GENERIC
};
366 const uint semantic_indices
[] = { 0, 0 };
368 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
369 semantic_indices
, FALSE
);
372 pipe
->bind_vs_state(pipe
, ctx
->vs
);
375 static void bind_vs_layered(struct blitter_context_priv
*ctx
)
377 struct pipe_context
*pipe
= ctx
->base
.pipe
;
379 if (!ctx
->vs_layered
) {
380 ctx
->vs_layered
= util_make_layered_clear_vertex_shader(pipe
);
383 pipe
->bind_vs_state(pipe
, ctx
->vs_layered
);
386 static void bind_fs_empty(struct blitter_context_priv
*ctx
)
388 struct pipe_context
*pipe
= ctx
->base
.pipe
;
390 if (!ctx
->fs_empty
) {
391 assert(!ctx
->cached_all_shaders
);
392 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
395 ctx
->bind_fs_state(pipe
, ctx
->fs_empty
);
398 static void bind_fs_write_one_cbuf(struct blitter_context_priv
*ctx
)
400 struct pipe_context
*pipe
= ctx
->base
.pipe
;
402 if (!ctx
->fs_write_one_cbuf
) {
403 assert(!ctx
->cached_all_shaders
);
404 ctx
->fs_write_one_cbuf
=
405 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
406 TGSI_INTERPOLATE_CONSTANT
, FALSE
);
409 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
412 static void bind_fs_write_all_cbufs(struct blitter_context_priv
*ctx
)
414 struct pipe_context
*pipe
= ctx
->base
.pipe
;
416 if (!ctx
->fs_write_all_cbufs
) {
417 assert(!ctx
->cached_all_shaders
);
418 ctx
->fs_write_all_cbufs
=
419 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
420 TGSI_INTERPOLATE_CONSTANT
, TRUE
);
423 ctx
->bind_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
426 void util_blitter_destroy(struct blitter_context
*blitter
)
428 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
429 struct pipe_context
*pipe
= blitter
->pipe
;
432 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++)
433 for (j
= 0; j
< 2; j
++)
434 pipe
->delete_blend_state(pipe
, ctx
->blend
[i
][j
]);
436 for (i
= 0; i
< ARRAY_SIZE(ctx
->blend_clear
); i
++) {
437 if (ctx
->blend_clear
[i
])
438 pipe
->delete_blend_state(pipe
, ctx
->blend_clear
[i
]);
440 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
441 pipe
->delete_depth_stencil_alpha_state(pipe
,
442 ctx
->dsa_write_depth_keep_stencil
);
443 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
444 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
446 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
);
447 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state_scissor
);
448 if (ctx
->rs_discard_state
)
449 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_discard_state
);
451 pipe
->delete_vs_state(pipe
, ctx
->vs
);
452 for (i
= 0; i
< 4; i
++)
453 if (ctx
->vs_pos_only
[i
])
454 pipe
->delete_vs_state(pipe
, ctx
->vs_pos_only
[i
]);
456 pipe
->delete_vs_state(pipe
, ctx
->vs_layered
);
457 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state
);
458 for (i
= 0; i
< 4; i
++) {
459 if (ctx
->velem_state_readbuf
[i
]) {
460 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[i
]);
464 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
465 for (unsigned type
= 0; type
< ARRAY_SIZE(ctx
->fs_texfetch_col
); ++type
) {
466 for (unsigned inst
= 0; inst
< 2; inst
++) {
467 if (ctx
->fs_texfetch_col
[type
][i
][inst
])
468 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[type
][i
][inst
]);
470 if (ctx
->fs_texfetch_col_msaa
[type
][i
])
471 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col_msaa
[type
][i
]);
474 for (unsigned inst
= 0; inst
< 2; inst
++) {
475 if (ctx
->fs_texfetch_depth
[i
][inst
])
476 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
][inst
]);
477 if (ctx
->fs_texfetch_depthstencil
[i
][inst
])
478 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil
[i
][inst
]);
479 if (ctx
->fs_texfetch_stencil
[i
][inst
])
480 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil
[i
][inst
]);
483 if (ctx
->fs_texfetch_depth_msaa
[i
])
484 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth_msaa
[i
]);
485 if (ctx
->fs_texfetch_depthstencil_msaa
[i
])
486 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil_msaa
[i
]);
487 if (ctx
->fs_texfetch_stencil_msaa
[i
])
488 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil_msaa
[i
]);
490 for (j
= 0; j
< ARRAY_SIZE(ctx
->fs_resolve
[i
]); j
++)
491 for (f
= 0; f
< 2; f
++)
492 if (ctx
->fs_resolve
[i
][j
][f
])
493 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve
[i
][j
][f
]);
497 ctx
->delete_fs_state(pipe
, ctx
->fs_empty
);
498 if (ctx
->fs_write_one_cbuf
)
499 ctx
->delete_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
500 if (ctx
->fs_write_all_cbufs
)
501 ctx
->delete_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
503 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect_linear
);
504 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect
);
505 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_linear
);
506 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
);
510 void util_blitter_set_texture_multisample(struct blitter_context
*blitter
,
513 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
515 ctx
->has_texture_multisample
= supported
;
518 void util_blitter_set_running_flag(struct blitter_context
*blitter
)
520 if (blitter
->running
) {
521 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
524 blitter
->running
= TRUE
;
526 blitter
->pipe
->set_active_query_state(blitter
->pipe
, false);
529 void util_blitter_unset_running_flag(struct blitter_context
*blitter
)
531 if (!blitter
->running
) {
532 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
535 blitter
->running
= FALSE
;
537 blitter
->pipe
->set_active_query_state(blitter
->pipe
, true);
540 static void blitter_check_saved_vertex_states(struct blitter_context_priv
*ctx
)
542 assert(ctx
->base
.saved_velem_state
!= INVALID_PTR
);
543 assert(ctx
->base
.saved_vs
!= INVALID_PTR
);
544 assert(!ctx
->has_geometry_shader
|| ctx
->base
.saved_gs
!= INVALID_PTR
);
545 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tcs
!= INVALID_PTR
);
546 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tes
!= INVALID_PTR
);
547 assert(!ctx
->has_stream_out
|| ctx
->base
.saved_num_so_targets
!= ~0u);
548 assert(ctx
->base
.saved_rs_state
!= INVALID_PTR
);
551 void util_blitter_restore_vertex_states(struct blitter_context
*blitter
)
553 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
554 struct pipe_context
*pipe
= ctx
->base
.pipe
;
558 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1,
559 &ctx
->base
.saved_vertex_buffer
);
560 pipe_vertex_buffer_unreference(&ctx
->base
.saved_vertex_buffer
);
562 /* Vertex elements. */
563 pipe
->bind_vertex_elements_state(pipe
, ctx
->base
.saved_velem_state
);
564 ctx
->base
.saved_velem_state
= INVALID_PTR
;
567 pipe
->bind_vs_state(pipe
, ctx
->base
.saved_vs
);
568 ctx
->base
.saved_vs
= INVALID_PTR
;
570 /* Geometry shader. */
571 if (ctx
->has_geometry_shader
) {
572 pipe
->bind_gs_state(pipe
, ctx
->base
.saved_gs
);
573 ctx
->base
.saved_gs
= INVALID_PTR
;
576 if (ctx
->has_tessellation
) {
577 pipe
->bind_tcs_state(pipe
, ctx
->base
.saved_tcs
);
578 pipe
->bind_tes_state(pipe
, ctx
->base
.saved_tes
);
579 ctx
->base
.saved_tcs
= INVALID_PTR
;
580 ctx
->base
.saved_tes
= INVALID_PTR
;
583 /* Stream outputs. */
584 if (ctx
->has_stream_out
) {
585 unsigned offsets
[PIPE_MAX_SO_BUFFERS
];
586 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
587 offsets
[i
] = (unsigned)-1;
588 pipe
->set_stream_output_targets(pipe
,
589 ctx
->base
.saved_num_so_targets
,
590 ctx
->base
.saved_so_targets
, offsets
);
592 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
593 pipe_so_target_reference(&ctx
->base
.saved_so_targets
[i
], NULL
);
595 ctx
->base
.saved_num_so_targets
= ~0;
599 pipe
->bind_rasterizer_state(pipe
, ctx
->base
.saved_rs_state
);
600 ctx
->base
.saved_rs_state
= INVALID_PTR
;
603 static void blitter_check_saved_fragment_states(struct blitter_context_priv
*ctx
)
605 assert(ctx
->base
.saved_fs
!= INVALID_PTR
);
606 assert(ctx
->base
.saved_dsa_state
!= INVALID_PTR
);
607 assert(ctx
->base
.saved_blend_state
!= INVALID_PTR
);
610 void util_blitter_restore_fragment_states(struct blitter_context
*blitter
)
612 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
613 struct pipe_context
*pipe
= ctx
->base
.pipe
;
615 /* Fragment shader. */
616 ctx
->bind_fs_state(pipe
, ctx
->base
.saved_fs
);
617 ctx
->base
.saved_fs
= INVALID_PTR
;
619 /* Depth, stencil, alpha. */
620 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->base
.saved_dsa_state
);
621 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
624 pipe
->bind_blend_state(pipe
, ctx
->base
.saved_blend_state
);
625 ctx
->base
.saved_blend_state
= INVALID_PTR
;
628 if (ctx
->base
.is_sample_mask_saved
) {
629 pipe
->set_sample_mask(pipe
, ctx
->base
.saved_sample_mask
);
630 ctx
->base
.is_sample_mask_saved
= FALSE
;
633 /* Miscellaneous states. */
634 /* XXX check whether these are saved and whether they need to be restored
635 * (depending on the operation) */
636 pipe
->set_stencil_ref(pipe
, &ctx
->base
.saved_stencil_ref
);
637 pipe
->set_viewport_states(pipe
, 0, 1, &ctx
->base
.saved_viewport
);
640 static void blitter_check_saved_fb_state(struct blitter_context_priv
*ctx
)
642 assert(ctx
->base
.saved_fb_state
.nr_cbufs
!= ~0u);
645 static void blitter_disable_render_cond(struct blitter_context_priv
*ctx
)
647 struct pipe_context
*pipe
= ctx
->base
.pipe
;
649 if (ctx
->base
.saved_render_cond_query
) {
650 pipe
->render_condition(pipe
, NULL
, FALSE
, 0);
654 void util_blitter_restore_render_cond(struct blitter_context
*blitter
)
656 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
657 struct pipe_context
*pipe
= ctx
->base
.pipe
;
659 if (ctx
->base
.saved_render_cond_query
) {
660 pipe
->render_condition(pipe
, ctx
->base
.saved_render_cond_query
,
661 ctx
->base
.saved_render_cond_cond
,
662 ctx
->base
.saved_render_cond_mode
);
663 ctx
->base
.saved_render_cond_query
= NULL
;
667 void util_blitter_restore_fb_state(struct blitter_context
*blitter
)
669 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
670 struct pipe_context
*pipe
= ctx
->base
.pipe
;
672 pipe
->set_framebuffer_state(pipe
, &ctx
->base
.saved_fb_state
);
673 util_unreference_framebuffer_state(&ctx
->base
.saved_fb_state
);
676 static void blitter_check_saved_textures(struct blitter_context_priv
*ctx
)
678 assert(ctx
->base
.saved_num_sampler_states
!= ~0u);
679 assert(ctx
->base
.saved_num_sampler_views
!= ~0u);
682 void util_blitter_restore_textures(struct blitter_context
*blitter
)
684 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
685 struct pipe_context
*pipe
= ctx
->base
.pipe
;
688 /* Fragment sampler states. */
689 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0,
690 ctx
->base
.saved_num_sampler_states
,
691 ctx
->base
.saved_sampler_states
);
693 ctx
->base
.saved_num_sampler_states
= ~0;
695 /* Fragment sampler views. */
696 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
697 ctx
->base
.saved_num_sampler_views
,
698 ctx
->base
.saved_sampler_views
);
700 for (i
= 0; i
< ctx
->base
.saved_num_sampler_views
; i
++)
701 pipe_sampler_view_reference(&ctx
->base
.saved_sampler_views
[i
], NULL
);
703 ctx
->base
.saved_num_sampler_views
= ~0;
706 void util_blitter_restore_constant_buffer_state(struct blitter_context
*blitter
)
708 struct pipe_context
*pipe
= blitter
->pipe
;
710 pipe
->set_constant_buffer(pipe
, PIPE_SHADER_FRAGMENT
, blitter
->cb_slot
,
711 &blitter
->saved_fs_constant_buffer
);
712 pipe_resource_reference(&blitter
->saved_fs_constant_buffer
.buffer
, NULL
);
715 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
716 int x1
, int y1
, int x2
, int y2
,
721 /* set vertex positions */
722 ctx
->vertices
[0][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v0.x*/
723 ctx
->vertices
[0][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v0.y*/
725 ctx
->vertices
[1][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v1.x*/
726 ctx
->vertices
[1][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v1.y*/
728 ctx
->vertices
[2][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v2.x*/
729 ctx
->vertices
[2][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v2.y*/
731 ctx
->vertices
[3][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v3.x*/
732 ctx
->vertices
[3][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v3.y*/
734 for (i
= 0; i
< 4; i
++)
735 ctx
->vertices
[i
][0][2] = depth
; /*z*/
738 ctx
->viewport
.scale
[0] = 0.5f
* ctx
->dst_width
;
739 ctx
->viewport
.scale
[1] = 0.5f
* ctx
->dst_height
;
740 ctx
->viewport
.scale
[2] = 1.0f
;
741 ctx
->viewport
.translate
[0] = 0.5f
* ctx
->dst_width
;
742 ctx
->viewport
.translate
[1] = 0.5f
* ctx
->dst_height
;
743 ctx
->viewport
.translate
[2] = 0.0f
;
744 ctx
->base
.pipe
->set_viewport_states(ctx
->base
.pipe
, 0, 1, &ctx
->viewport
);
747 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
748 const union pipe_color_union
*color
)
753 for (i
= 0; i
< 4; i
++) {
754 uint32_t *uiverts
= (uint32_t *)ctx
->vertices
[i
][1];
755 uiverts
[0] = color
->ui
[0];
756 uiverts
[1] = color
->ui
[1];
757 uiverts
[2] = color
->ui
[2];
758 uiverts
[3] = color
->ui
[3];
761 for (i
= 0; i
< 4; i
++) {
762 ctx
->vertices
[i
][1][0] = 0;
763 ctx
->vertices
[i
][1][1] = 0;
764 ctx
->vertices
[i
][1][2] = 0;
765 ctx
->vertices
[i
][1][3] = 0;
770 static void get_texcoords(struct pipe_sampler_view
*src
,
771 unsigned src_width0
, unsigned src_height0
,
772 int x1
, int y1
, int x2
, int y2
, bool uses_txf
,
775 unsigned level
= src
->u
.tex
.first_level
;
776 boolean normalized
= !uses_txf
&&
777 src
->target
!= PIPE_TEXTURE_RECT
&&
778 src
->texture
->nr_samples
<= 1;
781 out
[0] = x1
/ (float)u_minify(src_width0
, level
);
782 out
[1] = y1
/ (float)u_minify(src_height0
, level
);
783 out
[2] = x2
/ (float)u_minify(src_width0
, level
);
784 out
[3] = y2
/ (float)u_minify(src_height0
, level
);
793 static void set_texcoords_in_vertices(const float coord
[4],
794 float *out
, unsigned stride
)
796 out
[0] = coord
[0]; /*t0.s*/
797 out
[1] = coord
[1]; /*t0.t*/
799 out
[0] = coord
[2]; /*t1.s*/
800 out
[1] = coord
[1]; /*t1.t*/
802 out
[0] = coord
[2]; /*t2.s*/
803 out
[1] = coord
[3]; /*t2.t*/
805 out
[0] = coord
[0]; /*t3.s*/
806 out
[1] = coord
[3]; /*t3.t*/
809 static void blitter_set_texcoords(struct blitter_context_priv
*ctx
,
810 struct pipe_sampler_view
*src
,
811 unsigned src_width0
, unsigned src_height0
,
812 float layer
, unsigned sample
,
813 int x1
, int y1
, int x2
, int y2
,
818 float face_coord
[4][2];
820 get_texcoords(src
, src_width0
, src_height0
, x1
, y1
, x2
, y2
, uses_txf
,
823 if (src
->target
== PIPE_TEXTURE_CUBE
||
824 src
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
825 set_texcoords_in_vertices(coord
, &face_coord
[0][0], 2);
826 util_map_texcoords2d_onto_cubemap((unsigned)layer
% 6,
827 /* pointer, stride in floats */
828 &face_coord
[0][0], 2,
829 &ctx
->vertices
[0][1][0], 8,
832 set_texcoords_in_vertices(coord
, &ctx
->vertices
[0][1][0], 8);
836 switch (src
->target
) {
837 case PIPE_TEXTURE_3D
:
842 r
/= u_minify(src
->texture
->depth0
, src
->u
.tex
.first_level
);
844 for (i
= 0; i
< 4; i
++)
845 ctx
->vertices
[i
][1][2] = r
; /*r*/
849 case PIPE_TEXTURE_1D_ARRAY
:
850 for (i
= 0; i
< 4; i
++)
851 ctx
->vertices
[i
][1][1] = (float) layer
; /*t*/
854 case PIPE_TEXTURE_2D_ARRAY
:
855 for (i
= 0; i
< 4; i
++) {
856 ctx
->vertices
[i
][1][2] = (float) layer
; /*r*/
857 ctx
->vertices
[i
][1][3] = (float) sample
; /*q*/
861 case PIPE_TEXTURE_CUBE_ARRAY
:
862 for (i
= 0; i
< 4; i
++)
863 ctx
->vertices
[i
][1][3] = (float) ((unsigned)layer
/ 6); /*w*/
866 case PIPE_TEXTURE_2D
:
867 for (i
= 0; i
< 4; i
++) {
868 ctx
->vertices
[i
][1][3] = (float) sample
; /*r*/
876 static void blitter_set_dst_dimensions(struct blitter_context_priv
*ctx
,
877 unsigned width
, unsigned height
)
879 ctx
->dst_width
= width
;
880 ctx
->dst_height
= height
;
883 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
884 enum pipe_format src_format
,
885 enum pipe_format dst_format
,
886 enum pipe_texture_target target
,
887 unsigned src_nr_samples
,
888 unsigned dst_nr_samples
,
892 struct pipe_context
*pipe
= ctx
->base
.pipe
;
893 enum tgsi_texture_type tgsi_tex
=
894 util_pipe_tex_to_tgsi_tex(target
, src_nr_samples
);
895 enum tgsi_return_type stype
;
896 enum tgsi_return_type dtype
;
899 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
901 if (util_format_is_pure_uint(src_format
)) {
902 stype
= TGSI_RETURN_TYPE_UINT
;
903 if (util_format_is_pure_uint(dst_format
)) {
904 dtype
= TGSI_RETURN_TYPE_UINT
;
907 assert(util_format_is_pure_sint(dst_format
));
908 dtype
= TGSI_RETURN_TYPE_SINT
;
911 } else if (util_format_is_pure_sint(src_format
)) {
912 stype
= TGSI_RETURN_TYPE_SINT
;
913 if (util_format_is_pure_sint(dst_format
)) {
914 dtype
= TGSI_RETURN_TYPE_SINT
;
917 assert(util_format_is_pure_uint(dst_format
));
918 dtype
= TGSI_RETURN_TYPE_UINT
;
922 assert(!util_format_is_pure_uint(dst_format
) &&
923 !util_format_is_pure_sint(dst_format
));
924 dtype
= stype
= TGSI_RETURN_TYPE_FLOAT
;
928 if (src_nr_samples
> 1) {
931 /* OpenGL requires that integer textures just copy 1 sample instead
934 if (dst_nr_samples
<= 1 &&
935 stype
!= TGSI_RETURN_TYPE_UINT
&&
936 stype
!= TGSI_RETURN_TYPE_SINT
) {
937 /* The destination has one sample, so we'll do color resolve. */
938 unsigned index
= GET_MSAA_RESOLVE_FS_IDX(src_nr_samples
);
942 shader
= &ctx
->fs_resolve
[target
][index
][filter
];
945 assert(!ctx
->cached_all_shaders
);
946 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
947 *shader
= util_make_fs_msaa_resolve_bilinear(pipe
, tgsi_tex
,
952 *shader
= util_make_fs_msaa_resolve(pipe
, tgsi_tex
,
959 /* The destination has multiple samples, we'll do
960 * an MSAA->MSAA copy.
962 shader
= &ctx
->fs_texfetch_col_msaa
[type
][target
];
964 /* Create the fragment shader on-demand. */
966 assert(!ctx
->cached_all_shaders
);
967 *shader
= util_make_fs_blit_msaa_color(pipe
, tgsi_tex
, stype
, dtype
);
976 shader
= &ctx
->fs_texfetch_col
[type
][target
][1];
978 shader
= &ctx
->fs_texfetch_col
[type
][target
][0];
980 /* Create the fragment shader on-demand. */
982 assert(!ctx
->cached_all_shaders
);
983 *shader
= util_make_fragment_tex_shader(pipe
, tgsi_tex
,
984 TGSI_INTERPOLATE_LINEAR
,
986 ctx
->has_tex_lz
, use_txf
);
994 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
995 enum pipe_texture_target target
,
999 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1001 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1003 if (nr_samples
> 1) {
1004 void **shader
= &ctx
->fs_texfetch_depth_msaa
[target
];
1006 /* Create the fragment shader on-demand. */
1008 enum tgsi_texture_type tgsi_tex
;
1009 assert(!ctx
->cached_all_shaders
);
1010 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1011 *shader
= util_make_fs_blit_msaa_depth(pipe
, tgsi_tex
);
1019 shader
= &ctx
->fs_texfetch_depth
[target
][1];
1021 shader
= &ctx
->fs_texfetch_depth
[target
][0];
1023 /* Create the fragment shader on-demand. */
1025 enum tgsi_texture_type tgsi_tex
;
1026 assert(!ctx
->cached_all_shaders
);
1027 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1029 util_make_fragment_tex_shader_writedepth(pipe
, tgsi_tex
,
1030 TGSI_INTERPOLATE_LINEAR
,
1031 ctx
->has_tex_lz
, use_txf
);
1039 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv
*ctx
,
1040 enum pipe_texture_target target
,
1041 unsigned nr_samples
,
1044 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1046 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1048 if (nr_samples
> 1) {
1049 void **shader
= &ctx
->fs_texfetch_depthstencil_msaa
[target
];
1051 /* Create the fragment shader on-demand. */
1053 enum tgsi_texture_type tgsi_tex
;
1054 assert(!ctx
->cached_all_shaders
);
1055 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1056 *shader
= util_make_fs_blit_msaa_depthstencil(pipe
, tgsi_tex
);
1064 shader
= &ctx
->fs_texfetch_depthstencil
[target
][1];
1066 shader
= &ctx
->fs_texfetch_depthstencil
[target
][0];
1068 /* Create the fragment shader on-demand. */
1070 enum tgsi_texture_type tgsi_tex
;
1071 assert(!ctx
->cached_all_shaders
);
1072 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1074 util_make_fragment_tex_shader_writedepthstencil(pipe
, tgsi_tex
,
1075 TGSI_INTERPOLATE_LINEAR
,
1085 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv
*ctx
,
1086 enum pipe_texture_target target
,
1087 unsigned nr_samples
,
1090 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1092 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1094 if (nr_samples
> 1) {
1095 void **shader
= &ctx
->fs_texfetch_stencil_msaa
[target
];
1097 /* Create the fragment shader on-demand. */
1099 enum tgsi_texture_type tgsi_tex
;
1100 assert(!ctx
->cached_all_shaders
);
1101 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1102 *shader
= util_make_fs_blit_msaa_stencil(pipe
, tgsi_tex
);
1110 shader
= &ctx
->fs_texfetch_stencil
[target
][1];
1112 shader
= &ctx
->fs_texfetch_stencil
[target
][0];
1114 /* Create the fragment shader on-demand. */
1116 enum tgsi_texture_type tgsi_tex
;
1117 assert(!ctx
->cached_all_shaders
);
1118 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1120 util_make_fragment_tex_shader_writestencil(pipe
, tgsi_tex
,
1121 TGSI_INTERPOLATE_LINEAR
,
1122 ctx
->has_tex_lz
, use_txf
);
1131 * Generate and save all fragment shaders that we will ever need for
1132 * blitting. Drivers which use the 'draw' fallbacks will typically use
1133 * this to make sure we generate/use shaders that don't go through the
1134 * draw module's wrapper functions.
1136 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
)
1138 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1139 struct pipe_context
*pipe
= blitter
->pipe
;
1140 struct pipe_screen
*screen
= pipe
->screen
;
1141 unsigned samples
, j
, f
, target
, max_samples
, use_txf
;
1142 boolean has_arraytex
, has_cubearraytex
;
1144 max_samples
= ctx
->has_texture_multisample
? 2 : 1;
1145 has_arraytex
= screen
->get_param(screen
,
1146 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
) != 0;
1147 has_cubearraytex
= screen
->get_param(screen
,
1148 PIPE_CAP_CUBE_MAP_ARRAY
) != 0;
1150 /* It only matters if i <= 1 or > 1. */
1151 for (samples
= 1; samples
<= max_samples
; samples
++) {
1152 for (target
= PIPE_TEXTURE_1D
; target
< PIPE_MAX_TEXTURE_TYPES
; target
++) {
1153 for (use_txf
= 0; use_txf
<= ctx
->has_txf
; use_txf
++) {
1154 if (!has_arraytex
&&
1155 (target
== PIPE_TEXTURE_1D_ARRAY
||
1156 target
== PIPE_TEXTURE_2D_ARRAY
)) {
1159 if (!has_cubearraytex
&&
1160 (target
== PIPE_TEXTURE_CUBE_ARRAY
))
1164 (target
!= PIPE_TEXTURE_2D
&&
1165 target
!= PIPE_TEXTURE_2D_ARRAY
))
1168 if (samples
> 1 && use_txf
)
1169 continue; /* TXF is the only option, use_txf has no effect */
1171 /* If samples == 1, the shaders read one texel. If samples >= 1,
1172 * they read one sample.
1174 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
,
1175 PIPE_FORMAT_R32_FLOAT
, target
,
1176 samples
, samples
, 0, use_txf
);
1177 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1178 PIPE_FORMAT_R32_UINT
, target
,
1179 samples
, samples
, 0, use_txf
);
1180 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1181 PIPE_FORMAT_R32_SINT
, target
,
1182 samples
, samples
, 0, use_txf
);
1183 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1184 PIPE_FORMAT_R32_SINT
, target
,
1185 samples
, samples
, 0, use_txf
);
1186 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1187 PIPE_FORMAT_R32_UINT
, target
,
1188 samples
, samples
, 0, use_txf
);
1189 blitter_get_fs_texfetch_depth(ctx
, target
, samples
, use_txf
);
1190 if (ctx
->has_stencil_export
) {
1191 blitter_get_fs_texfetch_depthstencil(ctx
, target
, samples
, use_txf
);
1192 blitter_get_fs_texfetch_stencil(ctx
, target
, samples
, use_txf
);
1198 /* MSAA resolve shaders. */
1199 for (j
= 2; j
< 32; j
++) {
1200 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R32_FLOAT
,
1202 PIPE_BIND_SAMPLER_VIEW
)) {
1206 for (f
= 0; f
< 2; f
++) {
1207 if (f
!= PIPE_TEX_FILTER_NEAREST
&& use_txf
)
1210 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
,
1211 PIPE_FORMAT_R32_FLOAT
, target
,
1213 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1214 PIPE_FORMAT_R32_UINT
, target
,
1216 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1217 PIPE_FORMAT_R32_SINT
, target
,
1225 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
1227 ctx
->fs_write_one_cbuf
=
1228 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1229 TGSI_INTERPOLATE_CONSTANT
, FALSE
);
1231 ctx
->fs_write_all_cbufs
=
1232 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1233 TGSI_INTERPOLATE_CONSTANT
, TRUE
);
1235 ctx
->cached_all_shaders
= TRUE
;
1238 static void blitter_set_common_draw_rect_state(struct blitter_context_priv
*ctx
,
1242 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1244 pipe
->bind_rasterizer_state(pipe
, scissor
? ctx
->rs_state_scissor
1247 bind_vs_layered(ctx
);
1249 bind_vs_passthrough(ctx
);
1251 if (ctx
->has_geometry_shader
)
1252 pipe
->bind_gs_state(pipe
, NULL
);
1253 if (ctx
->has_tessellation
) {
1254 pipe
->bind_tcs_state(pipe
, NULL
);
1255 pipe
->bind_tes_state(pipe
, NULL
);
1257 if (ctx
->has_stream_out
)
1258 pipe
->set_stream_output_targets(pipe
, 0, NULL
, NULL
);
1261 static void blitter_draw(struct blitter_context_priv
*ctx
,
1262 int x1
, int y1
, int x2
, int y2
, float depth
,
1263 unsigned num_instances
)
1265 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1266 struct pipe_vertex_buffer vb
= {0};
1268 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
1270 vb
.stride
= 8 * sizeof(float);
1272 u_upload_data(pipe
->stream_uploader
, 0, sizeof(ctx
->vertices
), 4, ctx
->vertices
,
1273 &vb
.buffer_offset
, &vb
.buffer
.resource
);
1274 if (!vb
.buffer
.resource
)
1276 u_upload_unmap(pipe
->stream_uploader
);
1278 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1279 util_draw_arrays_instanced(pipe
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
1281 pipe_resource_reference(&vb
.buffer
.resource
, NULL
);
1284 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
1285 int x1
, int y1
, int x2
, int y2
, float depth
,
1286 enum blitter_attrib_type type
,
1287 const union pipe_color_union
*attrib
)
1289 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1292 case UTIL_BLITTER_ATTRIB_COLOR
:
1293 blitter_set_clear_color(ctx
, attrib
);
1296 case UTIL_BLITTER_ATTRIB_TEXCOORD
:
1297 set_texcoords_in_vertices(attrib
->f
, &ctx
->vertices
[0][1][0], 8);
1303 blitter_draw(ctx
, x1
, y1
, x2
, y2
, depth
, 1);
1306 static void *get_clear_blend_state(struct blitter_context_priv
*ctx
,
1307 unsigned clear_buffers
)
1309 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1312 clear_buffers
&= PIPE_CLEAR_COLOR
;
1314 /* Return an existing blend state. */
1316 return ctx
->blend
[0][0];
1318 index
= GET_CLEAR_BLEND_STATE_IDX(clear_buffers
);
1320 if (ctx
->blend_clear
[index
])
1321 return ctx
->blend_clear
[index
];
1323 /* Create a new one. */
1325 struct pipe_blend_state blend
= {0};
1328 blend
.independent_blend_enable
= 1;
1330 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1331 if (clear_buffers
& (PIPE_CLEAR_COLOR0
<< i
)) {
1332 blend
.rt
[i
].colormask
= PIPE_MASK_RGBA
;
1336 ctx
->blend_clear
[index
] = pipe
->create_blend_state(pipe
, &blend
);
1338 return ctx
->blend_clear
[index
];
1341 void util_blitter_common_clear_setup(struct blitter_context
*blitter
,
1342 unsigned width
, unsigned height
,
1343 unsigned clear_buffers
,
1344 void *custom_blend
, void *custom_dsa
)
1346 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1347 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1349 util_blitter_set_running_flag(blitter
);
1350 blitter_check_saved_vertex_states(ctx
);
1351 blitter_check_saved_fragment_states(ctx
);
1352 blitter_disable_render_cond(ctx
);
1356 pipe
->bind_blend_state(pipe
, custom_blend
);
1358 pipe
->bind_blend_state(pipe
, get_clear_blend_state(ctx
, clear_buffers
));
1362 pipe
->bind_depth_stencil_alpha_state(pipe
, custom_dsa
);
1363 } else if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1364 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1365 } else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
1366 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1367 } else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
1368 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1370 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1373 pipe
->set_sample_mask(pipe
, ~0);
1374 blitter_set_dst_dimensions(ctx
, width
, height
);
1377 static void util_blitter_clear_custom(struct blitter_context
*blitter
,
1378 unsigned width
, unsigned height
,
1379 unsigned num_layers
,
1380 unsigned clear_buffers
,
1381 const union pipe_color_union
*color
,
1382 double depth
, unsigned stencil
,
1383 void *custom_blend
, void *custom_dsa
)
1385 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1386 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1387 struct pipe_stencil_ref sr
= { { 0 } };
1389 assert(ctx
->has_layered
|| num_layers
<= 1);
1391 util_blitter_common_clear_setup(blitter
, width
, height
, clear_buffers
,
1392 custom_blend
, custom_dsa
);
1394 sr
.ref_value
[0] = stencil
& 0xff;
1395 pipe
->set_stencil_ref(pipe
, &sr
);
1397 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1398 bind_fs_write_all_cbufs(ctx
);
1400 if (num_layers
> 1 && ctx
->has_layered
) {
1401 blitter_set_common_draw_rect_state(ctx
, FALSE
, TRUE
);
1402 blitter_set_clear_color(ctx
, color
);
1403 blitter_draw(ctx
, 0, 0, width
, height
, depth
, num_layers
);
1406 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1407 blitter
->draw_rectangle(blitter
, 0, 0, width
, height
, (float) depth
,
1408 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1411 util_blitter_restore_vertex_states(blitter
);
1412 util_blitter_restore_fragment_states(blitter
);
1413 util_blitter_restore_render_cond(blitter
);
1414 util_blitter_unset_running_flag(blitter
);
1417 void util_blitter_clear(struct blitter_context
*blitter
,
1418 unsigned width
, unsigned height
, unsigned num_layers
,
1419 unsigned clear_buffers
,
1420 const union pipe_color_union
*color
,
1421 double depth
, unsigned stencil
)
1423 util_blitter_clear_custom(blitter
, width
, height
, num_layers
,
1424 clear_buffers
, color
, depth
, stencil
,
1428 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1429 unsigned width
, unsigned height
,
1430 double depth
, void *custom_dsa
)
1432 static const union pipe_color_union color
;
1433 util_blitter_clear_custom(blitter
, width
, height
, 0, 0, &color
, depth
, 0,
1437 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1438 struct pipe_resource
*dst
,
1442 memset(dst_templ
, 0, sizeof(*dst_templ
));
1443 dst_templ
->format
= util_format_linear(dst
->format
);
1444 dst_templ
->u
.tex
.level
= dstlevel
;
1445 dst_templ
->u
.tex
.first_layer
= dstz
;
1446 dst_templ
->u
.tex
.last_layer
= dstz
;
1449 static struct pipe_surface
*
1450 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
1451 struct pipe_surface
*surf
)
1453 struct pipe_surface dst_templ
;
1455 memset(&dst_templ
, 0, sizeof(dst_templ
));
1456 dst_templ
.format
= surf
->format
;
1457 dst_templ
.u
.tex
.level
= surf
->u
.tex
.level
;
1458 dst_templ
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
+ 1;
1459 dst_templ
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
+ 1;
1461 return pipe
->create_surface(pipe
, surf
->texture
, &dst_templ
);
1464 void util_blitter_default_src_texture(struct blitter_context
*blitter
,
1465 struct pipe_sampler_view
*src_templ
,
1466 struct pipe_resource
*src
,
1469 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1471 memset(src_templ
, 0, sizeof(*src_templ
));
1473 if (ctx
->cube_as_2darray
&&
1474 (src
->target
== PIPE_TEXTURE_CUBE
||
1475 src
->target
== PIPE_TEXTURE_CUBE_ARRAY
))
1476 src_templ
->target
= PIPE_TEXTURE_2D_ARRAY
;
1478 src_templ
->target
= src
->target
;
1480 src_templ
->format
= util_format_linear(src
->format
);
1481 src_templ
->u
.tex
.first_level
= srclevel
;
1482 src_templ
->u
.tex
.last_level
= srclevel
;
1483 src_templ
->u
.tex
.first_layer
= 0;
1484 src_templ
->u
.tex
.last_layer
=
1485 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1486 : src
->array_size
- 1;
1487 src_templ
->swizzle_r
= PIPE_SWIZZLE_X
;
1488 src_templ
->swizzle_g
= PIPE_SWIZZLE_Y
;
1489 src_templ
->swizzle_b
= PIPE_SWIZZLE_Z
;
1490 src_templ
->swizzle_a
= PIPE_SWIZZLE_W
;
1493 static boolean
is_blit_generic_supported(struct blitter_context
*blitter
,
1494 const struct pipe_resource
*dst
,
1495 enum pipe_format dst_format
,
1496 const struct pipe_resource
*src
,
1497 enum pipe_format src_format
,
1500 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1501 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1505 const struct util_format_description
*desc
=
1506 util_format_description(dst_format
);
1507 boolean dst_has_stencil
= util_format_has_stencil(desc
);
1509 /* Stencil export must be supported for stencil copy. */
1510 if ((mask
& PIPE_MASK_S
) && dst_has_stencil
&&
1511 !ctx
->has_stencil_export
) {
1515 if (dst_has_stencil
|| util_format_has_depth(desc
))
1516 bind
= PIPE_BIND_DEPTH_STENCIL
;
1518 bind
= PIPE_BIND_RENDER_TARGET
;
1520 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1521 dst
->nr_samples
, bind
)) {
1527 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1531 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1532 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
)) {
1536 /* Check stencil sampler support for stencil copy. */
1537 if (mask
& PIPE_MASK_S
) {
1538 if (util_format_has_stencil(util_format_description(src_format
))) {
1539 enum pipe_format stencil_format
=
1540 util_format_stencil_only(src_format
);
1541 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1543 if (stencil_format
!= src_format
&&
1544 !screen
->is_format_supported(screen
, stencil_format
,
1545 src
->target
, src
->nr_samples
,
1546 PIPE_BIND_SAMPLER_VIEW
)) {
1556 boolean
util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1557 const struct pipe_resource
*dst
,
1558 const struct pipe_resource
*src
)
1560 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1561 src
, src
->format
, PIPE_MASK_RGBAZS
);
1564 boolean
util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1565 const struct pipe_blit_info
*info
)
1567 return is_blit_generic_supported(blitter
,
1568 info
->dst
.resource
, info
->dst
.format
,
1569 info
->src
.resource
, info
->src
.format
,
1573 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1574 struct pipe_resource
*dst
,
1576 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1577 struct pipe_resource
*src
,
1579 const struct pipe_box
*srcbox
)
1581 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1582 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1583 struct pipe_surface
*dst_view
, dst_templ
;
1584 struct pipe_sampler_view src_templ
, *src_view
;
1585 struct pipe_box dstbox
;
1588 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1590 u_box_3d(dstx
, dsty
, dstz
, abs(srcbox
->width
), abs(srcbox
->height
),
1591 abs(srcbox
->depth
), &dstbox
);
1593 /* Initialize the surface. */
1594 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1595 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1597 /* Initialize the sampler view. */
1598 util_blitter_default_src_texture(blitter
, &src_templ
, src
, src_level
);
1599 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1602 util_blitter_blit_generic(blitter
, dst_view
, &dstbox
,
1603 src_view
, srcbox
, src
->width0
, src
->height0
,
1604 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
,
1607 pipe_surface_reference(&dst_view
, NULL
);
1608 pipe_sampler_view_reference(&src_view
, NULL
);
1611 static void do_blits(struct blitter_context_priv
*ctx
,
1612 struct pipe_surface
*dst
,
1613 const struct pipe_box
*dstbox
,
1614 struct pipe_sampler_view
*src
,
1615 unsigned src_width0
,
1616 unsigned src_height0
,
1617 const struct pipe_box
*srcbox
,
1621 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1622 unsigned src_samples
= src
->texture
->nr_samples
;
1623 unsigned dst_samples
= dst
->texture
->nr_samples
;
1624 enum pipe_texture_target src_target
= src
->target
;
1625 struct pipe_framebuffer_state fb_state
= {0};
1627 /* Initialize framebuffer state. */
1628 fb_state
.width
= dst
->width
;
1629 fb_state
.height
= dst
->height
;
1630 fb_state
.nr_cbufs
= is_zsbuf
? 0 : 1;
1632 blitter_set_dst_dimensions(ctx
, fb_state
.width
, fb_state
.height
);
1634 if ((src_target
== PIPE_TEXTURE_1D
||
1635 src_target
== PIPE_TEXTURE_2D
||
1636 src_target
== PIPE_TEXTURE_RECT
) &&
1638 /* Draw the quad with the draw_rectangle callback. */
1640 /* Set texture coordinates. - use a pipe color union
1641 * for interface purposes.
1642 * XXX pipe_color_union is a wrong name since we use that to set
1643 * texture coordinates too.
1645 union pipe_color_union coord
;
1646 get_texcoords(src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1647 srcbox
->x
+srcbox
->width
, srcbox
->y
+srcbox
->height
,
1650 /* Set framebuffer state. */
1652 fb_state
.zsbuf
= dst
;
1654 fb_state
.cbufs
[0] = dst
;
1656 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1659 pipe
->set_sample_mask(pipe
, ~0);
1660 ctx
->base
.draw_rectangle(&ctx
->base
, dstbox
->x
, dstbox
->y
,
1661 dstbox
->x
+ dstbox
->width
,
1662 dstbox
->y
+ dstbox
->height
, 0,
1663 UTIL_BLITTER_ATTRIB_TEXCOORD
, &coord
);
1665 /* Draw the quad with the generic codepath. */
1667 for (dst_z
= 0; dst_z
< dstbox
->depth
; dst_z
++) {
1668 struct pipe_surface
*old
;
1669 float dst2src_scale
= srcbox
->depth
/ (float)dstbox
->depth
;
1671 /* Scale Z properly if the blit is scaled.
1673 * When downscaling, we want the coordinates centered, so that
1674 * mipmapping works for 3D textures. For example, when generating
1675 * a 4x4x4 level, this wouldn't average the pixels:
1677 * src Z: 0 1 2 3 4 5 6 7
1680 * Because the pixels are not centered below the pixels of the higher
1681 * level. Therefore, we want this:
1682 * src Z: 0 1 2 3 4 5 6 7
1685 * dst_offset defines the offset needed for centering the pixels and
1686 * it works with any scaling (not just 2x).
1688 float dst_offset
= ((srcbox
->depth
- 1) -
1689 (dstbox
->depth
- 1) * dst2src_scale
) * 0.5;
1690 float src_z
= (dst_z
+ dst_offset
) * dst2src_scale
;
1692 /* Set framebuffer state. */
1694 fb_state
.zsbuf
= dst
;
1696 fb_state
.cbufs
[0] = dst
;
1698 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1700 /* See if we need to blit a multisample or singlesample buffer. */
1701 if (src_samples
== dst_samples
&& dst_samples
> 1) {
1703 unsigned i
, max_sample
= dst_samples
- 1;
1705 for (i
= 0; i
<= max_sample
; i
++) {
1706 pipe
->set_sample_mask(pipe
, 1 << i
);
1707 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1709 i
, srcbox
->x
, srcbox
->y
,
1710 srcbox
->x
+ srcbox
->width
,
1711 srcbox
->y
+ srcbox
->height
, uses_txf
);
1712 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1713 dstbox
->x
+ dstbox
->width
,
1714 dstbox
->y
+ dstbox
->height
, 0, 1);
1717 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1718 pipe
->set_sample_mask(pipe
, ~0);
1719 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1720 srcbox
->z
+ src_z
, 0,
1721 srcbox
->x
, srcbox
->y
,
1722 srcbox
->x
+ srcbox
->width
,
1723 srcbox
->y
+ srcbox
->height
, uses_txf
);
1724 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1725 dstbox
->x
+ dstbox
->width
,
1726 dstbox
->y
+ dstbox
->height
, 0, 1);
1729 /* Get the next surface or (if this is the last iteration)
1730 * just unreference the last one. */
1732 if (dst_z
< dstbox
->depth
-1) {
1733 dst
= ctx
->base
.get_next_surface_layer(ctx
->base
.pipe
, dst
);
1736 pipe_surface_reference(&old
, NULL
);
1742 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1743 struct pipe_surface
*dst
,
1744 const struct pipe_box
*dstbox
,
1745 struct pipe_sampler_view
*src
,
1746 const struct pipe_box
*srcbox
,
1747 unsigned src_width0
, unsigned src_height0
,
1748 unsigned mask
, unsigned filter
,
1749 const struct pipe_scissor_state
*scissor
,
1750 boolean alpha_blend
)
1752 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1753 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1754 enum pipe_texture_target src_target
= src
->target
;
1755 unsigned src_samples
= src
->texture
->nr_samples
;
1756 unsigned dst_samples
= dst
->texture
->nr_samples
;
1757 boolean has_depth
, has_stencil
, has_color
;
1758 boolean blit_stencil
, blit_depth
, blit_color
;
1759 void *sampler_state
;
1760 const struct util_format_description
*src_desc
=
1761 util_format_description(src
->format
);
1762 const struct util_format_description
*dst_desc
=
1763 util_format_description(dst
->format
);
1765 has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
&&
1766 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1767 has_depth
= util_format_has_depth(src_desc
) &&
1768 util_format_has_depth(dst_desc
);
1769 has_stencil
= util_format_has_stencil(src_desc
) &&
1770 util_format_has_stencil(dst_desc
);
1772 blit_color
= has_color
&& (mask
& PIPE_MASK_RGBA
);
1773 blit_depth
= has_depth
&& (mask
& PIPE_MASK_Z
);
1774 blit_stencil
= has_stencil
&& (mask
& PIPE_MASK_S
) &&
1775 ctx
->has_stencil_export
;
1777 if (!blit_stencil
&& !blit_depth
&& !blit_color
) {
1781 bool is_scaled
= dstbox
->width
!= abs(srcbox
->width
) ||
1782 dstbox
->height
!= abs(srcbox
->height
);
1784 if (blit_stencil
|| !is_scaled
)
1785 filter
= PIPE_TEX_FILTER_NEAREST
;
1787 bool use_txf
= false;
1789 /* Don't support scaled blits. The TXF shader uses F2I for rounding. */
1792 filter
== PIPE_TEX_FILTER_NEAREST
&&
1793 src
->target
!= PIPE_TEXTURE_CUBE
&&
1794 src
->target
!= PIPE_TEXTURE_CUBE_ARRAY
) {
1795 int src_width
= u_minify(src_width0
, src
->u
.tex
.first_level
);
1796 int src_height
= u_minify(src_height0
, src
->u
.tex
.first_level
);
1797 int src_depth
= src
->u
.tex
.last_layer
+ 1;
1798 struct pipe_box box
= *srcbox
;
1800 /* Eliminate negative width/height/depth. */
1801 if (box
.width
< 0) {
1805 if (box
.height
< 0) {
1806 box
.y
+= box
.height
;
1809 if (box
.depth
< 0) {
1814 /* See if srcbox is in bounds. TXF doesn't clamp the coordinates. */
1816 box
.x
>= 0 && box
.x
< src_width
&&
1817 box
.y
>= 0 && box
.y
< src_height
&&
1818 box
.z
>= 0 && box
.z
< src_depth
&&
1819 box
.x
+ box
.width
> 0 && box
.x
+ box
.width
<= src_width
&&
1820 box
.y
+ box
.height
> 0 && box
.y
+ box
.height
<= src_height
&&
1821 box
.z
+ box
.depth
> 0 && box
.z
+ box
.depth
<= src_depth
;
1824 /* Check whether the states are properly saved. */
1825 util_blitter_set_running_flag(blitter
);
1826 blitter_check_saved_vertex_states(ctx
);
1827 blitter_check_saved_fragment_states(ctx
);
1828 blitter_check_saved_textures(ctx
);
1829 blitter_check_saved_fb_state(ctx
);
1830 blitter_disable_render_cond(ctx
);
1832 if (blit_depth
|| blit_stencil
) {
1833 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
1835 if (blit_depth
&& blit_stencil
) {
1836 pipe
->bind_depth_stencil_alpha_state(pipe
,
1837 ctx
->dsa_write_depth_stencil
);
1838 ctx
->bind_fs_state(pipe
,
1839 blitter_get_fs_texfetch_depthstencil(ctx
, src_target
,
1840 src_samples
, use_txf
));
1841 } else if (blit_depth
) {
1842 pipe
->bind_depth_stencil_alpha_state(pipe
,
1843 ctx
->dsa_write_depth_keep_stencil
);
1844 ctx
->bind_fs_state(pipe
,
1845 blitter_get_fs_texfetch_depth(ctx
, src_target
,
1846 src_samples
, use_txf
));
1847 } else { /* is_stencil */
1848 pipe
->bind_depth_stencil_alpha_state(pipe
,
1849 ctx
->dsa_keep_depth_write_stencil
);
1850 ctx
->bind_fs_state(pipe
,
1851 blitter_get_fs_texfetch_stencil(ctx
, src_target
,
1852 src_samples
, use_txf
));
1856 unsigned colormask
= mask
& PIPE_MASK_RGBA
;
1858 pipe
->bind_blend_state(pipe
, ctx
->blend
[colormask
][alpha_blend
]);
1859 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1860 ctx
->bind_fs_state(pipe
,
1861 blitter_get_fs_texfetch_col(ctx
, src
->format
, dst
->format
, src_target
,
1862 src_samples
, dst_samples
, filter
,
1866 /* Set the linear filter only for scaled color non-MSAA blits. */
1867 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
1868 if (src_target
== PIPE_TEXTURE_RECT
) {
1869 sampler_state
= ctx
->sampler_state_rect_linear
;
1871 sampler_state
= ctx
->sampler_state_linear
;
1874 if (src_target
== PIPE_TEXTURE_RECT
) {
1875 sampler_state
= ctx
->sampler_state_rect
;
1877 sampler_state
= ctx
->sampler_state
;
1882 if (blit_depth
&& blit_stencil
) {
1883 /* Setup two samplers, one for depth and the other one for stencil. */
1884 struct pipe_sampler_view templ
;
1885 struct pipe_sampler_view
*views
[2];
1886 void *samplers
[2] = {sampler_state
, sampler_state
};
1889 templ
.format
= util_format_stencil_only(templ
.format
);
1890 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1893 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1895 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, views
);
1896 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, samplers
);
1898 pipe_sampler_view_reference(&views
[1], NULL
);
1899 } else if (blit_stencil
) {
1900 /* Set a stencil-only sampler view for it not to sample depth instead. */
1901 struct pipe_sampler_view templ
;
1902 struct pipe_sampler_view
*view
;
1905 templ
.format
= util_format_stencil_only(templ
.format
);
1906 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1908 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1910 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &view
);
1911 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1912 0, 1, &sampler_state
);
1914 pipe_sampler_view_reference(&view
, NULL
);
1916 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src
);
1917 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1918 0, 1, &sampler_state
);
1921 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1923 pipe
->set_scissor_states(pipe
, 0, 1, scissor
);
1926 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
, FALSE
);
1928 do_blits(ctx
, dst
, dstbox
, src
, src_width0
, src_height0
,
1929 srcbox
, blit_depth
|| blit_stencil
, use_txf
);
1931 util_blitter_restore_vertex_states(blitter
);
1932 util_blitter_restore_fragment_states(blitter
);
1933 util_blitter_restore_textures(blitter
);
1934 util_blitter_restore_fb_state(blitter
);
1936 pipe
->set_scissor_states(pipe
, 0, 1, &ctx
->base
.saved_scissor
);
1938 util_blitter_restore_render_cond(blitter
);
1939 util_blitter_unset_running_flag(blitter
);
1943 util_blitter_blit(struct blitter_context
*blitter
,
1944 const struct pipe_blit_info
*info
)
1946 struct pipe_resource
*dst
= info
->dst
.resource
;
1947 struct pipe_resource
*src
= info
->src
.resource
;
1948 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1949 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1950 struct pipe_surface
*dst_view
, dst_templ
;
1951 struct pipe_sampler_view src_templ
, *src_view
;
1953 /* Initialize the surface. */
1954 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
1956 dst_templ
.format
= info
->dst
.format
;
1957 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1959 /* Initialize the sampler view. */
1960 util_blitter_default_src_texture(blitter
, &src_templ
, src
, info
->src
.level
);
1961 src_templ
.format
= info
->src
.format
;
1962 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1965 util_blitter_blit_generic(blitter
, dst_view
, &info
->dst
.box
,
1966 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
1967 info
->mask
, info
->filter
,
1968 info
->scissor_enable
? &info
->scissor
: NULL
,
1971 pipe_surface_reference(&dst_view
, NULL
);
1972 pipe_sampler_view_reference(&src_view
, NULL
);
1975 void util_blitter_generate_mipmap(struct blitter_context
*blitter
,
1976 struct pipe_resource
*tex
,
1977 enum pipe_format format
,
1978 unsigned base_level
, unsigned last_level
,
1979 unsigned first_layer
, unsigned last_layer
)
1981 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1982 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1983 struct pipe_surface dst_templ
, *dst_view
;
1984 struct pipe_sampler_view src_templ
, *src_view
;
1986 void *sampler_state
;
1987 const struct util_format_description
*desc
=
1988 util_format_description(format
);
1990 unsigned target
= tex
->target
;
1992 if (ctx
->cube_as_2darray
&&
1993 (target
== PIPE_TEXTURE_CUBE
|| target
== PIPE_TEXTURE_CUBE_ARRAY
))
1994 target
= PIPE_TEXTURE_2D_ARRAY
;
1996 assert(tex
->nr_samples
<= 1);
1997 assert(!util_format_has_stencil(desc
));
1999 is_depth
= desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
;
2001 /* Check whether the states are properly saved. */
2002 util_blitter_set_running_flag(blitter
);
2003 blitter_check_saved_vertex_states(ctx
);
2004 blitter_check_saved_fragment_states(ctx
);
2005 blitter_check_saved_textures(ctx
);
2006 blitter_check_saved_fb_state(ctx
);
2007 blitter_disable_render_cond(ctx
);
2011 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
2012 pipe
->bind_depth_stencil_alpha_state(pipe
,
2013 ctx
->dsa_write_depth_keep_stencil
);
2014 ctx
->bind_fs_state(pipe
,
2015 blitter_get_fs_texfetch_depth(ctx
, target
, 1, false));
2017 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2018 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2019 ctx
->bind_fs_state(pipe
,
2020 blitter_get_fs_texfetch_col(ctx
, tex
->format
, tex
->format
, target
,
2021 1, 1, PIPE_TEX_FILTER_LINEAR
, false));
2024 if (target
== PIPE_TEXTURE_RECT
) {
2025 sampler_state
= ctx
->sampler_state_rect_linear
;
2027 sampler_state
= ctx
->sampler_state_linear
;
2029 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
2030 0, 1, &sampler_state
);
2032 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2033 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2035 for (src_level
= base_level
; src_level
< last_level
; src_level
++) {
2036 struct pipe_box dstbox
= {0}, srcbox
= {0};
2037 unsigned dst_level
= src_level
+ 1;
2039 dstbox
.width
= u_minify(tex
->width0
, dst_level
);
2040 dstbox
.height
= u_minify(tex
->height0
, dst_level
);
2042 srcbox
.width
= u_minify(tex
->width0
, src_level
);
2043 srcbox
.height
= u_minify(tex
->height0
, src_level
);
2045 if (target
== PIPE_TEXTURE_3D
) {
2046 dstbox
.depth
= util_max_layer(tex
, dst_level
) + 1;
2047 srcbox
.depth
= util_max_layer(tex
, src_level
) + 1;
2049 dstbox
.z
= srcbox
.z
= first_layer
;
2050 dstbox
.depth
= srcbox
.depth
= last_layer
- first_layer
+ 1;
2053 /* Initialize the surface. */
2054 util_blitter_default_dst_texture(&dst_templ
, tex
, dst_level
,
2056 dst_templ
.format
= format
;
2057 dst_view
= pipe
->create_surface(pipe
, tex
, &dst_templ
);
2059 /* Initialize the sampler view. */
2060 util_blitter_default_src_texture(blitter
, &src_templ
, tex
, src_level
);
2061 src_templ
.format
= format
;
2062 src_view
= pipe
->create_sampler_view(pipe
, tex
, &src_templ
);
2064 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src_view
);
2066 do_blits(ctx
, dst_view
, &dstbox
, src_view
, tex
->width0
, tex
->height0
,
2067 &srcbox
, is_depth
, false);
2069 pipe_surface_reference(&dst_view
, NULL
);
2070 pipe_sampler_view_reference(&src_view
, NULL
);
2073 util_blitter_restore_vertex_states(blitter
);
2074 util_blitter_restore_fragment_states(blitter
);
2075 util_blitter_restore_textures(blitter
);
2076 util_blitter_restore_fb_state(blitter
);
2077 util_blitter_restore_render_cond(blitter
);
2078 util_blitter_unset_running_flag(blitter
);
2081 /* Clear a region of a color surface to a constant value. */
2082 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
2083 struct pipe_surface
*dstsurf
,
2084 const union pipe_color_union
*color
,
2085 unsigned dstx
, unsigned dsty
,
2086 unsigned width
, unsigned height
)
2088 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2089 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2090 struct pipe_framebuffer_state fb_state
;
2091 unsigned num_layers
;
2093 assert(dstsurf
->texture
);
2094 if (!dstsurf
->texture
)
2097 /* check the saved state */
2098 util_blitter_set_running_flag(blitter
);
2099 blitter_check_saved_vertex_states(ctx
);
2100 blitter_check_saved_fragment_states(ctx
);
2101 blitter_check_saved_fb_state(ctx
);
2102 blitter_disable_render_cond(ctx
);
2105 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2106 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2107 bind_fs_write_one_cbuf(ctx
);
2108 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2110 /* set a framebuffer state */
2111 fb_state
.width
= dstsurf
->width
;
2112 fb_state
.height
= dstsurf
->height
;
2113 fb_state
.nr_cbufs
= 1;
2114 fb_state
.cbufs
[0] = dstsurf
;
2116 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2117 pipe
->set_sample_mask(pipe
, ~0);
2119 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2121 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
2122 if (num_layers
> 1 && ctx
->has_layered
) {
2123 blitter_set_common_draw_rect_state(ctx
, FALSE
, TRUE
);
2124 blitter_set_clear_color(ctx
, color
);
2125 blitter_draw(ctx
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0, num_layers
);
2128 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2129 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
2130 UTIL_BLITTER_ATTRIB_COLOR
, color
);
2133 util_blitter_restore_vertex_states(blitter
);
2134 util_blitter_restore_fragment_states(blitter
);
2135 util_blitter_restore_fb_state(blitter
);
2136 util_blitter_restore_render_cond(blitter
);
2137 util_blitter_unset_running_flag(blitter
);
2140 /* Clear a region of a depth stencil surface. */
2141 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
2142 struct pipe_surface
*dstsurf
,
2143 unsigned clear_flags
,
2146 unsigned dstx
, unsigned dsty
,
2147 unsigned width
, unsigned height
)
2149 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2150 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2151 struct pipe_framebuffer_state fb_state
;
2152 struct pipe_stencil_ref sr
= { { 0 } };
2153 unsigned num_layers
;
2155 assert(dstsurf
->texture
);
2156 if (!dstsurf
->texture
)
2159 /* check the saved state */
2160 util_blitter_set_running_flag(blitter
);
2161 blitter_check_saved_vertex_states(ctx
);
2162 blitter_check_saved_fragment_states(ctx
);
2163 blitter_check_saved_fb_state(ctx
);
2164 blitter_disable_render_cond(ctx
);
2167 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
2168 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
2169 sr
.ref_value
[0] = stencil
& 0xff;
2170 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
2171 pipe
->set_stencil_ref(pipe
, &sr
);
2173 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
2174 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
2176 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
2177 sr
.ref_value
[0] = stencil
& 0xff;
2178 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
2179 pipe
->set_stencil_ref(pipe
, &sr
);
2182 /* hmm that should be illegal probably, or make it a no-op somewhere */
2183 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2186 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2188 /* set a framebuffer state */
2189 fb_state
.width
= dstsurf
->width
;
2190 fb_state
.height
= dstsurf
->height
;
2191 fb_state
.nr_cbufs
= 0;
2192 fb_state
.cbufs
[0] = 0;
2193 fb_state
.zsbuf
= dstsurf
;
2194 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2195 pipe
->set_sample_mask(pipe
, ~0);
2197 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2199 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
2200 if (num_layers
> 1 && ctx
->has_layered
) {
2201 blitter_set_common_draw_rect_state(ctx
, FALSE
, TRUE
);
2202 blitter_draw(ctx
, dstx
, dsty
, dstx
+width
, dsty
+height
, (float) depth
, num_layers
);
2205 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2206 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
,
2208 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2211 util_blitter_restore_vertex_states(blitter
);
2212 util_blitter_restore_fragment_states(blitter
);
2213 util_blitter_restore_fb_state(blitter
);
2214 util_blitter_restore_render_cond(blitter
);
2215 util_blitter_unset_running_flag(blitter
);
2218 /* draw a rectangle across a region using a custom dsa stage - for r600g */
2219 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
2220 struct pipe_surface
*zsurf
,
2221 struct pipe_surface
*cbsurf
,
2222 unsigned sample_mask
,
2223 void *dsa_stage
, float depth
)
2225 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2226 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2227 struct pipe_framebuffer_state fb_state
;
2229 assert(zsurf
->texture
);
2230 if (!zsurf
->texture
)
2233 /* check the saved state */
2234 util_blitter_set_running_flag(blitter
);
2235 blitter_check_saved_vertex_states(ctx
);
2236 blitter_check_saved_fragment_states(ctx
);
2237 blitter_check_saved_fb_state(ctx
);
2238 blitter_disable_render_cond(ctx
);
2241 pipe
->bind_blend_state(pipe
, cbsurf
? ctx
->blend
[PIPE_MASK_RGBA
][0] :
2243 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
2245 bind_fs_write_one_cbuf(ctx
);
2248 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2250 /* set a framebuffer state */
2251 fb_state
.width
= zsurf
->width
;
2252 fb_state
.height
= zsurf
->height
;
2253 fb_state
.nr_cbufs
= 1;
2255 fb_state
.cbufs
[0] = cbsurf
;
2256 fb_state
.nr_cbufs
= 1;
2258 fb_state
.cbufs
[0] = NULL
;
2259 fb_state
.nr_cbufs
= 0;
2261 fb_state
.zsbuf
= zsurf
;
2262 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2263 pipe
->set_sample_mask(pipe
, sample_mask
);
2265 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2266 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
2267 blitter
->draw_rectangle(blitter
, 0, 0, zsurf
->width
, zsurf
->height
, depth
,
2268 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2270 util_blitter_restore_vertex_states(blitter
);
2271 util_blitter_restore_fragment_states(blitter
);
2272 util_blitter_restore_fb_state(blitter
);
2273 util_blitter_restore_render_cond(blitter
);
2274 util_blitter_unset_running_flag(blitter
);
2277 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
2278 struct pipe_resource
*dst
,
2280 struct pipe_resource
*src
,
2284 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2285 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2286 struct pipe_vertex_buffer vb
;
2287 struct pipe_stream_output_target
*so_target
;
2288 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2290 if (srcx
>= src
->width0
||
2291 dstx
>= dst
->width0
) {
2294 if (srcx
+ size
> src
->width0
) {
2295 size
= src
->width0
- srcx
;
2297 if (dstx
+ size
> dst
->width0
) {
2298 size
= dst
->width0
- dstx
;
2301 /* Drivers not capable of Stream Out should not call this function
2302 * in the first place. */
2303 assert(ctx
->has_stream_out
);
2305 /* Some alignment is required. */
2306 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
2307 !ctx
->has_stream_out
) {
2308 struct pipe_box box
;
2309 u_box_1d(srcx
, size
, &box
);
2310 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
2314 util_blitter_set_running_flag(blitter
);
2315 blitter_check_saved_vertex_states(ctx
);
2316 blitter_disable_render_cond(ctx
);
2318 vb
.is_user_buffer
= false;
2319 vb
.buffer
.resource
= src
;
2320 vb
.buffer_offset
= srcx
;
2323 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2324 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[0]);
2325 bind_vs_pos_only(ctx
, 1);
2326 if (ctx
->has_geometry_shader
)
2327 pipe
->bind_gs_state(pipe
, NULL
);
2328 if (ctx
->has_tessellation
) {
2329 pipe
->bind_tcs_state(pipe
, NULL
);
2330 pipe
->bind_tes_state(pipe
, NULL
);
2332 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2334 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
2335 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2337 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2339 util_blitter_restore_vertex_states(blitter
);
2340 util_blitter_restore_render_cond(blitter
);
2341 util_blitter_unset_running_flag(blitter
);
2342 pipe_so_target_reference(&so_target
, NULL
);
2345 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
2346 struct pipe_resource
*dst
,
2347 unsigned offset
, unsigned size
,
2348 unsigned num_channels
,
2349 const union pipe_color_union
*clear_value
)
2351 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2352 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2353 struct pipe_vertex_buffer vb
= {0};
2354 struct pipe_stream_output_target
*so_target
= NULL
;
2355 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2357 assert(num_channels
>= 1);
2358 assert(num_channels
<= 4);
2360 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
2362 * R600 uses this to initialize texture resources, so width0 might not be
2363 * what you think it is.
2366 /* Streamout is required. */
2367 if (!ctx
->has_stream_out
) {
2368 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2372 /* Some alignment is required. */
2373 if (offset
% 4 != 0 || size
% 4 != 0) {
2374 assert(!"Bad alignment in util_blitter_clear_buffer()");
2378 u_upload_data(pipe
->stream_uploader
, 0, num_channels
*4, 4, clear_value
,
2379 &vb
.buffer_offset
, &vb
.buffer
.resource
);
2380 if (!vb
.buffer
.resource
)
2385 util_blitter_set_running_flag(blitter
);
2386 blitter_check_saved_vertex_states(ctx
);
2387 blitter_disable_render_cond(ctx
);
2389 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2390 pipe
->bind_vertex_elements_state(pipe
,
2391 ctx
->velem_state_readbuf
[num_channels
-1]);
2392 bind_vs_pos_only(ctx
, num_channels
);
2393 if (ctx
->has_geometry_shader
)
2394 pipe
->bind_gs_state(pipe
, NULL
);
2395 if (ctx
->has_tessellation
) {
2396 pipe
->bind_tcs_state(pipe
, NULL
);
2397 pipe
->bind_tes_state(pipe
, NULL
);
2399 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2401 so_target
= pipe
->create_stream_output_target(pipe
, dst
, offset
, size
);
2402 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2404 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2407 util_blitter_restore_vertex_states(blitter
);
2408 util_blitter_restore_render_cond(blitter
);
2409 util_blitter_unset_running_flag(blitter
);
2410 pipe_so_target_reference(&so_target
, NULL
);
2411 pipe_resource_reference(&vb
.buffer
.resource
, NULL
);
2414 /* probably radeon specific */
2415 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
2416 struct pipe_resource
*dst
,
2419 struct pipe_resource
*src
,
2421 unsigned sample_mask
,
2423 enum pipe_format format
)
2425 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2426 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2427 struct pipe_framebuffer_state fb_state
;
2428 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
2430 util_blitter_set_running_flag(blitter
);
2431 blitter_check_saved_vertex_states(ctx
);
2432 blitter_check_saved_fragment_states(ctx
);
2433 blitter_disable_render_cond(ctx
);
2436 pipe
->bind_blend_state(pipe
, custom_blend
);
2437 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2438 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2439 bind_fs_write_one_cbuf(ctx
);
2440 pipe
->set_sample_mask(pipe
, sample_mask
);
2442 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
2443 surf_tmpl
.format
= format
;
2444 surf_tmpl
.u
.tex
.level
= dst_level
;
2445 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
2446 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
2448 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
2450 surf_tmpl
.u
.tex
.level
= 0;
2451 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
2452 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
2454 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
2456 /* set a framebuffer state */
2457 fb_state
.width
= src
->width0
;
2458 fb_state
.height
= src
->height0
;
2459 fb_state
.nr_cbufs
= 2;
2460 fb_state
.cbufs
[0] = srcsurf
;
2461 fb_state
.cbufs
[1] = dstsurf
;
2462 fb_state
.zsbuf
= NULL
;
2463 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2465 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2466 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
2467 blitter
->draw_rectangle(blitter
, 0, 0, src
->width0
, src
->height0
,
2469 util_blitter_restore_fb_state(blitter
);
2470 util_blitter_restore_vertex_states(blitter
);
2471 util_blitter_restore_fragment_states(blitter
);
2472 util_blitter_restore_render_cond(blitter
);
2473 util_blitter_unset_running_flag(blitter
);
2475 pipe_surface_reference(&srcsurf
, NULL
);
2476 pipe_surface_reference(&dstsurf
, NULL
);
2479 void util_blitter_custom_color(struct blitter_context
*blitter
,
2480 struct pipe_surface
*dstsurf
,
2483 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2484 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2485 struct pipe_framebuffer_state fb_state
;
2487 assert(dstsurf
->texture
);
2488 if (!dstsurf
->texture
)
2491 /* check the saved state */
2492 util_blitter_set_running_flag(blitter
);
2493 blitter_check_saved_vertex_states(ctx
);
2494 blitter_check_saved_fragment_states(ctx
);
2495 blitter_check_saved_fb_state(ctx
);
2496 blitter_disable_render_cond(ctx
);
2499 pipe
->bind_blend_state(pipe
, custom_blend
? custom_blend
2500 : ctx
->blend
[PIPE_MASK_RGBA
][0]);
2501 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2502 bind_fs_write_one_cbuf(ctx
);
2503 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2504 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2506 /* set a framebuffer state */
2507 fb_state
.width
= dstsurf
->width
;
2508 fb_state
.height
= dstsurf
->height
;
2509 fb_state
.nr_cbufs
= 1;
2510 fb_state
.cbufs
[0] = dstsurf
;
2512 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2513 pipe
->set_sample_mask(pipe
, ~0);
2515 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2516 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2517 blitter
->draw_rectangle(blitter
, 0, 0, dstsurf
->width
, dstsurf
->height
,
2520 util_blitter_restore_vertex_states(blitter
);
2521 util_blitter_restore_fragment_states(blitter
);
2522 util_blitter_restore_fb_state(blitter
);
2523 util_blitter_restore_render_cond(blitter
);
2524 util_blitter_unset_running_flag(blitter
);