shader_gles3.cpp 44 KB

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