Skip to content

Commit

Permalink
Implement Vertex buffer creation chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSmith33 committed Mar 26, 2022
1 parent 2b2ff8c commit 5cb8d23
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 16 deletions.
4 changes: 4 additions & 0 deletions plugins/core/src/core/utils.vx
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ else
if (len == 0) return;
RtlCopyMemory(cast(void*)dst.ptr, cast(void*)src.ptr, len);
}
void memcpy(void* dst, void* src, u64 length) {
if (length == 0) return;
RtlCopyMemory(dst, src, length);
}
}

struct Array[T]
Expand Down
5 changes: 5 additions & 0 deletions plugins/core/src/core/vector.vx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module core.vector;

struct vec2 { f32 x; f32 y; }
struct vec3 { f32 x; f32 y; f32 z; }
struct vec4 { f32 x; f32 y; f32 z; f32 w; }
19 changes: 5 additions & 14 deletions plugins/hello_triangle/res/shader.vert
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
#version 450

layout(location = 0) out vec3 fragColor;

vec2 positions[3] = vec2[](
vec2(0.0, -0.5),
vec2(0.5, 0.5),
vec2(-0.5, 0.5)
);
layout(location = 0) in vec2 inPosition;
layout(location = 1) in vec3 inColor;

vec3 colors[3] = vec3[](
vec3(1.0, 0.0, 0.0),
vec3(0.0, 1.0, 0.0),
vec3(0.0, 0.0, 1.0)
);
layout(location = 0) out vec3 fragColor;

void main() {
gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0);
fragColor = colors[gl_VertexIndex];
gl_Position = vec4(inPosition, 0.0, 1.0);
fragColor = inColor;
}
110 changes: 109 additions & 1 deletion plugins/hello_triangle/src/hello_triangle/main.vx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import core.mdbx;
import core.shaderc;
import core.tracy;
import core.utils;
import core.vector;
import core.vulkan.functions;
import core.vulkan.types;

Expand Down Expand Up @@ -96,6 +97,10 @@ struct Client
u8*[1] validationLayers;
u8*[1] deviceExtensions;

Vertex[3] vertices;
VkBuffer vertexBuffer;
VkDeviceMemory vertexBufferMemory;

bool isRunning = true;


Expand All @@ -106,6 +111,10 @@ struct Client
validationLayers[0] = "VK_LAYER_KHRONOS_validation";
deviceExtensions[0] = "VK_KHR_swapchain";

vertices[0] = Vertex(vec2( 0.0, -0.5), vec3(1, 0, 0));
vertices[1] = Vertex(vec2( 0.5, 0.5), vec3(0, 1, 0));
vertices[2] = Vertex(vec2(-0.5, 0.5), vec3(0, 0, 1));

initWindow();
initVulkan();
if (!isRunning) return;
Expand Down Expand Up @@ -217,6 +226,8 @@ struct Client
if (!isRunning) return;
createCommandPool();
if (!isRunning) return;
createVertexBuffer();
if (!isRunning) return;
createCommandBuffers();
if (!isRunning) return;
createSyncObjects();
Expand Down Expand Up @@ -268,6 +279,9 @@ struct Client

cleanupSwapChain();

vkDestroyBuffer(device, vertexBuffer, null);
vkFreeMemory(device, vertexBufferMemory, null);

for (u64 i = 0; i < MAX_FRAMES_IN_FLIGHT; ++i) {
vkDestroySemaphore(device, renderFinishedSemaphores[i], null);
vkDestroySemaphore(device, imageAvailableSemaphores[i], null);
Expand Down Expand Up @@ -908,6 +922,14 @@ struct Client
vertexInputInfo.vertexBindingDescriptionCount = 0;
vertexInputInfo.vertexAttributeDescriptionCount = 0;

VkVertexInputBindingDescription bindingDescription = Vertex.getBindingDescription();
VkVertexInputAttributeDescription[2] attributeDescriptions = Vertex.getAttributeDescriptions();

vertexInputInfo.vertexBindingDescriptionCount = 1;
vertexInputInfo.vertexAttributeDescriptionCount = cast(u32)attributeDescriptions.length;
vertexInputInfo.pVertexBindingDescriptions = &bindingDescription;
vertexInputInfo.pVertexAttributeDescriptions = attributeDescriptions.ptr;

VkPipelineInputAssemblyStateCreateInfo inputAssembly;
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
inputAssembly.primitiveRestartEnable = VK_FALSE;
Expand Down Expand Up @@ -1038,6 +1060,58 @@ struct Client
tracy_emit_zone_end(ctx);
}

void createVertexBuffer() {
@static TracyLoc zone = TracyLoc(null, "Client.createVertexBuffer", "main.vx", cast(u32)__LINE__, 0x000000); TracyZoneCtx ctx = tracy_emit_zone_begin(&zone, TRACY_ON);

VkBufferCreateInfo bufferInfo;
bufferInfo.size = vertices.sizeof;
bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;

if (vkCreateBuffer(device, &bufferInfo, null, &vertexBuffer) != VK_SUCCESS) {
println("failed to create vertex buffer!");
isRunning = false;
return;
}

VkMemoryRequirements memRequirements;
vkGetBufferMemoryRequirements(device, vertexBuffer, &memRequirements);

VkMemoryAllocateInfo allocInfo;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);

if (vkAllocateMemory(device, &allocInfo, null, &vertexBufferMemory) != VK_SUCCESS) {
println("failed to allocate vertex buffer memory!");
isRunning = false;
return;
}

vkBindBufferMemory(device, vertexBuffer, vertexBufferMemory, 0);

void* data;
vkMapMemory(device, vertexBufferMemory, 0, bufferInfo.size, 0, &data);
memcpy(data, vertices.ptr, vertices.sizeof);
vkUnmapMemory(device, vertexBufferMemory);

tracy_emit_zone_end(ctx);
}

u32 findMemoryType(u32 typeFilter, VkMemoryPropertyFlags properties) {
VkPhysicalDeviceMemoryProperties memProperties;
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);

for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
return i;
}
}

println("failed to find suitable memory type!");
isRunning = false;
return 0;
}

void createCommandBuffers() {
@static TracyLoc zone = TracyLoc(null, "Client.createCommandBuffers", "main.vx", cast(u32)__LINE__, 0x000000); TracyZoneCtx ctx = tracy_emit_zone_begin(&zone, TRACY_ON);

Expand Down Expand Up @@ -1086,7 +1160,10 @@ struct Client

vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline);

vkCmdDraw(commandBuffer, 3, 1, 0, 0);
VkDeviceSize offset = 0;
vkCmdBindVertexBuffers(commandBuffer, 0, 1, &vertexBuffer, &offset);

vkCmdDraw(commandBuffer, cast(u32)vertices.length, 1, 0, 0);

vkCmdEndRenderPass(commandBuffer);

Expand Down Expand Up @@ -1423,3 +1500,34 @@ bool checkExtensions(u8*[] requestedExtensions, VkExtensionProperties[] availabl
}
return true;
}

struct Vertex
{
vec2 pos;
vec3 color;

@static VkVertexInputBindingDescription getBindingDescription() {
VkVertexInputBindingDescription bindingDescription;
bindingDescription.binding = 0;
bindingDescription.stride = cast(u32)Vertex.sizeof;
bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;

return bindingDescription;
}

@static VkVertexInputAttributeDescription[2] getAttributeDescriptions() {
VkVertexInputAttributeDescription[2] attributeDescriptions;

attributeDescriptions[0].binding = 0;
attributeDescriptions[0].location = 0;
attributeDescriptions[0].format = VK_FORMAT_R32G32_SFLOAT;
attributeDescriptions[0].offset = cast(u32)Vertex.pos.offsetof;

attributeDescriptions[1].binding = 0;
attributeDescriptions[1].location = 1;
attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
attributeDescriptions[1].offset = cast(u32)Vertex.color.offsetof;

return attributeDescriptions;
}
}

0 comments on commit 5cb8d23

Please sign in to comment.