示例#1
0
        private void createVertexBuffer()
        {
            VkBufferCreateInfo bufferInfo = new VkBufferCreateInfo();

            bufferInfo.sType       = VkStructureType.VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
            bufferInfo.size        = Marshal.SizeOf(vertices[0]) * vertices.Length;
            bufferInfo.usage       = VkBufferUsageFlagBits.VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
            bufferInfo.sharingMode = VkSharingMode.VK_SHARING_MODE_EXCLUSIVE;

            VkResult result = Vulkan.vkCreateBuffer(device, bufferInfo, null, out vertexBuffer);

            if (result != VkResult.VK_SUCCESS)
            {
                throw Program.Throw("failed to create vertex buffer!", result);
            }

            VkMemoryRequirements memRequirements;

            Vulkan.vkGetBufferMemoryRequirements(device, vertexBuffer, out memRequirements);

            VkMemoryAllocateInfo allocInfo = new VkMemoryAllocateInfo();

            allocInfo.sType           = VkStructureType.VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
            allocInfo.allocationSize  = memRequirements.size;
            allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, VkMemoryPropertyFlagBits.VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VkMemoryPropertyFlagBits.VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);

            result = Vulkan.vkAllocateMemory(device, allocInfo, null, out vertexBufferMemory);
            if (result != VkResult.VK_SUCCESS)
            {
                throw Program.Throw("failed to allocate vertex buffer memory!", result);
            }

            Vulkan.vkBindBufferMemory(device, vertexBuffer, vertexBufferMemory, 0);

            byte[] data;
            Vulkan.vkMapMemory(device, vertexBufferMemory, 0, bufferInfo.size, 0, out data);
            MemoryCopyHelper.Copy(vertices, data, 0, bufferInfo.size);
            Vulkan.vkUnmapMemory(device, vertexBufferMemory);
        }