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