Skip to content

Commit 31bb5ea

Browse files
tangent-vectorslangbotcsyonghe
authored
Add an example for reflection of parameter blocks (#6161)
* Add an example for reflection of parameter blocks The example loads a shader program, compiles it for Vulkan, and then uses reflection information to set up descriptor set layouts and a pipeline layout. It then validates that the pipeline layout that is created is compatible with the compiled code, by using them together to make a pipeline state object with the validation layer enabled. The basic flow of the application follows the presentation in the companion article, and references its sections. This is example could be expanded in a few ways: * A D3D12 path could be added, to show the comparable operations for creating a root signature from reflection data * The existing shader could be modified to touch/use more of its parameter data, to help ensure that any errors would be caught by the validation layer * The set of shader files/modules that get loaded automatically could be expanded, to test more cases * The code could be expanded to handle more than just compute shaders, by allowing for multiple-entry-point files to be loaded for rasterization or ray-tracing pipelines * format code * fixup: build errors * format code * fixups for build * fixup * fixup --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com> Co-authored-by: Yong He <yonghe@outlook.com>
1 parent f030a5a commit 31bb5ea

File tree

7 files changed

+1251
-0
lines changed

7 files changed

+1251
-0
lines changed

examples/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ if(SLANG_ENABLE_EXAMPLES)
107107
example(ray-tracing WIN32_EXECUTABLE)
108108
example(ray-tracing-pipeline WIN32_EXECUTABLE)
109109
example(reflection-api)
110+
example(reflection-parameter-blocks LINK_WITH_PRIVATE Vulkan-Headers)
110111
example(shader-object)
111112
example(shader-toy WIN32_EXECUTABLE)
112113
example(triangle WIN32_EXECUTABLE)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Setting up Parameter Blocks Using Reflection
2+
============================================
3+
4+
This example shows how to make use of the `ParameterBlock<>` type in Slang shader code, in conjunction with the Slang reflection API, in order to easily set up descriptor sets for Vulkan and descriptor tables for D3D12.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
12+
struct Mesh
13+
{
14+
float4x4 modelToWorld;
15+
float4x4 modelToWorld_inverseTranspose;
16+
}
17+
18+
struct Material
19+
{
20+
Texture2D albedoMap;
21+
Texture2D glossMap;
22+
Texture2D normalMap;
23+
SamplerState sampler;
24+
}
25+
26+
interface ILight
27+
{
28+
}
29+
30+
struct DirectionalLight : ILight
31+
{
32+
float3 dir;
33+
float3 intensity;
34+
}
35+
36+
struct ShadowedLight<L : ILight> : ILight
37+
{
38+
L light;
39+
Texture2D shadowMap;
40+
SamplerComparisonState shadowSampler;
41+
float4x4 worldToShadow;
42+
}
43+
44+
struct EnvironmentMap
45+
{
46+
TextureCube texture;
47+
SamplerState sampler;
48+
}
49+
50+
struct Environment
51+
{
52+
ShadowedLight<DirectionalLight> sunLight;
53+
EnvironmentMap envMap;
54+
RWStructuredBuffer<float4> output;
55+
}
56+
57+
struct 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+
80+
void use(inout float4 r, float4 v) { r += v; }
81+
void use(inout float4 r, float3 v) { r.xyz += v; }
82+
83+
void use(inout float4 r, Texture2D t, SamplerState s)
84+
{
85+
use(r, t.SampleLevel(s, r.xy, 0));
86+
}
87+
88+
void use(inout float4 r, RWStructuredBuffer<float4> b)
89+
{
90+
use(r, b[int(r.x)]);
91+
b[int(r.x)] = r;
92+
}
93+
94+
void use(inout float4 r, Environment e)
95+
{
96+
use(r, e.sunLight.light.dir);
97+
use(r, e.output);
98+
}
99+
100+
void use(inout float4 r, View v)
101+
{
102+
use(r, v.worldToView[0]);
103+
}
104+
105+
void use(inout float4 r, Material m)
106+
{
107+
use(r, m.normalMap, m.sampler);
108+
}
109+
110+
void use(inout float4 r, Mesh m)
111+
{
112+
use(r, m.modelToWorld[0]);
113+
}

0 commit comments

Comments
 (0)