Shader.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /**
  2. * Copyright (c) 2006-2015 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "common/config.h"
  22. #include "Shader.h"
  23. #include "Canvas.h"
  24. // C++
  25. #include <algorithm>
  26. namespace love
  27. {
  28. namespace graphics
  29. {
  30. namespace opengl
  31. {
  32. namespace
  33. {
  34. // temporarily attaches a shader program (for setting uniforms, etc)
  35. // reattaches the originally active program when destroyed
  36. struct TemporaryAttacher
  37. {
  38. TemporaryAttacher(Shader *shader)
  39. : curShader(shader)
  40. , prevShader(Shader::current)
  41. {
  42. curShader->attach(true);
  43. }
  44. ~TemporaryAttacher()
  45. {
  46. if (prevShader != nullptr)
  47. prevShader->attach();
  48. else
  49. curShader->detach();
  50. }
  51. Shader *curShader;
  52. Shader *prevShader;
  53. };
  54. } // anonymous namespace
  55. Shader *Shader::current = nullptr;
  56. Shader *Shader::defaultShader = nullptr;
  57. Shader::ShaderSource Shader::defaultCode[Graphics::RENDERER_MAX_ENUM];
  58. GLint Shader::maxTexUnits = 0;
  59. std::vector<int> Shader::textureCounters;
  60. Shader::Shader(const ShaderSource &source)
  61. : shaderSource(source)
  62. , program(0)
  63. , builtinUniforms()
  64. , builtinAttributes()
  65. , lastCanvas((Canvas *) -1)
  66. , lastViewport()
  67. {
  68. if (source.vertex.empty() && source.pixel.empty())
  69. throw love::Exception("Cannot create shader: no source code!");
  70. if (maxTexUnits <= 0)
  71. {
  72. GLint maxtexunits;
  73. glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &maxtexunits);
  74. maxTexUnits = std::max(maxtexunits - 1, 0);
  75. }
  76. // initialize global texture id counters if needed
  77. if (textureCounters.size() < (size_t) maxTexUnits)
  78. textureCounters.resize(maxTexUnits, 0);
  79. // load shader source and create program object
  80. loadVolatile();
  81. }
  82. Shader::~Shader()
  83. {
  84. if (current == this)
  85. detach();
  86. for (const auto &retainable : boundRetainables)
  87. retainable.second->release();
  88. boundRetainables.clear();
  89. unloadVolatile();
  90. }
  91. GLuint Shader::compileCode(ShaderStage stage, const std::string &code)
  92. {
  93. GLenum glstage;
  94. const char *typestr;
  95. if (!stageNames.find(stage, typestr))
  96. typestr = "";
  97. switch (stage)
  98. {
  99. case STAGE_VERTEX:
  100. glstage = GL_VERTEX_SHADER;
  101. break;
  102. case STAGE_PIXEL:
  103. glstage = GL_FRAGMENT_SHADER;
  104. break;
  105. default:
  106. throw love::Exception("Cannot create shader object: unknown shader type.");
  107. break;
  108. }
  109. GLuint shaderid = glCreateShader(glstage);
  110. if (shaderid == 0)
  111. {
  112. if (glGetError() == GL_INVALID_ENUM)
  113. throw love::Exception("Cannot create %s shader object: %s shaders not supported.", typestr, typestr);
  114. else
  115. throw love::Exception("Cannot create %s shader object.", typestr);
  116. }
  117. const char *src = code.c_str();
  118. GLint srclen = (GLint) code.length();
  119. glShaderSource(shaderid, 1, (const GLchar **)&src, &srclen);
  120. glCompileShader(shaderid);
  121. GLint infologlen;
  122. glGetShaderiv(shaderid, GL_INFO_LOG_LENGTH, &infologlen);
  123. // Get any warnings the shader compiler may have produced.
  124. if (infologlen > 0)
  125. {
  126. GLchar *infolog = new GLchar[infologlen];
  127. glGetShaderInfoLog(shaderid, infologlen, nullptr, infolog);
  128. // Save any warnings for later querying.
  129. shaderWarnings[stage] = infolog;
  130. delete[] infolog;
  131. }
  132. GLint status;
  133. glGetShaderiv(shaderid, GL_COMPILE_STATUS, &status);
  134. if (status == GL_FALSE)
  135. {
  136. glDeleteShader(shaderid);
  137. throw love::Exception("Cannot compile %s shader code:\n%s",
  138. typestr, shaderWarnings[stage].c_str());
  139. }
  140. return shaderid;
  141. }
  142. void Shader::mapActiveUniforms()
  143. {
  144. // Built-in uniform locations default to -1 (nonexistant.)
  145. for (int i = 0; i < int(BUILTIN_MAX_ENUM); i++)
  146. builtinUniforms[i] = -1;
  147. uniforms.clear();
  148. GLint numuniforms;
  149. glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &numuniforms);
  150. GLchar cname[256];
  151. const GLint bufsize = (GLint) (sizeof(cname) / sizeof(GLchar));
  152. for (int i = 0; i < numuniforms; i++)
  153. {
  154. GLsizei namelen = 0;
  155. Uniform u = {};
  156. glGetActiveUniform(program, (GLuint) i, bufsize, &namelen, &u.count, &u.type, cname);
  157. u.name = std::string(cname, (size_t) namelen);
  158. u.location = glGetUniformLocation(program, u.name.c_str());
  159. u.baseType = getUniformBaseType(u.type);
  160. // glGetActiveUniform appends "[0]" to the end of array uniform names...
  161. if (u.name.length() > 3)
  162. {
  163. size_t findpos = u.name.find("[0]");
  164. if (findpos != std::string::npos && findpos == u.name.length() - 3)
  165. u.name.erase(u.name.length() - 3);
  166. }
  167. // If this is a built-in (LOVE-created) uniform, store the location.
  168. BuiltinUniform builtin;
  169. if (builtinNames.find(u.name.c_str(), builtin))
  170. builtinUniforms[int(builtin)] = u.location;
  171. if (u.location != -1)
  172. uniforms[u.name] = u;
  173. }
  174. }
  175. bool Shader::loadVolatile()
  176. {
  177. // zero out active texture list
  178. activeTexUnits.clear();
  179. activeTexUnits.insert(activeTexUnits.begin(), maxTexUnits, 0);
  180. std::vector<GLuint> shaderids;
  181. const ShaderSource *defaults = &defaultCode[Graphics::RENDERER_OPENGL];
  182. if (GLAD_ES_VERSION_2_0)
  183. defaults = &defaultCode[Graphics::RENDERER_OPENGLES];
  184. // The shader program must have both vertex and pixel shader stages.
  185. const std::string &vertexcode = shaderSource.vertex.empty() ? defaults->vertex : shaderSource.vertex;
  186. const std::string &pixelcode = shaderSource.pixel.empty() ? defaults->pixel : shaderSource.pixel;
  187. try
  188. {
  189. shaderids.push_back(compileCode(STAGE_VERTEX, vertexcode));
  190. shaderids.push_back(compileCode(STAGE_PIXEL, pixelcode));
  191. }
  192. catch (love::Exception &)
  193. {
  194. for (GLuint id : shaderids)
  195. glDeleteShader(id);
  196. throw;
  197. }
  198. program = glCreateProgram();
  199. if (program == 0)
  200. {
  201. for (GLuint id : shaderids)
  202. glDeleteShader(id);
  203. throw love::Exception("Cannot create shader program object.");
  204. }
  205. for (GLuint id : shaderids)
  206. glAttachShader(program, id);
  207. // Bind generic vertex attribute indices to names in the shader.
  208. for (int i = 0; i < int(ATTRIB_MAX_ENUM); i++)
  209. {
  210. VertexAttribID attrib = (VertexAttribID) i;
  211. // FIXME: We skip this both because pseudo-instancing is temporarily
  212. // disabled (see graphics.lua), and because binding a non-existant
  213. // attribute name to a location causes a shader linker warning.
  214. if (attrib == ATTRIB_PSEUDO_INSTANCE_ID)
  215. continue;
  216. const char *name = nullptr;
  217. if (attribNames.find(attrib, name))
  218. glBindAttribLocation(program, i, (const GLchar *) name);
  219. }
  220. glLinkProgram(program);
  221. // Flag shaders for auto-deletion when the program object is deleted.
  222. for (GLuint id : shaderids)
  223. glDeleteShader(id);
  224. GLint status;
  225. glGetProgramiv(program, GL_LINK_STATUS, &status);
  226. if (status == GL_FALSE)
  227. {
  228. std::string warnings = getProgramWarnings();
  229. glDeleteProgram(program);
  230. program = 0;
  231. throw love::Exception("Cannot link shader program object:\n%s", warnings.c_str());
  232. }
  233. // Retreive all active uniform variables in this shader from OpenGL.
  234. mapActiveUniforms();
  235. for (int i = 0; i < int(ATTRIB_MAX_ENUM); i++)
  236. {
  237. const char *name = nullptr;
  238. if (attribNames.find(VertexAttribID(i), name))
  239. builtinAttributes[i] = glGetAttribLocation(program, name);
  240. else
  241. builtinAttributes[i] = -1;
  242. }
  243. if (current == this)
  244. {
  245. // make sure glUseProgram gets called.
  246. current = nullptr;
  247. attach();
  248. }
  249. return true;
  250. }
  251. void Shader::unloadVolatile()
  252. {
  253. if (current == this)
  254. glUseProgram(0);
  255. if (program != 0)
  256. {
  257. glDeleteProgram(program);
  258. program = 0;
  259. }
  260. // decrement global texture id counters for texture units which had textures bound from this shader
  261. for (size_t i = 0; i < activeTexUnits.size(); ++i)
  262. {
  263. if (activeTexUnits[i] > 0)
  264. textureCounters[i] = std::max(textureCounters[i] - 1, 0);
  265. }
  266. // active texture list is probably invalid, clear it
  267. activeTexUnits.clear();
  268. activeTexUnits.resize(maxTexUnits, 0);
  269. // same with uniform location list
  270. uniforms.clear();
  271. // And the locations of any built-in uniform variables.
  272. for (int i = 0; i < int(BUILTIN_MAX_ENUM); i++)
  273. builtinUniforms[i] = -1;
  274. shaderWarnings.clear();
  275. }
  276. std::string Shader::getProgramWarnings() const
  277. {
  278. GLint strsize, nullpos;
  279. glGetProgramiv(program, GL_INFO_LOG_LENGTH, &strsize);
  280. if (strsize == 0)
  281. return "";
  282. char *tempstr = new char[strsize];
  283. // be extra sure that the error string will be 0-terminated
  284. memset(tempstr, '\0', strsize);
  285. glGetProgramInfoLog(program, strsize, &nullpos, tempstr);
  286. tempstr[nullpos] = '\0';
  287. std::string warnings(tempstr);
  288. delete[] tempstr;
  289. return warnings;
  290. }
  291. std::string Shader::getWarnings() const
  292. {
  293. std::string warnings;
  294. const char *stagestr;
  295. // Get the individual shader stage warnings
  296. for (const auto &warning : shaderWarnings)
  297. {
  298. if (stageNames.find(warning.first, stagestr))
  299. warnings += std::string(stagestr) + std::string(" shader:\n") + warning.second;
  300. }
  301. warnings += getProgramWarnings();
  302. return warnings;
  303. }
  304. void Shader::attach(bool temporary)
  305. {
  306. if (current != this)
  307. {
  308. glUseProgram(program);
  309. current = this;
  310. // retain/release happens in Graphics::setShader.
  311. }
  312. if (!temporary)
  313. {
  314. // make sure all sent textures are properly bound to their respective texture units
  315. // note: list potentially contains texture ids of deleted/invalid textures!
  316. for (size_t i = 0; i < activeTexUnits.size(); ++i)
  317. {
  318. if (activeTexUnits[i] > 0)
  319. gl.bindTextureToUnit(activeTexUnits[i], i + 1, false);
  320. }
  321. // We always want to use texture unit 0 for everyhing else.
  322. gl.setTextureUnit(0);
  323. }
  324. }
  325. void Shader::detach()
  326. {
  327. if (defaultShader)
  328. {
  329. if (current != defaultShader)
  330. defaultShader->attach();
  331. return;
  332. }
  333. if (current != nullptr)
  334. glUseProgram(0);
  335. current = nullptr;
  336. }
  337. const Shader::Uniform &Shader::getUniform(const std::string &name) const
  338. {
  339. std::map<std::string, Uniform>::const_iterator it = uniforms.find(name);
  340. if (it == uniforms.end())
  341. throw love::Exception("Variable '%s' does not exist.\n"
  342. "A common error is to define but not use the variable.", name.c_str());
  343. return it->second;
  344. }
  345. void Shader::checkSetUniformError(const Uniform &u, int size, int count, UniformType sendtype) const
  346. {
  347. if (!program)
  348. throw love::Exception("No active shader program.");
  349. int realsize = getUniformTypeSize(u.type);
  350. if (size != realsize)
  351. throw love::Exception("Value size of %d does not match variable size of %d.", size, realsize);
  352. if ((u.count == 1 && count > 1) || count < 0)
  353. throw love::Exception("Invalid number of values (expected %d, got %d).", u.count, count);
  354. if (u.baseType == UNIFORM_SAMPLER && sendtype != u.baseType)
  355. throw love::Exception("Cannot send a value of this type to an Image variable.");
  356. if ((sendtype == UNIFORM_FLOAT && u.baseType == UNIFORM_INT) || (sendtype == UNIFORM_INT && u.baseType == UNIFORM_FLOAT))
  357. throw love::Exception("Cannot convert between float and int.");
  358. }
  359. void Shader::sendInt(const std::string &name, int size, const GLint *vec, int count)
  360. {
  361. TemporaryAttacher attacher(this);
  362. const Uniform &u = getUniform(name);
  363. checkSetUniformError(u, size, count, UNIFORM_INT);
  364. switch (size)
  365. {
  366. case 4:
  367. glUniform4iv(u.location, count, vec);
  368. break;
  369. case 3:
  370. glUniform3iv(u.location, count, vec);
  371. break;
  372. case 2:
  373. glUniform2iv(u.location, count, vec);
  374. break;
  375. case 1:
  376. default:
  377. glUniform1iv(u.location, count, vec);
  378. break;
  379. }
  380. }
  381. void Shader::sendFloat(const std::string &name, int size, const GLfloat *vec, int count)
  382. {
  383. TemporaryAttacher attacher(this);
  384. const Uniform &u = getUniform(name);
  385. checkSetUniformError(u, size, count, UNIFORM_FLOAT);
  386. switch (size)
  387. {
  388. case 4:
  389. glUniform4fv(u.location, count, vec);
  390. break;
  391. case 3:
  392. glUniform3fv(u.location, count, vec);
  393. break;
  394. case 2:
  395. glUniform2fv(u.location, count, vec);
  396. break;
  397. case 1:
  398. default:
  399. glUniform1fv(u.location, count, vec);
  400. break;
  401. }
  402. }
  403. void Shader::sendMatrix(const std::string &name, int size, const GLfloat *m, int count)
  404. {
  405. TemporaryAttacher attacher(this);
  406. if (size < 2 || size > 4)
  407. {
  408. throw love::Exception("Invalid matrix size: %dx%d "
  409. "(can only set 2x2, 3x3 or 4x4 matrices.)", size,size);
  410. }
  411. const Uniform &u = getUniform(name);
  412. checkSetUniformError(u, size, count, UNIFORM_FLOAT);
  413. switch (size)
  414. {
  415. case 4:
  416. glUniformMatrix4fv(u.location, count, GL_FALSE, m);
  417. break;
  418. case 3:
  419. glUniformMatrix3fv(u.location, count, GL_FALSE, m);
  420. break;
  421. case 2:
  422. default:
  423. glUniformMatrix2fv(u.location, count, GL_FALSE, m);
  424. break;
  425. }
  426. }
  427. void Shader::sendTexture(const std::string &name, Texture *texture)
  428. {
  429. GLuint gltex = texture->getGLTexture();
  430. TemporaryAttacher attacher(this);
  431. int texunit = getTextureUnit(name);
  432. const Uniform &u = getUniform(name);
  433. checkSetUniformError(u, 1, 1, UNIFORM_SAMPLER);
  434. // bind texture to assigned texture unit and send uniform to shader program
  435. gl.bindTextureToUnit(gltex, texunit, false);
  436. glUniform1i(u.location, texunit);
  437. // reset texture unit
  438. gl.setTextureUnit(0);
  439. // increment global shader texture id counter for this texture unit, if we haven't already
  440. if (activeTexUnits[texunit-1] == 0)
  441. ++textureCounters[texunit-1];
  442. // store texture id so it can be re-bound to the proper texture unit later
  443. activeTexUnits[texunit-1] = gltex;
  444. retainObject(name, texture);
  445. }
  446. void Shader::retainObject(const std::string &name, Object *object)
  447. {
  448. object->retain();
  449. auto it = boundRetainables.find(name);
  450. if (it != boundRetainables.end())
  451. it->second->release();
  452. boundRetainables[name] = object;
  453. }
  454. int Shader::getTextureUnit(const std::string &name)
  455. {
  456. auto it = texUnitPool.find(name);
  457. if (it != texUnitPool.end())
  458. return it->second;
  459. int texunit = 1;
  460. // prefer texture units which are unused by all other shaders
  461. auto freeunit_it = std::find(textureCounters.begin(), textureCounters.end(), 0);
  462. if (freeunit_it != textureCounters.end())
  463. {
  464. // we don't want to use unit 0
  465. texunit = std::distance(textureCounters.begin(), freeunit_it) + 1;
  466. }
  467. else
  468. {
  469. // no completely unused texture units exist, try to use next free slot in our own list
  470. auto nextunit_it = std::find(activeTexUnits.begin(), activeTexUnits.end(), 0);
  471. if (nextunit_it == activeTexUnits.end())
  472. throw love::Exception("No more texture units available for shader.");
  473. // we don't want to use unit 0
  474. texunit = std::distance(activeTexUnits.begin(), nextunit_it) + 1;
  475. }
  476. texUnitPool[name] = texunit;
  477. return texunit;
  478. }
  479. Shader::UniformType Shader::getExternVariable(const std::string &name, int &components, int &count)
  480. {
  481. auto it = uniforms.find(name);
  482. if (it == uniforms.end())
  483. {
  484. components = 0;
  485. count = 0;
  486. return UNIFORM_UNKNOWN;
  487. }
  488. components = getUniformTypeSize(it->second.type);
  489. count = (int) it->second.count;
  490. return it->second.baseType;
  491. }
  492. bool Shader::hasVertexAttrib(VertexAttribID attrib) const
  493. {
  494. return builtinAttributes[int(attrib)] != -1;
  495. }
  496. bool Shader::hasBuiltinUniform(BuiltinUniform builtin) const
  497. {
  498. return builtinUniforms[int(builtin)] != -1;
  499. }
  500. bool Shader::sendBuiltinMatrix(BuiltinUniform builtin, int size, const GLfloat *m, int count)
  501. {
  502. if (!hasBuiltinUniform(builtin))
  503. return false;
  504. GLint location = builtinUniforms[GLint(builtin)];
  505. TemporaryAttacher attacher(this);
  506. switch (size)
  507. {
  508. case 2:
  509. glUniformMatrix2fv(location, count, GL_FALSE, m);
  510. break;
  511. case 3:
  512. glUniformMatrix3fv(location, count, GL_FALSE, m);
  513. break;
  514. case 4:
  515. glUniformMatrix4fv(location, count, GL_FALSE, m);
  516. break;
  517. default:
  518. return false;
  519. }
  520. return true;
  521. }
  522. bool Shader::sendBuiltinFloat(BuiltinUniform builtin, int size, const GLfloat *vec, int count)
  523. {
  524. if (!hasBuiltinUniform(builtin))
  525. return false;
  526. GLint location = builtinUniforms[int(builtin)];
  527. TemporaryAttacher attacher(this);
  528. switch (size)
  529. {
  530. case 1:
  531. glUniform1fv(location, count, vec);
  532. break;
  533. case 2:
  534. glUniform2fv(location, count, vec);
  535. break;
  536. case 3:
  537. glUniform3fv(location, count, vec);
  538. break;
  539. case 4:
  540. glUniform4fv(location, count, vec);
  541. break;
  542. default:
  543. return false;
  544. }
  545. return true;
  546. }
  547. void Shader::checkSetScreenParams()
  548. {
  549. OpenGL::Viewport view = gl.getViewport();
  550. if (view == lastViewport && lastCanvas == Canvas::current)
  551. return;
  552. // In the shader, we do pixcoord.y = gl_FragCoord.y * params.z + params.w.
  553. // This lets us flip pixcoord.y when needed, to be consistent (drawing with
  554. // no Canvas active makes the y-values for pixel coordinates flipped.)
  555. GLfloat params[] = {
  556. (GLfloat) view.w, (GLfloat) view.h,
  557. 0.0f, 0.0f,
  558. };
  559. if (Canvas::current != nullptr)
  560. {
  561. // No flipping: pixcoord.y = gl_FragCoord.y * 1.0 + 0.0.
  562. params[2] = 1.0f;
  563. params[3] = 0.0f;
  564. }
  565. else
  566. {
  567. // gl_FragCoord.y is flipped when drawing to the screen, so we un-flip:
  568. // pixcoord.y = gl_FragCoord.y * -1.0 + height.
  569. params[2] = -1.0f;
  570. params[3] = (GLfloat) view.h;
  571. }
  572. sendBuiltinFloat(BUILTIN_SCREEN_SIZE, 4, params, 1);
  573. lastCanvas = Canvas::current;
  574. lastViewport = view;
  575. }
  576. const std::map<std::string, Object *> &Shader::getBoundRetainables() const
  577. {
  578. return boundRetainables;
  579. }
  580. std::string Shader::getGLSLVersion()
  581. {
  582. const char *tmp = (const char *) glGetString(GL_SHADING_LANGUAGE_VERSION);
  583. if (tmp == nullptr)
  584. return "0.0";
  585. // the version string always begins with a version number of the format
  586. // major_number.minor_number
  587. // or
  588. // major_number.minor_number.release_number
  589. // we can keep release_number, since it does not affect the check below.
  590. std::string versionstring(tmp);
  591. size_t minorendpos = versionstring.find(' ');
  592. return versionstring.substr(0, minorendpos);
  593. }
  594. bool Shader::isSupported()
  595. {
  596. return GLAD_ES_VERSION_2_0 || (getGLSLVersion() >= "1.2");
  597. }
  598. int Shader::getUniformTypeSize(GLenum type) const
  599. {
  600. switch (type)
  601. {
  602. case GL_INT:
  603. case GL_FLOAT:
  604. case GL_BOOL:
  605. case GL_SAMPLER_1D:
  606. case GL_SAMPLER_2D:
  607. case GL_SAMPLER_3D:
  608. return 1;
  609. case GL_INT_VEC2:
  610. case GL_FLOAT_VEC2:
  611. case GL_FLOAT_MAT2:
  612. case GL_BOOL_VEC2:
  613. return 2;
  614. case GL_INT_VEC3:
  615. case GL_FLOAT_VEC3:
  616. case GL_FLOAT_MAT3:
  617. case GL_BOOL_VEC3:
  618. return 3;
  619. case GL_INT_VEC4:
  620. case GL_FLOAT_VEC4:
  621. case GL_FLOAT_MAT4:
  622. case GL_BOOL_VEC4:
  623. return 4;
  624. default:
  625. return 1;
  626. }
  627. }
  628. Shader::UniformType Shader::getUniformBaseType(GLenum type) const
  629. {
  630. switch (type)
  631. {
  632. case GL_INT:
  633. case GL_INT_VEC2:
  634. case GL_INT_VEC3:
  635. case GL_INT_VEC4:
  636. return UNIFORM_INT;
  637. case GL_FLOAT:
  638. case GL_FLOAT_VEC2:
  639. case GL_FLOAT_VEC3:
  640. case GL_FLOAT_VEC4:
  641. case GL_FLOAT_MAT2:
  642. case GL_FLOAT_MAT3:
  643. case GL_FLOAT_MAT4:
  644. return UNIFORM_FLOAT;
  645. case GL_BOOL:
  646. case GL_BOOL_VEC2:
  647. case GL_BOOL_VEC3:
  648. case GL_BOOL_VEC4:
  649. return UNIFORM_BOOL;
  650. case GL_SAMPLER_1D:
  651. case GL_SAMPLER_2D:
  652. case GL_SAMPLER_3D:
  653. return UNIFORM_SAMPLER;
  654. default:
  655. return UNIFORM_UNKNOWN;
  656. }
  657. }
  658. bool Shader::getConstant(const char *in, UniformType &out)
  659. {
  660. return uniformTypes.find(in, out);
  661. }
  662. bool Shader::getConstant(UniformType in, const char *&out)
  663. {
  664. return uniformTypes.find(in, out);
  665. }
  666. StringMap<Shader::ShaderStage, Shader::STAGE_MAX_ENUM>::Entry Shader::stageNameEntries[] =
  667. {
  668. {"vertex", Shader::STAGE_VERTEX},
  669. {"pixel", Shader::STAGE_PIXEL},
  670. };
  671. StringMap<Shader::ShaderStage, Shader::STAGE_MAX_ENUM> Shader::stageNames(Shader::stageNameEntries, sizeof(Shader::stageNameEntries));
  672. StringMap<Shader::UniformType, Shader::UNIFORM_MAX_ENUM>::Entry Shader::uniformTypeEntries[] =
  673. {
  674. {"float", Shader::UNIFORM_FLOAT},
  675. {"int", Shader::UNIFORM_INT},
  676. {"bool", Shader::UNIFORM_BOOL},
  677. {"image", Shader::UNIFORM_SAMPLER},
  678. {"unknown", Shader::UNIFORM_UNKNOWN},
  679. };
  680. StringMap<Shader::UniformType, Shader::UNIFORM_MAX_ENUM> Shader::uniformTypes(Shader::uniformTypeEntries, sizeof(Shader::uniformTypeEntries));
  681. StringMap<VertexAttribID, ATTRIB_MAX_ENUM>::Entry Shader::attribNameEntries[] =
  682. {
  683. {"VertexPosition", ATTRIB_POS},
  684. {"VertexTexCoord", ATTRIB_TEXCOORD},
  685. {"VertexColor", ATTRIB_COLOR},
  686. {"love_PseudoInstanceID", ATTRIB_PSEUDO_INSTANCE_ID},
  687. };
  688. StringMap<VertexAttribID, ATTRIB_MAX_ENUM> Shader::attribNames(Shader::attribNameEntries, sizeof(Shader::attribNameEntries));
  689. StringMap<Shader::BuiltinUniform, Shader::BUILTIN_MAX_ENUM>::Entry Shader::builtinNameEntries[] =
  690. {
  691. {"TransformMatrix", Shader::BUILTIN_TRANSFORM_MATRIX},
  692. {"ProjectionMatrix", Shader::BUILTIN_PROJECTION_MATRIX},
  693. {"TransformProjectionMatrix", Shader::BUILTIN_TRANSFORM_PROJECTION_MATRIX},
  694. {"love_PointSize", Shader::BUILTIN_POINT_SIZE},
  695. {"love_ScreenSize", Shader::BUILTIN_SCREEN_SIZE},
  696. };
  697. StringMap<Shader::BuiltinUniform, Shader::BUILTIN_MAX_ENUM> Shader::builtinNames(Shader::builtinNameEntries, sizeof(Shader::builtinNameEntries));
  698. } // opengl
  699. } // graphics
  700. } // love