aboutsummaryrefslogtreecommitdiffstatsci
path: root/CrowWindow/VkCrowWindow.cs
blob: a7e6f74a530a9d3d90e839ba763e0eeb80aba7f7 (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
// Copyright (c) 2019  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 Glfw;
using Vulkan;
using Crow;
using System.Threading;

namespace vke {
	/// <summary>
	/// Vulkan context with Crow enabled window.
	/// Crow vector drawing is handled with Cairo Image on an Host mapped vulkan image.
	/// This is an easy way to have GUI in my samples with low GPU cost. Most of the ui
	/// is cached on cpu memory images.
	/// </summary>
	public class CrowWindow : VkWindow, IValueChange {
		#region IValueChange implementation
		public event EventHandler<ValueChangeEventArgs> ValueChanged;
		public virtual void NotifyValueChanged (string MemberName, object _value)
		{
			ValueChanged?.Invoke (this, new ValueChangeEventArgs (MemberName, _value));
		}
		#endregion

		protected CrowWindow() : base("vkChess.net", 800, 600,false) {}
		public bool MouseIsInInterface =>
			iFace.HoverWidget != null;

		protected FSQPipeline fsqPl;
		DescriptorPool dsPool;
		protected DescriptorSet descSet;
		CommandPool cmdPoolCrow;
		PrimaryCommandBuffer cmdUpdateCrow;
		Image crowImage;
		HostBuffer crowBuffer;
		protected Interface iFace;
		protected RenderPass renderPass;

		volatile bool running;


		VkDescriptorSetLayoutBinding dslBinding = new VkDescriptorSetLayoutBinding (0, VkShaderStageFlags.Fragment, VkDescriptorType.CombinedImageSampler);
		FrameBuffers frameBuffers;

		protected override void initVulkan () {
			base.initVulkan ();

			iFace = new Interface ((int)Width, (int)Height, WindowHandle);
			iFace.Init ();

			CreateRenderPass ();

			fsqPl = new FSQPipeline (renderPass,
				new PipelineLayout (dev, new DescriptorSetLayout (dev, dslBinding)));

			cmdPoolCrow = new CommandPool (presentQueue, VkCommandPoolCreateFlags.ResetCommandBuffer);
			cmdUpdateCrow = cmdPoolCrow.AllocateCommandBuffer ();

			dsPool = new DescriptorPool (dev, 1, new VkDescriptorPoolSize (VkDescriptorType.CombinedImageSampler));
			descSet = dsPool.Allocate (fsqPl.Layout.DescriptorSetLayouts[0]);

			Thread ui = new Thread (crowThread);
			ui.IsBackground = true;
			ui.Start ();
		}
		protected virtual void CreateRenderPass () {
			//renderPass = new RenderPass (dev, swapChain.ColorFormat, VkSampleCountFlags.SampleCount1);			
			renderPass = new RenderPass (dev, VkSampleCountFlags.SampleCount1);
			renderPass.AddAttachment (swapChain.ColorFormat, VkImageLayout.PresentSrcKHR, VkSampleCountFlags.SampleCount1,
				VkAttachmentLoadOp.Load, VkAttachmentStoreOp.DontCare, VkImageLayout.ColorAttachmentOptimal);//final outpout
			SubPass subpass0 = new SubPass ();
			subpass0.AddColorReference (0, VkImageLayout.ColorAttachmentOptimal);
			renderPass.AddSubpass (subpass0);
			renderPass.AddDependency (Vk.SubpassExternal, 0,
				VkPipelineStageFlags.BottomOfPipe, VkPipelineStageFlags.ColorAttachmentOutput,
				VkAccessFlags.MemoryRead, VkAccessFlags.ColorAttachmentWrite);
			renderPass.AddDependency (0, Vk.SubpassExternal,
				VkPipelineStageFlags.ColorAttachmentOutput, VkPipelineStageFlags.BottomOfPipe,
				VkAccessFlags.ColorAttachmentWrite, VkAccessFlags.MemoryRead);
		}


		protected override void onMouseMove (double xPos, double yPos)
		{
			if (iFace.OnMouseMove ((int)xPos, (int)yPos))
				return;
			//base.onMouseMove (xPos, yPos);
		}
		protected override void onMouseButtonDown (MouseButton button) {
			if (iFace.OnMouseButtonDown (button))
				return;
			base.onMouseButtonDown (button);
		}
		protected override void onMouseButtonUp (MouseButton button)
		{
			if (iFace.OnMouseButtonUp (button))
				return;
			base.onMouseButtonUp (button);
		}
		protected override void onScroll (double xOffset, double yOffset) {
			if (iFace.OnMouseWheelChanged ((float)yOffset))
				return;
			base.onScroll (xOffset, yOffset);
		}
		protected override void onChar (CodePoint cp) {
			if (iFace.OnKeyPress (cp.ToChar()))
				return;
			base.onChar (cp);
		}
		protected override void onKeyUp (Key key, int scanCode, Modifier modifiers) {
			if (iFace.OnKeyUp (key))
				return;
			base.onKeyUp (key, scanCode, modifiers);
		}
		protected override void onKeyDown (Key key, int scanCode, Modifier modifiers) {
			if (iFace.OnKeyDown (key))
				return;
			base.onKeyDown (key, scanCode, modifiers);
		}

		protected override void render () {

			int idx = swapChain.GetNextImage ();
			if (idx < 0) {
				OnResize ();
				return;
			}

			if (cmds[idx] == null)
				return;

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

			if (Monitor.IsEntered (iFace.UpdateMutex))
				Monitor.Exit (iFace.UpdateMutex);

			presentQueue.Submit (cmds[idx], swapChain.presentComplete, drawComplete[idx], drawFence);
			presentQueue.Present (swapChain, drawComplete[idx]);
		}

		public override void Update () {
			if (iFace.IsDirty) {
				drawFence.Wait ();
				drawFence.Reset ();
				Monitor.Enter (iFace.UpdateMutex);
				presentQueue.Submit (cmdUpdateCrow, default, default, drawFence);
				iFace.IsDirty = false;
			}

			NotifyValueChanged ("fps", fps);
		}

		protected override void OnResize ()
		{
			base.OnResize ();
			dev.WaitIdle ();
			initCrowSurface ();
			iFace.ProcessResize (new Rectangle (0, 0, (int)Width, (int)Height));
			
			frameBuffers?.Dispose();
			frameBuffers = renderPass.CreateFrameBuffers(swapChain);
		}

		protected virtual void recordUICmd (PrimaryCommandBuffer cmd, int imageIndex) {			
			renderPass.Begin(cmd, frameBuffers[imageIndex]);
			fsqPl.BindDescriptorSet (cmd, descSet, 0);
			fsqPl.RecordDraw (cmd);
			renderPass.End (cmd);
		}

		void crowThread () {
			while (iFace.surf == null) {
				Thread.Sleep (10);
			}
			running = true;
			while (running) {
				iFace.Update ();
				Thread.Sleep (10);
			}
		}
		void initCrowSurface () {
			lock (iFace.UpdateMutex) {
				iFace.surf?.Dispose ();
				crowImage?.Dispose ();
				crowBuffer?.Dispose ();

				crowBuffer = new HostBuffer (dev, VkBufferUsageFlags.TransferSrc | VkBufferUsageFlags.TransferDst, Width * Height * 4, true);

				crowImage = new Image (dev, VkFormat.B8g8r8a8Unorm, VkImageUsageFlags.Sampled | VkImageUsageFlags.TransferDst,
					VkMemoryPropertyFlags.DeviceLocal, Width, Height, VkImageType.Image2D, VkSampleCountFlags.SampleCount1, VkImageTiling.Linear);
				crowImage.CreateView (VkImageViewType.ImageView2D, VkImageAspectFlags.Color);
				crowImage.CreateSampler (VkFilter.Nearest, VkFilter.Nearest, VkSamplerMipmapMode.Nearest, VkSamplerAddressMode.ClampToBorder);
				crowImage.Descriptor.imageLayout = VkImageLayout.ShaderReadOnlyOptimal;

				DescriptorSetWrites dsw = new DescriptorSetWrites (descSet, dslBinding);
				dsw.Write (dev, crowImage.Descriptor);

				iFace.surf = new Crow.Cairo.ImageSurface (crowBuffer.MappedData, Crow.Cairo.Format.ARGB32,
					(int)Width, (int)Height, (int)Width * 4);

				PrimaryCommandBuffer cmd = cmdPoolCrow.AllocateAndStart (VkCommandBufferUsageFlags.OneTimeSubmit);
				crowImage.SetLayout (cmd, VkImageAspectFlags.Color, VkImageLayout.Preinitialized, VkImageLayout.ShaderReadOnlyOptimal);
				presentQueue.EndSubmitAndWait (cmd, true);

				recordUpdateCrowCmd ();
			}
		}

		/// <summary>
		/// command buffer must have been reseted
		/// </summary>
		void recordUpdateCrowCmd () {
			cmdPoolCrow.Reset ();
			cmdUpdateCrow.Start ();
			crowImage.SetLayout (cmdUpdateCrow, VkImageAspectFlags.Color,
				VkImageLayout.ShaderReadOnlyOptimal, VkImageLayout.TransferDstOptimal,
				VkPipelineStageFlags.FragmentShader, VkPipelineStageFlags.Transfer);

			crowBuffer.CopyTo (cmdUpdateCrow, crowImage, VkImageLayout.ShaderReadOnlyOptimal);

			crowImage.SetLayout (cmdUpdateCrow, VkImageAspectFlags.Color,
				VkImageLayout.TransferDstOptimal, VkImageLayout.ShaderReadOnlyOptimal,
				VkPipelineStageFlags.Transfer, VkPipelineStageFlags.FragmentShader);
			cmdUpdateCrow.End ();
		}


		protected void loadWindow (string path, object dataSource = null) {
			try {
				Widget w = iFace.FindByName (path);
				if (w != null) {
					iFace.PutOnTop (w);
					return;
				}
				w = iFace.Load (path);
				w.Name = path;
				w.DataSource = dataSource;

			} catch (Exception ex) {
				System.Diagnostics.Debug.WriteLine (ex);
			}
		}
		protected void loadIMLFragment (string imlFragment, object dataSource = null) {			
			iFace.LoadIMLFragment (imlFragment).DataSource = dataSource;			
		}
		protected T loadIMLFragment<T> (string imlFragment, object dataSource = null) {			
			Widget tmp = iFace.LoadIMLFragment (imlFragment);
			tmp.DataSource = dataSource;
			return (T)Convert.ChangeType (tmp,typeof(T));
		}
		protected void closeWindow (string path) {
			Widget g = iFace.FindByName (path);
			if (g != null)
				iFace.DeleteWidget (g);
		}


		protected override void Dispose (bool disposing) {
			dev.WaitIdle ();

			running = false;
			frameBuffers?.Dispose();
			fsqPl.Dispose ();
			dsPool.Dispose ();
			cmdPoolCrow.Dispose ();
			crowImage?.Dispose ();
			crowBuffer?.Dispose ();
			iFace.Dispose ();

			base.Dispose (disposing);
		}

	}
}