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 struct blitter_context_priv
56 struct blitter_context base
;
58 struct u_upload_mgr
*upload
;
60 float vertices
[4][2][4]; /**< {pos, color} or {pos, texcoord} */
62 /* Templates for various state objects. */
64 /* Constant state objects. */
66 void *vs
; /**< Vertex shader which passes {pos, generic} to the output.*/
67 void *vs_pos_only
; /**< Vertex shader which passes pos to the output.*/
69 /* Fragment shaders. */
70 /* The shader at index i outputs color to color buffers 0,1,...,i-1. */
71 void *fs_col
[PIPE_MAX_COLOR_BUFS
+1];
72 void *fs_col_int
[PIPE_MAX_COLOR_BUFS
+1];
74 /* FS which outputs a color from a texture,
75 where the index is PIPE_TEXTURE_* to be sampled. */
76 void *fs_texfetch_col
[PIPE_MAX_TEXTURE_TYPES
];
78 /* FS which outputs a depth from a texture,
79 where the index is PIPE_TEXTURE_* to be sampled. */
80 void *fs_texfetch_depth
[PIPE_MAX_TEXTURE_TYPES
];
81 void *fs_texfetch_depthstencil
[PIPE_MAX_TEXTURE_TYPES
];
82 void *fs_texfetch_stencil
[PIPE_MAX_TEXTURE_TYPES
];
84 /* FS which outputs one sample from a multisample texture. */
85 void *fs_texfetch_col_msaa
[PIPE_MAX_TEXTURE_TYPES
];
86 void *fs_texfetch_depth_msaa
[PIPE_MAX_TEXTURE_TYPES
];
87 void *fs_texfetch_depthstencil_msaa
[PIPE_MAX_TEXTURE_TYPES
];
88 void *fs_texfetch_stencil_msaa
[PIPE_MAX_TEXTURE_TYPES
];
91 void *blend
[PIPE_MASK_RGBA
+1]; /**< blend state with writemask */
93 /* Depth stencil alpha state. */
94 void *dsa_write_depth_stencil
;
95 void *dsa_write_depth_keep_stencil
;
96 void *dsa_keep_depth_stencil
;
97 void *dsa_keep_depth_write_stencil
;
99 /* Vertex elements states. */
101 void *velem_uint_state
;
102 void *velem_sint_state
;
103 void *velem_state_readbuf
;
106 void *sampler_state
, *sampler_state_linear
;
108 /* Rasterizer state. */
109 void *rs_state
, *rs_state_scissor
, *rs_discard_state
;
111 /* Viewport state. */
112 struct pipe_viewport_state viewport
;
114 /* Destination surface dimensions. */
118 boolean has_geometry_shader
;
119 boolean vertex_has_integers
;
120 boolean has_stream_out
;
121 boolean has_stencil_export
;
122 boolean has_texture_multisample
;
124 /* The Draw module overrides these functions.
125 * Always create the blitter before Draw. */
126 void (*bind_fs_state
)(struct pipe_context
*, void *);
127 void (*delete_fs_state
)(struct pipe_context
*, void *);
130 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
)
132 struct blitter_context_priv
*ctx
;
133 struct pipe_blend_state blend
;
134 struct pipe_depth_stencil_alpha_state dsa
;
135 struct pipe_rasterizer_state rs_state
;
136 struct pipe_sampler_state sampler_state
;
137 struct pipe_vertex_element velem
[2];
140 ctx
= CALLOC_STRUCT(blitter_context_priv
);
144 ctx
->base
.pipe
= pipe
;
145 ctx
->base
.draw_rectangle
= util_blitter_draw_rectangle
;
147 ctx
->bind_fs_state
= pipe
->bind_fs_state
;
148 ctx
->delete_fs_state
= pipe
->delete_fs_state
;
150 /* init state objects for them to be considered invalid */
151 ctx
->base
.saved_blend_state
= INVALID_PTR
;
152 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
153 ctx
->base
.saved_rs_state
= INVALID_PTR
;
154 ctx
->base
.saved_fs
= INVALID_PTR
;
155 ctx
->base
.saved_vs
= INVALID_PTR
;
156 ctx
->base
.saved_gs
= INVALID_PTR
;
157 ctx
->base
.saved_velem_state
= INVALID_PTR
;
158 ctx
->base
.saved_fb_state
.nr_cbufs
= ~0;
159 ctx
->base
.saved_num_sampler_views
= ~0;
160 ctx
->base
.saved_num_sampler_states
= ~0;
161 ctx
->base
.saved_num_vertex_buffers
= ~0;
162 ctx
->base
.saved_num_so_targets
= ~0;
164 ctx
->has_geometry_shader
=
165 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
166 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
167 ctx
->vertex_has_integers
=
168 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_VERTEX
,
169 PIPE_SHADER_CAP_INTEGERS
);
170 ctx
->has_stream_out
=
171 pipe
->screen
->get_param(pipe
->screen
,
172 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0;
174 ctx
->has_stencil_export
=
175 pipe
->screen
->get_param(pipe
->screen
,
176 PIPE_CAP_SHADER_STENCIL_EXPORT
);
178 ctx
->has_texture_multisample
=
179 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TEXTURE_MULTISAMPLE
);
181 /* blend state objects */
182 memset(&blend
, 0, sizeof(blend
));
184 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
185 blend
.rt
[0].colormask
= i
;
186 ctx
->blend
[i
] = pipe
->create_blend_state(pipe
, &blend
);
189 /* depth stencil alpha state objects */
190 memset(&dsa
, 0, sizeof(dsa
));
191 ctx
->dsa_keep_depth_stencil
=
192 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
194 dsa
.depth
.enabled
= 1;
195 dsa
.depth
.writemask
= 1;
196 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
197 ctx
->dsa_write_depth_keep_stencil
=
198 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
200 dsa
.stencil
[0].enabled
= 1;
201 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
202 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
203 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
204 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
205 dsa
.stencil
[0].valuemask
= 0xff;
206 dsa
.stencil
[0].writemask
= 0xff;
207 ctx
->dsa_write_depth_stencil
=
208 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
210 dsa
.depth
.enabled
= 0;
211 dsa
.depth
.writemask
= 0;
212 ctx
->dsa_keep_depth_write_stencil
=
213 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
216 memset(&sampler_state
, 0, sizeof(sampler_state
));
217 sampler_state
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
218 sampler_state
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
219 sampler_state
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
220 sampler_state
.normalized_coords
= 1;
221 ctx
->sampler_state
= pipe
->create_sampler_state(pipe
, &sampler_state
);
223 sampler_state
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
224 sampler_state
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
225 ctx
->sampler_state_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
227 /* rasterizer state */
228 memset(&rs_state
, 0, sizeof(rs_state
));
229 rs_state
.cull_face
= PIPE_FACE_NONE
;
230 rs_state
.gl_rasterization_rules
= 1;
231 rs_state
.flatshade
= 1;
232 rs_state
.depth_clip
= 1;
233 ctx
->rs_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
235 rs_state
.scissor
= 1;
236 ctx
->rs_state_scissor
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
238 if (ctx
->has_stream_out
) {
239 rs_state
.scissor
= 0;
240 rs_state
.rasterizer_discard
= 1;
241 ctx
->rs_discard_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
244 /* vertex elements states */
245 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
246 for (i
= 0; i
< 2; i
++) {
247 velem
[i
].src_offset
= i
* 4 * sizeof(float);
248 velem
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
250 ctx
->velem_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
252 if (ctx
->vertex_has_integers
) {
253 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
254 velem
[0].src_offset
= 0;
255 velem
[0].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
256 velem
[1].src_offset
= 4 * sizeof(float);
257 velem
[1].src_format
= PIPE_FORMAT_R32G32B32A32_SINT
;
258 ctx
->velem_sint_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
260 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
261 velem
[0].src_offset
= 0;
262 velem
[0].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
263 velem
[1].src_offset
= 4 * sizeof(float);
264 velem
[1].src_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
265 ctx
->velem_uint_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
268 if (ctx
->has_stream_out
) {
269 velem
[0].src_format
= PIPE_FORMAT_R32_UINT
;
270 ctx
->velem_state_readbuf
= pipe
->create_vertex_elements_state(pipe
, 1, &velem
[0]);
273 /* fragment shaders are created on-demand */
277 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
278 TGSI_SEMANTIC_GENERIC
};
279 const uint semantic_indices
[] = { 0, 0 };
281 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
284 if (ctx
->has_stream_out
) {
285 struct pipe_stream_output_info so
;
286 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
};
287 const uint semantic_indices
[] = { 0 };
289 memset(&so
, 0, sizeof(so
));
291 so
.output
[0].num_components
= 1;
295 util_make_vertex_passthrough_shader_with_so(pipe
, 1, semantic_names
,
296 semantic_indices
, &so
);
299 /* set invariant vertex coordinates */
300 for (i
= 0; i
< 4; i
++)
301 ctx
->vertices
[i
][0][3] = 1; /*v.w*/
303 ctx
->upload
= u_upload_create(pipe
, 65536, 4, PIPE_BIND_VERTEX_BUFFER
);
308 void util_blitter_destroy(struct blitter_context
*blitter
)
310 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
311 struct pipe_context
*pipe
= blitter
->pipe
;
314 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
315 pipe
->delete_blend_state(pipe
, ctx
->blend
[i
]);
317 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
318 pipe
->delete_depth_stencil_alpha_state(pipe
,
319 ctx
->dsa_write_depth_keep_stencil
);
320 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
321 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
323 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
);
324 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state_scissor
);
325 if (ctx
->rs_discard_state
)
326 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_discard_state
);
327 pipe
->delete_vs_state(pipe
, ctx
->vs
);
328 if (ctx
->vs_pos_only
)
329 pipe
->delete_vs_state(pipe
, ctx
->vs_pos_only
);
330 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state
);
331 if (ctx
->vertex_has_integers
) {
332 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_sint_state
);
333 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_uint_state
);
335 if (ctx
->velem_state_readbuf
)
336 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
);
338 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
339 if (ctx
->fs_texfetch_col
[i
])
340 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[i
]);
341 if (ctx
->fs_texfetch_depth
[i
])
342 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
]);
343 if (ctx
->fs_texfetch_depthstencil
[i
])
344 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil
[i
]);
345 if (ctx
->fs_texfetch_stencil
[i
])
346 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil
[i
]);
349 for (i
= 0; i
<= PIPE_MAX_COLOR_BUFS
; i
++) {
351 ctx
->delete_fs_state(pipe
, ctx
->fs_col
[i
]);
352 if (ctx
->fs_col_int
[i
])
353 ctx
->delete_fs_state(pipe
, ctx
->fs_col_int
[i
]);
356 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
);
357 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_linear
);
358 u_upload_destroy(ctx
->upload
);
362 static void blitter_set_running_flag(struct blitter_context_priv
*ctx
)
364 if (ctx
->base
.running
) {
365 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
368 ctx
->base
.running
= TRUE
;
371 static void blitter_unset_running_flag(struct blitter_context_priv
*ctx
)
373 if (!ctx
->base
.running
) {
374 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
377 ctx
->base
.running
= FALSE
;
380 static void blitter_check_saved_vertex_states(struct blitter_context_priv
*ctx
)
382 assert(ctx
->base
.saved_num_vertex_buffers
!= ~0);
383 assert(ctx
->base
.saved_velem_state
!= INVALID_PTR
);
384 assert(ctx
->base
.saved_vs
!= INVALID_PTR
);
385 assert(!ctx
->has_geometry_shader
|| ctx
->base
.saved_gs
!= INVALID_PTR
);
386 assert(!ctx
->has_stream_out
|| ctx
->base
.saved_num_so_targets
!= ~0);
387 assert(ctx
->base
.saved_rs_state
!= INVALID_PTR
);
390 static void blitter_restore_vertex_states(struct blitter_context_priv
*ctx
)
392 struct pipe_context
*pipe
= ctx
->base
.pipe
;
395 /* Vertex buffers. */
396 pipe
->set_vertex_buffers(pipe
,
397 ctx
->base
.saved_num_vertex_buffers
,
398 ctx
->base
.saved_vertex_buffers
);
400 for (i
= 0; i
< ctx
->base
.saved_num_vertex_buffers
; i
++) {
401 if (ctx
->base
.saved_vertex_buffers
[i
].buffer
) {
402 pipe_resource_reference(&ctx
->base
.saved_vertex_buffers
[i
].buffer
,
406 ctx
->base
.saved_num_vertex_buffers
= ~0;
408 /* Vertex elements. */
409 pipe
->bind_vertex_elements_state(pipe
, ctx
->base
.saved_velem_state
);
410 ctx
->base
.saved_velem_state
= INVALID_PTR
;
413 pipe
->bind_vs_state(pipe
, ctx
->base
.saved_vs
);
414 ctx
->base
.saved_vs
= INVALID_PTR
;
416 /* Geometry shader. */
417 if (ctx
->has_geometry_shader
) {
418 pipe
->bind_gs_state(pipe
, ctx
->base
.saved_gs
);
419 ctx
->base
.saved_gs
= INVALID_PTR
;
422 /* Stream outputs. */
423 if (ctx
->has_stream_out
) {
424 pipe
->set_stream_output_targets(pipe
,
425 ctx
->base
.saved_num_so_targets
,
426 ctx
->base
.saved_so_targets
, ~0);
428 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
429 pipe_so_target_reference(&ctx
->base
.saved_so_targets
[i
], NULL
);
431 ctx
->base
.saved_num_so_targets
= ~0;
435 pipe
->bind_rasterizer_state(pipe
, ctx
->base
.saved_rs_state
);
436 ctx
->base
.saved_rs_state
= INVALID_PTR
;
439 static void blitter_check_saved_fragment_states(struct blitter_context_priv
*ctx
)
441 assert(ctx
->base
.saved_fs
!= INVALID_PTR
);
442 assert(ctx
->base
.saved_dsa_state
!= INVALID_PTR
);
443 assert(ctx
->base
.saved_blend_state
!= INVALID_PTR
);
446 static void blitter_restore_fragment_states(struct blitter_context_priv
*ctx
)
448 struct pipe_context
*pipe
= ctx
->base
.pipe
;
450 /* Fragment shader. */
451 ctx
->bind_fs_state(pipe
, ctx
->base
.saved_fs
);
452 ctx
->base
.saved_fs
= INVALID_PTR
;
454 /* Depth, stencil, alpha. */
455 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->base
.saved_dsa_state
);
456 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
459 pipe
->bind_blend_state(pipe
, ctx
->base
.saved_blend_state
);
460 ctx
->base
.saved_blend_state
= INVALID_PTR
;
463 if (ctx
->base
.is_sample_mask_saved
) {
464 pipe
->set_sample_mask(pipe
, ctx
->base
.saved_sample_mask
);
465 ctx
->base
.is_sample_mask_saved
= FALSE
;
468 /* Miscellaneous states. */
469 /* XXX check whether these are saved and whether they need to be restored
470 * (depending on the operation) */
471 pipe
->set_stencil_ref(pipe
, &ctx
->base
.saved_stencil_ref
);
472 pipe
->set_viewport_state(pipe
, &ctx
->base
.saved_viewport
);
475 static void blitter_check_saved_fb_state(struct blitter_context_priv
*ctx
)
477 assert(ctx
->base
.saved_fb_state
.nr_cbufs
!= ~0);
480 static void blitter_disable_render_cond(struct blitter_context_priv
*ctx
)
482 struct pipe_context
*pipe
= ctx
->base
.pipe
;
484 if (ctx
->base
.saved_render_cond_query
) {
485 pipe
->render_condition(pipe
, NULL
, 0);
489 static void blitter_restore_render_cond(struct blitter_context_priv
*ctx
)
491 struct pipe_context
*pipe
= ctx
->base
.pipe
;
493 if (ctx
->base
.saved_render_cond_query
) {
494 pipe
->render_condition(pipe
, ctx
->base
.saved_render_cond_query
,
495 ctx
->base
.saved_render_cond_mode
);
496 ctx
->base
.saved_render_cond_query
= NULL
;
500 static void blitter_restore_fb_state(struct blitter_context_priv
*ctx
)
502 struct pipe_context
*pipe
= ctx
->base
.pipe
;
504 pipe
->set_framebuffer_state(pipe
, &ctx
->base
.saved_fb_state
);
505 util_unreference_framebuffer_state(&ctx
->base
.saved_fb_state
);
508 static void blitter_check_saved_textures(struct blitter_context_priv
*ctx
)
510 assert(ctx
->base
.saved_num_sampler_states
!= ~0);
511 assert(ctx
->base
.saved_num_sampler_views
!= ~0);
514 static void blitter_restore_textures(struct blitter_context_priv
*ctx
)
516 struct pipe_context
*pipe
= ctx
->base
.pipe
;
519 /* Fragment sampler states. */
520 pipe
->bind_fragment_sampler_states(pipe
,
521 ctx
->base
.saved_num_sampler_states
,
522 ctx
->base
.saved_sampler_states
);
523 ctx
->base
.saved_num_sampler_states
= ~0;
525 /* Fragment sampler views. */
526 pipe
->set_fragment_sampler_views(pipe
,
527 ctx
->base
.saved_num_sampler_views
,
528 ctx
->base
.saved_sampler_views
);
530 for (i
= 0; i
< ctx
->base
.saved_num_sampler_views
; i
++)
531 pipe_sampler_view_reference(&ctx
->base
.saved_sampler_views
[i
], NULL
);
533 ctx
->base
.saved_num_sampler_views
= ~0;
536 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
537 int x1
, int y1
, int x2
, int y2
,
542 /* set vertex positions */
543 ctx
->vertices
[0][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v0.x*/
544 ctx
->vertices
[0][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v0.y*/
546 ctx
->vertices
[1][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v1.x*/
547 ctx
->vertices
[1][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v1.y*/
549 ctx
->vertices
[2][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v2.x*/
550 ctx
->vertices
[2][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v2.y*/
552 ctx
->vertices
[3][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v3.x*/
553 ctx
->vertices
[3][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v3.y*/
555 for (i
= 0; i
< 4; i
++)
556 ctx
->vertices
[i
][0][2] = depth
; /*z*/
559 ctx
->viewport
.scale
[0] = 0.5f
* ctx
->dst_width
;
560 ctx
->viewport
.scale
[1] = 0.5f
* ctx
->dst_height
;
561 ctx
->viewport
.scale
[2] = 1.0f
;
562 ctx
->viewport
.scale
[3] = 1.0f
;
563 ctx
->viewport
.translate
[0] = 0.5f
* ctx
->dst_width
;
564 ctx
->viewport
.translate
[1] = 0.5f
* ctx
->dst_height
;
565 ctx
->viewport
.translate
[2] = 0.0f
;
566 ctx
->viewport
.translate
[3] = 0.0f
;
567 ctx
->base
.pipe
->set_viewport_state(ctx
->base
.pipe
, &ctx
->viewport
);
570 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
571 const union pipe_color_union
*color
)
576 for (i
= 0; i
< 4; i
++) {
577 uint32_t *uiverts
= (uint32_t *)ctx
->vertices
[i
][1];
578 uiverts
[0] = color
->ui
[0];
579 uiverts
[1] = color
->ui
[1];
580 uiverts
[2] = color
->ui
[2];
581 uiverts
[3] = color
->ui
[3];
584 for (i
= 0; i
< 4; i
++) {
585 ctx
->vertices
[i
][1][0] = 0;
586 ctx
->vertices
[i
][1][1] = 0;
587 ctx
->vertices
[i
][1][2] = 0;
588 ctx
->vertices
[i
][1][3] = 0;
593 static void get_texcoords(struct pipe_sampler_view
*src
,
594 unsigned src_width0
, unsigned src_height0
,
595 int x1
, int y1
, int x2
, int y2
,
598 struct pipe_resource
*tex
= src
->texture
;
599 unsigned level
= src
->u
.tex
.first_level
;
600 boolean normalized
= tex
->target
!= PIPE_TEXTURE_RECT
&&
601 tex
->nr_samples
<= 1;
604 out
[0] = x1
/ (float)u_minify(src_width0
, level
);
605 out
[1] = y1
/ (float)u_minify(src_height0
, level
);
606 out
[2] = x2
/ (float)u_minify(src_width0
, level
);
607 out
[3] = y2
/ (float)u_minify(src_height0
, level
);
616 static void set_texcoords_in_vertices(const float coord
[4],
617 float *out
, unsigned stride
)
619 out
[0] = coord
[0]; /*t0.s*/
620 out
[1] = coord
[1]; /*t0.t*/
622 out
[0] = coord
[2]; /*t1.s*/
623 out
[1] = coord
[1]; /*t1.t*/
625 out
[0] = coord
[2]; /*t2.s*/
626 out
[1] = coord
[3]; /*t2.t*/
628 out
[0] = coord
[0]; /*t3.s*/
629 out
[1] = coord
[3]; /*t3.t*/
632 static void blitter_set_texcoords(struct blitter_context_priv
*ctx
,
633 struct pipe_sampler_view
*src
,
634 unsigned src_width0
, unsigned src_height0
,
635 unsigned layer
, unsigned sample
,
636 int x1
, int y1
, int x2
, int y2
)
640 float face_coord
[4][2];
642 get_texcoords(src
, src_width0
, src_height0
, x1
, y1
, x2
, y2
, coord
);
644 if (src
->texture
->target
== PIPE_TEXTURE_CUBE
) {
645 set_texcoords_in_vertices(coord
, &face_coord
[0][0], 2);
646 util_map_texcoords2d_onto_cubemap(layer
,
647 /* pointer, stride in floats */
648 &face_coord
[0][0], 2,
649 &ctx
->vertices
[0][1][0], 8);
651 set_texcoords_in_vertices(coord
, &ctx
->vertices
[0][1][0], 8);
655 switch (src
->texture
->target
) {
656 case PIPE_TEXTURE_3D
:
658 float r
= layer
/ (float)u_minify(src
->texture
->depth0
,
659 src
->u
.tex
.first_level
);
660 for (i
= 0; i
< 4; i
++)
661 ctx
->vertices
[i
][1][2] = r
; /*r*/
665 case PIPE_TEXTURE_1D_ARRAY
:
666 for (i
= 0; i
< 4; i
++)
667 ctx
->vertices
[i
][1][1] = layer
; /*t*/
670 case PIPE_TEXTURE_2D_ARRAY
:
671 for (i
= 0; i
< 4; i
++) {
672 ctx
->vertices
[i
][1][2] = layer
; /*r*/
673 ctx
->vertices
[i
][1][3] = sample
; /*q*/
677 case PIPE_TEXTURE_2D
:
678 for (i
= 0; i
< 4; i
++) {
679 ctx
->vertices
[i
][1][2] = sample
; /*r*/
687 static void blitter_set_dst_dimensions(struct blitter_context_priv
*ctx
,
688 unsigned width
, unsigned height
)
690 ctx
->dst_width
= width
;
691 ctx
->dst_height
= height
;
694 static void *blitter_get_fs_col(struct blitter_context_priv
*ctx
,
695 unsigned num_cbufs
, boolean int_format
)
697 struct pipe_context
*pipe
= ctx
->base
.pipe
;
699 assert(num_cbufs
<= PIPE_MAX_COLOR_BUFS
);
702 if (!ctx
->fs_col_int
[num_cbufs
])
703 ctx
->fs_col_int
[num_cbufs
] =
704 util_make_fragment_cloneinput_shader(pipe
, num_cbufs
,
705 TGSI_SEMANTIC_GENERIC
,
706 TGSI_INTERPOLATE_CONSTANT
);
707 return ctx
->fs_col_int
[num_cbufs
];
709 if (!ctx
->fs_col
[num_cbufs
])
710 ctx
->fs_col
[num_cbufs
] =
711 util_make_fragment_cloneinput_shader(pipe
, num_cbufs
,
712 TGSI_SEMANTIC_GENERIC
,
713 TGSI_INTERPOLATE_LINEAR
);
714 return ctx
->fs_col
[num_cbufs
];
718 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
719 enum pipe_texture_target target
,
722 struct pipe_context
*pipe
= ctx
->base
.pipe
;
724 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
726 if (nr_samples
> 1) {
727 void **shader
= &ctx
->fs_texfetch_col_msaa
[target
];
729 /* Create the fragment shader on-demand. */
731 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
,
734 *shader
= util_make_fs_blit_msaa_color(pipe
, tgsi_tex
);
739 void **shader
= &ctx
->fs_texfetch_col
[target
];
741 /* Create the fragment shader on-demand. */
743 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
746 util_make_fragment_tex_shader(pipe
, tgsi_tex
,
747 TGSI_INTERPOLATE_LINEAR
);
755 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
756 enum pipe_texture_target target
,
759 struct pipe_context
*pipe
= ctx
->base
.pipe
;
761 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
763 if (nr_samples
> 1) {
764 void **shader
= &ctx
->fs_texfetch_depth_msaa
[target
];
766 /* Create the fragment shader on-demand. */
768 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
,
772 util_make_fs_blit_msaa_depth(pipe
, tgsi_tex
);
777 void **shader
= &ctx
->fs_texfetch_depth
[target
];
779 /* Create the fragment shader on-demand. */
781 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
784 util_make_fragment_tex_shader_writedepth(pipe
, tgsi_tex
,
785 TGSI_INTERPOLATE_LINEAR
);
793 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv
*ctx
,
794 enum pipe_texture_target target
,
797 struct pipe_context
*pipe
= ctx
->base
.pipe
;
799 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
801 if (nr_samples
> 1) {
802 void **shader
= &ctx
->fs_texfetch_depthstencil_msaa
[target
];
804 /* Create the fragment shader on-demand. */
806 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
,
810 util_make_fs_blit_msaa_depthstencil(pipe
, tgsi_tex
);
815 void **shader
= &ctx
->fs_texfetch_depthstencil
[target
];
817 /* Create the fragment shader on-demand. */
819 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
822 util_make_fragment_tex_shader_writedepthstencil(pipe
, tgsi_tex
,
823 TGSI_INTERPOLATE_LINEAR
);
831 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv
*ctx
,
832 enum pipe_texture_target target
,
835 struct pipe_context
*pipe
= ctx
->base
.pipe
;
837 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
839 if (nr_samples
> 1) {
840 void **shader
= &ctx
->fs_texfetch_stencil_msaa
[target
];
842 /* Create the fragment shader on-demand. */
844 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
,
848 util_make_fs_blit_msaa_stencil(pipe
, tgsi_tex
);
853 void **shader
= &ctx
->fs_texfetch_stencil
[target
];
855 /* Create the fragment shader on-demand. */
857 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
860 util_make_fragment_tex_shader_writestencil(pipe
, tgsi_tex
,
861 TGSI_INTERPOLATE_LINEAR
);
868 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
)
870 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
871 struct pipe_screen
*screen
= blitter
->pipe
->screen
;
872 unsigned num_cbufs
, i
, target
, max_samples
;
873 boolean has_arraytex
;
875 num_cbufs
= MAX2(screen
->get_param(screen
,
876 PIPE_CAP_MAX_RENDER_TARGETS
), 1);
877 max_samples
= ctx
->has_texture_multisample
? 2 : 1;
878 has_arraytex
= screen
->get_param(screen
,
879 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
) != 0;
881 for (i
= 0; i
< num_cbufs
; i
++) {
882 blitter_get_fs_col(ctx
, i
, FALSE
);
883 blitter_get_fs_col(ctx
, i
, TRUE
);
886 /* It only matters if i <= 1 or > 1. */
887 for (i
= 1; i
<= max_samples
; i
++) {
888 for (target
= PIPE_TEXTURE_1D
; target
< PIPE_MAX_TEXTURE_TYPES
; target
++) {
890 (target
== PIPE_TEXTURE_1D_ARRAY
||
891 target
== PIPE_TEXTURE_2D_ARRAY
)) {
895 blitter_get_fs_texfetch_col(ctx
, target
, i
);
896 blitter_get_fs_texfetch_depth(ctx
, target
, i
);
897 if (ctx
->has_stencil_export
) {
898 blitter_get_fs_texfetch_depthstencil(ctx
, target
, i
);
899 blitter_get_fs_texfetch_stencil(ctx
, target
, i
);
905 static void blitter_set_common_draw_rect_state(struct blitter_context_priv
*ctx
,
908 struct pipe_context
*pipe
= ctx
->base
.pipe
;
910 pipe
->bind_rasterizer_state(pipe
, scissor
? ctx
->rs_state_scissor
912 pipe
->bind_vs_state(pipe
, ctx
->vs
);
913 if (ctx
->has_geometry_shader
)
914 pipe
->bind_gs_state(pipe
, NULL
);
915 if (ctx
->has_stream_out
)
916 pipe
->set_stream_output_targets(pipe
, 0, NULL
, 0);
919 static void blitter_draw(struct blitter_context_priv
*ctx
,
920 int x1
, int y1
, int x2
, int y2
, float depth
)
922 struct pipe_resource
*buf
= NULL
;
925 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
927 u_upload_data(ctx
->upload
, 0, sizeof(ctx
->vertices
), ctx
->vertices
,
929 u_upload_unmap(ctx
->upload
);
930 util_draw_vertex_buffer(ctx
->base
.pipe
, NULL
, buf
, offset
,
931 PIPE_PRIM_TRIANGLE_FAN
, 4, 2);
932 pipe_resource_reference(&buf
, NULL
);
935 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
936 int x1
, int y1
, int x2
, int y2
, float depth
,
937 enum blitter_attrib_type type
,
938 const union pipe_color_union
*attrib
)
940 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
943 case UTIL_BLITTER_ATTRIB_COLOR
:
944 blitter_set_clear_color(ctx
, attrib
);
947 case UTIL_BLITTER_ATTRIB_TEXCOORD
:
948 set_texcoords_in_vertices(attrib
->f
, &ctx
->vertices
[0][1][0], 8);
954 blitter_draw(ctx
, x1
, y1
, x2
, y2
, depth
);
957 static void util_blitter_clear_custom(struct blitter_context
*blitter
,
958 unsigned width
, unsigned height
,
960 unsigned clear_buffers
,
961 enum pipe_format cbuf_format
,
962 const union pipe_color_union
*color
,
963 double depth
, unsigned stencil
,
964 void *custom_blend
, void *custom_dsa
)
966 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
967 struct pipe_context
*pipe
= ctx
->base
.pipe
;
968 struct pipe_stencil_ref sr
= { { 0 } };
969 boolean int_format
= util_format_is_pure_integer(cbuf_format
);
970 assert(num_cbufs
<= PIPE_MAX_COLOR_BUFS
);
972 blitter_set_running_flag(ctx
);
973 blitter_check_saved_vertex_states(ctx
);
974 blitter_check_saved_fragment_states(ctx
);
975 blitter_disable_render_cond(ctx
);
979 pipe
->bind_blend_state(pipe
, custom_blend
);
980 } else if (clear_buffers
& PIPE_CLEAR_COLOR
) {
981 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
]);
983 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
987 pipe
->bind_depth_stencil_alpha_state(pipe
, custom_dsa
);
988 } else if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
989 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
990 } else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
991 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
992 } else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
993 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
995 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
998 sr
.ref_value
[0] = stencil
& 0xff;
999 pipe
->set_stencil_ref(pipe
, &sr
);
1001 if (util_format_is_pure_sint(cbuf_format
)) {
1002 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_sint_state
);
1003 } else if (util_format_is_pure_uint(cbuf_format
)) {
1004 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_uint_state
);
1006 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1008 ctx
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, num_cbufs
, int_format
));
1009 pipe
->set_sample_mask(pipe
, ~0);
1011 blitter_set_common_draw_rect_state(ctx
, FALSE
);
1012 blitter_set_dst_dimensions(ctx
, width
, height
);
1013 blitter
->draw_rectangle(blitter
, 0, 0, width
, height
, depth
,
1014 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1016 blitter_restore_vertex_states(ctx
);
1017 blitter_restore_fragment_states(ctx
);
1018 blitter_restore_render_cond(ctx
);
1019 blitter_unset_running_flag(ctx
);
1022 void util_blitter_clear(struct blitter_context
*blitter
,
1023 unsigned width
, unsigned height
,
1025 unsigned clear_buffers
,
1026 enum pipe_format cbuf_format
,
1027 const union pipe_color_union
*color
,
1028 double depth
, unsigned stencil
)
1030 util_blitter_clear_custom(blitter
, width
, height
, num_cbufs
,
1031 clear_buffers
, cbuf_format
, color
, depth
, stencil
,
1035 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1036 unsigned width
, unsigned height
,
1037 double depth
, void *custom_dsa
)
1039 static const union pipe_color_union color
;
1040 util_blitter_clear_custom(blitter
, width
, height
, 0,
1041 0, PIPE_FORMAT_NONE
, &color
, depth
, 0, NULL
, custom_dsa
);
1045 boolean
is_overlap(int dstx
, int dsty
, int dstz
,
1046 const struct pipe_box
*srcbox
)
1048 struct pipe_box src
= *srcbox
;
1050 if (src
.width
< 0) {
1052 src
.width
= -src
.width
;
1054 if (src
.height
< 0) {
1055 src
.y
+= src
.height
;
1056 src
.height
= -src
.height
;
1058 if (src
.depth
< 0) {
1060 src
.depth
= -src
.depth
;
1062 return src
.x
< dstx
+src
.width
&& src
.x
+src
.width
> dstx
&&
1063 src
.y
< dsty
+src
.height
&& src
.y
+src
.height
> dsty
&&
1064 src
.z
< dstz
+src
.depth
&& src
.z
+src
.depth
> dstz
;
1067 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1068 struct pipe_resource
*dst
,
1071 const struct pipe_box
*srcbox
)
1073 memset(dst_templ
, 0, sizeof(*dst_templ
));
1074 dst_templ
->format
= dst
->format
;
1075 if (util_format_is_depth_or_stencil(dst
->format
)) {
1076 dst_templ
->usage
= PIPE_BIND_DEPTH_STENCIL
;
1078 dst_templ
->usage
= PIPE_BIND_RENDER_TARGET
;
1080 dst_templ
->format
= util_format_linear(dst
->format
);
1081 dst_templ
->u
.tex
.level
= dstlevel
;
1082 dst_templ
->u
.tex
.first_layer
= dstz
;
1083 dst_templ
->u
.tex
.last_layer
= dstz
+ srcbox
->depth
- 1;
1086 void util_blitter_default_src_texture(struct pipe_sampler_view
*src_templ
,
1087 struct pipe_resource
*src
,
1090 memset(src_templ
, 0, sizeof(*src_templ
));
1091 src_templ
->format
= util_format_linear(src
->format
);
1092 src_templ
->u
.tex
.first_level
= srclevel
;
1093 src_templ
->u
.tex
.last_level
= srclevel
;
1094 src_templ
->u
.tex
.first_layer
= 0;
1095 src_templ
->u
.tex
.last_layer
=
1096 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1097 : src
->array_size
- 1;
1098 src_templ
->swizzle_r
= PIPE_SWIZZLE_RED
;
1099 src_templ
->swizzle_g
= PIPE_SWIZZLE_GREEN
;
1100 src_templ
->swizzle_b
= PIPE_SWIZZLE_BLUE
;
1101 src_templ
->swizzle_a
= PIPE_SWIZZLE_ALPHA
;
1104 static boolean
is_blit_generic_supported(struct blitter_context
*blitter
,
1105 const struct pipe_resource
*dst
,
1106 enum pipe_format dst_format
,
1107 const struct pipe_resource
*src
,
1108 enum pipe_format src_format
,
1111 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1112 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1117 const struct util_format_description
*desc
=
1118 util_format_description(dst_format
);
1120 is_stencil
= util_format_has_stencil(desc
);
1122 /* Stencil export must be supported for stencil copy. */
1123 if ((mask
& PIPE_MASK_S
) && is_stencil
&& !ctx
->has_stencil_export
) {
1127 if (is_stencil
|| util_format_has_depth(desc
))
1128 bind
= PIPE_BIND_DEPTH_STENCIL
;
1130 bind
= PIPE_BIND_RENDER_TARGET
;
1132 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1133 dst
->nr_samples
, bind
)) {
1139 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1143 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1144 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
)) {
1148 /* Check stencil sampler support for stencil copy. */
1149 if (util_format_has_stencil(util_format_description(src_format
))) {
1150 enum pipe_format stencil_format
=
1151 util_format_stencil_only(src_format
);
1152 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1154 if (stencil_format
!= src_format
&&
1155 !screen
->is_format_supported(screen
, stencil_format
, src
->target
,
1156 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
)) {
1165 boolean
util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1166 const struct pipe_resource
*dst
,
1167 const struct pipe_resource
*src
,
1170 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1171 src
, src
->format
, mask
);
1174 boolean
util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1175 const struct pipe_blit_info
*info
)
1177 return is_blit_generic_supported(blitter
,
1178 info
->dst
.resource
, info
->dst
.format
,
1179 info
->src
.resource
, info
->src
.format
,
1183 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1184 struct pipe_resource
*dst
,
1186 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1187 struct pipe_resource
*src
,
1189 const struct pipe_box
*srcbox
, unsigned mask
,
1190 boolean copy_all_samples
)
1192 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1193 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1194 struct pipe_surface
*dst_view
, dst_templ
;
1195 struct pipe_sampler_view src_templ
, *src_view
;
1198 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1200 /* Initialize the surface. */
1201 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
, srcbox
);
1202 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1204 /* Initialize the sampler view. */
1205 util_blitter_default_src_texture(&src_templ
, src
, src_level
);
1206 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1209 util_blitter_blit_generic(blitter
, dst_view
, dstx
, dsty
,
1210 abs(srcbox
->width
), abs(srcbox
->height
),
1211 src_view
, srcbox
, src
->width0
, src
->height0
,
1212 mask
, PIPE_TEX_FILTER_NEAREST
, NULL
,
1215 pipe_surface_reference(&dst_view
, NULL
);
1216 pipe_sampler_view_reference(&src_view
, NULL
);
1219 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1220 struct pipe_surface
*dst
,
1222 unsigned dst_width
, unsigned dst_height
,
1223 struct pipe_sampler_view
*src
,
1224 const struct pipe_box
*srcbox
,
1225 unsigned src_width0
, unsigned src_height0
,
1226 unsigned mask
, unsigned filter
,
1227 const struct pipe_scissor_state
*scissor
,
1228 boolean copy_all_samples
)
1230 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1231 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1232 struct pipe_framebuffer_state fb_state
;
1233 enum pipe_texture_target src_target
= src
->texture
->target
;
1234 boolean has_depth
, has_stencil
, has_color
;
1235 boolean blit_stencil
, blit_depth
, blit_color
;
1236 void *sampler_state
;
1237 const struct util_format_description
*src_desc
=
1238 util_format_description(src
->format
);
1239 const struct util_format_description
*dst_desc
=
1240 util_format_description(dst
->format
);
1242 has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
&&
1243 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1244 has_depth
= util_format_has_depth(src_desc
) &&
1245 util_format_has_depth(dst_desc
);
1246 has_stencil
= util_format_has_stencil(src_desc
) &&
1247 util_format_has_stencil(dst_desc
);
1249 blit_color
= has_color
&& (mask
& PIPE_MASK_RGBA
);
1250 blit_depth
= has_depth
&& (mask
& PIPE_MASK_Z
);
1251 blit_stencil
= has_stencil
&& (mask
& PIPE_MASK_S
) &&
1252 ctx
->has_stencil_export
;
1254 if (!blit_stencil
&& !blit_depth
&& !blit_color
) {
1258 /* Sanity checks. */
1259 if (dst
->texture
== src
->texture
&&
1260 dst
->u
.tex
.level
== src
->u
.tex
.first_level
) {
1261 assert(!is_overlap(dstx
, dsty
, 0, srcbox
));
1263 /* XXX should handle 3d regions */
1264 assert(srcbox
->depth
== 1);
1266 /* Check whether the states are properly saved. */
1267 blitter_set_running_flag(ctx
);
1268 blitter_check_saved_vertex_states(ctx
);
1269 blitter_check_saved_fragment_states(ctx
);
1270 blitter_check_saved_textures(ctx
);
1271 blitter_check_saved_fb_state(ctx
);
1272 blitter_disable_render_cond(ctx
);
1274 /* Initialize framebuffer state. */
1275 fb_state
.width
= dst
->width
;
1276 fb_state
.height
= dst
->height
;
1278 if (blit_depth
|| blit_stencil
) {
1279 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1281 if (blit_depth
&& blit_stencil
) {
1282 pipe
->bind_depth_stencil_alpha_state(pipe
,
1283 ctx
->dsa_write_depth_stencil
);
1284 ctx
->bind_fs_state(pipe
,
1285 blitter_get_fs_texfetch_depthstencil(ctx
, src
->texture
->target
,
1286 src
->texture
->nr_samples
));
1287 } else if (blit_depth
) {
1288 pipe
->bind_depth_stencil_alpha_state(pipe
,
1289 ctx
->dsa_write_depth_keep_stencil
);
1290 ctx
->bind_fs_state(pipe
,
1291 blitter_get_fs_texfetch_depth(ctx
, src
->texture
->target
,
1292 src
->texture
->nr_samples
));
1293 } else { /* is_stencil */
1294 pipe
->bind_depth_stencil_alpha_state(pipe
,
1295 ctx
->dsa_keep_depth_write_stencil
);
1296 ctx
->bind_fs_state(pipe
,
1297 blitter_get_fs_texfetch_stencil(ctx
, src
->texture
->target
,
1298 src
->texture
->nr_samples
));
1301 fb_state
.nr_cbufs
= 0;
1302 fb_state
.zsbuf
= dst
;
1304 pipe
->bind_blend_state(pipe
, ctx
->blend
[mask
& PIPE_MASK_RGBA
]);
1305 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1306 ctx
->bind_fs_state(pipe
,
1307 blitter_get_fs_texfetch_col(ctx
, src
->texture
->target
,
1308 src
->texture
->nr_samples
));
1310 fb_state
.nr_cbufs
= 1;
1311 fb_state
.cbufs
[0] = dst
;
1315 /* Set the linear filter only for scaled color non-MSAA blits. */
1316 if (filter
== PIPE_TEX_FILTER_LINEAR
&&
1317 !blit_depth
&& !blit_stencil
&&
1318 src
->texture
->nr_samples
<= 1 &&
1319 (dst_width
!= abs(srcbox
->width
) || dst_height
!= abs(srcbox
->height
))) {
1320 sampler_state
= ctx
->sampler_state_linear
;
1322 sampler_state
= ctx
->sampler_state
;
1326 if (blit_depth
&& blit_stencil
) {
1327 /* Setup two samplers, one for depth and the other one for stencil. */
1328 struct pipe_sampler_view templ
;
1329 struct pipe_sampler_view
*views
[2];
1330 void *samplers
[2] = {sampler_state
, sampler_state
};
1333 templ
.format
= util_format_stencil_only(templ
.format
);
1334 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1337 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1339 pipe
->set_fragment_sampler_views(pipe
, 2, views
);
1340 pipe
->bind_fragment_sampler_states(pipe
, 2, samplers
);
1342 pipe_sampler_view_reference(&views
[1], NULL
);
1343 } else if (blit_stencil
) {
1344 /* Set a stencil-only sampler view for it not to sample depth instead. */
1345 struct pipe_sampler_view templ
;
1346 struct pipe_sampler_view
*view
;
1349 templ
.format
= util_format_stencil_only(templ
.format
);
1350 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1352 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1354 pipe
->set_fragment_sampler_views(pipe
, 1, &view
);
1355 pipe
->bind_fragment_sampler_states(pipe
, 1, &sampler_state
);
1357 pipe_sampler_view_reference(&view
, NULL
);
1359 pipe
->set_fragment_sampler_views(pipe
, 1, &src
);
1360 pipe
->bind_fragment_sampler_states(pipe
, 1, &sampler_state
);
1363 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1364 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1367 pipe
->set_scissor_state(pipe
, scissor
);
1370 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
);
1371 blitter_set_dst_dimensions(ctx
, dst
->width
, dst
->height
);
1373 if ((src_target
== PIPE_TEXTURE_1D
||
1374 src_target
== PIPE_TEXTURE_2D
||
1375 src_target
== PIPE_TEXTURE_RECT
) &&
1376 src
->texture
->nr_samples
<= 1) {
1377 /* Draw the quad with the draw_rectangle callback. */
1379 /* Set texture coordinates. - use a pipe color union
1380 * for interface purposes.
1381 * XXX pipe_color_union is a wrong name since we use that to set
1382 * texture coordinates too.
1384 union pipe_color_union coord
;
1385 get_texcoords(src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1386 srcbox
->x
+srcbox
->width
, srcbox
->y
+srcbox
->height
, coord
.f
);
1389 pipe
->set_sample_mask(pipe
, ~0);
1390 blitter
->draw_rectangle(blitter
, dstx
, dsty
,
1391 dstx
+dst_width
, dsty
+dst_height
, 0,
1392 UTIL_BLITTER_ATTRIB_TEXCOORD
, &coord
);
1394 /* Draw the quad with the generic codepath. */
1395 if (copy_all_samples
&&
1396 src
->texture
->nr_samples
== dst
->texture
->nr_samples
&&
1397 dst
->texture
->nr_samples
> 1) {
1399 unsigned i
, max_sample
= MAX2(dst
->texture
->nr_samples
, 1) - 1;
1401 for (i
= 0; i
<= max_sample
; i
++) {
1402 pipe
->set_sample_mask(pipe
, 1 << i
);
1403 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
, srcbox
->z
,
1404 i
, srcbox
->x
, srcbox
->y
,
1405 srcbox
->x
+ srcbox
->width
,
1406 srcbox
->y
+ srcbox
->height
);
1407 blitter_draw(ctx
, dstx
, dsty
,
1408 dstx
+dst_width
, dsty
+dst_height
, 0);
1411 pipe
->set_sample_mask(pipe
, ~0);
1412 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
, srcbox
->z
, 0,
1413 srcbox
->x
, srcbox
->y
,
1414 srcbox
->x
+ srcbox
->width
,
1415 srcbox
->y
+ srcbox
->height
);
1416 blitter_draw(ctx
, dstx
, dsty
, dstx
+dst_width
, dsty
+dst_height
, 0);
1420 blitter_restore_vertex_states(ctx
);
1421 blitter_restore_fragment_states(ctx
);
1422 blitter_restore_textures(ctx
);
1423 blitter_restore_fb_state(ctx
);
1425 pipe
->set_scissor_state(pipe
, &ctx
->base
.saved_scissor
);
1427 blitter_restore_render_cond(ctx
);
1428 blitter_unset_running_flag(ctx
);
1432 util_blitter_blit(struct blitter_context
*blitter
,
1433 const struct pipe_blit_info
*info
)
1435 struct pipe_resource
*dst
= info
->dst
.resource
;
1436 struct pipe_resource
*src
= info
->src
.resource
;
1437 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1438 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1439 struct pipe_surface
*dst_view
, dst_templ
;
1440 struct pipe_sampler_view src_templ
, *src_view
;
1442 /* Initialize the surface. */
1443 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
1444 info
->dst
.box
.z
, &info
->src
.box
);
1445 dst_templ
.format
= info
->dst
.format
;
1446 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1448 /* Initialize the sampler view. */
1449 util_blitter_default_src_texture(&src_templ
, src
, info
->src
.level
);
1450 src_templ
.format
= info
->src
.format
;
1451 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1454 util_blitter_blit_generic(blitter
, dst_view
,
1455 info
->dst
.box
.x
, info
->dst
.box
.y
,
1456 info
->dst
.box
.width
, info
->dst
.box
.height
,
1457 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
1458 info
->mask
, info
->filter
,
1459 info
->scissor_enable
? &info
->scissor
: NULL
, TRUE
);
1461 pipe_surface_reference(&dst_view
, NULL
);
1462 pipe_sampler_view_reference(&src_view
, NULL
);
1465 /* Clear a region of a color surface to a constant value. */
1466 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
1467 struct pipe_surface
*dstsurf
,
1468 const union pipe_color_union
*color
,
1469 unsigned dstx
, unsigned dsty
,
1470 unsigned width
, unsigned height
)
1472 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1473 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1474 struct pipe_framebuffer_state fb_state
;
1476 assert(dstsurf
->texture
);
1477 if (!dstsurf
->texture
)
1480 /* check the saved state */
1481 blitter_set_running_flag(ctx
);
1482 blitter_check_saved_vertex_states(ctx
);
1483 blitter_check_saved_fragment_states(ctx
);
1484 blitter_check_saved_fb_state(ctx
);
1485 blitter_disable_render_cond(ctx
);
1488 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
]);
1489 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1490 ctx
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, 1, FALSE
));
1491 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1493 /* set a framebuffer state */
1494 fb_state
.width
= dstsurf
->width
;
1495 fb_state
.height
= dstsurf
->height
;
1496 fb_state
.nr_cbufs
= 1;
1497 fb_state
.cbufs
[0] = dstsurf
;
1499 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1500 pipe
->set_sample_mask(pipe
, ~0);
1502 blitter_set_common_draw_rect_state(ctx
, FALSE
);
1503 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1504 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
1505 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1507 blitter_restore_vertex_states(ctx
);
1508 blitter_restore_fragment_states(ctx
);
1509 blitter_restore_fb_state(ctx
);
1510 blitter_restore_render_cond(ctx
);
1511 blitter_unset_running_flag(ctx
);
1514 /* Clear a region of a depth stencil surface. */
1515 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
1516 struct pipe_surface
*dstsurf
,
1517 unsigned clear_flags
,
1520 unsigned dstx
, unsigned dsty
,
1521 unsigned width
, unsigned height
)
1523 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1524 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1525 struct pipe_framebuffer_state fb_state
;
1526 struct pipe_stencil_ref sr
= { { 0 } };
1528 assert(dstsurf
->texture
);
1529 if (!dstsurf
->texture
)
1532 /* check the saved state */
1533 blitter_set_running_flag(ctx
);
1534 blitter_check_saved_vertex_states(ctx
);
1535 blitter_check_saved_fragment_states(ctx
);
1536 blitter_check_saved_fb_state(ctx
);
1537 blitter_disable_render_cond(ctx
);
1540 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1541 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1542 sr
.ref_value
[0] = stencil
& 0xff;
1543 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1544 pipe
->set_stencil_ref(pipe
, &sr
);
1546 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
1547 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1549 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
1550 sr
.ref_value
[0] = stencil
& 0xff;
1551 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1552 pipe
->set_stencil_ref(pipe
, &sr
);
1555 /* hmm that should be illegal probably, or make it a no-op somewhere */
1556 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1558 ctx
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, 0, FALSE
));
1559 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1561 /* set a framebuffer state */
1562 fb_state
.width
= dstsurf
->width
;
1563 fb_state
.height
= dstsurf
->height
;
1564 fb_state
.nr_cbufs
= 0;
1565 fb_state
.cbufs
[0] = 0;
1566 fb_state
.zsbuf
= dstsurf
;
1567 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1568 pipe
->set_sample_mask(pipe
, ~0);
1570 blitter_set_common_draw_rect_state(ctx
, FALSE
);
1571 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1572 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, depth
,
1573 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
1575 blitter_restore_vertex_states(ctx
);
1576 blitter_restore_fragment_states(ctx
);
1577 blitter_restore_fb_state(ctx
);
1578 blitter_restore_render_cond(ctx
);
1579 blitter_unset_running_flag(ctx
);
1582 /* draw a rectangle across a region using a custom dsa stage - for r600g */
1583 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
1584 struct pipe_surface
*zsurf
,
1585 struct pipe_surface
*cbsurf
,
1586 unsigned sample_mask
,
1587 void *dsa_stage
, float depth
)
1589 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1590 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1591 struct pipe_framebuffer_state fb_state
;
1593 assert(zsurf
->texture
);
1594 if (!zsurf
->texture
)
1597 /* check the saved state */
1598 blitter_set_running_flag(ctx
);
1599 blitter_check_saved_vertex_states(ctx
);
1600 blitter_check_saved_fragment_states(ctx
);
1601 blitter_check_saved_fb_state(ctx
);
1602 blitter_disable_render_cond(ctx
);
1605 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
]);
1606 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
1607 ctx
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, 0, FALSE
));
1608 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1610 /* set a framebuffer state */
1611 fb_state
.width
= zsurf
->width
;
1612 fb_state
.height
= zsurf
->height
;
1613 fb_state
.nr_cbufs
= 1;
1615 fb_state
.cbufs
[0] = cbsurf
;
1616 fb_state
.nr_cbufs
= 1;
1618 fb_state
.cbufs
[0] = NULL
;
1619 fb_state
.nr_cbufs
= 0;
1621 fb_state
.zsbuf
= zsurf
;
1622 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1623 pipe
->set_sample_mask(pipe
, sample_mask
);
1625 blitter_set_common_draw_rect_state(ctx
, FALSE
);
1626 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
1627 blitter
->draw_rectangle(blitter
, 0, 0, zsurf
->width
, zsurf
->height
, depth
,
1628 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
1630 blitter_restore_vertex_states(ctx
);
1631 blitter_restore_fragment_states(ctx
);
1632 blitter_restore_fb_state(ctx
);
1633 blitter_restore_render_cond(ctx
);
1634 blitter_unset_running_flag(ctx
);
1637 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
1638 struct pipe_resource
*dst
,
1640 struct pipe_resource
*src
,
1644 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1645 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1646 struct pipe_vertex_buffer vb
;
1647 struct pipe_stream_output_target
*so_target
;
1649 if (srcx
>= src
->width0
||
1650 dstx
>= dst
->width0
) {
1653 if (srcx
+ size
> src
->width0
) {
1654 size
= src
->width0
- srcx
;
1656 if (dstx
+ size
> dst
->width0
) {
1657 size
= dst
->width0
- dstx
;
1660 /* Drivers not capable of Stream Out should not call this function
1661 * in the first place. */
1662 assert(ctx
->has_stream_out
);
1664 /* Some alignment is required. */
1665 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
1666 !ctx
->has_stream_out
) {
1667 struct pipe_box box
;
1668 u_box_1d(srcx
, size
, &box
);
1669 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
1673 blitter_set_running_flag(ctx
);
1674 blitter_check_saved_vertex_states(ctx
);
1675 blitter_disable_render_cond(ctx
);
1678 vb
.buffer_offset
= srcx
;
1681 pipe
->set_vertex_buffers(pipe
, 1, &vb
);
1682 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
);
1683 pipe
->bind_vs_state(pipe
, ctx
->vs_pos_only
);
1684 if (ctx
->has_geometry_shader
)
1685 pipe
->bind_gs_state(pipe
, NULL
);
1686 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
1688 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
1689 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, 0);
1691 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
1693 blitter_restore_vertex_states(ctx
);
1694 blitter_restore_render_cond(ctx
);
1695 blitter_unset_running_flag(ctx
);
1696 pipe_so_target_reference(&so_target
, NULL
);
1699 /* probably radeon specific */
1700 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
1701 struct pipe_resource
*dst
,
1704 struct pipe_resource
*src
,
1706 unsigned sample_mask
,
1708 enum pipe_format format
)
1710 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1711 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1712 struct pipe_framebuffer_state fb_state
;
1713 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
1715 blitter_set_running_flag(ctx
);
1716 blitter_check_saved_vertex_states(ctx
);
1717 blitter_check_saved_fragment_states(ctx
);
1718 blitter_disable_render_cond(ctx
);
1721 pipe
->bind_blend_state(pipe
, custom_blend
);
1722 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1723 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1724 ctx
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, 1, FALSE
));
1725 pipe
->set_sample_mask(pipe
, sample_mask
);
1727 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1728 surf_tmpl
.format
= format
;
1729 surf_tmpl
.u
.tex
.level
= dst_level
;
1730 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
1731 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
1732 surf_tmpl
.usage
= PIPE_BIND_RENDER_TARGET
;
1734 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
1736 surf_tmpl
.u
.tex
.level
= 0;
1737 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
1738 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
1740 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
1742 /* set a framebuffer state */
1743 fb_state
.width
= src
->width0
;
1744 fb_state
.height
= src
->height0
;
1745 fb_state
.nr_cbufs
= 2;
1746 fb_state
.cbufs
[0] = srcsurf
;
1747 fb_state
.cbufs
[1] = dstsurf
;
1748 fb_state
.zsbuf
= NULL
;
1749 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1751 blitter_set_common_draw_rect_state(ctx
, FALSE
);
1752 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
1753 blitter
->draw_rectangle(blitter
, 0, 0, src
->width0
, src
->height0
,
1755 blitter_restore_fb_state(ctx
);
1756 blitter_restore_vertex_states(ctx
);
1757 blitter_restore_fragment_states(ctx
);
1758 blitter_restore_render_cond(ctx
);
1759 blitter_unset_running_flag(ctx
);
1761 pipe_surface_reference(&srcsurf
, NULL
);
1762 pipe_surface_reference(&dstsurf
, NULL
);
1765 void util_blitter_custom_color(struct blitter_context
*blitter
,
1766 struct pipe_surface
*dstsurf
,
1769 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1770 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1771 struct pipe_framebuffer_state fb_state
;
1773 assert(dstsurf
->texture
);
1774 if (!dstsurf
->texture
)
1777 /* check the saved state */
1778 blitter_set_running_flag(ctx
);
1779 blitter_check_saved_vertex_states(ctx
);
1780 blitter_check_saved_fragment_states(ctx
);
1781 blitter_check_saved_fb_state(ctx
);
1782 blitter_disable_render_cond(ctx
);
1785 pipe
->bind_blend_state(pipe
, custom_blend
);
1786 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1787 ctx
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, 1, FALSE
));
1788 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1789 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
1791 /* set a framebuffer state */
1792 fb_state
.width
= dstsurf
->width
;
1793 fb_state
.height
= dstsurf
->height
;
1794 fb_state
.nr_cbufs
= 1;
1795 fb_state
.cbufs
[0] = dstsurf
;
1797 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1798 pipe
->set_sample_mask(pipe
, ~0);
1800 blitter_set_common_draw_rect_state(ctx
, FALSE
);
1801 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1802 blitter
->draw_rectangle(blitter
, 0, 0, dstsurf
->width
, dstsurf
->height
,
1805 blitter_restore_vertex_states(ctx
);
1806 blitter_restore_fragment_states(ctx
);
1807 blitter_restore_fb_state(ctx
);
1808 blitter_restore_render_cond(ctx
);
1809 blitter_unset_running_flag(ctx
);
1812 /* Return whether this is an RGBA, Z, S, or combined ZS format.
1814 static unsigned get_format_mask(enum pipe_format format
)
1816 const struct util_format_description
*desc
= util_format_description(format
);
1820 if (util_format_has_depth(desc
)) {
1821 if (util_format_has_stencil(desc
)) {
1822 return PIPE_MASK_ZS
;
1827 if (util_format_has_stencil(desc
)) {
1830 return PIPE_MASK_RGBA
;
1835 /* Return if the box is totally inside the resource.
1837 static boolean
is_box_inside_resource(const struct pipe_resource
*res
,
1838 const struct pipe_box
*box
,
1841 unsigned width
= 1, height
= 1, depth
= 1;
1843 switch (res
->target
) {
1845 width
= res
->width0
;
1849 case PIPE_TEXTURE_1D
:
1850 width
= u_minify(res
->width0
, level
);
1854 case PIPE_TEXTURE_2D
:
1855 case PIPE_TEXTURE_RECT
:
1856 width
= u_minify(res
->width0
, level
);
1857 height
= u_minify(res
->height0
, level
);
1860 case PIPE_TEXTURE_3D
:
1861 width
= u_minify(res
->width0
, level
);
1862 height
= u_minify(res
->height0
, level
);
1863 depth
= u_minify(res
->depth0
, level
);
1865 case PIPE_TEXTURE_CUBE
:
1866 width
= u_minify(res
->width0
, level
);
1867 height
= u_minify(res
->height0
, level
);
1870 case PIPE_TEXTURE_1D_ARRAY
:
1871 width
= u_minify(res
->width0
, level
);
1873 depth
= res
->array_size
;
1875 case PIPE_TEXTURE_2D_ARRAY
:
1876 width
= u_minify(res
->width0
, level
);
1877 height
= u_minify(res
->height0
, level
);
1878 depth
= res
->array_size
;
1880 case PIPE_MAX_TEXTURE_TYPES
:;
1883 return box
->x
>= 0 &&
1884 box
->x
+ box
->width
<= width
&&
1886 box
->y
+ box
->height
<= height
&&
1888 box
->z
+ box
->depth
<= depth
;
1891 static unsigned get_sample_count(const struct pipe_resource
*res
)
1893 return res
->nr_samples
? res
->nr_samples
: 1;
1896 boolean
util_try_blit_via_copy_region(struct pipe_context
*ctx
,
1897 const struct pipe_blit_info
*blit
)
1899 unsigned mask
= get_format_mask(blit
->dst
.format
);
1901 /* No format conversions. */
1902 if (blit
->src
.resource
->format
!= blit
->src
.format
||
1903 blit
->dst
.resource
->format
!= blit
->dst
.format
||
1904 !util_is_format_compatible(
1905 util_format_description(blit
->src
.resource
->format
),
1906 util_format_description(blit
->dst
.resource
->format
))) {
1910 /* No masks, no filtering, no scissor. */
1911 if ((blit
->mask
& mask
) != mask
||
1912 blit
->filter
!= PIPE_TEX_FILTER_NEAREST
||
1913 blit
->scissor_enable
) {
1918 if (blit
->src
.box
.width
< 0 ||
1919 blit
->src
.box
.height
< 0 ||
1920 blit
->src
.box
.depth
< 0) {
1925 if (blit
->src
.box
.width
!= blit
->dst
.box
.width
||
1926 blit
->src
.box
.height
!= blit
->dst
.box
.height
||
1927 blit
->src
.box
.depth
!= blit
->dst
.box
.depth
) {
1931 /* No out-of-bounds access. */
1932 if (!is_box_inside_resource(blit
->src
.resource
, &blit
->src
.box
,
1934 !is_box_inside_resource(blit
->dst
.resource
, &blit
->dst
.box
,
1939 /* Sample counts must match. */
1940 if (get_sample_count(blit
->src
.resource
) !=
1941 get_sample_count(blit
->dst
.resource
)) {
1945 ctx
->resource_copy_region(ctx
, blit
->dst
.resource
, blit
->dst
.level
,
1946 blit
->dst
.box
.x
, blit
->dst
.box
.y
, blit
->dst
.box
.z
,
1947 blit
->src
.resource
, blit
->src
.level
,