summaryrefslogtreecommitdiff
path: root/Source/Core/VideoCommon/PostProcessing.cpp
blob: 69d34dbe66534d2691764d28c101f35a4c2f3b08 (plain)
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.

#include <string>

#include "Common/CommonPaths.h"
#include "Common/FileUtil.h"
#include "Common/IniFile.h"
#include "Common/MsgHandler.h"
#include "Common/StringUtil.h"

#include "VideoCommon/PostProcessing.h"
#include "VideoCommon/VideoConfig.h"


PostProcessingShaderImplementation::PostProcessingShaderImplementation()
{
	m_timer.Start();
}

PostProcessingShaderImplementation::~PostProcessingShaderImplementation()
{
	m_timer.Stop();
}

std::string PostProcessingShaderConfiguration::LoadShader(std::string shader)
{
	// Load the shader from the configuration if there isn't one sent to us.
	if (shader == "")
		shader = g_ActiveConfig.sPostProcessingShader;
	m_current_shader = shader;

	// loading shader code
	std::string code;
	std::string path = File::GetUserPath(D_SHADERS_IDX) + shader + ".glsl";

	if (!File::Exists(path))
	{
		// Fallback to shared user dir
		path = File::GetSysDirectory() + SHADERS_DIR DIR_SEP + shader + ".glsl";
	}

	if (!File::ReadFileToString(path, code))
	{
		ERROR_LOG(VIDEO, "Post-processing shader not found: %s", path.c_str());
		return "";
	}

	LoadOptions(code);

	std::string error = VerifyOptions(m_options);
	if (error.size())
	{
		PanicAlert("%s", StringFromFormat("Shader %s has a configuration issue!\n%s", shader.c_str(), error.c_str()).c_str());
		return "";
	}

	LoadOptionsConfiguration();

	return code;
}

std::string PostProcessingShaderConfiguration::VerifyOptions(const ConfigMap& config_map)
{
	for (const auto& option : config_map)
	{
		// Make sure we have an option name
		if (!option.second.m_option_name.size())
			return "Option doesn't have a unique identifier via 'OptionName'";

		// Make sure we have a correct type
		if (option.second.m_type == ConfigurationOption::OptionType::OPTION_INVALID)
			return StringFromFormat("Option '%s' has invalid type", option.second.m_option_name.c_str());

		// Make sure we have a GUI name
		if (!option.second.m_gui_name.size())
			return StringFromFormat("Option '%s' has no GUI name", option.second.m_option_name.c_str());

		// If we have a dependent option, make sure that option actually exists
		if (option.second.m_dependent_option.size())
		{
			if (config_map.find(option.second.m_dependent_option) == config_map.end())
				return StringFromFormat("Option '%s' has a dependent option '%s' that doesn't exist",
				       option.second.m_option_name.c_str(), option.second.m_dependent_option.c_str());
		}

		switch (option.second.m_type)
		{
		case ConfigurationOption::OptionType::OPTION_INTEGER:
		{
			// Make sure our vectors are the same sizes
			if (option.second.m_integer_values.size() != option.second.m_integer_min_values.size() ||
			    option.second.m_integer_min_values.size() != option.second.m_integer_max_values.size() ||
			    option.second.m_integer_max_values.size() != option.second.m_integer_step_values.size())
				return StringFromFormat("Option '%s' has invalid set value amounts", option.second.m_option_name.c_str());

			int option_size = option.second.m_integer_values.size();
			// Make sure our minimums are lower than our maximums
			for (int i = 0; i < option_size; ++i)
			{
				if (option.second.m_integer_min_values[i] > option.second.m_integer_max_values[i])
					return StringFromFormat("Option '%s' minimum value index %d is greater than max(%d > %d)",
					       option.second.m_option_name.c_str(), i,
						 option.second.m_integer_min_values[i], option.second.m_integer_max_values[i]);
			}

			// Make sure the default value is between minimum and maximum
			for (int i = 0; i < option_size; ++i)
			{
				if (option.second.m_integer_values[i] < option.second.m_integer_min_values[i] ||
				    option.second.m_integer_values[i] > option.second.m_integer_max_values[i])
					return StringFromFormat("Option '%s' default value index %d is outside of available range(%d outside %d - %d)",
					       option.second.m_option_name.c_str(), i,
						 option.second.m_integer_values[i],
						 option.second.m_integer_min_values[i], option.second.m_integer_max_values[i]);
			}

			// Make sure our step size is smaller than the range acceptable values
			for (int i = 0; i < option_size; ++i)
			{
				if (option.second.m_integer_step_values[i] >
				    (option.second.m_integer_max_values[i] - option.second.m_integer_min_values[i]))
					return StringFromFormat("Option '%s' step value is larger than maximum range of %d",
					       option.second.m_option_name.c_str(), option.second.m_integer_max_values[i] - option.second.m_integer_min_values[i]);
			}
		}
		break;
		case ConfigurationOption::OptionType::OPTION_FLOAT:
		{
			// Make sure our vectors are the same sizes
			if (option.second.m_float_values.size() != option.second.m_float_min_values.size() ||
			    option.second.m_float_min_values.size() != option.second.m_float_max_values.size() ||
			    option.second.m_float_max_values.size() != option.second.m_float_step_values.size())
				return StringFromFormat("Option '%s' has invalid set value amounts", option.second.m_option_name.c_str());

			int option_size = option.second.m_float_values.size();
			// Make sure our minimums are lower than our maximums
			for (int i = 0; i < option_size; ++i)
			{
				if (option.second.m_float_min_values[i] > option.second.m_float_max_values[i])
					return StringFromFormat("Option '%s' minimum value index %d is greater than max(%f > %f)",
					       option.second.m_option_name.c_str(), i,
						 option.second.m_float_min_values[i], option.second.m_float_max_values[i]);
			}

			// Make sure the default value is between minimum and maximum
			for (int i = 0; i < option_size; ++i)
			{
				if (option.second.m_float_values[i] < option.second.m_float_min_values[i] ||
				    option.second.m_float_values[i] > option.second.m_float_max_values[i])
					return StringFromFormat("Option '%s' default value index %d is outside of available range(%f outside %f - %f)",
					       option.second.m_option_name.c_str(), i,
						 option.second.m_float_values[i],
						 option.second.m_float_min_values[i], option.second.m_float_max_values[i]);
			}

			// Make sure our step size is smaller than the range acceptable values
			for (int i = 0; i < option_size; ++i)
			{
				if (option.second.m_float_step_values[i] >
				    (option.second.m_float_max_values[i] - option.second.m_float_min_values[i]))
					return StringFromFormat("Option '%s' step value is larger than maximum range of %f",
					       option.second.m_option_name.c_str(), option.second.m_float_max_values[i] - option.second.m_float_min_values[i]);
			}
		}
		break;
		case ConfigurationOption::OptionType::OPTION_BOOL:
		case ConfigurationOption::OptionType::OPTION_INVALID: // Won't be hit
		default:
		break;
		}
	}
	return "";
}

void PostProcessingShaderConfiguration::LoadOptions(const std::string& code)
{
	const std::string config_start_delimiter = "[configuration]";
	const std::string config_end_delimiter = "[/configuration]";
	size_t configuration_start = code.find(config_start_delimiter);
	size_t configuration_end = code.find(config_end_delimiter);

	m_options.clear();
	m_any_options_dirty = true;

	if (configuration_start == std::string::npos ||
	    configuration_end == std::string::npos)
	{
		// Issue loading configuration or there isn't one.
		return;
	}

	std::string configuration_string = code.substr(configuration_start + config_start_delimiter.size(),
	                                               configuration_end - configuration_start - config_start_delimiter.size());

	std::istringstream in(configuration_string);

	struct GLSLStringOption
	{
		std::string m_type;
		std::vector<std::pair<std::string, std::string>> m_options;
	};

	std::vector<GLSLStringOption> option_strings;
	GLSLStringOption* current_strings = nullptr;
	while (!in.eof())
	{
		std::string line;

		if (std::getline(in, line))
		{
#ifndef _WIN32
			// Check for CRLF eol and convert it to LF
			if (!line.empty() && line.at(line.size()-1) == '\r')
			{
				line.erase(line.size()-1);
			}
#endif

			if (line.size() > 0)
			{
				if (line[0] == '[')
				{
					size_t endpos = line.find("]");

					if (endpos != std::string::npos)
					{
						// New section!
						std::string sub = line.substr(1, endpos - 1);
						option_strings.push_back({ sub });
						current_strings = &option_strings.back();
					}
				}
				else
				{
					if (current_strings)
					{
						std::string key, value;
						IniFile::ParseLine(line, &key, &value);

						if (!(key == "" && value == ""))
							current_strings->m_options.push_back(std::make_pair(key, value));
					}
				}
			}
		}
	}

	for (const auto& it : option_strings)
	{
		ConfigurationOption option;
		option.m_dirty = true;

		if (it.m_type == "OptionBool")
			option.m_type = ConfigurationOption::OptionType::OPTION_BOOL;
		else if (it.m_type == "OptionRangeFloat")
			option.m_type = ConfigurationOption::OptionType::OPTION_FLOAT;
		else if (it.m_type == "OptionRangeInteger")
			option.m_type = ConfigurationOption::OptionType::OPTION_INTEGER;
		else
			option.m_type = ConfigurationOption::OptionType::OPTION_INVALID;

		for (const auto& string_option : it.m_options)
		{
			if (string_option.first == "GUIName")
			{
				option.m_gui_name = string_option.second;
			}
			else if (string_option.first == "OptionName")
			{
				option.m_option_name = string_option.second;
			}
			else if (string_option.first == "DependentOption")
			{
				option.m_dependent_option = string_option.second;
			}
			else if (string_option.first == "MinValue" ||
			         string_option.first == "MaxValue" ||
			         string_option.first == "DefaultValue" ||
			         string_option.first == "StepAmount")
			{
				std::vector<s32>* output_integer = nullptr;
				std::vector<float>* output_float = nullptr;

				if (string_option.first == "MinValue")
				{
					output_integer = &option.m_integer_min_values;
					output_float = &option.m_float_min_values;
				}
				else if (string_option.first == "MaxValue")
				{
					output_integer = &option.m_integer_max_values;
					output_float = &option.m_float_max_values;
				}
				else if (string_option.first == "DefaultValue")
				{
					output_integer = &option.m_integer_values;
					output_float = &option.m_float_values;
				}
				else if (string_option.first == "StepAmount")
				{
					output_integer = &option.m_integer_step_values;
					output_float = &option.m_float_step_values;
				}

				if (option.m_type == ConfigurationOption::OptionType::OPTION_BOOL)
				{
					TryParse(string_option.second, &option.m_bool_value);
				}
				else if (option.m_type == ConfigurationOption::OptionType::OPTION_INTEGER)
				{
					TryParseVector(string_option.second, output_integer);
					if (output_integer->size() > 4)
						output_integer->erase(output_integer->begin() + 4, output_integer->end());
				}
				else if (option.m_type == ConfigurationOption::OptionType::OPTION_FLOAT)
				{
					TryParseVector(string_option.second, output_float);
					if (output_float->size() > 4)
						output_float->erase(output_float->begin() + 4, output_float->end());
				}
			}
		}
		m_options[option.m_option_name] = option;
	}
}

void PostProcessingShaderConfiguration::LoadOptionsConfiguration()
{
	IniFile ini;
	ini.Load(File::GetUserPath(F_DOLPHINCONFIG_IDX));
	std::string section = m_current_shader + "-options";

	for (auto& it : m_options)
	{
		switch (it.second.m_type)
		{
		case ConfigurationOption::OptionType::OPTION_BOOL:
			ini.GetOrCreateSection(section)->Get(it.second.m_option_name, &it.second.m_bool_value, it.second.m_bool_value);
		break;
		case ConfigurationOption::OptionType::OPTION_INTEGER:
		{
			std::string value;
			ini.GetOrCreateSection(section)->Get(it.second.m_option_name, &value);
			if (value != "")
				TryParseVector(value, &it.second.m_integer_values);
		}
		break;
		case ConfigurationOption::OptionType::OPTION_FLOAT:
		{
			std::string value;
			ini.GetOrCreateSection(section)->Get(it.second.m_option_name, &value);
			if (value != "")
				TryParseVector(value, &it.second.m_float_values);
		}
		break;
		case ConfigurationOption::OptionType::OPTION_INVALID: // Won't get hit
		break;
		}
	}
}

void PostProcessingShaderConfiguration::SaveOptionsConfiguration()
{
	IniFile ini;
	ini.Load(File::GetUserPath(F_DOLPHINCONFIG_IDX));
	std::string section = m_current_shader + "-options";

	for (auto& it : m_options)
	{
		switch (it.second.m_type)
		{
		case ConfigurationOption::OptionType::OPTION_BOOL:
		{
			ini.GetOrCreateSection(section)->Set(it.second.m_option_name, it.second.m_bool_value);
		}
		break;
		case ConfigurationOption::OptionType::OPTION_INTEGER:
		{
			std::string value = "";
			for (size_t i = 0; i < it.second.m_integer_values.size(); ++i)
				value += StringFromFormat("%d%s", it.second.m_integer_values[i], i == (it.second.m_integer_values.size() - 1) ? "": ", ");
			ini.GetOrCreateSection(section)->Set(it.second.m_option_name, value);
		}
		break;
		case ConfigurationOption::OptionType::OPTION_FLOAT:
		{
			std::string value = "";
			for (size_t i = 0; i < it.second.m_float_values.size(); ++i)
				value += StringFromFormat("%f%s", it.second.m_float_values[i], i == (it.second.m_float_values.size() - 1) ? "": ", ");
			ini.GetOrCreateSection(section)->Set(it.second.m_option_name, value);
		}
		break;
		case ConfigurationOption::OptionType::OPTION_INVALID: // Won't get hit
		break;
		}
	}
	ini.Save(File::GetUserPath(F_DOLPHINCONFIG_IDX));
}

void PostProcessingShaderConfiguration::ReloadShader()
{
	m_current_shader = "";
}

void PostProcessingShaderConfiguration::SetOptionf(std::string option, int index, float value)
{
	auto it = m_options.find(option);

	it->second.m_float_values[index] = value;
	it->second.m_dirty = true;
	m_any_options_dirty = true;
}

void PostProcessingShaderConfiguration::SetOptioni(std::string option, int index, s32 value)
{
	auto it = m_options.find(option);

	it->second.m_integer_values[index] = value;
	it->second.m_dirty = true;
	m_any_options_dirty = true;
}

void PostProcessingShaderConfiguration::SetOptionb(std::string option, bool value)
{
	auto it = m_options.find(option);

	it->second.m_bool_value = value;
	it->second.m_dirty = true;
	m_any_options_dirty = true;
}