Exemplo n.º 1
0
        VertexData CreateVertexData()
        {
            var data = new VertexData();

            var quadVertices = new[, ]
            {
                { 1.0f, 1.0f, 0.0f, /* UV: */ 1.0f, 1.0f, /* Normal: */ 0.0f, 0.0f, 1.0f },
                { -1.0f, 1.0f, 0.0f, /* UV: */ 0.0f, 1.0f, /* Normal: */ 0.0f, 0.0f, 1.0f },
                { -1.0f, -1.0f, 0.0f, /* UV: */ 0.0f, 0.0f, /* Normal: */ 0.0f, 0.0f, 1.0f },
                { 1.0f, -1.0f, 0.0f, /* UV: */ 1.0f, 0.0f, /* Normal: */ 0.0f, 0.0f, 1.0f },
            };

            DeviceSize memorySize = (ulong)(sizeof(float) * quadVertices.Length);

            data.Buffer = CreateBuffer(memorySize, BufferUsageFlags.VertexBuffer);

            var memoryRequirements = device.GetBufferMemoryRequirements(data.Buffer);
            var memoryIndex        = FindMemoryIndex(MemoryPropertyFlags.HostVisible);
            var allocateInfo       = new MemoryAllocateInfo(memoryRequirements.Size, memoryIndex);

            data.DeviceMemory = BindBuffer(data.Buffer, allocateInfo);

            var vertexPtr = device.MapMemory(data.DeviceMemory, 0, memorySize);

            VulkanUtils.Copy2DArray(quadVertices, vertexPtr, memorySize, memorySize);
            device.UnmapMemory(data.DeviceMemory);

            data.Indicies = new[] { 0, 1, 2, 2, 3, 0 };

            memorySize       = (ulong)(sizeof(uint) * data.Indicies.Length);
            data.IndexBuffer = CreateBuffer(memorySize, BufferUsageFlags.IndexBuffer);

            memoryRequirements     = device.GetBufferMemoryRequirements(data.IndexBuffer);
            memoryIndex            = FindMemoryIndex(MemoryPropertyFlags.HostVisible);
            allocateInfo           = new MemoryAllocateInfo(memoryRequirements.Size, memoryIndex);
            data.IndexDeviceMemory = BindBuffer(data.IndexBuffer, allocateInfo);

            var bytes = data.Indicies.SelectMany(BitConverter.GetBytes).ToArray(); // oh man, dat Linq tho

            CopyArrayToBuffer(data.IndexDeviceMemory, memorySize, bytes);

            data.BindingDescriptions = new[]
            {
                new VertexInputBindingDescription(0, (uint)(sizeof(float) * quadVertices.GetLength(1)), VertexInputRate.Vertex)
            };

            data.AttributeDescriptions = new[]
            {
                new VertexInputAttributeDescription(0, 0, Format.R32g32b32Sfloat, 0),                 // Vertex: X, Y, Z
                new VertexInputAttributeDescription(1, 0, Format.R32g32Sfloat, sizeof(float) * 3),    // UV: U, V
                new VertexInputAttributeDescription(2, 0, Format.R32g32b32Sfloat, sizeof(float) * 5), // Normal: X, Y, Z
            };

            return(data);
        }
Exemplo n.º 2
0
        protected VertexData CreateVertexData()
        {
            var data = new VertexData();

            var triangleVertices = new[, ]
            {
                { 0.5f, 0.5f, 0.0f, /* Vertex Color: */ 1.0f, 0.0f, 0.0f },
                { -0.5f, 0.5f, 0.0f, /* Vertex Color: */ 0.0f, 1.0f, 0.0f },
                { 0.0f, -0.5f, 0.0f, /* Vertex Color: */ 0.0f, 0.0f, 1.0f },
            };

            DeviceSize memorySize = (ulong)(sizeof(float) * triangleVertices.Length);

            data.Buffer = CreateBuffer(memorySize, BufferUsageFlags.VertexBuffer);

            var memoryRequirements = device.GetBufferMemoryRequirements(data.Buffer);
            var memoryIndex        = FindMemoryIndex(MemoryPropertyFlags.HostVisible);
            var allocateInfo       = new MemoryAllocateInfo(memoryRequirements.Size, memoryIndex);

            data.DeviceMemory = BindBuffer(data.Buffer, allocateInfo);

            var mapped = device.MapMemory(data.DeviceMemory, 0, memorySize);

            VulkanUtils.Copy2DArray(triangleVertices, mapped, memorySize, memorySize);
            device.UnmapMemory(data.DeviceMemory);

            data.BindingDescriptions = new[]
            {
                new VertexInputBindingDescription(0, (uint)(sizeof(float) * triangleVertices.GetLength(1)), VertexInputRate.Vertex)
            };

            data.AttributeDescriptions = new[]
            {
                new VertexInputAttributeDescription(0, 0, Format.R32g32b32Sfloat, 0),
                new VertexInputAttributeDescription(1, 0, Format.R32g32b32Sfloat, sizeof(float) * 3)
            };

            return(data);
        }