Clone of mesa.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

samplers.c 8.7KB

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