4 Copyright (c) 2022 Google LLC
5 Copyright (c) 2022 Sascha Willems
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 float4 frustumPlanes[6];
32 float displacementFactor;
33 float tessellationFactor;
35 float tessellatedEdgeSize;
37 cbuffer ubo : register(b0) { UBO ubo; };
39 Texture2D textureHeight : register(t1);
40 SamplerState samplerHeight : register(s1);
44 float4 Pos : SV_POSITION;
45 [[vk::location(0)]] float3 Normal : NORMAL0;
46 [[vk::location(1)]] float2 UV : TEXCOORD0;
51 [[vk::location(2)]] float4 Pos : SV_POSITION;
52 [[vk::location(0)]] float3 Normal : NORMAL0;
53 [[vk::location(1)]] float2 UV : TEXCOORD0;
56 struct ConstantsHSOutput
58 float TessLevelOuter[4] : SV_TessFactor;
59 float TessLevelInner[2] : SV_InsideTessFactor;
62 // Calculate the tessellation factor based on screen space
63 // dimensions of the edge
64 float screenSpaceTessFactor(float4 p0, float4 p1)
66 // Calculate edge mid point
67 float4 midPoint = 0.5 * (p0 + p1);
68 // Sphere radius as distance between the control points
69 float radius = distance(p0, p1) / 2.0;
72 float4 v0 = mul(ubo.modelview, midPoint);
74 // Project into clip space
75 float4 clip0 = mul(ubo.projection, (v0 - float4(radius, float3(0.0, 0.0, 0.0))));
76 float4 clip1 = mul(ubo.projection, (v0 + float4(radius, float3(0.0, 0.0, 0.0))));
78 // Get normalized device coordinates
82 // Convert to viewport coordinates
83 clip0.xy *= ubo.viewportDim;
84 clip1.xy *= ubo.viewportDim;
86 // Return the tessellation factor based on the screen size
87 // given by the distance of the two edge control points in screen space
88 // and a reference (min.) tessellation size for the edge set by the application
89 return clamp(distance(clip0, clip1) / ubo.tessellatedEdgeSize * ubo.tessellationFactor, 1.0, 64.0);
92 // Checks the current's patch visibility against the frustum using a sphere check
93 // Sphere radius is given by the patch size
94 bool frustumCheck(float4 Pos, float2 inUV)
96 // Fixed radius (increase if patch size is increased in example)
97 const float radius = 8.0f;
99 pos.y -= textureHeight.SampleLevel(samplerHeight, inUV, 0.0).r * ubo.displacementFactor;
101 // Check sphere against frustum planes
102 for (int i = 0; i < 6; i++) {
103 if (dot(pos, ubo.frustumPlanes[i]) + radius < 0.0)
111 ConstantsHSOutput ConstantsHS(InputPatch<VSOutput, 4> patch)
113 ConstantsHSOutput output = (ConstantsHSOutput)0;
115 if (!frustumCheck(patch[0].Pos, patch[0].UV))
117 output.TessLevelInner[0] = 0.0;
118 output.TessLevelInner[1] = 0.0;
119 output.TessLevelOuter[0] = 0.0;
120 output.TessLevelOuter[1] = 0.0;
121 output.TessLevelOuter[2] = 0.0;
122 output.TessLevelOuter[3] = 0.0;
126 if (ubo.tessellationFactor > 0.0)
128 output.TessLevelOuter[0] = screenSpaceTessFactor(patch[3].Pos, patch[0].Pos);
129 output.TessLevelOuter[1] = screenSpaceTessFactor(patch[0].Pos, patch[1].Pos);
130 output.TessLevelOuter[2] = screenSpaceTessFactor(patch[1].Pos, patch[2].Pos);
131 output.TessLevelOuter[3] = screenSpaceTessFactor(patch[2].Pos, patch[3].Pos);
132 output.TessLevelInner[0] = lerp(output.TessLevelOuter[0], output.TessLevelOuter[3], 0.5);
133 output.TessLevelInner[1] = lerp(output.TessLevelOuter[2], output.TessLevelOuter[1], 0.5);
137 // Tessellation factor can be set to zero by example
138 // to demonstrate a simple passthrough
139 output.TessLevelInner[0] = 1.0;
140 output.TessLevelInner[1] = 1.0;
141 output.TessLevelOuter[0] = 1.0;
142 output.TessLevelOuter[1] = 1.0;
143 output.TessLevelOuter[2] = 1.0;
144 output.TessLevelOuter[3] = 1.0;
152 [partitioning("integer")]
153 [outputtopology("triangle_cw")]
154 [outputcontrolpoints(4)]
155 [patchconstantfunc("ConstantsHS")]
156 [maxtessfactor(20.0f)]
157 HSOutput main(InputPatch<VSOutput, 4> patch, uint InvocationID : SV_OutputControlPointID)
159 HSOutput output = (HSOutput)0;
160 output.Pos = patch[InvocationID].Pos;
161 output.Normal = patch[InvocationID].Normal;
162 output.UV = patch[InvocationID].UV;