Changed the OpenGL part and the GLSL shaders to use UBO and
[demo] / src / vulkan / mesh-vk.cc
1 #include <vulkan/vulkan.h>
2 #include "allocator.h"
3 #include "mesh-vk.h"
4
5 MeshVK::MeshVK()
6 {
7 }
8
9 MeshVK::MeshVK(const MeshVK &mesh)
10 {
11         indices = mesh.indices;
12         vertices = mesh.vertices;
13         normals = mesh.normals;
14         tex_coords = mesh.tex_coords;
15 }
16
17 MeshVK &MeshVK::operator=(const MeshVK &mesh)
18 {
19         if(this == &mesh)
20                 return *this;
21
22         /* what the copy constructor does */
23         indices = mesh.indices;
24         vertices = mesh.vertices;
25         normals = mesh.normals;
26         tex_coords = mesh.tex_coords;
27
28         return *this;
29 }
30
31 MeshVK::~MeshVK()
32 {
33         vku_destroy_buffer(vk_vertices);
34         vku_destroy_buffer(vk_normals);
35         vku_destroy_buffer(vk_tex_coords);
36         vku_destroy_buffer(vk_indices);
37
38         vertices.clear();
39         normals.clear();
40         tex_coords.clear();
41         indices.clear();
42 }
43
44 bool MeshVK::update_vertex_data()
45 {
46         if(vertices.empty()) {
47                 printf("empty vertices!\n");
48                 return false;
49         }
50
51         /* create the buffers */
52
53         int vsz = vertices.size() * sizeof(Vec3);
54         if(!(vk_vertices = vku_create_buffer(vsz,
55                                              VK_BUFFER_USAGE_VERTEX_BUFFER_BIT))) {
56                 fprintf(stderr, "Failed to create the buffer for the vertices.\n");
57                 return false;
58         }
59
60         int nsz = normals.size() * sizeof(Vec3);
61         if(!(vk_normals = vku_create_buffer(nsz,
62                                             VK_BUFFER_USAGE_VERTEX_BUFFER_BIT))) {
63                 vku_destroy_buffer(vk_vertices);
64
65                 fprintf(stderr, "Failed to create the buffer for the normals.\n");
66                 return false;
67         }
68
69         int tsz = tex_coords.size() * sizeof(Vec2);
70         if(!(vk_tex_coords = vku_create_buffer(tsz,
71                                                VK_BUFFER_USAGE_VERTEX_BUFFER_BIT))) {
72                 vku_destroy_buffer(vk_vertices);
73                 vku_destroy_buffer(vk_normals);
74
75                 fprintf(stderr,
76                         "Failed to create the buffer for the texture coordinates.\n");
77                 return false;
78         }
79
80         int isz = indices.size() * 2;
81         if(!(vk_indices = vku_create_buffer(isz,
82                                             VK_BUFFER_USAGE_INDEX_BUFFER_BIT))) {
83                 vku_destroy_buffer(vk_vertices);
84                 vku_destroy_buffer(vk_normals);
85                 vku_destroy_buffer(vk_tex_coords);
86
87                 fprintf(stderr, "Failed to create the indices buffer.\n");
88                 return false;
89         }
90
91         /* write the buffers */
92
93         if(!vku_write_memory(vk_vertices->mem_pool, vsz, (void *)vertices.data())) {
94                 fprintf(stderr, "Failed to write the vertices to GPU.\n");
95                 return false;
96         }
97         if(vkBindBufferMemory(vk_device, vk_vertices->buf, vk_vertices->mem_pool,
98                               0) != VK_SUCCESS) {
99                 fprintf(stderr, "Failed to bind the vertex buffer memory\n");
100                 return false;
101         }
102
103         if(!vku_write_memory(vk_normals->mem_pool, nsz, (void *)normals.data())) {
104                 fprintf(stderr, "Failed to write the normals to GPU.\n");
105                 return false;
106         }
107         if(vkBindBufferMemory(vk_device, vk_normals->buf, vk_normals->mem_pool, 0)
108                 != VK_SUCCESS) {
109                 fprintf(stderr, "Failed to bind the normal buffer memory\n");
110                 return false;
111         }
112
113         if(!vku_write_memory(vk_tex_coords->mem_pool, tsz,
114                              (void *)tex_coords.data())) {
115                 fprintf(stderr, "Failed to write the texture coordinates to GPU.\n");
116                 return false;
117         }
118         if(vkBindBufferMemory(vk_device, vk_tex_coords->buf,
119                               vk_tex_coords->mem_pool, 0) != VK_SUCCESS) {
120                 fprintf(stderr, "Failed to bind the tex coordinates buffer memory.\n");
121                 return false;
122         }
123
124         if(!vku_write_memory(vk_indices->mem_pool, isz, (void *)indices.data())) {
125                 fprintf(stderr, "Failed to write the indices to GPU.\n");
126                 return false;
127         }
128         if(vkBindBufferMemory(vk_device, vk_indices->buf, vk_indices->mem_pool, 0)
129                 != VK_SUCCESS) {
130                 fprintf(stderr, "Failed to bind the index buffer memory.\n");
131                 return false;
132         }
133
134         return true;
135 }
136
137 void MeshVK::draw() const
138 {
139 }
140
141 void MeshVK::draw_normals(float scale) const
142 {
143 }