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_stream_out
;
134 boolean has_stencil_export
;
135 boolean has_texture_multisample
;
137 /* The Draw module overrides these functions.
138 * Always create the blitter before Draw. */
139 void (*bind_fs_state
)(struct pipe_context
*, void *);
140 void (*delete_fs_state
)(struct pipe_context
*, void *);
143 static struct pipe_surface
*
144 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
145 struct pipe_surface
*surf
);
147 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
)
149 struct blitter_context_priv
*ctx
;
150 struct pipe_blend_state blend
;
151 struct pipe_depth_stencil_alpha_state dsa
;
152 struct pipe_rasterizer_state rs_state
;
153 struct pipe_sampler_state sampler_state
;
154 struct pipe_vertex_element velem
[2];
157 ctx
= CALLOC_STRUCT(blitter_context_priv
);
161 ctx
->base
.pipe
= pipe
;
162 ctx
->base
.draw_rectangle
= util_blitter_draw_rectangle
;
163 ctx
->base
.get_next_surface_layer
= util_blitter_get_next_surface_layer
;
165 ctx
->bind_fs_state
= pipe
->bind_fs_state
;
166 ctx
->delete_fs_state
= pipe
->delete_fs_state
;
168 /* init state objects for them to be considered invalid */
169 ctx
->base
.saved_blend_state
= INVALID_PTR
;
170 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
171 ctx
->base
.saved_rs_state
= INVALID_PTR
;
172 ctx
->base
.saved_fs
= INVALID_PTR
;
173 ctx
->base
.saved_vs
= INVALID_PTR
;
174 ctx
->base
.saved_gs
= INVALID_PTR
;
175 ctx
->base
.saved_velem_state
= INVALID_PTR
;
176 ctx
->base
.saved_fb_state
.nr_cbufs
= ~0;
177 ctx
->base
.saved_num_sampler_views
= ~0;
178 ctx
->base
.saved_num_sampler_states
= ~0;
179 ctx
->base
.saved_num_so_targets
= ~0;
181 ctx
->has_geometry_shader
=
182 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
183 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
184 ctx
->has_stream_out
=
185 pipe
->screen
->get_param(pipe
->screen
,
186 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0;
188 ctx
->has_stencil_export
=
189 pipe
->screen
->get_param(pipe
->screen
,
190 PIPE_CAP_SHADER_STENCIL_EXPORT
);
192 ctx
->has_texture_multisample
=
193 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TEXTURE_MULTISAMPLE
);
195 /* blend state objects */
196 memset(&blend
, 0, sizeof(blend
));
198 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
199 blend
.rt
[0].colormask
= i
;
200 ctx
->blend
[i
] = pipe
->create_blend_state(pipe
, &blend
);
203 /* depth stencil alpha state objects */
204 memset(&dsa
, 0, sizeof(dsa
));
205 ctx
->dsa_keep_depth_stencil
=
206 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
208 dsa
.depth
.enabled
= 1;
209 dsa
.depth
.writemask
= 1;
210 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
211 ctx
->dsa_write_depth_keep_stencil
=
212 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
214 dsa
.stencil
[0].enabled
= 1;
215 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
216 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
217 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
218 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
219 dsa
.stencil
[0].valuemask
= 0xff;
220 dsa
.stencil
[0].writemask
= 0xff;
221 ctx
->dsa_write_depth_stencil
=
222 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
224 dsa
.depth
.enabled
= 0;
225 dsa
.depth
.writemask
= 0;
226 ctx
->dsa_keep_depth_write_stencil
=
227 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
230 memset(&sampler_state
, 0, sizeof(sampler_state
));
231 sampler_state
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
232 sampler_state
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
233 sampler_state
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
234 sampler_state
.normalized_coords
= 1;
235 ctx
->sampler_state
= pipe
->create_sampler_state(pipe
, &sampler_state
);
236 sampler_state
.normalized_coords
= 0;
237 ctx
->sampler_state_rect
= pipe
->create_sampler_state(pipe
, &sampler_state
);
239 sampler_state
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
240 sampler_state
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
241 sampler_state
.normalized_coords
= 1;
242 ctx
->sampler_state_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
243 sampler_state
.normalized_coords
= 0;
244 ctx
->sampler_state_rect_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
246 /* rasterizer state */
247 memset(&rs_state
, 0, sizeof(rs_state
));
248 rs_state
.cull_face
= PIPE_FACE_NONE
;
249 rs_state
.half_pixel_center
= 1;
250 rs_state
.bottom_edge_rule
= 1;
251 rs_state
.flatshade
= 1;
252 rs_state
.depth_clip
= 1;
253 ctx
->rs_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
255 rs_state
.scissor
= 1;
256 ctx
->rs_state_scissor
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
258 if (ctx
->has_stream_out
) {
259 rs_state
.scissor
= 0;
260 rs_state
.rasterizer_discard
= 1;
261 ctx
->rs_discard_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
264 ctx
->base
.vb_slot
= 0; /* 0 for now */
266 /* vertex elements states */
267 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
268 for (i
= 0; i
< 2; i
++) {
269 velem
[i
].src_offset
= i
* 4 * sizeof(float);
270 velem
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
271 velem
[i
].vertex_buffer_index
= ctx
->base
.vb_slot
;
273 ctx
->velem_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
275 if (ctx
->has_stream_out
) {
276 static enum pipe_format formats
[4] = {
277 PIPE_FORMAT_R32_UINT
,
278 PIPE_FORMAT_R32G32_UINT
,
279 PIPE_FORMAT_R32G32B32_UINT
,
280 PIPE_FORMAT_R32G32B32A32_UINT
283 for (i
= 0; i
< 4; i
++) {
284 velem
[0].src_format
= formats
[i
];
285 velem
[0].vertex_buffer_index
= ctx
->base
.vb_slot
;
286 ctx
->velem_state_readbuf
[i
] =
287 pipe
->create_vertex_elements_state(pipe
, 1, &velem
[0]);
291 /* Fragment shaders are created on-demand, except these.
292 * The interpolation must be constant for integer texture clearing to work.
294 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
295 ctx
->fs_write_one_cbuf
=
296 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
297 TGSI_INTERPOLATE_CONSTANT
, FALSE
);
298 ctx
->fs_write_all_cbufs
=
299 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
300 TGSI_INTERPOLATE_CONSTANT
, TRUE
);
304 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
305 TGSI_SEMANTIC_GENERIC
};
306 const uint semantic_indices
[] = { 0, 0 };
308 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
312 if (ctx
->has_stream_out
) {
313 struct pipe_stream_output_info so
;
314 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
};
315 const uint semantic_indices
[] = { 0 };
317 memset(&so
, 0, sizeof(so
));
319 so
.output
[0].num_components
= 1;
323 util_make_vertex_passthrough_shader_with_so(pipe
, 1, semantic_names
,
324 semantic_indices
, &so
);
327 if (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_INSTANCEID
) &&
328 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_VS_LAYER
)) {
329 ctx
->vs_layered
= util_make_layered_clear_vertex_shader(pipe
);
332 /* set invariant vertex coordinates */
333 for (i
= 0; i
< 4; i
++)
334 ctx
->vertices
[i
][0][3] = 1; /*v.w*/
336 ctx
->upload
= u_upload_create(pipe
, 65536, 4, PIPE_BIND_VERTEX_BUFFER
);
341 void util_blitter_destroy(struct blitter_context
*blitter
)
343 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
344 struct pipe_context
*pipe
= blitter
->pipe
;
347 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
348 pipe
->delete_blend_state(pipe
, ctx
->blend
[i
]);
350 for (i
= 0; i
< Elements(ctx
->blend_clear
); i
++) {
351 if (ctx
->blend_clear
[i
])
352 pipe
->delete_blend_state(pipe
, ctx
->blend_clear
[i
]);
354 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
355 pipe
->delete_depth_stencil_alpha_state(pipe
,
356 ctx
->dsa_write_depth_keep_stencil
);
357 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
358 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
360 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
);
361 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state_scissor
);
362 if (ctx
->rs_discard_state
)
363 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_discard_state
);
364 pipe
->delete_vs_state(pipe
, ctx
->vs
);
365 if (ctx
->vs_pos_only
)
366 pipe
->delete_vs_state(pipe
, ctx
->vs_pos_only
);
368 pipe
->delete_vs_state(pipe
, ctx
->vs_layered
);
369 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state
);
370 for (i
= 0; i
< 4; i
++) {
371 if (ctx
->velem_state_readbuf
[i
]) {
372 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[i
]);
376 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
377 if (ctx
->fs_texfetch_col
[i
])
378 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[i
]);
379 if (ctx
->fs_texfetch_depth
[i
])
380 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
]);
381 if (ctx
->fs_texfetch_depthstencil
[i
])
382 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil
[i
]);
383 if (ctx
->fs_texfetch_stencil
[i
])
384 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil
[i
]);
386 for (j
= 0; j
< Elements(ctx
->fs_resolve
[i
]); j
++)
387 for (f
= 0; f
< 2; f
++)
388 if (ctx
->fs_resolve
[i
][j
][f
])
389 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve
[i
][j
][f
]);
391 for (j
= 0; j
< Elements(ctx
->fs_resolve_sint
[i
]); j
++)
392 for (f
= 0; f
< 2; f
++)
393 if (ctx
->fs_resolve_sint
[i
][j
][f
])
394 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve_sint
[i
][j
][f
]);
396 for (j
= 0; j
< Elements(ctx
->fs_resolve_uint
[i
]); j
++)
397 for (f
= 0; f
< 2; f
++)
398 if (ctx
->fs_resolve_uint
[i
][j
][f
])
399 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve_uint
[i
][j
][f
]);
402 ctx
->delete_fs_state(pipe
, ctx
->fs_empty
);
403 ctx
->delete_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
404 ctx
->delete_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
406 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect_linear
);
407 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect
);
408 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_linear
);
409 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
);
410 u_upload_destroy(ctx
->upload
);
414 void util_blitter_set_texture_multisample(struct blitter_context
*blitter
,
417 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
419 ctx
->has_texture_multisample
= supported
;
422 static void blitter_set_running_flag(struct blitter_context_priv
*ctx
)
424 if (ctx
->base
.running
) {
425 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
428 ctx
->base
.running
= TRUE
;
431 static void blitter_unset_running_flag(struct blitter_context_priv
*ctx
)
433 if (!ctx
->base
.running
) {
434 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
437 ctx
->base
.running
= FALSE
;
440 static void blitter_check_saved_vertex_states(struct blitter_context_priv
*ctx
)
442 assert(ctx
->base
.saved_velem_state
!= INVALID_PTR
);
443 assert(ctx
->base
.saved_vs
!= INVALID_PTR
);
444 assert(!ctx
->has_geometry_shader
|| ctx
->base
.saved_gs
!= INVALID_PTR
);
445 assert(!ctx
->has_stream_out
|| ctx
->base
.saved_num_so_targets
!= ~0);
446 assert(ctx
->base
.saved_rs_state
!= INVALID_PTR
);
449 static void blitter_restore_vertex_states(struct blitter_context_priv
*ctx
)
451 struct pipe_context
*pipe
= ctx
->base
.pipe
;
455 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1,
456 &ctx
->base
.saved_vertex_buffer
);
457 pipe_resource_reference(&ctx
->base
.saved_vertex_buffer
.buffer
, NULL
);
459 /* Vertex elements. */
460 pipe
->bind_vertex_elements_state(pipe
, ctx
->base
.saved_velem_state
);
461 ctx
->base
.saved_velem_state
= INVALID_PTR
;
464 pipe
->bind_vs_state(pipe
, ctx
->base
.saved_vs
);
465 ctx
->base
.saved_vs
= INVALID_PTR
;
467 /* Geometry shader. */
468 if (ctx
->has_geometry_shader
) {
469 pipe
->bind_gs_state(pipe
, ctx
->base
.saved_gs
);
470 ctx
->base
.saved_gs
= INVALID_PTR
;
473 /* Stream outputs. */
474 if (ctx
->has_stream_out
) {
475 pipe
->set_stream_output_targets(pipe
,
476 ctx
->base
.saved_num_so_targets
,
477 ctx
->base
.saved_so_targets
, ~0);
479 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
480 pipe_so_target_reference(&ctx
->base
.saved_so_targets
[i
], NULL
);
482 ctx
->base
.saved_num_so_targets
= ~0;
486 pipe
->bind_rasterizer_state(pipe
, ctx
->base
.saved_rs_state
);
487 ctx
->base
.saved_rs_state
= INVALID_PTR
;
490 static void blitter_check_saved_fragment_states(struct blitter_context_priv
*ctx
)
492 assert(ctx
->base
.saved_fs
!= INVALID_PTR
);
493 assert(ctx
->base
.saved_dsa_state
!= INVALID_PTR
);
494 assert(ctx
->base
.saved_blend_state
!= INVALID_PTR
);
497 static void blitter_restore_fragment_states(struct blitter_context_priv
*ctx
)
499 struct pipe_context
*pipe
= ctx
->base
.pipe
;
501 /* Fragment shader. */
502 ctx
->bind_fs_state(pipe
, ctx
->base
.saved_fs
);
503 ctx
->base
.saved_fs
= INVALID_PTR
;
505 /* Depth, stencil, alpha. */
506 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->base
.saved_dsa_state
);
507 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
510 pipe
->bind_blend_state(pipe
, ctx
->base
.saved_blend_state
);
511 ctx
->base
.saved_blend_state
= INVALID_PTR
;
514 if (ctx
->base
.is_sample_mask_saved
) {
515 pipe
->set_sample_mask(pipe
, ctx
->base
.saved_sample_mask
);
516 ctx
->base
.is_sample_mask_saved
= FALSE
;
519 /* Miscellaneous states. */
520 /* XXX check whether these are saved and whether they need to be restored
521 * (depending on the operation) */
522 pipe
->set_stencil_ref(pipe
, &ctx
->base
.saved_stencil_ref
);
523 pipe
->set_viewport_states(pipe
, 0, 1, &ctx
->base
.saved_viewport
);
526 static void blitter_check_saved_fb_state(struct blitter_context_priv
*ctx
)
528 assert(ctx
->base
.saved_fb_state
.nr_cbufs
!= ~0);
531 static void blitter_disable_render_cond(struct blitter_context_priv
*ctx
)
533 struct pipe_context
*pipe
= ctx
->base
.pipe
;
535 if (ctx
->base
.saved_render_cond_query
) {
536 pipe
->render_condition(pipe
, NULL
, FALSE
, 0);
540 static void blitter_restore_render_cond(struct blitter_context_priv
*ctx
)
542 struct pipe_context
*pipe
= ctx
->base
.pipe
;
544 if (ctx
->base
.saved_render_cond_query
) {
545 pipe
->render_condition(pipe
, ctx
->base
.saved_render_cond_query
,
546 ctx
->base
.saved_render_cond_cond
,
547 ctx
->base
.saved_render_cond_mode
);
548 ctx
->base
.saved_render_cond_query
= NULL
;
552 static void blitter_restore_fb_state(struct blitter_context_priv
*ctx
)
554 struct pipe_context
*pipe
= ctx
->base
.pipe
;
556 pipe
->set_framebuffer_state(pipe
, &ctx
->base
.saved_fb_state
);
557 util_unreference_framebuffer_state(&ctx
->base
.saved_fb_state
);
560 static void blitter_check_saved_textures(struct blitter_context_priv
*ctx
)
562 assert(ctx
->base
.saved_num_sampler_states
!= ~0);
563 assert(ctx
->base
.saved_num_sampler_views
!= ~0);
566 static void blitter_restore_textures(struct blitter_context_priv
*ctx
)
568 struct pipe_context
*pipe
= ctx
->base
.pipe
;
571 /* Fragment sampler states. */
572 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0,
573 ctx
->base
.saved_num_sampler_states
,
574 ctx
->base
.saved_sampler_states
);
576 ctx
->base
.saved_num_sampler_states
= ~0;
578 /* Fragment sampler views. */
579 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
580 ctx
->base
.saved_num_sampler_views
,
581 ctx
->base
.saved_sampler_views
);
583 for (i
= 0; i
< ctx
->base
.saved_num_sampler_views
; i
++)
584 pipe_sampler_view_reference(&ctx
->base
.saved_sampler_views
[i
], NULL
);
586 ctx
->base
.saved_num_sampler_views
= ~0;
589 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
590 int x1
, int y1
, int x2
, int y2
,
595 /* set vertex positions */
596 ctx
->vertices
[0][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v0.x*/
597 ctx
->vertices
[0][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v0.y*/
599 ctx
->vertices
[1][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v1.x*/
600 ctx
->vertices
[1][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v1.y*/
602 ctx
->vertices
[2][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v2.x*/
603 ctx
->vertices
[2][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v2.y*/
605 ctx
->vertices
[3][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v3.x*/
606 ctx
->vertices
[3][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v3.y*/
608 for (i
= 0; i
< 4; i
++)
609 ctx
->vertices
[i
][0][2] = depth
; /*z*/
612 ctx
->viewport
.scale
[0] = 0.5f
* ctx
->dst_width
;
613 ctx
->viewport
.scale
[1] = 0.5f
* ctx
->dst_height
;
614 ctx
->viewport
.scale
[2] = 1.0f
;
615 ctx
->viewport
.scale
[3] = 1.0f
;
616 ctx
->viewport
.translate
[0] = 0.5f
* ctx
->dst_width
;
617 ctx
->viewport
.translate
[1] = 0.5f
* ctx
->dst_height
;
618 ctx
->viewport
.translate
[2] = 0.0f
;
619 ctx
->viewport
.translate
[3] = 0.0f
;
620 ctx
->base
.pipe
->set_viewport_states(ctx
->base
.pipe
, 0, 1, &ctx
->viewport
);
623 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
624 const union pipe_color_union
*color
)
629 for (i
= 0; i
< 4; i
++) {
630 uint32_t *uiverts
= (uint32_t *)ctx
->vertices
[i
][1];
631 uiverts
[0] = color
->ui
[0];
632 uiverts
[1] = color
->ui
[1];
633 uiverts
[2] = color
->ui
[2];
634 uiverts
[3] = color
->ui
[3];
637 for (i
= 0; i
< 4; i
++) {
638 ctx
->vertices
[i
][1][0] = 0;
639 ctx
->vertices
[i
][1][1] = 0;
640 ctx
->vertices
[i
][1][2] = 0;
641 ctx
->vertices
[i
][1][3] = 0;
646 static void get_texcoords(struct pipe_sampler_view
*src
,
647 unsigned src_width0
, unsigned src_height0
,
648 int x1
, int y1
, int x2
, int y2
,
651 struct pipe_resource
*tex
= src
->texture
;
652 unsigned level
= src
->u
.tex
.first_level
;
653 boolean normalized
= tex
->target
!= PIPE_TEXTURE_RECT
&&
654 tex
->nr_samples
<= 1;
657 out
[0] = x1
/ (float)u_minify(src_width0
, level
);
658 out
[1] = y1
/ (float)u_minify(src_height0
, level
);
659 out
[2] = x2
/ (float)u_minify(src_width0
, level
);
660 out
[3] = y2
/ (float)u_minify(src_height0
, level
);
669 static void set_texcoords_in_vertices(const float coord
[4],
670 float *out
, unsigned stride
)
672 out
[0] = coord
[0]; /*t0.s*/
673 out
[1] = coord
[1]; /*t0.t*/
675 out
[0] = coord
[2]; /*t1.s*/
676 out
[1] = coord
[1]; /*t1.t*/
678 out
[0] = coord
[2]; /*t2.s*/
679 out
[1] = coord
[3]; /*t2.t*/
681 out
[0] = coord
[0]; /*t3.s*/
682 out
[1] = coord
[3]; /*t3.t*/
685 static void blitter_set_texcoords(struct blitter_context_priv
*ctx
,
686 struct pipe_sampler_view
*src
,
687 unsigned src_width0
, unsigned src_height0
,
688 unsigned layer
, unsigned sample
,
689 int x1
, int y1
, int x2
, int y2
)
693 float face_coord
[4][2];
695 get_texcoords(src
, src_width0
, src_height0
, x1
, y1
, x2
, y2
, coord
);
697 if (src
->texture
->target
== PIPE_TEXTURE_CUBE
||
698 src
->texture
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
699 set_texcoords_in_vertices(coord
, &face_coord
[0][0], 2);
700 util_map_texcoords2d_onto_cubemap(layer
% 6,
701 /* pointer, stride in floats */
702 &face_coord
[0][0], 2,
703 &ctx
->vertices
[0][1][0], 8,
706 set_texcoords_in_vertices(coord
, &ctx
->vertices
[0][1][0], 8);
710 switch (src
->texture
->target
) {
711 case PIPE_TEXTURE_3D
:
713 float r
= layer
/ (float)u_minify(src
->texture
->depth0
,
714 src
->u
.tex
.first_level
);
715 for (i
= 0; i
< 4; i
++)
716 ctx
->vertices
[i
][1][2] = r
; /*r*/
720 case PIPE_TEXTURE_1D_ARRAY
:
721 for (i
= 0; i
< 4; i
++)
722 ctx
->vertices
[i
][1][1] = (float) layer
; /*t*/
725 case PIPE_TEXTURE_2D_ARRAY
:
726 for (i
= 0; i
< 4; i
++) {
727 ctx
->vertices
[i
][1][2] = (float) layer
; /*r*/
728 ctx
->vertices
[i
][1][3] = (float) sample
; /*q*/
732 case PIPE_TEXTURE_CUBE_ARRAY
:
733 for (i
= 0; i
< 4; i
++)
734 ctx
->vertices
[i
][1][3] = (float) (layer
/ 6); /*w*/
737 case PIPE_TEXTURE_2D
:
738 for (i
= 0; i
< 4; i
++) {
739 ctx
->vertices
[i
][1][3] = (float) sample
; /*r*/
747 static void blitter_set_dst_dimensions(struct blitter_context_priv
*ctx
,
748 unsigned width
, unsigned height
)
750 ctx
->dst_width
= width
;
751 ctx
->dst_height
= height
;
754 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
755 enum pipe_format format
,
756 enum pipe_texture_target target
,
757 unsigned src_nr_samples
,
758 unsigned dst_nr_samples
,
761 struct pipe_context
*pipe
= ctx
->base
.pipe
;
762 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, src_nr_samples
);
764 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
766 if (src_nr_samples
> 1) {
769 if (dst_nr_samples
<= 1) {
770 /* The destination has one sample, so we'll do color resolve. */
771 boolean is_uint
, is_sint
;
772 unsigned index
= GET_MSAA_RESOLVE_FS_IDX(src_nr_samples
);
774 is_uint
= util_format_is_pure_uint(format
);
775 is_sint
= util_format_is_pure_sint(format
);
780 shader
= &ctx
->fs_resolve_uint
[target
][index
][filter
];
782 shader
= &ctx
->fs_resolve_sint
[target
][index
][filter
];
784 shader
= &ctx
->fs_resolve
[target
][index
][filter
];
787 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
788 *shader
= util_make_fs_msaa_resolve_bilinear(pipe
, tgsi_tex
,
793 *shader
= util_make_fs_msaa_resolve(pipe
, tgsi_tex
,
800 /* The destination has multiple samples, we'll do
801 * an MSAA->MSAA copy.
803 shader
= &ctx
->fs_texfetch_col_msaa
[target
];
805 /* Create the fragment shader on-demand. */
807 *shader
= util_make_fs_blit_msaa_color(pipe
, tgsi_tex
);
813 void **shader
= &ctx
->fs_texfetch_col
[target
];
815 /* Create the fragment shader on-demand. */
817 *shader
= util_make_fragment_tex_shader(pipe
, tgsi_tex
,
818 TGSI_INTERPOLATE_LINEAR
);
826 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
827 enum pipe_texture_target target
,
830 struct pipe_context
*pipe
= ctx
->base
.pipe
;
832 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
834 if (nr_samples
> 1) {
835 void **shader
= &ctx
->fs_texfetch_depth_msaa
[target
];
837 /* Create the fragment shader on-demand. */
839 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
,
843 util_make_fs_blit_msaa_depth(pipe
, tgsi_tex
);
848 void **shader
= &ctx
->fs_texfetch_depth
[target
];
850 /* Create the fragment shader on-demand. */
852 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
855 util_make_fragment_tex_shader_writedepth(pipe
, tgsi_tex
,
856 TGSI_INTERPOLATE_LINEAR
);
864 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv
*ctx
,
865 enum pipe_texture_target target
,
868 struct pipe_context
*pipe
= ctx
->base
.pipe
;
870 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
872 if (nr_samples
> 1) {
873 void **shader
= &ctx
->fs_texfetch_depthstencil_msaa
[target
];
875 /* Create the fragment shader on-demand. */
877 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
,
881 util_make_fs_blit_msaa_depthstencil(pipe
, tgsi_tex
);
886 void **shader
= &ctx
->fs_texfetch_depthstencil
[target
];
888 /* Create the fragment shader on-demand. */
890 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
893 util_make_fragment_tex_shader_writedepthstencil(pipe
, tgsi_tex
,
894 TGSI_INTERPOLATE_LINEAR
);
902 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv
*ctx
,
903 enum pipe_texture_target target
,
906 struct pipe_context
*pipe
= ctx
->base
.pipe
;
908 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
910 if (nr_samples
> 1) {
911 void **shader
= &ctx
->fs_texfetch_stencil_msaa
[target
];
913 /* Create the fragment shader on-demand. */
915 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
,
919 util_make_fs_blit_msaa_stencil(pipe
, tgsi_tex
);
924 void **shader
= &ctx
->fs_texfetch_stencil
[target
];
926 /* Create the fragment shader on-demand. */
928 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
931 util_make_fragment_tex_shader_writestencil(pipe
, tgsi_tex
,
932 TGSI_INTERPOLATE_LINEAR
);
939 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
)
941 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
942 struct pipe_screen
*screen
= blitter
->pipe
->screen
;
943 unsigned samples
, j
, f
, target
, max_samples
;
944 boolean has_arraytex
, has_cubearraytex
;
946 max_samples
= ctx
->has_texture_multisample
? 2 : 1;
947 has_arraytex
= screen
->get_param(screen
,
948 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
) != 0;
949 has_cubearraytex
= screen
->get_param(screen
,
950 PIPE_CAP_CUBE_MAP_ARRAY
) != 0;
952 /* It only matters if i <= 1 or > 1. */
953 for (samples
= 1; samples
<= max_samples
; samples
++) {
954 for (target
= PIPE_TEXTURE_1D
; target
< PIPE_MAX_TEXTURE_TYPES
; target
++) {
956 (target
== PIPE_TEXTURE_1D_ARRAY
||
957 target
== PIPE_TEXTURE_2D_ARRAY
)) {
960 if (!has_cubearraytex
&&
961 (target
== PIPE_TEXTURE_CUBE_ARRAY
))
965 (target
!= PIPE_TEXTURE_2D
&&
966 target
!= PIPE_TEXTURE_2D_ARRAY
))
969 /* If samples == 1, the shaders read one texel. If samples >= 1,
970 * they read one sample.
972 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
, target
,
973 samples
, samples
, 0);
974 blitter_get_fs_texfetch_depth(ctx
, target
, samples
);
975 if (ctx
->has_stencil_export
) {
976 blitter_get_fs_texfetch_depthstencil(ctx
, target
, samples
);
977 blitter_get_fs_texfetch_stencil(ctx
, target
, samples
);
983 /* MSAA resolve shaders. */
984 for (j
= 2; j
< 32; j
++) {
985 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R32_FLOAT
,
987 PIPE_BIND_SAMPLER_VIEW
)) {
991 for (f
= 0; f
< 2; f
++) {
992 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
, target
,
994 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
, target
,
996 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
, target
,
1004 static void blitter_set_common_draw_rect_state(struct blitter_context_priv
*ctx
,
1008 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1010 pipe
->bind_rasterizer_state(pipe
, scissor
? ctx
->rs_state_scissor
1012 pipe
->bind_vs_state(pipe
, vs_layered
? ctx
->vs_layered
: ctx
->vs
);
1013 if (ctx
->has_geometry_shader
)
1014 pipe
->bind_gs_state(pipe
, NULL
);
1015 if (ctx
->has_stream_out
)
1016 pipe
->set_stream_output_targets(pipe
, 0, NULL
, 0);
1019 static void blitter_draw(struct blitter_context_priv
*ctx
,
1020 int x1
, int y1
, int x2
, int y2
, float depth
,
1021 unsigned num_instances
)
1023 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1024 struct pipe_vertex_buffer vb
= {0};
1026 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
1028 vb
.stride
= 8 * sizeof(float);
1030 u_upload_data(ctx
->upload
, 0, sizeof(ctx
->vertices
), ctx
->vertices
,
1031 &vb
.buffer_offset
, &vb
.buffer
);
1032 u_upload_unmap(ctx
->upload
);
1034 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1035 util_draw_arrays_instanced(pipe
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
1037 pipe_resource_reference(&vb
.buffer
, NULL
);
1040 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
1041 int x1
, int y1
, int x2
, int y2
, float depth
,
1042 enum blitter_attrib_type type
,
1043 const union pipe_color_union
*attrib
)
1045 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1048 case UTIL_BLITTER_ATTRIB_COLOR
:
1049 blitter_set_clear_color(ctx
, attrib
);
1052 case UTIL_BLITTER_ATTRIB_TEXCOORD
:
1053 set_texcoords_in_vertices(attrib
->f
, &ctx
->vertices
[0][1][0], 8);
1059 blitter_draw(ctx
, x1
, y1
, x2
, y2
, depth
, 1);
1062 static void *get_clear_blend_state(struct blitter_context_priv
*ctx
,
1063 unsigned clear_buffers
)
1065 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1068 clear_buffers
&= PIPE_CLEAR_COLOR
;
1070 /* Return an existing blend state. */
1072 return ctx
->blend
[0];
1074 index
= GET_CLEAR_BLEND_STATE_IDX(clear_buffers
);
1076 if (ctx
->blend_clear
[index
])
1077 return ctx
->blend_clear
[index
];
1079 /* Create a new one. */
1081 struct pipe_blend_state blend
= {0};
1084 blend
.independent_blend_enable
= 1;
1086 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1087 if (clear_buffers
& (PIPE_CLEAR_COLOR0
<< i
)) {
1088 blend
.rt
[i
].colormask
= PIPE_MASK_RGBA
;
1092 ctx
->blend_clear
[index
] = pipe
->create_blend_state(pipe
, &blend
);
1094 return ctx
->blend_clear
[index
];
1097 static void util_blitter_clear_custom(struct blitter_context
*blitter
,
1098 unsigned width
, unsigned height
,
1099 unsigned num_layers
,
1100 unsigned clear_buffers
,
1101 const union pipe_color_union
*color
,
1102 double depth
, unsigned stencil
,
1103 void *custom_blend
, void *custom_dsa
)
1105 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1106 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1107 struct pipe_stencil_ref sr
= { { 0 } };
1109 assert(ctx
->vs_layered
|| num_layers
<= 1);
1111 blitter_set_running_flag(ctx
);
1112 blitter_check_saved_vertex_states(ctx
);
1113 blitter_check_saved_fragment_states(ctx
);
1114 blitter_disable_render_cond(ctx
);
1118 pipe
->bind_blend_state(pipe
, custom_blend
);
1120 pipe
->bind_blend_state(pipe
, get_clear_blend_state(ctx
, clear_buffers
));
1124 pipe
->bind_depth_stencil_alpha_state(pipe
, custom_dsa
);
1125 } else if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1126 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1127 } else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
1128 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1129 } else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
1130 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1132 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1135 sr
.ref_value
[0] = stencil
& 0xff;
1136 pipe
->set_stencil_ref(pipe
, &sr
);
1138 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1139 ctx
->bind_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
1140 pipe
->set_sample_mask(pipe
, ~0);
1142 blitter_set_dst_dimensions(ctx
, width
, height
);
1144 if (num_layers
> 1 && ctx
->vs_layered
) {
1145 blitter_set_common_draw_rect_state(ctx
, FALSE
, TRUE
);
1146 blitter_set_clear_color(ctx
, color
);
1147 blitter_draw(ctx
, 0, 0, width
, height
, depth
, num_layers
);
1150 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1151 blitter
->draw_rectangle(blitter
, 0, 0, width
, height
, (float) depth
,
1152 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1155 blitter_restore_vertex_states(ctx
);
1156 blitter_restore_fragment_states(ctx
);
1157 blitter_restore_render_cond(ctx
);
1158 blitter_unset_running_flag(ctx
);
1161 void util_blitter_clear(struct blitter_context
*blitter
,
1162 unsigned width
, unsigned height
, unsigned num_layers
,
1163 unsigned clear_buffers
,
1164 const union pipe_color_union
*color
,
1165 double depth
, unsigned stencil
)
1167 util_blitter_clear_custom(blitter
, width
, height
, num_layers
,
1168 clear_buffers
, color
, depth
, stencil
,
1172 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1173 unsigned width
, unsigned height
,
1174 double depth
, void *custom_dsa
)
1176 static const union pipe_color_union color
;
1177 util_blitter_clear_custom(blitter
, width
, height
, 0, 0, &color
, depth
, 0,
1181 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1182 struct pipe_resource
*dst
,
1186 memset(dst_templ
, 0, sizeof(*dst_templ
));
1187 dst_templ
->format
= util_format_linear(dst
->format
);
1188 dst_templ
->u
.tex
.level
= dstlevel
;
1189 dst_templ
->u
.tex
.first_layer
= dstz
;
1190 dst_templ
->u
.tex
.last_layer
= dstz
;
1193 static struct pipe_surface
*
1194 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
1195 struct pipe_surface
*surf
)
1197 struct pipe_surface dst_templ
;
1199 memset(&dst_templ
, 0, sizeof(dst_templ
));
1200 dst_templ
.format
= surf
->format
;
1201 dst_templ
.u
.tex
.level
= surf
->u
.tex
.level
;
1202 dst_templ
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
+ 1;
1203 dst_templ
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
+ 1;
1205 return pipe
->create_surface(pipe
, surf
->texture
, &dst_templ
);
1208 void util_blitter_default_src_texture(struct pipe_sampler_view
*src_templ
,
1209 struct pipe_resource
*src
,
1212 memset(src_templ
, 0, sizeof(*src_templ
));
1213 src_templ
->format
= util_format_linear(src
->format
);
1214 src_templ
->u
.tex
.first_level
= srclevel
;
1215 src_templ
->u
.tex
.last_level
= srclevel
;
1216 src_templ
->u
.tex
.first_layer
= 0;
1217 src_templ
->u
.tex
.last_layer
=
1218 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1219 : src
->array_size
- 1;
1220 src_templ
->swizzle_r
= PIPE_SWIZZLE_RED
;
1221 src_templ
->swizzle_g
= PIPE_SWIZZLE_GREEN
;
1222 src_templ
->swizzle_b
= PIPE_SWIZZLE_BLUE
;
1223 src_templ
->swizzle_a
= PIPE_SWIZZLE_ALPHA
;
1226 static boolean
is_blit_generic_supported(struct blitter_context
*blitter
,
1227 const struct pipe_resource
*dst
,
1228 enum pipe_format dst_format
,
1229 const struct pipe_resource
*src
,
1230 enum pipe_format src_format
,
1233 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1234 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1238 const struct util_format_description
*desc
=
1239 util_format_description(dst_format
);
1240 boolean dst_has_stencil
= util_format_has_stencil(desc
);
1242 /* Stencil export must be supported for stencil copy. */
1243 if ((mask
& PIPE_MASK_S
) && dst_has_stencil
&&
1244 !ctx
->has_stencil_export
) {
1248 if (dst_has_stencil
|| util_format_has_depth(desc
))
1249 bind
= PIPE_BIND_DEPTH_STENCIL
;
1251 bind
= PIPE_BIND_RENDER_TARGET
;
1253 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1254 dst
->nr_samples
, bind
)) {
1260 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1264 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1265 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
)) {
1269 /* Check stencil sampler support for stencil copy. */
1270 if (mask
& PIPE_MASK_S
) {
1271 if (util_format_has_stencil(util_format_description(src_format
))) {
1272 enum pipe_format stencil_format
=
1273 util_format_stencil_only(src_format
);
1274 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1276 if (stencil_format
!= src_format
&&
1277 !screen
->is_format_supported(screen
, stencil_format
,
1278 src
->target
, src
->nr_samples
,
1279 PIPE_BIND_SAMPLER_VIEW
)) {
1289 boolean
util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1290 const struct pipe_resource
*dst
,
1291 const struct pipe_resource
*src
)
1293 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1294 src
, src
->format
, PIPE_MASK_RGBAZS
);
1297 boolean
util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1298 const struct pipe_blit_info
*info
)
1300 return is_blit_generic_supported(blitter
,
1301 info
->dst
.resource
, info
->dst
.format
,
1302 info
->src
.resource
, info
->src
.format
,
1306 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1307 struct pipe_resource
*dst
,
1309 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1310 struct pipe_resource
*src
,
1312 const struct pipe_box
*srcbox
)
1314 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1315 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1316 struct pipe_surface
*dst_view
, dst_templ
;
1317 struct pipe_sampler_view src_templ
, *src_view
;
1318 struct pipe_box dstbox
;
1321 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1323 u_box_3d(dstx
, dsty
, dstz
, abs(srcbox
->width
), abs(srcbox
->height
),
1324 abs(srcbox
->depth
), &dstbox
);
1326 /* Initialize the surface. */
1327 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1328 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1330 /* Initialize the sampler view. */
1331 util_blitter_default_src_texture(&src_templ
, src
, src_level
);
1332 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1335 util_blitter_blit_generic(blitter
, dst_view
, &dstbox
,
1336 src_view
, srcbox
, src
->width0
, src
->height0
,
1337 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
);
1339 pipe_surface_reference(&dst_view
, NULL
);
1340 pipe_sampler_view_reference(&src_view
, NULL
);
1343 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1344 struct pipe_surface
*dst
,
1345 const struct pipe_box
*dstbox
,
1346 struct pipe_sampler_view
*src
,
1347 const struct pipe_box
*srcbox
,
1348 unsigned src_width0
, unsigned src_height0
,
1349 unsigned mask
, unsigned filter
,
1350 const struct pipe_scissor_state
*scissor
)
1352 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1353 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1354 struct pipe_framebuffer_state fb_state
;
1355 enum pipe_texture_target src_target
= src
->texture
->target
;
1356 unsigned src_samples
= src
->texture
->nr_samples
;
1357 unsigned dst_samples
= dst
->texture
->nr_samples
;
1358 boolean has_depth
, has_stencil
, has_color
;
1359 boolean blit_stencil
, blit_depth
, blit_color
;
1360 void *sampler_state
;
1361 const struct util_format_description
*src_desc
=
1362 util_format_description(src
->format
);
1363 const struct util_format_description
*dst_desc
=
1364 util_format_description(dst
->format
);
1366 has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
&&
1367 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1368 has_depth
= util_format_has_depth(src_desc
) &&
1369 util_format_has_depth(dst_desc
);
1370 has_stencil
= util_format_has_stencil(src_desc
) &&
1371 util_format_has_stencil(dst_desc
);
1373 blit_color
= has_color
&& (mask
& PIPE_MASK_RGBA
);
1374 blit_depth
= has_depth
&& (mask
& PIPE_MASK_Z
);
1375 blit_stencil
= has_stencil
&& (mask
& PIPE_MASK_S
) &&
1376 ctx
->has_stencil_export
;
1378 if (!blit_stencil
&& !blit_depth
&& !blit_color
) {
1383 (dstbox
->width
== abs(srcbox
->width
) &&
1384 dstbox
->height
== abs(srcbox
->height
))) {
1385 filter
= PIPE_TEX_FILTER_NEAREST
;
1388 /* Check whether the states are properly saved. */
1389 blitter_set_running_flag(ctx
);
1390 blitter_check_saved_vertex_states(ctx
);
1391 blitter_check_saved_fragment_states(ctx
);
1392 blitter_check_saved_textures(ctx
);
1393 blitter_check_saved_fb_state(ctx
);
1394 blitter_disable_render_cond(ctx
);
1396 /* Initialize framebuffer state. */
1397 fb_state
.width
= dst
->width
;
1398 fb_state
.height
= dst
->height
;
1399 fb_state
.nr_cbufs
= blit_depth
|| blit_stencil
? 0 : 1;
1400 fb_state
.cbufs
[0] = NULL
;
1401 fb_state
.zsbuf
= NULL
;
1403 if (blit_depth
|| blit_stencil
) {
1404 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1406 if (blit_depth
&& blit_stencil
) {
1407 pipe
->bind_depth_stencil_alpha_state(pipe
,
1408 ctx
->dsa_write_depth_stencil
);
1409 ctx
->bind_fs_state(pipe
,
1410 blitter_get_fs_texfetch_depthstencil(ctx
, src_target
,
1412 } else if (blit_depth
) {
1413 pipe
->bind_depth_stencil_alpha_state(pipe
,
1414 ctx
->dsa_write_depth_keep_stencil
);
1415 ctx
->bind_fs_state(pipe
,
1416 blitter_get_fs_texfetch_depth(ctx
, src_target
,
1418 } else { /* is_stencil */
1419 pipe
->bind_depth_stencil_alpha_state(pipe
,
1420 ctx
->dsa_keep_depth_write_stencil
);
1421 ctx
->bind_fs_state(pipe
,
1422 blitter_get_fs_texfetch_stencil(ctx
, src_target
,
1427 pipe
->bind_blend_state(pipe
, ctx
->blend
[mask
& PIPE_MASK_RGBA
]);
1428 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1429 ctx
->bind_fs_state(pipe
,
1430 blitter_get_fs_texfetch_col(ctx
, src
->format
, src_target
,
1431 src_samples
, dst_samples
, filter
));
1434 /* Set the linear filter only for scaled color non-MSAA blits. */
1435 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
1436 if (src_target
== PIPE_TEXTURE_RECT
) {
1437 sampler_state
= ctx
->sampler_state_rect_linear
;
1439 sampler_state
= ctx
->sampler_state_linear
;
1442 if (src_target
== PIPE_TEXTURE_RECT
) {
1443 sampler_state
= ctx
->sampler_state_rect
;
1445 sampler_state
= ctx
->sampler_state
;
1450 if (blit_depth
&& blit_stencil
) {
1451 /* Setup two samplers, one for depth and the other one for stencil. */
1452 struct pipe_sampler_view templ
;
1453 struct pipe_sampler_view
*views
[2];
1454 void *samplers
[2] = {sampler_state
, sampler_state
};
1457 templ
.format
= util_format_stencil_only(templ
.format
);
1458 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1461 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1463 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, views
);
1464 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, samplers
);
1466 pipe_sampler_view_reference(&views
[1], NULL
);
1467 } else if (blit_stencil
) {
1468 /* Set a stencil-only sampler view for it not to sample depth instead. */
1469 struct pipe_sampler_view templ
;
1470 struct pipe_sampler_view
*view
;
1473 templ
.format
= util_format_stencil_only(templ
.format
);
1474 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1476 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1478 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &view
);
1479 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1480 0, 1, &sampler_state
);
1482 pipe_sampler_view_reference(&view
, NULL
);
1484 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src
);
1485 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1486 0, 1, &sampler_state
);
1489 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1491 pipe
->set_scissor_states(pipe
, 0, 1, scissor
);
1494 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
, FALSE
);
1495 blitter_set_dst_dimensions(ctx
, dst
->width
, dst
->height
);
1497 if ((src_target
== PIPE_TEXTURE_1D
||
1498 src_target
== PIPE_TEXTURE_2D
||
1499 src_target
== PIPE_TEXTURE_RECT
) &&
1501 /* Draw the quad with the draw_rectangle callback. */
1503 /* Set texture coordinates. - use a pipe color union
1504 * for interface purposes.
1505 * XXX pipe_color_union is a wrong name since we use that to set
1506 * texture coordinates too.
1508 union pipe_color_union coord
;
1509 get_texcoords(src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1510 srcbox
->x
+srcbox
->width
, srcbox
->y
+srcbox
->height
, coord
.f
);
1512 /* Set framebuffer state. */
1513 if (blit_depth
|| blit_stencil
) {
1514 fb_state
.zsbuf
= dst
;
1516 fb_state
.cbufs
[0] = dst
;
1518 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1521 pipe
->set_sample_mask(pipe
, ~0);
1522 blitter
->draw_rectangle(blitter
, dstbox
->x
, dstbox
->y
,
1523 dstbox
->x
+ dstbox
->width
,
1524 dstbox
->y
+ dstbox
->height
, 0,
1525 UTIL_BLITTER_ATTRIB_TEXCOORD
, &coord
);
1527 /* Draw the quad with the generic codepath. */
1529 for (z
= 0; z
< dstbox
->depth
; z
++) {
1530 struct pipe_surface
*old
;
1532 /* Set framebuffer state. */
1533 if (blit_depth
|| blit_stencil
) {
1534 fb_state
.zsbuf
= dst
;
1536 fb_state
.cbufs
[0] = dst
;
1538 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1540 /* See if we need to blit a multisample or singlesample buffer. */
1541 if (src_samples
== dst_samples
&& dst_samples
> 1) {
1543 unsigned i
, max_sample
= dst_samples
- 1;
1545 for (i
= 0; i
<= max_sample
; i
++) {
1546 pipe
->set_sample_mask(pipe
, 1 << i
);
1547 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1549 i
, srcbox
->x
, srcbox
->y
,
1550 srcbox
->x
+ srcbox
->width
,
1551 srcbox
->y
+ srcbox
->height
);
1552 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1553 dstbox
->x
+ dstbox
->width
,
1554 dstbox
->y
+ dstbox
->height
, 0, 1);
1557 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1558 pipe
->set_sample_mask(pipe
, ~0);
1559 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1561 srcbox
->x
, srcbox
->y
,
1562 srcbox
->x
+ srcbox
->width
,
1563 srcbox
->y
+ srcbox
->height
);
1564 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1565 dstbox
->x
+ dstbox
->width
,
1566 dstbox
->y
+ dstbox
->height
, 0, 1);
1569 /* Get the next surface or (if this is the last iteration)
1570 * just unreference the last one. */
1572 if (z
< dstbox
->depth
-1) {
1573 dst
= ctx
->base
.get_next_surface_layer(ctx
->base
.pipe
, dst
);
1576 pipe_surface_reference(&old
, NULL
);
1581 blitter_restore_vertex_states(ctx
);
1582 blitter_restore_fragment_states(ctx
);
1583 blitter_restore_textures(ctx
);
1584 blitter_restore_fb_state(ctx
);
1586 pipe
->set_scissor_states(pipe
, 0, 1, &ctx
->base
.saved_scissor
);
1588 blitter_restore_render_cond(ctx
);
1589 blitter_unset_running_flag(ctx
);
1593 util_blitter_blit(struct blitter_context
*blitter
,
1594 const struct pipe_blit_info
*info
)
1596 struct pipe_resource
*dst
= info
->dst
.resource
;
1597 struct pipe_resource
*src
= info
->src
.resource
;
1598 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1599 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1600 struct pipe_surface
*dst_view
, dst_templ
;
1601 struct pipe_sampler_view src_templ
, *src_view
;
1603 /* Initialize the surface. */
1604 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
1606 dst_templ
.format
= info
->dst
.format
;
1607 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1609 /* Initialize the sampler view. */
1610 util_blitter_default_src_texture(&src_templ
, src
, info
->src
.level
);
1611 src_templ
.format
= info
->src
.format
;
1612 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1615 util_blitter_blit_generic(blitter
, dst_view
, &info
->dst
.box
,
1616 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
1617 info
->mask
, info
->filter
,
1618 info
->scissor_enable
? &info
->scissor
: NULL
);
1620 pipe_surface_reference(&dst_view
, NULL
);
1621 pipe_sampler_view_reference(&src_view
, NULL
);
1624 /* Clear a region of a color surface to a constant value. */
1625 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
1626 struct pipe_surface
*dstsurf
,
1627 const union pipe_color_union
*color
,
1628 unsigned dstx
, unsigned dsty
,
1629 unsigned width
, unsigned height
)
1631 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1632 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1633 struct pipe_framebuffer_state fb_state
;
1635 assert(dstsurf
->texture
);
1636 if (!dstsurf
->texture
)
1639 /* check the saved state */
1640 blitter_set_running_flag(ctx
);
1641 blitter_check_saved_vertex_states(ctx
);
1642 blitter_check_saved_fragment_states(ctx
);
1643 blitter_check_saved_fb_state(ctx
);
1644 blitter_disable_render_cond(ctx
);
1647 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
]);
1648 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1649 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
1650 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1652 /* set a framebuffer state */
1653 fb_state
.width
= dstsurf
->width
;
1654 fb_state
.height
= dstsurf
->height
;
1655 fb_state
.nr_cbufs
= 1;
1656 fb_state
.cbufs
[0] = dstsurf
;
1658 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1659 pipe
->set_sample_mask(pipe
, ~0);
1661 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1662 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1663 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
1664 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1666 blitter_restore_vertex_states(ctx
);
1667 blitter_restore_fragment_states(ctx
);
1668 blitter_restore_fb_state(ctx
);
1669 blitter_restore_render_cond(ctx
);
1670 blitter_unset_running_flag(ctx
);
1673 /* Clear a region of a depth stencil surface. */
1674 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
1675 struct pipe_surface
*dstsurf
,
1676 unsigned clear_flags
,
1679 unsigned dstx
, unsigned dsty
,
1680 unsigned width
, unsigned height
)
1682 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1683 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1684 struct pipe_framebuffer_state fb_state
;
1685 struct pipe_stencil_ref sr
= { { 0 } };
1687 assert(dstsurf
->texture
);
1688 if (!dstsurf
->texture
)
1691 /* check the saved state */
1692 blitter_set_running_flag(ctx
);
1693 blitter_check_saved_vertex_states(ctx
);
1694 blitter_check_saved_fragment_states(ctx
);
1695 blitter_check_saved_fb_state(ctx
);
1696 blitter_disable_render_cond(ctx
);
1699 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1700 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1701 sr
.ref_value
[0] = stencil
& 0xff;
1702 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1703 pipe
->set_stencil_ref(pipe
, &sr
);
1705 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
1706 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1708 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
1709 sr
.ref_value
[0] = stencil
& 0xff;
1710 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1711 pipe
->set_stencil_ref(pipe
, &sr
);
1714 /* hmm that should be illegal probably, or make it a no-op somewhere */
1715 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1717 ctx
->bind_fs_state(pipe
, ctx
->fs_empty
);
1718 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1720 /* set a framebuffer state */
1721 fb_state
.width
= dstsurf
->width
;
1722 fb_state
.height
= dstsurf
->height
;
1723 fb_state
.nr_cbufs
= 0;
1724 fb_state
.cbufs
[0] = 0;
1725 fb_state
.zsbuf
= dstsurf
;
1726 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1727 pipe
->set_sample_mask(pipe
, ~0);
1729 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1730 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1731 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
,
1733 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
1735 blitter_restore_vertex_states(ctx
);
1736 blitter_restore_fragment_states(ctx
);
1737 blitter_restore_fb_state(ctx
);
1738 blitter_restore_render_cond(ctx
);
1739 blitter_unset_running_flag(ctx
);
1742 /* draw a rectangle across a region using a custom dsa stage - for r600g */
1743 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
1744 struct pipe_surface
*zsurf
,
1745 struct pipe_surface
*cbsurf
,
1746 unsigned sample_mask
,
1747 void *dsa_stage
, float depth
)
1749 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1750 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1751 struct pipe_framebuffer_state fb_state
;
1753 assert(zsurf
->texture
);
1754 if (!zsurf
->texture
)
1757 /* check the saved state */
1758 blitter_set_running_flag(ctx
);
1759 blitter_check_saved_vertex_states(ctx
);
1760 blitter_check_saved_fragment_states(ctx
);
1761 blitter_check_saved_fb_state(ctx
);
1762 blitter_disable_render_cond(ctx
);
1765 pipe
->bind_blend_state(pipe
, cbsurf
? ctx
->blend
[PIPE_MASK_RGBA
] :
1767 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
1768 ctx
->bind_fs_state(pipe
, ctx
->fs_empty
);
1769 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1771 /* set a framebuffer state */
1772 fb_state
.width
= zsurf
->width
;
1773 fb_state
.height
= zsurf
->height
;
1774 fb_state
.nr_cbufs
= 1;
1776 fb_state
.cbufs
[0] = cbsurf
;
1777 fb_state
.nr_cbufs
= 1;
1779 fb_state
.cbufs
[0] = NULL
;
1780 fb_state
.nr_cbufs
= 0;
1782 fb_state
.zsbuf
= zsurf
;
1783 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1784 pipe
->set_sample_mask(pipe
, sample_mask
);
1786 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1787 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
1788 blitter
->draw_rectangle(blitter
, 0, 0, zsurf
->width
, zsurf
->height
, depth
,
1789 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
1791 blitter_restore_vertex_states(ctx
);
1792 blitter_restore_fragment_states(ctx
);
1793 blitter_restore_fb_state(ctx
);
1794 blitter_restore_render_cond(ctx
);
1795 blitter_unset_running_flag(ctx
);
1798 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
1799 struct pipe_resource
*dst
,
1801 struct pipe_resource
*src
,
1805 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1806 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1807 struct pipe_vertex_buffer vb
;
1808 struct pipe_stream_output_target
*so_target
;
1810 if (srcx
>= src
->width0
||
1811 dstx
>= dst
->width0
) {
1814 if (srcx
+ size
> src
->width0
) {
1815 size
= src
->width0
- srcx
;
1817 if (dstx
+ size
> dst
->width0
) {
1818 size
= dst
->width0
- dstx
;
1821 /* Drivers not capable of Stream Out should not call this function
1822 * in the first place. */
1823 assert(ctx
->has_stream_out
);
1825 /* Some alignment is required. */
1826 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
1827 !ctx
->has_stream_out
) {
1828 struct pipe_box box
;
1829 u_box_1d(srcx
, size
, &box
);
1830 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
1834 blitter_set_running_flag(ctx
);
1835 blitter_check_saved_vertex_states(ctx
);
1836 blitter_disable_render_cond(ctx
);
1839 vb
.buffer_offset
= srcx
;
1842 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1843 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[0]);
1844 pipe
->bind_vs_state(pipe
, ctx
->vs_pos_only
);
1845 if (ctx
->has_geometry_shader
)
1846 pipe
->bind_gs_state(pipe
, NULL
);
1847 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
1849 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
1850 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, 0);
1852 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
1854 blitter_restore_vertex_states(ctx
);
1855 blitter_restore_render_cond(ctx
);
1856 blitter_unset_running_flag(ctx
);
1857 pipe_so_target_reference(&so_target
, NULL
);
1860 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
1861 struct pipe_resource
*dst
,
1862 unsigned offset
, unsigned size
,
1863 unsigned num_channels
,
1864 const union pipe_color_union
*clear_value
)
1866 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1867 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1868 struct pipe_vertex_buffer vb
= {0};
1869 struct pipe_stream_output_target
*so_target
;
1871 assert(num_channels
>= 1);
1872 assert(num_channels
<= 4);
1874 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
1876 * R600 uses this to initialize texture resources, so width0 might not be
1877 * what you think it is.
1880 /* Streamout is required. */
1881 if (!ctx
->has_stream_out
) {
1882 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
1886 /* Some alignment is required. */
1887 if (offset
% 4 != 0 || size
% 4 != 0) {
1888 assert(!"Bad alignment in util_blitter_clear_buffer()");
1892 u_upload_data(ctx
->upload
, 0, num_channels
*4, clear_value
,
1893 &vb
.buffer_offset
, &vb
.buffer
);
1896 blitter_set_running_flag(ctx
);
1897 blitter_check_saved_vertex_states(ctx
);
1898 blitter_disable_render_cond(ctx
);
1900 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1901 pipe
->bind_vertex_elements_state(pipe
,
1902 ctx
->velem_state_readbuf
[num_channels
-1]);
1903 pipe
->bind_vs_state(pipe
, ctx
->vs_pos_only
);
1904 if (ctx
->has_geometry_shader
)
1905 pipe
->bind_gs_state(pipe
, NULL
);
1906 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
1908 so_target
= pipe
->create_stream_output_target(pipe
, dst
, offset
, size
);
1909 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, 0);
1911 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
1913 blitter_restore_vertex_states(ctx
);
1914 blitter_restore_render_cond(ctx
);
1915 blitter_unset_running_flag(ctx
);
1916 pipe_so_target_reference(&so_target
, NULL
);
1917 pipe_resource_reference(&vb
.buffer
, NULL
);
1920 /* probably radeon specific */
1921 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
1922 struct pipe_resource
*dst
,
1925 struct pipe_resource
*src
,
1927 unsigned sample_mask
,
1929 enum pipe_format format
)
1931 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1932 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1933 struct pipe_framebuffer_state fb_state
;
1934 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
1936 blitter_set_running_flag(ctx
);
1937 blitter_check_saved_vertex_states(ctx
);
1938 blitter_check_saved_fragment_states(ctx
);
1939 blitter_disable_render_cond(ctx
);
1942 pipe
->bind_blend_state(pipe
, custom_blend
);
1943 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1944 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1945 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
1946 pipe
->set_sample_mask(pipe
, sample_mask
);
1948 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1949 surf_tmpl
.format
= format
;
1950 surf_tmpl
.u
.tex
.level
= dst_level
;
1951 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
1952 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
1954 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
1956 surf_tmpl
.u
.tex
.level
= 0;
1957 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
1958 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
1960 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
1962 /* set a framebuffer state */
1963 fb_state
.width
= src
->width0
;
1964 fb_state
.height
= src
->height0
;
1965 fb_state
.nr_cbufs
= 2;
1966 fb_state
.cbufs
[0] = srcsurf
;
1967 fb_state
.cbufs
[1] = dstsurf
;
1968 fb_state
.zsbuf
= NULL
;
1969 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1971 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1972 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
1973 blitter
->draw_rectangle(blitter
, 0, 0, src
->width0
, src
->height0
,
1975 blitter_restore_fb_state(ctx
);
1976 blitter_restore_vertex_states(ctx
);
1977 blitter_restore_fragment_states(ctx
);
1978 blitter_restore_render_cond(ctx
);
1979 blitter_unset_running_flag(ctx
);
1981 pipe_surface_reference(&srcsurf
, NULL
);
1982 pipe_surface_reference(&dstsurf
, NULL
);
1985 void util_blitter_custom_color(struct blitter_context
*blitter
,
1986 struct pipe_surface
*dstsurf
,
1989 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1990 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1991 struct pipe_framebuffer_state fb_state
;
1993 assert(dstsurf
->texture
);
1994 if (!dstsurf
->texture
)
1997 /* check the saved state */
1998 blitter_set_running_flag(ctx
);
1999 blitter_check_saved_vertex_states(ctx
);
2000 blitter_check_saved_fragment_states(ctx
);
2001 blitter_check_saved_fb_state(ctx
);
2002 blitter_disable_render_cond(ctx
);
2005 pipe
->bind_blend_state(pipe
, custom_blend
? custom_blend
2006 : ctx
->blend
[PIPE_MASK_RGBA
]);
2007 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2008 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
2009 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2010 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2012 /* set a framebuffer state */
2013 fb_state
.width
= dstsurf
->width
;
2014 fb_state
.height
= dstsurf
->height
;
2015 fb_state
.nr_cbufs
= 1;
2016 fb_state
.cbufs
[0] = dstsurf
;
2018 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2019 pipe
->set_sample_mask(pipe
, ~0);
2021 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2022 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2023 blitter
->draw_rectangle(blitter
, 0, 0, dstsurf
->width
, dstsurf
->height
,
2026 blitter_restore_vertex_states(ctx
);
2027 blitter_restore_fragment_states(ctx
);
2028 blitter_restore_fb_state(ctx
);
2029 blitter_restore_render_cond(ctx
);
2030 blitter_unset_running_flag(ctx
);