yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
db44c1b73
master
1//TEST:SIMPLE(filecheck=HULL): -target spirv -stage hull -entry hullMain 2//TEST:SIMPLE(filecheck=DOMAIN): -target spirv -stage domain -entry domainMain 3//TEST:SIMPLE(filecheck=DEBUGINFO): -target spirv -g2 -O0 -stage hull -entry hullMain 4//TEST:SIMPLE(filecheck=DEBUGINFO): -target spirv -g2 -O0 -stage domain -entry domainMain 5 6// HULL-DAG: OpExecutionMode %hullMain SpacingEqual 7// HULL-DAG: OpExecutionMode %hullMain OutputVertices 4 8// HULL-DAG: OpExecutionMode %hullMain VertexOrderCw 9// HULL-DAG: OpExecutionMode %hullMain Quads 10 11// HULL: OpDecorate %gl_TessLevelOuter BuiltIn TessLevelOuter 12// HULL: OpDecorate %gl_TessLevelOuter Patch 13// HULL: OpDecorate %gl_TessLevelInner BuiltIn TessLevelInner 14// HULL: OpDecorate %gl_TessLevelInner Patch 15 16// HULL: OpControlBarrier %uint_2 %uint_4 %uint_0 17 18// HULL: OpStore %gl_TessLevelOuter 19// HULL: OpStore %gl_TessLevelInner 20 21// DOMAIN-DAG: OpExecutionMode %domainMain SpacingEqual 22// DOMAIN-DAG: OpExecutionMode %domainMain Quads 23 24// Entry point params should be emitted as local-variable debug info 25// DEBUGINFO: %[[patchStr:[0-9]+]] = OpString "patch" 26// DEBUGINFO: %[[patchId:[a-zA-Z0-9_]+]] = {{.*}} DebugLocalVariable %[[patchStr]] 27// DEBUGINFO: DebugValue %[[patchId]] 28 29struct VS_OUT 30{ 31 float3 position : POSITION; 32}; 33 34struct HS_OUT 35{ 36 float3 position : POSITION; 37}; 38 39struct HSC_OUT 40{ 41 float EdgeTessFactor[4] : SV_TessFactor; 42 float InsideTessFactor[2] : SV_InsideTessFactor; 43}; 44 45struct DS_OUT 46{ 47 float4 position : SV_Position; 48}; 49 50// Hull Shader (HS) 51[domain("quad")] 52[partitioning("integer")] 53[outputtopology("triangle_cw")] 54[outputcontrolpoints(4)] 55[patchconstantfunc("constants")] 56HS_OUT hullMain(InputPatch<VS_OUT, 4> patch, uint i : SV_OutputControlPointID) 57{ 58 HS_OUT o; 59 o.position = patch[i].position; 60 return o; 61} 62 63HSC_OUT constants(InputPatch<VS_OUT, 4> patch) 64{ 65 float3 p0 = patch[0].position; 66 float3 p1 = patch[1].position; 67 float3 p2 = patch[2].position; 68 float3 p3 = patch[3].position; 69 70 HSC_OUT o; 71 o.EdgeTessFactor[0] = dot(p0, p1); 72 o.EdgeTessFactor[1] = dot(p0, p3); 73 o.EdgeTessFactor[2] = dot(p2, p3); 74 o.EdgeTessFactor[3] = dot(p1, p2); 75 o.InsideTessFactor[0] = lerp(o.EdgeTessFactor[1], o.EdgeTessFactor[3], 0.5); 76 o.InsideTessFactor[1] = lerp(o.EdgeTessFactor[0], o.EdgeTessFactor[2], 0.5); 77 return o; 78} 79 80[domain("quad")] 81DS_OUT domainMain( 82 float2 uv : SV_DomainLocation, // Tessellated coordinates (u, v) 83 const OutputPatch<HS_OUT, 4> patch, // Control points from the hull shader 84 const HSC_OUT patchConstants // Patch constants calculated by the hull shader 85) 86{ 87 DS_OUT o; 88 89 // Interpolate the position of the tessellated point within the patch 90 float3 p0 = patch[0].position; 91 float3 p1 = patch[1].position; 92 float3 p2 = patch[2].position; 93 float3 p3 = patch[3].position; 94 95 // Bilinear interpolation of the position in the quad 96 float3 interpolatedPosition = 97 p0 * (1 - uv.x) * (1 - uv.y) 98 + p1 * uv.x * (1 - uv.y) 99 + p3 * uv.x * uv.y 100 + p2 * (1 - uv.x) * uv.y; 101 102 // Output final position in clip space 103 o.position = float4(interpolatedPosition, 1.0); 104 return o; 105}