summaryrefslogtreecommitdiff
path: root/xserver/glamor/glamor_program.c
blob: dec116c7558fc57e0fc0a2fc07d211c1fdb95984 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/*
 * Copyright © 2014 Keith Packard
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#include "glamor_priv.h"
#include "glamor_transform.h"
#include "glamor_program.h"

static Bool
use_solid(PixmapPtr pixmap, GCPtr gc, glamor_program *prog, void *arg)
{
    return glamor_set_solid(pixmap, gc, TRUE, prog->fg_uniform);
}

const glamor_facet glamor_fill_solid = {
    .name = "solid",
    .fs_exec = "       gl_FragColor = fg;\n",
    .locations = glamor_program_location_fg,
    .use = use_solid,
};

static Bool
use_tile(PixmapPtr pixmap, GCPtr gc, glamor_program *prog, void *arg)
{
    return glamor_set_tiled(pixmap, gc, prog->fill_offset_uniform, prog->fill_size_inv_uniform);
}

static const glamor_facet glamor_fill_tile = {
    .name = "tile",
    .vs_exec =  "       fill_pos = (fill_offset + primitive.xy + pos) * fill_size_inv;\n",
    .fs_exec =  "       gl_FragColor = texture2D(sampler, fill_pos);\n",
    .locations = glamor_program_location_fillsamp | glamor_program_location_fillpos,
    .use = use_tile,
};

static Bool
use_stipple(PixmapPtr pixmap, GCPtr gc, glamor_program *prog, void *arg)
{
    return glamor_set_stippled(pixmap, gc, prog->fg_uniform,
                               prog->fill_offset_uniform,
                               prog->fill_size_inv_uniform);
}

static const glamor_facet glamor_fill_stipple = {
    .name = "stipple",
    .vs_exec =  "       fill_pos = (fill_offset + primitive.xy + pos) * fill_size_inv;\n",
    .fs_exec = ("       float a = texture2D(sampler, fill_pos).w;\n"
                "       if (a == 0.0)\n"
                "               discard;\n"
                "       gl_FragColor = fg;\n"),
    .locations = glamor_program_location_fg | glamor_program_location_fillsamp | glamor_program_location_fillpos,
    .use = use_stipple,
};

static Bool
use_opaque_stipple(PixmapPtr pixmap, GCPtr gc, glamor_program *prog, void *arg)
{
    if (!use_stipple(pixmap, gc, prog, arg))
        return FALSE;
    glamor_set_color(pixmap, gc->bgPixel, prog->bg_uniform);
    return TRUE;
}

static const glamor_facet glamor_fill_opaque_stipple = {
    .name = "opaque_stipple",
    .vs_exec =  "       fill_pos = (fill_offset + primitive.xy + pos) * fill_size_inv;\n",
    .fs_exec = ("       float a = texture2D(sampler, fill_pos).w;\n"
                "       if (a == 0.0)\n"
                "               gl_FragColor = bg;\n"
                "       else\n"
                "               gl_FragColor = fg;\n"),
    .locations = glamor_program_location_fg | glamor_program_location_bg | glamor_program_location_fillsamp | glamor_program_location_fillpos,
    .use = use_opaque_stipple
};

static const glamor_facet *glamor_facet_fill[4] = {
    &glamor_fill_solid,
    &glamor_fill_tile,
    &glamor_fill_stipple,
    &glamor_fill_opaque_stipple,
};

typedef struct {
    glamor_program_location     location;
    const char                  *vs_vars;
    const char                  *fs_vars;
} glamor_location_var;

static glamor_location_var location_vars[] = {
    {
        .location = glamor_program_location_fg,
        .fs_vars = "uniform vec4 fg;\n"
    },
    {
        .location = glamor_program_location_bg,
        .fs_vars = "uniform vec4 bg;\n"
    },
    {
        .location = glamor_program_location_fillsamp,
        .fs_vars = "uniform sampler2D sampler;\n"
    },
    {
        .location = glamor_program_location_fillpos,
        .vs_vars = ("uniform vec2 fill_offset;\n"
                    "uniform vec2 fill_size_inv;\n"
                    "varying vec2 fill_pos;\n"),
        .fs_vars = ("uniform vec2 fill_size_inv;\n"
                    "varying vec2 fill_pos;\n")
    },
    {
        .location = glamor_program_location_font,
        .fs_vars = "uniform usampler2D font;\n",
    },
    {
        .location = glamor_program_location_bitplane,
        .fs_vars = ("uniform uvec4 bitplane;\n"
                    "uniform vec4 bitmul;\n"),
    },
    {
        .location = glamor_program_location_dash,
        .vs_vars = "uniform float dash_length;\n",
        .fs_vars = "uniform sampler2D dash;\n",
    },
    {
        .location = glamor_program_location_atlas,
        .fs_vars = "uniform sampler2D atlas;\n",
    },
};

static char *
add_var(char *cur, const char *add)
{
    char *new;

    if (!add)
        return cur;

    new = realloc(cur, strlen(cur) + strlen(add) + 1);
    if (!new) {
        free(cur);
        return NULL;
    }
    strcat(new, add);
    return new;
}

static char *
vs_location_vars(glamor_program_location locations)
{
    int l;
    char *vars = strdup("");

    for (l = 0; vars && l < ARRAY_SIZE(location_vars); l++)
        if (locations & location_vars[l].location)
            vars = add_var(vars, location_vars[l].vs_vars);
    return vars;
}

static char *
fs_location_vars(glamor_program_location locations)
{
    int l;
    char *vars = strdup("");

    for (l = 0; vars && l < ARRAY_SIZE(location_vars); l++)
        if (locations & location_vars[l].location)
            vars = add_var(vars, location_vars[l].fs_vars);
    return vars;
}

static const char vs_template[] =
    "%s"                                /* version */
    "%s"                                /* defines */
    "%s"                                /* prim vs_vars */
    "%s"                                /* fill vs_vars */
    "%s"                                /* location vs_vars */
    GLAMOR_DECLARE_MATRIX
    "void main() {\n"
    "%s"                                /* prim vs_exec, outputs 'pos' and gl_Position */
    "%s"                                /* fill vs_exec */
    "}\n";

static const char fs_template[] =
    "%s"                                /* version */
    GLAMOR_DEFAULT_PRECISION
    "%s"                                /* defines */
    "%s"                                /* prim fs_vars */
    "%s"                                /* fill fs_vars */
    "%s"                                /* location fs_vars */
    "void main() {\n"
    "%s"                                /* prim fs_exec */
    "%s"                                /* fill fs_exec */
    "%s"                                /* combine */
    "}\n";

static const char *
str(const char *s)
{
    if (!s)
        return "";
    return s;
}

static const glamor_facet facet_null_fill = {
    .name = ""
};

#define DBG 0

static GLint
glamor_get_uniform(glamor_program               *prog,
                   glamor_program_location      location,
                   const char                   *name)
{
    GLint uniform;
    if (location && (prog->locations & location) == 0)
        return -2;
    uniform = glGetUniformLocation(prog->prog, name);
#if DBG
    ErrorF("%s uniform %d\n", name, uniform);
#endif
    return uniform;
}

Bool
glamor_build_program(ScreenPtr          screen,
                     glamor_program     *prog,
                     const glamor_facet *prim,
                     const glamor_facet *fill,
                     const char         *combine,
                     const char         *defines)
{
    glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);

    glamor_program_location     locations = prim->locations;
    glamor_program_flag         flags = prim->flags;

    int                         version = prim->version;
    char                        *version_string = NULL;

    char                        *fs_vars = NULL;
    char                        *vs_vars = NULL;

    char                        *vs_prog_string;
    char                        *fs_prog_string;

    GLint                       fs_prog, vs_prog;

    if (!fill)
        fill = &facet_null_fill;

    locations |= fill->locations;
    flags |= fill->flags;
    version = MAX(version, fill->version);

    if (version > glamor_priv->glsl_version)
        goto fail;

    vs_vars = vs_location_vars(locations);
    fs_vars = fs_location_vars(locations);

    if (!vs_vars)
        goto fail;
    if (!fs_vars)
        goto fail;

    if (version) {
        if (asprintf(&version_string, "#version %d\n", version) < 0)
            version_string = NULL;
        if (!version_string)
            goto fail;
    }

    if (asprintf(&vs_prog_string,
                 vs_template,
                 str(version_string),
                 str(defines),
                 str(prim->vs_vars),
                 str(fill->vs_vars),
                 vs_vars,
                 str(prim->vs_exec),
                 str(fill->vs_exec)) < 0)
        vs_prog_string = NULL;

    if (asprintf(&fs_prog_string,
                 fs_template,
                 str(version_string),
                 str(defines),
                 str(prim->fs_vars),
                 str(fill->fs_vars),
                 fs_vars,
                 str(prim->fs_exec),
                 str(fill->fs_exec),
                 str(combine)) < 0)
        fs_prog_string = NULL;

    if (!vs_prog_string || !fs_prog_string)
        goto fail;

    prog->prog = glCreateProgram();
#if DBG
    ErrorF("\n\tProgram %d for %s %s\n\tVertex shader:\n\n\t================\n%s\n\n\tFragment Shader:\n\n%s\t================\n",
           prog->prog, prim->name, fill->name, vs_prog_string, fs_prog_string);
#endif

    prog->flags = flags;
    prog->locations = locations;
    prog->prim_use = prim->use;
    prog->prim_use_render = prim->use_render;
    prog->fill_use = fill->use;
    prog->fill_use_render = fill->use_render;

    vs_prog = glamor_compile_glsl_prog(GL_VERTEX_SHADER, vs_prog_string);
    fs_prog = glamor_compile_glsl_prog(GL_FRAGMENT_SHADER, fs_prog_string);
    free(vs_prog_string);
    free(fs_prog_string);
    glAttachShader(prog->prog, vs_prog);
    glDeleteShader(vs_prog);
    glAttachShader(prog->prog, fs_prog);
    glDeleteShader(fs_prog);
    glBindAttribLocation(prog->prog, GLAMOR_VERTEX_POS, "primitive");

    if (prim->source_name) {
#if DBG
        ErrorF("Bind GLAMOR_VERTEX_SOURCE to %s\n", prim->source_name);
#endif
        glBindAttribLocation(prog->prog, GLAMOR_VERTEX_SOURCE, prim->source_name);
    }
    if (prog->alpha == glamor_program_alpha_dual_blend) {
        glBindFragDataLocationIndexed(prog->prog, 0, 0, "color0");
        glBindFragDataLocationIndexed(prog->prog, 0, 1, "color1");
    }

    glamor_link_glsl_prog(screen, prog->prog, "%s_%s", prim->name, fill->name);

    prog->matrix_uniform = glamor_get_uniform(prog, glamor_program_location_none, "v_matrix");
    prog->fg_uniform = glamor_get_uniform(prog, glamor_program_location_fg, "fg");
    prog->bg_uniform = glamor_get_uniform(prog, glamor_program_location_bg, "bg");
    prog->fill_offset_uniform = glamor_get_uniform(prog, glamor_program_location_fillpos, "fill_offset");
    prog->fill_size_inv_uniform = glamor_get_uniform(prog, glamor_program_location_fillpos, "fill_size_inv");
    prog->font_uniform = glamor_get_uniform(prog, glamor_program_location_font, "font");
    prog->bitplane_uniform = glamor_get_uniform(prog, glamor_program_location_bitplane, "bitplane");
    prog->bitmul_uniform = glamor_get_uniform(prog, glamor_program_location_bitplane, "bitmul");
    prog->dash_uniform = glamor_get_uniform(prog, glamor_program_location_dash, "dash");
    prog->dash_length_uniform = glamor_get_uniform(prog, glamor_program_location_dash, "dash_length");
    prog->atlas_uniform = glamor_get_uniform(prog, glamor_program_location_atlas, "atlas");

    free(version_string);
    free(fs_vars);
    free(vs_vars);
    return TRUE;
fail:
    prog->failed = 1;
    if (prog->prog) {
        glDeleteProgram(prog->prog);
        prog->prog = 0;
    }
    free(version_string);
    free(fs_vars);
    free(vs_vars);
    return FALSE;
}

Bool
glamor_use_program(PixmapPtr            pixmap,
                   GCPtr                gc,
                   glamor_program       *prog,
                   void                 *arg)
{
    glUseProgram(prog->prog);

    if (prog->prim_use && !prog->prim_use(pixmap, gc, prog, arg))
        return FALSE;

    if (prog->fill_use && !prog->fill_use(pixmap, gc, prog, arg))
        return FALSE;

    return TRUE;
}

glamor_program *
glamor_use_program_fill(PixmapPtr               pixmap,
                        GCPtr                   gc,
                        glamor_program_fill     *program_fill,
                        const glamor_facet      *prim)
{
    ScreenPtr                   screen = pixmap->drawable.pScreen;
    glamor_program              *prog = &program_fill->progs[gc->fillStyle];

    int                         fill_style = gc->fillStyle;
    const glamor_facet          *fill;

    if (prog->failed)
        return FALSE;

    if (!prog->prog) {
        fill = glamor_facet_fill[fill_style];
        if (!fill)
            return NULL;

        if (!glamor_build_program(screen, prog, prim, fill, NULL, NULL))
            return NULL;
    }

    if (!glamor_use_program(pixmap, gc, prog, NULL))
        return NULL;

    return prog;
}

static struct blendinfo composite_op_info[] = {
    [PictOpClear] = {0, 0, GL_ZERO, GL_ZERO},
    [PictOpSrc] = {0, 0, GL_ONE, GL_ZERO},
    [PictOpDst] = {0, 0, GL_ZERO, GL_ONE},
    [PictOpOver] = {0, 1, GL_ONE, GL_ONE_MINUS_SRC_ALPHA},
    [PictOpOverReverse] = {1, 0, GL_ONE_MINUS_DST_ALPHA, GL_ONE},
    [PictOpIn] = {1, 0, GL_DST_ALPHA, GL_ZERO},
    [PictOpInReverse] = {0, 1, GL_ZERO, GL_SRC_ALPHA},
    [PictOpOut] = {1, 0, GL_ONE_MINUS_DST_ALPHA, GL_ZERO},
    [PictOpOutReverse] = {0, 1, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA},
    [PictOpAtop] = {1, 1, GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA},
    [PictOpAtopReverse] = {1, 1, GL_ONE_MINUS_DST_ALPHA, GL_SRC_ALPHA},
    [PictOpXor] = {1, 1, GL_ONE_MINUS_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA},
    [PictOpAdd] = {0, 0, GL_ONE, GL_ONE},
};

static void
glamor_set_blend(CARD8 op, glamor_program_alpha alpha, PicturePtr dst)
{
    glamor_screen_private *glamor_priv = glamor_get_screen_private(dst->pDrawable->pScreen);
    GLenum src_blend, dst_blend;
    struct blendinfo *op_info;

    switch (alpha) {
    case glamor_program_alpha_ca_first:
        op = PictOpOutReverse;
        break;
    case glamor_program_alpha_ca_second:
        op = PictOpAdd;
        break;
    default:
        break;
    }

    if (glamor_priv->gl_flavor != GLAMOR_GL_ES2)
        glDisable(GL_COLOR_LOGIC_OP);

    if (op == PictOpSrc)
        return;

    op_info = &composite_op_info[op];

    src_blend = op_info->source_blend;
    dst_blend = op_info->dest_blend;

    /* If there's no dst alpha channel, adjust the blend op so that we'll treat
     * it as always 1.
     */
    if (PICT_FORMAT_A(dst->format) == 0 && op_info->dest_alpha) {
        if (src_blend == GL_DST_ALPHA)
            src_blend = GL_ONE;
        else if (src_blend == GL_ONE_MINUS_DST_ALPHA)
            src_blend = GL_ZERO;
    }

    /* Set up the source alpha value for blending in component alpha mode. */
    if (alpha == glamor_program_alpha_dual_blend) {
        switch (dst_blend) {
        case GL_SRC_ALPHA:
            dst_blend = GL_SRC1_COLOR;
            break;
        case GL_ONE_MINUS_SRC_ALPHA:
            dst_blend = GL_ONE_MINUS_SRC1_COLOR;
            break;
        }
    } else if (alpha != glamor_program_alpha_normal) {
        switch (dst_blend) {
        case GL_SRC_ALPHA:
            dst_blend = GL_SRC_COLOR;
            break;
        case GL_ONE_MINUS_SRC_ALPHA:
            dst_blend = GL_ONE_MINUS_SRC_COLOR;
            break;
        }
    }

    glEnable(GL_BLEND);
    glBlendFunc(src_blend, dst_blend);
}

static Bool
use_source_solid(CARD8 op, PicturePtr src, PicturePtr dst, glamor_program *prog)
{

    glamor_set_blend(op, prog->alpha, dst);

    glamor_set_color_depth(dst->pDrawable->pScreen, 32,
                           src->pSourcePict->solidFill.color,
                           prog->fg_uniform);
    return TRUE;
}

static const glamor_facet glamor_source_solid = {
    .name = "render_solid",
    .fs_exec = "       vec4 source = fg;\n",
    .locations = glamor_program_location_fg,
    .use_render = use_source_solid,
};

static Bool
use_source_picture(CARD8 op, PicturePtr src, PicturePtr dst, glamor_program *prog)
{
    glamor_set_blend(op, prog->alpha, dst);

    return glamor_set_texture((PixmapPtr) src->pDrawable,
                              glamor_picture_red_is_alpha(dst),
                              0, 0,
                              prog->fill_offset_uniform,
                              prog->fill_size_inv_uniform);
}

static const glamor_facet glamor_source_picture = {
    .name = "render_picture",
    .vs_exec =  "       fill_pos = (fill_offset + primitive.xy + pos) * fill_size_inv;\n",
    .fs_exec =  "       vec4 source = texture2D(sampler, fill_pos);\n",
    .locations = glamor_program_location_fillsamp | glamor_program_location_fillpos,
    .use_render = use_source_picture,
};

static Bool
use_source_1x1_picture(CARD8 op, PicturePtr src, PicturePtr dst, glamor_program *prog)
{
    glamor_set_blend(op, prog->alpha, dst);

    return glamor_set_texture_pixmap((PixmapPtr) src->pDrawable,
                                     glamor_picture_red_is_alpha(dst));
}

static const glamor_facet glamor_source_1x1_picture = {
    .name = "render_picture",
    .fs_exec =  "       vec4 source = texture2D(sampler, vec2(0.5));\n",
    .locations = glamor_program_location_fillsamp,
    .use_render = use_source_1x1_picture,
};

static const glamor_facet *glamor_facet_source[glamor_program_source_count] = {
    [glamor_program_source_solid] = &glamor_source_solid,
    [glamor_program_source_picture] = &glamor_source_picture,
    [glamor_program_source_1x1_picture] = &glamor_source_1x1_picture,
};

static const char *glamor_combine[] = {
    [glamor_program_alpha_normal]    = "       gl_FragColor = source * mask.a;\n",
    [glamor_program_alpha_ca_first]  = "       gl_FragColor = source.a * mask;\n",
    [glamor_program_alpha_ca_second] = "       gl_FragColor = source * mask;\n",
    [glamor_program_alpha_dual_blend] = "      color0 = source * mask;\n"
                                        "      color1 = source.a * mask;\n"
};

static Bool
glamor_setup_one_program_render(ScreenPtr               screen,
                                glamor_program          *prog,
                                glamor_program_source   source_type,
                                glamor_program_alpha    alpha,
                                const glamor_facet      *prim,
                                const char              *defines)
{
    if (prog->failed)
        return FALSE;

    if (!prog->prog) {
        const glamor_facet      *fill = glamor_facet_source[source_type];

        if (!fill)
            return FALSE;

        prog->alpha = alpha;
        if (!glamor_build_program(screen, prog, prim, fill, glamor_combine[alpha], defines))
            return FALSE;
    }

    return TRUE;
}

glamor_program *
glamor_setup_program_render(CARD8                 op,
                            PicturePtr            src,
                            PicturePtr            mask,
                            PicturePtr            dst,
                            glamor_program_render *program_render,
                            const glamor_facet    *prim,
                            const char            *defines)
{
    ScreenPtr                   screen = dst->pDrawable->pScreen;
    glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
    glamor_program_alpha        alpha;
    glamor_program_source       source_type;
    glamor_program              *prog;

    if (op > ARRAY_SIZE(composite_op_info))
        return NULL;

    if (glamor_is_component_alpha(mask)) {
        if (glamor_priv->has_dual_blend) {
            alpha = glamor_program_alpha_dual_blend;
        } else {
            /* This only works for PictOpOver */
            if (op != PictOpOver)
                return NULL;

            alpha = glamor_program_alpha_ca_first;
        }
    } else
        alpha = glamor_program_alpha_normal;

    if (src->pDrawable) {

        /* Can't do transforms, alphamaps or sourcing from non-pixmaps yet */
        if (src->transform || src->alphaMap || src->pDrawable->type != DRAWABLE_PIXMAP)
            return NULL;

        if (src->pDrawable->width == 1 && src->pDrawable->height == 1 && src->repeat)
            source_type = glamor_program_source_1x1_picture;
        else
            source_type = glamor_program_source_picture;
    } else {
        SourcePictPtr   sp = src->pSourcePict;
        if (!sp)
            return NULL;
        switch (sp->type) {
        case SourcePictTypeSolidFill:
            source_type = glamor_program_source_solid;
            break;
        default:
            return NULL;
        }
    }

    prog = &program_render->progs[source_type][alpha];
    if (!glamor_setup_one_program_render(screen, prog, source_type, alpha, prim, defines))
        return NULL;

    if (alpha == glamor_program_alpha_ca_first) {

	  /* Make sure we can also build the second program before
	   * deciding to use this path.
	   */
	  if (!glamor_setup_one_program_render(screen,
					       &program_render->progs[source_type][glamor_program_alpha_ca_second],
					       source_type, glamor_program_alpha_ca_second, prim,
					       defines))
	      return NULL;
    }
    return prog;
}

Bool
glamor_use_program_render(glamor_program        *prog,
                          CARD8                 op,
                          PicturePtr            src,
                          PicturePtr            dst)
{
    glUseProgram(prog->prog);

    if (prog->prim_use_render && !prog->prim_use_render(op, src, dst, prog))
        return FALSE;

    if (prog->fill_use_render && !prog->fill_use_render(op, src, dst, prog))
        return FALSE;
    return TRUE;
}