Advertisement
Boost

Untitled

Jul 19th, 2019
820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // GLSL Fragment Shader "fsmain"
  2. // Generated by XShaderCompiler
  3. // 19/07/2019 12:09:16
  4.  
  5. #version 450
  6.  
  7. #extension GL_EXT_samplerless_texture_functions : enable
  8.  
  9. layout(location = 0) in vec2 bs_TEXCOORD0;
  10. layout(location = 1) in vec3 bs_TEXCOORD1;
  11. layout(location = 2) in vec3 bs_NORMAL0;
  12. layout(location = 3) in vec4 bs_TANGENT0;
  13.  
  14. layout(location = 0) out vec4 SV_Target0;
  15.  
  16. layout(std140, row_major, binding = 31) uniform PerCamera
  17. {
  18.     vec3  gViewDir;
  19.     vec3  gViewOrigin;
  20.     mat4  gMatViewProj;
  21.     mat4  gMatView;
  22.     mat4  gMatProj;
  23.     mat4  gMatInvProj;
  24.     mat4  gMatInvViewProj;
  25.     mat4  gMatScreenToWorld;
  26.     mat4  gNDCToPrevNDC;
  27.     vec2  gDeviceZToWorldZ;
  28.     vec2  gNDCZToWorldZ;
  29.     vec2  gNDCZToDeviceZ;
  30.     vec2  gNearFar;
  31.     ivec4 gViewportRectangle;
  32.     vec4  gClipToUVScaleOffset;
  33.     vec4  gUVToClipScaleOffset;
  34.     float gAmbientFactor;
  35. };
  36.  
  37. struct VStoFS
  38. {
  39.     vec4 position;
  40.     vec2 uv0;
  41.     vec3 worldPosition;
  42.     vec3 tangentToWorldZ;
  43.     vec4 tangentToWorldX;
  44. };
  45.  
  46. vec3 calcWorldNormal(VStoFS xsr_input, vec3 surfaceNormal)
  47. {
  48.     vec3 tangentToWorldX = xsr_input.tangentToWorldX.xyz;
  49.     vec3 tangentToWorldZ = xsr_input.tangentToWorldZ;
  50.     vec3 tangentToWorldY = cross(tangentToWorldZ, tangentToWorldX) * xsr_input.tangentToWorldX.w;
  51.     mat3 tangentToWorld = mat3(tangentToWorldX, tangentToWorldY, tangentToWorldZ);
  52.     return normalize((tangentToWorld * surfaceNormal));
  53. }
  54.  
  55. struct SurfaceData
  56. {
  57.     vec4  albedo;
  58.     vec4  worldNormal;
  59.     float depth;
  60.     float roughness;
  61.     float metalness;
  62.     uint  mask;
  63. };
  64.  
  65. struct LightData
  66. {
  67.     vec3  position;
  68.     float boundRadius;
  69.     vec3  direction;
  70.     float luminance;
  71.     vec3  spotAngles;
  72.     float attRadiusSqrdInv;
  73.     vec3  color;
  74.     float srcRadius;
  75.     vec3  shiftedLightPosition;
  76.     float padding;
  77. };
  78.  
  79. vec3 calcMicrofacetFresnelShlick(vec3 F0, float LoH)
  80. {
  81.     return F0 + (vec3(1.0f) - F0) * pow(1.0f - LoH, 5.0f);
  82. }
  83.  
  84. float calcMicrofacetShadowingSmithGGX(float roughness4, float NoV, float NoL)
  85. {
  86.     float g1V = NoV + sqrt(NoV * (NoV - NoV * roughness4) + roughness4);
  87.     float g1L = NoL + sqrt(NoL * (NoL - NoL * roughness4) + roughness4);
  88.     return (float(1.0f) / (g1V * g1L));
  89. }
  90.  
  91. float calcMicrofacetDistGGX(float roughness4, float NoH)
  92. {
  93.     float d = (NoH * roughness4 - NoH) * NoH + 1.0f;
  94.     return roughness4 / (3.1415926f * d * d);
  95. }
  96.  
  97. vec3 calcDiffuseLambert(vec3 color)
  98. {
  99.     return color * (1.0f / 3.1415926f);
  100. }
  101.  
  102. float getSpotAttenuation(vec3 toLight, LightData lightData)
  103. {
  104.     float xsr_output = clamp((dot(toLight, -lightData.direction) - lightData.spotAngles.y) * lightData.spotAngles.z, float(0), float(1));
  105.     return xsr_output * xsr_output;
  106. }
  107.  
  108. float getRadialAttenuation(float distance2, LightData lightData)
  109. {
  110.     float radialAttenuation = distance2 * lightData.attRadiusSqrdInv;
  111.     radialAttenuation *= radialAttenuation;
  112.     radialAttenuation = clamp(1.0f - radialAttenuation, float(0), float(1));
  113.     radialAttenuation *= radialAttenuation;
  114.     return radialAttenuation;
  115. }
  116.  
  117. float illuminancePointLight(float distance2, float NoL, LightData lightData)
  118. {
  119.     return (lightData.luminance * NoL) / max(distance2, 0.01f * 0.01f);
  120. }
  121.  
  122. float illuminanceScaleSphereDiskAreaLight(float unclampedNoL, float sinSolidAngleSqrd)
  123. {
  124.     float sinSolidAngle = sqrt(sinSolidAngleSqrd);
  125.     return 3.1415926f * sinSolidAngleSqrd * clamp(unclampedNoL, float(0), float(1));
  126. }
  127.  
  128. float illuminanceSphereAreaLight(float unclampedNoL, float distToLight2, LightData lightData)
  129. {
  130.     float radius2 = lightData.srcRadius * lightData.srcRadius;
  131.     float sinSolidAngle2 = radius2 / distToLight2;
  132.     sinSolidAngle2 = min(sinSolidAngle2, 0.9999f);
  133.     return lightData.luminance * illuminanceScaleSphereDiskAreaLight(unclampedNoL, sinSolidAngle2);
  134. }
  135.  
  136. float illuminanceDiscAreaLight(float unclampedNoL, float distToLight2, vec3 L, LightData lightData)
  137. {
  138.     float radius2 = lightData.srcRadius * lightData.srcRadius;
  139.     float sinSolidAngle2 = clamp(radius2 / (radius2 + max(radius2, distToLight2)), float(0), float(1));
  140.     return lightData.luminance * illuminanceScaleSphereDiskAreaLight(unclampedNoL, sinSolidAngle2 * clamp(dot(lightData.direction, -L), float(0), float(1)));
  141. }
  142.  
  143. vec3 getSpecularDominantDir(vec3 N, vec3 R, float roughness)
  144. {
  145.     float r2 = roughness * roughness;
  146.     return normalize(mix(N, R, vec3((1,000000f - r2) * (sqrt(1,000000f - r2) + r2))));
  147. }
  148.  
  149. vec3 evaluateStandardBRDF(vec3 V, vec3 L, float specLobeEnergy, SurfaceData surfaceData)
  150. {
  151.     vec3 N = surfaceData.worldNormal.xyz;
  152.     vec3 H = normalize(V + L);
  153.     float LoH = clamp(dot(L, H), float(0), float(1));
  154.     float NoH = clamp(dot(N, H), float(0), float(1));
  155.     float NoV = clamp(dot(N, V), float(0), float(1));
  156.     float NoL = clamp(dot(N, L), float(0), float(1));
  157.     vec3 diffuseColor = mix(surfaceData.albedo.rgb, vec3(0.0f, 0.0f, 0.0f), vec3(surfaceData.metalness));
  158.     vec3 specularColor = mix(vec3(0.04f, 0.04f, 0.04f), surfaceData.albedo.rgb, vec3(surfaceData.metalness));
  159.     vec3 diffuse = calcDiffuseLambert(diffuseColor);
  160.     float roughness = max(surfaceData.roughness, 0.04f);
  161.     float roughness2 = roughness * roughness;
  162.     float roughness4 = roughness2 * roughness2;
  163.     vec3 specular = calcMicrofacetFresnelShlick(specularColor, LoH) * calcMicrofacetDistGGX(roughness4, NoH) * calcMicrofacetShadowingSmithGGX(roughness4, NoV, NoL);
  164.     return diffuse + specular * specLobeEnergy;
  165. }
  166.  
  167. vec3 getLuminanceDirectional(LightData lightData, vec3 worldPos, vec3 V, vec3 R, SurfaceData surfaceData)
  168. {
  169.     vec3 N = surfaceData.worldNormal.xyz;
  170.     vec3 L = -lightData.direction;
  171.     float NoL = clamp(dot(N, L), float(0), float(1));
  172.     float specEnergy = 1.0f;
  173.     if (lightData.srcRadius > 0,000000f)
  174.     {
  175.         float diskRadius = sin(lightData.srcRadius);
  176.         float distanceToDisk = cos(lightData.srcRadius);
  177.         float DoR = dot(L, R);
  178.         vec3 S = normalize(R - DoR * L);
  179.         L = DoR < distanceToDisk ? normalize(distanceToDisk * L + S * diskRadius) : R;
  180.     }
  181.     vec3 surfaceShading = evaluateStandardBRDF(V, L, specEnergy, surfaceData);
  182.     float illuminance = lightData.luminance * NoL;
  183.     return lightData.color * illuminance * surfaceShading;
  184. }
  185.  
  186. vec3 getLuminanceRadial(LightData lightData, vec3 worldPos, vec3 V, vec3 R, float roughness2, SurfaceData surfaceData)
  187. {
  188.     vec3 N = surfaceData.worldNormal.xyz;
  189.     vec3 toLight = lightData.position - worldPos;
  190.     float distToLightSqrd = dot(toLight, toLight);
  191.     float invDistToLight = inversesqrt(distToLightSqrd);
  192.     vec3 L = toLight * invDistToLight;
  193.     float NoL = dot(N, L);
  194.     float specEnergy = 1.0f;
  195.     float illuminance = 0.0f;
  196.     if (lightData.srcRadius > 0,000000f)
  197.     {
  198.         illuminance = illuminanceSphereAreaLight(NoL, distToLightSqrd, lightData);
  199.         float sphereAngle = clamp(lightData.srcRadius * invDistToLight, float(0), float(1));
  200.         specEnergy = roughness2 / clamp(roughness2 + 0.5f * sphereAngle, float(0), float(1));
  201.         specEnergy *= specEnergy;
  202.         vec3 closestPointOnRay = dot(toLight, R) * R;
  203.         vec3 centerToRay = closestPointOnRay - toLight;
  204.         float invDistToRay = inversesqrt(dot(centerToRay, centerToRay));
  205.         vec3 closestPointOnSphere = toLight + centerToRay * clamp(lightData.srcRadius * invDistToRay, float(0), float(1));
  206.         toLight = closestPointOnSphere;
  207.         L = normalize(toLight);
  208.     }
  209.     else
  210.     {
  211.         NoL = clamp(NoL, float(0), float(1));
  212.         illuminance = illuminancePointLight(distToLightSqrd, NoL, lightData);
  213.     }
  214.     float attenuation = getRadialAttenuation(distToLightSqrd, lightData);
  215.     vec3 surfaceShading = evaluateStandardBRDF(V, L, specEnergy, surfaceData);
  216.     return lightData.color * illuminance * attenuation * surfaceShading;
  217. }
  218.  
  219. vec3 getLuminanceSpot(LightData lightData, vec3 worldPos, vec3 V, vec3 R, float roughness2, SurfaceData surfaceData)
  220. {
  221.     vec3 N = surfaceData.worldNormal.xyz;
  222.     vec3 toLight = lightData.position - worldPos;
  223.     float distToLightSqrd = dot(toLight, toLight);
  224.     float invDistToLight = inversesqrt(distToLightSqrd);
  225.     vec3 L = toLight * invDistToLight;
  226.     float NoL = dot(N, L);
  227.     float specEnergy = 1.0f;
  228.     float illuminance = 0.0f;
  229.     float spotAttenuation = 1.0f;
  230.     if (lightData.srcRadius > 0,000000f)
  231.     {
  232.         illuminance = illuminanceDiscAreaLight(NoL, distToLightSqrd, L, lightData);
  233.         float rightDiscAngle = clamp(lightData.srcRadius * invDistToLight, float(0), float(1));
  234.         float discAngle = rightDiscAngle * clamp(dot(lightData.direction, -L), float(0), float(1));
  235.         specEnergy = roughness2 / clamp(roughness2 + 0.5f * discAngle, float(0), float(1));
  236.         specEnergy *= specEnergy;
  237.         vec3 discNormal = -lightData.direction;
  238.         float distAlongLightDir = max(dot(R, discNormal), 1.e-6f);
  239.         float t = dot(toLight, discNormal) / distAlongLightDir;
  240.         vec3 closestPointOnPlane = R * t;
  241.         vec3 centerToRay = closestPointOnPlane - toLight;
  242.         float invDistToRay = inversesqrt(dot(centerToRay, centerToRay));
  243.         vec3 closestPointOnDisc = toLight + centerToRay * clamp(lightData.srcRadius * invDistToRay, float(0), float(1));
  244.         toLight = closestPointOnDisc;
  245.         L = normalize(toLight);
  246.         vec3 toSpotEdge = normalize(lightData.shiftedLightPosition - worldPos);
  247.         spotAttenuation = getSpotAttenuation(toSpotEdge, lightData);
  248.     }
  249.     else
  250.     {
  251.         NoL = clamp(NoL, float(0), float(1));
  252.         illuminance = illuminancePointLight(distToLightSqrd, NoL, lightData);
  253.         spotAttenuation = getSpotAttenuation(L, lightData);
  254.     }
  255.     float radialAttenuation = getRadialAttenuation(distToLightSqrd, lightData);
  256.     float attenuation = spotAttenuation * radialAttenuation;
  257.     vec3 surfaceShading = evaluateStandardBRDF(V, L, specEnergy, surfaceData);
  258.     return lightData.color * illuminance * attenuation * surfaceShading;
  259. }
  260.  
  261. float mapRoughnessToMipLevel(float roughness, int numMips)
  262. {
  263.     return max(0,000000f, -2.8f * log2(1.0f - roughness));
  264. }
  265.  
  266. struct ReflProbeData
  267. {
  268.     vec3  position;
  269.     float radius;
  270.     vec3  boxExtents;
  271.     float transitionDistance;
  272.     mat4  invBoxTransform;
  273.     uint  cubemapIdx;
  274.     uint  type;
  275.     vec2  padding;
  276. };
  277.  
  278. layout(binding = 35) uniform textureCube gSkyReflectionTex;
  279.  
  280. layout(binding = 36) uniform sampler gSkyReflectionSamp;
  281.  
  282. layout(binding = 37) uniform textureCubeArray gReflProbeCubemaps;
  283.  
  284. layout(binding = 38) uniform sampler gReflProbeSamp;
  285.  
  286. layout(binding = 39) uniform texture2D gAmbientOcclusionTex;
  287.  
  288. layout(binding = 40) uniform sampler gAmbientOcclusionSamp;
  289.  
  290. layout(binding = 41) uniform texture2D gSSRTex;
  291.  
  292. layout(binding = 42) uniform sampler gSSRSamp;
  293.  
  294. layout(binding = 43) uniform texture2D gPreintegratedEnvBRDF;
  295.  
  296. layout(binding = 44) uniform sampler gPreintegratedEnvBRDFSamp;
  297.  
  298. layout(std140, binding = 45) uniform ReflProbeParams
  299. {
  300.     uint  gReflCubemapNumMips;
  301.     uint  gNumProbes;
  302.     uint  gSkyCubemapAvailable;
  303.     uint  gUseReflectionMaps;
  304.     uint  gSkyCubemapNumMips;
  305.     float gSkyBrightness;
  306. };
  307.  
  308. float getSphereReflectionContribution(float normalizedDistance)
  309. {
  310.     float t = clamp(2.5f - 2.5f * normalizedDistance, float(0), float(1));
  311.     return t * t * (3.0f - 2.0f * t);
  312. }
  313.  
  314. vec3 getLookupForSphereProxy(vec3 originWS, vec3 dirWS, vec3 centerWS, float radius)
  315. {
  316.     float radius2 = radius * radius;
  317.     vec3 originLS = originWS - centerWS;
  318.     float a = dot(originLS, dirWS);
  319.     float dist2 = a * a - dot(originLS, originLS) + radius2;
  320.     vec3 lookupDir = dirWS;
  321.     if (dist2 >= 0,000000f)
  322.     {
  323.         float farDist = sqrt(dist2) - a;
  324.         lookupDir = originLS + farDist * dirWS;
  325.     }
  326.     return lookupDir;
  327. }
  328.  
  329. float getDistBoxToPoint(vec3 pt, vec3 extents)
  330. {
  331.     vec3 d = max(max(-extents - pt, vec3(0)), pt - extents);
  332.     return length(d);
  333. }
  334.  
  335. vec3 getLookupForBoxProxy(vec3 originWS, vec3 dirWS, vec3 centerWS, vec3 extents, mat4 invBoxTransform, float transitionDistance, out float contribution)
  336. {
  337.     vec3 originLS = (vec4(originWS, 1) * invBoxTransform).xyz;
  338.     vec3 dirLS = (vec4(dirWS, 0) * invBoxTransform).xyz;
  339.     vec3 invDirLS = (vec3(vec3(1)) / (dirLS));
  340.     vec3 intersectsMax = invDirLS - originLS * invDirLS;
  341.     vec3 intersectsMin = -invDirLS - originLS * invDirLS;
  342.     vec3 positiveIntersections = max(intersectsMax, intersectsMin);
  343.     float intersectDist = min(positiveIntersections.x, min(positiveIntersections.y, positiveIntersections.z));
  344.     vec3 intersectPositionWS = originWS + intersectDist * dirWS;
  345.     vec3 lookupDir = intersectPositionWS - centerWS;
  346.     vec3 reducedExtents = extents - vec3(transitionDistance, transitionDistance, transitionDistance);
  347.     float distToBox = getDistBoxToPoint(originLS * extents, reducedExtents);
  348.     float normalizedDistance = distToBox / transitionDistance;
  349.     float t = clamp(3.3333f - 3.3333f * normalizedDistance, float(0), float(1));
  350.     contribution = t * t * (3.0f - 2.0f * t);
  351.     return lookupDir;
  352. }
  353.  
  354. vec4 evaluateProbe(vec3 worldPos, vec3 dir, float mipLevel, ReflProbeData probeData)
  355. {
  356.     vec3 probeToPos = worldPos - probeData.position;
  357.     float distToProbe = length(probeToPos);
  358.     float normalizedDist = clamp(distToProbe / probeData.radius, float(0), float(1));
  359.     if (distToProbe <= probeData.radius)
  360.     {
  361.         vec3 correctedDir;
  362.         float contribution = 0,000000f;
  363.         if (probeData.type == 0u)
  364.         {
  365.             correctedDir = getLookupForSphereProxy(worldPos, dir, probeData.position, probeData.radius);
  366.             contribution = getSphereReflectionContribution(normalizedDist);
  367.         }
  368.         else if (probeData.type == 1u)
  369.         {
  370.             correctedDir = getLookupForBoxProxy(worldPos, dir, probeData.position, probeData.boxExtents, probeData.invBoxTransform, probeData.transitionDistance, contribution);
  371.         }
  372.         vec4 probeSample = textureLod(samplerCubeArray(gReflProbeCubemaps, gReflProbeSamp), vec4(correctedDir, probeData.cubemapIdx), mipLevel);
  373.         probeSample *= vec4(contribution);
  374.         return vec4(probeSample.rgb, (1.0f - contribution));
  375.     }
  376.     return vec4(0, 0, 0, 1.0f);
  377. }
  378.  
  379. float getSpecularOcclusion(float NoV, float r, float ao)
  380. {
  381.     float r2 = r * r;
  382.     return clamp(pow(NoV + ao, r2) - 1.0f + ao, float(0), float(1));
  383. }
  384.  
  385. layout(std140, binding = 46) uniform Lights
  386. {
  387.     LightData gLights[8];
  388. };
  389.  
  390. vec4 getDirectLighting(vec3 worldPos, vec3 V, vec3 R, SurfaceData surfaceData, uvec4 lightOffsets)
  391. {
  392.     vec3 N = surfaceData.worldNormal.xyz;
  393.     float roughness2 = max(surfaceData.roughness, 0.08f);
  394.     roughness2 *= roughness2;
  395.     vec3 outLuminance = vec3(0);
  396.     float alpha = 0.0f;
  397.     if (surfaceData.worldNormal.w > 0.0f)
  398.     {
  399.         for (uint i = 0u; i < lightOffsets.x; ++i)
  400.         {
  401.             LightData lightData = gLights[i];
  402.             outLuminance += getLuminanceDirectional(lightData, worldPos, V, R, surfaceData);
  403.         }
  404.         for (uint j = lightOffsets.y; j < lightOffsets.z; ++j)
  405.         {
  406.             uint lightIdx = j;
  407.             LightData lightData = gLights[lightIdx];
  408.             outLuminance += getLuminanceRadial(lightData, worldPos, V, R, roughness2, surfaceData);
  409.         }
  410.         for (uint k = lightOffsets.z; k < lightOffsets.w; ++k)
  411.         {
  412.             uint lightIdx = k;
  413.             LightData lightData = gLights[lightIdx];
  414.             outLuminance += getLuminanceSpot(lightData, worldPos, V, R, roughness2, surfaceData);
  415.         }
  416.         outLuminance += surfaceData.albedo.rgb * gAmbientFactor / 3.1415926f;
  417.         alpha = 1.0f;
  418.     }
  419.     return vec4(outLuminance, alpha);
  420. }
  421.  
  422. layout(std140, binding = 47) uniform ReflProbes
  423. {
  424.     ReflProbeData gReflectionProbes[8];
  425. };
  426.  
  427. vec3 gatherReflectionRadiance(vec3 worldPos, vec3 dir, float roughness, float alpha, vec3 specularColor, uint probeOffset, uint numProbes)
  428. {
  429.     if (gUseReflectionMaps == 0u)
  430.         return specularColor;
  431.     float mipLevel = mapRoughnessToMipLevel(roughness, int(gReflCubemapNumMips));
  432.     vec3 xsr_output = vec3(0);
  433.     for (uint i = 0u; i < numProbes; i++)
  434.     {
  435.         if (alpha < 0.001f)
  436.             break;
  437.         uint probeIdx = probeOffset + i;
  438.         ReflProbeData probeData = gReflectionProbes[probeIdx];
  439.         vec4 probeValue = evaluateProbe(worldPos, dir, mipLevel, probeData);
  440.         xsr_output += probeValue.rgb * alpha;
  441.         alpha *= probeValue.w;
  442.     }
  443.     if (gSkyCubemapAvailable > 0u)
  444.     {
  445.         float skyMipLevel = mapRoughnessToMipLevel(roughness, int(gSkyCubemapNumMips));
  446.         vec4 skySample = textureLod(samplerCube(gSkyReflectionTex, gSkyReflectionSamp), dir, skyMipLevel) * gSkyBrightness;
  447.         xsr_output += skySample.rgb * alpha;
  448.     }
  449.     return xsr_output;
  450. }
  451.  
  452. vec3 getImageBasedSpecular(vec3 worldPos, vec3 V, vec3 R, SurfaceData surfaceData, float ao, vec4 ssr, uint probeOffset, uint numProbes)
  453. {
  454.     vec3 N = surfaceData.worldNormal.xyz;
  455.     float NoV = clamp(dot(N, V), float(0), float(1));
  456.     vec3 specularColor = mix(vec3(0.04f, 0.04f, 0.04f), surfaceData.albedo.rgb, vec3(surfaceData.metalness));
  457.     vec3 radiance = ssr.rgb;
  458.     float alpha = 1.0f - ssr.a;
  459.     float specOcclusion = getSpecularOcclusion(NoV, surfaceData.roughness * surfaceData.roughness, ao);
  460.     alpha *= specOcclusion;
  461.     radiance += gatherReflectionRadiance(worldPos, R, surfaceData.roughness, alpha, specularColor, probeOffset, numProbes);
  462.     vec2 envBRDF = textureLod(sampler2D(gPreintegratedEnvBRDF, gPreintegratedEnvBRDFSamp), vec2(NoV, surfaceData.roughness), 0).rg;
  463.     return radiance * (specularColor * envBRDF.x + vec3(envBRDF.y));
  464. }
  465.  
  466. layout(std140, binding = 48) uniform LightAndReflProbeParams
  467. {
  468.     uvec4 gLightOffsets;
  469.     uint  gReflProbeCount;
  470. };
  471.  
  472. vec3 calcLighting(vec3 worldPosition, vec3 screenPosition, vec2 uv, SurfaceData surfaceData)
  473. {
  474.     uvec4 lightOffsets = gLightOffsets;
  475.     uvec2 reflProbeOffsetAndSize = uvec2(0, gReflProbeCount);
  476.     vec3 V = normalize(gViewOrigin - worldPosition);
  477.     vec3 N = surfaceData.worldNormal.xyz;
  478.     vec3 R = 2,000000f * dot(V, N) * N - V;
  479.     vec3 specR = getSpecularDominantDir(N, R, surfaceData.roughness);
  480.     vec4 directLighting = getDirectLighting(worldPosition, V, specR, surfaceData, lightOffsets);
  481.     float ao = float(texture(sampler2D(gAmbientOcclusionTex, gAmbientOcclusionSamp), uv));
  482.     vec4 ssr = texture(sampler2D(gSSRTex, gSSRSamp), uv);
  483.     vec3 imageBasedSpecular = getImageBasedSpecular(worldPosition, V, specR, surfaceData, ao, ssr, reflProbeOffsetAndSize.x, reflProbeOffsetAndSize.y);
  484.     vec3 totalLighting = directLighting.rgb;
  485.     totalLighting.rgb += imageBasedSpecular;
  486.     return totalLighting;
  487. }
  488.  
  489. layout(std140, binding = 49) uniform MaterialParams
  490. {
  491.     float gOpacity;
  492.     vec3  gEmissiveColor;
  493.     vec2  gUVOffset;
  494.     vec2  gUVTile;
  495.     vec4  gTint;
  496.     float gJiggleAmount;
  497.     float gJiggleMaxAngle;
  498. };
  499.  
  500. layout(binding = 50) uniform sampler gAlbedoSamp;
  501.  
  502. layout(binding = 51) uniform sampler gNormalSamp;
  503.  
  504. layout(binding = 52) uniform sampler gRoughnessSamp;
  505.  
  506. layout(binding = 53) uniform sampler gMetalnessSamp;
  507.  
  508. layout(binding = 54) uniform sampler gEmissiveMaskSamp;
  509.  
  510. layout(binding = 55) uniform texture2D gAlbedoTex;
  511.  
  512. layout(binding = 56) uniform texture2D gNormalTex;
  513.  
  514. layout(binding = 57) uniform texture2D gRoughnessTex;
  515.  
  516. layout(binding = 58) uniform texture2D gMetalnessTex;
  517.  
  518. layout(binding = 59) uniform texture2D gEmissiveMaskTex;
  519.  
  520. void main()
  521. {
  522.     VStoFS xsr_input;
  523.     xsr_input.position        = gl_FragCoord;
  524.     xsr_input.uv0             = bs_TEXCOORD0;
  525.     xsr_input.worldPosition   = bs_TEXCOORD1;
  526.     xsr_input.tangentToWorldZ = bs_NORMAL0;
  527.     xsr_input.tangentToWorldX = bs_TANGENT0;
  528.     vec2 uv = xsr_input.uv0 * gUVTile + gUVOffset;
  529.     vec3 normal = normalize(texture(sampler2D(gNormalTex, gNormalSamp), uv).xyz * 2.0f - vec3(1, 1, 1));
  530.     vec3 worldNormal = calcWorldNormal(xsr_input, normal);
  531.     SurfaceData surfaceData;
  532.     surfaceData.albedo = texture(sampler2D(gAlbedoTex, gAlbedoSamp), uv) * gTint;
  533.     surfaceData.worldNormal.xyz = worldNormal;
  534.     surfaceData.worldNormal.w = 1.0f;
  535.     surfaceData.roughness = texture(sampler2D(gRoughnessTex, gRoughnessSamp), uv).x;
  536.     surfaceData.metalness = texture(sampler2D(gMetalnessTex, gMetalnessSamp), uv).x;
  537.     vec3 lighting = calcLighting(xsr_input.worldPosition.xyz, vec3(xsr_input.position), uv, surfaceData);
  538.     vec3 emissive = gEmissiveColor * texture(sampler2D(gEmissiveMaskTex, gEmissiveMaskSamp), uv).x;
  539.     SV_Target0 = vec4(emissive + surfaceData.albedo.rgb, surfaceData.albedo.a * gOpacity);
  540. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement