1 // Copyright 2021 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #include "Framebuffer.h"
5 #include "Framebuffer_ispc.h"
6 #include "Renderer_ispc.h"
7 
8 #include <rkcommon/math/rkmath.h>
9 #include <rkcommon/math/vec.h>
10 #include <rkcommon/tasking/parallel_for.h>
11 
12 #include <algorithm>
13 #include <chrono>
14 #include <sstream>
15 
16 namespace openvkl {
17   namespace examples {
18 
19     using namespace rkcommon::math;
20 
21     // -------------------------------------------------------------------------
22 
Buffer()23     Framebuffer::Buffer::Buffer() : Scheduler::Lockable() {}
24 
Buffer(Buffer && other)25     Framebuffer::Buffer::Buffer(Buffer &&other)
26     {
27       using std::swap;
28       swap(w, other.w);
29       swap(h, other.h);
30       swap(bufRgba, other.bufRgba);
31       swap(bufWeight, other.bufWeight);
32       swap(stats, other.stats);
33     }
34 
operator =(Buffer && other)35     Framebuffer::Buffer &Framebuffer::Buffer::operator=(Buffer &&other)
36     {
37       if (&other != this) {
38         using std::swap;
39         swap(w, other.w);
40         swap(h, other.h);
41         swap(bufRgba, other.bufRgba);
42         swap(bufWeight, other.bufWeight);
43         swap(stats, other.stats);
44       }
45       return *this;
46     }
47 
48     Framebuffer::Buffer::~Buffer() = default;
49 
resize(size_t _w,size_t _h)50     void Framebuffer::Buffer::resize(size_t _w, size_t _h)
51     {
52       const size_t numPixels = _w * _h;
53 
54       bufRgba.resize(numPixels);
55       std::fill(bufRgba.begin(), bufRgba.end(), vec4f(0.f));
56 
57       bufWeight.resize(numPixels);
58       std::fill(bufWeight.begin(), bufWeight.end(), 0.f);
59 
60       w = _w;
61       h = _h;
62     }
63 
copy(const Buffer & other)64     void Framebuffer::Buffer::copy(const Buffer &other)
65     {
66       assert(w == other.w);
67       assert(h == other.h);
68 
69       const auto start = Framebuffer::Stats::Clock::now();
70       bufRgba = other.bufRgba;
71       bufWeight = other.bufWeight;
72       stats          = other.stats;
73       const auto end = Framebuffer::Stats::Clock::now();
74       stats.copyTime = end - start;
75     }
76 
tonemapInPlace()77     void Framebuffer::Buffer::tonemapInPlace()
78     {
79       const auto start = Framebuffer::Stats::Clock::now();
80 
81       const size_t numPixels    = w * h;
82       const size_t pixelsPerJob = ispc::Renderer_pixelsPerJob();
83       const size_t numJobs      = numPixels / pixelsPerJob;
84       rkcommon::tasking::parallel_for(numJobs, [&](size_t i) {
85         const size_t ib = i * pixelsPerJob;
86         ispc::tonemap(static_cast<uint32_t>(ib),
87                       static_cast<uint32_t>(numPixels),
88                       bufWeight.data(),
89                       reinterpret_cast<ispc::vec4f *>(bufRgba.data()));
90       });
91 
92       const auto end    = Framebuffer::Stats::Clock::now();
93       stats.tonemapTime = end - start;
94     }
95 
96   }  // namespace examples
97 }  // namespace openvkl
98