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