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