forked from cosmatic-drift-14/cosmatic-drift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.Veldrid.cs
409 lines (330 loc) · 14.6 KB
/
Program.Veldrid.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
using System;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using ImGuiNET;
using OpenTK.Windowing.GraphicsLibraryFramework;
using Veldrid;
using Veldrid.OpenGL;
using Veldrid.SPIRV;
using Veldrid.Vk;
namespace Pow3r
{
internal sealed unsafe partial class Program
{
private const string VDVertexShader = @"
#version 460
layout (location = 0) in vec2 Position;
layout (location = 1) in vec2 UV;
layout (location = 2) in vec4 Color;
layout (set = 0, binding = 0) uniform ProjMtx {
mat4 _ProjMtx;
};
layout (location = 0) out vec2 Frag_UV;
layout (location = 1) out vec4 Frag_Color;
// Converts a color from sRGB gamma to linear light gamma
vec4 toLinear(vec4 sRGB)
{
bvec3 cutoff = lessThan(sRGB.rgb, vec3(0.04045));
vec3 higher = pow((sRGB.rgb + vec3(0.055))/vec3(1.055), vec3(2.4));
vec3 lower = sRGB.rgb/vec3(12.92);
return vec4(mix(higher, lower, cutoff), sRGB.a);
}
void main()
{
Frag_UV = UV;
Frag_Color = toLinear(Color);
gl_Position = _ProjMtx * vec4(Position.xy,0,1);
}";
private const string VDFragmentShader = @"
#version 460
layout (location = 0) in vec2 Frag_UV;
layout (location = 1) in vec4 Frag_Color;
layout (set = 1, binding = 0) uniform texture2D Texture;
layout (set = 1, binding = 1) uniform sampler TextureSampler;
layout (location = 0) out vec4 Out_Color;
void main()
{
Out_Color = Frag_Color * texture(sampler2D(Texture, TextureSampler), Frag_UV.st);
}";
private VeldridRenderer _vdRenderer = VeldridRenderer.Vulkan;
private GraphicsDevice _vdGfxDevice;
private CommandList _vdCommandList;
private Pipeline _vdPipeline;
private Shader[] _vdShaders;
private ResourceSet _vdSetTexture;
private ResourceSet _vdSetProjMatrix;
private Texture _vdTexture;
private Sampler _vdSampler;
private DeviceBuffer _vdProjMatrixUniformBuffer;
private int _vdLastWidth;
private int _vdLastHeight;
private VdFencedDatum[] _fencedData = Array.Empty<VdFencedDatum>();
private void InitVeldrid()
{
var options = new GraphicsDeviceOptions
{
#if DEBUG
Debug = true,
#endif
HasMainSwapchain = true,
SyncToVerticalBlank = _vsync,
PreferStandardClipSpaceYDirection = true,
SwapchainSrgbFormat = true
};
GLFW.GetFramebufferSize(_window.WindowPtr, out var w, out var h);
var hwnd = GLFW.GetWin32Window(_window.WindowPtr);
var hinstance = GetModuleHandleA(null);
switch (_vdRenderer)
{
case VeldridRenderer.Vulkan:
_vdGfxDevice = GraphicsDevice.CreateVulkan(
options,
VkSurfaceSource.CreateWin32((nint) hinstance, hwnd),
(uint) w, (uint) h);
break;
case VeldridRenderer.D3D11:
_vdGfxDevice = GraphicsDevice.CreateD3D11(options, hwnd, (uint) w, (uint) h);
break;
case VeldridRenderer.OpenGL:
{
var platInfo = new OpenGLPlatformInfo(
(nint) _window.WindowPtr,
GLFW.GetProcAddress,
ptr => GLFW.MakeContextCurrent((Window*) ptr),
() => (nint) GLFW.GetCurrentContext(),
() => GLFW.MakeContextCurrent(null),
ptr => GLFW.DestroyWindow((Window*) ptr),
() => GLFW.SwapBuffers(_window.WindowPtr),
vsync => GLFW.SwapInterval(vsync ? 1 : 0));
_vdGfxDevice = GraphicsDevice.CreateOpenGL(options, platInfo, (uint) w, (uint) h);
break;
}
}
var factory = _vdGfxDevice.ResourceFactory;
_vdCommandList = factory.CreateCommandList();
_vdCommandList.Name = "Honk";
var vtxLayout = new VertexLayoutDescription(
new VertexElementDescription("Position", VertexElementFormat.Float2,
VertexElementSemantic.TextureCoordinate),
new VertexElementDescription("UV", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate),
new VertexElementDescription("Color", VertexElementFormat.Byte4_Norm,
VertexElementSemantic.TextureCoordinate));
var vtxShaderDesc = new ShaderDescription(
ShaderStages.Vertex,
Encoding.UTF8.GetBytes(VDVertexShader),
"main");
var fragShaderDesc = new ShaderDescription(
ShaderStages.Fragment,
Encoding.UTF8.GetBytes(VDFragmentShader),
"main");
_vdShaders = factory.CreateFromSpirv(vtxShaderDesc, fragShaderDesc);
_vdShaders[0].Name = "VertexShader";
_vdShaders[1].Name = "FragmentShader";
var layoutTexture = factory.CreateResourceLayout(new ResourceLayoutDescription(
new ResourceLayoutElementDescription(
"Texture",
ResourceKind.TextureReadOnly,
ShaderStages.Fragment),
new ResourceLayoutElementDescription(
"TextureSampler",
ResourceKind.Sampler,
ShaderStages.Fragment)));
layoutTexture.Name = "LayoutTexture";
var layoutProjMatrix = factory.CreateResourceLayout(new ResourceLayoutDescription(
new ResourceLayoutElementDescription(
"ProjMtx",
ResourceKind.UniformBuffer,
ShaderStages.Vertex)));
layoutProjMatrix.Name = "LayoutProjMatrix";
var pipelineDesc = new GraphicsPipelineDescription(
new BlendStateDescription(
RgbaFloat.White,
new BlendAttachmentDescription(
true,
BlendFactor.SourceAlpha,
BlendFactor.InverseSourceAlpha,
BlendFunction.Add,
BlendFactor.One,
BlendFactor.InverseSourceAlpha,
BlendFunction.Add)
),
DepthStencilStateDescription.Disabled,
new RasterizerStateDescription(
FaceCullMode.None,
PolygonFillMode.Solid,
FrontFace.Clockwise,
depthClipEnabled: false,
scissorTestEnabled: true),
PrimitiveTopology.TriangleList,
new ShaderSetDescription(new[] {vtxLayout}, _vdShaders),
new[] {layoutProjMatrix, layoutTexture},
new OutputDescription(
null,
new OutputAttachmentDescription(PixelFormat.B8_G8_R8_A8_UNorm_SRgb))
);
_vdPipeline = factory.CreateGraphicsPipeline(pipelineDesc);
_vdPipeline.Name = "MainPipeline";
_vdProjMatrixUniformBuffer = factory.CreateBuffer(new BufferDescription(
(uint) sizeof(Matrix4x4),
BufferUsage.Dynamic | BufferUsage.UniformBuffer));
_vdProjMatrixUniformBuffer.Name = "_vdProjMatrixUniformBuffer";
_vdSetProjMatrix = factory.CreateResourceSet(new ResourceSetDescription(
layoutProjMatrix,
_vdProjMatrixUniformBuffer));
_vdSetProjMatrix.Name = "_vdSetProjMatrix";
var io = ImGui.GetIO();
io.Fonts.GetTexDataAsRGBA32(out byte* pixels, out var width, out var height, out _);
_vdTexture = factory.CreateTexture(TextureDescription.Texture2D(
(uint) width, (uint) height,
mipLevels: 1,
arrayLayers: 1,
PixelFormat.R8_G8_B8_A8_UNorm_SRgb,
TextureUsage.Sampled));
_vdTexture.Name = "MainTexture";
_vdSampler = factory.CreateSampler(SamplerDescription.Linear);
_vdSampler.Name = "MainSampler";
_vdGfxDevice.UpdateTexture(
_vdTexture,
(IntPtr) pixels,
(uint) (width * height * 4),
x: 0, y: 0, z: 0,
(uint) width, (uint) height, depth: 1,
mipLevel: 0,
arrayLayer: 0);
_vdSetTexture = factory.CreateResourceSet(new ResourceSetDescription(
layoutTexture,
_vdTexture,
_vdSampler));
_vdSetTexture.Name = "SetTexture";
io.Fonts.SetTexID(0);
io.Fonts.ClearTexData();
_vdGfxDevice.ResizeMainWindow((uint) w, (uint) h);
_vdGfxDevice.SwapBuffers();
}
private void RenderVeldrid()
{
GLFW.GetFramebufferSize(_window.WindowPtr, out var fbW, out var fbH);
if (_vdLastWidth != fbW && _vdLastHeight != fbH)
{
_vdGfxDevice.ResizeMainWindow((uint) fbW, (uint) fbH);
_vdLastWidth = fbW;
_vdLastHeight = fbH;
}
_vdCommandList.Begin();
_vdCommandList.SetFramebuffer(_vdGfxDevice.SwapchainFramebuffer);
_vdCommandList.SetViewport(0, new Viewport(0, 0, fbW, fbH, 0, 1));
_vdCommandList.ClearColorTarget(0, RgbaFloat.Black);
var factory = _vdGfxDevice.ResourceFactory;
var drawData = ImGui.GetDrawData();
ref var fencedData = ref GetFreeFencedData();
ref var vtxBuf = ref fencedData.VertexBuffer;
ref var idxBuf = ref fencedData.IndexBuffer;
var byteLenVtx = (uint) (sizeof(ImDrawVert) * drawData.TotalVtxCount);
if (fencedData.VertexBuffer == null || vtxBuf.SizeInBytes < byteLenVtx)
{
vtxBuf?.Dispose();
vtxBuf = factory.CreateBuffer(new BufferDescription(
byteLenVtx,
BufferUsage.VertexBuffer | BufferUsage.Dynamic));
vtxBuf.Name = "_vdVtxBuffer";
}
var byteLenIdx = (uint) (sizeof(ushort) * drawData.TotalIdxCount);
if (idxBuf == null || idxBuf.SizeInBytes < byteLenIdx)
{
idxBuf?.Dispose();
idxBuf = factory.CreateBuffer(new BufferDescription(
byteLenIdx,
BufferUsage.IndexBuffer | BufferUsage.Dynamic));
idxBuf.Name = "_vdIdxBuffer";
}
var vtxOffset = 0;
var idxOffset = 0;
var mappedVtxBuf = MappedToSpan(_vdGfxDevice.Map<ImDrawVert>(vtxBuf, MapMode.Write));
var mappedIdxBuf = MappedToSpan(_vdGfxDevice.Map<ushort>(idxBuf, MapMode.Write));
var l = drawData.DisplayPos.X;
var r = drawData.DisplayPos.X + drawData.DisplaySize.X;
var t = drawData.DisplayPos.Y;
var b = drawData.DisplayPos.Y + drawData.DisplaySize.Y;
var matrix = Matrix4x4.CreateOrthographicOffCenter(l, r, b, t, -1, 1);
var clipOff = drawData.DisplayPos;
var clipScale = drawData.FramebufferScale;
_vdCommandList.UpdateBuffer(_vdProjMatrixUniformBuffer, 0, ref matrix);
_vdCommandList.SetPipeline(_vdPipeline);
_vdCommandList.SetGraphicsResourceSet(0, _vdSetProjMatrix);
_vdCommandList.SetGraphicsResourceSet(1, _vdSetTexture);
_vdCommandList.SetVertexBuffer(0, vtxBuf);
_vdCommandList.SetIndexBuffer(idxBuf, IndexFormat.UInt16);
for (var n = 0; n < drawData.CmdListsCount; n++)
{
var drawList = drawData.CmdListsRange[n];
var drawVtx = new Span<ImDrawVert>((void*) drawList.VtxBuffer.Data, drawList.VtxBuffer.Size);
var drawIdx = new Span<ushort>((void*) drawList.IdxBuffer.Data, drawList.IdxBuffer.Size);
drawVtx.CopyTo(mappedVtxBuf[vtxOffset..]);
drawIdx.CopyTo(mappedIdxBuf[idxOffset..]);
for (var cmdI = 0; cmdI < drawList.CmdBuffer.Size; cmdI++)
{
var cmd = drawList.CmdBuffer[cmdI];
Vector4 clipRect = default;
clipRect.X = (cmd.ClipRect.X - clipOff.X) * clipScale.X;
clipRect.Y = (cmd.ClipRect.Y - clipOff.Y) * clipScale.Y;
clipRect.Z = (cmd.ClipRect.Z - clipOff.X) * clipScale.X;
clipRect.W = (cmd.ClipRect.W - clipOff.Y) * clipScale.Y;
_vdCommandList.SetScissorRect(
0,
(uint) clipRect.X,
(uint) clipRect.Y,
(uint) (clipRect.Z - clipRect.X),
(uint) (clipRect.W - clipRect.Y));
_vdCommandList.DrawIndexed(
cmd.ElemCount,
1,
(uint) (cmd.IdxOffset + idxOffset),
(int) (cmd.VtxOffset + vtxOffset),
0);
}
vtxOffset += drawVtx.Length;
idxOffset += drawIdx.Length;
}
_vdGfxDevice.Unmap(vtxBuf);
_vdGfxDevice.Unmap(idxBuf);
_vdCommandList.End();
_vdGfxDevice.SubmitCommands(_vdCommandList, fencedData.Fence);
_vdGfxDevice.SwapBuffers();
}
private ref VdFencedDatum GetFreeFencedData()
{
for (var i = 0; i < _fencedData.Length; i++)
{
ref var fenced = ref _fencedData[i];
if (fenced.Fence.Signaled)
{
fenced.Fence.Reset();
return ref fenced;
}
}
Array.Resize(ref _fencedData, _fencedData.Length + 1);
ref var slot = ref _fencedData[^1];
slot = new VdFencedDatum {Fence = _vdGfxDevice.ResourceFactory.CreateFence(false)};
return ref slot;
}
private static Span<T> MappedToSpan<T>(MappedResourceView<T> mapped) where T : struct
{
return MemoryMarshal.CreateSpan(ref mapped[0], mapped.Count);
}
[DllImport("kernel32.dll")]
private static extern void* GetModuleHandleA(byte* lpModuleName);
private struct VdFencedDatum
{
public Fence Fence;
public DeviceBuffer IndexBuffer;
public DeviceBuffer VertexBuffer;
}
private enum VeldridRenderer
{
Vulkan,
D3D11,
OpenGL
}
}
}