shader_rd.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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/string/string_builder.h"
  32. #include "renderer_compositor_rd.h"
  33. #include "servers/rendering/rendering_device.h"
  34. void ShaderRD::setup(const char *p_vertex_code, const char *p_fragment_code, const char *p_compute_code, const char *p_name) {
  35. name = p_name;
  36. //split vertex and shader code (thank you, shader compiler programmers from you know what company).
  37. if (p_vertex_code) {
  38. String defines_tag = "\nVERSION_DEFINES";
  39. String globals_tag = "\nVERTEX_SHADER_GLOBALS";
  40. String material_tag = "\nMATERIAL_UNIFORMS";
  41. String code_tag = "\nVERTEX_SHADER_CODE";
  42. String code = p_vertex_code;
  43. int cpos = code.find(defines_tag);
  44. if (cpos != -1) {
  45. vertex_codev = code.substr(0, cpos).ascii();
  46. code = code.substr(cpos + defines_tag.length(), code.length());
  47. }
  48. cpos = code.find(material_tag);
  49. if (cpos == -1) {
  50. vertex_code0 = code.ascii();
  51. } else {
  52. vertex_code0 = code.substr(0, cpos).ascii();
  53. code = code.substr(cpos + material_tag.length(), code.length());
  54. cpos = code.find(globals_tag);
  55. if (cpos == -1) {
  56. vertex_code1 = code.ascii();
  57. } else {
  58. vertex_code1 = code.substr(0, cpos).ascii();
  59. String code2 = code.substr(cpos + globals_tag.length(), code.length());
  60. cpos = code2.find(code_tag);
  61. if (cpos == -1) {
  62. vertex_code2 = code2.ascii();
  63. } else {
  64. vertex_code2 = code2.substr(0, cpos).ascii();
  65. vertex_code3 = code2.substr(cpos + code_tag.length(), code2.length()).ascii();
  66. }
  67. }
  68. }
  69. }
  70. if (p_fragment_code) {
  71. String defines_tag = "\nVERSION_DEFINES";
  72. String globals_tag = "\nFRAGMENT_SHADER_GLOBALS";
  73. String material_tag = "\nMATERIAL_UNIFORMS";
  74. String code_tag = "\nFRAGMENT_SHADER_CODE";
  75. String light_code_tag = "\nLIGHT_SHADER_CODE";
  76. String code = p_fragment_code;
  77. int cpos = code.find(defines_tag);
  78. if (cpos != -1) {
  79. fragment_codev = code.substr(0, cpos).ascii();
  80. code = code.substr(cpos + defines_tag.length(), code.length());
  81. }
  82. cpos = code.find(material_tag);
  83. if (cpos == -1) {
  84. fragment_code0 = code.ascii();
  85. } else {
  86. fragment_code0 = code.substr(0, cpos).ascii();
  87. //print_line("CODE0:\n"+String(fragment_code0.get_data()));
  88. code = code.substr(cpos + material_tag.length(), code.length());
  89. cpos = code.find(globals_tag);
  90. if (cpos == -1) {
  91. fragment_code1 = code.ascii();
  92. } else {
  93. fragment_code1 = code.substr(0, cpos).ascii();
  94. //print_line("CODE1:\n"+String(fragment_code1.get_data()));
  95. String code2 = code.substr(cpos + globals_tag.length(), code.length());
  96. cpos = code2.find(light_code_tag);
  97. if (cpos == -1) {
  98. fragment_code2 = code2.ascii();
  99. } else {
  100. fragment_code2 = code2.substr(0, cpos).ascii();
  101. //print_line("CODE2:\n"+String(fragment_code2.get_data()));
  102. String code3 = code2.substr(cpos + light_code_tag.length(), code2.length());
  103. cpos = code3.find(code_tag);
  104. if (cpos == -1) {
  105. fragment_code3 = code3.ascii();
  106. } else {
  107. fragment_code3 = code3.substr(0, cpos).ascii();
  108. //print_line("CODE3:\n"+String(fragment_code3.get_data()));
  109. fragment_code4 = code3.substr(cpos + code_tag.length(), code3.length()).ascii();
  110. //print_line("CODE4:\n"+String(fragment_code4.get_data()));
  111. }
  112. }
  113. }
  114. }
  115. }
  116. if (p_compute_code) {
  117. is_compute = true;
  118. String defines_tag = "\nVERSION_DEFINES";
  119. String globals_tag = "\nCOMPUTE_SHADER_GLOBALS";
  120. String material_tag = "\nMATERIAL_UNIFORMS";
  121. String code_tag = "\nCOMPUTE_SHADER_CODE";
  122. String code = p_compute_code;
  123. int cpos = code.find(defines_tag);
  124. if (cpos != -1) {
  125. compute_codev = code.substr(0, cpos).ascii();
  126. code = code.substr(cpos + defines_tag.length(), code.length());
  127. }
  128. cpos = code.find(material_tag);
  129. if (cpos == -1) {
  130. compute_code0 = code.ascii();
  131. } else {
  132. compute_code0 = code.substr(0, cpos).ascii();
  133. code = code.substr(cpos + material_tag.length(), code.length());
  134. cpos = code.find(globals_tag);
  135. if (cpos == -1) {
  136. compute_code1 = code.ascii();
  137. } else {
  138. compute_code1 = code.substr(0, cpos).ascii();
  139. String code2 = code.substr(cpos + globals_tag.length(), code.length());
  140. cpos = code2.find(code_tag);
  141. if (cpos == -1) {
  142. compute_code2 = code2.ascii();
  143. } else {
  144. compute_code2 = code2.substr(0, cpos).ascii();
  145. compute_code3 = code2.substr(cpos + code_tag.length(), code2.length()).ascii();
  146. }
  147. }
  148. }
  149. }
  150. }
  151. RID ShaderRD::version_create() {
  152. //initialize() was never called
  153. ERR_FAIL_COND_V(variant_defines.size() == 0, RID());
  154. Version version;
  155. version.dirty = true;
  156. version.valid = false;
  157. version.initialize_needed = true;
  158. version.variants = nullptr;
  159. return version_owner.make_rid(version);
  160. }
  161. void ShaderRD::_clear_version(Version *p_version) {
  162. //clear versions if they exist
  163. if (p_version->variants) {
  164. for (int i = 0; i < variant_defines.size(); i++) {
  165. RD::get_singleton()->free(p_version->variants[i]);
  166. }
  167. memdelete_arr(p_version->variants);
  168. p_version->variants = nullptr;
  169. }
  170. }
  171. void ShaderRD::_compile_variant(uint32_t p_variant, Version *p_version) {
  172. if (!variants_enabled[p_variant]) {
  173. return; //variant is disabled, return
  174. }
  175. Vector<RD::ShaderStageData> stages;
  176. String error;
  177. String current_source;
  178. RD::ShaderStage current_stage = RD::SHADER_STAGE_VERTEX;
  179. bool build_ok = true;
  180. if (!is_compute) {
  181. //vertex stage
  182. StringBuilder builder;
  183. builder.append(vertex_codev.get_data()); // version info (if exists)
  184. builder.append("\n"); //make sure defines begin at newline
  185. builder.append(general_defines.get_data());
  186. builder.append(variant_defines[p_variant].get_data());
  187. for (int j = 0; j < p_version->custom_defines.size(); j++) {
  188. builder.append(p_version->custom_defines[j].get_data());
  189. }
  190. builder.append(vertex_code0.get_data()); //first part of vertex
  191. builder.append(p_version->uniforms.get_data()); //uniforms (same for vertex and fragment)
  192. builder.append(vertex_code1.get_data()); //second part of vertex
  193. builder.append(p_version->vertex_globals.get_data()); // vertex globals
  194. builder.append(vertex_code2.get_data()); //third part of vertex
  195. builder.append(p_version->vertex_code.get_data()); // code
  196. builder.append(vertex_code3.get_data()); //fourth of vertex
  197. current_source = builder.as_string();
  198. RD::ShaderStageData stage;
  199. stage.spir_v = RD::get_singleton()->shader_compile_from_source(RD::SHADER_STAGE_VERTEX, current_source, RD::SHADER_LANGUAGE_GLSL, &error);
  200. if (stage.spir_v.size() == 0) {
  201. build_ok = false;
  202. } else {
  203. stage.shader_stage = RD::SHADER_STAGE_VERTEX;
  204. stages.push_back(stage);
  205. }
  206. }
  207. if (!is_compute && build_ok) {
  208. //fragment stage
  209. current_stage = RD::SHADER_STAGE_FRAGMENT;
  210. StringBuilder builder;
  211. builder.append(fragment_codev.get_data()); // version info (if exists)
  212. builder.append("\n"); //make sure defines begin at newline
  213. builder.append(general_defines.get_data());
  214. builder.append(variant_defines[p_variant].get_data());
  215. for (int j = 0; j < p_version->custom_defines.size(); j++) {
  216. builder.append(p_version->custom_defines[j].get_data());
  217. }
  218. builder.append(fragment_code0.get_data()); //first part of fragment
  219. builder.append(p_version->uniforms.get_data()); //uniforms (same for fragment and fragment)
  220. builder.append(fragment_code1.get_data()); //first part of fragment
  221. builder.append(p_version->fragment_globals.get_data()); // fragment globals
  222. builder.append(fragment_code2.get_data()); //third part of fragment
  223. builder.append(p_version->fragment_light.get_data()); // fragment light
  224. builder.append(fragment_code3.get_data()); //fourth part of fragment
  225. builder.append(p_version->fragment_code.get_data()); // fragment code
  226. builder.append(fragment_code4.get_data()); //fourth part of fragment
  227. current_source = builder.as_string();
  228. RD::ShaderStageData stage;
  229. stage.spir_v = RD::get_singleton()->shader_compile_from_source(RD::SHADER_STAGE_FRAGMENT, current_source, RD::SHADER_LANGUAGE_GLSL, &error);
  230. if (stage.spir_v.size() == 0) {
  231. build_ok = false;
  232. } else {
  233. stage.shader_stage = RD::SHADER_STAGE_FRAGMENT;
  234. stages.push_back(stage);
  235. }
  236. }
  237. if (is_compute) {
  238. //compute stage
  239. current_stage = RD::SHADER_STAGE_COMPUTE;
  240. StringBuilder builder;
  241. builder.append(compute_codev.get_data()); // version info (if exists)
  242. builder.append("\n"); //make sure defines begin at newline
  243. builder.append(base_compute_defines.get_data());
  244. builder.append(general_defines.get_data());
  245. builder.append(variant_defines[p_variant].get_data());
  246. for (int j = 0; j < p_version->custom_defines.size(); j++) {
  247. builder.append(p_version->custom_defines[j].get_data());
  248. }
  249. builder.append(compute_code0.get_data()); //first part of compute
  250. builder.append(p_version->uniforms.get_data()); //uniforms (same for compute and fragment)
  251. builder.append(compute_code1.get_data()); //second part of compute
  252. builder.append(p_version->compute_globals.get_data()); // compute globals
  253. builder.append(compute_code2.get_data()); //third part of compute
  254. builder.append(p_version->compute_code.get_data()); // code
  255. builder.append(compute_code3.get_data()); //fourth of compute
  256. current_source = builder.as_string();
  257. RD::ShaderStageData stage;
  258. stage.spir_v = RD::get_singleton()->shader_compile_from_source(RD::SHADER_STAGE_COMPUTE, current_source, RD::SHADER_LANGUAGE_GLSL, &error);
  259. if (stage.spir_v.size() == 0) {
  260. build_ok = false;
  261. } else {
  262. stage.shader_stage = RD::SHADER_STAGE_COMPUTE;
  263. stages.push_back(stage);
  264. }
  265. }
  266. if (!build_ok) {
  267. MutexLock lock(variant_set_mutex); //properly print the errors
  268. 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() + ").");
  269. ERR_PRINT(error);
  270. #ifdef DEBUG_ENABLED
  271. ERR_PRINT("code:\n" + current_source.get_with_code_lines());
  272. #endif
  273. return;
  274. }
  275. RID shader = RD::get_singleton()->shader_create(stages);
  276. {
  277. MutexLock lock(variant_set_mutex);
  278. p_version->variants[p_variant] = shader;
  279. }
  280. }
  281. RS::ShaderNativeSourceCode ShaderRD::version_get_native_source_code(RID p_version) {
  282. Version *version = version_owner.getornull(p_version);
  283. RS::ShaderNativeSourceCode source_code;
  284. ERR_FAIL_COND_V(!version, source_code);
  285. source_code.versions.resize(variant_defines.size());
  286. for (int i = 0; i < source_code.versions.size(); i++) {
  287. if (!is_compute) {
  288. //vertex stage
  289. StringBuilder builder;
  290. builder.append(vertex_codev.get_data()); // version info (if exists)
  291. builder.append("\n"); //make sure defines begin at newline
  292. builder.append(general_defines.get_data());
  293. builder.append(variant_defines[i].get_data());
  294. for (int j = 0; j < version->custom_defines.size(); j++) {
  295. builder.append(version->custom_defines[j].get_data());
  296. }
  297. builder.append(vertex_code0.get_data()); //first part of vertex
  298. builder.append(version->uniforms.get_data()); //uniforms (same for vertex and fragment)
  299. builder.append(vertex_code1.get_data()); //second part of vertex
  300. builder.append(version->vertex_globals.get_data()); // vertex globals
  301. builder.append(vertex_code2.get_data()); //third part of vertex
  302. builder.append(version->vertex_code.get_data()); // code
  303. builder.append(vertex_code3.get_data()); //fourth of vertex
  304. RS::ShaderNativeSourceCode::Version::Stage stage;
  305. stage.name = "vertex";
  306. stage.code = builder.as_string();
  307. source_code.versions.write[i].stages.push_back(stage);
  308. }
  309. if (!is_compute) {
  310. //fragment stage
  311. StringBuilder builder;
  312. builder.append(fragment_codev.get_data()); // version info (if exists)
  313. builder.append("\n"); //make sure defines begin at newline
  314. builder.append(general_defines.get_data());
  315. builder.append(variant_defines[i].get_data());
  316. for (int j = 0; j < version->custom_defines.size(); j++) {
  317. builder.append(version->custom_defines[j].get_data());
  318. }
  319. builder.append(fragment_code0.get_data()); //first part of fragment
  320. builder.append(version->uniforms.get_data()); //uniforms (same for fragment and fragment)
  321. builder.append(fragment_code1.get_data()); //first part of fragment
  322. builder.append(version->fragment_globals.get_data()); // fragment globals
  323. builder.append(fragment_code2.get_data()); //third part of fragment
  324. builder.append(version->fragment_light.get_data()); // fragment light
  325. builder.append(fragment_code3.get_data()); //fourth part of fragment
  326. builder.append(version->fragment_code.get_data()); // fragment code
  327. builder.append(fragment_code4.get_data()); //fourth part of fragment
  328. RS::ShaderNativeSourceCode::Version::Stage stage;
  329. stage.name = "fragment";
  330. stage.code = builder.as_string();
  331. source_code.versions.write[i].stages.push_back(stage);
  332. }
  333. if (is_compute) {
  334. //compute stage
  335. StringBuilder builder;
  336. builder.append(compute_codev.get_data()); // version info (if exists)
  337. builder.append("\n"); //make sure defines begin at newline
  338. builder.append(base_compute_defines.get_data());
  339. builder.append(general_defines.get_data());
  340. builder.append(variant_defines[i].get_data());
  341. for (int j = 0; j < version->custom_defines.size(); j++) {
  342. builder.append(version->custom_defines[j].get_data());
  343. }
  344. builder.append(compute_code0.get_data()); //first part of compute
  345. builder.append(version->uniforms.get_data()); //uniforms (same for compute and fragment)
  346. builder.append(compute_code1.get_data()); //second part of compute
  347. builder.append(version->compute_globals.get_data()); // compute globals
  348. builder.append(compute_code2.get_data()); //third part of compute
  349. builder.append(version->compute_code.get_data()); // code
  350. builder.append(compute_code3.get_data()); //fourth of compute
  351. RS::ShaderNativeSourceCode::Version::Stage stage;
  352. stage.name = "compute";
  353. stage.code = builder.as_string();
  354. source_code.versions.write[i].stages.push_back(stage);
  355. }
  356. }
  357. return source_code;
  358. }
  359. void ShaderRD::_compile_version(Version *p_version) {
  360. _clear_version(p_version);
  361. p_version->valid = false;
  362. p_version->dirty = false;
  363. p_version->variants = memnew_arr(RID, variant_defines.size());
  364. #if 1
  365. RendererThreadPool::singleton->thread_work_pool.do_work(variant_defines.size(), this, &ShaderRD::_compile_variant, p_version);
  366. #else
  367. for (int i = 0; i < variant_defines.size(); i++) {
  368. _compile_variant(i, p_version);
  369. }
  370. #endif
  371. bool all_valid = true;
  372. for (int i = 0; i < variant_defines.size(); i++) {
  373. if (!variants_enabled[i]) {
  374. continue; //disabled
  375. }
  376. if (p_version->variants[i].is_null()) {
  377. all_valid = false;
  378. break;
  379. }
  380. }
  381. if (!all_valid) {
  382. //clear versions if they exist
  383. for (int i = 0; i < variant_defines.size(); i++) {
  384. if (!variants_enabled[i]) {
  385. continue; //disabled
  386. }
  387. if (!p_version->variants[i].is_null()) {
  388. RD::get_singleton()->free(p_version->variants[i]);
  389. }
  390. }
  391. memdelete_arr(p_version->variants);
  392. p_version->variants = nullptr;
  393. return;
  394. }
  395. p_version->valid = true;
  396. }
  397. void ShaderRD::version_set_code(RID p_version, const String &p_uniforms, const String &p_vertex_globals, const String &p_vertex_code, const String &p_fragment_globals, const String &p_fragment_light, const String &p_fragment_code, const Vector<String> &p_custom_defines) {
  398. ERR_FAIL_COND(is_compute);
  399. Version *version = version_owner.getornull(p_version);
  400. ERR_FAIL_COND(!version);
  401. version->vertex_globals = p_vertex_globals.utf8();
  402. version->vertex_code = p_vertex_code.utf8();
  403. version->fragment_light = p_fragment_light.utf8();
  404. version->fragment_globals = p_fragment_globals.utf8();
  405. version->fragment_code = p_fragment_code.utf8();
  406. version->uniforms = p_uniforms.utf8();
  407. version->custom_defines.clear();
  408. for (int i = 0; i < p_custom_defines.size(); i++) {
  409. version->custom_defines.push_back(p_custom_defines[i].utf8());
  410. }
  411. version->dirty = true;
  412. if (version->initialize_needed) {
  413. _compile_version(version);
  414. version->initialize_needed = false;
  415. }
  416. }
  417. void ShaderRD::version_set_compute_code(RID p_version, const String &p_uniforms, const String &p_compute_globals, const String &p_compute_code, const Vector<String> &p_custom_defines) {
  418. ERR_FAIL_COND(!is_compute);
  419. Version *version = version_owner.getornull(p_version);
  420. ERR_FAIL_COND(!version);
  421. version->compute_globals = p_compute_globals.utf8();
  422. version->compute_code = p_compute_code.utf8();
  423. version->uniforms = p_uniforms.utf8();
  424. version->custom_defines.clear();
  425. for (int i = 0; i < p_custom_defines.size(); i++) {
  426. version->custom_defines.push_back(p_custom_defines[i].utf8());
  427. }
  428. version->dirty = true;
  429. if (version->initialize_needed) {
  430. _compile_version(version);
  431. version->initialize_needed = false;
  432. }
  433. }
  434. bool ShaderRD::version_is_valid(RID p_version) {
  435. Version *version = version_owner.getornull(p_version);
  436. ERR_FAIL_COND_V(!version, false);
  437. if (version->dirty) {
  438. _compile_version(version);
  439. }
  440. return version->valid;
  441. }
  442. bool ShaderRD::version_free(RID p_version) {
  443. if (version_owner.owns(p_version)) {
  444. Version *version = version_owner.getornull(p_version);
  445. _clear_version(version);
  446. version_owner.free(p_version);
  447. } else {
  448. return false;
  449. }
  450. return true;
  451. }
  452. void ShaderRD::set_variant_enabled(int p_variant, bool p_enabled) {
  453. ERR_FAIL_COND(version_owner.get_rid_count() > 0); //versions exist
  454. ERR_FAIL_INDEX(p_variant, variants_enabled.size());
  455. variants_enabled.write[p_variant] = p_enabled;
  456. }
  457. bool ShaderRD::is_variant_enabled(int p_variant) const {
  458. ERR_FAIL_INDEX_V(p_variant, variants_enabled.size(), false);
  459. return variants_enabled[p_variant];
  460. }
  461. ShaderRD::ShaderRD() {
  462. // Do not feel forced to use this, in most cases it makes little to no difference.
  463. bool use_32_threads = false;
  464. if (RD::get_singleton()->get_device_vendor_name() == "NVIDIA") {
  465. use_32_threads = true;
  466. }
  467. String base_compute_define_text;
  468. if (use_32_threads) {
  469. 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";
  470. } else {
  471. 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";
  472. }
  473. base_compute_defines = base_compute_define_text.ascii();
  474. }
  475. void ShaderRD::initialize(const Vector<String> &p_variant_defines, const String &p_general_defines) {
  476. ERR_FAIL_COND(variant_defines.size());
  477. ERR_FAIL_COND(p_variant_defines.size() == 0);
  478. general_defines = p_general_defines.utf8();
  479. for (int i = 0; i < p_variant_defines.size(); i++) {
  480. variant_defines.push_back(p_variant_defines[i].utf8());
  481. variants_enabled.push_back(true);
  482. }
  483. }
  484. ShaderRD::~ShaderRD() {
  485. List<RID> remaining;
  486. version_owner.get_owned_list(&remaining);
  487. if (remaining.size()) {
  488. ERR_PRINT(itos(remaining.size()) + " shaders of type " + name + " were never freed");
  489. while (remaining.size()) {
  490. version_free(remaining.front()->get());
  491. remaining.pop_front();
  492. }
  493. }
  494. }