Brian Paul | 46b8fe0 | 2009-01-01 14:02:17 -0700 | [diff] [blame] | 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> |
Keith Whitwell | b799af9 | 2009-06-29 14:13:58 +0100 | [diff] [blame] | 42 | #include <GL/glew.h> |
Brian Paul | 46b8fe0 | 2009-01-01 14:02:17 -0700 | [diff] [blame] | 43 | #include "GL/glut.h" |
| 44 | #include "readtex.h" |
Brian Paul | 46b8fe0 | 2009-01-01 14:02:17 -0700 | [diff] [blame] | 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); |
Brian Paul | f6d34c2 | 2009-08-26 11:53:25 -0600 | [diff] [blame^] | 214 | if (x < size / 2) { |
| 215 | texImage[k + 0] = intensity; |
| 216 | texImage[k + 1] = intensity; |
| 217 | texImage[k + 2] = 0; |
| 218 | texImage[k + 3] = 255; |
| 219 | } |
| 220 | else { |
| 221 | texImage[k + 0] = 255 - intensity; |
| 222 | texImage[k + 1] = 0; |
| 223 | texImage[k + 2] = 0; |
| 224 | texImage[k + 3] = 255; |
| 225 | } |
Brian Paul | 46b8fe0 | 2009-01-01 14:02:17 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | glActiveTexture(GL_TEXTURE0 + s); |
| 230 | glBindTexture(GL_TEXTURE_2D, Textures[s]); |
| 231 | gluBuild2DMipmaps(GL_TEXTURE_2D, 4, size, size, |
| 232 | GL_RGBA, GL_UNSIGNED_BYTE, texImage); |
| 233 | |
| 234 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 235 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 236 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter); |
| 237 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter); |
| 238 | } |
| 239 | |
| 240 | free(texImage); |
| 241 | } |
| 242 | |
| 243 | |
| 244 | /** |
| 245 | * Generate a fragment shader that uses the given number of samplers. |
| 246 | */ |
| 247 | static char * |
| 248 | GenFragmentShader(GLint numSamplers) |
| 249 | { |
| 250 | const int maxLen = 10 * 1000; |
| 251 | char *prog = (char *) malloc(maxLen); |
| 252 | char *p = prog; |
| 253 | int s; |
| 254 | |
| 255 | p += sprintf(p, "// Generated fragment shader:\n"); |
Alan Hourihane | e827845 | 2009-01-14 17:01:16 +0000 | [diff] [blame] | 256 | #ifndef SAMPLERS_ARRAY |
Brian Paul | 46b8fe0 | 2009-01-01 14:02:17 -0700 | [diff] [blame] | 257 | for (s = 0; s < numSamplers; s++) { |
| 258 | p += sprintf(p, "uniform sampler2D tex%d;\n", s); |
| 259 | } |
Alan Hourihane | e827845 | 2009-01-14 17:01:16 +0000 | [diff] [blame] | 260 | #else |
| 261 | p += sprintf(p, "uniform sampler2D tex[%d];\n", numSamplers); |
| 262 | #endif |
Brian Paul | 46b8fe0 | 2009-01-01 14:02:17 -0700 | [diff] [blame] | 263 | p += sprintf(p, "void main()\n"); |
| 264 | p += sprintf(p, "{\n"); |
| 265 | p += sprintf(p, " vec4 color = vec4(0.0);\n"); |
| 266 | for (s = 0; s < numSamplers; s++) { |
Alan Hourihane | e827845 | 2009-01-14 17:01:16 +0000 | [diff] [blame] | 267 | #ifndef SAMPLERS_ARRAY |
Brian Paul | 46b8fe0 | 2009-01-01 14:02:17 -0700 | [diff] [blame] | 268 | p += sprintf(p, " color += texture2D(tex%d, gl_TexCoord[0].xy);\n", s); |
Alan Hourihane | e827845 | 2009-01-14 17:01:16 +0000 | [diff] [blame] | 269 | #else |
| 270 | p += sprintf(p, " color += texture2D(tex[%d], gl_TexCoord[0].xy);\n", s); |
| 271 | #endif |
Brian Paul | 46b8fe0 | 2009-01-01 14:02:17 -0700 | [diff] [blame] | 272 | } |
| 273 | p += sprintf(p, " gl_FragColor = color;\n"); |
| 274 | p += sprintf(p, "}\n"); |
| 275 | |
| 276 | assert(p - prog < maxLen); |
| 277 | return prog; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | /** Create & bind shader program */ |
| 282 | static GLuint |
| 283 | CreateProgram(void) |
| 284 | { |
| 285 | GLuint fragShader, vertShader, program; |
| 286 | const char *vertShaderText = |
| 287 | "void main() \n" |
| 288 | "{ \n" |
| 289 | " gl_TexCoord[0] = gl_MultiTexCoord0; \n" |
| 290 | " gl_Position = ftransform(); \n" |
| 291 | "} \n"; |
| 292 | char *fragShaderText = GenFragmentShader(NumSamplers); |
| 293 | |
| 294 | printf("%s", fragShaderText); |
| 295 | |
| 296 | vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText); |
| 297 | fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText); |
| 298 | assert(vertShader); |
| 299 | program = LinkShaders(vertShader, fragShader); |
| 300 | |
Brian Paul | ee0b1bc | 2009-07-17 13:23:11 -0600 | [diff] [blame] | 301 | glUseProgram(program); |
Brian Paul | 46b8fe0 | 2009-01-01 14:02:17 -0700 | [diff] [blame] | 302 | |
| 303 | free(fragShaderText); |
| 304 | |
| 305 | return program; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | static void |
| 310 | InitProgram(void) |
| 311 | { |
| 312 | GLint s; |
| 313 | |
| 314 | Program = CreateProgram(); |
| 315 | |
| 316 | /* init sampler uniforms */ |
| 317 | for (s = 0; s < NumSamplers; s++) { |
| 318 | char uname[10]; |
| 319 | GLint loc; |
| 320 | |
Alan Hourihane | e827845 | 2009-01-14 17:01:16 +0000 | [diff] [blame] | 321 | #ifndef SAMPLERS_ARRAY |
Brian Paul | 46b8fe0 | 2009-01-01 14:02:17 -0700 | [diff] [blame] | 322 | sprintf(uname, "tex%d", s); |
Alan Hourihane | e827845 | 2009-01-14 17:01:16 +0000 | [diff] [blame] | 323 | #else |
| 324 | sprintf(uname, "tex[%d]", s); |
| 325 | #endif |
Brian Paul | ee0b1bc | 2009-07-17 13:23:11 -0600 | [diff] [blame] | 326 | loc = glGetUniformLocation(Program, uname); |
Brian Paul | 46b8fe0 | 2009-01-01 14:02:17 -0700 | [diff] [blame] | 327 | assert(loc >= 0); |
| 328 | |
Brian Paul | ee0b1bc | 2009-07-17 13:23:11 -0600 | [diff] [blame] | 329 | glUniform1i(loc, s); |
Brian Paul | 46b8fe0 | 2009-01-01 14:02:17 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | |
| 334 | static void |
| 335 | InitGL(void) |
| 336 | { |
| 337 | if (!ShadersSupported()) { |
| 338 | printf("GLSL not supported!\n"); |
| 339 | exit(1); |
| 340 | } |
| 341 | |
| 342 | printf("GL_RENDERER = %s\n", (const char *) glGetString(GL_RENDERER)); |
| 343 | |
Brian Paul | 46b8fe0 | 2009-01-01 14:02:17 -0700 | [diff] [blame] | 344 | glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &NumSamplers); |
| 345 | if (NumSamplers > MAX_SAMPLERS) |
| 346 | NumSamplers = MAX_SAMPLERS; |
| 347 | printf("Testing %d samplers\n", NumSamplers); |
| 348 | |
| 349 | InitTextures(); |
| 350 | InitProgram(); |
| 351 | |
| 352 | glClearColor(.6, .6, .9, 0); |
| 353 | glColor3f(1.0, 1.0, 1.0); |
| 354 | |
| 355 | printf("Each color corresponds to a separate sampler/texture.\n"); |
| 356 | } |
| 357 | |
| 358 | |
| 359 | int |
| 360 | main(int argc, char *argv[]) |
| 361 | { |
| 362 | glutInit(&argc, argv); |
| 363 | glutInitWindowSize(500, 400); |
| 364 | glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE); |
| 365 | glutCreateWindow(Demo); |
Keith Whitwell | b799af9 | 2009-06-29 14:13:58 +0100 | [diff] [blame] | 366 | glewInit(); |
Brian Paul | 46b8fe0 | 2009-01-01 14:02:17 -0700 | [diff] [blame] | 367 | glutReshapeFunc(Reshape); |
| 368 | glutKeyboardFunc(key); |
| 369 | glutSpecialFunc(specialkey); |
| 370 | glutDisplayFunc(draw); |
| 371 | if (Anim) |
| 372 | glutIdleFunc(idle); |
| 373 | InitGL(); |
| 374 | glutMainLoop(); |
| 375 | return 0; |
| 376 | } |