AVProVideo.cginc 11 KB

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