some descriptor functions in vkutil - wip
[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         return true;
47         if(vertices.empty()) {
48                 printf("empty vertices!\n");
49                 return false;
50         }
51
52         /* create the buffers */
53
54         int vsz = vertices.size() * sizeof(Vec3);
55         if(!(vk_vertices = vku_create_buffer(vsz,
56                                              VK_BUFFER_USAGE_VERTEX_BUFFER_BIT))) {
57                 fprintf(stderr, "Failed to create the buffer for the vertices.\n");
58                 return false;
59         }
60
61         int nsz = normals.size() * sizeof(Vec3);
62         if(!(vk_normals = vku_create_buffer(nsz,
63                                             VK_BUFFER_USAGE_VERTEX_BUFFER_BIT))) {
64                 vku_destroy_buffer(vk_vertices);
65
66                 fprintf(stderr, "Failed to create the buffer for the normals.\n");
67                 return false;
68         }
69
70         int tsz = tex_coords.size() * sizeof(Vec2);
71         if(!(vk_tex_coords = vku_create_buffer(tsz,
72                                                VK_BUFFER_USAGE_VERTEX_BUFFER_BIT))) {
73                 vku_destroy_buffer(vk_vertices);
74                 vku_destroy_buffer(vk_normals);
75
76                 fprintf(stderr,
77                         "Failed to create the buffer for the texture coordinates.\n");
78                 return false;
79         }
80
81         int isz = indices.size() * 2;
82         if(!(vk_indices = vku_create_buffer(isz,
83                                             VK_BUFFER_USAGE_INDEX_BUFFER_BIT))) {
84                 vku_destroy_buffer(vk_vertices);
85                 vku_destroy_buffer(vk_normals);
86                 vku_destroy_buffer(vk_tex_coords);
87
88                 fprintf(stderr, "Failed to create the indices buffer.\n");
89                 return false;
90         }
91
92         /* write the buffers */
93
94         if(!vku_write_memory(vk_vertices->mem_pool, vsz, (void *)vertices.data())) {
95                 fprintf(stderr, "Failed to write the vertices to GPU.\n");
96                 return false;
97         }
98         if(vkBindBufferMemory(vk_device, vk_vertices->buf, vk_vertices->mem_pool,
99                               0) != VK_SUCCESS) {
100                 fprintf(stderr, "Failed to bind the vertex buffer memory\n");
101                 return false;
102         }
103
104         if(!vku_write_memory(vk_normals->mem_pool, nsz, (void *)normals.data())) {
105                 fprintf(stderr, "Failed to write the normals to GPU.\n");
106                 return false;
107         }
108         if(vkBindBufferMemory(vk_device, vk_normals->buf, vk_normals->mem_pool, 0)
109                 != VK_SUCCESS) {
110                 fprintf(stderr, "Failed to bind the normal buffer memory\n");
111                 return false;
112         }
113
114         if(!vku_write_memory(vk_tex_coords->mem_pool, tsz,
115                              (void *)tex_coords.data())) {
116                 fprintf(stderr, "Failed to write the texture coordinates to GPU.\n");
117                 return false;
118         }
119         if(vkBindBufferMemory(vk_device, vk_tex_coords->buf,
120                               vk_tex_coords->mem_pool, 0) != VK_SUCCESS) {
121                 fprintf(stderr, "Failed to bind the tex coordinates buffer memory.\n");
122                 return false;
123         }
124
125         if(!vku_write_memory(vk_indices->mem_pool, isz, (void *)indices.data())) {
126                 fprintf(stderr, "Failed to write the indices to GPU.\n");
127                 return false;
128         }
129         if(vkBindBufferMemory(vk_device, vk_indices->buf, vk_indices->mem_pool, 0)
130                 != VK_SUCCESS) {
131                 fprintf(stderr, "Failed to bind the index buffer memory.\n");
132                 return false;
133         }
134
135         return true;
136 }
137
138 void MeshVK::draw() const
139 {
140 }
141
142 void MeshVK::draw_normals(float scale) const
143 {
144 }