1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
/**
* @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
|