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
27 static const glamor_facet glamor_facet_polyfillrect_130 = {
28 .name = "poly_fill_rect",
29 .version = 130,
30 .source_name = "size",
31 .vs_vars = "attribute vec2 primitive;\n"
32 "attribute vec2 size;\n",
33 .vs_exec = (" vec2 pos = size * vec2(gl_VertexID&1, (gl_VertexID&2)>>1);\n"
34 GLAMOR_POS(gl_Position, (primitive.xy + pos))),
35 };
36
37 static const glamor_facet glamor_facet_polyfillrect_120 = {
38 .name = "poly_fill_rect",
39 .vs_vars = "attribute vec2 primitive;\n",
40 .vs_exec = (" vec2 pos = vec2(0,0);\n"
41 GLAMOR_POS(gl_Position, primitive.xy)),
42 };
43
44 static Bool
glamor_poly_fill_rect_gl(DrawablePtr drawable,GCPtr gc,int nrect,xRectangle * prect)45 glamor_poly_fill_rect_gl(DrawablePtr drawable,
46 GCPtr gc, int nrect, xRectangle *prect)
47 {
48 ScreenPtr screen = drawable->pScreen;
49 glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
50 PixmapPtr pixmap = glamor_get_drawable_pixmap(drawable);
51 glamor_pixmap_private *pixmap_priv;
52 glamor_program *prog;
53 int off_x, off_y;
54 GLshort *v;
55 char *vbo_offset;
56 int box_index;
57 Bool ret = FALSE;
58 BoxRec bounds = glamor_no_rendering_bounds();
59
60 pixmap_priv = glamor_get_pixmap_private(pixmap);
61 if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(pixmap_priv))
62 goto bail;
63
64 glamor_make_current(glamor_priv);
65
66 if (nrect < 100) {
67 bounds = glamor_start_rendering_bounds();
68 for (int i = 0; i < nrect; i++)
69 glamor_bounds_union_rect(&bounds, &prect[i]);
70 }
71
72 if (glamor_priv->glsl_version >= 130) {
73 prog = glamor_use_program_fill(pixmap, gc,
74 &glamor_priv->poly_fill_rect_program,
75 &glamor_facet_polyfillrect_130);
76
77 if (!prog)
78 goto bail;
79
80 /* Set up the vertex buffers for the points */
81
82 v = glamor_get_vbo_space(drawable->pScreen, nrect * sizeof (xRectangle), &vbo_offset);
83
84 glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
85 glVertexAttribDivisor(GLAMOR_VERTEX_POS, 1);
86 glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_SHORT, GL_FALSE,
87 4 * sizeof (short), vbo_offset);
88
89 glEnableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
90 glVertexAttribDivisor(GLAMOR_VERTEX_SOURCE, 1);
91 glVertexAttribPointer(GLAMOR_VERTEX_SOURCE, 2, GL_UNSIGNED_SHORT, GL_FALSE,
92 4 * sizeof (short), vbo_offset + 2 * sizeof (short));
93
94 memcpy(v, prect, nrect * sizeof (xRectangle));
95
96 glamor_put_vbo_space(screen);
97 } else {
98 int n;
99
100 prog = glamor_use_program_fill(pixmap, gc,
101 &glamor_priv->poly_fill_rect_program,
102 &glamor_facet_polyfillrect_120);
103
104 if (!prog)
105 goto bail;
106
107 /* Set up the vertex buffers for the points */
108
109 v = glamor_get_vbo_space(drawable->pScreen, nrect * 8 * sizeof (short), &vbo_offset);
110
111 glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
112 glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_SHORT, GL_FALSE,
113 2 * sizeof (short), vbo_offset);
114
115 for (n = 0; n < nrect; n++) {
116 v[0] = prect->x; v[1] = prect->y;
117 v[2] = prect->x; v[3] = prect->y + prect->height;
118 v[4] = prect->x + prect->width; v[5] = prect->y + prect->height;
119 v[6] = prect->x + prect->width; v[7] = prect->y;
120 prect++;
121 v += 8;
122 }
123
124 glamor_put_vbo_space(screen);
125 }
126
127 glEnable(GL_SCISSOR_TEST);
128
129 glamor_pixmap_loop(pixmap_priv, box_index) {
130 int nbox = RegionNumRects(gc->pCompositeClip);
131 BoxPtr box = RegionRects(gc->pCompositeClip);
132
133 if (!glamor_set_destination_drawable(drawable, box_index, TRUE, FALSE,
134 prog->matrix_uniform, &off_x, &off_y))
135 goto bail;
136
137 while (nbox--) {
138 BoxRec scissor = {
139 .x1 = max(box->x1, bounds.x1 + drawable->x),
140 .y1 = max(box->y1, bounds.y1 + drawable->y),
141 .x2 = min(box->x2, bounds.x2 + drawable->x),
142 .y2 = min(box->y2, bounds.y2 + drawable->y),
143 };
144
145 box++;
146
147 if (scissor.x1 >= scissor.x2 || scissor.y1 >= scissor.y2)
148 continue;
149
150 glScissor(scissor.x1 + off_x,
151 scissor.y1 + off_y,
152 scissor.x2 - scissor.x1,
153 scissor.y2 - scissor.y1);
154 if (glamor_priv->glsl_version >= 130)
155 glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, nrect);
156 else {
157 glamor_glDrawArrays_GL_QUADS(glamor_priv, nrect);
158 }
159 }
160 }
161
162 ret = TRUE;
163
164 bail:
165 glDisable(GL_SCISSOR_TEST);
166 if (glamor_priv->glsl_version >= 130) {
167 glVertexAttribDivisor(GLAMOR_VERTEX_SOURCE, 0);
168 glDisableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
169 glVertexAttribDivisor(GLAMOR_VERTEX_POS, 0);
170 }
171 glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
172
173 return ret;
174 }
175
176 static void
glamor_poly_fill_rect_bail(DrawablePtr drawable,GCPtr gc,int nrect,xRectangle * prect)177 glamor_poly_fill_rect_bail(DrawablePtr drawable,
178 GCPtr gc, int nrect, xRectangle *prect)
179 {
180 glamor_fallback("to %p (%c)\n", drawable,
181 glamor_get_drawable_location(drawable));
182 if (glamor_prepare_access(drawable, GLAMOR_ACCESS_RW) &&
183 glamor_prepare_access_gc(gc)) {
184 fbPolyFillRect(drawable, gc, nrect, prect);
185 }
186 glamor_finish_access_gc(gc);
187 glamor_finish_access(drawable);
188 }
189
190 void
glamor_poly_fill_rect(DrawablePtr drawable,GCPtr gc,int nrect,xRectangle * prect)191 glamor_poly_fill_rect(DrawablePtr drawable,
192 GCPtr gc, int nrect, xRectangle *prect)
193 {
194 if (glamor_poly_fill_rect_gl(drawable, gc, nrect, prect))
195 return;
196 glamor_poly_fill_rect_bail(drawable, gc, nrect, prect);
197 }
198