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