copy.glsl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* clang-format off */
  2. [vertex]
  3. layout(location = 0) in highp vec4 vertex_attrib;
  4. /* clang-format on */
  5. #if defined(USE_CUBEMAP) || defined(USE_PANORAMA)
  6. layout(location = 4) in vec3 cube_in;
  7. #else
  8. layout(location = 4) in vec2 uv_in;
  9. #endif
  10. layout(location = 5) in vec2 uv2_in;
  11. #if defined(USE_CUBEMAP) || defined(USE_PANORAMA)
  12. out vec3 cube_interp;
  13. #else
  14. out vec2 uv_interp;
  15. #endif
  16. out vec2 uv2_interp;
  17. #ifdef USE_COPY_SECTION
  18. uniform vec4 copy_section;
  19. #endif
  20. void main() {
  21. #if defined(USE_CUBEMAP) || defined(USE_PANORAMA)
  22. cube_interp = cube_in;
  23. #elif defined(USE_ASYM_PANO)
  24. uv_interp = vertex_attrib.xy;
  25. #else
  26. uv_interp = uv_in;
  27. #ifdef V_FLIP
  28. uv_interp.y = 1.0 - uv_interp.y;
  29. #endif
  30. #endif
  31. uv2_interp = uv2_in;
  32. gl_Position = vertex_attrib;
  33. #ifdef USE_COPY_SECTION
  34. uv_interp = copy_section.xy + uv_interp * copy_section.zw;
  35. gl_Position.xy = (copy_section.xy + (gl_Position.xy * 0.5 + 0.5) * copy_section.zw) * 2.0 - 1.0;
  36. #endif
  37. }
  38. /* clang-format off */
  39. [fragment]
  40. #define M_PI 3.14159265359
  41. #if !defined(USE_GLES_OVER_GL)
  42. precision mediump float;
  43. #endif
  44. #if defined(USE_CUBEMAP) || defined(USE_PANORAMA)
  45. in vec3 cube_interp;
  46. #else
  47. in vec2 uv_interp;
  48. #endif
  49. #ifdef USE_ASYM_PANO
  50. uniform highp mat4 pano_transform;
  51. uniform highp vec4 asym_proj;
  52. #endif
  53. // These definitions are here because the shader-wrapper builder does
  54. // not understand `#elif defined()`
  55. #ifdef USE_TEXTURE3D
  56. #endif
  57. #ifdef USE_TEXTURE2DARRAY
  58. #endif
  59. #ifdef USE_CUBEMAP
  60. uniform samplerCube source_cube; //texunit:0
  61. #elif defined(USE_TEXTURE3D)
  62. uniform sampler3D source_3d; //texunit:0
  63. #elif defined(USE_TEXTURE2DARRAY)
  64. uniform sampler2DArray source_2d_array; //texunit:0
  65. #else
  66. uniform sampler2D source; //texunit:0
  67. #endif
  68. /* clang-format on */
  69. #if defined(USE_TEXTURE3D) || defined(USE_TEXTURE2DARRAY)
  70. uniform float layer;
  71. #endif
  72. #ifdef USE_MULTIPLIER
  73. uniform float multiplier;
  74. #endif
  75. #if defined(USE_PANORAMA) || defined(USE_ASYM_PANO)
  76. uniform highp mat4 sky_transform;
  77. vec4 texturePanorama(vec3 normal, sampler2D pano) {
  78. vec2 st = vec2(
  79. atan(normal.x, normal.z),
  80. acos(normal.y));
  81. if (st.x < 0.0)
  82. st.x += M_PI * 2.0;
  83. st /= vec2(M_PI * 2.0, M_PI);
  84. return textureLod(pano, st, 0.0);
  85. }
  86. #endif
  87. uniform vec2 pixel_size;
  88. in vec2 uv2_interp;
  89. #ifdef USE_BCS
  90. uniform vec3 bcs;
  91. #endif
  92. #ifdef USE_COLOR_CORRECTION
  93. uniform sampler2D color_correction; //texunit:1
  94. #endif
  95. layout(location = 0) out vec4 frag_color;
  96. void main() {
  97. //vec4 color = color_interp;
  98. #ifdef USE_PANORAMA
  99. vec3 cube_normal = normalize(cube_interp);
  100. cube_normal.z = -cube_normal.z;
  101. cube_normal = mat3(sky_transform) * cube_normal;
  102. cube_normal.z = -cube_normal.z;
  103. vec4 color = texturePanorama(cube_normal, source);
  104. #elif defined(USE_ASYM_PANO)
  105. // When an asymmetrical projection matrix is used (applicable for stereoscopic rendering i.e. VR) we need to do this calculation per fragment to get a perspective correct result.
  106. // Note that we're ignoring the x-offset for IPD, with Z sufficiently in the distance it becomes neglectible, as a result we could probably just set cube_normal.z to -1.
  107. // The Matrix[2][0] (= asym_proj.x) and Matrix[2][1] (= asym_proj.z) values are what provide the right shift in the image.
  108. vec3 cube_normal;
  109. cube_normal.z = -1000000.0;
  110. cube_normal.x = (cube_normal.z * (-uv_interp.x - asym_proj.x)) / asym_proj.y;
  111. cube_normal.y = (cube_normal.z * (-uv_interp.y - asym_proj.z)) / asym_proj.a;
  112. cube_normal = mat3(sky_transform) * mat3(pano_transform) * cube_normal;
  113. cube_normal.z = -cube_normal.z;
  114. vec4 color = texturePanorama(normalize(cube_normal.xyz), source);
  115. #elif defined(USE_CUBEMAP)
  116. vec4 color = texture(source_cube, normalize(cube_interp));
  117. #elif defined(USE_TEXTURE3D)
  118. vec4 color = textureLod(source_3d, vec3(uv_interp, layer), 0.0);
  119. #elif defined(USE_TEXTURE2DARRAY)
  120. vec4 color = textureLod(source_2d_array, vec3(uv_interp, layer), 0.0);
  121. #else
  122. vec4 color = textureLod(source, uv_interp, 0.0);
  123. #endif
  124. #ifdef LINEAR_TO_SRGB
  125. //regular Linear -> SRGB conversion
  126. vec3 a = vec3(0.055);
  127. color.rgb = mix((vec3(1.0) + a) * pow(color.rgb, vec3(1.0 / 2.4)) - a, 12.92 * color.rgb, lessThan(color.rgb, vec3(0.0031308)));
  128. #endif
  129. #ifdef SRGB_TO_LINEAR
  130. color.rgb = mix(pow((color.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), color.rgb * (1.0 / 12.92), lessThan(color.rgb, vec3(0.04045)));
  131. #endif
  132. #ifdef DEBUG_GRADIENT
  133. color.rg = uv_interp;
  134. color.b = 0.0;
  135. #endif
  136. #ifdef DISABLE_ALPHA
  137. color.a = 1.0;
  138. #endif
  139. #ifdef GAUSSIAN_HORIZONTAL
  140. color *= 0.38774;
  141. color += texture(source, uv_interp + vec2(1.0, 0.0) * pixel_size) * 0.24477;
  142. color += texture(source, uv_interp + vec2(2.0, 0.0) * pixel_size) * 0.06136;
  143. color += texture(source, uv_interp + vec2(-1.0, 0.0) * pixel_size) * 0.24477;
  144. color += texture(source, uv_interp + vec2(-2.0, 0.0) * pixel_size) * 0.06136;
  145. #endif
  146. #ifdef GAUSSIAN_VERTICAL
  147. color *= 0.38774;
  148. color += texture(source, uv_interp + vec2(0.0, 1.0) * pixel_size) * 0.24477;
  149. color += texture(source, uv_interp + vec2(0.0, 2.0) * pixel_size) * 0.06136;
  150. color += texture(source, uv_interp + vec2(0.0, -1.0) * pixel_size) * 0.24477;
  151. color += texture(source, uv_interp + vec2(0.0, -2.0) * pixel_size) * 0.06136;
  152. #endif
  153. #ifdef USE_BCS
  154. color.rgb = mix(vec3(0.0), color.rgb, bcs.x);
  155. color.rgb = mix(vec3(0.5), color.rgb, bcs.y);
  156. color.rgb = mix(vec3(dot(vec3(1.0), color.rgb) * 0.33333), color.rgb, bcs.z);
  157. #endif
  158. #ifdef USE_COLOR_CORRECTION
  159. color.r = texture(color_correction, vec2(color.r, 0.0)).r;
  160. color.g = texture(color_correction, vec2(color.g, 0.0)).g;
  161. color.b = texture(color_correction, vec2(color.b, 0.0)).b;
  162. #endif
  163. #ifdef USE_MULTIPLIER
  164. color.rgb *= multiplier;
  165. #endif
  166. frag_color = color;
  167. }