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_screen
*screen
= blitter
->pipe
->screen
;
1016 unsigned samples
, j
, f
, target
, max_samples
;
1017 boolean has_arraytex
, has_cubearraytex
;
1019 max_samples
= ctx
->has_texture_multisample
? 2 : 1;
1020 has_arraytex
= screen
->get_param(screen
,
1021 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
) != 0;
1022 has_cubearraytex
= screen
->get_param(screen
,
1023 PIPE_CAP_CUBE_MAP_ARRAY
) != 0;
1025 /* It only matters if i <= 1 or > 1. */
1026 for (samples
= 1; samples
<= max_samples
; samples
++) {
1027 for (target
= PIPE_TEXTURE_1D
; target
< PIPE_MAX_TEXTURE_TYPES
; target
++) {
1028 if (!has_arraytex
&&
1029 (target
== PIPE_TEXTURE_1D_ARRAY
||
1030 target
== PIPE_TEXTURE_2D_ARRAY
)) {
1033 if (!has_cubearraytex
&&
1034 (target
== PIPE_TEXTURE_CUBE_ARRAY
))
1038 (target
!= PIPE_TEXTURE_2D
&&
1039 target
!= PIPE_TEXTURE_2D_ARRAY
))
1042 /* If samples == 1, the shaders read one texel. If samples >= 1,
1043 * they read one sample.
1045 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
, target
,
1046 samples
, samples
, 0);
1047 blitter_get_fs_texfetch_depth(ctx
, target
, samples
);
1048 if (ctx
->has_stencil_export
) {
1049 blitter_get_fs_texfetch_depthstencil(ctx
, target
, samples
);
1050 blitter_get_fs_texfetch_stencil(ctx
, target
, samples
);
1056 /* MSAA resolve shaders. */
1057 for (j
= 2; j
< 32; j
++) {
1058 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R32_FLOAT
,
1060 PIPE_BIND_SAMPLER_VIEW
)) {
1064 for (f
= 0; f
< 2; f
++) {
1065 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
, target
,
1067 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
, target
,
1069 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
, target
,
1076 ctx
->cached_all_shaders
= TRUE
;
1079 static void blitter_set_common_draw_rect_state(struct blitter_context_priv
*ctx
,
1083 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1085 pipe
->bind_rasterizer_state(pipe
, scissor
? ctx
->rs_state_scissor
1088 bind_vs_layered(ctx
);
1090 bind_vs_passthrough(ctx
);
1092 if (ctx
->has_geometry_shader
)
1093 pipe
->bind_gs_state(pipe
, NULL
);
1094 if (ctx
->has_stream_out
)
1095 pipe
->set_stream_output_targets(pipe
, 0, NULL
, NULL
);
1098 static void blitter_draw(struct blitter_context_priv
*ctx
,
1099 int x1
, int y1
, int x2
, int y2
, float depth
,
1100 unsigned num_instances
)
1102 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1103 struct pipe_vertex_buffer vb
= {0};
1105 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
1107 vb
.stride
= 8 * sizeof(float);
1109 u_upload_data(ctx
->upload
, 0, sizeof(ctx
->vertices
), ctx
->vertices
,
1110 &vb
.buffer_offset
, &vb
.buffer
);
1111 u_upload_unmap(ctx
->upload
);
1113 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1114 util_draw_arrays_instanced(pipe
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
1116 pipe_resource_reference(&vb
.buffer
, NULL
);
1119 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
1120 int x1
, int y1
, int x2
, int y2
, float depth
,
1121 enum blitter_attrib_type type
,
1122 const union pipe_color_union
*attrib
)
1124 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1127 case UTIL_BLITTER_ATTRIB_COLOR
:
1128 blitter_set_clear_color(ctx
, attrib
);
1131 case UTIL_BLITTER_ATTRIB_TEXCOORD
:
1132 set_texcoords_in_vertices(attrib
->f
, &ctx
->vertices
[0][1][0], 8);
1138 blitter_draw(ctx
, x1
, y1
, x2
, y2
, depth
, 1);
1141 static void *get_clear_blend_state(struct blitter_context_priv
*ctx
,
1142 unsigned clear_buffers
)
1144 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1147 clear_buffers
&= PIPE_CLEAR_COLOR
;
1149 /* Return an existing blend state. */
1151 return ctx
->blend
[0];
1153 index
= GET_CLEAR_BLEND_STATE_IDX(clear_buffers
);
1155 if (ctx
->blend_clear
[index
])
1156 return ctx
->blend_clear
[index
];
1158 /* Create a new one. */
1160 struct pipe_blend_state blend
= {0};
1163 blend
.independent_blend_enable
= 1;
1165 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1166 if (clear_buffers
& (PIPE_CLEAR_COLOR0
<< i
)) {
1167 blend
.rt
[i
].colormask
= PIPE_MASK_RGBA
;
1171 ctx
->blend_clear
[index
] = pipe
->create_blend_state(pipe
, &blend
);
1173 return ctx
->blend_clear
[index
];
1176 static void util_blitter_clear_custom(struct blitter_context
*blitter
,
1177 unsigned width
, unsigned height
,
1178 unsigned num_layers
,
1179 unsigned clear_buffers
,
1180 const union pipe_color_union
*color
,
1181 double depth
, unsigned stencil
,
1182 void *custom_blend
, void *custom_dsa
)
1184 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1185 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1186 struct pipe_stencil_ref sr
= { { 0 } };
1188 assert(ctx
->has_layered
|| num_layers
<= 1);
1190 blitter_set_running_flag(ctx
);
1191 blitter_check_saved_vertex_states(ctx
);
1192 blitter_check_saved_fragment_states(ctx
);
1193 blitter_disable_render_cond(ctx
);
1197 pipe
->bind_blend_state(pipe
, custom_blend
);
1199 pipe
->bind_blend_state(pipe
, get_clear_blend_state(ctx
, clear_buffers
));
1203 pipe
->bind_depth_stencil_alpha_state(pipe
, custom_dsa
);
1204 } else if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1205 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1206 } else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
1207 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1208 } else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
1209 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1211 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1214 sr
.ref_value
[0] = stencil
& 0xff;
1215 pipe
->set_stencil_ref(pipe
, &sr
);
1217 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1218 bind_fs_write_all_cbufs(ctx
);
1219 pipe
->set_sample_mask(pipe
, ~0);
1221 blitter_set_dst_dimensions(ctx
, width
, height
);
1223 if (num_layers
> 1 && ctx
->has_layered
) {
1224 blitter_set_common_draw_rect_state(ctx
, FALSE
, TRUE
);
1225 blitter_set_clear_color(ctx
, color
);
1226 blitter_draw(ctx
, 0, 0, width
, height
, depth
, num_layers
);
1229 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1230 blitter
->draw_rectangle(blitter
, 0, 0, width
, height
, (float) depth
,
1231 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1234 blitter_restore_vertex_states(ctx
);
1235 blitter_restore_fragment_states(ctx
);
1236 blitter_restore_render_cond(ctx
);
1237 blitter_unset_running_flag(ctx
);
1240 void util_blitter_clear(struct blitter_context
*blitter
,
1241 unsigned width
, unsigned height
, unsigned num_layers
,
1242 unsigned clear_buffers
,
1243 const union pipe_color_union
*color
,
1244 double depth
, unsigned stencil
)
1246 util_blitter_clear_custom(blitter
, width
, height
, num_layers
,
1247 clear_buffers
, color
, depth
, stencil
,
1251 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1252 unsigned width
, unsigned height
,
1253 double depth
, void *custom_dsa
)
1255 static const union pipe_color_union color
;
1256 util_blitter_clear_custom(blitter
, width
, height
, 0, 0, &color
, depth
, 0,
1260 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1261 struct pipe_resource
*dst
,
1265 memset(dst_templ
, 0, sizeof(*dst_templ
));
1266 dst_templ
->format
= util_format_linear(dst
->format
);
1267 dst_templ
->u
.tex
.level
= dstlevel
;
1268 dst_templ
->u
.tex
.first_layer
= dstz
;
1269 dst_templ
->u
.tex
.last_layer
= dstz
;
1272 static struct pipe_surface
*
1273 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
1274 struct pipe_surface
*surf
)
1276 struct pipe_surface dst_templ
;
1278 memset(&dst_templ
, 0, sizeof(dst_templ
));
1279 dst_templ
.format
= surf
->format
;
1280 dst_templ
.u
.tex
.level
= surf
->u
.tex
.level
;
1281 dst_templ
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
+ 1;
1282 dst_templ
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
+ 1;
1284 return pipe
->create_surface(pipe
, surf
->texture
, &dst_templ
);
1287 void util_blitter_default_src_texture(struct pipe_sampler_view
*src_templ
,
1288 struct pipe_resource
*src
,
1291 memset(src_templ
, 0, sizeof(*src_templ
));
1292 src_templ
->format
= util_format_linear(src
->format
);
1293 src_templ
->u
.tex
.first_level
= srclevel
;
1294 src_templ
->u
.tex
.last_level
= srclevel
;
1295 src_templ
->u
.tex
.first_layer
= 0;
1296 src_templ
->u
.tex
.last_layer
=
1297 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1298 : src
->array_size
- 1;
1299 src_templ
->swizzle_r
= PIPE_SWIZZLE_RED
;
1300 src_templ
->swizzle_g
= PIPE_SWIZZLE_GREEN
;
1301 src_templ
->swizzle_b
= PIPE_SWIZZLE_BLUE
;
1302 src_templ
->swizzle_a
= PIPE_SWIZZLE_ALPHA
;
1305 static boolean
is_blit_generic_supported(struct blitter_context
*blitter
,
1306 const struct pipe_resource
*dst
,
1307 enum pipe_format dst_format
,
1308 const struct pipe_resource
*src
,
1309 enum pipe_format src_format
,
1312 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1313 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1317 const struct util_format_description
*desc
=
1318 util_format_description(dst_format
);
1319 boolean dst_has_stencil
= util_format_has_stencil(desc
);
1321 /* Stencil export must be supported for stencil copy. */
1322 if ((mask
& PIPE_MASK_S
) && dst_has_stencil
&&
1323 !ctx
->has_stencil_export
) {
1327 if (dst_has_stencil
|| util_format_has_depth(desc
))
1328 bind
= PIPE_BIND_DEPTH_STENCIL
;
1330 bind
= PIPE_BIND_RENDER_TARGET
;
1332 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1333 dst
->nr_samples
, bind
)) {
1339 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1343 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1344 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
)) {
1348 /* Check stencil sampler support for stencil copy. */
1349 if (mask
& PIPE_MASK_S
) {
1350 if (util_format_has_stencil(util_format_description(src_format
))) {
1351 enum pipe_format stencil_format
=
1352 util_format_stencil_only(src_format
);
1353 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1355 if (stencil_format
!= src_format
&&
1356 !screen
->is_format_supported(screen
, stencil_format
,
1357 src
->target
, src
->nr_samples
,
1358 PIPE_BIND_SAMPLER_VIEW
)) {
1368 boolean
util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1369 const struct pipe_resource
*dst
,
1370 const struct pipe_resource
*src
)
1372 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1373 src
, src
->format
, PIPE_MASK_RGBAZS
);
1376 boolean
util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1377 const struct pipe_blit_info
*info
)
1379 return is_blit_generic_supported(blitter
,
1380 info
->dst
.resource
, info
->dst
.format
,
1381 info
->src
.resource
, info
->src
.format
,
1385 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1386 struct pipe_resource
*dst
,
1388 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1389 struct pipe_resource
*src
,
1391 const struct pipe_box
*srcbox
)
1393 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1394 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1395 struct pipe_surface
*dst_view
, dst_templ
;
1396 struct pipe_sampler_view src_templ
, *src_view
;
1397 struct pipe_box dstbox
;
1400 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1402 u_box_3d(dstx
, dsty
, dstz
, abs(srcbox
->width
), abs(srcbox
->height
),
1403 abs(srcbox
->depth
), &dstbox
);
1405 /* Initialize the surface. */
1406 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1407 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1409 /* Initialize the sampler view. */
1410 util_blitter_default_src_texture(&src_templ
, src
, src_level
);
1411 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1414 util_blitter_blit_generic(blitter
, dst_view
, &dstbox
,
1415 src_view
, srcbox
, src
->width0
, src
->height0
,
1416 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
);
1418 pipe_surface_reference(&dst_view
, NULL
);
1419 pipe_sampler_view_reference(&src_view
, NULL
);
1422 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1423 struct pipe_surface
*dst
,
1424 const struct pipe_box
*dstbox
,
1425 struct pipe_sampler_view
*src
,
1426 const struct pipe_box
*srcbox
,
1427 unsigned src_width0
, unsigned src_height0
,
1428 unsigned mask
, unsigned filter
,
1429 const struct pipe_scissor_state
*scissor
)
1431 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1432 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1433 struct pipe_framebuffer_state fb_state
;
1434 enum pipe_texture_target src_target
= src
->texture
->target
;
1435 unsigned src_samples
= src
->texture
->nr_samples
;
1436 unsigned dst_samples
= dst
->texture
->nr_samples
;
1437 boolean has_depth
, has_stencil
, has_color
;
1438 boolean blit_stencil
, blit_depth
, blit_color
;
1439 void *sampler_state
;
1440 const struct util_format_description
*src_desc
=
1441 util_format_description(src
->format
);
1442 const struct util_format_description
*dst_desc
=
1443 util_format_description(dst
->format
);
1445 has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
&&
1446 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1447 has_depth
= util_format_has_depth(src_desc
) &&
1448 util_format_has_depth(dst_desc
);
1449 has_stencil
= util_format_has_stencil(src_desc
) &&
1450 util_format_has_stencil(dst_desc
);
1452 blit_color
= has_color
&& (mask
& PIPE_MASK_RGBA
);
1453 blit_depth
= has_depth
&& (mask
& PIPE_MASK_Z
);
1454 blit_stencil
= has_stencil
&& (mask
& PIPE_MASK_S
) &&
1455 ctx
->has_stencil_export
;
1457 if (!blit_stencil
&& !blit_depth
&& !blit_color
) {
1462 (dstbox
->width
== abs(srcbox
->width
) &&
1463 dstbox
->height
== abs(srcbox
->height
))) {
1464 filter
= PIPE_TEX_FILTER_NEAREST
;
1467 /* Check whether the states are properly saved. */
1468 blitter_set_running_flag(ctx
);
1469 blitter_check_saved_vertex_states(ctx
);
1470 blitter_check_saved_fragment_states(ctx
);
1471 blitter_check_saved_textures(ctx
);
1472 blitter_check_saved_fb_state(ctx
);
1473 blitter_disable_render_cond(ctx
);
1475 /* Initialize framebuffer state. */
1476 fb_state
.width
= dst
->width
;
1477 fb_state
.height
= dst
->height
;
1478 fb_state
.nr_cbufs
= blit_depth
|| blit_stencil
? 0 : 1;
1479 fb_state
.cbufs
[0] = NULL
;
1480 fb_state
.zsbuf
= NULL
;
1482 if (blit_depth
|| blit_stencil
) {
1483 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1485 if (blit_depth
&& blit_stencil
) {
1486 pipe
->bind_depth_stencil_alpha_state(pipe
,
1487 ctx
->dsa_write_depth_stencil
);
1488 ctx
->bind_fs_state(pipe
,
1489 blitter_get_fs_texfetch_depthstencil(ctx
, src_target
,
1491 } else if (blit_depth
) {
1492 pipe
->bind_depth_stencil_alpha_state(pipe
,
1493 ctx
->dsa_write_depth_keep_stencil
);
1494 ctx
->bind_fs_state(pipe
,
1495 blitter_get_fs_texfetch_depth(ctx
, src_target
,
1497 } else { /* is_stencil */
1498 pipe
->bind_depth_stencil_alpha_state(pipe
,
1499 ctx
->dsa_keep_depth_write_stencil
);
1500 ctx
->bind_fs_state(pipe
,
1501 blitter_get_fs_texfetch_stencil(ctx
, src_target
,
1506 pipe
->bind_blend_state(pipe
, ctx
->blend
[mask
& PIPE_MASK_RGBA
]);
1507 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1508 ctx
->bind_fs_state(pipe
,
1509 blitter_get_fs_texfetch_col(ctx
, src
->format
, src_target
,
1510 src_samples
, dst_samples
, filter
));
1513 /* Set the linear filter only for scaled color non-MSAA blits. */
1514 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
1515 if (src_target
== PIPE_TEXTURE_RECT
) {
1516 sampler_state
= ctx
->sampler_state_rect_linear
;
1518 sampler_state
= ctx
->sampler_state_linear
;
1521 if (src_target
== PIPE_TEXTURE_RECT
) {
1522 sampler_state
= ctx
->sampler_state_rect
;
1524 sampler_state
= ctx
->sampler_state
;
1529 if (blit_depth
&& blit_stencil
) {
1530 /* Setup two samplers, one for depth and the other one for stencil. */
1531 struct pipe_sampler_view templ
;
1532 struct pipe_sampler_view
*views
[2];
1533 void *samplers
[2] = {sampler_state
, sampler_state
};
1536 templ
.format
= util_format_stencil_only(templ
.format
);
1537 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1540 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1542 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, views
);
1543 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, samplers
);
1545 pipe_sampler_view_reference(&views
[1], NULL
);
1546 } else if (blit_stencil
) {
1547 /* Set a stencil-only sampler view for it not to sample depth instead. */
1548 struct pipe_sampler_view templ
;
1549 struct pipe_sampler_view
*view
;
1552 templ
.format
= util_format_stencil_only(templ
.format
);
1553 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1555 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1557 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &view
);
1558 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1559 0, 1, &sampler_state
);
1561 pipe_sampler_view_reference(&view
, NULL
);
1563 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src
);
1564 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1565 0, 1, &sampler_state
);
1568 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1570 pipe
->set_scissor_states(pipe
, 0, 1, scissor
);
1573 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
, FALSE
);
1574 blitter_set_dst_dimensions(ctx
, dst
->width
, dst
->height
);
1576 if ((src_target
== PIPE_TEXTURE_1D
||
1577 src_target
== PIPE_TEXTURE_2D
||
1578 src_target
== PIPE_TEXTURE_RECT
) &&
1580 /* Draw the quad with the draw_rectangle callback. */
1582 /* Set texture coordinates. - use a pipe color union
1583 * for interface purposes.
1584 * XXX pipe_color_union is a wrong name since we use that to set
1585 * texture coordinates too.
1587 union pipe_color_union coord
;
1588 get_texcoords(src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1589 srcbox
->x
+srcbox
->width
, srcbox
->y
+srcbox
->height
, coord
.f
);
1591 /* Set framebuffer state. */
1592 if (blit_depth
|| blit_stencil
) {
1593 fb_state
.zsbuf
= dst
;
1595 fb_state
.cbufs
[0] = dst
;
1597 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1600 pipe
->set_sample_mask(pipe
, ~0);
1601 blitter
->draw_rectangle(blitter
, dstbox
->x
, dstbox
->y
,
1602 dstbox
->x
+ dstbox
->width
,
1603 dstbox
->y
+ dstbox
->height
, 0,
1604 UTIL_BLITTER_ATTRIB_TEXCOORD
, &coord
);
1606 /* Draw the quad with the generic codepath. */
1608 for (dst_z
= 0; dst_z
< dstbox
->depth
; dst_z
++) {
1609 struct pipe_surface
*old
;
1610 float dst2src_scale
= srcbox
->depth
/ (float)dstbox
->depth
;
1612 /* Scale Z properly if the blit is scaled.
1614 * When downscaling, we want the coordinates centered, so that
1615 * mipmapping works for 3D textures. For example, when generating
1616 * a 4x4x4 level, this wouldn't average the pixels:
1618 * src Z: 0 1 2 3 4 5 6 7
1621 * Because the pixels are not centered below the pixels of the higher
1622 * level. Therefore, we want this:
1623 * src Z: 0 1 2 3 4 5 6 7
1626 * dst_offset defines the offset needed for centering the pixels and
1627 * it works with any scaling (not just 2x).
1629 float dst_offset
= ((srcbox
->depth
- 1) -
1630 (dstbox
->depth
- 1) * dst2src_scale
) * 0.5;
1631 float src_z
= (dst_z
+ dst_offset
) * dst2src_scale
;
1633 /* Set framebuffer state. */
1634 if (blit_depth
|| blit_stencil
) {
1635 fb_state
.zsbuf
= dst
;
1637 fb_state
.cbufs
[0] = dst
;
1639 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1641 /* See if we need to blit a multisample or singlesample buffer. */
1642 if (src_samples
== dst_samples
&& dst_samples
> 1) {
1644 unsigned i
, max_sample
= dst_samples
- 1;
1646 for (i
= 0; i
<= max_sample
; i
++) {
1647 pipe
->set_sample_mask(pipe
, 1 << i
);
1648 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1650 i
, srcbox
->x
, srcbox
->y
,
1651 srcbox
->x
+ srcbox
->width
,
1652 srcbox
->y
+ srcbox
->height
);
1653 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1654 dstbox
->x
+ dstbox
->width
,
1655 dstbox
->y
+ dstbox
->height
, 0, 1);
1658 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1659 pipe
->set_sample_mask(pipe
, ~0);
1660 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1661 srcbox
->z
+ src_z
, 0,
1662 srcbox
->x
, srcbox
->y
,
1663 srcbox
->x
+ srcbox
->width
,
1664 srcbox
->y
+ srcbox
->height
);
1665 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1666 dstbox
->x
+ dstbox
->width
,
1667 dstbox
->y
+ dstbox
->height
, 0, 1);
1670 /* Get the next surface or (if this is the last iteration)
1671 * just unreference the last one. */
1673 if (dst_z
< dstbox
->depth
-1) {
1674 dst
= ctx
->base
.get_next_surface_layer(ctx
->base
.pipe
, dst
);
1677 pipe_surface_reference(&old
, NULL
);
1682 blitter_restore_vertex_states(ctx
);
1683 blitter_restore_fragment_states(ctx
);
1684 blitter_restore_textures(ctx
);
1685 blitter_restore_fb_state(ctx
);
1687 pipe
->set_scissor_states(pipe
, 0, 1, &ctx
->base
.saved_scissor
);
1689 blitter_restore_render_cond(ctx
);
1690 blitter_unset_running_flag(ctx
);
1694 util_blitter_blit(struct blitter_context
*blitter
,
1695 const struct pipe_blit_info
*info
)
1697 struct pipe_resource
*dst
= info
->dst
.resource
;
1698 struct pipe_resource
*src
= info
->src
.resource
;
1699 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1700 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1701 struct pipe_surface
*dst_view
, dst_templ
;
1702 struct pipe_sampler_view src_templ
, *src_view
;
1704 /* Initialize the surface. */
1705 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
1707 dst_templ
.format
= info
->dst
.format
;
1708 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1710 /* Initialize the sampler view. */
1711 util_blitter_default_src_texture(&src_templ
, src
, info
->src
.level
);
1712 src_templ
.format
= info
->src
.format
;
1713 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1716 util_blitter_blit_generic(blitter
, dst_view
, &info
->dst
.box
,
1717 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
1718 info
->mask
, info
->filter
,
1719 info
->scissor_enable
? &info
->scissor
: NULL
);
1721 pipe_surface_reference(&dst_view
, NULL
);
1722 pipe_sampler_view_reference(&src_view
, NULL
);
1725 /* Clear a region of a color surface to a constant value. */
1726 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
1727 struct pipe_surface
*dstsurf
,
1728 const union pipe_color_union
*color
,
1729 unsigned dstx
, unsigned dsty
,
1730 unsigned width
, unsigned height
)
1732 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1733 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1734 struct pipe_framebuffer_state fb_state
;
1736 assert(dstsurf
->texture
);
1737 if (!dstsurf
->texture
)
1740 /* check the saved state */
1741 blitter_set_running_flag(ctx
);
1742 blitter_check_saved_vertex_states(ctx
);
1743 blitter_check_saved_fragment_states(ctx
);
1744 blitter_check_saved_fb_state(ctx
);
1745 blitter_disable_render_cond(ctx
);
1748 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
]);
1749 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1750 bind_fs_write_one_cbuf(ctx
);
1751 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1753 /* set a framebuffer state */
1754 fb_state
.width
= dstsurf
->width
;
1755 fb_state
.height
= dstsurf
->height
;
1756 fb_state
.nr_cbufs
= 1;
1757 fb_state
.cbufs
[0] = dstsurf
;
1759 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1760 pipe
->set_sample_mask(pipe
, ~0);
1762 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1763 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1764 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
1765 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1767 blitter_restore_vertex_states(ctx
);
1768 blitter_restore_fragment_states(ctx
);
1769 blitter_restore_fb_state(ctx
);
1770 blitter_restore_render_cond(ctx
);
1771 blitter_unset_running_flag(ctx
);
1774 /* Clear a region of a depth stencil surface. */
1775 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
1776 struct pipe_surface
*dstsurf
,
1777 unsigned clear_flags
,
1780 unsigned dstx
, unsigned dsty
,
1781 unsigned width
, unsigned height
)
1783 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1784 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1785 struct pipe_framebuffer_state fb_state
;
1786 struct pipe_stencil_ref sr
= { { 0 } };
1788 assert(dstsurf
->texture
);
1789 if (!dstsurf
->texture
)
1792 /* check the saved state */
1793 blitter_set_running_flag(ctx
);
1794 blitter_check_saved_vertex_states(ctx
);
1795 blitter_check_saved_fragment_states(ctx
);
1796 blitter_check_saved_fb_state(ctx
);
1797 blitter_disable_render_cond(ctx
);
1800 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1801 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1802 sr
.ref_value
[0] = stencil
& 0xff;
1803 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1804 pipe
->set_stencil_ref(pipe
, &sr
);
1806 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
1807 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1809 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
1810 sr
.ref_value
[0] = stencil
& 0xff;
1811 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1812 pipe
->set_stencil_ref(pipe
, &sr
);
1815 /* hmm that should be illegal probably, or make it a no-op somewhere */
1816 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1819 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1821 /* set a framebuffer state */
1822 fb_state
.width
= dstsurf
->width
;
1823 fb_state
.height
= dstsurf
->height
;
1824 fb_state
.nr_cbufs
= 0;
1825 fb_state
.cbufs
[0] = 0;
1826 fb_state
.zsbuf
= dstsurf
;
1827 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1828 pipe
->set_sample_mask(pipe
, ~0);
1830 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1831 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1832 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
,
1834 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
1836 blitter_restore_vertex_states(ctx
);
1837 blitter_restore_fragment_states(ctx
);
1838 blitter_restore_fb_state(ctx
);
1839 blitter_restore_render_cond(ctx
);
1840 blitter_unset_running_flag(ctx
);
1843 /* draw a rectangle across a region using a custom dsa stage - for r600g */
1844 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
1845 struct pipe_surface
*zsurf
,
1846 struct pipe_surface
*cbsurf
,
1847 unsigned sample_mask
,
1848 void *dsa_stage
, float depth
)
1850 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1851 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1852 struct pipe_framebuffer_state fb_state
;
1854 assert(zsurf
->texture
);
1855 if (!zsurf
->texture
)
1858 /* check the saved state */
1859 blitter_set_running_flag(ctx
);
1860 blitter_check_saved_vertex_states(ctx
);
1861 blitter_check_saved_fragment_states(ctx
);
1862 blitter_check_saved_fb_state(ctx
);
1863 blitter_disable_render_cond(ctx
);
1866 pipe
->bind_blend_state(pipe
, cbsurf
? ctx
->blend
[PIPE_MASK_RGBA
] :
1868 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
1870 bind_fs_write_one_cbuf(ctx
);
1873 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1875 /* set a framebuffer state */
1876 fb_state
.width
= zsurf
->width
;
1877 fb_state
.height
= zsurf
->height
;
1878 fb_state
.nr_cbufs
= 1;
1880 fb_state
.cbufs
[0] = cbsurf
;
1881 fb_state
.nr_cbufs
= 1;
1883 fb_state
.cbufs
[0] = NULL
;
1884 fb_state
.nr_cbufs
= 0;
1886 fb_state
.zsbuf
= zsurf
;
1887 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1888 pipe
->set_sample_mask(pipe
, sample_mask
);
1890 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1891 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
1892 blitter
->draw_rectangle(blitter
, 0, 0, zsurf
->width
, zsurf
->height
, depth
,
1893 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
1895 blitter_restore_vertex_states(ctx
);
1896 blitter_restore_fragment_states(ctx
);
1897 blitter_restore_fb_state(ctx
);
1898 blitter_restore_render_cond(ctx
);
1899 blitter_unset_running_flag(ctx
);
1902 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
1903 struct pipe_resource
*dst
,
1905 struct pipe_resource
*src
,
1909 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1910 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1911 struct pipe_vertex_buffer vb
;
1912 struct pipe_stream_output_target
*so_target
;
1913 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
1915 if (srcx
>= src
->width0
||
1916 dstx
>= dst
->width0
) {
1919 if (srcx
+ size
> src
->width0
) {
1920 size
= src
->width0
- srcx
;
1922 if (dstx
+ size
> dst
->width0
) {
1923 size
= dst
->width0
- dstx
;
1926 /* Drivers not capable of Stream Out should not call this function
1927 * in the first place. */
1928 assert(ctx
->has_stream_out
);
1930 /* Some alignment is required. */
1931 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
1932 !ctx
->has_stream_out
) {
1933 struct pipe_box box
;
1934 u_box_1d(srcx
, size
, &box
);
1935 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
1939 blitter_set_running_flag(ctx
);
1940 blitter_check_saved_vertex_states(ctx
);
1941 blitter_disable_render_cond(ctx
);
1944 vb
.buffer_offset
= srcx
;
1947 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1948 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[0]);
1949 bind_vs_pos_only(ctx
);
1950 if (ctx
->has_geometry_shader
)
1951 pipe
->bind_gs_state(pipe
, NULL
);
1952 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
1954 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
1955 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
1957 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
1959 blitter_restore_vertex_states(ctx
);
1960 blitter_restore_render_cond(ctx
);
1961 blitter_unset_running_flag(ctx
);
1962 pipe_so_target_reference(&so_target
, NULL
);
1965 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
1966 struct pipe_resource
*dst
,
1967 unsigned offset
, unsigned size
,
1968 unsigned num_channels
,
1969 const union pipe_color_union
*clear_value
)
1971 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1972 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1973 struct pipe_vertex_buffer vb
= {0};
1974 struct pipe_stream_output_target
*so_target
;
1975 unsigned offsets
[PIPE_MAX_SO_BUFFERS
] = {0};
1977 assert(num_channels
>= 1);
1978 assert(num_channels
<= 4);
1980 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
1982 * R600 uses this to initialize texture resources, so width0 might not be
1983 * what you think it is.
1986 /* Streamout is required. */
1987 if (!ctx
->has_stream_out
) {
1988 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
1992 /* Some alignment is required. */
1993 if (offset
% 4 != 0 || size
% 4 != 0) {
1994 assert(!"Bad alignment in util_blitter_clear_buffer()");
1998 u_upload_data(ctx
->upload
, 0, num_channels
*4, clear_value
,
1999 &vb
.buffer_offset
, &vb
.buffer
);
2002 blitter_set_running_flag(ctx
);
2003 blitter_check_saved_vertex_states(ctx
);
2004 blitter_disable_render_cond(ctx
);
2006 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
2007 pipe
->bind_vertex_elements_state(pipe
,
2008 ctx
->velem_state_readbuf
[num_channels
-1]);
2009 bind_vs_pos_only(ctx
);
2010 if (ctx
->has_geometry_shader
)
2011 pipe
->bind_gs_state(pipe
, NULL
);
2012 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
2014 so_target
= pipe
->create_stream_output_target(pipe
, dst
, offset
, size
);
2015 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, offsets
);
2017 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
2019 blitter_restore_vertex_states(ctx
);
2020 blitter_restore_render_cond(ctx
);
2021 blitter_unset_running_flag(ctx
);
2022 pipe_so_target_reference(&so_target
, NULL
);
2023 pipe_resource_reference(&vb
.buffer
, NULL
);
2026 /* probably radeon specific */
2027 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
2028 struct pipe_resource
*dst
,
2031 struct pipe_resource
*src
,
2033 unsigned sample_mask
,
2035 enum pipe_format format
)
2037 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2038 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2039 struct pipe_framebuffer_state fb_state
;
2040 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
2042 blitter_set_running_flag(ctx
);
2043 blitter_check_saved_vertex_states(ctx
);
2044 blitter_check_saved_fragment_states(ctx
);
2045 blitter_disable_render_cond(ctx
);
2048 pipe
->bind_blend_state(pipe
, custom_blend
);
2049 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2050 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2051 bind_fs_write_one_cbuf(ctx
);
2052 pipe
->set_sample_mask(pipe
, sample_mask
);
2054 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
2055 surf_tmpl
.format
= format
;
2056 surf_tmpl
.u
.tex
.level
= dst_level
;
2057 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
2058 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
2060 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
2062 surf_tmpl
.u
.tex
.level
= 0;
2063 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
2064 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
2066 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
2068 /* set a framebuffer state */
2069 fb_state
.width
= src
->width0
;
2070 fb_state
.height
= src
->height0
;
2071 fb_state
.nr_cbufs
= 2;
2072 fb_state
.cbufs
[0] = srcsurf
;
2073 fb_state
.cbufs
[1] = dstsurf
;
2074 fb_state
.zsbuf
= NULL
;
2075 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2077 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2078 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
2079 blitter
->draw_rectangle(blitter
, 0, 0, src
->width0
, src
->height0
,
2081 blitter_restore_fb_state(ctx
);
2082 blitter_restore_vertex_states(ctx
);
2083 blitter_restore_fragment_states(ctx
);
2084 blitter_restore_render_cond(ctx
);
2085 blitter_unset_running_flag(ctx
);
2087 pipe_surface_reference(&srcsurf
, NULL
);
2088 pipe_surface_reference(&dstsurf
, NULL
);
2091 void util_blitter_custom_color(struct blitter_context
*blitter
,
2092 struct pipe_surface
*dstsurf
,
2095 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
2096 struct pipe_context
*pipe
= ctx
->base
.pipe
;
2097 struct pipe_framebuffer_state fb_state
;
2099 assert(dstsurf
->texture
);
2100 if (!dstsurf
->texture
)
2103 /* check the saved state */
2104 blitter_set_running_flag(ctx
);
2105 blitter_check_saved_vertex_states(ctx
);
2106 blitter_check_saved_fragment_states(ctx
);
2107 blitter_check_saved_fb_state(ctx
);
2108 blitter_disable_render_cond(ctx
);
2111 pipe
->bind_blend_state(pipe
, custom_blend
? custom_blend
2112 : ctx
->blend
[PIPE_MASK_RGBA
]);
2113 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2114 bind_fs_write_one_cbuf(ctx
);
2115 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2116 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2118 /* set a framebuffer state */
2119 fb_state
.width
= dstsurf
->width
;
2120 fb_state
.height
= dstsurf
->height
;
2121 fb_state
.nr_cbufs
= 1;
2122 fb_state
.cbufs
[0] = dstsurf
;
2124 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2125 pipe
->set_sample_mask(pipe
, ~0);
2127 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2128 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2129 blitter
->draw_rectangle(blitter
, 0, 0, dstsurf
->width
, dstsurf
->height
,
2132 blitter_restore_vertex_states(ctx
);
2133 blitter_restore_fragment_states(ctx
);
2134 blitter_restore_fb_state(ctx
);
2135 blitter_restore_render_cond(ctx
);
2136 blitter_unset_running_flag(ctx
);