diff options
Diffstat (limited to 'src/osd/modules/render/bgfx/effectmanager.cpp')
-rw-r--r-- | src/osd/modules/render/bgfx/effectmanager.cpp | 129 |
1 files changed, 81 insertions, 48 deletions
diff --git a/src/osd/modules/render/bgfx/effectmanager.cpp b/src/osd/modules/render/bgfx/effectmanager.cpp index 6462ae75f72..e5e5d1698f6 100644 --- a/src/osd/modules/render/bgfx/effectmanager.cpp +++ b/src/osd/modules/render/bgfx/effectmanager.cpp @@ -9,86 +9,119 @@ // //============================================================ +#include "effectmanager.h" + +#include "effect.h" +#include "effectreader.h" +#include "shadermanager.h" + +#include "path.h" + +#include "osdfile.h" +#include "modules/lib/osdobj_common.h" + #include <rapidjson/document.h> #include <rapidjson/error/en.h> #include <bx/readerwriter.h> #include <bx/file.h> -#include "emu.h" - #include <bgfx/bgfx.h> -#include "effectmanager.h" -#include "effectreader.h" -#include "effect.h" +#include <utility> -using namespace rapidjson; -effect_manager::~effect_manager() +static bool prepare_effect_document(const std::string &name, const osd_options &options, rapidjson::Document &document) { - for (std::pair<std::string, bgfx_effect*> effect : m_effects) - { - delete effect.second; - } - m_effects.clear(); -} - -bgfx_effect* effect_manager::effect(std::string name) -{ - std::map<std::string, bgfx_effect*>::iterator iter = m_effects.find(name); - if (iter != m_effects.end()) + std::string full_name = name; + if (full_name.length() < 5 || (full_name.compare(full_name.length() - 5, 5, ".json") != 0)) { - return iter->second; + full_name += ".json"; } - return load_effect(name); -} - -bgfx_effect* effect_manager::load_effect(std::string name) -{ - std::string full_name = name; - if (full_name.length() < 5 || (full_name.compare(full_name.length() - 5, 5, ".json") != 0)) { - full_name = full_name + ".json"; - } - std::string path; - osd_subst_env(path, util::string_format("%s" PATH_SEPARATOR "effects" PATH_SEPARATOR, m_options.bgfx_path())); - path += full_name; + const std::string path = util::path_concat(options.bgfx_path(), "effects", full_name); bx::FileReader reader; if (!bx::open(&reader, path.c_str())) { - printf("Unable to open effect file %s\n", path.c_str()); - return nullptr; + osd_printf_error("Unable to open effect file %s\n", path); + return false; } - int32_t size (bx::getSize(&reader)); + const int32_t size = bx::getSize(&reader); + std::unique_ptr<char []> data(new (std::nothrow) char [size + 1]); + if (!data) + { + osd_printf_error("Out of memory reading effect file %s\n", path); + bx::close(&reader); + return false; + } - char* data = new char[size + 1]; - bx::read(&reader, reinterpret_cast<void*>(data), size); + bx::ErrorAssert err; + bx::read(&reader, reinterpret_cast<void*>(data.get()), size, &err); bx::close(&reader); data[size] = 0; - Document document; - document.Parse<kParseCommentsFlag>(data); + document.Parse<rapidjson::kParseCommentsFlag>(data.get()); + data.reset(); - delete [] data; + if (document.HasParseError()) + { + std::string error(rapidjson::GetParseError_En(document.GetParseError())); + osd_printf_error("Unable to parse effect %s. Errors returned:\n%s\n", path, error); + return false; + } + + return true; +} + + +// keep constructor and destructor out-of-line so the header works with forward declarations + +effect_manager::effect_manager(shader_manager& shaders) : m_shaders(shaders) +{ +} + +effect_manager::~effect_manager() +{ + // the map will automatically dispose of the effects +} - if (document.HasParseError()) { - std::string error(GetParseError_En(document.GetParseError())); - printf("Unable to parse effect %s. Errors returned:\n", path.c_str()); - printf("%s\n", error.c_str()); +bgfx_effect* effect_manager::get_or_load_effect(const osd_options &options, const std::string &name) +{ + const auto iter = m_effects.find(name); + if (iter != m_effects.end()) + return iter->second.get(); + + return load_effect(options, name); +} + +bgfx_effect* effect_manager::load_effect(const osd_options &options, const std::string &name) +{ + rapidjson::Document document; + if (!prepare_effect_document(name, options, document)) + { return nullptr; } - bgfx_effect* effect = effect_reader::read_from_value(document, "Effect '" + name + "': ", m_shaders); + std::unique_ptr<bgfx_effect> effect = effect_reader::read_from_value(name, document, "Effect '" + name + "': ", options, m_shaders); - if (effect == nullptr) { - printf("Unable to load effect %s\n", path.c_str()); + if (!effect) + { + osd_printf_error("Unable to load effect %s\n", name); return nullptr; } - m_effects[name] = effect; + return m_effects.emplace(name, std::move(effect)).first->second.get(); +} + +bool effect_manager::validate_effect(const osd_options &options, const std::string &name) +{ + rapidjson::Document document; + if (!prepare_effect_document(name, options, document)) + { + return false; + } - return effect; + return effect_reader::validate_value(document, "Effect '" + name + "': ", options); } |