cbb264dad17219d2b8b5e1b6d259fbd70243957f
[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 "extfuncs.h"
46 #include "shaderutil.h"
47
48
49 #define MAX_SAMPLERS 128
50
51
52 static const char *Demo = "samplers";
53
54 static GLuint Program;
55 static GLint NumSamplers;
56 static GLuint Textures[MAX_SAMPLERS];
57 static GLfloat Xrot = 0.0, Yrot = .0, Zrot = 0.0;
58 static GLfloat EyeDist = 10;
59 static GLboolean Anim = GL_FALSE;
60
61
62 static void
63 DrawPolygon(GLfloat size)
64 {
65 glPushMatrix();
66 glNormal3f(0, 0, 1);
67 glBegin(GL_POLYGON);
68
69 glMultiTexCoord2f(GL_TEXTURE0, 0, 0);
70 glVertex2f(-size, -size);
71
72 glMultiTexCoord2f(GL_TEXTURE0, 1, 0);
73 glVertex2f( size, -size);
74
75 glMultiTexCoord2f(GL_TEXTURE0, 1, 1);
76 glVertex2f( size, size);
77
78 glMultiTexCoord2f(GL_TEXTURE0, 0, 1);
79 glVertex2f(-size, size);
80
81 glEnd();
82 glPopMatrix();
83 }
84
85
86 static void
87 draw(void)
88 {
89 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
90
91 glPushMatrix();
92 glTranslatef(0.0, 0.0, -EyeDist);
93 glRotatef(Zrot, 0, 0, 1);
94 glRotatef(Yrot, 0, 1, 0);
95 glRotatef(Xrot, 1, 0, 0);
96
97 DrawPolygon(3.0);
98
99 glPopMatrix();
100
101 glutSwapBuffers();
102 }
103
104
105 static void
106 idle(void)
107 {
108 GLfloat t = 0.05 * glutGet(GLUT_ELAPSED_TIME);
109 Yrot = t;
110 glutPostRedisplay();
111 }
112
113
114 static void
115 key(unsigned char k, int x, int y)
116 {
117 (void) x;
118 (void) y;
119 switch (k) {
120 case ' ':
121 case 'a':
122 Anim = !Anim;
123 if (Anim)
124 glutIdleFunc(idle);
125 else
126 glutIdleFunc(NULL);
127 break;
128 case 'z':
129 EyeDist -= 0.5;
130 if (EyeDist < 3.0)
131 EyeDist = 3.0;
132 break;
133 case 'Z':
134 EyeDist += 0.5;
135 if (EyeDist > 90.0)
136 EyeDist = 90;
137 break;
138 case 27:
139 exit(0);
140 }
141 glutPostRedisplay();
142 }
143
144
145 static void
146 specialkey(int key, int x, int y)
147 {
148 GLfloat step = 2.0;
149 (void) x;
150 (void) y;
151 switch (key) {
152 case GLUT_KEY_UP:
153 Xrot += step;
154 break;
155 case GLUT_KEY_DOWN:
156 Xrot -= step;
157 break;
158 case GLUT_KEY_LEFT:
159 Yrot -= step;
160 break;
161 case GLUT_KEY_RIGHT:
162 Yrot += step;
163 break;
164 }
165 glutPostRedisplay();
166 }
167
168
169 /* new window size or exposure */
170 static void
171 Reshape(int width, int height)
172 {
173 GLfloat ar = (float) width / (float) height;
174 glViewport(0, 0, (GLint)width, (GLint)height);
175 glMatrixMode(GL_PROJECTION);
176 glLoadIdentity();
177 glFrustum(-2.0*ar, 2.0*ar, -2.0, 2.0, 4.0, 100.0);
178 glMatrixMode(GL_MODELVIEW);
179 glLoadIdentity();
180 }
181
182
183 static void
184 InitTextures(void)
185 {
186 const GLint size = MAX_SAMPLERS;
187 GLubyte *texImage;
188 GLenum filter = GL_NEAREST;
189 GLint stripeSize;
190 GLint s;
191
192 texImage = (GLubyte *) malloc(size * size * 4);
193
194 glGenTextures(NumSamplers, Textures);
195
196 /* size of texels stripe */
197 stripeSize = size / NumSamplers;
198
199 /* create a texture for each sampler */
200 for (s = 0; s < NumSamplers; s++) {
201 GLint x, y, ypos;
202 GLubyte intensity = 31 + s * (256-32) / (NumSamplers - 1);
203
204 printf("Texture %d: color = %d, %d, %d\n", s,
205 (int) intensity, 0, (int) intensity );
206
207 /* initialize the texture to black */
208 memset(texImage, 0, size * size * 4);
209
210 /* set a stripe of texels to the intensity value */
211 ypos = s * stripeSize;
212 for (y = 0; y < stripeSize; y++) {
213 for (x = 0; x < size; x++) {
214 GLint k = 4 * ((ypos + y) * size + x);
215 texImage[k + 0] = intensity;
216 texImage[k + 1] = intensity;
217 texImage[k + 2] = 0;
218 texImage[k + 3] = 255;
219 }
220 }
221
222 glActiveTexture(GL_TEXTURE0 + s);
223 glBindTexture(GL_TEXTURE_2D, Textures[s]);
224 gluBuild2DMipmaps(GL_TEXTURE_2D, 4, size, size,
225 GL_RGBA, GL_UNSIGNED_BYTE, texImage);
226
227 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
228 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
229 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
230 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
231 }
232
233 free(texImage);
234 }
235
236
237 /**
238 * Generate a fragment shader that uses the given number of samplers.
239 */
240 static char *
241 GenFragmentShader(GLint numSamplers)
242 {
243 const int maxLen = 10 * 1000;
244 char *prog = (char *) malloc(maxLen);
245 char *p = prog;
246 int s;
247
248 p += sprintf(p, "// Generated fragment shader:\n");
249 #ifndef SAMPLERS_ARRAY
250 for (s = 0; s < numSamplers; s++) {
251 p += sprintf(p, "uniform sampler2D tex%d;\n", s);
252 }
253 #else
254 p += sprintf(p, "uniform sampler2D tex[%d];\n", numSamplers);
255 #endif
256 p += sprintf(p, "void main()\n");
257 p += sprintf(p, "{\n");
258 p += sprintf(p, " vec4 color = vec4(0.0);\n");
259 for (s = 0; s < numSamplers; s++) {
260 #ifndef SAMPLERS_ARRAY
261 p += sprintf(p, " color += texture2D(tex%d, gl_TexCoord[0].xy);\n", s);
262 #else
263 p += sprintf(p, " color += texture2D(tex[%d], gl_TexCoord[0].xy);\n", s);
264 #endif
265 }
266 p += sprintf(p, " gl_FragColor = color;\n");
267 p += sprintf(p, "}\n");
268
269 assert(p - prog < maxLen);
270 return prog;
271 }
272
273
274 /** Create & bind shader program */
275 static GLuint
276 CreateProgram(void)
277 {
278 GLuint fragShader, vertShader, program;
279 const char *vertShaderText =
280 "void main() \n"
281 "{ \n"
282 " gl_TexCoord[0] = gl_MultiTexCoord0; \n"
283 " gl_Position = ftransform(); \n"
284 "} \n";
285 char *fragShaderText = GenFragmentShader(NumSamplers);
286
287 printf("%s", fragShaderText);
288
289 vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
290 fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
291 assert(vertShader);
292 program = LinkShaders(vertShader, fragShader);
293
294 glUseProgram_func(program);
295
296 free(fragShaderText);
297
298 return program;
299 }
300
301
302 static void
303 InitProgram(void)
304 {
305 GLint s;
306
307 Program = CreateProgram();
308
309 /* init sampler uniforms */
310 for (s = 0; s < NumSamplers; s++) {
311 char uname[10];
312 GLint loc;
313
314 #ifndef SAMPLERS_ARRAY
315 sprintf(uname, "tex%d", s);
316 #else
317 sprintf(uname, "tex[%d]", s);
318 #endif
319 loc = glGetUniformLocation_func(Program, uname);
320 assert(loc >= 0);
321
322 glUniform1i_func(loc, s);
323 }
324 }
325
326
327 static void
328 InitGL(void)
329 {
330 if (!ShadersSupported()) {
331 printf("GLSL not supported!\n");
332 exit(1);
333 }
334
335 printf("GL_RENDERER = %s\n", (const char *) glGetString(GL_RENDERER));
336
337 GetExtensionFuncs();
338
339 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &NumSamplers);
340 if (NumSamplers > MAX_SAMPLERS)
341 NumSamplers = MAX_SAMPLERS;
342 printf("Testing %d samplers\n", NumSamplers);
343
344 InitTextures();
345 InitProgram();
346
347 glClearColor(.6, .6, .9, 0);
348 glColor3f(1.0, 1.0, 1.0);
349
350 printf("Each color corresponds to a separate sampler/texture.\n");
351 }
352
353
354 int
355 main(int argc, char *argv[])
356 {
357 glutInit(&argc, argv);
358 glutInitWindowSize(500, 400);
359 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
360 glutCreateWindow(Demo);
361 glewInit();
362 glutReshapeFunc(Reshape);
363 glutKeyboardFunc(key);
364 glutSpecialFunc(specialkey);
365 glutDisplayFunc(draw);
366 if (Anim)
367 glutIdleFunc(idle);
368 InitGL();
369 glutMainLoop();
370 return 0;
371 }