Code cleanup, refactoring
This commit is contained in:
parent
a91be23d76
commit
9c6e30fdd6
1 changed files with 45 additions and 48 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Vulkan Example - Mesh rendering and loading using ASSIMP
|
* Vulkan Example - Model loading and rendering
|
||||||
*
|
*
|
||||||
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
|
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
|
||||||
*
|
*
|
||||||
|
|
@ -30,14 +30,6 @@
|
||||||
#define VERTEX_BUFFER_BIND_ID 0
|
#define VERTEX_BUFFER_BIND_ID 0
|
||||||
#define ENABLE_VALIDATION false
|
#define ENABLE_VALIDATION false
|
||||||
|
|
||||||
// Vertex layout used in this example
|
|
||||||
struct Vertex {
|
|
||||||
glm::vec3 pos;
|
|
||||||
glm::vec3 normal;
|
|
||||||
glm::vec2 uv;
|
|
||||||
glm::vec3 color;
|
|
||||||
};
|
|
||||||
|
|
||||||
class VulkanExample : public VulkanExampleBase
|
class VulkanExample : public VulkanExampleBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
@ -53,21 +45,36 @@ public:
|
||||||
std::vector<VkVertexInputAttributeDescription> attributeDescriptions;
|
std::vector<VkVertexInputAttributeDescription> attributeDescriptions;
|
||||||
} vertices;
|
} vertices;
|
||||||
|
|
||||||
// Contains all buffers and information
|
// Vertex layout used in this example
|
||||||
// necessary to represent a mesh for rendering purposes
|
// This must fit input locations of the vertex shader used to render the model
|
||||||
// This is for demonstration and learning purposes,
|
struct Vertex {
|
||||||
// the other examples use a mesh loader class for easy access
|
glm::vec3 pos;
|
||||||
struct Mesh {
|
glm::vec3 normal;
|
||||||
|
glm::vec2 uv;
|
||||||
|
glm::vec3 color;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Contains all Vulkan resources required to represent vertex and index buffers for a model
|
||||||
|
// This is for demonstration and learning purposes, the other examples use a model loader class for easy access
|
||||||
|
struct Model {
|
||||||
struct {
|
struct {
|
||||||
VkBuffer buf;
|
VkBuffer buffer;
|
||||||
VkDeviceMemory mem;
|
VkDeviceMemory memory;
|
||||||
} vertices;
|
} vertices;
|
||||||
struct {
|
struct {
|
||||||
int count;
|
int count;
|
||||||
VkBuffer buf;
|
VkBuffer buffer;
|
||||||
VkDeviceMemory mem;
|
VkDeviceMemory memory;
|
||||||
} indices;
|
} indices;
|
||||||
} mesh;
|
// Destroys all Vulkan resources created for this model
|
||||||
|
void destroy(VkDevice device)
|
||||||
|
{
|
||||||
|
vkDestroyBuffer(device, vertices.buffer, nullptr);
|
||||||
|
vkFreeMemory(device, vertices.memory, nullptr);
|
||||||
|
vkDestroyBuffer(device, indices.buffer, nullptr);
|
||||||
|
vkFreeMemory(device, indices.memory, nullptr);
|
||||||
|
};
|
||||||
|
} model;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
vk::Buffer scene;
|
vk::Buffer scene;
|
||||||
|
|
@ -96,11 +103,8 @@ public:
|
||||||
rotation = { -0.5f, -112.75f, 0.0f };
|
rotation = { -0.5f, -112.75f, 0.0f };
|
||||||
cameraPos = { 0.1f, 1.1f, 0.0f };
|
cameraPos = { 0.1f, 1.1f, 0.0f };
|
||||||
enableTextOverlay = true;
|
enableTextOverlay = true;
|
||||||
title = "Vulkan Example - Mesh rendering";
|
title = "Vulkan Example - Model rendering";
|
||||||
// Enable physical device features required for this example
|
// Enable physical device features required for this example
|
||||||
// Tell the driver that we are going to use geometry shaders
|
|
||||||
enabledFeatures.tessellationShader = VK_TRUE;
|
|
||||||
// Example also uses a wireframe pipeline, enable non-solid fill modes
|
|
||||||
enabledFeatures.fillModeNonSolid = VK_TRUE;
|
enabledFeatures.fillModeNonSolid = VK_TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -114,11 +118,7 @@ public:
|
||||||
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
||||||
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
|
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
|
||||||
|
|
||||||
// Destroy and free mesh resources
|
model.destroy(device);
|
||||||
vkDestroyBuffer(device, mesh.vertices.buf, nullptr);
|
|
||||||
vkFreeMemory(device, mesh.vertices.mem, nullptr);
|
|
||||||
vkDestroyBuffer(device, mesh.indices.buf, nullptr);
|
|
||||||
vkFreeMemory(device, mesh.indices.mem, nullptr);
|
|
||||||
|
|
||||||
textures.colorMap.destroy();
|
textures.colorMap.destroy();
|
||||||
uniformBuffers.scene.destroy();
|
uniformBuffers.scene.destroy();
|
||||||
|
|
@ -171,11 +171,11 @@ public:
|
||||||
|
|
||||||
VkDeviceSize offsets[1] = { 0 };
|
VkDeviceSize offsets[1] = { 0 };
|
||||||
// Bind mesh vertex buffer
|
// Bind mesh vertex buffer
|
||||||
vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &mesh.vertices.buf, offsets);
|
vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &model.vertices.buffer, offsets);
|
||||||
// Bind mesh index buffer
|
// Bind mesh index buffer
|
||||||
vkCmdBindIndexBuffer(drawCmdBuffers[i], mesh.indices.buf, 0, VK_INDEX_TYPE_UINT32);
|
vkCmdBindIndexBuffer(drawCmdBuffers[i], model.indices.buffer, 0, VK_INDEX_TYPE_UINT32);
|
||||||
// Render mesh vertex buffer using it's indices
|
// Render mesh vertex buffer using it's indices
|
||||||
vkCmdDrawIndexed(drawCmdBuffers[i], mesh.indices.count, 1, 0, 0, 0);
|
vkCmdDrawIndexed(drawCmdBuffers[i], model.indices.count, 1, 0, 0, 0);
|
||||||
|
|
||||||
vkCmdEndRenderPass(drawCmdBuffers[i]);
|
vkCmdEndRenderPass(drawCmdBuffers[i]);
|
||||||
|
|
||||||
|
|
@ -183,12 +183,9 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load a mesh based on data read via assimp
|
// Load a model from file using the ASSIMP model loader and generate all resources required to render the model
|
||||||
// The other example will use the VulkanMesh loader which has some additional functionality for loading meshes
|
void loadModel(std::string filename)
|
||||||
void loadMesh()
|
|
||||||
{
|
{
|
||||||
std::string filename = getAssetPath() + "models/voyager/voyager.dae";
|
|
||||||
|
|
||||||
// Load the model from file using ASSIMP
|
// Load the model from file using ASSIMP
|
||||||
|
|
||||||
const aiScene* scene;
|
const aiScene* scene;
|
||||||
|
|
@ -260,7 +257,7 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
size_t indexBufferSize = indexBuffer.size() * sizeof(uint32_t);
|
size_t indexBufferSize = indexBuffer.size() * sizeof(uint32_t);
|
||||||
mesh.indices.count = static_cast<uint32_t>(indexBuffer.size());
|
model.indices.count = static_cast<uint32_t>(indexBuffer.size());
|
||||||
|
|
||||||
// Static mesh should always be device local
|
// Static mesh should always be device local
|
||||||
|
|
||||||
|
|
@ -297,15 +294,15 @@ public:
|
||||||
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
||||||
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
|
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
|
||||||
vertexBufferSize,
|
vertexBufferSize,
|
||||||
&mesh.vertices.buf,
|
&model.vertices.buffer,
|
||||||
&mesh.vertices.mem));
|
&model.vertices.memory));
|
||||||
// Index buffer
|
// Index buffer
|
||||||
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
||||||
VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
|
||||||
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
|
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
|
||||||
indexBufferSize,
|
indexBufferSize,
|
||||||
&mesh.indices.buf,
|
&model.indices.buffer,
|
||||||
&mesh.indices.mem));
|
&model.indices.memory));
|
||||||
|
|
||||||
// Copy from staging buffers
|
// Copy from staging buffers
|
||||||
VkCommandBuffer copyCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
|
VkCommandBuffer copyCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
|
||||||
|
|
@ -316,7 +313,7 @@ public:
|
||||||
vkCmdCopyBuffer(
|
vkCmdCopyBuffer(
|
||||||
copyCmd,
|
copyCmd,
|
||||||
vertexStaging.buffer,
|
vertexStaging.buffer,
|
||||||
mesh.vertices.buf,
|
model.vertices.buffer,
|
||||||
1,
|
1,
|
||||||
©Region);
|
©Region);
|
||||||
|
|
||||||
|
|
@ -324,7 +321,7 @@ public:
|
||||||
vkCmdCopyBuffer(
|
vkCmdCopyBuffer(
|
||||||
copyCmd,
|
copyCmd,
|
||||||
indexStaging.buffer,
|
indexStaging.buffer,
|
||||||
mesh.indices.buf,
|
model.indices.buffer,
|
||||||
1,
|
1,
|
||||||
©Region);
|
©Region);
|
||||||
|
|
||||||
|
|
@ -342,22 +339,23 @@ public:
|
||||||
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
|
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
|
||||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
|
||||||
vertexBufferSize,
|
vertexBufferSize,
|
||||||
&mesh.vertices.buf,
|
&model.vertices.buffer,
|
||||||
&mesh.vertices.mem,
|
&model.vertices.memory,
|
||||||
vertexBuffer.data()));
|
vertexBuffer.data()));
|
||||||
// Index buffer
|
// Index buffer
|
||||||
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
||||||
VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
|
VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
|
||||||
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
|
||||||
indexBufferSize,
|
indexBufferSize,
|
||||||
&mesh.indices.buf,
|
&model.indices.buffer,
|
||||||
&mesh.indices.mem,
|
&model.indices.memory,
|
||||||
indexBuffer.data()));
|
indexBuffer.data()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void loadAssets()
|
void loadAssets()
|
||||||
{
|
{
|
||||||
|
loadModel(getAssetPath() + "models/voyager/voyager.dae");
|
||||||
textures.colorMap.loadFromFile(getAssetPath() + "models/voyager/voyager.ktx", VK_FORMAT_BC3_UNORM_BLOCK, vulkanDevice, queue);
|
textures.colorMap.loadFromFile(getAssetPath() + "models/voyager/voyager.ktx", VK_FORMAT_BC3_UNORM_BLOCK, vulkanDevice, queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -623,7 +621,6 @@ public:
|
||||||
{
|
{
|
||||||
VulkanExampleBase::prepare();
|
VulkanExampleBase::prepare();
|
||||||
loadAssets();
|
loadAssets();
|
||||||
loadMesh();
|
|
||||||
setupVertexDescriptions();
|
setupVertexDescriptions();
|
||||||
prepareUniformBuffers();
|
prepareUniformBuffers();
|
||||||
setupDescriptorSetLayout();
|
setupDescriptorSetLayout();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue