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