glsl_optimizer_tests.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. #include <string>
  2. #include <vector>
  3. #include <time.h>
  4. #include "../src/glsl/glsl_optimizer.h"
  5. #define GL_GLEXT_PROTOTYPES 1
  6. #if __linux__
  7. #define GOT_GFX 0
  8. #else
  9. #define GOT_GFX 1
  10. #endif
  11. #if GOT_GFX
  12. // ---- Windows GL bits
  13. #ifdef _MSC_VER
  14. #define GOT_MORE_THAN_GLSL_120 1
  15. #include <windows.h>
  16. #include <gl/GL.h>
  17. extern "C" {
  18. typedef char GLchar; /* native character */
  19. typedef unsigned int GLuint; /* shader object handle */
  20. #define GL_VERTEX_SHADER 0x8B31
  21. #define GL_FRAGMENT_SHADER 0x8B30
  22. #define GL_COMPILE_STATUS 0x8B81
  23. typedef void (WINAPI * PFNGLDELETESHADERPROC) (GLuint shader);
  24. typedef GLuint (WINAPI * PFNGLCREATESHADERPROC) (GLenum type);
  25. typedef void (WINAPI * PFNGLSHADERSOURCEPROC) (GLuint shader, GLsizei count, const GLchar *const*string, const GLint *length);
  26. typedef void (WINAPI * PFNGLCOMPILESHADERPROC) (GLuint shader);
  27. typedef void (WINAPI * PFNGLGETSHADERINFOLOGPROC) (GLuint shader, GLsizei bufSize, GLsizei *length, GLchar *infoLog);
  28. typedef void (WINAPI * PFNGLGETSHADERIVPROC) (GLuint shader, GLenum pname, GLint *params);
  29. static PFNGLDELETESHADERPROC glDeleteShader;
  30. static PFNGLCREATESHADERPROC glCreateShader;
  31. static PFNGLSHADERSOURCEPROC glShaderSource;
  32. static PFNGLCOMPILESHADERPROC glCompileShader;
  33. static PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
  34. static PFNGLGETSHADERIVPROC glGetShaderiv;
  35. }
  36. #endif // #ifdef _MSC_VER
  37. // ---- Apple GL bits
  38. #ifdef __APPLE__
  39. #define GOT_MORE_THAN_GLSL_120 0
  40. #include <OpenGL/OpenGL.h>
  41. #include <OpenGL/gl.h>
  42. #include <OpenGL/CGLTypes.h>
  43. #include <dirent.h>
  44. static CGLContextObj s_GLContext;
  45. static CGLContextObj s_GLContext3;
  46. static bool s_GL3Active = false;
  47. #endif // ifdef __APPLE__
  48. #else // #if GOT_GFX
  49. #define GOT_MORE_THAN_GLSL_120 0
  50. #include <cstdio>
  51. #include <cstring>
  52. #include "dirent.h"
  53. #include "GL/gl.h"
  54. #include "GL/glext.h"
  55. #endif // ! #if GOT_GFX
  56. #ifndef _MSC_VER
  57. #include <unistd.h>
  58. #endif
  59. static bool InitializeOpenGL ()
  60. {
  61. bool hasGLSL = false;
  62. #if GOT_GFX
  63. #ifdef _MSC_VER
  64. // setup minimal required GL
  65. HWND wnd = CreateWindowA(
  66. "STATIC",
  67. "GL",
  68. WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
  69. 0, 0, 16, 16,
  70. NULL, NULL,
  71. GetModuleHandle(NULL), NULL );
  72. HDC dc = GetDC( wnd );
  73. PIXELFORMATDESCRIPTOR pfd = {
  74. sizeof(PIXELFORMATDESCRIPTOR), 1,
  75. PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL,
  76. PFD_TYPE_RGBA, 32,
  77. 0, 0, 0, 0, 0, 0,
  78. 0, 0, 0, 0, 0, 0, 0,
  79. 16, 0,
  80. 0, PFD_MAIN_PLANE, 0, 0, 0, 0
  81. };
  82. int fmt = ChoosePixelFormat( dc, &pfd );
  83. SetPixelFormat( dc, fmt, &pfd );
  84. HGLRC rc = wglCreateContext( dc );
  85. wglMakeCurrent( dc, rc );
  86. #elif defined(__APPLE__)
  87. CGLPixelFormatAttribute attributes[] = {
  88. kCGLPFAAccelerated, // no software rendering
  89. (CGLPixelFormatAttribute) 0
  90. };
  91. CGLPixelFormatAttribute attributes3[] = {
  92. kCGLPFAAccelerated, // no software rendering
  93. kCGLPFAOpenGLProfile, // core profile with the version stated below
  94. (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core,
  95. (CGLPixelFormatAttribute) 0
  96. };
  97. GLint num;
  98. CGLPixelFormatObj pix;
  99. // create legacy context
  100. CGLChoosePixelFormat(attributes, &pix, &num);
  101. if (pix == NULL)
  102. return false;
  103. CGLCreateContext(pix, NULL, &s_GLContext);
  104. if (s_GLContext == NULL)
  105. return false;
  106. CGLDestroyPixelFormat(pix);
  107. CGLSetCurrentContext(s_GLContext);
  108. // create core 3.2 context
  109. CGLChoosePixelFormat(attributes3, &pix, &num);
  110. if (pix == NULL)
  111. return false;
  112. CGLCreateContext(pix, NULL, &s_GLContext3);
  113. if (s_GLContext3 == NULL)
  114. return false;
  115. CGLDestroyPixelFormat(pix);
  116. #endif
  117. // check if we have GLSL
  118. const char* extensions = (const char*)glGetString(GL_EXTENSIONS);
  119. hasGLSL = extensions != NULL && strstr(extensions, "GL_ARB_shader_objects") && strstr(extensions, "GL_ARB_vertex_shader") && strstr(extensions, "GL_ARB_fragment_shader");
  120. #if defined(__APPLE__)
  121. // using core profile; always has GLSL
  122. hasGLSL = true;
  123. #endif
  124. #ifdef _MSC_VER
  125. if (hasGLSL)
  126. {
  127. glDeleteShader = (PFNGLDELETESHADERPROC)wglGetProcAddress("glDeleteShader");
  128. glCreateShader = (PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader");
  129. glShaderSource = (PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource");
  130. glCompileShader = (PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader");
  131. glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)wglGetProcAddress("glGetShaderInfoLog");
  132. glGetShaderiv = (PFNGLGETSHADERIVPROC)wglGetProcAddress("glGetShaderiv");
  133. }
  134. #endif
  135. #endif
  136. return hasGLSL;
  137. }
  138. static void CleanupGL()
  139. {
  140. #if GOT_GFX
  141. #ifdef __APPLE__
  142. CGLSetCurrentContext(NULL);
  143. if (s_GLContext)
  144. CGLDestroyContext(s_GLContext);
  145. if (s_GLContext3)
  146. CGLDestroyContext(s_GLContext3);
  147. #endif // #ifdef __APPLE__
  148. #endif // #if GOT_GFX
  149. }
  150. static bool InitializeMetal ()
  151. {
  152. bool hasMetal = false;
  153. #if defined(__APPLE__)
  154. hasMetal = true; //@TODO: detect metal compiler presence
  155. #endif
  156. return hasMetal;
  157. }
  158. static void replace_string (std::string& target, const std::string& search, const std::string& replace, size_t startPos)
  159. {
  160. if (search.empty())
  161. return;
  162. std::string::size_type p = startPos;
  163. while ((p = target.find (search, p)) != std::string::npos)
  164. {
  165. target.replace (p, search.size (), replace);
  166. p += replace.size ();
  167. }
  168. }
  169. static bool CheckGLSL (bool vertex, bool gles, const std::string& testName, const char* prefix, const std::string& source)
  170. {
  171. #if !GOT_GFX
  172. return true; // just assume it's ok
  173. #endif
  174. #if !GOT_MORE_THAN_GLSL_120
  175. if (source.find("#version 140") != std::string::npos)
  176. return true;
  177. #endif
  178. const bool need3 =
  179. (source.find("#version 150") != std::string::npos) ||
  180. (source.find("#version 300") != std::string::npos);
  181. # ifdef __APPLE__
  182. // Mac core context does not accept any older shader versions, so need to switch to
  183. // either legacy context or core one.
  184. if (need3)
  185. {
  186. if (!s_GL3Active)
  187. CGLSetCurrentContext(s_GLContext3);
  188. s_GL3Active = true;
  189. }
  190. else
  191. {
  192. if (s_GL3Active)
  193. CGLSetCurrentContext(s_GLContext);
  194. s_GL3Active = false;
  195. }
  196. # endif // ifdef __APPLE__
  197. std::string src;
  198. if (gles)
  199. {
  200. src += "#define lowp\n";
  201. src += "#define mediump\n";
  202. src += "#define highp\n";
  203. src += "#define texture2DLodEXT texture2DLod\n";
  204. src += "#define texture2DProjLodEXT texture2DProjLod\n";
  205. src += "#define texture2DGradEXT texture2DGradARB\n";
  206. src += "#define textureCubeGradEXT textureCubeGradARB\n";
  207. src += "#define gl_FragDepthEXT gl_FragDepth\n";
  208. if (!need3)
  209. {
  210. src += "#define gl_LastFragData _glesLastFragData\n";
  211. src += "varying lowp vec4 _glesLastFragData[4];\n";
  212. }
  213. if (!need3)
  214. {
  215. src += "float shadow2DEXT (sampler2DShadow s, vec3 p) { return shadow2D(s,p).r; }\n";
  216. src += "float shadow2DProjEXT (sampler2DShadow s, vec4 p) { return shadow2DProj(s,p).r; }\n";
  217. }
  218. }
  219. src += source;
  220. if (gles)
  221. {
  222. replace_string (src, "GL_EXT_shader_texture_lod", "GL_ARB_shader_texture_lod", 0);
  223. replace_string (src, "GL_EXT_draw_instanced", "GL_ARB_draw_instanced", 0);
  224. replace_string (src, "gl_InstanceIDEXT", "gl_InstanceIDARB ", 0);
  225. replace_string (src, "#extension GL_OES_standard_derivatives : require", "", 0);
  226. replace_string (src, "#extension GL_EXT_shadow_samplers : require", "", 0);
  227. replace_string (src, "#extension GL_EXT_frag_depth : require", "", 0);
  228. replace_string (src, "#extension GL_OES_standard_derivatives : enable", "", 0);
  229. replace_string (src, "#extension GL_EXT_shadow_samplers : enable", "", 0);
  230. replace_string (src, "#extension GL_EXT_frag_depth : enable", "", 0);
  231. replace_string (src, "#extension GL_EXT_draw_buffers : enable", "", 0);
  232. replace_string (src, "#extension GL_EXT_draw_buffers : require", "", 0);
  233. replace_string (src, "precision ", "// precision ", 0);
  234. replace_string (src, "#version 300 es", "", 0);
  235. }
  236. // can't check FB fetch on PC
  237. if (src.find("#extension GL_EXT_shader_framebuffer_fetch") != std::string::npos)
  238. return true;
  239. if (gles && need3)
  240. {
  241. src = "#version 330\n" + src;
  242. }
  243. const char* sourcePtr = src.c_str();
  244. GLuint shader = glCreateShader (vertex ? GL_VERTEX_SHADER : GL_FRAGMENT_SHADER);
  245. glShaderSource (shader, 1, &sourcePtr, NULL);
  246. glCompileShader (shader);
  247. GLint status;
  248. glGetShaderiv (shader, GL_COMPILE_STATUS, &status);
  249. bool res = true;
  250. if (status != GL_TRUE)
  251. {
  252. char log[20000];
  253. log[0] = 0;
  254. GLsizei logLength;
  255. glGetShaderInfoLog (shader, sizeof(log), &logLength, log);
  256. printf ("\n %s: real glsl compiler error on %s:\n%s\n", testName.c_str(), prefix, log);
  257. res = false;
  258. }
  259. glDeleteShader (shader);
  260. return res;
  261. }
  262. static bool CheckMetal (bool vertex, bool gles, const std::string& testName, const char* prefix, const std::string& source)
  263. {
  264. #if !GOT_GFX || !defined(__APPLE__)
  265. return true; // just assume it's ok
  266. #else
  267. FILE* f = fopen ("metalTemp.metal", "wb");
  268. fwrite (source.c_str(), source.size(), 1, f);
  269. fclose (f);
  270. #if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
  271. int res = system("/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/usr/bin/metal metalTemp.metal -o metalTemp.o -std=ios-metal1.0 -Wno-parentheses-equality");
  272. if (res != 0)
  273. {
  274. printf ("\n %s: Metal compiler failed\n", testName.c_str());
  275. return false;
  276. }
  277. #endif //
  278. return true;
  279. #endif
  280. }
  281. static bool ReadStringFromFile (const char* pathName, std::string& output)
  282. {
  283. FILE* file = fopen( pathName, "rb" );
  284. if (file == NULL)
  285. return false;
  286. fseek(file, 0, SEEK_END);
  287. int length = ftell(file);
  288. fseek(file, 0, SEEK_SET);
  289. if (length < 0)
  290. {
  291. fclose( file );
  292. return false;
  293. }
  294. output.resize(length);
  295. int readLength = fread(&*output.begin(), 1, length, file);
  296. fclose(file);
  297. if (readLength != length)
  298. {
  299. output.clear();
  300. return false;
  301. }
  302. replace_string(output, "\r\n", "\n", 0);
  303. return true;
  304. }
  305. bool EndsWith (const std::string& str, const std::string& sub)
  306. {
  307. return (str.size() >= sub.size()) && (strncmp (str.c_str()+str.size()-sub.size(), sub.c_str(), sub.size())==0);
  308. }
  309. typedef std::vector<std::string> StringVector;
  310. static StringVector GetFiles (const std::string& folder, const std::string& endsWith)
  311. {
  312. StringVector res;
  313. #ifdef _MSC_VER
  314. WIN32_FIND_DATAA FindFileData;
  315. HANDLE hFind = FindFirstFileA ((folder+"/*"+endsWith).c_str(), &FindFileData);
  316. if (hFind == INVALID_HANDLE_VALUE)
  317. return res;
  318. do {
  319. res.push_back (FindFileData.cFileName);
  320. } while (FindNextFileA (hFind, &FindFileData));
  321. FindClose (hFind);
  322. #else
  323. DIR *dirp;
  324. struct dirent *dp;
  325. if ((dirp = opendir(folder.c_str())) == NULL)
  326. return res;
  327. while ( (dp = readdir(dirp)) )
  328. {
  329. std::string fname = dp->d_name;
  330. if (fname == "." || fname == "..")
  331. continue;
  332. if (!EndsWith (fname, endsWith))
  333. continue;
  334. res.push_back (fname);
  335. }
  336. closedir(dirp);
  337. #endif
  338. return res;
  339. }
  340. static void DeleteFile (const std::string& path)
  341. {
  342. #ifdef _MSC_VER
  343. DeleteFileA (path.c_str());
  344. #else
  345. unlink (path.c_str());
  346. #endif
  347. }
  348. static void MassageVertexForGLES (std::string& s)
  349. {
  350. if (s.find ("_glesVertex") != std::string::npos)
  351. return;
  352. std::string pre;
  353. std::string version = "#version 300 es\n";
  354. size_t insertPoint = s.find(version);
  355. if (insertPoint != std::string::npos)
  356. {
  357. insertPoint += version.size();
  358. pre += "#define gl_Vertex _glesVertex\nin highp vec4 _glesVertex;\n";
  359. pre += "#define gl_Normal _glesNormal\nin mediump vec3 _glesNormal;\n";
  360. pre += "#define gl_MultiTexCoord0 _glesMultiTexCoord0\nin highp vec4 _glesMultiTexCoord0;\n";
  361. pre += "#define gl_MultiTexCoord1 _glesMultiTexCoord1\nin highp vec4 _glesMultiTexCoord1;\n";
  362. pre += "#define gl_Color _glesColor\nin lowp vec4 _glesColor;\n";
  363. }
  364. else
  365. {
  366. insertPoint = 0;
  367. pre += "#define gl_Vertex _glesVertex\nattribute highp vec4 _glesVertex;\n";
  368. pre += "#define gl_Normal _glesNormal\nattribute mediump vec3 _glesNormal;\n";
  369. pre += "#define gl_MultiTexCoord0 _glesMultiTexCoord0\nattribute highp vec4 _glesMultiTexCoord0;\n";
  370. pre += "#define gl_MultiTexCoord1 _glesMultiTexCoord1\nattribute highp vec4 _glesMultiTexCoord1;\n";
  371. pre += "#define gl_Color _glesColor\nattribute lowp vec4 _glesColor;\n";
  372. }
  373. s.insert (insertPoint, pre);
  374. }
  375. static void MassageFragmentForGLES (std::string& s)
  376. {
  377. std::string pre;
  378. s = pre + s;
  379. }
  380. static const char* kGlslTypeNames[kGlslTypeCount] = {
  381. "float",
  382. "int",
  383. "bool",
  384. "2d",
  385. "3d",
  386. "cube",
  387. "2dshadow",
  388. "2darray",
  389. "other",
  390. };
  391. static const char* kGlslPrecNames[kGlslPrecCount] = {
  392. "high",
  393. "medium",
  394. "low",
  395. };
  396. static bool TestFile (glslopt_ctx* ctx, bool vertex,
  397. const std::string& testName,
  398. const std::string& inputPath,
  399. const std::string& outputPath,
  400. bool gles,
  401. bool doCheckGLSL,
  402. bool doCheckMetal)
  403. {
  404. std::string input;
  405. if (!ReadStringFromFile (inputPath.c_str(), input))
  406. {
  407. printf ("\n %s: failed to read input file\n", testName.c_str());
  408. return false;
  409. }
  410. if (doCheckGLSL)
  411. {
  412. if (!CheckGLSL (vertex, gles, testName, "input", input.c_str()))
  413. return false;
  414. }
  415. if (gles)
  416. {
  417. if (vertex)
  418. MassageVertexForGLES (input);
  419. else
  420. MassageFragmentForGLES (input);
  421. }
  422. bool res = true;
  423. glslopt_shader_type type = vertex ? kGlslOptShaderVertex : kGlslOptShaderFragment;
  424. glslopt_shader* shader = glslopt_optimize (ctx, type, input.c_str(), 0);
  425. bool optimizeOk = glslopt_get_status(shader);
  426. if (optimizeOk)
  427. {
  428. std::string textHir = glslopt_get_raw_output (shader);
  429. std::string textOpt = glslopt_get_output (shader);
  430. // append stats
  431. char buffer[1000];
  432. int statsAlu, statsTex, statsFlow;
  433. glslopt_shader_get_stats (shader, &statsAlu, &statsTex, &statsFlow);
  434. sprintf(buffer, "\n// stats: %i alu %i tex %i flow\n", statsAlu, statsTex, statsFlow);
  435. textOpt += buffer;
  436. // append inputs
  437. const int inputCount = glslopt_shader_get_input_count (shader);
  438. if (inputCount > 0)
  439. {
  440. sprintf(buffer, "// inputs: %i\n", inputCount);
  441. textOpt += buffer;
  442. }
  443. for (int i = 0; i < inputCount; ++i)
  444. {
  445. const char* parName;
  446. glslopt_basic_type parType;
  447. glslopt_precision parPrec;
  448. int parVecSize, parMatSize, parArrSize, location;
  449. glslopt_shader_get_input_desc(shader, i, &parName, &parType, &parPrec, &parVecSize, &parMatSize, &parArrSize, &location);
  450. if (location >= 0)
  451. sprintf(buffer, "// #%i: %s (%s %s) %ix%i [%i] loc %i\n", i, parName, kGlslPrecNames[parPrec], kGlslTypeNames[parType], parVecSize, parMatSize, parArrSize, location);
  452. else
  453. sprintf(buffer, "// #%i: %s (%s %s) %ix%i [%i]\n", i, parName, kGlslPrecNames[parPrec], kGlslTypeNames[parType], parVecSize, parMatSize, parArrSize);
  454. textOpt += buffer;
  455. }
  456. // append uniforms
  457. const int uniformCount = glslopt_shader_get_uniform_count (shader);
  458. const int uniformSize = glslopt_shader_get_uniform_total_size (shader);
  459. if (uniformCount > 0)
  460. {
  461. sprintf(buffer, "// uniforms: %i (total size: %i)\n", uniformCount, uniformSize);
  462. textOpt += buffer;
  463. }
  464. for (int i = 0; i < uniformCount; ++i)
  465. {
  466. const char* parName;
  467. glslopt_basic_type parType;
  468. glslopt_precision parPrec;
  469. int parVecSize, parMatSize, parArrSize, location;
  470. glslopt_shader_get_uniform_desc(shader, i, &parName, &parType, &parPrec, &parVecSize, &parMatSize, &parArrSize, &location);
  471. if (location >= 0)
  472. sprintf(buffer, "// #%i: %s (%s %s) %ix%i [%i] loc %i\n", i, parName, kGlslPrecNames[parPrec], kGlslTypeNames[parType], parVecSize, parMatSize, parArrSize, location);
  473. else
  474. sprintf(buffer, "// #%i: %s (%s %s) %ix%i [%i]\n", i, parName, kGlslPrecNames[parPrec], kGlslTypeNames[parType], parVecSize, parMatSize, parArrSize);
  475. textOpt += buffer;
  476. }
  477. // append textures
  478. const int textureCount = glslopt_shader_get_texture_count (shader);
  479. if (textureCount > 0)
  480. {
  481. sprintf(buffer, "// textures: %i\n", textureCount);
  482. textOpt += buffer;
  483. }
  484. for (int i = 0; i < textureCount; ++i)
  485. {
  486. const char* parName;
  487. glslopt_basic_type parType;
  488. glslopt_precision parPrec;
  489. int parVecSize, parMatSize, parArrSize, location;
  490. glslopt_shader_get_texture_desc(shader, i, &parName, &parType, &parPrec, &parVecSize, &parMatSize, &parArrSize, &location);
  491. if (location >= 0)
  492. sprintf(buffer, "// #%i: %s (%s %s) %ix%i [%i] loc %i\n", i, parName, kGlslPrecNames[parPrec], kGlslTypeNames[parType], parVecSize, parMatSize, parArrSize, location);
  493. else
  494. sprintf(buffer, "// #%i: %s (%s %s) %ix%i [%i]\n", i, parName, kGlslPrecNames[parPrec], kGlslTypeNames[parType], parVecSize, parMatSize, parArrSize);
  495. textOpt += buffer;
  496. }
  497. std::string outputOpt;
  498. ReadStringFromFile (outputPath.c_str(), outputOpt);
  499. if (res && doCheckMetal && !CheckMetal (vertex, gles, testName, "metal", textOpt.c_str()))
  500. res = false;
  501. if (textOpt != outputOpt)
  502. {
  503. // write output
  504. FILE* f = fopen (outputPath.c_str(), "wb");
  505. if (!f)
  506. {
  507. printf ("\n %s: can't write to optimized file!\n", testName.c_str());
  508. }
  509. else
  510. {
  511. fwrite (textOpt.c_str(), 1, textOpt.size(), f);
  512. fclose (f);
  513. }
  514. printf ("\n %s: does not match optimized output\n", testName.c_str());
  515. res = false;
  516. }
  517. if (res && doCheckGLSL && !CheckGLSL (vertex, gles, testName, "raw", textHir.c_str()))
  518. res = false;
  519. if (res && doCheckGLSL && !CheckGLSL (vertex, gles, testName, "optimized", textOpt.c_str()))
  520. res = false;
  521. }
  522. else
  523. {
  524. printf ("\n %s: optimize error: %s\n", testName.c_str(), glslopt_get_log(shader));
  525. res = false;
  526. }
  527. glslopt_shader_delete (shader);
  528. return res;
  529. }
  530. int main (int argc, const char** argv)
  531. {
  532. if (argc < 2)
  533. {
  534. printf ("USAGE: glsloptimizer testfolder\n");
  535. return 1;
  536. }
  537. bool hasOpenGL = InitializeOpenGL ();
  538. bool hasMetal = InitializeMetal ();
  539. glslopt_ctx* ctx[3] = {
  540. glslopt_initialize(kGlslTargetOpenGLES20),
  541. glslopt_initialize(kGlslTargetOpenGLES30),
  542. glslopt_initialize(kGlslTargetOpenGL),
  543. };
  544. glslopt_ctx* ctxMetal = glslopt_initialize(kGlslTargetMetal);
  545. std::string baseFolder = argv[1];
  546. clock_t time0 = clock();
  547. // 2.39s
  548. // ralloc fix 256 initial: 1.35s
  549. static const char* kTypeName[2] = { "vertex", "fragment" };
  550. size_t tests = 0;
  551. size_t errors = 0;
  552. for (int type = 0; type < 2; ++type)
  553. {
  554. std::string testFolder = baseFolder + "/" + kTypeName[type];
  555. static const char* kAPIName[3] = { "OpenGL ES 2.0", "OpenGL ES 3.0", "OpenGL" };
  556. static const char* kApiIn [3] = {"-inES.txt", "-inES3.txt", "-in.txt"};
  557. static const char* kApiOut[3] = {"-outES.txt", "-outES3.txt", "-out.txt"};
  558. static const char* kApiOutMetal[3] = {"-outESMetal.txt", "-outES3Metal.txt", "-outMetal.txt"};
  559. for (int api = 0; api < 3; ++api)
  560. {
  561. printf ("\n** running %s tests for %s...\n", kTypeName[type], kAPIName[api]);
  562. StringVector inputFiles = GetFiles (testFolder, kApiIn[api]);
  563. size_t n = inputFiles.size();
  564. for (size_t i = 0; i < n; ++i)
  565. {
  566. std::string inname = inputFiles[i];
  567. //if (inname != "ast-in.txt")
  568. // continue;
  569. std::string outname = inname.substr (0,inname.size()-strlen(kApiIn[api])) + kApiOut[api];
  570. std::string outnameMetal = inname.substr (0,inname.size()-strlen(kApiIn[api])) + kApiOutMetal[api];
  571. const bool useMetal = (api == 1);
  572. bool ok = TestFile (ctx[api], type==0, inname, testFolder + "/" + inname, testFolder + "/" + outname, api<=1, hasOpenGL, false);
  573. if (!ok)
  574. {
  575. ++errors;
  576. }
  577. if (useMetal)
  578. {
  579. ok = TestFile (ctxMetal, type==0, inname, testFolder + "/" + inname, testFolder + "/" + outnameMetal, api==0, false, hasMetal);
  580. if (!ok)
  581. {
  582. ++errors;
  583. }
  584. }
  585. ++tests;
  586. }
  587. }
  588. }
  589. clock_t time1 = clock();
  590. float timeDelta = float(time1-time0)/CLOCKS_PER_SEC;
  591. if (errors != 0)
  592. printf ("\n**** %i tests (%.2fsec), %i !!!FAILED!!!\n", (int)tests, timeDelta, (int)errors);
  593. else
  594. printf ("\n**** %i tests (%.2fsec) succeeded\n", (int)tests, timeDelta);
  595. // 3.25s
  596. // with builtin call linking, 3.84s
  597. for (int i = 0; i < 2; ++i)
  598. glslopt_cleanup (ctx[i]);
  599. glslopt_cleanup (ctxMetal);
  600. CleanupGL();
  601. return errors ? 1 : 0;
  602. }