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