146 lines
3.7 KiB
C
146 lines
3.7 KiB
C
/**
|
|
* @file graphics_context.h
|
|
* @author Piotr Krygier (everyonecancode@gmail.com)
|
|
* @brief Graphics context for Red Scarf Engine
|
|
* @version 0.1
|
|
* @date 2024-05-22
|
|
*
|
|
* @copyright Copyright (c) 2024
|
|
*
|
|
*/
|
|
|
|
#ifndef GRAPHICS_CONTEXT_H
|
|
#define GRAPHICS_CONTEXT_H
|
|
|
|
#include "vulkan/vulkan_core.h"
|
|
#include "vulkan_commons.h"
|
|
#include "utilities/linked_list.h"
|
|
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include <vulkan/vulkan.h>
|
|
#include <vk_mem_alloc.h>
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#define MAX_MESH_NUMBER 256
|
|
#define MAX_INSTANCE_NUMBER 256
|
|
|
|
/**
|
|
* @brief Maximum number of textures that can be loaded
|
|
*
|
|
*/
|
|
#define RSE_MAX_IMAGE_COUNT 256
|
|
|
|
#define MAX_DESCRIPTOR_SETS 16
|
|
#define MAX_PIPELINE_COUNT 16
|
|
#define MAX_DESCRIPTOR_POOLS_COUNT (16)
|
|
|
|
RSE_LINKED_LIST_DEFINE(shader_module_list_t, VkShaderModule);
|
|
|
|
/**
|
|
* @brief Represents data within vertex or instance buffer.
|
|
*
|
|
*/
|
|
struct buffer_data_t
|
|
{
|
|
size_t count;
|
|
size_t buffer_offset;
|
|
};
|
|
|
|
/**
|
|
* @brief Wrapper for Vulkan buffer. Holds allocation data;
|
|
*
|
|
*/
|
|
struct vulkan_image_t
|
|
{
|
|
uint8_t id_taken;
|
|
size_t allocated_size;
|
|
VkImage image;
|
|
VkImageView image_view;
|
|
VmaAllocation allocation;
|
|
VmaAllocationInfo allocation_info;
|
|
};
|
|
|
|
/**
|
|
* @brief Represents instance of a mesh and shows where it's located in instance buffer
|
|
*
|
|
*/
|
|
struct internal_instance_data_t
|
|
{
|
|
uint8_t instance_taken;
|
|
uint8_t instance_buffer_offset;
|
|
};
|
|
|
|
/**
|
|
* @brief Mesh data, with unique id and dynamic arrays holding vertices and indices.
|
|
* One mesh can have multiple instances.
|
|
*
|
|
*/
|
|
struct mesh_t
|
|
{
|
|
uint8_t is_mesh_id_taken;
|
|
uint32_t unique_id;
|
|
uint8_t instance_count;
|
|
struct buffer_data_t vertex_data;
|
|
struct buffer_data_t index_data;
|
|
struct internal_instance_data_t instance_data[MAX_INSTANCE_NUMBER];
|
|
};
|
|
|
|
struct pipeline_internal_t
|
|
{
|
|
size_t pipelines_count;
|
|
size_t descriptor_set_layouts_count;
|
|
VkPipeline pipelines[MAX_PIPELINE_COUNT];
|
|
VkPipelineLayout pipeline_layout;
|
|
};
|
|
|
|
struct graphics_context_t
|
|
{
|
|
GLFWwindow* window_handle;
|
|
VkDevice device;
|
|
VkSurfaceKHR surface;
|
|
VkInstance instance;
|
|
VkQueue graphics_queue;
|
|
VmaAllocator allocator;
|
|
VkSampler sampler;
|
|
VkPhysicalDevice physical_device;
|
|
VkCommandPool command_pool;
|
|
VkCommandBuffer command_buffers[SWAP_BUFFER_COUNT];
|
|
VkDescriptorSet descriptor_sets[SWAP_BUFFER_COUNT];
|
|
VkDescriptorSet descriptor_sets_bindless;
|
|
VkImageView* swapchain_image_views;
|
|
VkExtent2D swapchain_extent;
|
|
VkRenderPass render_pass;
|
|
VkFramebuffer* swapchain_framebuffers;
|
|
VkSwapchainKHR swapchain;
|
|
uint32_t swapchain_images_count;
|
|
VkImage* swapchain_images;
|
|
VkQueue present_queue;
|
|
VkSemaphore image_available_semaphores[SWAP_BUFFER_COUNT];
|
|
VkSemaphore render_finished_semaphores[SWAP_BUFFER_COUNT];
|
|
VkFence in_flight_fences[SWAP_BUFFER_COUNT];
|
|
VkDescriptorPool descriptor_pool;
|
|
VkDescriptorPool descriptor_pool_bindless;
|
|
struct pipeline_internal_t pipelines_data;
|
|
#ifndef NDEBUG
|
|
VkDebugUtilsMessengerEXT debug_messenger;
|
|
#endif // !NDEBUG
|
|
bool is_framebuffer_resized; //TODO: Consider moving this to heap
|
|
uint32_t current_frame;
|
|
uint32_t queue_family_indices[1];
|
|
struct vulkan_buffer_t uniform_buffers[SWAP_BUFFER_COUNT];
|
|
/* GPU visible buffer */
|
|
size_t vertex_offset;
|
|
struct vulkan_buffer_t vertex_buffer;
|
|
struct vulkan_buffer_t index_buffer;
|
|
struct vulkan_buffer_t instance_buffers[MAX_MESH_NUMBER];
|
|
struct vulkan_buffer_t draw_indirect_command_buffer;
|
|
struct vulkan_image_t texture_images[RSE_MAX_IMAGE_COUNT];
|
|
struct vulkan_image_t depth_image;
|
|
struct vulkan_image_t color_image;
|
|
struct shader_module_list_t* shader_modules;
|
|
struct mesh_t meshes[MAX_MESH_NUMBER];
|
|
};
|
|
|
|
#endif /* GRAPHICS_CONTEXT_H */
|