Code cleanup, simplified the sample
This commit is contained in:
parent
dfcc55b6d5
commit
5b8f09302e
2 changed files with 141 additions and 370 deletions
|
|
@ -1,6 +1,8 @@
|
||||||
/*
|
/*
|
||||||
* Vulkan Example - Physical based rendering with image based lighting
|
* Vulkan Example - Physical based rendering with image based lighting
|
||||||
*
|
*
|
||||||
|
* This sample adds imaged based lighting from an environment map to the PBR equation
|
||||||
|
*
|
||||||
* Copyright (C) 2016-2023 by Sascha Willems - www.saschawillems.de
|
* Copyright (C) 2016-2023 by Sascha Willems - www.saschawillems.de
|
||||||
*
|
*
|
||||||
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
|
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
|
||||||
|
|
@ -11,8 +13,6 @@
|
||||||
#include "vulkanexamplebase.h"
|
#include "vulkanexamplebase.h"
|
||||||
#include "VulkanglTFModel.h"
|
#include "VulkanglTFModel.h"
|
||||||
|
|
||||||
#define GRID_DIM 7
|
|
||||||
|
|
||||||
struct Material {
|
struct Material {
|
||||||
// Parameter block used as push constant block
|
// Parameter block used as push constant block
|
||||||
struct PushBlock {
|
struct PushBlock {
|
||||||
|
|
@ -69,17 +69,17 @@ public:
|
||||||
} uboParams;
|
} uboParams;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
VkPipeline skybox;
|
VkPipeline skybox{ VK_NULL_HANDLE };
|
||||||
VkPipeline pbr;
|
VkPipeline pbr{ VK_NULL_HANDLE };
|
||||||
} pipelines;
|
} pipelines;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
VkDescriptorSet object;
|
VkDescriptorSet object{ VK_NULL_HANDLE };
|
||||||
VkDescriptorSet skybox;
|
VkDescriptorSet skybox{ VK_NULL_HANDLE };
|
||||||
} descriptorSets;
|
} descriptorSets;
|
||||||
|
|
||||||
VkPipelineLayout pipelineLayout;
|
VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE };
|
||||||
VkDescriptorSetLayout descriptorSetLayout;
|
VkDescriptorSetLayout descriptorSetLayout{ VK_NULL_HANDLE };
|
||||||
|
|
||||||
// Default materials to select from
|
// Default materials to select from
|
||||||
std::vector<Material> materials;
|
std::vector<Material> materials;
|
||||||
|
|
@ -125,17 +125,19 @@ public:
|
||||||
|
|
||||||
~VulkanExample()
|
~VulkanExample()
|
||||||
{
|
{
|
||||||
vkDestroyPipeline(device, pipelines.skybox, nullptr);
|
if (device) {
|
||||||
vkDestroyPipeline(device, pipelines.pbr, nullptr);
|
vkDestroyPipeline(device, pipelines.skybox, nullptr);
|
||||||
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
vkDestroyPipeline(device, pipelines.pbr, nullptr);
|
||||||
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
|
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
||||||
uniformBuffers.object.destroy();
|
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
|
||||||
uniformBuffers.skybox.destroy();
|
uniformBuffers.object.destroy();
|
||||||
uniformBuffers.params.destroy();
|
uniformBuffers.skybox.destroy();
|
||||||
textures.environmentCube.destroy();
|
uniformBuffers.params.destroy();
|
||||||
textures.irradianceCube.destroy();
|
textures.environmentCube.destroy();
|
||||||
textures.prefilteredCube.destroy();
|
textures.irradianceCube.destroy();
|
||||||
textures.lutBrdf.destroy();
|
textures.prefilteredCube.destroy();
|
||||||
|
textures.lutBrdf.destroy();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void getEnabledFeatures()
|
virtual void getEnabledFeatures()
|
||||||
|
|
@ -189,11 +191,9 @@ public:
|
||||||
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets.object, 0, NULL);
|
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets.object, 0, NULL);
|
||||||
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.pbr);
|
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.pbr);
|
||||||
|
|
||||||
|
// Render a line of objects with the selected material and vary roughness/metallic material parameters
|
||||||
Material mat = materials[materialIndex];
|
Material mat = materials[materialIndex];
|
||||||
|
const uint32_t objcount = 10;
|
||||||
#define SINGLE_ROW 1
|
|
||||||
#ifdef SINGLE_ROW
|
|
||||||
uint32_t objcount = 10;
|
|
||||||
for (uint32_t x = 0; x < objcount; x++) {
|
for (uint32_t x = 0; x < objcount; x++) {
|
||||||
glm::vec3 pos = glm::vec3(float(x - (objcount / 2.0f)) * 2.15f, 0.0f, 0.0f);
|
glm::vec3 pos = glm::vec3(float(x - (objcount / 2.0f)) * 2.15f, 0.0f, 0.0f);
|
||||||
mat.params.roughness = 1.0f-glm::clamp((float)x / (float)objcount, 0.005f, 1.0f);
|
mat.params.roughness = 1.0f-glm::clamp((float)x / (float)objcount, 0.005f, 1.0f);
|
||||||
|
|
@ -203,18 +203,6 @@ public:
|
||||||
models.objects[models.objectIndex].draw(drawCmdBuffers[i]);
|
models.objects[models.objectIndex].draw(drawCmdBuffers[i]);
|
||||||
|
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
for (uint32_t y = 0; y < GRID_DIM; y++) {
|
|
||||||
mat.params.metallic = (float)y / (float)(GRID_DIM);
|
|
||||||
for (uint32_t x = 0; x < GRID_DIM; x++) {
|
|
||||||
glm::vec3 pos = glm::vec3(float(x - (GRID_DIM / 2.0f)) * 2.5f, 0.0f, float(y - (GRID_DIM / 2.0f)) * 2.5f);
|
|
||||||
vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(glm::vec3), &pos);
|
|
||||||
mat.params.roughness = glm::clamp((float)x / (float)(GRID_DIM), 0.05f, 1.0f);
|
|
||||||
vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, sizeof(glm::vec3), sizeof(Material::PushBlock), &mat);
|
|
||||||
models.objects[models.objectIndex].draw(drawCmdBuffers[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
drawUI(drawCmdBuffers[i]);
|
drawUI(drawCmdBuffers[i]);
|
||||||
|
|
||||||
vkCmdEndRenderPass(drawCmdBuffers[i]);
|
vkCmdEndRenderPass(drawCmdBuffers[i]);
|
||||||
|
|
@ -1374,17 +1362,6 @@ public:
|
||||||
memcpy(uniformBuffers.params.mapped, &uboParams, sizeof(uboParams));
|
memcpy(uniformBuffers.params.mapped, &uboParams, sizeof(uboParams));
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw()
|
|
||||||
{
|
|
||||||
VulkanExampleBase::prepareFrame();
|
|
||||||
|
|
||||||
submitInfo.commandBufferCount = 1;
|
|
||||||
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
|
|
||||||
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
|
||||||
|
|
||||||
VulkanExampleBase::submitFrame();
|
|
||||||
}
|
|
||||||
|
|
||||||
void prepare()
|
void prepare()
|
||||||
{
|
{
|
||||||
VulkanExampleBase::prepare();
|
VulkanExampleBase::prepare();
|
||||||
|
|
@ -1403,12 +1380,17 @@ public:
|
||||||
{
|
{
|
||||||
if (!prepared)
|
if (!prepared)
|
||||||
return;
|
return;
|
||||||
|
updateUniformBuffers();
|
||||||
draw();
|
draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void viewChanged()
|
void draw()
|
||||||
{
|
{
|
||||||
updateUniformBuffers();
|
VulkanExampleBase::prepareFrame();
|
||||||
|
submitInfo.commandBufferCount = 1;
|
||||||
|
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
|
||||||
|
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
||||||
|
VulkanExampleBase::submitFrame();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay)
|
virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay)
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,10 @@
|
||||||
/*
|
/*
|
||||||
* Vulkan Example - Text overlay rendering on-top of an existing scene using a separate render pass
|
* 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 sample renders a basic text overlay on top of a 3D scene that can be used e.g. for debug purposes
|
||||||
|
* For a more complete GUI sample see the ImGui sample
|
||||||
|
*
|
||||||
|
* Copyright (C) 2016-2023 by Sascha Willems - www.saschawillems.de
|
||||||
*
|
*
|
||||||
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
|
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
|
||||||
*/
|
*/
|
||||||
|
|
@ -17,56 +20,51 @@
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Mostly self-contained text overlay class
|
Mostly self-contained text overlay class
|
||||||
|
This class contains all Vulkan resources for drawing the text overlay
|
||||||
|
It can be plugged into an existing renderpass/command buffer
|
||||||
*/
|
*/
|
||||||
class TextOverlay
|
class TextOverlay
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
vks::VulkanDevice *vulkanDevice;
|
// Created by this class
|
||||||
|
// Font image
|
||||||
VkQueue queue;
|
|
||||||
VkFormat colorFormat;
|
|
||||||
VkFormat depthFormat;
|
|
||||||
|
|
||||||
uint32_t *frameBufferWidth;
|
|
||||||
uint32_t *frameBufferHeight;
|
|
||||||
float scale;
|
|
||||||
|
|
||||||
VkSampler sampler;
|
VkSampler sampler;
|
||||||
VkImage image;
|
VkImage image;
|
||||||
VkImageView view;
|
VkImageView view;
|
||||||
|
VkDeviceMemory imageMemory;
|
||||||
|
// Character vertex buffer
|
||||||
VkBuffer buffer;
|
VkBuffer buffer;
|
||||||
VkDeviceMemory memory;
|
VkDeviceMemory memory;
|
||||||
VkDeviceMemory imageMemory;
|
|
||||||
VkDescriptorPool descriptorPool;
|
VkDescriptorPool descriptorPool;
|
||||||
VkDescriptorSetLayout descriptorSetLayout;
|
VkDescriptorSetLayout descriptorSetLayout;
|
||||||
VkDescriptorSet descriptorSet;
|
VkDescriptorSet descriptorSet;
|
||||||
VkPipelineLayout pipelineLayout;
|
VkPipelineLayout pipelineLayout;
|
||||||
VkPipelineCache pipelineCache;
|
VkPipelineCache pipelineCache;
|
||||||
VkPipeline pipeline;
|
VkPipeline pipeline;
|
||||||
|
|
||||||
|
// Passed from the sample
|
||||||
VkRenderPass renderPass;
|
VkRenderPass renderPass;
|
||||||
VkCommandPool commandPool;
|
VkQueue queue;
|
||||||
std::vector<VkFramebuffer*> frameBuffers;
|
vks::VulkanDevice* vulkanDevice;
|
||||||
|
uint32_t* frameBufferWidth;
|
||||||
|
uint32_t* frameBufferHeight;
|
||||||
std::vector<VkPipelineShaderStageCreateInfo> shaderStages;
|
std::vector<VkPipelineShaderStageCreateInfo> shaderStages;
|
||||||
|
float scale;
|
||||||
|
|
||||||
// Pointer to mapped vertex buffer
|
// Pointer to mapped vertex buffer
|
||||||
glm::vec4 *mapped = nullptr;
|
glm::vec4 *mapped = nullptr;
|
||||||
|
|
||||||
stb_fontchar stbFontData[STB_FONT_consolas_24_latin1_NUM_CHARS];
|
stb_fontchar stbFontData[STB_FONT_consolas_24_latin1_NUM_CHARS];
|
||||||
uint32_t numLetters;
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
enum TextAlign { alignLeft, alignCenter, alignRight };
|
enum TextAlign { alignLeft, alignCenter, alignRight };
|
||||||
|
|
||||||
|
uint32_t numLetters;
|
||||||
bool visible = true;
|
bool visible = true;
|
||||||
|
|
||||||
std::vector<VkCommandBuffer> cmdBuffers;
|
|
||||||
|
|
||||||
TextOverlay(
|
TextOverlay(
|
||||||
vks::VulkanDevice *vulkanDevice,
|
vks::VulkanDevice *vulkanDevice,
|
||||||
VkQueue queue,
|
VkQueue queue,
|
||||||
std::vector<VkFramebuffer> &framebuffers,
|
VkRenderPass renderPass,
|
||||||
VkFormat colorformat,
|
|
||||||
VkFormat depthformat,
|
|
||||||
uint32_t *framebufferwidth,
|
uint32_t *framebufferwidth,
|
||||||
uint32_t *framebufferheight,
|
uint32_t *framebufferheight,
|
||||||
float scale,
|
float scale,
|
||||||
|
|
@ -74,24 +72,13 @@ public:
|
||||||
{
|
{
|
||||||
this->vulkanDevice = vulkanDevice;
|
this->vulkanDevice = vulkanDevice;
|
||||||
this->queue = queue;
|
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->shaderStages = shaderstages;
|
||||||
|
|
||||||
this->frameBufferWidth = framebufferwidth;
|
this->frameBufferWidth = framebufferwidth;
|
||||||
this->frameBufferHeight = framebufferheight;
|
this->frameBufferHeight = framebufferheight;
|
||||||
this->scale = scale;
|
this->scale = scale;
|
||||||
|
this->renderPass = renderPass;
|
||||||
|
|
||||||
cmdBuffers.resize(framebuffers.size());
|
|
||||||
prepareResources();
|
prepareResources();
|
||||||
prepareRenderPass();
|
|
||||||
preparePipeline();
|
preparePipeline();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -109,8 +96,6 @@ public:
|
||||||
vkDestroyPipelineLayout(vulkanDevice->logicalDevice, pipelineLayout, nullptr);
|
vkDestroyPipelineLayout(vulkanDevice->logicalDevice, pipelineLayout, nullptr);
|
||||||
vkDestroyPipelineCache(vulkanDevice->logicalDevice, pipelineCache, nullptr);
|
vkDestroyPipelineCache(vulkanDevice->logicalDevice, pipelineCache, nullptr);
|
||||||
vkDestroyPipeline(vulkanDevice->logicalDevice, pipeline, 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
|
// Prepare all vulkan resources required to render the font
|
||||||
|
|
@ -123,23 +108,6 @@ public:
|
||||||
static unsigned char font24pixels[fontHeight][fontWidth];
|
static unsigned char font24pixels[fontHeight][fontWidth];
|
||||||
stb_font_consolas_24_latin1(stbFontData, font24pixels, fontHeight);
|
stb_font_consolas_24_latin1(stbFontData, font24pixels, fontHeight);
|
||||||
|
|
||||||
// Command buffer
|
|
||||||
|
|
||||||
// Pool
|
|
||||||
VkCommandPoolCreateInfo cmdPoolInfo = {};
|
|
||||||
cmdPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
|
|
||||||
cmdPoolInfo.queueFamilyIndex = vulkanDevice->queueFamilyIndices.graphics;
|
|
||||||
cmdPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
|
|
||||||
VK_CHECK_RESULT(vkCreateCommandPool(vulkanDevice->logicalDevice, &cmdPoolInfo, nullptr, &commandPool));
|
|
||||||
|
|
||||||
VkCommandBufferAllocateInfo cmdBufAllocateInfo =
|
|
||||||
vks::initializers::commandBufferAllocateInfo(
|
|
||||||
commandPool,
|
|
||||||
VK_COMMAND_BUFFER_LEVEL_PRIMARY,
|
|
||||||
(uint32_t)cmdBuffers.size());
|
|
||||||
|
|
||||||
VK_CHECK_RESULT(vkAllocateCommandBuffers(vulkanDevice->logicalDevice, &cmdBufAllocateInfo, cmdBuffers.data()));
|
|
||||||
|
|
||||||
// Vertex buffer
|
// Vertex buffer
|
||||||
VkDeviceSize bufferSize = TEXTOVERLAY_MAX_CHAR_COUNT * sizeof(glm::vec4);
|
VkDeviceSize bufferSize = TEXTOVERLAY_MAX_CHAR_COUNT * sizeof(glm::vec4);
|
||||||
|
|
||||||
|
|
@ -212,12 +180,7 @@ public:
|
||||||
|
|
||||||
// Copy to image
|
// Copy to image
|
||||||
|
|
||||||
VkCommandBuffer copyCmd;
|
VkCommandBuffer copyCmd = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
|
||||||
cmdBufAllocateInfo.commandBufferCount = 1;
|
|
||||||
VK_CHECK_RESULT(vkAllocateCommandBuffers(vulkanDevice->logicalDevice, &cmdBufAllocateInfo, ©Cmd));
|
|
||||||
|
|
||||||
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
|
|
||||||
VK_CHECK_RESULT(vkBeginCommandBuffer(copyCmd, &cmdBufInfo));
|
|
||||||
|
|
||||||
// Prepare for transfer
|
// Prepare for transfer
|
||||||
vks::tools::setImageLayout(
|
vks::tools::setImageLayout(
|
||||||
|
|
@ -252,16 +215,8 @@ public:
|
||||||
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
||||||
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
||||||
|
|
||||||
VK_CHECK_RESULT(vkEndCommandBuffer(copyCmd));
|
vulkanDevice->flushCommandBuffer(copyCmd, queue);
|
||||||
|
|
||||||
VkSubmitInfo submitInfo = vks::initializers::submitInfo();
|
|
||||||
submitInfo.commandBufferCount = 1;
|
|
||||||
submitInfo.pCommandBuffers = ©Cmd;
|
|
||||||
|
|
||||||
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
|
||||||
VK_CHECK_RESULT(vkQueueWaitIdle(queue));
|
|
||||||
|
|
||||||
vkFreeCommandBuffers(vulkanDevice->logicalDevice, commandPool, 1, ©Cmd);
|
|
||||||
vkFreeMemory(vulkanDevice->logicalDevice, stagingBuffer.memory, nullptr);
|
vkFreeMemory(vulkanDevice->logicalDevice, stagingBuffer.memory, nullptr);
|
||||||
vkDestroyBuffer(vulkanDevice->logicalDevice, stagingBuffer.buffer, nullptr);
|
vkDestroyBuffer(vulkanDevice->logicalDevice, stagingBuffer.buffer, nullptr);
|
||||||
|
|
||||||
|
|
@ -304,48 +259,33 @@ public:
|
||||||
// Descriptor set layout
|
// Descriptor set layout
|
||||||
std::array<VkDescriptorSetLayoutBinding, 1> setLayoutBindings;
|
std::array<VkDescriptorSetLayoutBinding, 1> setLayoutBindings;
|
||||||
setLayoutBindings[0] = vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 0);
|
setLayoutBindings[0] = vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 0);
|
||||||
|
VkDescriptorSetLayoutCreateInfo descriptorSetLayoutInfo = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings.data(), static_cast<uint32_t>(setLayoutBindings.size()));
|
||||||
VkDescriptorSetLayoutCreateInfo descriptorSetLayoutInfo =
|
|
||||||
vks::initializers::descriptorSetLayoutCreateInfo(
|
|
||||||
setLayoutBindings.data(),
|
|
||||||
static_cast<uint32_t>(setLayoutBindings.size()));
|
|
||||||
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(vulkanDevice->logicalDevice, &descriptorSetLayoutInfo, nullptr, &descriptorSetLayout));
|
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(vulkanDevice->logicalDevice, &descriptorSetLayoutInfo, nullptr, &descriptorSetLayout));
|
||||||
|
|
||||||
// Pipeline layout
|
|
||||||
VkPipelineLayoutCreateInfo pipelineLayoutInfo =
|
|
||||||
vks::initializers::pipelineLayoutCreateInfo(
|
|
||||||
&descriptorSetLayout,
|
|
||||||
1);
|
|
||||||
VK_CHECK_RESULT(vkCreatePipelineLayout(vulkanDevice->logicalDevice, &pipelineLayoutInfo, nullptr, &pipelineLayout));
|
|
||||||
|
|
||||||
// Descriptor set
|
// Descriptor set
|
||||||
VkDescriptorSetAllocateInfo descriptorSetAllocInfo =
|
VkDescriptorSetAllocateInfo descriptorSetAllocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayout, 1);
|
||||||
vks::initializers::descriptorSetAllocateInfo(
|
|
||||||
descriptorPool,
|
|
||||||
&descriptorSetLayout,
|
|
||||||
1);
|
|
||||||
|
|
||||||
VK_CHECK_RESULT(vkAllocateDescriptorSets(vulkanDevice->logicalDevice, &descriptorSetAllocInfo, &descriptorSet));
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(vulkanDevice->logicalDevice, &descriptorSetAllocInfo, &descriptorSet));
|
||||||
|
|
||||||
VkDescriptorImageInfo texDescriptor =
|
// Descriptor for the font image
|
||||||
vks::initializers::descriptorImageInfo(
|
VkDescriptorImageInfo texDescriptor = vks::initializers::descriptorImageInfo(sampler, view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
||||||
sampler,
|
|
||||||
view,
|
|
||||||
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
|
||||||
|
|
||||||
std::array<VkWriteDescriptorSet, 1> writeDescriptorSets;
|
std::array<VkWriteDescriptorSet, 1> writeDescriptorSets;
|
||||||
writeDescriptorSets[0] = vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 0, &texDescriptor);
|
writeDescriptorSets[0] = vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 0, &texDescriptor);
|
||||||
vkUpdateDescriptorSets(vulkanDevice->logicalDevice, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
|
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;
|
|
||||||
VK_CHECK_RESULT(vkCreatePipelineCache(vulkanDevice->logicalDevice, &pipelineCacheCreateInfo, nullptr, &pipelineCache));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare a separate pipeline for the font rendering decoupled from the main application
|
// Prepare a separate pipeline for the font rendering decoupled from the main application
|
||||||
void preparePipeline()
|
void preparePipeline()
|
||||||
{
|
{
|
||||||
|
// Pipeline cache
|
||||||
|
VkPipelineCacheCreateInfo pipelineCacheCreateInfo = {};
|
||||||
|
pipelineCacheCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO;
|
||||||
|
VK_CHECK_RESULT(vkCreatePipelineCache(vulkanDevice->logicalDevice, &pipelineCacheCreateInfo, nullptr, &pipelineCache));
|
||||||
|
|
||||||
|
// Layout
|
||||||
|
VkPipelineLayoutCreateInfo pipelineLayoutInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayout, 1);
|
||||||
|
VK_CHECK_RESULT(vkCreatePipelineLayout(vulkanDevice->logicalDevice, &pipelineLayoutInfo, nullptr, &pipelineLayout));
|
||||||
|
|
||||||
// Enable blending, using alpha from red channel of the font texture (see text.frag)
|
// Enable blending, using alpha from red channel of the font texture (see text.frag)
|
||||||
VkPipelineColorBlendAttachmentState blendAttachmentState{};
|
VkPipelineColorBlendAttachmentState blendAttachmentState{};
|
||||||
blendAttachmentState.blendEnable = VK_TRUE;
|
blendAttachmentState.blendEnable = VK_TRUE;
|
||||||
|
|
@ -360,7 +300,7 @@ public:
|
||||||
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP, 0, VK_FALSE);
|
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);
|
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);
|
VkPipelineColorBlendStateCreateInfo colorBlendState = vks::initializers::pipelineColorBlendStateCreateInfo(1, &blendAttachmentState);
|
||||||
VkPipelineDepthStencilStateCreateInfo depthStencilState = vks::initializers::pipelineDepthStencilStateCreateInfo(VK_TRUE, VK_TRUE, VK_COMPARE_OP_LESS_OR_EQUAL);
|
VkPipelineDepthStencilStateCreateInfo depthStencilState = vks::initializers::pipelineDepthStencilStateCreateInfo(VK_FALSE, VK_FALSE, VK_COMPARE_OP_LESS_OR_EQUAL);
|
||||||
VkPipelineViewportStateCreateInfo viewportState = vks::initializers::pipelineViewportStateCreateInfo(1, 1, 0);
|
VkPipelineViewportStateCreateInfo viewportState = vks::initializers::pipelineViewportStateCreateInfo(1, 1, 0);
|
||||||
VkPipelineMultisampleStateCreateInfo multisampleState = vks::initializers::pipelineMultisampleStateCreateInfo(VK_SAMPLE_COUNT_1_BIT, 0);
|
VkPipelineMultisampleStateCreateInfo multisampleState = vks::initializers::pipelineMultisampleStateCreateInfo(VK_SAMPLE_COUNT_1_BIT, 0);
|
||||||
std::vector<VkDynamicState> dynamicStateEnables = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
|
std::vector<VkDynamicState> dynamicStateEnables = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
|
||||||
|
|
@ -396,86 +336,6 @@ public:
|
||||||
VK_CHECK_RESULT(vkCreateGraphicsPipelines(vulkanDevice->logicalDevice, pipelineCache, 1, &pipelineCreateInfo, nullptr, &pipeline));
|
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_PRESENT_SRC_KHR;
|
|
||||||
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 commands 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;
|
|
||||||
|
|
||||||
VK_CHECK_RESULT(vkCreateRenderPass(vulkanDevice->logicalDevice, &renderPassInfo, nullptr, &renderPass));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Map buffer
|
// Map buffer
|
||||||
void beginTextUpdate()
|
void beginTextUpdate()
|
||||||
{
|
{
|
||||||
|
|
@ -484,7 +344,6 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add text to the current buffer
|
// Add text to the current buffer
|
||||||
// todo : drop shadow? color attribute?
|
|
||||||
void addText(std::string text, float x, float y, TextAlign align)
|
void addText(std::string text, float x, float y, TextAlign align)
|
||||||
{
|
{
|
||||||
const uint32_t firstChar = STB_FONT_consolas_24_latin1_FIRST_CHAR;
|
const uint32_t firstChar = STB_FONT_consolas_24_latin1_FIRST_CHAR;
|
||||||
|
|
@ -559,53 +418,22 @@ public:
|
||||||
{
|
{
|
||||||
vkUnmapMemory(vulkanDevice->logicalDevice, memory);
|
vkUnmapMemory(vulkanDevice->logicalDevice, memory);
|
||||||
mapped = nullptr;
|
mapped = nullptr;
|
||||||
updateCommandBuffers();
|
//updateCommandBuffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Needs to be called by the application
|
// Issue the draw commands for the characters of the overlay
|
||||||
void updateCommandBuffers()
|
void draw(VkCommandBuffer cmdBuffer)
|
||||||
{
|
{
|
||||||
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
|
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
|
||||||
|
vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL);
|
||||||
|
|
||||||
VkClearValue clearValues[2];
|
VkDeviceSize offsets = 0;
|
||||||
clearValues[1].color = { { 0.0f, 0.0f, 0.0f, 0.0f } };
|
vkCmdBindVertexBuffers(cmdBuffer, 0, 1, &buffer, &offsets);
|
||||||
|
vkCmdBindVertexBuffers(cmdBuffer, 1, 1, &buffer, &offsets);
|
||||||
VkRenderPassBeginInfo renderPassBeginInfo = vks::initializers::renderPassBeginInfo();
|
// One draw command for every character. This is okay for a debug overlay, but not optimal
|
||||||
renderPassBeginInfo.renderPass = renderPass;
|
// In a real-world application one would try to batch draw commands
|
||||||
renderPassBeginInfo.renderArea.extent.width = *frameBufferWidth;
|
for (uint32_t j = 0; j < numLetters; j++) {
|
||||||
renderPassBeginInfo.renderArea.extent.height = *frameBufferHeight;
|
vkCmdDraw(cmdBuffer, 4, 1, j * 4, 0);
|
||||||
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 = vks::initializers::viewport((float)*frameBufferWidth, (float)*frameBufferHeight, 0.0f, 1.0f);
|
|
||||||
vkCmdSetViewport(cmdBuffers[i], 0, 1, &viewport);
|
|
||||||
|
|
||||||
VkRect2D scissor = vks::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]));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -616,23 +444,21 @@ public:
|
||||||
class VulkanExample : public VulkanExampleBase
|
class VulkanExample : public VulkanExampleBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TextOverlay *textOverlay = nullptr;
|
TextOverlay* textOverlay{ nullptr };
|
||||||
|
|
||||||
vkglTF::Model model;
|
vkglTF::Model model;
|
||||||
|
|
||||||
vks::Buffer uniformBuffer;
|
struct UniformData {
|
||||||
|
|
||||||
struct UBOVS {
|
|
||||||
glm::mat4 projection;
|
glm::mat4 projection;
|
||||||
glm::mat4 modelView;
|
glm::mat4 modelView;
|
||||||
glm::vec4 lightPos = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
|
glm::vec4 lightPos = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
|
||||||
} uboVS;
|
} uniformData;
|
||||||
|
vks::Buffer uniformBuffer;
|
||||||
|
|
||||||
VkPipelineLayout pipelineLayout;
|
VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE };
|
||||||
VkPipeline pipeline;
|
VkPipeline pipeline{ VK_NULL_HANDLE };
|
||||||
|
VkDescriptorSetLayout descriptorSetLayout{ VK_NULL_HANDLE };
|
||||||
VkDescriptorSetLayout descriptorSetLayout;
|
VkDescriptorSet descriptorSet{ VK_NULL_HANDLE };
|
||||||
VkDescriptorSet descriptorSet;
|
|
||||||
|
|
||||||
VulkanExample() : VulkanExampleBase()
|
VulkanExample() : VulkanExampleBase()
|
||||||
{
|
{
|
||||||
|
|
@ -646,11 +472,13 @@ public:
|
||||||
|
|
||||||
~VulkanExample()
|
~VulkanExample()
|
||||||
{
|
{
|
||||||
vkDestroyPipeline(device, pipeline, nullptr);
|
if (device) {
|
||||||
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
vkDestroyPipeline(device, pipeline, nullptr);
|
||||||
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
|
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
|
||||||
uniformBuffer.destroy();
|
vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
|
||||||
delete(textOverlay);
|
uniformBuffer.destroy();
|
||||||
|
delete(textOverlay);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void buildCommandBuffers()
|
void buildCommandBuffers()
|
||||||
|
|
@ -688,6 +516,8 @@ public:
|
||||||
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, nullptr);
|
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, nullptr);
|
||||||
model.draw(drawCmdBuffers[i]);
|
model.draw(drawCmdBuffers[i]);
|
||||||
|
|
||||||
|
textOverlay->draw(drawCmdBuffers[i]);
|
||||||
|
|
||||||
vkCmdEndRenderPass(drawCmdBuffers[i]);
|
vkCmdEndRenderPass(drawCmdBuffers[i]);
|
||||||
|
|
||||||
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
|
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
|
||||||
|
|
@ -699,6 +529,8 @@ public:
|
||||||
// Update the text buffer displayed by the text overlay
|
// Update the text buffer displayed by the text overlay
|
||||||
void updateTextOverlay(void)
|
void updateTextOverlay(void)
|
||||||
{
|
{
|
||||||
|
uint32_t lastNumLetters = textOverlay->numLetters;
|
||||||
|
|
||||||
textOverlay->beginTextUpdate();
|
textOverlay->beginTextUpdate();
|
||||||
|
|
||||||
textOverlay->addText(title, 5.0f * UIOverlay.scale, 5.0f * UIOverlay.scale, TextOverlay::alignLeft);
|
textOverlay->addText(title, 5.0f * UIOverlay.scale, 5.0f * UIOverlay.scale, TextOverlay::alignLeft);
|
||||||
|
|
@ -716,12 +548,12 @@ public:
|
||||||
{
|
{
|
||||||
ss.str("");
|
ss.str("");
|
||||||
ss << std::fixed << std::setprecision(2) << std::showpos;
|
ss << std::fixed << std::setprecision(2) << std::showpos;
|
||||||
ss << uboVS.modelView[0][i] << " " << uboVS.modelView[1][i] << " " << uboVS.modelView[2][i] << " " << uboVS.modelView[3][i];
|
ss << uniformData.modelView[0][i] << " " << uniformData.modelView[1][i] << " " << uniformData.modelView[2][i] << " " << uniformData.modelView[3][i];
|
||||||
textOverlay->addText(ss.str(), (float)width - 5.0f * UIOverlay.scale, (25.0f + (float)i * 20.0f) * UIOverlay.scale, TextOverlay::alignRight);
|
textOverlay->addText(ss.str(), (float)width - 5.0f * UIOverlay.scale, (25.0f + (float)i * 20.0f) * UIOverlay.scale, TextOverlay::alignRight);
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::vec3 projected = glm::project(glm::vec3(0.0f), uboVS.modelView, uboVS.projection, glm::vec4(0, 0, (float)width, (float)height));
|
glm::vec3 projected = glm::project(glm::vec3(0.0f), uniformData.modelView, uniformData.projection, glm::vec4(0, 0, (float)width, (float)height));
|
||||||
textOverlay->addText("A cube", projected.x, projected.y, TextOverlay::alignCenter);
|
textOverlay->addText("A torus knot", projected.x, projected.y, TextOverlay::alignCenter);
|
||||||
|
|
||||||
#if defined(__ANDROID__)
|
#if defined(__ANDROID__)
|
||||||
#else
|
#else
|
||||||
|
|
@ -729,54 +561,54 @@ public:
|
||||||
textOverlay->addText("Hold middle mouse button and drag to move", 5.0f * UIOverlay.scale, 85.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);
|
||||||
#endif
|
#endif
|
||||||
textOverlay->endTextUpdate();
|
textOverlay->endTextUpdate();
|
||||||
|
|
||||||
|
// If the no. of letters changed, the no. of draw commands also changes which requires a rebuild of the command buffers
|
||||||
|
if (lastNumLetters != textOverlay->numLetters) {
|
||||||
|
std::cout << "rebuild cb\n";
|
||||||
|
buildCommandBuffers();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void loadAssets()
|
void loadAssets()
|
||||||
{
|
{
|
||||||
const uint32_t glTFLoadingFlags = vkglTF::FileLoadingFlags::PreTransformVertices | vkglTF::FileLoadingFlags::PreMultiplyVertexColors | vkglTF::FileLoadingFlags::FlipY;
|
const uint32_t glTFLoadingFlags = vkglTF::FileLoadingFlags::PreTransformVertices | vkglTF::FileLoadingFlags::PreMultiplyVertexColors | vkglTF::FileLoadingFlags::FlipY;
|
||||||
model.loadFromFile(getAssetPath() + "models/cube.gltf", vulkanDevice, queue, glTFLoadingFlags);
|
model.loadFromFile(getAssetPath() + "models/torusknot.gltf", vulkanDevice, queue, glTFLoadingFlags);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setupDescriptorPool()
|
void setupDescriptors()
|
||||||
{
|
{
|
||||||
|
// Pool
|
||||||
std::vector<VkDescriptorPoolSize> poolSizes = {
|
std::vector<VkDescriptorPoolSize> poolSizes = {
|
||||||
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 2),
|
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 2),
|
||||||
};
|
};
|
||||||
|
VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo(poolSizes, 2);
|
||||||
VkDescriptorPoolCreateInfo descriptorPoolInfo =
|
|
||||||
vks::initializers::descriptorPoolCreateInfo(poolSizes, 2);
|
|
||||||
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
|
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
|
||||||
}
|
|
||||||
|
|
||||||
void setupDescriptorSetLayout()
|
// Layout
|
||||||
{
|
|
||||||
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
|
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
|
||||||
// Binding 0: Vertex shader uniform buffer
|
// Binding 0: Vertex shader uniform buffer
|
||||||
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0),
|
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0),
|
||||||
};
|
};
|
||||||
VkDescriptorSetLayoutCreateInfo descriptorLayout =
|
VkDescriptorSetLayoutCreateInfo descriptorLayout = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings);
|
||||||
vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings);
|
|
||||||
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout));
|
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &descriptorSetLayout));
|
||||||
|
|
||||||
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
|
// Set
|
||||||
vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayout, 1);
|
VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayout, 1);
|
||||||
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &pipelineLayout));
|
|
||||||
}
|
|
||||||
|
|
||||||
void setupDescriptorSet()
|
|
||||||
{
|
|
||||||
VkDescriptorSetAllocateInfo allocInfo =
|
|
||||||
vks::initializers::descriptorSetAllocateInfo(descriptorPool, &descriptorSetLayout, 1);
|
|
||||||
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet));
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet));
|
||||||
std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
|
std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
|
||||||
// Binding 0: Vertex shader uniform buffer
|
// Binding 0: Vertex shader uniform buffer
|
||||||
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffer.descriptor),
|
vks::initializers::writeDescriptorSet(descriptorSet, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBuffer.descriptor),
|
||||||
};
|
};
|
||||||
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, NULL);
|
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void preparePipelines()
|
void preparePipelines()
|
||||||
{
|
{
|
||||||
|
// Layout
|
||||||
|
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&descriptorSetLayout, 1);
|
||||||
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &pipelineLayout));
|
||||||
|
|
||||||
|
// Pipeline
|
||||||
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE);
|
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE);
|
||||||
VkPipelineRasterizationStateCreateInfo rasterizationState = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE, 0);
|
VkPipelineRasterizationStateCreateInfo rasterizationState = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE, 0);
|
||||||
VkPipelineColorBlendAttachmentState blendAttachmentState = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE);
|
VkPipelineColorBlendAttachmentState blendAttachmentState = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE);
|
||||||
|
|
@ -808,24 +640,15 @@ public:
|
||||||
// Prepare and initialize uniform buffer containing shader uniforms
|
// Prepare and initialize uniform buffer containing shader uniforms
|
||||||
void prepareUniformBuffers()
|
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(UniformData)));
|
||||||
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());
|
VK_CHECK_RESULT(uniformBuffer.map());
|
||||||
|
|
||||||
updateUniformBuffers();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateUniformBuffers()
|
void updateUniformBuffers()
|
||||||
{
|
{
|
||||||
uboVS.projection = camera.matrices.perspective;
|
uniformData.projection = camera.matrices.perspective;
|
||||||
uboVS.modelView = camera.matrices.view * glm::scale(glm::mat4(1.0f), glm::vec3(0.1f));
|
uniformData.modelView = camera.matrices.view;
|
||||||
memcpy(uniformBuffer.mapped, &uboVS, sizeof(uboVS));
|
memcpy(uniformBuffer.mapped, &uniformData, sizeof(UniformData));
|
||||||
}
|
}
|
||||||
|
|
||||||
void prepareTextOverlay()
|
void prepareTextOverlay()
|
||||||
|
|
@ -838,9 +661,7 @@ public:
|
||||||
textOverlay = new TextOverlay(
|
textOverlay = new TextOverlay(
|
||||||
vulkanDevice,
|
vulkanDevice,
|
||||||
queue,
|
queue,
|
||||||
frameBuffers,
|
renderPass,
|
||||||
swapChain.colorFormat,
|
|
||||||
depthFormat,
|
|
||||||
&width,
|
&width,
|
||||||
&height,
|
&height,
|
||||||
UIOverlay.scale,
|
UIOverlay.scale,
|
||||||
|
|
@ -849,68 +670,36 @@ public:
|
||||||
updateTextOverlay();
|
updateTextOverlay();
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw()
|
|
||||||
{
|
|
||||||
VulkanExampleBase::prepareFrame();
|
|
||||||
|
|
||||||
std::vector<VkCommandBuffer> commandBuffers = {
|
|
||||||
drawCmdBuffers[currentBuffer]
|
|
||||||
};
|
|
||||||
if (textOverlay->visible) {
|
|
||||||
commandBuffers.push_back(textOverlay->cmdBuffers[currentBuffer]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command buffer to be submitted to the queue
|
|
||||||
submitInfo.commandBufferCount = static_cast<uint32_t>(commandBuffers.size());
|
|
||||||
submitInfo.pCommandBuffers = commandBuffers.data();
|
|
||||||
|
|
||||||
// Submit to queue
|
|
||||||
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
|
||||||
|
|
||||||
VulkanExampleBase::submitFrame();
|
|
||||||
}
|
|
||||||
|
|
||||||
void prepare()
|
void prepare()
|
||||||
{
|
{
|
||||||
VulkanExampleBase::prepare();
|
VulkanExampleBase::prepare();
|
||||||
loadAssets();
|
loadAssets();
|
||||||
prepareUniformBuffers();
|
prepareUniformBuffers();
|
||||||
setupDescriptorSetLayout();
|
setupDescriptors();
|
||||||
preparePipelines();
|
preparePipelines();
|
||||||
setupDescriptorPool();
|
|
||||||
setupDescriptorSet();
|
|
||||||
buildCommandBuffers();
|
|
||||||
prepareTextOverlay();
|
prepareTextOverlay();
|
||||||
|
buildCommandBuffers();
|
||||||
prepared = true;
|
prepared = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void draw()
|
||||||
|
{
|
||||||
|
VulkanExampleBase::prepareFrame();
|
||||||
|
submitInfo.commandBufferCount = 1;
|
||||||
|
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
|
||||||
|
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
||||||
|
VulkanExampleBase::submitFrame();
|
||||||
|
}
|
||||||
|
|
||||||
virtual void render()
|
virtual void render()
|
||||||
{
|
{
|
||||||
if (!prepared)
|
if (!prepared)
|
||||||
return;
|
return;
|
||||||
draw();
|
updateUniformBuffers();
|
||||||
if (camera.updated)
|
if (camera.updated) {
|
||||||
{
|
|
||||||
updateUniformBuffers();
|
|
||||||
}
|
|
||||||
if (frameCounter == 0)
|
|
||||||
{
|
|
||||||
vkDeviceWaitIdle(device);
|
|
||||||
updateTextOverlay();
|
updateTextOverlay();
|
||||||
}
|
}
|
||||||
}
|
draw();
|
||||||
|
|
||||||
virtual void windowResized()
|
|
||||||
{
|
|
||||||
// SRS - Recreate text overlay resources in case number of swapchain images has changed on resize
|
|
||||||
delete textOverlay;
|
|
||||||
prepareTextOverlay();
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void viewChanged()
|
|
||||||
{
|
|
||||||
updateUniformBuffers();
|
|
||||||
updateTextOverlay();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void keyPressed(uint32_t keyCode)
|
virtual void keyPressed(uint32_t keyCode)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue