/* Copyright (c) 2014-2018, NVIDIA CORPORATION. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of NVIDIA CORPORATION nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ //- // This utility compresses a normal(x,y,z) to a uint and decompresses it #define C_Stack_Max 3.402823466e+38f uint CompressUnitVec(vec3 nv) { // map to octahedron and then flatten to 2D (see 'Octahedron Environment Maps' by Engelhardt & Dachsbacher) if((nv.x < C_Stack_Max) && !isinf(nv.x)) { const float d = 32767.0f / (abs(nv.x) + abs(nv.y) + abs(nv.z)); int x = int(roundEven(nv.x * d)); int y = int(roundEven(nv.y * d)); if(nv.z < 0.0f) { const int maskx = x >> 31; const int masky = y >> 31; const int tmp = 32767 + maskx + masky; const int tmpx = x; x = (tmp - (y ^ masky)) ^ maskx; y = (tmp - (tmpx ^ maskx)) ^ masky; } uint packed = (uint(y + 32767) << 16) | uint(x + 32767); if(packed == ~0u) return ~0x1u; return packed; } else { return ~0u; } } float ShortToFloatM11(const int v) // linearly maps a short 32767-32768 to a float -1-+1 //!! opt.? { return (v >= 0) ? (uintBitsToFloat(0x3F800000u | (uint(v) << 8)) - 1.0f) : (uintBitsToFloat((0x80000000u | 0x3F800000u) | (uint(-v) << 8)) + 1.0f); } vec3 DecompressUnitVec(uint packed) { if(packed != ~0u) // sanity check, not needed as isvalid_unit_vec is called earlier { int x = int(packed & 0xFFFFu) - 32767; int y = int(packed >> 16) - 32767; const int maskx = x >> 31; const int masky = y >> 31; const int tmp0 = 32767 + maskx + masky; const int ymask = y ^ masky; const int tmp1 = tmp0 - (x ^ maskx); const int z = tmp1 - ymask; float zf; if(z < 0) { x = (tmp0 - ymask) ^ maskx; y = tmp1 ^ masky; zf = uintBitsToFloat((0x80000000u | 0x3F800000u) | (uint(-z) << 8)) + 1.0f; } else { zf = uintBitsToFloat(0x3F800000u | (uint(z) << 8)) - 1.0f; } return normalize(vec3(ShortToFloatM11(x), ShortToFloatM11(y), zf)); } else { return vec3(C_Stack_Max); } } //------------------------------------------------------------------------------------------------- // Avoiding self intersections (see Ray Tracing Gems, Ch. 6) // vec3 OffsetRay(in vec3 p, in vec3 n) { const float intScale = 256.0f; const float floatScale = 1.0f / 65536.0f; const float origin = 1.0f / 32.0f; ivec3 of_i = ivec3(intScale * n.x, intScale * n.y, intScale * n.z); vec3 p_i = vec3(intBitsToFloat(floatBitsToInt(p.x) + ((p.x < 0) ? -of_i.x : of_i.x)), intBitsToFloat(floatBitsToInt(p.y) + ((p.y < 0) ? -of_i.y : of_i.y)), intBitsToFloat(floatBitsToInt(p.z) + ((p.z < 0) ? -of_i.z : of_i.z))); return vec3(abs(p.x) < origin ? p.x + floatScale * n.x : p_i.x, // abs(p.y) < origin ? p.y + floatScale * n.y : p_i.y, // abs(p.z) < origin ? p.z + floatScale * n.z : p_i.z); } //////////////////////////// AO ////////////////////////////////////// #define EPS 0.05 const float M_PI = 3.141592653589; void ComputeDefaultBasis(const vec3 normal, out vec3 x, out vec3 y) { // ZAP's default coordinate system for compatibility vec3 z = normal; const float yz = -z.y * z.z; y = normalize(((abs(z.z) > 0.99999f) ? vec3(-z.x * z.y, 1.0f - z.y * z.y, yz) : vec3(-z.x * z.z, yz, 1.0f - z.z * z.z))); x = cross(y, z); } //------------------------------------------------------------------------------------------------- // Random //------------------------------------------------------------------------------------------------- // Generate a random unsigned int from two unsigned int values, using 16 pairs // of rounds of the Tiny Encryption Algorithm. See Zafar, Olano, and Curtis, // "GPU Random Numbers via the Tiny Encryption Algorithm" uint tea(uint val0, uint val1) { uint v0 = val0; uint v1 = val1; uint s0 = 0; for(uint n = 0; n < 16; n++) { s0 += 0x9e3779b9; v0 += ((v1 << 4) + 0xa341316c) ^ (v1 + s0) ^ ((v1 >> 5) + 0xc8013ea4); v1 += ((v0 << 4) + 0xad90777d) ^ (v0 + s0) ^ ((v0 >> 5) + 0x7e95761e); } return v0; } uvec2 pcg2d(uvec2 v) { v = v * 1664525u + 1013904223u; v.x += v.y * 1664525u; v.y += v.x * 1664525u; v = v ^ (v >> 16u); v.x += v.y * 1664525u; v.y += v.x * 1664525u; v = v ^ (v >> 16u); return v; } // Generate a random unsigned int in [0, 2^24) given the previous RNG state // using the Numerical Recipes linear congruential generator uint lcg(inout uint prev) { uint LCG_A = 1664525u; uint LCG_C = 1013904223u; prev = (LCG_A * prev + LCG_C); return prev & 0x00FFFFFF; } // Generate a random float in [0, 1) given the previous RNG state float rnd(inout uint seed) { return (float(lcg(seed)) / float(0x01000000)); }