Samplers.hlsl 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Material map samplers
  2. sampler2D sDiffMap : register(S0);
  3. samplerCUBE sDiffCubeMap : register(S0);
  4. sampler2D sNormalMap : register(S1);
  5. sampler2D sSpecMap : register(S2);
  6. sampler2D sEmissiveMap : register(S3);
  7. sampler2D sDetailMap : register(S4);
  8. sampler2D sEnvMap : register(S5);
  9. samplerCUBE sEnvCubeMap : register(S5);
  10. // Shadow and light shape samplers
  11. sampler2D sShadowMap : register(S6);
  12. sampler1D sLightRampMap : register(S7);
  13. sampler2D sLightSpotMap : register(S8);
  14. samplerCUBE sLightCubeMap : register(S8);
  15. samplerCUBE sFaceSelectCubeMap : register(S9);
  16. samplerCUBE sIndirectionCubeMap : register(S10);
  17. // Deferred buffer samplers
  18. sampler2D sDepthBuffer : register(S0);
  19. sampler2D sNormalBuffer : register(S1);
  20. sampler2D sLightBuffer : register(S6);
  21. float4 Sample(sampler2D map, float2 texCoord)
  22. {
  23. // Use tex2Dlod if available to avoid divergence and allow branching
  24. #ifdef SM3
  25. return tex2Dlod(map, float4(texCoord, 0.0, 0.0));
  26. #else
  27. return tex2D(map, texCoord);
  28. #endif
  29. }
  30. float2 EncodeDepth(float depth)
  31. {
  32. depth *= 255.0;
  33. return float2(floor(depth) / 255.0, frac(depth));
  34. }
  35. float DecodeDepth(float2 depth)
  36. {
  37. const float2 dotValues = float2(1.0, 1.0 / 255.0);
  38. return dot(depth, dotValues);
  39. }
  40. float3 DecodeNormal(float4 normalInput)
  41. {
  42. float3 normal;
  43. normal.xy = normalInput.ag * 2.0 - 1.0;
  44. normal.z = sqrt(max(1.0 - dot(normal.xy, normal.xy), 0.0));
  45. return normal;
  46. }
  47. float4 PackNormalDepth(float3 normal, float depth)
  48. {
  49. float4 ret;
  50. ret.xy = normal.xz * 0.5 + 0.5;
  51. ret.z = (floor(depth * 127.0) + (normal.y < 0.0) * 128.0) * (1.0 / 255.0);
  52. ret.w = frac(depth * 127.0);
  53. return ret;
  54. }
  55. void UnpackNormalDepth(float4 input, out float3 normal, out float depth)
  56. {
  57. normal.xz = input.xy * 2.0 - 1.0;
  58. normal.y = sqrt(1.0 - dot(normal.xz, normal.xz));
  59. float hiDepth = input.z * 255.0;
  60. if (hiDepth > 127.0)
  61. {
  62. hiDepth -= 128.0;
  63. normal.y = -normal.y;
  64. }
  65. depth = (hiDepth + input.w) * (1.0 / 127.0);
  66. }
  67. float ReconstructDepth(float hwDepth)
  68. {
  69. return cDepthReconstruct.y / (hwDepth - cDepthReconstruct.x);
  70. }