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 pipe
->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 pipe
->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 pipe
->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
);
1012 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
)
1014 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1015 struct pipe_context
*pipe
= blitter
->pipe
;
1016 struct pipe_screen
*screen
= pipe
->screen
;
1017 unsigned samples
, j
, f
, target
, max_samples
;
1018 boolean has_arraytex
, has_cubearraytex
;
1020 max_samples
= ctx
->has_texture_multisample
? 2 : 1;
1021 has_arraytex
= screen
->get_param(screen
,
1022 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
) != 0;
1023 has_cubearraytex
= screen
->get_param(screen
,
1024 PIPE_CAP_CUBE_MAP_ARRAY
) != 0;
1026 /* It only matters if i <= 1 or > 1. */
1027 for (samples
= 1; samples
<= max_samples
; samples
++) {
1028 for (target
= PIPE_TEXTURE_1D
; target
< PIPE_MAX_TEXTURE_TYPES
; target
++) {
1029 if (!has_arraytex
&&
1030 (target
== PIPE_TEXTURE_1D_ARRAY
||
1031 target
== PIPE_TEXTURE_2D_ARRAY
)) {
1034 if (!has_cubearraytex
&&
1035 (target
== PIPE_TEXTURE_CUBE_ARRAY
))
1039 (target
!= PIPE_TEXTURE_2D
&&
1040 target
!= PIPE_TEXTURE_2D_ARRAY
))
1043 /* If samples == 1, the shaders read one texel. If samples >= 1,
1044 * they read one sample.
1046 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
, target
,
1047 samples
, samples
, 0);
1048 blitter_get_fs_texfetch_depth(ctx
, target
, samples
);
1049 if (ctx
->has_stencil_export
) {
1050 blitter_get_fs_texfetch_depthstencil(ctx
, target
, samples
);
1051 blitter_get_fs_texfetch_stencil(ctx
, target
, samples
);
1057 /* MSAA resolve shaders. */
1058 for (j
= 2; j
< 32; j
++) {
1059 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R32_FLOAT
,
1061 PIPE_BIND_SAMPLER_VIEW
)) {
1065 for (f
= 0; f
< 2; f
++) {
1066 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
, target
,
1068 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
, target
,
1070 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
, target
,
1077 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
1079 ctx
->fs_write_one_cbuf
=
1080 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1081 TGSI_INTERPOLATE_CONSTANT
, FALSE
);
1083 ctx
->fs_write_all_cbufs
=
1084 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
1085 TGSI_INTERPOLATE_CONSTANT
, TRUE
);
1087 ctx
->cached_all_shaders
= TRUE
;
1090 static void blitter_set_common_draw_rect_state(struct blitter_context_priv
*ctx
,
1094 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1096 pipe
->bind_rasterizer_state(pipe
, scissor
? ctx
->rs_state_scissor
1099 bind_vs_layered(ctx
);
1101 bind_vs_passthrough(ctx
);
1103 if (ctx
->has_geometry_shader
)
1104 pipe
->bind_gs_state(pipe
, NULL
);
1105 if (ctx
->has_stream_out
)
1106 pipe
->set_stream_output_targets(pipe
, 0, NULL
, NULL
);
1109 static void blitter_draw(struct blitter_context_priv
*ctx
,
1110 int x1
, int y1
, int x2
, int y2
, float depth
,
1111 unsigned num_instances
)
1113 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1114 struct pipe_vertex_buffer vb
= {0};
1116 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
1118 vb
.stride
= 8 * sizeof(float);
1120 u_upload_data(ctx
->upload
, 0, sizeof(ctx
->vertices
), ctx
->vertices
,
1121 &vb
.buffer_offset
, &vb
.buffer
);
1122 u_upload_unmap(ctx
->upload
);
1124 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1125 util_draw_arrays_instanced(pipe
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
1127 pipe_resource_reference(&vb
.buffer
, NULL
);
1130 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
1131 int x1
, int y1
, int x2
, int y2
, float depth
,
1132 enum blitter_attrib_type type
,
1133 const union pipe_color_union
*attrib
)
1135 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1138 case UTIL_BLITTER_ATTRIB_COLOR
:
1139 blitter_set_clear_color(ctx
, attrib
);
1142 case UTIL_BLITTER_ATTRIB_TEXCOORD
:
1143 set_texcoords_in_vertices(attrib
->f
, &ctx
->vertices
[0][1][0], 8);
1149 blitter_draw(ctx
, x1
, y1
, x2
, y2
, depth
, 1);
1152 static void *get_clear_blend_state(struct blitter_context_priv
*ctx
,
1153 unsigned clear_buffers
)
1155 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1158 clear_buffers
&= PIPE_CLEAR_COLOR
;
1160 /* Return an existing blend state. */
1162 return ctx
->blend
[0];
1164 index
= GET_CLEAR_BLEND_STATE_IDX(clear_buffers
);
1166 if (ctx
->blend_clear
[index
])
1167 return ctx
->blend_clear
[index
];
1169 /* Create a new one. */
1171 struct pipe_blend_state blend
= {0};
1174 blend
.independent_blend_enable
= 1;
1176 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1177 if (clear_buffers
& (PIPE_CLEAR_COLOR0
<< i
)) {
1178 blend
.rt
[i
].colormask
= PIPE_MASK_RGBA
;
1182 ctx
->blend_clear
[index
] = pipe
->create_blend_state(pipe
, &blend
);
1184 return ctx
->blend_clear
[index
];
1187 static void util_blitter_clear_custom(struct blitter_context
*blitter
,
1188 unsigned width
, unsigned height
,
1189 unsigned num_layers
,
1190 unsigned clear_buffers
,
1191 const union pipe_color_union
*color
,
1192 double depth
, unsigned stencil
,
1193 void *custom_blend
, void *custom_dsa
)
1195 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1196 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1197 struct pipe_stencil_ref sr
= { { 0 } };
1199 assert(ctx
->has_layered
|| num_layers
<= 1);
1201 blitter_set_running_flag(ctx
);
1202 blitter_check_saved_vertex_states(ctx
);
1203 blitter_check_saved_fragment_states(ctx
);
1204 blitter_disable_render_cond(ctx
);
1208 pipe
->bind_blend_state(pipe
, custom_blend
);
1210 pipe
->bind_blend_state(pipe
, get_clear_blend_state(ctx
, clear_buffers
));
1214 pipe
->bind_depth_stencil_alpha_state(pipe
, custom_dsa
);
1215 } else if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1216 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1217 } else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
1218 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1219 } else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
1220 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1222 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1225 sr
.ref_value
[0] = stencil
& 0xff;
1226 pipe
->set_stencil_ref(pipe
, &sr
);
1228 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1229 bind_fs_write_all_cbufs(ctx
);
1230 pipe
->set_sample_mask(pipe
, ~0);
1232 blitter_set_dst_dimensions(ctx
, width
, height
);
1234 if (num_layers
> 1 && ctx
->has_layered
) {
1235 blitter_set_common_draw_rect_state(ctx
, FALSE
, TRUE
);
1236 blitter_set_clear_color(ctx
, color
);
1237 blitter_draw(ctx
, 0, 0, width
, height
, depth
, num_layers
);
1240 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1241 blitter
->draw_rectangle(blitter
, 0, 0, width
, height
, (float) depth
,
1242 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1245 blitter_restore_vertex_states(ctx
);
1246 blitter_restore_fragment_states(ctx
);
1247 blitter_restore_render_cond(ctx
);
1248 blitter_unset_running_flag(ctx
);
1251 void util_blitter_clear(struct blitter_context
*blitter
,
1252 unsigned width
, unsigned height
, unsigned num_layers
,
1253 unsigned clear_buffers
,
1254 const union pipe_color_union
*color
,
1255 double depth
, unsigned stencil
)
1257 util_blitter_clear_custom(blitter
, width
, height
, num_layers
,
1258 clear_buffers
, color
, depth
, stencil
,
1262 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1263 unsigned width
, unsigned height
,
1264 double depth
, void *custom_dsa
)
1266 static const union pipe_color_union color
;
1267 util_blitter_clear_custom(blitter
, width
, height
, 0, 0, &color
, depth
, 0,
1271 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1272 struct pipe_resource
*dst
,
1276 memset(dst_templ
, 0, sizeof(*dst_templ
));
1277 dst_templ
->format
= util_format_linear(dst
->format
);
1278 dst_templ
->u
.tex
.level
= dstlevel
;
1279 dst_templ
->u
.tex
.first_layer
= dstz
;
1280 dst_templ
->u
.tex
.last_layer
= dstz
;
1283 static struct pipe_surface
*
1284 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
1285 struct pipe_surface
*surf
)
1287 struct pipe_surface dst_templ
;
1289 memset(&dst_templ
, 0, sizeof(dst_templ
));
1290 dst_templ
.format
= surf
->format
;
1291 dst_templ
.u
.tex
.level
= surf
->u
.tex
.level
;
1292 dst_templ
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
+ 1;
1293 dst_templ
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
+ 1;
1295 return pipe
->create_surface(pipe
, surf
->texture
, &dst_templ
);
1298 void util_blitter_default_src_texture(struct pipe_sampler_view
*src_templ
,
1299 struct pipe_resource
*src
,
1302 memset(src_templ
, 0, sizeof(*src_templ
));
1303 src_templ
->format
= util_format_linear(src
->format
);
1304 src_templ
->u
.tex
.first_level
= srclevel
;
1305 src_templ
->u
.tex
.last_level
= srclevel
;
1306 src_templ
->u
.tex
.first_layer
= 0;
1307 src_templ
->u
.tex
.last_layer
=
1308 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1309 : src
->array_size
- 1;
1310 src_templ
->swizzle_r
= PIPE_SWIZZLE_RED
;
1311 src_templ
->swizzle_g
= PIPE_SWIZZLE_GREEN
;
1312 src_templ
->swizzle_b
= PIPE_SWIZZLE_BLUE
;
1313 src_templ
->swizzle_a
= PIPE_SWIZZLE_ALPHA
;
1316 static boolean
is_blit_generic_supported(struct blitter_context
*blitter
,
1317 const struct pipe_resource
*dst
,
1318 enum pipe_format dst_format
,
1319 const struct pipe_resource
*src
,
1320 enum pipe_format src_format
,
1323 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1324 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1328 const struct util_format_description
*desc
=
1329 util_format_description(dst_format
);
1330 boolean dst_has_stencil
= util_format_has_stencil(desc
);
1332 /* Stencil export must be supported for stencil copy. */
1333 if ((mask
& PIPE_MASK_S
) && dst_has_stencil
&&
1334 !ctx
->has_stencil_export
) {
1338 if (dst_has_stencil
|| util_format_has_depth(desc
))
1339 bind
= PIPE_BIND_DEPTH_STENCIL
;
1341 bind
= PIPE_BIND_RENDER_TARGET
;
1343 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1344 dst
->nr_samples
, bind
)) {
1350 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1354 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1355 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
)) {
1359 /* Check stencil sampler support for stencil copy. */
1360 if (mask
& PIPE_MASK_S
) {
1361 if (util_format_has_stencil(util_format_description(src_format
))) {
1362 enum pipe_format stencil_format
=
1363 util_format_stencil_only(src_format
);
1364 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1366 if (stencil_format
!= src_format
&&
1367 !screen
->is_format_supported(screen
, stencil_format
,
1368 src
->target
, src
->nr_samples
,
1369 PIPE_BIND_SAMPLER_VIEW
)) {
1379 boolean
util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1380 const struct pipe_resource
*dst
,
1381 const struct pipe_resource
*src
)
1383 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1384 src
, src
->format
, PIPE_MASK_RGBAZS
);
1387 boolean
util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1388 const struct pipe_blit_info
*info
)
1390 return is_blit_generic_supported(blitter
,
1391 info
->dst
.resource
, info
->dst
.format
,
1392 info
->src
.resource
, info
->src
.format
,
1396 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1397 struct pipe_resource
*dst
,
1399 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1400 struct pipe_resource
*src
,
1402 const struct pipe_box
*srcbox
)
1404 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1405 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1406 struct pipe_surface
*dst_view
, dst_templ
;
1407 struct pipe_sampler_view src_templ
, *src_view
;
1408 struct pipe_box dstbox
;
1411 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1413 u_box_3d(dstx
, dsty
, dstz
, abs(srcbox
->width
), abs(srcbox
->height
),
1414 abs(srcbox
->depth
), &dstbox
);
1416 /* Initialize the surface. */
1417 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1418 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1420 /* Initialize the sampler view. */
1421 util_blitter_default_src_texture(&src_templ
, src
, src_level
);
1422 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1425 util_blitter_blit_generic(blitter
, dst_view
, &dstbox
,
1426 src_view
, srcbox
, src
->width0
, src
->height0
,
1427 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
);
1429 pipe_surface_reference(&dst_view
, NULL
);
1430 pipe_sampler_view_reference(&src_view
, NULL
);
1433 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1434 struct pipe_surface
*dst
,
1435 const struct pipe_box
*dstbox
,
1436 struct pipe_sampler_view
*src
,
1437 const struct pipe_box
*srcbox
,
1438 unsigned src_width0
, unsigned src_height0
,
1439 unsigned mask
, unsigned filter
,
1440 const struct pipe_scissor_state
*scissor
)
1442 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1443 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1444 struct pipe_framebuffer_state fb_state
;
1445 enum pipe_texture_target src_target
= src
->texture
->target
;
1446 unsigned src_samples
= src
->texture
->nr_samples
;
1447 unsigned dst_samples
= dst
->texture
->nr_samples
;
1448 boolean has_depth
, has_stencil
, has_color
;
1449 boolean blit_stencil
, blit_depth
, blit_color
;
1450 void *sampler_state
;
1451 const struct util_format_description
*src_desc
=
1452 util_format_description(src
->format
);
1453 const struct util_format_description
*dst_desc
=
1454 util_format_description(dst
->format
);
1456 has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
&&
1457 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1458 has_depth
= util_format_has_depth(src_desc
) &&
1459 util_format_has_depth(dst_desc
);
1460 has_stencil
= util_format_has_stencil(src_desc
) &&
1461 util_format_has_stencil(dst_desc
);
1463 blit_color
= has_color
&& (mask
& PIPE_MASK_RGBA
);
1464 blit_depth
= has_depth
&& (mask
& PIPE_MASK_Z
);
1465 blit_stencil
= has_stencil
&& (mask
& PIPE_MASK_S
) &&
1466 ctx
->has_stencil_export
;
1468 if (!blit_stencil
&& !blit_depth
&& !blit_color
) {
1473 (dstbox
->width
== abs(srcbox
->width
) &&
1474 dstbox
->height
== abs(srcbox
->height
))) {
1475 filter
= PIPE_TEX_FILTER_NEAREST
;
1478 /* Check whether the states are properly saved. */
1479 blitter_set_running_flag(ctx
);
1480 blitter_check_saved_vertex_states(ctx
);
1481 blitter_check_saved_fragment_states(ctx
);
1482 blitter_check_saved_textures(ctx
);
1483 blitter_check_saved_fb_state(ctx
);
1484 blitter_disable_render_cond(ctx
);
1486 /* Initialize framebuffer state. */
1487 fb_state
.width
= dst
->width
;
1488 fb_state
.height
= dst
->height
;
1489 fb_state
.nr_cbufs
= blit_depth
|| blit_stencil
? 0 : 1;
1490 fb_state
.cbufs
[0] = NULL
;
1491 fb_state
.zsbuf
= NULL
;
1493 if (blit_depth
|| blit_stencil
) {
1494 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1496 if (blit_depth
&& blit_stencil
) {
1497 pipe
->bind_depth_stencil_alpha_state(pipe
,
1498 ctx
->dsa_write_depth_stencil
);
1499 ctx
->bind_fs_state(pipe
,
1500 blitter_get_fs_texfetch_depthstencil(ctx
, src_target
,
1502 } else if (blit_depth
) {
1503 pipe
->bind_depth_stencil_alpha_state(pipe
,
1504 ctx
->dsa_write_depth_keep_stencil
);
1505 ctx
->bind_fs_state(pipe
,
1506 blitter_get_fs_texfetch_depth(ctx
, src_target
,
1508 } else { /* is_stencil */
1509 pipe
->bind_depth_stencil_alpha_state(pipe
,
1510 ctx
->dsa_keep_depth_write_stencil
);
1511 ctx
->bind_fs_state(pipe
,
1512 blitter_get_fs_texfetch_stencil(ctx
, src_target
,
1517 pipe
->bind_blend_state(pipe
, ctx
->blend
[mask
& PIPE_MASK_RGBA
]);
1518 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1519 ctx
->bind_fs_state(pipe
,
1520 blitter_get_fs_texfetch_col(ctx
, src
->format
, src_target
,
1521 src_samples
, dst_samples
, filter
));
1524 /* Set the linear filter only for scaled color non-MSAA blits. */
1525 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
1526 if (src_target
== PIPE_TEXTURE_RECT
) {
1527 sampler_state
= ctx
->sampler_state_rect_linear
;
1529 sampler_state
= ctx
->sampler_state_linear
;
1532 if (src_target
== PIPE_TEXTURE_RECT
) {
1533 sampler_state
= ctx
->sampler_state_rect
;
1535 sampler_state
= ctx
->sampler_state
;
1540 if (blit_depth
&& blit_stencil
) {
1541 /* Setup two samplers, one for depth and the other one for stencil. */
1542 struct pipe_sampler_view templ
;
1543 struct pipe_sampler_view
*views
[2];
1544 void *samplers
[2] = {sampler_state
, sampler_state
};
1547 templ
.format
= util_format_stencil_only(templ
.format
);
1548 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1551 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1553 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, views
);
1554 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, samplers
);
1556 pipe_sampler_view_reference(&views
[1], NULL
);
1557 } else if (blit_stencil
) {
1558 /* Set a stencil-only sampler view for it not to sample depth instead. */
1559 struct pipe_sampler_view templ
;
1560 struct pipe_sampler_view
*view
;
1563 templ
.format
= util_format_stencil_only(templ
.format
);
1564 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1566 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1568 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &view
);
1569 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1570 0, 1, &sampler_state
);
1572 pipe_sampler_view_reference(&view
, NULL
);
1574 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src
);
1575 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1576 0, 1, &sampler_state
);
1579 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1581 pipe
->set_scissor_states(pipe
, 0, 1, scissor
);
1584 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
, FALSE
);
1585 blitter_set_dst_dimensions(ctx
, dst
->width
, dst
->height
);
1587 if ((src_target
== PIPE_TEXTURE_1D
||
1588 src_target
== PIPE_TEXTURE_2D
||
1589 src_target
== PIPE_TEXTURE_RECT
) &&
1591 /* Draw the quad with the draw_rectangle callback. */
1593 /* Set texture coordinates. - use a pipe color union
1594 * for interface purposes.
1595 * XXX pipe_color_union is a wrong name since we use that to set
1596 * texture coordinates too.
1598 union pipe_color_union coord
;
1599 get_texcoords(src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1600 srcbox
->x
+srcbox
->width
, srcbox
->y
+srcbox
->height
, coord
.f
);
1602 /* Set framebuffer state. */
1603 if (blit_depth
|| blit_stencil
) {
1604 fb_state
.zsbuf
= dst
;
1606 fb_state
.cbufs
[0] = dst
;
1608 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1611 pipe
->set_sample_mask(pipe
, ~0);
1612 blitter
->draw_rectangle(blitter
, dstbox
->x
, dstbox
->y
,
1613 dstbox
->x
+ dstbox
->width
,
1614 dstbox
->y
+ dstbox
->height
, 0,
1615 UTIL_BLITTER_ATTRIB_TEXCOORD
, &coord
);
1617 /* Draw the quad with the generic codepath. */
1619 for (dst_z
= 0; dst_z
< dstbox
->depth
; dst_z
++) {
1620 struct pipe_surface
*old
;
1621 float dst2src_scale
= srcbox
->depth
/ (float)dstbox
->depth
;
1623 /* Scale Z properly if the blit is scaled.
1625 * When downscaling, we want the coordinates centered, so that
1626 * mipmapping works for 3D textures. For example, when generating
1627 * a 4x4x4 level, this wouldn't average the pixels:
1629 * src Z: 0 1 2 3 4 5 6 7
1632 * Because the pixels are not centered below the pixels of the higher
1633 * level. Therefore, we want this:
1634 * src Z: 0 1 2 3 4 5 6 7
1637 * dst_offset defines the offset needed for centering the pixels and
1638 * it works with any scaling (not just 2x).
1640 float dst_offset
= ((srcbox
->depth
- 1) -
1641 (dstbox
->depth
- 1) * dst2src_scale
) * 0.5;
1642 float src_z
= (dst_z
+ dst_offset
) * dst2src_scale
;
1644 /* Set framebuffer state. */
1645 if (blit_depth
|| blit_stencil
) {
1646 fb_state
.zsbuf
= dst
;
1648 fb_state
.cbufs
[0] = dst
;
1650 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1652 /* See if we need to blit a multisample or singlesample buffer. */
1653 if (src_samples
== dst_samples
&& dst_samples
> 1) {
1655 unsigned i
, max_sample
= dst_samples
- 1;
1657 for (i
= 0; i
<= max_sample
; i
++) {
1658 pipe
->set_sample_mask(pipe
, 1 << i
);
1659 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1661 i
, srcbox
->x
, srcbox
->y
,
1662 srcbox
->x
+ srcbox
->width
,
1663 srcbox
->y
+ srcbox
->height
);
1664 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1665 dstbox
->x
+ dstbox
->width
,
1666 dstbox
->y
+ dstbox
->height
, 0, 1);
1669 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1670 pipe
->set_sample_mask(pipe
, ~0);
1671 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1672 srcbox
->z
+ src_z
, 0,
1673 srcbox
->x
, srcbox
->y
,
1674 srcbox
->x
+ srcbox
->width
,
1675 srcbox
->y
+ srcbox
->height
);
1676 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1677 dstbox
->x
+ dstbox
->width
,
1678 dstbox
->y
+ dstbox
->height
, 0, 1);
1681 /* Get the next surface or (if this is the last iteration)
1682 * just unreference the last one. */
1684 if (dst_z
< dstbox
->depth
-1) {
1685 dst
= ctx
->base
.get_next_surface_layer(ctx
->base
.pipe
, dst
);
1688 pipe_surface_reference(&old
, NULL
);
1693 blitter_restore_vertex_states(ctx
);
1694 blitter_restore_fragment_states(ctx
);
1695 blitter_restore_textures(ctx
);
1696 blitter_restore_fb_state(ctx
);
1698 pipe
->set_scissor_states(pipe
, 0, 1, &ctx
->base
.saved_scissor
);
1700 blitter_restore_render_cond(ctx
);
1701 blitter_unset_running_flag(ctx
);
1705 util_blitter_blit(struct blitter_context
*blitter
,
1706 const struct pipe_blit_info
*info
)
1708 struct pipe_resource
*dst
= info
->dst
.resource
;
1709 struct pipe_resource
*src
= info
->src
.resource
;
1710 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1711 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1712 struct pipe_surface
*dst_view
, dst_templ
;
1713 struct pipe_sampler_view src_templ
, *src_view
;
1715 /* Initialize the surface. */
1716 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
1718 dst_templ
.format
= info
->dst
.format
;
1719 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1721 /* Initialize the sampler view. */
1722 util_blitter_default_src_texture(&src_templ
, src
, info
->src
.level
);
1723 src_templ
.format
= info
->src
.format
;
1724 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1727 util_blitter_blit_generic(blitter
, dst_view
, &info
->dst
.box
,
1728 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
1729 info
->mask
, info
->filter
,
1730 info
->scissor_enable
? &info
->scissor
: NULL
);
1732 pipe_surface_reference(&dst_view
, NULL
);
1733 pipe_sampler_view_reference(&src_view
, NULL
);
1736 /* Clear a region of a color surface to a constant value. */
1737 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
1738 struct pipe_surface
*dstsurf
,
1739 const union pipe_color_union
*color
,
1740 unsigned dstx
, unsigned dsty
,
1741 unsigned width
, unsigned height
)
1743 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1744 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1745 struct pipe_framebuffer_state fb_state
;
1747 assert(dstsurf
->texture
);
1748 if (!dstsurf
->texture
)
1751 /* check the saved state */
1752 blitter_set_running_flag(ctx
);
1753 blitter_check_saved_vertex_states(ctx
);
1754 blitter_check_saved_fragment_states(ctx
);
1755 blitter_check_saved_fb_state(ctx
);
1756 blitter_disable_render_cond(ctx
);
1759 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
]);
1760 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1761 bind_fs_write_one_cbuf(ctx
);
1762 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1764 /* set a framebuffer state */
1765 fb_state
.width
= dstsurf
->width
;
1766 fb_state
.height
= dstsurf
->height
;
1767 fb_state
.nr_cbufs
= 1;
1768 fb_state
.cbufs
[0] = dstsurf
;
1770 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1771 pipe
->set_sample_mask(pipe
, ~0);
1773 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1774 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1775 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
1776 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1778 blitter_restore_vertex_states(ctx
);
1779 blitter_restore_fragment_states(ctx
);
1780 blitter_restore_fb_state(ctx
);
1781 blitter_restore_render_cond(ctx
);
1782 blitter_unset_running_flag(ctx
);
1785 /* Clear a region of a depth stencil surface. */
1786 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
1787 struct pipe_surface
*dstsurf
,
1788 unsigned clear_flags
,
1791 unsigned dstx
, unsigned dsty
,
1792 unsigned width
, unsigned height
)
1794 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1795 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1796 struct pipe_framebuffer_state fb_state
;
1797 struct pipe_stencil_ref sr
= { { 0 } };
1799 assert(dstsurf
->texture
);
1800 if (!dstsurf
->texture
)
1803 /* check the saved state */
1804 blitter_set_running_flag(ctx
);
1805 blitter_check_saved_vertex_states(ctx
);
1806 blitter_check_saved_fragment_states(ctx
);
1807 blitter_check_saved_fb_state(ctx
);
1808 blitter_disable_render_cond(ctx
);
1811 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1812 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1813 sr
.ref_value
[0] = stencil
& 0xff;
1814 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1815 pipe
->set_stencil_ref(pipe
, &sr
);
1817 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
1818 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1820 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
1821 sr
.ref_value
[0] = stencil
& 0xff;
1822 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1823 pipe
->set_stencil_ref(pipe
, &sr
);
1826 /* hmm that should be illegal probably, or make it a no-op somewhere */
1827 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1830 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1832 /* set a framebuffer state */
1833 fb_state
.width
= dstsurf
->width
;
1834 fb_state
.height
= dstsurf
->height
;
1835 fb_state
.nr_cbufs
= 0;
1836 fb_state
.cbufs
[0] = 0;
1837 fb_state
.zsbuf
= dstsurf
;
1838 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1839 pipe
->set_sample_mask(pipe
, ~0);
1841 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1842 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1843 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
,
1845 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
1847 blitter_restore_vertex_states(ctx
);
1848 blitter_restore_fragment_states(ctx
);
1849 blitter_restore_fb_state(ctx
);
1850 blitter_restore_render_cond(ctx
);
1851 blitter_unset_running_flag(ctx
);
1854 /* draw a rectangle across a region using a custom dsa stage - for r600g */
1855 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
1856 struct pipe_surface
*zsurf
,
1857 struct pipe_surface
*cbsurf
,
1858 unsigned sample_mask
,
1859 void *dsa_stage
, float depth
)
1861 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1862 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1863 struct pipe_framebuffer_state fb_state
;
1865 assert(zsurf
->texture
);
1866 if (!zsurf
->texture
)
1869 /* check the saved state */
1870 blitter_set_running_flag(ctx
);
1871 blitter_check_saved_vertex_states(ctx
);
1872 blitter_check_saved_fragment_states(ctx
);
1873 blitter_check_saved_fb_state(ctx
);
1874 blitter_disable_render_cond(ctx
);
1877 pipe
->bind_blend_state(pipe
, cbsurf
? ctx
->blend
[PIPE_MASK_RGBA
] :
1879 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
1881 bind_fs_write_one_cbuf(ctx
);
1884 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1886 /* set a framebuffer state */
1887 fb_state
.width
= zsurf
->width
;
1888 fb_state
.height
= zsurf
->height
;
1889 fb_state
.nr_cbufs
= 1;
1891 fb_state
.cbufs
[0] = cbsurf
;
1892 fb_state
.nr_cbufs
= 1;
1894 fb_state
.cbufs
[0] = NULL
;
1895 fb_state
.nr_cbufs
= 0;
1897 fb_state
.zsbuf
= zsurf
;
1898 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1899 pipe
->set_sample_mask(pipe
, sample_mask
);
1901 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1902 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
1903 blitter
->draw_rectangle(blitter
, 0, 0, zsurf
->width
, zsurf
->height
, depth
,
1904 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
1906 blitter_restore_vertex_states(ctx
);
1907 blitter_restore_fragment_states(ctx
);
1908 blitter_restore_fb_state(ctx
);
1909 blitter_restore_render_cond(ctx
);
1910 blitter_unset_running_flag(ctx
);
1913 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
1914 struct pipe_resource
*dst
,
1916 struct pipe_resource
*src
,
1920 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1921 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1922 struct pipe_vertex_buffer vb
;
1923 struct pipe_stream_output_target
*so_target
;
1924 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
1926 if (srcx
>= src
->width0
||
1927 dstx
>= dst
->width0
) {
1930 if (srcx
+ size
> src
->width0
) {
1931 size
= src
->width0
- srcx
;
1933 if (dstx
+ size
> dst
->width0
) {
1934 size
= dst
->width0
- dstx
;
1937 /* Drivers not capable of Stream Out should not call this function
1938 * in the first place. */
1939 assert(ctx
->has_stream_out
);
1941 /* Some alignment is required. */
1942 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
1943 !ctx
->has_stream_out
) {
1944 struct pipe_box box
;
1945 u_box_1d(srcx
, size
, &box
);
1946 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
1950 blitter_set_running_flag(ctx
);
1951 blitter_check_saved_vertex_states(ctx
);
1952 blitter_disable_render_cond(ctx
);
1955 vb
.buffer_offset
= srcx
;
1958 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1959 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[0]);
1960 bind_vs_pos_only(ctx
);
1961 if (ctx
->has_geometry_shader
)
1962 pipe
->bind_gs_state(pipe
, NULL
);
1963 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
1965 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
1966 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
1968 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
1970 blitter_restore_vertex_states(ctx
);
1971 blitter_restore_render_cond(ctx
);
1972 blitter_unset_running_flag(ctx
);
1973 pipe_so_target_reference(&so_target
, NULL
);
1976 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
1977 struct pipe_resource
*dst
,
1978 unsigned offset
, unsigned size
,
1979 unsigned num_channels
,
1980 const union pipe_color_union
*clear_value
)
1982 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1983 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1984 struct pipe_vertex_buffer vb
= {0};
1985 struct pipe_stream_output_target
*so_target
;
1986 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
1988 assert(num_channels
>= 1);
1989 assert(num_channels
<= 4);
1991 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
1993 * R600 uses this to initialize texture resources, so width0 might not be
1994 * what you think it is.
1997 /* Streamout is required. */
1998 if (!ctx
->has_stream_out
) {
1999 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2003 /* Some alignment is required. */
2004 if (offset
% 4 != 0 || size
% 4 != 0) {
2005 assert(!"Bad alignment in util_blitter_clear_buffer()");
2009 u_upload_data(ctx
->upload
, 0, num_channels
*4, clear_value
,
2010 &vb
.buffer_offset
, &vb
.buffer
);
2013 blitter_set_running_flag(ctx
);
2014 blitter_check_saved_vertex_states(ctx
);
2015 blitter_disable_render_cond(ctx
);
2017 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2018 pipe
->bind_vertex_elements_state(pipe
,
2019 ctx
->velem_state_readbuf
[num_channels
-1]);
2020 bind_vs_pos_only(ctx
);
2021 if (ctx
->has_geometry_shader
)
2022 pipe
->bind_gs_state(pipe
, NULL
);
2023 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2025 so_target
= pipe
->create_stream_output_target(pipe
, dst
, offset
, size
);
2026 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2028 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2030 blitter_restore_vertex_states(ctx
);
2031 blitter_restore_render_cond(ctx
);
2032 blitter_unset_running_flag(ctx
);
2033 pipe_so_target_reference(&so_target
, NULL
);
2034 pipe_resource_reference(&vb
.buffer
, NULL
);
2037 /* probably radeon specific */
2038 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
2039 struct pipe_resource
*dst
,
2042 struct pipe_resource
*src
,
2044 unsigned sample_mask
,
2046 enum pipe_format format
)
2048 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2049 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2050 struct pipe_framebuffer_state fb_state
;
2051 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
2053 blitter_set_running_flag(ctx
);
2054 blitter_check_saved_vertex_states(ctx
);
2055 blitter_check_saved_fragment_states(ctx
);
2056 blitter_disable_render_cond(ctx
);
2059 pipe
->bind_blend_state(pipe
, custom_blend
);
2060 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2061 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2062 bind_fs_write_one_cbuf(ctx
);
2063 pipe
->set_sample_mask(pipe
, sample_mask
);
2065 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
2066 surf_tmpl
.format
= format
;
2067 surf_tmpl
.u
.tex
.level
= dst_level
;
2068 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
2069 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
2071 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
2073 surf_tmpl
.u
.tex
.level
= 0;
2074 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
2075 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
2077 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
2079 /* set a framebuffer state */
2080 fb_state
.width
= src
->width0
;
2081 fb_state
.height
= src
->height0
;
2082 fb_state
.nr_cbufs
= 2;
2083 fb_state
.cbufs
[0] = srcsurf
;
2084 fb_state
.cbufs
[1] = dstsurf
;
2085 fb_state
.zsbuf
= NULL
;
2086 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2088 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2089 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
2090 blitter
->draw_rectangle(blitter
, 0, 0, src
->width0
, src
->height0
,
2092 blitter_restore_fb_state(ctx
);
2093 blitter_restore_vertex_states(ctx
);
2094 blitter_restore_fragment_states(ctx
);
2095 blitter_restore_render_cond(ctx
);
2096 blitter_unset_running_flag(ctx
);
2098 pipe_surface_reference(&srcsurf
, NULL
);
2099 pipe_surface_reference(&dstsurf
, NULL
);
2102 void util_blitter_custom_color(struct blitter_context
*blitter
,
2103 struct pipe_surface
*dstsurf
,
2106 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2107 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2108 struct pipe_framebuffer_state fb_state
;
2110 assert(dstsurf
->texture
);
2111 if (!dstsurf
->texture
)
2114 /* check the saved state */
2115 blitter_set_running_flag(ctx
);
2116 blitter_check_saved_vertex_states(ctx
);
2117 blitter_check_saved_fragment_states(ctx
);
2118 blitter_check_saved_fb_state(ctx
);
2119 blitter_disable_render_cond(ctx
);
2122 pipe
->bind_blend_state(pipe
, custom_blend
? custom_blend
2123 : ctx
->blend
[PIPE_MASK_RGBA
]);
2124 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2125 bind_fs_write_one_cbuf(ctx
);
2126 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2127 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2129 /* set a framebuffer state */
2130 fb_state
.width
= dstsurf
->width
;
2131 fb_state
.height
= dstsurf
->height
;
2132 fb_state
.nr_cbufs
= 1;
2133 fb_state
.cbufs
[0] = dstsurf
;
2135 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2136 pipe
->set_sample_mask(pipe
, ~0);
2138 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2139 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2140 blitter
->draw_rectangle(blitter
, 0, 0, dstsurf
->width
, dstsurf
->height
,
2143 blitter_restore_vertex_states(ctx
);
2144 blitter_restore_fragment_states(ctx
);
2145 blitter_restore_fb_state(ctx
);
2146 blitter_restore_render_cond(ctx
);
2147 blitter_unset_running_flag(ctx
);