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
[4]; /**< X, XY, XYZ, XYZW */
107 void *sampler_state_linear
;
108 void *sampler_state_rect
;
109 void *sampler_state_rect_linear
;
111 /* Rasterizer state. */
112 void *rs_state
, *rs_state_scissor
, *rs_discard_state
;
114 /* Viewport state. */
115 struct pipe_viewport_state viewport
;
117 /* Destination surface dimensions. */
121 boolean has_geometry_shader
;
122 boolean vertex_has_integers
;
123 boolean has_stream_out
;
124 boolean has_stencil_export
;
125 boolean has_texture_multisample
;
127 /* The Draw module overrides these functions.
128 * Always create the blitter before Draw. */
129 void (*bind_fs_state
)(struct pipe_context
*, void *);
130 void (*delete_fs_state
)(struct pipe_context
*, void *);
133 static struct pipe_surface
*
134 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
135 struct pipe_surface
*surf
);
137 struct blitter_context
*util_blitter_create(struct pipe_context
*pipe
)
139 struct blitter_context_priv
*ctx
;
140 struct pipe_blend_state blend
;
141 struct pipe_depth_stencil_alpha_state dsa
;
142 struct pipe_rasterizer_state rs_state
;
143 struct pipe_sampler_state sampler_state
;
144 struct pipe_vertex_element velem
[2];
147 ctx
= CALLOC_STRUCT(blitter_context_priv
);
151 ctx
->base
.pipe
= pipe
;
152 ctx
->base
.draw_rectangle
= util_blitter_draw_rectangle
;
153 ctx
->base
.get_next_surface_layer
= util_blitter_get_next_surface_layer
;
155 ctx
->bind_fs_state
= pipe
->bind_fs_state
;
156 ctx
->delete_fs_state
= pipe
->delete_fs_state
;
158 /* init state objects for them to be considered invalid */
159 ctx
->base
.saved_blend_state
= INVALID_PTR
;
160 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
161 ctx
->base
.saved_rs_state
= INVALID_PTR
;
162 ctx
->base
.saved_fs
= INVALID_PTR
;
163 ctx
->base
.saved_vs
= INVALID_PTR
;
164 ctx
->base
.saved_gs
= INVALID_PTR
;
165 ctx
->base
.saved_velem_state
= INVALID_PTR
;
166 ctx
->base
.saved_fb_state
.nr_cbufs
= ~0;
167 ctx
->base
.saved_num_sampler_views
= ~0;
168 ctx
->base
.saved_num_sampler_states
= ~0;
169 ctx
->base
.saved_num_so_targets
= ~0;
171 ctx
->has_geometry_shader
=
172 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
173 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0;
174 ctx
->vertex_has_integers
=
175 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_VERTEX
,
176 PIPE_SHADER_CAP_INTEGERS
);
177 ctx
->has_stream_out
=
178 pipe
->screen
->get_param(pipe
->screen
,
179 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0;
181 ctx
->has_stencil_export
=
182 pipe
->screen
->get_param(pipe
->screen
,
183 PIPE_CAP_SHADER_STENCIL_EXPORT
);
185 ctx
->has_texture_multisample
=
186 pipe
->screen
->get_param(pipe
->screen
, PIPE_CAP_TEXTURE_MULTISAMPLE
);
188 /* blend state objects */
189 memset(&blend
, 0, sizeof(blend
));
191 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
192 blend
.rt
[0].colormask
= i
;
193 ctx
->blend
[i
] = pipe
->create_blend_state(pipe
, &blend
);
196 /* depth stencil alpha state objects */
197 memset(&dsa
, 0, sizeof(dsa
));
198 ctx
->dsa_keep_depth_stencil
=
199 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
201 dsa
.depth
.enabled
= 1;
202 dsa
.depth
.writemask
= 1;
203 dsa
.depth
.func
= PIPE_FUNC_ALWAYS
;
204 ctx
->dsa_write_depth_keep_stencil
=
205 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
207 dsa
.stencil
[0].enabled
= 1;
208 dsa
.stencil
[0].func
= PIPE_FUNC_ALWAYS
;
209 dsa
.stencil
[0].fail_op
= PIPE_STENCIL_OP_REPLACE
;
210 dsa
.stencil
[0].zpass_op
= PIPE_STENCIL_OP_REPLACE
;
211 dsa
.stencil
[0].zfail_op
= PIPE_STENCIL_OP_REPLACE
;
212 dsa
.stencil
[0].valuemask
= 0xff;
213 dsa
.stencil
[0].writemask
= 0xff;
214 ctx
->dsa_write_depth_stencil
=
215 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
217 dsa
.depth
.enabled
= 0;
218 dsa
.depth
.writemask
= 0;
219 ctx
->dsa_keep_depth_write_stencil
=
220 pipe
->create_depth_stencil_alpha_state(pipe
, &dsa
);
223 memset(&sampler_state
, 0, sizeof(sampler_state
));
224 sampler_state
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
225 sampler_state
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
226 sampler_state
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
227 sampler_state
.normalized_coords
= 1;
228 ctx
->sampler_state
= pipe
->create_sampler_state(pipe
, &sampler_state
);
229 sampler_state
.normalized_coords
= 0;
230 ctx
->sampler_state_rect
= pipe
->create_sampler_state(pipe
, &sampler_state
);
232 sampler_state
.min_img_filter
= PIPE_TEX_FILTER_LINEAR
;
233 sampler_state
.mag_img_filter
= PIPE_TEX_FILTER_LINEAR
;
234 sampler_state
.normalized_coords
= 1;
235 ctx
->sampler_state_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
236 sampler_state
.normalized_coords
= 0;
237 ctx
->sampler_state_rect_linear
= pipe
->create_sampler_state(pipe
, &sampler_state
);
239 /* rasterizer state */
240 memset(&rs_state
, 0, sizeof(rs_state
));
241 rs_state
.cull_face
= PIPE_FACE_NONE
;
242 rs_state
.half_pixel_center
= 1;
243 rs_state
.bottom_edge_rule
= 1;
244 rs_state
.flatshade
= 1;
245 rs_state
.depth_clip
= 1;
246 ctx
->rs_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
248 rs_state
.scissor
= 1;
249 ctx
->rs_state_scissor
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
251 if (ctx
->has_stream_out
) {
252 rs_state
.scissor
= 0;
253 rs_state
.rasterizer_discard
= 1;
254 ctx
->rs_discard_state
= pipe
->create_rasterizer_state(pipe
, &rs_state
);
257 ctx
->base
.vb_slot
= 0; /* 0 for now */
259 /* vertex elements states */
260 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
261 for (i
= 0; i
< 2; i
++) {
262 velem
[i
].src_offset
= i
* 4 * sizeof(float);
263 velem
[i
].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
264 velem
[i
].vertex_buffer_index
= ctx
->base
.vb_slot
;
266 ctx
->velem_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
268 if (ctx
->vertex_has_integers
) {
269 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
270 velem
[0].src_offset
= 0;
271 velem
[0].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
272 velem
[0].vertex_buffer_index
= ctx
->base
.vb_slot
;
273 velem
[1].src_offset
= 4 * sizeof(float);
274 velem
[1].src_format
= PIPE_FORMAT_R32G32B32A32_SINT
;
275 velem
[1].vertex_buffer_index
= ctx
->base
.vb_slot
;
276 ctx
->velem_sint_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
278 memset(&velem
[0], 0, sizeof(velem
[0]) * 2);
279 velem
[0].src_offset
= 0;
280 velem
[0].src_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
281 velem
[0].vertex_buffer_index
= ctx
->base
.vb_slot
;
282 velem
[1].src_offset
= 4 * sizeof(float);
283 velem
[1].src_format
= PIPE_FORMAT_R32G32B32A32_UINT
;
284 velem
[1].vertex_buffer_index
= ctx
->base
.vb_slot
;
285 ctx
->velem_uint_state
= pipe
->create_vertex_elements_state(pipe
, 2, &velem
[0]);
288 if (ctx
->has_stream_out
) {
289 static enum pipe_format formats
[4] = {
290 PIPE_FORMAT_R32_UINT
,
291 PIPE_FORMAT_R32G32_UINT
,
292 PIPE_FORMAT_R32G32B32_UINT
,
293 PIPE_FORMAT_R32G32B32A32_UINT
296 for (i
= 0; i
< 4; i
++) {
297 velem
[0].src_format
= formats
[i
];
298 velem
[0].vertex_buffer_index
= ctx
->base
.vb_slot
;
299 ctx
->velem_state_readbuf
[i
] =
300 pipe
->create_vertex_elements_state(pipe
, 1, &velem
[0]);
304 /* fragment shaders are created on-demand */
308 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
,
309 TGSI_SEMANTIC_GENERIC
};
310 const uint semantic_indices
[] = { 0, 0 };
312 util_make_vertex_passthrough_shader(pipe
, 2, semantic_names
,
315 if (ctx
->has_stream_out
) {
316 struct pipe_stream_output_info so
;
317 const uint semantic_names
[] = { TGSI_SEMANTIC_POSITION
};
318 const uint semantic_indices
[] = { 0 };
320 memset(&so
, 0, sizeof(so
));
322 so
.output
[0].num_components
= 1;
326 util_make_vertex_passthrough_shader_with_so(pipe
, 1, semantic_names
,
327 semantic_indices
, &so
);
330 /* set invariant vertex coordinates */
331 for (i
= 0; i
< 4; i
++)
332 ctx
->vertices
[i
][0][3] = 1; /*v.w*/
334 ctx
->upload
= u_upload_create(pipe
, 65536, 4, PIPE_BIND_VERTEX_BUFFER
);
339 void util_blitter_destroy(struct blitter_context
*blitter
)
341 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
342 struct pipe_context
*pipe
= blitter
->pipe
;
345 for (i
= 0; i
<= PIPE_MASK_RGBA
; i
++) {
346 pipe
->delete_blend_state(pipe
, ctx
->blend
[i
]);
348 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
349 pipe
->delete_depth_stencil_alpha_state(pipe
,
350 ctx
->dsa_write_depth_keep_stencil
);
351 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
352 pipe
->delete_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
354 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state
);
355 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_state_scissor
);
356 if (ctx
->rs_discard_state
)
357 pipe
->delete_rasterizer_state(pipe
, ctx
->rs_discard_state
);
358 pipe
->delete_vs_state(pipe
, ctx
->vs
);
359 if (ctx
->vs_pos_only
)
360 pipe
->delete_vs_state(pipe
, ctx
->vs_pos_only
);
361 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state
);
362 if (ctx
->vertex_has_integers
) {
363 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_sint_state
);
364 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_uint_state
);
366 for (i
= 0; i
< 4; i
++) {
367 if (ctx
->velem_state_readbuf
[i
]) {
368 pipe
->delete_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[i
]);
372 for (i
= 0; i
< PIPE_MAX_TEXTURE_TYPES
; i
++) {
373 if (ctx
->fs_texfetch_col
[i
])
374 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_col
[i
]);
375 if (ctx
->fs_texfetch_depth
[i
])
376 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depth
[i
]);
377 if (ctx
->fs_texfetch_depthstencil
[i
])
378 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_depthstencil
[i
]);
379 if (ctx
->fs_texfetch_stencil
[i
])
380 ctx
->delete_fs_state(pipe
, ctx
->fs_texfetch_stencil
[i
]);
383 for (i
= 0; i
<= PIPE_MAX_COLOR_BUFS
; i
++) {
385 ctx
->delete_fs_state(pipe
, ctx
->fs_col
[i
]);
386 if (ctx
->fs_col_int
[i
])
387 ctx
->delete_fs_state(pipe
, ctx
->fs_col_int
[i
]);
390 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect_linear
);
391 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_rect
);
392 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state_linear
);
393 pipe
->delete_sampler_state(pipe
, ctx
->sampler_state
);
394 u_upload_destroy(ctx
->upload
);
398 void util_blitter_set_texture_multisample(struct blitter_context
*blitter
,
401 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
403 ctx
->has_texture_multisample
= supported
;
406 static void blitter_set_running_flag(struct blitter_context_priv
*ctx
)
408 if (ctx
->base
.running
) {
409 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
412 ctx
->base
.running
= TRUE
;
415 static void blitter_unset_running_flag(struct blitter_context_priv
*ctx
)
417 if (!ctx
->base
.running
) {
418 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
421 ctx
->base
.running
= FALSE
;
424 static void blitter_check_saved_vertex_states(struct blitter_context_priv
*ctx
)
426 assert(ctx
->base
.saved_velem_state
!= INVALID_PTR
);
427 assert(ctx
->base
.saved_vs
!= INVALID_PTR
);
428 assert(!ctx
->has_geometry_shader
|| ctx
->base
.saved_gs
!= INVALID_PTR
);
429 assert(!ctx
->has_stream_out
|| ctx
->base
.saved_num_so_targets
!= ~0);
430 assert(ctx
->base
.saved_rs_state
!= INVALID_PTR
);
433 static void blitter_restore_vertex_states(struct blitter_context_priv
*ctx
)
435 struct pipe_context
*pipe
= ctx
->base
.pipe
;
439 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1,
440 &ctx
->base
.saved_vertex_buffer
);
441 pipe_resource_reference(&ctx
->base
.saved_vertex_buffer
.buffer
, NULL
);
443 /* Vertex elements. */
444 pipe
->bind_vertex_elements_state(pipe
, ctx
->base
.saved_velem_state
);
445 ctx
->base
.saved_velem_state
= INVALID_PTR
;
448 pipe
->bind_vs_state(pipe
, ctx
->base
.saved_vs
);
449 ctx
->base
.saved_vs
= INVALID_PTR
;
451 /* Geometry shader. */
452 if (ctx
->has_geometry_shader
) {
453 pipe
->bind_gs_state(pipe
, ctx
->base
.saved_gs
);
454 ctx
->base
.saved_gs
= INVALID_PTR
;
457 /* Stream outputs. */
458 if (ctx
->has_stream_out
) {
459 pipe
->set_stream_output_targets(pipe
,
460 ctx
->base
.saved_num_so_targets
,
461 ctx
->base
.saved_so_targets
, ~0);
463 for (i
= 0; i
< ctx
->base
.saved_num_so_targets
; i
++)
464 pipe_so_target_reference(&ctx
->base
.saved_so_targets
[i
], NULL
);
466 ctx
->base
.saved_num_so_targets
= ~0;
470 pipe
->bind_rasterizer_state(pipe
, ctx
->base
.saved_rs_state
);
471 ctx
->base
.saved_rs_state
= INVALID_PTR
;
474 static void blitter_check_saved_fragment_states(struct blitter_context_priv
*ctx
)
476 assert(ctx
->base
.saved_fs
!= INVALID_PTR
);
477 assert(ctx
->base
.saved_dsa_state
!= INVALID_PTR
);
478 assert(ctx
->base
.saved_blend_state
!= INVALID_PTR
);
481 static void blitter_restore_fragment_states(struct blitter_context_priv
*ctx
)
483 struct pipe_context
*pipe
= ctx
->base
.pipe
;
485 /* Fragment shader. */
486 ctx
->bind_fs_state(pipe
, ctx
->base
.saved_fs
);
487 ctx
->base
.saved_fs
= INVALID_PTR
;
489 /* Depth, stencil, alpha. */
490 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->base
.saved_dsa_state
);
491 ctx
->base
.saved_dsa_state
= INVALID_PTR
;
494 pipe
->bind_blend_state(pipe
, ctx
->base
.saved_blend_state
);
495 ctx
->base
.saved_blend_state
= INVALID_PTR
;
498 if (ctx
->base
.is_sample_mask_saved
) {
499 pipe
->set_sample_mask(pipe
, ctx
->base
.saved_sample_mask
);
500 ctx
->base
.is_sample_mask_saved
= FALSE
;
503 /* Miscellaneous states. */
504 /* XXX check whether these are saved and whether they need to be restored
505 * (depending on the operation) */
506 pipe
->set_stencil_ref(pipe
, &ctx
->base
.saved_stencil_ref
);
507 pipe
->set_viewport_state(pipe
, &ctx
->base
.saved_viewport
);
510 static void blitter_check_saved_fb_state(struct blitter_context_priv
*ctx
)
512 assert(ctx
->base
.saved_fb_state
.nr_cbufs
!= ~0);
515 static void blitter_disable_render_cond(struct blitter_context_priv
*ctx
)
517 struct pipe_context
*pipe
= ctx
->base
.pipe
;
519 if (ctx
->base
.saved_render_cond_query
) {
520 pipe
->render_condition(pipe
, NULL
, 0);
524 static void blitter_restore_render_cond(struct blitter_context_priv
*ctx
)
526 struct pipe_context
*pipe
= ctx
->base
.pipe
;
528 if (ctx
->base
.saved_render_cond_query
) {
529 pipe
->render_condition(pipe
, ctx
->base
.saved_render_cond_query
,
530 ctx
->base
.saved_render_cond_mode
);
531 ctx
->base
.saved_render_cond_query
= NULL
;
535 static void blitter_restore_fb_state(struct blitter_context_priv
*ctx
)
537 struct pipe_context
*pipe
= ctx
->base
.pipe
;
539 pipe
->set_framebuffer_state(pipe
, &ctx
->base
.saved_fb_state
);
540 util_unreference_framebuffer_state(&ctx
->base
.saved_fb_state
);
543 static void blitter_check_saved_textures(struct blitter_context_priv
*ctx
)
545 assert(ctx
->base
.saved_num_sampler_states
!= ~0);
546 assert(ctx
->base
.saved_num_sampler_views
!= ~0);
549 static void blitter_restore_textures(struct blitter_context_priv
*ctx
)
551 struct pipe_context
*pipe
= ctx
->base
.pipe
;
554 /* Fragment sampler states. */
555 pipe
->bind_fragment_sampler_states(pipe
,
556 ctx
->base
.saved_num_sampler_states
,
557 ctx
->base
.saved_sampler_states
);
558 ctx
->base
.saved_num_sampler_states
= ~0;
560 /* Fragment sampler views. */
561 pipe
->set_fragment_sampler_views(pipe
,
562 ctx
->base
.saved_num_sampler_views
,
563 ctx
->base
.saved_sampler_views
);
565 for (i
= 0; i
< ctx
->base
.saved_num_sampler_views
; i
++)
566 pipe_sampler_view_reference(&ctx
->base
.saved_sampler_views
[i
], NULL
);
568 ctx
->base
.saved_num_sampler_views
= ~0;
571 static void blitter_set_rectangle(struct blitter_context_priv
*ctx
,
572 int x1
, int y1
, int x2
, int y2
,
577 /* set vertex positions */
578 ctx
->vertices
[0][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v0.x*/
579 ctx
->vertices
[0][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v0.y*/
581 ctx
->vertices
[1][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v1.x*/
582 ctx
->vertices
[1][0][1] = (float)y1
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v1.y*/
584 ctx
->vertices
[2][0][0] = (float)x2
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v2.x*/
585 ctx
->vertices
[2][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v2.y*/
587 ctx
->vertices
[3][0][0] = (float)x1
/ ctx
->dst_width
* 2.0f
- 1.0f
; /*v3.x*/
588 ctx
->vertices
[3][0][1] = (float)y2
/ ctx
->dst_height
* 2.0f
- 1.0f
; /*v3.y*/
590 for (i
= 0; i
< 4; i
++)
591 ctx
->vertices
[i
][0][2] = depth
; /*z*/
594 ctx
->viewport
.scale
[0] = 0.5f
* ctx
->dst_width
;
595 ctx
->viewport
.scale
[1] = 0.5f
* ctx
->dst_height
;
596 ctx
->viewport
.scale
[2] = 1.0f
;
597 ctx
->viewport
.scale
[3] = 1.0f
;
598 ctx
->viewport
.translate
[0] = 0.5f
* ctx
->dst_width
;
599 ctx
->viewport
.translate
[1] = 0.5f
* ctx
->dst_height
;
600 ctx
->viewport
.translate
[2] = 0.0f
;
601 ctx
->viewport
.translate
[3] = 0.0f
;
602 ctx
->base
.pipe
->set_viewport_state(ctx
->base
.pipe
, &ctx
->viewport
);
605 static void blitter_set_clear_color(struct blitter_context_priv
*ctx
,
606 const union pipe_color_union
*color
)
611 for (i
= 0; i
< 4; i
++) {
612 uint32_t *uiverts
= (uint32_t *)ctx
->vertices
[i
][1];
613 uiverts
[0] = color
->ui
[0];
614 uiverts
[1] = color
->ui
[1];
615 uiverts
[2] = color
->ui
[2];
616 uiverts
[3] = color
->ui
[3];
619 for (i
= 0; i
< 4; i
++) {
620 ctx
->vertices
[i
][1][0] = 0;
621 ctx
->vertices
[i
][1][1] = 0;
622 ctx
->vertices
[i
][1][2] = 0;
623 ctx
->vertices
[i
][1][3] = 0;
628 static void get_texcoords(struct pipe_sampler_view
*src
,
629 unsigned src_width0
, unsigned src_height0
,
630 int x1
, int y1
, int x2
, int y2
,
633 struct pipe_resource
*tex
= src
->texture
;
634 unsigned level
= src
->u
.tex
.first_level
;
635 boolean normalized
= tex
->target
!= PIPE_TEXTURE_RECT
&&
636 tex
->nr_samples
<= 1;
639 out
[0] = x1
/ (float)u_minify(src_width0
, level
);
640 out
[1] = y1
/ (float)u_minify(src_height0
, level
);
641 out
[2] = x2
/ (float)u_minify(src_width0
, level
);
642 out
[3] = y2
/ (float)u_minify(src_height0
, level
);
651 static void set_texcoords_in_vertices(const float coord
[4],
652 float *out
, unsigned stride
)
654 out
[0] = coord
[0]; /*t0.s*/
655 out
[1] = coord
[1]; /*t0.t*/
657 out
[0] = coord
[2]; /*t1.s*/
658 out
[1] = coord
[1]; /*t1.t*/
660 out
[0] = coord
[2]; /*t2.s*/
661 out
[1] = coord
[3]; /*t2.t*/
663 out
[0] = coord
[0]; /*t3.s*/
664 out
[1] = coord
[3]; /*t3.t*/
667 static void blitter_set_texcoords(struct blitter_context_priv
*ctx
,
668 struct pipe_sampler_view
*src
,
669 unsigned src_width0
, unsigned src_height0
,
670 unsigned layer
, unsigned sample
,
671 int x1
, int y1
, int x2
, int y2
)
675 float face_coord
[4][2];
677 get_texcoords(src
, src_width0
, src_height0
, x1
, y1
, x2
, y2
, coord
);
679 if (src
->texture
->target
== PIPE_TEXTURE_CUBE
||
680 src
->texture
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
681 set_texcoords_in_vertices(coord
, &face_coord
[0][0], 2);
682 util_map_texcoords2d_onto_cubemap(layer
% 6,
683 /* pointer, stride in floats */
684 &face_coord
[0][0], 2,
685 &ctx
->vertices
[0][1][0], 8);
687 set_texcoords_in_vertices(coord
, &ctx
->vertices
[0][1][0], 8);
691 switch (src
->texture
->target
) {
692 case PIPE_TEXTURE_3D
:
694 float r
= layer
/ (float)u_minify(src
->texture
->depth0
,
695 src
->u
.tex
.first_level
);
696 for (i
= 0; i
< 4; i
++)
697 ctx
->vertices
[i
][1][2] = r
; /*r*/
701 case PIPE_TEXTURE_1D_ARRAY
:
702 for (i
= 0; i
< 4; i
++)
703 ctx
->vertices
[i
][1][1] = (float) layer
; /*t*/
706 case PIPE_TEXTURE_2D_ARRAY
:
707 for (i
= 0; i
< 4; i
++) {
708 ctx
->vertices
[i
][1][2] = (float) layer
; /*r*/
709 ctx
->vertices
[i
][1][3] = (float) sample
; /*q*/
713 case PIPE_TEXTURE_CUBE_ARRAY
:
714 for (i
= 0; i
< 4; i
++)
715 ctx
->vertices
[i
][1][3] = (float) (layer
/ 6); /*w*/
718 case PIPE_TEXTURE_2D
:
719 for (i
= 0; i
< 4; i
++) {
720 ctx
->vertices
[i
][1][3] = (float) sample
; /*r*/
728 static void blitter_set_dst_dimensions(struct blitter_context_priv
*ctx
,
729 unsigned width
, unsigned height
)
731 ctx
->dst_width
= width
;
732 ctx
->dst_height
= height
;
735 static void *blitter_get_fs_col(struct blitter_context_priv
*ctx
,
736 unsigned num_cbufs
, boolean int_format
)
738 struct pipe_context
*pipe
= ctx
->base
.pipe
;
740 assert(num_cbufs
<= PIPE_MAX_COLOR_BUFS
);
743 if (!ctx
->fs_col_int
[num_cbufs
])
744 ctx
->fs_col_int
[num_cbufs
] =
745 util_make_fragment_cloneinput_shader(pipe
, num_cbufs
,
746 TGSI_SEMANTIC_GENERIC
,
747 TGSI_INTERPOLATE_CONSTANT
);
748 return ctx
->fs_col_int
[num_cbufs
];
750 if (!ctx
->fs_col
[num_cbufs
])
751 ctx
->fs_col
[num_cbufs
] =
752 util_make_fragment_cloneinput_shader(pipe
, num_cbufs
,
753 TGSI_SEMANTIC_GENERIC
,
754 TGSI_INTERPOLATE_LINEAR
);
755 return ctx
->fs_col
[num_cbufs
];
759 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv
*ctx
,
760 enum pipe_texture_target target
,
763 struct pipe_context
*pipe
= ctx
->base
.pipe
;
765 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
767 if (nr_samples
> 1) {
768 void **shader
= &ctx
->fs_texfetch_col_msaa
[target
];
770 /* Create the fragment shader on-demand. */
772 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
,
775 *shader
= util_make_fs_blit_msaa_color(pipe
, tgsi_tex
);
780 void **shader
= &ctx
->fs_texfetch_col
[target
];
782 /* Create the fragment shader on-demand. */
784 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
787 util_make_fragment_tex_shader(pipe
, tgsi_tex
,
788 TGSI_INTERPOLATE_LINEAR
);
796 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv
*ctx
,
797 enum pipe_texture_target target
,
800 struct pipe_context
*pipe
= ctx
->base
.pipe
;
802 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
804 if (nr_samples
> 1) {
805 void **shader
= &ctx
->fs_texfetch_depth_msaa
[target
];
807 /* Create the fragment shader on-demand. */
809 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
,
813 util_make_fs_blit_msaa_depth(pipe
, tgsi_tex
);
818 void **shader
= &ctx
->fs_texfetch_depth
[target
];
820 /* Create the fragment shader on-demand. */
822 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
825 util_make_fragment_tex_shader_writedepth(pipe
, tgsi_tex
,
826 TGSI_INTERPOLATE_LINEAR
);
834 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv
*ctx
,
835 enum pipe_texture_target target
,
838 struct pipe_context
*pipe
= ctx
->base
.pipe
;
840 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
842 if (nr_samples
> 1) {
843 void **shader
= &ctx
->fs_texfetch_depthstencil_msaa
[target
];
845 /* Create the fragment shader on-demand. */
847 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
,
851 util_make_fs_blit_msaa_depthstencil(pipe
, tgsi_tex
);
856 void **shader
= &ctx
->fs_texfetch_depthstencil
[target
];
858 /* Create the fragment shader on-demand. */
860 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
863 util_make_fragment_tex_shader_writedepthstencil(pipe
, tgsi_tex
,
864 TGSI_INTERPOLATE_LINEAR
);
872 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv
*ctx
,
873 enum pipe_texture_target target
,
876 struct pipe_context
*pipe
= ctx
->base
.pipe
;
878 assert(target
< PIPE_MAX_TEXTURE_TYPES
);
880 if (nr_samples
> 1) {
881 void **shader
= &ctx
->fs_texfetch_stencil_msaa
[target
];
883 /* Create the fragment shader on-demand. */
885 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
,
889 util_make_fs_blit_msaa_stencil(pipe
, tgsi_tex
);
894 void **shader
= &ctx
->fs_texfetch_stencil
[target
];
896 /* Create the fragment shader on-demand. */
898 unsigned tgsi_tex
= util_pipe_tex_to_tgsi_tex(target
, 0);
901 util_make_fragment_tex_shader_writestencil(pipe
, tgsi_tex
,
902 TGSI_INTERPOLATE_LINEAR
);
909 void util_blitter_cache_all_shaders(struct blitter_context
*blitter
)
911 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
912 struct pipe_screen
*screen
= blitter
->pipe
->screen
;
913 unsigned num_cbufs
, i
, target
, max_samples
;
914 boolean has_arraytex
, has_cubearraytex
;
916 num_cbufs
= MAX2(screen
->get_param(screen
,
917 PIPE_CAP_MAX_RENDER_TARGETS
), 1);
918 max_samples
= ctx
->has_texture_multisample
? 2 : 1;
919 has_arraytex
= screen
->get_param(screen
,
920 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS
) != 0;
921 has_cubearraytex
= screen
->get_param(screen
,
922 PIPE_CAP_CUBE_MAP_ARRAY
) != 0;
924 for (i
= 0; i
< num_cbufs
; i
++) {
925 blitter_get_fs_col(ctx
, i
, FALSE
);
926 blitter_get_fs_col(ctx
, i
, TRUE
);
929 /* It only matters if i <= 1 or > 1. */
930 for (i
= 1; i
<= max_samples
; i
++) {
931 for (target
= PIPE_TEXTURE_1D
; target
< PIPE_MAX_TEXTURE_TYPES
; target
++) {
933 (target
== PIPE_TEXTURE_1D_ARRAY
||
934 target
== PIPE_TEXTURE_2D_ARRAY
)) {
937 if (!has_cubearraytex
&&
938 (target
== PIPE_TEXTURE_CUBE_ARRAY
))
942 (target
!= PIPE_TEXTURE_2D
&&
943 target
!= PIPE_TEXTURE_2D_ARRAY
))
946 blitter_get_fs_texfetch_col(ctx
, target
, i
);
947 blitter_get_fs_texfetch_depth(ctx
, target
, i
);
948 if (ctx
->has_stencil_export
) {
949 blitter_get_fs_texfetch_depthstencil(ctx
, target
, i
);
950 blitter_get_fs_texfetch_stencil(ctx
, target
, i
);
956 static void blitter_set_common_draw_rect_state(struct blitter_context_priv
*ctx
,
959 struct pipe_context
*pipe
= ctx
->base
.pipe
;
961 pipe
->bind_rasterizer_state(pipe
, scissor
? ctx
->rs_state_scissor
963 pipe
->bind_vs_state(pipe
, ctx
->vs
);
964 if (ctx
->has_geometry_shader
)
965 pipe
->bind_gs_state(pipe
, NULL
);
966 if (ctx
->has_stream_out
)
967 pipe
->set_stream_output_targets(pipe
, 0, NULL
, 0);
970 static void blitter_draw(struct blitter_context_priv
*ctx
,
971 int x1
, int y1
, int x2
, int y2
, float depth
)
973 struct pipe_resource
*buf
= NULL
;
976 blitter_set_rectangle(ctx
, x1
, y1
, x2
, y2
, depth
);
978 u_upload_data(ctx
->upload
, 0, sizeof(ctx
->vertices
), ctx
->vertices
,
980 u_upload_unmap(ctx
->upload
);
981 util_draw_vertex_buffer(ctx
->base
.pipe
, NULL
, buf
, ctx
->base
.vb_slot
,
982 offset
, PIPE_PRIM_TRIANGLE_FAN
, 4, 2);
983 pipe_resource_reference(&buf
, NULL
);
986 void util_blitter_draw_rectangle(struct blitter_context
*blitter
,
987 int x1
, int y1
, int x2
, int y2
, float depth
,
988 enum blitter_attrib_type type
,
989 const union pipe_color_union
*attrib
)
991 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
994 case UTIL_BLITTER_ATTRIB_COLOR
:
995 blitter_set_clear_color(ctx
, attrib
);
998 case UTIL_BLITTER_ATTRIB_TEXCOORD
:
999 set_texcoords_in_vertices(attrib
->f
, &ctx
->vertices
[0][1][0], 8);
1005 blitter_draw(ctx
, x1
, y1
, x2
, y2
, depth
);
1008 static void util_blitter_clear_custom(struct blitter_context
*blitter
,
1009 unsigned width
, unsigned height
,
1011 unsigned clear_buffers
,
1012 enum pipe_format cbuf_format
,
1013 const union pipe_color_union
*color
,
1014 double depth
, unsigned stencil
,
1015 void *custom_blend
, void *custom_dsa
)
1017 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1018 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1019 struct pipe_stencil_ref sr
= { { 0 } };
1020 boolean int_format
= util_format_is_pure_integer(cbuf_format
);
1021 assert(num_cbufs
<= PIPE_MAX_COLOR_BUFS
);
1023 blitter_set_running_flag(ctx
);
1024 blitter_check_saved_vertex_states(ctx
);
1025 blitter_check_saved_fragment_states(ctx
);
1026 blitter_disable_render_cond(ctx
);
1030 pipe
->bind_blend_state(pipe
, custom_blend
);
1031 } else if (clear_buffers
& PIPE_CLEAR_COLOR
) {
1032 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
]);
1034 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1038 pipe
->bind_depth_stencil_alpha_state(pipe
, custom_dsa
);
1039 } else if ((clear_buffers
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1040 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1041 } else if (clear_buffers
& PIPE_CLEAR_DEPTH
) {
1042 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1043 } else if (clear_buffers
& PIPE_CLEAR_STENCIL
) {
1044 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1046 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1049 sr
.ref_value
[0] = stencil
& 0xff;
1050 pipe
->set_stencil_ref(pipe
, &sr
);
1052 if (util_format_is_pure_sint(cbuf_format
)) {
1053 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_sint_state
);
1054 } else if (util_format_is_pure_uint(cbuf_format
)) {
1055 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_uint_state
);
1057 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1059 ctx
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, num_cbufs
, int_format
));
1060 pipe
->set_sample_mask(pipe
, ~0);
1062 blitter_set_common_draw_rect_state(ctx
, FALSE
);
1063 blitter_set_dst_dimensions(ctx
, width
, height
);
1064 blitter
->draw_rectangle(blitter
, 0, 0, width
, height
, (float) depth
,
1065 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1067 blitter_restore_vertex_states(ctx
);
1068 blitter_restore_fragment_states(ctx
);
1069 blitter_restore_render_cond(ctx
);
1070 blitter_unset_running_flag(ctx
);
1073 void util_blitter_clear(struct blitter_context
*blitter
,
1074 unsigned width
, unsigned height
,
1076 unsigned clear_buffers
,
1077 enum pipe_format cbuf_format
,
1078 const union pipe_color_union
*color
,
1079 double depth
, unsigned stencil
)
1081 util_blitter_clear_custom(blitter
, width
, height
, num_cbufs
,
1082 clear_buffers
, cbuf_format
, color
, depth
, stencil
,
1086 void util_blitter_custom_clear_depth(struct blitter_context
*blitter
,
1087 unsigned width
, unsigned height
,
1088 double depth
, void *custom_dsa
)
1090 static const union pipe_color_union color
;
1091 util_blitter_clear_custom(blitter
, width
, height
, 0,
1092 0, PIPE_FORMAT_NONE
, &color
, depth
, 0, NULL
, custom_dsa
);
1095 void util_blitter_default_dst_texture(struct pipe_surface
*dst_templ
,
1096 struct pipe_resource
*dst
,
1100 memset(dst_templ
, 0, sizeof(*dst_templ
));
1101 dst_templ
->format
= util_format_linear(dst
->format
);
1102 dst_templ
->u
.tex
.level
= dstlevel
;
1103 dst_templ
->u
.tex
.first_layer
= dstz
;
1104 dst_templ
->u
.tex
.last_layer
= dstz
;
1107 static struct pipe_surface
*
1108 util_blitter_get_next_surface_layer(struct pipe_context
*pipe
,
1109 struct pipe_surface
*surf
)
1111 struct pipe_surface dst_templ
;
1113 memset(&dst_templ
, 0, sizeof(dst_templ
));
1114 dst_templ
.format
= surf
->format
;
1115 dst_templ
.u
.tex
.level
= surf
->u
.tex
.level
;
1116 dst_templ
.u
.tex
.first_layer
= surf
->u
.tex
.first_layer
+ 1;
1117 dst_templ
.u
.tex
.last_layer
= surf
->u
.tex
.last_layer
+ 1;
1119 return pipe
->create_surface(pipe
, surf
->texture
, &dst_templ
);
1122 void util_blitter_default_src_texture(struct pipe_sampler_view
*src_templ
,
1123 struct pipe_resource
*src
,
1126 memset(src_templ
, 0, sizeof(*src_templ
));
1127 src_templ
->format
= util_format_linear(src
->format
);
1128 src_templ
->u
.tex
.first_level
= srclevel
;
1129 src_templ
->u
.tex
.last_level
= srclevel
;
1130 src_templ
->u
.tex
.first_layer
= 0;
1131 src_templ
->u
.tex
.last_layer
=
1132 src
->target
== PIPE_TEXTURE_3D
? u_minify(src
->depth0
, srclevel
) - 1
1133 : src
->array_size
- 1;
1134 src_templ
->swizzle_r
= PIPE_SWIZZLE_RED
;
1135 src_templ
->swizzle_g
= PIPE_SWIZZLE_GREEN
;
1136 src_templ
->swizzle_b
= PIPE_SWIZZLE_BLUE
;
1137 src_templ
->swizzle_a
= PIPE_SWIZZLE_ALPHA
;
1140 static boolean
is_blit_generic_supported(struct blitter_context
*blitter
,
1141 const struct pipe_resource
*dst
,
1142 enum pipe_format dst_format
,
1143 const struct pipe_resource
*src
,
1144 enum pipe_format src_format
,
1147 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1148 struct pipe_screen
*screen
= ctx
->base
.pipe
->screen
;
1152 const struct util_format_description
*desc
=
1153 util_format_description(dst_format
);
1154 boolean dst_has_stencil
= util_format_has_stencil(desc
);
1156 /* Stencil export must be supported for stencil copy. */
1157 if ((mask
& PIPE_MASK_S
) && dst_has_stencil
&&
1158 !ctx
->has_stencil_export
) {
1162 if (dst_has_stencil
|| util_format_has_depth(desc
))
1163 bind
= PIPE_BIND_DEPTH_STENCIL
;
1165 bind
= PIPE_BIND_RENDER_TARGET
;
1167 if (!screen
->is_format_supported(screen
, dst_format
, dst
->target
,
1168 dst
->nr_samples
, bind
)) {
1174 if (src
->nr_samples
> 1 && !ctx
->has_texture_multisample
) {
1178 if (!screen
->is_format_supported(screen
, src_format
, src
->target
,
1179 src
->nr_samples
, PIPE_BIND_SAMPLER_VIEW
)) {
1183 /* Check stencil sampler support for stencil copy. */
1184 if (mask
& PIPE_MASK_S
) {
1185 if (util_format_has_stencil(util_format_description(src_format
))) {
1186 enum pipe_format stencil_format
=
1187 util_format_stencil_only(src_format
);
1188 assert(stencil_format
!= PIPE_FORMAT_NONE
);
1190 if (stencil_format
!= src_format
&&
1191 !screen
->is_format_supported(screen
, stencil_format
,
1192 src
->target
, src
->nr_samples
,
1193 PIPE_BIND_SAMPLER_VIEW
)) {
1203 boolean
util_blitter_is_copy_supported(struct blitter_context
*blitter
,
1204 const struct pipe_resource
*dst
,
1205 const struct pipe_resource
*src
,
1208 return is_blit_generic_supported(blitter
, dst
, dst
->format
,
1209 src
, src
->format
, mask
);
1212 boolean
util_blitter_is_blit_supported(struct blitter_context
*blitter
,
1213 const struct pipe_blit_info
*info
)
1215 return is_blit_generic_supported(blitter
,
1216 info
->dst
.resource
, info
->dst
.format
,
1217 info
->src
.resource
, info
->src
.format
,
1221 void util_blitter_copy_texture(struct blitter_context
*blitter
,
1222 struct pipe_resource
*dst
,
1224 unsigned dstx
, unsigned dsty
, unsigned dstz
,
1225 struct pipe_resource
*src
,
1227 const struct pipe_box
*srcbox
, unsigned mask
,
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_surface
*dst_view
, dst_templ
;
1233 struct pipe_sampler_view src_templ
, *src_view
;
1234 struct pipe_box dstbox
;
1237 assert(src
->target
< PIPE_MAX_TEXTURE_TYPES
);
1239 u_box_3d(dstx
, dsty
, dstz
, abs(srcbox
->width
), abs(srcbox
->height
),
1240 abs(srcbox
->depth
), &dstbox
);
1242 /* Initialize the surface. */
1243 util_blitter_default_dst_texture(&dst_templ
, dst
, dst_level
, dstz
);
1244 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1246 /* Initialize the sampler view. */
1247 util_blitter_default_src_texture(&src_templ
, src
, src_level
);
1248 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1251 util_blitter_blit_generic(blitter
, dst_view
, &dstbox
,
1252 src_view
, srcbox
, src
->width0
, src
->height0
,
1253 mask
, PIPE_TEX_FILTER_NEAREST
, NULL
,
1256 pipe_surface_reference(&dst_view
, NULL
);
1257 pipe_sampler_view_reference(&src_view
, NULL
);
1260 void util_blitter_blit_generic(struct blitter_context
*blitter
,
1261 struct pipe_surface
*dst
,
1262 const struct pipe_box
*dstbox
,
1263 struct pipe_sampler_view
*src
,
1264 const struct pipe_box
*srcbox
,
1265 unsigned src_width0
, unsigned src_height0
,
1266 unsigned mask
, unsigned filter
,
1267 const struct pipe_scissor_state
*scissor
,
1268 boolean copy_all_samples
)
1270 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1271 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1272 struct pipe_framebuffer_state fb_state
;
1273 enum pipe_texture_target src_target
= src
->texture
->target
;
1274 unsigned src_samples
= src
->texture
->nr_samples
;
1275 boolean has_depth
, has_stencil
, has_color
;
1276 boolean blit_stencil
, blit_depth
, blit_color
;
1277 void *sampler_state
;
1278 const struct util_format_description
*src_desc
=
1279 util_format_description(src
->format
);
1280 const struct util_format_description
*dst_desc
=
1281 util_format_description(dst
->format
);
1283 has_color
= src_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
&&
1284 dst_desc
->colorspace
!= UTIL_FORMAT_COLORSPACE_ZS
;
1285 has_depth
= util_format_has_depth(src_desc
) &&
1286 util_format_has_depth(dst_desc
);
1287 has_stencil
= util_format_has_stencil(src_desc
) &&
1288 util_format_has_stencil(dst_desc
);
1290 blit_color
= has_color
&& (mask
& PIPE_MASK_RGBA
);
1291 blit_depth
= has_depth
&& (mask
& PIPE_MASK_Z
);
1292 blit_stencil
= has_stencil
&& (mask
& PIPE_MASK_S
) &&
1293 ctx
->has_stencil_export
;
1295 if (!blit_stencil
&& !blit_depth
&& !blit_color
) {
1299 /* Check whether the states are properly saved. */
1300 blitter_set_running_flag(ctx
);
1301 blitter_check_saved_vertex_states(ctx
);
1302 blitter_check_saved_fragment_states(ctx
);
1303 blitter_check_saved_textures(ctx
);
1304 blitter_check_saved_fb_state(ctx
);
1305 blitter_disable_render_cond(ctx
);
1307 /* Initialize framebuffer state. */
1308 fb_state
.width
= dst
->width
;
1309 fb_state
.height
= dst
->height
;
1310 fb_state
.nr_cbufs
= blit_depth
|| blit_stencil
? 0 : 1;
1311 fb_state
.cbufs
[0] = NULL
;
1312 fb_state
.zsbuf
= NULL
;
1314 if (blit_depth
|| blit_stencil
) {
1315 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1317 if (blit_depth
&& blit_stencil
) {
1318 pipe
->bind_depth_stencil_alpha_state(pipe
,
1319 ctx
->dsa_write_depth_stencil
);
1320 ctx
->bind_fs_state(pipe
,
1321 blitter_get_fs_texfetch_depthstencil(ctx
, src_target
,
1323 } else if (blit_depth
) {
1324 pipe
->bind_depth_stencil_alpha_state(pipe
,
1325 ctx
->dsa_write_depth_keep_stencil
);
1326 ctx
->bind_fs_state(pipe
,
1327 blitter_get_fs_texfetch_depth(ctx
, src_target
,
1329 } else { /* is_stencil */
1330 pipe
->bind_depth_stencil_alpha_state(pipe
,
1331 ctx
->dsa_keep_depth_write_stencil
);
1332 ctx
->bind_fs_state(pipe
,
1333 blitter_get_fs_texfetch_stencil(ctx
, src_target
,
1338 pipe
->bind_blend_state(pipe
, ctx
->blend
[mask
& PIPE_MASK_RGBA
]);
1339 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1340 ctx
->bind_fs_state(pipe
,
1341 blitter_get_fs_texfetch_col(ctx
, src_target
,
1345 /* Set the linear filter only for scaled color non-MSAA blits. */
1346 if (filter
== PIPE_TEX_FILTER_LINEAR
&&
1347 !blit_depth
&& !blit_stencil
&&
1349 (dstbox
->width
!= abs(srcbox
->width
) ||
1350 dstbox
->height
!= abs(srcbox
->height
))) {
1351 if (src_target
== PIPE_TEXTURE_RECT
) {
1352 sampler_state
= ctx
->sampler_state_rect_linear
;
1354 sampler_state
= ctx
->sampler_state_linear
;
1357 if (src_target
== PIPE_TEXTURE_RECT
) {
1358 sampler_state
= ctx
->sampler_state_rect
;
1360 sampler_state
= ctx
->sampler_state
;
1365 if (blit_depth
&& blit_stencil
) {
1366 /* Setup two samplers, one for depth and the other one for stencil. */
1367 struct pipe_sampler_view templ
;
1368 struct pipe_sampler_view
*views
[2];
1369 void *samplers
[2] = {sampler_state
, sampler_state
};
1372 templ
.format
= util_format_stencil_only(templ
.format
);
1373 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1376 views
[1] = pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1378 pipe
->set_fragment_sampler_views(pipe
, 2, views
);
1379 pipe
->bind_fragment_sampler_states(pipe
, 2, samplers
);
1381 pipe_sampler_view_reference(&views
[1], NULL
);
1382 } else if (blit_stencil
) {
1383 /* Set a stencil-only sampler view for it not to sample depth instead. */
1384 struct pipe_sampler_view templ
;
1385 struct pipe_sampler_view
*view
;
1388 templ
.format
= util_format_stencil_only(templ
.format
);
1389 assert(templ
.format
!= PIPE_FORMAT_NONE
);
1391 view
= pipe
->create_sampler_view(pipe
, src
->texture
, &templ
);
1393 pipe
->set_fragment_sampler_views(pipe
, 1, &view
);
1394 pipe
->bind_fragment_sampler_states(pipe
, 1, &sampler_state
);
1396 pipe_sampler_view_reference(&view
, NULL
);
1398 pipe
->set_fragment_sampler_views(pipe
, 1, &src
);
1399 pipe
->bind_fragment_sampler_states(pipe
, 1, &sampler_state
);
1402 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1404 pipe
->set_scissor_state(pipe
, scissor
);
1407 blitter_set_common_draw_rect_state(ctx
, scissor
!= NULL
);
1408 blitter_set_dst_dimensions(ctx
, dst
->width
, dst
->height
);
1410 if ((src_target
== PIPE_TEXTURE_1D
||
1411 src_target
== PIPE_TEXTURE_2D
||
1412 src_target
== PIPE_TEXTURE_RECT
) &&
1414 /* Draw the quad with the draw_rectangle callback. */
1416 /* Set texture coordinates. - use a pipe color union
1417 * for interface purposes.
1418 * XXX pipe_color_union is a wrong name since we use that to set
1419 * texture coordinates too.
1421 union pipe_color_union coord
;
1422 get_texcoords(src
, src_width0
, src_height0
, srcbox
->x
, srcbox
->y
,
1423 srcbox
->x
+srcbox
->width
, srcbox
->y
+srcbox
->height
, coord
.f
);
1425 /* Set framebuffer state. */
1426 if (blit_depth
|| blit_stencil
) {
1427 fb_state
.zsbuf
= dst
;
1429 fb_state
.cbufs
[0] = dst
;
1431 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1434 pipe
->set_sample_mask(pipe
, ~0);
1435 blitter
->draw_rectangle(blitter
, dstbox
->x
, dstbox
->y
,
1436 dstbox
->x
+ dstbox
->width
,
1437 dstbox
->y
+ dstbox
->height
, 0,
1438 UTIL_BLITTER_ATTRIB_TEXCOORD
, &coord
);
1440 /* Draw the quad with the generic codepath. */
1442 for (z
= 0; z
< dstbox
->depth
; z
++) {
1443 struct pipe_surface
*old
;
1445 /* Set framebuffer state. */
1446 if (blit_depth
|| blit_stencil
) {
1447 fb_state
.zsbuf
= dst
;
1449 fb_state
.cbufs
[0] = dst
;
1451 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1453 /* See if we need to blit a multisample or singlesample buffer. */
1454 if (copy_all_samples
&&
1455 src_samples
== dst
->texture
->nr_samples
&&
1456 dst
->texture
->nr_samples
> 1) {
1457 unsigned i
, max_sample
= MAX2(dst
->texture
->nr_samples
, 1) - 1;
1459 for (i
= 0; i
<= max_sample
; i
++) {
1460 pipe
->set_sample_mask(pipe
, 1 << i
);
1461 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1463 i
, srcbox
->x
, srcbox
->y
,
1464 srcbox
->x
+ srcbox
->width
,
1465 srcbox
->y
+ srcbox
->height
);
1466 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1467 dstbox
->x
+ dstbox
->width
,
1468 dstbox
->y
+ dstbox
->height
, 0);
1471 pipe
->set_sample_mask(pipe
, ~0);
1472 blitter_set_texcoords(ctx
, src
, src_width0
, src_height0
,
1474 srcbox
->x
, srcbox
->y
,
1475 srcbox
->x
+ srcbox
->width
,
1476 srcbox
->y
+ srcbox
->height
);
1477 blitter_draw(ctx
, dstbox
->x
, dstbox
->y
,
1478 dstbox
->x
+ dstbox
->width
,
1479 dstbox
->y
+ dstbox
->height
, 0);
1482 /* Get the next surface or (if this is the last iteration)
1483 * just unreference the last one. */
1485 if (z
< dstbox
->depth
-1) {
1486 dst
= ctx
->base
.get_next_surface_layer(ctx
->base
.pipe
, dst
);
1489 pipe_surface_reference(&old
, NULL
);
1494 blitter_restore_vertex_states(ctx
);
1495 blitter_restore_fragment_states(ctx
);
1496 blitter_restore_textures(ctx
);
1497 blitter_restore_fb_state(ctx
);
1499 pipe
->set_scissor_state(pipe
, &ctx
->base
.saved_scissor
);
1501 blitter_restore_render_cond(ctx
);
1502 blitter_unset_running_flag(ctx
);
1506 util_blitter_blit(struct blitter_context
*blitter
,
1507 const struct pipe_blit_info
*info
)
1509 struct pipe_resource
*dst
= info
->dst
.resource
;
1510 struct pipe_resource
*src
= info
->src
.resource
;
1511 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1512 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1513 struct pipe_surface
*dst_view
, dst_templ
;
1514 struct pipe_sampler_view src_templ
, *src_view
;
1516 /* Initialize the surface. */
1517 util_blitter_default_dst_texture(&dst_templ
, dst
, info
->dst
.level
,
1519 dst_templ
.format
= info
->dst
.format
;
1520 dst_view
= pipe
->create_surface(pipe
, dst
, &dst_templ
);
1522 /* Initialize the sampler view. */
1523 util_blitter_default_src_texture(&src_templ
, src
, info
->src
.level
);
1524 src_templ
.format
= info
->src
.format
;
1525 src_view
= pipe
->create_sampler_view(pipe
, src
, &src_templ
);
1528 util_blitter_blit_generic(blitter
, dst_view
, &info
->dst
.box
,
1529 src_view
, &info
->src
.box
, src
->width0
, src
->height0
,
1530 info
->mask
, info
->filter
,
1531 info
->scissor_enable
? &info
->scissor
: NULL
, TRUE
);
1533 pipe_surface_reference(&dst_view
, NULL
);
1534 pipe_sampler_view_reference(&src_view
, NULL
);
1537 /* Clear a region of a color surface to a constant value. */
1538 void util_blitter_clear_render_target(struct blitter_context
*blitter
,
1539 struct pipe_surface
*dstsurf
,
1540 const union pipe_color_union
*color
,
1541 unsigned dstx
, unsigned dsty
,
1542 unsigned width
, unsigned height
)
1544 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1545 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1546 struct pipe_framebuffer_state fb_state
;
1548 assert(dstsurf
->texture
);
1549 if (!dstsurf
->texture
)
1552 /* check the saved state */
1553 blitter_set_running_flag(ctx
);
1554 blitter_check_saved_vertex_states(ctx
);
1555 blitter_check_saved_fragment_states(ctx
);
1556 blitter_check_saved_fb_state(ctx
);
1557 blitter_disable_render_cond(ctx
);
1560 pipe
->bind_blend_state(pipe
, ctx
->blend
[PIPE_MASK_RGBA
]);
1561 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1562 ctx
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, 1, FALSE
));
1563 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1565 /* set a framebuffer state */
1566 fb_state
.width
= dstsurf
->width
;
1567 fb_state
.height
= dstsurf
->height
;
1568 fb_state
.nr_cbufs
= 1;
1569 fb_state
.cbufs
[0] = dstsurf
;
1571 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1572 pipe
->set_sample_mask(pipe
, ~0);
1574 blitter_set_common_draw_rect_state(ctx
, FALSE
);
1575 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1576 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
, 0,
1577 UTIL_BLITTER_ATTRIB_COLOR
, color
);
1579 blitter_restore_vertex_states(ctx
);
1580 blitter_restore_fragment_states(ctx
);
1581 blitter_restore_fb_state(ctx
);
1582 blitter_restore_render_cond(ctx
);
1583 blitter_unset_running_flag(ctx
);
1586 /* Clear a region of a depth stencil surface. */
1587 void util_blitter_clear_depth_stencil(struct blitter_context
*blitter
,
1588 struct pipe_surface
*dstsurf
,
1589 unsigned clear_flags
,
1592 unsigned dstx
, unsigned dsty
,
1593 unsigned width
, unsigned height
)
1595 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1596 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1597 struct pipe_framebuffer_state fb_state
;
1598 struct pipe_stencil_ref sr
= { { 0 } };
1600 assert(dstsurf
->texture
);
1601 if (!dstsurf
->texture
)
1604 /* check the saved state */
1605 blitter_set_running_flag(ctx
);
1606 blitter_check_saved_vertex_states(ctx
);
1607 blitter_check_saved_fragment_states(ctx
);
1608 blitter_check_saved_fb_state(ctx
);
1609 blitter_disable_render_cond(ctx
);
1612 pipe
->bind_blend_state(pipe
, ctx
->blend
[0]);
1613 if ((clear_flags
& PIPE_CLEAR_DEPTHSTENCIL
) == PIPE_CLEAR_DEPTHSTENCIL
) {
1614 sr
.ref_value
[0] = stencil
& 0xff;
1615 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_stencil
);
1616 pipe
->set_stencil_ref(pipe
, &sr
);
1618 else if (clear_flags
& PIPE_CLEAR_DEPTH
) {
1619 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_write_depth_keep_stencil
);
1621 else if (clear_flags
& PIPE_CLEAR_STENCIL
) {
1622 sr
.ref_value
[0] = stencil
& 0xff;
1623 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_write_stencil
);
1624 pipe
->set_stencil_ref(pipe
, &sr
);
1627 /* hmm that should be illegal probably, or make it a no-op somewhere */
1628 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1630 ctx
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, 0, FALSE
));
1631 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1633 /* set a framebuffer state */
1634 fb_state
.width
= dstsurf
->width
;
1635 fb_state
.height
= dstsurf
->height
;
1636 fb_state
.nr_cbufs
= 0;
1637 fb_state
.cbufs
[0] = 0;
1638 fb_state
.zsbuf
= dstsurf
;
1639 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1640 pipe
->set_sample_mask(pipe
, ~0);
1642 blitter_set_common_draw_rect_state(ctx
, FALSE
);
1643 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1644 blitter
->draw_rectangle(blitter
, dstx
, dsty
, dstx
+width
, dsty
+height
,
1646 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
1648 blitter_restore_vertex_states(ctx
);
1649 blitter_restore_fragment_states(ctx
);
1650 blitter_restore_fb_state(ctx
);
1651 blitter_restore_render_cond(ctx
);
1652 blitter_unset_running_flag(ctx
);
1655 /* draw a rectangle across a region using a custom dsa stage - for r600g */
1656 void util_blitter_custom_depth_stencil(struct blitter_context
*blitter
,
1657 struct pipe_surface
*zsurf
,
1658 struct pipe_surface
*cbsurf
,
1659 unsigned sample_mask
,
1660 void *dsa_stage
, float depth
)
1662 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1663 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1664 struct pipe_framebuffer_state fb_state
;
1666 assert(zsurf
->texture
);
1667 if (!zsurf
->texture
)
1670 /* check the saved state */
1671 blitter_set_running_flag(ctx
);
1672 blitter_check_saved_vertex_states(ctx
);
1673 blitter_check_saved_fragment_states(ctx
);
1674 blitter_check_saved_fb_state(ctx
);
1675 blitter_disable_render_cond(ctx
);
1678 pipe
->bind_blend_state(pipe
, cbsurf
? ctx
->blend
[PIPE_MASK_RGBA
] :
1680 pipe
->bind_depth_stencil_alpha_state(pipe
, dsa_stage
);
1681 ctx
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, 0, FALSE
));
1682 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1684 /* set a framebuffer state */
1685 fb_state
.width
= zsurf
->width
;
1686 fb_state
.height
= zsurf
->height
;
1687 fb_state
.nr_cbufs
= 1;
1689 fb_state
.cbufs
[0] = cbsurf
;
1690 fb_state
.nr_cbufs
= 1;
1692 fb_state
.cbufs
[0] = NULL
;
1693 fb_state
.nr_cbufs
= 0;
1695 fb_state
.zsbuf
= zsurf
;
1696 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1697 pipe
->set_sample_mask(pipe
, sample_mask
);
1699 blitter_set_common_draw_rect_state(ctx
, FALSE
);
1700 blitter_set_dst_dimensions(ctx
, zsurf
->width
, zsurf
->height
);
1701 blitter
->draw_rectangle(blitter
, 0, 0, zsurf
->width
, zsurf
->height
, depth
,
1702 UTIL_BLITTER_ATTRIB_NONE
, NULL
);
1704 blitter_restore_vertex_states(ctx
);
1705 blitter_restore_fragment_states(ctx
);
1706 blitter_restore_fb_state(ctx
);
1707 blitter_restore_render_cond(ctx
);
1708 blitter_unset_running_flag(ctx
);
1711 void util_blitter_copy_buffer(struct blitter_context
*blitter
,
1712 struct pipe_resource
*dst
,
1714 struct pipe_resource
*src
,
1718 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1719 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1720 struct pipe_vertex_buffer vb
;
1721 struct pipe_stream_output_target
*so_target
;
1723 if (srcx
>= src
->width0
||
1724 dstx
>= dst
->width0
) {
1727 if (srcx
+ size
> src
->width0
) {
1728 size
= src
->width0
- srcx
;
1730 if (dstx
+ size
> dst
->width0
) {
1731 size
= dst
->width0
- dstx
;
1734 /* Drivers not capable of Stream Out should not call this function
1735 * in the first place. */
1736 assert(ctx
->has_stream_out
);
1738 /* Some alignment is required. */
1739 if (srcx
% 4 != 0 || dstx
% 4 != 0 || size
% 4 != 0 ||
1740 !ctx
->has_stream_out
) {
1741 struct pipe_box box
;
1742 u_box_1d(srcx
, size
, &box
);
1743 util_resource_copy_region(pipe
, dst
, 0, dstx
, 0, 0, src
, 0, &box
);
1747 blitter_set_running_flag(ctx
);
1748 blitter_check_saved_vertex_states(ctx
);
1749 blitter_disable_render_cond(ctx
);
1752 vb
.buffer_offset
= srcx
;
1755 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1756 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state_readbuf
[0]);
1757 pipe
->bind_vs_state(pipe
, ctx
->vs_pos_only
);
1758 if (ctx
->has_geometry_shader
)
1759 pipe
->bind_gs_state(pipe
, NULL
);
1760 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
1762 so_target
= pipe
->create_stream_output_target(pipe
, dst
, dstx
, size
);
1763 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, 0);
1765 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
1767 blitter_restore_vertex_states(ctx
);
1768 blitter_restore_render_cond(ctx
);
1769 blitter_unset_running_flag(ctx
);
1770 pipe_so_target_reference(&so_target
, NULL
);
1773 void util_blitter_clear_buffer(struct blitter_context
*blitter
,
1774 struct pipe_resource
*dst
,
1775 unsigned offset
, unsigned size
,
1776 unsigned num_channels
,
1777 const union pipe_color_union
*clear_value
)
1779 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1780 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1781 struct pipe_vertex_buffer vb
= {0};
1782 struct pipe_stream_output_target
*so_target
;
1784 assert(num_channels
>= 1);
1785 assert(num_channels
<= 4);
1787 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
1789 * R600 uses this to initialize texture resources, so width0 might not be
1790 * what you think it is.
1793 /* Streamout is required. */
1794 if (!ctx
->has_stream_out
) {
1795 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
1799 /* Some alignment is required. */
1800 if (offset
% 4 != 0 || size
% 4 != 0) {
1801 assert(!"Bad alignment in util_blitter_clear_buffer()");
1805 u_upload_data(ctx
->upload
, 0, num_channels
*4, clear_value
,
1806 &vb
.buffer_offset
, &vb
.buffer
);
1809 blitter_set_running_flag(ctx
);
1810 blitter_check_saved_vertex_states(ctx
);
1811 blitter_disable_render_cond(ctx
);
1813 pipe
->set_vertex_buffers(pipe
, ctx
->base
.vb_slot
, 1, &vb
);
1814 pipe
->bind_vertex_elements_state(pipe
,
1815 ctx
->velem_state_readbuf
[num_channels
-1]);
1816 pipe
->bind_vs_state(pipe
, ctx
->vs_pos_only
);
1817 if (ctx
->has_geometry_shader
)
1818 pipe
->bind_gs_state(pipe
, NULL
);
1819 pipe
->bind_rasterizer_state(pipe
, ctx
->rs_discard_state
);
1821 so_target
= pipe
->create_stream_output_target(pipe
, dst
, offset
, size
);
1822 pipe
->set_stream_output_targets(pipe
, 1, &so_target
, 0);
1824 util_draw_arrays(pipe
, PIPE_PRIM_POINTS
, 0, size
/ 4);
1826 blitter_restore_vertex_states(ctx
);
1827 blitter_restore_render_cond(ctx
);
1828 blitter_unset_running_flag(ctx
);
1829 pipe_so_target_reference(&so_target
, NULL
);
1830 pipe_resource_reference(&vb
.buffer
, NULL
);
1833 /* probably radeon specific */
1834 void util_blitter_custom_resolve_color(struct blitter_context
*blitter
,
1835 struct pipe_resource
*dst
,
1838 struct pipe_resource
*src
,
1840 unsigned sample_mask
,
1842 enum pipe_format format
)
1844 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1845 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1846 struct pipe_framebuffer_state fb_state
;
1847 struct pipe_surface
*srcsurf
, *dstsurf
, surf_tmpl
;
1849 blitter_set_running_flag(ctx
);
1850 blitter_check_saved_vertex_states(ctx
);
1851 blitter_check_saved_fragment_states(ctx
);
1852 blitter_disable_render_cond(ctx
);
1855 pipe
->bind_blend_state(pipe
, custom_blend
);
1856 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1857 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1858 ctx
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, 1, FALSE
));
1859 pipe
->set_sample_mask(pipe
, sample_mask
);
1861 memset(&surf_tmpl
, 0, sizeof(surf_tmpl
));
1862 surf_tmpl
.format
= format
;
1863 surf_tmpl
.u
.tex
.level
= dst_level
;
1864 surf_tmpl
.u
.tex
.first_layer
= dst_layer
;
1865 surf_tmpl
.u
.tex
.last_layer
= dst_layer
;
1867 dstsurf
= pipe
->create_surface(pipe
, dst
, &surf_tmpl
);
1869 surf_tmpl
.u
.tex
.level
= 0;
1870 surf_tmpl
.u
.tex
.first_layer
= src_layer
;
1871 surf_tmpl
.u
.tex
.last_layer
= src_layer
;
1873 srcsurf
= pipe
->create_surface(pipe
, src
, &surf_tmpl
);
1875 /* set a framebuffer state */
1876 fb_state
.width
= src
->width0
;
1877 fb_state
.height
= src
->height0
;
1878 fb_state
.nr_cbufs
= 2;
1879 fb_state
.cbufs
[0] = srcsurf
;
1880 fb_state
.cbufs
[1] = dstsurf
;
1881 fb_state
.zsbuf
= NULL
;
1882 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1884 blitter_set_common_draw_rect_state(ctx
, FALSE
);
1885 blitter_set_dst_dimensions(ctx
, src
->width0
, src
->height0
);
1886 blitter
->draw_rectangle(blitter
, 0, 0, src
->width0
, src
->height0
,
1888 blitter_restore_fb_state(ctx
);
1889 blitter_restore_vertex_states(ctx
);
1890 blitter_restore_fragment_states(ctx
);
1891 blitter_restore_render_cond(ctx
);
1892 blitter_unset_running_flag(ctx
);
1894 pipe_surface_reference(&srcsurf
, NULL
);
1895 pipe_surface_reference(&dstsurf
, NULL
);
1898 void util_blitter_custom_color(struct blitter_context
*blitter
,
1899 struct pipe_surface
*dstsurf
,
1902 struct blitter_context_priv
*ctx
= (struct blitter_context_priv
*)blitter
;
1903 struct pipe_context
*pipe
= ctx
->base
.pipe
;
1904 struct pipe_framebuffer_state fb_state
;
1906 assert(dstsurf
->texture
);
1907 if (!dstsurf
->texture
)
1910 /* check the saved state */
1911 blitter_set_running_flag(ctx
);
1912 blitter_check_saved_vertex_states(ctx
);
1913 blitter_check_saved_fragment_states(ctx
);
1914 blitter_check_saved_fb_state(ctx
);
1915 blitter_disable_render_cond(ctx
);
1918 pipe
->bind_blend_state(pipe
, custom_blend
? custom_blend
1919 : ctx
->blend
[PIPE_MASK_RGBA
]);
1920 pipe
->bind_depth_stencil_alpha_state(pipe
, ctx
->dsa_keep_depth_stencil
);
1921 ctx
->bind_fs_state(pipe
, blitter_get_fs_col(ctx
, 1, FALSE
));
1922 pipe
->bind_vertex_elements_state(pipe
, ctx
->velem_state
);
1923 pipe
->set_sample_mask(pipe
, (1ull << MAX2(1, dstsurf
->texture
->nr_samples
)) - 1);
1925 /* set a framebuffer state */
1926 fb_state
.width
= dstsurf
->width
;
1927 fb_state
.height
= dstsurf
->height
;
1928 fb_state
.nr_cbufs
= 1;
1929 fb_state
.cbufs
[0] = dstsurf
;
1931 pipe
->set_framebuffer_state(pipe
, &fb_state
);
1932 pipe
->set_sample_mask(pipe
, ~0);
1934 blitter_set_common_draw_rect_state(ctx
, FALSE
);
1935 blitter_set_dst_dimensions(ctx
, dstsurf
->width
, dstsurf
->height
);
1936 blitter
->draw_rectangle(blitter
, 0, 0, dstsurf
->width
, dstsurf
->height
,
1939 blitter_restore_vertex_states(ctx
);
1940 blitter_restore_fragment_states(ctx
);
1941 blitter_restore_fb_state(ctx
);
1942 blitter_restore_render_cond(ctx
);
1943 blitter_unset_running_flag(ctx
);