/
VkPipeline.cs
370 lines (307 loc) · 18.1 KB
/
VkPipeline.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
using Vulkan;
using static Vulkan.VulkanNative;
using static Veldrid.Vk.VulkanUtil;
using System;
using System.Diagnostics;
namespace Veldrid.Vk
{
internal unsafe class VkPipeline : Pipeline
{
private readonly VkGraphicsDevice _gd;
private readonly Vulkan.VkPipeline _devicePipeline;
private readonly VkPipelineLayout _pipelineLayout;
private readonly VkRenderPass _renderPass;
private bool _destroyed;
private string _name;
public Vulkan.VkPipeline DevicePipeline => _devicePipeline;
public VkPipelineLayout PipelineLayout => _pipelineLayout;
public uint ResourceSetCount { get; }
public bool ScissorTestEnabled { get; }
public override bool IsComputePipeline { get; }
public VkPipeline(VkGraphicsDevice gd, ref GraphicsPipelineDescription description)
: base(ref description)
{
_gd = gd;
IsComputePipeline = false;
VkGraphicsPipelineCreateInfo pipelineCI = VkGraphicsPipelineCreateInfo.New();
// Blend State
VkPipelineColorBlendStateCreateInfo blendStateCI = VkPipelineColorBlendStateCreateInfo.New();
int attachmentsCount = description.BlendState.AttachmentStates.Length;
VkPipelineColorBlendAttachmentState* attachmentsPtr
= stackalloc VkPipelineColorBlendAttachmentState[attachmentsCount];
for (int i = 0; i < attachmentsCount; i++)
{
BlendAttachmentDescription vdDesc = description.BlendState.AttachmentStates[i];
VkPipelineColorBlendAttachmentState attachmentState = new VkPipelineColorBlendAttachmentState();
attachmentState.srcColorBlendFactor = VkFormats.VdToVkBlendFactor(vdDesc.SourceColorFactor);
attachmentState.dstColorBlendFactor = VkFormats.VdToVkBlendFactor(vdDesc.DestinationColorFactor);
attachmentState.colorBlendOp = VkFormats.VdToVkBlendOp(vdDesc.ColorFunction);
attachmentState.srcAlphaBlendFactor = VkFormats.VdToVkBlendFactor(vdDesc.SourceAlphaFactor);
attachmentState.dstAlphaBlendFactor = VkFormats.VdToVkBlendFactor(vdDesc.DestinationAlphaFactor);
attachmentState.alphaBlendOp = VkFormats.VdToVkBlendOp(vdDesc.AlphaFunction);
attachmentState.blendEnable = vdDesc.BlendEnabled;
attachmentState.colorWriteMask = VkColorComponentFlags.R | VkColorComponentFlags.G | VkColorComponentFlags.B | VkColorComponentFlags.A;
attachmentsPtr[i] = attachmentState;
}
blendStateCI.attachmentCount = (uint)attachmentsCount;
blendStateCI.pAttachments = attachmentsPtr;
RgbaFloat blendFactor = description.BlendState.BlendFactor;
blendStateCI.blendConstants_0 = blendFactor.R;
blendStateCI.blendConstants_1 = blendFactor.G;
blendStateCI.blendConstants_2 = blendFactor.B;
blendStateCI.blendConstants_3 = blendFactor.A;
pipelineCI.pColorBlendState = &blendStateCI;
// Rasterizer State
RasterizerStateDescription rsDesc = description.RasterizerState;
VkPipelineRasterizationStateCreateInfo rsCI = VkPipelineRasterizationStateCreateInfo.New();
rsCI.cullMode = VkFormats.VdToVkCullMode(rsDesc.CullMode);
rsCI.polygonMode = VkFormats.VdToVkPolygonMode(rsDesc.FillMode);
rsCI.depthClampEnable = !rsDesc.DepthClipEnabled;
rsCI.frontFace = rsDesc.FrontFace == FrontFace.Clockwise ? VkFrontFace.Clockwise : VkFrontFace.CounterClockwise;
rsCI.lineWidth = 1f;
pipelineCI.pRasterizationState = &rsCI;
ScissorTestEnabled = rsDesc.ScissorTestEnabled;
// Dynamic State
VkPipelineDynamicStateCreateInfo dynamicStateCI = VkPipelineDynamicStateCreateInfo.New();
VkDynamicState* dynamicStates = stackalloc VkDynamicState[2];
dynamicStates[0] = VkDynamicState.Viewport;
dynamicStates[1] = VkDynamicState.Scissor;
dynamicStateCI.dynamicStateCount = 2;
dynamicStateCI.pDynamicStates = dynamicStates;
pipelineCI.pDynamicState = &dynamicStateCI;
// Depth Stencil State
DepthStencilStateDescription vdDssDesc = description.DepthStencilState;
VkPipelineDepthStencilStateCreateInfo dssCI = VkPipelineDepthStencilStateCreateInfo.New();
dssCI.depthWriteEnable = vdDssDesc.DepthWriteEnabled;
dssCI.depthTestEnable = vdDssDesc.DepthTestEnabled;
dssCI.depthCompareOp = VkFormats.VdToVkCompareOp(vdDssDesc.DepthComparison);
dssCI.stencilTestEnable = vdDssDesc.StencilTestEnabled;
dssCI.front.failOp = VkFormats.VdToVkStencilOp(vdDssDesc.StencilFront.Fail);
dssCI.front.passOp = VkFormats.VdToVkStencilOp(vdDssDesc.StencilFront.Pass);
dssCI.front.depthFailOp = VkFormats.VdToVkStencilOp(vdDssDesc.StencilFront.DepthFail);
dssCI.front.compareMask = vdDssDesc.StencilReadMask;
dssCI.front.writeMask = vdDssDesc.StencilWriteMask;
dssCI.front.reference = vdDssDesc.StencilReference;
dssCI.back.failOp = VkFormats.VdToVkStencilOp(vdDssDesc.StencilBack.Fail);
dssCI.back.passOp = VkFormats.VdToVkStencilOp(vdDssDesc.StencilBack.Pass);
dssCI.back.depthFailOp = VkFormats.VdToVkStencilOp(vdDssDesc.StencilBack.DepthFail);
dssCI.back.compareMask = vdDssDesc.StencilReadMask;
dssCI.back.writeMask = vdDssDesc.StencilWriteMask;
dssCI.back.reference = vdDssDesc.StencilReference;
pipelineCI.pDepthStencilState = &dssCI;
// Multisample
VkPipelineMultisampleStateCreateInfo multisampleCI = VkPipelineMultisampleStateCreateInfo.New();
VkSampleCountFlags vkSampleCount = VkFormats.VdToVkSampleCount(description.Outputs.SampleCount);
multisampleCI.rasterizationSamples = vkSampleCount;
pipelineCI.pMultisampleState = &multisampleCI;
// Input Assembly
VkPipelineInputAssemblyStateCreateInfo inputAssemblyCI = VkPipelineInputAssemblyStateCreateInfo.New();
inputAssemblyCI.topology = VkFormats.VdToVkPrimitiveTopology(description.PrimitiveTopology);
pipelineCI.pInputAssemblyState = &inputAssemblyCI;
// Vertex Input State
VkPipelineVertexInputStateCreateInfo vertexInputCI = VkPipelineVertexInputStateCreateInfo.New();
VertexLayoutDescription[] inputDescriptions = description.ShaderSet.VertexLayouts;
uint bindingCount = (uint)inputDescriptions.Length;
uint attributeCount = 0;
for (int i = 0; i < inputDescriptions.Length; i++)
{
attributeCount += (uint)inputDescriptions[i].Elements.Length;
}
VkVertexInputBindingDescription* bindingDescs = stackalloc VkVertexInputBindingDescription[(int)bindingCount];
VkVertexInputAttributeDescription* attributeDescs = stackalloc VkVertexInputAttributeDescription[(int)attributeCount];
int targetIndex = 0;
int targetLocation = 0;
for (int binding = 0; binding < inputDescriptions.Length; binding++)
{
VertexLayoutDescription inputDesc = inputDescriptions[binding];
bindingDescs[binding] = new VkVertexInputBindingDescription()
{
binding = (uint)binding,
inputRate = (inputDesc.InstanceStepRate != 0) ? VkVertexInputRate.Instance : VkVertexInputRate.Vertex,
stride = inputDesc.Stride
};
uint currentOffset = 0;
for (int location = 0; location < inputDesc.Elements.Length; location++)
{
VertexElementDescription inputElement = inputDesc.Elements[location];
attributeDescs[targetIndex] = new VkVertexInputAttributeDescription()
{
format = VkFormats.VdToVkVertexElementFormat(inputElement.Format),
binding = (uint)binding,
location = (uint)(targetLocation + location),
offset = currentOffset
};
targetIndex += 1;
currentOffset += FormatHelpers.GetSizeInBytes(inputElement.Format);
}
targetLocation += inputDesc.Elements.Length;
}
vertexInputCI.vertexBindingDescriptionCount = bindingCount;
vertexInputCI.pVertexBindingDescriptions = bindingDescs;
vertexInputCI.vertexAttributeDescriptionCount = attributeCount;
vertexInputCI.pVertexAttributeDescriptions = attributeDescs;
pipelineCI.pVertexInputState = &vertexInputCI;
// Shader Stage
Shader[] shaders = description.ShaderSet.Shaders;
StackList<VkPipelineShaderStageCreateInfo> stages = new StackList<VkPipelineShaderStageCreateInfo>();
foreach (Shader shader in shaders)
{
VkShader vkShader = Util.AssertSubtype<Shader, VkShader>(shader);
VkPipelineShaderStageCreateInfo stageCI = VkPipelineShaderStageCreateInfo.New();
stageCI.module = vkShader.ShaderModule;
stageCI.stage = VkFormats.VdToVkShaderStages(shader.Stage);
stageCI.pName = CommonStrings.main; // Meh
stages.Add(stageCI);
}
pipelineCI.stageCount = stages.Count;
pipelineCI.pStages = (VkPipelineShaderStageCreateInfo*)stages.Data;
// ViewportState
VkPipelineViewportStateCreateInfo viewportStateCI = VkPipelineViewportStateCreateInfo.New();
viewportStateCI.viewportCount = 1;
viewportStateCI.scissorCount = 1;
pipelineCI.pViewportState = &viewportStateCI;
// Pipeline Layout
ResourceLayout[] resourceLayouts = description.ResourceLayouts;
VkPipelineLayoutCreateInfo pipelineLayoutCI = VkPipelineLayoutCreateInfo.New();
pipelineLayoutCI.setLayoutCount = (uint)resourceLayouts.Length;
VkDescriptorSetLayout* dsls = stackalloc VkDescriptorSetLayout[resourceLayouts.Length];
for (int i = 0; i < resourceLayouts.Length; i++)
{
dsls[i] = Util.AssertSubtype<ResourceLayout, VkResourceLayout>(resourceLayouts[i]).DescriptorSetLayout;
}
pipelineLayoutCI.pSetLayouts = dsls;
vkCreatePipelineLayout(_gd.Device, ref pipelineLayoutCI, null, out _pipelineLayout);
pipelineCI.layout = _pipelineLayout;
// Create fake RenderPass for compatibility.
VkRenderPassCreateInfo renderPassCI = VkRenderPassCreateInfo.New();
OutputDescription outputDesc = description.Outputs;
StackList<VkAttachmentDescription, Size512Bytes> attachments = new StackList<VkAttachmentDescription, Size512Bytes>();
// TODO: A huge portion of this next part is duplicated in VkFramebuffer.cs.
StackList<VkAttachmentDescription> colorAttachmentDescs = new StackList<VkAttachmentDescription>();
StackList<VkAttachmentReference> colorAttachmentRefs = new StackList<VkAttachmentReference>();
for (uint i = 0; i < outputDesc.ColorAttachments.Length; i++)
{
colorAttachmentDescs[i].format = VkFormats.VdToVkPixelFormat(outputDesc.ColorAttachments[i].Format);
colorAttachmentDescs[i].samples = vkSampleCount;
colorAttachmentDescs[i].loadOp = VkAttachmentLoadOp.DontCare;
colorAttachmentDescs[i].storeOp = VkAttachmentStoreOp.Store;
colorAttachmentDescs[i].stencilLoadOp = VkAttachmentLoadOp.DontCare;
colorAttachmentDescs[i].stencilStoreOp = VkAttachmentStoreOp.DontCare;
colorAttachmentDescs[i].initialLayout = VkImageLayout.Undefined;
colorAttachmentDescs[i].finalLayout = VkImageLayout.ShaderReadOnlyOptimal;
attachments.Add(colorAttachmentDescs[i]);
colorAttachmentRefs[i].attachment = i;
colorAttachmentRefs[i].layout = VkImageLayout.ColorAttachmentOptimal;
}
VkAttachmentDescription depthAttachmentDesc = new VkAttachmentDescription();
VkAttachmentReference depthAttachmentRef = new VkAttachmentReference();
if (outputDesc.DepthAttachment != null)
{
PixelFormat depthFormat = outputDesc.DepthAttachment.Value.Format;
bool hasStencil = FormatHelpers.IsStencilFormat(depthFormat);
depthAttachmentDesc.format = VkFormats.VdToVkPixelFormat(outputDesc.DepthAttachment.Value.Format, toDepthFormat: true);
depthAttachmentDesc.samples = vkSampleCount;
depthAttachmentDesc.loadOp = VkAttachmentLoadOp.DontCare;
depthAttachmentDesc.storeOp = VkAttachmentStoreOp.Store;
depthAttachmentDesc.stencilLoadOp = VkAttachmentLoadOp.DontCare;
depthAttachmentDesc.stencilStoreOp = hasStencil ? VkAttachmentStoreOp.Store : VkAttachmentStoreOp.DontCare;
depthAttachmentDesc.initialLayout = VkImageLayout.Undefined;
depthAttachmentDesc.finalLayout = VkImageLayout.DepthStencilAttachmentOptimal;
depthAttachmentRef.attachment = (uint)outputDesc.ColorAttachments.Length;
depthAttachmentRef.layout = VkImageLayout.DepthStencilAttachmentOptimal;
}
VkSubpassDescription subpass = new VkSubpassDescription();
subpass.pipelineBindPoint = VkPipelineBindPoint.Graphics;
subpass.colorAttachmentCount = (uint)outputDesc.ColorAttachments.Length;
subpass.pColorAttachments = (VkAttachmentReference*)colorAttachmentRefs.Data;
for (int i = 0; i < colorAttachmentDescs.Count; i++)
{
attachments.Add(colorAttachmentDescs[i]);
}
if (outputDesc.DepthAttachment != null)
{
subpass.pDepthStencilAttachment = &depthAttachmentRef;
attachments.Add(depthAttachmentDesc);
}
VkSubpassDependency subpassDependency = new VkSubpassDependency();
subpassDependency.srcSubpass = SubpassExternal;
subpassDependency.srcStageMask = VkPipelineStageFlags.ColorAttachmentOutput;
subpassDependency.dstStageMask = VkPipelineStageFlags.ColorAttachmentOutput;
subpassDependency.dstAccessMask = VkAccessFlags.ColorAttachmentRead | VkAccessFlags.ColorAttachmentWrite;
if (outputDesc.DepthAttachment != null)
{
subpassDependency.dstAccessMask |= VkAccessFlags.DepthStencilAttachmentRead | VkAccessFlags.DepthStencilAttachmentWrite;
}
renderPassCI.attachmentCount = attachments.Count;
renderPassCI.pAttachments = (VkAttachmentDescription*)attachments.Data;
renderPassCI.subpassCount = 1;
renderPassCI.pSubpasses = &subpass;
renderPassCI.dependencyCount = 1;
renderPassCI.pDependencies = &subpassDependency;
VkResult creationResult = vkCreateRenderPass(_gd.Device, ref renderPassCI, null, out _renderPass);
CheckResult(creationResult);
pipelineCI.renderPass = _renderPass;
VkResult result = vkCreateGraphicsPipelines(_gd.Device, VkPipelineCache.Null, 1, ref pipelineCI, null, out _devicePipeline);
CheckResult(result);
ResourceSetCount = (uint)description.ResourceLayouts.Length;
}
public VkPipeline(VkGraphicsDevice gd, ref ComputePipelineDescription description)
: base(ref description)
{
_gd = gd;
IsComputePipeline = true;
VkComputePipelineCreateInfo pipelineCI = VkComputePipelineCreateInfo.New();
// Pipeline Layout
ResourceLayout[] resourceLayouts = description.ResourceLayouts;
VkPipelineLayoutCreateInfo pipelineLayoutCI = VkPipelineLayoutCreateInfo.New();
pipelineLayoutCI.setLayoutCount = (uint)resourceLayouts.Length;
VkDescriptorSetLayout* dsls = stackalloc VkDescriptorSetLayout[resourceLayouts.Length];
for (int i = 0; i < resourceLayouts.Length; i++)
{
dsls[i] = Util.AssertSubtype<ResourceLayout, VkResourceLayout>(resourceLayouts[i]).DescriptorSetLayout;
}
pipelineLayoutCI.pSetLayouts = dsls;
vkCreatePipelineLayout(_gd.Device, ref pipelineLayoutCI, null, out _pipelineLayout);
pipelineCI.layout = _pipelineLayout;
// Shader Stage
Shader shader = description.ComputeShader;
VkShader vkShader = Util.AssertSubtype<Shader, VkShader>(shader);
VkPipelineShaderStageCreateInfo stageCI = VkPipelineShaderStageCreateInfo.New();
stageCI.module = vkShader.ShaderModule;
stageCI.stage = VkFormats.VdToVkShaderStages(shader.Stage);
stageCI.pName = CommonStrings.main; // Meh
pipelineCI.stage = stageCI;
VkResult result = vkCreateComputePipelines(
_gd.Device,
VkPipelineCache.Null,
1,
ref pipelineCI,
null,
out _devicePipeline);
CheckResult(result);
ResourceSetCount = (uint)description.ResourceLayouts.Length;
}
public override string Name
{
get => _name;
set
{
_name = value;
_gd.SetResourceName(this, value);
}
}
public override void Dispose()
{
if (!_destroyed)
{
_destroyed = true;
vkDestroyPipelineLayout(_gd.Device, _pipelineLayout, null);
vkDestroyPipeline(_gd.Device, _devicePipeline, null);
if (!IsComputePipeline)
{
vkDestroyRenderPass(_gd.Device, _renderPass, null);
}
}
}
}
}