shader_gles3.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. /**************************************************************************/
  2. /* shader_gles3.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #include "shader_gles3.h"
  31. #include "core/local_vector.h"
  32. #include "core/os/os.h"
  33. #include "core/print_string.h"
  34. #include "core/threaded_callable_queue.h"
  35. #include "drivers/gles3/shader_cache_gles3.h"
  36. #include "servers/visual_server.h"
  37. //#define DEBUG_OPENGL
  38. #ifdef DEBUG_OPENGL
  39. #define DEBUG_TEST_ERROR(m_section) \
  40. { \
  41. uint32_t err = glGetError(); \
  42. if (err) { \
  43. print_line("OpenGL Error #" + itos(err) + " at: " + m_section); \
  44. } \
  45. }
  46. #else
  47. #define DEBUG_TEST_ERROR(m_section)
  48. #endif
  49. ShaderGLES3 *ShaderGLES3::active = nullptr;
  50. SelfList<ShaderGLES3::Version>::List ShaderGLES3::versions_compiling;
  51. ShaderCacheGLES3 *ShaderGLES3::shader_cache;
  52. ThreadedCallableQueue<GLuint> *ShaderGLES3::cache_write_queue;
  53. ThreadedCallableQueue<GLuint> *ShaderGLES3::compile_queue;
  54. bool ShaderGLES3::parallel_compile_supported;
  55. bool ShaderGLES3::async_hidden_forbidden;
  56. uint32_t *ShaderGLES3::compiles_started_this_frame;
  57. uint32_t *ShaderGLES3::max_frame_compiles_in_progress;
  58. uint32_t ShaderGLES3::max_simultaneous_compiles;
  59. uint32_t ShaderGLES3::active_compiles_count;
  60. #ifdef DEBUG_ENABLED
  61. bool ShaderGLES3::log_active_async_compiles_count;
  62. #endif
  63. uint64_t ShaderGLES3::current_frame;
  64. //#define DEBUG_SHADER
  65. #ifdef DEBUG_SHADER
  66. #define DEBUG_PRINT(m_text) print_line(m_text);
  67. #else
  68. #define DEBUG_PRINT(m_text)
  69. #endif
  70. #define _EXT_COMPLETION_STATUS 0x91B1
  71. GLint ShaderGLES3::get_uniform_location(int p_index) const {
  72. ERR_FAIL_COND_V(!version, -1);
  73. return version->uniform_location[p_index];
  74. }
  75. bool ShaderGLES3::bind() {
  76. return _bind(false);
  77. }
  78. bool ShaderGLES3::_bind(bool p_binding_fallback) {
  79. // Same base shader and version valid version?
  80. if (active == this && version) {
  81. if (new_conditional_version.code_version == conditional_version.code_version) {
  82. if (new_conditional_version.version == conditional_version.version) {
  83. return false;
  84. }
  85. // From ubershader to ubershader of the same code?
  86. if ((conditional_version.version & VersionKey::UBERSHADER_FLAG) && (new_conditional_version.version & VersionKey::UBERSHADER_FLAG)) {
  87. conditional_version.version = new_conditional_version.version;
  88. return false;
  89. }
  90. }
  91. }
  92. bool must_be_ready_now = !is_async_compilation_supported() || p_binding_fallback;
  93. conditional_version = new_conditional_version;
  94. version = get_current_version(must_be_ready_now);
  95. ERR_FAIL_COND_V(!version, false);
  96. bool ready = false;
  97. ready = _process_program_state(version, must_be_ready_now);
  98. if (version->compile_status == Version::COMPILE_STATUS_RESTART_NEEDED) {
  99. get_current_version(must_be_ready_now); // Trigger recompile
  100. ready = _process_program_state(version, must_be_ready_now);
  101. }
  102. #ifdef DEBUG_ENABLED
  103. if (ready) {
  104. if (VS::get_singleton()->is_force_shader_fallbacks_enabled() && !must_be_ready_now && get_ubershader_flags_uniform() != -1) {
  105. ready = false;
  106. }
  107. }
  108. #endif
  109. if (ready) {
  110. glUseProgram(version->ids.main);
  111. if (!version->uniforms_ready) {
  112. _setup_uniforms(custom_code_map.getptr(conditional_version.code_version));
  113. version->uniforms_ready = true;
  114. }
  115. DEBUG_TEST_ERROR("Use Program");
  116. active = this;
  117. return true;
  118. } else if (!must_be_ready_now && version->async_mode == ASYNC_MODE_VISIBLE && !p_binding_fallback && get_ubershader_flags_uniform() != -1) {
  119. // We can and have to fall back to the ubershader
  120. return _bind_ubershader();
  121. } else {
  122. // We have a compile error or must fall back by skipping render
  123. unbind();
  124. return false;
  125. }
  126. }
  127. bool ShaderGLES3::is_custom_code_ready_for_render(uint32_t p_code_id) {
  128. if (p_code_id == 0) {
  129. return true;
  130. }
  131. if (!is_async_compilation_supported() || get_ubershader_flags_uniform() == -1) {
  132. return true;
  133. }
  134. CustomCode *cc = custom_code_map.getptr(p_code_id);
  135. ERR_FAIL_COND_V(!cc, false);
  136. if (cc->async_mode == ASYNC_MODE_HIDDEN) {
  137. #ifdef DEBUG_ENABLED
  138. if (VS::get_singleton()->is_force_shader_fallbacks_enabled()) {
  139. return false;
  140. }
  141. #endif
  142. VersionKey effective_version;
  143. effective_version.version = new_conditional_version.version;
  144. effective_version.code_version = p_code_id;
  145. Version *v = version_map.getptr(effective_version);
  146. if (!v || cc->version != v->code_version || v->compile_status != Version::COMPILE_STATUS_OK) {
  147. return false;
  148. }
  149. }
  150. return true;
  151. }
  152. bool ShaderGLES3::_bind_ubershader(bool p_for_warmup) {
  153. #ifdef DEBUG_ENABLED
  154. ERR_FAIL_COND_V(!is_async_compilation_supported(), false);
  155. ERR_FAIL_COND_V(get_ubershader_flags_uniform() == -1, false);
  156. #endif
  157. new_conditional_version.version |= VersionKey::UBERSHADER_FLAG;
  158. bool bound = _bind(true);
  159. new_conditional_version.version &= ~VersionKey::UBERSHADER_FLAG;
  160. if (p_for_warmup) {
  161. // Avoid GL UB message id 131222 caused by shadow samplers not properly set up yet
  162. unbind();
  163. return bound;
  164. }
  165. int conditionals_uniform = _get_uniform(get_ubershader_flags_uniform());
  166. #ifdef DEBUG_ENABLED
  167. ERR_FAIL_COND_V(conditionals_uniform == -1, false);
  168. #endif
  169. #ifdef DEV_ENABLED
  170. // So far we don't need bit 31 for conditionals. That allows us to use signed integers,
  171. // which are more compatible across GL driver vendors.
  172. CRASH_COND(new_conditional_version.version >= 0x80000000);
  173. #endif
  174. glUniform1i(conditionals_uniform, new_conditional_version.version);
  175. return bound;
  176. }
  177. void ShaderGLES3::advance_async_shaders_compilation() {
  178. SelfList<ShaderGLES3::Version> *curr = versions_compiling.first();
  179. while (curr) {
  180. SelfList<ShaderGLES3::Version> *next = curr->next();
  181. ShaderGLES3::Version *v = curr->self();
  182. // Only if it didn't already have a chance to be processed in this frame
  183. if (v->last_frame_processed != current_frame) {
  184. v->shader->_process_program_state(v, false);
  185. }
  186. curr = next;
  187. }
  188. }
  189. void ShaderGLES3::_log_active_compiles() {
  190. #ifdef DEBUG_ENABLED
  191. if (log_active_async_compiles_count) {
  192. if (parallel_compile_supported) {
  193. print_line("Async. shader compiles: " + itos(active_compiles_count));
  194. } else if (compile_queue) {
  195. print_line("Queued shader compiles: " + itos(active_compiles_count));
  196. } else {
  197. CRASH_NOW();
  198. }
  199. }
  200. #endif
  201. }
  202. bool ShaderGLES3::_process_program_state(Version *p_version, bool p_async_forbidden) {
  203. bool ready = false;
  204. bool run_next_step = true;
  205. while (run_next_step) {
  206. run_next_step = false;
  207. switch (p_version->compile_status) {
  208. case Version::COMPILE_STATUS_OK: {
  209. // Yeaaah!
  210. ready = true;
  211. } break;
  212. case Version::COMPILE_STATUS_ERROR: {
  213. // Sad, but we have to accept it
  214. } break;
  215. case Version::COMPILE_STATUS_PENDING:
  216. case Version::COMPILE_STATUS_RESTART_NEEDED: {
  217. // These lead to nowhere unless other piece of code starts the compile process
  218. } break;
  219. case Version::COMPILE_STATUS_SOURCE_PROVIDED: {
  220. uint32_t start_compiles_count = p_async_forbidden ? 2 : 0;
  221. if (!start_compiles_count) {
  222. uint32_t used_async_slots = MAX(active_compiles_count, *compiles_started_this_frame);
  223. uint32_t free_async_slots = used_async_slots < max_simultaneous_compiles ? max_simultaneous_compiles - used_async_slots : 0;
  224. start_compiles_count = MIN(2, free_async_slots);
  225. }
  226. if (start_compiles_count >= 1) {
  227. glCompileShader(p_version->ids.vert);
  228. if (start_compiles_count == 1) {
  229. p_version->compile_status = Version::COMPILE_STATUS_COMPILING_VERTEX;
  230. } else {
  231. glCompileShader(p_version->ids.frag);
  232. p_version->compile_status = Version::COMPILE_STATUS_COMPILING_VERTEX_AND_FRAGMENT;
  233. }
  234. if (!p_async_forbidden) {
  235. versions_compiling.add_last(&p_version->compiling_list);
  236. // Vertex and fragment shaders take independent compile slots
  237. active_compiles_count += start_compiles_count;
  238. *max_frame_compiles_in_progress = MAX(*max_frame_compiles_in_progress, active_compiles_count);
  239. _log_active_compiles();
  240. }
  241. (*compiles_started_this_frame) += start_compiles_count;
  242. run_next_step = p_async_forbidden;
  243. }
  244. } break;
  245. case Version::COMPILE_STATUS_COMPILING_VERTEX: {
  246. bool must_compile_frag_now = p_async_forbidden;
  247. if (!must_compile_frag_now) {
  248. if (active_compiles_count < max_simultaneous_compiles && *compiles_started_this_frame < max_simultaneous_compiles) {
  249. must_compile_frag_now = true;
  250. }
  251. }
  252. if (must_compile_frag_now) {
  253. glCompileShader(p_version->ids.frag);
  254. if (p_version->compiling_list.in_list()) {
  255. active_compiles_count++;
  256. *max_frame_compiles_in_progress = MAX(*max_frame_compiles_in_progress, active_compiles_count);
  257. _log_active_compiles();
  258. }
  259. p_version->compile_status = Version::COMPILE_STATUS_COMPILING_VERTEX_AND_FRAGMENT;
  260. } else if (parallel_compile_supported) {
  261. GLint completed = 0;
  262. glGetShaderiv(p_version->ids.vert, _EXT_COMPLETION_STATUS, &completed);
  263. if (completed) {
  264. // Not touching compiles count since the same slot used for vertex is now used for fragment
  265. glCompileShader(p_version->ids.frag);
  266. p_version->compile_status = Version::COMPILE_STATUS_COMPILING_FRAGMENT;
  267. }
  268. }
  269. run_next_step = p_async_forbidden;
  270. } break;
  271. case Version::COMPILE_STATUS_COMPILING_FRAGMENT:
  272. case Version::COMPILE_STATUS_COMPILING_VERTEX_AND_FRAGMENT: {
  273. bool must_complete_now = p_async_forbidden;
  274. if (!must_complete_now && parallel_compile_supported) {
  275. GLint vertex_completed = 0;
  276. if (p_version->compile_status == Version::COMPILE_STATUS_COMPILING_FRAGMENT) {
  277. vertex_completed = true;
  278. } else {
  279. glGetShaderiv(p_version->ids.vert, _EXT_COMPLETION_STATUS, &vertex_completed);
  280. if (p_version->compiling_list.in_list()) {
  281. active_compiles_count--;
  282. #ifdef DEV_ENABLED
  283. CRASH_COND(active_compiles_count == UINT32_MAX);
  284. #endif
  285. *max_frame_compiles_in_progress = MAX(*max_frame_compiles_in_progress, active_compiles_count);
  286. _log_active_compiles();
  287. }
  288. p_version->compile_status = Version::COMPILE_STATUS_COMPILING_FRAGMENT;
  289. }
  290. if (vertex_completed) {
  291. GLint frag_completed = 0;
  292. glGetShaderiv(p_version->ids.frag, _EXT_COMPLETION_STATUS, &frag_completed);
  293. if (frag_completed) {
  294. must_complete_now = true;
  295. }
  296. }
  297. }
  298. if (must_complete_now) {
  299. bool must_save_to_cache = p_version->version_key.is_subject_to_caching() && p_version->program_binary.source != Version::ProgramBinary::SOURCE_CACHE && shader_cache;
  300. bool ok = p_version->shader->_complete_compile(p_version->ids, must_save_to_cache);
  301. if (ok) {
  302. p_version->compile_status = Version::COMPILE_STATUS_LINKING;
  303. run_next_step = p_async_forbidden;
  304. } else {
  305. p_version->compile_status = Version::COMPILE_STATUS_ERROR;
  306. if (p_version->compiling_list.in_list()) {
  307. p_version->compiling_list.remove_from_list();
  308. active_compiles_count--;
  309. #ifdef DEV_ENABLED
  310. CRASH_COND(active_compiles_count == UINT32_MAX);
  311. #endif
  312. _log_active_compiles();
  313. }
  314. }
  315. }
  316. } break;
  317. case Version::COMPILE_STATUS_PROCESSING_AT_QUEUE: {
  318. // This is from the async. queue
  319. switch (p_version->program_binary.result_from_queue.get()) {
  320. case -1: { // Error
  321. p_version->compile_status = Version::COMPILE_STATUS_ERROR;
  322. p_version->compiling_list.remove_from_list();
  323. active_compiles_count--;
  324. #ifdef DEV_ENABLED
  325. CRASH_COND(active_compiles_count == UINT32_MAX);
  326. #endif
  327. _log_active_compiles();
  328. } break;
  329. case 0: { // In progress
  330. if (p_async_forbidden) {
  331. OS::get_singleton()->delay_usec(1000);
  332. run_next_step = true;
  333. }
  334. } break;
  335. case 1: { // Complete
  336. p_version->compile_status = Version::COMPILE_STATUS_BINARY_READY;
  337. run_next_step = true;
  338. } break;
  339. }
  340. } break;
  341. case Version::COMPILE_STATUS_BINARY_READY_FROM_CACHE: {
  342. bool eat_binary_now = p_async_forbidden;
  343. if (!eat_binary_now) {
  344. if (active_compiles_count < max_simultaneous_compiles && *compiles_started_this_frame < max_simultaneous_compiles) {
  345. eat_binary_now = true;
  346. }
  347. }
  348. if (eat_binary_now) {
  349. p_version->compile_status = Version::COMPILE_STATUS_BINARY_READY;
  350. run_next_step = true;
  351. if (!p_async_forbidden) {
  352. versions_compiling.add_last(&p_version->compiling_list);
  353. active_compiles_count++;
  354. *max_frame_compiles_in_progress = MAX(*max_frame_compiles_in_progress, active_compiles_count);
  355. _log_active_compiles();
  356. (*compiles_started_this_frame)++;
  357. }
  358. }
  359. } break;
  360. case Version::COMPILE_STATUS_BINARY_READY: {
  361. PoolByteArray::Read r = p_version->program_binary.data.read();
  362. glProgramBinary(p_version->ids.main, static_cast<GLenum>(p_version->program_binary.format), r.ptr(), p_version->program_binary.data.size());
  363. p_version->compile_status = Version::COMPILE_STATUS_LINKING;
  364. run_next_step = true;
  365. } break;
  366. case Version::COMPILE_STATUS_LINKING: {
  367. bool must_complete_now = p_async_forbidden || p_version->program_binary.source == Version::ProgramBinary::SOURCE_QUEUE;
  368. if (!must_complete_now && parallel_compile_supported) {
  369. GLint link_completed;
  370. glGetProgramiv(p_version->ids.main, _EXT_COMPLETION_STATUS, &link_completed);
  371. must_complete_now = link_completed;
  372. }
  373. if (must_complete_now) {
  374. bool must_save_to_cache = p_version->version_key.is_subject_to_caching() && p_version->program_binary.source != Version::ProgramBinary::SOURCE_CACHE && shader_cache;
  375. bool ok = false;
  376. if (must_save_to_cache && p_version->program_binary.source == Version::ProgramBinary::SOURCE_LOCAL) {
  377. ok = p_version->shader->_complete_link(p_version->ids, &p_version->program_binary.format, &p_version->program_binary.data);
  378. } else {
  379. ok = p_version->shader->_complete_link(p_version->ids);
  380. #ifdef DEBUG_ENABLED
  381. #if 0
  382. // Simulate GL rejecting program from cache
  383. if (p_version->program_binary.source == Version::ProgramBinary::SOURCE_CACHE) {
  384. ok = false;
  385. }
  386. #endif
  387. #endif
  388. }
  389. if (ok) {
  390. if (must_save_to_cache) {
  391. String &tmp_hash = p_version->program_binary.cache_hash;
  392. GLenum &tmp_format = p_version->program_binary.format;
  393. PoolByteArray &tmp_data = p_version->program_binary.data;
  394. cache_write_queue->enqueue(p_version->ids.main, [=]() {
  395. shader_cache->store(tmp_hash, static_cast<uint32_t>(tmp_format), tmp_data);
  396. });
  397. }
  398. p_version->compile_status = Version::COMPILE_STATUS_OK;
  399. ready = true;
  400. } else {
  401. if (p_version->program_binary.source == Version::ProgramBinary::SOURCE_CACHE) {
  402. #ifdef DEBUG_ENABLED
  403. WARN_PRINT("Program binary from cache has been rejected by the GL. Removing from cache.");
  404. #endif
  405. shader_cache->remove(p_version->program_binary.cache_hash);
  406. p_version->compile_status = Version::COMPILE_STATUS_RESTART_NEEDED;
  407. } else {
  408. if (p_version->program_binary.source == Version::ProgramBinary::SOURCE_QUEUE) {
  409. ERR_PRINT("Program binary from compile queue has been rejected by the GL. Bug?");
  410. }
  411. p_version->compile_status = Version::COMPILE_STATUS_ERROR;
  412. }
  413. }
  414. p_version->program_binary.data = PoolByteArray();
  415. p_version->program_binary.cache_hash.clear();
  416. if (p_version->compiling_list.in_list()) {
  417. p_version->compiling_list.remove_from_list();
  418. active_compiles_count--;
  419. #ifdef DEV_ENABLED
  420. CRASH_COND(active_compiles_count == UINT32_MAX);
  421. #endif
  422. _log_active_compiles();
  423. }
  424. }
  425. } break;
  426. }
  427. }
  428. return ready;
  429. }
  430. void ShaderGLES3::unbind() {
  431. version = nullptr;
  432. glUseProgram(0);
  433. active = nullptr;
  434. }
  435. static void _display_error_with_code(const String &p_error, GLuint p_shader_id) {
  436. int line = 1;
  437. GLint source_len;
  438. glGetShaderiv(p_shader_id, GL_SHADER_SOURCE_LENGTH, &source_len);
  439. LocalVector<GLchar> source_buffer;
  440. source_buffer.resize(source_len);
  441. glGetShaderSource(p_shader_id, source_len, NULL, source_buffer.ptr());
  442. String total_code(source_buffer.ptr());
  443. Vector<String> lines = String(total_code).split("\n");
  444. for (int j = 0; j < lines.size(); j++) {
  445. print_line(vformat("%4d | %s", line, lines[j]));
  446. line++;
  447. }
  448. ERR_PRINT(p_error);
  449. }
  450. static CharString _prepare_ubershader_chunk(const CharString &p_chunk) {
  451. String s(p_chunk.get_data());
  452. Vector<String> lines = s.split("\n");
  453. s.clear();
  454. for (int i = 0; i < lines.size(); ++i) {
  455. if (lines[i].ends_with("//ubershader-skip")) {
  456. continue;
  457. } else if (lines[i].ends_with("//ubershader-runtime")) {
  458. // Move from the preprocessor world to the true code realm
  459. String l = lines[i].trim_suffix("//ubershader-runtime").strip_edges();
  460. {
  461. // Ignore other comments
  462. Vector<String> pieces = l.split("//");
  463. l = pieces[0].strip_edges();
  464. }
  465. if (l == "#else") {
  466. s += "} else {\n";
  467. } else if (l == "#endif") {
  468. s += "}\n";
  469. } else if (l.begins_with("#ifdef")) {
  470. Vector<String> pieces = l.split_spaces();
  471. CRASH_COND(pieces.size() != 2);
  472. s += "if ((ubershader_flags & FLAG_" + pieces[1] + ") != 0) {\n";
  473. } else if (l.begins_with("#ifndef")) {
  474. Vector<String> pieces = l.split_spaces();
  475. CRASH_COND(pieces.size() != 2);
  476. s += "if ((ubershader_flags & FLAG_" + pieces[1] + ") == 0) {\n";
  477. } else {
  478. CRASH_NOW_MSG("The shader template is using too complex syntax in a line marked with ubershader-runtime.");
  479. }
  480. continue;
  481. }
  482. s += lines[i] + "\n";
  483. }
  484. return s.ascii();
  485. }
  486. // Possible source-status pairs after this:
  487. // Local - Source provided
  488. // Queue - Processing / Binary ready / Error
  489. // Cache - Binary ready
  490. ShaderGLES3::Version *ShaderGLES3::get_current_version(bool &r_async_forbidden) {
  491. VersionKey effective_version;
  492. effective_version.key = conditional_version.key;
  493. // Store and look up ubershader with all other version bits set to zero
  494. if ((conditional_version.version & VersionKey::UBERSHADER_FLAG)) {
  495. effective_version.version = VersionKey::UBERSHADER_FLAG;
  496. }
  497. Version *_v = version_map.getptr(effective_version);
  498. CustomCode *cc = nullptr;
  499. if (_v) {
  500. if (_v->compile_status == Version::COMPILE_STATUS_RESTART_NEEDED) {
  501. _v->program_binary.source = Version::ProgramBinary::SOURCE_NONE;
  502. } else {
  503. if (effective_version.code_version != 0) {
  504. cc = custom_code_map.getptr(effective_version.code_version);
  505. ERR_FAIL_COND_V(!cc, _v);
  506. if (cc->version == _v->code_version) {
  507. return _v;
  508. }
  509. } else {
  510. return _v;
  511. }
  512. }
  513. }
  514. if (!_v) {
  515. _v = &version_map[effective_version];
  516. _v->version_key = effective_version;
  517. _v->shader = this;
  518. _v->uniform_location = memnew_arr(GLint, uniform_count);
  519. }
  520. Version &v = *_v;
  521. /* SETUP CONDITIONALS */
  522. LocalVector<const char *> strings_common;
  523. #ifdef GLES_OVER_GL
  524. strings_common.push_back("#version 330\n");
  525. strings_common.push_back("#define GLES_OVER_GL\n");
  526. #else
  527. strings_common.push_back("#version 300 es\n");
  528. #endif
  529. #ifdef ANDROID_ENABLED
  530. strings_common.push_back("#define ANDROID_ENABLED\n");
  531. #endif
  532. for (int i = 0; i < custom_defines.size(); i++) {
  533. strings_common.push_back(custom_defines[i].get_data());
  534. strings_common.push_back("\n");
  535. }
  536. if (is_async_compilation_supported() && get_ubershader_flags_uniform() != -1) {
  537. // Indicate that this shader may be used both as ubershader and conditioned during the session
  538. strings_common.push_back("#define UBERSHADER_COMPAT\n");
  539. }
  540. LocalVector<CharString> flag_macros;
  541. bool build_ubershader = get_ubershader_flags_uniform() != -1 && (effective_version.version & VersionKey::UBERSHADER_FLAG);
  542. if (build_ubershader) {
  543. strings_common.push_back("#define IS_UBERSHADER\n");
  544. for (int i = 0; i < conditional_count; i++) {
  545. String s = vformat("#define FLAG_%s (1 << %d)\n", String(conditional_defines[i]).strip_edges().trim_prefix("#define "), i);
  546. CharString cs = s.ascii();
  547. flag_macros.push_back(cs);
  548. strings_common.push_back(cs.ptr());
  549. }
  550. strings_common.push_back("\n");
  551. } else {
  552. for (int i = 0; i < conditional_count; i++) {
  553. bool enable = ((1 << i) & effective_version.version);
  554. strings_common.push_back(enable ? conditional_defines[i] : "");
  555. if (enable) {
  556. DEBUG_PRINT(conditional_defines[i]);
  557. }
  558. }
  559. }
  560. //keep them around during the function
  561. struct {
  562. CharString code_string;
  563. CharString code_globals;
  564. CharString material_string;
  565. } vert;
  566. struct {
  567. CharString code_string;
  568. CharString code_string2;
  569. CharString code_globals;
  570. CharString material_string;
  571. } frag;
  572. if (effective_version.code_version != 0) {
  573. ERR_FAIL_COND_V(!custom_code_map.has(effective_version.code_version), nullptr);
  574. if (!cc) {
  575. cc = &custom_code_map[effective_version.code_version];
  576. }
  577. if (cc->version != v.code_version) {
  578. v.code_version = cc->version;
  579. v.async_mode = cc->async_mode;
  580. v.uniforms_ready = false;
  581. }
  582. }
  583. /* CREATE PROGRAM */
  584. v.ids.main = glCreateProgram();
  585. ERR_FAIL_COND_V(v.ids.main == 0, nullptr);
  586. // To create the ubershader we need to modify the static strings;
  587. // they'll go in this array
  588. LocalVector<CharString> filtered_strings;
  589. /* VERTEX SHADER */
  590. if (cc) {
  591. for (int i = 0; i < cc->custom_defines.size(); i++) {
  592. strings_common.push_back(cc->custom_defines[i].get_data());
  593. DEBUG_PRINT("CD #" + itos(i) + ": " + String(cc->custom_defines[i]));
  594. }
  595. }
  596. LocalVector<const char *> strings_vertex(strings_common);
  597. //vertex precision is high
  598. strings_vertex.push_back("precision highp float;\n");
  599. strings_vertex.push_back("precision highp int;\n");
  600. #ifndef GLES_OVER_GL
  601. strings_vertex.push_back("precision highp sampler2D;\n");
  602. strings_vertex.push_back("precision highp samplerCube;\n");
  603. strings_vertex.push_back("precision highp sampler2DArray;\n");
  604. #endif
  605. if (build_ubershader) {
  606. CharString s = _prepare_ubershader_chunk(vertex_code0);
  607. filtered_strings.push_back(s);
  608. strings_vertex.push_back(s.get_data());
  609. } else {
  610. strings_vertex.push_back(vertex_code0.get_data());
  611. }
  612. if (cc) {
  613. vert.material_string = cc->uniforms.ascii();
  614. strings_vertex.push_back(vert.material_string.get_data());
  615. }
  616. if (build_ubershader) {
  617. CharString s = _prepare_ubershader_chunk(vertex_code1);
  618. filtered_strings.push_back(s);
  619. strings_vertex.push_back(s.get_data());
  620. } else {
  621. strings_vertex.push_back(vertex_code1.get_data());
  622. }
  623. if (cc) {
  624. vert.code_globals = cc->vertex_globals.ascii();
  625. strings_vertex.push_back(vert.code_globals.get_data());
  626. }
  627. if (build_ubershader) {
  628. CharString s = _prepare_ubershader_chunk(vertex_code2);
  629. filtered_strings.push_back(s);
  630. strings_vertex.push_back(s.get_data());
  631. } else {
  632. strings_vertex.push_back(vertex_code2.get_data());
  633. }
  634. if (cc) {
  635. vert.code_string = cc->vertex.ascii();
  636. strings_vertex.push_back(vert.code_string.get_data());
  637. }
  638. if (build_ubershader) {
  639. CharString s = _prepare_ubershader_chunk(vertex_code3);
  640. filtered_strings.push_back(s);
  641. strings_vertex.push_back(s.get_data());
  642. } else {
  643. strings_vertex.push_back(vertex_code3.get_data());
  644. }
  645. #ifdef DEBUG_SHADER
  646. DEBUG_PRINT("\nVertex Code:\n\n" + String(code_string.get_data()));
  647. for (int i = 0; i < strings_vertex.size(); i++) {
  648. //print_line("vert strings "+itos(i)+":"+String(strings_vertex[i]));
  649. }
  650. #endif
  651. /* FRAGMENT SHADER */
  652. LocalVector<const char *> strings_fragment(strings_common);
  653. //fragment precision is medium
  654. strings_fragment.push_back("precision highp float;\n");
  655. strings_fragment.push_back("precision highp int;\n");
  656. #ifndef GLES_OVER_GL
  657. strings_fragment.push_back("precision highp sampler2D;\n");
  658. strings_fragment.push_back("precision highp samplerCube;\n");
  659. strings_fragment.push_back("precision highp sampler2DArray;\n");
  660. #endif
  661. if (build_ubershader) {
  662. CharString s = _prepare_ubershader_chunk(fragment_code0);
  663. filtered_strings.push_back(s);
  664. strings_fragment.push_back(s.get_data());
  665. } else {
  666. strings_fragment.push_back(fragment_code0.get_data());
  667. }
  668. if (cc) {
  669. frag.material_string = cc->uniforms.ascii();
  670. strings_fragment.push_back(frag.material_string.get_data());
  671. }
  672. if (build_ubershader) {
  673. CharString s = _prepare_ubershader_chunk(fragment_code1);
  674. filtered_strings.push_back(s);
  675. strings_fragment.push_back(s.get_data());
  676. } else {
  677. strings_fragment.push_back(fragment_code1.get_data());
  678. }
  679. if (cc) {
  680. frag.code_globals = cc->fragment_globals.ascii();
  681. strings_fragment.push_back(frag.code_globals.get_data());
  682. }
  683. if (build_ubershader) {
  684. CharString s = _prepare_ubershader_chunk(fragment_code2);
  685. filtered_strings.push_back(s);
  686. strings_fragment.push_back(s.get_data());
  687. } else {
  688. strings_fragment.push_back(fragment_code2.get_data());
  689. }
  690. if (cc) {
  691. frag.code_string = cc->light.ascii();
  692. strings_fragment.push_back(frag.code_string.get_data());
  693. }
  694. if (build_ubershader) {
  695. CharString s = _prepare_ubershader_chunk(fragment_code3);
  696. filtered_strings.push_back(s);
  697. strings_fragment.push_back(s.get_data());
  698. } else {
  699. strings_fragment.push_back(fragment_code3.get_data());
  700. }
  701. if (cc) {
  702. frag.code_string2 = cc->fragment.ascii();
  703. strings_fragment.push_back(frag.code_string2.get_data());
  704. }
  705. if (build_ubershader) {
  706. CharString s = _prepare_ubershader_chunk(fragment_code4);
  707. filtered_strings.push_back(s);
  708. strings_fragment.push_back(s.get_data());
  709. } else {
  710. strings_fragment.push_back(fragment_code4.get_data());
  711. }
  712. #ifdef DEBUG_SHADER
  713. DEBUG_PRINT("\nFragment Globals:\n\n" + String(code_globals.get_data()));
  714. DEBUG_PRINT("\nFragment Code:\n\n" + String(code_string2.get_data()));
  715. for (int i = 0; i < strings_fragment.size(); i++) {
  716. //print_line("frag strings "+itos(i)+":"+String(strings_fragment[i]));
  717. }
  718. #endif
  719. if (!r_async_forbidden) {
  720. r_async_forbidden =
  721. (v.async_mode == ASYNC_MODE_HIDDEN && async_hidden_forbidden) ||
  722. (v.async_mode == ASYNC_MODE_VISIBLE && get_ubershader_flags_uniform() == -1);
  723. }
  724. bool in_cache = false;
  725. if (shader_cache && effective_version.is_subject_to_caching()) {
  726. const char *strings_platform[] = {
  727. reinterpret_cast<const char *>(glGetString(GL_VENDOR)),
  728. reinterpret_cast<const char *>(glGetString(GL_RENDERER)),
  729. reinterpret_cast<const char *>(glGetString(GL_VERSION)),
  730. nullptr,
  731. };
  732. v.program_binary.cache_hash = ShaderCacheGLES3::hash_program(strings_platform, strings_vertex, strings_fragment);
  733. if (shader_cache->retrieve(v.program_binary.cache_hash, &v.program_binary.format, &v.program_binary.data)) {
  734. in_cache = true;
  735. v.program_binary.source = Version::ProgramBinary::SOURCE_CACHE;
  736. v.compile_status = Version::COMPILE_STATUS_BINARY_READY_FROM_CACHE;
  737. }
  738. }
  739. if (!in_cache) {
  740. if (compile_queue && !r_async_forbidden) {
  741. // Asynchronous compilation via queue (secondary context)
  742. // Remarks:
  743. // 1. We need to save vertex and fragment strings because they will not live beyond this function.
  744. // 2. We'll create another program since the other GL context is not shared.
  745. // We are doing it that way since GL drivers can implement context sharing via locking, which
  746. // would render (no pun intended) this whole effort to asynchronous useless.
  747. auto concat_shader_strings = [](const LocalVector<const char *> &p_shader_strings, LocalVector<char> *r_out) {
  748. r_out->clear();
  749. for (uint32_t i = 0; i < p_shader_strings.size(); i++) {
  750. uint32_t initial_size = r_out->size();
  751. uint32_t piece_len = strlen(reinterpret_cast<const char *>(p_shader_strings[i]));
  752. r_out->resize(initial_size + piece_len + 1);
  753. memcpy(r_out->ptr() + initial_size, p_shader_strings[i], piece_len);
  754. *(r_out->ptr() + initial_size + piece_len) = '\n';
  755. }
  756. *(r_out->ptr() + r_out->size() - 1) = '\0';
  757. };
  758. LocalVector<char> vertex_code;
  759. concat_shader_strings(strings_vertex, &vertex_code);
  760. LocalVector<char> fragment_code;
  761. concat_shader_strings(strings_fragment, &fragment_code);
  762. v.program_binary.source = Version::ProgramBinary::SOURCE_QUEUE;
  763. v.compile_status = Version::COMPILE_STATUS_PROCESSING_AT_QUEUE;
  764. versions_compiling.add_last(&v.compiling_list);
  765. active_compiles_count++;
  766. *max_frame_compiles_in_progress = MAX(*max_frame_compiles_in_progress, active_compiles_count);
  767. _log_active_compiles();
  768. (*compiles_started_this_frame)++;
  769. compile_queue->enqueue(v.ids.main, [this, &v, vertex_code, fragment_code]() {
  770. Version::Ids async_ids;
  771. async_ids.main = glCreateProgram();
  772. async_ids.vert = glCreateShader(GL_VERTEX_SHADER);
  773. async_ids.frag = glCreateShader(GL_FRAGMENT_SHADER);
  774. LocalVector<const char *> async_strings_vertex;
  775. async_strings_vertex.push_back(vertex_code.ptr());
  776. LocalVector<const char *> async_strings_fragment;
  777. async_strings_fragment.push_back(fragment_code.ptr());
  778. _set_source(async_ids, async_strings_vertex, async_strings_fragment);
  779. glCompileShader(async_ids.vert);
  780. glCompileShader(async_ids.frag);
  781. if (_complete_compile(async_ids, true) && _complete_link(async_ids, &v.program_binary.format, &v.program_binary.data)) {
  782. glDeleteShader(async_ids.frag);
  783. glDeleteShader(async_ids.vert);
  784. glDeleteProgram(async_ids.main);
  785. v.program_binary.result_from_queue.set(1);
  786. } else {
  787. v.program_binary.result_from_queue.set(0);
  788. }
  789. });
  790. } else {
  791. // Synchronous compilation, or async. via native support
  792. v.ids.vert = glCreateShader(GL_VERTEX_SHADER);
  793. v.ids.frag = glCreateShader(GL_FRAGMENT_SHADER);
  794. _set_source(v.ids, strings_vertex, strings_fragment);
  795. v.program_binary.source = Version::ProgramBinary::SOURCE_LOCAL;
  796. v.compile_status = Version::COMPILE_STATUS_SOURCE_PROVIDED;
  797. }
  798. }
  799. if (cc) {
  800. cc->versions.insert(effective_version.version);
  801. }
  802. return &v;
  803. }
  804. void ShaderGLES3::_set_source(Version::Ids p_ids, const LocalVector<const char *> &p_vertex_strings, const LocalVector<const char *> &p_fragment_strings) const {
  805. glShaderSource(p_ids.vert, p_vertex_strings.size(), p_vertex_strings.ptr(), nullptr);
  806. glShaderSource(p_ids.frag, p_fragment_strings.size(), p_fragment_strings.ptr(), nullptr);
  807. }
  808. bool ShaderGLES3::_complete_compile(Version::Ids p_ids, bool p_retrievable) const {
  809. GLint status;
  810. glGetShaderiv(p_ids.vert, GL_COMPILE_STATUS, &status);
  811. if (status == GL_FALSE) {
  812. // error compiling
  813. GLsizei iloglen;
  814. glGetShaderiv(p_ids.vert, GL_INFO_LOG_LENGTH, &iloglen);
  815. if (iloglen < 0) {
  816. glDeleteShader(p_ids.frag);
  817. glDeleteShader(p_ids.vert);
  818. glDeleteProgram(p_ids.main);
  819. ERR_PRINT("Vertex shader compilation failed with empty log");
  820. } else {
  821. if (iloglen == 0) {
  822. iloglen = 4096; //buggy driver (Adreno 220+....)
  823. }
  824. char *ilogmem = (char *)memalloc(iloglen + 1);
  825. ilogmem[iloglen] = 0;
  826. glGetShaderInfoLog(p_ids.vert, iloglen, &iloglen, ilogmem);
  827. String err_string = get_shader_name() + ": Vertex Program Compilation Failed:\n";
  828. err_string += ilogmem;
  829. _display_error_with_code(err_string, p_ids.vert);
  830. ERR_PRINT(err_string.ascii().get_data());
  831. memfree(ilogmem);
  832. glDeleteShader(p_ids.frag);
  833. glDeleteShader(p_ids.vert);
  834. glDeleteProgram(p_ids.main);
  835. }
  836. return false;
  837. }
  838. glGetShaderiv(p_ids.frag, GL_COMPILE_STATUS, &status);
  839. if (status == GL_FALSE) {
  840. // error compiling
  841. GLsizei iloglen;
  842. glGetShaderiv(p_ids.frag, GL_INFO_LOG_LENGTH, &iloglen);
  843. if (iloglen < 0) {
  844. glDeleteShader(p_ids.frag);
  845. glDeleteShader(p_ids.vert);
  846. glDeleteProgram(p_ids.main);
  847. ERR_PRINT("Fragment shader compilation failed with empty log");
  848. } else {
  849. if (iloglen == 0) {
  850. iloglen = 4096; //buggy driver (Adreno 220+....)
  851. }
  852. char *ilogmem = (char *)memalloc(iloglen + 1);
  853. ilogmem[iloglen] = 0;
  854. glGetShaderInfoLog(p_ids.frag, iloglen, &iloglen, ilogmem);
  855. String err_string = get_shader_name() + ": Fragment Program Compilation Failed:\n";
  856. err_string += ilogmem;
  857. _display_error_with_code(err_string, p_ids.frag);
  858. ERR_PRINT(err_string.ascii().get_data());
  859. memfree(ilogmem);
  860. glDeleteShader(p_ids.frag);
  861. glDeleteShader(p_ids.vert);
  862. glDeleteProgram(p_ids.main);
  863. }
  864. return false;
  865. }
  866. glAttachShader(p_ids.main, p_ids.frag);
  867. glAttachShader(p_ids.main, p_ids.vert);
  868. // bind attributes before linking
  869. for (int i = 0; i < attribute_pair_count; i++) {
  870. glBindAttribLocation(p_ids.main, attribute_pairs[i].index, attribute_pairs[i].name);
  871. }
  872. //if feedback exists, set it up
  873. if (feedback_count) {
  874. Vector<const char *> feedback;
  875. for (int i = 0; i < feedback_count; i++) {
  876. if (feedbacks[i].conditional == -1 || (1 << feedbacks[i].conditional) & conditional_version.version) {
  877. //conditional for this feedback is enabled
  878. feedback.push_back(feedbacks[i].name);
  879. }
  880. }
  881. if (feedback.size()) {
  882. glTransformFeedbackVaryings(p_ids.main, feedback.size(), feedback.ptr(), GL_INTERLEAVED_ATTRIBS);
  883. }
  884. }
  885. if (p_retrievable) {
  886. glProgramParameteri(p_ids.main, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE);
  887. }
  888. glLinkProgram(p_ids.main);
  889. return true;
  890. }
  891. bool ShaderGLES3::_complete_link(Version::Ids p_ids, GLenum *r_program_format, PoolByteArray *r_program_binary) const {
  892. GLint status;
  893. glGetProgramiv(p_ids.main, GL_LINK_STATUS, &status);
  894. if (status == GL_FALSE) {
  895. // error linking
  896. GLsizei iloglen;
  897. glGetProgramiv(p_ids.main, GL_INFO_LOG_LENGTH, &iloglen);
  898. if (iloglen < 0) {
  899. glDeleteShader(p_ids.frag);
  900. glDeleteShader(p_ids.vert);
  901. glDeleteProgram(p_ids.main);
  902. ERR_FAIL_COND_V(iloglen < 0, false);
  903. }
  904. if (iloglen == 0) {
  905. iloglen = 4096; //buggy driver (Adreno 220+....)
  906. }
  907. char *ilogmem = (char *)Memory::alloc_static(iloglen + 1);
  908. ilogmem[iloglen] = 0;
  909. glGetProgramInfoLog(p_ids.main, iloglen, &iloglen, ilogmem);
  910. String err_string = get_shader_name() + ": Program LINK FAILED:\n";
  911. err_string += ilogmem;
  912. ERR_PRINT(err_string.ascii().get_data());
  913. Memory::free_static(ilogmem);
  914. glDeleteShader(p_ids.frag);
  915. glDeleteShader(p_ids.vert);
  916. glDeleteProgram(p_ids.main);
  917. return false;
  918. }
  919. if (r_program_binary) {
  920. GLint program_len;
  921. glGetProgramiv(p_ids.main, GL_PROGRAM_BINARY_LENGTH, &program_len);
  922. r_program_binary->resize(program_len);
  923. PoolByteArray::Write w = r_program_binary->write();
  924. glGetProgramBinary(p_ids.main, program_len, NULL, r_program_format, w.ptr());
  925. }
  926. return true;
  927. }
  928. void ShaderGLES3::_setup_uniforms(CustomCode *p_cc) const {
  929. //print_line("uniforms: ");
  930. for (int j = 0; j < uniform_count; j++) {
  931. version->uniform_location[j] = glGetUniformLocation(version->ids.main, uniform_names[j]);
  932. //print_line("uniform "+String(uniform_names[j])+" location "+itos(version->uniform_location[j]));
  933. }
  934. // set texture uniforms
  935. for (int i = 0; i < texunit_pair_count; i++) {
  936. GLint loc = glGetUniformLocation(version->ids.main, texunit_pairs[i].name);
  937. if (loc >= 0) {
  938. if (texunit_pairs[i].index < 0) {
  939. glUniform1i(loc, max_image_units + texunit_pairs[i].index); //negative, goes down
  940. } else {
  941. glUniform1i(loc, texunit_pairs[i].index);
  942. }
  943. }
  944. }
  945. // assign uniform block bind points
  946. for (int i = 0; i < ubo_count; i++) {
  947. GLint loc = glGetUniformBlockIndex(version->ids.main, ubo_pairs[i].name);
  948. if (loc >= 0)
  949. glUniformBlockBinding(version->ids.main, loc, ubo_pairs[i].index);
  950. }
  951. if (p_cc) {
  952. version->texture_uniform_locations.resize(p_cc->texture_uniforms.size());
  953. for (int i = 0; i < p_cc->texture_uniforms.size(); i++) {
  954. version->texture_uniform_locations.write[i] = glGetUniformLocation(version->ids.main, String(p_cc->texture_uniforms[i]).ascii().get_data());
  955. glUniform1i(version->texture_uniform_locations[i], i + base_material_tex_index);
  956. }
  957. }
  958. }
  959. void ShaderGLES3::_dispose_program(Version *p_version) {
  960. if (compile_queue) {
  961. if (p_version->compile_status == Version::COMPILE_STATUS_PROCESSING_AT_QUEUE) {
  962. compile_queue->cancel(p_version->ids.main);
  963. }
  964. }
  965. glDeleteShader(p_version->ids.vert);
  966. glDeleteShader(p_version->ids.frag);
  967. glDeleteProgram(p_version->ids.main);
  968. if (p_version->compiling_list.in_list()) {
  969. p_version->compiling_list.remove_from_list();
  970. active_compiles_count--;
  971. #ifdef DEV_ENABLED
  972. CRASH_COND(active_compiles_count == UINT32_MAX);
  973. #endif
  974. if (p_version->compile_status == Version::COMPILE_STATUS_COMPILING_VERTEX_AND_FRAGMENT) {
  975. active_compiles_count--;
  976. #ifdef DEV_ENABLED
  977. CRASH_COND(active_compiles_count == UINT32_MAX);
  978. #endif
  979. }
  980. _log_active_compiles();
  981. }
  982. p_version->compile_status = Version::COMPILE_STATUS_ERROR;
  983. }
  984. GLint ShaderGLES3::get_uniform_location(const String &p_name) const {
  985. ERR_FAIL_COND_V(!version, -1);
  986. return glGetUniformLocation(version->ids.main, p_name.ascii().get_data());
  987. }
  988. void ShaderGLES3::setup(const char **p_conditional_defines, int p_conditional_count, const char **p_uniform_names, int p_uniform_count, const AttributePair *p_attribute_pairs, int p_attribute_count, const TexUnitPair *p_texunit_pairs, int p_texunit_pair_count, const UBOPair *p_ubo_pairs, int p_ubo_pair_count, const Feedback *p_feedback, int p_feedback_count, const char *p_vertex_code, const char *p_fragment_code, int p_vertex_code_start, int p_fragment_code_start) {
  989. ERR_FAIL_COND(version);
  990. conditional_version.key = 0;
  991. new_conditional_version.key = 0;
  992. uniform_count = p_uniform_count;
  993. conditional_count = p_conditional_count;
  994. conditional_defines = p_conditional_defines;
  995. uniform_names = p_uniform_names;
  996. vertex_code = p_vertex_code;
  997. fragment_code = p_fragment_code;
  998. texunit_pairs = p_texunit_pairs;
  999. texunit_pair_count = p_texunit_pair_count;
  1000. vertex_code_start = p_vertex_code_start;
  1001. fragment_code_start = p_fragment_code_start;
  1002. attribute_pairs = p_attribute_pairs;
  1003. attribute_pair_count = p_attribute_count;
  1004. ubo_pairs = p_ubo_pairs;
  1005. ubo_count = p_ubo_pair_count;
  1006. feedbacks = p_feedback;
  1007. feedback_count = p_feedback_count;
  1008. //split vertex and shader code (thank you, shader compiler programmers from you know what company).
  1009. {
  1010. String globals_tag = "\nVERTEX_SHADER_GLOBALS";
  1011. String material_tag = "\nMATERIAL_UNIFORMS";
  1012. String code_tag = "\nVERTEX_SHADER_CODE";
  1013. String code = vertex_code;
  1014. int cpos = code.find(material_tag);
  1015. if (cpos == -1) {
  1016. vertex_code0 = code.ascii();
  1017. } else {
  1018. vertex_code0 = code.substr(0, cpos).ascii();
  1019. code = code.substr(cpos + material_tag.length(), code.length());
  1020. cpos = code.find(globals_tag);
  1021. if (cpos == -1) {
  1022. vertex_code1 = code.ascii();
  1023. } else {
  1024. vertex_code1 = code.substr(0, cpos).ascii();
  1025. String code2 = code.substr(cpos + globals_tag.length(), code.length());
  1026. cpos = code2.find(code_tag);
  1027. if (cpos == -1) {
  1028. vertex_code2 = code2.ascii();
  1029. } else {
  1030. vertex_code2 = code2.substr(0, cpos).ascii();
  1031. vertex_code3 = code2.substr(cpos + code_tag.length(), code2.length()).ascii();
  1032. }
  1033. }
  1034. }
  1035. }
  1036. {
  1037. String globals_tag = "\nFRAGMENT_SHADER_GLOBALS";
  1038. String material_tag = "\nMATERIAL_UNIFORMS";
  1039. String code_tag = "\nFRAGMENT_SHADER_CODE";
  1040. String light_code_tag = "\nLIGHT_SHADER_CODE";
  1041. String code = fragment_code;
  1042. int cpos = code.find(material_tag);
  1043. if (cpos == -1) {
  1044. fragment_code0 = code.ascii();
  1045. } else {
  1046. fragment_code0 = code.substr(0, cpos).ascii();
  1047. //print_line("CODE0:\n"+String(fragment_code0.get_data()));
  1048. code = code.substr(cpos + material_tag.length(), code.length());
  1049. cpos = code.find(globals_tag);
  1050. if (cpos == -1) {
  1051. fragment_code1 = code.ascii();
  1052. } else {
  1053. fragment_code1 = code.substr(0, cpos).ascii();
  1054. //print_line("CODE1:\n"+String(fragment_code1.get_data()));
  1055. String code2 = code.substr(cpos + globals_tag.length(), code.length());
  1056. cpos = code2.find(light_code_tag);
  1057. if (cpos == -1) {
  1058. fragment_code2 = code2.ascii();
  1059. } else {
  1060. fragment_code2 = code2.substr(0, cpos).ascii();
  1061. //print_line("CODE2:\n"+String(fragment_code2.get_data()));
  1062. String code3 = code2.substr(cpos + light_code_tag.length(), code2.length());
  1063. cpos = code3.find(code_tag);
  1064. if (cpos == -1) {
  1065. fragment_code3 = code3.ascii();
  1066. } else {
  1067. fragment_code3 = code3.substr(0, cpos).ascii();
  1068. //print_line("CODE3:\n"+String(fragment_code3.get_data()));
  1069. fragment_code4 = code3.substr(cpos + code_tag.length(), code3.length()).ascii();
  1070. //print_line("CODE4:\n"+String(fragment_code4.get_data()));
  1071. }
  1072. }
  1073. }
  1074. }
  1075. }
  1076. glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_image_units);
  1077. }
  1078. void ShaderGLES3::init_async_compilation() {
  1079. if (is_async_compilation_supported() && get_ubershader_flags_uniform() != -1) {
  1080. // Warm up the ubershader for the case of no custom code
  1081. new_conditional_version.code_version = 0;
  1082. _bind_ubershader(true);
  1083. }
  1084. }
  1085. bool ShaderGLES3::is_async_compilation_supported() {
  1086. return max_simultaneous_compiles > 0 && (compile_queue || parallel_compile_supported);
  1087. }
  1088. void ShaderGLES3::finish() {
  1089. const VersionKey *V = nullptr;
  1090. while ((V = version_map.next(V))) {
  1091. Version &v = version_map[*V];
  1092. _dispose_program(&v);
  1093. memdelete_arr(v.uniform_location);
  1094. }
  1095. ERR_FAIL_COND(versions_compiling.first());
  1096. ERR_FAIL_COND(active_compiles_count != 0);
  1097. }
  1098. void ShaderGLES3::clear_caches() {
  1099. const VersionKey *V = nullptr;
  1100. while ((V = version_map.next(V))) {
  1101. Version &v = version_map[*V];
  1102. _dispose_program(&v);
  1103. memdelete_arr(v.uniform_location);
  1104. }
  1105. ERR_FAIL_COND(versions_compiling.first());
  1106. ERR_FAIL_COND(active_compiles_count != 0);
  1107. version_map.clear();
  1108. custom_code_map.clear();
  1109. version = nullptr;
  1110. last_custom_code = 1;
  1111. }
  1112. uint32_t ShaderGLES3::create_custom_shader() {
  1113. custom_code_map[last_custom_code] = CustomCode();
  1114. custom_code_map[last_custom_code].version = 1;
  1115. return last_custom_code++;
  1116. }
  1117. void ShaderGLES3::set_custom_shader_code(uint32_t p_code_id, const String &p_vertex, const String &p_vertex_globals, const String &p_fragment, const String &p_light, const String &p_fragment_globals, const String &p_uniforms, const Vector<StringName> &p_texture_uniforms, const Vector<CharString> &p_custom_defines, AsyncMode p_async_mode) {
  1118. ERR_FAIL_COND(!custom_code_map.has(p_code_id));
  1119. CustomCode *cc = &custom_code_map[p_code_id];
  1120. cc->vertex = p_vertex;
  1121. cc->vertex_globals = p_vertex_globals;
  1122. cc->fragment = p_fragment;
  1123. cc->fragment_globals = p_fragment_globals;
  1124. cc->light = p_light;
  1125. cc->texture_uniforms = p_texture_uniforms;
  1126. cc->uniforms = p_uniforms;
  1127. cc->custom_defines = p_custom_defines;
  1128. cc->async_mode = p_async_mode;
  1129. cc->version++;
  1130. if (p_async_mode == ASYNC_MODE_VISIBLE && is_async_compilation_supported() && get_ubershader_flags_uniform() != -1) {
  1131. // Warm up the ubershader for this custom code
  1132. new_conditional_version.code_version = p_code_id;
  1133. _bind_ubershader(true);
  1134. }
  1135. }
  1136. void ShaderGLES3::set_custom_shader(uint32_t p_code_id) {
  1137. new_conditional_version.code_version = p_code_id;
  1138. }
  1139. void ShaderGLES3::free_custom_shader(uint32_t p_code_id) {
  1140. ERR_FAIL_COND(!custom_code_map.has(p_code_id));
  1141. if (conditional_version.code_version == p_code_id) {
  1142. conditional_version.code_version = 0; //do not keep using a version that is going away
  1143. unbind();
  1144. }
  1145. VersionKey key;
  1146. key.code_version = p_code_id;
  1147. for (Set<uint32_t>::Element *E = custom_code_map[p_code_id].versions.front(); E; E = E->next()) {
  1148. key.version = E->get();
  1149. ERR_CONTINUE(!version_map.has(key));
  1150. Version &v = version_map[key];
  1151. _dispose_program(&v);
  1152. memdelete_arr(v.uniform_location);
  1153. version_map.erase(key);
  1154. }
  1155. custom_code_map.erase(p_code_id);
  1156. }
  1157. void ShaderGLES3::set_base_material_tex_index(int p_idx) {
  1158. base_material_tex_index = p_idx;
  1159. }
  1160. ShaderGLES3::ShaderGLES3() {
  1161. version = nullptr;
  1162. last_custom_code = 1;
  1163. base_material_tex_index = 0;
  1164. }
  1165. ShaderGLES3::~ShaderGLES3() {
  1166. finish();
  1167. }