procedural-3d-engine/mesh/mesh.cpp

668 lines
20 KiB
C++
Raw Normal View History

2016-02-16 15:07:25 +01:00
/*
2017-02-11 09:41:14 +01:00
* Vulkan Example - Model loading and rendering
2016-02-16 15:07:25 +01:00
*
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
*
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <vector>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
2016-02-16 15:07:25 +01:00
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <assimp/cimport.h>
2016-02-16 15:07:25 +01:00
#include <vulkan/vulkan.h>
#include "vulkanexamplebase.h"
#include "VulkanTexture.hpp"
2016-02-16 15:07:25 +01:00
#define VERTEX_BUFFER_BIND_ID 0
#define ENABLE_VALIDATION false
class VulkanExample : public VulkanExampleBase
{
public:
2016-05-15 11:13:57 +02:00
bool wireframe = false;
2016-02-16 15:07:25 +01:00
struct {
vks::Texture2D colorMap;
2016-02-16 15:07:25 +01:00
} textures;
struct {
VkPipelineVertexInputStateCreateInfo inputState;
std::vector<VkVertexInputBindingDescription> bindingDescriptions;
std::vector<VkVertexInputAttributeDescription> attributeDescriptions;
} vertices;
2017-02-11 09:41:14 +01:00
// Vertex layout used in this example
// This must fit input locations of the vertex shader used to render the model
struct Vertex {
glm::vec3 pos;
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 {
2016-02-16 15:07:25 +01:00
struct {
2017-02-11 09:41:14 +01:00
VkBuffer buffer;
VkDeviceMemory memory;
2016-02-16 15:07:25 +01:00
} vertices;
struct {
int count;
2017-02-11 09:41:14 +01:00
VkBuffer buffer;
VkDeviceMemory memory;
2016-02-16 15:07:25 +01:00
} indices;
2017-02-11 09:41:14 +01:00
// 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;
2016-02-16 15:07:25 +01:00
struct {
vks::Buffer scene;
} uniformBuffers;
2016-02-16 15:07:25 +01:00
struct {
glm::mat4 projection;
glm::mat4 model;
2016-05-15 11:13:57 +02:00
glm::vec4 lightPos = glm::vec4(25.0f, 5.0f, 5.0f, 1.0f);
2016-02-16 15:07:25 +01:00
} uboVS;
struct {
VkPipeline solid;
2016-05-15 11:13:57 +02:00
VkPipeline wireframe;
2016-02-16 15:07:25 +01:00
} pipelines;
VkPipelineLayout pipelineLayout;
VkDescriptorSet descriptorSet;
VkDescriptorSetLayout descriptorSetLayout;
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
{
2016-05-15 11:13:57 +02:00
zoom = -5.5f;
2016-02-16 15:07:25 +01:00
zoomSpeed = 2.5f;
rotationSpeed = 0.5f;
2016-05-15 11:13:57 +02:00
rotation = { -0.5f, -112.75f, 0.0f };
cameraPos = { 0.1f, 1.1f, 0.0f };
2016-06-05 20:28:39 +02:00
enableTextOverlay = true;
2017-02-11 09:41:14 +01:00
title = "Vulkan Example - Model rendering";
// Enable physical device features required for this example
enabledFeatures.fillModeNonSolid = VK_TRUE;
2016-02-16 15:07:25 +01:00
}
~VulkanExample()
{
// Clean up used Vulkan resources
// Note : Inherited destructor cleans up resources stored in base class
vkDestroyPipeline(device, pipelines.solid, nullptr);
vkDestroyPipeline(device, pipelines.wireframe, nullptr);
2016-02-16 15:07:25 +01:00
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
2017-02-11 09:41:14 +01:00
model.destroy(device);
2016-02-16 15:07:25 +01:00
textures.colorMap.destroy();
uniformBuffers.scene.destroy();
2016-02-16 15:07:25 +01:00
}
2016-05-15 11:13:57 +02:00
void reBuildCommandBuffers()
{
if (!checkCommandBuffers())
{
destroyCommandBuffers();
createCommandBuffers();
}
buildCommandBuffers();
}
2016-02-16 15:07:25 +01:00
void buildCommandBuffers()
{
VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo();
VkClearValue clearValues[2];
clearValues[0].color = defaultClearColor;
clearValues[1].depthStencil = { 1.0f, 0 };
VkRenderPassBeginInfo renderPassBeginInfo = vkTools::initializers::renderPassBeginInfo();
renderPassBeginInfo.renderPass = renderPass;
renderPassBeginInfo.renderArea.offset.x = 0;
renderPassBeginInfo.renderArea.offset.y = 0;
renderPassBeginInfo.renderArea.extent.width = width;
renderPassBeginInfo.renderArea.extent.height = height;
renderPassBeginInfo.clearValueCount = 2;
renderPassBeginInfo.pClearValues = clearValues;
for (int32_t i = 0; i < drawCmdBuffers.size(); ++i)
{
// Set target frame buffer
renderPassBeginInfo.framebuffer = frameBuffers[i];
VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));
2016-02-16 15:07:25 +01:00
vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
VkViewport viewport = vkTools::initializers::viewport((float)width, (float)height, 0.0f, 1.0f);
2016-02-16 15:07:25 +01:00
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
VkRect2D scissor = vkTools::initializers::rect2D(width, height, 0, 0);
2016-02-16 15:07:25 +01:00
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL);
2016-05-15 11:13:57 +02:00
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, wireframe ? pipelines.wireframe : pipelines.solid);
2016-02-16 15:07:25 +01:00
VkDeviceSize offsets[1] = { 0 };
// Bind mesh vertex buffer
2017-02-11 09:41:14 +01:00
vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &model.vertices.buffer, offsets);
2016-02-16 15:07:25 +01:00
// Bind mesh index buffer
2017-02-11 09:41:14 +01:00
vkCmdBindIndexBuffer(drawCmdBuffers[i], model.indices.buffer, 0, VK_INDEX_TYPE_UINT32);
2016-02-16 15:07:25 +01:00
// Render mesh vertex buffer using it's indices
2017-02-11 09:41:14 +01:00
vkCmdDrawIndexed(drawCmdBuffers[i], model.indices.count, 1, 0, 0, 0);
2016-02-16 15:07:25 +01:00
vkCmdEndRenderPass(drawCmdBuffers[i]);
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
2016-02-16 15:07:25 +01:00
}
}
2017-02-11 09:41:14 +01:00
// Load a model from file using the ASSIMP model loader and generate all resources required to render the model
void loadModel(std::string filename)
2016-02-16 15:07:25 +01:00
{
// Load the model from file using ASSIMP
const aiScene* scene;
Assimp::Importer Importer;
// Flags for loading the mesh
static const int assimpFlags = aiProcess_FlipWindingOrder | aiProcess_Triangulate | aiProcess_PreTransformVertices;
#if defined(__ANDROID__)
// Meshes are stored inside the apk on Android (compressed)
// So they need to be loaded via the asset manager
AAsset* asset = AAssetManager_open(androidApp->activity->assetManager, filename.c_str(), AASSET_MODE_STREAMING);
assert(asset);
size_t size = AAsset_getLength(asset);
assert(size > 0);
void *meshData = malloc(size);
AAsset_read(asset, meshData, size);
AAsset_close(asset);
scene = Importer.ReadFileFromMemory(meshData, size, assimpFlags);
free(meshData);
#else
scene = Importer.ReadFile(filename.c_str(), assimpFlags);
#endif
2016-02-16 15:07:25 +01:00
// Generate vertex buffer from ASSIMP scene data
2016-02-16 15:07:25 +01:00
float scale = 1.0f;
std::vector<Vertex> vertexBuffer;
// Iterate through all meshes in the file and extract the vertex components
for (uint32_t m = 0; m < scene->mNumMeshes; m++)
2016-02-16 15:07:25 +01:00
{
for (uint32_t v = 0; v < scene->mMeshes[m]->mNumVertices; v++)
2016-02-16 15:07:25 +01:00
{
Vertex vertex;
// Use glm make_* functions to convert ASSIMP vectors to glm vectors
vertex.pos = glm::make_vec3(&scene->mMeshes[m]->mVertices[v].x) * scale;
vertex.normal = glm::make_vec3(&scene->mMeshes[m]->mNormals[v].x);
// Texture coordinates and colors may have multiple channels, we only use the first [0] one
vertex.uv = glm::make_vec2(&scene->mMeshes[m]->mTextureCoords[0][v].x);
// Mesh may not have vertex colors
vertex.color = (scene->mMeshes[m]->HasVertexColors(0)) ? glm::make_vec3(&scene->mMeshes[m]->mColors[0][v].r) : glm::vec3(1.0f);
// Vulkan uses a right-handed NDC (contrary to OpenGL), so simply flip Y-Axis
vertex.pos.y *= -1.0f;
2016-02-16 15:07:25 +01:00
vertexBuffer.push_back(vertex);
}
}
size_t vertexBufferSize = vertexBuffer.size() * sizeof(Vertex);
2016-02-16 15:07:25 +01:00
// Generate index buffer from ASSIMP scene data
2016-02-16 15:07:25 +01:00
std::vector<uint32_t> indexBuffer;
for (uint32_t m = 0; m < scene->mNumMeshes; m++)
2016-02-16 15:07:25 +01:00
{
uint32_t indexBase = static_cast<uint32_t>(indexBuffer.size());
for (uint32_t f = 0; f < scene->mMeshes[m]->mNumFaces; f++)
2016-02-16 15:07:25 +01:00
{
// We assume that all faces are triangulated
for (uint32_t i = 0; i < 3; i++)
{
indexBuffer.push_back(scene->mMeshes[m]->mFaces[f].mIndices[i] + indexBase);
}
2016-02-16 15:07:25 +01:00
}
}
size_t indexBufferSize = indexBuffer.size() * sizeof(uint32_t);
2017-02-11 09:41:14 +01:00
model.indices.count = static_cast<uint32_t>(indexBuffer.size());
2016-02-16 15:07:25 +01:00
// Static mesh should always be device local
2016-02-16 15:07:25 +01:00
bool useStaging = true;
if (useStaging)
{
struct {
VkBuffer buffer;
VkDeviceMemory memory;
} vertexStaging, indexStaging;
// Create staging buffers
// Vertex data
VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
vertexBufferSize,
&vertexStaging.buffer,
&vertexStaging.memory,
vertexBuffer.data()));
// Index data
VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
indexBufferSize,
&indexStaging.buffer,
&indexStaging.memory,
indexBuffer.data()));
// Create device local buffers
// Vertex buffer
VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
vertexBufferSize,
2017-02-11 09:41:14 +01:00
&model.vertices.buffer,
&model.vertices.memory));
// Index buffer
VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
indexBufferSize,
2017-02-11 09:41:14 +01:00
&model.indices.buffer,
&model.indices.memory));
// Copy from staging buffers
VkCommandBuffer copyCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
VkBufferCopy copyRegion = {};
copyRegion.size = vertexBufferSize;
vkCmdCopyBuffer(
copyCmd,
vertexStaging.buffer,
2017-02-11 09:41:14 +01:00
model.vertices.buffer,
1,
&copyRegion);
copyRegion.size = indexBufferSize;
vkCmdCopyBuffer(
copyCmd,
indexStaging.buffer,
2017-02-11 09:41:14 +01:00
model.indices.buffer,
1,
&copyRegion);
VulkanExampleBase::flushCommandBuffer(copyCmd, queue, true);
vkDestroyBuffer(device, vertexStaging.buffer, nullptr);
vkFreeMemory(device, vertexStaging.memory, nullptr);
vkDestroyBuffer(device, indexStaging.buffer, nullptr);
vkFreeMemory(device, indexStaging.memory, nullptr);
}
else
{
// Vertex buffer
VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
vertexBufferSize,
2017-02-11 09:41:14 +01:00
&model.vertices.buffer,
&model.vertices.memory,
vertexBuffer.data()));
// Index buffer
VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
indexBufferSize,
2017-02-11 09:41:14 +01:00
&model.indices.buffer,
&model.indices.memory,
indexBuffer.data()));
}
2016-02-16 15:07:25 +01:00
}
void loadAssets()
2016-02-16 15:07:25 +01:00
{
2017-02-11 09:41:14 +01:00
loadModel(getAssetPath() + "models/voyager/voyager.dae");
textures.colorMap.loadFromFile(getAssetPath() + "models/voyager/voyager.ktx", VK_FORMAT_BC3_UNORM_BLOCK, vulkanDevice, queue);
2016-02-16 15:07:25 +01:00
}
void setupVertexDescriptions()
{
// Binding description
vertices.bindingDescriptions.resize(1);
vertices.bindingDescriptions[0] =
vkTools::initializers::vertexInputBindingDescription(
VERTEX_BUFFER_BIND_ID,
sizeof(Vertex),
VK_VERTEX_INPUT_RATE_VERTEX);
// Attribute descriptions
// Describes memory layout and shader positions
vertices.attributeDescriptions.resize(4);
// Location 0 : Position
vertices.attributeDescriptions[0] =
vkTools::initializers::vertexInputAttributeDescription(
VERTEX_BUFFER_BIND_ID,
0,
VK_FORMAT_R32G32B32_SFLOAT,
offsetof(Vertex, pos));
2016-02-16 15:07:25 +01:00
// Location 1 : Normal
vertices.attributeDescriptions[1] =
vkTools::initializers::vertexInputAttributeDescription(
VERTEX_BUFFER_BIND_ID,
1,
VK_FORMAT_R32G32B32_SFLOAT,
offsetof(Vertex, normal));
2016-02-16 15:07:25 +01:00
// Location 2 : Texture coordinates
vertices.attributeDescriptions[2] =
vkTools::initializers::vertexInputAttributeDescription(
VERTEX_BUFFER_BIND_ID,
2,
VK_FORMAT_R32G32_SFLOAT,
offsetof(Vertex, uv));
2016-02-16 15:07:25 +01:00
// Location 3 : Color
vertices.attributeDescriptions[3] =
vkTools::initializers::vertexInputAttributeDescription(
VERTEX_BUFFER_BIND_ID,
3,
VK_FORMAT_R32G32B32_SFLOAT,
offsetof(Vertex, color));
2016-02-16 15:07:25 +01:00
vertices.inputState = vkTools::initializers::pipelineVertexInputStateCreateInfo();
vertices.inputState.vertexBindingDescriptionCount = static_cast<uint32_t>(vertices.bindingDescriptions.size());
2016-02-16 15:07:25 +01:00
vertices.inputState.pVertexBindingDescriptions = vertices.bindingDescriptions.data();
vertices.inputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(vertices.attributeDescriptions.size());
2016-02-16 15:07:25 +01:00
vertices.inputState.pVertexAttributeDescriptions = vertices.attributeDescriptions.data();
}
void setupDescriptorPool()
{
// Example uses one ubo and one combined image sampler
std::vector<VkDescriptorPoolSize> poolSizes =
{
vkTools::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1),
vkTools::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1),
};
VkDescriptorPoolCreateInfo descriptorPoolInfo =
vkTools::initializers::descriptorPoolCreateInfo(
static_cast<uint32_t>(poolSizes.size()),
2016-02-16 15:07:25 +01:00
poolSizes.data(),
1);
2016-02-16 15:07:25 +01:00
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
2016-02-16 15:07:25 +01:00
}
void setupDescriptorSetLayout()
{
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings =
{
// Binding 0 : Vertex shader uniform buffer
vkTools::initializers::descriptorSetLayoutBinding(
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
VK_SHADER_STAGE_VERTEX_BIT,
0),
// Binding 1 : Fragment shader combined sampler
vkTools::initializers::descriptorSetLayoutBinding(
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
VK_SHADER_STAGE_FRAGMENT_BIT,
1),
};
VkDescriptorSetLayoutCreateInfo descriptorLayout =
vkTools::initializers::descriptorSetLayoutCreateInfo(
setLayoutBindings.data(),
static_cast<uint32_t>(setLayoutBindings.size()));
2016-02-16 15:07:25 +01:00
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout));
2016-02-16 15:07:25 +01:00
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
vkTools::initializers::pipelineLayoutCreateInfo(
&descriptorSetLayout,
1);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout));
2016-02-16 15:07:25 +01:00
}
void setupDescriptorSet()
{
VkDescriptorSetAllocateInfo allocInfo =
vkTools::initializers::descriptorSetAllocateInfo(
descriptorPool,
&descriptorSetLayout,
1);
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet));
2016-02-16 15:07:25 +01:00
VkDescriptorImageInfo texDescriptor =
vkTools::initializers::descriptorImageInfo(
textures.colorMap.sampler,
textures.colorMap.view,
VK_IMAGE_LAYOUT_GENERAL);
std::vector<VkWriteDescriptorSet> writeDescriptorSets =
{
// Binding 0 : Vertex shader uniform buffer
vkTools::initializers::writeDescriptorSet(
descriptorSet,
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
0,
&uniformBuffers.scene.descriptor),
2016-02-16 15:07:25 +01:00
// Binding 1 : Color map
vkTools::initializers::writeDescriptorSet(
descriptorSet,
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1,
&texDescriptor)
};
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
2016-02-16 15:07:25 +01:00
}
void preparePipelines()
{
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState =
vkTools::initializers::pipelineInputAssemblyStateCreateInfo(
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
0,
VK_FALSE);
VkPipelineRasterizationStateCreateInfo rasterizationState =
vkTools::initializers::pipelineRasterizationStateCreateInfo(
VK_POLYGON_MODE_FILL,
VK_CULL_MODE_BACK_BIT,
VK_FRONT_FACE_CLOCKWISE,
0);
VkPipelineColorBlendAttachmentState blendAttachmentState =
vkTools::initializers::pipelineColorBlendAttachmentState(
0xf,
VK_FALSE);
VkPipelineColorBlendStateCreateInfo colorBlendState =
vkTools::initializers::pipelineColorBlendStateCreateInfo(
1,
&blendAttachmentState);
VkPipelineDepthStencilStateCreateInfo depthStencilState =
vkTools::initializers::pipelineDepthStencilStateCreateInfo(
VK_TRUE,
VK_TRUE,
VK_COMPARE_OP_LESS_OR_EQUAL);
VkPipelineViewportStateCreateInfo viewportState =
vkTools::initializers::pipelineViewportStateCreateInfo(1, 1, 0);
VkPipelineMultisampleStateCreateInfo multisampleState =
vkTools::initializers::pipelineMultisampleStateCreateInfo(
VK_SAMPLE_COUNT_1_BIT,
0);
std::vector<VkDynamicState> dynamicStateEnables = {
VK_DYNAMIC_STATE_VIEWPORT,
VK_DYNAMIC_STATE_SCISSOR
};
VkPipelineDynamicStateCreateInfo dynamicState =
vkTools::initializers::pipelineDynamicStateCreateInfo(
dynamicStateEnables.data(),
static_cast<uint32_t>(dynamicStateEnables.size()),
2016-02-16 15:07:25 +01:00
0);
// Solid rendering pipeline
// Load shaders
std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages;
shaderStages[0] = loadShader(getAssetPath() + "shaders/mesh/mesh.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
shaderStages[1] = loadShader(getAssetPath() + "shaders/mesh/mesh.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
2016-02-16 15:07:25 +01:00
VkGraphicsPipelineCreateInfo pipelineCreateInfo =
vkTools::initializers::pipelineCreateInfo(
pipelineLayout,
renderPass,
0);
pipelineCreateInfo.pVertexInputState = &vertices.inputState;
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
pipelineCreateInfo.pRasterizationState = &rasterizationState;
pipelineCreateInfo.pColorBlendState = &colorBlendState;
pipelineCreateInfo.pMultisampleState = &multisampleState;
pipelineCreateInfo.pViewportState = &viewportState;
pipelineCreateInfo.pDepthStencilState = &depthStencilState;
pipelineCreateInfo.pDynamicState = &dynamicState;
pipelineCreateInfo.stageCount = static_cast<uint32_t>(shaderStages.size());
2016-02-16 15:07:25 +01:00
pipelineCreateInfo.pStages = shaderStages.data();
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.solid));
2016-05-15 11:13:57 +02:00
// Wire frame rendering pipeline
rasterizationState.polygonMode = VK_POLYGON_MODE_LINE;
rasterizationState.lineWidth = 1.0f;
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.wireframe));
2016-02-16 15:07:25 +01:00
}
// Prepare and initialize uniform buffer containing shader uniforms
void prepareUniformBuffers()
{
// Vertex shader uniform buffer block
VK_CHECK_RESULT(vulkanDevice->createBuffer(
2016-02-16 15:07:25 +01:00
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
2016-06-05 20:28:39 +02:00
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&uniformBuffers.scene,
sizeof(uboVS)));
// Map persistent
VK_CHECK_RESULT(uniformBuffers.scene.map());
2016-02-16 15:07:25 +01:00
updateUniformBuffers();
}
void updateUniformBuffers()
{
uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.1f, 256.0f);
glm::mat4 viewMatrix = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, zoom));
2016-02-16 15:07:25 +01:00
uboVS.model = viewMatrix * glm::translate(glm::mat4(), cameraPos);
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
2016-02-16 15:07:25 +01:00
memcpy(uniformBuffers.scene.mapped, &uboVS, sizeof(uboVS));
2016-02-16 15:07:25 +01:00
}
2016-06-05 20:28:39 +02:00
void draw()
{
VulkanExampleBase::prepareFrame();
// Command buffer to be sumitted to the queue
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
// Submit to queue
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
VulkanExampleBase::submitFrame();
}
2016-02-16 15:07:25 +01:00
void prepare()
{
VulkanExampleBase::prepare();
loadAssets();
2016-02-16 15:07:25 +01:00
setupVertexDescriptions();
prepareUniformBuffers();
setupDescriptorSetLayout();
preparePipelines();
setupDescriptorPool();
setupDescriptorSet();
buildCommandBuffers();
prepared = true;
}
virtual void render()
{
if (!prepared)
return;
draw();
}
virtual void viewChanged()
{
updateUniformBuffers();
}
2016-05-15 11:13:57 +02:00
virtual void keyPressed(uint32_t keyCode)
{
switch (keyCode)
{
case KEY_W:
2016-05-15 11:13:57 +02:00
case GAMEPAD_BUTTON_A:
wireframe = !wireframe;
reBuildCommandBuffers();
break;
}
}
2016-06-05 20:28:39 +02:00
virtual void getOverlayText(VulkanTextOverlay *textOverlay)
{
#if defined(__ANDROID__)
textOverlay->addText("Press \"Button A\" to toggle wireframe", 5.0f, 85.0f, VulkanTextOverlay::alignLeft);
#else
textOverlay->addText("Press \"w\" to toggle wireframe", 5.0f, 85.0f, VulkanTextOverlay::alignLeft);
#endif
}
2016-02-16 15:07:25 +01:00
};
VULKAN_EXAMPLE_MAIN()