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
];
85 /* FS which outputs a depth from a texture,
86 where the index is PIPE_TEXTURE_* to be sampled. */
87 void *fs_texfetch_depth
[PIPE_MAX_TEXTURE_TYPES
];
88 void *fs_texfetch_depthstencil
[PIPE_MAX_TEXTURE_TYPES
];
89 void *fs_texfetch_stencil
[PIPE_MAX_TEXTURE_TYPES
];
91 /* FS which outputs one sample from a multisample texture. */
92 void *fs_texfetch_col_msaa
[PIPE_MAX_TEXTURE_TYPES
];
93 void *fs_texfetch_depth_msaa
[PIPE_MAX_TEXTURE_TYPES
];
94 void *fs_texfetch_depthstencil_msaa
[PIPE_MAX_TEXTURE_TYPES
];
95 void *fs_texfetch_stencil_msaa
[PIPE_MAX_TEXTURE_TYPES
];
97 /* FS which outputs an average of all samples. */
98 void *fs_resolve
[PIPE_MAX_TEXTURE_TYPES
][NUM_RESOLVE_FRAG_SHADERS
][2];
99 void *fs_resolve_sint
[PIPE_MAX_TEXTURE_TYPES
][NUM_RESOLVE_FRAG_SHADERS
][2];
100 void *fs_resolve_uint
[PIPE_MAX_TEXTURE_TYPES
][NUM_RESOLVE_FRAG_SHADERS
][2];
103 void *blend
[PIPE_MASK_RGBA
+1]; /**< blend state with writemask */
104 void *blend_clear
[GET_CLEAR_BLEND_STATE_IDX(PIPE_CLEAR_COLOR
)+1];
106 /* Depth stencil alpha state. */
107 void *dsa_write_depth_stencil
;
108 void *dsa_write_depth_keep_stencil
;
109 void *dsa_keep_depth_stencil
;
110 void *dsa_keep_depth_write_stencil
;
112 /* Vertex elements states. */
114 void *velem_state_readbuf
[4]; /**< X, XY, XYZ, XYZW */
118 void *sampler_state_linear
;
119 void *sampler_state_rect
;
120 void *sampler_state_rect_linear
;
122 /* Rasterizer state. */
123 void *rs_state
, *rs_state_scissor
, *rs_discard_state
;
125 /* Viewport state. */
126 struct pipe_viewport_state viewport
;
128 /* Destination surface dimensions. */
132 boolean has_geometry_shader
;
133 boolean has_tessellation
;
135 boolean has_stream_out
;
136 boolean has_stencil_export
;
137 boolean has_texture_multisample
;
138 boolean cached_all_shaders
;
140 /* The Draw module overrides these functions.
141 * Always create the blitter before Draw. */
142 void (*bind_fs_state
)(struct pipe_context
*, void *);
143 void (*delete_fs_state
)(struct pipe_context
*, void *);
146 static struct pipe_surface
*
147 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
148 struct pipe_surface
*surf
);
150 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
)
152 struct blitter_context_priv
*ctx
;
153 struct pipe_blend_state blend
;
154 struct pipe_depth_stencil_alpha_state dsa
;
155 struct pipe_rasterizer_state rs_state
;
156 struct pipe_sampler_state sampler_state
;
157 struct pipe_vertex_element velem
[2];
160 ctx
= CALLOC_STRUCT(blitter_context_priv
);
164 ctx
->base
.pipe
= pipe
;
165 ctx
->base
.draw_rectangle
= util_blitter_draw_rectangle
;
166 ctx
->base
.get_next_surface_layer
= util_blitter_get_next_surface_layer
;
168 ctx
->bind_fs_state
= pipe
->bind_fs_state
;
169 ctx
->delete_fs_state
= pipe
->delete_fs_state
;
171 /* init state objects for them to be considered invalid */
172 ctx
->base
.saved_blend_state
= INVALID_PTR
;
173 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
174 ctx
->base
.saved_rs_state
= INVALID_PTR
;
175 ctx
->base
.saved_fs
= INVALID_PTR
;
176 ctx
->base
.saved_vs
= INVALID_PTR
;
177 ctx
->base
.saved_gs
= INVALID_PTR
;
178 ctx
->base
.saved_velem_state
= INVALID_PTR
;
179 ctx
->base
.saved_fb_state
.nr_cbufs
= ~0;
180 ctx
->base
.saved_num_sampler_views
= ~0;
181 ctx
->base
.saved_num_sampler_states
= ~0;
182 ctx
->base
.saved_num_so_targets
= ~0;
184 ctx
->has_geometry_shader
=
185 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
186 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
188 ctx
->has_tessellation
=
189 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_TESS_CTRL
,
190 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
192 ctx
->has_stream_out
=
193 pipe
->screen
->get_param(pipe
->screen
,
194 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0;
196 ctx
->has_stencil_export
=
197 pipe
->screen
->get_param(pipe
->screen
,
198 PIPE_CAP_SHADER_STENCIL_EXPORT
);
200 ctx
->has_texture_multisample
=
201 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TEXTURE_MULTISAMPLE
);
203 /* blend state objects */
204 memset(&blend
, 0, sizeof(blend
));
206 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
207 blend
.rt
[0].colormask
= i
;
208 ctx
->blend
[i
] = pipe
->create_blend_state(pipe
, &blend
);
211 /* depth stencil alpha state objects */
212 memset(&dsa
, 0, sizeof(dsa
));
213 ctx
->dsa_keep_depth_stencil
=
214 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
216 dsa
.depth
.enabled
= 1;
217 dsa
.depth
.writemask
= 1;
218 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
219 ctx
->dsa_write_depth_keep_stencil
=
220 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
222 dsa
.stencil
[0].enabled
= 1;
223 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
224 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
225 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
226 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
227 dsa
.stencil
[0].valuemask
= 0xff;
228 dsa
.stencil
[0].writemask
= 0xff;
229 ctx
->dsa_write_depth_stencil
=
230 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
232 dsa
.depth
.enabled
= 0;
233 dsa
.depth
.writemask
= 0;
234 ctx
->dsa_keep_depth_write_stencil
=
235 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
238 memset(&sampler_state
, 0, sizeof(sampler_state
));
239 sampler_state
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
240 sampler_state
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
241 sampler_state
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
242 sampler_state
.normalized_coords
= 1;
243 ctx
->sampler_state
= pipe
->create_sampler_state(pipe
, &sampler_state
);
244 sampler_state
.normalized_coords
= 0;
245 ctx
->sampler_state_rect
= pipe
->create_sampler_state(pipe
, &sampler_state
);
247 sampler_state
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
248 sampler_state
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
249 sampler_state
.normalized_coords
= 1;
250 ctx
->sampler_state_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
251 sampler_state
.normalized_coords
= 0;
252 ctx
->sampler_state_rect_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
254 /* rasterizer state */
255 memset(&rs_state
, 0, sizeof(rs_state
));
256 rs_state
.cull_face
= PIPE_FACE_NONE
;
257 rs_state
.half_pixel_center
= 1;
258 rs_state
.bottom_edge_rule
= 1;
259 rs_state
.flatshade
= 1;
260 rs_state
.depth_clip
= 1;
261 ctx
->rs_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
263 rs_state
.scissor
= 1;
264 ctx
->rs_state_scissor
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
266 if (ctx
->has_stream_out
) {
267 rs_state
.scissor
= 0;
268 rs_state
.rasterizer_discard
= 1;
269 ctx
->rs_discard_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
272 ctx
->base
.vb_slot
= 0; /* 0 for now */
274 /* vertex elements states */
275 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
276 for (i
= 0; i
< 2; i
++) {
277 velem
[i
].src_offset
= i
* 4 * sizeof(float);
278 velem
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
279 velem
[i
].vertex_buffer_index
= ctx
->base
.vb_slot
;
281 ctx
->velem_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
283 if (ctx
->has_stream_out
) {
284 static enum pipe_format formats
[4] = {
285 PIPE_FORMAT_R32_UINT
,
286 PIPE_FORMAT_R32G32_UINT
,
287 PIPE_FORMAT_R32G32B32_UINT
,
288 PIPE_FORMAT_R32G32B32A32_UINT
291 for (i
= 0; i
< 4; i
++) {
292 velem
[0].src_format
= formats
[i
];
293 velem
[0].vertex_buffer_index
= ctx
->base
.vb_slot
;
294 ctx
->velem_state_readbuf
[i
] =
295 pipe
->create_vertex_elements_state(pipe
, 1, &velem
[0]);
300 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_INSTANCEID
) &&
301 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT
);
303 /* set invariant vertex coordinates */
304 for (i
= 0; i
< 4; i
++)
305 ctx
->vertices
[i
][0][3] = 1; /*v.w*/
307 ctx
->upload
= u_upload_create(pipe
, 65536, 4, PIPE_BIND_VERTEX_BUFFER
);
312 static void bind_vs_pos_only(struct blitter_context_priv
*ctx
)
314 struct pipe_context
*pipe
= ctx
->base
.pipe
;
316 if (!ctx
->vs_pos_only
) {
317 struct pipe_stream_output_info so
;
318 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
};
319 const uint semantic_indices
[] = { 0 };
321 memset(&so
, 0, sizeof(so
));
323 so
.output
[0].num_components
= 1;
327 util_make_vertex_passthrough_shader_with_so(pipe
, 1, semantic_names
,
328 semantic_indices
, FALSE
,
332 pipe
->bind_vs_state(pipe
, ctx
->vs_pos_only
);
335 static void bind_vs_passthrough(struct blitter_context_priv
*ctx
)
337 struct pipe_context
*pipe
= ctx
->base
.pipe
;
340 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
341 TGSI_SEMANTIC_GENERIC
};
342 const uint semantic_indices
[] = { 0, 0 };
344 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
345 semantic_indices
, FALSE
);
348 pipe
->bind_vs_state(pipe
, ctx
->vs
);
351 static void bind_vs_layered(struct blitter_context_priv
*ctx
)
353 struct pipe_context
*pipe
= ctx
->base
.pipe
;
355 if (!ctx
->vs_layered
) {
356 ctx
->vs_layered
= util_make_layered_clear_vertex_shader(pipe
);
359 pipe
->bind_vs_state(pipe
, ctx
->vs_layered
);
362 static void bind_fs_empty(struct blitter_context_priv
*ctx
)
364 struct pipe_context
*pipe
= ctx
->base
.pipe
;
366 if (!ctx
->fs_empty
) {
367 assert(!ctx
->cached_all_shaders
);
368 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
371 ctx
->bind_fs_state(pipe
, ctx
->fs_empty
);
374 static void bind_fs_write_one_cbuf(struct blitter_context_priv
*ctx
)
376 struct pipe_context
*pipe
= ctx
->base
.pipe
;
378 if (!ctx
->fs_write_one_cbuf
) {
379 assert(!ctx
->cached_all_shaders
);
380 ctx
->fs_write_one_cbuf
=
381 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
382 TGSI_INTERPOLATE_CONSTANT
, FALSE
);
385 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
388 static void bind_fs_write_all_cbufs(struct blitter_context_priv
*ctx
)
390 struct pipe_context
*pipe
= ctx
->base
.pipe
;
392 if (!ctx
->fs_write_all_cbufs
) {
393 assert(!ctx
->cached_all_shaders
);
394 ctx
->fs_write_all_cbufs
=
395 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
396 TGSI_INTERPOLATE_CONSTANT
, TRUE
);
399 ctx
->bind_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
402 void util_blitter_destroy(struct blitter_context
*blitter
)
404 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
405 struct pipe_context
*pipe
= blitter
->pipe
;
408 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
409 pipe
->delete_blend_state(pipe
, ctx
->blend
[i
]);
411 for (i
= 0; i
< Elements(ctx
->blend_clear
); i
++) {
412 if (ctx
->blend_clear
[i
])
413 pipe
->delete_blend_state(pipe
, ctx
->blend_clear
[i
]);
415 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
416 pipe
->delete_depth_stencil_alpha_state(pipe
,
417 ctx
->dsa_write_depth_keep_stencil
);
418 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
419 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
421 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
);
422 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state_scissor
);
423 if (ctx
->rs_discard_state
)
424 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_discard_state
);
426 pipe
->delete_vs_state(pipe
, ctx
->vs
);
427 if (ctx
->vs_pos_only
)
428 pipe
->delete_vs_state(pipe
, ctx
->vs_pos_only
);
430 pipe
->delete_vs_state(pipe
, ctx
->vs_layered
);
431 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state
);
432 for (i
= 0; i
< 4; i
++) {
433 if (ctx
->velem_state_readbuf
[i
]) {
434 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[i
]);
438 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
439 if (ctx
->fs_texfetch_col
[i
])
440 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[i
]);
441 if (ctx
->fs_texfetch_depth
[i
])
442 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
]);
443 if (ctx
->fs_texfetch_depthstencil
[i
])
444 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil
[i
]);
445 if (ctx
->fs_texfetch_stencil
[i
])
446 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil
[i
]);
448 if (ctx
->fs_texfetch_col_msaa
[i
])
449 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col_msaa
[i
]);
450 if (ctx
->fs_texfetch_depth_msaa
[i
])
451 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth_msaa
[i
]);
452 if (ctx
->fs_texfetch_depthstencil_msaa
[i
])
453 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil_msaa
[i
]);
454 if (ctx
->fs_texfetch_stencil_msaa
[i
])
455 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil_msaa
[i
]);
457 for (j
= 0; j
< Elements(ctx
->fs_resolve
[i
]); j
++)
458 for (f
= 0; f
< 2; f
++)
459 if (ctx
->fs_resolve
[i
][j
][f
])
460 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve
[i
][j
][f
]);
462 for (j
= 0; j
< Elements(ctx
->fs_resolve_sint
[i
]); j
++)
463 for (f
= 0; f
< 2; f
++)
464 if (ctx
->fs_resolve_sint
[i
][j
][f
])
465 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve_sint
[i
][j
][f
]);
467 for (j
= 0; j
< Elements(ctx
->fs_resolve_uint
[i
]); j
++)
468 for (f
= 0; f
< 2; f
++)
469 if (ctx
->fs_resolve_uint
[i
][j
][f
])
470 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve_uint
[i
][j
][f
]);
474 ctx
->delete_fs_state(pipe
, ctx
->fs_empty
);
475 if (ctx
->fs_write_one_cbuf
)
476 ctx
->delete_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
477 if (ctx
->fs_write_all_cbufs
)
478 ctx
->delete_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
480 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect_linear
);
481 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect
);
482 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_linear
);
483 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
);
484 u_upload_destroy(ctx
->upload
);
488 void util_blitter_set_texture_multisample(struct blitter_context
*blitter
,
491 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
493 ctx
->has_texture_multisample
= supported
;
496 static void blitter_set_running_flag(struct blitter_context_priv
*ctx
)
498 if (ctx
->base
.running
) {
499 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
502 ctx
->base
.running
= TRUE
;
505 static void blitter_unset_running_flag(struct blitter_context_priv
*ctx
)
507 if (!ctx
->base
.running
) {
508 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
511 ctx
->base
.running
= FALSE
;
514 static void blitter_check_saved_vertex_states(struct blitter_context_priv
*ctx
)
516 assert(ctx
->base
.saved_velem_state
!= INVALID_PTR
);
517 assert(ctx
->base
.saved_vs
!= INVALID_PTR
);
518 assert(!ctx
->has_geometry_shader
|| ctx
->base
.saved_gs
!= INVALID_PTR
);
519 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tcs
!= INVALID_PTR
);
520 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tes
!= INVALID_PTR
);
521 assert(!ctx
->has_stream_out
|| ctx
->base
.saved_num_so_targets
!= ~0);
522 assert(ctx
->base
.saved_rs_state
!= INVALID_PTR
);
525 static void blitter_restore_vertex_states(struct blitter_context_priv
*ctx
)
527 struct pipe_context
*pipe
= ctx
->base
.pipe
;
531 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1,
532 &ctx
->base
.saved_vertex_buffer
);
533 pipe_resource_reference(&ctx
->base
.saved_vertex_buffer
.buffer
, NULL
);
535 /* Vertex elements. */
536 pipe
->bind_vertex_elements_state(pipe
, ctx
->base
.saved_velem_state
);
537 ctx
->base
.saved_velem_state
= INVALID_PTR
;
540 pipe
->bind_vs_state(pipe
, ctx
->base
.saved_vs
);
541 ctx
->base
.saved_vs
= INVALID_PTR
;
543 /* Geometry shader. */
544 if (ctx
->has_geometry_shader
) {
545 pipe
->bind_gs_state(pipe
, ctx
->base
.saved_gs
);
546 ctx
->base
.saved_gs
= INVALID_PTR
;
549 if (ctx
->has_tessellation
) {
550 pipe
->bind_tcs_state(pipe
, ctx
->base
.saved_tcs
);
551 pipe
->bind_tes_state(pipe
, ctx
->base
.saved_tes
);
552 ctx
->base
.saved_tcs
= INVALID_PTR
;
553 ctx
->base
.saved_tes
= INVALID_PTR
;
556 /* Stream outputs. */
557 if (ctx
->has_stream_out
) {
558 unsigned offsets
[PIPE_MAX_SO_BUFFERS
];
559 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
560 offsets
[i
] = (unsigned)-1;
561 pipe
->set_stream_output_targets(pipe
,
562 ctx
->base
.saved_num_so_targets
,
563 ctx
->base
.saved_so_targets
, offsets
);
565 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
566 pipe_so_target_reference(&ctx
->base
.saved_so_targets
[i
], NULL
);
568 ctx
->base
.saved_num_so_targets
= ~0;
572 pipe
->bind_rasterizer_state(pipe
, ctx
->base
.saved_rs_state
);
573 ctx
->base
.saved_rs_state
= INVALID_PTR
;
576 static void blitter_check_saved_fragment_states(struct blitter_context_priv
*ctx
)
578 assert(ctx
->base
.saved_fs
!= INVALID_PTR
);
579 assert(ctx
->base
.saved_dsa_state
!= INVALID_PTR
);
580 assert(ctx
->base
.saved_blend_state
!= INVALID_PTR
);
583 static void blitter_restore_fragment_states(struct blitter_context_priv
*ctx
)
585 struct pipe_context
*pipe
= ctx
->base
.pipe
;
587 /* Fragment shader. */
588 ctx
->bind_fs_state(pipe
, ctx
->base
.saved_fs
);
589 ctx
->base
.saved_fs
= INVALID_PTR
;
591 /* Depth, stencil, alpha. */
592 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->base
.saved_dsa_state
);
593 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
596 pipe
->bind_blend_state(pipe
, ctx
->base
.saved_blend_state
);
597 ctx
->base
.saved_blend_state
= INVALID_PTR
;
600 if (ctx
->base
.is_sample_mask_saved
) {
601 pipe
->set_sample_mask(pipe
, ctx
->base
.saved_sample_mask
);
602 ctx
->base
.is_sample_mask_saved
= FALSE
;
605 /* Miscellaneous states. */
606 /* XXX check whether these are saved and whether they need to be restored
607 * (depending on the operation) */
608 pipe
->set_stencil_ref(pipe
, &ctx
->base
.saved_stencil_ref
);
609 pipe
->set_viewport_states(pipe
, 0, 1, &ctx
->base
.saved_viewport
);
612 static void blitter_check_saved_fb_state(struct blitter_context_priv
*ctx
)
614 assert(ctx
->base
.saved_fb_state
.nr_cbufs
!= ~0);
617 static void blitter_disable_render_cond(struct blitter_context_priv
*ctx
)
619 struct pipe_context
*pipe
= ctx
->base
.pipe
;
621 if (ctx
->base
.saved_render_cond_query
) {
622 pipe
->render_condition(pipe
, NULL
, FALSE
, 0);
626 static void blitter_restore_render_cond(struct blitter_context_priv
*ctx
)
628 struct pipe_context
*pipe
= ctx
->base
.pipe
;
630 if (ctx
->base
.saved_render_cond_query
) {
631 pipe
->render_condition(pipe
, ctx
->base
.saved_render_cond_query
,
632 ctx
->base
.saved_render_cond_cond
,
633 ctx
->base
.saved_render_cond_mode
);
634 ctx
->base
.saved_render_cond_query
= NULL
;
638 static void blitter_restore_fb_state(struct blitter_context_priv
*ctx
)
640 struct pipe_context
*pipe
= ctx
->base
.pipe
;
642 pipe
->set_framebuffer_state(pipe
, &ctx
->base
.saved_fb_state
);
643 util_unreference_framebuffer_state(&ctx
->base
.saved_fb_state
);
646 static void blitter_check_saved_textures(struct blitter_context_priv
*ctx
)
648 assert(ctx
->base
.saved_num_sampler_states
!= ~0);
649 assert(ctx
->base
.saved_num_sampler_views
!= ~0);
652 static void blitter_restore_textures(struct blitter_context_priv
*ctx
)
654 struct pipe_context
*pipe
= ctx
->base
.pipe
;
657 /* Fragment sampler states. */
658 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0,
659 ctx
->base
.saved_num_sampler_states
,
660 ctx
->base
.saved_sampler_states
);
662 ctx
->base
.saved_num_sampler_states
= ~0;
664 /* Fragment sampler views. */
665 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
666 ctx
->base
.saved_num_sampler_views
,
667 ctx
->base
.saved_sampler_views
);
669 for (i
= 0; i
< ctx
->base
.saved_num_sampler_views
; i
++)
670 pipe_sampler_view_reference(&ctx
->base
.saved_sampler_views
[i
], NULL
);
672 ctx
->base
.saved_num_sampler_views
= ~0;
675 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
676 int x1
, int y1
, int x2
, int y2
,
681 /* set vertex positions */
682 ctx
->vertices
[0][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v0.x*/
683 ctx
->vertices
[0][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v0.y*/
685 ctx
->vertices
[1][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v1.x*/
686 ctx
->vertices
[1][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v1.y*/
688 ctx
->vertices
[2][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v2.x*/
689 ctx
->vertices
[2][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v2.y*/
691 ctx
->vertices
[3][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v3.x*/
692 ctx
->vertices
[3][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v3.y*/
694 for (i
= 0; i
< 4; i
++)
695 ctx
->vertices
[i
][0][2] = depth
; /*z*/
698 ctx
->viewport
.scale
[0] = 0.5f
* ctx
->dst_width
;
699 ctx
->viewport
.scale
[1] = 0.5f
* ctx
->dst_height
;
700 ctx
->viewport
.scale
[2] = 1.0f
;
701 ctx
->viewport
.translate
[0] = 0.5f
* ctx
->dst_width
;
702 ctx
->viewport
.translate
[1] = 0.5f
* ctx
->dst_height
;
703 ctx
->viewport
.translate
[2] = 0.0f
;
704 ctx
->base
.pipe
->set_viewport_states(ctx
->base
.pipe
, 0, 1, &ctx
->viewport
);
707 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
708 const union pipe_color_union
*color
)
713 for (i
= 0; i
< 4; i
++) {
714 uint32_t *uiverts
= (uint32_t *)ctx
->vertices
[i
][1];
715 uiverts
[0] = color
->ui
[0];
716 uiverts
[1] = color
->ui
[1];
717 uiverts
[2] = color
->ui
[2];
718 uiverts
[3] = color
->ui
[3];
721 for (i
= 0; i
< 4; i
++) {
722 ctx
->vertices
[i
][1][0] = 0;
723 ctx
->vertices
[i
][1][1] = 0;
724 ctx
->vertices
[i
][1][2] = 0;
725 ctx
->vertices
[i
][1][3] = 0;
730 static void get_texcoords(struct pipe_sampler_view
*src
,
731 unsigned src_width0
, unsigned src_height0
,
732 int x1
, int y1
, int x2
, int y2
,
735 struct pipe_resource
*tex
= src
->texture
;
736 unsigned level
= src
->u
.tex
.first_level
;
737 boolean normalized
= tex
->target
!= PIPE_TEXTURE_RECT
&&
738 tex
->nr_samples
<= 1;
741 out
[0] = x1
/ (float)u_minify(src_width0
, level
);
742 out
[1] = y1
/ (float)u_minify(src_height0
, level
);
743 out
[2] = x2
/ (float)u_minify(src_width0
, level
);
744 out
[3] = y2
/ (float)u_minify(src_height0
, level
);
753 static void set_texcoords_in_vertices(const float coord
[4],
754 float *out
, unsigned stride
)
756 out
[0] = coord
[0]; /*t0.s*/
757 out
[1] = coord
[1]; /*t0.t*/
759 out
[0] = coord
[2]; /*t1.s*/
760 out
[1] = coord
[1]; /*t1.t*/
762 out
[0] = coord
[2]; /*t2.s*/
763 out
[1] = coord
[3]; /*t2.t*/
765 out
[0] = coord
[0]; /*t3.s*/
766 out
[1] = coord
[3]; /*t3.t*/
769 static void blitter_set_texcoords(struct blitter_context_priv
*ctx
,
770 struct pipe_sampler_view
*src
,
771 unsigned src_width0
, unsigned src_height0
,
772 float layer
, unsigned sample
,
773 int x1
, int y1
, int x2
, int y2
)
777 float face_coord
[4][2];
779 get_texcoords(src
, src_width0
, src_height0
, x1
, y1
, x2
, y2
, coord
);
781 if (src
->texture
->target
== PIPE_TEXTURE_CUBE
||
782 src
->texture
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
783 set_texcoords_in_vertices(coord
, &face_coord
[0][0], 2);
784 util_map_texcoords2d_onto_cubemap((unsigned)layer
% 6,
785 /* pointer, stride in floats */
786 &face_coord
[0][0], 2,
787 &ctx
->vertices
[0][1][0], 8,
790 set_texcoords_in_vertices(coord
, &ctx
->vertices
[0][1][0], 8);
794 switch (src
->texture
->target
) {
795 case PIPE_TEXTURE_3D
:
797 float r
= layer
/ (float)u_minify(src
->texture
->depth0
,
798 src
->u
.tex
.first_level
);
799 for (i
= 0; i
< 4; i
++)
800 ctx
->vertices
[i
][1][2] = r
; /*r*/
804 case PIPE_TEXTURE_1D_ARRAY
:
805 for (i
= 0; i
< 4; i
++)
806 ctx
->vertices
[i
][1][1] = (float) layer
; /*t*/
809 case PIPE_TEXTURE_2D_ARRAY
:
810 for (i
= 0; i
< 4; i
++) {
811 ctx
->vertices
[i
][1][2] = (float) layer
; /*r*/
812 ctx
->vertices
[i
][1][3] = (float) sample
; /*q*/
816 case PIPE_TEXTURE_CUBE_ARRAY
:
817 for (i
= 0; i
< 4; i
++)
818 ctx
->vertices
[i
][1][3] = (float) ((unsigned)layer
/ 6); /*w*/
821 case PIPE_TEXTURE_2D
:
822 for (i
= 0; i
< 4; i
++) {
823 ctx
->vertices
[i
][1][3] = (float) sample
; /*r*/
831 static void blitter_set_dst_dimensions(struct blitter_context_priv
*ctx
,
832 unsigned width
, unsigned height
)
834 ctx
->dst_width
= width
;
835 ctx
->dst_height
= height
;
838 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
839 enum pipe_format format
,
840 enum pipe_texture_target target
,
841 unsigned src_nr_samples
,
842 unsigned dst_nr_samples
,
845 struct pipe_context
*pipe
= ctx
->base
.pipe
;
846 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, src_nr_samples
);
848 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
850 if (src_nr_samples
> 1) {
853 if (dst_nr_samples
<= 1) {
854 /* The destination has one sample, so we'll do color resolve. */
855 boolean is_uint
, is_sint
;
856 unsigned index
= GET_MSAA_RESOLVE_FS_IDX(src_nr_samples
);
858 is_uint
= util_format_is_pure_uint(format
);
859 is_sint
= util_format_is_pure_sint(format
);
864 shader
= &ctx
->fs_resolve_uint
[target
][index
][filter
];
866 shader
= &ctx
->fs_resolve_sint
[target
][index
][filter
];
868 shader
= &ctx
->fs_resolve
[target
][index
][filter
];
871 assert(!ctx
->cached_all_shaders
);
872 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
873 *shader
= util_make_fs_msaa_resolve_bilinear(pipe
, tgsi_tex
,
878 *shader
= util_make_fs_msaa_resolve(pipe
, tgsi_tex
,
885 /* The destination has multiple samples, we'll do
886 * an MSAA->MSAA copy.
888 shader
= &ctx
->fs_texfetch_col_msaa
[target
];
890 /* Create the fragment shader on-demand. */
892 assert(!ctx
->cached_all_shaders
);
893 *shader
= util_make_fs_blit_msaa_color(pipe
, tgsi_tex
);
899 void **shader
= &ctx
->fs_texfetch_col
[target
];
901 /* Create the fragment shader on-demand. */
903 assert(!ctx
->cached_all_shaders
);
904 *shader
= util_make_fragment_tex_shader(pipe
, tgsi_tex
,
905 TGSI_INTERPOLATE_LINEAR
);
913 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
914 enum pipe_texture_target target
,
917 struct pipe_context
*pipe
= ctx
->base
.pipe
;
919 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
921 if (nr_samples
> 1) {
922 void **shader
= &ctx
->fs_texfetch_depth_msaa
[target
];
924 /* Create the fragment shader on-demand. */
927 assert(!ctx
->cached_all_shaders
);
928 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
929 *shader
= util_make_fs_blit_msaa_depth(pipe
, tgsi_tex
);
934 void **shader
= &ctx
->fs_texfetch_depth
[target
];
936 /* Create the fragment shader on-demand. */
939 assert(!ctx
->cached_all_shaders
);
940 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
942 util_make_fragment_tex_shader_writedepth(pipe
, tgsi_tex
,
943 TGSI_INTERPOLATE_LINEAR
);
951 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv
*ctx
,
952 enum pipe_texture_target target
,
955 struct pipe_context
*pipe
= ctx
->base
.pipe
;
957 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
959 if (nr_samples
> 1) {
960 void **shader
= &ctx
->fs_texfetch_depthstencil_msaa
[target
];
962 /* Create the fragment shader on-demand. */
965 assert(!ctx
->cached_all_shaders
);
966 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
967 *shader
= util_make_fs_blit_msaa_depthstencil(pipe
, tgsi_tex
);
972 void **shader
= &ctx
->fs_texfetch_depthstencil
[target
];
974 /* Create the fragment shader on-demand. */
977 assert(!ctx
->cached_all_shaders
);
978 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
980 util_make_fragment_tex_shader_writedepthstencil(pipe
, tgsi_tex
,
981 TGSI_INTERPOLATE_LINEAR
);
989 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv
*ctx
,
990 enum pipe_texture_target target
,
993 struct pipe_context
*pipe
= ctx
->base
.pipe
;
995 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
997 if (nr_samples
> 1) {
998 void **shader
= &ctx
->fs_texfetch_stencil_msaa
[target
];
1000 /* Create the fragment shader on-demand. */
1003 assert(!ctx
->cached_all_shaders
);
1004 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1005 *shader
= util_make_fs_blit_msaa_stencil(pipe
, tgsi_tex
);
1010 void **shader
= &ctx
->fs_texfetch_stencil
[target
];
1012 /* Create the fragment shader on-demand. */
1015 assert(!ctx
->cached_all_shaders
);
1016 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1018 util_make_fragment_tex_shader_writestencil(pipe
, tgsi_tex
,
1019 TGSI_INTERPOLATE_LINEAR
);
1028 * Generate and save all fragment shaders that we will ever need for
1029 * blitting. Drivers which use the 'draw' fallbacks will typically use
1030 * this to make sure we generate/use shaders that don't go through the
1031 * draw module's wrapper functions.
1033 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
)
1035 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1036 struct pipe_context
*pipe
= blitter
->pipe
;
1037 struct pipe_screen
*screen
= pipe
->screen
;
1038 unsigned samples
, j
, f
, target
, max_samples
;
1039 boolean has_arraytex
, has_cubearraytex
;
1041 max_samples
= ctx
->has_texture_multisample
? 2 : 1;
1042 has_arraytex
= screen
->get_param(screen
,
1043 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
) != 0;
1044 has_cubearraytex
= screen
->get_param(screen
,
1045 PIPE_CAP_CUBE_MAP_ARRAY
) != 0;
1047 /* It only matters if i <= 1 or > 1. */
1048 for (samples
= 1; samples
<= max_samples
; samples
++) {
1049 for (target
= PIPE_TEXTURE_1D
; target
< PIPE_MAX_TEXTURE_TYPES
; target
++) {
1050 if (!has_arraytex
&&
1051 (target
== PIPE_TEXTURE_1D_ARRAY
||
1052 target
== PIPE_TEXTURE_2D_ARRAY
)) {
1055 if (!has_cubearraytex
&&
1056 (target
== PIPE_TEXTURE_CUBE_ARRAY
))
1060 (target
!= PIPE_TEXTURE_2D
&&
1061 target
!= PIPE_TEXTURE_2D_ARRAY
))
1064 /* If samples == 1, the shaders read one texel. If samples >= 1,
1065 * they read one sample.
1067 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
, target
,
1068 samples
, samples
, 0);
1069 blitter_get_fs_texfetch_depth(ctx
, target
, samples
);
1070 if (ctx
->has_stencil_export
) {
1071 blitter_get_fs_texfetch_depthstencil(ctx
, target
, samples
);
1072 blitter_get_fs_texfetch_stencil(ctx
, target
, samples
);
1078 /* MSAA resolve shaders. */
1079 for (j
= 2; j
< 32; j
++) {
1080 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R32_FLOAT
,
1082 PIPE_BIND_SAMPLER_VIEW
)) {
1086 for (f
= 0; f
< 2; f
++) {
1087 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
, target
,
1089 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
, target
,
1091 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
, target
,
1098 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
1100 ctx
->fs_write_one_cbuf
=
1101 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1102 TGSI_INTERPOLATE_CONSTANT
, FALSE
);
1104 ctx
->fs_write_all_cbufs
=
1105 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1106 TGSI_INTERPOLATE_CONSTANT
, TRUE
);
1108 ctx
->cached_all_shaders
= TRUE
;
1111 static void blitter_set_common_draw_rect_state(struct blitter_context_priv
*ctx
,
1115 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1117 pipe
->bind_rasterizer_state(pipe
, scissor
? ctx
->rs_state_scissor
1120 bind_vs_layered(ctx
);
1122 bind_vs_passthrough(ctx
);
1124 if (ctx
->has_geometry_shader
)
1125 pipe
->bind_gs_state(pipe
, NULL
);
1126 if (ctx
->has_tessellation
) {
1127 pipe
->bind_tcs_state(pipe
, NULL
);
1128 pipe
->bind_tes_state(pipe
, NULL
);
1130 if (ctx
->has_stream_out
)
1131 pipe
->set_stream_output_targets(pipe
, 0, NULL
, NULL
);
1134 static void blitter_draw(struct blitter_context_priv
*ctx
,
1135 int x1
, int y1
, int x2
, int y2
, float depth
,
1136 unsigned num_instances
)
1138 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1139 struct pipe_vertex_buffer vb
= {0};
1141 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
1143 vb
.stride
= 8 * sizeof(float);
1145 u_upload_data(ctx
->upload
, 0, sizeof(ctx
->vertices
), ctx
->vertices
,
1146 &vb
.buffer_offset
, &vb
.buffer
);
1147 u_upload_unmap(ctx
->upload
);
1149 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1150 util_draw_arrays_instanced(pipe
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
1152 pipe_resource_reference(&vb
.buffer
, NULL
);
1155 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
1156 int x1
, int y1
, int x2
, int y2
, float depth
,
1157 enum blitter_attrib_type type
,
1158 const union pipe_color_union
*attrib
)
1160 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1163 case UTIL_BLITTER_ATTRIB_COLOR
:
1164 blitter_set_clear_color(ctx
, attrib
);
1167 case UTIL_BLITTER_ATTRIB_TEXCOORD
:
1168 set_texcoords_in_vertices(attrib
->f
, &ctx
->vertices
[0][1][0], 8);
1174 blitter_draw(ctx
, x1
, y1
, x2
, y2
, depth
, 1);
1177 static void *get_clear_blend_state(struct blitter_context_priv
*ctx
,
1178 unsigned clear_buffers
)
1180 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1183 clear_buffers
&= PIPE_CLEAR_COLOR
;
1185 /* Return an existing blend state. */
1187 return ctx
->blend
[0];
1189 index
= GET_CLEAR_BLEND_STATE_IDX(clear_buffers
);
1191 if (ctx
->blend_clear
[index
])
1192 return ctx
->blend_clear
[index
];
1194 /* Create a new one. */
1196 struct pipe_blend_state blend
= {0};
1199 blend
.independent_blend_enable
= 1;
1201 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1202 if (clear_buffers
& (PIPE_CLEAR_COLOR0
<< i
)) {
1203 blend
.rt
[i
].colormask
= PIPE_MASK_RGBA
;
1207 ctx
->blend_clear
[index
] = pipe
->create_blend_state(pipe
, &blend
);
1209 return ctx
->blend_clear
[index
];
1212 static void util_blitter_clear_custom(struct blitter_context
*blitter
,
1213 unsigned width
, unsigned height
,
1214 unsigned num_layers
,
1215 unsigned clear_buffers
,
1216 const union pipe_color_union
*color
,
1217 double depth
, unsigned stencil
,
1218 void *custom_blend
, void *custom_dsa
)
1220 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1221 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1222 struct pipe_stencil_ref sr
= { { 0 } };
1224 assert(ctx
->has_layered
|| num_layers
<= 1);
1226 blitter_set_running_flag(ctx
);
1227 blitter_check_saved_vertex_states(ctx
);
1228 blitter_check_saved_fragment_states(ctx
);
1229 blitter_disable_render_cond(ctx
);
1233 pipe
->bind_blend_state(pipe
, custom_blend
);
1235 pipe
->bind_blend_state(pipe
, get_clear_blend_state(ctx
, clear_buffers
));
1239 pipe
->bind_depth_stencil_alpha_state(pipe
, custom_dsa
);
1240 } else if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1241 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1242 } else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
1243 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1244 } else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
1245 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1247 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1250 sr
.ref_value
[0] = stencil
& 0xff;
1251 pipe
->set_stencil_ref(pipe
, &sr
);
1253 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1254 bind_fs_write_all_cbufs(ctx
);
1255 pipe
->set_sample_mask(pipe
, ~0);
1257 blitter_set_dst_dimensions(ctx
, width
, height
);
1259 if (num_layers
> 1 && ctx
->has_layered
) {
1260 blitter_set_common_draw_rect_state(ctx
, FALSE
, TRUE
);
1261 blitter_set_clear_color(ctx
, color
);
1262 blitter_draw(ctx
, 0, 0, width
, height
, depth
, num_layers
);
1265 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1266 blitter
->draw_rectangle(blitter
, 0, 0, width
, height
, (float) depth
,
1267 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1270 blitter_restore_vertex_states(ctx
);
1271 blitter_restore_fragment_states(ctx
);
1272 blitter_restore_render_cond(ctx
);
1273 blitter_unset_running_flag(ctx
);
1276 void util_blitter_clear(struct blitter_context
*blitter
,
1277 unsigned width
, unsigned height
, unsigned num_layers
,
1278 unsigned clear_buffers
,
1279 const union pipe_color_union
*color
,
1280 double depth
, unsigned stencil
)
1282 util_blitter_clear_custom(blitter
, width
, height
, num_layers
,
1283 clear_buffers
, color
, depth
, stencil
,
1287 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1288 unsigned width
, unsigned height
,
1289 double depth
, void *custom_dsa
)
1291 static const union pipe_color_union color
;
1292 util_blitter_clear_custom(blitter
, width
, height
, 0, 0, &color
, depth
, 0,
1296 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1297 struct pipe_resource
*dst
,
1301 memset(dst_templ
, 0, sizeof(*dst_templ
));
1302 dst_templ
->format
= util_format_linear(dst
->format
);
1303 dst_templ
->u
.tex
.level
= dstlevel
;
1304 dst_templ
->u
.tex
.first_layer
= dstz
;
1305 dst_templ
->u
.tex
.last_layer
= dstz
;
1308 static struct pipe_surface
*
1309 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
1310 struct pipe_surface
*surf
)
1312 struct pipe_surface dst_templ
;
1314 memset(&dst_templ
, 0, sizeof(dst_templ
));
1315 dst_templ
.format
= surf
->format
;
1316 dst_templ
.u
.tex
.level
= surf
->u
.tex
.level
;
1317 dst_templ
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
+ 1;
1318 dst_templ
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
+ 1;
1320 return pipe
->create_surface(pipe
, surf
->texture
, &dst_templ
);
1323 void util_blitter_default_src_texture(struct pipe_sampler_view
*src_templ
,
1324 struct pipe_resource
*src
,
1327 memset(src_templ
, 0, sizeof(*src_templ
));
1328 src_templ
->target
= src
->target
;
1329 src_templ
->format
= util_format_linear(src
->format
);
1330 src_templ
->u
.tex
.first_level
= srclevel
;
1331 src_templ
->u
.tex
.last_level
= srclevel
;
1332 src_templ
->u
.tex
.first_layer
= 0;
1333 src_templ
->u
.tex
.last_layer
=
1334 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1335 : src
->array_size
- 1;
1336 src_templ
->swizzle_r
= PIPE_SWIZZLE_RED
;
1337 src_templ
->swizzle_g
= PIPE_SWIZZLE_GREEN
;
1338 src_templ
->swizzle_b
= PIPE_SWIZZLE_BLUE
;
1339 src_templ
->swizzle_a
= PIPE_SWIZZLE_ALPHA
;
1342 static boolean
is_blit_generic_supported(struct blitter_context
*blitter
,
1343 const struct pipe_resource
*dst
,
1344 enum pipe_format dst_format
,
1345 const struct pipe_resource
*src
,
1346 enum pipe_format src_format
,
1349 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1350 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1354 const struct util_format_description
*desc
=
1355 util_format_description(dst_format
);
1356 boolean dst_has_stencil
= util_format_has_stencil(desc
);
1358 /* Stencil export must be supported for stencil copy. */
1359 if ((mask
& PIPE_MASK_S
) && dst_has_stencil
&&
1360 !ctx
->has_stencil_export
) {
1364 if (dst_has_stencil
|| util_format_has_depth(desc
))
1365 bind
= PIPE_BIND_DEPTH_STENCIL
;
1367 bind
= PIPE_BIND_RENDER_TARGET
;
1369 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1370 dst
->nr_samples
, bind
)) {
1376 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1380 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1381 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
)) {
1385 /* Check stencil sampler support for stencil copy. */
1386 if (mask
& PIPE_MASK_S
) {
1387 if (util_format_has_stencil(util_format_description(src_format
))) {
1388 enum pipe_format stencil_format
=
1389 util_format_stencil_only(src_format
);
1390 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1392 if (stencil_format
!= src_format
&&
1393 !screen
->is_format_supported(screen
, stencil_format
,
1394 src
->target
, src
->nr_samples
,
1395 PIPE_BIND_SAMPLER_VIEW
)) {
1405 boolean
util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1406 const struct pipe_resource
*dst
,
1407 const struct pipe_resource
*src
)
1409 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1410 src
, src
->format
, PIPE_MASK_RGBAZS
);
1413 boolean
util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1414 const struct pipe_blit_info
*info
)
1416 return is_blit_generic_supported(blitter
,
1417 info
->dst
.resource
, info
->dst
.format
,
1418 info
->src
.resource
, info
->src
.format
,
1422 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1423 struct pipe_resource
*dst
,
1425 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1426 struct pipe_resource
*src
,
1428 const struct pipe_box
*srcbox
)
1430 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1431 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1432 struct pipe_surface
*dst_view
, dst_templ
;
1433 struct pipe_sampler_view src_templ
, *src_view
;
1434 struct pipe_box dstbox
;
1437 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1439 u_box_3d(dstx
, dsty
, dstz
, abs(srcbox
->width
), abs(srcbox
->height
),
1440 abs(srcbox
->depth
), &dstbox
);
1442 /* Initialize the surface. */
1443 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1444 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1446 /* Initialize the sampler view. */
1447 util_blitter_default_src_texture(&src_templ
, src
, src_level
);
1448 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1451 util_blitter_blit_generic(blitter
, dst_view
, &dstbox
,
1452 src_view
, srcbox
, src
->width0
, src
->height0
,
1453 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
);
1455 pipe_surface_reference(&dst_view
, NULL
);
1456 pipe_sampler_view_reference(&src_view
, NULL
);
1459 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1460 struct pipe_surface
*dst
,
1461 const struct pipe_box
*dstbox
,
1462 struct pipe_sampler_view
*src
,
1463 const struct pipe_box
*srcbox
,
1464 unsigned src_width0
, unsigned src_height0
,
1465 unsigned mask
, unsigned filter
,
1466 const struct pipe_scissor_state
*scissor
)
1468 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1469 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1470 struct pipe_framebuffer_state fb_state
;
1471 enum pipe_texture_target src_target
= src
->texture
->target
;
1472 unsigned src_samples
= src
->texture
->nr_samples
;
1473 unsigned dst_samples
= dst
->texture
->nr_samples
;
1474 boolean has_depth
, has_stencil
, has_color
;
1475 boolean blit_stencil
, blit_depth
, blit_color
;
1476 void *sampler_state
;
1477 const struct util_format_description
*src_desc
=
1478 util_format_description(src
->format
);
1479 const struct util_format_description
*dst_desc
=
1480 util_format_description(dst
->format
);
1482 has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
&&
1483 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1484 has_depth
= util_format_has_depth(src_desc
) &&
1485 util_format_has_depth(dst_desc
);
1486 has_stencil
= util_format_has_stencil(src_desc
) &&
1487 util_format_has_stencil(dst_desc
);
1489 blit_color
= has_color
&& (mask
& PIPE_MASK_RGBA
);
1490 blit_depth
= has_depth
&& (mask
& PIPE_MASK_Z
);
1491 blit_stencil
= has_stencil
&& (mask
& PIPE_MASK_S
) &&
1492 ctx
->has_stencil_export
;
1494 if (!blit_stencil
&& !blit_depth
&& !blit_color
) {
1499 (dstbox
->width
== abs(srcbox
->width
) &&
1500 dstbox
->height
== abs(srcbox
->height
))) {
1501 filter
= PIPE_TEX_FILTER_NEAREST
;
1504 /* Check whether the states are properly saved. */
1505 blitter_set_running_flag(ctx
);
1506 blitter_check_saved_vertex_states(ctx
);
1507 blitter_check_saved_fragment_states(ctx
);
1508 blitter_check_saved_textures(ctx
);
1509 blitter_check_saved_fb_state(ctx
);
1510 blitter_disable_render_cond(ctx
);
1512 /* Initialize framebuffer state. */
1513 fb_state
.width
= dst
->width
;
1514 fb_state
.height
= dst
->height
;
1515 fb_state
.nr_cbufs
= blit_depth
|| blit_stencil
? 0 : 1;
1516 fb_state
.cbufs
[0] = NULL
;
1517 fb_state
.zsbuf
= NULL
;
1519 if (blit_depth
|| blit_stencil
) {
1520 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1522 if (blit_depth
&& blit_stencil
) {
1523 pipe
->bind_depth_stencil_alpha_state(pipe
,
1524 ctx
->dsa_write_depth_stencil
);
1525 ctx
->bind_fs_state(pipe
,
1526 blitter_get_fs_texfetch_depthstencil(ctx
, src_target
,
1528 } else if (blit_depth
) {
1529 pipe
->bind_depth_stencil_alpha_state(pipe
,
1530 ctx
->dsa_write_depth_keep_stencil
);
1531 ctx
->bind_fs_state(pipe
,
1532 blitter_get_fs_texfetch_depth(ctx
, src_target
,
1534 } else { /* is_stencil */
1535 pipe
->bind_depth_stencil_alpha_state(pipe
,
1536 ctx
->dsa_keep_depth_write_stencil
);
1537 ctx
->bind_fs_state(pipe
,
1538 blitter_get_fs_texfetch_stencil(ctx
, src_target
,
1543 pipe
->bind_blend_state(pipe
, ctx
->blend
[mask
& PIPE_MASK_RGBA
]);
1544 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1545 ctx
->bind_fs_state(pipe
,
1546 blitter_get_fs_texfetch_col(ctx
, src
->format
, src_target
,
1547 src_samples
, dst_samples
, filter
));
1550 /* Set the linear filter only for scaled color non-MSAA blits. */
1551 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
1552 if (src_target
== PIPE_TEXTURE_RECT
) {
1553 sampler_state
= ctx
->sampler_state_rect_linear
;
1555 sampler_state
= ctx
->sampler_state_linear
;
1558 if (src_target
== PIPE_TEXTURE_RECT
) {
1559 sampler_state
= ctx
->sampler_state_rect
;
1561 sampler_state
= ctx
->sampler_state
;
1566 if (blit_depth
&& blit_stencil
) {
1567 /* Setup two samplers, one for depth and the other one for stencil. */
1568 struct pipe_sampler_view templ
;
1569 struct pipe_sampler_view
*views
[2];
1570 void *samplers
[2] = {sampler_state
, sampler_state
};
1573 templ
.format
= util_format_stencil_only(templ
.format
);
1574 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1577 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1579 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, views
);
1580 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, samplers
);
1582 pipe_sampler_view_reference(&views
[1], NULL
);
1583 } else if (blit_stencil
) {
1584 /* Set a stencil-only sampler view for it not to sample depth instead. */
1585 struct pipe_sampler_view templ
;
1586 struct pipe_sampler_view
*view
;
1589 templ
.format
= util_format_stencil_only(templ
.format
);
1590 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1592 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1594 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &view
);
1595 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1596 0, 1, &sampler_state
);
1598 pipe_sampler_view_reference(&view
, NULL
);
1600 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src
);
1601 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1602 0, 1, &sampler_state
);
1605 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1607 pipe
->set_scissor_states(pipe
, 0, 1, scissor
);
1610 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
, FALSE
);
1611 blitter_set_dst_dimensions(ctx
, dst
->width
, dst
->height
);
1613 if ((src_target
== PIPE_TEXTURE_1D
||
1614 src_target
== PIPE_TEXTURE_2D
||
1615 src_target
== PIPE_TEXTURE_RECT
) &&
1617 /* Draw the quad with the draw_rectangle callback. */
1619 /* Set texture coordinates. - use a pipe color union
1620 * for interface purposes.
1621 * XXX pipe_color_union is a wrong name since we use that to set
1622 * texture coordinates too.
1624 union pipe_color_union coord
;
1625 get_texcoords(src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1626 srcbox
->x
+srcbox
->width
, srcbox
->y
+srcbox
->height
, coord
.f
);
1628 /* Set framebuffer state. */
1629 if (blit_depth
|| blit_stencil
) {
1630 fb_state
.zsbuf
= dst
;
1632 fb_state
.cbufs
[0] = dst
;
1634 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1637 pipe
->set_sample_mask(pipe
, ~0);
1638 blitter
->draw_rectangle(blitter
, dstbox
->x
, dstbox
->y
,
1639 dstbox
->x
+ dstbox
->width
,
1640 dstbox
->y
+ dstbox
->height
, 0,
1641 UTIL_BLITTER_ATTRIB_TEXCOORD
, &coord
);
1643 /* Draw the quad with the generic codepath. */
1645 for (dst_z
= 0; dst_z
< dstbox
->depth
; dst_z
++) {
1646 struct pipe_surface
*old
;
1647 float dst2src_scale
= srcbox
->depth
/ (float)dstbox
->depth
;
1649 /* Scale Z properly if the blit is scaled.
1651 * When downscaling, we want the coordinates centered, so that
1652 * mipmapping works for 3D textures. For example, when generating
1653 * a 4x4x4 level, this wouldn't average the pixels:
1655 * src Z: 0 1 2 3 4 5 6 7
1658 * Because the pixels are not centered below the pixels of the higher
1659 * level. Therefore, we want this:
1660 * src Z: 0 1 2 3 4 5 6 7
1663 * dst_offset defines the offset needed for centering the pixels and
1664 * it works with any scaling (not just 2x).
1666 float dst_offset
= ((srcbox
->depth
- 1) -
1667 (dstbox
->depth
- 1) * dst2src_scale
) * 0.5;
1668 float src_z
= (dst_z
+ dst_offset
) * dst2src_scale
;
1670 /* Set framebuffer state. */
1671 if (blit_depth
|| blit_stencil
) {
1672 fb_state
.zsbuf
= dst
;
1674 fb_state
.cbufs
[0] = dst
;
1676 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1678 /* See if we need to blit a multisample or singlesample buffer. */
1679 if (src_samples
== dst_samples
&& dst_samples
> 1) {
1681 unsigned i
, max_sample
= dst_samples
- 1;
1683 for (i
= 0; i
<= max_sample
; i
++) {
1684 pipe
->set_sample_mask(pipe
, 1 << i
);
1685 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1687 i
, srcbox
->x
, srcbox
->y
,
1688 srcbox
->x
+ srcbox
->width
,
1689 srcbox
->y
+ srcbox
->height
);
1690 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1691 dstbox
->x
+ dstbox
->width
,
1692 dstbox
->y
+ dstbox
->height
, 0, 1);
1695 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1696 pipe
->set_sample_mask(pipe
, ~0);
1697 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1698 srcbox
->z
+ src_z
, 0,
1699 srcbox
->x
, srcbox
->y
,
1700 srcbox
->x
+ srcbox
->width
,
1701 srcbox
->y
+ srcbox
->height
);
1702 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1703 dstbox
->x
+ dstbox
->width
,
1704 dstbox
->y
+ dstbox
->height
, 0, 1);
1707 /* Get the next surface or (if this is the last iteration)
1708 * just unreference the last one. */
1710 if (dst_z
< dstbox
->depth
-1) {
1711 dst
= ctx
->base
.get_next_surface_layer(ctx
->base
.pipe
, dst
);
1714 pipe_surface_reference(&old
, NULL
);
1719 blitter_restore_vertex_states(ctx
);
1720 blitter_restore_fragment_states(ctx
);
1721 blitter_restore_textures(ctx
);
1722 blitter_restore_fb_state(ctx
);
1724 pipe
->set_scissor_states(pipe
, 0, 1, &ctx
->base
.saved_scissor
);
1726 blitter_restore_render_cond(ctx
);
1727 blitter_unset_running_flag(ctx
);
1731 util_blitter_blit(struct blitter_context
*blitter
,
1732 const struct pipe_blit_info
*info
)
1734 struct pipe_resource
*dst
= info
->dst
.resource
;
1735 struct pipe_resource
*src
= info
->src
.resource
;
1736 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1737 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1738 struct pipe_surface
*dst_view
, dst_templ
;
1739 struct pipe_sampler_view src_templ
, *src_view
;
1741 /* Initialize the surface. */
1742 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
1744 dst_templ
.format
= info
->dst
.format
;
1745 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1747 /* Initialize the sampler view. */
1748 util_blitter_default_src_texture(&src_templ
, src
, info
->src
.level
);
1749 src_templ
.format
= info
->src
.format
;
1750 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1753 util_blitter_blit_generic(blitter
, dst_view
, &info
->dst
.box
,
1754 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
1755 info
->mask
, info
->filter
,
1756 info
->scissor_enable
? &info
->scissor
: NULL
);
1758 pipe_surface_reference(&dst_view
, NULL
);
1759 pipe_sampler_view_reference(&src_view
, NULL
);
1762 /* Clear a region of a color surface to a constant value. */
1763 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
1764 struct pipe_surface
*dstsurf
,
1765 const union pipe_color_union
*color
,
1766 unsigned dstx
, unsigned dsty
,
1767 unsigned width
, unsigned height
)
1769 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1770 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1771 struct pipe_framebuffer_state fb_state
;
1773 assert(dstsurf
->texture
);
1774 if (!dstsurf
->texture
)
1777 /* check the saved state */
1778 blitter_set_running_flag(ctx
);
1779 blitter_check_saved_vertex_states(ctx
);
1780 blitter_check_saved_fragment_states(ctx
);
1781 blitter_check_saved_fb_state(ctx
);
1782 blitter_disable_render_cond(ctx
);
1785 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
]);
1786 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1787 bind_fs_write_one_cbuf(ctx
);
1788 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1790 /* set a framebuffer state */
1791 fb_state
.width
= dstsurf
->width
;
1792 fb_state
.height
= dstsurf
->height
;
1793 fb_state
.nr_cbufs
= 1;
1794 fb_state
.cbufs
[0] = dstsurf
;
1796 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1797 pipe
->set_sample_mask(pipe
, ~0);
1799 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1800 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1801 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
1802 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1804 blitter_restore_vertex_states(ctx
);
1805 blitter_restore_fragment_states(ctx
);
1806 blitter_restore_fb_state(ctx
);
1807 blitter_restore_render_cond(ctx
);
1808 blitter_unset_running_flag(ctx
);
1811 /* Clear a region of a depth stencil surface. */
1812 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
1813 struct pipe_surface
*dstsurf
,
1814 unsigned clear_flags
,
1817 unsigned dstx
, unsigned dsty
,
1818 unsigned width
, unsigned height
)
1820 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1821 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1822 struct pipe_framebuffer_state fb_state
;
1823 struct pipe_stencil_ref sr
= { { 0 } };
1825 assert(dstsurf
->texture
);
1826 if (!dstsurf
->texture
)
1829 /* check the saved state */
1830 blitter_set_running_flag(ctx
);
1831 blitter_check_saved_vertex_states(ctx
);
1832 blitter_check_saved_fragment_states(ctx
);
1833 blitter_check_saved_fb_state(ctx
);
1834 blitter_disable_render_cond(ctx
);
1837 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1838 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1839 sr
.ref_value
[0] = stencil
& 0xff;
1840 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1841 pipe
->set_stencil_ref(pipe
, &sr
);
1843 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
1844 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1846 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
1847 sr
.ref_value
[0] = stencil
& 0xff;
1848 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1849 pipe
->set_stencil_ref(pipe
, &sr
);
1852 /* hmm that should be illegal probably, or make it a no-op somewhere */
1853 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1856 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1858 /* set a framebuffer state */
1859 fb_state
.width
= dstsurf
->width
;
1860 fb_state
.height
= dstsurf
->height
;
1861 fb_state
.nr_cbufs
= 0;
1862 fb_state
.cbufs
[0] = 0;
1863 fb_state
.zsbuf
= dstsurf
;
1864 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1865 pipe
->set_sample_mask(pipe
, ~0);
1867 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1868 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1869 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
,
1871 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
1873 blitter_restore_vertex_states(ctx
);
1874 blitter_restore_fragment_states(ctx
);
1875 blitter_restore_fb_state(ctx
);
1876 blitter_restore_render_cond(ctx
);
1877 blitter_unset_running_flag(ctx
);
1880 /* draw a rectangle across a region using a custom dsa stage - for r600g */
1881 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
1882 struct pipe_surface
*zsurf
,
1883 struct pipe_surface
*cbsurf
,
1884 unsigned sample_mask
,
1885 void *dsa_stage
, float depth
)
1887 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1888 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1889 struct pipe_framebuffer_state fb_state
;
1891 assert(zsurf
->texture
);
1892 if (!zsurf
->texture
)
1895 /* check the saved state */
1896 blitter_set_running_flag(ctx
);
1897 blitter_check_saved_vertex_states(ctx
);
1898 blitter_check_saved_fragment_states(ctx
);
1899 blitter_check_saved_fb_state(ctx
);
1900 blitter_disable_render_cond(ctx
);
1903 pipe
->bind_blend_state(pipe
, cbsurf
? ctx
->blend
[PIPE_MASK_RGBA
] :
1905 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
1907 bind_fs_write_one_cbuf(ctx
);
1910 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1912 /* set a framebuffer state */
1913 fb_state
.width
= zsurf
->width
;
1914 fb_state
.height
= zsurf
->height
;
1915 fb_state
.nr_cbufs
= 1;
1917 fb_state
.cbufs
[0] = cbsurf
;
1918 fb_state
.nr_cbufs
= 1;
1920 fb_state
.cbufs
[0] = NULL
;
1921 fb_state
.nr_cbufs
= 0;
1923 fb_state
.zsbuf
= zsurf
;
1924 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1925 pipe
->set_sample_mask(pipe
, sample_mask
);
1927 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1928 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
1929 blitter
->draw_rectangle(blitter
, 0, 0, zsurf
->width
, zsurf
->height
, depth
,
1930 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
1932 blitter_restore_vertex_states(ctx
);
1933 blitter_restore_fragment_states(ctx
);
1934 blitter_restore_fb_state(ctx
);
1935 blitter_restore_render_cond(ctx
);
1936 blitter_unset_running_flag(ctx
);
1939 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
1940 struct pipe_resource
*dst
,
1942 struct pipe_resource
*src
,
1946 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1947 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1948 struct pipe_vertex_buffer vb
;
1949 struct pipe_stream_output_target
*so_target
;
1950 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
1952 if (srcx
>= src
->width0
||
1953 dstx
>= dst
->width0
) {
1956 if (srcx
+ size
> src
->width0
) {
1957 size
= src
->width0
- srcx
;
1959 if (dstx
+ size
> dst
->width0
) {
1960 size
= dst
->width0
- dstx
;
1963 /* Drivers not capable of Stream Out should not call this function
1964 * in the first place. */
1965 assert(ctx
->has_stream_out
);
1967 /* Some alignment is required. */
1968 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
1969 !ctx
->has_stream_out
) {
1970 struct pipe_box box
;
1971 u_box_1d(srcx
, size
, &box
);
1972 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
1976 blitter_set_running_flag(ctx
);
1977 blitter_check_saved_vertex_states(ctx
);
1978 blitter_disable_render_cond(ctx
);
1981 vb
.buffer_offset
= srcx
;
1984 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1985 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[0]);
1986 bind_vs_pos_only(ctx
);
1987 if (ctx
->has_geometry_shader
)
1988 pipe
->bind_gs_state(pipe
, NULL
);
1989 if (ctx
->has_tessellation
) {
1990 pipe
->bind_tcs_state(pipe
, NULL
);
1991 pipe
->bind_tes_state(pipe
, NULL
);
1993 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
1995 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
1996 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
1998 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2000 blitter_restore_vertex_states(ctx
);
2001 blitter_restore_render_cond(ctx
);
2002 blitter_unset_running_flag(ctx
);
2003 pipe_so_target_reference(&so_target
, NULL
);
2006 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
2007 struct pipe_resource
*dst
,
2008 unsigned offset
, unsigned size
,
2009 unsigned num_channels
,
2010 const union pipe_color_union
*clear_value
)
2012 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2013 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2014 struct pipe_vertex_buffer vb
= {0};
2015 struct pipe_stream_output_target
*so_target
;
2016 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2018 assert(num_channels
>= 1);
2019 assert(num_channels
<= 4);
2021 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
2023 * R600 uses this to initialize texture resources, so width0 might not be
2024 * what you think it is.
2027 /* Streamout is required. */
2028 if (!ctx
->has_stream_out
) {
2029 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2033 /* Some alignment is required. */
2034 if (offset
% 4 != 0 || size
% 4 != 0) {
2035 assert(!"Bad alignment in util_blitter_clear_buffer()");
2039 u_upload_data(ctx
->upload
, 0, num_channels
*4, clear_value
,
2040 &vb
.buffer_offset
, &vb
.buffer
);
2043 blitter_set_running_flag(ctx
);
2044 blitter_check_saved_vertex_states(ctx
);
2045 blitter_disable_render_cond(ctx
);
2047 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2048 pipe
->bind_vertex_elements_state(pipe
,
2049 ctx
->velem_state_readbuf
[num_channels
-1]);
2050 bind_vs_pos_only(ctx
);
2051 if (ctx
->has_geometry_shader
)
2052 pipe
->bind_gs_state(pipe
, NULL
);
2053 if (ctx
->has_tessellation
) {
2054 pipe
->bind_tcs_state(pipe
, NULL
);
2055 pipe
->bind_tes_state(pipe
, NULL
);
2057 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2059 so_target
= pipe
->create_stream_output_target(pipe
, dst
, offset
, size
);
2060 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2062 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2064 blitter_restore_vertex_states(ctx
);
2065 blitter_restore_render_cond(ctx
);
2066 blitter_unset_running_flag(ctx
);
2067 pipe_so_target_reference(&so_target
, NULL
);
2068 pipe_resource_reference(&vb
.buffer
, NULL
);
2071 /* probably radeon specific */
2072 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
2073 struct pipe_resource
*dst
,
2076 struct pipe_resource
*src
,
2078 unsigned sample_mask
,
2080 enum pipe_format format
)
2082 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2083 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2084 struct pipe_framebuffer_state fb_state
;
2085 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
2087 blitter_set_running_flag(ctx
);
2088 blitter_check_saved_vertex_states(ctx
);
2089 blitter_check_saved_fragment_states(ctx
);
2090 blitter_disable_render_cond(ctx
);
2093 pipe
->bind_blend_state(pipe
, custom_blend
);
2094 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2095 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2096 bind_fs_write_one_cbuf(ctx
);
2097 pipe
->set_sample_mask(pipe
, sample_mask
);
2099 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
2100 surf_tmpl
.format
= format
;
2101 surf_tmpl
.u
.tex
.level
= dst_level
;
2102 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
2103 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
2105 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
2107 surf_tmpl
.u
.tex
.level
= 0;
2108 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
2109 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
2111 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
2113 /* set a framebuffer state */
2114 fb_state
.width
= src
->width0
;
2115 fb_state
.height
= src
->height0
;
2116 fb_state
.nr_cbufs
= 2;
2117 fb_state
.cbufs
[0] = srcsurf
;
2118 fb_state
.cbufs
[1] = dstsurf
;
2119 fb_state
.zsbuf
= NULL
;
2120 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2122 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2123 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
2124 blitter
->draw_rectangle(blitter
, 0, 0, src
->width0
, src
->height0
,
2126 blitter_restore_fb_state(ctx
);
2127 blitter_restore_vertex_states(ctx
);
2128 blitter_restore_fragment_states(ctx
);
2129 blitter_restore_render_cond(ctx
);
2130 blitter_unset_running_flag(ctx
);
2132 pipe_surface_reference(&srcsurf
, NULL
);
2133 pipe_surface_reference(&dstsurf
, NULL
);
2136 void util_blitter_custom_color(struct blitter_context
*blitter
,
2137 struct pipe_surface
*dstsurf
,
2140 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2141 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2142 struct pipe_framebuffer_state fb_state
;
2144 assert(dstsurf
->texture
);
2145 if (!dstsurf
->texture
)
2148 /* check the saved state */
2149 blitter_set_running_flag(ctx
);
2150 blitter_check_saved_vertex_states(ctx
);
2151 blitter_check_saved_fragment_states(ctx
);
2152 blitter_check_saved_fb_state(ctx
);
2153 blitter_disable_render_cond(ctx
);
2156 pipe
->bind_blend_state(pipe
, custom_blend
? custom_blend
2157 : ctx
->blend
[PIPE_MASK_RGBA
]);
2158 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2159 bind_fs_write_one_cbuf(ctx
);
2160 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2161 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2163 /* set a framebuffer state */
2164 fb_state
.width
= dstsurf
->width
;
2165 fb_state
.height
= dstsurf
->height
;
2166 fb_state
.nr_cbufs
= 1;
2167 fb_state
.cbufs
[0] = dstsurf
;
2169 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2170 pipe
->set_sample_mask(pipe
, ~0);
2172 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2173 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2174 blitter
->draw_rectangle(blitter
, 0, 0, dstsurf
->width
, dstsurf
->height
,
2177 blitter_restore_vertex_states(ctx
);
2178 blitter_restore_fragment_states(ctx
);
2179 blitter_restore_fb_state(ctx
);
2180 blitter_restore_render_cond(ctx
);
2181 blitter_unset_running_flag(ctx
);