Shader.cpp 20 KB

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