Skip to content

Commit 651b63d

Browse files
committed
Add State Object database sample.
1 parent 1c57270 commit 651b63d

13 files changed

Lines changed: 983 additions & 0 deletions
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
page_type: sample
3+
languages:
4+
- cpp
5+
products:
6+
- windows-api-win32
7+
name: Direct3D 12 State Object Database Sample
8+
urlFragment: d3d12-state-object-database-sample-win32
9+
description: This sample demonstrates explicitly writing a State Object Database (SODB) with Direct3D 12.
10+
extendedZipContent:
11+
- path: LICENSE
12+
target: LICENSE
13+
---
14+
15+
# Direct3D 12 State Object Database Sample
16+
17+
## Sample to Write a State Object Database
18+
19+
This sample demonstrates explicitly writing a State Object Database (SODB) with Direct3D 12. State Object Databases contain the API arguments for creating Pipeline State Objects and State Objects. They are used to precompile shaders for a target adapter and application.
20+
21+
The explicit method for recording an SODB can be useful when translating a custom PSO storage mechanism into the standard SODB, or in other circumstances where the set of objects is known and playthrough can be avoided.
22+
23+
## State Object Database Capture
24+
25+
Alternatively, a capture method exists that will capture any Pipeline State Object or State Object encountered during a playthrough session into an SODB. No code changes to the target application are needed to record the objects encountered in the playthrough.
26+
27+
### 1. Add the application to d3dconfig to apply settings
28+
29+
D3DConfig settings only apply to applications in this list of apps.
30+
31+
```txt
32+
d3dconfig.exe apps --add <exefilename>
33+
```
34+
35+
Notes
36+
37+
- Full path may also be used, but be aware that applications launched through hard links or other references that may not match this path.
38+
- Settings are applied to all instances of the matching exe.
39+
- The add command has two dashes. “–-add”
40+
41+
### 2. Set the path for the SODB file output
42+
43+
This settings specifies the output State Object Database file.
44+
45+
```txt
46+
d3dconfig.exe device pso-db-path=<filepath>.sodb
47+
```
48+
49+
Notes
50+
51+
- All matching d3dconfig apps write to this db. Use exe apps to see the currently configured applications.
52+
53+
### 3. Enable SODB collection
54+
55+
Enable SODB collection.
56+
57+
```txt
58+
d3dconfig.exe device enable-pso-db=true
59+
```
60+
61+
Notes
62+
63+
- d3dconfig settings are only read at device creation. Exit and restart the game to ensure settings apply.
64+
- This setting may be set to false to temporarily stop collection.
65+
66+
### 4. Play through the game and exit
67+
68+
Pipeline State Objects and State Objects created by the D3D12 API are recorded to the database specified in step 2.
69+
70+
### 5. Clear D3DConfig Settings
71+
72+
Clear all D3Dconfig settings to stop SODB capture
73+
74+
```txt
75+
d3dconfig.exe --reset
76+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36811.4 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "StateObjectDatabase Sample", "StateObjectDatabase Sample.vcxproj", "{A862BEE8-8C56-48AD-B979-9E1AB73C67A6}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Debug|x86 = Debug|x86
12+
Release|x64 = Release|x64
13+
Release|x86 = Release|x86
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{A862BEE8-8C56-48AD-B979-9E1AB73C67A6}.Debug|x64.ActiveCfg = Debug|x64
17+
{A862BEE8-8C56-48AD-B979-9E1AB73C67A6}.Debug|x64.Build.0 = Debug|x64
18+
{A862BEE8-8C56-48AD-B979-9E1AB73C67A6}.Debug|x86.ActiveCfg = Debug|Win32
19+
{A862BEE8-8C56-48AD-B979-9E1AB73C67A6}.Debug|x86.Build.0 = Debug|Win32
20+
{A862BEE8-8C56-48AD-B979-9E1AB73C67A6}.Release|x64.ActiveCfg = Release|x64
21+
{A862BEE8-8C56-48AD-B979-9E1AB73C67A6}.Release|x64.Build.0 = Release|x64
22+
{A862BEE8-8C56-48AD-B979-9E1AB73C67A6}.Release|x86.ActiveCfg = Release|Win32
23+
{A862BEE8-8C56-48AD-B979-9E1AB73C67A6}.Release|x86.Build.0 = Release|Win32
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {F9F00C6F-8831-4F91-97BA-18AD3E817305}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
float4 NoRootSignaturePS(float4 pos : SV_POSITION) : SV_TARGET
2+
{
3+
return pos;
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Texture2D<float4> tex0 : register(t0);
2+
float4 NoRootSignatureVS(float4 pos : POS) : SV_POSITION
3+
{
4+
return tex0.Load(int3(0, 0, 0));
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
struct PSInput
2+
{
3+
float4 position : SV_POSITION;
4+
float4 color : COLOR;
5+
};
6+
7+
[RootSignature("RootFlags(ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT)")]
8+
float4 PositionColorPS(PSInput input) : SV_TARGET
9+
{
10+
return input.color;
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
struct PSInput
2+
{
3+
float4 position : SV_POSITION;
4+
float4 color : COLOR;
5+
};
6+
7+
[RootSignature("RootFlags(ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT)")]
8+
PSInput PositionColorVS(float4 position : POSITION, float4 color : COLOR)
9+
{
10+
PSInput result;
11+
12+
result.position = position;
13+
result.color = color;
14+
15+
return result;
16+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
RaytracingAccelerationStructure MyAccelerationStructure : register(t0);
2+
RWTexture2D<float4> RT : register(u0);
3+
RWByteAddressBuffer scratch : register(u1);
4+
RWByteAddressBuffer scratch2 : register(u2);
5+
struct MyPayload
6+
{
7+
float4 color;
8+
};
9+
10+
float bar2(float b)
11+
{
12+
return 2*b;
13+
}
14+
15+
float4 bar(float b)
16+
{
17+
return float4(b, b, b, b);
18+
}
19+
20+
21+
float4 foo(int a, float b)
22+
{
23+
return float4(b, b, (float)a, bar(b).x + scratch2.Load(0) );
24+
}
25+
26+
void touchScratch()
27+
{
28+
scratch.Store(4,scratch.Load(4)+1);
29+
}
30+
31+
32+
void touchScratchRenamed()
33+
{
34+
touchScratch();
35+
}
36+
void touchScratchRenamed2()
37+
{
38+
touchScratchRenamed();
39+
}
40+
41+
float4 foo(float b)
42+
{
43+
touchScratchRenamed();
44+
return float4(b, b, b, b) + bar2(3);
45+
}
46+
47+
48+
[shader("raygeneration")]
49+
void raygen_main()
50+
{
51+
RayDesc MyRay = { float3(0,0,0),0.1,float3(1,0,0),1.0 };
52+
MyPayload payload = { float4(0,0,0,1) };
53+
TraceRay(MyAccelerationStructure, RAY_FLAG_NONE, 0xff, 0, 1, 0, MyRay, payload);
54+
RT[(unsigned int2)DispatchRaysIndex()] = payload.color + foo(1,1.5);
55+
}
56+
57+
[shader("closesthit")]
58+
void closesthit_main(inout MyPayload payload, in BuiltInTriangleIntersectionAttributes attr)
59+
{
60+
touchScratchRenamed();
61+
payload.color.g = bar(5.5).y;
62+
}
63+
64+
[shader("anyhit")]
65+
void anyhit_main(inout MyPayload payload, in BuiltInTriangleIntersectionAttributes attr)
66+
{
67+
touchScratchRenamed2();
68+
payload.color += (0.1, 0.1, 0.1, 1);
69+
}
70+
71+
[shader("miss")]
72+
void miss_main(inout MyPayload payload)
73+
{
74+
touchScratch();
75+
payload.color = (1, 0, 0, 1);
76+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#define RootSig \
2+
"RootFlags(ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT|ALLOW_STREAM_OUTPUT)," \
3+
"DescriptorTable(SRV(t0, numDescriptors=1))," \
4+
"DescriptorTable(UAV(u0, numDescriptors=2))," \
5+
"DescriptorTable(Sampler(s0, numDescriptors=2))"
6+
7+
[RootSignature(RootSig)]
8+
float4 RootSignaturePS(float4 pos : SV_POSITION) : SV_TARGET
9+
{
10+
return pos;
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#define RootSig \
2+
"RootFlags(ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT|ALLOW_STREAM_OUTPUT)," \
3+
"DescriptorTable(SRV(t0, numDescriptors=1))," \
4+
"DescriptorTable(UAV(u0, numDescriptors=2))," \
5+
"DescriptorTable(Sampler(s0, numDescriptors=2))"
6+
7+
Texture2D<float4> tex0 : register(t0);
8+
[RootSignature(RootSig)]
9+
float4 RootSignatureVS(float4 pos : POS) : SV_POSITION
10+
{
11+
return tex0.Load(int3(0, 0, 0));
12+
}

0 commit comments

Comments
 (0)