shader_gles2.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*************************************************************************/
  2. /* shader_gles2.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef SHADER_GLES2_H
  31. #define SHADER_GLES2_H
  32. // This must come first to avoid windows.h mess
  33. #include "platform_config.h"
  34. #ifndef GLES2_INCLUDE_H
  35. #include <GLES2/gl2.h>
  36. #else
  37. #include GLES2_INCLUDE_H
  38. #endif
  39. #include "core/hash_map.h"
  40. #include "core/map.h"
  41. #include "core/math/camera_matrix.h"
  42. #include "core/pair.h"
  43. #include "core/variant.h"
  44. #include "servers/visual/shader_language.h"
  45. #include <stdio.h>
  46. class RasterizerStorageGLES2;
  47. class ShaderGLES2 {
  48. protected:
  49. struct Enum {
  50. uint64_t mask;
  51. uint64_t shift;
  52. const char *defines[16];
  53. };
  54. struct EnumValue {
  55. uint64_t set_mask;
  56. uint64_t clear_mask;
  57. };
  58. struct AttributePair {
  59. const char *name;
  60. int index;
  61. };
  62. struct UniformPair {
  63. const char *name;
  64. Variant::Type type_hint;
  65. };
  66. struct TexUnitPair {
  67. const char *name;
  68. int index;
  69. };
  70. bool uniforms_dirty;
  71. private:
  72. //@TODO Optimize to a fixed set of shader pools and use a LRU
  73. int uniform_count;
  74. int texunit_pair_count;
  75. int conditional_count;
  76. int vertex_code_start;
  77. int fragment_code_start;
  78. int attribute_pair_count;
  79. struct CustomCode {
  80. String vertex;
  81. String vertex_globals;
  82. String fragment;
  83. String fragment_globals;
  84. String light;
  85. uint32_t version;
  86. Vector<StringName> texture_uniforms;
  87. Vector<StringName> custom_uniforms;
  88. Vector<CharString> custom_defines;
  89. };
  90. struct Version {
  91. GLuint id;
  92. GLuint vert_id;
  93. GLuint frag_id;
  94. Vector<StringName> uniform_names;
  95. GLint *uniform_location;
  96. Vector<GLint> texture_uniform_locations;
  97. Map<StringName, GLint> custom_uniform_locations;
  98. uint32_t code_version;
  99. bool ok;
  100. Version() {
  101. code_version = 0;
  102. ok = false;
  103. uniform_location = NULL;
  104. }
  105. };
  106. Version *version;
  107. union VersionKey {
  108. struct {
  109. uint32_t version;
  110. uint32_t code_version;
  111. };
  112. uint64_t key;
  113. bool operator==(const VersionKey &p_key) const { return key == p_key.key; }
  114. bool operator<(const VersionKey &p_key) const { return key < p_key.key; }
  115. };
  116. struct VersionKeyHash {
  117. static _FORCE_INLINE_ uint32_t hash(const VersionKey &p_key) { return HashMapHasherDefault::hash(p_key.key); }
  118. };
  119. //this should use a way more cachefriendly version..
  120. HashMap<VersionKey, Version, VersionKeyHash> version_map;
  121. HashMap<uint32_t, CustomCode> custom_code_map;
  122. uint32_t last_custom_code;
  123. VersionKey conditional_version;
  124. VersionKey new_conditional_version;
  125. virtual String get_shader_name() const = 0;
  126. const char **conditional_defines;
  127. const char **uniform_names;
  128. const AttributePair *attribute_pairs;
  129. const TexUnitPair *texunit_pairs;
  130. const char *vertex_code;
  131. const char *fragment_code;
  132. CharString fragment_code0;
  133. CharString fragment_code1;
  134. CharString fragment_code2;
  135. CharString fragment_code3;
  136. CharString vertex_code0;
  137. CharString vertex_code1;
  138. CharString vertex_code2;
  139. Vector<CharString> custom_defines;
  140. Version *get_current_version();
  141. static ShaderGLES2 *active;
  142. int max_image_units;
  143. Map<uint32_t, Variant> uniform_defaults;
  144. Map<uint32_t, CameraMatrix> uniform_cameras;
  145. Map<StringName, Pair<ShaderLanguage::DataType, Vector<ShaderLanguage::ConstantNode::Value> > > uniform_values;
  146. protected:
  147. _FORCE_INLINE_ int _get_uniform(int p_which) const;
  148. _FORCE_INLINE_ void _set_conditional(int p_which, bool p_value);
  149. void setup(const char **p_conditional_defines,
  150. int p_conditional_count,
  151. const char **p_uniform_names,
  152. int p_uniform_count,
  153. const AttributePair *p_attribute_pairs,
  154. int p_attribute_count,
  155. const TexUnitPair *p_texunit_pairs,
  156. int p_texunit_pair_count,
  157. const char *p_vertex_code,
  158. const char *p_fragment_code,
  159. int p_vertex_code_start,
  160. int p_fragment_code_start);
  161. ShaderGLES2();
  162. public:
  163. enum {
  164. CUSTOM_SHADER_DISABLED = 0
  165. };
  166. GLint get_uniform_location(const String &p_name) const;
  167. GLint get_uniform_location(int p_index) const;
  168. static _FORCE_INLINE_ ShaderGLES2 *get_active() { return active; }
  169. bool bind();
  170. void unbind();
  171. void bind_uniforms();
  172. inline GLuint get_program() const { return version ? version->id : 0; }
  173. void clear_caches();
  174. _FORCE_INLINE_ void _set_uniform_value(GLint p_uniform, const Pair<ShaderLanguage::DataType, Vector<ShaderLanguage::ConstantNode::Value> > &value) {
  175. if (p_uniform < 0)
  176. return;
  177. const Vector<ShaderLanguage::ConstantNode::Value> &values = value.second;
  178. switch (value.first) {
  179. case ShaderLanguage::TYPE_BOOL: {
  180. glUniform1i(p_uniform, values[0].boolean);
  181. } break;
  182. case ShaderLanguage::TYPE_BVEC2: {
  183. glUniform2i(p_uniform, values[0].boolean, values[1].boolean);
  184. } break;
  185. case ShaderLanguage::TYPE_BVEC3: {
  186. glUniform3i(p_uniform, values[0].boolean, values[1].boolean, values[2].boolean);
  187. } break;
  188. case ShaderLanguage::TYPE_BVEC4: {
  189. glUniform4i(p_uniform, values[0].boolean, values[1].boolean, values[2].boolean, values[3].boolean);
  190. } break;
  191. case ShaderLanguage::TYPE_INT: {
  192. glUniform1i(p_uniform, values[0].sint);
  193. } break;
  194. case ShaderLanguage::TYPE_IVEC2: {
  195. glUniform2i(p_uniform, values[0].sint, values[1].sint);
  196. } break;
  197. case ShaderLanguage::TYPE_IVEC3: {
  198. glUniform3i(p_uniform, values[0].sint, values[1].sint, values[2].sint);
  199. } break;
  200. case ShaderLanguage::TYPE_IVEC4: {
  201. glUniform4i(p_uniform, values[0].sint, values[1].sint, values[2].sint, values[3].sint);
  202. } break;
  203. case ShaderLanguage::TYPE_UINT: {
  204. glUniform1i(p_uniform, values[0].uint);
  205. } break;
  206. case ShaderLanguage::TYPE_UVEC2: {
  207. glUniform2i(p_uniform, values[0].uint, values[1].uint);
  208. } break;
  209. case ShaderLanguage::TYPE_UVEC3: {
  210. glUniform3i(p_uniform, values[0].uint, values[1].uint, values[2].uint);
  211. } break;
  212. case ShaderLanguage::TYPE_UVEC4: {
  213. glUniform4i(p_uniform, values[0].uint, values[1].uint, values[2].uint, values[3].uint);
  214. } break;
  215. case ShaderLanguage::TYPE_FLOAT: {
  216. glUniform1f(p_uniform, values[0].real);
  217. } break;
  218. case ShaderLanguage::TYPE_VEC2: {
  219. glUniform2f(p_uniform, values[0].real, values[1].real);
  220. } break;
  221. case ShaderLanguage::TYPE_VEC3: {
  222. glUniform3f(p_uniform, values[0].real, values[1].real, values[2].real);
  223. } break;
  224. case ShaderLanguage::TYPE_VEC4: {
  225. glUniform4f(p_uniform, values[0].real, values[1].real, values[2].real, values[3].real);
  226. } break;
  227. case ShaderLanguage::TYPE_MAT2: {
  228. GLfloat mat[4];
  229. for (int i = 0; i < 4; i++) {
  230. mat[i] = values[i].real;
  231. }
  232. glUniformMatrix2fv(p_uniform, 1, GL_FALSE, mat);
  233. } break;
  234. case ShaderLanguage::TYPE_MAT3: {
  235. GLfloat mat[9];
  236. for (int i = 0; i < 9; i++) {
  237. mat[i] = values[i].real;
  238. }
  239. glUniformMatrix3fv(p_uniform, 1, GL_FALSE, mat);
  240. } break;
  241. case ShaderLanguage::TYPE_MAT4: {
  242. GLfloat mat[16];
  243. for (int i = 0; i < 16; i++) {
  244. mat[i] = values[i].real;
  245. }
  246. glUniformMatrix4fv(p_uniform, 1, GL_FALSE, mat);
  247. } break;
  248. case ShaderLanguage::TYPE_SAMPLER2D: {
  249. } break;
  250. case ShaderLanguage::TYPE_ISAMPLER2D: {
  251. } break;
  252. case ShaderLanguage::TYPE_USAMPLER2D: {
  253. } break;
  254. case ShaderLanguage::TYPE_SAMPLERCUBE: {
  255. } break;
  256. case ShaderLanguage::TYPE_SAMPLER2DARRAY:
  257. case ShaderLanguage::TYPE_ISAMPLER2DARRAY:
  258. case ShaderLanguage::TYPE_USAMPLER2DARRAY:
  259. case ShaderLanguage::TYPE_SAMPLER3D:
  260. case ShaderLanguage::TYPE_ISAMPLER3D:
  261. case ShaderLanguage::TYPE_USAMPLER3D: {
  262. // Not implemented in GLES2
  263. } break;
  264. case ShaderLanguage::TYPE_VOID: {
  265. // Nothing to do?
  266. } break;
  267. }
  268. }
  269. _FORCE_INLINE_ void _set_uniform_variant(GLint p_uniform, const Variant &p_value) {
  270. if (p_uniform < 0)
  271. return; // do none
  272. switch (p_value.get_type()) {
  273. case Variant::BOOL:
  274. case Variant::INT: {
  275. int val = p_value;
  276. glUniform1i(p_uniform, val);
  277. } break;
  278. case Variant::REAL: {
  279. real_t val = p_value;
  280. glUniform1f(p_uniform, val);
  281. } break;
  282. case Variant::COLOR: {
  283. Color val = p_value;
  284. glUniform4f(p_uniform, val.r, val.g, val.b, val.a);
  285. } break;
  286. case Variant::VECTOR2: {
  287. Vector2 val = p_value;
  288. glUniform2f(p_uniform, val.x, val.y);
  289. } break;
  290. case Variant::VECTOR3: {
  291. Vector3 val = p_value;
  292. glUniform3f(p_uniform, val.x, val.y, val.z);
  293. } break;
  294. case Variant::PLANE: {
  295. Plane val = p_value;
  296. glUniform4f(p_uniform, val.normal.x, val.normal.y, val.normal.z, val.d);
  297. } break;
  298. case Variant::QUAT: {
  299. Quat val = p_value;
  300. glUniform4f(p_uniform, val.x, val.y, val.z, val.w);
  301. } break;
  302. case Variant::TRANSFORM2D: {
  303. Transform2D tr = p_value;
  304. GLfloat matrix[16] = { /* build a 16x16 matrix */
  305. tr.elements[0][0],
  306. tr.elements[0][1],
  307. 0,
  308. 0,
  309. tr.elements[1][0],
  310. tr.elements[1][1],
  311. 0,
  312. 0,
  313. 0,
  314. 0,
  315. 1,
  316. 0,
  317. tr.elements[2][0],
  318. tr.elements[2][1],
  319. 0,
  320. 1
  321. };
  322. glUniformMatrix4fv(p_uniform, 1, false, matrix);
  323. } break;
  324. case Variant::BASIS:
  325. case Variant::TRANSFORM: {
  326. Transform tr = p_value;
  327. GLfloat matrix[16] = { /* build a 16x16 matrix */
  328. tr.basis.elements[0][0],
  329. tr.basis.elements[1][0],
  330. tr.basis.elements[2][0],
  331. 0,
  332. tr.basis.elements[0][1],
  333. tr.basis.elements[1][1],
  334. tr.basis.elements[2][1],
  335. 0,
  336. tr.basis.elements[0][2],
  337. tr.basis.elements[1][2],
  338. tr.basis.elements[2][2],
  339. 0,
  340. tr.origin.x,
  341. tr.origin.y,
  342. tr.origin.z,
  343. 1
  344. };
  345. glUniformMatrix4fv(p_uniform, 1, false, matrix);
  346. } break;
  347. case Variant::OBJECT: {
  348. } break;
  349. default: { ERR_FAIL(); } // do nothing
  350. }
  351. }
  352. uint32_t create_custom_shader();
  353. void set_custom_shader_code(uint32_t p_code_id,
  354. const String &p_vertex,
  355. const String &p_vertex_globals,
  356. const String &p_fragment,
  357. const String &p_light,
  358. const String &p_fragment_globals,
  359. const Vector<StringName> &p_uniforms,
  360. const Vector<StringName> &p_texture_uniforms,
  361. const Vector<CharString> &p_custom_defines);
  362. void set_custom_shader(uint32_t p_code_id);
  363. void free_custom_shader(uint32_t p_code_id);
  364. void set_uniform_default(int p_idx, const Variant &p_value) {
  365. if (p_value.get_type() == Variant::NIL) {
  366. uniform_defaults.erase(p_idx);
  367. } else {
  368. uniform_defaults[p_idx] = p_value;
  369. }
  370. uniforms_dirty = true;
  371. }
  372. // this void* is actually a RasterizerStorageGLES2::Material, but C++ doesn't
  373. // like forward declared nested classes.
  374. void use_material(void *p_material);
  375. _FORCE_INLINE_ uint32_t get_version() const { return new_conditional_version.version; }
  376. _FORCE_INLINE_ bool is_version_valid() const { return version && version->ok; }
  377. void set_uniform_camera(int p_idx, const CameraMatrix &p_mat) {
  378. uniform_cameras[p_idx] = p_mat;
  379. uniforms_dirty = true;
  380. }
  381. _FORCE_INLINE_ void set_texture_uniform(int p_idx, const Variant &p_value) {
  382. ERR_FAIL_COND(!version);
  383. ERR_FAIL_INDEX(p_idx, version->texture_uniform_locations.size());
  384. _set_uniform_variant(version->texture_uniform_locations[p_idx], p_value);
  385. }
  386. _FORCE_INLINE_ GLint get_texture_uniform_location(int p_idx) {
  387. ERR_FAIL_COND_V(!version, -1);
  388. ERR_FAIL_INDEX_V(p_idx, version->texture_uniform_locations.size(), -1);
  389. return version->texture_uniform_locations[p_idx];
  390. }
  391. virtual void init() = 0;
  392. void finish();
  393. void set_base_material_tex_index(int p_idx);
  394. void add_custom_define(const String &p_define) {
  395. custom_defines.push_back(p_define.utf8());
  396. }
  397. virtual ~ShaderGLES2();
  398. };
  399. // called a lot, made inline
  400. int ShaderGLES2::_get_uniform(int p_which) const {
  401. ERR_FAIL_INDEX_V(p_which, uniform_count, -1);
  402. ERR_FAIL_COND_V(!version, -1);
  403. return version->uniform_location[p_which];
  404. }
  405. void ShaderGLES2::_set_conditional(int p_which, bool p_value) {
  406. ERR_FAIL_INDEX(p_which, conditional_count);
  407. if (p_value)
  408. new_conditional_version.version |= (1 << p_which);
  409. else
  410. new_conditional_version.version &= ~(1 << p_which);
  411. }
  412. #endif