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