POM_VS.hlsl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // RUN: %dxc -E main -T vs_6_0 %s | FileCheck %s
  2. // CHECK: Sqrt
  3. // CHECK: storeOutput
  4. //--------------------------------------------------------------------------------------
  5. // File: POM.hlsl
  6. //
  7. // HLSL file containing shader functions for Parallax Occlusion Mapping.
  8. //
  9. // Copyright (c) Microsoft Corporation. All rights reserved.
  10. //--------------------------------------------------------------------------------------
  11. #include "shader_include.hlsli"
  12. //--------------------------------------------------------------------------------------
  13. // Structures
  14. //--------------------------------------------------------------------------------------
  15. struct VS_INPUT
  16. {
  17. float3 inPositionOS : POSITION;
  18. float2 inTexCoord : TEXCOORD0;
  19. float3 vInNormalOS : NORMAL;
  20. float3 vInBinormalOS : BINORMAL;
  21. float3 vInTangentOS : TANGENT;
  22. };
  23. struct VS_OUTPUT
  24. {
  25. float2 texCoord : TEXCOORD0;
  26. float3 vLightTS : TEXCOORD1; // Light vector in tangent space, denormalized
  27. float3 vViewTS : TEXCOORD2; // View vector in tangent space, denormalized
  28. float2 vParallaxOffsetTS : TEXCOORD3; // Parallax offset vector in tangent space
  29. float3 vNormalWS : TEXCOORD4; // Normal vector in world space
  30. float3 vViewWS : TEXCOORD5; // View vector in world space
  31. float4 position : SV_POSITION; // Output position
  32. };
  33. struct PS_INPUT
  34. {
  35. float2 texCoord : TEXCOORD0;
  36. float3 vLightTS : TEXCOORD1; // Light vector in tangent space, denormalized
  37. float3 vViewTS : TEXCOORD2; // View vector in tangent space, denormalized
  38. float2 vParallaxOffsetTS : TEXCOORD3; // Parallax offset vector in tangent space
  39. float3 vNormalWS : TEXCOORD4; // Normal vector in world space
  40. float3 vViewWS : TEXCOORD5; // View vector in world space
  41. };
  42. //--------------------------------------------------------------------------------------
  43. // Vertex shader for POM setup
  44. //--------------------------------------------------------------------------------------
  45. VS_OUTPUT main( VS_INPUT i )
  46. {
  47. VS_OUTPUT Out;
  48. // Transform and output input position
  49. Out.position = mul( float4( i.inPositionOS.xyz, 1.0 ), g_mWorldViewProjection );
  50. // Propagate texture coordinate through:
  51. Out.texCoord = i.inTexCoord * g_fBaseTextureRepeat.x;
  52. // Transform the normal, tangent and binormal vectors
  53. // from object space to homogeneous projection space:
  54. float3 vNormalWS = mul( i.vInNormalOS, (float3x3) g_mWorld );
  55. float3 vBinormalWS = mul( i.vInBinormalOS, (float3x3) g_mWorld );
  56. float3 vTangentWS = mul( i.vInTangentOS, (float3x3) g_mWorld );
  57. // Propagate the world space vertex normal through:
  58. Out.vNormalWS = vNormalWS;
  59. // Normalize tangent space vectors
  60. vNormalWS = normalize( vNormalWS );
  61. vBinormalWS = normalize( vBinormalWS );
  62. vTangentWS = normalize( vTangentWS );
  63. // Compute position in world space:
  64. float4 vPositionWS = mul( i.inPositionOS, g_mWorld );
  65. // Compute and output the world view vector (unnormalized):
  66. float3 vViewWS = g_vEye - vPositionWS;
  67. Out.vViewWS = vViewWS;
  68. // Compute denormalized light vector in world space:
  69. float3 vLightWS = g_LightPosition.xyz - vPositionWS.xyz;
  70. // Need to invert Z for correct lighting
  71. vLightWS.z = -vLightWS.z;
  72. // Normalize the light and view vectors and transform it to the tangent space:
  73. float3x3 mWorldToTangent = float3x3( vTangentWS, vBinormalWS, vNormalWS );
  74. // Propagate the view and the light vectors (in tangent space):
  75. Out.vLightTS = mul( mWorldToTangent, vLightWS );
  76. Out.vViewTS = mul( mWorldToTangent, vViewWS );
  77. // Compute the ray direction for intersecting the
  78. // height field profile with current view ray
  79. // Compute initial parallax displacement direction:
  80. float2 vParallaxDirection = normalize( Out.vViewTS.xy );
  81. // The length of this vector determines the furthest amount of displacement:
  82. float fLength = length( Out.vViewTS );
  83. float fParallaxLength = sqrt( fLength * fLength - Out.vViewTS.z * Out.vViewTS.z ) / Out.vViewTS.z;
  84. // Compute the actual reverse parallax displacement vector:
  85. Out.vParallaxOffsetTS = vParallaxDirection * fParallaxLength;
  86. // Need to scale the amount of displacement to account for different height ranges in height maps.
  87. Out.vParallaxOffsetTS *= g_fPOMHeightMapScale.x;
  88. return Out;
  89. }