17ec681f3Smrg/**********************************************************
27ec681f3Smrg * Copyright 2009-2011 VMware, Inc. All rights reserved.
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person
57ec681f3Smrg * obtaining a copy of this software and associated documentation
67ec681f3Smrg * files (the "Software"), to deal in the Software without
77ec681f3Smrg * restriction, including without limitation the rights to use, copy,
87ec681f3Smrg * modify, merge, publish, distribute, sublicense, and/or sell copies
97ec681f3Smrg * of the Software, and to permit persons to whom the Software is
107ec681f3Smrg * furnished to do so, subject to the following conditions:
117ec681f3Smrg *
127ec681f3Smrg * The above copyright notice and this permission notice shall be
137ec681f3Smrg * included in all copies or substantial portions of the Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
167ec681f3Smrg * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
177ec681f3Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
187ec681f3Smrg * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
197ec681f3Smrg * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
207ec681f3Smrg * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
217ec681f3Smrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
227ec681f3Smrg * SOFTWARE.
237ec681f3Smrg *
247ec681f3Smrg *********************************************************
257ec681f3Smrg * Authors:
267ec681f3Smrg * Zack Rusin <zackr-at-vmware-dot-com>
277ec681f3Smrg */
287ec681f3Smrg
297ec681f3Smrg#include "xa_context.h"
307ec681f3Smrg#include "xa_priv.h"
317ec681f3Smrg#include <math.h>
327ec681f3Smrg#include "cso_cache/cso_context.h"
337ec681f3Smrg#include "util/u_inlines.h"
347ec681f3Smrg#include "util/u_sampler.h"
357ec681f3Smrg#include "util/u_draw_quad.h"
367ec681f3Smrg
377ec681f3Smrg#define floatsEqual(x, y) (fabsf(x - y) <= 0.00001f * MIN2(fabsf(x), fabsf(y)))
387ec681f3Smrg#define floatIsZero(x) (floatsEqual((x) + 1, 1))
397ec681f3Smrg
407ec681f3Smrg#define NUM_COMPONENTS 4
417ec681f3Smrg
427ec681f3Smrgvoid
437ec681f3Smrg
447ec681f3Smrg
457ec681f3Smrgrenderer_set_constants(struct xa_context *r,
467ec681f3Smrg		       int shader_type, const float *params, int param_bytes);
477ec681f3Smrg
487ec681f3Smrgstatic inline boolean
497ec681f3Smrgis_affine(const float *matrix)
507ec681f3Smrg{
517ec681f3Smrg    return floatIsZero(matrix[2]) && floatIsZero(matrix[5])
527ec681f3Smrg	&& floatsEqual(matrix[8], 1);
537ec681f3Smrg}
547ec681f3Smrg
557ec681f3Smrgstatic inline void
567ec681f3Smrgmap_point(const float *mat, float x, float y, float *out_x, float *out_y)
577ec681f3Smrg{
587ec681f3Smrg    if (!mat) {
597ec681f3Smrg	*out_x = x;
607ec681f3Smrg	*out_y = y;
617ec681f3Smrg	return;
627ec681f3Smrg    }
637ec681f3Smrg
647ec681f3Smrg    *out_x = mat[0] * x + mat[3] * y + mat[6];
657ec681f3Smrg    *out_y = mat[1] * x + mat[4] * y + mat[7];
667ec681f3Smrg    if (!is_affine(mat)) {
677ec681f3Smrg	float w = 1 / (mat[2] * x + mat[5] * y + mat[8]);
687ec681f3Smrg
697ec681f3Smrg	*out_x *= w;
707ec681f3Smrg	*out_y *= w;
717ec681f3Smrg    }
727ec681f3Smrg}
737ec681f3Smrg
747ec681f3Smrgstatic inline void
757ec681f3Smrgrenderer_draw(struct xa_context *r)
767ec681f3Smrg{
777ec681f3Smrg    int num_verts = r->buffer_size / (r->attrs_per_vertex * NUM_COMPONENTS);
787ec681f3Smrg
797ec681f3Smrg    if (!r->buffer_size)
807ec681f3Smrg	return;
817ec681f3Smrg
827ec681f3Smrg    if (!r->scissor_valid) {
837ec681f3Smrg	r->scissor.minx = 0;
847ec681f3Smrg	r->scissor.miny = 0;
857ec681f3Smrg	r->scissor.maxx = r->dst->tex->width0;
867ec681f3Smrg	r->scissor.maxy = r->dst->tex->height0;
877ec681f3Smrg    }
887ec681f3Smrg
897ec681f3Smrg    r->pipe->set_scissor_states(r->pipe, 0, 1, &r->scissor);
907ec681f3Smrg
917ec681f3Smrg    struct cso_velems_state velems;
927ec681f3Smrg    velems.count = r->attrs_per_vertex;
937ec681f3Smrg    memcpy(velems.velems, r->velems, sizeof(r->velems[0]) * velems.count);
947ec681f3Smrg
957ec681f3Smrg    cso_set_vertex_elements(r->cso, &velems);
967ec681f3Smrg    util_draw_user_vertex_buffer(r->cso, r->buffer, PIPE_PRIM_QUADS,
977ec681f3Smrg                                 num_verts,	/* verts */
987ec681f3Smrg                                 r->attrs_per_vertex);	/* attribs/vert */
997ec681f3Smrg    r->buffer_size = 0;
1007ec681f3Smrg
1017ec681f3Smrg    xa_scissor_reset(r);
1027ec681f3Smrg}
1037ec681f3Smrg
1047ec681f3Smrgstatic inline void
1057ec681f3Smrgrenderer_draw_conditional(struct xa_context *r, int next_batch)
1067ec681f3Smrg{
1077ec681f3Smrg    if (r->buffer_size + next_batch >= XA_VB_SIZE ||
1087ec681f3Smrg	(next_batch == 0 && r->buffer_size)) {
1097ec681f3Smrg	renderer_draw(r);
1107ec681f3Smrg    }
1117ec681f3Smrg}
1127ec681f3Smrg
1137ec681f3Smrgvoid
1147ec681f3Smrgrenderer_init_state(struct xa_context *r)
1157ec681f3Smrg{
1167ec681f3Smrg    struct pipe_depth_stencil_alpha_state dsa;
1177ec681f3Smrg    struct pipe_rasterizer_state raster;
1187ec681f3Smrg    unsigned i;
1197ec681f3Smrg
1207ec681f3Smrg    /* set common initial clip state */
1217ec681f3Smrg    memset(&dsa, 0, sizeof(struct pipe_depth_stencil_alpha_state));
1227ec681f3Smrg    cso_set_depth_stencil_alpha(r->cso, &dsa);
1237ec681f3Smrg
1247ec681f3Smrg    /* XXX: move to renderer_init_state? */
1257ec681f3Smrg    memset(&raster, 0, sizeof(struct pipe_rasterizer_state));
1267ec681f3Smrg    raster.half_pixel_center = 1;
1277ec681f3Smrg    raster.bottom_edge_rule = 1;
1287ec681f3Smrg    raster.depth_clip_near = 1;
1297ec681f3Smrg    raster.depth_clip_far = 1;
1307ec681f3Smrg    raster.scissor = 1;
1317ec681f3Smrg    cso_set_rasterizer(r->cso, &raster);
1327ec681f3Smrg
1337ec681f3Smrg    /* vertex elements state */
1347ec681f3Smrg    memset(&r->velems[0], 0, sizeof(r->velems[0]) * 3);
1357ec681f3Smrg    for (i = 0; i < 3; i++) {
1367ec681f3Smrg	r->velems[i].src_offset = i * 4 * sizeof(float);
1377ec681f3Smrg	r->velems[i].instance_divisor = 0;
1387ec681f3Smrg	r->velems[i].vertex_buffer_index = 0;
1397ec681f3Smrg	r->velems[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
1407ec681f3Smrg    }
1417ec681f3Smrg}
1427ec681f3Smrg
1437ec681f3Smrgstatic inline void
1447ec681f3Smrgadd_vertex_none(struct xa_context *r, float x, float y)
1457ec681f3Smrg{
1467ec681f3Smrg    float *vertex = r->buffer + r->buffer_size;
1477ec681f3Smrg
1487ec681f3Smrg    vertex[0] = x;
1497ec681f3Smrg    vertex[1] = y;
1507ec681f3Smrg    vertex[2] = 0.f;		/*z */
1517ec681f3Smrg    vertex[3] = 1.f;		/*w */
1527ec681f3Smrg
1537ec681f3Smrg    r->buffer_size += 4;
1547ec681f3Smrg}
1557ec681f3Smrg
1567ec681f3Smrgstatic inline void
1577ec681f3Smrgadd_vertex_1tex(struct xa_context *r, float x, float y, float s, float t)
1587ec681f3Smrg{
1597ec681f3Smrg    float *vertex = r->buffer + r->buffer_size;
1607ec681f3Smrg
1617ec681f3Smrg    vertex[0] = x;
1627ec681f3Smrg    vertex[1] = y;
1637ec681f3Smrg    vertex[2] = 0.f;		/*z */
1647ec681f3Smrg    vertex[3] = 1.f;		/*w */
1657ec681f3Smrg
1667ec681f3Smrg    vertex[4] = s;		/*s */
1677ec681f3Smrg    vertex[5] = t;		/*t */
1687ec681f3Smrg    vertex[6] = 0.f;		/*r */
1697ec681f3Smrg    vertex[7] = 1.f;		/*q */
1707ec681f3Smrg
1717ec681f3Smrg    r->buffer_size += 8;
1727ec681f3Smrg}
1737ec681f3Smrg
1747ec681f3Smrgstatic inline void
1757ec681f3Smrgadd_vertex_2tex(struct xa_context *r,
1767ec681f3Smrg		float x, float y, float s0, float t0, float s1, float t1)
1777ec681f3Smrg{
1787ec681f3Smrg    float *vertex = r->buffer + r->buffer_size;
1797ec681f3Smrg
1807ec681f3Smrg    vertex[0] = x;
1817ec681f3Smrg    vertex[1] = y;
1827ec681f3Smrg    vertex[2] = 0.f;		/*z */
1837ec681f3Smrg    vertex[3] = 1.f;		/*w */
1847ec681f3Smrg
1857ec681f3Smrg    vertex[4] = s0;		/*s */
1867ec681f3Smrg    vertex[5] = t0;		/*t */
1877ec681f3Smrg    vertex[6] = 0.f;		/*r */
1887ec681f3Smrg    vertex[7] = 1.f;		/*q */
1897ec681f3Smrg
1907ec681f3Smrg    vertex[8] = s1;		/*s */
1917ec681f3Smrg    vertex[9] = t1;		/*t */
1927ec681f3Smrg    vertex[10] = 0.f;		/*r */
1937ec681f3Smrg    vertex[11] = 1.f;		/*q */
1947ec681f3Smrg
1957ec681f3Smrg    r->buffer_size += 12;
1967ec681f3Smrg}
1977ec681f3Smrg
1987ec681f3Smrgstatic void
1997ec681f3Smrgcompute_src_coords(float sx, float sy, const struct pipe_resource *src,
2007ec681f3Smrg                   const float *src_matrix,
2017ec681f3Smrg                   float width, float height,
2027ec681f3Smrg                   float tc0[2], float tc1[2], float tc2[2], float tc3[2])
2037ec681f3Smrg{
2047ec681f3Smrg    tc0[0] = sx;
2057ec681f3Smrg    tc0[1] = sy;
2067ec681f3Smrg    tc1[0] = sx + width;
2077ec681f3Smrg    tc1[1] = sy;
2087ec681f3Smrg    tc2[0] = sx + width;
2097ec681f3Smrg    tc2[1] = sy + height;
2107ec681f3Smrg    tc3[0] = sx;
2117ec681f3Smrg    tc3[1] = sy + height;
2127ec681f3Smrg
2137ec681f3Smrg    if (src_matrix) {
2147ec681f3Smrg	map_point(src_matrix, tc0[0], tc0[1], &tc0[0], &tc0[1]);
2157ec681f3Smrg	map_point(src_matrix, tc1[0], tc1[1], &tc1[0], &tc1[1]);
2167ec681f3Smrg	map_point(src_matrix, tc2[0], tc2[1], &tc2[0], &tc2[1]);
2177ec681f3Smrg	map_point(src_matrix, tc3[0], tc3[1], &tc3[0], &tc3[1]);
2187ec681f3Smrg    }
2197ec681f3Smrg
2207ec681f3Smrg    tc0[0] /= src->width0;
2217ec681f3Smrg    tc1[0] /= src->width0;
2227ec681f3Smrg    tc2[0] /= src->width0;
2237ec681f3Smrg    tc3[0] /= src->width0;
2247ec681f3Smrg    tc0[1] /= src->height0;
2257ec681f3Smrg    tc1[1] /= src->height0;
2267ec681f3Smrg    tc2[1] /= src->height0;
2277ec681f3Smrg    tc3[1] /= src->height0;
2287ec681f3Smrg}
2297ec681f3Smrg
2307ec681f3Smrgstatic void
2317ec681f3Smrgadd_vertex_data1(struct xa_context *r,
2327ec681f3Smrg                 float srcX, float srcY,  float dstX, float dstY,
2337ec681f3Smrg                 float width, float height,
2347ec681f3Smrg                 const struct pipe_resource *src, const float *src_matrix)
2357ec681f3Smrg{
2367ec681f3Smrg    float tc0[2], tc1[2], tc2[2], tc3[2];
2377ec681f3Smrg
2387ec681f3Smrg    compute_src_coords(srcX, srcY, src, src_matrix, width, height,
2397ec681f3Smrg                       tc0, tc1, tc2, tc3);
2407ec681f3Smrg    /* 1st vertex */
2417ec681f3Smrg    add_vertex_1tex(r, dstX, dstY, tc0[0], tc0[1]);
2427ec681f3Smrg    /* 2nd vertex */
2437ec681f3Smrg    add_vertex_1tex(r, dstX + width, dstY, tc1[0], tc1[1]);
2447ec681f3Smrg    /* 3rd vertex */
2457ec681f3Smrg    add_vertex_1tex(r, dstX + width, dstY + height, tc2[0], tc2[1]);
2467ec681f3Smrg    /* 4th vertex */
2477ec681f3Smrg    add_vertex_1tex(r, dstX, dstY + height, tc3[0], tc3[1]);
2487ec681f3Smrg}
2497ec681f3Smrg
2507ec681f3Smrgstatic void
2517ec681f3Smrgadd_vertex_data2(struct xa_context *r,
2527ec681f3Smrg                 float srcX, float srcY, float maskX, float maskY,
2537ec681f3Smrg                 float dstX, float dstY, float width, float height,
2547ec681f3Smrg                 struct pipe_resource *src,
2557ec681f3Smrg                 struct pipe_resource *mask,
2567ec681f3Smrg                 const float *src_matrix, const float *mask_matrix)
2577ec681f3Smrg{
2587ec681f3Smrg    float spt0[2], spt1[2], spt2[2], spt3[2];
2597ec681f3Smrg    float mpt0[2], mpt1[2], mpt2[2], mpt3[2];
2607ec681f3Smrg
2617ec681f3Smrg    compute_src_coords(srcX, srcY, src, src_matrix, width, height,
2627ec681f3Smrg                       spt0, spt1, spt2, spt3);
2637ec681f3Smrg    compute_src_coords(maskX, maskY, mask, mask_matrix, width, height,
2647ec681f3Smrg                       mpt0, mpt1, mpt2, mpt3);
2657ec681f3Smrg
2667ec681f3Smrg    /* 1st vertex */
2677ec681f3Smrg    add_vertex_2tex(r, dstX, dstY,
2687ec681f3Smrg		    spt0[0], spt0[1], mpt0[0], mpt0[1]);
2697ec681f3Smrg    /* 2nd vertex */
2707ec681f3Smrg    add_vertex_2tex(r, dstX + width, dstY,
2717ec681f3Smrg		    spt1[0], spt1[1], mpt1[0], mpt1[1]);
2727ec681f3Smrg    /* 3rd vertex */
2737ec681f3Smrg    add_vertex_2tex(r, dstX + width, dstY + height,
2747ec681f3Smrg		    spt2[0], spt2[1], mpt2[0], mpt2[1]);
2757ec681f3Smrg    /* 4th vertex */
2767ec681f3Smrg    add_vertex_2tex(r, dstX, dstY + height,
2777ec681f3Smrg		    spt3[0], spt3[1], mpt3[0], mpt3[1]);
2787ec681f3Smrg}
2797ec681f3Smrg
2807ec681f3Smrgstatic void
2817ec681f3Smrgsetup_vertex_data_yuv(struct xa_context *r,
2827ec681f3Smrg		      float srcX,
2837ec681f3Smrg		      float srcY,
2847ec681f3Smrg		      float srcW,
2857ec681f3Smrg		      float srcH,
2867ec681f3Smrg		      float dstX,
2877ec681f3Smrg		      float dstY,
2887ec681f3Smrg		      float dstW, float dstH, struct xa_surface *srf[])
2897ec681f3Smrg{
2907ec681f3Smrg    float s0, t0, s1, t1;
2917ec681f3Smrg    float spt0[2], spt1[2];
2927ec681f3Smrg    struct pipe_resource *tex;
2937ec681f3Smrg
2947ec681f3Smrg    spt0[0] = srcX;
2957ec681f3Smrg    spt0[1] = srcY;
2967ec681f3Smrg    spt1[0] = srcX + srcW;
2977ec681f3Smrg    spt1[1] = srcY + srcH;
2987ec681f3Smrg
2997ec681f3Smrg    tex = srf[0]->tex;
3007ec681f3Smrg    s0 = spt0[0] / tex->width0;
3017ec681f3Smrg    t0 = spt0[1] / tex->height0;
3027ec681f3Smrg    s1 = spt1[0] / tex->width0;
3037ec681f3Smrg    t1 = spt1[1] / tex->height0;
3047ec681f3Smrg
3057ec681f3Smrg    /* 1st vertex */
3067ec681f3Smrg    add_vertex_1tex(r, dstX, dstY, s0, t0);
3077ec681f3Smrg    /* 2nd vertex */
3087ec681f3Smrg    add_vertex_1tex(r, dstX + dstW, dstY, s1, t0);
3097ec681f3Smrg    /* 3rd vertex */
3107ec681f3Smrg    add_vertex_1tex(r, dstX + dstW, dstY + dstH, s1, t1);
3117ec681f3Smrg    /* 4th vertex */
3127ec681f3Smrg    add_vertex_1tex(r, dstX, dstY + dstH, s0, t1);
3137ec681f3Smrg}
3147ec681f3Smrg
3157ec681f3Smrg/* Set up framebuffer, viewport and vertex shader constant buffer
3167ec681f3Smrg * state for a particular destinaton surface.  In all our rendering,
3177ec681f3Smrg * these concepts are linked.
3187ec681f3Smrg */
3197ec681f3Smrgvoid
3207ec681f3Smrgrenderer_bind_destination(struct xa_context *r,
3217ec681f3Smrg			  struct pipe_surface *surface)
3227ec681f3Smrg{
3237ec681f3Smrg    int width = surface->width;
3247ec681f3Smrg    int height = surface->height;
3257ec681f3Smrg
3267ec681f3Smrg    struct pipe_framebuffer_state fb;
3277ec681f3Smrg    struct pipe_viewport_state viewport;
3287ec681f3Smrg
3297ec681f3Smrg    xa_scissor_reset(r);
3307ec681f3Smrg
3317ec681f3Smrg    /* Framebuffer uses actual surface width/height
3327ec681f3Smrg     */
3337ec681f3Smrg    memset(&fb, 0, sizeof fb);
3347ec681f3Smrg    fb.width = surface->width;
3357ec681f3Smrg    fb.height = surface->height;
3367ec681f3Smrg    fb.nr_cbufs = 1;
3377ec681f3Smrg    fb.cbufs[0] = surface;
3387ec681f3Smrg    fb.zsbuf = 0;
3397ec681f3Smrg
3407ec681f3Smrg    /* Viewport just touches the bit we're interested in:
3417ec681f3Smrg     */
3427ec681f3Smrg    viewport.scale[0] = width / 2.f;
3437ec681f3Smrg    viewport.scale[1] = height / 2.f;
3447ec681f3Smrg    viewport.scale[2] = 1.0;
3457ec681f3Smrg    viewport.translate[0] = width / 2.f;
3467ec681f3Smrg    viewport.translate[1] = height / 2.f;
3477ec681f3Smrg    viewport.translate[2] = 0.0;
3487ec681f3Smrg    viewport.swizzle_x = PIPE_VIEWPORT_SWIZZLE_POSITIVE_X;
3497ec681f3Smrg    viewport.swizzle_y = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y;
3507ec681f3Smrg    viewport.swizzle_z = PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z;
3517ec681f3Smrg    viewport.swizzle_w = PIPE_VIEWPORT_SWIZZLE_POSITIVE_W;
3527ec681f3Smrg
3537ec681f3Smrg    /* Constant buffer set up to match viewport dimensions:
3547ec681f3Smrg     */
3557ec681f3Smrg    if (r->fb_width != width || r->fb_height != height) {
3567ec681f3Smrg	float vs_consts[8] = {
3577ec681f3Smrg	    2.f / width, 2.f / height, 1, 1,
3587ec681f3Smrg	    -1, -1, 0, 0
3597ec681f3Smrg	};
3607ec681f3Smrg
3617ec681f3Smrg	r->fb_width = width;
3627ec681f3Smrg	r->fb_height = height;
3637ec681f3Smrg
3647ec681f3Smrg	renderer_set_constants(r, PIPE_SHADER_VERTEX,
3657ec681f3Smrg			       vs_consts, sizeof vs_consts);
3667ec681f3Smrg    }
3677ec681f3Smrg
3687ec681f3Smrg    cso_set_framebuffer(r->cso, &fb);
3697ec681f3Smrg    cso_set_viewport(r->cso, &viewport);
3707ec681f3Smrg}
3717ec681f3Smrg
3727ec681f3Smrgvoid
3737ec681f3Smrgrenderer_set_constants(struct xa_context *r,
3747ec681f3Smrg		       int shader_type, const float *params, int param_bytes)
3757ec681f3Smrg{
3767ec681f3Smrg    struct pipe_resource **cbuf =
3777ec681f3Smrg	(shader_type == PIPE_SHADER_VERTEX) ? &r->vs_const_buffer :
3787ec681f3Smrg	&r->fs_const_buffer;
3797ec681f3Smrg
3807ec681f3Smrg    pipe_resource_reference(cbuf, NULL);
3817ec681f3Smrg    *cbuf = pipe_buffer_create_const0(r->pipe->screen,
3827ec681f3Smrg                                      PIPE_BIND_CONSTANT_BUFFER,
3837ec681f3Smrg                                      PIPE_USAGE_DEFAULT,
3847ec681f3Smrg                                      param_bytes);
3857ec681f3Smrg
3867ec681f3Smrg    if (*cbuf) {
3877ec681f3Smrg	pipe_buffer_write(r->pipe, *cbuf, 0, param_bytes, params);
3887ec681f3Smrg    }
3897ec681f3Smrg    pipe_set_constant_buffer(r->pipe, shader_type, 0, *cbuf);
3907ec681f3Smrg}
3917ec681f3Smrg
3927ec681f3Smrgvoid
3937ec681f3Smrgrenderer_copy_prepare(struct xa_context *r,
3947ec681f3Smrg		      struct pipe_surface *dst_surface,
3957ec681f3Smrg		      struct pipe_resource *src_texture,
3967ec681f3Smrg		      const enum xa_formats src_xa_format,
3977ec681f3Smrg		      const enum xa_formats dst_xa_format)
3987ec681f3Smrg{
3997ec681f3Smrg    struct pipe_context *pipe = r->pipe;
4007ec681f3Smrg    struct pipe_screen *screen = pipe->screen;
4017ec681f3Smrg    struct xa_shader shader;
4027ec681f3Smrg    uint32_t fs_traits = FS_COMPOSITE;
4037ec681f3Smrg
4047ec681f3Smrg    assert(screen->is_format_supported(screen, dst_surface->format,
4057ec681f3Smrg				       PIPE_TEXTURE_2D, 0, 0,
4067ec681f3Smrg				       PIPE_BIND_RENDER_TARGET));
4077ec681f3Smrg    (void)screen;
4087ec681f3Smrg
4097ec681f3Smrg    renderer_bind_destination(r, dst_surface);
4107ec681f3Smrg
4117ec681f3Smrg    /* set misc state we care about */
4127ec681f3Smrg    {
4137ec681f3Smrg	struct pipe_blend_state blend;
4147ec681f3Smrg
4157ec681f3Smrg	memset(&blend, 0, sizeof(blend));
4167ec681f3Smrg	blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
4177ec681f3Smrg	blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
4187ec681f3Smrg	blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
4197ec681f3Smrg	blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
4207ec681f3Smrg	blend.rt[0].colormask = PIPE_MASK_RGBA;
4217ec681f3Smrg	cso_set_blend(r->cso, &blend);
4227ec681f3Smrg    }
4237ec681f3Smrg
4247ec681f3Smrg    /* sampler */
4257ec681f3Smrg    {
4267ec681f3Smrg	struct pipe_sampler_state sampler;
4277ec681f3Smrg        const struct pipe_sampler_state *p_sampler = &sampler;
4287ec681f3Smrg
4297ec681f3Smrg	memset(&sampler, 0, sizeof(sampler));
4307ec681f3Smrg	sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
4317ec681f3Smrg	sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
4327ec681f3Smrg	sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
4337ec681f3Smrg	sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
4347ec681f3Smrg	sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
4357ec681f3Smrg	sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
4367ec681f3Smrg	sampler.normalized_coords = 1;
4377ec681f3Smrg        cso_set_samplers(r->cso, PIPE_SHADER_FRAGMENT, 1, &p_sampler);
4387ec681f3Smrg        r->num_bound_samplers = 1;
4397ec681f3Smrg    }
4407ec681f3Smrg
4417ec681f3Smrg    /* texture/sampler view */
4427ec681f3Smrg    {
4437ec681f3Smrg	struct pipe_sampler_view templ;
4447ec681f3Smrg	struct pipe_sampler_view *src_view;
4457ec681f3Smrg
4467ec681f3Smrg	u_sampler_view_default_template(&templ,
4477ec681f3Smrg					src_texture, src_texture->format);
4487ec681f3Smrg	src_view = pipe->create_sampler_view(pipe, src_texture, &templ);
4497ec681f3Smrg	pipe->set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, 1, 0, false, &src_view);
4507ec681f3Smrg	pipe_sampler_view_reference(&src_view, NULL);
4517ec681f3Smrg    }
4527ec681f3Smrg
4537ec681f3Smrg    /* shaders */
4547ec681f3Smrg    if (src_texture->format == PIPE_FORMAT_L8_UNORM ||
4557ec681f3Smrg        src_texture->format == PIPE_FORMAT_R8_UNORM)
4567ec681f3Smrg	fs_traits |= FS_SRC_LUMINANCE;
4577ec681f3Smrg    if (dst_surface->format == PIPE_FORMAT_L8_UNORM ||
4587ec681f3Smrg        dst_surface->format == PIPE_FORMAT_R8_UNORM)
4597ec681f3Smrg	fs_traits |= FS_DST_LUMINANCE;
4607ec681f3Smrg    if (xa_format_a(dst_xa_format) != 0 &&
4617ec681f3Smrg	xa_format_a(src_xa_format) == 0)
4627ec681f3Smrg	fs_traits |= FS_SRC_SET_ALPHA;
4637ec681f3Smrg
4647ec681f3Smrg    shader = xa_shaders_get(r->shaders, VS_COMPOSITE, fs_traits);
4657ec681f3Smrg    cso_set_vertex_shader_handle(r->cso, shader.vs);
4667ec681f3Smrg    cso_set_fragment_shader_handle(r->cso, shader.fs);
4677ec681f3Smrg
4687ec681f3Smrg    r->buffer_size = 0;
4697ec681f3Smrg    r->attrs_per_vertex = 2;
4707ec681f3Smrg}
4717ec681f3Smrg
4727ec681f3Smrgvoid
4737ec681f3Smrgrenderer_copy(struct xa_context *r,
4747ec681f3Smrg	      int dx,
4757ec681f3Smrg	      int dy,
4767ec681f3Smrg	      int sx,
4777ec681f3Smrg	      int sy,
4787ec681f3Smrg	      int width, int height, float src_width, float src_height)
4797ec681f3Smrg{
4807ec681f3Smrg    float s0, t0, s1, t1;
4817ec681f3Smrg    float x0, y0, x1, y1;
4827ec681f3Smrg
4837ec681f3Smrg    /* XXX: could put the texcoord scaling calculation into the vertex
4847ec681f3Smrg     * shader.
4857ec681f3Smrg     */
4867ec681f3Smrg    s0 = sx / src_width;
4877ec681f3Smrg    s1 = (sx + width) / src_width;
4887ec681f3Smrg    t0 = sy / src_height;
4897ec681f3Smrg    t1 = (sy + height) / src_height;
4907ec681f3Smrg
4917ec681f3Smrg    x0 = dx;
4927ec681f3Smrg    x1 = dx + width;
4937ec681f3Smrg    y0 = dy;
4947ec681f3Smrg    y1 = dy + height;
4957ec681f3Smrg
4967ec681f3Smrg    /* draw quad */
4977ec681f3Smrg    renderer_draw_conditional(r, 4 * 8);
4987ec681f3Smrg    add_vertex_1tex(r, x0, y0, s0, t0);
4997ec681f3Smrg    add_vertex_1tex(r, x1, y0, s1, t0);
5007ec681f3Smrg    add_vertex_1tex(r, x1, y1, s1, t1);
5017ec681f3Smrg    add_vertex_1tex(r, x0, y1, s0, t1);
5027ec681f3Smrg}
5037ec681f3Smrg
5047ec681f3Smrgvoid
5057ec681f3Smrgrenderer_draw_yuv(struct xa_context *r,
5067ec681f3Smrg		  float src_x,
5077ec681f3Smrg		  float src_y,
5087ec681f3Smrg		  float src_w,
5097ec681f3Smrg		  float src_h,
5107ec681f3Smrg		  int dst_x,
5117ec681f3Smrg		  int dst_y, int dst_w, int dst_h, struct xa_surface *srf[])
5127ec681f3Smrg{
5137ec681f3Smrg   const int num_attribs = 2;	/*pos + tex coord */
5147ec681f3Smrg
5157ec681f3Smrg   setup_vertex_data_yuv(r,
5167ec681f3Smrg                         src_x, src_y, src_w, src_h,
5177ec681f3Smrg                         dst_x, dst_y, dst_w, dst_h, srf);
5187ec681f3Smrg
5197ec681f3Smrg   if (!r->scissor_valid) {
5207ec681f3Smrg       r->scissor.minx = 0;
5217ec681f3Smrg       r->scissor.miny = 0;
5227ec681f3Smrg       r->scissor.maxx = r->dst->tex->width0;
5237ec681f3Smrg       r->scissor.maxy = r->dst->tex->height0;
5247ec681f3Smrg   }
5257ec681f3Smrg
5267ec681f3Smrg   r->pipe->set_scissor_states(r->pipe, 0, 1, &r->scissor);
5277ec681f3Smrg
5287ec681f3Smrg   struct cso_velems_state velems;
5297ec681f3Smrg   velems.count = num_attribs;
5307ec681f3Smrg   memcpy(velems.velems, r->velems, sizeof(r->velems[0]) * velems.count);
5317ec681f3Smrg
5327ec681f3Smrg   cso_set_vertex_elements(r->cso, &velems);
5337ec681f3Smrg   util_draw_user_vertex_buffer(r->cso, r->buffer, PIPE_PRIM_QUADS,
5347ec681f3Smrg                                4,	/* verts */
5357ec681f3Smrg                                num_attribs);	/* attribs/vert */
5367ec681f3Smrg   r->buffer_size = 0;
5377ec681f3Smrg
5387ec681f3Smrg   xa_scissor_reset(r);
5397ec681f3Smrg}
5407ec681f3Smrg
5417ec681f3Smrgvoid
5427ec681f3Smrgrenderer_begin_solid(struct xa_context *r)
5437ec681f3Smrg{
5447ec681f3Smrg    r->buffer_size = 0;
5457ec681f3Smrg    r->attrs_per_vertex = 1;
5467ec681f3Smrg    renderer_set_constants(r, PIPE_SHADER_FRAGMENT, r->solid_color,
5477ec681f3Smrg                           4 * sizeof(float));
5487ec681f3Smrg}
5497ec681f3Smrg
5507ec681f3Smrgvoid
5517ec681f3Smrgrenderer_solid(struct xa_context *r,
5527ec681f3Smrg	       int x0, int y0, int x1, int y1)
5537ec681f3Smrg{
5547ec681f3Smrg    /*
5557ec681f3Smrg     * debug_printf("solid rect[(%d, %d), (%d, %d)], rgba[%f, %f, %f, %f]\n",
5567ec681f3Smrg     * x0, y0, x1, y1, color[0], color[1], color[2], color[3]); */
5577ec681f3Smrg
5587ec681f3Smrg    renderer_draw_conditional(r, 4 * 4);
5597ec681f3Smrg
5607ec681f3Smrg    /* 1st vertex */
5617ec681f3Smrg    add_vertex_none(r, x0, y0);
5627ec681f3Smrg    /* 2nd vertex */
5637ec681f3Smrg    add_vertex_none(r, x1, y0);
5647ec681f3Smrg    /* 3rd vertex */
5657ec681f3Smrg    add_vertex_none(r, x1, y1);
5667ec681f3Smrg    /* 4th vertex */
5677ec681f3Smrg    add_vertex_none(r, x0, y1);
5687ec681f3Smrg}
5697ec681f3Smrg
5707ec681f3Smrgvoid
5717ec681f3Smrgrenderer_draw_flush(struct xa_context *r)
5727ec681f3Smrg{
5737ec681f3Smrg    renderer_draw_conditional(r, 0);
5747ec681f3Smrg}
5757ec681f3Smrg
5767ec681f3Smrgvoid
5777ec681f3Smrgrenderer_begin_textures(struct xa_context *r)
5787ec681f3Smrg{
5797ec681f3Smrg    r->attrs_per_vertex = 1 + r->num_bound_samplers;
5807ec681f3Smrg    r->buffer_size = 0;
5817ec681f3Smrg    if (r->has_solid_src || r->has_solid_mask)
5827ec681f3Smrg       renderer_set_constants(r, PIPE_SHADER_FRAGMENT, r->solid_color,
5837ec681f3Smrg                              4 * sizeof(float));
5847ec681f3Smrg}
5857ec681f3Smrg
5867ec681f3Smrgvoid
5877ec681f3Smrgrenderer_texture(struct xa_context *r,
5887ec681f3Smrg		 int *pos,
5897ec681f3Smrg		 int width, int height,
5907ec681f3Smrg		 const float *src_matrix,
5917ec681f3Smrg		 const float *mask_matrix)
5927ec681f3Smrg{
5937ec681f3Smrg    struct pipe_sampler_view **sampler_view = r->bound_sampler_views;
5947ec681f3Smrg
5957ec681f3Smrg#if 0
5967ec681f3Smrg    if (src_matrix) {
5977ec681f3Smrg	debug_printf("src_matrix = \n");
5987ec681f3Smrg	debug_printf("%f, %f, %f\n", src_matrix[0], src_matrix[1], src_matrix[2]);
5997ec681f3Smrg	debug_printf("%f, %f, %f\n", src_matrix[3], src_matrix[4], src_matrix[5]);
6007ec681f3Smrg	debug_printf("%f, %f, %f\n", src_matrix[6], src_matrix[7], src_matrix[8]);
6017ec681f3Smrg    }
6027ec681f3Smrg    if (mask_matrix) {
6037ec681f3Smrg	debug_printf("mask_matrix = \n");
6047ec681f3Smrg	debug_printf("%f, %f, %f\n", mask_matrix[0], mask_matrix[1], mask_matrix[2]);
6057ec681f3Smrg	debug_printf("%f, %f, %f\n", mask_matrix[3], mask_matrix[4], mask_matrix[5]);
6067ec681f3Smrg	debug_printf("%f, %f, %f\n", mask_matrix[6], mask_matrix[7], mask_matrix[8]);
6077ec681f3Smrg    }
6087ec681f3Smrg#endif
6097ec681f3Smrg
6107ec681f3Smrg    switch(r->attrs_per_vertex) {
6117ec681f3Smrg    case 2:
6127ec681f3Smrg	renderer_draw_conditional(r, 4 * 8);
6137ec681f3Smrg        if (!r->has_solid_src) {
6147ec681f3Smrg           add_vertex_data1(r,
6157ec681f3Smrg                            pos[0], pos[1], /* src */
6167ec681f3Smrg                            pos[4], pos[5], /* dst */
6177ec681f3Smrg                            width, height,
6187ec681f3Smrg                            sampler_view[0]->texture, src_matrix);
6197ec681f3Smrg        } else {
6207ec681f3Smrg           add_vertex_data1(r,
6217ec681f3Smrg                            pos[2], pos[3], /* mask */
6227ec681f3Smrg                            pos[4], pos[5], /* dst */
6237ec681f3Smrg                            width, height,
6247ec681f3Smrg                            sampler_view[0]->texture, mask_matrix);
6257ec681f3Smrg        }
6267ec681f3Smrg	break;
6277ec681f3Smrg    case 3:
6287ec681f3Smrg	renderer_draw_conditional(r, 4 * 12);
6297ec681f3Smrg	add_vertex_data2(r,
6307ec681f3Smrg			 pos[0], pos[1], /* src */
6317ec681f3Smrg			 pos[2], pos[3], /* mask */
6327ec681f3Smrg			 pos[4], pos[5], /* dst */
6337ec681f3Smrg			 width, height,
6347ec681f3Smrg			 sampler_view[0]->texture, sampler_view[1]->texture,
6357ec681f3Smrg			 src_matrix, mask_matrix);
6367ec681f3Smrg	break;
6377ec681f3Smrg    default:
6387ec681f3Smrg	break;
6397ec681f3Smrg    }
6407ec681f3Smrg}
641