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 TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 **************************************************************************/
29 * Blitter utility to facilitate acceleration of the clear, clear_render_target,
30 * clear_depth_stencil, resource_copy_region, and blit functions.
35 #include "pipe/p_context.h"
36 #include "pipe/p_defines.h"
37 #include "util/u_inlines.h"
38 #include "pipe/p_shader_tokens.h"
39 #include "pipe/p_state.h"
41 #include "util/u_format.h"
42 #include "util/u_memory.h"
43 #include "util/u_math.h"
44 #include "util/u_blitter.h"
45 #include "util/u_draw_quad.h"
46 #include "util/u_sampler.h"
47 #include "util/u_simple_shaders.h"
48 #include "util/u_surface.h"
49 #include "util/u_texture.h"
50 #include "util/u_upload_mgr.h"
52 #define INVALID_PTR ((void*)~0)
54 #define GET_CLEAR_BLEND_STATE_IDX(clear_buffers) \
55 ((clear_buffers) / PIPE_CLEAR_COLOR0)
57 #define NUM_RESOLVE_FRAG_SHADERS 5 /* MSAA 2x, 4x, 8x, 16x, 32x */
58 #define GET_MSAA_RESOLVE_FS_IDX(nr_samples) (util_logbase2(nr_samples)-1)
60 struct blitter_context_priv
62 struct blitter_context base
;
64 struct u_upload_mgr
*upload
;
66 float vertices
[4][2][4]; /**< {pos, color} or {pos, texcoord} */
68 /* Templates for various state objects. */
70 /* Constant state objects. */
72 void *vs
; /**< Vertex shader which passes {pos, generic} to the output.*/
73 void *vs_pos_only
; /**< Vertex shader which passes pos to the output.*/
74 void *vs_layered
; /**< Vertex shader which sets LAYER = INSTANCEID. */
76 /* Fragment shaders. */
78 void *fs_write_one_cbuf
;
79 void *fs_write_all_cbufs
;
81 /* FS which outputs a color from a texture,
82 where the index is PIPE_TEXTURE_* to be sampled. */
83 void *fs_texfetch_col
[PIPE_MAX_TEXTURE_TYPES
];
85 /* FS which outputs a depth from a texture,
86 where the index is PIPE_TEXTURE_* to be sampled. */
87 void *fs_texfetch_depth
[PIPE_MAX_TEXTURE_TYPES
];
88 void *fs_texfetch_depthstencil
[PIPE_MAX_TEXTURE_TYPES
];
89 void *fs_texfetch_stencil
[PIPE_MAX_TEXTURE_TYPES
];
91 /* FS which outputs one sample from a multisample texture. */
92 void *fs_texfetch_col_msaa
[PIPE_MAX_TEXTURE_TYPES
];
93 void *fs_texfetch_depth_msaa
[PIPE_MAX_TEXTURE_TYPES
];
94 void *fs_texfetch_depthstencil_msaa
[PIPE_MAX_TEXTURE_TYPES
];
95 void *fs_texfetch_stencil_msaa
[PIPE_MAX_TEXTURE_TYPES
];
97 /* FS which outputs an average of all samples. */
98 void *fs_resolve
[PIPE_MAX_TEXTURE_TYPES
][NUM_RESOLVE_FRAG_SHADERS
][2];
99 void *fs_resolve_sint
[PIPE_MAX_TEXTURE_TYPES
][NUM_RESOLVE_FRAG_SHADERS
][2];
100 void *fs_resolve_uint
[PIPE_MAX_TEXTURE_TYPES
][NUM_RESOLVE_FRAG_SHADERS
][2];
103 void *blend
[PIPE_MASK_RGBA
+1]; /**< blend state with writemask */
104 void *blend_clear
[GET_CLEAR_BLEND_STATE_IDX(PIPE_CLEAR_COLOR
)+1];
106 /* Depth stencil alpha state. */
107 void *dsa_write_depth_stencil
;
108 void *dsa_write_depth_keep_stencil
;
109 void *dsa_keep_depth_stencil
;
110 void *dsa_keep_depth_write_stencil
;
112 /* Vertex elements states. */
114 void *velem_state_readbuf
[4]; /**< X, XY, XYZ, XYZW */
118 void *sampler_state_linear
;
119 void *sampler_state_rect
;
120 void *sampler_state_rect_linear
;
122 /* Rasterizer state. */
123 void *rs_state
, *rs_state_scissor
, *rs_discard_state
;
125 /* Viewport state. */
126 struct pipe_viewport_state viewport
;
128 /* Destination surface dimensions. */
132 boolean has_geometry_shader
;
133 boolean has_stream_out
;
134 boolean has_stencil_export
;
135 boolean has_texture_multisample
;
137 /* The Draw module overrides these functions.
138 * Always create the blitter before Draw. */
139 void (*bind_fs_state
)(struct pipe_context
*, void *);
140 void (*delete_fs_state
)(struct pipe_context
*, void *);
143 static struct pipe_surface
*
144 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
145 struct pipe_surface
*surf
);
147 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
)
149 struct blitter_context_priv
*ctx
;
150 struct pipe_blend_state blend
;
151 struct pipe_depth_stencil_alpha_state dsa
;
152 struct pipe_rasterizer_state rs_state
;
153 struct pipe_sampler_state sampler_state
;
154 struct pipe_vertex_element velem
[2];
157 ctx
= CALLOC_STRUCT(blitter_context_priv
);
161 ctx
->base
.pipe
= pipe
;
162 ctx
->base
.draw_rectangle
= util_blitter_draw_rectangle
;
163 ctx
->base
.get_next_surface_layer
= util_blitter_get_next_surface_layer
;
165 ctx
->bind_fs_state
= pipe
->bind_fs_state
;
166 ctx
->delete_fs_state
= pipe
->delete_fs_state
;
168 /* init state objects for them to be considered invalid */
169 ctx
->base
.saved_blend_state
= INVALID_PTR
;
170 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
171 ctx
->base
.saved_rs_state
= INVALID_PTR
;
172 ctx
->base
.saved_fs
= INVALID_PTR
;
173 ctx
->base
.saved_vs
= INVALID_PTR
;
174 ctx
->base
.saved_gs
= INVALID_PTR
;
175 ctx
->base
.saved_velem_state
= INVALID_PTR
;
176 ctx
->base
.saved_fb_state
.nr_cbufs
= ~0;
177 ctx
->base
.saved_num_sampler_views
= ~0;
178 ctx
->base
.saved_num_sampler_states
= ~0;
179 ctx
->base
.saved_num_so_targets
= ~0;
181 ctx
->has_geometry_shader
=
182 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
183 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
184 ctx
->has_stream_out
=
185 pipe
->screen
->get_param(pipe
->screen
,
186 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0;
188 ctx
->has_stencil_export
=
189 pipe
->screen
->get_param(pipe
->screen
,
190 PIPE_CAP_SHADER_STENCIL_EXPORT
);
192 ctx
->has_texture_multisample
=
193 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TEXTURE_MULTISAMPLE
);
195 /* blend state objects */
196 memset(&blend
, 0, sizeof(blend
));
198 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
199 blend
.rt
[0].colormask
= i
;
200 ctx
->blend
[i
] = pipe
->create_blend_state(pipe
, &blend
);
203 /* depth stencil alpha state objects */
204 memset(&dsa
, 0, sizeof(dsa
));
205 ctx
->dsa_keep_depth_stencil
=
206 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
208 dsa
.depth
.enabled
= 1;
209 dsa
.depth
.writemask
= 1;
210 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
211 ctx
->dsa_write_depth_keep_stencil
=
212 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
214 dsa
.stencil
[0].enabled
= 1;
215 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
216 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
217 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
218 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
219 dsa
.stencil
[0].valuemask
= 0xff;
220 dsa
.stencil
[0].writemask
= 0xff;
221 ctx
->dsa_write_depth_stencil
=
222 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
224 dsa
.depth
.enabled
= 0;
225 dsa
.depth
.writemask
= 0;
226 ctx
->dsa_keep_depth_write_stencil
=
227 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
230 memset(&sampler_state
, 0, sizeof(sampler_state
));
231 sampler_state
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
232 sampler_state
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
233 sampler_state
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
234 sampler_state
.normalized_coords
= 1;
235 ctx
->sampler_state
= pipe
->create_sampler_state(pipe
, &sampler_state
);
236 sampler_state
.normalized_coords
= 0;
237 ctx
->sampler_state_rect
= pipe
->create_sampler_state(pipe
, &sampler_state
);
239 sampler_state
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
240 sampler_state
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
241 sampler_state
.normalized_coords
= 1;
242 ctx
->sampler_state_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
243 sampler_state
.normalized_coords
= 0;
244 ctx
->sampler_state_rect_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
246 /* rasterizer state */
247 memset(&rs_state
, 0, sizeof(rs_state
));
248 rs_state
.cull_face
= PIPE_FACE_NONE
;
249 rs_state
.half_pixel_center
= 1;
250 rs_state
.bottom_edge_rule
= 1;
251 rs_state
.flatshade
= 1;
252 rs_state
.depth_clip
= 1;
253 ctx
->rs_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
255 rs_state
.scissor
= 1;
256 ctx
->rs_state_scissor
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
258 if (ctx
->has_stream_out
) {
259 rs_state
.scissor
= 0;
260 rs_state
.rasterizer_discard
= 1;
261 ctx
->rs_discard_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
264 ctx
->base
.vb_slot
= 0; /* 0 for now */
266 /* vertex elements states */
267 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
268 for (i
= 0; i
< 2; i
++) {
269 velem
[i
].src_offset
= i
* 4 * sizeof(float);
270 velem
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
271 velem
[i
].vertex_buffer_index
= ctx
->base
.vb_slot
;
273 ctx
->velem_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
275 if (ctx
->has_stream_out
) {
276 static enum pipe_format formats
[4] = {
277 PIPE_FORMAT_R32_UINT
,
278 PIPE_FORMAT_R32G32_UINT
,
279 PIPE_FORMAT_R32G32B32_UINT
,
280 PIPE_FORMAT_R32G32B32A32_UINT
283 for (i
= 0; i
< 4; i
++) {
284 velem
[0].src_format
= formats
[i
];
285 velem
[0].vertex_buffer_index
= ctx
->base
.vb_slot
;
286 ctx
->velem_state_readbuf
[i
] =
287 pipe
->create_vertex_elements_state(pipe
, 1, &velem
[0]);
291 /* Fragment shaders are created on-demand, except these.
292 * The interpolation must be constant for integer texture clearing to work.
294 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
295 ctx
->fs_write_one_cbuf
=
296 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
297 TGSI_INTERPOLATE_CONSTANT
, FALSE
);
298 ctx
->fs_write_all_cbufs
=
299 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
300 TGSI_INTERPOLATE_CONSTANT
, TRUE
);
304 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
305 TGSI_SEMANTIC_GENERIC
};
306 const uint semantic_indices
[] = { 0, 0 };
308 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
312 if (ctx
->has_stream_out
) {
313 struct pipe_stream_output_info so
;
314 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
};
315 const uint semantic_indices
[] = { 0 };
317 memset(&so
, 0, sizeof(so
));
319 so
.output
[0].num_components
= 1;
323 util_make_vertex_passthrough_shader_with_so(pipe
, 1, semantic_names
,
324 semantic_indices
, &so
);
327 if (pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_INSTANCEID
) &&
328 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TGSI_VS_LAYER
)) {
329 ctx
->vs_layered
= util_make_layered_clear_vertex_shader(pipe
);
332 /* set invariant vertex coordinates */
333 for (i
= 0; i
< 4; i
++)
334 ctx
->vertices
[i
][0][3] = 1; /*v.w*/
336 ctx
->upload
= u_upload_create(pipe
, 65536, 4, PIPE_BIND_VERTEX_BUFFER
);
341 void util_blitter_destroy(struct blitter_context
*blitter
)
343 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
344 struct pipe_context
*pipe
= blitter
->pipe
;
347 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
348 pipe
->delete_blend_state(pipe
, ctx
->blend
[i
]);
350 for (i
= 0; i
< Elements(ctx
->blend_clear
); i
++) {
351 if (ctx
->blend_clear
[i
])
352 pipe
->delete_blend_state(pipe
, ctx
->blend_clear
[i
]);
354 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
355 pipe
->delete_depth_stencil_alpha_state(pipe
,
356 ctx
->dsa_write_depth_keep_stencil
);
357 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
358 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
360 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
);
361 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state_scissor
);
362 if (ctx
->rs_discard_state
)
363 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_discard_state
);
364 pipe
->delete_vs_state(pipe
, ctx
->vs
);
365 if (ctx
->vs_pos_only
)
366 pipe
->delete_vs_state(pipe
, ctx
->vs_pos_only
);
367 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state
);
368 for (i
= 0; i
< 4; i
++) {
369 if (ctx
->velem_state_readbuf
[i
]) {
370 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[i
]);
374 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
375 if (ctx
->fs_texfetch_col
[i
])
376 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[i
]);
377 if (ctx
->fs_texfetch_depth
[i
])
378 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
]);
379 if (ctx
->fs_texfetch_depthstencil
[i
])
380 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil
[i
]);
381 if (ctx
->fs_texfetch_stencil
[i
])
382 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil
[i
]);
384 for (j
= 0; j
< Elements(ctx
->fs_resolve
[i
]); j
++)
385 for (f
= 0; f
< 2; f
++)
386 if (ctx
->fs_resolve
[i
][j
][f
])
387 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve
[i
][j
][f
]);
389 for (j
= 0; j
< Elements(ctx
->fs_resolve_sint
[i
]); j
++)
390 for (f
= 0; f
< 2; f
++)
391 if (ctx
->fs_resolve_sint
[i
][j
][f
])
392 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve_sint
[i
][j
][f
]);
394 for (j
= 0; j
< Elements(ctx
->fs_resolve_uint
[i
]); j
++)
395 for (f
= 0; f
< 2; f
++)
396 if (ctx
->fs_resolve_uint
[i
][j
][f
])
397 ctx
->delete_fs_state(pipe
, ctx
->fs_resolve_uint
[i
][j
][f
]);
400 ctx
->delete_fs_state(pipe
, ctx
->fs_empty
);
401 ctx
->delete_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
402 ctx
->delete_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
404 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect_linear
);
405 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect
);
406 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_linear
);
407 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
);
408 u_upload_destroy(ctx
->upload
);
412 void util_blitter_set_texture_multisample(struct blitter_context
*blitter
,
415 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
417 ctx
->has_texture_multisample
= supported
;
420 static void blitter_set_running_flag(struct blitter_context_priv
*ctx
)
422 if (ctx
->base
.running
) {
423 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
426 ctx
->base
.running
= TRUE
;
429 static void blitter_unset_running_flag(struct blitter_context_priv
*ctx
)
431 if (!ctx
->base
.running
) {
432 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
435 ctx
->base
.running
= FALSE
;
438 static void blitter_check_saved_vertex_states(struct blitter_context_priv
*ctx
)
440 assert(ctx
->base
.saved_velem_state
!= INVALID_PTR
);
441 assert(ctx
->base
.saved_vs
!= INVALID_PTR
);
442 assert(!ctx
->has_geometry_shader
|| ctx
->base
.saved_gs
!= INVALID_PTR
);
443 assert(!ctx
->has_stream_out
|| ctx
->base
.saved_num_so_targets
!= ~0);
444 assert(ctx
->base
.saved_rs_state
!= INVALID_PTR
);
447 static void blitter_restore_vertex_states(struct blitter_context_priv
*ctx
)
449 struct pipe_context
*pipe
= ctx
->base
.pipe
;
453 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1,
454 &ctx
->base
.saved_vertex_buffer
);
455 pipe_resource_reference(&ctx
->base
.saved_vertex_buffer
.buffer
, NULL
);
457 /* Vertex elements. */
458 pipe
->bind_vertex_elements_state(pipe
, ctx
->base
.saved_velem_state
);
459 ctx
->base
.saved_velem_state
= INVALID_PTR
;
462 pipe
->bind_vs_state(pipe
, ctx
->base
.saved_vs
);
463 ctx
->base
.saved_vs
= INVALID_PTR
;
465 /* Geometry shader. */
466 if (ctx
->has_geometry_shader
) {
467 pipe
->bind_gs_state(pipe
, ctx
->base
.saved_gs
);
468 ctx
->base
.saved_gs
= INVALID_PTR
;
471 /* Stream outputs. */
472 if (ctx
->has_stream_out
) {
473 pipe
->set_stream_output_targets(pipe
,
474 ctx
->base
.saved_num_so_targets
,
475 ctx
->base
.saved_so_targets
, ~0);
477 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
478 pipe_so_target_reference(&ctx
->base
.saved_so_targets
[i
], NULL
);
480 ctx
->base
.saved_num_so_targets
= ~0;
484 pipe
->bind_rasterizer_state(pipe
, ctx
->base
.saved_rs_state
);
485 ctx
->base
.saved_rs_state
= INVALID_PTR
;
488 static void blitter_check_saved_fragment_states(struct blitter_context_priv
*ctx
)
490 assert(ctx
->base
.saved_fs
!= INVALID_PTR
);
491 assert(ctx
->base
.saved_dsa_state
!= INVALID_PTR
);
492 assert(ctx
->base
.saved_blend_state
!= INVALID_PTR
);
495 static void blitter_restore_fragment_states(struct blitter_context_priv
*ctx
)
497 struct pipe_context
*pipe
= ctx
->base
.pipe
;
499 /* Fragment shader. */
500 ctx
->bind_fs_state(pipe
, ctx
->base
.saved_fs
);
501 ctx
->base
.saved_fs
= INVALID_PTR
;
503 /* Depth, stencil, alpha. */
504 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->base
.saved_dsa_state
);
505 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
508 pipe
->bind_blend_state(pipe
, ctx
->base
.saved_blend_state
);
509 ctx
->base
.saved_blend_state
= INVALID_PTR
;
512 if (ctx
->base
.is_sample_mask_saved
) {
513 pipe
->set_sample_mask(pipe
, ctx
->base
.saved_sample_mask
);
514 ctx
->base
.is_sample_mask_saved
= FALSE
;
517 /* Miscellaneous states. */
518 /* XXX check whether these are saved and whether they need to be restored
519 * (depending on the operation) */
520 pipe
->set_stencil_ref(pipe
, &ctx
->base
.saved_stencil_ref
);
521 pipe
->set_viewport_states(pipe
, 0, 1, &ctx
->base
.saved_viewport
);
524 static void blitter_check_saved_fb_state(struct blitter_context_priv
*ctx
)
526 assert(ctx
->base
.saved_fb_state
.nr_cbufs
!= ~0);
529 static void blitter_disable_render_cond(struct blitter_context_priv
*ctx
)
531 struct pipe_context
*pipe
= ctx
->base
.pipe
;
533 if (ctx
->base
.saved_render_cond_query
) {
534 pipe
->render_condition(pipe
, NULL
, FALSE
, 0);
538 static void blitter_restore_render_cond(struct blitter_context_priv
*ctx
)
540 struct pipe_context
*pipe
= ctx
->base
.pipe
;
542 if (ctx
->base
.saved_render_cond_query
) {
543 pipe
->render_condition(pipe
, ctx
->base
.saved_render_cond_query
,
544 ctx
->base
.saved_render_cond_cond
,
545 ctx
->base
.saved_render_cond_mode
);
546 ctx
->base
.saved_render_cond_query
= NULL
;
550 static void blitter_restore_fb_state(struct blitter_context_priv
*ctx
)
552 struct pipe_context
*pipe
= ctx
->base
.pipe
;
554 pipe
->set_framebuffer_state(pipe
, &ctx
->base
.saved_fb_state
);
555 util_unreference_framebuffer_state(&ctx
->base
.saved_fb_state
);
558 static void blitter_check_saved_textures(struct blitter_context_priv
*ctx
)
560 assert(ctx
->base
.saved_num_sampler_states
!= ~0);
561 assert(ctx
->base
.saved_num_sampler_views
!= ~0);
564 static void blitter_restore_textures(struct blitter_context_priv
*ctx
)
566 struct pipe_context
*pipe
= ctx
->base
.pipe
;
569 /* Fragment sampler states. */
570 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0,
571 ctx
->base
.saved_num_sampler_states
,
572 ctx
->base
.saved_sampler_states
);
574 ctx
->base
.saved_num_sampler_states
= ~0;
576 /* Fragment sampler views. */
577 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
578 ctx
->base
.saved_num_sampler_views
,
579 ctx
->base
.saved_sampler_views
);
581 for (i
= 0; i
< ctx
->base
.saved_num_sampler_views
; i
++)
582 pipe_sampler_view_reference(&ctx
->base
.saved_sampler_views
[i
], NULL
);
584 ctx
->base
.saved_num_sampler_views
= ~0;
587 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
588 int x1
, int y1
, int x2
, int y2
,
593 /* set vertex positions */
594 ctx
->vertices
[0][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v0.x*/
595 ctx
->vertices
[0][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v0.y*/
597 ctx
->vertices
[1][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v1.x*/
598 ctx
->vertices
[1][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v1.y*/
600 ctx
->vertices
[2][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v2.x*/
601 ctx
->vertices
[2][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v2.y*/
603 ctx
->vertices
[3][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v3.x*/
604 ctx
->vertices
[3][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v3.y*/
606 for (i
= 0; i
< 4; i
++)
607 ctx
->vertices
[i
][0][2] = depth
; /*z*/
610 ctx
->viewport
.scale
[0] = 0.5f
* ctx
->dst_width
;
611 ctx
->viewport
.scale
[1] = 0.5f
* ctx
->dst_height
;
612 ctx
->viewport
.scale
[2] = 1.0f
;
613 ctx
->viewport
.scale
[3] = 1.0f
;
614 ctx
->viewport
.translate
[0] = 0.5f
* ctx
->dst_width
;
615 ctx
->viewport
.translate
[1] = 0.5f
* ctx
->dst_height
;
616 ctx
->viewport
.translate
[2] = 0.0f
;
617 ctx
->viewport
.translate
[3] = 0.0f
;
618 ctx
->base
.pipe
->set_viewport_states(ctx
->base
.pipe
, 0, 1, &ctx
->viewport
);
621 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
622 const union pipe_color_union
*color
)
627 for (i
= 0; i
< 4; i
++) {
628 uint32_t *uiverts
= (uint32_t *)ctx
->vertices
[i
][1];
629 uiverts
[0] = color
->ui
[0];
630 uiverts
[1] = color
->ui
[1];
631 uiverts
[2] = color
->ui
[2];
632 uiverts
[3] = color
->ui
[3];
635 for (i
= 0; i
< 4; i
++) {
636 ctx
->vertices
[i
][1][0] = 0;
637 ctx
->vertices
[i
][1][1] = 0;
638 ctx
->vertices
[i
][1][2] = 0;
639 ctx
->vertices
[i
][1][3] = 0;
644 static void get_texcoords(struct pipe_sampler_view
*src
,
645 unsigned src_width0
, unsigned src_height0
,
646 int x1
, int y1
, int x2
, int y2
,
649 struct pipe_resource
*tex
= src
->texture
;
650 unsigned level
= src
->u
.tex
.first_level
;
651 boolean normalized
= tex
->target
!= PIPE_TEXTURE_RECT
&&
652 tex
->nr_samples
<= 1;
655 out
[0] = x1
/ (float)u_minify(src_width0
, level
);
656 out
[1] = y1
/ (float)u_minify(src_height0
, level
);
657 out
[2] = x2
/ (float)u_minify(src_width0
, level
);
658 out
[3] = y2
/ (float)u_minify(src_height0
, level
);
667 static void set_texcoords_in_vertices(const float coord
[4],
668 float *out
, unsigned stride
)
670 out
[0] = coord
[0]; /*t0.s*/
671 out
[1] = coord
[1]; /*t0.t*/
673 out
[0] = coord
[2]; /*t1.s*/
674 out
[1] = coord
[1]; /*t1.t*/
676 out
[0] = coord
[2]; /*t2.s*/
677 out
[1] = coord
[3]; /*t2.t*/
679 out
[0] = coord
[0]; /*t3.s*/
680 out
[1] = coord
[3]; /*t3.t*/
683 static void blitter_set_texcoords(struct blitter_context_priv
*ctx
,
684 struct pipe_sampler_view
*src
,
685 unsigned src_width0
, unsigned src_height0
,
686 unsigned layer
, unsigned sample
,
687 int x1
, int y1
, int x2
, int y2
)
691 float face_coord
[4][2];
693 get_texcoords(src
, src_width0
, src_height0
, x1
, y1
, x2
, y2
, coord
);
695 if (src
->texture
->target
== PIPE_TEXTURE_CUBE
||
696 src
->texture
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
697 set_texcoords_in_vertices(coord
, &face_coord
[0][0], 2);
698 util_map_texcoords2d_onto_cubemap(layer
% 6,
699 /* pointer, stride in floats */
700 &face_coord
[0][0], 2,
701 &ctx
->vertices
[0][1][0], 8,
704 set_texcoords_in_vertices(coord
, &ctx
->vertices
[0][1][0], 8);
708 switch (src
->texture
->target
) {
709 case PIPE_TEXTURE_3D
:
711 float r
= layer
/ (float)u_minify(src
->texture
->depth0
,
712 src
->u
.tex
.first_level
);
713 for (i
= 0; i
< 4; i
++)
714 ctx
->vertices
[i
][1][2] = r
; /*r*/
718 case PIPE_TEXTURE_1D_ARRAY
:
719 for (i
= 0; i
< 4; i
++)
720 ctx
->vertices
[i
][1][1] = (float) layer
; /*t*/
723 case PIPE_TEXTURE_2D_ARRAY
:
724 for (i
= 0; i
< 4; i
++) {
725 ctx
->vertices
[i
][1][2] = (float) layer
; /*r*/
726 ctx
->vertices
[i
][1][3] = (float) sample
; /*q*/
730 case PIPE_TEXTURE_CUBE_ARRAY
:
731 for (i
= 0; i
< 4; i
++)
732 ctx
->vertices
[i
][1][3] = (float) (layer
/ 6); /*w*/
735 case PIPE_TEXTURE_2D
:
736 for (i
= 0; i
< 4; i
++) {
737 ctx
->vertices
[i
][1][3] = (float) sample
; /*r*/
745 static void blitter_set_dst_dimensions(struct blitter_context_priv
*ctx
,
746 unsigned width
, unsigned height
)
748 ctx
->dst_width
= width
;
749 ctx
->dst_height
= height
;
752 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
753 enum pipe_format format
,
754 enum pipe_texture_target target
,
755 unsigned src_nr_samples
,
756 unsigned dst_nr_samples
,
759 struct pipe_context
*pipe
= ctx
->base
.pipe
;
760 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, src_nr_samples
);
762 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
764 if (src_nr_samples
> 1) {
767 if (dst_nr_samples
<= 1) {
768 /* The destination has one sample, so we'll do color resolve. */
769 boolean is_uint
, is_sint
;
770 unsigned index
= GET_MSAA_RESOLVE_FS_IDX(src_nr_samples
);
772 is_uint
= util_format_is_pure_uint(format
);
773 is_sint
= util_format_is_pure_sint(format
);
778 shader
= &ctx
->fs_resolve_uint
[target
][index
][filter
];
780 shader
= &ctx
->fs_resolve_sint
[target
][index
][filter
];
782 shader
= &ctx
->fs_resolve
[target
][index
][filter
];
785 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
786 *shader
= util_make_fs_msaa_resolve_bilinear(pipe
, tgsi_tex
,
791 *shader
= util_make_fs_msaa_resolve(pipe
, tgsi_tex
,
798 /* The destination has multiple samples, we'll do
799 * an MSAA->MSAA copy.
801 shader
= &ctx
->fs_texfetch_col_msaa
[target
];
803 /* Create the fragment shader on-demand. */
805 *shader
= util_make_fs_blit_msaa_color(pipe
, tgsi_tex
);
811 void **shader
= &ctx
->fs_texfetch_col
[target
];
813 /* Create the fragment shader on-demand. */
815 *shader
= util_make_fragment_tex_shader(pipe
, tgsi_tex
,
816 TGSI_INTERPOLATE_LINEAR
);
824 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
825 enum pipe_texture_target target
,
828 struct pipe_context
*pipe
= ctx
->base
.pipe
;
830 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
832 if (nr_samples
> 1) {
833 void **shader
= &ctx
->fs_texfetch_depth_msaa
[target
];
835 /* Create the fragment shader on-demand. */
837 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
,
841 util_make_fs_blit_msaa_depth(pipe
, tgsi_tex
);
846 void **shader
= &ctx
->fs_texfetch_depth
[target
];
848 /* Create the fragment shader on-demand. */
850 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
853 util_make_fragment_tex_shader_writedepth(pipe
, tgsi_tex
,
854 TGSI_INTERPOLATE_LINEAR
);
862 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv
*ctx
,
863 enum pipe_texture_target target
,
866 struct pipe_context
*pipe
= ctx
->base
.pipe
;
868 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
870 if (nr_samples
> 1) {
871 void **shader
= &ctx
->fs_texfetch_depthstencil_msaa
[target
];
873 /* Create the fragment shader on-demand. */
875 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
,
879 util_make_fs_blit_msaa_depthstencil(pipe
, tgsi_tex
);
884 void **shader
= &ctx
->fs_texfetch_depthstencil
[target
];
886 /* Create the fragment shader on-demand. */
888 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
891 util_make_fragment_tex_shader_writedepthstencil(pipe
, tgsi_tex
,
892 TGSI_INTERPOLATE_LINEAR
);
900 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv
*ctx
,
901 enum pipe_texture_target target
,
904 struct pipe_context
*pipe
= ctx
->base
.pipe
;
906 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
908 if (nr_samples
> 1) {
909 void **shader
= &ctx
->fs_texfetch_stencil_msaa
[target
];
911 /* Create the fragment shader on-demand. */
913 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
,
917 util_make_fs_blit_msaa_stencil(pipe
, tgsi_tex
);
922 void **shader
= &ctx
->fs_texfetch_stencil
[target
];
924 /* Create the fragment shader on-demand. */
926 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
929 util_make_fragment_tex_shader_writestencil(pipe
, tgsi_tex
,
930 TGSI_INTERPOLATE_LINEAR
);
937 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
)
939 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
940 struct pipe_screen
*screen
= blitter
->pipe
->screen
;
941 unsigned samples
, j
, f
, target
, max_samples
;
942 boolean has_arraytex
, has_cubearraytex
;
944 max_samples
= ctx
->has_texture_multisample
? 2 : 1;
945 has_arraytex
= screen
->get_param(screen
,
946 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
) != 0;
947 has_cubearraytex
= screen
->get_param(screen
,
948 PIPE_CAP_CUBE_MAP_ARRAY
) != 0;
950 /* It only matters if i <= 1 or > 1. */
951 for (samples
= 1; samples
<= max_samples
; samples
++) {
952 for (target
= PIPE_TEXTURE_1D
; target
< PIPE_MAX_TEXTURE_TYPES
; target
++) {
954 (target
== PIPE_TEXTURE_1D_ARRAY
||
955 target
== PIPE_TEXTURE_2D_ARRAY
)) {
958 if (!has_cubearraytex
&&
959 (target
== PIPE_TEXTURE_CUBE_ARRAY
))
963 (target
!= PIPE_TEXTURE_2D
&&
964 target
!= PIPE_TEXTURE_2D_ARRAY
))
967 /* If samples == 1, the shaders read one texel. If samples >= 1,
968 * they read one sample.
970 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
, target
,
971 samples
, samples
, 0);
972 blitter_get_fs_texfetch_depth(ctx
, target
, samples
);
973 if (ctx
->has_stencil_export
) {
974 blitter_get_fs_texfetch_depthstencil(ctx
, target
, samples
);
975 blitter_get_fs_texfetch_stencil(ctx
, target
, samples
);
981 /* MSAA resolve shaders. */
982 for (j
= 2; j
< 32; j
++) {
983 if (!screen
->is_format_supported(screen
, PIPE_FORMAT_R32_FLOAT
,
985 PIPE_BIND_SAMPLER_VIEW
)) {
989 for (f
= 0; f
< 2; f
++) {
990 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_FLOAT
, target
,
992 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_UINT
, target
,
994 blitter_get_fs_texfetch_col(ctx
, PIPE_FORMAT_R32_SINT
, target
,
1002 static void blitter_set_common_draw_rect_state(struct blitter_context_priv
*ctx
,
1006 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1008 pipe
->bind_rasterizer_state(pipe
, scissor
? ctx
->rs_state_scissor
1010 pipe
->bind_vs_state(pipe
, vs_layered
? ctx
->vs_layered
: ctx
->vs
);
1011 if (ctx
->has_geometry_shader
)
1012 pipe
->bind_gs_state(pipe
, NULL
);
1013 if (ctx
->has_stream_out
)
1014 pipe
->set_stream_output_targets(pipe
, 0, NULL
, 0);
1017 static void blitter_draw(struct blitter_context_priv
*ctx
,
1018 int x1
, int y1
, int x2
, int y2
, float depth
,
1019 unsigned num_instances
)
1021 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1022 struct pipe_vertex_buffer vb
= {0};
1024 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
1026 vb
.stride
= 8 * sizeof(float);
1028 u_upload_data(ctx
->upload
, 0, sizeof(ctx
->vertices
), ctx
->vertices
,
1029 &vb
.buffer_offset
, &vb
.buffer
);
1030 u_upload_unmap(ctx
->upload
);
1032 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1033 util_draw_arrays_instanced(pipe
, PIPE_PRIM_TRIANGLE_FAN
, 0, 4,
1035 pipe_resource_reference(&vb
.buffer
, NULL
);
1038 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
1039 int x1
, int y1
, int x2
, int y2
, float depth
,
1040 enum blitter_attrib_type type
,
1041 const union pipe_color_union
*attrib
)
1043 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1046 case UTIL_BLITTER_ATTRIB_COLOR
:
1047 blitter_set_clear_color(ctx
, attrib
);
1050 case UTIL_BLITTER_ATTRIB_TEXCOORD
:
1051 set_texcoords_in_vertices(attrib
->f
, &ctx
->vertices
[0][1][0], 8);
1057 blitter_draw(ctx
, x1
, y1
, x2
, y2
, depth
, 1);
1060 static void *get_clear_blend_state(struct blitter_context_priv
*ctx
,
1061 unsigned clear_buffers
)
1063 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1066 clear_buffers
&= PIPE_CLEAR_COLOR
;
1068 /* Return an existing blend state. */
1070 return ctx
->blend
[0];
1072 index
= GET_CLEAR_BLEND_STATE_IDX(clear_buffers
);
1074 if (ctx
->blend_clear
[index
])
1075 return ctx
->blend_clear
[index
];
1077 /* Create a new one. */
1079 struct pipe_blend_state blend
= {0};
1082 blend
.independent_blend_enable
= 1;
1084 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1085 if (clear_buffers
& (PIPE_CLEAR_COLOR0
<< i
)) {
1086 blend
.rt
[i
].colormask
= PIPE_MASK_RGBA
;
1090 ctx
->blend_clear
[index
] = pipe
->create_blend_state(pipe
, &blend
);
1092 return ctx
->blend_clear
[index
];
1095 static void util_blitter_clear_custom(struct blitter_context
*blitter
,
1096 unsigned width
, unsigned height
,
1097 unsigned num_layers
,
1098 unsigned clear_buffers
,
1099 const union pipe_color_union
*color
,
1100 double depth
, unsigned stencil
,
1101 void *custom_blend
, void *custom_dsa
)
1103 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1104 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1105 struct pipe_stencil_ref sr
= { { 0 } };
1107 assert(ctx
->vs_layered
|| num_layers
<= 1);
1109 blitter_set_running_flag(ctx
);
1110 blitter_check_saved_vertex_states(ctx
);
1111 blitter_check_saved_fragment_states(ctx
);
1112 blitter_disable_render_cond(ctx
);
1116 pipe
->bind_blend_state(pipe
, custom_blend
);
1118 pipe
->bind_blend_state(pipe
, get_clear_blend_state(ctx
, clear_buffers
));
1122 pipe
->bind_depth_stencil_alpha_state(pipe
, custom_dsa
);
1123 } else if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1124 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1125 } else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
1126 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1127 } else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
1128 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1130 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1133 sr
.ref_value
[0] = stencil
& 0xff;
1134 pipe
->set_stencil_ref(pipe
, &sr
);
1136 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1137 ctx
->bind_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
1138 pipe
->set_sample_mask(pipe
, ~0);
1140 blitter_set_dst_dimensions(ctx
, width
, height
);
1142 if (num_layers
> 1 && ctx
->vs_layered
) {
1143 blitter_set_common_draw_rect_state(ctx
, FALSE
, TRUE
);
1144 blitter_set_clear_color(ctx
, color
);
1145 blitter_draw(ctx
, 0, 0, width
, height
, depth
, num_layers
);
1148 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1149 blitter
->draw_rectangle(blitter
, 0, 0, width
, height
, (float) depth
,
1150 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1153 blitter_restore_vertex_states(ctx
);
1154 blitter_restore_fragment_states(ctx
);
1155 blitter_restore_render_cond(ctx
);
1156 blitter_unset_running_flag(ctx
);
1159 void util_blitter_clear(struct blitter_context
*blitter
,
1160 unsigned width
, unsigned height
, unsigned num_layers
,
1161 unsigned clear_buffers
,
1162 const union pipe_color_union
*color
,
1163 double depth
, unsigned stencil
)
1165 util_blitter_clear_custom(blitter
, width
, height
, num_layers
,
1166 clear_buffers
, color
, depth
, stencil
,
1170 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1171 unsigned width
, unsigned height
,
1172 double depth
, void *custom_dsa
)
1174 static const union pipe_color_union color
;
1175 util_blitter_clear_custom(blitter
, width
, height
, 0, 0, &color
, depth
, 0,
1179 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1180 struct pipe_resource
*dst
,
1184 memset(dst_templ
, 0, sizeof(*dst_templ
));
1185 dst_templ
->format
= util_format_linear(dst
->format
);
1186 dst_templ
->u
.tex
.level
= dstlevel
;
1187 dst_templ
->u
.tex
.first_layer
= dstz
;
1188 dst_templ
->u
.tex
.last_layer
= dstz
;
1191 static struct pipe_surface
*
1192 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
1193 struct pipe_surface
*surf
)
1195 struct pipe_surface dst_templ
;
1197 memset(&dst_templ
, 0, sizeof(dst_templ
));
1198 dst_templ
.format
= surf
->format
;
1199 dst_templ
.u
.tex
.level
= surf
->u
.tex
.level
;
1200 dst_templ
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
+ 1;
1201 dst_templ
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
+ 1;
1203 return pipe
->create_surface(pipe
, surf
->texture
, &dst_templ
);
1206 void util_blitter_default_src_texture(struct pipe_sampler_view
*src_templ
,
1207 struct pipe_resource
*src
,
1210 memset(src_templ
, 0, sizeof(*src_templ
));
1211 src_templ
->format
= util_format_linear(src
->format
);
1212 src_templ
->u
.tex
.first_level
= srclevel
;
1213 src_templ
->u
.tex
.last_level
= srclevel
;
1214 src_templ
->u
.tex
.first_layer
= 0;
1215 src_templ
->u
.tex
.last_layer
=
1216 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1217 : src
->array_size
- 1;
1218 src_templ
->swizzle_r
= PIPE_SWIZZLE_RED
;
1219 src_templ
->swizzle_g
= PIPE_SWIZZLE_GREEN
;
1220 src_templ
->swizzle_b
= PIPE_SWIZZLE_BLUE
;
1221 src_templ
->swizzle_a
= PIPE_SWIZZLE_ALPHA
;
1224 static boolean
is_blit_generic_supported(struct blitter_context
*blitter
,
1225 const struct pipe_resource
*dst
,
1226 enum pipe_format dst_format
,
1227 const struct pipe_resource
*src
,
1228 enum pipe_format src_format
,
1231 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1232 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1236 const struct util_format_description
*desc
=
1237 util_format_description(dst_format
);
1238 boolean dst_has_stencil
= util_format_has_stencil(desc
);
1240 /* Stencil export must be supported for stencil copy. */
1241 if ((mask
& PIPE_MASK_S
) && dst_has_stencil
&&
1242 !ctx
->has_stencil_export
) {
1246 if (dst_has_stencil
|| util_format_has_depth(desc
))
1247 bind
= PIPE_BIND_DEPTH_STENCIL
;
1249 bind
= PIPE_BIND_RENDER_TARGET
;
1251 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1252 dst
->nr_samples
, bind
)) {
1258 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1262 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1263 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
)) {
1267 /* Check stencil sampler support for stencil copy. */
1268 if (mask
& PIPE_MASK_S
) {
1269 if (util_format_has_stencil(util_format_description(src_format
))) {
1270 enum pipe_format stencil_format
=
1271 util_format_stencil_only(src_format
);
1272 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1274 if (stencil_format
!= src_format
&&
1275 !screen
->is_format_supported(screen
, stencil_format
,
1276 src
->target
, src
->nr_samples
,
1277 PIPE_BIND_SAMPLER_VIEW
)) {
1287 boolean
util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1288 const struct pipe_resource
*dst
,
1289 const struct pipe_resource
*src
)
1291 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1292 src
, src
->format
, PIPE_MASK_RGBAZS
);
1295 boolean
util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1296 const struct pipe_blit_info
*info
)
1298 return is_blit_generic_supported(blitter
,
1299 info
->dst
.resource
, info
->dst
.format
,
1300 info
->src
.resource
, info
->src
.format
,
1304 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1305 struct pipe_resource
*dst
,
1307 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1308 struct pipe_resource
*src
,
1310 const struct pipe_box
*srcbox
)
1312 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1313 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1314 struct pipe_surface
*dst_view
, dst_templ
;
1315 struct pipe_sampler_view src_templ
, *src_view
;
1316 struct pipe_box dstbox
;
1319 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1321 u_box_3d(dstx
, dsty
, dstz
, abs(srcbox
->width
), abs(srcbox
->height
),
1322 abs(srcbox
->depth
), &dstbox
);
1324 /* Initialize the surface. */
1325 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1326 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1328 /* Initialize the sampler view. */
1329 util_blitter_default_src_texture(&src_templ
, src
, src_level
);
1330 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1333 util_blitter_blit_generic(blitter
, dst_view
, &dstbox
,
1334 src_view
, srcbox
, src
->width0
, src
->height0
,
1335 PIPE_MASK_RGBAZS
, PIPE_TEX_FILTER_NEAREST
, NULL
);
1337 pipe_surface_reference(&dst_view
, NULL
);
1338 pipe_sampler_view_reference(&src_view
, NULL
);
1341 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1342 struct pipe_surface
*dst
,
1343 const struct pipe_box
*dstbox
,
1344 struct pipe_sampler_view
*src
,
1345 const struct pipe_box
*srcbox
,
1346 unsigned src_width0
, unsigned src_height0
,
1347 unsigned mask
, unsigned filter
,
1348 const struct pipe_scissor_state
*scissor
)
1350 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1351 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1352 struct pipe_framebuffer_state fb_state
;
1353 enum pipe_texture_target src_target
= src
->texture
->target
;
1354 unsigned src_samples
= src
->texture
->nr_samples
;
1355 unsigned dst_samples
= dst
->texture
->nr_samples
;
1356 boolean has_depth
, has_stencil
, has_color
;
1357 boolean blit_stencil
, blit_depth
, blit_color
;
1358 void *sampler_state
;
1359 const struct util_format_description
*src_desc
=
1360 util_format_description(src
->format
);
1361 const struct util_format_description
*dst_desc
=
1362 util_format_description(dst
->format
);
1364 has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
&&
1365 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1366 has_depth
= util_format_has_depth(src_desc
) &&
1367 util_format_has_depth(dst_desc
);
1368 has_stencil
= util_format_has_stencil(src_desc
) &&
1369 util_format_has_stencil(dst_desc
);
1371 blit_color
= has_color
&& (mask
& PIPE_MASK_RGBA
);
1372 blit_depth
= has_depth
&& (mask
& PIPE_MASK_Z
);
1373 blit_stencil
= has_stencil
&& (mask
& PIPE_MASK_S
) &&
1374 ctx
->has_stencil_export
;
1376 if (!blit_stencil
&& !blit_depth
&& !blit_color
) {
1381 (dstbox
->width
== abs(srcbox
->width
) &&
1382 dstbox
->height
== abs(srcbox
->height
))) {
1383 filter
= PIPE_TEX_FILTER_NEAREST
;
1386 /* Check whether the states are properly saved. */
1387 blitter_set_running_flag(ctx
);
1388 blitter_check_saved_vertex_states(ctx
);
1389 blitter_check_saved_fragment_states(ctx
);
1390 blitter_check_saved_textures(ctx
);
1391 blitter_check_saved_fb_state(ctx
);
1392 blitter_disable_render_cond(ctx
);
1394 /* Initialize framebuffer state. */
1395 fb_state
.width
= dst
->width
;
1396 fb_state
.height
= dst
->height
;
1397 fb_state
.nr_cbufs
= blit_depth
|| blit_stencil
? 0 : 1;
1398 fb_state
.cbufs
[0] = NULL
;
1399 fb_state
.zsbuf
= NULL
;
1401 if (blit_depth
|| blit_stencil
) {
1402 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1404 if (blit_depth
&& blit_stencil
) {
1405 pipe
->bind_depth_stencil_alpha_state(pipe
,
1406 ctx
->dsa_write_depth_stencil
);
1407 ctx
->bind_fs_state(pipe
,
1408 blitter_get_fs_texfetch_depthstencil(ctx
, src_target
,
1410 } else if (blit_depth
) {
1411 pipe
->bind_depth_stencil_alpha_state(pipe
,
1412 ctx
->dsa_write_depth_keep_stencil
);
1413 ctx
->bind_fs_state(pipe
,
1414 blitter_get_fs_texfetch_depth(ctx
, src_target
,
1416 } else { /* is_stencil */
1417 pipe
->bind_depth_stencil_alpha_state(pipe
,
1418 ctx
->dsa_keep_depth_write_stencil
);
1419 ctx
->bind_fs_state(pipe
,
1420 blitter_get_fs_texfetch_stencil(ctx
, src_target
,
1425 pipe
->bind_blend_state(pipe
, ctx
->blend
[mask
& PIPE_MASK_RGBA
]);
1426 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1427 ctx
->bind_fs_state(pipe
,
1428 blitter_get_fs_texfetch_col(ctx
, src
->format
, src_target
,
1429 src_samples
, dst_samples
, filter
));
1432 /* Set the linear filter only for scaled color non-MSAA blits. */
1433 if (filter
== PIPE_TEX_FILTER_LINEAR
) {
1434 if (src_target
== PIPE_TEXTURE_RECT
) {
1435 sampler_state
= ctx
->sampler_state_rect_linear
;
1437 sampler_state
= ctx
->sampler_state_linear
;
1440 if (src_target
== PIPE_TEXTURE_RECT
) {
1441 sampler_state
= ctx
->sampler_state_rect
;
1443 sampler_state
= ctx
->sampler_state
;
1448 if (blit_depth
&& blit_stencil
) {
1449 /* Setup two samplers, one for depth and the other one for stencil. */
1450 struct pipe_sampler_view templ
;
1451 struct pipe_sampler_view
*views
[2];
1452 void *samplers
[2] = {sampler_state
, sampler_state
};
1455 templ
.format
= util_format_stencil_only(templ
.format
);
1456 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1459 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1461 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, views
);
1462 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, samplers
);
1464 pipe_sampler_view_reference(&views
[1], NULL
);
1465 } else if (blit_stencil
) {
1466 /* Set a stencil-only sampler view for it not to sample depth instead. */
1467 struct pipe_sampler_view templ
;
1468 struct pipe_sampler_view
*view
;
1471 templ
.format
= util_format_stencil_only(templ
.format
);
1472 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1474 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1476 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &view
);
1477 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1478 0, 1, &sampler_state
);
1480 pipe_sampler_view_reference(&view
, NULL
);
1482 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src
);
1483 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1484 0, 1, &sampler_state
);
1487 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1489 pipe
->set_scissor_states(pipe
, 0, 1, scissor
);
1492 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
, FALSE
);
1493 blitter_set_dst_dimensions(ctx
, dst
->width
, dst
->height
);
1495 if ((src_target
== PIPE_TEXTURE_1D
||
1496 src_target
== PIPE_TEXTURE_2D
||
1497 src_target
== PIPE_TEXTURE_RECT
) &&
1499 /* Draw the quad with the draw_rectangle callback. */
1501 /* Set texture coordinates. - use a pipe color union
1502 * for interface purposes.
1503 * XXX pipe_color_union is a wrong name since we use that to set
1504 * texture coordinates too.
1506 union pipe_color_union coord
;
1507 get_texcoords(src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1508 srcbox
->x
+srcbox
->width
, srcbox
->y
+srcbox
->height
, coord
.f
);
1510 /* Set framebuffer state. */
1511 if (blit_depth
|| blit_stencil
) {
1512 fb_state
.zsbuf
= dst
;
1514 fb_state
.cbufs
[0] = dst
;
1516 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1519 pipe
->set_sample_mask(pipe
, ~0);
1520 blitter
->draw_rectangle(blitter
, dstbox
->x
, dstbox
->y
,
1521 dstbox
->x
+ dstbox
->width
,
1522 dstbox
->y
+ dstbox
->height
, 0,
1523 UTIL_BLITTER_ATTRIB_TEXCOORD
, &coord
);
1525 /* Draw the quad with the generic codepath. */
1527 for (z
= 0; z
< dstbox
->depth
; z
++) {
1528 struct pipe_surface
*old
;
1530 /* Set framebuffer state. */
1531 if (blit_depth
|| blit_stencil
) {
1532 fb_state
.zsbuf
= dst
;
1534 fb_state
.cbufs
[0] = dst
;
1536 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1538 /* See if we need to blit a multisample or singlesample buffer. */
1539 if (src_samples
== dst_samples
&& dst_samples
> 1) {
1541 unsigned i
, max_sample
= dst_samples
- 1;
1543 for (i
= 0; i
<= max_sample
; i
++) {
1544 pipe
->set_sample_mask(pipe
, 1 << i
);
1545 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1547 i
, srcbox
->x
, srcbox
->y
,
1548 srcbox
->x
+ srcbox
->width
,
1549 srcbox
->y
+ srcbox
->height
);
1550 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1551 dstbox
->x
+ dstbox
->width
,
1552 dstbox
->y
+ dstbox
->height
, 0, 1);
1555 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1556 pipe
->set_sample_mask(pipe
, ~0);
1557 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1559 srcbox
->x
, srcbox
->y
,
1560 srcbox
->x
+ srcbox
->width
,
1561 srcbox
->y
+ srcbox
->height
);
1562 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1563 dstbox
->x
+ dstbox
->width
,
1564 dstbox
->y
+ dstbox
->height
, 0, 1);
1567 /* Get the next surface or (if this is the last iteration)
1568 * just unreference the last one. */
1570 if (z
< dstbox
->depth
-1) {
1571 dst
= ctx
->base
.get_next_surface_layer(ctx
->base
.pipe
, dst
);
1574 pipe_surface_reference(&old
, NULL
);
1579 blitter_restore_vertex_states(ctx
);
1580 blitter_restore_fragment_states(ctx
);
1581 blitter_restore_textures(ctx
);
1582 blitter_restore_fb_state(ctx
);
1584 pipe
->set_scissor_states(pipe
, 0, 1, &ctx
->base
.saved_scissor
);
1586 blitter_restore_render_cond(ctx
);
1587 blitter_unset_running_flag(ctx
);
1591 util_blitter_blit(struct blitter_context
*blitter
,
1592 const struct pipe_blit_info
*info
)
1594 struct pipe_resource
*dst
= info
->dst
.resource
;
1595 struct pipe_resource
*src
= info
->src
.resource
;
1596 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1597 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1598 struct pipe_surface
*dst_view
, dst_templ
;
1599 struct pipe_sampler_view src_templ
, *src_view
;
1601 /* Initialize the surface. */
1602 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
1604 dst_templ
.format
= info
->dst
.format
;
1605 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1607 /* Initialize the sampler view. */
1608 util_blitter_default_src_texture(&src_templ
, src
, info
->src
.level
);
1609 src_templ
.format
= info
->src
.format
;
1610 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1613 util_blitter_blit_generic(blitter
, dst_view
, &info
->dst
.box
,
1614 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
1615 info
->mask
, info
->filter
,
1616 info
->scissor_enable
? &info
->scissor
: NULL
);
1618 pipe_surface_reference(&dst_view
, NULL
);
1619 pipe_sampler_view_reference(&src_view
, NULL
);
1622 /* Clear a region of a color surface to a constant value. */
1623 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
1624 struct pipe_surface
*dstsurf
,
1625 const union pipe_color_union
*color
,
1626 unsigned dstx
, unsigned dsty
,
1627 unsigned width
, unsigned height
)
1629 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1630 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1631 struct pipe_framebuffer_state fb_state
;
1633 assert(dstsurf
->texture
);
1634 if (!dstsurf
->texture
)
1637 /* check the saved state */
1638 blitter_set_running_flag(ctx
);
1639 blitter_check_saved_vertex_states(ctx
);
1640 blitter_check_saved_fragment_states(ctx
);
1641 blitter_check_saved_fb_state(ctx
);
1642 blitter_disable_render_cond(ctx
);
1645 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
]);
1646 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1647 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
1648 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1650 /* set a framebuffer state */
1651 fb_state
.width
= dstsurf
->width
;
1652 fb_state
.height
= dstsurf
->height
;
1653 fb_state
.nr_cbufs
= 1;
1654 fb_state
.cbufs
[0] = dstsurf
;
1656 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1657 pipe
->set_sample_mask(pipe
, ~0);
1659 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1660 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1661 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
1662 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1664 blitter_restore_vertex_states(ctx
);
1665 blitter_restore_fragment_states(ctx
);
1666 blitter_restore_fb_state(ctx
);
1667 blitter_restore_render_cond(ctx
);
1668 blitter_unset_running_flag(ctx
);
1671 /* Clear a region of a depth stencil surface. */
1672 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
1673 struct pipe_surface
*dstsurf
,
1674 unsigned clear_flags
,
1677 unsigned dstx
, unsigned dsty
,
1678 unsigned width
, unsigned height
)
1680 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1681 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1682 struct pipe_framebuffer_state fb_state
;
1683 struct pipe_stencil_ref sr
= { { 0 } };
1685 assert(dstsurf
->texture
);
1686 if (!dstsurf
->texture
)
1689 /* check the saved state */
1690 blitter_set_running_flag(ctx
);
1691 blitter_check_saved_vertex_states(ctx
);
1692 blitter_check_saved_fragment_states(ctx
);
1693 blitter_check_saved_fb_state(ctx
);
1694 blitter_disable_render_cond(ctx
);
1697 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1698 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1699 sr
.ref_value
[0] = stencil
& 0xff;
1700 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1701 pipe
->set_stencil_ref(pipe
, &sr
);
1703 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
1704 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1706 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
1707 sr
.ref_value
[0] = stencil
& 0xff;
1708 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1709 pipe
->set_stencil_ref(pipe
, &sr
);
1712 /* hmm that should be illegal probably, or make it a no-op somewhere */
1713 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1715 ctx
->bind_fs_state(pipe
, ctx
->fs_empty
);
1716 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1718 /* set a framebuffer state */
1719 fb_state
.width
= dstsurf
->width
;
1720 fb_state
.height
= dstsurf
->height
;
1721 fb_state
.nr_cbufs
= 0;
1722 fb_state
.cbufs
[0] = 0;
1723 fb_state
.zsbuf
= dstsurf
;
1724 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1725 pipe
->set_sample_mask(pipe
, ~0);
1727 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1728 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1729 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
,
1731 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
1733 blitter_restore_vertex_states(ctx
);
1734 blitter_restore_fragment_states(ctx
);
1735 blitter_restore_fb_state(ctx
);
1736 blitter_restore_render_cond(ctx
);
1737 blitter_unset_running_flag(ctx
);
1740 /* draw a rectangle across a region using a custom dsa stage - for r600g */
1741 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
1742 struct pipe_surface
*zsurf
,
1743 struct pipe_surface
*cbsurf
,
1744 unsigned sample_mask
,
1745 void *dsa_stage
, float depth
)
1747 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1748 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1749 struct pipe_framebuffer_state fb_state
;
1751 assert(zsurf
->texture
);
1752 if (!zsurf
->texture
)
1755 /* check the saved state */
1756 blitter_set_running_flag(ctx
);
1757 blitter_check_saved_vertex_states(ctx
);
1758 blitter_check_saved_fragment_states(ctx
);
1759 blitter_check_saved_fb_state(ctx
);
1760 blitter_disable_render_cond(ctx
);
1763 pipe
->bind_blend_state(pipe
, cbsurf
? ctx
->blend
[PIPE_MASK_RGBA
] :
1765 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
1766 ctx
->bind_fs_state(pipe
, ctx
->fs_empty
);
1767 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1769 /* set a framebuffer state */
1770 fb_state
.width
= zsurf
->width
;
1771 fb_state
.height
= zsurf
->height
;
1772 fb_state
.nr_cbufs
= 1;
1774 fb_state
.cbufs
[0] = cbsurf
;
1775 fb_state
.nr_cbufs
= 1;
1777 fb_state
.cbufs
[0] = NULL
;
1778 fb_state
.nr_cbufs
= 0;
1780 fb_state
.zsbuf
= zsurf
;
1781 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1782 pipe
->set_sample_mask(pipe
, sample_mask
);
1784 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1785 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
1786 blitter
->draw_rectangle(blitter
, 0, 0, zsurf
->width
, zsurf
->height
, depth
,
1787 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
1789 blitter_restore_vertex_states(ctx
);
1790 blitter_restore_fragment_states(ctx
);
1791 blitter_restore_fb_state(ctx
);
1792 blitter_restore_render_cond(ctx
);
1793 blitter_unset_running_flag(ctx
);
1796 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
1797 struct pipe_resource
*dst
,
1799 struct pipe_resource
*src
,
1803 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1804 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1805 struct pipe_vertex_buffer vb
;
1806 struct pipe_stream_output_target
*so_target
;
1808 if (srcx
>= src
->width0
||
1809 dstx
>= dst
->width0
) {
1812 if (srcx
+ size
> src
->width0
) {
1813 size
= src
->width0
- srcx
;
1815 if (dstx
+ size
> dst
->width0
) {
1816 size
= dst
->width0
- dstx
;
1819 /* Drivers not capable of Stream Out should not call this function
1820 * in the first place. */
1821 assert(ctx
->has_stream_out
);
1823 /* Some alignment is required. */
1824 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
1825 !ctx
->has_stream_out
) {
1826 struct pipe_box box
;
1827 u_box_1d(srcx
, size
, &box
);
1828 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
1832 blitter_set_running_flag(ctx
);
1833 blitter_check_saved_vertex_states(ctx
);
1834 blitter_disable_render_cond(ctx
);
1837 vb
.buffer_offset
= srcx
;
1840 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1841 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[0]);
1842 pipe
->bind_vs_state(pipe
, ctx
->vs_pos_only
);
1843 if (ctx
->has_geometry_shader
)
1844 pipe
->bind_gs_state(pipe
, NULL
);
1845 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
1847 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
1848 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, 0);
1850 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
1852 blitter_restore_vertex_states(ctx
);
1853 blitter_restore_render_cond(ctx
);
1854 blitter_unset_running_flag(ctx
);
1855 pipe_so_target_reference(&so_target
, NULL
);
1858 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
1859 struct pipe_resource
*dst
,
1860 unsigned offset
, unsigned size
,
1861 unsigned num_channels
,
1862 const union pipe_color_union
*clear_value
)
1864 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1865 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1866 struct pipe_vertex_buffer vb
= {0};
1867 struct pipe_stream_output_target
*so_target
;
1869 assert(num_channels
>= 1);
1870 assert(num_channels
<= 4);
1872 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
1874 * R600 uses this to initialize texture resources, so width0 might not be
1875 * what you think it is.
1878 /* Streamout is required. */
1879 if (!ctx
->has_stream_out
) {
1880 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
1884 /* Some alignment is required. */
1885 if (offset
% 4 != 0 || size
% 4 != 0) {
1886 assert(!"Bad alignment in util_blitter_clear_buffer()");
1890 u_upload_data(ctx
->upload
, 0, num_channels
*4, clear_value
,
1891 &vb
.buffer_offset
, &vb
.buffer
);
1894 blitter_set_running_flag(ctx
);
1895 blitter_check_saved_vertex_states(ctx
);
1896 blitter_disable_render_cond(ctx
);
1898 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1899 pipe
->bind_vertex_elements_state(pipe
,
1900 ctx
->velem_state_readbuf
[num_channels
-1]);
1901 pipe
->bind_vs_state(pipe
, ctx
->vs_pos_only
);
1902 if (ctx
->has_geometry_shader
)
1903 pipe
->bind_gs_state(pipe
, NULL
);
1904 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
1906 so_target
= pipe
->create_stream_output_target(pipe
, dst
, offset
, size
);
1907 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, 0);
1909 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
1911 blitter_restore_vertex_states(ctx
);
1912 blitter_restore_render_cond(ctx
);
1913 blitter_unset_running_flag(ctx
);
1914 pipe_so_target_reference(&so_target
, NULL
);
1915 pipe_resource_reference(&vb
.buffer
, NULL
);
1918 /* probably radeon specific */
1919 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
1920 struct pipe_resource
*dst
,
1923 struct pipe_resource
*src
,
1925 unsigned sample_mask
,
1927 enum pipe_format format
)
1929 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1930 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1931 struct pipe_framebuffer_state fb_state
;
1932 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
1934 blitter_set_running_flag(ctx
);
1935 blitter_check_saved_vertex_states(ctx
);
1936 blitter_check_saved_fragment_states(ctx
);
1937 blitter_disable_render_cond(ctx
);
1940 pipe
->bind_blend_state(pipe
, custom_blend
);
1941 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1942 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1943 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
1944 pipe
->set_sample_mask(pipe
, sample_mask
);
1946 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1947 surf_tmpl
.format
= format
;
1948 surf_tmpl
.u
.tex
.level
= dst_level
;
1949 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
1950 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
1952 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
1954 surf_tmpl
.u
.tex
.level
= 0;
1955 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
1956 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
1958 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
1960 /* set a framebuffer state */
1961 fb_state
.width
= src
->width0
;
1962 fb_state
.height
= src
->height0
;
1963 fb_state
.nr_cbufs
= 2;
1964 fb_state
.cbufs
[0] = srcsurf
;
1965 fb_state
.cbufs
[1] = dstsurf
;
1966 fb_state
.zsbuf
= NULL
;
1967 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1969 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
1970 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
1971 blitter
->draw_rectangle(blitter
, 0, 0, src
->width0
, src
->height0
,
1973 blitter_restore_fb_state(ctx
);
1974 blitter_restore_vertex_states(ctx
);
1975 blitter_restore_fragment_states(ctx
);
1976 blitter_restore_render_cond(ctx
);
1977 blitter_unset_running_flag(ctx
);
1979 pipe_surface_reference(&srcsurf
, NULL
);
1980 pipe_surface_reference(&dstsurf
, NULL
);
1983 void util_blitter_custom_color(struct blitter_context
*blitter
,
1984 struct pipe_surface
*dstsurf
,
1987 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1988 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1989 struct pipe_framebuffer_state fb_state
;
1991 assert(dstsurf
->texture
);
1992 if (!dstsurf
->texture
)
1995 /* check the saved state */
1996 blitter_set_running_flag(ctx
);
1997 blitter_check_saved_vertex_states(ctx
);
1998 blitter_check_saved_fragment_states(ctx
);
1999 blitter_check_saved_fb_state(ctx
);
2000 blitter_disable_render_cond(ctx
);
2003 pipe
->bind_blend_state(pipe
, custom_blend
? custom_blend
2004 : ctx
->blend
[PIPE_MASK_RGBA
]);
2005 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
2006 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
2007 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
2008 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
2010 /* set a framebuffer state */
2011 fb_state
.width
= dstsurf
->width
;
2012 fb_state
.height
= dstsurf
->height
;
2013 fb_state
.nr_cbufs
= 1;
2014 fb_state
.cbufs
[0] = dstsurf
;
2016 pipe
->set_framebuffer_state(pipe
, &fb_state
);
2017 pipe
->set_sample_mask(pipe
, ~0);
2019 blitter_set_common_draw_rect_state(ctx
, FALSE
, FALSE
);
2020 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
2021 blitter
->draw_rectangle(blitter
, 0, 0, dstsurf
->width
, dstsurf
->height
,
2024 blitter_restore_vertex_states(ctx
);
2025 blitter_restore_fragment_states(ctx
);
2026 blitter_restore_fb_state(ctx
);
2027 blitter_restore_render_cond(ctx
);
2028 blitter_unset_running_flag(ctx
);