shaders: Clean up some of the OpenGL/slang code.

This commit is contained in:
BearOso 2023-04-27 16:20:09 -05:00
parent a74769c194
commit ed695f3776
3 changed files with 130 additions and 131 deletions

View File

@ -74,6 +74,7 @@ list(APPEND SOURCES src/gtk_display_driver_opengl.cpp
src/gtk_glx_context.cpp src/gtk_glx_context.cpp
../shaders/glsl.cpp ../shaders/glsl.cpp
../shaders/shader_helpers.cpp ../shaders/shader_helpers.cpp
../vulkan/slang_preset_ini.cpp
src/gtk_shader_parameters.cpp) src/gtk_shader_parameters.cpp)
if(USE_SLANG) if(USE_SLANG)
@ -101,7 +102,8 @@ if(USE_SLANG)
"VK_USE_PLATFORM_WAYLAND_KHR" "VK_USE_PLATFORM_WAYLAND_KHR"
"VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1" "VULKAN_HPP_DISPATCH_LOADER_DYNAMIC=1"
"VMA_DYNAMIC_VULKAN_FUNCTIONS=1" "VMA_DYNAMIC_VULKAN_FUNCTIONS=1"
"VMA_STATIC_VULKAN_FUNCTIONS=0") "VMA_STATIC_VULKAN_FUNCTIONS=0"
"IMGUI_IMPL_VULKAN_NO_PROTOTYPES")
list(APPEND INCLUDES ../external/vulkan-headers/include) list(APPEND INCLUDES ../external/vulkan-headers/include)
list(APPEND INCLUDES ../external/VulkanMemoryAllocator-Hpp/include) list(APPEND INCLUDES ../external/VulkanMemoryAllocator-Hpp/include)
list(APPEND INCLUDES ../external/stb) list(APPEND INCLUDES ../external/stb)
@ -112,8 +114,6 @@ if(USE_SLANG)
../vulkan/slang_shader.hpp ../vulkan/slang_shader.hpp
../vulkan/slang_preset.cpp ../vulkan/slang_preset.cpp
../vulkan/slang_preset.hpp ../vulkan/slang_preset.hpp
../vulkan/slang_preset_ini.cpp
../vulkan/slang_preset_ini.hpp
../vulkan/vulkan_hpp_storage.cpp ../vulkan/vulkan_hpp_storage.cpp
../vulkan/vk_mem_alloc_implementation.cpp ../vulkan/vk_mem_alloc_implementation.cpp
../vulkan/vulkan_context.cpp ../vulkan/vulkan_context.cpp
@ -133,21 +133,19 @@ if(USE_SLANG)
../vulkan/std_chrono_throttle.cpp ../vulkan/std_chrono_throttle.cpp
../vulkan/std_chrono_throttle.hpp ../vulkan/std_chrono_throttle.hpp
src/gtk_display_driver_vulkan.cpp src/gtk_display_driver_vulkan.cpp
src/gtk_display_driver_vulkan.h) src/gtk_display_driver_vulkan.h
../external/imgui/imgui_impl_vulkan.cpp)
endif() endif()
list(APPEND SOURCES ../external/imgui/imgui.cpp list(APPEND SOURCES ../external/imgui/imgui.cpp
../external/imgui/imgui_demo.cpp ../external/imgui/imgui_demo.cpp
../external/imgui/imgui_draw.cpp ../external/imgui/imgui_draw.cpp
../external/imgui/imgui_impl_vulkan.cpp
../external/imgui/imgui_impl_opengl3.cpp ../external/imgui/imgui_impl_opengl3.cpp
../external/imgui/imgui_tables.cpp ../external/imgui/imgui_tables.cpp
../external/imgui/imgui_widgets.cpp ../external/imgui/imgui_widgets.cpp
../external/imgui/snes9x_imgui.cpp) ../external/imgui/snes9x_imgui.cpp)
list(APPEND INCLUDES ../external/imgui) list(APPEND INCLUDES ../external/imgui)
list(APPEND DEFINES "IMGUI_IMPL_VULKAN_NO_PROTOTYPES")
if(USE_WAYLAND) if(USE_WAYLAND)
pkg_check_modules(WAYLAND REQUIRED wayland-client wayland-egl) pkg_check_modules(WAYLAND REQUIRED wayland-client wayland-egl)

View File

@ -8,11 +8,12 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <fstream> #include <fstream>
#include <map>
#include "glsl.h" #include "glsl.h"
#include "../../conffile.h"
#include "shader_helpers.h" #include "shader_helpers.h"
#include "shader_platform.h" #include "shader_platform.h"
static const GLfloat tex_coords[16] = { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, static const GLfloat tex_coords[16] = { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f }; 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f };
static const GLfloat mvp_ortho[16] = { 2.0f, 0.0f, 0.0f, 0.0f, static const GLfloat mvp_ortho[16] = { 2.0f, 0.0f, 0.0f, 0.0f,
@ -20,22 +21,48 @@ static const GLfloat mvp_ortho[16] = { 2.0f, 0.0f, 0.0f, 0.0f,
0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f,
-1.0f, -1.0f, 0.0f, 1.0f }; -1.0f, -1.0f, 0.0f, 1.0f };
static int scale_string_to_enum(const char *string) static std::vector<std::string> split_string(const std::string_view &str, unsigned char delim)
{ {
if (!strcasecmp(string, "source")) std::vector<std::string> tokens;
size_t pos = 0;
size_t index;
while (pos < str.length())
{ {
return GLSL_SOURCE; index = str.find(delim, pos);
if (index == std::string::npos)
{
if (pos < str.length())
{
tokens.push_back(std::string{str.substr(pos)});
}
break;
}
else if (index > pos)
{
tokens.push_back(std::string{str.substr(pos, index - pos)});
}
pos = index + 1;
} }
else if (!strcasecmp(string, "viewport"))
return tokens;
}
static int scale_string_to_enum(std::string string)
{
const struct { const char *string; int value; } map[] =
{ {
return GLSL_VIEWPORT; { "source", GLSL_SOURCE },
} { "viewport", GLSL_VIEWPORT },
else if (!strcasecmp(string, "absolute")) { "absolute", GLSL_ABSOLUTE }
{ };
return GLSL_ABSOLUTE;
} for (size_t i = 0; i < sizeof(map); i++)
else if (string == map[i].string)
return GLSL_NONE; return map[i].value;
return GLSL_NONE;
} }
static const char *scale_enum_to_string(int val) static const char *scale_enum_to_string(int val)
@ -128,10 +155,10 @@ bool GLSLShader::load_shader_preset_file(const char *filename)
} }
else else
{ {
conf.LoadFile(filename); ini.load_file(filename);
} }
int shader_count = conf.GetInt("::shaders", 0); int shader_count = ini.get_int("shaders", 0);
if (shader_count < 1) if (shader_count < 1)
return false; return false;
@ -142,20 +169,20 @@ bool GLSLShader::load_shader_preset_file(const char *filename)
{ {
GLSLPass pass; GLSLPass pass;
snprintf(key, 256, "::filter_linear%u", i); snprintf(key, 256, "filter_linear%u", i);
pass.filter = conf.Exists(key) ? (conf.GetBool(key) ? GL_LINEAR : GL_NEAREST) : GLSL_UNDEFINED; pass.filter = ini.exists(key) ? (ini.get_bool(key, true) ? GL_LINEAR : GL_NEAREST) : GLSL_UNDEFINED;
sprintf(key, "::scale_type%u", i); sprintf(key, "scale_type%u", i);
const char* scaleType = conf.GetString(key, ""); std::string scaleType = ini.get_string(key, "");
if (!strcasecmp(scaleType, "")) if (scaleType == "")
{ {
sprintf(key, "::scale_type_x%u", i); sprintf(key, "scale_type_x%u", i);
const char* scaleTypeX = conf.GetString(key, ""); std::string scaleTypeX = ini.get_string(key, "");
pass.scale_type_x = scale_string_to_enum(scaleTypeX); pass.scale_type_x = scale_string_to_enum(scaleTypeX);
sprintf(key, "::scale_type_y%u", i); sprintf(key, "scale_type_y%u", i);
const char* scaleTypeY = conf.GetString(key, ""); std::string scaleTypeY = ini.get_string(key, "");
pass.scale_type_y = scale_string_to_enum(scaleTypeY); pass.scale_type_y = scale_string_to_enum(scaleTypeY);
} }
else else
@ -165,89 +192,65 @@ bool GLSLShader::load_shader_preset_file(const char *filename)
pass.scale_type_y = scale_type; pass.scale_type_y = scale_type;
} }
sprintf(key, "::scale%u", i); sprintf(key, "scale%u", i);
const char* scaleFloat = conf.GetString(key, ""); auto scaleFloat = ini.get_float(key, 1.0f);
if (!strcasecmp(scaleFloat, "")) sprintf(key, "scale_x%u", i);
{ pass.scale_x = ini.get_float(key, scaleFloat);
sprintf(key, "::scale_x%u", i); sprintf(key, "scale_y%u", i);
const char* scaleFloatX = conf.GetString(key, "1.0"); pass.scale_y = ini.get_float(key, scaleFloat);
pass.scale_x = atof(scaleFloatX);
sprintf(key, "::scale_y%u", i);
const char* scaleFloatY = conf.GetString(key, "1.0");
pass.scale_y = atof(scaleFloatY);
}
else
{
pass.scale_x = pass.scale_y = atof(scaleFloat);
}
sprintf(key, "::shader%u", i); sprintf(key, "shader%u", i);
strcpy(pass.filename, conf.GetString(key, "")); strcpy(pass.filename, ini.get_string(key, "").c_str());
sprintf(key, "::wrap_mode%u", i); sprintf(key, "wrap_mode%u", i);
pass.wrap_mode = wrap_mode_string_to_enum (conf.GetString (key ,"")); pass.wrap_mode = wrap_mode_string_to_enum(ini.get_string (key ,"").c_str());
sprintf(key, "::mipmap_input%u", i); sprintf(key, "mipmap_input%u", i);
pass.mipmap_input = conf.GetBool (key); pass.mipmap_input = ini.get_bool(key, true);
sprintf(key, "::frame_count_mod%u", i); sprintf(key, "frame_count_mod%u", i);
pass.frame_count_mod = conf.GetInt(key, 0); pass.frame_count_mod = ini.get_int(key, 0);
pass.fp = false;
if (gl_float_texture_available()) if (gl_float_texture_available())
{ {
sprintf(key, "::float_framebuffer%u", i); sprintf(key, "float_framebuffer%u", i);
pass.fp = conf.GetBool(key); pass.fp = ini.get_bool(key, false);
} }
else
pass.fp = false;
pass.srgb = false;
if (gl_srgb_available()) if (gl_srgb_available())
{ {
sprintf(key, "::srgb_framebuffer%u", i); sprintf(key, "srgb_framebuffer%u", i);
pass.srgb = conf.GetBool(key); pass.srgb = ini.get_bool(key, false);
} }
else
pass.srgb = false;
sprintf(key, "::alias%u", i); sprintf(key, "alias%u", i);
strcpy(pass.alias, conf.GetString(key, "")); strcpy(pass.alias, ini.get_string(key, "").c_str());
this->pass.push_back(pass); this->pass.push_back(pass);
} }
char* ids = conf.GetStringDup("::textures", ""); auto ids_string = ini.get_string("textures", "");
auto ids = split_string(ids_string, ';');
char* id = strtok(ids, ";"); for (auto &id : ids)
while (id != NULL)
{ {
GLSLLut lut; GLSLLut lut;
sprintf(key, "::%s", id); strcpy(lut.id, id.c_str());
strcpy(lut.id, id); strcpy(lut.filename, ini.get_string(id, "").c_str());
strcpy(lut.filename, conf.GetString(key, ""));
sprintf(key, "::%s_wrap_mode", id); lut.wrap_mode = wrap_mode_string_to_enum(ini.get_string(id + "_wrap_mode", "").c_str());
lut.wrap_mode = wrap_mode_string_to_enum (conf.GetString (key, "")); lut.mipmap = ini.get_bool(id + "_mipmap", false);
lut.filter = (ini.get_bool(id + "_linear", false)) ? GL_LINEAR : GL_NEAREST;
sprintf(key, "::%s_mipmap", id);
lut.mipmap = conf.GetBool (key);
sprintf(key, "::%s_linear", id);
lut.filter = (conf.GetBool(key, false)) ? GL_LINEAR : GL_NEAREST;
if (lut.mipmap) if (lut.mipmap)
{
lut.filter = (lut.filter == GL_LINEAR) ? GL_LINEAR_MIPMAP_LINEAR : GL_NEAREST_MIPMAP_NEAREST; lut.filter = (lut.filter == GL_LINEAR) ? GL_LINEAR_MIPMAP_LINEAR : GL_NEAREST_MIPMAP_NEAREST;
}
this->lut.push_back(lut); this->lut.push_back(lut);
id = strtok(NULL, ";");
} }
free(ids);
return true; return true;
} }
@ -271,40 +274,45 @@ static std::string canonicalize(const std::string &noncanonical)
#ifdef USE_SLANG #ifdef USE_SLANG
static GLuint string_to_format(char *format) static GLuint string_to_format(char *format)
{ {
#define MATCH(s, f) \ // const struct { const char *string; int value; } map[] =
if (!strcmp(format, s)) \ // {
return f; const std::map<const char *, int> map =
MATCH("R8_UNORM", GL_R8); {
MATCH("R8_UINT", GL_R8UI); { "R8_UNORM", GL_R8 },
MATCH("R8_SINT", GL_R8I); { "R8_UINT", GL_R8UI },
MATCH("R8G8_UNORM", GL_RG8); { "R8_SINT", GL_R8I },
MATCH("R8G8_UINT", GL_RG8UI); { "R8G8_UNORM", GL_RG8 },
MATCH("R8G8_SINT", GL_RG8I); { "R8G8_UINT", GL_RG8UI },
MATCH("R8G8B8A8_UNORM", GL_RGBA8); { "R8G8_SINT", GL_RG8I },
MATCH("R8G8B8A8_UINT", GL_RGBA8UI); { "R8G8B8A8_UNORM", GL_RGBA8 },
MATCH("R8G8B8A8_SINT", GL_RGBA8I); { "R8G8B8A8_UINT", GL_RGBA8UI },
MATCH("R8G8B8A8_SRGB", GL_SRGB8_ALPHA8); { "R8G8B8A8_SINT", GL_RGBA8I },
{ "R8G8B8A8_SRGB", GL_SRGB8_ALPHA8 },
MATCH("R16_UINT", GL_R16UI); { "R16_UINT", GL_R16UI },
MATCH("R16_SINT", GL_R16I); { "R16_SINT", GL_R16I },
MATCH("R16_SFLOAT", GL_R16F); { "R16_SFLOAT", GL_R16F },
MATCH("R16G16_UINT", GL_RG16UI); { "R16G16_UINT", GL_RG16UI },
MATCH("R16G16_SINT", GL_RG16I); { "R16G16_SINT", GL_RG16I },
MATCH("R16G16_SFLOAT", GL_RG16F); { "R16G16_SFLOAT", GL_RG16F },
MATCH("R16G16B16A16_UINT", GL_RGBA16UI); { "R16G16B16A16_UINT", GL_RGBA16UI },
MATCH("R16G16B16A16_SINT", GL_RGBA16I); { "R16G16B16A16_SINT", GL_RGBA16I },
MATCH("R16G16B16A16_SFLOAT", GL_RGBA16F); { "R16G16B16A16_SFLOAT", GL_RGBA16F },
MATCH("R32_UINT", GL_R32UI); { "R32_UINT", GL_R32UI },
MATCH("R32_SINT", GL_R32I); { "R32_SINT", GL_R32I },
MATCH("R32_SFLOAT", GL_R32F); { "R32_SFLOAT", GL_R32F },
MATCH("R32G32_UINT", GL_RG32UI); { "R32G32_UINT", GL_RG32UI },
MATCH("R32G32_SINT", GL_RG32I); { "R32G32_SINT", GL_RG32I },
MATCH("R32G32_SFLOAT", GL_RG32F); { "R32G32_SFLOAT", GL_RG32F },
MATCH("R32G32B32A32_UINT", GL_RGBA32UI); { "R32G32B32A32_UINT", GL_RGBA32UI },
MATCH("R32G32B32A32_SINT", GL_RGBA32I); { "R32G32B32A32_SINT", GL_RGBA32I },
MATCH("R32G32B32A32_SFLOAT", GL_RGBA32F); { "R32G32B32A32_SFLOAT", GL_RGBA32F }
};
auto iter = map.find(format);
if (iter != map.end())
return iter->second;
return GL_RGBA; return GL_RGBA;
} }
#endif #endif
@ -652,18 +660,11 @@ bool GLSLShader::load_shader(const char *filename)
// Check for parameters specified in file // Check for parameters specified in file
for (unsigned int i = 0; i < param.size(); i++) for (unsigned int i = 0; i < param.size(); i++)
{ {
char key[266]; param[i].val = ini.get_float(param[i].id, param[i].val);
const char *value; if (param[i].val < param[i].min)
snprintf (key, 266, "::%s", param[i].id.c_str()); param[i].val = param[i].min;
value = conf.GetString (key, NULL); if (param[i].val > param[i].max)
if (value) param[i].val = param[i].max;
{
param[i].val = atof(value);
if (param[i].val < param[i].min)
param[i].val = param[i].min;
if (param[i].val > param[i].max)
param[i].val = param[i].max;
}
} }
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
@ -1309,5 +1310,4 @@ void GLSLShader::destroy()
pass.clear(); pass.clear();
lut.clear(); lut.clear();
prev_frame.clear(); prev_frame.clear();
conf.Clear();
} }

View File

@ -7,7 +7,8 @@
#ifndef __GLSL_H #ifndef __GLSL_H
#define __GLSL_H #define __GLSL_H
#include "../../conffile.h" #include "snes9x.h"
#include "../vulkan/slang_preset_ini.hpp"
#include "shader_platform.h" #include "shader_platform.h"
#include <deque> #include <deque>
#include <limits.h> #include <limits.h>
@ -172,7 +173,7 @@ struct GLSLShader
void destroy(); void destroy();
void register_uniforms(); void register_uniforms();
ConfigFile conf; IniFile ini;
std::vector<GLSLPass> pass; std::vector<GLSLPass> pass;
std::vector<GLSLLut> lut; std::vector<GLSLLut> lut;