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