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