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