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