progs/glsl: finish conversion to GLEW
[mesa.git] / progs / glsl / samplers.c
1 /**
2 * Exercise all available GLSL texture samplers.
3 *
4 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * We generate a fragment shader which uses the maximum number of supported
26 * texture samplers.
27 * For each sampler we create a separate texture. Each texture has a
28 * single strip of color at a different intensity. The fragment shader
29 * samples all the textures at the same coordinate and sums the values.
30 * The result should be a quad with rows of colors of increasing intensity
31 * from bottom to top.
32 *
33 * Brian Paul
34 * 1 Jan 2009
35 */
36
37 #include <assert.h>
38 #include <math.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <GL/glew.h>
43 #include "GL/glut.h"
44 #include "readtex.h"
45 #include "shaderutil.h"
46
47
48 #define MAX_SAMPLERS 128
49
50
51 static const char *Demo = "samplers";
52
53 static GLuint Program;
54 static GLint NumSamplers;
55 static GLuint Textures[MAX_SAMPLERS];
56 static GLfloat Xrot = 0.0, Yrot = .0, Zrot = 0.0;
57 static GLfloat EyeDist = 10;
58 static GLboolean Anim = GL_FALSE;
59
60
61 static void
62 DrawPolygon(GLfloat size)
63 {
64 glPushMatrix();
65 glNormal3f(0, 0, 1);
66 glBegin(GL_POLYGON);
67
68 glMultiTexCoord2f(GL_TEXTURE0, 0, 0);
69 glVertex2f(-size, -size);
70
71 glMultiTexCoord2f(GL_TEXTURE0, 1, 0);
72 glVertex2f( size, -size);
73
74 glMultiTexCoord2f(GL_TEXTURE0, 1, 1);
75 glVertex2f( size, size);
76
77 glMultiTexCoord2f(GL_TEXTURE0, 0, 1);
78 glVertex2f(-size, size);
79
80 glEnd();
81 glPopMatrix();
82 }
83
84
85 static void
86 draw(void)
87 {
88 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
89
90 glPushMatrix();
91 glTranslatef(0.0, 0.0, -EyeDist);
92 glRotatef(Zrot, 0, 0, 1);
93 glRotatef(Yrot, 0, 1, 0);
94 glRotatef(Xrot, 1, 0, 0);
95
96 DrawPolygon(3.0);
97
98 glPopMatrix();
99
100 glutSwapBuffers();
101 }
102
103
104 static void
105 idle(void)
106 {
107 GLfloat t = 0.05 * glutGet(GLUT_ELAPSED_TIME);
108 Yrot = t;
109 glutPostRedisplay();
110 }
111
112
113 static void
114 key(unsigned char k, int x, int y)
115 {
116 (void) x;
117 (void) y;
118 switch (k) {
119 case ' ':
120 case 'a':
121 Anim = !Anim;
122 if (Anim)
123 glutIdleFunc(idle);
124 else
125 glutIdleFunc(NULL);
126 break;
127 case 'z':
128 EyeDist -= 0.5;
129 if (EyeDist < 3.0)
130 EyeDist = 3.0;
131 break;
132 case 'Z':
133 EyeDist += 0.5;
134 if (EyeDist > 90.0)
135 EyeDist = 90;
136 break;
137 case 27:
138 exit(0);
139 }
140 glutPostRedisplay();
141 }
142
143
144 static void
145 specialkey(int key, int x, int y)
146 {
147 GLfloat step = 2.0;
148 (void) x;
149 (void) y;
150 switch (key) {
151 case GLUT_KEY_UP:
152 Xrot += step;
153 break;
154 case GLUT_KEY_DOWN:
155 Xrot -= step;
156 break;
157 case GLUT_KEY_LEFT:
158 Yrot -= step;
159 break;
160 case GLUT_KEY_RIGHT:
161 Yrot += step;
162 break;
163 }
164 glutPostRedisplay();
165 }
166
167
168 /* new window size or exposure */
169 static void
170 Reshape(int width, int height)
171 {
172 GLfloat ar = (float) width / (float) height;
173 glViewport(0, 0, (GLint)width, (GLint)height);
174 glMatrixMode(GL_PROJECTION);
175 glLoadIdentity();
176 glFrustum(-2.0*ar, 2.0*ar, -2.0, 2.0, 4.0, 100.0);
177 glMatrixMode(GL_MODELVIEW);
178 glLoadIdentity();
179 }
180
181
182 static void
183 InitTextures(void)
184 {
185 const GLint size = MAX_SAMPLERS;
186 GLubyte *texImage;
187 GLenum filter = GL_NEAREST;
188 GLint stripeSize;
189 GLint s;
190
191 texImage = (GLubyte *) malloc(size * size * 4);
192
193 glGenTextures(NumSamplers, Textures);
194
195 /* size of texels stripe */
196 stripeSize = size / NumSamplers;
197
198 /* create a texture for each sampler */
199 for (s = 0; s < NumSamplers; s++) {
200 GLint x, y, ypos;
201 GLubyte intensity = 31 + s * (256-32) / (NumSamplers - 1);
202
203 printf("Texture %d: color = %d, %d, %d\n", s,
204 (int) intensity, 0, (int) intensity );
205
206 /* initialize the texture to black */
207 memset(texImage, 0, size * size * 4);
208
209 /* set a stripe of texels to the intensity value */
210 ypos = s * stripeSize;
211 for (y = 0; y < stripeSize; y++) {
212 for (x = 0; x < size; x++) {
213 GLint k = 4 * ((ypos + y) * size + x);
214 texImage[k + 0] = intensity;
215 texImage[k + 1] = intensity;
216 texImage[k + 2] = 0;
217 texImage[k + 3] = 255;
218 }
219 }
220
221 glActiveTexture(GL_TEXTURE0 + s);
222 glBindTexture(GL_TEXTURE_2D, Textures[s]);
223 gluBuild2DMipmaps(GL_TEXTURE_2D, 4, size, size,
224 GL_RGBA, GL_UNSIGNED_BYTE, texImage);
225
226 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
227 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
228 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
229 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
230 }
231
232 free(texImage);
233 }
234
235
236 /**
237 * Generate a fragment shader that uses the given number of samplers.
238 */
239 static char *
240 GenFragmentShader(GLint numSamplers)
241 {
242 const int maxLen = 10 * 1000;
243 char *prog = (char *) malloc(maxLen);
244 char *p = prog;
245 int s;
246
247 p += sprintf(p, "// Generated fragment shader:\n");
248 #ifndef SAMPLERS_ARRAY
249 for (s = 0; s < numSamplers; s++) {
250 p += sprintf(p, "uniform sampler2D tex%d;\n", s);
251 }
252 #else
253 p += sprintf(p, "uniform sampler2D tex[%d];\n", numSamplers);
254 #endif
255 p += sprintf(p, "void main()\n");
256 p += sprintf(p, "{\n");
257 p += sprintf(p, " vec4 color = vec4(0.0);\n");
258 for (s = 0; s < numSamplers; s++) {
259 #ifndef SAMPLERS_ARRAY
260 p += sprintf(p, " color += texture2D(tex%d, gl_TexCoord[0].xy);\n", s);
261 #else
262 p += sprintf(p, " color += texture2D(tex[%d], gl_TexCoord[0].xy);\n", s);
263 #endif
264 }
265 p += sprintf(p, " gl_FragColor = color;\n");
266 p += sprintf(p, "}\n");
267
268 assert(p - prog < maxLen);
269 return prog;
270 }
271
272
273 /** Create & bind shader program */
274 static GLuint
275 CreateProgram(void)
276 {
277 GLuint fragShader, vertShader, program;
278 const char *vertShaderText =
279 "void main() \n"
280 "{ \n"
281 " gl_TexCoord[0] = gl_MultiTexCoord0; \n"
282 " gl_Position = ftransform(); \n"
283 "} \n";
284 char *fragShaderText = GenFragmentShader(NumSamplers);
285
286 printf("%s", fragShaderText);
287
288 vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
289 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
290 assert(vertShader);
291 program = LinkShaders(vertShader, fragShader);
292
293 glUseProgram(program);
294
295 free(fragShaderText);
296
297 return program;
298 }
299
300
301 static void
302 InitProgram(void)
303 {
304 GLint s;
305
306 Program = CreateProgram();
307
308 /* init sampler uniforms */
309 for (s = 0; s < NumSamplers; s++) {
310 char uname[10];
311 GLint loc;
312
313 #ifndef SAMPLERS_ARRAY
314 sprintf(uname, "tex%d", s);
315 #else
316 sprintf(uname, "tex[%d]", s);
317 #endif
318 loc = glGetUniformLocation(Program, uname);
319 assert(loc >= 0);
320
321 glUniform1i(loc, s);
322 }
323 }
324
325
326 static void
327 InitGL(void)
328 {
329 if (!ShadersSupported()) {
330 printf("GLSL not supported!\n");
331 exit(1);
332 }
333
334 printf("GL_RENDERER = %s\n", (const char *) glGetString(GL_RENDERER));
335
336 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &NumSamplers);
337 if (NumSamplers > MAX_SAMPLERS)
338 NumSamplers = MAX_SAMPLERS;
339 printf("Testing %d samplers\n", NumSamplers);
340
341 InitTextures();
342 InitProgram();
343
344 glClearColor(.6, .6, .9, 0);
345 glColor3f(1.0, 1.0, 1.0);
346
347 printf("Each color corresponds to a separate sampler/texture.\n");
348 }
349
350
351 int
352 main(int argc, char *argv[])
353 {
354 glutInit(&argc, argv);
355 glutInitWindowSize(500, 400);
356 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
357 glutCreateWindow(Demo);
358 glewInit();
359 glutReshapeFunc(Reshape);
360 glutKeyboardFunc(key);
361 glutSpecialFunc(specialkey);
362 glutDisplayFunc(draw);
363 if (Anim)
364 glutIdleFunc(idle);
365 InitGL();
366 glutMainLoop();
367 return 0;
368 }