81b0dcf656f9789f47b20e66a27ab00b92aba654
[profile/ivi/mesa.git] / src / gallium / state_trackers / xorg / xorg_renderer.c
1 #include "xorg_exa.h"
2 #include "xorg_renderer.h"
3
4 #include "xorg_exa_tgsi.h"
5
6 #include "cso_cache/cso_context.h"
7 #include "util/u_draw_quad.h"
8 #include "util/u_math.h"
9 #include "util/u_memory.h"
10 #include "util/u_rect.h"
11 #include "util/u_sampler.h"
12
13 #include "util/u_inlines.h"
14
15 #include <math.h>
16
17 #define floatsEqual(x, y) (fabs(x - y) <= 0.00001f * MIN2(fabs(x), fabs(y)))
18 #define floatIsZero(x) (floatsEqual((x) + 1, 1))
19
20 #define NUM_COMPONENTS 4
21
22 static INLINE boolean is_affine(float *matrix)
23 {
24    return floatIsZero(matrix[2]) && floatIsZero(matrix[5])
25       && floatsEqual(matrix[8], 1);
26 }
27 static INLINE void map_point(float *mat, float x, float y,
28                              float *out_x, float *out_y)
29 {
30    if (!mat) {
31       *out_x = x;
32       *out_y = y;
33       return;
34    }
35
36    *out_x = mat[0]*x + mat[3]*y + mat[6];
37    *out_y = mat[1]*x + mat[4]*y + mat[7];
38    if (!is_affine(mat)) {
39       float w = 1/(mat[2]*x + mat[5]*y + mat[8]);
40       *out_x *= w;
41       *out_y *= w;
42    }
43 }
44
45 static INLINE struct pipe_buffer *
46 renderer_buffer_create(struct xorg_renderer *r)
47 {
48    struct pipe_buffer *buf =
49       pipe_user_buffer_create(r->pipe->screen,
50                               r->buffer,
51                               sizeof(float)*
52                               r->buffer_size);
53    r->buffer_size = 0;
54
55    return buf;
56 }
57
58 static INLINE void
59 renderer_draw(struct xorg_renderer *r)
60 {
61    struct pipe_context *pipe = r->pipe;
62    struct pipe_buffer *buf = 0;
63    int num_verts = r->buffer_size/(r->attrs_per_vertex * NUM_COMPONENTS);
64
65    if (!r->buffer_size)
66       return;
67
68    buf = renderer_buffer_create(r);
69
70
71    if (buf) {
72       cso_set_vertex_elements(r->cso, r->attrs_per_vertex, r->velems);
73
74       util_draw_vertex_buffer(pipe, buf, 0,
75                               PIPE_PRIM_QUADS,
76                               num_verts,  /* verts */
77                               r->attrs_per_vertex); /* attribs/vert */
78
79       pipe_buffer_reference(&buf, NULL);
80    }
81 }
82
83 static INLINE void
84 renderer_draw_conditional(struct xorg_renderer *r,
85                           int next_batch)
86 {
87    if (r->buffer_size + next_batch >= BUF_SIZE ||
88        (next_batch == 0 && r->buffer_size)) {
89       renderer_draw(r);
90    }
91 }
92
93 static void
94 renderer_init_state(struct xorg_renderer *r)
95 {
96    struct pipe_depth_stencil_alpha_state dsa;
97    struct pipe_rasterizer_state raster;
98    unsigned i;
99
100    /* set common initial clip state */
101    memset(&dsa, 0, sizeof(struct pipe_depth_stencil_alpha_state));
102    cso_set_depth_stencil_alpha(r->cso, &dsa);
103
104
105    /* XXX: move to renderer_init_state? */
106    memset(&raster, 0, sizeof(struct pipe_rasterizer_state));
107    raster.gl_rasterization_rules = 1;
108    cso_set_rasterizer(r->cso, &raster);
109
110    /* vertex elements state */
111    memset(&r->velems[0], 0, sizeof(r->velems[0]) * 3);
112    for (i = 0; i < 3; i++) {
113       r->velems[i].src_offset = i * 4 * sizeof(float);
114       r->velems[i].instance_divisor = 0;
115       r->velems[i].vertex_buffer_index = 0;
116       r->velems[i].src_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
117    }
118 }
119
120
121 static INLINE void
122 add_vertex_color(struct xorg_renderer *r,
123                  float x, float y,
124                  float color[4])
125 {
126    float *vertex = r->buffer + r->buffer_size;
127
128    vertex[0] = x;
129    vertex[1] = y;
130    vertex[2] = 0.f; /*z*/
131    vertex[3] = 1.f; /*w*/
132
133    vertex[4] = color[0]; /*r*/
134    vertex[5] = color[1]; /*g*/
135    vertex[6] = color[2]; /*b*/
136    vertex[7] = color[3]; /*a*/
137
138    r->buffer_size += 8;
139 }
140
141 static INLINE void
142 add_vertex_1tex(struct xorg_renderer *r,
143                 float x, float y, float s, float t)
144 {
145    float *vertex = r->buffer + r->buffer_size;
146
147    vertex[0] = x;
148    vertex[1] = y;
149    vertex[2] = 0.f; /*z*/
150    vertex[3] = 1.f; /*w*/
151
152    vertex[4] = s;   /*s*/
153    vertex[5] = t;   /*t*/
154    vertex[6] = 0.f; /*r*/
155    vertex[7] = 1.f; /*q*/
156
157    r->buffer_size += 8;
158 }
159
160 static void
161 add_vertex_data1(struct xorg_renderer *r,
162                  float srcX, float srcY,  float dstX, float dstY,
163                  float width, float height,
164                  struct pipe_texture *src, float *src_matrix)
165 {
166    float s0, t0, s1, t1, s2, t2, s3, t3;
167    float pt0[2], pt1[2], pt2[2], pt3[2];
168
169    pt0[0] = srcX;
170    pt0[1] = srcY;
171    pt1[0] = (srcX + width);
172    pt1[1] = srcY;
173    pt2[0] = (srcX + width);
174    pt2[1] = (srcY + height);
175    pt3[0] = srcX;
176    pt3[1] = (srcY + height);
177
178    if (src_matrix) {
179       map_point(src_matrix, pt0[0], pt0[1], &pt0[0], &pt0[1]);
180       map_point(src_matrix, pt1[0], pt1[1], &pt1[0], &pt1[1]);
181       map_point(src_matrix, pt2[0], pt2[1], &pt2[0], &pt2[1]);
182       map_point(src_matrix, pt3[0], pt3[1], &pt3[0], &pt3[1]);
183    }
184
185    s0 =  pt0[0] / src->width0;
186    s1 =  pt1[0] / src->width0;
187    s2 =  pt2[0] / src->width0;
188    s3 =  pt3[0] / src->width0;
189    t0 =  pt0[1] / src->height0;
190    t1 =  pt1[1] / src->height0;
191    t2 =  pt2[1] / src->height0;
192    t3 =  pt3[1] / src->height0;
193
194    /* 1st vertex */
195    add_vertex_1tex(r, dstX, dstY, s0, t0);
196    /* 2nd vertex */
197    add_vertex_1tex(r, dstX + width, dstY, s1, t1);
198    /* 3rd vertex */
199    add_vertex_1tex(r, dstX + width, dstY + height, s2, t2);
200    /* 4th vertex */
201    add_vertex_1tex(r, dstX, dstY + height, s3, t3);
202 }
203
204
205 static INLINE void
206 add_vertex_2tex(struct xorg_renderer *r,
207                 float x, float y,
208                 float s0, float t0, float s1, float t1)
209 {
210    float *vertex = r->buffer + r->buffer_size;
211
212    vertex[0] = x;
213    vertex[1] = y;
214    vertex[2] = 0.f; /*z*/
215    vertex[3] = 1.f; /*w*/
216
217    vertex[4] = s0;  /*s*/
218    vertex[5] = t0;  /*t*/
219    vertex[6] = 0.f; /*r*/
220    vertex[7] = 1.f; /*q*/
221
222    vertex[8] = s1;  /*s*/
223    vertex[9] = t1;  /*t*/
224    vertex[10] = 0.f; /*r*/
225    vertex[11] = 1.f; /*q*/
226
227    r->buffer_size += 12;
228 }
229
230 static void
231 add_vertex_data2(struct xorg_renderer *r,
232                  float srcX, float srcY, float maskX, float maskY,
233                  float dstX, float dstY, float width, float height,
234                  struct pipe_texture *src,
235                  struct pipe_texture *mask,
236                  float *src_matrix, float *mask_matrix)
237 {
238    float src_s0, src_t0, src_s1, src_t1;
239    float mask_s0, mask_t0, mask_s1, mask_t1;
240    float spt0[2], spt1[2];
241    float mpt0[2], mpt1[2];
242
243    spt0[0] = srcX;
244    spt0[1] = srcY;
245    spt1[0] = srcX + width;
246    spt1[1] = srcY + height;
247
248    mpt0[0] = maskX;
249    mpt0[1] = maskY;
250    mpt1[0] = maskX + width;
251    mpt1[1] = maskY + height;
252
253    if (src_matrix) {
254       map_point(src_matrix, spt0[0], spt0[1], &spt0[0], &spt0[1]);
255       map_point(src_matrix, spt1[0], spt1[1], &spt1[0], &spt1[1]);
256    }
257
258    if (mask_matrix) {
259       map_point(mask_matrix, mpt0[0], mpt0[1], &mpt0[0], &mpt0[1]);
260       map_point(mask_matrix, mpt1[0], mpt1[1], &mpt1[0], &mpt1[1]);
261    }
262
263    src_s0 = spt0[0] / src->width0;
264    src_t0 = spt0[1] / src->height0;
265    src_s1 = spt1[0] / src->width0;
266    src_t1 = spt1[1] / src->height0;
267
268    mask_s0 = mpt0[0] / mask->width0;
269    mask_t0 = mpt0[1] / mask->height0;
270    mask_s1 = mpt1[0] / mask->width0;
271    mask_t1 = mpt1[1] / mask->height0;
272
273    /* 1st vertex */
274    add_vertex_2tex(r, dstX, dstY,
275                    src_s0, src_t0, mask_s0, mask_t0);
276    /* 2nd vertex */
277    add_vertex_2tex(r, dstX + width, dstY,
278                    src_s1, src_t0, mask_s1, mask_t0);
279    /* 3rd vertex */
280    add_vertex_2tex(r, dstX + width, dstY + height,
281                    src_s1, src_t1, mask_s1, mask_t1);
282    /* 4th vertex */
283    add_vertex_2tex(r, dstX, dstY + height,
284                    src_s0, src_t1, mask_s0, mask_t1);
285 }
286
287 static struct pipe_buffer *
288 setup_vertex_data_yuv(struct xorg_renderer *r,
289                       float srcX, float srcY, float srcW, float srcH,
290                       float dstX, float dstY, float dstW, float dstH,
291                       struct pipe_texture **tex)
292 {
293    float s0, t0, s1, t1;
294    float spt0[2], spt1[2];
295
296    spt0[0] = srcX;
297    spt0[1] = srcY;
298    spt1[0] = srcX + srcW;
299    spt1[1] = srcY + srcH;
300
301    s0 = spt0[0] / tex[0]->width0;
302    t0 = spt0[1] / tex[0]->height0;
303    s1 = spt1[0] / tex[0]->width0;
304    t1 = spt1[1] / tex[0]->height0;
305
306    /* 1st vertex */
307    add_vertex_1tex(r, dstX, dstY, s0, t0);
308    /* 2nd vertex */
309    add_vertex_1tex(r, dstX + dstW, dstY,
310                    s1, t0);
311    /* 3rd vertex */
312    add_vertex_1tex(r, dstX + dstW, dstY + dstH,
313                    s1, t1);
314    /* 4th vertex */
315    add_vertex_1tex(r, dstX, dstY + dstH,
316                    s0, t1);
317
318    return renderer_buffer_create(r);
319 }
320
321
322
323 /* Set up framebuffer, viewport and vertex shader constant buffer
324  * state for a particular destinaton surface.  In all our rendering,
325  * these concepts are linked.
326  */
327 void renderer_bind_destination(struct xorg_renderer *r,
328                                struct pipe_surface *surface,
329                                int width,
330                                int height )
331 {
332
333    struct pipe_framebuffer_state fb;
334    struct pipe_viewport_state viewport;
335
336    /* Framebuffer uses actual surface width/height
337     */
338    memset(&fb, 0, sizeof fb);
339    fb.width  = surface->width;
340    fb.height = surface->height;
341    fb.nr_cbufs = 1;
342    fb.cbufs[0] = surface;
343    fb.zsbuf = 0;
344
345    /* Viewport just touches the bit we're interested in:
346     */
347    viewport.scale[0] =  width / 2.f;
348    viewport.scale[1] =  height / 2.f;
349    viewport.scale[2] =  1.0;
350    viewport.scale[3] =  1.0;
351    viewport.translate[0] = width / 2.f;
352    viewport.translate[1] = height / 2.f;
353    viewport.translate[2] = 0.0;
354    viewport.translate[3] = 0.0;
355
356    /* Constant buffer set up to match viewport dimensions:
357     */
358    if (r->fb_width != width ||
359        r->fb_height != height) 
360    {
361       float vs_consts[8] = {
362          2.f/width, 2.f/height, 1, 1,
363          -1, -1, 0, 0
364       };
365
366       r->fb_width = width;
367       r->fb_height = height;
368
369       renderer_set_constants(r, PIPE_SHADER_VERTEX,
370                              vs_consts, sizeof vs_consts);
371    }
372
373    cso_set_framebuffer(r->cso, &fb);
374    cso_set_viewport(r->cso, &viewport);
375 }
376
377
378 struct xorg_renderer * renderer_create(struct pipe_context *pipe)
379 {
380    struct xorg_renderer *renderer = CALLOC_STRUCT(xorg_renderer);
381
382    renderer->pipe = pipe;
383    renderer->cso = cso_create_context(pipe);
384    renderer->shaders = xorg_shaders_create(renderer);
385
386    renderer_init_state(renderer);
387
388    return renderer;
389 }
390
391 void renderer_destroy(struct xorg_renderer *r)
392 {
393    struct pipe_buffer **vsbuf = &r->vs_const_buffer;
394    struct pipe_buffer **fsbuf = &r->fs_const_buffer;
395
396    if (*vsbuf)
397       pipe_buffer_reference(vsbuf, NULL);
398
399    if (*fsbuf)
400       pipe_buffer_reference(fsbuf, NULL);
401
402    if (r->shaders) {
403       xorg_shaders_destroy(r->shaders);
404       r->shaders = NULL;
405    }
406
407    if (r->cso) {
408       cso_release_all(r->cso);
409       cso_destroy_context(r->cso);
410       r->cso = NULL;
411    }
412 }
413
414
415
416
417
418 void renderer_set_constants(struct xorg_renderer *r,
419                             int shader_type,
420                             const float *params,
421                             int param_bytes)
422 {
423    struct pipe_buffer **cbuf =
424       (shader_type == PIPE_SHADER_VERTEX) ? &r->vs_const_buffer :
425       &r->fs_const_buffer;
426
427    pipe_buffer_reference(cbuf, NULL);
428    *cbuf = pipe_buffer_create(r->pipe->screen, 16,
429                               PIPE_BUFFER_USAGE_CONSTANT,
430                               param_bytes);
431
432    if (*cbuf) {
433       pipe_buffer_write(r->pipe->screen, *cbuf,
434                         0, param_bytes, params);
435    }
436    r->pipe->set_constant_buffer(r->pipe, shader_type, 0, *cbuf);
437 }
438
439
440 void renderer_copy_prepare(struct xorg_renderer *r,
441                            struct pipe_surface *dst_surface,
442                            struct pipe_texture *src_texture)
443 {
444    struct pipe_context *pipe = r->pipe;
445    struct pipe_screen *screen = pipe->screen;
446    struct xorg_shader shader;
447
448    assert(screen->is_format_supported(screen, dst_surface->format,
449                                       PIPE_TEXTURE_2D,
450                                       PIPE_TEXTURE_USAGE_RENDER_TARGET,
451                                       0));
452    (void) screen;
453
454
455    /* set misc state we care about */
456    {
457       struct pipe_blend_state blend;
458       memset(&blend, 0, sizeof(blend));
459       blend.rt[0].rgb_src_factor = PIPE_BLENDFACTOR_ONE;
460       blend.rt[0].alpha_src_factor = PIPE_BLENDFACTOR_ONE;
461       blend.rt[0].rgb_dst_factor = PIPE_BLENDFACTOR_ZERO;
462       blend.rt[0].alpha_dst_factor = PIPE_BLENDFACTOR_ZERO;
463       blend.rt[0].colormask = PIPE_MASK_RGBA;
464       cso_set_blend(r->cso, &blend);
465    }
466
467    /* sampler */
468    {
469       struct pipe_sampler_state sampler;
470       memset(&sampler, 0, sizeof(sampler));
471       sampler.wrap_s = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
472       sampler.wrap_t = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
473       sampler.wrap_r = PIPE_TEX_WRAP_CLAMP_TO_EDGE;
474       sampler.min_mip_filter = PIPE_TEX_MIPFILTER_NONE;
475       sampler.min_img_filter = PIPE_TEX_FILTER_NEAREST;
476       sampler.mag_img_filter = PIPE_TEX_FILTER_NEAREST;
477       sampler.normalized_coords = 1;
478       cso_single_sampler(r->cso, 0, &sampler);
479       cso_single_sampler_done(r->cso);
480    }
481
482    renderer_bind_destination(r, dst_surface, 
483                              dst_surface->width,
484                              dst_surface->height);
485
486    /* texture/sampler view */
487    {
488       struct pipe_sampler_view templ;
489       struct pipe_sampler_view *src_view;
490       u_sampler_view_default_template(&templ,
491                                       src_texture,
492                                       src_texture->format);
493       src_view = pipe->create_sampler_view(pipe, src_texture, &templ);
494       cso_set_fragment_sampler_views(r->cso, 1, &src_view);
495       pipe_sampler_view_reference(&src_view, NULL);
496    }
497
498    /* shaders */
499    shader = xorg_shaders_get(r->shaders,
500                              VS_COMPOSITE,
501                              FS_COMPOSITE);
502    cso_set_vertex_shader_handle(r->cso, shader.vs);
503    cso_set_fragment_shader_handle(r->cso, shader.fs);
504
505    r->buffer_size = 0;
506    r->attrs_per_vertex = 2;
507 }
508
509 struct pipe_texture *
510 renderer_clone_texture(struct xorg_renderer *r,
511                        struct pipe_texture *src)
512 {
513    enum pipe_format format;
514    struct pipe_context *pipe = r->pipe;
515    struct pipe_screen *screen = pipe->screen;
516    struct pipe_texture *pt;
517    struct pipe_texture templ;
518
519    if (pipe->is_texture_referenced(pipe, src, 0, 0) &
520        PIPE_REFERENCED_FOR_WRITE)
521       pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, NULL);
522
523    /* the coming in texture should already have that invariance */
524    debug_assert(screen->is_format_supported(screen, src->format,
525                                             PIPE_TEXTURE_2D,
526                                             PIPE_TEXTURE_USAGE_SAMPLER, 0));
527
528    format = src->format;
529
530    memset(&templ, 0, sizeof(templ));
531    templ.target = PIPE_TEXTURE_2D;
532    templ.format = format;
533    templ.last_level = 0;
534    templ.width0 = src->width0;
535    templ.height0 = src->height0;
536    templ.depth0 = 1;
537    templ.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER;
538
539    pt = screen->texture_create(screen, &templ);
540
541    debug_assert(!pt || pipe_is_referenced(&pt->reference));
542
543    if (!pt)
544       return NULL;
545
546    {
547       /* copy source framebuffer surface into texture */
548       struct pipe_surface *ps_read = screen->get_tex_surface(
549          screen, src, 0, 0, 0, PIPE_BUFFER_USAGE_GPU_READ);
550       struct pipe_surface *ps_tex = screen->get_tex_surface(
551          screen, pt, 0, 0, 0, PIPE_BUFFER_USAGE_GPU_WRITE );
552       if (pipe->surface_copy) {
553          pipe->surface_copy(pipe,
554                 ps_tex, /* dest */
555                 0, 0, /* destx/y */
556                 ps_read,
557                 0, 0, src->width0, src->height0);
558       } else {
559           util_surface_copy(pipe, FALSE,
560                 ps_tex, /* dest */
561                 0, 0, /* destx/y */
562                 ps_read,
563                 0, 0, src->width0, src->height0);
564       }
565       pipe_surface_reference(&ps_read, NULL);
566       pipe_surface_reference(&ps_tex, NULL);
567    }
568
569    return pt;
570 }
571
572
573 void renderer_copy_pixmap(struct xorg_renderer *r,
574                           int dx, int dy,
575                           int sx, int sy,
576                           int width, int height,
577                           float src_width,
578                           float src_height)
579 {
580    float s0, t0, s1, t1;
581    float x0, y0, x1, y1;
582
583
584    /* XXX: could put the texcoord scaling calculation into the vertex
585     * shader.
586     */
587    s0 = sx            / src_width;
588    s1 = (sx + width)  / src_width;
589    t0 = sy            / src_height;
590    t1 = (sy + height) / src_height;
591
592    x0 = dx;
593    x1 = dx + width;
594    y0 = dy;
595    y1 = dy + height;
596
597    /* draw quad */
598    renderer_draw_conditional(r, 4*8);
599    add_vertex_1tex(r, x0, y0, s0, t0);
600    add_vertex_1tex(r, x1, y0, s1, t0);
601    add_vertex_1tex(r, x1, y1, s1, t1);
602    add_vertex_1tex(r, x0, y1, s0, t1);
603 }
604
605
606
607
608 void renderer_draw_yuv(struct xorg_renderer *r,
609                        int src_x, int src_y, int src_w, int src_h,
610                        int dst_x, int dst_y, int dst_w, int dst_h,
611                        struct pipe_texture **textures)
612 {
613    struct pipe_context *pipe = r->pipe;
614    struct pipe_buffer *buf = 0;
615
616    buf = setup_vertex_data_yuv(r,
617                                src_x, src_y, src_w, src_h,
618                                dst_x, dst_y, dst_w, dst_h,
619                                textures);
620
621    if (buf) {
622       const int num_attribs = 2; /*pos + tex coord*/
623
624       cso_set_vertex_elements(r->cso, num_attribs, r->velems);
625
626       util_draw_vertex_buffer(pipe, buf, 0,
627                               PIPE_PRIM_QUADS,
628                               4,  /* verts */
629                               num_attribs); /* attribs/vert */
630
631       pipe_buffer_reference(&buf, NULL);
632    }
633 }
634
635 void renderer_begin_solid(struct xorg_renderer *r)
636 {
637    r->buffer_size = 0;
638    r->attrs_per_vertex = 2;
639 }
640
641 void renderer_solid(struct xorg_renderer *r,
642                     int x0, int y0,
643                     int x1, int y1,
644                     float *color)
645 {
646    /*
647    debug_printf("solid rect[(%d, %d), (%d, %d)], rgba[%f, %f, %f, %f]\n",
648    x0, y0, x1, y1, color[0], color[1], color[2], color[3]);*/
649
650    renderer_draw_conditional(r, 4 * 8);
651
652    /* 1st vertex */
653    add_vertex_color(r, x0, y0, color);
654    /* 2nd vertex */
655    add_vertex_color(r, x1, y0, color);
656    /* 3rd vertex */
657    add_vertex_color(r, x1, y1, color);
658    /* 4th vertex */
659    add_vertex_color(r, x0, y1, color);
660 }
661
662 void renderer_draw_flush(struct xorg_renderer *r)
663 {
664    renderer_draw_conditional(r, 0);
665 }
666
667 void renderer_begin_textures(struct xorg_renderer *r,
668                              int num_textures)
669 {
670    r->attrs_per_vertex = 1 + num_textures;
671    r->buffer_size = 0;
672 }
673
674 void renderer_texture(struct xorg_renderer *r,
675                       int *pos,
676                       int width, int height,
677                       struct pipe_sampler_view **sampler_view,
678                       int num_textures,
679                       float *src_matrix,
680                       float *mask_matrix)
681 {
682
683 #if 0
684    if (src_matrix) {
685       debug_printf("src_matrix = \n");
686       debug_printf("%f, %f, %f\n", src_matrix[0], src_matrix[1], src_matrix[2]);
687       debug_printf("%f, %f, %f\n", src_matrix[3], src_matrix[4], src_matrix[5]);
688       debug_printf("%f, %f, %f\n", src_matrix[6], src_matrix[7], src_matrix[8]);
689    }
690    if (mask_matrix) {
691       debug_printf("mask_matrix = \n");
692       debug_printf("%f, %f, %f\n", mask_matrix[0], mask_matrix[1], mask_matrix[2]);
693       debug_printf("%f, %f, %f\n", mask_matrix[3], mask_matrix[4], mask_matrix[5]);
694       debug_printf("%f, %f, %f\n", mask_matrix[6], mask_matrix[7], mask_matrix[8]);
695    }
696 #endif
697
698    switch(r->attrs_per_vertex) {
699    case 2:
700       renderer_draw_conditional(r, 4 * 8);
701       add_vertex_data1(r,
702                        pos[0], pos[1], /* src */
703                        pos[4], pos[5], /* dst */
704                        width, height,
705                        sampler_view[0]->texture, src_matrix);
706       break;
707    case 3:
708       renderer_draw_conditional(r, 4 * 12);
709       add_vertex_data2(r,
710                        pos[0], pos[1], /* src */
711                        pos[2], pos[3], /* mask */
712                        pos[4], pos[5], /* dst */
713                        width, height,
714                        sampler_view[0]->texture, sampler_view[1]->texture,
715                        src_matrix, mask_matrix);
716       break;
717    default:
718       debug_assert(!"Unsupported number of textures");
719       break;
720    }
721 }