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