summaryrefslogtreecommitdiffstatshomepage
path: root/src/osd/modules/render/bgfx/effectmanager.cpp
blob: e5e5d1698f601f9c553826bf40cce1d3b8277b27 (plain) (blame)
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
// license:BSD-3-Clause
// copyright-holders:Ryan Holtz
//============================================================
//
//  effectmanager.cpp - BGFX shader effect manager
//
//  Maintains a string-to-entry lookup of BGFX shader
//  effects, defined by effect.h and read by effectreader.h
//
//============================================================

#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 <bgfx/bgfx.h>

#include <utility>


static bool prepare_effect_document(const std::string &name, const osd_options &options, rapidjson::Document &document)
{
	std::string full_name = name;
	if (full_name.length() < 5 || (full_name.compare(full_name.length() - 5, 5, ".json") != 0))
	{
		full_name += ".json";
	}

	const std::string path = util::path_concat(options.bgfx_path(), "effects", full_name);

	bx::FileReader reader;
	if (!bx::open(&reader, path.c_str()))
	{
		osd_printf_error("Unable to open effect file %s\n", path);
		return false;
	}

	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;
	}

	bx::ErrorAssert err;
	bx::read(&reader, reinterpret_cast<void*>(data.get()), size, &err);
	bx::close(&reader);
	data[size] = 0;

	document.Parse<rapidjson::kParseCommentsFlag>(data.get());
	data.reset();

	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
}

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;
	}

	std::unique_ptr<bgfx_effect> effect = effect_reader::read_from_value(name, document, "Effect '" + name + "': ", options, m_shaders);

	if (!effect)
	{
		osd_printf_error("Unable to load effect %s\n", name);
		return nullptr;
	}

	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_reader::validate_value(document, "Effect '" + name + "': ", options);
}