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