2016-05-03 22:38:57 +02:00
|
|
|
/*
|
|
|
|
|
* 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 <sstream>
|
|
|
|
|
#include <iomanip>
|
|
|
|
|
#include "vulkanexamplebase.h"
|
2020-07-28 20:20:38 +02:00
|
|
|
#include "VulkanglTFModel.h"
|
2016-05-03 22:38:57 +02:00
|
|
|
#include "../external/stb/stb_font_consolas_24_latin1.inl"
|
|
|
|
|
|
|
|
|
|
#define ENABLE_VALIDATION false
|
|
|
|
|
|
|
|
|
|
// Max. number of chars the text overlay buffer can hold
|
2017-02-04 14:37:55 +01:00
|
|
|
#define TEXTOVERLAY_MAX_CHAR_COUNT 2048
|
2016-05-03 22:38:57 +02:00
|
|
|
|
2018-03-24 11:35:02 +01:00
|
|
|
/*
|
|
|
|
|
Mostly self-contained text overlay class
|
|
|
|
|
*/
|
2016-05-15 13:33:57 +02:00
|
|
|
class TextOverlay
|
2016-05-03 22:38:57 +02:00
|
|
|
{
|
|
|
|
|
private:
|
2017-02-12 10:16:07 +01:00
|
|
|
vks::VulkanDevice *vulkanDevice;
|
2016-07-23 23:44:26 +02:00
|
|
|
|
2016-05-05 14:42:41 +02:00
|
|
|
VkQueue queue;
|
2016-05-03 22:38:57 +02:00
|
|
|
VkFormat colorFormat;
|
|
|
|
|
VkFormat depthFormat;
|
2016-05-05 13:03:56 +02:00
|
|
|
|
|
|
|
|
uint32_t *frameBufferWidth;
|
|
|
|
|
uint32_t *frameBufferHeight;
|
2022-07-12 18:46:39 -04:00
|
|
|
float scale;
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
VkSampler sampler;
|
|
|
|
|
VkImage image;
|
|
|
|
|
VkImageView view;
|
|
|
|
|
VkBuffer buffer;
|
2016-05-05 13:03:56 +02:00
|
|
|
VkDeviceMemory memory;
|
2016-05-03 22:38:57 +02:00
|
|
|
VkDeviceMemory imageMemory;
|
|
|
|
|
VkDescriptorPool descriptorPool;
|
|
|
|
|
VkDescriptorSetLayout descriptorSetLayout;
|
|
|
|
|
VkDescriptorSet descriptorSet;
|
|
|
|
|
VkPipelineLayout pipelineLayout;
|
|
|
|
|
VkPipelineCache pipelineCache;
|
|
|
|
|
VkPipeline pipeline;
|
2016-05-05 18:54:17 +02:00
|
|
|
VkRenderPass renderPass;
|
2016-05-03 22:38:57 +02:00
|
|
|
VkCommandPool commandPool;
|
2016-05-05 13:03:56 +02:00
|
|
|
std::vector<VkFramebuffer*> frameBuffers;
|
2016-05-05 18:54:17 +02:00
|
|
|
std::vector<VkPipelineShaderStageCreateInfo> shaderStages;
|
2016-05-03 22:38:57 +02:00
|
|
|
|
2016-05-05 13:03:56 +02:00
|
|
|
// Pointer to mapped vertex buffer
|
|
|
|
|
glm::vec4 *mapped = nullptr;
|
|
|
|
|
|
2018-03-24 11:35:02 +01:00
|
|
|
stb_fontchar stbFontData[STB_FONT_consolas_24_latin1_NUM_CHARS];
|
2016-05-05 13:03:56 +02:00
|
|
|
uint32_t numLetters;
|
2016-05-03 22:38:57 +02:00
|
|
|
public:
|
2016-05-05 20:52:46 +02:00
|
|
|
|
|
|
|
|
enum TextAlign { alignLeft, alignCenter, alignRight };
|
|
|
|
|
|
2016-05-07 12:37:12 +02:00
|
|
|
bool visible = true;
|
|
|
|
|
|
2020-02-05 19:48:49 +01:00
|
|
|
std::vector<VkCommandBuffer> cmdBuffers;
|
|
|
|
|
|
2016-05-15 13:33:57 +02:00
|
|
|
TextOverlay(
|
2017-02-12 10:16:07 +01:00
|
|
|
vks::VulkanDevice *vulkanDevice,
|
2016-05-05 14:42:41 +02:00
|
|
|
VkQueue queue,
|
2016-05-05 13:03:56 +02:00
|
|
|
std::vector<VkFramebuffer> &framebuffers,
|
2016-05-03 22:38:57 +02:00
|
|
|
VkFormat colorformat,
|
|
|
|
|
VkFormat depthformat,
|
2016-05-05 13:03:56 +02:00
|
|
|
uint32_t *framebufferwidth,
|
2016-05-05 18:54:17 +02:00
|
|
|
uint32_t *framebufferheight,
|
2022-07-12 18:46:39 -04:00
|
|
|
float scale,
|
2016-05-05 18:54:17 +02:00
|
|
|
std::vector<VkPipelineShaderStageCreateInfo> shaderstages)
|
2016-05-03 22:38:57 +02:00
|
|
|
{
|
2016-07-23 23:44:26 +02:00
|
|
|
this->vulkanDevice = vulkanDevice;
|
2016-05-05 14:42:41 +02:00
|
|
|
this->queue = queue;
|
2016-05-03 22:38:57 +02:00
|
|
|
this->colorFormat = colorformat;
|
|
|
|
|
this->depthFormat = depthformat;
|
2016-05-05 13:03:56 +02:00
|
|
|
|
|
|
|
|
this->frameBuffers.resize(framebuffers.size());
|
|
|
|
|
for (uint32_t i = 0; i < framebuffers.size(); i++)
|
|
|
|
|
{
|
|
|
|
|
this->frameBuffers[i] = &framebuffers[i];
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-05 18:54:17 +02:00
|
|
|
this->shaderStages = shaderstages;
|
|
|
|
|
|
2016-05-05 13:03:56 +02:00
|
|
|
this->frameBufferWidth = framebufferwidth;
|
|
|
|
|
this->frameBufferHeight = framebufferheight;
|
2022-07-12 18:46:39 -04:00
|
|
|
this->scale = scale;
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
cmdBuffers.resize(framebuffers.size());
|
|
|
|
|
prepareResources();
|
|
|
|
|
prepareRenderPass();
|
|
|
|
|
preparePipeline();
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-15 13:33:57 +02:00
|
|
|
~TextOverlay()
|
2016-05-05 18:54:17 +02:00
|
|
|
{
|
|
|
|
|
// 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);
|
2016-05-05 18:54:17 +02:00
|
|
|
}
|
|
|
|
|
|
2016-05-03 22:38:57 +02:00
|
|
|
// 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()
|
|
|
|
|
{
|
2018-03-24 11:35:02 +01:00
|
|
|
const uint32_t fontWidth = STB_FONT_consolas_24_latin1_BITMAP_WIDTH;
|
|
|
|
|
const uint32_t fontHeight = STB_FONT_consolas_24_latin1_BITMAP_WIDTH;
|
|
|
|
|
|
|
|
|
|
static unsigned char font24pixels[fontWidth][fontHeight];
|
|
|
|
|
stb_font_consolas_24_latin1(stbFontData, font24pixels, fontHeight);
|
2016-05-03 22:38:57 +02:00
|
|
|
|
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 =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::commandBufferAllocateInfo(
|
2016-05-05 14:42:41 +02:00
|
|
|
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-03 22:38:57 +02:00
|
|
|
|
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);
|
2016-05-03 22:38:57 +02:00
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkBufferCreateInfo bufferInfo = vks::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));
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
VkMemoryRequirements memReqs;
|
2017-02-12 11:12:42 +01:00
|
|
|
VkMemoryAllocateInfo allocInfo = vks::initializers::memoryAllocateInfo();
|
2016-05-03 22:38:57 +02:00
|
|
|
|
2016-07-23 23:44:26 +02:00
|
|
|
vkGetBufferMemoryRequirements(vulkanDevice->logicalDevice, buffer, &memReqs);
|
2016-05-03 22:38:57 +02:00
|
|
|
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-05-03 22:38:57 +02:00
|
|
|
|
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-03 22:38:57 +02:00
|
|
|
|
2016-05-05 14:42:41 +02:00
|
|
|
// Font texture
|
2017-02-12 11:12:42 +01:00
|
|
|
VkImageCreateInfo imageInfo = vks::initializers::imageCreateInfo();
|
2016-05-03 22:38:57 +02:00
|
|
|
imageInfo.imageType = VK_IMAGE_TYPE_2D;
|
|
|
|
|
imageInfo.format = VK_FORMAT_R8_UNORM;
|
2018-03-24 11:35:02 +01:00
|
|
|
imageInfo.extent.width = fontWidth;
|
|
|
|
|
imageInfo.extent.height = fontHeight;
|
2016-05-03 22:38:57 +02:00
|
|
|
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;
|
2016-05-03 22:38:57 +02:00
|
|
|
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
2016-06-22 20:30:14 +02:00
|
|
|
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
2016-05-03 22:38:57 +02:00
|
|
|
|
2016-07-23 23:44:26 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateImage(vulkanDevice->logicalDevice, &imageInfo, nullptr, &image));
|
2016-05-03 22:38:57 +02:00
|
|
|
|
2016-07-23 23:44:26 +02:00
|
|
|
vkGetImageMemoryRequirements(vulkanDevice->logicalDevice, image, &memReqs);
|
2016-05-17 15:42:29 +01:00
|
|
|
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-05-03 22:38:57 +02:00
|
|
|
|
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-03 22:38:57 +02:00
|
|
|
|
2016-05-05 14:42:41 +02:00
|
|
|
// Staging
|
|
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
VkDeviceMemory memory;
|
|
|
|
|
VkBuffer buffer;
|
|
|
|
|
} stagingBuffer;
|
|
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkBufferCreateInfo bufferCreateInfo = vks::initializers::bufferCreateInfo();
|
2016-05-05 14:42:41 +02:00
|
|
|
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));
|
2016-08-22 12:57:40 +02:00
|
|
|
// Size of the font texture is WIDTH * HEIGHT * 1 byte (only one channel)
|
2018-03-24 11:35:02 +01:00
|
|
|
memcpy(data, &font24pixels[0][0], fontWidth * fontHeight);
|
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, ©Cmd));
|
2020-05-29 16:08:53 +01:00
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
|
2016-05-08 11:40:25 +02:00
|
|
|
VK_CHECK_RESULT(vkBeginCommandBuffer(copyCmd, &cmdBufInfo));
|
2016-05-05 14:42:41 +02:00
|
|
|
|
|
|
|
|
// Prepare for transfer
|
2017-02-12 13:10:05 +01:00
|
|
|
vks::tools::setImageLayout(
|
2016-05-05 14:42:41 +02:00
|
|
|
copyCmd,
|
|
|
|
|
image,
|
|
|
|
|
VK_IMAGE_ASPECT_COLOR_BIT,
|
2016-06-22 20:30:14 +02:00
|
|
|
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;
|
2018-03-24 11:35:02 +01:00
|
|
|
bufferCopyRegion.imageExtent.width = fontWidth;
|
|
|
|
|
bufferCopyRegion.imageExtent.height = fontHeight;
|
2016-05-05 14:42:41 +02:00
|
|
|
bufferCopyRegion.imageExtent.depth = 1;
|
|
|
|
|
|
|
|
|
|
vkCmdCopyBufferToImage(
|
|
|
|
|
copyCmd,
|
|
|
|
|
stagingBuffer.buffer,
|
|
|
|
|
image,
|
|
|
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
|
|
|
|
1,
|
|
|
|
|
&bufferCopyRegion
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Prepare for shader read
|
2017-02-12 13:10:05 +01:00
|
|
|
vks::tools::setImageLayout(
|
2016-05-05 14:42:41 +02:00
|
|
|
copyCmd,
|
|
|
|
|
image,
|
|
|
|
|
VK_IMAGE_ASPECT_COLOR_BIT,
|
|
|
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
|
|
|
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
|
|
|
|
|
2016-05-08 11:40:25 +02:00
|
|
|
VK_CHECK_RESULT(vkEndCommandBuffer(copyCmd));
|
2016-05-05 14:42:41 +02:00
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkSubmitInfo submitInfo = vks::initializers::submitInfo();
|
2016-05-05 14:42:41 +02:00
|
|
|
submitInfo.commandBufferCount = 1;
|
|
|
|
|
submitInfo.pCommandBuffers = ©Cmd;
|
|
|
|
|
|
2016-05-08 11:40:25 +02:00
|
|
|
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, ©Cmd);
|
|
|
|
|
vkFreeMemory(vulkanDevice->logicalDevice, stagingBuffer.memory, nullptr);
|
|
|
|
|
vkDestroyBuffer(vulkanDevice->logicalDevice, stagingBuffer.buffer, nullptr);
|
2016-05-03 22:38:57 +02:00
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkImageViewCreateInfo imageViewInfo = vks::initializers::imageViewCreateInfo();
|
2016-05-03 22:38:57 +02:00
|
|
|
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));
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
// Sampler
|
2017-02-12 11:12:42 +01:00
|
|
|
VkSamplerCreateInfo samplerInfo = vks::initializers::samplerCreateInfo();
|
2016-05-03 22:38:57 +02:00
|
|
|
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));
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
// Descriptor
|
|
|
|
|
// Font uses a separate descriptor pool
|
|
|
|
|
std::array<VkDescriptorPoolSize, 1> poolSizes;
|
2017-02-12 11:12:42 +01:00
|
|
|
poolSizes[0] = vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1);
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
VkDescriptorPoolCreateInfo descriptorPoolInfo =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::descriptorPoolCreateInfo(
|
2017-02-04 14:37:55 +01:00
|
|
|
static_cast<uint32_t>(poolSizes.size()),
|
2016-05-03 22:38:57 +02:00
|
|
|
poolSizes.data(),
|
|
|
|
|
1);
|
|
|
|
|
|
2016-07-23 23:44:26 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateDescriptorPool(vulkanDevice->logicalDevice, &descriptorPoolInfo, nullptr, &descriptorPool));
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
// Descriptor set layout
|
|
|
|
|
std::array<VkDescriptorSetLayoutBinding, 1> setLayoutBindings;
|
2017-02-12 11:12:42 +01:00
|
|
|
setLayoutBindings[0] = vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 0);
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
VkDescriptorSetLayoutCreateInfo descriptorSetLayoutInfo =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::descriptorSetLayoutCreateInfo(
|
2016-05-03 22:38:57 +02:00
|
|
|
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));
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
// Pipeline layout
|
|
|
|
|
VkPipelineLayoutCreateInfo pipelineLayoutInfo =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::pipelineLayoutCreateInfo(
|
2016-05-03 22:38:57 +02:00
|
|
|
&descriptorSetLayout,
|
|
|
|
|
1);
|
2016-07-23 23:44:26 +02:00
|
|
|
VK_CHECK_RESULT(vkCreatePipelineLayout(vulkanDevice->logicalDevice, &pipelineLayoutInfo, nullptr, &pipelineLayout));
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
// Descriptor set
|
|
|
|
|
VkDescriptorSetAllocateInfo descriptorSetAllocInfo =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::descriptorSetAllocateInfo(
|
2016-05-03 22:38:57 +02:00
|
|
|
descriptorPool,
|
|
|
|
|
&descriptorSetLayout,
|
|
|
|
|
1);
|
|
|
|
|
|
2016-07-23 23:44:26 +02:00
|
|
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(vulkanDevice->logicalDevice, &descriptorSetAllocInfo, &descriptorSet));
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
VkDescriptorImageInfo texDescriptor =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::descriptorImageInfo(
|
2016-05-03 22:38:57 +02:00
|
|
|
sampler,
|
|
|
|
|
view,
|
2020-02-05 19:15:37 +01:00
|
|
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
std::array<VkWriteDescriptorSet, 1> writeDescriptorSets;
|
2017-02-12 11:12:42 +01:00
|
|
|
writeDescriptorSets[0] = vks::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);
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
// 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));
|
2016-05-03 22:38:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prepare a separate pipeline for the font rendering decoupled from the main application
|
|
|
|
|
void preparePipeline()
|
|
|
|
|
{
|
2018-03-24 11:35:02 +01:00
|
|
|
// Enable blending, using alpha from red channel of the font texture (see text.frag)
|
|
|
|
|
VkPipelineColorBlendAttachmentState blendAttachmentState{};
|
|
|
|
|
blendAttachmentState.blendEnable = VK_TRUE;
|
|
|
|
|
blendAttachmentState.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
|
|
|
|
|
blendAttachmentState.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
|
|
|
|
|
blendAttachmentState.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
|
2016-05-03 22:38:57 +02:00
|
|
|
blendAttachmentState.colorBlendOp = VK_BLEND_OP_ADD;
|
2018-03-24 11:35:02 +01:00
|
|
|
blendAttachmentState.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
|
|
|
|
|
blendAttachmentState.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
|
2016-05-03 22:38:57 +02:00
|
|
|
blendAttachmentState.alphaBlendOp = VK_BLEND_OP_ADD;
|
|
|
|
|
|
2018-03-24 11:35:02 +01:00
|
|
|
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP, 0, VK_FALSE);
|
|
|
|
|
VkPipelineRasterizationStateCreateInfo rasterizationState = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_CLOCKWISE, 0);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
std::array<VkVertexInputBindingDescription, 2> vertexInputBindings = {
|
|
|
|
|
vks::initializers::vertexInputBindingDescription(0, sizeof(glm::vec4), VK_VERTEX_INPUT_RATE_VERTEX),
|
|
|
|
|
vks::initializers::vertexInputBindingDescription(1, sizeof(glm::vec4), VK_VERTEX_INPUT_RATE_VERTEX),
|
2016-05-03 22:38:57 +02:00
|
|
|
};
|
2018-03-24 11:35:02 +01:00
|
|
|
std::array<VkVertexInputAttributeDescription, 2> vertexInputAttributes = {
|
|
|
|
|
vks::initializers::vertexInputAttributeDescription(0, 0, VK_FORMAT_R32G32_SFLOAT, 0), // Location 0: Position
|
|
|
|
|
vks::initializers::vertexInputAttributeDescription(1, 1, VK_FORMAT_R32G32_SFLOAT, sizeof(glm::vec2)), // Location 1: UV
|
|
|
|
|
};
|
2020-05-29 16:08:53 +01:00
|
|
|
|
2018-03-24 11:35:02 +01:00
|
|
|
VkPipelineVertexInputStateCreateInfo vertexInputState = vks::initializers::pipelineVertexInputStateCreateInfo();
|
|
|
|
|
vertexInputState.vertexBindingDescriptionCount = static_cast<uint32_t>(vertexInputBindings.size());
|
|
|
|
|
vertexInputState.pVertexBindingDescriptions = vertexInputBindings.data();
|
|
|
|
|
vertexInputState.vertexAttributeDescriptionCount = static_cast<uint32_t>(vertexInputAttributes.size());
|
|
|
|
|
vertexInputState.pVertexAttributeDescriptions = vertexInputAttributes.data();
|
|
|
|
|
|
|
|
|
|
VkGraphicsPipelineCreateInfo pipelineCreateInfo = vks::initializers::pipelineCreateInfo(pipelineLayout, renderPass, 0);
|
|
|
|
|
pipelineCreateInfo.pVertexInputState = &vertexInputState;
|
2016-05-03 22:38:57 +02:00
|
|
|
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());
|
2016-05-03 22:38:57 +02:00
|
|
|
pipelineCreateInfo.pStages = shaderStages.data();
|
|
|
|
|
|
2016-07-23 23:44:26 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(vulkanDevice->logicalDevice, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipeline));
|
2016-05-03 22:38:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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;
|
2018-02-01 12:40:08 +01:00
|
|
|
attachments[0].initialLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
2016-08-12 20:36:49 +02:00
|
|
|
attachments[0].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
// 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;
|
2016-08-12 20:36:49 +02:00
|
|
|
attachments[1].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
2016-05-03 22:38:57 +02:00
|
|
|
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;
|
|
|
|
|
|
2016-08-12 20:36:49 +02:00
|
|
|
// Use subpass dependencies for image layout transitions
|
|
|
|
|
VkSubpassDependency subpassDependencies[2] = {};
|
|
|
|
|
|
2020-08-09 13:48:49 +02:00
|
|
|
// Transition from final to initial (VK_SUBPASS_EXTERNAL refers to all commands executed outside of the actual renderpass)
|
2016-08-12 20:36:49 +02:00
|
|
|
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;
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
VkRenderPassCreateInfo renderPassInfo = {};
|
|
|
|
|
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
|
|
|
|
|
renderPassInfo.pNext = NULL;
|
|
|
|
|
renderPassInfo.attachmentCount = 2;
|
|
|
|
|
renderPassInfo.pAttachments = attachments;
|
|
|
|
|
renderPassInfo.subpassCount = 1;
|
2016-08-12 20:36:49 +02:00
|
|
|
renderPassInfo.pSubpasses = &subpassDescription;
|
|
|
|
|
renderPassInfo.dependencyCount = 2;
|
|
|
|
|
renderPassInfo.pDependencies = subpassDependencies;
|
2016-05-03 22:38:57 +02:00
|
|
|
|
2016-07-23 23:44:26 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateRenderPass(vulkanDevice->logicalDevice, &renderPassInfo, nullptr, &renderPass));
|
2016-05-03 22:38:57 +02:00
|
|
|
}
|
2016-05-05 13:03:56 +02:00
|
|
|
|
2020-05-29 16:08:53 +01:00
|
|
|
// Map buffer
|
2016-05-03 22:38:57 +02:00
|
|
|
void beginTextUpdate()
|
|
|
|
|
{
|
2016-07-23 23:44:26 +02:00
|
|
|
VK_CHECK_RESULT(vkMapMemory(vulkanDevice->logicalDevice, memory, 0, VK_WHOLE_SIZE, 0, (void **)&mapped));
|
2016-05-03 22:38:57 +02:00
|
|
|
numLetters = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add text to the current buffer
|
|
|
|
|
// todo : drop shadow? color attribute?
|
2016-05-05 20:52:46 +02:00
|
|
|
void addText(std::string text, float x, float y, TextAlign align)
|
2016-05-03 22:38:57 +02:00
|
|
|
{
|
2018-03-24 11:35:02 +01:00
|
|
|
const uint32_t firstChar = STB_FONT_consolas_24_latin1_FIRST_CHAR;
|
|
|
|
|
|
2016-05-03 22:38:57 +02:00
|
|
|
assert(mapped != nullptr);
|
|
|
|
|
|
2022-07-12 18:46:39 -04:00
|
|
|
const float charW = 1.5f * scale / *frameBufferWidth;
|
|
|
|
|
const float charH = 1.5f * scale / *frameBufferHeight;
|
2016-05-05 20:52:46 +02:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2018-03-24 11:35:02 +01:00
|
|
|
stb_fontchar *charData = &stbFontData[(uint32_t)letter - firstChar];
|
2016-05-05 20:52:46 +02:00
|
|
|
textWidth += charData->advance * charW;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (align)
|
|
|
|
|
{
|
|
|
|
|
case alignRight:
|
|
|
|
|
x -= textWidth;
|
|
|
|
|
break;
|
|
|
|
|
case alignCenter:
|
|
|
|
|
x -= textWidth / 2.0f;
|
|
|
|
|
break;
|
2022-06-13 23:04:53 -04:00
|
|
|
case alignLeft:
|
|
|
|
|
break;
|
2016-05-05 20:52:46 +02:00
|
|
|
}
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
// Generate a uv mapped quad per char in the new text
|
|
|
|
|
for (auto letter : text)
|
|
|
|
|
{
|
2018-03-24 11:35:02 +01:00
|
|
|
stb_fontchar *charData = &stbFontData[(uint32_t)letter - firstChar];
|
2016-05-03 22:38:57 +02:00
|
|
|
|
2016-05-05 20:52:46 +02:00
|
|
|
mapped->x = (x + (float)charData->x0 * charW);
|
|
|
|
|
mapped->y = (y + (float)charData->y0 * charH);
|
2016-05-03 22:38:57 +02:00
|
|
|
mapped->z = charData->s0;
|
|
|
|
|
mapped->w = charData->t0;
|
|
|
|
|
mapped++;
|
|
|
|
|
|
2016-05-05 20:52:46 +02:00
|
|
|
mapped->x = (x + (float)charData->x1 * charW);
|
|
|
|
|
mapped->y = (y + (float)charData->y0 * charH);
|
2016-05-03 22:38:57 +02:00
|
|
|
mapped->z = charData->s1;
|
|
|
|
|
mapped->w = charData->t0;
|
|
|
|
|
mapped++;
|
|
|
|
|
|
2016-05-05 20:52:46 +02:00
|
|
|
mapped->x = (x + (float)charData->x0 * charW);
|
|
|
|
|
mapped->y = (y + (float)charData->y1 * charH);
|
2016-05-03 22:38:57 +02:00
|
|
|
mapped->z = charData->s0;
|
|
|
|
|
mapped->w = charData->t1;
|
|
|
|
|
mapped++;
|
|
|
|
|
|
2016-05-05 20:52:46 +02:00
|
|
|
mapped->x = (x + (float)charData->x1 * charW);
|
|
|
|
|
mapped->y = (y + (float)charData->y1 * charH);
|
2016-05-03 22:38:57 +02:00
|
|
|
mapped->z = charData->s1;
|
|
|
|
|
mapped->w = charData->t1;
|
|
|
|
|
mapped++;
|
|
|
|
|
|
2016-05-05 20:52:46 +02:00
|
|
|
x += charData->advance * charW;
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
numLetters++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Unmap buffer and update command buffers
|
|
|
|
|
void endTextUpdate()
|
|
|
|
|
{
|
2016-07-23 23:44:26 +02:00
|
|
|
vkUnmapMemory(vulkanDevice->logicalDevice, memory);
|
2016-05-03 22:38:57 +02:00
|
|
|
mapped = nullptr;
|
|
|
|
|
updateCommandBuffers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Needs to be called by the application
|
|
|
|
|
void updateCommandBuffers()
|
|
|
|
|
{
|
2017-02-12 11:12:42 +01:00
|
|
|
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
|
2016-05-03 22:38:57 +02:00
|
|
|
|
2016-05-23 15:16:35 -07:00
|
|
|
VkClearValue clearValues[2];
|
|
|
|
|
clearValues[1].color = { { 0.0f, 0.0f, 0.0f, 0.0f } };
|
2016-05-03 22:38:57 +02:00
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkRenderPassBeginInfo renderPassBeginInfo = vks::initializers::renderPassBeginInfo();
|
2016-05-03 22:38:57 +02:00
|
|
|
renderPassBeginInfo.renderPass = renderPass;
|
2016-05-05 13:03:56 +02:00
|
|
|
renderPassBeginInfo.renderArea.extent.width = *frameBufferWidth;
|
|
|
|
|
renderPassBeginInfo.renderArea.extent.height = *frameBufferHeight;
|
2016-05-23 15:16:35 -07:00
|
|
|
renderPassBeginInfo.clearValueCount = 2;
|
2016-05-03 22:38:57 +02:00
|
|
|
renderPassBeginInfo.pClearValues = clearValues;
|
|
|
|
|
|
|
|
|
|
for (int32_t i = 0; i < cmdBuffers.size(); ++i)
|
|
|
|
|
{
|
2016-05-05 13:03:56 +02:00
|
|
|
renderPassBeginInfo.framebuffer = *frameBuffers[i];
|
2016-05-03 22:38:57 +02:00
|
|
|
|
2016-05-08 11:40:25 +02:00
|
|
|
VK_CHECK_RESULT(vkBeginCommandBuffer(cmdBuffers[i], &cmdBufInfo));
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
vkCmdBeginRenderPass(cmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
|
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkViewport viewport = vks::initializers::viewport((float)*frameBufferWidth, (float)*frameBufferHeight, 0.0f, 1.0f);
|
2016-05-03 22:38:57 +02:00
|
|
|
vkCmdSetViewport(cmdBuffers[i], 0, 1, &viewport);
|
|
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkRect2D scissor = vks::initializers::rect2D(*frameBufferWidth, *frameBufferHeight, 0, 0);
|
2016-05-03 22:38:57 +02:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-05 18:54:17 +02:00
|
|
|
|
2016-05-03 22:38:57 +02:00
|
|
|
vkCmdEndRenderPass(cmdBuffers[i]);
|
|
|
|
|
|
2016-05-08 11:40:25 +02:00
|
|
|
VK_CHECK_RESULT(vkEndCommandBuffer(cmdBuffers[i]));
|
2016-05-03 22:38:57 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-03-24 11:35:02 +01:00
|
|
|
/*
|
|
|
|
|
Vulkan example main class
|
|
|
|
|
*/
|
2016-05-03 22:38:57 +02:00
|
|
|
class VulkanExample : public VulkanExampleBase
|
|
|
|
|
{
|
|
|
|
|
public:
|
2016-05-15 13:33:57 +02:00
|
|
|
TextOverlay *textOverlay = nullptr;
|
2016-05-03 22:38:57 +02:00
|
|
|
|
2020-07-28 20:20:38 +02:00
|
|
|
vkglTF::Model model;
|
2017-02-04 15:00:45 +01:00
|
|
|
|
2017-02-12 10:44:51 +01:00
|
|
|
vks::Buffer uniformBuffer;
|
2016-05-03 22:38:57 +02:00
|
|
|
|
2016-12-24 12:48:01 +01:00
|
|
|
struct UBOVS {
|
2016-05-03 22:38:57 +02:00
|
|
|
glm::mat4 projection;
|
2020-04-22 20:58:24 +02:00
|
|
|
glm::mat4 modelView;
|
2016-05-07 12:37:12 +02:00
|
|
|
glm::vec4 lightPos = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
|
2016-05-03 22:38:57 +02:00
|
|
|
} uboVS;
|
|
|
|
|
|
|
|
|
|
VkPipelineLayout pipelineLayout;
|
2018-03-24 11:35:02 +01:00
|
|
|
VkPipeline pipeline;
|
2020-05-29 16:08:53 +01:00
|
|
|
|
2016-05-03 22:38:57 +02:00
|
|
|
VkDescriptorSetLayout descriptorSetLayout;
|
2018-03-24 11:35:02 +01:00
|
|
|
VkDescriptorSet descriptorSet;
|
2016-05-07 12:37:12 +02:00
|
|
|
|
2016-05-03 22:38:57 +02:00
|
|
|
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
|
|
|
|
|
{
|
|
|
|
|
title = "Vulkan Example - Text overlay";
|
2018-03-24 11:35:02 +01:00
|
|
|
camera.type = Camera::CameraType::lookat;
|
2020-07-28 20:20:38 +02:00
|
|
|
camera.setPosition(glm::vec3(0.0f, 0.0f, -2.5f));
|
2018-03-24 11:35:02 +01:00
|
|
|
camera.setRotation(glm::vec3(-25.0f, -0.0f, 0.0f));
|
|
|
|
|
camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f);
|
2020-04-22 20:58:24 +02:00
|
|
|
settings.overlay = false;
|
2016-05-03 22:38:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~VulkanExample()
|
|
|
|
|
{
|
2018-03-24 11:35:02 +01:00
|
|
|
vkDestroyPipeline(device, pipeline, nullptr);
|
2016-05-03 22:38:57 +02:00
|
|
|
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
|
|
|
|
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
|
2016-12-24 12:48:01 +01:00
|
|
|
uniformBuffer.destroy();
|
2016-05-03 22:38:57 +02:00
|
|
|
delete(textOverlay);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void buildCommandBuffers()
|
|
|
|
|
{
|
2017-02-12 11:12:42 +01:00
|
|
|
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
VkClearValue clearValues[3];
|
|
|
|
|
|
2018-03-24 11:35:02 +01:00
|
|
|
clearValues[0].color = { { 0.0f, 0.0f, 0.2f, 1.0f } };
|
2016-05-03 22:38:57 +02:00
|
|
|
clearValues[1].depthStencil = { 1.0f, 0 };
|
|
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkRenderPassBeginInfo renderPassBeginInfo = vks::initializers::renderPassBeginInfo();
|
2016-05-03 22:38:57 +02:00
|
|
|
renderPassBeginInfo.renderPass = renderPass;
|
|
|
|
|
renderPassBeginInfo.renderArea.extent.width = width;
|
|
|
|
|
renderPassBeginInfo.renderArea.extent.height = height;
|
2017-02-04 15:53:35 +01:00
|
|
|
renderPassBeginInfo.clearValueCount = 2;
|
2016-05-03 22:38:57 +02:00
|
|
|
renderPassBeginInfo.pClearValues = clearValues;
|
|
|
|
|
|
|
|
|
|
for (int32_t i = 0; i < drawCmdBuffers.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
// Set target frame buffer
|
|
|
|
|
renderPassBeginInfo.framebuffer = frameBuffers[i];
|
|
|
|
|
|
2016-05-08 11:40:25 +02:00
|
|
|
VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
|
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkViewport viewport = vks::initializers::viewport((float)width, (float)height, 0.0f, 1.0f);
|
2016-05-03 22:38:57 +02:00
|
|
|
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
|
|
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkRect2D scissor = vks::initializers::rect2D(width, height, 0, 0);
|
2016-05-03 22:38:57 +02:00
|
|
|
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
|
|
|
|
|
|
2018-03-24 11:35:02 +01:00
|
|
|
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
|
2020-07-28 20:20:38 +02:00
|
|
|
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, nullptr);
|
|
|
|
|
model.draw(drawCmdBuffers[i]);
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
vkCmdEndRenderPass(drawCmdBuffers[i]);
|
|
|
|
|
|
2016-05-08 11:40:25 +02:00
|
|
|
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
|
2016-05-03 22:38:57 +02:00
|
|
|
}
|
2016-05-05 13:03:56 +02:00
|
|
|
|
|
|
|
|
vkQueueWaitIdle(queue);
|
2016-05-03 22:38:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update the text buffer displayed by the text overlay
|
|
|
|
|
void updateTextOverlay(void)
|
|
|
|
|
{
|
|
|
|
|
textOverlay->beginTextUpdate();
|
|
|
|
|
|
2022-07-12 18:46:39 -04:00
|
|
|
textOverlay->addText(title, 5.0f * UIOverlay.scale, 5.0f * UIOverlay.scale, TextOverlay::alignLeft);
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
|
ss << std::fixed << std::setprecision(2) << (frameTimer * 1000.0f) << "ms (" << lastFPS << " fps)";
|
2022-07-12 18:46:39 -04:00
|
|
|
textOverlay->addText(ss.str(), 5.0f * UIOverlay.scale, 25.0f * UIOverlay.scale, TextOverlay::alignLeft);
|
2016-05-03 22:38:57 +02:00
|
|
|
|
2022-07-12 18:46:39 -04:00
|
|
|
textOverlay->addText(deviceProperties.deviceName, 5.0f * UIOverlay.scale, 45.0f * UIOverlay.scale, TextOverlay::alignLeft);
|
2020-05-29 16:08:53 +01:00
|
|
|
|
2016-05-07 12:37:12 +02:00
|
|
|
// Display current model view matrix
|
2022-07-12 18:46:39 -04:00
|
|
|
textOverlay->addText("model view matrix", (float)width - 5.0f * UIOverlay.scale, 5.0f * UIOverlay.scale, TextOverlay::alignRight);
|
2016-05-05 14:42:41 +02:00
|
|
|
|
2016-05-07 12:37:12 +02:00
|
|
|
for (uint32_t i = 0; i < 4; i++)
|
|
|
|
|
{
|
|
|
|
|
ss.str("");
|
|
|
|
|
ss << std::fixed << std::setprecision(2) << std::showpos;
|
2020-04-22 20:58:24 +02:00
|
|
|
ss << uboVS.modelView[0][i] << " " << uboVS.modelView[1][i] << " " << uboVS.modelView[2][i] << " " << uboVS.modelView[3][i];
|
2022-07-12 18:46:39 -04:00
|
|
|
textOverlay->addText(ss.str(), (float)width - 5.0f * UIOverlay.scale, (25.0f + (float)i * 20.0f) * UIOverlay.scale, TextOverlay::alignRight);
|
2016-05-07 12:37:12 +02:00
|
|
|
}
|
|
|
|
|
|
2020-04-22 20:58:24 +02:00
|
|
|
glm::vec3 projected = glm::project(glm::vec3(0.0f), uboVS.modelView, uboVS.projection, glm::vec4(0, 0, (float)width, (float)height));
|
2020-07-28 20:20:38 +02:00
|
|
|
textOverlay->addText("A cube", projected.x, projected.y, TextOverlay::alignCenter);
|
2016-05-03 22:38:57 +02:00
|
|
|
|
2016-05-07 12:37:12 +02:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
|
#else
|
2022-07-12 18:46:39 -04:00
|
|
|
textOverlay->addText("Press \"space\" to toggle text overlay", 5.0f * UIOverlay.scale, 65.0f * UIOverlay.scale, TextOverlay::alignLeft);
|
|
|
|
|
textOverlay->addText("Hold middle mouse button and drag to move", 5.0f * UIOverlay.scale, 85.0f * UIOverlay.scale, TextOverlay::alignLeft);
|
2016-05-07 12:37:12 +02:00
|
|
|
#endif
|
2016-05-03 22:38:57 +02:00
|
|
|
textOverlay->endTextUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-04 15:00:45 +01:00
|
|
|
void loadAssets()
|
2016-05-03 22:38:57 +02:00
|
|
|
{
|
2020-07-28 20:20:38 +02:00
|
|
|
const uint32_t glTFLoadingFlags = vkglTF::FileLoadingFlags::PreTransformVertices | vkglTF::FileLoadingFlags::PreMultiplyVertexColors | vkglTF::FileLoadingFlags::FlipY;
|
|
|
|
|
model.loadFromFile(getAssetPath() + "models/cube.gltf", vulkanDevice, queue, glTFLoadingFlags);
|
2016-05-03 22:38:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setupDescriptorPool()
|
|
|
|
|
{
|
2018-03-24 11:35:02 +01:00
|
|
|
std::vector<VkDescriptorPoolSize> poolSizes = {
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 2),
|
2016-05-03 22:38:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
VkDescriptorPoolCreateInfo descriptorPoolInfo =
|
2018-03-24 11:35:02 +01:00
|
|
|
vks::initializers::descriptorPoolCreateInfo(poolSizes, 2);
|
2016-05-08 11:40:25 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
|
2016-05-03 22:38:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setupDescriptorSetLayout()
|
|
|
|
|
{
|
2018-03-24 11:35:02 +01:00
|
|
|
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
|
|
|
|
|
// Binding 0: Vertex shader uniform buffer
|
|
|
|
|
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0),
|
2016-05-03 22:38:57 +02:00
|
|
|
};
|
|
|
|
|
VkDescriptorSetLayoutCreateInfo descriptorLayout =
|
2018-03-24 11:35:02 +01:00
|
|
|
vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings);
|
2016-05-08 11:40:25 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout));
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
|
2018-03-24 11:35:02 +01:00
|
|
|
vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayout, 1);
|
2016-05-08 11:40:25 +02:00
|
|
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout));
|
2016-05-03 22:38:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setupDescriptorSet()
|
|
|
|
|
{
|
|
|
|
|
VkDescriptorSetAllocateInfo allocInfo =
|
2018-03-24 11:35:02 +01:00
|
|
|
vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayout, 1);
|
|
|
|
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet));
|
|
|
|
|
std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
|
|
|
|
|
// Binding 0: Vertex shader uniform buffer
|
|
|
|
|
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffer.descriptor),
|
|
|
|
|
};
|
2017-02-04 14:37:55 +01:00
|
|
|
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
|
2016-05-03 22:38:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void preparePipelines()
|
|
|
|
|
{
|
2018-03-24 11:35:02 +01:00
|
|
|
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE);
|
2020-07-28 20:20:38 +02:00
|
|
|
VkPipelineRasterizationStateCreateInfo rasterizationState = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE, 0);
|
2018-03-24 11:35:02 +01:00
|
|
|
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);
|
2020-07-28 20:20:38 +02:00
|
|
|
std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages;
|
2018-03-24 11:35:02 +01:00
|
|
|
|
2020-07-28 20:20:38 +02:00
|
|
|
VkGraphicsPipelineCreateInfo pipelineCI = vks::initializers::pipelineCreateInfo(pipelineLayout, renderPass);
|
|
|
|
|
pipelineCI.pInputAssemblyState = &inputAssemblyState;
|
|
|
|
|
pipelineCI.pRasterizationState = &rasterizationState;
|
|
|
|
|
pipelineCI.pColorBlendState = &colorBlendState;
|
|
|
|
|
pipelineCI.pMultisampleState = &multisampleState;
|
|
|
|
|
pipelineCI.pViewportState = &viewportState;
|
|
|
|
|
pipelineCI.pDepthStencilState = &depthStencilState;
|
|
|
|
|
pipelineCI.pDynamicState = &dynamicState;
|
|
|
|
|
pipelineCI.stageCount = static_cast<uint32_t>(shaderStages.size());
|
|
|
|
|
pipelineCI.pStages = shaderStages.data();
|
|
|
|
|
pipelineCI.pVertexInputState = vkglTF::Vertex::getPipelineVertexInputState({vkglTF::VertexComponent::Position, vkglTF::VertexComponent::Normal, vkglTF::VertexComponent::UV});
|
2016-05-03 22:38:57 +02:00
|
|
|
|
2020-05-29 16:08:53 +01:00
|
|
|
shaderStages[0] = loadShader(getShadersPath() + "textoverlay/mesh.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
|
|
|
|
shaderStages[1] = loadShader(getShadersPath() + "textoverlay/mesh.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
2020-07-28 20:20:38 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCI, nullptr, &pipeline));
|
2016-05-03 22:38:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prepare and initialize uniform buffer containing shader uniforms
|
|
|
|
|
void prepareUniformBuffers()
|
|
|
|
|
{
|
|
|
|
|
// Vertex shader uniform buffer block
|
2016-12-24 12:48:01 +01:00
|
|
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
2016-05-03 22:38:57 +02:00
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
2016-12-24 12:48:01 +01:00
|
|
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
|
|
|
|
&uniformBuffer,
|
|
|
|
|
sizeof(uboVS)));
|
|
|
|
|
|
|
|
|
|
// Map persistent
|
|
|
|
|
VK_CHECK_RESULT(uniformBuffer.map());
|
2016-05-03 22:38:57 +02:00
|
|
|
|
|
|
|
|
updateUniformBuffers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void updateUniformBuffers()
|
|
|
|
|
{
|
2018-03-24 11:35:02 +01:00
|
|
|
uboVS.projection = camera.matrices.perspective;
|
2020-07-28 20:20:38 +02:00
|
|
|
uboVS.modelView = camera.matrices.view * glm::scale(glm::mat4(1.0f), glm::vec3(0.1f));
|
2016-12-24 12:48:01 +01:00
|
|
|
memcpy(uniformBuffer.mapped, &uboVS, sizeof(uboVS));
|
2016-05-03 22:38:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void prepareTextOverlay()
|
|
|
|
|
{
|
2016-05-05 18:54:17 +02:00
|
|
|
// Load the text rendering shaders
|
|
|
|
|
std::vector<VkPipelineShaderStageCreateInfo> shaderStages;
|
2020-05-29 16:08:53 +01:00
|
|
|
shaderStages.push_back(loadShader(getShadersPath() + "textoverlay/text.vert.spv", VK_SHADER_STAGE_VERTEX_BIT));
|
|
|
|
|
shaderStages.push_back(loadShader(getShadersPath() + "textoverlay/text.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT));
|
2016-05-05 18:54:17 +02:00
|
|
|
|
2016-05-15 13:33:57 +02:00
|
|
|
textOverlay = new TextOverlay(
|
2016-07-23 23:44:26 +02:00
|
|
|
vulkanDevice,
|
2016-05-05 14:42:41 +02:00
|
|
|
queue,
|
2016-05-03 22:38:57 +02:00
|
|
|
frameBuffers,
|
2017-01-25 18:54:09 +01:00
|
|
|
swapChain.colorFormat,
|
2016-05-03 22:38:57 +02:00
|
|
|
depthFormat,
|
2016-05-05 13:03:56 +02:00
|
|
|
&width,
|
2016-05-05 18:54:17 +02:00
|
|
|
&height,
|
2022-07-12 18:46:39 -04:00
|
|
|
UIOverlay.scale,
|
2016-05-05 18:54:17 +02:00
|
|
|
shaderStages
|
2016-05-03 22:38:57 +02:00
|
|
|
);
|
|
|
|
|
updateTextOverlay();
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-21 21:55:31 +02:00
|
|
|
void draw()
|
|
|
|
|
{
|
|
|
|
|
VulkanExampleBase::prepareFrame();
|
|
|
|
|
|
2020-02-05 19:48:49 +01:00
|
|
|
std::vector<VkCommandBuffer> commandBuffers = {
|
|
|
|
|
drawCmdBuffers[currentBuffer]
|
|
|
|
|
};
|
|
|
|
|
if (textOverlay->visible) {
|
|
|
|
|
commandBuffers.push_back(textOverlay->cmdBuffers[currentBuffer]);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-09 13:16:35 +02:00
|
|
|
// Command buffer to be submitted to the queue
|
2020-02-05 19:48:49 +01:00
|
|
|
submitInfo.commandBufferCount = static_cast<uint32_t>(commandBuffers.size());
|
|
|
|
|
submitInfo.pCommandBuffers = commandBuffers.data();
|
2016-06-21 21:55:31 +02:00
|
|
|
|
|
|
|
|
// Submit to queue
|
|
|
|
|
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
|
|
|
|
|
|
|
|
|
VulkanExampleBase::submitFrame();
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-03 22:38:57 +02:00
|
|
|
void prepare()
|
|
|
|
|
{
|
|
|
|
|
VulkanExampleBase::prepare();
|
2017-02-04 15:00:45 +01:00
|
|
|
loadAssets();
|
2016-05-03 22:38:57 +02:00
|
|
|
prepareUniformBuffers();
|
|
|
|
|
setupDescriptorSetLayout();
|
|
|
|
|
preparePipelines();
|
|
|
|
|
setupDescriptorPool();
|
|
|
|
|
setupDescriptorSet();
|
|
|
|
|
buildCommandBuffers();
|
|
|
|
|
prepareTextOverlay();
|
|
|
|
|
prepared = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void render()
|
|
|
|
|
{
|
|
|
|
|
if (!prepared)
|
|
|
|
|
return;
|
|
|
|
|
draw();
|
2022-06-13 23:04:53 -04:00
|
|
|
if (camera.updated)
|
|
|
|
|
{
|
|
|
|
|
updateUniformBuffers();
|
|
|
|
|
}
|
2016-05-03 22:38:57 +02:00
|
|
|
if (frameCounter == 0)
|
|
|
|
|
{
|
2016-05-08 11:40:25 +02:00
|
|
|
vkDeviceWaitIdle(device);
|
2016-05-03 22:38:57 +02:00
|
|
|
updateTextOverlay();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-05 13:03:56 +02:00
|
|
|
virtual void windowResized()
|
|
|
|
|
{
|
2022-06-01 13:02:33 -04:00
|
|
|
// SRS - Recreate text overlay resources in case number of swapchain images has changed on resize
|
|
|
|
|
delete textOverlay;
|
|
|
|
|
prepareTextOverlay();
|
2016-05-05 13:03:56 +02:00
|
|
|
}
|
2016-05-07 12:37:12 +02:00
|
|
|
|
2022-06-13 23:04:53 -04:00
|
|
|
virtual void viewChanged()
|
|
|
|
|
{
|
|
|
|
|
updateUniformBuffers();
|
|
|
|
|
updateTextOverlay();
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-04 16:37:43 +01:00
|
|
|
#if !defined(__ANDROID__)
|
2016-05-07 12:37:12 +02:00
|
|
|
virtual void keyPressed(uint32_t keyCode)
|
|
|
|
|
{
|
|
|
|
|
switch (keyCode)
|
|
|
|
|
{
|
2016-08-11 13:15:49 +02:00
|
|
|
case KEY_KPADD:
|
|
|
|
|
case KEY_SPACE:
|
2016-05-07 12:37:12 +02:00
|
|
|
textOverlay->visible = !textOverlay->visible;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-04 16:37:43 +01:00
|
|
|
#endif
|
2016-05-03 22:38:57 +02:00
|
|
|
};
|
|
|
|
|
|
2022-06-01 13:02:33 -04:00
|
|
|
VULKAN_EXAMPLE_MAIN()
|