shader_cache_gles3.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**************************************************************************/
  2. /* shader_cache_gles3.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "shader_cache_gles3.h"
  31. #include "core/crypto/crypto_core.h"
  32. #include "core/os/dir_access.h"
  33. #include "core/os/os.h"
  34. #include "core/project_settings.h"
  35. #include "core/sort_array.h"
  36. #include "core/ustring.h"
  37. String ShaderCacheGLES3::hash_program(const char *const *p_strings_platform, const LocalVector<const char *> &p_vertex_strings, const LocalVector<const char *> &p_fragment_strings) {
  38. CryptoCore::SHA256Context ctx;
  39. ctx.start();
  40. // GL may already reject a binary program if hardware/software has changed, but just in case
  41. for (const char *const *s = p_strings_platform; *s; s++) {
  42. uint8_t *bytes = reinterpret_cast<uint8_t *>(const_cast<char *>(*s));
  43. ctx.update(bytes, strlen(*s));
  44. }
  45. for (uint32_t i = 0; i < p_vertex_strings.size(); i++) {
  46. ctx.update((uint8_t *)p_vertex_strings[i], strlen(p_vertex_strings[i]));
  47. }
  48. for (uint32_t i = 0; i < p_fragment_strings.size(); i++) {
  49. ctx.update((uint8_t *)p_fragment_strings[i], strlen(p_fragment_strings[i]));
  50. }
  51. uint8_t hash[32];
  52. ctx.finish(hash);
  53. return String::hex_encode_buffer(hash, 32);
  54. }
  55. bool ShaderCacheGLES3::retrieve(const String &p_program_hash, uint32_t *r_format, PoolByteArray *r_data) {
  56. if (!storage_da) {
  57. return false;
  58. }
  59. FileAccessRef fa = FileAccess::open(storage_path.plus_file(p_program_hash), FileAccess::READ_WRITE);
  60. if (!fa) {
  61. return false;
  62. }
  63. *r_format = fa->get_32();
  64. uint32_t binary_len = fa->get_32();
  65. if (binary_len <= 0 || binary_len > 0x10000000) {
  66. ERR_PRINT("Program binary cache file is corrupted. Ignoring and removing.");
  67. fa->close();
  68. storage_da->remove(p_program_hash);
  69. return false;
  70. }
  71. r_data->resize(binary_len);
  72. PoolByteArray::Write w = r_data->write();
  73. if (fa->get_buffer(w.ptr(), binary_len) != static_cast<uint64_t>(binary_len)) {
  74. ERR_PRINT("Program binary cache file is truncated. Ignoring and removing.");
  75. fa->close();
  76. storage_da->remove(p_program_hash);
  77. return false;
  78. }
  79. // Force update modification time (for LRU purge)
  80. fa->seek(0);
  81. fa->store_32(*r_format);
  82. return true;
  83. }
  84. void ShaderCacheGLES3::store(const String &p_program_hash, uint32_t p_program_format, const PoolByteArray &p_program_data) {
  85. if (!storage_da) {
  86. return;
  87. }
  88. FileAccessRef fa = FileAccess::open(storage_path.plus_file(p_program_hash), FileAccess::WRITE);
  89. ERR_FAIL_COND(!fa);
  90. fa->store_32(p_program_format);
  91. fa->store_32(p_program_data.size());
  92. PoolByteArray::Read r = p_program_data.read();
  93. fa->store_buffer(r.ptr(), p_program_data.size());
  94. }
  95. void ShaderCacheGLES3::remove(const String &p_program_hash) {
  96. if (!storage_da) {
  97. return;
  98. }
  99. storage_da->remove(p_program_hash);
  100. }
  101. void ShaderCacheGLES3::_purge_excess() {
  102. if (!storage_da) {
  103. return;
  104. }
  105. struct Entry {
  106. String name;
  107. uint64_t timestamp;
  108. uint64_t size;
  109. bool operator<(const Entry &p_rhs) const {
  110. return timestamp < p_rhs.timestamp;
  111. }
  112. };
  113. LocalVector<Entry> entries;
  114. uint64_t total_size = 0;
  115. ERR_FAIL_COND(storage_da->list_dir_begin() != OK);
  116. while (true) {
  117. String f = storage_da->get_next();
  118. if (f == "") {
  119. break;
  120. }
  121. if (storage_da->current_is_dir()) {
  122. continue;
  123. }
  124. String path = storage_da->get_current_dir().plus_file(f);
  125. FileAccessRef fa = FileAccess::open(path, FileAccess::READ);
  126. ERR_CONTINUE(!fa);
  127. Entry entry;
  128. entry.name = f;
  129. entry.timestamp = FileAccess::get_modified_time(path);
  130. entry.size = fa->get_len();
  131. entries.push_back(entry);
  132. total_size += entry.size;
  133. }
  134. storage_da->list_dir_end();
  135. print_verbose("Shader cache size: " + itos(total_size / (1024 * 1024)) + " MiB (max. is " + (itos(storage_size / (1024 * 1024))) + " MiB)");
  136. if (total_size > storage_size) {
  137. print_verbose("Purging LRU from shader cache.");
  138. SortArray<Entry>().sort(entries.ptr(), entries.size());
  139. for (uint32_t i = 0; i < entries.size(); i++) {
  140. storage_da->remove(entries[i].name);
  141. total_size -= entries[i].size;
  142. if (total_size <= storage_size) {
  143. break;
  144. }
  145. }
  146. }
  147. }
  148. ShaderCacheGLES3::ShaderCacheGLES3() {
  149. storage_size = (int)GLOBAL_GET("rendering/gles3/shaders/shader_cache_size_mb") * 1024 * 1024;
  150. storage_da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  151. storage_path = OS::get_singleton()->get_cache_path().plus_file(OS::get_singleton()->get_godot_dir_name()).plus_file("shaders");
  152. print_verbose("Shader cache path: " + storage_path);
  153. if (storage_da->make_dir_recursive(storage_path) != OK) {
  154. ERR_PRINT("Couldn't create shader cache directory. Shader cache disabled.");
  155. memdelete(storage_da);
  156. storage_da = nullptr;
  157. return;
  158. }
  159. if (storage_da->change_dir(storage_path) != OK) {
  160. ERR_PRINT("Couldn't open shader cache directory. Shader cache disabled.");
  161. memdelete(storage_da);
  162. storage_da = nullptr;
  163. return;
  164. }
  165. _purge_excess();
  166. }
  167. ShaderCacheGLES3::~ShaderCacheGLES3() {
  168. if (storage_da) {
  169. memdelete(storage_da);
  170. }
  171. }