1 /**************************************************************************
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 **************************************************************************/
29 * Blitter utility to facilitate acceleration of the clear, clear_render_target,
30 * clear_depth_stencil, resource_copy_region, and blit functions.
35 #include "pipe/p_context.h"
36 #include "pipe/p_defines.h"
37 #include "util/u_inlines.h"
38 #include "pipe/p_shader_tokens.h"
39 #include "pipe/p_state.h"
41 #include "util/u_format.h"
42 #include "util/u_memory.h"
43 #include "util/u_math.h"
44 #include "util/u_blitter.h"
45 #include "util/u_draw_quad.h"
46 #include "util/u_sampler.h"
47 #include "util/u_simple_shaders.h"
48 #include "util/u_surface.h"
49 #include "util/u_texture.h"
50 #include "util/u_upload_mgr.h"
52 #define INVALID_PTR ((void*)~0)
54 #define GET_CLEAR_BLEND_STATE_IDX(clear_buffers) \
55 ((clear_buffers) / PIPE_CLEAR_COLOR0)
57 #define NUM_RESOLVE_FRAG_SHADERS 5 /* MSAA 2x, 4x, 8x, 16x, 32x */
58 #define GET_MSAA_RESOLVE_FS_IDX(nr_samples) (util_logbase2(nr_samples)-1)
60 struct blitter_context_priv
62 struct blitter_context base
;
64 float vertices
[4][2][4]; /**< {pos, color} or {pos, texcoord} */
66 /* Templates for various state objects. */
68 /* Constant state objects. */
70 void *vs
; /**< Vertex shader which passes {pos, generic} to the output.*/
71 void *vs_pos_only
[4]; /**< Vertex shader which passes pos to the output.*/
72 void *vs_layered
; /**< Vertex shader which sets LAYER = INSTANCEID. */
74 /* Fragment shaders. */
76 void *fs_write_one_cbuf
;
77 void *fs_write_all_cbufs
;
79 /* FS which outputs a color from a texture where
80 * the 1st index indicates the texture type / destination type,
81 * the 2nd index is the PIPE_TEXTURE_* to be sampled,
82 * the 3rd index is 0 = use TEX, 1 = use TXF.
84 void *fs_texfetch_col
[5][PIPE_MAX_TEXTURE_TYPES
][2];
86 /* FS which outputs a depth from a texture, where
87 * the 1st index is the PIPE_TEXTURE_* to be sampled,
88 * the 2nd index is 0 = use TEX, 1 = use TXF.
90 void *fs_texfetch_depth
[PIPE_MAX_TEXTURE_TYPES
][2];
91 void *fs_texfetch_depthstencil
[PIPE_MAX_TEXTURE_TYPES
][2];
92 void *fs_texfetch_stencil
[PIPE_MAX_TEXTURE_TYPES
][2];
94 /* FS which outputs one sample from a multisample texture. */
95 void *fs_texfetch_col_msaa
[5][PIPE_MAX_TEXTURE_TYPES
];
96 void *fs_texfetch_depth_msaa
[PIPE_MAX_TEXTURE_TYPES
];
97 void *fs_texfetch_depthstencil_msaa
[PIPE_MAX_TEXTURE_TYPES
];
98 void *fs_texfetch_stencil_msaa
[PIPE_MAX_TEXTURE_TYPES
];
100 /* FS which outputs an average of all samples. */
101 void *fs_resolve
[PIPE_MAX_TEXTURE_TYPES
][NUM_RESOLVE_FRAG_SHADERS
][2];
104 void *blend
[PIPE_MASK_RGBA
+1][2]; /**< blend state with writemask */
105 void *blend_clear
[GET_CLEAR_BLEND_STATE_IDX(PIPE_CLEAR_COLOR
)+1];
107 /* Depth stencil alpha state. */
108 void *dsa_write_depth_stencil
;
109 void *dsa_write_depth_keep_stencil
;
110 void *dsa_keep_depth_stencil
;
111 void *dsa_keep_depth_write_stencil
;
113 /* Vertex elements states. */
115 void *velem_state_readbuf
[4]; /**< X, XY, XYZ, XYZW */
119 void *sampler_state_linear
;
120 void *sampler_state_rect
;
121 void *sampler_state_rect_linear
;
123 /* Rasterizer state. */
124 void *rs_state
, *rs_state_scissor
, *rs_discard_state
;
126 /* Viewport state. */
127 struct pipe_viewport_state viewport
;
129 /* Destination surface dimensions. */
133 boolean has_geometry_shader
;
134 boolean has_tessellation
;
136 boolean has_stream_out
;
137 boolean has_stencil_export
;
138 boolean has_texture_multisample
;
141 boolean cached_all_shaders
;
143 /* The Draw module overrides these functions.
144 * Always create the blitter before Draw. */
145 void (*bind_fs_state
)(struct pipe_context
*, void *);
146 void (*delete_fs_state
)(struct pipe_context
*, void *);
149 static struct pipe_surface
*
150 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
151 struct pipe_surface
*surf
);
153 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
)
155 struct blitter_context_priv
*ctx
;
156 struct pipe_blend_state blend
;
157 struct pipe_depth_stencil_alpha_state dsa
;
158 struct pipe_rasterizer_state rs_state
;
159 struct pipe_sampler_state sampler_state
;
160 struct pipe_vertex_element velem
[2];
163 ctx
= CALLOC_STRUCT(blitter_context_priv
);
167 ctx
->base
.pipe
= pipe
;
168 ctx
->base
.draw_rectangle
= util_blitter_draw_rectangle
;
169 ctx
->base
.get_next_surface_layer
= util_blitter_get_next_surface_layer
;
171 ctx
->bind_fs_state
= pipe
->bind_fs_state
;
172 ctx
->delete_fs_state
= pipe
->delete_fs_state
;
174 /* init state objects for them to be considered invalid */
175 ctx
->base
.saved_blend_state
= INVALID_PTR
;
176 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
177 ctx
->base
.saved_rs_state
= INVALID_PTR
;
178 ctx
->base
.saved_fs
= INVALID_PTR
;
179 ctx
->base
.saved_vs
= INVALID_PTR
;
180 ctx
->base
.saved_gs
= INVALID_PTR
;
181 ctx
->base
.saved_velem_state
= INVALID_PTR
;
182 ctx
->base
.saved_fb_state
.nr_cbufs
= ~0;
183 ctx
->base
.saved_num_sampler_views
= ~0;
184 ctx
->base
.saved_num_sampler_states
= ~0;
185 ctx
->base
.saved_num_so_targets
= ~0;
187 ctx
->has_geometry_shader
=
188 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
189 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
191 ctx
->has_tessellation
=
192 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_TESS_CTRL
,
193 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
195 ctx
->has_stream_out
=
196 pipe
->screen
->get_param(pipe
->screen
,
197 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0;
199 ctx
->has_stencil_export
=
200 pipe
->screen
->get_param(pipe
->screen
,
201 PIPE_CAP_SHADER_STENCIL_EXPORT
);
203 ctx
->has_texture_multisample
=
204 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TEXTURE_MULTISAMPLE
);
206 ctx
->has_tex_lz
= pipe
->screen
->get_param(pipe
->screen
,
207 PIPE_CAP_TGSI_TEX_TXF_LZ
);
208 ctx
->has_txf
= pipe
->screen
->get_param(pipe
->screen
,
209 PIPE_CAP_GLSL_FEATURE_LEVEL
) > 130;
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(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_layered(struct blitter_context_priv
*ctx
)
374 struct pipe_context
*pipe
= ctx
->base
.pipe
;
376 if (!ctx
->vs_layered
) {
377 ctx
->vs_layered
= util_make_layered_clear_vertex_shader(pipe
);
380 pipe
->bind_vs_state(pipe
, ctx
->vs_layered
);
383 static void bind_fs_empty(struct blitter_context_priv
*ctx
)
385 struct pipe_context
*pipe
= ctx
->base
.pipe
;
387 if (!ctx
->fs_empty
) {
388 assert(!ctx
->cached_all_shaders
);
389 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
392 ctx
->bind_fs_state(pipe
, ctx
->fs_empty
);
395 static void bind_fs_write_one_cbuf(struct blitter_context_priv
*ctx
)
397 struct pipe_context
*pipe
= ctx
->base
.pipe
;
399 if (!ctx
->fs_write_one_cbuf
) {
400 assert(!ctx
->cached_all_shaders
);
401 ctx
->fs_write_one_cbuf
=
402 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
403 TGSI_INTERPOLATE_CONSTANT
, FALSE
);
406 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
409 static void bind_fs_write_all_cbufs(struct blitter_context_priv
*ctx
)
411 struct pipe_context
*pipe
= ctx
->base
.pipe
;
413 if (!ctx
->fs_write_all_cbufs
) {
414 assert(!ctx
->cached_all_shaders
);
415 ctx
->fs_write_all_cbufs
=
416 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
417 TGSI_INTERPOLATE_CONSTANT
, TRUE
);
420 ctx
->bind_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
423 void util_blitter_destroy(struct blitter_context
*blitter
)
425 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
426 struct pipe_context
*pipe
= blitter
->pipe
;
429 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++)
430 for (j
= 0; j
< 2; j
++)
431 pipe
->delete_blend_state(pipe
, ctx
->blend
[i
][j
]);
433 for (i
= 0; i
< ARRAY_SIZE(ctx
->blend_clear
); i
++) {
434 if (ctx
->blend_clear
[i
])
435 pipe
->delete_blend_state(pipe
, ctx
->blend_clear
[i
]);
437 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
438 pipe
->delete_depth_stencil_alpha_state(pipe
,
439 ctx
->dsa_write_depth_keep_stencil
);
440 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
441 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
443 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
);
444 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state_scissor
);
445 if (ctx
->rs_discard_state
)
446 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_discard_state
);
448 pipe
->delete_vs_state(pipe
, ctx
->vs
);
449 for (i
= 0; i
< 4; i
++)
450 if (ctx
->vs_pos_only
[i
])
451 pipe
->delete_vs_state(pipe
, ctx
->vs_pos_only
[i
]);
453 pipe
->delete_vs_state(pipe
, ctx
->vs_layered
);
454 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state
);
455 for (i
= 0; i
< 4; i
++) {
456 if (ctx
->velem_state_readbuf
[i
]) {
457 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[i
]);
461 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
462 for (unsigned type
= 0; type
< ARRAY_SIZE(ctx
->fs_texfetch_col
); ++type
) {
463 for (unsigned inst
= 0; inst
< 2; inst
++) {
464 if (ctx
->fs_texfetch_col
[type
][i
][inst
])
465 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[type
][i
][inst
]);
467 if (ctx
->fs_texfetch_col_msaa
[type
][i
])
468 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col_msaa
[type
][i
]);
471 for (unsigned inst
= 0; inst
< 2; inst
++) {
472 if (ctx
->fs_texfetch_depth
[i
][inst
])
473 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
][inst
]);
474 if (ctx
->fs_texfetch_depthstencil
[i
][inst
])
475 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil
[i
][inst
]);
476 if (ctx
->fs_texfetch_stencil
[i
][inst
])
477 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil
[i
][inst
]);
480 if (ctx
->fs_texfetch_depth_msaa
[i
])
481 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth_msaa
[i
]);
482 if (ctx
->fs_texfetch_depthstencil_msaa
[i
])
483 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil_msaa
[i
]);
484 if (ctx
->fs_texfetch_stencil_msaa
[i
])
485 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil_msaa
[i
]);
487 for (j
= 0; j
< ARRAY_SIZE(ctx
->fs_resolve
[i
]); j
++)
488 for (f
= 0; f
< 2; f
++)
489 if (ctx
->fs_resolve
[i
][j
][f
])
490 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve
[i
][j
][f
]);
494 ctx
->delete_fs_state(pipe
, ctx
->fs_empty
);
495 if (ctx
->fs_write_one_cbuf
)
496 ctx
->delete_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
497 if (ctx
->fs_write_all_cbufs
)
498 ctx
->delete_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
500 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect_linear
);
501 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect
);
502 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_linear
);
503 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
);
507 void util_blitter_set_texture_multisample(struct blitter_context
*blitter
,
510 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
512 ctx
->has_texture_multisample
= supported
;
515 void util_blitter_set_running_flag(struct blitter_context
*blitter
)
517 if (blitter
->running
) {
518 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
521 blitter
->running
= TRUE
;
523 blitter
->pipe
->set_active_query_state(blitter
->pipe
, false);
526 void util_blitter_unset_running_flag(struct blitter_context
*blitter
)
528 if (!blitter
->running
) {
529 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
532 blitter
->running
= FALSE
;
534 blitter
->pipe
->set_active_query_state(blitter
->pipe
, true);
537 static void blitter_check_saved_vertex_states(struct blitter_context_priv
*ctx
)
539 assert(ctx
->base
.saved_velem_state
!= INVALID_PTR
);
540 assert(ctx
->base
.saved_vs
!= INVALID_PTR
);
541 assert(!ctx
->has_geometry_shader
|| ctx
->base
.saved_gs
!= INVALID_PTR
);
542 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tcs
!= INVALID_PTR
);
543 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tes
!= INVALID_PTR
);
544 assert(!ctx
->has_stream_out
|| ctx
->base
.saved_num_so_targets
!= ~0u);
545 assert(ctx
->base
.saved_rs_state
!= INVALID_PTR
);
548 void util_blitter_restore_vertex_states(struct blitter_context
*blitter
)
550 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
551 struct pipe_context
*pipe
= ctx
->base
.pipe
;
555 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1,
556 &ctx
->base
.saved_vertex_buffer
);
557 pipe_vertex_buffer_unreference(&ctx
->base
.saved_vertex_buffer
);
559 /* Vertex elements. */
560 pipe
->bind_vertex_elements_state(pipe
, ctx
->base
.saved_velem_state
);
561 ctx
->base
.saved_velem_state
= INVALID_PTR
;
564 pipe
->bind_vs_state(pipe
, ctx
->base
.saved_vs
);
565 ctx
->base
.saved_vs
= INVALID_PTR
;
567 /* Geometry shader. */
568 if (ctx
->has_geometry_shader
) {
569 pipe
->bind_gs_state(pipe
, ctx
->base
.saved_gs
);
570 ctx
->base
.saved_gs
= INVALID_PTR
;
573 if (ctx
->has_tessellation
) {
574 pipe
->bind_tcs_state(pipe
, ctx
->base
.saved_tcs
);
575 pipe
->bind_tes_state(pipe
, ctx
->base
.saved_tes
);
576 ctx
->base
.saved_tcs
= INVALID_PTR
;
577 ctx
->base
.saved_tes
= INVALID_PTR
;
580 /* Stream outputs. */
581 if (ctx
->has_stream_out
) {
582 unsigned offsets
[PIPE_MAX_SO_BUFFERS
];
583 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
584 offsets
[i
] = (unsigned)-1;
585 pipe
->set_stream_output_targets(pipe
,
586 ctx
->base
.saved_num_so_targets
,
587 ctx
->base
.saved_so_targets
, offsets
);
589 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
590 pipe_so_target_reference(&ctx
->base
.saved_so_targets
[i
], NULL
);
592 ctx
->base
.saved_num_so_targets
= ~0;
596 pipe
->bind_rasterizer_state(pipe
, ctx
->base
.saved_rs_state
);
597 ctx
->base
.saved_rs_state
= INVALID_PTR
;
600 static void blitter_check_saved_fragment_states(struct blitter_context_priv
*ctx
)
602 assert(ctx
->base
.saved_fs
!= INVALID_PTR
);
603 assert(ctx
->base
.saved_dsa_state
!= INVALID_PTR
);
604 assert(ctx
->base
.saved_blend_state
!= INVALID_PTR
);
607 void util_blitter_restore_fragment_states(struct blitter_context
*blitter
)
609 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
610 struct pipe_context
*pipe
= ctx
->base
.pipe
;
612 /* Fragment shader. */
613 ctx
->bind_fs_state(pipe
, ctx
->base
.saved_fs
);
614 ctx
->base
.saved_fs
= INVALID_PTR
;
616 /* Depth, stencil, alpha. */
617 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->base
.saved_dsa_state
);
618 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
621 pipe
->bind_blend_state(pipe
, ctx
->base
.saved_blend_state
);
622 ctx
->base
.saved_blend_state
= INVALID_PTR
;
625 if (ctx
->base
.is_sample_mask_saved
) {
626 pipe
->set_sample_mask(pipe
, ctx
->base
.saved_sample_mask
);
627 ctx
->base
.is_sample_mask_saved
= FALSE
;
630 /* Miscellaneous states. */
631 /* XXX check whether these are saved and whether they need to be restored
632 * (depending on the operation) */
633 pipe
->set_stencil_ref(pipe
, &ctx
->base
.saved_stencil_ref
);
634 pipe
->set_viewport_states(pipe
, 0, 1, &ctx
->base
.saved_viewport
);
637 static void blitter_check_saved_fb_state(struct blitter_context_priv
*ctx
)
639 assert(ctx
->base
.saved_fb_state
.nr_cbufs
!= ~0u);
642 static void blitter_disable_render_cond(struct blitter_context_priv
*ctx
)
644 struct pipe_context
*pipe
= ctx
->base
.pipe
;
646 if (ctx
->base
.saved_render_cond_query
) {
647 pipe
->render_condition(pipe
, NULL
, FALSE
, 0);
651 void util_blitter_restore_render_cond(struct blitter_context
*blitter
)
653 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
654 struct pipe_context
*pipe
= ctx
->base
.pipe
;
656 if (ctx
->base
.saved_render_cond_query
) {
657 pipe
->render_condition(pipe
, ctx
->base
.saved_render_cond_query
,
658 ctx
->base
.saved_render_cond_cond
,
659 ctx
->base
.saved_render_cond_mode
);
660 ctx
->base
.saved_render_cond_query
= NULL
;
664 void util_blitter_restore_fb_state(struct blitter_context
*blitter
)
666 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
667 struct pipe_context
*pipe
= ctx
->base
.pipe
;
669 pipe
->set_framebuffer_state(pipe
, &ctx
->base
.saved_fb_state
);
670 util_unreference_framebuffer_state(&ctx
->base
.saved_fb_state
);
673 static void blitter_check_saved_textures(struct blitter_context_priv
*ctx
)
675 assert(ctx
->base
.saved_num_sampler_states
!= ~0u);
676 assert(ctx
->base
.saved_num_sampler_views
!= ~0u);
679 void util_blitter_restore_textures(struct blitter_context
*blitter
)
681 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
682 struct pipe_context
*pipe
= ctx
->base
.pipe
;
685 /* Fragment sampler states. */
686 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0,
687 ctx
->base
.saved_num_sampler_states
,
688 ctx
->base
.saved_sampler_states
);
690 ctx
->base
.saved_num_sampler_states
= ~0;
692 /* Fragment sampler views. */
693 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
694 ctx
->base
.saved_num_sampler_views
,
695 ctx
->base
.saved_sampler_views
);
697 for (i
= 0; i
< ctx
->base
.saved_num_sampler_views
; i
++)
698 pipe_sampler_view_reference(&ctx
->base
.saved_sampler_views
[i
], NULL
);
700 ctx
->base
.saved_num_sampler_views
= ~0;
703 void util_blitter_restore_constant_buffer_state(struct blitter_context
*blitter
)
705 struct pipe_context
*pipe
= blitter
->pipe
;
707 pipe
->set_constant_buffer(pipe
, PIPE_SHADER_FRAGMENT
, blitter
->cb_slot
,
708 &blitter
->saved_fs_constant_buffer
);
709 pipe_resource_reference(&blitter
->saved_fs_constant_buffer
.buffer
, NULL
);
712 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
713 int x1
, int y1
, int x2
, int y2
,
718 /* set vertex positions */
719 ctx
->vertices
[0][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v0.x*/
720 ctx
->vertices
[0][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v0.y*/
722 ctx
->vertices
[1][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v1.x*/
723 ctx
->vertices
[1][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v1.y*/
725 ctx
->vertices
[2][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v2.x*/
726 ctx
->vertices
[2][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v2.y*/
728 ctx
->vertices
[3][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v3.x*/
729 ctx
->vertices
[3][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v3.y*/
731 for (i
= 0; i
< 4; i
++)
732 ctx
->vertices
[i
][0][2] = depth
; /*z*/
735 ctx
->viewport
.scale
[0] = 0.5f
* ctx
->dst_width
;
736 ctx
->viewport
.scale
[1] = 0.5f
* ctx
->dst_height
;
737 ctx
->viewport
.scale
[2] = 1.0f
;
738 ctx
->viewport
.translate
[0] = 0.5f
* ctx
->dst_width
;
739 ctx
->viewport
.translate
[1] = 0.5f
* ctx
->dst_height
;
740 ctx
->viewport
.translate
[2] = 0.0f
;
741 ctx
->base
.pipe
->set_viewport_states(ctx
->base
.pipe
, 0, 1, &ctx
->viewport
);
744 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
745 const union pipe_color_union
*color
)
750 for (i
= 0; i
< 4; i
++) {
751 uint32_t *uiverts
= (uint32_t *)ctx
->vertices
[i
][1];
752 uiverts
[0] = color
->ui
[0];
753 uiverts
[1] = color
->ui
[1];
754 uiverts
[2] = color
->ui
[2];
755 uiverts
[3] = color
->ui
[3];
758 for (i
= 0; i
< 4; i
++) {
759 ctx
->vertices
[i
][1][0] = 0;
760 ctx
->vertices
[i
][1][1] = 0;
761 ctx
->vertices
[i
][1][2] = 0;
762 ctx
->vertices
[i
][1][3] = 0;
767 static void get_texcoords(struct pipe_sampler_view
*src
,
768 unsigned src_width0
, unsigned src_height0
,
769 int x1
, int y1
, int x2
, int y2
, bool uses_txf
,
772 struct pipe_resource
*tex
= src
->texture
;
773 unsigned level
= src
->u
.tex
.first_level
;
774 boolean normalized
= !uses_txf
&&
775 tex
->target
!= PIPE_TEXTURE_RECT
&&
776 tex
->nr_samples
<= 1;
779 out
[0] = x1
/ (float)u_minify(src_width0
, level
);
780 out
[1] = y1
/ (float)u_minify(src_height0
, level
);
781 out
[2] = x2
/ (float)u_minify(src_width0
, level
);
782 out
[3] = y2
/ (float)u_minify(src_height0
, level
);
791 static void set_texcoords_in_vertices(const float coord
[4],
792 float *out
, unsigned stride
)
794 out
[0] = coord
[0]; /*t0.s*/
795 out
[1] = coord
[1]; /*t0.t*/
797 out
[0] = coord
[2]; /*t1.s*/
798 out
[1] = coord
[1]; /*t1.t*/
800 out
[0] = coord
[2]; /*t2.s*/
801 out
[1] = coord
[3]; /*t2.t*/
803 out
[0] = coord
[0]; /*t3.s*/
804 out
[1] = coord
[3]; /*t3.t*/
807 static void blitter_set_texcoords(struct blitter_context_priv
*ctx
,
808 struct pipe_sampler_view
*src
,
809 unsigned src_width0
, unsigned src_height0
,
810 float layer
, unsigned sample
,
811 int x1
, int y1
, int x2
, int y2
,
816 float face_coord
[4][2];
818 get_texcoords(src
, src_width0
, src_height0
, x1
, y1
, x2
, y2
, uses_txf
,
821 if (src
->texture
->target
== PIPE_TEXTURE_CUBE
||
822 src
->texture
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
823 set_texcoords_in_vertices(coord
, &face_coord
[0][0], 2);
824 util_map_texcoords2d_onto_cubemap((unsigned)layer
% 6,
825 /* pointer, stride in floats */
826 &face_coord
[0][0], 2,
827 &ctx
->vertices
[0][1][0], 8,
830 set_texcoords_in_vertices(coord
, &ctx
->vertices
[0][1][0], 8);
834 switch (src
->texture
->target
) {
835 case PIPE_TEXTURE_3D
:
840 r
/= u_minify(src
->texture
->depth0
, src
->u
.tex
.first_level
);
842 for (i
= 0; i
< 4; i
++)
843 ctx
->vertices
[i
][1][2] = r
; /*r*/
847 case PIPE_TEXTURE_1D_ARRAY
:
848 for (i
= 0; i
< 4; i
++)
849 ctx
->vertices
[i
][1][1] = (float) layer
; /*t*/
852 case PIPE_TEXTURE_2D_ARRAY
:
853 for (i
= 0; i
< 4; i
++) {
854 ctx
->vertices
[i
][1][2] = (float) layer
; /*r*/
855 ctx
->vertices
[i
][1][3] = (float) sample
; /*q*/
859 case PIPE_TEXTURE_CUBE_ARRAY
:
860 for (i
= 0; i
< 4; i
++)
861 ctx
->vertices
[i
][1][3] = (float) ((unsigned)layer
/ 6); /*w*/
864 case PIPE_TEXTURE_2D
:
865 for (i
= 0; i
< 4; i
++) {
866 ctx
->vertices
[i
][1][3] = (float) sample
; /*r*/
874 static void blitter_set_dst_dimensions(struct blitter_context_priv
*ctx
,
875 unsigned width
, unsigned height
)
877 ctx
->dst_width
= width
;
878 ctx
->dst_height
= height
;
881 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
882 enum pipe_format src_format
,
883 enum pipe_format dst_format
,
884 enum pipe_texture_target target
,
885 unsigned src_nr_samples
,
886 unsigned dst_nr_samples
,
890 struct pipe_context
*pipe
= ctx
->base
.pipe
;
891 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, src_nr_samples
);
892 enum tgsi_return_type stype
;
893 enum tgsi_return_type dtype
;
896 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
898 if (util_format_is_pure_uint(src_format
)) {
899 stype
= TGSI_RETURN_TYPE_UINT
;
900 if (util_format_is_pure_uint(dst_format
)) {
901 dtype
= TGSI_RETURN_TYPE_UINT
;
904 assert(util_format_is_pure_sint(dst_format
));
905 dtype
= TGSI_RETURN_TYPE_SINT
;
908 } else if (util_format_is_pure_sint(src_format
)) {
909 stype
= TGSI_RETURN_TYPE_SINT
;
910 if (util_format_is_pure_sint(dst_format
)) {
911 dtype
= TGSI_RETURN_TYPE_SINT
;
914 assert(util_format_is_pure_uint(dst_format
));
915 dtype
= TGSI_RETURN_TYPE_UINT
;
919 assert(!util_format_is_pure_uint(dst_format
) &&
920 !util_format_is_pure_sint(dst_format
));
921 dtype
= stype
= TGSI_RETURN_TYPE_FLOAT
;
925 if (src_nr_samples
> 1) {
928 /* OpenGL requires that integer textures just copy 1 sample instead
931 if (dst_nr_samples
<= 1 &&
932 stype
!= TGSI_RETURN_TYPE_UINT
&&
933 stype
!= TGSI_RETURN_TYPE_SINT
) {
934 /* The destination has one sample, so we'll do color resolve. */
935 unsigned index
= GET_MSAA_RESOLVE_FS_IDX(src_nr_samples
);
939 shader
= &ctx
->fs_resolve
[target
][index
][filter
];
942 assert(!ctx
->cached_all_shaders
);
943 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
944 *shader
= util_make_fs_msaa_resolve_bilinear(pipe
, tgsi_tex
,
949 *shader
= util_make_fs_msaa_resolve(pipe
, tgsi_tex
,
956 /* The destination has multiple samples, we'll do
957 * an MSAA->MSAA copy.
959 shader
= &ctx
->fs_texfetch_col_msaa
[type
][target
];
961 /* Create the fragment shader on-demand. */
963 assert(!ctx
->cached_all_shaders
);
964 *shader
= util_make_fs_blit_msaa_color(pipe
, tgsi_tex
, stype
, dtype
);
973 shader
= &ctx
->fs_texfetch_col
[type
][target
][1];
975 shader
= &ctx
->fs_texfetch_col
[type
][target
][0];
977 /* Create the fragment shader on-demand. */
979 assert(!ctx
->cached_all_shaders
);
980 *shader
= util_make_fragment_tex_shader(pipe
, tgsi_tex
,
981 TGSI_INTERPOLATE_LINEAR
,
983 ctx
->has_tex_lz
, use_txf
);
991 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
992 enum pipe_texture_target target
,
996 struct pipe_context
*pipe
= ctx
->base
.pipe
;
998 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1000 if (nr_samples
> 1) {
1001 void **shader
= &ctx
->fs_texfetch_depth_msaa
[target
];
1003 /* Create the fragment shader on-demand. */
1006 assert(!ctx
->cached_all_shaders
);
1007 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1008 *shader
= util_make_fs_blit_msaa_depth(pipe
, tgsi_tex
);
1016 shader
= &ctx
->fs_texfetch_depth
[target
][1];
1018 shader
= &ctx
->fs_texfetch_depth
[target
][0];
1020 /* Create the fragment shader on-demand. */
1023 assert(!ctx
->cached_all_shaders
);
1024 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1026 util_make_fragment_tex_shader_writedepth(pipe
, tgsi_tex
,
1027 TGSI_INTERPOLATE_LINEAR
,
1028 ctx
->has_tex_lz
, use_txf
);
1036 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv
*ctx
,
1037 enum pipe_texture_target target
,
1038 unsigned nr_samples
,
1041 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1043 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1045 if (nr_samples
> 1) {
1046 void **shader
= &ctx
->fs_texfetch_depthstencil_msaa
[target
];
1048 /* Create the fragment shader on-demand. */
1051 assert(!ctx
->cached_all_shaders
);
1052 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1053 *shader
= util_make_fs_blit_msaa_depthstencil(pipe
, tgsi_tex
);
1061 shader
= &ctx
->fs_texfetch_depthstencil
[target
][1];
1063 shader
= &ctx
->fs_texfetch_depthstencil
[target
][0];
1065 /* Create the fragment shader on-demand. */
1068 assert(!ctx
->cached_all_shaders
);
1069 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1071 util_make_fragment_tex_shader_writedepthstencil(pipe
, tgsi_tex
,
1072 TGSI_INTERPOLATE_LINEAR
,
1082 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv
*ctx
,
1083 enum pipe_texture_target target
,
1084 unsigned nr_samples
,
1087 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1089 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1091 if (nr_samples
> 1) {
1092 void **shader
= &ctx
->fs_texfetch_stencil_msaa
[target
];
1094 /* Create the fragment shader on-demand. */
1097 assert(!ctx
->cached_all_shaders
);
1098 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1099 *shader
= util_make_fs_blit_msaa_stencil(pipe
, tgsi_tex
);
1107 shader
= &ctx
->fs_texfetch_stencil
[target
][1];
1109 shader
= &ctx
->fs_texfetch_stencil
[target
][0];
1111 /* Create the fragment shader on-demand. */
1114 assert(!ctx
->cached_all_shaders
);
1115 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1117 util_make_fragment_tex_shader_writestencil(pipe
, tgsi_tex
,
1118 TGSI_INTERPOLATE_LINEAR
,
1119 ctx
->has_tex_lz
, use_txf
);
1128 * Generate and save all fragment shaders that we will ever need for
1129 * blitting. Drivers which use the 'draw' fallbacks will typically use
1130 * this to make sure we generate/use shaders that don't go through the
1131 * draw module's wrapper functions.
1133 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
)
1135 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1136 struct pipe_context
*pipe
= blitter
->pipe
;
1137 struct pipe_screen
*screen
= pipe
->screen
;
1138 unsigned samples
, j
, f
, target
, max_samples
, use_txf
;
1139 boolean has_arraytex
, has_cubearraytex
;
1141 max_samples
= ctx
->has_texture_multisample
? 2 : 1;
1142 has_arraytex
= screen
->get_param(screen
,
1143 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
) != 0;
1144 has_cubearraytex
= screen
->get_param(screen
,
1145 PIPE_CAP_CUBE_MAP_ARRAY
) != 0;
1147 /* It only matters if i <= 1 or > 1. */
1148 for (samples
= 1; samples
<= max_samples
; samples
++) {
1149 for (target
= PIPE_TEXTURE_1D
; target
< PIPE_MAX_TEXTURE_TYPES
; target
++) {
1150 for (use_txf
= 0; use_txf
<= ctx
->has_txf
; use_txf
++) {
1151 if (!has_arraytex
&&
1152 (target
== PIPE_TEXTURE_1D_ARRAY
||
1153 target
== PIPE_TEXTURE_2D_ARRAY
)) {
1156 if (!has_cubearraytex
&&
1157 (target
== PIPE_TEXTURE_CUBE_ARRAY
))
1161 (target
!= PIPE_TEXTURE_2D
&&
1162 target
!= PIPE_TEXTURE_2D_ARRAY
))
1165 if (samples
> 1 && use_txf
)
1166 continue; /* TXF is the only option, use_txf has no effect */
1168 /* If samples == 1, the shaders read one texel. If samples >= 1,
1169 * they read one sample.
1171 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
,
1172 PIPE_FORMAT_R32_FLOAT
, target
,
1173 samples
, samples
, 0, use_txf
);
1174 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1175 PIPE_FORMAT_R32_UINT
, target
,
1176 samples
, samples
, 0, use_txf
);
1177 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1178 PIPE_FORMAT_R32_SINT
, target
,
1179 samples
, samples
, 0, use_txf
);
1180 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1181 PIPE_FORMAT_R32_SINT
, target
,
1182 samples
, samples
, 0, use_txf
);
1183 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1184 PIPE_FORMAT_R32_UINT
, target
,
1185 samples
, samples
, 0, use_txf
);
1186 blitter_get_fs_texfetch_depth(ctx
, target
, samples
, use_txf
);
1187 if (ctx
->has_stencil_export
) {
1188 blitter_get_fs_texfetch_depthstencil(ctx
, target
, samples
, use_txf
);
1189 blitter_get_fs_texfetch_stencil(ctx
, target
, samples
, use_txf
);
1195 /* MSAA resolve shaders. */
1196 for (j
= 2; j
< 32; j
++) {
1197 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R32_FLOAT
,
1199 PIPE_BIND_SAMPLER_VIEW
)) {
1203 for (f
= 0; f
< 2; f
++) {
1204 if (f
!= PIPE_TEX_FILTER_NEAREST
&& use_txf
)
1207 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
,
1208 PIPE_FORMAT_R32_FLOAT
, target
,
1210 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
,
1211 PIPE_FORMAT_R32_UINT
, target
,
1213 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
,
1214 PIPE_FORMAT_R32_SINT
, target
,
1222 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
1224 ctx
->fs_write_one_cbuf
=
1225 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1226 TGSI_INTERPOLATE_CONSTANT
, FALSE
);
1228 ctx
->fs_write_all_cbufs
=
1229 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1230 TGSI_INTERPOLATE_CONSTANT
, TRUE
);
1232 ctx
->cached_all_shaders
= TRUE
;
1235 static void blitter_set_common_draw_rect_state(struct blitter_context_priv
*ctx
,
1239 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1241 pipe
->bind_rasterizer_state(pipe
, scissor
? ctx
->rs_state_scissor
1244 bind_vs_layered(ctx
);
1246 bind_vs_passthrough(ctx
);
1248 if (ctx
->has_geometry_shader
)
1249 pipe
->bind_gs_state(pipe
, NULL
);
1250 if (ctx
->has_tessellation
) {
1251 pipe
->bind_tcs_state(pipe
, NULL
);
1252 pipe
->bind_tes_state(pipe
, NULL
);
1254 if (ctx
->has_stream_out
)
1255 pipe
->set_stream_output_targets(pipe
, 0, NULL
, NULL
);
1258 static void blitter_draw(struct blitter_context_priv
*ctx
,
1259 int x1
, int y1
, int x2
, int y2
, float depth
,
1260 unsigned num_instances
)
1262 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1263 struct pipe_vertex_buffer vb
= {0};
1265 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
1267 vb
.stride
= 8 * sizeof(float);
1269 u_upload_data(pipe
->stream_uploader
, 0, sizeof(ctx
->vertices
), 4, ctx
->vertices
,
1270 &vb
.buffer_offset
, &vb
.buffer
.resource
);
1271 if (!vb
.buffer
.resource
)
1273 u_upload_unmap(pipe
->stream_uploader
);
1275 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1276 util_draw_arrays_instanced(pipe
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
1278 pipe_resource_reference(&vb
.buffer
.resource
, NULL
);
1281 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
1282 int x1
, int y1
, int x2
, int y2
, float depth
,
1283 enum blitter_attrib_type type
,
1284 const union pipe_color_union
*attrib
)
1286 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1289 case UTIL_BLITTER_ATTRIB_COLOR
:
1290 blitter_set_clear_color(ctx
, attrib
);
1293 case UTIL_BLITTER_ATTRIB_TEXCOORD
:
1294 set_texcoords_in_vertices(attrib
->f
, &ctx
->vertices
[0][1][0], 8);
1300 blitter_draw(ctx
, x1
, y1
, x2
, y2
, depth
, 1);
1303 static void *get_clear_blend_state(struct blitter_context_priv
*ctx
,
1304 unsigned clear_buffers
)
1306 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1309 clear_buffers
&= PIPE_CLEAR_COLOR
;
1311 /* Return an existing blend state. */
1313 return ctx
->blend
[0][0];
1315 index
= GET_CLEAR_BLEND_STATE_IDX(clear_buffers
);
1317 if (ctx
->blend_clear
[index
])
1318 return ctx
->blend_clear
[index
];
1320 /* Create a new one. */
1322 struct pipe_blend_state blend
= {0};
1325 blend
.independent_blend_enable
= 1;
1327 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1328 if (clear_buffers
& (PIPE_CLEAR_COLOR0
<< i
)) {
1329 blend
.rt
[i
].colormask
= PIPE_MASK_RGBA
;
1333 ctx
->blend_clear
[index
] = pipe
->create_blend_state(pipe
, &blend
);
1335 return ctx
->blend_clear
[index
];
1338 void util_blitter_common_clear_setup(struct blitter_context
*blitter
,
1339 unsigned width
, unsigned height
,
1340 unsigned clear_buffers
,
1341 void *custom_blend
, void *custom_dsa
)
1343 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1344 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1346 util_blitter_set_running_flag(blitter
);
1347 blitter_check_saved_vertex_states(ctx
);
1348 blitter_check_saved_fragment_states(ctx
);
1349 blitter_disable_render_cond(ctx
);
1353 pipe
->bind_blend_state(pipe
, custom_blend
);
1355 pipe
->bind_blend_state(pipe
, get_clear_blend_state(ctx
, clear_buffers
));
1359 pipe
->bind_depth_stencil_alpha_state(pipe
, custom_dsa
);
1360 } else if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1361 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1362 } else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
1363 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1364 } else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
1365 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1367 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1370 pipe
->set_sample_mask(pipe
, ~0);
1371 blitter_set_dst_dimensions(ctx
, width
, height
);
1374 static void util_blitter_clear_custom(struct blitter_context
*blitter
,
1375 unsigned width
, unsigned height
,
1376 unsigned num_layers
,
1377 unsigned clear_buffers
,
1378 const union pipe_color_union
*color
,
1379 double depth
, unsigned stencil
,
1380 void *custom_blend
, void *custom_dsa
)
1382 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1383 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1384 struct pipe_stencil_ref sr
= { { 0 } };
1386 assert(ctx
->has_layered
|| num_layers
<= 1);
1388 util_blitter_common_clear_setup(blitter
, width
, height
, clear_buffers
,
1389 custom_blend
, custom_dsa
);
1391 sr
.ref_value
[0] = stencil
& 0xff;
1392 pipe
->set_stencil_ref(pipe
, &sr
);
1394 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1395 bind_fs_write_all_cbufs(ctx
);
1397 if (num_layers
> 1 && ctx
->has_layered
) {
1398 blitter_set_common_draw_rect_state(ctx
, FALSE
, TRUE
);
1399 blitter_set_clear_color(ctx
, color
);
1400 blitter_draw(ctx
, 0, 0, width
, height
, depth
, num_layers
);
1403 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1404 blitter
->draw_rectangle(blitter
, 0, 0, width
, height
, (float) depth
,
1405 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1408 util_blitter_restore_vertex_states(blitter
);
1409 util_blitter_restore_fragment_states(blitter
);
1410 util_blitter_restore_render_cond(blitter
);
1411 util_blitter_unset_running_flag(blitter
);
1414 void util_blitter_clear(struct blitter_context
*blitter
,
1415 unsigned width
, unsigned height
, unsigned num_layers
,
1416 unsigned clear_buffers
,
1417 const union pipe_color_union
*color
,
1418 double depth
, unsigned stencil
)
1420 util_blitter_clear_custom(blitter
, width
, height
, num_layers
,
1421 clear_buffers
, color
, depth
, stencil
,
1425 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1426 unsigned width
, unsigned height
,
1427 double depth
, void *custom_dsa
)
1429 static const union pipe_color_union color
;
1430 util_blitter_clear_custom(blitter
, width
, height
, 0, 0, &color
, depth
, 0,
1434 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1435 struct pipe_resource
*dst
,
1439 memset(dst_templ
, 0, sizeof(*dst_templ
));
1440 dst_templ
->format
= util_format_linear(dst
->format
);
1441 dst_templ
->u
.tex
.level
= dstlevel
;
1442 dst_templ
->u
.tex
.first_layer
= dstz
;
1443 dst_templ
->u
.tex
.last_layer
= dstz
;
1446 static struct pipe_surface
*
1447 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
1448 struct pipe_surface
*surf
)
1450 struct pipe_surface dst_templ
;
1452 memset(&dst_templ
, 0, sizeof(dst_templ
));
1453 dst_templ
.format
= surf
->format
;
1454 dst_templ
.u
.tex
.level
= surf
->u
.tex
.level
;
1455 dst_templ
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
+ 1;
1456 dst_templ
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
+ 1;
1458 return pipe
->create_surface(pipe
, surf
->texture
, &dst_templ
);
1461 void util_blitter_default_src_texture(struct pipe_sampler_view
*src_templ
,
1462 struct pipe_resource
*src
,
1465 memset(src_templ
, 0, sizeof(*src_templ
));
1466 src_templ
->target
= src
->target
;
1467 src_templ
->format
= util_format_linear(src
->format
);
1468 src_templ
->u
.tex
.first_level
= srclevel
;
1469 src_templ
->u
.tex
.last_level
= srclevel
;
1470 src_templ
->u
.tex
.first_layer
= 0;
1471 src_templ
->u
.tex
.last_layer
=
1472 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1473 : src
->array_size
- 1;
1474 src_templ
->swizzle_r
= PIPE_SWIZZLE_X
;
1475 src_templ
->swizzle_g
= PIPE_SWIZZLE_Y
;
1476 src_templ
->swizzle_b
= PIPE_SWIZZLE_Z
;
1477 src_templ
->swizzle_a
= PIPE_SWIZZLE_W
;
1480 static boolean
is_blit_generic_supported(struct blitter_context
*blitter
,
1481 const struct pipe_resource
*dst
,
1482 enum pipe_format dst_format
,
1483 const struct pipe_resource
*src
,
1484 enum pipe_format src_format
,
1487 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1488 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1492 const struct util_format_description
*desc
=
1493 util_format_description(dst_format
);
1494 boolean dst_has_stencil
= util_format_has_stencil(desc
);
1496 /* Stencil export must be supported for stencil copy. */
1497 if ((mask
& PIPE_MASK_S
) && dst_has_stencil
&&
1498 !ctx
->has_stencil_export
) {
1502 if (dst_has_stencil
|| util_format_has_depth(desc
))
1503 bind
= PIPE_BIND_DEPTH_STENCIL
;
1505 bind
= PIPE_BIND_RENDER_TARGET
;
1507 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1508 dst
->nr_samples
, bind
)) {
1514 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1518 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1519 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
)) {
1523 /* Check stencil sampler support for stencil copy. */
1524 if (mask
& PIPE_MASK_S
) {
1525 if (util_format_has_stencil(util_format_description(src_format
))) {
1526 enum pipe_format stencil_format
=
1527 util_format_stencil_only(src_format
);
1528 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1530 if (stencil_format
!= src_format
&&
1531 !screen
->is_format_supported(screen
, stencil_format
,
1532 src
->target
, src
->nr_samples
,
1533 PIPE_BIND_SAMPLER_VIEW
)) {
1543 boolean
util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1544 const struct pipe_resource
*dst
,
1545 const struct pipe_resource
*src
)
1547 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1548 src
, src
->format
, PIPE_MASK_RGBAZS
);
1551 boolean
util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1552 const struct pipe_blit_info
*info
)
1554 return is_blit_generic_supported(blitter
,
1555 info
->dst
.resource
, info
->dst
.format
,
1556 info
->src
.resource
, info
->src
.format
,
1560 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1561 struct pipe_resource
*dst
,
1563 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1564 struct pipe_resource
*src
,
1566 const struct pipe_box
*srcbox
)
1568 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1569 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1570 struct pipe_surface
*dst_view
, dst_templ
;
1571 struct pipe_sampler_view src_templ
, *src_view
;
1572 struct pipe_box dstbox
;
1575 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1577 u_box_3d(dstx
, dsty
, dstz
, abs(srcbox
->width
), abs(srcbox
->height
),
1578 abs(srcbox
->depth
), &dstbox
);
1580 /* Initialize the surface. */
1581 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1582 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1584 /* Initialize the sampler view. */
1585 util_blitter_default_src_texture(&src_templ
, src
, src_level
);
1586 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1589 util_blitter_blit_generic(blitter
, dst_view
, &dstbox
,
1590 src_view
, srcbox
, src
->width0
, src
->height0
,
1591 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
,
1594 pipe_surface_reference(&dst_view
, NULL
);
1595 pipe_sampler_view_reference(&src_view
, NULL
);
1598 static void do_blits(struct blitter_context_priv
*ctx
,
1599 struct pipe_surface
*dst
,
1600 const struct pipe_box
*dstbox
,
1601 struct pipe_sampler_view
*src
,
1602 unsigned src_width0
,
1603 unsigned src_height0
,
1604 const struct pipe_box
*srcbox
,
1608 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1609 unsigned src_samples
= src
->texture
->nr_samples
;
1610 unsigned dst_samples
= dst
->texture
->nr_samples
;
1611 enum pipe_texture_target src_target
= src
->texture
->target
;
1612 struct pipe_framebuffer_state fb_state
= {0};
1614 /* Initialize framebuffer state. */
1615 fb_state
.width
= dst
->width
;
1616 fb_state
.height
= dst
->height
;
1617 fb_state
.nr_cbufs
= is_zsbuf
? 0 : 1;
1619 blitter_set_dst_dimensions(ctx
, fb_state
.width
, fb_state
.height
);
1621 if ((src_target
== PIPE_TEXTURE_1D
||
1622 src_target
== PIPE_TEXTURE_2D
||
1623 src_target
== PIPE_TEXTURE_RECT
) &&
1625 /* Draw the quad with the draw_rectangle callback. */
1627 /* Set texture coordinates. - use a pipe color union
1628 * for interface purposes.
1629 * XXX pipe_color_union is a wrong name since we use that to set
1630 * texture coordinates too.
1632 union pipe_color_union coord
;
1633 get_texcoords(src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1634 srcbox
->x
+srcbox
->width
, srcbox
->y
+srcbox
->height
,
1637 /* Set framebuffer state. */
1639 fb_state
.zsbuf
= dst
;
1641 fb_state
.cbufs
[0] = dst
;
1643 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1646 pipe
->set_sample_mask(pipe
, ~0);
1647 ctx
->base
.draw_rectangle(&ctx
->base
, dstbox
->x
, dstbox
->y
,
1648 dstbox
->x
+ dstbox
->width
,
1649 dstbox
->y
+ dstbox
->height
, 0,
1650 UTIL_BLITTER_ATTRIB_TEXCOORD
, &coord
);
1652 /* Draw the quad with the generic codepath. */
1654 for (dst_z
= 0; dst_z
< dstbox
->depth
; dst_z
++) {
1655 struct pipe_surface
*old
;
1656 float dst2src_scale
= srcbox
->depth
/ (float)dstbox
->depth
;
1658 /* Scale Z properly if the blit is scaled.
1660 * When downscaling, we want the coordinates centered, so that
1661 * mipmapping works for 3D textures. For example, when generating
1662 * a 4x4x4 level, this wouldn't average the pixels:
1664 * src Z: 0 1 2 3 4 5 6 7
1667 * Because the pixels are not centered below the pixels of the higher
1668 * level. Therefore, we want this:
1669 * src Z: 0 1 2 3 4 5 6 7
1672 * dst_offset defines the offset needed for centering the pixels and
1673 * it works with any scaling (not just 2x).
1675 float dst_offset
= ((srcbox
->depth
- 1) -
1676 (dstbox
->depth
- 1) * dst2src_scale
) * 0.5;
1677 float src_z
= (dst_z
+ dst_offset
) * dst2src_scale
;
1679 /* Set framebuffer state. */
1681 fb_state
.zsbuf
= dst
;
1683 fb_state
.cbufs
[0] = dst
;
1685 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1687 /* See if we need to blit a multisample or singlesample buffer. */
1688 if (src_samples
== dst_samples
&& dst_samples
> 1) {
1690 unsigned i
, max_sample
= dst_samples
- 1;
1692 for (i
= 0; i
<= max_sample
; i
++) {
1693 pipe
->set_sample_mask(pipe
, 1 << i
);
1694 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1696 i
, srcbox
->x
, srcbox
->y
,
1697 srcbox
->x
+ srcbox
->width
,
1698 srcbox
->y
+ srcbox
->height
, uses_txf
);
1699 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1700 dstbox
->x
+ dstbox
->width
,
1701 dstbox
->y
+ dstbox
->height
, 0, 1);
1704 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1705 pipe
->set_sample_mask(pipe
, ~0);
1706 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1707 srcbox
->z
+ src_z
, 0,
1708 srcbox
->x
, srcbox
->y
,
1709 srcbox
->x
+ srcbox
->width
,
1710 srcbox
->y
+ srcbox
->height
, uses_txf
);
1711 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1712 dstbox
->x
+ dstbox
->width
,
1713 dstbox
->y
+ dstbox
->height
, 0, 1);
1716 /* Get the next surface or (if this is the last iteration)
1717 * just unreference the last one. */
1719 if (dst_z
< dstbox
->depth
-1) {
1720 dst
= ctx
->base
.get_next_surface_layer(ctx
->base
.pipe
, dst
);
1723 pipe_surface_reference(&old
, NULL
);
1729 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1730 struct pipe_surface
*dst
,
1731 const struct pipe_box
*dstbox
,
1732 struct pipe_sampler_view
*src
,
1733 const struct pipe_box
*srcbox
,
1734 unsigned src_width0
, unsigned src_height0
,
1735 unsigned mask
, unsigned filter
,
1736 const struct pipe_scissor_state
*scissor
,
1737 boolean alpha_blend
)
1739 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1740 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1741 enum pipe_texture_target src_target
= src
->texture
->target
;
1742 unsigned src_samples
= src
->texture
->nr_samples
;
1743 unsigned dst_samples
= dst
->texture
->nr_samples
;
1744 boolean has_depth
, has_stencil
, has_color
;
1745 boolean blit_stencil
, blit_depth
, blit_color
;
1746 void *sampler_state
;
1747 const struct util_format_description
*src_desc
=
1748 util_format_description(src
->format
);
1749 const struct util_format_description
*dst_desc
=
1750 util_format_description(dst
->format
);
1752 has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
&&
1753 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1754 has_depth
= util_format_has_depth(src_desc
) &&
1755 util_format_has_depth(dst_desc
);
1756 has_stencil
= util_format_has_stencil(src_desc
) &&
1757 util_format_has_stencil(dst_desc
);
1759 blit_color
= has_color
&& (mask
& PIPE_MASK_RGBA
);
1760 blit_depth
= has_depth
&& (mask
& PIPE_MASK_Z
);
1761 blit_stencil
= has_stencil
&& (mask
& PIPE_MASK_S
) &&
1762 ctx
->has_stencil_export
;
1764 if (!blit_stencil
&& !blit_depth
&& !blit_color
) {
1769 (dstbox
->width
== abs(srcbox
->width
) &&
1770 dstbox
->height
== abs(srcbox
->height
))) {
1771 filter
= PIPE_TEX_FILTER_NEAREST
;
1774 bool use_txf
= false;
1777 filter
== PIPE_TEX_FILTER_NEAREST
&&
1778 src
->target
!= PIPE_TEXTURE_CUBE
&&
1779 src
->target
!= PIPE_TEXTURE_CUBE_ARRAY
) {
1780 int src_width
= u_minify(src_width0
, src
->u
.tex
.first_level
);
1781 int src_height
= u_minify(src_height0
, src
->u
.tex
.first_level
);
1782 int src_depth
= src
->u
.tex
.last_layer
+ 1;
1783 struct pipe_box box
= *srcbox
;
1785 /* Eliminate negative width/height/depth. */
1786 if (box
.width
< 0) {
1790 if (box
.height
< 0) {
1791 box
.y
+= box
.height
;
1794 if (box
.depth
< 0) {
1799 /* See if srcbox is in bounds. TXF doesn't clamp the coordinates. */
1801 box
.x
>= 0 && box
.x
< src_width
&&
1802 box
.y
>= 0 && box
.y
< src_height
&&
1803 box
.z
>= 0 && box
.z
< src_depth
&&
1804 box
.x
+ box
.width
> 0 && box
.x
+ box
.width
<= src_width
&&
1805 box
.y
+ box
.height
> 0 && box
.y
+ box
.height
<= src_height
&&
1806 box
.z
+ box
.depth
> 0 && box
.z
+ box
.depth
<= src_depth
;
1809 /* Check whether the states are properly saved. */
1810 util_blitter_set_running_flag(blitter
);
1811 blitter_check_saved_vertex_states(ctx
);
1812 blitter_check_saved_fragment_states(ctx
);
1813 blitter_check_saved_textures(ctx
);
1814 blitter_check_saved_fb_state(ctx
);
1815 blitter_disable_render_cond(ctx
);
1817 if (blit_depth
|| blit_stencil
) {
1818 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
1820 if (blit_depth
&& blit_stencil
) {
1821 pipe
->bind_depth_stencil_alpha_state(pipe
,
1822 ctx
->dsa_write_depth_stencil
);
1823 ctx
->bind_fs_state(pipe
,
1824 blitter_get_fs_texfetch_depthstencil(ctx
, src_target
,
1825 src_samples
, use_txf
));
1826 } else if (blit_depth
) {
1827 pipe
->bind_depth_stencil_alpha_state(pipe
,
1828 ctx
->dsa_write_depth_keep_stencil
);
1829 ctx
->bind_fs_state(pipe
,
1830 blitter_get_fs_texfetch_depth(ctx
, src_target
,
1831 src_samples
, use_txf
));
1832 } else { /* is_stencil */
1833 pipe
->bind_depth_stencil_alpha_state(pipe
,
1834 ctx
->dsa_keep_depth_write_stencil
);
1835 ctx
->bind_fs_state(pipe
,
1836 blitter_get_fs_texfetch_stencil(ctx
, src_target
,
1837 src_samples
, use_txf
));
1841 unsigned colormask
= mask
& PIPE_MASK_RGBA
;
1843 pipe
->bind_blend_state(pipe
, ctx
->blend
[colormask
][alpha_blend
]);
1844 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1845 ctx
->bind_fs_state(pipe
,
1846 blitter_get_fs_texfetch_col(ctx
, src
->format
, dst
->format
, src_target
,
1847 src_samples
, dst_samples
, filter
,
1851 /* Set the linear filter only for scaled color non-MSAA blits. */
1852 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
1853 if (src_target
== PIPE_TEXTURE_RECT
) {
1854 sampler_state
= ctx
->sampler_state_rect_linear
;
1856 sampler_state
= ctx
->sampler_state_linear
;
1859 if (src_target
== PIPE_TEXTURE_RECT
) {
1860 sampler_state
= ctx
->sampler_state_rect
;
1862 sampler_state
= ctx
->sampler_state
;
1867 if (blit_depth
&& blit_stencil
) {
1868 /* Setup two samplers, one for depth and the other one for stencil. */
1869 struct pipe_sampler_view templ
;
1870 struct pipe_sampler_view
*views
[2];
1871 void *samplers
[2] = {sampler_state
, sampler_state
};
1874 templ
.format
= util_format_stencil_only(templ
.format
);
1875 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1878 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1880 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, views
);
1881 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, samplers
);
1883 pipe_sampler_view_reference(&views
[1], NULL
);
1884 } else if (blit_stencil
) {
1885 /* Set a stencil-only sampler view for it not to sample depth instead. */
1886 struct pipe_sampler_view templ
;
1887 struct pipe_sampler_view
*view
;
1890 templ
.format
= util_format_stencil_only(templ
.format
);
1891 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1893 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1895 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &view
);
1896 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1897 0, 1, &sampler_state
);
1899 pipe_sampler_view_reference(&view
, NULL
);
1901 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src
);
1902 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1903 0, 1, &sampler_state
);
1906 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1908 pipe
->set_scissor_states(pipe
, 0, 1, scissor
);
1911 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
, FALSE
);
1913 do_blits(ctx
, dst
, dstbox
, src
, src_width0
, src_height0
,
1914 srcbox
, blit_depth
|| blit_stencil
, use_txf
);
1916 util_blitter_restore_vertex_states(blitter
);
1917 util_blitter_restore_fragment_states(blitter
);
1918 util_blitter_restore_textures(blitter
);
1919 util_blitter_restore_fb_state(blitter
);
1921 pipe
->set_scissor_states(pipe
, 0, 1, &ctx
->base
.saved_scissor
);
1923 util_blitter_restore_render_cond(blitter
);
1924 util_blitter_unset_running_flag(blitter
);
1928 util_blitter_blit(struct blitter_context
*blitter
,
1929 const struct pipe_blit_info
*info
)
1931 struct pipe_resource
*dst
= info
->dst
.resource
;
1932 struct pipe_resource
*src
= info
->src
.resource
;
1933 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1934 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1935 struct pipe_surface
*dst_view
, dst_templ
;
1936 struct pipe_sampler_view src_templ
, *src_view
;
1938 /* Initialize the surface. */
1939 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
1941 dst_templ
.format
= info
->dst
.format
;
1942 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1944 /* Initialize the sampler view. */
1945 util_blitter_default_src_texture(&src_templ
, src
, info
->src
.level
);
1946 src_templ
.format
= info
->src
.format
;
1947 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1950 util_blitter_blit_generic(blitter
, dst_view
, &info
->dst
.box
,
1951 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
1952 info
->mask
, info
->filter
,
1953 info
->scissor_enable
? &info
->scissor
: NULL
,
1956 pipe_surface_reference(&dst_view
, NULL
);
1957 pipe_sampler_view_reference(&src_view
, NULL
);
1960 void util_blitter_generate_mipmap(struct blitter_context
*blitter
,
1961 struct pipe_resource
*tex
,
1962 enum pipe_format format
,
1963 unsigned base_level
, unsigned last_level
,
1964 unsigned first_layer
, unsigned last_layer
)
1966 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1967 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1968 struct pipe_surface dst_templ
, *dst_view
;
1969 struct pipe_sampler_view src_templ
, *src_view
;
1971 void *sampler_state
;
1972 const struct util_format_description
*desc
=
1973 util_format_description(format
);
1976 assert(tex
->nr_samples
<= 1);
1977 assert(!util_format_has_stencil(desc
));
1979 is_depth
= desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
;
1981 /* Check whether the states are properly saved. */
1982 util_blitter_set_running_flag(blitter
);
1983 blitter_check_saved_vertex_states(ctx
);
1984 blitter_check_saved_fragment_states(ctx
);
1985 blitter_check_saved_textures(ctx
);
1986 blitter_check_saved_fb_state(ctx
);
1987 blitter_disable_render_cond(ctx
);
1991 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
1992 pipe
->bind_depth_stencil_alpha_state(pipe
,
1993 ctx
->dsa_write_depth_keep_stencil
);
1994 ctx
->bind_fs_state(pipe
,
1995 blitter_get_fs_texfetch_depth(ctx
, tex
->target
, 1,
1998 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
1999 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2000 ctx
->bind_fs_state(pipe
,
2001 blitter_get_fs_texfetch_col(ctx
, tex
->format
, tex
->format
, tex
->target
,
2002 1, 1, PIPE_TEX_FILTER_LINEAR
, false));
2005 if (tex
->target
== PIPE_TEXTURE_RECT
) {
2006 sampler_state
= ctx
->sampler_state_rect_linear
;
2008 sampler_state
= ctx
->sampler_state_linear
;
2010 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
2011 0, 1, &sampler_state
);
2013 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2014 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2016 for (src_level
= base_level
; src_level
< last_level
; src_level
++) {
2017 struct pipe_box dstbox
= {0}, srcbox
= {0};
2018 unsigned dst_level
= src_level
+ 1;
2020 dstbox
.width
= u_minify(tex
->width0
, dst_level
);
2021 dstbox
.height
= u_minify(tex
->height0
, dst_level
);
2023 srcbox
.width
= u_minify(tex
->width0
, src_level
);
2024 srcbox
.height
= u_minify(tex
->height0
, src_level
);
2026 if (tex
->target
== PIPE_TEXTURE_3D
) {
2027 dstbox
.depth
= util_max_layer(tex
, dst_level
) + 1;
2028 srcbox
.depth
= util_max_layer(tex
, src_level
) + 1;
2030 dstbox
.z
= srcbox
.z
= first_layer
;
2031 dstbox
.depth
= srcbox
.depth
= last_layer
- first_layer
+ 1;
2034 /* Initialize the surface. */
2035 util_blitter_default_dst_texture(&dst_templ
, tex
, dst_level
,
2037 dst_templ
.format
= format
;
2038 dst_view
= pipe
->create_surface(pipe
, tex
, &dst_templ
);
2040 /* Initialize the sampler view. */
2041 util_blitter_default_src_texture(&src_templ
, tex
, src_level
);
2042 src_templ
.format
= format
;
2043 src_view
= pipe
->create_sampler_view(pipe
, tex
, &src_templ
);
2045 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src_view
);
2047 do_blits(ctx
, dst_view
, &dstbox
, src_view
, tex
->width0
, tex
->height0
,
2048 &srcbox
, is_depth
, false);
2050 pipe_surface_reference(&dst_view
, NULL
);
2051 pipe_sampler_view_reference(&src_view
, NULL
);
2054 util_blitter_restore_vertex_states(blitter
);
2055 util_blitter_restore_fragment_states(blitter
);
2056 util_blitter_restore_textures(blitter
);
2057 util_blitter_restore_fb_state(blitter
);
2058 util_blitter_restore_render_cond(blitter
);
2059 util_blitter_unset_running_flag(blitter
);
2062 /* Clear a region of a color surface to a constant value. */
2063 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
2064 struct pipe_surface
*dstsurf
,
2065 const union pipe_color_union
*color
,
2066 unsigned dstx
, unsigned dsty
,
2067 unsigned width
, unsigned height
)
2069 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2070 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2071 struct pipe_framebuffer_state fb_state
;
2072 unsigned num_layers
;
2074 assert(dstsurf
->texture
);
2075 if (!dstsurf
->texture
)
2078 /* check the saved state */
2079 util_blitter_set_running_flag(blitter
);
2080 blitter_check_saved_vertex_states(ctx
);
2081 blitter_check_saved_fragment_states(ctx
);
2082 blitter_check_saved_fb_state(ctx
);
2083 blitter_disable_render_cond(ctx
);
2086 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
2087 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2088 bind_fs_write_one_cbuf(ctx
);
2089 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2091 /* set a framebuffer state */
2092 fb_state
.width
= dstsurf
->width
;
2093 fb_state
.height
= dstsurf
->height
;
2094 fb_state
.nr_cbufs
= 1;
2095 fb_state
.cbufs
[0] = dstsurf
;
2097 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2098 pipe
->set_sample_mask(pipe
, ~0);
2100 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2102 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
2103 if (num_layers
> 1 && ctx
->has_layered
) {
2104 blitter_set_common_draw_rect_state(ctx
, FALSE
, TRUE
);
2105 blitter_set_clear_color(ctx
, color
);
2106 blitter_draw(ctx
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0, num_layers
);
2109 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2110 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
2111 UTIL_BLITTER_ATTRIB_COLOR
, color
);
2114 util_blitter_restore_vertex_states(blitter
);
2115 util_blitter_restore_fragment_states(blitter
);
2116 util_blitter_restore_fb_state(blitter
);
2117 util_blitter_restore_render_cond(blitter
);
2118 util_blitter_unset_running_flag(blitter
);
2121 /* Clear a region of a depth stencil surface. */
2122 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
2123 struct pipe_surface
*dstsurf
,
2124 unsigned clear_flags
,
2127 unsigned dstx
, unsigned dsty
,
2128 unsigned width
, unsigned height
)
2130 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2131 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2132 struct pipe_framebuffer_state fb_state
;
2133 struct pipe_stencil_ref sr
= { { 0 } };
2134 unsigned num_layers
;
2136 assert(dstsurf
->texture
);
2137 if (!dstsurf
->texture
)
2140 /* check the saved state */
2141 util_blitter_set_running_flag(blitter
);
2142 blitter_check_saved_vertex_states(ctx
);
2143 blitter_check_saved_fragment_states(ctx
);
2144 blitter_check_saved_fb_state(ctx
);
2145 blitter_disable_render_cond(ctx
);
2148 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
2149 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
2150 sr
.ref_value
[0] = stencil
& 0xff;
2151 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
2152 pipe
->set_stencil_ref(pipe
, &sr
);
2154 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
2155 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
2157 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
2158 sr
.ref_value
[0] = stencil
& 0xff;
2159 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
2160 pipe
->set_stencil_ref(pipe
, &sr
);
2163 /* hmm that should be illegal probably, or make it a no-op somewhere */
2164 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2167 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2169 /* set a framebuffer state */
2170 fb_state
.width
= dstsurf
->width
;
2171 fb_state
.height
= dstsurf
->height
;
2172 fb_state
.nr_cbufs
= 0;
2173 fb_state
.cbufs
[0] = 0;
2174 fb_state
.zsbuf
= dstsurf
;
2175 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2176 pipe
->set_sample_mask(pipe
, ~0);
2178 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2180 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
2181 if (num_layers
> 1 && ctx
->has_layered
) {
2182 blitter_set_common_draw_rect_state(ctx
, FALSE
, TRUE
);
2183 blitter_draw(ctx
, dstx
, dsty
, dstx
+width
, dsty
+height
, (float) depth
, num_layers
);
2186 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2187 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
,
2189 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2192 util_blitter_restore_vertex_states(blitter
);
2193 util_blitter_restore_fragment_states(blitter
);
2194 util_blitter_restore_fb_state(blitter
);
2195 util_blitter_restore_render_cond(blitter
);
2196 util_blitter_unset_running_flag(blitter
);
2199 /* draw a rectangle across a region using a custom dsa stage - for r600g */
2200 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
2201 struct pipe_surface
*zsurf
,
2202 struct pipe_surface
*cbsurf
,
2203 unsigned sample_mask
,
2204 void *dsa_stage
, float depth
)
2206 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2207 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2208 struct pipe_framebuffer_state fb_state
;
2210 assert(zsurf
->texture
);
2211 if (!zsurf
->texture
)
2214 /* check the saved state */
2215 util_blitter_set_running_flag(blitter
);
2216 blitter_check_saved_vertex_states(ctx
);
2217 blitter_check_saved_fragment_states(ctx
);
2218 blitter_check_saved_fb_state(ctx
);
2219 blitter_disable_render_cond(ctx
);
2222 pipe
->bind_blend_state(pipe
, cbsurf
? ctx
->blend
[PIPE_MASK_RGBA
][0] :
2224 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
2226 bind_fs_write_one_cbuf(ctx
);
2229 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2231 /* set a framebuffer state */
2232 fb_state
.width
= zsurf
->width
;
2233 fb_state
.height
= zsurf
->height
;
2234 fb_state
.nr_cbufs
= 1;
2236 fb_state
.cbufs
[0] = cbsurf
;
2237 fb_state
.nr_cbufs
= 1;
2239 fb_state
.cbufs
[0] = NULL
;
2240 fb_state
.nr_cbufs
= 0;
2242 fb_state
.zsbuf
= zsurf
;
2243 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2244 pipe
->set_sample_mask(pipe
, sample_mask
);
2246 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2247 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
2248 blitter
->draw_rectangle(blitter
, 0, 0, zsurf
->width
, zsurf
->height
, depth
,
2249 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2251 util_blitter_restore_vertex_states(blitter
);
2252 util_blitter_restore_fragment_states(blitter
);
2253 util_blitter_restore_fb_state(blitter
);
2254 util_blitter_restore_render_cond(blitter
);
2255 util_blitter_unset_running_flag(blitter
);
2258 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
2259 struct pipe_resource
*dst
,
2261 struct pipe_resource
*src
,
2265 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2266 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2267 struct pipe_vertex_buffer vb
;
2268 struct pipe_stream_output_target
*so_target
;
2269 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2271 if (srcx
>= src
->width0
||
2272 dstx
>= dst
->width0
) {
2275 if (srcx
+ size
> src
->width0
) {
2276 size
= src
->width0
- srcx
;
2278 if (dstx
+ size
> dst
->width0
) {
2279 size
= dst
->width0
- dstx
;
2282 /* Drivers not capable of Stream Out should not call this function
2283 * in the first place. */
2284 assert(ctx
->has_stream_out
);
2286 /* Some alignment is required. */
2287 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
2288 !ctx
->has_stream_out
) {
2289 struct pipe_box box
;
2290 u_box_1d(srcx
, size
, &box
);
2291 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
2295 util_blitter_set_running_flag(blitter
);
2296 blitter_check_saved_vertex_states(ctx
);
2297 blitter_disable_render_cond(ctx
);
2299 vb
.is_user_buffer
= false;
2300 vb
.buffer
.resource
= src
;
2301 vb
.buffer_offset
= srcx
;
2304 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2305 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[0]);
2306 bind_vs_pos_only(ctx
, 1);
2307 if (ctx
->has_geometry_shader
)
2308 pipe
->bind_gs_state(pipe
, NULL
);
2309 if (ctx
->has_tessellation
) {
2310 pipe
->bind_tcs_state(pipe
, NULL
);
2311 pipe
->bind_tes_state(pipe
, NULL
);
2313 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2315 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
2316 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2318 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2320 util_blitter_restore_vertex_states(blitter
);
2321 util_blitter_restore_render_cond(blitter
);
2322 util_blitter_unset_running_flag(blitter
);
2323 pipe_so_target_reference(&so_target
, NULL
);
2326 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
2327 struct pipe_resource
*dst
,
2328 unsigned offset
, unsigned size
,
2329 unsigned num_channels
,
2330 const union pipe_color_union
*clear_value
)
2332 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2333 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2334 struct pipe_vertex_buffer vb
= {0};
2335 struct pipe_stream_output_target
*so_target
= NULL
;
2336 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2338 assert(num_channels
>= 1);
2339 assert(num_channels
<= 4);
2341 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
2343 * R600 uses this to initialize texture resources, so width0 might not be
2344 * what you think it is.
2347 /* Streamout is required. */
2348 if (!ctx
->has_stream_out
) {
2349 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2353 /* Some alignment is required. */
2354 if (offset
% 4 != 0 || size
% 4 != 0) {
2355 assert(!"Bad alignment in util_blitter_clear_buffer()");
2359 u_upload_data(pipe
->stream_uploader
, 0, num_channels
*4, 4, clear_value
,
2360 &vb
.buffer_offset
, &vb
.buffer
.resource
);
2361 if (!vb
.buffer
.resource
)
2366 util_blitter_set_running_flag(blitter
);
2367 blitter_check_saved_vertex_states(ctx
);
2368 blitter_disable_render_cond(ctx
);
2370 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2371 pipe
->bind_vertex_elements_state(pipe
,
2372 ctx
->velem_state_readbuf
[num_channels
-1]);
2373 bind_vs_pos_only(ctx
, num_channels
);
2374 if (ctx
->has_geometry_shader
)
2375 pipe
->bind_gs_state(pipe
, NULL
);
2376 if (ctx
->has_tessellation
) {
2377 pipe
->bind_tcs_state(pipe
, NULL
);
2378 pipe
->bind_tes_state(pipe
, NULL
);
2380 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2382 so_target
= pipe
->create_stream_output_target(pipe
, dst
, offset
, size
);
2383 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2385 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2388 util_blitter_restore_vertex_states(blitter
);
2389 util_blitter_restore_render_cond(blitter
);
2390 util_blitter_unset_running_flag(blitter
);
2391 pipe_so_target_reference(&so_target
, NULL
);
2392 pipe_resource_reference(&vb
.buffer
.resource
, NULL
);
2395 /* probably radeon specific */
2396 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
2397 struct pipe_resource
*dst
,
2400 struct pipe_resource
*src
,
2402 unsigned sample_mask
,
2404 enum pipe_format format
)
2406 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2407 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2408 struct pipe_framebuffer_state fb_state
;
2409 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
2411 util_blitter_set_running_flag(blitter
);
2412 blitter_check_saved_vertex_states(ctx
);
2413 blitter_check_saved_fragment_states(ctx
);
2414 blitter_disable_render_cond(ctx
);
2417 pipe
->bind_blend_state(pipe
, custom_blend
);
2418 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2419 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2420 bind_fs_write_one_cbuf(ctx
);
2421 pipe
->set_sample_mask(pipe
, sample_mask
);
2423 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
2424 surf_tmpl
.format
= format
;
2425 surf_tmpl
.u
.tex
.level
= dst_level
;
2426 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
2427 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
2429 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
2431 surf_tmpl
.u
.tex
.level
= 0;
2432 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
2433 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
2435 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
2437 /* set a framebuffer state */
2438 fb_state
.width
= src
->width0
;
2439 fb_state
.height
= src
->height0
;
2440 fb_state
.nr_cbufs
= 2;
2441 fb_state
.cbufs
[0] = srcsurf
;
2442 fb_state
.cbufs
[1] = dstsurf
;
2443 fb_state
.zsbuf
= NULL
;
2444 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2446 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2447 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
2448 blitter
->draw_rectangle(blitter
, 0, 0, src
->width0
, src
->height0
,
2450 util_blitter_restore_fb_state(blitter
);
2451 util_blitter_restore_vertex_states(blitter
);
2452 util_blitter_restore_fragment_states(blitter
);
2453 util_blitter_restore_render_cond(blitter
);
2454 util_blitter_unset_running_flag(blitter
);
2456 pipe_surface_reference(&srcsurf
, NULL
);
2457 pipe_surface_reference(&dstsurf
, NULL
);
2460 void util_blitter_custom_color(struct blitter_context
*blitter
,
2461 struct pipe_surface
*dstsurf
,
2464 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2465 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2466 struct pipe_framebuffer_state fb_state
;
2468 assert(dstsurf
->texture
);
2469 if (!dstsurf
->texture
)
2472 /* check the saved state */
2473 util_blitter_set_running_flag(blitter
);
2474 blitter_check_saved_vertex_states(ctx
);
2475 blitter_check_saved_fragment_states(ctx
);
2476 blitter_check_saved_fb_state(ctx
);
2477 blitter_disable_render_cond(ctx
);
2480 pipe
->bind_blend_state(pipe
, custom_blend
? custom_blend
2481 : ctx
->blend
[PIPE_MASK_RGBA
][0]);
2482 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2483 bind_fs_write_one_cbuf(ctx
);
2484 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2485 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2487 /* set a framebuffer state */
2488 fb_state
.width
= dstsurf
->width
;
2489 fb_state
.height
= dstsurf
->height
;
2490 fb_state
.nr_cbufs
= 1;
2491 fb_state
.cbufs
[0] = dstsurf
;
2493 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2494 pipe
->set_sample_mask(pipe
, ~0);
2496 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2497 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2498 blitter
->draw_rectangle(blitter
, 0, 0, dstsurf
->width
, dstsurf
->height
,
2501 util_blitter_restore_vertex_states(blitter
);
2502 util_blitter_restore_fragment_states(blitter
);
2503 util_blitter_restore_fb_state(blitter
);
2504 util_blitter_restore_render_cond(blitter
);
2505 util_blitter_unset_running_flag(blitter
);