Moved example source files into sub folder

This commit is contained in:
saschawillems 2017-11-12 19:32:09 +01:00
parent a17e3924b3
commit 94a076e1ae
69 changed files with 685 additions and 164 deletions

375
examples/gears/gears.cpp Normal file
View file

@ -0,0 +1,375 @@
/*
* Vulkan Example - Animated gears using multiple uniform buffers
*
* 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
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <vulkan/vulkan.h>
#include "vulkangear.h"
#include "vulkanexamplebase.h"
#define VERTEX_BUFFER_BIND_ID 0
#define ENABLE_VALIDATION false
class VulkanExample : public VulkanExampleBase
{
public:
struct {
VkPipelineVertexInputStateCreateInfo inputState;
std::vector<VkVertexInputBindingDescription> bindingDescriptions;
std::vector<VkVertexInputAttributeDescription> attributeDescriptions;
} vertices;
struct {
VkPipeline solid;
} pipelines;
std::vector<VulkanGear*> gears;
VkPipelineLayout pipelineLayout;
VkDescriptorSetLayout descriptorSetLayout;
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
{
zoom = -16.0f;
rotation = glm::vec3(-23.75f, 41.25f, 21.0f);
timerSpeed *= 0.25f;
title = "Rotating gears";
settings.overlay = true;
}
~VulkanExample()
{
// Clean up used Vulkan resources
// Note : Inherited destructor cleans up resources stored in base class
vkDestroyPipeline(device, pipelines.solid, nullptr);
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
for (auto& gear : gears)
{
delete(gear);
}
}
void buildCommandBuffers()
{
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
VkClearValue clearValues[2];
clearValues[0].color = defaultClearColor;
clearValues[1].depthStencil = { 1.0f, 0 };
VkRenderPassBeginInfo renderPassBeginInfo = vks::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)
{
renderPassBeginInfo.framebuffer = frameBuffers[i];
VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));
vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
VkViewport viewport = vks::initializers::viewport((float)width, (float)height, 0.0f, 1.0f);
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
VkRect2D scissor = vks::initializers::rect2D(width, height, 0, 0);
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.solid);
for (auto& gear : gears)
{
gear->draw(drawCmdBuffers[i], pipelineLayout);
}
vkCmdEndRenderPass(drawCmdBuffers[i]);
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
}
}
void prepareVertices()
{
// Gear definitions
std::vector<float> innerRadiuses = { 1.0f, 0.5f, 1.3f };
std::vector<float> outerRadiuses = { 4.0f, 2.0f, 2.0f };
std::vector<float> widths = { 1.0f, 2.0f, 0.5f };
std::vector<int32_t> toothCount = { 20, 10, 10 };
std::vector<float> toothDepth = { 0.7f, 0.7f, 0.7f };
std::vector<glm::vec3> colors = {
glm::vec3(1.0f, 0.0f, 0.0f),
glm::vec3(0.0f, 1.0f, 0.2f),
glm::vec3(0.0f, 0.0f, 1.0f)
};
std::vector<glm::vec3> positions = {
glm::vec3(-3.0, 0.0, 0.0),
glm::vec3(3.1, 0.0, 0.0),
glm::vec3(-3.1, -6.2, 0.0)
};
std::vector<float> rotationSpeeds = { 1.0f, -2.0f, -2.0f };
std::vector<float> rotationOffsets = { 0.0f, -9.0f, -30.0f };
gears.resize(positions.size());
for (int32_t i = 0; i < gears.size(); ++i)
{
GearInfo gearInfo = {};
gearInfo.innerRadius = innerRadiuses[i];
gearInfo.outerRadius = outerRadiuses[i];
gearInfo.width = widths[i];
gearInfo.numTeeth = toothCount[i];
gearInfo.toothDepth = toothDepth[i];
gearInfo.color = colors[i];
gearInfo.pos = positions[i];
gearInfo.rotSpeed = rotationSpeeds[i];
gearInfo.rotOffset = rotationOffsets[i];
gears[i] = new VulkanGear(vulkanDevice);
gears[i]->generate(&gearInfo, queue);
}
// Binding and attribute descriptions are shared across all gears
vertices.bindingDescriptions.resize(1);
vertices.bindingDescriptions[0] =
vks::initializers::vertexInputBindingDescription(
VERTEX_BUFFER_BIND_ID,
sizeof(Vertex),
VK_VERTEX_INPUT_RATE_VERTEX);
// Attribute descriptions
// Describes memory layout and shader positions
vertices.attributeDescriptions.resize(3);
// Location 0 : Position
vertices.attributeDescriptions[0] =
vks::initializers::vertexInputAttributeDescription(
VERTEX_BUFFER_BIND_ID,
0,
VK_FORMAT_R32G32B32_SFLOAT,
0);
// Location 1 : Normal
vertices.attributeDescriptions[1] =
vks::initializers::vertexInputAttributeDescription(
VERTEX_BUFFER_BIND_ID,
1,
VK_FORMAT_R32G32B32_SFLOAT,
sizeof(float) * 3);
// Location 2 : Color
vertices.attributeDescriptions[2] =
vks::initializers::vertexInputAttributeDescription(
VERTEX_BUFFER_BIND_ID,
2,
VK_FORMAT_R32G32B32_SFLOAT,
sizeof(float) * 6);
vertices.inputState = vks::initializers::pipelineVertexInputStateCreateInfo();
vertices.inputState.vertexBindingDescriptionCount = static_cast<uint32_t>(vertices.bindingDescriptions.size());
vertices.inputState.pVertexBindingDescriptions = vertices.bindingDescriptions.data();
vertices.inputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(vertices.attributeDescriptions.size());
vertices.inputState.pVertexAttributeDescriptions = vertices.attributeDescriptions.data();
}
void setupDescriptorPool()
{
// One UBO for each gear
std::vector<VkDescriptorPoolSize> poolSizes =
{
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 3),
};
VkDescriptorPoolCreateInfo descriptorPoolInfo =
vks::initializers::descriptorPoolCreateInfo(
static_cast<uint32_t>(poolSizes.size()),
poolSizes.data(),
// Three descriptor sets (for each gear)
3);
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
}
void setupDescriptorSetLayout()
{
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings =
{
// Binding 0 : Vertex shader uniform buffer
vks::initializers::descriptorSetLayoutBinding(
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
VK_SHADER_STAGE_VERTEX_BIT,
0)
};
VkDescriptorSetLayoutCreateInfo descriptorLayout =
vks::initializers::descriptorSetLayoutCreateInfo(
setLayoutBindings.data(),
static_cast<uint32_t>(setLayoutBindings.size()));
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout));
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
vks::initializers::pipelineLayoutCreateInfo(
&descriptorSetLayout,
1);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout));
}
void setupDescriptorSets()
{
for (auto& gear : gears)
{
gear->setupDescriptorSet(descriptorPool, descriptorSetLayout);
}
}
void preparePipelines()
{
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState =
vks::initializers::pipelineInputAssemblyStateCreateInfo(
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
0,
VK_FALSE);
VkPipelineRasterizationStateCreateInfo rasterizationState =
vks::initializers::pipelineRasterizationStateCreateInfo(
VK_POLYGON_MODE_FILL,
VK_CULL_MODE_BACK_BIT,
VK_FRONT_FACE_CLOCKWISE,
0);
VkPipelineColorBlendAttachmentState blendAttachmentState =
vks::initializers::pipelineColorBlendAttachmentState(
0xf,
VK_FALSE);
VkPipelineColorBlendStateCreateInfo colorBlendState =
vks::initializers::pipelineColorBlendStateCreateInfo(
1,
&blendAttachmentState);
VkPipelineDepthStencilStateCreateInfo depthStencilState =
vks::initializers::pipelineDepthStencilStateCreateInfo(
VK_TRUE,
VK_TRUE,
VK_COMPARE_OP_LESS_OR_EQUAL);
VkPipelineViewportStateCreateInfo viewportState =
vks::initializers::pipelineViewportStateCreateInfo(1, 1, 0);
VkPipelineMultisampleStateCreateInfo multisampleState =
vks::initializers::pipelineMultisampleStateCreateInfo(
VK_SAMPLE_COUNT_1_BIT,
0);
std::vector<VkDynamicState> dynamicStateEnables = {
VK_DYNAMIC_STATE_VIEWPORT,
VK_DYNAMIC_STATE_SCISSOR
};
VkPipelineDynamicStateCreateInfo dynamicState =
vks::initializers::pipelineDynamicStateCreateInfo(
dynamicStateEnables.data(),
static_cast<uint32_t>(dynamicStateEnables.size()),
0);
// Solid rendering pipeline
// Load shaders
std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages;
shaderStages[0] = loadShader(getAssetPath() + "shaders/gears/gears.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
shaderStages[1] = loadShader(getAssetPath() + "shaders/gears/gears.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
VkGraphicsPipelineCreateInfo pipelineCreateInfo =
vks::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());
pipelineCreateInfo.pStages = shaderStages.data();
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.solid));
}
void updateUniformBuffers()
{
glm::mat4 perspective = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.001f, 256.0f);
for (auto& gear : gears)
{
gear->updateUniformBuffer(perspective, rotation, zoom, timer * 360.0f);
}
}
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();
}
void prepare()
{
VulkanExampleBase::prepare();
prepareVertices();
setupDescriptorSetLayout();
preparePipelines();
setupDescriptorPool();
setupDescriptorSets();
updateUniformBuffers();
buildCommandBuffers();
prepared = true;
}
virtual void render()
{
if (!prepared)
return;
vkDeviceWaitIdle(device);
draw();
vkDeviceWaitIdle(device);
if (!paused)
{
updateUniformBuffers();
}
}
virtual void viewChanged()
{
updateUniformBuffers();
}
};
VULKAN_EXAMPLE_MAIN()

View file

@ -0,0 +1,324 @@
/*
* Vulkan Example - Animated gears using multiple uniform buffers
*
* See readme.md for details
*
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
*
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/
#include "vulkangear.h"
int32_t VulkanGear::newVertex(std::vector<Vertex> *vBuffer, float x, float y, float z, const glm::vec3& normal)
{
Vertex v(glm::vec3(x, y, z), normal, color);
vBuffer->push_back(v);
return static_cast<int32_t>(vBuffer->size()) - 1;
}
void VulkanGear::newFace(std::vector<uint32_t> *iBuffer, int a, int b, int c)
{
iBuffer->push_back(a);
iBuffer->push_back(b);
iBuffer->push_back(c);
}
VulkanGear::~VulkanGear()
{
// Clean up vulkan resources
uniformBuffer.destroy();
vertexBuffer.destroy();
indexBuffer.destroy();
}
void VulkanGear::generate(GearInfo *gearinfo, VkQueue queue)
{
this->color = gearinfo->color;
this->pos = gearinfo->pos;
this->rotOffset = gearinfo->rotOffset;
this->rotSpeed = gearinfo->rotSpeed;
std::vector<Vertex> vBuffer;
std::vector<uint32_t> iBuffer;
int i, j;
float r0, r1, r2;
float ta, da;
float u1, v1, u2, v2, len;
float cos_ta, cos_ta_1da, cos_ta_2da, cos_ta_3da, cos_ta_4da;
float sin_ta, sin_ta_1da, sin_ta_2da, sin_ta_3da, sin_ta_4da;
int32_t ix0, ix1, ix2, ix3, ix4, ix5;
r0 = gearinfo->innerRadius;
r1 = gearinfo->outerRadius - gearinfo->toothDepth / 2.0f;
r2 = gearinfo->outerRadius + gearinfo->toothDepth / 2.0f;
da = 2.0f * M_PI / gearinfo->numTeeth / 4.0f;
glm::vec3 normal;
for (i = 0; i < gearinfo->numTeeth; i++)
{
ta = i * 2.0f * M_PI / gearinfo->numTeeth;
cos_ta = cos(ta);
cos_ta_1da = cos(ta + da);
cos_ta_2da = cos(ta + 2.0f * da);
cos_ta_3da = cos(ta + 3.0f * da);
cos_ta_4da = cos(ta + 4.0f * da);
sin_ta = sin(ta);
sin_ta_1da = sin(ta + da);
sin_ta_2da = sin(ta + 2.0f * da);
sin_ta_3da = sin(ta + 3.0f * da);
sin_ta_4da = sin(ta + 4.0f * da);
u1 = r2 * cos_ta_1da - r1 * cos_ta;
v1 = r2 * sin_ta_1da - r1 * sin_ta;
len = sqrt(u1 * u1 + v1 * v1);
u1 /= len;
v1 /= len;
u2 = r1 * cos_ta_3da - r2 * cos_ta_2da;
v2 = r1 * sin_ta_3da - r2 * sin_ta_2da;
// front face
normal = glm::vec3(0.0f, 0.0f, 1.0f);
ix0 = newVertex(&vBuffer, r0 * cos_ta, r0 * sin_ta, gearinfo->width * 0.5f, normal);
ix1 = newVertex(&vBuffer, r1 * cos_ta, r1 * sin_ta, gearinfo->width * 0.5f, normal);
ix2 = newVertex(&vBuffer, r0 * cos_ta, r0 * sin_ta, gearinfo->width * 0.5f, normal);
ix3 = newVertex(&vBuffer, r1 * cos_ta_3da, r1 * sin_ta_3da, gearinfo->width * 0.5f, normal);
ix4 = newVertex(&vBuffer, r0 * cos_ta_4da, r0 * sin_ta_4da, gearinfo->width * 0.5f, normal);
ix5 = newVertex(&vBuffer, r1 * cos_ta_4da, r1 * sin_ta_4da, gearinfo->width * 0.5f, normal);
newFace(&iBuffer, ix0, ix1, ix2);
newFace(&iBuffer, ix1, ix3, ix2);
newFace(&iBuffer, ix2, ix3, ix4);
newFace(&iBuffer, ix3, ix5, ix4);
// front sides of teeth
normal = glm::vec3(0.0f, 0.0f, 1.0f);
ix0 = newVertex(&vBuffer, r1 * cos_ta, r1 * sin_ta, gearinfo->width * 0.5f, normal);
ix1 = newVertex(&vBuffer, r2 * cos_ta_1da, r2 * sin_ta_1da, gearinfo->width * 0.5f, normal);
ix2 = newVertex(&vBuffer, r1 * cos_ta_3da, r1 * sin_ta_3da, gearinfo->width * 0.5f, normal);
ix3 = newVertex(&vBuffer, r2 * cos_ta_2da, r2 * sin_ta_2da, gearinfo->width * 0.5f, normal);
newFace(&iBuffer, ix0, ix1, ix2);
newFace(&iBuffer, ix1, ix3, ix2);
// back face
normal = glm::vec3(0.0f, 0.0f, -1.0f);
ix0 = newVertex(&vBuffer, r1 * cos_ta, r1 * sin_ta, -gearinfo->width * 0.5f, normal);
ix1 = newVertex(&vBuffer, r0 * cos_ta, r0 * sin_ta, -gearinfo->width * 0.5f, normal);
ix2 = newVertex(&vBuffer, r1 * cos_ta_3da, r1 * sin_ta_3da, -gearinfo->width * 0.5f, normal);
ix3 = newVertex(&vBuffer, r0 * cos_ta, r0 * sin_ta, -gearinfo->width * 0.5f, normal);
ix4 = newVertex(&vBuffer, r1 * cos_ta_4da, r1 * sin_ta_4da, -gearinfo->width * 0.5f, normal);
ix5 = newVertex(&vBuffer, r0 * cos_ta_4da, r0 * sin_ta_4da, -gearinfo->width * 0.5f, normal);
newFace(&iBuffer, ix0, ix1, ix2);
newFace(&iBuffer, ix1, ix3, ix2);
newFace(&iBuffer, ix2, ix3, ix4);
newFace(&iBuffer, ix3, ix5, ix4);
// back sides of teeth
normal = glm::vec3(0.0f, 0.0f, -1.0f);
ix0 = newVertex(&vBuffer, r1 * cos_ta_3da, r1 * sin_ta_3da, -gearinfo->width * 0.5f, normal);
ix1 = newVertex(&vBuffer, r2 * cos_ta_2da, r2 * sin_ta_2da, -gearinfo->width * 0.5f, normal);
ix2 = newVertex(&vBuffer, r1 * cos_ta, r1 * sin_ta, -gearinfo->width * 0.5f, normal);
ix3 = newVertex(&vBuffer, r2 * cos_ta_1da, r2 * sin_ta_1da, -gearinfo->width * 0.5f, normal);
newFace(&iBuffer, ix0, ix1, ix2);
newFace(&iBuffer, ix1, ix3, ix2);
// draw outward faces of teeth
normal = glm::vec3(v1, -u1, 0.0f);
ix0 = newVertex(&vBuffer, r1 * cos_ta, r1 * sin_ta, gearinfo->width * 0.5f, normal);
ix1 = newVertex(&vBuffer, r1 * cos_ta, r1 * sin_ta, -gearinfo->width * 0.5f, normal);
ix2 = newVertex(&vBuffer, r2 * cos_ta_1da, r2 * sin_ta_1da, gearinfo->width * 0.5f, normal);
ix3 = newVertex(&vBuffer, r2 * cos_ta_1da, r2 * sin_ta_1da, -gearinfo->width * 0.5f, normal);
newFace(&iBuffer, ix0, ix1, ix2);
newFace(&iBuffer, ix1, ix3, ix2);
normal = glm::vec3(cos_ta, sin_ta, 0.0f);
ix0 = newVertex(&vBuffer, r2 * cos_ta_1da, r2 * sin_ta_1da, gearinfo->width * 0.5f, normal);
ix1 = newVertex(&vBuffer, r2 * cos_ta_1da, r2 * sin_ta_1da, -gearinfo->width * 0.5f, normal);
ix2 = newVertex(&vBuffer, r2 * cos_ta_2da, r2 * sin_ta_2da, gearinfo->width * 0.5f, normal);
ix3 = newVertex(&vBuffer, r2 * cos_ta_2da, r2 * sin_ta_2da, -gearinfo->width * 0.5f, normal);
newFace(&iBuffer, ix0, ix1, ix2);
newFace(&iBuffer, ix1, ix3, ix2);
normal = glm::vec3(v2, -u2, 0.0f);
ix0 = newVertex(&vBuffer, r2 * cos_ta_2da, r2 * sin_ta_2da, gearinfo->width * 0.5f, normal);
ix1 = newVertex(&vBuffer, r2 * cos_ta_2da, r2 * sin_ta_2da, -gearinfo->width * 0.5f, normal);
ix2 = newVertex(&vBuffer, r1 * cos_ta_3da, r1 * sin_ta_3da, gearinfo->width * 0.5f, normal);
ix3 = newVertex(&vBuffer, r1 * cos_ta_3da, r1 * sin_ta_3da, -gearinfo->width * 0.5f, normal);
newFace(&iBuffer, ix0, ix1, ix2);
newFace(&iBuffer, ix1, ix3, ix2);
normal = glm::vec3(cos_ta, sin_ta, 0.0f);
ix0 = newVertex(&vBuffer, r1 * cos_ta_3da, r1 * sin_ta_3da, gearinfo->width * 0.5f, normal);
ix1 = newVertex(&vBuffer, r1 * cos_ta_3da, r1 * sin_ta_3da, -gearinfo->width * 0.5f, normal);
ix2 = newVertex(&vBuffer, r1 * cos_ta_4da, r1 * sin_ta_4da, gearinfo->width * 0.5f, normal);
ix3 = newVertex(&vBuffer, r1 * cos_ta_4da, r1 * sin_ta_4da, -gearinfo->width * 0.5f, normal);
newFace(&iBuffer, ix0, ix1, ix2);
newFace(&iBuffer, ix1, ix3, ix2);
// draw inside radius cylinder
ix0 = newVertex(&vBuffer, r0 * cos_ta, r0 * sin_ta, -gearinfo->width * 0.5f, glm::vec3(-cos_ta, -sin_ta, 0.0f));
ix1 = newVertex(&vBuffer, r0 * cos_ta, r0 * sin_ta, gearinfo->width * 0.5f, glm::vec3(-cos_ta, -sin_ta, 0.0f));
ix2 = newVertex(&vBuffer, r0 * cos_ta_4da, r0 * sin_ta_4da, -gearinfo->width * 0.5f, glm::vec3(-cos_ta_4da, -sin_ta_4da, 0.0f));
ix3 = newVertex(&vBuffer, r0 * cos_ta_4da, r0 * sin_ta_4da, gearinfo->width * 0.5f, glm::vec3(-cos_ta_4da, -sin_ta_4da, 0.0f));
newFace(&iBuffer, ix0, ix1, ix2);
newFace(&iBuffer, ix1, ix3, ix2);
}
size_t vertexBufferSize = vBuffer.size() * sizeof(Vertex);
size_t indexBufferSize = iBuffer.size() * sizeof(uint32_t);
bool useStaging = true;
if (useStaging)
{
vks::Buffer vertexStaging, indexStaging;
// Create staging buffers
// Vertex data
vulkanDevice->createBuffer(
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
&vertexStaging,
vertexBufferSize,
vBuffer.data());
// Index data
vulkanDevice->createBuffer(
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
&indexStaging,
indexBufferSize,
iBuffer.data());
// Create device local buffers
// Vertex buffer
vulkanDevice->createBuffer(
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
&vertexBuffer,
vertexBufferSize);
// Index buffer
vulkanDevice->createBuffer(
VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
&indexBuffer,
indexBufferSize);
// Copy from staging buffers
VkCommandBuffer copyCmd = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
VkBufferCopy copyRegion = {};
copyRegion.size = vertexBufferSize;
vkCmdCopyBuffer(
copyCmd,
vertexStaging.buffer,
vertexBuffer.buffer,
1,
&copyRegion);
copyRegion.size = indexBufferSize;
vkCmdCopyBuffer(
copyCmd,
indexStaging.buffer,
indexBuffer.buffer,
1,
&copyRegion);
vulkanDevice->flushCommandBuffer(copyCmd, queue, true);
vkDestroyBuffer(vulkanDevice->logicalDevice, vertexStaging.buffer, nullptr);
vkFreeMemory(vulkanDevice->logicalDevice, vertexStaging.memory, nullptr);
vkDestroyBuffer(vulkanDevice->logicalDevice, indexStaging.buffer, nullptr);
vkFreeMemory(vulkanDevice->logicalDevice, indexStaging.memory, nullptr);
}
else
{
// Vertex buffer
vulkanDevice->createBuffer(
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
&vertexBuffer,
vertexBufferSize,
vBuffer.data());
// Index buffer
vulkanDevice->createBuffer(
VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT,
&indexBuffer,
indexBufferSize,
iBuffer.data());
}
indexCount = iBuffer.size();
prepareUniformBuffer();
}
void VulkanGear::draw(VkCommandBuffer cmdbuffer, VkPipelineLayout pipelineLayout)
{
VkDeviceSize offsets[1] = { 0 };
vkCmdBindDescriptorSets(cmdbuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL);
vkCmdBindVertexBuffers(cmdbuffer, 0, 1, &vertexBuffer.buffer, offsets);
vkCmdBindIndexBuffer(cmdbuffer, indexBuffer.buffer, 0, VK_INDEX_TYPE_UINT32);
vkCmdDrawIndexed(cmdbuffer, indexCount, 1, 0, 0, 1);
}
void VulkanGear::updateUniformBuffer(glm::mat4 perspective, glm::vec3 rotation, float zoom, float timer)
{
ubo.projection = perspective;
ubo.view = glm::lookAt(
glm::vec3(0, 0, -zoom),
glm::vec3(-1.0, -1.5, 0),
glm::vec3(0, 1, 0)
);
ubo.view = glm::rotate(ubo.view, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
ubo.view = glm::rotate(ubo.view, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
ubo.model = glm::mat4(1.0f);
ubo.model = glm::translate(ubo.model, pos);
rotation.z = (rotSpeed * timer) + rotOffset;
ubo.model = glm::rotate(ubo.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
ubo.normal = glm::inverseTranspose(ubo.view * ubo.model);
ubo.lightPos = glm::vec3(0.0f, 0.0f, 2.5f);
ubo.lightPos.x = sin(glm::radians(timer)) * 8.0f;
ubo.lightPos.z = cos(glm::radians(timer)) * 8.0f;
memcpy(uniformBuffer.mapped, &ubo, sizeof(ubo));
}
void VulkanGear::setupDescriptorSet(VkDescriptorPool pool, VkDescriptorSetLayout descriptorSetLayout)
{
VkDescriptorSetAllocateInfo allocInfo =
vks::initializers::descriptorSetAllocateInfo(
pool,
&descriptorSetLayout,
1);
VK_CHECK_RESULT(vkAllocateDescriptorSets(vulkanDevice->logicalDevice, &allocInfo, &descriptorSet));
// Binding 0 : Vertex shader uniform buffer
VkWriteDescriptorSet writeDescriptorSet =
vks::initializers::writeDescriptorSet(
descriptorSet,
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
0,
&uniformBuffer.descriptor);
vkUpdateDescriptorSets(vulkanDevice->logicalDevice, 1, &writeDescriptorSet, 0, NULL);
}
void VulkanGear::prepareUniformBuffer()
{
VK_CHECK_RESULT(vulkanDevice->createBuffer(
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
&uniformBuffer,
sizeof(ubo)));
// Map persistent
VK_CHECK_RESULT(uniformBuffer.map());
}

105
examples/gears/vulkangear.h Normal file
View file

@ -0,0 +1,105 @@
/*
* Vulkan Example - Animated gears using multiple uniform buffers
*
* See readme.md for details
*
* Copyright (C) 2015 by Sascha Willems - www.saschawillems.de
*
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/
#pragma once
#include <math.h>
#include <vector>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/matrix_inverse.hpp>
#include "vulkan/vulkan.h"
#include "VulkanTools.h"
#include "VulkanDevice.hpp"
#include "VulkanBuffer.hpp"
struct Vertex
{
float pos[3];
float normal[3];
float color[3];
Vertex(const glm::vec3& p, const glm::vec3& n, const glm::vec3& c)
{
pos[0] = p.x;
pos[1] = p.y;
pos[2] = p.z;
color[0] = c.x;
color[1] = c.y;
color[2] = c.z;
normal[0] = n.x;
normal[1] = n.y;
normal[2] = n.z;
}
};
struct GearInfo
{
float innerRadius;
float outerRadius;
float width;
int numTeeth;
float toothDepth;
glm::vec3 color;
glm::vec3 pos;
float rotSpeed;
float rotOffset;
};
class VulkanGear
{
private:
struct UBO
{
glm::mat4 projection;
glm::mat4 model;
glm::mat4 normal;
glm::mat4 view;
glm::vec3 lightPos;
};
vks::VulkanDevice *vulkanDevice;
glm::vec3 color;
glm::vec3 pos;
float rotSpeed;
float rotOffset;
vks::Buffer vertexBuffer;
vks::Buffer indexBuffer;
uint32_t indexCount;
UBO ubo;
vks::Buffer uniformBuffer;
int32_t newVertex(std::vector<Vertex> *vBuffer, float x, float y, float z, const glm::vec3& normal);
void newFace(std::vector<uint32_t> *iBuffer, int a, int b, int c);
void prepareUniformBuffer();
public:
VkDescriptorSet descriptorSet;
void draw(VkCommandBuffer cmdbuffer, VkPipelineLayout pipelineLayout);
void updateUniformBuffer(glm::mat4 perspective, glm::vec3 rotation, float zoom, float timer);
void setupDescriptorSet(VkDescriptorPool pool, VkDescriptorSetLayout descriptorSetLayout);
VulkanGear(vks::VulkanDevice *vulkanDevice) : vulkanDevice(vulkanDevice) {};
~VulkanGear();
void generate(GearInfo *gearinfo, VkQueue queue);
};