]> O.S.I.I.S - jp/vke.net.git/commitdiff
introduce span<t> for HostBuffer<T>, update triangle sample with it
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Mon, 22 Nov 2021 01:40:15 +0000 (02:40 +0100)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Mon, 22 Nov 2021 01:40:15 +0000 (02:40 +0100)
samples/Triangle/main.cs
vke/src/base/HostBuffer.cs
vke/src/base/Queue.cs
vke/src/base/Resource.cs

index 82b7374d16cd53f3ab830bd17eddefe75426a863..075f9f3dd79af773db12f5b44d6dff75280accff 100644 (file)
@@ -35,11 +35,9 @@ namespace Triangle {
                        }
                }
 
-               Matrix4x4 mvp;      //the model view projection matrix
-
                HostBuffer ibo;     //a host mappable buffer to hold the indices.
                HostBuffer vbo;     //a host mappable buffer to hold vertices.
-               HostBuffer uboMats; //a host mappable buffer for mvp matrice.
+               HostBuffer<Matrix4x4> uboMVPmatrix; //a host mappable buffer for mvp matrice.
 
                DescriptorPool descriptorPool;
                DescriptorSet descriptorSet;//descriptor set for the mvp matrice.
@@ -62,7 +60,7 @@ namespace Triangle {
                        vbo = new HostBuffer<Vertex> (dev, VkBufferUsageFlags.VertexBuffer, vertices);
                        ibo = new HostBuffer<ushort> (dev, VkBufferUsageFlags.IndexBuffer, indices);
                        //because mvp matrice may be updated by mouse move, we keep it mapped after creation.
-                       uboMats = new HostBuffer (dev, VkBufferUsageFlags.UniformBuffer, mvp, true);
+                       uboMVPmatrix = new HostBuffer<Matrix4x4> (dev, VkBufferUsageFlags.UniformBuffer, 1, true);
 
                        //a descriptor pool to allocate the mvp matrice descriptor from.
                        descriptorPool = new DescriptorPool (dev, 1, new VkDescriptorPoolSize (VkDescriptorType.UniformBuffer));
@@ -101,7 +99,7 @@ namespace Triangle {
                        //Write the content of the descriptor, the mvp matrice.
                        DescriptorSetWrites uboUpdate = new DescriptorSetWrites (descriptorSet, pipeline.Layout.DescriptorSetLayouts[0]);
                        //Descriptor property of the mvp buffer will return a default descriptor with no offset of the full size of the buffer.
-                       uboUpdate.Write (dev, uboMats.Descriptor);
+                       uboUpdate.Write (dev, uboMVPmatrix.Descriptor);
 
                        //allocate the default VkWindow buffers, one per swapchain image. Their will be only reset when rebuilding and not reallocated.
                        cmds = cmdPool.AllocateCommandBuffer (swapChain.ImageCount);
@@ -109,13 +107,12 @@ namespace Triangle {
 
                //view update override, see base method for more informations.
                public override void UpdateView () {
-                       mvp =
+                       uboMVPmatrix.AsSpan()[0] =
                                Matrix4x4.CreateFromAxisAngle (Vector3.UnitY, rotY) *
                                Matrix4x4.CreateFromAxisAngle (Vector3.UnitX, rotX) *
                                Matrix4x4.CreateTranslation (0, 0, -3f * zoom) *
                                Utils.CreatePerspectiveFieldOfView (Utils.DegreesToRadians (45f), (float)swapChain.Width / (float)swapChain.Height, 0.1f, 256.0f);
 
-                       uboMats.Update (mvp, (uint)Marshal.SizeOf<Matrix4x4> ());
                        base.UpdateView ();
                }
                protected override void onMouseMove (double xPos, double yPos) {
@@ -181,7 +178,7 @@ namespace Triangle {
                                        //resources have to be explicityly disposed.
                                        vbo.Dispose ();
                                        ibo.Dispose ();
-                                       uboMats.Dispose ();
+                                       uboMVPmatrix.Dispose ();
                                }
                        }
 
index f61cb845f7ccaa23e3a85dbf06afb1dde5b5911b..19d59e6dab591ff0808f37d51db6bb841e7237d0 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (c) 2019  Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
+// Copyright (c) 2019-2022  Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
 //
 // This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
 using System;
@@ -13,6 +13,7 @@ namespace vke {
        /// </summary>
        public class HostBuffer<T> : HostBuffer {
                int TSize;
+               int elementCount;
                /// <summary>
                /// Create an empty mappable vulkan buffer for elements of type T whith specified size.
                /// </summary>
@@ -21,8 +22,9 @@ namespace vke {
                /// <param name="arrayElementCount">Array element count.</param>
                /// <param name="keepMapped">If set to <c>true</c>, buffer will stay mapped after the constructor.</param>
                /// <param name="coherentMem">If set to <c>true</c> vulkan memory with have the coherent flag.</param>
-               public HostBuffer (Device device, VkBufferUsageFlags usage, uint arrayElementCount, bool keepMapped = false, bool coherentMem = true)
+               public HostBuffer (Device device, VkBufferUsageFlags usage, int arrayElementCount, bool keepMapped = false, bool coherentMem = true)
                        : base (device, usage, (ulong)(Marshal.SizeOf<T> () * arrayElementCount), keepMapped, coherentMem) {
+                       elementCount = arrayElementCount;
                        TSize = Marshal.SizeOf<T> ();
                }
                /// <summary>
@@ -36,11 +38,21 @@ namespace vke {
                public HostBuffer (Device device, VkBufferUsageFlags usage, IList<T> data, bool keepMapped = false, bool coherentMem = true)
                        : base (device, usage, (ulong)(Marshal.SizeOf<T> () * data.Count), keepMapped, coherentMem) {
                        TSize = Marshal.SizeOf<T> ();
+                       elementCount = data.Count;
                        Map ();
                        Update (data, createInfo.size);
                        if (!keepMapped)
                                Unmap ();
                }
+               public HostBuffer (Device device, VkBufferUsageFlags usage, T data, bool keepMapped = false, bool coherentMem = true)
+                       : base (device, usage, (ulong)(Marshal.SizeOf<T> ()), keepMapped, coherentMem) {
+                       TSize = Marshal.SizeOf<T> ();
+                       elementCount = 1;
+                       Map ();
+                       this.AsSpan()[0] = data;
+                       if (!keepMapped)
+                               Unmap ();
+               }
                /// <summary>
                /// Create and populate a mappable vulkan buffer.
                /// </summary>
@@ -96,6 +108,42 @@ namespace vke {
                        };
                        vkFlushMappedMemoryRanges (Dev.VkDev, 1, ref mr);
                }
+               /// <summary>
+               /// Retrieve a Span&lt;T&gt; on native memory of the buffer. Automatically Map it if not yet done.
+               /// </summary>
+               /// <returns>Span&lt;T&gt; on native memory valid as long as the buffer is mapped</returns>
+               public Span<T> AsSpan () {
+                       if (!IsMapped)
+                               Map();
+                       unsafe {
+                               return new Span<T>(mappedData.ToPointer(), elementCount);
+                       }
+               }
+               /// <summary>
+               /// Retrieve a Span&lt;T&gt; on native memory of the buffer. Automatically Map it if not yet done.
+               /// </summary>
+               /// <param name="startIndex">start index in the native memory</param>
+               /// <returns>Span&lt;T&gt; on native memory valid as long as the buffer is mapped</returns>
+               public Span<T> AsSpan (int startIndex) {
+                       if (!IsMapped)
+                               Map();
+                       unsafe {
+                               return new Span<T>(mappedData.ToPointer(), elementCount).Slice (startIndex);
+                       }
+               }
+               /// <summary>
+               /// Retrieve a Span&lt;T&gt; on native memory of the buffer. Automatically Map it if not yet done.
+               /// </summary>
+               /// <param name="startIndex">start index in the native memory</param>
+               /// <param name="lenght">leght of the span to return</param>
+               /// <returns>Span&lt;T&gt; on native memory valid as long as the buffer is mapped</returns>
+               public Span<T> AsSpan (int startIndex, int lenght) {
+                       if (!IsMapped)
+                               Map();
+                       unsafe {
+                               return new Span<T>(mappedData.ToPointer(), elementCount).Slice (startIndex, lenght);
+                       }
+               }
        }
        /// <summary>
        /// Mappable Buffer with HostVisble and HostCoherent memory flags
index 1818e7fe20ea4236c18c6793716fb2f371947c21..49b99b221063906e6a0b14d20ec3de543f38129d 100644 (file)
@@ -11,7 +11,7 @@ namespace vke {
     public class PresentQueue : Queue {
         public readonly VkSurfaceKHR Surface;
 
-        public PresentQueue (Device _dev, VkQueueFlags requestedFlags, VkSurfaceKHR _surface, float _priority = 0.0f) {        
+        public PresentQueue (Device _dev, VkQueueFlags requestedFlags, VkSurfaceKHR _surface, float _priority = 0.0f) {
             dev = _dev;
             priority = _priority;
             Surface = _surface;
@@ -19,16 +19,16 @@ namespace vke {
             qFamIndex = searchQFamily (requestedFlags);
             dev.queues.Add (this);
         }
-        
+
         uint searchQFamily (VkQueueFlags requestedFlags) {
             //search for dedicated Q
             for (uint i = 0; i < dev.phy.QueueFamilies.Length; i++) {
-                if (dev.phy.QueueFamilies[i].queueFlags == requestedFlags && dev.phy.GetPresentIsSupported (i, Surface)) 
+                if (dev.phy.QueueFamilies[i].queueFlags == requestedFlags && dev.phy.GetPresentIsSupported (i, Surface))
                     return i;
             }
             //search Q having flags
             for (uint i = 0; i < dev.phy.QueueFamilies.Length; i++) {
-                if ((dev.phy.QueueFamilies[i].queueFlags & requestedFlags) == requestedFlags && dev.phy.GetPresentIsSupported (i, Surface)) 
+                if ((dev.phy.QueueFamilies[i].queueFlags & requestedFlags) == requestedFlags && dev.phy.GetPresentIsSupported (i, Surface))
                     return i;
             }
 
index 0cd0e982875bee2904500a9a5a8de10652051a32..216a3d174504af80e044b47a91d6d6e7a600be57 100644 (file)
@@ -67,8 +67,7 @@ namespace vke {
                        Utils.CheckResult (vkAllocateMemory (Dev.VkDev, ref memInfo, IntPtr.Zero, out vkMemory));
                }
 #endif
-
-
+               public bool IsMapped => mappedData != IntPtr.Zero;
                public void Map (ulong offset = 0) {
 #if MEMORY_POOLS
                        if (!memoryPool.IsMapped)