Debian Codegraph @

libs/ibl/src/CubemapUtilsImpl.h

raw
1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef IBL_CUBEMAPUTILSIMPL_H
18 #define IBL_CUBEMAPUTILSIMPL_H
19
20 #include <ibl/CubemapUtils.h>
21
22 #include <utils/compiler.h>
23 #include <utils/JobSystem.h>
24
25 namespace filament {
26 namespace ibl {
27
28 template<typename STATE>
29 void CubemapUtils::process(
30 Cubemap& cm,
31 utils::JobSystem& js,
32 CubemapUtils::ScanlineProc<STATE> proc,
33 ReduceProc<STATE> reduce,
34 const STATE& prototype) {
35 using namespace utils;
36
37 const size_t dim = cm.getDimensions();
38
39 STATE states[6];
40 for (STATE& s : states) {
41 s = prototype;
42 }
43
44 JobSystem::Job* parent = js.createJob();
45 for (size_t faceIndex = 0; faceIndex < 6; faceIndex++) {
46
47 auto perFaceJob = [faceIndex, &states, &cm, dim, &proc]
48 (utils::JobSystem& js, utils::JobSystem::Job* parent) {
49 STATE& s = states[faceIndex];
50 Image& image(cm.getImageForFace((Cubemap::Face)faceIndex));
51
52 // here we must limit how much we capture so we can use this closure
53 // by value.
54 auto parallelJobTask = [&s, &image, &proc, dim = uint16_t(dim),
55 faceIndex = uint8_t(faceIndex)](size_t y0, size_t c) {
56 for (size_t y = y0; y < y0 + c; y++) {
57 Cubemap::Texel* data =
58 static_cast<Cubemap::Texel*>(image.getPixelRef(0, y));
59 proc(s, y, (Cubemap::Face)faceIndex, data, dim);
60 }
61 };
62
63 constexpr bool isStateLess = std::is_same<STATE, CubemapUtils::EmptyState>::value;
64 if (UTILS_LIKELY(isStateLess)) {
65 // create the job, copying it by value
66 auto job = jobs::parallel_for(js, parent, 0, uint32_t(dim),
67 parallelJobTask, jobs::CountSplitter<64, 8>());
68 // not need to signal here, since we're just scheduling work
69 js.run(job, JobSystem::DONT_SIGNAL);
70 } else {
71 // if we have a per-thread STATE, we can't parallel_for()
72 parallelJobTask(0, dim);
73 }
74 };
75
76 // not need to signal here, since we're just scheduling work
77 js.run(jobs::createJob(js, parent, perFaceJob, std::ref(js), parent), JobSystem::DONT_SIGNAL);
78 }
79
80 // wait for all our threads to finish
81 js.runAndWait(parent);
82
83 for (STATE& s : states) {
84 reduce(s);
85 }
86 }
87
88 template<typename STATE>
89 void CubemapUtils::processSingleThreaded(
90 Cubemap& cm,
91 utils::JobSystem& js,
92 CubemapUtils::ScanlineProc<STATE> proc,
93 ReduceProc<STATE> reduce,
94 const STATE& prototype) {
95 using namespace utils;
96
97 const size_t dim = cm.getDimensions();
98
99 STATE s;
100 s = prototype;
101
102 for (size_t faceIndex = 0; faceIndex < 6; faceIndex++) {
103 const Cubemap::Face f = (Cubemap::Face)faceIndex;
104 Image& image(cm.getImageForFace(f));
105 for (size_t y = 0; y < dim; y++) {
106 Cubemap::Texel* data = static_cast<Cubemap::Texel*>(image.getPixelRef(0, y));
107 proc(s, y, f, data, dim);
108 }
109 }
110 reduce(s);
111 }
112
113
114 } // namespace ibl
115 } // namespace filament
116
117 #endif // IBL_CUBEMAPUTILSIMPL_H