Skip to content

Commit

Permalink
[Vk] move VulkanPhysicalDevices into VulkanInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenegff committed Nov 21, 2024
1 parent 9d5b55a commit 53157eb
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 59 deletions.
4 changes: 4 additions & 0 deletions RenderSystems/Vulkan/include/OgreVulkanDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ namespace Ogre
void initDebugFeatures( PFN_vkDebugReportCallbackEXT callback, void *userdata,
bool hasRenderDocApi );

void initPhysicalDeviceList();

public:
VkInstance mVkInstance;
bool mVkInstanceIsExternal;

FastArray<VulkanPhysicalDevice> mVulkanPhysicalDevices;

PFN_vkCreateDebugReportCallbackEXT CreateDebugReportCallback;
PFN_vkDestroyDebugReportCallbackEXT DestroyDebugReportCallback;
VkDebugReportCallbackEXT mDebugReportCallback;
Expand Down
1 change: 1 addition & 0 deletions RenderSystems/Vulkan/include/OgreVulkanPrerequisites.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ namespace Ogre
class VulkanDynamicBuffer;
struct VulkanGlobalBindingTable;
class VulkanGpuProgramManager;
struct VulkanPhysicalDevice;
class VulkanProgram;
class VulkanProgramFactory;
class VulkanQueue;
Expand Down
7 changes: 2 additions & 5 deletions RenderSystems/Vulkan/include/OgreVulkanRenderSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ namespace Ogre
VkPhysicalDevice physicalDevice;
String title;
};
typedef std::vector<VulkanPhysicalDevice> VulkanPhysicalDeviceList;

/**
Implementation of Vulkan as a rendering system.
Expand All @@ -84,9 +83,8 @@ namespace Ogre
VulkanProgramFactory *mVulkanProgramFactory3;

std::shared_ptr<VulkanInstance> mInstance;
VulkanPhysicalDeviceList mVulkanPhysicalDeviceList;
VulkanSupport *mVulkanSupport;

VulkanSupport *mVulkanSupport;
std::map<IdString, VulkanSupport *> mAvailableVulkanSupports;

// TODO: AutoParamsBuffer probably belongs to MetalDevice (because it's per device?)
Expand Down Expand Up @@ -148,6 +146,7 @@ namespace Ogre
~VulkanRenderSystem() override;

void shutdown() override;
const FastArray<VulkanPhysicalDevice> &getVulkanPhysicalDevices() const;

const String &getName() const override;
const String &getFriendlyName() const override;
Expand Down Expand Up @@ -187,8 +186,6 @@ namespace Ogre

void reinitialise() override;

const VulkanPhysicalDeviceList &getVulkanPhysicalDevices( bool refreshList = false );

Window *_initialise( bool autoCreateWindow,
const String &windowTitle = "OGRE Render Window" ) override;

Expand Down
44 changes: 43 additions & 1 deletion RenderSystems/Vulkan/src/OgreVulkanDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ namespace Ogre
#if OGRE_DEBUG_MODE >= OGRE_DEBUG_MEDIUM
initDebugFeatures( debugCallback, renderSystem, renderSystem->getRenderDocApi() );
#endif
initPhysicalDeviceList();
}
//-------------------------------------------------------------------------
VulkanInstance::~VulkanInstance()
Expand Down Expand Up @@ -393,6 +394,47 @@ namespace Ogre
#endif
}
//-------------------------------------------------------------------------
void VulkanInstance::initPhysicalDeviceList()
{
LogManager::getSingleton().logMessage( "Vulkan: Device detection starts" );

// enumerate physical devices - list never changes for the same VkInstance
FastArray<VkPhysicalDevice> devices;
uint32 numDevices = 0u;
VkResult result = vkEnumeratePhysicalDevices( mVkInstance, &numDevices, NULL );
checkVkResult( result, "vkEnumeratePhysicalDevices" );

devices.resize( numDevices );
result = vkEnumeratePhysicalDevices( mVkInstance, &numDevices, devices.begin() );
checkVkResult( result, "vkEnumeratePhysicalDevices" );

if( numDevices == 0u )
{
OGRE_EXCEPT( Exception::ERR_RENDERINGAPI_ERROR, "No Vulkan devices found.",
"VulkanRenderSystem::getVkPhysicalDevices" );
}

// assign unique names, allowing reordering/inserting/removing
map<String, unsigned>::type sameNameCounter;
mVulkanPhysicalDevices.clear();
mVulkanPhysicalDevices.reserve( devices.size() );
for( auto device : devices )
{
VkPhysicalDeviceProperties deviceProps;
vkGetPhysicalDeviceProperties( device, &deviceProps );

String name( deviceProps.deviceName );
unsigned sameNameIndex = sameNameCounter[name]++; // inserted entry is zero-initialized
if( sameNameIndex != 0 )
name += " (" + Ogre::StringConverter::toString( sameNameIndex + 1 ) + ")";

LogManager::getSingleton().logMessage( "Vulkan: \"" + name + "\"" );
mVulkanPhysicalDevices.push_back( { device, name } );
}

LogManager::getSingleton().logMessage( "Vulkan: Device detection ends" );
}
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
VulkanDevice::VulkanDevice( VkInstance instance, const String &deviceName,
Expand Down Expand Up @@ -654,7 +696,7 @@ namespace Ogre
//-------------------------------------------------------------------------
void VulkanDevice::createPhysicalDevice( const String &deviceName )
{
const VulkanPhysicalDeviceList &devices = mRenderSystem->getVulkanPhysicalDevices();
auto &devices = mRenderSystem->getVulkanPhysicalDevices();
size_t deviceIdx = 0;
for( size_t i = 0; i < devices.size(); ++i )
{
Expand Down
58 changes: 5 additions & 53 deletions RenderSystems/Vulkan/src/OgreVulkanRenderSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -994,59 +994,6 @@ namespace Ogre
this->_initialise( true );
}
//-------------------------------------------------------------------------
const VulkanPhysicalDeviceList &VulkanRenderSystem::getVulkanPhysicalDevices( bool refreshList )
{
if( refreshList || mVulkanPhysicalDeviceList.empty() )
{
LogManager::getSingleton().logMessage( "Vulkan: Device detection starts" );

// enumerate
std::vector<VkPhysicalDevice> devices;
VkResult result = VK_SUCCESS;
do
{
uint32 numDevices = 0u;
result = vkEnumeratePhysicalDevices( mInstance->mVkInstance, &numDevices, NULL );
checkVkResult( result, "vkEnumeratePhysicalDevices" );

if( numDevices == 0u )
{
OGRE_EXCEPT( Exception::ERR_RENDERINGAPI_ERROR, "No Vulkan devices found.",
"VulkanRenderSystem::getVkPhysicalDevices" );
}

devices.resize( numDevices );
result =
vkEnumeratePhysicalDevices( mInstance->mVkInstance, &numDevices, devices.data() );
devices.resize( numDevices );
if( result != VK_INCOMPLETE )
checkVkResult( result, "vkEnumeratePhysicalDevices" );

} while( result == VK_INCOMPLETE );

// assign unique names, allowing reordering/inserting/removing
map<String, unsigned>::type sameNameCounter;
mVulkanPhysicalDeviceList.clear();
mVulkanPhysicalDeviceList.reserve( devices.size() );
for( VkPhysicalDevice device : devices )
{
VkPhysicalDeviceProperties deviceProps;
vkGetPhysicalDeviceProperties( device, &deviceProps );

String name( deviceProps.deviceName );
unsigned sameNameIndex = sameNameCounter[name]++; // inserted entry is zero-initialized
if( sameNameIndex != 0 )
name += " (" + Ogre::StringConverter::toString( sameNameIndex + 1 ) + ")";

LogManager::getSingleton().logMessage( "Vulkan: \"" + name + "\"" );
mVulkanPhysicalDeviceList.push_back( { device, name } );
}

LogManager::getSingleton().logMessage( "Vulkan: Device detection ends" );
}
return mVulkanPhysicalDeviceList;
}
//-------------------------------------------------------------------------
Window *VulkanRenderSystem::_initialise( bool autoCreateWindow, const String &windowTitle )
{
Window *autoWindow = 0;
Expand Down Expand Up @@ -1368,6 +1315,11 @@ namespace Ogre
return win;
}
//-------------------------------------------------------------------------
const FastArray<VulkanPhysicalDevice> &VulkanRenderSystem::getVulkanPhysicalDevices() const
{
return mInstance->mVulkanPhysicalDevices;
}
//-------------------------------------------------------------------------
void VulkanRenderSystem::_notifyDeviceStalled()
{
v1::VulkanHardwareBufferManager *hwBufferMgr =
Expand Down

0 comments on commit 53157eb

Please sign in to comment.