Path: blob/master/servers/rendering/renderer_rd/effects/debug_effects.cpp
10279 views
/**************************************************************************/1/* debug_effects.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "debug_effects.h"31#include "servers/rendering/renderer_rd/storage_rd/light_storage.h"32#include "servers/rendering/renderer_rd/storage_rd/material_storage.h"33#include "servers/rendering/renderer_rd/uniform_set_cache_rd.h"3435using namespace RendererRD;3637DebugEffects::DebugEffects() {38{39// Shadow Frustum debug shader40Vector<String> modes;41modes.push_back("");4243shadow_frustum.shader.initialize(modes);44shadow_frustum.shader_version = shadow_frustum.shader.version_create();4546RD::PipelineRasterizationState raster_state = RD::PipelineRasterizationState();47shadow_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);4849raster_state.wireframe = true;50shadow_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);51}5253{54// Motion Vectors debug shader.55Vector<String> modes;56modes.push_back("");5758motion_vectors.shader.initialize(modes);59motion_vectors.shader_version = motion_vectors.shader.version_create();6061motion_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);62}63}6465void DebugEffects::_create_frustum_arrays() {66if (frustum.vertex_buffer.is_null()) {67// Create vertex buffer, but don't put data in it yet68frustum.vertex_buffer = RD::get_singleton()->vertex_buffer_create(8 * sizeof(float) * 3, Vector<uint8_t>());6970Vector<RD::VertexAttribute> attributes;71Vector<RID> buffers;72RD::VertexAttribute vd;7374vd.location = 0;75vd.stride = sizeof(float) * 3;76vd.format = RD::DATA_FORMAT_R32G32B32_SFLOAT;7778attributes.push_back(vd);79buffers.push_back(frustum.vertex_buffer);8081frustum.vertex_format = RD::get_singleton()->vertex_format_create(attributes);82frustum.vertex_array = RD::get_singleton()->vertex_array_create(8, frustum.vertex_format, buffers);83}8485if (frustum.index_buffer.is_null()) {86uint16_t indices[6 * 2 * 3] = {87// Far880, 1, 2, // FLT, FLB, FRT891, 3, 2, // FLB, FRB, FRT90// Near914, 6, 5, // NLT, NRT, NLB926, 7, 5, // NRT, NRB, NLB93// Left940, 4, 1, // FLT, NLT, FLB954, 5, 1, // NLT, NLB, FLB96// Right976, 2, 7, // NRT, FRT, NRB982, 3, 7, // FRT, FRB, NRB99// Top1000, 2, 4, // FLT, FRT, NLT1012, 6, 4, // FRT, NRT, NLT102// Bottom1035, 7, 1, // NLB, NRB, FLB,1047, 3, 1, // NRB, FRB, FLB105};106107// Create our index_array108PackedByteArray data;109data.resize(6 * 2 * 3 * 2);110{111uint8_t *w = data.ptrw();112uint16_t *p16 = (uint16_t *)w;113for (int i = 0; i < 6 * 2 * 3; i++) {114*p16 = indices[i];115p16++;116}117}118119frustum.index_buffer = RD::get_singleton()->index_buffer_create(6 * 2 * 3, RenderingDevice::INDEX_BUFFER_FORMAT_UINT16, data);120frustum.index_array = RD::get_singleton()->index_array_create(frustum.index_buffer, 0, 6 * 2 * 3);121}122123if (frustum.lines_buffer.is_null()) {124uint16_t indices[12 * 2] = {1250, 1, // FLT - FLB1261, 3, // FLB - FRB1273, 2, // FRB - FRT1282, 0, // FRT - FLT1291304, 6, // NLT - NRT1316, 7, // NRT - NRB1327, 5, // NRB - NLB1335, 4, // NLB - NLT1341350, 4, // FLT - NLT1361, 5, // FLB - NLB1372, 6, // FRT - NRT1383, 7, // FRB - NRB139};140141// Create our lines_array142PackedByteArray data;143data.resize(12 * 2 * 2);144{145uint8_t *w = data.ptrw();146uint16_t *p16 = (uint16_t *)w;147for (int i = 0; i < 12 * 2; i++) {148*p16 = indices[i];149p16++;150}151}152153frustum.lines_buffer = RD::get_singleton()->index_buffer_create(12 * 2, RenderingDevice::INDEX_BUFFER_FORMAT_UINT16, data);154frustum.lines_array = RD::get_singleton()->index_array_create(frustum.lines_buffer, 0, 12 * 2);155}156}157158DebugEffects::~DebugEffects() {159shadow_frustum.shader.version_free(shadow_frustum.shader_version);160161// Destroy vertex buffer and array.162if (frustum.vertex_buffer.is_valid()) {163RD::get_singleton()->free(frustum.vertex_buffer); // Array gets freed as dependency.164}165166// Destroy index buffer and array,167if (frustum.index_buffer.is_valid()) {168RD::get_singleton()->free(frustum.index_buffer); // Array gets freed as dependency.169}170171// Destroy lines buffer and array.172if (frustum.lines_buffer.is_valid()) {173RD::get_singleton()->free(frustum.lines_buffer); // Array gets freed as dependency.174}175176motion_vectors.shader.version_free(motion_vectors.shader_version);177}178179void 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) {180RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton();181182RID base = light_storage->light_instance_get_base_light(p_light);183ERR_FAIL_COND(light_storage->light_get_type(base) != RS::LIGHT_DIRECTIONAL);184185// Make sure our buffers and arrays exist.186_create_frustum_arrays();187188// Setup a points buffer for our view frustum.189PackedByteArray points;190points.resize(8 * sizeof(float) * 3);191192// Get info about our splits.193RS::LightDirectionalShadowMode shadow_mode = light_storage->light_directional_get_shadow_mode(base);194bool overlap = light_storage->light_directional_get_blend_splits(base);195int splits = 1;196if (shadow_mode == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_4_SPLITS) {197splits = 4;198} else if (shadow_mode == RS::LIGHT_DIRECTIONAL_SHADOW_PARALLEL_2_SPLITS) {199splits = 2;200}201202// Setup our camera info (this is mostly a duplicate of the logic found in RendererSceneCull::_light_instance_setup_directional_shadow).203bool is_orthogonal = p_cam_projection.is_orthogonal();204real_t aspect = p_cam_projection.get_aspect();205real_t fov = 0.0;206Vector2 vp_he;207if (is_orthogonal) {208vp_he = p_cam_projection.get_viewport_half_extents();209} else {210fov = p_cam_projection.get_fov(); //this is actually yfov, because set aspect tries to keep it211}212real_t min_distance = p_cam_projection.get_z_near();213real_t max_distance = p_cam_projection.get_z_far();214real_t shadow_max = RSG::light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_MAX_DISTANCE);215if (shadow_max > 0 && !is_orthogonal) {216max_distance = MIN(shadow_max, max_distance);217}218219// Make sure we've not got bad info coming in.220max_distance = MAX(max_distance, min_distance + 0.001);221min_distance = MIN(min_distance, max_distance);222real_t range = max_distance - min_distance;223224real_t distances[5];225distances[0] = min_distance;226for (int i = 0; i < splits; i++) {227distances[i + 1] = min_distance + RSG::light_storage->light_get_param(base, RS::LightParam(RS::LIGHT_PARAM_SHADOW_SPLIT_1_OFFSET + i)) * range;228};229distances[splits] = max_distance;230231Color colors[4] = {232Color(1.0, 0.0, 0.0, 0.1),233Color(0.0, 1.0, 0.0, 0.1),234Color(0.0, 0.0, 1.0, 0.1),235Color(1.0, 1.0, 0.0, 0.1),236};237238for (int split = 0; split < splits; split++) {239// Load frustum points into vertex buffer.240uint8_t *w = points.ptrw();241Vector3 *vw = (Vector3 *)w;242243Projection projection;244245if (is_orthogonal) {246projection.set_orthogonal(vp_he.y * 2.0, aspect, distances[(split == 0 || !overlap) ? split : split - 1], distances[split + 1], false);247} else {248projection.set_perspective(fov, aspect, distances[(split == 0 || !overlap) ? split : split - 1], distances[split + 1], true);249}250251bool res = projection.get_endpoints(p_cam_transform, vw);252ERR_CONTINUE(!res);253254RD::get_singleton()->buffer_update(frustum.vertex_buffer, 0, 8 * sizeof(float) * 3, w);255256// Get our light projection info.257Projection light_projection = light_storage->light_instance_get_shadow_camera(p_light, split);258Transform3D light_transform = light_storage->light_instance_get_shadow_transform(p_light, split);259Rect2 atlas_rect_norm = light_storage->light_instance_get_directional_shadow_atlas_rect(p_light, split);260261if (!is_orthogonal) {262light_transform.orthogonalize();263}264265// Setup our push constant.266ShadowFrustumPushConstant push_constant;267MaterialStorage::store_camera(light_projection * Projection(light_transform.inverse()), push_constant.mvp);268push_constant.color[0] = colors[split].r;269push_constant.color[1] = colors[split].g;270push_constant.color[2] = colors[split].b;271push_constant.color[3] = colors[split].a;272273// Adjust our rect to our atlas position.274Rect2 rect = p_rect;275rect.position.x += atlas_rect_norm.position.x * rect.size.x;276rect.position.y += atlas_rect_norm.position.y * rect.size.y;277rect.size.x *= atlas_rect_norm.size.x;278rect.size.y *= atlas_rect_norm.size.y;279280// And draw our frustum.281RD::FramebufferFormatID fb_format_id = RD::get_singleton()->framebuffer_get_format(p_dest_fb);282283RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_fb, RD::DRAW_DEFAULT_ALL, Vector<Color>(), 1.0f, 0, rect);284285RID pipeline = shadow_frustum.pipelines[SFP_TRANSPARENT].get_render_pipeline(frustum.vertex_format, fb_format_id);286RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, pipeline);287RD::get_singleton()->draw_list_bind_vertex_array(draw_list, frustum.vertex_array);288RD::get_singleton()->draw_list_bind_index_array(draw_list, frustum.index_array);289RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(ShadowFrustumPushConstant));290RD::get_singleton()->draw_list_draw(draw_list, true);291292pipeline = shadow_frustum.pipelines[SFP_WIREFRAME].get_render_pipeline(frustum.vertex_format, fb_format_id);293RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, pipeline);294RD::get_singleton()->draw_list_bind_vertex_array(draw_list, frustum.vertex_array);295RD::get_singleton()->draw_list_bind_index_array(draw_list, frustum.lines_array);296RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(ShadowFrustumPushConstant));297RD::get_singleton()->draw_list_draw(draw_list, true);298299RD::get_singleton()->draw_list_end();300301if (split < (splits - 1) && splits > 1) {302// Also draw it in the last split so we get a proper overview of the whole view frustum...303304// Get our light projection info.305light_projection = light_storage->light_instance_get_shadow_camera(p_light, (splits - 1));306light_transform = light_storage->light_instance_get_shadow_transform(p_light, (splits - 1));307atlas_rect_norm = light_storage->light_instance_get_directional_shadow_atlas_rect(p_light, (splits - 1));308309if (!is_orthogonal) {310light_transform.orthogonalize();311}312313// Update our push constant.314MaterialStorage::store_camera(light_projection * Projection(light_transform.inverse()), push_constant.mvp);315push_constant.color[0] = colors[split].r;316push_constant.color[1] = colors[split].g;317push_constant.color[2] = colors[split].b;318push_constant.color[3] = colors[split].a;319320// Adjust our rect to our atlas position.321rect = p_rect;322rect.position.x += atlas_rect_norm.position.x * rect.size.x;323rect.position.y += atlas_rect_norm.position.y * rect.size.y;324rect.size.x *= atlas_rect_norm.size.x;325rect.size.y *= atlas_rect_norm.size.y;326327draw_list = RD::get_singleton()->draw_list_begin(p_dest_fb, RD::DRAW_DEFAULT_ALL, Vector<Color>(), 1.0f, 0, rect);328329pipeline = shadow_frustum.pipelines[SFP_TRANSPARENT].get_render_pipeline(frustum.vertex_format, fb_format_id);330RD::get_singleton()->draw_list_bind_render_pipeline(draw_list, pipeline);331RD::get_singleton()->draw_list_bind_vertex_array(draw_list, frustum.vertex_array);332RD::get_singleton()->draw_list_bind_index_array(draw_list, frustum.index_array);333RD::get_singleton()->draw_list_set_push_constant(draw_list, &push_constant, sizeof(ShadowFrustumPushConstant));334RD::get_singleton()->draw_list_draw(draw_list, true);335336RD::get_singleton()->draw_list_end();337}338}339}340341void 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) {342MaterialStorage *material_storage = MaterialStorage::get_singleton();343ERR_FAIL_NULL(material_storage);344345UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();346ERR_FAIL_NULL(uniform_set_cache);347348RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);349RD::Uniform u_source_velocity(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, p_velocity }));350RD::Uniform u_source_depth(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 1, Vector<RID>({ default_sampler, p_depth }));351352RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(p_dest_fb);353RD::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()));354355Projection correction;356correction.set_depth_correction(true, true, false);357Projection reprojection = (correction * p_previous_projection) * p_previous_transform.affine_inverse() * p_current_transform * (correction * p_current_projection).inverse();358RendererRD::MaterialStorage::store_camera(reprojection, motion_vectors.push_constant.reprojection_matrix);359360motion_vectors.push_constant.resolution[0] = p_resolution.width;361motion_vectors.push_constant.resolution[1] = p_resolution.height;362motion_vectors.push_constant.force_derive_from_depth = false;363364RID shader = motion_vectors.shader.version_get_shader(motion_vectors.shader_version, 0);365RD::get_singleton()->draw_list_bind_uniform_set(draw_list, uniform_set_cache->get_cache(shader, 0, u_source_velocity, u_source_depth), 0);366RD::get_singleton()->draw_list_set_push_constant(draw_list, &motion_vectors.push_constant, sizeof(MotionVectorsPushConstant));367RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);368369#ifdef DRAW_DERIVATION_FROM_DEPTH_ON_TOP370motion_vectors.push_constant.force_derive_from_depth = true;371372RD::get_singleton()->draw_list_set_push_constant(draw_list, &motion_vectors.push_constant, sizeof(MotionVectorsPushConstant));373RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 3u);374#endif375376RD::get_singleton()->draw_list_end();377}378379380