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