From b1a805cea804ed98d7fe8e8167fc7833b893cd9d Mon Sep 17 00:00:00 2001 From: James Bulman Date: Sat, 4 Oct 2025 17:24:30 +0100 Subject: [PATCH] Added image loading Added some string functions and macros Added path listing on windows Added assets --- assets/hunter.png | Bin 0 -> 730 bytes code/core/arena.h | 2 +- code/core/core.c | 1 + code/core/core.h | 1 + code/core/impl/string.c | 150 +++++++++++++++++++++++++++++ code/core/macros.h | 2 + code/core/string.h | 36 +++++++ code/first.c | 10 ++ code/game/core.c | 119 +++++++++++++++++++++++ code/game/core.h | 16 ++++ code/os/filesystem.h | 29 ++++++ code/os/impl/windows/core.c | 13 +++ code/os/impl/windows/filesystem.c | 35 +++++++ code/vulkan/core.c | 153 +++++++++++++++++++++++++++++- code/vulkan/core.h | 38 ++++++++ code/vulkan/functions.h | 10 ++ windows.bat | 7 ++ 17 files changed, 617 insertions(+), 5 deletions(-) create mode 100644 assets/hunter.png create mode 100644 code/core/impl/string.c create mode 100644 code/core/string.h create mode 100644 code/game/core.c create mode 100644 code/game/core.h diff --git a/assets/hunter.png b/assets/hunter.png new file mode 100644 index 0000000000000000000000000000000000000000..02e60e56e59a03e17d762965f0b005f412f27c98 GIT binary patch literal 730 zcmV<00ww*4P)k00001b5ch_0Itp) z=>Px%lu1NER7i=XRL^S@K@|Qrgx=x?VjwwH2%^|aA^uo;s0X3#%`A$=KR_r%1SM?n zAXakP5aOkU{sDq0m1)64TJX>u+_f4Afrv#`^bis@UZS@i91rP?JF}B*z;}A{?R($* zxle;aEX$%QyjrblDm>)uI1ZJ?aU7~5jz>j2ExT&9s%e&GQPVVG7zQ_bxwfWszPq)> zb=P%42pMO<07eotREg`lVhJ*9l$fR|yyGXGrKV|u5Q6r>ZX^LCA%u*3z(C8gX!~Fn z2|bB=rHn^UZ0=C6lmP(u9y|sB%wNCBM=H$q*3vb)@%$OSv_1j=HeS3%y;8=vqb>m8 zV*U#5KYWTe`yaTUK=9-JD>|d={GGY93;-B>-s9NuXoT5?)$l@DmPIM0wBK#eezy@? zq#!WW5Y0%x-79^blV?A*$~i^NDacUo9nTvV3RY&KCW= result.count) { + M_ArenaPop(arena, offset); + + result.count = needed; + result.data = M_ArenaPush(arena, U8, .count = result.count + 1); + + Str8_ProcessFormat(result, format, copy); + } + else { + U64 size = result.count - needed - 1; + M_ArenaPopSize(arena, size); + + result.count = needed; + } + + return result; +} + +B32 Str8_Equal(Str8 a, Str8 b, Str8_EqualFlags flags) { + B32 result = (a.count == b.count); + + if (result) { + B32 ignore_case = (flags & STR8_EQUAL_IGNORE_CASE) != 0; + + for (S64 it = 0; it < a.count; ++it) { + U8 ac = ignore_case ? Chr_ToLowercase(a.data[it]) : a.data[it]; + U8 bc = ignore_case ? Chr_ToLowercase(b.data[it]) : b.data[it]; + + if (ac != bc) { + result = false; + break; + } + } + } + + return result; +} + +Str8 Str8_Prefix(Str8 s, S64 count) { + Str8 result; + result.data = s.data; + result.count = Min(s.count, count); + + return result; +} + +Str8 Str8_Suffix(Str8 s, S64 count) { + Str8 result; + result.count = Min(s.count, count); + result.data = s.data + (s.count - result.count); + + return result; +} + +Str8 Str8_RemoveAfterLast(Str8 s, U8 c) { + Str8 result; + result.data = s.data; + result.count = s.count; + + for (S64 it = s.count - 1; it >= 0; --it) { + if (result.data[it] == c) { + result.count = it; + break; + } + } + + return result; +} + +B32 Str8_EndsWith(Str8 s, Str8 suffix) { + B32 result = Str8_Equal(Str8_Suffix(s, suffix.count), suffix, 0); + return result; +} + +U8 Chr_ToUppercase(U8 c) { + U8 result = (c >= 'a' && c <= 'z') ? (c - ('a' - 'A')) : c; + return result; +} + +U8 Chr_ToLowercase(U8 c) { + U8 result = (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c; + return result; +} diff --git a/code/core/macros.h b/code/core/macros.h index f2d89ff..cae2d9d 100644 --- a/code/core/macros.h +++ b/code/core/macros.h @@ -34,6 +34,8 @@ #define SLL_PushN(h, n, next) ((n)->next = (h), (h) = (n)) #define SLL_PopN(h, next) (((h) != 0) ? (h) = (h)->next : 0) +#define SLL_Enqueue(h, t, n) SLL_EnqueueN(h, t, n, next) + #define function static #define internal static #define global_var static diff --git a/code/core/string.h b/code/core/string.h new file mode 100644 index 0000000..4adb71e --- /dev/null +++ b/code/core/string.h @@ -0,0 +1,36 @@ +#if !defined(LD_CORE_STRING_H_) +#define LD_CORE_STRING_H_ + +#define S(x) Str8_Wrap(sizeof(x) - sizeof(*(x)), (U8 *) (x)) +#define Sz(x) Str8_WrapZ((U8 *) (x)) + +#define Sv(x) (int) (x).count, (x).data +#define Sf(arena, fmt, ...) Str8_Format(arena, fmt, ##__VA_ARGS__) + +function Str8 Str8_Wrap(S64 count, U8 *data); +function Str8 Str8_WrapRange(U8 *start, U8 *end); +function Str8 Str8_WrapZ(U8 *data); + +function Str8 Str8_Copy(M_Arena *arena, Str8 s); + +function Str8 Str8_Format(M_Arena *arena, const char *format, ...); +function Str8 Str8_FormatArgs(M_Arena *arena, const char *format, va_list args); + +typedef U32 Str8_EqualFlags; +enum { + STR8_EQUAL_IGNORE_CASE = (1 << 0) +}; + +function B32 Str8_Equal(Str8 a, Str8 b, Str8_EqualFlags flags); + +function Str8 Str8_Prefix(Str8 s, S64 count); +function Str8 Str8_Suffix(Str8 s, S64 count); + +function Str8 Str8_RemoveAfterLast(Str8 s, U8 c); + +function B32 Str8_EndsWith(Str8 s, Str8 suffix); + +function U8 Chr_ToUppercase(U8 c); +function U8 Chr_ToLowercase(U8 c); + +#endif // LD_CORE_STRING_H_ diff --git a/code/first.c b/code/first.c index a3faef5..4f5b271 100644 --- a/code/first.c +++ b/code/first.c @@ -2,11 +2,16 @@ #include #include +#define STB_IMAGE_IMPLEMENTATION 1 +#include + #include "core/core.h" #include "os/core.h" #include "vulkan/core.h" +#include "game/core.h" + int main(int argc, char **argv) { (void) argc; (void) argv; @@ -24,6 +29,10 @@ int main(int argc, char **argv) { Vk_Setup(window); + + M_Arena *garena = M_ArenaAlloc(GB(64), .initial = MB(4)); + G_ImagesLoad(garena); + bool running = true; while (running) { SDL_Event e; @@ -74,3 +83,4 @@ int main(int argc, char **argv) { #include "core/core.c" #include "os/core.c" #include "vulkan/core.c" +#include "game/core.c" diff --git a/code/game/core.c b/code/game/core.c new file mode 100644 index 0000000..3a63d7a --- /dev/null +++ b/code/game/core.c @@ -0,0 +1,119 @@ + +void G_ImagesLoad(M_Arena *arena) { + M_TempScope(1, &arena) { + FS_List assets = FS_PathList(temp.arena, S("assets")); + + Vk_Buffer staging = Vk_BufferCreate(MB(256), true /* host_visible */); + + U8 *base = staging.data; + U64 offset = 0; + + Vk_CommandBuffer *cmds = Vk_CommandBufferPush(); + + U32 n_images = 0; + + for (FS_Entry *it = assets.first; it != 0; it = it->next) { + if (Str8_EndsWith(it->basename, S("png"))) { + n_images += 1; + } + } + + VkBufferImageCopy copy = { 0 }; + G_Image *images = M_ArenaPush(arena, G_Image, .count = n_images); + n_images = 0; + + // Image upload is sbi_load -> copy to staging -> upload to gpu texture + + for (FS_Entry *it = assets.first; it != 0; it = it->next) { + if (Str8_EndsWith(it->basename, S("png"))) { + S32 w, h, c; + stbi_uc *data = stbi_load((const char *) it->path.data, &w, &h, &c, 4); + + if (data) { + G_Image *image = &images[n_images]; + + U64 image_sz = 4 * w * h; + + M_CopySize(base, data, image_sz); + + copy.bufferOffset = offset; + copy.bufferRowLength = 0; + copy.bufferImageHeight = 0; + + copy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + copy.imageSubresource.mipLevel = 0; + copy.imageSubresource.baseArrayLayer = 0; + copy.imageSubresource.layerCount = 1; + + copy.imageExtent.width = w; + copy.imageExtent.height = h; + copy.imageExtent.depth = 1; + + base += image_sz; + offset += image_sz; + + Assert(offset <= staging.size); + + n_images += 1; + + image->name = Str8_Copy(arena, Str8_RemoveAfterLast(it->basename, '.')); + + image->image.width = w; + image->image.height = h; + + image->image.format = VK_FORMAT_R8G8B8A8_SRGB; + image->image.usage = VK_IMAGE_USAGE_SAMPLED_BIT; + + Vk_ImageCreate(&image->image); + + VkImageMemoryBarrier2 transfer = { 0 }; + VkImageMemoryBarrier2 shader_read = { 0 }; + + transfer.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2; + transfer.srcStageMask = VK_PIPELINE_STAGE_2_NONE; + transfer.srcAccessMask = VK_ACCESS_2_NONE; + transfer.dstStageMask = VK_PIPELINE_STAGE_2_TRANSFER_BIT; + transfer.dstAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT; + transfer.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; + transfer.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; + transfer.image = image->image.handle; + + transfer.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + transfer.subresourceRange.layerCount = 1; + transfer.subresourceRange.levelCount = 1; + + shader_read.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2; + shader_read.srcStageMask = VK_PIPELINE_STAGE_2_TRANSFER_BIT; + shader_read.srcAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT; + shader_read.dstStageMask = VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT; + shader_read.dstAccessMask = VK_ACCESS_2_SHADER_SAMPLED_READ_BIT; + shader_read.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; + shader_read.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; + shader_read.image = image->image.handle; + + shader_read.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + shader_read.subresourceRange.layerCount = 1; + shader_read.subresourceRange.levelCount = 1; + + VkDependencyInfo dep = { 0 }; + dep.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO; + + dep.imageMemoryBarrierCount = 1; + dep.pImageMemoryBarriers = &transfer; + + vk.CmdPipelineBarrier2(cmds->handle, &dep); + + vk.CmdCopyBufferToImage(cmds->handle, staging.handle, image->image.handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©); + + dep.pImageMemoryBarriers = &shader_read; + + vk.CmdPipelineBarrier2(cmds->handle, &dep); + + stbi_image_free(data); + } + } + } + + Vk_CommandBufferSubmit(cmds, true /* wait */); + } +} diff --git a/code/game/core.h b/code/game/core.h new file mode 100644 index 0000000..63d9c47 --- /dev/null +++ b/code/game/core.h @@ -0,0 +1,16 @@ +#if !defined(LD_GAME_CORE_H_) +#define LD_GAME_CORE_H_ + +typedef struct G_Image G_Image; +struct G_Image { + Vk_Image image; + + Str8 name; + + U32 width; + U32 height; +}; + +function void G_ImagesLoad(M_Arena *arena); + +#endif // LD_GAME_CORE_H_ diff --git a/code/os/filesystem.h b/code/os/filesystem.h index 20d6d4e..180966e 100644 --- a/code/os/filesystem.h +++ b/code/os/filesystem.h @@ -13,4 +13,33 @@ function void FS_FileClose(OS_Handle file); function void FS_FileRead(OS_Handle file, void *ptr, U64 size, U64 offset); function void FS_FileWrite(OS_Handle file, void *ptr, U64 size, U64 offset); +typedef U32 FS_EntryType; +enum { + FS_ENTRY_TYPE_FILE = 0, + FS_ENTRY_TYPE_DIR +}; + +typedef struct FS_Entry FS_Entry; +struct FS_Entry { + FS_Entry *next; + + FS_EntryType type; + + Str8 path; + Str8 basename; + + U64 time; + U64 size; +}; + +typedef struct FS_List FS_List; +struct FS_List { + FS_Entry *first; + FS_Entry *last; + + U32 count; +}; + +function FS_List FS_PathList(M_Arena *arena, Str8 path); + #endif // LD_OS_FILESYSTEM_H_ diff --git a/code/os/impl/windows/core.c b/code/os/impl/windows/core.c index 7f70845..1586489 100644 --- a/code/os/impl/windows/core.c +++ b/code/os/impl/windows/core.c @@ -14,6 +14,19 @@ internal Str8 Win32_WideStr(M_Arena *arena, Str8 v) { return result; } +internal Str8 Win32_MultiByteStr(M_Arena *arena, LPWSTR wstr) { + Str8 result = { 0 }; + + S32 nbytes = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, 0, 0, 0, 0); + + result.count = nbytes - 1; + result.data = M_ArenaPush(arena, U8, .count = nbytes); + + WideCharToMultiByte(CP_UTF8, 0, wstr, -1, (LPSTR) result.data, nbytes, 0, 0); + + return result; +} + // Virtual memory U64 VM_PageSize() { diff --git a/code/os/impl/windows/filesystem.c b/code/os/impl/windows/filesystem.c index 7516caf..5b960ff 100644 --- a/code/os/impl/windows/filesystem.c +++ b/code/os/impl/windows/filesystem.c @@ -83,4 +83,39 @@ void FS_FileWrite(OS_Handle file, void *ptr, U64 size, U64 offset) { } } +FS_List FS_PathList(M_Arena *arena, Str8 path) { + FS_List result = { 0 }; + M_TempScope(1, &arena) { + Str8 wpath = Win32_WideStr(temp.arena, Sf(temp.arena, "%.*s\\*", Sv(path))); + + WIN32_FIND_DATAW find = { 0 }; + HANDLE hFind = FindFirstFileW((LPCWSTR) wpath.data, &find); + + while (hFind != INVALID_HANDLE_VALUE) { + if (find.cFileName[0] != L'.') { + FS_Entry *entry = M_ArenaPush(arena, FS_Entry); + + entry->type = (find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? FS_ENTRY_TYPE_DIR : FS_ENTRY_TYPE_FILE; + + entry->basename = Win32_MultiByteStr(arena, find.cFileName); + entry->path = Sf(arena, "%.*s/%.*s", Sv(path), Sv(entry->basename)); + + FILETIME *wt = &find.ftLastWriteTime; + + entry->time = ((U64) wt->dwHighDateTime << 32) | ((U64) wt->dwLowDateTime); + entry->size = ((U64) find.nFileSizeHigh << 32) | ((U64) find.nFileSizeLow); + + SLL_Enqueue(result.first, result.last, entry); + + result.count += 1; + } + + if (!FindNextFileW(hFind, &find)) { + break; + } + } + } + + return result; +} diff --git a/code/vulkan/core.c b/code/vulkan/core.c index 306666f..fa27a51 100644 --- a/code/vulkan/core.c +++ b/code/vulkan/core.c @@ -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); +} diff --git a/code/vulkan/core.h b/code/vulkan/core.h index 88825ea..8c5cc9e 100644 --- a/code/vulkan/core.h +++ b/code/vulkan/core.h @@ -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_ diff --git a/code/vulkan/functions.h b/code/vulkan/functions.h index 426bb55..9103cf9 100644 --- a/code/vulkan/functions.h +++ b/code/vulkan/functions.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); diff --git a/windows.bat b/windows.bat index 3539c84..a5a4012 100644 --- a/windows.bat +++ b/windows.bat @@ -9,6 +9,7 @@ PUSHD "build" SET deps=0 SET release=0 +SET assets=0 FOR %%A in (%*) DO ( SET %%A=1 @@ -51,6 +52,12 @@ IF %deps% equ 1 ( COPY "..\thirdparty\stb\*.h" "deps\stb" > NUL ) +IF %assets% equ 1 ( + ECHO [Copying assets] + + xcopy /Y /Q "..\assets" "assets" +) + ECHO [Building source] SET COMPILER_OPTS=-nologo -W4 -I"deps\SDL3\include" -I"deps\stb" -I"%VULKAN_SDK%\Include"