yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f02b08490
master
1//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK): -task -output-using-type -dx12 -profile sm_6_6 -render-features mesh-shader 2//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK): -task -output-using-type -vk -profile sm_6_5 -render-features mesh-shader 3//TEST:SIMPLE(filecheck=HLSL):-target hlsl -entry meshMain -stage mesh 4//TEST:SIMPLE(filecheck=CHECK_SPV):-target spirv -entry taskMain -stage amplification 5 6// CHECK_SPV: OpEntryPoint 7// CHECK_SPV: TaskPayloadWorkgroupEXT 8 9 10// To test a simple mesh shader, we'll generate 4 triangles, the vertices of 11// each one will hold the triangle index and a value (the square). The fragment 12// shader will write the value to the specified index of the output buffer. 13 14// CHECK: 0 15// CHECK-NEXT: 1 16// CHECK-NEXT: 8 17// CHECK-NEXT: 27 18 19//TEST_INPUT: ubuffer(data=[0 0 0 0], stride=4):out,name outputBuffer 20 21RWStructuredBuffer<float> outputBuffer; 22 23cbuffer Uniforms 24{ 25 float4x4 modelViewProjection; 26} 27 28// 29// Task shader 30// 31 32struct MeshPayload 33{ 34 int exponent; 35}; 36 37const static uint AMPLIFICATION_NUM_THREADS_X = 1; 38 39[numthreads(AMPLIFICATION_NUM_THREADS_X, 1, 1)] 40[shader("amplification")] 41void taskMain(in uint tig : SV_GroupIndex) 42{ 43 MeshPayload p; 44 p.exponent = select(AMPLIFICATION_NUM_THREADS_X == WorkgroupSize().x, 3, 0); 45 DispatchMesh(1,1,1,p); 46} 47 48 49// 50// Mesh shader 51// 52 53const static float2 positions[3] = { 54 float2(0.0, -0.5), 55 float2(0.5, 0.5), 56 float2(-0.5, 0.5) 57}; 58 59const static float3 colors[3] = { 60 float3(1.0, 1.0, 0.0), 61 float3(0.0, 1.0, 1.0), 62 float3(1.0, 0.0, 1.0) 63}; 64 65struct Vertex 66{ 67 float4 pos : SV_Position; 68 float3 color : Color; 69 int index : Index; 70 int value : Value; 71}; 72 73const static uint MAX_VERTS = 12; 74const static uint MAX_PRIMS = 4; 75 76const static uint MESH_NUM_THREADS_X = 12; 77 78[outputtopology("triangle")] 79[numthreads(MESH_NUM_THREADS_X, 1, 1)] 80void meshMain( 81 in uint tig : SV_GroupIndex, 82 in payload MeshPayload meshPayload, 83 // Check that we correctly generate the specific 'in payload' that HLSL 84 // requires: 85 // HLSL: , in payload MeshPayload 86 OutputVertices<Vertex, MAX_VERTS> verts, 87 OutputIndices<uint3, MAX_PRIMS> triangles) 88{ 89 const uint numVertices = 12; 90 const uint numPrimitives = 4; 91 SetMeshOutputCounts(numVertices, numPrimitives); 92 93 if(tig < numVertices) 94 { 95 const int tri = select(WorkgroupSize().x == MESH_NUM_THREADS_X, tig / 3, -1); 96 verts[tig] = {float4(positions[tig % 3], 0, 1), colors[tig % 3], tri, int(pow(tri, meshPayload.exponent))}; 97 } 98 99 if(tig < numPrimitives) 100 triangles[tig] = tig * 3 + uint3(0,1,2); 101} 102 103// 104// Fragment Shader 105// 106 107struct Fragment 108{ 109 float4 color : SV_Target; 110}; 111 112Fragment fragmentMain(Vertex input) 113{ 114 outputBuffer[input.index] = input.value; 115 116 Fragment output; 117 output.color = float4(input.color, 1.0); 118 return output; 119}