aboutsummaryrefslogtreecommitdiffstatsci
path: root/Backends/VkvgBackend/src/DefaultBackend.cs
blob: ac7ff1db13766a60ceeab38897792384f730718d (plain)
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
// Copyright (c) 2021  Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
//
// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Drawing2D;
using Glfw;
using vke;
using Vulkan;
using static Vulkan.Vk;
using Device = vke.Device;

namespace Crow.Backends
{
	public class DefaultBackend : IBackend
	{
		protected Instance instance;
		protected PhysicalDevice phy;
		protected vke.Device dev;
		protected Queue graphicQueue;	//for vkvg, we must have at least a graphic queue
		protected IntPtr hWin;			//GLFW window native pointer.
		protected VkSurfaceKHR hSurf;	//Vulkan Surface
		protected SwapChain swapChain;
		protected CommandPool cmdPool;
		protected PrimaryCommandBuffer[] cmds;
		protected VkSemaphore[] drawComplete;
		protected Fence drawFence;
		VkvgBackend.Surface surf;
		Crow.VkvgBackend.Device vkvgDev;
		SampleCount samples = SampleCount.Sample_1;
		bool vsync = false;

		bool tryGetPhy (vke.PhysicalDeviceCollection physicalDevices, VkPhysicalDeviceType phyType, out PhysicalDevice phy, bool swapchainSupport) {
			phy = physicalDevices.FirstOrDefault (p => p.Properties.deviceType == phyType && p.HasSwapChainSupport == swapchainSupport);
			return phy != null;
		}
		public DefaultBackend (int width, int height)
		{
#if DEBUG
			instance = new Instance (Ext.I.VK_EXT_debug_utils);
#else
			instance = new Instance ();
#endif
			vke.PhysicalDeviceCollection phys = instance.GetAvailablePhysicalDevice ();
			if (phys.Count() == 0)
				throw new Exception ("[Crow.VkvgBackend] No vulkan hardware found.");

			if (!tryGetPhy (phys, VkPhysicalDeviceType.DiscreteGpu, out phy, false))
				if (!tryGetPhy (phys, VkPhysicalDeviceType.IntegratedGpu, out phy, false))
					phy = phys[0];

			VkPhysicalDeviceFeatures enabledFeatures = default;
			dev = new vke.Device (phy);
			graphicQueue = new Queue (dev, VkQueueFlags.Graphics);
			dev.Activate (enabledFeatures);

			vkvgDev = new Crow.VkvgBackend.Device (
				instance.Handle, phy.Handle, dev.VkDev.Handle, graphicQueue.qFamIndex, samples);

			surf = new VkvgBackend.Surface (vkvgDev, (int)width, (int)height);
		}
		public DefaultBackend (ref IntPtr nativeWindoPointer, out bool ownGlfwWinHandle, int width, int height)
		{
			if (nativeWindoPointer == IntPtr.Zero) {
				Glfw3.Init ();
				Glfw3.WindowHint (WindowAttribute.ClientApi, 0);
				Glfw3.WindowHint (WindowAttribute.Resizable, 1);
				Glfw3.WindowHint (WindowAttribute.Decorated, 1);

				hWin = Glfw3.CreateWindow (width, height, "win name", MonitorHandle.Zero, IntPtr.Zero);
				if (hWin == IntPtr.Zero)
					throw new Exception ("[GLFW3] Unable to create Window");

				nativeWindoPointer = hWin;
				ownGlfwWinHandle = true;
			} else {
				hWin = nativeWindoPointer;
				ownGlfwWinHandle = false;
			}

			SwapChain.IMAGES_USAGE = VkImageUsageFlags.ColorAttachment | VkImageUsageFlags.TransferDst;
			SwapChain.PREFERED_FORMAT = VkFormat.B8g8r8a8Unorm;

			List<string> instExts = new List<string> (Glfw3.GetRequiredInstanceExtensions ());
#if DEBUG
			instExts.Add (Ext.I.VK_EXT_debug_utils);
#endif
			instance = new Instance (instExts.ToArray());
			hSurf = instance.CreateSurface (hWin);

			vke.PhysicalDeviceCollection phys = instance.GetAvailablePhysicalDevice ();
			if (phys.Count() == 0)
				throw new Exception ("[Crow.VkvgBackend] No vulkan hardware found.");

			if (!tryGetPhy (phys, VkPhysicalDeviceType.DiscreteGpu, out phy, true))
				if (!tryGetPhy (phys, VkPhysicalDeviceType.IntegratedGpu, out phy, true))
					phy = phys[0];

			VkPhysicalDeviceFeatures enabledFeatures = default;
			dev = new vke.Device (phy);
			graphicQueue = new PresentQueue (dev, VkQueueFlags.Graphics, hSurf);

			dev.Activate (enabledFeatures, Ext.D.VK_KHR_swapchain);

			swapChain = new SwapChain (graphicQueue as PresentQueue, (uint)width, (uint)height, SwapChain.PREFERED_FORMAT,
				vsync ? VkPresentModeKHR.FifoKHR : VkPresentModeKHR.ImmediateKHR);
			swapChain.Activate ();

			/*width = swapChain.Width;
			height = swapChain.Height;*/

			cmdPool = new CommandPool (dev, graphicQueue.qFamIndex, VkCommandPoolCreateFlags.ResetCommandBuffer);
			cmds = cmdPool.AllocateCommandBuffer (swapChain.ImageCount);

			drawComplete = new VkSemaphore[swapChain.ImageCount];
			drawFence = new Fence (dev, true, "draw fence");

			for (int i = 0; i < swapChain.ImageCount; i++) {
				drawComplete[i] = dev.CreateSemaphore ();
				drawComplete[i].SetDebugMarkerName (dev, "Semaphore DrawComplete" + i);
			}

			cmdPool.SetName ("main CmdPool");

			vkvgDev = new Crow.VkvgBackend.Device (
				instance.Handle, phy.Handle, dev.VkDev.Handle, graphicQueue.qFamIndex, samples);
			vkvgDev.SetDpy (72,72);

			createMainSurface ((uint)width, (uint)height);
		}
		~DefaultBackend ()
		{
			Dispose (false);
		}
		public virtual ISurface CreateSurface(int width, int height)
			=> new Crow.VkvgBackend.Surface (vkvgDev, width, height);
		public virtual ISurface CreateSurface(byte[] data, int width, int height)
			=> new Crow.VkvgBackend.Surface (vkvgDev, data, width, height);
		public ISurface MainSurface => surf;
		public IRegion CreateRegion () => new Crow.VkvgBackend.Region ();
		public IContext CreateContext (ISurface surf)
		{
			Crow.VkvgBackend.Context gr = new VkvgBackend.Context (surf as VkvgBackend.Surface);
			return gr;
		}
		//IPattern CreatePattern (PatternType patternType);
		public IGradient CreateGradient (GradientType gradientType, Rectangle bounds)
		{
			switch (gradientType) {
			case GradientType.Vertical:
				return new Crow.VkvgBackend.LinearGradient (bounds.Left, bounds.Top, bounds.Left, bounds.Bottom);
			case GradientType.Horizontal:
				return new Crow.VkvgBackend.LinearGradient (bounds.Left, bounds.Top, bounds.Right, bounds.Top);
			case GradientType.Oblic:
				return new Crow.VkvgBackend.LinearGradient (bounds.Left, bounds.Top, bounds.Right, bounds.Bottom);
			case GradientType.Radial:
				throw new NotImplementedException ();
			}
			return null;
		}
		public byte[] LoadBitmap (Stream stream, out Size dimensions)
		{
			byte[] image;
#if STB_SHARP
			StbImageSharp.ImageResult stbi = StbImageSharp.ImageResult.FromStream (stream, StbImageSharp.ColorComponents.RedGreenBlueAlpha);
			image = new byte[stbi.Data.Length];

			Array.Copy (stbi.Data, image, stbi.Data.Length);
			dimensions = new Size (stbi.Width, stbi.Height);
#else
			using (StbImage stbi = new StbImage (stream)) {
				image = new byte [stbi.Size];
				Marshal.Copy (stbi.Handle, image, 0, stbi.Size);
				dimensions = new Size (stbi.Width, stbi.Height);
			}
#endif
			return image;
		}
		public ISvgHandle LoadSvg(Stream stream)
		{
			using (BinaryReader sr = new BinaryReader (stream))
				return new VkvgBackend.SvgHandle(vkvgDev, sr.ReadBytes ((int)stream.Length));
		}
		public ISvgHandle LoadSvg(string svgFragment) =>
			new VkvgBackend.SvgHandle (vkvgDev,System.Text.Encoding.Unicode.GetBytes (svgFragment));
		bool disposeContextOnFlush;
		IRegion clipping;
		protected void clear(IContext ctx) {
			for (int i = 0; i < clipping.NumRectangles; i++)
				ctx.Rectangle (clipping.GetRectangle (i));

			ctx.ClipPreserve ();
			ctx.Operator = Operator.Clear;
			ctx.Fill ();
			ctx.Operator = Operator.Over;
		}
		public IContext PrepareUIFrame(IContext existingContext, IRegion clipping)
		{
			this.clipping = clipping;
			IContext ctx = existingContext;
			if (ctx == null) {
				disposeContextOnFlush = true;
				ctx = new VkvgBackend.Context (surf);
			} else
				disposeContextOnFlush = false;

			clear (ctx);

			return ctx;
		}
		public void FlushUIFrame(IContext ctx)
		{
			if (disposeContextOnFlush)
				ctx.Dispose ();
			clipping = null;

			dev.WaitIdle();

			int idx = swapChain.GetNextImage ();
			if (idx < 0) {
				createMainSurface (swapChain.Width, swapChain.Height);
				return;
			}

			drawFence.Wait ();
			drawFence.Reset ();

			graphicQueue.Submit (cmds[idx], swapChain.presentComplete, drawComplete[idx], drawFence);
			(graphicQueue as PresentQueue).Present (swapChain, drawComplete[idx]);

			dev.WaitIdle();
		}
		public void ResizeMainSurface (int width, int height) {
			//resize is done on swapchain image aquisition failure
		}
		vke.Image blitSource;
		void createMainSurface (uint width, uint height) {
			dev.WaitIdle();

			blitSource?.Dispose ();
			surf?.Dispose ();
			surf = new VkvgBackend.Surface (vkvgDev, (int)width, (int)height);

			cmdPool.Reset();

			blitSource = new vke.Image (dev,
				new Vulkan.VkImage((ulong)surf.VkImage.ToInt64()),
				Vulkan.VkFormat.B8g8r8a8Unorm,
				Vulkan.VkImageUsageFlags.TransferSrc | Vulkan.VkImageUsageFlags.TransferDst | Vulkan.VkImageUsageFlags.ColorAttachment,
				width, height);

			for (int i = 0; i < swapChain.ImageCount; i++) {
				vke.Image blitDest = swapChain.images[i];
				vke.PrimaryCommandBuffer cmd = cmds[i];
				cmd.Start();

				blitDest.SetLayout (cmd, VkImageAspectFlags.Color,
					VkImageLayout.Undefined, VkImageLayout.TransferDstOptimal,
					VkPipelineStageFlags.BottomOfPipe, VkPipelineStageFlags.Transfer);

				blitSource.SetLayout (cmd, VkImageAspectFlags.Color,
					VkImageLayout.ColorAttachmentOptimal, VkImageLayout.TransferSrcOptimal,
					VkPipelineStageFlags.ColorAttachmentOutput, VkPipelineStageFlags.Transfer);

				blitSource.BlitTo (cmd, blitDest, VkFilter.Nearest);

				blitDest.SetLayout (cmd, VkImageAspectFlags.Color,
					VkImageLayout.TransferDstOptimal, VkImageLayout.PresentSrcKHR,
					VkPipelineStageFlags.Transfer, VkPipelineStageFlags.BottomOfPipe);

				blitSource.SetLayout (cmd, VkImageAspectFlags.Color,
					VkImageLayout.TransferSrcOptimal, VkImageLayout.ColorAttachmentOptimal,
					VkPipelineStageFlags.Transfer, VkPipelineStageFlags.ColorAttachmentOutput);

				cmd.End ();
			}
			dev.WaitIdle ();
		}
		#region IDispose implementation
		bool isDisposed;
		public void Dispose ()
		{
			Dispose (true);
			GC.SuppressFinalize (this);
		}
		protected virtual void Dispose (bool disposing)
		{
			if (!isDisposed && disposing) {
				dev.WaitIdle ();

				surf.Dispose ();
				vkvgDev.Dispose ();

				for (int i = 0; i < swapChain.ImageCount; i++) {
					dev.DestroySemaphore (drawComplete[i]);
					cmds[i].Free ();
				}
				drawFence.Dispose ();
				swapChain.Dispose ();
				cmdPool.Dispose ();

				vkDestroySurfaceKHR (instance.Handle, hSurf, IntPtr.Zero);

				dev.Dispose ();
				instance.Dispose ();
			}
			isDisposed = true;
		}
		#endregion
	}
}