Advertisement
Guest User

536936449PixelShader_ps.hlsl

a guest
Jun 25th, 2015
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.80 KB | None | 0 0
  1.  
  2. // START UNIFORM DECLARATION
  3.  
  4.  
  5. struct ShadowReceiverData
  6. {
  7. float4x4 texViewProj;
  8. float2 shadowDepthRange;
  9. float2 padding;
  10. float4 invShadowMapSize;
  11. };
  12.  
  13. struct Light
  14. {
  15. float3 position;
  16. float3 diffuse;
  17. float3 specular;
  18.  
  19. float3 attenuation;
  20. float3 spotDirection;
  21. float3 spotParams;
  22. };
  23.  
  24. //Uniforms that change per pass
  25. cbuffer PassBuffer : register(b0)
  26. {
  27. struct PassData
  28. {
  29. //Vertex shader (common to both receiver and casters)
  30. float4x4 viewProj;
  31.  
  32.  
  33. //Vertex shader
  34. float4x4 view;
  35. ShadowReceiverData shadowRcv[5];
  36. //-------------------------------------------------------------------------
  37.  
  38. //Pixel shader
  39. float3x3 invViewMatCubemap;
  40. float padding; //Compatibility with GLSL.
  41.  
  42. float pssmSplitPoints0;
  43. float pssmSplitPoints1;
  44. float pssmSplitPoints2; Light lights[3];
  45.  
  46. } passBuf;
  47. };
  48.  
  49.  
  50. //Uniforms that change per Item/Entity, but change very infrequently
  51. struct Material
  52. {
  53. /* kD is already divided by PI to make it energy conserving.
  54. (formula is finalDiffuse = NdotL * surfaceDiffuse / PI)
  55. */
  56. float4 kD; //kD.w is alpha_test_threshold
  57. float4 kS; //kS.w is roughness
  58. //Fresnel coefficient, may be per colour component (float3) or scalar (float)
  59. //F0.w is mNormalMapWeight
  60. float4 F0;
  61. float4 normalWeights;
  62. float4 cDetailWeights;
  63. float4 detailOffsetScaleD[4];
  64. float4 detailOffsetScaleN[4];
  65.  
  66. uint4 indices0_3;
  67. uint4 indices4_7;
  68. };
  69.  
  70. cbuffer MaterialBuf : register(b1)
  71. {
  72. Material materialArray[273];
  73. };
  74.  
  75.  
  76. //Uniforms that change per Item/Entity
  77. cbuffer InstanceBuffer : register(b2)
  78. {
  79. //.x =
  80. //The lower 9 bits contain the material's start index.
  81. //The higher 23 bits contain the world matrix start index.
  82. //
  83. //.y =
  84. //shadowConstantBias. Send the bias directly to avoid an
  85. //unnecessary indirection during the shadow mapping pass.
  86. //Must be loaded with uintBitsToFloat
  87. uint4 worldMaterialIdx[4096];
  88. };
  89.  
  90.  
  91.  
  92. // END UNIFORM DECLARATION
  93. struct PS_INPUT
  94. {
  95.  
  96.  
  97. nointerpolation uint drawId : TEXCOORD0;
  98.  
  99. float3 pos : TEXCOORD1;
  100. float3 normal : TEXCOORD2;
  101.  
  102. float2 uv0 : TEXCOORD3;
  103.  
  104. float4 posL0 : TEXCOORD4;
  105. float4 posL1 : TEXCOORD5;
  106. float4 posL2 : TEXCOORD6;
  107. float4 posL3 : TEXCOORD7;
  108. float4 posL4 : TEXCOORD8; float depth : TEXCOORD9;
  109.  
  110. };
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117. Texture2DArray textureMaps[2] : register(t6);
  118.  
  119.  
  120. SamplerState samplerStates[2] : register(s6);
  121.  
  122.  
  123.  
  124.  
  125. Texture2D texShadowMap[5] : register(t1);
  126. SamplerComparisonState shadowSampler : register(s1);
  127.  
  128. float getShadow( Texture2D shadowMap, float4 psPosLN, float4 invShadowMapSize )
  129. {
  130. float fDepth = psPosLN.z;
  131. float2 uv = psPosLN.xy / psPosLN.w;
  132. /*float c = shadowMap.SampleCmpLevelZero( shadowSampler, uv.xy, fDepth );
  133. return c;*/
  134.  
  135. float retVal = 0;
  136.  
  137.  
  138. float2 offsets[4] =
  139. {
  140.  
  141. float2( 0, 0 ), //0, 0
  142. float2( 1, 0 ), //1, 0
  143. float2( 0, 1 ), //1, 1
  144. float2( 0, 0 ) //1, 1
  145.  
  146.  
  147. };
  148.  
  149.  
  150.  
  151. uv += offsets[0] * invShadowMapSize.xy;
  152. // 2x2 PCF
  153. retVal += shadowMap.SampleCmpLevelZero( shadowSampler, uv.xy, fDepth );
  154.  
  155. uv += offsets[1] * invShadowMapSize.xy;
  156. // 2x2 PCF
  157. retVal += shadowMap.SampleCmpLevelZero( shadowSampler, uv.xy, fDepth );
  158.  
  159. uv += offsets[2] * invShadowMapSize.xy;
  160. // 2x2 PCF
  161. retVal += shadowMap.SampleCmpLevelZero( shadowSampler, uv.xy, fDepth );
  162.  
  163. uv += offsets[3] * invShadowMapSize.xy;
  164. // 2x2 PCF
  165. retVal += shadowMap.SampleCmpLevelZero( shadowSampler, uv.xy, fDepth );
  166.  
  167.  
  168.  
  169. retVal *= 0.25;
  170.  
  171.  
  172. return retVal;
  173. }
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183. //Default BRDF
  184. float3 BRDF( float3 lightDir, float3 viewDir, float NdotV, float3 lightDiffuse, float3 lightSpecular, Material material, float3 nNormal , float3 diffuseCol, float ROUGHNESS )
  185. {
  186. float3 halfWay= normalize( lightDir + viewDir );
  187. float NdotL = saturate( dot( nNormal, lightDir ) );
  188. float NdotH = saturate( dot( nNormal, halfWay ) );
  189. float VdotH = saturate( dot( viewDir, halfWay ) );
  190.  
  191. float sqR = ROUGHNESS * ROUGHNESS * ROUGHNESS * 0.2;
  192.  
  193. //Roughness/Distribution/NDF term (GGX)
  194. //Formula:
  195. // Where alpha = roughness
  196. // R = alpha^2 / [ PI * [ ( NdotH^2 * (alpha^2 - 1) ) + 1 ]^2 ]
  197. float f = ( NdotH * sqR - NdotH ) * NdotH + 1.0;
  198. float R = sqR / (f * f + 1e-6f);
  199.  
  200. //Geometric/Visibility term (Smith GGX Height-Correlated)
  201.  
  202. float Lambda_GGXV = NdotL * sqrt( (-NdotV * sqR + NdotV) * NdotV + sqR );
  203. float Lambda_GGXL = NdotV * sqrt( (-NdotL * sqR + NdotL) * NdotL + sqR );
  204.  
  205. float G = 0.5 / (( Lambda_GGXV + Lambda_GGXL + 1e-6f ) * 3.141592654);
  206.  
  207.  
  208. //Formula:
  209. // fresnelS = lerp( (1 - V*H)^5, 1, F0 )
  210. float3 fresnelS = material.kS.xyz.xyz + pow( 1.0 - VdotH, 5.0 ) * (1.0 - material.kS.xyz.xyz);
  211.  
  212. //We should divide Rs by PI, but it was done inside G for performance
  213. float3 Rs = ( fresnelS * (R * G) ) * float3(1,1,1) * lightSpecular;
  214.  
  215. //Diffuse BRDF (*Normalized* Disney, see course_notes_moving_frostbite_to_pbr.pdf
  216. //"Moving Frostbite to Physically Based Rendering" Sebastien Lagarde & Charles de Rousiers)
  217. float energyBias = ROUGHNESS * 0.5;
  218. float energyFactor = lerp( 1.0, 1.0 / 1.51, ROUGHNESS );
  219. float fd90 = energyBias + 2.0 * VdotH * VdotH * ROUGHNESS;
  220. float lightScatter = 1.0 + (fd90 - 1.0) * pow( 1.0 - NdotL, 5.0 );
  221. float viewScatter = 1.0 + (fd90 - 1.0) * pow( 1.0 - NdotV, 5.0 );
  222.  
  223.  
  224. float3 fresnelD = 1.0 - fresnelS;
  225.  
  226. //We should divide Rd by PI, but it is already included in kD
  227. float3 Rd = (lightScatter * viewScatter * fresnelD) * diffuseCol.xyz * lightDiffuse;
  228.  
  229. return NdotL * (Rs + Rd);
  230. }
  231.  
  232.  
  233.  
  234.  
  235.  
  236. float4 main( PS_INPUT inPs
  237. ) : SV_Target0
  238. {
  239.  
  240.  
  241. Material material;
  242. float4 outColour;
  243.  
  244.  
  245.  
  246.  
  247. uint roughnessIdx;
  248.  
  249.  
  250. uint detailMapIdx0;
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261. float4 diffuseCol;
  262.  
  263. float ROUGHNESS;
  264.  
  265. float3 nNormal;
  266.  
  267.  
  268. uint materialId = worldMaterialIdx[inPs.drawId].x & 0x1FFu;
  269. material = materialArray[materialId];
  270.  
  271.  
  272.  
  273. roughnessIdx = material.indices0_3.y >> 16u;
  274.  
  275. detailMapIdx0 = material.indices0_3.z >> 16u;
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290. float4 detailWeights = float4( 1.0, 1.0, 1.0, 1.0 );
  291.  
  292.  
  293.  
  294.  
  295. float4 detailCol0 = textureMaps[1].Sample( samplerStates[1], float3( inPs.uv0.xy * material.detailOffsetScaleD[0].zw + material.detailOffsetScaleD[0].xy, detailMapIdx0 ) );
  296.  
  297. detailWeights.x *= detailCol0.w;
  298. detailCol0.w = detailWeights.x;
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307. nNormal = normalize( inPs.normal );
  308.  
  309.  
  310.  
  311. float fShadow = 1.0;
  312. if( inPs.depth <= passBuf.pssmSplitPoints0 )
  313. fShadow = getShadow( texShadowMap[0], inPs.posL0, passBuf.shadowRcv[0].invShadowMapSize );
  314. else if( inPs.depth <= passBuf.pssmSplitPoints1 )
  315. fShadow = getShadow( texShadowMap[1], inPs.posL1, passBuf.shadowRcv[1].invShadowMapSize );
  316. else if( inPs.depth <= passBuf.pssmSplitPoints2 )
  317. fShadow = getShadow( texShadowMap[2], inPs.posL2, passBuf.shadowRcv[2].invShadowMapSize );
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325. ROUGHNESS = material.kS.w * textureMaps[0].Sample( samplerStates[0], float3(inPs.uv0.xy, roughnessIdx) ).x;
  326. ROUGHNESS = max( ROUGHNESS, 0.001f );
  327.  
  328. diffuseCol = float4( 0.0, 0.0, 0.0, 0.0 );
  329.  
  330.  
  331.  
  332. //Normal Non Premultiplied 0
  333. diffuseCol.xyz = lerp( diffuseCol.xyz, detailCol0.xyz, detailCol0.a );
  334.  
  335.  
  336.  
  337.  
  338.  
  339. diffuseCol.xyz *= material.kD.xyz;
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351. //Everything's in Camera space
  352.  
  353. float3 viewDir = normalize( -inPs.pos );
  354. float NdotV = saturate( dot( nNormal, viewDir ) );
  355.  
  356. float3 finalColour = float3(0, 0, 0);
  357.  
  358.  
  359.  
  360.  
  361.  
  362. finalColour += BRDF( passBuf.lights[0].position, viewDir, NdotV, passBuf.lights[0].diffuse, passBuf.lights[0].specular, material, nNormal , diffuseCol.xyz, ROUGHNESS );
  363. finalColour *= fShadow; //1st directional light's shadow
  364.  
  365.  
  366.  
  367.  
  368.  
  369. float3 lightDir;
  370. float fDistance;
  371. float3 tmpColour;
  372. float spotCosAngle;
  373.  
  374. //Point lights
  375.  
  376.  
  377. //Spot lights
  378. //spotParams[0].x = 1.0 / cos( InnerAngle ) - cos( OuterAngle )
  379. //spotParams[0].y = cos( OuterAngle / 2 )
  380. //spotParams[0].z = falloff
  381.  
  382. lightDir = passBuf.lights[1].position - inPs.pos;
  383. fDistance= length( lightDir );
  384. spotCosAngle = dot( normalize( inPs.pos - passBuf.lights[1].position ), passBuf.lights[1].spotDirection );
  385.  
  386. if( fDistance <= passBuf.lights[1].attenuation.x && spotCosAngle >= passBuf.lights[1].spotParams.y )
  387. {
  388. lightDir *= 1.0 / fDistance;
  389.  
  390.  
  391. float spotAtten = saturate( (spotCosAngle - passBuf.lights[1].spotParams.y) * passBuf.lights[1].spotParams.x );
  392. spotAtten = pow( spotAtten, passBuf.lights[1].spotParams.z );
  393.  
  394. tmpColour = BRDF( lightDir, viewDir, NdotV, passBuf.lights[1].diffuse, passBuf.lights[1].specular, material, nNormal , diffuseCol.xyz, ROUGHNESS ) * getShadow( texShadowMap[3], inPs.posL3, passBuf.shadowRcv[3].invShadowMapSize );
  395. float atten = 1.0 / (1.0 + (passBuf.lights[1].attenuation.y + passBuf.lights[1].attenuation.z * fDistance) * fDistance );
  396. finalColour += tmpColour * (atten * spotAtten);
  397. }
  398. lightDir = passBuf.lights[2].position - inPs.pos;
  399. fDistance= length( lightDir );
  400. spotCosAngle = dot( normalize( inPs.pos - passBuf.lights[2].position ), passBuf.lights[2].spotDirection );
  401.  
  402. if( fDistance <= passBuf.lights[2].attenuation.x && spotCosAngle >= passBuf.lights[2].spotParams.y )
  403. {
  404. lightDir *= 1.0 / fDistance;
  405.  
  406.  
  407. float spotAtten = saturate( (spotCosAngle - passBuf.lights[2].spotParams.y) * passBuf.lights[2].spotParams.x );
  408. spotAtten = pow( spotAtten, passBuf.lights[2].spotParams.z );
  409.  
  410. tmpColour = BRDF( lightDir, viewDir, NdotV, passBuf.lights[2].diffuse, passBuf.lights[2].specular, material, nNormal , diffuseCol.xyz, ROUGHNESS ) * getShadow( texShadowMap[4], inPs.posL4, passBuf.shadowRcv[4].invShadowMapSize );
  411. float atten = 1.0 / (1.0 + (passBuf.lights[2].attenuation.y + passBuf.lights[2].attenuation.z * fDistance) * fDistance );
  412. finalColour += tmpColour * (atten * spotAtten);
  413. }
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420. outColour.xyz = finalColour;
  421.  
  422. //outColour.w = 1.0;
  423.  
  424.  
  425. outColour.w = material.kD.w;
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432. return outColour;
  433. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement