2016-02-16 15:07:25 +01:00
|
|
|
/*
|
|
|
|
|
* Vulkan Example - Compute shader image processing
|
2024-01-13 11:27:06 +01:00
|
|
|
*
|
|
|
|
|
* This sample uses a compute shader to apply different filters to an image
|
2016-02-16 15:07:25 +01:00
|
|
|
*
|
2023-12-22 14:30:18 +01:00
|
|
|
* Copyright (C) 2016-2023 by Sascha Willems - www.saschawillems.de
|
2016-02-16 15:07:25 +01:00
|
|
|
*
|
|
|
|
|
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "vulkanexamplebase.h"
|
|
|
|
|
|
|
|
|
|
// Vertex layout for this example
|
|
|
|
|
struct Vertex {
|
|
|
|
|
float pos[3];
|
|
|
|
|
float uv[2];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class VulkanExample : public VulkanExampleBase
|
|
|
|
|
{
|
|
|
|
|
public:
|
2024-01-13 11:27:06 +01:00
|
|
|
// Input image
|
|
|
|
|
vks::Texture2D textureColorMap;
|
|
|
|
|
// Storage image that the compute shader uses to apply the filter effect to
|
|
|
|
|
vks::Texture2D storageImage;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-08-18 22:22:03 +02:00
|
|
|
// Resources for the graphics part of the example
|
2024-01-13 11:27:06 +01:00
|
|
|
struct Graphics {
|
|
|
|
|
VkDescriptorSetLayout descriptorSetLayout{ VK_NULL_HANDLE }; // Image display shader binding layout
|
|
|
|
|
VkDescriptorSet descriptorSetPreCompute{ VK_NULL_HANDLE }; // Image display shader bindings before compute shader image manipulation
|
|
|
|
|
VkDescriptorSet descriptorSetPostCompute{ VK_NULL_HANDLE }; // Image display shader bindings after compute shader image manipulation
|
|
|
|
|
VkPipeline pipeline{ VK_NULL_HANDLE }; // Image display pipeline
|
|
|
|
|
VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE }; // Layout of the graphics pipeline
|
|
|
|
|
VkSemaphore semaphore{ VK_NULL_HANDLE }; // Execution dependency between compute & graphic submission
|
|
|
|
|
// Used to pass data to the graphics shaders
|
|
|
|
|
struct UniformData {
|
|
|
|
|
glm::mat4 projection;
|
|
|
|
|
glm::mat4 modelView;
|
|
|
|
|
} uniformData;
|
|
|
|
|
vks::Buffer uniformBuffer;
|
2016-08-18 22:22:03 +02:00
|
|
|
} graphics;
|
|
|
|
|
|
|
|
|
|
// Resources for the compute part of the example
|
|
|
|
|
struct Compute {
|
2024-01-13 11:27:06 +01:00
|
|
|
VkQueue queue{ VK_NULL_HANDLE }; // Separate queue for compute commands (queue family may differ from the one used for graphics)
|
|
|
|
|
VkCommandPool commandPool{ VK_NULL_HANDLE }; // Use a separate command pool (queue family may differ from the one used for graphics)
|
|
|
|
|
VkCommandBuffer commandBuffer{ VK_NULL_HANDLE }; // Command buffer storing the dispatch commands and barriers
|
|
|
|
|
VkSemaphore semaphore{ VK_NULL_HANDLE }; // Execution dependency between compute & graphic submission
|
|
|
|
|
VkDescriptorSetLayout descriptorSetLayout{ VK_NULL_HANDLE }; // Compute shader binding layout
|
|
|
|
|
VkDescriptorSet descriptorSet{ VK_NULL_HANDLE }; // Compute shader bindings
|
|
|
|
|
VkPipelineLayout pipelineLayout{ VK_NULL_HANDLE }; // Layout of the compute pipeline
|
|
|
|
|
std::vector<VkPipeline> pipelines{}; // Compute pipelines for image filters
|
|
|
|
|
int32_t pipelineIndex{ 0 }; // Current image filtering compute pipeline index
|
2016-08-18 22:22:03 +02:00
|
|
|
} compute;
|
|
|
|
|
|
2017-02-12 10:44:51 +01:00
|
|
|
vks::Buffer vertexBuffer;
|
|
|
|
|
vks::Buffer indexBuffer;
|
2024-01-13 11:27:06 +01:00
|
|
|
uint32_t indexCount{ 0 };
|
|
|
|
|
uint32_t vertexBufferSize{ 0 };
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2024-01-13 11:27:06 +01:00
|
|
|
std::vector<std::string> filterNames{};
|
2017-11-01 14:22:10 +01:00
|
|
|
|
2023-12-30 13:15:37 +01:00
|
|
|
VulkanExample() : VulkanExampleBase()
|
2016-02-16 15:07:25 +01:00
|
|
|
{
|
2017-11-01 14:22:10 +01:00
|
|
|
title = "Compute shader image load/store";
|
2020-04-22 20:58:24 +02:00
|
|
|
camera.type = Camera::CameraType::lookat;
|
|
|
|
|
camera.setPosition(glm::vec3(0.0f, 0.0f, -2.0f));
|
|
|
|
|
camera.setRotation(glm::vec3(0.0f));
|
|
|
|
|
camera.setPerspective(60.0f, (float)width * 0.5f / (float)height, 1.0f, 256.0f);
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~VulkanExample()
|
|
|
|
|
{
|
2024-01-13 11:27:06 +01:00
|
|
|
if (device) {
|
|
|
|
|
// Graphics
|
|
|
|
|
vkDestroyPipeline(device, graphics.pipeline, nullptr);
|
|
|
|
|
vkDestroyPipelineLayout(device, graphics.pipelineLayout, nullptr);
|
|
|
|
|
vkDestroyDescriptorSetLayout(device, graphics.descriptorSetLayout, nullptr);
|
|
|
|
|
vkDestroySemaphore(device, graphics.semaphore, nullptr);
|
|
|
|
|
graphics.uniformBuffer.destroy();
|
|
|
|
|
|
|
|
|
|
// Compute
|
|
|
|
|
for (auto& pipeline : compute.pipelines)
|
|
|
|
|
{
|
|
|
|
|
vkDestroyPipeline(device, pipeline, nullptr);
|
|
|
|
|
}
|
|
|
|
|
vkDestroyPipelineLayout(device, compute.pipelineLayout, nullptr);
|
|
|
|
|
vkDestroyDescriptorSetLayout(device, compute.descriptorSetLayout, nullptr);
|
|
|
|
|
vkDestroySemaphore(device, compute.semaphore, nullptr);
|
|
|
|
|
vkDestroyCommandPool(device, compute.commandPool, nullptr);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2024-01-13 11:27:06 +01:00
|
|
|
vertexBuffer.destroy();
|
|
|
|
|
indexBuffer.destroy();
|
2017-01-07 20:46:28 +01:00
|
|
|
|
2024-01-13 11:27:06 +01:00
|
|
|
textureColorMap.destroy();
|
|
|
|
|
storageImage.destroy();
|
|
|
|
|
}
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-13 11:27:06 +01:00
|
|
|
// Prepare a storage image that is used to store the compute shader filter
|
|
|
|
|
void prepareStorageImage()
|
2016-02-16 15:07:25 +01:00
|
|
|
{
|
2024-01-13 11:27:06 +01:00
|
|
|
const VkFormat format = VK_FORMAT_R8G8B8A8_UNORM;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2024-01-13 11:27:06 +01:00
|
|
|
VkFormatProperties formatProperties;
|
2016-02-16 15:07:25 +01:00
|
|
|
// Get device properties for the requested texture format
|
|
|
|
|
vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &formatProperties);
|
2024-01-13 11:27:06 +01:00
|
|
|
// Check if requested image format supports image storage operations required for storing pixel from the compute shader
|
2016-03-16 19:55:38 +01:00
|
|
|
assert(formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Prepare blit target texture
|
2024-01-13 11:27:06 +01:00
|
|
|
storageImage.width = textureColorMap.width;
|
|
|
|
|
storageImage.height = textureColorMap.height;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkImageCreateInfo imageCreateInfo = vks::initializers::imageCreateInfo();
|
2016-02-16 15:07:25 +01:00
|
|
|
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
|
|
|
|
|
imageCreateInfo.format = format;
|
2024-01-13 11:27:06 +01:00
|
|
|
imageCreateInfo.extent = { storageImage.width, storageImage.height, 1 };
|
2016-02-16 15:07:25 +01:00
|
|
|
imageCreateInfo.mipLevels = 1;
|
|
|
|
|
imageCreateInfo.arrayLayers = 1;
|
|
|
|
|
imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
|
|
|
imageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
2016-03-16 19:55:38 +01:00
|
|
|
// Image will be sampled in the fragment shader and used as storage target in the compute shader
|
2016-08-18 22:22:03 +02:00
|
|
|
imageCreateInfo.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT;
|
2016-02-16 15:07:25 +01:00
|
|
|
imageCreateInfo.flags = 0;
|
2020-08-29 12:19:05 +02:00
|
|
|
// If compute and graphics queue family indices differ, we create an image that can be shared between them
|
|
|
|
|
// This can result in worse performance than exclusive sharing mode, but save some synchronization to keep the sample simple
|
|
|
|
|
std::vector<uint32_t> queueFamilyIndices;
|
|
|
|
|
if (vulkanDevice->queueFamilyIndices.graphics != vulkanDevice->queueFamilyIndices.compute) {
|
|
|
|
|
queueFamilyIndices = {
|
|
|
|
|
vulkanDevice->queueFamilyIndices.graphics,
|
|
|
|
|
vulkanDevice->queueFamilyIndices.compute
|
|
|
|
|
};
|
|
|
|
|
imageCreateInfo.sharingMode = VK_SHARING_MODE_CONCURRENT;
|
|
|
|
|
imageCreateInfo.queueFamilyIndexCount = 2;
|
|
|
|
|
imageCreateInfo.pQueueFamilyIndices = queueFamilyIndices.data();
|
|
|
|
|
}
|
2024-01-13 11:27:06 +01:00
|
|
|
VK_CHECK_RESULT(vkCreateImage(device, &imageCreateInfo, nullptr, &storageImage.image));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkMemoryAllocateInfo memAllocInfo = vks::initializers::memoryAllocateInfo();
|
2016-02-16 15:07:25 +01:00
|
|
|
VkMemoryRequirements memReqs;
|
2024-01-13 11:27:06 +01:00
|
|
|
vkGetImageMemoryRequirements(device, storageImage.image, &memReqs);
|
2016-02-16 15:07:25 +01:00
|
|
|
memAllocInfo.allocationSize = memReqs.size;
|
2016-07-23 20:42:03 +02:00
|
|
|
memAllocInfo.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
2024-01-13 11:27:06 +01:00
|
|
|
VK_CHECK_RESULT(vkAllocateMemory(device, &memAllocInfo, nullptr, &storageImage.deviceMemory));
|
|
|
|
|
VK_CHECK_RESULT(vkBindImageMemory(device, storageImage.image, storageImage.deviceMemory, 0));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2024-01-13 11:27:06 +01:00
|
|
|
// Transition image to the general layout, so we can use it as a storage image in the compute shader
|
2020-04-20 22:13:51 +02:00
|
|
|
VkCommandBuffer layoutCmd = vulkanDevice->createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
|
2024-01-13 11:27:06 +01:00
|
|
|
storageImage.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
|
|
|
|
|
vks::tools::setImageLayout(layoutCmd, storageImage.image, VK_IMAGE_ASPECT_COLOR_BIT, VK_IMAGE_LAYOUT_UNDEFINED, storageImage.imageLayout);
|
2020-04-20 22:13:51 +02:00
|
|
|
vulkanDevice->flushCommandBuffer(layoutCmd, queue, true);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Create sampler
|
2017-02-12 11:12:42 +01:00
|
|
|
VkSamplerCreateInfo sampler = vks::initializers::samplerCreateInfo();
|
2016-02-16 15:07:25 +01:00
|
|
|
sampler.magFilter = VK_FILTER_LINEAR;
|
|
|
|
|
sampler.minFilter = VK_FILTER_LINEAR;
|
|
|
|
|
sampler.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
2016-05-23 21:22:00 +02:00
|
|
|
sampler.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
|
2016-02-16 15:07:25 +01:00
|
|
|
sampler.addressModeV = sampler.addressModeU;
|
|
|
|
|
sampler.addressModeW = sampler.addressModeU;
|
|
|
|
|
sampler.mipLodBias = 0.0f;
|
2017-06-17 16:07:38 +02:00
|
|
|
sampler.maxAnisotropy = 1.0f;
|
2016-02-16 15:07:25 +01:00
|
|
|
sampler.compareOp = VK_COMPARE_OP_NEVER;
|
|
|
|
|
sampler.minLod = 0.0f;
|
2024-01-13 11:27:06 +01:00
|
|
|
sampler.maxLod = 1.0f;
|
2016-02-16 15:07:25 +01:00
|
|
|
sampler.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
|
2024-01-13 11:27:06 +01:00
|
|
|
VK_CHECK_RESULT(vkCreateSampler(device, &sampler, nullptr, &storageImage.sampler));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Create image view
|
2017-02-12 11:12:42 +01:00
|
|
|
VkImageViewCreateInfo view = vks::initializers::imageViewCreateInfo();
|
2016-02-16 15:07:25 +01:00
|
|
|
view.image = VK_NULL_HANDLE;
|
|
|
|
|
view.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
|
|
|
|
view.format = format;
|
|
|
|
|
view.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
|
2024-01-13 11:27:06 +01:00
|
|
|
view.image = storageImage.image;
|
|
|
|
|
VK_CHECK_RESULT(vkCreateImageView(device, &view, nullptr, &storageImage.view));
|
2016-08-18 22:22:03 +02:00
|
|
|
|
|
|
|
|
// Initialize a descriptor for later use
|
2024-01-13 11:27:06 +01:00
|
|
|
storageImage.descriptor.imageLayout = storageImage.imageLayout;
|
|
|
|
|
storageImage.descriptor.imageView = storageImage.view;
|
|
|
|
|
storageImage.descriptor.sampler = storageImage.sampler;
|
|
|
|
|
storageImage.device = vulkanDevice;
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
2017-02-09 21:55:35 +01:00
|
|
|
void loadAssets()
|
2016-02-16 15:07:25 +01:00
|
|
|
{
|
2018-03-23 22:04:14 +01:00
|
|
|
textureColorMap.loadFromFile(getAssetPath() + "textures/vulkan_11_rgba.ktx", VK_FORMAT_R8G8B8A8_UNORM, vulkanDevice, queue, VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_STORAGE_BIT, VK_IMAGE_LAYOUT_GENERAL);
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void buildCommandBuffers()
|
|
|
|
|
{
|
2017-02-12 11:12:42 +01:00
|
|
|
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
VkClearValue clearValues[2];
|
|
|
|
|
clearValues[0].color = defaultClearColor;
|
|
|
|
|
clearValues[1].depthStencil = { 1.0f, 0 };
|
|
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkRenderPassBeginInfo renderPassBeginInfo = vks::initializers::renderPassBeginInfo();
|
2016-02-16 15:07:25 +01:00
|
|
|
renderPassBeginInfo.renderPass = renderPass;
|
|
|
|
|
renderPassBeginInfo.renderArea.offset.x = 0;
|
|
|
|
|
renderPassBeginInfo.renderArea.offset.y = 0;
|
|
|
|
|
renderPassBeginInfo.renderArea.extent.width = width;
|
|
|
|
|
renderPassBeginInfo.renderArea.extent.height = height;
|
|
|
|
|
renderPassBeginInfo.clearValueCount = 2;
|
|
|
|
|
renderPassBeginInfo.pClearValues = clearValues;
|
|
|
|
|
|
|
|
|
|
for (int32_t i = 0; i < drawCmdBuffers.size(); ++i)
|
|
|
|
|
{
|
|
|
|
|
// Set target frame buffer
|
|
|
|
|
renderPassBeginInfo.framebuffer = frameBuffers[i];
|
|
|
|
|
|
2016-05-23 21:22:00 +02:00
|
|
|
VK_CHECK_RESULT(vkBeginCommandBuffer(drawCmdBuffers[i], &cmdBufInfo));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-08-18 22:22:03 +02:00
|
|
|
// Image memory barrier to make sure that compute shader writes are finished before sampling from the texture
|
2016-02-16 15:07:25 +01:00
|
|
|
VkImageMemoryBarrier imageMemoryBarrier = {};
|
|
|
|
|
imageMemoryBarrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
2016-08-18 22:22:03 +02:00
|
|
|
// We won't be changing the layout of the image
|
2016-02-16 15:07:25 +01:00
|
|
|
imageMemoryBarrier.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
|
|
|
|
|
imageMemoryBarrier.newLayout = VK_IMAGE_LAYOUT_GENERAL;
|
2024-01-13 11:27:06 +01:00
|
|
|
imageMemoryBarrier.image = storageImage.image;
|
2016-02-16 15:07:25 +01:00
|
|
|
imageMemoryBarrier.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
|
|
|
|
|
imageMemoryBarrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
|
2016-08-18 22:22:03 +02:00
|
|
|
imageMemoryBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
2020-08-29 12:19:05 +02:00
|
|
|
imageMemoryBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
|
|
|
|
imageMemoryBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
2016-02-16 15:07:25 +01:00
|
|
|
vkCmdPipelineBarrier(
|
|
|
|
|
drawCmdBuffers[i],
|
2016-08-18 22:22:03 +02:00
|
|
|
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
|
|
|
|
|
VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
|
2016-02-16 15:07:25 +01:00
|
|
|
VK_FLAGS_NONE,
|
|
|
|
|
0, nullptr,
|
|
|
|
|
0, nullptr,
|
|
|
|
|
1, &imageMemoryBarrier);
|
|
|
|
|
vkCmdBeginRenderPass(drawCmdBuffers[i], &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
|
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkViewport viewport = vks::initializers::viewport((float)width * 0.5f, (float)height, 0.0f, 1.0f);
|
2016-02-16 15:07:25 +01: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-02-16 15:07:25 +01:00
|
|
|
vkCmdSetScissor(drawCmdBuffers[i], 0, 1, &scissor);
|
|
|
|
|
|
|
|
|
|
VkDeviceSize offsets[1] = { 0 };
|
2024-01-08 20:26:41 +01:00
|
|
|
vkCmdBindVertexBuffers(drawCmdBuffers[i], 0, 1, &vertexBuffer.buffer, offsets);
|
2017-01-07 20:46:28 +01:00
|
|
|
vkCmdBindIndexBuffer(drawCmdBuffers[i], indexBuffer.buffer, 0, VK_INDEX_TYPE_UINT32);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Left (pre compute)
|
2016-08-18 22:22:03 +02:00
|
|
|
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphics.pipelineLayout, 0, 1, &graphics.descriptorSetPreCompute, 0, NULL);
|
|
|
|
|
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphics.pipeline);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2017-01-07 20:46:28 +01:00
|
|
|
vkCmdDrawIndexed(drawCmdBuffers[i], indexCount, 1, 0, 0, 0);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Right (post compute)
|
2016-08-18 22:22:03 +02:00
|
|
|
vkCmdBindDescriptorSets(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphics.pipelineLayout, 0, 1, &graphics.descriptorSetPostCompute, 0, NULL);
|
|
|
|
|
vkCmdBindPipeline(drawCmdBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphics.pipeline);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
viewport.x = (float)width / 2.0f;
|
|
|
|
|
vkCmdSetViewport(drawCmdBuffers[i], 0, 1, &viewport);
|
2017-01-07 20:46:28 +01:00
|
|
|
vkCmdDrawIndexed(drawCmdBuffers[i], indexCount, 1, 0, 0, 0);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2018-08-30 21:08:02 +02:00
|
|
|
drawUI(drawCmdBuffers[i]);
|
|
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
vkCmdEndRenderPass(drawCmdBuffers[i]);
|
|
|
|
|
|
2016-05-23 21:22:00 +02:00
|
|
|
VK_CHECK_RESULT(vkEndCommandBuffer(drawCmdBuffers[i]));
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void buildComputeCommandBuffer()
|
|
|
|
|
{
|
2016-08-18 22:22:03 +02:00
|
|
|
// Flush the queue if we're rebuilding the command buffer after a pipeline change to ensure it's not currently in use
|
|
|
|
|
vkQueueWaitIdle(compute.queue);
|
|
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkCommandBufferBeginInfo cmdBufInfo = vks::initializers::commandBufferBeginInfo();
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-08-18 22:22:03 +02:00
|
|
|
VK_CHECK_RESULT(vkBeginCommandBuffer(compute.commandBuffer, &cmdBufInfo));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-08-18 22:22:03 +02:00
|
|
|
vkCmdBindPipeline(compute.commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, compute.pipelines[compute.pipelineIndex]);
|
|
|
|
|
vkCmdBindDescriptorSets(compute.commandBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, compute.pipelineLayout, 0, 1, &compute.descriptorSet, 0, 0);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2024-01-13 11:27:06 +01:00
|
|
|
vkCmdDispatch(compute.commandBuffer, storageImage.width / 16, storageImage.height / 16, 1);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-08-18 22:22:03 +02:00
|
|
|
vkEndCommandBuffer(compute.commandBuffer);
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-13 11:27:06 +01:00
|
|
|
// Setup vertices for a single uv-mapped quad used to display the input and output images
|
2016-02-16 15:07:25 +01:00
|
|
|
void generateQuad()
|
|
|
|
|
{
|
2017-01-07 20:46:28 +01:00
|
|
|
// Setup vertices for a single uv-mapped quad made from two triangles
|
2024-01-13 11:27:06 +01:00
|
|
|
std::vector<Vertex> vertices = {
|
2017-01-07 20:46:28 +01:00
|
|
|
{ { 1.0f, 1.0f, 0.0f }, { 1.0f, 1.0f } },
|
|
|
|
|
{ { -1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f } },
|
|
|
|
|
{ { -1.0f, -1.0f, 0.0f }, { 0.0f, 0.0f } },
|
|
|
|
|
{ { 1.0f, -1.0f, 0.0f }, { 1.0f, 0.0f } }
|
2016-02-16 15:07:25 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Setup indices
|
2017-01-07 20:46:28 +01:00
|
|
|
std::vector<uint32_t> indices = { 0,1,2, 2,3,0 };
|
|
|
|
|
indexCount = static_cast<uint32_t>(indices.size());
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2024-01-13 11:27:06 +01:00
|
|
|
// Create buffers and upload data to the GPU
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2024-01-13 11:27:06 +01:00
|
|
|
struct StagingBuffers {
|
|
|
|
|
vks::Buffer vertices;
|
|
|
|
|
vks::Buffer indices;
|
|
|
|
|
} stagingBuffers;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2024-01-13 11:27:06 +01:00
|
|
|
// Host visible source buffers (staging)
|
|
|
|
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &stagingBuffers.vertices, vertices.size() * sizeof(Vertex), vertices.data()));
|
|
|
|
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &stagingBuffers.indices, indices.size() * sizeof(uint32_t), indices.data()));
|
|
|
|
|
|
|
|
|
|
// Device local destination buffers
|
|
|
|
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &vertexBuffer, vertices.size() * sizeof(Vertex)));
|
|
|
|
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, &indexBuffer, indices.size() * sizeof(uint32_t)));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2024-01-13 11:27:06 +01:00
|
|
|
// Copy from host do device
|
|
|
|
|
vulkanDevice->copyBuffer(&stagingBuffers.vertices, &vertexBuffer, queue);
|
|
|
|
|
vulkanDevice->copyBuffer(&stagingBuffers.indices, &indexBuffer, queue);
|
|
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
|
stagingBuffers.vertices.destroy();
|
|
|
|
|
stagingBuffers.indices.destroy();
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-13 11:27:06 +01:00
|
|
|
// The descriptor pool will be shared between graphics and compute
|
2016-02-16 15:07:25 +01:00
|
|
|
void setupDescriptorPool()
|
|
|
|
|
{
|
2018-03-23 22:04:14 +01:00
|
|
|
std::vector<VkDescriptorPoolSize> poolSizes = {
|
2020-05-29 16:08:53 +01:00
|
|
|
// Graphics pipelines uniform buffers
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 2),
|
2018-03-23 22:04:14 +01:00
|
|
|
// Graphics pipelines image samplers for displaying compute output image
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 2),
|
2016-08-06 23:08:31 +02:00
|
|
|
// Compute pipelines uses a storage image for image reads and writes
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::descriptorPoolSize(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 2),
|
2016-02-16 15:07:25 +01:00
|
|
|
};
|
2018-03-23 22:04:14 +01:00
|
|
|
VkDescriptorPoolCreateInfo descriptorPoolInfo = vks::initializers::descriptorPoolCreateInfo(poolSizes, 3);
|
2016-05-23 21:22:00 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateDescriptorPool(device, &descriptorPoolInfo, nullptr, &descriptorPool));
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
2024-01-13 11:27:06 +01:00
|
|
|
// Prepare the graphics resources used to display the ray traced output of the compute shader
|
|
|
|
|
void prepareGraphics()
|
2016-02-16 15:07:25 +01:00
|
|
|
{
|
2024-01-13 11:27:06 +01:00
|
|
|
// Create a semaphore for compute & graphics sync
|
|
|
|
|
VkSemaphoreCreateInfo semaphoreCreateInfo = vks::initializers::semaphoreCreateInfo();
|
|
|
|
|
VK_CHECK_RESULT(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &graphics.semaphore));
|
|
|
|
|
|
|
|
|
|
// Signal the semaphore
|
|
|
|
|
VkSubmitInfo submitInfo = vks::initializers::submitInfo();
|
|
|
|
|
submitInfo.signalSemaphoreCount = 1;
|
|
|
|
|
submitInfo.pSignalSemaphores = &graphics.semaphore;
|
|
|
|
|
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
|
|
|
|
VK_CHECK_RESULT(vkQueueWaitIdle(queue));
|
|
|
|
|
|
|
|
|
|
// Setup descriptors
|
|
|
|
|
|
|
|
|
|
// The graphics pipeline uses two sets with two bindings
|
|
|
|
|
// One set for displaying the input image and one set for displaying the output image with the compute filter applied
|
|
|
|
|
// Binding 0: Vertex shader uniform buffer
|
|
|
|
|
// Binding 1: Sampled image (before/after compute filter is applied)
|
|
|
|
|
|
2020-05-29 16:08:53 +01:00
|
|
|
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
|
2018-03-23 22:04:14 +01:00
|
|
|
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0),
|
|
|
|
|
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, 1)
|
2016-02-16 15:07:25 +01:00
|
|
|
};
|
2018-03-23 22:04:14 +01:00
|
|
|
VkDescriptorSetLayoutCreateInfo descriptorLayout = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings);
|
2016-08-18 22:22:03 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &graphics.descriptorSetLayout));
|
2020-05-29 16:08:53 +01:00
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
VkDescriptorSetAllocateInfo allocInfo =
|
2018-03-23 22:04:14 +01:00
|
|
|
vks::initializers::descriptorSetAllocateInfo(descriptorPool, &graphics.descriptorSetLayout, 1);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2018-03-23 22:04:14 +01:00
|
|
|
// Input image (before compute post processing)
|
2016-08-18 22:22:03 +02:00
|
|
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &graphics.descriptorSetPreCompute));
|
2018-03-23 22:04:14 +01:00
|
|
|
std::vector<VkWriteDescriptorSet> baseImageWriteDescriptorSets = {
|
2024-01-13 11:27:06 +01:00
|
|
|
vks::initializers::writeDescriptorSet(graphics.descriptorSetPreCompute, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &graphics.uniformBuffer.descriptor),
|
2018-03-23 22:04:14 +01:00
|
|
|
vks::initializers::writeDescriptorSet(graphics.descriptorSetPreCompute, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, &textureColorMap.descriptor)
|
|
|
|
|
};
|
2023-12-22 14:30:18 +01:00
|
|
|
vkUpdateDescriptorSets(device, static_cast<uint32_t>(baseImageWriteDescriptorSets.size()), baseImageWriteDescriptorSets.data(), 0, nullptr);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2018-03-23 22:04:14 +01:00
|
|
|
// Final image (after compute shader processing)
|
|
|
|
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &graphics.descriptorSetPostCompute));
|
2020-05-29 16:08:53 +01:00
|
|
|
std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
|
2024-01-13 11:27:06 +01:00
|
|
|
vks::initializers::writeDescriptorSet(graphics.descriptorSetPostCompute, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &graphics.uniformBuffer.descriptor),
|
|
|
|
|
vks::initializers::writeDescriptorSet(graphics.descriptorSetPostCompute, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, &storageImage.descriptor)
|
2016-02-16 15:07:25 +01:00
|
|
|
};
|
2023-12-22 14:30:18 +01:00
|
|
|
vkUpdateDescriptorSets(device, static_cast<uint32_t>(writeDescriptorSets.size()), writeDescriptorSets.data(), 0, nullptr);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2024-01-13 11:27:06 +01:00
|
|
|
// Graphics pipeline used to display the images (before and after the compute effect is applied)
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2024-01-13 11:27:06 +01:00
|
|
|
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&graphics.descriptorSetLayout, 1);
|
|
|
|
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &graphics.pipelineLayout));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2024-01-13 11:27:06 +01:00
|
|
|
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_NONE, VK_FRONT_FACE_COUNTER_CLOCKWISE, 0);
|
|
|
|
|
VkPipelineColorBlendAttachmentState blendAttachmentState = vks::initializers::pipelineColorBlendAttachmentState(0xf, VK_FALSE);
|
|
|
|
|
VkPipelineColorBlendStateCreateInfo colorBlendState = vks::initializers::pipelineColorBlendStateCreateInfo(1, &blendAttachmentState);
|
|
|
|
|
VkPipelineDepthStencilStateCreateInfo depthStencilState = vks::initializers::pipelineDepthStencilStateCreateInfo(VK_TRUE, VK_TRUE, VK_COMPARE_OP_LESS_OR_EQUAL);
|
|
|
|
|
VkPipelineViewportStateCreateInfo viewportState = vks::initializers::pipelineViewportStateCreateInfo(1, 1, 0);
|
|
|
|
|
VkPipelineMultisampleStateCreateInfo multisampleState = vks::initializers::pipelineMultisampleStateCreateInfo(VK_SAMPLE_COUNT_1_BIT, 0);
|
|
|
|
|
std::vector<VkDynamicState> dynamicStateEnables = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
|
|
|
|
|
VkPipelineDynamicStateCreateInfo dynamicState = vks::initializers::pipelineDynamicStateCreateInfo(dynamicStateEnables);
|
|
|
|
|
std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2024-01-13 11:27:06 +01:00
|
|
|
// Shaders
|
2020-05-29 16:08:53 +01:00
|
|
|
shaderStages[0] = loadShader(getShadersPath() + "computeshader/texture.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
|
|
|
|
shaderStages[1] = loadShader(getShadersPath() + "computeshader/texture.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2024-01-13 11:27:06 +01:00
|
|
|
// Vertex input state
|
|
|
|
|
std::vector<VkVertexInputBindingDescription> vertexInputBindings = {
|
|
|
|
|
vks::initializers::vertexInputBindingDescription(0, sizeof(Vertex), VK_VERTEX_INPUT_RATE_VERTEX)
|
|
|
|
|
};
|
|
|
|
|
std::vector<VkVertexInputAttributeDescription> vertexInputAttributes = {
|
|
|
|
|
vks::initializers::vertexInputAttributeDescription(0, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(Vertex, pos)),
|
|
|
|
|
vks::initializers::vertexInputAttributeDescription(0, 1, VK_FORMAT_R32G32_SFLOAT, offsetof(Vertex, uv)),
|
|
|
|
|
};
|
|
|
|
|
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(graphics.pipelineLayout, renderPass, 0);
|
|
|
|
|
pipelineCreateInfo.pVertexInputState = &vertexInputState;
|
2016-02-16 15:07:25 +01:00
|
|
|
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
|
|
|
|
|
pipelineCreateInfo.pRasterizationState = &rasterizationState;
|
|
|
|
|
pipelineCreateInfo.pColorBlendState = &colorBlendState;
|
|
|
|
|
pipelineCreateInfo.pMultisampleState = &multisampleState;
|
|
|
|
|
pipelineCreateInfo.pViewportState = &viewportState;
|
|
|
|
|
pipelineCreateInfo.pDepthStencilState = &depthStencilState;
|
|
|
|
|
pipelineCreateInfo.pDynamicState = &dynamicState;
|
2023-12-22 14:30:18 +01:00
|
|
|
pipelineCreateInfo.stageCount = static_cast<uint32_t>(shaderStages.size());
|
2016-02-16 15:07:25 +01:00
|
|
|
pipelineCreateInfo.pStages = shaderStages.data();
|
2016-08-18 22:22:03 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &graphics.pipeline));
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
void prepareCompute()
|
|
|
|
|
{
|
2020-08-29 12:19:05 +02:00
|
|
|
// Get a compute queue from the device
|
|
|
|
|
vkGetDeviceQueue(device, vulkanDevice->queueFamilyIndices.compute, 0, &compute.queue);
|
2016-08-18 22:22:03 +02:00
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
// Create compute pipeline
|
2016-08-18 22:22:03 +02:00
|
|
|
// Compute pipelines are created separate from graphics pipelines even if they use the same queue
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
|
2018-03-23 22:04:14 +01:00
|
|
|
// Binding 0: Input image (read-only)
|
|
|
|
|
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, VK_SHADER_STAGE_COMPUTE_BIT, 0),
|
|
|
|
|
// Binding 1: Output image (write)
|
|
|
|
|
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, VK_SHADER_STAGE_COMPUTE_BIT, 1),
|
2016-02-16 15:07:25 +01:00
|
|
|
};
|
|
|
|
|
|
2018-03-23 22:04:14 +01:00
|
|
|
VkDescriptorSetLayoutCreateInfo descriptorLayout = vks::initializers::descriptorSetLayoutCreateInfo(setLayoutBindings);
|
2016-08-18 22:22:03 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateDescriptorSetLayout(device, &descriptorLayout, nullptr, &compute.descriptorSetLayout));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2024-01-13 11:27:06 +01:00
|
|
|
VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&compute.descriptorSetLayout, 1);
|
|
|
|
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &compute.pipelineLayout));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2024-01-13 11:27:06 +01:00
|
|
|
VkDescriptorSetAllocateInfo allocInfo = vks::initializers::descriptorSetAllocateInfo(descriptorPool, &compute.descriptorSetLayout, 1);
|
2016-08-18 22:22:03 +02:00
|
|
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &compute.descriptorSet));
|
2020-05-29 16:08:53 +01:00
|
|
|
std::vector<VkWriteDescriptorSet> computeWriteDescriptorSets = {
|
2018-03-23 22:04:14 +01:00
|
|
|
vks::initializers::writeDescriptorSet(compute.descriptorSet, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 0, &textureColorMap.descriptor),
|
2024-01-13 11:27:06 +01:00
|
|
|
vks::initializers::writeDescriptorSet(compute.descriptorSet, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, &storageImage.descriptor)
|
2016-02-16 15:07:25 +01:00
|
|
|
};
|
2023-12-22 14:30:18 +01:00
|
|
|
vkUpdateDescriptorSets(device, static_cast<uint32_t>(computeWriteDescriptorSets.size()), computeWriteDescriptorSets.data(), 0, nullptr);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Create compute shader pipelines
|
2024-01-13 11:27:06 +01:00
|
|
|
VkComputePipelineCreateInfo computePipelineCreateInfo = vks::initializers::computePipelineCreateInfo(compute.pipelineLayout, 0);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2024-01-13 11:27:06 +01:00
|
|
|
// One pipeline for each available image filter
|
|
|
|
|
filterNames = { "emboss", "edgedetect", "sharpen" };
|
|
|
|
|
for (auto& shaderName : filterNames) {
|
2020-05-29 16:08:53 +01:00
|
|
|
std::string fileName = getShadersPath() + "computeshader/" + shaderName + ".comp.spv";
|
2018-04-13 18:06:30 +02:00
|
|
|
computePipelineCreateInfo.stage = loadShader(fileName, VK_SHADER_STAGE_COMPUTE_BIT);
|
2016-02-16 15:07:25 +01:00
|
|
|
VkPipeline pipeline;
|
2016-05-23 21:22:00 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateComputePipelines(device, pipelineCache, 1, &computePipelineCreateInfo, nullptr, &pipeline));
|
2016-08-18 22:22:03 +02:00
|
|
|
compute.pipelines.push_back(pipeline);
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
2016-08-18 22:22:03 +02:00
|
|
|
|
|
|
|
|
// Separate command pool as queue family for compute may be different than graphics
|
|
|
|
|
VkCommandPoolCreateInfo cmdPoolInfo = {};
|
|
|
|
|
cmdPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
|
2020-08-29 12:19:05 +02:00
|
|
|
cmdPoolInfo.queueFamilyIndex = vulkanDevice->queueFamilyIndices.compute;
|
2016-08-18 22:22:03 +02:00
|
|
|
cmdPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
|
|
|
|
|
VK_CHECK_RESULT(vkCreateCommandPool(device, &cmdPoolInfo, nullptr, &compute.commandPool));
|
|
|
|
|
|
|
|
|
|
// Create a command buffer for compute operations
|
2024-01-13 11:27:06 +01:00
|
|
|
VkCommandBufferAllocateInfo cmdBufAllocateInfo = vks::initializers::commandBufferAllocateInfo( compute.commandPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY, 1);
|
2016-08-18 22:22:03 +02:00
|
|
|
VK_CHECK_RESULT(vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, &compute.commandBuffer));
|
|
|
|
|
|
2020-08-29 12:19:05 +02:00
|
|
|
// Semaphore for compute & graphics sync
|
|
|
|
|
VkSemaphoreCreateInfo semaphoreCreateInfo = vks::initializers::semaphoreCreateInfo();
|
|
|
|
|
VK_CHECK_RESULT(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &compute.semaphore));
|
|
|
|
|
|
2016-08-18 22:22:03 +02:00
|
|
|
// Build a single command buffer containing the compute dispatch commands
|
|
|
|
|
buildComputeCommandBuffer();
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void prepareUniformBuffers()
|
|
|
|
|
{
|
|
|
|
|
// Vertex shader uniform buffer block
|
2024-01-13 11:27:06 +01:00
|
|
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &graphics.uniformBuffer, sizeof(Graphics::UniformData)));
|
2016-12-24 12:48:01 +01:00
|
|
|
// Map persistent
|
2024-01-13 11:27:06 +01:00
|
|
|
VK_CHECK_RESULT(graphics.uniformBuffer.map());
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void updateUniformBuffers()
|
|
|
|
|
{
|
2024-01-13 11:27:06 +01:00
|
|
|
// We need to adjust the perspective as this sample displays two viewports side-by-side
|
|
|
|
|
camera.setPerspective(60.0f, (float)width * 0.5f / (float)height, 1.0f, 256.0f);
|
|
|
|
|
graphics.uniformData.projection = camera.matrices.perspective;
|
|
|
|
|
graphics.uniformData.modelView = camera.matrices.view;
|
|
|
|
|
memcpy(graphics.uniformBuffer.mapped, &graphics.uniformData, sizeof(Graphics::UniformData));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void prepare()
|
|
|
|
|
{
|
|
|
|
|
VulkanExampleBase::prepare();
|
|
|
|
|
loadAssets();
|
|
|
|
|
generateQuad();
|
|
|
|
|
prepareUniformBuffers();
|
|
|
|
|
prepareStorageImage();
|
|
|
|
|
setupDescriptorPool();
|
|
|
|
|
prepareGraphics();
|
|
|
|
|
prepareCompute();
|
|
|
|
|
buildCommandBuffers();
|
|
|
|
|
prepared = true;
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
2016-05-23 21:22:00 +02:00
|
|
|
void draw()
|
|
|
|
|
{
|
2021-08-03 23:04:40 +08:00
|
|
|
// Wait for rendering finished
|
|
|
|
|
VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
|
|
|
|
|
|
|
|
|
|
// Submit compute commands
|
|
|
|
|
VkSubmitInfo computeSubmitInfo = vks::initializers::submitInfo();
|
|
|
|
|
computeSubmitInfo.commandBufferCount = 1;
|
|
|
|
|
computeSubmitInfo.pCommandBuffers = &compute.commandBuffer;
|
|
|
|
|
computeSubmitInfo.waitSemaphoreCount = 1;
|
|
|
|
|
computeSubmitInfo.pWaitSemaphores = &graphics.semaphore;
|
|
|
|
|
computeSubmitInfo.pWaitDstStageMask = &waitStageMask;
|
|
|
|
|
computeSubmitInfo.signalSemaphoreCount = 1;
|
|
|
|
|
computeSubmitInfo.pSignalSemaphores = &compute.semaphore;
|
2024-01-13 11:27:06 +01:00
|
|
|
VK_CHECK_RESULT(vkQueueSubmit(compute.queue, 1, &computeSubmitInfo, VK_NULL_HANDLE));
|
2016-05-23 21:22:00 +02:00
|
|
|
VulkanExampleBase::prepareFrame();
|
|
|
|
|
|
2020-08-29 12:19:05 +02:00
|
|
|
VkPipelineStageFlags graphicsWaitStageMasks[] = { VK_PIPELINE_STAGE_VERTEX_INPUT_BIT, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
|
|
|
|
|
VkSemaphore graphicsWaitSemaphores[] = { compute.semaphore, semaphores.presentComplete };
|
|
|
|
|
VkSemaphore graphicsSignalSemaphores[] = { graphics.semaphore, semaphores.renderComplete };
|
|
|
|
|
|
|
|
|
|
// Submit graphics commands
|
2016-05-23 21:22:00 +02:00
|
|
|
submitInfo.commandBufferCount = 1;
|
|
|
|
|
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
|
2020-08-29 12:19:05 +02:00
|
|
|
submitInfo.waitSemaphoreCount = 2;
|
|
|
|
|
submitInfo.pWaitSemaphores = graphicsWaitSemaphores;
|
|
|
|
|
submitInfo.pWaitDstStageMask = graphicsWaitStageMasks;
|
|
|
|
|
submitInfo.signalSemaphoreCount = 2;
|
|
|
|
|
submitInfo.pSignalSemaphores = graphicsSignalSemaphores;
|
2016-05-23 21:22:00 +02:00
|
|
|
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
|
|
|
|
|
|
|
|
|
VulkanExampleBase::submitFrame();
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
virtual void render()
|
|
|
|
|
{
|
|
|
|
|
if (!prepared)
|
|
|
|
|
return;
|
|
|
|
|
draw();
|
2022-06-13 23:04:53 -04:00
|
|
|
updateUniformBuffers();
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-01 14:22:10 +01:00
|
|
|
virtual void OnUpdateUIOverlay(vks::UIOverlay *overlay)
|
2016-05-23 21:22:00 +02:00
|
|
|
{
|
2017-11-01 14:22:10 +01:00
|
|
|
if (overlay->header("Settings")) {
|
2024-01-13 11:27:06 +01:00
|
|
|
if (overlay->comboBox("Shader", &compute.pipelineIndex, filterNames)) {
|
2017-11-01 14:22:10 +01:00
|
|
|
buildComputeCommandBuffer();
|
|
|
|
|
}
|
2016-05-23 21:22:00 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-02-16 15:07:25 +01:00
|
|
|
};
|
|
|
|
|
|
2016-08-18 22:22:03 +02:00
|
|
|
VULKAN_EXAMPLE_MAIN()
|