-
Notifications
You must be signed in to change notification settings - Fork 45
/
Texture.cs
237 lines (186 loc) · 9.17 KB
/
Texture.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
using System;
using System.ComponentModel;
using System.IO;
using System.Threading.Tasks;
using Vortice.Direct3D12;
using Vortice.DXGI;
namespace DirectX12GameEngine.Graphics
{
public sealed class Texture : GraphicsResource
{
[EditorBrowsable(EditorBrowsableState.Never)]
public Texture()
{
}
internal Texture(GraphicsDevice device) : base(device)
{
}
public TextureDescription Description { get; private set; }
public int Width => Description.Width;
public int Height => Description.Height;
internal CpuDescriptorHandle NativeDepthStencilView { get; private set; }
public static async Task<Texture> LoadAsync(GraphicsDevice device, string filePath)
{
using FileStream stream = File.OpenRead(filePath);
return await LoadAsync(device, stream);
}
public static async Task<Texture> LoadAsync(GraphicsDevice device, Stream stream)
{
using Image image = await Image.LoadAsync(stream);
return New2D(device, image.Data.Span, image.Width, image.Height, image.Description.Format);
}
public static Texture New(GraphicsDevice device, TextureDescription description)
{
return new Texture(device).InitializeFrom(description);
}
public static Texture New2D(GraphicsDevice device, int width, int height, PixelFormat format, TextureFlags textureFlags = TextureFlags.ShaderResource, short mipCount = 1, short arraySize = 1, int multisampleCount = 1, GraphicsHeapType heapType = GraphicsHeapType.Default)
{
return New(device, TextureDescription.New2D(width, height, format, textureFlags, mipCount, arraySize, multisampleCount, heapType));
}
public static Texture New2D<T>(GraphicsDevice device, Span<T> data, int width, int height, PixelFormat format, TextureFlags textureFlags = TextureFlags.ShaderResource, short mipCount = 1, short arraySize = 1, int sampleCount = 1, GraphicsHeapType heapType = GraphicsHeapType.Default) where T : unmanaged
{
Texture texture = New2D(device, width, height, format, textureFlags, mipCount, arraySize, sampleCount, heapType);
texture.SetData(data);
return texture;
}
public unsafe void SetData<T>(Span<T> data) where T : unmanaged
{
if (NativeResource is null) throw new InvalidOperationException();
ID3D12Resource uploadResource = GraphicsDevice.NativeDevice.CreateCommittedResource(new HeapProperties(CpuPageProperty.WriteBack, MemoryPool.L0), HeapFlags.None, NativeResource.Description, ResourceStates.CopyDestination);
using Texture textureUploadBuffer = new Texture(GraphicsDevice).InitializeFrom(uploadResource);
textureUploadBuffer.NativeResource!.WriteToSubresource(0, data, Width * 4, Width * Height * 4);
using CommandList copyCommandList = new CommandList(GraphicsDevice, CommandListType.Copy);
copyCommandList.CopyResource(textureUploadBuffer, this);
copyCommandList.Flush(true);
}
public Texture InitializeFrom(TextureDescription description)
{
ResourceStates resourceStates = ResourceStates.Common;
if (description.HeapType == GraphicsHeapType.Upload)
{
resourceStates = ResourceStates.GenericRead;
}
else if (description.HeapType == GraphicsHeapType.Readback)
{
resourceStates = ResourceStates.CopyDestination;
}
ID3D12Resource resource = GraphicsDevice.NativeDevice.CreateCommittedResource(
new HeapProperties((HeapType)description.HeapType), HeapFlags.None,
ConvertToNativeDescription(description), resourceStates);
return InitializeFrom(resource, description);
}
internal Texture InitializeFrom(ID3D12Resource resource, bool isShaderResource = false)
{
resource.GetHeapProperties(out HeapProperties heapProperties, out _);
TextureDescription description = ConvertFromNativeDescription(resource.Description, (GraphicsHeapType)heapProperties.Type, isShaderResource);
return InitializeFrom(resource, description);
}
internal Texture InitializeFrom(ID3D12Resource resource, TextureDescription description)
{
NativeResource = resource;
Description = description;
if (description.Flags.HasFlag(TextureFlags.DepthStencil))
{
NativeDepthStencilView = CreateDepthStencilView();
}
if (description.Flags.HasFlag(TextureFlags.RenderTarget))
{
NativeRenderTargetView = CreateRenderTargetView();
}
if (description.Flags.HasFlag(TextureFlags.ShaderResource))
{
NativeShaderResourceView = CreateShaderResourceView();
}
if (description.Flags.HasFlag(TextureFlags.UnorderedAccess))
{
NativeUnorderedAccessView = CreateUnorderedAccessView();
}
return this;
}
internal static ResourceFlags GetBindFlagsFromTextureFlags(TextureFlags flags)
{
ResourceFlags result = ResourceFlags.None;
if (flags.HasFlag(TextureFlags.RenderTarget))
{
result |= ResourceFlags.AllowRenderTarget;
}
if (flags.HasFlag(TextureFlags.UnorderedAccess))
{
result |= ResourceFlags.AllowUnorderedAccess;
}
if (flags.HasFlag(TextureFlags.DepthStencil))
{
result |= ResourceFlags.AllowDepthStencil;
if (!flags.HasFlag(TextureFlags.ShaderResource))
{
result |= ResourceFlags.DenyShaderResource;
}
}
return result;
}
private static TextureDescription ConvertFromNativeDescription(ResourceDescription description, GraphicsHeapType heapType, bool isShaderResource = false)
{
TextureDescription textureDescription = new TextureDescription
{
Dimension = TextureDimension.Texture2D,
Width = (int)description.Width,
Height = description.Height,
SampleCount = description.SampleDescription.Count,
Format = (PixelFormat)description.Format,
MipLevels = description.MipLevels,
HeapType = heapType,
DepthOrArraySize = description.DepthOrArraySize,
Flags = TextureFlags.None
};
if (description.Flags.HasFlag(ResourceFlags.AllowRenderTarget))
{
textureDescription.Flags |= TextureFlags.RenderTarget;
}
if (description.Flags.HasFlag(ResourceFlags.AllowUnorderedAccess))
{
textureDescription.Flags |= TextureFlags.UnorderedAccess;
}
if (description.Flags.HasFlag(ResourceFlags.AllowDepthStencil))
{
textureDescription.Flags |= TextureFlags.DepthStencil;
}
if (!description.Flags.HasFlag(ResourceFlags.DenyShaderResource) && isShaderResource)
{
textureDescription.Flags |= TextureFlags.ShaderResource;
}
return textureDescription;
}
private static ResourceDescription ConvertToNativeDescription(TextureDescription description)
{
return description.Dimension switch
{
TextureDimension.Texture2D => ResourceDescription.Texture2D((Format)description.Format, description.Width, description.Height, description.DepthOrArraySize, description.MipLevels, description.SampleCount, 0, GetBindFlagsFromTextureFlags(description.Flags)),
_ => throw new NotSupportedException()
};
}
internal CpuDescriptorHandle CreateDepthStencilView()
{
CpuDescriptorHandle cpuHandle = GraphicsDevice.DepthStencilViewAllocator.Allocate(1);
GraphicsDevice.NativeDevice.CreateDepthStencilView(NativeResource, null, cpuHandle);
return cpuHandle;
}
internal CpuDescriptorHandle CreateRenderTargetView()
{
CpuDescriptorHandle cpuHandle = GraphicsDevice.RenderTargetViewAllocator.Allocate(1);
GraphicsDevice.NativeDevice.CreateRenderTargetView(NativeResource, null, cpuHandle);
return cpuHandle;
}
internal CpuDescriptorHandle CreateShaderResourceView()
{
CpuDescriptorHandle cpuHandle = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1);
GraphicsDevice.NativeDevice.CreateShaderResourceView(NativeResource, null, cpuHandle);
return cpuHandle;
}
internal CpuDescriptorHandle CreateUnorderedAccessView()
{
CpuDescriptorHandle cpuHandle = GraphicsDevice.ShaderResourceViewAllocator.Allocate(1);
GraphicsDevice.NativeDevice.CreateUnorderedAccessView(NativeResource, null, null, cpuHandle);
return cpuHandle;
}
}
}