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 struct u_upload_mgr
*upload
;
66 float vertices
[4][2][4]; /**< {pos, color} or {pos, texcoord} */
68 /* Templates for various state objects. */
70 /* Constant state objects. */
72 void *vs
; /**< Vertex shader which passes {pos, generic} to the output.*/
73 void *vs_pos_only
; /**< Vertex shader which passes pos to the output.*/
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,
82 where the index is PIPE_TEXTURE_* to be sampled. */
83 void *fs_texfetch_col
[PIPE_MAX_TEXTURE_TYPES
];
84 void *fs_texfetch_col_uint
[PIPE_MAX_TEXTURE_TYPES
];
85 void *fs_texfetch_col_sint
[PIPE_MAX_TEXTURE_TYPES
];
87 /* FS which outputs a depth from a texture,
88 where the index is PIPE_TEXTURE_* to be sampled. */
89 void *fs_texfetch_depth
[PIPE_MAX_TEXTURE_TYPES
];
90 void *fs_texfetch_depthstencil
[PIPE_MAX_TEXTURE_TYPES
];
91 void *fs_texfetch_stencil
[PIPE_MAX_TEXTURE_TYPES
];
93 /* FS which outputs one sample from a multisample texture. */
94 void *fs_texfetch_col_msaa
[PIPE_MAX_TEXTURE_TYPES
];
95 void *fs_texfetch_col_msaa_uint
[PIPE_MAX_TEXTURE_TYPES
];
96 void *fs_texfetch_col_msaa_sint
[PIPE_MAX_TEXTURE_TYPES
];
97 void *fs_texfetch_depth_msaa
[PIPE_MAX_TEXTURE_TYPES
];
98 void *fs_texfetch_depthstencil_msaa
[PIPE_MAX_TEXTURE_TYPES
];
99 void *fs_texfetch_stencil_msaa
[PIPE_MAX_TEXTURE_TYPES
];
101 /* FS which outputs an average of all samples. */
102 void *fs_resolve
[PIPE_MAX_TEXTURE_TYPES
][NUM_RESOLVE_FRAG_SHADERS
][2];
103 void *fs_resolve_sint
[PIPE_MAX_TEXTURE_TYPES
][NUM_RESOLVE_FRAG_SHADERS
][2];
104 void *fs_resolve_uint
[PIPE_MAX_TEXTURE_TYPES
][NUM_RESOLVE_FRAG_SHADERS
][2];
107 void *blend
[PIPE_MASK_RGBA
+1]; /**< blend state with writemask */
108 void *blend_clear
[GET_CLEAR_BLEND_STATE_IDX(PIPE_CLEAR_COLOR
)+1];
110 /* Depth stencil alpha state. */
111 void *dsa_write_depth_stencil
;
112 void *dsa_write_depth_keep_stencil
;
113 void *dsa_keep_depth_stencil
;
114 void *dsa_keep_depth_write_stencil
;
116 /* Vertex elements states. */
118 void *velem_state_readbuf
[4]; /**< X, XY, XYZ, XYZW */
122 void *sampler_state_linear
;
123 void *sampler_state_rect
;
124 void *sampler_state_rect_linear
;
126 /* Rasterizer state. */
127 void *rs_state
, *rs_state_scissor
, *rs_discard_state
;
129 /* Viewport state. */
130 struct pipe_viewport_state viewport
;
132 /* Destination surface dimensions. */
136 boolean has_geometry_shader
;
137 boolean has_tessellation
;
139 boolean has_stream_out
;
140 boolean has_stencil_export
;
141 boolean has_texture_multisample
;
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 /* blend state objects */
208 memset(&blend
, 0, sizeof(blend
));
210 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
211 blend
.rt
[0].colormask
= i
;
212 ctx
->blend
[i
] = pipe
->create_blend_state(pipe
, &blend
);
215 /* depth stencil alpha state objects */
216 memset(&dsa
, 0, sizeof(dsa
));
217 ctx
->dsa_keep_depth_stencil
=
218 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
220 dsa
.depth
.enabled
= 1;
221 dsa
.depth
.writemask
= 1;
222 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
223 ctx
->dsa_write_depth_keep_stencil
=
224 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
226 dsa
.stencil
[0].enabled
= 1;
227 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
228 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
229 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
230 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
231 dsa
.stencil
[0].valuemask
= 0xff;
232 dsa
.stencil
[0].writemask
= 0xff;
233 ctx
->dsa_write_depth_stencil
=
234 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
236 dsa
.depth
.enabled
= 0;
237 dsa
.depth
.writemask
= 0;
238 ctx
->dsa_keep_depth_write_stencil
=
239 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
242 memset(&sampler_state
, 0, sizeof(sampler_state
));
243 sampler_state
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
244 sampler_state
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
245 sampler_state
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
246 sampler_state
.normalized_coords
= 1;
247 ctx
->sampler_state
= pipe
->create_sampler_state(pipe
, &sampler_state
);
248 sampler_state
.normalized_coords
= 0;
249 ctx
->sampler_state_rect
= pipe
->create_sampler_state(pipe
, &sampler_state
);
251 sampler_state
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
252 sampler_state
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
253 sampler_state
.normalized_coords
= 1;
254 ctx
->sampler_state_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
255 sampler_state
.normalized_coords
= 0;
256 ctx
->sampler_state_rect_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
258 /* rasterizer state */
259 memset(&rs_state
, 0, sizeof(rs_state
));
260 rs_state
.cull_face
= PIPE_FACE_NONE
;
261 rs_state
.half_pixel_center
= 1;
262 rs_state
.bottom_edge_rule
= 1;
263 rs_state
.flatshade
= 1;
264 rs_state
.depth_clip
= 1;
265 ctx
->rs_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
267 rs_state
.scissor
= 1;
268 ctx
->rs_state_scissor
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
270 if (ctx
->has_stream_out
) {
271 rs_state
.scissor
= 0;
272 rs_state
.rasterizer_discard
= 1;
273 ctx
->rs_discard_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
276 ctx
->base
.vb_slot
= 0; /* 0 for now */
278 /* vertex elements states */
279 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
280 for (i
= 0; i
< 2; i
++) {
281 velem
[i
].src_offset
= i
* 4 * sizeof(float);
282 velem
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
283 velem
[i
].vertex_buffer_index
= ctx
->base
.vb_slot
;
285 ctx
->velem_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
287 if (ctx
->has_stream_out
) {
288 static enum pipe_format formats
[4] = {
289 PIPE_FORMAT_R32_UINT
,
290 PIPE_FORMAT_R32G32_UINT
,
291 PIPE_FORMAT_R32G32B32_UINT
,
292 PIPE_FORMAT_R32G32B32A32_UINT
295 for (i
= 0; i
< 4; i
++) {
296 velem
[0].src_format
= formats
[i
];
297 velem
[0].vertex_buffer_index
= ctx
->base
.vb_slot
;
298 ctx
->velem_state_readbuf
[i
] =
299 pipe
->create_vertex_elements_state(pipe
, 1, &velem
[0]);
304 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_INSTANCEID
) &&
305 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT
);
307 /* set invariant vertex coordinates */
308 for (i
= 0; i
< 4; i
++)
309 ctx
->vertices
[i
][0][3] = 1; /*v.w*/
311 ctx
->upload
= u_upload_create(pipe
, 65536, 4, PIPE_BIND_VERTEX_BUFFER
);
316 static void bind_vs_pos_only(struct blitter_context_priv
*ctx
)
318 struct pipe_context
*pipe
= ctx
->base
.pipe
;
320 if (!ctx
->vs_pos_only
) {
321 struct pipe_stream_output_info so
;
322 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
};
323 const uint semantic_indices
[] = { 0 };
325 memset(&so
, 0, sizeof(so
));
327 so
.output
[0].num_components
= 1;
331 util_make_vertex_passthrough_shader_with_so(pipe
, 1, semantic_names
,
332 semantic_indices
, FALSE
,
336 pipe
->bind_vs_state(pipe
, ctx
->vs_pos_only
);
339 static void bind_vs_passthrough(struct blitter_context_priv
*ctx
)
341 struct pipe_context
*pipe
= ctx
->base
.pipe
;
344 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
345 TGSI_SEMANTIC_GENERIC
};
346 const uint semantic_indices
[] = { 0, 0 };
348 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
349 semantic_indices
, FALSE
);
352 pipe
->bind_vs_state(pipe
, ctx
->vs
);
355 static void bind_vs_layered(struct blitter_context_priv
*ctx
)
357 struct pipe_context
*pipe
= ctx
->base
.pipe
;
359 if (!ctx
->vs_layered
) {
360 ctx
->vs_layered
= util_make_layered_clear_vertex_shader(pipe
);
363 pipe
->bind_vs_state(pipe
, ctx
->vs_layered
);
366 static void bind_fs_empty(struct blitter_context_priv
*ctx
)
368 struct pipe_context
*pipe
= ctx
->base
.pipe
;
370 if (!ctx
->fs_empty
) {
371 assert(!ctx
->cached_all_shaders
);
372 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
375 ctx
->bind_fs_state(pipe
, ctx
->fs_empty
);
378 static void bind_fs_write_one_cbuf(struct blitter_context_priv
*ctx
)
380 struct pipe_context
*pipe
= ctx
->base
.pipe
;
382 if (!ctx
->fs_write_one_cbuf
) {
383 assert(!ctx
->cached_all_shaders
);
384 ctx
->fs_write_one_cbuf
=
385 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
386 TGSI_INTERPOLATE_CONSTANT
, FALSE
);
389 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
392 static void bind_fs_write_all_cbufs(struct blitter_context_priv
*ctx
)
394 struct pipe_context
*pipe
= ctx
->base
.pipe
;
396 if (!ctx
->fs_write_all_cbufs
) {
397 assert(!ctx
->cached_all_shaders
);
398 ctx
->fs_write_all_cbufs
=
399 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
400 TGSI_INTERPOLATE_CONSTANT
, TRUE
);
403 ctx
->bind_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
406 void util_blitter_destroy(struct blitter_context
*blitter
)
408 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
409 struct pipe_context
*pipe
= blitter
->pipe
;
412 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
413 pipe
->delete_blend_state(pipe
, ctx
->blend
[i
]);
415 for (i
= 0; i
< Elements(ctx
->blend_clear
); i
++) {
416 if (ctx
->blend_clear
[i
])
417 pipe
->delete_blend_state(pipe
, ctx
->blend_clear
[i
]);
419 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
420 pipe
->delete_depth_stencil_alpha_state(pipe
,
421 ctx
->dsa_write_depth_keep_stencil
);
422 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
423 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
425 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
);
426 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state_scissor
);
427 if (ctx
->rs_discard_state
)
428 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_discard_state
);
430 pipe
->delete_vs_state(pipe
, ctx
->vs
);
431 if (ctx
->vs_pos_only
)
432 pipe
->delete_vs_state(pipe
, ctx
->vs_pos_only
);
434 pipe
->delete_vs_state(pipe
, ctx
->vs_layered
);
435 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state
);
436 for (i
= 0; i
< 4; i
++) {
437 if (ctx
->velem_state_readbuf
[i
]) {
438 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[i
]);
442 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
443 if (ctx
->fs_texfetch_col
[i
])
444 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[i
]);
445 if (ctx
->fs_texfetch_col_sint
[i
])
446 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col_sint
[i
]);
447 if (ctx
->fs_texfetch_col_uint
[i
])
448 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col_uint
[i
]);
449 if (ctx
->fs_texfetch_depth
[i
])
450 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
]);
451 if (ctx
->fs_texfetch_depthstencil
[i
])
452 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil
[i
]);
453 if (ctx
->fs_texfetch_stencil
[i
])
454 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil
[i
]);
456 if (ctx
->fs_texfetch_col_msaa
[i
])
457 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col_msaa
[i
]);
458 if (ctx
->fs_texfetch_col_msaa_sint
[i
])
459 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col_msaa_sint
[i
]);
460 if (ctx
->fs_texfetch_col_msaa_uint
[i
])
461 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col_msaa_uint
[i
]);
462 if (ctx
->fs_texfetch_depth_msaa
[i
])
463 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth_msaa
[i
]);
464 if (ctx
->fs_texfetch_depthstencil_msaa
[i
])
465 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil_msaa
[i
]);
466 if (ctx
->fs_texfetch_stencil_msaa
[i
])
467 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil_msaa
[i
]);
469 for (j
= 0; j
< Elements(ctx
->fs_resolve
[i
]); j
++)
470 for (f
= 0; f
< 2; f
++)
471 if (ctx
->fs_resolve
[i
][j
][f
])
472 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve
[i
][j
][f
]);
474 for (j
= 0; j
< Elements(ctx
->fs_resolve_sint
[i
]); j
++)
475 for (f
= 0; f
< 2; f
++)
476 if (ctx
->fs_resolve_sint
[i
][j
][f
])
477 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve_sint
[i
][j
][f
]);
479 for (j
= 0; j
< Elements(ctx
->fs_resolve_uint
[i
]); j
++)
480 for (f
= 0; f
< 2; f
++)
481 if (ctx
->fs_resolve_uint
[i
][j
][f
])
482 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve_uint
[i
][j
][f
]);
486 ctx
->delete_fs_state(pipe
, ctx
->fs_empty
);
487 if (ctx
->fs_write_one_cbuf
)
488 ctx
->delete_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
489 if (ctx
->fs_write_all_cbufs
)
490 ctx
->delete_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
492 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect_linear
);
493 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect
);
494 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_linear
);
495 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
);
496 u_upload_destroy(ctx
->upload
);
500 void util_blitter_set_texture_multisample(struct blitter_context
*blitter
,
503 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
505 ctx
->has_texture_multisample
= supported
;
508 static void blitter_set_running_flag(struct blitter_context_priv
*ctx
)
510 if (ctx
->base
.running
) {
511 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
514 ctx
->base
.running
= TRUE
;
517 static void blitter_unset_running_flag(struct blitter_context_priv
*ctx
)
519 if (!ctx
->base
.running
) {
520 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
523 ctx
->base
.running
= FALSE
;
526 static void blitter_check_saved_vertex_states(struct blitter_context_priv
*ctx
)
528 assert(ctx
->base
.saved_velem_state
!= INVALID_PTR
);
529 assert(ctx
->base
.saved_vs
!= INVALID_PTR
);
530 assert(!ctx
->has_geometry_shader
|| ctx
->base
.saved_gs
!= INVALID_PTR
);
531 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tcs
!= INVALID_PTR
);
532 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tes
!= INVALID_PTR
);
533 assert(!ctx
->has_stream_out
|| ctx
->base
.saved_num_so_targets
!= ~0);
534 assert(ctx
->base
.saved_rs_state
!= INVALID_PTR
);
537 static void blitter_restore_vertex_states(struct blitter_context_priv
*ctx
)
539 struct pipe_context
*pipe
= ctx
->base
.pipe
;
543 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1,
544 &ctx
->base
.saved_vertex_buffer
);
545 pipe_resource_reference(&ctx
->base
.saved_vertex_buffer
.buffer
, NULL
);
547 /* Vertex elements. */
548 pipe
->bind_vertex_elements_state(pipe
, ctx
->base
.saved_velem_state
);
549 ctx
->base
.saved_velem_state
= INVALID_PTR
;
552 pipe
->bind_vs_state(pipe
, ctx
->base
.saved_vs
);
553 ctx
->base
.saved_vs
= INVALID_PTR
;
555 /* Geometry shader. */
556 if (ctx
->has_geometry_shader
) {
557 pipe
->bind_gs_state(pipe
, ctx
->base
.saved_gs
);
558 ctx
->base
.saved_gs
= INVALID_PTR
;
561 if (ctx
->has_tessellation
) {
562 pipe
->bind_tcs_state(pipe
, ctx
->base
.saved_tcs
);
563 pipe
->bind_tes_state(pipe
, ctx
->base
.saved_tes
);
564 ctx
->base
.saved_tcs
= INVALID_PTR
;
565 ctx
->base
.saved_tes
= INVALID_PTR
;
568 /* Stream outputs. */
569 if (ctx
->has_stream_out
) {
570 unsigned offsets
[PIPE_MAX_SO_BUFFERS
];
571 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
572 offsets
[i
] = (unsigned)-1;
573 pipe
->set_stream_output_targets(pipe
,
574 ctx
->base
.saved_num_so_targets
,
575 ctx
->base
.saved_so_targets
, offsets
);
577 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
578 pipe_so_target_reference(&ctx
->base
.saved_so_targets
[i
], NULL
);
580 ctx
->base
.saved_num_so_targets
= ~0;
584 pipe
->bind_rasterizer_state(pipe
, ctx
->base
.saved_rs_state
);
585 ctx
->base
.saved_rs_state
= INVALID_PTR
;
588 static void blitter_check_saved_fragment_states(struct blitter_context_priv
*ctx
)
590 assert(ctx
->base
.saved_fs
!= INVALID_PTR
);
591 assert(ctx
->base
.saved_dsa_state
!= INVALID_PTR
);
592 assert(ctx
->base
.saved_blend_state
!= INVALID_PTR
);
595 static void blitter_restore_fragment_states(struct blitter_context_priv
*ctx
)
597 struct pipe_context
*pipe
= ctx
->base
.pipe
;
599 /* Fragment shader. */
600 ctx
->bind_fs_state(pipe
, ctx
->base
.saved_fs
);
601 ctx
->base
.saved_fs
= INVALID_PTR
;
603 /* Depth, stencil, alpha. */
604 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->base
.saved_dsa_state
);
605 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
608 pipe
->bind_blend_state(pipe
, ctx
->base
.saved_blend_state
);
609 ctx
->base
.saved_blend_state
= INVALID_PTR
;
612 if (ctx
->base
.is_sample_mask_saved
) {
613 pipe
->set_sample_mask(pipe
, ctx
->base
.saved_sample_mask
);
614 ctx
->base
.is_sample_mask_saved
= FALSE
;
617 /* Miscellaneous states. */
618 /* XXX check whether these are saved and whether they need to be restored
619 * (depending on the operation) */
620 pipe
->set_stencil_ref(pipe
, &ctx
->base
.saved_stencil_ref
);
621 pipe
->set_viewport_states(pipe
, 0, 1, &ctx
->base
.saved_viewport
);
624 static void blitter_check_saved_fb_state(struct blitter_context_priv
*ctx
)
626 assert(ctx
->base
.saved_fb_state
.nr_cbufs
!= ~0);
629 static void blitter_disable_render_cond(struct blitter_context_priv
*ctx
)
631 struct pipe_context
*pipe
= ctx
->base
.pipe
;
633 if (ctx
->base
.saved_render_cond_query
) {
634 pipe
->render_condition(pipe
, NULL
, FALSE
, 0);
638 static void blitter_restore_render_cond(struct blitter_context_priv
*ctx
)
640 struct pipe_context
*pipe
= ctx
->base
.pipe
;
642 if (ctx
->base
.saved_render_cond_query
) {
643 pipe
->render_condition(pipe
, ctx
->base
.saved_render_cond_query
,
644 ctx
->base
.saved_render_cond_cond
,
645 ctx
->base
.saved_render_cond_mode
);
646 ctx
->base
.saved_render_cond_query
= NULL
;
650 static void blitter_restore_fb_state(struct blitter_context_priv
*ctx
)
652 struct pipe_context
*pipe
= ctx
->base
.pipe
;
654 pipe
->set_framebuffer_state(pipe
, &ctx
->base
.saved_fb_state
);
655 util_unreference_framebuffer_state(&ctx
->base
.saved_fb_state
);
658 static void blitter_check_saved_textures(struct blitter_context_priv
*ctx
)
660 assert(ctx
->base
.saved_num_sampler_states
!= ~0);
661 assert(ctx
->base
.saved_num_sampler_views
!= ~0);
664 static void blitter_restore_textures(struct blitter_context_priv
*ctx
)
666 struct pipe_context
*pipe
= ctx
->base
.pipe
;
669 /* Fragment sampler states. */
670 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0,
671 ctx
->base
.saved_num_sampler_states
,
672 ctx
->base
.saved_sampler_states
);
674 ctx
->base
.saved_num_sampler_states
= ~0;
676 /* Fragment sampler views. */
677 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
678 ctx
->base
.saved_num_sampler_views
,
679 ctx
->base
.saved_sampler_views
);
681 for (i
= 0; i
< ctx
->base
.saved_num_sampler_views
; i
++)
682 pipe_sampler_view_reference(&ctx
->base
.saved_sampler_views
[i
], NULL
);
684 ctx
->base
.saved_num_sampler_views
= ~0;
687 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
688 int x1
, int y1
, int x2
, int y2
,
693 /* set vertex positions */
694 ctx
->vertices
[0][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v0.x*/
695 ctx
->vertices
[0][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v0.y*/
697 ctx
->vertices
[1][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v1.x*/
698 ctx
->vertices
[1][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v1.y*/
700 ctx
->vertices
[2][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v2.x*/
701 ctx
->vertices
[2][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v2.y*/
703 ctx
->vertices
[3][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v3.x*/
704 ctx
->vertices
[3][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v3.y*/
706 for (i
= 0; i
< 4; i
++)
707 ctx
->vertices
[i
][0][2] = depth
; /*z*/
710 ctx
->viewport
.scale
[0] = 0.5f
* ctx
->dst_width
;
711 ctx
->viewport
.scale
[1] = 0.5f
* ctx
->dst_height
;
712 ctx
->viewport
.scale
[2] = 1.0f
;
713 ctx
->viewport
.translate
[0] = 0.5f
* ctx
->dst_width
;
714 ctx
->viewport
.translate
[1] = 0.5f
* ctx
->dst_height
;
715 ctx
->viewport
.translate
[2] = 0.0f
;
716 ctx
->base
.pipe
->set_viewport_states(ctx
->base
.pipe
, 0, 1, &ctx
->viewport
);
719 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
720 const union pipe_color_union
*color
)
725 for (i
= 0; i
< 4; i
++) {
726 uint32_t *uiverts
= (uint32_t *)ctx
->vertices
[i
][1];
727 uiverts
[0] = color
->ui
[0];
728 uiverts
[1] = color
->ui
[1];
729 uiverts
[2] = color
->ui
[2];
730 uiverts
[3] = color
->ui
[3];
733 for (i
= 0; i
< 4; i
++) {
734 ctx
->vertices
[i
][1][0] = 0;
735 ctx
->vertices
[i
][1][1] = 0;
736 ctx
->vertices
[i
][1][2] = 0;
737 ctx
->vertices
[i
][1][3] = 0;
742 static void get_texcoords(struct pipe_sampler_view
*src
,
743 unsigned src_width0
, unsigned src_height0
,
744 int x1
, int y1
, int x2
, int y2
,
747 struct pipe_resource
*tex
= src
->texture
;
748 unsigned level
= src
->u
.tex
.first_level
;
749 boolean normalized
= tex
->target
!= PIPE_TEXTURE_RECT
&&
750 tex
->nr_samples
<= 1;
753 out
[0] = x1
/ (float)u_minify(src_width0
, level
);
754 out
[1] = y1
/ (float)u_minify(src_height0
, level
);
755 out
[2] = x2
/ (float)u_minify(src_width0
, level
);
756 out
[3] = y2
/ (float)u_minify(src_height0
, level
);
765 static void set_texcoords_in_vertices(const float coord
[4],
766 float *out
, unsigned stride
)
768 out
[0] = coord
[0]; /*t0.s*/
769 out
[1] = coord
[1]; /*t0.t*/
771 out
[0] = coord
[2]; /*t1.s*/
772 out
[1] = coord
[1]; /*t1.t*/
774 out
[0] = coord
[2]; /*t2.s*/
775 out
[1] = coord
[3]; /*t2.t*/
777 out
[0] = coord
[0]; /*t3.s*/
778 out
[1] = coord
[3]; /*t3.t*/
781 static void blitter_set_texcoords(struct blitter_context_priv
*ctx
,
782 struct pipe_sampler_view
*src
,
783 unsigned src_width0
, unsigned src_height0
,
784 float layer
, unsigned sample
,
785 int x1
, int y1
, int x2
, int y2
)
789 float face_coord
[4][2];
791 get_texcoords(src
, src_width0
, src_height0
, x1
, y1
, x2
, y2
, coord
);
793 if (src
->texture
->target
== PIPE_TEXTURE_CUBE
||
794 src
->texture
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
795 set_texcoords_in_vertices(coord
, &face_coord
[0][0], 2);
796 util_map_texcoords2d_onto_cubemap((unsigned)layer
% 6,
797 /* pointer, stride in floats */
798 &face_coord
[0][0], 2,
799 &ctx
->vertices
[0][1][0], 8,
802 set_texcoords_in_vertices(coord
, &ctx
->vertices
[0][1][0], 8);
806 switch (src
->texture
->target
) {
807 case PIPE_TEXTURE_3D
:
809 float r
= layer
/ (float)u_minify(src
->texture
->depth0
,
810 src
->u
.tex
.first_level
);
811 for (i
= 0; i
< 4; i
++)
812 ctx
->vertices
[i
][1][2] = r
; /*r*/
816 case PIPE_TEXTURE_1D_ARRAY
:
817 for (i
= 0; i
< 4; i
++)
818 ctx
->vertices
[i
][1][1] = (float) layer
; /*t*/
821 case PIPE_TEXTURE_2D_ARRAY
:
822 for (i
= 0; i
< 4; i
++) {
823 ctx
->vertices
[i
][1][2] = (float) layer
; /*r*/
824 ctx
->vertices
[i
][1][3] = (float) sample
; /*q*/
828 case PIPE_TEXTURE_CUBE_ARRAY
:
829 for (i
= 0; i
< 4; i
++)
830 ctx
->vertices
[i
][1][3] = (float) ((unsigned)layer
/ 6); /*w*/
833 case PIPE_TEXTURE_2D
:
834 for (i
= 0; i
< 4; i
++) {
835 ctx
->vertices
[i
][1][3] = (float) sample
; /*r*/
843 static void blitter_set_dst_dimensions(struct blitter_context_priv
*ctx
,
844 unsigned width
, unsigned height
)
846 ctx
->dst_width
= width
;
847 ctx
->dst_height
= height
;
850 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
851 enum pipe_format format
,
852 enum pipe_texture_target target
,
853 unsigned src_nr_samples
,
854 unsigned dst_nr_samples
,
857 struct pipe_context
*pipe
= ctx
->base
.pipe
;
858 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, src_nr_samples
);
859 enum tgsi_return_type stype
;
861 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
863 if (util_format_is_pure_uint(format
))
864 stype
= TGSI_RETURN_TYPE_UINT
;
865 else if (util_format_is_pure_sint(format
))
866 stype
= TGSI_RETURN_TYPE_SINT
;
868 stype
= TGSI_RETURN_TYPE_FLOAT
;
870 if (src_nr_samples
> 1) {
873 if (dst_nr_samples
<= 1) {
874 /* The destination has one sample, so we'll do color resolve. */
875 unsigned index
= GET_MSAA_RESOLVE_FS_IDX(src_nr_samples
);
879 if (stype
== TGSI_RETURN_TYPE_UINT
)
880 shader
= &ctx
->fs_resolve_uint
[target
][index
][filter
];
881 else if (stype
== TGSI_RETURN_TYPE_SINT
)
882 shader
= &ctx
->fs_resolve_sint
[target
][index
][filter
];
884 shader
= &ctx
->fs_resolve
[target
][index
][filter
];
887 assert(!ctx
->cached_all_shaders
);
888 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
889 *shader
= util_make_fs_msaa_resolve_bilinear(pipe
, tgsi_tex
,
894 *shader
= util_make_fs_msaa_resolve(pipe
, tgsi_tex
,
901 /* The destination has multiple samples, we'll do
902 * an MSAA->MSAA copy.
904 if (stype
== TGSI_RETURN_TYPE_UINT
)
905 shader
= &ctx
->fs_texfetch_col_msaa_uint
[target
];
906 else if (stype
== TGSI_RETURN_TYPE_SINT
)
907 shader
= &ctx
->fs_texfetch_col_msaa_sint
[target
];
909 shader
= &ctx
->fs_texfetch_col_msaa
[target
];
911 /* Create the fragment shader on-demand. */
913 assert(!ctx
->cached_all_shaders
);
914 *shader
= util_make_fs_blit_msaa_color(pipe
, tgsi_tex
, stype
);
922 if (stype
== TGSI_RETURN_TYPE_UINT
)
923 shader
= &ctx
->fs_texfetch_col_uint
[target
];
924 else if (stype
== TGSI_RETURN_TYPE_SINT
)
925 shader
= &ctx
->fs_texfetch_col_sint
[target
];
927 shader
= &ctx
->fs_texfetch_col
[target
];
929 /* Create the fragment shader on-demand. */
931 assert(!ctx
->cached_all_shaders
);
932 *shader
= util_make_fragment_tex_shader(pipe
, tgsi_tex
,
933 TGSI_INTERPOLATE_LINEAR
,
942 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
943 enum pipe_texture_target target
,
946 struct pipe_context
*pipe
= ctx
->base
.pipe
;
948 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
950 if (nr_samples
> 1) {
951 void **shader
= &ctx
->fs_texfetch_depth_msaa
[target
];
953 /* Create the fragment shader on-demand. */
956 assert(!ctx
->cached_all_shaders
);
957 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
958 *shader
= util_make_fs_blit_msaa_depth(pipe
, tgsi_tex
);
963 void **shader
= &ctx
->fs_texfetch_depth
[target
];
965 /* Create the fragment shader on-demand. */
968 assert(!ctx
->cached_all_shaders
);
969 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
971 util_make_fragment_tex_shader_writedepth(pipe
, tgsi_tex
,
972 TGSI_INTERPOLATE_LINEAR
);
980 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv
*ctx
,
981 enum pipe_texture_target target
,
984 struct pipe_context
*pipe
= ctx
->base
.pipe
;
986 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
988 if (nr_samples
> 1) {
989 void **shader
= &ctx
->fs_texfetch_depthstencil_msaa
[target
];
991 /* Create the fragment shader on-demand. */
994 assert(!ctx
->cached_all_shaders
);
995 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
996 *shader
= util_make_fs_blit_msaa_depthstencil(pipe
, tgsi_tex
);
1001 void **shader
= &ctx
->fs_texfetch_depthstencil
[target
];
1003 /* Create the fragment shader on-demand. */
1006 assert(!ctx
->cached_all_shaders
);
1007 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1009 util_make_fragment_tex_shader_writedepthstencil(pipe
, tgsi_tex
,
1010 TGSI_INTERPOLATE_LINEAR
);
1018 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv
*ctx
,
1019 enum pipe_texture_target target
,
1020 unsigned nr_samples
)
1022 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1024 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1026 if (nr_samples
> 1) {
1027 void **shader
= &ctx
->fs_texfetch_stencil_msaa
[target
];
1029 /* Create the fragment shader on-demand. */
1032 assert(!ctx
->cached_all_shaders
);
1033 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1034 *shader
= util_make_fs_blit_msaa_stencil(pipe
, tgsi_tex
);
1039 void **shader
= &ctx
->fs_texfetch_stencil
[target
];
1041 /* Create the fragment shader on-demand. */
1044 assert(!ctx
->cached_all_shaders
);
1045 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1047 util_make_fragment_tex_shader_writestencil(pipe
, tgsi_tex
,
1048 TGSI_INTERPOLATE_LINEAR
);
1057 * Generate and save all fragment shaders that we will ever need for
1058 * blitting. Drivers which use the 'draw' fallbacks will typically use
1059 * this to make sure we generate/use shaders that don't go through the
1060 * draw module's wrapper functions.
1062 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
)
1064 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1065 struct pipe_context
*pipe
= blitter
->pipe
;
1066 struct pipe_screen
*screen
= pipe
->screen
;
1067 unsigned samples
, j
, f
, target
, max_samples
;
1068 boolean has_arraytex
, has_cubearraytex
;
1070 max_samples
= ctx
->has_texture_multisample
? 2 : 1;
1071 has_arraytex
= screen
->get_param(screen
,
1072 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
) != 0;
1073 has_cubearraytex
= screen
->get_param(screen
,
1074 PIPE_CAP_CUBE_MAP_ARRAY
) != 0;
1076 /* It only matters if i <= 1 or > 1. */
1077 for (samples
= 1; samples
<= max_samples
; samples
++) {
1078 for (target
= PIPE_TEXTURE_1D
; target
< PIPE_MAX_TEXTURE_TYPES
; target
++) {
1079 if (!has_arraytex
&&
1080 (target
== PIPE_TEXTURE_1D_ARRAY
||
1081 target
== PIPE_TEXTURE_2D_ARRAY
)) {
1084 if (!has_cubearraytex
&&
1085 (target
== PIPE_TEXTURE_CUBE_ARRAY
))
1089 (target
!= PIPE_TEXTURE_2D
&&
1090 target
!= PIPE_TEXTURE_2D_ARRAY
))
1093 /* If samples == 1, the shaders read one texel. If samples >= 1,
1094 * they read one sample.
1096 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
, target
,
1097 samples
, samples
, 0);
1098 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
, target
,
1099 samples
, samples
, 0);
1100 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
, target
,
1101 samples
, samples
, 0);
1102 blitter_get_fs_texfetch_depth(ctx
, target
, samples
);
1103 if (ctx
->has_stencil_export
) {
1104 blitter_get_fs_texfetch_depthstencil(ctx
, target
, samples
);
1105 blitter_get_fs_texfetch_stencil(ctx
, target
, samples
);
1111 /* MSAA resolve shaders. */
1112 for (j
= 2; j
< 32; j
++) {
1113 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R32_FLOAT
,
1115 PIPE_BIND_SAMPLER_VIEW
)) {
1119 for (f
= 0; f
< 2; f
++) {
1120 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
, target
,
1122 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
, target
,
1124 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
, target
,
1131 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
1133 ctx
->fs_write_one_cbuf
=
1134 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1135 TGSI_INTERPOLATE_CONSTANT
, FALSE
);
1137 ctx
->fs_write_all_cbufs
=
1138 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1139 TGSI_INTERPOLATE_CONSTANT
, TRUE
);
1141 ctx
->cached_all_shaders
= TRUE
;
1144 static void blitter_set_common_draw_rect_state(struct blitter_context_priv
*ctx
,
1148 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1150 pipe
->bind_rasterizer_state(pipe
, scissor
? ctx
->rs_state_scissor
1153 bind_vs_layered(ctx
);
1155 bind_vs_passthrough(ctx
);
1157 if (ctx
->has_geometry_shader
)
1158 pipe
->bind_gs_state(pipe
, NULL
);
1159 if (ctx
->has_tessellation
) {
1160 pipe
->bind_tcs_state(pipe
, NULL
);
1161 pipe
->bind_tes_state(pipe
, NULL
);
1163 if (ctx
->has_stream_out
)
1164 pipe
->set_stream_output_targets(pipe
, 0, NULL
, NULL
);
1167 static void blitter_draw(struct blitter_context_priv
*ctx
,
1168 int x1
, int y1
, int x2
, int y2
, float depth
,
1169 unsigned num_instances
)
1171 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1172 struct pipe_vertex_buffer vb
= {0};
1174 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
1176 vb
.stride
= 8 * sizeof(float);
1178 u_upload_data(ctx
->upload
, 0, sizeof(ctx
->vertices
), ctx
->vertices
,
1179 &vb
.buffer_offset
, &vb
.buffer
);
1180 u_upload_unmap(ctx
->upload
);
1182 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1183 util_draw_arrays_instanced(pipe
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
1185 pipe_resource_reference(&vb
.buffer
, NULL
);
1188 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
1189 int x1
, int y1
, int x2
, int y2
, float depth
,
1190 enum blitter_attrib_type type
,
1191 const union pipe_color_union
*attrib
)
1193 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1196 case UTIL_BLITTER_ATTRIB_COLOR
:
1197 blitter_set_clear_color(ctx
, attrib
);
1200 case UTIL_BLITTER_ATTRIB_TEXCOORD
:
1201 set_texcoords_in_vertices(attrib
->f
, &ctx
->vertices
[0][1][0], 8);
1207 blitter_draw(ctx
, x1
, y1
, x2
, y2
, depth
, 1);
1210 static void *get_clear_blend_state(struct blitter_context_priv
*ctx
,
1211 unsigned clear_buffers
)
1213 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1216 clear_buffers
&= PIPE_CLEAR_COLOR
;
1218 /* Return an existing blend state. */
1220 return ctx
->blend
[0];
1222 index
= GET_CLEAR_BLEND_STATE_IDX(clear_buffers
);
1224 if (ctx
->blend_clear
[index
])
1225 return ctx
->blend_clear
[index
];
1227 /* Create a new one. */
1229 struct pipe_blend_state blend
= {0};
1232 blend
.independent_blend_enable
= 1;
1234 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1235 if (clear_buffers
& (PIPE_CLEAR_COLOR0
<< i
)) {
1236 blend
.rt
[i
].colormask
= PIPE_MASK_RGBA
;
1240 ctx
->blend_clear
[index
] = pipe
->create_blend_state(pipe
, &blend
);
1242 return ctx
->blend_clear
[index
];
1245 static void util_blitter_clear_custom(struct blitter_context
*blitter
,
1246 unsigned width
, unsigned height
,
1247 unsigned num_layers
,
1248 unsigned clear_buffers
,
1249 const union pipe_color_union
*color
,
1250 double depth
, unsigned stencil
,
1251 void *custom_blend
, void *custom_dsa
)
1253 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1254 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1255 struct pipe_stencil_ref sr
= { { 0 } };
1257 assert(ctx
->has_layered
|| num_layers
<= 1);
1259 blitter_set_running_flag(ctx
);
1260 blitter_check_saved_vertex_states(ctx
);
1261 blitter_check_saved_fragment_states(ctx
);
1262 blitter_disable_render_cond(ctx
);
1266 pipe
->bind_blend_state(pipe
, custom_blend
);
1268 pipe
->bind_blend_state(pipe
, get_clear_blend_state(ctx
, clear_buffers
));
1272 pipe
->bind_depth_stencil_alpha_state(pipe
, custom_dsa
);
1273 } else if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1274 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1275 } else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
1276 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1277 } else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
1278 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1280 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1283 sr
.ref_value
[0] = stencil
& 0xff;
1284 pipe
->set_stencil_ref(pipe
, &sr
);
1286 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1287 bind_fs_write_all_cbufs(ctx
);
1288 pipe
->set_sample_mask(pipe
, ~0);
1290 blitter_set_dst_dimensions(ctx
, width
, height
);
1292 if (num_layers
> 1 && ctx
->has_layered
) {
1293 blitter_set_common_draw_rect_state(ctx
, FALSE
, TRUE
);
1294 blitter_set_clear_color(ctx
, color
);
1295 blitter_draw(ctx
, 0, 0, width
, height
, depth
, num_layers
);
1298 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1299 blitter
->draw_rectangle(blitter
, 0, 0, width
, height
, (float) depth
,
1300 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1303 blitter_restore_vertex_states(ctx
);
1304 blitter_restore_fragment_states(ctx
);
1305 blitter_restore_render_cond(ctx
);
1306 blitter_unset_running_flag(ctx
);
1309 void util_blitter_clear(struct blitter_context
*blitter
,
1310 unsigned width
, unsigned height
, unsigned num_layers
,
1311 unsigned clear_buffers
,
1312 const union pipe_color_union
*color
,
1313 double depth
, unsigned stencil
)
1315 util_blitter_clear_custom(blitter
, width
, height
, num_layers
,
1316 clear_buffers
, color
, depth
, stencil
,
1320 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1321 unsigned width
, unsigned height
,
1322 double depth
, void *custom_dsa
)
1324 static const union pipe_color_union color
;
1325 util_blitter_clear_custom(blitter
, width
, height
, 0, 0, &color
, depth
, 0,
1329 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1330 struct pipe_resource
*dst
,
1334 memset(dst_templ
, 0, sizeof(*dst_templ
));
1335 dst_templ
->format
= util_format_linear(dst
->format
);
1336 dst_templ
->u
.tex
.level
= dstlevel
;
1337 dst_templ
->u
.tex
.first_layer
= dstz
;
1338 dst_templ
->u
.tex
.last_layer
= dstz
;
1341 static struct pipe_surface
*
1342 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
1343 struct pipe_surface
*surf
)
1345 struct pipe_surface dst_templ
;
1347 memset(&dst_templ
, 0, sizeof(dst_templ
));
1348 dst_templ
.format
= surf
->format
;
1349 dst_templ
.u
.tex
.level
= surf
->u
.tex
.level
;
1350 dst_templ
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
+ 1;
1351 dst_templ
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
+ 1;
1353 return pipe
->create_surface(pipe
, surf
->texture
, &dst_templ
);
1356 void util_blitter_default_src_texture(struct pipe_sampler_view
*src_templ
,
1357 struct pipe_resource
*src
,
1360 memset(src_templ
, 0, sizeof(*src_templ
));
1361 src_templ
->target
= src
->target
;
1362 src_templ
->format
= util_format_linear(src
->format
);
1363 src_templ
->u
.tex
.first_level
= srclevel
;
1364 src_templ
->u
.tex
.last_level
= srclevel
;
1365 src_templ
->u
.tex
.first_layer
= 0;
1366 src_templ
->u
.tex
.last_layer
=
1367 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1368 : src
->array_size
- 1;
1369 src_templ
->swizzle_r
= PIPE_SWIZZLE_RED
;
1370 src_templ
->swizzle_g
= PIPE_SWIZZLE_GREEN
;
1371 src_templ
->swizzle_b
= PIPE_SWIZZLE_BLUE
;
1372 src_templ
->swizzle_a
= PIPE_SWIZZLE_ALPHA
;
1375 static boolean
is_blit_generic_supported(struct blitter_context
*blitter
,
1376 const struct pipe_resource
*dst
,
1377 enum pipe_format dst_format
,
1378 const struct pipe_resource
*src
,
1379 enum pipe_format src_format
,
1382 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1383 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1387 const struct util_format_description
*desc
=
1388 util_format_description(dst_format
);
1389 boolean dst_has_stencil
= util_format_has_stencil(desc
);
1391 /* Stencil export must be supported for stencil copy. */
1392 if ((mask
& PIPE_MASK_S
) && dst_has_stencil
&&
1393 !ctx
->has_stencil_export
) {
1397 if (dst_has_stencil
|| util_format_has_depth(desc
))
1398 bind
= PIPE_BIND_DEPTH_STENCIL
;
1400 bind
= PIPE_BIND_RENDER_TARGET
;
1402 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1403 dst
->nr_samples
, bind
)) {
1409 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1413 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1414 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
)) {
1418 /* Check stencil sampler support for stencil copy. */
1419 if (mask
& PIPE_MASK_S
) {
1420 if (util_format_has_stencil(util_format_description(src_format
))) {
1421 enum pipe_format stencil_format
=
1422 util_format_stencil_only(src_format
);
1423 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1425 if (stencil_format
!= src_format
&&
1426 !screen
->is_format_supported(screen
, stencil_format
,
1427 src
->target
, src
->nr_samples
,
1428 PIPE_BIND_SAMPLER_VIEW
)) {
1438 boolean
util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1439 const struct pipe_resource
*dst
,
1440 const struct pipe_resource
*src
)
1442 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1443 src
, src
->format
, PIPE_MASK_RGBAZS
);
1446 boolean
util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1447 const struct pipe_blit_info
*info
)
1449 return is_blit_generic_supported(blitter
,
1450 info
->dst
.resource
, info
->dst
.format
,
1451 info
->src
.resource
, info
->src
.format
,
1455 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1456 struct pipe_resource
*dst
,
1458 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1459 struct pipe_resource
*src
,
1461 const struct pipe_box
*srcbox
)
1463 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1464 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1465 struct pipe_surface
*dst_view
, dst_templ
;
1466 struct pipe_sampler_view src_templ
, *src_view
;
1467 struct pipe_box dstbox
;
1470 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1472 u_box_3d(dstx
, dsty
, dstz
, abs(srcbox
->width
), abs(srcbox
->height
),
1473 abs(srcbox
->depth
), &dstbox
);
1475 /* Initialize the surface. */
1476 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1477 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1479 /* Initialize the sampler view. */
1480 util_blitter_default_src_texture(&src_templ
, src
, src_level
);
1481 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1484 util_blitter_blit_generic(blitter
, dst_view
, &dstbox
,
1485 src_view
, srcbox
, src
->width0
, src
->height0
,
1486 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
);
1488 pipe_surface_reference(&dst_view
, NULL
);
1489 pipe_sampler_view_reference(&src_view
, NULL
);
1492 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1493 struct pipe_surface
*dst
,
1494 const struct pipe_box
*dstbox
,
1495 struct pipe_sampler_view
*src
,
1496 const struct pipe_box
*srcbox
,
1497 unsigned src_width0
, unsigned src_height0
,
1498 unsigned mask
, unsigned filter
,
1499 const struct pipe_scissor_state
*scissor
)
1501 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1502 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1503 struct pipe_framebuffer_state fb_state
;
1504 enum pipe_texture_target src_target
= src
->texture
->target
;
1505 unsigned src_samples
= src
->texture
->nr_samples
;
1506 unsigned dst_samples
= dst
->texture
->nr_samples
;
1507 boolean has_depth
, has_stencil
, has_color
;
1508 boolean blit_stencil
, blit_depth
, blit_color
;
1509 void *sampler_state
;
1510 const struct util_format_description
*src_desc
=
1511 util_format_description(src
->format
);
1512 const struct util_format_description
*dst_desc
=
1513 util_format_description(dst
->format
);
1515 has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
&&
1516 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1517 has_depth
= util_format_has_depth(src_desc
) &&
1518 util_format_has_depth(dst_desc
);
1519 has_stencil
= util_format_has_stencil(src_desc
) &&
1520 util_format_has_stencil(dst_desc
);
1522 blit_color
= has_color
&& (mask
& PIPE_MASK_RGBA
);
1523 blit_depth
= has_depth
&& (mask
& PIPE_MASK_Z
);
1524 blit_stencil
= has_stencil
&& (mask
& PIPE_MASK_S
) &&
1525 ctx
->has_stencil_export
;
1527 if (!blit_stencil
&& !blit_depth
&& !blit_color
) {
1532 (dstbox
->width
== abs(srcbox
->width
) &&
1533 dstbox
->height
== abs(srcbox
->height
))) {
1534 filter
= PIPE_TEX_FILTER_NEAREST
;
1537 /* Check whether the states are properly saved. */
1538 blitter_set_running_flag(ctx
);
1539 blitter_check_saved_vertex_states(ctx
);
1540 blitter_check_saved_fragment_states(ctx
);
1541 blitter_check_saved_textures(ctx
);
1542 blitter_check_saved_fb_state(ctx
);
1543 blitter_disable_render_cond(ctx
);
1545 /* Initialize framebuffer state. */
1546 fb_state
.width
= dst
->width
;
1547 fb_state
.height
= dst
->height
;
1548 fb_state
.nr_cbufs
= blit_depth
|| blit_stencil
? 0 : 1;
1549 fb_state
.cbufs
[0] = NULL
;
1550 fb_state
.zsbuf
= NULL
;
1552 if (blit_depth
|| blit_stencil
) {
1553 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1555 if (blit_depth
&& blit_stencil
) {
1556 pipe
->bind_depth_stencil_alpha_state(pipe
,
1557 ctx
->dsa_write_depth_stencil
);
1558 ctx
->bind_fs_state(pipe
,
1559 blitter_get_fs_texfetch_depthstencil(ctx
, src_target
,
1561 } else if (blit_depth
) {
1562 pipe
->bind_depth_stencil_alpha_state(pipe
,
1563 ctx
->dsa_write_depth_keep_stencil
);
1564 ctx
->bind_fs_state(pipe
,
1565 blitter_get_fs_texfetch_depth(ctx
, src_target
,
1567 } else { /* is_stencil */
1568 pipe
->bind_depth_stencil_alpha_state(pipe
,
1569 ctx
->dsa_keep_depth_write_stencil
);
1570 ctx
->bind_fs_state(pipe
,
1571 blitter_get_fs_texfetch_stencil(ctx
, src_target
,
1576 pipe
->bind_blend_state(pipe
, ctx
->blend
[mask
& PIPE_MASK_RGBA
]);
1577 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1578 ctx
->bind_fs_state(pipe
,
1579 blitter_get_fs_texfetch_col(ctx
, src
->format
, src_target
,
1580 src_samples
, dst_samples
, filter
));
1583 /* Set the linear filter only for scaled color non-MSAA blits. */
1584 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
1585 if (src_target
== PIPE_TEXTURE_RECT
) {
1586 sampler_state
= ctx
->sampler_state_rect_linear
;
1588 sampler_state
= ctx
->sampler_state_linear
;
1591 if (src_target
== PIPE_TEXTURE_RECT
) {
1592 sampler_state
= ctx
->sampler_state_rect
;
1594 sampler_state
= ctx
->sampler_state
;
1599 if (blit_depth
&& blit_stencil
) {
1600 /* Setup two samplers, one for depth and the other one for stencil. */
1601 struct pipe_sampler_view templ
;
1602 struct pipe_sampler_view
*views
[2];
1603 void *samplers
[2] = {sampler_state
, sampler_state
};
1606 templ
.format
= util_format_stencil_only(templ
.format
);
1607 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1610 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1612 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, views
);
1613 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, samplers
);
1615 pipe_sampler_view_reference(&views
[1], NULL
);
1616 } else if (blit_stencil
) {
1617 /* Set a stencil-only sampler view for it not to sample depth instead. */
1618 struct pipe_sampler_view templ
;
1619 struct pipe_sampler_view
*view
;
1622 templ
.format
= util_format_stencil_only(templ
.format
);
1623 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1625 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1627 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &view
);
1628 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1629 0, 1, &sampler_state
);
1631 pipe_sampler_view_reference(&view
, NULL
);
1633 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src
);
1634 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1635 0, 1, &sampler_state
);
1638 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1640 pipe
->set_scissor_states(pipe
, 0, 1, scissor
);
1643 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
, FALSE
);
1644 blitter_set_dst_dimensions(ctx
, dst
->width
, dst
->height
);
1646 if ((src_target
== PIPE_TEXTURE_1D
||
1647 src_target
== PIPE_TEXTURE_2D
||
1648 src_target
== PIPE_TEXTURE_RECT
) &&
1650 /* Draw the quad with the draw_rectangle callback. */
1652 /* Set texture coordinates. - use a pipe color union
1653 * for interface purposes.
1654 * XXX pipe_color_union is a wrong name since we use that to set
1655 * texture coordinates too.
1657 union pipe_color_union coord
;
1658 get_texcoords(src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1659 srcbox
->x
+srcbox
->width
, srcbox
->y
+srcbox
->height
, coord
.f
);
1661 /* Set framebuffer state. */
1662 if (blit_depth
|| blit_stencil
) {
1663 fb_state
.zsbuf
= dst
;
1665 fb_state
.cbufs
[0] = dst
;
1667 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1670 pipe
->set_sample_mask(pipe
, ~0);
1671 blitter
->draw_rectangle(blitter
, dstbox
->x
, dstbox
->y
,
1672 dstbox
->x
+ dstbox
->width
,
1673 dstbox
->y
+ dstbox
->height
, 0,
1674 UTIL_BLITTER_ATTRIB_TEXCOORD
, &coord
);
1676 /* Draw the quad with the generic codepath. */
1678 for (dst_z
= 0; dst_z
< dstbox
->depth
; dst_z
++) {
1679 struct pipe_surface
*old
;
1680 float dst2src_scale
= srcbox
->depth
/ (float)dstbox
->depth
;
1682 /* Scale Z properly if the blit is scaled.
1684 * When downscaling, we want the coordinates centered, so that
1685 * mipmapping works for 3D textures. For example, when generating
1686 * a 4x4x4 level, this wouldn't average the pixels:
1688 * src Z: 0 1 2 3 4 5 6 7
1691 * Because the pixels are not centered below the pixels of the higher
1692 * level. Therefore, we want this:
1693 * src Z: 0 1 2 3 4 5 6 7
1696 * dst_offset defines the offset needed for centering the pixels and
1697 * it works with any scaling (not just 2x).
1699 float dst_offset
= ((srcbox
->depth
- 1) -
1700 (dstbox
->depth
- 1) * dst2src_scale
) * 0.5;
1701 float src_z
= (dst_z
+ dst_offset
) * dst2src_scale
;
1703 /* Set framebuffer state. */
1704 if (blit_depth
|| blit_stencil
) {
1705 fb_state
.zsbuf
= dst
;
1707 fb_state
.cbufs
[0] = dst
;
1709 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1711 /* See if we need to blit a multisample or singlesample buffer. */
1712 if (src_samples
== dst_samples
&& dst_samples
> 1) {
1714 unsigned i
, max_sample
= dst_samples
- 1;
1716 for (i
= 0; i
<= max_sample
; i
++) {
1717 pipe
->set_sample_mask(pipe
, 1 << i
);
1718 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1720 i
, srcbox
->x
, srcbox
->y
,
1721 srcbox
->x
+ srcbox
->width
,
1722 srcbox
->y
+ srcbox
->height
);
1723 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1724 dstbox
->x
+ dstbox
->width
,
1725 dstbox
->y
+ dstbox
->height
, 0, 1);
1728 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1729 pipe
->set_sample_mask(pipe
, ~0);
1730 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1731 srcbox
->z
+ src_z
, 0,
1732 srcbox
->x
, srcbox
->y
,
1733 srcbox
->x
+ srcbox
->width
,
1734 srcbox
->y
+ srcbox
->height
);
1735 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1736 dstbox
->x
+ dstbox
->width
,
1737 dstbox
->y
+ dstbox
->height
, 0, 1);
1740 /* Get the next surface or (if this is the last iteration)
1741 * just unreference the last one. */
1743 if (dst_z
< dstbox
->depth
-1) {
1744 dst
= ctx
->base
.get_next_surface_layer(ctx
->base
.pipe
, dst
);
1747 pipe_surface_reference(&old
, NULL
);
1752 blitter_restore_vertex_states(ctx
);
1753 blitter_restore_fragment_states(ctx
);
1754 blitter_restore_textures(ctx
);
1755 blitter_restore_fb_state(ctx
);
1757 pipe
->set_scissor_states(pipe
, 0, 1, &ctx
->base
.saved_scissor
);
1759 blitter_restore_render_cond(ctx
);
1760 blitter_unset_running_flag(ctx
);
1764 util_blitter_blit(struct blitter_context
*blitter
,
1765 const struct pipe_blit_info
*info
)
1767 struct pipe_resource
*dst
= info
->dst
.resource
;
1768 struct pipe_resource
*src
= info
->src
.resource
;
1769 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1770 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1771 struct pipe_surface
*dst_view
, dst_templ
;
1772 struct pipe_sampler_view src_templ
, *src_view
;
1774 /* Initialize the surface. */
1775 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
1777 dst_templ
.format
= info
->dst
.format
;
1778 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1780 /* Initialize the sampler view. */
1781 util_blitter_default_src_texture(&src_templ
, src
, info
->src
.level
);
1782 src_templ
.format
= info
->src
.format
;
1783 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1786 util_blitter_blit_generic(blitter
, dst_view
, &info
->dst
.box
,
1787 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
1788 info
->mask
, info
->filter
,
1789 info
->scissor_enable
? &info
->scissor
: NULL
);
1791 pipe_surface_reference(&dst_view
, NULL
);
1792 pipe_sampler_view_reference(&src_view
, NULL
);
1795 /* Clear a region of a color surface to a constant value. */
1796 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
1797 struct pipe_surface
*dstsurf
,
1798 const union pipe_color_union
*color
,
1799 unsigned dstx
, unsigned dsty
,
1800 unsigned width
, unsigned height
)
1802 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1803 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1804 struct pipe_framebuffer_state fb_state
;
1806 assert(dstsurf
->texture
);
1807 if (!dstsurf
->texture
)
1810 /* check the saved state */
1811 blitter_set_running_flag(ctx
);
1812 blitter_check_saved_vertex_states(ctx
);
1813 blitter_check_saved_fragment_states(ctx
);
1814 blitter_check_saved_fb_state(ctx
);
1815 blitter_disable_render_cond(ctx
);
1818 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
]);
1819 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1820 bind_fs_write_one_cbuf(ctx
);
1821 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1823 /* set a framebuffer state */
1824 fb_state
.width
= dstsurf
->width
;
1825 fb_state
.height
= dstsurf
->height
;
1826 fb_state
.nr_cbufs
= 1;
1827 fb_state
.cbufs
[0] = dstsurf
;
1829 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1830 pipe
->set_sample_mask(pipe
, ~0);
1832 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1833 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1834 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
1835 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1837 blitter_restore_vertex_states(ctx
);
1838 blitter_restore_fragment_states(ctx
);
1839 blitter_restore_fb_state(ctx
);
1840 blitter_restore_render_cond(ctx
);
1841 blitter_unset_running_flag(ctx
);
1844 /* Clear a region of a depth stencil surface. */
1845 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
1846 struct pipe_surface
*dstsurf
,
1847 unsigned clear_flags
,
1850 unsigned dstx
, unsigned dsty
,
1851 unsigned width
, unsigned height
)
1853 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1854 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1855 struct pipe_framebuffer_state fb_state
;
1856 struct pipe_stencil_ref sr
= { { 0 } };
1858 assert(dstsurf
->texture
);
1859 if (!dstsurf
->texture
)
1862 /* check the saved state */
1863 blitter_set_running_flag(ctx
);
1864 blitter_check_saved_vertex_states(ctx
);
1865 blitter_check_saved_fragment_states(ctx
);
1866 blitter_check_saved_fb_state(ctx
);
1867 blitter_disable_render_cond(ctx
);
1870 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1871 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1872 sr
.ref_value
[0] = stencil
& 0xff;
1873 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1874 pipe
->set_stencil_ref(pipe
, &sr
);
1876 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
1877 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1879 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
1880 sr
.ref_value
[0] = stencil
& 0xff;
1881 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1882 pipe
->set_stencil_ref(pipe
, &sr
);
1885 /* hmm that should be illegal probably, or make it a no-op somewhere */
1886 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1889 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1891 /* set a framebuffer state */
1892 fb_state
.width
= dstsurf
->width
;
1893 fb_state
.height
= dstsurf
->height
;
1894 fb_state
.nr_cbufs
= 0;
1895 fb_state
.cbufs
[0] = 0;
1896 fb_state
.zsbuf
= dstsurf
;
1897 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1898 pipe
->set_sample_mask(pipe
, ~0);
1900 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1901 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1902 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
,
1904 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
1906 blitter_restore_vertex_states(ctx
);
1907 blitter_restore_fragment_states(ctx
);
1908 blitter_restore_fb_state(ctx
);
1909 blitter_restore_render_cond(ctx
);
1910 blitter_unset_running_flag(ctx
);
1913 /* draw a rectangle across a region using a custom dsa stage - for r600g */
1914 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
1915 struct pipe_surface
*zsurf
,
1916 struct pipe_surface
*cbsurf
,
1917 unsigned sample_mask
,
1918 void *dsa_stage
, float depth
)
1920 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1921 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1922 struct pipe_framebuffer_state fb_state
;
1924 assert(zsurf
->texture
);
1925 if (!zsurf
->texture
)
1928 /* check the saved state */
1929 blitter_set_running_flag(ctx
);
1930 blitter_check_saved_vertex_states(ctx
);
1931 blitter_check_saved_fragment_states(ctx
);
1932 blitter_check_saved_fb_state(ctx
);
1933 blitter_disable_render_cond(ctx
);
1936 pipe
->bind_blend_state(pipe
, cbsurf
? ctx
->blend
[PIPE_MASK_RGBA
] :
1938 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
1940 bind_fs_write_one_cbuf(ctx
);
1943 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1945 /* set a framebuffer state */
1946 fb_state
.width
= zsurf
->width
;
1947 fb_state
.height
= zsurf
->height
;
1948 fb_state
.nr_cbufs
= 1;
1950 fb_state
.cbufs
[0] = cbsurf
;
1951 fb_state
.nr_cbufs
= 1;
1953 fb_state
.cbufs
[0] = NULL
;
1954 fb_state
.nr_cbufs
= 0;
1956 fb_state
.zsbuf
= zsurf
;
1957 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1958 pipe
->set_sample_mask(pipe
, sample_mask
);
1960 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1961 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
1962 blitter
->draw_rectangle(blitter
, 0, 0, zsurf
->width
, zsurf
->height
, depth
,
1963 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
1965 blitter_restore_vertex_states(ctx
);
1966 blitter_restore_fragment_states(ctx
);
1967 blitter_restore_fb_state(ctx
);
1968 blitter_restore_render_cond(ctx
);
1969 blitter_unset_running_flag(ctx
);
1972 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
1973 struct pipe_resource
*dst
,
1975 struct pipe_resource
*src
,
1979 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1980 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1981 struct pipe_vertex_buffer vb
;
1982 struct pipe_stream_output_target
*so_target
;
1983 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
1985 if (srcx
>= src
->width0
||
1986 dstx
>= dst
->width0
) {
1989 if (srcx
+ size
> src
->width0
) {
1990 size
= src
->width0
- srcx
;
1992 if (dstx
+ size
> dst
->width0
) {
1993 size
= dst
->width0
- dstx
;
1996 /* Drivers not capable of Stream Out should not call this function
1997 * in the first place. */
1998 assert(ctx
->has_stream_out
);
2000 /* Some alignment is required. */
2001 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
2002 !ctx
->has_stream_out
) {
2003 struct pipe_box box
;
2004 u_box_1d(srcx
, size
, &box
);
2005 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
2009 blitter_set_running_flag(ctx
);
2010 blitter_check_saved_vertex_states(ctx
);
2011 blitter_disable_render_cond(ctx
);
2014 vb
.buffer_offset
= srcx
;
2017 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2018 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[0]);
2019 bind_vs_pos_only(ctx
);
2020 if (ctx
->has_geometry_shader
)
2021 pipe
->bind_gs_state(pipe
, NULL
);
2022 if (ctx
->has_tessellation
) {
2023 pipe
->bind_tcs_state(pipe
, NULL
);
2024 pipe
->bind_tes_state(pipe
, NULL
);
2026 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2028 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
2029 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2031 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2033 blitter_restore_vertex_states(ctx
);
2034 blitter_restore_render_cond(ctx
);
2035 blitter_unset_running_flag(ctx
);
2036 pipe_so_target_reference(&so_target
, NULL
);
2039 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
2040 struct pipe_resource
*dst
,
2041 unsigned offset
, unsigned size
,
2042 unsigned num_channels
,
2043 const union pipe_color_union
*clear_value
)
2045 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2046 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2047 struct pipe_vertex_buffer vb
= {0};
2048 struct pipe_stream_output_target
*so_target
;
2049 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2051 assert(num_channels
>= 1);
2052 assert(num_channels
<= 4);
2054 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
2056 * R600 uses this to initialize texture resources, so width0 might not be
2057 * what you think it is.
2060 /* Streamout is required. */
2061 if (!ctx
->has_stream_out
) {
2062 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2066 /* Some alignment is required. */
2067 if (offset
% 4 != 0 || size
% 4 != 0) {
2068 assert(!"Bad alignment in util_blitter_clear_buffer()");
2072 u_upload_data(ctx
->upload
, 0, num_channels
*4, clear_value
,
2073 &vb
.buffer_offset
, &vb
.buffer
);
2076 blitter_set_running_flag(ctx
);
2077 blitter_check_saved_vertex_states(ctx
);
2078 blitter_disable_render_cond(ctx
);
2080 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2081 pipe
->bind_vertex_elements_state(pipe
,
2082 ctx
->velem_state_readbuf
[num_channels
-1]);
2083 bind_vs_pos_only(ctx
);
2084 if (ctx
->has_geometry_shader
)
2085 pipe
->bind_gs_state(pipe
, NULL
);
2086 if (ctx
->has_tessellation
) {
2087 pipe
->bind_tcs_state(pipe
, NULL
);
2088 pipe
->bind_tes_state(pipe
, NULL
);
2090 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2092 so_target
= pipe
->create_stream_output_target(pipe
, dst
, offset
, size
);
2093 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2095 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2097 blitter_restore_vertex_states(ctx
);
2098 blitter_restore_render_cond(ctx
);
2099 blitter_unset_running_flag(ctx
);
2100 pipe_so_target_reference(&so_target
, NULL
);
2101 pipe_resource_reference(&vb
.buffer
, NULL
);
2104 /* probably radeon specific */
2105 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
2106 struct pipe_resource
*dst
,
2109 struct pipe_resource
*src
,
2111 unsigned sample_mask
,
2113 enum pipe_format format
)
2115 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2116 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2117 struct pipe_framebuffer_state fb_state
;
2118 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
2120 blitter_set_running_flag(ctx
);
2121 blitter_check_saved_vertex_states(ctx
);
2122 blitter_check_saved_fragment_states(ctx
);
2123 blitter_disable_render_cond(ctx
);
2126 pipe
->bind_blend_state(pipe
, custom_blend
);
2127 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2128 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2129 bind_fs_write_one_cbuf(ctx
);
2130 pipe
->set_sample_mask(pipe
, sample_mask
);
2132 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
2133 surf_tmpl
.format
= format
;
2134 surf_tmpl
.u
.tex
.level
= dst_level
;
2135 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
2136 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
2138 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
2140 surf_tmpl
.u
.tex
.level
= 0;
2141 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
2142 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
2144 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
2146 /* set a framebuffer state */
2147 fb_state
.width
= src
->width0
;
2148 fb_state
.height
= src
->height0
;
2149 fb_state
.nr_cbufs
= 2;
2150 fb_state
.cbufs
[0] = srcsurf
;
2151 fb_state
.cbufs
[1] = dstsurf
;
2152 fb_state
.zsbuf
= NULL
;
2153 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2155 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2156 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
2157 blitter
->draw_rectangle(blitter
, 0, 0, src
->width0
, src
->height0
,
2159 blitter_restore_fb_state(ctx
);
2160 blitter_restore_vertex_states(ctx
);
2161 blitter_restore_fragment_states(ctx
);
2162 blitter_restore_render_cond(ctx
);
2163 blitter_unset_running_flag(ctx
);
2165 pipe_surface_reference(&srcsurf
, NULL
);
2166 pipe_surface_reference(&dstsurf
, NULL
);
2169 void util_blitter_custom_color(struct blitter_context
*blitter
,
2170 struct pipe_surface
*dstsurf
,
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
;
2177 assert(dstsurf
->texture
);
2178 if (!dstsurf
->texture
)
2181 /* check the saved state */
2182 blitter_set_running_flag(ctx
);
2183 blitter_check_saved_vertex_states(ctx
);
2184 blitter_check_saved_fragment_states(ctx
);
2185 blitter_check_saved_fb_state(ctx
);
2186 blitter_disable_render_cond(ctx
);
2189 pipe
->bind_blend_state(pipe
, custom_blend
? custom_blend
2190 : ctx
->blend
[PIPE_MASK_RGBA
]);
2191 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2192 bind_fs_write_one_cbuf(ctx
);
2193 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2194 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2196 /* set a framebuffer state */
2197 fb_state
.width
= dstsurf
->width
;
2198 fb_state
.height
= dstsurf
->height
;
2199 fb_state
.nr_cbufs
= 1;
2200 fb_state
.cbufs
[0] = dstsurf
;
2202 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2203 pipe
->set_sample_mask(pipe
, ~0);
2205 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2206 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2207 blitter
->draw_rectangle(blitter
, 0, 0, dstsurf
->width
, dstsurf
->height
,
2210 blitter_restore_vertex_states(ctx
);
2211 blitter_restore_fragment_states(ctx
);
2212 blitter_restore_fb_state(ctx
);
2213 blitter_restore_render_cond(ctx
);
2214 blitter_unset_running_flag(ctx
);