openglsupport.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2010 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #include <main.h>
  10. #include <iostream>
  11. #include <string>
  12. #if defined(__APPLE_CC__)
  13. // Prevent deprecation warnings caused by GLEW on MacOS.
  14. #define GL_SILENCE_DEPRECATION 1
  15. #endif
  16. #include <GL/glew.h>
  17. #include <Eigen/OpenGLSupport>
  18. #if defined(__APPLE_CC__)
  19. #include <GLUT/glut.h>
  20. #else
  21. #include <GL/freeglut.h>
  22. #endif
  23. using namespace Eigen;
  24. #define VERIFY_MATRIX(CODE,REF) { \
  25. glMatrixMode(GL_MODELVIEW); \
  26. glLoadIdentity(); \
  27. CODE; \
  28. Matrix<float,4,4,ColMajor> m; m.setZero(); \
  29. glGet(GL_MODELVIEW_MATRIX, m); \
  30. if(!(REF).cast<float>().isApprox(m)) { \
  31. std::cerr << "Expected:\n" << ((REF).cast<float>()) << "\n" << "got\n" << m << "\n\n"; \
  32. } \
  33. VERIFY_IS_APPROX((REF).cast<float>(), m); \
  34. }
  35. #define VERIFY_UNIFORM(SUFFIX,NAME,TYPE) { \
  36. TYPE value; value.setRandom(); \
  37. TYPE data; \
  38. int loc = glGetUniformLocation(prg_id, #NAME); \
  39. VERIFY((loc!=-1) && "uniform not found"); \
  40. glUniform(loc,value); \
  41. EIGEN_CAT(glGetUniform,SUFFIX)(prg_id,loc,data.data()); \
  42. if(!value.isApprox(data)) { \
  43. std::cerr << "Expected:\n" << value << "\n" << "got\n" << data << "\n\n"; \
  44. } \
  45. VERIFY_IS_APPROX(value, data); \
  46. }
  47. #define VERIFY_UNIFORMi(NAME,TYPE) { \
  48. TYPE value = TYPE::Random().eval().cast<float>().cast<TYPE::Scalar>(); \
  49. TYPE data; \
  50. int loc = glGetUniformLocation(prg_id, #NAME); \
  51. VERIFY((loc!=-1) && "uniform not found"); \
  52. glUniform(loc,value); \
  53. glGetUniformiv(prg_id,loc,(GLint*)data.data()); \
  54. if(!value.isApprox(data)) { \
  55. std::cerr << "Expected:\n" << value << "\n" << "got\n" << data << "\n\n"; \
  56. } \
  57. VERIFY_IS_APPROX(value, data); \
  58. }
  59. void printProgramInfoLog(GLuint objectID)
  60. {
  61. int infologLength, charsWritten;
  62. GLchar *infoLog;
  63. glGetProgramiv(objectID, GL_INFO_LOG_LENGTH, &infologLength);
  64. if(infologLength > 0)
  65. {
  66. infoLog = new GLchar[infologLength];
  67. glGetProgramInfoLog(objectID, infologLength, &charsWritten, infoLog);
  68. if (charsWritten > 0)
  69. std::cerr << "Program info : \n" << infoLog << std::endl;
  70. delete[] infoLog;
  71. }
  72. }
  73. void printShaderInfoLog(GLuint objectID)
  74. {
  75. int infologLength, charsWritten;
  76. GLchar *infoLog;
  77. glGetShaderiv(objectID, GL_INFO_LOG_LENGTH, &infologLength);
  78. if(infologLength > 0)
  79. {
  80. infoLog = new GLchar[infologLength];
  81. glGetShaderInfoLog(objectID, infologLength, &charsWritten, infoLog);
  82. if (charsWritten > 0)
  83. std::cerr << "Shader info : \n" << infoLog << std::endl;
  84. delete[] infoLog;
  85. }
  86. }
  87. GLint createProgram(const char* vtx, const char* frg, bool print_errors = true)
  88. {
  89. GLint prg_id = glCreateProgram();
  90. GLint vtx_id = glCreateShader(GL_VERTEX_SHADER);
  91. GLint frg_id = glCreateShader(GL_FRAGMENT_SHADER);
  92. GLint ok;
  93. glShaderSource(vtx_id, 1, &vtx, 0);
  94. glCompileShader(vtx_id);
  95. glGetShaderiv(vtx_id, GL_COMPILE_STATUS, &ok);
  96. if(!ok)
  97. {
  98. if (print_errors)
  99. {
  100. std::cerr << "vtx compilation failed\n";
  101. std::cerr << "Source:\n" << vtx << "\n";
  102. printShaderInfoLog(vtx_id);
  103. }
  104. glDeleteShader(vtx_id);
  105. return GL_ZERO;
  106. }
  107. glShaderSource(frg_id, 1, &frg, 0);
  108. glCompileShader(frg_id);
  109. glGetShaderiv(frg_id, GL_COMPILE_STATUS, &ok);
  110. if(!ok)
  111. {
  112. if (print_errors)
  113. {
  114. std::cerr << "frg compilation failed.\n";
  115. std::cerr << "Source:\n" << frg << "\n";
  116. printShaderInfoLog(frg_id);
  117. }
  118. glDeleteShader(vtx_id);
  119. glDeleteShader(frg_id);
  120. return GL_ZERO;
  121. }
  122. glAttachShader(prg_id, vtx_id);
  123. glAttachShader(prg_id, frg_id);
  124. glLinkProgram(prg_id);
  125. // Delete shaders once linked.
  126. glDeleteShader(vtx_id);
  127. glDeleteShader(frg_id);
  128. glGetProgramiv(prg_id, GL_LINK_STATUS, &ok);
  129. if(!ok)
  130. {
  131. if (print_errors)
  132. {
  133. std::cerr << "linking failed.\n";
  134. printProgramInfoLog(prg_id);
  135. }
  136. glDeleteProgram(prg_id);
  137. return GL_ZERO;
  138. }
  139. glUseProgram(prg_id);
  140. return prg_id;
  141. }
  142. GLint createProgram(const std::string& vtx, const std::string& frg, bool print_errors = true)
  143. {
  144. return createProgram(vtx.c_str(), frg.c_str(), print_errors);
  145. }
  146. std::string getGlslVersionString(int gl_major_version, int gl_minor_version)
  147. {
  148. switch (gl_major_version)
  149. {
  150. case 2:
  151. switch (gl_minor_version)
  152. {
  153. case 0:
  154. return "#version 110";
  155. case 1:
  156. return "#version 120";
  157. }
  158. break;
  159. case 3:
  160. switch (gl_minor_version)
  161. {
  162. case 0:
  163. return "#version 130";
  164. case 1:
  165. return "#version 140";
  166. case 2:
  167. return "#version 150";
  168. case 3:
  169. return "#version 330";
  170. }
  171. break;
  172. case 4:
  173. switch (gl_minor_version)
  174. {
  175. case 0:
  176. return "#version 400";
  177. case 1:
  178. return "#version 410";
  179. case 2:
  180. return "#version 420";
  181. case 3:
  182. return "#version 430";
  183. case 4:
  184. return "#version 440";
  185. case 5:
  186. return "#version 450";
  187. case 6:
  188. return "#version 460";
  189. }
  190. break;
  191. }
  192. return "";
  193. }
  194. void find_and_replace(
  195. std::string& str,
  196. const std::string& find,
  197. const std::string& replace)
  198. {
  199. size_t loc = 0;
  200. size_t flen = find.length();
  201. size_t rlen = replace.length();
  202. while ( (loc = str.find(find, loc)) != std::string::npos) {
  203. str.replace(loc, flen, replace);
  204. loc += rlen;
  205. }
  206. }
  207. // Finds and replaces a set of substrings in a string.
  208. std::string format(
  209. const std::string& str,
  210. const std::vector<std::string>& find,
  211. const std::vector<std::string>& replace)
  212. {
  213. std::string out = str;
  214. for (std::size_t i=0; i<find.size(); ++i) {
  215. find_and_replace(out, find[i], replace[i]);
  216. }
  217. return out;
  218. }
  219. // GLUT display function that runs test. Must be run within the display loop
  220. // in order to properly destroy resources.
  221. void openglsupport_test_loop()
  222. {
  223. // Get context info.
  224. const GLubyte* gl_version_string = glGetString(GL_VERSION);
  225. std::cerr << "GL version: " << gl_version_string << std::endl;
  226. std::cerr << "GLSL version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
  227. // Parse version from string since GL_MAJOR_VERSION is only supported in GL 3.0+.
  228. // Version string guaranteed to be <major>.<minor><vender extension>.
  229. GLint gl_major_version = gl_version_string[0] - '0';
  230. GLint gl_minor_version = gl_version_string[2] - '0';
  231. bool legacy_gl = gl_major_version < 3 || (gl_major_version == 3 && gl_minor_version < 2);
  232. // Fixed-function pipeline removed in OpenGL 3.2.
  233. if (legacy_gl)
  234. {
  235. // Draw a basic triangle.
  236. Vector3f v3f;
  237. Matrix3f rot;
  238. glBegin(GL_POINTS);
  239. {
  240. glVertex(v3f);
  241. glVertex(2*v3f+v3f);
  242. glVertex(rot*v3f);
  243. }
  244. glEnd();
  245. // 4x4 matrices
  246. Matrix4f mf44; mf44.setRandom();
  247. VERIFY_MATRIX(glLoadMatrix(mf44), mf44);
  248. VERIFY_MATRIX(glMultMatrix(mf44), mf44);
  249. Matrix4d md44; md44.setRandom();
  250. VERIFY_MATRIX(glLoadMatrix(md44), md44);
  251. VERIFY_MATRIX(glMultMatrix(md44), md44);
  252. // Quaternion
  253. Quaterniond qd(AngleAxisd(internal::random<double>(), Vector3d::Random()));
  254. VERIFY_MATRIX(glRotate(qd), Projective3d(qd).matrix());
  255. Quaternionf qf(AngleAxisf(internal::random<double>(), Vector3f::Random()));
  256. VERIFY_MATRIX(glRotate(qf), Projective3f(qf).matrix());
  257. // 3D Transform
  258. Transform<float,3,AffineCompact> acf3; acf3.matrix().setRandom();
  259. VERIFY_MATRIX(glLoadMatrix(acf3), Projective3f(acf3).matrix());
  260. VERIFY_MATRIX(glMultMatrix(acf3), Projective3f(acf3).matrix());
  261. Transform<float,3,Affine> af3(acf3);
  262. VERIFY_MATRIX(glLoadMatrix(af3), Projective3f(af3).matrix());
  263. VERIFY_MATRIX(glMultMatrix(af3), Projective3f(af3).matrix());
  264. Transform<float,3,Projective> pf3; pf3.matrix().setRandom();
  265. VERIFY_MATRIX(glLoadMatrix(pf3), Projective3f(pf3).matrix());
  266. VERIFY_MATRIX(glMultMatrix(pf3), Projective3f(pf3).matrix());
  267. Transform<double,3,AffineCompact> acd3; acd3.matrix().setRandom();
  268. VERIFY_MATRIX(glLoadMatrix(acd3), Projective3d(acd3).matrix());
  269. VERIFY_MATRIX(glMultMatrix(acd3), Projective3d(acd3).matrix());
  270. Transform<double,3,Affine> ad3(acd3);
  271. VERIFY_MATRIX(glLoadMatrix(ad3), Projective3d(ad3).matrix());
  272. VERIFY_MATRIX(glMultMatrix(ad3), Projective3d(ad3).matrix());
  273. Transform<double,3,Projective> pd3; pd3.matrix().setRandom();
  274. VERIFY_MATRIX(glLoadMatrix(pd3), Projective3d(pd3).matrix());
  275. VERIFY_MATRIX(glMultMatrix(pd3), Projective3d(pd3).matrix());
  276. // translations (2D and 3D)
  277. {
  278. Vector2f vf2; vf2.setRandom(); Vector3f vf23; vf23 << vf2, 0;
  279. VERIFY_MATRIX(glTranslate(vf2), Projective3f(Translation3f(vf23)).matrix());
  280. Vector2d vd2; vd2.setRandom(); Vector3d vd23; vd23 << vd2, 0;
  281. VERIFY_MATRIX(glTranslate(vd2), Projective3d(Translation3d(vd23)).matrix());
  282. Vector3f vf3; vf3.setRandom();
  283. VERIFY_MATRIX(glTranslate(vf3), Projective3f(Translation3f(vf3)).matrix());
  284. Vector3d vd3; vd3.setRandom();
  285. VERIFY_MATRIX(glTranslate(vd3), Projective3d(Translation3d(vd3)).matrix());
  286. Translation<float,3> tf3; tf3.vector().setRandom();
  287. VERIFY_MATRIX(glTranslate(tf3), Projective3f(tf3).matrix());
  288. Translation<double,3> td3; td3.vector().setRandom();
  289. VERIFY_MATRIX(glTranslate(td3), Projective3d(td3).matrix());
  290. }
  291. // scaling (2D and 3D)
  292. {
  293. Vector2f vf2; vf2.setRandom(); Vector3f vf23; vf23 << vf2, 1;
  294. VERIFY_MATRIX(glScale(vf2), Projective3f(Scaling(vf23)).matrix());
  295. Vector2d vd2; vd2.setRandom(); Vector3d vd23; vd23 << vd2, 1;
  296. VERIFY_MATRIX(glScale(vd2), Projective3d(Scaling(vd23)).matrix());
  297. Vector3f vf3; vf3.setRandom();
  298. VERIFY_MATRIX(glScale(vf3), Projective3f(Scaling(vf3)).matrix());
  299. Vector3d vd3; vd3.setRandom();
  300. VERIFY_MATRIX(glScale(vd3), Projective3d(Scaling(vd3)).matrix());
  301. UniformScaling<float> usf(internal::random<float>());
  302. VERIFY_MATRIX(glScale(usf), Projective3f(usf).matrix());
  303. UniformScaling<double> usd(internal::random<double>());
  304. VERIFY_MATRIX(glScale(usd), Projective3d(usd).matrix());
  305. }
  306. } else {
  307. std::cerr << "Warning: fixed-function pipeline was not tested.\n";
  308. }
  309. // Dynamic shader substitution variables.
  310. // Modern shaders require a version string, and newer runtimes fail to
  311. // compile old GLSL versions. Thus, we dynamically set the GLSL version
  312. // string based on runtime. Also, pre OpenGL 3.0, the output gl_FragColor was
  313. // built-in. This was deprecated in OpenGL 3.0, requiring us to explicitly
  314. // define the output variable.
  315. std::vector<std::string> glsl_vars;
  316. glsl_vars.push_back("${GLSL_VERSION}");
  317. glsl_vars.push_back("${FRAG_OUTPUT_DECLARATION}");
  318. glsl_vars.push_back("${FRAG_OUTPUT_VARIABLE}");
  319. std::vector<std::string> glsl_vals;
  320. glsl_vals.push_back(getGlslVersionString(gl_major_version, gl_minor_version));
  321. if (gl_major_version >= 3) {
  322. glsl_vals.push_back("out vec4 fragColor;");
  323. glsl_vals.push_back("fragColor");
  324. } else {
  325. glsl_vals.push_back("");
  326. glsl_vals.push_back("gl_FragColor");
  327. }
  328. // uniform
  329. {
  330. // vertex shader.
  331. std::string vtx = format(
  332. "${GLSL_VERSION}\n"
  333. "void main(void) {\n"
  334. " gl_Position = vec4(0,0,0,1);\n"
  335. "}\n",
  336. glsl_vars, glsl_vals);
  337. #ifdef GL_VERSION_2_0
  338. if(GLEW_VERSION_2_0 && GL_VERSION_2_0)
  339. {
  340. std::string frg = format(
  341. "${GLSL_VERSION}\n"
  342. "uniform vec2 v2f;\n"
  343. "uniform vec3 v3f;\n"
  344. "uniform vec4 v4f;\n"
  345. "uniform ivec2 v2i;\n"
  346. "uniform ivec3 v3i;\n"
  347. "uniform ivec4 v4i;\n"
  348. "uniform mat2 m2f;\n"
  349. "uniform mat3 m3f;\n"
  350. "uniform mat4 m4f;\n"
  351. "${FRAG_OUTPUT_DECLARATION}\n"
  352. "void main(void) { \n"
  353. " ${FRAG_OUTPUT_VARIABLE} = vec4(v2f[0]+v3f[0]+v4f[0])+vec4(v2i[0]+v3i[0]+v4i[0])+vec4(m2f[0][0]+m3f[0][0]+m4f[0][0]);\n"
  354. "}\n",
  355. glsl_vars, glsl_vals);
  356. GLint prg_id = createProgram(vtx, frg);
  357. VERIFY(prg_id > 0 && "Failed to create program.");
  358. VERIFY_UNIFORM(fv, v2f, Vector2f);
  359. VERIFY_UNIFORM(fv, v3f, Vector3f);
  360. VERIFY_UNIFORM(fv, v4f, Vector4f);
  361. VERIFY_UNIFORMi(v2i, Vector2i);
  362. VERIFY_UNIFORMi(v3i, Vector3i);
  363. VERIFY_UNIFORMi(v4i, Vector4i);
  364. VERIFY_UNIFORM(fv, m2f, Matrix2f);
  365. VERIFY_UNIFORM(fv, m3f, Matrix3f);
  366. VERIFY_UNIFORM(fv, m4f, Matrix4f);
  367. glDeleteProgram(prg_id);
  368. }
  369. else
  370. #endif
  371. std::cerr << "Warning: opengl 2.0 was not tested.\n";
  372. #ifdef GL_VERSION_2_1
  373. if(GLEW_VERSION_2_1 && GL_VERSION_2_1 &&
  374. (gl_major_version > 2 || (gl_major_version == 2 && gl_minor_version >= 1)))
  375. {
  376. std::string frg = format(
  377. "${GLSL_VERSION}\n"
  378. "uniform mat2x3 m23f;\n"
  379. "uniform mat3x2 m32f;\n"
  380. "uniform mat2x4 m24f;\n"
  381. "uniform mat4x2 m42f;\n"
  382. "uniform mat3x4 m34f;\n"
  383. "uniform mat4x3 m43f;\n"
  384. "${FRAG_OUTPUT_DECLARATION}\n"
  385. "void main(void) {\n"
  386. " ${FRAG_OUTPUT_VARIABLE} = vec4(m23f[0][0]+m32f[0][0]+m24f[0][0]+m42f[0][0]+m34f[0][0]+m43f[0][0]);\n"
  387. "}\n",
  388. glsl_vars, glsl_vals);
  389. GLint prg_id = createProgram(vtx, frg);
  390. VERIFY(prg_id > 0 && "Failed to create program.");
  391. typedef Matrix<float,2,3> Matrix23f;
  392. typedef Matrix<float,3,2> Matrix32f;
  393. typedef Matrix<float,2,4> Matrix24f;
  394. typedef Matrix<float,4,2> Matrix42f;
  395. typedef Matrix<float,3,4> Matrix34f;
  396. typedef Matrix<float,4,3> Matrix43f;
  397. VERIFY_UNIFORM(fv, m23f, Matrix23f);
  398. VERIFY_UNIFORM(fv, m32f, Matrix32f);
  399. VERIFY_UNIFORM(fv, m24f, Matrix24f);
  400. VERIFY_UNIFORM(fv, m42f, Matrix42f);
  401. VERIFY_UNIFORM(fv, m34f, Matrix34f);
  402. VERIFY_UNIFORM(fv, m43f, Matrix43f);
  403. glDeleteProgram(prg_id);
  404. }
  405. else
  406. #endif
  407. std::cerr << "Warning: opengl 2.1 was not tested.\n";
  408. #ifdef GL_VERSION_3_0
  409. if(GLEW_VERSION_3_0 && GL_VERSION_3_0 && gl_major_version >= 3)
  410. {
  411. std::string frg = format(
  412. "${GLSL_VERSION}\n"
  413. "uniform uvec2 v2ui;\n"
  414. "uniform uvec3 v3ui;\n"
  415. "uniform uvec4 v4ui;\n"
  416. "${FRAG_OUTPUT_DECLARATION}\n"
  417. "void main(void) {\n"
  418. " ${FRAG_OUTPUT_VARIABLE} = vec4(v2ui[0]+v3ui[0]+v4ui[0]);\n"
  419. "}\n",
  420. glsl_vars, glsl_vals);
  421. GLint prg_id = createProgram(vtx, frg);
  422. VERIFY(prg_id > 0 && "Failed to create program.");
  423. typedef Matrix<unsigned int,2,1> Vector2ui;
  424. typedef Matrix<unsigned int,3,1> Vector3ui;
  425. typedef Matrix<unsigned int,4,1> Vector4ui;
  426. VERIFY_UNIFORMi(v2ui, Vector2ui);
  427. VERIFY_UNIFORMi(v3ui, Vector3ui);
  428. VERIFY_UNIFORMi(v4ui, Vector4ui);
  429. glDeleteProgram(prg_id);
  430. }
  431. else
  432. #endif
  433. std::cerr << "Warning: opengl 3.0 was not tested.\n";
  434. // dvecn supported if >= 4.1 or ARB_vertex_attrib_64bit
  435. bool has_fp64_native = (gl_major_version == 4 && gl_minor_version >= 1);
  436. bool has_fp64_extension = false;
  437. #ifdef GLEW_ARB_gpu_shader_fp64
  438. if(GLEW_ARB_gpu_shader_fp64)
  439. {
  440. // Check that extension can actually be compiled.
  441. if (has_fp64_extension)
  442. {
  443. std::string frg = format(
  444. "${GLSL_VERSION}\n"
  445. "#extension GL_ARB_gpu_shader_fp64 : enable\n"
  446. "uniform dvec2 dv2;\n"
  447. "${FRAG_OUTPUT_DECLARATION}\n"
  448. "void main(void) {\n"
  449. " ${FRAG_OUTPUT_VARIABLE} = vec4(dv2.x, dv2.y, dv2.x, dv2.y);\n"
  450. "}\n",
  451. glsl_vars, glsl_vals);
  452. GLint prg_id = createProgram(vtx, frg, /*print_errors=*/false);
  453. if (prg_id)
  454. {
  455. has_fp64_extension = true;
  456. glDeleteProgram(prg_id);
  457. }
  458. }
  459. }
  460. #endif
  461. if( has_fp64_native || has_fp64_extension )
  462. {
  463. std::vector<std::string> glsl_vars_with_extension = glsl_vars;
  464. glsl_vars_with_extension.push_back("${GLSL_EXTENSIONS}");
  465. std::vector<std::string> glsl_vals_with_extension = glsl_vals;
  466. if (has_fp64_extension)
  467. {
  468. glsl_vals_with_extension.push_back("#extension GL_ARB_gpu_shader_fp64 : enable");
  469. }
  470. else
  471. {
  472. glsl_vals_with_extension.push_back("");
  473. }
  474. std::string frg = format(
  475. "${GLSL_VERSION}\n"
  476. "${GLSL_EXTENSIONS}\n"
  477. "uniform dvec2 v2d;\n"
  478. "uniform dvec3 v3d;\n"
  479. "uniform dvec4 v4d;\n"
  480. "${FRAG_OUTPUT_DECLARATION}\n"
  481. "void main(void) {\n"
  482. " ${FRAG_OUTPUT_VARIABLE} = vec4(v2d[0]+v3d[0]+v4d[0]);\n"
  483. "}\n",
  484. glsl_vars_with_extension, glsl_vals_with_extension);
  485. GLint prg_id = createProgram(vtx,frg);
  486. VERIFY(prg_id > 0 && "Failed to create program.");
  487. VERIFY_UNIFORM(dv, v2d, Vector2d);
  488. VERIFY_UNIFORM(dv, v3d, Vector3d);
  489. VERIFY_UNIFORM(dv, v4d, Vector4d);
  490. glDeleteProgram(prg_id);
  491. }
  492. else
  493. std::cerr << "Warning: dvec (fp64) was not tested.\n";
  494. }
  495. // Exit loop - Leaving main loop is supported by freeglut, otherwise we
  496. // are forced to exit.
  497. #ifdef FREEGLUT
  498. glutLeaveMainLoop();
  499. // Trigger another display loop iteration. Otherwise, it just hangs.
  500. glutPostRedisplay();
  501. #else
  502. exit(0);
  503. #endif
  504. }
  505. EIGEN_DECLARE_TEST(openglsupport)
  506. {
  507. int argc = 0;
  508. glutInit(&argc, 0);
  509. GLint glut_display_mode = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH;
  510. #ifndef EIGEN_LEGACY_OPENGL
  511. // Initialize 3.2+ OpenGL context.
  512. #if defined(__APPLE_CC__)
  513. glut_display_mode |= GLUT_3_2_CORE_PROFILE;
  514. #elif defined(FREEGLUT)
  515. glutInitContextVersion(3, 2);
  516. glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
  517. glutInitContextProfile(GLUT_CORE_PROFILE);
  518. #endif
  519. #endif
  520. glutInitDisplayMode(glut_display_mode);
  521. glutInitWindowPosition(0, 0);
  522. glutInitWindowSize(10, 10);
  523. int window = glutCreateWindow("Eigen");
  524. if(window <= 0)
  525. {
  526. std::cerr << "Error: Unable to create GLUT Window.\n";
  527. exit(1);
  528. }
  529. glewExperimental = GL_TRUE;
  530. if(glewInit() != GLEW_OK)
  531. {
  532. std::cerr << "Warning: Failed to initialize GLEW.\n";
  533. exit(1);
  534. }
  535. // Run test in display, otherwise GLUT fails to clean up and leads to memory
  536. // access errors on exit.
  537. glutDisplayFunc(openglsupport_test_loop);
  538. glutMainLoop();
  539. glutDestroyWindow(window);
  540. }