glamor_rects.c revision 35c4bbdf
1/*
2 * Copyright © 2014 Keith Packard
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission.  The copyright holders make no representations
11 * about the suitability of this software for any purpose.  It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
23#include "glamor_priv.h"
24#include "glamor_program.h"
25#include "glamor_transform.h"
26
27static const glamor_facet glamor_facet_polyfillrect_130 = {
28    .name = "poly_fill_rect",
29    .version = 130,
30    .vs_vars = "attribute vec4 primitive;\n",
31    .vs_exec = ("       vec2 pos = primitive.zw * vec2(gl_VertexID&1, (gl_VertexID&2)>>1);\n"
32                GLAMOR_POS(gl_Position, (primitive.xy + pos))),
33};
34
35static const glamor_facet glamor_facet_polyfillrect_120 = {
36    .name = "poly_fill_rect",
37    .vs_vars = "attribute vec2 primitive;\n",
38    .vs_exec = ("        vec2 pos = vec2(0,0);\n"
39                GLAMOR_POS(gl_Position, primitive.xy)),
40};
41
42static Bool
43glamor_poly_fill_rect_gl(DrawablePtr drawable,
44                         GCPtr gc, int nrect, xRectangle *prect)
45{
46    ScreenPtr screen = drawable->pScreen;
47    glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
48    PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
49    glamor_pixmap_private *pixmap_priv;
50    glamor_program *prog;
51    int off_x, off_y;
52    GLshort *v;
53    char *vbo_offset;
54    int box_index;
55
56    pixmap_priv = glamor_get_pixmap_private(pixmap);
57    if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
58        goto bail;
59
60    glamor_make_current(glamor_priv);
61
62    if (glamor_priv->glsl_version >= 130) {
63        prog = glamor_use_program_fill(pixmap, gc,
64                                       &glamor_priv->poly_fill_rect_program,
65                                       &glamor_facet_polyfillrect_130);
66
67        if (!prog)
68            goto bail;
69
70        /* Set up the vertex buffers for the points */
71
72        v = glamor_get_vbo_space(drawable->pScreen, nrect * sizeof (xRectangle), &vbo_offset);
73
74        glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
75        glVertexAttribDivisor(GLAMOR_VERTEX_POS, 1);
76        glVertexAttribPointer(GLAMOR_VERTEX_POS, 4, GL_SHORT, GL_FALSE,
77                              4 * sizeof (short), vbo_offset);
78
79        memcpy(v, prect, nrect * sizeof (xRectangle));
80
81        glamor_put_vbo_space(screen);
82    } else {
83        int n;
84
85        prog = glamor_use_program_fill(pixmap, gc,
86                                       &glamor_priv->poly_fill_rect_program,
87                                       &glamor_facet_polyfillrect_120);
88
89        if (!prog)
90            goto bail;
91
92        /* Set up the vertex buffers for the points */
93
94        v = glamor_get_vbo_space(drawable->pScreen, nrect * 8 * sizeof (short), &vbo_offset);
95
96        glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
97        glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_SHORT, GL_FALSE,
98                              2 * sizeof (short), vbo_offset);
99
100        for (n = 0; n < nrect; n++) {
101            v[0] = prect->x;                v[1] = prect->y;
102            v[2] = prect->x;                v[3] = prect->y + prect->height;
103            v[4] = prect->x + prect->width; v[5] = prect->y + prect->height;
104            v[6] = prect->x + prect->width; v[7] = prect->y;
105            prect++;
106            v += 8;
107        }
108
109        glamor_put_vbo_space(screen);
110    }
111
112    glEnable(GL_SCISSOR_TEST);
113
114    glamor_pixmap_loop(pixmap_priv, box_index) {
115        int nbox = RegionNumRects(gc->pCompositeClip);
116        BoxPtr box = RegionRects(gc->pCompositeClip);
117
118        glamor_set_destination_drawable(drawable, box_index, TRUE, FALSE,
119                                        prog->matrix_uniform, &off_x, &off_y);
120
121        while (nbox--) {
122            glScissor(box->x1 + off_x,
123                      box->y1 + off_y,
124                      box->x2 - box->x1,
125                      box->y2 - box->y1);
126            box++;
127            if (glamor_priv->glsl_version >= 130)
128                glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, nrect);
129            else {
130                glamor_glDrawArrays_GL_QUADS(glamor_priv, nrect);
131            }
132        }
133    }
134
135    glDisable(GL_SCISSOR_TEST);
136    if (glamor_priv->glsl_version >= 130)
137        glVertexAttribDivisor(GLAMOR_VERTEX_POS, 0);
138    glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
139
140    return TRUE;
141bail:
142    return FALSE;
143}
144
145static void
146glamor_poly_fill_rect_bail(DrawablePtr drawable,
147                           GCPtr gc, int nrect, xRectangle *prect)
148{
149    glamor_fallback("to %p (%c)\n", drawable,
150                    glamor_get_drawable_location(drawable));
151    if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RW) &&
152        glamor_prepare_access_gc(gc)) {
153        fbPolyFillRect(drawable, gc, nrect, prect);
154    }
155    glamor_finish_access_gc(gc);
156    glamor_finish_access(drawable);
157}
158
159void
160glamor_poly_fill_rect(DrawablePtr drawable,
161                      GCPtr gc, int nrect, xRectangle *prect)
162{
163    if (glamor_poly_fill_rect_gl(drawable, gc, nrect, prect))
164        return;
165    glamor_poly_fill_rect_bail(drawable, gc, nrect, prect);
166}
167