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
[4]; /**< 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
[3][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
[3][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];
101 void *blend
[PIPE_MASK_RGBA
+1][2]; /**< blend state with writemask */
102 void *blend_clear
[GET_CLEAR_BLEND_STATE_IDX(PIPE_CLEAR_COLOR
)+1];
104 /* Depth stencil alpha state. */
105 void *dsa_write_depth_stencil
;
106 void *dsa_write_depth_keep_stencil
;
107 void *dsa_keep_depth_stencil
;
108 void *dsa_keep_depth_write_stencil
;
110 /* Vertex elements states. */
112 void *velem_state_readbuf
[4]; /**< X, XY, XYZ, XYZW */
116 void *sampler_state_linear
;
117 void *sampler_state_rect
;
118 void *sampler_state_rect_linear
;
120 /* Rasterizer state. */
121 void *rs_state
, *rs_state_scissor
, *rs_discard_state
;
123 /* Viewport state. */
124 struct pipe_viewport_state viewport
;
126 /* Destination surface dimensions. */
130 boolean has_geometry_shader
;
131 boolean has_tessellation
;
133 boolean has_stream_out
;
134 boolean has_stencil_export
;
135 boolean has_texture_multisample
;
136 boolean cached_all_shaders
;
138 /* The Draw module overrides these functions.
139 * Always create the blitter before Draw. */
140 void (*bind_fs_state
)(struct pipe_context
*, void *);
141 void (*delete_fs_state
)(struct pipe_context
*, void *);
144 static struct pipe_surface
*
145 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
146 struct pipe_surface
*surf
);
148 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
)
150 struct blitter_context_priv
*ctx
;
151 struct pipe_blend_state blend
;
152 struct pipe_depth_stencil_alpha_state dsa
;
153 struct pipe_rasterizer_state rs_state
;
154 struct pipe_sampler_state sampler_state
;
155 struct pipe_vertex_element velem
[2];
158 ctx
= CALLOC_STRUCT(blitter_context_priv
);
162 ctx
->base
.pipe
= pipe
;
163 ctx
->base
.draw_rectangle
= util_blitter_draw_rectangle
;
164 ctx
->base
.get_next_surface_layer
= util_blitter_get_next_surface_layer
;
166 ctx
->bind_fs_state
= pipe
->bind_fs_state
;
167 ctx
->delete_fs_state
= pipe
->delete_fs_state
;
169 /* init state objects for them to be considered invalid */
170 ctx
->base
.saved_blend_state
= INVALID_PTR
;
171 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
172 ctx
->base
.saved_rs_state
= INVALID_PTR
;
173 ctx
->base
.saved_fs
= INVALID_PTR
;
174 ctx
->base
.saved_vs
= INVALID_PTR
;
175 ctx
->base
.saved_gs
= INVALID_PTR
;
176 ctx
->base
.saved_velem_state
= INVALID_PTR
;
177 ctx
->base
.saved_fb_state
.nr_cbufs
= ~0;
178 ctx
->base
.saved_num_sampler_views
= ~0;
179 ctx
->base
.saved_num_sampler_states
= ~0;
180 ctx
->base
.saved_num_so_targets
= ~0;
182 ctx
->has_geometry_shader
=
183 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
184 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
186 ctx
->has_tessellation
=
187 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_TESS_CTRL
,
188 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
190 ctx
->has_stream_out
=
191 pipe
->screen
->get_param(pipe
->screen
,
192 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0;
194 ctx
->has_stencil_export
=
195 pipe
->screen
->get_param(pipe
->screen
,
196 PIPE_CAP_SHADER_STENCIL_EXPORT
);
198 ctx
->has_texture_multisample
=
199 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TEXTURE_MULTISAMPLE
);
201 /* blend state objects */
202 memset(&blend
, 0, sizeof(blend
));
204 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
205 for (j
= 0; j
< 2; j
++) {
206 memset(&blend
.rt
[0], 0, sizeof(blend
.rt
[0]));
207 blend
.rt
[0].colormask
= i
;
209 blend
.rt
[0].blend_enable
= 1;
210 blend
.rt
[0].rgb_func
= PIPE_BLEND_ADD
;
211 blend
.rt
[0].rgb_src_factor
= PIPE_BLENDFACTOR_SRC_ALPHA
;
212 blend
.rt
[0].rgb_dst_factor
= PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
213 blend
.rt
[0].alpha_func
= PIPE_BLEND_ADD
;
214 blend
.rt
[0].alpha_src_factor
= PIPE_BLENDFACTOR_SRC_ALPHA
;
215 blend
.rt
[0].alpha_dst_factor
= PIPE_BLENDFACTOR_INV_SRC_ALPHA
;
217 ctx
->blend
[i
][j
] = pipe
->create_blend_state(pipe
, &blend
);
221 /* depth stencil alpha state objects */
222 memset(&dsa
, 0, sizeof(dsa
));
223 ctx
->dsa_keep_depth_stencil
=
224 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
226 dsa
.depth
.enabled
= 1;
227 dsa
.depth
.writemask
= 1;
228 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
229 ctx
->dsa_write_depth_keep_stencil
=
230 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
232 dsa
.stencil
[0].enabled
= 1;
233 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
234 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
235 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
236 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
237 dsa
.stencil
[0].valuemask
= 0xff;
238 dsa
.stencil
[0].writemask
= 0xff;
239 ctx
->dsa_write_depth_stencil
=
240 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
242 dsa
.depth
.enabled
= 0;
243 dsa
.depth
.writemask
= 0;
244 ctx
->dsa_keep_depth_write_stencil
=
245 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
248 memset(&sampler_state
, 0, sizeof(sampler_state
));
249 sampler_state
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
250 sampler_state
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
251 sampler_state
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
252 sampler_state
.normalized_coords
= 1;
253 ctx
->sampler_state
= pipe
->create_sampler_state(pipe
, &sampler_state
);
254 sampler_state
.normalized_coords
= 0;
255 ctx
->sampler_state_rect
= pipe
->create_sampler_state(pipe
, &sampler_state
);
257 sampler_state
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
258 sampler_state
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
259 sampler_state
.normalized_coords
= 1;
260 ctx
->sampler_state_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
261 sampler_state
.normalized_coords
= 0;
262 ctx
->sampler_state_rect_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
264 /* rasterizer state */
265 memset(&rs_state
, 0, sizeof(rs_state
));
266 rs_state
.cull_face
= PIPE_FACE_NONE
;
267 rs_state
.half_pixel_center
= 1;
268 rs_state
.bottom_edge_rule
= 1;
269 rs_state
.flatshade
= 1;
270 rs_state
.depth_clip
= 1;
271 ctx
->rs_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
273 rs_state
.scissor
= 1;
274 ctx
->rs_state_scissor
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
276 if (ctx
->has_stream_out
) {
277 rs_state
.scissor
= 0;
278 rs_state
.rasterizer_discard
= 1;
279 ctx
->rs_discard_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
282 ctx
->base
.cb_slot
= 0; /* 0 for now */
283 ctx
->base
.vb_slot
= 0; /* 0 for now */
285 /* vertex elements states */
286 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
287 for (i
= 0; i
< 2; i
++) {
288 velem
[i
].src_offset
= i
* 4 * sizeof(float);
289 velem
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
290 velem
[i
].vertex_buffer_index
= ctx
->base
.vb_slot
;
292 ctx
->velem_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
294 if (ctx
->has_stream_out
) {
295 static enum pipe_format formats
[4] = {
296 PIPE_FORMAT_R32_UINT
,
297 PIPE_FORMAT_R32G32_UINT
,
298 PIPE_FORMAT_R32G32B32_UINT
,
299 PIPE_FORMAT_R32G32B32A32_UINT
302 for (i
= 0; i
< 4; i
++) {
303 velem
[0].src_format
= formats
[i
];
304 velem
[0].vertex_buffer_index
= ctx
->base
.vb_slot
;
305 ctx
->velem_state_readbuf
[i
] =
306 pipe
->create_vertex_elements_state(pipe
, 1, &velem
[0]);
311 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_INSTANCEID
) &&
312 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT
);
314 /* set invariant vertex coordinates */
315 for (i
= 0; i
< 4; i
++)
316 ctx
->vertices
[i
][0][3] = 1; /*v.w*/
318 ctx
->upload
= u_upload_create(pipe
, 65536, PIPE_BIND_VERTEX_BUFFER
,
324 static void bind_vs_pos_only(struct blitter_context_priv
*ctx
,
325 unsigned num_so_channels
)
327 struct pipe_context
*pipe
= ctx
->base
.pipe
;
328 int index
= num_so_channels
? num_so_channels
- 1 : 0;
330 if (!ctx
->vs_pos_only
[index
]) {
331 struct pipe_stream_output_info so
;
332 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
};
333 const uint semantic_indices
[] = { 0 };
335 memset(&so
, 0, sizeof(so
));
337 so
.output
[0].num_components
= num_so_channels
;
338 so
.stride
[0] = num_so_channels
;
340 ctx
->vs_pos_only
[index
] =
341 util_make_vertex_passthrough_shader_with_so(pipe
, 1, semantic_names
,
342 semantic_indices
, FALSE
,
346 pipe
->bind_vs_state(pipe
, ctx
->vs_pos_only
[index
]);
349 static void bind_vs_passthrough(struct blitter_context_priv
*ctx
)
351 struct pipe_context
*pipe
= ctx
->base
.pipe
;
354 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
355 TGSI_SEMANTIC_GENERIC
};
356 const uint semantic_indices
[] = { 0, 0 };
358 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
359 semantic_indices
, FALSE
);
362 pipe
->bind_vs_state(pipe
, ctx
->vs
);
365 static void bind_vs_layered(struct blitter_context_priv
*ctx
)
367 struct pipe_context
*pipe
= ctx
->base
.pipe
;
369 if (!ctx
->vs_layered
) {
370 ctx
->vs_layered
= util_make_layered_clear_vertex_shader(pipe
);
373 pipe
->bind_vs_state(pipe
, ctx
->vs_layered
);
376 static void bind_fs_empty(struct blitter_context_priv
*ctx
)
378 struct pipe_context
*pipe
= ctx
->base
.pipe
;
380 if (!ctx
->fs_empty
) {
381 assert(!ctx
->cached_all_shaders
);
382 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
385 ctx
->bind_fs_state(pipe
, ctx
->fs_empty
);
388 static void bind_fs_write_one_cbuf(struct blitter_context_priv
*ctx
)
390 struct pipe_context
*pipe
= ctx
->base
.pipe
;
392 if (!ctx
->fs_write_one_cbuf
) {
393 assert(!ctx
->cached_all_shaders
);
394 ctx
->fs_write_one_cbuf
=
395 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
396 TGSI_INTERPOLATE_CONSTANT
, FALSE
);
399 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
402 static void bind_fs_write_all_cbufs(struct blitter_context_priv
*ctx
)
404 struct pipe_context
*pipe
= ctx
->base
.pipe
;
406 if (!ctx
->fs_write_all_cbufs
) {
407 assert(!ctx
->cached_all_shaders
);
408 ctx
->fs_write_all_cbufs
=
409 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
410 TGSI_INTERPOLATE_CONSTANT
, TRUE
);
413 ctx
->bind_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
416 void util_blitter_destroy(struct blitter_context
*blitter
)
418 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
419 struct pipe_context
*pipe
= blitter
->pipe
;
422 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++)
423 for (j
= 0; j
< 2; j
++)
424 pipe
->delete_blend_state(pipe
, ctx
->blend
[i
][j
]);
426 for (i
= 0; i
< ARRAY_SIZE(ctx
->blend_clear
); i
++) {
427 if (ctx
->blend_clear
[i
])
428 pipe
->delete_blend_state(pipe
, ctx
->blend_clear
[i
]);
430 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
431 pipe
->delete_depth_stencil_alpha_state(pipe
,
432 ctx
->dsa_write_depth_keep_stencil
);
433 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
434 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
436 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
);
437 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state_scissor
);
438 if (ctx
->rs_discard_state
)
439 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_discard_state
);
441 pipe
->delete_vs_state(pipe
, ctx
->vs
);
442 for (i
= 0; i
< 4; i
++)
443 if (ctx
->vs_pos_only
[i
])
444 pipe
->delete_vs_state(pipe
, ctx
->vs_pos_only
[i
]);
446 pipe
->delete_vs_state(pipe
, ctx
->vs_layered
);
447 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state
);
448 for (i
= 0; i
< 4; i
++) {
449 if (ctx
->velem_state_readbuf
[i
]) {
450 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[i
]);
454 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
455 for (unsigned type
= 0; type
< 3; ++type
) {
456 if (ctx
->fs_texfetch_col
[type
][i
])
457 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[type
][i
]);
458 if (ctx
->fs_texfetch_col_msaa
[type
][i
])
459 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col_msaa
[type
][i
]);
462 if (ctx
->fs_texfetch_depth
[i
])
463 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
]);
464 if (ctx
->fs_texfetch_depthstencil
[i
])
465 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil
[i
]);
466 if (ctx
->fs_texfetch_stencil
[i
])
467 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil
[i
]);
469 if (ctx
->fs_texfetch_depth_msaa
[i
])
470 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth_msaa
[i
]);
471 if (ctx
->fs_texfetch_depthstencil_msaa
[i
])
472 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil_msaa
[i
]);
473 if (ctx
->fs_texfetch_stencil_msaa
[i
])
474 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil_msaa
[i
]);
476 for (j
= 0; j
< ARRAY_SIZE(ctx
->fs_resolve
[i
]); j
++)
477 for (f
= 0; f
< 2; f
++)
478 if (ctx
->fs_resolve
[i
][j
][f
])
479 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve
[i
][j
][f
]);
483 ctx
->delete_fs_state(pipe
, ctx
->fs_empty
);
484 if (ctx
->fs_write_one_cbuf
)
485 ctx
->delete_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
486 if (ctx
->fs_write_all_cbufs
)
487 ctx
->delete_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
489 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect_linear
);
490 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect
);
491 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_linear
);
492 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
);
493 u_upload_destroy(ctx
->upload
);
497 void util_blitter_set_texture_multisample(struct blitter_context
*blitter
,
500 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
502 ctx
->has_texture_multisample
= supported
;
505 void util_blitter_set_running_flag(struct blitter_context
*blitter
)
507 if (blitter
->running
) {
508 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
511 blitter
->running
= TRUE
;
513 blitter
->pipe
->set_active_query_state(blitter
->pipe
, false);
516 void util_blitter_unset_running_flag(struct blitter_context
*blitter
)
518 if (!blitter
->running
) {
519 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
522 blitter
->running
= FALSE
;
524 blitter
->pipe
->set_active_query_state(blitter
->pipe
, true);
527 static void blitter_check_saved_vertex_states(struct blitter_context_priv
*ctx
)
529 assert(ctx
->base
.saved_velem_state
!= INVALID_PTR
);
530 assert(ctx
->base
.saved_vs
!= INVALID_PTR
);
531 assert(!ctx
->has_geometry_shader
|| ctx
->base
.saved_gs
!= INVALID_PTR
);
532 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tcs
!= INVALID_PTR
);
533 assert(!ctx
->has_tessellation
|| ctx
->base
.saved_tes
!= INVALID_PTR
);
534 assert(!ctx
->has_stream_out
|| ctx
->base
.saved_num_so_targets
!= ~0u);
535 assert(ctx
->base
.saved_rs_state
!= INVALID_PTR
);
538 void util_blitter_restore_vertex_states(struct blitter_context
*blitter
)
540 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
541 struct pipe_context
*pipe
= ctx
->base
.pipe
;
545 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1,
546 &ctx
->base
.saved_vertex_buffer
);
547 pipe_resource_reference(&ctx
->base
.saved_vertex_buffer
.buffer
, NULL
);
549 /* Vertex elements. */
550 pipe
->bind_vertex_elements_state(pipe
, ctx
->base
.saved_velem_state
);
551 ctx
->base
.saved_velem_state
= INVALID_PTR
;
554 pipe
->bind_vs_state(pipe
, ctx
->base
.saved_vs
);
555 ctx
->base
.saved_vs
= INVALID_PTR
;
557 /* Geometry shader. */
558 if (ctx
->has_geometry_shader
) {
559 pipe
->bind_gs_state(pipe
, ctx
->base
.saved_gs
);
560 ctx
->base
.saved_gs
= INVALID_PTR
;
563 if (ctx
->has_tessellation
) {
564 pipe
->bind_tcs_state(pipe
, ctx
->base
.saved_tcs
);
565 pipe
->bind_tes_state(pipe
, ctx
->base
.saved_tes
);
566 ctx
->base
.saved_tcs
= INVALID_PTR
;
567 ctx
->base
.saved_tes
= INVALID_PTR
;
570 /* Stream outputs. */
571 if (ctx
->has_stream_out
) {
572 unsigned offsets
[PIPE_MAX_SO_BUFFERS
];
573 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
574 offsets
[i
] = (unsigned)-1;
575 pipe
->set_stream_output_targets(pipe
,
576 ctx
->base
.saved_num_so_targets
,
577 ctx
->base
.saved_so_targets
, offsets
);
579 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
580 pipe_so_target_reference(&ctx
->base
.saved_so_targets
[i
], NULL
);
582 ctx
->base
.saved_num_so_targets
= ~0;
586 pipe
->bind_rasterizer_state(pipe
, ctx
->base
.saved_rs_state
);
587 ctx
->base
.saved_rs_state
= INVALID_PTR
;
590 static void blitter_check_saved_fragment_states(struct blitter_context_priv
*ctx
)
592 assert(ctx
->base
.saved_fs
!= INVALID_PTR
);
593 assert(ctx
->base
.saved_dsa_state
!= INVALID_PTR
);
594 assert(ctx
->base
.saved_blend_state
!= INVALID_PTR
);
597 void util_blitter_restore_fragment_states(struct blitter_context
*blitter
)
599 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
600 struct pipe_context
*pipe
= ctx
->base
.pipe
;
602 /* Fragment shader. */
603 ctx
->bind_fs_state(pipe
, ctx
->base
.saved_fs
);
604 ctx
->base
.saved_fs
= INVALID_PTR
;
606 /* Depth, stencil, alpha. */
607 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->base
.saved_dsa_state
);
608 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
611 pipe
->bind_blend_state(pipe
, ctx
->base
.saved_blend_state
);
612 ctx
->base
.saved_blend_state
= INVALID_PTR
;
615 if (ctx
->base
.is_sample_mask_saved
) {
616 pipe
->set_sample_mask(pipe
, ctx
->base
.saved_sample_mask
);
617 ctx
->base
.is_sample_mask_saved
= FALSE
;
620 /* Miscellaneous states. */
621 /* XXX check whether these are saved and whether they need to be restored
622 * (depending on the operation) */
623 pipe
->set_stencil_ref(pipe
, &ctx
->base
.saved_stencil_ref
);
624 pipe
->set_viewport_states(pipe
, 0, 1, &ctx
->base
.saved_viewport
);
627 static void blitter_check_saved_fb_state(struct blitter_context_priv
*ctx
)
629 assert(ctx
->base
.saved_fb_state
.nr_cbufs
!= ~0u);
632 static void blitter_disable_render_cond(struct blitter_context_priv
*ctx
)
634 struct pipe_context
*pipe
= ctx
->base
.pipe
;
636 if (ctx
->base
.saved_render_cond_query
) {
637 pipe
->render_condition(pipe
, NULL
, FALSE
, 0);
641 void util_blitter_restore_render_cond(struct blitter_context
*blitter
)
643 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
644 struct pipe_context
*pipe
= ctx
->base
.pipe
;
646 if (ctx
->base
.saved_render_cond_query
) {
647 pipe
->render_condition(pipe
, ctx
->base
.saved_render_cond_query
,
648 ctx
->base
.saved_render_cond_cond
,
649 ctx
->base
.saved_render_cond_mode
);
650 ctx
->base
.saved_render_cond_query
= NULL
;
654 void util_blitter_restore_fb_state(struct blitter_context
*blitter
)
656 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
657 struct pipe_context
*pipe
= ctx
->base
.pipe
;
659 pipe
->set_framebuffer_state(pipe
, &ctx
->base
.saved_fb_state
);
660 util_unreference_framebuffer_state(&ctx
->base
.saved_fb_state
);
663 static void blitter_check_saved_textures(struct blitter_context_priv
*ctx
)
665 assert(ctx
->base
.saved_num_sampler_states
!= ~0u);
666 assert(ctx
->base
.saved_num_sampler_views
!= ~0u);
669 void util_blitter_restore_textures(struct blitter_context
*blitter
)
671 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
672 struct pipe_context
*pipe
= ctx
->base
.pipe
;
675 /* Fragment sampler states. */
676 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0,
677 ctx
->base
.saved_num_sampler_states
,
678 ctx
->base
.saved_sampler_states
);
680 ctx
->base
.saved_num_sampler_states
= ~0;
682 /* Fragment sampler views. */
683 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
684 ctx
->base
.saved_num_sampler_views
,
685 ctx
->base
.saved_sampler_views
);
687 for (i
= 0; i
< ctx
->base
.saved_num_sampler_views
; i
++)
688 pipe_sampler_view_reference(&ctx
->base
.saved_sampler_views
[i
], NULL
);
690 ctx
->base
.saved_num_sampler_views
= ~0;
693 void util_blitter_restore_constant_buffer_state(struct blitter_context
*blitter
)
695 struct pipe_context
*pipe
= blitter
->pipe
;
697 pipe
->set_constant_buffer(pipe
, PIPE_SHADER_FRAGMENT
, blitter
->cb_slot
,
698 &blitter
->saved_fs_constant_buffer
);
699 pipe_resource_reference(&blitter
->saved_fs_constant_buffer
.buffer
, NULL
);
702 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
703 int x1
, int y1
, int x2
, int y2
,
708 /* set vertex positions */
709 ctx
->vertices
[0][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v0.x*/
710 ctx
->vertices
[0][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v0.y*/
712 ctx
->vertices
[1][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v1.x*/
713 ctx
->vertices
[1][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v1.y*/
715 ctx
->vertices
[2][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v2.x*/
716 ctx
->vertices
[2][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v2.y*/
718 ctx
->vertices
[3][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v3.x*/
719 ctx
->vertices
[3][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v3.y*/
721 for (i
= 0; i
< 4; i
++)
722 ctx
->vertices
[i
][0][2] = depth
; /*z*/
725 ctx
->viewport
.scale
[0] = 0.5f
* ctx
->dst_width
;
726 ctx
->viewport
.scale
[1] = 0.5f
* ctx
->dst_height
;
727 ctx
->viewport
.scale
[2] = 1.0f
;
728 ctx
->viewport
.translate
[0] = 0.5f
* ctx
->dst_width
;
729 ctx
->viewport
.translate
[1] = 0.5f
* ctx
->dst_height
;
730 ctx
->viewport
.translate
[2] = 0.0f
;
731 ctx
->base
.pipe
->set_viewport_states(ctx
->base
.pipe
, 0, 1, &ctx
->viewport
);
734 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
735 const union pipe_color_union
*color
)
740 for (i
= 0; i
< 4; i
++) {
741 uint32_t *uiverts
= (uint32_t *)ctx
->vertices
[i
][1];
742 uiverts
[0] = color
->ui
[0];
743 uiverts
[1] = color
->ui
[1];
744 uiverts
[2] = color
->ui
[2];
745 uiverts
[3] = color
->ui
[3];
748 for (i
= 0; i
< 4; i
++) {
749 ctx
->vertices
[i
][1][0] = 0;
750 ctx
->vertices
[i
][1][1] = 0;
751 ctx
->vertices
[i
][1][2] = 0;
752 ctx
->vertices
[i
][1][3] = 0;
757 static void get_texcoords(struct pipe_sampler_view
*src
,
758 unsigned src_width0
, unsigned src_height0
,
759 int x1
, int y1
, int x2
, int y2
,
762 struct pipe_resource
*tex
= src
->texture
;
763 unsigned level
= src
->u
.tex
.first_level
;
764 boolean normalized
= tex
->target
!= PIPE_TEXTURE_RECT
&&
765 tex
->nr_samples
<= 1;
768 out
[0] = x1
/ (float)u_minify(src_width0
, level
);
769 out
[1] = y1
/ (float)u_minify(src_height0
, level
);
770 out
[2] = x2
/ (float)u_minify(src_width0
, level
);
771 out
[3] = y2
/ (float)u_minify(src_height0
, level
);
780 static void set_texcoords_in_vertices(const float coord
[4],
781 float *out
, unsigned stride
)
783 out
[0] = coord
[0]; /*t0.s*/
784 out
[1] = coord
[1]; /*t0.t*/
786 out
[0] = coord
[2]; /*t1.s*/
787 out
[1] = coord
[1]; /*t1.t*/
789 out
[0] = coord
[2]; /*t2.s*/
790 out
[1] = coord
[3]; /*t2.t*/
792 out
[0] = coord
[0]; /*t3.s*/
793 out
[1] = coord
[3]; /*t3.t*/
796 static void blitter_set_texcoords(struct blitter_context_priv
*ctx
,
797 struct pipe_sampler_view
*src
,
798 unsigned src_width0
, unsigned src_height0
,
799 float layer
, unsigned sample
,
800 int x1
, int y1
, int x2
, int y2
)
804 float face_coord
[4][2];
806 get_texcoords(src
, src_width0
, src_height0
, x1
, y1
, x2
, y2
, coord
);
808 if (src
->texture
->target
== PIPE_TEXTURE_CUBE
||
809 src
->texture
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
810 set_texcoords_in_vertices(coord
, &face_coord
[0][0], 2);
811 util_map_texcoords2d_onto_cubemap((unsigned)layer
% 6,
812 /* pointer, stride in floats */
813 &face_coord
[0][0], 2,
814 &ctx
->vertices
[0][1][0], 8,
817 set_texcoords_in_vertices(coord
, &ctx
->vertices
[0][1][0], 8);
821 switch (src
->texture
->target
) {
822 case PIPE_TEXTURE_3D
:
824 float r
= layer
/ (float)u_minify(src
->texture
->depth0
,
825 src
->u
.tex
.first_level
);
826 for (i
= 0; i
< 4; i
++)
827 ctx
->vertices
[i
][1][2] = r
; /*r*/
831 case PIPE_TEXTURE_1D_ARRAY
:
832 for (i
= 0; i
< 4; i
++)
833 ctx
->vertices
[i
][1][1] = (float) layer
; /*t*/
836 case PIPE_TEXTURE_2D_ARRAY
:
837 for (i
= 0; i
< 4; i
++) {
838 ctx
->vertices
[i
][1][2] = (float) layer
; /*r*/
839 ctx
->vertices
[i
][1][3] = (float) sample
; /*q*/
843 case PIPE_TEXTURE_CUBE_ARRAY
:
844 for (i
= 0; i
< 4; i
++)
845 ctx
->vertices
[i
][1][3] = (float) ((unsigned)layer
/ 6); /*w*/
848 case PIPE_TEXTURE_2D
:
849 for (i
= 0; i
< 4; i
++) {
850 ctx
->vertices
[i
][1][3] = (float) sample
; /*r*/
858 static void blitter_set_dst_dimensions(struct blitter_context_priv
*ctx
,
859 unsigned width
, unsigned height
)
861 ctx
->dst_width
= width
;
862 ctx
->dst_height
= height
;
865 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
866 enum pipe_format format
,
867 enum pipe_texture_target target
,
868 unsigned src_nr_samples
,
869 unsigned dst_nr_samples
,
872 struct pipe_context
*pipe
= ctx
->base
.pipe
;
873 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, src_nr_samples
);
874 enum tgsi_return_type stype
;
877 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
879 if (util_format_is_pure_uint(format
)) {
880 stype
= TGSI_RETURN_TYPE_UINT
;
882 } else if (util_format_is_pure_sint(format
)) {
883 stype
= TGSI_RETURN_TYPE_SINT
;
886 stype
= TGSI_RETURN_TYPE_FLOAT
;
890 if (src_nr_samples
> 1) {
893 /* OpenGL requires that integer textures just copy 1 sample instead
896 if (dst_nr_samples
<= 1 &&
897 stype
!= TGSI_RETURN_TYPE_UINT
&&
898 stype
!= TGSI_RETURN_TYPE_SINT
) {
899 /* The destination has one sample, so we'll do color resolve. */
900 unsigned index
= GET_MSAA_RESOLVE_FS_IDX(src_nr_samples
);
904 shader
= &ctx
->fs_resolve
[target
][index
][filter
];
907 assert(!ctx
->cached_all_shaders
);
908 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
909 *shader
= util_make_fs_msaa_resolve_bilinear(pipe
, tgsi_tex
,
914 *shader
= util_make_fs_msaa_resolve(pipe
, tgsi_tex
,
921 /* The destination has multiple samples, we'll do
922 * an MSAA->MSAA copy.
924 shader
= &ctx
->fs_texfetch_col_msaa
[type
][target
];
926 /* Create the fragment shader on-demand. */
928 assert(!ctx
->cached_all_shaders
);
929 *shader
= util_make_fs_blit_msaa_color(pipe
, tgsi_tex
, stype
);
935 void **shader
= &ctx
->fs_texfetch_col
[type
][target
];
937 /* Create the fragment shader on-demand. */
939 assert(!ctx
->cached_all_shaders
);
940 *shader
= util_make_fragment_tex_shader(pipe
, tgsi_tex
,
941 TGSI_INTERPOLATE_LINEAR
,
950 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
951 enum pipe_texture_target target
,
954 struct pipe_context
*pipe
= ctx
->base
.pipe
;
956 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
958 if (nr_samples
> 1) {
959 void **shader
= &ctx
->fs_texfetch_depth_msaa
[target
];
961 /* Create the fragment shader on-demand. */
964 assert(!ctx
->cached_all_shaders
);
965 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
966 *shader
= util_make_fs_blit_msaa_depth(pipe
, tgsi_tex
);
971 void **shader
= &ctx
->fs_texfetch_depth
[target
];
973 /* Create the fragment shader on-demand. */
976 assert(!ctx
->cached_all_shaders
);
977 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
979 util_make_fragment_tex_shader_writedepth(pipe
, tgsi_tex
,
980 TGSI_INTERPOLATE_LINEAR
);
988 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv
*ctx
,
989 enum pipe_texture_target target
,
992 struct pipe_context
*pipe
= ctx
->base
.pipe
;
994 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
996 if (nr_samples
> 1) {
997 void **shader
= &ctx
->fs_texfetch_depthstencil_msaa
[target
];
999 /* Create the fragment shader on-demand. */
1002 assert(!ctx
->cached_all_shaders
);
1003 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1004 *shader
= util_make_fs_blit_msaa_depthstencil(pipe
, tgsi_tex
);
1009 void **shader
= &ctx
->fs_texfetch_depthstencil
[target
];
1011 /* Create the fragment shader on-demand. */
1014 assert(!ctx
->cached_all_shaders
);
1015 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1017 util_make_fragment_tex_shader_writedepthstencil(pipe
, tgsi_tex
,
1018 TGSI_INTERPOLATE_LINEAR
);
1026 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv
*ctx
,
1027 enum pipe_texture_target target
,
1028 unsigned nr_samples
)
1030 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1032 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
1034 if (nr_samples
> 1) {
1035 void **shader
= &ctx
->fs_texfetch_stencil_msaa
[target
];
1037 /* Create the fragment shader on-demand. */
1040 assert(!ctx
->cached_all_shaders
);
1041 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
1042 *shader
= util_make_fs_blit_msaa_stencil(pipe
, tgsi_tex
);
1047 void **shader
= &ctx
->fs_texfetch_stencil
[target
];
1049 /* Create the fragment shader on-demand. */
1052 assert(!ctx
->cached_all_shaders
);
1053 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1055 util_make_fragment_tex_shader_writestencil(pipe
, tgsi_tex
,
1056 TGSI_INTERPOLATE_LINEAR
);
1065 * Generate and save all fragment shaders that we will ever need for
1066 * blitting. Drivers which use the 'draw' fallbacks will typically use
1067 * this to make sure we generate/use shaders that don't go through the
1068 * draw module's wrapper functions.
1070 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
)
1072 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1073 struct pipe_context
*pipe
= blitter
->pipe
;
1074 struct pipe_screen
*screen
= pipe
->screen
;
1075 unsigned samples
, j
, f
, target
, max_samples
;
1076 boolean has_arraytex
, has_cubearraytex
;
1078 max_samples
= ctx
->has_texture_multisample
? 2 : 1;
1079 has_arraytex
= screen
->get_param(screen
,
1080 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
) != 0;
1081 has_cubearraytex
= screen
->get_param(screen
,
1082 PIPE_CAP_CUBE_MAP_ARRAY
) != 0;
1084 /* It only matters if i <= 1 or > 1. */
1085 for (samples
= 1; samples
<= max_samples
; samples
++) {
1086 for (target
= PIPE_TEXTURE_1D
; target
< PIPE_MAX_TEXTURE_TYPES
; target
++) {
1087 if (!has_arraytex
&&
1088 (target
== PIPE_TEXTURE_1D_ARRAY
||
1089 target
== PIPE_TEXTURE_2D_ARRAY
)) {
1092 if (!has_cubearraytex
&&
1093 (target
== PIPE_TEXTURE_CUBE_ARRAY
))
1097 (target
!= PIPE_TEXTURE_2D
&&
1098 target
!= PIPE_TEXTURE_2D_ARRAY
))
1101 /* If samples == 1, the shaders read one texel. If samples >= 1,
1102 * they read one sample.
1104 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
, target
,
1105 samples
, samples
, 0);
1106 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
, target
,
1107 samples
, samples
, 0);
1108 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
, target
,
1109 samples
, samples
, 0);
1110 blitter_get_fs_texfetch_depth(ctx
, target
, samples
);
1111 if (ctx
->has_stencil_export
) {
1112 blitter_get_fs_texfetch_depthstencil(ctx
, target
, samples
);
1113 blitter_get_fs_texfetch_stencil(ctx
, target
, samples
);
1119 /* MSAA resolve shaders. */
1120 for (j
= 2; j
< 32; j
++) {
1121 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R32_FLOAT
,
1123 PIPE_BIND_SAMPLER_VIEW
)) {
1127 for (f
= 0; f
< 2; f
++) {
1128 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
, target
,
1130 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
, target
,
1132 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
, target
,
1139 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
1141 ctx
->fs_write_one_cbuf
=
1142 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1143 TGSI_INTERPOLATE_CONSTANT
, FALSE
);
1145 ctx
->fs_write_all_cbufs
=
1146 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1147 TGSI_INTERPOLATE_CONSTANT
, TRUE
);
1149 ctx
->cached_all_shaders
= TRUE
;
1152 static void blitter_set_common_draw_rect_state(struct blitter_context_priv
*ctx
,
1156 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1158 pipe
->bind_rasterizer_state(pipe
, scissor
? ctx
->rs_state_scissor
1161 bind_vs_layered(ctx
);
1163 bind_vs_passthrough(ctx
);
1165 if (ctx
->has_geometry_shader
)
1166 pipe
->bind_gs_state(pipe
, NULL
);
1167 if (ctx
->has_tessellation
) {
1168 pipe
->bind_tcs_state(pipe
, NULL
);
1169 pipe
->bind_tes_state(pipe
, NULL
);
1171 if (ctx
->has_stream_out
)
1172 pipe
->set_stream_output_targets(pipe
, 0, NULL
, NULL
);
1175 static void blitter_draw(struct blitter_context_priv
*ctx
,
1176 int x1
, int y1
, int x2
, int y2
, float depth
,
1177 unsigned num_instances
)
1179 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1180 struct pipe_vertex_buffer vb
= {0};
1182 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
1184 vb
.stride
= 8 * sizeof(float);
1186 u_upload_data(ctx
->upload
, 0, sizeof(ctx
->vertices
), 4, ctx
->vertices
,
1187 &vb
.buffer_offset
, &vb
.buffer
);
1190 u_upload_unmap(ctx
->upload
);
1192 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1193 util_draw_arrays_instanced(pipe
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
1195 pipe_resource_reference(&vb
.buffer
, NULL
);
1198 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
1199 int x1
, int y1
, int x2
, int y2
, float depth
,
1200 enum blitter_attrib_type type
,
1201 const union pipe_color_union
*attrib
)
1203 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1206 case UTIL_BLITTER_ATTRIB_COLOR
:
1207 blitter_set_clear_color(ctx
, attrib
);
1210 case UTIL_BLITTER_ATTRIB_TEXCOORD
:
1211 set_texcoords_in_vertices(attrib
->f
, &ctx
->vertices
[0][1][0], 8);
1217 blitter_draw(ctx
, x1
, y1
, x2
, y2
, depth
, 1);
1220 static void *get_clear_blend_state(struct blitter_context_priv
*ctx
,
1221 unsigned clear_buffers
)
1223 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1226 clear_buffers
&= PIPE_CLEAR_COLOR
;
1228 /* Return an existing blend state. */
1230 return ctx
->blend
[0][0];
1232 index
= GET_CLEAR_BLEND_STATE_IDX(clear_buffers
);
1234 if (ctx
->blend_clear
[index
])
1235 return ctx
->blend_clear
[index
];
1237 /* Create a new one. */
1239 struct pipe_blend_state blend
= {0};
1242 blend
.independent_blend_enable
= 1;
1244 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1245 if (clear_buffers
& (PIPE_CLEAR_COLOR0
<< i
)) {
1246 blend
.rt
[i
].colormask
= PIPE_MASK_RGBA
;
1250 ctx
->blend_clear
[index
] = pipe
->create_blend_state(pipe
, &blend
);
1252 return ctx
->blend_clear
[index
];
1255 void util_blitter_common_clear_setup(struct blitter_context
*blitter
,
1256 unsigned width
, unsigned height
,
1257 unsigned clear_buffers
,
1258 void *custom_blend
, void *custom_dsa
)
1260 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1261 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1263 util_blitter_set_running_flag(blitter
);
1264 blitter_check_saved_vertex_states(ctx
);
1265 blitter_check_saved_fragment_states(ctx
);
1266 blitter_disable_render_cond(ctx
);
1270 pipe
->bind_blend_state(pipe
, custom_blend
);
1272 pipe
->bind_blend_state(pipe
, get_clear_blend_state(ctx
, clear_buffers
));
1276 pipe
->bind_depth_stencil_alpha_state(pipe
, custom_dsa
);
1277 } else if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1278 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1279 } else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
1280 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1281 } else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
1282 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1284 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1287 pipe
->set_sample_mask(pipe
, ~0);
1288 blitter_set_dst_dimensions(ctx
, width
, height
);
1291 static void util_blitter_clear_custom(struct blitter_context
*blitter
,
1292 unsigned width
, unsigned height
,
1293 unsigned num_layers
,
1294 unsigned clear_buffers
,
1295 const union pipe_color_union
*color
,
1296 double depth
, unsigned stencil
,
1297 void *custom_blend
, void *custom_dsa
)
1299 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1300 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1301 struct pipe_stencil_ref sr
= { { 0 } };
1303 assert(ctx
->has_layered
|| num_layers
<= 1);
1305 util_blitter_common_clear_setup(blitter
, width
, height
, clear_buffers
,
1306 custom_blend
, custom_dsa
);
1308 sr
.ref_value
[0] = stencil
& 0xff;
1309 pipe
->set_stencil_ref(pipe
, &sr
);
1311 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1312 bind_fs_write_all_cbufs(ctx
);
1314 if (num_layers
> 1 && ctx
->has_layered
) {
1315 blitter_set_common_draw_rect_state(ctx
, FALSE
, TRUE
);
1316 blitter_set_clear_color(ctx
, color
);
1317 blitter_draw(ctx
, 0, 0, width
, height
, depth
, num_layers
);
1320 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1321 blitter
->draw_rectangle(blitter
, 0, 0, width
, height
, (float) depth
,
1322 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1325 util_blitter_restore_vertex_states(blitter
);
1326 util_blitter_restore_fragment_states(blitter
);
1327 util_blitter_restore_render_cond(blitter
);
1328 util_blitter_unset_running_flag(blitter
);
1331 void util_blitter_clear(struct blitter_context
*blitter
,
1332 unsigned width
, unsigned height
, unsigned num_layers
,
1333 unsigned clear_buffers
,
1334 const union pipe_color_union
*color
,
1335 double depth
, unsigned stencil
)
1337 util_blitter_clear_custom(blitter
, width
, height
, num_layers
,
1338 clear_buffers
, color
, depth
, stencil
,
1342 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1343 unsigned width
, unsigned height
,
1344 double depth
, void *custom_dsa
)
1346 static const union pipe_color_union color
;
1347 util_blitter_clear_custom(blitter
, width
, height
, 0, 0, &color
, depth
, 0,
1351 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1352 struct pipe_resource
*dst
,
1356 memset(dst_templ
, 0, sizeof(*dst_templ
));
1357 dst_templ
->format
= util_format_linear(dst
->format
);
1358 dst_templ
->u
.tex
.level
= dstlevel
;
1359 dst_templ
->u
.tex
.first_layer
= dstz
;
1360 dst_templ
->u
.tex
.last_layer
= dstz
;
1363 static struct pipe_surface
*
1364 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
1365 struct pipe_surface
*surf
)
1367 struct pipe_surface dst_templ
;
1369 memset(&dst_templ
, 0, sizeof(dst_templ
));
1370 dst_templ
.format
= surf
->format
;
1371 dst_templ
.u
.tex
.level
= surf
->u
.tex
.level
;
1372 dst_templ
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
+ 1;
1373 dst_templ
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
+ 1;
1375 return pipe
->create_surface(pipe
, surf
->texture
, &dst_templ
);
1378 void util_blitter_default_src_texture(struct pipe_sampler_view
*src_templ
,
1379 struct pipe_resource
*src
,
1382 memset(src_templ
, 0, sizeof(*src_templ
));
1383 src_templ
->target
= src
->target
;
1384 src_templ
->format
= util_format_linear(src
->format
);
1385 src_templ
->u
.tex
.first_level
= srclevel
;
1386 src_templ
->u
.tex
.last_level
= srclevel
;
1387 src_templ
->u
.tex
.first_layer
= 0;
1388 src_templ
->u
.tex
.last_layer
=
1389 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1390 : src
->array_size
- 1;
1391 src_templ
->swizzle_r
= PIPE_SWIZZLE_X
;
1392 src_templ
->swizzle_g
= PIPE_SWIZZLE_Y
;
1393 src_templ
->swizzle_b
= PIPE_SWIZZLE_Z
;
1394 src_templ
->swizzle_a
= PIPE_SWIZZLE_W
;
1397 static boolean
is_blit_generic_supported(struct blitter_context
*blitter
,
1398 const struct pipe_resource
*dst
,
1399 enum pipe_format dst_format
,
1400 const struct pipe_resource
*src
,
1401 enum pipe_format src_format
,
1404 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1405 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1409 const struct util_format_description
*desc
=
1410 util_format_description(dst_format
);
1411 boolean dst_has_stencil
= util_format_has_stencil(desc
);
1413 /* Stencil export must be supported for stencil copy. */
1414 if ((mask
& PIPE_MASK_S
) && dst_has_stencil
&&
1415 !ctx
->has_stencil_export
) {
1419 if (dst_has_stencil
|| util_format_has_depth(desc
))
1420 bind
= PIPE_BIND_DEPTH_STENCIL
;
1422 bind
= PIPE_BIND_RENDER_TARGET
;
1424 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1425 dst
->nr_samples
, bind
)) {
1431 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1435 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1436 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
)) {
1440 /* Check stencil sampler support for stencil copy. */
1441 if (mask
& PIPE_MASK_S
) {
1442 if (util_format_has_stencil(util_format_description(src_format
))) {
1443 enum pipe_format stencil_format
=
1444 util_format_stencil_only(src_format
);
1445 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1447 if (stencil_format
!= src_format
&&
1448 !screen
->is_format_supported(screen
, stencil_format
,
1449 src
->target
, src
->nr_samples
,
1450 PIPE_BIND_SAMPLER_VIEW
)) {
1460 boolean
util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1461 const struct pipe_resource
*dst
,
1462 const struct pipe_resource
*src
)
1464 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1465 src
, src
->format
, PIPE_MASK_RGBAZS
);
1468 boolean
util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1469 const struct pipe_blit_info
*info
)
1471 return is_blit_generic_supported(blitter
,
1472 info
->dst
.resource
, info
->dst
.format
,
1473 info
->src
.resource
, info
->src
.format
,
1477 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1478 struct pipe_resource
*dst
,
1480 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1481 struct pipe_resource
*src
,
1483 const struct pipe_box
*srcbox
)
1485 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1486 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1487 struct pipe_surface
*dst_view
, dst_templ
;
1488 struct pipe_sampler_view src_templ
, *src_view
;
1489 struct pipe_box dstbox
;
1492 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1494 u_box_3d(dstx
, dsty
, dstz
, abs(srcbox
->width
), abs(srcbox
->height
),
1495 abs(srcbox
->depth
), &dstbox
);
1497 /* Initialize the surface. */
1498 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1499 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1501 /* Initialize the sampler view. */
1502 util_blitter_default_src_texture(&src_templ
, src
, src_level
);
1503 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1506 util_blitter_blit_generic(blitter
, dst_view
, &dstbox
,
1507 src_view
, srcbox
, src
->width0
, src
->height0
,
1508 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
,
1511 pipe_surface_reference(&dst_view
, NULL
);
1512 pipe_sampler_view_reference(&src_view
, NULL
);
1515 static void do_blits(struct blitter_context_priv
*ctx
,
1516 struct pipe_surface
*dst
,
1517 const struct pipe_box
*dstbox
,
1518 struct pipe_sampler_view
*src
,
1519 unsigned src_width0
,
1520 unsigned src_height0
,
1521 const struct pipe_box
*srcbox
,
1524 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1525 unsigned src_samples
= src
->texture
->nr_samples
;
1526 unsigned dst_samples
= dst
->texture
->nr_samples
;
1527 enum pipe_texture_target src_target
= src
->texture
->target
;
1528 struct pipe_framebuffer_state fb_state
= {0};
1530 /* Initialize framebuffer state. */
1531 fb_state
.width
= dst
->width
;
1532 fb_state
.height
= dst
->height
;
1533 fb_state
.nr_cbufs
= is_zsbuf
? 0 : 1;
1535 blitter_set_dst_dimensions(ctx
, fb_state
.width
, fb_state
.height
);
1537 if ((src_target
== PIPE_TEXTURE_1D
||
1538 src_target
== PIPE_TEXTURE_2D
||
1539 src_target
== PIPE_TEXTURE_RECT
) &&
1541 /* Draw the quad with the draw_rectangle callback. */
1543 /* Set texture coordinates. - use a pipe color union
1544 * for interface purposes.
1545 * XXX pipe_color_union is a wrong name since we use that to set
1546 * texture coordinates too.
1548 union pipe_color_union coord
;
1549 get_texcoords(src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1550 srcbox
->x
+srcbox
->width
, srcbox
->y
+srcbox
->height
, coord
.f
);
1552 /* Set framebuffer state. */
1554 fb_state
.zsbuf
= dst
;
1556 fb_state
.cbufs
[0] = dst
;
1558 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1561 pipe
->set_sample_mask(pipe
, ~0);
1562 ctx
->base
.draw_rectangle(&ctx
->base
, dstbox
->x
, dstbox
->y
,
1563 dstbox
->x
+ dstbox
->width
,
1564 dstbox
->y
+ dstbox
->height
, 0,
1565 UTIL_BLITTER_ATTRIB_TEXCOORD
, &coord
);
1567 /* Draw the quad with the generic codepath. */
1569 for (dst_z
= 0; dst_z
< dstbox
->depth
; dst_z
++) {
1570 struct pipe_surface
*old
;
1571 float dst2src_scale
= srcbox
->depth
/ (float)dstbox
->depth
;
1573 /* Scale Z properly if the blit is scaled.
1575 * When downscaling, we want the coordinates centered, so that
1576 * mipmapping works for 3D textures. For example, when generating
1577 * a 4x4x4 level, this wouldn't average the pixels:
1579 * src Z: 0 1 2 3 4 5 6 7
1582 * Because the pixels are not centered below the pixels of the higher
1583 * level. Therefore, we want this:
1584 * src Z: 0 1 2 3 4 5 6 7
1587 * dst_offset defines the offset needed for centering the pixels and
1588 * it works with any scaling (not just 2x).
1590 float dst_offset
= ((srcbox
->depth
- 1) -
1591 (dstbox
->depth
- 1) * dst2src_scale
) * 0.5;
1592 float src_z
= (dst_z
+ dst_offset
) * dst2src_scale
;
1594 /* Set framebuffer state. */
1596 fb_state
.zsbuf
= dst
;
1598 fb_state
.cbufs
[0] = dst
;
1600 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1602 /* See if we need to blit a multisample or singlesample buffer. */
1603 if (src_samples
== dst_samples
&& dst_samples
> 1) {
1605 unsigned i
, max_sample
= dst_samples
- 1;
1607 for (i
= 0; i
<= max_sample
; i
++) {
1608 pipe
->set_sample_mask(pipe
, 1 << i
);
1609 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1611 i
, srcbox
->x
, srcbox
->y
,
1612 srcbox
->x
+ srcbox
->width
,
1613 srcbox
->y
+ srcbox
->height
);
1614 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1615 dstbox
->x
+ dstbox
->width
,
1616 dstbox
->y
+ dstbox
->height
, 0, 1);
1619 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1620 pipe
->set_sample_mask(pipe
, ~0);
1621 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1622 srcbox
->z
+ src_z
, 0,
1623 srcbox
->x
, srcbox
->y
,
1624 srcbox
->x
+ srcbox
->width
,
1625 srcbox
->y
+ srcbox
->height
);
1626 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1627 dstbox
->x
+ dstbox
->width
,
1628 dstbox
->y
+ dstbox
->height
, 0, 1);
1631 /* Get the next surface or (if this is the last iteration)
1632 * just unreference the last one. */
1634 if (dst_z
< dstbox
->depth
-1) {
1635 dst
= ctx
->base
.get_next_surface_layer(ctx
->base
.pipe
, dst
);
1638 pipe_surface_reference(&old
, NULL
);
1644 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1645 struct pipe_surface
*dst
,
1646 const struct pipe_box
*dstbox
,
1647 struct pipe_sampler_view
*src
,
1648 const struct pipe_box
*srcbox
,
1649 unsigned src_width0
, unsigned src_height0
,
1650 unsigned mask
, unsigned filter
,
1651 const struct pipe_scissor_state
*scissor
,
1652 boolean alpha_blend
)
1654 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1655 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1656 enum pipe_texture_target src_target
= src
->texture
->target
;
1657 unsigned src_samples
= src
->texture
->nr_samples
;
1658 unsigned dst_samples
= dst
->texture
->nr_samples
;
1659 boolean has_depth
, has_stencil
, has_color
;
1660 boolean blit_stencil
, blit_depth
, blit_color
;
1661 void *sampler_state
;
1662 const struct util_format_description
*src_desc
=
1663 util_format_description(src
->format
);
1664 const struct util_format_description
*dst_desc
=
1665 util_format_description(dst
->format
);
1667 has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
&&
1668 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1669 has_depth
= util_format_has_depth(src_desc
) &&
1670 util_format_has_depth(dst_desc
);
1671 has_stencil
= util_format_has_stencil(src_desc
) &&
1672 util_format_has_stencil(dst_desc
);
1674 blit_color
= has_color
&& (mask
& PIPE_MASK_RGBA
);
1675 blit_depth
= has_depth
&& (mask
& PIPE_MASK_Z
);
1676 blit_stencil
= has_stencil
&& (mask
& PIPE_MASK_S
) &&
1677 ctx
->has_stencil_export
;
1679 if (!blit_stencil
&& !blit_depth
&& !blit_color
) {
1684 (dstbox
->width
== abs(srcbox
->width
) &&
1685 dstbox
->height
== abs(srcbox
->height
))) {
1686 filter
= PIPE_TEX_FILTER_NEAREST
;
1689 /* Check whether the states are properly saved. */
1690 util_blitter_set_running_flag(blitter
);
1691 blitter_check_saved_vertex_states(ctx
);
1692 blitter_check_saved_fragment_states(ctx
);
1693 blitter_check_saved_textures(ctx
);
1694 blitter_check_saved_fb_state(ctx
);
1695 blitter_disable_render_cond(ctx
);
1697 if (blit_depth
|| blit_stencil
) {
1698 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
1700 if (blit_depth
&& blit_stencil
) {
1701 pipe
->bind_depth_stencil_alpha_state(pipe
,
1702 ctx
->dsa_write_depth_stencil
);
1703 ctx
->bind_fs_state(pipe
,
1704 blitter_get_fs_texfetch_depthstencil(ctx
, src_target
,
1706 } else if (blit_depth
) {
1707 pipe
->bind_depth_stencil_alpha_state(pipe
,
1708 ctx
->dsa_write_depth_keep_stencil
);
1709 ctx
->bind_fs_state(pipe
,
1710 blitter_get_fs_texfetch_depth(ctx
, src_target
,
1712 } else { /* is_stencil */
1713 pipe
->bind_depth_stencil_alpha_state(pipe
,
1714 ctx
->dsa_keep_depth_write_stencil
);
1715 ctx
->bind_fs_state(pipe
,
1716 blitter_get_fs_texfetch_stencil(ctx
, src_target
,
1721 unsigned colormask
= mask
& PIPE_MASK_RGBA
;
1723 pipe
->bind_blend_state(pipe
, ctx
->blend
[colormask
][alpha_blend
]);
1724 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1725 ctx
->bind_fs_state(pipe
,
1726 blitter_get_fs_texfetch_col(ctx
, src
->format
, src_target
,
1727 src_samples
, dst_samples
, filter
));
1730 /* Set the linear filter only for scaled color non-MSAA blits. */
1731 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
1732 if (src_target
== PIPE_TEXTURE_RECT
) {
1733 sampler_state
= ctx
->sampler_state_rect_linear
;
1735 sampler_state
= ctx
->sampler_state_linear
;
1738 if (src_target
== PIPE_TEXTURE_RECT
) {
1739 sampler_state
= ctx
->sampler_state_rect
;
1741 sampler_state
= ctx
->sampler_state
;
1746 if (blit_depth
&& blit_stencil
) {
1747 /* Setup two samplers, one for depth and the other one for stencil. */
1748 struct pipe_sampler_view templ
;
1749 struct pipe_sampler_view
*views
[2];
1750 void *samplers
[2] = {sampler_state
, sampler_state
};
1753 templ
.format
= util_format_stencil_only(templ
.format
);
1754 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1757 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1759 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, views
);
1760 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, samplers
);
1762 pipe_sampler_view_reference(&views
[1], NULL
);
1763 } else if (blit_stencil
) {
1764 /* Set a stencil-only sampler view for it not to sample depth instead. */
1765 struct pipe_sampler_view templ
;
1766 struct pipe_sampler_view
*view
;
1769 templ
.format
= util_format_stencil_only(templ
.format
);
1770 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1772 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1774 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &view
);
1775 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1776 0, 1, &sampler_state
);
1778 pipe_sampler_view_reference(&view
, NULL
);
1780 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src
);
1781 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1782 0, 1, &sampler_state
);
1785 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1787 pipe
->set_scissor_states(pipe
, 0, 1, scissor
);
1790 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
, FALSE
);
1792 do_blits(ctx
, dst
, dstbox
, src
, src_width0
, src_height0
,
1793 srcbox
, blit_depth
|| blit_stencil
);
1795 util_blitter_restore_vertex_states(blitter
);
1796 util_blitter_restore_fragment_states(blitter
);
1797 util_blitter_restore_textures(blitter
);
1798 util_blitter_restore_fb_state(blitter
);
1800 pipe
->set_scissor_states(pipe
, 0, 1, &ctx
->base
.saved_scissor
);
1802 util_blitter_restore_render_cond(blitter
);
1803 util_blitter_unset_running_flag(blitter
);
1807 util_blitter_blit(struct blitter_context
*blitter
,
1808 const struct pipe_blit_info
*info
)
1810 struct pipe_resource
*dst
= info
->dst
.resource
;
1811 struct pipe_resource
*src
= info
->src
.resource
;
1812 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1813 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1814 struct pipe_surface
*dst_view
, dst_templ
;
1815 struct pipe_sampler_view src_templ
, *src_view
;
1817 /* Initialize the surface. */
1818 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
1820 dst_templ
.format
= info
->dst
.format
;
1821 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1823 /* Initialize the sampler view. */
1824 util_blitter_default_src_texture(&src_templ
, src
, info
->src
.level
);
1825 src_templ
.format
= info
->src
.format
;
1826 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1829 util_blitter_blit_generic(blitter
, dst_view
, &info
->dst
.box
,
1830 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
1831 info
->mask
, info
->filter
,
1832 info
->scissor_enable
? &info
->scissor
: NULL
,
1835 pipe_surface_reference(&dst_view
, NULL
);
1836 pipe_sampler_view_reference(&src_view
, NULL
);
1839 void util_blitter_generate_mipmap(struct blitter_context
*blitter
,
1840 struct pipe_resource
*tex
,
1841 enum pipe_format format
,
1842 unsigned base_level
, unsigned last_level
,
1843 unsigned first_layer
, unsigned last_layer
)
1845 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1846 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1847 struct pipe_surface dst_templ
, *dst_view
;
1848 struct pipe_sampler_view src_templ
, *src_view
;
1850 void *sampler_state
;
1851 const struct util_format_description
*desc
=
1852 util_format_description(format
);
1855 assert(tex
->nr_samples
<= 1);
1856 assert(!util_format_has_stencil(desc
));
1858 is_depth
= desc
->colorspace
== UTIL_FORMAT_COLORSPACE_ZS
;
1860 /* Check whether the states are properly saved. */
1861 util_blitter_set_running_flag(blitter
);
1862 blitter_check_saved_vertex_states(ctx
);
1863 blitter_check_saved_fragment_states(ctx
);
1864 blitter_check_saved_textures(ctx
);
1865 blitter_check_saved_fb_state(ctx
);
1866 blitter_disable_render_cond(ctx
);
1870 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
1871 pipe
->bind_depth_stencil_alpha_state(pipe
,
1872 ctx
->dsa_write_depth_keep_stencil
);
1873 ctx
->bind_fs_state(pipe
,
1874 blitter_get_fs_texfetch_depth(ctx
, tex
->target
, 1));
1876 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
1877 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1878 ctx
->bind_fs_state(pipe
,
1879 blitter_get_fs_texfetch_col(ctx
, tex
->format
, tex
->target
,
1880 1, 1, PIPE_TEX_FILTER_LINEAR
));
1883 if (tex
->target
== PIPE_TEXTURE_RECT
) {
1884 sampler_state
= ctx
->sampler_state_rect_linear
;
1886 sampler_state
= ctx
->sampler_state_linear
;
1888 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1889 0, 1, &sampler_state
);
1891 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1892 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1894 for (src_level
= base_level
; src_level
< last_level
; src_level
++) {
1895 struct pipe_box dstbox
= {0}, srcbox
= {0};
1896 unsigned dst_level
= src_level
+ 1;
1898 dstbox
.width
= u_minify(tex
->width0
, dst_level
);
1899 dstbox
.height
= u_minify(tex
->height0
, dst_level
);
1901 srcbox
.width
= u_minify(tex
->width0
, src_level
);
1902 srcbox
.height
= u_minify(tex
->height0
, src_level
);
1904 if (tex
->target
== PIPE_TEXTURE_3D
) {
1905 dstbox
.depth
= util_max_layer(tex
, dst_level
) + 1;
1906 srcbox
.depth
= util_max_layer(tex
, src_level
) + 1;
1908 dstbox
.z
= srcbox
.z
= first_layer
;
1909 dstbox
.depth
= srcbox
.depth
= last_layer
- first_layer
+ 1;
1912 /* Initialize the surface. */
1913 util_blitter_default_dst_texture(&dst_templ
, tex
, dst_level
,
1915 dst_templ
.format
= format
;
1916 dst_view
= pipe
->create_surface(pipe
, tex
, &dst_templ
);
1918 /* Initialize the sampler view. */
1919 util_blitter_default_src_texture(&src_templ
, tex
, src_level
);
1920 src_templ
.format
= format
;
1921 src_view
= pipe
->create_sampler_view(pipe
, tex
, &src_templ
);
1923 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src_view
);
1925 do_blits(ctx
, dst_view
, &dstbox
, src_view
, tex
->width0
, tex
->height0
,
1928 pipe_surface_reference(&dst_view
, NULL
);
1929 pipe_sampler_view_reference(&src_view
, NULL
);
1932 util_blitter_restore_vertex_states(blitter
);
1933 util_blitter_restore_fragment_states(blitter
);
1934 util_blitter_restore_textures(blitter
);
1935 util_blitter_restore_fb_state(blitter
);
1936 util_blitter_restore_render_cond(blitter
);
1937 util_blitter_unset_running_flag(blitter
);
1940 /* Clear a region of a color surface to a constant value. */
1941 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
1942 struct pipe_surface
*dstsurf
,
1943 const union pipe_color_union
*color
,
1944 unsigned dstx
, unsigned dsty
,
1945 unsigned width
, unsigned height
)
1947 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1948 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1949 struct pipe_framebuffer_state fb_state
;
1950 unsigned num_layers
;
1952 assert(dstsurf
->texture
);
1953 if (!dstsurf
->texture
)
1956 /* check the saved state */
1957 util_blitter_set_running_flag(blitter
);
1958 blitter_check_saved_vertex_states(ctx
);
1959 blitter_check_saved_fragment_states(ctx
);
1960 blitter_check_saved_fb_state(ctx
);
1961 blitter_disable_render_cond(ctx
);
1964 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
][0]);
1965 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1966 bind_fs_write_one_cbuf(ctx
);
1967 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1969 /* set a framebuffer state */
1970 fb_state
.width
= dstsurf
->width
;
1971 fb_state
.height
= dstsurf
->height
;
1972 fb_state
.nr_cbufs
= 1;
1973 fb_state
.cbufs
[0] = dstsurf
;
1975 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1976 pipe
->set_sample_mask(pipe
, ~0);
1978 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1980 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
1981 if (num_layers
> 1 && ctx
->has_layered
) {
1982 blitter_set_common_draw_rect_state(ctx
, FALSE
, TRUE
);
1983 blitter_set_clear_color(ctx
, color
);
1984 blitter_draw(ctx
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0, num_layers
);
1987 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1988 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
1989 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1992 util_blitter_restore_vertex_states(blitter
);
1993 util_blitter_restore_fragment_states(blitter
);
1994 util_blitter_restore_fb_state(blitter
);
1995 util_blitter_restore_render_cond(blitter
);
1996 util_blitter_unset_running_flag(blitter
);
1999 /* Clear a region of a depth stencil surface. */
2000 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
2001 struct pipe_surface
*dstsurf
,
2002 unsigned clear_flags
,
2005 unsigned dstx
, unsigned dsty
,
2006 unsigned width
, unsigned height
)
2008 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2009 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2010 struct pipe_framebuffer_state fb_state
;
2011 struct pipe_stencil_ref sr
= { { 0 } };
2012 unsigned num_layers
;
2014 assert(dstsurf
->texture
);
2015 if (!dstsurf
->texture
)
2018 /* check the saved state */
2019 util_blitter_set_running_flag(blitter
);
2020 blitter_check_saved_vertex_states(ctx
);
2021 blitter_check_saved_fragment_states(ctx
);
2022 blitter_check_saved_fb_state(ctx
);
2023 blitter_disable_render_cond(ctx
);
2026 pipe
->bind_blend_state(pipe
, ctx
->blend
[0][0]);
2027 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
2028 sr
.ref_value
[0] = stencil
& 0xff;
2029 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
2030 pipe
->set_stencil_ref(pipe
, &sr
);
2032 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
2033 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
2035 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
2036 sr
.ref_value
[0] = stencil
& 0xff;
2037 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
2038 pipe
->set_stencil_ref(pipe
, &sr
);
2041 /* hmm that should be illegal probably, or make it a no-op somewhere */
2042 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2045 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2047 /* set a framebuffer state */
2048 fb_state
.width
= dstsurf
->width
;
2049 fb_state
.height
= dstsurf
->height
;
2050 fb_state
.nr_cbufs
= 0;
2051 fb_state
.cbufs
[0] = 0;
2052 fb_state
.zsbuf
= dstsurf
;
2053 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2054 pipe
->set_sample_mask(pipe
, ~0);
2056 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2058 num_layers
= dstsurf
->u
.tex
.last_layer
- dstsurf
->u
.tex
.first_layer
+ 1;
2059 if (num_layers
> 1 && ctx
->has_layered
) {
2060 blitter_set_common_draw_rect_state(ctx
, FALSE
, TRUE
);
2061 blitter_draw(ctx
, dstx
, dsty
, dstx
+width
, dsty
+height
, (float) depth
, num_layers
);
2064 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2065 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
,
2067 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2070 util_blitter_restore_vertex_states(blitter
);
2071 util_blitter_restore_fragment_states(blitter
);
2072 util_blitter_restore_fb_state(blitter
);
2073 util_blitter_restore_render_cond(blitter
);
2074 util_blitter_unset_running_flag(blitter
);
2077 /* draw a rectangle across a region using a custom dsa stage - for r600g */
2078 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
2079 struct pipe_surface
*zsurf
,
2080 struct pipe_surface
*cbsurf
,
2081 unsigned sample_mask
,
2082 void *dsa_stage
, float depth
)
2084 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2085 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2086 struct pipe_framebuffer_state fb_state
;
2088 assert(zsurf
->texture
);
2089 if (!zsurf
->texture
)
2092 /* check the saved state */
2093 util_blitter_set_running_flag(blitter
);
2094 blitter_check_saved_vertex_states(ctx
);
2095 blitter_check_saved_fragment_states(ctx
);
2096 blitter_check_saved_fb_state(ctx
);
2097 blitter_disable_render_cond(ctx
);
2100 pipe
->bind_blend_state(pipe
, cbsurf
? ctx
->blend
[PIPE_MASK_RGBA
][0] :
2102 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
2104 bind_fs_write_one_cbuf(ctx
);
2107 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2109 /* set a framebuffer state */
2110 fb_state
.width
= zsurf
->width
;
2111 fb_state
.height
= zsurf
->height
;
2112 fb_state
.nr_cbufs
= 1;
2114 fb_state
.cbufs
[0] = cbsurf
;
2115 fb_state
.nr_cbufs
= 1;
2117 fb_state
.cbufs
[0] = NULL
;
2118 fb_state
.nr_cbufs
= 0;
2120 fb_state
.zsbuf
= zsurf
;
2121 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2122 pipe
->set_sample_mask(pipe
, sample_mask
);
2124 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2125 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
2126 blitter
->draw_rectangle(blitter
, 0, 0, zsurf
->width
, zsurf
->height
, depth
,
2127 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
2129 util_blitter_restore_vertex_states(blitter
);
2130 util_blitter_restore_fragment_states(blitter
);
2131 util_blitter_restore_fb_state(blitter
);
2132 util_blitter_restore_render_cond(blitter
);
2133 util_blitter_unset_running_flag(blitter
);
2136 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
2137 struct pipe_resource
*dst
,
2139 struct pipe_resource
*src
,
2143 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2144 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2145 struct pipe_vertex_buffer vb
;
2146 struct pipe_stream_output_target
*so_target
;
2147 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2149 if (srcx
>= src
->width0
||
2150 dstx
>= dst
->width0
) {
2153 if (srcx
+ size
> src
->width0
) {
2154 size
= src
->width0
- srcx
;
2156 if (dstx
+ size
> dst
->width0
) {
2157 size
= dst
->width0
- dstx
;
2160 /* Drivers not capable of Stream Out should not call this function
2161 * in the first place. */
2162 assert(ctx
->has_stream_out
);
2164 /* Some alignment is required. */
2165 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
2166 !ctx
->has_stream_out
) {
2167 struct pipe_box box
;
2168 u_box_1d(srcx
, size
, &box
);
2169 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
2173 util_blitter_set_running_flag(blitter
);
2174 blitter_check_saved_vertex_states(ctx
);
2175 blitter_disable_render_cond(ctx
);
2178 vb
.buffer_offset
= srcx
;
2181 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2182 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[0]);
2183 bind_vs_pos_only(ctx
, 1);
2184 if (ctx
->has_geometry_shader
)
2185 pipe
->bind_gs_state(pipe
, NULL
);
2186 if (ctx
->has_tessellation
) {
2187 pipe
->bind_tcs_state(pipe
, NULL
);
2188 pipe
->bind_tes_state(pipe
, NULL
);
2190 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2192 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
2193 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2195 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2197 util_blitter_restore_vertex_states(blitter
);
2198 util_blitter_restore_render_cond(blitter
);
2199 util_blitter_unset_running_flag(blitter
);
2200 pipe_so_target_reference(&so_target
, NULL
);
2203 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
2204 struct pipe_resource
*dst
,
2205 unsigned offset
, unsigned size
,
2206 unsigned num_channels
,
2207 const union pipe_color_union
*clear_value
)
2209 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2210 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2211 struct pipe_vertex_buffer vb
= {0};
2212 struct pipe_stream_output_target
*so_target
= NULL
;
2213 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
2215 assert(num_channels
>= 1);
2216 assert(num_channels
<= 4);
2218 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
2220 * R600 uses this to initialize texture resources, so width0 might not be
2221 * what you think it is.
2224 /* Streamout is required. */
2225 if (!ctx
->has_stream_out
) {
2226 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2230 /* Some alignment is required. */
2231 if (offset
% 4 != 0 || size
% 4 != 0) {
2232 assert(!"Bad alignment in util_blitter_clear_buffer()");
2236 u_upload_data(ctx
->upload
, 0, num_channels
*4, 4, clear_value
,
2237 &vb
.buffer_offset
, &vb
.buffer
);
2243 util_blitter_set_running_flag(blitter
);
2244 blitter_check_saved_vertex_states(ctx
);
2245 blitter_disable_render_cond(ctx
);
2247 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2248 pipe
->bind_vertex_elements_state(pipe
,
2249 ctx
->velem_state_readbuf
[num_channels
-1]);
2250 bind_vs_pos_only(ctx
, num_channels
);
2251 if (ctx
->has_geometry_shader
)
2252 pipe
->bind_gs_state(pipe
, NULL
);
2253 if (ctx
->has_tessellation
) {
2254 pipe
->bind_tcs_state(pipe
, NULL
);
2255 pipe
->bind_tes_state(pipe
, NULL
);
2257 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2259 so_target
= pipe
->create_stream_output_target(pipe
, dst
, offset
, size
);
2260 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2262 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2265 util_blitter_restore_vertex_states(blitter
);
2266 util_blitter_restore_render_cond(blitter
);
2267 util_blitter_unset_running_flag(blitter
);
2268 pipe_so_target_reference(&so_target
, NULL
);
2269 pipe_resource_reference(&vb
.buffer
, NULL
);
2272 /* probably radeon specific */
2273 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
2274 struct pipe_resource
*dst
,
2277 struct pipe_resource
*src
,
2279 unsigned sample_mask
,
2281 enum pipe_format format
)
2283 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2284 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2285 struct pipe_framebuffer_state fb_state
;
2286 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
2288 util_blitter_set_running_flag(blitter
);
2289 blitter_check_saved_vertex_states(ctx
);
2290 blitter_check_saved_fragment_states(ctx
);
2291 blitter_disable_render_cond(ctx
);
2294 pipe
->bind_blend_state(pipe
, custom_blend
);
2295 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2296 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2297 bind_fs_write_one_cbuf(ctx
);
2298 pipe
->set_sample_mask(pipe
, sample_mask
);
2300 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
2301 surf_tmpl
.format
= format
;
2302 surf_tmpl
.u
.tex
.level
= dst_level
;
2303 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
2304 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
2306 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
2308 surf_tmpl
.u
.tex
.level
= 0;
2309 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
2310 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
2312 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
2314 /* set a framebuffer state */
2315 fb_state
.width
= src
->width0
;
2316 fb_state
.height
= src
->height0
;
2317 fb_state
.nr_cbufs
= 2;
2318 fb_state
.cbufs
[0] = srcsurf
;
2319 fb_state
.cbufs
[1] = dstsurf
;
2320 fb_state
.zsbuf
= NULL
;
2321 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2323 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2324 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
2325 blitter
->draw_rectangle(blitter
, 0, 0, src
->width0
, src
->height0
,
2327 util_blitter_restore_fb_state(blitter
);
2328 util_blitter_restore_vertex_states(blitter
);
2329 util_blitter_restore_fragment_states(blitter
);
2330 util_blitter_restore_render_cond(blitter
);
2331 util_blitter_unset_running_flag(blitter
);
2333 pipe_surface_reference(&srcsurf
, NULL
);
2334 pipe_surface_reference(&dstsurf
, NULL
);
2337 void util_blitter_custom_color(struct blitter_context
*blitter
,
2338 struct pipe_surface
*dstsurf
,
2341 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2342 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2343 struct pipe_framebuffer_state fb_state
;
2345 assert(dstsurf
->texture
);
2346 if (!dstsurf
->texture
)
2349 /* check the saved state */
2350 util_blitter_set_running_flag(blitter
);
2351 blitter_check_saved_vertex_states(ctx
);
2352 blitter_check_saved_fragment_states(ctx
);
2353 blitter_check_saved_fb_state(ctx
);
2354 blitter_disable_render_cond(ctx
);
2357 pipe
->bind_blend_state(pipe
, custom_blend
? custom_blend
2358 : ctx
->blend
[PIPE_MASK_RGBA
][0]);
2359 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2360 bind_fs_write_one_cbuf(ctx
);
2361 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2362 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2364 /* set a framebuffer state */
2365 fb_state
.width
= dstsurf
->width
;
2366 fb_state
.height
= dstsurf
->height
;
2367 fb_state
.nr_cbufs
= 1;
2368 fb_state
.cbufs
[0] = dstsurf
;
2370 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2371 pipe
->set_sample_mask(pipe
, ~0);
2373 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2374 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2375 blitter
->draw_rectangle(blitter
, 0, 0, dstsurf
->width
, dstsurf
->height
,
2378 util_blitter_restore_vertex_states(blitter
);
2379 util_blitter_restore_fragment_states(blitter
);
2380 util_blitter_restore_fb_state(blitter
);
2381 util_blitter_restore_render_cond(blitter
);
2382 util_blitter_unset_running_flag(blitter
);