1#version 460 2uniform sampler2D sTDNoiseMap; 3uniform sampler1D sTDSineLookup; 4uniform sampler2D sTDWhite2D; 5uniform sampler3D sTDWhite3D; 6uniform sampler2DArray sTDWhite2DArray; 7uniform samplerCube sTDWhiteCube; 8uniform int uTDInstanceIDOffset; 9uniform int uTDNumInstances; 10uniform float uTDAlphaTestVal; 11#define TD_NUM_COLOR_BUFFERS 1 12#define TD_NUM_LIGHTS 0 13#define TD_NUM_SHADOWED_LIGHTS 0 14#define TD_NUM_ENV_LIGHTS 0 15#define TD_LIGHTS_ARRAY_SIZE 1 16#define TD_ENV_LIGHTS_ARRAY_SIZE 1 17#define TD_NUM_CAMERAS 1 18struct TDLight 19{ 20 vec4 position; 21 vec3 direction; 22 vec3 diffuse; 23 vec4 nearFar; 24 vec4 lightSize; 25 vec4 misc; 26 vec4 coneLookupScaleBias; 27 vec4 attenScaleBiasRoll; 28 mat4 shadowMapMatrix; 29 mat4 shadowMapCamMatrix; 30 vec4 shadowMapRes; 31 mat4 projMapMatrix; 32}; 33struct TDEnvLight 34{ 35 vec3 color; 36 mat3 rotate; 37}; 38layout(std140) uniform TDLightBlock 39{ 40 TDLight uTDLights[TD_LIGHTS_ARRAY_SIZE]; 41}; 42layout(std140) uniform TDEnvLightBlock 43{ 44 TDEnvLight uTDEnvLights[TD_ENV_LIGHTS_ARRAY_SIZE]; 45}; 46layout(std430) readonly restrict buffer TDEnvLightBuffer 47{ 48 vec3 shCoeffs[9]; 49} uTDEnvLightBuffers[TD_ENV_LIGHTS_ARRAY_SIZE]; 50struct TDPhongResult 51{ 52 vec3 diffuse; 53 vec3 specular; 54 vec3 specular2; 55 float shadowStrength; 56}; 57struct TDPBRResult 58{ 59 vec3 diffuse; 60 vec3 specular; 61 float shadowStrength; 62}; 63struct TDMatrix 64{ 65 mat4 world; 66 mat4 worldInverse; 67 mat4 worldCam; 68 mat4 worldCamInverse; 69 mat4 cam; 70 mat4 camInverse; 71 mat4 camProj; 72 mat4 camProjInverse; 73 mat4 proj; 74 mat4 projInverse; 75 mat4 worldCamProj; 76 mat4 worldCamProjInverse; 77 mat4 quadReproject; 78 mat3 worldForNormals; 79 mat3 camForNormals; 80 mat3 worldCamForNormals; 81}; 82layout(std140) uniform TDMatricesBlock { 83 TDMatrix uTDMats[TD_NUM_CAMERAS]; 84}; 85struct TDCameraInfo 86{ 87 vec4 nearFar; 88 vec4 fog; 89 vec4 fogColor; 90 int renderTOPCameraIndex; 91}; 92layout(std140) uniform TDCameraInfoBlock { 93 TDCameraInfo uTDCamInfos[TD_NUM_CAMERAS]; 94}; 95struct TDGeneral 96{ 97 vec4 ambientColor; 98 vec4 nearFar; 99 vec4 viewport; 100 vec4 viewportRes; 101 vec4 fog; 102 vec4 fogColor; 103}; 104layout(std140) uniform TDGeneralBlock { 105 TDGeneral uTDGeneral; 106}; 107 108layout(binding = 15) uniform samplerBuffer sTDInstanceT; 109layout(binding = 16) uniform samplerBuffer sTDInstanceTexCoord; 110layout(binding = 17) uniform samplerBuffer sTDInstanceColor; 111vec4 TDDither(vec4 color); 112vec3 TDHSVToRGB(vec3 c); 113vec3 TDRGBToHSV(vec3 c); 114#define PI 3.14159265 115 116vec4 TDColor(vec4 color) { return color; } 117void TDCheckOrderIndTrans() { 118} 119void TDCheckDiscard() { 120 TDCheckOrderIndTrans(); 121} 122vec4 TDDither(vec4 color) 123{ 124 float d = texture(sTDNoiseMap, 125 gl_FragCoord.xy / 256.0).r; 126 d -= 0.5; 127 d /= 256.0; 128 return vec4(color.rgb + d, color.a); 129} 130bool TDFrontFacing(vec3 pos, vec3 normal) 131{ 132 return gl_FrontFacing; 133} 134float TDAttenuateLight(int index, float lightDist) 135{ 136 return 1.0; 137} 138void TDAlphaTest(float alpha) { 139} 140float TDHardShadow(int lightIndex, vec3 worldSpacePos) 141{ return 0.0; } 142float TDSoftShadow(int lightIndex, vec3 worldSpacePos, int samples, int steps) 143{ return 0.0; } 144float TDSoftShadow(int lightIndex, vec3 worldSpacePos) 145{ return 0.0; } 146float TDShadow(int lightIndex, vec3 worldSpacePos) 147{ return 0.0; } 148vec3 TDEquirectangularToCubeMap(vec2 mapCoord); 149vec2 TDCubeMapToEquirectangular(vec3 envMapCoord); 150vec2 TDCubeMapToEquirectangular(vec3 envMapCoord, out float mipMapBias); 151vec2 TDTexGenSphere(vec3 envMapCoord); 152float iTDRadicalInverse_VdC(uint bits) 153{ 154 bits = (bits << 16u) | (bits >> 16u); 155 bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u); 156 bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u); 157 bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u); 158 bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u); 159 return float(bits) * 2.3283064365386963e-10; // / 0x100000000 160} 161vec2 iTDHammersley(uint i, uint N) 162{ 163 return vec2(float(i) / float(N), iTDRadicalInverse_VdC(i)); 164} 165vec3 iTDImportanceSampleGGX(vec2 Xi, float roughness2, vec3 N) 166{ 167 float a = roughness2; 168 float phi = 2 * 3.14159265 * Xi.x; 169 float cosTheta = sqrt( (1 - Xi.y) / (1 + (a*a - 1) * Xi.y) ); 170 float sinTheta = sqrt( 1 - cosTheta * cosTheta ); 171 172 vec3 H; 173 H.x = sinTheta * cos(phi); 174 H.y = sinTheta * sin(phi); 175 H.z = cosTheta; 176 177 vec3 upVector = abs(N.z) < 0.999 ? vec3(0, 0, 1) : vec3(1, 0, 0); 178 vec3 tangentX = normalize(cross(upVector, N)); 179 vec3 tangentY = cross(N, tangentX); 180 181 // Tangent to world space 182 vec3 worldResult = tangentX * H.x + tangentY * H.y + N * H.z; 183 return worldResult; 184} 185float iTDDistributionGGX(vec3 normal, vec3 half_vector, float roughness2) 186{ 187 const float Epsilon = 0.000001; 188 189 float NdotH = clamp(dot(normal, half_vector), Epsilon, 1.0); 190 191 float alpha2 = roughness2 * roughness2; 192 193 float denom = NdotH * NdotH * (alpha2 - 1.0) + 1.0; 194 denom = max(1e-8, denom); 195 return alpha2 / (PI * denom * denom); 196} 197vec3 iTDCalcF(vec3 F0, float VdotH) { 198 return F0 + (vec3(1.0) - F0) * pow(2.0, (-5.55473*VdotH - 6.98316) * VdotH); 199} 200 201float iTDCalcG(float NdotL, float NdotV, float k) { 202 float Gl = 1.0 / (NdotL * (1.0 - k) + k); 203 float Gv = 1.0 / (NdotV * (1.0 - k) + k); 204 return Gl * Gv; 205} 206// 0 - All options 207TDPBRResult TDLightingPBR(int index,vec3 diffuseColor,vec3 specularColor,vec3 worldSpacePos,vec3 normal,float shadowStrength,vec3 shadowColor,vec3 camVector,float roughness) 208{ 209 TDPBRResult res; 210 return res; 211} 212// 0 - All options 213void TDLightingPBR(inout vec3 diffuseContrib,inout vec3 specularContrib,inout float shadowStrengthOut,int index,vec3 diffuseColor,vec3 specularColor,vec3 worldSpacePos,vec3 normal,float shadowStrength,vec3 shadowColor,vec3 camVector,float roughness) 214{ 215 TDPBRResult res = TDLightingPBR(index,diffuseColor,specularColor,worldSpacePos,normal,shadowStrength,shadowColor,camVector,roughness); diffuseContrib = res.diffuse; 216 specularContrib = res.specular; 217 shadowStrengthOut = res.shadowStrength; 218} 219// 0 - All options 220void TDLightingPBR(inout vec3 diffuseContrib,inout vec3 specularContrib,int index,vec3 diffuseColor,vec3 specularColor,vec3 worldSpacePos,vec3 normal,float shadowStrength,vec3 shadowColor,vec3 camVector,float roughness) 221{ 222 TDPBRResult res = TDLightingPBR(index,diffuseColor,specularColor,worldSpacePos,normal,shadowStrength,shadowColor,camVector,roughness); diffuseContrib = res.diffuse; 223 specularContrib = res.specular; 224} 225// 0 - All options 226TDPBRResult TDEnvLightingPBR(int index,vec3 diffuseColor,vec3 specularColor,vec3 normal,vec3 camVector,float roughness,float ambientOcclusion) 227{ 228 TDPBRResult res; 229 return res; 230} 231// 0 - All options 232void TDEnvLightingPBR(inout vec3 diffuseContrib,inout vec3 specularContrib,int index,vec3 diffuseColor,vec3 specularColor,vec3 normal,vec3 camVector,float roughness,float ambientOcclusion) 233{ 234 TDPBRResult res = TDEnvLightingPBR(index, diffuseColor, specularColor, normal, camVector, roughness, ambientOcclusion); 235 diffuseContrib = res.diffuse; 236 specularContrib = res.specular; 237} 238// 0 - All options 239TDPhongResult TDLighting(int index,vec3 worldSpacePos,vec3 normal,float shadowStrength,vec3 shadowColor,vec3 camVector,float shininess,float shininess2) 240{ 241 TDPhongResult res; 242 switch (index) 243 { 244 default: 245 res.diffuse = vec3(0.0); 246 res.specular = vec3(0.0); 247 res.specular2 = vec3(0.0); 248 res.shadowStrength = 0.0; 249 break; 250 } 251 return res; 252} 253// 0 - Legacy 254void TDLighting(inout vec3 diffuseContrib,inout vec3 specularContrib,inout vec3 specularContrib2,inout float shadowStrengthOut,int index,vec3 worldSpacePos,vec3 normal,float shadowStrength,vec3 shadowColor,vec3 camVector,float shininess,float shininess2) 255{ 256 TDPhongResult res; 257 switch (index) 258 { 259 default: 260 res.diffuse = vec3(0.0); 261 res.specular = vec3(0.0); 262 res.specular2 = vec3(0.0); 263 res.shadowStrength = 0.0; 264 break; 265 } 266 diffuseContrib = res.diffuse; 267 specularContrib = res.specular; 268 specularContrib2 = res.specular2; 269 shadowStrengthOut = res.shadowStrength; 270} 271// 0 - Legacy 272void TDLighting(inout vec3 diffuseContrib,inout vec3 specularContrib,inout vec3 specularContrib2,int index,vec3 worldSpacePos,vec3 normal,float shadowStrength,vec3 shadowColor,vec3 camVector,float shininess,float shininess2) 273{ 274 TDPhongResult res; 275 switch (index) 276 { 277 default: 278 res.diffuse = vec3(0.0); 279 res.specular = vec3(0.0); 280 res.specular2 = vec3(0.0); 281 res.shadowStrength = 0.0; 282 break; 283 } 284 diffuseContrib = res.diffuse; 285 specularContrib = res.specular; 286 specularContrib2 = res.specular2; 287} 288// 1 - Without specular2 289void TDLighting(inout vec3 diffuseContrib,inout vec3 specularContrib,int index,vec3 worldSpacePos,vec3 normal,float shadowStrength,vec3 shadowColor,vec3 camVector,float shininess) 290{ 291 TDPhongResult res; 292 switch (index) 293 { 294 default: 295 res.diffuse = vec3(0.0); 296 res.specular = vec3(0.0); 297 res.specular2 = vec3(0.0); 298 res.shadowStrength = 0.0; 299 break; 300 } 301 diffuseContrib = res.diffuse; 302 specularContrib = res.specular; 303} 304// 2 - Without shadows 305void TDLighting(inout vec3 diffuseContrib,inout vec3 specularContrib,inout vec3 specularContrib2,int index,vec3 worldSpacePos,vec3 normal,vec3 camVector,float shininess,float shininess2) 306{ 307 TDPhongResult res; 308 switch (index) 309 { 310 default: 311 res.diffuse = vec3(0.0); 312 res.specular = vec3(0.0); 313 res.specular2 = vec3(0.0); 314 res.shadowStrength = 0.0; 315 break; 316 } 317 diffuseContrib = res.diffuse; 318 specularContrib = res.specular; 319 specularContrib2 = res.specular2; 320} 321// 3 - diffuse and specular only 322void TDLighting(inout vec3 diffuseContrib,inout vec3 specularContrib,int index,vec3 worldSpacePos,vec3 normal,vec3 camVector,float shininess) 323{ 324 TDPhongResult res; 325 switch (index) 326 { 327 default: 328 res.diffuse = vec3(0.0); 329 res.specular = vec3(0.0); 330 res.specular2 = vec3(0.0); 331 res.shadowStrength = 0.0; 332 break; 333 } 334 diffuseContrib = res.diffuse; 335 specularContrib = res.specular; 336} 337// 4 - Diffuse only 338void TDLighting(inout vec3 diffuseContrib,int index, vec3 worldSpacePos, vec3 normal) 339{ 340 TDPhongResult res; 341 switch (index) 342 { 343 default: 344 res.diffuse = vec3(0.0); 345 res.specular = vec3(0.0); 346 res.specular2 = vec3(0.0); 347 res.shadowStrength = 0.0; 348 break; 349 } 350 diffuseContrib = res.diffuse; 351} 352// 5 - diffuse only with shadows 353void TDLighting(inout vec3 diffuseContrib,int index,vec3 worldSpacePos,vec3 normal,float shadowStrength,vec3 shadowColor) 354{ 355 TDPhongResult res; 356 switch (index) 357 { 358 default: 359 res.diffuse = vec3(0.0); 360 res.specular = vec3(0.0); 361 res.specular2 = vec3(0.0); 362 res.shadowStrength = 0.0; 363 break; 364 } 365 diffuseContrib = res.diffuse; 366} 367vec4 TDProjMap(int index, vec3 worldSpacePos, vec4 defaultColor) { 368 switch (index) 369 { 370 default: return defaultColor; 371 } 372} 373vec4 TDFog(vec4 color, vec3 lightingSpacePosition, int cameraIndex) { 374 switch (cameraIndex) { 375 default: 376 case 0: 377 { 378 return color; 379 } 380 } 381} 382vec4 TDFog(vec4 color, vec3 lightingSpacePosition) 383{ 384 return TDFog(color, lightingSpacePosition, 0); 385} 386vec3 TDInstanceTexCoord(int index, vec3 t) { 387 vec3 v; 388 int coord = index; 389 vec4 samp = texelFetch(sTDInstanceTexCoord, coord); 390 v[0] = t.s; 391 v[1] = t.t; 392 v[2] = samp[0]; 393 t.stp = v.stp; 394 return t; 395} 396bool TDInstanceActive(int index) { 397 index -= uTDInstanceIDOffset; 398 float v; 399 int coord = index; 400 vec4 samp = texelFetch(sTDInstanceT, coord); 401 v = samp[0]; 402 return v != 0.0; 403} 404vec3 iTDInstanceTranslate(int index, out bool instanceActive) { 405 int origIndex = index; 406 index -= uTDInstanceIDOffset; 407 vec3 v; 408 int coord = index; 409 vec4 samp = texelFetch(sTDInstanceT, coord); 410 v[0] = samp[1]; 411 v[1] = samp[2]; 412 v[2] = samp[3]; 413 instanceActive = samp[0] != 0.0; 414 return v; 415} 416vec3 TDInstanceTranslate(int index) { 417 index -= uTDInstanceIDOffset; 418 vec3 v; 419 int coord = index; 420 vec4 samp = texelFetch(sTDInstanceT, coord); 421 v[0] = samp[1]; 422 v[1] = samp[2]; 423 v[2] = samp[3]; 424 return v; 425} 426mat3 TDInstanceRotateMat(int index) { 427 index -= uTDInstanceIDOffset; 428 vec3 v = vec3(0.0, 0.0, 0.0); 429 mat3 m = mat3(1.0); 430{ 431 mat3 r; 432} 433 return m; 434} 435vec3 TDInstanceScale(int index) { 436 index -= uTDInstanceIDOffset; 437 vec3 v = vec3(1.0, 1.0, 1.0); 438 return v; 439} 440vec3 TDInstancePivot(int index) { 441 index -= uTDInstanceIDOffset; 442 vec3 v = vec3(0.0, 0.0, 0.0); 443 return v; 444} 445vec3 TDInstanceRotTo(int index) { 446 index -= uTDInstanceIDOffset; 447 vec3 v = vec3(0.0, 0.0, 1.0); 448 return v; 449} 450vec3 TDInstanceRotUp(int index) { 451 index -= uTDInstanceIDOffset; 452 vec3 v = vec3(0.0, 1.0, 0.0); 453 return v; 454} 455mat4 TDInstanceMat(int id) { 456 bool instanceActive = true; 457 vec3 t = iTDInstanceTranslate(id, instanceActive); 458 if (!instanceActive) 459 { 460 return mat4(0.0); 461 } 462 mat4 m = mat4(1.0); 463 { 464 vec3 tt = t; 465 m[3][0] += m[0][0]*tt.x; 466 m[3][1] += m[0][1]*tt.x; 467 m[3][2] += m[0][2]*tt.x; 468 m[3][3] += m[0][3]*tt.x; 469 m[3][0] += m[1][0]*tt.y; 470 m[3][1] += m[1][1]*tt.y; 471 m[3][2] += m[1][2]*tt.y; 472 m[3][3] += m[1][3]*tt.y; 473 m[3][0] += m[2][0]*tt.z; 474 m[3][1] += m[2][1]*tt.z; 475 m[3][2] += m[2][2]*tt.z; 476 m[3][3] += m[2][3]*tt.z; 477 } 478 return m; 479} 480mat3 TDInstanceMat3(int id) { 481 mat3 m = mat3(1.0); 482 return m; 483} 484mat3 TDInstanceMat3ForNorm(int id) { 485 mat3 m = TDInstanceMat3(id); 486 return m; 487} 488vec4 TDInstanceColor(int index, vec4 curColor) { 489 index -= uTDInstanceIDOffset; 490 vec4 v; 491 int coord = index; 492 vec4 samp = texelFetch(sTDInstanceColor, coord); 493 v[0] = samp[0]; 494 v[1] = samp[1]; 495 v[2] = samp[2]; 496 v[3] = 1.0; 497 curColor[0] = v[0]; 498; 499 curColor[1] = v[1]; 500; 501 curColor[2] = v[2]; 502; 503 return curColor; 504} 505