From b7eac53bd857df2dcb3de4ed70b8c38101a1b5dc Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Tue, 29 Oct 2019 05:47:49 +0100 Subject: [PATCH] debug --- addons/gltfLoader/PbrModelTexArray.cs | 2 + addons/gltfLoader/glTFLoader.cs | 25 +++--- vke/src/base/GraphicPipelineConfig.cs | 2 +- vke/src/base/Image.cs | 2 +- vke/src/base/PhysicalDevice.cs | 115 ++++++++++++++------------ vke/src/base/Queue.cs | 4 +- vke/vke.csproj | 2 +- 7 files changed, 83 insertions(+), 69 deletions(-) diff --git a/addons/gltfLoader/PbrModelTexArray.cs b/addons/gltfLoader/PbrModelTexArray.cs index 8ac9d0f..abde06a 100644 --- a/addons/gltfLoader/PbrModelTexArray.cs +++ b/addons/gltfLoader/PbrModelTexArray.cs @@ -1,6 +1,7 @@ // Copyright (c) 2019 Jean-Philippe Bruyère // // This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) +using System; using System.Diagnostics; using System.Numerics; using System.Runtime.InteropServices; @@ -49,6 +50,7 @@ namespace vke.glTF { public float roughnessFactor; public float alphaMask; public float alphaMaskCutoff; + } public Image texArray; diff --git a/addons/gltfLoader/glTFLoader.cs b/addons/gltfLoader/glTFLoader.cs index 50ad8dc..94fabd3 100644 --- a/addons/gltfLoader/glTFLoader.cs +++ b/addons/gltfLoader/glTFLoader.cs @@ -87,10 +87,10 @@ namespace vke.glTF { this.path = path; transferQ = _transferQ; cmdPool = _cmdPool; - baseDirectory = System.IO.Path.GetDirectoryName (path); - gltf = Interface.LoadModel (path); ; - loadedBuffers = new byte[gltf.Buffers.Length][]; - bufferHandles = new GCHandle[gltf.Buffers.Length]; + baseDirectory = Path.GetDirectoryName (path); + gltf = Interface.LoadModel (path); + loadedBuffers = new byte [gltf.Buffers.Length] []; + bufferHandles = new GCHandle [gltf.Buffers.Length]; } static byte[] loadDataUri (GL.Image img) { @@ -102,7 +102,7 @@ namespace vke.glTF { return Convert.FromBase64String (buff.Uri.Substring (idxComa + 1)); } - void EnsureBufferIsLoaded (int bufferIdx) { + void ensureBufferIsLoaded (int bufferIdx) { if (loadedBuffers[bufferIdx] == null) { //load full buffer string uri = gltf.Buffers[bufferIdx].Uri; @@ -143,6 +143,7 @@ namespace vke.glTF { public Mesh[] LoadMeshes (VkIndexType indexType, Buffer vbo, ulong vboOffset, Buffer ibo, ulong iboOffset) { ulong vCount, iCount; VkIndexType idxType; + GetVertexCount (out vCount, out iCount, out idxType); int vertexByteSize = Marshal.SizeOf (); @@ -179,19 +180,19 @@ namespace vke.glTF { int accessorIdx; if (p.Attributes.TryGetValue ("POSITION", out accessorIdx)) { AccPos = gltf.Accessors[accessorIdx]; - EnsureBufferIsLoaded (gltf.BufferViews[(int)AccPos.BufferView].Buffer); + ensureBufferIsLoaded (gltf.BufferViews[(int)AccPos.BufferView].Buffer); } if (p.Attributes.TryGetValue ("NORMAL", out accessorIdx)) { AccNorm = gltf.Accessors[accessorIdx]; - EnsureBufferIsLoaded (gltf.BufferViews[(int)AccNorm.BufferView].Buffer); + ensureBufferIsLoaded (gltf.BufferViews[(int)AccNorm.BufferView].Buffer); } if (p.Attributes.TryGetValue ("TEXCOORD_0", out accessorIdx)) { AccUv = gltf.Accessors[accessorIdx]; - EnsureBufferIsLoaded (gltf.BufferViews[(int)AccUv.BufferView].Buffer); + ensureBufferIsLoaded (gltf.BufferViews[(int)AccUv.BufferView].Buffer); } if (p.Attributes.TryGetValue ("TEXCOORD_1", out accessorIdx)) { AccUv1 = gltf.Accessors[accessorIdx]; - EnsureBufferIsLoaded (gltf.BufferViews[(int)AccUv1.BufferView].Buffer); + ensureBufferIsLoaded (gltf.BufferViews[(int)AccUv1.BufferView].Buffer); } Primitive prim = new Primitive { @@ -228,7 +229,7 @@ namespace vke.glTF { inUv1Ptr += AccUv1.ByteOffset + bv.ByteOffset; } - + //TODO: use vertex attributes scan for copying data if they exists for (int j = 0; j < prim.vertexCount; j++) { System.Buffer.MemoryCopy (inPosPtr, stagVertPtr, 12, 12); inPosPtr += 12; @@ -432,7 +433,7 @@ namespace vke.glTF { if (img.BufferView != null) {//load image from gltf buffer view GL.BufferView bv = gltf.BufferViews[(int)img.BufferView]; - EnsureBufferIsLoaded (bv.Buffer); + ensureBufferIsLoaded (bv.Buffer); vkimg = Image.Load (dev, bufferHandles[bv.Buffer].AddrOfPinnedObject () + bv.ByteOffset, (ulong)bv.ByteLength, VkImageUsageFlags.TransferSrc); } else if (img.Uri.StartsWith ("data:", StringComparison.Ordinal)) {//load base64 encoded image Debug.WriteLine ("loading embedded image {0} : {1}", img.Name, img.MimeType); @@ -514,7 +515,7 @@ namespace vke.glTF { if (img.BufferView != null) {//load image from gltf buffer view GL.BufferView bv = gltf.BufferViews[(int)img.BufferView]; - EnsureBufferIsLoaded (bv.Buffer); + ensureBufferIsLoaded (bv.Buffer); vkimg = Image.Load (dev, transferQ, cmdPool, bufferHandles[bv.Buffer].AddrOfPinnedObject () + bv.ByteOffset, (ulong)bv.ByteLength); } else if (img.Uri.StartsWith ("data:", StringComparison.Ordinal)) {//load base64 encoded image Debug.WriteLine ("loading embedded image {0} : {1}", img.Name, img.MimeType); diff --git a/vke/src/base/GraphicPipelineConfig.cs b/vke/src/base/GraphicPipelineConfig.cs index 2b7c8d6..bca8ee6 100644 --- a/vke/src/base/GraphicPipelineConfig.cs +++ b/vke/src/base/GraphicPipelineConfig.cs @@ -26,7 +26,7 @@ namespace vke { public List dynamicStates = new List (); public List vertexBindings = new List (); public List vertexAttributes = new List (); - public readonly List shaders = new List (); + public List shaders = new List (); public VkBool32 ColorBlendLogicOpEnable = false; public VkLogicOp ColorBlendLogicOp; public Vector4 ColorBlendConstants; diff --git a/vke/src/base/Image.cs b/vke/src/base/Image.cs index 0527674..aa5eb44 100644 --- a/vke/src/base/Image.cs +++ b/vke/src/base/Image.cs @@ -33,7 +33,7 @@ namespace vke { public uint Height => CreateInfo.extent.height; public override bool IsLinar => CreateInfo.tiling == VkImageTiling.Linear; - VkImageLayout lastKnownLayout; + public VkImageLayout lastKnownLayout { get; private set; } protected override VkDebugMarkerObjectNameInfoEXT DebugMarkerInfo => new VkDebugMarkerObjectNameInfoEXT(VkDebugReportObjectTypeEXT.ImageEXT, handle.Handle); diff --git a/vke/src/base/PhysicalDevice.cs b/vke/src/base/PhysicalDevice.cs index c82e20e..d08bc4f 100644 --- a/vke/src/base/PhysicalDevice.cs +++ b/vke/src/base/PhysicalDevice.cs @@ -15,15 +15,16 @@ namespace vke { /// Collection of physical devices returned by the vulkan instance. /// public class PhysicalDeviceCollection : IEnumerable { - readonly VkInstance inst; - readonly PhysicalDevice[] phys; + readonly VkInstance inst; + readonly PhysicalDevice [] phys; /// /// Retrieve the physical devices available for the provided vulkan instance /// /// The vulkan instance to retrieve the physical devices from. - public PhysicalDeviceCollection (VkInstance instance) { - inst = instance; + public PhysicalDeviceCollection (VkInstance instance) + { + inst = instance; CheckResult (vkEnumeratePhysicalDevices (inst, out uint gpuCount, IntPtr.Zero)); if (gpuCount <= 0) throw new Exception ("No GPU found"); @@ -31,26 +32,26 @@ namespace vke { IntPtr gpus = Marshal.AllocHGlobal (Marshal.SizeOf () * (int)gpuCount); CheckResult (vkEnumeratePhysicalDevices (inst, out gpuCount, gpus), "Could not enumerate physical devices."); - phys = new PhysicalDevice[gpuCount]; + phys = new PhysicalDevice [gpuCount]; for (int i = 0; i < gpuCount; i++) - phys[i] = new PhysicalDevice (Marshal.ReadIntPtr (gpus + i * Marshal.SizeOf ())); + phys [i] = new PhysicalDevice (Marshal.ReadIntPtr (gpus + i * Marshal.SizeOf ())); Marshal.FreeHGlobal (gpus); } - public PhysicalDevice this[int i] => phys[i]; - public IEnumerator GetEnumerator () => ((IEnumerable)phys).GetEnumerator (); - IEnumerator IEnumerable.GetEnumerator () => ((IEnumerable)phys).GetEnumerator (); - } + public PhysicalDevice this [int i] => phys [i]; + public IEnumerator GetEnumerator () => ((IEnumerable)phys).GetEnumerator (); + IEnumerator IEnumerable.GetEnumerator () => ((IEnumerable)phys).GetEnumerator (); + } /// /// Vke class that encapsulate a physical device. /// - public class PhysicalDevice { - readonly IntPtr phy; + public class PhysicalDevice { + readonly IntPtr phy; - public VkPhysicalDeviceMemoryProperties memoryProperties { get; private set; } - public VkQueueFamilyProperties[] QueueFamilies { get; private set; } + public VkPhysicalDeviceMemoryProperties memoryProperties { get; private set; } + public VkQueueFamilyProperties [] QueueFamilies { get; private set; } public VkPhysicalDeviceProperties Properties { get { vkGetPhysicalDeviceProperties (phy, out VkPhysicalDeviceProperties pdp); @@ -65,21 +66,23 @@ namespace vke { } public VkPhysicalDeviceLimits Limits => Properties.limits; - public bool HasSwapChainSupport { get; private set; } - public IntPtr Handle => phy; + public bool HasSwapChainSupport { get; private set; } + public IntPtr Handle => phy; public bool GetDeviceExtensionSupported (string extName) => SupportedExtensions (IntPtr.Zero).Contains (extName); #region CTOR - internal PhysicalDevice (IntPtr vkPhy) { - phy = vkPhy; + internal PhysicalDevice (IntPtr vkPhy) + { + phy = vkPhy; // Gather physical Device memory properties IntPtr tmp = Marshal.AllocHGlobal (Marshal.SizeOf ()); vkGetPhysicalDeviceMemoryProperties (phy, tmp); memoryProperties = Marshal.PtrToStructure (tmp); + Marshal.FreeHGlobal (tmp); vkGetPhysicalDeviceQueueFamilyProperties (phy, out uint queueFamilyCount, IntPtr.Zero); - QueueFamilies = new VkQueueFamilyProperties[queueFamilyCount]; + QueueFamilies = new VkQueueFamilyProperties [queueFamilyCount]; if (queueFamilyCount <= 0) throw new Exception ("No queues found for physical device"); @@ -91,17 +94,18 @@ namespace vke { } #endregion - public string[] SupportedExtensions (IntPtr layer) { + public string [] SupportedExtensions (IntPtr layer) + { CheckResult (vkEnumerateDeviceExtensionProperties (phy, layer, out uint count, IntPtr.Zero)); int sizeStruct = Marshal.SizeOf (); IntPtr ptrSupExts = Marshal.AllocHGlobal (sizeStruct * (int)count); CheckResult (vkEnumerateDeviceExtensionProperties (phy, layer, out count, ptrSupExts)); - string[] result = new string[count]; + string [] result = new string [count]; IntPtr tmp = ptrSupExts; for (int i = 0; i < count; i++) { - result[i] = Marshal.PtrToStringAnsi (tmp); + result [i] = Marshal.PtrToStringAnsi (tmp); tmp += sizeStruct; } @@ -109,37 +113,42 @@ namespace vke { return result; } - public bool GetPresentIsSupported (uint qFamilyIndex, VkSurfaceKHR surf) { - vkGetPhysicalDeviceSurfaceSupportKHR (phy, qFamilyIndex, surf, out VkBool32 isSupported); - return isSupported; - } - - public VkSurfaceCapabilitiesKHR GetSurfaceCapabilities (VkSurfaceKHR surf) { - vkGetPhysicalDeviceSurfaceCapabilitiesKHR (phy, surf, out VkSurfaceCapabilitiesKHR caps); - return caps; - } - - public VkSurfaceFormatKHR[] GetSurfaceFormats (VkSurfaceKHR surf) { - vkGetPhysicalDeviceSurfaceFormatsKHR (phy, surf, out uint count, IntPtr.Zero); - VkSurfaceFormatKHR[] formats = new VkSurfaceFormatKHR[count]; - - vkGetPhysicalDeviceSurfaceFormatsKHR (phy, surf, out count, formats.Pin()); + public bool GetPresentIsSupported (uint qFamilyIndex, VkSurfaceKHR surf) + { + vkGetPhysicalDeviceSurfaceSupportKHR (phy, qFamilyIndex, surf, out VkBool32 isSupported); + return isSupported; + } + + public VkSurfaceCapabilitiesKHR GetSurfaceCapabilities (VkSurfaceKHR surf) + { + vkGetPhysicalDeviceSurfaceCapabilitiesKHR (phy, surf, out VkSurfaceCapabilitiesKHR caps); + return caps; + } + + public VkSurfaceFormatKHR [] GetSurfaceFormats (VkSurfaceKHR surf) + { + vkGetPhysicalDeviceSurfaceFormatsKHR (phy, surf, out uint count, IntPtr.Zero); + VkSurfaceFormatKHR [] formats = new VkSurfaceFormatKHR [count]; + + vkGetPhysicalDeviceSurfaceFormatsKHR (phy, surf, out count, formats.Pin ()); formats.Unpin (); - - return formats; - } - public VkPresentModeKHR[] GetSurfacePresentModes (VkSurfaceKHR surf) { - vkGetPhysicalDeviceSurfacePresentModesKHR (phy, surf, out uint count, IntPtr.Zero); - VkPresentModeKHR[] modes = new VkPresentModeKHR[count]; - - vkGetPhysicalDeviceSurfacePresentModesKHR (phy, surf, out count, modes.Pin()); + + return formats; + } + public VkPresentModeKHR [] GetSurfacePresentModes (VkSurfaceKHR surf) + { + vkGetPhysicalDeviceSurfacePresentModesKHR (phy, surf, out uint count, IntPtr.Zero); + VkPresentModeKHR [] modes = new VkPresentModeKHR [count]; + + vkGetPhysicalDeviceSurfacePresentModesKHR (phy, surf, out count, modes.Pin ()); modes.Unpin (); - - return modes; - } - public VkFormatProperties GetFormatProperties (VkFormat format) { - vkGetPhysicalDeviceFormatProperties (phy, format, out VkFormatProperties properties); - return properties; - } - } + + return modes; + } + public VkFormatProperties GetFormatProperties (VkFormat format) + { + vkGetPhysicalDeviceFormatProperties (phy, format, out VkFormatProperties properties); + return properties; + } + } } diff --git a/vke/src/base/Queue.cs b/vke/src/base/Queue.cs index 6c3bad8..b37bd94 100644 --- a/vke/src/base/Queue.cs +++ b/vke/src/base/Queue.cs @@ -79,10 +79,12 @@ namespace vke { /// /// End command recording, submit, and wait queue idle /// - public void EndSubmitAndWait (CommandBuffer cmd) { + public void EndSubmitAndWait (CommandBuffer cmd, bool freeCommandBuffer = false) { cmd.End (); Submit (cmd); WaitIdle (); + if (freeCommandBuffer) + cmd.Free (); } public void Submit (CommandBuffer cmd, VkSemaphore wait = default(VkSemaphore), VkSemaphore signal = default (VkSemaphore), VkFence fence = default (VkFence)) { cmd.Submit (handle, wait, signal, fence); diff --git a/vke/vke.csproj b/vke/vke.csproj index 1430087..2d7aab6 100644 --- a/vke/vke.csproj +++ b/vke/vke.csproj @@ -49,7 +49,7 @@ - + -- 2.47.3