Skip to content

Commit

Permalink
Somewhat ugly way to call Streamline only for the main window
Browse files Browse the repository at this point in the history
  • Loading branch information
sultim-t authored and sultim-t-nv committed Aug 9, 2023
1 parent 68580c7 commit 5803442
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Source/DebugWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ RTGL1::DebugWindows::DebugWindows( VkInstance _ins
VK_CHECKERROR( r );

customSwapchain = std::make_unique< Swapchain >(
device, customSurface, _physDevice, _cmdManager );
device, customSurface, _physDevice, _cmdManager, std::shared_ptr< Streamline >{} );

renderPass = CreateRenderPass( device, customSwapchain->GetSurfaceFormat() );

Expand Down
152 changes: 106 additions & 46 deletions Source/Swapchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,37 +47,42 @@ bool operator!=( const VkExtent2D& a, const VkExtent2D& b )
RTGL1::Swapchain::Swapchain( VkDevice _device,
VkSurfaceKHR _surface,
VkPhysicalDevice _physDevice,
std::shared_ptr< CommandBufferManager > _cmdManager )
: device( _device )
, surface( _surface )
, physDevice( _physDevice )
, cmdManager( std::move( _cmdManager ) )
std::shared_ptr< CommandBufferManager > _cmdManager,
const std::shared_ptr< Streamline >& _slForFrameGeneration )
: device{ _device }
, surface{ _surface }
, physDevice{ _physDevice }
, cmdManager{ std::move( _cmdManager ) }
, sl{ _slForFrameGeneration }
, surfaceFormat{}
, presentModeVsync( VK_PRESENT_MODE_FIFO_KHR )
, presentModeImmediate( VK_PRESENT_MODE_FIFO_KHR )
, requestedVsync( true )
, presentModeVsync{ VK_PRESENT_MODE_FIFO_KHR }
, presentModeImmediate{ VK_PRESENT_MODE_FIFO_KHR }
, requestedVsync{ true }
, surfaceExtent{ UINT32_MAX, UINT32_MAX }
, isVsync( true )
, swapchain( VK_NULL_HANDLE )
, currentSwapchainIndex( UINT32_MAX )
, isVsync{ true }
, swapchain{ VK_NULL_HANDLE }
, currentSwapchainIndex{ UINT32_MAX }
{
VkResult r;

// find surface format
{
uint32_t formatCount = 0;
r = vkGetPhysicalDeviceSurfaceFormatsKHR( physDevice, surface, &formatCount, nullptr );
VK_CHECKERROR( r );

std::vector< VkSurfaceFormatKHR > surfaceFormats;
surfaceFormats.resize( formatCount );

r = vkGetPhysicalDeviceSurfaceFormatsKHR(
physDevice, surface, &formatCount, surfaceFormats.data() );
VK_CHECKERROR( r );
auto surfaceFormats = std::vector< VkSurfaceFormatKHR >{};
{
uint32_t formatCount = 0;
r = vkGetPhysicalDeviceSurfaceFormatsKHR( physDevice, surface, &formatCount, nullptr );
VK_CHECKERROR( r );

surfaceFormats.resize( formatCount );
r = vkGetPhysicalDeviceSurfaceFormatsKHR(
physDevice, surface, &formatCount, surfaceFormats.data() );
VK_CHECKERROR( r );
}

std::vector< VkFormat > acceptFormats = { VK_FORMAT_R8G8B8A8_SRGB,
VK_FORMAT_B8G8R8A8_SRGB };
VkFormat acceptFormats[] = {
VK_FORMAT_R8G8B8A8_SRGB,
VK_FORMAT_B8G8R8A8_SRGB,
};

for( VkFormat f : acceptFormats )
{
Expand All @@ -98,15 +103,18 @@ RTGL1::Swapchain::Swapchain( VkDevice _device,

// find present modes
{
uint32_t presentModeCount = 0;
r = vkGetPhysicalDeviceSurfacePresentModesKHR(
physDevice, surface, &presentModeCount, nullptr );
VK_CHECKERROR( r );

std::vector< VkPresentModeKHR > presentModes( presentModeCount );
r = vkGetPhysicalDeviceSurfacePresentModesKHR(
physDevice, surface, &presentModeCount, presentModes.data() );
VK_CHECKERROR( r );
auto presentModes = std::vector< VkPresentModeKHR >{};
{
uint32_t presentModeCount = 0;
r = vkGetPhysicalDeviceSurfacePresentModesKHR(
physDevice, surface, &presentModeCount, nullptr );
VK_CHECKERROR( r );

presentModes.resize( presentModeCount );
r = vkGetPhysicalDeviceSurfacePresentModesKHR(
physDevice, surface, &presentModeCount, presentModes.data() );
VK_CHECKERROR( r );
}

// try to find mailbox / fifo-relaxed
for( auto p : presentModes )
Expand All @@ -128,7 +136,7 @@ bool RTGL1::Swapchain::IsExtentOptimal() const
{
VkSurfaceCapabilitiesKHR surfCapabilities;

VkResult r =
VkResult r =
vkGetPhysicalDeviceSurfaceCapabilitiesKHR( physDevice, surface, &surfCapabilities );

if( r == VK_ERROR_SURFACE_LOST_KHR )
Expand All @@ -146,7 +154,7 @@ VkExtent2D RTGL1::Swapchain::GetOptimalExtent() const
{
VkSurfaceCapabilitiesKHR surfCapabilities;

VkResult r =
VkResult r =
vkGetPhysicalDeviceSurfaceCapabilitiesKHR( physDevice, surface, &surfCapabilities );
VK_CHECKERROR( r );

Expand Down Expand Up @@ -184,12 +192,26 @@ void RTGL1::Swapchain::AcquireImage( VkSemaphore imageAvailableSemaphore )

while( true )
{
VkResult r = svkAcquireNextImageKHR( device,
swapchain,
UINT64_MAX,
imageAvailableSemaphore,
VK_NULL_HANDLE,
&currentSwapchainIndex );
VkResult r;

if( !sl.expired() )
{
r = svkAcquireNextImageKHR( device,
swapchain,
UINT64_MAX,
imageAvailableSemaphore,
VK_NULL_HANDLE,
&currentSwapchainIndex );
}
else
{
r = vkAcquireNextImageKHR( device,
swapchain,
UINT64_MAX,
imageAvailableSemaphore,
VK_NULL_HANDLE,
&currentSwapchainIndex );
}

if( r == VK_SUCCESS )
{
Expand All @@ -214,7 +236,7 @@ void RTGL1::Swapchain::BlitForPresent( VkCommandBuffer cmd,
VkImageLayout srcImageLayout )
{
// if source has almost the same size as the surface, then use nearest blit
if( std::abs( int( srcImageWidth ) - int( surfaceExtent.width ) ) < 8 &&
if( std::abs( int( srcImageWidth ) - int( surfaceExtent.width ) ) < 8 &&
std::abs( int( srcImageHeight ) - int( surfaceExtent.height ) ) < 8 )
{
filter = VK_FILTER_NEAREST;
Expand Down Expand Up @@ -495,21 +517,51 @@ void RTGL1::Swapchain::Create( uint32_t newWidth,
.oldSwapchain = oldSwapchain,
};

r = svkCreateSwapchainKHR( device, &swapchainInfo, nullptr, &swapchain );
if( !sl.expired() )
{
r = svkCreateSwapchainKHR( device, &swapchainInfo, nullptr, &swapchain );
}
else
{
r = vkCreateSwapchainKHR( device, &swapchainInfo, nullptr, &swapchain );
}
VK_CHECKERROR( r );


if( oldSwapchain != VK_NULL_HANDLE )
{
svkDestroySwapchainKHR( device, oldSwapchain, nullptr );
if( !sl.expired() )
{
svkDestroySwapchainKHR( device, oldSwapchain, nullptr );
}
else
{
vkDestroySwapchainKHR( device, oldSwapchain, nullptr );
}
}

r = svkGetSwapchainImagesKHR( device, swapchain, &imageCount, nullptr );

if( !sl.expired() )
{
r = svkGetSwapchainImagesKHR( device, swapchain, &imageCount, nullptr );
}
else
{
r = vkGetSwapchainImagesKHR( device, swapchain, &imageCount, nullptr );
}
VK_CHECKERROR( r );

swapchainImages.resize( imageCount );
swapchainViews.resize( imageCount );

r = svkGetSwapchainImagesKHR( device, swapchain, &imageCount, swapchainImages.data() );
if( !sl.expired() )
{
r = svkGetSwapchainImagesKHR( device, swapchain, &imageCount, swapchainImages.data() );
}
else
{
r = vkGetSwapchainImagesKHR( device, swapchain, &imageCount, swapchainImages.data() );
}
VK_CHECKERROR( r );

for( uint32_t i = 0; i < imageCount; i++ )
Expand Down Expand Up @@ -556,7 +608,15 @@ void RTGL1::Swapchain::Create( uint32_t newWidth,
void RTGL1::Swapchain::Destroy()
{
VkSwapchainKHR old = DestroyWithoutSwapchain();
svkDestroySwapchainKHR( device, old, nullptr );

if( !sl.expired() )
{
svkDestroySwapchainKHR( device, old, nullptr );
}
else
{
vkDestroySwapchainKHR( device, old, nullptr );
}
}

VkSwapchainKHR RTGL1::Swapchain::DestroyWithoutSwapchain()
Expand Down
6 changes: 5 additions & 1 deletion Source/Swapchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@
namespace RTGL1
{

struct Streamline;

class Swapchain
{
public:
Swapchain( VkDevice device,
VkSurfaceKHR surface,
VkPhysicalDevice physDevice,
std::shared_ptr< CommandBufferManager > cmdManager );
std::shared_ptr< CommandBufferManager > cmdManager,
const std::shared_ptr< Streamline >& slForFrameGeneration );
~Swapchain();

Swapchain( const Swapchain& other ) = delete;
Expand Down Expand Up @@ -94,6 +97,7 @@ class Swapchain
VkSurfaceKHR surface;
VkPhysicalDevice physDevice;
std::shared_ptr< CommandBufferManager > cmdManager;
std::weak_ptr< Streamline > sl;

VkSurfaceFormatKHR surfaceFormat;
VkPresentModeKHR presentModeVsync;
Expand Down
3 changes: 2 additions & 1 deletion Source/VulkanDevice_Init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ RTGL1::VulkanDevice::VulkanDevice( const RgInstanceCreateInfo* info )
device,
surface,
physDevice->Get(),
cmdManager );
cmdManager,
streamline );

if( libconfig.developerMode )
{
Expand Down

0 comments on commit 5803442

Please sign in to comment.