softpipe: Implement sampler view swizzling.
[mesa.git] / src / gallium / auxiliary / util / u_blitter.c
1 /**************************************************************************
2 *
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27 /**
28 * @file
29 * Blitter utility to facilitate acceleration of the clear, surface_copy,
30 * and surface_fill 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_pack_color.h"
47 #include "util/u_rect.h"
48 #include "util/u_sampler.h"
49 #include "util/u_simple_shaders.h"
50 #include "util/u_texture.h"
51
52 #define INVALID_PTR ((void*)~0)
53
54 struct blitter_context_priv
55 {
56 struct blitter_context blitter;
57
58 struct pipe_context *pipe; /**< pipe context */
59 struct pipe_buffer *vbuf; /**< quad */
60
61 float vertices[4][2][4]; /**< {pos, color} or {pos, texcoord} */
62
63 /* Templates for various state objects. */
64 struct pipe_sampler_state template_sampler_state;
65
66 /* Constant state objects. */
67 /* Vertex shaders. */
68 void *vs_col; /**< Vertex shader which passes {pos, color} to the output */
69 void *vs_tex; /**< Vertex shader which passes {pos, texcoord} to the output.*/
70
71 /* Fragment shaders. */
72 /* FS which outputs a color to multiple color buffers. */
73 void *fs_col[PIPE_MAX_COLOR_BUFS];
74
75 /* FS which outputs a color from a texture,
76 where the index is PIPE_TEXTURE_* to be sampled. */
77 void *fs_texfetch_col[PIPE_MAX_TEXTURE_TYPES];
78
79 /* FS which outputs a depth from a texture,
80 where the index is PIPE_TEXTURE_* to be sampled. */
81 void *fs_texfetch_depth[PIPE_MAX_TEXTURE_TYPES];
82
83 /* Blend state. */
84 void *blend_write_color; /**< blend state with writemask of RGBA */
85 void *blend_keep_color; /**< blend state with writemask of 0 */
86
87 /* Depth stencil alpha state. */
88 void *dsa_write_depth_stencil;
89 void *dsa_write_depth_keep_stencil;
90 void *dsa_keep_depth_stencil;
91
92 /* Sampler state for clamping to a miplevel. */
93 void *sampler_state[PIPE_MAX_TEXTURE_LEVELS];
94
95 /* Rasterizer state. */
96 void *rs_state;
97
98 struct pipe_sampler_view *sampler_view;
99 };
100
101 struct blitter_context *util_blitter_create(struct pipe_context *pipe)
102 {
103 struct blitter_context_priv *ctx;
104 struct pipe_blend_state blend = { 0 };
105 struct pipe_depth_stencil_alpha_state dsa = { { 0 } };
106 struct pipe_rasterizer_state rs_state = { 0 };
107 struct pipe_sampler_state *sampler_state;
108 unsigned i;
109
110 ctx = CALLOC_STRUCT(blitter_context_priv);
111 if (!ctx)
112 return NULL;
113
114 ctx->pipe = pipe;
115
116 /* init state objects for them to be considered invalid */
117 ctx->blitter.saved_blend_state = INVALID_PTR;
118 ctx->blitter.saved_dsa_state = INVALID_PTR;
119 ctx->blitter.saved_rs_state = INVALID_PTR;
120 ctx->blitter.saved_fs = INVALID_PTR;
121 ctx->blitter.saved_vs = INVALID_PTR;
122 ctx->blitter.saved_fb_state.nr_cbufs = ~0;
123 ctx->blitter.saved_num_sampler_views = ~0;
124 ctx->blitter.saved_num_sampler_states = ~0;
125
126 /* blend state objects */
127 ctx->blend_keep_color = pipe->create_blend_state(pipe, &blend);
128
129 blend.rt[0].colormask = PIPE_MASK_RGBA;
130 ctx->blend_write_color = pipe->create_blend_state(pipe, &blend);
131
132 /* depth stencil alpha state objects */
133 ctx->dsa_keep_depth_stencil =
134 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
135
136 dsa.depth.enabled = 1;
137 dsa.depth.writemask = 1;
138 dsa.depth.func = PIPE_FUNC_ALWAYS;
139 ctx->dsa_write_depth_keep_stencil =
140 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
141
142 dsa.stencil[0].enabled = 1;
143 dsa.stencil[0].func = PIPE_FUNC_ALWAYS;
144 dsa.stencil[0].fail_op = PIPE_STENCIL_OP_REPLACE;
145 dsa.stencil[0].zpass_op = PIPE_STENCIL_OP_REPLACE;
146 dsa.stencil[0].zfail_op = PIPE_STENCIL_OP_REPLACE;
147 dsa.stencil[0].valuemask = 0xff;
148 dsa.stencil[0].writemask = 0xff;
149 ctx->dsa_write_depth_stencil =
150 pipe->create_depth_stencil_alpha_state(pipe, &dsa);
151 /* The DSA state objects which write depth and stencil are created
152 * on-demand. */
153
154 /* sampler state */
155 sampler_state = &ctx->template_sampler_state;
156 sampler_state->wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
157 sampler_state->wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
158 sampler_state->wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
159 /* The sampler state objects which sample from a specified mipmap level
160 * are created on-demand. */
161
162 /* rasterizer state */
163 memset(&rs_state, 0, sizeof(rs_state));
164 rs_state.front_winding = PIPE_WINDING_CW;
165 rs_state.cull_mode = PIPE_WINDING_NONE;
166 rs_state.bypass_vs_clip_and_viewport = 1;
167 rs_state.gl_rasterization_rules = 1;
168 rs_state.flatshade = 1;
169 ctx->rs_state = pipe->create_rasterizer_state(pipe, &rs_state);
170
171 /* fragment shaders are created on-demand */
172
173 /* vertex shaders */
174 {
175 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
176 TGSI_SEMANTIC_COLOR };
177 const uint semantic_indices[] = { 0, 0 };
178 ctx->vs_col =
179 util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
180 semantic_indices);
181 }
182 {
183 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
184 TGSI_SEMANTIC_GENERIC };
185 const uint semantic_indices[] = { 0, 0 };
186 ctx->vs_tex =
187 util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
188 semantic_indices);
189 }
190
191 /* set invariant vertex coordinates */
192 for (i = 0; i < 4; i++)
193 ctx->vertices[i][0][3] = 1; /*v.w*/
194
195 /* create the vertex buffer */
196 ctx->vbuf = pipe_buffer_create(ctx->pipe->screen,
197 32,
198 PIPE_BUFFER_USAGE_VERTEX,
199 sizeof(ctx->vertices));
200
201 return &ctx->blitter;
202 }
203
204 void util_blitter_destroy(struct blitter_context *blitter)
205 {
206 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
207 struct pipe_context *pipe = ctx->pipe;
208 int i;
209
210 pipe->delete_blend_state(pipe, ctx->blend_write_color);
211 pipe->delete_blend_state(pipe, ctx->blend_keep_color);
212 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
213 pipe->delete_depth_stencil_alpha_state(pipe,
214 ctx->dsa_write_depth_keep_stencil);
215 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
216
217 pipe->delete_rasterizer_state(pipe, ctx->rs_state);
218 pipe->delete_vs_state(pipe, ctx->vs_col);
219 pipe->delete_vs_state(pipe, ctx->vs_tex);
220
221 for (i = 0; i < PIPE_MAX_TEXTURE_TYPES; i++) {
222 if (ctx->fs_texfetch_col[i])
223 pipe->delete_fs_state(pipe, ctx->fs_texfetch_col[i]);
224 if (ctx->fs_texfetch_depth[i])
225 pipe->delete_fs_state(pipe, ctx->fs_texfetch_depth[i]);
226 }
227
228 for (i = 0; i < PIPE_MAX_COLOR_BUFS && ctx->fs_col[i]; i++)
229 if (ctx->fs_col[i])
230 pipe->delete_fs_state(pipe, ctx->fs_col[i]);
231
232 for (i = 0; i < PIPE_MAX_TEXTURE_LEVELS; i++)
233 if (ctx->sampler_state[i])
234 pipe->delete_sampler_state(pipe, ctx->sampler_state[i]);
235
236 if (ctx->sampler_view) {
237 pipe_sampler_view_reference(&ctx->sampler_view, NULL);
238 }
239
240 pipe_buffer_reference(&ctx->vbuf, NULL);
241 FREE(ctx);
242 }
243
244 static void blitter_check_saved_CSOs(struct blitter_context_priv *ctx)
245 {
246 /* make sure these CSOs have been saved */
247 assert(ctx->blitter.saved_blend_state != INVALID_PTR &&
248 ctx->blitter.saved_dsa_state != INVALID_PTR &&
249 ctx->blitter.saved_rs_state != INVALID_PTR &&
250 ctx->blitter.saved_fs != INVALID_PTR &&
251 ctx->blitter.saved_vs != INVALID_PTR);
252 }
253
254 static void blitter_restore_CSOs(struct blitter_context_priv *ctx)
255 {
256 struct pipe_context *pipe = ctx->pipe;
257
258 /* restore the state objects which are always required to be saved */
259 pipe->bind_blend_state(pipe, ctx->blitter.saved_blend_state);
260 pipe->bind_depth_stencil_alpha_state(pipe, ctx->blitter.saved_dsa_state);
261 pipe->bind_rasterizer_state(pipe, ctx->blitter.saved_rs_state);
262 pipe->bind_fs_state(pipe, ctx->blitter.saved_fs);
263 pipe->bind_vs_state(pipe, ctx->blitter.saved_vs);
264
265 ctx->blitter.saved_blend_state = INVALID_PTR;
266 ctx->blitter.saved_dsa_state = INVALID_PTR;
267 ctx->blitter.saved_rs_state = INVALID_PTR;
268 ctx->blitter.saved_fs = INVALID_PTR;
269 ctx->blitter.saved_vs = INVALID_PTR;
270
271 pipe->set_stencil_ref(pipe, &ctx->blitter.saved_stencil_ref);
272
273 /* restore the state objects which are required to be saved before copy/fill
274 */
275 if (ctx->blitter.saved_fb_state.nr_cbufs != ~0) {
276 pipe->set_framebuffer_state(pipe, &ctx->blitter.saved_fb_state);
277 ctx->blitter.saved_fb_state.nr_cbufs = ~0;
278 }
279
280 if (ctx->blitter.saved_num_sampler_states != ~0) {
281 pipe->bind_fragment_sampler_states(pipe,
282 ctx->blitter.saved_num_sampler_states,
283 ctx->blitter.saved_sampler_states);
284 ctx->blitter.saved_num_sampler_states = ~0;
285 }
286
287 if (ctx->blitter.saved_num_sampler_views != ~0) {
288 pipe->set_fragment_sampler_views(pipe,
289 ctx->blitter.saved_num_sampler_views,
290 ctx->blitter.saved_sampler_views);
291 ctx->blitter.saved_num_sampler_views = ~0;
292 }
293 }
294
295 static void blitter_set_rectangle(struct blitter_context_priv *ctx,
296 unsigned x1, unsigned y1,
297 unsigned x2, unsigned y2,
298 float depth)
299 {
300 int i;
301
302 /* set vertex positions */
303 ctx->vertices[0][0][0] = x1; /*v0.x*/
304 ctx->vertices[0][0][1] = y1; /*v0.y*/
305
306 ctx->vertices[1][0][0] = x2; /*v1.x*/
307 ctx->vertices[1][0][1] = y1; /*v1.y*/
308
309 ctx->vertices[2][0][0] = x2; /*v2.x*/
310 ctx->vertices[2][0][1] = y2; /*v2.y*/
311
312 ctx->vertices[3][0][0] = x1; /*v3.x*/
313 ctx->vertices[3][0][1] = y2; /*v3.y*/
314
315 for (i = 0; i < 4; i++)
316 ctx->vertices[i][0][2] = depth; /*z*/
317 }
318
319 static void blitter_set_clear_color(struct blitter_context_priv *ctx,
320 const float *rgba)
321 {
322 int i;
323
324 for (i = 0; i < 4; i++) {
325 ctx->vertices[i][1][0] = rgba[0];
326 ctx->vertices[i][1][1] = rgba[1];
327 ctx->vertices[i][1][2] = rgba[2];
328 ctx->vertices[i][1][3] = rgba[3];
329 }
330 }
331
332 static void blitter_set_texcoords_2d(struct blitter_context_priv *ctx,
333 struct pipe_surface *surf,
334 unsigned x1, unsigned y1,
335 unsigned x2, unsigned y2)
336 {
337 int i;
338 float s1 = x1 / (float)surf->width;
339 float t1 = y1 / (float)surf->height;
340 float s2 = x2 / (float)surf->width;
341 float t2 = y2 / (float)surf->height;
342
343 ctx->vertices[0][1][0] = s1; /*t0.s*/
344 ctx->vertices[0][1][1] = t1; /*t0.t*/
345
346 ctx->vertices[1][1][0] = s2; /*t1.s*/
347 ctx->vertices[1][1][1] = t1; /*t1.t*/
348
349 ctx->vertices[2][1][0] = s2; /*t2.s*/
350 ctx->vertices[2][1][1] = t2; /*t2.t*/
351
352 ctx->vertices[3][1][0] = s1; /*t3.s*/
353 ctx->vertices[3][1][1] = t2; /*t3.t*/
354
355 for (i = 0; i < 4; i++) {
356 ctx->vertices[i][1][2] = 0; /*r*/
357 ctx->vertices[i][1][3] = 1; /*q*/
358 }
359 }
360
361 static void blitter_set_texcoords_3d(struct blitter_context_priv *ctx,
362 struct pipe_surface *surf,
363 unsigned x1, unsigned y1,
364 unsigned x2, unsigned y2)
365 {
366 int i;
367 float depth = u_minify(surf->texture->depth0, surf->level);
368 float r = surf->zslice / depth;
369
370 blitter_set_texcoords_2d(ctx, surf, x1, y1, x2, y2);
371
372 for (i = 0; i < 4; i++)
373 ctx->vertices[i][1][2] = r; /*r*/
374 }
375
376 static void blitter_set_texcoords_cube(struct blitter_context_priv *ctx,
377 struct pipe_surface *surf,
378 unsigned x1, unsigned y1,
379 unsigned x2, unsigned y2)
380 {
381 int i;
382 float s1 = x1 / (float)surf->width;
383 float t1 = y1 / (float)surf->height;
384 float s2 = x2 / (float)surf->width;
385 float t2 = y2 / (float)surf->height;
386 float st[4][2];
387
388 st[0][0] = s1;
389 st[0][1] = t1;
390 st[1][0] = s2;
391 st[1][1] = t1;
392 st[2][0] = s2;
393 st[2][1] = t2;
394 st[3][0] = s1;
395 st[3][1] = t2;
396
397 util_map_texcoords2d_onto_cubemap(surf->face,
398 /* pointer, stride in floats */
399 &st[0][0], 2,
400 &ctx->vertices[0][1][0], 8);
401
402 for (i = 0; i < 4; i++)
403 ctx->vertices[i][1][3] = 1; /*q*/
404 }
405
406 static void blitter_draw_quad(struct blitter_context_priv *ctx)
407 {
408 struct pipe_context *pipe = ctx->pipe;
409
410 /* write vertices and draw them */
411 pipe_buffer_write(pipe->screen, ctx->vbuf,
412 0, sizeof(ctx->vertices), ctx->vertices);
413
414 util_draw_vertex_buffer(pipe, ctx->vbuf, 0, PIPE_PRIM_TRIANGLE_FAN,
415 4, /* verts */
416 2); /* attribs/vert */
417 }
418
419 static INLINE
420 void **blitter_get_sampler_state(struct blitter_context_priv *ctx,
421 int miplevel)
422 {
423 struct pipe_context *pipe = ctx->pipe;
424 struct pipe_sampler_state *sampler_state = &ctx->template_sampler_state;
425
426 assert(miplevel < PIPE_MAX_TEXTURE_LEVELS);
427
428 /* Create the sampler state on-demand. */
429 if (!ctx->sampler_state[miplevel]) {
430 sampler_state->lod_bias = miplevel;
431 sampler_state->min_lod = miplevel;
432 sampler_state->max_lod = miplevel;
433
434 ctx->sampler_state[miplevel] = pipe->create_sampler_state(pipe,
435 sampler_state);
436 }
437
438 /* Return void** so that it can be passed to bind_fragment_sampler_states
439 * directly. */
440 return &ctx->sampler_state[miplevel];
441 }
442
443 static INLINE
444 void *blitter_get_fs_col(struct blitter_context_priv *ctx, unsigned num_cbufs)
445 {
446 struct pipe_context *pipe = ctx->pipe;
447 unsigned index = num_cbufs ? num_cbufs - 1 : 0;
448
449 assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
450
451 if (!ctx->fs_col[index])
452 ctx->fs_col[index] =
453 util_make_fragment_clonecolor_shader(pipe, num_cbufs);
454
455 return ctx->fs_col[index];
456 }
457
458 static INLINE
459 void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx,
460 unsigned tex_target)
461 {
462 struct pipe_context *pipe = ctx->pipe;
463
464 assert(tex_target < PIPE_MAX_TEXTURE_TYPES);
465
466 /* Create the fragment shader on-demand. */
467 if (!ctx->fs_texfetch_col[tex_target]) {
468 switch (tex_target) {
469 case PIPE_TEXTURE_1D:
470 ctx->fs_texfetch_col[PIPE_TEXTURE_1D] =
471 util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_1D);
472 break;
473 case PIPE_TEXTURE_2D:
474 ctx->fs_texfetch_col[PIPE_TEXTURE_2D] =
475 util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_2D);
476 break;
477 case PIPE_TEXTURE_3D:
478 ctx->fs_texfetch_col[PIPE_TEXTURE_3D] =
479 util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_3D);
480 break;
481 case PIPE_TEXTURE_CUBE:
482 ctx->fs_texfetch_col[PIPE_TEXTURE_CUBE] =
483 util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_CUBE);
484 break;
485 default:;
486 }
487 }
488
489 return ctx->fs_texfetch_col[tex_target];
490 }
491
492 static INLINE
493 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx,
494 unsigned tex_target)
495 {
496 struct pipe_context *pipe = ctx->pipe;
497
498 assert(tex_target < PIPE_MAX_TEXTURE_TYPES);
499
500 /* Create the fragment shader on-demand. */
501 if (!ctx->fs_texfetch_depth[tex_target]) {
502 switch (tex_target) {
503 case PIPE_TEXTURE_1D:
504 ctx->fs_texfetch_depth[PIPE_TEXTURE_1D] =
505 util_make_fragment_tex_shader_writedepth(pipe, TGSI_TEXTURE_1D);
506 break;
507 case PIPE_TEXTURE_2D:
508 ctx->fs_texfetch_depth[PIPE_TEXTURE_2D] =
509 util_make_fragment_tex_shader_writedepth(pipe, TGSI_TEXTURE_2D);
510 break;
511 case PIPE_TEXTURE_3D:
512 ctx->fs_texfetch_depth[PIPE_TEXTURE_3D] =
513 util_make_fragment_tex_shader_writedepth(pipe, TGSI_TEXTURE_3D);
514 break;
515 case PIPE_TEXTURE_CUBE:
516 ctx->fs_texfetch_depth[PIPE_TEXTURE_CUBE] =
517 util_make_fragment_tex_shader_writedepth(pipe,TGSI_TEXTURE_CUBE);
518 break;
519 default:;
520 }
521 }
522
523 return ctx->fs_texfetch_depth[tex_target];
524 }
525
526 void util_blitter_clear(struct blitter_context *blitter,
527 unsigned width, unsigned height,
528 unsigned num_cbufs,
529 unsigned clear_buffers,
530 const float *rgba,
531 double depth, unsigned stencil)
532 {
533 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
534 struct pipe_context *pipe = ctx->pipe;
535 struct pipe_stencil_ref sr = { { 0 } };
536
537 assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
538
539 blitter_check_saved_CSOs(ctx);
540
541 /* bind CSOs */
542 if (clear_buffers & PIPE_CLEAR_COLOR)
543 pipe->bind_blend_state(pipe, ctx->blend_write_color);
544 else
545 pipe->bind_blend_state(pipe, ctx->blend_keep_color);
546
547 if (clear_buffers & PIPE_CLEAR_DEPTHSTENCIL) {
548 sr.ref_value[0] = stencil & 0xff;
549 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_write_depth_stencil);
550 pipe->set_stencil_ref(pipe, &sr);
551 }
552 else
553 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
554
555 pipe->bind_rasterizer_state(pipe, ctx->rs_state);
556 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, num_cbufs));
557 pipe->bind_vs_state(pipe, ctx->vs_col);
558
559 blitter_set_clear_color(ctx, rgba);
560 blitter_set_rectangle(ctx, 0, 0, width, height, depth);
561 blitter_draw_quad(ctx);
562 blitter_restore_CSOs(ctx);
563 }
564
565 static boolean
566 is_overlap(unsigned sx1, unsigned sx2, unsigned sy1, unsigned sy2,
567 unsigned dx1, unsigned dx2, unsigned dy1, unsigned dy2)
568 {
569 if (sx1 >= dx2 || sx2 <= dx1 || sy1 >= dy2 || sy2 <= dy1) {
570 return FALSE;
571 } else {
572 return TRUE;
573 }
574 }
575
576 static void util_blitter_do_copy(struct blitter_context *blitter,
577 struct pipe_surface *dst,
578 unsigned dstx, unsigned dsty,
579 struct pipe_surface *src,
580 unsigned srcx, unsigned srcy,
581 unsigned width, unsigned height,
582 boolean is_depth)
583 {
584 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
585 struct pipe_context *pipe = ctx->pipe;
586 struct pipe_framebuffer_state fb_state;
587 struct pipe_sampler_view viewTempl, *view;
588
589 assert(blitter->saved_fb_state.nr_cbufs != ~0);
590 assert(blitter->saved_num_sampler_views != ~0);
591 assert(blitter->saved_num_sampler_states != ~0);
592 assert(src->texture->target < PIPE_MAX_TEXTURE_TYPES);
593
594 /* bind CSOs */
595 fb_state.width = dst->width;
596 fb_state.height = dst->height;
597
598 if (is_depth) {
599 pipe->bind_blend_state(pipe, ctx->blend_keep_color);
600 pipe->bind_depth_stencil_alpha_state(pipe,
601 ctx->dsa_write_depth_keep_stencil);
602 pipe->bind_fs_state(pipe,
603 blitter_get_fs_texfetch_depth(ctx, src->texture->target));
604
605 fb_state.nr_cbufs = 0;
606 fb_state.zsbuf = dst;
607 } else {
608 pipe->bind_blend_state(pipe, ctx->blend_write_color);
609 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
610 pipe->bind_fs_state(pipe,
611 blitter_get_fs_texfetch_col(ctx, src->texture->target));
612
613 fb_state.nr_cbufs = 1;
614 fb_state.cbufs[0] = dst;
615 fb_state.zsbuf = 0;
616 }
617
618 u_sampler_view_default_template(&viewTempl,
619 src->texture,
620 src->texture->format);
621 view = pipe->create_sampler_view(pipe,
622 src->texture,
623 &viewTempl);
624
625 if (ctx->sampler_view) {
626 pipe_sampler_view_reference(&ctx->sampler_view, NULL);
627 }
628 ctx->sampler_view = view;
629
630 pipe->bind_rasterizer_state(pipe, ctx->rs_state);
631 pipe->bind_vs_state(pipe, ctx->vs_tex);
632 pipe->bind_fragment_sampler_states(pipe, 1,
633 blitter_get_sampler_state(ctx, src->level));
634 pipe->set_fragment_sampler_views(pipe, 1, &view);
635 pipe->set_framebuffer_state(pipe, &fb_state);
636
637 /* set texture coordinates */
638 switch (src->texture->target) {
639 case PIPE_TEXTURE_1D:
640 case PIPE_TEXTURE_2D:
641 blitter_set_texcoords_2d(ctx, src, srcx, srcy,
642 srcx+width, srcy+height);
643 break;
644 case PIPE_TEXTURE_3D:
645 blitter_set_texcoords_3d(ctx, src, srcx, srcy,
646 srcx+width, srcy+height);
647 break;
648 case PIPE_TEXTURE_CUBE:
649 blitter_set_texcoords_cube(ctx, src, srcx, srcy,
650 srcx+width, srcy+height);
651 break;
652 default:
653 assert(0);
654 }
655
656 blitter_set_rectangle(ctx, dstx, dsty, dstx+width, dsty+height, 0);
657 blitter_draw_quad(ctx);
658
659 }
660
661 static void util_blitter_overlap_copy(struct blitter_context *blitter,
662 struct pipe_surface *dst,
663 unsigned dstx, unsigned dsty,
664 struct pipe_surface *src,
665 unsigned srcx, unsigned srcy,
666 unsigned width, unsigned height)
667 {
668 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
669 struct pipe_context *pipe = ctx->pipe;
670 struct pipe_screen *screen = pipe->screen;
671
672 struct pipe_texture texTemp;
673 struct pipe_texture *texture;
674 struct pipe_surface *tex_surf;
675
676 /* check whether the states are properly saved */
677 blitter_check_saved_CSOs(ctx);
678
679 memset(&texTemp, 0, sizeof(texTemp));
680 texTemp.target = PIPE_TEXTURE_2D;
681 texTemp.format = dst->texture->format; /* XXX verify supported by driver! */
682 texTemp.last_level = 0;
683 texTemp.width0 = width;
684 texTemp.height0 = height;
685 texTemp.depth0 = 1;
686
687 texture = screen->texture_create(screen, &texTemp);
688 if (!texture)
689 return;
690
691 tex_surf = screen->get_tex_surface(screen, texture, 0, 0, 0,
692 PIPE_BUFFER_USAGE_GPU_READ |
693 PIPE_BUFFER_USAGE_GPU_WRITE);
694
695 /* blit from the src to the temp */
696 util_blitter_do_copy(blitter, tex_surf, 0, 0,
697 src, srcx, srcy,
698 width, height,
699 FALSE);
700 util_blitter_do_copy(blitter, dst, dstx, dsty,
701 tex_surf, 0, 0,
702 width, height,
703 FALSE);
704 pipe_surface_reference(&tex_surf, NULL);
705 pipe_texture_reference(&texture, NULL);
706 blitter_restore_CSOs(ctx);
707 }
708
709 void util_blitter_copy(struct blitter_context *blitter,
710 struct pipe_surface *dst,
711 unsigned dstx, unsigned dsty,
712 struct pipe_surface *src,
713 unsigned srcx, unsigned srcy,
714 unsigned width, unsigned height,
715 boolean ignore_stencil)
716 {
717 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
718 struct pipe_context *pipe = ctx->pipe;
719 struct pipe_screen *screen = pipe->screen;
720 boolean is_stencil, is_depth;
721 unsigned dst_tex_usage;
722
723 /* give up if textures are not set */
724 assert(dst->texture && src->texture);
725 if (!dst->texture || !src->texture)
726 return;
727
728 if (dst->texture == src->texture) {
729 if (is_overlap(srcx, srcx + width, srcy, srcy + height,
730 dstx, dstx + width, dsty, dsty + height)) {
731 util_blitter_overlap_copy(blitter, dst, dstx, dsty, src, srcx, srcy,
732 width, height);
733 return;
734 }
735 }
736
737 is_depth = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 0) != 0;
738 is_stencil = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 1) != 0;
739 dst_tex_usage = is_depth || is_stencil ? PIPE_TEXTURE_USAGE_DEPTH_STENCIL :
740 PIPE_TEXTURE_USAGE_RENDER_TARGET;
741
742 /* check if we can sample from and render to the surfaces */
743 /* (assuming copying a stencil buffer is not possible) */
744 if ((!ignore_stencil && is_stencil) ||
745 !screen->is_format_supported(screen, dst->format, dst->texture->target,
746 dst_tex_usage, 0) ||
747 !screen->is_format_supported(screen, src->format, src->texture->target,
748 PIPE_TEXTURE_USAGE_SAMPLER, 0)) {
749 util_surface_copy(pipe, FALSE, dst, dstx, dsty, src, srcx, srcy,
750 width, height);
751 return;
752 }
753
754 /* check whether the states are properly saved */
755 blitter_check_saved_CSOs(ctx);
756 util_blitter_do_copy(blitter,
757 dst, dstx, dsty,
758 src, srcx, srcy,
759 width, height, is_depth);
760 blitter_restore_CSOs(ctx);
761 }
762
763 void util_blitter_fill(struct blitter_context *blitter,
764 struct pipe_surface *dst,
765 unsigned dstx, unsigned dsty,
766 unsigned width, unsigned height,
767 unsigned value)
768 {
769 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
770 struct pipe_context *pipe = ctx->pipe;
771 struct pipe_screen *screen = pipe->screen;
772 struct pipe_framebuffer_state fb_state;
773 float rgba[4];
774 ubyte ub_rgba[4] = {0};
775 union util_color color;
776 int i;
777
778 assert(dst->texture);
779 if (!dst->texture)
780 return;
781
782 /* check if we can render to the surface */
783 if (util_format_is_depth_or_stencil(dst->format) || /* unlikely, but you never know */
784 !screen->is_format_supported(screen, dst->format, dst->texture->target,
785 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)) {
786 util_surface_fill(pipe, dst, dstx, dsty, width, height, value);
787 return;
788 }
789
790 /* unpack the color */
791 color.ui = value;
792 util_unpack_color_ub(dst->format, &color,
793 ub_rgba, ub_rgba+1, ub_rgba+2, ub_rgba+3);
794 for (i = 0; i < 4; i++)
795 rgba[i] = ubyte_to_float(ub_rgba[i]);
796
797 /* check the saved state */
798 blitter_check_saved_CSOs(ctx);
799 assert(blitter->saved_fb_state.nr_cbufs != ~0);
800
801 /* bind CSOs */
802 pipe->bind_blend_state(pipe, ctx->blend_write_color);
803 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
804 pipe->bind_rasterizer_state(pipe, ctx->rs_state);
805 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1));
806 pipe->bind_vs_state(pipe, ctx->vs_col);
807
808 /* set a framebuffer state */
809 fb_state.width = dst->width;
810 fb_state.height = dst->height;
811 fb_state.nr_cbufs = 1;
812 fb_state.cbufs[0] = dst;
813 fb_state.zsbuf = 0;
814 pipe->set_framebuffer_state(pipe, &fb_state);
815
816 blitter_set_clear_color(ctx, rgba);
817 blitter_set_rectangle(ctx, 0, 0, width, height, 0);
818 blitter_draw_quad(ctx);
819 blitter_restore_CSOs(ctx);
820 }