yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f4d5372d3
master
1// constexpr.slang 2//TEST(compute):COMPARE_COMPUTE_EX:-slang -gcompute -shaderobj 3//DISABLED://TEST(compute, vulkan):COMPARE_COMPUTE_EX:-vk -gcompute -shaderobj 4//TEST(compute):COMPARE_COMPUTE_EX:-mtl -gcompute -shaderobj 5//TEST(compute):COMPARE_COMPUTE_EX:-wgpu -gcompute -shaderobj 6 7//TEST_INPUT: Texture2D(size=4, content = one):name tex 8//TEST_INPUT: Sampler:name samp 9//TEST_INPUT: ubuffer(data=[0 0], stride=4):out,name outputBuffer 10 11// Note: Vulkan version of this test is disabled pending adding 12// support for rendering tests to the harness. 13 14Texture2D tex; 15SamplerState samp; 16RWStructuredBuffer<float> outputBuffer; 17 18cbuffer Uniforms 19{ 20 float4x4 modelViewProjection; 21} 22 23struct AssembledVertex 24{ 25 float3 position; 26 float3 color; 27 float2 uv; 28}; 29 30struct CoarseVertex 31{ 32 float3 color; 33 float2 uv; 34}; 35 36struct Fragment 37{ 38 float4 color; 39}; 40 41 42// Vertex Shader 43 44struct VertexStageInput 45{ 46 AssembledVertex assembledVertex : A; 47}; 48 49struct VertexStageOutput 50{ 51 CoarseVertex coarseVertex : CoarseVertex; 52 float4 sv_position : SV_Position; 53}; 54 55[shader("vertex")] 56VertexStageOutput vertexMain(VertexStageInput input) 57{ 58 VertexStageOutput output; 59 60 float3 position = input.assembledVertex.position; 61 float3 color = input.assembledVertex.color; 62 63 output.coarseVertex.color = color; 64 output.sv_position = mul(modelViewProjection, float4(position, 1.0)); 65 output.coarseVertex.uv = input.assembledVertex.uv; 66 return output; 67} 68 69// Fragment Shader 70 71struct FragmentStageInput 72{ 73 CoarseVertex coarseVertex : CoarseVertex; 74}; 75 76struct FragmentStageOutput 77{ 78 Fragment fragment : SV_Target; 79}; 80 81[shader("fragment")] 82FragmentStageOutput fragmentMain(FragmentStageInput input) 83{ 84 // The texel offset argument to `Texture2D.Sample` is 85 // required to be `constexpr`. This test is going to 86 // check that we correctly propagate this constraint 87 // backward to the value `a`. 88 // 89 // Because the HLSL compiler(s) already do this kind 90 // of propagation, the only real way to test this 91 // will be to target Vulkan, where the standard 92 // GLSL compiler gives an error message rather than 93 // infer `const`-ness. 94 95 uint a = 0; 96 constexpr uint b = 1; 97 98 uint2 ab = uint2(a,b); 99 100 FragmentStageOutput output; 101 102 float3 color = input.coarseVertex.color; 103 float2 uv = input.coarseVertex.uv; 104 output.fragment.color = float4(color, 1.0); 105 106 float4 val = float4(color, 1.0); 107 val = val - 16*tex.Sample(samp, uv, int2(ab)); 108 109 outputBuffer[0] = 1; 110 111 if(val.x < 0) 112 discard; 113 114 outputBuffer[1] = 1; 115 return output; 116}