Merge branch 'master' of ssh://people.freedesktop.org/~jbarnes/mesa
[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 "pipe/p_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_simple_shaders.h"
49 #include "util/u_texture.h"
50
51 #define INVALID_PTR ((void*)~0)
52
53 struct blitter_context_priv
54 {
55 struct blitter_context blitter;
56
57 struct pipe_context *pipe; /**< pipe context */
58 struct pipe_buffer *vbuf; /**< quad */
59
60 float vertices[4][2][4]; /**< {pos, color} or {pos, texcoord} */
61
62 /* Templates for various state objects. */
63 struct pipe_depth_stencil_alpha_state template_dsa;
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[0xff]; /**< indices are stencil clear values */
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
99 struct blitter_context *util_blitter_create(struct pipe_context *pipe)
100 {
101 struct blitter_context_priv *ctx;
102 struct pipe_blend_state blend;
103 struct pipe_depth_stencil_alpha_state *dsa;
104 struct pipe_rasterizer_state rs_state;
105 struct pipe_sampler_state *sampler_state;
106 unsigned i;
107
108 ctx = CALLOC_STRUCT(blitter_context_priv);
109 if (!ctx)
110 return NULL;
111
112 ctx->pipe = pipe;
113
114 /* init state objects for them to be considered invalid */
115 ctx->blitter.saved_blend_state = INVALID_PTR;
116 ctx->blitter.saved_dsa_state = INVALID_PTR;
117 ctx->blitter.saved_rs_state = INVALID_PTR;
118 ctx->blitter.saved_fs = INVALID_PTR;
119 ctx->blitter.saved_vs = INVALID_PTR;
120 ctx->blitter.saved_fb_state.nr_cbufs = ~0;
121 ctx->blitter.saved_num_textures = ~0;
122 ctx->blitter.saved_num_sampler_states = ~0;
123
124 /* blend state objects */
125 memset(&blend, 0, sizeof(blend));
126 ctx->blend_keep_color = pipe->create_blend_state(pipe, &blend);
127
128 blend.colormask = PIPE_MASK_RGBA;
129 ctx->blend_write_color = pipe->create_blend_state(pipe, &blend);
130
131 /* depth stencil alpha state objects */
132 dsa = &ctx->template_dsa;
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 /* The DSA state objects which write depth and stencil are created
150 * on-demand. */
151
152 /* sampler state */
153 sampler_state = &ctx->template_sampler_state;
154 sampler_state->wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
155 sampler_state->wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
156 sampler_state->wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
157 /* The sampler state objects which sample from a specified mipmap level
158 * are created on-demand. */
159
160 /* rasterizer state */
161 memset(&rs_state, 0, sizeof(rs_state));
162 rs_state.front_winding = PIPE_WINDING_CW;
163 rs_state.cull_mode = PIPE_WINDING_NONE;
164 rs_state.bypass_vs_clip_and_viewport = 1;
165 rs_state.gl_rasterization_rules = 1;
166 rs_state.flatshade = 1;
167 ctx->rs_state = pipe->create_rasterizer_state(pipe, &rs_state);
168
169 /* fragment shaders are created on-demand */
170
171 /* vertex shaders */
172 {
173 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
174 TGSI_SEMANTIC_COLOR };
175 const uint semantic_indices[] = { 0, 0 };
176 ctx->vs_col =
177 util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
178 semantic_indices);
179 }
180 {
181 const uint semantic_names[] = { TGSI_SEMANTIC_POSITION,
182 TGSI_SEMANTIC_GENERIC };
183 const uint semantic_indices[] = { 0, 0 };
184 ctx->vs_tex =
185 util_make_vertex_passthrough_shader(pipe, 2, semantic_names,
186 semantic_indices);
187 }
188
189 /* set invariant vertex coordinates */
190 for (i = 0; i < 4; i++)
191 ctx->vertices[i][0][3] = 1; /*v.w*/
192
193 /* create the vertex buffer */
194 ctx->vbuf = pipe_buffer_create(ctx->pipe->screen,
195 32,
196 PIPE_BUFFER_USAGE_VERTEX,
197 sizeof(ctx->vertices));
198
199 return &ctx->blitter;
200 }
201
202 void util_blitter_destroy(struct blitter_context *blitter)
203 {
204 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
205 struct pipe_context *pipe = ctx->pipe;
206 int i;
207
208 pipe->delete_blend_state(pipe, ctx->blend_write_color);
209 pipe->delete_blend_state(pipe, ctx->blend_keep_color);
210 pipe->delete_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
211 pipe->delete_depth_stencil_alpha_state(pipe,
212 ctx->dsa_write_depth_keep_stencil);
213
214 for (i = 0; i < 0xff; i++)
215 if (ctx->dsa_write_depth_stencil[i])
216 pipe->delete_depth_stencil_alpha_state(pipe,
217 ctx->dsa_write_depth_stencil[i]);
218
219 pipe->delete_rasterizer_state(pipe, ctx->rs_state);
220 pipe->delete_vs_state(pipe, ctx->vs_col);
221 pipe->delete_vs_state(pipe, ctx->vs_tex);
222
223 for (i = 0; i < PIPE_MAX_TEXTURE_TYPES; i++) {
224 if (ctx->fs_texfetch_col[i])
225 pipe->delete_fs_state(pipe, ctx->fs_texfetch_col[i]);
226 if (ctx->fs_texfetch_depth[i])
227 pipe->delete_fs_state(pipe, ctx->fs_texfetch_depth[i]);
228 }
229
230 for (i = 0; i < PIPE_MAX_COLOR_BUFS && ctx->fs_col[i]; i++)
231 if (ctx->fs_col[i])
232 pipe->delete_fs_state(pipe, ctx->fs_col[i]);
233
234 for (i = 0; i < PIPE_MAX_TEXTURE_LEVELS; i++)
235 if (ctx->sampler_state[i])
236 pipe->delete_sampler_state(pipe, ctx->sampler_state[i]);
237
238 pipe_buffer_reference(&ctx->vbuf, NULL);
239 FREE(ctx);
240 }
241
242 static void blitter_check_saved_CSOs(struct blitter_context_priv *ctx)
243 {
244 /* make sure these CSOs have been saved */
245 assert(ctx->blitter.saved_blend_state != INVALID_PTR &&
246 ctx->blitter.saved_dsa_state != INVALID_PTR &&
247 ctx->blitter.saved_rs_state != INVALID_PTR &&
248 ctx->blitter.saved_fs != INVALID_PTR &&
249 ctx->blitter.saved_vs != INVALID_PTR);
250 }
251
252 static void blitter_restore_CSOs(struct blitter_context_priv *ctx)
253 {
254 struct pipe_context *pipe = ctx->pipe;
255
256 /* restore the state objects which are always required to be saved */
257 pipe->bind_blend_state(pipe, ctx->blitter.saved_blend_state);
258 pipe->bind_depth_stencil_alpha_state(pipe, ctx->blitter.saved_dsa_state);
259 pipe->bind_rasterizer_state(pipe, ctx->blitter.saved_rs_state);
260 pipe->bind_fs_state(pipe, ctx->blitter.saved_fs);
261 pipe->bind_vs_state(pipe, ctx->blitter.saved_vs);
262
263 ctx->blitter.saved_blend_state = INVALID_PTR;
264 ctx->blitter.saved_dsa_state = INVALID_PTR;
265 ctx->blitter.saved_rs_state = INVALID_PTR;
266 ctx->blitter.saved_fs = INVALID_PTR;
267 ctx->blitter.saved_vs = INVALID_PTR;
268
269 /* restore the state objects which are required to be saved before copy/fill
270 */
271 if (ctx->blitter.saved_fb_state.nr_cbufs != ~0) {
272 pipe->set_framebuffer_state(pipe, &ctx->blitter.saved_fb_state);
273 ctx->blitter.saved_fb_state.nr_cbufs = ~0;
274 }
275
276 if (ctx->blitter.saved_num_sampler_states != ~0) {
277 pipe->bind_fragment_sampler_states(pipe,
278 ctx->blitter.saved_num_sampler_states,
279 ctx->blitter.saved_sampler_states);
280 ctx->blitter.saved_num_sampler_states = ~0;
281 }
282
283 if (ctx->blitter.saved_num_textures != ~0) {
284 pipe->set_fragment_sampler_textures(pipe,
285 ctx->blitter.saved_num_textures,
286 ctx->blitter.saved_textures);
287 ctx->blitter.saved_num_textures = ~0;
288 }
289 }
290
291 static void blitter_set_rectangle(struct blitter_context_priv *ctx,
292 unsigned x1, unsigned y1,
293 unsigned x2, unsigned y2,
294 float depth)
295 {
296 int i;
297
298 /* set vertex positions */
299 ctx->vertices[0][0][0] = x1; /*v0.x*/
300 ctx->vertices[0][0][1] = y1; /*v0.y*/
301
302 ctx->vertices[1][0][0] = x2; /*v1.x*/
303 ctx->vertices[1][0][1] = y1; /*v1.y*/
304
305 ctx->vertices[2][0][0] = x2; /*v2.x*/
306 ctx->vertices[2][0][1] = y2; /*v2.y*/
307
308 ctx->vertices[3][0][0] = x1; /*v3.x*/
309 ctx->vertices[3][0][1] = y2; /*v3.y*/
310
311 for (i = 0; i < 4; i++)
312 ctx->vertices[i][0][2] = depth; /*z*/
313 }
314
315 static void blitter_set_clear_color(struct blitter_context_priv *ctx,
316 const float *rgba)
317 {
318 int i;
319
320 for (i = 0; i < 4; i++) {
321 ctx->vertices[i][1][0] = rgba[0];
322 ctx->vertices[i][1][1] = rgba[1];
323 ctx->vertices[i][1][2] = rgba[2];
324 ctx->vertices[i][1][3] = rgba[3];
325 }
326 }
327
328 static void blitter_set_texcoords_2d(struct blitter_context_priv *ctx,
329 struct pipe_surface *surf,
330 unsigned x1, unsigned y1,
331 unsigned x2, unsigned y2)
332 {
333 int i;
334 float s1 = x1 / (float)surf->width;
335 float t1 = y1 / (float)surf->height;
336 float s2 = x2 / (float)surf->width;
337 float t2 = y2 / (float)surf->height;
338
339 ctx->vertices[0][1][0] = s1; /*t0.s*/
340 ctx->vertices[0][1][1] = t1; /*t0.t*/
341
342 ctx->vertices[1][1][0] = s2; /*t1.s*/
343 ctx->vertices[1][1][1] = t1; /*t1.t*/
344
345 ctx->vertices[2][1][0] = s2; /*t2.s*/
346 ctx->vertices[2][1][1] = t2; /*t2.t*/
347
348 ctx->vertices[3][1][0] = s1; /*t3.s*/
349 ctx->vertices[3][1][1] = t2; /*t3.t*/
350
351 for (i = 0; i < 4; i++) {
352 ctx->vertices[i][1][2] = 0; /*r*/
353 ctx->vertices[i][1][3] = 1; /*q*/
354 }
355 }
356
357 static void blitter_set_texcoords_3d(struct blitter_context_priv *ctx,
358 struct pipe_surface *surf,
359 unsigned x1, unsigned y1,
360 unsigned x2, unsigned y2)
361 {
362 int i;
363 float depth = u_minify(surf->texture->depth0, surf->level);
364 float r = surf->zslice / depth;
365
366 blitter_set_texcoords_2d(ctx, surf, x1, y1, x2, y2);
367
368 for (i = 0; i < 4; i++)
369 ctx->vertices[i][1][2] = r; /*r*/
370 }
371
372 static void blitter_set_texcoords_cube(struct blitter_context_priv *ctx,
373 struct pipe_surface *surf,
374 unsigned x1, unsigned y1,
375 unsigned x2, unsigned y2)
376 {
377 int i;
378 float s1 = x1 / (float)surf->width;
379 float t1 = y1 / (float)surf->height;
380 float s2 = x2 / (float)surf->width;
381 float t2 = y2 / (float)surf->height;
382 const float st[4][2] = {
383 {s1, t1}, {s2, t1}, {s2, t2}, {s1, t2}
384 };
385
386 util_map_texcoords2d_onto_cubemap(surf->face,
387 /* pointer, stride in floats */
388 &st[0][0], 2,
389 &ctx->vertices[0][1][0], 8);
390
391 for (i = 0; i < 4; i++)
392 ctx->vertices[i][1][3] = 1; /*q*/
393 }
394
395 static void blitter_draw_quad(struct blitter_context_priv *ctx)
396 {
397 struct pipe_context *pipe = ctx->pipe;
398
399 /* write vertices and draw them */
400 pipe_buffer_write(pipe->screen, ctx->vbuf,
401 0, sizeof(ctx->vertices), ctx->vertices);
402
403 util_draw_vertex_buffer(pipe, ctx->vbuf, 0, PIPE_PRIM_TRIANGLE_FAN,
404 4, /* verts */
405 2); /* attribs/vert */
406 }
407
408 static INLINE
409 void *blitter_get_state_write_depth_stencil(
410 struct blitter_context_priv *ctx,
411 unsigned stencil)
412 {
413 struct pipe_context *pipe = ctx->pipe;
414
415 stencil &= 0xff;
416
417 /* Create the DSA state on-demand. */
418 if (!ctx->dsa_write_depth_stencil[stencil]) {
419 ctx->template_dsa.stencil[0].ref_value = stencil;
420
421 ctx->dsa_write_depth_stencil[stencil] =
422 pipe->create_depth_stencil_alpha_state(pipe, &ctx->template_dsa);
423 }
424
425 return ctx->dsa_write_depth_stencil[stencil];
426 }
427
428 static INLINE
429 void **blitter_get_sampler_state(struct blitter_context_priv *ctx,
430 int miplevel)
431 {
432 struct pipe_context *pipe = ctx->pipe;
433 struct pipe_sampler_state *sampler_state = &ctx->template_sampler_state;
434
435 assert(miplevel < PIPE_MAX_TEXTURE_LEVELS);
436
437 /* Create the sampler state on-demand. */
438 if (!ctx->sampler_state[miplevel]) {
439 sampler_state->lod_bias = miplevel;
440 sampler_state->min_lod = miplevel;
441 sampler_state->max_lod = miplevel;
442
443 ctx->sampler_state[miplevel] = pipe->create_sampler_state(pipe,
444 sampler_state);
445 }
446
447 /* Return void** so that it can be passed to bind_fragment_sampler_states
448 * directly. */
449 return &ctx->sampler_state[miplevel];
450 }
451
452 static INLINE
453 void *blitter_get_fs_col(struct blitter_context_priv *ctx, unsigned num_cbufs)
454 {
455 struct pipe_context *pipe = ctx->pipe;
456 unsigned index = num_cbufs ? num_cbufs - 1 : 0;
457
458 assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
459
460 if (!ctx->fs_col[index])
461 ctx->fs_col[index] =
462 util_make_fragment_clonecolor_shader(pipe, num_cbufs);
463
464 return ctx->fs_col[index];
465 }
466
467 static INLINE
468 void *blitter_get_fs_texfetch_col(struct blitter_context_priv *ctx,
469 unsigned tex_target)
470 {
471 struct pipe_context *pipe = ctx->pipe;
472
473 assert(tex_target < PIPE_MAX_TEXTURE_TYPES);
474
475 /* Create the fragment shader on-demand. */
476 if (!ctx->fs_texfetch_col[tex_target]) {
477 switch (tex_target) {
478 case PIPE_TEXTURE_1D:
479 ctx->fs_texfetch_col[PIPE_TEXTURE_1D] =
480 util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_1D);
481 break;
482 case PIPE_TEXTURE_2D:
483 ctx->fs_texfetch_col[PIPE_TEXTURE_2D] =
484 util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_2D);
485 break;
486 case PIPE_TEXTURE_3D:
487 ctx->fs_texfetch_col[PIPE_TEXTURE_3D] =
488 util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_3D);
489 break;
490 case PIPE_TEXTURE_CUBE:
491 ctx->fs_texfetch_col[PIPE_TEXTURE_CUBE] =
492 util_make_fragment_tex_shader(pipe, TGSI_TEXTURE_CUBE);
493 break;
494 default:;
495 }
496 }
497
498 return ctx->fs_texfetch_col[tex_target];
499 }
500
501 static INLINE
502 void *blitter_get_fs_texfetch_depth(struct blitter_context_priv *ctx,
503 unsigned tex_target)
504 {
505 struct pipe_context *pipe = ctx->pipe;
506
507 assert(tex_target < PIPE_MAX_TEXTURE_TYPES);
508
509 /* Create the fragment shader on-demand. */
510 if (!ctx->fs_texfetch_depth[tex_target]) {
511 switch (tex_target) {
512 case PIPE_TEXTURE_1D:
513 ctx->fs_texfetch_depth[PIPE_TEXTURE_1D] =
514 util_make_fragment_tex_shader_writedepth(pipe, TGSI_TEXTURE_1D);
515 break;
516 case PIPE_TEXTURE_2D:
517 ctx->fs_texfetch_depth[PIPE_TEXTURE_2D] =
518 util_make_fragment_tex_shader_writedepth(pipe, TGSI_TEXTURE_2D);
519 break;
520 case PIPE_TEXTURE_3D:
521 ctx->fs_texfetch_depth[PIPE_TEXTURE_3D] =
522 util_make_fragment_tex_shader_writedepth(pipe, TGSI_TEXTURE_3D);
523 break;
524 case PIPE_TEXTURE_CUBE:
525 ctx->fs_texfetch_depth[PIPE_TEXTURE_CUBE] =
526 util_make_fragment_tex_shader_writedepth(pipe,TGSI_TEXTURE_CUBE);
527 break;
528 default:;
529 }
530 }
531
532 return ctx->fs_texfetch_depth[tex_target];
533 }
534
535 void util_blitter_clear(struct blitter_context *blitter,
536 unsigned width, unsigned height,
537 unsigned num_cbufs,
538 unsigned clear_buffers,
539 const float *rgba,
540 double depth, unsigned stencil)
541 {
542 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
543 struct pipe_context *pipe = ctx->pipe;
544
545 assert(num_cbufs <= PIPE_MAX_COLOR_BUFS);
546
547 blitter_check_saved_CSOs(ctx);
548
549 /* bind CSOs */
550 if (clear_buffers & PIPE_CLEAR_COLOR)
551 pipe->bind_blend_state(pipe, ctx->blend_write_color);
552 else
553 pipe->bind_blend_state(pipe, ctx->blend_keep_color);
554
555 if (clear_buffers & PIPE_CLEAR_DEPTHSTENCIL)
556 pipe->bind_depth_stencil_alpha_state(pipe,
557 blitter_get_state_write_depth_stencil(ctx, stencil));
558 else
559 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
560
561 pipe->bind_rasterizer_state(pipe, ctx->rs_state);
562 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, num_cbufs));
563 pipe->bind_vs_state(pipe, ctx->vs_col);
564
565 blitter_set_clear_color(ctx, rgba);
566 blitter_set_rectangle(ctx, 0, 0, width, height, depth);
567 blitter_draw_quad(ctx);
568 blitter_restore_CSOs(ctx);
569 }
570
571 static boolean
572 is_overlap(unsigned sx1, unsigned sx2, unsigned sy1, unsigned sy2,
573 unsigned dx1, unsigned dx2, unsigned dy1, unsigned dy2)
574 {
575 if (sx1 >= dx2 || sx2 <= dx1 || sy1 >= dy2 || sy2 <= dy1) {
576 return FALSE;
577 } else {
578 return TRUE;
579 }
580 }
581
582 static void util_blitter_do_copy(struct blitter_context *blitter,
583 struct pipe_surface *dst,
584 unsigned dstx, unsigned dsty,
585 struct pipe_surface *src,
586 unsigned srcx, unsigned srcy,
587 unsigned width, unsigned height,
588 boolean is_depth)
589 {
590 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
591 struct pipe_context *pipe = ctx->pipe;
592 struct pipe_framebuffer_state fb_state;
593
594 assert(blitter->saved_fb_state.nr_cbufs != ~0);
595 assert(blitter->saved_num_textures != ~0);
596 assert(blitter->saved_num_sampler_states != ~0);
597 assert(src->texture->target < PIPE_MAX_TEXTURE_TYPES);
598
599 /* bind CSOs */
600 fb_state.width = dst->width;
601 fb_state.height = dst->height;
602
603 if (is_depth) {
604 pipe->bind_blend_state(pipe, ctx->blend_keep_color);
605 pipe->bind_depth_stencil_alpha_state(pipe,
606 ctx->dsa_write_depth_keep_stencil);
607 pipe->bind_fs_state(pipe,
608 blitter_get_fs_texfetch_depth(ctx, src->texture->target));
609
610 fb_state.nr_cbufs = 0;
611 fb_state.zsbuf = dst;
612 } else {
613 pipe->bind_blend_state(pipe, ctx->blend_write_color);
614 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
615 pipe->bind_fs_state(pipe,
616 blitter_get_fs_texfetch_col(ctx, src->texture->target));
617
618 fb_state.nr_cbufs = 1;
619 fb_state.cbufs[0] = dst;
620 fb_state.zsbuf = 0;
621 }
622
623 pipe->bind_rasterizer_state(pipe, ctx->rs_state);
624 pipe->bind_vs_state(pipe, ctx->vs_tex);
625 pipe->bind_fragment_sampler_states(pipe, 1,
626 blitter_get_sampler_state(ctx, src->level));
627 pipe->set_fragment_sampler_textures(pipe, 1, &src->texture);
628 pipe->set_framebuffer_state(pipe, &fb_state);
629
630 /* set texture coordinates */
631 switch (src->texture->target) {
632 case PIPE_TEXTURE_1D:
633 case PIPE_TEXTURE_2D:
634 blitter_set_texcoords_2d(ctx, src, srcx, srcy,
635 srcx+width, srcy+height);
636 break;
637 case PIPE_TEXTURE_3D:
638 blitter_set_texcoords_3d(ctx, src, srcx, srcy,
639 srcx+width, srcy+height);
640 break;
641 case PIPE_TEXTURE_CUBE:
642 blitter_set_texcoords_cube(ctx, src, srcx, srcy,
643 srcx+width, srcy+height);
644 break;
645 default:
646 assert(0);
647 }
648
649 blitter_set_rectangle(ctx, dstx, dsty, dstx+width, dsty+height, 0);
650 blitter_draw_quad(ctx);
651
652 }
653
654 static void util_blitter_overlap_copy(struct blitter_context *blitter,
655 struct pipe_surface *dst,
656 unsigned dstx, unsigned dsty,
657 struct pipe_surface *src,
658 unsigned srcx, unsigned srcy,
659 unsigned width, unsigned height)
660 {
661 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
662 struct pipe_context *pipe = ctx->pipe;
663 struct pipe_screen *screen = pipe->screen;
664
665 struct pipe_texture texTemp;
666 struct pipe_texture *texture;
667 struct pipe_surface *tex_surf;
668
669 /* check whether the states are properly saved */
670 blitter_check_saved_CSOs(ctx);
671
672 memset(&texTemp, 0, sizeof(texTemp));
673 texTemp.target = PIPE_TEXTURE_2D;
674 texTemp.format = dst->texture->format; /* XXX verify supported by driver! */
675 texTemp.last_level = 0;
676 texTemp.width0 = width;
677 texTemp.height0 = height;
678 texTemp.depth0 = 1;
679
680 texture = screen->texture_create(screen, &texTemp);
681 if (!texture)
682 return;
683
684 tex_surf = screen->get_tex_surface(screen, texture, 0, 0, 0,
685 PIPE_BUFFER_USAGE_GPU_READ |
686 PIPE_BUFFER_USAGE_GPU_WRITE);
687
688 /* blit from the src to the temp */
689 util_blitter_do_copy(blitter, tex_surf, 0, 0,
690 src, srcx, srcy,
691 width, height,
692 FALSE);
693 util_blitter_do_copy(blitter, dst, dstx, dsty,
694 tex_surf, 0, 0,
695 width, height,
696 FALSE);
697 pipe_surface_reference(&tex_surf, NULL);
698 pipe_texture_reference(&texture, NULL);
699 blitter_restore_CSOs(ctx);
700 }
701
702 void util_blitter_copy(struct blitter_context *blitter,
703 struct pipe_surface *dst,
704 unsigned dstx, unsigned dsty,
705 struct pipe_surface *src,
706 unsigned srcx, unsigned srcy,
707 unsigned width, unsigned height,
708 boolean ignore_stencil)
709 {
710 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
711 struct pipe_context *pipe = ctx->pipe;
712 struct pipe_screen *screen = pipe->screen;
713 boolean is_stencil, is_depth;
714 unsigned dst_tex_usage;
715
716 /* give up if textures are not set */
717 assert(dst->texture && src->texture);
718 if (!dst->texture || !src->texture)
719 return;
720
721 if (dst->texture == src->texture) {
722 if (is_overlap(srcx, srcx + width, srcy, srcy + height,
723 dstx, dstx + width, dsty, dsty + height)) {
724 util_blitter_overlap_copy(blitter, dst, dstx, dsty, src, srcx, srcy,
725 width, height);
726 return;
727 }
728 }
729
730 is_depth = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 0) != 0;
731 is_stencil = util_format_get_component_bits(src->format, UTIL_FORMAT_COLORSPACE_ZS, 1) != 0;
732 dst_tex_usage = is_depth || is_stencil ? PIPE_TEXTURE_USAGE_DEPTH_STENCIL :
733 PIPE_TEXTURE_USAGE_RENDER_TARGET;
734
735 /* check if we can sample from and render to the surfaces */
736 /* (assuming copying a stencil buffer is not possible) */
737 if ((!ignore_stencil && is_stencil) ||
738 !screen->is_format_supported(screen, dst->format, dst->texture->target,
739 dst_tex_usage, 0) ||
740 !screen->is_format_supported(screen, src->format, src->texture->target,
741 PIPE_TEXTURE_USAGE_SAMPLER, 0)) {
742 util_surface_copy(pipe, FALSE, dst, dstx, dsty, src, srcx, srcy,
743 width, height);
744 return;
745 }
746
747 /* check whether the states are properly saved */
748 blitter_check_saved_CSOs(ctx);
749 util_blitter_do_copy(blitter,
750 dst, dstx, dsty,
751 src, srcx, srcy,
752 width, height, is_depth);
753 blitter_restore_CSOs(ctx);
754 }
755
756 void util_blitter_fill(struct blitter_context *blitter,
757 struct pipe_surface *dst,
758 unsigned dstx, unsigned dsty,
759 unsigned width, unsigned height,
760 unsigned value)
761 {
762 struct blitter_context_priv *ctx = (struct blitter_context_priv*)blitter;
763 struct pipe_context *pipe = ctx->pipe;
764 struct pipe_screen *screen = pipe->screen;
765 struct pipe_framebuffer_state fb_state;
766 float rgba[4];
767 ubyte ub_rgba[4] = {0};
768 union util_color color;
769 int i;
770
771 assert(dst->texture);
772 if (!dst->texture)
773 return;
774
775 /* check if we can render to the surface */
776 if (util_format_is_depth_or_stencil(dst->format) || /* unlikely, but you never know */
777 !screen->is_format_supported(screen, dst->format, dst->texture->target,
778 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)) {
779 util_surface_fill(pipe, dst, dstx, dsty, width, height, value);
780 return;
781 }
782
783 /* unpack the color */
784 color.ui = value;
785 util_unpack_color_ub(dst->format, &color,
786 ub_rgba, ub_rgba+1, ub_rgba+2, ub_rgba+3);
787 for (i = 0; i < 4; i++)
788 rgba[i] = ubyte_to_float(ub_rgba[i]);
789
790 /* check the saved state */
791 blitter_check_saved_CSOs(ctx);
792 assert(blitter->saved_fb_state.nr_cbufs != ~0);
793
794 /* bind CSOs */
795 pipe->bind_blend_state(pipe, ctx->blend_write_color);
796 pipe->bind_depth_stencil_alpha_state(pipe, ctx->dsa_keep_depth_stencil);
797 pipe->bind_rasterizer_state(pipe, ctx->rs_state);
798 pipe->bind_fs_state(pipe, blitter_get_fs_col(ctx, 1));
799 pipe->bind_vs_state(pipe, ctx->vs_col);
800
801 /* set a framebuffer state */
802 fb_state.width = dst->width;
803 fb_state.height = dst->height;
804 fb_state.nr_cbufs = 1;
805 fb_state.cbufs[0] = dst;
806 fb_state.zsbuf = 0;
807 pipe->set_framebuffer_state(pipe, &fb_state);
808
809 blitter_set_clear_color(ctx, rgba);
810 blitter_set_rectangle(ctx, 0, 0, width, height, 0);
811 blitter_draw_quad(ctx);
812 blitter_restore_CSOs(ctx);
813 }