2016-02-16 15:07:25 +01:00
|
|
|
/*
|
|
|
|
|
* Vulkan Example - Compute shader image processing
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de
|
|
|
|
|
*
|
|
|
|
|
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
#define GLM_FORCE_RADIANS
|
2016-03-08 21:52:40 +01:00
|
|
|
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
2016-02-16 15:07:25 +01:00
|
|
|
#include <glm/glm.hpp>
|
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
|
|
|
|
|
|
#include <vulkan/vulkan.h>
|
|
|
|
|
#include "vulkanexamplebase.h"
|
2017-02-09 21:55:35 +01:00
|
|
|
#include "VulkanTexture.hpp"
|
2017-02-12 10:44:51 +01:00
|
|
|
#include "VulkanBuffer.hpp"
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
#define VERTEX_BUFFER_BIND_ID 0
|
|
|
|
|
#define ENABLE_VALIDATION false
|
|
|
|
|
|
|
|
|
|
// Vertex layout for this example
|
|
|
|
|
struct Vertex {
|
|
|
|
|
float pos[3];
|
|
|
|
|
float uv[2];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class VulkanExample : public VulkanExampleBase
|
|
|
|
|
{
|
|
|
|
|
private:
|
2017-02-09 21:55:35 +01:00
|
|
|
vks::Texture2D textureColorMap;
|
|
|
|
|
vks::Texture2D textureComputeTarget;
|
2016-02-16 15:07:25 +01:00
|
|
|
public:
|
|
|
|
|
struct {
|
|
|
|
|
VkPipelineVertexInputStateCreateInfo inputState;
|
|
|
|
|
std::vector<VkVertexInputBindingDescription> bindingDescriptions;
|
|
|
|
|
std::vector<VkVertexInputAttributeDescription> attributeDescriptions;
|
|
|
|
|
} vertices;
|
|
|
|
|
|
2016-08-18 22:22:03 +02:00
|
|
|
// Resources for the graphics part of the example
|
|
|
|
|
struct {
|
|
|
|
|
VkDescriptorSetLayout descriptorSetLayout; // Image display shader binding layout
|
|
|
|
|
VkDescriptorSet descriptorSetPreCompute; // Image display shader bindings before compute shader image manipulation
|
|
|
|
|
VkDescriptorSet descriptorSetPostCompute; // Image display shader bindings after compute shader image manipulation
|
|
|
|
|
VkPipeline pipeline; // Image display pipeline
|
|
|
|
|
VkPipelineLayout pipelineLayout; // Layout of the graphics pipeline
|
|
|
|
|
} graphics;
|
|
|
|
|
|
|
|
|
|
// Resources for the compute part of the example
|
|
|
|
|
struct Compute {
|
|
|
|
|
VkQueue queue; // Separate queue for compute commands (queue family may differ from the one used for graphics)
|
|
|
|
|
VkCommandPool commandPool; // Use a separate command pool (queue family may differ from the one used for graphics)
|
|
|
|
|
VkCommandBuffer commandBuffer; // Command buffer storing the dispatch commands and barriers
|
|
|
|
|
VkFence fence; // Synchronization fence to avoid rewriting compute CB if still in use
|
|
|
|
|
VkDescriptorSetLayout descriptorSetLayout; // Compute shader binding layout
|
|
|
|
|
VkDescriptorSet descriptorSet; // Compute shader bindings
|
|
|
|
|
VkPipelineLayout pipelineLayout; // Layout of the compute pipeline
|
|
|
|
|
std::vector<VkPipeline> pipelines; // Compute pipelines for image filters
|
2017-11-01 14:22:10 +01:00
|
|
|
int32_t pipelineIndex = 0; // Current image filtering compute pipeline index
|
2016-08-18 22:22:03 +02:00
|
|
|
uint32_t queueFamilyIndex; // Family index of the graphics queue, used for barriers
|
|
|
|
|
} compute;
|
|
|
|
|
|
2017-02-12 10:44:51 +01:00
|
|
|
vks::Buffer vertexBuffer;
|
|
|
|
|
vks::Buffer indexBuffer;
|
2017-01-07 20:46:28 +01:00
|
|
|
uint32_t indexCount;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2017-02-12 10:44:51 +01:00
|
|
|
vks::Buffer uniformBufferVS;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
glm::mat4 projection;
|
|
|
|
|
glm::mat4 model;
|
|
|
|
|
} uboVS;
|
|
|
|
|
|
|
|
|
|
int vertexBufferSize;
|
|
|
|
|
|
2017-11-01 14:22:10 +01:00
|
|
|
std::vector<std::string> shaderNames;
|
|
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
|
|
|
|
|
{
|
|
|
|
|
zoom = -2.0f;
|
2017-11-01 14:22:10 +01:00
|
|
|
title = "Compute shader image load/store";
|
|
|
|
|
settings.overlay = true;
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~VulkanExample()
|
|
|
|
|
{
|
2016-08-18 22:22:03 +02:00
|
|
|
// Graphics
|
|
|
|
|
vkDestroyPipeline(device, graphics.pipeline, nullptr);
|
|
|
|
|
vkDestroyPipelineLayout(device, graphics.pipelineLayout, nullptr);
|
|
|
|
|
vkDestroyDescriptorSetLayout(device, graphics.descriptorSetLayout, nullptr);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-08-18 22:22:03 +02:00
|
|
|
// Compute
|
|
|
|
|
for (auto& pipeline : compute.pipelines)
|
2016-02-16 15:07:25 +01:00
|
|
|
{
|
|
|
|
|
vkDestroyPipeline(device, pipeline, nullptr);
|
|
|
|
|
}
|
2016-08-18 22:22:03 +02:00
|
|
|
vkDestroyPipelineLayout(device, compute.pipelineLayout, nullptr);
|
|
|
|
|
vkDestroyDescriptorSetLayout(device, compute.descriptorSetLayout, nullptr);
|
|
|
|
|
vkDestroyFence(device, compute.fence, nullptr);
|
|
|
|
|
vkDestroyCommandPool(device, compute.commandPool, nullptr);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2017-01-07 20:46:28 +01:00
|
|
|
vertexBuffer.destroy();
|
|
|
|
|
indexBuffer.destroy();
|
2016-12-24 12:48:01 +01:00
|
|
|
uniformBufferVS.destroy();
|
2017-01-07 20:46:28 +01:00
|
|
|
|
2017-02-09 21:55:35 +01:00
|
|
|
textureColorMap.destroy();
|
|
|
|
|
textureComputeTarget.destroy();
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
2016-03-16 19:55:38 +01:00
|
|
|
// Prepare a texture target that is used to store compute shader calculations
|
2017-02-09 21:55:35 +01:00
|
|
|
void prepareTextureTarget(vks::Texture *tex, uint32_t width, uint32_t height, VkFormat format)
|
2016-02-16 15:07:25 +01:00
|
|
|
{
|
|
|
|
|
VkFormatProperties formatProperties;
|
|
|
|
|
|
|
|
|
|
// Get device properties for the requested texture format
|
|
|
|
|
vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &formatProperties);
|
2016-03-16 19:55:38 +01:00
|
|
|
// Check if requested image format supports image storage operations
|
|
|
|
|
assert(formatProperties.optimalTilingFeatures & VK_FORMAT_FEATURE_STORAGE_IMAGE_BIT);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Prepare blit target texture
|
|
|
|
|
tex->width = width;
|
|
|
|
|
tex->height = height;
|
|
|
|
|
|
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;
|
|
|
|
|
imageCreateInfo.extent = { width, height, 1 };
|
|
|
|
|
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;
|
2016-08-18 22:22:03 +02:00
|
|
|
// Sharing mode exclusive means that ownership of the image does not need to be explicitly transferred between the compute and graphics queue
|
|
|
|
|
imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
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;
|
|
|
|
|
|
2016-05-23 21:22:00 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateImage(device, &imageCreateInfo, nullptr, &tex->image));
|
|
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
vkGetImageMemoryRequirements(device, tex->image, &memReqs);
|
|
|
|
|
memAllocInfo.allocationSize = memReqs.size;
|
2016-07-23 20:42:03 +02:00
|
|
|
memAllocInfo.memoryTypeIndex = vulkanDevice->getMemoryType(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
2016-05-23 21:22:00 +02:00
|
|
|
VK_CHECK_RESULT(vkAllocateMemory(device, &memAllocInfo, nullptr, &tex->deviceMemory));
|
|
|
|
|
VK_CHECK_RESULT(vkBindImageMemory(device, tex->image, tex->deviceMemory, 0));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-05-14 15:49:01 +02:00
|
|
|
VkCommandBuffer layoutCmd = VulkanExampleBase::createCommandBuffer(VK_COMMAND_BUFFER_LEVEL_PRIMARY, true);
|
|
|
|
|
|
|
|
|
|
tex->imageLayout = VK_IMAGE_LAYOUT_GENERAL;
|
2017-02-12 13:10:05 +01:00
|
|
|
vks::tools::setImageLayout(
|
2016-05-14 15:49:01 +02:00
|
|
|
layoutCmd, tex->image,
|
|
|
|
|
VK_IMAGE_ASPECT_COLOR_BIT,
|
|
|
|
|
VK_IMAGE_LAYOUT_UNDEFINED,
|
|
|
|
|
tex->imageLayout);
|
|
|
|
|
|
|
|
|
|
VulkanExampleBase::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;
|
2018-03-23 22:04:14 +01:00
|
|
|
sampler.maxLod = tex->mipLevels;
|
2016-02-16 15:07:25 +01:00
|
|
|
sampler.borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE;
|
2016-05-23 21:22:00 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateSampler(device, &sampler, nullptr, &tex->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.components = { VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_G, VK_COMPONENT_SWIZZLE_B, VK_COMPONENT_SWIZZLE_A };
|
|
|
|
|
view.subresourceRange = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 };
|
|
|
|
|
view.image = tex->image;
|
2016-05-23 21:22:00 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateImageView(device, &view, nullptr, &tex->view));
|
2016-08-18 22:22:03 +02:00
|
|
|
|
|
|
|
|
// Initialize a descriptor for later use
|
|
|
|
|
tex->descriptor.imageLayout = tex->imageLayout;
|
|
|
|
|
tex->descriptor.imageView = tex->view;
|
|
|
|
|
tex->descriptor.sampler = tex->sampler;
|
2017-02-09 21:55:35 +01:00
|
|
|
tex->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()
|
|
|
|
|
{
|
|
|
|
|
// Destroy command buffers if already present
|
|
|
|
|
if (!checkCommandBuffers())
|
|
|
|
|
{
|
|
|
|
|
destroyCommandBuffers();
|
|
|
|
|
createCommandBuffers();
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
imageMemoryBarrier.image = textureComputeTarget.image;
|
|
|
|
|
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;
|
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 };
|
2017-01-07 20:46:28 +01:00
|
|
|
vkCmdBindVertexBuffers(drawCmdBuffers[i], VERTEX_BUFFER_BIND_ID, 1, &vertexBuffer.buffer, offsets);
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-08-18 22:22:03 +02:00
|
|
|
vkCmdDispatch(compute.commandBuffer, textureComputeTarget.width / 16, textureComputeTarget.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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Setup vertices for a single uv-mapped quad
|
|
|
|
|
void generateQuad()
|
|
|
|
|
{
|
2017-01-07 20:46:28 +01:00
|
|
|
// Setup vertices for a single uv-mapped quad made from two triangles
|
|
|
|
|
std::vector<Vertex> vertices =
|
2016-02-16 15:07:25 +01:00
|
|
|
{
|
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
|
|
|
|
2017-01-07 20:46:28 +01:00
|
|
|
// Create buffers
|
|
|
|
|
// For the sake of simplicity we won't stage the vertex data to the gpu memory
|
|
|
|
|
// 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,
|
|
|
|
|
vertices.size() * sizeof(Vertex),
|
|
|
|
|
vertices.data()));
|
|
|
|
|
// Index buffer
|
|
|
|
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
2016-02-16 15:07:25 +01:00
|
|
|
VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
|
2017-01-07 20:46:28 +01:00
|
|
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
|
|
|
|
&indexBuffer,
|
|
|
|
|
indices.size() * sizeof(uint32_t),
|
|
|
|
|
indices.data()));
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setupVertexDescriptions()
|
|
|
|
|
{
|
|
|
|
|
// Binding description
|
2018-03-23 22:04:14 +01:00
|
|
|
vertices.bindingDescriptions = {
|
|
|
|
|
vks::initializers::vertexInputBindingDescription(VERTEX_BUFFER_BIND_ID, sizeof(Vertex), VK_VERTEX_INPUT_RATE_VERTEX)
|
|
|
|
|
};
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Attribute descriptions
|
|
|
|
|
// Describes memory layout and shader positions
|
2018-03-23 22:04:14 +01:00
|
|
|
vertices.attributeDescriptions = {
|
|
|
|
|
// Location 0: Position
|
|
|
|
|
vks::initializers::vertexInputAttributeDescription(VERTEX_BUFFER_BIND_ID, 0, VK_FORMAT_R32G32B32_SFLOAT, offsetof(Vertex, pos)),
|
|
|
|
|
// Location 1: Texture coordinates
|
|
|
|
|
vks::initializers::vertexInputAttributeDescription(VERTEX_BUFFER_BIND_ID, 1, VK_FORMAT_R32G32_SFLOAT, offsetof(Vertex, uv)),
|
|
|
|
|
};
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
// Assign to vertex buffer
|
2017-02-12 11:12:42 +01:00
|
|
|
vertices.inputState = vks::initializers::pipelineVertexInputStateCreateInfo();
|
2016-02-16 15:07:25 +01:00
|
|
|
vertices.inputState.vertexBindingDescriptionCount = vertices.bindingDescriptions.size();
|
|
|
|
|
vertices.inputState.pVertexBindingDescriptions = vertices.bindingDescriptions.data();
|
|
|
|
|
vertices.inputState.vertexAttributeDescriptionCount = vertices.attributeDescriptions.size();
|
|
|
|
|
vertices.inputState.pVertexAttributeDescriptions = vertices.attributeDescriptions.data();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setupDescriptorPool()
|
|
|
|
|
{
|
2018-03-23 22:04:14 +01:00
|
|
|
std::vector<VkDescriptorPoolSize> poolSizes = {
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setupDescriptorSetLayout()
|
|
|
|
|
{
|
2018-03-23 22:04:14 +01:00
|
|
|
std::vector<VkDescriptorSetLayoutBinding> setLayoutBindings = {
|
|
|
|
|
// Binding 0: Vertex shader uniform buffer
|
|
|
|
|
vks::initializers::descriptorSetLayoutBinding(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, 0),
|
|
|
|
|
// Binding 1: Fragment shader input image
|
|
|
|
|
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));
|
2016-05-23 21:22:00 +02:00
|
|
|
|
2018-03-23 22:04:14 +01:00
|
|
|
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo = vks::initializers::pipelineLayoutCreateInfo(&graphics.descriptorSetLayout, 1);
|
2016-08-18 22:22:03 +02:00
|
|
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &graphics.pipelineLayout));
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setupDescriptorSet()
|
|
|
|
|
{
|
|
|
|
|
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 = {
|
|
|
|
|
vks::initializers::writeDescriptorSet(graphics.descriptorSetPreCompute, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBufferVS.descriptor),
|
|
|
|
|
vks::initializers::writeDescriptorSet(graphics.descriptorSetPreCompute, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, &textureColorMap.descriptor)
|
|
|
|
|
};
|
|
|
|
|
vkUpdateDescriptorSets(device, 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));
|
|
|
|
|
std::vector<VkWriteDescriptorSet> writeDescriptorSets = {
|
|
|
|
|
vks::initializers::writeDescriptorSet(graphics.descriptorSetPostCompute, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 0, &uniformBufferVS.descriptor),
|
|
|
|
|
vks::initializers::writeDescriptorSet(graphics.descriptorSetPostCompute, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, &textureComputeTarget.descriptor)
|
2016-02-16 15:07:25 +01:00
|
|
|
};
|
2018-03-23 22:04:14 +01:00
|
|
|
vkUpdateDescriptorSets(device, writeDescriptorSets.size(), writeDescriptorSets.data(), 0, nullptr);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void preparePipelines()
|
|
|
|
|
{
|
|
|
|
|
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::pipelineInputAssemblyStateCreateInfo(
|
2016-02-16 15:07:25 +01:00
|
|
|
VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
|
|
|
|
|
0,
|
|
|
|
|
VK_FALSE);
|
|
|
|
|
|
|
|
|
|
VkPipelineRasterizationStateCreateInfo rasterizationState =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::pipelineRasterizationStateCreateInfo(
|
2016-02-16 15:07:25 +01:00
|
|
|
VK_POLYGON_MODE_FILL,
|
|
|
|
|
VK_CULL_MODE_NONE,
|
|
|
|
|
VK_FRONT_FACE_COUNTER_CLOCKWISE,
|
|
|
|
|
0);
|
|
|
|
|
|
|
|
|
|
VkPipelineColorBlendAttachmentState blendAttachmentState =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::pipelineColorBlendAttachmentState(
|
2016-02-16 15:07:25 +01:00
|
|
|
0xf,
|
|
|
|
|
VK_FALSE);
|
|
|
|
|
|
|
|
|
|
VkPipelineColorBlendStateCreateInfo colorBlendState =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::pipelineColorBlendStateCreateInfo(
|
2016-02-16 15:07:25 +01:00
|
|
|
1,
|
|
|
|
|
&blendAttachmentState);
|
|
|
|
|
|
|
|
|
|
VkPipelineDepthStencilStateCreateInfo depthStencilState =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::pipelineDepthStencilStateCreateInfo(
|
2016-02-16 15:07:25 +01:00
|
|
|
VK_TRUE,
|
|
|
|
|
VK_TRUE,
|
|
|
|
|
VK_COMPARE_OP_LESS_OR_EQUAL);
|
|
|
|
|
|
|
|
|
|
VkPipelineViewportStateCreateInfo viewportState =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::pipelineViewportStateCreateInfo(1, 1, 0);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
VkPipelineMultisampleStateCreateInfo multisampleState =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::pipelineMultisampleStateCreateInfo(
|
2016-02-16 15:07:25 +01:00
|
|
|
VK_SAMPLE_COUNT_1_BIT,
|
|
|
|
|
0);
|
|
|
|
|
|
|
|
|
|
std::vector<VkDynamicState> dynamicStateEnables = {
|
|
|
|
|
VK_DYNAMIC_STATE_VIEWPORT,
|
|
|
|
|
VK_DYNAMIC_STATE_SCISSOR
|
|
|
|
|
};
|
|
|
|
|
VkPipelineDynamicStateCreateInfo dynamicState =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::pipelineDynamicStateCreateInfo(
|
2016-02-16 15:07:25 +01:00
|
|
|
dynamicStateEnables.data(),
|
|
|
|
|
dynamicStateEnables.size(),
|
|
|
|
|
0);
|
|
|
|
|
|
|
|
|
|
// Rendering pipeline
|
|
|
|
|
// Load shaders
|
|
|
|
|
std::array<VkPipelineShaderStageCreateInfo,2> shaderStages;
|
|
|
|
|
|
2016-03-24 23:07:14 +01:00
|
|
|
shaderStages[0] = loadShader(getAssetPath() + "shaders/computeshader/texture.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
|
|
|
|
shaderStages[1] = loadShader(getAssetPath() + "shaders/computeshader/texture.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
VkGraphicsPipelineCreateInfo pipelineCreateInfo =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::pipelineCreateInfo(
|
2016-08-18 22:22:03 +02:00
|
|
|
graphics.pipelineLayout,
|
2016-02-16 15:07:25 +01:00
|
|
|
renderPass,
|
|
|
|
|
0);
|
|
|
|
|
|
|
|
|
|
pipelineCreateInfo.pVertexInputState = &vertices.inputState;
|
|
|
|
|
pipelineCreateInfo.pInputAssemblyState = &inputAssemblyState;
|
|
|
|
|
pipelineCreateInfo.pRasterizationState = &rasterizationState;
|
|
|
|
|
pipelineCreateInfo.pColorBlendState = &colorBlendState;
|
|
|
|
|
pipelineCreateInfo.pMultisampleState = &multisampleState;
|
|
|
|
|
pipelineCreateInfo.pViewportState = &viewportState;
|
|
|
|
|
pipelineCreateInfo.pDepthStencilState = &depthStencilState;
|
|
|
|
|
pipelineCreateInfo.pDynamicState = &dynamicState;
|
|
|
|
|
pipelineCreateInfo.stageCount = shaderStages.size();
|
|
|
|
|
pipelineCreateInfo.pStages = shaderStages.data();
|
|
|
|
|
pipelineCreateInfo.renderPass = renderPass;
|
|
|
|
|
|
2016-08-18 22:22:03 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateGraphicsPipelines(device, pipelineCache, 1, &pipelineCreateInfo, nullptr, &graphics.pipeline));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find and create a compute capable device queue
|
|
|
|
|
void getComputeQueue()
|
|
|
|
|
{
|
|
|
|
|
uint32_t queueFamilyCount;
|
|
|
|
|
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, NULL);
|
|
|
|
|
assert(queueFamilyCount >= 1);
|
|
|
|
|
|
|
|
|
|
std::vector<VkQueueFamilyProperties> queueFamilyProperties;
|
|
|
|
|
queueFamilyProperties.resize(queueFamilyCount);
|
|
|
|
|
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, queueFamilyProperties.data());
|
|
|
|
|
|
|
|
|
|
// Some devices have dedicated compute queues, so we first try to find a queue that supports compute and not graphics
|
|
|
|
|
bool computeQueueFound = false;
|
|
|
|
|
for (uint32_t i = 0; i < static_cast<uint32_t>(queueFamilyProperties.size()); i++)
|
|
|
|
|
{
|
|
|
|
|
if ((queueFamilyProperties[i].queueFlags & VK_QUEUE_COMPUTE_BIT) && ((queueFamilyProperties[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) == 0))
|
|
|
|
|
{
|
|
|
|
|
compute.queueFamilyIndex = i;
|
|
|
|
|
computeQueueFound = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// If there is no dedicated compute queue, just find the first queue family that supports compute
|
|
|
|
|
if (!computeQueueFound)
|
|
|
|
|
{
|
|
|
|
|
for (uint32_t i = 0; i < static_cast<uint32_t>(queueFamilyProperties.size()); i++)
|
|
|
|
|
{
|
|
|
|
|
if (queueFamilyProperties[i].queueFlags & VK_QUEUE_COMPUTE_BIT)
|
|
|
|
|
{
|
|
|
|
|
compute.queueFamilyIndex = i;
|
|
|
|
|
computeQueueFound = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Compute is mandatory in Vulkan, so there must be at least one queue family that supports compute
|
|
|
|
|
assert(computeQueueFound);
|
2017-04-16 13:09:08 +02:00
|
|
|
// Get a compute queue from the device
|
2016-08-18 22:22:03 +02:00
|
|
|
vkGetDeviceQueue(device, compute.queueFamilyIndex, 0, &compute.queue);
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void prepareCompute()
|
|
|
|
|
{
|
2016-08-18 22:22:03 +02:00
|
|
|
getComputeQueue();
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
VkPipelineLayoutCreateInfo pPipelineLayoutCreateInfo =
|
2018-03-23 22:04:14 +01:00
|
|
|
vks::initializers::pipelineLayoutCreateInfo(&compute.descriptorSetLayout, 1);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-08-18 22:22:03 +02:00
|
|
|
VK_CHECK_RESULT(vkCreatePipelineLayout(device, &pPipelineLayoutCreateInfo, nullptr, &compute.pipelineLayout));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
VkDescriptorSetAllocateInfo allocInfo =
|
2018-03-23 22:04:14 +01:00
|
|
|
vks::initializers::descriptorSetAllocateInfo(descriptorPool, &compute.descriptorSetLayout, 1);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-08-18 22:22:03 +02:00
|
|
|
VK_CHECK_RESULT(vkAllocateDescriptorSets(device, &allocInfo, &compute.descriptorSet));
|
2018-03-23 22:04:14 +01:00
|
|
|
std::vector<VkWriteDescriptorSet> computeWriteDescriptorSets = {
|
|
|
|
|
vks::initializers::writeDescriptorSet(compute.descriptorSet, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 0, &textureColorMap.descriptor),
|
|
|
|
|
vks::initializers::writeDescriptorSet(compute.descriptorSet, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, 1, &textureComputeTarget.descriptor)
|
2016-02-16 15:07:25 +01:00
|
|
|
};
|
|
|
|
|
vkUpdateDescriptorSets(device, computeWriteDescriptorSets.size(), computeWriteDescriptorSets.data(), 0, NULL);
|
|
|
|
|
|
|
|
|
|
// Create compute shader pipelines
|
|
|
|
|
VkComputePipelineCreateInfo computePipelineCreateInfo =
|
2018-03-23 22:04:14 +01:00
|
|
|
vks::initializers::computePipelineCreateInfo(compute.pipelineLayout, 0);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-05-23 21:22:00 +02:00
|
|
|
// One pipeline for each effect
|
2018-03-23 22:04:14 +01:00
|
|
|
shaderNames = { "emboss", "edgedetect", "sharpen" };
|
2017-11-01 14:22:10 +01:00
|
|
|
for (auto& shaderName : shaderNames) {
|
2016-03-24 23:07:14 +01:00
|
|
|
std::string fileName = getAssetPath() + "shaders/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;
|
|
|
|
|
cmdPoolInfo.queueFamilyIndex = compute.queueFamilyIndex;
|
|
|
|
|
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
|
|
|
|
|
VkCommandBufferAllocateInfo cmdBufAllocateInfo =
|
2017-02-12 11:12:42 +01:00
|
|
|
vks::initializers::commandBufferAllocateInfo(
|
2016-08-18 22:22:03 +02:00
|
|
|
compute.commandPool,
|
|
|
|
|
VK_COMMAND_BUFFER_LEVEL_PRIMARY,
|
|
|
|
|
1);
|
|
|
|
|
|
|
|
|
|
VK_CHECK_RESULT(vkAllocateCommandBuffers(device, &cmdBufAllocateInfo, &compute.commandBuffer));
|
|
|
|
|
|
|
|
|
|
// Fence for compute CB sync
|
2017-02-12 11:12:42 +01:00
|
|
|
VkFenceCreateInfo fenceCreateInfo = vks::initializers::fenceCreateInfo(VK_FENCE_CREATE_SIGNALED_BIT);
|
2016-08-18 22:22:03 +02:00
|
|
|
VK_CHECK_RESULT(vkCreateFence(device, &fenceCreateInfo, nullptr, &compute.fence));
|
|
|
|
|
|
|
|
|
|
// Build a single command buffer containing the compute dispatch commands
|
|
|
|
|
buildComputeCommandBuffer();
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Prepare and initialize uniform buffer containing shader uniforms
|
|
|
|
|
void prepareUniformBuffers()
|
|
|
|
|
{
|
|
|
|
|
// Vertex shader uniform buffer block
|
2016-12-24 12:48:01 +01:00
|
|
|
VK_CHECK_RESULT(vulkanDevice->createBuffer(
|
2016-02-16 15:07:25 +01:00
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
|
2016-05-23 21:22:00 +02:00
|
|
|
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
|
2016-12-24 12:48:01 +01:00
|
|
|
&uniformBufferVS,
|
|
|
|
|
sizeof(uboVS)));
|
|
|
|
|
|
|
|
|
|
// Map persistent
|
|
|
|
|
VK_CHECK_RESULT(uniformBufferVS.map());
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
updateUniformBuffers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void updateUniformBuffers()
|
|
|
|
|
{
|
2016-05-23 21:22:00 +02:00
|
|
|
// Vertex shader uniform buffer block
|
2016-03-08 20:59:25 +01:00
|
|
|
uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width*0.5f / (float)height, 0.1f, 256.0f);
|
2017-09-24 18:17:07 +02:00
|
|
|
glm::mat4 viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, zoom));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2017-09-24 18:17:07 +02:00
|
|
|
uboVS.model = viewMatrix * glm::translate(glm::mat4(1.0f), cameraPos);
|
2016-03-08 20:59:25 +01:00
|
|
|
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
|
|
|
|
|
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
|
|
|
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-12-24 12:48:01 +01:00
|
|
|
memcpy(uniformBufferVS.mapped, &uboVS, sizeof(uboVS));
|
2016-02-16 15:07:25 +01:00
|
|
|
}
|
|
|
|
|
|
2016-05-23 21:22:00 +02:00
|
|
|
void draw()
|
|
|
|
|
{
|
|
|
|
|
VulkanExampleBase::prepareFrame();
|
|
|
|
|
|
|
|
|
|
submitInfo.commandBufferCount = 1;
|
|
|
|
|
submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer];
|
|
|
|
|
VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE));
|
|
|
|
|
|
|
|
|
|
VulkanExampleBase::submitFrame();
|
|
|
|
|
|
2016-08-18 22:22:03 +02:00
|
|
|
// Submit compute commands
|
|
|
|
|
// Use a fence to ensure that compute command buffer has finished executin before using it again
|
|
|
|
|
vkWaitForFences(device, 1, &compute.fence, VK_TRUE, UINT64_MAX);
|
|
|
|
|
vkResetFences(device, 1, &compute.fence);
|
|
|
|
|
|
2017-02-12 11:12:42 +01:00
|
|
|
VkSubmitInfo computeSubmitInfo = vks::initializers::submitInfo();
|
2016-05-23 21:22:00 +02:00
|
|
|
computeSubmitInfo.commandBufferCount = 1;
|
2016-08-18 22:22:03 +02:00
|
|
|
computeSubmitInfo.pCommandBuffers = &compute.commandBuffer;
|
2016-05-23 21:22:00 +02:00
|
|
|
|
2016-08-18 22:22:03 +02:00
|
|
|
VK_CHECK_RESULT(vkQueueSubmit(compute.queue, 1, &computeSubmitInfo, compute.fence));
|
2016-05-23 21:22:00 +02:00
|
|
|
}
|
|
|
|
|
|
2016-02-16 15:07:25 +01:00
|
|
|
void prepare()
|
|
|
|
|
{
|
|
|
|
|
VulkanExampleBase::prepare();
|
2017-02-09 21:55:35 +01:00
|
|
|
loadAssets();
|
2016-02-16 15:07:25 +01:00
|
|
|
generateQuad();
|
|
|
|
|
setupVertexDescriptions();
|
|
|
|
|
prepareUniformBuffers();
|
|
|
|
|
prepareTextureTarget(&textureComputeTarget, textureColorMap.width, textureColorMap.height, VK_FORMAT_R8G8B8A8_UNORM);
|
|
|
|
|
setupDescriptorSetLayout();
|
|
|
|
|
preparePipelines();
|
|
|
|
|
setupDescriptorPool();
|
|
|
|
|
setupDescriptorSet();
|
|
|
|
|
prepareCompute();
|
|
|
|
|
buildCommandBuffers();
|
|
|
|
|
prepared = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void render()
|
|
|
|
|
{
|
|
|
|
|
if (!prepared)
|
|
|
|
|
return;
|
|
|
|
|
draw();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void viewChanged()
|
|
|
|
|
{
|
|
|
|
|
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")) {
|
|
|
|
|
if (overlay->comboBox("Shader", &compute.pipelineIndex, shaderNames)) {
|
|
|
|
|
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()
|