Wayverb
finder.h
1 #pragma once
2 
3 #include "program.h"
4 
5 #include "raytracer/cl/structs.h"
6 
7 #include "core/cl/common.h"
8 #include "core/conversions.h"
9 #include "core/pressure_intensity.h"
10 #include "core/spatial_division/scene_buffers.h"
11 
12 #include "utilities/aligned/vector.h"
13 
14 namespace wayverb {
15 namespace raytracer {
16 namespace stochastic {
17 
18 constexpr auto compute_ray_energy(size_t total_rays,
19  float dist,
20  float open_angle) {
21  // see schroder2011 5.54
22  // The extra factor of 4pi here is because image-source intensity =
23  // 1 / 4pir^2 instead of just 1 / r^2
24  return 2.0 / (4 * M_PI * total_rays * dist * dist * (1 - open_angle));
25 }
26 
27 float compute_ray_energy(size_t total_rays,
28  const glm::vec3& source,
29  const glm::vec3& receiver,
30  float receiver_radius);
31 
33 
34 class finder final {
35 public:
36  finder(const core::compute_context& cc,
37  size_t group_size,
38  const glm::vec3& source,
39  const glm::vec3& receiver,
40  float receiver_radius,
41  float starting_energy);
42 
43  struct results final {
44  util::aligned::vector<impulse<core::simulation_bands>> specular;
45  util::aligned::vector<impulse<core::simulation_bands>> stochastic;
46  };
47 
48  template <typename It>
49  auto process(It b, It e, const core::scene_buffers& scene_buffers) {
50  // copy the current batch of reflections to the device
51  cl::copy(queue_, b, e, reflections_buffer_);
52 
53  // get the kernel and run it
54  kernel_(cl::EnqueueArgs(queue_, cl::NDRange(rays_)),
55  reflections_buffer_,
56  receiver_,
57  receiver_radius_,
58  scene_buffers.get_triangles_buffer(),
59  scene_buffers.get_vertices_buffer(),
60  scene_buffers.get_surfaces_buffer(),
61  stochastic_path_buffer_,
62  stochastic_output_buffer_,
63  specular_output_buffer_);
64 
65  const auto read_out_impulses = [&](const auto& buffer) {
66  auto raw = core::read_from_buffer<impulse<core::simulation_bands>>(
67  queue_, buffer);
68  raw.erase(std::remove_if(begin(raw),
69  end(raw),
70  [](const auto& impulse) {
71  return !impulse.distance;
72  }),
73  end(raw));
74  return raw;
75  };
76 
77  return results{read_out_impulses(specular_output_buffer_),
78  read_out_impulses(stochastic_output_buffer_)};
79  }
80 
81 private:
82  using kernel_t = decltype(std::declval<program>().get_kernel());
83 
85  cl::CommandQueue queue_;
86  kernel_t kernel_;
87  cl_float3 receiver_;
88  cl_float receiver_radius_;
89  size_t rays_;
90 
91  cl::Buffer reflections_buffer_;
92  cl::Buffer stochastic_path_buffer_;
93  cl::Buffer stochastic_output_buffer_;
94  cl::Buffer specular_output_buffer_;
95 };
96 
97 } // namespace stochastic
98 } // namespace raytracer
99 } // namespace wayverb
Definition: scene_buffers.h:12
Definition: structs.h:38
Definition: pressure.h:22
Definition: capsule_base.h:9
invariant: device is a valid device for the context
Definition: common.h:13