yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
31bb5eaa0
master
1// shader.slang 2 3// This module is part of the `reflection-parameter-blocks` 4// example program. 5// 6// This module is split out from the files that define 7// individual programs, so that we can share some type 8// definitions and utility functions between all of 9// the programs and keep them focused on just defining 10// the shader entry points. 11 12struct Mesh 13{ 14 float4x4 modelToWorld; 15 float4x4 modelToWorld_inverseTranspose; 16} 17 18struct Material 19{ 20 Texture2D albedoMap; 21 Texture2D glossMap; 22 Texture2D normalMap; 23 SamplerState sampler; 24} 25 26interface ILight 27{ 28} 29 30struct DirectionalLight : ILight 31{ 32 float3 dir; 33 float3 intensity; 34} 35 36struct ShadowedLight<L : ILight> : ILight 37{ 38 L light; 39 Texture2D shadowMap; 40 SamplerComparisonState shadowSampler; 41 float4x4 worldToShadow; 42} 43 44struct EnvironmentMap 45{ 46 TextureCube texture; 47 SamplerState sampler; 48} 49 50struct Environment 51{ 52 ShadowedLight<DirectionalLight> sunLight; 53 EnvironmentMap envMap; 54 RWStructuredBuffer<float4> output; 55} 56 57struct View 58{ 59 float4x4 worldToView; 60 float4x4 viewToProj; 61} 62 63// While the Slang compilation library will *reflect* all of 64// the shader parameters that a program declares, 65// back-ends (such as the SPIR-V code generator) will often 66// strip out parameters that are not used as part of the 67// computation that a shader performs. 68// 69// When shader parameters are stripped from the output 70// binary code, the runtime system for a particular API 71// (e.g., the Vulkan validation layer) cannot check 72// whether a program is correctly handling the binding 73// of those parameters. 74// 75// Our example entry points will thus make use of some 76// utility routines that serve the purpose of allowing 77// us to ensure that specific parameters are seen as 78// "used" during code generation. 79 80void use(inout float4 r, float4 v) { r += v; } 81void use(inout float4 r, float3 v) { r.xyz += v; } 82 83void use(inout float4 r, Texture2D t, SamplerState s) 84{ 85 use(r, t.SampleLevel(s, r.xy, 0)); 86} 87 88void use(inout float4 r, RWStructuredBuffer<float4> b) 89{ 90 use(r, b[int(r.x)]); 91 b[int(r.x)] = r; 92} 93 94void use(inout float4 r, Environment e) 95{ 96 use(r, e.sunLight.light.dir); 97 use(r, e.output); 98} 99 100void use(inout float4 r, View v) 101{ 102 use(r, v.worldToView[0]); 103} 104 105void use(inout float4 r, Material m) 106{ 107 use(r, m.normalMap, m.sampler); 108} 109 110void use(inout float4 r, Mesh m) 111{ 112 use(r, m.modelToWorld[0]); 113}