1617a3babSopenharmony_ci/*
2617a3babSopenharmony_ciThe MIT License (MIT)
3617a3babSopenharmony_ci
4617a3babSopenharmony_ciCopyright (c) 2022 Google LLC
5617a3babSopenharmony_ciCopyright (c) 2022 Sascha Willems
6617a3babSopenharmony_ci
7617a3babSopenharmony_ciPermission is hereby granted, free of charge, to any person obtaining a copy
8617a3babSopenharmony_ciof this software and associated documentation files (the "Software"), to deal
9617a3babSopenharmony_ciin the Software without restriction, including without limitation the rights
10617a3babSopenharmony_cito use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11617a3babSopenharmony_cicopies of the Software, and to permit persons to whom the Software is
12617a3babSopenharmony_cifurnished to do so, subject to the following conditions:
13617a3babSopenharmony_ci
14617a3babSopenharmony_ciThe above copyright notice and this permission notice shall be included in all
15617a3babSopenharmony_cicopies or substantial portions of the Software.
16617a3babSopenharmony_ci
17617a3babSopenharmony_ciTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18617a3babSopenharmony_ciIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19617a3babSopenharmony_ciFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20617a3babSopenharmony_ciAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21617a3babSopenharmony_ciLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22617a3babSopenharmony_ciOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23617a3babSopenharmony_ciSOFTWARE.
24617a3babSopenharmony_ci*/
25617a3babSopenharmony_ci
26617a3babSopenharmony_cistruct UBO
27617a3babSopenharmony_ci{
28617a3babSopenharmony_ci	float4x4 projection;
29617a3babSopenharmony_ci	float4x4 modelview;
30617a3babSopenharmony_ci	float4 lightPos;
31617a3babSopenharmony_ci	float4 frustumPlanes[6];
32617a3babSopenharmony_ci	float displacementFactor;
33617a3babSopenharmony_ci	float tessellationFactor;
34617a3babSopenharmony_ci	float2 viewportDim;
35617a3babSopenharmony_ci	float tessellatedEdgeSize;
36617a3babSopenharmony_ci};
37617a3babSopenharmony_cicbuffer ubo : register(b0) { UBO ubo; };
38617a3babSopenharmony_ci
39617a3babSopenharmony_ciTexture2D displacementMapTexture : register(t1);
40617a3babSopenharmony_ciSamplerState displacementMapSampler : register(s1);
41617a3babSopenharmony_ci
42617a3babSopenharmony_cistruct HSOutput
43617a3babSopenharmony_ci{
44617a3babSopenharmony_ci[[vk::location(2)]]	float4 Pos : SV_POSITION;
45617a3babSopenharmony_ci[[vk::location(0)]] float3 Normal : NORMAL0;
46617a3babSopenharmony_ci[[vk::location(1)]] float2 UV : TEXCOORD0;
47617a3babSopenharmony_ci};
48617a3babSopenharmony_ci
49617a3babSopenharmony_cistruct ConstantsHSOutput
50617a3babSopenharmony_ci{
51617a3babSopenharmony_ci    float TessLevelOuter[4] : SV_TessFactor;
52617a3babSopenharmony_ci    float TessLevelInner[2] : SV_InsideTessFactor;
53617a3babSopenharmony_ci};
54617a3babSopenharmony_ci
55617a3babSopenharmony_cistruct DSOutput
56617a3babSopenharmony_ci{
57617a3babSopenharmony_ci	float4 Pos : SV_POSITION;
58617a3babSopenharmony_ci[[vk::location(0)]] float3 Normal : NORMAL0;
59617a3babSopenharmony_ci[[vk::location(1)]] float2 UV : TEXCOORD0;
60617a3babSopenharmony_ci[[vk::location(2)]] float3 ViewVec : TEXCOORD1;
61617a3babSopenharmony_ci[[vk::location(3)]] float3 LightVec : TEXCOORD2;
62617a3babSopenharmony_ci[[vk::location(4)]] float3 EyePos : POSITION1;
63617a3babSopenharmony_ci[[vk::location(5)]] float3 WorldPos : POSITION0;
64617a3babSopenharmony_ci};
65617a3babSopenharmony_ci
66617a3babSopenharmony_ci[domain("quad")]
67617a3babSopenharmony_ciDSOutput main(ConstantsHSOutput input, float2 TessCoord : SV_DomainLocation, const OutputPatch<HSOutput, 4> patch)
68617a3babSopenharmony_ci{
69617a3babSopenharmony_ci	// Interpolate UV coordinates
70617a3babSopenharmony_ci	DSOutput output = (DSOutput)0;
71617a3babSopenharmony_ci	float2 uv1 = lerp(patch[0].UV, patch[1].UV, TessCoord.x);
72617a3babSopenharmony_ci	float2 uv2 = lerp(patch[3].UV, patch[2].UV, TessCoord.x);
73617a3babSopenharmony_ci	output.UV = lerp(uv1, uv2, TessCoord.y);
74617a3babSopenharmony_ci
75617a3babSopenharmony_ci	float3 n1 = lerp(patch[0].Normal, patch[1].Normal, TessCoord.x);
76617a3babSopenharmony_ci	float3 n2 = lerp(patch[3].Normal, patch[2].Normal, TessCoord.x);
77617a3babSopenharmony_ci	output.Normal = lerp(n1, n2, TessCoord.y);
78617a3babSopenharmony_ci
79617a3babSopenharmony_ci	// Interpolate positions
80617a3babSopenharmony_ci	float4 pos1 = lerp(patch[0].Pos, patch[1].Pos, TessCoord.x);
81617a3babSopenharmony_ci	float4 pos2 = lerp(patch[3].Pos, patch[2].Pos, TessCoord.x);
82617a3babSopenharmony_ci	float4 pos = lerp(pos1, pos2, TessCoord.y);
83617a3babSopenharmony_ci	// Displace
84617a3babSopenharmony_ci	pos.y -= displacementMapTexture.SampleLevel(displacementMapSampler, output.UV, 0.0).r * ubo.displacementFactor;
85617a3babSopenharmony_ci	// Perspective projection
86617a3babSopenharmony_ci	output.Pos = mul(ubo.projection, mul(ubo.modelview, pos));
87617a3babSopenharmony_ci
88617a3babSopenharmony_ci	// Calculate vectors for lighting based on tessellated position
89617a3babSopenharmony_ci	output.ViewVec = -pos.xyz;
90617a3babSopenharmony_ci	output.LightVec = normalize(ubo.lightPos.xyz + output.ViewVec);
91617a3babSopenharmony_ci	output.WorldPos = pos.xyz;
92617a3babSopenharmony_ci	output.EyePos = mul(ubo.modelview, pos).xyz;
93617a3babSopenharmony_ci	return output;
94617a3babSopenharmony_ci}
95