forked from discosultan/dx12-game-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShadowMap.cs
141 lines (120 loc) · 4.3 KB
/
ShadowMap.cs
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using SharpDX;
using SharpDX.Direct3D12;
using SharpDX.DXGI;
using Device = SharpDX.Direct3D12.Device;
using Resource = SharpDX.Direct3D12.Resource;
namespace DX12GameProgramming
{
internal class ShadowMap : IDisposable
{
private static readonly Format Format = Format.R24G8_Typeless;
private readonly Device _device;
private CpuDescriptorHandle _cpuSrv;
private GpuDescriptorHandle _gpuSrv;
private CpuDescriptorHandle _cpuDsv;
public ShadowMap(Device device, int width, int height)
{
_device = device;
Width = width;
Height = height;
Viewport = new ViewportF(0, 0, width, height);
ScissorRectangle = new RectangleF(0, 0, width, height);
BuildResource();
}
public int Width { get; private set; }
public int Height { get; private set; }
public Resource Resource { get; private set; }
public GpuDescriptorHandle Srv => _gpuSrv;
public CpuDescriptorHandle Dsv => _cpuDsv;
public ViewportF Viewport { get; private set; }
public RectangleF ScissorRectangle { get; private set; }
public void BuildDescriptors(
CpuDescriptorHandle cpuSrv,
GpuDescriptorHandle gpuSrv,
CpuDescriptorHandle cpuDsv)
{
// Save references to the descriptors.
_cpuSrv = cpuSrv;
_gpuSrv = gpuSrv;
_cpuDsv = cpuDsv;
// Create the descriptors
BuildDescriptors();
}
public void OnResize(int newWidth, int newHeight)
{
if (Width != newWidth || Height != newHeight)
{
Width = newWidth;
Height = newHeight;
Dispose();
BuildResource();
}
}
private void BuildDescriptors()
{
// Create SRV to resource so we can sample the shadow map in a shader program.
var srvDesc = new ShaderResourceViewDescription
{
Shader4ComponentMapping = D3DUtil.DefaultShader4ComponentMapping,
Format = Format.R24_UNorm_X8_Typeless,
Dimension = ShaderResourceViewDimension.Texture2D,
Texture2D = new ShaderResourceViewDescription.Texture2DResource
{
MostDetailedMip = 0,
MipLevels = 1,
ResourceMinLODClamp = 0.0f,
PlaneSlice = 0
}
};
_device.CreateShaderResourceView(Resource, srvDesc, _cpuSrv);
// Create DSV to resource so we can render to the shadow map.
var dsvDesc = new DepthStencilViewDescription
{
Flags = DepthStencilViewFlags.None,
Dimension = DepthStencilViewDimension.Texture2D,
Format = Format.D24_UNorm_S8_UInt,
Texture2D = new DepthStencilViewDescription.Texture2DResource
{
MipSlice = 0
}
};
_device.CreateDepthStencilView(Resource, dsvDesc, _cpuDsv);
}
private void BuildResource()
{
var texDesc = new ResourceDescription
{
Dimension = ResourceDimension.Texture2D,
Alignment = 0,
Width = Width,
Height = Height,
DepthOrArraySize = 1,
MipLevels = 1,
Format = Format,
SampleDescription = new SampleDescription(1, 0),
Layout = TextureLayout.Unknown,
Flags = ResourceFlags.AllowDepthStencil
};
var optClear = new ClearValue
{
Format = Format.D24_UNorm_S8_UInt,
DepthStencil = new DepthStencilValue
{
Depth = 1.0f,
Stencil = 0
}
};
Resource = _device.CreateCommittedResource(
new HeapProperties(HeapType.Default),
HeapFlags.None,
texDesc,
ResourceStates.GenericRead,
optClear);
}
public void Dispose()
{
Resource?.Dispose();
}
}
}