From 0c73af4374a68fbc87b6083cfa4f1e05ac16f6a0 Mon Sep 17 00:00:00 2001 From: Matthew Date: Sat, 3 May 2025 11:54:40 +1000 Subject: [PATCH] changed asset packer, changed vulkan config, misc fixes --- src/assets.h | 27 +++++-- src/ds.c | 6 ++ src/ds.h | 18 +++-- src/packer.c | 54 ++++++++++--- src/packer.h | 2 +- src/platform/platform.h | 12 +-- src/platform/platform_linux.h | 12 +-- src/renderer.h | 4 +- src/renderer_vulkan.c | 102 ++++++++++++++++++------- src/renderer_vulkan.h | 139 ++++------------------------------ src/renderer_vulkan_public.c | 26 ++++--- src/vulkan_config.c | 2 + 12 files changed, 201 insertions(+), 203 deletions(-) diff --git a/src/assets.h b/src/assets.h index d201190..0408962 100644 --- a/src/assets.h +++ b/src/assets.h @@ -8,7 +8,7 @@ // ::Assets::Types::Header:: -typedef enum AssetType_e +typedef enum AssetType_e : u32 { SHADER_ASSET, TEXTURE_ASSET, @@ -17,7 +17,7 @@ typedef enum AssetType_e ASSET_TYPE_MAX, } AssetType; -typedef enum ShaderAsset_e +typedef enum ShaderAsset_e : u32 { QUAD_FRAG_SPIRV_SHADER, QUAD_VERT_SPIRV_SHADER, @@ -27,7 +27,7 @@ typedef enum ShaderAsset_e SHADER_ASSET_MAX, } ShaderAsset; -typedef enum TextureAsset_e +typedef enum TextureAsset_e : u32 { PATTERMON_OBESE, PATTERMON_PURPLOID, @@ -36,27 +36,27 @@ typedef enum TextureAsset_e TEXTURE_ASSET_MAX } TextureAsset; -typedef enum TextureAssetTag_e +typedef enum TextureAssetTag_e : u32 { TEXTURE_ASSET_TAG_MAX, } TextureAssetTag; -typedef enum SoundAsset_e +typedef enum SoundAsset_e : u32 { SOUND_ASSET_MAX, } SoundAsset; -typedef enum SoundAssetTag_e +typedef enum SoundAssetTag_e : u32 { SOUND_ASSET_TAG_MAX, } SoundAssetTag; -typedef enum ModelAsset_e +typedef enum ModelAsset_e : u32 { MODEL_ASSET_MAX, } ModelAsset; -typedef enum ModelAssetTag_e +typedef enum ModelAssetTag_e : u32 { MODEL_ASSET_TAG_MAX, } ModelAssetTag; @@ -68,6 +68,12 @@ typedef struct TextureAssetMeta u32 ch; } TextureAssetMeta; +typedef struct ModelAssetMeta +{ + u32 vert_count; + u32 idx_count; +} ModelAssetMeta; + typedef struct Asset { u8 *bytes; @@ -88,6 +94,11 @@ typedef struct AssetFile { u64 data_offset; u64 len; + union + { + TextureAssetMeta texture_meta; + ModelAssetMeta model_meta; + }; } AssetFile; typedef struct FileHeader diff --git a/src/ds.c b/src/ds.c index 0526d2e..a204906 100644 --- a/src/ds.c +++ b/src/ds.c @@ -508,6 +508,12 @@ static HashNode *HashTablePushU64Rawptr(HashTable *table, u64 key, rawptr value) return HashTablePush(table, hash, (KeyValuePair){ .key_u64 = key, .value_rawptr = value }); } +static HashNode *HashTablePushU64U64Split(HashTable *table, u64 key, u32 upper, u32 lower) +{ + u64 hash = HashFromString(String8Struct(&key)); + return HashTablePush(table, hash, (KeyValuePair){ .key_u64 = key, .value_u64_upper = upper, .value_u64_lower = lower }); +} + static HashNode *HashTablePushRawptrU64(HashTable *table, rawptr key, u64 value) { u64 hash = HashFromString(String8Struct(&key)); diff --git a/src/ds.h b/src/ds.h index 7c8c820..cb5f9c6 100644 --- a/src/ds.h +++ b/src/ds.h @@ -77,15 +77,20 @@ typedef struct KeyValuePair { union { - u64 key_u64; - rawptr key_rawptr; + u64 key_u64; + rawptr key_rawptr; }; union { - String8 value_string; - rawptr value_rawptr; - u32 value_u32; - u64 value_u64; + String8 value_string; + rawptr value_rawptr; + u32 value_u32; + u64 value_u64; + union + { + u32 value_u64_upper; + u32 value_u64_lower; + }; }; } KeyValuePair; @@ -121,5 +126,6 @@ static HashNode *HashTablePushU64U32(HashTable *table, u64 key, u32 value); static HashNode *HashTablePushU64U64(HashTable *table, u64 key, u64 value); static HashNode *HashTablePushU64String8(HashTable *table, u64 key, String8 value); static HashNode *HashTablePushU64Rawptr(HashTable *table, u64 key, rawptr value); +static HashNode *HashTablePushU64U64Split(HashTable *table, u64 key, u32 upper, u32 lower); static void HashTableDeleteU64(HashTable *table, u64 key); diff --git a/src/packer.c b/src/packer.c index 48dc3ff..fbff72e 100644 --- a/src/packer.c +++ b/src/packer.c @@ -246,13 +246,23 @@ void PackFiles(Arena *arena, FileHeader *header) u8 *file_data = MakeArray(arena, u8, file_size); ReadData(file_data, 0, file_size, asset_file); - u64 prev_offset = data_offset; - data_offset += WriteData(file_data, data_offset, file_size, file); + int w, h, ch; + u8 *image_bytes = stbi_load_from_memory(file_data, file_size, &w, &h, &ch, 4); - Assert((data_offset - prev_offset) == file_size, "File write size invalid"); + u64 loaded_length = u64(w * h * ch); + + u64 prev_offset = data_offset; + data_offset += WriteData(image_bytes, data_offset, loaded_length, file); + + Assert((data_offset - prev_offset) == loaded_length, "File write size invalid"); texture_assets[i].data_offset = prev_offset; - texture_assets[i].len = file_size; + texture_assets[i].len = loaded_length; + texture_assets[i].texture_meta.w = u32(w); + texture_assets[i].texture_meta.h = u32(h); + texture_assets[i].texture_meta.ch = u32(ch); + + stbi_image_free(image_bytes); CloseFile(asset_file); } @@ -269,24 +279,44 @@ void PackFiles(Arena *arena, FileHeader *header) // ::Packer::Tests::Functions::Start:: -static inline void TestAssetIsCorrect(Arena *arena, c8 *file_name, AssetFile *file_info, FILE *file) +static inline void TestAssetIsCorrect(Arena *arena, c8 *file_name, AssetFile *file_info, AssetType type, FILE *file) { FILE *asset_file = OpenFile(file_name, "r"); u64 size = FileLength(asset_file); + u8 *file_data = MakeArray(arena, u8, size); u64 write_count = ReadData(file_data, 0, size, asset_file); Assert(write_count == size, "Incorrect asset size retrieved"); - Assert(file_info->len == size, "file length incorrect"); - u8 *packed_asset = MakeArray(arena, u8, file_info->len); - ReadData(packed_asset, file_info->data_offset, file_info->len, file); - for (u64 i = 0; i < size; i++) + u8 *image_bytes; + u64 image_length; + if (type == TEXTURE_ASSET) { - Assert(file_data[i] == packed_asset[i], "Asset file data is invalid"); + int w, h, ch; + image_bytes = stbi_load_from_memory(file_data, size, &w, &h, &ch, 4); + image_length = u64(w * h * ch); + + Assert(file_info->len == image_length, "file length incorrect"); + + for (u64 i = 0; i < image_length; i++) + { + Assert(image_bytes[i] == packed_asset[i], "Asset file data is invalid"); + } + + stbi_image_free(image_bytes); + } + else + { + Assert(file_info->len == size, "file length incorrect"); + + for (u64 i = 0; i < size; i++) + { + Assert(file_data[i] == packed_asset[i], "Asset file data is invalid"); + } } CloseFile(asset_file); @@ -337,7 +367,7 @@ void TestAssetPack(Arena *arena) for (u32 i = 0; i < SHADER_ASSET_MAX; i++) { - TestAssetIsCorrect(arena, g_Shader_File_Names[i], &files[SHADER_ASSET][i], file); + TestAssetIsCorrect(arena, g_Shader_File_Names[i], &files[SHADER_ASSET][i], SHADER_ASSET, file); } pDirNavigate(return_dir); @@ -345,7 +375,7 @@ void TestAssetPack(Arena *arena) for (u32 i = 0; i < TEXTURE_ASSET_MAX; i++) { - TestAssetIsCorrect(arena, g_Texture_File_Names[i], &files[TEXTURE_ASSET][i], file); + TestAssetIsCorrect(arena, g_Texture_File_Names[i], &files[TEXTURE_ASSET][i], TEXTURE_ASSET, file); } } diff --git a/src/packer.h b/src/packer.h index f26c564..8df4299 100644 --- a/src/packer.h +++ b/src/packer.h @@ -63,7 +63,7 @@ void MoveToShaderDir(c8 **return_dir); // ::Packer::Tests::Functions::Header:: void TestAssetPack(Arena *arena); -static inline void TestAssetIsCorrect(Arena *arena, c8 *file_name, AssetFile *file_info, FILE *file); +static inline void TestAssetIsCorrect(Arena *arena, c8 *file_name, AssetFile *file_info, AssetType type, FILE *file); // ::Packer::Main::Functions::Header:: diff --git a/src/platform/platform.h b/src/platform/platform.h index 1e22b11..dd47d4d 100644 --- a/src/platform/platform.h +++ b/src/platform/platform.h @@ -132,12 +132,12 @@ static inline u64 pCPUTimerRead(); // ::Platform::Atomics::Header:: -#define DefSigAtomicFetchIncr(T) static inline T p##T##AtomicFetchIncr(T *ptr) -#define DefSigAtomicFetchSub(T) static inline T p##T##AtomicFetchSub(T *ptr, T count) -#define DefSigAtomicIncr(T) static inline void p##T##AtomicIncr(T *ptr) -#define DefSigAtomicStore(T) static inline void p##T##AtomicStore(T *ptr, T value) -#define DefSigAtomicLoad(T) static inline T p##T##AtomicLoad(T *ptr) -#define DefSigAtomicCompareExchange(T) static inline b32 p##T##AtomicCompareExchange(T *ptr, T *expected, T desired) +#define DefSigAtomicFetchIncr(T) static inline T p##T##AtomicFetchIncr(T volatile *ptr) +#define DefSigAtomicFetchSub(T) static inline T p##T##AtomicFetchSub(T volatile *ptr, T count) +#define DefSigAtomicIncr(T) static inline void p##T##AtomicIncr(T volatile *ptr) +#define DefSigAtomicStore(T) static inline void p##T##AtomicStore(T volatile *ptr, T value) +#define DefSigAtomicLoad(T) static inline T p##T##AtomicLoad(T volatile *ptr) +#define DefSigAtomicCompareExchange(T) static inline b32 p##T##AtomicCompareExchange(T volatile *ptr, T *expected, T desired) DefScalarSig(AtomicFetchIncr); DefScalarSig(AtomicFetchSub); diff --git a/src/platform/platform_linux.h b/src/platform/platform_linux.h index 5af8955..7de0eea 100644 --- a/src/platform/platform_linux.h +++ b/src/platform/platform_linux.h @@ -90,37 +90,37 @@ b32 pSyscallErrCheck(void *ptr); // ::Platform::Linux::Atomics::Header:: #define DefAtomicFetchIncr(T) \ -static inline T p##T##AtomicFetchIncr(T *ptr) \ +static inline T p##T##AtomicFetchIncr(T volatile *ptr) \ { \ return __atomic_fetch_add(ptr, (T)1, __ATOMIC_ACQUIRE); \ } #define DefAtomicIncr(T) \ -static inline void p##T##AtomicIncr(T *ptr) \ +static inline void p##T##AtomicIncr(T volatile *ptr) \ { \ __atomic_fetch_add(ptr, (T)1, __ATOMIC_RELEASE); \ } #define DefAtomicStore(T) \ -static inline void p##T##AtomicStore(T *ptr, T value) \ +static inline void p##T##AtomicStore(T volatile *ptr, T value) \ { \ __atomic_store_n(ptr, value, __ATOMIC_RELEASE); \ } #define DefAtomicLoad(T) \ -static inline T p##T##AtomicLoad(T *ptr) \ +static inline T p##T##AtomicLoad(T volatile *ptr) \ { \ return __atomic_load_n(ptr, __ATOMIC_ACQUIRE); \ } #define DefAtomicFetchSub(T) \ -static inline T p##T##AtomicFetchSub(T *ptr, T count) \ +static inline T p##T##AtomicFetchSub(T volatile *ptr, T count) \ { \ return __atomic_fetch_sub(ptr, count, __ATOMIC_ACQUIRE); \ } #define DefAtomicCompareExchange(T) \ -static inline b32 p##T##AtomicCompareExchange(T *ptr, T *expected, T desired) \ +static inline b32 p##T##AtomicCompareExchange(T volatile *ptr, T *expected, T desired) \ { \ return __atomic_compare_exchange_n(ptr, expected, desired, true, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED); \ } diff --git a/src/renderer.h b/src/renderer.h index 22813df..34bfd48 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -2,7 +2,7 @@ // ::Renderer::Declarations::Header:: -typedef u32 rDescHandle; +typedef struct rDescHandle rDescHandle; // @requirement rRenderBufferType type // @requirement u32 size @@ -107,7 +107,7 @@ static rBuffer rBufferCreate(rRenderBufferType type, u64 size); static b32 rBufferMap(); static void rBufferFree(rRenderBuffer *buffers, u32 buffer_count); static b32 rBufferUpload(rRenderBuffer **buffer, rawptr *ptr, u32 count, u32 thr_ix); -static void rBufferCreateAndUpload(rRenderBuffer *buffer, rawptr ptr); +static void rBufferCreateAndUpload(ModelAsset asset_id); static rDescHandle rTextureCreateAndUpload(TextureAsset asset_id); static void rBufferBindVertex(rRenderBuffer *buffer); static void rBufferBindIndex(rRenderBuffer *buffer); diff --git a/src/renderer_vulkan.c b/src/renderer_vulkan.c index 1aa23c4..97860cb 100644 --- a/src/renderer_vulkan.c +++ b/src/renderer_vulkan.c @@ -494,54 +494,51 @@ static void vImageUpload(rTextureBuffer *buffer, u8 *data, u32 thread_idx) // ::Vulkan::Descriptors::Functions::Start:: -static void vDescHandlePush(vDescType type, rDescHandle handle) +static void vDescIndexPush(vDescType type, u32 index) { vDescBindings *bindings = v_Renderer.desc_bindings + type; - Assert(bindings->free_count < DESC_MAX_BINDINGS-1, "vDescHandlePush failure: free_count equal to DESC_MAX_BINDINGS-1"); + Assert(bindings->free_count < DESC_MAX_BINDINGS-1, "vDescIndexPush failure: free_count equal to DESC_MAX_BINDINGS-1"); - bindings->free[bindings->free_count] = handle; + bindings->free[bindings->free_count] = index; bindings->free_count += 1; } -static rDescHandle vDescHandlePop(vDescType type) +static u32 vDescIndexPop(vDescType type) { vDescBindings *bindings = v_Renderer.desc_bindings + type; - Assert(bindings->free_count > 0, "vDescHandlePop failure: free_count is 0"); + Assert(bindings->free_count > 0, "vDescIndexPop failure: free_count is 0"); bindings->free_count -= 1; return bindings->free[bindings->free_count]; } -static vAssetInfo *vDescHandleSearch(vDescType type, u64 asset_id) +static rDescHandle vDescHandleSearch(vDescType type, u32 asset_id) { - vAssetInfo *asset_info = NULL; + rDescHandle asset_info = { + .asset_id = UINT32_MAX, + }; HashTable *table = &v_Renderer.desc_bindings[type].lookup_table; KeyValuePair *kv_pair = HashTableSearchU64(table, asset_id); if (kv_pair != NULL) { - asset_info = (vAssetInfo *)kv_pair->value_rawptr; + asset_info.asset_id = kv_pair->value_u64_upper; + asset_info.desc_index = kv_pair->value_u64_lower; } return asset_info; } -static void DescriptorTableInsert(vDescType type, u64 asset_id, rDescHandle handle) +static void DescriptorTableInsert(vDescType type, rDescHandle handle) { - vAssetInfo *asset_info = FLMemAlloc(sizeof(vAssetInfo)); - - asset_info->handle = handle; - asset_info->type = type; - asset_info->asset_id = asset_id; - HashTable *table = &v_Renderer.desc_bindings[type].lookup_table; - HashTablePushU64Rawptr(table, asset_id, asset_info); + HashTablePushU64U64Split(table, handle.asset_id, handle.asset_id, handle.desc_index); } -static void vDescHandleDelete(vDescType type, u64 asset_id) +static void vDescHandleDelete(vDescType type, u32 asset_id) { HashTable *table = &v_Renderer.desc_bindings[type].lookup_table; HashTableDeleteU64(table, asset_id); @@ -665,7 +662,7 @@ static void vEnableDebug() } } -static void vInitArenas() +static void vArenasInit() { v_Renderer.mem.perm_arena = ArenaCreateDebug(MB(16), __LINE__); @@ -1417,21 +1414,68 @@ static b32 vDescriptorsInit() if (result != VK_SUCCESS) success = false; - for (u32 i = 0; i < vDT_MAX; i++) + if (success) { - bindings[i].free = ArenaAlloc(v_Renderer.mem.perm_arena, sizeof(u32) * DESC_MAX_BINDINGS); - - HashTableInit(&bindings[i].lookup_table, 6); - - u32 free_count = 0; - for (i32 j = DESC_MAX_BINDINGS-1; j >= 0; j--) + for (u32 i = 0; i < vDT_MAX; i++) { - bindings[i].free[j] = free_count++; + bindings[i].free = ArenaAlloc(v_Renderer.mem.perm_arena, sizeof(u32) * DESC_MAX_BINDINGS); + + HashTableInit(&bindings[i].lookup_table, 6); + + u32 free_count = 0; + for (i32 j = DESC_MAX_BINDINGS-1; j >= 0; j--) + { + bindings[i].free[j] = free_count++; + } + + bindings[i].free_count = free_count; } - - bindings[i].free_count = free_count; } + if (success) + { + VkPhysicalDeviceProperties properties; + vkGetPhysicalDeviceProperties(v_Renderer.handles.phys_device, &properties); + + VkSamplerCreateInfo sampler_create_info = { + .sType = STYPE(SAMPLER_CREATE_INFO), + .magFilter = VK_FILTER_NEAREST, + .minFilter = VK_FILTER_NEAREST, + .addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT, + .addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT, + .addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT, + .anisotropyEnable = VK_TRUE, + .maxAnisotropy = properties.limits.maxSamplerAnisotropy, + .borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK, + .compareOp = VK_COMPARE_OP_ALWAYS, + .mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR, + }; + + result = vkCreateSampler(v_Renderer.handles.device, &sampler_create_info, NULL, &v_Renderer.handles.nearest_sampler); + if (result != VK_SUCCESS) + { + Printfln("vDescriptorsInit failure: vkCreateSampler failed: %s", vVkResultStr(result)); + success = false; + } + } + + if (success) + { + VkDescriptorImageInfo sampler_info = { + .sampler = v_Renderer.handles.nearest_sampler, + }; + + VkWriteDescriptorSet desc_write = { + .sType = STYPE(WRITE_DESCRIPTOR_SET), + .dstSet = v_Renderer.handles.desc_sets[vDT_SHARED], + .dstBinding = 3, + .descriptorCount = 1, + .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER, + .pImageInfo = &sampler_info, + }; + + vkUpdateDescriptorSets(v_Renderer.handles.device, 1, &desc_write, 0, NULL); + } return success; } @@ -1553,7 +1597,7 @@ static b32 vShaderModuleInit(u8 *bytes, u32 len, VkShaderModule *module) return success; } -static b32 vInitBuffers() +static b32 vBuffersInit() { vRBuffers *buf = &v_Renderer.buffers; Arena *arena = v_Renderer.mem.perm_arena; diff --git a/src/renderer_vulkan.h b/src/renderer_vulkan.h index 5f17139..e2bc24b 100644 --- a/src/renderer_vulkan.h +++ b/src/renderer_vulkan.h @@ -175,124 +175,16 @@ typedef enum DescType_e vDT_MAX, } vDescType; -/* -typedef struct vMeshBuffer +typedef struct rDescHandle { - //TODO: FIX - //rRenderBuffer index_buf, vertex_buf; - u32 index_count; -} vMeshBuffer; - -typedef struct vPipelineStructures -{ - VkPipelineLayout pipeline_layout; - VkDescriptorPool pool; - VkDescriptorSetLayout layouts[vDT_MAX]; - VkDescriptorSet sets[vDT_MAX]; - vDescBindings *bindings; - u16 bindings_count; - VkPipeline pipelines[R_PIPELINE_MAX]; -} vPipelineStructures; - -typedef struct vFrameStructures -{ - VkCommandPool *pools; - VkCommandBuffer *buffers; - VkSemaphore *swapchain_sems; - VkSemaphore *render_sems; - VkFence *render_fences; - rRenderBuffer **buffer_destroy_queues; - u32 *buffer_counts; -} vFrameStructures; - -typedef struct vImmediateStructures -{ - VkCommandPool *pools; - VkCommandBuffer *cmds; - VkFence *fences; -} vImmediateStructures; - -typedef struct vSwapchainStructures -{ - VkFormat format; - VkColorSpaceKHR color_space; - VkPresentModeKHR present_mode; - VkExtent3D extent; - VkImage *imgs; - VkImageView *views; - u32 img_count; - vImage draw_img; - vImage depth_img; -} vSwapchainStructures; - -typedef struct vFrameState -{ - u32 img_ix; - u64 frame_cnt; - u64 prev_frame; - b8 begin_rendering; - rPipelineHandle last_pipeline; - rRenderBuffer *prev_buffers; - u32 prev_buffer_count; -} vFrameState; - -typedef struct vVulkan -{ - pLibrary lib; - VkInstance inst; - VkSurfaceKHR surface; - VkDevice device; - VkSwapchainKHR swapchain; - VkPhysicalDevice phys_device; - vDeviceQueues queues; - VmaAllocator alloc; - vFrameStructures frame; - vImmediateStructures imm; - vSwapchainStructures sc; - vPipelineStructures pipe; -#ifdef BUILD_DEBUG - VkDebugUtilsMessengerEXT debug; -#endif -} vVulkan; - -typedef struct vPendingUpdates -{ - u16 render_width; - u16 render_height; - b8 resized; -} vPendingUpdates; - -typedef struct vVulkanConfig -{ - u8 avail_threads; - u8 volatile sleeping_threads; -#ifdef __linux__ - pthread_t *threads; -#elif _WIN32 - #error not yet implemented -#endif -} vVulkanConfig; - - -typedef struct vRenderer -{ - vVulkan vk; - vVulkanConfig vk_conf; - vFrameState frame_state; - vPendingUpdates pending; - Arena *perm_arena; - Arena *frame_arena[FRAME_OVERLAP]; - rUploadQueue upload_queues[vDT_MAX]; -} vRenderer; - -*/ - -// NEW + u32 asset_id; + u32 desc_index; +} rDescHandle; typedef struct vAssetInfo { rDescHandle handle; - vDescType type; + vDescType type; union { u64 asset_id; @@ -424,6 +316,7 @@ typedef struct vRHandles VkDescriptorSet desc_sets[vDT_MAX]; VkQueue gfx_queue; VkQueue tfer_queue; + VkSampler nearest_sampler; #ifdef BUILD_DEBUG VkDebugUtilsMessengerEXT debug; #endif @@ -552,7 +445,7 @@ const char *vVkResultStr(VkResult result); static b32 vInitInstance(); static void vEnableDebug(); -static void vInitArenas(); +static void vArenasInit(); static b32 vLibraryLoad(); static b32 vInstanceFunctionsInit(); static b32 vGlobalFunctionsInit(); @@ -574,7 +467,7 @@ static b32 vPipelinesInit(); static b32 vShaderModuleInit(u8 *bytes, u32 len, VkShaderModule *module); static void vUploadQueuesInit(); static void vLoaderStartThreads(); -static b32 vInitBuffers(); +static b32 vBuffersInit(); // ::Vulkan::Util::Functions::Header:: @@ -593,16 +486,16 @@ static inline void vImageCopyToImage(VkCommandBuffer cmd, VkImage src, VkImage d // ::Vulkan::Async::Functions::Header:: +void vLoaderWake(); +static u32 vLoaderProcessBuffers(u32 thread_index); +static u32 vLoaderProcessSamplers(u32 thread_index); + #ifdef __linux__ void *vLoaderStart(void *thread_data); #elif _WIN32 # error not yet implemented #endif -void vLoaderWake(); -static u32 vLoaderProcessBuffers(u32 thread_index); -static u32 vLoaderProcessSamplers(u32 thread_index); - // ::Vulkan::ImmediateSubmit::Functions::Header:: static b32 vImmSubmitBegin(VkDevice device, VkFence fence, VkCommandBuffer cmd); @@ -623,10 +516,10 @@ static void vImageUpload(rTextureBuffer *buffer, u8 *data, u32 thread_idx); // ::Vulkan::Descriptors::Functions::Header:: -static void vDescHandlePush(vDescType type, rDescHandle handle); -static rDescHandle vDescHandlePop(vDescType type); -static vAssetInfo *vDescHandleSearch(vDescType type, u64 asset_id); -static void vDescHandleDelete(vDescType type, u64 asset_id); +static void vDescIndexPush(vDescType type, u32 index); +static u32 vDescIndexPop(vDescType type); +static rDescHandle vDescHandleSearch(vDescType type, u32 asset_id); +static void vDescHandleDelete(vDescType type, u32 asset_id); // ::Vulkan::Buffers::Functions::Header:: diff --git a/src/renderer_vulkan_public.c b/src/renderer_vulkan_public.c index 1207f22..3548745 100644 --- a/src/renderer_vulkan_public.c +++ b/src/renderer_vulkan_public.c @@ -3,7 +3,7 @@ b32 rInit() { rThreadCountSet(pCPUCountGet()); - vInitArenas(); + vArenasInit(); vCustomizePipelines(); Assert(vInitInstance(), "Unable to initialize instance"); @@ -24,7 +24,7 @@ b32 rInit() Assert(vImmediateStructuresInit(), "Unable to create immediate structures"); Assert(vDescriptorsInit(), "Unable to initialize descriptors."); Assert(vPipelinesInit(), "Unable to initialize pipelines."); - Assert(vInitBuffers(), "Unable to initialize buffers."); + Assert(vBuffersInit(), "Unable to initialize buffers."); vUploadQueuesInit(); vLoaderStartThreads(); @@ -265,28 +265,32 @@ static b32 rBufferUpload(rRenderBuffer **buffers, rawptr *ptrs, u32 count, u32 t return false; } -static void rBufferCreateAndUpload(rRenderBuffer *buffer, rawptr ptr) +static void rBufferCreateAndUpload(ModelAsset asset_id) { - - // TODO: replace - /* + /* TODO: figure out how buffers should be loaded to get index count, etc TicketMutLock(&renderer.upload_queues[vDT_MATERIAL].ticket_mut); + rDescHandle handle = vDescHandleSearch(MODEL_ASSET, asset_id); + if (handle.asset_id != UINT32_MAX) + { + vBufferAlloc *buffer = FLMemAlloc(sizeof(vBufferAlloc)); + buffer-> + } + u32 job_idx = JobQueueAdd(&renderer.upload_queues[vDT_MATERIAL].job_queue, 1); renderer.upload_queues[vDT_MATERIAL].queued_buffers[job_idx] = buffer; renderer.upload_queues[vDT_MATERIAL].data[job_idx] = ptr; TicketMutUnlock(&renderer.upload_queues[vDT_MATERIAL].ticket_mut); - */ vLoaderWake(); + */ } static rDescHandle rTextureCreateAndUpload(TextureAsset asset_id) { - rDescHandle handle = 0; - // TODO: replace it + // TODO: replace it, store asset information (vertex count, res, etc) in asset header files and pull them for use in draw commands, etc. /* vAssetInfo *info = vDescHandleSearch(vDT_SAMPLED_IMAGE, asset_id); if (info == NULL) @@ -305,7 +309,7 @@ static rDescHandle rTextureCreateAndUpload(TextureAsset asset_id) renderer.upload_queues[vDT_SAMPLED_IMAGE].queued_textures[job_idx] = buffer; renderer.upload_queues[vDT_SAMPLED_IMAGE].data[job_idx] = asset.bytes; - handle = vDescHandlePop(vDT_SAMPLED_IMAGE); + handle = vDescIndexPop(vDT_SAMPLED_IMAGE); TicketMutUnlock(&renderer.upload_queues[vDT_SAMPLED_IMAGE].ticket_mut); @@ -317,6 +321,8 @@ static rDescHandle rTextureCreateAndUpload(TextureAsset asset_id) } */ + rDescHandle handle = {UINT32_MAX}; + return handle; } diff --git a/src/vulkan_config.c b/src/vulkan_config.c index 87c383f..6bee594 100644 --- a/src/vulkan_config.c +++ b/src/vulkan_config.c @@ -229,6 +229,7 @@ static VkDescriptorBindingFlags shared_desc_binding_flags[] = { VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT, VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT, VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT, + VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT, }; static VkDescriptorSetLayoutBindingFlagsCreateInfo shared_layout_binding_flags_info = { @@ -241,6 +242,7 @@ static VkDescriptorSetLayoutBinding shared_layout_bindings[] = { { .binding = 0, .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, .descriptorCount = 1, .stageFlags = VK_SHADER_STAGE_ALL }, { .binding = 1, .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, .descriptorCount = 1, .stageFlags = VK_SHADER_STAGE_ALL }, { .binding = 2, .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, .descriptorCount = 1, .stageFlags = VK_SHADER_STAGE_ALL }, + { .binding = 3, .descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER, .descriptorCount = 1, .stageFlags = VK_SHADER_STAGE_ALL }, }; static VkDescriptorSetLayoutCreateInfo shared_layout_create_info = {