kiba-engine
types.h
1 #pragma once
2 
3 #include <kiba/containers/array.h>
4 #include <kiba/core/id.h>
5 #include <kiba/core/types.h>
6 #include <kiba/gpu/enums.h>
7 #include <kiba/gpu/internal.h>
8 #include <kiba/gpu/limits.h>
9 #include <vulkan/vulkan.h>
10 
11 struct gpu_extent_3d {
12  usize width, height;
13 
14  union {
15  usize depth;
16  usize layers;
17  };
18 };
19 
20 // TODO gpu_surface_capabilities
21 
23  // TODO is texture usage required when the only valid one is RENDER_ATTACHMENT anyway??
24  enum gpu_texture_format format;
25  enum gpu_present_mode present_mode;
26  u32 width;
27  u32 height;
28  // TODO composite alpha mode
29  // investigate view_formats
30 };
31 
32 struct gpu_device;
33 typedef struct gpu_device *gpu_device;
34 
36  const char *label;
37  struct gpu_extent_3d size;
38  usize mip_level_count;
39  usize sample_count;
40 
41  enum gpu_texture_dimension dimension;
42 
43  enum gpu_texture_format format;
44 
45  enum gpu_texture_usage usage;
46 
47  // TODO view_formats
48 };
49 
50 struct gpu_texture {
51  struct gpu_backend_texture bt;
52  gpu_device device;
53  identifier tracker_id;
54  struct gpu_texture_descriptor desc;
55 };
56 typedef struct gpu_texture *gpu_texture;
57 
59  gpu_texture texture;
60  enum gpu_texture_use start_use;
61  enum gpu_texture_use cur_use;
62 };
63 
64 struct gpu_surface {
65  struct gpu_backend_surface bs;
66  struct gpu_surface_configuration config;
67  gpu_device device;
68  gpu_texture presentation;
69 };
70 typedef struct gpu_surface *gpu_surface;
71 
73  const char *label;
74  enum gpu_texture_format format; // must either be texture format or inside texture's view_formats list
75  enum gpu_texture_view_dimension dimension;
76  enum gpu_texture_aspect aspect;
77  usize base_mip_level;
78  usize mip_level_count;
79  usize base_array_layer;
80  usize array_layer_count;
81 };
82 
84  struct gpu_backend_texture_view btv;
85  struct gpu_texture_view_descriptor desc;
86  gpu_texture texture;
87  gpu_device device;
88  // TODO store more info e.g. extent, samples, mips and layers
89  // (https://github.com/gfx-rs/wgpu/blob/5b8be97a887eb198f1e58963b4c94d46cf4a84db/wgpu-core/src/resource.rs#L1286)
90 };
91 typedef struct gpu_texture_view *gpu_texture_view;
92 
94  const char *label;
95  usize size;
96  enum gpu_buffer_usage usage;
97 };
98 
99 struct gpu_buffer {
100  struct gpu_backend_buffer bb;
101  gpu_device device;
102  identifier tracker_id;
103  struct gpu_buffer_descriptor desc;
104 };
105 typedef struct gpu_buffer *gpu_buffer;
106 
108  gpu_buffer buffer;
109  enum gpu_buffer_usage start_use;
110  enum gpu_buffer_usage cur_use;
111 };
112 
114  struct gpu_backend_texture *tex;
115  enum gpu_texture_format format;
116  enum gpu_texture_use source_use;
117  enum gpu_texture_use target_use;
118 };
119 
121  struct gpu_backend_buffer *buf;
122  enum gpu_buffer_usage source_usage;
123  enum gpu_buffer_usage target_usage;
124 };
125 
126 struct gpu_color {
127  f64 r, g, b, a;
128 };
129 
131  const char *label;
132  struct gpu_color debug_color;
133 };
134 
136  struct gpu_backend_command_encoder bce;
137  gpu_device device;
138  array_of(struct gpu_texture_tracker) texture_trackers;
139  array_of(struct gpu_texture_barrier) pending_barriers;
140  array_of(struct gpu_buffer_tracker) buffer_trackers;
141  array_of(struct gpu_buffer_barrier) pending_buffer_barriers;
142  struct gpu_command_encoder_descriptor desc;
143 };
145 
146 enum gpu_device_resource_type {
147  GPU_DEVICE_RESOURCE_TEXTURE,
148  GPU_DEVICE_RESOURCE_TEXTURE_VIEW,
149  GPU_DEVICE_RESOURCE_PIPELINE,
150  GPU_DEVICE_RESOURCE_PIPELINE_LAYOUT,
151  GPU_DEVICE_RESOURCE_SHADER_MODULE,
152  GPU_DEVICE_RESOURCE_BUFFER,
153  GPU_DEVICE_RESOURCE_COMMAND_ENCODER,
154 };
155 
157  enum gpu_device_resource_type type;
158  void *handle;
159 };
161  array_of(struct gpu_device_resource) resources;
162 };
163 
164 // TODO add device feature struct
165 struct gpu_device {
166  struct gpu_backend_device bd;
167  id_generator texture_id_gen;
168  array_of(struct gpu_texture_tracker) textures; // TODO rename this to texture_trackers
169  id_generator buffer_id_gen;
170  array_of(struct gpu_buffer_tracker) buffer_trackers;
171  struct gpu_queued_resource_destructions destruction_queues[2];
172  usize destruction_queue_index;
173  allocator alloc;
174 };
175 
177  enum {
178  GPU_LOAD_OP_CLEAR,
179  GPU_LOAD_OP_LOAD,
180  } load;
181 
182  enum {
183  GPU_STORE_OP_STORE,
184  GPU_STORE_OP_DISCARD,
185  } store;
186 
187  union {
188  struct gpu_color clear_color;
189  f32 clear_depth;
190  u32 clear_stencil;
191  };
192 };
193 
194 static inline enum gpu_attachment_ops gpu_operations_to_attachment_ops(struct gpu_operations ops) {
195  enum gpu_attachment_ops ret = 0;
196  if (ops.load == GPU_LOAD_OP_LOAD) {
197  ret |= GPU_ATTACHMENT_OP_LOAD;
198  }
199  if (ops.store == GPU_STORE_OP_STORE) {
200  ret |= GPU_ATTACHMENT_OP_STORE;
201  }
202  return ret;
203 }
204 
206  gpu_texture_view view;
207  // TODO resolve target
208  struct gpu_operations ops;
209 };
210 
212  gpu_texture_view *view;
213  struct gpu_operations depth_ops;
214  struct gpu_operations stencil_ops;
215 };
216 
218  const char *label;
219  struct gpu_extent_3d extent;
220  u32 color_attachment_count;
221  struct gpu_color_attachment color_attachments[KB_GPU_MAX_COLOR_ATTACHMENTS];
222  struct gpu_depth_stencil_attachment depth_stencil_attachment;
223  // TODO timestamp writes, occulusion query set
224 };
225 
226 // shader
227 
229  const char *label;
230  enum gpu_shader_type type;
231  array_of(u8) code;
232 };
233 
235  struct gpu_backend_shader_module bs;
236  gpu_device device;
237  enum gpu_shader_type type;
238 };
239 typedef struct gpu_shader_module *gpu_shader_module;
240 
241 // pipeline
242 
244  enum {
245  GPU_ADDRESS_MODE_CLAMP_TO_EDGE,
246  GPU_ADDRESS_MODE_REPEAT,
247  GPU_ADDRESS_MODE_MIRROR_REPEAT,
248  GPU_ADDRESS_MODE_CLAMP_TO_BORDER, // TODO check if this should be supported
249  } u, v, w;
250 
251  enum {
252  GPU_FILTER_MODE_LINEAR,
253  GPU_FILTER_MODE_NEAREST,
254  } mag_filter, min_filter, mipmap_filter;
255 
256  f32 load_min_clamp, load_max_clamp;
257 };
258 
260  const char *label;
261  // TODO push constant ranges
262  array_of(gpu_bind_group_layout) bind_group_layouts; // TODO from struct gpu_bind_group_layout_descriptor
263 };
264 
266  struct gpu_backend_pipeline_layout bl;
267  gpu_device device;
268 };
270 
272  usize offset;
273  usize location;
274  enum gpu_vertex_format format;
275 };
276 
278  usize stride;
279  enum gpu_step_mode step_mode;
280  array_of(struct gpu_vertex_buffer_layout_attribute) attributes;
281 };
282 
284  gpu_shader_module shader;
285  const char *entry_point; // TODO This is pointless with the current shader impl. glslc only supports one entry
286  // point per compilation unit of glsl. In glsl that entry point must be named `main`.
287  array_of(struct gpu_vertex_buffer_layout) buffers;
288 };
289 
291  enum gpu_primitive_topology topology;
292  // TODO check strip index format
293  enum gpu_front_face front_face;
294  enum gpu_cull_mode cull_mode;
295  // TODO check unclipped depth
296  enum gpu_polygon_mode polygon_mode;
297  // TODO check conservative
298 };
299 
301  b8 active;
302  // TODO
303 };
304 
306  enum gpu_texture_format format;
307  struct gpu_blend_state blend;
308  enum gpu_color_writes write_mask;
309 };
310 
312  b8 active;
313  gpu_shader_module shader;
314  const char *entry_point;
315  array_of(struct gpu_color_target_state) targets;
316  // TODO multiview
317 };
318 
320  enum gpu_texture_format format;
321  b8 depth_write_enabled;
322  enum gpu_compare_func depth_compare;
323 
324  struct {
325  struct {
326  enum gpu_compare_func compare;
327 
328  enum {
329  GPU_STENCIL_OPERATION_KEEP,
330  GPU_STENCIL_OPERATION_ZERO,
331  GPU_STENCIL_OPERATION_REPLACE,
332  GPU_STENCIL_OPERATION_INVERT,
333  GPU_STENCIL_OPERATION_INCREMENT_CLAMP,
334  GPU_STENCIL_OPERATION_DECREMENT_CLAMP,
335  GPU_STENCIL_OPERATION_INCREMENT_WRAP,
336  GPU_STENCIL_OPERATION_DECREMENT_WRAP,
337  } fail_op, depth_fail_op, pass_op;
338  } front, back;
339 
340  u8 read_mask, write_mask;
341  } stencil;
342 
343  struct {
344  f32 constant;
345  f32 slope_scale;
346  f32 clamp;
347  } depth_bias;
348 };
349 
351  const char *label;
352  gpu_pipeline_layout layout;
353  struct gpu_vertex_state vertex;
354  struct gpu_primitive_state primitive;
355  struct gpu_depth_stencil_state depth_stencil;
356  struct gpu_fragment_state fragment;
357  // TODO multisample state, multiview
358 };
359 
360 struct gpu_pipeline {
361  struct gpu_backend_pipeline bp;
362  gpu_device device;
363 };
364 typedef struct gpu_pipeline *gpu_pipeline;
Central allocator structure.
Definition: allocator.h:87