Common.hlsl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #pragma warning(disable:3571)
  2. // Vertex shader parameters
  3. uniform float3 cCameraPos : register(C0);
  4. uniform float3x3 cCameraRot : register(C1);
  5. uniform float4 cDepthMode : register(C4);
  6. uniform float2 cElapsedTime : register(C5);
  7. uniform float4 cFrustumSize : register(C6);
  8. uniform float4 cGBufferOffsets : register(C7);
  9. uniform float4x3 cModel : register(C8);
  10. uniform float4x4 cShadowProj : register(C11);
  11. uniform float4x4 cSpotProj : register(C15);
  12. uniform float4x4 cViewProj : register(C19);
  13. uniform float4 cUOffset : register(C23);
  14. uniform float4 cVOffset : register(C24);
  15. uniform float3 cViewRightVector : register(C25);
  16. uniform float3 cViewUpVector : register(C26);
  17. uniform float4x3 cSkinMatrices[64] : register(C27);
  18. // Pixel shader parameters
  19. uniform float3 cAmbientColor : register(C0);
  20. uniform float4 cAntiAliasWeights : register(C1);
  21. uniform float3 cCameraPosPS : register(C2);
  22. uniform float2 cElapsedTimePS : register(C3);
  23. uniform float4 cFogParams : register(C4);
  24. uniform float3 cFogColor : register(C5);
  25. uniform float4 cGBufferOffsetsPS : register(C6);
  26. uniform float4 cGBufferViewport : register(C7);
  27. uniform float cLightAtten : register(C8);
  28. uniform float4 cLightColor : register(C9);
  29. uniform float3 cLightDir : register(C10);
  30. uniform float3 cLightPos : register(C11);
  31. uniform float4 cLightSplits : register(C12);
  32. uniform float3x3 cLightVecRot: register(C13);
  33. uniform float4 cMatDiffColor : register(C16);
  34. uniform float3 cMatEmissiveColor : register(C17);
  35. uniform float2 cMatSpecProperties : register(C18);
  36. uniform float4 cSampleOffsets : register(C19);
  37. uniform float2 cShadowIntensity : register(C20);
  38. uniform float4x4 cShadowProjPS : register(C21);
  39. uniform float4x4 cSpotProjPS : register(C25);
  40. uniform float4x4 cViewProjPS : register(C25);
  41. // Material map samplers
  42. sampler2D sDiffMap : register(S0);
  43. samplerCUBE sDiffCubeMap : register(S0);
  44. sampler2D sNormalMap : register(S1);
  45. sampler2D sSpecMap : register(S2);
  46. sampler2D sDetailMap : register(S3);
  47. sampler2D sEnvMap : register(S4);
  48. samplerCUBE sEnvCubeMap : register(S4);
  49. sampler2D sEmissiveMap : register(S5);
  50. // Shadow and light shape samplers
  51. sampler2D sShadowMap : register(S5);
  52. sampler1D sLightRampMap : register(S6);
  53. sampler2D sLightSpotMap : register(S7);
  54. samplerCUBE sLightCubeMap : register(S7);
  55. // Rendertarget samplers
  56. sampler2D sDiffBuffer : register(S0);
  57. sampler2D sNormalBuffer : register(S1);
  58. sampler2D sDepthBuffer : register(S2);
  59. sampler2D sLightBuffer : register(S6);
  60. float4 Sample(sampler2D map, float2 texCoord)
  61. {
  62. // Use tex2Dlod if available to avoid divergence and allow branching
  63. #ifdef SM3
  64. return tex2Dlod(map, float4(texCoord, 0.0, 0.0));
  65. #else
  66. return tex2D(map, texCoord);
  67. #endif
  68. }
  69. float GetDepth(float4 clipPos)
  70. {
  71. return dot(clipPos.zw, cDepthMode.zw);
  72. }
  73. float4 GetScreenPos(float4 clipPos)
  74. {
  75. return float4(
  76. clipPos.x * cGBufferOffsets.z + cGBufferOffsets.x * clipPos.w,
  77. -clipPos.y * cGBufferOffsets.w + cGBufferOffsets.y * clipPos.w,
  78. 0.0,
  79. clipPos.w);
  80. }
  81. float2 GetScreenPosPreDiv(float4 clipPos)
  82. {
  83. return float2(
  84. clipPos.x / clipPos.w * cGBufferOffsets.z + cGBufferOffsets.x,
  85. -clipPos.y / clipPos.w * cGBufferOffsets.w + cGBufferOffsets.y);
  86. }
  87. float3 GetFarRay(float4 clipPos)
  88. {
  89. float3 viewRay = float3(
  90. clipPos.x / clipPos.w * cFrustumSize.x,
  91. clipPos.y / clipPos.w * cFrustumSize.y,
  92. cFrustumSize.z);
  93. return mul(viewRay, cCameraRot);
  94. }
  95. float3 GetNearRay(float4 clipPos)
  96. {
  97. float3 viewRay = float3(
  98. clipPos.x / clipPos.w * cFrustumSize.x,
  99. clipPos.y / clipPos.w * cFrustumSize.y,
  100. 0.0);
  101. return mul(viewRay, cCameraRot) * cDepthMode.z;
  102. }
  103. float GetIntensity(float3 color)
  104. {
  105. return dot(color, float3(0.333, 0.333, 0.333));
  106. }
  107. float3 UnpackNormal(float4 normalInput)
  108. {
  109. float3 normal;
  110. normal.xy = normalInput.ag * 2.0 - 1.0;
  111. normal.z = sqrt(1.0 - dot(normal.xy, normal.xy));
  112. return normal;
  113. }
  114. float4 GetPosition(float4 iPos, out float4 oPos)
  115. {
  116. float4 worldPos = float4(mul(iPos, cModel), 1.0);
  117. oPos = mul(worldPos, cViewProj);
  118. return worldPos;
  119. }
  120. float4 GetPositionBillboard(float4 iPos, float2 iSize, out float4 oPos)
  121. {
  122. float4 worldPos = float4(iPos.xyz + iSize.x * cViewRightVector + iSize.y * cViewUpVector, 1.0);
  123. oPos = mul(worldPos, cViewProj);
  124. return worldPos;
  125. }
  126. float4 GetPositionSkinned(float4 iPos, float4 iBlendWeights, int4 iBlendIndices, out float4 oPos)
  127. {
  128. float4x3 skinMatrix =
  129. cSkinMatrices[iBlendIndices.x] * iBlendWeights.x +
  130. cSkinMatrices[iBlendIndices.y] * iBlendWeights.y +
  131. cSkinMatrices[iBlendIndices.z] * iBlendWeights.z +
  132. cSkinMatrices[iBlendIndices.w] * iBlendWeights.w;
  133. float4 worldPos = float4(mul(iPos, skinMatrix), 1.0);
  134. oPos = mul(worldPos, cViewProj);
  135. return worldPos;
  136. }
  137. float4 GetPositionInstanced(float4 iPos, float4x3 iModel, out float4 oPos)
  138. {
  139. float4 worldPos = float4(mul(iPos, iModel), 1.0);
  140. oPos = mul(worldPos, cViewProj);
  141. return worldPos;
  142. }
  143. float4 GetPositionNormal(float4 iPos, float3 iNormal, out float4 oPos, out float3 oNormal)
  144. {
  145. float4 worldPos = float4(mul(iPos, cModel), 1.0);
  146. oPos = mul(worldPos, cViewProj);
  147. oNormal = normalize(mul(iNormal, (float3x3)cModel));
  148. return worldPos;
  149. }
  150. float4 GetPositionNormalSkinned(float4 iPos, float3 iNormal, float4 iBlendWeights, int4 iBlendIndices, out float4 oPos, out float3 oNormal)
  151. {
  152. float4x3 skinMatrix =
  153. cSkinMatrices[iBlendIndices.x] * iBlendWeights.x +
  154. cSkinMatrices[iBlendIndices.y] * iBlendWeights.y +
  155. cSkinMatrices[iBlendIndices.z] * iBlendWeights.z +
  156. cSkinMatrices[iBlendIndices.w] * iBlendWeights.w;
  157. float4 worldPos = float4(mul(iPos, skinMatrix), 1.0);
  158. oPos = mul(worldPos, cViewProj);
  159. oNormal = normalize(mul(iNormal, (float3x3)skinMatrix));
  160. return worldPos;
  161. }
  162. float4 GetPositionNormalInstanced(float4 iPos, float3 iNormal, float4x3 iModel, out float4 oPos, out float3 oNormal)
  163. {
  164. float4 worldPos = float4(mul(iPos, iModel), 1.0);
  165. oPos = mul(worldPos, cViewProj);
  166. oNormal = normalize(mul(iNormal, (float3x3)iModel));
  167. return worldPos;
  168. }
  169. float4 GetPositionNormalTangent(float4 iPos, float3 iNormal, float4 iTangent, out float4 oPos, out float3 oNormal, out float3 oTangent)
  170. {
  171. float4 worldPos = float4(mul(iPos, cModel), 1.0);
  172. oPos = mul(worldPos, cViewProj);
  173. oNormal = normalize(mul(iNormal, (float3x3)cModel));
  174. oTangent = normalize(mul(iTangent.xyz, (float3x3)cModel));
  175. return worldPos;
  176. }
  177. float4 GetPositionNormalTangentSkinned(float4 iPos, float3 iNormal, float4 iTangent, float4 iBlendWeights, int4 iBlendIndices, out float4 oPos, out float3 oNormal, out float3 oTangent)
  178. {
  179. float4x3 skinMatrix =
  180. cSkinMatrices[iBlendIndices.x] * iBlendWeights.x +
  181. cSkinMatrices[iBlendIndices.y] * iBlendWeights.y +
  182. cSkinMatrices[iBlendIndices.z] * iBlendWeights.z +
  183. cSkinMatrices[iBlendIndices.w] * iBlendWeights.w;
  184. float4 worldPos = float4(mul(iPos, skinMatrix), 1.0);
  185. oPos = mul(worldPos, cViewProj);
  186. oNormal = normalize(mul(iNormal, (float3x3)skinMatrix));
  187. oTangent = normalize(mul(iTangent.xyz, (float3x3)skinMatrix));
  188. return worldPos;
  189. }
  190. float4 GetPositionNormalTangentInstanced(float4 iPos, float3 iNormal, float4 iTangent, float4x3 iModel, out float4 oPos, out float3 oNormal, out float3 oTangent)
  191. {
  192. float4 worldPos = float4(mul(iPos, iModel), 1.0);
  193. oPos = mul(worldPos, cViewProj);
  194. oNormal = normalize(mul(iNormal, (float3x3)iModel));
  195. oTangent = normalize(mul(iTangent.xyz, (float3x3)iModel));
  196. return worldPos;
  197. }
  198. float2 GetTexCoord(float2 iTexCoord)
  199. {
  200. return float2(dot(iTexCoord, cUOffset.xy) + cUOffset.w, dot(iTexCoord, cVOffset.xy) + cVOffset.w);
  201. };
  202. float GetDiffuseDir(float3 normal, out float3 lightDir)
  203. {
  204. lightDir = cLightDir;
  205. float NdotL = max(dot(normal, lightDir), 0.0);
  206. return NdotL;
  207. }
  208. float GetDiffusePointOrSpot(float3 normal, float3 worldPos, out float3 lightDir, out float3 lightVec)
  209. {
  210. lightVec = (cLightPos - worldPos) * cLightAtten.x;
  211. float lightDist = length(lightVec);
  212. lightDir = lightVec / lightDist;
  213. float NdotL = max(dot(normal, lightDir), 0.0);
  214. return NdotL * tex1D(sLightRampMap, lightDist).r;
  215. }
  216. float GetDiffuseDirVolumetric()
  217. {
  218. return 1.0;
  219. }
  220. float GetDiffusePointOrSpotVolumetric(float3 worldPos, out float3 lightVec)
  221. {
  222. lightVec = (cLightPos - worldPos) * cLightAtten.x;
  223. float lightDist = length(lightVec);
  224. return tex1D(sLightRampMap, lightDist).r;
  225. }
  226. float GetSplitFade(float depth)
  227. {
  228. float nearFadeFactor = saturate((depth - cLightSplits.x) * cLightSplits.y);
  229. float farFadeFactor = 1.0 - saturate((depth - cLightSplits.z) * cLightSplits.w);
  230. return nearFadeFactor * farFadeFactor;
  231. }
  232. float GetSpecular(float3 normal, float3 worldPos, float3 lightDir, float specularPower)
  233. {
  234. float3 eyeDir = normalize(-worldPos);
  235. float3 halfDir = normalize(eyeDir + lightDir);
  236. return pow(dot(normal, halfDir), specularPower);
  237. }
  238. float GetShadow(float4 shadowPos)
  239. {
  240. // Take four samples and average them
  241. float4 pcfValues = cShadowIntensity.x;
  242. #ifdef SM3
  243. float2 ofs = cSampleOffsets.xy;
  244. float4 projShadowPos = float4(shadowPos.xyz / shadowPos.w, 0.0);
  245. float4 inLight = float4(
  246. tex2Dlod(sShadowMap, float4(projShadowPos.xy + float2(ofs.x, ofs.x), projShadowPos.zw)).r,
  247. tex2Dlod(sShadowMap, float4(projShadowPos.xy + float2(ofs.y, ofs.x), projShadowPos.zw)).r,
  248. tex2Dlod(sShadowMap, float4(projShadowPos.xy + float2(ofs.x, ofs.y), projShadowPos.zw)).r,
  249. tex2Dlod(sShadowMap, float4(projShadowPos.xy + float2(ofs.y, ofs.y), projShadowPos.zw)).r
  250. );
  251. #ifdef HWSHADOW
  252. return cShadowIntensity.y + dot(inLight, pcfValues);
  253. #else
  254. return cShadowIntensity.y + dot(inLight > projShadowPos.z, pcfValues);
  255. #endif
  256. #else
  257. float2 projOfs = cSampleOffsets.xy * shadowPos.w;
  258. float4 inLight = float4(
  259. tex2Dproj(sShadowMap, float4(shadowPos.xy + float2(projOfs.x, projOfs.x), shadowPos.zw)).r,
  260. tex2Dproj(sShadowMap, float4(shadowPos.xy + float2(projOfs.y, projOfs.x), shadowPos.zw)).r,
  261. tex2Dproj(sShadowMap, float4(shadowPos.xy + float2(projOfs.x, projOfs.y), shadowPos.zw)).r,
  262. tex2Dproj(sShadowMap, float4(shadowPos.xy + float2(projOfs.y, projOfs.y), shadowPos.zw)).r
  263. );
  264. #ifdef HWSHADOW
  265. return cShadowIntensity.y + dot(inLight, pcfValues);
  266. #else
  267. return cShadowIntensity.y + dot((inLight * shadowPos.w) > shadowPos.z, pcfValues);
  268. #endif
  269. #endif
  270. }
  271. float3 GetFog(float3 color, float depth)
  272. {
  273. return lerp(color, cFogColor, saturate((depth - cFogParams.x) * cFogParams.z));
  274. }
  275. float3 GetLitFog(float3 color, float depth)
  276. {
  277. return color * saturate((cFogParams.y - depth) * cFogParams.z);
  278. }
  279. float GetFogFactor(float depth)
  280. {
  281. return saturate((depth - cFogParams.x) * cFogParams.z);
  282. }
  283. float GetReverseFogFactor(float depth)
  284. {
  285. return saturate((cFogParams.y - depth) * cFogParams.z);
  286. }