Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions Samples/Desktop/D3D12StateObjectDatabase/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
page_type: sample
languages:
- cpp
products:
- windows-api-win32
name: Direct3D 12 State Object Database Sample
urlFragment: d3d12-state-object-database-sample-win32
description: This sample demonstrates explicitly writing a State Object Database (SODB) with Direct3D 12.
extendedZipContent:
- path: LICENSE
target: LICENSE
---

# Direct3D 12 State Object Database Sample

## Sample to Write a State Object Database

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.

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.

## State Object Database Capture

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.

### 1. Add the application to d3dconfig to apply settings

D3DConfig settings only apply to applications in this list of apps.

```txt
d3dconfig.exe apps --add <exefilename>
```

Notes

- Full path may also be used, but be aware that applications launched through hard links or other references that may not match this path.
- Settings are applied to all instances of the matching exe.
- The add command has two dashes. “–-add”

### 2. Set the path for the SODB file output

This settings specifies the output State Object Database file.

```txt
d3dconfig.exe device pso-db-path=<filepath>.sodb
```

Notes

- All matching d3dconfig apps write to this db. Use exe apps to see the currently configured applications.

### 3. Enable SODB collection

Enable SODB collection.

```txt
d3dconfig.exe device enable-pso-db=true
```

Notes

- d3dconfig settings are only read at device creation. Exit and restart the game to ensure settings apply.
- This setting may be set to false to temporarily stop collection.

### 4. Play through the game and exit

Pipeline State Objects and State Objects created by the D3D12 API are recorded to the database specified in step 2.

### 5. Clear D3DConfig Settings

Clear all D3Dconfig settings to stop SODB capture

```txt
d3dconfig.exe --reset
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36811.4 d17.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "StateObjectDatabase Sample", "StateObjectDatabase Sample.vcxproj", "{A862BEE8-8C56-48AD-B979-9E1AB73C67A6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A862BEE8-8C56-48AD-B979-9E1AB73C67A6}.Debug|x64.ActiveCfg = Debug|x64
{A862BEE8-8C56-48AD-B979-9E1AB73C67A6}.Debug|x64.Build.0 = Debug|x64
{A862BEE8-8C56-48AD-B979-9E1AB73C67A6}.Debug|x86.ActiveCfg = Debug|Win32
{A862BEE8-8C56-48AD-B979-9E1AB73C67A6}.Debug|x86.Build.0 = Debug|Win32
{A862BEE8-8C56-48AD-B979-9E1AB73C67A6}.Release|x64.ActiveCfg = Release|x64
{A862BEE8-8C56-48AD-B979-9E1AB73C67A6}.Release|x64.Build.0 = Release|x64
{A862BEE8-8C56-48AD-B979-9E1AB73C67A6}.Release|x86.ActiveCfg = Release|Win32
{A862BEE8-8C56-48AD-B979-9E1AB73C67A6}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F9F00C6F-8831-4F91-97BA-18AD3E817305}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
float4 NoRootSignaturePS(float4 pos : SV_POSITION) : SV_TARGET
{
return pos;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Texture2D<float4> tex0 : register(t0);
float4 NoRootSignatureVS(float4 pos : POS) : SV_POSITION
{
return tex0.Load(int3(0, 0, 0));
}
11 changes: 11 additions & 0 deletions Samples/Desktop/D3D12StateObjectDatabase/src/PositionColorPS.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
struct PSInput
{
float4 position : SV_POSITION;
float4 color : COLOR;
};

[RootSignature("RootFlags(ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT)")]
float4 PositionColorPS(PSInput input) : SV_TARGET
{
return input.color;
}
16 changes: 16 additions & 0 deletions Samples/Desktop/D3D12StateObjectDatabase/src/PositionColorVS.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
struct PSInput
{
float4 position : SV_POSITION;
float4 color : COLOR;
};

[RootSignature("RootFlags(ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT)")]
PSInput PositionColorVS(float4 position : POSITION, float4 color : COLOR)
{
PSInput result;

result.position = position;
result.color = color;

return result;
}
76 changes: 76 additions & 0 deletions Samples/Desktop/D3D12StateObjectDatabase/src/RayTracing.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
RaytracingAccelerationStructure MyAccelerationStructure : register(t0);
RWTexture2D<float4> RT : register(u0);
RWByteAddressBuffer scratch : register(u1);
RWByteAddressBuffer scratch2 : register(u2);
struct MyPayload
{
float4 color;
};

float bar2(float b)
{
return 2*b;
}

float4 bar(float b)
{
return float4(b, b, b, b);
}


float4 foo(int a, float b)
{
return float4(b, b, (float)a, bar(b).x + scratch2.Load(0) );
}

void touchScratch()
{
scratch.Store(4,scratch.Load(4)+1);
}


void touchScratchRenamed()
{
touchScratch();
}
void touchScratchRenamed2()
{
touchScratchRenamed();
}

float4 foo(float b)
{
touchScratchRenamed();
return float4(b, b, b, b) + bar2(3);
}


[shader("raygeneration")]
void raygen_main()
{
RayDesc MyRay = { float3(0,0,0),0.1,float3(1,0,0),1.0 };
MyPayload payload = { float4(0,0,0,1) };
TraceRay(MyAccelerationStructure, RAY_FLAG_NONE, 0xff, 0, 1, 0, MyRay, payload);
RT[(unsigned int2)DispatchRaysIndex()] = payload.color + foo(1,1.5);
}

[shader("closesthit")]
void closesthit_main(inout MyPayload payload, in BuiltInTriangleIntersectionAttributes attr)
{
touchScratchRenamed();
payload.color.g = bar(5.5).y;
}

[shader("anyhit")]
void anyhit_main(inout MyPayload payload, in BuiltInTriangleIntersectionAttributes attr)
{
touchScratchRenamed2();
payload.color += (0.1, 0.1, 0.1, 1);
}

[shader("miss")]
void miss_main(inout MyPayload payload)
{
touchScratch();
payload.color = (1, 0, 0, 1);
}
11 changes: 11 additions & 0 deletions Samples/Desktop/D3D12StateObjectDatabase/src/RootSignaturePS.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#define RootSig \
"RootFlags(ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT|ALLOW_STREAM_OUTPUT)," \
"DescriptorTable(SRV(t0, numDescriptors=1))," \
"DescriptorTable(UAV(u0, numDescriptors=2))," \
"DescriptorTable(Sampler(s0, numDescriptors=2))"

[RootSignature(RootSig)]
float4 RootSignaturePS(float4 pos : SV_POSITION) : SV_TARGET
{
return pos;
}
12 changes: 12 additions & 0 deletions Samples/Desktop/D3D12StateObjectDatabase/src/RootSignatureVS.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#define RootSig \
"RootFlags(ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT|ALLOW_STREAM_OUTPUT)," \
"DescriptorTable(SRV(t0, numDescriptors=1))," \
"DescriptorTable(UAV(u0, numDescriptors=2))," \
"DescriptorTable(Sampler(s0, numDescriptors=2))"

Texture2D<float4> tex0 : register(t0);
[RootSignature(RootSig)]
float4 RootSignatureVS(float4 pos : POS) : SV_POSITION
{
return tex0.Load(int3(0, 0, 0));
}
Loading