shader_rd.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /**************************************************************************/
  2. /* shader_rd.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_rd.h"
  31. #include "core/io/compression.h"
  32. #include "core/io/dir_access.h"
  33. #include "core/io/file_access.h"
  34. #include "core/version.h"
  35. #include "renderer_compositor_rd.h"
  36. #include "servers/rendering/rendering_device.h"
  37. #include "thirdparty/misc/smolv.h"
  38. void ShaderRD::_add_stage(const char *p_code, StageType p_stage_type) {
  39. Vector<String> lines = String(p_code).split("\n");
  40. String text;
  41. for (int i = 0; i < lines.size(); i++) {
  42. String l = lines[i];
  43. bool push_chunk = false;
  44. StageTemplate::Chunk chunk;
  45. if (l.begins_with("#VERSION_DEFINES")) {
  46. chunk.type = StageTemplate::Chunk::TYPE_VERSION_DEFINES;
  47. push_chunk = true;
  48. } else if (l.begins_with("#GLOBALS")) {
  49. switch (p_stage_type) {
  50. case STAGE_TYPE_VERTEX:
  51. chunk.type = StageTemplate::Chunk::TYPE_VERTEX_GLOBALS;
  52. break;
  53. case STAGE_TYPE_FRAGMENT:
  54. chunk.type = StageTemplate::Chunk::TYPE_FRAGMENT_GLOBALS;
  55. break;
  56. case STAGE_TYPE_COMPUTE:
  57. chunk.type = StageTemplate::Chunk::TYPE_COMPUTE_GLOBALS;
  58. break;
  59. default: {
  60. }
  61. }
  62. push_chunk = true;
  63. } else if (l.begins_with("#MATERIAL_UNIFORMS")) {
  64. chunk.type = StageTemplate::Chunk::TYPE_MATERIAL_UNIFORMS;
  65. push_chunk = true;
  66. } else if (l.begins_with("#CODE")) {
  67. chunk.type = StageTemplate::Chunk::TYPE_CODE;
  68. push_chunk = true;
  69. chunk.code = l.replace_first("#CODE", String()).replace(":", "").strip_edges().to_upper();
  70. } else {
  71. text += l + "\n";
  72. }
  73. if (push_chunk) {
  74. if (!text.is_empty()) {
  75. StageTemplate::Chunk text_chunk;
  76. text_chunk.type = StageTemplate::Chunk::TYPE_TEXT;
  77. text_chunk.text = text.utf8();
  78. stage_templates[p_stage_type].chunks.push_back(text_chunk);
  79. text = String();
  80. }
  81. stage_templates[p_stage_type].chunks.push_back(chunk);
  82. }
  83. }
  84. if (!text.is_empty()) {
  85. StageTemplate::Chunk text_chunk;
  86. text_chunk.type = StageTemplate::Chunk::TYPE_TEXT;
  87. text_chunk.text = text.utf8();
  88. stage_templates[p_stage_type].chunks.push_back(text_chunk);
  89. text = String();
  90. }
  91. }
  92. void ShaderRD::setup(const char *p_vertex_code, const char *p_fragment_code, const char *p_compute_code, const char *p_name) {
  93. name = p_name;
  94. if (p_compute_code) {
  95. _add_stage(p_compute_code, STAGE_TYPE_COMPUTE);
  96. is_compute = true;
  97. } else {
  98. is_compute = false;
  99. if (p_vertex_code) {
  100. _add_stage(p_vertex_code, STAGE_TYPE_VERTEX);
  101. }
  102. if (p_fragment_code) {
  103. _add_stage(p_fragment_code, STAGE_TYPE_FRAGMENT);
  104. }
  105. }
  106. StringBuilder tohash;
  107. tohash.append("[GodotVersionNumber]");
  108. tohash.append(VERSION_NUMBER);
  109. tohash.append("[GodotVersionHash]");
  110. tohash.append(VERSION_HASH);
  111. tohash.append("[SpirvCacheKey]");
  112. tohash.append(RenderingDevice::get_singleton()->shader_get_spirv_cache_key());
  113. tohash.append("[BinaryCacheKey]");
  114. tohash.append(RenderingDevice::get_singleton()->shader_get_binary_cache_key());
  115. tohash.append("[Vertex]");
  116. tohash.append(p_vertex_code ? p_vertex_code : "");
  117. tohash.append("[Fragment]");
  118. tohash.append(p_fragment_code ? p_fragment_code : "");
  119. tohash.append("[Compute]");
  120. tohash.append(p_compute_code ? p_compute_code : "");
  121. base_sha256 = tohash.as_string().sha256_text();
  122. }
  123. RID ShaderRD::version_create() {
  124. //initialize() was never called
  125. ERR_FAIL_COND_V(variant_defines.size() == 0, RID());
  126. Version version;
  127. version.dirty = true;
  128. version.valid = false;
  129. version.initialize_needed = true;
  130. version.variants = nullptr;
  131. return version_owner.make_rid(version);
  132. }
  133. void ShaderRD::_clear_version(Version *p_version) {
  134. //clear versions if they exist
  135. if (p_version->variants) {
  136. for (int i = 0; i < variant_defines.size(); i++) {
  137. if (variants_enabled[i]) {
  138. RD::get_singleton()->free(p_version->variants[i]);
  139. }
  140. }
  141. memdelete_arr(p_version->variants);
  142. if (p_version->variant_data) {
  143. memdelete_arr(p_version->variant_data);
  144. }
  145. p_version->variants = nullptr;
  146. }
  147. }
  148. void ShaderRD::_build_variant_code(StringBuilder &builder, uint32_t p_variant, const Version *p_version, const StageTemplate &p_template) {
  149. for (const StageTemplate::Chunk &chunk : p_template.chunks) {
  150. switch (chunk.type) {
  151. case StageTemplate::Chunk::TYPE_VERSION_DEFINES: {
  152. builder.append("\n"); //make sure defines begin at newline
  153. builder.append(general_defines.get_data());
  154. builder.append(variant_defines[p_variant].get_data());
  155. for (int j = 0; j < p_version->custom_defines.size(); j++) {
  156. builder.append(p_version->custom_defines[j].get_data());
  157. }
  158. builder.append("\n"); //make sure defines begin at newline
  159. if (p_version->uniforms.size()) {
  160. builder.append("#define MATERIAL_UNIFORMS_USED\n");
  161. }
  162. for (const KeyValue<StringName, CharString> &E : p_version->code_sections) {
  163. builder.append(String("#define ") + String(E.key) + "_CODE_USED\n");
  164. }
  165. #if defined(MACOS_ENABLED) || defined(IOS_ENABLED)
  166. builder.append("#define MOLTENVK_USED\n");
  167. #endif
  168. builder.append(String("#define RENDER_DRIVER_") + OS::get_singleton()->get_current_rendering_driver_name().to_upper() + "\n");
  169. } break;
  170. case StageTemplate::Chunk::TYPE_MATERIAL_UNIFORMS: {
  171. builder.append(p_version->uniforms.get_data()); //uniforms (same for vertex and fragment)
  172. } break;
  173. case StageTemplate::Chunk::TYPE_VERTEX_GLOBALS: {
  174. builder.append(p_version->vertex_globals.get_data()); // vertex globals
  175. } break;
  176. case StageTemplate::Chunk::TYPE_FRAGMENT_GLOBALS: {
  177. builder.append(p_version->fragment_globals.get_data()); // fragment globals
  178. } break;
  179. case StageTemplate::Chunk::TYPE_COMPUTE_GLOBALS: {
  180. builder.append(p_version->compute_globals.get_data()); // compute globals
  181. } break;
  182. case StageTemplate::Chunk::TYPE_CODE: {
  183. if (p_version->code_sections.has(chunk.code)) {
  184. builder.append(p_version->code_sections[chunk.code].get_data());
  185. }
  186. } break;
  187. case StageTemplate::Chunk::TYPE_TEXT: {
  188. builder.append(chunk.text.get_data());
  189. } break;
  190. }
  191. }
  192. }
  193. void ShaderRD::_compile_variant(uint32_t p_variant, Version *p_version) {
  194. if (!variants_enabled[p_variant]) {
  195. return; //variant is disabled, return
  196. }
  197. Vector<RD::ShaderStageSPIRVData> stages;
  198. String error;
  199. String current_source;
  200. RD::ShaderStage current_stage = RD::SHADER_STAGE_VERTEX;
  201. bool build_ok = true;
  202. if (!is_compute) {
  203. //vertex stage
  204. StringBuilder builder;
  205. _build_variant_code(builder, p_variant, p_version, stage_templates[STAGE_TYPE_VERTEX]);
  206. current_source = builder.as_string();
  207. RD::ShaderStageSPIRVData stage;
  208. stage.spir_v = RD::get_singleton()->shader_compile_spirv_from_source(RD::SHADER_STAGE_VERTEX, current_source, RD::SHADER_LANGUAGE_GLSL, &error);
  209. if (stage.spir_v.size() == 0) {
  210. build_ok = false;
  211. } else {
  212. stage.shader_stage = RD::SHADER_STAGE_VERTEX;
  213. stages.push_back(stage);
  214. }
  215. }
  216. if (!is_compute && build_ok) {
  217. //fragment stage
  218. current_stage = RD::SHADER_STAGE_FRAGMENT;
  219. StringBuilder builder;
  220. _build_variant_code(builder, p_variant, p_version, stage_templates[STAGE_TYPE_FRAGMENT]);
  221. current_source = builder.as_string();
  222. RD::ShaderStageSPIRVData stage;
  223. stage.spir_v = RD::get_singleton()->shader_compile_spirv_from_source(RD::SHADER_STAGE_FRAGMENT, current_source, RD::SHADER_LANGUAGE_GLSL, &error);
  224. if (stage.spir_v.size() == 0) {
  225. build_ok = false;
  226. } else {
  227. stage.shader_stage = RD::SHADER_STAGE_FRAGMENT;
  228. stages.push_back(stage);
  229. }
  230. }
  231. if (is_compute) {
  232. //compute stage
  233. current_stage = RD::SHADER_STAGE_COMPUTE;
  234. StringBuilder builder;
  235. _build_variant_code(builder, p_variant, p_version, stage_templates[STAGE_TYPE_COMPUTE]);
  236. current_source = builder.as_string();
  237. RD::ShaderStageSPIRVData stage;
  238. stage.spir_v = RD::get_singleton()->shader_compile_spirv_from_source(RD::SHADER_STAGE_COMPUTE, current_source, RD::SHADER_LANGUAGE_GLSL, &error);
  239. if (stage.spir_v.size() == 0) {
  240. build_ok = false;
  241. } else {
  242. stage.shader_stage = RD::SHADER_STAGE_COMPUTE;
  243. stages.push_back(stage);
  244. }
  245. }
  246. if (!build_ok) {
  247. MutexLock lock(variant_set_mutex); //properly print the errors
  248. ERR_PRINT("Error compiling " + String(current_stage == RD::SHADER_STAGE_COMPUTE ? "Compute " : (current_stage == RD::SHADER_STAGE_VERTEX ? "Vertex" : "Fragment")) + " shader, variant #" + itos(p_variant) + " (" + variant_defines[p_variant].get_data() + ").");
  249. ERR_PRINT(error);
  250. #ifdef DEBUG_ENABLED
  251. ERR_PRINT("code:\n" + current_source.get_with_code_lines());
  252. #endif
  253. return;
  254. }
  255. Vector<uint8_t> shader_data = RD::get_singleton()->shader_compile_binary_from_spirv(stages, name + ":" + itos(p_variant));
  256. ERR_FAIL_COND(shader_data.size() == 0);
  257. RID shader = RD::get_singleton()->shader_create_from_bytecode(shader_data);
  258. {
  259. MutexLock lock(variant_set_mutex);
  260. p_version->variants[p_variant] = shader;
  261. p_version->variant_data[p_variant] = shader_data;
  262. }
  263. }
  264. RS::ShaderNativeSourceCode ShaderRD::version_get_native_source_code(RID p_version) {
  265. Version *version = version_owner.get_or_null(p_version);
  266. RS::ShaderNativeSourceCode source_code;
  267. ERR_FAIL_COND_V(!version, source_code);
  268. source_code.versions.resize(variant_defines.size());
  269. for (int i = 0; i < source_code.versions.size(); i++) {
  270. if (!is_compute) {
  271. //vertex stage
  272. StringBuilder builder;
  273. _build_variant_code(builder, i, version, stage_templates[STAGE_TYPE_VERTEX]);
  274. RS::ShaderNativeSourceCode::Version::Stage stage;
  275. stage.name = "vertex";
  276. stage.code = builder.as_string();
  277. source_code.versions.write[i].stages.push_back(stage);
  278. }
  279. if (!is_compute) {
  280. //fragment stage
  281. StringBuilder builder;
  282. _build_variant_code(builder, i, version, stage_templates[STAGE_TYPE_FRAGMENT]);
  283. RS::ShaderNativeSourceCode::Version::Stage stage;
  284. stage.name = "fragment";
  285. stage.code = builder.as_string();
  286. source_code.versions.write[i].stages.push_back(stage);
  287. }
  288. if (is_compute) {
  289. //compute stage
  290. StringBuilder builder;
  291. _build_variant_code(builder, i, version, stage_templates[STAGE_TYPE_COMPUTE]);
  292. RS::ShaderNativeSourceCode::Version::Stage stage;
  293. stage.name = "compute";
  294. stage.code = builder.as_string();
  295. source_code.versions.write[i].stages.push_back(stage);
  296. }
  297. }
  298. return source_code;
  299. }
  300. String ShaderRD::_version_get_sha1(Version *p_version) const {
  301. StringBuilder hash_build;
  302. hash_build.append("[uniforms]");
  303. hash_build.append(p_version->uniforms.get_data());
  304. hash_build.append("[vertex_globals]");
  305. hash_build.append(p_version->vertex_globals.get_data());
  306. hash_build.append("[fragment_globals]");
  307. hash_build.append(p_version->fragment_globals.get_data());
  308. hash_build.append("[compute_globals]");
  309. hash_build.append(p_version->compute_globals.get_data());
  310. Vector<StringName> code_sections;
  311. for (const KeyValue<StringName, CharString> &E : p_version->code_sections) {
  312. code_sections.push_back(E.key);
  313. }
  314. code_sections.sort_custom<StringName::AlphCompare>();
  315. for (int i = 0; i < code_sections.size(); i++) {
  316. hash_build.append(String("[code:") + String(code_sections[i]) + "]");
  317. hash_build.append(p_version->code_sections[code_sections[i]].get_data());
  318. }
  319. for (int i = 0; i < p_version->custom_defines.size(); i++) {
  320. hash_build.append("[custom_defines:" + itos(i) + "]");
  321. hash_build.append(p_version->custom_defines[i].get_data());
  322. }
  323. return hash_build.as_string().sha1_text();
  324. }
  325. static const char *shader_file_header = "GDSC";
  326. static const uint32_t cache_file_version = 2;
  327. bool ShaderRD::_load_from_cache(Version *p_version) {
  328. String sha1 = _version_get_sha1(p_version);
  329. String path = shader_cache_dir.path_join(name).path_join(base_sha256).path_join(sha1) + ".cache";
  330. Ref<FileAccess> f = FileAccess::open(path, FileAccess::READ);
  331. if (f.is_null()) {
  332. return false;
  333. }
  334. char header[5] = { 0, 0, 0, 0, 0 };
  335. f->get_buffer((uint8_t *)header, 4);
  336. ERR_FAIL_COND_V(header != String(shader_file_header), false);
  337. uint32_t file_version = f->get_32();
  338. if (file_version != cache_file_version) {
  339. return false; // wrong version
  340. }
  341. uint32_t variant_count = f->get_32();
  342. ERR_FAIL_COND_V(variant_count != (uint32_t)variant_defines.size(), false); //should not happen but check
  343. for (uint32_t i = 0; i < variant_count; i++) {
  344. uint32_t variant_size = f->get_32();
  345. ERR_FAIL_COND_V(variant_size == 0 && variants_enabled[i], false);
  346. if (!variants_enabled[i]) {
  347. continue;
  348. }
  349. Vector<uint8_t> variant_bytes;
  350. variant_bytes.resize(variant_size);
  351. uint32_t br = f->get_buffer(variant_bytes.ptrw(), variant_size);
  352. ERR_FAIL_COND_V(br != variant_size, false);
  353. p_version->variant_data[i] = variant_bytes;
  354. }
  355. for (uint32_t i = 0; i < variant_count; i++) {
  356. if (!variants_enabled[i]) {
  357. MutexLock lock(variant_set_mutex);
  358. p_version->variants[i] = RID();
  359. continue;
  360. }
  361. RID shader = RD::get_singleton()->shader_create_from_bytecode(p_version->variant_data[i]);
  362. if (shader.is_null()) {
  363. for (uint32_t j = 0; j < i; j++) {
  364. RD::get_singleton()->free(p_version->variants[i]);
  365. }
  366. ERR_FAIL_COND_V(shader.is_null(), false);
  367. }
  368. {
  369. MutexLock lock(variant_set_mutex);
  370. p_version->variants[i] = shader;
  371. }
  372. }
  373. memdelete_arr(p_version->variant_data); //clear stages
  374. p_version->variant_data = nullptr;
  375. p_version->valid = true;
  376. return true;
  377. }
  378. void ShaderRD::_save_to_cache(Version *p_version) {
  379. String sha1 = _version_get_sha1(p_version);
  380. String path = shader_cache_dir.path_join(name).path_join(base_sha256).path_join(sha1) + ".cache";
  381. Ref<FileAccess> f = FileAccess::open(path, FileAccess::WRITE);
  382. ERR_FAIL_COND(f.is_null());
  383. f->store_buffer((const uint8_t *)shader_file_header, 4);
  384. f->store_32(cache_file_version); //file version
  385. uint32_t variant_count = variant_defines.size();
  386. f->store_32(variant_count); //variant count
  387. for (uint32_t i = 0; i < variant_count; i++) {
  388. f->store_32(p_version->variant_data[i].size()); //stage count
  389. f->store_buffer(p_version->variant_data[i].ptr(), p_version->variant_data[i].size());
  390. }
  391. }
  392. void ShaderRD::_compile_version(Version *p_version) {
  393. _clear_version(p_version);
  394. p_version->valid = false;
  395. p_version->dirty = false;
  396. p_version->variants = memnew_arr(RID, variant_defines.size());
  397. typedef Vector<uint8_t> ShaderStageData;
  398. p_version->variant_data = memnew_arr(ShaderStageData, variant_defines.size());
  399. if (shader_cache_dir_valid) {
  400. if (_load_from_cache(p_version)) {
  401. return;
  402. }
  403. }
  404. #if 1
  405. WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &ShaderRD::_compile_variant, p_version, variant_defines.size(), -1, true, SNAME("ShaderCompilation"));
  406. WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
  407. #else
  408. for (int i = 0; i < variant_defines.size(); i++) {
  409. _compile_variant(i, p_version);
  410. }
  411. #endif
  412. bool all_valid = true;
  413. for (int i = 0; i < variant_defines.size(); i++) {
  414. if (!variants_enabled[i]) {
  415. continue; //disabled
  416. }
  417. if (p_version->variants[i].is_null()) {
  418. all_valid = false;
  419. break;
  420. }
  421. }
  422. if (!all_valid) {
  423. //clear versions if they exist
  424. for (int i = 0; i < variant_defines.size(); i++) {
  425. if (!variants_enabled[i]) {
  426. continue; //disabled
  427. }
  428. if (!p_version->variants[i].is_null()) {
  429. RD::get_singleton()->free(p_version->variants[i]);
  430. }
  431. }
  432. memdelete_arr(p_version->variants);
  433. if (p_version->variant_data) {
  434. memdelete_arr(p_version->variant_data);
  435. }
  436. p_version->variants = nullptr;
  437. p_version->variant_data = nullptr;
  438. return;
  439. } else if (shader_cache_dir_valid) {
  440. //save shader cache
  441. _save_to_cache(p_version);
  442. }
  443. memdelete_arr(p_version->variant_data); //clear stages
  444. p_version->variant_data = nullptr;
  445. p_version->valid = true;
  446. }
  447. void ShaderRD::version_set_code(RID p_version, const HashMap<String, String> &p_code, const String &p_uniforms, const String &p_vertex_globals, const String &p_fragment_globals, const Vector<String> &p_custom_defines) {
  448. ERR_FAIL_COND(is_compute);
  449. Version *version = version_owner.get_or_null(p_version);
  450. ERR_FAIL_COND(!version);
  451. version->vertex_globals = p_vertex_globals.utf8();
  452. version->fragment_globals = p_fragment_globals.utf8();
  453. version->uniforms = p_uniforms.utf8();
  454. version->code_sections.clear();
  455. for (const KeyValue<String, String> &E : p_code) {
  456. version->code_sections[StringName(E.key.to_upper())] = E.value.utf8();
  457. }
  458. version->custom_defines.clear();
  459. for (int i = 0; i < p_custom_defines.size(); i++) {
  460. version->custom_defines.push_back(p_custom_defines[i].utf8());
  461. }
  462. version->dirty = true;
  463. if (version->initialize_needed) {
  464. _compile_version(version);
  465. version->initialize_needed = false;
  466. }
  467. }
  468. void ShaderRD::version_set_compute_code(RID p_version, const HashMap<String, String> &p_code, const String &p_uniforms, const String &p_compute_globals, const Vector<String> &p_custom_defines) {
  469. ERR_FAIL_COND(!is_compute);
  470. Version *version = version_owner.get_or_null(p_version);
  471. ERR_FAIL_COND(!version);
  472. version->compute_globals = p_compute_globals.utf8();
  473. version->uniforms = p_uniforms.utf8();
  474. version->code_sections.clear();
  475. for (const KeyValue<String, String> &E : p_code) {
  476. version->code_sections[StringName(E.key.to_upper())] = E.value.utf8();
  477. }
  478. version->custom_defines.clear();
  479. for (int i = 0; i < p_custom_defines.size(); i++) {
  480. version->custom_defines.push_back(p_custom_defines[i].utf8());
  481. }
  482. version->dirty = true;
  483. if (version->initialize_needed) {
  484. _compile_version(version);
  485. version->initialize_needed = false;
  486. }
  487. }
  488. bool ShaderRD::version_is_valid(RID p_version) {
  489. Version *version = version_owner.get_or_null(p_version);
  490. ERR_FAIL_COND_V(!version, false);
  491. if (version->dirty) {
  492. _compile_version(version);
  493. }
  494. return version->valid;
  495. }
  496. bool ShaderRD::version_free(RID p_version) {
  497. if (version_owner.owns(p_version)) {
  498. Version *version = version_owner.get_or_null(p_version);
  499. _clear_version(version);
  500. version_owner.free(p_version);
  501. } else {
  502. return false;
  503. }
  504. return true;
  505. }
  506. void ShaderRD::set_variant_enabled(int p_variant, bool p_enabled) {
  507. ERR_FAIL_COND(version_owner.get_rid_count() > 0); //versions exist
  508. ERR_FAIL_INDEX(p_variant, variants_enabled.size());
  509. variants_enabled.write[p_variant] = p_enabled;
  510. }
  511. bool ShaderRD::is_variant_enabled(int p_variant) const {
  512. ERR_FAIL_INDEX_V(p_variant, variants_enabled.size(), false);
  513. return variants_enabled[p_variant];
  514. }
  515. bool ShaderRD::shader_cache_cleanup_on_start = false;
  516. ShaderRD::ShaderRD() {
  517. // Do not feel forced to use this, in most cases it makes little to no difference.
  518. bool use_32_threads = false;
  519. if (RD::get_singleton()->get_device_vendor_name() == "NVIDIA") {
  520. use_32_threads = true;
  521. }
  522. String base_compute_define_text;
  523. if (use_32_threads) {
  524. base_compute_define_text = "\n#define NATIVE_LOCAL_GROUP_SIZE 32\n#define NATIVE_LOCAL_SIZE_2D_X 8\n#define NATIVE_LOCAL_SIZE_2D_Y 4\n";
  525. } else {
  526. base_compute_define_text = "\n#define NATIVE_LOCAL_GROUP_SIZE 64\n#define NATIVE_LOCAL_SIZE_2D_X 8\n#define NATIVE_LOCAL_SIZE_2D_Y 8\n";
  527. }
  528. base_compute_defines = base_compute_define_text.ascii();
  529. }
  530. void ShaderRD::initialize(const Vector<String> &p_variant_defines, const String &p_general_defines) {
  531. ERR_FAIL_COND(variant_defines.size());
  532. ERR_FAIL_COND(p_variant_defines.size() == 0);
  533. general_defines = p_general_defines.utf8();
  534. for (int i = 0; i < p_variant_defines.size(); i++) {
  535. variant_defines.push_back(p_variant_defines[i].utf8());
  536. variants_enabled.push_back(true);
  537. }
  538. if (!shader_cache_dir.is_empty()) {
  539. StringBuilder hash_build;
  540. hash_build.append("[base_hash]");
  541. hash_build.append(base_sha256);
  542. hash_build.append("[general_defines]");
  543. hash_build.append(general_defines.get_data());
  544. for (int i = 0; i < variant_defines.size(); i++) {
  545. hash_build.append("[variant_defines:" + itos(i) + "]");
  546. hash_build.append(variant_defines[i].get_data());
  547. }
  548. base_sha256 = hash_build.as_string().sha256_text();
  549. Ref<DirAccess> d = DirAccess::open(shader_cache_dir);
  550. ERR_FAIL_COND(d.is_null());
  551. if (d->change_dir(name) != OK) {
  552. Error err = d->make_dir(name);
  553. ERR_FAIL_COND(err != OK);
  554. d->change_dir(name);
  555. }
  556. //erase other versions?
  557. if (shader_cache_cleanup_on_start) {
  558. }
  559. //
  560. if (d->change_dir(base_sha256) != OK) {
  561. Error err = d->make_dir(base_sha256);
  562. ERR_FAIL_COND(err != OK);
  563. }
  564. shader_cache_dir_valid = true;
  565. print_verbose("Shader '" + name + "' SHA256: " + base_sha256);
  566. }
  567. }
  568. void ShaderRD::set_shader_cache_dir(const String &p_dir) {
  569. shader_cache_dir = p_dir;
  570. }
  571. void ShaderRD::set_shader_cache_save_compressed(bool p_enable) {
  572. shader_cache_save_compressed = p_enable;
  573. }
  574. void ShaderRD::set_shader_cache_save_compressed_zstd(bool p_enable) {
  575. shader_cache_save_compressed_zstd = p_enable;
  576. }
  577. void ShaderRD::set_shader_cache_save_debug(bool p_enable) {
  578. shader_cache_save_debug = p_enable;
  579. }
  580. String ShaderRD::shader_cache_dir;
  581. bool ShaderRD::shader_cache_save_compressed = true;
  582. bool ShaderRD::shader_cache_save_compressed_zstd = true;
  583. bool ShaderRD::shader_cache_save_debug = true;
  584. ShaderRD::~ShaderRD() {
  585. List<RID> remaining;
  586. version_owner.get_owned_list(&remaining);
  587. if (remaining.size()) {
  588. ERR_PRINT(itos(remaining.size()) + " shaders of type " + name + " were never freed");
  589. while (remaining.size()) {
  590. version_free(remaining.front()->get());
  591. remaining.pop_front();
  592. }
  593. }
  594. }