2023-02-28 01:15:26 +00:00
|
|
|
// AUTOGENERATED COPYRIGHT HEADER START
|
|
|
|
// Copyright (C) 2019-2023 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
|
|
|
|
// Copyright (C) 2022 lainon <GermanAizek@yandex.ru>
|
|
|
|
// AUTOGENERATED COPYRIGHT HEADER END
|
2017-09-17 19:55:16 +00:00
|
|
|
|
2019-01-14 10:23:21 +00:00
|
|
|
#include "gs-effect.hpp"
|
2022-08-29 10:29:44 +00:00
|
|
|
#include "obs/gs/gs-helper.hpp"
|
|
|
|
#include "util/util-platform.hpp"
|
|
|
|
|
|
|
|
#include "warning-disable.hpp"
|
2019-01-31 01:30:18 +00:00
|
|
|
#include <fstream>
|
2021-11-07 13:37:05 +00:00
|
|
|
#include <sstream>
|
2019-04-02 22:16:13 +00:00
|
|
|
#include <stdexcept>
|
2019-01-31 01:30:18 +00:00
|
|
|
#include <vector>
|
2022-08-29 10:29:44 +00:00
|
|
|
#include "warning-enable.hpp"
|
2018-09-28 12:18:09 +00:00
|
|
|
|
2021-11-07 13:37:05 +00:00
|
|
|
#define MAX_EFFECT_SIZE 32 * 1024 * 1024 // 32 MiB, big enough for everything.
|
2019-01-31 01:30:18 +00:00
|
|
|
|
2022-07-21 11:09:10 +00:00
|
|
|
static std::string load_file_as_code(const std::filesystem::path& shader_file, bool is_top_level = true)
|
2018-09-28 12:18:09 +00:00
|
|
|
{
|
2022-08-19 05:58:45 +00:00
|
|
|
std::stringstream shader_stream;
|
|
|
|
const std::filesystem::path shader_path = std::filesystem::absolute(shader_file.native());
|
|
|
|
const std::filesystem::path shader_root = std::filesystem::path(shader_path.native()).remove_filename();
|
2021-11-07 13:37:05 +00:00
|
|
|
|
|
|
|
// Ensure it meets size limits.
|
|
|
|
uintmax_t size = std::filesystem::file_size(shader_path);
|
2019-12-15 10:13:47 +00:00
|
|
|
if (size > MAX_EFFECT_SIZE) {
|
|
|
|
throw std::runtime_error("File is too large to be loaded.");
|
2019-01-31 01:30:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 13:37:05 +00:00
|
|
|
// Try to open as-is.
|
|
|
|
std::ifstream ifs(shader_path, std::ios::in);
|
2019-12-15 10:13:47 +00:00
|
|
|
if (!ifs.is_open() || ifs.bad()) {
|
2021-11-07 13:37:05 +00:00
|
|
|
throw std::runtime_error("Failed to open file.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push Graphics API to shader.
|
2021-11-27 01:07:41 +00:00
|
|
|
if (is_top_level) {
|
2021-11-27 00:59:40 +00:00
|
|
|
auto gctx = streamfx::obs::gs::context();
|
|
|
|
switch (gs_get_device_type()) {
|
|
|
|
case GS_DEVICE_DIRECT3D_11:
|
|
|
|
shader_stream << "#define GS_DEVICE_DIRECT3D_11" << std::endl;
|
|
|
|
shader_stream << "#define GS_DEVICE_DIRECT3D" << std::endl;
|
|
|
|
break;
|
|
|
|
case GS_DEVICE_OPENGL:
|
|
|
|
shader_stream << "#define GS_DEVICE_OPENGL" << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
2019-01-31 01:30:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 13:37:05 +00:00
|
|
|
// Pre-process the shader.
|
|
|
|
std::string line;
|
|
|
|
while (std::getline(ifs, line)) {
|
|
|
|
std::string line_trimmed = line;
|
|
|
|
|
|
|
|
{ // Figure out the length of the trim.
|
|
|
|
size_t trim_length = 0;
|
|
|
|
for (size_t idx = 0, edx = line_trimmed.length(); idx < edx; idx++) {
|
|
|
|
char ch = line_trimmed.at(idx);
|
|
|
|
if ((ch != ' ') && (ch != '\t')) {
|
|
|
|
trim_length = idx;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (trim_length > 0) {
|
|
|
|
line_trimmed.erase(0, trim_length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle '#include'
|
|
|
|
if (line_trimmed.substr(0, 8) == "#include") {
|
|
|
|
std::string include_str = line_trimmed.substr(10, line_trimmed.size() - 11); // '#include "'
|
|
|
|
std::filesystem::path include_path = include_str;
|
|
|
|
|
|
|
|
if (!include_path.is_absolute()) {
|
|
|
|
include_path = shader_root / include_str;
|
|
|
|
}
|
|
|
|
|
2021-11-27 01:07:41 +00:00
|
|
|
line = load_file_as_code(include_path, false);
|
2021-11-07 13:37:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
shader_stream << line << std::endl;
|
|
|
|
}
|
2019-01-31 01:30:18 +00:00
|
|
|
|
2021-11-07 13:37:05 +00:00
|
|
|
return shader_stream.str();
|
2017-09-17 19:55:16 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 11:09:10 +00:00
|
|
|
streamfx::obs::gs::effect::effect(std::string_view code, std::string_view name)
|
2018-09-28 12:18:09 +00:00
|
|
|
{
|
2021-06-08 02:38:24 +00:00
|
|
|
auto gctx = streamfx::obs::gs::context();
|
2019-08-04 20:44:46 +00:00
|
|
|
|
2019-12-15 10:13:47 +00:00
|
|
|
char* error_buffer = nullptr;
|
2022-07-21 11:09:10 +00:00
|
|
|
gs_effect_t* effect = gs_effect_create(code.data(), name.data(), &error_buffer);
|
2019-08-04 20:44:46 +00:00
|
|
|
|
2019-12-15 10:13:47 +00:00
|
|
|
if (!effect) {
|
2023-05-13 12:35:46 +00:00
|
|
|
throw error_buffer ? std::runtime_error(error_buffer) : std::runtime_error("Unknown error during effect compile.");
|
2019-08-04 20:44:46 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 22:07:49 +00:00
|
|
|
reset(effect, [](gs_effect_t* ptr) { gs_effect_destroy(ptr); });
|
2019-08-04 20:44:46 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 12:35:46 +00:00
|
|
|
streamfx::obs::gs::effect::effect(std::filesystem::path file) : effect(load_file_as_code(file), streamfx::util::platform::utf8_to_native(std::filesystem::absolute(file)).generic_u8string()) {}
|
2019-08-04 20:44:46 +00:00
|
|
|
|
2021-06-08 02:38:24 +00:00
|
|
|
streamfx::obs::gs::effect::~effect()
|
2018-09-28 12:18:09 +00:00
|
|
|
{
|
2021-06-08 02:38:24 +00:00
|
|
|
auto gctx = streamfx::obs::gs::context();
|
2019-12-16 22:07:49 +00:00
|
|
|
reset();
|
2017-09-17 19:55:16 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:38:24 +00:00
|
|
|
std::size_t streamfx::obs::gs::effect::count_techniques()
|
2019-08-04 20:44:46 +00:00
|
|
|
{
|
2019-12-15 10:13:47 +00:00
|
|
|
return static_cast<size_t>(get()->techniques.num);
|
2019-08-04 20:44:46 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:38:24 +00:00
|
|
|
streamfx::obs::gs::effect_technique streamfx::obs::gs::effect::get_technique(std::size_t idx)
|
2019-08-04 20:44:46 +00:00
|
|
|
{
|
2019-12-15 10:13:47 +00:00
|
|
|
if (idx >= count_techniques()) {
|
|
|
|
return nullptr;
|
2019-08-04 20:44:46 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:38:24 +00:00
|
|
|
return streamfx::obs::gs::effect_technique(get()->techniques.array + idx, *this);
|
2019-08-04 20:44:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 11:09:10 +00:00
|
|
|
streamfx::obs::gs::effect_technique streamfx::obs::gs::effect::get_technique(std::string_view name)
|
2019-08-04 20:44:46 +00:00
|
|
|
{
|
2020-04-08 21:38:42 +00:00
|
|
|
for (std::size_t idx = 0; idx < count_techniques(); idx++) {
|
2019-12-15 10:13:47 +00:00
|
|
|
auto ptr = get()->techniques.array + idx;
|
2022-07-21 11:09:10 +00:00
|
|
|
if (strcmp(ptr->name, name.data()) == 0) {
|
2021-06-08 02:38:24 +00:00
|
|
|
return streamfx::obs::gs::effect_technique(ptr, *this);
|
2019-12-15 10:13:47 +00:00
|
|
|
}
|
2019-08-04 20:44:46 +00:00
|
|
|
}
|
|
|
|
|
2019-12-15 10:13:47 +00:00
|
|
|
return nullptr;
|
2017-09-17 19:55:16 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 11:09:10 +00:00
|
|
|
bool streamfx::obs::gs::effect::has_technique(std::string_view name)
|
2018-09-28 12:18:09 +00:00
|
|
|
{
|
2019-12-15 10:13:47 +00:00
|
|
|
if (get_technique(name))
|
|
|
|
return true;
|
|
|
|
return false;
|
2017-09-17 19:55:16 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:38:24 +00:00
|
|
|
std::size_t streamfx::obs::gs::effect::count_parameters()
|
2019-08-04 20:44:46 +00:00
|
|
|
{
|
2019-12-15 10:13:47 +00:00
|
|
|
return get()->params.num;
|
2019-08-04 20:44:46 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:38:24 +00:00
|
|
|
streamfx::obs::gs::effect_parameter streamfx::obs::gs::effect::get_parameter(std::size_t idx)
|
2019-08-04 20:44:46 +00:00
|
|
|
{
|
2019-12-15 10:13:47 +00:00
|
|
|
if (idx >= count_parameters()) {
|
|
|
|
throw std::out_of_range("Index is out of range.");
|
2019-08-04 20:44:46 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 02:38:24 +00:00
|
|
|
return streamfx::obs::gs::effect_parameter(get()->params.array + idx, *this);
|
2019-08-04 20:44:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 11:09:10 +00:00
|
|
|
streamfx::obs::gs::effect_parameter streamfx::obs::gs::effect::get_parameter(std::string_view name)
|
2019-08-04 20:44:46 +00:00
|
|
|
{
|
2020-04-08 21:38:42 +00:00
|
|
|
for (std::size_t idx = 0; idx < count_parameters(); idx++) {
|
2019-12-15 10:13:47 +00:00
|
|
|
auto ptr = get()->params.array + idx;
|
2022-07-21 11:09:10 +00:00
|
|
|
if (strcmp(ptr->name, name.data()) == 0) {
|
2021-06-08 02:38:24 +00:00
|
|
|
return streamfx::obs::gs::effect_parameter(ptr, *this);
|
2019-12-15 10:13:47 +00:00
|
|
|
}
|
2019-08-04 20:44:46 +00:00
|
|
|
}
|
|
|
|
|
2019-12-15 10:13:47 +00:00
|
|
|
return nullptr;
|
2019-08-04 20:44:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 11:09:10 +00:00
|
|
|
bool streamfx::obs::gs::effect::has_parameter(std::string_view name)
|
2019-08-04 20:44:46 +00:00
|
|
|
{
|
2019-12-15 10:13:47 +00:00
|
|
|
if (get_parameter(name))
|
2019-08-04 21:12:30 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
2019-08-04 20:44:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 11:09:10 +00:00
|
|
|
bool streamfx::obs::gs::effect::has_parameter(std::string_view name, effect_parameter::type type)
|
2019-08-04 20:44:46 +00:00
|
|
|
{
|
2019-12-15 10:13:47 +00:00
|
|
|
auto eprm = get_parameter(name);
|
2019-08-04 21:12:30 +00:00
|
|
|
if (eprm)
|
2019-12-15 10:13:47 +00:00
|
|
|
return eprm.get_type() == type;
|
2019-08-04 21:12:30 +00:00
|
|
|
return false;
|
2019-08-04 20:44:46 +00:00
|
|
|
}
|