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