SubD11_SmoothPS.hlsl 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // RUN: %dxc -E main -T ps_6_0 %s | FileCheck %s
  2. // RUN: %dxc -E main -T ps_6_0 %s | %D3DReflect %s | FileCheck -check-prefix=REFL %s
  3. // CHECK: sample
  4. // CHECK: dot3
  5. // CHECK: dot3
  6. // CHECK: dot3
  7. // CHECK: Rsqrt
  8. // CHECK: sample
  9. // CHECK: sample
  10. // CHECK: Rsqrt
  11. // CHECK: dot3
  12. // CHECK: Saturate
  13. // CHECK: storeOutput
  14. //--------------------------------------------------------------------------------------
  15. // File: SubD11.hlsl
  16. //
  17. // This file contains functions to convert from a Catmull-Clark subdivision
  18. // representation to a bicubic patch representation.
  19. //
  20. // Copyright (c) Microsoft Corporation. All rights reserved.
  21. //--------------------------------------------------------------------------------------
  22. //--------------------------------------------------------------------------------------
  23. // A sample extraordinary SubD quad is represented by the following diagram:
  24. //
  25. // 15 Valences:
  26. // / \ Vertex 0: 5
  27. // / 14 Vertex 1: 4
  28. // 17---------16 / \ Vertex 2: 5
  29. // | \ | / \ Vertex 3: 3
  30. // | \ | / 13
  31. // | \ |/ / Prefixes:
  32. // | 3------2------12 Vertex 0: 9
  33. // | | | | Vertex 1: 12
  34. // | | | | Vertex 2: 16
  35. // 4----0------1------11 Vertex 3: 18
  36. // / /| | |
  37. // / / | | |
  38. // 5 / 8------9------10
  39. // \ / /
  40. // 6 /
  41. // \ /
  42. // 7
  43. //
  44. // Where the quad bounded by vertices 0,1,2,3 represents the actual subd surface of interest
  45. // The 1-ring neighborhood of the quad is represented by vertices 4 through 17. The counter-
  46. // clockwise winding of this 1-ring neighborhood is important, especially when it comes to compute
  47. // the corner vertices of the bicubic patch that we will use to approximate the subd quad (0,1,2,3).
  48. //
  49. // The resulting bicubic patch fits within the subd quad (0,1,2,3) and has the following control
  50. // point layout:
  51. //
  52. // 12--13--14--15
  53. // 8---9--10--11
  54. // 4---5---6---7
  55. // 0---1---2---3
  56. //
  57. // The inner 4 control points of the bicubic patch are a combination of only the vertices (0,1,2,3)
  58. // of the subd quad. However, the corner control points for the bicubic patch (0,3,15,12) are actually
  59. // a much more complex weighting of the subd patch and the 1-ring neighborhood. In the example above
  60. // the bicubic control point 0 is actually a weighted combination of subd points 0,1,2,3 and 1-ring
  61. // neighborhood points 17, 4, 5, 6, 7, 8, and 9. We can see that the 1-ring neighbor hood is simply
  62. // walked from the prefix value from the previous corner (corner 3 in this case) to the prefix
  63. // prefix value for the current corner. We add one more vertex on either side of the prefix values
  64. // and we have all the data necessary to calculate the value for the corner points.
  65. //
  66. // The edge control points of the bicubic patch (1,2,13,14,4,8,7,11) are also combinations of their
  67. // neighbors, but fortunately each one is only a combination of 6 values and no walk is required.
  68. //--------------------------------------------------------------------------------------
  69. #define MOD4(x) ((x)&3)
  70. #ifndef MAX_POINTS
  71. #define MAX_POINTS 32
  72. #endif
  73. #define MAX_BONE_MATRICES 80
  74. //--------------------------------------------------------------------------------------
  75. // Textures
  76. //--------------------------------------------------------------------------------------
  77. Texture2D g_txHeight : register( t0 ); // Height and Bump texture
  78. Texture2D g_txDiffuse : register( t1 ); // Diffuse texture
  79. Texture2D g_txSpecular : register( t2 ); // Specular texture
  80. //--------------------------------------------------------------------------------------
  81. // Samplers
  82. //--------------------------------------------------------------------------------------
  83. SamplerState g_samLinear : register( s0 );
  84. SamplerState g_samPoint : register( s0 );
  85. //--------------------------------------------------------------------------------------
  86. // Constant Buffers
  87. //--------------------------------------------------------------------------------------
  88. cbuffer cbTangentStencilConstants : register( b0 )
  89. {
  90. float g_TanM[1024]; // Tangent patch stencils precomputed by the application
  91. float g_fCi[16]; // Valence coefficients precomputed by the application
  92. };
  93. cbuffer cbPerMesh : register( b1 )
  94. {
  95. matrix g_mConstBoneWorld[MAX_BONE_MATRICES];
  96. };
  97. cbuffer cbPerFrame : register( b2 )
  98. {
  99. matrix g_mViewProjection;
  100. float3 g_vCameraPosWorld;
  101. float g_fTessellationFactor;
  102. float g_fDisplacementHeight;
  103. float3 g_vSolidColor;
  104. };
  105. cbuffer cbPerSubset : register( b3 )
  106. {
  107. int g_iPatchStartIndex;
  108. }
  109. //--------------------------------------------------------------------------------------
  110. Buffer<uint4> g_ValencePrefixBuffer : register( t0 );
  111. //--------------------------------------------------------------------------------------
  112. struct VS_CONTROL_POINT_OUTPUT
  113. {
  114. float3 vPosition : WORLDPOS;
  115. float2 vUV : TEXCOORD0;
  116. float3 vTangent : TANGENT;
  117. };
  118. struct BEZIER_CONTROL_POINT
  119. {
  120. float3 vPosition : BEZIERPOS;
  121. };
  122. struct PS_INPUT
  123. {
  124. float3 vWorldPos : POSITION;
  125. float3 vNormal : NORMAL;
  126. float2 vUV : TEXCOORD;
  127. float3 vTangent : TANGENT;
  128. float3 vBiTangent : BITANGENT;
  129. };
  130. //--------------------------------------------------------------------------------------
  131. // Smooth shading pixel shader section
  132. //--------------------------------------------------------------------------------------
  133. float3 safe_normalize( float3 vInput )
  134. {
  135. float len2 = dot( vInput, vInput );
  136. if( len2 > 0 )
  137. {
  138. return vInput * rsqrt( len2 );
  139. }
  140. return vInput;
  141. }
  142. static const float g_fSpecularExponent = 32.0f;
  143. static const float g_fSpecularIntensity = 0.6f;
  144. static const float g_fNormalMapIntensity = 1.5f;
  145. float2 ComputeDirectionalLight( float3 vWorldPos, float3 vWorldNormal, float3 vDirLightDir )
  146. {
  147. // Result.x is diffuse illumination, Result.y is specular illumination
  148. float2 Result = float2( 0, 0 );
  149. Result.x = pow( saturate( dot( vWorldNormal, -vDirLightDir ) ), 2 );
  150. float3 vPointToCamera = normalize( g_vCameraPosWorld - vWorldPos );
  151. float3 vHalfAngle = normalize( vPointToCamera - vDirLightDir );
  152. Result.y = pow( saturate( dot( vHalfAngle, vWorldNormal ) ), g_fSpecularExponent );
  153. return Result;
  154. }
  155. float3 ColorGamma( float3 Input )
  156. {
  157. return pow( Input, 2.2f );
  158. }
  159. float4 main( PS_INPUT Input ) : SV_TARGET
  160. {
  161. float4 vNormalMapSampleRaw = g_txHeight.Sample( g_samLinear, Input.vUV );
  162. float3 vNormalMapSampleBiased = ( vNormalMapSampleRaw.xyz * 2 ) - 1;
  163. vNormalMapSampleBiased.xy *= g_fNormalMapIntensity;
  164. float3 vNormalMapSample = normalize( vNormalMapSampleBiased );
  165. float3 vNormal = safe_normalize( Input.vNormal ) * vNormalMapSample.z;
  166. vNormal += safe_normalize( Input.vTangent ) * vNormalMapSample.x;
  167. vNormal += safe_normalize( Input.vBiTangent ) * vNormalMapSample.y;
  168. //float3 vColor = float3( 1, 1, 1 );
  169. float3 vColor = g_txDiffuse.Sample( g_samLinear, Input.vUV ).rgb;
  170. float vSpecular = g_txSpecular.Sample( g_samLinear, Input.vUV ).r * g_fSpecularIntensity;
  171. const float3 DirLightDirections[4] =
  172. {
  173. // key light
  174. normalize( float3( -63.345150, -58.043934, 27.785097 ) ),
  175. // fill light
  176. normalize( float3( 23.652107, -17.391443, 54.972504 ) ),
  177. // back light 1
  178. normalize( float3( 20.470509, -22.939510, -33.929531 ) ),
  179. // back light 2
  180. normalize( float3( -31.003685, 24.242104, -41.352859 ) ),
  181. };
  182. const float3 DirLightColors[4] =
  183. {
  184. // key light
  185. ColorGamma( float3( 1.0f, 0.964f, 0.706f ) * 1.0f ),
  186. // fill light
  187. ColorGamma( float3( 0.446f, 0.641f, 1.0f ) * 1.0f ),
  188. // back light 1
  189. ColorGamma( float3( 1.0f, 0.862f, 0.419f ) * 1.0f ),
  190. // back light 2
  191. ColorGamma( float3( 0.405f, 0.630f, 1.0f ) * 1.0f ),
  192. };
  193. float3 fLightColor = 0;
  194. for( int i = 0; i < 4; ++i )
  195. {
  196. float2 LightDiffuseSpecular = ComputeDirectionalLight( Input.vWorldPos, vNormal, DirLightDirections[i] );
  197. fLightColor += DirLightColors[i] * vColor * LightDiffuseSpecular.x;
  198. fLightColor += DirLightColors[i] * LightDiffuseSpecular.y * vSpecular;
  199. }
  200. return float4( fLightColor, 1 );
  201. }
  202. // REFL: TempArrayCount: 0
  203. // REFL: DynamicFlowControlCount: 4
  204. // REFL: ArrayInstructionCount: 0
  205. // REFL: TextureNormalInstructions: 3
  206. // REFL: TextureLoadInstructions: 0
  207. // REFL: TextureCompInstructions: 0
  208. // REFL: TextureBiasInstructions: 0
  209. // REFL: TextureGradientInstructions: 0
  210. // REFL: IntInstructionCount: 2
  211. // REFL: UintInstructionCount: 0
  212. // REFL: CutInstructionCount: 0
  213. // REFL: EmitInstructionCount: 0
  214. // REFL: cBarrierInstructions: 0
  215. // REFL: cInterlockedInstructions: 0
  216. // REFL: cTextureStoreInstructions: 0