Skip to content

Commit 0460eb6

Browse files
jkwak-workcsyonghe
andauthored
Force inline functions that takes InputPatch and OutputPatch (#6407)
This commit inlines functions that takes InputPatch and OutputPatch as the function parameter. Co-authored-by: Yong He <yonghe@outlook.com>
1 parent a023792 commit 0460eb6

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

source/slang/slang-ir-specialize-resources.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1359,6 +1359,10 @@ bool isIllegalGLSLParameterType(IRType* type)
13591359
return true;
13601360
if (as<IRDynamicResourceType>(type))
13611361
return true;
1362+
if (as<IRHLSLInputPatchType>(type))
1363+
return true;
1364+
if (as<IRHLSLOutputPatchType>(type))
1365+
return true;
13621366
return false;
13631367
}
13641368

source/slang/slang-ir.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -5259,6 +5259,11 @@ IRInst* IRBuilder::emitElementAddress(IRInst* basePtr, IRInst* index)
52595259
SLANG_ASSERT(as<IRIntLit>(index));
52605260
type = (IRType*)tupleType->getOperand(getIntVal(index));
52615261
}
5262+
else if (auto hlslInputPatchType = as<IRHLSLInputPatchType>(valueType))
5263+
{
5264+
type = hlslInputPatchType->getElementType();
5265+
}
5266+
52625267
SLANG_RELEASE_ASSERT(type);
52635268
auto inst = createInst<IRGetElementPtr>(
52645269
this,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
//TEST:SIMPLE(filecheck=HULL): -target spirv -stage hull -entry hullMain
2+
//TEST:SIMPLE(filecheck=DOMAIN): -target spirv -stage domain -entry domainMain
3+
4+
// Testing if `InputPatch` and `OutputPatch` can be used as function arguments.
5+
// `[ForceInline]` can be used to workaround but it should work without it.
6+
7+
// HULL: OpEntryPoint TessellationControl %hullMain
8+
// HULL: = OpVariable %{{[a-zA-Z_0-9]*}} Input
9+
10+
// DOMAIN: OpEntryPoint TessellationEvaluation %domainMain
11+
// DOMAIN: = OpVariable %{{[a-zA-Z_0-9]*}} Output
12+
13+
struct VS_OUT
14+
{
15+
float3 position : POSITION;
16+
};
17+
18+
struct HS_OUT
19+
{
20+
float3 position : POSITION;
21+
};
22+
23+
struct HSC_OUT
24+
{
25+
float EdgeTessFactor[4] : SV_TessFactor;
26+
float InsideTessFactor[2] : SV_InsideTessFactor;
27+
};
28+
29+
struct DS_OUT
30+
{
31+
float4 position : SV_Position;
32+
};
33+
34+
35+
VS_OUT GetInputPatch(InputPatch<VS_OUT, 4> patch, int index)
36+
{
37+
return patch[index];
38+
}
39+
40+
// Hull Shader (HS)
41+
[domain("quad")]
42+
[partitioning("integer")]
43+
[outputtopology("triangle_cw")]
44+
[outputcontrolpoints(4)]
45+
[patchconstantfunc("constants")]
46+
HS_OUT hullMain(InputPatch<VS_OUT, 4> patch, uint i : SV_OutputControlPointID)
47+
{
48+
HS_OUT o;
49+
o.position = patch[i].position;
50+
return o;
51+
}
52+
53+
HSC_OUT constants(InputPatch<VS_OUT, 4> patch)
54+
{
55+
float3 p0 = GetInputPatch(patch, 0).position;
56+
float3 p1 = patch[1].position;
57+
float3 p2 = patch[2].position;
58+
float3 p3 = patch[3].position;
59+
60+
HSC_OUT o;
61+
o.EdgeTessFactor[0] = dot(p0, p1);
62+
o.EdgeTessFactor[1] = dot(p0, p3);
63+
o.EdgeTessFactor[2] = dot(p2, p3);
64+
o.EdgeTessFactor[3] = dot(p1, p2);
65+
o.InsideTessFactor[0] = lerp(o.EdgeTessFactor[1], o.EdgeTessFactor[3], 0.5);
66+
o.InsideTessFactor[1] = lerp(o.EdgeTessFactor[0], o.EdgeTessFactor[2], 0.5);
67+
return o;
68+
}
69+
70+
HS_OUT GetOutputPatch(const OutputPatch<HS_OUT, 4> patch, int index)
71+
{
72+
return patch[index];
73+
}
74+
75+
[domain("quad")]
76+
DS_OUT domainMain(
77+
float2 uv : SV_DomainLocation, // Tessellated coordinates (u, v)
78+
const OutputPatch<HS_OUT, 4> patch, // Control points from the hull shader
79+
const HSC_OUT patchConstants // Patch constants calculated by the hull shader
80+
)
81+
{
82+
DS_OUT o;
83+
84+
// Interpolate the position of the tessellated point within the patch
85+
float3 p0 = GetOutputPatch(patch, 0).position;
86+
float3 p1 = patch[1].position;
87+
float3 p2 = patch[2].position;
88+
float3 p3 = patch[3].position;
89+
90+
// Bilinear interpolation of the position in the quad
91+
float3 interpolatedPosition =
92+
p0 * (1 - uv.x) * (1 - uv.y)
93+
+ p1 * uv.x * (1 - uv.y)
94+
+ p3 * uv.x * uv.y
95+
+ p2 * (1 - uv.x) * uv.y;
96+
97+
// Output final position in clip space
98+
o.position = float4(interpolatedPosition, 1.0);
99+
return o;
100+
}

0 commit comments

Comments
 (0)