gallium/u_blitter: add new union blitter_attrib to replace pipe_color_union
[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, bool uses_txf,
759 union blitter_attrib *out)
760 {
761 unsigned level = src->u.tex.first_level;
762 bool normalized = !uses_txf &&
763 src->target != PIPE_TEXTURE_RECT &&
764 src->texture->nr_samples <= 1;
765
766 if (normalized) {
767 out->texcoord.x1 = x1 / (float)u_minify(src_width0, level);
768 out->texcoord.y1 = y1 / (float)u_minify(src_height0, level);
769 out->texcoord.x2 = x2 / (float)u_minify(src_width0, level);
770 out->texcoord.y2 = y2 / (float)u_minify(src_height0, level);
771 } else {
772 out->texcoord.x1 = x1;
773 out->texcoord.y1 = y1;
774 out->texcoord.x2 = x2;
775 out->texcoord.y2 = y2;
776 }
777 }
778
779 static void set_texcoords_in_vertices(const union blitter_attrib *attrib,
780 float *out, unsigned stride)
781 {
782 out[0] = attrib->texcoord.x1;
783 out[1] = attrib->texcoord.y1;
784 out += stride;
785 out[0] = attrib->texcoord.x2;
786 out[1] = attrib->texcoord.y1;
787 out += stride;
788 out[0] = attrib->texcoord.x2;
789 out[1] = attrib->texcoord.y2;
790 out += stride;
791 out[0] = attrib->texcoord.x1;
792 out[1] = attrib->texcoord.y2;
793 }
794
795 static void blitter_set_texcoords(struct blitter_context_priv *ctx,
796 struct pipe_sampler_view *src,
797 unsigned src_width0, unsigned src_height0,
798 float layer, unsigned sample,
799 int x1, int y1, int x2, int y2,
800 bool uses_txf)
801 {
802 unsigned i;
803 union blitter_attrib coord;
804 float face_coord[4][2];
805
806 get_texcoords(src, src_width0, src_height0, x1, y1, x2, y2, uses_txf,
807 &coord);
808
809 if (src->target == PIPE_TEXTURE_CUBE ||
810 src->target == PIPE_TEXTURE_CUBE_ARRAY) {
811 set_texcoords_in_vertices(&coord, &face_coord[0][0], 2);
812 util_map_texcoords2d_onto_cubemap((unsigned)layer % 6,
813 /* pointer, stride in floats */
814 &face_coord[0][0], 2,
815 &ctx->vertices[0][1][0], 8,
816 false);
817 } else {
818 set_texcoords_in_vertices(&coord, &ctx->vertices[0][1][0], 8);
819 }
820
821 /* Set the layer. */
822 switch (src->target) {
823 case PIPE_TEXTURE_3D:
824 {
825 float r = layer;
826
827 if (!uses_txf)
828 r /= u_minify(src->texture->depth0, src->u.tex.first_level);
829
830 for (i = 0; i < 4; i++)
831 ctx->vertices[i][1][2] = r; /*r*/
832 }
833 break;
834
835 case PIPE_TEXTURE_1D_ARRAY:
836 for (i = 0; i < 4; i++)
837 ctx->vertices[i][1][1] = (float) layer; /*t*/
838 break;
839
840 case PIPE_TEXTURE_2D_ARRAY:
841 for (i = 0; i < 4; i++) {
842 ctx->vertices[i][1][2] = (float) layer; /*r*/
843 ctx->vertices[i][1][3] = (float) sample; /*q*/
844 }
845 break;
846
847 case PIPE_TEXTURE_CUBE_ARRAY:
848 for (i = 0; i < 4; i++)
849 ctx->vertices[i][1][3] = (float) ((unsigned)layer / 6); /*w*/
850 break;
851
852 case PIPE_TEXTURE_2D:
853 for (i = 0; i < 4; i++) {
854 ctx->vertices[i][1][3] = (float) sample; /*r*/
855 }
856 break;
857
858 default:;
859 }
860 }
861
862 static void blitter_set_dst_dimensions(struct blitter_context_priv *ctx,
863 unsigned width, unsigned height)
864 {
865 ctx->dst_width = width;
866 ctx->dst_height = height;
867 }
868
869 static void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx,
870 enum pipe_format src_format,
871 enum pipe_format dst_format,
872 enum pipe_texture_target target,
873 unsigned src_nr_samples,
874 unsigned dst_nr_samples,
875 unsigned filter,
876 bool use_txf)
877 {
878 struct pipe_context *pipe = ctx->base.pipe;
879 enum tgsi_texture_type tgsi_tex =
880 util_pipe_tex_to_tgsi_tex(target, src_nr_samples);
881 enum tgsi_return_type stype;
882 enum tgsi_return_type dtype;
883 unsigned type;
884
885 assert(target < PIPE_MAX_TEXTURE_TYPES);
886
887 if (util_format_is_pure_uint(src_format)) {
888 stype = TGSI_RETURN_TYPE_UINT;
889 if (util_format_is_pure_uint(dst_format)) {
890 dtype = TGSI_RETURN_TYPE_UINT;
891 type = 0;
892 } else {
893 assert(util_format_is_pure_sint(dst_format));
894 dtype = TGSI_RETURN_TYPE_SINT;
895 type = 1;
896 }
897 } else if (util_format_is_pure_sint(src_format)) {
898 stype = TGSI_RETURN_TYPE_SINT;
899 if (util_format_is_pure_sint(dst_format)) {
900 dtype = TGSI_RETURN_TYPE_SINT;
901 type = 2;
902 } else {
903 assert(util_format_is_pure_uint(dst_format));
904 dtype = TGSI_RETURN_TYPE_UINT;
905 type = 3;
906 }
907 } else {
908 assert(!util_format_is_pure_uint(dst_format) &&
909 !util_format_is_pure_sint(dst_format));
910 dtype = stype = TGSI_RETURN_TYPE_FLOAT;
911 type = 4;
912 }
913
914 if (src_nr_samples > 1) {
915 void **shader;
916
917 /* OpenGL requires that integer textures just copy 1 sample instead
918 * of averaging.
919 */
920 if (dst_nr_samples <= 1 &&
921 stype != TGSI_RETURN_TYPE_UINT &&
922 stype != TGSI_RETURN_TYPE_SINT) {
923 /* The destination has one sample, so we'll do color resolve. */
924 unsigned index = GET_MSAA_RESOLVE_FS_IDX(src_nr_samples);
925
926 assert(filter < 2);
927
928 shader = &ctx->fs_resolve[target][index][filter];
929
930 if (!*shader) {
931 assert(!ctx->cached_all_shaders);
932 if (filter == PIPE_TEX_FILTER_LINEAR) {
933 *shader = util_make_fs_msaa_resolve_bilinear(pipe, tgsi_tex,
934 src_nr_samples,
935 stype);
936 }
937 else {
938 *shader = util_make_fs_msaa_resolve(pipe, tgsi_tex,
939 src_nr_samples,
940 stype);
941 }
942 }
943 }
944 else {
945 /* The destination has multiple samples, we'll do
946 * an MSAA->MSAA copy.
947 */
948 shader = &ctx->fs_texfetch_col_msaa[type][target];
949
950 /* Create the fragment shader on-demand. */
951 if (!*shader) {
952 assert(!ctx->cached_all_shaders);
953 *shader = util_make_fs_blit_msaa_color(pipe, tgsi_tex, stype, dtype);
954 }
955 }
956
957 return *shader;
958 } else {
959 void **shader;
960
961 if (use_txf)
962 shader = &ctx->fs_texfetch_col[type][target][1];
963 else
964 shader = &ctx->fs_texfetch_col[type][target][0];
965
966 /* Create the fragment shader on-demand. */
967 if (!*shader) {
968 assert(!ctx->cached_all_shaders);
969 *shader = util_make_fragment_tex_shader(pipe, tgsi_tex,
970 TGSI_INTERPOLATE_LINEAR,
971 stype, dtype,
972 ctx->has_tex_lz, use_txf);
973 }
974
975 return *shader;
976 }
977 }
978
979 static inline
980 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx,
981 enum pipe_texture_target target,
982 unsigned nr_samples,
983 bool use_txf)
984 {
985 struct pipe_context *pipe = ctx->base.pipe;
986
987 assert(target < PIPE_MAX_TEXTURE_TYPES);
988
989 if (nr_samples > 1) {
990 void **shader = &ctx->fs_texfetch_depth_msaa[target];
991
992 /* Create the fragment shader on-demand. */
993 if (!*shader) {
994 enum tgsi_texture_type tgsi_tex;
995 assert(!ctx->cached_all_shaders);
996 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
997 *shader = util_make_fs_blit_msaa_depth(pipe, tgsi_tex);
998 }
999
1000 return *shader;
1001 } else {
1002 void **shader;
1003
1004 if (use_txf)
1005 shader = &ctx->fs_texfetch_depth[target][1];
1006 else
1007 shader = &ctx->fs_texfetch_depth[target][0];
1008
1009 /* Create the fragment shader on-demand. */
1010 if (!*shader) {
1011 enum tgsi_texture_type tgsi_tex;
1012 assert(!ctx->cached_all_shaders);
1013 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
1014 *shader =
1015 util_make_fragment_tex_shader_writedepth(pipe, tgsi_tex,
1016 TGSI_INTERPOLATE_LINEAR,
1017 ctx->has_tex_lz, use_txf);
1018 }
1019
1020 return *shader;
1021 }
1022 }
1023
1024 static inline
1025 void *blitter_get_fs_texfetch_depthstencil(struct blitter_context_priv *ctx,
1026 enum pipe_texture_target target,
1027 unsigned nr_samples,
1028 bool use_txf)
1029 {
1030 struct pipe_context *pipe = ctx->base.pipe;
1031
1032 assert(target < PIPE_MAX_TEXTURE_TYPES);
1033
1034 if (nr_samples > 1) {
1035 void **shader = &ctx->fs_texfetch_depthstencil_msaa[target];
1036
1037 /* Create the fragment shader on-demand. */
1038 if (!*shader) {
1039 enum tgsi_texture_type tgsi_tex;
1040 assert(!ctx->cached_all_shaders);
1041 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
1042 *shader = util_make_fs_blit_msaa_depthstencil(pipe, tgsi_tex);
1043 }
1044
1045 return *shader;
1046 } else {
1047 void **shader;
1048
1049 if (use_txf)
1050 shader = &ctx->fs_texfetch_depthstencil[target][1];
1051 else
1052 shader = &ctx->fs_texfetch_depthstencil[target][0];
1053
1054 /* Create the fragment shader on-demand. */
1055 if (!*shader) {
1056 enum tgsi_texture_type tgsi_tex;
1057 assert(!ctx->cached_all_shaders);
1058 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
1059 *shader =
1060 util_make_fragment_tex_shader_writedepthstencil(pipe, tgsi_tex,
1061 TGSI_INTERPOLATE_LINEAR,
1062 ctx->has_tex_lz,
1063 use_txf);
1064 }
1065
1066 return *shader;
1067 }
1068 }
1069
1070 static inline
1071 void *blitter_get_fs_texfetch_stencil(struct blitter_context_priv *ctx,
1072 enum pipe_texture_target target,
1073 unsigned nr_samples,
1074 bool use_txf)
1075 {
1076 struct pipe_context *pipe = ctx->base.pipe;
1077
1078 assert(target < PIPE_MAX_TEXTURE_TYPES);
1079
1080 if (nr_samples > 1) {
1081 void **shader = &ctx->fs_texfetch_stencil_msaa[target];
1082
1083 /* Create the fragment shader on-demand. */
1084 if (!*shader) {
1085 enum tgsi_texture_type tgsi_tex;
1086 assert(!ctx->cached_all_shaders);
1087 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, nr_samples);
1088 *shader = util_make_fs_blit_msaa_stencil(pipe, tgsi_tex);
1089 }
1090
1091 return *shader;
1092 } else {
1093 void **shader;
1094
1095 if (use_txf)
1096 shader = &ctx->fs_texfetch_stencil[target][1];
1097 else
1098 shader = &ctx->fs_texfetch_stencil[target][0];
1099
1100 /* Create the fragment shader on-demand. */
1101 if (!*shader) {
1102 enum tgsi_texture_type tgsi_tex;
1103 assert(!ctx->cached_all_shaders);
1104 tgsi_tex = util_pipe_tex_to_tgsi_tex(target, 0);
1105 *shader =
1106 util_make_fragment_tex_shader_writestencil(pipe, tgsi_tex,
1107 TGSI_INTERPOLATE_LINEAR,
1108 ctx->has_tex_lz, use_txf);
1109 }
1110
1111 return *shader;
1112 }
1113 }
1114
1115
1116 /**
1117 * Generate and save all fragment shaders that we will ever need for
1118 * blitting. Drivers which use the 'draw' fallbacks will typically use
1119 * this to make sure we generate/use shaders that don't go through the
1120 * draw module's wrapper functions.
1121 */
1122 void util_blitter_cache_all_shaders(struct blitter_context *blitter)
1123 {
1124 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1125 struct pipe_context *pipe = blitter->pipe;
1126 struct pipe_screen *screen = pipe->screen;
1127 unsigned samples, j, f, target, max_samples, use_txf;
1128 bool has_arraytex, has_cubearraytex;
1129
1130 max_samples = ctx->has_texture_multisample ? 2 : 1;
1131 has_arraytex = screen->get_param(screen,
1132 PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS) != 0;
1133 has_cubearraytex = screen->get_param(screen,
1134 PIPE_CAP_CUBE_MAP_ARRAY) != 0;
1135
1136 /* It only matters if i <= 1 or > 1. */
1137 for (samples = 1; samples <= max_samples; samples++) {
1138 for (target = PIPE_TEXTURE_1D; target < PIPE_MAX_TEXTURE_TYPES; target++) {
1139 for (use_txf = 0; use_txf <= ctx->has_txf; use_txf++) {
1140 if (!has_arraytex &&
1141 (target == PIPE_TEXTURE_1D_ARRAY ||
1142 target == PIPE_TEXTURE_2D_ARRAY)) {
1143 continue;
1144 }
1145 if (!has_cubearraytex &&
1146 (target == PIPE_TEXTURE_CUBE_ARRAY))
1147 continue;
1148
1149 if (samples > 1 &&
1150 (target != PIPE_TEXTURE_2D &&
1151 target != PIPE_TEXTURE_2D_ARRAY))
1152 continue;
1153
1154 if (samples > 1 && use_txf)
1155 continue; /* TXF is the only option, use_txf has no effect */
1156
1157 /* If samples == 1, the shaders read one texel. If samples >= 1,
1158 * they read one sample.
1159 */
1160 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_FLOAT,
1161 PIPE_FORMAT_R32_FLOAT, target,
1162 samples, samples, 0, use_txf);
1163 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT,
1164 PIPE_FORMAT_R32_UINT, target,
1165 samples, samples, 0, use_txf);
1166 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT,
1167 PIPE_FORMAT_R32_SINT, target,
1168 samples, samples, 0, use_txf);
1169 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT,
1170 PIPE_FORMAT_R32_SINT, target,
1171 samples, samples, 0, use_txf);
1172 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT,
1173 PIPE_FORMAT_R32_UINT, target,
1174 samples, samples, 0, use_txf);
1175 blitter_get_fs_texfetch_depth(ctx, target, samples, use_txf);
1176 if (ctx->has_stencil_export) {
1177 blitter_get_fs_texfetch_depthstencil(ctx, target, samples, use_txf);
1178 blitter_get_fs_texfetch_stencil(ctx, target, samples, use_txf);
1179 }
1180
1181 if (samples == 1)
1182 continue;
1183
1184 /* MSAA resolve shaders. */
1185 for (j = 2; j < 32; j++) {
1186 if (!screen->is_format_supported(screen, PIPE_FORMAT_R32_FLOAT,
1187 target, j,
1188 PIPE_BIND_SAMPLER_VIEW)) {
1189 continue;
1190 }
1191
1192 for (f = 0; f < 2; f++) {
1193 if (f != PIPE_TEX_FILTER_NEAREST && use_txf)
1194 continue;
1195
1196 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_FLOAT,
1197 PIPE_FORMAT_R32_FLOAT, target,
1198 j, 1, f, use_txf);
1199 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_UINT,
1200 PIPE_FORMAT_R32_UINT, target,
1201 j, 1, f, use_txf);
1202 blitter_get_fs_texfetch_col(ctx, PIPE_FORMAT_R32_SINT,
1203 PIPE_FORMAT_R32_SINT, target,
1204 j, 1, f, use_txf);
1205 }
1206 }
1207 }
1208 }
1209 }
1210
1211 ctx->fs_empty = util_make_empty_fragment_shader(pipe);
1212
1213 ctx->fs_write_one_cbuf =
1214 util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
1215 TGSI_INTERPOLATE_CONSTANT, false);
1216
1217 ctx->fs_write_all_cbufs =
1218 util_make_fragment_passthrough_shader(pipe, TGSI_SEMANTIC_GENERIC,
1219 TGSI_INTERPOLATE_CONSTANT, true);
1220
1221 ctx->cached_all_shaders = true;
1222 }
1223
1224 static void blitter_set_common_draw_rect_state(struct blitter_context_priv *ctx,
1225 bool scissor,
1226 bool vs_layered)
1227 {
1228 struct pipe_context *pipe = ctx->base.pipe;
1229
1230 pipe->bind_rasterizer_state(pipe, scissor ? ctx->rs_state_scissor
1231 : ctx->rs_state);
1232 if (vs_layered)
1233 bind_vs_layered(ctx);
1234 else
1235 bind_vs_passthrough(ctx);
1236
1237 if (ctx->has_geometry_shader)
1238 pipe->bind_gs_state(pipe, NULL);
1239 if (ctx->has_tessellation) {
1240 pipe->bind_tcs_state(pipe, NULL);
1241 pipe->bind_tes_state(pipe, NULL);
1242 }
1243 if (ctx->has_stream_out)
1244 pipe->set_stream_output_targets(pipe, 0, NULL, NULL);
1245 }
1246
1247 static void blitter_draw(struct blitter_context_priv *ctx,
1248 int x1, int y1, int x2, int y2, float depth,
1249 unsigned num_instances)
1250 {
1251 struct pipe_context *pipe = ctx->base.pipe;
1252 struct pipe_vertex_buffer vb = {0};
1253
1254 blitter_set_rectangle(ctx, x1, y1, x2, y2, depth);
1255
1256 vb.stride = 8 * sizeof(float);
1257
1258 u_upload_data(pipe->stream_uploader, 0, sizeof(ctx->vertices), 4, ctx->vertices,
1259 &vb.buffer_offset, &vb.buffer.resource);
1260 if (!vb.buffer.resource)
1261 return;
1262 u_upload_unmap(pipe->stream_uploader);
1263
1264 pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
1265 util_draw_arrays_instanced(pipe, PIPE_PRIM_TRIANGLE_FAN, 0, 4,
1266 0, num_instances);
1267 pipe_resource_reference(&vb.buffer.resource, NULL);
1268 }
1269
1270 void util_blitter_draw_rectangle(struct blitter_context *blitter,
1271 int x1, int y1, int x2, int y2, float depth,
1272 enum blitter_attrib_type type,
1273 const union blitter_attrib *attrib)
1274 {
1275 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1276
1277 switch (type) {
1278 case UTIL_BLITTER_ATTRIB_COLOR:
1279 blitter_set_clear_color(ctx, attrib->color);
1280 break;
1281
1282 case UTIL_BLITTER_ATTRIB_TEXCOORD:
1283 set_texcoords_in_vertices(attrib, &ctx->vertices[0][1][0], 8);
1284 break;
1285
1286 default:;
1287 }
1288
1289 blitter_draw(ctx, x1, y1, x2, y2, depth, 1);
1290 }
1291
1292 static void *get_clear_blend_state(struct blitter_context_priv *ctx,
1293 unsigned clear_buffers)
1294 {
1295 struct pipe_context *pipe = ctx->base.pipe;
1296 int index;
1297
1298 clear_buffers &= PIPE_CLEAR_COLOR;
1299
1300 /* Return an existing blend state. */
1301 if (!clear_buffers)
1302 return ctx->blend[0][0];
1303
1304 index = GET_CLEAR_BLEND_STATE_IDX(clear_buffers);
1305
1306 if (ctx->blend_clear[index])
1307 return ctx->blend_clear[index];
1308
1309 /* Create a new one. */
1310 {
1311 struct pipe_blend_state blend = {0};
1312 unsigned i;
1313
1314 blend.independent_blend_enable = 1;
1315
1316 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
1317 if (clear_buffers & (PIPE_CLEAR_COLOR0 << i)) {
1318 blend.rt[i].colormask = PIPE_MASK_RGBA;
1319 }
1320 }
1321
1322 ctx->blend_clear[index] = pipe->create_blend_state(pipe, &blend);
1323 }
1324 return ctx->blend_clear[index];
1325 }
1326
1327 void util_blitter_common_clear_setup(struct blitter_context *blitter,
1328 unsigned width, unsigned height,
1329 unsigned clear_buffers,
1330 void *custom_blend, void *custom_dsa)
1331 {
1332 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1333 struct pipe_context *pipe = ctx->base.pipe;
1334
1335 util_blitter_set_running_flag(blitter);
1336 blitter_check_saved_vertex_states(ctx);
1337 blitter_check_saved_fragment_states(ctx);
1338 blitter_disable_render_cond(ctx);
1339
1340 /* bind states */
1341 if (custom_blend) {
1342 pipe->bind_blend_state(pipe, custom_blend);
1343 } else {
1344 pipe->bind_blend_state(pipe, get_clear_blend_state(ctx, clear_buffers));
1345 }
1346
1347 if (custom_dsa) {
1348 pipe->bind_depth_stencil_alpha_state(pipe, custom_dsa);
1349 } else if ((clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
1350 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
1351 } else if (clear_buffers & PIPE_CLEAR_DEPTH) {
1352 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
1353 } else if (clear_buffers & PIPE_CLEAR_STENCIL) {
1354 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
1355 } else {
1356 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1357 }
1358
1359 pipe->set_sample_mask(pipe, ~0);
1360 blitter_set_dst_dimensions(ctx, width, height);
1361 }
1362
1363 static void util_blitter_clear_custom(struct blitter_context *blitter,
1364 unsigned width, unsigned height,
1365 unsigned num_layers,
1366 unsigned clear_buffers,
1367 const union pipe_color_union *color,
1368 double depth, unsigned stencil,
1369 void *custom_blend, void *custom_dsa)
1370 {
1371 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1372 struct pipe_context *pipe = ctx->base.pipe;
1373 struct pipe_stencil_ref sr = { { 0 } };
1374
1375 assert(ctx->has_layered || num_layers <= 1);
1376
1377 util_blitter_common_clear_setup(blitter, width, height, clear_buffers,
1378 custom_blend, custom_dsa);
1379
1380 sr.ref_value[0] = stencil & 0xff;
1381 pipe->set_stencil_ref(pipe, &sr);
1382
1383 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1384 bind_fs_write_all_cbufs(ctx);
1385
1386 if (num_layers > 1 && ctx->has_layered) {
1387 blitter_set_common_draw_rect_state(ctx, false, true);
1388 blitter_set_clear_color(ctx, color->f);
1389 blitter_draw(ctx, 0, 0, width, height, depth, num_layers);
1390 }
1391 else {
1392 union blitter_attrib attrib;
1393
1394 memcpy(attrib.color, color->ui, sizeof(color->ui));
1395 blitter_set_common_draw_rect_state(ctx, false, false);
1396 blitter->draw_rectangle(blitter, 0, 0, width, height, (float) depth,
1397 UTIL_BLITTER_ATTRIB_COLOR, &attrib);
1398 }
1399
1400 util_blitter_restore_vertex_states(blitter);
1401 util_blitter_restore_fragment_states(blitter);
1402 util_blitter_restore_render_cond(blitter);
1403 util_blitter_unset_running_flag(blitter);
1404 }
1405
1406 void util_blitter_clear(struct blitter_context *blitter,
1407 unsigned width, unsigned height, unsigned num_layers,
1408 unsigned clear_buffers,
1409 const union pipe_color_union *color,
1410 double depth, unsigned stencil)
1411 {
1412 util_blitter_clear_custom(blitter, width, height, num_layers,
1413 clear_buffers, color, depth, stencil,
1414 NULL, NULL);
1415 }
1416
1417 void util_blitter_custom_clear_depth(struct blitter_context *blitter,
1418 unsigned width, unsigned height,
1419 double depth, void *custom_dsa)
1420 {
1421 static const union pipe_color_union color;
1422 util_blitter_clear_custom(blitter, width, height, 0, 0, &color, depth, 0,
1423 NULL, custom_dsa);
1424 }
1425
1426 void util_blitter_default_dst_texture(struct pipe_surface *dst_templ,
1427 struct pipe_resource *dst,
1428 unsigned dstlevel,
1429 unsigned dstz)
1430 {
1431 memset(dst_templ, 0, sizeof(*dst_templ));
1432 dst_templ->format = util_format_linear(dst->format);
1433 dst_templ->u.tex.level = dstlevel;
1434 dst_templ->u.tex.first_layer = dstz;
1435 dst_templ->u.tex.last_layer = dstz;
1436 }
1437
1438 static struct pipe_surface *
1439 util_blitter_get_next_surface_layer(struct pipe_context *pipe,
1440 struct pipe_surface *surf)
1441 {
1442 struct pipe_surface dst_templ;
1443
1444 memset(&dst_templ, 0, sizeof(dst_templ));
1445 dst_templ.format = surf->format;
1446 dst_templ.u.tex.level = surf->u.tex.level;
1447 dst_templ.u.tex.first_layer = surf->u.tex.first_layer + 1;
1448 dst_templ.u.tex.last_layer = surf->u.tex.last_layer + 1;
1449
1450 return pipe->create_surface(pipe, surf->texture, &dst_templ);
1451 }
1452
1453 void util_blitter_default_src_texture(struct blitter_context *blitter,
1454 struct pipe_sampler_view *src_templ,
1455 struct pipe_resource *src,
1456 unsigned srclevel)
1457 {
1458 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1459
1460 memset(src_templ, 0, sizeof(*src_templ));
1461
1462 if (ctx->cube_as_2darray &&
1463 (src->target == PIPE_TEXTURE_CUBE ||
1464 src->target == PIPE_TEXTURE_CUBE_ARRAY))
1465 src_templ->target = PIPE_TEXTURE_2D_ARRAY;
1466 else
1467 src_templ->target = src->target;
1468
1469 src_templ->format = util_format_linear(src->format);
1470 src_templ->u.tex.first_level = srclevel;
1471 src_templ->u.tex.last_level = srclevel;
1472 src_templ->u.tex.first_layer = 0;
1473 src_templ->u.tex.last_layer =
1474 src->target == PIPE_TEXTURE_3D ? u_minify(src->depth0, srclevel) - 1
1475 : src->array_size - 1;
1476 src_templ->swizzle_r = PIPE_SWIZZLE_X;
1477 src_templ->swizzle_g = PIPE_SWIZZLE_Y;
1478 src_templ->swizzle_b = PIPE_SWIZZLE_Z;
1479 src_templ->swizzle_a = PIPE_SWIZZLE_W;
1480 }
1481
1482 static bool is_blit_generic_supported(struct blitter_context *blitter,
1483 const struct pipe_resource *dst,
1484 enum pipe_format dst_format,
1485 const struct pipe_resource *src,
1486 enum pipe_format src_format,
1487 unsigned mask)
1488 {
1489 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1490 struct pipe_screen *screen = ctx->base.pipe->screen;
1491
1492 if (dst) {
1493 unsigned bind;
1494 const struct util_format_description *desc =
1495 util_format_description(dst_format);
1496 bool dst_has_stencil = util_format_has_stencil(desc);
1497
1498 /* Stencil export must be supported for stencil copy. */
1499 if ((mask & PIPE_MASK_S) && dst_has_stencil &&
1500 !ctx->has_stencil_export) {
1501 return false;
1502 }
1503
1504 if (dst_has_stencil || util_format_has_depth(desc))
1505 bind = PIPE_BIND_DEPTH_STENCIL;
1506 else
1507 bind = PIPE_BIND_RENDER_TARGET;
1508
1509 if (!screen->is_format_supported(screen, dst_format, dst->target,
1510 dst->nr_samples, bind)) {
1511 return false;
1512 }
1513 }
1514
1515 if (src) {
1516 if (src->nr_samples > 1 && !ctx->has_texture_multisample) {
1517 return false;
1518 }
1519
1520 if (!screen->is_format_supported(screen, src_format, src->target,
1521 src->nr_samples, PIPE_BIND_SAMPLER_VIEW)) {
1522 return false;
1523 }
1524
1525 /* Check stencil sampler support for stencil copy. */
1526 if (mask & PIPE_MASK_S) {
1527 if (util_format_has_stencil(util_format_description(src_format))) {
1528 enum pipe_format stencil_format =
1529 util_format_stencil_only(src_format);
1530 assert(stencil_format != PIPE_FORMAT_NONE);
1531
1532 if (stencil_format != src_format &&
1533 !screen->is_format_supported(screen, stencil_format,
1534 src->target, src->nr_samples,
1535 PIPE_BIND_SAMPLER_VIEW)) {
1536 return false;
1537 }
1538 }
1539 }
1540 }
1541
1542 return true;
1543 }
1544
1545 bool util_blitter_is_copy_supported(struct blitter_context *blitter,
1546 const struct pipe_resource *dst,
1547 const struct pipe_resource *src)
1548 {
1549 return is_blit_generic_supported(blitter, dst, dst->format,
1550 src, src->format, PIPE_MASK_RGBAZS);
1551 }
1552
1553 bool util_blitter_is_blit_supported(struct blitter_context *blitter,
1554 const struct pipe_blit_info *info)
1555 {
1556 return is_blit_generic_supported(blitter,
1557 info->dst.resource, info->dst.format,
1558 info->src.resource, info->src.format,
1559 info->mask);
1560 }
1561
1562 void util_blitter_copy_texture(struct blitter_context *blitter,
1563 struct pipe_resource *dst,
1564 unsigned dst_level,
1565 unsigned dstx, unsigned dsty, unsigned dstz,
1566 struct pipe_resource *src,
1567 unsigned src_level,
1568 const struct pipe_box *srcbox)
1569 {
1570 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1571 struct pipe_context *pipe = ctx->base.pipe;
1572 struct pipe_surface *dst_view, dst_templ;
1573 struct pipe_sampler_view src_templ, *src_view;
1574 struct pipe_box dstbox;
1575
1576 assert(dst && src);
1577 assert(src->target < PIPE_MAX_TEXTURE_TYPES);
1578
1579 u_box_3d(dstx, dsty, dstz, abs(srcbox->width), abs(srcbox->height),
1580 abs(srcbox->depth), &dstbox);
1581
1582 /* Initialize the surface. */
1583 util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
1584 dst_view = pipe->create_surface(pipe, dst, &dst_templ);
1585
1586 /* Initialize the sampler view. */
1587 util_blitter_default_src_texture(blitter, &src_templ, src, src_level);
1588 src_view = pipe->create_sampler_view(pipe, src, &src_templ);
1589
1590 /* Copy. */
1591 util_blitter_blit_generic(blitter, dst_view, &dstbox,
1592 src_view, srcbox, src->width0, src->height0,
1593 PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,
1594 false);
1595
1596 pipe_surface_reference(&dst_view, NULL);
1597 pipe_sampler_view_reference(&src_view, NULL);
1598 }
1599
1600 static void do_blits(struct blitter_context_priv *ctx,
1601 struct pipe_surface *dst,
1602 const struct pipe_box *dstbox,
1603 struct pipe_sampler_view *src,
1604 unsigned src_width0,
1605 unsigned src_height0,
1606 const struct pipe_box *srcbox,
1607 bool is_zsbuf,
1608 bool uses_txf)
1609 {
1610 struct pipe_context *pipe = ctx->base.pipe;
1611 unsigned src_samples = src->texture->nr_samples;
1612 unsigned dst_samples = dst->texture->nr_samples;
1613 enum pipe_texture_target src_target = src->target;
1614 struct pipe_framebuffer_state fb_state = {0};
1615
1616 /* Initialize framebuffer state. */
1617 fb_state.width = dst->width;
1618 fb_state.height = dst->height;
1619 fb_state.nr_cbufs = is_zsbuf ? 0 : 1;
1620
1621 blitter_set_dst_dimensions(ctx, fb_state.width, fb_state.height);
1622
1623 if ((src_target == PIPE_TEXTURE_1D ||
1624 src_target == PIPE_TEXTURE_2D ||
1625 src_target == PIPE_TEXTURE_RECT) &&
1626 src_samples <= 1) {
1627 /* Draw the quad with the draw_rectangle callback. */
1628
1629 /* Set texture coordinates. */
1630 union blitter_attrib coord;
1631 get_texcoords(src, src_width0, src_height0, srcbox->x, srcbox->y,
1632 srcbox->x+srcbox->width, srcbox->y+srcbox->height,
1633 uses_txf, &coord);
1634
1635 /* Set framebuffer state. */
1636 if (is_zsbuf) {
1637 fb_state.zsbuf = dst;
1638 } else {
1639 fb_state.cbufs[0] = dst;
1640 }
1641 pipe->set_framebuffer_state(pipe, &fb_state);
1642
1643 /* Draw. */
1644 pipe->set_sample_mask(pipe, ~0);
1645 ctx->base.draw_rectangle(&ctx->base, dstbox->x, dstbox->y,
1646 dstbox->x + dstbox->width,
1647 dstbox->y + dstbox->height, 0,
1648 UTIL_BLITTER_ATTRIB_TEXCOORD, &coord);
1649 } else {
1650 /* Draw the quad with the generic codepath. */
1651 int dst_z;
1652 for (dst_z = 0; dst_z < dstbox->depth; dst_z++) {
1653 struct pipe_surface *old;
1654 float dst2src_scale = srcbox->depth / (float)dstbox->depth;
1655
1656 /* Scale Z properly if the blit is scaled.
1657 *
1658 * When downscaling, we want the coordinates centered, so that
1659 * mipmapping works for 3D textures. For example, when generating
1660 * a 4x4x4 level, this wouldn't average the pixels:
1661 *
1662 * src Z: 0 1 2 3 4 5 6 7
1663 * dst Z: 0 1 2 3
1664 *
1665 * Because the pixels are not centered below the pixels of the higher
1666 * level. Therefore, we want this:
1667 * src Z: 0 1 2 3 4 5 6 7
1668 * dst Z: 0 1 2 3
1669 *
1670 * dst_offset defines the offset needed for centering the pixels and
1671 * it works with any scaling (not just 2x).
1672 */
1673 float dst_offset = ((srcbox->depth - 1) -
1674 (dstbox->depth - 1) * dst2src_scale) * 0.5;
1675 float src_z = (dst_z + dst_offset) * dst2src_scale;
1676
1677 /* Set framebuffer state. */
1678 if (is_zsbuf) {
1679 fb_state.zsbuf = dst;
1680 } else {
1681 fb_state.cbufs[0] = dst;
1682 }
1683 pipe->set_framebuffer_state(pipe, &fb_state);
1684
1685 /* See if we need to blit a multisample or singlesample buffer. */
1686 if (src_samples == dst_samples && dst_samples > 1) {
1687 /* MSAA copy. */
1688 unsigned i, max_sample = dst_samples - 1;
1689
1690 for (i = 0; i <= max_sample; i++) {
1691 pipe->set_sample_mask(pipe, 1 << i);
1692 blitter_set_texcoords(ctx, src, src_width0, src_height0,
1693 srcbox->z + src_z,
1694 i, srcbox->x, srcbox->y,
1695 srcbox->x + srcbox->width,
1696 srcbox->y + srcbox->height, uses_txf);
1697 blitter_draw(ctx, dstbox->x, dstbox->y,
1698 dstbox->x + dstbox->width,
1699 dstbox->y + dstbox->height, 0, 1);
1700 }
1701 } else {
1702 /* Normal copy, MSAA upsampling, or MSAA resolve. */
1703 pipe->set_sample_mask(pipe, ~0);
1704 blitter_set_texcoords(ctx, src, src_width0, src_height0,
1705 srcbox->z + src_z, 0,
1706 srcbox->x, srcbox->y,
1707 srcbox->x + srcbox->width,
1708 srcbox->y + srcbox->height, uses_txf);
1709 blitter_draw(ctx, dstbox->x, dstbox->y,
1710 dstbox->x + dstbox->width,
1711 dstbox->y + dstbox->height, 0, 1);
1712 }
1713
1714 /* Get the next surface or (if this is the last iteration)
1715 * just unreference the last one. */
1716 old = dst;
1717 if (dst_z < dstbox->depth-1) {
1718 dst = util_blitter_get_next_surface_layer(ctx->base.pipe, dst);
1719 }
1720 if (dst_z) {
1721 pipe_surface_reference(&old, NULL);
1722 }
1723 }
1724 }
1725 }
1726
1727 void util_blitter_blit_generic(struct blitter_context *blitter,
1728 struct pipe_surface *dst,
1729 const struct pipe_box *dstbox,
1730 struct pipe_sampler_view *src,
1731 const struct pipe_box *srcbox,
1732 unsigned src_width0, unsigned src_height0,
1733 unsigned mask, unsigned filter,
1734 const struct pipe_scissor_state *scissor,
1735 bool alpha_blend)
1736 {
1737 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1738 struct pipe_context *pipe = ctx->base.pipe;
1739 enum pipe_texture_target src_target = src->target;
1740 unsigned src_samples = src->texture->nr_samples;
1741 unsigned dst_samples = dst->texture->nr_samples;
1742 bool has_depth, has_stencil, has_color;
1743 bool blit_stencil, blit_depth, blit_color;
1744 void *sampler_state;
1745 const struct util_format_description *src_desc =
1746 util_format_description(src->format);
1747 const struct util_format_description *dst_desc =
1748 util_format_description(dst->format);
1749
1750 has_color = src_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS &&
1751 dst_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS;
1752 has_depth = util_format_has_depth(src_desc) &&
1753 util_format_has_depth(dst_desc);
1754 has_stencil = util_format_has_stencil(src_desc) &&
1755 util_format_has_stencil(dst_desc);
1756
1757 blit_color = has_color && (mask & PIPE_MASK_RGBA);
1758 blit_depth = has_depth && (mask & PIPE_MASK_Z);
1759 blit_stencil = has_stencil && (mask & PIPE_MASK_S) &&
1760 ctx->has_stencil_export;
1761
1762 if (!blit_stencil && !blit_depth && !blit_color) {
1763 return;
1764 }
1765
1766 bool is_scaled = dstbox->width != abs(srcbox->width) ||
1767 dstbox->height != abs(srcbox->height);
1768
1769 if (blit_stencil || !is_scaled)
1770 filter = PIPE_TEX_FILTER_NEAREST;
1771
1772 bool use_txf = false;
1773
1774 /* Don't support scaled blits. The TXF shader uses F2I for rounding. */
1775 if (ctx->has_txf &&
1776 !is_scaled &&
1777 filter == PIPE_TEX_FILTER_NEAREST &&
1778 src->target != PIPE_TEXTURE_CUBE &&
1779 src->target != PIPE_TEXTURE_CUBE_ARRAY) {
1780 int src_width = u_minify(src_width0, src->u.tex.first_level);
1781 int src_height = u_minify(src_height0, src->u.tex.first_level);
1782 int src_depth = src->u.tex.last_layer + 1;
1783 struct pipe_box box = *srcbox;
1784
1785 /* Eliminate negative width/height/depth. */
1786 if (box.width < 0) {
1787 box.x += box.width;
1788 box.width *= -1;
1789 }
1790 if (box.height < 0) {
1791 box.y += box.height;
1792 box.height *= -1;
1793 }
1794 if (box.depth < 0) {
1795 box.z += box.depth;
1796 box.depth *= -1;
1797 }
1798
1799 /* See if srcbox is in bounds. TXF doesn't clamp the coordinates. */
1800 use_txf =
1801 box.x >= 0 && box.x < src_width &&
1802 box.y >= 0 && box.y < src_height &&
1803 box.z >= 0 && box.z < src_depth &&
1804 box.x + box.width > 0 && box.x + box.width <= src_width &&
1805 box.y + box.height > 0 && box.y + box.height <= src_height &&
1806 box.z + box.depth > 0 && box.z + box.depth <= src_depth;
1807 }
1808
1809 /* Check whether the states are properly saved. */
1810 util_blitter_set_running_flag(blitter);
1811 blitter_check_saved_vertex_states(ctx);
1812 blitter_check_saved_fragment_states(ctx);
1813 blitter_check_saved_textures(ctx);
1814 blitter_check_saved_fb_state(ctx);
1815 blitter_disable_render_cond(ctx);
1816
1817 if (blit_depth || blit_stencil) {
1818 pipe->bind_blend_state(pipe, ctx->blend[0][0]);
1819
1820 if (blit_depth && blit_stencil) {
1821 pipe->bind_depth_stencil_alpha_state(pipe,
1822 ctx->dsa_write_depth_stencil);
1823 ctx->bind_fs_state(pipe,
1824 blitter_get_fs_texfetch_depthstencil(ctx, src_target,
1825 src_samples, use_txf));
1826 } else if (blit_depth) {
1827 pipe->bind_depth_stencil_alpha_state(pipe,
1828 ctx->dsa_write_depth_keep_stencil);
1829 ctx->bind_fs_state(pipe,
1830 blitter_get_fs_texfetch_depth(ctx, src_target,
1831 src_samples, use_txf));
1832 } else { /* is_stencil */
1833 pipe->bind_depth_stencil_alpha_state(pipe,
1834 ctx->dsa_keep_depth_write_stencil);
1835 ctx->bind_fs_state(pipe,
1836 blitter_get_fs_texfetch_stencil(ctx, src_target,
1837 src_samples, use_txf));
1838 }
1839
1840 } else {
1841 unsigned colormask = mask & PIPE_MASK_RGBA;
1842
1843 pipe->bind_blend_state(pipe, ctx->blend[colormask][alpha_blend]);
1844 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
1845 ctx->bind_fs_state(pipe,
1846 blitter_get_fs_texfetch_col(ctx, src->format, dst->format, src_target,
1847 src_samples, dst_samples, filter,
1848 use_txf));
1849 }
1850
1851 /* Set the linear filter only for scaled color non-MSAA blits. */
1852 if (filter == PIPE_TEX_FILTER_LINEAR) {
1853 if (src_target == PIPE_TEXTURE_RECT) {
1854 sampler_state = ctx->sampler_state_rect_linear;
1855 } else {
1856 sampler_state = ctx->sampler_state_linear;
1857 }
1858 } else {
1859 if (src_target == PIPE_TEXTURE_RECT) {
1860 sampler_state = ctx->sampler_state_rect;
1861 } else {
1862 sampler_state = ctx->sampler_state;
1863 }
1864 }
1865
1866 /* Set samplers. */
1867 if (blit_depth && blit_stencil) {
1868 /* Setup two samplers, one for depth and the other one for stencil. */
1869 struct pipe_sampler_view templ;
1870 struct pipe_sampler_view *views[2];
1871 void *samplers[2] = {sampler_state, sampler_state};
1872
1873 templ = *src;
1874 templ.format = util_format_stencil_only(templ.format);
1875 assert(templ.format != PIPE_FORMAT_NONE);
1876
1877 views[0] = src;
1878 views[1] = pipe->create_sampler_view(pipe, src->texture, &templ);
1879
1880 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 2, views);
1881 pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, 0, 2, samplers);
1882
1883 pipe_sampler_view_reference(&views[1], NULL);
1884 } else if (blit_stencil) {
1885 /* Set a stencil-only sampler view for it not to sample depth instead. */
1886 struct pipe_sampler_view templ;
1887 struct pipe_sampler_view *view;
1888
1889 templ = *src;
1890 templ.format = util_format_stencil_only(templ.format);
1891 assert(templ.format != PIPE_FORMAT_NONE);
1892
1893 view = pipe->create_sampler_view(pipe, src->texture, &templ);
1894
1895 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &view);
1896 pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
1897 0, 1, &sampler_state);
1898
1899 pipe_sampler_view_reference(&view, NULL);
1900 } else {
1901 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &src);
1902 pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
1903 0, 1, &sampler_state);
1904 }
1905
1906 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
1907 if (scissor) {
1908 pipe->set_scissor_states(pipe, 0, 1, scissor);
1909 }
1910
1911 blitter_set_common_draw_rect_state(ctx, scissor != NULL, false);
1912
1913 do_blits(ctx, dst, dstbox, src, src_width0, src_height0,
1914 srcbox, blit_depth || blit_stencil, use_txf);
1915
1916 util_blitter_restore_vertex_states(blitter);
1917 util_blitter_restore_fragment_states(blitter);
1918 util_blitter_restore_textures(blitter);
1919 util_blitter_restore_fb_state(blitter);
1920 if (scissor) {
1921 pipe->set_scissor_states(pipe, 0, 1, &ctx->base.saved_scissor);
1922 }
1923 util_blitter_restore_render_cond(blitter);
1924 util_blitter_unset_running_flag(blitter);
1925 }
1926
1927 void
1928 util_blitter_blit(struct blitter_context *blitter,
1929 const struct pipe_blit_info *info)
1930 {
1931 struct pipe_resource *dst = info->dst.resource;
1932 struct pipe_resource *src = info->src.resource;
1933 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1934 struct pipe_context *pipe = ctx->base.pipe;
1935 struct pipe_surface *dst_view, dst_templ;
1936 struct pipe_sampler_view src_templ, *src_view;
1937
1938 /* Initialize the surface. */
1939 util_blitter_default_dst_texture(&dst_templ, dst, info->dst.level,
1940 info->dst.box.z);
1941 dst_templ.format = info->dst.format;
1942 dst_view = pipe->create_surface(pipe, dst, &dst_templ);
1943
1944 /* Initialize the sampler view. */
1945 util_blitter_default_src_texture(blitter, &src_templ, src, info->src.level);
1946 src_templ.format = info->src.format;
1947 src_view = pipe->create_sampler_view(pipe, src, &src_templ);
1948
1949 /* Copy. */
1950 util_blitter_blit_generic(blitter, dst_view, &info->dst.box,
1951 src_view, &info->src.box, src->width0, src->height0,
1952 info->mask, info->filter,
1953 info->scissor_enable ? &info->scissor : NULL,
1954 info->alpha_blend);
1955
1956 pipe_surface_reference(&dst_view, NULL);
1957 pipe_sampler_view_reference(&src_view, NULL);
1958 }
1959
1960 void util_blitter_generate_mipmap(struct blitter_context *blitter,
1961 struct pipe_resource *tex,
1962 enum pipe_format format,
1963 unsigned base_level, unsigned last_level,
1964 unsigned first_layer, unsigned last_layer)
1965 {
1966 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
1967 struct pipe_context *pipe = ctx->base.pipe;
1968 struct pipe_surface dst_templ, *dst_view;
1969 struct pipe_sampler_view src_templ, *src_view;
1970 bool is_depth;
1971 void *sampler_state;
1972 const struct util_format_description *desc =
1973 util_format_description(format);
1974 unsigned src_level;
1975 unsigned target = tex->target;
1976
1977 if (ctx->cube_as_2darray &&
1978 (target == PIPE_TEXTURE_CUBE || target == PIPE_TEXTURE_CUBE_ARRAY))
1979 target = PIPE_TEXTURE_2D_ARRAY;
1980
1981 assert(tex->nr_samples <= 1);
1982 assert(!util_format_has_stencil(desc));
1983
1984 is_depth = desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS;
1985
1986 /* Check whether the states are properly saved. */
1987 util_blitter_set_running_flag(blitter);
1988 blitter_check_saved_vertex_states(ctx);
1989 blitter_check_saved_fragment_states(ctx);
1990 blitter_check_saved_textures(ctx);
1991 blitter_check_saved_fb_state(ctx);
1992 blitter_disable_render_cond(ctx);
1993
1994 /* Set states. */
1995 if (is_depth) {
1996 pipe->bind_blend_state(pipe, ctx->blend[0][0]);
1997 pipe->bind_depth_stencil_alpha_state(pipe,
1998 ctx->dsa_write_depth_keep_stencil);
1999 ctx->bind_fs_state(pipe,
2000 blitter_get_fs_texfetch_depth(ctx, target, 1, false));
2001 } else {
2002 pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA][0]);
2003 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2004 ctx->bind_fs_state(pipe,
2005 blitter_get_fs_texfetch_col(ctx, tex->format, tex->format, target,
2006 1, 1, PIPE_TEX_FILTER_LINEAR, false));
2007 }
2008
2009 if (target == PIPE_TEXTURE_RECT) {
2010 sampler_state = ctx->sampler_state_rect_linear;
2011 } else {
2012 sampler_state = ctx->sampler_state_linear;
2013 }
2014 pipe->bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
2015 0, 1, &sampler_state);
2016
2017 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
2018 blitter_set_common_draw_rect_state(ctx, false, false);
2019
2020 for (src_level = base_level; src_level < last_level; src_level++) {
2021 struct pipe_box dstbox = {0}, srcbox = {0};
2022 unsigned dst_level = src_level + 1;
2023
2024 dstbox.width = u_minify(tex->width0, dst_level);
2025 dstbox.height = u_minify(tex->height0, dst_level);
2026
2027 srcbox.width = u_minify(tex->width0, src_level);
2028 srcbox.height = u_minify(tex->height0, src_level);
2029
2030 if (target == PIPE_TEXTURE_3D) {
2031 dstbox.depth = util_max_layer(tex, dst_level) + 1;
2032 srcbox.depth = util_max_layer(tex, src_level) + 1;
2033 } else {
2034 dstbox.z = srcbox.z = first_layer;
2035 dstbox.depth = srcbox.depth = last_layer - first_layer + 1;
2036 }
2037
2038 /* Initialize the surface. */
2039 util_blitter_default_dst_texture(&dst_templ, tex, dst_level,
2040 first_layer);
2041 dst_templ.format = format;
2042 dst_view = pipe->create_surface(pipe, tex, &dst_templ);
2043
2044 /* Initialize the sampler view. */
2045 util_blitter_default_src_texture(blitter, &src_templ, tex, src_level);
2046 src_templ.format = format;
2047 src_view = pipe->create_sampler_view(pipe, tex, &src_templ);
2048
2049 pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, &src_view);
2050
2051 do_blits(ctx, dst_view, &dstbox, src_view, tex->width0, tex->height0,
2052 &srcbox, is_depth, false);
2053
2054 pipe_surface_reference(&dst_view, NULL);
2055 pipe_sampler_view_reference(&src_view, NULL);
2056 }
2057
2058 util_blitter_restore_vertex_states(blitter);
2059 util_blitter_restore_fragment_states(blitter);
2060 util_blitter_restore_textures(blitter);
2061 util_blitter_restore_fb_state(blitter);
2062 util_blitter_restore_render_cond(blitter);
2063 util_blitter_unset_running_flag(blitter);
2064 }
2065
2066 /* Clear a region of a color surface to a constant value. */
2067 void util_blitter_clear_render_target(struct blitter_context *blitter,
2068 struct pipe_surface *dstsurf,
2069 const union pipe_color_union *color,
2070 unsigned dstx, unsigned dsty,
2071 unsigned width, unsigned height)
2072 {
2073 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2074 struct pipe_context *pipe = ctx->base.pipe;
2075 struct pipe_framebuffer_state fb_state;
2076 unsigned num_layers;
2077
2078 assert(dstsurf->texture);
2079 if (!dstsurf->texture)
2080 return;
2081
2082 /* check the saved state */
2083 util_blitter_set_running_flag(blitter);
2084 blitter_check_saved_vertex_states(ctx);
2085 blitter_check_saved_fragment_states(ctx);
2086 blitter_check_saved_fb_state(ctx);
2087 blitter_disable_render_cond(ctx);
2088
2089 /* bind states */
2090 pipe->bind_blend_state(pipe, ctx->blend[PIPE_MASK_RGBA][0]);
2091 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2092 bind_fs_write_one_cbuf(ctx);
2093 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
2094
2095 /* set a framebuffer state */
2096 fb_state.width = dstsurf->width;
2097 fb_state.height = dstsurf->height;
2098 fb_state.nr_cbufs = 1;
2099 fb_state.cbufs[0] = dstsurf;
2100 fb_state.zsbuf = 0;
2101 pipe->set_framebuffer_state(pipe, &fb_state);
2102 pipe->set_sample_mask(pipe, ~0);
2103
2104 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
2105
2106 num_layers = dstsurf->u.tex.last_layer - dstsurf->u.tex.first_layer + 1;
2107 if (num_layers > 1 && ctx->has_layered) {
2108 blitter_set_common_draw_rect_state(ctx, false, true);
2109 blitter_set_clear_color(ctx, color->f);
2110 blitter_draw(ctx, dstx, dsty, dstx+width, dsty+height, 0, num_layers);
2111 }
2112 else {
2113 union blitter_attrib attrib;
2114
2115 memcpy(attrib.color, color->ui, sizeof(color->ui));
2116 blitter_set_common_draw_rect_state(ctx, false, false);
2117 blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height, 0,
2118 UTIL_BLITTER_ATTRIB_COLOR, &attrib);
2119 }
2120
2121 util_blitter_restore_vertex_states(blitter);
2122 util_blitter_restore_fragment_states(blitter);
2123 util_blitter_restore_fb_state(blitter);
2124 util_blitter_restore_render_cond(blitter);
2125 util_blitter_unset_running_flag(blitter);
2126 }
2127
2128 /* Clear a region of a depth stencil surface. */
2129 void util_blitter_clear_depth_stencil(struct blitter_context *blitter,
2130 struct pipe_surface *dstsurf,
2131 unsigned clear_flags,
2132 double depth,
2133 unsigned stencil,
2134 unsigned dstx, unsigned dsty,
2135 unsigned width, unsigned height)
2136 {
2137 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2138 struct pipe_context *pipe = ctx->base.pipe;
2139 struct pipe_framebuffer_state fb_state;
2140 struct pipe_stencil_ref sr = { { 0 } };
2141 unsigned num_layers;
2142
2143 assert(dstsurf->texture);
2144 if (!dstsurf->texture)
2145 return;
2146
2147 /* check the saved state */
2148 util_blitter_set_running_flag(blitter);
2149 blitter_check_saved_vertex_states(ctx);
2150 blitter_check_saved_fragment_states(ctx);
2151 blitter_check_saved_fb_state(ctx);
2152 blitter_disable_render_cond(ctx);
2153
2154 /* bind states */
2155 pipe->bind_blend_state(pipe, ctx->blend[0][0]);
2156 if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) == PIPE_CLEAR_DEPTHSTENCIL) {
2157 sr.ref_value[0] = stencil & 0xff;
2158 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
2159 pipe->set_stencil_ref(pipe, &sr);
2160 }
2161 else if (clear_flags & PIPE_CLEAR_DEPTH) {
2162 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_keep_stencil);
2163 }
2164 else if (clear_flags & PIPE_CLEAR_STENCIL) {
2165 sr.ref_value[0] = stencil & 0xff;
2166 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_write_stencil);
2167 pipe->set_stencil_ref(pipe, &sr);
2168 }
2169 else
2170 /* hmm that should be illegal probably, or make it a no-op somewhere */
2171 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2172
2173 bind_fs_empty(ctx);
2174 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
2175
2176 /* set a framebuffer state */
2177 fb_state.width = dstsurf->width;
2178 fb_state.height = dstsurf->height;
2179 fb_state.nr_cbufs = 0;
2180 fb_state.cbufs[0] = 0;
2181 fb_state.zsbuf = dstsurf;
2182 pipe->set_framebuffer_state(pipe, &fb_state);
2183 pipe->set_sample_mask(pipe, ~0);
2184
2185 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
2186
2187 num_layers = dstsurf->u.tex.last_layer - dstsurf->u.tex.first_layer + 1;
2188 if (num_layers > 1 && ctx->has_layered) {
2189 blitter_set_common_draw_rect_state(ctx, false, true);
2190 blitter_draw(ctx, dstx, dsty, dstx+width, dsty+height, (float) depth, num_layers);
2191 }
2192 else {
2193 blitter_set_common_draw_rect_state(ctx, false, false);
2194 blitter->draw_rectangle(blitter, dstx, dsty, dstx+width, dsty+height,
2195 (float) depth,
2196 UTIL_BLITTER_ATTRIB_NONE, NULL);
2197 }
2198
2199 util_blitter_restore_vertex_states(blitter);
2200 util_blitter_restore_fragment_states(blitter);
2201 util_blitter_restore_fb_state(blitter);
2202 util_blitter_restore_render_cond(blitter);
2203 util_blitter_unset_running_flag(blitter);
2204 }
2205
2206 /* draw a rectangle across a region using a custom dsa stage - for r600g */
2207 void util_blitter_custom_depth_stencil(struct blitter_context *blitter,
2208 struct pipe_surface *zsurf,
2209 struct pipe_surface *cbsurf,
2210 unsigned sample_mask,
2211 void *dsa_stage, float depth)
2212 {
2213 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2214 struct pipe_context *pipe = ctx->base.pipe;
2215 struct pipe_framebuffer_state fb_state;
2216
2217 assert(zsurf->texture);
2218 if (!zsurf->texture)
2219 return;
2220
2221 /* check the saved state */
2222 util_blitter_set_running_flag(blitter);
2223 blitter_check_saved_vertex_states(ctx);
2224 blitter_check_saved_fragment_states(ctx);
2225 blitter_check_saved_fb_state(ctx);
2226 blitter_disable_render_cond(ctx);
2227
2228 /* bind states */
2229 pipe->bind_blend_state(pipe, cbsurf ? ctx->blend[PIPE_MASK_RGBA][0] :
2230 ctx->blend[0][0]);
2231 pipe->bind_depth_stencil_alpha_state(pipe, dsa_stage);
2232 if (cbsurf)
2233 bind_fs_write_one_cbuf(ctx);
2234 else
2235 bind_fs_empty(ctx);
2236 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
2237
2238 /* set a framebuffer state */
2239 fb_state.width = zsurf->width;
2240 fb_state.height = zsurf->height;
2241 fb_state.nr_cbufs = 1;
2242 if (cbsurf) {
2243 fb_state.cbufs[0] = cbsurf;
2244 fb_state.nr_cbufs = 1;
2245 } else {
2246 fb_state.cbufs[0] = NULL;
2247 fb_state.nr_cbufs = 0;
2248 }
2249 fb_state.zsbuf = zsurf;
2250 pipe->set_framebuffer_state(pipe, &fb_state);
2251 pipe->set_sample_mask(pipe, sample_mask);
2252
2253 blitter_set_common_draw_rect_state(ctx, false, false);
2254 blitter_set_dst_dimensions(ctx, zsurf->width, zsurf->height);
2255 blitter->draw_rectangle(blitter, 0, 0, zsurf->width, zsurf->height, depth,
2256 UTIL_BLITTER_ATTRIB_NONE, NULL);
2257
2258 util_blitter_restore_vertex_states(blitter);
2259 util_blitter_restore_fragment_states(blitter);
2260 util_blitter_restore_fb_state(blitter);
2261 util_blitter_restore_render_cond(blitter);
2262 util_blitter_unset_running_flag(blitter);
2263 }
2264
2265 void util_blitter_copy_buffer(struct blitter_context *blitter,
2266 struct pipe_resource *dst,
2267 unsigned dstx,
2268 struct pipe_resource *src,
2269 unsigned srcx,
2270 unsigned size)
2271 {
2272 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2273 struct pipe_context *pipe = ctx->base.pipe;
2274 struct pipe_vertex_buffer vb;
2275 struct pipe_stream_output_target *so_target;
2276 unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0};
2277
2278 if (srcx >= src->width0 ||
2279 dstx >= dst->width0) {
2280 return;
2281 }
2282 if (srcx + size > src->width0) {
2283 size = src->width0 - srcx;
2284 }
2285 if (dstx + size > dst->width0) {
2286 size = dst->width0 - dstx;
2287 }
2288
2289 /* Drivers not capable of Stream Out should not call this function
2290 * in the first place. */
2291 assert(ctx->has_stream_out);
2292
2293 /* Some alignment is required. */
2294 if (srcx % 4 != 0 || dstx % 4 != 0 || size % 4 != 0 ||
2295 !ctx->has_stream_out) {
2296 struct pipe_box box;
2297 u_box_1d(srcx, size, &box);
2298 util_resource_copy_region(pipe, dst, 0, dstx, 0, 0, src, 0, &box);
2299 return;
2300 }
2301
2302 util_blitter_set_running_flag(blitter);
2303 blitter_check_saved_vertex_states(ctx);
2304 blitter_disable_render_cond(ctx);
2305
2306 vb.is_user_buffer = false;
2307 vb.buffer.resource = src;
2308 vb.buffer_offset = srcx;
2309 vb.stride = 4;
2310
2311 pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
2312 pipe->bind_vertex_elements_state(pipe, ctx->velem_state_readbuf[0]);
2313 bind_vs_pos_only(ctx, 1);
2314 if (ctx->has_geometry_shader)
2315 pipe->bind_gs_state(pipe, NULL);
2316 if (ctx->has_tessellation) {
2317 pipe->bind_tcs_state(pipe, NULL);
2318 pipe->bind_tes_state(pipe, NULL);
2319 }
2320 pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state);
2321
2322 so_target = pipe->create_stream_output_target(pipe, dst, dstx, size);
2323 pipe->set_stream_output_targets(pipe, 1, &so_target, offsets);
2324
2325 util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
2326
2327 util_blitter_restore_vertex_states(blitter);
2328 util_blitter_restore_render_cond(blitter);
2329 util_blitter_unset_running_flag(blitter);
2330 pipe_so_target_reference(&so_target, NULL);
2331 }
2332
2333 void util_blitter_clear_buffer(struct blitter_context *blitter,
2334 struct pipe_resource *dst,
2335 unsigned offset, unsigned size,
2336 unsigned num_channels,
2337 const union pipe_color_union *clear_value)
2338 {
2339 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2340 struct pipe_context *pipe = ctx->base.pipe;
2341 struct pipe_vertex_buffer vb = {0};
2342 struct pipe_stream_output_target *so_target = NULL;
2343 unsigned offsets[PIPE_MAX_SO_BUFFERS] = {0};
2344
2345 assert(num_channels >= 1);
2346 assert(num_channels <= 4);
2347
2348 /* IMPORTANT: DON'T DO ANY BOUNDS CHECKING HERE!
2349 *
2350 * R600 uses this to initialize texture resources, so width0 might not be
2351 * what you think it is.
2352 */
2353
2354 /* Streamout is required. */
2355 if (!ctx->has_stream_out) {
2356 assert(!"Streamout unsupported in util_blitter_clear_buffer()");
2357 return;
2358 }
2359
2360 /* Some alignment is required. */
2361 if (offset % 4 != 0 || size % 4 != 0) {
2362 assert(!"Bad alignment in util_blitter_clear_buffer()");
2363 return;
2364 }
2365
2366 u_upload_data(pipe->stream_uploader, 0, num_channels*4, 4, clear_value,
2367 &vb.buffer_offset, &vb.buffer.resource);
2368 if (!vb.buffer.resource)
2369 goto out;
2370
2371 vb.stride = 0;
2372
2373 util_blitter_set_running_flag(blitter);
2374 blitter_check_saved_vertex_states(ctx);
2375 blitter_disable_render_cond(ctx);
2376
2377 pipe->set_vertex_buffers(pipe, ctx->base.vb_slot, 1, &vb);
2378 pipe->bind_vertex_elements_state(pipe,
2379 ctx->velem_state_readbuf[num_channels-1]);
2380 bind_vs_pos_only(ctx, num_channels);
2381 if (ctx->has_geometry_shader)
2382 pipe->bind_gs_state(pipe, NULL);
2383 if (ctx->has_tessellation) {
2384 pipe->bind_tcs_state(pipe, NULL);
2385 pipe->bind_tes_state(pipe, NULL);
2386 }
2387 pipe->bind_rasterizer_state(pipe, ctx->rs_discard_state);
2388
2389 so_target = pipe->create_stream_output_target(pipe, dst, offset, size);
2390 pipe->set_stream_output_targets(pipe, 1, &so_target, offsets);
2391
2392 util_draw_arrays(pipe, PIPE_PRIM_POINTS, 0, size / 4);
2393
2394 out:
2395 util_blitter_restore_vertex_states(blitter);
2396 util_blitter_restore_render_cond(blitter);
2397 util_blitter_unset_running_flag(blitter);
2398 pipe_so_target_reference(&so_target, NULL);
2399 pipe_resource_reference(&vb.buffer.resource, NULL);
2400 }
2401
2402 /* probably radeon specific */
2403 void util_blitter_custom_resolve_color(struct blitter_context *blitter,
2404 struct pipe_resource *dst,
2405 unsigned dst_level,
2406 unsigned dst_layer,
2407 struct pipe_resource *src,
2408 unsigned src_layer,
2409 unsigned sample_mask,
2410 void *custom_blend,
2411 enum pipe_format format)
2412 {
2413 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2414 struct pipe_context *pipe = ctx->base.pipe;
2415 struct pipe_framebuffer_state fb_state;
2416 struct pipe_surface *srcsurf, *dstsurf, surf_tmpl;
2417
2418 util_blitter_set_running_flag(blitter);
2419 blitter_check_saved_vertex_states(ctx);
2420 blitter_check_saved_fragment_states(ctx);
2421 blitter_disable_render_cond(ctx);
2422
2423 /* bind states */
2424 pipe->bind_blend_state(pipe, custom_blend);
2425 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2426 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
2427 bind_fs_write_one_cbuf(ctx);
2428 pipe->set_sample_mask(pipe, sample_mask);
2429
2430 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
2431 surf_tmpl.format = format;
2432 surf_tmpl.u.tex.level = dst_level;
2433 surf_tmpl.u.tex.first_layer = dst_layer;
2434 surf_tmpl.u.tex.last_layer = dst_layer;
2435
2436 dstsurf = pipe->create_surface(pipe, dst, &surf_tmpl);
2437
2438 surf_tmpl.u.tex.level = 0;
2439 surf_tmpl.u.tex.first_layer = src_layer;
2440 surf_tmpl.u.tex.last_layer = src_layer;
2441
2442 srcsurf = pipe->create_surface(pipe, src, &surf_tmpl);
2443
2444 /* set a framebuffer state */
2445 fb_state.width = src->width0;
2446 fb_state.height = src->height0;
2447 fb_state.nr_cbufs = 2;
2448 fb_state.cbufs[0] = srcsurf;
2449 fb_state.cbufs[1] = dstsurf;
2450 fb_state.zsbuf = NULL;
2451 pipe->set_framebuffer_state(pipe, &fb_state);
2452
2453 blitter_set_common_draw_rect_state(ctx, false, false);
2454 blitter_set_dst_dimensions(ctx, src->width0, src->height0);
2455 blitter->draw_rectangle(blitter, 0, 0, src->width0, src->height0,
2456 0, 0, NULL);
2457 util_blitter_restore_fb_state(blitter);
2458 util_blitter_restore_vertex_states(blitter);
2459 util_blitter_restore_fragment_states(blitter);
2460 util_blitter_restore_render_cond(blitter);
2461 util_blitter_unset_running_flag(blitter);
2462
2463 pipe_surface_reference(&srcsurf, NULL);
2464 pipe_surface_reference(&dstsurf, NULL);
2465 }
2466
2467 void util_blitter_custom_color(struct blitter_context *blitter,
2468 struct pipe_surface *dstsurf,
2469 void *custom_blend)
2470 {
2471 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
2472 struct pipe_context *pipe = ctx->base.pipe;
2473 struct pipe_framebuffer_state fb_state;
2474
2475 assert(dstsurf->texture);
2476 if (!dstsurf->texture)
2477 return;
2478
2479 /* check the saved state */
2480 util_blitter_set_running_flag(blitter);
2481 blitter_check_saved_vertex_states(ctx);
2482 blitter_check_saved_fragment_states(ctx);
2483 blitter_check_saved_fb_state(ctx);
2484 blitter_disable_render_cond(ctx);
2485
2486 /* bind states */
2487 pipe->bind_blend_state(pipe, custom_blend ? custom_blend
2488 : ctx->blend[PIPE_MASK_RGBA][0]);
2489 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
2490 bind_fs_write_one_cbuf(ctx);
2491 pipe->bind_vertex_elements_state(pipe, ctx->velem_state);
2492 pipe->set_sample_mask(pipe, (1ull << MAX2(1, dstsurf->texture->nr_samples)) - 1);
2493
2494 /* set a framebuffer state */
2495 fb_state.width = dstsurf->width;
2496 fb_state.height = dstsurf->height;
2497 fb_state.nr_cbufs = 1;
2498 fb_state.cbufs[0] = dstsurf;
2499 fb_state.zsbuf = 0;
2500 pipe->set_framebuffer_state(pipe, &fb_state);
2501 pipe->set_sample_mask(pipe, ~0);
2502
2503 blitter_set_common_draw_rect_state(ctx, false, false);
2504 blitter_set_dst_dimensions(ctx, dstsurf->width, dstsurf->height);
2505 blitter->draw_rectangle(blitter, 0, 0, dstsurf->width, dstsurf->height,
2506 0, 0, NULL);
2507
2508 util_blitter_restore_vertex_states(blitter);
2509 util_blitter_restore_fragment_states(blitter);
2510 util_blitter_restore_fb_state(blitter);
2511 util_blitter_restore_render_cond(blitter);
2512 util_blitter_unset_running_flag(blitter);
2513 }