Debian Codegraph @

libs/ibl/src/CubemapUtils.cpp

raw
1 /*
2 * Copyright (C) 2015 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 #include <ibl/CubemapUtils.h>
18
19 #include "CubemapUtilsImpl.h"
20
21 #include <ibl/utilities.h>
22
23 #include <utils/JobSystem.h>
24
25 #include <math/mat4.h>
26
27 #include <algorithm>
28
29 #include <math.h>
30 #include <string.h>
31
32 using namespace filament::math;
33 using namespace utils;
34
35 namespace filament {
36 namespace ibl {
37
38 void CubemapUtils::clamp(Image& src) {
39 // See: http://graphicrants.blogspot.com/2013/12/tone-mapping.html
40 // By Brian Karis
41 auto compress = [](float3 color, float linear, float compressed) {
42 float luma = dot(color, float3{ 0.2126, 0.7152, 0.0722 }); // REC 709
43 return luma <= linear ? color :
44 (color / luma) * ((linear * linear - compressed * luma)
45 / (2 * linear - compressed - luma));
46 };
47 const size_t width = src.getWidth();
48 const size_t height = src.getHeight();
49 for (size_t y = 0; y < height; ++y) {
50 for (size_t x = 0; x < width; ++x) {
51 float3& c = *static_cast<float3*>(src.getPixelRef(x, y));
52 // these values are chosen arbitrarily and seem to produce good result with
53 // 4096 samples
54 c = compress(c, 4096.0f, 16384.0f);
55 }
56 }
57 }
58
59 void CubemapUtils::highlight(Image& src) {
60 const size_t width = src.getWidth();
61 const size_t height = src.getHeight();
62 for (size_t y = 0; y < height; ++y) {
63 for (size_t x = 0; x < width; ++x) {
64 float3& c = *static_cast<float3*>(src.getPixelRef(x, y));
65 if (min(c) < 0.0f) {
66 c = { 0, 0, 1 };
67 } else if (max(c) > 64512.0f) { // maximum encodable by 10-bits float (RGB_11_11_10)
68 c = { 1, 0, 0 };
69 }
70 }
71 }
72 }
73
74 void CubemapUtils::downsampleCubemapLevelBoxFilter(JobSystem& js, Cubemap& dst, const Cubemap& src) {
75 size_t scale = src.getDimensions() / dst.getDimensions();
76 processSingleThreaded<EmptyState>(dst, js,
77 [&](EmptyState&, size_t y, Cubemap::Face f, Cubemap::Texel* data, size_t dim) {
78 const Image& image(src.getImageForFace(f));
79 for (size_t x = 0; x < dim; ++x, ++data) {
80 Cubemap::writeAt(data, Cubemap::filterAtCenter(image, x * scale, y * scale));
81 }
82 });
83 }
84
85
86 /*
87 * Area of a cube face's quadrant projected onto a sphere
88 *
89 * 1 +---+----------+
90 * | | |
91 * |---+----------|
92 * | |(x,y) |
93 * | | |
94 * | | |
95 * -1 +---+----------+
96 * -1 1
97 *
98 *
99 * The quadrant (-1,1)-(x,y) is projected onto the unit sphere
100 *
101 */
102 static inline float sphereQuadrantArea(float x, float y) {
103 return std::atan2(x*y, std::sqrt(x*x + y*y + 1));
104 }
105
106 float CubemapUtils::solidAngle(size_t dim, size_t u, size_t v) {
107 const float iDim = 1.0f / dim;
108 float s = ((u + 0.5f) * 2 * iDim) - 1;
109 float t = ((v + 0.5f) * 2 * iDim) - 1;
110 const float x0 = s - iDim;
111 const float y0 = t - iDim;
112 const float x1 = s + iDim;
113 const float y1 = t + iDim;
114 float solidAngle = sphereQuadrantArea(x0, y0) -
115 sphereQuadrantArea(x0, y1) -
116 sphereQuadrantArea(x1, y0) +
117 sphereQuadrantArea(x1, y1);
118 return solidAngle;
119 }
120
121 Cubemap CubemapUtils::create(Image& image, size_t dim, bool horizontal) {
122 Cubemap cm(dim);
123 Image temp(CubemapUtils::createCubemapImage(dim, horizontal));
124 CubemapUtils::setAllFacesFromCross(cm, temp);
125 std::swap(image, temp);
126 return cm;
127 }
128
129 void CubemapUtils::setFaceFromCross(Cubemap& cm, Cubemap::Face face, const Image& image) {
130 size_t dim = cm.getDimensions() + 2; // 2 extra per image, for seamlessness
131 size_t x = 0;
132 size_t y = 0;
133 switch (face) {
134 case Cubemap::Face::NX:
135 x = 0, y = dim;
136 break;
137 case Cubemap::Face::PX:
138 x = 2 * dim, y = dim;
139 break;
140 case Cubemap::Face::NY:
141 x = dim, y = 2 * dim;
142 break;
143 case Cubemap::Face::PY:
144 x = dim, y = 0;
145 break;
146 case Cubemap::Face::NZ:
147 x = 3 * dim, y = dim;
148 break;
149 case Cubemap::Face::PZ:
150 x = dim, y = dim;
151 break;
152 }
153 Image subImage;
154 subImage.subset(image, x + 1, y + 1, dim - 2, dim - 2);
155 cm.setImageForFace(face, subImage);
156 }
157
158 void CubemapUtils::setAllFacesFromCross(Cubemap& cm, const Image& image) {
159 CubemapUtils::setFaceFromCross(cm, Cubemap::Face::NX, image);
160 CubemapUtils::setFaceFromCross(cm, Cubemap::Face::PX, image);
161 CubemapUtils::setFaceFromCross(cm, Cubemap::Face::NY, image);
162 CubemapUtils::setFaceFromCross(cm, Cubemap::Face::PY, image);
163 CubemapUtils::setFaceFromCross(cm, Cubemap::Face::NZ, image);
164 CubemapUtils::setFaceFromCross(cm, Cubemap::Face::PZ, image);
165 }
166
167 Image CubemapUtils::createCubemapImage(size_t dim, bool horizontal) {
168 // always allocate 2 extra column and row / face, to allow the cubemap to be "seamless"
169 size_t width = 4 * (dim + 2);
170 size_t height = 3 * (dim + 2);
171 if (!horizontal) {
172 std::swap(width, height);
173 }
174
175 Image image(width, height);
176 memset(image.getData(), 0, image.getBytesPerRow() * height);
177 return image;
178 }
179
180 #ifndef FILAMENT_IBL_LITE
181
182 void CubemapUtils::equirectangularToCubemap(JobSystem& js, Cubemap& dst, const Image& src) {
183 const size_t width = src.getWidth();
184 const size_t height = src.getHeight();
185
186 auto toRectilinear = [width, height](float3 s) -> float2 {
187 float xf = std::atan2(s.x, s.z) * F_1_PI; // range [-1.0, 1.0]
188 float yf = std::asin(s.y) * (2 * F_1_PI); // range [-1.0, 1.0]
189 xf = (xf + 1.0f) * 0.5f * (width - 1); // range [0, width [
190 yf = (1.0f - yf) * 0.5f * (height - 1); // range [0, height[
191 return float2(xf, yf);
192 };
193
194 process<EmptyState>(dst, js,
195 [&](EmptyState&, size_t y, Cubemap::Face f, Cubemap::Texel* data, size_t dim) {
196 for (size_t x=0 ; x<dim ; ++x, ++data) {
197 // calculate how many samples we need based on dx, dy in the source
198 // x = cos(phi) sin(theta)
199 // y = sin(phi)
200 // z = cos(phi) cos(theta)
201
202 // here we try to figure out how many samples we need, by evaluating the surface
203 // (in pixels) in the equirectangular -- we take the bounding box of the
204 // projection of the cubemap texel's corners.
205
206 auto pos0 = toRectilinear(dst.getDirectionFor(f, x + 0.0f, y + 0.0f)); // make sure to use the float version
207 auto pos1 = toRectilinear(dst.getDirectionFor(f, x + 1.0f, y + 0.0f)); // make sure to use the float version
208 auto pos2 = toRectilinear(dst.getDirectionFor(f, x + 0.0f, y + 1.0f)); // make sure to use the float version
209 auto pos3 = toRectilinear(dst.getDirectionFor(f, x + 1.0f, y + 1.0f)); // make sure to use the float version
210 const float minx = std::min(pos0.x, std::min(pos1.x, std::min(pos2.x, pos3.x)));
211 const float maxx = std::max(pos0.x, std::max(pos1.x, std::max(pos2.x, pos3.x)));
212 const float miny = std::min(pos0.y, std::min(pos1.y, std::min(pos2.y, pos3.y)));
213 const float maxy = std::max(pos0.y, std::max(pos1.y, std::max(pos2.y, pos3.y)));
214 const float dx = std::max(1.0f, maxx - minx);
215 const float dy = std::max(1.0f, maxy - miny);
216 const size_t numSamples = size_t(dx * dy);
217
218 const float iNumSamples = 1.0f / numSamples;
219 float3 c = 0;
220 for (size_t sample = 0; sample < numSamples; sample++) {
221 // Generate numSamples in our destination pixels and map them to input pixels
222 const float2 h = hammersley(uint32_t(sample), iNumSamples);
223 const float3 s(dst.getDirectionFor(f, x + h.x, y + h.y));
224 auto pos = toRectilinear(s);
225
226 // we can't use filterAt() here because it reads past the width/height
227 // which is okay for cubmaps but not for square images
228
229 // TODO: the sample should be weighed by the area it covers in the cubemap texel
230
231 c += Cubemap::sampleAt(src.getPixelRef((uint32_t)pos.x, (uint32_t)pos.y));
232 }
233 c *= iNumSamples;
234
235 Cubemap::writeAt(data, c);
236 }
237 });
238 }
239
240 void CubemapUtils::cubemapToEquirectangular(JobSystem& js, Image& dst, const Cubemap& src) {
241 const float w = dst.getWidth();
242 const float h = dst.getHeight();
243 auto parallelJobTask = [&](size_t j0, size_t count) {
244 for (size_t j = j0; j < j0 + count; j++) {
245 for (size_t i = 0; i < w; i++) {
246 float3 c = 0;
247 const size_t numSamples = 64; // TODO: how to chose numsamples
248 for (size_t sample = 0; sample < numSamples; sample++) {
249 const float2 u = hammersley(uint32_t(sample), 1.0f / numSamples);
250 float x = 2.0f * (i + u.x) / w - 1.0f;
251 float y = 1.0f - 2.0f * (j + u.y) / h;
252 float theta = x * F_PI;
253 float phi = y * F_PI * 0.5;
254 float3 s = {
255 std::cos(phi) * std::sin(theta),
256 std::sin(phi),
257 std::cos(phi) * std::cos(theta) };
258 c += src.filterAt(s);
259 }
260 Cubemap::writeAt(dst.getPixelRef(i, j), c * (1.0f / numSamples));
261 }
262 }
263 };
264
265 auto job = jobs::parallel_for(js, nullptr, 0, uint32_t(h),
266 std::ref(parallelJobTask), jobs::CountSplitter<1, 8>());
267 js.runAndWait(job);
268 }
269
270 void CubemapUtils::cubemapToOctahedron(JobSystem& js, Image& dst, const Cubemap& src) {
271 const float w = dst.getWidth();
272 const float h = dst.getHeight();
273 auto parallelJobTask = [&](size_t j0, size_t count) {
274 for (size_t j = j0; j < j0 + count; j++) {
275 for (size_t i = 0; i < w; i++) {
276 float3 c = 0;
277 const size_t numSamples = 64; // TODO: how to chose numsamples
278 for (size_t sample = 0; sample < numSamples; sample++) {
279 const float2 u = hammersley(uint32_t(sample), 1.0f / numSamples);
280 float x = 2.0f * (i + u.x) / w - 1.0f;
281 float z = 2.0f * (j + u.y) / h - 1.0f;
282 float y;
283 if (std::abs(z) > (1.0f - std::abs(x))) {
284 float u = x < 0 ? std::abs(z) - 1 : 1 - std::abs(z);
285 float v = z < 0 ? std::abs(x) - 1 : 1 - std::abs(x);
286 x = u;
287 z = v;
288 y = (std::abs(x) + std::abs(z)) - 1.0f;
289 } else {
290 y = 1.0f - (std::abs(x) + std::abs(z));
291 }
292 c += src.filterAt({x, y, z});
293 }
294 Cubemap::writeAt(dst.getPixelRef(i, j), c * (1.0f / numSamples));
295 }
296 }
297 };
298
299 auto job = jobs::parallel_for(js, nullptr, 0, uint32_t(h),
300 std::ref(parallelJobTask), jobs::CountSplitter<1, 8>());
301 js.runAndWait(job);
302 }
303
304 void CubemapUtils::crossToCubemap(JobSystem& js, Cubemap& dst, const Image& src) {
305 process<EmptyState>(dst, js,
306 [&](EmptyState&, size_t iy, Cubemap::Face f, Cubemap::Texel* data, size_t dimension) {
307 for (size_t ix = 0; ix < dimension; ++ix, ++data) {
308 // find offsets from face
309 size_t x = ix;
310 size_t y = iy;
311 size_t dx = 0;
312 size_t dy = 0;
313 size_t dim = std::max(src.getHeight(), src.getWidth()) / 4;
314
315 switch (f) {
316 case Cubemap::Face::NX:
317 dx = 0, dy = dim;
318 break;
319 case Cubemap::Face::PX:
320 dx = 2 * dim, dy = dim;
321 break;
322 case Cubemap::Face::NY:
323 dx = dim, dy = 2 * dim;
324 break;
325 case Cubemap::Face::PY:
326 dx = dim, dy = 0;
327 break;
328 case Cubemap::Face::NZ:
329 if (src.getHeight() > src.getWidth()) {
330 dx = dim, dy = 3 * dim;
331 x = dimension - 1 - ix;
332 y = dimension - 1 - iy;
333 } else {
334 dx = 3 * dim, dy = dim;
335 }
336 break;
337 case Cubemap::Face::PZ:
338 dx = dim, dy = dim;
339 break;
340 }
341
342 size_t sampleCount = std::max(size_t(1), dim / dimension);
343 sampleCount = std::min(size_t(256), sampleCount * sampleCount);
344 for (size_t i = 0; i < sampleCount; i++) {
345 const float2 h = hammersley(uint32_t(i), 1.0f / sampleCount);
346 size_t u = dx + size_t((x + h.x) * dim / dimension);
347 size_t v = dy + size_t((y + h.y) * dim / dimension);
348 Cubemap::writeAt(data, Cubemap::sampleAt(src.getPixelRef(u, v)));
349 }
350 }
351 });
352 }
353
354 const char* CubemapUtils::getFaceName(Cubemap::Face face) {
355 switch (face) {
356 case Cubemap::Face::NX: return "nx";
357 case Cubemap::Face::PX: return "px";
358 case Cubemap::Face::NY: return "ny";
359 case Cubemap::Face::PY: return "py";
360 case Cubemap::Face::NZ: return "nz";
361 case Cubemap::Face::PZ: return "pz";
362 }
363 }
364
365 void CubemapUtils::mirrorCubemap(JobSystem& js, Cubemap& dst, const Cubemap& src) {
366 processSingleThreaded<EmptyState>(dst, js,
367 [&](EmptyState&, size_t y, Cubemap::Face f, Cubemap::Texel* data, size_t dim) {
368 for (size_t x=0 ; x<dim ; ++x, ++data) {
369 const float3 N(dst.getDirectionFor(f, x, y));
370 Cubemap::writeAt(data, src.sampleAt(float3{ -N.x, N.y, N.z }));
371 }
372 });
373 }
374
375 void CubemapUtils::generateUVGrid(JobSystem& js, Cubemap& cml, size_t gridFrequencyX, size_t gridFrequencyY) {
376 Cubemap::Texel const colors[6] = {
377 { 1, 1, 1 }, // +X / r - white
378 { 1, 0, 0 }, // -X / l - red
379 { 0, 0, 1 }, // +Y / t - blue
380 { 0, 1, 0 }, // -Y / b - green
381 { 1, 1, 0 }, // +z / fr - yellow
382 { 1, 0, 1 }, // -Z / bk - magenta
383 };
384 const float uvGridHDRIntensity = 5.0f;
385 size_t gridSizeX = cml.getDimensions() / gridFrequencyX;
386 size_t gridSizeY = cml.getDimensions() / gridFrequencyY;
387 CubemapUtils::process<EmptyState>(cml, js,
388 [ & ](EmptyState&,
389 size_t y, Cubemap::Face f, Cubemap::Texel* data, size_t dim) {
390 for (size_t x = 0; x < dim; ++x, ++data) {
391 bool grid = bool(((x / gridSizeX) ^ (y / gridSizeY)) & 1);
392 Cubemap::Texel t = grid ? colors[(int)f] * uvGridHDRIntensity : 0;
393 Cubemap::writeAt(data, t);
394 }
395 });
396 }
397 #endif
398
399 } // namespace ibl
400 } // namespace filament