Advertisement
illwieckz

material-clusters oops

Jul 29th, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 36.38 KB | None | 0 0
  1. Debug: building lightMapping shader permutation with macro: none
  2. Warn: Source for shader program lightMappingMaterial:
  3. 0: #version 460 core
  4. 1: #define HAVE_EXT_gpu_shader4 1
  5. 2: #define HAVE_ARB_gpu_shader5 1
  6. 3: #define HAVE_ARB_texture_gather 1
  7. 4: #define HAVE_EXT_texture_integer 1
  8. 5: #define HAVE_ARB_texture_rg 1
  9. 6: #define HAVE_ARB_uniform_buffer_object 1
  10. 7: #extension GL_ARB_bindless_texture : require
  11. 8: #define HAVE_ARB_bindless_texture 1
  12. 9: #extension GL_ARB_shader_draw_parameters : require
  13. 10: #define HAVE_ARB_shader_draw_parameters 1
  14. 11: #define HAVE_ARB_shader_storage_buffer_object 1
  15. 12: #define HAVE_ARB_explicit_attrib_location 1
  16. 13: #define IN(mode) mode in
  17. 14: #define DECLARE_OUTPUT(type) out type outputColor;
  18. 15: #define textureCube texture
  19. 16: #define texture2D texture
  20. 17: #define texture2DProj textureProj
  21. 18: #define texture3D texture
  22. 19: layout(bindless_sampler) uniform;
  23. 20: IN(flat) int in_drawID;
  24. 21: IN(flat) int in_baseInstance;
  25. 22: #define drawID in_drawID
  26. 23: #define baseInstance in_baseInstance
  27. 24: IN(flat) uint in_MatID;
  28. 25: #define matID in_MatID
  29. 26:
  30. 27: #ifndef r_highPrecisionRendering
  31. 28: #define r_highPrecisionRendering 1
  32. 29: #endif
  33. 30: #ifndef r_texturePacks
  34. 31: #define r_texturePacks 1
  35. 32: #endif
  36. 33: #ifndef r_precomputedLighting
  37. 34: #define r_precomputedLighting 1
  38. 35: #endif
  39. 36: #ifndef r_vertexSkinning
  40. 37: #define r_vertexSkinning 1
  41. 38: #endif
  42. 39: const int MAX_GLSL_BONES = 256;
  43. 40: #ifndef r_halfLambertLighting
  44. 41: #define r_halfLambertLighting 1
  45. 42: #endif
  46. 43: #ifndef r_rimLighting
  47. 44: #define r_rimLighting 1
  48. 45: #endif
  49. 46: const float r_RimExponent = 3.00000000e+00;
  50. 47: #ifndef r_normalMapping
  51. 48: #define r_normalMapping 1
  52. 49: #endif
  53. 50: #ifndef r_specularMapping
  54. 51: #define r_specularMapping 1
  55. 52: #endif
  56. 53: #ifndef r_physicalMapping
  57. 54: #define r_physicalMapping 1
  58. 55: #endif
  59. 56: #ifndef r_glowMapping
  60. 57: #define r_glowMapping 1
  61. 58: #endif
  62. 59: #define USE_MATERIAL_SYSTEM
  63. 60:
  64. 61: struct Material {
  65. 62: vec3 u_DiffuseMapModifier;
  66. 63: int u_DiffuseMapModifier_padding;
  67. 64: vec3 u_MaterialMapModifier;
  68. 65: int u_MaterialMapModifier_padding;
  69. 66: vec3 u_LightMapModifier;
  70. 67: int u_LightMapModifier_padding;
  71. 68: vec3 u_DeluxeMapModifier;
  72. 69: int u_DeluxeMapModifier_padding;
  73. 70: vec3 u_GlowMapModifier;
  74. 71: int u_GlowMapModifier_padding;
  75. 72: vec3 u_NormalMapModifier;
  76. 73: int u_NormalMapModifier_padding;
  77. 74: vec3 u_HeightMapModifier;
  78. 75: int u_HeightMapModifier_padding;
  79. 76: mat4 u_TextureMatrix;
  80. 77: vec4 u_ColorModulate;
  81. 78: vec4 u_Color;
  82. 79: vec3 u_NormalScale;
  83. 80: int u_NormalScale_padding;
  84. 81: uvec2 u_DiffuseMap;
  85. 82: uvec2 u_NormalMap;
  86. 83: uvec2 u_HeightMap;
  87. 84: uvec2 u_MaterialMap;
  88. 85: uvec2 u_LightMap;
  89. 86: uvec2 u_DeluxeMap;
  90. 87: uvec2 u_GlowMap;
  91. 88: uvec2 u_EnvironmentMap0;
  92. 89: uvec2 u_EnvironmentMap1;
  93. 90: uvec2 u_LightGrid1;
  94. 91: uvec2 u_LightGrid2;
  95. 92: uvec2 u_LightTilesInt;
  96. 93: vec2 u_SpecularExponent;
  97. 94: float u_AlphaThreshold;
  98. 95: float u_InverseLightFactor;
  99. 96: float u_VertexInterpolation;
  100. 97: float u_ReliefDepthScale;
  101. 98: float u_ReliefOffsetBias;
  102. 99: float u_EnvironmentInterpolation;
  103. 100: bool u_ShowTris;
  104. 101: float u_Time;
  105. 102: int material_padding0;
  106. 103: int material_padding1;
  107. 104: };
  108. 105:
  109. 106: layout(std430, binding = 0) readonly buffer materialsSSBO {
  110. 107: Material materials[];
  111. 108: };
  112. 109:
  113. 110: #define u_DiffuseMapModifier materials[823].u_DiffuseMapModifier
  114. 111: #define u_MaterialMapModifier materials[823].u_MaterialMapModifier
  115. 112: #define u_LightMapModifier materials[823].u_LightMapModifier
  116. 113: #define u_DeluxeMapModifier materials[823].u_DeluxeMapModifier
  117. 114: #define u_GlowMapModifier materials[823].u_GlowMapModifier
  118. 115: #define u_NormalMapModifier materials[823].u_NormalMapModifier
  119. 116: #define u_HeightMapModifier materials[823].u_HeightMapModifier
  120. 117: #define u_TextureMatrix materials[823].u_TextureMatrix
  121. 118: #define u_ColorModulate materials[823].u_ColorModulate
  122. 119: #define u_Color materials[823].u_Color
  123. 120: #define u_NormalScale materials[823].u_NormalScale
  124. 121: #define u_DiffuseMap_initial uvec2( materials[823].u_DiffuseMap )
  125. 122: #define u_NormalMap_initial uvec2( materials[823].u_NormalMap )
  126. 123: #define u_HeightMap_initial uvec2( materials[823].u_HeightMap )
  127. 124: #define u_MaterialMap_initial uvec2( materials[823].u_MaterialMap )
  128. 125: #define u_LightMap_initial uvec2( materials[823].u_LightMap )
  129. 126: #define u_DeluxeMap_initial uvec2( materials[823].u_DeluxeMap )
  130. 127: #define u_GlowMap_initial uvec2( materials[823].u_GlowMap )
  131. 128: #define u_EnvironmentMap0_initial uvec2( materials[823].u_EnvironmentMap0 )
  132. 129: #define u_EnvironmentMap1_initial uvec2( materials[823].u_EnvironmentMap1 )
  133. 130: #define u_LightGrid1_initial uvec2( materials[823].u_LightGrid1 )
  134. 131: #define u_LightGrid2_initial uvec2( materials[823].u_LightGrid2 )
  135. 132: #define u_LightTilesInt_initial uvec2( materials[823].u_LightTilesInt )
  136. 133: #define u_SpecularExponent materials[823].u_SpecularExponent
  137. 134: #define u_AlphaThreshold materials[823].u_AlphaThreshold
  138. 135: #define u_InverseLightFactor materials[823].u_InverseLightFactor
  139. 136: #define u_VertexInterpolation materials[823].u_VertexInterpolation
  140. 137: #define u_ReliefDepthScale materials[823].u_ReliefDepthScale
  141. 138: #define u_ReliefOffsetBias materials[823].u_ReliefOffsetBias
  142. 139: #define u_EnvironmentInterpolation materials[823].u_EnvironmentInterpolation
  143. 140: #define u_ShowTris materials[823].u_ShowTris
  144. 141: #define u_Time materials[823].u_Time
  145. 142:
  146. 143: uniform vec3 u_ViewOrigin;
  147. 144: uniform mat4 u_ModelMatrix;
  148. 145: uniform mat4 u_ModelViewProjectionMatrix;
  149. 146: uniform vec3 u_LightGridOrigin;
  150. 147: uniform vec3 u_LightGridScale;
  151. 148: uniform int u_numLights;
  152. 149:
  153. 150: #ifndef r_AmbientScale
  154. 151: #define r_AmbientScale 1.00000000e+00
  155. 152: #endif
  156. 153: #ifndef r_SpecularScale
  157. 154: #define r_SpecularScale 1.00000000e+00
  158. 155: #endif
  159. 156: #ifndef r_zNear
  160. 157: #define r_zNear 3.00000000e+00
  161. 158: #endif
  162. 159: #ifndef M_PI
  163. 160: #define M_PI 3.14159274e+00
  164. 161: #endif
  165. 162: #ifndef MAX_SHADOWMAPS
  166. 163: #define MAX_SHADOWMAPS 5
  167. 164: #endif
  168. 165: #ifndef MAX_REF_LIGHTS
  169. 166: #define MAX_REF_LIGHTS 1024
  170. 167: #endif
  171. 168: #ifndef TILE_SIZE
  172. 169: #define TILE_SIZE 16
  173. 170: #endif
  174. 171: #ifndef r_FBufSize
  175. 172: #define r_FBufSize vec2(2.56000000e+03, 1.44000000e+03)
  176. 173: #endif
  177. 174: #ifndef r_tileStep
  178. 175: #define r_tileStep vec2(6.24999963e-03, 1.11111114e-02)
  179. 176: #endif
  180. 0: #line 0
  181. 1: /*
  182. 2: ===========================================================================
  183. 3: Copyright (C) 2009-2011 Robert Beckebans <trebor_7@users.sourceforge.net>
  184. 4:
  185. 5: This file is part of XreaL source code.
  186. 6:
  187. 7: XreaL source code is free software; you can redistribute it
  188. 8: and/or modify it under the terms of the GNU General Public License as
  189. 9: published by the Free Software Foundation; either version 2 of the License,
  190. 10: or (at your option) any later version.
  191. 11:
  192. 12: XreaL source code is distributed in the hope that it will be
  193. 13: useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  194. 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  195. 15: GNU General Public License for more details.
  196. 16:
  197. 17: You should have received a copy of the GNU General Public License
  198. 18: along with XreaL source code; if not, write to the Free Software
  199. 19: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  200. 20: ===========================================================================
  201. 21: */
  202. 22: // computeLight_fp.glsl - Light computing helper functions
  203. 23:
  204. 24: #define COMPUTELIGHT_GLSL
  205. 25:
  206. 26: #if !defined(USE_BSP_SURFACE)
  207. 27: #define USE_MODEL_SURFACE
  208. 28: #endif
  209. 29:
  210. 30: #if !defined(USE_GRID_LIGHTING)
  211. 31: #define USE_LIGHT_MAPPING
  212. 32: #endif
  213. 33:
  214. 34: #if defined(USE_REFLECTIVE_SPECULAR)
  215. 35: #endif // USE_REFLECTIVE_SPECULAR
  216. 36:
  217. 37: #if defined(HAVE_ARB_uniform_buffer_object)
  218. 38: struct light {
  219. 39: vec4 center_radius;
  220. 40: vec4 color_type;
  221. 41: vec4 direction_angle;
  222. 42: };
  223. 43:
  224. 44: layout(std140) uniform u_Lights {
  225. 45: light lights[ MAX_REF_LIGHTS ];
  226. 46: };
  227. 47: #define GetLight(idx, component) lights[idx].component
  228. 48: #else // !HAVE_ARB_uniform_buffer_object
  229. 49: #define idxToTC( idx, w, h ) vec2( floor( ( idx * ( 1.0 / w ) ) + 0.5 ) * ( 1.0 / h ), \
  230. 50: fract( ( idx + 0.5 ) * (1.0 / w ) ) )
  231. 51: const struct GetLightOffsets {
  232. 52: int center_radius;
  233. 53: int color_type;
  234. 54: int direction_angle;
  235. 55: } getLightOffsets = GetLightOffsets(0, 1, 2);
  236. 56: #define GetLight(idx, component) texture2D( u_LightsTexture, idxToTC(3 * idx + getLightOffsets.component, 64.0, float( 3 * MAX_REF_LIGHTS / 64 ) ) )
  237. 57: #endif // HAVE_ARB_uniform_buffer_object
  238. 58:
  239. 59:
  240. 60:
  241. 61: // lighting helper functions
  242. 62:
  243. 63: void ReadLightGrid(in vec4 texel, out vec3 ambientColor, out vec3 lightColor) {
  244. 64: float ambientScale = 2.0 * texel.a;
  245. 65: float directedScale = 2.0 - ambientScale;
  246. 66: ambientColor = ambientScale * texel.rgb;
  247. 67: lightColor = directedScale * texel.rgb;
  248. 68: }
  249. 69:
  250. 70: void computeLight(in vec3 lightColor, vec4 diffuseColor, inout vec4 color) {
  251. 71: color.rgb += lightColor.rgb * diffuseColor.rgb;
  252. 72: }
  253. 73:
  254. 74: #if defined(USE_REFLECTIVE_SPECULAR)
  255. 75: void computeDeluxeLight( vec3 lightDir, vec3 normal, vec3 viewDir, vec3 lightColor,
  256. 76: vec4 diffuseColor, vec4 materialColor,
  257. 77: inout vec4 color, in samplerCube u_EnvironmentMap0, in samplerCube u_EnvironmentMap1 )
  258. 78: #else // !USE_REFLECTIVE_SPECULAR
  259. 79: void computeDeluxeLight( vec3 lightDir, vec3 normal, vec3 viewDir, vec3 lightColor,
  260. 80: vec4 diffuseColor, vec4 materialColor,
  261. 81: inout vec4 color )
  262. 82: #endif // !USE_REFLECTIVE_SPECULAR
  263. 83: {
  264. 84: vec3 H = normalize( lightDir + viewDir );
  265. 85:
  266. 86: #if defined(USE_PHYSICAL_MAPPING) || defined(r_specularMapping)
  267. 87: float NdotH = clamp( dot( normal, H ), 0.0, 1.0 );
  268. 88: #endif // USE_PHYSICAL_MAPPING || r_specularMapping
  269. 89:
  270. 90: // clamp( NdotL, 0.0, 1.0 ) is done below
  271. 91: float NdotL = dot( normal, lightDir );
  272. 92:
  273. 93: #if !defined(USE_BSP_SURFACE) && defined(r_halfLambertLighting)
  274. 94: // http://developer.valvesoftware.com/wiki/Half_Lambert
  275. 95: NdotL = NdotL * 0.5 + 0.5;
  276. 96: NdotL *= NdotL;
  277. 97: #endif
  278. 98:
  279. 99: NdotL = clamp( NdotL, 0.0, 1.0 );
  280. 100:
  281. 101: #if defined(USE_PHYSICAL_MAPPING)
  282. 102: // Daemon PBR packing defaults to ORM like glTF 2.0 defines
  283. 103: // https://www.khronos.org/blog/art-pipeline-for-gltf
  284. 104: // > ORM texture for Occlusion, Roughness, and Metallic
  285. 105: // https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/schema/material.pbrMetallicRoughness.schema.json
  286. 106: // > The metalness values are sampled from the B channel. The roughness values are sampled from the G channel.
  287. 107: // > These values are linear. If other channels are present (R or A), they are ignored for metallic-roughness calculations.
  288. 108: // https://docs.blender.org/manual/en/2.80/addons/io_scene_gltf2.html
  289. 109: // > glTF stores occlusion in the red (R) channel, allowing it to optionally share the same image
  290. 110: // > with the roughness and metallic channels.
  291. 111: float roughness = materialColor.g;
  292. 112: float metalness = materialColor.b;
  293. 113:
  294. 114: float NdotV = clamp( dot( normal, viewDir ), 0.0, 1.0);
  295. 115: float VdotH = clamp( dot( viewDir, H ), 0.0, 1.0);
  296. 116:
  297. 117: float alpha = roughness * roughness;
  298. 118: float k = 0.125 * (roughness + 1.0) * (roughness + 1.0);
  299. 119:
  300. 120: float D = alpha / ((NdotH * NdotH) * (alpha * alpha - 1.0) + 1.0);
  301. 121: D *= D;
  302. 122:
  303. 123: float FexpNH = pow(1.0 - NdotH, 5.0);
  304. 124: float FexpNV = pow(1.0 - NdotV, 5.0);
  305. 125: vec3 F = mix(vec3(0.04), diffuseColor.rgb, metalness);
  306. 126: F = F + (1.0 - F) * FexpNH;
  307. 127:
  308. 128: float G = NdotL / (NdotL * (1.0 - k) + k);
  309. 129: G *= NdotV / (NdotV * (1.0 - k) + k);
  310. 130:
  311. 131: vec3 diffuseBRDF = NdotL * diffuseColor.rgb * (1.0 - metalness);
  312. 132: vec3 specularBRDF = vec3((D * F * G) / max(4.0 * NdotL * NdotV, 0.0001f));
  313. 133: color.rgb += (diffuseBRDF + specularBRDF) * lightColor.rgb * NdotL;
  314. 134: color.a = mix(diffuseColor.a, 1.0, FexpNV);
  315. 135: #else // !USE_PHYSICAL_MAPPING
  316. 136:
  317. 137: #if defined(USE_REFLECTIVE_SPECULAR)
  318. 138: // not implemented for PBR yet
  319. 139: vec4 envColor0 = textureCube(u_EnvironmentMap0, reflect(-viewDir, normal));
  320. 140: vec4 envColor1 = textureCube(u_EnvironmentMap1, reflect(-viewDir, normal));
  321. 141:
  322. 142: materialColor.rgb *= mix(envColor0, envColor1, u_EnvironmentInterpolation).rgb;
  323. 143: #endif // USE_REFLECTIVE_SPECULAR
  324. 144:
  325. 145: color.rgb += lightColor.rgb * NdotL * diffuseColor.rgb;
  326. 146: #if defined(r_specularMapping)
  327. 147: // The minimal specular exponent should preferably be nonzero to avoid the undefined pow(0, 0)
  328. 148: color.rgb += lightColor.rgb * materialColor.rgb * pow( NdotH, u_SpecularExponent.x * materialColor.a + u_SpecularExponent.y) * r_SpecularScale;
  329. 149: #endif // r_specularMapping
  330. 150: #endif // !USE_PHYSICAL_MAPPING
  331. 151: }
  332. 152:
  333. 153: #if defined(HAVE_EXT_texture_integer) && defined(r_highPrecisionRendering)
  334. 154: const int lightsPerLayer = 16;
  335. 155: #define idxs_t uvec4
  336. 156: idxs_t fetchIdxs( in vec3 coords, in usampler3D u_LightTilesInt ) {
  337. 157: return texture3D( u_LightTilesInt, coords );
  338. 158: }
  339. 159: int nextIdx( inout idxs_t idxs ) {
  340. 160: uvec4 tmp = ( idxs & uvec4( 3 ) ) * uvec4( 0x40, 0x10, 0x04, 0x01 );
  341. 161: idxs = idxs >> 2;
  342. 162: return int( tmp.x + tmp.y + tmp.z + tmp.w );
  343. 163: }
  344. 164: #else // !HAVE_EXT_texture_integer
  345. 165: const int lightsPerLayer = 4;
  346. 166: #define idxs_t vec4
  347. 167: idxs_t fetchIdxs( in vec3 coords ) {
  348. 168: return texture3D( u_LightTiles, coords ) * 255.0;
  349. 169: }
  350. 170: int nextIdx( inout idxs_t idxs ) {
  351. 171: vec4 tmp = idxs;
  352. 172: idxs = floor(idxs * 0.25);
  353. 173: tmp -= 4.0 * idxs;
  354. 174: return int( dot( tmp, vec4( 64.0, 16.0, 4.0, 1.0 ) ) );
  355. 175: }
  356. 176: #endif // HAVE_EXT_texture_integer
  357. 177:
  358. 178: const int numLayers = MAX_REF_LIGHTS / 256;
  359. 179:
  360. 180: #if defined(r_dynamicLight) && r_dynamicLightRenderer == 1
  361. 181: #if defined(USE_REFLECTIVE_SPECULAR)
  362. 182: void computeDynamicLight( int idx, vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse,
  363. 183: vec4 material, inout vec4 color, in samplerCube u_EnvironmentMap0, in samplerCube u_EnvironmentMap1 )
  364. 184: #else // !USE_REFLECTIVE_SPECULAR
  365. 185: void computeDynamicLight( int idx, vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse,
  366. 186: vec4 material, inout vec4 color )
  367. 187: #endif // !USE_REFLECTIVE_SPECULAR
  368. 188: {
  369. 189: vec4 center_radius = GetLight( idx, center_radius );
  370. 190: vec4 color_type = GetLight( idx, color_type );
  371. 191: vec3 L;
  372. 192: float attenuation;
  373. 193:
  374. 194: if( color_type.w == 0.0 ) {
  375. 195: // point light
  376. 196: L = center_radius.xyz - P;
  377. 197: // 2.57 ~= 8.0 ^ ( 1.0 / 2.2 ), adjusted after overbright changes
  378. 198: float t = 1.0 + 2.57 * length(L) / center_radius.w;
  379. 199: // Quadratic attenuation function instead of linear because of overbright changes
  380. 200: attenuation = 1.0 / ( t * t );
  381. 201: L = normalize(L);
  382. 202: } else if( color_type.w == 1.0 ) {
  383. 203: // spot light
  384. 204: vec4 direction_angle = GetLight( idx, direction_angle );
  385. 205: L = center_radius.xyz - P;
  386. 206: // 2.57 ~= 8.0 ^ ( 1.0 / 2.2 ), adjusted after overbright changes
  387. 207: float t = 1.0 + 2.57 * length(L) / center_radius.w;
  388. 208: // Quadratic attenuation function instead of linear because of overbright changes
  389. 209: attenuation = 1.0 / ( t * t );
  390. 210: L = normalize( L );
  391. 211:
  392. 212: if( dot( L, direction_angle.xyz ) <= direction_angle.w ) {
  393. 213: attenuation = 0.0;
  394. 214: }
  395. 215: } else if( color_type.w == 2.0 ) {
  396. 216: // sun (directional) light
  397. 217: L = GetLight( idx, direction_angle ).xyz;
  398. 218: attenuation = 1.0;
  399. 219: }
  400. 220: #if defined(USE_REFLECTIVE_SPECULAR)
  401. 221: computeDeluxeLight( L, normal, viewDir,
  402. 222: attenuation * attenuation * color_type.xyz,
  403. 223: diffuse, material, color, u_EnvironmentMap0, u_EnvironmentMap1 );
  404. 224: #else // !USE_REFLECTIVE_SPECULAR
  405. 225: computeDeluxeLight( L, normal, viewDir,
  406. 226: attenuation * attenuation * color_type.xyz,
  407. 227: diffuse, material, color );
  408. 228: #endif // !USE_REFLECTIVE_SPECULAR
  409. 229: }
  410. 230:
  411. 231: #if defined(USE_REFLECTIVE_SPECULAR)
  412. 232: void computeDynamicLights( vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse, vec4 material,
  413. 233: inout vec4 color, in usampler3D u_LightTilesInt, in samplerCube u_EnvironmentMap0, in samplerCube u_EnvironmentMap1 )
  414. 234: #else // !USE_REFLECTIVE_SPECULAR
  415. 235: void computeDynamicLights( vec3 P, vec3 normal, vec3 viewDir, vec4 diffuse, vec4 material,
  416. 236: inout vec4 color, in usampler3D u_LightTilesInt )
  417. 237: #endif // !USE_REFLECTIVE_SPECULAR
  418. 238: {
  419. 239: vec2 tile = floor( gl_FragCoord.xy * (1.0 / float( TILE_SIZE ) ) ) + 0.5;
  420. 240: vec3 tileScale = vec3( r_tileStep, 1.0/numLayers );
  421. 241:
  422. 242: #if defined(r_showLightTiles)
  423. 243: float numLights = 0.0;
  424. 244: #endif
  425. 245:
  426. 246: for( int layer = 0; layer < numLayers; layer++ ) {
  427. 247: idxs_t idxs = fetchIdxs( tileScale * vec3( tile, float( layer ) + 0.5 ), u_LightTilesInt );
  428. 248: for( int i = 0; i < lightsPerLayer; i++ ) {
  429. 249: int idx = numLayers * nextIdx( idxs ) + layer;
  430. 250:
  431. 251: if( idx >= u_numLights )
  432. 252: {
  433. 253: break;
  434. 254: }
  435. 255:
  436. 256: #if defined(USE_REFLECTIVE_SPECULAR)
  437. 257: computeDynamicLight( idx, P, normal, viewDir, diffuse, material, color, u_EnvironmentMap0, u_EnvironmentMap1 );
  438. 258: #else // !USE_REFLECTIVE_SPECULAR
  439. 259: computeDynamicLight( idx, P, normal, viewDir, diffuse, material, color );
  440. 260: #endif // !USE_REFLECTIVE_SPECULAR
  441. 261:
  442. 262: #if defined(r_showLightTiles)
  443. 263: numLights++;
  444. 264: #endif
  445. 265: }
  446. 266: }
  447. 267:
  448. 268: #if defined(r_showLightTiles)
  449. 269: if (numLights > 0.0)
  450. 270: {
  451. 271: color = vec4(numLights/(lightsPerLayer*numLayers), numLights/(lightsPerLayer*numLayers), numLights/(lightsPerLayer*numLayers), 1.0);
  452. 272: }
  453. 273: #endif
  454. 274: }
  455. 275: #endif
  456. 0: #line 0
  457. 1: /*
  458. 2: ===========================================================================
  459. 3: Copyright (C) 2009-2011 Robert Beckebans <trebor_7@users.sourceforge.net>
  460. 4:
  461. 5: This file is part of XreaL source code.
  462. 6:
  463. 7: XreaL source code is free software; you can redistribute it
  464. 8: and/or modify it under the terms of the GNU General Public License as
  465. 9: published by the Free Software Foundation; either version 2 of the License,
  466. 10: or (at your option) any later version.
  467. 11:
  468. 12: XreaL source code is distributed in the hope that it will be
  469. 13: useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  470. 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  471. 15: GNU General Public License for more details.
  472. 16:
  473. 17: You should have received a copy of the GNU General Public License
  474. 18: along with XreaL source code; if not, write to the Free Software
  475. 19: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  476. 20: ===========================================================================
  477. 21: */
  478. 22: // reliefMapping_fp.glsl - Relief mapping helper functions
  479. 23:
  480. 24: #define RELIEFMAPPING_GLSL
  481. 25:
  482. 26: #if defined(r_normalMapping) || defined(USE_HEIGHTMAP_IN_NORMALMAP)
  483. 27: #if defined(r_texturePacks)
  484. 28: #else
  485. 29: #endif
  486. 30: #endif // r_normalMapping || USE_HEIGHTMAP_IN_NORMALMAP
  487. 31:
  488. 32: #if defined(r_normalMapping)
  489. 33: #endif // r_normalMapping
  490. 34:
  491. 35: #if defined(USE_RELIEF_MAPPING)
  492. 36: #if !defined(USE_HEIGHTMAP_IN_NORMALMAP)
  493. 37: #if defined(r_texturePacks)
  494. 38: #else
  495. 39: #endif
  496. 40: #endif // !USE_HEIGHTMAP_IN_NORMALMAP
  497. 41: #endif // USE_RELIEF_MAPPING
  498. 42:
  499. 43: // compute normal in tangent space
  500. 44: #if defined(r_normalMapping)
  501. 45: vec3 NormalInTangentSpace(vec2 texNormal, in sampler2D u_NormalMap)
  502. 46: #else // !r_normalMapping
  503. 47: vec3 NormalInTangentSpace(vec2 texNormal)
  504. 48: #endif // !r_normalMapping
  505. 49: {
  506. 50: vec3 normal;
  507. 51:
  508. 52: #if defined(r_normalMapping)
  509. 53: #if defined(USE_HEIGHTMAP_IN_NORMALMAP)
  510. 54: // alpha channel contains the height map so do not try to reconstruct normal map from it
  511. 55: #if defined(r_texturePacks)
  512. 56: normal = texture2D(u_NormalMap, vec3( texNormal * u_NormalMapModifier.xy, u_NormalMapModifier.z )).rgb;
  513. 57: #else
  514. 58: normal = texture2D(u_NormalMap, texNormal).rgb;
  515. 59: #endif
  516. 60: normal = 2.0 * normal - 1.0;
  517. 61: #else // !USE_HEIGHTMAP_IN_NORMALMAP
  518. 62: // the Capcom trick abusing alpha channel of DXT1/5 formats to encode normal map
  519. 63: // https://github.com/DaemonEngine/Daemon/issues/183#issuecomment-473691252
  520. 64: //
  521. 65: // the algorithm also works with normal maps in rgb format without alpha channel
  522. 66: // but we still must be sure there is no height map in alpha channel hence the test
  523. 67: //
  524. 68: // crunch -dxn seems to produce such files, since alpha channel is abused such format
  525. 69: // is unsuitable to embed height map, then height map must be distributed as loose file
  526. 70: normal = texture2D(u_NormalMap, texNormal).rga;
  527. 71: #if defined(r_texturePacks)
  528. 72: normal = texture2D(u_NormalMap, vec3( texNormal * u_NormalMapModifier.xy, u_NormalMapModifier.z )).rga;
  529. 73: #else
  530. 74: normal = texture2D(u_NormalMap, texNormal).rga;
  531. 75: #endif
  532. 76: normal.x *= normal.z;
  533. 77: normal.xy = 2.0 * normal.xy - 1.0;
  534. 78: // In a perfect world this code must be enough:
  535. 79: // normal.z = sqrt(1.0 - dot(normal.xy, normal.xy));
  536. 80: //
  537. 81: // Unvanquished texture known to trigger black normalmap artifacts
  538. 82: // when doing Z reconstruction:
  539. 83: // textures/shared_pk02_src/rock01_n
  540. 84: //
  541. 85: // Although the normal vector is supposed to have a length of 1,
  542. 86: // dot(normal.xy, normal.xy) may be greater than 1 due to compression
  543. 87: // artifacts: values as large as 1.27 have been observed with crunch -dxn.
  544. 88: // https://github.com/DaemonEngine/Daemon/pull/260#issuecomment-571010935
  545. 89: //
  546. 90: // This might happen with other formats too. So we must take care not to
  547. 91: // take the square root of a negative number here.
  548. 92: normal.z = sqrt(max(0, 1.0 - dot(normal.xy, normal.xy)));
  549. 93: #endif // !USE_HEIGHTMAP_IN_NORMALMAP
  550. 94: /* Disable normal map scaling when normal Z scale is set to zero.
  551. 95:
  552. 96: This happens when r_normalScale is set to zero because
  553. 97: u_NormalScale.z is premultiplied with r_normalScale. User can
  554. 98: disable normal map scaling by setting r_normalScale to zero.
  555. 99:
  556. 100: Normal Z component equal to zero would be wrong anyway.
  557. 101: */
  558. 102: if (u_NormalScale.z != 0)
  559. 103: {
  560. 104: normal *= u_NormalScale;
  561. 105: }
  562. 106:
  563. 107: // HACK: the GLSL code is currently assuming
  564. 108: // DirectX normal map format (+X -Y +Z)
  565. 109: // but engine is assuming the OpenGL way (+X +Y +Z)
  566. 110: normal.y *= -1;
  567. 111: #else // !r_normalMapping
  568. 112: // Flat normal map is {0.5, 0.5, 1.0} in [ 0.0, 1.0]
  569. 113: // which is stored as {0.0, 0.0, 1.0} in [-1.0, 1.0].
  570. 114: normal = vec3(0.0, 0.0, 1.0);
  571. 115: #endif // !r_normalMapping
  572. 116:
  573. 117: return normal;
  574. 118: }
  575. 119:
  576. 120: // compute normal in worldspace from normalmap
  577. 121: #if defined(r_normalMapping)
  578. 122: vec3 NormalInWorldSpace(vec2 texNormal, mat3 tangentToWorldMatrix, in sampler2D u_NormalMap)
  579. 123: {
  580. 124: // compute normal in tangent space from normalmap
  581. 125: vec3 normal = NormalInTangentSpace(texNormal, u_NormalMap);
  582. 126: // transform normal into world space
  583. 127: return normalize(tangentToWorldMatrix * normal);
  584. 128: }
  585. 129: #else // !r_normalMapping
  586. 130: vec3 NormalInWorldSpace(vec2 texNormal, mat3 tangentToWorldMatrix)
  587. 131: {
  588. 132: // compute normal in tangent space from normalmap
  589. 133: vec3 normal = NormalInTangentSpace(texNormal);
  590. 134: // transform normal into world space
  591. 135: return normalize(tangentToWorldMatrix * normal);
  592. 136: }
  593. 137: #endif // !r_normalMapping
  594. 138:
  595. 139: #if defined(USE_RELIEF_MAPPING)
  596. 140: // compute texcoords offset from heightmap
  597. 141: // most of the code doing somewhat the same is likely to be named
  598. 142: // RayIntersectDisplaceMap in other id tech3-based engines
  599. 143: // so please keep the comment above to enable cross-tree look-up
  600. 144: vec2 ReliefTexOffset(vec2 rayStartTexCoords, vec3 viewDir, mat3 tangentToWorldMatrix, in sampler2D u_HeightMap)
  601. 145: {
  602. 146: // compute view direction in tangent space
  603. 147: vec3 tangentViewDir = normalize(viewDir * tangentToWorldMatrix);
  604. 148:
  605. 149: vec2 displacement = tangentViewDir.xy * -u_ReliefDepthScale / tangentViewDir.z;
  606. 150:
  607. 151: const int linearSearchSteps = 16;
  608. 152: const int binarySearchSteps = 6;
  609. 153:
  610. 154: float depthStep = 1.0 / float(linearSearchSteps);
  611. 155: float topDepth = 1.0 - u_ReliefOffsetBias;
  612. 156:
  613. 157: // current size of search window
  614. 158: float currentSize = depthStep;
  615. 159:
  616. 160: // current depth position
  617. 161: float currentDepth = 0.0;
  618. 162:
  619. 163: // best match found (starts with last position 1.0)
  620. 164: float bestDepth = 1.0;
  621. 165:
  622. 166: // search front to back for first point inside object
  623. 167: for(int i = 0; i < linearSearchSteps - 1; ++i)
  624. 168: {
  625. 169: currentDepth += currentSize;
  626. 170:
  627. 171: #if defined(USE_HEIGHTMAP_IN_NORMALMAP)
  628. 172: #if defined(r_texturePacks)
  629. 173: float depth = texture2D(u_HeightMap, vec3( ( rayStartTexCoords + displacement * currentDepth )
  630. 174: * u_HeightMapModifier.xy, u_HeightMapModifier.z )).a;
  631. 175: #else
  632. 176: float depth = texture2D(u_HeightMap, rayStartTexCoords + displacement * currentDepth).a;
  633. 177: #endif
  634. 178: #else // !USE_HEIGHTMAP_IN_NORMALMAP
  635. 179: #if defined(r_texturePacks)
  636. 180: float depth = texture2D(u_HeightMap, vec3( ( rayStartTexCoords + displacement * currentDepth )
  637. 181: * u_HeightMapModifier.xy, u_HeightMapModifier.z )).g;
  638. 182: #else
  639. 183: float depth = texture2D(u_HeightMap, rayStartTexCoords + displacement * currentDepth).g;
  640. 184: #endif
  641. 185: #endif // !USE_HEIGHTMAP_IN_NORMALMAP
  642. 186:
  643. 187: float heightMapDepth = topDepth - depth;
  644. 188:
  645. 189: if(bestDepth > 0.996) // if no depth found yet
  646. 190: {
  647. 191: if(currentDepth >= heightMapDepth)
  648. 192: {
  649. 193: bestDepth = currentDepth;
  650. 194: }
  651. 195: }
  652. 196: }
  653. 197:
  654. 198: currentDepth = bestDepth;
  655. 199:
  656. 200: // recurse around first point (depth) for closest match
  657. 201: for(int i = 0; i < binarySearchSteps; ++i)
  658. 202: {
  659. 203: currentSize *= 0.5;
  660. 204:
  661. 205: #if defined(USE_HEIGHTMAP_IN_NORMALMAP)
  662. 206: float depth = texture2D(u_HeightMap, rayStartTexCoords + displacement * currentDepth).a;
  663. 207: #else // !USE_HEIGHTMAP_IN_NORMALMAP
  664. 208: float depth = texture2D(u_HeightMap, rayStartTexCoords + displacement * currentDepth).g;
  665. 209: #endif // !USE_HEIGHTMAP_IN_NORMALMAP
  666. 210:
  667. 211: float heightMapDepth = topDepth - depth;
  668. 212:
  669. 213: if(currentDepth >= heightMapDepth)
  670. 214: {
  671. 215: bestDepth = currentDepth;
  672. 216: currentDepth -= 2.0 * currentSize;
  673. 217: }
  674. 218:
  675. 219: currentDepth += currentSize;
  676. 220: }
  677. 221:
  678. 222: return bestDepth * displacement;
  679. 223: }
  680. 224: #endif // USE_RELIEF_MAPPING
  681. 0: #line 0
  682. 1: /*
  683. 2: ===========================================================================
  684. 3: Copyright (C) 2006-2011 Robert Beckebans <trebor_7@users.sourceforge.net>
  685. 4:
  686. 5: This file is part of XreaL source code.
  687. 6:
  688. 7: XreaL source code is free software; you can redistribute it
  689. 8: and/or modify it under the terms of the GNU General Public License as
  690. 9: published by the Free Software Foundation; either version 2 of the License,
  691. 10: or (at your option) any later version.
  692. 11:
  693. 12: XreaL source code is distributed in the hope that it will be
  694. 13: useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  695. 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  696. 15: GNU General Public License for more details.
  697. 16:
  698. 17: You should have received a copy of the GNU General Public License
  699. 18: along with XreaL source code; if not, write to the Free Software
  700. 19: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  701. 20: ===========================================================================
  702. 21: */
  703. 22:
  704. 23: /* lightMapping_fp.glsl */
  705. 24:
  706. 25: #define LIGHTMAPPING_GLSL
  707. 26:
  708. 27: #if defined(r_texturePacks)
  709. 28: #else
  710. 29: #endif
  711. 30:
  712. 31:
  713. 32: IN(smooth) vec3 var_Position;
  714. 33: IN(smooth) vec2 var_TexCoords;
  715. 34: IN(smooth) vec4 var_Color;
  716. 35: IN(smooth) vec3 var_Tangent;
  717. 36: IN(smooth) vec3 var_Binormal;
  718. 37: IN(smooth) vec3 var_Normal;
  719. 38:
  720. 39: #if defined(r_texturePacks)
  721. 40: #else
  722. 41: #endif
  723. 42:
  724. 43:
  725. 44: #if defined(USE_LIGHT_MAPPING) || defined(USE_DELUXE_MAPPING)
  726. 45: IN(smooth) vec2 var_TexLight;
  727. 46: #endif
  728. 47:
  729. 48: #if defined(USE_GRID_LIGHTING) || defined(USE_GRID_DELUXE_MAPPING)
  730. 49: #endif
  731. 50:
  732. 51:
  733. 52: DECLARE_OUTPUT(vec4)
  734. 53:
  735. 54: void main()
  736. 55: {
  737. 10000: #line 10000 // material_fp.glsl
  738. 10001: /*
  739. 10002: ===========================================================================
  740. 10003:
  741. 10004: Daemon BSD Source Code
  742. 10005: Copyright (c) 2024 Daemon Developers
  743. 10006: All rights reserved.
  744. 10007:
  745. 10008: This file is part of the Daemon BSD Source Code (Daemon Source Code).
  746. 10009:
  747. 10010: Redistribution and use in source and binary forms, with or without
  748. 10011: modification, are permitted provided that the following conditions are met:
  749. 10012: * Redistributions of source code must retain the above copyright
  750. 10013: notice, this list of conditions and the following disclaimer.
  751. 10014: * Redistributions in binary form must reproduce the above copyright
  752. 10015: notice, this list of conditions and the following disclaimer in the
  753. 10016: documentation and/or other materials provided with the distribution.
  754. 10017: * Neither the name of the Daemon developers nor the
  755. 10018: names of its contributors may be used to endorse or promote products
  756. 10019: derived from this software without specific prior written permission.
  757. 10020:
  758. 10021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  759. 10022: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  760. 10023: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  761. 10024: DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
  762. 10025: DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  763. 10026: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  764. 10027: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  765. 10028: ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  766. 10029: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  767. 10030: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  768. 10031:
  769. 10032: ===========================================================================
  770. 10033: */
  771. 10034:
  772. 10035: /* material_fp.glsl */
  773. 10036:
  774. 10037: /*
  775. 10038: * For use with material system
  776. 10039: * All changes to uniform samplers in shaders which use this system must be reflected here
  777. 10040: * Any shader using this system should add #define SHADERNAME_GLSL in the beginning (including lib shaders)
  778. 10041: * It should then be added here in the material system and bindless texture ifdefs:
  779. 10042: * #if defined(SHADERNAME_GLSL)
  780. 10043: * sampler* samplerName = sampler*( samplerName_initial );
  781. 10044: * #endif // !SHADERNAME_GLSL
  782. 10045: * In the main shader add
  783. 10046: * #insert material
  784. 10047: * in the beginning of main() once
  785. 10048: * Any texture samplers should be passed to functions from main() or other functions
  786. 10049: */
  787. 10050:
  788. 10051: #if defined(USE_MATERIAL_SYSTEM)
  789. 10052:
  790. 10053: #if defined(r_texturePacks)
  791. 10054: #define samplerPack sampler2DArray
  792. 10055: #else
  793. 10056: #define samplerPack sampler2D
  794. Warn: Compile log:
  795. 0:71(11): error: no matching function for call to `texture(sampler2D, vec3)'; candidates are:
  796. 0:71(11): error: vec4 texture(sampler1D, float)
  797. 0:71(11): error: ivec4 texture(isampler1D, float)
  798. 0:71(11): error: uvec4 texture(usampler1D, float)
  799. 0:71(11): error: vec4 texture(sampler2D, vec2)
  800. 0:71(11): error: ivec4 texture(isampler2D, vec2)
  801. 0:71(11): error: uvec4 texture(usampler2D, vec2)
  802. 0:71(11): error: vec4 texture(sampler3D, vec3)
  803. 0:71(11): error: ivec4 texture(isampler3D, vec3)
  804. 0:71(11): error: uvec4 texture(usampler3D, vec3)
  805. 0:71(11): error: vec4 texture(samplerCube, vec3)
  806. 0:71(11): error: ivec4 texture(isamplerCube, vec3)
  807. 0:71(11): error: uvec4 texture(usamplerCube, vec3)
  808. 0:71(11): error: float texture(sampler1DShadow, vec3)
  809. 0:71(11): error: float texture(sampler2DShadow, vec3)
  810. 0:71(11): error: float texture(samplerCubeShadow, vec4)
  811. 0:71(11): error: vec4 texture(sampler1DArray, vec2)
  812. 0:71(11): error: ivec4 texture(isampler1DArray, vec2)
  813. 0:71(11): error: uvec4 texture(usampler1DArray, vec2)
  814. 0:71(11): error: vec4 texture(sampler2DArray, vec3)
  815. 0:71(11): error: ivec4 texture(isampler2DArray, vec3)
  816. 0:71(11): error: uvec4 texture(usampler2DArray, vec3)
  817. 0:71(11): error: vec4 texture(samplerCubeArray, vec4)
  818. 0:71(11): error: ivec4 texture(isamplerCubeArray, vec4)
  819. 0:71(11): error: uvec4 texture(usamplerCubeArray, vec4)
  820. 0:71(11): error: float texture(sampler1DArrayShadow, vec3)
  821. 0:71(11): error: float texture(sampler2DArrayShadow, vec4)
  822. 0:71(11): error: float texture(samplerCubeArrayShadow, vec4, float)
  823. 0:71(11): error: vec4 texture(sampler2DRect, vec2)
  824. 0:71(11): error: ivec4 texture(isampler2DRect, vec2)
  825. 0:71(11): error: uvec4 texture(usampler2DRect, vec2)
  826. 0:71(11): error: float texture(sampler2DRectShadow, vec3)
  827. 0:71(11): error: vec4 texture(sampler1D, float, float)
  828. 0:71(11): error: ivec4 texture(isampler1D, float, float)
  829. 0:71(11): error: uvec4 texture(usampler1D, float, float)
  830. 0:71(11): error: vec4 texture(sampler2D, vec2, float)
  831. 0:71(11): error: ivec4 texture(isampler2D, vec2, float)
  832. 0:71(11): error: uvec4 texture(usampler2D, vec2, float)
  833. 0:71(11): error: vec4 texture(sampler3D, vec3, float)
  834. 0:71(11): error: ivec4 texture(isampler3D, vec3, float)
  835. 0:71(11): error: uvec4 texture(usampler3D, vec3, float)
  836. 0:71(11): error: vec4 texture(samplerCube, vec3, float)
  837. 0:71(11): error: ivec4 texture(isamplerCube, vec3, float)
  838. 0:71(11): error: uvec4 texture(usamplerCube, vec3, float)
  839. 0:71(11): error: float texture(sampler1DShadow, vec3, float)
  840. 0:71(11): error: float texture(sampler2DShadow, vec3, float)
  841. 0:71(11): error: float texture(samplerCubeShadow, vec4, float)
  842. 0:71(11): error: vec4 texture(sampler1DArray, vec2, float)
  843. 0:71(11): error: ivec4 texture(isampler1DArray, vec2, float)
  844. 0:71(11): error: uvec4 texture(usampler1DArray, vec2, float)
  845. 0:71(11): error: vec4 texture(sampler2DArray, vec3, float)
  846. 0:71(11): error: ivec4 texture(isampler2DArray, vec3, float)
  847. 0:71(11): error: uvec4 texture(usampler2DArray, vec3, float)
  848. 0:71(11): error: vec4 texture(samplerCubeArray, vec4, float)
  849. 0:71(11): error: ivec4 texture(isamplerCubeArray, vec4, float)
  850. 0:71(11): error: uvec4 texture(usamplerCubeArray, vec4, float)
  851. 0:71(11): error: float texture(sampler1DArrayShadow, vec3, float)
  852. 0:71(11): error: type mismatch
  853. 0:117(16): error: no matching function for call to `NormalInWorldSpace(vec2, mat3, sampler2DArray)'; candidates are:
  854. 0:117(16): error: vec3 NormalInWorldSpace(vec2, mat3, sampler2D)
  855.  
  856. Warn: Unhandled exception (15ShaderException): Couldn't compile fragment shader: lightMappingMaterial
  857.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement