summaryrefslogtreecommitdiff
path: root/render/vulkan/renderer.c
blob: 0facddf3f954306eab65d0f0397f3964c180bb4a (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
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
#include <vulkan/vulkan.h>
#include <GLFW/glfw3.h>
#include <rune/render/renderer.h>
#include <rune/core/logging.h>
#include <rune/core/alloc.h>
#include <rune/core/abort.h>
#include "context.h"
#include "image.h"
#include <sys/time.h>

static struct vkcontext *context = NULL;

void _init_cmdbuffers(void) {
        uint32_t num_buffers = context->swapchain->img_count;
        if (context->cmdbuffers == NULL)
                context->cmdbuffers = rune_calloc(0, sizeof(struct vkcmdbuffer*) * num_buffers);

        for (uint32_t i = 0; i < num_buffers; i++) {
                if (context->cmdbuffers[i] != NULL)
                        destroy_vkcmdbuffer(context->cmdbuffers[i], context->dev);
                context->cmdbuffers[i] = create_vkcmdbuffer(context->dev, 1);
        }
        log_output(LOG_DEBUG, "Created %d command buffers", num_buffers);
}

void _destroy_cmdbuffers(void) {
        uint32_t num_buffers = context->swapchain->img_count;
        if (context->cmdbuffers == NULL)
                return;
        for (uint32_t i = 0; i < num_buffers; i++) {
                if (context->cmdbuffers[i] != NULL)
                        destroy_vkcmdbuffer(context->cmdbuffers[i], context->dev);
        }
        log_output(LOG_DEBUG, "Destroyed %d command buffers", num_buffers);
}

void _init_framebuffers(void) {
        uint32_t num_buffers = context->swapchain->img_count;
        if (context->framebuffers == NULL)
                context->framebuffers = rune_calloc(0, sizeof(struct vkframebuffer*) * num_buffers);

        uint32_t at_count = 2;
        for (uint32_t i = 0; i < num_buffers; i++) {
                VkImageView attachments[at_count];
                attachments[0] = context->swapchain->views[i];
                attachments[1] = context->swapchain->depth_attachment->view;

                if (context->framebuffers[i] != NULL)
                        destroy_vkframebuffer(context->framebuffers[i], context->dev);
                context->framebuffers[i] = create_vkframebuffer(context->dev,
                                                                context->rendpass,
                                                                context->surface->width,
                                                                context->surface->height,
                                                                at_count,
                                                                attachments);
        }
        log_output(LOG_DEBUG, "Created %d frame buffers", num_buffers);
}

void _destroy_framebuffers(void) {
        uint32_t num_buffers = context->swapchain->img_count;
        if (context->framebuffers == NULL)
                return;

        for (uint32_t i = 0; i < num_buffers; i++)
                destroy_vkframebuffer(context->framebuffers[i], context->dev);
        log_output(LOG_DEBUG, "Destroyed %d frame buffers", num_buffers);
}

int _init_vulkan(struct rune_window *window) {
        struct timeval start;
        struct timeval stop;
        gettimeofday(&start, NULL);

        struct ext_container ext;
        ext.extensions = glfwGetRequiredInstanceExtensions(&ext.ext_count);
        struct vklayer_container *vklayers = init_vklayers(&ext);

        context = create_vkcontext(vklayers, &ext);
        if (context == NULL)
                return -1;

        VkResult res = glfwCreateWindowSurface(context->instance,
                                               window->window,
                                               NULL,
                                               &context->surface->handle);
        if (res != VK_SUCCESS) {
                log_output(LOG_FATAL, "Cannot create rendering surface");
                return -1;
        }

        context->dev = create_vkdev(context->instance,
                                         context->surface->handle);
        if (context->dev == NULL)
                return -1;

        context->surface->width = window->winw;
        context->surface->height = window->winh;
        context->swapchain = create_swapchain(context->surface, context->dev);
        if (context->swapchain == NULL)
                return -1;

        vec4 area = {0, 0, context->surface->width, context->surface->height};
        vec4 color = {0, 0, 0, 1.0f};
        context->rendpass = create_vkrendpass(context->dev, context->swapchain, area, color, 1.0, 0);
        if (context->rendpass == NULL)
                return -1;

        _init_framebuffers();
        _init_cmdbuffers();

        context->image_semaphores = rune_alloc(sizeof(VkSemaphore) * context->swapchain->max_frames);
        context->queue_semaphores = rune_alloc(sizeof(VkSemaphore) * context->swapchain->max_frames);
        context->fences_in_flight = rune_calloc(0, sizeof(struct vkfence*) * context->swapchain->max_frames);

        VkSemaphoreCreateInfo scinfo;
        for (uint8_t i = 0; i < context->swapchain->max_frames; i++) {
                scinfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
                scinfo.pNext = NULL;
                scinfo.flags = 0;
                vkCreateSemaphore(context->dev->ldev, &scinfo, NULL, &context->image_semaphores[i]);
                vkCreateSemaphore(context->dev->ldev, &scinfo, NULL, &context->queue_semaphores[i]);
                context->fences_in_flight[i] = create_vkfence(context->dev, 1);
        }
        context->images_in_flight = rune_calloc(0, sizeof(struct vkfence*) * context->swapchain->img_count);

        gettimeofday(&stop, NULL);
        log_output(LOG_INFO, "Finished initializing Vulkan in %lums", (stop.tv_sec - start.tv_sec) * 1000000 + stop.tv_usec - start.tv_usec);
        return 0;
}

void _close_vulkan(void) {
        vkDeviceWaitIdle(context->dev->ldev);
        for (uint8_t i = 0; i < context->swapchain->max_frames; i++) {
                if (context->image_semaphores[i] != NULL)
                        vkDestroySemaphore(context->dev->ldev, context->image_semaphores[i], NULL);
                if (context->queue_semaphores[i] != NULL)
                        vkDestroySemaphore(context->dev->ldev, context->queue_semaphores[i], NULL);
                destroy_vkfence(context->fences_in_flight[i], context->dev);
        }

        _destroy_cmdbuffers();
        _destroy_framebuffers();
        destroy_vkrendpass(context->rendpass, context->dev);
        destroy_swapchain(context->swapchain, context->dev);
        destroy_vkdev(context->dev);
        destroy_vkcontext(context);
        rune_free(renderer);
}

void _draw_vulkan(void) {
        if (fence_lock(context->fences_in_flight[context->swapchain->frame], context->dev, UINT64_MAX) != 0)
                log_output(LOG_WARN, "In-flight fence locking failure");
}

void _clear_vulkan(void) {
}

struct rune_renderer* select_render_vulkan(struct rune_window *window) {
        struct rune_renderer *ret = rune_alloc(sizeof(struct rune_renderer));
        ret->close = _close_vulkan;
        ret->draw = _draw_vulkan;
        ret->clear = _clear_vulkan;
        if (_init_vulkan(window) != 0)
                rune_abort();
        return ret;
}