Wayverb
reflector.h
1 #pragma once
2 
3 #include "raytracer/program.h"
4 
5 #include "core/cl/geometry.h"
6 #include "core/cl/include.h"
7 #include "core/geo/geometric.h"
8 #include "core/spatial_division/scene_buffers.h"
9 
10 #include "utilities/aligned/vector.h"
11 #include "utilities/map_to_vector.h"
12 
13 #include "glm/glm.hpp"
14 
15 namespace wayverb {
16 namespace raytracer {
17 
18 template <typename It>
19 auto get_rays_from_directions(It begin, It end, const glm::vec3& source) {
20  return util::map_to_vector(begin, end, [&](const auto& i) {
21  return core::geo::ray{source, i};
22  });
23 }
24 
25 class reflector final {
26 public:
27  template <typename It>
29  const glm::vec3& receiver,
30  It b,
31  It e)
32  : cc_{cc}
33  , queue_{cc.context, cc.device}
34  , kernel_{program{cc}.get_kernel()}
35  , receiver_{core::to_cl_float3{}(receiver)}
36  , rays_(std::distance(b, e))
37  , ray_buffer_{core::load_to_buffer(
38  cc.context,
39  util::map_to_vector(
40  b,
41  e,
42  [](const auto& i) { return core::convert(i); }),
43  false)}
44  , reflection_buffer_{cc.context,
45  CL_MEM_READ_WRITE,
46  rays_ * sizeof(reflection)}
47  , rng_buffer_{cc.context,
48  CL_MEM_READ_WRITE,
49  rays_ * 2 * sizeof(cl_float)} {
50  program{cc_}.get_init_reflections_kernel()(
51  cl::EnqueueArgs{queue_, cl::NDRange{rays_}},
52  reflection_buffer_);
53  }
54 
55  util::aligned::vector<reflection> run_step(
56  const core::scene_buffers& buffers);
57 
58  util::aligned::vector<core::ray> get_rays();
59  util::aligned::vector<reflection> get_reflections();
60  util::aligned::vector<cl_float> get_rng();
61 
63  static constexpr auto get_per_ray_size() {
64  return sizeof(core::ray) + sizeof(reflection) + 2 * sizeof(cl_float);
65  }
66 
67 private:
68  using kernel_t = decltype(std::declval<program>().get_kernel());
69 
71  cl::CommandQueue queue_;
72  kernel_t kernel_;
73  cl_float3 receiver_;
74  size_t rays_;
75 
76  cl::Buffer ray_buffer_;
77  cl::Buffer reflection_buffer_;
78 
79  cl::Buffer rng_buffer_;
80 };
81 
82 } // namespace raytracer
83 } // namespace wayverb
Definition: geometry_structs.h:11
Definition: reflector.h:25
Definition: conversions.h:10
Definition: scene_buffers.h:12
Definition: pressure.h:22
static constexpr auto get_per_ray_size()
The constant buffer size required per parallel ray.
Definition: reflector.h:63
Definition: capsule_base.h:9
Definition: reflection.h:10
invariant: device is a valid device for the context
Definition: common.h:13
Definition: program.h:11