Repository navigation
Expand file tree
/
Copy pathVulkanTriangle.cs
More file actions
172 lines (161 loc) · 7.35 KB
/
Copy pathVulkanTriangle.cs
File metadata and controls
172 lines (161 loc) · 7.35 KB
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
using System.Runtime.InteropServices;
using Silk.NET.Vulkan;
using Result = Silk.NET.Vulkan.Result;
namespace ImGui4NET.Template.MultiPlatforms;
internal sealed unsafe partial class VulkanTriangle : IDisposable
{
private readonly VulkanDeviceContext _device;
private PipelineLayout _layout;
private Pipeline _pipeline;
private Format _colorFormat;
private bool _disposed;
public VulkanTriangle(VulkanDeviceContext device) => _device = device;
public void Draw(in RenderFrameContext context)
{
ObjectDisposedException.ThrowIf(_disposed, this);
if (context.Graphics.Vulkan != _device || context.Vulkan is not { } frame ||
context.Width <= 0 || context.Height <= 0)
return;
// The renderer's clear and discard passes are compatible. Only a format change requires a new pipeline.
if (_pipeline.Handle == 0 || _colorFormat != frame.ColorFormat)
{
Pipeline replacement = CreatePipeline(frame.RenderPass);
// Swapchain recreation already waited for the device before changing formats.
if (_pipeline.Handle != 0)
_device.Api.DestroyPipeline(_device.Device, _pipeline, null);
_pipeline = replacement;
_colorFormat = frame.ColorFormat;
}
var viewport = new Viewport(0, 0, context.Width, context.Height, 0, 1);
var scissor = new Rect2D(default, new Extent2D((uint)context.Width, (uint)context.Height));
Vk vk = _device.Api;
vk.CmdBindPipeline(frame.CommandBuffer, PipelineBindPoint.Graphics, _pipeline);
vk.CmdSetViewport(frame.CommandBuffer, 0, 1, &viewport);
vk.CmdSetScissor(frame.CommandBuffer, 0, 1, &scissor);
vk.CmdDraw(frame.CommandBuffer, 3, 1, 0, 0);
}
private Pipeline CreatePipeline(RenderPass renderPass)
{
Vk vk = _device.Api;
Device device = _device.Device;
if (_layout.Handle == 0)
{
var layoutInfo = new PipelineLayoutCreateInfo { SType = StructureType.PipelineLayoutCreateInfo };
Check(vk.CreatePipelineLayout(device, &layoutInfo, null, out PipelineLayout layout), "vkCreatePipelineLayout");
_layout = layout;
}
ShaderModule vertex = CreateShaderModule(VertexSpirv);
ShaderModule fragment = default;
try
{
fragment = CreateShaderModule(FragmentSpirv);
byte* entryPoint = stackalloc byte[] { (byte)'m', (byte)'a', (byte)'i', (byte)'n', 0 };
var stages = stackalloc PipelineShaderStageCreateInfo[]
{
new() { SType = StructureType.PipelineShaderStageCreateInfo, Stage = ShaderStageFlags.VertexBit,
Module = vertex, PName = entryPoint },
new() { SType = StructureType.PipelineShaderStageCreateInfo, Stage = ShaderStageFlags.FragmentBit,
Module = fragment, PName = entryPoint }
};
var vertexInput = new PipelineVertexInputStateCreateInfo { SType = StructureType.PipelineVertexInputStateCreateInfo };
var inputAssembly = new PipelineInputAssemblyStateCreateInfo
{
SType = StructureType.PipelineInputAssemblyStateCreateInfo,
Topology = PrimitiveTopology.TriangleList
};
var viewport = new PipelineViewportStateCreateInfo
{
SType = StructureType.PipelineViewportStateCreateInfo,
ViewportCount = 1,
ScissorCount = 1
};
var rasterization = new PipelineRasterizationStateCreateInfo
{
SType = StructureType.PipelineRasterizationStateCreateInfo,
PolygonMode = PolygonMode.Fill,
CullMode = CullModeFlags.None,
FrontFace = FrontFace.CounterClockwise,
LineWidth = 1f
};
var multisample = new PipelineMultisampleStateCreateInfo
{
SType = StructureType.PipelineMultisampleStateCreateInfo,
RasterizationSamples = SampleCountFlags.Count1Bit
};
var colorAttachment = new PipelineColorBlendAttachmentState
{
ColorWriteMask = ColorComponentFlags.RBit | ColorComponentFlags.GBit | ColorComponentFlags.BBit | ColorComponentFlags.ABit
};
var colorBlend = new PipelineColorBlendStateCreateInfo
{
SType = StructureType.PipelineColorBlendStateCreateInfo,
AttachmentCount = 1,
PAttachments = &colorAttachment
};
var dynamicStates = stackalloc DynamicState[] { DynamicState.Viewport, DynamicState.Scissor };
var dynamicState = new PipelineDynamicStateCreateInfo
{
SType = StructureType.PipelineDynamicStateCreateInfo,
DynamicStateCount = 2,
PDynamicStates = dynamicStates
};
var info = new GraphicsPipelineCreateInfo
{
SType = StructureType.GraphicsPipelineCreateInfo,
StageCount = 2,
PStages = stages,
PVertexInputState = &vertexInput,
PInputAssemblyState = &inputAssembly,
PViewportState = &viewport,
PRasterizationState = &rasterization,
PMultisampleState = &multisample,
PColorBlendState = &colorBlend,
PDynamicState = &dynamicState,
Layout = _layout,
RenderPass = renderPass
};
Pipeline pipeline = default;
Result result = vk.CreateGraphicsPipelines(device, default, 1, &info, null, &pipeline);
if (result != Result.Success && pipeline.Handle != 0)
vk.DestroyPipeline(device, pipeline, null);
Check(result, "vkCreateGraphicsPipelines");
return pipeline;
}
finally
{
if (fragment.Handle != 0)
vk.DestroyShaderModule(device, fragment, null);
vk.DestroyShaderModule(device, vertex, null);
}
}
private ShaderModule CreateShaderModule(ReadOnlySpan<byte> code)
{
// Vulkan expects SPIR-V as 32-bit words, but the embedded data is a byte span.
uint* words = stackalloc uint[code.Length / sizeof(uint)];
code.CopyTo(MemoryMarshal.AsBytes(new Span<uint>(words, code.Length / sizeof(uint))));
var info = new ShaderModuleCreateInfo
{
SType = StructureType.ShaderModuleCreateInfo,
CodeSize = (nuint)code.Length,
PCode = words
};
Check(_device.Api.CreateShaderModule(_device.Device, &info, null, out ShaderModule shader), "vkCreateShaderModule");
return shader;
}
private static void Check(Result result, string operation)
{
if (result != Result.Success)
throw new InvalidOperationException($"{operation} failed: {result}");
}
// Dispose from the application's graphics shutdown callback, after the renderer waits idle.
public void Dispose()
{
if (_disposed)
return;
_disposed = true;
if (_pipeline.Handle != 0)
_device.Api.DestroyPipeline(_device.Device, _pipeline, null);
if (_layout.Handle != 0)
_device.Api.DestroyPipelineLayout(_device.Device, _layout, null);
}
}