Packing.hlsl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. #ifndef UNITY_PACKING_INCLUDED
  2. #define UNITY_PACKING_INCLUDED
  3. //-----------------------------------------------------------------------------
  4. // Normal packing
  5. //-----------------------------------------------------------------------------
  6. real3 PackNormalMaxComponent(real3 n)
  7. {
  8. return (n / Max3(abs(n.x), abs(n.y), abs(n.z))) * 0.5 + 0.5;
  9. }
  10. real3 UnpackNormalMaxComponent(real3 n)
  11. {
  12. return normalize(n * 2.0 - 1.0);
  13. }
  14. // Ref: http://www.vis.uni-stuttgart.de/~engelhts/paper/vmvOctaMaps.pdf
  15. // Encode with Oct, this function work with any size of output
  16. // return real between [-1, 1]
  17. real2 PackNormalOctRectEncode(real3 n)
  18. {
  19. // Perform planar projection.
  20. real3 p = n * rcp(dot(abs(n), 1.0));
  21. real x = p.x, y = p.y, z = p.z;
  22. // Unfold the octahedron.
  23. // Also correct the aspect ratio from 2:1 to 1:1.
  24. real r = saturate(0.5 - 0.5 * x + 0.5 * y);
  25. real g = x + y;
  26. // Negative hemisphere on the left, positive on the right.
  27. return real2(CopySign(r, z), g);
  28. }
  29. real3 UnpackNormalOctRectEncode(real2 f)
  30. {
  31. real r = f.r, g = f.g;
  32. // Solve for {x, y, z} given {r, g}.
  33. real x = 0.5 + 0.5 * g - abs(r);
  34. real y = g - x;
  35. real z = max(1.0 - abs(x) - abs(y), REAL_EPS); // EPS is absolutely crucial for anisotropy
  36. real3 p = real3(x, y, CopySign(z, r));
  37. return normalize(p);
  38. }
  39. // Ref: http://jcgt.org/published/0003/02/01/paper.pdf
  40. // Encode with Oct, this function work with any size of output
  41. // return float between [-1, 1]
  42. float2 PackNormalOctQuadEncode(float3 n)
  43. {
  44. //float l1norm = dot(abs(n), 1.0);
  45. //float2 res0 = n.xy * (1.0 / l1norm);
  46. //float2 val = 1.0 - abs(res0.yx);
  47. //return (n.zz < float2(0.0, 0.0) ? (res0 >= 0.0 ? val : -val) : res0);
  48. // Optimized version of above code:
  49. n *= rcp(dot(abs(n), 1.0));
  50. float t = saturate(-n.z);
  51. return n.xy + (n.xy >= 0.0 ? t : -t);
  52. }
  53. float3 UnpackNormalOctQuadEncode(float2 f)
  54. {
  55. float3 n = float3(f.x, f.y, 1.0 - abs(f.x) - abs(f.y));
  56. //float2 val = 1.0 - abs(n.yx);
  57. //n.xy = (n.zz < float2(0.0, 0.0) ? (n.xy >= 0.0 ? val : -val) : n.xy);
  58. // Optimized version of above code:
  59. float t = max(-n.z, 0.0);
  60. n.xy += n.xy >= 0.0 ? -t.xx : t.xx;
  61. return normalize(n);
  62. }
  63. real2 PackNormalHemiOctEncode(real3 n)
  64. {
  65. real l1norm = dot(abs(n), 1.0);
  66. real2 res = n.xy * (1.0 / l1norm);
  67. return real2(res.x + res.y, res.x - res.y);
  68. }
  69. real3 UnpackNormalHemiOctEncode(real2 f)
  70. {
  71. real2 val = real2(f.x + f.y, f.x - f.y) * 0.5;
  72. real3 n = real3(val, 1.0 - dot(abs(val), 1.0));
  73. return normalize(n);
  74. }
  75. // Tetrahedral encoding - Looks like Tetra encoding 10:10 + 2 is similar to oct 11:11, as oct is cheaper prefer it
  76. // To generate the basisNormal below we use these 4 vertex of a regular tetrahedron
  77. // v0 = float3(1.0, 0.0, -1.0 / sqrt(2.0));
  78. // v1 = float3(-1.0, 0.0, -1.0 / sqrt(2.0));
  79. // v2 = float3(0.0, 1.0, 1.0 / sqrt(2.0));
  80. // v3 = float3(0.0, -1.0, 1.0 / sqrt(2.0));
  81. // Then we normalize the average of each face's vertices
  82. // normalize(v0 + v1 + v2), etc...
  83. static const real3 tetraBasisNormal[4] =
  84. {
  85. real3(0., 0.816497, -0.57735),
  86. real3(-0.816497, 0., 0.57735),
  87. real3(0.816497, 0., 0.57735),
  88. real3(0., -0.816497, -0.57735)
  89. };
  90. // Then to get the local matrix (with z axis rotate to basisNormal) use GetLocalFrame(basisNormal[xxx])
  91. static const real3x3 tetraBasisArray[4] =
  92. {
  93. real3x3(-1., 0., 0.,0., 0.57735, 0.816497,0., 0.816497, -0.57735),
  94. real3x3(0., -1., 0.,0.57735, 0., 0.816497,-0.816497, 0., 0.57735),
  95. real3x3(0., 1., 0.,-0.57735, 0., 0.816497,0.816497, 0., 0.57735),
  96. real3x3(1., 0., 0.,0., -0.57735, 0.816497,0., -0.816497, -0.57735)
  97. };
  98. // Return [-1..1] vector2 oriented in plane of the faceIndex of a regular tetrahedron
  99. real2 PackNormalTetraEncode(float3 n, out uint faceIndex)
  100. {
  101. // Retrieve the tetrahedra's face for the normal direction
  102. // It is the one with the greatest dot value with face normal
  103. real dot0 = dot(n, tetraBasisNormal[0]);
  104. real dot1 = dot(n, tetraBasisNormal[1]);
  105. real dot2 = dot(n, tetraBasisNormal[2]);
  106. real dot3 = dot(n, tetraBasisNormal[3]);
  107. real maxi0 = max(dot0, dot1);
  108. real maxi1 = max(dot2, dot3);
  109. real maxi = max(maxi0, maxi1);
  110. // Get the index from the greatest dot
  111. if (maxi == dot0)
  112. faceIndex = 0;
  113. else if (maxi == dot1)
  114. faceIndex = 1;
  115. else if (maxi == dot2)
  116. faceIndex = 2;
  117. else //(maxi == dot3)
  118. faceIndex = 3;
  119. // Rotate n into this local basis
  120. n = mul(tetraBasisArray[faceIndex], n);
  121. // Project n onto the local plane
  122. return n.xy;
  123. }
  124. // Assume f [-1..1]
  125. real3 UnpackNormalTetraEncode(real2 f, uint faceIndex)
  126. {
  127. // Recover n from local plane
  128. real3 n = real3(f.xy, sqrt(1.0 - dot(f.xy, f.xy)));
  129. // Inverse of transform PackNormalTetraEncode (just swap order in mul as we have a rotation)
  130. return mul(n, tetraBasisArray[faceIndex]);
  131. }
  132. // Unpack from normal map
  133. real3 UnpackNormalRGB(real4 packedNormal, real scale = 1.0)
  134. {
  135. real3 normal;
  136. normal.xyz = packedNormal.rgb * 2.0 - 1.0;
  137. normal.xy *= scale;
  138. return normal;
  139. }
  140. real3 UnpackNormalRGBNoScale(real4 packedNormal)
  141. {
  142. return packedNormal.rgb * 2.0 - 1.0;
  143. }
  144. real3 UnpackNormalAG(real4 packedNormal, real scale = 1.0)
  145. {
  146. real3 normal;
  147. normal.xy = packedNormal.ag * 2.0 - 1.0;
  148. normal.xy *= scale;
  149. normal.z = sqrt(1.0 - saturate(dot(normal.xy, normal.xy)));
  150. return normal;
  151. }
  152. // Unpack normal as DXT5nm (1, y, 0, x) or BC5 (x, y, 0, 1)
  153. real3 UnpackNormalmapRGorAG(real4 packedNormal, real scale = 1.0)
  154. {
  155. // Convert to (?, y, 0, x)
  156. packedNormal.a *= packedNormal.r;
  157. return UnpackNormalAG(packedNormal, scale);
  158. }
  159. real3 UnpackNormal(real4 packedNormal)
  160. {
  161. #if defined(UNITY_NO_DXT5nm)
  162. return UnpackNormalRGBNoScale(packedNormal);
  163. #else
  164. // Compiler will optimize the scale away
  165. return UnpackNormalmapRGorAG(packedNormal, 1.0);
  166. #endif
  167. }
  168. real3 UnpackNormalScale(real4 packedNormal, real bumpScale)
  169. {
  170. #if defined(UNITY_NO_DXT5nm)
  171. return UnpackNormalRGB(packedNormal, bumpScale);
  172. #else
  173. return UnpackNormalmapRGorAG(packedNormal, bumpScale);
  174. #endif
  175. }
  176. //-----------------------------------------------------------------------------
  177. // HDR packing
  178. //-----------------------------------------------------------------------------
  179. // HDR Packing not defined in GLES2
  180. #if !defined(SHADER_API_GLES)
  181. // Ref: http://realtimecollisiondetection.net/blog/?p=15
  182. real4 PackToLogLuv(real3 vRGB)
  183. {
  184. // M matrix, for encoding
  185. const real3x3 M = real3x3(
  186. 0.2209, 0.3390, 0.4184,
  187. 0.1138, 0.6780, 0.7319,
  188. 0.0102, 0.1130, 0.2969);
  189. real4 vResult;
  190. real3 Xp_Y_XYZp = mul(vRGB, M);
  191. Xp_Y_XYZp = max(Xp_Y_XYZp, real3(1e-6, 1e-6, 1e-6));
  192. vResult.xy = Xp_Y_XYZp.xy / Xp_Y_XYZp.z;
  193. real Le = 2.0 * log2(Xp_Y_XYZp.y) + 127.0;
  194. vResult.w = frac(Le);
  195. vResult.z = (Le - (floor(vResult.w * 255.0)) / 255.0) / 255.0;
  196. return vResult;
  197. }
  198. real3 UnpackFromLogLuv(real4 vLogLuv)
  199. {
  200. // Inverse M matrix, for decoding
  201. const real3x3 InverseM = real3x3(
  202. 6.0014, -2.7008, -1.7996,
  203. -1.3320, 3.1029, -5.7721,
  204. 0.3008, -1.0882, 5.6268);
  205. real Le = vLogLuv.z * 255.0 + vLogLuv.w;
  206. real3 Xp_Y_XYZp;
  207. Xp_Y_XYZp.y = exp2((Le - 127.0) / 2.0);
  208. Xp_Y_XYZp.z = Xp_Y_XYZp.y / vLogLuv.y;
  209. Xp_Y_XYZp.x = vLogLuv.x * Xp_Y_XYZp.z;
  210. real3 vRGB = mul(Xp_Y_XYZp, InverseM);
  211. return max(vRGB, real3(0.0, 0.0, 0.0));
  212. }
  213. // The standard 32-bit HDR color format
  214. uint PackToR11G11B10f(float3 rgb)
  215. {
  216. uint r = (f32tof16(rgb.x) << 17) & 0xFFE00000;
  217. uint g = (f32tof16(rgb.y) << 6) & 0x001FFC00;
  218. uint b = (f32tof16(rgb.z) >> 5) & 0x000003FF;
  219. return r | g | b;
  220. }
  221. float3 UnpackFromR11G11B10f(uint rgb)
  222. {
  223. float r = f16tof32((rgb >> 17) & 0x7FF0);
  224. float g = f16tof32((rgb >> 6) & 0x7FF0);
  225. float b = f16tof32((rgb << 5) & 0x7FE0);
  226. return float3(r, g, b);
  227. }
  228. #endif // SHADER_API_GLES
  229. //-----------------------------------------------------------------------------
  230. // Quaternion packing
  231. //-----------------------------------------------------------------------------
  232. // Ref: https://cedec.cesa.or.jp/2015/session/ENG/14698.html The Rendering Materials of Far Cry 4
  233. /*
  234. // This is GCN intrinsic
  235. uint FindBiggestComponent(real4 q)
  236. {
  237. uint xyzIndex = CubeMapFaceID(q.x, q.y, q.z) * 0.5f;
  238. uint wIndex = 3;
  239. bool wBiggest = abs(q.w) > max3(abs(q.x), qbs(q.y), qbs(q.z));
  240. return wBiggest ? wIndex : xyzIndex;
  241. }
  242. // Pack a quaternion into a 10:10:10:2
  243. real4 PackQuat(real4 quat)
  244. {
  245. uint index = FindBiggestComponent(quat);
  246. if (index == 0) quat = quat.yzwx;
  247. if (index == 1) quat = quat.xzwy;
  248. if (index == 2) quat = quat.xywz;
  249. real4 packedQuat;
  250. packedQuat.xyz = quat.xyz * FastSign(quat.w) * sqrt(0.5) + 0.5;
  251. packedQuat.w = index / 3.0;
  252. return packedQuat;
  253. }
  254. */
  255. // Unpack a quaternion from a 10:10:10:2
  256. real4 UnpackQuat(real4 packedQuat)
  257. {
  258. uint index = (uint)(packedQuat.w * 3.0);
  259. real4 quat;
  260. quat.xyz = packedQuat.xyz * sqrt(2.0) - (1.0 / sqrt(2.0));
  261. quat.w = sqrt(1.0 - saturate(dot(quat.xyz, quat.xyz)));
  262. if (index == 0) quat = quat.wxyz;
  263. if (index == 1) quat = quat.xwyz;
  264. if (index == 2) quat = quat.xywz;
  265. return quat;
  266. }
  267. // Integer and Float packing not defined in GLES2
  268. #if !defined(SHADER_API_GLES)
  269. //-----------------------------------------------------------------------------
  270. // Integer packing
  271. //-----------------------------------------------------------------------------
  272. // Packs an integer stored using at most 'numBits' into a [0..1] real.
  273. real PackInt(uint i, uint numBits)
  274. {
  275. uint maxInt = (1u << numBits) - 1u;
  276. return saturate(i * rcp(maxInt));
  277. }
  278. // Unpacks a [0..1] real into an integer of size 'numBits'.
  279. uint UnpackInt(real f, uint numBits)
  280. {
  281. uint maxInt = (1u << numBits) - 1u;
  282. return (uint)(f * maxInt + 0.5); // Round instead of truncating
  283. }
  284. // Packs a [0..255] integer into a [0..1] real.
  285. real PackByte(uint i)
  286. {
  287. return PackInt(i, 8);
  288. }
  289. // Unpacks a [0..1] real into a [0..255] integer.
  290. uint UnpackByte(real f)
  291. {
  292. return UnpackInt(f, 8);
  293. }
  294. // Packs a [0..65535] integer into a [0..1] real.
  295. real PackShort(uint i)
  296. {
  297. return PackInt(i, 16);
  298. }
  299. // Unpacks a [0..1] real into a [0..65535] integer.
  300. uint UnpackShort(real f)
  301. {
  302. return UnpackInt(f, 16);
  303. }
  304. // Packs 8 lowermost bits of a [0..65535] integer into a [0..1] real.
  305. real PackShortLo(uint i)
  306. {
  307. uint lo = BitFieldExtract(i, 0u, 8u);
  308. return PackInt(lo, 8);
  309. }
  310. // Packs 8 uppermost bits of a [0..65535] integer into a [0..1] real.
  311. real PackShortHi(uint i)
  312. {
  313. uint hi = BitFieldExtract(i, 8u, 8u);
  314. return PackInt(hi, 8);
  315. }
  316. real Pack2Byte(real2 inputs)
  317. {
  318. real2 temp = inputs * real2(255.0, 255.0);
  319. temp.x *= 256.0;
  320. temp = round(temp);
  321. real combined = temp.x + temp.y;
  322. return combined * (1.0 / 65535.0);
  323. }
  324. real2 Unpack2Byte(real inputs)
  325. {
  326. real temp = round(inputs * 65535.0);
  327. real ipart;
  328. real fpart = modf(temp / 256.0, ipart);
  329. real2 result = real2(ipart, round(256.0 * fpart));
  330. return result * (1.0 / real2(255.0, 255.0));
  331. }
  332. // Encode a real in [0..1] and an int in [0..maxi - 1] as a real [0..1] to be store in log2(precision) bit
  333. // maxi must be a power of two and define the number of bit dedicated 0..1 to the int part (log2(maxi))
  334. // Example: precision is 256.0, maxi is 2, i is [0..1] encode on 1 bit. f is [0..1] encode on 7 bit.
  335. // Example: precision is 256.0, maxi is 4, i is [0..3] encode on 2 bit. f is [0..1] encode on 6 bit.
  336. // Example: precision is 256.0, maxi is 8, i is [0..7] encode on 3 bit. f is [0..1] encode on 5 bit.
  337. // ...
  338. // Example: precision is 1024.0, maxi is 8, i is [0..7] encode on 3 bit. f is [0..1] encode on 7 bit.
  339. //...
  340. real PackFloatInt(real f, uint i, real maxi, real precision)
  341. {
  342. // Constant
  343. real precisionMinusOne = precision - 1.0;
  344. real t1 = ((precision / maxi) - 1.0) / precisionMinusOne;
  345. real t2 = (precision / maxi) / precisionMinusOne;
  346. return t1 * f + t2 * real(i);
  347. }
  348. void UnpackFloatInt(real val, real maxi, real precision, out real f, out uint i)
  349. {
  350. // Constant
  351. real precisionMinusOne = precision - 1.0;
  352. real t1 = ((precision / maxi) - 1.0) / precisionMinusOne;
  353. real t2 = (precision / maxi) / precisionMinusOne;
  354. // extract integer part
  355. i = int((val / t2) + rcp(precisionMinusOne)); // + rcp(precisionMinusOne) to deal with precision issue (can't use round() as val contain the floating number
  356. // Now that we have i, solve formula in PackFloatInt for f
  357. //f = (val - t2 * real(i)) / t1 => convert in mads form
  358. f = saturate((-t2 * real(i) + val) / t1); // Saturate in case of precision issue
  359. }
  360. // Define various variante for ease of read
  361. real PackFloatInt8bit(real f, uint i, real maxi)
  362. {
  363. return PackFloatInt(f, i, maxi, 256.0);
  364. }
  365. void UnpackFloatInt8bit(real val, real maxi, out real f, out uint i)
  366. {
  367. UnpackFloatInt(val, maxi, 256.0, f, i);
  368. }
  369. real PackFloatInt10bit(real f, uint i, real maxi)
  370. {
  371. return PackFloatInt(f, i, maxi, 1024.0);
  372. }
  373. void UnpackFloatInt10bit(real val, real maxi, out real f, out uint i)
  374. {
  375. UnpackFloatInt(val, maxi, 1024.0, f, i);
  376. }
  377. real PackFloatInt16bit(real f, uint i, real maxi)
  378. {
  379. return PackFloatInt(f, i, maxi, 65536.0);
  380. }
  381. void UnpackFloatInt16bit(real val, real maxi, out real f, out uint i)
  382. {
  383. UnpackFloatInt(val, maxi, 65536.0, f, i);
  384. }
  385. //-----------------------------------------------------------------------------
  386. // Float packing
  387. //-----------------------------------------------------------------------------
  388. // src must be between 0.0 and 1.0
  389. uint PackFloatToUInt(real src, uint offset, uint numBits)
  390. {
  391. return UnpackInt(src, numBits) << offset;
  392. }
  393. real UnpackUIntToFloat(uint src, uint offset, uint numBits)
  394. {
  395. uint maxInt = (1u << numBits) - 1u;
  396. return real(BitFieldExtract(src, offset, numBits)) * rcp(maxInt);
  397. }
  398. uint PackToR10G10B10A2(real4 rgba)
  399. {
  400. return (PackFloatToUInt(rgba.x, 0, 10) |
  401. PackFloatToUInt(rgba.y, 10, 10) |
  402. PackFloatToUInt(rgba.z, 20, 10) |
  403. PackFloatToUInt(rgba.w, 30, 2));
  404. }
  405. real4 UnpackFromR10G10B10A2(uint rgba)
  406. {
  407. real4 output;
  408. output.x = UnpackUIntToFloat(rgba, 0, 10);
  409. output.y = UnpackUIntToFloat(rgba, 10, 10);
  410. output.z = UnpackUIntToFloat(rgba, 20, 10);
  411. output.w = UnpackUIntToFloat(rgba, 30, 2);
  412. return output;
  413. }
  414. // Both the input and the output are in the [0, 1] range.
  415. real2 PackFloatToR8G8(real f)
  416. {
  417. uint i = UnpackShort(f);
  418. return real2(PackShortLo(i), PackShortHi(i));
  419. }
  420. // Both the input and the output are in the [0, 1] range.
  421. real UnpackFloatFromR8G8(real2 f)
  422. {
  423. uint lo = UnpackByte(f.x);
  424. uint hi = UnpackByte(f.y);
  425. uint cb = (hi << 8) + lo;
  426. return PackShort(cb);
  427. }
  428. // Pack float2 (each of 12 bit) in 888
  429. float3 PackFloat2To888(float2 f)
  430. {
  431. uint2 i = (uint2)(f * 4095.5);
  432. uint2 hi = i >> 8;
  433. uint2 lo = i & 255;
  434. // 8 bit in lo, 4 bit in hi
  435. uint3 cb = uint3(lo, hi.x | (hi.y << 4));
  436. return cb / 255.0;
  437. }
  438. // Unpack 2 float of 12bit packed into a 888
  439. float2 Unpack888ToFloat2(float3 x)
  440. {
  441. uint3 i = (uint3)(x * 255.0);
  442. // 8 bit in lo, 4 bit in hi
  443. uint hi = i.z >> 4;
  444. uint lo = i.z & 15;
  445. uint2 cb = i.xy | uint2(lo << 8, hi << 8);
  446. return cb / 4095.0;
  447. }
  448. #endif // SHADER_API_GLES
  449. #endif // UNITY_PACKING_INCLUDED