-
Notifications
You must be signed in to change notification settings - Fork 266
/
ResourceFactory.cs
273 lines (255 loc) · 13.8 KB
/
ResourceFactory.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
namespace Veldrid
{
/// <summary>
/// A device object responsible for the creation of graphics resources.
/// </summary>
public abstract class ResourceFactory
{
/// <summary>
/// Gets the <see cref="GraphicsBackend"/> of this instance.
/// </summary>
public abstract GraphicsBackend BackendType { get; }
/// <summary>
/// Creates a new <see cref="Pipeline"/>.
/// </summary>
/// <param name="description">The desired properties of the created object.</param>
/// <returns>A new <see cref="Pipeline"/>.</returns>
public Pipeline CreateGraphicsPipeline(GraphicsPipelineDescription description) => CreateGraphicsPipeline(ref description);
/// <summary>
/// Creates a new <see cref="Pipeline"/> object.
/// </summary>
/// <param name="description">The desired properties of the created object.</param>
/// <returns>A new <see cref="Pipeline"/> which, when bound to a CommandList, is used to dispatch draw commands.</returns>
public abstract Pipeline CreateGraphicsPipeline(ref GraphicsPipelineDescription description);
/// <summary>
/// Creates a new compute <see cref="Pipeline"/> object.
/// </summary>
/// <param name="description">The desirede properties of the created object.</param>
/// <returns>A new <see cref="Pipeline"/> which, when bound to a CommandList, is used to dispatch compute commands.</returns>
public Pipeline CreateComputePipeline(ComputePipelineDescription description) => CreateComputePipeline(ref description);
/// <summary>
/// Creates a new compute <see cref="Pipeline"/> object.
/// </summary>
/// <param name="description">The desirede properties of the created object.</param>
/// <returns>A new <see cref="Pipeline"/> which, when bound to a CommandList, is used to dispatch compute commands.</returns>
public abstract Pipeline CreateComputePipeline(ref ComputePipelineDescription description);
/// <summary>
/// Creates a new <see cref="Framebuffer"/>.
/// </summary>
/// <param name="description">The desired properties of the created object.</param>
/// <returns>A new <see cref="Framebuffer"/>.</returns>
public Framebuffer CreateFramebuffer(FramebufferDescription description) => CreateFramebuffer(ref description);
/// <summary>
/// Creates a new <see cref="Framebuffer"/>.
/// </summary>
/// <param name="description">The desired properties of the created object.</param>
/// <returns>A new <see cref="Framebuffer"/>.</returns>
public abstract Framebuffer CreateFramebuffer(ref FramebufferDescription description);
/// <summary>
/// Creates a new <see cref="Texture"/>.
/// </summary>
/// <param name="description">The desired properties of the created object.</param>
/// <returns>A new <see cref="Texture"/>.</returns>
public Texture CreateTexture(TextureDescription description) => CreateTexture(ref description);
/// <summary>
/// Creates a new <see cref="Texture"/>.
/// </summary>
/// <param name="description">The desired properties of the created object.</param>
/// <returns>A new <see cref="Texture"/>.</returns>
public Texture CreateTexture(ref TextureDescription description)
{
#if VALIDATE_USAGE
if (description.Width == 0 || description.Height == 0 || description.Depth == 0)
{
throw new VeldridException("Width, Height, and Depth must be non-zero.");
}
if ((description.Format == PixelFormat.D24_UNorm_S8_UInt || description.Format == PixelFormat.D32_Float_S8_UInt)
&& (description.Usage & TextureUsage.DepthStencil) == 0)
{
throw new VeldridException("The givel PixelFormat can only be used in a Texture with DepthStencil usage.");
}
if ((description.Type == TextureType.Texture1D || description.Type == TextureType.Texture3D)
&& description.SampleCount != TextureSampleCount.Count1)
{
throw new VeldridException(
$"1D and 3D Textures must use {nameof(TextureSampleCount)}.{nameof(TextureSampleCount.Count1)}.");
}
#endif
return CreateTextureCore(ref description);
}
// TODO: private protected
/// <summary>
/// </summary>
/// <param name="description"></param>
/// <returns></returns>
protected abstract Texture CreateTextureCore(ref TextureDescription description);
/// <summary>
/// Creates a new <see cref="TextureView"/>.
/// </summary>
/// <param name="target">The target <see cref="Texture"/> used in the new view.</param>
/// <returns>A new <see cref="TextureView"/>.</returns>
public TextureView CreateTextureView(Texture target) => CreateTextureView(new TextureViewDescription(target));
/// <summary>
/// Creates a new <see cref="TextureView"/>.
/// </summary>
/// <param name="description">The desired properties of the created object.</param>
/// <returns>A new <see cref="TextureView"/>.</returns>
public TextureView CreateTextureView(TextureViewDescription description) => CreateTextureView(ref description);
/// <summary>
/// Creates a new <see cref="TextureView"/>.
/// </summary>
/// <param name="description">The desired properties of the created object.</param>
/// <returns>A new <see cref="TextureView"/>.</returns>
public TextureView CreateTextureView(ref TextureViewDescription description)
{
#if VALIDATE_USAGE
if (description.MipLevels == 0 || description.ArrayLayers == 0
|| (description.BaseMipLevel + description.MipLevels) > description.Target.MipLevels
|| (description.BaseArrayLayer + description.ArrayLayers) > description.Target.ArrayLayers)
{
throw new VeldridException(
"TextureView mip level and array layer range must be contained in the target Texture.");
}
if ((description.Target.Usage & TextureUsage.Sampled) == 0
&& (description.Target.Usage & TextureUsage.Storage) == 0)
{
throw new VeldridException(
"To create a TextureView, the target texture must have either Sampled or Storage usage flags.");
}
#endif
return CreateTextureViewCore(ref description);
}
// TODO: private protected
/// <summary>
/// </summary>
/// <param name="description"></param>
/// <returns></returns>
protected abstract TextureView CreateTextureViewCore(ref TextureViewDescription description);
/// <summary>
/// Creates a new <see cref="DeviceBuffer"/>.
/// </summary>
/// <param name="description">The desired properties of the created object.</param>
/// <returns>A new <see cref="DeviceBuffer"/>.</returns>
public DeviceBuffer CreateBuffer(BufferDescription description) => CreateBuffer(ref description);
/// <summary>
/// Creates a new <see cref="DeviceBuffer"/>.
/// </summary>
/// <param name="description">The desired properties of the created object.</param>
/// <returns>A new <see cref="DeviceBuffer"/>.</returns>
public DeviceBuffer CreateBuffer(ref BufferDescription description)
{
#if VALIDATE_USAGE
if ((description.Usage & BufferUsage.StructuredBufferReadOnly) == BufferUsage.StructuredBufferReadOnly
|| (description.Usage & BufferUsage.StructuredBufferReadWrite) == BufferUsage.StructuredBufferReadWrite)
{
if (description.StructureByteStride == 0)
{
throw new VeldridException("Structured Buffer objects must have a non-zero StructureByteStride.");
}
}
else if (description.StructureByteStride != 0)
{
throw new VeldridException("Non-structured Buffers must have a StructureByteStride of zero.");
}
if ((description.Usage & BufferUsage.Staging) != 0 && description.Usage != BufferUsage.Staging)
{
throw new VeldridException("Buffers with Staging Usage must not specify any other Usage flags.");
}
if ((description.Usage & BufferUsage.UniformBuffer) != 0 && (description.SizeInBytes % 16) != 0)
{
throw new VeldridException($"Uniform buffer size must be a multiple of 16 bytes.");
}
#endif
return CreateBufferCore(ref description);
}
// TODO: private protected
/// <summary>
/// </summary>
/// <param name="description"></param>
/// <returns></returns>
protected abstract DeviceBuffer CreateBufferCore(ref BufferDescription description);
/// <summary>
/// Creates a new <see cref="Sampler"/>.
/// </summary>
/// <param name="description">The desired properties of the created object.</param>
/// <returns>A new <see cref="Sampler"/>.</returns>
public Sampler CreateSampler(SamplerDescription description) => CreateSampler(ref description);
/// <summary>
/// Creates a new <see cref="Sampler"/>.
/// </summary>
/// <param name="description">The desired properties of the created object.</param>
/// <returns>A new <see cref="Sampler"/>.</returns>
public abstract Sampler CreateSampler(ref SamplerDescription description);
/// <summary>
/// Creates a new <see cref="Shader"/>.
/// </summary>
/// <param name="description">The desired properties of the created object.</param>
/// <returns>A new <see cref="Shader"/>.</returns>
public Shader CreateShader(ShaderDescription description) => CreateShader(ref description);
/// <summary>
/// Creates a new <see cref="Shader"/>.
/// </summary>
/// <param name="description">The desired properties of the created object.</param>
/// <returns>A new <see cref="Shader"/>.</returns>
public abstract Shader CreateShader(ref ShaderDescription description);
/// <summary>
/// Creates a new <see cref="CommandList"/>.
/// </summary>
/// <returns>A new <see cref="CommandList"/>.</returns>
public CommandList CreateCommandList() => CreateCommandList(new CommandListDescription());
/// <summary>
/// Creates a new <see cref="CommandList"/>.
/// </summary>
/// <param name="description">The desired properties of the created object.</param>
/// <returns>A new <see cref="CommandList"/>.</returns>
public CommandList CreateCommandList(CommandListDescription description) => CreateCommandList(ref description);
/// <summary>
/// Creates a new <see cref="CommandList"/>.
/// </summary>
/// <param name="description">The desired properties of the created object.</param>
/// <returns>A new <see cref="CommandList"/>.</returns>
public abstract CommandList CreateCommandList(ref CommandListDescription description);
/// <summary>
/// Creates a new <see cref="ResourceLayout"/>.
/// </summary>
/// <param name="description">The desired properties of the created object.</param>
/// <returns>A new <see cref="ResourceLayout"/>.</returns>
public ResourceLayout CreateResourceLayout(ResourceLayoutDescription description) => CreateResourceLayout(ref description);
/// <summary>
/// Creates a new <see cref="ResourceLayout"/>.
/// </summary>
/// <param name="description">The desired properties of the created object.</param>
/// <returns>A new <see cref="ResourceLayout"/>.</returns>
public abstract ResourceLayout CreateResourceLayout(ref ResourceLayoutDescription description);
/// <summary>
/// Creates a new <see cref="ResourceSet"/>.
/// </summary>
/// <param name="description">The desired properties of the created object.</param>
/// <returns>A new <see cref="ResourceSet"/>.</returns>
public ResourceSet CreateResourceSet(ResourceSetDescription description) => CreateResourceSet(ref description);
/// <summary>
/// Creates a new <see cref="ResourceSet"/>.
/// </summary>
/// <param name="description">The desired properties of the created object.</param>
/// <returns>A new <see cref="ResourceSet"/>.</returns>
public abstract ResourceSet CreateResourceSet(ref ResourceSetDescription description);
/// <summary>
/// Creates a new <see cref="Fence"/> in the given state.
/// </summary>
/// <param name="signaled">A value indicating whether the Fence should be in the signaled state when created.</param>
/// <returns>A new <see cref="Fence"/>.</returns>
public abstract Fence CreateFence(bool signaled);
/// <summary>
/// Creates a new <see cref="Swapchain"/>.
/// </summary>
/// <param name="description">The desired properties of the created object.</param>
/// <returns>A new <see cref="Swapchain"/>.</returns>
public Swapchain CreateSwapchain(SwapchainDescription description) => CreateSwapchain(ref description);
/// <summary>
/// Creates a new <see cref="Swapchain"/>.
/// </summary>
/// <param name="description">The desired properties of the created object.</param>
/// <returns>A new <see cref="Swapchain"/>.</returns>
public abstract Swapchain CreateSwapchain(ref SwapchainDescription description);
}
}