-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpu.h
217 lines (172 loc) · 5.19 KB
/
gpu.h
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#ifndef GPU_H
#define GPU_H
#include <vulkan/vulkan_core.h>
#include "shader.h"
#include "chars.h"
#define SC_MAX_IMGS 4 /* Arbitrarily small size that I doubt will be exceeded */
#define SC_MIN_IMGS 2
#define FRAME_WRAP 2
extern u32 frm_i; // frame index, is either 0 or 1
enum {
DB_SI_T, // transfer complete
DB_SI_G, // color output complete
DB_SI_I, // glyph upload
DB_SEM_CNT,
};
#define GPU_MAX_CMDS 16
enum gpu_flags {
GPU_MEM_INI = 0x01, // mem.type is valid
GPU_MEM_UNI = 0x02, // mem arch is unified
GPU_MEM_OFS = 0x04, // buffers use the top half this frame
GPU_MEM_BITS = GPU_MEM_INI|GPU_MEM_UNI,
};
enum gpu_mem_indices {
GPU_MI_G,
GPU_MI_T,
GPU_MI_I,
GPU_MI_R, // msaa render target
GPU_MEM_CNT,
};
enum gpu_buf_indices {
GPU_BI_G,
GPU_BI_T,
GPU_BUF_CNT,
};
enum gpu_q_indices {
GPU_QI_G,
GPU_QI_T,
GPU_QI_P,
GPU_Q_CNT,
};
// represents queues that can be submitted to
enum gpu_cmdq_indices {
GPU_CI_G,
GPU_CI_T,
GPU_CMD_CNT
};
struct gpu {
VkInstance inst;
VkSurfaceKHR surf;
VkPhysicalDeviceProperties props;
VkPhysicalDeviceMemoryProperties memprops;
VkPhysicalDevice phys_dev;
VkDevice dev;
u32 flags;
u32 q_cnt;
struct {
VkQueue handle;
u32 i;
struct {
u32 buf_cnt;
VkCommandPool pool;
VkCommandBuffer bufs[GPU_MAX_CMDS];
} cmd[FRAME_WRAP];
} q[GPU_Q_CNT];
struct {
VkDeviceMemory handle;
u32 type;
} mem[GPU_MEM_CNT];
struct {
VkBuffer handle;
void *data;
u64 size;
u64 used;
} buf[GPU_BUF_CNT];
struct gpu_glyph {
VkImage img;
VkImageView view;
s16 x,y,w,h;
} glyph[CHT_SZ];
struct {
struct extent_u16 dim_px; // pixel dimensions of a character cell
struct extent_u16 win_dim_cells; // window dimensions in cells
struct extent_f32 rdim_px; // reciprocals
struct extent_f32 rwin_dim_cells;
s32 y_ofs; // default cell px shift
u32 cnt;
} cell;
struct {
VkSwapchainKHR handle;
VkSwapchainCreateInfoKHR info;
VkImage imgs[SC_MAX_IMGS];
VkImageView views[SC_MAX_IMGS];
VkSemaphore sem[SC_MAX_IMGS];
u32 img_i[SC_MAX_IMGS];
u32 img_cnt,i;
} sc;
struct {
VkShaderModule vert;
VkShaderModule frag;
} sh;
VkPipelineLayout pll;
VkPipeline pl;
VkRenderPass rp;
VkFramebuffer fb[FRAME_WRAP];
VkDescriptorSetLayout dsl;
VkDescriptorPool dp;
VkDescriptorSet ds;
VkSampler sampler;
struct draw_buffer { // size == gpu.cell.cnt
VkSemaphore sem[DB_SEM_CNT];
VkFence fence[FRAME_WRAP];
VkImage img[FRAME_WRAP]; // msaa render target
VkImageView view[FRAME_WRAP];
struct draw_info {
struct rect_u16 pd;
struct rgba fg,bg;
} *di;
u32 used; // number of occupied draw infos
u32 in_use_fences; // bit mask
VkSampleCountFlags msaa_samples;
} db;
};
#ifdef LIB
extern struct gpu *gpu;
#define def_create_gpu(name) int name(void)
def_create_gpu(create_gpu);
#define def_gpu_create_sh(name) int name(void)
def_gpu_create_sh(gpu_create_sh);
#define def_gpu_handle_win_resize(name) int name(void)
def_gpu_handle_win_resize(gpu_handle_win_resize);
#define def_gpu_update(name) int name(void)
def_gpu_update(gpu_update);
#define def_gpu_db_add(name) int name(struct rect_u16 rect, struct rgba fg, struct rgba bg)
def_gpu_db_add(gpu_db_add);
#define def_gpu_db_flush(name) int name(void)
def_gpu_db_flush(gpu_db_flush);
#define def_gpu_check_leaks(name) void name(void)
def_gpu_check_leaks(gpu_check_leaks);
/**********************************************************************/
// gpu.c and vdt.c helper stuff
// TODO(SollyCB): I would REALLY like to have a way to define typesafe
// integers...
enum gpu_cmd_opt {
GPU_CMD_OT = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
GPU_CMD_RE = VK_COMMAND_BUFFER_RESET_RELEASE_RESOURCES_BIT,
};
union gpu_memreq_info {
VkBufferCreateInfo *buf;
VkImageCreateInfo *img;
};
enum cell_vertex_fmts {
CELL_PD_FMT = VK_FORMAT_R16G16B16A16_UNORM,
CELL_FG_FMT = VK_FORMAT_R8G8B8A8_UNORM,
CELL_BG_FMT = VK_FORMAT_R8G8B8A8_UINT,
CELL_GL_FMT = VK_FORMAT_R8_UNORM,
};
enum gpu_fb_attachment_indices {
GPU_FB_AI_MSAA, // msaa color
GPU_FB_AI_SWAP, // swapchain image resolve
};
extern u32 gpu_bi_to_mi[GPU_BUF_CNT];
extern u32 gpu_ci_to_qi[GPU_CMD_CNT];
extern char* gpu_mem_names[GPU_MEM_CNT];
extern char *gpu_cmdq_names[GPU_CMD_CNT];
#define gpu_buf_name(bi) gpu_mem_names[gpu_bi_to_mi[bi]]
#define gpu_cmd_name(ci) gpu_cmdq_names[ci]
#define gpu_cmd(ci) gpu->q[gpu_ci_to_qi[ci]].cmd[frm_i]
// calculate the size in bytes of the draw buffer
#define gpu_dba_sz(cc) (sizeof(*gpu->db.di) * cc)
#define GPU_GLYPH_FENCE_HANDLE gpu->db.fence[0]
#endif // ifdef LIB
#endif