AVProVideo.cginc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. //-----------------------------------------------------------------------------
  2. // Copyright 2015-2021 RenderHeads Ltd. All rights reserverd.
  3. //-----------------------------------------------------------------------------
  4. //#define AVPRO_CHEAP_GAMMA_CONVERSION
  5. #if defined (SHADERLAB_GLSL)
  6. #define AVPRO_CHEAP_GAMMA_CONVERSION
  7. #define INLINE
  8. #define FIXED float
  9. #define HALF float
  10. #define HALF2 vec2
  11. #define HALF3 vec3
  12. #define HALF4 vec4
  13. #define FLOAT2 vec2
  14. #define FLOAT3 vec3
  15. #define FLOAT4 vec4
  16. #define FIXED4 vec4
  17. #define FLOAT3X3 mat3
  18. #define FLOAT4X4 mat4
  19. #define LERP mix
  20. #else
  21. #define INLINE inline
  22. #define FIXED fixed
  23. #define HALF half
  24. #define HALF2 half2
  25. #define HALF3 half3
  26. #define HALF4 half4
  27. #define FLOAT2 float2
  28. #define FLOAT3 float3
  29. #define FLOAT4 float4
  30. #define FIXED4 fixed4
  31. #define FLOAT3X3 float3x3
  32. #define FLOAT4X4 float4x4
  33. #define LERP lerp
  34. #endif
  35. // Specify this so Unity doesn't automatically update our shaders.
  36. #define UNITY_SHADER_NO_UPGRADE 1
  37. //#pragma multi_compile __ XR_USE_BUILT_IN_EYE_VARIABLE
  38. // We use this method so that when Unity automatically updates the shader from the old
  39. // mul(UNITY_MATRIX_MVP.. to UnityObjectToClipPos that it only changes in one place.
  40. INLINE FLOAT4 XFormObjectToClip(FLOAT4 vertex)
  41. {
  42. #if defined(SHADERLAB_GLSL)
  43. return gl_ModelViewProjectionMatrix * vertex;
  44. #else
  45. return UnityObjectToClipPos(vertex);
  46. #endif
  47. }
  48. uniform FLOAT3 _WorldCameraPosition;
  49. uniform FLOAT3 _WorldCameraRight;
  50. INLINE bool IsStereoEyeLeft()
  51. {
  52. #if defined(FORCEEYE_LEFT)
  53. return true;
  54. #elif defined(FORCEEYE_RIGHT)
  55. return false;
  56. //#elif defined(USING_STEREO_MATRICES) || defined(XR_USE_BUILT_IN_EYE_VARIABLE)
  57. #elif defined(USING_STEREO_MATRICES)
  58. // Unity 5.4 has this new variable
  59. return (unity_StereoEyeIndex == 0);
  60. #elif defined (UNITY_DECLARE_MULTIVIEW)
  61. // OVR_multiview extension
  62. return (UNITY_VIEWID == 0);
  63. #else
  64. // NOTE: Bug #1165: _WorldSpaceCameraPos is not correct in multipass VR (when skybox is used) but UNITY_MATRIX_I_V seems to be
  65. #if defined(UNITY_MATRIX_I_V)
  66. FLOAT3 renderCameraPos = UNITY_MATRIX_I_V._m03_m13_m23;
  67. #else
  68. FLOAT3 renderCameraPos = _WorldSpaceCameraPos.xyz;
  69. #endif
  70. float fL = distance(_WorldCameraPosition - _WorldCameraRight, renderCameraPos);
  71. float fR = distance(_WorldCameraPosition + _WorldCameraRight, renderCameraPos);
  72. return (fL < fR);
  73. #endif
  74. }
  75. #if defined(STEREO_TOP_BOTTOM) || defined(STEREO_LEFT_RIGHT)
  76. FLOAT4 GetStereoScaleOffset(bool isLeftEye, bool isYFlipped)
  77. {
  78. FLOAT2 scale = FLOAT2(1.0, 1.0);
  79. FLOAT2 offset = FLOAT2(0.0, 0.0);
  80. // Top-Bottom
  81. #if defined(STEREO_TOP_BOTTOM)
  82. scale.y = 0.5;
  83. offset.y = 0.0;
  84. if (!isLeftEye)
  85. {
  86. offset.y = 0.5;
  87. }
  88. #if !defined(SHADERLAB_GLSL)
  89. //#if !defined(UNITY_UV_STARTS_AT_TOP) // UNITY_UV_STARTS_AT_TOP is for directx
  90. if (!isYFlipped)
  91. {
  92. // Currently this only runs for Android and Windows using DirectShow
  93. offset.y = 0.5 - offset.y;
  94. }
  95. //#endif
  96. #endif
  97. // Left-Right
  98. #elif defined(STEREO_LEFT_RIGHT)
  99. scale.x = 0.5;
  100. offset.x = 0.0;
  101. if (!isLeftEye)
  102. {
  103. offset.x = 0.5;
  104. }
  105. #endif
  106. return FLOAT4(scale, offset);
  107. }
  108. #endif
  109. #if defined(STEREO_DEBUG)
  110. INLINE FLOAT4 GetStereoDebugTint(bool isLeftEye)
  111. {
  112. FLOAT4 tint = FLOAT4(1.0, 1.0, 1.0, 1.0);
  113. #if defined(STEREO_TOP_BOTTOM) || defined(STEREO_LEFT_RIGHT) || defined(STEREO_CUSTOM_UV)
  114. FLOAT4 leftEyeColor = FLOAT4(0.0, 1.0, 0.0, 1.0); // green
  115. FLOAT4 rightEyeColor = FLOAT4(1.0, 0.0, 0.0, 1.0); // red
  116. if (isLeftEye)
  117. {
  118. tint = leftEyeColor;
  119. }
  120. else
  121. {
  122. tint = rightEyeColor;
  123. }
  124. #endif
  125. #if defined(UNITY_UV_STARTS_AT_TOP)
  126. //tint.b = 0.5;
  127. #endif
  128. /*#if defined(UNITY_SINGLE_PASS_STEREO) || defined(UNITY_STEREO_INSTANCING_ENABLED) || defined(UNITY_DECLARE_MULTIVIEW)
  129. tint.b = 1.0;
  130. #endif*/
  131. return tint;
  132. }
  133. #endif
  134. FLOAT2 ScaleZoomToFit(float targetWidth, float targetHeight, float sourceWidth, float sourceHeight)
  135. {
  136. #if defined(ALPHAPACK_TOP_BOTTOM)
  137. sourceHeight *= 0.5;
  138. #elif defined(ALPHAPACK_LEFT_RIGHT)
  139. sourceWidth *= 0.5;
  140. #endif
  141. float targetAspect = targetHeight / targetWidth;
  142. float sourceAspect = sourceHeight / sourceWidth;
  143. FLOAT2 scale = FLOAT2(1.0, sourceAspect / targetAspect);
  144. if (targetAspect < sourceAspect)
  145. {
  146. scale = FLOAT2(targetAspect / sourceAspect, 1.0);
  147. }
  148. return scale;
  149. }
  150. FLOAT4 OffsetAlphaPackingUV(FLOAT2 texelSize, FLOAT2 uv, bool flipVertical)
  151. {
  152. FLOAT4 result = uv.xyxy;
  153. // We don't want bilinear interpolation to cause bleeding
  154. // when reading the pixels at the edge of the packed areas.
  155. // So we shift the UV's by a fraction of a pixel so the edges don't get sampled.
  156. #if defined(ALPHAPACK_TOP_BOTTOM)
  157. float offset = texelSize.y * 1.5;
  158. result.y = LERP(0.0 + offset, 0.5 - offset, uv.y);
  159. result.w = result.y + 0.5;
  160. if (flipVertical)
  161. {
  162. // Flip vertically (and offset to put back in 0..1 range)
  163. result.yw = 1.0 - result.yw;
  164. result.yw = result.wy;
  165. }
  166. else
  167. {
  168. #if !defined(UNITY_UV_STARTS_AT_TOP)
  169. // For opengl we flip
  170. result.yw = result.wy;
  171. #endif
  172. }
  173. #elif defined(ALPHAPACK_LEFT_RIGHT)
  174. float offset = texelSize.x * 1.5;
  175. result.x = LERP(0.0 + offset, 0.5 - offset, uv.x);
  176. result.z = result.x + 0.5;
  177. if (flipVertical)
  178. {
  179. // Flip vertically (and offset to put back in 0..1 range)
  180. result.yw = 1.0 - result.yw;
  181. }
  182. #else
  183. if (flipVertical)
  184. {
  185. // Flip vertically (and offset to put back in 0..1 range)
  186. result.yw = 1.0 - result.yw;
  187. }
  188. #endif
  189. return result;
  190. }
  191. INLINE HALF3 GammaToLinear_ApproxPow(HALF3 col)
  192. {
  193. #if defined (SHADERLAB_GLSL)
  194. return pow(col, HALF3(2.2, 2.2, 2.2));
  195. #else
  196. return pow(col, HALF3(2.2h, 2.2h, 2.2h));
  197. #endif
  198. }
  199. INLINE HALF3 LinearToGamma_ApproxPow(HALF3 col)
  200. {
  201. #if defined (SHADERLAB_GLSL)
  202. return pow(col, HALF3(1.0/2.2, 1.0/2.2, 1.0/2.2));
  203. #else
  204. return pow(col, HALF3(1.0h/2.2h, 1.0h/2.2h, 1.0h/2.2h));
  205. #endif
  206. }
  207. // Approximate version from http://chilliant.blogspot.com.au/2012/08/srgb-approximations-for-hlsl.html?m=1
  208. // NOTE: This is about 4 instructions vs 10 instructions for the accurate version
  209. INLINE HALF3 GammaToLinear_ApproxFit(HALF3 col)
  210. {
  211. #if defined (SHADERLAB_GLSL)
  212. HALF a = 0.305306011;
  213. HALF b = 0.682171111;
  214. HALF c = 0.012522878;
  215. #else
  216. HALF a = 0.305306011h;
  217. HALF b = 0.682171111h;
  218. HALF c = 0.012522878h;
  219. #endif
  220. return col * (col * (col * a + b) + c);
  221. }
  222. // Approximate version from http://chilliant.blogspot.com.au/2012/08/srgb-approximations-for-hlsl.html?m=1
  223. INLINE HALF3 LinearToGamma_ApproxFit(HALF3 col)
  224. {
  225. #if defined (SHADERLAB_GLSL)
  226. HALF a = 0.416666667;
  227. HALF b = 0.055;
  228. HALF c = 0.0;
  229. HALF d = 1.055;
  230. #else
  231. HALF a = 0.416666667h;
  232. HALF b = 0.055h;
  233. HALF c = 0.0h;
  234. HALF d = 1.055h;
  235. #endif
  236. return max(d * pow(col, HALF3(a, a, a)) - b, c);
  237. }
  238. INLINE HALF3 GammaToLinear_Accurate(HALF3 col)
  239. {
  240. if (col.r <= 0.04045)
  241. col.r = col.r / 12.92;
  242. else
  243. col.r = pow((col.r + 0.055) / 1.055, 2.4);
  244. if (col.g <= 0.04045)
  245. col.g = col.g / 12.92;
  246. else
  247. col.g = pow((col.g + 0.055) / 1.055, 2.4);
  248. if (col.b <= 0.04045)
  249. col.b = col.b / 12.92;
  250. else
  251. col.b = pow((col.b + 0.055) / 1.055, 2.4);
  252. // NOTE: We tried to optimise the above, but actually the compiler does a better job..
  253. /*HALF3 a = col / 12.92;
  254. HALF3 b = pow((col + 0.055) / 1.055, 2.4);
  255. HALF3 c = step(col,0.04045);
  256. col = LERP(b, a, c);*/
  257. return col;
  258. }
  259. INLINE HALF3 LinearToGamma_Accurate(HALF3 col)
  260. {
  261. if (col.r <= 0.0031308)
  262. col.r = col.r * 12.92;
  263. else
  264. col.r = 1.055 * pow(col.r, 0.4166667) - 0.055;
  265. if (col.g <= 0.0031308)
  266. col.g = col.g * 12.92;
  267. else
  268. col.g = 1.055 * pow(col.g, 0.4166667) - 0.055;
  269. if (col.b <= 0.0031308)
  270. col.b = col.b * 12.92;
  271. else
  272. col.b = 1.055 * pow(col.b, 0.4166667) - 0.055;
  273. return col;
  274. }
  275. // http://entropymine.com/imageworsener/srgbformula/
  276. INLINE HALF3 GammaToLinear(HALF3 col)
  277. {
  278. #if defined(AVPRO_CHEAP_GAMMA_CONVERSION)
  279. return GammaToLinear_ApproxFit(col);
  280. #else
  281. return GammaToLinear_Accurate(col);
  282. #endif
  283. }
  284. // http://entropymine.com/imageworsener/srgbformula/
  285. INLINE HALF3 LinearToGamma(HALF3 col)
  286. {
  287. #if defined(AVPRO_CHEAP_GAMMA_CONVERSION)
  288. return LinearToGamma_ApproxFit(col);
  289. #else
  290. return LinearToGamma_Accurate(col);
  291. #endif
  292. }
  293. INLINE FLOAT3 ConvertYpCbCrToRGB(FLOAT3 YpCbCr, FLOAT4X4 YpCbCrTransform)
  294. {
  295. #if defined(SHADERLAB_GLSL)
  296. return clamp(FLOAT3X3(YpCbCrTransform) * (YpCbCr + YpCbCrTransform[3].xyz), 0.0, 1.0);
  297. #else
  298. return saturate(mul((FLOAT3X3)YpCbCrTransform, YpCbCr + YpCbCrTransform[3].xyz));
  299. #endif
  300. }
  301. #if defined(SHADERLAB_GLSL)
  302. #if __VERSION__ < 300
  303. #define TEX_EXTERNAL(sampler, uv) texture2D(sampler, uv.xy);
  304. #else
  305. #define TEX_EXTERNAL(sampler, uv) texture(sampler, uv.xy)
  306. #endif
  307. #endif
  308. INLINE HALF4 SampleRGBA(sampler2D tex, FLOAT2 uv)
  309. {
  310. #if defined(SHADERLAB_GLSL) // GLSL doesn't support tex2D, and Adreno GPU doesn't support passing sampler as a parameter, so just return if this is called
  311. return HALF4(1.0, 1.0, 0.0, 1.0);
  312. #else
  313. HALF4 rgba = tex2D(tex, uv);
  314. #if defined(APPLY_GAMMA)
  315. rgba.rgb = GammaToLinear(rgba.rgb);
  316. #endif
  317. return rgba;
  318. #endif
  319. }
  320. INLINE HALF4 SampleYpCbCr(sampler2D luma, sampler2D chroma, FLOAT2 uv, FLOAT4X4 YpCbCrTransform)
  321. {
  322. #if defined(SHADERLAB_GLSL) // GLSL doesn't support tex2D, and Adreno GPU doesn't support passing sampler as a parameter, so just return if this is called
  323. return HALF4(1.0, 1.0, 0.0, 1.0);
  324. #else
  325. #if defined(SHADER_API_METAL) || defined(SHADER_API_GLES) || defined(SHADER_API_GLES3)
  326. FLOAT3 YpCbCr = FLOAT3(tex2D(luma, uv).r, tex2D(chroma, uv).rg);
  327. #else
  328. FLOAT3 YpCbCr = FLOAT3(tex2D(luma, uv).r, tex2D(chroma, uv).ra);
  329. #endif
  330. HALF4 rgba = HALF4(ConvertYpCbCrToRGB(YpCbCr, YpCbCrTransform), 1.0);
  331. #if defined(APPLY_GAMMA)
  332. rgba.rgb = GammaToLinear(rgba.rgb);
  333. #endif
  334. return rgba;
  335. #endif
  336. }
  337. INLINE HALF SamplePackedAlpha(sampler2D tex, FLOAT2 uv)
  338. {
  339. #if defined(SHADERLAB_GLSL) // GLSL doesn't support tex2D, and Adreno GPU doesn't support passing sampler as a parameter, so just return if this is called
  340. return 0.0;
  341. #else
  342. HALF alpha;
  343. #if defined(USE_YPCBCR)
  344. alpha = (tex2D(tex, uv).r - 0.0625) * (255.0 / 219.0);
  345. #else
  346. HALF3 rgb = tex2D(tex, uv).rgb;
  347. #if defined(APPLY_GAMMA)
  348. rgb = GammaToLinear(rgb);
  349. #endif
  350. alpha = (rgb.r + rgb.g + rgb.b) / 3.0;
  351. #endif
  352. return alpha;
  353. #endif
  354. }
  355. #if defined(USE_HSBC)
  356. INLINE HALF3 ApplyHue(HALF3 color, HALF hue)
  357. {
  358. HALF angle = radians(hue);
  359. HALF3 k = HALF3(0.57735, 0.57735, 0.57735);
  360. HALF cosAngle = cos(angle);
  361. //Rodrigues' rotation formula
  362. return color * cosAngle + cross(k, color) * sin(angle) + k * dot(k, color) * (1.0 - cosAngle);
  363. }
  364. INLINE HALF3 ApplyHSBEffect(HALF3 color, FIXED4 hsbc)
  365. {
  366. HALF hue = hsbc.r * 360.0;
  367. HALF saturation = hsbc.g * 2.0;
  368. HALF brightness = hsbc.b * 2.0 - 1.0;
  369. HALF contrast = hsbc.a * 2.0;
  370. HALF3 result = color;
  371. result.rgb = ApplyHue(result, hue);
  372. result.rgb = (result - 0.5) * contrast + 0.5 + brightness;
  373. #if defined(SHADERLAB_GLSL)
  374. result.rgb = LERP(vec3(Luminance(result)), result, saturation);
  375. #else
  376. result.rgb = LERP(Luminance(result), result, saturation);
  377. #endif
  378. return result;
  379. }
  380. #endif