diff options
author | Omniscient <omniscient.oce@gmail.com> | 2024-10-27 00:56:55 +1100 |
---|---|---|
committer | Omniscient <omniscient.oce@gmail.com> | 2024-10-27 00:56:55 +1100 |
commit | e597fbb916848df1f6fbd4da04c1ab6f89a25b45 (patch) | |
tree | 4f7205d14cd78f2a489ab3098c8211549d638448 /src/backend_vk.c | |
parent | 3946ae807a2de00b3c810f986f60ba9cc32bc1a7 (diff) |
start on vulkan backend
Diffstat (limited to 'src/backend_vk.c')
-rw-r--r-- | src/backend_vk.c | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/src/backend_vk.c b/src/backend_vk.c index b6585d9..585eae8 100644 --- a/src/backend_vk.c +++ b/src/backend_vk.c @@ -1 +1,150 @@ +/** + * @file backend_vk.c + * @author Omniscient + * @brief Implements the RAL interface using Vulkan. + * + * @copyright Copyright (c) 2024 + */ + #include <celeritas.h> + +#ifdef GPU_VULKAN + +#include <vulkan/vk_platform.h> +#include <vulkan/vulkan.h> +#include <vulkan/vulkan_core.h> + +NAMESPACED_LOGGER(vulkan); + +#define MIN_API_VERSION VK_MAKE_API_VERSION(0, 1, 3, 0) + +struct gpu_swapchain { + VkSwapchainKHR handle; + VkExtent2D extents; +}; + +struct gpu_encoder {}; + +struct gpu_compute_encoder {}; + +typedef struct vk_pipeline { +} vk_pipeline; + +typedef struct vk_buffer { + VkBuffer handle; + VkDeviceMemory memory; + u64 size; +} vk_buffer; + +typedef struct vk_texture { +} vk_texture; + +TYPED_POOL(vk_buffer, buf); +TYPED_POOL(vk_texture, tex); +TYPED_POOL(vk_pipeline, pipeline); + +typedef struct vulkan_context { + GLFWwindow* window; + VkInstance instance; + VkSurfaceKHR surface; + VkDebugUtilsMessengerEXT vk_debugger; + VkPhysicalDevice gpu; + VkDevice device; + + /* pools */ + buf_pool bufpool; + tex_pool texpool; + pipeline_pool psopool; +} vulkan_context; + +/** globally available vulkan data */ +static vulkan_context ctx; + +// Forward declares + +void _init_device(); +void _init_swapchain(); +void _init_commands(); +void _init_sync_objects(); + +void ral_backend_init(const char* window_name, struct GLFWwindow* window) { + TRACE("loading vulkan backend"); + + // Application info + VkApplicationInfo app_info = { .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO }; + app_info.apiVersion = VK_API_VERSION_1_2; + app_info.pApplicationName = window_name; + app_info.applicationVersion = MIN_API_VERSION; + app_info.pEngineName = "Celeritas Engine"; + app_info.engineVersion = MIN_API_VERSION; + + // Instance setup + VkInstanceCreateInfo create_info = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO }; + create_info.pApplicationInfo = &app_info; + + // TODO: Extensions + + // Finally, create the Instance + VkResult res = vkCreateInstance(&create_info, NULL, &ctx.instance); + if (res != VK_SUCCESS) { + ERROR("vkCreateInstance failed with result: %u", res); + // TODO: change function sig to return bool + exit(1); + } + + INFO("successfully initialised Vulkan RAL backend"); +} + +void ral_backend_shutdown() {} +void ral_backend_resize_framebuffer(int width, int height) {} + +buf_handle ral_buffer_create(u64 size, const void* data) { + buf_handle handle; + vk_buffer* buffer = buf_pool_alloc(&ctx.bufpool, &handle); + + VkBufferCreateInfo buffer_info = { .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; + buffer_info.size = size; + // Add all the flags and leave optimisation up to the driver and the GPU + buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | + VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT | + VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; + buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE; + + vkCreateBuffer(ctx.device, &buffer_info, NULL, &buffer->handle); + + VkMemoryRequirements mem_reqs; + vkGetBufferMemoryRequirements(ctx.device, buffer->handle, &mem_reqs); + + return handle; +} + +void ral_buffer_destroy(buf_handle handle); +void ral_buffer_upload_data(buf_handle, u64 size, const void* data); + +tex_handle ral_texture_create(texture_desc desc, bool create_view, const void* data); +tex_handle ral_texture_load_from_file(const char* filepath) {} +void ral_texture_destroy(tex_handle handle); + +gpu_encoder* ral_render_encoder(render_pass_desc rpass_desc) { return NULL; } + +void ral_encoder_finish(gpu_encoder* enc) {} +void ral_encoder_submit(gpu_encoder* enc) {} +void ral_encoder_finish_and_submit(gpu_encoder* enc) {} + +pipeline_handle ral_gfx_pipeline_create(gfx_pipeline_desc desc) {} +void ral_gfx_pipeline_destroy(pipeline_handle handle) {} + +void ral_encode_bind_pipeline(gpu_encoder* enc, pipeline_handle pipeline) {} +void ral_set_default_settings(gpu_encoder* enc) {} +void ral_encode_set_vertex_buf(gpu_encoder* enc, buf_handle vbuf) {} +void ral_encode_set_index_buf(gpu_encoder* enc, buf_handle ibuf) {} +void ral_encode_set_texture(gpu_encoder* enc, tex_handle texture, u32 slot) {} +void ral_bind_buffer(gpu_encoder* enc, buf_handle, u32 index) {} +void ral_encode_draw_tris(gpu_encoder* enc, size_t start, size_t count) {} + +void ral_frame_start() {} + +void ral_frame_draw(scoped_draw_commands draw_fn) {} +void ral_frame_end() {} + +#endif |