shader_rd.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /*************************************************************************/
  2. /* shader_rd.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 != String()) {
  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 != String()) {
  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 (Map<StringName, CharString>::Element *E = p_version->code_sections.front(); E; E = E->next()) {
  159. builder.append(String("#define ") + String(E->key()) + "_CODE_USED\n");
  160. }
  161. } break;
  162. case StageTemplate::Chunk::TYPE_MATERIAL_UNIFORMS: {
  163. builder.append(p_version->uniforms.get_data()); //uniforms (same for vertex and fragment)
  164. } break;
  165. case StageTemplate::Chunk::TYPE_VERTEX_GLOBALS: {
  166. builder.append(p_version->vertex_globals.get_data()); // vertex globals
  167. } break;
  168. case StageTemplate::Chunk::TYPE_FRAGMENT_GLOBALS: {
  169. builder.append(p_version->fragment_globals.get_data()); // fragment globals
  170. } break;
  171. case StageTemplate::Chunk::TYPE_COMPUTE_GLOBALS: {
  172. builder.append(p_version->compute_globals.get_data()); // compute globals
  173. } break;
  174. case StageTemplate::Chunk::TYPE_CODE: {
  175. if (p_version->code_sections.has(chunk.code)) {
  176. builder.append(p_version->code_sections[chunk.code].get_data());
  177. }
  178. } break;
  179. case StageTemplate::Chunk::TYPE_TEXT: {
  180. builder.append(chunk.text.get_data());
  181. } break;
  182. }
  183. }
  184. }
  185. void ShaderRD::_compile_variant(uint32_t p_variant, Version *p_version) {
  186. if (!variants_enabled[p_variant]) {
  187. return; //variant is disabled, return
  188. }
  189. Vector<RD::ShaderStageSPIRVData> stages;
  190. String error;
  191. String current_source;
  192. RD::ShaderStage current_stage = RD::SHADER_STAGE_VERTEX;
  193. bool build_ok = true;
  194. if (!is_compute) {
  195. //vertex stage
  196. StringBuilder builder;
  197. _build_variant_code(builder, p_variant, p_version, stage_templates[STAGE_TYPE_VERTEX]);
  198. current_source = builder.as_string();
  199. RD::ShaderStageSPIRVData stage;
  200. stage.spir_v = RD::get_singleton()->shader_compile_spirv_from_source(RD::SHADER_STAGE_VERTEX, current_source, RD::SHADER_LANGUAGE_GLSL, &error);
  201. if (stage.spir_v.size() == 0) {
  202. build_ok = false;
  203. } else {
  204. stage.shader_stage = RD::SHADER_STAGE_VERTEX;
  205. stages.push_back(stage);
  206. }
  207. }
  208. if (!is_compute && build_ok) {
  209. //fragment stage
  210. current_stage = RD::SHADER_STAGE_FRAGMENT;
  211. StringBuilder builder;
  212. _build_variant_code(builder, p_variant, p_version, stage_templates[STAGE_TYPE_FRAGMENT]);
  213. current_source = builder.as_string();
  214. RD::ShaderStageSPIRVData stage;
  215. stage.spir_v = RD::get_singleton()->shader_compile_spirv_from_source(RD::SHADER_STAGE_FRAGMENT, current_source, RD::SHADER_LANGUAGE_GLSL, &error);
  216. if (stage.spir_v.size() == 0) {
  217. build_ok = false;
  218. } else {
  219. stage.shader_stage = RD::SHADER_STAGE_FRAGMENT;
  220. stages.push_back(stage);
  221. }
  222. }
  223. if (is_compute) {
  224. //compute stage
  225. current_stage = RD::SHADER_STAGE_COMPUTE;
  226. StringBuilder builder;
  227. _build_variant_code(builder, p_variant, p_version, stage_templates[STAGE_TYPE_COMPUTE]);
  228. current_source = builder.as_string();
  229. RD::ShaderStageSPIRVData stage;
  230. stage.spir_v = RD::get_singleton()->shader_compile_spirv_from_source(RD::SHADER_STAGE_COMPUTE, current_source, RD::SHADER_LANGUAGE_GLSL, &error);
  231. if (stage.spir_v.size() == 0) {
  232. build_ok = false;
  233. } else {
  234. stage.shader_stage = RD::SHADER_STAGE_COMPUTE;
  235. stages.push_back(stage);
  236. }
  237. }
  238. if (!build_ok) {
  239. MutexLock lock(variant_set_mutex); //properly print the errors
  240. 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() + ").");
  241. ERR_PRINT(error);
  242. #ifdef DEBUG_ENABLED
  243. ERR_PRINT("code:\n" + current_source.get_with_code_lines());
  244. #endif
  245. return;
  246. }
  247. Vector<uint8_t> shader_data = RD::get_singleton()->shader_compile_binary_from_spirv(stages);
  248. ERR_FAIL_COND(shader_data.size() == 0);
  249. RID shader = RD::get_singleton()->shader_create_from_bytecode(shader_data);
  250. {
  251. MutexLock lock(variant_set_mutex);
  252. p_version->variants[p_variant] = shader;
  253. p_version->variant_data[p_variant] = shader_data;
  254. }
  255. }
  256. RS::ShaderNativeSourceCode ShaderRD::version_get_native_source_code(RID p_version) {
  257. Version *version = version_owner.getornull(p_version);
  258. RS::ShaderNativeSourceCode source_code;
  259. ERR_FAIL_COND_V(!version, source_code);
  260. source_code.versions.resize(variant_defines.size());
  261. for (int i = 0; i < source_code.versions.size(); i++) {
  262. if (!is_compute) {
  263. //vertex stage
  264. StringBuilder builder;
  265. _build_variant_code(builder, i, version, stage_templates[STAGE_TYPE_VERTEX]);
  266. RS::ShaderNativeSourceCode::Version::Stage stage;
  267. stage.name = "vertex";
  268. stage.code = builder.as_string();
  269. source_code.versions.write[i].stages.push_back(stage);
  270. }
  271. if (!is_compute) {
  272. //fragment stage
  273. StringBuilder builder;
  274. _build_variant_code(builder, i, version, stage_templates[STAGE_TYPE_FRAGMENT]);
  275. RS::ShaderNativeSourceCode::Version::Stage stage;
  276. stage.name = "fragment";
  277. stage.code = builder.as_string();
  278. source_code.versions.write[i].stages.push_back(stage);
  279. }
  280. if (is_compute) {
  281. //compute stage
  282. StringBuilder builder;
  283. _build_variant_code(builder, i, version, stage_templates[STAGE_TYPE_COMPUTE]);
  284. RS::ShaderNativeSourceCode::Version::Stage stage;
  285. stage.name = "compute";
  286. stage.code = builder.as_string();
  287. source_code.versions.write[i].stages.push_back(stage);
  288. }
  289. }
  290. return source_code;
  291. }
  292. String ShaderRD::_version_get_sha1(Version *p_version) const {
  293. StringBuilder hash_build;
  294. hash_build.append("[uniforms]");
  295. hash_build.append(p_version->uniforms.get_data());
  296. hash_build.append("[vertex_globals]");
  297. hash_build.append(p_version->vertex_globals.get_data());
  298. hash_build.append("[fragment_globals]");
  299. hash_build.append(p_version->fragment_globals.get_data());
  300. hash_build.append("[compute_globals]");
  301. hash_build.append(p_version->compute_globals.get_data());
  302. Vector<StringName> code_sections;
  303. for (Map<StringName, CharString>::Element *E = p_version->code_sections.front(); E; E = E->next()) {
  304. code_sections.push_back(E->key());
  305. }
  306. code_sections.sort_custom<StringName::AlphCompare>();
  307. for (int i = 0; i < code_sections.size(); i++) {
  308. hash_build.append(String("[code:") + String(code_sections[i]) + "]");
  309. hash_build.append(p_version->code_sections[code_sections[i]].get_data());
  310. }
  311. for (int i = 0; i < p_version->custom_defines.size(); i++) {
  312. hash_build.append("[custom_defines:" + itos(i) + "]");
  313. hash_build.append(p_version->custom_defines[i].get_data());
  314. }
  315. return hash_build.as_string().sha1_text();
  316. }
  317. static const char *shader_file_header = "GDSC";
  318. static const uint32_t cache_file_version = 2;
  319. bool ShaderRD::_load_from_cache(Version *p_version) {
  320. String sha1 = _version_get_sha1(p_version);
  321. String path = shader_cache_dir.plus_file(name).plus_file(base_sha256).plus_file(sha1) + ".cache";
  322. FileAccessRef f = FileAccess::open(path, FileAccess::READ);
  323. if (!f) {
  324. return false;
  325. }
  326. char header[5] = { 0, 0, 0, 0, 0 };
  327. f->get_buffer((uint8_t *)header, 4);
  328. ERR_FAIL_COND_V(header != String(shader_file_header), false);
  329. uint32_t file_version = f->get_32();
  330. if (file_version != cache_file_version) {
  331. return false; // wrong version
  332. }
  333. uint32_t variant_count = f->get_32();
  334. ERR_FAIL_COND_V(variant_count != (uint32_t)variant_defines.size(), false); //should not happen but check
  335. for (uint32_t i = 0; i < variant_count; i++) {
  336. uint32_t variant_size = f->get_32();
  337. ERR_FAIL_COND_V(variant_size == 0 && variants_enabled[i], false);
  338. if (!variants_enabled[i]) {
  339. continue;
  340. }
  341. Vector<uint8_t> variant_bytes;
  342. variant_bytes.resize(variant_size);
  343. uint32_t br = f->get_buffer(variant_bytes.ptrw(), variant_size);
  344. ERR_FAIL_COND_V(br != variant_size, false);
  345. p_version->variant_data[i] = variant_bytes;
  346. }
  347. for (uint32_t i = 0; i < variant_count; i++) {
  348. if (!variants_enabled[i]) {
  349. MutexLock lock(variant_set_mutex);
  350. p_version->variants[i] = RID();
  351. continue;
  352. }
  353. RID shader = RD::get_singleton()->shader_create_from_bytecode(p_version->variant_data[i]);
  354. if (shader.is_null()) {
  355. for (uint32_t j = 0; j < i; j++) {
  356. RD::get_singleton()->free(p_version->variants[i]);
  357. }
  358. ERR_FAIL_COND_V(shader.is_null(), false);
  359. }
  360. {
  361. MutexLock lock(variant_set_mutex);
  362. p_version->variants[i] = shader;
  363. }
  364. }
  365. memdelete_arr(p_version->variant_data); //clear stages
  366. p_version->variant_data = nullptr;
  367. p_version->valid = true;
  368. return true;
  369. }
  370. void ShaderRD::_save_to_cache(Version *p_version) {
  371. String sha1 = _version_get_sha1(p_version);
  372. String path = shader_cache_dir.plus_file(name).plus_file(base_sha256).plus_file(sha1) + ".cache";
  373. FileAccessRef f = FileAccess::open(path, FileAccess::WRITE);
  374. ERR_FAIL_COND(!f);
  375. f->store_buffer((const uint8_t *)shader_file_header, 4);
  376. f->store_32(cache_file_version); //file version
  377. uint32_t variant_count = variant_defines.size();
  378. f->store_32(variant_count); //variant count
  379. for (uint32_t i = 0; i < variant_count; i++) {
  380. f->store_32(p_version->variant_data[i].size()); //stage count
  381. f->store_buffer(p_version->variant_data[i].ptr(), p_version->variant_data[i].size());
  382. }
  383. f->close();
  384. }
  385. void ShaderRD::_compile_version(Version *p_version) {
  386. _clear_version(p_version);
  387. p_version->valid = false;
  388. p_version->dirty = false;
  389. p_version->variants = memnew_arr(RID, variant_defines.size());
  390. typedef Vector<uint8_t> ShaderStageData;
  391. p_version->variant_data = memnew_arr(ShaderStageData, variant_defines.size());
  392. if (shader_cache_dir_valid) {
  393. if (_load_from_cache(p_version)) {
  394. return;
  395. }
  396. }
  397. #if 1
  398. RendererThreadPool::singleton->thread_work_pool.do_work(variant_defines.size(), this, &ShaderRD::_compile_variant, p_version);
  399. #else
  400. for (int i = 0; i < variant_defines.size(); i++) {
  401. _compile_variant(i, p_version);
  402. }
  403. #endif
  404. bool all_valid = true;
  405. for (int i = 0; i < variant_defines.size(); i++) {
  406. if (!variants_enabled[i]) {
  407. continue; //disabled
  408. }
  409. if (p_version->variants[i].is_null()) {
  410. all_valid = false;
  411. break;
  412. }
  413. }
  414. if (!all_valid) {
  415. //clear versions if they exist
  416. for (int i = 0; i < variant_defines.size(); i++) {
  417. if (!variants_enabled[i]) {
  418. continue; //disabled
  419. }
  420. if (!p_version->variants[i].is_null()) {
  421. RD::get_singleton()->free(p_version->variants[i]);
  422. }
  423. }
  424. memdelete_arr(p_version->variants);
  425. if (p_version->variant_data) {
  426. memdelete_arr(p_version->variant_data);
  427. }
  428. p_version->variants = nullptr;
  429. p_version->variant_data = nullptr;
  430. return;
  431. } else if (shader_cache_dir_valid) {
  432. //save shader cache
  433. _save_to_cache(p_version);
  434. }
  435. memdelete_arr(p_version->variant_data); //clear stages
  436. p_version->variant_data = nullptr;
  437. p_version->valid = true;
  438. }
  439. void ShaderRD::version_set_code(RID p_version, const Map<String, String> &p_code, const String &p_uniforms, const String &p_vertex_globals, const String &p_fragment_globals, const Vector<String> &p_custom_defines) {
  440. ERR_FAIL_COND(is_compute);
  441. Version *version = version_owner.getornull(p_version);
  442. ERR_FAIL_COND(!version);
  443. version->vertex_globals = p_vertex_globals.utf8();
  444. version->fragment_globals = p_fragment_globals.utf8();
  445. version->uniforms = p_uniforms.utf8();
  446. version->code_sections.clear();
  447. for (Map<String, String>::Element *E = p_code.front(); E; E = E->next()) {
  448. version->code_sections[StringName(E->key().to_upper())] = E->get().utf8();
  449. }
  450. version->custom_defines.clear();
  451. for (int i = 0; i < p_custom_defines.size(); i++) {
  452. version->custom_defines.push_back(p_custom_defines[i].utf8());
  453. }
  454. version->dirty = true;
  455. if (version->initialize_needed) {
  456. _compile_version(version);
  457. version->initialize_needed = false;
  458. }
  459. }
  460. void ShaderRD::version_set_compute_code(RID p_version, const Map<String, String> &p_code, const String &p_uniforms, const String &p_compute_globals, const Vector<String> &p_custom_defines) {
  461. ERR_FAIL_COND(!is_compute);
  462. Version *version = version_owner.getornull(p_version);
  463. ERR_FAIL_COND(!version);
  464. version->compute_globals = p_compute_globals.utf8();
  465. version->uniforms = p_uniforms.utf8();
  466. version->code_sections.clear();
  467. for (Map<String, String>::Element *E = p_code.front(); E; E = E->next()) {
  468. version->code_sections[StringName(E->key().to_upper())] = E->get().utf8();
  469. }
  470. version->custom_defines.clear();
  471. for (int i = 0; i < p_custom_defines.size(); i++) {
  472. version->custom_defines.push_back(p_custom_defines[i].utf8());
  473. }
  474. version->dirty = true;
  475. if (version->initialize_needed) {
  476. _compile_version(version);
  477. version->initialize_needed = false;
  478. }
  479. }
  480. bool ShaderRD::version_is_valid(RID p_version) {
  481. Version *version = version_owner.getornull(p_version);
  482. ERR_FAIL_COND_V(!version, false);
  483. if (version->dirty) {
  484. _compile_version(version);
  485. }
  486. return version->valid;
  487. }
  488. bool ShaderRD::version_free(RID p_version) {
  489. if (version_owner.owns(p_version)) {
  490. Version *version = version_owner.getornull(p_version);
  491. _clear_version(version);
  492. version_owner.free(p_version);
  493. } else {
  494. return false;
  495. }
  496. return true;
  497. }
  498. void ShaderRD::set_variant_enabled(int p_variant, bool p_enabled) {
  499. ERR_FAIL_COND(version_owner.get_rid_count() > 0); //versions exist
  500. ERR_FAIL_INDEX(p_variant, variants_enabled.size());
  501. variants_enabled.write[p_variant] = p_enabled;
  502. }
  503. bool ShaderRD::is_variant_enabled(int p_variant) const {
  504. ERR_FAIL_INDEX_V(p_variant, variants_enabled.size(), false);
  505. return variants_enabled[p_variant];
  506. }
  507. bool ShaderRD::shader_cache_cleanup_on_start = false;
  508. ShaderRD::ShaderRD() {
  509. // Do not feel forced to use this, in most cases it makes little to no difference.
  510. bool use_32_threads = false;
  511. if (RD::get_singleton()->get_device_vendor_name() == "NVIDIA") {
  512. use_32_threads = true;
  513. }
  514. String base_compute_define_text;
  515. if (use_32_threads) {
  516. 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";
  517. } else {
  518. 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";
  519. }
  520. base_compute_defines = base_compute_define_text.ascii();
  521. }
  522. void ShaderRD::initialize(const Vector<String> &p_variant_defines, const String &p_general_defines) {
  523. ERR_FAIL_COND(variant_defines.size());
  524. ERR_FAIL_COND(p_variant_defines.size() == 0);
  525. general_defines = p_general_defines.utf8();
  526. for (int i = 0; i < p_variant_defines.size(); i++) {
  527. variant_defines.push_back(p_variant_defines[i].utf8());
  528. variants_enabled.push_back(true);
  529. }
  530. if (shader_cache_dir != String()) {
  531. StringBuilder hash_build;
  532. hash_build.append("[base_hash]");
  533. hash_build.append(base_sha256);
  534. hash_build.append("[general_defines]");
  535. hash_build.append(general_defines.get_data());
  536. for (int i = 0; i < variant_defines.size(); i++) {
  537. hash_build.append("[variant_defines:" + itos(i) + "]");
  538. hash_build.append(variant_defines[i].get_data());
  539. }
  540. base_sha256 = hash_build.as_string().sha256_text();
  541. DirAccessRef d = DirAccess::open(shader_cache_dir);
  542. ERR_FAIL_COND(!d);
  543. if (d->change_dir(name) != OK) {
  544. Error err = d->make_dir(name);
  545. ERR_FAIL_COND(err != OK);
  546. d->change_dir(name);
  547. }
  548. //erase other versions?
  549. if (shader_cache_cleanup_on_start) {
  550. }
  551. //
  552. if (d->change_dir(base_sha256) != OK) {
  553. Error err = d->make_dir(base_sha256);
  554. ERR_FAIL_COND(err != OK);
  555. }
  556. shader_cache_dir_valid = true;
  557. print_verbose("Shader '" + name + "' SHA256: " + base_sha256);
  558. }
  559. }
  560. void ShaderRD::set_shader_cache_dir(const String &p_dir) {
  561. shader_cache_dir = p_dir;
  562. }
  563. void ShaderRD::set_shader_cache_save_compressed(bool p_enable) {
  564. shader_cache_save_compressed = p_enable;
  565. }
  566. void ShaderRD::set_shader_cache_save_compressed_zstd(bool p_enable) {
  567. shader_cache_save_compressed_zstd = p_enable;
  568. }
  569. void ShaderRD::set_shader_cache_save_debug(bool p_enable) {
  570. shader_cache_save_debug = p_enable;
  571. }
  572. String ShaderRD::shader_cache_dir;
  573. bool ShaderRD::shader_cache_save_compressed = true;
  574. bool ShaderRD::shader_cache_save_compressed_zstd = true;
  575. bool ShaderRD::shader_cache_save_debug = true;
  576. ShaderRD::~ShaderRD() {
  577. List<RID> remaining;
  578. version_owner.get_owned_list(&remaining);
  579. if (remaining.size()) {
  580. ERR_PRINT(itos(remaining.size()) + " shaders of type " + name + " were never freed");
  581. while (remaining.size()) {
  582. version_free(remaining.front()->get());
  583. remaining.pop_front();
  584. }
  585. }
  586. }