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