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. */
71 void *fs_write_one_cbuf
;
72 void *fs_write_all_cbufs
;
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_state_readbuf
[4]; /**< X, XY, XYZ, XYZW */
105 void *sampler_state_linear
;
106 void *sampler_state_rect
;
107 void *sampler_state_rect_linear
;
109 /* Rasterizer state. */
110 void *rs_state
, *rs_state_scissor
, *rs_discard_state
;
112 /* Viewport state. */
113 struct pipe_viewport_state viewport
;
115 /* Destination surface dimensions. */
119 boolean has_geometry_shader
;
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 static struct pipe_surface
*
131 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
132 struct pipe_surface
*surf
);
134 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
)
136 struct blitter_context_priv
*ctx
;
137 struct pipe_blend_state blend
;
138 struct pipe_depth_stencil_alpha_state dsa
;
139 struct pipe_rasterizer_state rs_state
;
140 struct pipe_sampler_state sampler_state
;
141 struct pipe_vertex_element velem
[2];
144 ctx
= CALLOC_STRUCT(blitter_context_priv
);
148 ctx
->base
.pipe
= pipe
;
149 ctx
->base
.draw_rectangle
= util_blitter_draw_rectangle
;
150 ctx
->base
.get_next_surface_layer
= util_blitter_get_next_surface_layer
;
152 ctx
->bind_fs_state
= pipe
->bind_fs_state
;
153 ctx
->delete_fs_state
= pipe
->delete_fs_state
;
155 /* init state objects for them to be considered invalid */
156 ctx
->base
.saved_blend_state
= INVALID_PTR
;
157 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
158 ctx
->base
.saved_rs_state
= INVALID_PTR
;
159 ctx
->base
.saved_fs
= INVALID_PTR
;
160 ctx
->base
.saved_vs
= INVALID_PTR
;
161 ctx
->base
.saved_gs
= INVALID_PTR
;
162 ctx
->base
.saved_velem_state
= INVALID_PTR
;
163 ctx
->base
.saved_fb_state
.nr_cbufs
= ~0;
164 ctx
->base
.saved_num_sampler_views
= ~0;
165 ctx
->base
.saved_num_sampler_states
= ~0;
166 ctx
->base
.saved_num_so_targets
= ~0;
168 ctx
->has_geometry_shader
=
169 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
170 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
171 ctx
->has_stream_out
=
172 pipe
->screen
->get_param(pipe
->screen
,
173 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0;
175 ctx
->has_stencil_export
=
176 pipe
->screen
->get_param(pipe
->screen
,
177 PIPE_CAP_SHADER_STENCIL_EXPORT
);
179 ctx
->has_texture_multisample
=
180 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TEXTURE_MULTISAMPLE
);
182 /* blend state objects */
183 memset(&blend
, 0, sizeof(blend
));
185 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
186 blend
.rt
[0].colormask
= i
;
187 ctx
->blend
[i
] = pipe
->create_blend_state(pipe
, &blend
);
190 /* depth stencil alpha state objects */
191 memset(&dsa
, 0, sizeof(dsa
));
192 ctx
->dsa_keep_depth_stencil
=
193 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
195 dsa
.depth
.enabled
= 1;
196 dsa
.depth
.writemask
= 1;
197 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
198 ctx
->dsa_write_depth_keep_stencil
=
199 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
201 dsa
.stencil
[0].enabled
= 1;
202 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
203 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
204 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
205 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
206 dsa
.stencil
[0].valuemask
= 0xff;
207 dsa
.stencil
[0].writemask
= 0xff;
208 ctx
->dsa_write_depth_stencil
=
209 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
211 dsa
.depth
.enabled
= 0;
212 dsa
.depth
.writemask
= 0;
213 ctx
->dsa_keep_depth_write_stencil
=
214 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
217 memset(&sampler_state
, 0, sizeof(sampler_state
));
218 sampler_state
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
219 sampler_state
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
220 sampler_state
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
221 sampler_state
.normalized_coords
= 1;
222 ctx
->sampler_state
= pipe
->create_sampler_state(pipe
, &sampler_state
);
223 sampler_state
.normalized_coords
= 0;
224 ctx
->sampler_state_rect
= pipe
->create_sampler_state(pipe
, &sampler_state
);
226 sampler_state
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
227 sampler_state
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
228 sampler_state
.normalized_coords
= 1;
229 ctx
->sampler_state_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
230 sampler_state
.normalized_coords
= 0;
231 ctx
->sampler_state_rect_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
233 /* rasterizer state */
234 memset(&rs_state
, 0, sizeof(rs_state
));
235 rs_state
.cull_face
= PIPE_FACE_NONE
;
236 rs_state
.half_pixel_center
= 1;
237 rs_state
.bottom_edge_rule
= 1;
238 rs_state
.flatshade
= 1;
239 rs_state
.depth_clip
= 1;
240 ctx
->rs_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
242 rs_state
.scissor
= 1;
243 ctx
->rs_state_scissor
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
245 if (ctx
->has_stream_out
) {
246 rs_state
.scissor
= 0;
247 rs_state
.rasterizer_discard
= 1;
248 ctx
->rs_discard_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
251 ctx
->base
.vb_slot
= 0; /* 0 for now */
253 /* vertex elements states */
254 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
255 for (i
= 0; i
< 2; i
++) {
256 velem
[i
].src_offset
= i
* 4 * sizeof(float);
257 velem
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
258 velem
[i
].vertex_buffer_index
= ctx
->base
.vb_slot
;
260 ctx
->velem_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
262 if (ctx
->has_stream_out
) {
263 static enum pipe_format formats
[4] = {
264 PIPE_FORMAT_R32_UINT
,
265 PIPE_FORMAT_R32G32_UINT
,
266 PIPE_FORMAT_R32G32B32_UINT
,
267 PIPE_FORMAT_R32G32B32A32_UINT
270 for (i
= 0; i
< 4; i
++) {
271 velem
[0].src_format
= formats
[i
];
272 velem
[0].vertex_buffer_index
= ctx
->base
.vb_slot
;
273 ctx
->velem_state_readbuf
[i
] =
274 pipe
->create_vertex_elements_state(pipe
, 1, &velem
[0]);
278 /* Fragment shaders are created on-demand, except these.
279 * The interpolation must be constant for integer texture clearing to work.
281 ctx
->fs_empty
= util_make_empty_fragment_shader(pipe
);
282 ctx
->fs_write_one_cbuf
=
283 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
284 TGSI_INTERPOLATE_CONSTANT
, FALSE
);
285 ctx
->fs_write_all_cbufs
=
286 util_make_fragment_passthrough_shader(pipe
, TGSI_SEMANTIC_GENERIC
,
287 TGSI_INTERPOLATE_CONSTANT
, TRUE
);
291 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
292 TGSI_SEMANTIC_GENERIC
};
293 const uint semantic_indices
[] = { 0, 0 };
295 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
298 if (ctx
->has_stream_out
) {
299 struct pipe_stream_output_info so
;
300 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
};
301 const uint semantic_indices
[] = { 0 };
303 memset(&so
, 0, sizeof(so
));
305 so
.output
[0].num_components
= 1;
309 util_make_vertex_passthrough_shader_with_so(pipe
, 1, semantic_names
,
310 semantic_indices
, &so
);
313 /* set invariant vertex coordinates */
314 for (i
= 0; i
< 4; i
++)
315 ctx
->vertices
[i
][0][3] = 1; /*v.w*/
317 ctx
->upload
= u_upload_create(pipe
, 65536, 4, PIPE_BIND_VERTEX_BUFFER
);
322 void util_blitter_destroy(struct blitter_context
*blitter
)
324 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
325 struct pipe_context
*pipe
= blitter
->pipe
;
328 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
329 pipe
->delete_blend_state(pipe
, ctx
->blend
[i
]);
331 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
332 pipe
->delete_depth_stencil_alpha_state(pipe
,
333 ctx
->dsa_write_depth_keep_stencil
);
334 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
335 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
337 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
);
338 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state_scissor
);
339 if (ctx
->rs_discard_state
)
340 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_discard_state
);
341 pipe
->delete_vs_state(pipe
, ctx
->vs
);
342 if (ctx
->vs_pos_only
)
343 pipe
->delete_vs_state(pipe
, ctx
->vs_pos_only
);
344 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state
);
345 for (i
= 0; i
< 4; i
++) {
346 if (ctx
->velem_state_readbuf
[i
]) {
347 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[i
]);
351 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
352 if (ctx
->fs_texfetch_col
[i
])
353 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[i
]);
354 if (ctx
->fs_texfetch_depth
[i
])
355 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
]);
356 if (ctx
->fs_texfetch_depthstencil
[i
])
357 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil
[i
]);
358 if (ctx
->fs_texfetch_stencil
[i
])
359 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil
[i
]);
361 ctx
->delete_fs_state(pipe
, ctx
->fs_empty
);
362 ctx
->delete_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
363 ctx
->delete_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
365 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect_linear
);
366 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect
);
367 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_linear
);
368 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
);
369 u_upload_destroy(ctx
->upload
);
373 void util_blitter_set_texture_multisample(struct blitter_context
*blitter
,
376 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
378 ctx
->has_texture_multisample
= supported
;
381 static void blitter_set_running_flag(struct blitter_context_priv
*ctx
)
383 if (ctx
->base
.running
) {
384 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
387 ctx
->base
.running
= TRUE
;
390 static void blitter_unset_running_flag(struct blitter_context_priv
*ctx
)
392 if (!ctx
->base
.running
) {
393 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
396 ctx
->base
.running
= FALSE
;
399 static void blitter_check_saved_vertex_states(struct blitter_context_priv
*ctx
)
401 assert(ctx
->base
.saved_velem_state
!= INVALID_PTR
);
402 assert(ctx
->base
.saved_vs
!= INVALID_PTR
);
403 assert(!ctx
->has_geometry_shader
|| ctx
->base
.saved_gs
!= INVALID_PTR
);
404 assert(!ctx
->has_stream_out
|| ctx
->base
.saved_num_so_targets
!= ~0);
405 assert(ctx
->base
.saved_rs_state
!= INVALID_PTR
);
408 static void blitter_restore_vertex_states(struct blitter_context_priv
*ctx
)
410 struct pipe_context
*pipe
= ctx
->base
.pipe
;
414 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1,
415 &ctx
->base
.saved_vertex_buffer
);
416 pipe_resource_reference(&ctx
->base
.saved_vertex_buffer
.buffer
, NULL
);
418 /* Vertex elements. */
419 pipe
->bind_vertex_elements_state(pipe
, ctx
->base
.saved_velem_state
);
420 ctx
->base
.saved_velem_state
= INVALID_PTR
;
423 pipe
->bind_vs_state(pipe
, ctx
->base
.saved_vs
);
424 ctx
->base
.saved_vs
= INVALID_PTR
;
426 /* Geometry shader. */
427 if (ctx
->has_geometry_shader
) {
428 pipe
->bind_gs_state(pipe
, ctx
->base
.saved_gs
);
429 ctx
->base
.saved_gs
= INVALID_PTR
;
432 /* Stream outputs. */
433 if (ctx
->has_stream_out
) {
434 pipe
->set_stream_output_targets(pipe
,
435 ctx
->base
.saved_num_so_targets
,
436 ctx
->base
.saved_so_targets
, ~0);
438 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
439 pipe_so_target_reference(&ctx
->base
.saved_so_targets
[i
], NULL
);
441 ctx
->base
.saved_num_so_targets
= ~0;
445 pipe
->bind_rasterizer_state(pipe
, ctx
->base
.saved_rs_state
);
446 ctx
->base
.saved_rs_state
= INVALID_PTR
;
449 static void blitter_check_saved_fragment_states(struct blitter_context_priv
*ctx
)
451 assert(ctx
->base
.saved_fs
!= INVALID_PTR
);
452 assert(ctx
->base
.saved_dsa_state
!= INVALID_PTR
);
453 assert(ctx
->base
.saved_blend_state
!= INVALID_PTR
);
456 static void blitter_restore_fragment_states(struct blitter_context_priv
*ctx
)
458 struct pipe_context
*pipe
= ctx
->base
.pipe
;
460 /* Fragment shader. */
461 ctx
->bind_fs_state(pipe
, ctx
->base
.saved_fs
);
462 ctx
->base
.saved_fs
= INVALID_PTR
;
464 /* Depth, stencil, alpha. */
465 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->base
.saved_dsa_state
);
466 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
469 pipe
->bind_blend_state(pipe
, ctx
->base
.saved_blend_state
);
470 ctx
->base
.saved_blend_state
= INVALID_PTR
;
473 if (ctx
->base
.is_sample_mask_saved
) {
474 pipe
->set_sample_mask(pipe
, ctx
->base
.saved_sample_mask
);
475 ctx
->base
.is_sample_mask_saved
= FALSE
;
478 /* Miscellaneous states. */
479 /* XXX check whether these are saved and whether they need to be restored
480 * (depending on the operation) */
481 pipe
->set_stencil_ref(pipe
, &ctx
->base
.saved_stencil_ref
);
482 pipe
->set_viewport_states(pipe
, 0, 1, &ctx
->base
.saved_viewport
);
485 static void blitter_check_saved_fb_state(struct blitter_context_priv
*ctx
)
487 assert(ctx
->base
.saved_fb_state
.nr_cbufs
!= ~0);
490 static void blitter_disable_render_cond(struct blitter_context_priv
*ctx
)
492 struct pipe_context
*pipe
= ctx
->base
.pipe
;
494 if (ctx
->base
.saved_render_cond_query
) {
495 pipe
->render_condition(pipe
, NULL
, FALSE
, 0);
499 static void blitter_restore_render_cond(struct blitter_context_priv
*ctx
)
501 struct pipe_context
*pipe
= ctx
->base
.pipe
;
503 if (ctx
->base
.saved_render_cond_query
) {
504 pipe
->render_condition(pipe
, ctx
->base
.saved_render_cond_query
,
505 ctx
->base
.saved_render_cond_cond
,
506 ctx
->base
.saved_render_cond_mode
);
507 ctx
->base
.saved_render_cond_query
= NULL
;
511 static void blitter_restore_fb_state(struct blitter_context_priv
*ctx
)
513 struct pipe_context
*pipe
= ctx
->base
.pipe
;
515 pipe
->set_framebuffer_state(pipe
, &ctx
->base
.saved_fb_state
);
516 util_unreference_framebuffer_state(&ctx
->base
.saved_fb_state
);
519 static void blitter_check_saved_textures(struct blitter_context_priv
*ctx
)
521 assert(ctx
->base
.saved_num_sampler_states
!= ~0);
522 assert(ctx
->base
.saved_num_sampler_views
!= ~0);
525 static void blitter_restore_textures(struct blitter_context_priv
*ctx
)
527 struct pipe_context
*pipe
= ctx
->base
.pipe
;
530 /* Fragment sampler states. */
531 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0,
532 ctx
->base
.saved_num_sampler_states
,
533 ctx
->base
.saved_sampler_states
);
535 ctx
->base
.saved_num_sampler_states
= ~0;
537 /* Fragment sampler views. */
538 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0,
539 ctx
->base
.saved_num_sampler_views
,
540 ctx
->base
.saved_sampler_views
);
542 for (i
= 0; i
< ctx
->base
.saved_num_sampler_views
; i
++)
543 pipe_sampler_view_reference(&ctx
->base
.saved_sampler_views
[i
], NULL
);
545 ctx
->base
.saved_num_sampler_views
= ~0;
548 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
549 int x1
, int y1
, int x2
, int y2
,
554 /* set vertex positions */
555 ctx
->vertices
[0][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v0.x*/
556 ctx
->vertices
[0][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v0.y*/
558 ctx
->vertices
[1][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v1.x*/
559 ctx
->vertices
[1][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v1.y*/
561 ctx
->vertices
[2][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v2.x*/
562 ctx
->vertices
[2][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v2.y*/
564 ctx
->vertices
[3][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v3.x*/
565 ctx
->vertices
[3][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v3.y*/
567 for (i
= 0; i
< 4; i
++)
568 ctx
->vertices
[i
][0][2] = depth
; /*z*/
571 ctx
->viewport
.scale
[0] = 0.5f
* ctx
->dst_width
;
572 ctx
->viewport
.scale
[1] = 0.5f
* ctx
->dst_height
;
573 ctx
->viewport
.scale
[2] = 1.0f
;
574 ctx
->viewport
.scale
[3] = 1.0f
;
575 ctx
->viewport
.translate
[0] = 0.5f
* ctx
->dst_width
;
576 ctx
->viewport
.translate
[1] = 0.5f
* ctx
->dst_height
;
577 ctx
->viewport
.translate
[2] = 0.0f
;
578 ctx
->viewport
.translate
[3] = 0.0f
;
579 ctx
->base
.pipe
->set_viewport_states(ctx
->base
.pipe
, 0, 1, &ctx
->viewport
);
582 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
583 const union pipe_color_union
*color
)
588 for (i
= 0; i
< 4; i
++) {
589 uint32_t *uiverts
= (uint32_t *)ctx
->vertices
[i
][1];
590 uiverts
[0] = color
->ui
[0];
591 uiverts
[1] = color
->ui
[1];
592 uiverts
[2] = color
->ui
[2];
593 uiverts
[3] = color
->ui
[3];
596 for (i
= 0; i
< 4; i
++) {
597 ctx
->vertices
[i
][1][0] = 0;
598 ctx
->vertices
[i
][1][1] = 0;
599 ctx
->vertices
[i
][1][2] = 0;
600 ctx
->vertices
[i
][1][3] = 0;
605 static void get_texcoords(struct pipe_sampler_view
*src
,
606 unsigned src_width0
, unsigned src_height0
,
607 int x1
, int y1
, int x2
, int y2
,
610 struct pipe_resource
*tex
= src
->texture
;
611 unsigned level
= src
->u
.tex
.first_level
;
612 boolean normalized
= tex
->target
!= PIPE_TEXTURE_RECT
&&
613 tex
->nr_samples
<= 1;
616 out
[0] = x1
/ (float)u_minify(src_width0
, level
);
617 out
[1] = y1
/ (float)u_minify(src_height0
, level
);
618 out
[2] = x2
/ (float)u_minify(src_width0
, level
);
619 out
[3] = y2
/ (float)u_minify(src_height0
, level
);
628 static void set_texcoords_in_vertices(const float coord
[4],
629 float *out
, unsigned stride
)
631 out
[0] = coord
[0]; /*t0.s*/
632 out
[1] = coord
[1]; /*t0.t*/
634 out
[0] = coord
[2]; /*t1.s*/
635 out
[1] = coord
[1]; /*t1.t*/
637 out
[0] = coord
[2]; /*t2.s*/
638 out
[1] = coord
[3]; /*t2.t*/
640 out
[0] = coord
[0]; /*t3.s*/
641 out
[1] = coord
[3]; /*t3.t*/
644 static void blitter_set_texcoords(struct blitter_context_priv
*ctx
,
645 struct pipe_sampler_view
*src
,
646 unsigned src_width0
, unsigned src_height0
,
647 unsigned layer
, unsigned sample
,
648 int x1
, int y1
, int x2
, int y2
)
652 float face_coord
[4][2];
654 get_texcoords(src
, src_width0
, src_height0
, x1
, y1
, x2
, y2
, coord
);
656 if (src
->texture
->target
== PIPE_TEXTURE_CUBE
||
657 src
->texture
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
658 set_texcoords_in_vertices(coord
, &face_coord
[0][0], 2);
659 util_map_texcoords2d_onto_cubemap(layer
% 6,
660 /* pointer, stride in floats */
661 &face_coord
[0][0], 2,
662 &ctx
->vertices
[0][1][0], 8);
664 set_texcoords_in_vertices(coord
, &ctx
->vertices
[0][1][0], 8);
668 switch (src
->texture
->target
) {
669 case PIPE_TEXTURE_3D
:
671 float r
= layer
/ (float)u_minify(src
->texture
->depth0
,
672 src
->u
.tex
.first_level
);
673 for (i
= 0; i
< 4; i
++)
674 ctx
->vertices
[i
][1][2] = r
; /*r*/
678 case PIPE_TEXTURE_1D_ARRAY
:
679 for (i
= 0; i
< 4; i
++)
680 ctx
->vertices
[i
][1][1] = (float) layer
; /*t*/
683 case PIPE_TEXTURE_2D_ARRAY
:
684 for (i
= 0; i
< 4; i
++) {
685 ctx
->vertices
[i
][1][2] = (float) layer
; /*r*/
686 ctx
->vertices
[i
][1][3] = (float) sample
; /*q*/
690 case PIPE_TEXTURE_CUBE_ARRAY
:
691 for (i
= 0; i
< 4; i
++)
692 ctx
->vertices
[i
][1][3] = (float) (layer
/ 6); /*w*/
695 case PIPE_TEXTURE_2D
:
696 for (i
= 0; i
< 4; i
++) {
697 ctx
->vertices
[i
][1][3] = (float) sample
; /*r*/
705 static void blitter_set_dst_dimensions(struct blitter_context_priv
*ctx
,
706 unsigned width
, unsigned height
)
708 ctx
->dst_width
= width
;
709 ctx
->dst_height
= height
;
712 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
713 enum pipe_texture_target target
,
716 struct pipe_context
*pipe
= ctx
->base
.pipe
;
718 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
720 if (nr_samples
> 1) {
721 void **shader
= &ctx
->fs_texfetch_col_msaa
[target
];
723 /* Create the fragment shader on-demand. */
725 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
,
728 *shader
= util_make_fs_blit_msaa_color(pipe
, tgsi_tex
);
733 void **shader
= &ctx
->fs_texfetch_col
[target
];
735 /* Create the fragment shader on-demand. */
737 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
740 util_make_fragment_tex_shader(pipe
, tgsi_tex
,
741 TGSI_INTERPOLATE_LINEAR
);
749 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
750 enum pipe_texture_target target
,
753 struct pipe_context
*pipe
= ctx
->base
.pipe
;
755 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
757 if (nr_samples
> 1) {
758 void **shader
= &ctx
->fs_texfetch_depth_msaa
[target
];
760 /* Create the fragment shader on-demand. */
762 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
,
766 util_make_fs_blit_msaa_depth(pipe
, tgsi_tex
);
771 void **shader
= &ctx
->fs_texfetch_depth
[target
];
773 /* Create the fragment shader on-demand. */
775 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
778 util_make_fragment_tex_shader_writedepth(pipe
, tgsi_tex
,
779 TGSI_INTERPOLATE_LINEAR
);
787 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv
*ctx
,
788 enum pipe_texture_target target
,
791 struct pipe_context
*pipe
= ctx
->base
.pipe
;
793 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
795 if (nr_samples
> 1) {
796 void **shader
= &ctx
->fs_texfetch_depthstencil_msaa
[target
];
798 /* Create the fragment shader on-demand. */
800 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
,
804 util_make_fs_blit_msaa_depthstencil(pipe
, tgsi_tex
);
809 void **shader
= &ctx
->fs_texfetch_depthstencil
[target
];
811 /* Create the fragment shader on-demand. */
813 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
816 util_make_fragment_tex_shader_writedepthstencil(pipe
, tgsi_tex
,
817 TGSI_INTERPOLATE_LINEAR
);
825 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv
*ctx
,
826 enum pipe_texture_target target
,
829 struct pipe_context
*pipe
= ctx
->base
.pipe
;
831 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
833 if (nr_samples
> 1) {
834 void **shader
= &ctx
->fs_texfetch_stencil_msaa
[target
];
836 /* Create the fragment shader on-demand. */
838 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
,
842 util_make_fs_blit_msaa_stencil(pipe
, tgsi_tex
);
847 void **shader
= &ctx
->fs_texfetch_stencil
[target
];
849 /* Create the fragment shader on-demand. */
851 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
854 util_make_fragment_tex_shader_writestencil(pipe
, tgsi_tex
,
855 TGSI_INTERPOLATE_LINEAR
);
862 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
)
864 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
865 struct pipe_screen
*screen
= blitter
->pipe
->screen
;
866 unsigned i
, target
, max_samples
;
867 boolean has_arraytex
, has_cubearraytex
;
869 max_samples
= ctx
->has_texture_multisample
? 2 : 1;
870 has_arraytex
= screen
->get_param(screen
,
871 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
) != 0;
872 has_cubearraytex
= screen
->get_param(screen
,
873 PIPE_CAP_CUBE_MAP_ARRAY
) != 0;
875 /* It only matters if i <= 1 or > 1. */
876 for (i
= 1; i
<= max_samples
; i
++) {
877 for (target
= PIPE_TEXTURE_1D
; target
< PIPE_MAX_TEXTURE_TYPES
; target
++) {
879 (target
== PIPE_TEXTURE_1D_ARRAY
||
880 target
== PIPE_TEXTURE_2D_ARRAY
)) {
883 if (!has_cubearraytex
&&
884 (target
== PIPE_TEXTURE_CUBE_ARRAY
))
888 (target
!= PIPE_TEXTURE_2D
&&
889 target
!= PIPE_TEXTURE_2D_ARRAY
))
892 blitter_get_fs_texfetch_col(ctx
, target
, i
);
893 blitter_get_fs_texfetch_depth(ctx
, target
, i
);
894 if (ctx
->has_stencil_export
) {
895 blitter_get_fs_texfetch_depthstencil(ctx
, target
, i
);
896 blitter_get_fs_texfetch_stencil(ctx
, target
, i
);
902 static void blitter_set_common_draw_rect_state(struct blitter_context_priv
*ctx
,
905 struct pipe_context
*pipe
= ctx
->base
.pipe
;
907 pipe
->bind_rasterizer_state(pipe
, scissor
? ctx
->rs_state_scissor
909 pipe
->bind_vs_state(pipe
, ctx
->vs
);
910 if (ctx
->has_geometry_shader
)
911 pipe
->bind_gs_state(pipe
, NULL
);
912 if (ctx
->has_stream_out
)
913 pipe
->set_stream_output_targets(pipe
, 0, NULL
, 0);
916 static void blitter_draw(struct blitter_context_priv
*ctx
,
917 int x1
, int y1
, int x2
, int y2
, float depth
)
919 struct pipe_resource
*buf
= NULL
;
922 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
924 u_upload_data(ctx
->upload
, 0, sizeof(ctx
->vertices
), ctx
->vertices
,
926 u_upload_unmap(ctx
->upload
);
927 util_draw_vertex_buffer(ctx
->base
.pipe
, NULL
, buf
, ctx
->base
.vb_slot
,
928 offset
, PIPE_PRIM_TRIANGLE_FAN
, 4, 2);
929 pipe_resource_reference(&buf
, NULL
);
932 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
933 int x1
, int y1
, int x2
, int y2
, float depth
,
934 enum blitter_attrib_type type
,
935 const union pipe_color_union
*attrib
)
937 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
940 case UTIL_BLITTER_ATTRIB_COLOR
:
941 blitter_set_clear_color(ctx
, attrib
);
944 case UTIL_BLITTER_ATTRIB_TEXCOORD
:
945 set_texcoords_in_vertices(attrib
->f
, &ctx
->vertices
[0][1][0], 8);
951 blitter_draw(ctx
, x1
, y1
, x2
, y2
, depth
);
954 static void util_blitter_clear_custom(struct blitter_context
*blitter
,
955 unsigned width
, unsigned height
,
956 unsigned clear_buffers
,
957 const union pipe_color_union
*color
,
958 double depth
, unsigned stencil
,
959 void *custom_blend
, void *custom_dsa
)
961 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
962 struct pipe_context
*pipe
= ctx
->base
.pipe
;
963 struct pipe_stencil_ref sr
= { { 0 } };
965 blitter_set_running_flag(ctx
);
966 blitter_check_saved_vertex_states(ctx
);
967 blitter_check_saved_fragment_states(ctx
);
968 blitter_disable_render_cond(ctx
);
972 pipe
->bind_blend_state(pipe
, custom_blend
);
973 } else if (clear_buffers
& PIPE_CLEAR_COLOR
) {
974 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
]);
976 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
980 pipe
->bind_depth_stencil_alpha_state(pipe
, custom_dsa
);
981 } else if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
982 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
983 } else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
984 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
985 } else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
986 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
988 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
991 sr
.ref_value
[0] = stencil
& 0xff;
992 pipe
->set_stencil_ref(pipe
, &sr
);
994 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
995 ctx
->bind_fs_state(pipe
, ctx
->fs_write_all_cbufs
);
996 pipe
->set_sample_mask(pipe
, ~0);
998 blitter_set_common_draw_rect_state(ctx
, FALSE
);
999 blitter_set_dst_dimensions(ctx
, width
, height
);
1000 blitter
->draw_rectangle(blitter
, 0, 0, width
, height
, (float) depth
,
1001 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1003 blitter_restore_vertex_states(ctx
);
1004 blitter_restore_fragment_states(ctx
);
1005 blitter_restore_render_cond(ctx
);
1006 blitter_unset_running_flag(ctx
);
1009 void util_blitter_clear(struct blitter_context
*blitter
,
1010 unsigned width
, unsigned height
,
1011 unsigned clear_buffers
,
1012 const union pipe_color_union
*color
,
1013 double depth
, unsigned stencil
)
1015 util_blitter_clear_custom(blitter
, width
, height
,
1016 clear_buffers
, color
, depth
, stencil
,
1020 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1021 unsigned width
, unsigned height
,
1022 double depth
, void *custom_dsa
)
1024 static const union pipe_color_union color
;
1025 util_blitter_clear_custom(blitter
, width
, height
, 0, &color
, depth
, 0,
1029 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1030 struct pipe_resource
*dst
,
1034 memset(dst_templ
, 0, sizeof(*dst_templ
));
1035 dst_templ
->format
= util_format_linear(dst
->format
);
1036 dst_templ
->u
.tex
.level
= dstlevel
;
1037 dst_templ
->u
.tex
.first_layer
= dstz
;
1038 dst_templ
->u
.tex
.last_layer
= dstz
;
1041 static struct pipe_surface
*
1042 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
1043 struct pipe_surface
*surf
)
1045 struct pipe_surface dst_templ
;
1047 memset(&dst_templ
, 0, sizeof(dst_templ
));
1048 dst_templ
.format
= surf
->format
;
1049 dst_templ
.u
.tex
.level
= surf
->u
.tex
.level
;
1050 dst_templ
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
+ 1;
1051 dst_templ
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
+ 1;
1053 return pipe
->create_surface(pipe
, surf
->texture
, &dst_templ
);
1056 void util_blitter_default_src_texture(struct pipe_sampler_view
*src_templ
,
1057 struct pipe_resource
*src
,
1060 memset(src_templ
, 0, sizeof(*src_templ
));
1061 src_templ
->format
= util_format_linear(src
->format
);
1062 src_templ
->u
.tex
.first_level
= srclevel
;
1063 src_templ
->u
.tex
.last_level
= srclevel
;
1064 src_templ
->u
.tex
.first_layer
= 0;
1065 src_templ
->u
.tex
.last_layer
=
1066 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1067 : src
->array_size
- 1;
1068 src_templ
->swizzle_r
= PIPE_SWIZZLE_RED
;
1069 src_templ
->swizzle_g
= PIPE_SWIZZLE_GREEN
;
1070 src_templ
->swizzle_b
= PIPE_SWIZZLE_BLUE
;
1071 src_templ
->swizzle_a
= PIPE_SWIZZLE_ALPHA
;
1074 static boolean
is_blit_generic_supported(struct blitter_context
*blitter
,
1075 const struct pipe_resource
*dst
,
1076 enum pipe_format dst_format
,
1077 const struct pipe_resource
*src
,
1078 enum pipe_format src_format
,
1081 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1082 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1086 const struct util_format_description
*desc
=
1087 util_format_description(dst_format
);
1088 boolean dst_has_stencil
= util_format_has_stencil(desc
);
1090 /* Stencil export must be supported for stencil copy. */
1091 if ((mask
& PIPE_MASK_S
) && dst_has_stencil
&&
1092 !ctx
->has_stencil_export
) {
1096 if (dst_has_stencil
|| util_format_has_depth(desc
))
1097 bind
= PIPE_BIND_DEPTH_STENCIL
;
1099 bind
= PIPE_BIND_RENDER_TARGET
;
1101 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1102 dst
->nr_samples
, bind
)) {
1108 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1112 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1113 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
)) {
1117 /* Check stencil sampler support for stencil copy. */
1118 if (mask
& PIPE_MASK_S
) {
1119 if (util_format_has_stencil(util_format_description(src_format
))) {
1120 enum pipe_format stencil_format
=
1121 util_format_stencil_only(src_format
);
1122 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1124 if (stencil_format
!= src_format
&&
1125 !screen
->is_format_supported(screen
, stencil_format
,
1126 src
->target
, src
->nr_samples
,
1127 PIPE_BIND_SAMPLER_VIEW
)) {
1137 boolean
util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1138 const struct pipe_resource
*dst
,
1139 const struct pipe_resource
*src
,
1142 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1143 src
, src
->format
, mask
);
1146 boolean
util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1147 const struct pipe_blit_info
*info
)
1149 return is_blit_generic_supported(blitter
,
1150 info
->dst
.resource
, info
->dst
.format
,
1151 info
->src
.resource
, info
->src
.format
,
1155 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1156 struct pipe_resource
*dst
,
1158 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1159 struct pipe_resource
*src
,
1161 const struct pipe_box
*srcbox
, unsigned mask
,
1162 boolean copy_all_samples
)
1164 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1165 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1166 struct pipe_surface
*dst_view
, dst_templ
;
1167 struct pipe_sampler_view src_templ
, *src_view
;
1168 struct pipe_box dstbox
;
1171 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1173 u_box_3d(dstx
, dsty
, dstz
, abs(srcbox
->width
), abs(srcbox
->height
),
1174 abs(srcbox
->depth
), &dstbox
);
1176 /* Initialize the surface. */
1177 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1178 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1180 /* Initialize the sampler view. */
1181 util_blitter_default_src_texture(&src_templ
, src
, src_level
);
1182 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1185 util_blitter_blit_generic(blitter
, dst_view
, &dstbox
,
1186 src_view
, srcbox
, src
->width0
, src
->height0
,
1187 mask
, PIPE_TEX_FILTER_NEAREST
, NULL
,
1190 pipe_surface_reference(&dst_view
, NULL
);
1191 pipe_sampler_view_reference(&src_view
, NULL
);
1194 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1195 struct pipe_surface
*dst
,
1196 const struct pipe_box
*dstbox
,
1197 struct pipe_sampler_view
*src
,
1198 const struct pipe_box
*srcbox
,
1199 unsigned src_width0
, unsigned src_height0
,
1200 unsigned mask
, unsigned filter
,
1201 const struct pipe_scissor_state
*scissor
,
1202 boolean copy_all_samples
)
1204 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1205 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1206 struct pipe_framebuffer_state fb_state
;
1207 enum pipe_texture_target src_target
= src
->texture
->target
;
1208 unsigned src_samples
= src
->texture
->nr_samples
;
1209 boolean has_depth
, has_stencil
, has_color
;
1210 boolean blit_stencil
, blit_depth
, blit_color
;
1211 void *sampler_state
;
1212 const struct util_format_description
*src_desc
=
1213 util_format_description(src
->format
);
1214 const struct util_format_description
*dst_desc
=
1215 util_format_description(dst
->format
);
1217 has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
&&
1218 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1219 has_depth
= util_format_has_depth(src_desc
) &&
1220 util_format_has_depth(dst_desc
);
1221 has_stencil
= util_format_has_stencil(src_desc
) &&
1222 util_format_has_stencil(dst_desc
);
1224 blit_color
= has_color
&& (mask
& PIPE_MASK_RGBA
);
1225 blit_depth
= has_depth
&& (mask
& PIPE_MASK_Z
);
1226 blit_stencil
= has_stencil
&& (mask
& PIPE_MASK_S
) &&
1227 ctx
->has_stencil_export
;
1229 if (!blit_stencil
&& !blit_depth
&& !blit_color
) {
1233 /* Check whether the states are properly saved. */
1234 blitter_set_running_flag(ctx
);
1235 blitter_check_saved_vertex_states(ctx
);
1236 blitter_check_saved_fragment_states(ctx
);
1237 blitter_check_saved_textures(ctx
);
1238 blitter_check_saved_fb_state(ctx
);
1239 blitter_disable_render_cond(ctx
);
1241 /* Initialize framebuffer state. */
1242 fb_state
.width
= dst
->width
;
1243 fb_state
.height
= dst
->height
;
1244 fb_state
.nr_cbufs
= blit_depth
|| blit_stencil
? 0 : 1;
1245 fb_state
.cbufs
[0] = NULL
;
1246 fb_state
.zsbuf
= NULL
;
1248 if (blit_depth
|| blit_stencil
) {
1249 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1251 if (blit_depth
&& blit_stencil
) {
1252 pipe
->bind_depth_stencil_alpha_state(pipe
,
1253 ctx
->dsa_write_depth_stencil
);
1254 ctx
->bind_fs_state(pipe
,
1255 blitter_get_fs_texfetch_depthstencil(ctx
, src_target
,
1257 } else if (blit_depth
) {
1258 pipe
->bind_depth_stencil_alpha_state(pipe
,
1259 ctx
->dsa_write_depth_keep_stencil
);
1260 ctx
->bind_fs_state(pipe
,
1261 blitter_get_fs_texfetch_depth(ctx
, src_target
,
1263 } else { /* is_stencil */
1264 pipe
->bind_depth_stencil_alpha_state(pipe
,
1265 ctx
->dsa_keep_depth_write_stencil
);
1266 ctx
->bind_fs_state(pipe
,
1267 blitter_get_fs_texfetch_stencil(ctx
, src_target
,
1272 pipe
->bind_blend_state(pipe
, ctx
->blend
[mask
& PIPE_MASK_RGBA
]);
1273 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1274 ctx
->bind_fs_state(pipe
,
1275 blitter_get_fs_texfetch_col(ctx
, src_target
,
1279 /* Set the linear filter only for scaled color non-MSAA blits. */
1280 if (filter
== PIPE_TEX_FILTER_LINEAR
&&
1281 !blit_depth
&& !blit_stencil
&&
1283 (dstbox
->width
!= abs(srcbox
->width
) ||
1284 dstbox
->height
!= abs(srcbox
->height
))) {
1285 if (src_target
== PIPE_TEXTURE_RECT
) {
1286 sampler_state
= ctx
->sampler_state_rect_linear
;
1288 sampler_state
= ctx
->sampler_state_linear
;
1291 if (src_target
== PIPE_TEXTURE_RECT
) {
1292 sampler_state
= ctx
->sampler_state_rect
;
1294 sampler_state
= ctx
->sampler_state
;
1299 if (blit_depth
&& blit_stencil
) {
1300 /* Setup two samplers, one for depth and the other one for stencil. */
1301 struct pipe_sampler_view templ
;
1302 struct pipe_sampler_view
*views
[2];
1303 void *samplers
[2] = {sampler_state
, sampler_state
};
1306 templ
.format
= util_format_stencil_only(templ
.format
);
1307 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1310 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1312 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, views
);
1313 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, 0, 2, samplers
);
1315 pipe_sampler_view_reference(&views
[1], NULL
);
1316 } else if (blit_stencil
) {
1317 /* Set a stencil-only sampler view for it not to sample depth instead. */
1318 struct pipe_sampler_view templ
;
1319 struct pipe_sampler_view
*view
;
1322 templ
.format
= util_format_stencil_only(templ
.format
);
1323 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1325 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1327 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &view
);
1328 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1329 0, 1, &sampler_state
);
1331 pipe_sampler_view_reference(&view
, NULL
);
1333 pipe
->set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, 1, &src
);
1334 pipe
->bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
1335 0, 1, &sampler_state
);
1338 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1340 pipe
->set_scissor_states(pipe
, 0, 1, scissor
);
1343 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
);
1344 blitter_set_dst_dimensions(ctx
, dst
->width
, dst
->height
);
1346 if ((src_target
== PIPE_TEXTURE_1D
||
1347 src_target
== PIPE_TEXTURE_2D
||
1348 src_target
== PIPE_TEXTURE_RECT
) &&
1350 /* Draw the quad with the draw_rectangle callback. */
1352 /* Set texture coordinates. - use a pipe color union
1353 * for interface purposes.
1354 * XXX pipe_color_union is a wrong name since we use that to set
1355 * texture coordinates too.
1357 union pipe_color_union coord
;
1358 get_texcoords(src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1359 srcbox
->x
+srcbox
->width
, srcbox
->y
+srcbox
->height
, coord
.f
);
1361 /* Set framebuffer state. */
1362 if (blit_depth
|| blit_stencil
) {
1363 fb_state
.zsbuf
= dst
;
1365 fb_state
.cbufs
[0] = dst
;
1367 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1370 pipe
->set_sample_mask(pipe
, ~0);
1371 blitter
->draw_rectangle(blitter
, dstbox
->x
, dstbox
->y
,
1372 dstbox
->x
+ dstbox
->width
,
1373 dstbox
->y
+ dstbox
->height
, 0,
1374 UTIL_BLITTER_ATTRIB_TEXCOORD
, &coord
);
1376 /* Draw the quad with the generic codepath. */
1378 for (z
= 0; z
< dstbox
->depth
; z
++) {
1379 struct pipe_surface
*old
;
1381 /* Set framebuffer state. */
1382 if (blit_depth
|| blit_stencil
) {
1383 fb_state
.zsbuf
= dst
;
1385 fb_state
.cbufs
[0] = dst
;
1387 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1389 /* See if we need to blit a multisample or singlesample buffer. */
1390 if (copy_all_samples
&&
1391 src_samples
== dst
->texture
->nr_samples
&&
1392 dst
->texture
->nr_samples
> 1) {
1393 unsigned i
, max_sample
= MAX2(dst
->texture
->nr_samples
, 1) - 1;
1395 for (i
= 0; i
<= max_sample
; i
++) {
1396 pipe
->set_sample_mask(pipe
, 1 << i
);
1397 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1399 i
, srcbox
->x
, srcbox
->y
,
1400 srcbox
->x
+ srcbox
->width
,
1401 srcbox
->y
+ srcbox
->height
);
1402 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1403 dstbox
->x
+ dstbox
->width
,
1404 dstbox
->y
+ dstbox
->height
, 0);
1407 pipe
->set_sample_mask(pipe
, ~0);
1408 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1410 srcbox
->x
, srcbox
->y
,
1411 srcbox
->x
+ srcbox
->width
,
1412 srcbox
->y
+ srcbox
->height
);
1413 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1414 dstbox
->x
+ dstbox
->width
,
1415 dstbox
->y
+ dstbox
->height
, 0);
1418 /* Get the next surface or (if this is the last iteration)
1419 * just unreference the last one. */
1421 if (z
< dstbox
->depth
-1) {
1422 dst
= ctx
->base
.get_next_surface_layer(ctx
->base
.pipe
, dst
);
1425 pipe_surface_reference(&old
, NULL
);
1430 blitter_restore_vertex_states(ctx
);
1431 blitter_restore_fragment_states(ctx
);
1432 blitter_restore_textures(ctx
);
1433 blitter_restore_fb_state(ctx
);
1435 pipe
->set_scissor_states(pipe
, 0, 1, &ctx
->base
.saved_scissor
);
1437 blitter_restore_render_cond(ctx
);
1438 blitter_unset_running_flag(ctx
);
1442 util_blitter_blit(struct blitter_context
*blitter
,
1443 const struct pipe_blit_info
*info
)
1445 struct pipe_resource
*dst
= info
->dst
.resource
;
1446 struct pipe_resource
*src
= info
->src
.resource
;
1447 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1448 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1449 struct pipe_surface
*dst_view
, dst_templ
;
1450 struct pipe_sampler_view src_templ
, *src_view
;
1452 /* Initialize the surface. */
1453 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
1455 dst_templ
.format
= info
->dst
.format
;
1456 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1458 /* Initialize the sampler view. */
1459 util_blitter_default_src_texture(&src_templ
, src
, info
->src
.level
);
1460 src_templ
.format
= info
->src
.format
;
1461 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1464 util_blitter_blit_generic(blitter
, dst_view
, &info
->dst
.box
,
1465 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
1466 info
->mask
, info
->filter
,
1467 info
->scissor_enable
? &info
->scissor
: NULL
, TRUE
);
1469 pipe_surface_reference(&dst_view
, NULL
);
1470 pipe_sampler_view_reference(&src_view
, NULL
);
1473 /* Clear a region of a color surface to a constant value. */
1474 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
1475 struct pipe_surface
*dstsurf
,
1476 const union pipe_color_union
*color
,
1477 unsigned dstx
, unsigned dsty
,
1478 unsigned width
, unsigned height
)
1480 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1481 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1482 struct pipe_framebuffer_state fb_state
;
1484 assert(dstsurf
->texture
);
1485 if (!dstsurf
->texture
)
1488 /* check the saved state */
1489 blitter_set_running_flag(ctx
);
1490 blitter_check_saved_vertex_states(ctx
);
1491 blitter_check_saved_fragment_states(ctx
);
1492 blitter_check_saved_fb_state(ctx
);
1493 blitter_disable_render_cond(ctx
);
1496 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
]);
1497 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1498 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
1499 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1501 /* set a framebuffer state */
1502 fb_state
.width
= dstsurf
->width
;
1503 fb_state
.height
= dstsurf
->height
;
1504 fb_state
.nr_cbufs
= 1;
1505 fb_state
.cbufs
[0] = dstsurf
;
1507 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1508 pipe
->set_sample_mask(pipe
, ~0);
1510 blitter_set_common_draw_rect_state(ctx
, FALSE
);
1511 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1512 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
1513 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1515 blitter_restore_vertex_states(ctx
);
1516 blitter_restore_fragment_states(ctx
);
1517 blitter_restore_fb_state(ctx
);
1518 blitter_restore_render_cond(ctx
);
1519 blitter_unset_running_flag(ctx
);
1522 /* Clear a region of a depth stencil surface. */
1523 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
1524 struct pipe_surface
*dstsurf
,
1525 unsigned clear_flags
,
1528 unsigned dstx
, unsigned dsty
,
1529 unsigned width
, unsigned height
)
1531 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1532 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1533 struct pipe_framebuffer_state fb_state
;
1534 struct pipe_stencil_ref sr
= { { 0 } };
1536 assert(dstsurf
->texture
);
1537 if (!dstsurf
->texture
)
1540 /* check the saved state */
1541 blitter_set_running_flag(ctx
);
1542 blitter_check_saved_vertex_states(ctx
);
1543 blitter_check_saved_fragment_states(ctx
);
1544 blitter_check_saved_fb_state(ctx
);
1545 blitter_disable_render_cond(ctx
);
1548 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1549 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1550 sr
.ref_value
[0] = stencil
& 0xff;
1551 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1552 pipe
->set_stencil_ref(pipe
, &sr
);
1554 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
1555 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1557 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
1558 sr
.ref_value
[0] = stencil
& 0xff;
1559 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1560 pipe
->set_stencil_ref(pipe
, &sr
);
1563 /* hmm that should be illegal probably, or make it a no-op somewhere */
1564 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1566 ctx
->bind_fs_state(pipe
, ctx
->fs_empty
);
1567 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1569 /* set a framebuffer state */
1570 fb_state
.width
= dstsurf
->width
;
1571 fb_state
.height
= dstsurf
->height
;
1572 fb_state
.nr_cbufs
= 0;
1573 fb_state
.cbufs
[0] = 0;
1574 fb_state
.zsbuf
= dstsurf
;
1575 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1576 pipe
->set_sample_mask(pipe
, ~0);
1578 blitter_set_common_draw_rect_state(ctx
, FALSE
);
1579 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1580 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
,
1582 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
1584 blitter_restore_vertex_states(ctx
);
1585 blitter_restore_fragment_states(ctx
);
1586 blitter_restore_fb_state(ctx
);
1587 blitter_restore_render_cond(ctx
);
1588 blitter_unset_running_flag(ctx
);
1591 /* draw a rectangle across a region using a custom dsa stage - for r600g */
1592 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
1593 struct pipe_surface
*zsurf
,
1594 struct pipe_surface
*cbsurf
,
1595 unsigned sample_mask
,
1596 void *dsa_stage
, float depth
)
1598 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1599 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1600 struct pipe_framebuffer_state fb_state
;
1602 assert(zsurf
->texture
);
1603 if (!zsurf
->texture
)
1606 /* check the saved state */
1607 blitter_set_running_flag(ctx
);
1608 blitter_check_saved_vertex_states(ctx
);
1609 blitter_check_saved_fragment_states(ctx
);
1610 blitter_check_saved_fb_state(ctx
);
1611 blitter_disable_render_cond(ctx
);
1614 pipe
->bind_blend_state(pipe
, cbsurf
? ctx
->blend
[PIPE_MASK_RGBA
] :
1616 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
1617 ctx
->bind_fs_state(pipe
, ctx
->fs_empty
);
1618 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1620 /* set a framebuffer state */
1621 fb_state
.width
= zsurf
->width
;
1622 fb_state
.height
= zsurf
->height
;
1623 fb_state
.nr_cbufs
= 1;
1625 fb_state
.cbufs
[0] = cbsurf
;
1626 fb_state
.nr_cbufs
= 1;
1628 fb_state
.cbufs
[0] = NULL
;
1629 fb_state
.nr_cbufs
= 0;
1631 fb_state
.zsbuf
= zsurf
;
1632 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1633 pipe
->set_sample_mask(pipe
, sample_mask
);
1635 blitter_set_common_draw_rect_state(ctx
, FALSE
);
1636 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
1637 blitter
->draw_rectangle(blitter
, 0, 0, zsurf
->width
, zsurf
->height
, depth
,
1638 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
1640 blitter_restore_vertex_states(ctx
);
1641 blitter_restore_fragment_states(ctx
);
1642 blitter_restore_fb_state(ctx
);
1643 blitter_restore_render_cond(ctx
);
1644 blitter_unset_running_flag(ctx
);
1647 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
1648 struct pipe_resource
*dst
,
1650 struct pipe_resource
*src
,
1654 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1655 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1656 struct pipe_vertex_buffer vb
;
1657 struct pipe_stream_output_target
*so_target
;
1659 if (srcx
>= src
->width0
||
1660 dstx
>= dst
->width0
) {
1663 if (srcx
+ size
> src
->width0
) {
1664 size
= src
->width0
- srcx
;
1666 if (dstx
+ size
> dst
->width0
) {
1667 size
= dst
->width0
- dstx
;
1670 /* Drivers not capable of Stream Out should not call this function
1671 * in the first place. */
1672 assert(ctx
->has_stream_out
);
1674 /* Some alignment is required. */
1675 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
1676 !ctx
->has_stream_out
) {
1677 struct pipe_box box
;
1678 u_box_1d(srcx
, size
, &box
);
1679 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
1683 blitter_set_running_flag(ctx
);
1684 blitter_check_saved_vertex_states(ctx
);
1685 blitter_disable_render_cond(ctx
);
1688 vb
.buffer_offset
= srcx
;
1691 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1692 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[0]);
1693 pipe
->bind_vs_state(pipe
, ctx
->vs_pos_only
);
1694 if (ctx
->has_geometry_shader
)
1695 pipe
->bind_gs_state(pipe
, NULL
);
1696 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
1698 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
1699 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, 0);
1701 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
1703 blitter_restore_vertex_states(ctx
);
1704 blitter_restore_render_cond(ctx
);
1705 blitter_unset_running_flag(ctx
);
1706 pipe_so_target_reference(&so_target
, NULL
);
1709 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
1710 struct pipe_resource
*dst
,
1711 unsigned offset
, unsigned size
,
1712 unsigned num_channels
,
1713 const union pipe_color_union
*clear_value
)
1715 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1716 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1717 struct pipe_vertex_buffer vb
= {0};
1718 struct pipe_stream_output_target
*so_target
;
1720 assert(num_channels
>= 1);
1721 assert(num_channels
<= 4);
1723 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
1725 * R600 uses this to initialize texture resources, so width0 might not be
1726 * what you think it is.
1729 /* Streamout is required. */
1730 if (!ctx
->has_stream_out
) {
1731 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
1735 /* Some alignment is required. */
1736 if (offset
% 4 != 0 || size
% 4 != 0) {
1737 assert(!"Bad alignment in util_blitter_clear_buffer()");
1741 u_upload_data(ctx
->upload
, 0, num_channels
*4, clear_value
,
1742 &vb
.buffer_offset
, &vb
.buffer
);
1745 blitter_set_running_flag(ctx
);
1746 blitter_check_saved_vertex_states(ctx
);
1747 blitter_disable_render_cond(ctx
);
1749 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1750 pipe
->bind_vertex_elements_state(pipe
,
1751 ctx
->velem_state_readbuf
[num_channels
-1]);
1752 pipe
->bind_vs_state(pipe
, ctx
->vs_pos_only
);
1753 if (ctx
->has_geometry_shader
)
1754 pipe
->bind_gs_state(pipe
, NULL
);
1755 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
1757 so_target
= pipe
->create_stream_output_target(pipe
, dst
, offset
, size
);
1758 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, 0);
1760 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
1762 blitter_restore_vertex_states(ctx
);
1763 blitter_restore_render_cond(ctx
);
1764 blitter_unset_running_flag(ctx
);
1765 pipe_so_target_reference(&so_target
, NULL
);
1766 pipe_resource_reference(&vb
.buffer
, NULL
);
1769 /* probably radeon specific */
1770 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
1771 struct pipe_resource
*dst
,
1774 struct pipe_resource
*src
,
1776 unsigned sample_mask
,
1778 enum pipe_format format
)
1780 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1781 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1782 struct pipe_framebuffer_state fb_state
;
1783 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
1785 blitter_set_running_flag(ctx
);
1786 blitter_check_saved_vertex_states(ctx
);
1787 blitter_check_saved_fragment_states(ctx
);
1788 blitter_disable_render_cond(ctx
);
1791 pipe
->bind_blend_state(pipe
, custom_blend
);
1792 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1793 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1794 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
1795 pipe
->set_sample_mask(pipe
, sample_mask
);
1797 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1798 surf_tmpl
.format
= format
;
1799 surf_tmpl
.u
.tex
.level
= dst_level
;
1800 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
1801 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
1803 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
1805 surf_tmpl
.u
.tex
.level
= 0;
1806 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
1807 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
1809 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
1811 /* set a framebuffer state */
1812 fb_state
.width
= src
->width0
;
1813 fb_state
.height
= src
->height0
;
1814 fb_state
.nr_cbufs
= 2;
1815 fb_state
.cbufs
[0] = srcsurf
;
1816 fb_state
.cbufs
[1] = dstsurf
;
1817 fb_state
.zsbuf
= NULL
;
1818 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1820 blitter_set_common_draw_rect_state(ctx
, FALSE
);
1821 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
1822 blitter
->draw_rectangle(blitter
, 0, 0, src
->width0
, src
->height0
,
1824 blitter_restore_fb_state(ctx
);
1825 blitter_restore_vertex_states(ctx
);
1826 blitter_restore_fragment_states(ctx
);
1827 blitter_restore_render_cond(ctx
);
1828 blitter_unset_running_flag(ctx
);
1830 pipe_surface_reference(&srcsurf
, NULL
);
1831 pipe_surface_reference(&dstsurf
, NULL
);
1834 void util_blitter_custom_color(struct blitter_context
*blitter
,
1835 struct pipe_surface
*dstsurf
,
1838 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1839 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1840 struct pipe_framebuffer_state fb_state
;
1842 assert(dstsurf
->texture
);
1843 if (!dstsurf
->texture
)
1846 /* check the saved state */
1847 blitter_set_running_flag(ctx
);
1848 blitter_check_saved_vertex_states(ctx
);
1849 blitter_check_saved_fragment_states(ctx
);
1850 blitter_check_saved_fb_state(ctx
);
1851 blitter_disable_render_cond(ctx
);
1854 pipe
->bind_blend_state(pipe
, custom_blend
? custom_blend
1855 : ctx
->blend
[PIPE_MASK_RGBA
]);
1856 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1857 ctx
->bind_fs_state(pipe
, ctx
->fs_write_one_cbuf
);
1858 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1859 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
1861 /* set a framebuffer state */
1862 fb_state
.width
= dstsurf
->width
;
1863 fb_state
.height
= dstsurf
->height
;
1864 fb_state
.nr_cbufs
= 1;
1865 fb_state
.cbufs
[0] = dstsurf
;
1867 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1868 pipe
->set_sample_mask(pipe
, ~0);
1870 blitter_set_common_draw_rect_state(ctx
, FALSE
);
1871 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1872 blitter
->draw_rectangle(blitter
, 0, 0, dstsurf
->width
, dstsurf
->height
,
1875 blitter_restore_vertex_states(ctx
);
1876 blitter_restore_fragment_states(ctx
);
1877 blitter_restore_fb_state(ctx
);
1878 blitter_restore_render_cond(ctx
);
1879 blitter_unset_running_flag(ctx
);