-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
8000e0e
commit a3b5108
Showing
7 changed files
with
1,292 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Setting up Parameter Blocks Using Reflection | ||
============================================ | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// shader.slang | ||
|
||
// This module is part of the `reflection-parameter-blocks` | ||
// example program. | ||
// | ||
// This module is split out from the files that define | ||
// individual programs, so that we can share some type | ||
// definitions and utility functions between all of | ||
// the programs and keep them focused on just defining | ||
// the shader entry points. | ||
|
||
struct Mesh | ||
{ | ||
float4x4 modelToWorld; | ||
float4x4 modelToWorld_inverseTranspose; | ||
} | ||
|
||
struct Material | ||
{ | ||
Texture2D albedoMap; | ||
Texture2D glossMap; | ||
Texture2D normalMap; | ||
SamplerState sampler; | ||
} | ||
|
||
interface ILight | ||
{ | ||
} | ||
|
||
struct DirectionalLight : ILight | ||
{ | ||
float3 dir; | ||
float3 intensity; | ||
} | ||
|
||
struct ShadowedLight<L : ILight> : ILight | ||
{ | ||
L light; | ||
Texture2D shadowMap; | ||
SamplerComparisonState shadowSampler; | ||
float4x4 worldToShadow; | ||
} | ||
|
||
struct EnvironmentMap | ||
{ | ||
TextureCube texture; | ||
SamplerState sampler; | ||
} | ||
|
||
struct Environment | ||
{ | ||
ShadowedLight<DirectionalLight> sunLight; | ||
EnvironmentMap envMap; | ||
RWStructuredBuffer<float4> output; | ||
} | ||
|
||
struct View | ||
{ | ||
float4x4 worldToView; | ||
float4x4 viewToProj; | ||
} | ||
|
||
// While the Slang compilation library will *reflect* all of | ||
// the shader parameters that a program declares, | ||
// back-ends (such as the SPIR-V code generator) will often | ||
// strip out parameters that are not used as part of the | ||
// computation that a shader performs. | ||
// | ||
// When shader parameters are stripped from the output | ||
// binary code, the runtime system for a particular API | ||
// (e.g., the Vulkan validation layer) cannot check | ||
// whether a program is correctly handling the binding | ||
// of those parameters. | ||
// | ||
// Our example entry points will thus make use of some | ||
// utility routines that serve the purpose of allowing | ||
// us to ensure that specific parameters are seen as | ||
// "used" during code generation. | ||
|
||
void use(inout float4 r, float4 v) { r += v; } | ||
void use(inout float4 r, float3 v) { r.xyz += v; } | ||
|
||
void use(inout float4 r, Texture2D t, SamplerState s) | ||
{ | ||
use(r, t.SampleLevel(s, r.xy, 0)); | ||
} | ||
|
||
void use(inout float4 r, RWStructuredBuffer<float4> b) | ||
{ | ||
use(r, b[int(r.x)]); | ||
b[int(r.x)] = r; | ||
} | ||
|
||
void use(inout float4 r, Environment e) | ||
{ | ||
use(r, e.sunLight.light.dir); | ||
use(r, e.output); | ||
} | ||
|
||
void use(inout float4 r, View v) | ||
{ | ||
use(r, v.worldToView[0]); | ||
} | ||
|
||
void use(inout float4 r, Material m) | ||
{ | ||
use(r, m.normalMap, m.sampler); | ||
} | ||
|
||
void use(inout float4 r, Mesh m) | ||
{ | ||
use(r, m.modelToWorld[0]); | ||
} |
Oops, something went wrong.