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