Archived
1
Fork 0
This repository has been archived on 2025-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
prism/engine/gfx/vulkan/src/gfx_vulkan.cpp

1945 lines
71 KiB
C++
Raw Normal View History

2020-08-11 12:07:21 -04:00
#include "gfx_vulkan.hpp"
#include <vector>
#include <limits>
#include <cstddef>
#include <array>
#include <sstream>
2020-08-11 12:07:21 -04:00
#include "gfx_vulkan_buffer.hpp"
#include "gfx_vulkan_pipeline.hpp"
#include "gfx_commandbuffer.hpp"
#include "gfx_vulkan_texture.hpp"
#include "gfx_vulkan_framebuffer.hpp"
#include "gfx_vulkan_renderpass.hpp"
#include "gfx_vulkan_sampler.hpp"
2020-08-11 12:07:21 -04:00
#include "file.hpp"
#include "log.hpp"
#include "utility.hpp"
#include "gfx_vulkan_commandbuffer.hpp"
2020-08-11 12:07:21 -04:00
#include <platform.hpp>
2021-03-01 14:40:02 -05:00
#ifdef PLATFORM_LINUX
#include <SDL2/SDL.h>
#endif
2020-08-11 12:07:21 -04:00
VkFormat toVkFormat(GFXPixelFormat format) {
switch (format) {
case GFXPixelFormat::R_32F:
return VK_FORMAT_R32_SFLOAT;
2020-09-23 09:53:45 -04:00
case GFXPixelFormat::R_16F:
return VK_FORMAT_R16_SFLOAT;
2020-08-11 12:07:21 -04:00
case GFXPixelFormat::RGBA_32F:
return VK_FORMAT_R32G32B32A32_SFLOAT;
case GFXPixelFormat::RGBA8_UNORM:
return VK_FORMAT_R8G8B8A8_UNORM;
case GFXPixelFormat::R8_UNORM:
return VK_FORMAT_R8_UNORM;
case GFXPixelFormat::R8G8_UNORM:
return VK_FORMAT_R8G8_UNORM;
2020-09-23 09:53:45 -04:00
case GFXPixelFormat::R8G8_SFLOAT:
return VK_FORMAT_R16G16_SFLOAT;
2020-09-23 09:53:45 -04:00
2020-08-11 12:07:21 -04:00
case GFXPixelFormat::R8G8B8A8_UNORM:
return VK_FORMAT_R8G8B8A8_UNORM;
case GFXPixelFormat::R16G16B16A16_SFLOAT:
return VK_FORMAT_R16G16B16A16_SFLOAT;
case GFXPixelFormat::DEPTH_32F:
return VK_FORMAT_D32_SFLOAT;
}
return VK_FORMAT_UNDEFINED;
}
VkFormat toVkFormat(GFXVertexFormat format) {
switch (format) {
case GFXVertexFormat::FLOAT2:
return VK_FORMAT_R32G32_SFLOAT;
case GFXVertexFormat::FLOAT3:
return VK_FORMAT_R32G32B32_SFLOAT;
case GFXVertexFormat::FLOAT4:
return VK_FORMAT_R32G32B32A32_SFLOAT;
case GFXVertexFormat::INT:
return VK_FORMAT_R8_SINT;
case GFXVertexFormat::INT4:
return VK_FORMAT_R32G32B32A32_SINT;
case GFXVertexFormat::UNORM4:
return VK_FORMAT_R8G8B8A8_UNORM;
}
return VK_FORMAT_UNDEFINED;
}
VkBlendFactor toVkFactor(GFXBlendFactor factor) {
switch (factor) {
case GFXBlendFactor::One:
return VK_BLEND_FACTOR_ONE;
case GFXBlendFactor::OneMinusSrcAlpha:
return VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
case GFXBlendFactor::OneMinusSrcColor:
return VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR;
case GFXBlendFactor::SrcAlpha:
return VK_BLEND_FACTOR_SRC_ALPHA;
}
return VK_BLEND_FACTOR_ONE;
}
VkSamplerAddressMode toSamplerMode(SamplingMode mode) {
switch (mode) {
case SamplingMode::Repeat:
return VK_SAMPLER_ADDRESS_MODE_REPEAT;
case SamplingMode::ClampToBorder:
return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER;
case SamplingMode::ClampToEdge:
return VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
}
return VK_SAMPLER_ADDRESS_MODE_REPEAT;
}
VkFilter toFilter(GFXFilter filter) {
switch (filter) {
case GFXFilter::Nearest:
return VK_FILTER_NEAREST;
case GFXFilter::Linear:
return VK_FILTER_LINEAR;
}
return VK_FILTER_LINEAR;
}
VkBorderColor toBorderColor(GFXBorderColor color) {
switch (color) {
case GFXBorderColor::OpaqueBlack:
return VK_BORDER_COLOR_INT_OPAQUE_BLACK;
case GFXBorderColor::OpaqueWhite:
return VK_BORDER_COLOR_INT_OPAQUE_WHITE;
}
return VK_BORDER_COLOR_INT_OPAQUE_BLACK;
}
VkCompareOp toCompareFunc(GFXCompareFunction func) {
switch (func) {
case GFXCompareFunction::Never:
return VK_COMPARE_OP_NEVER;
break;
case GFXCompareFunction::Less:
return VK_COMPARE_OP_LESS;
break;
case GFXCompareFunction::Equal:
return VK_COMPARE_OP_EQUAL;
break;
case GFXCompareFunction::LessOrEqual:
return VK_COMPARE_OP_LESS_OR_EQUAL;
break;
case GFXCompareFunction::Greater:
return VK_COMPARE_OP_GREATER;
break;
case GFXCompareFunction::NotEqual:
return VK_COMPARE_OP_NOT_EQUAL;
break;
case GFXCompareFunction::GreaterOrEqual:
return VK_COMPARE_OP_GREATER_OR_EQUAL;
break;
case GFXCompareFunction::Always:
return VK_COMPARE_OP_ALWAYS;
break;
}
}
2020-08-11 12:07:21 -04:00
VKAPI_ATTR VkBool32 VKAPI_CALL DebugCallback(
VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
VkDebugUtilsMessageTypeFlagsEXT messageType,
const VkDebugUtilsMessengerCallbackDataEXT *pCallbackData,
void *pUserData) {
prism::log::debug(System::GFX, pCallbackData->pMessage);
2020-08-11 12:07:21 -04:00
return VK_FALSE;
}
VkResult name_object(VkDevice device, VkObjectType type, uint64_t object, std::string_view name) {
if(object == 0x0) {
prism::log::error(System::GFX, "Failed to name object {}", name);
return VK_ERROR_DEVICE_LOST;
}
2020-08-11 12:07:21 -04:00
VkDebugUtilsObjectNameInfoEXT info = {};
info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
info.objectType = type;
info.pObjectName = name.data();
info.objectHandle = object;
auto func = (PFN_vkSetDebugUtilsObjectNameEXT)vkGetDeviceProcAddr(device, "vkSetDebugUtilsObjectNameEXT");
if (func != nullptr)
return func(device, &info);
else
return VK_ERROR_EXTENSION_NOT_PRESENT;
}
bool GFXVulkan::initialize(const GFXCreateInfo& info) {
#ifdef PLATFORM_WINDOWS
const char* surface_name = "VK_KHR_win32_surface";
#endif
uint32_t extensionPropertyCount = 0;
vkEnumerateInstanceExtensionProperties(nullptr, &extensionPropertyCount, nullptr);
std::vector<VkExtensionProperties> extensionProperties(extensionPropertyCount);
vkEnumerateInstanceExtensionProperties(nullptr, &extensionPropertyCount, extensionProperties.data());
2021-03-01 14:40:02 -05:00
std::vector<const char*> enabledExtensions = {};
for (auto prop : extensionProperties) {
if (!strcmp(prop.extensionName, "VK_EXT_debug_utils"))
enabledExtensions.push_back("VK_EXT_debug_utils");
}
2021-03-01 14:40:02 -05:00
auto required_extensions = platform::get_native_surface_extension();
enabledExtensions.insert(enabledExtensions.end(), required_extensions.begin(), required_extensions.end());
createInstance({}, enabledExtensions);
2020-08-11 12:07:21 -04:00
createLogicalDevice({ VK_KHR_SWAPCHAIN_EXTENSION_NAME });
createDescriptorPool();
return true;
}
void GFXVulkan::initialize_view(void* native_handle, const int identifier, const uint32_t width, const uint32_t height) {
vkDeviceWaitIdle(device);
NativeSurface* surface = new NativeSurface();
surface->identifier = identifier;
surface->surfaceWidth = width;
surface->surfaceHeight = height;
surface->windowNativeHandle = native_handle;
createSwapchain(surface);
createSyncPrimitives(surface);
native_surfaces.push_back(surface);
2020-08-11 12:07:21 -04:00
}
void GFXVulkan::recreate_view(const int identifier, const uint32_t width, const uint32_t height) {
vkDeviceWaitIdle(device);
NativeSurface* found_surface = nullptr;
for(auto surface : native_surfaces) {
if(surface->identifier == identifier)
found_surface = surface;
}
if(found_surface != nullptr) {
found_surface->surfaceWidth = width;
found_surface->surfaceHeight = height;
createSwapchain(found_surface, found_surface->swapchain);
}
2020-08-11 12:07:21 -04:00
}
GFXBuffer* GFXVulkan::create_buffer(void *data, const GFXSize size, const bool dynamic_data, const GFXBufferUsage usage) {
GFXVulkanBuffer* buffer = new GFXVulkanBuffer();
vkDeviceWaitIdle(device);
// choose buffer features
VkBufferUsageFlags bufferUsage = 0;
if (usage == GFXBufferUsage::Storage)
bufferUsage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
else if (usage == GFXBufferUsage::Vertex)
bufferUsage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
else
bufferUsage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
// create buffer
VkBufferCreateInfo bufferInfo = {};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.size = size;
bufferInfo.usage = bufferUsage;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
vkCreateBuffer(device, &bufferInfo, nullptr, &buffer->handle);
2020-08-11 12:07:21 -04:00
buffer->size = size;
// allocate memory
VkMemoryRequirements memRequirements;
vkGetBufferMemoryRequirements(device, buffer->handle, &memRequirements);
2020-08-11 12:07:21 -04:00
VkMemoryAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
vkAllocateMemory(device, &allocInfo, nullptr, &buffer->memory);
2020-08-11 12:07:21 -04:00
vkBindBufferMemory(device, buffer->handle, buffer->memory, 0);
2020-08-11 12:07:21 -04:00
if (data != nullptr) {
void* mapped_data;
vkMapMemory(device, buffer->memory, 0, size, 0, &mapped_data);
memcpy(mapped_data, data, size);
vkUnmapMemory(device, buffer->memory);
2020-08-11 12:07:21 -04:00
}
return buffer;
}
void GFXVulkan::copy_buffer(GFXBuffer* buffer, void* data, GFXSize offset, GFXSize size) {
GFXVulkanBuffer* vulkanBuffer = (GFXVulkanBuffer*)buffer;
void* mapped_data = nullptr;
vkMapMemory(device, vulkanBuffer->memory, offset, vulkanBuffer->size - offset, 0, &mapped_data);
if(mapped_data == nullptr)
return;
2020-08-11 12:07:21 -04:00
memcpy(mapped_data, data, size);
VkMappedMemoryRange range = {};
range.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
range.memory = vulkanBuffer->memory;
range.size = size;
range.offset = offset;
vkFlushMappedMemoryRanges(device, 1, &range);
vkUnmapMemory(device, vulkanBuffer->memory);
2020-08-11 12:07:21 -04:00
}
void* GFXVulkan::get_buffer_contents(GFXBuffer* buffer) {
GFXVulkanBuffer* vulkanBuffer = (GFXVulkanBuffer*)buffer;
void* mapped_data;
vkMapMemory(device, vulkanBuffer->memory, 0, vulkanBuffer->size, 0, &mapped_data);
2020-08-11 12:07:21 -04:00
return mapped_data;
}
void GFXVulkan::release_buffer_contents(GFXBuffer* buffer, void* handle) {
GFXVulkanBuffer* vulkanBuffer = (GFXVulkanBuffer*)buffer;
vkUnmapMemory(device, vulkanBuffer->memory);
2020-08-11 12:07:21 -04:00
}
GFXTexture* GFXVulkan::create_texture(const GFXTextureCreateInfo& info) {
GFXVulkanTexture* texture = new GFXVulkanTexture();
vkDeviceWaitIdle(device);
// choose image features
VkFormat imageFormat = toVkFormat(info.format);
VkImageTiling imageTiling;
imageTiling = VK_IMAGE_TILING_OPTIMAL;
VkImageUsageFlags imageUsage = 0;
if ((info.usage & GFXTextureUsage::Attachment) == GFXTextureUsage::Attachment) {
if (info.format == GFXPixelFormat::DEPTH_32F) {
imageUsage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
}
else {
imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
}
}
else {
imageUsage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
}
2021-02-15 17:59:54 -05:00
if((info.usage & GFXTextureUsage::Transfer) == GFXTextureUsage::Transfer)
imageUsage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
2020-08-11 12:07:21 -04:00
2021-02-03 09:04:30 -05:00
VkImageAspectFlagBits imageAspect;
2020-08-11 12:07:21 -04:00
if (info.format == GFXPixelFormat::DEPTH_32F)
imageAspect = VK_IMAGE_ASPECT_DEPTH_BIT;
else
imageAspect = VK_IMAGE_ASPECT_COLOR_BIT;
int array_length = info.array_length;
if (info.type == GFXTextureType::Cubemap)
array_length = 6;
else if (info.type == GFXTextureType::CubemapArray)
array_length *= 6;
2020-08-11 12:07:21 -04:00
// create image
VkImageCreateInfo imageInfo = {};
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageInfo.imageType = VK_IMAGE_TYPE_2D;
imageInfo.extent.width = info.width;
imageInfo.extent.height = info.height;
imageInfo.extent.depth = 1;
imageInfo.mipLevels = info.mip_count;
imageInfo.arrayLayers = array_length;
2020-08-11 12:07:21 -04:00
imageInfo.format = imageFormat;
imageInfo.tiling = imageTiling;
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
imageInfo.usage = imageUsage;
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
if (info.type == GFXTextureType::Cubemap || info.type == GFXTextureType::CubemapArray)
imageInfo.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
2020-08-11 12:07:21 -04:00
vkCreateImage(device, &imageInfo, nullptr, &texture->handle);
name_object(device, VK_OBJECT_TYPE_IMAGE, (uint64_t)texture->handle, info.label);
2020-08-11 12:07:21 -04:00
texture->width = info.width;
texture->height = info.height;
texture->format = imageFormat;
2021-02-03 09:04:30 -05:00
texture->aspect = imageAspect;
texture->layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
2020-08-11 12:07:21 -04:00
// allocate memory
VkMemoryRequirements memRequirements;
vkGetImageMemoryRequirements(device, texture->handle, &memRequirements);
VkMemoryAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
vkAllocateMemory(device, &allocInfo, nullptr, &texture->memory);
vkBindImageMemory(device, texture->handle, texture->memory, 0);
2021-02-15 17:59:54 -05:00
VkImageSubresourceRange range = {};
range.baseMipLevel = 0;
range.levelCount = info.mip_count;
range.baseArrayLayer = 0;
range.layerCount = info.array_length;
if(info.type == GFXTextureType::Cubemap || info.type == GFXTextureType::CubemapArray)
range.layerCount *= 6;
texture->range = range;
transitionImageLayout(texture->handle, imageFormat, imageAspect, range, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
2020-08-11 12:07:21 -04:00
// create image view
VkImageViewCreateInfo viewInfo = {};
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
viewInfo.image = texture->handle;
switch (info.type) {
case GFXTextureType::Single2D:
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
break;
case GFXTextureType::Array2D:
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
break;
case GFXTextureType::Cubemap:
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
break;
case GFXTextureType::CubemapArray:
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_CUBE_ARRAY;
break;
}
2020-08-11 12:07:21 -04:00
viewInfo.format = imageFormat;
2021-02-15 17:59:54 -05:00
viewInfo.subresourceRange = range;
2020-08-11 12:07:21 -04:00
viewInfo.subresourceRange.aspectMask = imageAspect;
vkCreateImageView(device, &viewInfo, nullptr, &texture->view);
const VkSamplerAddressMode samplerMode = toSamplerMode(info.samplingMode);
2020-08-11 12:07:21 -04:00
// create sampler
VkSamplerCreateInfo samplerInfo = {};
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
2021-02-15 19:01:17 -05:00
samplerInfo.magFilter = VK_FILTER_NEAREST;
samplerInfo.minFilter = VK_FILTER_NEAREST;
2020-08-11 12:07:21 -04:00
samplerInfo.addressModeU = samplerMode;
samplerInfo.addressModeV = samplerMode;
samplerInfo.addressModeW = samplerMode;
samplerInfo.anisotropyEnable = VK_TRUE;
samplerInfo.maxAnisotropy = 16;
samplerInfo.compareEnable = info.compare_enabled;
samplerInfo.borderColor = toBorderColor(info.border_color);
samplerInfo.compareOp = toCompareFunc(info.compare_function);
2020-08-11 12:07:21 -04:00
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
vkCreateSampler(device, &samplerInfo, nullptr, &texture->sampler);
return texture;
}
void GFXVulkan::copy_texture(GFXTexture* texture, void* data, GFXSize size) {
GFXVulkanTexture* vulkanTexture = (GFXVulkanTexture*)texture;
vkDeviceWaitIdle(device);
// create staging buffer
VkBuffer stagingBuffer;
VkDeviceMemory stagingBufferMemory;
VkBufferCreateInfo bufferInfo = {};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.size = size;
bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
vkCreateBuffer(device, &bufferInfo, nullptr, &stagingBuffer);
// allocate staging memory
VkMemoryRequirements memRequirements;
vkGetBufferMemoryRequirements(device, stagingBuffer, &memRequirements);
VkMemoryAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
vkAllocateMemory(device, &allocInfo, nullptr, &stagingBufferMemory);
vkBindBufferMemory(device, stagingBuffer, stagingBufferMemory, 0);
// copy to staging buffer
void* mapped_data;
vkMapMemory(device, stagingBufferMemory, 0, size, 0, &mapped_data);
memcpy(mapped_data, data, size);
vkUnmapMemory(device, stagingBufferMemory);
// copy staging buffer to image
VkCommandBuffer commandBuffer = beginSingleTimeCommands();
2021-02-15 17:59:54 -05:00
VkImageSubresourceRange range = {};
range.baseMipLevel = 0;
range.levelCount = 1;
range.baseArrayLayer = 0;
range.layerCount = 1;
inlineTransitionImageLayout(commandBuffer, vulkanTexture->handle, vulkanTexture->format, vulkanTexture->aspect, range, VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
2021-02-03 09:04:30 -05:00
2020-08-11 12:07:21 -04:00
VkBufferImageCopy region = {};
2021-02-03 09:04:30 -05:00
region.imageSubresource.aspectMask = vulkanTexture->aspect;
2020-08-11 12:07:21 -04:00
region.imageSubresource.mipLevel = 0;
region.imageSubresource.baseArrayLayer = 0;
region.imageSubresource.layerCount = 1;
region.imageExtent = {
(uint32_t)vulkanTexture->width,
(uint32_t)vulkanTexture->height,
1
};
vkCmdCopyBufferToImage(commandBuffer, stagingBuffer, vulkanTexture->handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
2021-02-15 17:59:54 -05:00
inlineTransitionImageLayout(commandBuffer, vulkanTexture->handle, vulkanTexture->format, vulkanTexture->aspect, range, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
2020-08-11 12:07:21 -04:00
2021-02-03 09:04:30 -05:00
endSingleTimeCommands(commandBuffer);
2020-08-11 12:07:21 -04:00
}
void GFXVulkan::copy_texture(GFXTexture* from, GFXTexture* to) {
prism::log::error(System::GFX, "Copy Texture->Texture unimplemented!");
2020-08-11 12:07:21 -04:00
}
void GFXVulkan::copy_texture(GFXTexture* from, GFXBuffer* to) {
2021-02-03 09:04:30 -05:00
GFXVulkanTexture* vulkanTexture = (GFXVulkanTexture*)from;
GFXVulkanBuffer* vulkanBuffer = (GFXVulkanBuffer*)to;
VkCommandBuffer commandBuffer = beginSingleTimeCommands();
VkBufferImageCopy region = {};
region.imageSubresource.aspectMask = vulkanTexture->aspect;
region.imageSubresource.mipLevel = 0;
region.imageSubresource.baseArrayLayer = 0;
region.imageSubresource.layerCount = 1;
region.imageExtent = {
(uint32_t)vulkanTexture->width,
(uint32_t)vulkanTexture->height,
1
};
vkCmdCopyImageToBuffer(commandBuffer, vulkanTexture->handle, vulkanTexture->layout, vulkanBuffer->handle, 1, &region);
2021-02-03 09:04:30 -05:00
endSingleTimeCommands(commandBuffer);
2020-08-11 12:07:21 -04:00
}
GFXSampler* GFXVulkan::create_sampler(const GFXSamplerCreateInfo& info) {
GFXVulkanSampler* sampler = new GFXVulkanSampler();
const VkSamplerAddressMode samplerMode = toSamplerMode(info.samplingMode);
VkSamplerCreateInfo samplerInfo = {};
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
samplerInfo.magFilter = toFilter(info.mag_filter);
samplerInfo.minFilter = toFilter(info.min_filter);
samplerInfo.addressModeU = samplerMode;
samplerInfo.addressModeV = samplerMode;
samplerInfo.addressModeW = samplerMode;
samplerInfo.anisotropyEnable = VK_TRUE;
samplerInfo.maxAnisotropy = 16;
samplerInfo.borderColor = toBorderColor(info.border_color);
samplerInfo.compareEnable = info.compare_enabled;
samplerInfo.compareOp = toCompareFunc(info.compare_function);
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
vkCreateSampler(device, &samplerInfo, nullptr, &sampler->sampler);
return sampler;
}
2020-08-11 12:07:21 -04:00
GFXFramebuffer* GFXVulkan::create_framebuffer(const GFXFramebufferCreateInfo& info) {
GFXVulkanFramebuffer* framebuffer = new GFXVulkanFramebuffer();
vkDeviceWaitIdle(device);
GFXVulkanRenderPass* renderPass = (GFXVulkanRenderPass*)info.render_pass;
std::vector<VkImageView> attachments;
for (auto& attachment : info.attachments) {
GFXVulkanTexture* texture = (GFXVulkanTexture*)attachment;
attachments.push_back(texture->view);
2021-02-03 09:04:30 -05:00
VkImageLayout expectedLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
if (texture->aspect & VK_IMAGE_ASPECT_DEPTH_BIT)
expectedLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
2021-02-15 17:59:54 -05:00
VkImageSubresourceRange range = {};
range.baseMipLevel = 0;
range.levelCount = 1;
range.baseArrayLayer = 0;
range.layerCount = 1;
transitionImageLayout(texture->handle, texture->format, texture->aspect, range, texture->layout, expectedLayout);
2020-08-11 12:07:21 -04:00
}
VkFramebufferCreateInfo framebufferInfo = {};
framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
framebufferInfo.renderPass = renderPass->handle;
framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
framebufferInfo.pAttachments = attachments.data();
framebufferInfo.width = ((GFXVulkanTexture*)info.attachments[0])->width; // FIXME: eww!!
framebufferInfo.height = ((GFXVulkanTexture*)info.attachments[0])->height;
framebufferInfo.layers = 1;
vkCreateFramebuffer(device, &framebufferInfo, nullptr, &framebuffer->handle);
name_object(device, VK_OBJECT_TYPE_FRAMEBUFFER, (uint64_t)framebuffer->handle, info.label);
2020-08-11 12:07:21 -04:00
framebuffer->width = ((GFXVulkanTexture*)info.attachments[0])->width;
framebuffer->height = ((GFXVulkanTexture*)info.attachments[0])->height;
return framebuffer;
}
GFXRenderPass* GFXVulkan::create_render_pass(const GFXRenderPassCreateInfo& info) {
GFXVulkanRenderPass* renderPass = new GFXVulkanRenderPass();
vkDeviceWaitIdle(device);
std::vector<VkAttachmentDescription> descriptions;
std::vector<VkAttachmentReference> references;
bool hasDepthAttachment = false;
VkAttachmentDescription depthAttachment;
VkAttachmentReference depthAttachmentRef;
for (int i = 0; i < info.attachments.size(); i++) {
bool isDepthAttachment = false;
if (info.attachments[i] == GFXPixelFormat::DEPTH_32F)
isDepthAttachment = true;
VkAttachmentDescription attachment = {};
attachment.format = toVkFormat(info.attachments[i]);
attachment.samples = VK_SAMPLE_COUNT_1_BIT;
attachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
attachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
2021-02-15 19:01:17 -05:00
if(info.will_use_in_shader) {
attachment.finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
} else {
if (isDepthAttachment)
attachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL;
else
attachment.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
}
2020-08-11 12:07:21 -04:00
VkAttachmentReference attachmentRef = {};
attachmentRef.attachment = i;
2021-02-03 09:04:30 -05:00
if (isDepthAttachment)
attachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
else
attachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
2020-08-11 12:07:21 -04:00
if (isDepthAttachment) {
hasDepthAttachment = true;
depthAttachment = attachment;
depthAttachmentRef = attachmentRef;
renderPass->depth_attachment = i;
}
else {
descriptions.push_back(attachment);
references.push_back(attachmentRef);
}
}
VkSubpassDescription subpass = {};
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.colorAttachmentCount = static_cast<uint32_t>(references.size());
subpass.pColorAttachments = references.data();
if(hasDepthAttachment) {
subpass.pDepthStencilAttachment = &depthAttachmentRef;
descriptions.push_back(depthAttachment);
}
VkRenderPassCreateInfo renderPassInfo = {};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
renderPassInfo.attachmentCount = static_cast<uint32_t>(descriptions.size());
renderPassInfo.pAttachments = descriptions.data();
renderPassInfo.subpassCount = 1;
renderPassInfo.pSubpasses = &subpass;
vkCreateRenderPass(device, &renderPassInfo, nullptr, &renderPass->handle);
name_object(device, VK_OBJECT_TYPE_RENDER_PASS, (uint64_t)renderPass->handle, info.label);
2020-08-11 12:07:21 -04:00
renderPass->numAttachments = static_cast<unsigned int>(descriptions.size());
renderPass->hasDepthAttachment = hasDepthAttachment;
return renderPass;
}
GFXPipeline* GFXVulkan::create_graphics_pipeline(const GFXGraphicsPipelineCreateInfo& info) {
GFXVulkanPipeline* pipeline = new GFXVulkanPipeline();
vkDeviceWaitIdle(device);
VkShaderModule vertex_module = VK_NULL_HANDLE, fragment_module = VK_NULL_HANDLE;
2020-08-11 12:07:21 -04:00
const bool has_vertex_stage = !info.shaders.vertex_src.empty();
const bool has_fragment_stage = !info.shaders.fragment_src.empty();
2020-08-11 12:07:21 -04:00
std::vector<VkPipelineShaderStageCreateInfo> shaderStages;
if (has_vertex_stage) {
const bool vertex_use_shader_source = !info.shaders.vertex_src.is_path();
if (vertex_use_shader_source) {
2021-02-15 15:06:13 -05:00
auto vertex_shader_vector = info.shaders.vertex_src.as_bytecode();
vertex_module = createShaderModule(vertex_shader_vector.data(), vertex_shader_vector.size() * sizeof(uint32_t));
}
else {
auto vertex_shader = file::open(file::internal_domain / (info.shaders.vertex_src.as_path().string() + ".spv"), true);
vertex_shader->read_all();
vertex_module = createShaderModule(vertex_shader->cast_data<uint32_t>(), vertex_shader->size());
}
2020-08-11 12:07:21 -04:00
if(!vertex_use_shader_source)
name_object(device, VK_OBJECT_TYPE_SHADER_MODULE, (uint64_t)vertex_module, info.shaders.vertex_src.as_path().string());
VkPipelineShaderStageCreateInfo vertShaderStageInfo = {};
vertShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
vertShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
vertShaderStageInfo.module = vertex_module;
vertShaderStageInfo.pName = "main";
shaderStages.push_back(vertShaderStageInfo);
}
if (has_fragment_stage) {
const bool fragment_use_shader_source = !info.shaders.fragment_src.is_path();
2020-08-11 12:07:21 -04:00
if (fragment_use_shader_source) {
2021-02-15 15:06:13 -05:00
auto fragment_shader_vector = info.shaders.fragment_src.as_bytecode();
2020-08-11 12:07:21 -04:00
fragment_module = createShaderModule(fragment_shader_vector.data(), fragment_shader_vector.size() * sizeof(uint32_t));
}
else {
auto fragment_shader = file::open(file::internal_domain / (info.shaders.fragment_src.as_path().string() + ".spv"), true);
fragment_shader->read_all();
fragment_module = createShaderModule(fragment_shader->cast_data<uint32_t>(), fragment_shader->size());
}
if(!fragment_use_shader_source)
name_object(device, VK_OBJECT_TYPE_SHADER_MODULE, (uint64_t)fragment_module, info.shaders.fragment_src.as_path().string());
2020-08-11 12:07:21 -04:00
VkPipelineShaderStageCreateInfo fragShaderStageInfo = {};
fragShaderStageInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
fragShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
fragShaderStageInfo.module = fragment_module;
fragShaderStageInfo.pName = "main";
shaderStages.push_back(fragShaderStageInfo);
}
2020-08-11 12:07:21 -04:00
// setup vertex inputs/bindings
std::vector<VkVertexInputBindingDescription> inputs;
for (auto& binding : info.vertex_input.inputs) {
VkVertexInputBindingDescription b;
b.binding = binding.location;
b.stride = binding.stride;
b.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
inputs.push_back(b);
}
std::vector<VkVertexInputAttributeDescription> attributes;
for (auto& attribute : info.vertex_input.attributes) {
VkVertexInputAttributeDescription description;
description.binding = attribute.binding;
description.format = toVkFormat(attribute.format);
description.location = attribute.location;
description.offset = attribute.offset;
attributes.push_back(description);
}
// fixed functions
VkPipelineVertexInputStateCreateInfo vertexInputInfo = {};
vertexInputInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
vertexInputInfo.pVertexBindingDescriptions = inputs.data();
vertexInputInfo.vertexBindingDescriptionCount = static_cast<uint32_t>(inputs.size());
vertexInputInfo.pVertexAttributeDescriptions = attributes.data();
vertexInputInfo.vertexAttributeDescriptionCount = static_cast<uint32_t>(attributes.size());
VkPipelineInputAssemblyStateCreateInfo inputAssembly = {};
inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
if (info.rasterization.primitive_type == GFXPrimitiveType::TriangleStrip)
inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
VkPipelineViewportStateCreateInfo viewportState = {};
viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
viewportState.viewportCount = 1;
viewportState.scissorCount = 1;
VkPipelineRasterizationStateCreateInfo rasterizer = {};
rasterizer.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
rasterizer.polygonMode = VK_POLYGON_MODE_FILL;
rasterizer.lineWidth = 1.0f;
switch (info.rasterization.culling_mode) {
case GFXCullingMode::Backface:
rasterizer.cullMode = VK_CULL_MODE_BACK_BIT;
break;
case GFXCullingMode::Frontface:
rasterizer.cullMode = VK_CULL_MODE_FRONT_BIT;
break;
case GFXCullingMode::None:
rasterizer.cullMode = VK_CULL_MODE_NONE;
}
switch (info.rasterization.winding_mode) {
case GFXWindingMode::Clockwise:
rasterizer.frontFace = VK_FRONT_FACE_CLOCKWISE;
break;
case GFXWindingMode::CounterClockwise:
rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
break;
}
2020-08-11 12:07:21 -04:00
if (info.rasterization.polygon_type == GFXPolygonType::Line)
rasterizer.polygonMode = VK_POLYGON_MODE_LINE;
VkPipelineMultisampleStateCreateInfo multisampling = {};
multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
VkPipelineColorBlendAttachmentState colorBlendAttachment = {};
colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
if (info.blending.enable_blending) {
colorBlendAttachment.blendEnable = VK_TRUE;
colorBlendAttachment.srcColorBlendFactor = toVkFactor(info.blending.src_rgb);
colorBlendAttachment.dstColorBlendFactor = toVkFactor(info.blending.dst_rgb);
colorBlendAttachment.srcAlphaBlendFactor = toVkFactor(info.blending.src_alpha);
colorBlendAttachment.dstAlphaBlendFactor = toVkFactor(info.blending.dst_alpha);
}
VkPipelineColorBlendStateCreateInfo colorBlending = {};
colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
colorBlending.attachmentCount = 1;
colorBlending.pAttachments = &colorBlendAttachment;
VkPipelineDepthStencilStateCreateInfo depthStencil = {};
depthStencil.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
if (info.depth.depth_mode != GFXDepthMode::None) {
depthStencil.depthTestEnable = VK_TRUE;
depthStencil.depthWriteEnable = VK_TRUE;
2021-02-07 16:34:22 -05:00
switch (info.depth.depth_mode) {
case GFXDepthMode::Less:
depthStencil.depthCompareOp = VK_COMPARE_OP_LESS;
break;
case GFXDepthMode::LessOrEqual:
depthStencil.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
break;
case GFXDepthMode::Greater:
depthStencil.depthCompareOp = VK_COMPARE_OP_GREATER;
break;
}
2020-08-11 12:07:21 -04:00
}
std::vector<VkDynamicState> dynamicStates = {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR, VK_DYNAMIC_STATE_DEPTH_BIAS};
2020-08-11 12:07:21 -04:00
VkPipelineDynamicStateCreateInfo dynamicState = {};
dynamicState.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
dynamicState.dynamicStateCount = static_cast<uint32_t>(dynamicStates.size());
dynamicState.pDynamicStates = dynamicStates.data();
// create push constants
std::vector<VkPushConstantRange> pushConstants;
for (auto& pushConstant : info.shader_input.push_constants) {
VkPushConstantRange range;
range.offset = pushConstant.offset;
range.size = pushConstant.size;
range.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
2020-08-11 12:07:21 -04:00
pushConstants.push_back(range);
}
// create descriptor layout
std::vector<VkDescriptorSetLayoutBinding> layoutBindings;
for (auto& binding : info.shader_input.bindings) {
// ignore push constants
if (binding.type == GFXBindingType::PushConstant)
continue;
VkDescriptorType descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
switch (binding.type) {
case GFXBindingType::StorageBuffer:
descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
break;
case GFXBindingType::Texture:
descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
break;
case GFXBindingType::StorageImage:
{
descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
pipeline->bindings_marked_as_storage_images.push_back(binding.binding);
}
break;
case GFXBindingType::SampledImage:
{
descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
pipeline->bindings_marked_as_sampled_images.push_back(binding.binding);
}
break;
case GFXBindingType::Sampler:
descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER;
break;
2020-08-11 12:07:21 -04:00
}
VkDescriptorSetLayoutBinding layoutBinding = {};
layoutBinding.binding = binding.binding;
layoutBinding.descriptorType = descriptorType;
layoutBinding.descriptorCount = 1;
layoutBinding.stageFlags = VK_SHADER_STAGE_ALL;
layoutBindings.push_back(layoutBinding);
}
VkDescriptorSetLayoutCreateInfo layoutCreateInfo = {};
layoutCreateInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
layoutCreateInfo.bindingCount = static_cast<uint32_t>(layoutBindings.size());
layoutCreateInfo.pBindings = layoutBindings.data();
vkCreateDescriptorSetLayout(device, &layoutCreateInfo, nullptr, &pipeline->descriptorLayout);
// create layout
VkPipelineLayoutCreateInfo pipelineLayoutInfo = {};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutInfo.pushConstantRangeCount = static_cast<uint32_t>(pushConstants.size());
pipelineLayoutInfo.pPushConstantRanges = pushConstants.data();
pipelineLayoutInfo.pSetLayouts = &pipeline->descriptorLayout;
pipelineLayoutInfo.setLayoutCount = 1;
vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipeline->layout);
// create pipeline
VkGraphicsPipelineCreateInfo pipelineInfo = {};
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipelineInfo.stageCount = static_cast<uint32_t>(shaderStages.size());
pipelineInfo.pStages = shaderStages.data();
2020-08-11 12:07:21 -04:00
pipelineInfo.pVertexInputState = &vertexInputInfo;
pipelineInfo.pInputAssemblyState = &inputAssembly;
pipelineInfo.pViewportState = &viewportState;
pipelineInfo.pRasterizationState = &rasterizer;
pipelineInfo.pMultisampleState = &multisampling;
pipelineInfo.pColorBlendState = &colorBlending;
pipelineInfo.pDynamicState = &dynamicState;
pipelineInfo.layout = pipeline->layout;
if (info.render_pass != nullptr) {
pipelineInfo.renderPass = ((GFXVulkanRenderPass*)info.render_pass)->handle;
}
else {
pipelineInfo.renderPass = native_surfaces[0]->swapchainRenderPass;
2020-08-11 12:07:21 -04:00
}
if (info.render_pass != nullptr && ((GFXVulkanRenderPass*)info.render_pass)->hasDepthAttachment)
pipelineInfo.pDepthStencilState = &depthStencil;
vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &pipeline->handle);
pipeline->label = info.label;
2020-08-11 12:07:21 -04:00
name_object(device, VK_OBJECT_TYPE_PIPELINE, (uint64_t)pipeline->handle, pipeline->label);
name_object(device, VK_OBJECT_TYPE_PIPELINE_LAYOUT, (uint64_t)pipeline->layout, pipeline->label);
2020-08-11 12:07:21 -04:00
return pipeline;
}
GFXSize GFXVulkan::get_alignment(GFXSize size) {
VkPhysicalDeviceProperties properties;
vkGetPhysicalDeviceProperties(physicalDevice, &properties);
VkDeviceSize minUboAlignment = properties.limits.minStorageBufferOffsetAlignment;
return (size + minUboAlignment / 2) & ~int(minUboAlignment - 1);
}
GFXCommandBuffer* GFXVulkan::acquire_command_buffer(bool for_presentation_use) {
GFXVulkanCommandBuffer* cmdbuf = new GFXVulkanCommandBuffer();
if(!for_presentation_use) {
VkCommandBufferAllocateInfo info = {};
info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
info.commandPool = commandPool;
info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
info.commandBufferCount = 1;
vkAllocateCommandBuffers(device, &info, &cmdbuf->handle);
}
return cmdbuf;
}
void GFXVulkan::submit(GFXCommandBuffer* command_buffer, const int identifier) {
NativeSurface* current_surface = nullptr;
for(auto surface : native_surfaces) {
if(surface->identifier == identifier)
current_surface = surface;
}
uint32_t imageIndex = 0;
if(identifier != -1 && current_surface != nullptr) {
vkWaitForFences(device, 1, &current_surface->inFlightFences[current_surface->currentFrame], VK_TRUE, std::numeric_limits<uint64_t>::max());
2020-08-11 12:07:21 -04:00
VkResult result = vkAcquireNextImageKHR(device, current_surface->swapchain, std::numeric_limits<uint64_t>::max(), current_surface->imageAvailableSemaphores[current_surface->currentFrame], VK_NULL_HANDLE, &imageIndex);
if (result == VK_ERROR_OUT_OF_DATE_KHR)
return;
}
VkCommandBuffer cmd = VK_NULL_HANDLE;
2020-08-11 12:07:21 -04:00
GFXVulkanCommandBuffer* cmdbuf = (GFXVulkanCommandBuffer*)command_buffer;
if(cmdbuf->handle != VK_NULL_HANDLE)
cmd = cmdbuf->handle;
else if(current_surface != nullptr)
cmd = commandBuffers[current_surface-> currentFrame];
if(cmd == nullptr)
return;
2021-02-04 09:19:17 -05:00
2020-08-11 12:07:21 -04:00
VkCommandBufferBeginInfo beginInfo = {};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
2021-02-04 09:19:17 -05:00
vkBeginCommandBuffer(cmd, &beginInfo);
2020-08-11 12:07:21 -04:00
VkRenderPass currentRenderPass = VK_NULL_HANDLE;
GFXVulkanPipeline* currentPipeline = nullptr;
uint64_t lastDescriptorHash = 0;
const auto try_bind_descriptor = [cmd, this, &currentPipeline, &lastDescriptorHash]() -> bool {
if(currentPipeline == nullptr)
return false;
if (lastDescriptorHash != getDescriptorHash(currentPipeline)) {
if (!currentPipeline->cachedDescriptorSets.count(getDescriptorHash(currentPipeline)))
cacheDescriptorState(currentPipeline, currentPipeline->descriptorLayout);
auto& descriptor_set = currentPipeline->cachedDescriptorSets[getDescriptorHash(currentPipeline)];
if (descriptor_set == VK_NULL_HANDLE)
return false;
vkCmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, currentPipeline->layout, 0, 1, &descriptor_set, 0, nullptr);
lastDescriptorHash = getDescriptorHash(currentPipeline);
}
return true;
};
for (const auto& command : command_buffer->commands) {
2020-08-11 12:07:21 -04:00
switch (command.type) {
2021-02-15 17:59:54 -05:00
case GFXCommandType::SetRenderPass:
{
// end the previous render pass
if (currentRenderPass != VK_NULL_HANDLE) {
vkCmdEndRenderPass(cmd);
}
GFXVulkanRenderPass* renderPass = (GFXVulkanRenderPass*)command.data.set_render_pass.render_pass;
GFXVulkanFramebuffer* framebuffer = (GFXVulkanFramebuffer*)command.data.set_render_pass.framebuffer;
if (renderPass != nullptr) {
currentRenderPass = renderPass->handle;
}
else {
currentRenderPass = current_surface->swapchainRenderPass;
2021-02-15 17:59:54 -05:00
}
VkRenderPassBeginInfo renderPassInfo = {};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO;
renderPassInfo.renderPass = currentRenderPass;
if (framebuffer != nullptr) {
renderPassInfo.framebuffer = framebuffer->handle;
2020-08-11 12:07:21 -04:00
VkViewport viewport = {};
2021-02-15 17:59:54 -05:00
viewport.y = static_cast<float>(framebuffer->height);
2020-08-11 12:07:21 -04:00
viewport.width = static_cast<float>(framebuffer->width);
2020-09-23 12:21:15 -04:00
viewport.height = -static_cast<float>(framebuffer->height);
2020-08-11 12:07:21 -04:00
viewport.maxDepth = 1.0f;
2021-02-15 17:59:54 -05:00
2021-02-04 09:19:17 -05:00
vkCmdSetViewport(cmd, 0, 1, &viewport);
2021-02-15 17:59:54 -05:00
2020-08-11 12:07:21 -04:00
VkRect2D scissor = {};
scissor.extent.width = framebuffer->width;
scissor.extent.height = framebuffer->height;
2021-02-15 17:59:54 -05:00
2021-02-04 09:19:17 -05:00
vkCmdSetScissor(cmd, 0, 1, &scissor);
2021-02-15 17:59:54 -05:00
}
else if(current_surface != nullptr) {
renderPassInfo.framebuffer = current_surface->swapchainFramebuffers[imageIndex];
2021-02-15 17:59:54 -05:00
2020-08-11 12:07:21 -04:00
VkViewport viewport = {};
viewport.y = static_cast<float>(current_surface->surfaceHeight);
viewport.width = static_cast<float>(current_surface->surfaceWidth);
viewport.height = -static_cast<float>(current_surface->surfaceHeight);
2020-08-11 12:07:21 -04:00
viewport.maxDepth = 1.0f;
2021-02-15 17:59:54 -05:00
2021-02-04 09:19:17 -05:00
vkCmdSetViewport(cmd, 0, 1, &viewport);
2021-02-15 17:59:54 -05:00
2020-08-11 12:07:21 -04:00
VkRect2D scissor = {};
scissor.extent.width = current_surface->surfaceWidth;
scissor.extent.height = current_surface->surfaceHeight;
2021-02-15 17:59:54 -05:00
2021-02-04 09:19:17 -05:00
vkCmdSetScissor(cmd, 0, 1, &scissor);
2021-02-15 17:59:54 -05:00
}
renderPassInfo.renderArea.offset = { command.data.set_render_pass.render_area.offset.x, command.data.set_render_pass.render_area.offset.y };
2020-08-13 07:48:50 -04:00
renderPassInfo.renderArea.extent = { command.data.set_render_pass.render_area.extent.width, command.data.set_render_pass.render_area.extent.height };
2021-02-15 17:59:54 -05:00
std::vector<VkClearValue> clearColors;
if (renderPass != nullptr) {
clearColors.resize(renderPass->numAttachments);
}
else {
clearColors.resize(1);
}
clearColors[0].color.float32[0] = command.data.set_render_pass.clear_color.r;
clearColors[0].color.float32[1] = command.data.set_render_pass.clear_color.g;
clearColors[0].color.float32[2] = command.data.set_render_pass.clear_color.b;
clearColors[0].color.float32[3] = command.data.set_render_pass.clear_color.a;
2020-08-11 12:07:21 -04:00
if(renderPass != nullptr) {
if(renderPass->depth_attachment != -1)
clearColors[renderPass->depth_attachment].depthStencil.depth = 1.0f;
}
2021-02-15 17:59:54 -05:00
renderPassInfo.clearValueCount = static_cast<uint32_t>(clearColors.size());
renderPassInfo.pClearValues = clearColors.data();
vkCmdBeginRenderPass(cmd, &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
currentPipeline = nullptr;
}
break;
2020-09-23 08:44:14 -04:00
case GFXCommandType::SetGraphicsPipeline:
2020-08-11 12:07:21 -04:00
{
2020-09-23 08:44:14 -04:00
currentPipeline = (GFXVulkanPipeline*)command.data.set_graphics_pipeline.pipeline;
2021-02-04 09:19:17 -05:00
vkCmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, currentPipeline->handle);
2020-08-11 12:07:21 -04:00
resetDescriptorState();
lastDescriptorHash = 0;
}
break;
case GFXCommandType::SetVertexBuffer:
{
VkBuffer buffer = ((GFXVulkanBuffer*)command.data.set_vertex_buffer.buffer)->handle;
2020-08-11 12:07:21 -04:00
VkDeviceSize offset = command.data.set_vertex_buffer.offset;
2021-02-04 09:19:17 -05:00
vkCmdBindVertexBuffers(cmd, command.data.set_vertex_buffer.index, 1, &buffer, &offset);
2020-08-11 12:07:21 -04:00
}
break;
case GFXCommandType::SetIndexBuffer:
{
VkIndexType indexType = VK_INDEX_TYPE_UINT32;
if (command.data.set_index_buffer.index_type == IndexType::UINT16)
indexType = VK_INDEX_TYPE_UINT16;
vkCmdBindIndexBuffer(cmd, ((GFXVulkanBuffer*)command.data.set_index_buffer.buffer)->handle, 0, indexType);
2020-08-11 12:07:21 -04:00
}
break;
case GFXCommandType::SetPushConstant:
{
if(currentPipeline != nullptr)
2021-02-04 09:19:17 -05:00
vkCmdPushConstants(cmd, currentPipeline->layout, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, command.data.set_push_constant.size, command.data.set_push_constant.bytes.data());
2020-08-11 12:07:21 -04:00
}
break;
case GFXCommandType::BindShaderBuffer:
{
BoundShaderBuffer bsb;
bsb.buffer = command.data.bind_shader_buffer.buffer;
bsb.offset = command.data.bind_shader_buffer.offset;
bsb.size = command.data.bind_shader_buffer.size;
boundShaderBuffers[command.data.bind_shader_buffer.index] = bsb;
}
break;
case GFXCommandType::BindTexture:
{
boundTextures[command.data.bind_texture.index] = command.data.bind_texture.texture;
}
break;
case GFXCommandType::BindSampler:
{
boundSamplers[command.data.bind_sampler.index] = command.data.bind_sampler.sampler;
}
break;
2020-08-11 12:07:21 -04:00
case GFXCommandType::Draw:
{
if(try_bind_descriptor())
vkCmdDraw(cmd, command.data.draw.vertex_count, command.data.draw.instance_count, command.data.draw.vertex_offset, command.data.draw.base_instance);
2020-08-11 12:07:21 -04:00
}
break;
case GFXCommandType::DrawIndexed:
{
if(try_bind_descriptor())
vkCmdDrawIndexed(cmd, command.data.draw_indexed.index_count, 1, command.data.draw_indexed.first_index, command.data.draw_indexed.vertex_offset, 0);
2020-08-11 12:07:21 -04:00
}
break;
case GFXCommandType::SetDepthBias:
{
vkCmdSetDepthBias(cmd, command.data.set_depth_bias.constant, command.data.set_depth_bias.clamp, command.data.set_depth_bias.slope_factor);
}
break;
2021-02-15 17:59:54 -05:00
case GFXCommandType::CopyTexture:
{
GFXVulkanTexture* src = (GFXVulkanTexture*)command.data.copy_texture.src;
GFXVulkanTexture* dst = (GFXVulkanTexture*)command.data.copy_texture.dst;
inlineTransitionImageLayout(cmd, src->handle, src->format, src->aspect, src->range, src->layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
inlineTransitionImageLayout(cmd, dst->handle, dst->format, dst->aspect, dst->range, dst->layout, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
const int slice_offset = command.data.copy_texture.to_slice + command.data.copy_texture.to_layer * 6;
VkImageCopy region = {};
region.extent = {static_cast<uint32_t>(command.data.copy_texture.width), static_cast<uint32_t>(command.data.copy_texture.height), 1};
region.dstSubresource.baseArrayLayer = slice_offset;
region.dstSubresource.mipLevel = command.data.copy_texture.to_level;
region.srcSubresource.aspectMask = src->aspect;
region.dstSubresource.aspectMask = dst->aspect;
region.srcSubresource.layerCount = 1;
region.dstSubresource.layerCount = 1;
vkCmdCopyImage(cmd, src->handle, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, dst->handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
inlineTransitionImageLayout(cmd, src->handle, src->format, src->aspect, src->range, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, src->layout);
inlineTransitionImageLayout(cmd, dst->handle, dst->format, dst->aspect, dst->range, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, dst->layout);
}
break;
case GFXCommandType::EndRenderPass:
{
if(currentRenderPass != nullptr) {
vkCmdEndRenderPass(cmd);
currentRenderPass = nullptr;
}
2020-08-11 12:07:21 -04:00
}
2021-02-15 17:59:54 -05:00
break;
}
2020-08-11 12:07:21 -04:00
}
// end the last render pass
if (currentRenderPass != VK_NULL_HANDLE) {
2021-02-04 09:19:17 -05:00
vkCmdEndRenderPass(cmd);
2020-08-11 12:07:21 -04:00
}
2021-02-04 09:19:17 -05:00
vkEndCommandBuffer(cmd);
2020-08-11 12:07:21 -04:00
if(identifier == -1) {
VkSubmitInfo submitInfo = {};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &cmd;
2020-08-11 12:07:21 -04:00
vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
} else if(current_surface != nullptr) {
// submit
VkSubmitInfo submitInfo = {};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
VkSemaphore waitSemaphores[] = { current_surface->imageAvailableSemaphores[current_surface->currentFrame] };
VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
submitInfo.waitSemaphoreCount = 1;
submitInfo.pWaitSemaphores = waitSemaphores;
submitInfo.pWaitDstStageMask = waitStages;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &cmd;
VkSemaphore signalSemaphores[] = { current_surface->renderFinishedSemaphores[current_surface->currentFrame] };
submitInfo.signalSemaphoreCount = 1;
submitInfo.pSignalSemaphores = signalSemaphores;
vkResetFences(device, 1, &current_surface->inFlightFences[current_surface->currentFrame]);
if(vkQueueSubmit(graphicsQueue, 1, &submitInfo, current_surface->inFlightFences[current_surface->currentFrame]) != VK_SUCCESS)
return;
// present
VkPresentInfoKHR presentInfo = {};
presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
presentInfo.waitSemaphoreCount = 1;
presentInfo.pWaitSemaphores = signalSemaphores;
VkSwapchainKHR swapChains[] = { current_surface->swapchain };
presentInfo.swapchainCount = 1;
presentInfo.pSwapchains = swapChains;
presentInfo.pImageIndices = &imageIndex;
vkQueuePresentKHR(presentQueue, &presentInfo);
current_surface->currentFrame = (current_surface->currentFrame + 1) % MAX_FRAMES_IN_FLIGHT;
}
2020-08-11 12:07:21 -04:00
}
const char* GFXVulkan::get_name() {
return "Vulkan";
}
VkResult CreateDebugUtilsMessengerEXT(
VkInstance instance,
const VkDebugUtilsMessengerCreateInfoEXT *pCreateInfo,
const VkAllocationCallbacks *pAllocator,
VkDebugUtilsMessengerEXT *pCallback) {
// Note: It seems that static_cast<...> doesn't work. Use the C-style forced cast.
auto func = (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT");
if (func != nullptr) {
return func(instance, pCreateInfo, pAllocator, pCallback);
}
else {
return VK_ERROR_EXTENSION_NOT_PRESENT;
}
}
void GFXVulkan::createInstance(std::vector<const char*> layers, std::vector<const char*> extensions) {
VkDebugUtilsMessengerCreateInfoEXT debugCreateInfo = {};
debugCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
debugCreateInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
2021-02-03 09:04:30 -05:00
debugCreateInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT ;
debugCreateInfo.pfnUserCallback = DebugCallback;
2020-08-11 12:07:21 -04:00
VkApplicationInfo appInfo = {};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = "Prism Engine app";
2020-08-11 12:07:21 -04:00
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
appInfo.pEngineName = "Prism Engine";
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
2020-09-23 09:53:45 -04:00
appInfo.apiVersion = VK_API_VERSION_1_1;
2020-08-11 12:07:21 -04:00
VkInstanceCreateInfo createInfo = {};
createInfo.pNext = &debugCreateInfo;
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pApplicationInfo = &appInfo;
createInfo.ppEnabledLayerNames = layers.data();
createInfo.enabledLayerCount = static_cast<uint32_t>(layers.size());
createInfo.ppEnabledExtensionNames = extensions.data();
createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
vkCreateInstance(&createInfo, nullptr, &instance);
VkDebugUtilsMessengerEXT callback;
CreateDebugUtilsMessengerEXT(instance, &debugCreateInfo, nullptr, &callback);
}
void GFXVulkan::createLogicalDevice(std::vector<const char*> extensions) {
// pick physical device
uint32_t deviceCount = 0;
vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
std::vector<VkPhysicalDevice> devices(deviceCount);
vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
physicalDevice = devices[0];
uint32_t graphicsFamilyIndex = 0, presentFamilyIndex = 0;
// create logical device
uint32_t queueFamilyCount = 0;
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, nullptr);
std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
vkGetPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyCount, queueFamilies.data());
int i = 0;
for (const auto& queueFamily : queueFamilies) {
if (queueFamily.queueCount > 0 && queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
graphicsFamilyIndex = i;
}
i++;
}
std::vector<VkDeviceQueueCreateInfo> queueCreateInfos;
if (graphicsFamilyIndex == presentFamilyIndex) {
VkDeviceQueueCreateInfo queueCreateInfo = {};
queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
queueCreateInfo.queueFamilyIndex = graphicsFamilyIndex;
queueCreateInfo.queueCount = 1;
float queuePriority = 1.0f;
queueCreateInfo.pQueuePriorities = &queuePriority;
queueCreateInfos.push_back(queueCreateInfo);
}
VkDeviceCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
createInfo.pQueueCreateInfos = queueCreateInfos.data();
createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());
createInfo.ppEnabledExtensionNames = extensions.data();
createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
VkPhysicalDeviceFeatures enabledFeatures = {};
//enabledFeatures.vertexPipelineStoresAndAtomics = true;
enabledFeatures.fragmentStoresAndAtomics = true;
enabledFeatures.samplerAnisotropy = true;
enabledFeatures.fillModeNonSolid = true;
enabledFeatures.imageCubeArray = true;
2020-08-11 12:07:21 -04:00
createInfo.pEnabledFeatures = &enabledFeatures;
vkCreateDevice(physicalDevice, &createInfo, nullptr, &device);
// get queues
vkGetDeviceQueue(device, graphicsFamilyIndex, 0, &graphicsQueue);
vkGetDeviceQueue(device, presentFamilyIndex, 0, &presentQueue);
// command pool
VkCommandPoolCreateInfo poolInfo = {};
poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
poolInfo.queueFamilyIndex = graphicsFamilyIndex;
poolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool);
}
void GFXVulkan::createSwapchain(NativeSurface* native_surface, VkSwapchainKHR oldSwapchain) {
2020-08-11 12:07:21 -04:00
#ifdef PLATFORM_WINDOWS_OLD
2020-08-11 12:07:21 -04:00
// create win32 surface
if(native_surface->surface == VK_NULL_HANDLE)
2020-08-11 12:07:21 -04:00
{
VkWin32SurfaceCreateInfoKHR createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
createInfo.hwnd = (HWND)native_surface->windowNativeHandle;
2020-08-11 12:07:21 -04:00
createInfo.hinstance = GetModuleHandle(nullptr);
vkCreateWin32SurfaceKHR(instance, &createInfo, nullptr, &native_surface->surface);
2020-08-11 12:07:21 -04:00
}
#else
if(native_surface->surface == VK_NULL_HANDLE) {
2021-03-01 14:40:02 -05:00
native_surface->surface = (VkSurfaceKHR)platform::create_native_surface(native_surface->identifier, (void*)instance);
2020-08-11 12:07:21 -04:00
}
#endif
// TODO: fix this pls
VkBool32 supported;
vkGetPhysicalDeviceSurfaceSupportKHR(physicalDevice, 0, native_surface->surface, &supported);
2020-08-11 12:07:21 -04:00
// query swapchain support
VkSurfaceCapabilitiesKHR capabilities;
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physicalDevice, native_surface->surface, &capabilities);
2020-08-11 12:07:21 -04:00
std::vector<VkSurfaceFormatKHR> formats;
uint32_t formatCount;
vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, native_surface->surface, &formatCount, nullptr);
2020-08-11 12:07:21 -04:00
formats.resize(formatCount);
vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, native_surface->surface, &formatCount, formats.data());
2020-08-11 12:07:21 -04:00
std::vector<VkPresentModeKHR> presentModes;
uint32_t presentModeCount;
vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, native_surface->surface, &presentModeCount, nullptr);
2020-08-11 12:07:21 -04:00
presentModes.resize(presentModeCount);
vkGetPhysicalDeviceSurfacePresentModesKHR(physicalDevice, native_surface->surface, &presentModeCount, presentModes.data());
2020-08-11 12:07:21 -04:00
// choosing swapchain features
VkSurfaceFormatKHR swapchainSurfaceFormat = formats[0];
for (const auto& availableFormat : formats) {
if (availableFormat.format == VK_FORMAT_B8G8R8A8_UNORM && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {
swapchainSurfaceFormat = availableFormat;
}
}
VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;
for (const auto& availablePresentMode : presentModes) {
if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
swapchainPresentMode = availablePresentMode;
}
}
uint32_t imageCount = capabilities.minImageCount + 1;
if (capabilities.maxImageCount > 0 && imageCount > capabilities.maxImageCount) {
imageCount = capabilities.maxImageCount;
}
// create swapchain
VkSwapchainCreateInfoKHR createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
createInfo.surface = native_surface->surface;
2020-08-11 12:07:21 -04:00
createInfo.minImageCount = imageCount;
createInfo.imageFormat = swapchainSurfaceFormat.format;
createInfo.imageColorSpace = swapchainSurfaceFormat.colorSpace;
createInfo.imageExtent.width = native_surface->surfaceWidth;
createInfo.imageExtent.height = native_surface->surfaceHeight;
2020-08-11 12:07:21 -04:00
createInfo.imageArrayLayers = 1;
createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
createInfo.preTransform = capabilities.currentTransform;
createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
createInfo.presentMode = swapchainPresentMode;
createInfo.clipped = VK_TRUE;
createInfo.oldSwapchain = oldSwapchain;
vkCreateSwapchainKHR(device, &createInfo, nullptr, &native_surface->swapchain);
2020-08-11 12:07:21 -04:00
if (oldSwapchain != VK_NULL_HANDLE) {
vkDestroySwapchainKHR(device, oldSwapchain, nullptr);
}
native_surface->swapchainExtent.width = native_surface->surfaceWidth;
native_surface->swapchainExtent.height = native_surface->surfaceHeight;
2020-08-11 12:07:21 -04:00
// get swapchain images
vkGetSwapchainImagesKHR(device, native_surface->swapchain, &imageCount, nullptr);
native_surface->swapchainImages.resize(imageCount);
vkGetSwapchainImagesKHR(device, native_surface->swapchain, &imageCount, native_surface->swapchainImages.data());
2020-08-11 12:07:21 -04:00
// create swapchain image views
native_surface->swapchainImageViews.resize(native_surface->swapchainImages.size());
2020-08-11 12:07:21 -04:00
for (size_t i = 0; i < native_surface->swapchainImages.size(); i++) {
2020-08-11 12:07:21 -04:00
VkImageViewCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
createInfo.image = native_surface->swapchainImages[i];
2020-08-11 12:07:21 -04:00
createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
createInfo.format = swapchainSurfaceFormat.format;
createInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
createInfo.subresourceRange.baseMipLevel = 0;
createInfo.subresourceRange.levelCount = 1;
createInfo.subresourceRange.baseArrayLayer = 0;
createInfo.subresourceRange.layerCount = 1;
vkCreateImageView(device, &createInfo, nullptr, &native_surface->swapchainImageViews[i]);
2020-08-11 12:07:21 -04:00
}
// create render pass
VkAttachmentDescription colorAttachment = {};
colorAttachment.format = swapchainSurfaceFormat.format;
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
VkAttachmentReference colorAttachmentRef = {};
colorAttachmentRef.attachment = 0;
colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkSubpassDependency dependency = {};
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
dependency.dstSubpass = 0;
dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
dependency.srcAccessMask = 0;
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
dependency.dependencyFlags = 0;
2020-08-11 12:07:21 -04:00
VkSubpassDescription subpass = {};
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.colorAttachmentCount = 1;
subpass.pColorAttachments = &colorAttachmentRef;
VkRenderPassCreateInfo renderPassInfo = {};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
renderPassInfo.attachmentCount = 1;
renderPassInfo.pAttachments = &colorAttachment;
renderPassInfo.subpassCount = 1;
renderPassInfo.pSubpasses = &subpass;
renderPassInfo.dependencyCount = 1;
renderPassInfo.pDependencies = &dependency;
2020-08-11 12:07:21 -04:00
vkCreateRenderPass(device, &renderPassInfo, nullptr, &native_surface->swapchainRenderPass);
2020-08-11 12:07:21 -04:00
// create swapchain framebuffers
native_surface->swapchainFramebuffers.resize(native_surface->swapchainImageViews.size());
2020-08-11 12:07:21 -04:00
for (size_t i = 0; i < native_surface->swapchainImageViews.size(); i++) {
2020-08-11 12:07:21 -04:00
VkImageView attachments[] = {
native_surface->swapchainImageViews[i]
2020-08-11 12:07:21 -04:00
};
VkFramebufferCreateInfo framebufferInfo = {};
framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
framebufferInfo.renderPass = native_surface->swapchainRenderPass;
2020-08-11 12:07:21 -04:00
framebufferInfo.attachmentCount = 1;
framebufferInfo.pAttachments = attachments;
framebufferInfo.width = native_surface->surfaceWidth;
framebufferInfo.height = native_surface->surfaceHeight;
2020-08-11 12:07:21 -04:00
framebufferInfo.layers = 1;
vkCreateFramebuffer(device, &framebufferInfo, nullptr, &native_surface->swapchainFramebuffers[i]);
2020-08-11 12:07:21 -04:00
}
// allocate command buffers
2021-02-04 09:19:17 -05:00
commandBuffers.resize(MAX_FRAMES_IN_FLIGHT);
2020-08-11 12:07:21 -04:00
VkCommandBufferAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.commandPool = commandPool;
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocInfo.commandBufferCount = (uint32_t)commandBuffers.size();
vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data());
2021-02-04 09:01:28 -05:00
for (auto [i, cmdbuf] : utility::enumerate(commandBuffers))
name_object(device, VK_OBJECT_TYPE_COMMAND_BUFFER, (uint64_t)cmdbuf, ("main cmd buf " + std::to_string(i)).c_str());
2020-08-11 12:07:21 -04:00
}
void GFXVulkan::createDescriptorPool() {
const std::array<VkDescriptorPoolSize, 2> poolSizes = { {
{VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 5000},
{VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 5000}
2020-08-11 12:07:21 -04:00
}};
VkDescriptorPoolCreateInfo poolInfo = {};
poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
poolInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
poolInfo.poolSizeCount = static_cast<uint32_t>(poolSizes.size());
poolInfo.pPoolSizes = poolSizes.data();
poolInfo.maxSets = 5000;
2020-08-11 12:07:21 -04:00
vkCreateDescriptorPool(device, &poolInfo, nullptr, &descriptorPool);
}
void GFXVulkan::createSyncPrimitives(NativeSurface* native_surface) {
native_surface->imageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
native_surface->renderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
native_surface->inFlightFences.resize(MAX_FRAMES_IN_FLIGHT);
2020-08-11 12:07:21 -04:00
VkSemaphoreCreateInfo semaphoreInfo = {};
semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
VkFenceCreateInfo fenceCreateInfo = {};
fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
fenceCreateInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
vkCreateSemaphore(device, &semaphoreInfo, nullptr, &native_surface->imageAvailableSemaphores[i]);
vkCreateSemaphore(device, &semaphoreInfo, nullptr, &native_surface->renderFinishedSemaphores[i]);
vkCreateFence(device, &fenceCreateInfo, nullptr, &native_surface->inFlightFences[i]);
2020-08-11 12:07:21 -04:00
}
}
void GFXVulkan::resetDescriptorState() {
for (auto& buffer : boundShaderBuffers)
buffer.buffer = nullptr;
for (auto& texture : boundTextures)
texture = nullptr;
for (auto& sampler : boundSamplers)
sampler = nullptr;
2020-08-11 12:07:21 -04:00
}
void GFXVulkan::cacheDescriptorState(GFXVulkanPipeline* pipeline, VkDescriptorSetLayout layout) {
uint64_t hash = getDescriptorHash(pipeline);
prism::log::debug(System::GFX, "Caching descriptor hash {}", std::to_string(hash));
2020-08-11 12:07:21 -04:00
vkDeviceWaitIdle(device);
// create set object
VkDescriptorSet descriptorSet;
VkDescriptorSetAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
allocInfo.descriptorPool = descriptorPool;
allocInfo.descriptorSetCount = 1;
allocInfo.pSetLayouts = &layout;
VkResult error = vkAllocateDescriptorSets(device, &allocInfo, &descriptorSet);
if(error == VK_ERROR_OUT_OF_POOL_MEMORY) {
prism::log::error(System::GFX, "ERROR: COULD NOT CACHE BECAUSE OUT OF DESCRIPTOR SETS.");
}
if(error != VK_SUCCESS)
return;
2020-08-11 12:07:21 -04:00
name_object(device, VK_OBJECT_TYPE_DESCRIPTOR_SET, (uint64_t)descriptorSet, pipeline->label);
if (descriptorSet == VK_NULL_HANDLE)
return;
2020-08-11 12:07:21 -04:00
// update set
int i = 0;
for (auto& buffer : boundShaderBuffers) {
if (buffer.buffer != nullptr) {
GFXVulkanBuffer* vulkanBuffer = (GFXVulkanBuffer*)buffer.buffer;
VkDescriptorBufferInfo bufferInfo = {};
bufferInfo.buffer = vulkanBuffer->handle; // will this break?
bufferInfo.offset = buffer.offset;
bufferInfo.range = buffer.size;
VkWriteDescriptorSet descriptorWrite = {};
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrite.dstSet = descriptorSet;
descriptorWrite.dstBinding = i;
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
descriptorWrite.descriptorCount = 1;
descriptorWrite.pBufferInfo = &bufferInfo;
vkUpdateDescriptorSets(device, 1, &descriptorWrite, 0, nullptr);
}
i++;
}
i = 0;
for (auto& texture : boundTextures) {
if (texture == nullptr) {
2020-08-11 12:07:21 -04:00
i++;
continue;
}
GFXVulkanTexture* vulkanTexture = (GFXVulkanTexture*)texture;
VkDescriptorImageInfo imageInfo = {};
2021-02-15 19:01:17 -05:00
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
imageInfo.imageView = vulkanTexture->view;
imageInfo.sampler = vulkanTexture->sampler;
2020-08-11 12:07:21 -04:00
2021-02-03 09:04:30 -05:00
//if (imageInfo.imageLayout != vulkanTexture->layout) {
GFXVulkanPipeline::ExpectedTransisition trans;
trans.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
trans.newLayout = imageInfo.imageLayout;
pipeline->expectedTransisitions[vulkanTexture] = trans;
//}
2020-08-11 12:07:21 -04:00
VkWriteDescriptorSet descriptorWrite = {};
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrite.dstSet = descriptorSet;
descriptorWrite.dstBinding = i;
descriptorWrite.descriptorCount = 1;
descriptorWrite.pImageInfo = &imageInfo;
if (utility::contains(pipeline->bindings_marked_as_storage_images, i)) {
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE;
} else if (utility::contains(pipeline->bindings_marked_as_sampled_images, i)) {
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE;
} else {
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
}
2020-08-11 12:07:21 -04:00
vkUpdateDescriptorSets(device, 1, &descriptorWrite, 0, nullptr);
i++;
}
i = 0;
for (auto& sampler : boundSamplers) {
if (sampler == nullptr) {
2020-08-11 12:07:21 -04:00
i++;
continue;
}
GFXVulkanSampler* vulkanSampler = (GFXVulkanSampler*)sampler;
2020-08-11 12:07:21 -04:00
VkDescriptorImageInfo imageInfo = {};
imageInfo.sampler = vulkanSampler->sampler;
2020-08-11 12:07:21 -04:00
VkWriteDescriptorSet descriptorWrite = {};
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrite.dstSet = descriptorSet;
descriptorWrite.dstBinding = i;
descriptorWrite.descriptorCount = 1;
descriptorWrite.pImageInfo = &imageInfo;
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER;
2020-08-11 12:07:21 -04:00
vkUpdateDescriptorSets(device, 1, &descriptorWrite, 0, nullptr);
i++;
}
pipeline->cachedDescriptorSets[hash] = descriptorSet;
}
uint64_t GFXVulkan::getDescriptorHash(GFXVulkanPipeline* pipeline) {
uint64_t hash = 0;
hash += (int64_t)pipeline;
int i = 0;
2020-08-11 12:07:21 -04:00
for (auto& buffer : boundShaderBuffers) {
if (buffer.buffer != nullptr) {
hash += (uint64_t)buffer.buffer * (i + 1);
}
2020-08-11 12:07:21 -04:00
}
i = 0;
2020-08-11 12:07:21 -04:00
for (auto& texture : boundTextures) {
if (texture != nullptr) {
hash += (uint64_t)texture * (i + 1);
}
}
return hash;
}
uint32_t GFXVulkan::findMemoryType(uint32_t typeFilter, VkMemoryPropertyFlags properties) {
VkPhysicalDeviceMemoryProperties memProperties;
vkGetPhysicalDeviceMemoryProperties(physicalDevice, &memProperties);
for (uint32_t i = 0; i < memProperties.memoryTypeCount; i++) {
if ((typeFilter & (1 << i)) && (memProperties.memoryTypes[i].propertyFlags & properties) == properties) {
return i;
}
}
return -1;
}
2021-02-15 17:59:54 -05:00
void GFXVulkan::transitionImageLayout(VkImage image, VkFormat format, VkImageAspectFlags aspect, VkImageSubresourceRange range, VkImageLayout oldLayout, VkImageLayout newLayout) {
2020-08-11 12:07:21 -04:00
VkCommandBuffer commandBuffer = beginSingleTimeCommands();
2021-02-15 17:59:54 -05:00
inlineTransitionImageLayout(commandBuffer, image, format, aspect, range, oldLayout, newLayout);
2021-02-03 09:04:30 -05:00
endSingleTimeCommands(commandBuffer);
}
2021-02-15 17:59:54 -05:00
void GFXVulkan::inlineTransitionImageLayout(VkCommandBuffer commandBuffer, VkImage image, VkFormat format, VkImageAspectFlags aspect, VkImageSubresourceRange range, VkImageLayout oldLayout, VkImageLayout newLayout) {
2021-02-03 09:04:30 -05:00
VkImageMemoryBarrier barrier{};
2020-08-11 12:07:21 -04:00
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
barrier.oldLayout = oldLayout;
barrier.newLayout = newLayout;
barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
barrier.image = image;
2021-02-15 17:59:54 -05:00
barrier.subresourceRange = range;
2020-08-11 12:07:21 -04:00
barrier.subresourceRange.aspectMask = aspect;
2021-02-03 09:04:30 -05:00
VkPipelineStageFlags sourceStage;
VkPipelineStageFlags destinationStage;
if (oldLayout == VK_IMAGE_LAYOUT_UNDEFINED && newLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
barrier.srcAccessMask = 0;
barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
destinationStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
}
else if (oldLayout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && newLayout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) {
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
sourceStage = VK_PIPELINE_STAGE_TRANSFER_BIT;
destinationStage = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
}
else {
barrier.srcAccessMask = VK_ACCESS_MEMORY_READ_BIT;
barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
2021-02-03 09:04:30 -05:00
sourceStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
destinationStage = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT;
2021-02-03 09:04:30 -05:00
}
2020-08-11 12:07:21 -04:00
vkCmdPipelineBarrier(
commandBuffer,
2021-02-03 09:04:30 -05:00
sourceStage, destinationStage,
2020-08-11 12:07:21 -04:00
0,
0, nullptr,
0, nullptr,
1, &barrier
);
}
VkShaderModule GFXVulkan::createShaderModule(const uint32_t* code, const int length) {
2020-08-11 12:07:21 -04:00
VkShaderModuleCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
createInfo.codeSize = length;
createInfo.pCode = reinterpret_cast<const uint32_t*>(code);
VkShaderModule shaderModule;
vkCreateShaderModule(device, &createInfo, nullptr, &shaderModule);
return shaderModule;
}
VkCommandBuffer GFXVulkan::beginSingleTimeCommands() {
VkCommandBufferAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
allocInfo.commandPool = commandPool;
allocInfo.commandBufferCount = 1;
VkCommandBuffer commandBuffer;
vkAllocateCommandBuffers(device, &allocInfo, &commandBuffer);
VkCommandBufferBeginInfo beginInfo = {};
beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
vkBeginCommandBuffer(commandBuffer, &beginInfo);
return commandBuffer;
}
void GFXVulkan::endSingleTimeCommands(VkCommandBuffer commandBuffer) {
vkEndCommandBuffer(commandBuffer);
VkSubmitInfo submitInfo = {};
submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
submitInfo.commandBufferCount = 1;
submitInfo.pCommandBuffers = &commandBuffer;
vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE);
vkQueueWaitIdle(graphicsQueue);
vkFreeCommandBuffers(device, commandPool, 1, &commandBuffer);
}