Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.81 KB | None | 0 0
  1. // Copyright (c) 2011 NVIDIA Corporation. All rights reserved.
  2. //
  3. // TO  THE MAXIMUM  EXTENT PERMITTED  BY APPLICABLE  LAW, THIS SOFTWARE  IS PROVIDED
  4. // *AS IS*  AND NVIDIA AND  ITS SUPPLIERS DISCLAIM  ALL WARRANTIES,  EITHER  EXPRESS
  5. // OR IMPLIED, INCLUDING, BUT NOT LIMITED  TO, NONINFRINGEMENT,IMPLIED WARRANTIES OF
  6. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL  NVIDIA
  7. // OR ITS SUPPLIERS BE  LIABLE  FOR  ANY  DIRECT, SPECIAL,  INCIDENTAL,  INDIRECT,  OR  
  8. // CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION,  DAMAGES FOR LOSS
  9. // OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY
  10. // OTHER PECUNIARY LOSS) ARISING OUT OF THE  USE OF OR INABILITY  TO USE THIS SOFTWARE,
  11. // EVEN IF NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  12. //
  13. // Please direct any bugs or questions to SDKFeedback@nvidia.com
  14.  
  15. // This dample demonstrates the use of approximating Catmull-clark subdivision
  16. // surfaces with gregory patches.
  17.  
  18. //=================================================================================================================================
  19. // Constant buffer
  20. //=================================================================================================================================
  21.  
  22. cbuffer cbTriangles : register( b0 )
  23. {
  24.     float4x4    g_f4x4WorldViewProjection;  // World * View * Projection matrix
  25.     float4      g_f4TessFactors;            
  26.     float4      g_f2Modes;
  27. }
  28.  
  29.  
  30. //=================================================================================================================================
  31. // Shader structures
  32. //=================================================================================================================================
  33.  
  34. struct Position_Input
  35. {
  36.     float3 f3Position   : POSITION0;  
  37. };
  38.  
  39. struct HS_ConstantOutput
  40. {
  41.     float fTessFactor[3]    : SV_TessFactor;
  42.     float fInsideTessFactor : SV_InsideTessFactor;
  43. };
  44.  
  45. struct HS_ConstantOutput_Quad
  46. {
  47.     float fTessFactor[4]       : SV_TessFactor;
  48.     float fInsideTessFactor[2] : SV_InsideTessFactor;
  49. };
  50.  
  51. struct DS_Output
  52. {
  53.     float4 f4Position   : SV_Position;
  54.  
  55. };
  56.  
  57. struct PS_RenderSceneInput
  58. {
  59.     float4 f4Position   : SV_Position;
  60.  
  61. };
  62.  
  63. struct PS_RenderOutput
  64. {
  65.     float4 f4Color      : SV_Target0;
  66. };
  67. //=================================================================================================================================
  68. // Functions
  69. //=================================================================================================================================
  70. float3 bilerpUV(float3 v[4], float2 uv)
  71. {
  72.     // bilerp the texture coordinates    
  73.     float3 bottom = lerp( v[0], v[1], uv.x );
  74.     float3 top = lerp( v[3], v[2], uv.x );
  75.     float3 result = lerp( bottom, top, uv.y );
  76.    
  77.     return result;    
  78. }
  79.  
  80. //=================================================================================================================================
  81. // This vertex shader is a pass through stage, with HS, tessellation, and DS stages following
  82. //=================================================================================================================================
  83. Position_Input VS_RenderSceneWithTessellation( Position_Input I )
  84. {
  85.     Position_Input O;
  86.    
  87.     // Pass through world space position
  88.     O.f3Position = I.f3Position;
  89.    
  90.     return O;    
  91. }
  92.  
  93. HS_ConstantOutput HS_TrianglesConstant( InputPatch<Position_Input, 3> I )
  94. {
  95.     HS_ConstantOutput O = (HS_ConstantOutput)0;
  96.  
  97.     float3 rtf; float ritf, itf;
  98.     uint mode = (uint)g_f2Modes.x;
  99.     switch (mode)
  100.     {
  101.         case 0: ProcessTriTessFactorsMax(g_f4TessFactors.xyz, g_f2Modes.y, rtf, ritf, itf);
  102.             break;
  103.         case 1: ProcessTriTessFactorsMin(g_f4TessFactors.xyz, g_f2Modes.y, rtf, ritf, itf);
  104.             break;
  105.         case 2: ProcessTriTessFactorsAvg(g_f4TessFactors.xyz, g_f2Modes.y, rtf, ritf, itf);
  106.             break;
  107.         default: ProcessTriTessFactorsMax(g_f4TessFactors.xyz, g_f2Modes.y, rtf, ritf, itf);
  108.             break;
  109.     }
  110.     O.fTessFactor[0] = rtf.x;
  111.     O.fTessFactor[1] = rtf.y;
  112.     O.fTessFactor[2] = rtf.z;
  113.     O.fInsideTessFactor = ritf;
  114.  
  115.     return O;
  116. }
  117. [domain("tri")]
  118. [partitioning("integer")]
  119. [outputtopology("triangle_cw")]
  120. [patchconstantfunc("HS_TrianglesConstant")]
  121. [outputcontrolpoints(3)]
  122. Position_Input HS_Triangles_integer( InputPatch<Position_Input, 3> I, uint uCPID : SV_OutputControlPointID )
  123. {
  124.     Position_Input O = (Position_Input)0;
  125.  
  126.     O.f3Position = I[uCPID].f3Position;
  127.    
  128.     return O;
  129. }
  130. [domain("tri")]
  131. [partitioning("fractional_odd")]
  132. [outputtopology("triangle_cw")]
  133. [patchconstantfunc("HS_TrianglesConstant")]
  134. [outputcontrolpoints(3)]
  135. Position_Input HS_Triangles_fractionalodd( InputPatch<Position_Input, 3> I, uint uCPID : SV_OutputControlPointID )
  136. {
  137.     Position_Input O = (Position_Input)0;
  138.  
  139.     O.f3Position = I[uCPID].f3Position;
  140.    
  141.     return O;
  142. }
  143. [domain("tri")]
  144. [partitioning("fractional_even")]
  145. [outputtopology("triangle_cw")]
  146. [patchconstantfunc("HS_TrianglesConstant")]
  147. [outputcontrolpoints(3)]
  148. Position_Input HS_Triangles_fractionaleven( InputPatch<Position_Input, 3> I, uint uCPID : SV_OutputControlPointID )
  149. {
  150.     Position_Input O = (Position_Input)0;
  151.  
  152.     O.f3Position = I[uCPID].f3Position;
  153.    
  154.     return O;
  155. }
  156. [domain("tri")]
  157. [partitioning("pow2")]
  158. [outputtopology("triangle_cw")]
  159. [patchconstantfunc("HS_TrianglesConstant")]
  160. [outputcontrolpoints(3)]
  161. Position_Input HS_Triangles_pow2( InputPatch<Position_Input, 3> I, uint uCPID : SV_OutputControlPointID )
  162. {
  163.     Position_Input O = (Position_Input)0;
  164.  
  165.     O.f3Position = I[uCPID].f3Position;
  166.    
  167.     return O;
  168. }
  169. //=================================================================================================================================
  170. // This domain shader applies contol point weighting to the barycentric coords produced by the FF tessellator
  171. //=================================================================================================================================
  172. [domain("tri")]
  173. PS_RenderSceneInput DS_Triangles( HS_ConstantOutput HSConstantData, const OutputPatch<Position_Input, 3> I, float3 f3BarycentricCoords : SV_DomainLocation )
  174. {
  175.     PS_RenderSceneInput O = (PS_RenderSceneInput)0;
  176.  
  177.     // The barycentric coordinates
  178.     float fU = f3BarycentricCoords.x;
  179.     float fV = f3BarycentricCoords.y;
  180.     float fW = f3BarycentricCoords.z;
  181.  
  182.  
  183.     float3 f3Position = I[0].f3Position * fW +
  184.                         I[1].f3Position * fU +
  185.                         I[2].f3Position * fV;
  186.  
  187.  
  188.     // Transform model position with view-projection matrix
  189.     O.f4Position = mul( float4( f3Position.xyz, 1.0 ), g_f4x4WorldViewProjection );
  190.  
  191.        
  192.     return O;
  193. }
  194.  
  195.  
  196.  
  197. HS_ConstantOutput_Quad HS_QuadsConstant( InputPatch<Position_Input, 4> I )
  198. {
  199.     HS_ConstantOutput_Quad O = (HS_ConstantOutput_Quad)0;
  200.    
  201.     float2 ritf,itf; float4 rtf;
  202.     uint mode = (uint)g_f2Modes.x;
  203.     switch (mode)
  204.     {
  205.         case 0: Process2DQuadTessFactorsMax(g_f4TessFactors, g_f2Modes.y, rtf, ritf, itf);
  206.             break;
  207.         case 1: Process2DQuadTessFactorsMin(g_f4TessFactors, g_f2Modes.y, rtf, ritf, itf);
  208.             break;
  209.         case 2: Process2DQuadTessFactorsAvg(g_f4TessFactors, g_f2Modes.y, rtf, ritf, itf);
  210.             break;
  211.         default: Process2DQuadTessFactorsMax(g_f4TessFactors, g_f2Modes.y, rtf, ritf, itf);
  212.             break;
  213.     }
  214.  
  215.     O.fTessFactor[0] = rtf.x;
  216.     O.fTessFactor[1] = rtf.y;
  217.     O.fTessFactor[2] = rtf.z;
  218.     O.fTessFactor[3] = rtf.w;
  219.     O.fInsideTessFactor[0] = ritf.x;
  220.     O.fInsideTessFactor[1] = ritf.y;
  221.  
  222.     return O;
  223. }
  224. [domain("quad")]
  225. [partitioning("pow2")]
  226. [outputtopology("triangle_cw")]
  227. [patchconstantfunc("HS_QuadsConstant")]
  228. [outputcontrolpoints(4)]
  229. Position_Input HS_Quads_pow2( InputPatch<Position_Input, 4> I, uint uCPID : SV_OutputControlPointID )
  230. {
  231.     Position_Input O = (Position_Input)0;
  232.  
  233.     O.f3Position = I[uCPID].f3Position;
  234.    
  235.     return O;
  236. }
  237. [domain("quad")]
  238. [partitioning("integer")]
  239. [outputtopology("triangle_cw")]
  240. [patchconstantfunc("HS_QuadsConstant")]
  241. [outputcontrolpoints(4)]
  242. Position_Input HS_Quads_integer( InputPatch<Position_Input, 4> I, uint uCPID : SV_OutputControlPointID )
  243. {
  244.     Position_Input O = (Position_Input)0;
  245.  
  246.     O.f3Position = I[uCPID].f3Position;
  247.    
  248.     return O;
  249. }
  250. [domain("quad")]
  251. [partitioning("fractional_odd")]
  252. [outputtopology("triangle_cw")]
  253. [patchconstantfunc("HS_QuadsConstant")]
  254. [outputcontrolpoints(4)]
  255. Position_Input HS_Quads_fractionalodd( InputPatch<Position_Input, 4> I, uint uCPID : SV_OutputControlPointID )
  256. {
  257.     Position_Input O = (Position_Input)0;
  258.  
  259.     O.f3Position = I[uCPID].f3Position;
  260.    
  261.     return O;
  262. }
  263. [domain("quad")]
  264. [partitioning("fractional_even")]
  265. [outputtopology("triangle_cw")]
  266. [patchconstantfunc("HS_QuadsConstant")]
  267. [outputcontrolpoints(4)]
  268. Position_Input HS_Quads_fractionaleven( InputPatch<Position_Input, 4> I, uint uCPID : SV_OutputControlPointID )
  269. {
  270.     Position_Input O = (Position_Input)0;
  271.  
  272.     O.f3Position = I[uCPID].f3Position;
  273.    
  274.     return O;
  275. }
  276.  
  277. [domain("quad")]
  278. PS_RenderSceneInput DS_Quads( HS_ConstantOutput_Quad HSConstantData, const OutputPatch<Position_Input, 4> I, float2 uv : SV_DomainLocation )
  279. {
  280.     PS_RenderSceneInput O = (PS_RenderSceneInput)0;
  281.     float3 f3Position;
  282.  
  283.     float3 p[4];
  284.     [unroll]
  285.     for (uint i=0; i<4; i++)
  286.     {
  287.         p[i]=I[i].f3Position;
  288.     }
  289.     f3Position = bilerpUV(p, uv);
  290.  
  291.     O.f4Position = mul( float4( f3Position.xyz, 1.0 ), g_f4x4WorldViewProjection );
  292.        
  293.     return O;
  294. }
  295.  
  296. PS_RenderOutput PS_SolidColor( PS_RenderSceneInput I )
  297. {
  298.     PS_RenderOutput O;
  299.  
  300.  
  301.     O.f4Color=float4(0.9,0.9,0.9,1);
  302.  
  303.  
  304.     return O;
  305. }
  306. //=================================================================================================================================
  307. // EOF
  308. //=================================================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement