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