shader_gles3.cpp 45 KB

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