1 /**************************************************************************
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 **************************************************************************/
29 * Blitter utility to facilitate acceleration of the clear, clear_render_target,
30 * clear_depth_stencil, resource_copy_region, and blit functions.
35 #include "pipe/p_context.h"
36 #include "pipe/p_defines.h"
37 #include "util/u_inlines.h"
38 #include "pipe/p_shader_tokens.h"
39 #include "pipe/p_state.h"
41 #include "util/u_format.h"
42 #include "util/u_memory.h"
43 #include "util/u_math.h"
44 #include "util/u_blitter.h"
45 #include "util/u_draw_quad.h"
46 #include "util/u_sampler.h"
47 #include "util/u_simple_shaders.h"
48 #include "util/u_surface.h"
49 #include "util/u_texture.h"
50 #include "util/u_upload_mgr.h"
52 #define INVALID_PTR ((void*)~0)
54 #define GET_CLEAR_BLEND_STATE_IDX(clear_buffers) \
55 ((clear_buffers) / PIPE_CLEAR_COLOR0)
57 #define NUM_RESOLVE_FRAG_SHADERS 5 /* MSAA 2x, 4x, 8x, 16x, 32x */
58 #define GET_MSAA_RESOLVE_FS_IDX(nr_samples) (util_logbase2(nr_samples)-1)
60 struct blitter_context_priv
62 struct blitter_context base
;
64 float vertices
[4][2][4]; /**< {pos, color} or {pos, texcoord} */
66 /* Templates for various state objects. */
68 /* Constant state objects. */
70 void *vs
; /**< Vertex shader which passes {pos, generic} to the output.*/
72 void *vs_pos_only
[4]; /**< Vertex shader which passes pos to the output
73 for clear_buffer/copy_buffer.*/
74 void *vs_layered
; /**< Vertex shader which sets LAYER = INSTANCEID. */
76 /* Fragment shaders. */
78 void *fs_write_one_cbuf
;
79 void *fs_write_all_cbufs
;
81 /* FS which outputs a color from a texture where
82 * the 1st index indicates the texture type / destination type,
83 * the 2nd index is the PIPE_TEXTURE_* to be sampled,
84 * the 3rd index is 0 = use TEX, 1 = use TXF.
86 void *fs_texfetch_col
[5][PIPE_MAX_TEXTURE_TYPES
][2];
88 /* FS which outputs a depth from a texture, where
89 * the 1st index is the PIPE_TEXTURE_* to be sampled,
90 * the 2nd index is 0 = use TEX, 1 = use TXF.
92 void *fs_texfetch_depth
[PIPE_MAX_TEXTURE_TYPES
][2];
93 void *fs_texfetch_depthstencil
[PIPE_MAX_TEXTURE_TYPES
][2];
94 void *fs_texfetch_stencil
[PIPE_MAX_TEXTURE_TYPES
][2];
96 /* FS which outputs one sample from a multisample texture. */
97 void *fs_texfetch_col_msaa
[5][PIPE_MAX_TEXTURE_TYPES
];
98 void *fs_texfetch_depth_msaa
[PIPE_MAX_TEXTURE_TYPES
];
99 void *fs_texfetch_depthstencil_msaa
[PIPE_MAX_TEXTURE_TYPES
];
100 void *fs_texfetch_stencil_msaa
[PIPE_MAX_TEXTURE_TYPES
];
102 /* FS which outputs an average of all samples. */
103 void *fs_resolve
[PIPE_MAX_TEXTURE_TYPES
][NUM_RESOLVE_FRAG_SHADERS
][2];
106 void *blend
[PIPE_MASK_RGBA
+1][2]; /**< blend state with writemask */
107 void *blend_clear
[GET_CLEAR_BLEND_STATE_IDX(PIPE_CLEAR_COLOR
)+1];
109 /* Depth stencil alpha state. */
110 void *dsa_write_depth_stencil
;
111 void *dsa_write_depth_keep_stencil
;
112 void *dsa_keep_depth_stencil
;
113 void *dsa_keep_depth_write_stencil
;
115 /* Vertex elements states. */
117 void *velem_state_readbuf
[4]; /**< X, XY, XYZ, XYZW */
121 void *sampler_state_linear
;
122 void *sampler_state_rect
;
123 void *sampler_state_rect_linear
;
125 /* Rasterizer state. */
126 void *rs_state
, *rs_state_scissor
, *rs_discard_state
;
128 /* Viewport state. */
129 struct pipe_viewport_state viewport
;
131 /* Destination surface dimensions. */
135 bool has_geometry_shader
;
136 bool has_tessellation
;
139 bool has_stencil_export
;
140 bool has_texture_multisample
;
143 bool cube_as_2darray
;
144 bool cached_all_shaders
;
146 /* The Draw module overrides these functions.
147 * Always create the blitter before Draw. */
148 void (*bind_fs_state
)(struct pipe_context
*, void *);
149 void (*delete_fs_state
)(struct pipe_context
*, void *);
152 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
)
154 struct blitter_context_priv
*ctx
;
155 struct pipe_blend_state blend
;
156 struct pipe_depth_stencil_alpha_state dsa
;
157 struct pipe_rasterizer_state rs_state
;
158 struct pipe_sampler_state sampler_state
;
159 struct pipe_vertex_element velem
[2];
162 ctx
= CALLOC_STRUCT(blitter_context_priv
);
166 ctx
->base
.pipe
= pipe
;
167 ctx
->base
.draw_rectangle
= util_blitter_draw_rectangle
;
169 ctx
->bind_fs_state
= pipe
->bind_fs_state
;
170 ctx
->delete_fs_state
= pipe
->delete_fs_state
;
172 /* init state objects for them to be considered invalid */
173 ctx
->base
.saved_blend_state
= INVALID_PTR
;
174 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
175 ctx
->base
.saved_rs_state
= INVALID_PTR
;
176 ctx
->base
.saved_fs
= INVALID_PTR
;
177 ctx
->base
.saved_vs
= INVALID_PTR
;
178 ctx
->base
.saved_gs
= INVALID_PTR
;
179 ctx
->base
.saved_velem_state
= INVALID_PTR
;
180 ctx
->base
.saved_fb_state
.nr_cbufs
= ~0;
181 ctx
->base
.saved_num_sampler_views
= ~0;
182 ctx
->base
.saved_num_sampler_states
= ~0;
183 ctx
->base
.saved_num_so_targets
= ~0;
185 ctx
->has_geometry_shader
=
186 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
187 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
189 ctx
->has_tessellation
=
190 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_TESS_CTRL
,
191 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
193 ctx
->has_stream_out
=
194 pipe
->screen
->get_param(pipe
->screen
,
195 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0;
197 ctx
->has_stencil_export
=
198 pipe
->screen
->get_param(pipe
->screen
,
199 PIPE_CAP_SHADER_STENCIL_EXPORT
);
201 ctx
->has_texture_multisample
=
202 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TEXTURE_MULTISAMPLE
);
204 ctx
->has_tex_lz
= pipe
->screen
->get_param(pipe
->screen
,
205 PIPE_CAP_TGSI_TEX_TXF_LZ
);
206 ctx
->has_txf
= pipe
->screen
->get_param(pipe
->screen
,
207 PIPE_CAP_GLSL_FEATURE_LEVEL
) > 130;
208 ctx
->cube_as_2darray
= pipe
->screen
->get_param(pipe
->screen
,
209 PIPE_CAP_SAMPLER_VIEW_TARGET
);
211 /* blend state objects */
212 memset(&blend
, 0, sizeof(blend
));
214 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
215 for (j
= 0; j
< 2; j
++) {
216 memset(&blend
.rt
[0], 0, sizeof(blend
.rt
[0]));
217 blend
.rt
[0].colormask
= i
;
219 blend
.rt
[0].blend_enable
= 1;
220 blend
.rt
[0].rgb_func
= PIPE_BLEND_ADD
;
221 blend
.rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_SRC_ALPHA
;
222 blend
.rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
223 blend
.rt
[0].alpha_func
= PIPE_BLEND_ADD
;
224 blend
.rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_SRC_ALPHA
;
225 blend
.rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
227 ctx
->blend
[i
][j
] = pipe
->create_blend_state(pipe
, &blend
);
231 /* depth stencil alpha state objects */
232 memset(&dsa
, 0, sizeof(dsa
));
233 ctx
->dsa_keep_depth_stencil
=
234 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
236 dsa
.depth
.enabled
= 1;
237 dsa
.depth
.writemask
= 1;
238 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
239 ctx
->dsa_write_depth_keep_stencil
=
240 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
242 dsa
.stencil
[0].enabled
= 1;
243 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
244 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
245 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
246 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
247 dsa
.stencil
[0].valuemask
= 0xff;
248 dsa
.stencil
[0].writemask
= 0xff;
249 ctx
->dsa_write_depth_stencil
=
250 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
252 dsa
.depth
.enabled
= 0;
253 dsa
.depth
.writemask
= 0;
254 ctx
->dsa_keep_depth_write_stencil
=
255 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
258 memset(&sampler_state
, 0, sizeof(sampler_state
));
259 sampler_state
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
260 sampler_state
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
261 sampler_state
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
262 sampler_state
.normalized_coords
= 1;
263 ctx
->sampler_state
= pipe
->create_sampler_state(pipe
, &sampler_state
);
264 sampler_state
.normalized_coords
= 0;
265 ctx
->sampler_state_rect
= pipe
->create_sampler_state(pipe
, &sampler_state
);
267 sampler_state
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
268 sampler_state
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
269 sampler_state
.normalized_coords
= 1;
270 ctx
->sampler_state_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
271 sampler_state
.normalized_coords
= 0;
272 ctx
->sampler_state_rect_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
274 /* rasterizer state */
275 memset(&rs_state
, 0, sizeof(rs_state
));
276 rs_state
.cull_face
= PIPE_FACE_NONE
;
277 rs_state
.half_pixel_center
= 1;
278 rs_state
.bottom_edge_rule
= 1;
279 rs_state
.flatshade
= 1;
280 rs_state
.depth_clip
= 1;
281 ctx
->rs_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
283 rs_state
.scissor
= 1;
284 ctx
->rs_state_scissor
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
286 if (ctx
->has_stream_out
) {
287 rs_state
.scissor
= 0;
288 rs_state
.rasterizer_discard
= 1;
289 ctx
->rs_discard_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
292 ctx
->base
.cb_slot
= 0; /* 0 for now */
293 ctx
->base
.vb_slot
= 0; /* 0 for now */
295 /* vertex elements states */
296 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
297 for (i
= 0; i
< 2; i
++) {
298 velem
[i
].src_offset
= i
* 4 * sizeof(float);
299 velem
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
300 velem
[i
].vertex_buffer_index
= ctx
->base
.vb_slot
;
302 ctx
->velem_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
304 if (ctx
->has_stream_out
) {
305 static enum pipe_format formats
[4] = {
306 PIPE_FORMAT_R32_UINT
,
307 PIPE_FORMAT_R32G32_UINT
,
308 PIPE_FORMAT_R32G32B32_UINT
,
309 PIPE_FORMAT_R32G32B32A32_UINT
312 for (i
= 0; i
< 4; i
++) {
313 velem
[0].src_format
= formats
[i
];
314 velem
[0].vertex_buffer_index
= ctx
->base
.vb_slot
;
315 ctx
->velem_state_readbuf
[i
] =
316 pipe
->create_vertex_elements_state(pipe
, 1, &velem
[0]);
321 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_INSTANCEID
) &&
322 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT
);
324 /* set invariant vertex coordinates */
325 for (i
= 0; i
< 4; i
++)
326 ctx
->vertices
[i
][0][3] = 1; /*v.w*/
331 static void bind_vs_pos_only(struct blitter_context_priv
*ctx
,
332 unsigned num_so_channels
)
334 struct pipe_context
*pipe
= ctx
->base
.pipe
;
335 int index
= num_so_channels
? num_so_channels
- 1 : 0;
337 if (!ctx
->vs_pos_only
[index
]) {
338 struct pipe_stream_output_info so
;
339 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
};
340 const uint semantic_indices
[] = { 0 };
342 memset(&so
, 0, sizeof(so
));
344 so
.output
[0].num_components
= num_so_channels
;
345 so
.stride
[0] = num_so_channels
;
347 ctx
->vs_pos_only
[index
] =
348 util_make_vertex_passthrough_shader_with_so(pipe
, 1, semantic_names
,
349 semantic_indices
, false,
353 pipe
->bind_vs_state(pipe
, ctx
->vs_pos_only
[index
]);
356 static void bind_vs_passthrough_pos_generic(struct blitter_context_priv
*ctx
)
358 struct pipe_context
*pipe
= ctx
->base
.pipe
;
361 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
362 TGSI_SEMANTIC_GENERIC
};
363 const uint semantic_indices
[] = { 0, 0 };
365 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
366 semantic_indices
, false);
369 pipe
->bind_vs_state(pipe
, ctx
->vs
);
372 static void bind_vs_passthrough_pos(struct blitter_context_priv
*ctx
)
374 struct pipe_context
*pipe
= ctx
->base
.pipe
;
376 if (!ctx
->vs_nogeneric
) {
377 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
};
378 const uint semantic_indices
[] = { 0 };
381 util_make_vertex_passthrough_shader(pipe
, 1,
383 semantic_indices
, false);
386 pipe
->bind_vs_state(pipe
, ctx
->vs_nogeneric
);
389 static void bind_vs_layered(struct blitter_context_priv
*ctx
)
391 struct pipe_context
*pipe
= ctx
->base
.pipe
;
393 if (!ctx
->vs_layered
) {
394 ctx
->vs_layered
= util_make_layered_clear_vertex_shader(pipe
);
397 pipe
->bind_vs_state(pipe
, ctx
->vs_layered
);
400 static void bind_fs_empty(struct blitter_context_priv
*ctx
)
402 struct pipe_context
*pipe
= ctx
->base
.pipe
;
404 if (!ctx
->fs_empty
) {
405 assert(!ctx
->cached_all_shaders
);
406 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
409 ctx
->bind_fs_state(pipe
, ctx
->fs_empty
);
412 static void bind_fs_write_one_cbuf(struct blitter_context_priv
*ctx
)
414 struct pipe_context
*pipe
= ctx
->base
.pipe
;
416 if (!ctx
->fs_write_one_cbuf
) {
417 assert(!ctx
->cached_all_shaders
);
418 ctx
->fs_write_one_cbuf
=
419 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
420 TGSI_INTERPOLATE_CONSTANT
, false);
423 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
426 static void bind_fs_write_all_cbufs(struct blitter_context_priv
*ctx
)
428 struct pipe_context
*pipe
= ctx
->base
.pipe
;
430 if (!ctx
->fs_write_all_cbufs
) {
431 assert(!ctx
->cached_all_shaders
);
432 ctx
->fs_write_all_cbufs
=
433 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
434 TGSI_INTERPOLATE_CONSTANT
, true);
437 ctx
->bind_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
440 void util_blitter_destroy(struct blitter_context
*blitter
)
442 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
443 struct pipe_context
*pipe
= blitter
->pipe
;
446 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++)
447 for (j
= 0; j
< 2; j
++)
448 pipe
->delete_blend_state(pipe
, ctx
->blend
[i
][j
]);
450 for (i
= 0; i
< ARRAY_SIZE(ctx
->blend_clear
); i
++) {
451 if (ctx
->blend_clear
[i
])
452 pipe
->delete_blend_state(pipe
, ctx
->blend_clear
[i
]);
454 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
455 pipe
->delete_depth_stencil_alpha_state(pipe
,
456 ctx
->dsa_write_depth_keep_stencil
);
457 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
458 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
460 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
);
461 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state_scissor
);
462 if (ctx
->rs_discard_state
)
463 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_discard_state
);
465 pipe
->delete_vs_state(pipe
, ctx
->vs
);
466 if (ctx
->vs_nogeneric
)
467 pipe
->delete_vs_state(pipe
, ctx
->vs_nogeneric
);
468 for (i
= 0; i
< 4; i
++)
469 if (ctx
->vs_pos_only
[i
])
470 pipe
->delete_vs_state(pipe
, ctx
->vs_pos_only
[i
]);
472 pipe
->delete_vs_state(pipe
, ctx
->vs_layered
);
473 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state
);
474 for (i
= 0; i
< 4; i
++) {
475 if (ctx
->velem_state_readbuf
[i
]) {
476 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[i
]);
480 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
481 for (unsigned type
= 0; type
< ARRAY_SIZE(ctx
->fs_texfetch_col
); ++type
) {
482 for (unsigned inst
= 0; inst
< 2; inst
++) {
483 if (ctx
->fs_texfetch_col
[type
][i
][inst
])
484 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[type
][i
][inst
]);
486 if (ctx
->fs_texfetch_col_msaa
[type
][i
])
487 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col_msaa
[type
][i
]);
490 for (unsigned inst
= 0; inst
< 2; inst
++) {
491 if (ctx
->fs_texfetch_depth
[i
][inst
])
492 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
][inst
]);
493 if (ctx
->fs_texfetch_depthstencil
[i
][inst
])
494 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil
[i
][inst
]);
495 if (ctx
->fs_texfetch_stencil
[i
][inst
])
496 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil
[i
][inst
]);
499 if (ctx
->fs_texfetch_depth_msaa
[i
])
500 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth_msaa
[i
]);
501 if (ctx
->fs_texfetch_depthstencil_msaa
[i
])
502 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil_msaa
[i
]);
503 if (ctx
->fs_texfetch_stencil_msaa
[i
])
504 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil_msaa
[i
]);
506 for (j
= 0; j
< ARRAY_SIZE(ctx
->fs_resolve
[i
]); j
++)
507 for (f
= 0; f
< 2; f
++)
508 if (ctx
->fs_resolve
[i
][j
][f
])
509 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve
[i
][j
][f
]);
513 ctx
->delete_fs_state(pipe
, ctx
->fs_empty
);
514 if (ctx
->fs_write_one_cbuf
)
515 ctx
->delete_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
516 if (ctx
->fs_write_all_cbufs
)
517 ctx
->delete_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
519 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect_linear
);
520 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect
);
521 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_linear
);
522 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
);
526 void util_blitter_set_texture_multisample(struct blitter_context
*blitter
,
529 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
531 ctx
->has_texture_multisample
= supported
;
534 void util_blitter_set_running_flag(struct blitter_context
*blitter
)
536 if (blitter
->running
) {
537 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
540 blitter
->running
= true;
542 blitter
->pipe
->set_active_query_state(blitter
->pipe
, false);
545 void util_blitter_unset_running_flag(struct blitter_context
*blitter
)
547 if (!blitter
->running
) {
548 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
551 blitter
->running
= false;
553 blitter
->pipe
->set_active_query_state(blitter
->pipe
, true);
556 static void blitter_check_saved_vertex_states(struct blitter_context_priv
*ctx
)
558 assert(ctx
->base
.saved_velem_state
!= INVALID_PTR
);
559 assert(ctx
->base
.saved_vs
!= INVALID_PTR
);
560 assert(!ctx
->has_geometry_shader
|| ctx
->base
.saved_gs
!= INVALID_PTR
);
561 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tcs
!= INVALID_PTR
);
562 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tes
!= INVALID_PTR
);
563 assert(!ctx
->has_stream_out
|| ctx
->base
.saved_num_so_targets
!= ~0u);
564 assert(ctx
->base
.saved_rs_state
!= INVALID_PTR
);
567 void util_blitter_restore_vertex_states(struct blitter_context
*blitter
)
569 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
570 struct pipe_context
*pipe
= ctx
->base
.pipe
;
574 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1,
575 &ctx
->base
.saved_vertex_buffer
);
576 pipe_vertex_buffer_unreference(&ctx
->base
.saved_vertex_buffer
);
578 /* Vertex elements. */
579 pipe
->bind_vertex_elements_state(pipe
, ctx
->base
.saved_velem_state
);
580 ctx
->base
.saved_velem_state
= INVALID_PTR
;
583 pipe
->bind_vs_state(pipe
, ctx
->base
.saved_vs
);
584 ctx
->base
.saved_vs
= INVALID_PTR
;
586 /* Geometry shader. */
587 if (ctx
->has_geometry_shader
) {
588 pipe
->bind_gs_state(pipe
, ctx
->base
.saved_gs
);
589 ctx
->base
.saved_gs
= INVALID_PTR
;
592 if (ctx
->has_tessellation
) {
593 pipe
->bind_tcs_state(pipe
, ctx
->base
.saved_tcs
);
594 pipe
->bind_tes_state(pipe
, ctx
->base
.saved_tes
);
595 ctx
->base
.saved_tcs
= INVALID_PTR
;
596 ctx
->base
.saved_tes
= INVALID_PTR
;
599 /* Stream outputs. */
600 if (ctx
->has_stream_out
) {
601 unsigned offsets
[PIPE_MAX_SO_BUFFERS
];
602 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
603 offsets
[i
] = (unsigned)-1;
604 pipe
->set_stream_output_targets(pipe
,
605 ctx
->base
.saved_num_so_targets
,
606 ctx
->base
.saved_so_targets
, offsets
);
608 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
609 pipe_so_target_reference(&ctx
->base
.saved_so_targets
[i
], NULL
);
611 ctx
->base
.saved_num_so_targets
= ~0;
615 pipe
->bind_rasterizer_state(pipe
, ctx
->base
.saved_rs_state
);
616 ctx
->base
.saved_rs_state
= INVALID_PTR
;
619 static void blitter_check_saved_fragment_states(struct blitter_context_priv
*ctx
)
621 assert(ctx
->base
.saved_fs
!= INVALID_PTR
);
622 assert(ctx
->base
.saved_dsa_state
!= INVALID_PTR
);
623 assert(ctx
->base
.saved_blend_state
!= INVALID_PTR
);
626 void util_blitter_restore_fragment_states(struct blitter_context
*blitter
)
628 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
629 struct pipe_context
*pipe
= ctx
->base
.pipe
;
631 /* Fragment shader. */
632 ctx
->bind_fs_state(pipe
, ctx
->base
.saved_fs
);
633 ctx
->base
.saved_fs
= INVALID_PTR
;
635 /* Depth, stencil, alpha. */
636 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->base
.saved_dsa_state
);
637 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
640 pipe
->bind_blend_state(pipe
, ctx
->base
.saved_blend_state
);
641 ctx
->base
.saved_blend_state
= INVALID_PTR
;
644 if (ctx
->base
.is_sample_mask_saved
) {
645 pipe
->set_sample_mask(pipe
, ctx
->base
.saved_sample_mask
);
646 ctx
->base
.is_sample_mask_saved
= false;
649 /* Miscellaneous states. */
650 /* XXX check whether these are saved and whether they need to be restored
651 * (depending on the operation) */
652 pipe
->set_stencil_ref(pipe
, &ctx
->base
.saved_stencil_ref
);
653 pipe
->set_viewport_states(pipe
, 0, 1, &ctx
->base
.saved_viewport
);
656 static void blitter_check_saved_fb_state(struct blitter_context_priv
*ctx
)
658 assert(ctx
->base
.saved_fb_state
.nr_cbufs
!= ~0u);
661 static void blitter_disable_render_cond(struct blitter_context_priv
*ctx
)
663 struct pipe_context
*pipe
= ctx
->base
.pipe
;
665 if (ctx
->base
.saved_render_cond_query
) {
666 pipe
->render_condition(pipe
, NULL
, false, 0);
670 void util_blitter_restore_render_cond(struct blitter_context
*blitter
)
672 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
673 struct pipe_context
*pipe
= ctx
->base
.pipe
;
675 if (ctx
->base
.saved_render_cond_query
) {
676 pipe
->render_condition(pipe
, ctx
->base
.saved_render_cond_query
,
677 ctx
->base
.saved_render_cond_cond
,
678 ctx
->base
.saved_render_cond_mode
);
679 ctx
->base
.saved_render_cond_query
= NULL
;
683 void util_blitter_restore_fb_state(struct blitter_context
*blitter
)
685 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
686 struct pipe_context
*pipe
= ctx
->base
.pipe
;
688 pipe
->set_framebuffer_state(pipe
, &ctx
->base
.saved_fb_state
);
689 util_unreference_framebuffer_state(&ctx
->base
.saved_fb_state
);
692 static void blitter_check_saved_textures(struct blitter_context_priv
*ctx
)
694 assert(ctx
->base
.saved_num_sampler_states
!= ~0u);
695 assert(ctx
->base
.saved_num_sampler_views
!= ~0u);
698 void util_blitter_restore_textures(struct blitter_context
*blitter
)
700 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
701 struct pipe_context
*pipe
= ctx
->base
.pipe
;
704 /* Fragment sampler states. */
705 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0,
706 ctx
->base
.saved_num_sampler_states
,
707 ctx
->base
.saved_sampler_states
);
709 ctx
->base
.saved_num_sampler_states
= ~0;
711 /* Fragment sampler views. */
712 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
713 ctx
->base
.saved_num_sampler_views
,
714 ctx
->base
.saved_sampler_views
);
716 for (i
= 0; i
< ctx
->base
.saved_num_sampler_views
; i
++)
717 pipe_sampler_view_reference(&ctx
->base
.saved_sampler_views
[i
], NULL
);
719 ctx
->base
.saved_num_sampler_views
= ~0;
722 void util_blitter_restore_constant_buffer_state(struct blitter_context
*blitter
)
724 struct pipe_context
*pipe
= blitter
->pipe
;
726 pipe
->set_constant_buffer(pipe
, PIPE_SHADER_FRAGMENT
, blitter
->cb_slot
,
727 &blitter
->saved_fs_constant_buffer
);
728 pipe_resource_reference(&blitter
->saved_fs_constant_buffer
.buffer
, NULL
);
731 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
732 int x1
, int y1
, int x2
, int y2
,
737 /* set vertex positions */
738 ctx
->vertices
[0][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v0.x*/
739 ctx
->vertices
[0][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v0.y*/
741 ctx
->vertices
[1][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v1.x*/
742 ctx
->vertices
[1][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v1.y*/
744 ctx
->vertices
[2][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v2.x*/
745 ctx
->vertices
[2][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v2.y*/
747 ctx
->vertices
[3][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v3.x*/
748 ctx
->vertices
[3][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v3.y*/
750 for (i
= 0; i
< 4; i
++)
751 ctx
->vertices
[i
][0][2] = depth
; /*z*/
754 ctx
->viewport
.scale
[0] = 0.5f
* ctx
->dst_width
;
755 ctx
->viewport
.scale
[1] = 0.5f
* ctx
->dst_height
;
756 ctx
->viewport
.scale
[2] = 1.0f
;
757 ctx
->viewport
.translate
[0] = 0.5f
* ctx
->dst_width
;
758 ctx
->viewport
.translate
[1] = 0.5f
* ctx
->dst_height
;
759 ctx
->viewport
.translate
[2] = 0.0f
;
760 ctx
->base
.pipe
->set_viewport_states(ctx
->base
.pipe
, 0, 1, &ctx
->viewport
);
763 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
764 const float color
[4])
769 for (i
= 0; i
< 4; i
++)
770 memcpy(&ctx
->vertices
[i
][1][0], color
, sizeof(uint32_t) * 4);
772 for (i
= 0; i
< 4; i
++)
773 memset(&ctx
->vertices
[i
][1][0], 0, sizeof(uint32_t) * 4);
777 static void get_texcoords(struct pipe_sampler_view
*src
,
778 unsigned src_width0
, unsigned src_height0
,
779 int x1
, int y1
, int x2
, int y2
,
780 float layer
, unsigned sample
,
781 bool uses_txf
, union blitter_attrib
*out
)
783 unsigned level
= src
->u
.tex
.first_level
;
784 bool normalized
= !uses_txf
&&
785 src
->target
!= PIPE_TEXTURE_RECT
&&
786 src
->texture
->nr_samples
<= 1;
789 out
->texcoord
.x1
= x1
/ (float)u_minify(src_width0
, level
);
790 out
->texcoord
.y1
= y1
/ (float)u_minify(src_height0
, level
);
791 out
->texcoord
.x2
= x2
/ (float)u_minify(src_width0
, level
);
792 out
->texcoord
.y2
= y2
/ (float)u_minify(src_height0
, level
);
794 out
->texcoord
.x1
= x1
;
795 out
->texcoord
.y1
= y1
;
796 out
->texcoord
.x2
= x2
;
797 out
->texcoord
.y2
= y2
;
804 switch (src
->target
) {
805 case PIPE_TEXTURE_3D
:
810 r
/= u_minify(src
->texture
->depth0
, src
->u
.tex
.first_level
);
816 case PIPE_TEXTURE_1D_ARRAY
:
817 out
->texcoord
.y1
= out
->texcoord
.y2
= layer
;
820 case PIPE_TEXTURE_2D_ARRAY
:
821 out
->texcoord
.z
= layer
;
822 out
->texcoord
.w
= sample
;
825 case PIPE_TEXTURE_CUBE_ARRAY
:
826 out
->texcoord
.w
= (unsigned)layer
/ 6;
829 case PIPE_TEXTURE_2D
:
830 out
->texcoord
.w
= sample
;
837 static void blitter_set_dst_dimensions(struct blitter_context_priv
*ctx
,
838 unsigned width
, unsigned height
)
840 ctx
->dst_width
= width
;
841 ctx
->dst_height
= height
;
844 static void set_texcoords_in_vertices(const union blitter_attrib
*attrib
,
845 float *out
, unsigned stride
)
847 out
[0] = attrib
->texcoord
.x1
;
848 out
[1] = attrib
->texcoord
.y1
;
850 out
[0] = attrib
->texcoord
.x2
;
851 out
[1] = attrib
->texcoord
.y1
;
853 out
[0] = attrib
->texcoord
.x2
;
854 out
[1] = attrib
->texcoord
.y2
;
856 out
[0] = attrib
->texcoord
.x1
;
857 out
[1] = attrib
->texcoord
.y2
;
860 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
861 enum pipe_format src_format
,
862 enum pipe_format dst_format
,
863 enum pipe_texture_target target
,
864 unsigned src_nr_samples
,
865 unsigned dst_nr_samples
,
869 struct pipe_context
*pipe
= ctx
->base
.pipe
;
870 enum tgsi_texture_type tgsi_tex
=
871 util_pipe_tex_to_tgsi_tex(target
, src_nr_samples
);
872 enum tgsi_return_type stype
;
873 enum tgsi_return_type dtype
;
876 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
878 if (util_format_is_pure_uint(src_format
)) {
879 stype
= TGSI_RETURN_TYPE_UINT
;
880 if (util_format_is_pure_uint(dst_format
)) {
881 dtype
= TGSI_RETURN_TYPE_UINT
;
884 assert(util_format_is_pure_sint(dst_format
));
885 dtype
= TGSI_RETURN_TYPE_SINT
;
888 } else if (util_format_is_pure_sint(src_format
)) {
889 stype
= TGSI_RETURN_TYPE_SINT
;
890 if (util_format_is_pure_sint(dst_format
)) {
891 dtype
= TGSI_RETURN_TYPE_SINT
;
894 assert(util_format_is_pure_uint(dst_format
));
895 dtype
= TGSI_RETURN_TYPE_UINT
;
899 assert(!util_format_is_pure_uint(dst_format
) &&
900 !util_format_is_pure_sint(dst_format
));
901 dtype
= stype
= TGSI_RETURN_TYPE_FLOAT
;
905 if (src_nr_samples
> 1) {
908 /* OpenGL requires that integer textures just copy 1 sample instead
911 if (dst_nr_samples
<= 1 &&
912 stype
!= TGSI_RETURN_TYPE_UINT
&&
913 stype
!= TGSI_RETURN_TYPE_SINT
) {
914 /* The destination has one sample, so we'll do color resolve. */
915 unsigned index
= GET_MSAA_RESOLVE_FS_IDX(src_nr_samples
);
919 shader
= &ctx
->fs_resolve
[target
][index
][filter
];
922 assert(!ctx
->cached_all_shaders
);
923 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
924 *shader
= util_make_fs_msaa_resolve_bilinear(pipe
, tgsi_tex
,
929 *shader
= util_make_fs_msaa_resolve(pipe
, tgsi_tex
,
936 /* The destination has multiple samples, we'll do
937 * an MSAA->MSAA copy.
939 shader
= &ctx
->fs_texfetch_col_msaa
[type
][target
];
941 /* Create the fragment shader on-demand. */
943 assert(!ctx
->cached_all_shaders
);
944 *shader
= util_make_fs_blit_msaa_color(pipe
, tgsi_tex
, stype
, dtype
);
953 shader
= &ctx
->fs_texfetch_col
[type
][target
][1];
955 shader
= &ctx
->fs_texfetch_col
[type
][target
][0];
957 /* Create the fragment shader on-demand. */
959 assert(!ctx
->cached_all_shaders
);
960 *shader
= util_make_fragment_tex_shader(pipe
, tgsi_tex
,
961 TGSI_INTERPOLATE_LINEAR
,
963 ctx
->has_tex_lz
, use_txf
);
971 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
972 enum pipe_texture_target target
,
976 struct pipe_context
*pipe
= ctx
->base
.pipe
;
978 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
980 if (nr_samples
> 1) {
981 void **shader
= &ctx
->fs_texfetch_depth_msaa
[target
];
983 /* Create the fragment shader on-demand. */
985 enum tgsi_texture_type tgsi_tex
;
986 assert(!ctx
->cached_all_shaders
);
987 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
988 *shader
= util_make_fs_blit_msaa_depth(pipe
, tgsi_tex
);
996 shader
= &ctx
->fs_texfetch_depth
[target
][1];
998 shader
= &ctx
->fs_texfetch_depth
[target
][0];
1000 /* Create the fragment shader on-demand. */
1002 enum tgsi_texture_type tgsi_tex
;
1003 assert(!ctx
->cached_all_shaders
);
1004 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1006 util_make_fragment_tex_shader_writedepth(pipe
, tgsi_tex
,
1007 TGSI_INTERPOLATE_LINEAR
,
1008 ctx
->has_tex_lz
, use_txf
);
1016 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv
*ctx
,
1017 enum pipe_texture_target target
,
1018 unsigned nr_samples
,
1021 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1023 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1025 if (nr_samples
> 1) {
1026 void **shader
= &ctx
->fs_texfetch_depthstencil_msaa
[target
];
1028 /* Create the fragment shader on-demand. */
1030 enum tgsi_texture_type tgsi_tex
;
1031 assert(!ctx
->cached_all_shaders
);
1032 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1033 *shader
= util_make_fs_blit_msaa_depthstencil(pipe
, tgsi_tex
);
1041 shader
= &ctx
->fs_texfetch_depthstencil
[target
][1];
1043 shader
= &ctx
->fs_texfetch_depthstencil
[target
][0];
1045 /* Create the fragment shader on-demand. */
1047 enum tgsi_texture_type tgsi_tex
;
1048 assert(!ctx
->cached_all_shaders
);
1049 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1051 util_make_fragment_tex_shader_writedepthstencil(pipe
, tgsi_tex
,
1052 TGSI_INTERPOLATE_LINEAR
,
1062 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv
*ctx
,
1063 enum pipe_texture_target target
,
1064 unsigned nr_samples
,
1067 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1069 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1071 if (nr_samples
> 1) {
1072 void **shader
= &ctx
->fs_texfetch_stencil_msaa
[target
];
1074 /* Create the fragment shader on-demand. */
1076 enum tgsi_texture_type tgsi_tex
;
1077 assert(!ctx
->cached_all_shaders
);
1078 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1079 *shader
= util_make_fs_blit_msaa_stencil(pipe
, tgsi_tex
);
1087 shader
= &ctx
->fs_texfetch_stencil
[target
][1];
1089 shader
= &ctx
->fs_texfetch_stencil
[target
][0];
1091 /* Create the fragment shader on-demand. */
1093 enum tgsi_texture_type tgsi_tex
;
1094 assert(!ctx
->cached_all_shaders
);
1095 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1097 util_make_fragment_tex_shader_writestencil(pipe
, tgsi_tex
,
1098 TGSI_INTERPOLATE_LINEAR
,
1099 ctx
->has_tex_lz
, use_txf
);
1108 * Generate and save all fragment shaders that we will ever need for
1109 * blitting. Drivers which use the 'draw' fallbacks will typically use
1110 * this to make sure we generate/use shaders that don't go through the
1111 * draw module's wrapper functions.
1113 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
)
1115 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1116 struct pipe_context
*pipe
= blitter
->pipe
;
1117 struct pipe_screen
*screen
= pipe
->screen
;
1118 unsigned samples
, j
, f
, target
, max_samples
, use_txf
;
1119 bool has_arraytex
, has_cubearraytex
;
1121 max_samples
= ctx
->has_texture_multisample
? 2 : 1;
1122 has_arraytex
= screen
->get_param(screen
,
1123 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
) != 0;
1124 has_cubearraytex
= screen
->get_param(screen
,
1125 PIPE_CAP_CUBE_MAP_ARRAY
) != 0;
1127 /* It only matters if i <= 1 or > 1. */
1128 for (samples
= 1; samples
<= max_samples
; samples
++) {
1129 for (target
= PIPE_TEXTURE_1D
; target
< PIPE_MAX_TEXTURE_TYPES
; target
++) {
1130 for (use_txf
= 0; use_txf
<= ctx
->has_txf
; use_txf
++) {
1131 if (!has_arraytex
&&
1132 (target
== PIPE_TEXTURE_1D_ARRAY
||
1133 target
== PIPE_TEXTURE_2D_ARRAY
)) {
1136 if (!has_cubearraytex
&&
1137 (target
== PIPE_TEXTURE_CUBE_ARRAY
))
1141 (target
!= PIPE_TEXTURE_2D
&&
1142 target
!= PIPE_TEXTURE_2D_ARRAY
))
1145 if (samples
> 1 && use_txf
)
1146 continue; /* TXF is the only option, use_txf has no effect */
1148 /* If samples == 1, the shaders read one texel. If samples >= 1,
1149 * they read one sample.
1151 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
,
1152 PIPE_FORMAT_R32_FLOAT
, target
,
1153 samples
, samples
, 0, use_txf
);
1154 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1155 PIPE_FORMAT_R32_UINT
, target
,
1156 samples
, samples
, 0, use_txf
);
1157 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1158 PIPE_FORMAT_R32_SINT
, target
,
1159 samples
, samples
, 0, use_txf
);
1160 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1161 PIPE_FORMAT_R32_SINT
, target
,
1162 samples
, samples
, 0, use_txf
);
1163 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1164 PIPE_FORMAT_R32_UINT
, target
,
1165 samples
, samples
, 0, use_txf
);
1166 blitter_get_fs_texfetch_depth(ctx
, target
, samples
, use_txf
);
1167 if (ctx
->has_stencil_export
) {
1168 blitter_get_fs_texfetch_depthstencil(ctx
, target
, samples
, use_txf
);
1169 blitter_get_fs_texfetch_stencil(ctx
, target
, samples
, use_txf
);
1175 /* MSAA resolve shaders. */
1176 for (j
= 2; j
< 32; j
++) {
1177 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R32_FLOAT
,
1179 PIPE_BIND_SAMPLER_VIEW
)) {
1183 for (f
= 0; f
< 2; f
++) {
1184 if (f
!= PIPE_TEX_FILTER_NEAREST
&& use_txf
)
1187 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
,
1188 PIPE_FORMAT_R32_FLOAT
, target
,
1190 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1191 PIPE_FORMAT_R32_UINT
, target
,
1193 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1194 PIPE_FORMAT_R32_SINT
, target
,
1202 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
1204 ctx
->fs_write_one_cbuf
=
1205 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1206 TGSI_INTERPOLATE_CONSTANT
, false);
1208 ctx
->fs_write_all_cbufs
=
1209 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1210 TGSI_INTERPOLATE_CONSTANT
, true);
1212 ctx
->cached_all_shaders
= true;
1215 static void blitter_set_common_draw_rect_state(struct blitter_context_priv
*ctx
,
1218 bool vs_pass_generic
)
1220 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1222 pipe
->bind_rasterizer_state(pipe
, scissor
? ctx
->rs_state_scissor
1225 bind_vs_layered(ctx
);
1226 else if (vs_pass_generic
)
1227 bind_vs_passthrough_pos_generic(ctx
);
1229 bind_vs_passthrough_pos(ctx
);
1231 if (ctx
->has_geometry_shader
)
1232 pipe
->bind_gs_state(pipe
, NULL
);
1233 if (ctx
->has_tessellation
) {
1234 pipe
->bind_tcs_state(pipe
, NULL
);
1235 pipe
->bind_tes_state(pipe
, NULL
);
1237 if (ctx
->has_stream_out
)
1238 pipe
->set_stream_output_targets(pipe
, 0, NULL
, NULL
);
1241 static void blitter_draw(struct blitter_context_priv
*ctx
,
1242 int x1
, int y1
, int x2
, int y2
, float depth
,
1243 unsigned num_instances
)
1245 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1246 struct pipe_vertex_buffer vb
= {0};
1248 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
1250 vb
.stride
= 8 * sizeof(float);
1252 u_upload_data(pipe
->stream_uploader
, 0, sizeof(ctx
->vertices
), 4, ctx
->vertices
,
1253 &vb
.buffer_offset
, &vb
.buffer
.resource
);
1254 if (!vb
.buffer
.resource
)
1256 u_upload_unmap(pipe
->stream_uploader
);
1258 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1259 util_draw_arrays_instanced(pipe
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
1261 pipe_resource_reference(&vb
.buffer
.resource
, NULL
);
1264 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
1265 int x1
, int y1
, int x2
, int y2
,
1266 float depth
, unsigned num_instances
,
1267 enum blitter_attrib_type type
,
1268 const union blitter_attrib
*attrib
)
1270 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1274 case UTIL_BLITTER_ATTRIB_COLOR
:
1275 blitter_set_clear_color(ctx
, attrib
->color
);
1278 case UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW
:
1279 for (i
= 0; i
< 4; i
++) {
1280 ctx
->vertices
[i
][1][2] = attrib
->texcoord
.z
;
1281 ctx
->vertices
[i
][1][3] = attrib
->texcoord
.w
;
1284 case UTIL_BLITTER_ATTRIB_TEXCOORD_XY
:
1285 set_texcoords_in_vertices(attrib
, &ctx
->vertices
[0][1][0], 8);
1291 blitter_draw(ctx
, x1
, y1
, x2
, y2
, depth
, num_instances
);
1294 static void *get_clear_blend_state(struct blitter_context_priv
*ctx
,
1295 unsigned clear_buffers
)
1297 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1300 clear_buffers
&= PIPE_CLEAR_COLOR
;
1302 /* Return an existing blend state. */
1304 return ctx
->blend
[0][0];
1306 index
= GET_CLEAR_BLEND_STATE_IDX(clear_buffers
);
1308 if (ctx
->blend_clear
[index
])
1309 return ctx
->blend_clear
[index
];
1311 /* Create a new one. */
1313 struct pipe_blend_state blend
= {0};
1316 blend
.independent_blend_enable
= 1;
1318 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1319 if (clear_buffers
& (PIPE_CLEAR_COLOR0
<< i
)) {
1320 blend
.rt
[i
].colormask
= PIPE_MASK_RGBA
;
1324 ctx
->blend_clear
[index
] = pipe
->create_blend_state(pipe
, &blend
);
1326 return ctx
->blend_clear
[index
];
1329 void util_blitter_common_clear_setup(struct blitter_context
*blitter
,
1330 unsigned width
, unsigned height
,
1331 unsigned clear_buffers
,
1332 void *custom_blend
, void *custom_dsa
)
1334 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1335 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1337 util_blitter_set_running_flag(blitter
);
1338 blitter_check_saved_vertex_states(ctx
);
1339 blitter_check_saved_fragment_states(ctx
);
1340 blitter_disable_render_cond(ctx
);
1344 pipe
->bind_blend_state(pipe
, custom_blend
);
1346 pipe
->bind_blend_state(pipe
, get_clear_blend_state(ctx
, clear_buffers
));
1350 pipe
->bind_depth_stencil_alpha_state(pipe
, custom_dsa
);
1351 } else if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1352 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1353 } else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
1354 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1355 } else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
1356 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1358 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1361 pipe
->set_sample_mask(pipe
, ~0);
1362 blitter_set_dst_dimensions(ctx
, width
, height
);
1365 static void util_blitter_clear_custom(struct blitter_context
*blitter
,
1366 unsigned width
, unsigned height
,
1367 unsigned num_layers
,
1368 unsigned clear_buffers
,
1369 const union pipe_color_union
*color
,
1370 double depth
, unsigned stencil
,
1371 void *custom_blend
, void *custom_dsa
)
1373 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1374 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1375 struct pipe_stencil_ref sr
= { { 0 } };
1377 assert(ctx
->has_layered
|| num_layers
<= 1);
1379 util_blitter_common_clear_setup(blitter
, width
, height
, clear_buffers
,
1380 custom_blend
, custom_dsa
);
1382 sr
.ref_value
[0] = stencil
& 0xff;
1383 pipe
->set_stencil_ref(pipe
, &sr
);
1385 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1386 bind_fs_write_all_cbufs(ctx
);
1388 union blitter_attrib attrib
;
1389 memcpy(attrib
.color
, color
->ui
, sizeof(color
->ui
));
1391 bool pass_generic
= (clear_buffers
& PIPE_CLEAR_COLOR
) != 0;
1392 enum blitter_attrib_type type
= pass_generic
? UTIL_BLITTER_ATTRIB_COLOR
:
1393 UTIL_BLITTER_ATTRIB_NONE
;
1395 if (num_layers
> 1 && ctx
->has_layered
) {
1396 blitter_set_common_draw_rect_state(ctx
, false, true, pass_generic
);
1397 blitter
->draw_rectangle(blitter
, 0, 0, width
, height
, (float) depth
,
1398 num_layers
, type
, &attrib
);
1400 blitter_set_common_draw_rect_state(ctx
, false, false, pass_generic
);
1401 blitter
->draw_rectangle(blitter
, 0, 0, width
, height
, (float) depth
,
1405 util_blitter_restore_vertex_states(blitter
);
1406 util_blitter_restore_fragment_states(blitter
);
1407 util_blitter_restore_render_cond(blitter
);
1408 util_blitter_unset_running_flag(blitter
);
1411 void util_blitter_clear(struct blitter_context
*blitter
,
1412 unsigned width
, unsigned height
, unsigned num_layers
,
1413 unsigned clear_buffers
,
1414 const union pipe_color_union
*color
,
1415 double depth
, unsigned stencil
)
1417 util_blitter_clear_custom(blitter
, width
, height
, num_layers
,
1418 clear_buffers
, color
, depth
, stencil
,
1422 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1423 unsigned width
, unsigned height
,
1424 double depth
, void *custom_dsa
)
1426 static const union pipe_color_union color
;
1427 util_blitter_clear_custom(blitter
, width
, height
, 0, 0, &color
, depth
, 0,
1431 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1432 struct pipe_resource
*dst
,
1436 memset(dst_templ
, 0, sizeof(*dst_templ
));
1437 dst_templ
->format
= util_format_linear(dst
->format
);
1438 dst_templ
->u
.tex
.level
= dstlevel
;
1439 dst_templ
->u
.tex
.first_layer
= dstz
;
1440 dst_templ
->u
.tex
.last_layer
= dstz
;
1443 static struct pipe_surface
*
1444 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
1445 struct pipe_surface
*surf
)
1447 struct pipe_surface dst_templ
;
1449 memset(&dst_templ
, 0, sizeof(dst_templ
));
1450 dst_templ
.format
= surf
->format
;
1451 dst_templ
.u
.tex
.level
= surf
->u
.tex
.level
;
1452 dst_templ
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
+ 1;
1453 dst_templ
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
+ 1;
1455 return pipe
->create_surface(pipe
, surf
->texture
, &dst_templ
);
1458 void util_blitter_default_src_texture(struct blitter_context
*blitter
,
1459 struct pipe_sampler_view
*src_templ
,
1460 struct pipe_resource
*src
,
1463 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1465 memset(src_templ
, 0, sizeof(*src_templ
));
1467 if (ctx
->cube_as_2darray
&&
1468 (src
->target
== PIPE_TEXTURE_CUBE
||
1469 src
->target
== PIPE_TEXTURE_CUBE_ARRAY
))
1470 src_templ
->target
= PIPE_TEXTURE_2D_ARRAY
;
1472 src_templ
->target
= src
->target
;
1474 src_templ
->format
= util_format_linear(src
->format
);
1475 src_templ
->u
.tex
.first_level
= srclevel
;
1476 src_templ
->u
.tex
.last_level
= srclevel
;
1477 src_templ
->u
.tex
.first_layer
= 0;
1478 src_templ
->u
.tex
.last_layer
=
1479 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1480 : src
->array_size
- 1;
1481 src_templ
->swizzle_r
= PIPE_SWIZZLE_X
;
1482 src_templ
->swizzle_g
= PIPE_SWIZZLE_Y
;
1483 src_templ
->swizzle_b
= PIPE_SWIZZLE_Z
;
1484 src_templ
->swizzle_a
= PIPE_SWIZZLE_W
;
1487 static bool is_blit_generic_supported(struct blitter_context
*blitter
,
1488 const struct pipe_resource
*dst
,
1489 enum pipe_format dst_format
,
1490 const struct pipe_resource
*src
,
1491 enum pipe_format src_format
,
1494 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1495 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1499 const struct util_format_description
*desc
=
1500 util_format_description(dst_format
);
1501 bool dst_has_stencil
= util_format_has_stencil(desc
);
1503 /* Stencil export must be supported for stencil copy. */
1504 if ((mask
& PIPE_MASK_S
) && dst_has_stencil
&&
1505 !ctx
->has_stencil_export
) {
1509 if (dst_has_stencil
|| util_format_has_depth(desc
))
1510 bind
= PIPE_BIND_DEPTH_STENCIL
;
1512 bind
= PIPE_BIND_RENDER_TARGET
;
1514 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1515 dst
->nr_samples
, bind
)) {
1521 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1525 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1526 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
)) {
1530 /* Check stencil sampler support for stencil copy. */
1531 if (mask
& PIPE_MASK_S
) {
1532 if (util_format_has_stencil(util_format_description(src_format
))) {
1533 enum pipe_format stencil_format
=
1534 util_format_stencil_only(src_format
);
1535 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1537 if (stencil_format
!= src_format
&&
1538 !screen
->is_format_supported(screen
, stencil_format
,
1539 src
->target
, src
->nr_samples
,
1540 PIPE_BIND_SAMPLER_VIEW
)) {
1550 bool util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1551 const struct pipe_resource
*dst
,
1552 const struct pipe_resource
*src
)
1554 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1555 src
, src
->format
, PIPE_MASK_RGBAZS
);
1558 bool util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1559 const struct pipe_blit_info
*info
)
1561 return is_blit_generic_supported(blitter
,
1562 info
->dst
.resource
, info
->dst
.format
,
1563 info
->src
.resource
, info
->src
.format
,
1567 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1568 struct pipe_resource
*dst
,
1570 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1571 struct pipe_resource
*src
,
1573 const struct pipe_box
*srcbox
)
1575 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1576 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1577 struct pipe_surface
*dst_view
, dst_templ
;
1578 struct pipe_sampler_view src_templ
, *src_view
;
1579 struct pipe_box dstbox
;
1582 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1584 u_box_3d(dstx
, dsty
, dstz
, abs(srcbox
->width
), abs(srcbox
->height
),
1585 abs(srcbox
->depth
), &dstbox
);
1587 /* Initialize the surface. */
1588 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1589 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1591 /* Initialize the sampler view. */
1592 util_blitter_default_src_texture(blitter
, &src_templ
, src
, src_level
);
1593 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1596 util_blitter_blit_generic(blitter
, dst_view
, &dstbox
,
1597 src_view
, srcbox
, src
->width0
, src
->height0
,
1598 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
,
1601 pipe_surface_reference(&dst_view
, NULL
);
1602 pipe_sampler_view_reference(&src_view
, NULL
);
1606 blitter_draw_tex(struct blitter_context_priv
*ctx
,
1607 int dst_x1
, int dst_y1
, int dst_x2
, int dst_y2
,
1608 struct pipe_sampler_view
*src
,
1609 unsigned src_width0
, unsigned src_height0
,
1610 int src_x1
, int src_y1
, int src_x2
, int src_y2
,
1611 float layer
, unsigned sample
,
1612 bool uses_txf
, enum blitter_attrib_type type
)
1614 union blitter_attrib coord
;
1616 get_texcoords(src
, src_width0
, src_height0
,
1617 src_x1
, src_y1
, src_x2
, src_y2
, layer
, sample
,
1620 if (src
->target
== PIPE_TEXTURE_CUBE
||
1621 src
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
1622 float face_coord
[4][2];
1624 set_texcoords_in_vertices(&coord
, &face_coord
[0][0], 2);
1625 util_map_texcoords2d_onto_cubemap((unsigned)layer
% 6,
1626 /* pointer, stride in floats */
1627 &face_coord
[0][0], 2,
1628 &ctx
->vertices
[0][1][0], 8,
1630 for (unsigned i
= 0; i
< 4; i
++)
1631 ctx
->vertices
[i
][1][3] = coord
.texcoord
.w
;
1633 /* Cubemaps don't use draw_rectangle. */
1634 blitter_draw(ctx
, dst_x1
, dst_y1
, dst_x2
, dst_y2
, 0, 1);
1636 ctx
->base
.draw_rectangle(&ctx
->base
, dst_x1
, dst_y1
, dst_x2
, dst_y2
, 0,
1641 static void do_blits(struct blitter_context_priv
*ctx
,
1642 struct pipe_surface
*dst
,
1643 const struct pipe_box
*dstbox
,
1644 struct pipe_sampler_view
*src
,
1645 unsigned src_width0
,
1646 unsigned src_height0
,
1647 const struct pipe_box
*srcbox
,
1651 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1652 unsigned src_samples
= src
->texture
->nr_samples
;
1653 unsigned dst_samples
= dst
->texture
->nr_samples
;
1654 enum pipe_texture_target src_target
= src
->target
;
1655 struct pipe_framebuffer_state fb_state
= {0};
1657 /* Initialize framebuffer state. */
1658 fb_state
.width
= dst
->width
;
1659 fb_state
.height
= dst
->height
;
1660 fb_state
.nr_cbufs
= is_zsbuf
? 0 : 1;
1662 blitter_set_dst_dimensions(ctx
, fb_state
.width
, fb_state
.height
);
1664 if ((src_target
== PIPE_TEXTURE_1D
||
1665 src_target
== PIPE_TEXTURE_2D
||
1666 src_target
== PIPE_TEXTURE_RECT
) &&
1668 /* Set framebuffer state. */
1670 fb_state
.zsbuf
= dst
;
1672 fb_state
.cbufs
[0] = dst
;
1674 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1677 pipe
->set_sample_mask(pipe
, ~0);
1678 blitter_draw_tex(ctx
, dstbox
->x
, dstbox
->y
,
1679 dstbox
->x
+ dstbox
->width
,
1680 dstbox
->y
+ dstbox
->height
,
1681 src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1682 srcbox
->x
+ srcbox
->width
, srcbox
->y
+ srcbox
->height
,
1683 0, 0, uses_txf
, UTIL_BLITTER_ATTRIB_TEXCOORD_XY
);
1685 /* Draw the quad with the generic codepath. */
1687 for (dst_z
= 0; dst_z
< dstbox
->depth
; dst_z
++) {
1688 struct pipe_surface
*old
;
1689 float dst2src_scale
= srcbox
->depth
/ (float)dstbox
->depth
;
1691 /* Scale Z properly if the blit is scaled.
1693 * When downscaling, we want the coordinates centered, so that
1694 * mipmapping works for 3D textures. For example, when generating
1695 * a 4x4x4 level, this wouldn't average the pixels:
1697 * src Z: 0 1 2 3 4 5 6 7
1700 * Because the pixels are not centered below the pixels of the higher
1701 * level. Therefore, we want this:
1702 * src Z: 0 1 2 3 4 5 6 7
1705 * dst_offset defines the offset needed for centering the pixels and
1706 * it works with any scaling (not just 2x).
1708 float dst_offset
= ((srcbox
->depth
- 1) -
1709 (dstbox
->depth
- 1) * dst2src_scale
) * 0.5;
1710 float src_z
= (dst_z
+ dst_offset
) * dst2src_scale
;
1712 /* Set framebuffer state. */
1714 fb_state
.zsbuf
= dst
;
1716 fb_state
.cbufs
[0] = dst
;
1718 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1720 /* See if we need to blit a multisample or singlesample buffer. */
1721 if (src_samples
== dst_samples
&& dst_samples
> 1) {
1723 unsigned i
, max_sample
= dst_samples
- 1;
1725 for (i
= 0; i
<= max_sample
; i
++) {
1726 pipe
->set_sample_mask(pipe
, 1 << i
);
1727 blitter_draw_tex(ctx
, dstbox
->x
, dstbox
->y
,
1728 dstbox
->x
+ dstbox
->width
,
1729 dstbox
->y
+ dstbox
->height
,
1730 src
, src_width0
, src_height0
,
1731 srcbox
->x
, srcbox
->y
,
1732 srcbox
->x
+ srcbox
->width
,
1733 srcbox
->y
+ srcbox
->height
,
1734 srcbox
->z
+ src_z
, i
, uses_txf
,
1735 UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW
);
1738 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1739 pipe
->set_sample_mask(pipe
, ~0);
1740 blitter_draw_tex(ctx
, dstbox
->x
, dstbox
->y
,
1741 dstbox
->x
+ dstbox
->width
,
1742 dstbox
->y
+ dstbox
->height
,
1743 src
, src_width0
, src_height0
,
1744 srcbox
->x
, srcbox
->y
,
1745 srcbox
->x
+ srcbox
->width
,
1746 srcbox
->y
+ srcbox
->height
,
1747 srcbox
->z
+ src_z
, 0, uses_txf
,
1748 UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW
);
1751 /* Get the next surface or (if this is the last iteration)
1752 * just unreference the last one. */
1754 if (dst_z
< dstbox
->depth
-1) {
1755 dst
= util_blitter_get_next_surface_layer(ctx
->base
.pipe
, dst
);
1758 pipe_surface_reference(&old
, NULL
);
1764 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1765 struct pipe_surface
*dst
,
1766 const struct pipe_box
*dstbox
,
1767 struct pipe_sampler_view
*src
,
1768 const struct pipe_box
*srcbox
,
1769 unsigned src_width0
, unsigned src_height0
,
1770 unsigned mask
, unsigned filter
,
1771 const struct pipe_scissor_state
*scissor
,
1774 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1775 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1776 enum pipe_texture_target src_target
= src
->target
;
1777 unsigned src_samples
= src
->texture
->nr_samples
;
1778 unsigned dst_samples
= dst
->texture
->nr_samples
;
1779 bool has_depth
, has_stencil
, has_color
;
1780 bool blit_stencil
, blit_depth
, blit_color
;
1781 void *sampler_state
;
1782 const struct util_format_description
*src_desc
=
1783 util_format_description(src
->format
);
1784 const struct util_format_description
*dst_desc
=
1785 util_format_description(dst
->format
);
1787 has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
&&
1788 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1789 has_depth
= util_format_has_depth(src_desc
) &&
1790 util_format_has_depth(dst_desc
);
1791 has_stencil
= util_format_has_stencil(src_desc
) &&
1792 util_format_has_stencil(dst_desc
);
1794 blit_color
= has_color
&& (mask
& PIPE_MASK_RGBA
);
1795 blit_depth
= has_depth
&& (mask
& PIPE_MASK_Z
);
1796 blit_stencil
= has_stencil
&& (mask
& PIPE_MASK_S
) &&
1797 ctx
->has_stencil_export
;
1799 if (!blit_stencil
&& !blit_depth
&& !blit_color
) {
1803 bool is_scaled
= dstbox
->width
!= abs(srcbox
->width
) ||
1804 dstbox
->height
!= abs(srcbox
->height
);
1806 if (blit_stencil
|| !is_scaled
)
1807 filter
= PIPE_TEX_FILTER_NEAREST
;
1809 bool use_txf
= false;
1811 /* Don't support scaled blits. The TXF shader uses F2I for rounding. */
1814 filter
== PIPE_TEX_FILTER_NEAREST
&&
1815 src
->target
!= PIPE_TEXTURE_CUBE
&&
1816 src
->target
!= PIPE_TEXTURE_CUBE_ARRAY
) {
1817 int src_width
= u_minify(src_width0
, src
->u
.tex
.first_level
);
1818 int src_height
= u_minify(src_height0
, src
->u
.tex
.first_level
);
1819 int src_depth
= src
->u
.tex
.last_layer
+ 1;
1820 struct pipe_box box
= *srcbox
;
1822 /* Eliminate negative width/height/depth. */
1823 if (box
.width
< 0) {
1827 if (box
.height
< 0) {
1828 box
.y
+= box
.height
;
1831 if (box
.depth
< 0) {
1836 /* See if srcbox is in bounds. TXF doesn't clamp the coordinates. */
1838 box
.x
>= 0 && box
.x
< src_width
&&
1839 box
.y
>= 0 && box
.y
< src_height
&&
1840 box
.z
>= 0 && box
.z
< src_depth
&&
1841 box
.x
+ box
.width
> 0 && box
.x
+ box
.width
<= src_width
&&
1842 box
.y
+ box
.height
> 0 && box
.y
+ box
.height
<= src_height
&&
1843 box
.z
+ box
.depth
> 0 && box
.z
+ box
.depth
<= src_depth
;
1846 /* Check whether the states are properly saved. */
1847 util_blitter_set_running_flag(blitter
);
1848 blitter_check_saved_vertex_states(ctx
);
1849 blitter_check_saved_fragment_states(ctx
);
1850 blitter_check_saved_textures(ctx
);
1851 blitter_check_saved_fb_state(ctx
);
1852 blitter_disable_render_cond(ctx
);
1854 if (blit_depth
|| blit_stencil
) {
1855 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
1857 if (blit_depth
&& blit_stencil
) {
1858 pipe
->bind_depth_stencil_alpha_state(pipe
,
1859 ctx
->dsa_write_depth_stencil
);
1860 ctx
->bind_fs_state(pipe
,
1861 blitter_get_fs_texfetch_depthstencil(ctx
, src_target
,
1862 src_samples
, use_txf
));
1863 } else if (blit_depth
) {
1864 pipe
->bind_depth_stencil_alpha_state(pipe
,
1865 ctx
->dsa_write_depth_keep_stencil
);
1866 ctx
->bind_fs_state(pipe
,
1867 blitter_get_fs_texfetch_depth(ctx
, src_target
,
1868 src_samples
, use_txf
));
1869 } else { /* is_stencil */
1870 pipe
->bind_depth_stencil_alpha_state(pipe
,
1871 ctx
->dsa_keep_depth_write_stencil
);
1872 ctx
->bind_fs_state(pipe
,
1873 blitter_get_fs_texfetch_stencil(ctx
, src_target
,
1874 src_samples
, use_txf
));
1878 unsigned colormask
= mask
& PIPE_MASK_RGBA
;
1880 pipe
->bind_blend_state(pipe
, ctx
->blend
[colormask
][alpha_blend
]);
1881 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1882 ctx
->bind_fs_state(pipe
,
1883 blitter_get_fs_texfetch_col(ctx
, src
->format
, dst
->format
, src_target
,
1884 src_samples
, dst_samples
, filter
,
1888 /* Set the linear filter only for scaled color non-MSAA blits. */
1889 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
1890 if (src_target
== PIPE_TEXTURE_RECT
) {
1891 sampler_state
= ctx
->sampler_state_rect_linear
;
1893 sampler_state
= ctx
->sampler_state_linear
;
1896 if (src_target
== PIPE_TEXTURE_RECT
) {
1897 sampler_state
= ctx
->sampler_state_rect
;
1899 sampler_state
= ctx
->sampler_state
;
1904 if (blit_depth
&& blit_stencil
) {
1905 /* Setup two samplers, one for depth and the other one for stencil. */
1906 struct pipe_sampler_view templ
;
1907 struct pipe_sampler_view
*views
[2];
1908 void *samplers
[2] = {sampler_state
, sampler_state
};
1911 templ
.format
= util_format_stencil_only(templ
.format
);
1912 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1915 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1917 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, views
);
1918 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, samplers
);
1920 pipe_sampler_view_reference(&views
[1], NULL
);
1921 } else if (blit_stencil
) {
1922 /* Set a stencil-only sampler view for it not to sample depth instead. */
1923 struct pipe_sampler_view templ
;
1924 struct pipe_sampler_view
*view
;
1927 templ
.format
= util_format_stencil_only(templ
.format
);
1928 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1930 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1932 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &view
);
1933 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1934 0, 1, &sampler_state
);
1936 pipe_sampler_view_reference(&view
, NULL
);
1938 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src
);
1939 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1940 0, 1, &sampler_state
);
1943 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1945 pipe
->set_scissor_states(pipe
, 0, 1, scissor
);
1948 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
, false, true);
1950 do_blits(ctx
, dst
, dstbox
, src
, src_width0
, src_height0
,
1951 srcbox
, blit_depth
|| blit_stencil
, use_txf
);
1953 util_blitter_restore_vertex_states(blitter
);
1954 util_blitter_restore_fragment_states(blitter
);
1955 util_blitter_restore_textures(blitter
);
1956 util_blitter_restore_fb_state(blitter
);
1958 pipe
->set_scissor_states(pipe
, 0, 1, &ctx
->base
.saved_scissor
);
1960 util_blitter_restore_render_cond(blitter
);
1961 util_blitter_unset_running_flag(blitter
);
1965 util_blitter_blit(struct blitter_context
*blitter
,
1966 const struct pipe_blit_info
*info
)
1968 struct pipe_resource
*dst
= info
->dst
.resource
;
1969 struct pipe_resource
*src
= info
->src
.resource
;
1970 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1971 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1972 struct pipe_surface
*dst_view
, dst_templ
;
1973 struct pipe_sampler_view src_templ
, *src_view
;
1975 /* Initialize the surface. */
1976 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
1978 dst_templ
.format
= info
->dst
.format
;
1979 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1981 /* Initialize the sampler view. */
1982 util_blitter_default_src_texture(blitter
, &src_templ
, src
, info
->src
.level
);
1983 src_templ
.format
= info
->src
.format
;
1984 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1987 util_blitter_blit_generic(blitter
, dst_view
, &info
->dst
.box
,
1988 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
1989 info
->mask
, info
->filter
,
1990 info
->scissor_enable
? &info
->scissor
: NULL
,
1993 pipe_surface_reference(&dst_view
, NULL
);
1994 pipe_sampler_view_reference(&src_view
, NULL
);
1997 void util_blitter_generate_mipmap(struct blitter_context
*blitter
,
1998 struct pipe_resource
*tex
,
1999 enum pipe_format format
,
2000 unsigned base_level
, unsigned last_level
,
2001 unsigned first_layer
, unsigned last_layer
)
2003 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2004 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2005 struct pipe_surface dst_templ
, *dst_view
;
2006 struct pipe_sampler_view src_templ
, *src_view
;
2008 void *sampler_state
;
2009 const struct util_format_description
*desc
=
2010 util_format_description(format
);
2012 unsigned target
= tex
->target
;
2014 if (ctx
->cube_as_2darray
&&
2015 (target
== PIPE_TEXTURE_CUBE
|| target
== PIPE_TEXTURE_CUBE_ARRAY
))
2016 target
= PIPE_TEXTURE_2D_ARRAY
;
2018 assert(tex
->nr_samples
<= 1);
2019 assert(!util_format_has_stencil(desc
));
2021 is_depth
= desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
;
2023 /* Check whether the states are properly saved. */
2024 util_blitter_set_running_flag(blitter
);
2025 blitter_check_saved_vertex_states(ctx
);
2026 blitter_check_saved_fragment_states(ctx
);
2027 blitter_check_saved_textures(ctx
);
2028 blitter_check_saved_fb_state(ctx
);
2029 blitter_disable_render_cond(ctx
);
2033 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
2034 pipe
->bind_depth_stencil_alpha_state(pipe
,
2035 ctx
->dsa_write_depth_keep_stencil
);
2036 ctx
->bind_fs_state(pipe
,
2037 blitter_get_fs_texfetch_depth(ctx
, target
, 1, false));
2039 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2040 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2041 ctx
->bind_fs_state(pipe
,
2042 blitter_get_fs_texfetch_col(ctx
, tex
->format
, tex
->format
, target
,
2043 1, 1, PIPE_TEX_FILTER_LINEAR
, false));
2046 if (target
== PIPE_TEXTURE_RECT
) {
2047 sampler_state
= ctx
->sampler_state_rect_linear
;
2049 sampler_state
= ctx
->sampler_state_linear
;
2051 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
2052 0, 1, &sampler_state
);
2054 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2055 blitter_set_common_draw_rect_state(ctx
, false, false, true);
2057 for (src_level
= base_level
; src_level
< last_level
; src_level
++) {
2058 struct pipe_box dstbox
= {0}, srcbox
= {0};
2059 unsigned dst_level
= src_level
+ 1;
2061 dstbox
.width
= u_minify(tex
->width0
, dst_level
);
2062 dstbox
.height
= u_minify(tex
->height0
, dst_level
);
2064 srcbox
.width
= u_minify(tex
->width0
, src_level
);
2065 srcbox
.height
= u_minify(tex
->height0
, src_level
);
2067 if (target
== PIPE_TEXTURE_3D
) {
2068 dstbox
.depth
= util_max_layer(tex
, dst_level
) + 1;
2069 srcbox
.depth
= util_max_layer(tex
, src_level
) + 1;
2071 dstbox
.z
= srcbox
.z
= first_layer
;
2072 dstbox
.depth
= srcbox
.depth
= last_layer
- first_layer
+ 1;
2075 /* Initialize the surface. */
2076 util_blitter_default_dst_texture(&dst_templ
, tex
, dst_level
,
2078 dst_templ
.format
= format
;
2079 dst_view
= pipe
->create_surface(pipe
, tex
, &dst_templ
);
2081 /* Initialize the sampler view. */
2082 util_blitter_default_src_texture(blitter
, &src_templ
, tex
, src_level
);
2083 src_templ
.format
= format
;
2084 src_view
= pipe
->create_sampler_view(pipe
, tex
, &src_templ
);
2086 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src_view
);
2088 do_blits(ctx
, dst_view
, &dstbox
, src_view
, tex
->width0
, tex
->height0
,
2089 &srcbox
, is_depth
, false);
2091 pipe_surface_reference(&dst_view
, NULL
);
2092 pipe_sampler_view_reference(&src_view
, NULL
);
2095 util_blitter_restore_vertex_states(blitter
);
2096 util_blitter_restore_fragment_states(blitter
);
2097 util_blitter_restore_textures(blitter
);
2098 util_blitter_restore_fb_state(blitter
);
2099 util_blitter_restore_render_cond(blitter
);
2100 util_blitter_unset_running_flag(blitter
);
2103 /* Clear a region of a color surface to a constant value. */
2104 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
2105 struct pipe_surface
*dstsurf
,
2106 const union pipe_color_union
*color
,
2107 unsigned dstx
, unsigned dsty
,
2108 unsigned width
, unsigned height
)
2110 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2111 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2112 struct pipe_framebuffer_state fb_state
;
2113 unsigned num_layers
;
2115 assert(dstsurf
->texture
);
2116 if (!dstsurf
->texture
)
2119 /* check the saved state */
2120 util_blitter_set_running_flag(blitter
);
2121 blitter_check_saved_vertex_states(ctx
);
2122 blitter_check_saved_fragment_states(ctx
);
2123 blitter_check_saved_fb_state(ctx
);
2124 blitter_disable_render_cond(ctx
);
2127 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2128 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2129 bind_fs_write_one_cbuf(ctx
);
2130 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2132 /* set a framebuffer state */
2133 fb_state
.width
= dstsurf
->width
;
2134 fb_state
.height
= dstsurf
->height
;
2135 fb_state
.nr_cbufs
= 1;
2136 fb_state
.cbufs
[0] = dstsurf
;
2138 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2139 pipe
->set_sample_mask(pipe
, ~0);
2141 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2143 union blitter_attrib attrib
;
2144 memcpy(attrib
.color
, color
->ui
, sizeof(color
->ui
));
2146 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
2147 if (num_layers
> 1 && ctx
->has_layered
) {
2148 blitter_set_common_draw_rect_state(ctx
, false, true, true);
2149 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
2150 num_layers
, UTIL_BLITTER_ATTRIB_COLOR
, &attrib
);
2152 blitter_set_common_draw_rect_state(ctx
, false, false, true);
2153 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
2154 1, UTIL_BLITTER_ATTRIB_COLOR
, &attrib
);
2157 util_blitter_restore_vertex_states(blitter
);
2158 util_blitter_restore_fragment_states(blitter
);
2159 util_blitter_restore_fb_state(blitter
);
2160 util_blitter_restore_render_cond(blitter
);
2161 util_blitter_unset_running_flag(blitter
);
2164 /* Clear a region of a depth stencil surface. */
2165 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
2166 struct pipe_surface
*dstsurf
,
2167 unsigned clear_flags
,
2170 unsigned dstx
, unsigned dsty
,
2171 unsigned width
, unsigned height
)
2173 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2174 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2175 struct pipe_framebuffer_state fb_state
;
2176 struct pipe_stencil_ref sr
= { { 0 } };
2177 unsigned num_layers
;
2179 assert(dstsurf
->texture
);
2180 if (!dstsurf
->texture
)
2183 /* check the saved state */
2184 util_blitter_set_running_flag(blitter
);
2185 blitter_check_saved_vertex_states(ctx
);
2186 blitter_check_saved_fragment_states(ctx
);
2187 blitter_check_saved_fb_state(ctx
);
2188 blitter_disable_render_cond(ctx
);
2191 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
2192 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
2193 sr
.ref_value
[0] = stencil
& 0xff;
2194 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
2195 pipe
->set_stencil_ref(pipe
, &sr
);
2197 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
2198 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
2200 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
2201 sr
.ref_value
[0] = stencil
& 0xff;
2202 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
2203 pipe
->set_stencil_ref(pipe
, &sr
);
2206 /* hmm that should be illegal probably, or make it a no-op somewhere */
2207 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2210 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2212 /* set a framebuffer state */
2213 fb_state
.width
= dstsurf
->width
;
2214 fb_state
.height
= dstsurf
->height
;
2215 fb_state
.nr_cbufs
= 0;
2216 fb_state
.cbufs
[0] = 0;
2217 fb_state
.zsbuf
= dstsurf
;
2218 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2219 pipe
->set_sample_mask(pipe
, ~0);
2221 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2223 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
2224 if (num_layers
> 1 && ctx
->has_layered
) {
2225 blitter_set_common_draw_rect_state(ctx
, false, true, false);
2226 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
,
2228 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2230 blitter_set_common_draw_rect_state(ctx
, false, false, false);
2231 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
,
2233 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2236 util_blitter_restore_vertex_states(blitter
);
2237 util_blitter_restore_fragment_states(blitter
);
2238 util_blitter_restore_fb_state(blitter
);
2239 util_blitter_restore_render_cond(blitter
);
2240 util_blitter_unset_running_flag(blitter
);
2243 /* draw a rectangle across a region using a custom dsa stage - for r600g */
2244 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
2245 struct pipe_surface
*zsurf
,
2246 struct pipe_surface
*cbsurf
,
2247 unsigned sample_mask
,
2248 void *dsa_stage
, float depth
)
2250 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2251 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2252 struct pipe_framebuffer_state fb_state
;
2254 assert(zsurf
->texture
);
2255 if (!zsurf
->texture
)
2258 /* check the saved state */
2259 util_blitter_set_running_flag(blitter
);
2260 blitter_check_saved_vertex_states(ctx
);
2261 blitter_check_saved_fragment_states(ctx
);
2262 blitter_check_saved_fb_state(ctx
);
2263 blitter_disable_render_cond(ctx
);
2266 pipe
->bind_blend_state(pipe
, cbsurf
? ctx
->blend
[PIPE_MASK_RGBA
][0] :
2268 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
2270 bind_fs_write_one_cbuf(ctx
);
2273 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2275 /* set a framebuffer state */
2276 fb_state
.width
= zsurf
->width
;
2277 fb_state
.height
= zsurf
->height
;
2278 fb_state
.nr_cbufs
= 1;
2280 fb_state
.cbufs
[0] = cbsurf
;
2281 fb_state
.nr_cbufs
= 1;
2283 fb_state
.cbufs
[0] = NULL
;
2284 fb_state
.nr_cbufs
= 0;
2286 fb_state
.zsbuf
= zsurf
;
2287 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2288 pipe
->set_sample_mask(pipe
, sample_mask
);
2290 blitter_set_common_draw_rect_state(ctx
, false, false, false);
2291 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
2292 blitter
->draw_rectangle(blitter
, 0, 0, zsurf
->width
, zsurf
->height
, depth
,
2293 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2295 util_blitter_restore_vertex_states(blitter
);
2296 util_blitter_restore_fragment_states(blitter
);
2297 util_blitter_restore_fb_state(blitter
);
2298 util_blitter_restore_render_cond(blitter
);
2299 util_blitter_unset_running_flag(blitter
);
2302 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
2303 struct pipe_resource
*dst
,
2305 struct pipe_resource
*src
,
2309 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2310 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2311 struct pipe_vertex_buffer vb
;
2312 struct pipe_stream_output_target
*so_target
;
2313 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2315 if (srcx
>= src
->width0
||
2316 dstx
>= dst
->width0
) {
2319 if (srcx
+ size
> src
->width0
) {
2320 size
= src
->width0
- srcx
;
2322 if (dstx
+ size
> dst
->width0
) {
2323 size
= dst
->width0
- dstx
;
2326 /* Drivers not capable of Stream Out should not call this function
2327 * in the first place. */
2328 assert(ctx
->has_stream_out
);
2330 /* Some alignment is required. */
2331 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
2332 !ctx
->has_stream_out
) {
2333 struct pipe_box box
;
2334 u_box_1d(srcx
, size
, &box
);
2335 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
2339 util_blitter_set_running_flag(blitter
);
2340 blitter_check_saved_vertex_states(ctx
);
2341 blitter_disable_render_cond(ctx
);
2343 vb
.is_user_buffer
= false;
2344 vb
.buffer
.resource
= src
;
2345 vb
.buffer_offset
= srcx
;
2348 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2349 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[0]);
2350 bind_vs_pos_only(ctx
, 1);
2351 if (ctx
->has_geometry_shader
)
2352 pipe
->bind_gs_state(pipe
, NULL
);
2353 if (ctx
->has_tessellation
) {
2354 pipe
->bind_tcs_state(pipe
, NULL
);
2355 pipe
->bind_tes_state(pipe
, NULL
);
2357 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2359 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
2360 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2362 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2364 util_blitter_restore_vertex_states(blitter
);
2365 util_blitter_restore_render_cond(blitter
);
2366 util_blitter_unset_running_flag(blitter
);
2367 pipe_so_target_reference(&so_target
, NULL
);
2370 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
2371 struct pipe_resource
*dst
,
2372 unsigned offset
, unsigned size
,
2373 unsigned num_channels
,
2374 const union pipe_color_union
*clear_value
)
2376 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2377 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2378 struct pipe_vertex_buffer vb
= {0};
2379 struct pipe_stream_output_target
*so_target
= NULL
;
2380 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2382 assert(num_channels
>= 1);
2383 assert(num_channels
<= 4);
2385 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
2387 * R600 uses this to initialize texture resources, so width0 might not be
2388 * what you think it is.
2391 /* Streamout is required. */
2392 if (!ctx
->has_stream_out
) {
2393 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2397 /* Some alignment is required. */
2398 if (offset
% 4 != 0 || size
% 4 != 0) {
2399 assert(!"Bad alignment in util_blitter_clear_buffer()");
2403 u_upload_data(pipe
->stream_uploader
, 0, num_channels
*4, 4, clear_value
,
2404 &vb
.buffer_offset
, &vb
.buffer
.resource
);
2405 if (!vb
.buffer
.resource
)
2410 util_blitter_set_running_flag(blitter
);
2411 blitter_check_saved_vertex_states(ctx
);
2412 blitter_disable_render_cond(ctx
);
2414 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2415 pipe
->bind_vertex_elements_state(pipe
,
2416 ctx
->velem_state_readbuf
[num_channels
-1]);
2417 bind_vs_pos_only(ctx
, num_channels
);
2418 if (ctx
->has_geometry_shader
)
2419 pipe
->bind_gs_state(pipe
, NULL
);
2420 if (ctx
->has_tessellation
) {
2421 pipe
->bind_tcs_state(pipe
, NULL
);
2422 pipe
->bind_tes_state(pipe
, NULL
);
2424 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2426 so_target
= pipe
->create_stream_output_target(pipe
, dst
, offset
, size
);
2427 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2429 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2432 util_blitter_restore_vertex_states(blitter
);
2433 util_blitter_restore_render_cond(blitter
);
2434 util_blitter_unset_running_flag(blitter
);
2435 pipe_so_target_reference(&so_target
, NULL
);
2436 pipe_resource_reference(&vb
.buffer
.resource
, NULL
);
2439 /* probably radeon specific */
2440 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
2441 struct pipe_resource
*dst
,
2444 struct pipe_resource
*src
,
2446 unsigned sample_mask
,
2448 enum pipe_format format
)
2450 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2451 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2452 struct pipe_framebuffer_state fb_state
;
2453 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
2455 util_blitter_set_running_flag(blitter
);
2456 blitter_check_saved_vertex_states(ctx
);
2457 blitter_check_saved_fragment_states(ctx
);
2458 blitter_disable_render_cond(ctx
);
2461 pipe
->bind_blend_state(pipe
, custom_blend
);
2462 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2463 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2464 bind_fs_write_one_cbuf(ctx
);
2465 pipe
->set_sample_mask(pipe
, sample_mask
);
2467 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
2468 surf_tmpl
.format
= format
;
2469 surf_tmpl
.u
.tex
.level
= dst_level
;
2470 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
2471 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
2473 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
2475 surf_tmpl
.u
.tex
.level
= 0;
2476 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
2477 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
2479 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
2481 /* set a framebuffer state */
2482 fb_state
.width
= src
->width0
;
2483 fb_state
.height
= src
->height0
;
2484 fb_state
.nr_cbufs
= 2;
2485 fb_state
.cbufs
[0] = srcsurf
;
2486 fb_state
.cbufs
[1] = dstsurf
;
2487 fb_state
.zsbuf
= NULL
;
2488 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2490 blitter_set_common_draw_rect_state(ctx
, false, false, false);
2491 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
2492 blitter
->draw_rectangle(blitter
, 0, 0, src
->width0
, src
->height0
,
2493 0, 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2494 util_blitter_restore_fb_state(blitter
);
2495 util_blitter_restore_vertex_states(blitter
);
2496 util_blitter_restore_fragment_states(blitter
);
2497 util_blitter_restore_render_cond(blitter
);
2498 util_blitter_unset_running_flag(blitter
);
2500 pipe_surface_reference(&srcsurf
, NULL
);
2501 pipe_surface_reference(&dstsurf
, NULL
);
2504 void util_blitter_custom_color(struct blitter_context
*blitter
,
2505 struct pipe_surface
*dstsurf
,
2508 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2509 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2510 struct pipe_framebuffer_state fb_state
;
2512 assert(dstsurf
->texture
);
2513 if (!dstsurf
->texture
)
2516 /* check the saved state */
2517 util_blitter_set_running_flag(blitter
);
2518 blitter_check_saved_vertex_states(ctx
);
2519 blitter_check_saved_fragment_states(ctx
);
2520 blitter_check_saved_fb_state(ctx
);
2521 blitter_disable_render_cond(ctx
);
2524 pipe
->bind_blend_state(pipe
, custom_blend
? custom_blend
2525 : ctx
->blend
[PIPE_MASK_RGBA
][0]);
2526 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2527 bind_fs_write_one_cbuf(ctx
);
2528 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2529 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2531 /* set a framebuffer state */
2532 fb_state
.width
= dstsurf
->width
;
2533 fb_state
.height
= dstsurf
->height
;
2534 fb_state
.nr_cbufs
= 1;
2535 fb_state
.cbufs
[0] = dstsurf
;
2537 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2538 pipe
->set_sample_mask(pipe
, ~0);
2540 blitter_set_common_draw_rect_state(ctx
, false, false, false);
2541 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2542 blitter
->draw_rectangle(blitter
, 0, 0, dstsurf
->width
, dstsurf
->height
,
2543 0, 1, UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2545 util_blitter_restore_vertex_states(blitter
);
2546 util_blitter_restore_fragment_states(blitter
);
2547 util_blitter_restore_fb_state(blitter
);
2548 util_blitter_restore_render_cond(blitter
);
2549 util_blitter_unset_running_flag(blitter
);