Shader.cpp 20 KB

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