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
, &so
);
325 pipe
->bind_vs_state(pipe
, ctx
->vs_pos_only
);
328 static void bind_vs_passthrough(struct blitter_context_priv
*ctx
)
330 struct pipe_context
*pipe
= ctx
->base
.pipe
;
333 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
334 TGSI_SEMANTIC_GENERIC
};
335 const uint semantic_indices
[] = { 0, 0 };
337 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
341 pipe
->bind_vs_state(pipe
, ctx
->vs
);
344 static void bind_vs_layered(struct blitter_context_priv
*ctx
)
346 struct pipe_context
*pipe
= ctx
->base
.pipe
;
348 if (!ctx
->vs_layered
) {
349 ctx
->vs_layered
= util_make_layered_clear_vertex_shader(pipe
);
352 pipe
->bind_vs_state(pipe
, ctx
->vs_layered
);
355 static void bind_fs_empty(struct blitter_context_priv
*ctx
)
357 struct pipe_context
*pipe
= ctx
->base
.pipe
;
359 if (!ctx
->fs_empty
) {
360 assert(!ctx
->cached_all_shaders
);
361 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
364 ctx
->bind_fs_state(pipe
, ctx
->fs_empty
);
367 static void bind_fs_write_one_cbuf(struct blitter_context_priv
*ctx
)
369 struct pipe_context
*pipe
= ctx
->base
.pipe
;
371 if (!ctx
->fs_write_one_cbuf
) {
372 assert(!ctx
->cached_all_shaders
);
373 ctx
->fs_write_one_cbuf
=
374 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
375 TGSI_INTERPOLATE_CONSTANT
, FALSE
);
378 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
381 static void bind_fs_write_all_cbufs(struct blitter_context_priv
*ctx
)
383 struct pipe_context
*pipe
= ctx
->base
.pipe
;
385 if (!ctx
->fs_write_all_cbufs
) {
386 assert(!ctx
->cached_all_shaders
);
387 ctx
->fs_write_all_cbufs
=
388 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
389 TGSI_INTERPOLATE_CONSTANT
, TRUE
);
392 ctx
->bind_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
395 void util_blitter_destroy(struct blitter_context
*blitter
)
397 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
398 struct pipe_context
*pipe
= blitter
->pipe
;
401 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
402 pipe
->delete_blend_state(pipe
, ctx
->blend
[i
]);
404 for (i
= 0; i
< Elements(ctx
->blend_clear
); i
++) {
405 if (ctx
->blend_clear
[i
])
406 pipe
->delete_blend_state(pipe
, ctx
->blend_clear
[i
]);
408 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
409 pipe
->delete_depth_stencil_alpha_state(pipe
,
410 ctx
->dsa_write_depth_keep_stencil
);
411 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
412 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
414 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
);
415 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state_scissor
);
416 if (ctx
->rs_discard_state
)
417 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_discard_state
);
419 pipe
->delete_vs_state(pipe
, ctx
->vs
);
420 if (ctx
->vs_pos_only
)
421 pipe
->delete_vs_state(pipe
, ctx
->vs_pos_only
);
423 pipe
->delete_vs_state(pipe
, ctx
->vs_layered
);
424 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state
);
425 for (i
= 0; i
< 4; i
++) {
426 if (ctx
->velem_state_readbuf
[i
]) {
427 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[i
]);
431 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
432 if (ctx
->fs_texfetch_col
[i
])
433 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[i
]);
434 if (ctx
->fs_texfetch_depth
[i
])
435 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
]);
436 if (ctx
->fs_texfetch_depthstencil
[i
])
437 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil
[i
]);
438 if (ctx
->fs_texfetch_stencil
[i
])
439 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil
[i
]);
441 if (ctx
->fs_texfetch_col_msaa
[i
])
442 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col_msaa
[i
]);
443 if (ctx
->fs_texfetch_depth_msaa
[i
])
444 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth_msaa
[i
]);
445 if (ctx
->fs_texfetch_depthstencil_msaa
[i
])
446 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil_msaa
[i
]);
447 if (ctx
->fs_texfetch_stencil_msaa
[i
])
448 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil_msaa
[i
]);
450 for (j
= 0; j
< Elements(ctx
->fs_resolve
[i
]); j
++)
451 for (f
= 0; f
< 2; f
++)
452 if (ctx
->fs_resolve
[i
][j
][f
])
453 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve
[i
][j
][f
]);
455 for (j
= 0; j
< Elements(ctx
->fs_resolve_sint
[i
]); j
++)
456 for (f
= 0; f
< 2; f
++)
457 if (ctx
->fs_resolve_sint
[i
][j
][f
])
458 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve_sint
[i
][j
][f
]);
460 for (j
= 0; j
< Elements(ctx
->fs_resolve_uint
[i
]); j
++)
461 for (f
= 0; f
< 2; f
++)
462 if (ctx
->fs_resolve_uint
[i
][j
][f
])
463 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve_uint
[i
][j
][f
]);
467 ctx
->delete_fs_state(pipe
, ctx
->fs_empty
);
468 if (ctx
->fs_write_one_cbuf
)
469 ctx
->delete_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
470 if (ctx
->fs_write_all_cbufs
)
471 ctx
->delete_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
473 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect_linear
);
474 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect
);
475 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_linear
);
476 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
);
477 u_upload_destroy(ctx
->upload
);
481 void util_blitter_set_texture_multisample(struct blitter_context
*blitter
,
484 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
486 ctx
->has_texture_multisample
= supported
;
489 static void blitter_set_running_flag(struct blitter_context_priv
*ctx
)
491 if (ctx
->base
.running
) {
492 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
495 ctx
->base
.running
= TRUE
;
498 static void blitter_unset_running_flag(struct blitter_context_priv
*ctx
)
500 if (!ctx
->base
.running
) {
501 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
504 ctx
->base
.running
= FALSE
;
507 static void blitter_check_saved_vertex_states(struct blitter_context_priv
*ctx
)
509 assert(ctx
->base
.saved_velem_state
!= INVALID_PTR
);
510 assert(ctx
->base
.saved_vs
!= INVALID_PTR
);
511 assert(!ctx
->has_geometry_shader
|| ctx
->base
.saved_gs
!= INVALID_PTR
);
512 assert(!ctx
->has_stream_out
|| ctx
->base
.saved_num_so_targets
!= ~0);
513 assert(ctx
->base
.saved_rs_state
!= INVALID_PTR
);
516 static void blitter_restore_vertex_states(struct blitter_context_priv
*ctx
)
518 struct pipe_context
*pipe
= ctx
->base
.pipe
;
522 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1,
523 &ctx
->base
.saved_vertex_buffer
);
524 pipe_resource_reference(&ctx
->base
.saved_vertex_buffer
.buffer
, NULL
);
526 /* Vertex elements. */
527 pipe
->bind_vertex_elements_state(pipe
, ctx
->base
.saved_velem_state
);
528 ctx
->base
.saved_velem_state
= INVALID_PTR
;
531 pipe
->bind_vs_state(pipe
, ctx
->base
.saved_vs
);
532 ctx
->base
.saved_vs
= INVALID_PTR
;
534 /* Geometry shader. */
535 if (ctx
->has_geometry_shader
) {
536 pipe
->bind_gs_state(pipe
, ctx
->base
.saved_gs
);
537 ctx
->base
.saved_gs
= INVALID_PTR
;
540 /* Stream outputs. */
541 if (ctx
->has_stream_out
) {
542 unsigned offsets
[PIPE_MAX_SO_BUFFERS
];
543 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
544 offsets
[i
] = (unsigned)-1;
545 pipe
->set_stream_output_targets(pipe
,
546 ctx
->base
.saved_num_so_targets
,
547 ctx
->base
.saved_so_targets
, offsets
);
549 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
550 pipe_so_target_reference(&ctx
->base
.saved_so_targets
[i
], NULL
);
552 ctx
->base
.saved_num_so_targets
= ~0;
556 pipe
->bind_rasterizer_state(pipe
, ctx
->base
.saved_rs_state
);
557 ctx
->base
.saved_rs_state
= INVALID_PTR
;
560 static void blitter_check_saved_fragment_states(struct blitter_context_priv
*ctx
)
562 assert(ctx
->base
.saved_fs
!= INVALID_PTR
);
563 assert(ctx
->base
.saved_dsa_state
!= INVALID_PTR
);
564 assert(ctx
->base
.saved_blend_state
!= INVALID_PTR
);
567 static void blitter_restore_fragment_states(struct blitter_context_priv
*ctx
)
569 struct pipe_context
*pipe
= ctx
->base
.pipe
;
571 /* Fragment shader. */
572 ctx
->bind_fs_state(pipe
, ctx
->base
.saved_fs
);
573 ctx
->base
.saved_fs
= INVALID_PTR
;
575 /* Depth, stencil, alpha. */
576 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->base
.saved_dsa_state
);
577 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
580 pipe
->bind_blend_state(pipe
, ctx
->base
.saved_blend_state
);
581 ctx
->base
.saved_blend_state
= INVALID_PTR
;
584 if (ctx
->base
.is_sample_mask_saved
) {
585 pipe
->set_sample_mask(pipe
, ctx
->base
.saved_sample_mask
);
586 ctx
->base
.is_sample_mask_saved
= FALSE
;
589 /* Miscellaneous states. */
590 /* XXX check whether these are saved and whether they need to be restored
591 * (depending on the operation) */
592 pipe
->set_stencil_ref(pipe
, &ctx
->base
.saved_stencil_ref
);
593 pipe
->set_viewport_states(pipe
, 0, 1, &ctx
->base
.saved_viewport
);
596 static void blitter_check_saved_fb_state(struct blitter_context_priv
*ctx
)
598 assert(ctx
->base
.saved_fb_state
.nr_cbufs
!= ~0);
601 static void blitter_disable_render_cond(struct blitter_context_priv
*ctx
)
603 struct pipe_context
*pipe
= ctx
->base
.pipe
;
605 if (ctx
->base
.saved_render_cond_query
) {
606 pipe
->render_condition(pipe
, NULL
, FALSE
, 0);
610 static void blitter_restore_render_cond(struct blitter_context_priv
*ctx
)
612 struct pipe_context
*pipe
= ctx
->base
.pipe
;
614 if (ctx
->base
.saved_render_cond_query
) {
615 pipe
->render_condition(pipe
, ctx
->base
.saved_render_cond_query
,
616 ctx
->base
.saved_render_cond_cond
,
617 ctx
->base
.saved_render_cond_mode
);
618 ctx
->base
.saved_render_cond_query
= NULL
;
622 static void blitter_restore_fb_state(struct blitter_context_priv
*ctx
)
624 struct pipe_context
*pipe
= ctx
->base
.pipe
;
626 pipe
->set_framebuffer_state(pipe
, &ctx
->base
.saved_fb_state
);
627 util_unreference_framebuffer_state(&ctx
->base
.saved_fb_state
);
630 static void blitter_check_saved_textures(struct blitter_context_priv
*ctx
)
632 assert(ctx
->base
.saved_num_sampler_states
!= ~0);
633 assert(ctx
->base
.saved_num_sampler_views
!= ~0);
636 static void blitter_restore_textures(struct blitter_context_priv
*ctx
)
638 struct pipe_context
*pipe
= ctx
->base
.pipe
;
641 /* Fragment sampler states. */
642 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0,
643 ctx
->base
.saved_num_sampler_states
,
644 ctx
->base
.saved_sampler_states
);
646 ctx
->base
.saved_num_sampler_states
= ~0;
648 /* Fragment sampler views. */
649 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
650 ctx
->base
.saved_num_sampler_views
,
651 ctx
->base
.saved_sampler_views
);
653 for (i
= 0; i
< ctx
->base
.saved_num_sampler_views
; i
++)
654 pipe_sampler_view_reference(&ctx
->base
.saved_sampler_views
[i
], NULL
);
656 ctx
->base
.saved_num_sampler_views
= ~0;
659 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
660 int x1
, int y1
, int x2
, int y2
,
665 /* set vertex positions */
666 ctx
->vertices
[0][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v0.x*/
667 ctx
->vertices
[0][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v0.y*/
669 ctx
->vertices
[1][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v1.x*/
670 ctx
->vertices
[1][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v1.y*/
672 ctx
->vertices
[2][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v2.x*/
673 ctx
->vertices
[2][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v2.y*/
675 ctx
->vertices
[3][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v3.x*/
676 ctx
->vertices
[3][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v3.y*/
678 for (i
= 0; i
< 4; i
++)
679 ctx
->vertices
[i
][0][2] = depth
; /*z*/
682 ctx
->viewport
.scale
[0] = 0.5f
* ctx
->dst_width
;
683 ctx
->viewport
.scale
[1] = 0.5f
* ctx
->dst_height
;
684 ctx
->viewport
.scale
[2] = 1.0f
;
685 ctx
->viewport
.scale
[3] = 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
->viewport
.translate
[3] = 0.0f
;
690 ctx
->base
.pipe
->set_viewport_states(ctx
->base
.pipe
, 0, 1, &ctx
->viewport
);
693 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
694 const union pipe_color_union
*color
)
699 for (i
= 0; i
< 4; i
++) {
700 uint32_t *uiverts
= (uint32_t *)ctx
->vertices
[i
][1];
701 uiverts
[0] = color
->ui
[0];
702 uiverts
[1] = color
->ui
[1];
703 uiverts
[2] = color
->ui
[2];
704 uiverts
[3] = color
->ui
[3];
707 for (i
= 0; i
< 4; i
++) {
708 ctx
->vertices
[i
][1][0] = 0;
709 ctx
->vertices
[i
][1][1] = 0;
710 ctx
->vertices
[i
][1][2] = 0;
711 ctx
->vertices
[i
][1][3] = 0;
716 static void get_texcoords(struct pipe_sampler_view
*src
,
717 unsigned src_width0
, unsigned src_height0
,
718 int x1
, int y1
, int x2
, int y2
,
721 struct pipe_resource
*tex
= src
->texture
;
722 unsigned level
= src
->u
.tex
.first_level
;
723 boolean normalized
= tex
->target
!= PIPE_TEXTURE_RECT
&&
724 tex
->nr_samples
<= 1;
727 out
[0] = x1
/ (float)u_minify(src_width0
, level
);
728 out
[1] = y1
/ (float)u_minify(src_height0
, level
);
729 out
[2] = x2
/ (float)u_minify(src_width0
, level
);
730 out
[3] = y2
/ (float)u_minify(src_height0
, level
);
739 static void set_texcoords_in_vertices(const float coord
[4],
740 float *out
, unsigned stride
)
742 out
[0] = coord
[0]; /*t0.s*/
743 out
[1] = coord
[1]; /*t0.t*/
745 out
[0] = coord
[2]; /*t1.s*/
746 out
[1] = coord
[1]; /*t1.t*/
748 out
[0] = coord
[2]; /*t2.s*/
749 out
[1] = coord
[3]; /*t2.t*/
751 out
[0] = coord
[0]; /*t3.s*/
752 out
[1] = coord
[3]; /*t3.t*/
755 static void blitter_set_texcoords(struct blitter_context_priv
*ctx
,
756 struct pipe_sampler_view
*src
,
757 unsigned src_width0
, unsigned src_height0
,
758 float layer
, unsigned sample
,
759 int x1
, int y1
, int x2
, int y2
)
763 float face_coord
[4][2];
765 get_texcoords(src
, src_width0
, src_height0
, x1
, y1
, x2
, y2
, coord
);
767 if (src
->texture
->target
== PIPE_TEXTURE_CUBE
||
768 src
->texture
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
769 set_texcoords_in_vertices(coord
, &face_coord
[0][0], 2);
770 util_map_texcoords2d_onto_cubemap((unsigned)layer
% 6,
771 /* pointer, stride in floats */
772 &face_coord
[0][0], 2,
773 &ctx
->vertices
[0][1][0], 8,
776 set_texcoords_in_vertices(coord
, &ctx
->vertices
[0][1][0], 8);
780 switch (src
->texture
->target
) {
781 case PIPE_TEXTURE_3D
:
783 float r
= layer
/ (float)u_minify(src
->texture
->depth0
,
784 src
->u
.tex
.first_level
);
785 for (i
= 0; i
< 4; i
++)
786 ctx
->vertices
[i
][1][2] = r
; /*r*/
790 case PIPE_TEXTURE_1D_ARRAY
:
791 for (i
= 0; i
< 4; i
++)
792 ctx
->vertices
[i
][1][1] = (float) layer
; /*t*/
795 case PIPE_TEXTURE_2D_ARRAY
:
796 for (i
= 0; i
< 4; i
++) {
797 ctx
->vertices
[i
][1][2] = (float) layer
; /*r*/
798 ctx
->vertices
[i
][1][3] = (float) sample
; /*q*/
802 case PIPE_TEXTURE_CUBE_ARRAY
:
803 for (i
= 0; i
< 4; i
++)
804 ctx
->vertices
[i
][1][3] = (float) ((unsigned)layer
/ 6); /*w*/
807 case PIPE_TEXTURE_2D
:
808 for (i
= 0; i
< 4; i
++) {
809 ctx
->vertices
[i
][1][3] = (float) sample
; /*r*/
817 static void blitter_set_dst_dimensions(struct blitter_context_priv
*ctx
,
818 unsigned width
, unsigned height
)
820 ctx
->dst_width
= width
;
821 ctx
->dst_height
= height
;
824 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
825 enum pipe_format format
,
826 enum pipe_texture_target target
,
827 unsigned src_nr_samples
,
828 unsigned dst_nr_samples
,
831 struct pipe_context
*pipe
= ctx
->base
.pipe
;
832 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, src_nr_samples
);
834 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
836 if (src_nr_samples
> 1) {
839 if (dst_nr_samples
<= 1) {
840 /* The destination has one sample, so we'll do color resolve. */
841 boolean is_uint
, is_sint
;
842 unsigned index
= GET_MSAA_RESOLVE_FS_IDX(src_nr_samples
);
844 is_uint
= util_format_is_pure_uint(format
);
845 is_sint
= util_format_is_pure_sint(format
);
850 shader
= &ctx
->fs_resolve_uint
[target
][index
][filter
];
852 shader
= &ctx
->fs_resolve_sint
[target
][index
][filter
];
854 shader
= &ctx
->fs_resolve
[target
][index
][filter
];
857 assert(!ctx
->cached_all_shaders
);
858 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
859 *shader
= util_make_fs_msaa_resolve_bilinear(pipe
, tgsi_tex
,
864 *shader
= util_make_fs_msaa_resolve(pipe
, tgsi_tex
,
871 /* The destination has multiple samples, we'll do
872 * an MSAA->MSAA copy.
874 shader
= &ctx
->fs_texfetch_col_msaa
[target
];
876 /* Create the fragment shader on-demand. */
878 assert(!ctx
->cached_all_shaders
);
879 *shader
= util_make_fs_blit_msaa_color(pipe
, tgsi_tex
);
885 void **shader
= &ctx
->fs_texfetch_col
[target
];
887 /* Create the fragment shader on-demand. */
889 assert(!ctx
->cached_all_shaders
);
890 *shader
= util_make_fragment_tex_shader(pipe
, tgsi_tex
,
891 TGSI_INTERPOLATE_LINEAR
);
899 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
900 enum pipe_texture_target target
,
903 struct pipe_context
*pipe
= ctx
->base
.pipe
;
905 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
907 if (nr_samples
> 1) {
908 void **shader
= &ctx
->fs_texfetch_depth_msaa
[target
];
910 /* Create the fragment shader on-demand. */
913 assert(!ctx
->cached_all_shaders
);
914 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
915 *shader
= util_make_fs_blit_msaa_depth(pipe
, tgsi_tex
);
920 void **shader
= &ctx
->fs_texfetch_depth
[target
];
922 /* Create the fragment shader on-demand. */
925 assert(!ctx
->cached_all_shaders
);
926 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
928 util_make_fragment_tex_shader_writedepth(pipe
, tgsi_tex
,
929 TGSI_INTERPOLATE_LINEAR
);
937 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv
*ctx
,
938 enum pipe_texture_target target
,
941 struct pipe_context
*pipe
= ctx
->base
.pipe
;
943 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
945 if (nr_samples
> 1) {
946 void **shader
= &ctx
->fs_texfetch_depthstencil_msaa
[target
];
948 /* Create the fragment shader on-demand. */
951 assert(!ctx
->cached_all_shaders
);
952 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
953 *shader
= util_make_fs_blit_msaa_depthstencil(pipe
, tgsi_tex
);
958 void **shader
= &ctx
->fs_texfetch_depthstencil
[target
];
960 /* Create the fragment shader on-demand. */
963 assert(!ctx
->cached_all_shaders
);
964 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
966 util_make_fragment_tex_shader_writedepthstencil(pipe
, tgsi_tex
,
967 TGSI_INTERPOLATE_LINEAR
);
975 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv
*ctx
,
976 enum pipe_texture_target target
,
979 struct pipe_context
*pipe
= ctx
->base
.pipe
;
981 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
983 if (nr_samples
> 1) {
984 void **shader
= &ctx
->fs_texfetch_stencil_msaa
[target
];
986 /* Create the fragment shader on-demand. */
989 assert(!ctx
->cached_all_shaders
);
990 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, nr_samples
);
991 *shader
= util_make_fs_blit_msaa_stencil(pipe
, tgsi_tex
);
996 void **shader
= &ctx
->fs_texfetch_stencil
[target
];
998 /* Create the fragment shader on-demand. */
1001 assert(!ctx
->cached_all_shaders
);
1002 tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
1004 util_make_fragment_tex_shader_writestencil(pipe
, tgsi_tex
,
1005 TGSI_INTERPOLATE_LINEAR
);
1014 * Generate and save all fragment shaders that we will ever need for
1015 * blitting. Drivers which use the 'draw' fallbacks will typically use
1016 * this to make sure we generate/use shaders that don't go through the
1017 * draw module's wrapper functions.
1019 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
)
1021 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1022 struct pipe_context
*pipe
= blitter
->pipe
;
1023 struct pipe_screen
*screen
= pipe
->screen
;
1024 unsigned samples
, j
, f
, target
, max_samples
;
1025 boolean has_arraytex
, has_cubearraytex
;
1027 max_samples
= ctx
->has_texture_multisample
? 2 : 1;
1028 has_arraytex
= screen
->get_param(screen
,
1029 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
) != 0;
1030 has_cubearraytex
= screen
->get_param(screen
,
1031 PIPE_CAP_CUBE_MAP_ARRAY
) != 0;
1033 /* It only matters if i <= 1 or > 1. */
1034 for (samples
= 1; samples
<= max_samples
; samples
++) {
1035 for (target
= PIPE_TEXTURE_1D
; target
< PIPE_MAX_TEXTURE_TYPES
; target
++) {
1036 if (!has_arraytex
&&
1037 (target
== PIPE_TEXTURE_1D_ARRAY
||
1038 target
== PIPE_TEXTURE_2D_ARRAY
)) {
1041 if (!has_cubearraytex
&&
1042 (target
== PIPE_TEXTURE_CUBE_ARRAY
))
1046 (target
!= PIPE_TEXTURE_2D
&&
1047 target
!= PIPE_TEXTURE_2D_ARRAY
))
1050 /* If samples == 1, the shaders read one texel. If samples >= 1,
1051 * they read one sample.
1053 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
, target
,
1054 samples
, samples
, 0);
1055 blitter_get_fs_texfetch_depth(ctx
, target
, samples
);
1056 if (ctx
->has_stencil_export
) {
1057 blitter_get_fs_texfetch_depthstencil(ctx
, target
, samples
);
1058 blitter_get_fs_texfetch_stencil(ctx
, target
, samples
);
1064 /* MSAA resolve shaders. */
1065 for (j
= 2; j
< 32; j
++) {
1066 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R32_FLOAT
,
1068 PIPE_BIND_SAMPLER_VIEW
)) {
1072 for (f
= 0; f
< 2; f
++) {
1073 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
, target
,
1075 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
, target
,
1077 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
, target
,
1084 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
1086 ctx
->fs_write_one_cbuf
=
1087 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1088 TGSI_INTERPOLATE_CONSTANT
, FALSE
);
1090 ctx
->fs_write_all_cbufs
=
1091 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1092 TGSI_INTERPOLATE_CONSTANT
, TRUE
);
1094 ctx
->cached_all_shaders
= TRUE
;
1097 static void blitter_set_common_draw_rect_state(struct blitter_context_priv
*ctx
,
1101 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1103 pipe
->bind_rasterizer_state(pipe
, scissor
? ctx
->rs_state_scissor
1106 bind_vs_layered(ctx
);
1108 bind_vs_passthrough(ctx
);
1110 if (ctx
->has_geometry_shader
)
1111 pipe
->bind_gs_state(pipe
, NULL
);
1112 if (ctx
->has_stream_out
)
1113 pipe
->set_stream_output_targets(pipe
, 0, NULL
, NULL
);
1116 static void blitter_draw(struct blitter_context_priv
*ctx
,
1117 int x1
, int y1
, int x2
, int y2
, float depth
,
1118 unsigned num_instances
)
1120 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1121 struct pipe_vertex_buffer vb
= {0};
1123 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
1125 vb
.stride
= 8 * sizeof(float);
1127 u_upload_data(ctx
->upload
, 0, sizeof(ctx
->vertices
), ctx
->vertices
,
1128 &vb
.buffer_offset
, &vb
.buffer
);
1129 u_upload_unmap(ctx
->upload
);
1131 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1132 util_draw_arrays_instanced(pipe
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
1134 pipe_resource_reference(&vb
.buffer
, NULL
);
1137 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
1138 int x1
, int y1
, int x2
, int y2
, float depth
,
1139 enum blitter_attrib_type type
,
1140 const union pipe_color_union
*attrib
)
1142 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1145 case UTIL_BLITTER_ATTRIB_COLOR
:
1146 blitter_set_clear_color(ctx
, attrib
);
1149 case UTIL_BLITTER_ATTRIB_TEXCOORD
:
1150 set_texcoords_in_vertices(attrib
->f
, &ctx
->vertices
[0][1][0], 8);
1156 blitter_draw(ctx
, x1
, y1
, x2
, y2
, depth
, 1);
1159 static void *get_clear_blend_state(struct blitter_context_priv
*ctx
,
1160 unsigned clear_buffers
)
1162 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1165 clear_buffers
&= PIPE_CLEAR_COLOR
;
1167 /* Return an existing blend state. */
1169 return ctx
->blend
[0];
1171 index
= GET_CLEAR_BLEND_STATE_IDX(clear_buffers
);
1173 if (ctx
->blend_clear
[index
])
1174 return ctx
->blend_clear
[index
];
1176 /* Create a new one. */
1178 struct pipe_blend_state blend
= {0};
1181 blend
.independent_blend_enable
= 1;
1183 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1184 if (clear_buffers
& (PIPE_CLEAR_COLOR0
<< i
)) {
1185 blend
.rt
[i
].colormask
= PIPE_MASK_RGBA
;
1189 ctx
->blend_clear
[index
] = pipe
->create_blend_state(pipe
, &blend
);
1191 return ctx
->blend_clear
[index
];
1194 static void util_blitter_clear_custom(struct blitter_context
*blitter
,
1195 unsigned width
, unsigned height
,
1196 unsigned num_layers
,
1197 unsigned clear_buffers
,
1198 const union pipe_color_union
*color
,
1199 double depth
, unsigned stencil
,
1200 void *custom_blend
, void *custom_dsa
)
1202 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1203 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1204 struct pipe_stencil_ref sr
= { { 0 } };
1206 assert(ctx
->has_layered
|| num_layers
<= 1);
1208 blitter_set_running_flag(ctx
);
1209 blitter_check_saved_vertex_states(ctx
);
1210 blitter_check_saved_fragment_states(ctx
);
1211 blitter_disable_render_cond(ctx
);
1215 pipe
->bind_blend_state(pipe
, custom_blend
);
1217 pipe
->bind_blend_state(pipe
, get_clear_blend_state(ctx
, clear_buffers
));
1221 pipe
->bind_depth_stencil_alpha_state(pipe
, custom_dsa
);
1222 } else if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1223 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1224 } else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
1225 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1226 } else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
1227 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1229 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1232 sr
.ref_value
[0] = stencil
& 0xff;
1233 pipe
->set_stencil_ref(pipe
, &sr
);
1235 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1236 bind_fs_write_all_cbufs(ctx
);
1237 pipe
->set_sample_mask(pipe
, ~0);
1239 blitter_set_dst_dimensions(ctx
, width
, height
);
1241 if (num_layers
> 1 && ctx
->has_layered
) {
1242 blitter_set_common_draw_rect_state(ctx
, FALSE
, TRUE
);
1243 blitter_set_clear_color(ctx
, color
);
1244 blitter_draw(ctx
, 0, 0, width
, height
, depth
, num_layers
);
1247 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1248 blitter
->draw_rectangle(blitter
, 0, 0, width
, height
, (float) depth
,
1249 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1252 blitter_restore_vertex_states(ctx
);
1253 blitter_restore_fragment_states(ctx
);
1254 blitter_restore_render_cond(ctx
);
1255 blitter_unset_running_flag(ctx
);
1258 void util_blitter_clear(struct blitter_context
*blitter
,
1259 unsigned width
, unsigned height
, unsigned num_layers
,
1260 unsigned clear_buffers
,
1261 const union pipe_color_union
*color
,
1262 double depth
, unsigned stencil
)
1264 util_blitter_clear_custom(blitter
, width
, height
, num_layers
,
1265 clear_buffers
, color
, depth
, stencil
,
1269 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1270 unsigned width
, unsigned height
,
1271 double depth
, void *custom_dsa
)
1273 static const union pipe_color_union color
;
1274 util_blitter_clear_custom(blitter
, width
, height
, 0, 0, &color
, depth
, 0,
1278 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1279 struct pipe_resource
*dst
,
1283 memset(dst_templ
, 0, sizeof(*dst_templ
));
1284 dst_templ
->format
= util_format_linear(dst
->format
);
1285 dst_templ
->u
.tex
.level
= dstlevel
;
1286 dst_templ
->u
.tex
.first_layer
= dstz
;
1287 dst_templ
->u
.tex
.last_layer
= dstz
;
1290 static struct pipe_surface
*
1291 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
1292 struct pipe_surface
*surf
)
1294 struct pipe_surface dst_templ
;
1296 memset(&dst_templ
, 0, sizeof(dst_templ
));
1297 dst_templ
.format
= surf
->format
;
1298 dst_templ
.u
.tex
.level
= surf
->u
.tex
.level
;
1299 dst_templ
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
+ 1;
1300 dst_templ
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
+ 1;
1302 return pipe
->create_surface(pipe
, surf
->texture
, &dst_templ
);
1305 void util_blitter_default_src_texture(struct pipe_sampler_view
*src_templ
,
1306 struct pipe_resource
*src
,
1309 memset(src_templ
, 0, sizeof(*src_templ
));
1310 src_templ
->format
= util_format_linear(src
->format
);
1311 src_templ
->u
.tex
.first_level
= srclevel
;
1312 src_templ
->u
.tex
.last_level
= srclevel
;
1313 src_templ
->u
.tex
.first_layer
= 0;
1314 src_templ
->u
.tex
.last_layer
=
1315 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1316 : src
->array_size
- 1;
1317 src_templ
->swizzle_r
= PIPE_SWIZZLE_RED
;
1318 src_templ
->swizzle_g
= PIPE_SWIZZLE_GREEN
;
1319 src_templ
->swizzle_b
= PIPE_SWIZZLE_BLUE
;
1320 src_templ
->swizzle_a
= PIPE_SWIZZLE_ALPHA
;
1323 static boolean
is_blit_generic_supported(struct blitter_context
*blitter
,
1324 const struct pipe_resource
*dst
,
1325 enum pipe_format dst_format
,
1326 const struct pipe_resource
*src
,
1327 enum pipe_format src_format
,
1330 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1331 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1335 const struct util_format_description
*desc
=
1336 util_format_description(dst_format
);
1337 boolean dst_has_stencil
= util_format_has_stencil(desc
);
1339 /* Stencil export must be supported for stencil copy. */
1340 if ((mask
& PIPE_MASK_S
) && dst_has_stencil
&&
1341 !ctx
->has_stencil_export
) {
1345 if (dst_has_stencil
|| util_format_has_depth(desc
))
1346 bind
= PIPE_BIND_DEPTH_STENCIL
;
1348 bind
= PIPE_BIND_RENDER_TARGET
;
1350 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1351 dst
->nr_samples
, bind
)) {
1357 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1361 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1362 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
)) {
1366 /* Check stencil sampler support for stencil copy. */
1367 if (mask
& PIPE_MASK_S
) {
1368 if (util_format_has_stencil(util_format_description(src_format
))) {
1369 enum pipe_format stencil_format
=
1370 util_format_stencil_only(src_format
);
1371 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1373 if (stencil_format
!= src_format
&&
1374 !screen
->is_format_supported(screen
, stencil_format
,
1375 src
->target
, src
->nr_samples
,
1376 PIPE_BIND_SAMPLER_VIEW
)) {
1386 boolean
util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1387 const struct pipe_resource
*dst
,
1388 const struct pipe_resource
*src
)
1390 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1391 src
, src
->format
, PIPE_MASK_RGBAZS
);
1394 boolean
util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1395 const struct pipe_blit_info
*info
)
1397 return is_blit_generic_supported(blitter
,
1398 info
->dst
.resource
, info
->dst
.format
,
1399 info
->src
.resource
, info
->src
.format
,
1403 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1404 struct pipe_resource
*dst
,
1406 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1407 struct pipe_resource
*src
,
1409 const struct pipe_box
*srcbox
)
1411 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1412 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1413 struct pipe_surface
*dst_view
, dst_templ
;
1414 struct pipe_sampler_view src_templ
, *src_view
;
1415 struct pipe_box dstbox
;
1418 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1420 u_box_3d(dstx
, dsty
, dstz
, abs(srcbox
->width
), abs(srcbox
->height
),
1421 abs(srcbox
->depth
), &dstbox
);
1423 /* Initialize the surface. */
1424 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1425 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1427 /* Initialize the sampler view. */
1428 util_blitter_default_src_texture(&src_templ
, src
, src_level
);
1429 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1432 util_blitter_blit_generic(blitter
, dst_view
, &dstbox
,
1433 src_view
, srcbox
, src
->width0
, src
->height0
,
1434 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
);
1436 pipe_surface_reference(&dst_view
, NULL
);
1437 pipe_sampler_view_reference(&src_view
, NULL
);
1440 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1441 struct pipe_surface
*dst
,
1442 const struct pipe_box
*dstbox
,
1443 struct pipe_sampler_view
*src
,
1444 const struct pipe_box
*srcbox
,
1445 unsigned src_width0
, unsigned src_height0
,
1446 unsigned mask
, unsigned filter
,
1447 const struct pipe_scissor_state
*scissor
)
1449 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1450 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1451 struct pipe_framebuffer_state fb_state
;
1452 enum pipe_texture_target src_target
= src
->texture
->target
;
1453 unsigned src_samples
= src
->texture
->nr_samples
;
1454 unsigned dst_samples
= dst
->texture
->nr_samples
;
1455 boolean has_depth
, has_stencil
, has_color
;
1456 boolean blit_stencil
, blit_depth
, blit_color
;
1457 void *sampler_state
;
1458 const struct util_format_description
*src_desc
=
1459 util_format_description(src
->format
);
1460 const struct util_format_description
*dst_desc
=
1461 util_format_description(dst
->format
);
1463 has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
&&
1464 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1465 has_depth
= util_format_has_depth(src_desc
) &&
1466 util_format_has_depth(dst_desc
);
1467 has_stencil
= util_format_has_stencil(src_desc
) &&
1468 util_format_has_stencil(dst_desc
);
1470 blit_color
= has_color
&& (mask
& PIPE_MASK_RGBA
);
1471 blit_depth
= has_depth
&& (mask
& PIPE_MASK_Z
);
1472 blit_stencil
= has_stencil
&& (mask
& PIPE_MASK_S
) &&
1473 ctx
->has_stencil_export
;
1475 if (!blit_stencil
&& !blit_depth
&& !blit_color
) {
1480 (dstbox
->width
== abs(srcbox
->width
) &&
1481 dstbox
->height
== abs(srcbox
->height
))) {
1482 filter
= PIPE_TEX_FILTER_NEAREST
;
1485 /* Check whether the states are properly saved. */
1486 blitter_set_running_flag(ctx
);
1487 blitter_check_saved_vertex_states(ctx
);
1488 blitter_check_saved_fragment_states(ctx
);
1489 blitter_check_saved_textures(ctx
);
1490 blitter_check_saved_fb_state(ctx
);
1491 blitter_disable_render_cond(ctx
);
1493 /* Initialize framebuffer state. */
1494 fb_state
.width
= dst
->width
;
1495 fb_state
.height
= dst
->height
;
1496 fb_state
.nr_cbufs
= blit_depth
|| blit_stencil
? 0 : 1;
1497 fb_state
.cbufs
[0] = NULL
;
1498 fb_state
.zsbuf
= NULL
;
1500 if (blit_depth
|| blit_stencil
) {
1501 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1503 if (blit_depth
&& blit_stencil
) {
1504 pipe
->bind_depth_stencil_alpha_state(pipe
,
1505 ctx
->dsa_write_depth_stencil
);
1506 ctx
->bind_fs_state(pipe
,
1507 blitter_get_fs_texfetch_depthstencil(ctx
, src_target
,
1509 } else if (blit_depth
) {
1510 pipe
->bind_depth_stencil_alpha_state(pipe
,
1511 ctx
->dsa_write_depth_keep_stencil
);
1512 ctx
->bind_fs_state(pipe
,
1513 blitter_get_fs_texfetch_depth(ctx
, src_target
,
1515 } else { /* is_stencil */
1516 pipe
->bind_depth_stencil_alpha_state(pipe
,
1517 ctx
->dsa_keep_depth_write_stencil
);
1518 ctx
->bind_fs_state(pipe
,
1519 blitter_get_fs_texfetch_stencil(ctx
, src_target
,
1524 pipe
->bind_blend_state(pipe
, ctx
->blend
[mask
& PIPE_MASK_RGBA
]);
1525 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1526 ctx
->bind_fs_state(pipe
,
1527 blitter_get_fs_texfetch_col(ctx
, src
->format
, src_target
,
1528 src_samples
, dst_samples
, filter
));
1531 /* Set the linear filter only for scaled color non-MSAA blits. */
1532 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
1533 if (src_target
== PIPE_TEXTURE_RECT
) {
1534 sampler_state
= ctx
->sampler_state_rect_linear
;
1536 sampler_state
= ctx
->sampler_state_linear
;
1539 if (src_target
== PIPE_TEXTURE_RECT
) {
1540 sampler_state
= ctx
->sampler_state_rect
;
1542 sampler_state
= ctx
->sampler_state
;
1547 if (blit_depth
&& blit_stencil
) {
1548 /* Setup two samplers, one for depth and the other one for stencil. */
1549 struct pipe_sampler_view templ
;
1550 struct pipe_sampler_view
*views
[2];
1551 void *samplers
[2] = {sampler_state
, sampler_state
};
1554 templ
.format
= util_format_stencil_only(templ
.format
);
1555 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1558 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1560 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, views
);
1561 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, samplers
);
1563 pipe_sampler_view_reference(&views
[1], NULL
);
1564 } else if (blit_stencil
) {
1565 /* Set a stencil-only sampler view for it not to sample depth instead. */
1566 struct pipe_sampler_view templ
;
1567 struct pipe_sampler_view
*view
;
1570 templ
.format
= util_format_stencil_only(templ
.format
);
1571 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1573 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1575 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &view
);
1576 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1577 0, 1, &sampler_state
);
1579 pipe_sampler_view_reference(&view
, NULL
);
1581 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src
);
1582 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1583 0, 1, &sampler_state
);
1586 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1588 pipe
->set_scissor_states(pipe
, 0, 1, scissor
);
1591 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
, FALSE
);
1592 blitter_set_dst_dimensions(ctx
, dst
->width
, dst
->height
);
1594 if ((src_target
== PIPE_TEXTURE_1D
||
1595 src_target
== PIPE_TEXTURE_2D
||
1596 src_target
== PIPE_TEXTURE_RECT
) &&
1598 /* Draw the quad with the draw_rectangle callback. */
1600 /* Set texture coordinates. - use a pipe color union
1601 * for interface purposes.
1602 * XXX pipe_color_union is a wrong name since we use that to set
1603 * texture coordinates too.
1605 union pipe_color_union coord
;
1606 get_texcoords(src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1607 srcbox
->x
+srcbox
->width
, srcbox
->y
+srcbox
->height
, coord
.f
);
1609 /* Set framebuffer state. */
1610 if (blit_depth
|| blit_stencil
) {
1611 fb_state
.zsbuf
= dst
;
1613 fb_state
.cbufs
[0] = dst
;
1615 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1618 pipe
->set_sample_mask(pipe
, ~0);
1619 blitter
->draw_rectangle(blitter
, dstbox
->x
, dstbox
->y
,
1620 dstbox
->x
+ dstbox
->width
,
1621 dstbox
->y
+ dstbox
->height
, 0,
1622 UTIL_BLITTER_ATTRIB_TEXCOORD
, &coord
);
1624 /* Draw the quad with the generic codepath. */
1626 for (dst_z
= 0; dst_z
< dstbox
->depth
; dst_z
++) {
1627 struct pipe_surface
*old
;
1628 float dst2src_scale
= srcbox
->depth
/ (float)dstbox
->depth
;
1630 /* Scale Z properly if the blit is scaled.
1632 * When downscaling, we want the coordinates centered, so that
1633 * mipmapping works for 3D textures. For example, when generating
1634 * a 4x4x4 level, this wouldn't average the pixels:
1636 * src Z: 0 1 2 3 4 5 6 7
1639 * Because the pixels are not centered below the pixels of the higher
1640 * level. Therefore, we want this:
1641 * src Z: 0 1 2 3 4 5 6 7
1644 * dst_offset defines the offset needed for centering the pixels and
1645 * it works with any scaling (not just 2x).
1647 float dst_offset
= ((srcbox
->depth
- 1) -
1648 (dstbox
->depth
- 1) * dst2src_scale
) * 0.5;
1649 float src_z
= (dst_z
+ dst_offset
) * dst2src_scale
;
1651 /* Set framebuffer state. */
1652 if (blit_depth
|| blit_stencil
) {
1653 fb_state
.zsbuf
= dst
;
1655 fb_state
.cbufs
[0] = dst
;
1657 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1659 /* See if we need to blit a multisample or singlesample buffer. */
1660 if (src_samples
== dst_samples
&& dst_samples
> 1) {
1662 unsigned i
, max_sample
= dst_samples
- 1;
1664 for (i
= 0; i
<= max_sample
; i
++) {
1665 pipe
->set_sample_mask(pipe
, 1 << i
);
1666 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1668 i
, srcbox
->x
, srcbox
->y
,
1669 srcbox
->x
+ srcbox
->width
,
1670 srcbox
->y
+ srcbox
->height
);
1671 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1672 dstbox
->x
+ dstbox
->width
,
1673 dstbox
->y
+ dstbox
->height
, 0, 1);
1676 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1677 pipe
->set_sample_mask(pipe
, ~0);
1678 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1679 srcbox
->z
+ src_z
, 0,
1680 srcbox
->x
, srcbox
->y
,
1681 srcbox
->x
+ srcbox
->width
,
1682 srcbox
->y
+ srcbox
->height
);
1683 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1684 dstbox
->x
+ dstbox
->width
,
1685 dstbox
->y
+ dstbox
->height
, 0, 1);
1688 /* Get the next surface or (if this is the last iteration)
1689 * just unreference the last one. */
1691 if (dst_z
< dstbox
->depth
-1) {
1692 dst
= ctx
->base
.get_next_surface_layer(ctx
->base
.pipe
, dst
);
1695 pipe_surface_reference(&old
, NULL
);
1700 blitter_restore_vertex_states(ctx
);
1701 blitter_restore_fragment_states(ctx
);
1702 blitter_restore_textures(ctx
);
1703 blitter_restore_fb_state(ctx
);
1705 pipe
->set_scissor_states(pipe
, 0, 1, &ctx
->base
.saved_scissor
);
1707 blitter_restore_render_cond(ctx
);
1708 blitter_unset_running_flag(ctx
);
1712 util_blitter_blit(struct blitter_context
*blitter
,
1713 const struct pipe_blit_info
*info
)
1715 struct pipe_resource
*dst
= info
->dst
.resource
;
1716 struct pipe_resource
*src
= info
->src
.resource
;
1717 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1718 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1719 struct pipe_surface
*dst_view
, dst_templ
;
1720 struct pipe_sampler_view src_templ
, *src_view
;
1722 /* Initialize the surface. */
1723 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
1725 dst_templ
.format
= info
->dst
.format
;
1726 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1728 /* Initialize the sampler view. */
1729 util_blitter_default_src_texture(&src_templ
, src
, info
->src
.level
);
1730 src_templ
.format
= info
->src
.format
;
1731 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1734 util_blitter_blit_generic(blitter
, dst_view
, &info
->dst
.box
,
1735 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
1736 info
->mask
, info
->filter
,
1737 info
->scissor_enable
? &info
->scissor
: NULL
);
1739 pipe_surface_reference(&dst_view
, NULL
);
1740 pipe_sampler_view_reference(&src_view
, NULL
);
1743 /* Clear a region of a color surface to a constant value. */
1744 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
1745 struct pipe_surface
*dstsurf
,
1746 const union pipe_color_union
*color
,
1747 unsigned dstx
, unsigned dsty
,
1748 unsigned width
, unsigned height
)
1750 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1751 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1752 struct pipe_framebuffer_state fb_state
;
1754 assert(dstsurf
->texture
);
1755 if (!dstsurf
->texture
)
1758 /* check the saved state */
1759 blitter_set_running_flag(ctx
);
1760 blitter_check_saved_vertex_states(ctx
);
1761 blitter_check_saved_fragment_states(ctx
);
1762 blitter_check_saved_fb_state(ctx
);
1763 blitter_disable_render_cond(ctx
);
1766 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
]);
1767 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1768 bind_fs_write_one_cbuf(ctx
);
1769 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1771 /* set a framebuffer state */
1772 fb_state
.width
= dstsurf
->width
;
1773 fb_state
.height
= dstsurf
->height
;
1774 fb_state
.nr_cbufs
= 1;
1775 fb_state
.cbufs
[0] = dstsurf
;
1777 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1778 pipe
->set_sample_mask(pipe
, ~0);
1780 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1781 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1782 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
1783 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1785 blitter_restore_vertex_states(ctx
);
1786 blitter_restore_fragment_states(ctx
);
1787 blitter_restore_fb_state(ctx
);
1788 blitter_restore_render_cond(ctx
);
1789 blitter_unset_running_flag(ctx
);
1792 /* Clear a region of a depth stencil surface. */
1793 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
1794 struct pipe_surface
*dstsurf
,
1795 unsigned clear_flags
,
1798 unsigned dstx
, unsigned dsty
,
1799 unsigned width
, unsigned height
)
1801 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1802 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1803 struct pipe_framebuffer_state fb_state
;
1804 struct pipe_stencil_ref sr
= { { 0 } };
1806 assert(dstsurf
->texture
);
1807 if (!dstsurf
->texture
)
1810 /* check the saved state */
1811 blitter_set_running_flag(ctx
);
1812 blitter_check_saved_vertex_states(ctx
);
1813 blitter_check_saved_fragment_states(ctx
);
1814 blitter_check_saved_fb_state(ctx
);
1815 blitter_disable_render_cond(ctx
);
1818 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1819 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1820 sr
.ref_value
[0] = stencil
& 0xff;
1821 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1822 pipe
->set_stencil_ref(pipe
, &sr
);
1824 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
1825 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1827 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
1828 sr
.ref_value
[0] = stencil
& 0xff;
1829 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1830 pipe
->set_stencil_ref(pipe
, &sr
);
1833 /* hmm that should be illegal probably, or make it a no-op somewhere */
1834 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1837 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1839 /* set a framebuffer state */
1840 fb_state
.width
= dstsurf
->width
;
1841 fb_state
.height
= dstsurf
->height
;
1842 fb_state
.nr_cbufs
= 0;
1843 fb_state
.cbufs
[0] = 0;
1844 fb_state
.zsbuf
= dstsurf
;
1845 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1846 pipe
->set_sample_mask(pipe
, ~0);
1848 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1849 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1850 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
,
1852 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
1854 blitter_restore_vertex_states(ctx
);
1855 blitter_restore_fragment_states(ctx
);
1856 blitter_restore_fb_state(ctx
);
1857 blitter_restore_render_cond(ctx
);
1858 blitter_unset_running_flag(ctx
);
1861 /* draw a rectangle across a region using a custom dsa stage - for r600g */
1862 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
1863 struct pipe_surface
*zsurf
,
1864 struct pipe_surface
*cbsurf
,
1865 unsigned sample_mask
,
1866 void *dsa_stage
, float depth
)
1868 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1869 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1870 struct pipe_framebuffer_state fb_state
;
1872 assert(zsurf
->texture
);
1873 if (!zsurf
->texture
)
1876 /* check the saved state */
1877 blitter_set_running_flag(ctx
);
1878 blitter_check_saved_vertex_states(ctx
);
1879 blitter_check_saved_fragment_states(ctx
);
1880 blitter_check_saved_fb_state(ctx
);
1881 blitter_disable_render_cond(ctx
);
1884 pipe
->bind_blend_state(pipe
, cbsurf
? ctx
->blend
[PIPE_MASK_RGBA
] :
1886 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
1888 bind_fs_write_one_cbuf(ctx
);
1891 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1893 /* set a framebuffer state */
1894 fb_state
.width
= zsurf
->width
;
1895 fb_state
.height
= zsurf
->height
;
1896 fb_state
.nr_cbufs
= 1;
1898 fb_state
.cbufs
[0] = cbsurf
;
1899 fb_state
.nr_cbufs
= 1;
1901 fb_state
.cbufs
[0] = NULL
;
1902 fb_state
.nr_cbufs
= 0;
1904 fb_state
.zsbuf
= zsurf
;
1905 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1906 pipe
->set_sample_mask(pipe
, sample_mask
);
1908 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1909 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
1910 blitter
->draw_rectangle(blitter
, 0, 0, zsurf
->width
, zsurf
->height
, depth
,
1911 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
1913 blitter_restore_vertex_states(ctx
);
1914 blitter_restore_fragment_states(ctx
);
1915 blitter_restore_fb_state(ctx
);
1916 blitter_restore_render_cond(ctx
);
1917 blitter_unset_running_flag(ctx
);
1920 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
1921 struct pipe_resource
*dst
,
1923 struct pipe_resource
*src
,
1927 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1928 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1929 struct pipe_vertex_buffer vb
;
1930 struct pipe_stream_output_target
*so_target
;
1931 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
1933 if (srcx
>= src
->width0
||
1934 dstx
>= dst
->width0
) {
1937 if (srcx
+ size
> src
->width0
) {
1938 size
= src
->width0
- srcx
;
1940 if (dstx
+ size
> dst
->width0
) {
1941 size
= dst
->width0
- dstx
;
1944 /* Drivers not capable of Stream Out should not call this function
1945 * in the first place. */
1946 assert(ctx
->has_stream_out
);
1948 /* Some alignment is required. */
1949 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
1950 !ctx
->has_stream_out
) {
1951 struct pipe_box box
;
1952 u_box_1d(srcx
, size
, &box
);
1953 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
1957 blitter_set_running_flag(ctx
);
1958 blitter_check_saved_vertex_states(ctx
);
1959 blitter_disable_render_cond(ctx
);
1962 vb
.buffer_offset
= srcx
;
1965 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1966 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[0]);
1967 bind_vs_pos_only(ctx
);
1968 if (ctx
->has_geometry_shader
)
1969 pipe
->bind_gs_state(pipe
, NULL
);
1970 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
1972 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
1973 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
1975 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
1977 blitter_restore_vertex_states(ctx
);
1978 blitter_restore_render_cond(ctx
);
1979 blitter_unset_running_flag(ctx
);
1980 pipe_so_target_reference(&so_target
, NULL
);
1983 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
1984 struct pipe_resource
*dst
,
1985 unsigned offset
, unsigned size
,
1986 unsigned num_channels
,
1987 const union pipe_color_union
*clear_value
)
1989 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1990 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1991 struct pipe_vertex_buffer vb
= {0};
1992 struct pipe_stream_output_target
*so_target
;
1993 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
1995 assert(num_channels
>= 1);
1996 assert(num_channels
<= 4);
1998 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
2000 * R600 uses this to initialize texture resources, so width0 might not be
2001 * what you think it is.
2004 /* Streamout is required. */
2005 if (!ctx
->has_stream_out
) {
2006 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2010 /* Some alignment is required. */
2011 if (offset
% 4 != 0 || size
% 4 != 0) {
2012 assert(!"Bad alignment in util_blitter_clear_buffer()");
2016 u_upload_data(ctx
->upload
, 0, num_channels
*4, clear_value
,
2017 &vb
.buffer_offset
, &vb
.buffer
);
2020 blitter_set_running_flag(ctx
);
2021 blitter_check_saved_vertex_states(ctx
);
2022 blitter_disable_render_cond(ctx
);
2024 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2025 pipe
->bind_vertex_elements_state(pipe
,
2026 ctx
->velem_state_readbuf
[num_channels
-1]);
2027 bind_vs_pos_only(ctx
);
2028 if (ctx
->has_geometry_shader
)
2029 pipe
->bind_gs_state(pipe
, NULL
);
2030 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2032 so_target
= pipe
->create_stream_output_target(pipe
, dst
, offset
, size
);
2033 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2035 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2037 blitter_restore_vertex_states(ctx
);
2038 blitter_restore_render_cond(ctx
);
2039 blitter_unset_running_flag(ctx
);
2040 pipe_so_target_reference(&so_target
, NULL
);
2041 pipe_resource_reference(&vb
.buffer
, NULL
);
2044 /* probably radeon specific */
2045 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
2046 struct pipe_resource
*dst
,
2049 struct pipe_resource
*src
,
2051 unsigned sample_mask
,
2053 enum pipe_format format
)
2055 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2056 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2057 struct pipe_framebuffer_state fb_state
;
2058 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
2060 blitter_set_running_flag(ctx
);
2061 blitter_check_saved_vertex_states(ctx
);
2062 blitter_check_saved_fragment_states(ctx
);
2063 blitter_disable_render_cond(ctx
);
2066 pipe
->bind_blend_state(pipe
, custom_blend
);
2067 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2068 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2069 bind_fs_write_one_cbuf(ctx
);
2070 pipe
->set_sample_mask(pipe
, sample_mask
);
2072 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
2073 surf_tmpl
.format
= format
;
2074 surf_tmpl
.u
.tex
.level
= dst_level
;
2075 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
2076 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
2078 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
2080 surf_tmpl
.u
.tex
.level
= 0;
2081 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
2082 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
2084 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
2086 /* set a framebuffer state */
2087 fb_state
.width
= src
->width0
;
2088 fb_state
.height
= src
->height0
;
2089 fb_state
.nr_cbufs
= 2;
2090 fb_state
.cbufs
[0] = srcsurf
;
2091 fb_state
.cbufs
[1] = dstsurf
;
2092 fb_state
.zsbuf
= NULL
;
2093 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2095 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2096 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
2097 blitter
->draw_rectangle(blitter
, 0, 0, src
->width0
, src
->height0
,
2099 blitter_restore_fb_state(ctx
);
2100 blitter_restore_vertex_states(ctx
);
2101 blitter_restore_fragment_states(ctx
);
2102 blitter_restore_render_cond(ctx
);
2103 blitter_unset_running_flag(ctx
);
2105 pipe_surface_reference(&srcsurf
, NULL
);
2106 pipe_surface_reference(&dstsurf
, NULL
);
2109 void util_blitter_custom_color(struct blitter_context
*blitter
,
2110 struct pipe_surface
*dstsurf
,
2113 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2114 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2115 struct pipe_framebuffer_state fb_state
;
2117 assert(dstsurf
->texture
);
2118 if (!dstsurf
->texture
)
2121 /* check the saved state */
2122 blitter_set_running_flag(ctx
);
2123 blitter_check_saved_vertex_states(ctx
);
2124 blitter_check_saved_fragment_states(ctx
);
2125 blitter_check_saved_fb_state(ctx
);
2126 blitter_disable_render_cond(ctx
);
2129 pipe
->bind_blend_state(pipe
, custom_blend
? custom_blend
2130 : ctx
->blend
[PIPE_MASK_RGBA
]);
2131 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2132 bind_fs_write_one_cbuf(ctx
);
2133 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2134 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2136 /* set a framebuffer state */
2137 fb_state
.width
= dstsurf
->width
;
2138 fb_state
.height
= dstsurf
->height
;
2139 fb_state
.nr_cbufs
= 1;
2140 fb_state
.cbufs
[0] = dstsurf
;
2142 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2143 pipe
->set_sample_mask(pipe
, ~0);
2145 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2146 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2147 blitter
->draw_rectangle(blitter
, 0, 0, dstsurf
->width
, dstsurf
->height
,
2150 blitter_restore_vertex_states(ctx
);
2151 blitter_restore_fragment_states(ctx
);
2152 blitter_restore_fb_state(ctx
);
2153 blitter_restore_render_cond(ctx
);
2154 blitter_unset_running_flag(ctx
);