Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #ifndef INCLUDED_TERRAINTEXTUREMANAGER 19 : #define INCLUDED_TERRAINTEXTUREMANAGER 20 : 21 : #include "lib/file/vfs/vfs_path.h" 22 : #include "ps/CStr.h" 23 : #include "ps/Loader.h" 24 : #include "ps/Singleton.h" 25 : #include "renderer/backend/ITexture.h" 26 : 27 : #include <cstddef> 28 : #include <map> 29 : #include <memory> 30 : #include <vector> 31 : 32 : class CTerrainProperties; 33 : class CTerrainTextureEntry; 34 : namespace Renderer::Backend { class IDevice; } 35 : namespace Renderer::Backend { class IDeviceCommandContext; } 36 : 37 : typedef std::shared_ptr<CTerrainProperties> CTerrainPropertiesPtr; 38 : 39 : #define NUM_ALPHA_MAPS 14 40 : 41 : class CTerrainGroup 42 : { 43 : // name of this terrain group (as specified by the terrain XML) 44 : CStr m_Name; 45 : // "index".. basically a bogus integer that can be used by ScEd to set texture 46 : // priorities 47 : size_t m_Index; 48 : // list of textures of this type (found from the texture directory) 49 : std::vector<CTerrainTextureEntry*> m_Terrains; 50 : 51 : public: 52 0 : CTerrainGroup(CStr name, size_t index): 53 0 : m_Name(name), 54 0 : m_Index(index) 55 : {} 56 : 57 : // Add a texture entry to this terrain type 58 : void AddTerrain(CTerrainTextureEntry*); 59 : // Remove a texture entry 60 : void RemoveTerrain(CTerrainTextureEntry*); 61 : 62 : size_t GetIndex() const 63 : { return m_Index; } 64 : CStr GetName() const 65 : { return m_Name; } 66 : 67 : const std::vector<CTerrainTextureEntry*> &GetTerrains() const 68 0 : { return m_Terrains; } 69 : }; 70 : 71 : 72 0 : struct TerrainAlpha 73 : { 74 : // Composite alpha map (all the alpha maps packed into one texture). 75 : std::unique_ptr<Renderer::Backend::ITexture> m_CompositeAlphaMap; 76 : // Data is used to separate file loading and uploading to GPU. 77 : std::shared_ptr<std::uint8_t> m_CompositeDataToUpload; 78 : // Coordinates of each (untransformed) alpha map within the packed texture. 79 : struct 80 : { 81 : float u0, u1, v0, v1; 82 : } m_AlphaMapCoords[NUM_ALPHA_MAPS]; 83 : }; 84 : 85 : 86 : /////////////////////////////////////////////////////////////////////////////////////////// 87 : // CTerrainTextureManager : manager class for all terrain texture objects 88 : class CTerrainTextureManager : public Singleton<CTerrainTextureManager> 89 : { 90 : friend class CTerrainTextureEntry; 91 : 92 : public: 93 : using TerrainGroupMap = std::map<CStr, std::unique_ptr<CTerrainGroup>>; 94 : using TerrainAlphaMap = std::map<VfsPath, TerrainAlpha>; 95 : 96 : // constructor, destructor 97 : CTerrainTextureManager(Renderer::Backend::IDevice* device); 98 : ~CTerrainTextureManager(); 99 : 100 : // Find all XML's in the directory (with subdirs) and try to load them as 101 : // terrain XML's 102 : PS::Loader::Task LoadTerrainTextures(); 103 : 104 : void UnloadTerrainTextures(); 105 : 106 : CTerrainTextureEntry* FindTexture(const CStr& tag); 107 : 108 : // Create a texture object for a new terrain texture at path, using the 109 : // property sheet props. 110 : void AddTexture(const CTerrainPropertiesPtr& props, const VfsPath& path); 111 : 112 : // Find or create a new texture group. All terrain groups are owned by the 113 : // texturemanager (TerrainTypeManager) 114 : CTerrainGroup* FindGroup(const CStr& name); 115 : 116 : const TerrainGroupMap& GetGroups() const 117 0 : { return m_TerrainGroups; } 118 : 119 : CTerrainTextureManager::TerrainAlphaMap::iterator LoadAlphaMap(const VfsPath& alphaMapType); 120 : 121 : void UploadResourcesIfNeeded(Renderer::Backend::IDeviceCommandContext* deviceCommandContext); 122 : 123 : private: 124 : Renderer::Backend::IDevice* m_Device = nullptr; 125 : 126 : // All texture entries created by this class, for easy freeing now that 127 : // textures may be in several STextureType's 128 : std::vector<std::unique_ptr<CTerrainTextureEntry>> m_TextureEntries; 129 : 130 : TerrainGroupMap m_TerrainGroups; 131 : 132 : TerrainAlphaMap m_TerrainAlphas; 133 : 134 : size_t m_LastGroupIndex = 0; 135 : 136 : // A way to separate file loading and uploading to GPU to not stall uploading. 137 : // Once we get a properly threaded loading we might optimize that. 138 : std::vector<CTerrainTextureManager::TerrainAlphaMap::iterator> m_AlphaMapsToUpload; 139 : }; 140 : 141 : // access to sole CTerrainTextureManager object 142 : #define g_TexMan CTerrainTextureManager::GetSingleton() 143 : 144 : #endif // INCLUDED_TERRAINTEXTUREMANAGER