From 93c8d40b39fe55a626e66d412450fb4cca1f993b Mon Sep 17 00:00:00 2001 From: omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sat, 27 Apr 2024 17:07:03 +1000 Subject: scene thoughts --- src/scene.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/scene.h (limited to 'src/scene.h') diff --git a/src/scene.h b/src/scene.h new file mode 100644 index 0000000..2cc4d8a --- /dev/null +++ b/src/scene.h @@ -0,0 +1,30 @@ +/** + * @file scene.h + * @author your name (you@domain.com) + * @brief + * @version 0.1 + * @date 2024-04-27 + * + * @copyright Copyright (c) 2024 + * + */ +#include "defines.h" +#include "types.h" + +typedef struct scene { + // directional_light dir_light; + // point_light point_lights[4]; + // size_t n_point_lights; +} scene; + +bool scene_add_directional_light(scene* s /* TODO */); +bool scene_add_point_light(scene* s /* TODO */); + +// There can only be one heightmap terrain at a time right now. +bool scene_add_heightmap(scene* s /* TODO */); +bool scene_delete_heightmap(scene* s); + +bool scene_add_model(scene *s, model_handle model); +void scene_remove_model(scene *s, model_handle model); + +// TODO: functions to load and save scenes from disk \ No newline at end of file -- cgit v1.2.3-70-g09d2 From aed7d1b7ac340c19656059c9cbd94aff40952f83 Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Fri, 3 May 2024 21:37:51 +1000 Subject: create logical device --- src/defines.h | 4 +- src/renderer/backends/backend_dx11.h | 1 - src/renderer/backends/backend_vulkan.c | 237 +++++++++++++++++++++++++++++---- src/renderer/backends/vulkan_helpers.h | 42 +++++- src/renderer/bind_group_layouts.h | 4 +- src/renderer/cleanroom/types.h | 2 +- src/renderer/immediate.h | 6 +- src/renderer/ral.h | 7 +- src/renderer/ral_types.h | 14 +- src/renderer/render.h | 2 +- src/renderer/render_types.h | 16 +-- src/renderer/renderpasses.h | 7 +- src/scene.h | 10 +- src/std/buf.h | 6 +- src/std/mem.c | 9 +- src/std/mem.h | 7 + src/systems/terrain.h | 2 +- xmake.lua | 2 +- 18 files changed, 299 insertions(+), 79 deletions(-) (limited to 'src/scene.h') diff --git a/src/defines.h b/src/defines.h index ec526e0..4b6f8c7 100644 --- a/src/defines.h +++ b/src/defines.h @@ -66,8 +66,8 @@ Renderer backend defines: // Platform will inform renderer backend (unless user overrides) #if defined(CEL_PLATFORM_LINUX) -#define CEL_REND_BACKEND_OPENGL 1 -// #define CEL_REND_BACKEND_VULKAN 1 +// #define CEL_REND_BACKEND_OPENGL 1 +#define CEL_REND_BACKEND_VULKAN 1 #endif #if defined(CEL_PLATFORM_WINDOWS) diff --git a/src/renderer/backends/backend_dx11.h b/src/renderer/backends/backend_dx11.h index 8e3a513..53738aa 100644 --- a/src/renderer/backends/backend_dx11.h +++ b/src/renderer/backends/backend_dx11.h @@ -8,7 +8,6 @@ // typedef struct gpu_swapchain gpu_swapchain; typedef struct gpu_device { - // VkPhysicalDevice physical_device; // VkDevice logical_device; // VkPhysicalDeviceProperties properties; diff --git a/src/renderer/backends/backend_vulkan.c b/src/renderer/backends/backend_vulkan.c index ae857a0..c21a6b9 100644 --- a/src/renderer/backends/backend_vulkan.c +++ b/src/renderer/backends/backend_vulkan.c @@ -5,6 +5,7 @@ #include #include "backend_vulkan.h" +#include "maths_types.h" #include "mem.h" #include "vulkan_helpers.h" @@ -31,6 +32,8 @@ typedef struct vulkan_context { u32 screen_width; u32 screen_height; + + VkDebugUtilsMessengerEXT vk_debugger; } vulkan_context; static vulkan_context context; @@ -39,6 +42,13 @@ static vulkan_context context; /** @brief Enumerates and selects the most appropriate graphics device */ bool select_physical_device(gpu_device* out_device); + +bool is_physical_device_suitable(VkPhysicalDevice device); + +queue_family_indices find_queue_families(VkPhysicalDevice device); + +bool create_logical_device(gpu_device* out_device); + /** @brief Helper function for creating array of all extensions we want */ cstr_darray* get_all_extensions(); @@ -49,7 +59,7 @@ bool gpu_backend_init(const char* window_name, GLFWwindow* window) { // Create an allocator size_t temp_arena_size = 1024 * 1024; - arena_create(malloc(temp_arena_size), temp_arena_size); + context.temp_arena = arena_create(malloc(temp_arena_size), temp_arena_size); // Setup Vulkan instance VkApplicationInfo app_info = { VK_STRUCTURE_TYPE_APPLICATION_INFO }; @@ -63,20 +73,61 @@ bool gpu_backend_init(const char* window_name, GLFWwindow* window) { create_info.pApplicationInfo = &app_info; // Extensions - // FIXME: Use my own extension choices - // cstr_darray* required_extensions = cstr_darray_new(2); + cstr_darray* required_extensions = cstr_darray_new(2); // cstr_darray_push(required_extensions, VK_KHR_SURFACE_EXTENSION_NAME); - // create_info.enabledExtensionCount = cstr_darray_len(required_extensions); - // create_info.ppEnabledExtensionNames = required_extensions->data; + uint32_t count; const char** extensions = glfwGetRequiredInstanceExtensions(&count); - create_info.enabledExtensionCount = count; - create_info.ppEnabledExtensionNames = extensions; + for (u32 i = 0; i < count; i++) { + cstr_darray_push(required_extensions, extensions[i]); + } + + cstr_darray_push(required_extensions, VK_EXT_DEBUG_UTILS_EXTENSION_NAME); + + DEBUG("Required extensions:"); + for (u32 i = 0; i < cstr_darray_len(required_extensions); i++) { + DEBUG(" %s", required_extensions->data[i]); + } + + create_info.enabledExtensionCount = cstr_darray_len(required_extensions); + create_info.ppEnabledExtensionNames = required_extensions->data; // TODO: Validation layers create_info.enabledLayerCount = 0; create_info.ppEnabledLayerNames = NULL; + INFO("Validation layers enabled"); + cstr_darray* desired_validation_layers = cstr_darray_new(1); + cstr_darray_push(desired_validation_layers, "VK_LAYER_KHRONOS_validation"); + + u32 n_available_layers = 0; + VK_CHECK(vkEnumerateInstanceLayerProperties(&n_available_layers, 0)); + TRACE("%d available layers", n_available_layers); + VkLayerProperties* available_layers = + arena_alloc(&context.temp_arena, n_available_layers * sizeof(VkLayerProperties)); + VK_CHECK(vkEnumerateInstanceLayerProperties(&n_available_layers, available_layers)); + + for (int i = 0; i < cstr_darray_len(desired_validation_layers); i++) { + // look through layers to make sure we can find the ones we want + bool found = false; + for (int j = 0; j < n_available_layers; j++) { + if (str8_equals(str8_cstr_view(desired_validation_layers->data[i]), + str8_cstr_view(available_layers[j].layerName))) { + found = true; + TRACE("Found layer %s", desired_validation_layers->data[i]); + break; + } + } + + if (!found) { + FATAL("Required validation is missing %s", desired_validation_layers->data[i]); + return false; + } + } + INFO("All validation layers are present"); + create_info.enabledLayerCount = cstr_darray_len(desired_validation_layers); + create_info.ppEnabledLayerNames = desired_validation_layers->data; + VkResult result = vkCreateInstance(&create_info, NULL, &context.instance); if (result != VK_SUCCESS) { ERROR("vkCreateInstance failed with result: %u", result); @@ -84,6 +135,25 @@ bool gpu_backend_init(const char* window_name, GLFWwindow* window) { } TRACE("Vulkan Instance created"); + DEBUG("Creating Vulkan debugger"); + u32 log_severity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT | + VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT; + VkDebugUtilsMessengerCreateInfoEXT debug_create_info = { + VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT + }; + debug_create_info.messageSeverity = log_severity; + debug_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | + VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT | + VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT; + debug_create_info.pfnUserCallback = vk_debug_callback; + + PFN_vkCreateDebugUtilsMessengerEXT func = + (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(context.instance, + "vkCreateDebugUtilsMessengerEXT"); + assert(func); + VK_CHECK(func(context.instance, &debug_create_info, context.allocator, &context.vk_debugger)); + DEBUG("Vulkan Debugger created"); + // Surface creation VkSurfaceKHR surface; VK_CHECK(glfwCreateWindowSurface(context.instance, window, NULL, &surface)); @@ -96,39 +166,43 @@ bool gpu_backend_init(const char* window_name, GLFWwindow* window) { void gpu_backend_shutdown() { arena_free_storage(&context.temp_arena); } bool gpu_device_create(gpu_device* out_device) { + // First things first store this poitner from the renderer + context.device = out_device; + + arena_save savept = arena_savepoint(&context.temp_arena); // Physical device if (!select_physical_device(out_device)) { return false; } TRACE("Physical device selected"); - // Features - VkPhysicalDeviceFeatures device_features = { 0 }; - device_features.samplerAnisotropy = VK_TRUE; // request anistrophy - // Logical device - VkDeviceQueueCreateInfo queue_create_info[2]; - //.. - VkDeviceCreateInfo device_create_info = { VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO }; - device_create_info.queueCreateInfoCount = VULKAN_QUEUES_COUNT; - device_create_info.pQueueCreateInfos = queue_create_info; - device_create_info.pEnabledFeatures = &device_features; - device_create_info.enabledExtensionCount = 1; - const char* extension_names = VK_KHR_SWAPCHAIN_EXTENSION_NAME; - device_create_info.ppEnabledExtensionNames = &extension_names; - - VkResult result = vkCreateDevice(out_device->physical_device, &device_create_info, - context.allocator, &out_device->logical_device); - if (result != VK_SUCCESS) { - FATAL("Error creating logical device with status %u\n", result); - exit(1); - } - TRACE("Logical device created"); + create_logical_device(out_device); + // VkDeviceQueueCreateInfo queue_create_info = {}; + + // queue_family_indices indices = find_queue_families(context.device->physical_device); + // //.. + // VkDeviceCreateInfo device_create_info = { VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO }; + // device_create_info.queueCreateInfoCount = VULKAN_QUEUES_COUNT; + // device_create_info.pQueueCreateInfos = queue_create_info; + // device_create_info.pEnabledFeatures = &device_features; + // device_create_info.enabledExtensionCount = 1; + // const char* extension_names = VK_KHR_SWAPCHAIN_EXTENSION_NAME; + // device_create_info.ppEnabledExtensionNames = &extension_names; + + // VkResult result = vkCreateDevice(out_device->physical_device, &device_create_info, + // context.allocator, &out_device->logical_device); + // if (result != VK_SUCCESS) { + // FATAL("Error creating logical device with status %u\n", result); + // exit(1); + // } + // TRACE("Logical device created"); // Queues // Create the command pool + arena_rewind(savept); // Free any temp data return true; } @@ -316,4 +390,109 @@ inline void encode_draw_indexed(gpu_cmd_encoder* encoder, u64 index_count) { vkCmdDrawIndexed(encoder->cmd_buffer, index_count, 1, 0, 0, 0); } -bool select_physical_device(gpu_device* out_device) {} \ No newline at end of file +bool select_physical_device(gpu_device* out_device) { + u32 physical_device_count = 0; + VK_CHECK(vkEnumeratePhysicalDevices(context.instance, &physical_device_count, 0)); + if (physical_device_count == 0) { + FATAL("No devices that support vulkan were found"); + return false; + } + TRACE("Number of devices found %d", physical_device_count); + + VkPhysicalDevice* physical_devices = + arena_alloc(&context.temp_arena, physical_device_count * sizeof(VkPhysicalDevice)); + VK_CHECK(vkEnumeratePhysicalDevices(context.instance, &physical_device_count, physical_devices)); + + bool found = false; + for (u32 device_i = 0; device_i < physical_device_count; device_i++) { + if (is_physical_device_suitable(physical_devices[device_i])) { + out_device->physical_device = physical_devices[device_i]; + found = true; + break; + } + } + + if (!found) { + FATAL("Couldn't find a suitable physical device"); + return false; + } + + return true; +} + +bool is_physical_device_suitable(VkPhysicalDevice device) { + VkPhysicalDeviceProperties properties; + vkGetPhysicalDeviceProperties(device, &properties); + + VkPhysicalDeviceFeatures features; + vkGetPhysicalDeviceFeatures(device, &features); + + VkPhysicalDeviceMemoryProperties memory; + vkGetPhysicalDeviceMemoryProperties(device, &memory); + + // TODO: Check against these device properties + + queue_family_indices indices = find_queue_families(device); + + return indices.has_graphics; +} + +queue_family_indices find_queue_families(VkPhysicalDevice device) { + queue_family_indices indices = { 0 }; + + u32 queue_family_count = 0; + vkGetPhysicalDeviceQueueFamilyProperties(device, &queue_family_count, 0); + + VkQueueFamilyProperties* queue_families = + arena_alloc(&context.temp_arena, queue_family_count * sizeof(VkQueueFamilyProperties)); + vkGetPhysicalDeviceQueueFamilyProperties(device, &queue_family_count, queue_families); + + for (u32 queue_i = 0; queue_i < queue_family_count; queue_i++) { + // Graphics queue + if (queue_families[queue_i].queueFlags & VK_QUEUE_GRAPHICS_BIT) { + indices.graphics_queue_index = queue_i; + indices.has_graphics = true; + } + } + + return indices; +} + +bool create_logical_device(gpu_device* out_device) { + queue_family_indices indices = find_queue_families(out_device->physical_device); + + f32 prio_one = 1.0; + VkDeviceQueueCreateInfo queue_create_info = { VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO }; + queue_create_info.queueFamilyIndex = indices.graphics_queue_index; + queue_create_info.queueCount = 1; + queue_create_info.pQueuePriorities = &prio_one; + queue_create_info.flags = 0; + queue_create_info.pNext = 0; + + // Features + VkPhysicalDeviceFeatures device_features = { 0 }; + device_features.samplerAnisotropy = VK_TRUE; // request anistrophy + + // Device itself + VkDeviceCreateInfo device_create_info = { VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO }; + device_create_info.queueCreateInfoCount = 1; + device_create_info.pQueueCreateInfos = &queue_create_info; + device_create_info.pEnabledFeatures = &device_features; + device_create_info.enabledExtensionCount = 1; + const char* extension_names = VK_KHR_SWAPCHAIN_EXTENSION_NAME; + device_create_info.ppEnabledExtensionNames = &extension_names; + + // deprecated + device_create_info.enabledLayerCount = 0; + device_create_info.ppEnabledLayerNames = 0; + + VkResult result = vkCreateDevice(context.device->physical_device, &device_create_info, + context.allocator, &context.device->logical_device); + if (result != VK_SUCCESS) { + printf("error creating logical device with status %u\n", result); + ERROR_EXIT("Unable to create vulkan logical device. Exiting.."); + } + TRACE("Logical device created"); + + return true; +} \ No newline at end of file diff --git a/src/renderer/backends/vulkan_helpers.h b/src/renderer/backends/vulkan_helpers.h index 4bd02f1..baff4e7 100644 --- a/src/renderer/backends/vulkan_helpers.h +++ b/src/renderer/backends/vulkan_helpers.h @@ -20,16 +20,27 @@ static void plat_get_required_extension_names(cstr_darray* extensions) { } // TODO(omni): port to using internal assert functions -#define VK_CHECK(vulkan_expr) \ - do { \ - VkResult res = vulkan_expr; \ - if (res != VK_SUCCESS) { \ +#define VK_CHECK(vulkan_expr) \ + do { \ + VkResult res = vulkan_expr; \ + if (res != VK_SUCCESS) { \ ERROR_EXIT("Vulkan error: %u", res); \ - } \ + } \ } while (0) // TODO: typedef struct vk_debugger {} vk_debugger; +typedef struct queue_family_indices { + u32 graphics_queue_index; + u32 present_queue_index; + u32 compute_queue_index; + u32 transfer_queue_index; + bool has_graphics; + bool has_present; + bool has_compute; + bool has_transfer; +} queue_family_indices; + typedef struct vulkan_physical_device_requirements { bool graphics; bool present; @@ -168,4 +179,25 @@ static bool physical_device_meets_requirements( } return false; +} + +VKAPI_ATTR VkBool32 VKAPI_CALL vk_debug_callback( + VkDebugUtilsMessageSeverityFlagBitsEXT severity, VkDebugUtilsMessageTypeFlagsEXT flags, + const VkDebugUtilsMessengerCallbackDataEXT* callback_data, void* user_data) { + switch (severity) { + default: + case VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT: + ERROR("%s", callback_data->pMessage); + break; + case VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT: + WARN("%s", callback_data->pMessage); + break; + case VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT: + INFO("%s", callback_data->pMessage); + break; + case VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT: + TRACE("%s", callback_data->pMessage); + break; + } + return VK_FALSE; } \ No newline at end of file diff --git a/src/renderer/bind_group_layouts.h b/src/renderer/bind_group_layouts.h index d163fab..d2571ef 100644 --- a/src/renderer/bind_group_layouts.h +++ b/src/renderer/bind_group_layouts.h @@ -4,9 +4,9 @@ * @brief Common bindgroups (descriptor set layouts) * @version 0.1 * @date 2024-04-28 - * + * * @copyright Copyright (c) 2024 - * + * */ #pragma once #include "defines.h" diff --git a/src/renderer/cleanroom/types.h b/src/renderer/cleanroom/types.h index 7360ebe..6686be5 100644 --- a/src/renderer/cleanroom/types.h +++ b/src/renderer/cleanroom/types.h @@ -2,5 +2,5 @@ #include "darray.h" #include "defines.h" #include "maths_types.h" -#include "str.h" #include "render_types.h" +#include "str.h" diff --git a/src/renderer/immediate.h b/src/renderer/immediate.h index 6d93c53..b9d7c61 100644 --- a/src/renderer/immediate.h +++ b/src/renderer/immediate.h @@ -14,7 +14,7 @@ void imm_draw_camera_frustum(); // const char* model_filepath); // tracks internally whether the model is loaded // static void imm_draw_model(const char* model_filepath) { - // check that model is loaded - // if not loaded, load model and upload to gpu - LRU cache for models - // else submit draw call +// check that model is loaded +// if not loaded, load model and upload to gpu - LRU cache for models +// else submit draw call // } \ No newline at end of file diff --git a/src/renderer/ral.h b/src/renderer/ral.h index 8e49dbe..7c143f2 100644 --- a/src/renderer/ral.h +++ b/src/renderer/ral.h @@ -11,10 +11,10 @@ */ #pragma once -#include "ral_types.h" +#include "buf.h" #include "defines.h" +#include "ral_types.h" #include "str.h" -#include "buf.h" // Unrelated forward declares typedef struct arena arena; @@ -60,7 +60,6 @@ struct graphics_pipeline_desc { }; typedef struct gpu_renderpass_desc { - } gpu_renderpass_desc; // --- Lifecycle functions @@ -97,7 +96,7 @@ void buffer_upload_bytes(buffer_handle gpu_buf, bytebuffer cpu_buf, u64 offset, void encode_bind_pipeline(gpu_cmd_encoder* encoder, pipeline_kind kind, gpu_pipeline* pipeline); void encode_set_vertex_buffer(gpu_cmd_encoder* encoder, buffer_handle buf); void encode_set_index_buffer(gpu_cmd_encoder* encoder, buffer_handle buf); -void encode_set_bind_group(); // TODO +void encode_set_bind_group(); // TODO void encode_draw(gpu_cmd_encoder* encoder); void encode_draw_indexed(gpu_cmd_encoder* encoder, u64 index_count); diff --git a/src/renderer/ral_types.h b/src/renderer/ral_types.h index d6c5865..ae54b53 100644 --- a/src/renderer/ral_types.h +++ b/src/renderer/ral_types.h @@ -1,18 +1,18 @@ /** * @file ral_types.h * @author your name (you@domain.com) - * @brief + * @brief * @version 0.1 * @date 2024-04-27 - * + * * @copyright Copyright (c) 2024 - * + * */ #pragma once +#include "darray.h" #include "defines.h" #include "maths_types.h" -#include "darray.h" #ifndef RENDERER_TYPED_HANDLES CORE_DEFINE_HANDLE(buffer_handle); @@ -99,11 +99,7 @@ KITC_DECL_TYPED_ARRAY(u32) #define TYPED_VERTEX_ARRAY #endif -typedef enum gpu_cull_mode { - CULL_BACK_FACE, - CULL_FRONT_FACE, - CULL_COUNT -} gpu_cull_mode; +typedef enum gpu_cull_mode { CULL_BACK_FACE, CULL_FRONT_FACE, CULL_COUNT } gpu_cull_mode; // ? How to tie together materials and shaders diff --git a/src/renderer/render.h b/src/renderer/render.h index a9370e0..e6dd8b8 100644 --- a/src/renderer/render.h +++ b/src/renderer/render.h @@ -10,8 +10,8 @@ */ #pragma once -#include "render_types.h" #include "ral_types.h" +#include "render_types.h" bool renderer_init(renderer* ren); void renderer_shutdown(renderer* ren); diff --git a/src/renderer/render_types.h b/src/renderer/render_types.h index 4866ef4..a5c0c1a 100644 --- a/src/renderer/render_types.h +++ b/src/renderer/render_types.h @@ -1,21 +1,21 @@ /** * @file render_types.h * @author your name (you@domain.com) - * @brief + * @brief * @version 0.1 * @date 2024-04-27 - * + * * @copyright Copyright (c) 2024 - * + * */ #pragma once -#include "ral_types.h" #include "ral.h" +#include "ral_types.h" #if defined(CEL_PLATFORM_WINDOWS) // #include "backend_dx11.h" -#include "backend_vulkan.h" #endif +#include "backend_vulkan.h" struct GLFWwindow; @@ -37,7 +37,7 @@ typedef struct renderer { typedef struct geometry_data { vertex_format format; - vertex_darray* vertices; // TODO: make it not a pointer + vertex_darray* vertices; // TODO: make it not a pointer bool has_indices; u32_darray indices; vec3 colour; /** Optional: set vertex colours */ @@ -66,8 +66,8 @@ typedef struct model { typedef struct texture { u32 texture_id; char name[256]; - void *image_data; - void *backend_data; + void* image_data; + void* backend_data; u32 width; u32 height; u8 channel_count; diff --git a/src/renderer/renderpasses.h b/src/renderer/renderpasses.h index 67badaa..91970d8 100644 --- a/src/renderer/renderpasses.h +++ b/src/renderer/renderpasses.h @@ -4,9 +4,9 @@ * @brief Built-in renderpasses to the engine * @version 0.1 * @date 2024-04-28 - * + * * @copyright Copyright (c) 2024 - * + * */ #pragma once #include "maths_types.h" @@ -25,7 +25,8 @@ typedef struct render_entity { // Don't need to pass in *anything*. gpu_renderpass* renderpass_blinn_phong_create(); -void renderpass_blinn_phong_execute(gpu_renderpass* pass, render_entity* entities, size_t entity_count); +void renderpass_blinn_phong_execute(gpu_renderpass* pass, render_entity* entities, + size_t entity_count); gpu_renderpass* renderpass_shadows_create(); void renderpass_shadows_execute(gpu_renderpass* pass, render_entity* entities, size_t entity_count); \ No newline at end of file diff --git a/src/scene.h b/src/scene.h index 2cc4d8a..6cac061 100644 --- a/src/scene.h +++ b/src/scene.h @@ -1,12 +1,12 @@ /** * @file scene.h * @author your name (you@domain.com) - * @brief + * @brief * @version 0.1 * @date 2024-04-27 - * + * * @copyright Copyright (c) 2024 - * + * */ #include "defines.h" #include "types.h" @@ -24,7 +24,7 @@ bool scene_add_point_light(scene* s /* TODO */); bool scene_add_heightmap(scene* s /* TODO */); bool scene_delete_heightmap(scene* s); -bool scene_add_model(scene *s, model_handle model); -void scene_remove_model(scene *s, model_handle model); +bool scene_add_model(scene* s, model_handle model); +void scene_remove_model(scene* s, model_handle model); // TODO: functions to load and save scenes from disk \ No newline at end of file diff --git a/src/std/buf.h b/src/std/buf.h index b0f8b85..de093ec 100644 --- a/src/std/buf.h +++ b/src/std/buf.h @@ -1,12 +1,12 @@ /** * @file buf.h * @author your name (you@domain.com) - * @brief + * @brief * @version 0.1 * @date 2024-04-28 - * + * * @copyright Copyright (c) 2024 - * + * */ #pragma once #include "defines.h" diff --git a/src/std/mem.c b/src/std/mem.c index 5468898..4886d72 100644 --- a/src/std/mem.c +++ b/src/std/mem.c @@ -31,4 +31,11 @@ void arena_free_all(arena* a) { a->curr = a->begin; // pop everything at once and reset to the start. } -void arena_free_storage(arena* a) { free(a->begin); } \ No newline at end of file +void arena_free_storage(arena* a) { free(a->begin); } + +arena_save arena_savepoint(arena* a) { + arena_save savept = { .arena = a, .savepoint = a->curr }; + return savept; +} + +void arena_rewind(arena_save savepoint) { savepoint.arena->curr = savepoint.savepoint; } \ No newline at end of file diff --git a/src/std/mem.h b/src/std/mem.h index 2f92894..bbfb852 100644 --- a/src/std/mem.h +++ b/src/std/mem.h @@ -18,9 +18,16 @@ typedef struct arena { char* end; } arena; +typedef struct arena_save { + arena* arena; + char* savepoint; +} arena_save; + arena arena_create(void* backing_buffer, size_t capacity); void* arena_alloc(arena* a, size_t size); void* arena_alloc_align(arena* a, size_t size, size_t align); void arena_free_all(arena* a); void arena_free_storage(arena* a); +arena_save arena_savepoint(arena* a); +void arena_rewind(arena_save savepoint); // TODO: arena_resize \ No newline at end of file diff --git a/src/systems/terrain.h b/src/systems/terrain.h index 3d6f1c1..a8bff17 100644 --- a/src/systems/terrain.h +++ b/src/systems/terrain.h @@ -29,7 +29,7 @@ typedef struct heightmap { typedef struct terrain_state { arena terrain_allocator; - heightmap* heightmap; // NULL = no heightmap + heightmap* heightmap; // NULL = no heightmap } terrain_state; bool terrain_system_init(terrain_state* state); diff --git a/xmake.lua b/xmake.lua index ab6a7a6..949dd76 100644 --- a/xmake.lua +++ b/xmake.lua @@ -1,6 +1,6 @@ set_project("celeritas") set_version("0.1.0") -set_config("cc", "clang-cl") +set_config("cc", "gcc") add_rules("mode.debug", "mode.release") -- we have two modes: debug & release -- cgit v1.2.3-70-g09d2 From f7944239b793d1d5c49336856965d3a793f99316 Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Fri, 10 May 2024 13:24:05 +1000 Subject: make core a static and add a default scene to it --- examples/triangle/ex_triangle.c | 14 ++++++---- src/core.c | 35 +++++++++++------------ src/core.h | 16 +++++++---- src/maths/maths_types.h | 3 +- src/renderer/archive/render_types.h | 30 ++++++++++---------- src/renderer/render_types.h | 33 ++++++++++++++++++---- src/scene.c | 56 +++++++++++++++++++++++++++++++++++++ src/scene.h | 42 ++++++++++++++++++++-------- 8 files changed, 165 insertions(+), 64 deletions(-) create mode 100644 src/scene.c (limited to 'src/scene.h') diff --git a/examples/triangle/ex_triangle.c b/examples/triangle/ex_triangle.c index 3a9b7db..97d6484 100644 --- a/examples/triangle/ex_triangle.c +++ b/examples/triangle/ex_triangle.c @@ -12,8 +12,10 @@ // Example setting up a renderer +extern core g_core; + int main() { - core* core = core_bringup(); + core_bringup(); arena scratch = arena_create(malloc(1024 * 1024), 1024 * 1024); gpu_renderpass_desc pass_description = {}; @@ -44,11 +46,11 @@ int main() { gpu_pipeline* gfx_pipeline = gpu_graphics_pipeline_create(pipeline_description); // Main loop - while (!should_exit(core)) { + while (!should_exit(&g_core)) { glfwPollEvents(); - input_update(&core->input); + input_update(&g_core.input); - render_frame_begin(&core->renderer); + render_frame_begin(&g_core.renderer); static f64 x = 0.0; x += 0.01; @@ -74,11 +76,11 @@ int main() { // Submit gpu_backend_end_frame(); - render_frame_end(&core->renderer); + render_frame_end(&g_core.renderer); // glfwSwapBuffers(core->renderer.window); } - renderer_shutdown(&core->renderer); + renderer_shutdown(&g_core.renderer); return 0; } diff --git a/src/core.c b/src/core.c index cd6ff88..3596e01 100644 --- a/src/core.c +++ b/src/core.c @@ -13,25 +13,28 @@ #define SCR_WIDTH 1000 #define SCR_HEIGHT 1000 -core* core_bringup() { +core g_core; /** @brief global `core` that other files can use */ + +inline core* get_global_core() { return &g_core; } + +void core_bringup() { INFO("Initiate Core bringup"); - core* c = malloc(sizeof(core)); renderer_config conf = { .window_name = { "Celeritas Engine Core" }, .scr_width = SCR_WIDTH, .scr_height = SCR_HEIGHT, .clear_colour = (vec3){ .08, .08, .1 } }; - c->renderer.config = conf; - c->renderer.backend_context = NULL; + g_core.renderer.config = conf; + g_core.renderer.backend_context = NULL; // threadpool_create(&c->threadpool, 6, 256); // threadpool_set_ctx(&c->threadpool, c); // Gives the threadpool access to the core // initialise all subsystems - if (!renderer_init(&c->renderer)) { + if (!renderer_init(&g_core.renderer)) { // FATAL("Failed to start renderer"); ERROR_EXIT("Failed to start renderer\n"); } - if (!input_system_init(&c->input, c->renderer.window)) { + if (!input_system_init(&g_core.input, g_core.renderer.window)) { // the input system needs the glfw window which is created by the renderer // hence the order here is important FATAL("Failed to start input system"); @@ -48,26 +51,22 @@ core* core_bringup() { } */ - c->models = model_darray_new(10); - - return c; + g_core.models = model_darray_new(10); } #include -#include "input.h" -#include "render.h" bool should_window_close(core* core) { glfwWindowShouldClose(core->renderer.window); } -void core_input_update(core* core) { input_update(&core->input); } +void core_input_update() { input_update(&g_core.input); } void core_frame_begin(core* core) { render_frame_begin(&core->renderer); } void core_frame_end(core* core) { render_frame_end(&core->renderer); } -void core_shutdown(core* core) { +void core_shutdown() { // threadpool_destroy(&core->threadpool); - input_system_shutdown(&core->input); - renderer_shutdown(&core->renderer); + input_system_shutdown(&g_core.input); + renderer_shutdown(&g_core.renderer); } -bool should_exit(core* core) { - return key_just_released(KEYCODE_ESCAPE) || glfwWindowShouldClose(core->renderer.window); -} \ No newline at end of file +bool should_exit() { + return key_just_released(KEYCODE_ESCAPE) || glfwWindowShouldClose(g_core.renderer.window); +} diff --git a/src/core.h b/src/core.h index a122448..ec8cde9 100644 --- a/src/core.h +++ b/src/core.h @@ -1,9 +1,8 @@ #pragma once -#include "defines.h" #include "input.h" -#include "ral.h" #include "screenspace.h" +#include "scene.h" #include "terrain.h" #include "text.h" // #include "threadpool.h" @@ -19,12 +18,17 @@ typedef struct core { terrain_state terrain; screenspace_state screenspace; // data storage + scene default_scene; model_darray* models; } core; +core* get_global_core(); + // --- Lifecycle -core* core_bringup(); -void core_shutdown(core* core); -bool should_exit(core* core); -void core_input_update(core* core); +/** @brief Throws error if the core cannot be instantiated */ +void core_bringup(); +void core_shutdown(); +bool should_exit(); + +void core_input_update(); diff --git a/src/maths/maths_types.h b/src/maths/maths_types.h index aa86eb0..5ef09db 100644 --- a/src/maths/maths_types.h +++ b/src/maths/maths_types.h @@ -61,6 +61,7 @@ typedef struct transform { f32 scale; bool is_dirty; } transform; +typedef transform transform3d; typedef struct vec4i { i32 x, y, z, w; @@ -99,4 +100,4 @@ typedef struct vec2 f32x2; #define f32x2(x, y) ((f32x2){ x, y }) typedef struct vec3 f32x3; -#define f32x3(x, y, z) ((f32x3){ x, y, z }) \ No newline at end of file +#define f32x3(x, y, z) ((f32x3){ x, y, z }) diff --git a/src/renderer/archive/render_types.h b/src/renderer/archive/render_types.h index 13a6651..f5ea986 100644 --- a/src/renderer/archive/render_types.h +++ b/src/renderer/archive/render_types.h @@ -94,20 +94,20 @@ KITC_DECL_TYPED_ARRAY(animation_clip) // creates "material_darray" #endif // // lights -// typedef struct point_light { -// vec3 position; -// f32 constant, linear, quadratic; -// vec3 ambient; -// vec3 diffuse; -// vec3 specular; -// } point_light; - -// typedef struct directional_light { -// vec3 direction; -// vec3 ambient; -// vec3 diffuse; -// vec3 specular; -// } directional_light; +typedef struct point_light { + vec3 position; + f32 constant, linear, quadratic; + vec3 ambient; + vec3 diffuse; + vec3 specular; +} point_light; + +typedef struct directional_light { + vec3 direction; + vec3 ambient; + vec3 diffuse; + vec3 specular; +} directional_light; // void point_light_upload_uniforms(shader shader, point_light *light, char index); // void dir_light_upload_uniforms(shader shader, directional_light *light); @@ -207,4 +207,4 @@ typedef struct model { // // typedef enum pipeline_kind { // // GRAPHICS, // // COMPUTE, -// // } pipeline_kind; \ No newline at end of file +// // } pipeline_kind; diff --git a/src/renderer/render_types.h b/src/renderer/render_types.h index 08b9e94..3763967 100644 --- a/src/renderer/render_types.h +++ b/src/renderer/render_types.h @@ -116,10 +116,31 @@ KITC_DECL_TYPED_ARRAY(animation_clip) /** @brief Describes all the data required for the renderer to start executing draws */ typedef struct render_entity { - buffer_handle index_buffer; - u32 index_count; - u32 index_offset; - buffer_handle vertex_buffer; - material* material; + /* buffer_handle index_buffer; */ + /* u32 index_count; */ + /* u32 index_offset; */ + /* buffer_handle vertex_buffer; */ + model_handle model; transform tf; -} render_entity; \ No newline at end of file +} render_entity; + +#ifndef TYPED_RENDER_ENTITY_ARRAY +KITC_DECL_TYPED_ARRAY(render_entity) +#define TYPED_RENDER_ENTITY_ARRAY +#endif + +// --- Lights +typedef struct point_light { + vec3 position; + f32 constant, linear, quadratic; + vec3 ambient; + vec3 diffuse; + vec3 specular; +} point_light; + +typedef struct directional_light { + vec3 direction; + vec3 ambient; + vec3 diffuse; + vec3 specular; +} directional_light; diff --git a/src/scene.c b/src/scene.c new file mode 100644 index 0000000..d9fea05 --- /dev/null +++ b/src/scene.c @@ -0,0 +1,56 @@ +#include "scene.h" +#include "core.h" +#include "log.h" +#include "maths.h" +#include "render_types.h" + +extern core g_core; + +void scene_init(scene *s) { + memset(s, 0, sizeof(scene)); + s->renderables = render_entity_darray_new(10); + // default camera position - moved slightly along Z axis looking at 0,0,0 + vec3 cam_pos = vec3_create(0, 0, -5); + s->camera = camera_create(cam_pos, vec3_negate(cam_pos), VEC3_Y, deg_to_rad(45.0)); +} +void scene_free(scene *s) { render_entity_darray_free(s->renderables); } + +void scene_set_dir_light(directional_light light) { g_core.default_scene.dir_light = light; } +void scene_add_point_light(point_light light) { + scene s = g_core.default_scene; + if (s.point_lights_count == 4) { + WARN("Already 4 point lights, we can't add more."); + } else { + s.point_lights[s.point_lights_count] = light; + s.point_lights_count++; + } +} +void scene_add_model(model_handle model, transform3d transform) { + render_entity renderable = { .model = model, .tf = transform }; + render_entity_darray_push(g_core.default_scene.renderables, renderable); +} + +bool scene_remove_model(model_handle model) { + scene s = g_core.default_scene; + for (u32 i = 0; i <= s.renderables->len; i++) { + if (s.renderables->data[i].model.raw == model.raw) { + // TODO: add remove function to darray + } + } + return true; +} + +void scene_set_model_transform(model_handle model, transform3d new_transform) { + scene s = g_core.default_scene; + for (u32 i = 0; i <= s.renderables->len; i++) { + if (s.renderables->data[i].model.raw == model.raw) { + s.renderables->data[i].tf = new_transform; + } + } +} + +void scene_set_camera(vec3 pos, vec3 front) { + scene s = g_core.default_scene; + s.camera.position = pos; + s.camera.front = front; +} diff --git a/src/scene.h b/src/scene.h index 6cac061..5399ab7 100644 --- a/src/scene.h +++ b/src/scene.h @@ -8,23 +8,41 @@ * @copyright Copyright (c) 2024 * */ +#pragma once +#include "camera.h" #include "defines.h" -#include "types.h" +#include "render_types.h" +#include "maths_types.h" typedef struct scene { - // directional_light dir_light; - // point_light point_lights[4]; - // size_t n_point_lights; + // camera + camera camera; + // lights + directional_light dir_light; + point_light point_lights[4]; + size_t point_lights_count; + // geometry + render_entity_darray* renderables; + // TODO: tree - transform_hierarchy } scene; -bool scene_add_directional_light(scene* s /* TODO */); -bool scene_add_point_light(scene* s /* TODO */); +void scene_init(scene* s); +void scene_free(scene* s); -// There can only be one heightmap terrain at a time right now. -bool scene_add_heightmap(scene* s /* TODO */); -bool scene_delete_heightmap(scene* s); +// Simplified API - no scene pointer; gets and sets global scene -bool scene_add_model(scene* s, model_handle model); -void scene_remove_model(scene* s, model_handle model); +// Add/Remove objects from the scene +void scene_set_dir_light(directional_light light); +void scene_add_point_light(point_light light); +void scene_add_model(model_handle model, transform3d transform); +bool scene_remove_model(model_handle model); -// TODO: functions to load and save scenes from disk \ No newline at end of file +// Getter & Setters +void scene_set_model_transform(model_handle model, transform3d new_transform); +void scene_set_camera(vec3 pos, vec3 front); + +/* // There can only be one heightmap terrain at a time right now. */ +/* bool scene_add_heightmap(scene* s /\* TODO *\/); */ +/* bool scene_delete_heightmap(scene* s); */ + +// TODO: functions to load and save scenes from disk -- cgit v1.2.3-70-g09d2 From eedd332680dbdd367197616658903f00252f5a9c Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sat, 11 May 2024 00:22:13 +1000 Subject: upload data using staging buffer. use vertex buffer data --- assets/shaders/triangle.vert | 11 ++- examples/triangle/ex_triangle.c | 23 ++++- src/core.c | 9 +- src/core.h | 2 +- src/maths/maths.h | 5 +- src/renderer/backends/backend_vulkan.c | 170 +++++++++++++++++++++++++++++++-- src/renderer/backends/backend_vulkan.h | 8 +- src/renderer/ral.h | 7 +- src/renderer/ral_types.h | 22 ++++- src/renderer/render.c | 43 ++++----- src/scene.h | 2 +- 11 files changed, 250 insertions(+), 52 deletions(-) (limited to 'src/scene.h') diff --git a/assets/shaders/triangle.vert b/assets/shaders/triangle.vert index ee3675d..b8c8f63 100644 --- a/assets/shaders/triangle.vert +++ b/assets/shaders/triangle.vert @@ -1,12 +1,13 @@ #version 450 -vec2 positions[3] = vec2[](vec2(-0.5, 0.5), vec2(0.5, 0.5), vec2(0.0, -0.5)); - -vec3 colors[3] = vec3[](vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0)); +layout(location = 0) in vec2 inPos; +layout(location = 1) in vec3 inColor; +// layout(location = 1) in vec3 inNormal; +// layout(location = 2) in vec2 inTexCoords; layout(location = 0) out vec3 fragColor; void main() { - gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0); - fragColor = colors[gl_VertexIndex]; + gl_Position = vec4(inPos, 0.0, 1.0); + fragColor = inColor; } diff --git a/examples/triangle/ex_triangle.c b/examples/triangle/ex_triangle.c index 7b8a1d0..c4f90f2 100644 --- a/examples/triangle/ex_triangle.c +++ b/examples/triangle/ex_triangle.c @@ -1,6 +1,7 @@ #include #include "backend_vulkan.h" +#include "buf.h" #include "camera.h" #include "core.h" #include "file.h" @@ -8,12 +9,20 @@ #include "maths.h" #include "mem.h" #include "ral.h" +#include "ral_types.h" #include "render.h" // Example setting up a renderer extern core g_core; +const custom_vertex vertices[] = { + (custom_vertex){ .pos = vec2(-0.5, 0.5), .color = vec3(0.0, 0.0, 1.0) }, + (custom_vertex){ .pos = vec2(0.5, 0.5), .color = vec3(0.0, 1.0, 0.0) }, + (custom_vertex){ .pos = vec2(0.0, -0.5), .color = vec3(1.0, 0.0, 0.0) }, +}; +_Static_assert(sizeof(vertices) == (5 * 3 * sizeof(float)), ""); + int main() { core_bringup(); arena scratch = arena_create(malloc(1024 * 1024), 1024 * 1024); @@ -47,12 +56,19 @@ int main() { }; gpu_pipeline* gfx_pipeline = gpu_graphics_pipeline_create(pipeline_description); + buffer_handle triangle_vert_buf = gpu_buffer_create( + 3 * sizeof(custom_vertex), CEL_BUFFER_VERTEX, CEL_BUFFER_FLAG_GPU, + vertices); // gpu_buffer_create(3 * sizeof(custom_vertex), CEL_BUFFER_VERTEX); + /* buffer_upload_bytes(triangle_vert_buf, (bytebuffer){ .buf = vertices, .size = sizeof(vertices) + * }, */ + /* 0, sizeof(vertices)); */ + // Main loop while (!should_exit(&g_core)) { glfwPollEvents(); input_update(&g_core.input); - render_frame_begin(&g_core.renderer); + // render_frame_begin(&g_core.renderer); static f64 x = 0.0; x += 0.01; @@ -68,7 +84,8 @@ int main() { encode_set_default_settings(enc); // Record draw calls - gpu_temp_draw(); + encode_set_vertex_buffer(enc, triangle_vert_buf); + gpu_temp_draw(3); // End recording gpu_cmd_encoder_end_render(enc); @@ -78,7 +95,7 @@ int main() { // Submit gpu_backend_end_frame(); - render_frame_end(&g_core.renderer); + // render_frame_end(&g_core.renderer); // glfwSwapBuffers(core->renderer.window); } diff --git a/src/core.c b/src/core.c index 26e7613..84c9cae 100644 --- a/src/core.c +++ b/src/core.c @@ -59,9 +59,7 @@ void core_bringup() { #include /* bool should_window_close(core* core) { glfwWindowShouldClose(core->renderer.window); } */ -void core_input_update() { - input_update(&g_core.input); -} +void core_input_update() { input_update(&g_core.input); } void core_frame_begin(core* core) { render_frame_begin(&core->renderer); } void core_frame_end(core* core) { render_frame_end(&core->renderer); } @@ -76,7 +74,8 @@ bool should_exit() { } void frame_begin() { - glfwPollEvents(); - render_frame_begin(&g_core.renderer); } + glfwPollEvents(); + render_frame_begin(&g_core.renderer); +} void frame_draw() {} void frame_end() { render_frame_end(&g_core.renderer); } diff --git a/src/core.h b/src/core.h index db711d0..1031868 100644 --- a/src/core.h +++ b/src/core.h @@ -1,8 +1,8 @@ #pragma once #include "input.h" -#include "screenspace.h" #include "scene.h" +#include "screenspace.h" #include "terrain.h" #include "text.h" // #include "threadpool.h" diff --git a/src/maths/maths.h b/src/maths/maths.h index 88a5215..217f2e0 100644 --- a/src/maths/maths.h +++ b/src/maths/maths.h @@ -21,7 +21,7 @@ // Dimension 3 static inline vec3 vec3_create(f32 x, f32 y, f32 z) { return (vec3){ x, y, z }; } -#define vec3(x, y, z) (vec3_create(x, y, z)) +#define vec3(x, y, z) ((vec3){ x, y, z }) static inline vec3 vec3_add(vec3 a, vec3 b) { return (vec3){ a.x + b.x, a.y + b.y, a.z + b.z }; } static inline vec3 vec3_sub(vec3 a, vec3 b) { return (vec3){ a.x - b.x, a.y - b.y, a.z - b.z }; } static inline vec3 vec3_mult(vec3 a, f32 s) { return (vec3){ a.x * s, a.y * s, a.z * s }; } @@ -53,6 +53,7 @@ static inline void print_vec3(vec3 v) { printf("{ x: %f, y: %f, z: %f )\n", v.x, // TODO: Dimension 2 static inline vec2 vec2_create(f32 x, f32 y) { return (vec2){ x, y }; } +#define vec2(x, y) ((vec2){ x, y }) static inline vec2 vec2_div(vec2 a, f32 s) { return (vec2){ a.x / s, a.y / s }; } // TODO: Dimension 4 @@ -320,4 +321,4 @@ static inline mat4 transform_to_mat(transform *tf) { _Static_assert(alignof(vec3) == 4, "vec3 is 4 byte aligned"); _Static_assert(sizeof(vec3) == 12, "vec3 is 12 bytes so has no padding"); -_Static_assert(alignof(vec4) == 4, "vec4 is 4 byte aligned"); \ No newline at end of file +_Static_assert(alignof(vec4) == 4, "vec4 is 4 byte aligned"); diff --git a/src/renderer/backends/backend_vulkan.c b/src/renderer/backends/backend_vulkan.c index 5390f1f..e30d3c1 100644 --- a/src/renderer/backends/backend_vulkan.c +++ b/src/renderer/backends/backend_vulkan.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -8,8 +9,10 @@ #include #include "backend_vulkan.h" +#include "buf.h" #include "maths_types.h" #include "mem.h" +#include "ral_types.h" #include "str.h" #include "vulkan_helpers.h" @@ -54,6 +57,10 @@ typedef struct vulkan_context { bool is_resizing; GLFWwindow* window; + // Storage + gpu_buffer buffers[1024]; + size_t buffer_count; + VkDebugUtilsMessengerEXT vk_debugger; } vulkan_context; @@ -366,17 +373,31 @@ gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc descrip VkPipelineShaderStageCreateInfo shader_stages[2] = { vert_shader_stage_info, frag_shader_stage_info }; - // Vertex Input - // TODO: Attributes + VkVertexInputAttributeDescription attribute_descs[2]; + attribute_descs[0].binding = 0; + attribute_descs[0].location = 0; + attribute_descs[0].format = VK_FORMAT_R32G32_SFLOAT; + attribute_descs[0].offset = offsetof(custom_vertex, pos); + + attribute_descs[1].binding = 0; + attribute_descs[1].location = 1; + attribute_descs[1].format = VK_FORMAT_R32G32B32_SFLOAT; + attribute_descs[1].offset = offsetof(custom_vertex, color); + + // Vertex input + VkVertexInputBindingDescription binding_desc; + binding_desc.binding = 0; + binding_desc.stride = sizeof(custom_vertex); + binding_desc.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; VkPipelineVertexInputStateCreateInfo vertex_input_info = { VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO }; - vertex_input_info.vertexBindingDescriptionCount = 0; - vertex_input_info.pVertexBindingDescriptions = NULL; - vertex_input_info.vertexAttributeDescriptionCount = 0; - vertex_input_info.pVertexAttributeDescriptions = NULL; + vertex_input_info.vertexBindingDescriptionCount = 1; + vertex_input_info.pVertexBindingDescriptions = &binding_desc; + vertex_input_info.vertexAttributeDescriptionCount = 2; + vertex_input_info.pVertexAttributeDescriptions = attribute_descs; // Input Assembly VkPipelineInputAssemblyStateCreateInfo input_assembly = { @@ -633,6 +654,10 @@ gpu_cmd_encoder gpu_cmd_encoder_create() { return encoder; } +void gpu_cmd_encoder_destroy(gpu_cmd_encoder* encoder) { + vkFreeCommandBuffers(context.device->logical_device, context.device->pool, 1, + &encoder->cmd_buffer); +} void gpu_cmd_encoder_begin(gpu_cmd_encoder encoder) { VkCommandBufferBeginInfo begin_info = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO }; @@ -674,6 +699,12 @@ gpu_cmd_buffer gpu_cmd_encoder_finish(gpu_cmd_encoder* encoder) { void encode_bind_pipeline(gpu_cmd_encoder* encoder, pipeline_kind kind, gpu_pipeline* pipeline) { vkCmdBindPipeline(encoder->cmd_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline->handle); } +void encode_set_vertex_buffer(gpu_cmd_encoder* encoder, buffer_handle buf) { + gpu_buffer buffer = context.buffers[buf.raw]; + VkBuffer vbs[] = { buffer.handle }; + VkDeviceSize offsets[] = { 0 }; + vkCmdBindVertexBuffers(encoder->cmd_buffer, 0, 1, vbs, offsets); +} // TEMP void encode_set_default_settings(gpu_cmd_encoder* encoder) { @@ -720,9 +751,9 @@ bool gpu_backend_begin_frame() { return true; } -void gpu_temp_draw() { +void gpu_temp_draw(size_t n_verts) { gpu_cmd_encoder* encoder = gpu_get_default_cmd_encoder(); // &context.main_cmd_buf; - vkCmdDraw(encoder->cmd_buffer, 3, 1, 0, 0); + vkCmdDraw(encoder->cmd_buffer, n_verts, 1, 0, 0); } void gpu_backend_end_frame() { @@ -971,3 +1002,126 @@ void create_sync_objects() { &context.in_flight_fences[i])); } } + +static i32 find_memory_index(u32 type_filter, u32 property_flags) { + VkPhysicalDeviceMemoryProperties memory_properties; + vkGetPhysicalDeviceMemoryProperties(context.device->physical_device, &memory_properties); + + for (u32 i = 0; i < memory_properties.memoryTypeCount; ++i) { + // Check each memory type to see if its bit is set to 1. + if (type_filter & (1 << i) && + (memory_properties.memoryTypes[i].propertyFlags & property_flags) == property_flags) { + return i; + } + } + + WARN("Unable to find suitable memory type!"); + return -1; +} + +buffer_handle gpu_buffer_create(u64 size, gpu_buffer_type buf_type, gpu_buffer_flags flags, + const void* data) { + VkBufferCreateInfo buffer_info = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; + buffer_info.size = size; + buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; + + switch (buf_type) { + case CEL_BUFFER_DEFAULT: + buffer_info.usage |= VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; + break; + case CEL_BUFFER_VERTEX: + buffer_info.usage |= VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; + break; + case CEL_BUFFER_COUNT: + WARN("Incorrect gpu_buffer_type provided. using default"); + break; + } + + buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE; + + // "allocating" the cpu-side buffer struct + gpu_buffer buffer; + buffer.size = size; + buffer_handle handle = { .raw = (u32)context.buffer_count }; + + VK_CHECK(vkCreateBuffer(context.device->logical_device, &buffer_info, context.allocator, + &buffer.handle)); + + VkMemoryRequirements requirements; + vkGetBufferMemoryRequirements(context.device->logical_device, buffer.handle, &requirements); + + // Just make them always need all of them for now + i32 memory_index = + find_memory_index(requirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | + VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | + VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); + + // Allocate the actual VRAM + VkMemoryAllocateInfo allocate_info = { VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO }; + allocate_info.allocationSize = requirements.size; + allocate_info.memoryTypeIndex = (u32)memory_index; + + vkAllocateMemory(context.device->logical_device, &allocate_info, context.allocator, + &buffer.memory); + vkBindBufferMemory(context.device->logical_device, buffer.handle, buffer.memory, 0); + + /* Now there are two options: + * 1. create CPU-accessible memory -> map memory -> memcpy -> unmap + * 2. use a staging buffer thats CPU-accessible and copy its contents to a + * GPU-only buffer + */ + + context.buffers[context.buffer_count] = buffer; + context.buffer_count++; + + if (data) { + if (flags & CEL_BUFFER_FLAG_CPU) { + buffer_upload_bytes(handle, (bytebuffer){ .buf = (u8*)data, .size = size }, 0, size); + } else if (flags & CEL_BUFFER_FLAG_GPU) { + TRACE("Uploading data to buffer using staging buffer"); + buffer_handle staging = + gpu_buffer_create(size, CEL_BUFFER_DEFAULT, CEL_BUFFER_FLAG_CPU, data); + gpu_cmd_encoder temp_encoder = gpu_cmd_encoder_create(); + gpu_cmd_encoder_begin(temp_encoder); + encode_buffer_copy(&temp_encoder, handle, 0, staging, 0, size); + gpu_cmd_buffer copy_cmd_buffer = gpu_cmd_encoder_finish(&temp_encoder); + + VkSubmitInfo submit_info = { VK_STRUCTURE_TYPE_SUBMIT_INFO }; + submit_info.commandBufferCount = 1; + submit_info.pCommandBuffers = &temp_encoder.cmd_buffer; + vkQueueSubmit(context.device->graphics_queue, 1, &submit_info, VK_NULL_HANDLE); + + vkQueueWaitIdle(context.device->graphics_queue); + gpu_cmd_encoder_destroy(&temp_encoder); + gpu_buffer_destroy(staging); + } + } + + return handle; +} + +void gpu_buffer_destroy(buffer_handle buffer) { + gpu_buffer b = context.buffers[buffer.raw]; + vkDestroyBuffer(context.device->logical_device, b.handle, context.allocator); + vkFreeMemory(context.device->logical_device, b.memory, context.allocator); +} + +// Upload data to a +void buffer_upload_bytes(buffer_handle gpu_buf, bytebuffer cpu_buf, u64 offset, u64 size) { + gpu_buffer buffer = context.buffers[gpu_buf.raw]; + void* data_ptr; + vkMapMemory(context.device->logical_device, buffer.memory, 0, size, 0, &data_ptr); + memcpy(data_ptr, cpu_buf.buf, size); + vkUnmapMemory(context.device->logical_device, buffer.memory); +} + +void encode_buffer_copy(gpu_cmd_encoder* encoder, buffer_handle src, u64 src_offset, + buffer_handle dst, u64 dst_offset, u64 copy_size) { + VkBufferCopy copy_region; + copy_region.srcOffset = src_offset; + copy_region.dstOffset = dst_offset; + copy_region.size = copy_size; + + vkCmdCopyBuffer(encoder->cmd_buffer, context.buffers[src.raw].handle, + context.buffers[dst.raw].handle, 1, ©_region); +} diff --git a/src/renderer/backends/backend_vulkan.h b/src/renderer/backends/backend_vulkan.h index 330ba18..0b0a492 100644 --- a/src/renderer/backends/backend_vulkan.h +++ b/src/renderer/backends/backend_vulkan.h @@ -75,4 +75,10 @@ typedef struct gpu_cmd_encoder { typedef struct gpu_cmd_buffer { VkCommandBuffer cmd_buffer; -} gpu_cmd_buffer; \ No newline at end of file +} gpu_cmd_buffer; + +typedef struct gpu_buffer { + VkBuffer handle; + VkDeviceMemory memory; + u64 size; +} gpu_buffer; diff --git a/src/renderer/ral.h b/src/renderer/ral.h index ec9793e..2fb0166 100644 --- a/src/renderer/ral.h +++ b/src/renderer/ral.h @@ -28,6 +28,7 @@ typedef struct gpu_pipeline gpu_pipeline; typedef struct gpu_renderpass gpu_renderpass; typedef struct gpu_cmd_encoder gpu_cmd_encoder; // Recording typedef struct gpu_cmd_buffer gpu_cmd_buffer; // Ready for submission +typedef struct gpu_buffer gpu_buffer; /** @brief A*/ // typedef struct gpu_bind_group @@ -85,6 +86,7 @@ bool gpu_swapchain_create(gpu_swapchain* out_swapchain); void gpu_swapchain_destroy(gpu_swapchain* swapchain); gpu_cmd_encoder gpu_cmd_encoder_create(); +void gpu_cmd_encoder_destroy(gpu_cmd_encoder* encoder); void gpu_cmd_encoder_begin(gpu_cmd_encoder encoder); void gpu_cmd_encoder_begin_render(gpu_cmd_encoder* encoder, gpu_renderpass* renderpass); void gpu_cmd_encoder_end_render(gpu_cmd_encoder* encoder); @@ -117,7 +119,8 @@ gpu_cmd_buffer gpu_cmd_encoder_finish(gpu_cmd_encoder* encoder); void gpu_queue_submit(gpu_cmd_buffer* buffer); // Buffers -void gpu_buffer_create(u64 size); +buffer_handle gpu_buffer_create(u64 size, gpu_buffer_type buf_type, gpu_buffer_flags flags, + const void* data); void gpu_buffer_destroy(buffer_handle buffer); void gpu_buffer_upload(); void gpu_buffer_bind(buffer_handle buffer); @@ -137,4 +140,4 @@ bytebuffer vertices_as_bytebuffer(arena* a, vertex_format format, vertex_darray* // TEMP -void gpu_temp_draw(); +void gpu_temp_draw(size_t n_verts); diff --git a/src/renderer/ral_types.h b/src/renderer/ral_types.h index ae54b53..7dd254e 100644 --- a/src/renderer/ral_types.h +++ b/src/renderer/ral_types.h @@ -55,6 +55,20 @@ typedef struct texture_desc { u32x2 extents; } texture_desc; +typedef enum gpu_buffer_type { + CEL_BUFFER_DEFAULT, // on Vulkan this would be a storage buffer? + CEL_BUFFER_VERTEX, + CEL_BUFFER_COUNT +} gpu_buffer_type; + +typedef enum gpu_buffer_flag { + CEL_BUFFER_FLAG_CPU = 1 << 0, + CEL_BUFFER_FLAG_GPU = 1 << 1, + CEL_BUFFER_FLAG_STORAGE = 1 << 2, + CEL_BUFFER_FLAG_COUNT +} gpu_buffer_flag; +typedef u32 gpu_buffer_flags; + typedef enum vertex_format { VERTEX_STATIC_3D, VERTEX_SPRITE, @@ -99,6 +113,12 @@ KITC_DECL_TYPED_ARRAY(u32) #define TYPED_VERTEX_ARRAY #endif +// TEMP +typedef struct custom_vertex { + vec2 pos; + vec3 color; +} custom_vertex; + typedef enum gpu_cull_mode { CULL_BACK_FACE, CULL_FRONT_FACE, CULL_COUNT } gpu_cull_mode; // ? How to tie together materials and shaders @@ -110,4 +130,4 @@ typedef enum gpu_cull_mode { CULL_BACK_FACE, CULL_FRONT_FACE, CULL_COUNT } gpu_c // 3 - you don't need to know how the renderer works at all // 2 - you need to know how the overall renderer is designed -// 1 - you need to understand graphics API specifics \ No newline at end of file +// 1 - you need to understand graphics API specifics diff --git a/src/renderer/render.c b/src/renderer/render.c index 10589e5..9f5b21a 100644 --- a/src/renderer/render.c +++ b/src/renderer/render.c @@ -55,7 +55,7 @@ bool renderer_init(renderer* ren) { // default_material_init(); // Create default rendering pipeline - default_pipelines_init(ren); + /* default_pipelines_init(ren); */ return true; } @@ -67,9 +67,6 @@ void renderer_shutdown(renderer* ren) { void default_pipelines_init(renderer* ren) { // Static opaque geometry - // graphics_pipeline_desc gfx = { - // }; - // ren->static_opaque_pipeline = gpu_graphics_pipeline_create(); arena scratch = arena_create(malloc(1024 * 1024), 1024 * 1024); gpu_renderpass_desc pass_description = {}; @@ -77,8 +74,8 @@ void default_pipelines_init(renderer* ren) { ren->default_renderpass = *renderpass; - str8 vert_path = str8lit("celeritas-core/build/linux/x86_64/debug/triangle.vert.spv"); - str8 frag_path = str8lit("celeritas-core/build/linux/x86_64/debug/triangle.frag.spv"); + str8 vert_path = str8lit("build/linux/x86_64/debug/triangle.vert.spv"); + str8 frag_path = str8lit("build/linux/x86_64/debug/triangle.frag.spv"); str8_opt vertex_shader = str8_from_file(&scratch, vert_path); str8_opt fragment_shader = str8_from_file(&scratch, frag_path); if (!vertex_shader.has_value || !fragment_shader.has_value) { @@ -104,28 +101,28 @@ void default_pipelines_init(renderer* ren) { } void render_frame_begin(renderer* ren) { - ren->frame_aborted = false; - if (!gpu_backend_begin_frame()) { - ren->frame_aborted = true; - return; - } - gpu_cmd_encoder* enc = gpu_get_default_cmd_encoder(); - // begin recording - gpu_cmd_encoder_begin(*enc); - gpu_cmd_encoder_begin_render(enc, &ren->default_renderpass); - encode_bind_pipeline(enc, PIPELINE_GRAPHICS, &ren->static_opaque_pipeline); - encode_set_default_settings(enc); + ren->frame_aborted = false; + if (!gpu_backend_begin_frame()) { + ren->frame_aborted = true; + return; + } + gpu_cmd_encoder* enc = gpu_get_default_cmd_encoder(); + // begin recording + gpu_cmd_encoder_begin(*enc); + gpu_cmd_encoder_begin_render(enc, &ren->default_renderpass); + encode_bind_pipeline(enc, PIPELINE_GRAPHICS, &ren->static_opaque_pipeline); + encode_set_default_settings(enc); } void render_frame_end(renderer* ren) { if (ren->frame_aborted) { return; } - gpu_temp_draw(); - gpu_cmd_encoder* enc = gpu_get_default_cmd_encoder(); - gpu_cmd_encoder_end_render(enc); - gpu_cmd_buffer buf = gpu_cmd_encoder_finish(enc); - gpu_queue_submit(&buf); - gpu_backend_end_frame(); + gpu_temp_draw(3); + gpu_cmd_encoder* enc = gpu_get_default_cmd_encoder(); + gpu_cmd_encoder_end_render(enc); + gpu_cmd_buffer buf = gpu_cmd_encoder_finish(enc); + gpu_queue_submit(&buf); + gpu_backend_end_frame(); } void render_frame_draw(renderer* ren) {} diff --git a/src/scene.h b/src/scene.h index 5399ab7..24429b3 100644 --- a/src/scene.h +++ b/src/scene.h @@ -11,8 +11,8 @@ #pragma once #include "camera.h" #include "defines.h" -#include "render_types.h" #include "maths_types.h" +#include "render_types.h" typedef struct scene { // camera -- cgit v1.2.3-70-g09d2 From fcaf838886efc3c12f60b47c021c6a85965eb6e9 Mon Sep 17 00:00:00 2001 From: Omniscient Date: Sat, 18 May 2024 14:15:23 +1000 Subject: initialise gpu resource pools in renderer --- assets/textures/texture.jpg | Bin 0 -> 77120 bytes scene.c | 1 + src/core.c | 4 +--- src/renderer/backends/backend_vulkan.c | 24 ++++++++++++++++++++++++ src/renderer/ral.c | 12 ++++++------ src/renderer/ral.h | 17 +++++++++++++++-- src/renderer/render.c | 19 ++++++++++++++----- src/renderer/render_types.h | 1 + src/scene.h | 6 ++++++ 9 files changed, 68 insertions(+), 16 deletions(-) create mode 100644 assets/textures/texture.jpg create mode 100644 scene.c (limited to 'src/scene.h') diff --git a/assets/textures/texture.jpg b/assets/textures/texture.jpg new file mode 100644 index 0000000..975e338 Binary files /dev/null and b/assets/textures/texture.jpg differ diff --git a/scene.c b/scene.c new file mode 100644 index 0000000..69a8b3c --- /dev/null +++ b/scene.c @@ -0,0 +1 @@ +#include "scene.h" diff --git a/src/core.c b/src/core.c index 83c7b6e..84c9cae 100644 --- a/src/core.c +++ b/src/core.c @@ -78,6 +78,4 @@ void frame_begin() { render_frame_begin(&g_core.renderer); } void frame_draw() {} -void frame_end() { - render_frame_end(&g_core.renderer); -} +void frame_end() { render_frame_end(&g_core.renderer); } diff --git a/src/renderer/backends/backend_vulkan.c b/src/renderer/backends/backend_vulkan.c index 37f9b59..61527ee 100644 --- a/src/renderer/backends/backend_vulkan.c +++ b/src/renderer/backends/backend_vulkan.c @@ -65,6 +65,7 @@ typedef struct vulkan_context { gpu_buffer buffers[1024]; size_t buffer_count; VkDescriptorSet_darray* free_set_queue; + struct resource_pools* resource_pools; VkDebugUtilsMessengerEXT vk_debugger; } vulkan_context; @@ -1360,6 +1361,17 @@ void encode_buffer_copy(gpu_cmd_encoder* encoder, buffer_handle src, u64 src_off context.buffers[dst.raw].handle, 1, ©_region); } +texture_handle gpu_texture_create(texture_desc desc, const void* data) { + VkDeviceSize image_size = desc.extents.x * desc.extents.y * 4; + + TRACE("Uploading pixel data to texture using staging buffer"); + // Create a staging buffer + buffer_handle staging = + gpu_buffer_create(image_size, CEL_BUFFER_DEFAULT, CEL_BUFFER_FLAG_CPU, NULL); + // Copy data into it + buffer_upload_bytes(staging, (bytebuffer){ .buf = (u8*)data, .size = image_size }, 0, image_size); +} + size_t vertex_attrib_size(vertex_attrib_type attr) { switch (attr) { case ATTR_F32: @@ -1392,3 +1404,15 @@ void vertex_desc_add(vertex_description* builder, const char* name, vertex_attri builder->attributes_count++; } + +/* TYPED_POOL(gpu_buffer, buffer); */ +/* TYPED_POOL(gpu_texture, texture); */ + +void resource_pools_init(arena* a, struct resource_pools* res_pools) { + buffer_pool buf_pool = buffer_pool_create(a, MAX_BUFFERS, sizeof(gpu_buffer)); + res_pools->buffers = buf_pool; + texture_pool tex_pool = texture_pool_create(a, MAX_TEXTURES, sizeof(gpu_texture)); + res_pools->textures = tex_pool; + + context.resource_pools = res_pools; +} diff --git a/src/renderer/ral.c b/src/renderer/ral.c index 25c2909..6a417dd 100644 --- a/src/renderer/ral.c +++ b/src/renderer/ral.c @@ -13,10 +13,10 @@ /* } buffer_handle; */ /* typedef struct gpu_buffer gpu_buffer; */ -TYPED_POOL(gpu_buffer, buffer); -TYPED_POOL(gpu_texture, texture); +/* TYPED_POOL(gpu_buffer, buffer); */ +/* TYPED_POOL(gpu_texture, texture); */ -struct resource_pools { - buffer_pool buffers; - texture_pool textures; -}; +/* struct resource_pools { */ +/* buffer_pool buffers; */ +/* texture_pool textures; */ +/* }; */ diff --git a/src/renderer/ral.h b/src/renderer/ral.h index 03bdeab..da9eb93 100644 --- a/src/renderer/ral.h +++ b/src/renderer/ral.h @@ -32,6 +32,8 @@ typedef struct gpu_cmd_buffer gpu_cmd_buffer; // Ready for submission typedef struct gpu_buffer gpu_buffer; #define MAX_SHADER_DATA_LAYOUTS 5 +#define MAX_BUFFERS 256 +#define MAX_TEXTURES 256 /** @brief A*/ // typedef struct gpu_bind_group @@ -41,7 +43,7 @@ typedef struct gpu_backend_pools { // pools for each gpu structure } gpu_backend_pools; -typedef struct resource_pools resource_pools; +/* typedef struct resource_pools resource_pools; */ typedef enum pipeline_kind { PIPELINE_GRAPHICS, @@ -144,7 +146,7 @@ void gpu_buffer_upload(); void gpu_buffer_bind(buffer_handle buffer); // Textures -void gpu_texture_create(); +texture_handle gpu_texture_create(texture_desc desc, const void* data); void gpu_texture_destroy(); void gpu_texture_upload(); @@ -161,3 +163,14 @@ void vertex_desc_add(vertex_description* builder, const char* name, vertex_attri // TEMP void gpu_temp_draw(size_t n_verts); + +TYPED_POOL(gpu_buffer, buffer); +TYPED_POOL(gpu_texture, texture); + +struct resource_pools { + buffer_pool buffers; + texture_pool textures; +}; + +// Must be implemented by backends +void resource_pools_init(arena* a, struct resource_pools* res_pools); diff --git a/src/renderer/render.c b/src/renderer/render.c index 6eacc0d..c83678b 100644 --- a/src/renderer/render.c +++ b/src/renderer/render.c @@ -3,6 +3,7 @@ #include "camera.h" #include "file.h" #include "log.h" +#include "mem.h" #include "ral.h" #include "ral_types.h" @@ -46,6 +47,10 @@ bool renderer_init(renderer* ren) { gpu_device_create(&ren->device); // TODO: handle errors gpu_swapchain_create(&ren->swapchain); + DEBUG("Initialise GPU resource pools"); + arena pool_arena = arena_create(malloc(1024 * 1024), 1024 * 1024); + resource_pools_init(&pool_arena, ren->resource_pools); + // ren->blinn_phong = // shader_create_separate("assets/shaders/blinn_phong.vert", // "assets/shaders/blinn_phong.frag"); @@ -56,7 +61,7 @@ bool renderer_init(renderer* ren) { // default_material_init(); // Create default rendering pipeline - default_pipelines_init(ren); + /* default_pipelines_init(ren); */ return true; } @@ -75,11 +80,15 @@ void default_pipelines_init(renderer* ren) { ren->default_renderpass = *renderpass; - // str8 vert_path = str8lit("build/linux/x86_64/debug/triangle.vert.spv"); - // str8 frag_path = str8lit("build/linux/x86_64/debug/triangle.frag.spv"); printf("Load shaders\n"); - str8 vert_path = str8lit("/home/void/code/celeritas-engine/celeritas-core/build/linux/x86_64/debug/triangle.vert.spv"); - str8 frag_path = str8lit("/home/void/code/celeritas-engine/celeritas-core/build/linux/x86_64/debug/triangle.frag.spv"); + str8 vert_path = str8lit("build/linux/x86_64/debug/triangle.vert.spv"); + str8 frag_path = str8lit("build/linux/x86_64/debug/triangle.frag.spv"); + /* str8 vert_path = + * str8lit("/home/void/code/celeritas-engine/celeritas-core/build/linux/x86_64/debug/triangle.vert.spv"); + */ + /* str8 frag_path = + * str8lit("/home/void/code/celeritas-engine/celeritas-core/build/linux/x86_64/debug/triangle.frag.spv"); + */ str8_opt vertex_shader = str8_from_file(&scratch, vert_path); str8_opt fragment_shader = str8_from_file(&scratch, frag_path); if (!vertex_shader.has_value || !fragment_shader.has_value) { diff --git a/src/renderer/render_types.h b/src/renderer/render_types.h index ac98627..bd9ef3c 100644 --- a/src/renderer/render_types.h +++ b/src/renderer/render_types.h @@ -35,6 +35,7 @@ typedef struct renderer { gpu_renderpass default_renderpass; gpu_pipeline static_opaque_pipeline; bool frame_aborted; + struct resource_pools* resource_pools; } renderer; typedef struct geometry_data { diff --git a/src/scene.h b/src/scene.h index 24429b3..5a85e2e 100644 --- a/src/scene.h +++ b/src/scene.h @@ -32,7 +32,13 @@ void scene_free(scene* s); // Simplified API - no scene pointer; gets and sets global scene // Add/Remove objects from the scene + /* vec3 direction; */ + /* vec3 ambient; */ + /* vec3 diffuse; */ + /* vec3 specular; */ void scene_set_dir_light(directional_light light); +void _scene_set_dir_light(vec3 ambient, vec3 diffuse, vec3 specular, vec3 direction); + void scene_add_point_light(point_light light); void scene_add_model(model_handle model, transform3d transform); bool scene_remove_model(model_handle model); -- cgit v1.2.3-70-g09d2 From ebee348781e68e61f97c31411512dc0aabab7acb Mon Sep 17 00:00:00 2001 From: Omniscient Date: Sun, 19 May 2024 00:04:31 +1000 Subject: chore: fmt --- examples/cube/ex_cube.c | 2 +- examples/triangle/ex_triangle.c | 6 +++--- src/renderer/backends/backend_vulkan.c | 15 ++++++++------- src/renderer/ral.h | 4 ++-- src/renderer/render.h | 10 +++++----- src/renderer/render_types.h | 4 ++-- src/scene.h | 8 ++++---- 7 files changed, 25 insertions(+), 24 deletions(-) (limited to 'src/scene.h') diff --git a/examples/cube/ex_cube.c b/examples/cube/ex_cube.c index 346dd10..80a4e26 100644 --- a/examples/cube/ex_cube.c +++ b/examples/cube/ex_cube.c @@ -48,7 +48,7 @@ int main() { vec3 camera_front = vec3_normalise(vec3_negate(camera_pos)); camera cam = camera_create(camera_pos, camera_front, VEC3_Y, deg_to_rad(45.0)); - vertex_description vertex_input = {.use_full_vertex_size = true}; + vertex_description vertex_input = { .use_full_vertex_size = true }; vertex_input.debug_label = "Standard Static 3D Vertex Format"; vertex_desc_add(&vertex_input, "inPosition", ATTR_F32x3); vertex_desc_add(&vertex_input, "inNormal", ATTR_F32x3); diff --git a/examples/triangle/ex_triangle.c b/examples/triangle/ex_triangle.c index 69c29ac..d57e224 100644 --- a/examples/triangle/ex_triangle.c +++ b/examples/triangle/ex_triangle.c @@ -21,13 +21,13 @@ const custom_vertex vertices[] = { (custom_vertex){ .pos = vec2(-0.5, 0.5), .color = vec3(0.0, 0.0, 1.0) }, (custom_vertex){ .pos = vec2(0.5, 0.5), .color = vec3(0.0, 1.0, 0.0) }, }; -const u32 indices[] = { 2, 1, 0, 1, 2, 3}; +const u32 indices[] = { 2, 1, 0, 1, 2, 3 }; int main() { core_bringup(); arena scratch = arena_create(malloc(1024 * 1024), 1024 * 1024); - vertex_description vertex_input = {.use_full_vertex_size=false}; + vertex_description vertex_input = { .use_full_vertex_size = false }; vertex_input.debug_label = "Hello"; vertex_desc_add(&vertex_input, "inPos", ATTR_F32x2); vertex_desc_add(&vertex_input, "inColor", ATTR_F32x3); @@ -64,7 +64,7 @@ int main() { // Load triangle vertex and index data buffer_handle triangle_vert_buf = - gpu_buffer_create(4 * sizeof(vertex) , CEL_BUFFER_VERTEX, CEL_BUFFER_FLAG_GPU, vertices); + gpu_buffer_create(4 * sizeof(vertex), CEL_BUFFER_VERTEX, CEL_BUFFER_FLAG_GPU, vertices); buffer_handle triangle_index_buf = gpu_buffer_create(sizeof(indices), CEL_BUFFER_INDEX, CEL_BUFFER_FLAG_GPU, indices); diff --git a/src/renderer/backends/backend_vulkan.c b/src/renderer/backends/backend_vulkan.c index ae49cc3..33e0860 100644 --- a/src/renderer/backends/backend_vulkan.c +++ b/src/renderer/backends/backend_vulkan.c @@ -425,7 +425,7 @@ gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc descrip attribute_descs[i].location = i; attribute_descs[i].format = format_from_vertex_attr(description.vertex_desc.attributes[i]); attribute_descs[i].offset = offset; - size_t this_offset = vertex_attrib_size(description.vertex_desc.attributes[i]); + size_t this_offset = vertex_attrib_size(description.vertex_desc.attributes[i]); printf("offset total %d this attr %ld\n", offset, this_offset); printf("sizeof vertex %ld\n", sizeof(vertex)); /* printf("%d \n", offsetof(vertex, static_3d)); */ @@ -446,7 +446,9 @@ gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc descrip // TODO: Generate this from descroiption now VkVertexInputBindingDescription binding_desc; binding_desc.binding = 0; - binding_desc.stride = description.vertex_desc.use_full_vertex_size ? sizeof(vertex) : description.vertex_desc.stride; + binding_desc.stride = description.vertex_desc.use_full_vertex_size + ? sizeof(vertex) + : description.vertex_desc.stride; binding_desc.inputRate = VK_VERTEX_INPUT_RATE_VERTEX; VkPipelineVertexInputStateCreateInfo vertex_input_info = { @@ -561,8 +563,8 @@ gpu_pipeline* gpu_graphics_pipeline_create(struct graphics_pipeline_desc descrip pipeline->desc_set_layouts = desc_set_layouts; pipeline->desc_set_layouts_count = description.data_layouts_count; if (description.data_layouts_count > 0) { - pipeline->uniform_pointers = - malloc(description.data_layouts_count * sizeof(desc_set_uniform_buffer)); + pipeline->uniform_pointers = + malloc(description.data_layouts_count * sizeof(desc_set_uniform_buffer)); } else { pipeline->uniform_pointers = NULL; } @@ -1349,8 +1351,7 @@ buffer_handle gpu_buffer_create(u64 size, gpu_buffer_type buf_type, gpu_buffer_f } void gpu_buffer_destroy(buffer_handle buffer) { - gpu_buffer* b = - buffer_pool_get(&context.resource_pools->buffers, buffer); + gpu_buffer* b = buffer_pool_get(&context.resource_pools->buffers, buffer); vkDestroyBuffer(context.device->logical_device, b->handle, context.allocator); vkFreeMemory(context.device->logical_device, b->memory, context.allocator); buffer_pool_dealloc(&context.resource_pools->buffers, buffer); @@ -1493,7 +1494,7 @@ texture_handle gpu_texture_create(texture_desc desc, const void* data) { vkBindImageMemory(context.device->logical_device, image, image_memory, 0); -gpu_buffer_destroy(staging); + gpu_buffer_destroy(staging); texture_handle handle; gpu_texture* texture = texture_pool_alloc(&context.resource_pools->textures, &handle); diff --git a/src/renderer/ral.h b/src/renderer/ral.h index 48be83a..a82a2ba 100644 --- a/src/renderer/ral.h +++ b/src/renderer/ral.h @@ -176,8 +176,8 @@ struct resource_pools { // Must be implemented by backends void resource_pools_init(arena* a, struct resource_pools* res_pools); -void copy_buffer_to_buffer_oneshot(buffer_handle src, u64 src_offset, buffer_handle dst, u64 dst_offset, - u64 copy_size); +void copy_buffer_to_buffer_oneshot(buffer_handle src, u64 src_offset, buffer_handle dst, + u64 dst_offset, u64 copy_size); void copy_buffer_to_image_oneshot(buffer_handle src, texture_handle dst); // --- Helpers diff --git a/src/renderer/render.h b/src/renderer/render.h index 9a7ff2f..c193ff9 100644 --- a/src/renderer/render.h +++ b/src/renderer/render.h @@ -37,11 +37,11 @@ texture_handle texture_create(const char* debug_name, texture_desc description, texture_data texture_data_load(const char* path, bool invert_y); /** - * @brief - * - * @param data - * @param free_on_upload frees the CPU-side pixel data stored in `data` - * @return texture_handle + * @brief + * + * @param data + * @param free_on_upload frees the CPU-side pixel data stored in `data` + * @return texture_handle */ texture_handle texture_data_upload(texture_data data, bool free_on_upload); diff --git a/src/renderer/render_types.h b/src/renderer/render_types.h index c6aec8e..cc5fd93 100644 --- a/src/renderer/render_types.h +++ b/src/renderer/render_types.h @@ -69,14 +69,14 @@ typedef struct model { u32 mesh_count; } model; -typedef struct texture {} texture; +typedef struct texture { +} texture; typedef struct texture_data { texture_desc description; void* image_data; } texture_data; - typedef struct blinn_phong_material { char name[256]; texture diffuse_texture; diff --git a/src/scene.h b/src/scene.h index 5a85e2e..5ac7542 100644 --- a/src/scene.h +++ b/src/scene.h @@ -32,10 +32,10 @@ void scene_free(scene* s); // Simplified API - no scene pointer; gets and sets global scene // Add/Remove objects from the scene - /* vec3 direction; */ - /* vec3 ambient; */ - /* vec3 diffuse; */ - /* vec3 specular; */ +/* vec3 direction; */ +/* vec3 ambient; */ +/* vec3 diffuse; */ +/* vec3 specular; */ void scene_set_dir_light(directional_light light); void _scene_set_dir_light(vec3 ambient, vec3 diffuse, vec3 specular, vec3 direction); -- cgit v1.2.3-70-g09d2