Added image loading
Added some string functions and macros Added path listing on windows Added assets
This commit is contained in:
@@ -330,6 +330,7 @@ bool Vk_Setup(SDL_Window *window) {
|
||||
|
||||
VkResult err = vk.CreateCommandPool(vk.device, &pool, 0, &frame->pool);
|
||||
|
||||
// Base command buffer
|
||||
VkCommandBufferAllocateInfo alloc = { 0 };
|
||||
alloc.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
||||
alloc.commandBufferCount = 1;
|
||||
@@ -338,16 +339,26 @@ bool Vk_Setup(SDL_Window *window) {
|
||||
|
||||
vk.AllocateCommandBuffers(vk.device, &alloc, &frame->cmd);
|
||||
|
||||
VkSemaphoreCreateInfo semaphore = { 0 };
|
||||
semaphore.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
||||
// Scratch command buffers
|
||||
VkCommandBuffer scratch[VK_NUM_SCRATCH];
|
||||
alloc.commandBufferCount = VK_NUM_SCRATCH;
|
||||
|
||||
err = Min(vk.CreateSemaphore(vk.device, &semaphore, 0, &frame->acquire), err);
|
||||
err = Min(vk.CreateSemaphore(vk.device, &semaphore, 0, &frame->complete), err);
|
||||
vk.AllocateCommandBuffers(vk.device, &alloc, scratch);
|
||||
|
||||
VkFenceCreateInfo fence = { 0 };
|
||||
fence.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
|
||||
fence.flags = VK_FENCE_CREATE_SIGNALED_BIT;
|
||||
|
||||
for (U32 s = 0; s < VK_NUM_SCRATCH; ++s) {
|
||||
frame->scratch[s].handle = scratch[s];
|
||||
err = Min(vk.CreateFence(vk.device, &fence, 0, &frame->scratch[s].fence), err);
|
||||
}
|
||||
|
||||
VkSemaphoreCreateInfo semaphore = { 0 };
|
||||
semaphore.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
||||
|
||||
err = Min(vk.CreateSemaphore(vk.device, &semaphore, 0, &frame->acquire), err);
|
||||
err = Min(vk.CreateSemaphore(vk.device, &semaphore, 0, &frame->complete), err);
|
||||
err = Min(vk.CreateFence(vk.device, &fence, 0, &frame->fence), err);
|
||||
|
||||
vk.err = err;
|
||||
@@ -450,3 +461,137 @@ void Vk_FrameEnd() {
|
||||
|
||||
vk.n_frames += 1;
|
||||
}
|
||||
|
||||
Vk_CommandBuffer *Vk_CommandBufferPush() {
|
||||
Vk_Frame *frame = &vk.frames[vk.n_frames % vk.in_flight];
|
||||
|
||||
// If this scratch buffer is still in use wait for it to finish, this is _bad_ but we
|
||||
// shouldn't hit this
|
||||
U32 idx = frame->next_scratch & (VK_NUM_SCRATCH - 1);
|
||||
|
||||
Vk_CommandBuffer *result = &frame->scratch[idx];
|
||||
|
||||
vk.WaitForFences(vk.device, 1, &result->fence, VK_TRUE, U64_MAX);
|
||||
vk.ResetFences(vk.device, 1, &result->fence);
|
||||
|
||||
VkCommandBufferBeginInfo begin_info = { 0 };
|
||||
begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
||||
|
||||
vk.BeginCommandBuffer(result->handle, &begin_info);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void Vk_CommandBufferSubmit(Vk_CommandBuffer *cmds, B32 wait) {
|
||||
vk.EndCommandBuffer(cmds->handle);
|
||||
|
||||
VkSubmitInfo submit_info = { 0 };
|
||||
submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
||||
submit_info.commandBufferCount = 1;
|
||||
submit_info.pCommandBuffers = &cmds->handle;
|
||||
|
||||
vk.QueueSubmit(vk.queue.handle, 1, &submit_info, cmds->fence);
|
||||
|
||||
if (wait) {
|
||||
vk.DeviceWaitIdle(vk.device);
|
||||
}
|
||||
}
|
||||
|
||||
#define VK_HOST_VISIBLE_FLAGS (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
|
||||
|
||||
internal VkDeviceMemory Vk_Allocate(VkMemoryRequirements *mreq, VkMemoryPropertyFlags usage) {
|
||||
VkDeviceMemory result = VK_NULL_HANDLE;
|
||||
|
||||
VkPhysicalDeviceMemoryProperties2 _props = { 0 };
|
||||
_props.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2;
|
||||
|
||||
vk.GetPhysicalDeviceMemoryProperties2(vk.gpu, &_props);
|
||||
|
||||
// ?????
|
||||
VkPhysicalDeviceMemoryProperties *props = &_props.memoryProperties;
|
||||
|
||||
U32 type_index = U32_MAX;
|
||||
|
||||
for (U32 it = 0; it < props->memoryTypeCount; ++it) {
|
||||
VkMemoryType *type = &props->memoryTypes[it];
|
||||
if ((type->propertyFlags & usage) == usage) {
|
||||
type_index = it;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (type_index != -1) {
|
||||
VkMemoryAllocateInfo alloc_info = { 0 };
|
||||
alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
||||
alloc_info.allocationSize = mreq->size;
|
||||
alloc_info.memoryTypeIndex = type_index;
|
||||
|
||||
vk.AllocateMemory(vk.device, &alloc_info, 0, &result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Vk_Buffer Vk_BufferCreate(U64 size, B32 host_visible) {
|
||||
Vk_Buffer result = { 0 };
|
||||
|
||||
VkBufferCreateInfo create_info = { 0 };
|
||||
create_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||||
create_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
||||
create_info.size = size;
|
||||
|
||||
vk.CreateBuffer(vk.device, &create_info, 0, &result.handle);
|
||||
|
||||
VkMemoryRequirements req;
|
||||
vk.GetBufferMemoryRequirements(vk.device, result.handle, &req);
|
||||
|
||||
VkMemoryPropertyFlags usage = host_visible ? VK_HOST_VISIBLE_FLAGS : VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
|
||||
|
||||
result.size = req.size;
|
||||
result.memory = Vk_Allocate(&req, usage);
|
||||
|
||||
vk.BindBufferMemory(vk.device, result.handle, result.memory, 0);
|
||||
|
||||
if (host_visible) {
|
||||
vk.MapMemory(vk.device, result.memory, 0, result.size, 0, &result.data);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void Vk_ImageCreate(Vk_Image *image) {
|
||||
VkImageCreateInfo create_info = { 0 };
|
||||
create_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
|
||||
create_info.imageType = VK_IMAGE_TYPE_2D;
|
||||
create_info.format = image->format;
|
||||
create_info.extent.width = image->width;
|
||||
create_info.extent.height = image->height;
|
||||
create_info.extent.depth = 1;
|
||||
create_info.mipLevels = 1;
|
||||
create_info.arrayLayers = 1;
|
||||
create_info.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | image->usage;
|
||||
create_info.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||
create_info.tiling = VK_IMAGE_TILING_OPTIMAL;
|
||||
|
||||
vk.CreateImage(vk.device, &create_info, 0, &image->handle);
|
||||
|
||||
VkMemoryRequirements req;
|
||||
vk.GetImageMemoryRequirements(vk.device, image->handle, &req);
|
||||
|
||||
image->size = req.size;
|
||||
image->memory = Vk_Allocate(&req, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
||||
|
||||
vk.BindImageMemory(vk.device, image->handle, image->memory, 0);
|
||||
|
||||
VkImageViewCreateInfo view_info = { 0 };
|
||||
view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
||||
view_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
||||
view_info.format = image->format;
|
||||
view_info.image = image->handle;
|
||||
|
||||
view_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
view_info.subresourceRange.levelCount = 1;
|
||||
view_info.subresourceRange.layerCount = 1;
|
||||
|
||||
vk.CreateImageView(vk.device, &view_info, 0, &image->view);
|
||||
}
|
||||
|
||||
@@ -23,6 +23,13 @@
|
||||
#define function static
|
||||
|
||||
#define VK_MAX_FRAMES_IN_FLIGHT 8
|
||||
#define VK_NUM_SCRATCH 8
|
||||
|
||||
typedef struct Vk_CommandBuffer Vk_CommandBuffer;
|
||||
struct Vk_CommandBuffer {
|
||||
VkCommandBuffer handle;
|
||||
VkFence fence;
|
||||
};
|
||||
|
||||
typedef struct Vk_Frame Vk_Frame;
|
||||
struct Vk_Frame {
|
||||
@@ -32,9 +39,35 @@ struct Vk_Frame {
|
||||
VkSemaphore acquire, complete;
|
||||
VkFence fence;
|
||||
|
||||
U32 next_scratch;
|
||||
Vk_CommandBuffer scratch[VK_NUM_SCRATCH];
|
||||
|
||||
U32 image; // swapchain image index
|
||||
};
|
||||
|
||||
typedef struct Vk_Image Vk_Image;
|
||||
struct Vk_Image {
|
||||
VkDeviceMemory memory;
|
||||
VkDeviceSize size;
|
||||
|
||||
VkImage handle;
|
||||
VkImageView view;
|
||||
|
||||
VkFormat format;
|
||||
VkImageUsageFlags usage;
|
||||
U32 width, height;
|
||||
};
|
||||
|
||||
typedef struct Vk_Buffer Vk_Buffer;
|
||||
struct Vk_Buffer {
|
||||
VkDeviceMemory memory;
|
||||
VkDeviceSize size;
|
||||
|
||||
VkBuffer handle;
|
||||
|
||||
void *data; // if mapped host visible memory
|
||||
};
|
||||
|
||||
typedef struct Vk_Context Vk_Context;
|
||||
struct Vk_Context {
|
||||
void *lib;
|
||||
@@ -86,4 +119,9 @@ function bool Vk_Setup(SDL_Window *window);
|
||||
function Vk_Frame *Vk_FrameBegin(SDL_Window *window);
|
||||
function void Vk_FrameEnd();
|
||||
|
||||
function Vk_CommandBuffer *Vk_CommandBufferPush();
|
||||
function void Vk_CommandBufferSubmit(Vk_CommandBuffer *cmds, B32 wait);
|
||||
|
||||
function Vk_Buffer Vk_BufferCreate(U64 size, B32 host_visible);
|
||||
|
||||
#endif // LD_VULKAN_CORE_H_
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
VK_FUNC(GetDeviceProcAddr);
|
||||
VK_FUNC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
|
||||
VK_FUNC(GetPhysicalDeviceSurfaceFormatsKHR);
|
||||
VK_FUNC(GetPhysicalDeviceMemoryProperties2);
|
||||
|
||||
#if defined(VK_USE_PLATFORM_WIN32_KHR)
|
||||
VK_FUNC(CreateWin32SurfaceKHR);
|
||||
@@ -39,7 +40,16 @@
|
||||
VK_FUNC(BeginCommandBuffer);
|
||||
VK_FUNC(EndCommandBuffer);
|
||||
VK_FUNC(DeviceWaitIdle);
|
||||
VK_FUNC(AllocateMemory);
|
||||
VK_FUNC(CreateBuffer);
|
||||
VK_FUNC(GetBufferMemoryRequirements);
|
||||
VK_FUNC(BindBufferMemory);
|
||||
VK_FUNC(CreateImage);
|
||||
VK_FUNC(GetImageMemoryRequirements);
|
||||
VK_FUNC(BindImageMemory);
|
||||
VK_FUNC(MapMemory);
|
||||
|
||||
VK_FUNC(CmdCopyBufferToImage);
|
||||
VK_FUNC(CmdPipelineBarrier2);
|
||||
VK_FUNC(CmdBeginRendering);
|
||||
VK_FUNC(CmdEndRendering);
|
||||
|
||||
Reference in New Issue
Block a user