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