motion_vectors_store.cpp 5.0 KB

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