shader_rd.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 "renderer_compositor_rd.h"
  32. #include "servers/rendering/rendering_device.h"
  33. void ShaderRD::_add_stage(const char *p_code, StageType p_stage_type) {
  34. Vector<String> lines = String(p_code).split("\n");
  35. String text;
  36. for (int i = 0; i < lines.size(); i++) {
  37. String l = lines[i];
  38. bool push_chunk = false;
  39. StageTemplate::Chunk chunk;
  40. if (l.begins_with("#VERSION_DEFINES")) {
  41. chunk.type = StageTemplate::Chunk::TYPE_VERSION_DEFINES;
  42. push_chunk = true;
  43. } else if (l.begins_with("#GLOBALS")) {
  44. switch (p_stage_type) {
  45. case STAGE_TYPE_VERTEX:
  46. chunk.type = StageTemplate::Chunk::TYPE_VERTEX_GLOBALS;
  47. break;
  48. case STAGE_TYPE_FRAGMENT:
  49. chunk.type = StageTemplate::Chunk::TYPE_FRAGMENT_GLOBALS;
  50. break;
  51. case STAGE_TYPE_COMPUTE:
  52. chunk.type = StageTemplate::Chunk::TYPE_COMPUTE_GLOBALS;
  53. break;
  54. default: {
  55. }
  56. }
  57. push_chunk = true;
  58. } else if (l.begins_with("#MATERIAL_UNIFORMS")) {
  59. chunk.type = StageTemplate::Chunk::TYPE_MATERIAL_UNIFORMS;
  60. push_chunk = true;
  61. } else if (l.begins_with("#CODE")) {
  62. chunk.type = StageTemplate::Chunk::TYPE_CODE;
  63. push_chunk = true;
  64. chunk.code = l.replace_first("#CODE", String()).replace(":", "").strip_edges().to_upper();
  65. } else {
  66. text += l + "\n";
  67. }
  68. if (push_chunk) {
  69. if (text != String()) {
  70. StageTemplate::Chunk text_chunk;
  71. text_chunk.type = StageTemplate::Chunk::TYPE_TEXT;
  72. text_chunk.text = text.utf8();
  73. stage_templates[p_stage_type].chunks.push_back(text_chunk);
  74. text = String();
  75. }
  76. stage_templates[p_stage_type].chunks.push_back(chunk);
  77. }
  78. }
  79. if (text != String()) {
  80. StageTemplate::Chunk text_chunk;
  81. text_chunk.type = StageTemplate::Chunk::TYPE_TEXT;
  82. text_chunk.text = text.utf8();
  83. stage_templates[p_stage_type].chunks.push_back(text_chunk);
  84. text = String();
  85. }
  86. }
  87. void ShaderRD::setup(const char *p_vertex_code, const char *p_fragment_code, const char *p_compute_code, const char *p_name) {
  88. name = p_name;
  89. if (p_compute_code) {
  90. _add_stage(p_compute_code, STAGE_TYPE_COMPUTE);
  91. is_compute = true;
  92. } else {
  93. is_compute = false;
  94. if (p_vertex_code) {
  95. _add_stage(p_vertex_code, STAGE_TYPE_VERTEX);
  96. }
  97. if (p_fragment_code) {
  98. _add_stage(p_fragment_code, STAGE_TYPE_FRAGMENT);
  99. }
  100. }
  101. }
  102. RID ShaderRD::version_create() {
  103. //initialize() was never called
  104. ERR_FAIL_COND_V(variant_defines.size() == 0, RID());
  105. Version version;
  106. version.dirty = true;
  107. version.valid = false;
  108. version.initialize_needed = true;
  109. version.variants = nullptr;
  110. return version_owner.make_rid(version);
  111. }
  112. void ShaderRD::_clear_version(Version *p_version) {
  113. //clear versions if they exist
  114. if (p_version->variants) {
  115. for (int i = 0; i < variant_defines.size(); i++) {
  116. RD::get_singleton()->free(p_version->variants[i]);
  117. }
  118. memdelete_arr(p_version->variants);
  119. p_version->variants = nullptr;
  120. }
  121. }
  122. void ShaderRD::_build_variant_code(StringBuilder &builder, uint32_t p_variant, const Version *p_version, const StageTemplate &p_template) {
  123. for (uint32_t i = 0; i < p_template.chunks.size(); i++) {
  124. const StageTemplate::Chunk &chunk = p_template.chunks[i];
  125. switch (chunk.type) {
  126. case StageTemplate::Chunk::TYPE_VERSION_DEFINES: {
  127. builder.append("\n"); //make sure defines begin at newline
  128. builder.append(general_defines.get_data());
  129. builder.append(variant_defines[p_variant].get_data());
  130. for (int j = 0; j < p_version->custom_defines.size(); j++) {
  131. builder.append(p_version->custom_defines[j].get_data());
  132. }
  133. builder.append("\n"); //make sure defines begin at newline
  134. if (p_version->uniforms.size()) {
  135. builder.append("#define MATERIAL_UNIFORMS_USED\n");
  136. }
  137. for (Map<StringName, CharString>::Element *E = p_version->code_sections.front(); E; E = E->next()) {
  138. builder.append(String("#define ") + String(E->key()) + "_CODE_USED\n");
  139. }
  140. } break;
  141. case StageTemplate::Chunk::TYPE_MATERIAL_UNIFORMS: {
  142. builder.append(p_version->uniforms.get_data()); //uniforms (same for vertex and fragment)
  143. } break;
  144. case StageTemplate::Chunk::TYPE_VERTEX_GLOBALS: {
  145. builder.append(p_version->vertex_globals.get_data()); // vertex globals
  146. } break;
  147. case StageTemplate::Chunk::TYPE_FRAGMENT_GLOBALS: {
  148. builder.append(p_version->fragment_globals.get_data()); // fragment globals
  149. } break;
  150. case StageTemplate::Chunk::TYPE_COMPUTE_GLOBALS: {
  151. builder.append(p_version->compute_globals.get_data()); // compute globals
  152. } break;
  153. case StageTemplate::Chunk::TYPE_CODE: {
  154. if (p_version->code_sections.has(chunk.code)) {
  155. builder.append(p_version->code_sections[chunk.code].get_data());
  156. }
  157. } break;
  158. case StageTemplate::Chunk::TYPE_TEXT: {
  159. builder.append(chunk.text.get_data());
  160. } break;
  161. }
  162. }
  163. }
  164. void ShaderRD::_compile_variant(uint32_t p_variant, Version *p_version) {
  165. if (!variants_enabled[p_variant]) {
  166. return; //variant is disabled, return
  167. }
  168. Vector<RD::ShaderStageData> stages;
  169. String error;
  170. String current_source;
  171. RD::ShaderStage current_stage = RD::SHADER_STAGE_VERTEX;
  172. bool build_ok = true;
  173. if (!is_compute) {
  174. //vertex stage
  175. StringBuilder builder;
  176. _build_variant_code(builder, p_variant, p_version, stage_templates[STAGE_TYPE_VERTEX]);
  177. current_source = builder.as_string();
  178. RD::ShaderStageData stage;
  179. stage.spir_v = RD::get_singleton()->shader_compile_from_source(RD::SHADER_STAGE_VERTEX, current_source, RD::SHADER_LANGUAGE_GLSL, &error);
  180. if (stage.spir_v.size() == 0) {
  181. build_ok = false;
  182. } else {
  183. stage.shader_stage = RD::SHADER_STAGE_VERTEX;
  184. stages.push_back(stage);
  185. }
  186. }
  187. if (!is_compute && build_ok) {
  188. //fragment stage
  189. current_stage = RD::SHADER_STAGE_FRAGMENT;
  190. StringBuilder builder;
  191. _build_variant_code(builder, p_variant, p_version, stage_templates[STAGE_TYPE_FRAGMENT]);
  192. current_source = builder.as_string();
  193. RD::ShaderStageData stage;
  194. stage.spir_v = RD::get_singleton()->shader_compile_from_source(RD::SHADER_STAGE_FRAGMENT, current_source, RD::SHADER_LANGUAGE_GLSL, &error);
  195. if (stage.spir_v.size() == 0) {
  196. build_ok = false;
  197. } else {
  198. stage.shader_stage = RD::SHADER_STAGE_FRAGMENT;
  199. stages.push_back(stage);
  200. }
  201. }
  202. if (is_compute) {
  203. //compute stage
  204. current_stage = RD::SHADER_STAGE_COMPUTE;
  205. StringBuilder builder;
  206. _build_variant_code(builder, p_variant, p_version, stage_templates[STAGE_TYPE_COMPUTE]);
  207. current_source = builder.as_string();
  208. RD::ShaderStageData stage;
  209. stage.spir_v = RD::get_singleton()->shader_compile_from_source(RD::SHADER_STAGE_COMPUTE, current_source, RD::SHADER_LANGUAGE_GLSL, &error);
  210. if (stage.spir_v.size() == 0) {
  211. build_ok = false;
  212. } else {
  213. stage.shader_stage = RD::SHADER_STAGE_COMPUTE;
  214. stages.push_back(stage);
  215. }
  216. }
  217. if (!build_ok) {
  218. MutexLock lock(variant_set_mutex); //properly print the errors
  219. 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() + ").");
  220. ERR_PRINT(error);
  221. #ifdef DEBUG_ENABLED
  222. ERR_PRINT("code:\n" + current_source.get_with_code_lines());
  223. #endif
  224. return;
  225. }
  226. RID shader = RD::get_singleton()->shader_create(stages);
  227. {
  228. MutexLock lock(variant_set_mutex);
  229. p_version->variants[p_variant] = shader;
  230. }
  231. }
  232. RS::ShaderNativeSourceCode ShaderRD::version_get_native_source_code(RID p_version) {
  233. Version *version = version_owner.getornull(p_version);
  234. RS::ShaderNativeSourceCode source_code;
  235. ERR_FAIL_COND_V(!version, source_code);
  236. source_code.versions.resize(variant_defines.size());
  237. for (int i = 0; i < source_code.versions.size(); i++) {
  238. if (!is_compute) {
  239. //vertex stage
  240. StringBuilder builder;
  241. _build_variant_code(builder, i, version, stage_templates[STAGE_TYPE_VERTEX]);
  242. RS::ShaderNativeSourceCode::Version::Stage stage;
  243. stage.name = "vertex";
  244. stage.code = builder.as_string();
  245. source_code.versions.write[i].stages.push_back(stage);
  246. }
  247. if (!is_compute) {
  248. //fragment stage
  249. StringBuilder builder;
  250. _build_variant_code(builder, i, version, stage_templates[STAGE_TYPE_FRAGMENT]);
  251. RS::ShaderNativeSourceCode::Version::Stage stage;
  252. stage.name = "fragment";
  253. stage.code = builder.as_string();
  254. source_code.versions.write[i].stages.push_back(stage);
  255. }
  256. if (is_compute) {
  257. //compute stage
  258. StringBuilder builder;
  259. _build_variant_code(builder, i, version, stage_templates[STAGE_TYPE_COMPUTE]);
  260. RS::ShaderNativeSourceCode::Version::Stage stage;
  261. stage.name = "compute";
  262. stage.code = builder.as_string();
  263. source_code.versions.write[i].stages.push_back(stage);
  264. }
  265. }
  266. return source_code;
  267. }
  268. void ShaderRD::_compile_version(Version *p_version) {
  269. _clear_version(p_version);
  270. p_version->valid = false;
  271. p_version->dirty = false;
  272. p_version->variants = memnew_arr(RID, variant_defines.size());
  273. #if 1
  274. RendererThreadPool::singleton->thread_work_pool.do_work(variant_defines.size(), this, &ShaderRD::_compile_variant, p_version);
  275. #else
  276. for (int i = 0; i < variant_defines.size(); i++) {
  277. _compile_variant(i, p_version);
  278. }
  279. #endif
  280. bool all_valid = true;
  281. for (int i = 0; i < variant_defines.size(); i++) {
  282. if (!variants_enabled[i]) {
  283. continue; //disabled
  284. }
  285. if (p_version->variants[i].is_null()) {
  286. all_valid = false;
  287. break;
  288. }
  289. }
  290. if (!all_valid) {
  291. //clear versions if they exist
  292. for (int i = 0; i < variant_defines.size(); i++) {
  293. if (!variants_enabled[i]) {
  294. continue; //disabled
  295. }
  296. if (!p_version->variants[i].is_null()) {
  297. RD::get_singleton()->free(p_version->variants[i]);
  298. }
  299. }
  300. memdelete_arr(p_version->variants);
  301. p_version->variants = nullptr;
  302. return;
  303. }
  304. p_version->valid = true;
  305. }
  306. 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) {
  307. ERR_FAIL_COND(is_compute);
  308. Version *version = version_owner.getornull(p_version);
  309. ERR_FAIL_COND(!version);
  310. version->vertex_globals = p_vertex_globals.utf8();
  311. version->fragment_globals = p_fragment_globals.utf8();
  312. version->uniforms = p_uniforms.utf8();
  313. version->code_sections.clear();
  314. for (Map<String, String>::Element *E = p_code.front(); E; E = E->next()) {
  315. version->code_sections[StringName(E->key().to_upper())] = E->get().utf8();
  316. }
  317. version->custom_defines.clear();
  318. for (int i = 0; i < p_custom_defines.size(); i++) {
  319. version->custom_defines.push_back(p_custom_defines[i].utf8());
  320. }
  321. version->dirty = true;
  322. if (version->initialize_needed) {
  323. _compile_version(version);
  324. version->initialize_needed = false;
  325. }
  326. }
  327. 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) {
  328. ERR_FAIL_COND(!is_compute);
  329. Version *version = version_owner.getornull(p_version);
  330. ERR_FAIL_COND(!version);
  331. version->compute_globals = p_compute_globals.utf8();
  332. version->uniforms = p_uniforms.utf8();
  333. version->code_sections.clear();
  334. for (Map<String, String>::Element *E = p_code.front(); E; E = E->next()) {
  335. version->code_sections[StringName(E->key().to_upper())] = E->get().utf8();
  336. }
  337. version->custom_defines.clear();
  338. for (int i = 0; i < p_custom_defines.size(); i++) {
  339. version->custom_defines.push_back(p_custom_defines[i].utf8());
  340. }
  341. version->dirty = true;
  342. if (version->initialize_needed) {
  343. _compile_version(version);
  344. version->initialize_needed = false;
  345. }
  346. }
  347. bool ShaderRD::version_is_valid(RID p_version) {
  348. Version *version = version_owner.getornull(p_version);
  349. ERR_FAIL_COND_V(!version, false);
  350. if (version->dirty) {
  351. _compile_version(version);
  352. }
  353. return version->valid;
  354. }
  355. bool ShaderRD::version_free(RID p_version) {
  356. if (version_owner.owns(p_version)) {
  357. Version *version = version_owner.getornull(p_version);
  358. _clear_version(version);
  359. version_owner.free(p_version);
  360. } else {
  361. return false;
  362. }
  363. return true;
  364. }
  365. void ShaderRD::set_variant_enabled(int p_variant, bool p_enabled) {
  366. ERR_FAIL_COND(version_owner.get_rid_count() > 0); //versions exist
  367. ERR_FAIL_INDEX(p_variant, variants_enabled.size());
  368. variants_enabled.write[p_variant] = p_enabled;
  369. }
  370. bool ShaderRD::is_variant_enabled(int p_variant) const {
  371. ERR_FAIL_INDEX_V(p_variant, variants_enabled.size(), false);
  372. return variants_enabled[p_variant];
  373. }
  374. ShaderRD::ShaderRD() {
  375. // Do not feel forced to use this, in most cases it makes little to no difference.
  376. bool use_32_threads = false;
  377. if (RD::get_singleton()->get_device_vendor_name() == "NVIDIA") {
  378. use_32_threads = true;
  379. }
  380. String base_compute_define_text;
  381. if (use_32_threads) {
  382. 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";
  383. } else {
  384. 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";
  385. }
  386. base_compute_defines = base_compute_define_text.ascii();
  387. }
  388. void ShaderRD::initialize(const Vector<String> &p_variant_defines, const String &p_general_defines) {
  389. ERR_FAIL_COND(variant_defines.size());
  390. ERR_FAIL_COND(p_variant_defines.size() == 0);
  391. general_defines = p_general_defines.utf8();
  392. for (int i = 0; i < p_variant_defines.size(); i++) {
  393. variant_defines.push_back(p_variant_defines[i].utf8());
  394. variants_enabled.push_back(true);
  395. }
  396. }
  397. ShaderRD::~ShaderRD() {
  398. List<RID> remaining;
  399. version_owner.get_owned_list(&remaining);
  400. if (remaining.size()) {
  401. ERR_PRINT(itos(remaining.size()) + " shaders of type " + name + " were never freed");
  402. while (remaining.size()) {
  403. version_free(remaining.front()->get());
  404. remaining.pop_front();
  405. }
  406. }
  407. }