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