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
;
134 boolean has_stream_out
;
135 boolean has_stencil_export
;
136 boolean has_texture_multisample
;
137 boolean cached_all_shaders
;
139 /* The Draw module overrides these functions.
140 * Always create the blitter before Draw. */
141 void (*bind_fs_state
)(struct pipe_context
*, void *);
142 void (*delete_fs_state
)(struct pipe_context
*, void *);
145 static struct pipe_surface
*
146 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
147 struct pipe_surface
*surf
);
149 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
)
151 struct blitter_context_priv
*ctx
;
152 struct pipe_blend_state blend
;
153 struct pipe_depth_stencil_alpha_state dsa
;
154 struct pipe_rasterizer_state rs_state
;
155 struct pipe_sampler_state sampler_state
;
156 struct pipe_vertex_element velem
[2];
159 ctx
= CALLOC_STRUCT(blitter_context_priv
);
163 ctx
->base
.pipe
= pipe
;
164 ctx
->base
.draw_rectangle
= util_blitter_draw_rectangle
;
165 ctx
->base
.get_next_surface_layer
= util_blitter_get_next_surface_layer
;
167 ctx
->bind_fs_state
= pipe
->bind_fs_state
;
168 ctx
->delete_fs_state
= pipe
->delete_fs_state
;
170 /* init state objects for them to be considered invalid */
171 ctx
->base
.saved_blend_state
= INVALID_PTR
;
172 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
173 ctx
->base
.saved_rs_state
= INVALID_PTR
;
174 ctx
->base
.saved_fs
= INVALID_PTR
;
175 ctx
->base
.saved_vs
= INVALID_PTR
;
176 ctx
->base
.saved_gs
= INVALID_PTR
;
177 ctx
->base
.saved_velem_state
= INVALID_PTR
;
178 ctx
->base
.saved_fb_state
.nr_cbufs
= ~0;
179 ctx
->base
.saved_num_sampler_views
= ~0;
180 ctx
->base
.saved_num_sampler_states
= ~0;
181 ctx
->base
.saved_num_so_targets
= ~0;
183 ctx
->has_geometry_shader
=
184 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
185 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
186 ctx
->has_stream_out
=
187 pipe
->screen
->get_param(pipe
->screen
,
188 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0;
190 ctx
->has_stencil_export
=
191 pipe
->screen
->get_param(pipe
->screen
,
192 PIPE_CAP_SHADER_STENCIL_EXPORT
);
194 ctx
->has_texture_multisample
=
195 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TEXTURE_MULTISAMPLE
);
197 /* blend state objects */
198 memset(&blend
, 0, sizeof(blend
));
200 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
201 blend
.rt
[0].colormask
= i
;
202 ctx
->blend
[i
] = pipe
->create_blend_state(pipe
, &blend
);
205 /* depth stencil alpha state objects */
206 memset(&dsa
, 0, sizeof(dsa
));
207 ctx
->dsa_keep_depth_stencil
=
208 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
210 dsa
.depth
.enabled
= 1;
211 dsa
.depth
.writemask
= 1;
212 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
213 ctx
->dsa_write_depth_keep_stencil
=
214 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
216 dsa
.stencil
[0].enabled
= 1;
217 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
218 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
219 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
220 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
221 dsa
.stencil
[0].valuemask
= 0xff;
222 dsa
.stencil
[0].writemask
= 0xff;
223 ctx
->dsa_write_depth_stencil
=
224 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
226 dsa
.depth
.enabled
= 0;
227 dsa
.depth
.writemask
= 0;
228 ctx
->dsa_keep_depth_write_stencil
=
229 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
232 memset(&sampler_state
, 0, sizeof(sampler_state
));
233 sampler_state
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
234 sampler_state
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
235 sampler_state
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
236 sampler_state
.normalized_coords
= 1;
237 ctx
->sampler_state
= pipe
->create_sampler_state(pipe
, &sampler_state
);
238 sampler_state
.normalized_coords
= 0;
239 ctx
->sampler_state_rect
= pipe
->create_sampler_state(pipe
, &sampler_state
);
241 sampler_state
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
242 sampler_state
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
243 sampler_state
.normalized_coords
= 1;
244 ctx
->sampler_state_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
245 sampler_state
.normalized_coords
= 0;
246 ctx
->sampler_state_rect_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
248 /* rasterizer state */
249 memset(&rs_state
, 0, sizeof(rs_state
));
250 rs_state
.cull_face
= PIPE_FACE_NONE
;
251 rs_state
.half_pixel_center
= 1;
252 rs_state
.bottom_edge_rule
= 1;
253 rs_state
.flatshade
= 1;
254 rs_state
.depth_clip
= 1;
255 ctx
->rs_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
257 rs_state
.scissor
= 1;
258 ctx
->rs_state_scissor
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
260 if (ctx
->has_stream_out
) {
261 rs_state
.scissor
= 0;
262 rs_state
.rasterizer_discard
= 1;
263 ctx
->rs_discard_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
266 ctx
->base
.vb_slot
= 0; /* 0 for now */
268 /* vertex elements states */
269 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
270 for (i
= 0; i
< 2; i
++) {
271 velem
[i
].src_offset
= i
* 4 * sizeof(float);
272 velem
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
273 velem
[i
].vertex_buffer_index
= ctx
->base
.vb_slot
;
275 ctx
->velem_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
277 if (ctx
->has_stream_out
) {
278 static enum pipe_format formats
[4] = {
279 PIPE_FORMAT_R32_UINT
,
280 PIPE_FORMAT_R32G32_UINT
,
281 PIPE_FORMAT_R32G32B32_UINT
,
282 PIPE_FORMAT_R32G32B32A32_UINT
285 for (i
= 0; i
< 4; i
++) {
286 velem
[0].src_format
= formats
[i
];
287 velem
[0].vertex_buffer_index
= ctx
->base
.vb_slot
;
288 ctx
->velem_state_readbuf
[i
] =
289 pipe
->create_vertex_elements_state(pipe
, 1, &velem
[0]);
294 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_INSTANCEID
) &&
295 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT
);
297 /* set invariant vertex coordinates */
298 for (i
= 0; i
< 4; i
++)
299 ctx
->vertices
[i
][0][3] = 1; /*v.w*/
301 ctx
->upload
= u_upload_create(pipe
, 65536, 4, PIPE_BIND_VERTEX_BUFFER
);
306 static void bind_vs_pos_only(struct blitter_context_priv
*ctx
)
308 struct pipe_context
*pipe
= ctx
->base
.pipe
;
310 if (!ctx
->vs_pos_only
) {
311 struct pipe_stream_output_info so
;
312 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
};
313 const uint semantic_indices
[] = { 0 };
315 memset(&so
, 0, sizeof(so
));
317 so
.output
[0].num_components
= 1;
321 util_make_vertex_passthrough_shader_with_so(pipe
, 1, semantic_names
,
322 semantic_indices
, FALSE
,
326 pipe
->bind_vs_state(pipe
, ctx
->vs_pos_only
);
329 static void bind_vs_passthrough(struct blitter_context_priv
*ctx
)
331 struct pipe_context
*pipe
= ctx
->base
.pipe
;
334 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
335 TGSI_SEMANTIC_GENERIC
};
336 const uint semantic_indices
[] = { 0, 0 };
338 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
339 semantic_indices
, FALSE
);
342 pipe
->bind_vs_state(pipe
, ctx
->vs
);
345 static void bind_vs_layered(struct blitter_context_priv
*ctx
)
347 struct pipe_context
*pipe
= ctx
->base
.pipe
;
349 if (!ctx
->vs_layered
) {
350 ctx
->vs_layered
= util_make_layered_clear_vertex_shader(pipe
);
353 pipe
->bind_vs_state(pipe
, ctx
->vs_layered
);
356 static void bind_fs_empty(struct blitter_context_priv
*ctx
)
358 struct pipe_context
*pipe
= ctx
->base
.pipe
;
360 if (!ctx
->fs_empty
) {
361 assert(!ctx
->cached_all_shaders
);
362 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
365 ctx
->bind_fs_state(pipe
, ctx
->fs_empty
);
368 static void bind_fs_write_one_cbuf(struct blitter_context_priv
*ctx
)
370 struct pipe_context
*pipe
= ctx
->base
.pipe
;
372 if (!ctx
->fs_write_one_cbuf
) {
373 assert(!ctx
->cached_all_shaders
);
374 ctx
->fs_write_one_cbuf
=
375 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
376 TGSI_INTERPOLATE_CONSTANT
, FALSE
);
379 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
382 static void bind_fs_write_all_cbufs(struct blitter_context_priv
*ctx
)
384 struct pipe_context
*pipe
= ctx
->base
.pipe
;
386 if (!ctx
->fs_write_all_cbufs
) {
387 assert(!ctx
->cached_all_shaders
);
388 ctx
->fs_write_all_cbufs
=
389 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
390 TGSI_INTERPOLATE_CONSTANT
, TRUE
);
393 ctx
->bind_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
396 void util_blitter_destroy(struct blitter_context
*blitter
)
398 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
399 struct pipe_context
*pipe
= blitter
->pipe
;
402 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
403 pipe
->delete_blend_state(pipe
, ctx
->blend
[i
]);
405 for (i
= 0; i
< Elements(ctx
->blend_clear
); i
++) {
406 if (ctx
->blend_clear
[i
])
407 pipe
->delete_blend_state(pipe
, ctx
->blend_clear
[i
]);
409 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
410 pipe
->delete_depth_stencil_alpha_state(pipe
,
411 ctx
->dsa_write_depth_keep_stencil
);
412 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
413 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
415 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
);
416 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state_scissor
);
417 if (ctx
->rs_discard_state
)
418 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_discard_state
);
420 pipe
->delete_vs_state(pipe
, ctx
->vs
);
421 if (ctx
->vs_pos_only
)
422 pipe
->delete_vs_state(pipe
, ctx
->vs_pos_only
);
424 pipe
->delete_vs_state(pipe
, ctx
->vs_layered
);
425 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state
);
426 for (i
= 0; i
< 4; i
++) {
427 if (ctx
->velem_state_readbuf
[i
]) {
428 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[i
]);
432 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
433 if (ctx
->fs_texfetch_col
[i
])
434 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[i
]);
435 if (ctx
->fs_texfetch_depth
[i
])
436 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
]);
437 if (ctx
->fs_texfetch_depthstencil
[i
])
438 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil
[i
]);
439 if (ctx
->fs_texfetch_stencil
[i
])
440 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil
[i
]);
442 if (ctx
->fs_texfetch_col_msaa
[i
])
443 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col_msaa
[i
]);
444 if (ctx
->fs_texfetch_depth_msaa
[i
])
445 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth_msaa
[i
]);
446 if (ctx
->fs_texfetch_depthstencil_msaa
[i
])
447 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil_msaa
[i
]);
448 if (ctx
->fs_texfetch_stencil_msaa
[i
])
449 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil_msaa
[i
]);
451 for (j
= 0; j
< Elements(ctx
->fs_resolve
[i
]); j
++)
452 for (f
= 0; f
< 2; f
++)
453 if (ctx
->fs_resolve
[i
][j
][f
])
454 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve
[i
][j
][f
]);
456 for (j
= 0; j
< Elements(ctx
->fs_resolve_sint
[i
]); j
++)
457 for (f
= 0; f
< 2; f
++)
458 if (ctx
->fs_resolve_sint
[i
][j
][f
])
459 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve_sint
[i
][j
][f
]);
461 for (j
= 0; j
< Elements(ctx
->fs_resolve_uint
[i
]); j
++)
462 for (f
= 0; f
< 2; f
++)
463 if (ctx
->fs_resolve_uint
[i
][j
][f
])
464 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve_uint
[i
][j
][f
]);
468 ctx
->delete_fs_state(pipe
, ctx
->fs_empty
);
469 if (ctx
->fs_write_one_cbuf
)
470 ctx
->delete_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
471 if (ctx
->fs_write_all_cbufs
)
472 ctx
->delete_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
474 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect_linear
);
475 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect
);
476 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_linear
);
477 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
);
478 u_upload_destroy(ctx
->upload
);
482 void util_blitter_set_texture_multisample(struct blitter_context
*blitter
,
485 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
487 ctx
->has_texture_multisample
= supported
;
490 static void blitter_set_running_flag(struct blitter_context_priv
*ctx
)
492 if (ctx
->base
.running
) {
493 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
496 ctx
->base
.running
= TRUE
;
499 static void blitter_unset_running_flag(struct blitter_context_priv
*ctx
)
501 if (!ctx
->base
.running
) {
502 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
505 ctx
->base
.running
= FALSE
;
508 static void blitter_check_saved_vertex_states(struct blitter_context_priv
*ctx
)
510 assert(ctx
->base
.saved_velem_state
!= INVALID_PTR
);
511 assert(ctx
->base
.saved_vs
!= INVALID_PTR
);
512 assert(!ctx
->has_geometry_shader
|| ctx
->base
.saved_gs
!= INVALID_PTR
);
513 assert(!ctx
->has_stream_out
|| ctx
->base
.saved_num_so_targets
!= ~0);
514 assert(ctx
->base
.saved_rs_state
!= INVALID_PTR
);
517 static void blitter_restore_vertex_states(struct blitter_context_priv
*ctx
)
519 struct pipe_context
*pipe
= ctx
->base
.pipe
;
523 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1,
524 &ctx
->base
.saved_vertex_buffer
);
525 pipe_resource_reference(&ctx
->base
.saved_vertex_buffer
.buffer
, NULL
);
527 /* Vertex elements. */
528 pipe
->bind_vertex_elements_state(pipe
, ctx
->base
.saved_velem_state
);
529 ctx
->base
.saved_velem_state
= INVALID_PTR
;
532 pipe
->bind_vs_state(pipe
, ctx
->base
.saved_vs
);
533 ctx
->base
.saved_vs
= INVALID_PTR
;
535 /* Geometry shader. */
536 if (ctx
->has_geometry_shader
) {
537 pipe
->bind_gs_state(pipe
, ctx
->base
.saved_gs
);
538 ctx
->base
.saved_gs
= INVALID_PTR
;
541 /* Stream outputs. */
542 if (ctx
->has_stream_out
) {
543 unsigned offsets
[PIPE_MAX_SO_BUFFERS
];
544 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
545 offsets
[i
] = (unsigned)-1;
546 pipe
->set_stream_output_targets(pipe
,
547 ctx
->base
.saved_num_so_targets
,
548 ctx
->base
.saved_so_targets
, offsets
);
550 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
551 pipe_so_target_reference(&ctx
->base
.saved_so_targets
[i
], NULL
);
553 ctx
->base
.saved_num_so_targets
= ~0;
557 pipe
->bind_rasterizer_state(pipe
, ctx
->base
.saved_rs_state
);
558 ctx
->base
.saved_rs_state
= INVALID_PTR
;
561 static void blitter_check_saved_fragment_states(struct blitter_context_priv
*ctx
)
563 assert(ctx
->base
.saved_fs
!= INVALID_PTR
);
564 assert(ctx
->base
.saved_dsa_state
!= INVALID_PTR
);
565 assert(ctx
->base
.saved_blend_state
!= INVALID_PTR
);
568 static void blitter_restore_fragment_states(struct blitter_context_priv
*ctx
)
570 struct pipe_context
*pipe
= ctx
->base
.pipe
;
572 /* Fragment shader. */
573 ctx
->bind_fs_state(pipe
, ctx
->base
.saved_fs
);
574 ctx
->base
.saved_fs
= INVALID_PTR
;
576 /* Depth, stencil, alpha. */
577 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->base
.saved_dsa_state
);
578 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
581 pipe
->bind_blend_state(pipe
, ctx
->base
.saved_blend_state
);
582 ctx
->base
.saved_blend_state
= INVALID_PTR
;
585 if (ctx
->base
.is_sample_mask_saved
) {
586 pipe
->set_sample_mask(pipe
, ctx
->base
.saved_sample_mask
);
587 ctx
->base
.is_sample_mask_saved
= FALSE
;
590 /* Miscellaneous states. */
591 /* XXX check whether these are saved and whether they need to be restored
592 * (depending on the operation) */
593 pipe
->set_stencil_ref(pipe
, &ctx
->base
.saved_stencil_ref
);
594 pipe
->set_viewport_states(pipe
, 0, 1, &ctx
->base
.saved_viewport
);
597 static void blitter_check_saved_fb_state(struct blitter_context_priv
*ctx
)
599 assert(ctx
->base
.saved_fb_state
.nr_cbufs
!= ~0);
602 static void blitter_disable_render_cond(struct blitter_context_priv
*ctx
)
604 struct pipe_context
*pipe
= ctx
->base
.pipe
;
606 if (ctx
->base
.saved_render_cond_query
) {
607 pipe
->render_condition(pipe
, NULL
, FALSE
, 0);
611 static void blitter_restore_render_cond(struct blitter_context_priv
*ctx
)
613 struct pipe_context
*pipe
= ctx
->base
.pipe
;
615 if (ctx
->base
.saved_render_cond_query
) {
616 pipe
->render_condition(pipe
, ctx
->base
.saved_render_cond_query
,
617 ctx
->base
.saved_render_cond_cond
,
618 ctx
->base
.saved_render_cond_mode
);
619 ctx
->base
.saved_render_cond_query
= NULL
;
623 static void blitter_restore_fb_state(struct blitter_context_priv
*ctx
)
625 struct pipe_context
*pipe
= ctx
->base
.pipe
;
627 pipe
->set_framebuffer_state(pipe
, &ctx
->base
.saved_fb_state
);
628 util_unreference_framebuffer_state(&ctx
->base
.saved_fb_state
);
631 static void blitter_check_saved_textures(struct blitter_context_priv
*ctx
)
633 assert(ctx
->base
.saved_num_sampler_states
!= ~0);
634 assert(ctx
->base
.saved_num_sampler_views
!= ~0);
637 static void blitter_restore_textures(struct blitter_context_priv
*ctx
)
639 struct pipe_context
*pipe
= ctx
->base
.pipe
;
642 /* Fragment sampler states. */
643 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0,
644 ctx
->base
.saved_num_sampler_states
,
645 ctx
->base
.saved_sampler_states
);
647 ctx
->base
.saved_num_sampler_states
= ~0;
649 /* Fragment sampler views. */
650 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
651 ctx
->base
.saved_num_sampler_views
,
652 ctx
->base
.saved_sampler_views
);
654 for (i
= 0; i
< ctx
->base
.saved_num_sampler_views
; i
++)
655 pipe_sampler_view_reference(&ctx
->base
.saved_sampler_views
[i
], NULL
);
657 ctx
->base
.saved_num_sampler_views
= ~0;
660 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
661 int x1
, int y1
, int x2
, int y2
,
666 /* set vertex positions */
667 ctx
->vertices
[0][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v0.x*/
668 ctx
->vertices
[0][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v0.y*/
670 ctx
->vertices
[1][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v1.x*/
671 ctx
->vertices
[1][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v1.y*/
673 ctx
->vertices
[2][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v2.x*/
674 ctx
->vertices
[2][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v2.y*/
676 ctx
->vertices
[3][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v3.x*/
677 ctx
->vertices
[3][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v3.y*/
679 for (i
= 0; i
< 4; i
++)
680 ctx
->vertices
[i
][0][2] = depth
; /*z*/
683 ctx
->viewport
.scale
[0] = 0.5f
* ctx
->dst_width
;
684 ctx
->viewport
.scale
[1] = 0.5f
* ctx
->dst_height
;
685 ctx
->viewport
.scale
[2] = 1.0f
;
686 ctx
->viewport
.translate
[0] = 0.5f
* ctx
->dst_width
;
687 ctx
->viewport
.translate
[1] = 0.5f
* ctx
->dst_height
;
688 ctx
->viewport
.translate
[2] = 0.0f
;
689 ctx
->base
.pipe
->set_viewport_states(ctx
->base
.pipe
, 0, 1, &ctx
->viewport
);
692 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
693 const union pipe_color_union
*color
)
698 for (i
= 0; i
< 4; i
++) {
699 uint32_t *uiverts
= (uint32_t *)ctx
->vertices
[i
][1];
700 uiverts
[0] = color
->ui
[0];
701 uiverts
[1] = color
->ui
[1];
702 uiverts
[2] = color
->ui
[2];
703 uiverts
[3] = color
->ui
[3];
706 for (i
= 0; i
< 4; i
++) {
707 ctx
->vertices
[i
][1][0] = 0;
708 ctx
->vertices
[i
][1][1] = 0;
709 ctx
->vertices
[i
][1][2] = 0;
710 ctx
->vertices
[i
][1][3] = 0;
715 static void get_texcoords(struct pipe_sampler_view
*src
,
716 unsigned src_width0
, unsigned src_height0
,
717 int x1
, int y1
, int x2
, int y2
,
720 struct pipe_resource
*tex
= src
->texture
;
721 unsigned level
= src
->u
.tex
.first_level
;
722 boolean normalized
= tex
->target
!= PIPE_TEXTURE_RECT
&&
723 tex
->nr_samples
<= 1;
726 out
[0] = x1
/ (float)u_minify(src_width0
, level
);
727 out
[1] = y1
/ (float)u_minify(src_height0
, level
);
728 out
[2] = x2
/ (float)u_minify(src_width0
, level
);
729 out
[3] = y2
/ (float)u_minify(src_height0
, level
);
738 static void set_texcoords_in_vertices(const float coord
[4],
739 float *out
, unsigned stride
)
741 out
[0] = coord
[0]; /*t0.s*/
742 out
[1] = coord
[1]; /*t0.t*/
744 out
[0] = coord
[2]; /*t1.s*/
745 out
[1] = coord
[1]; /*t1.t*/
747 out
[0] = coord
[2]; /*t2.s*/
748 out
[1] = coord
[3]; /*t2.t*/
750 out
[0] = coord
[0]; /*t3.s*/
751 out
[1] = coord
[3]; /*t3.t*/
754 static void blitter_set_texcoords(struct blitter_context_priv
*ctx
,
755 struct pipe_sampler_view
*src
,
756 unsigned src_width0
, unsigned src_height0
,
757 float layer
, unsigned sample
,
758 int x1
, int y1
, int x2
, int y2
)
762 float face_coord
[4][2];
764 get_texcoords(src
, src_width0
, src_height0
, x1
, y1
, x2
, y2
, coord
);
766 if (src
->texture
->target
== PIPE_TEXTURE_CUBE
||
767 src
->texture
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
768 set_texcoords_in_vertices(coord
, &face_coord
[0][0], 2);
769 util_map_texcoords2d_onto_cubemap((unsigned)layer
% 6,
770 /* pointer, stride in floats */
771 &face_coord
[0][0], 2,
772 &ctx
->vertices
[0][1][0], 8,
775 set_texcoords_in_vertices(coord
, &ctx
->vertices
[0][1][0], 8);
779 switch (src
->texture
->target
) {
780 case PIPE_TEXTURE_3D
:
782 float r
= layer
/ (float)u_minify(src
->texture
->depth0
,
783 src
->u
.tex
.first_level
);
784 for (i
= 0; i
< 4; i
++)
785 ctx
->vertices
[i
][1][2] = r
; /*r*/
789 case PIPE_TEXTURE_1D_ARRAY
:
790 for (i
= 0; i
< 4; i
++)
791 ctx
->vertices
[i
][1][1] = (float) layer
; /*t*/
794 case PIPE_TEXTURE_2D_ARRAY
:
795 for (i
= 0; i
< 4; i
++) {
796 ctx
->vertices
[i
][1][2] = (float) layer
; /*r*/
797 ctx
->vertices
[i
][1][3] = (float) sample
; /*q*/
801 case PIPE_TEXTURE_CUBE_ARRAY
:
802 for (i
= 0; i
< 4; i
++)
803 ctx
->vertices
[i
][1][3] = (float) ((unsigned)layer
/ 6); /*w*/
806 case PIPE_TEXTURE_2D
:
807 for (i
= 0; i
< 4; i
++) {
808 ctx
->vertices
[i
][1][3] = (float) sample
; /*r*/
816 static void blitter_set_dst_dimensions(struct blitter_context_priv
*ctx
,
817 unsigned width
, unsigned height
)
819 ctx
->dst_width
= width
;
820 ctx
->dst_height
= height
;
823 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
824 enum pipe_format format
,
825 enum pipe_texture_target target
,
826 unsigned src_nr_samples
,
827 unsigned dst_nr_samples
,
830 struct pipe_context
*pipe
= ctx
->base
.pipe
;
831 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, src_nr_samples
);
833 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
835 if (src_nr_samples
> 1) {
838 if (dst_nr_samples
<= 1) {
839 /* The destination has one sample, so we'll do color resolve. */
840 boolean is_uint
, is_sint
;
841 unsigned index
= GET_MSAA_RESOLVE_FS_IDX(src_nr_samples
);
843 is_uint
= util_format_is_pure_uint(format
);
844 is_sint
= util_format_is_pure_sint(format
);
849 shader
= &ctx
->fs_resolve_uint
[target
][index
][filter
];
851 shader
= &ctx
->fs_resolve_sint
[target
][index
][filter
];
853 shader
= &ctx
->fs_resolve
[target
][index
][filter
];
856 assert(!ctx
->cached_all_shaders
);
857 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
858 *shader
= util_make_fs_msaa_resolve_bilinear(pipe
, tgsi_tex
,
863 *shader
= util_make_fs_msaa_resolve(pipe
, tgsi_tex
,
870 /* The destination has multiple samples, we'll do
871 * an MSAA->MSAA copy.
873 shader
= &ctx
->fs_texfetch_col_msaa
[target
];
875 /* Create the fragment shader on-demand. */
877 assert(!ctx
->cached_all_shaders
);
878 *shader
= util_make_fs_blit_msaa_color(pipe
, tgsi_tex
);
884 void **shader
= &ctx
->fs_texfetch_col
[target
];
886 /* Create the fragment shader on-demand. */
888 assert(!ctx
->cached_all_shaders
);
889 *shader
= util_make_fragment_tex_shader(pipe
, tgsi_tex
,
890 TGSI_INTERPOLATE_LINEAR
);
898 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
899 enum pipe_texture_target target
,
902 struct pipe_context
*pipe
= ctx
->base
.pipe
;
904 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
906 if (nr_samples
> 1) {
907 void **shader
= &ctx
->fs_texfetch_depth_msaa
[target
];
909 /* Create the fragment shader on-demand. */
912 assert(!ctx
->cached_all_shaders
);
913 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
914 *shader
= util_make_fs_blit_msaa_depth(pipe
, tgsi_tex
);
919 void **shader
= &ctx
->fs_texfetch_depth
[target
];
921 /* Create the fragment shader on-demand. */
924 assert(!ctx
->cached_all_shaders
);
925 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
927 util_make_fragment_tex_shader_writedepth(pipe
, tgsi_tex
,
928 TGSI_INTERPOLATE_LINEAR
);
936 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv
*ctx
,
937 enum pipe_texture_target target
,
940 struct pipe_context
*pipe
= ctx
->base
.pipe
;
942 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
944 if (nr_samples
> 1) {
945 void **shader
= &ctx
->fs_texfetch_depthstencil_msaa
[target
];
947 /* Create the fragment shader on-demand. */
950 assert(!ctx
->cached_all_shaders
);
951 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
952 *shader
= util_make_fs_blit_msaa_depthstencil(pipe
, tgsi_tex
);
957 void **shader
= &ctx
->fs_texfetch_depthstencil
[target
];
959 /* Create the fragment shader on-demand. */
962 assert(!ctx
->cached_all_shaders
);
963 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
965 util_make_fragment_tex_shader_writedepthstencil(pipe
, tgsi_tex
,
966 TGSI_INTERPOLATE_LINEAR
);
974 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv
*ctx
,
975 enum pipe_texture_target target
,
978 struct pipe_context
*pipe
= ctx
->base
.pipe
;
980 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
982 if (nr_samples
> 1) {
983 void **shader
= &ctx
->fs_texfetch_stencil_msaa
[target
];
985 /* Create the fragment shader on-demand. */
988 assert(!ctx
->cached_all_shaders
);
989 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
990 *shader
= util_make_fs_blit_msaa_stencil(pipe
, tgsi_tex
);
995 void **shader
= &ctx
->fs_texfetch_stencil
[target
];
997 /* Create the fragment shader on-demand. */
1000 assert(!ctx
->cached_all_shaders
);
1001 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1003 util_make_fragment_tex_shader_writestencil(pipe
, tgsi_tex
,
1004 TGSI_INTERPOLATE_LINEAR
);
1013 * Generate and save all fragment shaders that we will ever need for
1014 * blitting. Drivers which use the 'draw' fallbacks will typically use
1015 * this to make sure we generate/use shaders that don't go through the
1016 * draw module's wrapper functions.
1018 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
)
1020 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1021 struct pipe_context
*pipe
= blitter
->pipe
;
1022 struct pipe_screen
*screen
= pipe
->screen
;
1023 unsigned samples
, j
, f
, target
, max_samples
;
1024 boolean has_arraytex
, has_cubearraytex
;
1026 max_samples
= ctx
->has_texture_multisample
? 2 : 1;
1027 has_arraytex
= screen
->get_param(screen
,
1028 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
) != 0;
1029 has_cubearraytex
= screen
->get_param(screen
,
1030 PIPE_CAP_CUBE_MAP_ARRAY
) != 0;
1032 /* It only matters if i <= 1 or > 1. */
1033 for (samples
= 1; samples
<= max_samples
; samples
++) {
1034 for (target
= PIPE_TEXTURE_1D
; target
< PIPE_MAX_TEXTURE_TYPES
; target
++) {
1035 if (!has_arraytex
&&
1036 (target
== PIPE_TEXTURE_1D_ARRAY
||
1037 target
== PIPE_TEXTURE_2D_ARRAY
)) {
1040 if (!has_cubearraytex
&&
1041 (target
== PIPE_TEXTURE_CUBE_ARRAY
))
1045 (target
!= PIPE_TEXTURE_2D
&&
1046 target
!= PIPE_TEXTURE_2D_ARRAY
))
1049 /* If samples == 1, the shaders read one texel. If samples >= 1,
1050 * they read one sample.
1052 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
, target
,
1053 samples
, samples
, 0);
1054 blitter_get_fs_texfetch_depth(ctx
, target
, samples
);
1055 if (ctx
->has_stencil_export
) {
1056 blitter_get_fs_texfetch_depthstencil(ctx
, target
, samples
);
1057 blitter_get_fs_texfetch_stencil(ctx
, target
, samples
);
1063 /* MSAA resolve shaders. */
1064 for (j
= 2; j
< 32; j
++) {
1065 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R32_FLOAT
,
1067 PIPE_BIND_SAMPLER_VIEW
)) {
1071 for (f
= 0; f
< 2; f
++) {
1072 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
, target
,
1074 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
, target
,
1076 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
, target
,
1083 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
1085 ctx
->fs_write_one_cbuf
=
1086 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1087 TGSI_INTERPOLATE_CONSTANT
, FALSE
);
1089 ctx
->fs_write_all_cbufs
=
1090 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1091 TGSI_INTERPOLATE_CONSTANT
, TRUE
);
1093 ctx
->cached_all_shaders
= TRUE
;
1096 static void blitter_set_common_draw_rect_state(struct blitter_context_priv
*ctx
,
1100 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1102 pipe
->bind_rasterizer_state(pipe
, scissor
? ctx
->rs_state_scissor
1105 bind_vs_layered(ctx
);
1107 bind_vs_passthrough(ctx
);
1109 if (ctx
->has_geometry_shader
)
1110 pipe
->bind_gs_state(pipe
, NULL
);
1111 if (ctx
->has_stream_out
)
1112 pipe
->set_stream_output_targets(pipe
, 0, NULL
, NULL
);
1115 static void blitter_draw(struct blitter_context_priv
*ctx
,
1116 int x1
, int y1
, int x2
, int y2
, float depth
,
1117 unsigned num_instances
)
1119 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1120 struct pipe_vertex_buffer vb
= {0};
1122 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
1124 vb
.stride
= 8 * sizeof(float);
1126 u_upload_data(ctx
->upload
, 0, sizeof(ctx
->vertices
), ctx
->vertices
,
1127 &vb
.buffer_offset
, &vb
.buffer
);
1128 u_upload_unmap(ctx
->upload
);
1130 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1131 util_draw_arrays_instanced(pipe
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
1133 pipe_resource_reference(&vb
.buffer
, NULL
);
1136 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
1137 int x1
, int y1
, int x2
, int y2
, float depth
,
1138 enum blitter_attrib_type type
,
1139 const union pipe_color_union
*attrib
)
1141 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1144 case UTIL_BLITTER_ATTRIB_COLOR
:
1145 blitter_set_clear_color(ctx
, attrib
);
1148 case UTIL_BLITTER_ATTRIB_TEXCOORD
:
1149 set_texcoords_in_vertices(attrib
->f
, &ctx
->vertices
[0][1][0], 8);
1155 blitter_draw(ctx
, x1
, y1
, x2
, y2
, depth
, 1);
1158 static void *get_clear_blend_state(struct blitter_context_priv
*ctx
,
1159 unsigned clear_buffers
)
1161 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1164 clear_buffers
&= PIPE_CLEAR_COLOR
;
1166 /* Return an existing blend state. */
1168 return ctx
->blend
[0];
1170 index
= GET_CLEAR_BLEND_STATE_IDX(clear_buffers
);
1172 if (ctx
->blend_clear
[index
])
1173 return ctx
->blend_clear
[index
];
1175 /* Create a new one. */
1177 struct pipe_blend_state blend
= {0};
1180 blend
.independent_blend_enable
= 1;
1182 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1183 if (clear_buffers
& (PIPE_CLEAR_COLOR0
<< i
)) {
1184 blend
.rt
[i
].colormask
= PIPE_MASK_RGBA
;
1188 ctx
->blend_clear
[index
] = pipe
->create_blend_state(pipe
, &blend
);
1190 return ctx
->blend_clear
[index
];
1193 static void util_blitter_clear_custom(struct blitter_context
*blitter
,
1194 unsigned width
, unsigned height
,
1195 unsigned num_layers
,
1196 unsigned clear_buffers
,
1197 const union pipe_color_union
*color
,
1198 double depth
, unsigned stencil
,
1199 void *custom_blend
, void *custom_dsa
)
1201 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1202 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1203 struct pipe_stencil_ref sr
= { { 0 } };
1205 assert(ctx
->has_layered
|| num_layers
<= 1);
1207 blitter_set_running_flag(ctx
);
1208 blitter_check_saved_vertex_states(ctx
);
1209 blitter_check_saved_fragment_states(ctx
);
1210 blitter_disable_render_cond(ctx
);
1214 pipe
->bind_blend_state(pipe
, custom_blend
);
1216 pipe
->bind_blend_state(pipe
, get_clear_blend_state(ctx
, clear_buffers
));
1220 pipe
->bind_depth_stencil_alpha_state(pipe
, custom_dsa
);
1221 } else if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1222 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1223 } else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
1224 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1225 } else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
1226 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1228 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1231 sr
.ref_value
[0] = stencil
& 0xff;
1232 pipe
->set_stencil_ref(pipe
, &sr
);
1234 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1235 bind_fs_write_all_cbufs(ctx
);
1236 pipe
->set_sample_mask(pipe
, ~0);
1238 blitter_set_dst_dimensions(ctx
, width
, height
);
1240 if (num_layers
> 1 && ctx
->has_layered
) {
1241 blitter_set_common_draw_rect_state(ctx
, FALSE
, TRUE
);
1242 blitter_set_clear_color(ctx
, color
);
1243 blitter_draw(ctx
, 0, 0, width
, height
, depth
, num_layers
);
1246 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1247 blitter
->draw_rectangle(blitter
, 0, 0, width
, height
, (float) depth
,
1248 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1251 blitter_restore_vertex_states(ctx
);
1252 blitter_restore_fragment_states(ctx
);
1253 blitter_restore_render_cond(ctx
);
1254 blitter_unset_running_flag(ctx
);
1257 void util_blitter_clear(struct blitter_context
*blitter
,
1258 unsigned width
, unsigned height
, unsigned num_layers
,
1259 unsigned clear_buffers
,
1260 const union pipe_color_union
*color
,
1261 double depth
, unsigned stencil
)
1263 util_blitter_clear_custom(blitter
, width
, height
, num_layers
,
1264 clear_buffers
, color
, depth
, stencil
,
1268 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1269 unsigned width
, unsigned height
,
1270 double depth
, void *custom_dsa
)
1272 static const union pipe_color_union color
;
1273 util_blitter_clear_custom(blitter
, width
, height
, 0, 0, &color
, depth
, 0,
1277 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1278 struct pipe_resource
*dst
,
1282 memset(dst_templ
, 0, sizeof(*dst_templ
));
1283 dst_templ
->format
= util_format_linear(dst
->format
);
1284 dst_templ
->u
.tex
.level
= dstlevel
;
1285 dst_templ
->u
.tex
.first_layer
= dstz
;
1286 dst_templ
->u
.tex
.last_layer
= dstz
;
1289 static struct pipe_surface
*
1290 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
1291 struct pipe_surface
*surf
)
1293 struct pipe_surface dst_templ
;
1295 memset(&dst_templ
, 0, sizeof(dst_templ
));
1296 dst_templ
.format
= surf
->format
;
1297 dst_templ
.u
.tex
.level
= surf
->u
.tex
.level
;
1298 dst_templ
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
+ 1;
1299 dst_templ
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
+ 1;
1301 return pipe
->create_surface(pipe
, surf
->texture
, &dst_templ
);
1304 void util_blitter_default_src_texture(struct pipe_sampler_view
*src_templ
,
1305 struct pipe_resource
*src
,
1308 memset(src_templ
, 0, sizeof(*src_templ
));
1309 src_templ
->format
= util_format_linear(src
->format
);
1310 src_templ
->u
.tex
.first_level
= srclevel
;
1311 src_templ
->u
.tex
.last_level
= srclevel
;
1312 src_templ
->u
.tex
.first_layer
= 0;
1313 src_templ
->u
.tex
.last_layer
=
1314 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1315 : src
->array_size
- 1;
1316 src_templ
->swizzle_r
= PIPE_SWIZZLE_RED
;
1317 src_templ
->swizzle_g
= PIPE_SWIZZLE_GREEN
;
1318 src_templ
->swizzle_b
= PIPE_SWIZZLE_BLUE
;
1319 src_templ
->swizzle_a
= PIPE_SWIZZLE_ALPHA
;
1322 static boolean
is_blit_generic_supported(struct blitter_context
*blitter
,
1323 const struct pipe_resource
*dst
,
1324 enum pipe_format dst_format
,
1325 const struct pipe_resource
*src
,
1326 enum pipe_format src_format
,
1329 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1330 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1334 const struct util_format_description
*desc
=
1335 util_format_description(dst_format
);
1336 boolean dst_has_stencil
= util_format_has_stencil(desc
);
1338 /* Stencil export must be supported for stencil copy. */
1339 if ((mask
& PIPE_MASK_S
) && dst_has_stencil
&&
1340 !ctx
->has_stencil_export
) {
1344 if (dst_has_stencil
|| util_format_has_depth(desc
))
1345 bind
= PIPE_BIND_DEPTH_STENCIL
;
1347 bind
= PIPE_BIND_RENDER_TARGET
;
1349 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1350 dst
->nr_samples
, bind
)) {
1356 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1360 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1361 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
)) {
1365 /* Check stencil sampler support for stencil copy. */
1366 if (mask
& PIPE_MASK_S
) {
1367 if (util_format_has_stencil(util_format_description(src_format
))) {
1368 enum pipe_format stencil_format
=
1369 util_format_stencil_only(src_format
);
1370 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1372 if (stencil_format
!= src_format
&&
1373 !screen
->is_format_supported(screen
, stencil_format
,
1374 src
->target
, src
->nr_samples
,
1375 PIPE_BIND_SAMPLER_VIEW
)) {
1385 boolean
util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1386 const struct pipe_resource
*dst
,
1387 const struct pipe_resource
*src
)
1389 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1390 src
, src
->format
, PIPE_MASK_RGBAZS
);
1393 boolean
util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1394 const struct pipe_blit_info
*info
)
1396 return is_blit_generic_supported(blitter
,
1397 info
->dst
.resource
, info
->dst
.format
,
1398 info
->src
.resource
, info
->src
.format
,
1402 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1403 struct pipe_resource
*dst
,
1405 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1406 struct pipe_resource
*src
,
1408 const struct pipe_box
*srcbox
)
1410 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1411 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1412 struct pipe_surface
*dst_view
, dst_templ
;
1413 struct pipe_sampler_view src_templ
, *src_view
;
1414 struct pipe_box dstbox
;
1417 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1419 u_box_3d(dstx
, dsty
, dstz
, abs(srcbox
->width
), abs(srcbox
->height
),
1420 abs(srcbox
->depth
), &dstbox
);
1422 /* Initialize the surface. */
1423 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1424 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1426 /* Initialize the sampler view. */
1427 util_blitter_default_src_texture(&src_templ
, src
, src_level
);
1428 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1431 util_blitter_blit_generic(blitter
, dst_view
, &dstbox
,
1432 src_view
, srcbox
, src
->width0
, src
->height0
,
1433 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
);
1435 pipe_surface_reference(&dst_view
, NULL
);
1436 pipe_sampler_view_reference(&src_view
, NULL
);
1439 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1440 struct pipe_surface
*dst
,
1441 const struct pipe_box
*dstbox
,
1442 struct pipe_sampler_view
*src
,
1443 const struct pipe_box
*srcbox
,
1444 unsigned src_width0
, unsigned src_height0
,
1445 unsigned mask
, unsigned filter
,
1446 const struct pipe_scissor_state
*scissor
)
1448 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1449 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1450 struct pipe_framebuffer_state fb_state
;
1451 enum pipe_texture_target src_target
= src
->texture
->target
;
1452 unsigned src_samples
= src
->texture
->nr_samples
;
1453 unsigned dst_samples
= dst
->texture
->nr_samples
;
1454 boolean has_depth
, has_stencil
, has_color
;
1455 boolean blit_stencil
, blit_depth
, blit_color
;
1456 void *sampler_state
;
1457 const struct util_format_description
*src_desc
=
1458 util_format_description(src
->format
);
1459 const struct util_format_description
*dst_desc
=
1460 util_format_description(dst
->format
);
1462 has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
&&
1463 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1464 has_depth
= util_format_has_depth(src_desc
) &&
1465 util_format_has_depth(dst_desc
);
1466 has_stencil
= util_format_has_stencil(src_desc
) &&
1467 util_format_has_stencil(dst_desc
);
1469 blit_color
= has_color
&& (mask
& PIPE_MASK_RGBA
);
1470 blit_depth
= has_depth
&& (mask
& PIPE_MASK_Z
);
1471 blit_stencil
= has_stencil
&& (mask
& PIPE_MASK_S
) &&
1472 ctx
->has_stencil_export
;
1474 if (!blit_stencil
&& !blit_depth
&& !blit_color
) {
1479 (dstbox
->width
== abs(srcbox
->width
) &&
1480 dstbox
->height
== abs(srcbox
->height
))) {
1481 filter
= PIPE_TEX_FILTER_NEAREST
;
1484 /* Check whether the states are properly saved. */
1485 blitter_set_running_flag(ctx
);
1486 blitter_check_saved_vertex_states(ctx
);
1487 blitter_check_saved_fragment_states(ctx
);
1488 blitter_check_saved_textures(ctx
);
1489 blitter_check_saved_fb_state(ctx
);
1490 blitter_disable_render_cond(ctx
);
1492 /* Initialize framebuffer state. */
1493 fb_state
.width
= dst
->width
;
1494 fb_state
.height
= dst
->height
;
1495 fb_state
.nr_cbufs
= blit_depth
|| blit_stencil
? 0 : 1;
1496 fb_state
.cbufs
[0] = NULL
;
1497 fb_state
.zsbuf
= NULL
;
1499 if (blit_depth
|| blit_stencil
) {
1500 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1502 if (blit_depth
&& blit_stencil
) {
1503 pipe
->bind_depth_stencil_alpha_state(pipe
,
1504 ctx
->dsa_write_depth_stencil
);
1505 ctx
->bind_fs_state(pipe
,
1506 blitter_get_fs_texfetch_depthstencil(ctx
, src_target
,
1508 } else if (blit_depth
) {
1509 pipe
->bind_depth_stencil_alpha_state(pipe
,
1510 ctx
->dsa_write_depth_keep_stencil
);
1511 ctx
->bind_fs_state(pipe
,
1512 blitter_get_fs_texfetch_depth(ctx
, src_target
,
1514 } else { /* is_stencil */
1515 pipe
->bind_depth_stencil_alpha_state(pipe
,
1516 ctx
->dsa_keep_depth_write_stencil
);
1517 ctx
->bind_fs_state(pipe
,
1518 blitter_get_fs_texfetch_stencil(ctx
, src_target
,
1523 pipe
->bind_blend_state(pipe
, ctx
->blend
[mask
& PIPE_MASK_RGBA
]);
1524 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1525 ctx
->bind_fs_state(pipe
,
1526 blitter_get_fs_texfetch_col(ctx
, src
->format
, src_target
,
1527 src_samples
, dst_samples
, filter
));
1530 /* Set the linear filter only for scaled color non-MSAA blits. */
1531 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
1532 if (src_target
== PIPE_TEXTURE_RECT
) {
1533 sampler_state
= ctx
->sampler_state_rect_linear
;
1535 sampler_state
= ctx
->sampler_state_linear
;
1538 if (src_target
== PIPE_TEXTURE_RECT
) {
1539 sampler_state
= ctx
->sampler_state_rect
;
1541 sampler_state
= ctx
->sampler_state
;
1546 if (blit_depth
&& blit_stencil
) {
1547 /* Setup two samplers, one for depth and the other one for stencil. */
1548 struct pipe_sampler_view templ
;
1549 struct pipe_sampler_view
*views
[2];
1550 void *samplers
[2] = {sampler_state
, sampler_state
};
1553 templ
.format
= util_format_stencil_only(templ
.format
);
1554 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1557 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1559 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, views
);
1560 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, samplers
);
1562 pipe_sampler_view_reference(&views
[1], NULL
);
1563 } else if (blit_stencil
) {
1564 /* Set a stencil-only sampler view for it not to sample depth instead. */
1565 struct pipe_sampler_view templ
;
1566 struct pipe_sampler_view
*view
;
1569 templ
.format
= util_format_stencil_only(templ
.format
);
1570 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1572 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1574 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &view
);
1575 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1576 0, 1, &sampler_state
);
1578 pipe_sampler_view_reference(&view
, NULL
);
1580 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src
);
1581 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1582 0, 1, &sampler_state
);
1585 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1587 pipe
->set_scissor_states(pipe
, 0, 1, scissor
);
1590 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
, FALSE
);
1591 blitter_set_dst_dimensions(ctx
, dst
->width
, dst
->height
);
1593 if ((src_target
== PIPE_TEXTURE_1D
||
1594 src_target
== PIPE_TEXTURE_2D
||
1595 src_target
== PIPE_TEXTURE_RECT
) &&
1597 /* Draw the quad with the draw_rectangle callback. */
1599 /* Set texture coordinates. - use a pipe color union
1600 * for interface purposes.
1601 * XXX pipe_color_union is a wrong name since we use that to set
1602 * texture coordinates too.
1604 union pipe_color_union coord
;
1605 get_texcoords(src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1606 srcbox
->x
+srcbox
->width
, srcbox
->y
+srcbox
->height
, coord
.f
);
1608 /* Set framebuffer state. */
1609 if (blit_depth
|| blit_stencil
) {
1610 fb_state
.zsbuf
= dst
;
1612 fb_state
.cbufs
[0] = dst
;
1614 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1617 pipe
->set_sample_mask(pipe
, ~0);
1618 blitter
->draw_rectangle(blitter
, dstbox
->x
, dstbox
->y
,
1619 dstbox
->x
+ dstbox
->width
,
1620 dstbox
->y
+ dstbox
->height
, 0,
1621 UTIL_BLITTER_ATTRIB_TEXCOORD
, &coord
);
1623 /* Draw the quad with the generic codepath. */
1625 for (dst_z
= 0; dst_z
< dstbox
->depth
; dst_z
++) {
1626 struct pipe_surface
*old
;
1627 float dst2src_scale
= srcbox
->depth
/ (float)dstbox
->depth
;
1629 /* Scale Z properly if the blit is scaled.
1631 * When downscaling, we want the coordinates centered, so that
1632 * mipmapping works for 3D textures. For example, when generating
1633 * a 4x4x4 level, this wouldn't average the pixels:
1635 * src Z: 0 1 2 3 4 5 6 7
1638 * Because the pixels are not centered below the pixels of the higher
1639 * level. Therefore, we want this:
1640 * src Z: 0 1 2 3 4 5 6 7
1643 * dst_offset defines the offset needed for centering the pixels and
1644 * it works with any scaling (not just 2x).
1646 float dst_offset
= ((srcbox
->depth
- 1) -
1647 (dstbox
->depth
- 1) * dst2src_scale
) * 0.5;
1648 float src_z
= (dst_z
+ dst_offset
) * dst2src_scale
;
1650 /* Set framebuffer state. */
1651 if (blit_depth
|| blit_stencil
) {
1652 fb_state
.zsbuf
= dst
;
1654 fb_state
.cbufs
[0] = dst
;
1656 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1658 /* See if we need to blit a multisample or singlesample buffer. */
1659 if (src_samples
== dst_samples
&& dst_samples
> 1) {
1661 unsigned i
, max_sample
= dst_samples
- 1;
1663 for (i
= 0; i
<= max_sample
; i
++) {
1664 pipe
->set_sample_mask(pipe
, 1 << i
);
1665 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1667 i
, srcbox
->x
, srcbox
->y
,
1668 srcbox
->x
+ srcbox
->width
,
1669 srcbox
->y
+ srcbox
->height
);
1670 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1671 dstbox
->x
+ dstbox
->width
,
1672 dstbox
->y
+ dstbox
->height
, 0, 1);
1675 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1676 pipe
->set_sample_mask(pipe
, ~0);
1677 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1678 srcbox
->z
+ src_z
, 0,
1679 srcbox
->x
, srcbox
->y
,
1680 srcbox
->x
+ srcbox
->width
,
1681 srcbox
->y
+ srcbox
->height
);
1682 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1683 dstbox
->x
+ dstbox
->width
,
1684 dstbox
->y
+ dstbox
->height
, 0, 1);
1687 /* Get the next surface or (if this is the last iteration)
1688 * just unreference the last one. */
1690 if (dst_z
< dstbox
->depth
-1) {
1691 dst
= ctx
->base
.get_next_surface_layer(ctx
->base
.pipe
, dst
);
1694 pipe_surface_reference(&old
, NULL
);
1699 blitter_restore_vertex_states(ctx
);
1700 blitter_restore_fragment_states(ctx
);
1701 blitter_restore_textures(ctx
);
1702 blitter_restore_fb_state(ctx
);
1704 pipe
->set_scissor_states(pipe
, 0, 1, &ctx
->base
.saved_scissor
);
1706 blitter_restore_render_cond(ctx
);
1707 blitter_unset_running_flag(ctx
);
1711 util_blitter_blit(struct blitter_context
*blitter
,
1712 const struct pipe_blit_info
*info
)
1714 struct pipe_resource
*dst
= info
->dst
.resource
;
1715 struct pipe_resource
*src
= info
->src
.resource
;
1716 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1717 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1718 struct pipe_surface
*dst_view
, dst_templ
;
1719 struct pipe_sampler_view src_templ
, *src_view
;
1721 /* Initialize the surface. */
1722 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
1724 dst_templ
.format
= info
->dst
.format
;
1725 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1727 /* Initialize the sampler view. */
1728 util_blitter_default_src_texture(&src_templ
, src
, info
->src
.level
);
1729 src_templ
.format
= info
->src
.format
;
1730 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1733 util_blitter_blit_generic(blitter
, dst_view
, &info
->dst
.box
,
1734 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
1735 info
->mask
, info
->filter
,
1736 info
->scissor_enable
? &info
->scissor
: NULL
);
1738 pipe_surface_reference(&dst_view
, NULL
);
1739 pipe_sampler_view_reference(&src_view
, NULL
);
1742 /* Clear a region of a color surface to a constant value. */
1743 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
1744 struct pipe_surface
*dstsurf
,
1745 const union pipe_color_union
*color
,
1746 unsigned dstx
, unsigned dsty
,
1747 unsigned width
, unsigned height
)
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(dstsurf
->texture
);
1754 if (!dstsurf
->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
, ctx
->blend
[PIPE_MASK_RGBA
]);
1766 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1767 bind_fs_write_one_cbuf(ctx
);
1768 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1770 /* set a framebuffer state */
1771 fb_state
.width
= dstsurf
->width
;
1772 fb_state
.height
= dstsurf
->height
;
1773 fb_state
.nr_cbufs
= 1;
1774 fb_state
.cbufs
[0] = dstsurf
;
1776 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1777 pipe
->set_sample_mask(pipe
, ~0);
1779 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1780 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1781 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
1782 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1784 blitter_restore_vertex_states(ctx
);
1785 blitter_restore_fragment_states(ctx
);
1786 blitter_restore_fb_state(ctx
);
1787 blitter_restore_render_cond(ctx
);
1788 blitter_unset_running_flag(ctx
);
1791 /* Clear a region of a depth stencil surface. */
1792 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
1793 struct pipe_surface
*dstsurf
,
1794 unsigned clear_flags
,
1797 unsigned dstx
, unsigned dsty
,
1798 unsigned width
, unsigned height
)
1800 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1801 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1802 struct pipe_framebuffer_state fb_state
;
1803 struct pipe_stencil_ref sr
= { { 0 } };
1805 assert(dstsurf
->texture
);
1806 if (!dstsurf
->texture
)
1809 /* check the saved state */
1810 blitter_set_running_flag(ctx
);
1811 blitter_check_saved_vertex_states(ctx
);
1812 blitter_check_saved_fragment_states(ctx
);
1813 blitter_check_saved_fb_state(ctx
);
1814 blitter_disable_render_cond(ctx
);
1817 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1818 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1819 sr
.ref_value
[0] = stencil
& 0xff;
1820 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1821 pipe
->set_stencil_ref(pipe
, &sr
);
1823 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
1824 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1826 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
1827 sr
.ref_value
[0] = stencil
& 0xff;
1828 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1829 pipe
->set_stencil_ref(pipe
, &sr
);
1832 /* hmm that should be illegal probably, or make it a no-op somewhere */
1833 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1836 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1838 /* set a framebuffer state */
1839 fb_state
.width
= dstsurf
->width
;
1840 fb_state
.height
= dstsurf
->height
;
1841 fb_state
.nr_cbufs
= 0;
1842 fb_state
.cbufs
[0] = 0;
1843 fb_state
.zsbuf
= dstsurf
;
1844 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1845 pipe
->set_sample_mask(pipe
, ~0);
1847 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1848 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1849 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
,
1851 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
1853 blitter_restore_vertex_states(ctx
);
1854 blitter_restore_fragment_states(ctx
);
1855 blitter_restore_fb_state(ctx
);
1856 blitter_restore_render_cond(ctx
);
1857 blitter_unset_running_flag(ctx
);
1860 /* draw a rectangle across a region using a custom dsa stage - for r600g */
1861 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
1862 struct pipe_surface
*zsurf
,
1863 struct pipe_surface
*cbsurf
,
1864 unsigned sample_mask
,
1865 void *dsa_stage
, float depth
)
1867 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1868 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1869 struct pipe_framebuffer_state fb_state
;
1871 assert(zsurf
->texture
);
1872 if (!zsurf
->texture
)
1875 /* check the saved state */
1876 blitter_set_running_flag(ctx
);
1877 blitter_check_saved_vertex_states(ctx
);
1878 blitter_check_saved_fragment_states(ctx
);
1879 blitter_check_saved_fb_state(ctx
);
1880 blitter_disable_render_cond(ctx
);
1883 pipe
->bind_blend_state(pipe
, cbsurf
? ctx
->blend
[PIPE_MASK_RGBA
] :
1885 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
1887 bind_fs_write_one_cbuf(ctx
);
1890 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1892 /* set a framebuffer state */
1893 fb_state
.width
= zsurf
->width
;
1894 fb_state
.height
= zsurf
->height
;
1895 fb_state
.nr_cbufs
= 1;
1897 fb_state
.cbufs
[0] = cbsurf
;
1898 fb_state
.nr_cbufs
= 1;
1900 fb_state
.cbufs
[0] = NULL
;
1901 fb_state
.nr_cbufs
= 0;
1903 fb_state
.zsbuf
= zsurf
;
1904 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1905 pipe
->set_sample_mask(pipe
, sample_mask
);
1907 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1908 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
1909 blitter
->draw_rectangle(blitter
, 0, 0, zsurf
->width
, zsurf
->height
, depth
,
1910 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
1912 blitter_restore_vertex_states(ctx
);
1913 blitter_restore_fragment_states(ctx
);
1914 blitter_restore_fb_state(ctx
);
1915 blitter_restore_render_cond(ctx
);
1916 blitter_unset_running_flag(ctx
);
1919 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
1920 struct pipe_resource
*dst
,
1922 struct pipe_resource
*src
,
1926 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1927 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1928 struct pipe_vertex_buffer vb
;
1929 struct pipe_stream_output_target
*so_target
;
1930 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
1932 if (srcx
>= src
->width0
||
1933 dstx
>= dst
->width0
) {
1936 if (srcx
+ size
> src
->width0
) {
1937 size
= src
->width0
- srcx
;
1939 if (dstx
+ size
> dst
->width0
) {
1940 size
= dst
->width0
- dstx
;
1943 /* Drivers not capable of Stream Out should not call this function
1944 * in the first place. */
1945 assert(ctx
->has_stream_out
);
1947 /* Some alignment is required. */
1948 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
1949 !ctx
->has_stream_out
) {
1950 struct pipe_box box
;
1951 u_box_1d(srcx
, size
, &box
);
1952 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
1956 blitter_set_running_flag(ctx
);
1957 blitter_check_saved_vertex_states(ctx
);
1958 blitter_disable_render_cond(ctx
);
1961 vb
.buffer_offset
= srcx
;
1964 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1965 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[0]);
1966 bind_vs_pos_only(ctx
);
1967 if (ctx
->has_geometry_shader
)
1968 pipe
->bind_gs_state(pipe
, NULL
);
1969 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
1971 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
1972 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
1974 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
1976 blitter_restore_vertex_states(ctx
);
1977 blitter_restore_render_cond(ctx
);
1978 blitter_unset_running_flag(ctx
);
1979 pipe_so_target_reference(&so_target
, NULL
);
1982 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
1983 struct pipe_resource
*dst
,
1984 unsigned offset
, unsigned size
,
1985 unsigned num_channels
,
1986 const union pipe_color_union
*clear_value
)
1988 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1989 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1990 struct pipe_vertex_buffer vb
= {0};
1991 struct pipe_stream_output_target
*so_target
;
1992 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
1994 assert(num_channels
>= 1);
1995 assert(num_channels
<= 4);
1997 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
1999 * R600 uses this to initialize texture resources, so width0 might not be
2000 * what you think it is.
2003 /* Streamout is required. */
2004 if (!ctx
->has_stream_out
) {
2005 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2009 /* Some alignment is required. */
2010 if (offset
% 4 != 0 || size
% 4 != 0) {
2011 assert(!"Bad alignment in util_blitter_clear_buffer()");
2015 u_upload_data(ctx
->upload
, 0, num_channels
*4, clear_value
,
2016 &vb
.buffer_offset
, &vb
.buffer
);
2019 blitter_set_running_flag(ctx
);
2020 blitter_check_saved_vertex_states(ctx
);
2021 blitter_disable_render_cond(ctx
);
2023 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2024 pipe
->bind_vertex_elements_state(pipe
,
2025 ctx
->velem_state_readbuf
[num_channels
-1]);
2026 bind_vs_pos_only(ctx
);
2027 if (ctx
->has_geometry_shader
)
2028 pipe
->bind_gs_state(pipe
, NULL
);
2029 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2031 so_target
= pipe
->create_stream_output_target(pipe
, dst
, offset
, size
);
2032 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2034 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2036 blitter_restore_vertex_states(ctx
);
2037 blitter_restore_render_cond(ctx
);
2038 blitter_unset_running_flag(ctx
);
2039 pipe_so_target_reference(&so_target
, NULL
);
2040 pipe_resource_reference(&vb
.buffer
, NULL
);
2043 /* probably radeon specific */
2044 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
2045 struct pipe_resource
*dst
,
2048 struct pipe_resource
*src
,
2050 unsigned sample_mask
,
2052 enum pipe_format format
)
2054 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2055 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2056 struct pipe_framebuffer_state fb_state
;
2057 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
2059 blitter_set_running_flag(ctx
);
2060 blitter_check_saved_vertex_states(ctx
);
2061 blitter_check_saved_fragment_states(ctx
);
2062 blitter_disable_render_cond(ctx
);
2065 pipe
->bind_blend_state(pipe
, custom_blend
);
2066 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2067 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2068 bind_fs_write_one_cbuf(ctx
);
2069 pipe
->set_sample_mask(pipe
, sample_mask
);
2071 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
2072 surf_tmpl
.format
= format
;
2073 surf_tmpl
.u
.tex
.level
= dst_level
;
2074 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
2075 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
2077 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
2079 surf_tmpl
.u
.tex
.level
= 0;
2080 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
2081 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
2083 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
2085 /* set a framebuffer state */
2086 fb_state
.width
= src
->width0
;
2087 fb_state
.height
= src
->height0
;
2088 fb_state
.nr_cbufs
= 2;
2089 fb_state
.cbufs
[0] = srcsurf
;
2090 fb_state
.cbufs
[1] = dstsurf
;
2091 fb_state
.zsbuf
= NULL
;
2092 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2094 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2095 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
2096 blitter
->draw_rectangle(blitter
, 0, 0, src
->width0
, src
->height0
,
2098 blitter_restore_fb_state(ctx
);
2099 blitter_restore_vertex_states(ctx
);
2100 blitter_restore_fragment_states(ctx
);
2101 blitter_restore_render_cond(ctx
);
2102 blitter_unset_running_flag(ctx
);
2104 pipe_surface_reference(&srcsurf
, NULL
);
2105 pipe_surface_reference(&dstsurf
, NULL
);
2108 void util_blitter_custom_color(struct blitter_context
*blitter
,
2109 struct pipe_surface
*dstsurf
,
2112 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2113 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2114 struct pipe_framebuffer_state fb_state
;
2116 assert(dstsurf
->texture
);
2117 if (!dstsurf
->texture
)
2120 /* check the saved state */
2121 blitter_set_running_flag(ctx
);
2122 blitter_check_saved_vertex_states(ctx
);
2123 blitter_check_saved_fragment_states(ctx
);
2124 blitter_check_saved_fb_state(ctx
);
2125 blitter_disable_render_cond(ctx
);
2128 pipe
->bind_blend_state(pipe
, custom_blend
? custom_blend
2129 : ctx
->blend
[PIPE_MASK_RGBA
]);
2130 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2131 bind_fs_write_one_cbuf(ctx
);
2132 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2133 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2135 /* set a framebuffer state */
2136 fb_state
.width
= dstsurf
->width
;
2137 fb_state
.height
= dstsurf
->height
;
2138 fb_state
.nr_cbufs
= 1;
2139 fb_state
.cbufs
[0] = dstsurf
;
2141 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2142 pipe
->set_sample_mask(pipe
, ~0);
2144 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2145 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2146 blitter
->draw_rectangle(blitter
, 0, 0, dstsurf
->width
, dstsurf
->height
,
2149 blitter_restore_vertex_states(ctx
);
2150 blitter_restore_fragment_states(ctx
);
2151 blitter_restore_fb_state(ctx
);
2152 blitter_restore_render_cond(ctx
);
2153 blitter_unset_running_flag(ctx
);