2016-07-26 19:41:18 +00:00
|
|
|
#line 0 "osd/mtlPatchBSpline.metal"
|
|
|
|
|
|
|
|
//
|
|
|
|
// Copyright 2015 Pixar
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "Apache License")
|
|
|
|
// with the following modification; you may not use this file except in
|
|
|
|
// compliance with the Apache License and the following modification to it:
|
|
|
|
// Section 6. Trademarks. is deleted and replaced with:
|
|
|
|
//
|
|
|
|
// 6. Trademarks. This License does not grant permission to use the trade
|
|
|
|
// names, trademarks, service marks, or product names of the Licensor
|
|
|
|
// and its affiliates, except as required to comply with Section 4(c) of
|
|
|
|
// the License and to reproduce the content of the NOTICE file.
|
|
|
|
//
|
|
|
|
// You may obtain a copy of the Apache License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the Apache License with the above modification is
|
|
|
|
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
// KIND, either express or implied. See the Apache License for the specific
|
|
|
|
// language governing permissions and limitations under the Apache License.
|
|
|
|
//
|
|
|
|
|
|
|
|
//----------------------------------------------------------
|
|
|
|
// Patches.BSpline.Hull
|
|
|
|
//----------------------------------------------------------
|
|
|
|
|
|
|
|
void OsdComputePerVertex(
|
2019-05-25 02:56:09 +00:00
|
|
|
float4 position,
|
2019-05-23 01:00:36 +00:00
|
|
|
threadgroup HullVertex& hullVertex,
|
|
|
|
int vertexId,
|
2019-05-25 02:56:09 +00:00
|
|
|
float4x4 modelViewProjectionMatrix,
|
2019-05-23 01:00:36 +00:00
|
|
|
OsdPatchParamBufferSet osdBuffers
|
|
|
|
)
|
2016-07-26 19:41:18 +00:00
|
|
|
{
|
2019-05-25 02:56:09 +00:00
|
|
|
hullVertex.position = position;
|
2016-07-26 19:41:18 +00:00
|
|
|
#if OSD_ENABLE_PATCH_CULL
|
2019-05-25 02:56:09 +00:00
|
|
|
float4 clipPos = mul(modelViewProjectionMatrix, position);
|
2019-05-23 01:00:36 +00:00
|
|
|
short3 clip0 = short3(clipPos.x < clipPos.w,
|
|
|
|
clipPos.y < clipPos.w,
|
|
|
|
clipPos.z < clipPos.w);
|
|
|
|
short3 clip1 = short3(clipPos.x > -clipPos.w,
|
|
|
|
clipPos.y > -clipPos.w,
|
|
|
|
clipPos.z > -clipPos.w);
|
|
|
|
hullVertex.clipFlag = short3(clip0) + 2*short3(clip1);
|
2016-07-26 19:41:18 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------
|
|
|
|
// Patches.BSpline.Factors
|
|
|
|
//----------------------------------------------------------
|
|
|
|
|
|
|
|
void OsdComputePerPatchBSplineFactors(
|
2019-05-23 01:00:36 +00:00
|
|
|
int3 patchParam,
|
|
|
|
float tessLevel,
|
|
|
|
float4x4 projectionMatrix,
|
|
|
|
float4x4 modelViewMatrix,
|
2019-05-25 02:56:09 +00:00
|
|
|
device OsdPerPatchVertexBezier* patch,
|
2016-07-26 19:41:18 +00:00
|
|
|
#if !USE_PTVS_FACTORS
|
2019-05-25 02:56:09 +00:00
|
|
|
device OsdPerPatchTessFactors& patchFactors,
|
2016-07-26 19:41:18 +00:00
|
|
|
#endif
|
2019-05-25 02:56:09 +00:00
|
|
|
device MTLQuadTessellationFactorsHalf& quadFactors
|
2019-05-23 01:00:36 +00:00
|
|
|
)
|
2016-07-26 19:41:18 +00:00
|
|
|
{
|
2019-05-30 19:20:03 +00:00
|
|
|
float4 tessLevelOuter = float4(0);
|
|
|
|
float2 tessLevelInner = float2(0);
|
|
|
|
float4 tessOuterLo = float4(0);
|
|
|
|
float4 tessOuterHi = float4(0);
|
2016-07-26 19:41:18 +00:00
|
|
|
|
|
|
|
#if OSD_ENABLE_SCREENSPACE_TESSELLATION
|
2019-05-25 02:56:09 +00:00
|
|
|
OsdEvalPatchBezierTessLevels(
|
2019-05-23 01:00:36 +00:00
|
|
|
tessLevel,
|
|
|
|
projectionMatrix,
|
2016-07-26 19:41:18 +00:00
|
|
|
modelViewMatrix,
|
|
|
|
patch,
|
|
|
|
patchParam,
|
|
|
|
tessLevelOuter,
|
|
|
|
tessLevelInner,
|
|
|
|
tessOuterLo,
|
|
|
|
tessOuterHi
|
|
|
|
);
|
|
|
|
#else
|
|
|
|
OsdGetTessLevelsUniform(
|
2019-05-23 01:00:36 +00:00
|
|
|
tessLevel,
|
|
|
|
patchParam,
|
2016-07-26 19:41:18 +00:00
|
|
|
tessLevelOuter,
|
|
|
|
tessLevelInner,
|
|
|
|
tessOuterLo,
|
|
|
|
tessOuterHi
|
|
|
|
);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
quadFactors.edgeTessellationFactor[0] = tessLevelOuter[0];
|
|
|
|
quadFactors.edgeTessellationFactor[1] = tessLevelOuter[1];
|
|
|
|
quadFactors.edgeTessellationFactor[2] = tessLevelOuter[2];
|
|
|
|
quadFactors.edgeTessellationFactor[3] = tessLevelOuter[3];
|
|
|
|
quadFactors.insideTessellationFactor[0] = tessLevelInner[0];
|
|
|
|
quadFactors.insideTessellationFactor[1] = tessLevelInner[1];
|
|
|
|
#if !USE_PTVS_FACTORS
|
|
|
|
patchFactors.tessOuterLo = tessOuterLo;
|
|
|
|
patchFactors.tessOuterHi = tessOuterHi;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void OsdComputePerPatchFactors(
|
2019-05-23 01:00:36 +00:00
|
|
|
int3 patchParam,
|
|
|
|
float tessLevel,
|
|
|
|
unsigned patchID,
|
|
|
|
float4x4 projectionMatrix,
|
|
|
|
float4x4 modelViewMatrix,
|
|
|
|
OsdPatchParamBufferSet osdBuffer,
|
|
|
|
threadgroup PatchVertexType* patchVertices,
|
|
|
|
device MTLQuadTessellationFactorsHalf& quadFactors
|
|
|
|
)
|
2016-07-26 19:41:18 +00:00
|
|
|
{
|
2019-05-23 01:00:36 +00:00
|
|
|
OsdComputePerPatchBSplineFactors(
|
|
|
|
patchParam,
|
|
|
|
tessLevel,
|
|
|
|
projectionMatrix,
|
|
|
|
modelViewMatrix,
|
2019-05-30 19:20:03 +00:00
|
|
|
osdBuffer.perPatchVertexBuffer + patchID * VERTEX_CONTROL_POINTS_PER_PATCH,
|
2016-07-26 19:41:18 +00:00
|
|
|
#if !USE_PTVS_FACTORS
|
2019-05-23 01:00:36 +00:00
|
|
|
osdBuffer.patchTessBuffer[patchID],
|
2016-07-26 19:41:18 +00:00
|
|
|
#endif
|
2019-05-23 01:00:36 +00:00
|
|
|
quadFactors
|
|
|
|
);
|
2016-07-26 19:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------
|
|
|
|
// Patches.BSpline.Vertex
|
|
|
|
//----------------------------------------------------------
|
|
|
|
|
|
|
|
void OsdComputePerPatchVertex(
|
2019-05-23 01:00:36 +00:00
|
|
|
int3 patchParam,
|
|
|
|
unsigned ID,
|
|
|
|
unsigned PrimitiveID,
|
|
|
|
unsigned ControlID,
|
|
|
|
threadgroup PatchVertexType* patchVertices,
|
|
|
|
OsdPatchParamBufferSet osdBuffers
|
|
|
|
)
|
2016-07-26 19:41:18 +00:00
|
|
|
{
|
2019-05-23 01:00:36 +00:00
|
|
|
OsdComputePerPatchVertexBSpline(patchParam, ID,
|
|
|
|
patchVertices, osdBuffers.perPatchVertexBuffer[ControlID]);
|
2016-07-26 19:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------
|
|
|
|
// Patches.BSpline.Domain
|
|
|
|
//----------------------------------------------------------
|
|
|
|
|
|
|
|
template<typename PerPatchVertexBezier>
|
|
|
|
OsdPatchVertex ds_regular_patches(
|
2019-05-25 02:56:09 +00:00
|
|
|
const float tessLevel,
|
2016-07-26 19:41:18 +00:00
|
|
|
#if !USE_PTVS_FACTORS
|
2019-05-23 01:00:36 +00:00
|
|
|
float4 tessOuterLo,
|
2019-05-25 02:56:09 +00:00
|
|
|
float4 tessOuterHi,
|
2016-07-26 19:41:18 +00:00
|
|
|
#endif
|
2019-05-23 01:00:36 +00:00
|
|
|
PerPatchVertexBezier cv,
|
|
|
|
int3 patchParam,
|
|
|
|
float2 domainCoord
|
|
|
|
)
|
2016-07-26 19:41:18 +00:00
|
|
|
{
|
2019-05-25 02:56:09 +00:00
|
|
|
#if USE_PTVS_FACTORS
|
|
|
|
float4 tessOuterLo(0), tessOuterHi(0);
|
|
|
|
OsdGetTessLevelsUniform(tessLevel, patchParam, tessOuterLo, tessOuterHi);
|
|
|
|
#endif
|
|
|
|
|
2019-05-30 19:20:03 +00:00
|
|
|
float2 UV = OsdGetTessParameterization(domainCoord,
|
2019-05-25 02:56:09 +00:00
|
|
|
tessOuterLo,
|
|
|
|
tessOuterHi);
|
|
|
|
|
2016-07-26 19:41:18 +00:00
|
|
|
OsdPatchVertex output;
|
|
|
|
|
2019-05-30 19:20:03 +00:00
|
|
|
float3 P = float3(0), dPu = float3(0), dPv = float3(0);
|
|
|
|
float3 N = float3(0), dNu = float3(0), dNv = float3(0);
|
|
|
|
float2 vSegments = float2(0);
|
2019-05-23 01:00:36 +00:00
|
|
|
|
2016-07-26 19:41:18 +00:00
|
|
|
OsdEvalPatchBezier(patchParam, UV, cv, P, dPu, dPv, N, dNu, dNv, vSegments);
|
2019-05-25 02:56:09 +00:00
|
|
|
|
2016-07-26 19:41:18 +00:00
|
|
|
output.normal = N;
|
|
|
|
output.tangent = dPu;
|
|
|
|
output.bitangent = dPv;
|
|
|
|
#if OSD_COMPUTE_NORMAL_DERIVATIVES
|
|
|
|
output.Nu = dNu;
|
|
|
|
output.Nv = dNv;
|
|
|
|
#endif
|
|
|
|
#if OSD_PATCH_ENABLE_SINGLE_CREASE
|
|
|
|
output.vSegments = vSegments;
|
|
|
|
#endif
|
|
|
|
|
2019-05-24 20:52:47 +00:00
|
|
|
output.tessCoord = UV;
|
2016-07-26 19:41:18 +00:00
|
|
|
output.patchCoord = OsdInterpolatePatchCoord(UV, patchParam);
|
|
|
|
output.position = P;
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if USE_STAGE_IN
|
|
|
|
template<typename PerPatchVertexBezier>
|
|
|
|
#endif
|
|
|
|
OsdPatchVertex OsdComputePatch(
|
2019-05-23 01:00:36 +00:00
|
|
|
float tessLevel,
|
|
|
|
float2 domainCoord,
|
|
|
|
unsigned patchID,
|
2016-07-26 19:41:18 +00:00
|
|
|
#if USE_STAGE_IN
|
2019-05-23 01:00:36 +00:00
|
|
|
PerPatchVertexBezier osdPatch
|
2016-07-26 19:41:18 +00:00
|
|
|
#else
|
2019-05-23 01:00:36 +00:00
|
|
|
OsdVertexBufferSet osdBuffers
|
2016-07-26 19:41:18 +00:00
|
|
|
#endif
|
2019-05-23 01:00:36 +00:00
|
|
|
)
|
2016-07-26 19:41:18 +00:00
|
|
|
{
|
2019-05-23 01:00:36 +00:00
|
|
|
return ds_regular_patches(
|
|
|
|
tessLevel,
|
2016-07-26 19:41:18 +00:00
|
|
|
#if !USE_PTVS_FACTORS
|
|
|
|
#if USE_STAGE_IN
|
2019-05-23 01:00:36 +00:00
|
|
|
osdPatch.tessOuterLo,
|
2019-05-25 02:56:09 +00:00
|
|
|
osdPatch.tessOuterHi,
|
2016-07-26 19:41:18 +00:00
|
|
|
#else
|
2019-05-23 01:00:36 +00:00
|
|
|
osdBuffers.patchTessBuffer[patchID].tessOuterLo,
|
2019-05-25 02:56:09 +00:00
|
|
|
osdBuffers.patchTessBuffer[patchID].tessOuterHi,
|
2016-07-26 19:41:18 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#if USE_STAGE_IN
|
2019-05-23 01:00:36 +00:00
|
|
|
osdPatch.cv,
|
|
|
|
osdPatch.patchParam,
|
2016-07-26 19:41:18 +00:00
|
|
|
#else
|
2019-05-23 01:00:36 +00:00
|
|
|
osdBuffers.perPatchVertexBuffer + patchID * VERTEX_CONTROL_POINTS_PER_PATCH,
|
|
|
|
osdBuffers.patchParamBuffer[patchID],
|
2016-07-26 19:41:18 +00:00
|
|
|
#endif
|
2019-05-23 01:00:36 +00:00
|
|
|
domainCoord
|
|
|
|
);
|
2016-07-26 19:41:18 +00:00
|
|
|
}
|
|
|
|
|