diff --git a/lib/graphics_engine/include/ge_vulkan_driver.hpp b/lib/graphics_engine/include/ge_vulkan_driver.hpp index e4f761a17..9c130d631 100644 --- a/lib/graphics_engine/include/ge_vulkan_driver.hpp +++ b/lib/graphics_engine/include/ge_vulkan_driver.hpp @@ -22,6 +22,7 @@ using namespace video; namespace GE { + class GEVulkanMeshCache; enum GEVulkanSampler : unsigned { GVS_MIN = 0, @@ -473,6 +474,7 @@ namespace GE std::string getDriverVersionString() const; void destroySwapChainRelated(bool handle_surface); void createSwapChainRelated(bool handle_surface); + GEVulkanMeshCache* getVulkanMeshCache() const; }; } diff --git a/lib/graphics_engine/include/ge_vulkan_mesh_cache.hpp b/lib/graphics_engine/include/ge_vulkan_mesh_cache.hpp deleted file mode 100644 index 3c5de2ffc..000000000 --- a/lib/graphics_engine/include/ge_vulkan_mesh_cache.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef HEADER_GE_VULKAN_MESH_CACHE_HPP -#define HEADER_GE_VULKAN_MESH_CACHE_HPP - -#include "vulkan_wrapper.h" - -namespace GE -{ -class GEVulkanDriver; -namespace GEVulkanMeshCache -{ -// ---------------------------------------------------------------------------- -void init(GEVulkanDriver*); -// ---------------------------------------------------------------------------- -void irrlichtMeshChanged(); -// ---------------------------------------------------------------------------- -void updateCache(); -// ---------------------------------------------------------------------------- -void destroy(); -// ---------------------------------------------------------------------------- -VkBuffer getVBO(); -// ---------------------------------------------------------------------------- -VkBuffer getIBO(); -}; // GEVulkanMeshCache - -} - -#endif diff --git a/lib/graphics_engine/include/ge_vulkan_scene_manager.hpp b/lib/graphics_engine/include/ge_vulkan_scene_manager.hpp index 641b6cc9c..81856ffd6 100644 --- a/lib/graphics_engine/include/ge_vulkan_scene_manager.hpp +++ b/lib/graphics_engine/include/ge_vulkan_scene_manager.hpp @@ -14,7 +14,6 @@ public: GEVulkanSceneManager(irr::video::IVideoDriver* driver, irr::io::IFileSystem* fs, irr::gui::ICursorControl* cursor_control, - irr::scene::IMeshCache* cache, irr::gui::IGUIEnvironment* gui_environment); // ------------------------------------------------------------------------ ~GEVulkanSceneManager(); diff --git a/lib/graphics_engine/src/ge_vulkan_driver.cpp b/lib/graphics_engine/src/ge_vulkan_driver.cpp index e2a9bcfab..bf055dd06 100644 --- a/lib/graphics_engine/src/ge_vulkan_driver.cpp +++ b/lib/graphics_engine/src/ge_vulkan_driver.cpp @@ -9,6 +9,9 @@ #include "ge_vulkan_texture.hpp" #include "ge_vulkan_command_loader.hpp" +#include "ISceneManager.h" +#include "IrrlichtDevice.h" + #ifdef _IRR_COMPILE_WITH_VULKAN_ #include "SDL_vulkan.h" #include @@ -552,7 +555,6 @@ GEVulkanDriver::GEVulkanDriver(const SIrrlichtCreationParameters& params, // For GEVulkanDynamicBuffer GE::setVideoDriver(this); GEVulkan2dRenderer::init(this); - GEVulkanMeshCache::init(this); createUnicolorTextures(); GEVulkanFeatures::printStats(); } @@ -583,7 +585,7 @@ void GEVulkanDriver::destroyVulkan() m_transparent_texture = NULL; } - GEVulkanMeshCache::destroy(); + getVulkanMeshCache()->destroy(); GEVulkan2dRenderer::destroy(); GEVulkanShaderManager::destroy(); @@ -2026,6 +2028,13 @@ void GEVulkanDriver::waitIdle() m->unlock(); } // waitIdle +// ---------------------------------------------------------------------------- +GEVulkanMeshCache* GEVulkanDriver::getVulkanMeshCache() const +{ + return static_cast + (m_irrlicht_device->getSceneManager()->getMeshCache()); +} // getVulkanMeshCache + } namespace irr diff --git a/lib/graphics_engine/src/ge_vulkan_mesh_cache.cpp b/lib/graphics_engine/src/ge_vulkan_mesh_cache.cpp index 36f6d5d11..252b7b869 100644 --- a/lib/graphics_engine/src/ge_vulkan_mesh_cache.cpp +++ b/lib/graphics_engine/src/ge_vulkan_mesh_cache.cpp @@ -5,59 +5,45 @@ #include "ge_vulkan_driver.hpp" #include "IAnimatedMesh.h" -#include "IMeshCache.h" -#include "ISceneManager.h" -#include "IrrlichtDevice.h" #include namespace GE { -namespace GEVulkanMeshCache -{ -// ============================================================================ -GEVulkanDriver* g_vk; -uint64_t g_irrlicht_cache_time; -uint64_t g_ge_cache_time; -VkBuffer g_vbo_buffer; -VkDeviceMemory g_vbo_memory; -VkBuffer g_ibo_buffer; -VkDeviceMemory g_ibo_memory; // ---------------------------------------------------------------------------- -void init(GEVulkanDriver* vk) +GEVulkanMeshCache::GEVulkanMeshCache() + : irr::scene::CMeshCache() { - g_vk = vk; - g_irrlicht_cache_time = getMonoTimeMs(); - g_ge_cache_time = 0; - g_vbo_buffer = VK_NULL_HANDLE; - g_vbo_memory = VK_NULL_HANDLE; - g_ibo_buffer = VK_NULL_HANDLE; - g_ibo_memory = VK_NULL_HANDLE; + m_vk = getVKDriver(); + m_irrlicht_cache_time = getMonoTimeMs(); + m_ge_cache_time = 0; + m_vbo_buffer = VK_NULL_HANDLE; + m_vbo_memory = VK_NULL_HANDLE; + m_ibo_buffer = VK_NULL_HANDLE; + m_ibo_memory = VK_NULL_HANDLE; } // init // ---------------------------------------------------------------------------- -void irrlichtMeshChanged() +void GEVulkanMeshCache::meshCacheChanged() { - g_irrlicht_cache_time = getMonoTimeMs(); -} // irrlichtMeshChanged + m_irrlicht_cache_time = getMonoTimeMs(); +} // meshCacheChanged // ---------------------------------------------------------------------------- -void updateCache() +void GEVulkanMeshCache::updateCache() { - if (g_irrlicht_cache_time <= g_ge_cache_time) + if (m_irrlicht_cache_time <= m_ge_cache_time) return; - g_ge_cache_time = g_irrlicht_cache_time; + m_ge_cache_time = m_irrlicht_cache_time; destroy(); size_t vbo_size, ibo_size; vbo_size = 0; ibo_size = 0; - scene::IMeshCache* cache = g_vk->getIrrlichtDevice()->getSceneManager() - ->getMeshCache(); std::vector buffers; - for (unsigned i = 0; i < cache->getMeshCount(); i++) + for (unsigned i = 0; i < Meshes.size(); i++) { - scene::IAnimatedMesh* mesh = cache->getMeshByIndex(i); + scene::IAnimatedMesh* mesh = Meshes[i].Mesh; if (mesh->getMeshType() != scene::EAMT_SPM) continue; for (unsigned j = 0; j < mesh->getMeshBufferCount(); j++) @@ -74,7 +60,7 @@ void updateCache() VkBuffer staging_buffer = VK_NULL_HANDLE; VkDeviceMemory staging_memory = VK_NULL_HANDLE; - if (!g_vk->createBuffer(vbo_size, + if (!m_vk->createBuffer(vbo_size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, @@ -82,7 +68,7 @@ void updateCache() throw std::runtime_error("updateCache create staging vbo failed"); uint8_t* mapped; - if (vkMapMemory(g_vk->getDevice(), staging_memory, 0, + if (vkMapMemory(m_vk->getDevice(), staging_memory, 0, vbo_size, 0, (void**)&mapped) != VK_SUCCESS) throw std::runtime_error("updateCache vkMapMemory failed"); size_t offset = 0; @@ -96,25 +82,25 @@ void updateCache() offset += copy_size; } - if (!g_vk->createBuffer(vbo_size, + if (!m_vk->createBuffer(vbo_size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, - g_vbo_buffer, g_vbo_memory)) + m_vbo_buffer, m_vbo_memory)) throw std::runtime_error("updateCache create vbo failed"); - g_vk->copyBuffer(staging_buffer, g_vbo_buffer, vbo_size); - vkUnmapMemory(g_vk->getDevice(), staging_memory); - vkFreeMemory(g_vk->getDevice(), staging_memory, NULL); - vkDestroyBuffer(g_vk->getDevice(), staging_buffer, NULL); + m_vk->copyBuffer(staging_buffer, m_vbo_buffer, vbo_size); + vkUnmapMemory(m_vk->getDevice(), staging_memory); + vkFreeMemory(m_vk->getDevice(), staging_memory, NULL); + vkDestroyBuffer(m_vk->getDevice(), staging_buffer, NULL); - if (!g_vk->createBuffer(ibo_size, + if (!m_vk->createBuffer(ibo_size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, staging_buffer, staging_memory)) throw std::runtime_error("updateCache create staging ibo failed"); - if (vkMapMemory(g_vk->getDevice(), staging_memory, 0, + if (vkMapMemory(m_vk->getDevice(), staging_memory, 0, ibo_size, 0, (void**)&mapped) != VK_SUCCESS) throw std::runtime_error("updateCache vkMapMemory failed"); offset = 0; @@ -126,50 +112,36 @@ void updateCache() offset += copy_size; } - if (!g_vk->createBuffer(ibo_size, + if (!m_vk->createBuffer(ibo_size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, - g_ibo_buffer, g_ibo_memory)) + m_ibo_buffer, m_ibo_memory)) throw std::runtime_error("updateCache create ibo failed"); - g_vk->copyBuffer(staging_buffer, g_ibo_buffer, ibo_size); - vkUnmapMemory(g_vk->getDevice(), staging_memory); - vkFreeMemory(g_vk->getDevice(), staging_memory, NULL); - vkDestroyBuffer(g_vk->getDevice(), staging_buffer, NULL); + m_vk->copyBuffer(staging_buffer, m_ibo_buffer, ibo_size); + vkUnmapMemory(m_vk->getDevice(), staging_memory); + vkFreeMemory(m_vk->getDevice(), staging_memory, NULL); + vkDestroyBuffer(m_vk->getDevice(), staging_buffer, NULL); } // updateCache // ---------------------------------------------------------------------------- -void destroy() +void GEVulkanMeshCache::destroy() { - g_vk->waitIdle(); + m_vk->waitIdle(); - if (g_vbo_memory != VK_NULL_HANDLE) - vkFreeMemory(g_vk->getDevice(), g_vbo_memory, NULL); - g_vbo_memory = VK_NULL_HANDLE; - if (g_vbo_buffer != VK_NULL_HANDLE) - vkDestroyBuffer(g_vk->getDevice(), g_vbo_buffer, NULL); - g_vbo_buffer = VK_NULL_HANDLE; + if (m_vbo_memory != VK_NULL_HANDLE) + vkFreeMemory(m_vk->getDevice(), m_vbo_memory, NULL); + m_vbo_memory = VK_NULL_HANDLE; + if (m_vbo_buffer != VK_NULL_HANDLE) + vkDestroyBuffer(m_vk->getDevice(), m_vbo_buffer, NULL); + m_vbo_buffer = VK_NULL_HANDLE; - if (g_ibo_memory != VK_NULL_HANDLE) - vkFreeMemory(g_vk->getDevice(), g_ibo_memory, NULL); - g_ibo_memory = VK_NULL_HANDLE; - if (g_ibo_buffer != VK_NULL_HANDLE) - vkDestroyBuffer(g_vk->getDevice(), g_ibo_buffer, NULL); - g_ibo_buffer = VK_NULL_HANDLE; + if (m_ibo_memory != VK_NULL_HANDLE) + vkFreeMemory(m_vk->getDevice(), m_ibo_memory, NULL); + m_ibo_memory = VK_NULL_HANDLE; + if (m_ibo_buffer != VK_NULL_HANDLE) + vkDestroyBuffer(m_vk->getDevice(), m_ibo_buffer, NULL); + m_ibo_buffer = VK_NULL_HANDLE; } // destroy -// ---------------------------------------------------------------------------- -VkBuffer getVBO() -{ - return g_vbo_buffer; -} // getVBO - -// ---------------------------------------------------------------------------- -VkBuffer getIBO() -{ - return g_ibo_buffer; -} // getIBO - -} - } diff --git a/lib/graphics_engine/src/ge_vulkan_mesh_cache.hpp b/lib/graphics_engine/src/ge_vulkan_mesh_cache.hpp new file mode 100644 index 000000000..6fbca561f --- /dev/null +++ b/lib/graphics_engine/src/ge_vulkan_mesh_cache.hpp @@ -0,0 +1,39 @@ +#ifndef HEADER_GE_VULKAN_MESH_CACHE_HPP +#define HEADER_GE_VULKAN_MESH_CACHE_HPP + +#include "vulkan_wrapper.h" +#include "../source/Irrlicht/CMeshCache.h" + +#include + +namespace GE +{ +class GEVulkanDriver; +class GEVulkanMeshCache : public irr::scene::CMeshCache +{ +private: + GEVulkanDriver* m_vk; + + uint64_t m_irrlicht_cache_time, m_ge_cache_time; + + VkBuffer m_vbo_buffer, m_ibo_buffer; + + VkDeviceMemory m_vbo_memory, m_ibo_memory; +public: + // ------------------------------------------------------------------------ + GEVulkanMeshCache(); + // ------------------------------------------------------------------------ + virtual void meshCacheChanged(); + // ------------------------------------------------------------------------ + void updateCache(); + // ------------------------------------------------------------------------ + void destroy(); + // ------------------------------------------------------------------------ + VkBuffer getVBO() const { return m_vbo_buffer; } + // ------------------------------------------------------------------------ + VkBuffer getIBO() const { return m_ibo_buffer; } +}; // GEVulkanMeshCache + +} + +#endif diff --git a/lib/graphics_engine/src/ge_vulkan_scene_manager.cpp b/lib/graphics_engine/src/ge_vulkan_scene_manager.cpp index f628578df..62630f699 100644 --- a/lib/graphics_engine/src/ge_vulkan_scene_manager.cpp +++ b/lib/graphics_engine/src/ge_vulkan_scene_manager.cpp @@ -1,15 +1,16 @@ #include "ge_vulkan_scene_manager.hpp" +#include "ge_vulkan_mesh_cache.hpp" + namespace GE { // ---------------------------------------------------------------------------- GEVulkanSceneManager::GEVulkanSceneManager(irr::video::IVideoDriver* driver, irr::io::IFileSystem* fs, irr::gui::ICursorControl* cursor_control, - irr::scene::IMeshCache* cache, irr::gui::IGUIEnvironment* gui_environment) - : CSceneManager(driver, fs, cursor_control, cache, - gui_environment) + : CSceneManager(driver, fs, cursor_control, + new GEVulkanMeshCache(), gui_environment) { } // GEVulkanSceneManager diff --git a/lib/irrlicht/source/Irrlicht/CIrrDeviceSDL.cpp b/lib/irrlicht/source/Irrlicht/CIrrDeviceSDL.cpp index 1e1b9eec0..d1ca50fba 100644 --- a/lib/irrlicht/source/Irrlicht/CIrrDeviceSDL.cpp +++ b/lib/irrlicht/source/Irrlicht/CIrrDeviceSDL.cpp @@ -1596,7 +1596,7 @@ void CIrrDeviceSDL::createGUIAndVulkanScene() #endif // create Scene manager - SceneManager = new GE::GEVulkanSceneManager(VideoDriver, FileSystem, CursorControl, NULL, GUIEnvironment); + SceneManager = new GE::GEVulkanSceneManager(VideoDriver, FileSystem, CursorControl, GUIEnvironment); setEventReceiver(UserReceiver); } diff --git a/lib/irrlicht/source/Irrlicht/CMeshCache.cpp b/lib/irrlicht/source/Irrlicht/CMeshCache.cpp index c48d4ffeb..de74a83d1 100644 --- a/lib/irrlicht/source/Irrlicht/CMeshCache.cpp +++ b/lib/irrlicht/source/Irrlicht/CMeshCache.cpp @@ -6,10 +6,6 @@ #include "IAnimatedMesh.h" #include "IMesh.h" -#ifndef SERVER_ONLY -#include -#endif - namespace irr { namespace scene @@ -33,9 +29,7 @@ void CMeshCache::addMesh(const io::path& filename, IAnimatedMesh* mesh) e.Mesh = mesh; Meshes.push_back(e); -#ifndef SERVER_ONLY - GE::GEVulkanMeshCache::irrlichtMeshChanged(); -#endif + meshCacheChanged(); } @@ -50,9 +44,7 @@ void CMeshCache::removeMesh(const IMesh* const mesh) { Meshes[i].Mesh->drop(); Meshes.erase(i); -#ifndef SERVER_ONLY - GE::GEVulkanMeshCache::irrlichtMeshChanged(); -#endif + meshCacheChanged(); return; } } @@ -166,9 +158,7 @@ void CMeshCache::clear() Meshes[i].Mesh->drop(); Meshes.clear(); -#ifndef SERVER_ONLY - GE::GEVulkanMeshCache::irrlichtMeshChanged(); -#endif + meshCacheChanged(); } //! Clears all meshes that are held in the mesh cache but not used anywhere else. @@ -181,9 +171,7 @@ void CMeshCache::clearUnusedMeshes() Meshes[i].Mesh->drop(); Meshes.erase(i); --i; -#ifndef SERVER_ONLY - GE::GEVulkanMeshCache::irrlichtMeshChanged(); -#endif + meshCacheChanged(); } } } diff --git a/lib/irrlicht/source/Irrlicht/CMeshCache.h b/lib/irrlicht/source/Irrlicht/CMeshCache.h index e3cdc9f5f..9424c5ffe 100644 --- a/lib/irrlicht/source/Irrlicht/CMeshCache.h +++ b/lib/irrlicht/source/Irrlicht/CMeshCache.h @@ -113,6 +113,8 @@ namespace scene //! loaded meshes core::array Meshes; + + virtual void meshCacheChanged() {} };