Merge ../mesa into vulkan
[mesa.git] / src / gallium / auxiliary / util / u_blitter.c
1 /**************************************************************************
2 *
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
4 *
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:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27 /**
28 * @file
29 * Blitter utility to facilitate acceleration of the clear, clear_render_target,
30 * clear_depth_stencil, resource_copy_region, and blit functions.
31 *
32 * @author Marek Olšák
33 */
34
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"
40
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"
51
52 #define INVALID_PTR ((void*)~0)
53
54 #define GET_CLEAR_BLEND_STATE_IDX(clear_buffers) \
55 ((clear_buffers) / PIPE_CLEAR_COLOR0)
56
57 #define NUM_RESOLVE_FRAG_SHADERS 5 /* MSAA 2x, 4x, 8x, 16x, 32x */
58 #define GET_MSAA_RESOLVE_FS_IDX(nr_samples) (util_logbase2(nr_samples)-1)
59
60 struct blitter_context_priv
61 {
62 struct blitter_context base;
63
64 struct u_upload_mgr *upload;
65
66 float vertices[4][2][4]; /**< {pos, color} or {pos, texcoord} */
67
68 /* Templates for various state objects. */
69
70 /* Constant state objects. */
71 /* Vertex shaders. */
72 void *vs; /**< Vertex shader which passes {pos, generic} to the output.*/
73 void *vs_pos_only[4]; /**< Vertex shader which passes pos to the output.*/
74 void *vs_layered; /**< Vertex shader which sets LAYER = INSTANCEID. */
75
76 /* Fragment shaders. */
77 void *fs_empty;
78 void *fs_write_one_cbuf;
79 void *fs_write_all_cbufs;
80
81 /* FS which outputs a color from a texture,
82 where the index is PIPE_TEXTURE_* to be sampled. */
83 void *fs_texfetch_col[PIPE_MAX_TEXTURE_TYPES];
84 void *fs_texfetch_col_uint[PIPE_MAX_TEXTURE_TYPES];
85 void *fs_texfetch_col_sint[PIPE_MAX_TEXTURE_TYPES];
86
87 /* FS which outputs a depth from a texture,
88 where the index is PIPE_TEXTURE_* to be sampled. */
89 void *fs_texfetch_depth[PIPE_MAX_TEXTURE_TYPES];
90 void *fs_texfetch_depthstencil[PIPE_MAX_TEXTURE_TYPES];
91 void *fs_texfetch_stencil[PIPE_MAX_TEXTURE_TYPES];
92
93 /* FS which outputs one sample from a multisample texture. */
94 void *fs_texfetch_col_msaa[PIPE_MAX_TEXTURE_TYPES];
95 void *fs_texfetch_col_msaa_uint[PIPE_MAX_TEXTURE_TYPES];
96 void *fs_texfetch_col_msaa_sint[PIPE_MAX_TEXTURE_TYPES];
97 void *fs_texfetch_depth_msaa[PIPE_MAX_TEXTURE_TYPES];
98 void *fs_texfetch_depthstencil_msaa[PIPE_MAX_TEXTURE_TYPES];
99 void *fs_texfetch_stencil_msaa[PIPE_MAX_TEXTURE_TYPES];
100
101 /* FS which outputs an average of all samples. */
102 void *fs_resolve[PIPE_MAX_TEXTURE_TYPES][NUM_RESOLVE_FRAG_SHADERS][2];
103 void *fs_resolve_sint[PIPE_MAX_TEXTURE_TYPES][NUM_RESOLVE_FRAG_SHADERS][2];
104 void *fs_resolve_uint[PIPE_MAX_TEXTURE_TYPES][NUM_RESOLVE_FRAG_SHADERS][2];
105
106 /* Blend state. */
107 void *blend[PIPE_MASK_RGBA+1][2]; /**< blend state with writemask */
108 void *blend_clear[GET_CLEAR_BLEND_STATE_IDX(PIPE_CLEAR_COLOR)+1];
109
110 /* Depth stencil alpha state. */
111 void *dsa_write_depth_stencil;
112 void *dsa_write_depth_keep_stencil;
113 void *dsa_keep_depth_stencil;
114 void *dsa_keep_depth_write_stencil;
115
116 /* Vertex elements states. */
117 void *velem_state;
118 void *velem_state_readbuf[4]; /**< X, XY, XYZ, XYZW */
119
120 /* Sampler state. */
121 void *sampler_state;
122 void *sampler_state_linear;
123 void *sampler_state_rect;
124 void *sampler_state_rect_linear;
125
126 /* Rasterizer state. */
127 void *rs_state, *rs_state_scissor, *rs_discard_state;
128
129 /* Viewport state. */
130 struct pipe_viewport_state viewport;
131
132 /* Destination surface dimensions. */
133 unsigned dst_width;
134 unsigned dst_height;
135
136 boolean has_geometry_shader;
137 boolean has_tessellation;
138 boolean has_layered;
139 boolean has_stream_out;
140 boolean has_stencil_export;
141 boolean has_texture_multisample;
142 boolean cached_all_shaders;
143
144 /* The Draw module overrides these functions.
145 * Always create the blitter before Draw. */
146 void (*bind_fs_state)(struct pipe_context *, void *);
147 void (*delete_fs_state)(struct pipe_context *, void *);
148 };
149
150 static struct pipe_surface *
151 util_blitter_get_next_surface_layer(struct pipe_context *pipe,
152 struct pipe_surface *surf);
153
154 struct blitter_context *util_blitter_create(struct pipe_context *pipe)
155 {
156 struct blitter_context_priv *ctx;
157 struct pipe_blend_state blend;
158 struct pipe_depth_stencil_alpha_state dsa;
159 struct pipe_rasterizer_state rs_state;
160 struct pipe_sampler_state sampler_state;
161 struct pipe_vertex_element velem[2];
162 unsigned i, j;
163
164 ctx = CALLOC_STRUCT(blitter_context_priv);
165 if (!ctx)
166 return NULL;
167
168 ctx->base.pipe = pipe;
169 ctx->base.draw_rectangle = util_blitter_draw_rectangle;
170 ctx->base.get_next_surface_layer = util_blitter_get_next_surface_layer;
171
172 ctx->bind_fs_state = pipe->bind_fs_state;
173 ctx->delete_fs_state = pipe->delete_fs_state;
174
175 /* init state objects for them to be considered invalid */
176 ctx->base.saved_blend_state = INVALID_PTR;
177 ctx->base.saved_dsa_state = INVALID_PTR;
178 ctx->base.saved_rs_state = INVALID_PTR;
179 ctx->base.saved_fs = INVALID_PTR;
180 ctx->base.saved_vs = INVALID_PTR;
181 ctx->base.saved_gs = INVALID_PTR;
182 ctx->base.saved_velem_state = INVALID_PTR;
183 ctx->base.saved_fb_state.nr_cbufs = ~0;
184 ctx->base.saved_num_sampler_views = ~0;
185 ctx->base.saved_num_sampler_states = ~0;
186 ctx->base.saved_num_so_targets = ~0;
187
188 ctx->has_geometry_shader =
189 pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_GEOMETRY,
190 PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0;
191
192 ctx->has_tessellation =
193 pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_TESS_CTRL,
194 PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0;
195
196 ctx->has_stream_out =
197 pipe->screen->get_param(pipe->screen,
198 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS) != 0;
199
200 ctx->has_stencil_export =
201 pipe->screen->get_param(pipe->screen,
202 PIPE_CAP_SHADER_STENCIL_EXPORT);
203
204 ctx->has_texture_multisample =
205 pipe->screen->get_param(pipe->screen, PIPE_CAP_TEXTURE_MULTISAMPLE);
206
207 /* blend state objects */
208 memset(&blend, 0, sizeof(blend));
209
210 for (i = 0; i <= PIPE_MASK_RGBA; i++) {
211 for (j = 0; j < 2; j++) {
212 memset(&blend.rt[0], 0, sizeof(blend.rt[0]));
213 blend.rt[0].colormask = i;
214 if (j) {
215 blend.rt[0].blend_enable = 1;
216 blend.rt[0].rgb_func = PIPE_BLEND_ADD;
217 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA;
218 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA;
219 blend.rt[0].alpha_func = PIPE_BLEND_ADD;
220 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA;
221 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA;
222 }
223 ctx->blend[i][j] = pipe->create_blend_state(pipe, &blend);
224 }
225 }
226
227 /* depth stencil alpha state objects */
228 memset(&dsa, 0, sizeof(dsa));
229 ctx->dsa_keep_depth_stencil =
230 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
231
232 dsa.depth.enabled = 1;
233 dsa.depth.writemask = 1;
234 dsa.depth.func = PIPE_FUNC_ALWAYS;
235 ctx->dsa_write_depth_keep_stencil =
236 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
237
238 dsa.stencil[0].enabled = 1;
239 dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
240 dsa.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
241 dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
242 dsa.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
243 dsa.stencil[0].valuemask = 0xff;
244 dsa.stencil[0].writemask = 0xff;
245 ctx->dsa_write_depth_stencil =
246 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
247
248 dsa.depth.enabled = 0;
249 dsa.depth.writemask = 0;
250 ctx->dsa_keep_depth_write_stencil =
251 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
252
253 /* sampler state */
254 memset(&sampler_state, 0, sizeof(sampler_state));
255 sampler_state.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
256 sampler_state.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
257 sampler_state.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
258 sampler_state.normalized_coords = 1;
259 ctx->sampler_state = pipe->create_sampler_state(pipe, &sampler_state);
260 sampler_state.normalized_coords = 0;
261 ctx->sampler_state_rect = pipe->create_sampler_state(pipe, &sampler_state);
262
263 sampler_state.min_img_filter = PIPE_TEX_FILTER_LINEAR;
264 sampler_state.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
265 sampler_state.normalized_coords = 1;
266 ctx->sampler_state_linear = pipe->create_sampler_state(pipe, &sampler_state);
267 sampler_state.normalized_coords = 0;
268 ctx->sampler_state_rect_linear = pipe->create_sampler_state(pipe, &sampler_state);
269
270 /* rasterizer state */
271 memset(&rs_state, 0, sizeof(rs_state));
272 rs_state.cull_face = PIPE_FACE_NONE;
273 rs_state.half_pixel_center = 1;
274 rs_state.bottom_edge_rule = 1;
275 rs_state.flatshade = 1;
276 rs_state.depth_clip = 1;
277 ctx->rs_state = pipe->create_rasterizer_state(pipe, &rs_state);
278
279 rs_state.scissor = 1;
280 ctx->rs_state_scissor = pipe->create_rasterizer_state(pipe, &rs_state);
281
282 if (ctx->has_stream_out) {
283 rs_state.scissor = 0;
284 rs_state.rasterizer_discard = 1;
285 ctx->rs_discard_state = pipe->create_rasterizer_state(pipe, &rs_state);
286 }
287
288 ctx->base.vb_slot = 0; /* 0 for now */
289
290 /* vertex elements states */
291 memset(&velem[0], 0, sizeof(velem[0]) * 2);
292 for (i = 0; i < 2; i++) {
293 velem[i].src_offset = i * 4 * sizeof(float);
294 velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
295 velem[i].vertex_buffer_index = ctx->base.vb_slot;
296 }
297 ctx->velem_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]);
298
299 if (ctx->has_stream_out) {
300 static enum pipe_format formats[4] = {
301 PIPE_FORMAT_R32_UINT,
302 PIPE_FORMAT_R32G32_UINT,
303 PIPE_FORMAT_R32G32B32_UINT,
304 PIPE_FORMAT_R32G32B32A32_UINT
305 };
306
307 for (i = 0; i < 4; i++) {
308 velem[0].src_format = formats[i];
309 velem[0].vertex_buffer_index = ctx->base.vb_slot;
310 ctx->velem_state_readbuf[i] =
311 pipe->create_vertex_elements_state(pipe, 1, &velem[0]);
312 }
313 }
314
315 ctx->has_layered =
316 pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_INSTANCEID) &&
317 pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT);
318
319 /* set invariant vertex coordinates */
320 for (i = 0; i < 4; i++)
321 ctx->vertices[i][0][3] = 1; /*v.w*/
322
323 ctx->upload = u_upload_create(pipe, 65536, PIPE_BIND_VERTEX_BUFFER,
324 PIPE_USAGE_STREAM);
325
326 return &ctx->base;
327 }
328
329 static void bind_vs_pos_only(struct blitter_context_priv *ctx,
330 unsigned num_so_channels)
331 {
332 struct pipe_context *pipe = ctx->base.pipe;
333 int index = num_so_channels ? num_so_channels - 1 : 0;
334
335 if (!ctx->vs_pos_only[index]) {
336 struct pipe_stream_output_info so;
337 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION };
338 const uint semantic_indices[] = { 0 };
339
340 memset(&so, 0, sizeof(so));
341 so.num_outputs = 1;
342 so.output[0].num_components = num_so_channels;
343 so.stride[0] = num_so_channels;
344
345 ctx->vs_pos_only[index] =
346 util_make_vertex_passthrough_shader_with_so(pipe, 1, semantic_names,
347 semantic_indices, FALSE,
348 &so);
349 }
350
351 pipe->bind_vs_state(pipe, ctx->vs_pos_only[index]);
352 }
353
354 static void bind_vs_passthrough(struct blitter_context_priv *ctx)
355 {
356 struct pipe_context *pipe = ctx->base.pipe;
357
358 if (!ctx->vs) {
359 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
360 TGSI_SEMANTIC_GENERIC };
361 const uint semantic_indices[] = { 0, 0 };
362 ctx->vs =
363 util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
364 semantic_indices, FALSE);
365 }
366
367 pipe->bind_vs_state(pipe, ctx->vs);
368 }
369
370 static void bind_vs_layered(struct blitter_context_priv *ctx)
371 {
372 struct pipe_context *pipe = ctx->base.pipe;
373
374 if (!ctx->vs_layered) {
375 ctx->vs_layered = util_make_layered_clear_vertex_shader(pipe);
376 }
377
378 pipe->bind_vs_state(pipe, ctx->vs_layered);
379 }
380
381 static void bind_fs_empty(struct blitter_context_priv *ctx)
382 {
383 struct pipe_context *pipe = ctx->base.pipe;
384
385 if (!ctx->fs_empty) {
386 assert(!ctx->cached_all_shaders);
387 ctx->fs_empty = util_make_empty_fragment_shader(pipe);
388 }
389
390 ctx->bind_fs_state(pipe, ctx->fs_empty);
391 }
392
393 static void bind_fs_write_one_cbuf(struct blitter_context_priv *ctx)
394 {
395 struct pipe_context *pipe = ctx->base.pipe;
396
397 if (!ctx->fs_write_one_cbuf) {
398 assert(!ctx->cached_all_shaders);
399 ctx->fs_write_one_cbuf =
400 util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
401 TGSI_INTERPOLATE_CONSTANT, FALSE);
402 }
403
404 ctx->bind_fs_state(pipe, ctx->fs_write_one_cbuf);
405 }
406
407 static void bind_fs_write_all_cbufs(struct blitter_context_priv *ctx)
408 {
409 struct pipe_context *pipe = ctx->base.pipe;
410
411 if (!ctx->fs_write_all_cbufs) {
412 assert(!ctx->cached_all_shaders);
413 ctx->fs_write_all_cbufs =
414 util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
415 TGSI_INTERPOLATE_CONSTANT, TRUE);
416 }
417
418 ctx->bind_fs_state(pipe, ctx->fs_write_all_cbufs);
419 }
420
421 void util_blitter_destroy(struct blitter_context *blitter)
422 {
423 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
424 struct pipe_context *pipe = blitter->pipe;
425 int i, j, f;
426
427 for (i = 0; i <= PIPE_MASK_RGBA; i++)
428 for (j = 0; j < 2; j++)
429 pipe->delete_blend_state(pipe, ctx->blend[i][j]);
430
431 for (i = 0; i < Elements(ctx->blend_clear); i++) {
432 if (ctx->blend_clear[i])
433 pipe->delete_blend_state(pipe, ctx->blend_clear[i]);
434 }
435 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
436 pipe->delete_depth_stencil_alpha_state(pipe,
437 ctx->dsa_write_depth_keep_stencil);
438 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
439 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
440
441 pipe->delete_rasterizer_state(pipe, ctx->rs_state);
442 pipe->delete_rasterizer_state(pipe, ctx->rs_state_scissor);
443 if (ctx->rs_discard_state)
444 pipe->delete_rasterizer_state(pipe, ctx->rs_discard_state);
445 if (ctx->vs)
446 pipe->delete_vs_state(pipe, ctx->vs);
447 for (i = 0; i < 4; i++)
448 if (ctx->vs_pos_only[i])
449 pipe->delete_vs_state(pipe, ctx->vs_pos_only[i]);
450 if (ctx->vs_layered)
451 pipe->delete_vs_state(pipe, ctx->vs_layered);
452 pipe->delete_vertex_elements_state(pipe, ctx->velem_state);
453 for (i = 0; i < 4; i++) {
454 if (ctx->velem_state_readbuf[i]) {
455 pipe->delete_vertex_elements_state(pipe, ctx->velem_state_readbuf[i]);
456 }
457 }
458
459 for (i = 0; i < PIPE_MAX_TEXTURE_TYPES; i++) {
460 if (ctx->fs_texfetch_col[i])
461 ctx->delete_fs_state(pipe, ctx->fs_texfetch_col[i]);
462 if (ctx->fs_texfetch_col_sint[i])
463 ctx->delete_fs_state(pipe, ctx->fs_texfetch_col_sint[i]);
464 if (ctx->fs_texfetch_col_uint[i])
465 ctx->delete_fs_state(pipe, ctx->fs_texfetch_col_uint[i]);
466 if (ctx->fs_texfetch_depth[i])
467 ctx->delete_fs_state(pipe, ctx->fs_texfetch_depth[i]);
468 if (ctx->fs_texfetch_depthstencil[i])
469 ctx->delete_fs_state(pipe, ctx->fs_texfetch_depthstencil[i]);
470 if (ctx->fs_texfetch_stencil[i])
471 ctx->delete_fs_state(pipe, ctx->fs_texfetch_stencil[i]);
472
473 if (ctx->fs_texfetch_col_msaa[i])
474 ctx->delete_fs_state(pipe, ctx->fs_texfetch_col_msaa[i]);
475 if (ctx->fs_texfetch_col_msaa_sint[i])
476 ctx->delete_fs_state(pipe, ctx->fs_texfetch_col_msaa_sint[i]);
477 if (ctx->fs_texfetch_col_msaa_uint[i])
478 ctx->delete_fs_state(pipe, ctx->fs_texfetch_col_msaa_uint[i]);
479 if (ctx->fs_texfetch_depth_msaa[i])
480 ctx->delete_fs_state(pipe, ctx->fs_texfetch_depth_msaa[i]);
481 if (ctx->fs_texfetch_depthstencil_msaa[i])
482 ctx->delete_fs_state(pipe, ctx->fs_texfetch_depthstencil_msaa[i]);
483 if (ctx->fs_texfetch_stencil_msaa[i])
484 ctx->delete_fs_state(pipe, ctx->fs_texfetch_stencil_msaa[i]);
485
486 for (j = 0; j< Elements(ctx->fs_resolve[i]); j++)
487 for (f = 0; f < 2; f++)
488 if (ctx->fs_resolve[i][j][f])
489 ctx->delete_fs_state(pipe, ctx->fs_resolve[i][j][f]);
490
491 for (j = 0; j< Elements(ctx->fs_resolve_sint[i]); j++)
492 for (f = 0; f < 2; f++)
493 if (ctx->fs_resolve_sint[i][j][f])
494 ctx->delete_fs_state(pipe, ctx->fs_resolve_sint[i][j][f]);
495
496 for (j = 0; j< Elements(ctx->fs_resolve_uint[i]); j++)
497 for (f = 0; f < 2; f++)
498 if (ctx->fs_resolve_uint[i][j][f])
499 ctx->delete_fs_state(pipe, ctx->fs_resolve_uint[i][j][f]);
500 }
501
502 if (ctx->fs_empty)
503 ctx->delete_fs_state(pipe, ctx->fs_empty);
504 if (ctx->fs_write_one_cbuf)
505 ctx->delete_fs_state(pipe, ctx->fs_write_one_cbuf);
506 if (ctx->fs_write_all_cbufs)
507 ctx->delete_fs_state(pipe, ctx->fs_write_all_cbufs);
508
509 pipe->delete_sampler_state(pipe, ctx->sampler_state_rect_linear);
510 pipe->delete_sampler_state(pipe, ctx->sampler_state_rect);
511 pipe->delete_sampler_state(pipe, ctx->sampler_state_linear);
512 pipe->delete_sampler_state(pipe, ctx->sampler_state);
513 u_upload_destroy(ctx->upload);
514 FREE(ctx);
515 }
516
517 void util_blitter_set_texture_multisample(struct blitter_context *blitter,
518 boolean supported)
519 {
520 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
521
522 ctx->has_texture_multisample = supported;
523 }
524
525 static void blitter_set_running_flag(struct blitter_context_priv *ctx)
526 {
527 if (ctx->base.running) {
528 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
529 __LINE__);
530 }
531 ctx->base.running = TRUE;
532 }
533
534 static void blitter_unset_running_flag(struct blitter_context_priv *ctx)
535 {
536 if (!ctx->base.running) {
537 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
538 __LINE__);
539 }
540 ctx->base.running = FALSE;
541 }
542
543 static void blitter_check_saved_vertex_states(struct blitter_context_priv *ctx)
544 {
545 assert(ctx->base.saved_velem_state != INVALID_PTR);
546 assert(ctx->base.saved_vs != INVALID_PTR);
547 assert(!ctx->has_geometry_shader || ctx->base.saved_gs != INVALID_PTR);
548 assert(!ctx->has_tessellation || ctx->base.saved_tcs != INVALID_PTR);
549 assert(!ctx->has_tessellation || ctx->base.saved_tes != INVALID_PTR);
550 assert(!ctx->has_stream_out || ctx->base.saved_num_so_targets != ~0);
551 assert(ctx->base.saved_rs_state != INVALID_PTR);
552 }
553
554 static void blitter_restore_vertex_states(struct blitter_context_priv *ctx)
555 {
556 struct pipe_context *pipe = ctx->base.pipe;
557 unsigned i;
558
559 /* Vertex buffer. */
560 pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1,
561 &ctx->base.saved_vertex_buffer);
562 pipe_resource_reference(&ctx->base.saved_vertex_buffer.buffer, NULL);
563
564 /* Vertex elements. */
565 pipe->bind_vertex_elements_state(pipe, ctx->base.saved_velem_state);
566 ctx->base.saved_velem_state = INVALID_PTR;
567
568 /* Vertex shader. */
569 pipe->bind_vs_state(pipe, ctx->base.saved_vs);
570 ctx->base.saved_vs = INVALID_PTR;
571
572 /* Geometry shader. */
573 if (ctx->has_geometry_shader) {
574 pipe->bind_gs_state(pipe, ctx->base.saved_gs);
575 ctx->base.saved_gs = INVALID_PTR;
576 }
577
578 if (ctx->has_tessellation) {
579 pipe->bind_tcs_state(pipe, ctx->base.saved_tcs);
580 pipe->bind_tes_state(pipe, ctx->base.saved_tes);
581 ctx->base.saved_tcs = INVALID_PTR;
582 ctx->base.saved_tes = INVALID_PTR;
583 }
584
585 /* Stream outputs. */
586 if (ctx->has_stream_out) {
587 unsigned offsets[PIPE_MAX_SO_BUFFERS];
588 for (i = 0; i < ctx->base.saved_num_so_targets; i++)
589 offsets[i] = (unsigned)-1;
590 pipe->set_stream_output_targets(pipe,
591 ctx->base.saved_num_so_targets,
592 ctx->base.saved_so_targets, offsets);
593
594 for (i = 0; i < ctx->base.saved_num_so_targets; i++)
595 pipe_so_target_reference(&ctx->base.saved_so_targets[i], NULL);
596
597 ctx->base.saved_num_so_targets = ~0;
598 }
599
600 /* Rasterizer. */
601 pipe->bind_rasterizer_state(pipe, ctx->base.saved_rs_state);
602 ctx->base.saved_rs_state = INVALID_PTR;
603 }
604
605 static void blitter_check_saved_fragment_states(struct blitter_context_priv *ctx)
606 {
607 assert(ctx->base.saved_fs != INVALID_PTR);
608 assert(ctx->base.saved_dsa_state != INVALID_PTR);
609 assert(ctx->base.saved_blend_state != INVALID_PTR);
610 }
611
612 static void blitter_restore_fragment_states(struct blitter_context_priv *ctx)
613 {
614 struct pipe_context *pipe = ctx->base.pipe;
615
616 /* Fragment shader. */
617 ctx->bind_fs_state(pipe, ctx->base.saved_fs);
618 ctx->base.saved_fs = INVALID_PTR;
619
620 /* Depth, stencil, alpha. */
621 pipe->bind_depth_stencil_alpha_state(pipe, ctx->base.saved_dsa_state);
622 ctx->base.saved_dsa_state = INVALID_PTR;
623
624 /* Blend state. */
625 pipe->bind_blend_state(pipe, ctx->base.saved_blend_state);
626 ctx->base.saved_blend_state = INVALID_PTR;
627
628 /* Sample mask. */
629 if (ctx->base.is_sample_mask_saved) {
630 pipe->set_sample_mask(pipe, ctx->base.saved_sample_mask);
631 ctx->base.is_sample_mask_saved = FALSE;
632 }
633
634 /* Miscellaneous states. */
635 /* XXX check whether these are saved and whether they need to be restored
636 * (depending on the operation) */
637 pipe->set_stencil_ref(pipe, &ctx->base.saved_stencil_ref);
638 pipe->set_viewport_states(pipe, 0, 1, &ctx->base.saved_viewport);
639 }
640
641 static void blitter_check_saved_fb_state(struct blitter_context_priv *ctx)
642 {
643 assert(ctx->base.saved_fb_state.nr_cbufs != ~0);
644 }
645
646 static void blitter_disable_render_cond(struct blitter_context_priv *ctx)
647 {
648 struct pipe_context *pipe = ctx->base.pipe;
649
650 if (ctx->base.saved_render_cond_query) {
651 pipe->render_condition(pipe, NULL, FALSE, 0);
652 }
653 }
654
655 static void blitter_restore_render_cond(struct blitter_context_priv *ctx)
656 {
657 struct pipe_context *pipe = ctx->base.pipe;
658
659 if (ctx->base.saved_render_cond_query) {
660 pipe->render_condition(pipe, ctx->base.saved_render_cond_query,
661 ctx->base.saved_render_cond_cond,
662 ctx->base.saved_render_cond_mode);
663 ctx->base.saved_render_cond_query = NULL;
664 }
665 }
666
667 static void blitter_restore_fb_state(struct blitter_context_priv *ctx)
668 {
669 struct pipe_context *pipe = ctx->base.pipe;
670
671 pipe->set_framebuffer_state(pipe, &ctx->base.saved_fb_state);
672 util_unreference_framebuffer_state(&ctx->base.saved_fb_state);
673 }
674
675 static void blitter_check_saved_textures(struct blitter_context_priv *ctx)
676 {
677 assert(ctx->base.saved_num_sampler_states != ~0);
678 assert(ctx->base.saved_num_sampler_views != ~0);
679 }
680
681 static void blitter_restore_textures(struct blitter_context_priv *ctx)
682 {
683 struct pipe_context *pipe = ctx->base.pipe;
684 unsigned i;
685
686 /* Fragment sampler states. */
687 pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
688 ctx->base.saved_num_sampler_states,
689 ctx->base.saved_sampler_states);
690
691 ctx->base.saved_num_sampler_states = ~0;
692
693 /* Fragment sampler views. */
694 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,
695 ctx->base.saved_num_sampler_views,
696 ctx->base.saved_sampler_views);
697
698 for (i = 0; i < ctx->base.saved_num_sampler_views; i++)
699 pipe_sampler_view_reference(&ctx->base.saved_sampler_views[i], NULL);
700
701 ctx->base.saved_num_sampler_views = ~0;
702 }
703
704 static void blitter_set_rectangle(struct blitter_context_priv *ctx,
705 int x1, int y1, int x2, int y2,
706 float depth)
707 {
708 int i;
709
710 /* set vertex positions */
711 ctx->vertices[0][0][0] = (float)x1 / ctx->dst_width * 2.0f - 1.0f; /*v0.x*/
712 ctx->vertices[0][0][1] = (float)y1 / ctx->dst_height * 2.0f - 1.0f; /*v0.y*/
713
714 ctx->vertices[1][0][0] = (float)x2 / ctx->dst_width * 2.0f - 1.0f; /*v1.x*/
715 ctx->vertices[1][0][1] = (float)y1 / ctx->dst_height * 2.0f - 1.0f; /*v1.y*/
716
717 ctx->vertices[2][0][0] = (float)x2 / ctx->dst_width * 2.0f - 1.0f; /*v2.x*/
718 ctx->vertices[2][0][1] = (float)y2 / ctx->dst_height * 2.0f - 1.0f; /*v2.y*/
719
720 ctx->vertices[3][0][0] = (float)x1 / ctx->dst_width * 2.0f - 1.0f; /*v3.x*/
721 ctx->vertices[3][0][1] = (float)y2 / ctx->dst_height * 2.0f - 1.0f; /*v3.y*/
722
723 for (i = 0; i < 4; i++)
724 ctx->vertices[i][0][2] = depth; /*z*/
725
726 /* viewport */
727 ctx->viewport.scale[0] = 0.5f * ctx->dst_width;
728 ctx->viewport.scale[1] = 0.5f * ctx->dst_height;
729 ctx->viewport.scale[2] = 1.0f;
730 ctx->viewport.translate[0] = 0.5f * ctx->dst_width;
731 ctx->viewport.translate[1] = 0.5f * ctx->dst_height;
732 ctx->viewport.translate[2] = 0.0f;
733 ctx->base.pipe->set_viewport_states(ctx->base.pipe, 0, 1, &ctx->viewport);
734 }
735
736 static void blitter_set_clear_color(struct blitter_context_priv *ctx,
737 const union pipe_color_union *color)
738 {
739 int i;
740
741 if (color) {
742 for (i = 0; i < 4; i++) {
743 uint32_t *uiverts = (uint32_t *)ctx->vertices[i][1];
744 uiverts[0] = color->ui[0];
745 uiverts[1] = color->ui[1];
746 uiverts[2] = color->ui[2];
747 uiverts[3] = color->ui[3];
748 }
749 } else {
750 for (i = 0; i < 4; i++) {
751 ctx->vertices[i][1][0] = 0;
752 ctx->vertices[i][1][1] = 0;
753 ctx->vertices[i][1][2] = 0;
754 ctx->vertices[i][1][3] = 0;
755 }
756 }
757 }
758
759 static void get_texcoords(struct pipe_sampler_view *src,
760 unsigned src_width0, unsigned src_height0,
761 int x1, int y1, int x2, int y2,
762 float out[4])
763 {
764 struct pipe_resource *tex = src->texture;
765 unsigned level = src->u.tex.first_level;
766 boolean normalized = tex->target != PIPE_TEXTURE_RECT &&
767 tex->nr_samples <= 1;
768
769 if (normalized) {
770 out[0] = x1 / (float)u_minify(src_width0, level);
771 out[1] = y1 / (float)u_minify(src_height0, level);
772 out[2] = x2 / (float)u_minify(src_width0, level);
773 out[3] = y2 / (float)u_minify(src_height0, level);
774 } else {
775 out[0] = (float) x1;
776 out[1] = (float) y1;
777 out[2] = (float) x2;
778 out[3] = (float) y2;
779 }
780 }
781
782 static void set_texcoords_in_vertices(const float coord[4],
783 float *out, unsigned stride)
784 {
785 out[0] = coord[0]; /*t0.s*/
786 out[1] = coord[1]; /*t0.t*/
787 out += stride;
788 out[0] = coord[2]; /*t1.s*/
789 out[1] = coord[1]; /*t1.t*/
790 out += stride;
791 out[0] = coord[2]; /*t2.s*/
792 out[1] = coord[3]; /*t2.t*/
793 out += stride;
794 out[0] = coord[0]; /*t3.s*/
795 out[1] = coord[3]; /*t3.t*/
796 }
797
798 static void blitter_set_texcoords(struct blitter_context_priv *ctx,
799 struct pipe_sampler_view *src,
800 unsigned src_width0, unsigned src_height0,
801 float layer, unsigned sample,
802 int x1, int y1, int x2, int y2)
803 {
804 unsigned i;
805 float coord[4];
806 float face_coord[4][2];
807
808 get_texcoords(src, src_width0, src_height0, x1, y1, x2, y2, coord);
809
810 if (src->texture->target == PIPE_TEXTURE_CUBE ||
811 src->texture->target == PIPE_TEXTURE_CUBE_ARRAY) {
812 set_texcoords_in_vertices(coord, &face_coord[0][0], 2);
813 util_map_texcoords2d_onto_cubemap((unsigned)layer % 6,
814 /* pointer, stride in floats */
815 &face_coord[0][0], 2,
816 &ctx->vertices[0][1][0], 8,
817 FALSE);
818 } else {
819 set_texcoords_in_vertices(coord, &ctx->vertices[0][1][0], 8);
820 }
821
822 /* Set the layer. */
823 switch (src->texture->target) {
824 case PIPE_TEXTURE_3D:
825 {
826 float r = layer / (float)u_minify(src->texture->depth0,
827 src->u.tex.first_level);
828 for (i = 0; i < 4; i++)
829 ctx->vertices[i][1][2] = r; /*r*/
830 }
831 break;
832
833 case PIPE_TEXTURE_1D_ARRAY:
834 for (i = 0; i < 4; i++)
835 ctx->vertices[i][1][1] = (float) layer; /*t*/
836 break;
837
838 case PIPE_TEXTURE_2D_ARRAY:
839 for (i = 0; i < 4; i++) {
840 ctx->vertices[i][1][2] = (float) layer; /*r*/
841 ctx->vertices[i][1][3] = (float) sample; /*q*/
842 }
843 break;
844
845 case PIPE_TEXTURE_CUBE_ARRAY:
846 for (i = 0; i < 4; i++)
847 ctx->vertices[i][1][3] = (float) ((unsigned)layer / 6); /*w*/
848 break;
849
850 case PIPE_TEXTURE_2D:
851 for (i = 0; i < 4; i++) {
852 ctx->vertices[i][1][3] = (float) sample; /*r*/
853 }
854 break;
855
856 default:;
857 }
858 }
859
860 static void blitter_set_dst_dimensions(struct blitter_context_priv *ctx,
861 unsigned width, unsigned height)
862 {
863 ctx->dst_width = width;
864 ctx->dst_height = height;
865 }
866
867 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx,
868 enum pipe_format format,
869 enum pipe_texture_target target,
870 unsigned src_nr_samples,
871 unsigned dst_nr_samples,
872 unsigned filter)
873 {
874 struct pipe_context *pipe = ctx->base.pipe;
875 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(target, src_nr_samples);
876 enum tgsi_return_type stype;
877
878 assert(target < PIPE_MAX_TEXTURE_TYPES);
879
880 if (util_format_is_pure_uint(format))
881 stype = TGSI_RETURN_TYPE_UINT;
882 else if (util_format_is_pure_sint(format))
883 stype = TGSI_RETURN_TYPE_SINT;
884 else
885 stype = TGSI_RETURN_TYPE_FLOAT;
886
887 if (src_nr_samples > 1) {
888 void **shader;
889
890 if (dst_nr_samples <= 1) {
891 /* The destination has one sample, so we'll do color resolve. */
892 unsigned index = GET_MSAA_RESOLVE_FS_IDX(src_nr_samples);
893
894 assert(filter < 2);
895
896 if (stype == TGSI_RETURN_TYPE_UINT)
897 shader = &ctx->fs_resolve_uint[target][index][filter];
898 else if (stype == TGSI_RETURN_TYPE_SINT)
899 shader = &ctx->fs_resolve_sint[target][index][filter];
900 else
901 shader = &ctx->fs_resolve[target][index][filter];
902
903 if (!*shader) {
904 assert(!ctx->cached_all_shaders);
905 if (filter == PIPE_TEX_FILTER_LINEAR) {
906 *shader = util_make_fs_msaa_resolve_bilinear(pipe, tgsi_tex,
907 src_nr_samples,
908 stype);
909 }
910 else {
911 *shader = util_make_fs_msaa_resolve(pipe, tgsi_tex,
912 src_nr_samples,
913 stype);
914 }
915 }
916 }
917 else {
918 /* The destination has multiple samples, we'll do
919 * an MSAA->MSAA copy.
920 */
921 if (stype == TGSI_RETURN_TYPE_UINT)
922 shader = &ctx->fs_texfetch_col_msaa_uint[target];
923 else if (stype == TGSI_RETURN_TYPE_SINT)
924 shader = &ctx->fs_texfetch_col_msaa_sint[target];
925 else
926 shader = &ctx->fs_texfetch_col_msaa[target];
927
928 /* Create the fragment shader on-demand. */
929 if (!*shader) {
930 assert(!ctx->cached_all_shaders);
931 *shader = util_make_fs_blit_msaa_color(pipe, tgsi_tex, stype);
932 }
933 }
934
935 return *shader;
936 } else {
937 void **shader;
938
939 if (stype == TGSI_RETURN_TYPE_UINT)
940 shader = &ctx->fs_texfetch_col_uint[target];
941 else if (stype == TGSI_RETURN_TYPE_SINT)
942 shader = &ctx->fs_texfetch_col_sint[target];
943 else
944 shader = &ctx->fs_texfetch_col[target];
945
946 /* Create the fragment shader on-demand. */
947 if (!*shader) {
948 assert(!ctx->cached_all_shaders);
949 *shader = util_make_fragment_tex_shader(pipe, tgsi_tex,
950 TGSI_INTERPOLATE_LINEAR,
951 stype);
952 }
953
954 return *shader;
955 }
956 }
957
958 static inline
959 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx,
960 enum pipe_texture_target target,
961 unsigned nr_samples)
962 {
963 struct pipe_context *pipe = ctx->base.pipe;
964
965 assert(target < PIPE_MAX_TEXTURE_TYPES);
966
967 if (nr_samples > 1) {
968 void **shader = &ctx->fs_texfetch_depth_msaa[target];
969
970 /* Create the fragment shader on-demand. */
971 if (!*shader) {
972 unsigned tgsi_tex;
973 assert(!ctx->cached_all_shaders);
974 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
975 *shader = util_make_fs_blit_msaa_depth(pipe, tgsi_tex);
976 }
977
978 return *shader;
979 } else {
980 void **shader = &ctx->fs_texfetch_depth[target];
981
982 /* Create the fragment shader on-demand. */
983 if (!*shader) {
984 unsigned tgsi_tex;
985 assert(!ctx->cached_all_shaders);
986 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
987 *shader =
988 util_make_fragment_tex_shader_writedepth(pipe, tgsi_tex,
989 TGSI_INTERPOLATE_LINEAR);
990 }
991
992 return *shader;
993 }
994 }
995
996 static inline
997 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv *ctx,
998 enum pipe_texture_target target,
999 unsigned nr_samples)
1000 {
1001 struct pipe_context *pipe = ctx->base.pipe;
1002
1003 assert(target < PIPE_MAX_TEXTURE_TYPES);
1004
1005 if (nr_samples > 1) {
1006 void **shader = &ctx->fs_texfetch_depthstencil_msaa[target];
1007
1008 /* Create the fragment shader on-demand. */
1009 if (!*shader) {
1010 unsigned tgsi_tex;
1011 assert(!ctx->cached_all_shaders);
1012 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
1013 *shader = util_make_fs_blit_msaa_depthstencil(pipe, tgsi_tex);
1014 }
1015
1016 return *shader;
1017 } else {
1018 void **shader = &ctx->fs_texfetch_depthstencil[target];
1019
1020 /* Create the fragment shader on-demand. */
1021 if (!*shader) {
1022 unsigned tgsi_tex;
1023 assert(!ctx->cached_all_shaders);
1024 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
1025 *shader =
1026 util_make_fragment_tex_shader_writedepthstencil(pipe, tgsi_tex,
1027 TGSI_INTERPOLATE_LINEAR);
1028 }
1029
1030 return *shader;
1031 }
1032 }
1033
1034 static inline
1035 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv *ctx,
1036 enum pipe_texture_target target,
1037 unsigned nr_samples)
1038 {
1039 struct pipe_context *pipe = ctx->base.pipe;
1040
1041 assert(target < PIPE_MAX_TEXTURE_TYPES);
1042
1043 if (nr_samples > 1) {
1044 void **shader = &ctx->fs_texfetch_stencil_msaa[target];
1045
1046 /* Create the fragment shader on-demand. */
1047 if (!*shader) {
1048 unsigned tgsi_tex;
1049 assert(!ctx->cached_all_shaders);
1050 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
1051 *shader = util_make_fs_blit_msaa_stencil(pipe, tgsi_tex);
1052 }
1053
1054 return *shader;
1055 } else {
1056 void **shader = &ctx->fs_texfetch_stencil[target];
1057
1058 /* Create the fragment shader on-demand. */
1059 if (!*shader) {
1060 unsigned tgsi_tex;
1061 assert(!ctx->cached_all_shaders);
1062 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
1063 *shader =
1064 util_make_fragment_tex_shader_writestencil(pipe, tgsi_tex,
1065 TGSI_INTERPOLATE_LINEAR);
1066 }
1067
1068 return *shader;
1069 }
1070 }
1071
1072
1073 /**
1074 * Generate and save all fragment shaders that we will ever need for
1075 * blitting. Drivers which use the 'draw' fallbacks will typically use
1076 * this to make sure we generate/use shaders that don't go through the
1077 * draw module's wrapper functions.
1078 */
1079 void util_blitter_cache_all_shaders(struct blitter_context *blitter)
1080 {
1081 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1082 struct pipe_context *pipe = blitter->pipe;
1083 struct pipe_screen *screen = pipe->screen;
1084 unsigned samples, j, f, target, max_samples;
1085 boolean has_arraytex, has_cubearraytex;
1086
1087 max_samples = ctx->has_texture_multisample ? 2 : 1;
1088 has_arraytex = screen->get_param(screen,
1089 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS) != 0;
1090 has_cubearraytex = screen->get_param(screen,
1091 PIPE_CAP_CUBE_MAP_ARRAY) != 0;
1092
1093 /* It only matters if i <= 1 or > 1. */
1094 for (samples = 1; samples <= max_samples; samples++) {
1095 for (target = PIPE_TEXTURE_1D; target < PIPE_MAX_TEXTURE_TYPES; target++) {
1096 if (!has_arraytex &&
1097 (target == PIPE_TEXTURE_1D_ARRAY ||
1098 target == PIPE_TEXTURE_2D_ARRAY)) {
1099 continue;
1100 }
1101 if (!has_cubearraytex &&
1102 (target == PIPE_TEXTURE_CUBE_ARRAY))
1103 continue;
1104
1105 if (samples > 1 &&
1106 (target != PIPE_TEXTURE_2D &&
1107 target != PIPE_TEXTURE_2D_ARRAY))
1108 continue;
1109
1110 /* If samples == 1, the shaders read one texel. If samples >= 1,
1111 * they read one sample.
1112 */
1113 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_FLOAT, target,
1114 samples, samples, 0);
1115 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT, target,
1116 samples, samples, 0);
1117 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT, target,
1118 samples, samples, 0);
1119 blitter_get_fs_texfetch_depth(ctx, target, samples);
1120 if (ctx->has_stencil_export) {
1121 blitter_get_fs_texfetch_depthstencil(ctx, target, samples);
1122 blitter_get_fs_texfetch_stencil(ctx, target, samples);
1123 }
1124
1125 if (samples == 1)
1126 continue;
1127
1128 /* MSAA resolve shaders. */
1129 for (j = 2; j < 32; j++) {
1130 if (!screen->is_format_supported(screen, PIPE_FORMAT_R32_FLOAT,
1131 target, j,
1132 PIPE_BIND_SAMPLER_VIEW)) {
1133 continue;
1134 }
1135
1136 for (f = 0; f < 2; f++) {
1137 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_FLOAT, target,
1138 j, 1, f);
1139 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT, target,
1140 j, 1, f);
1141 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT, target,
1142 j, 1, f);
1143 }
1144 }
1145 }
1146 }
1147
1148 ctx->fs_empty = util_make_empty_fragment_shader(pipe);
1149
1150 ctx->fs_write_one_cbuf =
1151 util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
1152 TGSI_INTERPOLATE_CONSTANT, FALSE);
1153
1154 ctx->fs_write_all_cbufs =
1155 util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
1156 TGSI_INTERPOLATE_CONSTANT, TRUE);
1157
1158 ctx->cached_all_shaders = TRUE;
1159 }
1160
1161 static void blitter_set_common_draw_rect_state(struct blitter_context_priv *ctx,
1162 boolean scissor,
1163 boolean vs_layered)
1164 {
1165 struct pipe_context *pipe = ctx->base.pipe;
1166
1167 pipe->bind_rasterizer_state(pipe, scissor ? ctx->rs_state_scissor
1168 : ctx->rs_state);
1169 if (vs_layered)
1170 bind_vs_layered(ctx);
1171 else
1172 bind_vs_passthrough(ctx);
1173
1174 if (ctx->has_geometry_shader)
1175 pipe->bind_gs_state(pipe, NULL);
1176 if (ctx->has_tessellation) {
1177 pipe->bind_tcs_state(pipe, NULL);
1178 pipe->bind_tes_state(pipe, NULL);
1179 }
1180 if (ctx->has_stream_out)
1181 pipe->set_stream_output_targets(pipe, 0, NULL, NULL);
1182 }
1183
1184 static void blitter_draw(struct blitter_context_priv *ctx,
1185 int x1, int y1, int x2, int y2, float depth,
1186 unsigned num_instances)
1187 {
1188 struct pipe_context *pipe = ctx->base.pipe;
1189 struct pipe_vertex_buffer vb = {0};
1190
1191 blitter_set_rectangle(ctx, x1, y1, x2, y2, depth);
1192
1193 vb.stride = 8 * sizeof(float);
1194
1195 u_upload_data(ctx->upload, 0, sizeof(ctx->vertices), 4, ctx->vertices,
1196 &vb.buffer_offset, &vb.buffer);
1197 if (!vb.buffer)
1198 return;
1199 u_upload_unmap(ctx->upload);
1200
1201 pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
1202 util_draw_arrays_instanced(pipe, PIPE_PRIM_TRIANGLE_FAN, 0, 4,
1203 0, num_instances);
1204 pipe_resource_reference(&vb.buffer, NULL);
1205 }
1206
1207 void util_blitter_draw_rectangle(struct blitter_context *blitter,
1208 int x1, int y1, int x2, int y2, float depth,
1209 enum blitter_attrib_type type,
1210 const union pipe_color_union *attrib)
1211 {
1212 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1213
1214 switch (type) {
1215 case UTIL_BLITTER_ATTRIB_COLOR:
1216 blitter_set_clear_color(ctx, attrib);
1217 break;
1218
1219 case UTIL_BLITTER_ATTRIB_TEXCOORD:
1220 set_texcoords_in_vertices(attrib->f, &ctx->vertices[0][1][0], 8);
1221 break;
1222
1223 default:;
1224 }
1225
1226 blitter_draw(ctx, x1, y1, x2, y2, depth, 1);
1227 }
1228
1229 static void *get_clear_blend_state(struct blitter_context_priv *ctx,
1230 unsigned clear_buffers)
1231 {
1232 struct pipe_context *pipe = ctx->base.pipe;
1233 int index;
1234
1235 clear_buffers &= PIPE_CLEAR_COLOR;
1236
1237 /* Return an existing blend state. */
1238 if (!clear_buffers)
1239 return ctx->blend[0][0];
1240
1241 index = GET_CLEAR_BLEND_STATE_IDX(clear_buffers);
1242
1243 if (ctx->blend_clear[index])
1244 return ctx->blend_clear[index];
1245
1246 /* Create a new one. */
1247 {
1248 struct pipe_blend_state blend = {0};
1249 unsigned i;
1250
1251 blend.independent_blend_enable = 1;
1252
1253 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
1254 if (clear_buffers & (PIPE_CLEAR_COLOR0 << i)) {
1255 blend.rt[i].colormask = PIPE_MASK_RGBA;
1256 }
1257 }
1258
1259 ctx->blend_clear[index] = pipe->create_blend_state(pipe, &blend);
1260 }
1261 return ctx->blend_clear[index];
1262 }
1263
1264 static void util_blitter_clear_custom(struct blitter_context *blitter,
1265 unsigned width, unsigned height,
1266 unsigned num_layers,
1267 unsigned clear_buffers,
1268 const union pipe_color_union *color,
1269 double depth, unsigned stencil,
1270 void *custom_blend, void *custom_dsa)
1271 {
1272 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1273 struct pipe_context *pipe = ctx->base.pipe;
1274 struct pipe_stencil_ref sr = { { 0 } };
1275
1276 assert(ctx->has_layered || num_layers <= 1);
1277
1278 blitter_set_running_flag(ctx);
1279 blitter_check_saved_vertex_states(ctx);
1280 blitter_check_saved_fragment_states(ctx);
1281 blitter_disable_render_cond(ctx);
1282
1283 /* bind states */
1284 if (custom_blend) {
1285 pipe->bind_blend_state(pipe, custom_blend);
1286 } else {
1287 pipe->bind_blend_state(pipe, get_clear_blend_state(ctx, clear_buffers));
1288 }
1289
1290 if (custom_dsa) {
1291 pipe->bind_depth_stencil_alpha_state(pipe, custom_dsa);
1292 } else if ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
1293 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
1294 } else if (clear_buffers & PIPE_CLEAR_DEPTH) {
1295 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
1296 } else if (clear_buffers & PIPE_CLEAR_STENCIL) {
1297 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
1298 } else {
1299 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1300 }
1301
1302 sr.ref_value[0] = stencil & 0xff;
1303 pipe->set_stencil_ref(pipe, &sr);
1304
1305 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1306 bind_fs_write_all_cbufs(ctx);
1307 pipe->set_sample_mask(pipe, ~0);
1308
1309 blitter_set_dst_dimensions(ctx, width, height);
1310
1311 if (num_layers > 1 && ctx->has_layered) {
1312 blitter_set_common_draw_rect_state(ctx, FALSE, TRUE);
1313 blitter_set_clear_color(ctx, color);
1314 blitter_draw(ctx, 0, 0, width, height, depth, num_layers);
1315 }
1316 else {
1317 blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
1318 blitter->draw_rectangle(blitter, 0, 0, width, height, (float) depth,
1319 UTIL_BLITTER_ATTRIB_COLOR, color);
1320 }
1321
1322 blitter_restore_vertex_states(ctx);
1323 blitter_restore_fragment_states(ctx);
1324 blitter_restore_render_cond(ctx);
1325 blitter_unset_running_flag(ctx);
1326 }
1327
1328 void util_blitter_clear(struct blitter_context *blitter,
1329 unsigned width, unsigned height, unsigned num_layers,
1330 unsigned clear_buffers,
1331 const union pipe_color_union *color,
1332 double depth, unsigned stencil)
1333 {
1334 util_blitter_clear_custom(blitter, width, height, num_layers,
1335 clear_buffers, color, depth, stencil,
1336 NULL, NULL);
1337 }
1338
1339 void util_blitter_custom_clear_depth(struct blitter_context *blitter,
1340 unsigned width, unsigned height,
1341 double depth, void *custom_dsa)
1342 {
1343 static const union pipe_color_union color;
1344 util_blitter_clear_custom(blitter, width, height, 0, 0, &color, depth, 0,
1345 NULL, custom_dsa);
1346 }
1347
1348 void util_blitter_default_dst_texture(struct pipe_surface *dst_templ,
1349 struct pipe_resource *dst,
1350 unsigned dstlevel,
1351 unsigned dstz)
1352 {
1353 memset(dst_templ, 0, sizeof(*dst_templ));
1354 dst_templ->format = util_format_linear(dst->format);
1355 dst_templ->u.tex.level = dstlevel;
1356 dst_templ->u.tex.first_layer = dstz;
1357 dst_templ->u.tex.last_layer = dstz;
1358 }
1359
1360 static struct pipe_surface *
1361 util_blitter_get_next_surface_layer(struct pipe_context *pipe,
1362 struct pipe_surface *surf)
1363 {
1364 struct pipe_surface dst_templ;
1365
1366 memset(&dst_templ, 0, sizeof(dst_templ));
1367 dst_templ.format = surf->format;
1368 dst_templ.u.tex.level = surf->u.tex.level;
1369 dst_templ.u.tex.first_layer = surf->u.tex.first_layer + 1;
1370 dst_templ.u.tex.last_layer = surf->u.tex.last_layer + 1;
1371
1372 return pipe->create_surface(pipe, surf->texture, &dst_templ);
1373 }
1374
1375 void util_blitter_default_src_texture(struct pipe_sampler_view *src_templ,
1376 struct pipe_resource *src,
1377 unsigned srclevel)
1378 {
1379 memset(src_templ, 0, sizeof(*src_templ));
1380 src_templ->target = src->target;
1381 src_templ->format = util_format_linear(src->format);
1382 src_templ->u.tex.first_level = srclevel;
1383 src_templ->u.tex.last_level = srclevel;
1384 src_templ->u.tex.first_layer = 0;
1385 src_templ->u.tex.last_layer =
1386 src->target == PIPE_TEXTURE_3D ? u_minify(src->depth0, srclevel) - 1
1387 : src->array_size - 1;
1388 src_templ->swizzle_r = PIPE_SWIZZLE_RED;
1389 src_templ->swizzle_g = PIPE_SWIZZLE_GREEN;
1390 src_templ->swizzle_b = PIPE_SWIZZLE_BLUE;
1391 src_templ->swizzle_a = PIPE_SWIZZLE_ALPHA;
1392 }
1393
1394 static boolean is_blit_generic_supported(struct blitter_context *blitter,
1395 const struct pipe_resource *dst,
1396 enum pipe_format dst_format,
1397 const struct pipe_resource *src,
1398 enum pipe_format src_format,
1399 unsigned mask)
1400 {
1401 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1402 struct pipe_screen *screen = ctx->base.pipe->screen;
1403
1404 if (dst) {
1405 unsigned bind;
1406 const struct util_format_description *desc =
1407 util_format_description(dst_format);
1408 boolean dst_has_stencil = util_format_has_stencil(desc);
1409
1410 /* Stencil export must be supported for stencil copy. */
1411 if ((mask & PIPE_MASK_S) && dst_has_stencil &&
1412 !ctx->has_stencil_export) {
1413 return FALSE;
1414 }
1415
1416 if (dst_has_stencil || util_format_has_depth(desc))
1417 bind = PIPE_BIND_DEPTH_STENCIL;
1418 else
1419 bind = PIPE_BIND_RENDER_TARGET;
1420
1421 if (!screen->is_format_supported(screen, dst_format, dst->target,
1422 dst->nr_samples, bind)) {
1423 return FALSE;
1424 }
1425 }
1426
1427 if (src) {
1428 if (src->nr_samples > 1 && !ctx->has_texture_multisample) {
1429 return FALSE;
1430 }
1431
1432 if (!screen->is_format_supported(screen, src_format, src->target,
1433 src->nr_samples, PIPE_BIND_SAMPLER_VIEW)) {
1434 return FALSE;
1435 }
1436
1437 /* Check stencil sampler support for stencil copy. */
1438 if (mask & PIPE_MASK_S) {
1439 if (util_format_has_stencil(util_format_description(src_format))) {
1440 enum pipe_format stencil_format =
1441 util_format_stencil_only(src_format);
1442 assert(stencil_format != PIPE_FORMAT_NONE);
1443
1444 if (stencil_format != src_format &&
1445 !screen->is_format_supported(screen, stencil_format,
1446 src->target, src->nr_samples,
1447 PIPE_BIND_SAMPLER_VIEW)) {
1448 return FALSE;
1449 }
1450 }
1451 }
1452 }
1453
1454 return TRUE;
1455 }
1456
1457 boolean util_blitter_is_copy_supported(struct blitter_context *blitter,
1458 const struct pipe_resource *dst,
1459 const struct pipe_resource *src)
1460 {
1461 return is_blit_generic_supported(blitter, dst, dst->format,
1462 src, src->format, PIPE_MASK_RGBAZS);
1463 }
1464
1465 boolean util_blitter_is_blit_supported(struct blitter_context *blitter,
1466 const struct pipe_blit_info *info)
1467 {
1468 return is_blit_generic_supported(blitter,
1469 info->dst.resource, info->dst.format,
1470 info->src.resource, info->src.format,
1471 info->mask);
1472 }
1473
1474 void util_blitter_copy_texture(struct blitter_context *blitter,
1475 struct pipe_resource *dst,
1476 unsigned dst_level,
1477 unsigned dstx, unsigned dsty, unsigned dstz,
1478 struct pipe_resource *src,
1479 unsigned src_level,
1480 const struct pipe_box *srcbox)
1481 {
1482 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1483 struct pipe_context *pipe = ctx->base.pipe;
1484 struct pipe_surface *dst_view, dst_templ;
1485 struct pipe_sampler_view src_templ, *src_view;
1486 struct pipe_box dstbox;
1487
1488 assert(dst && src);
1489 assert(src->target < PIPE_MAX_TEXTURE_TYPES);
1490
1491 u_box_3d(dstx, dsty, dstz, abs(srcbox->width), abs(srcbox->height),
1492 abs(srcbox->depth), &dstbox);
1493
1494 /* Initialize the surface. */
1495 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
1496 dst_view = pipe->create_surface(pipe, dst, &dst_templ);
1497
1498 /* Initialize the sampler view. */
1499 util_blitter_default_src_texture(&src_templ, src, src_level);
1500 src_view = pipe->create_sampler_view(pipe, src, &src_templ);
1501
1502 /* Copy. */
1503 util_blitter_blit_generic(blitter, dst_view, &dstbox,
1504 src_view, srcbox, src->width0, src->height0,
1505 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,
1506 FALSE);
1507
1508 pipe_surface_reference(&dst_view, NULL);
1509 pipe_sampler_view_reference(&src_view, NULL);
1510 }
1511
1512 void util_blitter_blit_generic(struct blitter_context *blitter,
1513 struct pipe_surface *dst,
1514 const struct pipe_box *dstbox,
1515 struct pipe_sampler_view *src,
1516 const struct pipe_box *srcbox,
1517 unsigned src_width0, unsigned src_height0,
1518 unsigned mask, unsigned filter,
1519 const struct pipe_scissor_state *scissor,
1520 boolean alpha_blend)
1521 {
1522 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1523 struct pipe_context *pipe = ctx->base.pipe;
1524 struct pipe_framebuffer_state fb_state;
1525 enum pipe_texture_target src_target = src->texture->target;
1526 unsigned src_samples = src->texture->nr_samples;
1527 unsigned dst_samples = dst->texture->nr_samples;
1528 boolean has_depth, has_stencil, has_color;
1529 boolean blit_stencil, blit_depth, blit_color;
1530 void *sampler_state;
1531 const struct util_format_description *src_desc =
1532 util_format_description(src->format);
1533 const struct util_format_description *dst_desc =
1534 util_format_description(dst->format);
1535
1536 has_color = src_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS &&
1537 dst_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS;
1538 has_depth = util_format_has_depth(src_desc) &&
1539 util_format_has_depth(dst_desc);
1540 has_stencil = util_format_has_stencil(src_desc) &&
1541 util_format_has_stencil(dst_desc);
1542
1543 blit_color = has_color && (mask & PIPE_MASK_RGBA);
1544 blit_depth = has_depth && (mask & PIPE_MASK_Z);
1545 blit_stencil = has_stencil && (mask & PIPE_MASK_S) &&
1546 ctx->has_stencil_export;
1547
1548 if (!blit_stencil && !blit_depth && !blit_color) {
1549 return;
1550 }
1551
1552 if (blit_stencil ||
1553 (dstbox->width == abs(srcbox->width) &&
1554 dstbox->height == abs(srcbox->height))) {
1555 filter = PIPE_TEX_FILTER_NEAREST;
1556 }
1557
1558 /* Check whether the states are properly saved. */
1559 blitter_set_running_flag(ctx);
1560 blitter_check_saved_vertex_states(ctx);
1561 blitter_check_saved_fragment_states(ctx);
1562 blitter_check_saved_textures(ctx);
1563 blitter_check_saved_fb_state(ctx);
1564 blitter_disable_render_cond(ctx);
1565
1566 /* Initialize framebuffer state. */
1567 fb_state.width = dst->width;
1568 fb_state.height = dst->height;
1569 fb_state.nr_cbufs = blit_depth || blit_stencil ? 0 : 1;
1570 fb_state.cbufs[0] = NULL;
1571 fb_state.zsbuf = NULL;
1572
1573 if (blit_depth || blit_stencil) {
1574 pipe->bind_blend_state(pipe, ctx->blend[0][0]);
1575
1576 if (blit_depth && blit_stencil) {
1577 pipe->bind_depth_stencil_alpha_state(pipe,
1578 ctx->dsa_write_depth_stencil);
1579 ctx->bind_fs_state(pipe,
1580 blitter_get_fs_texfetch_depthstencil(ctx, src_target,
1581 src_samples));
1582 } else if (blit_depth) {
1583 pipe->bind_depth_stencil_alpha_state(pipe,
1584 ctx->dsa_write_depth_keep_stencil);
1585 ctx->bind_fs_state(pipe,
1586 blitter_get_fs_texfetch_depth(ctx, src_target,
1587 src_samples));
1588 } else { /* is_stencil */
1589 pipe->bind_depth_stencil_alpha_state(pipe,
1590 ctx->dsa_keep_depth_write_stencil);
1591 ctx->bind_fs_state(pipe,
1592 blitter_get_fs_texfetch_stencil(ctx, src_target,
1593 src_samples));
1594 }
1595
1596 } else {
1597 unsigned colormask = mask & PIPE_MASK_RGBA;
1598
1599 pipe->bind_blend_state(pipe, ctx->blend[colormask][alpha_blend]);
1600 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1601 ctx->bind_fs_state(pipe,
1602 blitter_get_fs_texfetch_col(ctx, src->format, src_target,
1603 src_samples, dst_samples, filter));
1604 }
1605
1606 /* Set the linear filter only for scaled color non-MSAA blits. */
1607 if (filter == PIPE_TEX_FILTER_LINEAR) {
1608 if (src_target == PIPE_TEXTURE_RECT) {
1609 sampler_state = ctx->sampler_state_rect_linear;
1610 } else {
1611 sampler_state = ctx->sampler_state_linear;
1612 }
1613 } else {
1614 if (src_target == PIPE_TEXTURE_RECT) {
1615 sampler_state = ctx->sampler_state_rect;
1616 } else {
1617 sampler_state = ctx->sampler_state;
1618 }
1619 }
1620
1621 /* Set samplers. */
1622 if (blit_depth && blit_stencil) {
1623 /* Setup two samplers, one for depth and the other one for stencil. */
1624 struct pipe_sampler_view templ;
1625 struct pipe_sampler_view *views[2];
1626 void *samplers[2] = {sampler_state, sampler_state};
1627
1628 templ = *src;
1629 templ.format = util_format_stencil_only(templ.format);
1630 assert(templ.format != PIPE_FORMAT_NONE);
1631
1632 views[0] = src;
1633 views[1] = pipe->create_sampler_view(pipe, src->texture, &templ);
1634
1635 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 2, views);
1636 pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0, 2, samplers);
1637
1638 pipe_sampler_view_reference(&views[1], NULL);
1639 } else if (blit_stencil) {
1640 /* Set a stencil-only sampler view for it not to sample depth instead. */
1641 struct pipe_sampler_view templ;
1642 struct pipe_sampler_view *view;
1643
1644 templ = *src;
1645 templ.format = util_format_stencil_only(templ.format);
1646 assert(templ.format != PIPE_FORMAT_NONE);
1647
1648 view = pipe->create_sampler_view(pipe, src->texture, &templ);
1649
1650 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &view);
1651 pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
1652 0, 1, &sampler_state);
1653
1654 pipe_sampler_view_reference(&view, NULL);
1655 } else {
1656 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &src);
1657 pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
1658 0, 1, &sampler_state);
1659 }
1660
1661 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1662 if (scissor) {
1663 pipe->set_scissor_states(pipe, 0, 1, scissor);
1664 }
1665
1666 blitter_set_common_draw_rect_state(ctx, scissor != NULL, FALSE);
1667 blitter_set_dst_dimensions(ctx, dst->width, dst->height);
1668
1669 if ((src_target == PIPE_TEXTURE_1D ||
1670 src_target == PIPE_TEXTURE_2D ||
1671 src_target == PIPE_TEXTURE_RECT) &&
1672 src_samples <= 1) {
1673 /* Draw the quad with the draw_rectangle callback. */
1674
1675 /* Set texture coordinates. - use a pipe color union
1676 * for interface purposes.
1677 * XXX pipe_color_union is a wrong name since we use that to set
1678 * texture coordinates too.
1679 */
1680 union pipe_color_union coord;
1681 get_texcoords(src, src_width0, src_height0, srcbox->x, srcbox->y,
1682 srcbox->x+srcbox->width, srcbox->y+srcbox->height, coord.f);
1683
1684 /* Set framebuffer state. */
1685 if (blit_depth || blit_stencil) {
1686 fb_state.zsbuf = dst;
1687 } else {
1688 fb_state.cbufs[0] = dst;
1689 }
1690 pipe->set_framebuffer_state(pipe, &fb_state);
1691
1692 /* Draw. */
1693 pipe->set_sample_mask(pipe, ~0);
1694 blitter->draw_rectangle(blitter, dstbox->x, dstbox->y,
1695 dstbox->x + dstbox->width,
1696 dstbox->y + dstbox->height, 0,
1697 UTIL_BLITTER_ATTRIB_TEXCOORD, &coord);
1698 } else {
1699 /* Draw the quad with the generic codepath. */
1700 int dst_z;
1701 for (dst_z = 0; dst_z < dstbox->depth; dst_z++) {
1702 struct pipe_surface *old;
1703 float dst2src_scale = srcbox->depth / (float)dstbox->depth;
1704
1705 /* Scale Z properly if the blit is scaled.
1706 *
1707 * When downscaling, we want the coordinates centered, so that
1708 * mipmapping works for 3D textures. For example, when generating
1709 * a 4x4x4 level, this wouldn't average the pixels:
1710 *
1711 * src Z: 0 1 2 3 4 5 6 7
1712 * dst Z: 0 1 2 3
1713 *
1714 * Because the pixels are not centered below the pixels of the higher
1715 * level. Therefore, we want this:
1716 * src Z: 0 1 2 3 4 5 6 7
1717 * dst Z: 0 1 2 3
1718 *
1719 * dst_offset defines the offset needed for centering the pixels and
1720 * it works with any scaling (not just 2x).
1721 */
1722 float dst_offset = ((srcbox->depth - 1) -
1723 (dstbox->depth - 1) * dst2src_scale) * 0.5;
1724 float src_z = (dst_z + dst_offset) * dst2src_scale;
1725
1726 /* Set framebuffer state. */
1727 if (blit_depth || blit_stencil) {
1728 fb_state.zsbuf = dst;
1729 } else {
1730 fb_state.cbufs[0] = dst;
1731 }
1732 pipe->set_framebuffer_state(pipe, &fb_state);
1733
1734 /* See if we need to blit a multisample or singlesample buffer. */
1735 if (src_samples == dst_samples && dst_samples > 1) {
1736 /* MSAA copy. */
1737 unsigned i, max_sample = dst_samples - 1;
1738
1739 for (i = 0; i <= max_sample; i++) {
1740 pipe->set_sample_mask(pipe, 1 << i);
1741 blitter_set_texcoords(ctx, src, src_width0, src_height0,
1742 srcbox->z + src_z,
1743 i, srcbox->x, srcbox->y,
1744 srcbox->x + srcbox->width,
1745 srcbox->y + srcbox->height);
1746 blitter_draw(ctx, dstbox->x, dstbox->y,
1747 dstbox->x + dstbox->width,
1748 dstbox->y + dstbox->height, 0, 1);
1749 }
1750 } else {
1751 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1752 pipe->set_sample_mask(pipe, ~0);
1753 blitter_set_texcoords(ctx, src, src_width0, src_height0,
1754 srcbox->z + src_z, 0,
1755 srcbox->x, srcbox->y,
1756 srcbox->x + srcbox->width,
1757 srcbox->y + srcbox->height);
1758 blitter_draw(ctx, dstbox->x, dstbox->y,
1759 dstbox->x + dstbox->width,
1760 dstbox->y + dstbox->height, 0, 1);
1761 }
1762
1763 /* Get the next surface or (if this is the last iteration)
1764 * just unreference the last one. */
1765 old = dst;
1766 if (dst_z < dstbox->depth-1) {
1767 dst = ctx->base.get_next_surface_layer(ctx->base.pipe, dst);
1768 }
1769 if (dst_z) {
1770 pipe_surface_reference(&old, NULL);
1771 }
1772 }
1773 }
1774
1775 blitter_restore_vertex_states(ctx);
1776 blitter_restore_fragment_states(ctx);
1777 blitter_restore_textures(ctx);
1778 blitter_restore_fb_state(ctx);
1779 if (scissor) {
1780 pipe->set_scissor_states(pipe, 0, 1, &ctx->base.saved_scissor);
1781 }
1782 blitter_restore_render_cond(ctx);
1783 blitter_unset_running_flag(ctx);
1784 }
1785
1786 void
1787 util_blitter_blit(struct blitter_context *blitter,
1788 const struct pipe_blit_info *info)
1789 {
1790 struct pipe_resource *dst = info->dst.resource;
1791 struct pipe_resource *src = info->src.resource;
1792 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1793 struct pipe_context *pipe = ctx->base.pipe;
1794 struct pipe_surface *dst_view, dst_templ;
1795 struct pipe_sampler_view src_templ, *src_view;
1796
1797 /* Initialize the surface. */
1798 util_blitter_default_dst_texture(&dst_templ, dst, info->dst.level,
1799 info->dst.box.z);
1800 dst_templ.format = info->dst.format;
1801 dst_view = pipe->create_surface(pipe, dst, &dst_templ);
1802
1803 /* Initialize the sampler view. */
1804 util_blitter_default_src_texture(&src_templ, src, info->src.level);
1805 src_templ.format = info->src.format;
1806 src_view = pipe->create_sampler_view(pipe, src, &src_templ);
1807
1808 /* Copy. */
1809 util_blitter_blit_generic(blitter, dst_view, &info->dst.box,
1810 src_view, &info->src.box, src->width0, src->height0,
1811 info->mask, info->filter,
1812 info->scissor_enable ? &info->scissor : NULL,
1813 info->alpha_blend);
1814
1815 pipe_surface_reference(&dst_view, NULL);
1816 pipe_sampler_view_reference(&src_view, NULL);
1817 }
1818
1819 /* Clear a region of a color surface to a constant value. */
1820 void util_blitter_clear_render_target(struct blitter_context *blitter,
1821 struct pipe_surface *dstsurf,
1822 const union pipe_color_union *color,
1823 unsigned dstx, unsigned dsty,
1824 unsigned width, unsigned height)
1825 {
1826 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1827 struct pipe_context *pipe = ctx->base.pipe;
1828 struct pipe_framebuffer_state fb_state;
1829 unsigned num_layers;
1830
1831 assert(dstsurf->texture);
1832 if (!dstsurf->texture)
1833 return;
1834
1835 /* check the saved state */
1836 blitter_set_running_flag(ctx);
1837 blitter_check_saved_vertex_states(ctx);
1838 blitter_check_saved_fragment_states(ctx);
1839 blitter_check_saved_fb_state(ctx);
1840 blitter_disable_render_cond(ctx);
1841
1842 /* bind states */
1843 pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA][0]);
1844 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1845 bind_fs_write_one_cbuf(ctx);
1846 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1847
1848 /* set a framebuffer state */
1849 fb_state.width = dstsurf->width;
1850 fb_state.height = dstsurf->height;
1851 fb_state.nr_cbufs = 1;
1852 fb_state.cbufs[0] = dstsurf;
1853 fb_state.zsbuf = 0;
1854 pipe->set_framebuffer_state(pipe, &fb_state);
1855 pipe->set_sample_mask(pipe, ~0);
1856
1857 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
1858
1859 num_layers = dstsurf->u.tex.last_layer - dstsurf->u.tex.first_layer + 1;
1860 if (num_layers > 1 && ctx->has_layered) {
1861 blitter_set_common_draw_rect_state(ctx, FALSE, TRUE);
1862 blitter_set_clear_color(ctx, color);
1863 blitter_draw(ctx, dstx, dsty, dstx+width, dsty+height, 0, num_layers);
1864 }
1865 else {
1866 blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
1867 blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, 0,
1868 UTIL_BLITTER_ATTRIB_COLOR, color);
1869 }
1870
1871 blitter_restore_vertex_states(ctx);
1872 blitter_restore_fragment_states(ctx);
1873 blitter_restore_fb_state(ctx);
1874 blitter_restore_render_cond(ctx);
1875 blitter_unset_running_flag(ctx);
1876 }
1877
1878 /* Clear a region of a depth stencil surface. */
1879 void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
1880 struct pipe_surface *dstsurf,
1881 unsigned clear_flags,
1882 double depth,
1883 unsigned stencil,
1884 unsigned dstx, unsigned dsty,
1885 unsigned width, unsigned height)
1886 {
1887 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1888 struct pipe_context *pipe = ctx->base.pipe;
1889 struct pipe_framebuffer_state fb_state;
1890 struct pipe_stencil_ref sr = { { 0 } };
1891 unsigned num_layers;
1892
1893 assert(dstsurf->texture);
1894 if (!dstsurf->texture)
1895 return;
1896
1897 /* check the saved state */
1898 blitter_set_running_flag(ctx);
1899 blitter_check_saved_vertex_states(ctx);
1900 blitter_check_saved_fragment_states(ctx);
1901 blitter_check_saved_fb_state(ctx);
1902 blitter_disable_render_cond(ctx);
1903
1904 /* bind states */
1905 pipe->bind_blend_state(pipe, ctx->blend[0][0]);
1906 if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
1907 sr.ref_value[0] = stencil & 0xff;
1908 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
1909 pipe->set_stencil_ref(pipe, &sr);
1910 }
1911 else if (clear_flags & PIPE_CLEAR_DEPTH) {
1912 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
1913 }
1914 else if (clear_flags & PIPE_CLEAR_STENCIL) {
1915 sr.ref_value[0] = stencil & 0xff;
1916 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
1917 pipe->set_stencil_ref(pipe, &sr);
1918 }
1919 else
1920 /* hmm that should be illegal probably, or make it a no-op somewhere */
1921 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1922
1923 bind_fs_empty(ctx);
1924 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1925
1926 /* set a framebuffer state */
1927 fb_state.width = dstsurf->width;
1928 fb_state.height = dstsurf->height;
1929 fb_state.nr_cbufs = 0;
1930 fb_state.cbufs[0] = 0;
1931 fb_state.zsbuf = dstsurf;
1932 pipe->set_framebuffer_state(pipe, &fb_state);
1933 pipe->set_sample_mask(pipe, ~0);
1934
1935 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
1936
1937 num_layers = dstsurf->u.tex.last_layer - dstsurf->u.tex.first_layer + 1;
1938 if (num_layers > 1 && ctx->has_layered) {
1939 blitter_set_common_draw_rect_state(ctx, FALSE, TRUE);
1940 blitter_draw(ctx, dstx, dsty, dstx+width, dsty+height, (float) depth, num_layers);
1941 }
1942 else {
1943 blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
1944 blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height,
1945 (float) depth,
1946 UTIL_BLITTER_ATTRIB_NONE, NULL);
1947 }
1948
1949 blitter_restore_vertex_states(ctx);
1950 blitter_restore_fragment_states(ctx);
1951 blitter_restore_fb_state(ctx);
1952 blitter_restore_render_cond(ctx);
1953 blitter_unset_running_flag(ctx);
1954 }
1955
1956 /* draw a rectangle across a region using a custom dsa stage - for r600g */
1957 void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
1958 struct pipe_surface *zsurf,
1959 struct pipe_surface *cbsurf,
1960 unsigned sample_mask,
1961 void *dsa_stage, float depth)
1962 {
1963 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1964 struct pipe_context *pipe = ctx->base.pipe;
1965 struct pipe_framebuffer_state fb_state;
1966
1967 assert(zsurf->texture);
1968 if (!zsurf->texture)
1969 return;
1970
1971 /* check the saved state */
1972 blitter_set_running_flag(ctx);
1973 blitter_check_saved_vertex_states(ctx);
1974 blitter_check_saved_fragment_states(ctx);
1975 blitter_check_saved_fb_state(ctx);
1976 blitter_disable_render_cond(ctx);
1977
1978 /* bind states */
1979 pipe->bind_blend_state(pipe, cbsurf ? ctx->blend[PIPE_MASK_RGBA][0] :
1980 ctx->blend[0][0]);
1981 pipe->bind_depth_stencil_alpha_state(pipe, dsa_stage);
1982 if (cbsurf)
1983 bind_fs_write_one_cbuf(ctx);
1984 else
1985 bind_fs_empty(ctx);
1986 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1987
1988 /* set a framebuffer state */
1989 fb_state.width = zsurf->width;
1990 fb_state.height = zsurf->height;
1991 fb_state.nr_cbufs = 1;
1992 if (cbsurf) {
1993 fb_state.cbufs[0] = cbsurf;
1994 fb_state.nr_cbufs = 1;
1995 } else {
1996 fb_state.cbufs[0] = NULL;
1997 fb_state.nr_cbufs = 0;
1998 }
1999 fb_state.zsbuf = zsurf;
2000 pipe->set_framebuffer_state(pipe, &fb_state);
2001 pipe->set_sample_mask(pipe, sample_mask);
2002
2003 blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
2004 blitter_set_dst_dimensions(ctx, zsurf->width, zsurf->height);
2005 blitter->draw_rectangle(blitter, 0, 0, zsurf->width, zsurf->height, depth,
2006 UTIL_BLITTER_ATTRIB_NONE, NULL);
2007
2008 blitter_restore_vertex_states(ctx);
2009 blitter_restore_fragment_states(ctx);
2010 blitter_restore_fb_state(ctx);
2011 blitter_restore_render_cond(ctx);
2012 blitter_unset_running_flag(ctx);
2013 }
2014
2015 void util_blitter_copy_buffer(struct blitter_context *blitter,
2016 struct pipe_resource *dst,
2017 unsigned dstx,
2018 struct pipe_resource *src,
2019 unsigned srcx,
2020 unsigned size)
2021 {
2022 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2023 struct pipe_context *pipe = ctx->base.pipe;
2024 struct pipe_vertex_buffer vb;
2025 struct pipe_stream_output_target *so_target;
2026 unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0};
2027
2028 if (srcx >= src->width0 ||
2029 dstx >= dst->width0) {
2030 return;
2031 }
2032 if (srcx + size > src->width0) {
2033 size = src->width0 - srcx;
2034 }
2035 if (dstx + size > dst->width0) {
2036 size = dst->width0 - dstx;
2037 }
2038
2039 /* Drivers not capable of Stream Out should not call this function
2040 * in the first place. */
2041 assert(ctx->has_stream_out);
2042
2043 /* Some alignment is required. */
2044 if (srcx % 4 != 0 || dstx % 4 != 0 || size % 4 != 0 ||
2045 !ctx->has_stream_out) {
2046 struct pipe_box box;
2047 u_box_1d(srcx, size, &box);
2048 util_resource_copy_region(pipe, dst, 0, dstx, 0, 0, src, 0, &box);
2049 return;
2050 }
2051
2052 blitter_set_running_flag(ctx);
2053 blitter_check_saved_vertex_states(ctx);
2054 blitter_disable_render_cond(ctx);
2055
2056 vb.buffer = src;
2057 vb.buffer_offset = srcx;
2058 vb.stride = 4;
2059
2060 pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
2061 pipe->bind_vertex_elements_state(pipe, ctx->velem_state_readbuf[0]);
2062 bind_vs_pos_only(ctx, 1);
2063 if (ctx->has_geometry_shader)
2064 pipe->bind_gs_state(pipe, NULL);
2065 if (ctx->has_tessellation) {
2066 pipe->bind_tcs_state(pipe, NULL);
2067 pipe->bind_tes_state(pipe, NULL);
2068 }
2069 pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state);
2070
2071 so_target = pipe->create_stream_output_target(pipe, dst, dstx, size);
2072 pipe->set_stream_output_targets(pipe, 1, &so_target, offsets);
2073
2074 util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
2075
2076 blitter_restore_vertex_states(ctx);
2077 blitter_restore_render_cond(ctx);
2078 blitter_unset_running_flag(ctx);
2079 pipe_so_target_reference(&so_target, NULL);
2080 }
2081
2082 void util_blitter_clear_buffer(struct blitter_context *blitter,
2083 struct pipe_resource *dst,
2084 unsigned offset, unsigned size,
2085 unsigned num_channels,
2086 const union pipe_color_union *clear_value)
2087 {
2088 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2089 struct pipe_context *pipe = ctx->base.pipe;
2090 struct pipe_vertex_buffer vb = {0};
2091 struct pipe_stream_output_target *so_target = NULL;
2092 unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0};
2093
2094 assert(num_channels >= 1);
2095 assert(num_channels <= 4);
2096
2097 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
2098 *
2099 * R600 uses this to initialize texture resources, so width0 might not be
2100 * what you think it is.
2101 */
2102
2103 /* Streamout is required. */
2104 if (!ctx->has_stream_out) {
2105 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2106 return;
2107 }
2108
2109 /* Some alignment is required. */
2110 if (offset % 4 != 0 || size % 4 != 0) {
2111 assert(!"Bad alignment in util_blitter_clear_buffer()");
2112 return;
2113 }
2114
2115 u_upload_data(ctx->upload, 0, num_channels*4, 4, clear_value,
2116 &vb.buffer_offset, &vb.buffer);
2117 if (!vb.buffer)
2118 goto out;
2119
2120 vb.stride = 0;
2121
2122 blitter_set_running_flag(ctx);
2123 blitter_check_saved_vertex_states(ctx);
2124 blitter_disable_render_cond(ctx);
2125
2126 pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
2127 pipe->bind_vertex_elements_state(pipe,
2128 ctx->velem_state_readbuf[num_channels-1]);
2129 bind_vs_pos_only(ctx, num_channels);
2130 if (ctx->has_geometry_shader)
2131 pipe->bind_gs_state(pipe, NULL);
2132 if (ctx->has_tessellation) {
2133 pipe->bind_tcs_state(pipe, NULL);
2134 pipe->bind_tes_state(pipe, NULL);
2135 }
2136 pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state);
2137
2138 so_target = pipe->create_stream_output_target(pipe, dst, offset, size);
2139 pipe->set_stream_output_targets(pipe, 1, &so_target, offsets);
2140
2141 util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
2142
2143 out:
2144 blitter_restore_vertex_states(ctx);
2145 blitter_restore_render_cond(ctx);
2146 blitter_unset_running_flag(ctx);
2147 pipe_so_target_reference(&so_target, NULL);
2148 pipe_resource_reference(&vb.buffer, NULL);
2149 }
2150
2151 /* probably radeon specific */
2152 void util_blitter_custom_resolve_color(struct blitter_context *blitter,
2153 struct pipe_resource *dst,
2154 unsigned dst_level,
2155 unsigned dst_layer,
2156 struct pipe_resource *src,
2157 unsigned src_layer,
2158 unsigned sample_mask,
2159 void *custom_blend,
2160 enum pipe_format format)
2161 {
2162 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2163 struct pipe_context *pipe = ctx->base.pipe;
2164 struct pipe_framebuffer_state fb_state;
2165 struct pipe_surface *srcsurf, *dstsurf, surf_tmpl;
2166
2167 blitter_set_running_flag(ctx);
2168 blitter_check_saved_vertex_states(ctx);
2169 blitter_check_saved_fragment_states(ctx);
2170 blitter_disable_render_cond(ctx);
2171
2172 /* bind states */
2173 pipe->bind_blend_state(pipe, custom_blend);
2174 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2175 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
2176 bind_fs_write_one_cbuf(ctx);
2177 pipe->set_sample_mask(pipe, sample_mask);
2178
2179 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
2180 surf_tmpl.format = format;
2181 surf_tmpl.u.tex.level = dst_level;
2182 surf_tmpl.u.tex.first_layer = dst_layer;
2183 surf_tmpl.u.tex.last_layer = dst_layer;
2184
2185 dstsurf = pipe->create_surface(pipe, dst, &surf_tmpl);
2186
2187 surf_tmpl.u.tex.level = 0;
2188 surf_tmpl.u.tex.first_layer = src_layer;
2189 surf_tmpl.u.tex.last_layer = src_layer;
2190
2191 srcsurf = pipe->create_surface(pipe, src, &surf_tmpl);
2192
2193 /* set a framebuffer state */
2194 fb_state.width = src->width0;
2195 fb_state.height = src->height0;
2196 fb_state.nr_cbufs = 2;
2197 fb_state.cbufs[0] = srcsurf;
2198 fb_state.cbufs[1] = dstsurf;
2199 fb_state.zsbuf = NULL;
2200 pipe->set_framebuffer_state(pipe, &fb_state);
2201
2202 blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
2203 blitter_set_dst_dimensions(ctx, src->width0, src->height0);
2204 blitter->draw_rectangle(blitter, 0, 0, src->width0, src->height0,
2205 0, 0, NULL);
2206 blitter_restore_fb_state(ctx);
2207 blitter_restore_vertex_states(ctx);
2208 blitter_restore_fragment_states(ctx);
2209 blitter_restore_render_cond(ctx);
2210 blitter_unset_running_flag(ctx);
2211
2212 pipe_surface_reference(&srcsurf, NULL);
2213 pipe_surface_reference(&dstsurf, NULL);
2214 }
2215
2216 void util_blitter_custom_color(struct blitter_context *blitter,
2217 struct pipe_surface *dstsurf,
2218 void *custom_blend)
2219 {
2220 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2221 struct pipe_context *pipe = ctx->base.pipe;
2222 struct pipe_framebuffer_state fb_state;
2223
2224 assert(dstsurf->texture);
2225 if (!dstsurf->texture)
2226 return;
2227
2228 /* check the saved state */
2229 blitter_set_running_flag(ctx);
2230 blitter_check_saved_vertex_states(ctx);
2231 blitter_check_saved_fragment_states(ctx);
2232 blitter_check_saved_fb_state(ctx);
2233 blitter_disable_render_cond(ctx);
2234
2235 /* bind states */
2236 pipe->bind_blend_state(pipe, custom_blend ? custom_blend
2237 : ctx->blend[PIPE_MASK_RGBA][0]);
2238 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2239 bind_fs_write_one_cbuf(ctx);
2240 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
2241 pipe->set_sample_mask(pipe, (1ull << MAX2(1, dstsurf->texture->nr_samples)) - 1);
2242
2243 /* set a framebuffer state */
2244 fb_state.width = dstsurf->width;
2245 fb_state.height = dstsurf->height;
2246 fb_state.nr_cbufs = 1;
2247 fb_state.cbufs[0] = dstsurf;
2248 fb_state.zsbuf = 0;
2249 pipe->set_framebuffer_state(pipe, &fb_state);
2250 pipe->set_sample_mask(pipe, ~0);
2251
2252 blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
2253 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
2254 blitter->draw_rectangle(blitter, 0, 0, dstsurf->width, dstsurf->height,
2255 0, 0, NULL);
2256
2257 blitter_restore_vertex_states(ctx);
2258 blitter_restore_fragment_states(ctx);
2259 blitter_restore_fb_state(ctx);
2260 blitter_restore_render_cond(ctx);
2261 blitter_unset_running_flag(ctx);
2262 }