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