summaryrefslogtreecommitdiff
path: root/src/graphics.h
blob: e69582ae0171b7cb28ef252dca36bc3e1b289aa9 (plain)
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
#ifndef GRAPHICS_H
#define GRAPHICS_H

#include <stdint.h>
#include <stdbool.h>
#include <cglm/cglm.h>
#include <vulkan/vulkan.h>

#include "common.h"

// from vulkan source
#define MAKE_VERSION(major, minor, patch) \
    ((((u32)(major)) << 22U) | (((u32)(minor)) << 12U) | ((u32)(patch)))

typedef uint32_t u32;

typedef struct vertex {
    vec3 pos;
    vec3 normal;
    vec3 color;
} vertex_t;

typedef struct buffer {
    VkBuffer buffer;
    VkDeviceMemory memory;
} buffer_t;

typedef struct image {
    VkImage image;
    VkImageView image_view;
    VkDeviceMemory memory;
} image_t;

typedef struct graphics {
    VkInstance instance;
    VkDebugUtilsMessengerEXT debug_messenger;
    
    VkPhysicalDevice physical_device;    
    VkDevice logical_device;
    
    VkQueue graphics_queue;
    VkQueue present_queue;
    VkQueue transfer_queue;

    VkSurfaceKHR surface;

    struct pipeline {
        VkRenderPass render_pass;
        VkDescriptorSetLayout descriptor_layout; // VkDescriptorSetLayout *descriptor_layouts;
        VkPipelineLayout layout;
        VkPipeline pipeline;
    } pipeline;
    
    struct swap_chain {
        VkSwapchainKHR swap_chain;
        
        VkImage *images;
        VkImageView *image_views;
        VkFramebuffer *framebuffers;
        u32 nimages;

        image_t depth_image;

        VkFormat image_format;
        VkExtent2D extent;
    } swap_chain;

    struct command {
        VkCommandPool pool;
        VkCommandBuffer buffer;
    } command_graphics, command_transfer;

    struct descriptor {
        VkDescriptorPool pool;
        VkDescriptorSetLayout layout;
        VkDescriptorSet set;
    } ubo_descriptor;

    struct sync {
        VkSemaphore semph_image_available;
        VkSemaphore semph_render_finished;
        VkFence     fence_inflight;
    } sync;
    
    buffer_t vertex_buffer;
    buffer_t index_buffer;
    
    struct {
        buffer_t buffer;
        void *mapped_data;
        int (*update_ubo)(void *ubo);
    } ubo;
    
} * graphics_t;

struct graphics_info {
    char *name;
    u32 version;
    
    const char* const* extensions;
    u32 ext_count;

    vertex_t *vertices;
    size_t nvertices;
    u32 *indices;
    size_t nindices;

    size_t ubo_size; 
    int (*update_ubo)(void *ubo);
    
    int (*surface_func)(VkInstance instance, VkSurfaceKHR *surface);
};

graphics_t graphics_create(struct graphics_info *info);
void graphics_destroy(graphics_t device);
int graphics_draw_frame(graphics_t device, u32 nvertices);

#endif