shader_rd.cpp 23 KB

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