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

1221 lines
43 KiB
C++
Raw Normal View History

/*
* Vulkan Example - Text overlay rendering on-top of an existing scene using a separate render pass
*
* 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>
#include <sstream>
#include <iomanip>
#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 "vulkanexamplebase.h"
2016-07-23 23:44:26 +02:00
#include "vulkandevice.hpp"
#include "vulkanbuffer.hpp"
#include "VulkanModel.hpp"
#include "VulkanTexture.hpp"
2016-07-23 23:44:26 +02:00
#include "../external/stb/stb_font_consolas_24_latin1.inl"
#define VERTEX_BUFFER_BIND_ID 0
#define ENABLE_VALIDATION false
// Defines for the STB font used
// STB font files can be found at http://nothings.org/stb/font/
#define STB_FONT_NAME stb_font_consolas_24_latin1
#define STB_FONT_WIDTH STB_FONT_consolas_24_latin1_BITMAP_WIDTH
#define STB_FONT_HEIGHT STB_FONT_consolas_24_latin1_BITMAP_HEIGHT
#define STB_FIRST_CHAR STB_FONT_consolas_24_latin1_FIRST_CHAR
#define STB_NUM_CHARS STB_FONT_consolas_24_latin1_NUM_CHARS
// Max. number of chars the text overlay buffer can hold
2017-02-04 14:37:55 +01:00
#define TEXTOVERLAY_MAX_CHAR_COUNT 2048
// Mostly self-contained text overlay class
class TextOverlay
{
private:
2016-07-23 23:44:26 +02:00
vk::VulkanDevice *vulkanDevice;
2016-05-05 14:42:41 +02:00
VkQueue queue;
VkFormat colorFormat;
VkFormat depthFormat;
uint32_t *frameBufferWidth;
uint32_t *frameBufferHeight;
VkSampler sampler;
VkImage image;
VkImageView view;
VkBuffer buffer;
VkDeviceMemory memory;
VkDeviceMemory imageMemory;
VkDescriptorPool descriptorPool;
VkDescriptorSetLayout descriptorSetLayout;
VkDescriptorSet descriptorSet;
VkPipelineLayout pipelineLayout;
VkPipelineCache pipelineCache;
VkPipeline pipeline;
VkRenderPass renderPass;
VkCommandPool commandPool;
std::vector<VkCommandBuffer> cmdBuffers;
std::vector<VkFramebuffer*> frameBuffers;
std::vector<VkPipelineShaderStageCreateInfo> shaderStages;
// Pointer to mapped vertex buffer
glm::vec4 *mapped = nullptr;
stb_fontchar stbFontData[STB_NUM_CHARS];
uint32_t numLetters;
public:
enum TextAlign { alignLeft, alignCenter, alignRight };
bool visible = true;
TextOverlay(
2016-07-23 23:44:26 +02:00
vk::VulkanDevice *vulkanDevice,
2016-05-05 14:42:41 +02:00
VkQueue queue,
std::vector<VkFramebuffer> &framebuffers,
VkFormat colorformat,
VkFormat depthformat,
uint32_t *framebufferwidth,
uint32_t *framebufferheight,
std::vector<VkPipelineShaderStageCreateInfo> shaderstages)
{
2016-07-23 23:44:26 +02:00
this->vulkanDevice = vulkanDevice;
2016-05-05 14:42:41 +02:00
this->queue = queue;
this->colorFormat = colorformat;
this->depthFormat = depthformat;
this->frameBuffers.resize(framebuffers.size());
for (uint32_t i = 0; i < framebuffers.size(); i++)
{
this->frameBuffers[i] = &framebuffers[i];
}
this->shaderStages = shaderstages;
this->frameBufferWidth = framebufferwidth;
this->frameBufferHeight = framebufferheight;
cmdBuffers.resize(framebuffers.size());
prepareResources();
prepareRenderPass();
preparePipeline();
}
~TextOverlay()
{
// Free up all Vulkan resources requested by the text overlay
2016-07-23 23:44:26 +02:00
vkDestroySampler(vulkanDevice->logicalDevice, sampler, nullptr);
vkDestroyImage(vulkanDevice->logicalDevice, image, nullptr);
vkDestroyImageView(vulkanDevice->logicalDevice, view, nullptr);
vkDestroyBuffer(vulkanDevice->logicalDevice, buffer, nullptr);
vkFreeMemory(vulkanDevice->logicalDevice, memory, nullptr);
vkFreeMemory(vulkanDevice->logicalDevice, imageMemory, nullptr);
vkDestroyDescriptorSetLayout(vulkanDevice->logicalDevice, descriptorSetLayout, nullptr);
vkDestroyDescriptorPool(vulkanDevice->logicalDevice, descriptorPool, nullptr);
vkDestroyPipelineLayout(vulkanDevice->logicalDevice, pipelineLayout, nullptr);
vkDestroyPipelineCache(vulkanDevice->logicalDevice, pipelineCache, nullptr);
vkDestroyPipeline(vulkanDevice->logicalDevice, pipeline, nullptr);
vkDestroyRenderPass(vulkanDevice->logicalDevice, renderPass, nullptr);
vkDestroyCommandPool(vulkanDevice->logicalDevice, commandPool, nullptr);
}
// Prepare all vulkan resources required to render the font
// The text overlay uses separate resources for descriptors (pool, sets, layouts), pipelines and command buffers
void prepareResources()
{
static unsigned char font24pixels[STB_FONT_HEIGHT][STB_FONT_WIDTH];
STB_FONT_NAME(stbFontData, font24pixels, STB_FONT_HEIGHT);
2016-05-05 14:42:41 +02:00
// Command buffer
// Pool
VkCommandPoolCreateInfo cmdPoolInfo = {};
cmdPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
2016-07-23 23:44:26 +02:00
cmdPoolInfo.queueFamilyIndex = vulkanDevice->queueFamilyIndices.graphics;
2016-05-05 14:42:41 +02:00
cmdPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
2016-07-23 23:44:26 +02:00
VK_CHECK_RESULT(vkCreateCommandPool(vulkanDevice->logicalDevice, &cmdPoolInfo, nullptr, &commandPool));
2016-05-05 14:42:41 +02:00
VkCommandBufferAllocateInfo cmdBufAllocateInfo =
vkTools::initializers::commandBufferAllocateInfo(
commandPool,
VK_COMMAND_BUFFER_LEVEL_PRIMARY,
(uint32_t)cmdBuffers.size());
2016-07-23 23:44:26 +02:00
VK_CHECK_RESULT(vkAllocateCommandBuffers(vulkanDevice->logicalDevice, &cmdBufAllocateInfo, cmdBuffers.data()));
2016-05-05 14:42:41 +02:00
// Vertex buffer
2017-02-04 14:37:55 +01:00
VkDeviceSize bufferSize = TEXTOVERLAY_MAX_CHAR_COUNT * sizeof(glm::vec4);
VkBufferCreateInfo bufferInfo = vkTools::initializers::bufferCreateInfo(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, bufferSize);
2016-07-23 23:44:26 +02:00
VK_CHECK_RESULT(vkCreateBuffer(vulkanDevice->logicalDevice, &bufferInfo, nullptr, &buffer));
VkMemoryRequirements memReqs;
VkMemoryAllocateInfo allocInfo = vkTools::initializers::memoryAllocateInfo();
2016-07-23 23:44:26 +02:00
vkGetBufferMemoryRequirements(vulkanDevice->logicalDevice, buffer, &memReqs);
allocInfo.allocationSize = memReqs.size;
2016-07-23 23:44:26 +02:00
allocInfo.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
2016-07-23 23:44:26 +02:00
VK_CHECK_RESULT(vkAllocateMemory(vulkanDevice->logicalDevice, &allocInfo, nullptr, &memory));
VK_CHECK_RESULT(vkBindBufferMemory(vulkanDevice->logicalDevice, buffer, memory, 0));
2016-05-05 14:42:41 +02:00
// Font texture
VkImageCreateInfo imageInfo = vkTools::initializers::imageCreateInfo();
imageInfo.imageType = VK_IMAGE_TYPE_2D;
imageInfo.format = VK_FORMAT_R8_UNORM;
imageInfo.extent.width = STB_FONT_WIDTH;
imageInfo.extent.height = STB_FONT_HEIGHT;
imageInfo.extent.depth = 1;
imageInfo.mipLevels = 1;
imageInfo.arrayLayers = 1;
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
2016-05-05 14:42:41 +02:00
imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
2016-07-23 23:44:26 +02:00
VK_CHECK_RESULT(vkCreateImage(vulkanDevice->logicalDevice, &imageInfo, nullptr, &image));
2016-07-23 23:44:26 +02:00
vkGetImageMemoryRequirements(vulkanDevice->logicalDevice, image, &memReqs);
allocInfo.allocationSize = memReqs.size;
2016-07-23 23:44:26 +02:00
allocInfo.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
2016-07-23 23:44:26 +02:00
VK_CHECK_RESULT(vkAllocateMemory(vulkanDevice->logicalDevice, &allocInfo, nullptr, &imageMemory));
VK_CHECK_RESULT(vkBindImageMemory(vulkanDevice->logicalDevice, image, imageMemory, 0));
2016-05-05 14:42:41 +02:00
// Staging
struct {
VkDeviceMemory memory;
VkBuffer buffer;
} stagingBuffer;
VkBufferCreateInfo bufferCreateInfo = vkTools::initializers::bufferCreateInfo();
bufferCreateInfo.size = allocInfo.allocationSize;
bufferCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
2016-07-23 23:44:26 +02:00
VK_CHECK_RESULT(vkCreateBuffer(vulkanDevice->logicalDevice, &bufferCreateInfo, nullptr, &stagingBuffer.buffer));
2016-05-05 14:42:41 +02:00
// Get memory requirements for the staging buffer (alignment, memory type bits)
2016-07-23 23:44:26 +02:00
vkGetBufferMemoryRequirements(vulkanDevice->logicalDevice, stagingBuffer.buffer, &memReqs);
2016-05-05 14:42:41 +02:00
allocInfo.allocationSize = memReqs.size;
// Get memory type index for a host visible buffer
2016-07-23 23:44:26 +02:00
allocInfo.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
2016-05-05 14:42:41 +02:00
2016-07-23 23:44:26 +02:00
VK_CHECK_RESULT(vkAllocateMemory(vulkanDevice->logicalDevice, &allocInfo, nullptr, &stagingBuffer.memory));
VK_CHECK_RESULT(vkBindBufferMemory(vulkanDevice->logicalDevice, stagingBuffer.buffer, stagingBuffer.memory, 0));
2016-05-05 14:42:41 +02:00
uint8_t *data;
2016-07-23 23:44:26 +02:00
VK_CHECK_RESULT(vkMapMemory(vulkanDevice->logicalDevice, stagingBuffer.memory, 0, allocInfo.allocationSize, 0, (void **)&data));
// Size of the font texture is WIDTH * HEIGHT * 1 byte (only one channel)
2016-05-05 14:42:41 +02:00
memcpy(data, &font24pixels[0][0], STB_FONT_WIDTH * STB_FONT_HEIGHT);
2016-07-23 23:44:26 +02:00
vkUnmapMemory(vulkanDevice->logicalDevice, stagingBuffer.memory);
2016-05-05 14:42:41 +02:00
// Copy to image
VkCommandBuffer copyCmd;
cmdBufAllocateInfo.commandBufferCount = 1;
2016-07-23 23:44:26 +02:00
VK_CHECK_RESULT(vkAllocateCommandBuffers(vulkanDevice->logicalDevice, &cmdBufAllocateInfo, &copyCmd));
2016-05-05 14:42:41 +02:00
VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo();
VK_CHECK_RESULT(vkBeginCommandBuffer(copyCmd, &cmdBufInfo));
2016-05-05 14:42:41 +02:00
// Prepare for transfer
vkTools::setImageLayout(
copyCmd,
image,
VK_IMAGE_ASPECT_COLOR_BIT,
VK_IMAGE_LAYOUT_UNDEFINED,
2016-05-05 14:42:41 +02:00
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
VkBufferImageCopy bufferCopyRegion = {};
bufferCopyRegion.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
bufferCopyRegion.imageSubresource.mipLevel = 0;
bufferCopyRegion.imageSubresource.layerCount = 1;
bufferCopyRegion.imageExtent.width = STB_FONT_WIDTH;
bufferCopyRegion.imageExtent.height = STB_FONT_HEIGHT;
bufferCopyRegion.imageExtent.depth = 1;
vkCmdCopyBufferToImage(
copyCmd,
stagingBuffer.buffer,
image,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1,
&bufferCopyRegion
);
// Prepare for shader read
vkTools::setImageLayout(
copyCmd,
image,
VK_IMAGE_ASPECT_COLOR_BIT,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
VK_CHECK_RESULT(vkEndCommandBuffer(copyCmd));
2016-05-05 14:42:41 +02:00
VkSubmitInfo submitInfo = vkTools::initializers::submitInfo();
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &copyCmd;
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
VK_CHECK_RESULT(vkQueueWaitIdle(queue));
2016-05-05 14:42:41 +02:00
2016-07-23 23:44:26 +02:00
vkFreeCommandBuffers(vulkanDevice->logicalDevice, commandPool, 1, &copyCmd);
vkFreeMemory(vulkanDevice->logicalDevice, stagingBuffer.memory, nullptr);
vkDestroyBuffer(vulkanDevice->logicalDevice, stagingBuffer.buffer, nullptr);
VkImageViewCreateInfo imageViewInfo = vkTools::initializers::imageViewCreateInfo();
imageViewInfo.image = image;
imageViewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
imageViewInfo.format = imageInfo.format;
imageViewInfo.components = { VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A };
imageViewInfo.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
2016-07-23 23:44:26 +02:00
VK_CHECK_RESULT(vkCreateImageView(vulkanDevice->logicalDevice, &imageViewInfo, nullptr, &view));
// Sampler
VkSamplerCreateInfo samplerInfo = vkTools::initializers::samplerCreateInfo();
samplerInfo.magFilter = VK_FILTER_LINEAR;
samplerInfo.minFilter = VK_FILTER_LINEAR;
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerInfo.mipLodBias = 0.0f;
samplerInfo.compareOp = VK_COMPARE_OP_NEVER;
samplerInfo.minLod = 0.0f;
samplerInfo.maxLod = 1.0f;
samplerInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
2016-07-23 23:44:26 +02:00
VK_CHECK_RESULT(vkCreateSampler(vulkanDevice->logicalDevice, &samplerInfo, nullptr, &sampler));
// Descriptor
// Font uses a separate descriptor pool
std::array<VkDescriptorPoolSize, 1> poolSizes;
poolSizes[0] = vkTools::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1);
VkDescriptorPoolCreateInfo descriptorPoolInfo =
vkTools::initializers::descriptorPoolCreateInfo(
2017-02-04 14:37:55 +01:00
static_cast<uint32_t>(poolSizes.size()),
poolSizes.data(),
1);
2016-07-23 23:44:26 +02:00
VK_CHECK_RESULT(vkCreateDescriptorPool(vulkanDevice->logicalDevice, &descriptorPoolInfo, nullptr, &descriptorPool));
// Descriptor set layout
std::array<VkDescriptorSetLayoutBinding, 1> setLayoutBindings;
setLayoutBindings[0] = vkTools::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 0);
VkDescriptorSetLayoutCreateInfo descriptorSetLayoutInfo =
vkTools::initializers::descriptorSetLayoutCreateInfo(
setLayoutBindings.data(),
2017-02-04 14:37:55 +01:00
static_cast<uint32_t>(setLayoutBindings.size()));
2016-07-23 23:44:26 +02:00
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(vulkanDevice->logicalDevice, &descriptorSetLayoutInfo, nullptr, &descriptorSetLayout));
// Pipeline layout
VkPipelineLayoutCreateInfo pipelineLayoutInfo =
vkTools::initializers::pipelineLayoutCreateInfo(
&descriptorSetLayout,
1);
2016-07-23 23:44:26 +02:00
VK_CHECK_RESULT(vkCreatePipelineLayout(vulkanDevice->logicalDevice, &pipelineLayoutInfo, nullptr, &pipelineLayout));
// Descriptor set
VkDescriptorSetAllocateInfo descriptorSetAllocInfo =
vkTools::initializers::descriptorSetAllocateInfo(
descriptorPool,
&descriptorSetLayout,
1);
2016-07-23 23:44:26 +02:00
VK_CHECK_RESULT(vkAllocateDescriptorSets(vulkanDevice->logicalDevice, &descriptorSetAllocInfo, &descriptorSet));
VkDescriptorImageInfo texDescriptor =
vkTools::initializers::descriptorImageInfo(
sampler,
view,
VK_IMAGE_LAYOUT_GENERAL);
std::array<VkWriteDescriptorSet, 1> writeDescriptorSets;
writeDescriptorSets[0] = vkTools::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 0, &texDescriptor);
2017-02-04 14:37:55 +01:00
vkUpdateDescriptorSets(vulkanDevice->logicalDevice, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
// Pipeline cache
VkPipelineCacheCreateInfo pipelineCacheCreateInfo = {};
pipelineCacheCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
2016-07-23 23:44:26 +02:00
VK_CHECK_RESULT(vkCreatePipelineCache(vulkanDevice->logicalDevice, &pipelineCacheCreateInfo, nullptr, &pipelineCache));
}
// Prepare a separate pipeline for the font rendering decoupled from the main application
void preparePipeline()
{
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState =
vkTools::initializers::pipelineInputAssemblyStateCreateInfo(
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
0,
VK_FALSE);
VkPipelineRasterizationStateCreateInfo rasterizationState =
vkTools::initializers::pipelineRasterizationStateCreateInfo(
VK_POLYGON_MODE_FILL,
VK_CULL_MODE_BACK_BIT,
VK_FRONT_FACE_CLOCKWISE,
0);
// Enable blending
VkPipelineColorBlendAttachmentState blendAttachmentState =
vkTools::initializers::pipelineColorBlendAttachmentState(0xf, VK_TRUE);
blendAttachmentState.srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
blendAttachmentState.dstColorBlendFactor = VK_BLEND_FACTOR_ONE;
blendAttachmentState.colorBlendOp = VK_BLEND_OP_ADD;
blendAttachmentState.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
blendAttachmentState.dstAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
blendAttachmentState.alphaBlendOp = VK_BLEND_OP_ADD;
blendAttachmentState.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
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(),
2017-02-04 14:37:55 +01:00
static_cast<uint32_t>(dynamicStateEnables.size()),
0);
std::array<VkVertexInputBindingDescription, 2> vertexBindings = {};
vertexBindings[0] = vkTools::initializers::vertexInputBindingDescription(0, sizeof(glm::vec4), VK_VERTEX_INPUT_RATE_VERTEX);
vertexBindings[1] = vkTools::initializers::vertexInputBindingDescription(1, sizeof(glm::vec4), VK_VERTEX_INPUT_RATE_VERTEX);
std::array<VkVertexInputAttributeDescription, 2> vertexAttribs = {};
// Position
vertexAttribs[0] = vkTools::initializers::vertexInputAttributeDescription(0, 0, VK_FORMAT_R32G32_SFLOAT, 0);
// UV
vertexAttribs[1] = vkTools::initializers::vertexInputAttributeDescription(1, 1, VK_FORMAT_R32G32_SFLOAT, sizeof(glm::vec2));
VkPipelineVertexInputStateCreateInfo inputState = vkTools::initializers::pipelineVertexInputStateCreateInfo();
2017-02-04 14:37:55 +01:00
inputState.vertexBindingDescriptionCount = static_cast<uint32_t>(vertexBindings.size());
inputState.pVertexBindingDescriptions = vertexBindings.data();
2017-02-04 14:37:55 +01:00
inputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(vertexAttribs.size());
inputState.pVertexAttributeDescriptions = vertexAttribs.data();
VkGraphicsPipelineCreateInfo pipelineCreateInfo =
vkTools::initializers::pipelineCreateInfo(
pipelineLayout,
renderPass,
0);
pipelineCreateInfo.pVertexInputState = &inputState;
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
pipelineCreateInfo.pRasterizationState = &rasterizationState;
pipelineCreateInfo.pColorBlendState = &colorBlendState;
pipelineCreateInfo.pMultisampleState = &multisampleState;
pipelineCreateInfo.pViewportState = &viewportState;
pipelineCreateInfo.pDepthStencilState = &depthStencilState;
pipelineCreateInfo.pDynamicState = &dynamicState;
2017-02-04 14:37:55 +01:00
pipelineCreateInfo.stageCount = static_cast<uint32_t>(shaderStages.size());
pipelineCreateInfo.pStages = shaderStages.data();
2016-07-23 23:44:26 +02:00
VK_CHECK_RESULT(vkCreateGraphicsPipelines(vulkanDevice->logicalDevice, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipeline));
}
// Prepare a separate render pass for rendering the text as an overlay
void prepareRenderPass()
{
VkAttachmentDescription attachments[2] = {};
// Color attachment
attachments[0].format = colorFormat;
attachments[0].samples = VK_SAMPLE_COUNT_1_BIT;
// Don't clear the framebuffer (like the renderpass from the example does)
attachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
attachments[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
attachments[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachments[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[0].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
attachments[0].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
// Depth attachment
attachments[1].format = depthFormat;
attachments[1].samples = VK_SAMPLE_COUNT_1_BIT;
attachments[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
attachments[1].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
attachments[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachments[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachments[1].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
attachments[1].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
VkAttachmentReference colorReference = {};
colorReference.attachment = 0;
colorReference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkAttachmentReference depthReference = {};
depthReference.attachment = 1;
depthReference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
// Use subpass dependencies for image layout transitions
VkSubpassDependency subpassDependencies[2] = {};
// Transition from final to initial (VK_SUBPASS_EXTERNAL refers to all commmands executed outside of the actual renderpass)
subpassDependencies[0].srcSubpass = VK_SUBPASS_EXTERNAL;
subpassDependencies[0].dstSubpass = 0;
subpassDependencies[0].srcStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
subpassDependencies[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
subpassDependencies[0].srcAccessMask = VK_ACCESS_MEMORY_READ_BIT;
subpassDependencies[0].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
subpassDependencies[0].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
// Transition from initial to final
subpassDependencies[1].srcSubpass = 0;
subpassDependencies[1].dstSubpass = VK_SUBPASS_EXTERNAL;
subpassDependencies[1].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
subpassDependencies[1].dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
subpassDependencies[1].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
subpassDependencies[1].dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
subpassDependencies[1].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
VkSubpassDescription subpassDescription = {};
subpassDescription.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpassDescription.flags = 0;
subpassDescription.inputAttachmentCount = 0;
subpassDescription.pInputAttachments = NULL;
subpassDescription.colorAttachmentCount = 1;
subpassDescription.pColorAttachments = &colorReference;
subpassDescription.pResolveAttachments = NULL;
subpassDescription.pDepthStencilAttachment = &depthReference;
subpassDescription.preserveAttachmentCount = 0;
subpassDescription.pPreserveAttachments = NULL;
VkRenderPassCreateInfo renderPassInfo = {};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
renderPassInfo.pNext = NULL;
renderPassInfo.attachmentCount = 2;
renderPassInfo.pAttachments = attachments;
renderPassInfo.subpassCount = 1;
renderPassInfo.pSubpasses = &subpassDescription;
renderPassInfo.dependencyCount = 2;
renderPassInfo.pDependencies = subpassDependencies;
2016-07-23 23:44:26 +02:00
VK_CHECK_RESULT(vkCreateRenderPass(vulkanDevice->logicalDevice, &renderPassInfo, nullptr, &renderPass));
}
// Map buffer
void beginTextUpdate()
{
2016-07-23 23:44:26 +02:00
VK_CHECK_RESULT(vkMapMemory(vulkanDevice->logicalDevice, memory, 0, VK_WHOLE_SIZE, 0, (void **)&mapped));
numLetters = 0;
}
// Add text to the current buffer
// todo : drop shadow? color attribute?
void addText(std::string text, float x, float y, TextAlign align)
{
assert(mapped != nullptr);
const float charW = 1.5f / *frameBufferWidth;
const float charH = 1.5f / *frameBufferHeight;
float fbW = (float)*frameBufferWidth;
float fbH = (float)*frameBufferHeight;
x = (x / fbW * 2.0f) - 1.0f;
y = (y / fbH * 2.0f) - 1.0f;
// Calculate text width
float textWidth = 0;
for (auto letter : text)
{
stb_fontchar *charData = &stbFontData[(uint32_t)letter - STB_FIRST_CHAR];
textWidth += charData->advance * charW;
}
switch (align)
{
case alignRight:
x -= textWidth;
break;
case alignCenter:
x -= textWidth / 2.0f;
break;
}
// Generate a uv mapped quad per char in the new text
for (auto letter : text)
{
stb_fontchar *charData = &stbFontData[(uint32_t)letter - STB_FIRST_CHAR];
mapped->x = (x + (float)charData->x0 * charW);
mapped->y = (y + (float)charData->y0 * charH);
mapped->z = charData->s0;
mapped->w = charData->t0;
mapped++;
mapped->x = (x + (float)charData->x1 * charW);
mapped->y = (y + (float)charData->y0 * charH);
mapped->z = charData->s1;
mapped->w = charData->t0;
mapped++;
mapped->x = (x + (float)charData->x0 * charW);
mapped->y = (y + (float)charData->y1 * charH);
mapped->z = charData->s0;
mapped->w = charData->t1;
mapped++;
mapped->x = (x + (float)charData->x1 * charW);
mapped->y = (y + (float)charData->y1 * charH);
mapped->z = charData->s1;
mapped->w = charData->t1;
mapped++;
x += charData->advance * charW;
numLetters++;
}
}
// Unmap buffer and update command buffers
void endTextUpdate()
{
2016-07-23 23:44:26 +02:00
vkUnmapMemory(vulkanDevice->logicalDevice, memory);
mapped = nullptr;
updateCommandBuffers();
}
// Needs to be called by the application
void updateCommandBuffers()
{
VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo();
VkClearValue clearValues[2];
clearValues[1].color = { { 0.0f, 0.0f, 0.0f, 0.0f } };
VkRenderPassBeginInfo renderPassBeginInfo = vkTools::initializers::renderPassBeginInfo();
renderPassBeginInfo.renderPass = renderPass;
renderPassBeginInfo.renderArea.extent.width = *frameBufferWidth;
renderPassBeginInfo.renderArea.extent.height = *frameBufferHeight;
renderPassBeginInfo.clearValueCount = 2;
renderPassBeginInfo.pClearValues = clearValues;
for (int32_t i = 0; i < cmdBuffers.size(); ++i)
{
renderPassBeginInfo.framebuffer = *frameBuffers[i];
VK_CHECK_RESULT(vkBeginCommandBuffer(cmdBuffers[i], &cmdBufInfo));
vkCmdBeginRenderPass(cmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
VkViewport viewport = vkTools::initializers::viewport((float)*frameBufferWidth, (float)*frameBufferHeight, 0.0f, 1.0f);
vkCmdSetViewport(cmdBuffers[i], 0, 1, &viewport);
VkRect2D scissor = vkTools::initializers::rect2D(*frameBufferWidth, *frameBufferHeight, 0, 0);
vkCmdSetScissor(cmdBuffers[i], 0, 1, &scissor);
vkCmdBindPipeline(cmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
vkCmdBindDescriptorSets(cmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL);
VkDeviceSize offsets = 0;
vkCmdBindVertexBuffers(cmdBuffers[i], 0, 1, &buffer, &offsets);
vkCmdBindVertexBuffers(cmdBuffers[i], 1, 1, &buffer, &offsets);
for (uint32_t j = 0; j < numLetters; j++)
{
vkCmdDraw(cmdBuffers[i], 4, 1, j * 4, 0);
}
vkCmdEndRenderPass(cmdBuffers[i]);
VK_CHECK_RESULT(vkEndCommandBuffer(cmdBuffers[i]));
}
}
// Submit the text command buffers to a queue
// Does a queue wait idle
void submit(VkQueue queue, uint32_t bufferindex)
{
if (!visible)
{
return;
}
VkSubmitInfo submitInfo = {};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &cmdBuffers[bufferindex];
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
VK_CHECK_RESULT(vkQueueWaitIdle(queue));
}
};
class VulkanExample : public VulkanExampleBase
{
public:
TextOverlay *textOverlay = nullptr;
// Vertex layout for the models
vks::VertexLayout vertexLayout = vks::VertexLayout({
vks::VERTEX_COMPONENT_POSITION,
vks::VERTEX_COMPONENT_NORMAL,
vks::VERTEX_COMPONENT_UV,
vks::VERTEX_COMPONENT_COLOR,
});
struct {
vks::Texture2D background;
vks::Texture2D cube;
} textures;
struct {
vks::Model cube;
} models;
struct {
VkPipelineVertexInputStateCreateInfo inputState;
std::vector<VkVertexInputBindingDescription> bindingDescriptions;
std::vector<VkVertexInputAttributeDescription> attributeDescriptions;
} vertices;
vk::Buffer uniformBuffer;
struct UBOVS {
glm::mat4 projection;
glm::mat4 model;
glm::vec4 lightPos = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
} uboVS;
struct {
VkPipeline solid;
VkPipeline background;
} pipelines;
VkPipelineLayout pipelineLayout;
VkDescriptorSetLayout descriptorSetLayout;
struct {
VkDescriptorSet background;
VkDescriptorSet cube;
} descriptorSets;
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
{
zoom = -4.5f;
zoomSpeed = 2.5f;
rotation = { -25.0f, 0.0f, 0.0f };
title = "Vulkan Example - Text overlay";
// Disable text overlay of the example base class
enableTextOverlay = false;
}
~VulkanExample()
{
vkDestroyPipeline(device, pipelines.solid, nullptr);
vkDestroyPipeline(device, pipelines.background, nullptr);
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
models.cube.destroy();
textures.background.destroy();
textures.cube.destroy();
uniformBuffer.destroy();
delete(textOverlay);
}
void buildCommandBuffers()
{
VkCommandBufferBeginInfo cmdBufInfo = vkTools::initializers::commandBufferBeginInfo();
VkClearValue clearValues[3];
clearValues[0].color = { { 0.0f, 0.0f, 0.0f, 1.0f } };
clearValues[1].depthStencil = { 1.0f, 0 };
VkRenderPassBeginInfo renderPassBeginInfo = vkTools::initializers::renderPassBeginInfo();
renderPassBeginInfo.renderPass = renderPass;
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));
vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
VkViewport viewport = vkTools::initializers::viewport((float)width, (float)height, 0.0f, 1.0f);
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
VkRect2D scissor = vkTools::initializers::rect2D(width, height, 0, 0);
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets.background, 0, NULL);
VkDeviceSize offsets[1] = { 0 };
vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &models.cube.vertices.buffer, offsets);
vkCmdBindIndexBuffer(drawCmdBuffers[i], models.cube.indices.buffer, 0, VK_INDEX_TYPE_UINT32);
// Background
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.background);
// Vertices are generated by the vertex shader
vkCmdDraw(drawCmdBuffers[i], 4, 1, 0, 0);
// Cube
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.solid);
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets.cube, 0, NULL);
vkCmdDrawIndexed(drawCmdBuffers[i], models.cube.indexCount, 1, 0, 0, 0);
vkCmdEndRenderPass(drawCmdBuffers[i]);
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
}
vkQueueWaitIdle(queue);
}
// Update the text buffer displayed by the text overlay
void updateTextOverlay(void)
{
textOverlay->beginTextUpdate();
textOverlay->addText(title, 5.0f, 5.0f, TextOverlay::alignLeft);
std::stringstream ss;
ss << std::fixed << std::setprecision(2) << (frameTimer * 1000.0f) << "ms (" << lastFPS << " fps)";
textOverlay->addText(ss.str(), 5.0f, 25.0f, TextOverlay::alignLeft);
textOverlay->addText(deviceProperties.deviceName, 5.0f, 45.0f, TextOverlay::alignLeft);
textOverlay->addText("Press \"space\" to toggle text overlay", 5.0f, 65.0f, TextOverlay::alignLeft);
// Display projected cube vertices
for (int32_t x = -1; x <= 1; x += 2)
{
for (int32_t y = -1; y <= 1; y += 2)
{
for (int32_t z = -1; z <= 1; z += 2)
{
std::stringstream vpos;
vpos << std::showpos << x << "/" << y << "/" << z;
glm::vec3 projected = glm::project(glm::vec3((float)x, (float)y, (float)z), uboVS.model, uboVS.projection, glm::vec4(0, 0, (float)width, (float)height));
textOverlay->addText(vpos.str(), projected.x, projected.y + (y > -1 ? 5.0f : -20.0f), TextOverlay::alignCenter);
}
}
}
// Display current model view matrix
2017-02-04 14:37:55 +01:00
textOverlay->addText("model view matrix", (float)width, 5.0f, TextOverlay::alignRight);
2016-05-05 14:42:41 +02:00
for (uint32_t i = 0; i < 4; i++)
{
ss.str("");
ss << std::fixed << std::setprecision(2) << std::showpos;
ss << uboVS.model[0][i] << " " << uboVS.model[1][i] << " " << uboVS.model[2][i] << " " << uboVS.model[3][i];
2017-02-04 14:37:55 +01:00
textOverlay->addText(ss.str(), (float)width, 25.0f + (float)i * 20.0f, TextOverlay::alignRight);
}
glm::vec3 projected = glm::project(glm::vec3(0.0f), uboVS.model, uboVS.projection, glm::vec4(0, 0, (float)width, (float)height));
textOverlay->addText("Uniform cube", projected.x, projected.y, TextOverlay::alignCenter);
#if defined(__ANDROID__)
// toto
#else
textOverlay->addText("Hold middle mouse button and drag to move", 5.0f, 85.0f, TextOverlay::alignLeft);
#endif
textOverlay->endTextUpdate();
}
void loadAssets()
{
textures.background.loadFromFile(getAssetPath() + "textures/skysphere_bc3.ktx", VK_FORMAT_BC3_UNORM_BLOCK, vulkanDevice, queue);
textures.cube.loadFromFile(getAssetPath() + "textures/round_window_bc3.ktx", VK_FORMAT_BC3_UNORM_BLOCK, vulkanDevice, queue);
models.cube.loadFromFile(getAssetPath() + "models/cube.dae", vertexLayout, 1.0f, vulkanDevice, queue);
}
void setupVertexDescriptions()
{
// Binding description
vertices.bindingDescriptions.resize(1);
vertices.bindingDescriptions[0] =
vkTools::initializers::vertexInputBindingDescription(
VERTEX_BUFFER_BIND_ID,
vertexLayout.stride(),
VK_VERTEX_INPUT_RATE_VERTEX);
// Attribute descriptions
vertices.attributeDescriptions.resize(4);
// Location 0 : Position
vertices.attributeDescriptions[0] =
vkTools::initializers::vertexInputAttributeDescription(
VERTEX_BUFFER_BIND_ID,
0,
VK_FORMAT_R32G32B32_SFLOAT,
0);
// Location 1 : Normal
vertices.attributeDescriptions[1] =
vkTools::initializers::vertexInputAttributeDescription(
VERTEX_BUFFER_BIND_ID,
1,
VK_FORMAT_R32G32B32_SFLOAT,
sizeof(float) * 3);
// Location 2 : Texture coordinates
vertices.attributeDescriptions[2] =
vkTools::initializers::vertexInputAttributeDescription(
VERTEX_BUFFER_BIND_ID,
2,
VK_FORMAT_R32G32_SFLOAT,
sizeof(float) * 6);
// Location 3 : Color
vertices.attributeDescriptions[3] =
vkTools::initializers::vertexInputAttributeDescription(
VERTEX_BUFFER_BIND_ID,
3,
VK_FORMAT_R32G32B32_SFLOAT,
sizeof(float) * 8);
vertices.inputState = vkTools::initializers::pipelineVertexInputStateCreateInfo();
2017-02-04 14:37:55 +01:00
vertices.inputState.vertexBindingDescriptionCount = static_cast<uint32_t>(vertices.bindingDescriptions.size());
vertices.inputState.pVertexBindingDescriptions = vertices.bindingDescriptions.data();
2017-02-04 14:37:55 +01:00
vertices.inputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(vertices.attributeDescriptions.size());
vertices.inputState.pVertexAttributeDescriptions = vertices.attributeDescriptions.data();
}
void setupDescriptorPool()
{
std::vector<VkDescriptorPoolSize> poolSizes =
{
vkTools::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 2),
vkTools::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2),
};
VkDescriptorPoolCreateInfo descriptorPoolInfo =
vkTools::initializers::descriptorPoolCreateInfo(
2017-02-04 14:37:55 +01:00
static_cast<uint32_t>(poolSizes.size()),
poolSizes.data(),
2);
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
}
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(),
2017-02-04 14:37:55 +01:00
static_cast<uint32_t>(setLayoutBindings.size()));
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout));
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
vkTools::initializers::pipelineLayoutCreateInfo(
&descriptorSetLayout,
1);
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout));
}
void setupDescriptorSet()
{
VkDescriptorSetAllocateInfo allocInfo =
vkTools::initializers::descriptorSetAllocateInfo(
descriptorPool,
&descriptorSetLayout,
1);
// Background
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.background));
VkDescriptorImageInfo texDescriptor =
vkTools::initializers::descriptorImageInfo(
textures.background.sampler,
textures.background.view,
VK_IMAGE_LAYOUT_GENERAL);
std::vector<VkWriteDescriptorSet> writeDescriptorSets;
// Binding 0 : Vertex shader uniform buffer
writeDescriptorSets.push_back(
vkTools::initializers::writeDescriptorSet(
descriptorSets.background,
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
0,
&uniformBuffer.descriptor));
// Binding 1 : Color map
writeDescriptorSets.push_back(
vkTools::initializers::writeDescriptorSet(
descriptorSets.background,
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1,
&texDescriptor));
2017-02-04 14:37:55 +01:00
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
// Cube
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSets.cube));
texDescriptor.sampler = textures.cube.sampler;
texDescriptor.imageView = textures.cube.view;
writeDescriptorSets[0].dstSet = descriptorSets.cube;
writeDescriptorSets[1].dstSet = descriptorSets.cube;
2017-02-04 14:37:55 +01:00
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
}
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(),
2017-02-04 14:37:55 +01:00
static_cast<uint32_t>(dynamicStateEnables.size()),
0);
// Wire frame rendering pipeline
std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages;
shaderStages[0] = loadShader(getAssetPath() + "shaders/textoverlay/mesh.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
shaderStages[1] = loadShader(getAssetPath() + "shaders/textoverlay/mesh.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
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;
2017-02-04 14:37:55 +01:00
pipelineCreateInfo.stageCount = static_cast<uint32_t>(shaderStages.size());
pipelineCreateInfo.pStages = shaderStages.data();
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.solid));
// Background rendering pipeline
depthStencilState.depthTestEnable = VK_FALSE;
depthStencilState.depthWriteEnable = VK_FALSE;
rasterizationState.polygonMode = VK_POLYGON_MODE_FILL;
shaderStages[0] = loadShader(getAssetPath() + "shaders/textoverlay/background.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
shaderStages[1] = loadShader(getAssetPath() + "shaders/textoverlay/background.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipelines.background));
}
// Prepare and initialize uniform buffer containing shader uniforms
void prepareUniformBuffers()
{
// Vertex shader uniform buffer block
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(uboVS)));
// Map persistent
VK_CHECK_RESULT(uniformBuffer.map());
updateUniformBuffers();
}
void updateUniformBuffers()
{
// Vertex shader
uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.1f, 256.0f);
2016-05-05 14:42:41 +02:00
glm::mat4 viewMatrix = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, zoom));
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));
memcpy(uniformBuffer.mapped, &uboVS, sizeof(uboVS));
}
void prepareTextOverlay()
{
// Load the text rendering shaders
std::vector<VkPipelineShaderStageCreateInfo> shaderStages;
shaderStages.push_back(loadShader(getAssetPath() + "shaders/textoverlay/text.vert.spv", VK_SHADER_STAGE_VERTEX_BIT));
shaderStages.push_back(loadShader(getAssetPath() + "shaders/textoverlay/text.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT));
textOverlay = new TextOverlay(
2016-07-23 23:44:26 +02:00
vulkanDevice,
2016-05-05 14:42:41 +02:00
queue,
frameBuffers,
swapChain.colorFormat,
depthFormat,
&width,
&height,
shaderStages
);
updateTextOverlay();
}
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));
// Submit text overlay to queue
textOverlay->submit(queue, currentBuffer);
VulkanExampleBase::submitFrame();
}
void prepare()
{
VulkanExampleBase::prepare();
loadAssets();
setupVertexDescriptions();
prepareUniformBuffers();
setupDescriptorSetLayout();
preparePipelines();
setupDescriptorPool();
setupDescriptorSet();
buildCommandBuffers();
prepareTextOverlay();
prepared = true;
}
virtual void render()
{
if (!prepared)
return;
draw();
if (frameCounter == 0)
{
vkDeviceWaitIdle(device);
updateTextOverlay();
}
}
virtual void viewChanged()
{
vkDeviceWaitIdle(device);
updateUniformBuffers();
updateTextOverlay();
}
virtual void windowResized()
{
updateTextOverlay();
}
virtual void keyPressed(uint32_t keyCode)
{
switch (keyCode)
{
case KEY_KPADD:
case KEY_SPACE:
textOverlay->visible = !textOverlay->visible;
}
}
};
VULKAN_EXAMPLE_MAIN()