| 1 | /**************************************************************************/ |
| 2 | /* motion_vectors_store.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 "motion_vectors_store.h" |
| 32 | |
| 33 | #include "servers/rendering/renderer_rd/uniform_set_cache_rd.h" |
| 34 | |
| 35 | namespace RendererRD { |
| 36 | |
| 37 | MotionVectorsStore::MotionVectorsStore() { |
| 38 | Vector<String> modes; |
| 39 | modes.push_back(""); |
| 40 | |
| 41 | motion_shader.initialize(modes); |
| 42 | shader_version = motion_shader.version_create(); |
| 43 | |
| 44 | pipeline = RD::get_singleton()->compute_pipeline_create(motion_shader.version_get_shader(shader_version, 0)); |
| 45 | } |
| 46 | |
| 47 | MotionVectorsStore::~MotionVectorsStore() { |
| 48 | motion_shader.version_free(shader_version); |
| 49 | } |
| 50 | |
| 51 | void MotionVectorsStore::process(Ref<RenderSceneBuffersRD> p_render_buffers, |
| 52 | const Projection &p_current_projection, const Transform3D &p_current_transform, |
| 53 | const Projection &p_previous_projection, const Transform3D &p_previous_transform) { |
| 54 | MaterialStorage *material_storage = MaterialStorage::get_singleton(); |
| 55 | ERR_FAIL_NULL(material_storage); |
| 56 | |
| 57 | UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton(); |
| 58 | ERR_FAIL_NULL(uniform_set_cache); |
| 59 | |
| 60 | uint32_t view_count = p_render_buffers->get_view_count(); |
| 61 | Size2i internal_size = p_render_buffers->get_internal_size(); |
| 62 | |
| 63 | PushConstant push_constant; |
| 64 | { |
| 65 | push_constant.resolution[0] = internal_size.width; |
| 66 | push_constant.resolution[1] = internal_size.height; |
| 67 | |
| 68 | Projection correction; |
| 69 | correction.set_depth_correction(true, true, false); |
| 70 | Projection reprojection = (correction * p_previous_projection) * p_previous_transform.affine_inverse() * p_current_transform * (correction * p_current_projection).inverse(); |
| 71 | RendererRD::MaterialStorage::store_camera(reprojection, push_constant.reprojection_matrix); |
| 72 | } |
| 73 | |
| 74 | RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED); |
| 75 | |
| 76 | RD::get_singleton()->draw_command_begin_label("Motion Vector Store"); |
| 77 | |
| 78 | RID shader = motion_shader.version_get_shader(shader_version, 0); |
| 79 | ERR_FAIL_COND(shader.is_null()); |
| 80 | |
| 81 | RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin(); |
| 82 | RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, pipeline); |
| 83 | |
| 84 | for (uint32_t v = 0; v < view_count; v++) { |
| 85 | RID velocity = p_render_buffers->get_velocity_buffer(false, v); |
| 86 | RID depth = p_render_buffers->get_depth_texture(v); |
| 87 | RD::Uniform u_depth(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 0, Vector<RID>({ default_sampler, depth })); |
| 88 | RD::Uniform u_velocity(RD::UNIFORM_TYPE_IMAGE, 1, velocity); |
| 89 | |
| 90 | RID uniform_set = uniform_set_cache->get_cache(shader, 0, u_depth, u_velocity); |
| 91 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set, 0); |
| 92 | RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(PushConstant)); |
| 93 | RD::get_singleton()->compute_list_dispatch_threads(compute_list, internal_size.width, internal_size.height, 1); |
| 94 | } |
| 95 | |
| 96 | RD::get_singleton()->compute_list_end(); |
| 97 | |
| 98 | RD::get_singleton()->draw_command_end_label(); |
| 99 | } |
| 100 | |
| 101 | } //namespace RendererRD |