Use createBuffer function from Vulkan device class

This commit is contained in:
saschawillems 2017-01-07 19:37:36 +01:00
parent 6e19ea926a
commit 1fa3364513

View file

@ -45,11 +45,11 @@ public:
std::vector<VkVertexInputAttributeDescription> attributeDescriptions; std::vector<VkVertexInputAttributeDescription> attributeDescriptions;
} vertices; } vertices;
struct { vk::Buffer vertexBuffer;
vkMeshLoader::MeshBuffer quad; vk::Buffer indexBuffer;
} meshes; uint32_t indexCount;
vk::Buffer uniformBuffer; vk::Buffer uniformBufferVS;
struct UboInstanceData { struct UboInstanceData {
// Model matrix // Model matrix
@ -100,9 +100,10 @@ public:
vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
vkMeshLoader::freeMeshBufferResources(device, &meshes.quad); vertexBuffer.destroy();
indexBuffer.destroy();
uniformBuffer.destroy(); uniformBufferVS.destroy();
delete[] uboVS.instance; delete[] uboVS.instance;
} }
@ -349,13 +350,13 @@ public:
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor); vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL); vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL);
VkDeviceSize offsets[1] = { 0 };
vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &meshes.quad.vertices.buf, offsets);
vkCmdBindIndexBuffer(drawCmdBuffers[i], meshes.quad.indices.buf, 0, VK_INDEX_TYPE_UINT32);
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline); vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
vkCmdDrawIndexed(drawCmdBuffers[i], meshes.quad.indexCount, layerCount, 0, 0, 0); VkDeviceSize offsets[1] = { 0 };
vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &vertexBuffer.buffer, offsets);
vkCmdBindIndexBuffer(drawCmdBuffers[i], indexBuffer.buffer, 0, VK_INDEX_TYPE_UINT32);
vkCmdDrawIndexed(drawCmdBuffers[i], indexCount, layerCount, 0, 0, 0);
vkCmdEndRenderPass(drawCmdBuffers[i]); vkCmdEndRenderPass(drawCmdBuffers[i]);
@ -363,36 +364,37 @@ public:
} }
} }
// Setup vertices for a single uv-mapped quad
void generateQuad() void generateQuad()
{ {
#define dim 2.5f // Setup vertices for a single uv-mapped quad made from two triangles
std::vector<Vertex> vertexBuffer = std::vector<Vertex> vertices =
{ {
{ { dim, dim, 0.0f }, { 1.0f, 1.0f } }, { { 2.5f, 2.5f, 0.0f }, { 1.0f, 1.0f } },
{ { -dim, dim, 0.0f }, { 0.0f, 1.0f } }, { { -2.5f, 2.5f, 0.0f }, { 0.0f, 1.0f } },
{ { -dim, -dim, 0.0f }, { 0.0f, 0.0f } }, { { -2.5f, -2.5f, 0.0f }, { 0.0f, 0.0f } },
{ { dim, -dim, 0.0f }, { 1.0f, 0.0f } } { { 2.5f, -2.5f, 0.0f }, { 1.0f, 0.0f } }
}; };
#undef dim
createBuffer(
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
vertexBuffer.size() * sizeof(Vertex),
vertexBuffer.data(),
&meshes.quad.vertices.buf,
&meshes.quad.vertices.mem);
// Setup indices // Setup indices
std::vector<uint32_t> indexBuffer = { 0,1,2, 2,3,0 }; std::vector<uint32_t> indices = { 0,1,2, 2,3,0 };
meshes.quad.indexCount = indexBuffer.size(); indexCount = static_cast<uint32_t>(indices.size());
createBuffer( // Create buffers
// For the sake of simplicity we won't stage the vertex data to the gpu memory
// Vertex buffer
VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&vertexBuffer,
vertices.size() * sizeof(Vertex),
vertices.data()));
// Index buffer
VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_INDEX_BUFFER_BIT, VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
indexBuffer.size() * sizeof(uint32_t), VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
indexBuffer.data(), &indexBuffer,
&meshes.quad.indices.buf, indices.size() * sizeof(uint32_t),
&meshes.quad.indices.mem); indices.data()));
} }
void setupVertexDescriptions() void setupVertexDescriptions()
@ -502,7 +504,7 @@ public:
descriptorSet, descriptorSet,
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
0, 0,
&uniformBuffer.descriptor), &uniformBufferVS.descriptor),
// Binding 1 : Fragment shader cubemap sampler // Binding 1 : Fragment shader cubemap sampler
vkTools::initializers::writeDescriptorSet( vkTools::initializers::writeDescriptorSet(
descriptorSet, descriptorSet,
@ -600,7 +602,7 @@ public:
VK_CHECK_RESULT(vulkanDevice->createBuffer( VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&uniformBuffer, &uniformBufferVS,
uboSize)); uboSize));
// Array indices and model matrices are fixed // Array indices and model matrices are fixed
@ -620,12 +622,12 @@ public:
uint8_t *pData; uint8_t *pData;
uint32_t dataOffset = sizeof(uboVS.matrices); uint32_t dataOffset = sizeof(uboVS.matrices);
uint32_t dataSize = layerCount * sizeof(UboInstanceData); uint32_t dataSize = layerCount * sizeof(UboInstanceData);
VK_CHECK_RESULT(vkMapMemory(device, uniformBuffer.memory, dataOffset, dataSize, 0, (void **)&pData)); VK_CHECK_RESULT(vkMapMemory(device, uniformBufferVS.memory, dataOffset, dataSize, 0, (void **)&pData));
memcpy(pData, uboVS.instance, dataSize); memcpy(pData, uboVS.instance, dataSize);
vkUnmapMemory(device, uniformBuffer.memory); vkUnmapMemory(device, uniformBufferVS.memory);
// Map persistent // Map persistent
VK_CHECK_RESULT(uniformBuffer.map()); VK_CHECK_RESULT(uniformBufferVS.map());
updateUniformBufferMatrices(); updateUniformBufferMatrices();
} }
@ -644,7 +646,7 @@ public:
uboVS.matrices.view = glm::rotate(uboVS.matrices.view, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); uboVS.matrices.view = glm::rotate(uboVS.matrices.view, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
// Only update the matrices part of the uniform buffer // Only update the matrices part of the uniform buffer
memcpy(uniformBuffer.mapped, &uboVS.matrices, sizeof(uboVS.matrices)); memcpy(uniformBufferVS.mapped, &uboVS.matrices, sizeof(uboVS.matrices));
} }
void draw() void draw()