1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
#include "MaterialManager.h"
#include "MaterialSourceGenerator.h"
#include "i18n.h"
#include "ideclmanager.h"
#include "iregistry.h"
#include "icommandsystem.h"
#include "ifilesystem.h"
#include "ifiletypes.h"
#include "igame.h"
#include "ShaderExpression.h"
#include "debugging/ScopedDebugTimer.h"
#include "module/StaticModule.h"
#include "decl/DeclarationCreator.h"
#include "stream/TemporaryOutputStream.h"
#include "materials/ParseLib.h"
#include <functional>
#include "decl/DeclLib.h"
namespace
{
const char* TEXTURE_PREFIX = "textures/";
// Default image maps for optional material stages
const std::string IMAGE_FLAT = "_flat.bmp";
const std::string IMAGE_BLACK = "_black.bmp";
inline std::string getBitmapsPath()
{
return module::GlobalModuleRegistry().getApplicationContext().getBitmapsPath();
}
}
namespace shaders
{
MaterialManager::MaterialManager() :
_enableActiveUpdates(true)
{}
void MaterialManager::construct()
{
_library = std::make_shared<ShaderLibrary>();
_textureManager = std::make_shared<GLTextureManager>();
}
void MaterialManager::destroy()
{
// Don't destroy the GLTextureManager, it's called from
// the CShader destructors.
}
void MaterialManager::freeShaders() {
_library->clear();
_textureManager->checkBindings();
activeShadersChangedNotify();
}
MaterialPtr MaterialManager::getMaterial(const std::string& name)
{
return _library->findShader(name);
}
bool MaterialManager::materialExists(const std::string& name)
{
return _library->definitionExists(name);
}
bool MaterialManager::materialCanBeModified(const std::string& name)
{
if (!_library->definitionExists(name))
{
return false;
}
auto decl = _library->getTemplate(name);
const auto& fileInfo = decl->getBlockSyntax().fileInfo;
return fileInfo.name.empty() || fileInfo.getIsPhysicalFile();
}
void MaterialManager::foreachShaderName(const ShaderNameCallback& callback)
{
// Pass the call to the Library
_library->foreachShaderName(callback);
}
const char* MaterialManager::getTexturePrefix() const
{
return TEXTURE_PREFIX;
}
GLTextureManager& MaterialManager::getTextureManager()
{
return *_textureManager;
}
// Get default textures
TexturePtr MaterialManager::getDefaultInteractionTexture(IShaderLayer::Type type)
{
TexturePtr defaultTex;
// Look up based on layer type
switch (type)
{
case IShaderLayer::DIFFUSE:
case IShaderLayer::SPECULAR:
defaultTex = _textureManager->getBinding(getBitmapsPath() + IMAGE_BLACK);
break;
case IShaderLayer::BUMP:
defaultTex = _textureManager->getBinding(getBitmapsPath() + IMAGE_FLAT);
break;
default:
break;
}
return defaultTex;
}
sigc::signal<void> MaterialManager::signal_activeShadersChanged() const
{
return _signalActiveShadersChanged;
}
void MaterialManager::activeShadersChangedNotify()
{
if (_enableActiveUpdates)
{
_signalActiveShadersChanged.emit();
}
}
void MaterialManager::foreachMaterial(const std::function<void(const MaterialPtr&)>& func)
{
_library->foreachShader(func);
}
TexturePtr MaterialManager::loadTextureFromFile(const std::string& filename)
{
// Remove any unused Textures before allocating new ones.
_textureManager->checkBindings();
// Get the binding (i.e. load the texture)
return _textureManager->getBinding(filename);
}
sigc::signal<void, const std::string&>& MaterialManager::signal_materialCreated()
{
return _sigMaterialCreated;
}
sigc::signal<void, const std::string&, const std::string&>& MaterialManager::signal_materialRenamed()
{
return _sigMaterialRenamed;
}
sigc::signal<void, const std::string&>& MaterialManager::signal_materialRemoved()
{
return _sigMaterialRemoved;
}
IShaderExpression::Ptr MaterialManager::createShaderExpressionFromString(const std::string& exprStr)
{
return ShaderExpression::createFromString(exprStr);
}
MaterialPtr MaterialManager::createEmptyMaterial(const std::string& name)
{
// Find a non-conflicting name and create an empty declaration
auto candidate = decl::generateNonConflictingName(decl::Type::Material, name);
auto decl = GlobalDeclarationManager().findOrCreateDeclaration(decl::Type::Material, candidate);
auto material = _library->findShader(candidate);
material->setIsModified();
_sigMaterialCreated.emit(candidate);
return material;
}
bool MaterialManager::renameMaterial(const std::string& oldName, const std::string& newName)
{
auto result = _library->renameDefinition(oldName, newName);
if (result)
{
// Mark the material as modified
_library->findShader(newName)->setIsModified();
_sigMaterialRenamed(oldName, newName);
}
return result;
}
void MaterialManager::removeMaterial(const std::string& name)
{
if (!_library->definitionExists(name))
{
rWarning() << "Cannot remove non-existent material " << name << std::endl;
return;
}
_library->removeDefinition(name);
_sigMaterialRemoved.emit(name);
}
MaterialPtr MaterialManager::copyMaterial(const std::string& nameOfOriginal, const std::string& nameOfCopy)
{
if (nameOfCopy.empty())
{
rWarning() << "Cannot copy, the new name must not be empty" << std::endl;
return MaterialPtr();
}
auto candidate = decl::generateNonConflictingName(decl::Type::Material, nameOfCopy);
if (!_library->definitionExists(nameOfOriginal))
{
rWarning() << "Cannot copy non-existent material " << nameOfOriginal << std::endl;
return MaterialPtr();
}
_library->copyDefinition(nameOfOriginal, candidate);
_sigMaterialCreated.emit(candidate);
auto material = _library->findShader(candidate);
material->setIsModified();
return material;
}
void MaterialManager::saveMaterial(const std::string& name)
{
auto material = _library->findShader(name);
if (!material->isModified())
{
rMessage() << "Material is not modified, nothing to save." << std::endl;
return;
}
if (!materialCanBeModified(material->getName()))
{
throw std::runtime_error("Cannot save this material, it's read-only.");
}
// Store the modifications in our actual template and un-mark the file
material->commitModifications();
// Write the declaration to disk
GlobalDeclarationManager().saveDeclaration(material->getTemplate());
}
ITableDefinition::Ptr MaterialManager::getTable(const std::string& name)
{
return std::static_pointer_cast<ITableDefinition>(
GlobalDeclarationManager().findDeclaration(decl::Type::Table, name)
);
}
void MaterialManager::reloadImages()
{
_library->foreachShader([](const CShaderPtr& shader)
{
shader->refreshImageMaps();
});
}
const std::string& MaterialManager::getName() const
{
static std::string _name(MODULE_SHADERSYSTEM);
return _name;
}
const StringSet& MaterialManager::getDependencies() const
{
static StringSet _dependencies
{
MODULE_DECLMANAGER,
MODULE_VIRTUALFILESYSTEM,
MODULE_COMMANDSYSTEM,
MODULE_XMLREGISTRY,
MODULE_GAMEMANAGER,
MODULE_FILETYPES,
};
return _dependencies;
}
void MaterialManager::initialiseModule(const IApplicationContext& ctx)
{
GlobalDeclarationManager().registerDeclType("table", std::make_shared<decl::DeclarationCreator<TableDefinition>>(decl::Type::Table));
GlobalDeclarationManager().registerDeclType("material", std::make_shared<decl::DeclarationCreator<ShaderTemplate>>(decl::Type::Material));
GlobalDeclarationManager().registerDeclFolder(decl::Type::Material, "materials/", ".mtr");
_materialsReloadedSignal = GlobalDeclarationManager().signal_DeclsReloaded(decl::Type::Material)
.connect(sigc::mem_fun(this, &MaterialManager::onMaterialDefsReloaded));
construct();
// Register the mtr file extension
GlobalFiletypes().registerPattern("material", FileTypePattern(_("Material File"), "mtr", "*.mtr"));
GlobalCommandSystem().addCommand("ReloadImages", [this](const cmd::ArgumentList&) { reloadImages(); });
}
void MaterialManager::onMaterialDefsReloaded()
{
_library->foreachShader([](const CShaderPtr& shader)
{
shader->unrealise();
shader->realise();
shader->refreshImageMaps();
});
}
void MaterialManager::shutdownModule()
{
rMessage() << "MaterialManager::shutdownModule called" << std::endl;
destroy();
_library->clear();
_library.reset();
}
// Accessor function encapsulating the static shadersystem instance
MaterialManagerPtr GetShaderSystem()
{
// Acquire the moduleptr from the module registry
RegisterableModulePtr modulePtr(module::GlobalModuleRegistry().getModule(MODULE_SHADERSYSTEM));
// static_cast it onto our shadersystem type
return std::static_pointer_cast<MaterialManager>(modulePtr);
}
GLTextureManager& GetTextureManager()
{
return GetShaderSystem()->getTextureManager();
}
// Static module instance
module::StaticModuleRegistration<MaterialManager> materialManagerModule;
} // namespace shaders
|