1//
2//   Copyright 2015 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.VertexGregoryBasis
27//----------------------------------------------------------
28
29void vs_main_patches( in InputVertex input,
30                      out HullVertex output )
31{
32    output.position = input.position;
33    OSD_PATCH_CULL_COMPUTE_CLIPFLAGS(input.position);
34}
35
36//----------------------------------------------------------
37// Patches.HullGregoryBasis
38//----------------------------------------------------------
39
40[domain("quad")]
41[partitioning(OSD_PARTITIONING)]
42[outputtopology("triangle_cw")]
43[outputcontrolpoints(20)]
44[patchconstantfunc("HSConstFunc")]
45OsdPerPatchVertexGregoryBasis hs_main_patches(
46    in InputPatch<HullVertex, 20> patch,
47    uint primitiveID : SV_PrimitiveID,
48    in uint ID : SV_OutputControlPointID )
49{
50    OsdPerPatchVertexGregoryBasis output;
51
52    float3 cv = patch[ID].position.xyz;
53
54    int3 patchParam = OsdGetPatchParam(OsdGetPatchIndex(primitiveID));
55    OsdComputePerPatchVertexGregoryBasis(patchParam, ID, cv, output);
56
57    return output;
58}
59
60HS_CONSTANT_FUNC_OUT
61HSConstFunc(
62    InputPatch<HullVertex, 20> patch,
63    uint primitiveID : SV_PrimitiveID)
64{
65    HS_CONSTANT_FUNC_OUT output;
66
67    int3 patchParam = OsdGetPatchParam(OsdGetPatchIndex(primitiveID));
68
69    float4 tessLevelOuter = float4(0,0,0,0);
70    float2 tessLevelInner = float2(0,0);
71    float4 tessOuterLo = float4(0,0,0,0);
72    float4 tessOuterHi = float4(0,0,0,0);
73
74    OSD_PATCH_CULL(20);
75
76#if defined OSD_ENABLE_SCREENSPACE_TESSELLATION
77    // Gather bezier control points to compute limit surface tess levels
78    OsdPerPatchVertexBezier bezcv[16];
79    bezcv[ 0].P = patch[ 0].position.xyz;
80    bezcv[ 1].P = patch[ 1].position.xyz;
81    bezcv[ 2].P = patch[ 7].position.xyz;
82    bezcv[ 3].P = patch[ 5].position.xyz;
83    bezcv[ 4].P = patch[ 2].position.xyz;
84    bezcv[ 5].P = patch[ 3].position.xyz;
85    bezcv[ 6].P = patch[ 8].position.xyz;
86    bezcv[ 7].P = patch[ 6].position.xyz;
87    bezcv[ 8].P = patch[16].position.xyz;
88    bezcv[ 9].P = patch[18].position.xyz;
89    bezcv[10].P = patch[13].position.xyz;
90    bezcv[11].P = patch[12].position.xyz;
91    bezcv[12].P = patch[15].position.xyz;
92    bezcv[13].P = patch[17].position.xyz;
93    bezcv[14].P = patch[11].position.xyz;
94    bezcv[15].P = patch[10].position.xyz;
95
96    OsdEvalPatchBezierTessLevels(
97                bezcv, patchParam,
98                tessLevelOuter, tessLevelInner,
99                tessOuterLo, tessOuterHi);
100#else
101    OsdGetTessLevelsUniform(
102                patchParam,
103                tessLevelOuter, tessLevelInner,
104                tessOuterLo, tessOuterHi);
105#endif
106
107    output.tessLevelOuter[0] = tessLevelOuter[0];
108    output.tessLevelOuter[1] = tessLevelOuter[1];
109    output.tessLevelOuter[2] = tessLevelOuter[2];
110    output.tessLevelOuter[3] = tessLevelOuter[3];
111
112    output.tessLevelInner[0] = tessLevelInner[0];
113    output.tessLevelInner[1] = tessLevelInner[1];
114
115    output.tessOuterLo = tessOuterLo;
116    output.tessOuterHi = tessOuterHi;
117
118    return output;
119}
120
121//----------------------------------------------------------
122// Patches.DomainGregoryBasis
123//----------------------------------------------------------
124
125[domain("quad")]
126void ds_main_patches(
127    in HS_CONSTANT_FUNC_OUT input,
128    in OutputPatch<OsdPerPatchVertexGregoryBasis, 20> patch,
129    in float2 domainCoord : SV_DomainLocation,
130    out OutputVertex output )
131{
132    float3 P = float3(0,0,0), dPu = float3(0,0,0), dPv = float3(0,0,0);
133    float3 N = float3(0,0,0), dNu = float3(0,0,0), dNv = float3(0,0,0);
134
135    float3 cv[20];
136    for (int i = 0; i < 20; ++i) {
137        cv[i] = patch[i].P;
138    }
139
140    float2 UV = OsdGetTessParameterization(domainCoord,
141                                           input.tessOuterLo,
142                                           input.tessOuterHi);
143
144    int3 patchParam = patch[0].patchParam;
145    OsdEvalPatchGregory(patchParam, UV, cv, P, dPu, dPv, N, dNu, dNv);
146
147    // all code below here is client code
148    output.position = mul(OsdModelViewMatrix(), float4(P, 1.0f));
149    output.normal = mul(OsdModelViewMatrix(), float4(N, 0.0f)).xyz;
150    output.tangent = mul(OsdModelViewMatrix(), float4(dPu, 0.0f)).xyz;
151    output.bitangent = mul(OsdModelViewMatrix(), float4(dPv, 0.0f)).xyz;
152#ifdef OSD_COMPUTE_NORMAL_DERIVATIVES
153    output.Nu = dNu;
154    output.Nv = dNv;
155#endif
156#if defined OSD_PATCH_ENABLE_SINGLE_CREASE
157    output.vSegments = float2(0,0);
158#endif
159
160    output.patchCoord = OsdInterpolatePatchCoord(UV, patchParam);
161
162    OSD_DISPLACEMENT_CALLBACK;
163
164    output.positionOut = mul(OsdProjectionMatrix(), output.position);
165    output.edgeDistance = 0;
166}
167