2024-10-21 12:52:45 -06:00
|
|
|
#ifndef GPU_H
|
|
|
|
#define GPU_H
|
2024-10-09 10:54:19 -06:00
|
|
|
|
|
|
|
#define VK_USE_PLATFORM_MACOS_MVK
|
2024-10-30 17:36:53 -06:00
|
|
|
#include "vulkan/vk_enum_string_helper.h"
|
2024-10-09 10:54:19 -06:00
|
|
|
#include "vulkan/vulkan_core.h"
|
|
|
|
|
|
|
|
#include "vk_mem_alloc.h"
|
|
|
|
|
|
|
|
#define GLFW_INCLUDE_VULKAN
|
|
|
|
#include <GLFW/glfw3.h>
|
2024-10-14 21:29:35 -06:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
2024-10-09 10:54:19 -06:00
|
|
|
#define GLFW_EXPOSE_NATIVE_COCOA
|
2024-10-14 21:29:35 -06:00
|
|
|
#endif
|
|
|
|
|
2024-10-09 10:54:19 -06:00
|
|
|
#include <GLFW/glfw3native.h>
|
|
|
|
|
|
|
|
#define GLM_FORCE_RADIANS
|
|
|
|
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
|
|
|
#include <cglm/types.h>
|
|
|
|
#include <cglm/mat4.h>
|
|
|
|
#include <cglm/vec3.h>
|
|
|
|
#include <cglm/affine.h>
|
|
|
|
#include <cglm/quat.h>
|
|
|
|
#include <cglm/cam.h>
|
|
|
|
|
2024-11-04 00:24:42 -07:00
|
|
|
#include <math.h>
|
|
|
|
|
2024-10-26 12:43:07 -06:00
|
|
|
#define MAX_FRAMES_IN_FLIGHT 2
|
|
|
|
#define WINDOW_MIN_WIDTH 800
|
|
|
|
#define WINDOW_MIN_HEIGHT 600
|
2024-11-04 00:24:42 -07:00
|
|
|
#define PERSPECTIVE_FOVY (-M_PI/3)
|
|
|
|
#define PERSPECTIVE_NEARZ 0.01
|
|
|
|
#define PERSPECTIVE_FARZ 1000
|
2024-10-26 12:43:07 -06:00
|
|
|
|
2024-10-30 17:36:53 -06:00
|
|
|
#define VK_RESULT(x) {\
|
|
|
|
result = x;\
|
|
|
|
if(result != VK_SUCCESS) {\
|
2024-10-30 21:24:03 -06:00
|
|
|
fprintf(stderr, "%s:%d %s failed with %s\n", __FILE__, __LINE__, #x, string_VkResult(result));\
|
2024-10-30 17:36:53 -06:00
|
|
|
return x;\
|
|
|
|
}\
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct ComputePipelineStruct {
|
|
|
|
VkPipelineLayout layout;
|
|
|
|
VkPipeline pipeline;
|
|
|
|
} ComputePipeline;
|
|
|
|
|
|
|
|
typedef struct GraphicsPipelineStruct {
|
|
|
|
VkPipelineLayout layout;
|
|
|
|
VkPipeline pipeline;
|
|
|
|
} GraphicsPipeline;
|
|
|
|
|
|
|
|
typedef struct DrawCommandStruct {
|
|
|
|
uint32_t vertex_count;
|
|
|
|
uint32_t instance_count;
|
|
|
|
uint32_t first_vertex;
|
|
|
|
uint32_t first_instance;
|
|
|
|
} DrawCommand;
|
|
|
|
|
|
|
|
typedef struct DispatchCommandStruct {
|
|
|
|
uint32_t x;
|
|
|
|
uint32_t y;
|
|
|
|
uint32_t z;
|
|
|
|
} DispatchCommand;
|
|
|
|
|
2024-10-24 20:49:59 -06:00
|
|
|
typedef struct GPUQueueStruct {
|
2024-10-21 12:52:45 -06:00
|
|
|
VkQueue handle;
|
|
|
|
uint32_t family;
|
|
|
|
uint32_t index;
|
2024-10-24 20:49:59 -06:00
|
|
|
} GPUQueue;
|
2024-10-21 12:52:45 -06:00
|
|
|
|
2024-10-09 10:54:19 -06:00
|
|
|
typedef struct SwapchainDetailsStruct {
|
|
|
|
VkSurfaceCapabilitiesKHR capabilities;
|
|
|
|
|
|
|
|
VkSurfaceFormatKHR* formats;
|
|
|
|
uint32_t formats_count;
|
|
|
|
|
|
|
|
VkPresentModeKHR* present_modes;
|
|
|
|
uint32_t present_modes_count;
|
|
|
|
} SwapchainDetails;
|
|
|
|
|
2024-10-28 13:44:34 -06:00
|
|
|
typedef struct TransferInfoStruct {
|
2024-10-28 14:48:36 -06:00
|
|
|
VkDeviceSize offset;
|
2024-10-28 13:44:34 -06:00
|
|
|
VkDeviceSize size;
|
2024-10-28 22:07:13 -06:00
|
|
|
VkBuffer buffer;
|
2024-10-28 13:44:34 -06:00
|
|
|
} TransferInfo;
|
2024-10-26 18:25:44 -06:00
|
|
|
|
2024-10-29 13:48:06 -06:00
|
|
|
typedef struct TransferBufferStruct {
|
|
|
|
VkBuffer buffer;
|
|
|
|
VmaAllocation memory;
|
|
|
|
void* mapped;
|
|
|
|
size_t written;
|
|
|
|
|
|
|
|
size_t max_size;
|
|
|
|
size_t max_count;
|
|
|
|
|
|
|
|
TransferInfo* infos;
|
|
|
|
uint32_t count;
|
|
|
|
} TransferBuffer;
|
|
|
|
|
2024-10-26 12:43:07 -06:00
|
|
|
typedef struct FrameContextStruct {
|
|
|
|
VkFence ready;
|
|
|
|
VkSemaphore image;
|
|
|
|
VkSemaphore render;
|
|
|
|
VkSemaphore transfer;
|
2024-10-28 22:36:43 -06:00
|
|
|
VkSemaphore compute;
|
2024-10-26 18:25:44 -06:00
|
|
|
|
2024-10-29 12:47:04 -06:00
|
|
|
uint64_t transfer_index;
|
2024-10-28 22:36:43 -06:00
|
|
|
uint64_t compute_index;
|
2024-10-28 13:44:34 -06:00
|
|
|
|
2024-10-28 22:36:43 -06:00
|
|
|
VkCommandBuffer compute_commands;
|
2024-10-28 22:07:13 -06:00
|
|
|
VkCommandBuffer transfer_commands;
|
2024-10-28 14:48:36 -06:00
|
|
|
|
2024-10-29 13:48:06 -06:00
|
|
|
TransferBuffer transfers[2];
|
2024-10-26 12:43:07 -06:00
|
|
|
} FrameContext;
|
|
|
|
|
2024-10-09 10:54:19 -06:00
|
|
|
typedef struct RenderContextStruct {
|
|
|
|
VkInstance instance;
|
|
|
|
VkDebugUtilsMessengerEXT debug_messenger;
|
|
|
|
VkPhysicalDevice physical_device;
|
|
|
|
VkSurfaceKHR surface;
|
2024-10-24 20:49:59 -06:00
|
|
|
GPUQueue graphics_queue;
|
|
|
|
GPUQueue present_queue;
|
|
|
|
GPUQueue transfer_queue;
|
2024-10-09 10:54:19 -06:00
|
|
|
VkDevice device;
|
|
|
|
VmaAllocator allocator;
|
|
|
|
|
|
|
|
SwapchainDetails swapchain_details;
|
|
|
|
VkSurfaceFormatKHR swapchain_format;
|
|
|
|
VkPresentModeKHR swapchain_present_mode;
|
|
|
|
VkExtent2D swapchain_extent;
|
|
|
|
VkSwapchainKHR swapchain;
|
|
|
|
|
|
|
|
uint32_t swapchain_image_count;
|
|
|
|
VkImage* swapchain_images;
|
|
|
|
VkImageView* swapchain_image_views;
|
|
|
|
VkFramebuffer* swapchain_framebuffers;
|
|
|
|
|
|
|
|
VkFormat depth_format;
|
|
|
|
VkImageView depth_image_view;
|
|
|
|
VkImage depth_image;
|
|
|
|
VmaAllocation depth_image_memory;
|
|
|
|
|
|
|
|
VkCommandPool extra_graphics_pool;
|
|
|
|
VkCommandPool graphics_pool;
|
|
|
|
VkCommandPool transfer_pool;
|
|
|
|
|
2024-10-15 00:06:46 -06:00
|
|
|
VkRenderPass render_pass;
|
2024-10-09 10:54:19 -06:00
|
|
|
|
|
|
|
VkCommandBuffer* swapchain_command_buffers;
|
2024-12-03 21:05:10 -07:00
|
|
|
uint64_t swapchain_command_buffer_record_id;
|
|
|
|
uint64_t* swapchain_command_buffer_record_ids;
|
2024-10-09 10:54:19 -06:00
|
|
|
|
2024-10-26 12:43:07 -06:00
|
|
|
FrameContext frame[MAX_FRAMES_IN_FLIGHT];
|
2024-10-09 10:54:19 -06:00
|
|
|
|
2024-10-09 14:31:30 -06:00
|
|
|
uint32_t current_frame;
|
2024-10-15 12:11:28 -06:00
|
|
|
vec2 window_scale;
|
2024-11-03 14:25:59 -07:00
|
|
|
|
|
|
|
bool framebuffer_recreated;
|
2024-10-09 10:54:19 -06:00
|
|
|
} RenderContext;
|
|
|
|
|
|
|
|
GLFWwindow* init_window();
|
2024-10-17 11:06:09 -06:00
|
|
|
|
|
|
|
VkResult init_vulkan(
|
|
|
|
GLFWwindow* window,
|
|
|
|
RenderContext* context);
|
|
|
|
|
|
|
|
VkResult create_transfer_buffer(
|
|
|
|
VmaAllocator allocator,
|
2024-10-18 16:15:00 -06:00
|
|
|
VkDeviceSize size,
|
2024-10-17 11:06:09 -06:00
|
|
|
VkBuffer* buffer,
|
|
|
|
VmaAllocation* memory,
|
|
|
|
void** mapped);
|
2024-10-09 10:54:19 -06:00
|
|
|
|
2024-10-18 16:15:00 -06:00
|
|
|
void destroy_transfer_buffer(
|
|
|
|
VmaAllocator allocator,
|
|
|
|
VkBuffer buffer,
|
|
|
|
VmaAllocation memory);
|
|
|
|
|
|
|
|
VkResult create_storage_buffer(
|
|
|
|
VmaAllocator allocator,
|
|
|
|
VkBufferUsageFlags usage,
|
|
|
|
VkDeviceSize size,
|
|
|
|
VkBuffer* buffer,
|
|
|
|
VmaAllocation* memory);
|
|
|
|
|
|
|
|
VkDeviceAddress buffer_address(
|
|
|
|
VkDevice device,
|
|
|
|
VkBuffer buffer);
|
|
|
|
|
2024-10-28 14:48:36 -06:00
|
|
|
VkResult add_transfer(
|
2024-10-28 22:07:13 -06:00
|
|
|
void* data,
|
|
|
|
VkBuffer buffer,
|
|
|
|
VkDeviceSize offset,
|
|
|
|
VkDeviceSize size,
|
|
|
|
uint32_t frame_index,
|
|
|
|
RenderContext* gpu);
|
|
|
|
|
|
|
|
VkResult add_transfers(
|
2024-10-28 14:48:36 -06:00
|
|
|
void* data,
|
|
|
|
VkBuffer* buffers,
|
|
|
|
VkDeviceSize offset,
|
|
|
|
VkDeviceSize size,
|
|
|
|
RenderContext* gpu);
|
|
|
|
|
2024-10-30 17:36:53 -06:00
|
|
|
VkCommandBuffer command_begin_single(
|
|
|
|
VkDevice device,
|
|
|
|
VkCommandPool transfer_pool);
|
|
|
|
|
|
|
|
VkResult command_end_single(
|
|
|
|
VkDevice device,
|
|
|
|
VkCommandBuffer command_buffer,
|
|
|
|
VkCommandPool transfer_pool,
|
|
|
|
GPUQueue transfer_queue);
|
|
|
|
|
|
|
|
void command_copy_buffer(
|
|
|
|
VkCommandBuffer command_buffer,
|
|
|
|
VkBuffer src,
|
|
|
|
VkBuffer dst,
|
|
|
|
VkDeviceSize src_offset,
|
|
|
|
VkDeviceSize dst_offset,
|
|
|
|
VkDeviceSize size);
|
|
|
|
|
|
|
|
VkResult command_transition_image_layout(
|
|
|
|
VkDevice device,
|
|
|
|
VkCommandPool transfer_pool,
|
|
|
|
GPUQueue transfer_queue,
|
|
|
|
VkImageLayout old_layout,
|
|
|
|
VkImageLayout new_layout,
|
|
|
|
VkImage image,
|
|
|
|
VkAccessFlags src_mask,
|
|
|
|
VkAccessFlags dst_mask,
|
|
|
|
VkPipelineStageFlags source,
|
|
|
|
VkPipelineStageFlags dest,
|
|
|
|
uint32_t source_family,
|
|
|
|
uint32_t dest_family,
|
|
|
|
VkImageAspectFlags aspect_flags);
|
|
|
|
|
2024-11-03 14:25:59 -07:00
|
|
|
VkShaderModule load_shader_file(
|
|
|
|
const char* path,
|
|
|
|
VkDevice device);
|
|
|
|
|
|
|
|
VkResult recreate_framebuffer(
|
|
|
|
RenderContext* gpu);
|
2024-10-30 17:36:53 -06:00
|
|
|
|
2024-10-09 10:54:19 -06:00
|
|
|
#endif
|