Clone of mesa.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. * We generate a fragment shader which uses the maximum number of supported
  25. * texture samplers.
  26. * For each sampler we create a separate texture. Each texture has a
  27. * single strip of color at a different intensity. The fragment shader
  28. * samples all the textures at the same coordinate and sums the values.
  29. * The result should be a quad with rows of colors of increasing intensity
  30. * from bottom to top.
  31. *
  32. * Brian Paul
  33. * 1 Jan 2009
  34. */
  35. #include <assert.h>
  36. #include <math.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <GL/glew.h>
  41. #include "GL/glut.h"
  42. #include "readtex.h"
  43. #include "shaderutil.h"
  44. #define MAX_SAMPLERS 128
  45. static const char *Demo = "samplers";
  46. static GLuint Program;
  47. static GLint NumSamplers;
  48. static GLuint Textures[MAX_SAMPLERS];
  49. static GLfloat Xrot = 0.0, Yrot = .0, Zrot = 0.0;
  50. static GLfloat EyeDist = 10;
  51. static GLboolean Anim = GL_FALSE;
  52. static void
  53. DrawPolygon(GLfloat size)
  54. {
  55. glPushMatrix();
  56. glNormal3f(0, 0, 1);
  57. glBegin(GL_POLYGON);
  58. glMultiTexCoord2f(GL_TEXTURE0, 0, 0);
  59. glVertex2f(-size, -size);
  60. glMultiTexCoord2f(GL_TEXTURE0, 1, 0);
  61. glVertex2f( size, -size);
  62. glMultiTexCoord2f(GL_TEXTURE0, 1, 1);
  63. glVertex2f( size, size);
  64. glMultiTexCoord2f(GL_TEXTURE0, 0, 1);
  65. glVertex2f(-size, size);
  66. glEnd();
  67. glPopMatrix();
  68. }
  69. static void
  70. draw(void)
  71. {
  72. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  73. glPushMatrix();
  74. glTranslatef(0.0, 0.0, -EyeDist);
  75. glRotatef(Zrot, 0, 0, 1);
  76. glRotatef(Yrot, 0, 1, 0);
  77. glRotatef(Xrot, 1, 0, 0);
  78. DrawPolygon(3.0);
  79. glPopMatrix();
  80. glutSwapBuffers();
  81. }
  82. static void
  83. idle(void)
  84. {
  85. GLfloat t = 0.05 * glutGet(GLUT_ELAPSED_TIME);
  86. Yrot = t;
  87. glutPostRedisplay();
  88. }
  89. static void
  90. key(unsigned char k, int x, int y)
  91. {
  92. (void) x;
  93. (void) y;
  94. switch (k) {
  95. case ' ':
  96. case 'a':
  97. Anim = !Anim;
  98. if (Anim)
  99. glutIdleFunc(idle);
  100. else
  101. glutIdleFunc(NULL);
  102. break;
  103. case 'z':
  104. EyeDist -= 0.5;
  105. if (EyeDist < 3.0)
  106. EyeDist = 3.0;
  107. break;
  108. case 'Z':
  109. EyeDist += 0.5;
  110. if (EyeDist > 90.0)
  111. EyeDist = 90;
  112. break;
  113. case 27:
  114. exit(0);
  115. }
  116. glutPostRedisplay();
  117. }
  118. static void
  119. specialkey(int key, int x, int y)
  120. {
  121. GLfloat step = 2.0;
  122. (void) x;
  123. (void) y;
  124. switch (key) {
  125. case GLUT_KEY_UP:
  126. Xrot += step;
  127. break;
  128. case GLUT_KEY_DOWN:
  129. Xrot -= step;
  130. break;
  131. case GLUT_KEY_LEFT:
  132. Yrot -= step;
  133. break;
  134. case GLUT_KEY_RIGHT:
  135. Yrot += step;
  136. break;
  137. }
  138. glutPostRedisplay();
  139. }
  140. /* new window size or exposure */
  141. static void
  142. Reshape(int width, int height)
  143. {
  144. GLfloat ar = (float) width / (float) height;
  145. glViewport(0, 0, (GLint)width, (GLint)height);
  146. glMatrixMode(GL_PROJECTION);
  147. glLoadIdentity();
  148. glFrustum(-2.0*ar, 2.0*ar, -2.0, 2.0, 4.0, 100.0);
  149. glMatrixMode(GL_MODELVIEW);
  150. glLoadIdentity();
  151. }
  152. static void
  153. InitTextures(void)
  154. {
  155. const GLint size = MAX_SAMPLERS;
  156. GLubyte *texImage;
  157. GLenum filter = GL_NEAREST;
  158. GLint stripeSize;
  159. GLint s;
  160. texImage = (GLubyte *) malloc(size * size * 4);
  161. glGenTextures(NumSamplers, Textures);
  162. /* size of texels stripe */
  163. stripeSize = size / NumSamplers;
  164. /* create a texture for each sampler */
  165. for (s = 0; s < NumSamplers; s++) {
  166. GLint x, y, ypos;
  167. GLubyte intensity = 31 + s * (256-32) / (NumSamplers - 1);
  168. printf("Texture %d: color = %d, %d, %d\n", s,
  169. (int) intensity, 0, (int) intensity );
  170. /* initialize the texture to black */
  171. memset(texImage, 0, size * size * 4);
  172. /* set a stripe of texels to the intensity value */
  173. ypos = s * stripeSize;
  174. for (y = 0; y < stripeSize; y++) {
  175. for (x = 0; x < size; x++) {
  176. GLint k = 4 * ((ypos + y) * size + x);
  177. if (x < size / 2) {
  178. texImage[k + 0] = intensity;
  179. texImage[k + 1] = intensity;
  180. texImage[k + 2] = 0;
  181. texImage[k + 3] = 255;
  182. }
  183. else {
  184. texImage[k + 0] = 255 - intensity;
  185. texImage[k + 1] = 0;
  186. texImage[k + 2] = 0;
  187. texImage[k + 3] = 255;
  188. }
  189. }
  190. }
  191. glActiveTexture(GL_TEXTURE0 + s);
  192. glBindTexture(GL_TEXTURE_2D, Textures[s]);
  193. gluBuild2DMipmaps(GL_TEXTURE_2D, 4, size, size,
  194. GL_RGBA, GL_UNSIGNED_BYTE, texImage);
  195. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  196. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  197. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
  198. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
  199. }
  200. free(texImage);
  201. }
  202. /**
  203. * Generate a fragment shader that uses the given number of samplers.
  204. */
  205. static char *
  206. GenFragmentShader(GLint numSamplers)
  207. {
  208. const int maxLen = 10 * 1000;
  209. char *prog = (char *) malloc(maxLen);
  210. char *p = prog;
  211. int s;
  212. p += sprintf(p, "// Generated fragment shader:\n");
  213. #ifndef SAMPLERS_ARRAY
  214. for (s = 0; s < numSamplers; s++) {
  215. p += sprintf(p, "uniform sampler2D tex%d;\n", s);
  216. }
  217. #else
  218. p += sprintf(p, "uniform sampler2D tex[%d];\n", numSamplers);
  219. #endif
  220. p += sprintf(p, "void main()\n");
  221. p += sprintf(p, "{\n");
  222. p += sprintf(p, " vec4 color = vec4(0.0);\n");
  223. for (s = 0; s < numSamplers; s++) {
  224. #ifndef SAMPLERS_ARRAY
  225. p += sprintf(p, " color += texture2D(tex%d, gl_TexCoord[0].xy);\n", s);
  226. #else
  227. p += sprintf(p, " color += texture2D(tex[%d], gl_TexCoord[0].xy);\n", s);
  228. #endif
  229. }
  230. p += sprintf(p, " gl_FragColor = color;\n");
  231. p += sprintf(p, "}\n");
  232. assert(p - prog < maxLen);
  233. return prog;
  234. }
  235. /** Create & bind shader program */
  236. static GLuint
  237. CreateProgram(void)
  238. {
  239. GLuint fragShader, vertShader, program;
  240. const char *vertShaderText =
  241. "void main() \n"
  242. "{ \n"
  243. " gl_TexCoord[0] = gl_MultiTexCoord0; \n"
  244. " gl_Position = ftransform(); \n"
  245. "} \n";
  246. char *fragShaderText = GenFragmentShader(NumSamplers);
  247. printf("%s", fragShaderText);
  248. vertShader = CompileShaderText(GL_VERTEX_SHADER, vertShaderText);
  249. fragShader = CompileShaderText(GL_FRAGMENT_SHADER, fragShaderText);
  250. assert(vertShader);
  251. program = LinkShaders(vertShader, fragShader);
  252. glUseProgram(program);
  253. free(fragShaderText);
  254. return program;
  255. }
  256. static void
  257. InitProgram(void)
  258. {
  259. GLint s;
  260. Program = CreateProgram();
  261. /* init sampler uniforms */
  262. for (s = 0; s < NumSamplers; s++) {
  263. char uname[10];
  264. GLint loc;
  265. #ifndef SAMPLERS_ARRAY
  266. sprintf(uname, "tex%d", s);
  267. #else
  268. sprintf(uname, "tex[%d]", s);
  269. #endif
  270. loc = glGetUniformLocation(Program, uname);
  271. assert(loc >= 0);
  272. glUniform1i(loc, s);
  273. }
  274. }
  275. static void
  276. InitGL(void)
  277. {
  278. if (!ShadersSupported()) {
  279. printf("GLSL not supported!\n");
  280. exit(1);
  281. }
  282. printf("GL_RENDERER = %s\n", (const char *) glGetString(GL_RENDERER));
  283. glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &NumSamplers);
  284. if (NumSamplers > MAX_SAMPLERS)
  285. NumSamplers = MAX_SAMPLERS;
  286. printf("Testing %d samplers\n", NumSamplers);
  287. InitTextures();
  288. InitProgram();
  289. glClearColor(.6, .6, .9, 0);
  290. glColor3f(1.0, 1.0, 1.0);
  291. printf("Each color corresponds to a separate sampler/texture.\n");
  292. }
  293. int
  294. main(int argc, char *argv[])
  295. {
  296. glutInit(&argc, argv);
  297. glutInitWindowSize(500, 400);
  298. glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
  299. glutCreateWindow(Demo);
  300. glewInit();
  301. glutReshapeFunc(Reshape);
  302. glutKeyboardFunc(key);
  303. glutSpecialFunc(specialkey);
  304. glutDisplayFunc(draw);
  305. if (Anim)
  306. glutIdleFunc(idle);
  307. InitGL();
  308. glutMainLoop();
  309. return 0;
  310. }