renderer_storage_rd.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*************************************************************************/
  2. /* renderer_storage_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 "renderer_storage_rd.h"
  31. #include "core/config/engine.h"
  32. #include "core/config/project_settings.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/math/math_defs.h"
  35. #include "renderer_compositor_rd.h"
  36. #include "servers/rendering/renderer_rd/storage_rd/light_storage.h"
  37. #include "servers/rendering/renderer_rd/storage_rd/mesh_storage.h"
  38. #include "servers/rendering/renderer_rd/storage_rd/particles_storage.h"
  39. #include "servers/rendering/renderer_rd/storage_rd/texture_storage.h"
  40. #include "servers/rendering/rendering_server_globals.h"
  41. #include "servers/rendering/shader_language.h"
  42. /* FOG VOLUMES */
  43. RID RendererStorageRD::fog_volume_allocate() {
  44. return fog_volume_owner.allocate_rid();
  45. }
  46. void RendererStorageRD::fog_volume_initialize(RID p_rid) {
  47. fog_volume_owner.initialize_rid(p_rid, FogVolume());
  48. }
  49. void RendererStorageRD::fog_volume_set_shape(RID p_fog_volume, RS::FogVolumeShape p_shape) {
  50. FogVolume *fog_volume = fog_volume_owner.get_or_null(p_fog_volume);
  51. ERR_FAIL_COND(!fog_volume);
  52. if (p_shape == fog_volume->shape) {
  53. return;
  54. }
  55. fog_volume->shape = p_shape;
  56. fog_volume->dependency.changed_notify(DEPENDENCY_CHANGED_AABB);
  57. }
  58. void RendererStorageRD::fog_volume_set_extents(RID p_fog_volume, const Vector3 &p_extents) {
  59. FogVolume *fog_volume = fog_volume_owner.get_or_null(p_fog_volume);
  60. ERR_FAIL_COND(!fog_volume);
  61. fog_volume->extents = p_extents;
  62. fog_volume->dependency.changed_notify(DEPENDENCY_CHANGED_AABB);
  63. }
  64. void RendererStorageRD::fog_volume_set_material(RID p_fog_volume, RID p_material) {
  65. FogVolume *fog_volume = fog_volume_owner.get_or_null(p_fog_volume);
  66. ERR_FAIL_COND(!fog_volume);
  67. fog_volume->material = p_material;
  68. }
  69. RID RendererStorageRD::fog_volume_get_material(RID p_fog_volume) const {
  70. FogVolume *fog_volume = fog_volume_owner.get_or_null(p_fog_volume);
  71. ERR_FAIL_COND_V(!fog_volume, RID());
  72. return fog_volume->material;
  73. }
  74. RS::FogVolumeShape RendererStorageRD::fog_volume_get_shape(RID p_fog_volume) const {
  75. FogVolume *fog_volume = fog_volume_owner.get_or_null(p_fog_volume);
  76. ERR_FAIL_COND_V(!fog_volume, RS::FOG_VOLUME_SHAPE_BOX);
  77. return fog_volume->shape;
  78. }
  79. AABB RendererStorageRD::fog_volume_get_aabb(RID p_fog_volume) const {
  80. FogVolume *fog_volume = fog_volume_owner.get_or_null(p_fog_volume);
  81. ERR_FAIL_COND_V(!fog_volume, AABB());
  82. switch (fog_volume->shape) {
  83. case RS::FOG_VOLUME_SHAPE_ELLIPSOID:
  84. case RS::FOG_VOLUME_SHAPE_CONE:
  85. case RS::FOG_VOLUME_SHAPE_CYLINDER:
  86. case RS::FOG_VOLUME_SHAPE_BOX: {
  87. AABB aabb;
  88. aabb.position = -fog_volume->extents;
  89. aabb.size = fog_volume->extents * 2;
  90. return aabb;
  91. }
  92. default: {
  93. // Need some size otherwise will get culled
  94. return AABB(Vector3(-1, -1, -1), Vector3(2, 2, 2));
  95. }
  96. }
  97. return AABB();
  98. }
  99. Vector3 RendererStorageRD::fog_volume_get_extents(RID p_fog_volume) const {
  100. const FogVolume *fog_volume = fog_volume_owner.get_or_null(p_fog_volume);
  101. ERR_FAIL_COND_V(!fog_volume, Vector3());
  102. return fog_volume->extents;
  103. }
  104. /* VISIBILITY NOTIFIER */
  105. RID RendererStorageRD::visibility_notifier_allocate() {
  106. return visibility_notifier_owner.allocate_rid();
  107. }
  108. void RendererStorageRD::visibility_notifier_initialize(RID p_notifier) {
  109. visibility_notifier_owner.initialize_rid(p_notifier, VisibilityNotifier());
  110. }
  111. void RendererStorageRD::visibility_notifier_set_aabb(RID p_notifier, const AABB &p_aabb) {
  112. VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
  113. ERR_FAIL_COND(!vn);
  114. vn->aabb = p_aabb;
  115. vn->dependency.changed_notify(DEPENDENCY_CHANGED_AABB);
  116. }
  117. void RendererStorageRD::visibility_notifier_set_callbacks(RID p_notifier, const Callable &p_enter_callbable, const Callable &p_exit_callable) {
  118. VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
  119. ERR_FAIL_COND(!vn);
  120. vn->enter_callback = p_enter_callbable;
  121. vn->exit_callback = p_exit_callable;
  122. }
  123. AABB RendererStorageRD::visibility_notifier_get_aabb(RID p_notifier) const {
  124. const VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
  125. ERR_FAIL_COND_V(!vn, AABB());
  126. return vn->aabb;
  127. }
  128. void RendererStorageRD::visibility_notifier_call(RID p_notifier, bool p_enter, bool p_deferred) {
  129. VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_notifier);
  130. ERR_FAIL_COND(!vn);
  131. if (p_enter) {
  132. if (!vn->enter_callback.is_null()) {
  133. if (p_deferred) {
  134. vn->enter_callback.call_deferred(nullptr, 0);
  135. } else {
  136. Variant r;
  137. Callable::CallError ce;
  138. vn->enter_callback.call(nullptr, 0, r, ce);
  139. }
  140. }
  141. } else {
  142. if (!vn->exit_callback.is_null()) {
  143. if (p_deferred) {
  144. vn->exit_callback.call_deferred(nullptr, 0);
  145. } else {
  146. Variant r;
  147. Callable::CallError ce;
  148. vn->exit_callback.call(nullptr, 0, r, ce);
  149. }
  150. }
  151. }
  152. }
  153. /* VOXEL GI */
  154. RID RendererStorageRD::voxel_gi_allocate() {
  155. return voxel_gi_owner.allocate_rid();
  156. }
  157. void RendererStorageRD::voxel_gi_initialize(RID p_voxel_gi) {
  158. voxel_gi_owner.initialize_rid(p_voxel_gi, VoxelGI());
  159. }
  160. void RendererStorageRD::voxel_gi_allocate_data(RID p_voxel_gi, const Transform3D &p_to_cell_xform, const AABB &p_aabb, const Vector3i &p_octree_size, const Vector<uint8_t> &p_octree_cells, const Vector<uint8_t> &p_data_cells, const Vector<uint8_t> &p_distance_field, const Vector<int> &p_level_counts) {
  161. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  162. ERR_FAIL_COND(!voxel_gi);
  163. if (voxel_gi->octree_buffer.is_valid()) {
  164. RD::get_singleton()->free(voxel_gi->octree_buffer);
  165. RD::get_singleton()->free(voxel_gi->data_buffer);
  166. if (voxel_gi->sdf_texture.is_valid()) {
  167. RD::get_singleton()->free(voxel_gi->sdf_texture);
  168. }
  169. voxel_gi->sdf_texture = RID();
  170. voxel_gi->octree_buffer = RID();
  171. voxel_gi->data_buffer = RID();
  172. voxel_gi->octree_buffer_size = 0;
  173. voxel_gi->data_buffer_size = 0;
  174. voxel_gi->cell_count = 0;
  175. }
  176. voxel_gi->to_cell_xform = p_to_cell_xform;
  177. voxel_gi->bounds = p_aabb;
  178. voxel_gi->octree_size = p_octree_size;
  179. voxel_gi->level_counts = p_level_counts;
  180. if (p_octree_cells.size()) {
  181. ERR_FAIL_COND(p_octree_cells.size() % 32 != 0); //cells size must be a multiple of 32
  182. uint32_t cell_count = p_octree_cells.size() / 32;
  183. ERR_FAIL_COND(p_data_cells.size() != (int)cell_count * 16); //see that data size matches
  184. voxel_gi->cell_count = cell_count;
  185. voxel_gi->octree_buffer = RD::get_singleton()->storage_buffer_create(p_octree_cells.size(), p_octree_cells);
  186. voxel_gi->octree_buffer_size = p_octree_cells.size();
  187. voxel_gi->data_buffer = RD::get_singleton()->storage_buffer_create(p_data_cells.size(), p_data_cells);
  188. voxel_gi->data_buffer_size = p_data_cells.size();
  189. if (p_distance_field.size()) {
  190. RD::TextureFormat tf;
  191. tf.format = RD::DATA_FORMAT_R8_UNORM;
  192. tf.width = voxel_gi->octree_size.x;
  193. tf.height = voxel_gi->octree_size.y;
  194. tf.depth = voxel_gi->octree_size.z;
  195. tf.texture_type = RD::TEXTURE_TYPE_3D;
  196. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_CAN_UPDATE_BIT | RD::TEXTURE_USAGE_CAN_COPY_FROM_BIT;
  197. Vector<Vector<uint8_t>> s;
  198. s.push_back(p_distance_field);
  199. voxel_gi->sdf_texture = RD::get_singleton()->texture_create(tf, RD::TextureView(), s);
  200. }
  201. #if 0
  202. {
  203. RD::TextureFormat tf;
  204. tf.format = RD::DATA_FORMAT_R8_UNORM;
  205. tf.width = voxel_gi->octree_size.x;
  206. tf.height = voxel_gi->octree_size.y;
  207. tf.depth = voxel_gi->octree_size.z;
  208. tf.type = RD::TEXTURE_TYPE_3D;
  209. tf.usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT | RD::TEXTURE_USAGE_CAN_COPY_TO_BIT;
  210. tf.shareable_formats.push_back(RD::DATA_FORMAT_R8_UNORM);
  211. tf.shareable_formats.push_back(RD::DATA_FORMAT_R8_UINT);
  212. voxel_gi->sdf_texture = RD::get_singleton()->texture_create(tf, RD::TextureView());
  213. }
  214. RID shared_tex;
  215. {
  216. RD::TextureView tv;
  217. tv.format_override = RD::DATA_FORMAT_R8_UINT;
  218. shared_tex = RD::get_singleton()->texture_create_shared(tv, voxel_gi->sdf_texture);
  219. }
  220. //update SDF texture
  221. Vector<RD::Uniform> uniforms;
  222. {
  223. RD::Uniform u;
  224. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  225. u.binding = 1;
  226. u.append_id(voxel_gi->octree_buffer);
  227. uniforms.push_back(u);
  228. }
  229. {
  230. RD::Uniform u;
  231. u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER;
  232. u.binding = 2;
  233. u.append_id(voxel_gi->data_buffer);
  234. uniforms.push_back(u);
  235. }
  236. {
  237. RD::Uniform u;
  238. u.uniform_type = RD::UNIFORM_TYPE_IMAGE;
  239. u.binding = 3;
  240. u.append_id(shared_tex);
  241. uniforms.push_back(u);
  242. }
  243. RID uniform_set = RD::get_singleton()->uniform_set_create(uniforms, voxel_gi_sdf_shader_version_shader, 0);
  244. {
  245. uint32_t push_constant[4] = { 0, 0, 0, 0 };
  246. for (int i = 0; i < voxel_gi->level_counts.size() - 1; i++) {
  247. push_constant[0] += voxel_gi->level_counts[i];
  248. }
  249. push_constant[1] = push_constant[0] + voxel_gi->level_counts[voxel_gi->level_counts.size() - 1];
  250. print_line("offset: " + itos(push_constant[0]));
  251. print_line("size: " + itos(push_constant[1]));
  252. //create SDF
  253. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  254. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, voxel_gi_sdf_shader_pipeline);
  255. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set, 0);
  256. RD::get_singleton()->compute_list_set_push_constant(compute_list, push_constant, sizeof(uint32_t) * 4);
  257. RD::get_singleton()->compute_list_dispatch(compute_list, voxel_gi->octree_size.x / 4, voxel_gi->octree_size.y / 4, voxel_gi->octree_size.z / 4);
  258. RD::get_singleton()->compute_list_end();
  259. }
  260. RD::get_singleton()->free(uniform_set);
  261. RD::get_singleton()->free(shared_tex);
  262. }
  263. #endif
  264. }
  265. voxel_gi->version++;
  266. voxel_gi->data_version++;
  267. voxel_gi->dependency.changed_notify(DEPENDENCY_CHANGED_AABB);
  268. }
  269. AABB RendererStorageRD::voxel_gi_get_bounds(RID p_voxel_gi) const {
  270. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  271. ERR_FAIL_COND_V(!voxel_gi, AABB());
  272. return voxel_gi->bounds;
  273. }
  274. Vector3i RendererStorageRD::voxel_gi_get_octree_size(RID p_voxel_gi) const {
  275. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  276. ERR_FAIL_COND_V(!voxel_gi, Vector3i());
  277. return voxel_gi->octree_size;
  278. }
  279. Vector<uint8_t> RendererStorageRD::voxel_gi_get_octree_cells(RID p_voxel_gi) const {
  280. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  281. ERR_FAIL_COND_V(!voxel_gi, Vector<uint8_t>());
  282. if (voxel_gi->octree_buffer.is_valid()) {
  283. return RD::get_singleton()->buffer_get_data(voxel_gi->octree_buffer);
  284. }
  285. return Vector<uint8_t>();
  286. }
  287. Vector<uint8_t> RendererStorageRD::voxel_gi_get_data_cells(RID p_voxel_gi) const {
  288. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  289. ERR_FAIL_COND_V(!voxel_gi, Vector<uint8_t>());
  290. if (voxel_gi->data_buffer.is_valid()) {
  291. return RD::get_singleton()->buffer_get_data(voxel_gi->data_buffer);
  292. }
  293. return Vector<uint8_t>();
  294. }
  295. Vector<uint8_t> RendererStorageRD::voxel_gi_get_distance_field(RID p_voxel_gi) const {
  296. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  297. ERR_FAIL_COND_V(!voxel_gi, Vector<uint8_t>());
  298. if (voxel_gi->data_buffer.is_valid()) {
  299. return RD::get_singleton()->texture_get_data(voxel_gi->sdf_texture, 0);
  300. }
  301. return Vector<uint8_t>();
  302. }
  303. Vector<int> RendererStorageRD::voxel_gi_get_level_counts(RID p_voxel_gi) const {
  304. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  305. ERR_FAIL_COND_V(!voxel_gi, Vector<int>());
  306. return voxel_gi->level_counts;
  307. }
  308. Transform3D RendererStorageRD::voxel_gi_get_to_cell_xform(RID p_voxel_gi) const {
  309. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  310. ERR_FAIL_COND_V(!voxel_gi, Transform3D());
  311. return voxel_gi->to_cell_xform;
  312. }
  313. void RendererStorageRD::voxel_gi_set_dynamic_range(RID p_voxel_gi, float p_range) {
  314. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  315. ERR_FAIL_COND(!voxel_gi);
  316. voxel_gi->dynamic_range = p_range;
  317. voxel_gi->version++;
  318. }
  319. float RendererStorageRD::voxel_gi_get_dynamic_range(RID p_voxel_gi) const {
  320. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  321. ERR_FAIL_COND_V(!voxel_gi, 0);
  322. return voxel_gi->dynamic_range;
  323. }
  324. void RendererStorageRD::voxel_gi_set_propagation(RID p_voxel_gi, float p_range) {
  325. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  326. ERR_FAIL_COND(!voxel_gi);
  327. voxel_gi->propagation = p_range;
  328. voxel_gi->version++;
  329. }
  330. float RendererStorageRD::voxel_gi_get_propagation(RID p_voxel_gi) const {
  331. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  332. ERR_FAIL_COND_V(!voxel_gi, 0);
  333. return voxel_gi->propagation;
  334. }
  335. void RendererStorageRD::voxel_gi_set_energy(RID p_voxel_gi, float p_energy) {
  336. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  337. ERR_FAIL_COND(!voxel_gi);
  338. voxel_gi->energy = p_energy;
  339. }
  340. float RendererStorageRD::voxel_gi_get_energy(RID p_voxel_gi) const {
  341. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  342. ERR_FAIL_COND_V(!voxel_gi, 0);
  343. return voxel_gi->energy;
  344. }
  345. void RendererStorageRD::voxel_gi_set_bias(RID p_voxel_gi, float p_bias) {
  346. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  347. ERR_FAIL_COND(!voxel_gi);
  348. voxel_gi->bias = p_bias;
  349. }
  350. float RendererStorageRD::voxel_gi_get_bias(RID p_voxel_gi) const {
  351. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  352. ERR_FAIL_COND_V(!voxel_gi, 0);
  353. return voxel_gi->bias;
  354. }
  355. void RendererStorageRD::voxel_gi_set_normal_bias(RID p_voxel_gi, float p_normal_bias) {
  356. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  357. ERR_FAIL_COND(!voxel_gi);
  358. voxel_gi->normal_bias = p_normal_bias;
  359. }
  360. float RendererStorageRD::voxel_gi_get_normal_bias(RID p_voxel_gi) const {
  361. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  362. ERR_FAIL_COND_V(!voxel_gi, 0);
  363. return voxel_gi->normal_bias;
  364. }
  365. void RendererStorageRD::voxel_gi_set_anisotropy_strength(RID p_voxel_gi, float p_strength) {
  366. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  367. ERR_FAIL_COND(!voxel_gi);
  368. voxel_gi->anisotropy_strength = p_strength;
  369. }
  370. float RendererStorageRD::voxel_gi_get_anisotropy_strength(RID p_voxel_gi) const {
  371. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  372. ERR_FAIL_COND_V(!voxel_gi, 0);
  373. return voxel_gi->anisotropy_strength;
  374. }
  375. void RendererStorageRD::voxel_gi_set_interior(RID p_voxel_gi, bool p_enable) {
  376. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  377. ERR_FAIL_COND(!voxel_gi);
  378. voxel_gi->interior = p_enable;
  379. }
  380. void RendererStorageRD::voxel_gi_set_use_two_bounces(RID p_voxel_gi, bool p_enable) {
  381. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  382. ERR_FAIL_COND(!voxel_gi);
  383. voxel_gi->use_two_bounces = p_enable;
  384. voxel_gi->version++;
  385. }
  386. bool RendererStorageRD::voxel_gi_is_using_two_bounces(RID p_voxel_gi) const {
  387. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  388. ERR_FAIL_COND_V(!voxel_gi, false);
  389. return voxel_gi->use_two_bounces;
  390. }
  391. bool RendererStorageRD::voxel_gi_is_interior(RID p_voxel_gi) const {
  392. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  393. ERR_FAIL_COND_V(!voxel_gi, 0);
  394. return voxel_gi->interior;
  395. }
  396. uint32_t RendererStorageRD::voxel_gi_get_version(RID p_voxel_gi) {
  397. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  398. ERR_FAIL_COND_V(!voxel_gi, 0);
  399. return voxel_gi->version;
  400. }
  401. uint32_t RendererStorageRD::voxel_gi_get_data_version(RID p_voxel_gi) {
  402. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  403. ERR_FAIL_COND_V(!voxel_gi, 0);
  404. return voxel_gi->data_version;
  405. }
  406. RID RendererStorageRD::voxel_gi_get_octree_buffer(RID p_voxel_gi) const {
  407. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  408. ERR_FAIL_COND_V(!voxel_gi, RID());
  409. return voxel_gi->octree_buffer;
  410. }
  411. RID RendererStorageRD::voxel_gi_get_data_buffer(RID p_voxel_gi) const {
  412. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  413. ERR_FAIL_COND_V(!voxel_gi, RID());
  414. return voxel_gi->data_buffer;
  415. }
  416. RID RendererStorageRD::voxel_gi_get_sdf_texture(RID p_voxel_gi) {
  417. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_voxel_gi);
  418. ERR_FAIL_COND_V(!voxel_gi, RID());
  419. return voxel_gi->sdf_texture;
  420. }
  421. /* misc */
  422. void RendererStorageRD::base_update_dependency(RID p_base, DependencyTracker *p_instance) {
  423. if (RendererRD::MeshStorage::get_singleton()->owns_mesh(p_base)) {
  424. RendererRD::Mesh *mesh = RendererRD::MeshStorage::get_singleton()->get_mesh(p_base);
  425. p_instance->update_dependency(&mesh->dependency);
  426. } else if (RendererRD::MeshStorage::get_singleton()->owns_multimesh(p_base)) {
  427. RendererRD::MultiMesh *multimesh = RendererRD::MeshStorage::get_singleton()->get_multimesh(p_base);
  428. p_instance->update_dependency(&multimesh->dependency);
  429. if (multimesh->mesh.is_valid()) {
  430. base_update_dependency(multimesh->mesh, p_instance);
  431. }
  432. } else if (RendererRD::LightStorage::get_singleton()->owns_reflection_probe(p_base)) {
  433. RendererRD::ReflectionProbe *rp = RendererRD::LightStorage::get_singleton()->get_reflection_probe(p_base);
  434. p_instance->update_dependency(&rp->dependency);
  435. } else if (RendererRD::TextureStorage::get_singleton()->owns_decal(p_base)) {
  436. RendererRD::Decal *decal = RendererRD::TextureStorage::get_singleton()->get_decal(p_base);
  437. p_instance->update_dependency(&decal->dependency);
  438. } else if (voxel_gi_owner.owns(p_base)) {
  439. VoxelGI *gip = voxel_gi_owner.get_or_null(p_base);
  440. p_instance->update_dependency(&gip->dependency);
  441. } else if (RendererRD::LightStorage::get_singleton()->owns_lightmap(p_base)) {
  442. RendererRD::Lightmap *lm = RendererRD::LightStorage::get_singleton()->get_lightmap(p_base);
  443. p_instance->update_dependency(&lm->dependency);
  444. } else if (RendererRD::LightStorage::get_singleton()->owns_light(p_base)) {
  445. RendererRD::Light *l = RendererRD::LightStorage::get_singleton()->get_light(p_base);
  446. p_instance->update_dependency(&l->dependency);
  447. } else if (RendererRD::ParticlesStorage::get_singleton()->owns_particles(p_base)) {
  448. RendererRD::Particles *p = RendererRD::ParticlesStorage::get_singleton()->get_particles(p_base);
  449. p_instance->update_dependency(&p->dependency);
  450. } else if (RendererRD::ParticlesStorage::get_singleton()->owns_particles_collision(p_base)) {
  451. RendererRD::ParticlesCollision *pc = RendererRD::ParticlesStorage::get_singleton()->get_particles_collision(p_base);
  452. p_instance->update_dependency(&pc->dependency);
  453. } else if (fog_volume_owner.owns(p_base)) {
  454. FogVolume *fv = fog_volume_owner.get_or_null(p_base);
  455. p_instance->update_dependency(&fv->dependency);
  456. } else if (visibility_notifier_owner.owns(p_base)) {
  457. VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_base);
  458. p_instance->update_dependency(&vn->dependency);
  459. }
  460. }
  461. RS::InstanceType RendererStorageRD::get_base_type(RID p_rid) const {
  462. if (RendererRD::MeshStorage::get_singleton()->owns_mesh(p_rid)) {
  463. return RS::INSTANCE_MESH;
  464. }
  465. if (RendererRD::MeshStorage::get_singleton()->owns_multimesh(p_rid)) {
  466. return RS::INSTANCE_MULTIMESH;
  467. }
  468. if (RendererRD::LightStorage::get_singleton()->owns_reflection_probe(p_rid)) {
  469. return RS::INSTANCE_REFLECTION_PROBE;
  470. }
  471. if (RendererRD::TextureStorage::get_singleton()->owns_decal(p_rid)) {
  472. return RS::INSTANCE_DECAL;
  473. }
  474. if (voxel_gi_owner.owns(p_rid)) {
  475. return RS::INSTANCE_VOXEL_GI;
  476. }
  477. if (RendererRD::LightStorage::get_singleton()->owns_light(p_rid)) {
  478. return RS::INSTANCE_LIGHT;
  479. }
  480. if (RendererRD::LightStorage::get_singleton()->owns_lightmap(p_rid)) {
  481. return RS::INSTANCE_LIGHTMAP;
  482. }
  483. if (RendererRD::ParticlesStorage::get_singleton()->owns_particles(p_rid)) {
  484. return RS::INSTANCE_PARTICLES;
  485. }
  486. if (RendererRD::ParticlesStorage::get_singleton()->owns_particles_collision(p_rid)) {
  487. return RS::INSTANCE_PARTICLES_COLLISION;
  488. }
  489. if (fog_volume_owner.owns(p_rid)) {
  490. return RS::INSTANCE_FOG_VOLUME;
  491. }
  492. if (visibility_notifier_owner.owns(p_rid)) {
  493. return RS::INSTANCE_VISIBLITY_NOTIFIER;
  494. }
  495. return RS::INSTANCE_NONE;
  496. }
  497. void RendererStorageRD::update_dirty_resources() {
  498. RendererRD::MaterialStorage::get_singleton()->_update_global_variables(); //must do before materials, so it can queue them for update
  499. RendererRD::MaterialStorage::get_singleton()->_update_queued_materials();
  500. RendererRD::MeshStorage::get_singleton()->_update_dirty_multimeshes();
  501. RendererRD::MeshStorage::get_singleton()->_update_dirty_skeletons();
  502. RendererRD::TextureStorage::get_singleton()->update_decal_atlas();
  503. }
  504. bool RendererStorageRD::has_os_feature(const String &p_feature) const {
  505. if (!RD::get_singleton()) {
  506. return false;
  507. }
  508. if (p_feature == "rgtc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC5_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  509. return true;
  510. }
  511. if (p_feature == "s3tc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC1_RGB_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  512. return true;
  513. }
  514. if (p_feature == "bptc" && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_BC7_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  515. return true;
  516. }
  517. if ((p_feature == "etc" || p_feature == "etc2") && RD::get_singleton()->texture_is_format_supported_for_usage(RD::DATA_FORMAT_ETC2_R8G8B8_UNORM_BLOCK, RD::TEXTURE_USAGE_SAMPLING_BIT)) {
  518. return true;
  519. }
  520. return false;
  521. }
  522. bool RendererStorageRD::free(RID p_rid) {
  523. if (RendererRD::TextureStorage::get_singleton()->owns_texture(p_rid)) {
  524. RendererRD::TextureStorage::get_singleton()->texture_free(p_rid);
  525. } else if (RendererRD::TextureStorage::get_singleton()->owns_canvas_texture(p_rid)) {
  526. RendererRD::TextureStorage::get_singleton()->canvas_texture_free(p_rid);
  527. } else if (RendererRD::MaterialStorage::get_singleton()->owns_shader(p_rid)) {
  528. RendererRD::MaterialStorage::get_singleton()->shader_free(p_rid);
  529. } else if (RendererRD::MaterialStorage::get_singleton()->owns_material(p_rid)) {
  530. RendererRD::MaterialStorage::get_singleton()->material_free(p_rid);
  531. } else if (RendererRD::MeshStorage::get_singleton()->owns_mesh(p_rid)) {
  532. RendererRD::MeshStorage::get_singleton()->mesh_free(p_rid);
  533. } else if (RendererRD::MeshStorage::get_singleton()->owns_mesh_instance(p_rid)) {
  534. RendererRD::MeshStorage::get_singleton()->mesh_instance_free(p_rid);
  535. } else if (RendererRD::MeshStorage::get_singleton()->owns_multimesh(p_rid)) {
  536. RendererRD::MeshStorage::get_singleton()->multimesh_free(p_rid);
  537. } else if (RendererRD::MeshStorage::get_singleton()->owns_skeleton(p_rid)) {
  538. RendererRD::MeshStorage::get_singleton()->skeleton_free(p_rid);
  539. } else if (RendererRD::LightStorage::get_singleton()->owns_reflection_probe(p_rid)) {
  540. RendererRD::LightStorage::get_singleton()->reflection_probe_free(p_rid);
  541. } else if (RendererRD::TextureStorage::get_singleton()->owns_decal(p_rid)) {
  542. RendererRD::TextureStorage::get_singleton()->decal_free(p_rid);
  543. } else if (voxel_gi_owner.owns(p_rid)) {
  544. voxel_gi_allocate_data(p_rid, Transform3D(), AABB(), Vector3i(), Vector<uint8_t>(), Vector<uint8_t>(), Vector<uint8_t>(), Vector<int>()); //deallocate
  545. VoxelGI *voxel_gi = voxel_gi_owner.get_or_null(p_rid);
  546. voxel_gi->dependency.deleted_notify(p_rid);
  547. voxel_gi_owner.free(p_rid);
  548. } else if (RendererRD::LightStorage::get_singleton()->owns_lightmap(p_rid)) {
  549. RendererRD::LightStorage::get_singleton()->lightmap_free(p_rid);
  550. } else if (RendererRD::LightStorage::get_singleton()->owns_light(p_rid)) {
  551. RendererRD::LightStorage::get_singleton()->light_free(p_rid);
  552. } else if (RendererRD::ParticlesStorage::get_singleton()->owns_particles(p_rid)) {
  553. RendererRD::ParticlesStorage::get_singleton()->particles_free(p_rid);
  554. } else if (RendererRD::ParticlesStorage::get_singleton()->owns_particles_collision(p_rid)) {
  555. RendererRD::ParticlesStorage::get_singleton()->particles_collision_free(p_rid);
  556. } else if (visibility_notifier_owner.owns(p_rid)) {
  557. VisibilityNotifier *vn = visibility_notifier_owner.get_or_null(p_rid);
  558. vn->dependency.deleted_notify(p_rid);
  559. visibility_notifier_owner.free(p_rid);
  560. } else if (RendererRD::ParticlesStorage::get_singleton()->owns_particles_collision_instance(p_rid)) {
  561. RendererRD::ParticlesStorage::get_singleton()->particles_collision_instance_free(p_rid);
  562. } else if (fog_volume_owner.owns(p_rid)) {
  563. FogVolume *fog_volume = fog_volume_owner.get_or_null(p_rid);
  564. fog_volume->dependency.deleted_notify(p_rid);
  565. fog_volume_owner.free(p_rid);
  566. } else if (RendererRD::TextureStorage::get_singleton()->owns_render_target(p_rid)) {
  567. RendererRD::TextureStorage::get_singleton()->render_target_free(p_rid);
  568. } else {
  569. return false;
  570. }
  571. return true;
  572. }
  573. void RendererStorageRD::init_effects(bool p_prefer_raster_effects) {
  574. effects = memnew(EffectsRD(p_prefer_raster_effects));
  575. }
  576. EffectsRD *RendererStorageRD::get_effects() {
  577. ERR_FAIL_NULL_V_MSG(effects, nullptr, "Effects haven't been initialised yet.");
  578. return effects;
  579. }
  580. void RendererStorageRD::capture_timestamps_begin() {
  581. RD::get_singleton()->capture_timestamp("Frame Begin");
  582. }
  583. void RendererStorageRD::capture_timestamp(const String &p_name) {
  584. RD::get_singleton()->capture_timestamp(p_name);
  585. }
  586. uint32_t RendererStorageRD::get_captured_timestamps_count() const {
  587. return RD::get_singleton()->get_captured_timestamps_count();
  588. }
  589. uint64_t RendererStorageRD::get_captured_timestamps_frame() const {
  590. return RD::get_singleton()->get_captured_timestamps_frame();
  591. }
  592. uint64_t RendererStorageRD::get_captured_timestamp_gpu_time(uint32_t p_index) const {
  593. return RD::get_singleton()->get_captured_timestamp_gpu_time(p_index);
  594. }
  595. uint64_t RendererStorageRD::get_captured_timestamp_cpu_time(uint32_t p_index) const {
  596. return RD::get_singleton()->get_captured_timestamp_cpu_time(p_index);
  597. }
  598. String RendererStorageRD::get_captured_timestamp_name(uint32_t p_index) const {
  599. return RD::get_singleton()->get_captured_timestamp_name(p_index);
  600. }
  601. void RendererStorageRD::update_memory_info() {
  602. texture_mem_cache = RenderingDevice::get_singleton()->get_memory_usage(RenderingDevice::MEMORY_TEXTURES);
  603. buffer_mem_cache = RenderingDevice::get_singleton()->get_memory_usage(RenderingDevice::MEMORY_BUFFERS);
  604. total_mem_cache = RenderingDevice::get_singleton()->get_memory_usage(RenderingDevice::MEMORY_TOTAL);
  605. }
  606. uint64_t RendererStorageRD::get_rendering_info(RS::RenderingInfo p_info) {
  607. if (p_info == RS::RENDERING_INFO_TEXTURE_MEM_USED) {
  608. return texture_mem_cache;
  609. } else if (p_info == RS::RENDERING_INFO_BUFFER_MEM_USED) {
  610. return buffer_mem_cache;
  611. } else if (p_info == RS::RENDERING_INFO_VIDEO_MEM_USED) {
  612. return total_mem_cache;
  613. }
  614. return 0;
  615. }
  616. String RendererStorageRD::get_video_adapter_name() const {
  617. return RenderingDevice::get_singleton()->get_device_name();
  618. }
  619. String RendererStorageRD::get_video_adapter_vendor() const {
  620. return RenderingDevice::get_singleton()->get_device_vendor_name();
  621. }
  622. RenderingDevice::DeviceType RendererStorageRD::get_video_adapter_type() const {
  623. return RenderingDevice::get_singleton()->get_device_type();
  624. }
  625. String RendererStorageRD::get_video_adapter_api_version() const {
  626. return RenderingDevice::get_singleton()->get_device_api_version();
  627. }
  628. RendererStorageRD *RendererStorageRD::base_singleton = nullptr;
  629. RendererStorageRD::RendererStorageRD() {
  630. base_singleton = this;
  631. }
  632. RendererStorageRD::~RendererStorageRD() {
  633. if (effects) {
  634. memdelete(effects);
  635. effects = nullptr;
  636. }
  637. }