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