debug_effects.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /**************************************************************************/
  2. /* debug_effects.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 "debug_effects.h"
  31. #include "servers/rendering/renderer_rd/renderer_compositor_rd.h"
  32. #include "servers/rendering/renderer_rd/storage_rd/light_storage.h"
  33. #include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
  34. #include "servers/rendering/renderer_rd/uniform_set_cache_rd.h"
  35. using namespace RendererRD;
  36. DebugEffects::DebugEffects() {
  37. {
  38. // Shadow Frustum debug shader
  39. Vector<String> modes;
  40. modes.push_back("");
  41. shadow_frustum.shader.initialize(modes);
  42. shadow_frustum.shader_version = shadow_frustum.shader.version_create();
  43. RD::PipelineRasterizationState raster_state = RD::PipelineRasterizationState();
  44. shadow_frustum.pipelines[SFP_TRANSPARENT].setup(shadow_frustum.shader.version_get_shader(shadow_frustum.shader_version, 0), RD::RENDER_PRIMITIVE_TRIANGLES, raster_state, RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_blend(), 0);
  45. raster_state.wireframe = true;
  46. shadow_frustum.pipelines[SFP_WIREFRAME].setup(shadow_frustum.shader.version_get_shader(shadow_frustum.shader_version, 0), RD::RENDER_PRIMITIVE_LINES, raster_state, RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_disabled(), 0);
  47. }
  48. {
  49. // Motion Vectors debug shader.
  50. Vector<String> modes;
  51. modes.push_back("");
  52. motion_vectors.shader.initialize(modes);
  53. motion_vectors.shader_version = motion_vectors.shader.version_create();
  54. motion_vectors.pipeline.setup(motion_vectors.shader.version_get_shader(motion_vectors.shader_version, 0), RD::RENDER_PRIMITIVE_TRIANGLES, RD::PipelineRasterizationState(), RD::PipelineMultisampleState(), RD::PipelineDepthStencilState(), RD::PipelineColorBlendState::create_blend(), 0);
  55. }
  56. }
  57. void DebugEffects::_create_frustum_arrays() {
  58. if (frustum.vertex_buffer.is_null()) {
  59. // Create vertex buffer, but don't put data in it yet
  60. frustum.vertex_buffer = RD::get_singleton()->vertex_buffer_create(8 * sizeof(float) * 3, Vector<uint8_t>(), false);
  61. Vector<RD::VertexAttribute> attributes;
  62. Vector<RID> buffers;
  63. RD::VertexAttribute vd;
  64. vd.location = 0;
  65. vd.stride = sizeof(float) * 3;
  66. vd.format = RD::DATA_FORMAT_R32G32B32_SFLOAT;
  67. attributes.push_back(vd);
  68. buffers.push_back(frustum.vertex_buffer);
  69. frustum.vertex_format = RD::get_singleton()->vertex_format_create(attributes);
  70. frustum.vertex_array = RD::get_singleton()->vertex_array_create(8, frustum.vertex_format, buffers);
  71. }
  72. if (frustum.index_buffer.is_null()) {
  73. uint16_t indices[6 * 2 * 3] = {
  74. // Far
  75. 0, 1, 2, // FLT, FLB, FRT
  76. 1, 3, 2, // FLB, FRB, FRT
  77. // Near
  78. 4, 6, 5, // NLT, NRT, NLB
  79. 6, 7, 5, // NRT, NRB, NLB
  80. // Left
  81. 0, 4, 1, // FLT, NLT, FLB
  82. 4, 5, 1, // NLT, NLB, FLB
  83. // Right
  84. 6, 2, 7, // NRT, FRT, NRB
  85. 2, 3, 7, // FRT, FRB, NRB
  86. // Top
  87. 0, 2, 4, // FLT, FRT, NLT
  88. 2, 6, 4, // FRT, NRT, NLT
  89. // Bottom
  90. 5, 7, 1, // NLB, NRB, FLB,
  91. 7, 3, 1, // NRB, FRB, FLB
  92. };
  93. // Create our index_array
  94. PackedByteArray data;
  95. data.resize(6 * 2 * 3 * 2);
  96. {
  97. uint8_t *w = data.ptrw();
  98. uint16_t *p16 = (uint16_t *)w;
  99. for (int i = 0; i < 6 * 2 * 3; i++) {
  100. *p16 = indices[i];
  101. p16++;
  102. }
  103. }
  104. frustum.index_buffer = RD::get_singleton()->index_buffer_create(6 * 2 * 3, RenderingDevice::INDEX_BUFFER_FORMAT_UINT16, data);
  105. frustum.index_array = RD::get_singleton()->index_array_create(frustum.index_buffer, 0, 6 * 2 * 3);
  106. }
  107. if (frustum.lines_buffer.is_null()) {
  108. uint16_t indices[12 * 2] = {
  109. 0, 1, // FLT - FLB
  110. 1, 3, // FLB - FRB
  111. 3, 2, // FRB - FRT
  112. 2, 0, // FRT - FLT
  113. 4, 6, // NLT - NRT
  114. 6, 7, // NRT - NRB
  115. 7, 5, // NRB - NLB
  116. 5, 4, // NLB - NLT
  117. 0, 4, // FLT - NLT
  118. 1, 5, // FLB - NLB
  119. 2, 6, // FRT - NRT
  120. 3, 7, // FRB - NRB
  121. };
  122. // Create our lines_array
  123. PackedByteArray data;
  124. data.resize(12 * 2 * 2);
  125. {
  126. uint8_t *w = data.ptrw();
  127. uint16_t *p16 = (uint16_t *)w;
  128. for (int i = 0; i < 12 * 2; i++) {
  129. *p16 = indices[i];
  130. p16++;
  131. }
  132. }
  133. frustum.lines_buffer = RD::get_singleton()->index_buffer_create(12 * 2, RenderingDevice::INDEX_BUFFER_FORMAT_UINT16, data);
  134. frustum.lines_array = RD::get_singleton()->index_array_create(frustum.lines_buffer, 0, 12 * 2);
  135. }
  136. }
  137. DebugEffects::~DebugEffects() {
  138. shadow_frustum.shader.version_free(shadow_frustum.shader_version);
  139. // Destroy vertex buffer and array.
  140. if (frustum.vertex_buffer.is_valid()) {
  141. RD::get_singleton()->free(frustum.vertex_buffer); // Array gets freed as dependency.
  142. }
  143. // Destroy index buffer and array,
  144. if (frustum.index_buffer.is_valid()) {
  145. RD::get_singleton()->free(frustum.index_buffer); // Array gets freed as dependency.
  146. }
  147. // Destroy lines buffer and array.
  148. if (frustum.lines_buffer.is_valid()) {
  149. RD::get_singleton()->free(frustum.lines_buffer); // Array gets freed as dependency.
  150. }
  151. motion_vectors.shader.version_free(motion_vectors.shader_version);
  152. }
  153. void DebugEffects::draw_shadow_frustum(RID p_light, const Projection &p_cam_projection, const Transform3D &p_cam_transform, RID p_dest_fb, const Rect2 p_rect) {
  154. RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton();
  155. RID base = light_storage->light_instance_get_base_light(p_light);
  156. ERR_FAIL_COND(light_storage->light_get_type(base) != RS::LIGHT_DIRECTIONAL);
  157. // Make sure our buffers and arrays exist.
  158. _create_frustum_arrays();
  159. // Setup a points buffer for our view frustum.
  160. PackedByteArray points;
  161. points.resize(8 * sizeof(float) * 3);
  162. // Get info about our splits.
  163. RS::LightDirectionalShadowMode shadow_mode = light_storage->light_directional_get_shadow_mode(base);
  164. bool overlap = light_storage->light_directional_get_blend_splits(base);
  165. int splits = 1;
  166. if (shadow_mode == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS) {
  167. splits = 4;
  168. } else if (shadow_mode == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS) {
  169. splits = 2;
  170. }
  171. // Setup our camera info (this is mostly a duplicate of the logic found in RendererSceneCull::_light_instance_setup_directional_shadow).
  172. bool is_orthogonal = p_cam_projection.is_orthogonal();
  173. real_t aspect = p_cam_projection.get_aspect();
  174. real_t fov = 0.0;
  175. Vector2 vp_he;
  176. if (is_orthogonal) {
  177. vp_he = p_cam_projection.get_viewport_half_extents();
  178. } else {
  179. fov = p_cam_projection.get_fov(); //this is actually yfov, because set aspect tries to keep it
  180. }
  181. real_t min_distance = p_cam_projection.get_z_near();
  182. real_t max_distance = p_cam_projection.get_z_far();
  183. real_t shadow_max = RSG::light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE);
  184. if (shadow_max > 0 && !is_orthogonal) {
  185. max_distance = MIN(shadow_max, max_distance);
  186. }
  187. // Make sure we've not got bad info coming in.
  188. max_distance = MAX(max_distance, min_distance + 0.001);
  189. min_distance = MIN(min_distance, max_distance);
  190. real_t range = max_distance - min_distance;
  191. real_t distances[5];
  192. distances[0] = min_distance;
  193. for (int i = 0; i < splits; i++) {
  194. distances[i + 1] = min_distance + RSG::light_storage->light_get_param(base, RS::LightParam(RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET + i)) * range;
  195. };
  196. distances[splits] = max_distance;
  197. Color colors[4] = {
  198. Color(1.0, 0.0, 0.0, 0.1),
  199. Color(0.0, 1.0, 0.0, 0.1),
  200. Color(0.0, 0.0, 1.0, 0.1),
  201. Color(1.0, 1.0, 0.0, 0.1),
  202. };
  203. for (int split = 0; split < splits; split++) {
  204. // Load frustum points into vertex buffer.
  205. uint8_t *w = points.ptrw();
  206. Vector3 *vw = (Vector3 *)w;
  207. Projection projection;
  208. if (is_orthogonal) {
  209. projection.set_orthogonal(vp_he.y * 2.0, aspect, distances[(split == 0 || !overlap) ? split : split - 1], distances[split + 1], false);
  210. } else {
  211. projection.set_perspective(fov, aspect, distances[(split == 0 || !overlap) ? split : split - 1], distances[split + 1], true);
  212. }
  213. bool res = projection.get_endpoints(p_cam_transform, vw);
  214. ERR_CONTINUE(!res);
  215. RD::get_singleton()->buffer_update(frustum.vertex_buffer, 0, 8 * sizeof(float) * 3, w);
  216. // Get our light projection info.
  217. Projection light_projection = light_storage->light_instance_get_shadow_camera(p_light, split);
  218. Transform3D light_transform = light_storage->light_instance_get_shadow_transform(p_light, split);
  219. Rect2 atlas_rect_norm = light_storage->light_instance_get_directional_shadow_atlas_rect(p_light, split);
  220. if (!is_orthogonal) {
  221. light_transform.orthogonalize();
  222. }
  223. // Setup our push constant.
  224. ShadowFrustumPushConstant push_constant;
  225. MaterialStorage::store_camera(light_projection * Projection(light_transform.inverse()), push_constant.mvp);
  226. push_constant.color[0] = colors[split].r;
  227. push_constant.color[1] = colors[split].g;
  228. push_constant.color[2] = colors[split].b;
  229. push_constant.color[3] = colors[split].a;
  230. // Adjust our rect to our atlas position.
  231. Rect2 rect = p_rect;
  232. rect.position.x += atlas_rect_norm.position.x * rect.size.x;
  233. rect.position.y += atlas_rect_norm.position.y * rect.size.y;
  234. rect.size.x *= atlas_rect_norm.size.x;
  235. rect.size.y *= atlas_rect_norm.size.y;
  236. // And draw our frustum.
  237. RD::FramebufferFormatID fb_format_id = RD::get_singleton()->framebuffer_get_format(p_dest_fb);
  238. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_fb, RD::INITIAL_ACTION_LOAD, RD::FINAL_ACTION_STORE, RD::INITIAL_ACTION_LOAD, RD::FINAL_ACTION_DISCARD, Vector<Color>(), 0.0, 0, rect);
  239. RID pipeline = shadow_frustum.pipelines[SFP_TRANSPARENT].get_render_pipeline(frustum.vertex_format, fb_format_id);
  240. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, pipeline);
  241. RD::get_singleton()->draw_list_bind_vertex_array(draw_list, frustum.vertex_array);
  242. RD::get_singleton()->draw_list_bind_index_array(draw_list, frustum.index_array);
  243. RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(ShadowFrustumPushConstant));
  244. RD::get_singleton()->draw_list_draw(draw_list, true);
  245. pipeline = shadow_frustum.pipelines[SFP_WIREFRAME].get_render_pipeline(frustum.vertex_format, fb_format_id);
  246. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, pipeline);
  247. RD::get_singleton()->draw_list_bind_vertex_array(draw_list, frustum.vertex_array);
  248. RD::get_singleton()->draw_list_bind_index_array(draw_list, frustum.lines_array);
  249. RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(ShadowFrustumPushConstant));
  250. RD::get_singleton()->draw_list_draw(draw_list, true);
  251. RD::get_singleton()->draw_list_end();
  252. if (split < (splits - 1) && splits > 1) {
  253. // Also draw it in the last split so we get a proper overview of the whole view frustum...
  254. // Get our light projection info.
  255. light_projection = light_storage->light_instance_get_shadow_camera(p_light, (splits - 1));
  256. light_transform = light_storage->light_instance_get_shadow_transform(p_light, (splits - 1));
  257. atlas_rect_norm = light_storage->light_instance_get_directional_shadow_atlas_rect(p_light, (splits - 1));
  258. if (!is_orthogonal) {
  259. light_transform.orthogonalize();
  260. }
  261. // Update our push constant.
  262. MaterialStorage::store_camera(light_projection * Projection(light_transform.inverse()), push_constant.mvp);
  263. push_constant.color[0] = colors[split].r;
  264. push_constant.color[1] = colors[split].g;
  265. push_constant.color[2] = colors[split].b;
  266. push_constant.color[3] = colors[split].a;
  267. // Adjust our rect to our atlas position.
  268. rect = p_rect;
  269. rect.position.x += atlas_rect_norm.position.x * rect.size.x;
  270. rect.position.y += atlas_rect_norm.position.y * rect.size.y;
  271. rect.size.x *= atlas_rect_norm.size.x;
  272. rect.size.y *= atlas_rect_norm.size.y;
  273. draw_list = RD::get_singleton()->draw_list_begin(p_dest_fb, RD::INITIAL_ACTION_LOAD, RD::FINAL_ACTION_STORE, RD::INITIAL_ACTION_LOAD, RD::FINAL_ACTION_DISCARD, Vector<Color>(), 0.0, 0, rect);
  274. pipeline = shadow_frustum.pipelines[SFP_TRANSPARENT].get_render_pipeline(frustum.vertex_format, fb_format_id);
  275. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, pipeline);
  276. RD::get_singleton()->draw_list_bind_vertex_array(draw_list, frustum.vertex_array);
  277. RD::get_singleton()->draw_list_bind_index_array(draw_list, frustum.index_array);
  278. RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(ShadowFrustumPushConstant));
  279. RD::get_singleton()->draw_list_draw(draw_list, true);
  280. RD::get_singleton()->draw_list_end();
  281. }
  282. }
  283. }
  284. void DebugEffects::draw_motion_vectors(RID p_velocity, RID p_depth, RID p_dest_fb, const Projection &p_current_projection, const Transform3D &p_current_transform, const Projection &p_previous_projection, const Transform3D &p_previous_transform, Size2i p_resolution) {
  285. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  286. ERR_FAIL_NULL(material_storage);
  287. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  288. ERR_FAIL_NULL(uniform_set_cache);
  289. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  290. RD::Uniform u_source_velocity(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_velocity }));
  291. RD::Uniform u_source_depth(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 1, Vector<RID>({ default_sampler, p_depth }));
  292. RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_fb, RD::INITIAL_ACTION_LOAD, RD::FINAL_ACTION_STORE, RD::INITIAL_ACTION_DISCARD, RD::FINAL_ACTION_DISCARD);
  293. RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, motion_vectors.pipeline.get_render_pipeline(RD::INVALID_ID, RD::get_singleton()->framebuffer_get_format(p_dest_fb), false, RD::get_singleton()->draw_list_get_current_pass()));
  294. Projection correction;
  295. correction.set_depth_correction(true, true, false);
  296. Projection reprojection = (correction * p_previous_projection) * p_previous_transform.affine_inverse() * p_current_transform * (correction * p_current_projection).inverse();
  297. RendererRD::MaterialStorage::store_camera(reprojection, motion_vectors.push_constant.reprojection_matrix);
  298. motion_vectors.push_constant.resolution[0] = p_resolution.width;
  299. motion_vectors.push_constant.resolution[1] = p_resolution.height;
  300. motion_vectors.push_constant.force_derive_from_depth = false;
  301. RID shader = motion_vectors.shader.version_get_shader(motion_vectors.shader_version, 0);
  302. RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_velocity, u_source_depth), 0);
  303. RD::get_singleton()->draw_list_set_push_constant(draw_list, &motion_vectors.push_constant, sizeof(MotionVectorsPushConstant));
  304. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  305. #ifdef DRAW_DERIVATION_FROM_DEPTH_ON_TOP
  306. motion_vectors.push_constant.force_derive_from_depth = true;
  307. RD::get_singleton()->draw_list_set_push_constant(draw_list, &motion_vectors.push_constant, sizeof(MotionVectorsPushConstant));
  308. RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);
  309. #endif
  310. RD::get_singleton()->draw_list_end();
  311. }