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