gallium/u_blitter: implement copying from ZS to color and vice versa
[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 {
1456 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1457 struct pipe_context *pipe = ctx->base.pipe;
1458 struct pipe_stencil_ref sr = { { 0 } };
1459
1460 assert(ctx->has_layered || num_layers <= 1);
1461
1462 util_blitter_common_clear_setup(blitter, width, height, clear_buffers,
1463 custom_blend, custom_dsa);
1464
1465 sr.ref_value[0] = stencil & 0xff;
1466 pipe->set_stencil_ref(pipe, &sr);
1467
1468 bind_fs_write_all_cbufs(ctx);
1469
1470 union blitter_attrib attrib;
1471 memcpy(attrib.color, color->ui, sizeof(color->ui));
1472
1473 bool pass_generic = (clear_buffers & PIPE_CLEAR_COLOR) != 0;
1474 enum blitter_attrib_type type = pass_generic ? UTIL_BLITTER_ATTRIB_COLOR :
1475 UTIL_BLITTER_ATTRIB_NONE;
1476
1477 if (num_layers > 1 && ctx->has_layered) {
1478 blitter_get_vs_func get_vs = get_vs_layered;
1479
1480 blitter_set_common_draw_rect_state(ctx, false, false);
1481 blitter->draw_rectangle(blitter, ctx->velem_state, get_vs,
1482 0, 0, width, height,
1483 (float) depth, num_layers, type, &attrib);
1484 } else {
1485 blitter_get_vs_func get_vs;
1486
1487 if (pass_generic)
1488 get_vs = get_vs_passthrough_pos_generic;
1489 else
1490 get_vs = get_vs_passthrough_pos;
1491
1492 blitter_set_common_draw_rect_state(ctx, false, false);
1493 blitter->draw_rectangle(blitter, ctx->velem_state, get_vs,
1494 0, 0, width, height,
1495 (float) depth, 1, type, &attrib);
1496 }
1497
1498 util_blitter_restore_vertex_states(blitter);
1499 util_blitter_restore_fragment_states(blitter);
1500 util_blitter_restore_render_cond(blitter);
1501 util_blitter_unset_running_flag(blitter);
1502 }
1503
1504 void util_blitter_clear(struct blitter_context *blitter,
1505 unsigned width, unsigned height, unsigned num_layers,
1506 unsigned clear_buffers,
1507 const union pipe_color_union *color,
1508 double depth, unsigned stencil)
1509 {
1510 util_blitter_clear_custom(blitter, width, height, num_layers,
1511 clear_buffers, color, depth, stencil,
1512 NULL, NULL);
1513 }
1514
1515 void util_blitter_custom_clear_depth(struct blitter_context *blitter,
1516 unsigned width, unsigned height,
1517 double depth, void *custom_dsa)
1518 {
1519 static const union pipe_color_union color;
1520 util_blitter_clear_custom(blitter, width, height, 0, 0, &color, depth, 0,
1521 NULL, custom_dsa);
1522 }
1523
1524 void util_blitter_default_dst_texture(struct pipe_surface *dst_templ,
1525 struct pipe_resource *dst,
1526 unsigned dstlevel,
1527 unsigned dstz)
1528 {
1529 memset(dst_templ, 0, sizeof(*dst_templ));
1530 dst_templ->format = util_format_linear(dst->format);
1531 dst_templ->u.tex.level = dstlevel;
1532 dst_templ->u.tex.first_layer = dstz;
1533 dst_templ->u.tex.last_layer = dstz;
1534 }
1535
1536 static struct pipe_surface *
1537 util_blitter_get_next_surface_layer(struct pipe_context *pipe,
1538 struct pipe_surface *surf)
1539 {
1540 struct pipe_surface dst_templ;
1541
1542 memset(&dst_templ, 0, sizeof(dst_templ));
1543 dst_templ.format = surf->format;
1544 dst_templ.u.tex.level = surf->u.tex.level;
1545 dst_templ.u.tex.first_layer = surf->u.tex.first_layer + 1;
1546 dst_templ.u.tex.last_layer = surf->u.tex.last_layer + 1;
1547
1548 return pipe->create_surface(pipe, surf->texture, &dst_templ);
1549 }
1550
1551 void util_blitter_default_src_texture(struct blitter_context *blitter,
1552 struct pipe_sampler_view *src_templ,
1553 struct pipe_resource *src,
1554 unsigned srclevel)
1555 {
1556 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1557
1558 memset(src_templ, 0, sizeof(*src_templ));
1559
1560 if (ctx->cube_as_2darray &&
1561 (src->target == PIPE_TEXTURE_CUBE ||
1562 src->target == PIPE_TEXTURE_CUBE_ARRAY))
1563 src_templ->target = PIPE_TEXTURE_2D_ARRAY;
1564 else
1565 src_templ->target = src->target;
1566
1567 src_templ->format = util_format_linear(src->format);
1568 src_templ->u.tex.first_level = srclevel;
1569 src_templ->u.tex.last_level = srclevel;
1570 src_templ->u.tex.first_layer = 0;
1571 src_templ->u.tex.last_layer =
1572 src->target == PIPE_TEXTURE_3D ? u_minify(src->depth0, srclevel) - 1
1573 : (unsigned)(src->array_size - 1);
1574 src_templ->swizzle_r = PIPE_SWIZZLE_X;
1575 src_templ->swizzle_g = PIPE_SWIZZLE_Y;
1576 src_templ->swizzle_b = PIPE_SWIZZLE_Z;
1577 src_templ->swizzle_a = PIPE_SWIZZLE_W;
1578 }
1579
1580 static bool is_blit_generic_supported(struct blitter_context *blitter,
1581 const struct pipe_resource *dst,
1582 enum pipe_format dst_format,
1583 const struct pipe_resource *src,
1584 enum pipe_format src_format,
1585 unsigned mask)
1586 {
1587 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1588 struct pipe_screen *screen = ctx->base.pipe->screen;
1589
1590 if (dst) {
1591 unsigned bind;
1592 const struct util_format_description *desc =
1593 util_format_description(dst_format);
1594 bool dst_has_stencil = util_format_has_stencil(desc);
1595
1596 /* Stencil export must be supported for stencil copy. */
1597 if ((mask & PIPE_MASK_S) && dst_has_stencil &&
1598 !ctx->has_stencil_export) {
1599 return false;
1600 }
1601
1602 if (dst_has_stencil || util_format_has_depth(desc))
1603 bind = PIPE_BIND_DEPTH_STENCIL;
1604 else
1605 bind = PIPE_BIND_RENDER_TARGET;
1606
1607 if (!screen->is_format_supported(screen, dst_format, dst->target,
1608 dst->nr_samples, dst->nr_storage_samples,
1609 bind)) {
1610 return false;
1611 }
1612 }
1613
1614 if (src) {
1615 if (src->nr_samples > 1 && !ctx->has_texture_multisample) {
1616 return false;
1617 }
1618
1619 if (!screen->is_format_supported(screen, src_format, src->target,
1620 src->nr_samples, src->nr_storage_samples,
1621 PIPE_BIND_SAMPLER_VIEW)) {
1622 return false;
1623 }
1624
1625 /* Check stencil sampler support for stencil copy. */
1626 if (mask & PIPE_MASK_S) {
1627 if (util_format_has_stencil(util_format_description(src_format))) {
1628 enum pipe_format stencil_format =
1629 util_format_stencil_only(src_format);
1630 assert(stencil_format != PIPE_FORMAT_NONE);
1631
1632 if (stencil_format != src_format &&
1633 !screen->is_format_supported(screen, stencil_format,
1634 src->target, src->nr_samples,
1635 src->nr_storage_samples,
1636 PIPE_BIND_SAMPLER_VIEW)) {
1637 return false;
1638 }
1639 }
1640 }
1641 }
1642
1643 return true;
1644 }
1645
1646 bool util_blitter_is_copy_supported(struct blitter_context *blitter,
1647 const struct pipe_resource *dst,
1648 const struct pipe_resource *src)
1649 {
1650 return is_blit_generic_supported(blitter, dst, dst->format,
1651 src, src->format, PIPE_MASK_RGBAZS);
1652 }
1653
1654 bool util_blitter_is_blit_supported(struct blitter_context *blitter,
1655 const struct pipe_blit_info *info)
1656 {
1657 return is_blit_generic_supported(blitter,
1658 info->dst.resource, info->dst.format,
1659 info->src.resource, info->src.format,
1660 info->mask);
1661 }
1662
1663 void util_blitter_copy_texture(struct blitter_context *blitter,
1664 struct pipe_resource *dst,
1665 unsigned dst_level,
1666 unsigned dstx, unsigned dsty, unsigned dstz,
1667 struct pipe_resource *src,
1668 unsigned src_level,
1669 const struct pipe_box *srcbox)
1670 {
1671 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1672 struct pipe_context *pipe = ctx->base.pipe;
1673 struct pipe_surface *dst_view, dst_templ;
1674 struct pipe_sampler_view src_templ, *src_view;
1675 struct pipe_box dstbox;
1676
1677 assert(dst && src);
1678 assert(src->target < PIPE_MAX_TEXTURE_TYPES);
1679
1680 u_box_3d(dstx, dsty, dstz, abs(srcbox->width), abs(srcbox->height),
1681 abs(srcbox->depth), &dstbox);
1682
1683 /* Initialize the surface. */
1684 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
1685 dst_view = pipe->create_surface(pipe, dst, &dst_templ);
1686
1687 /* Initialize the sampler view. */
1688 util_blitter_default_src_texture(blitter, &src_templ, src, src_level);
1689 src_view = pipe->create_sampler_view(pipe, src, &src_templ);
1690
1691 /* Copy. */
1692 util_blitter_blit_generic(blitter, dst_view, &dstbox,
1693 src_view, srcbox, src->width0, src->height0,
1694 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,
1695 false);
1696
1697 pipe_surface_reference(&dst_view, NULL);
1698 pipe_sampler_view_reference(&src_view, NULL);
1699 }
1700
1701 static void
1702 blitter_draw_tex(struct blitter_context_priv *ctx,
1703 int dst_x1, int dst_y1, int dst_x2, int dst_y2,
1704 struct pipe_sampler_view *src,
1705 unsigned src_width0, unsigned src_height0,
1706 int src_x1, int src_y1, int src_x2, int src_y2,
1707 float layer, unsigned sample,
1708 bool uses_txf, enum blitter_attrib_type type)
1709 {
1710 union blitter_attrib coord;
1711 blitter_get_vs_func get_vs = get_vs_passthrough_pos_generic;
1712
1713 get_texcoords(src, src_width0, src_height0,
1714 src_x1, src_y1, src_x2, src_y2, layer, sample,
1715 uses_txf, &coord);
1716
1717 if (src->target == PIPE_TEXTURE_CUBE ||
1718 src->target == PIPE_TEXTURE_CUBE_ARRAY) {
1719 float face_coord[4][2];
1720
1721 set_texcoords_in_vertices(&coord, &face_coord[0][0], 2);
1722 util_map_texcoords2d_onto_cubemap((unsigned)layer % 6,
1723 /* pointer, stride in floats */
1724 &face_coord[0][0], 2,
1725 &ctx->vertices[0][1][0], 8,
1726 false);
1727 for (unsigned i = 0; i < 4; i++)
1728 ctx->vertices[i][1][3] = coord.texcoord.w;
1729
1730 /* Cubemaps don't use draw_rectangle. */
1731 blitter_draw(ctx, ctx->velem_state, get_vs,
1732 dst_x1, dst_y1, dst_x2, dst_y2, 0, 1);
1733 } else {
1734 ctx->base.draw_rectangle(&ctx->base, ctx->velem_state, get_vs,
1735 dst_x1, dst_y1, dst_x2, dst_y2,
1736 0, 1, type, &coord);
1737 }
1738 }
1739
1740 static void do_blits(struct blitter_context_priv *ctx,
1741 struct pipe_surface *dst,
1742 const struct pipe_box *dstbox,
1743 struct pipe_sampler_view *src,
1744 unsigned src_width0,
1745 unsigned src_height0,
1746 const struct pipe_box *srcbox,
1747 bool is_zsbuf,
1748 bool uses_txf)
1749 {
1750 struct pipe_context *pipe = ctx->base.pipe;
1751 unsigned src_samples = src->texture->nr_samples;
1752 unsigned dst_samples = dst->texture->nr_samples;
1753 enum pipe_texture_target src_target = src->target;
1754 struct pipe_framebuffer_state fb_state = {0};
1755
1756 /* Initialize framebuffer state. */
1757 fb_state.width = dst->width;
1758 fb_state.height = dst->height;
1759 fb_state.nr_cbufs = is_zsbuf ? 0 : 1;
1760
1761 blitter_set_dst_dimensions(ctx, fb_state.width, fb_state.height);
1762
1763 if ((src_target == PIPE_TEXTURE_1D ||
1764 src_target == PIPE_TEXTURE_2D ||
1765 src_target == PIPE_TEXTURE_RECT) &&
1766 src_samples <= 1) {
1767 /* Set framebuffer state. */
1768 if (is_zsbuf) {
1769 fb_state.zsbuf = dst;
1770 } else {
1771 fb_state.cbufs[0] = dst;
1772 }
1773 pipe->set_framebuffer_state(pipe, &fb_state);
1774
1775 /* Draw. */
1776 pipe->set_sample_mask(pipe, ~0);
1777 blitter_draw_tex(ctx, dstbox->x, dstbox->y,
1778 dstbox->x + dstbox->width,
1779 dstbox->y + dstbox->height,
1780 src, src_width0, src_height0, srcbox->x, srcbox->y,
1781 srcbox->x + srcbox->width, srcbox->y + srcbox->height,
1782 0, 0, uses_txf, UTIL_BLITTER_ATTRIB_TEXCOORD_XY);
1783 } else {
1784 /* Draw the quad with the generic codepath. */
1785 int dst_z;
1786 for (dst_z = 0; dst_z < dstbox->depth; dst_z++) {
1787 struct pipe_surface *old;
1788 float dst2src_scale = srcbox->depth / (float)dstbox->depth;
1789
1790 /* Scale Z properly if the blit is scaled.
1791 *
1792 * When downscaling, we want the coordinates centered, so that
1793 * mipmapping works for 3D textures. For example, when generating
1794 * a 4x4x4 level, this wouldn't average the pixels:
1795 *
1796 * src Z: 0 1 2 3 4 5 6 7
1797 * dst Z: 0 1 2 3
1798 *
1799 * Because the pixels are not centered below the pixels of the higher
1800 * level. Therefore, we want this:
1801 * src Z: 0 1 2 3 4 5 6 7
1802 * dst Z: 0 1 2 3
1803 *
1804 * dst_offset defines the offset needed for centering the pixels and
1805 * it works with any scaling (not just 2x).
1806 */
1807 float dst_offset = ((srcbox->depth - 1) -
1808 (dstbox->depth - 1) * dst2src_scale) * 0.5;
1809 float src_z = (dst_z + dst_offset) * dst2src_scale;
1810
1811 /* Set framebuffer state. */
1812 if (is_zsbuf) {
1813 fb_state.zsbuf = dst;
1814 } else {
1815 fb_state.cbufs[0] = dst;
1816 }
1817 pipe->set_framebuffer_state(pipe, &fb_state);
1818
1819 /* See if we need to blit a multisample or singlesample buffer. */
1820 if (src_samples == dst_samples && dst_samples > 1) {
1821 /* MSAA copy. */
1822 unsigned i, max_sample = dst_samples - 1;
1823
1824 for (i = 0; i <= max_sample; i++) {
1825 pipe->set_sample_mask(pipe, 1 << i);
1826 blitter_draw_tex(ctx, dstbox->x, dstbox->y,
1827 dstbox->x + dstbox->width,
1828 dstbox->y + dstbox->height,
1829 src, src_width0, src_height0,
1830 srcbox->x, srcbox->y,
1831 srcbox->x + srcbox->width,
1832 srcbox->y + srcbox->height,
1833 srcbox->z + src_z, i, uses_txf,
1834 UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW);
1835 }
1836 } else {
1837 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1838 pipe->set_sample_mask(pipe, ~0);
1839 blitter_draw_tex(ctx, dstbox->x, dstbox->y,
1840 dstbox->x + dstbox->width,
1841 dstbox->y + dstbox->height,
1842 src, src_width0, src_height0,
1843 srcbox->x, srcbox->y,
1844 srcbox->x + srcbox->width,
1845 srcbox->y + srcbox->height,
1846 srcbox->z + src_z, 0, uses_txf,
1847 UTIL_BLITTER_ATTRIB_TEXCOORD_XYZW);
1848 }
1849
1850 /* Get the next surface or (if this is the last iteration)
1851 * just unreference the last one. */
1852 old = dst;
1853 if (dst_z < dstbox->depth-1) {
1854 dst = util_blitter_get_next_surface_layer(ctx->base.pipe, dst);
1855 }
1856 if (dst_z) {
1857 pipe_surface_reference(&old, NULL);
1858 }
1859 }
1860 }
1861 }
1862
1863 void util_blitter_blit_generic(struct blitter_context *blitter,
1864 struct pipe_surface *dst,
1865 const struct pipe_box *dstbox,
1866 struct pipe_sampler_view *src,
1867 const struct pipe_box *srcbox,
1868 unsigned src_width0, unsigned src_height0,
1869 unsigned mask, unsigned filter,
1870 const struct pipe_scissor_state *scissor,
1871 bool alpha_blend)
1872 {
1873 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1874 struct pipe_context *pipe = ctx->base.pipe;
1875 enum pipe_texture_target src_target = src->target;
1876 unsigned src_samples = src->texture->nr_samples;
1877 unsigned dst_samples = dst->texture->nr_samples;
1878 void *sampler_state;
1879 const struct util_format_description *src_desc =
1880 util_format_description(src->format);
1881 const struct util_format_description *dst_desc =
1882 util_format_description(dst->format);
1883
1884 bool src_has_color = src_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS;
1885 bool src_has_depth = util_format_has_depth(src_desc);
1886 bool src_has_stencil = util_format_has_stencil(src_desc);
1887
1888 bool dst_has_color = mask & PIPE_MASK_RGBA &&
1889 dst_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS;
1890 bool dst_has_depth = mask & PIPE_MASK_Z &&
1891 util_format_has_depth(dst_desc);
1892 bool dst_has_stencil = ctx->has_stencil_export &&
1893 mask & PIPE_MASK_S &&
1894 util_format_has_stencil(dst_desc);
1895
1896 /* Return if there is nothing to do. */
1897 if (!dst_has_color && !dst_has_depth && !dst_has_stencil) {
1898 return;
1899 }
1900
1901 bool is_scaled = dstbox->width != abs(srcbox->width) ||
1902 dstbox->height != abs(srcbox->height);
1903
1904 if (src_has_stencil || !is_scaled)
1905 filter = PIPE_TEX_FILTER_NEAREST;
1906
1907 bool use_txf = false;
1908
1909 /* Don't support scaled blits. The TXF shader uses F2I for rounding. */
1910 if (ctx->has_txf &&
1911 !is_scaled &&
1912 filter == PIPE_TEX_FILTER_NEAREST &&
1913 src->target != PIPE_TEXTURE_CUBE &&
1914 src->target != PIPE_TEXTURE_CUBE_ARRAY) {
1915 int src_width = u_minify(src_width0, src->u.tex.first_level);
1916 int src_height = u_minify(src_height0, src->u.tex.first_level);
1917 int src_depth = src->u.tex.last_layer + 1;
1918 struct pipe_box box = *srcbox;
1919
1920 /* Eliminate negative width/height/depth. */
1921 if (box.width < 0) {
1922 box.x += box.width;
1923 box.width *= -1;
1924 }
1925 if (box.height < 0) {
1926 box.y += box.height;
1927 box.height *= -1;
1928 }
1929 if (box.depth < 0) {
1930 box.z += box.depth;
1931 box.depth *= -1;
1932 }
1933
1934 /* See if srcbox is in bounds. TXF doesn't clamp the coordinates. */
1935 use_txf =
1936 box.x >= 0 && box.x < src_width &&
1937 box.y >= 0 && box.y < src_height &&
1938 box.z >= 0 && box.z < src_depth &&
1939 box.x + box.width > 0 && box.x + box.width <= src_width &&
1940 box.y + box.height > 0 && box.y + box.height <= src_height &&
1941 box.z + box.depth > 0 && box.z + box.depth <= src_depth;
1942 }
1943
1944 /* Check whether the states are properly saved. */
1945 util_blitter_set_running_flag(blitter);
1946 blitter_check_saved_vertex_states(ctx);
1947 blitter_check_saved_fragment_states(ctx);
1948 blitter_check_saved_textures(ctx);
1949 blitter_check_saved_fb_state(ctx);
1950 blitter_disable_render_cond(ctx);
1951
1952 /* Blend, DSA, fragment shader. */
1953 if (dst_has_depth && dst_has_stencil) {
1954 pipe->bind_blend_state(pipe, ctx->blend[0][0]);
1955 pipe->bind_depth_stencil_alpha_state(pipe,
1956 ctx->dsa_write_depth_stencil);
1957 if (src_has_color) {
1958 assert(use_txf);
1959 ctx->bind_fs_state(pipe,
1960 blitter_get_fs_pack_color_zs(ctx, src_target,
1961 src_samples, dst->format, false));
1962 } else {
1963 ctx->bind_fs_state(pipe,
1964 blitter_get_fs_texfetch_depthstencil(ctx, src_target,
1965 src_samples, use_txf));
1966 }
1967 } else if (dst_has_depth) {
1968 pipe->bind_blend_state(pipe, ctx->blend[0][0]);
1969 pipe->bind_depth_stencil_alpha_state(pipe,
1970 ctx->dsa_write_depth_keep_stencil);
1971 if (src_has_color &&
1972 (src->format == PIPE_FORMAT_R32_UINT ||
1973 src->format == PIPE_FORMAT_R32G32_UINT)) {
1974 assert(use_txf);
1975 ctx->bind_fs_state(pipe,
1976 blitter_get_fs_pack_color_zs(ctx, src_target,
1977 src_samples, dst->format, false));
1978 } else {
1979 ctx->bind_fs_state(pipe,
1980 blitter_get_fs_texfetch_depth(ctx, src_target,
1981 src_samples, use_txf));
1982 }
1983 } else if (dst_has_stencil) {
1984 pipe->bind_blend_state(pipe, ctx->blend[0][0]);
1985 pipe->bind_depth_stencil_alpha_state(pipe,
1986 ctx->dsa_keep_depth_write_stencil);
1987
1988 assert(src_has_stencil); /* unpacking from color is unsupported */
1989 ctx->bind_fs_state(pipe,
1990 blitter_get_fs_texfetch_stencil(ctx, src_target,
1991 src_samples, use_txf));
1992 } else {
1993 unsigned colormask = mask & PIPE_MASK_RGBA;
1994
1995 pipe->bind_blend_state(pipe, ctx->blend[colormask][alpha_blend]);
1996 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1997
1998 if (src_has_depth &&
1999 (dst->format == PIPE_FORMAT_R32_UINT ||
2000 dst->format == PIPE_FORMAT_R32G32_UINT)) {
2001 assert(use_txf);
2002 ctx->bind_fs_state(pipe,
2003 blitter_get_fs_pack_color_zs(ctx, src_target,
2004 src_samples, src->format, true));
2005 } else {
2006 ctx->bind_fs_state(pipe,
2007 blitter_get_fs_texfetch_col(ctx, src->format, dst->format, src_target,
2008 src_samples, dst_samples, filter,
2009 use_txf));
2010 }
2011 }
2012
2013 /* Set the linear filter only for scaled color non-MSAA blits. */
2014 if (filter == PIPE_TEX_FILTER_LINEAR) {
2015 if (src_target == PIPE_TEXTURE_RECT) {
2016 sampler_state = ctx->sampler_state_rect_linear;
2017 } else {
2018 sampler_state = ctx->sampler_state_linear;
2019 }
2020 } else {
2021 if (src_target == PIPE_TEXTURE_RECT) {
2022 sampler_state = ctx->sampler_state_rect;
2023 } else {
2024 sampler_state = ctx->sampler_state;
2025 }
2026 }
2027
2028 /* Set samplers. */
2029 if (src_has_depth && src_has_stencil &&
2030 (dst_has_color || (dst_has_depth && dst_has_stencil))) {
2031 /* Setup two samplers, one for depth and the other one for stencil. */
2032 struct pipe_sampler_view templ;
2033 struct pipe_sampler_view *views[2];
2034 void *samplers[2] = {sampler_state, sampler_state};
2035
2036 templ = *src;
2037 templ.format = util_format_stencil_only(templ.format);
2038 assert(templ.format != PIPE_FORMAT_NONE);
2039
2040 views[0] = src;
2041 views[1] = pipe->create_sampler_view(pipe, src->texture, &templ);
2042
2043 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 2, views);
2044 pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0, 2, samplers);
2045
2046 pipe_sampler_view_reference(&views[1], NULL);
2047 } else if (src_has_stencil && dst_has_stencil) {
2048 /* Set a stencil-only sampler view for it not to sample depth instead. */
2049 struct pipe_sampler_view templ;
2050 struct pipe_sampler_view *view;
2051
2052 templ = *src;
2053 templ.format = util_format_stencil_only(templ.format);
2054 assert(templ.format != PIPE_FORMAT_NONE);
2055
2056 view = pipe->create_sampler_view(pipe, src->texture, &templ);
2057
2058 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &view);
2059 pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
2060 0, 1, &sampler_state);
2061
2062 pipe_sampler_view_reference(&view, NULL);
2063 } else {
2064 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &src);
2065 pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
2066 0, 1, &sampler_state);
2067 }
2068
2069 if (scissor) {
2070 pipe->set_scissor_states(pipe, 0, 1, scissor);
2071 }
2072
2073 blitter_set_common_draw_rect_state(ctx, scissor != NULL, dst_samples > 1);
2074
2075 do_blits(ctx, dst, dstbox, src, src_width0, src_height0,
2076 srcbox, dst_has_depth || dst_has_stencil, use_txf);
2077
2078 util_blitter_restore_vertex_states(blitter);
2079 util_blitter_restore_fragment_states(blitter);
2080 util_blitter_restore_textures(blitter);
2081 util_blitter_restore_fb_state(blitter);
2082 if (scissor) {
2083 pipe->set_scissor_states(pipe, 0, 1, &ctx->base.saved_scissor);
2084 }
2085 util_blitter_restore_render_cond(blitter);
2086 util_blitter_unset_running_flag(blitter);
2087 }
2088
2089 void
2090 util_blitter_blit(struct blitter_context *blitter,
2091 const struct pipe_blit_info *info)
2092 {
2093 struct pipe_resource *dst = info->dst.resource;
2094 struct pipe_resource *src = info->src.resource;
2095 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2096 struct pipe_context *pipe = ctx->base.pipe;
2097 struct pipe_surface *dst_view, dst_templ;
2098 struct pipe_sampler_view src_templ, *src_view;
2099
2100 /* Initialize the surface. */
2101 util_blitter_default_dst_texture(&dst_templ, dst, info->dst.level,
2102 info->dst.box.z);
2103 dst_templ.format = info->dst.format;
2104 dst_view = pipe->create_surface(pipe, dst, &dst_templ);
2105
2106 /* Initialize the sampler view. */
2107 util_blitter_default_src_texture(blitter, &src_templ, src, info->src.level);
2108 src_templ.format = info->src.format;
2109 src_view = pipe->create_sampler_view(pipe, src, &src_templ);
2110
2111 /* Copy. */
2112 util_blitter_blit_generic(blitter, dst_view, &info->dst.box,
2113 src_view, &info->src.box, src->width0, src->height0,
2114 info->mask, info->filter,
2115 info->scissor_enable ? &info->scissor : NULL,
2116 info->alpha_blend);
2117
2118 pipe_surface_reference(&dst_view, NULL);
2119 pipe_sampler_view_reference(&src_view, NULL);
2120 }
2121
2122 void util_blitter_generate_mipmap(struct blitter_context *blitter,
2123 struct pipe_resource *tex,
2124 enum pipe_format format,
2125 unsigned base_level, unsigned last_level,
2126 unsigned first_layer, unsigned last_layer)
2127 {
2128 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2129 struct pipe_context *pipe = ctx->base.pipe;
2130 struct pipe_surface dst_templ, *dst_view;
2131 struct pipe_sampler_view src_templ, *src_view;
2132 bool is_depth;
2133 void *sampler_state;
2134 const struct util_format_description *desc =
2135 util_format_description(format);
2136 unsigned src_level;
2137 unsigned target = tex->target;
2138
2139 if (ctx->cube_as_2darray &&
2140 (target == PIPE_TEXTURE_CUBE || target == PIPE_TEXTURE_CUBE_ARRAY))
2141 target = PIPE_TEXTURE_2D_ARRAY;
2142
2143 assert(tex->nr_samples <= 1);
2144 /* Disallow stencil formats without depth. */
2145 assert(!util_format_has_stencil(desc) || util_format_has_depth(desc));
2146
2147 is_depth = desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS;
2148
2149 /* Check whether the states are properly saved. */
2150 util_blitter_set_running_flag(blitter);
2151 blitter_check_saved_vertex_states(ctx);
2152 blitter_check_saved_fragment_states(ctx);
2153 blitter_check_saved_textures(ctx);
2154 blitter_check_saved_fb_state(ctx);
2155 blitter_disable_render_cond(ctx);
2156
2157 /* Set states. */
2158 if (is_depth) {
2159 pipe->bind_blend_state(pipe, ctx->blend[0][0]);
2160 pipe->bind_depth_stencil_alpha_state(pipe,
2161 ctx->dsa_write_depth_keep_stencil);
2162 ctx->bind_fs_state(pipe,
2163 blitter_get_fs_texfetch_depth(ctx, target, 1, false));
2164 } else {
2165 pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA][0]);
2166 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2167 ctx->bind_fs_state(pipe,
2168 blitter_get_fs_texfetch_col(ctx, tex->format, tex->format, target,
2169 1, 1, PIPE_TEX_FILTER_LINEAR, false));
2170 }
2171
2172 if (target == PIPE_TEXTURE_RECT) {
2173 sampler_state = ctx->sampler_state_rect_linear;
2174 } else {
2175 sampler_state = ctx->sampler_state_linear;
2176 }
2177 pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
2178 0, 1, &sampler_state);
2179
2180 blitter_set_common_draw_rect_state(ctx, false, false);
2181
2182 for (src_level = base_level; src_level < last_level; src_level++) {
2183 struct pipe_box dstbox = {0}, srcbox = {0};
2184 unsigned dst_level = src_level + 1;
2185
2186 dstbox.width = u_minify(tex->width0, dst_level);
2187 dstbox.height = u_minify(tex->height0, dst_level);
2188
2189 srcbox.width = u_minify(tex->width0, src_level);
2190 srcbox.height = u_minify(tex->height0, src_level);
2191
2192 if (target == PIPE_TEXTURE_3D) {
2193 dstbox.depth = util_num_layers(tex, dst_level);
2194 srcbox.depth = util_num_layers(tex, src_level);
2195 } else {
2196 dstbox.z = srcbox.z = first_layer;
2197 dstbox.depth = srcbox.depth = last_layer - first_layer + 1;
2198 }
2199
2200 /* Initialize the surface. */
2201 util_blitter_default_dst_texture(&dst_templ, tex, dst_level,
2202 first_layer);
2203 dst_templ.format = format;
2204 dst_view = pipe->create_surface(pipe, tex, &dst_templ);
2205
2206 /* Initialize the sampler view. */
2207 util_blitter_default_src_texture(blitter, &src_templ, tex, src_level);
2208 src_templ.format = format;
2209 src_view = pipe->create_sampler_view(pipe, tex, &src_templ);
2210
2211 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &src_view);
2212
2213 do_blits(ctx, dst_view, &dstbox, src_view, tex->width0, tex->height0,
2214 &srcbox, is_depth, false);
2215
2216 pipe_surface_reference(&dst_view, NULL);
2217 pipe_sampler_view_reference(&src_view, NULL);
2218 }
2219
2220 util_blitter_restore_vertex_states(blitter);
2221 util_blitter_restore_fragment_states(blitter);
2222 util_blitter_restore_textures(blitter);
2223 util_blitter_restore_fb_state(blitter);
2224 util_blitter_restore_render_cond(blitter);
2225 util_blitter_unset_running_flag(blitter);
2226 }
2227
2228 /* Clear a region of a color surface to a constant value. */
2229 void util_blitter_clear_render_target(struct blitter_context *blitter,
2230 struct pipe_surface *dstsurf,
2231 const union pipe_color_union *color,
2232 unsigned dstx, unsigned dsty,
2233 unsigned width, unsigned height)
2234 {
2235 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2236 struct pipe_context *pipe = ctx->base.pipe;
2237 struct pipe_framebuffer_state fb_state;
2238 unsigned num_layers;
2239
2240 assert(dstsurf->texture);
2241 if (!dstsurf->texture)
2242 return;
2243
2244 /* check the saved state */
2245 util_blitter_set_running_flag(blitter);
2246 blitter_check_saved_vertex_states(ctx);
2247 blitter_check_saved_fragment_states(ctx);
2248 blitter_check_saved_fb_state(ctx);
2249 blitter_disable_render_cond(ctx);
2250
2251 /* bind states */
2252 pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA][0]);
2253 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2254 bind_fs_write_one_cbuf(ctx);
2255
2256 /* set a framebuffer state */
2257 fb_state.width = dstsurf->width;
2258 fb_state.height = dstsurf->height;
2259 fb_state.nr_cbufs = 1;
2260 fb_state.cbufs[0] = dstsurf;
2261 fb_state.zsbuf = 0;
2262 pipe->set_framebuffer_state(pipe, &fb_state);
2263 pipe->set_sample_mask(pipe, ~0);
2264
2265 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
2266
2267 union blitter_attrib attrib;
2268 memcpy(attrib.color, color->ui, sizeof(color->ui));
2269
2270 num_layers = dstsurf->u.tex.last_layer - dstsurf->u.tex.first_layer + 1;
2271 if (num_layers > 1 && ctx->has_layered) {
2272 blitter_set_common_draw_rect_state(ctx, false, false);
2273 blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_layered,
2274 dstx, dsty, dstx+width, dsty+height, 0,
2275 num_layers, UTIL_BLITTER_ATTRIB_COLOR, &attrib);
2276 } else {
2277 blitter_set_common_draw_rect_state(ctx, false, false);
2278 blitter->draw_rectangle(blitter, ctx->velem_state,
2279 get_vs_passthrough_pos_generic,
2280 dstx, dsty, dstx+width, dsty+height, 0,
2281 1, UTIL_BLITTER_ATTRIB_COLOR, &attrib);
2282 }
2283
2284 util_blitter_restore_vertex_states(blitter);
2285 util_blitter_restore_fragment_states(blitter);
2286 util_blitter_restore_fb_state(blitter);
2287 util_blitter_restore_render_cond(blitter);
2288 util_blitter_unset_running_flag(blitter);
2289 }
2290
2291 /* Clear a region of a depth stencil surface. */
2292 void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
2293 struct pipe_surface *dstsurf,
2294 unsigned clear_flags,
2295 double depth,
2296 unsigned stencil,
2297 unsigned dstx, unsigned dsty,
2298 unsigned width, unsigned height)
2299 {
2300 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2301 struct pipe_context *pipe = ctx->base.pipe;
2302 struct pipe_framebuffer_state fb_state;
2303 struct pipe_stencil_ref sr = { { 0 } };
2304 unsigned num_layers;
2305
2306 assert(dstsurf->texture);
2307 if (!dstsurf->texture)
2308 return;
2309
2310 /* check the saved state */
2311 util_blitter_set_running_flag(blitter);
2312 blitter_check_saved_vertex_states(ctx);
2313 blitter_check_saved_fragment_states(ctx);
2314 blitter_check_saved_fb_state(ctx);
2315 blitter_disable_render_cond(ctx);
2316
2317 /* bind states */
2318 pipe->bind_blend_state(pipe, ctx->blend[0][0]);
2319 if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
2320 sr.ref_value[0] = stencil & 0xff;
2321 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
2322 pipe->set_stencil_ref(pipe, &sr);
2323 }
2324 else if (clear_flags & PIPE_CLEAR_DEPTH) {
2325 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
2326 }
2327 else if (clear_flags & PIPE_CLEAR_STENCIL) {
2328 sr.ref_value[0] = stencil & 0xff;
2329 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
2330 pipe->set_stencil_ref(pipe, &sr);
2331 }
2332 else
2333 /* hmm that should be illegal probably, or make it a no-op somewhere */
2334 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2335
2336 bind_fs_empty(ctx);
2337
2338 /* set a framebuffer state */
2339 fb_state.width = dstsurf->width;
2340 fb_state.height = dstsurf->height;
2341 fb_state.nr_cbufs = 0;
2342 fb_state.cbufs[0] = 0;
2343 fb_state.zsbuf = dstsurf;
2344 pipe->set_framebuffer_state(pipe, &fb_state);
2345 pipe->set_sample_mask(pipe, ~0);
2346
2347 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
2348
2349 num_layers = dstsurf->u.tex.last_layer - dstsurf->u.tex.first_layer + 1;
2350 if (num_layers > 1 && ctx->has_layered) {
2351 blitter_set_common_draw_rect_state(ctx, false, false);
2352 blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_layered,
2353 dstx, dsty, dstx+width, dsty+height, depth,
2354 num_layers, UTIL_BLITTER_ATTRIB_NONE, NULL);
2355 } else {
2356 blitter_set_common_draw_rect_state(ctx, false, false);
2357 blitter->draw_rectangle(blitter, ctx->velem_state,
2358 get_vs_passthrough_pos,
2359 dstx, dsty, dstx+width, dsty+height, depth, 1,
2360 UTIL_BLITTER_ATTRIB_NONE, NULL);
2361 }
2362
2363 util_blitter_restore_vertex_states(blitter);
2364 util_blitter_restore_fragment_states(blitter);
2365 util_blitter_restore_fb_state(blitter);
2366 util_blitter_restore_render_cond(blitter);
2367 util_blitter_unset_running_flag(blitter);
2368 }
2369
2370 /* draw a rectangle across a region using a custom dsa stage - for r600g */
2371 void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
2372 struct pipe_surface *zsurf,
2373 struct pipe_surface *cbsurf,
2374 unsigned sample_mask,
2375 void *dsa_stage, float depth)
2376 {
2377 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2378 struct pipe_context *pipe = ctx->base.pipe;
2379 struct pipe_framebuffer_state fb_state;
2380
2381 assert(zsurf->texture);
2382 if (!zsurf->texture)
2383 return;
2384
2385 /* check the saved state */
2386 util_blitter_set_running_flag(blitter);
2387 blitter_check_saved_vertex_states(ctx);
2388 blitter_check_saved_fragment_states(ctx);
2389 blitter_check_saved_fb_state(ctx);
2390 blitter_disable_render_cond(ctx);
2391
2392 /* bind states */
2393 pipe->bind_blend_state(pipe, cbsurf ? ctx->blend[PIPE_MASK_RGBA][0] :
2394 ctx->blend[0][0]);
2395 pipe->bind_depth_stencil_alpha_state(pipe, dsa_stage);
2396 if (cbsurf)
2397 bind_fs_write_one_cbuf(ctx);
2398 else
2399 bind_fs_empty(ctx);
2400
2401 /* set a framebuffer state */
2402 fb_state.width = zsurf->width;
2403 fb_state.height = zsurf->height;
2404 fb_state.nr_cbufs = 1;
2405 if (cbsurf) {
2406 fb_state.cbufs[0] = cbsurf;
2407 fb_state.nr_cbufs = 1;
2408 } else {
2409 fb_state.cbufs[0] = NULL;
2410 fb_state.nr_cbufs = 0;
2411 }
2412 fb_state.zsbuf = zsurf;
2413 pipe->set_framebuffer_state(pipe, &fb_state);
2414 pipe->set_sample_mask(pipe, sample_mask);
2415
2416 blitter_set_common_draw_rect_state(ctx, false, false);
2417 blitter_set_dst_dimensions(ctx, zsurf->width, zsurf->height);
2418 blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_passthrough_pos,
2419 0, 0, zsurf->width, zsurf->height, depth,
2420 1, UTIL_BLITTER_ATTRIB_NONE, NULL);
2421
2422 util_blitter_restore_vertex_states(blitter);
2423 util_blitter_restore_fragment_states(blitter);
2424 util_blitter_restore_fb_state(blitter);
2425 util_blitter_restore_render_cond(blitter);
2426 util_blitter_unset_running_flag(blitter);
2427 }
2428
2429 void util_blitter_copy_buffer(struct blitter_context *blitter,
2430 struct pipe_resource *dst,
2431 unsigned dstx,
2432 struct pipe_resource *src,
2433 unsigned srcx,
2434 unsigned size)
2435 {
2436 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2437 struct pipe_context *pipe = ctx->base.pipe;
2438 struct pipe_vertex_buffer vb;
2439 struct pipe_stream_output_target *so_target;
2440 unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0};
2441
2442 if (srcx >= src->width0 ||
2443 dstx >= dst->width0) {
2444 return;
2445 }
2446 if (srcx + size > src->width0) {
2447 size = src->width0 - srcx;
2448 }
2449 if (dstx + size > dst->width0) {
2450 size = dst->width0 - dstx;
2451 }
2452
2453 /* Drivers not capable of Stream Out should not call this function
2454 * in the first place. */
2455 assert(ctx->has_stream_out);
2456
2457 /* Some alignment is required. */
2458 if (srcx % 4 != 0 || dstx % 4 != 0 || size % 4 != 0 ||
2459 !ctx->has_stream_out) {
2460 struct pipe_box box;
2461 u_box_1d(srcx, size, &box);
2462 util_resource_copy_region(pipe, dst, 0, dstx, 0, 0, src, 0, &box);
2463 return;
2464 }
2465
2466 util_blitter_set_running_flag(blitter);
2467 blitter_check_saved_vertex_states(ctx);
2468 blitter_disable_render_cond(ctx);
2469
2470 vb.is_user_buffer = false;
2471 vb.buffer.resource = src;
2472 vb.buffer_offset = srcx;
2473 vb.stride = 4;
2474
2475 pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
2476 pipe->bind_vertex_elements_state(pipe, ctx->velem_state_readbuf[0]);
2477 bind_vs_pos_only(ctx, 1);
2478 if (ctx->has_geometry_shader)
2479 pipe->bind_gs_state(pipe, NULL);
2480 if (ctx->has_tessellation) {
2481 pipe->bind_tcs_state(pipe, NULL);
2482 pipe->bind_tes_state(pipe, NULL);
2483 }
2484 pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state);
2485
2486 so_target = pipe->create_stream_output_target(pipe, dst, dstx, size);
2487 pipe->set_stream_output_targets(pipe, 1, &so_target, offsets);
2488
2489 util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
2490
2491 util_blitter_restore_vertex_states(blitter);
2492 util_blitter_restore_render_cond(blitter);
2493 util_blitter_unset_running_flag(blitter);
2494 pipe_so_target_reference(&so_target, NULL);
2495 }
2496
2497 void util_blitter_clear_buffer(struct blitter_context *blitter,
2498 struct pipe_resource *dst,
2499 unsigned offset, unsigned size,
2500 unsigned num_channels,
2501 const union pipe_color_union *clear_value)
2502 {
2503 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2504 struct pipe_context *pipe = ctx->base.pipe;
2505 struct pipe_vertex_buffer vb = {0};
2506 struct pipe_stream_output_target *so_target = NULL;
2507 unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0};
2508
2509 assert(num_channels >= 1);
2510 assert(num_channels <= 4);
2511
2512 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
2513 *
2514 * R600 uses this to initialize texture resources, so width0 might not be
2515 * what you think it is.
2516 */
2517
2518 /* Streamout is required. */
2519 if (!ctx->has_stream_out) {
2520 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2521 return;
2522 }
2523
2524 /* Some alignment is required. */
2525 if (offset % 4 != 0 || size % 4 != 0) {
2526 assert(!"Bad alignment in util_blitter_clear_buffer()");
2527 return;
2528 }
2529
2530 u_upload_data(pipe->stream_uploader, 0, num_channels*4, 4, clear_value,
2531 &vb.buffer_offset, &vb.buffer.resource);
2532 if (!vb.buffer.resource)
2533 goto out;
2534
2535 vb.stride = 0;
2536
2537 util_blitter_set_running_flag(blitter);
2538 blitter_check_saved_vertex_states(ctx);
2539 blitter_disable_render_cond(ctx);
2540
2541 pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
2542 pipe->bind_vertex_elements_state(pipe,
2543 ctx->velem_state_readbuf[num_channels-1]);
2544 bind_vs_pos_only(ctx, num_channels);
2545 if (ctx->has_geometry_shader)
2546 pipe->bind_gs_state(pipe, NULL);
2547 if (ctx->has_tessellation) {
2548 pipe->bind_tcs_state(pipe, NULL);
2549 pipe->bind_tes_state(pipe, NULL);
2550 }
2551 pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state);
2552
2553 so_target = pipe->create_stream_output_target(pipe, dst, offset, size);
2554 pipe->set_stream_output_targets(pipe, 1, &so_target, offsets);
2555
2556 util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
2557
2558 out:
2559 util_blitter_restore_vertex_states(blitter);
2560 util_blitter_restore_render_cond(blitter);
2561 util_blitter_unset_running_flag(blitter);
2562 pipe_so_target_reference(&so_target, NULL);
2563 pipe_resource_reference(&vb.buffer.resource, NULL);
2564 }
2565
2566 /* probably radeon specific */
2567 void util_blitter_custom_resolve_color(struct blitter_context *blitter,
2568 struct pipe_resource *dst,
2569 unsigned dst_level,
2570 unsigned dst_layer,
2571 struct pipe_resource *src,
2572 unsigned src_layer,
2573 unsigned sample_mask,
2574 void *custom_blend,
2575 enum pipe_format format)
2576 {
2577 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2578 struct pipe_context *pipe = ctx->base.pipe;
2579 struct pipe_framebuffer_state fb_state;
2580 struct pipe_surface *srcsurf, *dstsurf, surf_tmpl;
2581
2582 util_blitter_set_running_flag(blitter);
2583 blitter_check_saved_vertex_states(ctx);
2584 blitter_check_saved_fragment_states(ctx);
2585 blitter_disable_render_cond(ctx);
2586
2587 /* bind states */
2588 pipe->bind_blend_state(pipe, custom_blend);
2589 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2590 bind_fs_write_one_cbuf(ctx);
2591 pipe->set_sample_mask(pipe, sample_mask);
2592
2593 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
2594 surf_tmpl.format = format;
2595 surf_tmpl.u.tex.level = dst_level;
2596 surf_tmpl.u.tex.first_layer = dst_layer;
2597 surf_tmpl.u.tex.last_layer = dst_layer;
2598
2599 dstsurf = pipe->create_surface(pipe, dst, &surf_tmpl);
2600
2601 surf_tmpl.u.tex.level = 0;
2602 surf_tmpl.u.tex.first_layer = src_layer;
2603 surf_tmpl.u.tex.last_layer = src_layer;
2604
2605 srcsurf = pipe->create_surface(pipe, src, &surf_tmpl);
2606
2607 /* set a framebuffer state */
2608 fb_state.width = src->width0;
2609 fb_state.height = src->height0;
2610 fb_state.nr_cbufs = 2;
2611 fb_state.cbufs[0] = srcsurf;
2612 fb_state.cbufs[1] = dstsurf;
2613 fb_state.zsbuf = NULL;
2614 pipe->set_framebuffer_state(pipe, &fb_state);
2615
2616 blitter_set_common_draw_rect_state(ctx, false, false);
2617 blitter_set_dst_dimensions(ctx, src->width0, src->height0);
2618 blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_passthrough_pos,
2619 0, 0, src->width0, src->height0,
2620 0, 1, UTIL_BLITTER_ATTRIB_NONE, NULL);
2621 util_blitter_restore_fb_state(blitter);
2622 util_blitter_restore_vertex_states(blitter);
2623 util_blitter_restore_fragment_states(blitter);
2624 util_blitter_restore_render_cond(blitter);
2625 util_blitter_unset_running_flag(blitter);
2626
2627 pipe_surface_reference(&srcsurf, NULL);
2628 pipe_surface_reference(&dstsurf, NULL);
2629 }
2630
2631 void util_blitter_custom_color(struct blitter_context *blitter,
2632 struct pipe_surface *dstsurf,
2633 void *custom_blend)
2634 {
2635 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2636 struct pipe_context *pipe = ctx->base.pipe;
2637 struct pipe_framebuffer_state fb_state;
2638
2639 assert(dstsurf->texture);
2640 if (!dstsurf->texture)
2641 return;
2642
2643 /* check the saved state */
2644 util_blitter_set_running_flag(blitter);
2645 blitter_check_saved_vertex_states(ctx);
2646 blitter_check_saved_fragment_states(ctx);
2647 blitter_check_saved_fb_state(ctx);
2648 blitter_disable_render_cond(ctx);
2649
2650 /* bind states */
2651 pipe->bind_blend_state(pipe, custom_blend ? custom_blend
2652 : ctx->blend[PIPE_MASK_RGBA][0]);
2653 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2654 bind_fs_write_one_cbuf(ctx);
2655 pipe->set_sample_mask(pipe, (1ull << MAX2(1, dstsurf->texture->nr_samples)) - 1);
2656
2657 /* set a framebuffer state */
2658 fb_state.width = dstsurf->width;
2659 fb_state.height = dstsurf->height;
2660 fb_state.nr_cbufs = 1;
2661 fb_state.cbufs[0] = dstsurf;
2662 fb_state.zsbuf = 0;
2663 pipe->set_framebuffer_state(pipe, &fb_state);
2664 pipe->set_sample_mask(pipe, ~0);
2665
2666 blitter_set_common_draw_rect_state(ctx, false, false);
2667 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
2668 blitter->draw_rectangle(blitter, ctx->velem_state, get_vs_passthrough_pos,
2669 0, 0, dstsurf->width, dstsurf->height,
2670 0, 1, UTIL_BLITTER_ATTRIB_NONE, NULL);
2671
2672 util_blitter_restore_vertex_states(blitter);
2673 util_blitter_restore_fragment_states(blitter);
2674 util_blitter_restore_fb_state(blitter);
2675 util_blitter_restore_render_cond(blitter);
2676 util_blitter_unset_running_flag(blitter);
2677 }
2678
2679 static void *get_custom_vs(struct blitter_context *blitter)
2680 {
2681 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2682
2683 return ctx->custom_vs;
2684 }
2685
2686 /**
2687 * Performs a custom blit to the destination surface, using the VS and FS
2688 * provided.
2689 *
2690 * Used by vc4 for the 8-bit linear-to-tiled blit.
2691 */
2692 void util_blitter_custom_shader(struct blitter_context *blitter,
2693 struct pipe_surface *dstsurf,
2694 void *custom_vs, void *custom_fs)
2695 {
2696 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2697 struct pipe_context *pipe = ctx->base.pipe;
2698 struct pipe_framebuffer_state fb_state;
2699
2700 ctx->custom_vs = custom_vs;
2701
2702 assert(dstsurf->texture);
2703 if (!dstsurf->texture)
2704 return;
2705
2706 /* check the saved state */
2707 util_blitter_set_running_flag(blitter);
2708 blitter_check_saved_vertex_states(ctx);
2709 blitter_check_saved_fragment_states(ctx);
2710 blitter_check_saved_fb_state(ctx);
2711 blitter_disable_render_cond(ctx);
2712
2713 /* bind states */
2714 pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA][0]);
2715 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2716 pipe->bind_fs_state(pipe, custom_fs);
2717 pipe->set_sample_mask(pipe, (1ull << MAX2(1, dstsurf->texture->nr_samples)) - 1);
2718
2719 /* set a framebuffer state */
2720 fb_state.width = dstsurf->width;
2721 fb_state.height = dstsurf->height;
2722 fb_state.nr_cbufs = 1;
2723 fb_state.cbufs[0] = dstsurf;
2724 fb_state.zsbuf = 0;
2725 pipe->set_framebuffer_state(pipe, &fb_state);
2726 pipe->set_sample_mask(pipe, ~0);
2727
2728 blitter_set_common_draw_rect_state(ctx, false, false);
2729 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
2730 blitter->draw_rectangle(blitter, ctx->velem_state, get_custom_vs,
2731 0, 0, dstsurf->width, dstsurf->height,
2732 0, 1, UTIL_BLITTER_ATTRIB_NONE, NULL);
2733
2734 util_blitter_restore_vertex_states(blitter);
2735 util_blitter_restore_fragment_states(blitter);
2736 util_blitter_restore_fb_state(blitter);
2737 util_blitter_restore_render_cond(blitter);
2738 util_blitter_unset_running_flag(blitter);
2739 }