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