Пример #1
0
        public void GetData()
        {
            var descriptorSetLayoutCreateInfo = new DescriptorSetLayoutCreateInfo(
                new DescriptorSetLayoutBinding(0, DescriptorType.StorageBuffer, 1, ShaderStages.Compute),
                new DescriptorSetLayoutBinding(1, DescriptorType.StorageBuffer, 1, ShaderStages.Compute));

            using (DescriptorSetLayout descriptorSetLayout = Device.CreateDescriptorSetLayout(descriptorSetLayoutCreateInfo))
                using (PipelineLayout pipelineLayout = Device.CreatePipelineLayout(new PipelineLayoutCreateInfo(new[] { descriptorSetLayout })))
                    using (ShaderModule shader = Device.CreateShaderModule(new ShaderModuleCreateInfo(ReadAllBytes("Shader.comp.spv"))))
                    {
                        var pipelineCreateInfo = new ComputePipelineCreateInfo(
                            new PipelineShaderStageCreateInfo(ShaderStages.Compute, shader, "main"),
                            pipelineLayout);

                        byte[] cacheBytes;

                        // Populate cache.
                        using (PipelineCache cache = Device.CreatePipelineCache())
                        {
                            using (Device.CreateComputePipeline(pipelineCreateInfo, cache)) { }
                            cacheBytes = cache.GetData();
                        }

                        Assert.False(cacheBytes.All(x => x == 0));

                        // Recreate pipeline from cache.
                        using (PipelineCache cache = Device.CreatePipelineCache(new PipelineCacheCreateInfo(cacheBytes)))
                        {
                            using (Device.CreateComputePipeline(pipelineCreateInfo, cache)) { }
                        }
                    }
        }
Пример #2
0
        void CreateDescriptorSetLayout()
        {
            /*
             * Here we specify a descriptor set layout. This allows us to bind our descriptors to
             * resources in the shader.
             */

            /*
             * Here we specify a binding of type VK_DESCRIPTOR_TYPE_STORAGE_BUFFER to the binding point
             * 0. This binds to
             * layout(std140, binding = 0) buffer buf
             * in the compute shader.
             */
            DescriptorSetLayoutBinding descriptorSetLayoutBinding = new DescriptorSetLayoutBinding()
            {
                Binding         = 0,                            // binding = 0
                DescriptorType  = DescriptorType.StorageBuffer, // VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
                DescriptorCount = 1,
                StageFlags      = ShaderStages.Compute          // VK_SHADER_STAGE_COMPUTE_BIT;
            };
            DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo = new DescriptorSetLayoutCreateInfo();

            // descriptorSetLayoutCreateInfo.bindingCount = 1; // only a single binding in this descriptor set layout.
            DescriptorSetLayoutBinding[] temp = { descriptorSetLayoutBinding };
            descriptorSetLayoutCreateInfo.Bindings = temp;

            // Create the descriptor set layout.
            descriptorSetLayout = device.CreateDescriptorSetLayout(descriptorSetLayoutCreateInfo);
        }
Пример #3
0
        private DescriptorSetLayout[] CreateDescriptorSetLayouts()
        {
            DescriptorSetLayoutBinding binding = new DescriptorSetLayoutBinding
            {
                Binding         = 0,
                DescriptorType  = DescriptorType.UniformBuffer,
                DescriptorCount = 1,
                StageFlags      = ShaderStageFlags.ShaderStageVertexBit
            };

            DescriptorSetLayoutCreateInfo createInfo = new DescriptorSetLayoutCreateInfo
            {
                SType        = StructureType.DescriptorSetLayoutCreateInfo,
                BindingCount = 1,
                PBindings    = &binding
            };

            DescriptorSetLayout layout;
            var res = VkApi.CreateDescriptorSetLayout(this.Device, &createInfo, null, &layout);

            if (res != Result.Success)
            {
                throw new VMASharp.VulkanResultException("Failed to create Descriptor Set Layout!", res);
            }

            return(new[] { layout });
        }
Пример #4
0
        public void CreateComputePipeline()
        {
            var descriptorSetLayoutCreateInfo = new DescriptorSetLayoutCreateInfo(
                new DescriptorSetLayoutBinding(0, DescriptorType.StorageBuffer, 1, ShaderStages.Compute),
                new DescriptorSetLayoutBinding(1, DescriptorType.StorageBuffer, 1, ShaderStages.Compute));

            using (DescriptorSetLayout descriptorSetLayout = Device.CreateDescriptorSetLayout(descriptorSetLayoutCreateInfo))
                using (PipelineLayout pipelineLayout = Device.CreatePipelineLayout(new PipelineLayoutCreateInfo(new[] { descriptorSetLayout })))
                    using (ShaderModule shader = Device.CreateShaderModule(new ShaderModuleCreateInfo(ReadAllBytes("Shader.comp.spv"))))
                        using (PipelineCache cache = Device.CreatePipelineCache())
                        {
                            var pipelineCreateInfo = new ComputePipelineCreateInfo(
                                new PipelineShaderStageCreateInfo(ShaderStages.Compute, shader, "main"),
                                pipelineLayout);

                            using (Device.CreateComputePipelines(new[] { pipelineCreateInfo })[0]) { }
                            using (Device.CreateComputePipelines(new[] { pipelineCreateInfo }, cache)[0]) { }
                            using (Device.CreateComputePipelines(new[] { pipelineCreateInfo }, allocator: CustomAllocator)[0]) { }
                            using (Device.CreateComputePipelines(new[] { pipelineCreateInfo }, cache, CustomAllocator)[0]) { }
                            using (Device.CreateComputePipeline(pipelineCreateInfo)) { }
                            using (Device.CreateComputePipeline(pipelineCreateInfo, allocator: CustomAllocator)) { }
                            using (Device.CreateComputePipeline(pipelineCreateInfo, cache)) { }
                            using (Device.CreateComputePipeline(pipelineCreateInfo, cache, CustomAllocator)) { }
                        }
        }
Пример #5
0
        public void UpdateSetsDescriptorWrite()
        {
            const int bufferSize = 256;

            var layoutCreateInfo = new DescriptorSetLayoutCreateInfo(
                new DescriptorSetLayoutBinding(0, DescriptorType.StorageBuffer, 1));
            var poolCreateInfo = new DescriptorPoolCreateInfo(
                1,
                new[] { new DescriptorPoolSize(DescriptorType.StorageBuffer, 1) },
                DescriptorPoolCreateFlags.FreeDescriptorSet);

            using (Buffer buffer = Device.CreateBuffer(new BufferCreateInfo(bufferSize, BufferUsages.StorageBuffer)))
                using (DeviceMemory memory = Device.AllocateMemory(new MemoryAllocateInfo(bufferSize, 0)))
                    using (DescriptorSetLayout layout = Device.CreateDescriptorSetLayout(layoutCreateInfo))
                        using (DescriptorPool pool = Device.CreateDescriptorPool(poolCreateInfo))
                            using (DescriptorSet set = pool.AllocateSets(new DescriptorSetAllocateInfo(1, layout))[0])
                            {
                                // Required to satisfy the validation layer.
                                buffer.GetMemoryRequirements();

                                buffer.BindMemory(memory);

                                var descriptorWrite = new WriteDescriptorSet(set, 0, 0, 1, DescriptorType.StorageBuffer,
                                                                             bufferInfo: new[] { new DescriptorBufferInfo(buffer) });
                                pool.UpdateSets(new[] { descriptorWrite });
                            }
        }
Пример #6
0
        protected override void CreateDescriptorSetLayout()
        {
            var uboLayoutBinding = new DescriptorSetLayoutBinding
            {
                Binding           = 0,
                DescriptorType    = DescriptorType.UniformBuffer,
                DescriptorCount   = 1,
                ImmutableSamplers = null,
                StageFlags        = ShaderStageFlags.Vertex
            };

            var samplerLayoutBinding = new DescriptorSetLayoutBinding
            {
                Binding           = 1,
                DescriptorCount   = 1,
                DescriptorType    = DescriptorType.CombinedImageSampler,
                ImmutableSamplers = null,
                StageFlags        = ShaderStageFlags.Fragment
            };

            var bindings = new[] { uboLayoutBinding, samplerLayoutBinding };

            var layoutInfo = new DescriptorSetLayoutCreateInfo
            {
                BindingCount = (uint)bindings.Length,
                Bindings     = bindings,
            };

            descriptorSetLayout = device.CreateDescriptorSetLayout(layoutInfo);
        }
Пример #7
0
        protected override unsafe DescriptorSetLayout[] CreateDescriptorSetLayouts(VulkanGraphicsDevice gd, Device device, out PipelineLayout layout)
        {
            DescriptorSetLayoutBinding uLayoutBindings = new DescriptorSetLayoutBinding
            {
                Binding         = 0,
                DescriptorType  = DescriptorType.UniformBuffer,
                DescriptorCount = 1,
                StageFlags      = ShaderStageFlags.ShaderStageVertexBit
            };

            DescriptorSetLayoutBinding tLayoutBindings = new DescriptorSetLayoutBinding
            {
                Binding         = 0,
                DescriptorType  = DescriptorType.CombinedImageSampler,
                DescriptorCount = 1,
                StageFlags      = ShaderStageFlags.ShaderStageFragmentBit
            };

            DescriptorSetLayout[] layouts = new DescriptorSetLayout[3];

            var uDescriptorSetLayoutCreateInfo = new DescriptorSetLayoutCreateInfo()
            {
                SType        = StructureType.DescriptorSetLayoutCreateInfo,
                PBindings    = &uLayoutBindings,
                BindingCount = 1
            };

            var sDescriptorSetLayoutCreateInfo = new DescriptorSetLayoutCreateInfo()
            {
                SType        = StructureType.DescriptorSetLayoutCreateInfo,
                BindingCount = 0
            };

            var tDescriptorSetLayoutCreateInfo = new DescriptorSetLayoutCreateInfo()
            {
                SType        = StructureType.DescriptorSetLayoutCreateInfo,
                PBindings    = &tLayoutBindings,
                BindingCount = 1
            };

            gd.Api.CreateDescriptorSetLayout(device, uDescriptorSetLayoutCreateInfo, null, out layouts[0]).ThrowOnError();
            gd.Api.CreateDescriptorSetLayout(device, sDescriptorSetLayoutCreateInfo, null, out layouts[1]).ThrowOnError();
            gd.Api.CreateDescriptorSetLayout(device, tDescriptorSetLayoutCreateInfo, null, out layouts[2]).ThrowOnError();

            fixed(DescriptorSetLayout *pLayouts = layouts)
            {
                var pipelineLayoutCreateInfo = new PipelineLayoutCreateInfo()
                {
                    SType          = StructureType.PipelineLayoutCreateInfo,
                    PSetLayouts    = pLayouts,
                    SetLayoutCount = 3
                };

                gd.Api.CreatePipelineLayout(device, &pipelineLayoutCreateInfo, null, out layout).ThrowOnError();
            }

            return(layouts);
        }
Пример #8
0
        internal static unsafe SharpVulkan.DescriptorSetLayout CreateNativeDescriptorSetLayout(GraphicsDevice device, IList <DescriptorSetLayoutBuilder.Entry> entries, out uint[] typeCounts)
        {
            var bindings          = new DescriptorSetLayoutBinding[entries.Count];
            var immutableSamplers = new Sampler[entries.Count];

            int usedBindingCount = 0;

            typeCounts = new uint[DescriptorTypeCount];

            fixed(Sampler *immutableSamplersPointer = &immutableSamplers[0])
            {
                for (int i = 0; i < entries.Count; i++)
                {
                    var entry = entries[i];

                    // TODO VULKAN: Special case for unused bindings in PipelineState. Handle more nicely.
                    if (entry.ArraySize == 0)
                    {
                        continue;
                    }

                    bindings[usedBindingCount] = new DescriptorSetLayoutBinding
                    {
                        DescriptorType  = VulkanConvertExtensions.ConvertDescriptorType(entry.Class, entry.Type),
                        StageFlags      = ShaderStageFlags.All, // TODO VULKAN: Filter?
                        Binding         = (uint)i,
                        DescriptorCount = (uint)entry.ArraySize
                    };

                    if (entry.ImmutableSampler != null)
                    {
                        // TODO VULKAN: Handle immutable samplers for DescriptorCount > 1
                        if (entry.ArraySize > 1)
                        {
                            throw new NotImplementedException();
                        }

                        // Remember this, so we can choose the right DescriptorType in DescriptorSet.SetShaderResourceView
                        immutableSamplers[i] = entry.ImmutableSampler.NativeSampler;
                        //bindings[i].DescriptorType = DescriptorType.CombinedImageSampler;
                        bindings[usedBindingCount].ImmutableSamplers = new IntPtr(immutableSamplersPointer + i);
                    }

                    typeCounts[(int)bindings[usedBindingCount].DescriptorType] += bindings[usedBindingCount].DescriptorCount;

                    usedBindingCount++;
                }

                var createInfo = new DescriptorSetLayoutCreateInfo
                {
                    StructureType = StructureType.DescriptorSetLayoutCreateInfo,
                    BindingCount  = (uint)usedBindingCount,
                    Bindings      = usedBindingCount > 0 ? new IntPtr(Interop.Fixed(bindings)) : IntPtr.Zero
                };

                return(device.NativeDevice.CreateDescriptorSetLayout(ref createInfo));
            }
        }
Пример #9
0
        DescriptorSetLayout CreateDescriptorSetLayout()
        {
            var layoutBinding = new DescriptorSetLayoutBinding {
                DescriptorType  = DescriptorType.UniformBuffer,
                DescriptorCount = 1,
                StageFlags      = ShaderStageFlags.Vertex
            };
            var descriptorSetLayoutCreateInfo = new DescriptorSetLayoutCreateInfo {
                Bindings = new DescriptorSetLayoutBinding [] { layoutBinding }
            };

            return(device.CreateDescriptorSetLayout(descriptorSetLayoutCreateInfo));
        }
Пример #10
0
        /*        public void Write(string name, DescriptorImageInfo myInfo)
         * internal void WriteUniform<T>(string v, T mYBuffer) where T : struct*/
        internal void AllocateFrom(DescriptorPool myNewPoolObject)
        {
            DescriptorSetLayout myBindings = GetDescriptorSetLayout();

            DescriptorSetLayoutCreateInfo myInfo = new DescriptorSetLayoutCreateInfo();

            DescriptorSetAllocateInfo mySetAlloc = new DescriptorSetAllocateInfo(); //TODO: Garbage and memory stuff

            mySetAlloc.SetLayouts     = new DescriptorSetLayout[] { myBindings };
            mySetAlloc.DescriptorPool = myNewPoolObject;

            myDSet = VulkanRenderer.SelectedLogicalDevice.AllocateDescriptorSets(mySetAlloc);
            // Debug.Assert(myDSet.Count() == myBindings.Count);
        }
Пример #11
0
        public void ResetPool()
        {
            var layoutCreateInfo = new DescriptorSetLayoutCreateInfo(
                new DescriptorSetLayoutBinding(0, DescriptorType.StorageBuffer, 1));
            var poolCreateInfo = new DescriptorPoolCreateInfo(
                1,
                new[] { new DescriptorPoolSize(DescriptorType.StorageBuffer, 1) });

            using (DescriptorSetLayout layout = Device.CreateDescriptorSetLayout(layoutCreateInfo))
                using (DescriptorPool pool = Device.CreateDescriptorPool(poolCreateInfo))
                {
                    pool.AllocateSets(new DescriptorSetAllocateInfo(1, layout));
                    pool.Reset();
                }
        }
Пример #12
0
        DescriptorSetLayout CreateDescriptorSetLayout()
        {
            var layoutBindings = new[]
            {
                new DescriptorSetLayoutBinding(0, DescriptorType.UniformBuffer, ShaderStageFlags.Vertex),
                new DescriptorSetLayoutBinding(1, DescriptorType.CombinedImageSampler, ShaderStageFlags.Fragment),
            };

            layoutBindings[0].DescriptorCount = 1;
            layoutBindings[1].DescriptorCount = 1;

            var createInfo = new DescriptorSetLayoutCreateInfo(layoutBindings);

            return(device.CreateDescriptorSetLayout(createInfo));
        }
Пример #13
0
        public void AllocateSetsAndFreeSets()
        {
            var layoutCreateInfo = new DescriptorSetLayoutCreateInfo(
                new DescriptorSetLayoutBinding(0, DescriptorType.StorageBuffer, 1));
            var poolCreateInfo = new DescriptorPoolCreateInfo(
                1,
                new[] { new DescriptorPoolSize(DescriptorType.StorageBuffer, 1) },
                DescriptorPoolCreateFlags.FreeDescriptorSet);

            using (DescriptorSetLayout layout = Device.CreateDescriptorSetLayout(layoutCreateInfo))
                using (DescriptorPool pool = Device.CreateDescriptorPool(poolCreateInfo))
                {
                    using (pool.AllocateSets(new DescriptorSetAllocateInfo(1, layout))[0]) { }
                    DescriptorSet set = pool.AllocateSets(new DescriptorSetAllocateInfo(1, layout))[0];
                    pool.FreeSets(set);
                }
        }
        private void CreateDescriptorSetLayout()
        {
            var uboLayoutBinding = new DescriptorSetLayoutBinding()
            {
                Binding         = 0,
                DescriptorType  = DescriptorType.UniformBuffer,
                DescriptorCount = 1,
                StageFlags      = ShaderStageFlags.Vertex,
            };

            var layoutInfo = new DescriptorSetLayoutCreateInfo()
            {
                BindingCount = 1,
                Bindings     = new DescriptorSetLayoutBinding[] { uboLayoutBinding },
            };

            vkDescriptorSetLayout = vkDevice.CreateDescriptorSetLayout(layoutInfo);
        }
Пример #15
0
        public void UpdateSetsDescriptorCopy()
        {
            var layoutCreateInfo = new DescriptorSetLayoutCreateInfo(
                new DescriptorSetLayoutBinding(0, DescriptorType.StorageBuffer, 1),
                new DescriptorSetLayoutBinding(1, DescriptorType.StorageBuffer, 1));
            var poolCreateInfo = new DescriptorPoolCreateInfo(
                1,
                new[] { new DescriptorPoolSize(DescriptorType.StorageBuffer, 2) });

            using (DescriptorSetLayout layout = Device.CreateDescriptorSetLayout(layoutCreateInfo))
                using (DescriptorPool pool = Device.CreateDescriptorPool(poolCreateInfo))
                {
                    DescriptorSet set = pool.AllocateSets(new DescriptorSetAllocateInfo(1, layout))[0];
                    // It is valid to copy from self to self (without overlapping memory boundaries).
                    var descriptorCopy = new CopyDescriptorSet(set, 0, 0, set, 1, 0, 1);
                    pool.UpdateSets(descriptorCopies: new[] { descriptorCopy });
                }
        }
Пример #16
0
        public void CreateDescriptorLayout()
        {
            DescriptorSetLayoutBinding dslb = new DescriptorSetLayoutBinding();

            dslb.Binding         = 0;
            dslb.DescriptorType  = DescriptorType.UniformBuffer;
            dslb.DescriptorCount = 1;
            dslb.StageFlags      = ShaderStages.Vertex;

            DescriptorSetLayoutCreateInfo dslci = new DescriptorSetLayoutCreateInfo();

            dslci.Bindings = new DescriptorSetLayoutBinding[] { dslb };

            mDSLs = new DescriptorSetLayout[mChainImageViews.Length];

            for (int i = 0; i < mChainImageViews.Length; i++)
            {
                mDSLs[i] = mLogical.CreateDescriptorSetLayout(dslci);
            }
        }
Пример #17
0
        public DescriptorSetLayout GetDescriptorSetLayout()
        {
            List <DescriptorSetLayoutBinding> myBindings = new List <DescriptorSetLayoutBinding>();

            foreach (var A in myBindingNumberToItems)
            {
                var Item = new DescriptorSetLayoutBinding();
                Item.DescriptorType  = (DescriptorType)A.Value.myType;
                Item.StageFlags      = A.Value.ShaderStage;
                Item.DescriptorCount = A.Value.DescriptorCount;
                Item.Binding         = A.Value.Binding;
                myBindings.Add(Item);
            }
            var descriptorSetLayoutCreateInfo = new DescriptorSetLayoutCreateInfo
            {
                Bindings = myBindings.ToArray()
            };

            return(new DescriptorSetLayout(VulkanRenderer.SelectedLogicalDevice, descriptorSetLayoutCreateInfo));
        }
Пример #18
0
        public unsafe DescriptorSetLayout(Api api, DescriptorBinding[] descriptorBindings)
        {
            _api = api;

            Span <DescriptorSetLayoutBinding> layoutBindings = stackalloc DescriptorSetLayoutBinding[descriptorBindings.Length];

            for (int i = 0; i < descriptorBindings.Length; i++)
            {
                layoutBindings[i].Binding         = descriptorBindings[i].Binding;
                layoutBindings[i].DescriptorCount = descriptorBindings[i].DescriptorCount;
                layoutBindings[i].DescriptorType  = descriptorBindings[i].Type;
                layoutBindings[i].StageFlags      = descriptorBindings[i].Stage;
            }

            var layoutInfo = new DescriptorSetLayoutCreateInfo();

            layoutInfo.SType        = StructureType.DescriptorSetLayoutCreateInfo;
            layoutInfo.BindingCount = (uint)layoutBindings.Length;
            layoutInfo.PBindings    = (DescriptorSetLayoutBinding *)Unsafe.AsPointer(ref layoutBindings[0]);

            Util.Verify(_api.Vk.CreateDescriptorSetLayout(_api.Device.VkDevice, layoutInfo, default, out _vkDescriptorSetLayout), $"{nameof(DescriptorSetLayout)}: Unable to create descriptor set layout");
Пример #19
0
        public void CmdBindPipeline()
        {
            var descriptorSetLayoutCreateInfo = new DescriptorSetLayoutCreateInfo(
                new DescriptorSetLayoutBinding(0, DescriptorType.StorageBuffer, 1, ShaderStages.Compute),
                new DescriptorSetLayoutBinding(1, DescriptorType.StorageBuffer, 1, ShaderStages.Compute));

            using (DescriptorSetLayout descriptorSetLayout = Device.CreateDescriptorSetLayout(descriptorSetLayoutCreateInfo))
                using (PipelineLayout pipelineLayout = Device.CreatePipelineLayout(new PipelineLayoutCreateInfo(new[] { descriptorSetLayout })))
                    using (ShaderModule shader = Device.CreateShaderModule(new ShaderModuleCreateInfo(ReadAllBytes("Shader.comp.spv"))))
                    {
                        var pipelineCreateInfo = new ComputePipelineCreateInfo(
                            new PipelineShaderStageCreateInfo(ShaderStages.Compute, shader, "main"),
                            pipelineLayout);

                        using (Pipeline pipeline = Device.CreateComputePipeline(pipelineCreateInfo))
                        {
                            CommandBuffer.Begin();
                            CommandBuffer.CmdBindPipeline(PipelineBindPoint.Compute, pipeline);
                            CommandBuffer.End();
                        }
                    }
        }
        public DescriptorSetLayout(Device selectedDevice, DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo)
        {
            AllocationCallbacks pAllocator = null;

            this.selectedDevice = selectedDevice;
            Result result;

            //DescriptorSetLayout pSetLayout;
            unsafe
            {
                //pSetLayout = new DescriptorSetLayout();

                fixed(UInt64 *ptrpSetLayout = &this.m)
                {
                    result = Interop.NativeMethods.vkCreateDescriptorSetLayout(selectedDevice.Handle, descriptorSetLayoutCreateInfo != null ? descriptorSetLayoutCreateInfo.m : (Interop.DescriptorSetLayoutCreateInfo *) default(IntPtr), pAllocator != null ? pAllocator.m : null, ptrpSetLayout);
                }

                if (result != Result.Success)
                {
                    throw new ResultException(result);
                }
            }
        }
        public DescriptorSetLayout(Device selectedDevice)
        {
            AllocationCallbacks pAllocator = null;

            this.selectedDevice = selectedDevice;

            var layoutBinding = new DescriptorSetLayoutBinding
            {
                DescriptorType  = DescriptorType.UniformBuffer,
                DescriptorCount = 1,
                StageFlags      = ShaderStageFlags.Vertex
            };


            var descriptorSetLayoutCreateInfo = new DescriptorSetLayoutCreateInfo
            {
                Bindings = new DescriptorSetLayoutBinding[] { layoutBinding }
            };

            Result result;

            //DescriptorSetLayout pSetLayout;
            unsafe
            {
                //pSetLayout = new DescriptorSetLayout();

                fixed(UInt64 *ptrpSetLayout = &this.m)
                {
                    result = Interop.NativeMethods.vkCreateDescriptorSetLayout(selectedDevice.Handle, descriptorSetLayoutCreateInfo != null ? descriptorSetLayoutCreateInfo.m : (Interop.DescriptorSetLayoutCreateInfo *) default(IntPtr), pAllocator != null ? pAllocator.m : null, ptrpSetLayout);
                }

                if (result != Result.Success)
                {
                    throw new ResultException(result);
                }
            }
        }
Пример #22
0
        public void CmdBindDescriptorSet()
        {
            const int bufferSize = 256;

            var layoutCreateInfo = new DescriptorSetLayoutCreateInfo(
                new DescriptorSetLayoutBinding(0, DescriptorType.StorageBuffer, 1));
            var descriptorPoolCreateInfo = new DescriptorPoolCreateInfo(
                1,
                new[] { new DescriptorPoolSize(DescriptorType.StorageBuffer, 1) });

            using (DescriptorSetLayout descriptorSetLayout = Device.CreateDescriptorSetLayout(layoutCreateInfo))
                using (DescriptorPool descriptorPool = Device.CreateDescriptorPool(descriptorPoolCreateInfo))
                    using (PipelineLayout pipelineLayout = Device.CreatePipelineLayout(new PipelineLayoutCreateInfo(new[] { descriptorSetLayout })))
                        using (Buffer buffer = Device.CreateBuffer(new BufferCreateInfo(bufferSize, BufferUsages.StorageBuffer)))
                            using (DeviceMemory memory = Device.AllocateMemory(new MemoryAllocateInfo(bufferSize, 0)))
                            {
                                // Required to satisfy the validation layer.
                                buffer.GetMemoryRequirements();

                                buffer.BindMemory(memory);

                                DescriptorSet descriptorSet =
                                    descriptorPool.AllocateSets(new DescriptorSetAllocateInfo(1, descriptorSetLayout))[0];

                                var descriptorWrite = new WriteDescriptorSet(descriptorSet, 0, 0, 1, DescriptorType.StorageBuffer,
                                                                             bufferInfo: new[] { new DescriptorBufferInfo(buffer) });

                                descriptorPool.UpdateSets(new[] { descriptorWrite });

                                CommandBuffer.Begin();
                                CommandBuffer.CmdBindDescriptorSet(PipelineBindPoint.Graphics, pipelineLayout, descriptorSet);
                                CommandBuffer.CmdBindDescriptorSets(PipelineBindPoint.Graphics, pipelineLayout, 0, new[] { descriptorSet });
                                CommandBuffer.End();

                                descriptorPool.Reset();
                            }
        }
Пример #23
0
        private unsafe void CreatePipelineLayout()
        {
            var binding = new DescriptorSetLayoutBinding
            {
                Binding = 0,
                DescriptorCount = 1,
                DescriptorType = DescriptorType.UniformBuffer,
                StageFlags = ShaderStageFlags.Vertex
            };

            var descriptorSetLayoutCreateInfo = new DescriptorSetLayoutCreateInfo
            {
                StructureType = StructureType.DescriptorSetLayoutCreateInfo,
                BindingCount = 1,
                Bindings = new IntPtr(&binding)
            };
            descriptorSetLayout = device.CreateDescriptorSetLayout(ref descriptorSetLayoutCreateInfo);

            var localDescriptorSetLayout = descriptorSetLayout;
            var createInfo = new PipelineLayoutCreateInfo
            {
                StructureType = StructureType.PipelineLayoutCreateInfo,
                SetLayoutCount = 1,
                SetLayouts = new IntPtr(&localDescriptorSetLayout)
            };
            pipelineLayout = device.CreatePipelineLayout(ref createInfo);

            var poolSize = new DescriptorPoolSize { DescriptorCount = 2, Type = DescriptorType.UniformBuffer };
            var descriptorPoolCreateinfo = new DescriptorPoolCreateInfo
            {
                StructureType = StructureType.DescriptorPoolCreateInfo,
                PoolSizeCount = 1,
                PoolSizes = new IntPtr(&poolSize),
                MaxSets = 2
            };
            descriptorPool = device.CreateDescriptorPool(ref descriptorPoolCreateinfo);

            var bufferCreateInfo = new BufferCreateInfo
            {
                StructureType = StructureType.BufferCreateInfo,
                Usage = BufferUsageFlags.UniformBuffer,
                Size = 64,
            };
            uniformBuffer = device.CreateBuffer(ref bufferCreateInfo);

            MemoryRequirements memoryRequirements;
            device.GetBufferMemoryRequirements(uniformBuffer, out memoryRequirements);
            var memory = AllocateMemory(MemoryPropertyFlags.HostVisible | MemoryPropertyFlags.HostCoherent, memoryRequirements);

            device.BindBufferMemory(uniformBuffer, memory, 0);

            var mappedMemory = device.MapMemory(memory, 0, 64, MemoryMapFlags.None);
            var data = new[]
            {
                1.0f, 0.0f, 0.0f, 0.0f,
                0.0f, 1.0f, 0.0f, 0.0f,
                0.0f, 0.0f, 1.0f, 0.0f,
                0.0f, 0.0f, 0.0f, 1.0f,
            };
            Utilities.Write(mappedMemory, data, 0, data.Length);
            device.UnmapMemory(memory);
        }
Пример #24
0
 public unsafe DescriptorSetLayout CreateDescriptorSetLayout(ref DescriptorSetLayoutCreateInfo createInfo, AllocationCallbacks* allocator = null)
 {
     DescriptorSetLayout setLayout;
     fixed (DescriptorSetLayoutCreateInfo* __createInfo__ = &createInfo)
     {
         vkCreateDescriptorSetLayout(this, __createInfo__, allocator, &setLayout).CheckError();
     }
     return setLayout;
 }
Пример #25
0
        public static unsafe DescriptorSetLayout[] CreateMinimal(VulkanRenderer gd, Device device, ShaderSource[] shaders, out PipelineLayout layout)
        {
            int stagesCount = shaders.Length;

            int uCount = 0;
            int tCount = 0;
            int iCount = 0;

            foreach (var shader in shaders)
            {
                uCount += shader.Bindings.UniformBufferBindings.Count;
                tCount += shader.Bindings.TextureBindings.Count;
                iCount += shader.Bindings.ImageBindings.Count;
            }

            DescriptorSetLayoutBinding *uLayoutBindings = stackalloc DescriptorSetLayoutBinding[uCount];
            DescriptorSetLayoutBinding *sLayoutBindings = stackalloc DescriptorSetLayoutBinding[stagesCount];
            DescriptorSetLayoutBinding *tLayoutBindings = stackalloc DescriptorSetLayoutBinding[tCount];
            DescriptorSetLayoutBinding *iLayoutBindings = stackalloc DescriptorSetLayoutBinding[iCount];

            int uIndex = 0;
            int sIndex = 0;
            int tIndex = 0;
            int iIndex = 0;

            foreach (var shader in shaders)
            {
                var stageFlags = shader.Stage.Convert();

                void Set(DescriptorSetLayoutBinding *bindings, DescriptorType type, ref int start, IEnumerable <int> bds)
                {
                    foreach (var b in bds)
                    {
                        bindings[start++] = new DescriptorSetLayoutBinding
                        {
                            Binding         = (uint)b,
                            DescriptorType  = type,
                            DescriptorCount = 1,
                            StageFlags      = stageFlags
                        };
                    }
                }

                void SetStorage(DescriptorSetLayoutBinding *bindings, ref int start, int count)
                {
                    bindings[start++] = new DescriptorSetLayoutBinding
                    {
                        Binding         = (uint)start,
                        DescriptorType  = DescriptorType.StorageBuffer,
                        DescriptorCount = (uint)count,
                        StageFlags      = stageFlags
                    };
                }

                // TODO: Support buffer textures and images here.
                // This is only used for the helper shaders on the backend, and we don't use buffer textures on them
                // so far, so it's not really necessary right now.
                Set(uLayoutBindings, DescriptorType.UniformBuffer, ref uIndex, shader.Bindings.UniformBufferBindings);
                SetStorage(sLayoutBindings, ref sIndex, shader.Bindings.StorageBufferBindings.Count);
                Set(tLayoutBindings, DescriptorType.CombinedImageSampler, ref tIndex, shader.Bindings.TextureBindings);
                Set(iLayoutBindings, DescriptorType.StorageImage, ref iIndex, shader.Bindings.ImageBindings);
            }

            DescriptorSetLayout[] layouts = new DescriptorSetLayout[PipelineFull.DescriptorSetLayouts];

            var uDescriptorSetLayoutCreateInfo = new DescriptorSetLayoutCreateInfo()
            {
                SType        = StructureType.DescriptorSetLayoutCreateInfo,
                PBindings    = uLayoutBindings,
                BindingCount = (uint)uCount
            };

            var sDescriptorSetLayoutCreateInfo = new DescriptorSetLayoutCreateInfo()
            {
                SType        = StructureType.DescriptorSetLayoutCreateInfo,
                PBindings    = sLayoutBindings,
                BindingCount = (uint)stagesCount
            };

            var tDescriptorSetLayoutCreateInfo = new DescriptorSetLayoutCreateInfo()
            {
                SType        = StructureType.DescriptorSetLayoutCreateInfo,
                PBindings    = tLayoutBindings,
                BindingCount = (uint)tCount
            };

            var iDescriptorSetLayoutCreateInfo = new DescriptorSetLayoutCreateInfo()
            {
                SType        = StructureType.DescriptorSetLayoutCreateInfo,
                PBindings    = iLayoutBindings,
                BindingCount = (uint)iCount
            };

            gd.Api.CreateDescriptorSetLayout(device, uDescriptorSetLayoutCreateInfo, null, out layouts[PipelineFull.UniformSetIndex]).ThrowOnError();
            gd.Api.CreateDescriptorSetLayout(device, sDescriptorSetLayoutCreateInfo, null, out layouts[PipelineFull.StorageSetIndex]).ThrowOnError();
            gd.Api.CreateDescriptorSetLayout(device, tDescriptorSetLayoutCreateInfo, null, out layouts[PipelineFull.TextureSetIndex]).ThrowOnError();
            gd.Api.CreateDescriptorSetLayout(device, iDescriptorSetLayoutCreateInfo, null, out layouts[PipelineFull.ImageSetIndex]).ThrowOnError();

            fixed(DescriptorSetLayout *pLayouts = layouts)
            {
                var pipelineLayoutCreateInfo = new PipelineLayoutCreateInfo()
                {
                    SType          = StructureType.PipelineLayoutCreateInfo,
                    PSetLayouts    = pLayouts,
                    SetLayoutCount = PipelineFull.DescriptorSetLayouts
                };

                gd.Api.CreatePipelineLayout(device, &pipelineLayoutCreateInfo, null, out layout).ThrowOnError();
            }

            return(layouts);
        }
        internal static unsafe SharpVulkan.DescriptorSetLayout CreateNativeDescriptorSetLayout(GraphicsDevice device, IList<DescriptorSetLayoutBuilder.Entry> entries, out uint[] typeCounts)
        {
            var bindings = new DescriptorSetLayoutBinding[entries.Count];
            var immutableSamplers = new Sampler[entries.Count];

            int usedBindingCount = 0;

            typeCounts = new uint[DescriptorTypeCount];

            fixed (Sampler* immutableSamplersPointer = &immutableSamplers[0])
            {
                for (int i = 0; i < entries.Count; i++)
                {
                    var entry = entries[i];

                    // TODO VULKAN: Special case for unused bindings in PipelineState. Handle more nicely.
                    if (entry.ArraySize == 0)
                        continue;

                    bindings[usedBindingCount] = new DescriptorSetLayoutBinding
                    {
                        DescriptorType = VulkanConvertExtensions.ConvertDescriptorType(entry.Class, entry.Type),
                        StageFlags = ShaderStageFlags.All, // TODO VULKAN: Filter?
                        Binding = (uint)i,
                        DescriptorCount = (uint)entry.ArraySize
                    };

                    if (entry.ImmutableSampler != null)
                    {
                        // TODO VULKAN: Handle immutable samplers for DescriptorCount > 1
                        if (entry.ArraySize > 1)
                        {
                            throw new NotImplementedException();
                        }

                        // Remember this, so we can choose the right DescriptorType in DescriptorSet.SetShaderResourceView
                        immutableSamplers[i] = entry.ImmutableSampler.NativeSampler;
                        //bindings[i].DescriptorType = DescriptorType.CombinedImageSampler;
                        bindings[usedBindingCount].ImmutableSamplers = new IntPtr(immutableSamplersPointer + i);
                    }

                    typeCounts[(int)bindings[usedBindingCount].DescriptorType] += bindings[usedBindingCount].DescriptorCount;

                    usedBindingCount++;
                }

                var createInfo = new DescriptorSetLayoutCreateInfo
                {
                    StructureType = StructureType.DescriptorSetLayoutCreateInfo,
                    BindingCount = (uint)usedBindingCount,
                    Bindings = usedBindingCount > 0 ? new IntPtr(Interop.Fixed(bindings)) : IntPtr.Zero
                };
                return device.NativeDevice.CreateDescriptorSetLayout(ref createInfo);
            }
        }
Пример #27
0
 public abstract void GetDescriptorSetLayoutSupport([Count(Count = 0)] Device device, [Count(Count = 0), Flow(FlowDirection.In)] ref DescriptorSetLayoutCreateInfo pCreateInfo, [Count(Count = 0), Flow(FlowDirection.Out)] out DescriptorSetLayoutSupport pSupport);
Пример #28
0
 internal static unsafe extern Result vkCreateDescriptorSetLayout(Device device, DescriptorSetLayoutCreateInfo* createInfo, AllocationCallbacks* allocator, DescriptorSetLayout* setLayout);
Пример #29
0
        static public MPointArray ComputeData(MPointArray inPos, float angle, float envolope)
        {
            var memorySize = inPos.Count * 4 * sizeof(float);

            float[] ubo = { inPos.Count, angle, envolope };

            // pos to float[]
            float[] allPos = new float[inPos.Count * 4];
            var     i      = 0;

            foreach (var point in inPos)
            {
                allPos[i * 4 + 0] = (float)point.x;
                allPos[i * 4 + 1] = (float)point.y;
                allPos[i * 4 + 2] = (float)point.z;
                allPos[i * 4 + 3] = (float)point.w;
                i++;
            }

            var instance = new Instance(new InstanceCreateInfo {
                ApplicationInfo = new ApplicationInfo {
                    ApplicationName    = "CSharp Vulkan",
                    ApplicationVersion = Vulkan.Version.Make(1, 0, 0),
                    ApiVersion         = Vulkan.Version.Make(1, 0, 0),
                    EngineName         = "CSharp Engine",
                    EngineVersion      = Vulkan.Version.Make(1, 0, 0)
                },
                //EnabledExtensionCount = 0,
                //EnabledLayerCount = 0
                EnabledExtensionNames = new string[] { "VK_EXT_debug_report" },
                EnabledLayerNames     = new string[] { "VK_LAYER_LUNARG_standard_validation" }
            });

            var debugCallback = new Instance.DebugReportCallback(DebugReportCallback);

            instance.EnableDebug(debugCallback, DebugReportFlagsExt.Warning | DebugReportFlagsExt.Error);

            PhysicalDevice physicalDevice = instance.EnumeratePhysicalDevices()[0];

            var queueFamilyIndex = FindBestComputeQueue(physicalDevice);

            DeviceQueueCreateInfo[] deviceQueueCreateInfo =
            {
                new DeviceQueueCreateInfo
                {
                    QueueFamilyIndex = queueFamilyIndex,
                    QueueCount       = 1,
                    QueuePriorities  = new float[] { 1.0f }
                }
            };

            var deviceCreateInfo = new DeviceCreateInfo
            {
                QueueCreateInfos      = deviceQueueCreateInfo,
                EnabledFeatures       = new PhysicalDeviceFeatures(),
                EnabledExtensionCount = 0,
                //EnabledLayerCount = 0
                //EnabledExtensionNames = new string[] { "VK_EXT_debug_report" },
                EnabledLayerNames = new string[] { "VK_LAYER_LUNARG_standard_validation" }
            };

            var device = physicalDevice.CreateDevice(deviceCreateInfo);

            //var memProperties = physicalDevice.GetMemoryProperties();

            var bufferCreateInfo = new BufferCreateInfo
            {
                Size               = memorySize,
                Usage              = BufferUsageFlags.StorageBuffer,
                SharingMode        = SharingMode.Exclusive,
                QueueFamilyIndices = new uint[] { queueFamilyIndex }
            };

            var inBuffer  = device.CreateBuffer(bufferCreateInfo);
            var outBuffer = device.CreateBuffer(bufferCreateInfo);

            var memRequirements = device.GetBufferMemoryRequirements(inBuffer);

            uint memIndex = FindMemoryType(physicalDevice, memRequirements.MemoryTypeBits, MemoryPropertyFlags.HostVisible | MemoryPropertyFlags.HostCoherent);

            var memoryAllocatInfo = new MemoryAllocateInfo
            {
                AllocationSize  = memRequirements.Size,
                MemoryTypeIndex = memIndex
            };

            var memory = device.AllocateMemory(memoryAllocatInfo);

            var dataPtr = device.MapMemory(memory, 0, memorySize);

            Marshal.Copy(allPos, 0, dataPtr, allPos.Length);

            device.UnmapMemory(memory);

            memRequirements = device.GetBufferMemoryRequirements(outBuffer);
            memoryAllocatInfo.MemoryTypeIndex = FindMemoryType(physicalDevice, memRequirements.MemoryTypeBits, MemoryPropertyFlags.HostVisible | MemoryPropertyFlags.HostCoherent);
            var outMemory = device.AllocateMemory(memoryAllocatInfo);

            device.BindBufferMemory(inBuffer, memory, 0);
            device.BindBufferMemory(outBuffer, outMemory, 0);

            //var uboSize = Marshal.SizeOf(ubo);
            var uboSize             = 3 * sizeof(float);
            var uboBufferCreateInfo = new BufferCreateInfo
            {
                Size               = uboSize,
                Usage              = BufferUsageFlags.UniformBuffer,
                SharingMode        = SharingMode.Exclusive,
                QueueFamilyIndices = new uint[] { queueFamilyIndex }
            };

            var uboBuffer = device.CreateBuffer(uboBufferCreateInfo);

            memRequirements = device.GetBufferMemoryRequirements(uboBuffer);

            var uboMemoryAllocInfo = new MemoryAllocateInfo
            {
                AllocationSize  = memRequirements.Size,
                MemoryTypeIndex = FindMemoryType(physicalDevice, memRequirements.MemoryTypeBits, MemoryPropertyFlags.HostVisible | MemoryPropertyFlags.HostCoherent)
            };

            var uboMemory = device.AllocateMemory(uboMemoryAllocInfo);
            var uboPtr    = device.MapMemory(uboMemory, 0, uboSize);

            Marshal.Copy(ubo, 0, uboPtr, ubo.Length);
            device.UnmapMemory(uboMemory);

            device.BindBufferMemory(uboBuffer, uboMemory, 0);

            //string textureName = string.Format("{0}.comp.spv", typeof(VulankCompute).Namespace);
            //Stream stream = typeof(VulankCompute).GetTypeInfo().Assembly.GetManifestResourceStream(textureName);
            string shaderFile = @"E:\coding\CShape\yTwistCSharpVulkan\comp.spv";
            Stream stream     = File.Open(shaderFile, FileMode.Open);

            byte[] shaderCode = new byte[stream.Length];
            stream.Read(shaderCode, 0, (int)stream.Length);
            stream.Close();
            stream.Dispose();
            var shaderModule = device.CreateShaderModule(shaderCode);

            DescriptorSetLayoutBinding[] descriptorSetLayoutBindings =
            {
                new DescriptorSetLayoutBinding
                {
                    Binding         = 0,
                    DescriptorType  = DescriptorType.StorageBuffer,
                    DescriptorCount = 1,
                    StageFlags      = ShaderStageFlags.Compute
                },

                new DescriptorSetLayoutBinding
                {
                    Binding         = 1,
                    DescriptorType  = DescriptorType.StorageBuffer,
                    DescriptorCount = 1,
                    StageFlags      = ShaderStageFlags.Compute
                },

                new DescriptorSetLayoutBinding
                {
                    Binding         = 2,
                    DescriptorType  = DescriptorType.UniformBuffer,
                    DescriptorCount = 1,
                    StageFlags      = ShaderStageFlags.Compute
                }
            };

            var descriporSetLayoutCreateinfo = new DescriptorSetLayoutCreateInfo
            {
                Bindings = descriptorSetLayoutBindings
            };

            var descriptorSetLayout = device.CreateDescriptorSetLayout(descriporSetLayoutCreateinfo);

            var descriptorPool = device.CreateDescriptorPool(new DescriptorPoolCreateInfo
            {
                MaxSets   = 1,
                PoolSizes = new DescriptorPoolSize[]
                {
                    new DescriptorPoolSize
                    {
                        DescriptorCount = 3
                    }
                }
            });

            var descriptorSet = device.AllocateDescriptorSets(new DescriptorSetAllocateInfo
            {
                DescriptorPool = descriptorPool,
                SetLayouts     = new DescriptorSetLayout[] { descriptorSetLayout }
            })[0];

            var inBufferInfo = new DescriptorBufferInfo
            {
                Buffer = inBuffer,
                Offset = 0,
                Range  = memorySize
            };

            var outBufferInfo = new DescriptorBufferInfo
            {
                Buffer = outBuffer,
                Offset = 0,
                Range  = memorySize
            };

            var uboBufferInfo = new DescriptorBufferInfo
            {
                Buffer = uboBuffer,
                Offset = 0,
                Range  = uboSize
            };

            WriteDescriptorSet[] writeDescriptorSets =
            {
                new WriteDescriptorSet
                {
                    DstSet          = descriptorSet,
                    DstBinding      = 0,
                    DstArrayElement = 0,
                    DescriptorCount = 1,
                    DescriptorType  = DescriptorType.StorageBuffer,
                    BufferInfo      = new DescriptorBufferInfo[] { inBufferInfo }
                },

                new WriteDescriptorSet
                {
                    DstSet          = descriptorSet,
                    DstBinding      = 1,
                    DstArrayElement = 0,
                    DescriptorCount = 1,
                    DescriptorType  = DescriptorType.StorageBuffer,
                    BufferInfo      = new DescriptorBufferInfo[] { outBufferInfo }
                },

                new WriteDescriptorSet
                {
                    DstSet          = descriptorSet,
                    DstBinding      = 2,
                    DstArrayElement = 0,
                    DescriptorCount = 1,
                    DescriptorType  = DescriptorType.UniformBuffer,
                    BufferInfo      = new DescriptorBufferInfo[] { uboBufferInfo }
                }
            };

            device.UpdateDescriptorSets(writeDescriptorSets, null);

            var pipelineLayout = device.CreatePipelineLayout(new PipelineLayoutCreateInfo
            {
                SetLayouts = new DescriptorSetLayout[] { descriptorSetLayout }
            });

            var shaderStage = new PipelineShaderStageCreateInfo
            {
                Stage  = ShaderStageFlags.Compute,
                Module = shaderModule,
                Name   = "main"
            };

            var pipeline = device.CreateComputePipelines(null, new ComputePipelineCreateInfo[]
            {
                new ComputePipelineCreateInfo
                {
                    Stage  = shaderStage,
                    Layout = pipelineLayout
                }
            })[0];

            var commandPool = device.CreateCommandPool(new CommandPoolCreateInfo
            {
                QueueFamilyIndex = queueFamilyIndex
            });

            var cmdBuffer = device.AllocateCommandBuffers(new CommandBufferAllocateInfo
            {
                CommandPool        = commandPool,
                CommandBufferCount = 1,
                Level = CommandBufferLevel.Primary
            })[0];

            cmdBuffer.Begin(new CommandBufferBeginInfo
            {
                Flags = CommandBufferUsageFlags.OneTimeSubmit
            });

            cmdBuffer.CmdBindPipeline(PipelineBindPoint.Compute, pipeline);

            cmdBuffer.CmdBindDescriptorSet(PipelineBindPoint.Compute, pipelineLayout, 0, descriptorSet, null);

            cmdBuffer.CmdDispatch((uint)inPos.Count, 1, 1);

            cmdBuffer.End();

            var queue = device.GetQueue(queueFamilyIndex, 0);

            //var fence = device.CreateFence(new FenceCreateInfo { Flags=0 });
            queue.Submit(new SubmitInfo
            {
                WaitSemaphoreCount = 0,
                CommandBuffers     = new CommandBuffer[] { cmdBuffer },
            });
            queue.WaitIdle();
            //device.WaitForFence(fence, true, UInt64.MaxValue);

            var newDataPtr = device.MapMemory(outMemory, 0, memorySize);

            Marshal.Copy(newDataPtr, allPos, 0, allPos.Length);
            device.UnmapMemory(outMemory);

            var j = 0;

            foreach (var p in inPos)
            {
                p.x = allPos[j * 4 + 0];
                p.y = allPos[j * 4 + 1];
                p.z = allPos[j * 4 + 2];
                p.w = allPos[j * 4 + 3];
                j++;
            }

            //device.DestroyFence(fence);
            device.DestroyShaderModule(shaderModule);
            device.DestroyCommandPool(commandPool);
            device.DestroyDescriptorSetLayout(descriptorSetLayout);
            device.DestroyDescriptorPool(descriptorPool);
            device.DestroyPipelineLayout(pipelineLayout);
            device.DestroyPipeline(pipeline);
            device.DestroyBuffer(inBuffer);
            device.DestroyBuffer(outBuffer);
            device.DestroyBuffer(uboBuffer);
            device.FreeMemory(memory);
            device.FreeMemory(outMemory);
            device.FreeMemory(uboMemory);
            device.Destroy();

            //instance.Destroy();

            return(inPos);
        }
Пример #30
0
        public static unsafe DescriptorSetLayout[] Create(VulkanGraphicsDevice gd, Device device, uint stages, out PipelineLayout layout)
        {
            int stagesCount = BitOperations.PopCount(stages);

            int uCount  = Constants.MaxUniformBuffersPerStage * stagesCount + 1;
            int tCount  = Constants.MaxTexturesPerStage * stagesCount;
            int iCount  = Constants.MaxImagesPerStage * stagesCount;
            int bTCount = tCount;
            int bICount = iCount;

            DescriptorSetLayoutBinding *uLayoutBindings  = stackalloc DescriptorSetLayoutBinding[uCount];
            DescriptorSetLayoutBinding *sLayoutBindings  = stackalloc DescriptorSetLayoutBinding[stagesCount];
            DescriptorSetLayoutBinding *tLayoutBindings  = stackalloc DescriptorSetLayoutBinding[tCount];
            DescriptorSetLayoutBinding *iLayoutBindings  = stackalloc DescriptorSetLayoutBinding[iCount];
            DescriptorSetLayoutBinding *bTLayoutBindings = stackalloc DescriptorSetLayoutBinding[bTCount];
            DescriptorSetLayoutBinding *bILayoutBindings = stackalloc DescriptorSetLayoutBinding[bICount];

            uLayoutBindings[0] = new DescriptorSetLayoutBinding
            {
                Binding         = 0,
                DescriptorType  = DescriptorType.UniformBuffer,
                DescriptorCount = 1,
                StageFlags      = ShaderStageFlags.ShaderStageFragmentBit | ShaderStageFlags.ShaderStageComputeBit
            };

            int iter = 0;

            while (stages != 0)
            {
                int stage = BitOperations.TrailingZeroCount(stages);
                stages &= ~(1u << stage);

                var stageFlags = stage switch
                {
                    1 => ShaderStageFlags.ShaderStageFragmentBit,
                    2 => ShaderStageFlags.ShaderStageGeometryBit,
                    3 => ShaderStageFlags.ShaderStageTessellationControlBit,
                    4 => ShaderStageFlags.ShaderStageTessellationEvaluationBit,
                    _ => ShaderStageFlags.ShaderStageVertexBit | ShaderStageFlags.ShaderStageComputeBit
                };

                void Set(DescriptorSetLayoutBinding *bindings, int maxPerStage, DescriptorType type, int start = 0)
                {
                    for (int i = 0; i < maxPerStage; i++)
                    {
                        bindings[start + iter * maxPerStage + i] = new DescriptorSetLayoutBinding
                        {
                            Binding         = (uint)(start + stage * maxPerStage + i),
                            DescriptorType  = type,
                            DescriptorCount = 1,
                            StageFlags      = stageFlags
                        };
                    }
                }

                void SetStorage(DescriptorSetLayoutBinding *bindings, int maxPerStage, int start = 0)
                {
                    bindings[start + iter] = new DescriptorSetLayoutBinding
                    {
                        Binding         = (uint)(start + stage * maxPerStage),
                        DescriptorType  = DescriptorType.StorageBuffer,
                        DescriptorCount = (uint)maxPerStage,
                        StageFlags      = stageFlags
                    };
                }

                Set(uLayoutBindings, Constants.MaxUniformBuffersPerStage, DescriptorType.UniformBuffer, 1);
                SetStorage(sLayoutBindings, Constants.MaxStorageBuffersPerStage);
                Set(tLayoutBindings, Constants.MaxTexturesPerStage, DescriptorType.CombinedImageSampler);
                Set(iLayoutBindings, Constants.MaxImagesPerStage, DescriptorType.StorageImage);
                Set(bTLayoutBindings, Constants.MaxTexturesPerStage, DescriptorType.UniformTexelBuffer);
                Set(bILayoutBindings, Constants.MaxImagesPerStage, DescriptorType.StorageTexelBuffer);

                iter++;
            }

            DescriptorSetLayout[] layouts = new DescriptorSetLayout[PipelineFull.DescriptorSetLayouts];

            var uDescriptorSetLayoutCreateInfo = new DescriptorSetLayoutCreateInfo()
            {
                SType        = StructureType.DescriptorSetLayoutCreateInfo,
                PBindings    = uLayoutBindings,
                BindingCount = (uint)uCount
            };

            var sDescriptorSetLayoutCreateInfo = new DescriptorSetLayoutCreateInfo()
            {
                SType        = StructureType.DescriptorSetLayoutCreateInfo,
                PBindings    = sLayoutBindings,
                BindingCount = (uint)stagesCount
            };

            var tDescriptorSetLayoutCreateInfo = new DescriptorSetLayoutCreateInfo()
            {
                SType        = StructureType.DescriptorSetLayoutCreateInfo,
                PBindings    = tLayoutBindings,
                BindingCount = (uint)tCount
            };

            var iDescriptorSetLayoutCreateInfo = new DescriptorSetLayoutCreateInfo()
            {
                SType        = StructureType.DescriptorSetLayoutCreateInfo,
                PBindings    = iLayoutBindings,
                BindingCount = (uint)iCount
            };

            var bTDescriptorSetLayoutCreateInfo = new DescriptorSetLayoutCreateInfo()
            {
                SType        = StructureType.DescriptorSetLayoutCreateInfo,
                PBindings    = bTLayoutBindings,
                BindingCount = (uint)bTCount
            };

            var bIDescriptorSetLayoutCreateInfo = new DescriptorSetLayoutCreateInfo()
            {
                SType        = StructureType.DescriptorSetLayoutCreateInfo,
                PBindings    = bILayoutBindings,
                BindingCount = (uint)bICount
            };

            gd.Api.CreateDescriptorSetLayout(device, uDescriptorSetLayoutCreateInfo, null, out layouts[PipelineFull.UniformSetIndex]).ThrowOnError();
            gd.Api.CreateDescriptorSetLayout(device, sDescriptorSetLayoutCreateInfo, null, out layouts[PipelineFull.StorageSetIndex]).ThrowOnError();
            gd.Api.CreateDescriptorSetLayout(device, tDescriptorSetLayoutCreateInfo, null, out layouts[PipelineFull.TextureSetIndex]).ThrowOnError();
            gd.Api.CreateDescriptorSetLayout(device, iDescriptorSetLayoutCreateInfo, null, out layouts[PipelineFull.ImageSetIndex]).ThrowOnError();
            gd.Api.CreateDescriptorSetLayout(device, bTDescriptorSetLayoutCreateInfo, null, out layouts[PipelineFull.BufferTextureSetIndex]).ThrowOnError();
            gd.Api.CreateDescriptorSetLayout(device, bIDescriptorSetLayoutCreateInfo, null, out layouts[PipelineFull.BufferImageSetIndex]).ThrowOnError();

            fixed(DescriptorSetLayout *pLayouts = layouts)
            {
                var pipelineLayoutCreateInfo = new PipelineLayoutCreateInfo()
                {
                    SType          = StructureType.PipelineLayoutCreateInfo,
                    PSetLayouts    = pLayouts,
                    SetLayoutCount = PipelineFull.DescriptorSetLayouts
                };

                gd.Api.CreatePipelineLayout(device, &pipelineLayoutCreateInfo, null, out layout).ThrowOnError();
            }

            return(layouts);
        }
    }
Пример #31
0
        private unsafe void CreatePipelineLayout()
        {
            var binding = new DescriptorSetLayoutBinding
            {
                Binding         = 0,
                DescriptorCount = 1,
                DescriptorType  = DescriptorType.UniformBuffer,
                StageFlags      = ShaderStageFlags.Vertex
            };

            var descriptorSetLayoutCreateInfo = new DescriptorSetLayoutCreateInfo
            {
                StructureType = StructureType.DescriptorSetLayoutCreateInfo,
                BindingCount  = 1,
                Bindings      = new IntPtr(&binding)
            };

            descriptorSetLayout = device.CreateDescriptorSetLayout(ref descriptorSetLayoutCreateInfo);

            var localDescriptorSetLayout = descriptorSetLayout;
            var createInfo = new PipelineLayoutCreateInfo
            {
                StructureType  = StructureType.PipelineLayoutCreateInfo,
                SetLayoutCount = 1,
                SetLayouts     = new IntPtr(&localDescriptorSetLayout)
            };

            pipelineLayout = device.CreatePipelineLayout(ref createInfo);

            var poolSize = new DescriptorPoolSize {
                DescriptorCount = 2, Type = DescriptorType.UniformBuffer
            };
            var descriptorPoolCreateinfo = new DescriptorPoolCreateInfo
            {
                StructureType = StructureType.DescriptorPoolCreateInfo,
                PoolSizeCount = 1,
                PoolSizes     = new IntPtr(&poolSize),
                MaxSets       = 2
            };

            descriptorPool = device.CreateDescriptorPool(ref descriptorPoolCreateinfo);

            var bufferCreateInfo = new BufferCreateInfo
            {
                StructureType = StructureType.BufferCreateInfo,
                Usage         = BufferUsageFlags.UniformBuffer,
                Size          = 64,
            };

            uniformBuffer = device.CreateBuffer(ref bufferCreateInfo);

            MemoryRequirements memoryRequirements;

            device.GetBufferMemoryRequirements(uniformBuffer, out memoryRequirements);
            var memory = AllocateMemory(MemoryPropertyFlags.HostVisible | MemoryPropertyFlags.HostCoherent, memoryRequirements);

            device.BindBufferMemory(uniformBuffer, memory, 0);

            var mappedMemory = device.MapMemory(memory, 0, 64, MemoryMapFlags.None);
            var data         = new[]
            {
                1.0f, 0.0f, 0.0f, 0.0f,
                0.0f, 1.0f, 0.0f, 0.0f,
                0.0f, 0.0f, 1.0f, 0.0f,
                0.0f, 0.0f, 0.0f, 1.0f,
            };

            Utilities.Write(mappedMemory, data, 0, data.Length);
            device.UnmapMemory(memory);
        }