1c141e67f27896a3ee8ffd998c039fbe78a2cccb
[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 float vertices[4][2][4]; /**< {pos, color} or {pos, texcoord} */
65
66 /* Templates for various state objects. */
67
68 /* Constant state objects. */
69 /* Vertex shaders. */
70 void *vs; /**< Vertex shader which passes {pos, generic} to the output.*/
71 void *vs_nogeneric;
72 void *vs_pos_only[4]; /**< Vertex shader which passes pos to the output
73 for clear_buffer/copy_buffer.*/
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 where
82 * the 1st index indicates the texture type / destination type,
83 * the 2nd index is the PIPE_TEXTURE_* to be sampled,
84 * the 3rd index is 0 = use TEX, 1 = use TXF.
85 */
86 void *fs_texfetch_col[5][PIPE_MAX_TEXTURE_TYPES][2];
87
88 /* FS which outputs a depth from a texture, where
89 * the 1st index is the PIPE_TEXTURE_* to be sampled,
90 * the 2nd index is 0 = use TEX, 1 = use TXF.
91 */
92 void *fs_texfetch_depth[PIPE_MAX_TEXTURE_TYPES][2];
93 void *fs_texfetch_depthstencil[PIPE_MAX_TEXTURE_TYPES][2];
94 void *fs_texfetch_stencil[PIPE_MAX_TEXTURE_TYPES][2];
95
96 /* FS which outputs one sample from a multisample texture. */
97 void *fs_texfetch_col_msaa[5][PIPE_MAX_TEXTURE_TYPES];
98 void *fs_texfetch_depth_msaa[PIPE_MAX_TEXTURE_TYPES];
99 void *fs_texfetch_depthstencil_msaa[PIPE_MAX_TEXTURE_TYPES];
100 void *fs_texfetch_stencil_msaa[PIPE_MAX_TEXTURE_TYPES];
101
102 /* FS which outputs an average of all samples. */
103 void *fs_resolve[PIPE_MAX_TEXTURE_TYPES][NUM_RESOLVE_FRAG_SHADERS][2];
104
105 /* Blend state. */
106 void *blend[PIPE_MASK_RGBA+1][2]; /**< blend state with writemask */
107 void *blend_clear[GET_CLEAR_BLEND_STATE_IDX(PIPE_CLEAR_COLOR)+1];
108
109 /* Depth stencil alpha state. */
110 void *dsa_write_depth_stencil;
111 void *dsa_write_depth_keep_stencil;
112 void *dsa_keep_depth_stencil;
113 void *dsa_keep_depth_write_stencil;
114
115 /* Vertex elements states. */
116 void *velem_state;
117 void *velem_state_readbuf[4]; /**< X, XY, XYZ, XYZW */
118
119 /* Sampler state. */
120 void *sampler_state;
121 void *sampler_state_linear;
122 void *sampler_state_rect;
123 void *sampler_state_rect_linear;
124
125 /* Rasterizer state. */
126 void *rs_state[2][2]; /**< [scissor][msaa] */
127 void *rs_discard_state;
128
129 /* Destination surface dimensions. */
130 unsigned dst_width;
131 unsigned dst_height;
132
133 void *custom_vs;
134
135 bool has_geometry_shader;
136 bool has_tessellation;
137 bool has_layered;
138 bool has_stream_out;
139 bool has_stencil_export;
140 bool has_texture_multisample;
141 bool has_tex_lz;
142 bool has_txf;
143 bool cube_as_2darray;
144 bool cached_all_shaders;
145
146 /* The Draw module overrides these functions.
147 * Always create the blitter before Draw. */
148 void (*bind_fs_state)(struct pipe_context *, void *);
149 void (*delete_fs_state)(struct pipe_context *, void *);
150 };
151
152 struct blitter_context *util_blitter_create(struct pipe_context *pipe)
153 {
154 struct blitter_context_priv *ctx;
155 struct pipe_blend_state blend;
156 struct pipe_depth_stencil_alpha_state dsa;
157 struct pipe_rasterizer_state rs_state;
158 struct pipe_sampler_state sampler_state;
159 struct pipe_vertex_element velem[2];
160 unsigned i, j;
161
162 ctx = CALLOC_STRUCT(blitter_context_priv);
163 if (!ctx)
164 return NULL;
165
166 ctx->base.pipe = pipe;
167 ctx->base.draw_rectangle = util_blitter_draw_rectangle;
168
169 ctx->bind_fs_state = pipe->bind_fs_state;
170 ctx->delete_fs_state = pipe->delete_fs_state;
171
172 /* init state objects for them to be considered invalid */
173 ctx->base.saved_blend_state = INVALID_PTR;
174 ctx->base.saved_dsa_state = INVALID_PTR;
175 ctx->base.saved_rs_state = INVALID_PTR;
176 ctx->base.saved_fs = INVALID_PTR;
177 ctx->base.saved_vs = INVALID_PTR;
178 ctx->base.saved_gs = INVALID_PTR;
179 ctx->base.saved_velem_state = INVALID_PTR;
180 ctx->base.saved_fb_state.nr_cbufs = ~0;
181 ctx->base.saved_num_sampler_views = ~0;
182 ctx->base.saved_num_sampler_states = ~0;
183 ctx->base.saved_num_so_targets = ~0;
184
185 ctx->has_geometry_shader =
186 pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_GEOMETRY,
187 PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0;
188
189 ctx->has_tessellation =
190 pipe->screen->get_shader_param(pipe->screen, PIPE_SHADER_TESS_CTRL,
191 PIPE_SHADER_CAP_MAX_INSTRUCTIONS) > 0;
192
193 ctx->has_stream_out =
194 pipe->screen->get_param(pipe->screen,
195 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS) != 0;
196
197 ctx->has_stencil_export =
198 pipe->screen->get_param(pipe->screen,
199 PIPE_CAP_SHADER_STENCIL_EXPORT);
200
201 ctx->has_texture_multisample =
202 pipe->screen->get_param(pipe->screen, PIPE_CAP_TEXTURE_MULTISAMPLE);
203
204 ctx->has_tex_lz = pipe->screen->get_param(pipe->screen,
205 PIPE_CAP_TGSI_TEX_TXF_LZ);
206 ctx->has_txf = pipe->screen->get_param(pipe->screen,
207 PIPE_CAP_GLSL_FEATURE_LEVEL) > 130;
208 ctx->cube_as_2darray = pipe->screen->get_param(pipe->screen,
209 PIPE_CAP_SAMPLER_VIEW_TARGET);
210
211 /* blend state objects */
212 memset(&blend, 0, sizeof(blend));
213
214 for (i = 0; i <= PIPE_MASK_RGBA; i++) {
215 for (j = 0; j < 2; j++) {
216 memset(&blend.rt[0], 0, sizeof(blend.rt[0]));
217 blend.rt[0].colormask = i;
218 if (j) {
219 blend.rt[0].blend_enable = 1;
220 blend.rt[0].rgb_func = PIPE_BLEND_ADD;
221 blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA;
222 blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA;
223 blend.rt[0].alpha_func = PIPE_BLEND_ADD;
224 blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_SRC_ALPHA;
225 blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_INV_SRC_ALPHA;
226 }
227 ctx->blend[i][j] = pipe->create_blend_state(pipe, &blend);
228 }
229 }
230
231 /* depth stencil alpha state objects */
232 memset(&dsa, 0, sizeof(dsa));
233 ctx->dsa_keep_depth_stencil =
234 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
235
236 dsa.depth.enabled = 1;
237 dsa.depth.writemask = 1;
238 dsa.depth.func = PIPE_FUNC_ALWAYS;
239 ctx->dsa_write_depth_keep_stencil =
240 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
241
242 dsa.stencil[0].enabled = 1;
243 dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
244 dsa.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
245 dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
246 dsa.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
247 dsa.stencil[0].valuemask = 0xff;
248 dsa.stencil[0].writemask = 0xff;
249 ctx->dsa_write_depth_stencil =
250 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
251
252 dsa.depth.enabled = 0;
253 dsa.depth.writemask = 0;
254 ctx->dsa_keep_depth_write_stencil =
255 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
256
257 /* sampler state */
258 memset(&sampler_state, 0, sizeof(sampler_state));
259 sampler_state.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
260 sampler_state.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
261 sampler_state.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
262 sampler_state.normalized_coords = 1;
263 ctx->sampler_state = pipe->create_sampler_state(pipe, &sampler_state);
264 sampler_state.normalized_coords = 0;
265 ctx->sampler_state_rect = pipe->create_sampler_state(pipe, &sampler_state);
266
267 sampler_state.min_img_filter = PIPE_TEX_FILTER_LINEAR;
268 sampler_state.mag_img_filter = PIPE_TEX_FILTER_LINEAR;
269 sampler_state.normalized_coords = 1;
270 ctx->sampler_state_linear = pipe->create_sampler_state(pipe, &sampler_state);
271 sampler_state.normalized_coords = 0;
272 ctx->sampler_state_rect_linear = pipe->create_sampler_state(pipe, &sampler_state);
273
274 /* rasterizer state */
275 memset(&rs_state, 0, sizeof(rs_state));
276 rs_state.cull_face = PIPE_FACE_NONE;
277 rs_state.half_pixel_center = 1;
278 rs_state.bottom_edge_rule = 1;
279 rs_state.flatshade = 1;
280 rs_state.depth_clip_near = 1;
281 rs_state.depth_clip_far = 1;
282
283 unsigned scissor, msaa;
284 for (scissor = 0; scissor < 2; scissor++) {
285 for (msaa = 0; msaa < 2; msaa++) {
286 rs_state.scissor = scissor;
287 rs_state.multisample = msaa;
288 ctx->rs_state[scissor][msaa] =
289 pipe->create_rasterizer_state(pipe, &rs_state);
290 }
291 }
292
293 if (ctx->has_stream_out) {
294 rs_state.scissor = rs_state.multisample = 0;
295 rs_state.rasterizer_discard = 1;
296 ctx->rs_discard_state = pipe->create_rasterizer_state(pipe, &rs_state);
297 }
298
299 ctx->base.cb_slot = 0; /* 0 for now */
300 ctx->base.vb_slot = 0; /* 0 for now */
301
302 /* vertex elements states */
303 memset(&velem[0], 0, sizeof(velem[0]) * 2);
304 for (i = 0; i < 2; i++) {
305 velem[i].src_offset = i * 4 * sizeof(float);
306 velem[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
307 velem[i].vertex_buffer_index = ctx->base.vb_slot;
308 }
309 ctx->velem_state = pipe->create_vertex_elements_state(pipe, 2, &velem[0]);
310
311 if (ctx->has_stream_out) {
312 static enum pipe_format formats[4] = {
313 PIPE_FORMAT_R32_UINT,
314 PIPE_FORMAT_R32G32_UINT,
315 PIPE_FORMAT_R32G32B32_UINT,
316 PIPE_FORMAT_R32G32B32A32_UINT
317 };
318
319 for (i = 0; i < 4; i++) {
320 velem[0].src_format = formats[i];
321 velem[0].vertex_buffer_index = ctx->base.vb_slot;
322 ctx->velem_state_readbuf[i] =
323 pipe->create_vertex_elements_state(pipe, 1, &velem[0]);
324 }
325 }
326
327 ctx->has_layered =
328 pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_INSTANCEID) &&
329 pipe->screen->get_param(pipe->screen, PIPE_CAP_TGSI_VS_LAYER_VIEWPORT);
330
331 /* set invariant vertex coordinates */
332 for (i = 0; i < 4; i++)
333 ctx->vertices[i][0][3] = 1; /*v.w*/
334
335 return &ctx->base;
336 }
337
338 static void bind_vs_pos_only(struct blitter_context_priv *ctx,
339 unsigned num_so_channels)
340 {
341 struct pipe_context *pipe = ctx->base.pipe;
342 int index = num_so_channels ? num_so_channels - 1 : 0;
343
344 if (!ctx->vs_pos_only[index]) {
345 struct pipe_stream_output_info so;
346 static const enum tgsi_semantic semantic_names[] =
347 { TGSI_SEMANTIC_POSITION };
348 const uint semantic_indices[] = { 0 };
349
350 memset(&so, 0, sizeof(so));
351 so.num_outputs = 1;
352 so.output[0].num_components = num_so_channels;
353 so.stride[0] = num_so_channels;
354
355 ctx->vs_pos_only[index] =
356 util_make_vertex_passthrough_shader_with_so(pipe, 1, semantic_names,
357 semantic_indices, false,
358 false, &so);
359 }
360
361 pipe->bind_vs_state(pipe, ctx->vs_pos_only[index]);
362 }
363
364 static void *get_vs_passthrough_pos_generic(struct blitter_context *blitter)
365 {
366 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
367 struct pipe_context *pipe = ctx->base.pipe;
368
369 if (!ctx->vs) {
370 static const enum tgsi_semantic semantic_names[] =
371 { TGSI_SEMANTIC_POSITION, TGSI_SEMANTIC_GENERIC };
372 const uint semantic_indices[] = { 0, 0 };
373 ctx->vs =
374 util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
375 semantic_indices, false);
376 }
377 return ctx->vs;
378 }
379
380 static void *get_vs_passthrough_pos(struct blitter_context *blitter)
381 {
382 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
383 struct pipe_context *pipe = ctx->base.pipe;
384
385 if (!ctx->vs_nogeneric) {
386 static const enum tgsi_semantic semantic_names[] =
387 { TGSI_SEMANTIC_POSITION };
388 const uint semantic_indices[] = { 0 };
389
390 ctx->vs_nogeneric =
391 util_make_vertex_passthrough_shader(pipe, 1,
392 semantic_names,
393 semantic_indices, false);
394 }
395 return ctx->vs_nogeneric;
396 }
397
398 static void *get_vs_layered(struct blitter_context *blitter)
399 {
400 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
401 struct pipe_context *pipe = ctx->base.pipe;
402
403 if (!ctx->vs_layered) {
404 ctx->vs_layered = util_make_layered_clear_vertex_shader(pipe);
405 }
406 return ctx->vs_layered;
407 }
408
409 static void bind_fs_empty(struct blitter_context_priv *ctx)
410 {
411 struct pipe_context *pipe = ctx->base.pipe;
412
413 if (!ctx->fs_empty) {
414 assert(!ctx->cached_all_shaders);
415 ctx->fs_empty = util_make_empty_fragment_shader(pipe);
416 }
417
418 ctx->bind_fs_state(pipe, ctx->fs_empty);
419 }
420
421 static void bind_fs_write_one_cbuf(struct blitter_context_priv *ctx)
422 {
423 struct pipe_context *pipe = ctx->base.pipe;
424
425 if (!ctx->fs_write_one_cbuf) {
426 assert(!ctx->cached_all_shaders);
427 ctx->fs_write_one_cbuf =
428 util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
429 TGSI_INTERPOLATE_CONSTANT, false);
430 }
431
432 ctx->bind_fs_state(pipe, ctx->fs_write_one_cbuf);
433 }
434
435 static void bind_fs_write_all_cbufs(struct blitter_context_priv *ctx)
436 {
437 struct pipe_context *pipe = ctx->base.pipe;
438
439 if (!ctx->fs_write_all_cbufs) {
440 assert(!ctx->cached_all_shaders);
441 ctx->fs_write_all_cbufs =
442 util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
443 TGSI_INTERPOLATE_CONSTANT, true);
444 }
445
446 ctx->bind_fs_state(pipe, ctx->fs_write_all_cbufs);
447 }
448
449 void util_blitter_destroy(struct blitter_context *blitter)
450 {
451 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
452 struct pipe_context *pipe = blitter->pipe;
453 unsigned i, j, f;
454
455 for (i = 0; i <= PIPE_MASK_RGBA; i++)
456 for (j = 0; j < 2; j++)
457 pipe->delete_blend_state(pipe, ctx->blend[i][j]);
458
459 for (i = 0; i < ARRAY_SIZE(ctx->blend_clear); i++) {
460 if (ctx->blend_clear[i])
461 pipe->delete_blend_state(pipe, ctx->blend_clear[i]);
462 }
463 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
464 pipe->delete_depth_stencil_alpha_state(pipe,
465 ctx->dsa_write_depth_keep_stencil);
466 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
467 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
468
469 unsigned scissor, msaa;
470 for (scissor = 0; scissor < 2; scissor++) {
471 for (msaa = 0; msaa < 2; msaa++) {
472 pipe->delete_rasterizer_state(pipe, ctx->rs_state[scissor][msaa]);
473 }
474 }
475
476 if (ctx->rs_discard_state)
477 pipe->delete_rasterizer_state(pipe, ctx->rs_discard_state);
478 if (ctx->vs)
479 pipe->delete_vs_state(pipe, ctx->vs);
480 if (ctx->vs_nogeneric)
481 pipe->delete_vs_state(pipe, ctx->vs_nogeneric);
482 for (i = 0; i < 4; i++)
483 if (ctx->vs_pos_only[i])
484 pipe->delete_vs_state(pipe, ctx->vs_pos_only[i]);
485 if (ctx->vs_layered)
486 pipe->delete_vs_state(pipe, ctx->vs_layered);
487 pipe->delete_vertex_elements_state(pipe, ctx->velem_state);
488 for (i = 0; i < 4; i++) {
489 if (ctx->velem_state_readbuf[i]) {
490 pipe->delete_vertex_elements_state(pipe, ctx->velem_state_readbuf[i]);
491 }
492 }
493
494 for (i = 0; i < PIPE_MAX_TEXTURE_TYPES; i++) {
495 for (unsigned type = 0; type < ARRAY_SIZE(ctx->fs_texfetch_col); ++type) {
496 for (unsigned inst = 0; inst < 2; inst++) {
497 if (ctx->fs_texfetch_col[type][i][inst])
498 ctx->delete_fs_state(pipe, ctx->fs_texfetch_col[type][i][inst]);
499 }
500 if (ctx->fs_texfetch_col_msaa[type][i])
501 ctx->delete_fs_state(pipe, ctx->fs_texfetch_col_msaa[type][i]);
502 }
503
504 for (unsigned inst = 0; inst < 2; inst++) {
505 if (ctx->fs_texfetch_depth[i][inst])
506 ctx->delete_fs_state(pipe, ctx->fs_texfetch_depth[i][inst]);
507 if (ctx->fs_texfetch_depthstencil[i][inst])
508 ctx->delete_fs_state(pipe, ctx->fs_texfetch_depthstencil[i][inst]);
509 if (ctx->fs_texfetch_stencil[i][inst])
510 ctx->delete_fs_state(pipe, ctx->fs_texfetch_stencil[i][inst]);
511 }
512
513 if (ctx->fs_texfetch_depth_msaa[i])
514 ctx->delete_fs_state(pipe, ctx->fs_texfetch_depth_msaa[i]);
515 if (ctx->fs_texfetch_depthstencil_msaa[i])
516 ctx->delete_fs_state(pipe, ctx->fs_texfetch_depthstencil_msaa[i]);
517 if (ctx->fs_texfetch_stencil_msaa[i])
518 ctx->delete_fs_state(pipe, ctx->fs_texfetch_stencil_msaa[i]);
519
520 for (j = 0; j< ARRAY_SIZE(ctx->fs_resolve[i]); j++)
521 for (f = 0; f < 2; f++)
522 if (ctx->fs_resolve[i][j][f])
523 ctx->delete_fs_state(pipe, ctx->fs_resolve[i][j][f]);
524 }
525
526 if (ctx->fs_empty)
527 ctx->delete_fs_state(pipe, ctx->fs_empty);
528 if (ctx->fs_write_one_cbuf)
529 ctx->delete_fs_state(pipe, ctx->fs_write_one_cbuf);
530 if (ctx->fs_write_all_cbufs)
531 ctx->delete_fs_state(pipe, ctx->fs_write_all_cbufs);
532
533 pipe->delete_sampler_state(pipe, ctx->sampler_state_rect_linear);
534 pipe->delete_sampler_state(pipe, ctx->sampler_state_rect);
535 pipe->delete_sampler_state(pipe, ctx->sampler_state_linear);
536 pipe->delete_sampler_state(pipe, ctx->sampler_state);
537 FREE(ctx);
538 }
539
540 void util_blitter_set_texture_multisample(struct blitter_context *blitter,
541 bool supported)
542 {
543 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
544
545 ctx->has_texture_multisample = supported;
546 }
547
548 void util_blitter_set_running_flag(struct blitter_context *blitter)
549 {
550 if (blitter->running) {
551 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
552 __LINE__);
553 }
554 blitter->running = true;
555
556 blitter->pipe->set_active_query_state(blitter->pipe, false);
557 }
558
559 void util_blitter_unset_running_flag(struct blitter_context *blitter)
560 {
561 if (!blitter->running) {
562 _debug_printf("u_blitter:%i: Caught recursion. This is a driver bug.\n",
563 __LINE__);
564 }
565 blitter->running = false;
566
567 blitter->pipe->set_active_query_state(blitter->pipe, true);
568 }
569
570 static void blitter_check_saved_vertex_states(MAYBE_UNUSED struct blitter_context_priv *ctx)
571 {
572 assert(ctx->base.saved_vs != INVALID_PTR);
573 assert(!ctx->has_geometry_shader || ctx->base.saved_gs != INVALID_PTR);
574 assert(!ctx->has_tessellation || ctx->base.saved_tcs != INVALID_PTR);
575 assert(!ctx->has_tessellation || ctx->base.saved_tes != INVALID_PTR);
576 assert(!ctx->has_stream_out || ctx->base.saved_num_so_targets != ~0u);
577 assert(ctx->base.saved_rs_state != INVALID_PTR);
578 }
579
580 void util_blitter_restore_vertex_states(struct blitter_context *blitter)
581 {
582 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
583 struct pipe_context *pipe = ctx->base.pipe;
584 unsigned i;
585
586 /* Vertex buffer. */
587 if (ctx->base.saved_vertex_buffer.buffer.resource) {
588 pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1,
589 &ctx->base.saved_vertex_buffer);
590 pipe_vertex_buffer_unreference(&ctx->base.saved_vertex_buffer);
591 }
592
593 /* Vertex elements. */
594 if (ctx->base.saved_velem_state != INVALID_PTR) {
595 pipe->bind_vertex_elements_state(pipe, ctx->base.saved_velem_state);
596 ctx->base.saved_velem_state = INVALID_PTR;
597 }
598
599 /* Vertex shader. */
600 pipe->bind_vs_state(pipe, ctx->base.saved_vs);
601 ctx->base.saved_vs = INVALID_PTR;
602
603 /* Geometry shader. */
604 if (ctx->has_geometry_shader) {
605 pipe->bind_gs_state(pipe, ctx->base.saved_gs);
606 ctx->base.saved_gs = INVALID_PTR;
607 }
608
609 if (ctx->has_tessellation) {
610 pipe->bind_tcs_state(pipe, ctx->base.saved_tcs);
611 pipe->bind_tes_state(pipe, ctx->base.saved_tes);
612 ctx->base.saved_tcs = INVALID_PTR;
613 ctx->base.saved_tes = INVALID_PTR;
614 }
615
616 /* Stream outputs. */
617 if (ctx->has_stream_out) {
618 unsigned offsets[PIPE_MAX_SO_BUFFERS];
619 for (i = 0; i < ctx->base.saved_num_so_targets; i++)
620 offsets[i] = (unsigned)-1;
621 pipe->set_stream_output_targets(pipe,
622 ctx->base.saved_num_so_targets,
623 ctx->base.saved_so_targets, offsets);
624
625 for (i = 0; i < ctx->base.saved_num_so_targets; i++)
626 pipe_so_target_reference(&ctx->base.saved_so_targets[i], NULL);
627
628 ctx->base.saved_num_so_targets = ~0;
629 }
630
631 /* Rasterizer. */
632 pipe->bind_rasterizer_state(pipe, ctx->base.saved_rs_state);
633 ctx->base.saved_rs_state = INVALID_PTR;
634 }
635
636 static void blitter_check_saved_fragment_states(MAYBE_UNUSED struct blitter_context_priv *ctx)
637 {
638 assert(ctx->base.saved_fs != INVALID_PTR);
639 assert(ctx->base.saved_dsa_state != INVALID_PTR);
640 assert(ctx->base.saved_blend_state != INVALID_PTR);
641 }
642
643 void util_blitter_restore_fragment_states(struct blitter_context *blitter)
644 {
645 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
646 struct pipe_context *pipe = ctx->base.pipe;
647
648 /* Fragment shader. */
649 ctx->bind_fs_state(pipe, ctx->base.saved_fs);
650 ctx->base.saved_fs = INVALID_PTR;
651
652 /* Depth, stencil, alpha. */
653 pipe->bind_depth_stencil_alpha_state(pipe, ctx->base.saved_dsa_state);
654 ctx->base.saved_dsa_state = INVALID_PTR;
655
656 /* Blend state. */
657 pipe->bind_blend_state(pipe, ctx->base.saved_blend_state);
658 ctx->base.saved_blend_state = INVALID_PTR;
659
660 /* Sample mask. */
661 if (ctx->base.is_sample_mask_saved) {
662 pipe->set_sample_mask(pipe, ctx->base.saved_sample_mask);
663 ctx->base.is_sample_mask_saved = false;
664 }
665
666 /* Miscellaneous states. */
667 /* XXX check whether these are saved and whether they need to be restored
668 * (depending on the operation) */
669 pipe->set_stencil_ref(pipe, &ctx->base.saved_stencil_ref);
670
671 if (!blitter->skip_viewport_restore)
672 pipe->set_viewport_states(pipe, 0, 1, &ctx->base.saved_viewport);
673
674 if (blitter->saved_num_window_rectangles) {
675 pipe->set_window_rectangles(pipe,
676 blitter->saved_window_rectangles_include,
677 blitter->saved_num_window_rectangles,
678 blitter->saved_window_rectangles);
679 }
680 }
681
682 static void blitter_check_saved_fb_state(MAYBE_UNUSED struct blitter_context_priv *ctx)
683 {
684 assert(ctx->base.saved_fb_state.nr_cbufs != (ubyte) ~0);
685 }
686
687 static void blitter_disable_render_cond(struct blitter_context_priv *ctx)
688 {
689 struct pipe_context *pipe = ctx->base.pipe;
690
691 if (ctx->base.saved_render_cond_query) {
692 pipe->render_condition(pipe, NULL, false, 0);
693 }
694 }
695
696 void util_blitter_restore_render_cond(struct blitter_context *blitter)
697 {
698 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
699 struct pipe_context *pipe = ctx->base.pipe;
700
701 if (ctx->base.saved_render_cond_query) {
702 pipe->render_condition(pipe, ctx->base.saved_render_cond_query,
703 ctx->base.saved_render_cond_cond,
704 ctx->base.saved_render_cond_mode);
705 ctx->base.saved_render_cond_query = NULL;
706 }
707 }
708
709 void util_blitter_restore_fb_state(struct blitter_context *blitter)
710 {
711 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
712 struct pipe_context *pipe = ctx->base.pipe;
713
714 pipe->set_framebuffer_state(pipe, &ctx->base.saved_fb_state);
715 util_unreference_framebuffer_state(&ctx->base.saved_fb_state);
716 }
717
718 static void blitter_check_saved_textures(MAYBE_UNUSED struct blitter_context_priv *ctx)
719 {
720 assert(ctx->base.saved_num_sampler_states != ~0u);
721 assert(ctx->base.saved_num_sampler_views != ~0u);
722 }
723
724 void util_blitter_restore_textures(struct blitter_context *blitter)
725 {
726 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
727 struct pipe_context *pipe = ctx->base.pipe;
728 unsigned i;
729
730 /* Fragment sampler states. */
731 pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0,
732 ctx->base.saved_num_sampler_states,
733 ctx->base.saved_sampler_states);
734
735 ctx->base.saved_num_sampler_states = ~0;
736
737 /* Fragment sampler views. */
738 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0,
739 ctx->base.saved_num_sampler_views,
740 ctx->base.saved_sampler_views);
741
742 for (i = 0; i < ctx->base.saved_num_sampler_views; i++)
743 pipe_sampler_view_reference(&ctx->base.saved_sampler_views[i], NULL);
744
745 ctx->base.saved_num_sampler_views = ~0;
746 }
747
748 void util_blitter_restore_constant_buffer_state(struct blitter_context *blitter)
749 {
750 struct pipe_context *pipe = blitter->pipe;
751
752 pipe->set_constant_buffer(pipe, PIPE_SHADER_FRAGMENT, blitter->cb_slot,
753 &blitter->saved_fs_constant_buffer);
754 pipe_resource_reference(&blitter->saved_fs_constant_buffer.buffer, NULL);
755 }
756
757 static void blitter_set_rectangle(struct blitter_context_priv *ctx,
758 int x1, int y1, int x2, int y2,
759 float depth)
760 {
761 int i;
762
763 /* set vertex positions */
764 ctx->vertices[0][0][0] = (float)x1 / ctx->dst_width * 2.0f - 1.0f; /*v0.x*/
765 ctx->vertices[0][0][1] = (float)y1 / ctx->dst_height * 2.0f - 1.0f; /*v0.y*/
766
767 ctx->vertices[1][0][0] = (float)x2 / ctx->dst_width * 2.0f - 1.0f; /*v1.x*/
768 ctx->vertices[1][0][1] = (float)y1 / ctx->dst_height * 2.0f - 1.0f; /*v1.y*/
769
770 ctx->vertices[2][0][0] = (float)x2 / ctx->dst_width * 2.0f - 1.0f; /*v2.x*/
771 ctx->vertices[2][0][1] = (float)y2 / ctx->dst_height * 2.0f - 1.0f; /*v2.y*/
772
773 ctx->vertices[3][0][0] = (float)x1 / ctx->dst_width * 2.0f - 1.0f; /*v3.x*/
774 ctx->vertices[3][0][1] = (float)y2 / ctx->dst_height * 2.0f - 1.0f; /*v3.y*/
775
776 for (i = 0; i < 4; i++)
777 ctx->vertices[i][0][2] = depth; /*z*/
778
779 /* viewport */
780 struct pipe_viewport_state viewport;
781 viewport.scale[0] = 0.5f * ctx->dst_width;
782 viewport.scale[1] = 0.5f * ctx->dst_height;
783 viewport.scale[2] = 1.0f;
784 viewport.translate[0] = 0.5f * ctx->dst_width;
785 viewport.translate[1] = 0.5f * ctx->dst_height;
786 viewport.translate[2] = 0.0f;
787 ctx->base.pipe->set_viewport_states(ctx->base.pipe, 0, 1, &viewport);
788 }
789
790 static void blitter_set_clear_color(struct blitter_context_priv *ctx,
791 const float color[4])
792 {
793 int i;
794
795 if (color) {
796 for (i = 0; i < 4; i++)
797 memcpy(&ctx->vertices[i][1][0], color, sizeof(uint32_t) * 4);
798 } else {
799 for (i = 0; i < 4; i++)
800 memset(&ctx->vertices[i][1][0], 0, sizeof(uint32_t) * 4);
801 }
802 }
803
804 static void get_texcoords(struct pipe_sampler_view *src,
805 unsigned src_width0, unsigned src_height0,
806 int x1, int y1, int x2, int y2,
807 float layer, unsigned sample,
808 bool uses_txf, union blitter_attrib *out)
809 {
810 unsigned level = src->u.tex.first_level;
811 bool normalized = !uses_txf &&
812 src->target != PIPE_TEXTURE_RECT &&
813 src->texture->nr_samples <= 1;
814
815 if (normalized) {
816 out->texcoord.x1 = x1 / (float)u_minify(src_width0, level);
817 out->texcoord.y1 = y1 / (float)u_minify(src_height0, level);
818 out->texcoord.x2 = x2 / (float)u_minify(src_width0, level);
819 out->texcoord.y2 = y2 / (float)u_minify(src_height0, level);
820 } else {
821 out->texcoord.x1 = x1;
822 out->texcoord.y1 = y1;
823 out->texcoord.x2 = x2;
824 out->texcoord.y2 = y2;
825 }
826
827 out->texcoord.z = 0;
828 out->texcoord.w = 0;
829
830 /* Set the layer. */
831 switch (src->target) {
832 case PIPE_TEXTURE_3D:
833 {
834 float r = layer;
835
836 if (!uses_txf)
837 r /= u_minify(src->texture->depth0, src->u.tex.first_level);
838
839 out->texcoord.z = r;
840 }
841 break;
842
843 case PIPE_TEXTURE_1D_ARRAY:
844 out->texcoord.y1 = out->texcoord.y2 = layer;
845 break;
846
847 case PIPE_TEXTURE_2D_ARRAY:
848 out->texcoord.z = layer;
849 out->texcoord.w = sample;
850 break;
851
852 case PIPE_TEXTURE_CUBE_ARRAY:
853 out->texcoord.w = (unsigned)layer / 6;
854 break;
855
856 case PIPE_TEXTURE_2D:
857 out->texcoord.w = sample;
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 set_texcoords_in_vertices(const union blitter_attrib *attrib,
872 float *out, unsigned stride)
873 {
874 out[0] = attrib->texcoord.x1;
875 out[1] = attrib->texcoord.y1;
876 out += stride;
877 out[0] = attrib->texcoord.x2;
878 out[1] = attrib->texcoord.y1;
879 out += stride;
880 out[0] = attrib->texcoord.x2;
881 out[1] = attrib->texcoord.y2;
882 out += stride;
883 out[0] = attrib->texcoord.x1;
884 out[1] = attrib->texcoord.y2;
885 }
886
887 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx,
888 enum pipe_format src_format,
889 enum pipe_format dst_format,
890 enum pipe_texture_target target,
891 unsigned src_nr_samples,
892 unsigned dst_nr_samples,
893 unsigned filter,
894 bool use_txf)
895 {
896 struct pipe_context *pipe = ctx->base.pipe;
897 enum tgsi_texture_type tgsi_tex =
898 util_pipe_tex_to_tgsi_tex(target, src_nr_samples);
899 enum tgsi_return_type stype;
900 enum tgsi_return_type dtype;
901 unsigned type;
902
903 assert(target < PIPE_MAX_TEXTURE_TYPES);
904
905 if (util_format_is_pure_uint(src_format)) {
906 stype = TGSI_RETURN_TYPE_UINT;
907 if (util_format_is_pure_uint(dst_format)) {
908 dtype = TGSI_RETURN_TYPE_UINT;
909 type = 0;
910 } else {
911 assert(util_format_is_pure_sint(dst_format));
912 dtype = TGSI_RETURN_TYPE_SINT;
913 type = 1;
914 }
915 } else if (util_format_is_pure_sint(src_format)) {
916 stype = TGSI_RETURN_TYPE_SINT;
917 if (util_format_is_pure_sint(dst_format)) {
918 dtype = TGSI_RETURN_TYPE_SINT;
919 type = 2;
920 } else {
921 assert(util_format_is_pure_uint(dst_format));
922 dtype = TGSI_RETURN_TYPE_UINT;
923 type = 3;
924 }
925 } else {
926 assert(!util_format_is_pure_uint(dst_format) &&
927 !util_format_is_pure_sint(dst_format));
928 dtype = stype = TGSI_RETURN_TYPE_FLOAT;
929 type = 4;
930 }
931
932 if (src_nr_samples > 1) {
933 void **shader;
934
935 /* OpenGL requires that integer textures just copy 1 sample instead
936 * of averaging.
937 */
938 if (dst_nr_samples <= 1 &&
939 stype != TGSI_RETURN_TYPE_UINT &&
940 stype != TGSI_RETURN_TYPE_SINT) {
941 /* The destination has one sample, so we'll do color resolve. */
942 unsigned index = GET_MSAA_RESOLVE_FS_IDX(src_nr_samples);
943
944 assert(filter < 2);
945
946 shader = &ctx->fs_resolve[target][index][filter];
947
948 if (!*shader) {
949 assert(!ctx->cached_all_shaders);
950 if (filter == PIPE_TEX_FILTER_LINEAR) {
951 *shader = util_make_fs_msaa_resolve_bilinear(pipe, tgsi_tex,
952 src_nr_samples,
953 stype);
954 }
955 else {
956 *shader = util_make_fs_msaa_resolve(pipe, tgsi_tex,
957 src_nr_samples,
958 stype);
959 }
960 }
961 }
962 else {
963 /* The destination has multiple samples, we'll do
964 * an MSAA->MSAA copy.
965 */
966 shader = &ctx->fs_texfetch_col_msaa[type][target];
967
968 /* Create the fragment shader on-demand. */
969 if (!*shader) {
970 assert(!ctx->cached_all_shaders);
971 *shader = util_make_fs_blit_msaa_color(pipe, tgsi_tex, stype, dtype);
972 }
973 }
974
975 return *shader;
976 } else {
977 void **shader;
978
979 if (use_txf)
980 shader = &ctx->fs_texfetch_col[type][target][1];
981 else
982 shader = &ctx->fs_texfetch_col[type][target][0];
983
984 /* Create the fragment shader on-demand. */
985 if (!*shader) {
986 assert(!ctx->cached_all_shaders);
987 *shader = util_make_fragment_tex_shader(pipe, tgsi_tex,
988 TGSI_INTERPOLATE_LINEAR,
989 stype, dtype,
990 ctx->has_tex_lz, use_txf);
991 }
992
993 return *shader;
994 }
995 }
996
997 static inline
998 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx,
999 enum pipe_texture_target target,
1000 unsigned nr_samples,
1001 bool use_txf)
1002 {
1003 struct pipe_context *pipe = ctx->base.pipe;
1004
1005 assert(target < PIPE_MAX_TEXTURE_TYPES);
1006
1007 if (nr_samples > 1) {
1008 void **shader = &ctx->fs_texfetch_depth_msaa[target];
1009
1010 /* Create the fragment shader on-demand. */
1011 if (!*shader) {
1012 enum tgsi_texture_type tgsi_tex;
1013 assert(!ctx->cached_all_shaders);
1014 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
1015 *shader = util_make_fs_blit_msaa_depth(pipe, tgsi_tex);
1016 }
1017
1018 return *shader;
1019 } else {
1020 void **shader;
1021
1022 if (use_txf)
1023 shader = &ctx->fs_texfetch_depth[target][1];
1024 else
1025 shader = &ctx->fs_texfetch_depth[target][0];
1026
1027 /* Create the fragment shader on-demand. */
1028 if (!*shader) {
1029 enum tgsi_texture_type tgsi_tex;
1030 assert(!ctx->cached_all_shaders);
1031 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
1032 *shader =
1033 util_make_fragment_tex_shader_writedepth(pipe, tgsi_tex,
1034 TGSI_INTERPOLATE_LINEAR,
1035 ctx->has_tex_lz, use_txf);
1036 }
1037
1038 return *shader;
1039 }
1040 }
1041
1042 static inline
1043 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv *ctx,
1044 enum pipe_texture_target target,
1045 unsigned nr_samples,
1046 bool use_txf)
1047 {
1048 struct pipe_context *pipe = ctx->base.pipe;
1049
1050 assert(target < PIPE_MAX_TEXTURE_TYPES);
1051
1052 if (nr_samples > 1) {
1053 void **shader = &ctx->fs_texfetch_depthstencil_msaa[target];
1054
1055 /* Create the fragment shader on-demand. */
1056 if (!*shader) {
1057 enum tgsi_texture_type tgsi_tex;
1058 assert(!ctx->cached_all_shaders);
1059 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
1060 *shader = util_make_fs_blit_msaa_depthstencil(pipe, tgsi_tex);
1061 }
1062
1063 return *shader;
1064 } else {
1065 void **shader;
1066
1067 if (use_txf)
1068 shader = &ctx->fs_texfetch_depthstencil[target][1];
1069 else
1070 shader = &ctx->fs_texfetch_depthstencil[target][0];
1071
1072 /* Create the fragment shader on-demand. */
1073 if (!*shader) {
1074 enum tgsi_texture_type tgsi_tex;
1075 assert(!ctx->cached_all_shaders);
1076 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
1077 *shader =
1078 util_make_fragment_tex_shader_writedepthstencil(pipe, tgsi_tex,
1079 TGSI_INTERPOLATE_LINEAR,
1080 ctx->has_tex_lz,
1081 use_txf);
1082 }
1083
1084 return *shader;
1085 }
1086 }
1087
1088 static inline
1089 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv *ctx,
1090 enum pipe_texture_target target,
1091 unsigned nr_samples,
1092 bool use_txf)
1093 {
1094 struct pipe_context *pipe = ctx->base.pipe;
1095
1096 assert(target < PIPE_MAX_TEXTURE_TYPES);
1097
1098 if (nr_samples > 1) {
1099 void **shader = &ctx->fs_texfetch_stencil_msaa[target];
1100
1101 /* Create the fragment shader on-demand. */
1102 if (!*shader) {
1103 enum tgsi_texture_type tgsi_tex;
1104 assert(!ctx->cached_all_shaders);
1105 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
1106 *shader = util_make_fs_blit_msaa_stencil(pipe, tgsi_tex);
1107 }
1108
1109 return *shader;
1110 } else {
1111 void **shader;
1112
1113 if (use_txf)
1114 shader = &ctx->fs_texfetch_stencil[target][1];
1115 else
1116 shader = &ctx->fs_texfetch_stencil[target][0];
1117
1118 /* Create the fragment shader on-demand. */
1119 if (!*shader) {
1120 enum tgsi_texture_type tgsi_tex;
1121 assert(!ctx->cached_all_shaders);
1122 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
1123 *shader =
1124 util_make_fragment_tex_shader_writestencil(pipe, tgsi_tex,
1125 TGSI_INTERPOLATE_LINEAR,
1126 ctx->has_tex_lz, use_txf);
1127 }
1128
1129 return *shader;
1130 }
1131 }
1132
1133
1134 /**
1135 * Generate and save all fragment shaders that we will ever need for
1136 * blitting. Drivers which use the 'draw' fallbacks will typically use
1137 * this to make sure we generate/use shaders that don't go through the
1138 * draw module's wrapper functions.
1139 */
1140 void util_blitter_cache_all_shaders(struct blitter_context *blitter)
1141 {
1142 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1143 struct pipe_context *pipe = blitter->pipe;
1144 struct pipe_screen *screen = pipe->screen;
1145 unsigned samples, j, f, target, max_samples, use_txf;
1146 bool has_arraytex, has_cubearraytex;
1147
1148 max_samples = ctx->has_texture_multisample ? 2 : 1;
1149 has_arraytex = screen->get_param(screen,
1150 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS) != 0;
1151 has_cubearraytex = screen->get_param(screen,
1152 PIPE_CAP_CUBE_MAP_ARRAY) != 0;
1153
1154 /* It only matters if i <= 1 or > 1. */
1155 for (samples = 1; samples <= max_samples; samples++) {
1156 for (target = PIPE_TEXTURE_1D; target < PIPE_MAX_TEXTURE_TYPES; target++) {
1157 for (use_txf = 0; use_txf <= ctx->has_txf; use_txf++) {
1158 if (!has_arraytex &&
1159 (target == PIPE_TEXTURE_1D_ARRAY ||
1160 target == PIPE_TEXTURE_2D_ARRAY)) {
1161 continue;
1162 }
1163 if (!has_cubearraytex &&
1164 (target == PIPE_TEXTURE_CUBE_ARRAY))
1165 continue;
1166
1167 if (samples > 1 &&
1168 (target != PIPE_TEXTURE_2D &&
1169 target != PIPE_TEXTURE_2D_ARRAY))
1170 continue;
1171
1172 if (samples > 1 && use_txf)
1173 continue; /* TXF is the only option, use_txf has no effect */
1174
1175 /* If samples == 1, the shaders read one texel. If samples >= 1,
1176 * they read one sample.
1177 */
1178 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_FLOAT,
1179 PIPE_FORMAT_R32_FLOAT, target,
1180 samples, samples, 0, use_txf);
1181 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT,
1182 PIPE_FORMAT_R32_UINT, target,
1183 samples, samples, 0, use_txf);
1184 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT,
1185 PIPE_FORMAT_R32_SINT, target,
1186 samples, samples, 0, use_txf);
1187 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT,
1188 PIPE_FORMAT_R32_SINT, target,
1189 samples, samples, 0, use_txf);
1190 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT,
1191 PIPE_FORMAT_R32_UINT, target,
1192 samples, samples, 0, use_txf);
1193 blitter_get_fs_texfetch_depth(ctx, target, samples, use_txf);
1194 if (ctx->has_stencil_export) {
1195 blitter_get_fs_texfetch_depthstencil(ctx, target, samples, use_txf);
1196 blitter_get_fs_texfetch_stencil(ctx, target, samples, use_txf);
1197 }
1198
1199 if (samples == 1)
1200 continue;
1201
1202 /* MSAA resolve shaders. */
1203 for (j = 2; j < 32; j++) {
1204 if (!screen->is_format_supported(screen, PIPE_FORMAT_R32_FLOAT,
1205 target, j, j,
1206 PIPE_BIND_SAMPLER_VIEW)) {
1207 continue;
1208 }
1209
1210 for (f = 0; f < 2; f++) {
1211 if (f != PIPE_TEX_FILTER_NEAREST && use_txf)
1212 continue;
1213
1214 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_FLOAT,
1215 PIPE_FORMAT_R32_FLOAT, target,
1216 j, 1, f, use_txf);
1217 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT,
1218 PIPE_FORMAT_R32_UINT, target,
1219 j, 1, f, use_txf);
1220 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT,
1221 PIPE_FORMAT_R32_SINT, target,
1222 j, 1, f, use_txf);
1223 }
1224 }
1225 }
1226 }
1227 }
1228
1229 ctx->fs_empty = util_make_empty_fragment_shader(pipe);
1230
1231 ctx->fs_write_one_cbuf =
1232 util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
1233 TGSI_INTERPOLATE_CONSTANT, false);
1234
1235 ctx->fs_write_all_cbufs =
1236 util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
1237 TGSI_INTERPOLATE_CONSTANT, true);
1238
1239 ctx->cached_all_shaders = true;
1240 }
1241
1242 static void blitter_set_common_draw_rect_state(struct blitter_context_priv *ctx,
1243 bool scissor, bool msaa)
1244 {
1245 struct pipe_context *pipe = ctx->base.pipe;
1246
1247 if (ctx->base.saved_num_window_rectangles)
1248 pipe->set_window_rectangles(pipe, false, 0, NULL);
1249
1250 pipe->bind_rasterizer_state(pipe, ctx->rs_state[scissor][msaa]);
1251
1252 if (ctx->has_geometry_shader)
1253 pipe->bind_gs_state(pipe, NULL);
1254 if (ctx->has_tessellation) {
1255 pipe->bind_tcs_state(pipe, NULL);
1256 pipe->bind_tes_state(pipe, NULL);
1257 }
1258 if (ctx->has_stream_out)
1259 pipe->set_stream_output_targets(pipe, 0, NULL, NULL);
1260 }
1261
1262 static void blitter_draw(struct blitter_context_priv *ctx,
1263 void *vertex_elements_cso,
1264 blitter_get_vs_func get_vs,
1265 int x1, int y1, int x2, int y2, float depth,
1266 unsigned num_instances)
1267 {
1268 struct pipe_context *pipe = ctx->base.pipe;
1269 struct pipe_vertex_buffer vb = {0};
1270
1271 blitter_set_rectangle(ctx, x1, y1, x2, y2, depth);
1272
1273 vb.stride = 8 * sizeof(float);
1274
1275 u_upload_data(pipe->stream_uploader, 0, sizeof(ctx->vertices), 4, ctx->vertices,
1276 &vb.buffer_offset, &vb.buffer.resource);
1277 if (!vb.buffer.resource)
1278 return;
1279 u_upload_unmap(pipe->stream_uploader);
1280
1281 pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
1282 pipe->bind_vertex_elements_state(pipe, vertex_elements_cso);
1283 pipe->bind_vs_state(pipe, get_vs(&ctx->base));
1284
1285 if (ctx->base.use_index_buffer) {
1286 /* Note that for V3D,
1287 * dEQP-GLES3.functional.fbo.blit.rect.nearest_consistency_* require
1288 * that the last vert of the two tris be the same.
1289 */
1290 static uint8_t indices[6] = { 0, 1, 2, 0, 3, 2 };
1291 util_draw_elements_instanced(pipe, indices, 1, 0,
1292 PIPE_PRIM_TRIANGLES, 0, 6,
1293 0, num_instances);
1294 } else {
1295 util_draw_arrays_instanced(pipe, PIPE_PRIM_TRIANGLE_FAN, 0, 4,
1296 0, num_instances);
1297 }
1298 pipe_resource_reference(&vb.buffer.resource, NULL);
1299 }
1300
1301 void util_blitter_draw_rectangle(struct blitter_context *blitter,
1302 void *vertex_elements_cso,
1303 blitter_get_vs_func get_vs,
1304 int x1, int y1, int x2, int y2,
1305 float depth, unsigned num_instances,
1306 enum blitter_attrib_type type,
1307 const union blitter_attrib *attrib)
1308 {
1309 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1310 unsigned i;
1311
1312 switch (type) {
1313 case UTIL_BLITTER_ATTRIB_COLOR:
1314 blitter_set_clear_color(ctx, attrib->color);
1315 break;
1316
1317 case UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW:
1318 for (i = 0; i < 4; i++) {
1319 ctx->vertices[i][1][2] = attrib->texcoord.z;
1320 ctx->vertices[i][1][3] = attrib->texcoord.w;
1321 }
1322 /* fall through */
1323 case UTIL_BLITTER_ATTRIB_TEXCOORD_XY:
1324 set_texcoords_in_vertices(attrib, &ctx->vertices[0][1][0], 8);
1325 break;
1326
1327 default:;
1328 }
1329
1330 blitter_draw(ctx, vertex_elements_cso, get_vs, x1, y1, x2, y2, depth,
1331 num_instances);
1332 }
1333
1334 static void *get_clear_blend_state(struct blitter_context_priv *ctx,
1335 unsigned clear_buffers)
1336 {
1337 struct pipe_context *pipe = ctx->base.pipe;
1338 int index;
1339
1340 clear_buffers &= PIPE_CLEAR_COLOR;
1341
1342 /* Return an existing blend state. */
1343 if (!clear_buffers)
1344 return ctx->blend[0][0];
1345
1346 index = GET_CLEAR_BLEND_STATE_IDX(clear_buffers);
1347
1348 if (ctx->blend_clear[index])
1349 return ctx->blend_clear[index];
1350
1351 /* Create a new one. */
1352 {
1353 struct pipe_blend_state blend = {0};
1354 unsigned i;
1355
1356 blend.independent_blend_enable = 1;
1357
1358 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
1359 if (clear_buffers & (PIPE_CLEAR_COLOR0 << i)) {
1360 blend.rt[i].colormask = PIPE_MASK_RGBA;
1361 }
1362 }
1363
1364 ctx->blend_clear[index] = pipe->create_blend_state(pipe, &blend);
1365 }
1366 return ctx->blend_clear[index];
1367 }
1368
1369 void util_blitter_common_clear_setup(struct blitter_context *blitter,
1370 unsigned width, unsigned height,
1371 unsigned clear_buffers,
1372 void *custom_blend, void *custom_dsa)
1373 {
1374 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1375 struct pipe_context *pipe = ctx->base.pipe;
1376
1377 util_blitter_set_running_flag(blitter);
1378 blitter_check_saved_vertex_states(ctx);
1379 blitter_check_saved_fragment_states(ctx);
1380 blitter_disable_render_cond(ctx);
1381
1382 /* bind states */
1383 if (custom_blend) {
1384 pipe->bind_blend_state(pipe, custom_blend);
1385 } else {
1386 pipe->bind_blend_state(pipe, get_clear_blend_state(ctx, clear_buffers));
1387 }
1388
1389 if (custom_dsa) {
1390 pipe->bind_depth_stencil_alpha_state(pipe, custom_dsa);
1391 } else if ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
1392 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
1393 } else if (clear_buffers & PIPE_CLEAR_DEPTH) {
1394 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
1395 } else if (clear_buffers & PIPE_CLEAR_STENCIL) {
1396 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
1397 } else {
1398 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1399 }
1400
1401 pipe->set_sample_mask(pipe, ~0);
1402 blitter_set_dst_dimensions(ctx, width, height);
1403 }
1404
1405 static void util_blitter_clear_custom(struct blitter_context *blitter,
1406 unsigned width, unsigned height,
1407 unsigned num_layers,
1408 unsigned clear_buffers,
1409 const union pipe_color_union *color,
1410 double depth, unsigned stencil,
1411 void *custom_blend, void *custom_dsa)
1412 {
1413 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1414 struct pipe_context *pipe = ctx->base.pipe;
1415 struct pipe_stencil_ref sr = { { 0 } };
1416
1417 assert(ctx->has_layered || num_layers <= 1);
1418
1419 util_blitter_common_clear_setup(blitter, width, height, clear_buffers,
1420 custom_blend, custom_dsa);
1421
1422 sr.ref_value[0] = stencil & 0xff;
1423 pipe->set_stencil_ref(pipe, &sr);
1424
1425 bind_fs_write_all_cbufs(ctx);
1426
1427 union blitter_attrib attrib;
1428 memcpy(attrib.color, color->ui, sizeof(color->ui));
1429
1430 bool pass_generic = (clear_buffers & PIPE_CLEAR_COLOR) != 0;
1431 enum blitter_attrib_type type = pass_generic ? UTIL_BLITTER_ATTRIB_COLOR :
1432 UTIL_BLITTER_ATTRIB_NONE;
1433
1434 if (num_layers > 1 && ctx->has_layered) {
1435 blitter_get_vs_func get_vs = get_vs_layered;
1436
1437 blitter_set_common_draw_rect_state(ctx, false, false);
1438 blitter->draw_rectangle(blitter, ctx->velem_state, get_vs,
1439 0, 0, width, height,
1440 (float) depth, num_layers, type, &attrib);
1441 } else {
1442 blitter_get_vs_func get_vs;
1443
1444 if (pass_generic)
1445 get_vs = get_vs_passthrough_pos_generic;
1446 else
1447 get_vs = get_vs_passthrough_pos;
1448
1449 blitter_set_common_draw_rect_state(ctx, false, false);
1450 blitter->draw_rectangle(blitter, ctx->velem_state, get_vs,
1451 0, 0, width, height,
1452 (float) depth, 1, type, &attrib);
1453 }
1454
1455 util_blitter_restore_vertex_states(blitter);
1456 util_blitter_restore_fragment_states(blitter);
1457 util_blitter_restore_render_cond(blitter);
1458 util_blitter_unset_running_flag(blitter);
1459 }
1460
1461 void util_blitter_clear(struct blitter_context *blitter,
1462 unsigned width, unsigned height, unsigned num_layers,
1463 unsigned clear_buffers,
1464 const union pipe_color_union *color,
1465 double depth, unsigned stencil)
1466 {
1467 util_blitter_clear_custom(blitter, width, height, num_layers,
1468 clear_buffers, color, depth, stencil,
1469 NULL, NULL);
1470 }
1471
1472 void util_blitter_custom_clear_depth(struct blitter_context *blitter,
1473 unsigned width, unsigned height,
1474 double depth, void *custom_dsa)
1475 {
1476 static const union pipe_color_union color;
1477 util_blitter_clear_custom(blitter, width, height, 0, 0, &color, depth, 0,
1478 NULL, custom_dsa);
1479 }
1480
1481 void util_blitter_default_dst_texture(struct pipe_surface *dst_templ,
1482 struct pipe_resource *dst,
1483 unsigned dstlevel,
1484 unsigned dstz)
1485 {
1486 memset(dst_templ, 0, sizeof(*dst_templ));
1487 dst_templ->format = util_format_linear(dst->format);
1488 dst_templ->u.tex.level = dstlevel;
1489 dst_templ->u.tex.first_layer = dstz;
1490 dst_templ->u.tex.last_layer = dstz;
1491 }
1492
1493 static struct pipe_surface *
1494 util_blitter_get_next_surface_layer(struct pipe_context *pipe,
1495 struct pipe_surface *surf)
1496 {
1497 struct pipe_surface dst_templ;
1498
1499 memset(&dst_templ, 0, sizeof(dst_templ));
1500 dst_templ.format = surf->format;
1501 dst_templ.u.tex.level = surf->u.tex.level;
1502 dst_templ.u.tex.first_layer = surf->u.tex.first_layer + 1;
1503 dst_templ.u.tex.last_layer = surf->u.tex.last_layer + 1;
1504
1505 return pipe->create_surface(pipe, surf->texture, &dst_templ);
1506 }
1507
1508 void util_blitter_default_src_texture(struct blitter_context *blitter,
1509 struct pipe_sampler_view *src_templ,
1510 struct pipe_resource *src,
1511 unsigned srclevel)
1512 {
1513 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1514
1515 memset(src_templ, 0, sizeof(*src_templ));
1516
1517 if (ctx->cube_as_2darray &&
1518 (src->target == PIPE_TEXTURE_CUBE ||
1519 src->target == PIPE_TEXTURE_CUBE_ARRAY))
1520 src_templ->target = PIPE_TEXTURE_2D_ARRAY;
1521 else
1522 src_templ->target = src->target;
1523
1524 src_templ->format = util_format_linear(src->format);
1525 src_templ->u.tex.first_level = srclevel;
1526 src_templ->u.tex.last_level = srclevel;
1527 src_templ->u.tex.first_layer = 0;
1528 src_templ->u.tex.last_layer =
1529 src->target == PIPE_TEXTURE_3D ? u_minify(src->depth0, srclevel) - 1
1530 : (unsigned)(src->array_size - 1);
1531 src_templ->swizzle_r = PIPE_SWIZZLE_X;
1532 src_templ->swizzle_g = PIPE_SWIZZLE_Y;
1533 src_templ->swizzle_b = PIPE_SWIZZLE_Z;
1534 src_templ->swizzle_a = PIPE_SWIZZLE_W;
1535 }
1536
1537 static bool is_blit_generic_supported(struct blitter_context *blitter,
1538 const struct pipe_resource *dst,
1539 enum pipe_format dst_format,
1540 const struct pipe_resource *src,
1541 enum pipe_format src_format,
1542 unsigned mask)
1543 {
1544 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1545 struct pipe_screen *screen = ctx->base.pipe->screen;
1546
1547 if (dst) {
1548 unsigned bind;
1549 const struct util_format_description *desc =
1550 util_format_description(dst_format);
1551 bool dst_has_stencil = util_format_has_stencil(desc);
1552
1553 /* Stencil export must be supported for stencil copy. */
1554 if ((mask & PIPE_MASK_S) && dst_has_stencil &&
1555 !ctx->has_stencil_export) {
1556 return false;
1557 }
1558
1559 if (dst_has_stencil || util_format_has_depth(desc))
1560 bind = PIPE_BIND_DEPTH_STENCIL;
1561 else
1562 bind = PIPE_BIND_RENDER_TARGET;
1563
1564 if (!screen->is_format_supported(screen, dst_format, dst->target,
1565 dst->nr_samples, dst->nr_storage_samples,
1566 bind)) {
1567 return false;
1568 }
1569 }
1570
1571 if (src) {
1572 if (src->nr_samples > 1 && !ctx->has_texture_multisample) {
1573 return false;
1574 }
1575
1576 if (!screen->is_format_supported(screen, src_format, src->target,
1577 src->nr_samples, src->nr_storage_samples,
1578 PIPE_BIND_SAMPLER_VIEW)) {
1579 return false;
1580 }
1581
1582 /* Check stencil sampler support for stencil copy. */
1583 if (mask & PIPE_MASK_S) {
1584 if (util_format_has_stencil(util_format_description(src_format))) {
1585 enum pipe_format stencil_format =
1586 util_format_stencil_only(src_format);
1587 assert(stencil_format != PIPE_FORMAT_NONE);
1588
1589 if (stencil_format != src_format &&
1590 !screen->is_format_supported(screen, stencil_format,
1591 src->target, src->nr_samples,
1592 src->nr_storage_samples,
1593 PIPE_BIND_SAMPLER_VIEW)) {
1594 return false;
1595 }
1596 }
1597 }
1598 }
1599
1600 return true;
1601 }
1602
1603 bool util_blitter_is_copy_supported(struct blitter_context *blitter,
1604 const struct pipe_resource *dst,
1605 const struct pipe_resource *src)
1606 {
1607 return is_blit_generic_supported(blitter, dst, dst->format,
1608 src, src->format, PIPE_MASK_RGBAZS);
1609 }
1610
1611 bool util_blitter_is_blit_supported(struct blitter_context *blitter,
1612 const struct pipe_blit_info *info)
1613 {
1614 return is_blit_generic_supported(blitter,
1615 info->dst.resource, info->dst.format,
1616 info->src.resource, info->src.format,
1617 info->mask);
1618 }
1619
1620 void util_blitter_copy_texture(struct blitter_context *blitter,
1621 struct pipe_resource *dst,
1622 unsigned dst_level,
1623 unsigned dstx, unsigned dsty, unsigned dstz,
1624 struct pipe_resource *src,
1625 unsigned src_level,
1626 const struct pipe_box *srcbox)
1627 {
1628 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1629 struct pipe_context *pipe = ctx->base.pipe;
1630 struct pipe_surface *dst_view, dst_templ;
1631 struct pipe_sampler_view src_templ, *src_view;
1632 struct pipe_box dstbox;
1633
1634 assert(dst && src);
1635 assert(src->target < PIPE_MAX_TEXTURE_TYPES);
1636
1637 u_box_3d(dstx, dsty, dstz, abs(srcbox->width), abs(srcbox->height),
1638 abs(srcbox->depth), &dstbox);
1639
1640 /* Initialize the surface. */
1641 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
1642 dst_view = pipe->create_surface(pipe, dst, &dst_templ);
1643
1644 /* Initialize the sampler view. */
1645 util_blitter_default_src_texture(blitter, &src_templ, src, src_level);
1646 src_view = pipe->create_sampler_view(pipe, src, &src_templ);
1647
1648 /* Copy. */
1649 util_blitter_blit_generic(blitter, dst_view, &dstbox,
1650 src_view, srcbox, src->width0, src->height0,
1651 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,
1652 false);
1653
1654 pipe_surface_reference(&dst_view, NULL);
1655 pipe_sampler_view_reference(&src_view, NULL);
1656 }
1657
1658 static void
1659 blitter_draw_tex(struct blitter_context_priv *ctx,
1660 int dst_x1, int dst_y1, int dst_x2, int dst_y2,
1661 struct pipe_sampler_view *src,
1662 unsigned src_width0, unsigned src_height0,
1663 int src_x1, int src_y1, int src_x2, int src_y2,
1664 float layer, unsigned sample,
1665 bool uses_txf, enum blitter_attrib_type type)
1666 {
1667 union blitter_attrib coord;
1668 blitter_get_vs_func get_vs = get_vs_passthrough_pos_generic;
1669
1670 get_texcoords(src, src_width0, src_height0,
1671 src_x1, src_y1, src_x2, src_y2, layer, sample,
1672 uses_txf, &coord);
1673
1674 if (src->target == PIPE_TEXTURE_CUBE ||
1675 src->target == PIPE_TEXTURE_CUBE_ARRAY) {
1676 float face_coord[4][2];
1677
1678 set_texcoords_in_vertices(&coord, &face_coord[0][0], 2);
1679 util_map_texcoords2d_onto_cubemap((unsigned)layer % 6,
1680 /* pointer, stride in floats */
1681 &face_coord[0][0], 2,
1682 &ctx->vertices[0][1][0], 8,
1683 false);
1684 for (unsigned i = 0; i < 4; i++)
1685 ctx->vertices[i][1][3] = coord.texcoord.w;
1686
1687 /* Cubemaps don't use draw_rectangle. */
1688 blitter_draw(ctx, ctx->velem_state, get_vs,
1689 dst_x1, dst_y1, dst_x2, dst_y2, 0, 1);
1690 } else {
1691 ctx->base.draw_rectangle(&ctx->base, ctx->velem_state, get_vs,
1692 dst_x1, dst_y1, dst_x2, dst_y2,
1693 0, 1, type, &coord);
1694 }
1695 }
1696
1697 static void do_blits(struct blitter_context_priv *ctx,
1698 struct pipe_surface *dst,
1699 const struct pipe_box *dstbox,
1700 struct pipe_sampler_view *src,
1701 unsigned src_width0,
1702 unsigned src_height0,
1703 const struct pipe_box *srcbox,
1704 bool is_zsbuf,
1705 bool uses_txf)
1706 {
1707 struct pipe_context *pipe = ctx->base.pipe;
1708 unsigned src_samples = src->texture->nr_samples;
1709 unsigned dst_samples = dst->texture->nr_samples;
1710 enum pipe_texture_target src_target = src->target;
1711 struct pipe_framebuffer_state fb_state = {0};
1712
1713 /* Initialize framebuffer state. */
1714 fb_state.width = dst->width;
1715 fb_state.height = dst->height;
1716 fb_state.nr_cbufs = is_zsbuf ? 0 : 1;
1717
1718 blitter_set_dst_dimensions(ctx, fb_state.width, fb_state.height);
1719
1720 if ((src_target == PIPE_TEXTURE_1D ||
1721 src_target == PIPE_TEXTURE_2D ||
1722 src_target == PIPE_TEXTURE_RECT) &&
1723 src_samples <= 1) {
1724 /* Set framebuffer state. */
1725 if (is_zsbuf) {
1726 fb_state.zsbuf = dst;
1727 } else {
1728 fb_state.cbufs[0] = dst;
1729 }
1730 pipe->set_framebuffer_state(pipe, &fb_state);
1731
1732 /* Draw. */
1733 pipe->set_sample_mask(pipe, ~0);
1734 blitter_draw_tex(ctx, dstbox->x, dstbox->y,
1735 dstbox->x + dstbox->width,
1736 dstbox->y + dstbox->height,
1737 src, src_width0, src_height0, srcbox->x, srcbox->y,
1738 srcbox->x + srcbox->width, srcbox->y + srcbox->height,
1739 0, 0, uses_txf, UTIL_BLITTER_ATTRIB_TEXCOORD_XY);
1740 } else {
1741 /* Draw the quad with the generic codepath. */
1742 int dst_z;
1743 for (dst_z = 0; dst_z < dstbox->depth; dst_z++) {
1744 struct pipe_surface *old;
1745 float dst2src_scale = srcbox->depth / (float)dstbox->depth;
1746
1747 /* Scale Z properly if the blit is scaled.
1748 *
1749 * When downscaling, we want the coordinates centered, so that
1750 * mipmapping works for 3D textures. For example, when generating
1751 * a 4x4x4 level, this wouldn't average the pixels:
1752 *
1753 * src Z: 0 1 2 3 4 5 6 7
1754 * dst Z: 0 1 2 3
1755 *
1756 * Because the pixels are not centered below the pixels of the higher
1757 * level. Therefore, we want this:
1758 * src Z: 0 1 2 3 4 5 6 7
1759 * dst Z: 0 1 2 3
1760 *
1761 * dst_offset defines the offset needed for centering the pixels and
1762 * it works with any scaling (not just 2x).
1763 */
1764 float dst_offset = ((srcbox->depth - 1) -
1765 (dstbox->depth - 1) * dst2src_scale) * 0.5;
1766 float src_z = (dst_z + dst_offset) * dst2src_scale;
1767
1768 /* Set framebuffer state. */
1769 if (is_zsbuf) {
1770 fb_state.zsbuf = dst;
1771 } else {
1772 fb_state.cbufs[0] = dst;
1773 }
1774 pipe->set_framebuffer_state(pipe, &fb_state);
1775
1776 /* See if we need to blit a multisample or singlesample buffer. */
1777 if (src_samples == dst_samples && dst_samples > 1) {
1778 /* MSAA copy. */
1779 unsigned i, max_sample = dst_samples - 1;
1780
1781 for (i = 0; i <= max_sample; i++) {
1782 pipe->set_sample_mask(pipe, 1 << i);
1783 blitter_draw_tex(ctx, dstbox->x, dstbox->y,
1784 dstbox->x + dstbox->width,
1785 dstbox->y + dstbox->height,
1786 src, src_width0, src_height0,
1787 srcbox->x, srcbox->y,
1788 srcbox->x + srcbox->width,
1789 srcbox->y + srcbox->height,
1790 srcbox->z + src_z, i, uses_txf,
1791 UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW);
1792 }
1793 } else {
1794 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1795 pipe->set_sample_mask(pipe, ~0);
1796 blitter_draw_tex(ctx, dstbox->x, dstbox->y,
1797 dstbox->x + dstbox->width,
1798 dstbox->y + dstbox->height,
1799 src, src_width0, src_height0,
1800 srcbox->x, srcbox->y,
1801 srcbox->x + srcbox->width,
1802 srcbox->y + srcbox->height,
1803 srcbox->z + src_z, 0, uses_txf,
1804 UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW);
1805 }
1806
1807 /* Get the next surface or (if this is the last iteration)
1808 * just unreference the last one. */
1809 old = dst;
1810 if (dst_z < dstbox->depth-1) {
1811 dst = util_blitter_get_next_surface_layer(ctx->base.pipe, dst);
1812 }
1813 if (dst_z) {
1814 pipe_surface_reference(&old, NULL);
1815 }
1816 }
1817 }
1818 }
1819
1820 void util_blitter_blit_generic(struct blitter_context *blitter,
1821 struct pipe_surface *dst,
1822 const struct pipe_box *dstbox,
1823 struct pipe_sampler_view *src,
1824 const struct pipe_box *srcbox,
1825 unsigned src_width0, unsigned src_height0,
1826 unsigned mask, unsigned filter,
1827 const struct pipe_scissor_state *scissor,
1828 bool alpha_blend)
1829 {
1830 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1831 struct pipe_context *pipe = ctx->base.pipe;
1832 enum pipe_texture_target src_target = src->target;
1833 unsigned src_samples = src->texture->nr_samples;
1834 unsigned dst_samples = dst->texture->nr_samples;
1835 bool has_depth, has_stencil, has_color;
1836 bool blit_stencil, blit_depth, blit_color;
1837 void *sampler_state;
1838 const struct util_format_description *src_desc =
1839 util_format_description(src->format);
1840 const struct util_format_description *dst_desc =
1841 util_format_description(dst->format);
1842
1843 has_color = src_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS &&
1844 dst_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS;
1845 has_depth = util_format_has_depth(src_desc) &&
1846 util_format_has_depth(dst_desc);
1847 has_stencil = util_format_has_stencil(src_desc) &&
1848 util_format_has_stencil(dst_desc);
1849
1850 blit_color = has_color && (mask & PIPE_MASK_RGBA);
1851 blit_depth = has_depth && (mask & PIPE_MASK_Z);
1852 blit_stencil = has_stencil && (mask & PIPE_MASK_S) &&
1853 ctx->has_stencil_export;
1854
1855 if (!blit_stencil && !blit_depth && !blit_color) {
1856 return;
1857 }
1858
1859 bool is_scaled = dstbox->width != abs(srcbox->width) ||
1860 dstbox->height != abs(srcbox->height);
1861
1862 if (blit_stencil || !is_scaled)
1863 filter = PIPE_TEX_FILTER_NEAREST;
1864
1865 bool use_txf = false;
1866
1867 /* Don't support scaled blits. The TXF shader uses F2I for rounding. */
1868 if (ctx->has_txf &&
1869 !is_scaled &&
1870 filter == PIPE_TEX_FILTER_NEAREST &&
1871 src->target != PIPE_TEXTURE_CUBE &&
1872 src->target != PIPE_TEXTURE_CUBE_ARRAY) {
1873 int src_width = u_minify(src_width0, src->u.tex.first_level);
1874 int src_height = u_minify(src_height0, src->u.tex.first_level);
1875 int src_depth = src->u.tex.last_layer + 1;
1876 struct pipe_box box = *srcbox;
1877
1878 /* Eliminate negative width/height/depth. */
1879 if (box.width < 0) {
1880 box.x += box.width;
1881 box.width *= -1;
1882 }
1883 if (box.height < 0) {
1884 box.y += box.height;
1885 box.height *= -1;
1886 }
1887 if (box.depth < 0) {
1888 box.z += box.depth;
1889 box.depth *= -1;
1890 }
1891
1892 /* See if srcbox is in bounds. TXF doesn't clamp the coordinates. */
1893 use_txf =
1894 box.x >= 0 && box.x < src_width &&
1895 box.y >= 0 && box.y < src_height &&
1896 box.z >= 0 && box.z < src_depth &&
1897 box.x + box.width > 0 && box.x + box.width <= src_width &&
1898 box.y + box.height > 0 && box.y + box.height <= src_height &&
1899 box.z + box.depth > 0 && box.z + box.depth <= src_depth;
1900 }
1901
1902 /* Check whether the states are properly saved. */
1903 util_blitter_set_running_flag(blitter);
1904 blitter_check_saved_vertex_states(ctx);
1905 blitter_check_saved_fragment_states(ctx);
1906 blitter_check_saved_textures(ctx);
1907 blitter_check_saved_fb_state(ctx);
1908 blitter_disable_render_cond(ctx);
1909
1910 if (blit_depth || blit_stencil) {
1911 pipe->bind_blend_state(pipe, ctx->blend[0][0]);
1912
1913 if (blit_depth && blit_stencil) {
1914 pipe->bind_depth_stencil_alpha_state(pipe,
1915 ctx->dsa_write_depth_stencil);
1916 ctx->bind_fs_state(pipe,
1917 blitter_get_fs_texfetch_depthstencil(ctx, src_target,
1918 src_samples, use_txf));
1919 } else if (blit_depth) {
1920 pipe->bind_depth_stencil_alpha_state(pipe,
1921 ctx->dsa_write_depth_keep_stencil);
1922 ctx->bind_fs_state(pipe,
1923 blitter_get_fs_texfetch_depth(ctx, src_target,
1924 src_samples, use_txf));
1925 } else { /* is_stencil */
1926 pipe->bind_depth_stencil_alpha_state(pipe,
1927 ctx->dsa_keep_depth_write_stencil);
1928 ctx->bind_fs_state(pipe,
1929 blitter_get_fs_texfetch_stencil(ctx, src_target,
1930 src_samples, use_txf));
1931 }
1932
1933 } else {
1934 unsigned colormask = mask & PIPE_MASK_RGBA;
1935
1936 pipe->bind_blend_state(pipe, ctx->blend[colormask][alpha_blend]);
1937 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1938 ctx->bind_fs_state(pipe,
1939 blitter_get_fs_texfetch_col(ctx, src->format, dst->format, src_target,
1940 src_samples, dst_samples, filter,
1941 use_txf));
1942 }
1943
1944 /* Set the linear filter only for scaled color non-MSAA blits. */
1945 if (filter == PIPE_TEX_FILTER_LINEAR) {
1946 if (src_target == PIPE_TEXTURE_RECT) {
1947 sampler_state = ctx->sampler_state_rect_linear;
1948 } else {
1949 sampler_state = ctx->sampler_state_linear;
1950 }
1951 } else {
1952 if (src_target == PIPE_TEXTURE_RECT) {
1953 sampler_state = ctx->sampler_state_rect;
1954 } else {
1955 sampler_state = ctx->sampler_state;
1956 }
1957 }
1958
1959 /* Set samplers. */
1960 if (blit_depth && blit_stencil) {
1961 /* Setup two samplers, one for depth and the other one for stencil. */
1962 struct pipe_sampler_view templ;
1963 struct pipe_sampler_view *views[2];
1964 void *samplers[2] = {sampler_state, sampler_state};
1965
1966 templ = *src;
1967 templ.format = util_format_stencil_only(templ.format);
1968 assert(templ.format != PIPE_FORMAT_NONE);
1969
1970 views[0] = src;
1971 views[1] = pipe->create_sampler_view(pipe, src->texture, &templ);
1972
1973 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 2, views);
1974 pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0, 2, samplers);
1975
1976 pipe_sampler_view_reference(&views[1], NULL);
1977 } else if (blit_stencil) {
1978 /* Set a stencil-only sampler view for it not to sample depth instead. */
1979 struct pipe_sampler_view templ;
1980 struct pipe_sampler_view *view;
1981
1982 templ = *src;
1983 templ.format = util_format_stencil_only(templ.format);
1984 assert(templ.format != PIPE_FORMAT_NONE);
1985
1986 view = pipe->create_sampler_view(pipe, src->texture, &templ);
1987
1988 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &view);
1989 pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
1990 0, 1, &sampler_state);
1991
1992 pipe_sampler_view_reference(&view, NULL);
1993 } else {
1994 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &src);
1995 pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
1996 0, 1, &sampler_state);
1997 }
1998
1999 if (scissor) {
2000 pipe->set_scissor_states(pipe, 0, 1, scissor);
2001 }
2002
2003 blitter_set_common_draw_rect_state(ctx, scissor != NULL, dst_samples > 1);
2004
2005 do_blits(ctx, dst, dstbox, src, src_width0, src_height0,
2006 srcbox, blit_depth || blit_stencil, use_txf);
2007
2008 util_blitter_restore_vertex_states(blitter);
2009 util_blitter_restore_fragment_states(blitter);
2010 util_blitter_restore_textures(blitter);
2011 util_blitter_restore_fb_state(blitter);
2012 if (scissor) {
2013 pipe->set_scissor_states(pipe, 0, 1, &ctx->base.saved_scissor);
2014 }
2015 util_blitter_restore_render_cond(blitter);
2016 util_blitter_unset_running_flag(blitter);
2017 }
2018
2019 void
2020 util_blitter_blit(struct blitter_context *blitter,
2021 const struct pipe_blit_info *info)
2022 {
2023 struct pipe_resource *dst = info->dst.resource;
2024 struct pipe_resource *src = info->src.resource;
2025 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2026 struct pipe_context *pipe = ctx->base.pipe;
2027 struct pipe_surface *dst_view, dst_templ;
2028 struct pipe_sampler_view src_templ, *src_view;
2029
2030 /* Initialize the surface. */
2031 util_blitter_default_dst_texture(&dst_templ, dst, info->dst.level,
2032 info->dst.box.z);
2033 dst_templ.format = info->dst.format;
2034 dst_view = pipe->create_surface(pipe, dst, &dst_templ);
2035
2036 /* Initialize the sampler view. */
2037 util_blitter_default_src_texture(blitter, &src_templ, src, info->src.level);
2038 src_templ.format = info->src.format;
2039 src_view = pipe->create_sampler_view(pipe, src, &src_templ);
2040
2041 /* Copy. */
2042 util_blitter_blit_generic(blitter, dst_view, &info->dst.box,
2043 src_view, &info->src.box, src->width0, src->height0,
2044 info->mask, info->filter,
2045 info->scissor_enable ? &info->scissor : NULL,
2046 info->alpha_blend);
2047
2048 pipe_surface_reference(&dst_view, NULL);
2049 pipe_sampler_view_reference(&src_view, NULL);
2050 }
2051
2052 void util_blitter_generate_mipmap(struct blitter_context *blitter,
2053 struct pipe_resource *tex,
2054 enum pipe_format format,
2055 unsigned base_level, unsigned last_level,
2056 unsigned first_layer, unsigned last_layer)
2057 {
2058 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2059 struct pipe_context *pipe = ctx->base.pipe;
2060 struct pipe_surface dst_templ, *dst_view;
2061 struct pipe_sampler_view src_templ, *src_view;
2062 bool is_depth;
2063 void *sampler_state;
2064 const struct util_format_description *desc =
2065 util_format_description(format);
2066 unsigned src_level;
2067 unsigned target = tex->target;
2068
2069 if (ctx->cube_as_2darray &&
2070 (target == PIPE_TEXTURE_CUBE || target == PIPE_TEXTURE_CUBE_ARRAY))
2071 target = PIPE_TEXTURE_2D_ARRAY;
2072
2073 assert(tex->nr_samples <= 1);
2074 /* Disallow stencil formats without depth. */
2075 assert(!util_format_has_stencil(desc) || util_format_has_depth(desc));
2076
2077 is_depth = desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS;
2078
2079 /* Check whether the states are properly saved. */
2080 util_blitter_set_running_flag(blitter);
2081 blitter_check_saved_vertex_states(ctx);
2082 blitter_check_saved_fragment_states(ctx);
2083 blitter_check_saved_textures(ctx);
2084 blitter_check_saved_fb_state(ctx);
2085 blitter_disable_render_cond(ctx);
2086
2087 /* Set states. */
2088 if (is_depth) {
2089 pipe->bind_blend_state(pipe, ctx->blend[0][0]);
2090 pipe->bind_depth_stencil_alpha_state(pipe,
2091 ctx->dsa_write_depth_keep_stencil);
2092 ctx->bind_fs_state(pipe,
2093 blitter_get_fs_texfetch_depth(ctx, target, 1, false));
2094 } else {
2095 pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA][0]);
2096 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2097 ctx->bind_fs_state(pipe,
2098 blitter_get_fs_texfetch_col(ctx, tex->format, tex->format, target,
2099 1, 1, PIPE_TEX_FILTER_LINEAR, false));
2100 }
2101
2102 if (target == PIPE_TEXTURE_RECT) {
2103 sampler_state = ctx->sampler_state_rect_linear;
2104 } else {
2105 sampler_state = ctx->sampler_state_linear;
2106 }
2107 pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
2108 0, 1, &sampler_state);
2109
2110 blitter_set_common_draw_rect_state(ctx, false, false);
2111
2112 for (src_level = base_level; src_level < last_level; src_level++) {
2113 struct pipe_box dstbox = {0}, srcbox = {0};
2114 unsigned dst_level = src_level + 1;
2115
2116 dstbox.width = u_minify(tex->width0, dst_level);
2117 dstbox.height = u_minify(tex->height0, dst_level);
2118
2119 srcbox.width = u_minify(tex->width0, src_level);
2120 srcbox.height = u_minify(tex->height0, src_level);
2121
2122 if (target == PIPE_TEXTURE_3D) {
2123 dstbox.depth = util_num_layers(tex, dst_level);
2124 srcbox.depth = util_num_layers(tex, src_level);
2125 } else {
2126 dstbox.z = srcbox.z = first_layer;
2127 dstbox.depth = srcbox.depth = last_layer - first_layer + 1;
2128 }
2129
2130 /* Initialize the surface. */
2131 util_blitter_default_dst_texture(&dst_templ, tex, dst_level,
2132 first_layer);
2133 dst_templ.format = format;
2134 dst_view = pipe->create_surface(pipe, tex, &dst_templ);
2135
2136 /* Initialize the sampler view. */
2137 util_blitter_default_src_texture(blitter, &src_templ, tex, src_level);
2138 src_templ.format = format;
2139 src_view = pipe->create_sampler_view(pipe, tex, &src_templ);
2140
2141 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &src_view);
2142
2143 do_blits(ctx, dst_view, &dstbox, src_view, tex->width0, tex->height0,
2144 &srcbox, is_depth, false);
2145
2146 pipe_surface_reference(&dst_view, NULL);
2147 pipe_sampler_view_reference(&src_view, NULL);
2148 }
2149
2150 util_blitter_restore_vertex_states(blitter);
2151 util_blitter_restore_fragment_states(blitter);
2152 util_blitter_restore_textures(blitter);
2153 util_blitter_restore_fb_state(blitter);
2154 util_blitter_restore_render_cond(blitter);
2155 util_blitter_unset_running_flag(blitter);
2156 }
2157
2158 /* Clear a region of a color surface to a constant value. */
2159 void util_blitter_clear_render_target(struct blitter_context *blitter,
2160 struct pipe_surface *dstsurf,
2161 const union pipe_color_union *color,
2162 unsigned dstx, unsigned dsty,
2163 unsigned width, unsigned height)
2164 {
2165 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2166 struct pipe_context *pipe = ctx->base.pipe;
2167 struct pipe_framebuffer_state fb_state;
2168 unsigned num_layers;
2169
2170 assert(dstsurf->texture);
2171 if (!dstsurf->texture)
2172 return;
2173
2174 /* check the saved state */
2175 util_blitter_set_running_flag(blitter);
2176 blitter_check_saved_vertex_states(ctx);
2177 blitter_check_saved_fragment_states(ctx);
2178 blitter_check_saved_fb_state(ctx);
2179 blitter_disable_render_cond(ctx);
2180
2181 /* bind states */
2182 pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA][0]);
2183 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2184 bind_fs_write_one_cbuf(ctx);
2185
2186 /* set a framebuffer state */
2187 fb_state.width = dstsurf->width;
2188 fb_state.height = dstsurf->height;
2189 fb_state.nr_cbufs = 1;
2190 fb_state.cbufs[0] = dstsurf;
2191 fb_state.zsbuf = 0;
2192 pipe->set_framebuffer_state(pipe, &fb_state);
2193 pipe->set_sample_mask(pipe, ~0);
2194
2195 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
2196
2197 union blitter_attrib attrib;
2198 memcpy(attrib.color, color->ui, sizeof(color->ui));
2199
2200 num_layers = dstsurf->u.tex.last_layer - dstsurf->u.tex.first_layer + 1;
2201 if (num_layers > 1 && ctx->has_layered) {
2202 blitter_set_common_draw_rect_state(ctx, false, false);
2203 blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_layered,
2204 dstx, dsty, dstx+width, dsty+height, 0,
2205 num_layers, UTIL_BLITTER_ATTRIB_COLOR, &attrib);
2206 } else {
2207 blitter_set_common_draw_rect_state(ctx, false, false);
2208 blitter->draw_rectangle(blitter, ctx->velem_state,
2209 get_vs_passthrough_pos_generic,
2210 dstx, dsty, dstx+width, dsty+height, 0,
2211 1, UTIL_BLITTER_ATTRIB_COLOR, &attrib);
2212 }
2213
2214 util_blitter_restore_vertex_states(blitter);
2215 util_blitter_restore_fragment_states(blitter);
2216 util_blitter_restore_fb_state(blitter);
2217 util_blitter_restore_render_cond(blitter);
2218 util_blitter_unset_running_flag(blitter);
2219 }
2220
2221 /* Clear a region of a depth stencil surface. */
2222 void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
2223 struct pipe_surface *dstsurf,
2224 unsigned clear_flags,
2225 double depth,
2226 unsigned stencil,
2227 unsigned dstx, unsigned dsty,
2228 unsigned width, unsigned height)
2229 {
2230 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2231 struct pipe_context *pipe = ctx->base.pipe;
2232 struct pipe_framebuffer_state fb_state;
2233 struct pipe_stencil_ref sr = { { 0 } };
2234 unsigned num_layers;
2235
2236 assert(dstsurf->texture);
2237 if (!dstsurf->texture)
2238 return;
2239
2240 /* check the saved state */
2241 util_blitter_set_running_flag(blitter);
2242 blitter_check_saved_vertex_states(ctx);
2243 blitter_check_saved_fragment_states(ctx);
2244 blitter_check_saved_fb_state(ctx);
2245 blitter_disable_render_cond(ctx);
2246
2247 /* bind states */
2248 pipe->bind_blend_state(pipe, ctx->blend[0][0]);
2249 if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
2250 sr.ref_value[0] = stencil & 0xff;
2251 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
2252 pipe->set_stencil_ref(pipe, &sr);
2253 }
2254 else if (clear_flags & PIPE_CLEAR_DEPTH) {
2255 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
2256 }
2257 else if (clear_flags & PIPE_CLEAR_STENCIL) {
2258 sr.ref_value[0] = stencil & 0xff;
2259 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
2260 pipe->set_stencil_ref(pipe, &sr);
2261 }
2262 else
2263 /* hmm that should be illegal probably, or make it a no-op somewhere */
2264 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2265
2266 bind_fs_empty(ctx);
2267
2268 /* set a framebuffer state */
2269 fb_state.width = dstsurf->width;
2270 fb_state.height = dstsurf->height;
2271 fb_state.nr_cbufs = 0;
2272 fb_state.cbufs[0] = 0;
2273 fb_state.zsbuf = dstsurf;
2274 pipe->set_framebuffer_state(pipe, &fb_state);
2275 pipe->set_sample_mask(pipe, ~0);
2276
2277 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
2278
2279 num_layers = dstsurf->u.tex.last_layer - dstsurf->u.tex.first_layer + 1;
2280 if (num_layers > 1 && ctx->has_layered) {
2281 blitter_set_common_draw_rect_state(ctx, false, false);
2282 blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_layered,
2283 dstx, dsty, dstx+width, dsty+height, depth,
2284 num_layers, UTIL_BLITTER_ATTRIB_NONE, NULL);
2285 } else {
2286 blitter_set_common_draw_rect_state(ctx, false, false);
2287 blitter->draw_rectangle(blitter, ctx->velem_state,
2288 get_vs_passthrough_pos,
2289 dstx, dsty, dstx+width, dsty+height, depth, 1,
2290 UTIL_BLITTER_ATTRIB_NONE, NULL);
2291 }
2292
2293 util_blitter_restore_vertex_states(blitter);
2294 util_blitter_restore_fragment_states(blitter);
2295 util_blitter_restore_fb_state(blitter);
2296 util_blitter_restore_render_cond(blitter);
2297 util_blitter_unset_running_flag(blitter);
2298 }
2299
2300 /* draw a rectangle across a region using a custom dsa stage - for r600g */
2301 void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
2302 struct pipe_surface *zsurf,
2303 struct pipe_surface *cbsurf,
2304 unsigned sample_mask,
2305 void *dsa_stage, float depth)
2306 {
2307 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2308 struct pipe_context *pipe = ctx->base.pipe;
2309 struct pipe_framebuffer_state fb_state;
2310
2311 assert(zsurf->texture);
2312 if (!zsurf->texture)
2313 return;
2314
2315 /* check the saved state */
2316 util_blitter_set_running_flag(blitter);
2317 blitter_check_saved_vertex_states(ctx);
2318 blitter_check_saved_fragment_states(ctx);
2319 blitter_check_saved_fb_state(ctx);
2320 blitter_disable_render_cond(ctx);
2321
2322 /* bind states */
2323 pipe->bind_blend_state(pipe, cbsurf ? ctx->blend[PIPE_MASK_RGBA][0] :
2324 ctx->blend[0][0]);
2325 pipe->bind_depth_stencil_alpha_state(pipe, dsa_stage);
2326 if (cbsurf)
2327 bind_fs_write_one_cbuf(ctx);
2328 else
2329 bind_fs_empty(ctx);
2330
2331 /* set a framebuffer state */
2332 fb_state.width = zsurf->width;
2333 fb_state.height = zsurf->height;
2334 fb_state.nr_cbufs = 1;
2335 if (cbsurf) {
2336 fb_state.cbufs[0] = cbsurf;
2337 fb_state.nr_cbufs = 1;
2338 } else {
2339 fb_state.cbufs[0] = NULL;
2340 fb_state.nr_cbufs = 0;
2341 }
2342 fb_state.zsbuf = zsurf;
2343 pipe->set_framebuffer_state(pipe, &fb_state);
2344 pipe->set_sample_mask(pipe, sample_mask);
2345
2346 blitter_set_common_draw_rect_state(ctx, false, false);
2347 blitter_set_dst_dimensions(ctx, zsurf->width, zsurf->height);
2348 blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_passthrough_pos,
2349 0, 0, zsurf->width, zsurf->height, depth,
2350 1, UTIL_BLITTER_ATTRIB_NONE, NULL);
2351
2352 util_blitter_restore_vertex_states(blitter);
2353 util_blitter_restore_fragment_states(blitter);
2354 util_blitter_restore_fb_state(blitter);
2355 util_blitter_restore_render_cond(blitter);
2356 util_blitter_unset_running_flag(blitter);
2357 }
2358
2359 void util_blitter_copy_buffer(struct blitter_context *blitter,
2360 struct pipe_resource *dst,
2361 unsigned dstx,
2362 struct pipe_resource *src,
2363 unsigned srcx,
2364 unsigned size)
2365 {
2366 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2367 struct pipe_context *pipe = ctx->base.pipe;
2368 struct pipe_vertex_buffer vb;
2369 struct pipe_stream_output_target *so_target;
2370 unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0};
2371
2372 if (srcx >= src->width0 ||
2373 dstx >= dst->width0) {
2374 return;
2375 }
2376 if (srcx + size > src->width0) {
2377 size = src->width0 - srcx;
2378 }
2379 if (dstx + size > dst->width0) {
2380 size = dst->width0 - dstx;
2381 }
2382
2383 /* Drivers not capable of Stream Out should not call this function
2384 * in the first place. */
2385 assert(ctx->has_stream_out);
2386
2387 /* Some alignment is required. */
2388 if (srcx % 4 != 0 || dstx % 4 != 0 || size % 4 != 0 ||
2389 !ctx->has_stream_out) {
2390 struct pipe_box box;
2391 u_box_1d(srcx, size, &box);
2392 util_resource_copy_region(pipe, dst, 0, dstx, 0, 0, src, 0, &box);
2393 return;
2394 }
2395
2396 util_blitter_set_running_flag(blitter);
2397 blitter_check_saved_vertex_states(ctx);
2398 blitter_disable_render_cond(ctx);
2399
2400 vb.is_user_buffer = false;
2401 vb.buffer.resource = src;
2402 vb.buffer_offset = srcx;
2403 vb.stride = 4;
2404
2405 pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
2406 pipe->bind_vertex_elements_state(pipe, ctx->velem_state_readbuf[0]);
2407 bind_vs_pos_only(ctx, 1);
2408 if (ctx->has_geometry_shader)
2409 pipe->bind_gs_state(pipe, NULL);
2410 if (ctx->has_tessellation) {
2411 pipe->bind_tcs_state(pipe, NULL);
2412 pipe->bind_tes_state(pipe, NULL);
2413 }
2414 pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state);
2415
2416 so_target = pipe->create_stream_output_target(pipe, dst, dstx, size);
2417 pipe->set_stream_output_targets(pipe, 1, &so_target, offsets);
2418
2419 util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
2420
2421 util_blitter_restore_vertex_states(blitter);
2422 util_blitter_restore_render_cond(blitter);
2423 util_blitter_unset_running_flag(blitter);
2424 pipe_so_target_reference(&so_target, NULL);
2425 }
2426
2427 void util_blitter_clear_buffer(struct blitter_context *blitter,
2428 struct pipe_resource *dst,
2429 unsigned offset, unsigned size,
2430 unsigned num_channels,
2431 const union pipe_color_union *clear_value)
2432 {
2433 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2434 struct pipe_context *pipe = ctx->base.pipe;
2435 struct pipe_vertex_buffer vb = {0};
2436 struct pipe_stream_output_target *so_target = NULL;
2437 unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0};
2438
2439 assert(num_channels >= 1);
2440 assert(num_channels <= 4);
2441
2442 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
2443 *
2444 * R600 uses this to initialize texture resources, so width0 might not be
2445 * what you think it is.
2446 */
2447
2448 /* Streamout is required. */
2449 if (!ctx->has_stream_out) {
2450 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2451 return;
2452 }
2453
2454 /* Some alignment is required. */
2455 if (offset % 4 != 0 || size % 4 != 0) {
2456 assert(!"Bad alignment in util_blitter_clear_buffer()");
2457 return;
2458 }
2459
2460 u_upload_data(pipe->stream_uploader, 0, num_channels*4, 4, clear_value,
2461 &vb.buffer_offset, &vb.buffer.resource);
2462 if (!vb.buffer.resource)
2463 goto out;
2464
2465 vb.stride = 0;
2466
2467 util_blitter_set_running_flag(blitter);
2468 blitter_check_saved_vertex_states(ctx);
2469 blitter_disable_render_cond(ctx);
2470
2471 pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
2472 pipe->bind_vertex_elements_state(pipe,
2473 ctx->velem_state_readbuf[num_channels-1]);
2474 bind_vs_pos_only(ctx, num_channels);
2475 if (ctx->has_geometry_shader)
2476 pipe->bind_gs_state(pipe, NULL);
2477 if (ctx->has_tessellation) {
2478 pipe->bind_tcs_state(pipe, NULL);
2479 pipe->bind_tes_state(pipe, NULL);
2480 }
2481 pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state);
2482
2483 so_target = pipe->create_stream_output_target(pipe, dst, offset, size);
2484 pipe->set_stream_output_targets(pipe, 1, &so_target, offsets);
2485
2486 util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
2487
2488 out:
2489 util_blitter_restore_vertex_states(blitter);
2490 util_blitter_restore_render_cond(blitter);
2491 util_blitter_unset_running_flag(blitter);
2492 pipe_so_target_reference(&so_target, NULL);
2493 pipe_resource_reference(&vb.buffer.resource, NULL);
2494 }
2495
2496 /* probably radeon specific */
2497 void util_blitter_custom_resolve_color(struct blitter_context *blitter,
2498 struct pipe_resource *dst,
2499 unsigned dst_level,
2500 unsigned dst_layer,
2501 struct pipe_resource *src,
2502 unsigned src_layer,
2503 unsigned sample_mask,
2504 void *custom_blend,
2505 enum pipe_format format)
2506 {
2507 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2508 struct pipe_context *pipe = ctx->base.pipe;
2509 struct pipe_framebuffer_state fb_state;
2510 struct pipe_surface *srcsurf, *dstsurf, surf_tmpl;
2511
2512 util_blitter_set_running_flag(blitter);
2513 blitter_check_saved_vertex_states(ctx);
2514 blitter_check_saved_fragment_states(ctx);
2515 blitter_disable_render_cond(ctx);
2516
2517 /* bind states */
2518 pipe->bind_blend_state(pipe, custom_blend);
2519 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2520 bind_fs_write_one_cbuf(ctx);
2521 pipe->set_sample_mask(pipe, sample_mask);
2522
2523 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
2524 surf_tmpl.format = format;
2525 surf_tmpl.u.tex.level = dst_level;
2526 surf_tmpl.u.tex.first_layer = dst_layer;
2527 surf_tmpl.u.tex.last_layer = dst_layer;
2528
2529 dstsurf = pipe->create_surface(pipe, dst, &surf_tmpl);
2530
2531 surf_tmpl.u.tex.level = 0;
2532 surf_tmpl.u.tex.first_layer = src_layer;
2533 surf_tmpl.u.tex.last_layer = src_layer;
2534
2535 srcsurf = pipe->create_surface(pipe, src, &surf_tmpl);
2536
2537 /* set a framebuffer state */
2538 fb_state.width = src->width0;
2539 fb_state.height = src->height0;
2540 fb_state.nr_cbufs = 2;
2541 fb_state.cbufs[0] = srcsurf;
2542 fb_state.cbufs[1] = dstsurf;
2543 fb_state.zsbuf = NULL;
2544 pipe->set_framebuffer_state(pipe, &fb_state);
2545
2546 blitter_set_common_draw_rect_state(ctx, false, false);
2547 blitter_set_dst_dimensions(ctx, src->width0, src->height0);
2548 blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_passthrough_pos,
2549 0, 0, src->width0, src->height0,
2550 0, 1, UTIL_BLITTER_ATTRIB_NONE, NULL);
2551 util_blitter_restore_fb_state(blitter);
2552 util_blitter_restore_vertex_states(blitter);
2553 util_blitter_restore_fragment_states(blitter);
2554 util_blitter_restore_render_cond(blitter);
2555 util_blitter_unset_running_flag(blitter);
2556
2557 pipe_surface_reference(&srcsurf, NULL);
2558 pipe_surface_reference(&dstsurf, NULL);
2559 }
2560
2561 void util_blitter_custom_color(struct blitter_context *blitter,
2562 struct pipe_surface *dstsurf,
2563 void *custom_blend)
2564 {
2565 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2566 struct pipe_context *pipe = ctx->base.pipe;
2567 struct pipe_framebuffer_state fb_state;
2568
2569 assert(dstsurf->texture);
2570 if (!dstsurf->texture)
2571 return;
2572
2573 /* check the saved state */
2574 util_blitter_set_running_flag(blitter);
2575 blitter_check_saved_vertex_states(ctx);
2576 blitter_check_saved_fragment_states(ctx);
2577 blitter_check_saved_fb_state(ctx);
2578 blitter_disable_render_cond(ctx);
2579
2580 /* bind states */
2581 pipe->bind_blend_state(pipe, custom_blend ? custom_blend
2582 : ctx->blend[PIPE_MASK_RGBA][0]);
2583 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2584 bind_fs_write_one_cbuf(ctx);
2585 pipe->set_sample_mask(pipe, (1ull << MAX2(1, dstsurf->texture->nr_samples)) - 1);
2586
2587 /* set a framebuffer state */
2588 fb_state.width = dstsurf->width;
2589 fb_state.height = dstsurf->height;
2590 fb_state.nr_cbufs = 1;
2591 fb_state.cbufs[0] = dstsurf;
2592 fb_state.zsbuf = 0;
2593 pipe->set_framebuffer_state(pipe, &fb_state);
2594 pipe->set_sample_mask(pipe, ~0);
2595
2596 blitter_set_common_draw_rect_state(ctx, false, false);
2597 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
2598 blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_passthrough_pos,
2599 0, 0, dstsurf->width, dstsurf->height,
2600 0, 1, UTIL_BLITTER_ATTRIB_NONE, NULL);
2601
2602 util_blitter_restore_vertex_states(blitter);
2603 util_blitter_restore_fragment_states(blitter);
2604 util_blitter_restore_fb_state(blitter);
2605 util_blitter_restore_render_cond(blitter);
2606 util_blitter_unset_running_flag(blitter);
2607 }
2608
2609 static void *get_custom_vs(struct blitter_context *blitter)
2610 {
2611 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2612
2613 return ctx->custom_vs;
2614 }
2615
2616 /**
2617 * Performs a custom blit to the destination surface, using the VS and FS
2618 * provided.
2619 *
2620 * Used by vc4 for the 8-bit linear-to-tiled blit.
2621 */
2622 void util_blitter_custom_shader(struct blitter_context *blitter,
2623 struct pipe_surface *dstsurf,
2624 void *custom_vs, void *custom_fs)
2625 {
2626 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2627 struct pipe_context *pipe = ctx->base.pipe;
2628 struct pipe_framebuffer_state fb_state;
2629
2630 ctx->custom_vs = custom_vs;
2631
2632 assert(dstsurf->texture);
2633 if (!dstsurf->texture)
2634 return;
2635
2636 /* check the saved state */
2637 util_blitter_set_running_flag(blitter);
2638 blitter_check_saved_vertex_states(ctx);
2639 blitter_check_saved_fragment_states(ctx);
2640 blitter_check_saved_fb_state(ctx);
2641 blitter_disable_render_cond(ctx);
2642
2643 /* bind states */
2644 pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA][0]);
2645 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2646 pipe->bind_fs_state(pipe, custom_fs);
2647 pipe->set_sample_mask(pipe, (1ull << MAX2(1, dstsurf->texture->nr_samples)) - 1);
2648
2649 /* set a framebuffer state */
2650 fb_state.width = dstsurf->width;
2651 fb_state.height = dstsurf->height;
2652 fb_state.nr_cbufs = 1;
2653 fb_state.cbufs[0] = dstsurf;
2654 fb_state.zsbuf = 0;
2655 pipe->set_framebuffer_state(pipe, &fb_state);
2656 pipe->set_sample_mask(pipe, ~0);
2657
2658 blitter_set_common_draw_rect_state(ctx, false, false);
2659 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
2660 blitter->draw_rectangle(blitter, ctx->velem_state, get_custom_vs,
2661 0, 0, dstsurf->width, dstsurf->height,
2662 0, 1, UTIL_BLITTER_ATTRIB_NONE, NULL);
2663
2664 util_blitter_restore_vertex_states(blitter);
2665 util_blitter_restore_fragment_states(blitter);
2666 util_blitter_restore_fb_state(blitter);
2667 util_blitter_restore_render_cond(blitter);
2668 util_blitter_unset_running_flag(blitter);
2669 }