0f4074ec57934ae260f8b71b959b7b36c41fe684
[demo] / src / vulkan / renderer-vk.cc
1 #include <vulkan/vulkan.h>
2 #include <stdio.h>
3
4 #include "gfxapi.h"
5 #include "renderer-vk.h"
6 #include "uniforms.h"
7 #include "vkutil.h"
8 #include "vkutil-pipeline.h"
9
10 static void set_resources_layouts(VkuPipelineGenerator *pipe_gen, std::vector<ResourceVK> *res);
11
12 RendererVK::RendererVK()
13 {
14         scene = 0;
15         camera = 0;
16         sprog = 0;
17
18         skytex = 0;
19         dskytex = 0;
20
21         fog_density = 0;
22
23         default_vs_ub = 0;
24         default_fs_ub = 0;
25         sky_vs_ub = 0;
26 }
27
28 RendererVK::~RendererVK()
29 {
30         delete default_vs_ub;
31         delete default_fs_ub;
32         delete sky_vs_ub;
33 }
34
35 bool RendererVK::create_resources()
36 {
37         /* default pipeline resources */
38
39         default_vs_ub = gfx_create_uniform_buffer();
40         int data_size = sizeof default_vs_uniforms;
41         if(!default_vs_ub->create(data_size)) {
42                 fprintf(stderr, "Failed to create the default VS uniform buffer.\n");
43                 return false;
44         }
45         ResourceVK default_vs_res;
46         if(!default_vs_res.create_ds_layout(MATRIX_UNIFORMS,
47                                             VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
48                                             VK_SHADER_STAGE_VERTEX_BIT, 0)) {
49                 fprintf(stderr, "Failed to create the ds layout for the default vs uniform buffer.\n");
50                 return false;
51         }
52         default_vs_res.size = data_size;
53         default_resources.push_back(default_vs_res);
54
55         default_fs_ub = gfx_create_uniform_buffer();
56         if(!default_fs_ub->create(sizeof default_fs_uniforms)) {
57                 fprintf(stderr, "Failed to create the default FS uniform buffer.\n");
58                 return false;
59         }
60         ResourceVK default_fs_res;
61         if(!default_fs_res.create_ds_layout(SHADING_UNIFORMS,
62                                             VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
63                                             VK_SHADER_STAGE_FRAGMENT_BIT, 0)) {
64                 fprintf(stderr, "Failed to create the ds layout for the default VS uniform buffer.\n");
65                 return false;
66         }
67         default_resources.push_back(default_fs_res);
68         set_resources_layouts(&default_pipe_gen, &default_resources);
69
70         /* sky pipeline resources */
71
72         sky_vs_ub = gfx_create_uniform_buffer();
73         if(!sky_vs_ub->create(sizeof sky_vs_uniforms)) {
74                 fprintf(stderr, "Failed to create the VS uniform buffer for the sky.\n");
75                 return false;
76         }
77         ResourceVK sky_vs_res;
78         if(!sky_vs_res.create_ds_layout(SKY_MATRIX_UNIFORMS,
79                                         VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
80                                         VK_SHADER_STAGE_FRAGMENT_BIT, 0)) {
81                 fprintf(stderr, "Failed to create the ds layout for the sky VS uniform buffer.\n");
82                 return false;
83         }
84         sky_resources.push_back(sky_vs_res);
85         set_resources_layouts(&sky_pipe_gen, &sky_resources);
86
87         return true;
88 }
89
90 bool RendererVK::create()
91 {
92         /*      if(!create_sky_pipeline()) {
93                         fprintf(stderr, "Failed to create sky pipeline.\n");
94                         return false;
95                 }*/
96
97         return true;
98 }
99
100 bool RendererVK::create_default_pipeline()
101 {
102         return true;
103 }
104
105 bool RendererVK::create_desc_pool()
106 {
107         int num_desc = default_resources.size() + sky_resources.size();
108         VkDescriptorPoolCreateInfo dpinf;
109         memset(&dpinf, 0, sizeof dpinf);
110
111         dpinf.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
112         dpinf.maxSets = num_desc + 1;
113         dpinf.poolSizeCount = num_desc;
114
115         for(size_t i=0; i<default_resources.size(); i++) {
116         }
117         return true;
118 }
119
120 static void set_resources_layouts(VkuPipelineGenerator *pipe_gen, std::vector<ResourceVK> *res)
121 {
122         for(size_t i=0; i<res->size(); i++) {
123                 if((*res)[i].type == RES_DESC_SET) {
124                         pipe_gen->set_resources_layout((*res)[i].layout);
125                 }
126         }
127 }