1//
2//   Copyright 2013 Pixar
3//
4//   Licensed under the Apache License, Version 2.0 (the "Apache License")
5//   with the following modification; you may not use this file except in
6//   compliance with the Apache License and the following modification to it:
7//   Section 6. Trademarks. is deleted and replaced with:
8//
9//   6. Trademarks. This License does not grant permission to use the trade
10//      names, trademarks, service marks, or product names of the Licensor
11//      and its affiliates, except as required to comply with Section 4(c) of
12//      the License and to reproduce the content of the NOTICE file.
13//
14//   You may obtain a copy of the Apache License at
15//
16//       http://www.apache.org/licenses/LICENSE-2.0
17//
18//   Unless required by applicable law or agreed to in writing, software
19//   distributed under the Apache License with the above modification is
20//   distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21//   KIND, either express or implied. See the Apache License for the specific
22//   language governing permissions and limitations under the Apache License.
23//
24
25//----------------------------------------------------------
26// Patches.VertexGregory
27//----------------------------------------------------------
28
29void vs_main_patches( in InputVertex input,
30                      uint vID : SV_VertexID,
31                      out OsdPerVertexGregory output )
32{
33    OsdComputePerVertexGregory(vID, input.position, output);
34    OSD_PATCH_CULL_COMPUTE_CLIPFLAGS(input.position);
35}
36
37//----------------------------------------------------------
38// Patches.HullGregory
39//----------------------------------------------------------
40
41[domain("quad")]
42[partitioning(OSD_PARTITIONING)]
43[outputtopology("triangle_cw")]
44[outputcontrolpoints(4)]
45[patchconstantfunc("HSConstFunc")]
46OsdPerPatchVertexGregory hs_main_patches(
47    in InputPatch<OsdPerVertexGregory, 4> patch,
48    uint primitiveID : SV_PrimitiveID,
49    in uint ID : SV_OutputControlPointID )
50{
51    OsdPerPatchVertexGregory output;
52
53    OsdPerVertexGregory cv[4];
54    for (int i=0; i<4; ++i) {
55        cv[i] = patch[i];
56    }
57
58    int3 patchParam = OsdGetPatchParam(OsdGetPatchIndex(primitiveID));
59    OsdComputePerPatchVertexGregory(patchParam, ID, primitiveID, cv, output);
60
61    return output;
62}
63
64HS_CONSTANT_FUNC_OUT HSConstFunc(
65    InputPatch<OsdPerVertexGregory, 4> patch,
66    OutputPatch<OsdPerPatchVertexGregory, 4> gregoryPatch,
67    uint primitiveID : SV_PrimitiveID)
68{
69    HS_CONSTANT_FUNC_OUT output;
70
71    int3 patchParam = OsdGetPatchParam(OsdGetPatchIndex(primitiveID));
72
73    float4 tessLevelOuter = float4(0,0,0,0);
74    float2 tessLevelInner = float2(0,0);
75    float4 tessOuterLo = float4(0,0,0,0);
76    float4 tessOuterHi = float4(0,0,0,0);
77
78    OSD_PATCH_CULL(4);
79
80#if defined OSD_ENABLE_SCREENSPACE_TESSELLATION
81    // Gather bezier control points to compute limit surface tess levels
82    OsdPerPatchVertexBezier bezcv[16];
83    bezcv[ 0].P = gregoryPatch[0].P;
84    bezcv[ 1].P = gregoryPatch[0].Ep;
85    bezcv[ 2].P = gregoryPatch[1].Em;
86    bezcv[ 3].P = gregoryPatch[1].P;
87    bezcv[ 4].P = gregoryPatch[0].Em;
88    bezcv[ 5].P = gregoryPatch[0].Fp;
89    bezcv[ 6].P = gregoryPatch[1].Fm;
90    bezcv[ 7].P = gregoryPatch[1].Ep;
91    bezcv[ 8].P = gregoryPatch[3].Ep;
92    bezcv[ 9].P = gregoryPatch[3].Fm;
93    bezcv[10].P = gregoryPatch[2].Fp;
94    bezcv[11].P = gregoryPatch[2].Em;
95    bezcv[12].P = gregoryPatch[3].P;
96    bezcv[13].P = gregoryPatch[3].Em;
97    bezcv[14].P = gregoryPatch[2].Ep;
98    bezcv[15].P = gregoryPatch[2].P;
99
100    OsdEvalPatchBezierTessLevels(bezcv, patchParam,
101                                 tessLevelOuter, tessLevelInner,
102                                 tessOuterLo, tessOuterHi);
103#else
104    OsdGetTessLevelsUniform(patchParam, tessLevelOuter, tessLevelInner,
105                     tessOuterLo, tessOuterHi);
106
107#endif
108
109    output.tessLevelOuter[0] = tessLevelOuter[0];
110    output.tessLevelOuter[1] = tessLevelOuter[1];
111    output.tessLevelOuter[2] = tessLevelOuter[2];
112    output.tessLevelOuter[3] = tessLevelOuter[3];
113
114    output.tessLevelInner[0] = tessLevelInner[0];
115    output.tessLevelInner[1] = tessLevelInner[1];
116
117    output.tessOuterLo = tessOuterLo;
118    output.tessOuterHi = tessOuterHi;
119
120
121    return output;
122}
123
124//----------------------------------------------------------
125// Patches.DomainGregory
126//----------------------------------------------------------
127
128[domain("quad")]
129void ds_main_patches(
130    in HS_CONSTANT_FUNC_OUT input,
131    in OutputPatch<OsdPerPatchVertexGregory, 4> patch,
132    in float2 domainCoord : SV_DomainLocation,
133    out OutputVertex output )
134{
135    float3 P = float3(0,0,0), dPu = float3(0,0,0), dPv = float3(0,0,0);
136    float3 N = float3(0,0,0), dNu = float3(0,0,0), dNv = float3(0,0,0);
137
138    float3 cv[20];
139    cv[0] = patch[0].P;
140    cv[1] = patch[0].Ep;
141    cv[2] = patch[0].Em;
142    cv[3] = patch[0].Fp;
143    cv[4] = patch[0].Fm;
144
145    cv[5] = patch[1].P;
146    cv[6] = patch[1].Ep;
147    cv[7] = patch[1].Em;
148    cv[8] = patch[1].Fp;
149    cv[9] = patch[1].Fm;
150
151    cv[10] = patch[2].P;
152    cv[11] = patch[2].Ep;
153    cv[12] = patch[2].Em;
154    cv[13] = patch[2].Fp;
155    cv[14] = patch[2].Fm;
156
157    cv[15] = patch[3].P;
158    cv[16] = patch[3].Ep;
159    cv[17] = patch[3].Em;
160    cv[18] = patch[3].Fp;
161    cv[19] = patch[3].Fm;
162
163    float2 UV = OsdGetTessParameterization(domainCoord,
164                                           input.tessOuterLo,
165                                           input.tessOuterHi);
166
167    int3 patchParam = patch[0].patchParam;
168    OsdEvalPatchGregory(patchParam, UV, cv, P, dPu, dPv, N, dNu, dNv);
169
170    // all code below here is client code
171    output.position = mul(OsdModelViewMatrix(), float4(P, 1.0f));
172    output.normal = mul(OsdModelViewMatrix(), float4(N, 0.0f)).xyz;
173    output.tangent = mul(OsdModelViewMatrix(), float4(dPu, 0.0f)).xyz;
174    output.bitangent = mul(OsdModelViewMatrix(), float4(dPv, 0.0f)).xyz;
175#ifdef OSD_COMPUTE_NORMAL_DERIVATIVES
176    output.Nu = dNu;
177    output.Nv = dNv;
178#endif
179
180    output.patchCoord = OsdInterpolatePatchCoord(UV, patchParam);
181
182    OSD_DISPLACEMENT_CALLBACK;
183
184    output.positionOut = mul(OsdProjectionMatrix(), output.position);
185    output.edgeDistance = 0;
186}
187