Commit cd7c146f authored by Nicholas Milef's avatar Nicholas Milef

ENH: added VR rendering to Vulkan, fixed layout transitions

parent 5afd536c
#-----------------------------------------------------------------------------
# Find path
#-----------------------------------------------------------------------------
find_path(openvr_INCLUDE_DIR
NAMES
openvr.h
)
mark_as_advanced(openvr_INCLUDE_DIR)
#-----------------------------------------------------------------------------
# Find library
#-----------------------------------------------------------------------------
find_library(openvr_LIBRARIES
NAMES
openvr_api
)
mark_as_advanced(openvr_LIBRARIES)
message(STATUS "openvr_INCLUDE_DIR : ${openvr_LIBRARIES}")
#-----------------------------------------------------------------------------
# Find package
#-----------------------------------------------------------------------------
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(openvr
REQUIRED_VARS
openvr_INCLUDE_DIR
openvr_LIBRARIES)
#-----------------------------------------------------------------------------
# If missing target, create it
#-----------------------------------------------------------------------------
if(openvr_FOUND AND NOT TARGET openvr)
add_library(openvr INTERFACE IMPORTED)
set_target_properties(openvr PROPERTIES
INTERFACE_LINK_LIBRARIES "${openvr_LIBRARIES}"
INTERFACE_INCLUDE_DIRECTORIES "${openvr_INCLUDE_DIR}"
)
endif()
......@@ -29,6 +29,7 @@ function(CopyAndCompileShaders)
compileShaders(PostProcessing/HDR_tonemap_frag.frag PostProcessing/HDR_tonemap_frag.spv)
compileShaders(PostProcessing/postprocess_vert.vert PostProcessing/postprocess_vert.spv)
compileShaders(PostProcessing/postprocess_frag.frag PostProcessing/postprocess_frag.spv)
compileShaders(PostProcessing/passthrough_frag.frag PostProcessing/passthrough_frag.spv)
compileShaders(PostProcessing/sss_frag.frag PostProcessing/sss_frag.spv)
compileShaders(PostProcessing/lens_distortion_frag.frag PostProcessing/lens_distortion_frag.spv)
compileShaders(PostProcessing/composite_frag.frag PostProcessing/composite_frag.spv)
......
......@@ -249,6 +249,7 @@ endif()
if(${PROJECT_NAME}_ENABLE_VR)
add_definitions( -DiMSTK_ENABLE_VR )
find_package( openvr REQUIRED )
else()
remove_definitions( -DiMSTK_ENABLE_VR )
endif()
......
......@@ -54,6 +54,7 @@ set(VULKAN_H_FILES
VulkanRenderer/imstkVulkanMaterialDelegate.h
VulkanRenderer/imstkVulkanTextureDelegate.h
VulkanRenderer/imstkVulkanMemoryManager.h
VulkanRenderer/imstkVulkanResources.h
VulkanRenderer/imstkVulkanValidation.h
VulkanRenderer/imstkVulkanBuffer.h
VulkanRenderer/imstkVulkanUniformBuffer.h
......@@ -105,6 +106,9 @@ else()
glfw
gli
GUIOverlay)
if (iMSTK_ENABLE_VR)
list(APPEND RENDERING_DEPENDENCIES openvr)
endif()
endif()
include(imstkAddLibrary)
......
......@@ -25,11 +25,18 @@
namespace imstk
{
VulkanPostProcess::VulkanPostProcess(VulkanRenderer * renderer, unsigned int level, bool lastPass)
VulkanPostProcess::VulkanPostProcess(VulkanRenderer * renderer, unsigned int level)
{
m_lastPass = lastPass;
m_downsampleLevels = level;
this->createFramebuffer(renderer, level, lastPass);
auto width = renderer->m_width >> level;
auto height = renderer->m_height >> level;
this->createFramebuffer(renderer, width, height);
}
VulkanPostProcess::VulkanPostProcess(VulkanRenderer * renderer, unsigned int width, unsigned int height)
{
m_downsampleLevels = 0;
this->createFramebuffer(renderer, width, height);
}
void
......@@ -463,8 +470,7 @@ VulkanPostProcess::createRenderPass(VulkanRenderer * renderer)
attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
attachment.finalLayout =
m_lastPass ? VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL : VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
attachment.finalLayout = m_framebuffer->m_colorLayout;
attachments.push_back(attachment);
}
......@@ -481,25 +487,25 @@ VulkanPostProcess::createRenderPass(VulkanRenderer * renderer)
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;
attachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
attachment.initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL;
attachment.finalLayout = m_framebuffer->m_depthLayout;
attachments.push_back(attachment);
}
// Color attachment
VkAttachmentReference colorReference;
colorReference.attachment = 0;
colorReference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
colorReference.layout = m_framebuffer->m_colorLayout;
// Depth attachment
VkAttachmentReference depthReference;
depthReference.attachment = 1;
depthReference.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
depthReference.layout = m_framebuffer->m_depthLayout;
// Normal attachment
VkAttachmentReference normalReference;
normalReference.attachment = 2;
normalReference.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
normalReference.layout = m_framebuffer->m_normalLayout;
// First pass: geometry
m_colorAttachments.push_back(colorReference);
......@@ -557,14 +563,13 @@ VulkanPostProcess::initializeFramebuffer(VulkanRenderer * renderer)
void
VulkanPostProcess::createFramebuffer(VulkanRenderer * renderer,
unsigned int level,
bool lastPass)
unsigned int width,
unsigned int height)
{
m_framebuffer = std::make_shared<VulkanFramebuffer>(
renderer->m_memoryManager,
renderer->m_width >> level,
renderer->m_height >> level,
lastPass);
width,
height);
}
void
......@@ -578,6 +583,70 @@ VulkanPostProcess::addInputImage(
m_layouts.push_back(layout);
}
void
VulkanPostProcess::updateImageLayouts()
{
if (m_framebuffer->m_colorFormat != VK_FORMAT_UNDEFINED)
{
m_framebuffer->m_colorImage->setImageLayout(m_framebuffer->m_colorLayout);
}
if (m_framebuffer->m_depthFormat != VK_FORMAT_UNDEFINED)
{
m_framebuffer->m_depthImage->setImageLayout(m_framebuffer->m_depthLayout);
}
if (m_framebuffer->m_normalFormat != VK_FORMAT_UNDEFINED)
{
m_framebuffer->m_normalImage->setImageLayout(m_framebuffer->m_normalLayout);
}
if (m_framebuffer->m_specularFormat != VK_FORMAT_UNDEFINED)
{
m_framebuffer->m_specularImage->setImageLayout(m_framebuffer->m_specularLayout);
}
}
void
VulkanPostProcess::setAttachmentsToReadLayout(VkCommandBuffer * commandBuffer, uint32_t queueFamily)
{
if (m_framebuffer->m_colorFormat != VK_FORMAT_UNDEFINED)
{
VulkanAttachmentBarriers::changeImageLayout(commandBuffer,
queueFamily,
m_framebuffer->m_colorImage,
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
}
if (m_framebuffer->m_depthFormat != VK_FORMAT_UNDEFINED)
{
VulkanAttachmentBarriers::changeImageLayout(commandBuffer,
queueFamily,
m_framebuffer->m_depthImage,
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL);
}
if (m_framebuffer->m_normalFormat != VK_FORMAT_UNDEFINED)
{
VulkanAttachmentBarriers::changeImageLayout(commandBuffer,
queueFamily,
m_framebuffer->m_normalImage,
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
}
if (m_framebuffer->m_specularFormat != VK_FORMAT_UNDEFINED)
{
VulkanAttachmentBarriers::changeImageLayout(commandBuffer,
queueFamily,
m_framebuffer->m_specularImage,
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
}
}
void
VulkanPostProcess::clear(VkDevice * device)
{
......
......@@ -43,7 +43,8 @@ public:
///
/// \brief Constructor
///
VulkanPostProcess(VulkanRenderer * renderer, unsigned int level = 0, bool lastPass = false);
VulkanPostProcess(VulkanRenderer * renderer, unsigned int level = 0);
VulkanPostProcess(VulkanRenderer * renderer, unsigned int width, unsigned int height);
void addInputImage(
VkSampler * sampler,
......@@ -63,8 +64,8 @@ protected:
void createPipeline(VulkanRenderer * renderer, std::string fragmentSource);
void createRenderPass(VulkanRenderer * renderer);
void createFramebuffer(VulkanRenderer * renderer,
unsigned int level = 0,
bool lastPass = false);
unsigned int width,
unsigned int height);
void createFullscreenQuad(VulkanRenderer * renderer);
......@@ -72,6 +73,15 @@ protected:
std::string = "./Shaders/VulkanShaders/PostProcessing/postprocess_frag.spv");
void initializeFramebuffer(VulkanRenderer * renderer);
///
/// \brief Updates image information to current layout after renderpass
///
void updateImageLayouts();
///
/// \brief Set image attachments to a readable layout
///
void setAttachmentsToReadLayout(VkCommandBuffer * commandBuffer, uint32_t queueFamily);
void createDescriptors(VulkanRenderer * renderer);
void createDescriptorSetLayouts(VulkanRenderer * renderer);
......@@ -101,7 +111,6 @@ protected:
unsigned int m_downsampleLevels = 0;
unsigned int m_outputIndex = 0;
bool m_lastPass = false;
std::vector<VkAttachmentReference> m_colorAttachments;
VkRenderPass m_renderPass;
......
......@@ -35,10 +35,9 @@ VulkanPostProcessingChain::VulkanPostProcessingChain(VulkanRenderer * renderer)
auto sssHorizontalBlurPass = std::make_shared<VulkanPostProcess>(renderer, 0);
sssHorizontalBlurPass->addInputImage(&renderer->m_HDRImageSampler, &renderer->m_HDRImageView[0][0]);
sssHorizontalBlurPass->addInputImage(&renderer->m_HDRImageSampler, &renderer->m_depthImageView[0],
VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL);
sssHorizontalBlurPass->addInputImage(&renderer->m_HDRImageSampler, &renderer->m_depthImageView[0], VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL);
sssHorizontalBlurPass->addInputImage(&renderer->m_HDRImageSampler, &renderer->m_normalImageView);
sssHorizontalBlurPass->m_framebuffer->setColor(&renderer->m_HDRImageView[2][0], VK_FORMAT_R16G16B16A16_SFLOAT);
sssHorizontalBlurPass->m_framebuffer->setColor(renderer->m_HDRImage[2][0], &renderer->m_HDRImageView[2][0], VK_FORMAT_R16G16B16A16_SFLOAT);
sssHorizontalBlurPass->initialize(renderer, "./Shaders/VulkanShaders/PostProcessing/sss_frag.spv");
sssHorizontalBlurPass->m_pushConstantData[0] = 1.0;
sssHorizontalBlurPass->m_pushConstantData[1] = 0.0;
......@@ -57,10 +56,9 @@ VulkanPostProcessingChain::VulkanPostProcessingChain(VulkanRenderer * renderer)
auto sssVerticalBlurPass = std::make_shared<VulkanPostProcess>(renderer, 0);
sssVerticalBlurPass->addInputImage(&renderer->m_HDRImageSampler, &renderer->m_HDRImageView[2][0]);
sssVerticalBlurPass->addInputImage(&renderer->m_HDRImageSampler, &renderer->m_depthImageView[0],
VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL);
sssVerticalBlurPass->addInputImage(&renderer->m_HDRImageSampler, &renderer->m_depthImageView[0], VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL);
sssVerticalBlurPass->addInputImage(&renderer->m_HDRImageSampler, &renderer->m_normalImageView);
sssVerticalBlurPass->m_framebuffer->setColor(&renderer->m_HDRImageView[0][0], VK_FORMAT_R16G16B16A16_SFLOAT);
sssVerticalBlurPass->m_framebuffer->setColor(renderer->m_HDRImage[0][0], &renderer->m_HDRImageView[0][0], VK_FORMAT_R16G16B16A16_SFLOAT);
sssVerticalBlurPass->initialize(renderer, "./Shaders/VulkanShaders/PostProcessing/sss_frag.spv");
sssVerticalBlurPass->m_pushConstantData[0] = 0.0;
sssVerticalBlurPass->m_pushConstantData[1] = 1.0;
......@@ -82,7 +80,7 @@ VulkanPostProcessingChain::VulkanPostProcessingChain(VulkanRenderer * renderer)
auto accumulationCompositePass = std::make_shared<VulkanPostProcess>(renderer);
accumulationCompositePass->addInputImage(&renderer->m_HDRImageSampler, &renderer->m_HDRImageView[0][0]);
accumulationCompositePass->addInputImage(&renderer->m_HDRImageSampler, &renderer->m_HDRImageView[1][0]);
accumulationCompositePass->m_framebuffer->setColor(&renderer->m_HDRImageView[2][0], VK_FORMAT_R16G16B16A16_SFLOAT);
accumulationCompositePass->m_framebuffer->setColor(renderer->m_HDRImage[2][0], &renderer->m_HDRImageView[2][0], VK_FORMAT_R16G16B16A16_SFLOAT);
accumulationCompositePass->initialize(renderer, "./Shaders/VulkanShaders/PostProcessing/composite_frag.spv");
m_postProcesses.push_back(accumulationCompositePass);
......@@ -94,13 +92,15 @@ VulkanPostProcessingChain::VulkanPostProcessingChain(VulkanRenderer * renderer)
auto bloomThresholdPass = std::make_shared<VulkanPostProcess>(renderer, level);
bloomThresholdPass->addInputImage(&renderer->m_HDRImageSampler, &renderer->m_HDRImageView[m_lastOutput][0]);
bloomThresholdPass->m_framebuffer->setColor(&renderer->m_HDRImageView[m_lastInput][level], VK_FORMAT_R16G16B16A16_SFLOAT);
bloomThresholdPass->m_framebuffer->setColor(renderer->m_HDRImage[m_lastInput][level],
&renderer->m_HDRImageView[m_lastInput][level], VK_FORMAT_R16G16B16A16_SFLOAT);
bloomThresholdPass->initialize(renderer, "./Shaders/VulkanShaders/PostProcessing/bloom_threshold_frag.spv");
m_postProcesses.push_back(bloomThresholdPass);
auto bloomHorizontalBlurPass = std::make_shared<VulkanPostProcess>(renderer, level);
bloomHorizontalBlurPass->addInputImage(&renderer->m_HDRImageSampler, &renderer->m_HDRImageView[m_lastInput][level]);
bloomHorizontalBlurPass->m_framebuffer->setColor(&renderer->m_HDRImageView[m_lastOutput][level], VK_FORMAT_R16G16B16A16_SFLOAT);
bloomHorizontalBlurPass->m_framebuffer->setColor(renderer->m_HDRImage[m_lastOutput][level],
&renderer->m_HDRImageView[m_lastOutput][level], VK_FORMAT_R16G16B16A16_SFLOAT);
bloomHorizontalBlurPass->initialize(renderer, "./Shaders/VulkanShaders/PostProcessing/blur_horizontal_frag.spv");
bloomHorizontalBlurPass->m_pushConstantData[0] = std::max(renderer->m_width >> level, 1u);
bloomHorizontalBlurPass->m_pushConstantData[1] = std::max(renderer->m_height >> level, 1u);
......@@ -112,7 +112,8 @@ VulkanPostProcessingChain::VulkanPostProcessingChain(VulkanRenderer * renderer)
auto bloomVerticalBlurPass = std::make_shared<VulkanPostProcess>(renderer, level);
bloomVerticalBlurPass->addInputImage(&renderer->m_HDRImageSampler, &renderer->m_HDRImageView[m_lastOutput][level]);
bloomVerticalBlurPass->m_framebuffer->setColor(&renderer->m_HDRImageView[m_lastInput][level], VK_FORMAT_R16G16B16A16_SFLOAT);
bloomVerticalBlurPass->m_framebuffer->setColor(renderer->m_HDRImage[m_lastInput][level],
&renderer->m_HDRImageView[m_lastInput][level], VK_FORMAT_R16G16B16A16_SFLOAT);
bloomVerticalBlurPass->initialize(renderer, "./Shaders/VulkanShaders/PostProcessing/blur_vertical_frag.spv");
bloomVerticalBlurPass->m_pushConstantData[0] = std::max(renderer->m_width >> level, 1u);
bloomVerticalBlurPass->m_pushConstantData[1] = std::max(renderer->m_height >> level, 1u);
......@@ -125,7 +126,8 @@ VulkanPostProcessingChain::VulkanPostProcessingChain(VulkanRenderer * renderer)
auto bloomCompositePass = std::make_shared<VulkanPostProcess>(renderer);
bloomCompositePass->addInputImage(&renderer->m_HDRImageSampler, &renderer->m_HDRImageView[m_lastOutput][0]);
bloomCompositePass->addInputImage(&renderer->m_HDRImageSampler, &renderer->m_HDRImageView[m_lastInput][level]);
bloomCompositePass->m_framebuffer->setColor(&renderer->m_HDRImageView[m_lastInput][0], VK_FORMAT_R16G16B16A16_SFLOAT);
bloomCompositePass->m_framebuffer->setColor(renderer->m_HDRImage[m_lastInput][0],
&renderer->m_HDRImageView[m_lastInput][0], VK_FORMAT_R16G16B16A16_SFLOAT);
bloomCompositePass->initialize(renderer, "./Shaders/VulkanShaders/PostProcessing/composite_frag.spv");
m_postProcesses.push_back(bloomCompositePass);
this->incrementBufferNumbers();
......@@ -137,7 +139,8 @@ VulkanPostProcessingChain::VulkanPostProcessingChain(VulkanRenderer * renderer)
auto lensDistortionPass = std::make_shared<VulkanPostProcess>(renderer);
lensDistortionPass->addInputImage(&renderer->m_HDRImageSampler, &renderer->m_HDRImageView[m_lastOutput][0]);
lensDistortionPass->addInputImage(&renderer->m_HDRImageSampler, &renderer->m_HDRImageView[m_lastInput][0]);
lensDistortionPass->m_framebuffer->setColor(&renderer->m_HDRImageView[m_lastInput][0], VK_FORMAT_R16G16B16A16_SFLOAT);
lensDistortionPass->m_framebuffer->setColor(renderer->m_HDRImage[m_lastInput][0],
&renderer->m_HDRImageView[m_lastInput][0], VK_FORMAT_R16G16B16A16_SFLOAT);
lensDistortionPass->initialize(renderer, "./Shaders/VulkanShaders/PostProcessing/lens_distortion_frag.spv");
lensDistortionPass->m_pushConstantData[0] = renderer->m_lensDistortionFactor;
m_postProcesses.push_back(lensDistortionPass);
......
#version 450
layout (set = 0, binding = 0) uniform sampler2D inputTexture;
layout (location = 0) out vec4 finalColor;
layout (location = 3) in vertexData{
vec2 uv;
}vertex;
void main(void)
{
ivec2 resolution = textureSize(inputTexture, 0);
ivec2 offset = ivec2(vertex.uv * resolution);
finalColor = texture(inputTexture, vertex.uv);
}
\ No newline at end of file
......@@ -27,13 +27,11 @@ VulkanFramebuffer::VulkanFramebuffer(
VulkanMemoryManager& memoryManager,
uint32_t width,
uint32_t height,
bool lastPass,
VkSampleCountFlagBits samples)
{
m_renderDevice = memoryManager.m_device;
m_width = width;
m_height = height;
m_lastPass = lastPass;
m_samples = samples;
}
......@@ -55,8 +53,7 @@ VulkanFramebuffer::initializeFramebuffer(VkRenderPass * renderPass)
attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
attachment.finalLayout =
m_lastPass ? VK_IMAGE_LAYOUT_PRESENT_SRC_KHR : VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
attachment.finalLayout = m_colorLayout;
m_attachments.push_back(attachment);
framebufferAttachments.push_back(*m_colorImageView);
}
......@@ -73,7 +70,7 @@ VulkanFramebuffer::initializeFramebuffer(VkRenderPass * renderPass)
attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
attachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
attachment.finalLayout = m_depthLayout;
m_attachments.push_back(attachment);
framebufferAttachments.push_back(*m_depthImageView);
}
......@@ -90,7 +87,7 @@ VulkanFramebuffer::initializeFramebuffer(VkRenderPass * renderPass)
attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
attachment.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
attachment.finalLayout = m_normalLayout;
m_attachments.push_back(attachment);
framebufferAttachments.push_back(*m_normalImageView);
}
......@@ -107,7 +104,7 @@ VulkanFramebuffer::initializeFramebuffer(VkRenderPass * renderPass)
attachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
attachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
attachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
attachment.finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
attachment.finalLayout = m_specularLayout;
m_attachments.push_back(attachment);
framebufferAttachments.push_back(*m_specularImageView);
}
......@@ -127,31 +124,51 @@ VulkanFramebuffer::initializeFramebuffer(VkRenderPass * renderPass)
}
void
VulkanFramebuffer::setColor(VkImageView * color, VkFormat format)
VulkanFramebuffer::setColor(VulkanInternalImage * image,
VkImageView * imageView,
VkFormat format,
VkImageLayout layout)
{
m_colorImageView = color;
m_colorImage = image;
m_colorImageView = imageView;
m_colorFormat = format;
m_colorLayout = layout;
}
void
VulkanFramebuffer::setDepth(VkImageView * depth, VkFormat format)
VulkanFramebuffer::setDepth(VulkanInternalImage * image,
VkImageView * imageView,
VkFormat format,
VkImageLayout layout)
{
m_depthImageView = depth;
m_depthImage = image;
m_depthImageView = imageView;
m_depthFormat = format;
m_depthLayout = layout;
}
void
VulkanFramebuffer::setNormal(VkImageView * normal, VkFormat format)
VulkanFramebuffer::setNormal(VulkanInternalImage * image,
VkImageView * imageView,
VkFormat format,
VkImageLayout layout)
{
m_normalImageView = normal;
m_normalImage = image;
m_normalImageView = imageView;
m_normalFormat = format;
m_normalLayout = layout;
}
void
VulkanFramebuffer::setSpecular(VkImageView * specular, VkFormat format)
VulkanFramebuffer::setSpecular(VulkanInternalImage * image,
VkImageView * imageView,
VkFormat format,
VkImageLayout layout)
{
m_specularImageView = specular;
m_specularImage = image;
m_specularImageView = imageView;
m_specularFormat = format;
m_specularLayout = layout;
}
void
......
......@@ -38,16 +38,27 @@ public:
VulkanMemoryManager& memoryManager,
unsigned int width,
unsigned int height,
bool lastPass = false,
VkSampleCountFlagBits samples = VK_SAMPLE_COUNT_1_BIT);
void setColor(VkImageView * imageView, VkFormat format);
void setColor(VulkanInternalImage * image,
VkImageView * imageView,
VkFormat format,
VkImageLayout layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
void setSpecular(VkImageView * imageView, VkFormat format);
void setSpecular(VulkanInternalImage * image,
VkImageView * imageView,
VkFormat format,
VkImageLayout layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
void setDepth(VkImageView * depthImage, VkFormat format);
void setDepth(VulkanInternalImage * image,
VkImageView * imageView,
VkFormat format,
VkImageLayout layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
void setNormal(VkImageView * normalImage, VkFormat format);
void setNormal(VulkanInternalImage * image,
VkImageView * imageView,
VkFormat format,
VkImageLayout layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
void initializeFramebuffer(VkRenderPass * renderPass);
......@@ -75,20 +86,28 @@ private:
VkDevice m_renderDevice;
// Depth buffer
VulkanInternalImage * m_depthImage;
VkImageView * m_depthImageView;
VkFormat m_depthFormat = VK_FORMAT_UNDEFINED;
VkImageLayout m_depthLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
// Normal buffer
VulkanInternalImage * m_normalImage;
VkImageView * m_normalImageView;
VkFormat m_normalFormat = VK_FORMAT_UNDEFINED;
VkImageLayout m_normalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
// Color accumulation buffer
VulkanInternalImage * m_colorImage;
VkImageView * m_colorImageView;
VkFormat m_colorFormat = VK_FORMAT_UNDEFINED;
VkImageLayout m_colorLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
// Specular accumulation buffer
VulkanInternalImage * m_specularImage;
VkImageView * m_specularImageView;
VkFormat m_specularFormat = VK_FORMAT_UNDEFINED;
VkImageLayout m_specularLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
// Attachments
std::vector<VkAttachmentDescription> m_attachments;
......
......@@ -194,6 +194,7 @@ VulkanMemoryManager::requestImage(VkDevice& device,
// Always allocate a new image
auto newImage = new VulkanInternalImage();
vkCreateImage(device, &info, nullptr, newImage->getImage());
newImage->setImageLayout(info.initialLayout);
m_images.push_back(newImage);
......
......@@ -28,178 +28,10 @@
#include "g3log/g3log.hpp"
namespace imstk
{
enum VulkanMemoryType
{
TEXTURE,
STAGING_TEXTURE,
VERTEX,
STAGING_VERTEX,
INDEX,
STAGING_INDEX,
UNIFORM,
STAGING_UNIFORM,
FRAMEBUFFER
};
class VulkanInternalMemory
{
protected:
friend class VulkanMemoryManager;
friend class VulkanInternalBuffer;
friend class VulkanInternalImage;
friend class VulkanInternalResource;
VulkanInternalMemory()
{
m_memory = new VkDeviceMemory();
}
VkDeviceSize m_size;
VkDeviceSize m_capacity;
VkDeviceSize m_lastOffset;
VkDeviceMemory * m_memory;
VkDeviceMemory* getMemory()
{
return m_memory;
}
};
class VulkanInternalResource
{
public:
VulkanInternalMemory* getMemory()
{
return m_memory;
}
void unmapMemory(VkDevice& device)
{
vkUnmapMemory(device, *m_memory->m_memory);
}
protected:
VulkanInternalMemory * m_memory;
VkDeviceSize m_size;
};
class VulkanInternalImage : public VulkanInternalResource
{
public:
VulkanInternalImage()
{
m_image = new VkImage();
};
VkImage* getImage()
{
return m_image;
}
void mapMemory(VkDevice& device, void ** data)
{
vkMapMemory(device, *m_memory->m_memory, m_memoryOffset, m_size, 0, data);
}
protected:
friend class VulkanMemoryManager;
VkImage * m_image;
VkDeviceSize m_memoryOffset;
};
class VulkanInternalBufferGroup : public VulkanInternalResource
{
public:
VulkanInternalBufferGroup(VulkanMemoryType type)
{
m_type = type;
m_buffer = new VkBuffer();
};
#include "imstkVulkanResources.h"
VkBuffer* getBuffer()
{
return m_buffer;
}
VkDeviceSize getSize()
{
return m_size;
}
protected:
friend class VulkanMemoryManager;
friend class VulkanInternalBuffer;
VkBuffer * m_buffer;
char * m_data;
VulkanMemoryType m_type;
bool m_mapped = false;
};
class VulkanInternalBuffer
namespace imstk
{
public:
VulkanInternalBuffer(VulkanInternalBufferGroup * bufferGroup)
{
m_bufferGroup = bufferGroup;
};
VkBuffer* getBuffer()
{
return m_bufferGroup->getBuffer();
}
VulkanInternalMemory* getMemory()
{
return m_bufferGroup->getMemory();
}
void * getMemoryData(VkDevice& device)
{
void * data;
if (!m_bufferGroup->m_mapped)
{
auto result = vkMapMemory(device,