taa.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**************************************************************************/
  2. /* taa.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 "taa.h"
  31. #include "servers/rendering/renderer_rd/effects/copy_effects.h"
  32. #include "servers/rendering/renderer_rd/storage_rd/material_storage.h"
  33. #include "servers/rendering/renderer_rd/uniform_set_cache_rd.h"
  34. using namespace RendererRD;
  35. TAA::TAA() {
  36. Vector<String> taa_modes;
  37. taa_modes.push_back("\n#define MODE_TAA_RESOLVE");
  38. taa_shader.initialize(taa_modes);
  39. shader_version = taa_shader.version_create();
  40. pipeline = RD::get_singleton()->compute_pipeline_create(taa_shader.version_get_shader(shader_version, 0));
  41. }
  42. TAA::~TAA() {
  43. taa_shader.version_free(shader_version);
  44. }
  45. void TAA::resolve(RID p_frame, RID p_temp, RID p_depth, RID p_velocity, RID p_prev_velocity, RID p_history, Size2 p_resolution, float p_z_near, float p_z_far) {
  46. UniformSetCacheRD *uniform_set_cache = UniformSetCacheRD::get_singleton();
  47. ERR_FAIL_NULL(uniform_set_cache);
  48. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  49. ERR_FAIL_NULL(material_storage);
  50. RID shader = taa_shader.version_get_shader(shader_version, 0);
  51. ERR_FAIL_COND(shader.is_null());
  52. RID default_sampler = material_storage->sampler_rd_get_default(RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR, RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED);
  53. TAAResolvePushConstant push_constant;
  54. memset(&push_constant, 0, sizeof(TAAResolvePushConstant));
  55. push_constant.resolution_width = p_resolution.width;
  56. push_constant.resolution_height = p_resolution.height;
  57. push_constant.disocclusion_threshold = 0.025f;
  58. push_constant.disocclusion_scale = 10.0f;
  59. RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin();
  60. RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, pipeline);
  61. RD::Uniform u_frame_source(RD::UNIFORM_TYPE_IMAGE, 0, { p_frame });
  62. RD::Uniform u_depth(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 1, { default_sampler, p_depth });
  63. RD::Uniform u_velocity(RD::UNIFORM_TYPE_IMAGE, 2, { p_velocity });
  64. RD::Uniform u_prev_velocity(RD::UNIFORM_TYPE_IMAGE, 3, { p_prev_velocity });
  65. RD::Uniform u_history(RD::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE, 4, { default_sampler, p_history });
  66. RD::Uniform u_frame_dest(RD::UNIFORM_TYPE_IMAGE, 5, { p_temp });
  67. RD::get_singleton()->compute_list_bind_uniform_set(compute_list, uniform_set_cache->get_cache(shader, 0, u_frame_source, u_depth, u_velocity, u_prev_velocity, u_history, u_frame_dest), 0);
  68. RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(TAAResolvePushConstant));
  69. RD::get_singleton()->compute_list_dispatch_threads(compute_list, p_resolution.width, p_resolution.height, 1);
  70. RD::get_singleton()->compute_list_end();
  71. }
  72. void TAA::process(Ref<RenderSceneBuffersRD> p_render_buffers, RD::DataFormat p_format, float p_z_near, float p_z_far) {
  73. CopyEffects *copy_effects = CopyEffects::get_singleton();
  74. uint32_t view_count = p_render_buffers->get_view_count();
  75. Size2i internal_size = p_render_buffers->get_internal_size();
  76. Size2i target_size = p_render_buffers->get_target_size();
  77. bool just_allocated = false;
  78. if (!p_render_buffers->has_texture(SNAME("taa"), SNAME("history"))) {
  79. uint32_t usage_bits = RD::TEXTURE_USAGE_SAMPLING_BIT | RD::TEXTURE_USAGE_STORAGE_BIT;
  80. p_render_buffers->create_texture(SNAME("taa"), SNAME("history"), p_format, usage_bits);
  81. p_render_buffers->create_texture(SNAME("taa"), SNAME("temp"), p_format, usage_bits);
  82. p_render_buffers->create_texture(SNAME("taa"), SNAME("prev_velocity"), RD::DATA_FORMAT_R16G16_SFLOAT, usage_bits);
  83. just_allocated = true;
  84. }
  85. RD::get_singleton()->draw_command_begin_label("TAA");
  86. for (uint32_t v = 0; v < view_count; v++) {
  87. // Get our (cached) slices
  88. RID internal_texture = p_render_buffers->get_internal_texture(v);
  89. RID velocity_buffer = p_render_buffers->get_velocity_buffer(false, v);
  90. RID taa_history = p_render_buffers->get_texture_slice(SNAME("taa"), SNAME("history"), v, 0);
  91. RID taa_prev_velocity = p_render_buffers->get_texture_slice(SNAME("taa"), SNAME("prev_velocity"), v, 0);
  92. if (!just_allocated) {
  93. RID depth_texture = p_render_buffers->get_depth_texture(v);
  94. RID taa_temp = p_render_buffers->get_texture_slice(SNAME("taa"), SNAME("temp"), v, 0);
  95. resolve(internal_texture, taa_temp, depth_texture, velocity_buffer, taa_prev_velocity, taa_history, Size2(internal_size.x, internal_size.y), p_z_near, p_z_far);
  96. copy_effects->copy_to_rect(taa_temp, internal_texture, Rect2(0, 0, internal_size.x, internal_size.y));
  97. }
  98. copy_effects->copy_to_rect(internal_texture, taa_history, Rect2(0, 0, internal_size.x, internal_size.y));
  99. copy_effects->copy_to_rect(velocity_buffer, taa_prev_velocity, Rect2(0, 0, target_size.x, target_size.y));
  100. }
  101. RD::get_singleton()->draw_command_end_label();
  102. }