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