forked from StereoKit/StereoKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocSpecConstants.cs
More file actions
47 lines (43 loc) · 1.71 KB
/
Copy pathDocSpecConstants.cs
File metadata and controls
47 lines (43 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using StereoKit;
class DocSpecConstants : ITest
{
/// :CodeSample: Material.SetInt Material.SetFloat Material.SetBool Material.SetUInt
/// ### Specialization constants
/// If a shader declares specialization constants with HLSL's
/// `[[vk::constant_id(N)]]`, StereoKit exposes them through the ordinary
/// scalar setters. The name you pass must match the HLSL variable name.
///
/// ```hlsl
/// [[vk::constant_id(0)]] const int COLOR_STEPS = 2;
/// [[vk::constant_id(1)]] const float BRIGHTNESS = 0.25;
/// [[vk::constant_id(2)]] const bool USE_TINT = true;
/// [[vk::constant_id(3)]] const uint BLUE_SCALE = 4;
/// ```
///
/// Unlike a normal material parameter, a spec constant is baked into the
/// shader pipeline. Setting one to a new value rebuilds that pipeline (a
/// heavier operation than a uniform write), so prefer setting them at
/// load time rather than every frame. Re-setting the same value is free.
/// Two materials sharing one shader with different spec-constant values are
/// distinct pipeline variants — handy for feature toggles or loop/array
/// sizes the driver can then fold at compile time.
Material variantA;
Material variantB;
public void Initialize()
{
Shader shader = Shader.FromFile("spec_constant_test.hlsl");
variantA = new Material(shader); // uses the HLSL defaults
variantB = new Material(shader);
variantB.SetInt ("COLOR_STEPS", 4);
variantB.SetFloat("BRIGHTNESS", 0.6f);
variantB.SetBool ("USE_TINT", false);
variantB.SetUInt ("BLUE_SCALE", 12);
}
/// :End:
public void Shutdown() { }
public void Step()
{
Mesh.Sphere.Draw(variantA, Matrix.TS(-0.15f, 0, -0.5f, 0.2f));
Mesh.Sphere.Draw(variantB, Matrix.TS( 0.15f, 0, -0.5f, 0.2f));
}
}