gallium,utils: Fix trivial sign compare warnings
[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 unsigned 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 < ARRAY_SIZE(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< ARRAY_SIZE(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< ARRAY_SIZE(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< ARRAY_SIZE(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 ctx->base.pipe->set_active_query_state(ctx->base.pipe, false);
534 }
535
536 static void blitter_unset_running_flag(struct blitter_context_priv *ctx)
537 {
538 if (!ctx->base.running) {
539 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
540 __LINE__);
541 }
542 ctx->base.running = FALSE;
543
544 ctx->base.pipe->set_active_query_state(ctx->base.pipe, true);
545 }
546
547 static void blitter_check_saved_vertex_states(struct blitter_context_priv *ctx)
548 {
549 assert(ctx->base.saved_velem_state != INVALID_PTR);
550 assert(ctx->base.saved_vs != INVALID_PTR);
551 assert(!ctx->has_geometry_shader || ctx->base.saved_gs != INVALID_PTR);
552 assert(!ctx->has_tessellation || ctx->base.saved_tcs != INVALID_PTR);
553 assert(!ctx->has_tessellation || ctx->base.saved_tes != INVALID_PTR);
554 assert(!ctx->has_stream_out || ctx->base.saved_num_so_targets != ~0u);
555 assert(ctx->base.saved_rs_state != INVALID_PTR);
556 }
557
558 static void blitter_restore_vertex_states(struct blitter_context_priv *ctx)
559 {
560 struct pipe_context *pipe = ctx->base.pipe;
561 unsigned i;
562
563 /* Vertex buffer. */
564 pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1,
565 &ctx->base.saved_vertex_buffer);
566 pipe_resource_reference(&ctx->base.saved_vertex_buffer.buffer, NULL);
567
568 /* Vertex elements. */
569 pipe->bind_vertex_elements_state(pipe, ctx->base.saved_velem_state);
570 ctx->base.saved_velem_state = INVALID_PTR;
571
572 /* Vertex shader. */
573 pipe->bind_vs_state(pipe, ctx->base.saved_vs);
574 ctx->base.saved_vs = INVALID_PTR;
575
576 /* Geometry shader. */
577 if (ctx->has_geometry_shader) {
578 pipe->bind_gs_state(pipe, ctx->base.saved_gs);
579 ctx->base.saved_gs = INVALID_PTR;
580 }
581
582 if (ctx->has_tessellation) {
583 pipe->bind_tcs_state(pipe, ctx->base.saved_tcs);
584 pipe->bind_tes_state(pipe, ctx->base.saved_tes);
585 ctx->base.saved_tcs = INVALID_PTR;
586 ctx->base.saved_tes = INVALID_PTR;
587 }
588
589 /* Stream outputs. */
590 if (ctx->has_stream_out) {
591 unsigned offsets[PIPE_MAX_SO_BUFFERS];
592 for (i = 0; i < ctx->base.saved_num_so_targets; i++)
593 offsets[i] = (unsigned)-1;
594 pipe->set_stream_output_targets(pipe,
595 ctx->base.saved_num_so_targets,
596 ctx->base.saved_so_targets, offsets);
597
598 for (i = 0; i < ctx->base.saved_num_so_targets; i++)
599 pipe_so_target_reference(&ctx->base.saved_so_targets[i], NULL);
600
601 ctx->base.saved_num_so_targets = ~0;
602 }
603
604 /* Rasterizer. */
605 pipe->bind_rasterizer_state(pipe, ctx->base.saved_rs_state);
606 ctx->base.saved_rs_state = INVALID_PTR;
607 }
608
609 static void blitter_check_saved_fragment_states(struct blitter_context_priv *ctx)
610 {
611 assert(ctx->base.saved_fs != INVALID_PTR);
612 assert(ctx->base.saved_dsa_state != INVALID_PTR);
613 assert(ctx->base.saved_blend_state != INVALID_PTR);
614 }
615
616 static void blitter_restore_fragment_states(struct blitter_context_priv *ctx)
617 {
618 struct pipe_context *pipe = ctx->base.pipe;
619
620 /* Fragment shader. */
621 ctx->bind_fs_state(pipe, ctx->base.saved_fs);
622 ctx->base.saved_fs = INVALID_PTR;
623
624 /* Depth, stencil, alpha. */
625 pipe->bind_depth_stencil_alpha_state(pipe, ctx->base.saved_dsa_state);
626 ctx->base.saved_dsa_state = INVALID_PTR;
627
628 /* Blend state. */
629 pipe->bind_blend_state(pipe, ctx->base.saved_blend_state);
630 ctx->base.saved_blend_state = INVALID_PTR;
631
632 /* Sample mask. */
633 if (ctx->base.is_sample_mask_saved) {
634 pipe->set_sample_mask(pipe, ctx->base.saved_sample_mask);
635 ctx->base.is_sample_mask_saved = FALSE;
636 }
637
638 /* Miscellaneous states. */
639 /* XXX check whether these are saved and whether they need to be restored
640 * (depending on the operation) */
641 pipe->set_stencil_ref(pipe, &ctx->base.saved_stencil_ref);
642 pipe->set_viewport_states(pipe, 0, 1, &ctx->base.saved_viewport);
643 }
644
645 static void blitter_check_saved_fb_state(struct blitter_context_priv *ctx)
646 {
647 assert(ctx->base.saved_fb_state.nr_cbufs != ~0u);
648 }
649
650 static void blitter_disable_render_cond(struct blitter_context_priv *ctx)
651 {
652 struct pipe_context *pipe = ctx->base.pipe;
653
654 if (ctx->base.saved_render_cond_query) {
655 pipe->render_condition(pipe, NULL, FALSE, 0);
656 }
657 }
658
659 static void blitter_restore_render_cond(struct blitter_context_priv *ctx)
660 {
661 struct pipe_context *pipe = ctx->base.pipe;
662
663 if (ctx->base.saved_render_cond_query) {
664 pipe->render_condition(pipe, ctx->base.saved_render_cond_query,
665 ctx->base.saved_render_cond_cond,
666 ctx->base.saved_render_cond_mode);
667 ctx->base.saved_render_cond_query = NULL;
668 }
669 }
670
671 static void blitter_restore_fb_state(struct blitter_context_priv *ctx)
672 {
673 struct pipe_context *pipe = ctx->base.pipe;
674
675 pipe->set_framebuffer_state(pipe, &ctx->base.saved_fb_state);
676 util_unreference_framebuffer_state(&ctx->base.saved_fb_state);
677 }
678
679 static void blitter_check_saved_textures(struct blitter_context_priv *ctx)
680 {
681 assert(ctx->base.saved_num_sampler_states != ~0u);
682 assert(ctx->base.saved_num_sampler_views != ~0u);
683 }
684
685 static void blitter_restore_textures(struct blitter_context_priv *ctx)
686 {
687 struct pipe_context *pipe = ctx->base.pipe;
688 unsigned i;
689
690 /* Fragment sampler states. */
691 pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
692 ctx->base.saved_num_sampler_states,
693 ctx->base.saved_sampler_states);
694
695 ctx->base.saved_num_sampler_states = ~0;
696
697 /* Fragment sampler views. */
698 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,
699 ctx->base.saved_num_sampler_views,
700 ctx->base.saved_sampler_views);
701
702 for (i = 0; i < ctx->base.saved_num_sampler_views; i++)
703 pipe_sampler_view_reference(&ctx->base.saved_sampler_views[i], NULL);
704
705 ctx->base.saved_num_sampler_views = ~0;
706 }
707
708 static void blitter_set_rectangle(struct blitter_context_priv *ctx,
709 int x1, int y1, int x2, int y2,
710 float depth)
711 {
712 int i;
713
714 /* set vertex positions */
715 ctx->vertices[0][0][0] = (float)x1 / ctx->dst_width * 2.0f - 1.0f; /*v0.x*/
716 ctx->vertices[0][0][1] = (float)y1 / ctx->dst_height * 2.0f - 1.0f; /*v0.y*/
717
718 ctx->vertices[1][0][0] = (float)x2 / ctx->dst_width * 2.0f - 1.0f; /*v1.x*/
719 ctx->vertices[1][0][1] = (float)y1 / ctx->dst_height * 2.0f - 1.0f; /*v1.y*/
720
721 ctx->vertices[2][0][0] = (float)x2 / ctx->dst_width * 2.0f - 1.0f; /*v2.x*/
722 ctx->vertices[2][0][1] = (float)y2 / ctx->dst_height * 2.0f - 1.0f; /*v2.y*/
723
724 ctx->vertices[3][0][0] = (float)x1 / ctx->dst_width * 2.0f - 1.0f; /*v3.x*/
725 ctx->vertices[3][0][1] = (float)y2 / ctx->dst_height * 2.0f - 1.0f; /*v3.y*/
726
727 for (i = 0; i < 4; i++)
728 ctx->vertices[i][0][2] = depth; /*z*/
729
730 /* viewport */
731 ctx->viewport.scale[0] = 0.5f * ctx->dst_width;
732 ctx->viewport.scale[1] = 0.5f * ctx->dst_height;
733 ctx->viewport.scale[2] = 1.0f;
734 ctx->viewport.translate[0] = 0.5f * ctx->dst_width;
735 ctx->viewport.translate[1] = 0.5f * ctx->dst_height;
736 ctx->viewport.translate[2] = 0.0f;
737 ctx->base.pipe->set_viewport_states(ctx->base.pipe, 0, 1, &ctx->viewport);
738 }
739
740 static void blitter_set_clear_color(struct blitter_context_priv *ctx,
741 const union pipe_color_union *color)
742 {
743 int i;
744
745 if (color) {
746 for (i = 0; i < 4; i++) {
747 uint32_t *uiverts = (uint32_t *)ctx->vertices[i][1];
748 uiverts[0] = color->ui[0];
749 uiverts[1] = color->ui[1];
750 uiverts[2] = color->ui[2];
751 uiverts[3] = color->ui[3];
752 }
753 } else {
754 for (i = 0; i < 4; i++) {
755 ctx->vertices[i][1][0] = 0;
756 ctx->vertices[i][1][1] = 0;
757 ctx->vertices[i][1][2] = 0;
758 ctx->vertices[i][1][3] = 0;
759 }
760 }
761 }
762
763 static void get_texcoords(struct pipe_sampler_view *src,
764 unsigned src_width0, unsigned src_height0,
765 int x1, int y1, int x2, int y2,
766 float out[4])
767 {
768 struct pipe_resource *tex = src->texture;
769 unsigned level = src->u.tex.first_level;
770 boolean normalized = tex->target != PIPE_TEXTURE_RECT &&
771 tex->nr_samples <= 1;
772
773 if (normalized) {
774 out[0] = x1 / (float)u_minify(src_width0, level);
775 out[1] = y1 / (float)u_minify(src_height0, level);
776 out[2] = x2 / (float)u_minify(src_width0, level);
777 out[3] = y2 / (float)u_minify(src_height0, level);
778 } else {
779 out[0] = (float) x1;
780 out[1] = (float) y1;
781 out[2] = (float) x2;
782 out[3] = (float) y2;
783 }
784 }
785
786 static void set_texcoords_in_vertices(const float coord[4],
787 float *out, unsigned stride)
788 {
789 out[0] = coord[0]; /*t0.s*/
790 out[1] = coord[1]; /*t0.t*/
791 out += stride;
792 out[0] = coord[2]; /*t1.s*/
793 out[1] = coord[1]; /*t1.t*/
794 out += stride;
795 out[0] = coord[2]; /*t2.s*/
796 out[1] = coord[3]; /*t2.t*/
797 out += stride;
798 out[0] = coord[0]; /*t3.s*/
799 out[1] = coord[3]; /*t3.t*/
800 }
801
802 static void blitter_set_texcoords(struct blitter_context_priv *ctx,
803 struct pipe_sampler_view *src,
804 unsigned src_width0, unsigned src_height0,
805 float layer, unsigned sample,
806 int x1, int y1, int x2, int y2)
807 {
808 unsigned i;
809 float coord[4];
810 float face_coord[4][2];
811
812 get_texcoords(src, src_width0, src_height0, x1, y1, x2, y2, coord);
813
814 if (src->texture->target == PIPE_TEXTURE_CUBE ||
815 src->texture->target == PIPE_TEXTURE_CUBE_ARRAY) {
816 set_texcoords_in_vertices(coord, &face_coord[0][0], 2);
817 util_map_texcoords2d_onto_cubemap((unsigned)layer % 6,
818 /* pointer, stride in floats */
819 &face_coord[0][0], 2,
820 &ctx->vertices[0][1][0], 8,
821 FALSE);
822 } else {
823 set_texcoords_in_vertices(coord, &ctx->vertices[0][1][0], 8);
824 }
825
826 /* Set the layer. */
827 switch (src->texture->target) {
828 case PIPE_TEXTURE_3D:
829 {
830 float r = layer / (float)u_minify(src->texture->depth0,
831 src->u.tex.first_level);
832 for (i = 0; i < 4; i++)
833 ctx->vertices[i][1][2] = r; /*r*/
834 }
835 break;
836
837 case PIPE_TEXTURE_1D_ARRAY:
838 for (i = 0; i < 4; i++)
839 ctx->vertices[i][1][1] = (float) layer; /*t*/
840 break;
841
842 case PIPE_TEXTURE_2D_ARRAY:
843 for (i = 0; i < 4; i++) {
844 ctx->vertices[i][1][2] = (float) layer; /*r*/
845 ctx->vertices[i][1][3] = (float) sample; /*q*/
846 }
847 break;
848
849 case PIPE_TEXTURE_CUBE_ARRAY:
850 for (i = 0; i < 4; i++)
851 ctx->vertices[i][1][3] = (float) ((unsigned)layer / 6); /*w*/
852 break;
853
854 case PIPE_TEXTURE_2D:
855 for (i = 0; i < 4; i++) {
856 ctx->vertices[i][1][3] = (float) sample; /*r*/
857 }
858 break;
859
860 default:;
861 }
862 }
863
864 static void blitter_set_dst_dimensions(struct blitter_context_priv *ctx,
865 unsigned width, unsigned height)
866 {
867 ctx->dst_width = width;
868 ctx->dst_height = height;
869 }
870
871 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx,
872 enum pipe_format format,
873 enum pipe_texture_target target,
874 unsigned src_nr_samples,
875 unsigned dst_nr_samples,
876 unsigned filter)
877 {
878 struct pipe_context *pipe = ctx->base.pipe;
879 unsigned tgsi_tex = util_pipe_tex_to_tgsi_tex(target, src_nr_samples);
880 enum tgsi_return_type stype;
881
882 assert(target < PIPE_MAX_TEXTURE_TYPES);
883
884 if (util_format_is_pure_uint(format))
885 stype = TGSI_RETURN_TYPE_UINT;
886 else if (util_format_is_pure_sint(format))
887 stype = TGSI_RETURN_TYPE_SINT;
888 else
889 stype = TGSI_RETURN_TYPE_FLOAT;
890
891 if (src_nr_samples > 1) {
892 void **shader;
893
894 if (dst_nr_samples <= 1) {
895 /* The destination has one sample, so we'll do color resolve. */
896 unsigned index = GET_MSAA_RESOLVE_FS_IDX(src_nr_samples);
897
898 assert(filter < 2);
899
900 if (stype == TGSI_RETURN_TYPE_UINT)
901 shader = &ctx->fs_resolve_uint[target][index][filter];
902 else if (stype == TGSI_RETURN_TYPE_SINT)
903 shader = &ctx->fs_resolve_sint[target][index][filter];
904 else
905 shader = &ctx->fs_resolve[target][index][filter];
906
907 if (!*shader) {
908 assert(!ctx->cached_all_shaders);
909 if (filter == PIPE_TEX_FILTER_LINEAR) {
910 *shader = util_make_fs_msaa_resolve_bilinear(pipe, tgsi_tex,
911 src_nr_samples,
912 stype);
913 }
914 else {
915 *shader = util_make_fs_msaa_resolve(pipe, tgsi_tex,
916 src_nr_samples,
917 stype);
918 }
919 }
920 }
921 else {
922 /* The destination has multiple samples, we'll do
923 * an MSAA->MSAA copy.
924 */
925 if (stype == TGSI_RETURN_TYPE_UINT)
926 shader = &ctx->fs_texfetch_col_msaa_uint[target];
927 else if (stype == TGSI_RETURN_TYPE_SINT)
928 shader = &ctx->fs_texfetch_col_msaa_sint[target];
929 else
930 shader = &ctx->fs_texfetch_col_msaa[target];
931
932 /* Create the fragment shader on-demand. */
933 if (!*shader) {
934 assert(!ctx->cached_all_shaders);
935 *shader = util_make_fs_blit_msaa_color(pipe, tgsi_tex, stype);
936 }
937 }
938
939 return *shader;
940 } else {
941 void **shader;
942
943 if (stype == TGSI_RETURN_TYPE_UINT)
944 shader = &ctx->fs_texfetch_col_uint[target];
945 else if (stype == TGSI_RETURN_TYPE_SINT)
946 shader = &ctx->fs_texfetch_col_sint[target];
947 else
948 shader = &ctx->fs_texfetch_col[target];
949
950 /* Create the fragment shader on-demand. */
951 if (!*shader) {
952 assert(!ctx->cached_all_shaders);
953 *shader = util_make_fragment_tex_shader(pipe, tgsi_tex,
954 TGSI_INTERPOLATE_LINEAR,
955 stype);
956 }
957
958 return *shader;
959 }
960 }
961
962 static inline
963 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx,
964 enum pipe_texture_target target,
965 unsigned nr_samples)
966 {
967 struct pipe_context *pipe = ctx->base.pipe;
968
969 assert(target < PIPE_MAX_TEXTURE_TYPES);
970
971 if (nr_samples > 1) {
972 void **shader = &ctx->fs_texfetch_depth_msaa[target];
973
974 /* Create the fragment shader on-demand. */
975 if (!*shader) {
976 unsigned tgsi_tex;
977 assert(!ctx->cached_all_shaders);
978 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
979 *shader = util_make_fs_blit_msaa_depth(pipe, tgsi_tex);
980 }
981
982 return *shader;
983 } else {
984 void **shader = &ctx->fs_texfetch_depth[target];
985
986 /* Create the fragment shader on-demand. */
987 if (!*shader) {
988 unsigned tgsi_tex;
989 assert(!ctx->cached_all_shaders);
990 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
991 *shader =
992 util_make_fragment_tex_shader_writedepth(pipe, tgsi_tex,
993 TGSI_INTERPOLATE_LINEAR);
994 }
995
996 return *shader;
997 }
998 }
999
1000 static inline
1001 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv *ctx,
1002 enum pipe_texture_target target,
1003 unsigned nr_samples)
1004 {
1005 struct pipe_context *pipe = ctx->base.pipe;
1006
1007 assert(target < PIPE_MAX_TEXTURE_TYPES);
1008
1009 if (nr_samples > 1) {
1010 void **shader = &ctx->fs_texfetch_depthstencil_msaa[target];
1011
1012 /* Create the fragment shader on-demand. */
1013 if (!*shader) {
1014 unsigned tgsi_tex;
1015 assert(!ctx->cached_all_shaders);
1016 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
1017 *shader = util_make_fs_blit_msaa_depthstencil(pipe, tgsi_tex);
1018 }
1019
1020 return *shader;
1021 } else {
1022 void **shader = &ctx->fs_texfetch_depthstencil[target];
1023
1024 /* Create the fragment shader on-demand. */
1025 if (!*shader) {
1026 unsigned tgsi_tex;
1027 assert(!ctx->cached_all_shaders);
1028 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
1029 *shader =
1030 util_make_fragment_tex_shader_writedepthstencil(pipe, tgsi_tex,
1031 TGSI_INTERPOLATE_LINEAR);
1032 }
1033
1034 return *shader;
1035 }
1036 }
1037
1038 static inline
1039 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv *ctx,
1040 enum pipe_texture_target target,
1041 unsigned nr_samples)
1042 {
1043 struct pipe_context *pipe = ctx->base.pipe;
1044
1045 assert(target < PIPE_MAX_TEXTURE_TYPES);
1046
1047 if (nr_samples > 1) {
1048 void **shader = &ctx->fs_texfetch_stencil_msaa[target];
1049
1050 /* Create the fragment shader on-demand. */
1051 if (!*shader) {
1052 unsigned tgsi_tex;
1053 assert(!ctx->cached_all_shaders);
1054 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
1055 *shader = util_make_fs_blit_msaa_stencil(pipe, tgsi_tex);
1056 }
1057
1058 return *shader;
1059 } else {
1060 void **shader = &ctx->fs_texfetch_stencil[target];
1061
1062 /* Create the fragment shader on-demand. */
1063 if (!*shader) {
1064 unsigned tgsi_tex;
1065 assert(!ctx->cached_all_shaders);
1066 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
1067 *shader =
1068 util_make_fragment_tex_shader_writestencil(pipe, tgsi_tex,
1069 TGSI_INTERPOLATE_LINEAR);
1070 }
1071
1072 return *shader;
1073 }
1074 }
1075
1076
1077 /**
1078 * Generate and save all fragment shaders that we will ever need for
1079 * blitting. Drivers which use the 'draw' fallbacks will typically use
1080 * this to make sure we generate/use shaders that don't go through the
1081 * draw module's wrapper functions.
1082 */
1083 void util_blitter_cache_all_shaders(struct blitter_context *blitter)
1084 {
1085 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1086 struct pipe_context *pipe = blitter->pipe;
1087 struct pipe_screen *screen = pipe->screen;
1088 unsigned samples, j, f, target, max_samples;
1089 boolean has_arraytex, has_cubearraytex;
1090
1091 max_samples = ctx->has_texture_multisample ? 2 : 1;
1092 has_arraytex = screen->get_param(screen,
1093 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS) != 0;
1094 has_cubearraytex = screen->get_param(screen,
1095 PIPE_CAP_CUBE_MAP_ARRAY) != 0;
1096
1097 /* It only matters if i <= 1 or > 1. */
1098 for (samples = 1; samples <= max_samples; samples++) {
1099 for (target = PIPE_TEXTURE_1D; target < PIPE_MAX_TEXTURE_TYPES; target++) {
1100 if (!has_arraytex &&
1101 (target == PIPE_TEXTURE_1D_ARRAY ||
1102 target == PIPE_TEXTURE_2D_ARRAY)) {
1103 continue;
1104 }
1105 if (!has_cubearraytex &&
1106 (target == PIPE_TEXTURE_CUBE_ARRAY))
1107 continue;
1108
1109 if (samples > 1 &&
1110 (target != PIPE_TEXTURE_2D &&
1111 target != PIPE_TEXTURE_2D_ARRAY))
1112 continue;
1113
1114 /* If samples == 1, the shaders read one texel. If samples >= 1,
1115 * they read one sample.
1116 */
1117 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_FLOAT, target,
1118 samples, samples, 0);
1119 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT, target,
1120 samples, samples, 0);
1121 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT, target,
1122 samples, samples, 0);
1123 blitter_get_fs_texfetch_depth(ctx, target, samples);
1124 if (ctx->has_stencil_export) {
1125 blitter_get_fs_texfetch_depthstencil(ctx, target, samples);
1126 blitter_get_fs_texfetch_stencil(ctx, target, samples);
1127 }
1128
1129 if (samples == 1)
1130 continue;
1131
1132 /* MSAA resolve shaders. */
1133 for (j = 2; j < 32; j++) {
1134 if (!screen->is_format_supported(screen, PIPE_FORMAT_R32_FLOAT,
1135 target, j,
1136 PIPE_BIND_SAMPLER_VIEW)) {
1137 continue;
1138 }
1139
1140 for (f = 0; f < 2; f++) {
1141 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_FLOAT, target,
1142 j, 1, f);
1143 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT, target,
1144 j, 1, f);
1145 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT, target,
1146 j, 1, f);
1147 }
1148 }
1149 }
1150 }
1151
1152 ctx->fs_empty = util_make_empty_fragment_shader(pipe);
1153
1154 ctx->fs_write_one_cbuf =
1155 util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
1156 TGSI_INTERPOLATE_CONSTANT, FALSE);
1157
1158 ctx->fs_write_all_cbufs =
1159 util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
1160 TGSI_INTERPOLATE_CONSTANT, TRUE);
1161
1162 ctx->cached_all_shaders = TRUE;
1163 }
1164
1165 static void blitter_set_common_draw_rect_state(struct blitter_context_priv *ctx,
1166 boolean scissor,
1167 boolean vs_layered)
1168 {
1169 struct pipe_context *pipe = ctx->base.pipe;
1170
1171 pipe->bind_rasterizer_state(pipe, scissor ? ctx->rs_state_scissor
1172 : ctx->rs_state);
1173 if (vs_layered)
1174 bind_vs_layered(ctx);
1175 else
1176 bind_vs_passthrough(ctx);
1177
1178 if (ctx->has_geometry_shader)
1179 pipe->bind_gs_state(pipe, NULL);
1180 if (ctx->has_tessellation) {
1181 pipe->bind_tcs_state(pipe, NULL);
1182 pipe->bind_tes_state(pipe, NULL);
1183 }
1184 if (ctx->has_stream_out)
1185 pipe->set_stream_output_targets(pipe, 0, NULL, NULL);
1186 }
1187
1188 static void blitter_draw(struct blitter_context_priv *ctx,
1189 int x1, int y1, int x2, int y2, float depth,
1190 unsigned num_instances)
1191 {
1192 struct pipe_context *pipe = ctx->base.pipe;
1193 struct pipe_vertex_buffer vb = {0};
1194
1195 blitter_set_rectangle(ctx, x1, y1, x2, y2, depth);
1196
1197 vb.stride = 8 * sizeof(float);
1198
1199 u_upload_data(ctx->upload, 0, sizeof(ctx->vertices), 4, ctx->vertices,
1200 &vb.buffer_offset, &vb.buffer);
1201 if (!vb.buffer)
1202 return;
1203 u_upload_unmap(ctx->upload);
1204
1205 pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
1206 util_draw_arrays_instanced(pipe, PIPE_PRIM_TRIANGLE_FAN, 0, 4,
1207 0, num_instances);
1208 pipe_resource_reference(&vb.buffer, NULL);
1209 }
1210
1211 void util_blitter_draw_rectangle(struct blitter_context *blitter,
1212 int x1, int y1, int x2, int y2, float depth,
1213 enum blitter_attrib_type type,
1214 const union pipe_color_union *attrib)
1215 {
1216 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1217
1218 switch (type) {
1219 case UTIL_BLITTER_ATTRIB_COLOR:
1220 blitter_set_clear_color(ctx, attrib);
1221 break;
1222
1223 case UTIL_BLITTER_ATTRIB_TEXCOORD:
1224 set_texcoords_in_vertices(attrib->f, &ctx->vertices[0][1][0], 8);
1225 break;
1226
1227 default:;
1228 }
1229
1230 blitter_draw(ctx, x1, y1, x2, y2, depth, 1);
1231 }
1232
1233 static void *get_clear_blend_state(struct blitter_context_priv *ctx,
1234 unsigned clear_buffers)
1235 {
1236 struct pipe_context *pipe = ctx->base.pipe;
1237 int index;
1238
1239 clear_buffers &= PIPE_CLEAR_COLOR;
1240
1241 /* Return an existing blend state. */
1242 if (!clear_buffers)
1243 return ctx->blend[0][0];
1244
1245 index = GET_CLEAR_BLEND_STATE_IDX(clear_buffers);
1246
1247 if (ctx->blend_clear[index])
1248 return ctx->blend_clear[index];
1249
1250 /* Create a new one. */
1251 {
1252 struct pipe_blend_state blend = {0};
1253 unsigned i;
1254
1255 blend.independent_blend_enable = 1;
1256
1257 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
1258 if (clear_buffers & (PIPE_CLEAR_COLOR0 << i)) {
1259 blend.rt[i].colormask = PIPE_MASK_RGBA;
1260 }
1261 }
1262
1263 ctx->blend_clear[index] = pipe->create_blend_state(pipe, &blend);
1264 }
1265 return ctx->blend_clear[index];
1266 }
1267
1268 static void util_blitter_clear_custom(struct blitter_context *blitter,
1269 unsigned width, unsigned height,
1270 unsigned num_layers,
1271 unsigned clear_buffers,
1272 const union pipe_color_union *color,
1273 double depth, unsigned stencil,
1274 void *custom_blend, void *custom_dsa)
1275 {
1276 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1277 struct pipe_context *pipe = ctx->base.pipe;
1278 struct pipe_stencil_ref sr = { { 0 } };
1279
1280 assert(ctx->has_layered || num_layers <= 1);
1281
1282 blitter_set_running_flag(ctx);
1283 blitter_check_saved_vertex_states(ctx);
1284 blitter_check_saved_fragment_states(ctx);
1285 blitter_disable_render_cond(ctx);
1286
1287 /* bind states */
1288 if (custom_blend) {
1289 pipe->bind_blend_state(pipe, custom_blend);
1290 } else {
1291 pipe->bind_blend_state(pipe, get_clear_blend_state(ctx, clear_buffers));
1292 }
1293
1294 if (custom_dsa) {
1295 pipe->bind_depth_stencil_alpha_state(pipe, custom_dsa);
1296 } else if ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
1297 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
1298 } else if (clear_buffers & PIPE_CLEAR_DEPTH) {
1299 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
1300 } else if (clear_buffers & PIPE_CLEAR_STENCIL) {
1301 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
1302 } else {
1303 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1304 }
1305
1306 sr.ref_value[0] = stencil & 0xff;
1307 pipe->set_stencil_ref(pipe, &sr);
1308
1309 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1310 bind_fs_write_all_cbufs(ctx);
1311 pipe->set_sample_mask(pipe, ~0);
1312
1313 blitter_set_dst_dimensions(ctx, width, height);
1314
1315 if (num_layers > 1 && ctx->has_layered) {
1316 blitter_set_common_draw_rect_state(ctx, FALSE, TRUE);
1317 blitter_set_clear_color(ctx, color);
1318 blitter_draw(ctx, 0, 0, width, height, depth, num_layers);
1319 }
1320 else {
1321 blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
1322 blitter->draw_rectangle(blitter, 0, 0, width, height, (float) depth,
1323 UTIL_BLITTER_ATTRIB_COLOR, color);
1324 }
1325
1326 blitter_restore_vertex_states(ctx);
1327 blitter_restore_fragment_states(ctx);
1328 blitter_restore_render_cond(ctx);
1329 blitter_unset_running_flag(ctx);
1330 }
1331
1332 void util_blitter_clear(struct blitter_context *blitter,
1333 unsigned width, unsigned height, unsigned num_layers,
1334 unsigned clear_buffers,
1335 const union pipe_color_union *color,
1336 double depth, unsigned stencil)
1337 {
1338 util_blitter_clear_custom(blitter, width, height, num_layers,
1339 clear_buffers, color, depth, stencil,
1340 NULL, NULL);
1341 }
1342
1343 void util_blitter_custom_clear_depth(struct blitter_context *blitter,
1344 unsigned width, unsigned height,
1345 double depth, void *custom_dsa)
1346 {
1347 static const union pipe_color_union color;
1348 util_blitter_clear_custom(blitter, width, height, 0, 0, &color, depth, 0,
1349 NULL, custom_dsa);
1350 }
1351
1352 void util_blitter_default_dst_texture(struct pipe_surface *dst_templ,
1353 struct pipe_resource *dst,
1354 unsigned dstlevel,
1355 unsigned dstz)
1356 {
1357 memset(dst_templ, 0, sizeof(*dst_templ));
1358 dst_templ->format = util_format_linear(dst->format);
1359 dst_templ->u.tex.level = dstlevel;
1360 dst_templ->u.tex.first_layer = dstz;
1361 dst_templ->u.tex.last_layer = dstz;
1362 }
1363
1364 static struct pipe_surface *
1365 util_blitter_get_next_surface_layer(struct pipe_context *pipe,
1366 struct pipe_surface *surf)
1367 {
1368 struct pipe_surface dst_templ;
1369
1370 memset(&dst_templ, 0, sizeof(dst_templ));
1371 dst_templ.format = surf->format;
1372 dst_templ.u.tex.level = surf->u.tex.level;
1373 dst_templ.u.tex.first_layer = surf->u.tex.first_layer + 1;
1374 dst_templ.u.tex.last_layer = surf->u.tex.last_layer + 1;
1375
1376 return pipe->create_surface(pipe, surf->texture, &dst_templ);
1377 }
1378
1379 void util_blitter_default_src_texture(struct pipe_sampler_view *src_templ,
1380 struct pipe_resource *src,
1381 unsigned srclevel)
1382 {
1383 memset(src_templ, 0, sizeof(*src_templ));
1384 src_templ->target = src->target;
1385 src_templ->format = util_format_linear(src->format);
1386 src_templ->u.tex.first_level = srclevel;
1387 src_templ->u.tex.last_level = srclevel;
1388 src_templ->u.tex.first_layer = 0;
1389 src_templ->u.tex.last_layer =
1390 src->target == PIPE_TEXTURE_3D ? u_minify(src->depth0, srclevel) - 1
1391 : src->array_size - 1;
1392 src_templ->swizzle_r = PIPE_SWIZZLE_X;
1393 src_templ->swizzle_g = PIPE_SWIZZLE_Y;
1394 src_templ->swizzle_b = PIPE_SWIZZLE_Z;
1395 src_templ->swizzle_a = PIPE_SWIZZLE_W;
1396 }
1397
1398 static boolean is_blit_generic_supported(struct blitter_context *blitter,
1399 const struct pipe_resource *dst,
1400 enum pipe_format dst_format,
1401 const struct pipe_resource *src,
1402 enum pipe_format src_format,
1403 unsigned mask)
1404 {
1405 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1406 struct pipe_screen *screen = ctx->base.pipe->screen;
1407
1408 if (dst) {
1409 unsigned bind;
1410 const struct util_format_description *desc =
1411 util_format_description(dst_format);
1412 boolean dst_has_stencil = util_format_has_stencil(desc);
1413
1414 /* Stencil export must be supported for stencil copy. */
1415 if ((mask & PIPE_MASK_S) && dst_has_stencil &&
1416 !ctx->has_stencil_export) {
1417 return FALSE;
1418 }
1419
1420 if (dst_has_stencil || util_format_has_depth(desc))
1421 bind = PIPE_BIND_DEPTH_STENCIL;
1422 else
1423 bind = PIPE_BIND_RENDER_TARGET;
1424
1425 if (!screen->is_format_supported(screen, dst_format, dst->target,
1426 dst->nr_samples, bind)) {
1427 return FALSE;
1428 }
1429 }
1430
1431 if (src) {
1432 if (src->nr_samples > 1 && !ctx->has_texture_multisample) {
1433 return FALSE;
1434 }
1435
1436 if (!screen->is_format_supported(screen, src_format, src->target,
1437 src->nr_samples, PIPE_BIND_SAMPLER_VIEW)) {
1438 return FALSE;
1439 }
1440
1441 /* Check stencil sampler support for stencil copy. */
1442 if (mask & PIPE_MASK_S) {
1443 if (util_format_has_stencil(util_format_description(src_format))) {
1444 enum pipe_format stencil_format =
1445 util_format_stencil_only(src_format);
1446 assert(stencil_format != PIPE_FORMAT_NONE);
1447
1448 if (stencil_format != src_format &&
1449 !screen->is_format_supported(screen, stencil_format,
1450 src->target, src->nr_samples,
1451 PIPE_BIND_SAMPLER_VIEW)) {
1452 return FALSE;
1453 }
1454 }
1455 }
1456 }
1457
1458 return TRUE;
1459 }
1460
1461 boolean util_blitter_is_copy_supported(struct blitter_context *blitter,
1462 const struct pipe_resource *dst,
1463 const struct pipe_resource *src)
1464 {
1465 return is_blit_generic_supported(blitter, dst, dst->format,
1466 src, src->format, PIPE_MASK_RGBAZS);
1467 }
1468
1469 boolean util_blitter_is_blit_supported(struct blitter_context *blitter,
1470 const struct pipe_blit_info *info)
1471 {
1472 return is_blit_generic_supported(blitter,
1473 info->dst.resource, info->dst.format,
1474 info->src.resource, info->src.format,
1475 info->mask);
1476 }
1477
1478 void util_blitter_copy_texture(struct blitter_context *blitter,
1479 struct pipe_resource *dst,
1480 unsigned dst_level,
1481 unsigned dstx, unsigned dsty, unsigned dstz,
1482 struct pipe_resource *src,
1483 unsigned src_level,
1484 const struct pipe_box *srcbox)
1485 {
1486 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1487 struct pipe_context *pipe = ctx->base.pipe;
1488 struct pipe_surface *dst_view, dst_templ;
1489 struct pipe_sampler_view src_templ, *src_view;
1490 struct pipe_box dstbox;
1491
1492 assert(dst && src);
1493 assert(src->target < PIPE_MAX_TEXTURE_TYPES);
1494
1495 u_box_3d(dstx, dsty, dstz, abs(srcbox->width), abs(srcbox->height),
1496 abs(srcbox->depth), &dstbox);
1497
1498 /* Initialize the surface. */
1499 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
1500 dst_view = pipe->create_surface(pipe, dst, &dst_templ);
1501
1502 /* Initialize the sampler view. */
1503 util_blitter_default_src_texture(&src_templ, src, src_level);
1504 src_view = pipe->create_sampler_view(pipe, src, &src_templ);
1505
1506 /* Copy. */
1507 util_blitter_blit_generic(blitter, dst_view, &dstbox,
1508 src_view, srcbox, src->width0, src->height0,
1509 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,
1510 FALSE);
1511
1512 pipe_surface_reference(&dst_view, NULL);
1513 pipe_sampler_view_reference(&src_view, NULL);
1514 }
1515
1516 void util_blitter_blit_generic(struct blitter_context *blitter,
1517 struct pipe_surface *dst,
1518 const struct pipe_box *dstbox,
1519 struct pipe_sampler_view *src,
1520 const struct pipe_box *srcbox,
1521 unsigned src_width0, unsigned src_height0,
1522 unsigned mask, unsigned filter,
1523 const struct pipe_scissor_state *scissor,
1524 boolean alpha_blend)
1525 {
1526 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1527 struct pipe_context *pipe = ctx->base.pipe;
1528 struct pipe_framebuffer_state fb_state = {0};
1529 enum pipe_texture_target src_target = src->texture->target;
1530 unsigned src_samples = src->texture->nr_samples;
1531 unsigned dst_samples = dst->texture->nr_samples;
1532 boolean has_depth, has_stencil, has_color;
1533 boolean blit_stencil, blit_depth, blit_color;
1534 void *sampler_state;
1535 const struct util_format_description *src_desc =
1536 util_format_description(src->format);
1537 const struct util_format_description *dst_desc =
1538 util_format_description(dst->format);
1539
1540 has_color = src_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS &&
1541 dst_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS;
1542 has_depth = util_format_has_depth(src_desc) &&
1543 util_format_has_depth(dst_desc);
1544 has_stencil = util_format_has_stencil(src_desc) &&
1545 util_format_has_stencil(dst_desc);
1546
1547 blit_color = has_color && (mask & PIPE_MASK_RGBA);
1548 blit_depth = has_depth && (mask & PIPE_MASK_Z);
1549 blit_stencil = has_stencil && (mask & PIPE_MASK_S) &&
1550 ctx->has_stencil_export;
1551
1552 if (!blit_stencil && !blit_depth && !blit_color) {
1553 return;
1554 }
1555
1556 if (blit_stencil ||
1557 (dstbox->width == abs(srcbox->width) &&
1558 dstbox->height == abs(srcbox->height))) {
1559 filter = PIPE_TEX_FILTER_NEAREST;
1560 }
1561
1562 /* Check whether the states are properly saved. */
1563 blitter_set_running_flag(ctx);
1564 blitter_check_saved_vertex_states(ctx);
1565 blitter_check_saved_fragment_states(ctx);
1566 blitter_check_saved_textures(ctx);
1567 blitter_check_saved_fb_state(ctx);
1568 blitter_disable_render_cond(ctx);
1569
1570 /* Initialize framebuffer state. */
1571 fb_state.width = dst->width;
1572 fb_state.height = dst->height;
1573 fb_state.nr_cbufs = blit_depth || blit_stencil ? 0 : 1;
1574 fb_state.cbufs[0] = NULL;
1575 fb_state.zsbuf = NULL;
1576
1577 if (blit_depth || blit_stencil) {
1578 pipe->bind_blend_state(pipe, ctx->blend[0][0]);
1579
1580 if (blit_depth && blit_stencil) {
1581 pipe->bind_depth_stencil_alpha_state(pipe,
1582 ctx->dsa_write_depth_stencil);
1583 ctx->bind_fs_state(pipe,
1584 blitter_get_fs_texfetch_depthstencil(ctx, src_target,
1585 src_samples));
1586 } else if (blit_depth) {
1587 pipe->bind_depth_stencil_alpha_state(pipe,
1588 ctx->dsa_write_depth_keep_stencil);
1589 ctx->bind_fs_state(pipe,
1590 blitter_get_fs_texfetch_depth(ctx, src_target,
1591 src_samples));
1592 } else { /* is_stencil */
1593 pipe->bind_depth_stencil_alpha_state(pipe,
1594 ctx->dsa_keep_depth_write_stencil);
1595 ctx->bind_fs_state(pipe,
1596 blitter_get_fs_texfetch_stencil(ctx, src_target,
1597 src_samples));
1598 }
1599
1600 } else {
1601 unsigned colormask = mask & PIPE_MASK_RGBA;
1602
1603 pipe->bind_blend_state(pipe, ctx->blend[colormask][alpha_blend]);
1604 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1605 ctx->bind_fs_state(pipe,
1606 blitter_get_fs_texfetch_col(ctx, src->format, src_target,
1607 src_samples, dst_samples, filter));
1608 }
1609
1610 /* Set the linear filter only for scaled color non-MSAA blits. */
1611 if (filter == PIPE_TEX_FILTER_LINEAR) {
1612 if (src_target == PIPE_TEXTURE_RECT) {
1613 sampler_state = ctx->sampler_state_rect_linear;
1614 } else {
1615 sampler_state = ctx->sampler_state_linear;
1616 }
1617 } else {
1618 if (src_target == PIPE_TEXTURE_RECT) {
1619 sampler_state = ctx->sampler_state_rect;
1620 } else {
1621 sampler_state = ctx->sampler_state;
1622 }
1623 }
1624
1625 /* Set samplers. */
1626 if (blit_depth && blit_stencil) {
1627 /* Setup two samplers, one for depth and the other one for stencil. */
1628 struct pipe_sampler_view templ;
1629 struct pipe_sampler_view *views[2];
1630 void *samplers[2] = {sampler_state, sampler_state};
1631
1632 templ = *src;
1633 templ.format = util_format_stencil_only(templ.format);
1634 assert(templ.format != PIPE_FORMAT_NONE);
1635
1636 views[0] = src;
1637 views[1] = pipe->create_sampler_view(pipe, src->texture, &templ);
1638
1639 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 2, views);
1640 pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0, 2, samplers);
1641
1642 pipe_sampler_view_reference(&views[1], NULL);
1643 } else if (blit_stencil) {
1644 /* Set a stencil-only sampler view for it not to sample depth instead. */
1645 struct pipe_sampler_view templ;
1646 struct pipe_sampler_view *view;
1647
1648 templ = *src;
1649 templ.format = util_format_stencil_only(templ.format);
1650 assert(templ.format != PIPE_FORMAT_NONE);
1651
1652 view = pipe->create_sampler_view(pipe, src->texture, &templ);
1653
1654 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &view);
1655 pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
1656 0, 1, &sampler_state);
1657
1658 pipe_sampler_view_reference(&view, NULL);
1659 } else {
1660 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &src);
1661 pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
1662 0, 1, &sampler_state);
1663 }
1664
1665 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1666 if (scissor) {
1667 pipe->set_scissor_states(pipe, 0, 1, scissor);
1668 }
1669
1670 blitter_set_common_draw_rect_state(ctx, scissor != NULL, FALSE);
1671 blitter_set_dst_dimensions(ctx, dst->width, dst->height);
1672
1673 if ((src_target == PIPE_TEXTURE_1D ||
1674 src_target == PIPE_TEXTURE_2D ||
1675 src_target == PIPE_TEXTURE_RECT) &&
1676 src_samples <= 1) {
1677 /* Draw the quad with the draw_rectangle callback. */
1678
1679 /* Set texture coordinates. - use a pipe color union
1680 * for interface purposes.
1681 * XXX pipe_color_union is a wrong name since we use that to set
1682 * texture coordinates too.
1683 */
1684 union pipe_color_union coord;
1685 get_texcoords(src, src_width0, src_height0, srcbox->x, srcbox->y,
1686 srcbox->x+srcbox->width, srcbox->y+srcbox->height, coord.f);
1687
1688 /* Set framebuffer state. */
1689 if (blit_depth || blit_stencil) {
1690 fb_state.zsbuf = dst;
1691 } else {
1692 fb_state.cbufs[0] = dst;
1693 }
1694 pipe->set_framebuffer_state(pipe, &fb_state);
1695
1696 /* Draw. */
1697 pipe->set_sample_mask(pipe, ~0);
1698 blitter->draw_rectangle(blitter, dstbox->x, dstbox->y,
1699 dstbox->x + dstbox->width,
1700 dstbox->y + dstbox->height, 0,
1701 UTIL_BLITTER_ATTRIB_TEXCOORD, &coord);
1702 } else {
1703 /* Draw the quad with the generic codepath. */
1704 int dst_z;
1705 for (dst_z = 0; dst_z < dstbox->depth; dst_z++) {
1706 struct pipe_surface *old;
1707 float dst2src_scale = srcbox->depth / (float)dstbox->depth;
1708
1709 /* Scale Z properly if the blit is scaled.
1710 *
1711 * When downscaling, we want the coordinates centered, so that
1712 * mipmapping works for 3D textures. For example, when generating
1713 * a 4x4x4 level, this wouldn't average the pixels:
1714 *
1715 * src Z: 0 1 2 3 4 5 6 7
1716 * dst Z: 0 1 2 3
1717 *
1718 * Because the pixels are not centered below the pixels of the higher
1719 * level. Therefore, we want this:
1720 * src Z: 0 1 2 3 4 5 6 7
1721 * dst Z: 0 1 2 3
1722 *
1723 * dst_offset defines the offset needed for centering the pixels and
1724 * it works with any scaling (not just 2x).
1725 */
1726 float dst_offset = ((srcbox->depth - 1) -
1727 (dstbox->depth - 1) * dst2src_scale) * 0.5;
1728 float src_z = (dst_z + dst_offset) * dst2src_scale;
1729
1730 /* Set framebuffer state. */
1731 if (blit_depth || blit_stencil) {
1732 fb_state.zsbuf = dst;
1733 } else {
1734 fb_state.cbufs[0] = dst;
1735 }
1736 pipe->set_framebuffer_state(pipe, &fb_state);
1737
1738 /* See if we need to blit a multisample or singlesample buffer. */
1739 if (src_samples == dst_samples && dst_samples > 1) {
1740 /* MSAA copy. */
1741 unsigned i, max_sample = dst_samples - 1;
1742
1743 for (i = 0; i <= max_sample; i++) {
1744 pipe->set_sample_mask(pipe, 1 << i);
1745 blitter_set_texcoords(ctx, src, src_width0, src_height0,
1746 srcbox->z + src_z,
1747 i, srcbox->x, srcbox->y,
1748 srcbox->x + srcbox->width,
1749 srcbox->y + srcbox->height);
1750 blitter_draw(ctx, dstbox->x, dstbox->y,
1751 dstbox->x + dstbox->width,
1752 dstbox->y + dstbox->height, 0, 1);
1753 }
1754 } else {
1755 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1756 pipe->set_sample_mask(pipe, ~0);
1757 blitter_set_texcoords(ctx, src, src_width0, src_height0,
1758 srcbox->z + src_z, 0,
1759 srcbox->x, srcbox->y,
1760 srcbox->x + srcbox->width,
1761 srcbox->y + srcbox->height);
1762 blitter_draw(ctx, dstbox->x, dstbox->y,
1763 dstbox->x + dstbox->width,
1764 dstbox->y + dstbox->height, 0, 1);
1765 }
1766
1767 /* Get the next surface or (if this is the last iteration)
1768 * just unreference the last one. */
1769 old = dst;
1770 if (dst_z < dstbox->depth-1) {
1771 dst = ctx->base.get_next_surface_layer(ctx->base.pipe, dst);
1772 }
1773 if (dst_z) {
1774 pipe_surface_reference(&old, NULL);
1775 }
1776 }
1777 }
1778
1779 blitter_restore_vertex_states(ctx);
1780 blitter_restore_fragment_states(ctx);
1781 blitter_restore_textures(ctx);
1782 blitter_restore_fb_state(ctx);
1783 if (scissor) {
1784 pipe->set_scissor_states(pipe, 0, 1, &ctx->base.saved_scissor);
1785 }
1786 blitter_restore_render_cond(ctx);
1787 blitter_unset_running_flag(ctx);
1788 }
1789
1790 void
1791 util_blitter_blit(struct blitter_context *blitter,
1792 const struct pipe_blit_info *info)
1793 {
1794 struct pipe_resource *dst = info->dst.resource;
1795 struct pipe_resource *src = info->src.resource;
1796 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1797 struct pipe_context *pipe = ctx->base.pipe;
1798 struct pipe_surface *dst_view, dst_templ;
1799 struct pipe_sampler_view src_templ, *src_view;
1800
1801 /* Initialize the surface. */
1802 util_blitter_default_dst_texture(&dst_templ, dst, info->dst.level,
1803 info->dst.box.z);
1804 dst_templ.format = info->dst.format;
1805 dst_view = pipe->create_surface(pipe, dst, &dst_templ);
1806
1807 /* Initialize the sampler view. */
1808 util_blitter_default_src_texture(&src_templ, src, info->src.level);
1809 src_templ.format = info->src.format;
1810 src_view = pipe->create_sampler_view(pipe, src, &src_templ);
1811
1812 /* Copy. */
1813 util_blitter_blit_generic(blitter, dst_view, &info->dst.box,
1814 src_view, &info->src.box, src->width0, src->height0,
1815 info->mask, info->filter,
1816 info->scissor_enable ? &info->scissor : NULL,
1817 info->alpha_blend);
1818
1819 pipe_surface_reference(&dst_view, NULL);
1820 pipe_sampler_view_reference(&src_view, NULL);
1821 }
1822
1823 /* Clear a region of a color surface to a constant value. */
1824 void util_blitter_clear_render_target(struct blitter_context *blitter,
1825 struct pipe_surface *dstsurf,
1826 const union pipe_color_union *color,
1827 unsigned dstx, unsigned dsty,
1828 unsigned width, unsigned height)
1829 {
1830 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1831 struct pipe_context *pipe = ctx->base.pipe;
1832 struct pipe_framebuffer_state fb_state;
1833 unsigned num_layers;
1834
1835 assert(dstsurf->texture);
1836 if (!dstsurf->texture)
1837 return;
1838
1839 /* check the saved state */
1840 blitter_set_running_flag(ctx);
1841 blitter_check_saved_vertex_states(ctx);
1842 blitter_check_saved_fragment_states(ctx);
1843 blitter_check_saved_fb_state(ctx);
1844 blitter_disable_render_cond(ctx);
1845
1846 /* bind states */
1847 pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA][0]);
1848 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1849 bind_fs_write_one_cbuf(ctx);
1850 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1851
1852 /* set a framebuffer state */
1853 fb_state.width = dstsurf->width;
1854 fb_state.height = dstsurf->height;
1855 fb_state.nr_cbufs = 1;
1856 fb_state.cbufs[0] = dstsurf;
1857 fb_state.zsbuf = 0;
1858 pipe->set_framebuffer_state(pipe, &fb_state);
1859 pipe->set_sample_mask(pipe, ~0);
1860
1861 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
1862
1863 num_layers = dstsurf->u.tex.last_layer - dstsurf->u.tex.first_layer + 1;
1864 if (num_layers > 1 && ctx->has_layered) {
1865 blitter_set_common_draw_rect_state(ctx, FALSE, TRUE);
1866 blitter_set_clear_color(ctx, color);
1867 blitter_draw(ctx, dstx, dsty, dstx+width, dsty+height, 0, num_layers);
1868 }
1869 else {
1870 blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
1871 blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, 0,
1872 UTIL_BLITTER_ATTRIB_COLOR, color);
1873 }
1874
1875 blitter_restore_vertex_states(ctx);
1876 blitter_restore_fragment_states(ctx);
1877 blitter_restore_fb_state(ctx);
1878 blitter_restore_render_cond(ctx);
1879 blitter_unset_running_flag(ctx);
1880 }
1881
1882 /* Clear a region of a depth stencil surface. */
1883 void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
1884 struct pipe_surface *dstsurf,
1885 unsigned clear_flags,
1886 double depth,
1887 unsigned stencil,
1888 unsigned dstx, unsigned dsty,
1889 unsigned width, unsigned height)
1890 {
1891 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1892 struct pipe_context *pipe = ctx->base.pipe;
1893 struct pipe_framebuffer_state fb_state;
1894 struct pipe_stencil_ref sr = { { 0 } };
1895 unsigned num_layers;
1896
1897 assert(dstsurf->texture);
1898 if (!dstsurf->texture)
1899 return;
1900
1901 /* check the saved state */
1902 blitter_set_running_flag(ctx);
1903 blitter_check_saved_vertex_states(ctx);
1904 blitter_check_saved_fragment_states(ctx);
1905 blitter_check_saved_fb_state(ctx);
1906 blitter_disable_render_cond(ctx);
1907
1908 /* bind states */
1909 pipe->bind_blend_state(pipe, ctx->blend[0][0]);
1910 if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
1911 sr.ref_value[0] = stencil & 0xff;
1912 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
1913 pipe->set_stencil_ref(pipe, &sr);
1914 }
1915 else if (clear_flags & PIPE_CLEAR_DEPTH) {
1916 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
1917 }
1918 else if (clear_flags & PIPE_CLEAR_STENCIL) {
1919 sr.ref_value[0] = stencil & 0xff;
1920 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
1921 pipe->set_stencil_ref(pipe, &sr);
1922 }
1923 else
1924 /* hmm that should be illegal probably, or make it a no-op somewhere */
1925 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1926
1927 bind_fs_empty(ctx);
1928 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1929
1930 /* set a framebuffer state */
1931 fb_state.width = dstsurf->width;
1932 fb_state.height = dstsurf->height;
1933 fb_state.nr_cbufs = 0;
1934 fb_state.cbufs[0] = 0;
1935 fb_state.zsbuf = dstsurf;
1936 pipe->set_framebuffer_state(pipe, &fb_state);
1937 pipe->set_sample_mask(pipe, ~0);
1938
1939 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
1940
1941 num_layers = dstsurf->u.tex.last_layer - dstsurf->u.tex.first_layer + 1;
1942 if (num_layers > 1 && ctx->has_layered) {
1943 blitter_set_common_draw_rect_state(ctx, FALSE, TRUE);
1944 blitter_draw(ctx, dstx, dsty, dstx+width, dsty+height, (float) depth, num_layers);
1945 }
1946 else {
1947 blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
1948 blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height,
1949 (float) depth,
1950 UTIL_BLITTER_ATTRIB_NONE, NULL);
1951 }
1952
1953 blitter_restore_vertex_states(ctx);
1954 blitter_restore_fragment_states(ctx);
1955 blitter_restore_fb_state(ctx);
1956 blitter_restore_render_cond(ctx);
1957 blitter_unset_running_flag(ctx);
1958 }
1959
1960 /* draw a rectangle across a region using a custom dsa stage - for r600g */
1961 void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
1962 struct pipe_surface *zsurf,
1963 struct pipe_surface *cbsurf,
1964 unsigned sample_mask,
1965 void *dsa_stage, float depth)
1966 {
1967 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1968 struct pipe_context *pipe = ctx->base.pipe;
1969 struct pipe_framebuffer_state fb_state;
1970
1971 assert(zsurf->texture);
1972 if (!zsurf->texture)
1973 return;
1974
1975 /* check the saved state */
1976 blitter_set_running_flag(ctx);
1977 blitter_check_saved_vertex_states(ctx);
1978 blitter_check_saved_fragment_states(ctx);
1979 blitter_check_saved_fb_state(ctx);
1980 blitter_disable_render_cond(ctx);
1981
1982 /* bind states */
1983 pipe->bind_blend_state(pipe, cbsurf ? ctx->blend[PIPE_MASK_RGBA][0] :
1984 ctx->blend[0][0]);
1985 pipe->bind_depth_stencil_alpha_state(pipe, dsa_stage);
1986 if (cbsurf)
1987 bind_fs_write_one_cbuf(ctx);
1988 else
1989 bind_fs_empty(ctx);
1990 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1991
1992 /* set a framebuffer state */
1993 fb_state.width = zsurf->width;
1994 fb_state.height = zsurf->height;
1995 fb_state.nr_cbufs = 1;
1996 if (cbsurf) {
1997 fb_state.cbufs[0] = cbsurf;
1998 fb_state.nr_cbufs = 1;
1999 } else {
2000 fb_state.cbufs[0] = NULL;
2001 fb_state.nr_cbufs = 0;
2002 }
2003 fb_state.zsbuf = zsurf;
2004 pipe->set_framebuffer_state(pipe, &fb_state);
2005 pipe->set_sample_mask(pipe, sample_mask);
2006
2007 blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
2008 blitter_set_dst_dimensions(ctx, zsurf->width, zsurf->height);
2009 blitter->draw_rectangle(blitter, 0, 0, zsurf->width, zsurf->height, depth,
2010 UTIL_BLITTER_ATTRIB_NONE, NULL);
2011
2012 blitter_restore_vertex_states(ctx);
2013 blitter_restore_fragment_states(ctx);
2014 blitter_restore_fb_state(ctx);
2015 blitter_restore_render_cond(ctx);
2016 blitter_unset_running_flag(ctx);
2017 }
2018
2019 void util_blitter_copy_buffer(struct blitter_context *blitter,
2020 struct pipe_resource *dst,
2021 unsigned dstx,
2022 struct pipe_resource *src,
2023 unsigned srcx,
2024 unsigned size)
2025 {
2026 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2027 struct pipe_context *pipe = ctx->base.pipe;
2028 struct pipe_vertex_buffer vb;
2029 struct pipe_stream_output_target *so_target;
2030 unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0};
2031
2032 if (srcx >= src->width0 ||
2033 dstx >= dst->width0) {
2034 return;
2035 }
2036 if (srcx + size > src->width0) {
2037 size = src->width0 - srcx;
2038 }
2039 if (dstx + size > dst->width0) {
2040 size = dst->width0 - dstx;
2041 }
2042
2043 /* Drivers not capable of Stream Out should not call this function
2044 * in the first place. */
2045 assert(ctx->has_stream_out);
2046
2047 /* Some alignment is required. */
2048 if (srcx % 4 != 0 || dstx % 4 != 0 || size % 4 != 0 ||
2049 !ctx->has_stream_out) {
2050 struct pipe_box box;
2051 u_box_1d(srcx, size, &box);
2052 util_resource_copy_region(pipe, dst, 0, dstx, 0, 0, src, 0, &box);
2053 return;
2054 }
2055
2056 blitter_set_running_flag(ctx);
2057 blitter_check_saved_vertex_states(ctx);
2058 blitter_disable_render_cond(ctx);
2059
2060 vb.buffer = src;
2061 vb.buffer_offset = srcx;
2062 vb.stride = 4;
2063
2064 pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
2065 pipe->bind_vertex_elements_state(pipe, ctx->velem_state_readbuf[0]);
2066 bind_vs_pos_only(ctx, 1);
2067 if (ctx->has_geometry_shader)
2068 pipe->bind_gs_state(pipe, NULL);
2069 if (ctx->has_tessellation) {
2070 pipe->bind_tcs_state(pipe, NULL);
2071 pipe->bind_tes_state(pipe, NULL);
2072 }
2073 pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state);
2074
2075 so_target = pipe->create_stream_output_target(pipe, dst, dstx, size);
2076 pipe->set_stream_output_targets(pipe, 1, &so_target, offsets);
2077
2078 util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
2079
2080 blitter_restore_vertex_states(ctx);
2081 blitter_restore_render_cond(ctx);
2082 blitter_unset_running_flag(ctx);
2083 pipe_so_target_reference(&so_target, NULL);
2084 }
2085
2086 void util_blitter_clear_buffer(struct blitter_context *blitter,
2087 struct pipe_resource *dst,
2088 unsigned offset, unsigned size,
2089 unsigned num_channels,
2090 const union pipe_color_union *clear_value)
2091 {
2092 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2093 struct pipe_context *pipe = ctx->base.pipe;
2094 struct pipe_vertex_buffer vb = {0};
2095 struct pipe_stream_output_target *so_target = NULL;
2096 unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0};
2097
2098 assert(num_channels >= 1);
2099 assert(num_channels <= 4);
2100
2101 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
2102 *
2103 * R600 uses this to initialize texture resources, so width0 might not be
2104 * what you think it is.
2105 */
2106
2107 /* Streamout is required. */
2108 if (!ctx->has_stream_out) {
2109 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2110 return;
2111 }
2112
2113 /* Some alignment is required. */
2114 if (offset % 4 != 0 || size % 4 != 0) {
2115 assert(!"Bad alignment in util_blitter_clear_buffer()");
2116 return;
2117 }
2118
2119 u_upload_data(ctx->upload, 0, num_channels*4, 4, clear_value,
2120 &vb.buffer_offset, &vb.buffer);
2121 if (!vb.buffer)
2122 goto out;
2123
2124 vb.stride = 0;
2125
2126 blitter_set_running_flag(ctx);
2127 blitter_check_saved_vertex_states(ctx);
2128 blitter_disable_render_cond(ctx);
2129
2130 pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
2131 pipe->bind_vertex_elements_state(pipe,
2132 ctx->velem_state_readbuf[num_channels-1]);
2133 bind_vs_pos_only(ctx, num_channels);
2134 if (ctx->has_geometry_shader)
2135 pipe->bind_gs_state(pipe, NULL);
2136 if (ctx->has_tessellation) {
2137 pipe->bind_tcs_state(pipe, NULL);
2138 pipe->bind_tes_state(pipe, NULL);
2139 }
2140 pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state);
2141
2142 so_target = pipe->create_stream_output_target(pipe, dst, offset, size);
2143 pipe->set_stream_output_targets(pipe, 1, &so_target, offsets);
2144
2145 util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
2146
2147 out:
2148 blitter_restore_vertex_states(ctx);
2149 blitter_restore_render_cond(ctx);
2150 blitter_unset_running_flag(ctx);
2151 pipe_so_target_reference(&so_target, NULL);
2152 pipe_resource_reference(&vb.buffer, NULL);
2153 }
2154
2155 /* probably radeon specific */
2156 void util_blitter_custom_resolve_color(struct blitter_context *blitter,
2157 struct pipe_resource *dst,
2158 unsigned dst_level,
2159 unsigned dst_layer,
2160 struct pipe_resource *src,
2161 unsigned src_layer,
2162 unsigned sample_mask,
2163 void *custom_blend,
2164 enum pipe_format format)
2165 {
2166 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2167 struct pipe_context *pipe = ctx->base.pipe;
2168 struct pipe_framebuffer_state fb_state;
2169 struct pipe_surface *srcsurf, *dstsurf, surf_tmpl;
2170
2171 blitter_set_running_flag(ctx);
2172 blitter_check_saved_vertex_states(ctx);
2173 blitter_check_saved_fragment_states(ctx);
2174 blitter_disable_render_cond(ctx);
2175
2176 /* bind states */
2177 pipe->bind_blend_state(pipe, custom_blend);
2178 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2179 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
2180 bind_fs_write_one_cbuf(ctx);
2181 pipe->set_sample_mask(pipe, sample_mask);
2182
2183 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
2184 surf_tmpl.format = format;
2185 surf_tmpl.u.tex.level = dst_level;
2186 surf_tmpl.u.tex.first_layer = dst_layer;
2187 surf_tmpl.u.tex.last_layer = dst_layer;
2188
2189 dstsurf = pipe->create_surface(pipe, dst, &surf_tmpl);
2190
2191 surf_tmpl.u.tex.level = 0;
2192 surf_tmpl.u.tex.first_layer = src_layer;
2193 surf_tmpl.u.tex.last_layer = src_layer;
2194
2195 srcsurf = pipe->create_surface(pipe, src, &surf_tmpl);
2196
2197 /* set a framebuffer state */
2198 fb_state.width = src->width0;
2199 fb_state.height = src->height0;
2200 fb_state.nr_cbufs = 2;
2201 fb_state.cbufs[0] = srcsurf;
2202 fb_state.cbufs[1] = dstsurf;
2203 fb_state.zsbuf = NULL;
2204 pipe->set_framebuffer_state(pipe, &fb_state);
2205
2206 blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
2207 blitter_set_dst_dimensions(ctx, src->width0, src->height0);
2208 blitter->draw_rectangle(blitter, 0, 0, src->width0, src->height0,
2209 0, 0, NULL);
2210 blitter_restore_fb_state(ctx);
2211 blitter_restore_vertex_states(ctx);
2212 blitter_restore_fragment_states(ctx);
2213 blitter_restore_render_cond(ctx);
2214 blitter_unset_running_flag(ctx);
2215
2216 pipe_surface_reference(&srcsurf, NULL);
2217 pipe_surface_reference(&dstsurf, NULL);
2218 }
2219
2220 void util_blitter_custom_color(struct blitter_context *blitter,
2221 struct pipe_surface *dstsurf,
2222 void *custom_blend)
2223 {
2224 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2225 struct pipe_context *pipe = ctx->base.pipe;
2226 struct pipe_framebuffer_state fb_state;
2227
2228 assert(dstsurf->texture);
2229 if (!dstsurf->texture)
2230 return;
2231
2232 /* check the saved state */
2233 blitter_set_running_flag(ctx);
2234 blitter_check_saved_vertex_states(ctx);
2235 blitter_check_saved_fragment_states(ctx);
2236 blitter_check_saved_fb_state(ctx);
2237 blitter_disable_render_cond(ctx);
2238
2239 /* bind states */
2240 pipe->bind_blend_state(pipe, custom_blend ? custom_blend
2241 : ctx->blend[PIPE_MASK_RGBA][0]);
2242 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2243 bind_fs_write_one_cbuf(ctx);
2244 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
2245 pipe->set_sample_mask(pipe, (1ull << MAX2(1, dstsurf->texture->nr_samples)) - 1);
2246
2247 /* set a framebuffer state */
2248 fb_state.width = dstsurf->width;
2249 fb_state.height = dstsurf->height;
2250 fb_state.nr_cbufs = 1;
2251 fb_state.cbufs[0] = dstsurf;
2252 fb_state.zsbuf = 0;
2253 pipe->set_framebuffer_state(pipe, &fb_state);
2254 pipe->set_sample_mask(pipe, ~0);
2255
2256 blitter_set_common_draw_rect_state(ctx, FALSE, FALSE);
2257 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
2258 blitter->draw_rectangle(blitter, 0, 0, dstsurf->width, dstsurf->height,
2259 0, 0, NULL);
2260
2261 blitter_restore_vertex_states(ctx);
2262 blitter_restore_fragment_states(ctx);
2263 blitter_restore_fb_state(ctx);
2264 blitter_restore_render_cond(ctx);
2265 blitter_unset_running_flag(ctx);
2266 }