Added image loading
Added some string functions and macros Added path listing on windows Added assets
This commit is contained in:
BIN
assets/hunter.png
Normal file
BIN
assets/hunter.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 730 B |
@@ -1,7 +1,7 @@
|
|||||||
#if !defined(LD_CORE_ARENA_H_)
|
#if !defined(LD_CORE_ARENA_H_)
|
||||||
#define LD_CORE_ARENA_H_
|
#define LD_CORE_ARENA_H_
|
||||||
|
|
||||||
#define AlignUp(x, a) (((x) + ~((a) - 1)) & ~((a) - 1))
|
#define AlignUp(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
|
||||||
#define AlignDown(x, a) (((x)) & ~((a) - 1))
|
#define AlignDown(x, a) (((x)) & ~((a) - 1))
|
||||||
|
|
||||||
#define KB(x) ((U64) (x) << 10)
|
#define KB(x) ((U64) (x) << 10)
|
||||||
|
|||||||
@@ -1 +1,2 @@
|
|||||||
#include "impl/arena.c"
|
#include "impl/arena.c"
|
||||||
|
#include "impl/string.c"
|
||||||
|
|||||||
@@ -5,5 +5,6 @@
|
|||||||
#include "platform.h"
|
#include "platform.h"
|
||||||
#include "macros.h"
|
#include "macros.h"
|
||||||
#include "arena.h"
|
#include "arena.h"
|
||||||
|
#include "string.h"
|
||||||
|
|
||||||
#endif // LD_CORE_CORE_H_
|
#endif // LD_CORE_CORE_H_
|
||||||
|
|||||||
150
code/core/impl/string.c
Normal file
150
code/core/impl/string.c
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
Str8 Str8_Wrap(S64 count, U8 *data) {
|
||||||
|
Str8 result;
|
||||||
|
result.data = data;
|
||||||
|
result.count = count;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
Str8 Str8_WrapRange(U8 *start, U8 *end) {
|
||||||
|
Str8 result;
|
||||||
|
result.data = start;
|
||||||
|
result.count = cast(S64) (end - start);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal S64 Str8_CountZ(U8 *data) {
|
||||||
|
S64 result = 0;
|
||||||
|
while (data[result] != 0) { result += 1; }
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
Str8 Str8_WrapZ(U8 *data) {
|
||||||
|
Str8 result;
|
||||||
|
result.data = data;
|
||||||
|
result.count = Str8_CountZ(data);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
Str8 Str8_Copy(M_Arena *arena, Str8 s) {
|
||||||
|
Str8 result;
|
||||||
|
result.count = s.count;
|
||||||
|
result.data = M_ArenaPush(arena, U8, .count = s.count + 1);
|
||||||
|
|
||||||
|
M_CopySize(result.data, s.data, s.count);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
Str8 Str8_Format(M_Arena *arena, const char *format, ...) {
|
||||||
|
va_list args;
|
||||||
|
va_start(args, format);
|
||||||
|
|
||||||
|
Str8 result = Str8_FormatArgs(arena, format, args);
|
||||||
|
va_end(args);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal S64 Str8_ProcessFormat(Str8 out, const char *format, va_list args) {
|
||||||
|
S64 result = vsnprintf((char *) out.data, (int) out.count, format, args);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
Str8 Str8_FormatArgs(M_Arena *arena, const char *format, va_list args) {
|
||||||
|
Str8 result;
|
||||||
|
|
||||||
|
va_list copy;
|
||||||
|
va_copy(copy, args);
|
||||||
|
|
||||||
|
U64 offset = M_ArenaOffset(arena);
|
||||||
|
|
||||||
|
result.count = KB(1);
|
||||||
|
result.data = M_ArenaPush(arena, U8, .count = result.count);
|
||||||
|
|
||||||
|
S64 needed = Str8_ProcessFormat(result, format, args);
|
||||||
|
if (needed >= 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;
|
||||||
|
}
|
||||||
@@ -34,6 +34,8 @@
|
|||||||
#define SLL_PushN(h, n, next) ((n)->next = (h), (h) = (n))
|
#define SLL_PushN(h, n, next) ((n)->next = (h), (h) = (n))
|
||||||
#define SLL_PopN(h, next) (((h) != 0) ? (h) = (h)->next : 0)
|
#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 function static
|
||||||
#define internal static
|
#define internal static
|
||||||
#define global_var static
|
#define global_var static
|
||||||
|
|||||||
36
code/core/string.h
Normal file
36
code/core/string.h
Normal file
@@ -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_
|
||||||
10
code/first.c
10
code/first.c
@@ -2,11 +2,16 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
|
#define STB_IMAGE_IMPLEMENTATION 1
|
||||||
|
#include <stb_image.h>
|
||||||
|
|
||||||
#include "core/core.h"
|
#include "core/core.h"
|
||||||
#include "os/core.h"
|
#include "os/core.h"
|
||||||
|
|
||||||
#include "vulkan/core.h"
|
#include "vulkan/core.h"
|
||||||
|
|
||||||
|
#include "game/core.h"
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
(void) argc;
|
(void) argc;
|
||||||
(void) argv;
|
(void) argv;
|
||||||
@@ -24,6 +29,10 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
Vk_Setup(window);
|
Vk_Setup(window);
|
||||||
|
|
||||||
|
|
||||||
|
M_Arena *garena = M_ArenaAlloc(GB(64), .initial = MB(4));
|
||||||
|
G_ImagesLoad(garena);
|
||||||
|
|
||||||
bool running = true;
|
bool running = true;
|
||||||
while (running) {
|
while (running) {
|
||||||
SDL_Event e;
|
SDL_Event e;
|
||||||
@@ -74,3 +83,4 @@ int main(int argc, char **argv) {
|
|||||||
#include "core/core.c"
|
#include "core/core.c"
|
||||||
#include "os/core.c"
|
#include "os/core.c"
|
||||||
#include "vulkan/core.c"
|
#include "vulkan/core.c"
|
||||||
|
#include "game/core.c"
|
||||||
|
|||||||
119
code/game/core.c
Normal file
119
code/game/core.c
Normal file
@@ -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 */);
|
||||||
|
}
|
||||||
|
}
|
||||||
16
code/game/core.h
Normal file
16
code/game/core.h
Normal file
@@ -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_
|
||||||
@@ -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_FileRead(OS_Handle file, void *ptr, U64 size, U64 offset);
|
||||||
function void FS_FileWrite(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_
|
#endif // LD_OS_FILESYSTEM_H_
|
||||||
|
|||||||
@@ -14,6 +14,19 @@ internal Str8 Win32_WideStr(M_Arena *arena, Str8 v) {
|
|||||||
return result;
|
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
|
// Virtual memory
|
||||||
|
|
||||||
U64 VM_PageSize() {
|
U64 VM_PageSize() {
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -330,6 +330,7 @@ bool Vk_Setup(SDL_Window *window) {
|
|||||||
|
|
||||||
VkResult err = vk.CreateCommandPool(vk.device, &pool, 0, &frame->pool);
|
VkResult err = vk.CreateCommandPool(vk.device, &pool, 0, &frame->pool);
|
||||||
|
|
||||||
|
// Base command buffer
|
||||||
VkCommandBufferAllocateInfo alloc = { 0 };
|
VkCommandBufferAllocateInfo alloc = { 0 };
|
||||||
alloc.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
alloc.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
||||||
alloc.commandBufferCount = 1;
|
alloc.commandBufferCount = 1;
|
||||||
@@ -338,16 +339,26 @@ bool Vk_Setup(SDL_Window *window) {
|
|||||||
|
|
||||||
vk.AllocateCommandBuffers(vk.device, &alloc, &frame->cmd);
|
vk.AllocateCommandBuffers(vk.device, &alloc, &frame->cmd);
|
||||||
|
|
||||||
VkSemaphoreCreateInfo semaphore = { 0 };
|
// Scratch command buffers
|
||||||
semaphore.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
VkCommandBuffer scratch[VK_NUM_SCRATCH];
|
||||||
|
alloc.commandBufferCount = VK_NUM_SCRATCH;
|
||||||
|
|
||||||
err = Min(vk.CreateSemaphore(vk.device, &semaphore, 0, &frame->acquire), err);
|
vk.AllocateCommandBuffers(vk.device, &alloc, scratch);
|
||||||
err = Min(vk.CreateSemaphore(vk.device, &semaphore, 0, &frame->complete), err);
|
|
||||||
|
|
||||||
VkFenceCreateInfo fence = { 0 };
|
VkFenceCreateInfo fence = { 0 };
|
||||||
fence.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
|
fence.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
|
||||||
fence.flags = VK_FENCE_CREATE_SIGNALED_BIT;
|
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);
|
err = Min(vk.CreateFence(vk.device, &fence, 0, &frame->fence), err);
|
||||||
|
|
||||||
vk.err = err;
|
vk.err = err;
|
||||||
@@ -450,3 +461,137 @@ void Vk_FrameEnd() {
|
|||||||
|
|
||||||
vk.n_frames += 1;
|
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 function static
|
||||||
|
|
||||||
#define VK_MAX_FRAMES_IN_FLIGHT 8
|
#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;
|
typedef struct Vk_Frame Vk_Frame;
|
||||||
struct Vk_Frame {
|
struct Vk_Frame {
|
||||||
@@ -32,9 +39,35 @@ struct Vk_Frame {
|
|||||||
VkSemaphore acquire, complete;
|
VkSemaphore acquire, complete;
|
||||||
VkFence fence;
|
VkFence fence;
|
||||||
|
|
||||||
|
U32 next_scratch;
|
||||||
|
Vk_CommandBuffer scratch[VK_NUM_SCRATCH];
|
||||||
|
|
||||||
U32 image; // swapchain image index
|
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;
|
typedef struct Vk_Context Vk_Context;
|
||||||
struct Vk_Context {
|
struct Vk_Context {
|
||||||
void *lib;
|
void *lib;
|
||||||
@@ -86,4 +119,9 @@ function bool Vk_Setup(SDL_Window *window);
|
|||||||
function Vk_Frame *Vk_FrameBegin(SDL_Window *window);
|
function Vk_Frame *Vk_FrameBegin(SDL_Window *window);
|
||||||
function void Vk_FrameEnd();
|
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_
|
#endif // LD_VULKAN_CORE_H_
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
VK_FUNC(GetDeviceProcAddr);
|
VK_FUNC(GetDeviceProcAddr);
|
||||||
VK_FUNC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
|
VK_FUNC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
|
||||||
VK_FUNC(GetPhysicalDeviceSurfaceFormatsKHR);
|
VK_FUNC(GetPhysicalDeviceSurfaceFormatsKHR);
|
||||||
|
VK_FUNC(GetPhysicalDeviceMemoryProperties2);
|
||||||
|
|
||||||
#if defined(VK_USE_PLATFORM_WIN32_KHR)
|
#if defined(VK_USE_PLATFORM_WIN32_KHR)
|
||||||
VK_FUNC(CreateWin32SurfaceKHR);
|
VK_FUNC(CreateWin32SurfaceKHR);
|
||||||
@@ -39,7 +40,16 @@
|
|||||||
VK_FUNC(BeginCommandBuffer);
|
VK_FUNC(BeginCommandBuffer);
|
||||||
VK_FUNC(EndCommandBuffer);
|
VK_FUNC(EndCommandBuffer);
|
||||||
VK_FUNC(DeviceWaitIdle);
|
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(CmdPipelineBarrier2);
|
||||||
VK_FUNC(CmdBeginRendering);
|
VK_FUNC(CmdBeginRendering);
|
||||||
VK_FUNC(CmdEndRendering);
|
VK_FUNC(CmdEndRendering);
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ PUSHD "build"
|
|||||||
|
|
||||||
SET deps=0
|
SET deps=0
|
||||||
SET release=0
|
SET release=0
|
||||||
|
SET assets=0
|
||||||
|
|
||||||
FOR %%A in (%*) DO (
|
FOR %%A in (%*) DO (
|
||||||
SET %%A=1
|
SET %%A=1
|
||||||
@@ -51,6 +52,12 @@ IF %deps% equ 1 (
|
|||||||
COPY "..\thirdparty\stb\*.h" "deps\stb" > NUL
|
COPY "..\thirdparty\stb\*.h" "deps\stb" > NUL
|
||||||
)
|
)
|
||||||
|
|
||||||
|
IF %assets% equ 1 (
|
||||||
|
ECHO [Copying assets]
|
||||||
|
|
||||||
|
xcopy /Y /Q "..\assets" "assets"
|
||||||
|
)
|
||||||
|
|
||||||
ECHO [Building source]
|
ECHO [Building source]
|
||||||
|
|
||||||
SET COMPILER_OPTS=-nologo -W4 -I"deps\SDL3\include" -I"deps\stb" -I"%VULKAN_SDK%\Include"
|
SET COMPILER_OPTS=-nologo -W4 -I"deps\SDL3\include" -I"deps\stb" -I"%VULKAN_SDK%\Include"
|
||||||
|
|||||||
Reference in New Issue
Block a user