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