Skip to content

Commit b9d3f3e

Browse files
committed
HLSL: Added support for tessellation
Emit HLSL hull (tesc) and domain (tese) shaders from Vulkan-style SPIR-V where per-CP and patch-constant work share one entry point gated by `if (gl_InvocationID == 0)` (the dxc/glslang pattern). The hull entry is split into tesc_main + tesc_main_patch so [patchconstantfunc] can point at the patch half. Done via in-place block mutation: pre-scan for the gate header, OpControlBarrier and per-CP OpFunctionCall; for each pass truncate the header's ops and redirect its terminator to the merge block (per-CP) or the then-block (patch), run emit_block_chain, restore state. Also adds HS/DS I/O struct emission, builtin lowering for SV_TessFactor / SV_InsideTessFactor / SV_DomainLocation / SV_OutputControlPointID / InvocationId, a patch-constant wrapper that marshals InputPatch/OutputPatch into globals, and [domain] / [partitioning] / [outputtopology] attribute emission with sensible defaults when execution modes are absent (e.g. TCS compiled without a linked TES). Includes shaders-hlsl/{tesc,tese}/basic.{tesc,tese} reference tests.
1 parent 4d4b79b commit b9d3f3e

6 files changed

Lines changed: 992 additions & 25 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
static uint gl_InvocationID;
2+
static float gl_TessLevelOuter[4];
3+
static float gl_TessLevelInner[2];
4+
static float3 vtxColor[3];
5+
6+
struct SPIRV_Cross_Input
7+
{
8+
};
9+
10+
struct SPIRV_Cross_Output
11+
{
12+
float3 vtxColor : TEXCOORD0;
13+
};
14+
15+
struct SPIRV_Cross_PatchConstant
16+
{
17+
float gl_TessLevelOuter[3] : SV_TessFactor;
18+
float gl_TessLevelInner[1] : SV_InsideTessFactor;
19+
};
20+
21+
void tesc_main()
22+
{
23+
float _18 = float(gl_InvocationID);
24+
float3 _19 = _18.xxx;
25+
vtxColor[gl_InvocationID] = _19;
26+
}
27+
28+
void tesc_main_patch()
29+
{
30+
float _18 = float(gl_InvocationID);
31+
float3 _19 = _18.xxx;
32+
vtxColor[gl_InvocationID] = _19;
33+
gl_TessLevelOuter[0] = 1.0f;
34+
gl_TessLevelOuter[1] = 2.0f;
35+
gl_TessLevelOuter[2] = 3.0f;
36+
gl_TessLevelInner[0] = 4.0f;
37+
}
38+
39+
[domain("tri")]
40+
[partitioning("integer")]
41+
[outputtopology("triangle_cw")]
42+
[outputcontrolpoints(3)]
43+
[patchconstantfunc("patch_constant")]
44+
SPIRV_Cross_Output main(InputPatch<SPIRV_Cross_Input, 3> patch, uint uCPID : SV_OutputControlPointID)
45+
{
46+
gl_InvocationID = uCPID;
47+
tesc_main();
48+
SPIRV_Cross_Output stage_output;
49+
stage_output.vtxColor = vtxColor[gl_InvocationID];
50+
return stage_output;
51+
}
52+
53+
SPIRV_Cross_PatchConstant patch_constant(InputPatch<SPIRV_Cross_Input, 3> patch)
54+
{
55+
gl_InvocationID = 0u;
56+
tesc_main_patch();
57+
SPIRV_Cross_PatchConstant patch_output;
58+
patch_output.gl_TessLevelOuter[0] = gl_TessLevelOuter[0];
59+
patch_output.gl_TessLevelOuter[1] = gl_TessLevelOuter[1];
60+
patch_output.gl_TessLevelOuter[2] = gl_TessLevelOuter[2];
61+
patch_output.gl_TessLevelInner[0] = gl_TessLevelInner[0];
62+
return patch_output;
63+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
static float4 gl_Position;
2+
static float3 gl_TessCoord;
3+
static float3 outColor;
4+
5+
struct SPIRV_Cross_Input
6+
{
7+
};
8+
9+
struct SPIRV_Cross_Output
10+
{
11+
float3 outColor : TEXCOORD0;
12+
float4 gl_Position : SV_Position;
13+
};
14+
15+
struct SPIRV_Cross_PatchConstant
16+
{
17+
float gl_TessLevelOuter[3] : SV_TessFactor;
18+
float gl_TessLevelInner[1] : SV_InsideTessFactor;
19+
};
20+
21+
void tese_main()
22+
{
23+
gl_Position = float4(gl_TessCoord, 1.0f);
24+
outColor = gl_TessCoord;
25+
}
26+
27+
[domain("tri")]
28+
SPIRV_Cross_Output main(SPIRV_Cross_PatchConstant pc, const OutputPatch<SPIRV_Cross_Input, 3> patch, float3 domain : SV_DomainLocation)
29+
{
30+
gl_TessCoord = domain;
31+
tese_main();
32+
SPIRV_Cross_Output stage_output;
33+
stage_output.gl_Position = gl_Position;
34+
stage_output.outColor = outColor;
35+
return stage_output;
36+
}

shaders-hlsl/tesc/basic.tesc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#version 450
2+
3+
layout(vertices = 3) out;
4+
5+
layout(location = 0) out vec3 vtxColor[];
6+
7+
void main()
8+
{
9+
vtxColor[gl_InvocationID] = vec3(float(gl_InvocationID));
10+
11+
barrier();
12+
13+
if (gl_InvocationID == 0)
14+
{
15+
gl_TessLevelOuter[0] = 1.0;
16+
gl_TessLevelOuter[1] = 2.0;
17+
gl_TessLevelOuter[2] = 3.0;
18+
gl_TessLevelInner[0] = 4.0;
19+
}
20+
}

shaders-hlsl/tese/basic.tese

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#version 450
2+
3+
layout(triangles, fractional_odd_spacing, cw) in;
4+
5+
layout(location = 0) out vec3 outColor;
6+
7+
void main()
8+
{
9+
gl_Position = vec4(gl_TessCoord, 1.0);
10+
outColor = gl_TessCoord;
11+
}

0 commit comments

Comments
 (0)