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