void destroy() { verticesYUp.destroy(); verticesYDown.destroy(); indicesCCW.destroy(); indicesCW.destroy(); }
~VulkanExample() { vkDestroyPipeline(device, pipeline, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); uniformBuffer.destroy(); conditionalBuffer.destroy(); }
void updateUniformBuffers() { uboVS.projection = camera.matrices.perspective; uboVS.view = camera.matrices.view; uboVS.model = glm::mat4(1.0f); VK_CHECK_RESULT(uniformBuffer.map()); uniformBuffer.copyTo(&uboVS, sizeof(uboVS)); uniformBuffer.unmap(); }
~VulkanExample() { if (uboDataDynamic.model) { alignedFree(uboDataDynamic.model); } // Clean up used Vulkan resources // Note : Inherited destructor cleans up resources stored in base class vkDestroyPipeline(device, pipeline, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); vertexBuffer.destroy(); indexBuffer.destroy(); uniformBuffers.view.destroy(); uniformBuffers.dynamic.destroy(); }
void prepareUniformBuffers() { 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))); VK_CHECK_RESULT(uniformBuffer.map()); updateUniformBuffers(); }
~VulkanExample() { // Clean up used Vulkan resources // Note : Inherited destructor cleans up resources stored in base class vkDestroyPipeline(device, pipeline, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); models.object.destroy(); uniformBuffer.destroy(); }
~VulkanExample() { vkDestroyPipeline(device, pipelines.phong, nullptr); vkDestroyPipeline(device, pipelines.textured, nullptr); vkDestroyPipeline(device, pipelines.toon, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); models.cube.destroy(); textures.colormap.destroy(); uniformBuffer.destroy(); }
// Prepare and initialize uniform buffer containing shader uniforms void prepareUniformBuffers() { // Create the vertex shader uniform buffer block VK_CHECK_RESULT(vulkanDevice->createBuffer( VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &uniformBuffer, sizeof(uboVS))); // Map persistent VK_CHECK_RESULT(uniformBuffer.map()); updateUniformBuffers(); }
/** * Default destructor, frees up all Vulkan resources acquired by the text overlay */ ~VulkanTextOverlay() { // Free up all Vulkan resources requested by the text overlay vertexBuffer.destroy(); vkDestroySampler(vulkanDevice->logicalDevice, sampler, nullptr); vkDestroyImage(vulkanDevice->logicalDevice, image, nullptr); vkDestroyImageView(vulkanDevice->logicalDevice, view, 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); vkFreeCommandBuffers(vulkanDevice->logicalDevice, commandPool, static_cast<uint32_t>(cmdBuffers.size()), cmdBuffers.data()); vkDestroyCommandPool(vulkanDevice->logicalDevice, commandPool, nullptr); vkDestroyFence(vulkanDevice->logicalDevice, fence, nullptr); }
/* [POI] Extension specific setup Gets the function pointers required for conditonal rendering Sets up a dedicated conditional buffer that is used to determine visibility at draw time */ void prepareConditionalRendering() { /* The conditional rendering functions are part of an extension so they have to be loaded manually */ vkCmdBeginConditionalRenderingEXT = (PFN_vkCmdBeginConditionalRenderingEXT)vkGetDeviceProcAddr(device, "vkCmdBeginConditionalRenderingEXT"); if (!vkCmdBeginConditionalRenderingEXT) { vks::tools::exitFatal("Could not get a valid function pointer for vkCmdBeginConditionalRenderingEXT", -1); } vkCmdEndConditionalRenderingEXT = (PFN_vkCmdEndConditionalRenderingEXT)vkGetDeviceProcAddr(device, "vkCmdEndConditionalRenderingEXT"); if (!vkCmdEndConditionalRenderingEXT) { vks::tools::exitFatal("Could not get a valid function pointer for vkCmdEndConditionalRenderingEXT", -1); } /* Create the buffer that contains the conditional rendering information A single conditional value is 32 bits and if it's zero the rendering commands are discarded This sample renders multiple rows of objects conditionally, so we setup a buffer with one value per row */ conditionalVisibility.resize(scene.linearNodes.size()); VK_CHECK_RESULT(vulkanDevice->createBuffer( VK_BUFFER_USAGE_CONDITIONAL_RENDERING_BIT_EXT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &conditionalBuffer, sizeof(int32_t) *conditionalVisibility.size(), conditionalVisibility.data())); VK_CHECK_RESULT(conditionalBuffer.map()); // By default, all parts of the glTF are visible for (auto i = 0; i < conditionalVisibility.size(); i++) { conditionalVisibility[i] = 1; } /* Copy visibility data */ updateConditionalBuffer(); }
~VulkanExample() { // Clean up used Vulkan resources // Note : Inherited destructor cleans up resources stored in base class vkDestroyPipeline(device, pipelines.MSAA, nullptr); vkDestroyPipeline(device, pipelines.MSAASampleShading, nullptr); vkDestroyPipelineLayout(device, pipelineLayout, nullptr); vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr); models.example.destroy(); // Destroy MSAA target vkDestroyImage(device, multisampleTarget.color.image, nullptr); vkDestroyImageView(device, multisampleTarget.color.view, nullptr); vkFreeMemory(device, multisampleTarget.color.memory, nullptr); vkDestroyImage(device, multisampleTarget.depth.image, nullptr); vkDestroyImageView(device, multisampleTarget.depth.view, nullptr); vkFreeMemory(device, multisampleTarget.depth.memory, nullptr); textures.colorMap.destroy(); uniformBuffer.destroy(); }
/** * Prepare all vulkan resources required to render the font * The text overlay uses separate resources for descriptors (pool, sets, layouts), pipelines and command buffers */ void prepareResources() { static unsigned char font24pixels[STB_FONT_HEIGHT][STB_FONT_WIDTH]; STB_FONT_NAME(stbFontData, font24pixels, STB_FONT_HEIGHT); // 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 VK_CHECK_RESULT(vulkanDevice->createBuffer( VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &vertexBuffer, MAX_CHAR_COUNT * sizeof(glm::vec4))); // Map persistent vertexBuffer.map(); // Font texture VkImageCreateInfo imageInfo = vks::initializers::imageCreateInfo(); imageInfo.imageType = VK_IMAGE_TYPE_2D; imageInfo.format = VK_FORMAT_R8_UNORM; imageInfo.extent.width = STB_FONT_WIDTH; imageInfo.extent.height = STB_FONT_HEIGHT; imageInfo.extent.depth = 1; imageInfo.mipLevels = 1; imageInfo.arrayLayers = 1; imageInfo.samples = VK_SAMPLE_COUNT_1_BIT; imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL; imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; imageInfo.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED; VK_CHECK_RESULT(vkCreateImage(vulkanDevice->logicalDevice, &imageInfo, nullptr, &image)); VkMemoryRequirements memReqs; VkMemoryAllocateInfo allocInfo = vks::initializers::memoryAllocateInfo(); vkGetImageMemoryRequirements(vulkanDevice->logicalDevice, image, &memReqs); allocInfo.allocationSize = memReqs.size; allocInfo.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); VK_CHECK_RESULT(vkAllocateMemory(vulkanDevice->logicalDevice, &allocInfo, nullptr, &imageMemory)); VK_CHECK_RESULT(vkBindImageMemory(vulkanDevice->logicalDevice, image, imageMemory, 0)); // Staging vks::Buffer stagingBuffer; VK_CHECK_RESULT(vulkanDevice->createBuffer( VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &stagingBuffer, allocInfo.allocationSize)); stagingBuffer.map(); memcpy(stagingBuffer.mapped, &font24pixels[0][0], STB_FONT_WIDTH * STB_FONT_HEIGHT); // Only one channel, so data size = W * H (*R8) stagingBuffer.unmap(); // Copy to image VkCommandBuffer copyCmd; 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 vks::tools::setImageLayout( copyCmd, image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_PREINITIALIZED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL); VkBufferImageCopy bufferCopyRegion = {}; bufferCopyRegion.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; bufferCopyRegion.imageSubresource.mipLevel = 0; bufferCopyRegion.imageSubresource.layerCount = 1; bufferCopyRegion.imageExtent.width = STB_FONT_WIDTH; bufferCopyRegion.imageExtent.height = STB_FONT_HEIGHT; bufferCopyRegion.imageExtent.depth = 1; vkCmdCopyBufferToImage( copyCmd, stagingBuffer.buffer, image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &bufferCopyRegion ); // Prepare for shader read vks::tools::setImageLayout( copyCmd, image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); VK_CHECK_RESULT(vkEndCommandBuffer(copyCmd)); 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)); stagingBuffer.destroy(); vkFreeCommandBuffers(vulkanDevice->logicalDevice, commandPool, 1, ©Cmd); VkImageViewCreateInfo imageViewInfo = vks::initializers::imageViewCreateInfo(); imageViewInfo.image = image; imageViewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; imageViewInfo.format = imageInfo.format; imageViewInfo.components = { VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A }; imageViewInfo.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 }; VK_CHECK_RESULT(vkCreateImageView(vulkanDevice->logicalDevice, &imageViewInfo, nullptr, &view)); // Sampler VkSamplerCreateInfo samplerInfo = vks::initializers::samplerCreateInfo(); samplerInfo.magFilter = VK_FILTER_LINEAR; samplerInfo.minFilter = VK_FILTER_LINEAR; samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR; samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT; samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT; samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT; samplerInfo.mipLodBias = 0.0f; samplerInfo.compareOp = VK_COMPARE_OP_NEVER; samplerInfo.minLod = 0.0f; samplerInfo.maxLod = 1.0f; samplerInfo.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE; samplerInfo.maxAnisotropy = 1.0f; VK_CHECK_RESULT(vkCreateSampler(vulkanDevice->logicalDevice, &samplerInfo, nullptr, &sampler)); // Descriptor // Font uses a separate descriptor pool std::array<VkDescriptorPoolSize, 1> poolSizes; poolSizes[0] = vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1); VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo( static_cast<uint32_t>(poolSizes.size()), poolSizes.data(), 1); VK_CHECK_RESULT(vkCreateDescriptorPool(vulkanDevice->logicalDevice, &descriptorPoolInfo, nullptr, &descriptorPool)); // Descriptor set layout std::array<VkDescriptorSetLayoutBinding, 1> setLayoutBindings; 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())); 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 VkDescriptorSetAllocateInfo descriptorSetAllocInfo = vks::initializers::descriptorSetAllocateInfo( descriptorPool, &descriptorSetLayout, 1); VK_CHECK_RESULT(vkAllocateDescriptorSets(vulkanDevice->logicalDevice, &descriptorSetAllocInfo, &descriptorSet)); VkDescriptorImageInfo texDescriptor = vks::initializers::descriptorImageInfo( sampler, view, VK_IMAGE_LAYOUT_GENERAL); std::array<VkWriteDescriptorSet, 1> writeDescriptorSets; 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); // Pipeline cache VkPipelineCacheCreateInfo pipelineCacheCreateInfo = {}; pipelineCacheCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO; VK_CHECK_RESULT(vkCreatePipelineCache(vulkanDevice->logicalDevice, &pipelineCacheCreateInfo, nullptr, &pipelineCache)); // Command buffer execution fence VkFenceCreateInfo fenceCreateInfo = vks::initializers::fenceCreateInfo(); VK_CHECK_RESULT(vkCreateFence(vulkanDevice->logicalDevice, &fenceCreateInfo, nullptr, &fence)); }