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