glamor_tile.c 6.79 KB
Newer Older
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
/*
 * Copyright © 2009 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *    Eric Anholt <eric@anholt.net>
 *
 */

#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

#include "glamor_priv.h"

/** @file glamor_tile.c
 *
 * Implements the basic fill-with-a-tile support used by multiple GC ops.
 */

void
glamor_init_tile_shader(ScreenPtr screen)
{
    glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
    const char *tile_vs =
44
45
46
47
48
49
#if 0
#else
        "attribute vec4 v_position;\n"
        "attribute vec4 v_texcoord0;\n"
        "varying vec2 tile_texture;\n"
#endif
50
51
	"void main()\n"
	"{\n"
52
#if 0
53
54
	"	gl_Position = gl_Vertex;\n"
	"	gl_TexCoord[0] = gl_MultiTexCoord0;\n"
55
56
57
58
#else
        "       gl_Position = v_position;\n"
        "       tile_texture = v_texcoord0.xy;\n"
#endif
59
60
	"}\n";
    const char *tile_fs =
61
62
63
64
#if 0
#else
        "varying vec2 tile_texture;\n"
#endif
65
66
67
	"uniform sampler2D sampler;\n"
	"void main()\n"
	"{\n"
68
#if 0
69
	"	gl_FragColor = texture2D(sampler, gl_TexCoord[0].xy);\n"
70
71
72
#else
	"	gl_FragColor = texture2D(sampler, tile_texture);\n"
#endif
73
74
75
76
	"}\n";
    GLint fs_prog, vs_prog;
    GLint sampler_uniform_location;

77
78
79
80
81
    glamor_priv->tile_prog = glCreateProgram();
    vs_prog = glamor_compile_glsl_prog(GL_VERTEX_SHADER, tile_vs);
    fs_prog = glamor_compile_glsl_prog(GL_FRAGMENT_SHADER, tile_fs);
    glAttachShader(glamor_priv->tile_prog, vs_prog);
    glAttachShader(glamor_priv->tile_prog, fs_prog);
82
83
84

    glBindAttribLocation(glamor_priv->tile_prog, GLAMOR_VERTEX_POS, "v_position");
    glBindAttribLocation(glamor_priv->tile_prog, GLAMOR_VERTEX_SOURCE, "v_texcoord0");
85
86
87
    glamor_link_glsl_prog(glamor_priv->tile_prog);

    sampler_uniform_location =
88
89
90
91
	glGetUniformLocation(glamor_priv->tile_prog, "sampler");
    glUseProgram(glamor_priv->tile_prog);
    glUniform1i(sampler_uniform_location, 0);
    glUseProgram(0);
92
93
}

94
Bool
95
96
97
98
99
100
101
102
103
104
105
glamor_tile(PixmapPtr pixmap, PixmapPtr tile,
	    int x, int y, int width, int height,
	    unsigned char alu, unsigned long planemask,
	    int tile_x, int tile_y)
{
    ScreenPtr screen = pixmap->drawable.pScreen;
    glamor_screen_private *glamor_priv = glamor_get_screen_private(screen);
    int x1 = x;
    int x2 = x + width;
    int y1 = y;
    int y2 = y + height;
106
107
108
109
    int tile_x1 = tile_x;
    int tile_x2 = tile_x + width;
    int tile_y1 = tile_y;
    int tile_y2 = tile_y + height;
110
111
112
113
114
115
    float vertices[8];
    float source_texcoords[8];
    GLfloat dst_xscale, dst_yscale, src_xscale, src_yscale;
    glamor_pixmap_private *src_pixmap_priv;
    glamor_pixmap_private *dst_pixmap_priv;

116

117
118
119
    src_pixmap_priv = glamor_get_pixmap_private(tile);
    dst_pixmap_priv = glamor_get_pixmap_private(pixmap);

120
    if (glamor_priv->tile_prog == 0) {
121
	glamor_fallback("Tiling unsupported\n");
122
123
124
	goto fail;
    }

125
    if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(dst_pixmap_priv)) {      
126
      glamor_fallback("dest has no fbo.\n");
127
      goto fail;
128
    }
129
130
131
132
    if (!GLAMOR_PIXMAP_PRIV_HAS_FBO(src_pixmap_priv)) {
      /* XXX dynamic uploading candidate. */
      glamor_fallback("Non-texture tile pixmap\n");
      goto fail;
133
    }
134

135
136
    if (!glamor_set_planemask(pixmap, planemask)) {
        glamor_fallback("unsupported planemask %lx\n", planemask);
137
	goto fail;
138
    }
139
140
141
142
143
    if (alu != GXcopy) {
      glamor_set_destination_pixmap_priv_nc(src_pixmap_priv);
      glamor_validate_pixmap(tile);
    }
 
144
    glamor_set_destination_pixmap_priv_nc(dst_pixmap_priv);
145
    glamor_validate_pixmap(pixmap);
146
147
    pixmap_priv_get_scale(dst_pixmap_priv, &dst_xscale, &dst_yscale);

148
    glamor_set_alu(alu);
149

150
151
    if (GLAMOR_PIXMAP_PRIV_NO_PENDING(src_pixmap_priv)) {
      pixmap_priv_get_scale(src_pixmap_priv, &src_xscale, &src_yscale);
152
      glUseProgram(glamor_priv->tile_prog);
153
154
155
156
157
158
159
160
161
 
      glActiveTexture(GL_TEXTURE0);
      glBindTexture(GL_TEXTURE_2D, src_pixmap_priv->tex);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
      glEnable(GL_TEXTURE_2D);
      glamor_set_normalize_tcoords(src_xscale, src_yscale,
162
163
164
165
				 tile_x1, tile_y1,
				 tile_x2, tile_y2,
				 glamor_priv->yInverted,
				 source_texcoords);
166
#if 0
167
168
169
      glClientActiveTexture(GL_TEXTURE0);
      glTexCoordPointer(2, GL_FLOAT, sizeof(float) * 2, source_texcoords);
      glEnableClientState(GL_TEXTURE_COORD_ARRAY);
170
171
172
173
174
175
#else
      glVertexAttribPointer(GLAMOR_VERTEX_SOURCE, 2, GL_FLOAT, GL_FALSE,
                            2 * sizeof(float),
                            source_texcoords);
      glEnableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
#endif
176
177
178
179
180
181
182
183
184
   } 
   else {
     GLAMOR_CHECK_PENDING_FILL(glamor_priv, src_pixmap_priv);
   }
 
    glamor_set_normalize_vcoords(dst_xscale, dst_yscale,
				 x1, y1,x2, y2,
				 glamor_priv->yInverted,
				 vertices);
185

186
#if 0
187
188
    glVertexPointer(2, GL_FLOAT, sizeof(float) * 2, vertices);
    glEnableClientState(GL_VERTEX_ARRAY);
189
190
191
192
193
194
#else
    glVertexAttribPointer(GLAMOR_VERTEX_POS, 2, GL_FLOAT, GL_FALSE,
                          2 * sizeof(float),
                         vertices);
    glEnableVertexAttribArray(GLAMOR_VERTEX_POS);
#endif
195
196
    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);

197
#if 0
198
    if (GLAMOR_PIXMAP_PRIV_NO_PENDING(src_pixmap_priv)) {
199
200
    glClientActiveTexture(GL_TEXTURE0);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
201
202
    glDisable(GL_TEXTURE_2D);
    }
203
    glDisableClientState(GL_VERTEX_ARRAY);
204
205
206
207
208
209
210
#else
    if (GLAMOR_PIXMAP_PRIV_NO_PENDING(src_pixmap_priv)) {
    glDisableVertexAttribArray(GLAMOR_VERTEX_SOURCE);
    glDisable(GL_TEXTURE_2D);
    }
    glDisableVertexAttribArray(GLAMOR_VERTEX_POS);
#endif
211
    glUseProgram(0);
212
    glamor_set_alu(GXcopy);
213
    glamor_set_planemask(pixmap, ~0);
214
    return TRUE;
215
216

fail:
217
    return FALSE;
218
}