(shaders slang) Cut down on code duplication

This commit is contained in:
twinaphex 2020-08-03 18:13:08 +02:00
parent 4465a7bd0b
commit cce2ac6904
4 changed files with 34 additions and 69 deletions

View file

@ -39,22 +39,6 @@
using namespace std;
template <typename P>
static bool gl_core_shader_set_unique_map(unordered_map<string, P> &m,
const string &name, const P &p)
{
auto itr = m.find(name);
if (itr != end(m))
{
RARCH_ERR("[slang]: Alias \"%s\" already exists.\n",
name.c_str());
return false;
}
m[name] = p;
return true;
}
GLuint gl_core_cross_compile_program(
const uint32_t *vertex, size_t vertex_size,
const uint32_t *fragment, size_t fragment_size,
@ -875,7 +859,7 @@ bool Pass::build()
for (i = 0; i < parameters.size(); i++)
{
if (!gl_core_shader_set_unique_map(semantic_map, parameters[i].id,
if (!slang_set_unique_map(semantic_map, parameters[i].id,
slang_semantic_map{ SLANG_SEMANTIC_FLOAT_PARAMETER, j }))
return false;
j++;
@ -1919,21 +1903,21 @@ bool gl_core_filter_chain::init_alias()
j = &passes[i] - passes.data();
if (!gl_core_shader_set_unique_map(common.texture_semantic_map, name,
if (!slang_set_unique_map(common.texture_semantic_map, name,
slang_texture_semantic_map{ SLANG_TEXTURE_SEMANTIC_PASS_OUTPUT, j }))
return false;
if (!gl_core_shader_set_unique_map(common.texture_semantic_uniform_map,
if (!slang_set_unique_map(common.texture_semantic_uniform_map,
name + "Size",
slang_texture_semantic_map{ SLANG_TEXTURE_SEMANTIC_PASS_OUTPUT, j }))
return false;
if (!gl_core_shader_set_unique_map(common.texture_semantic_map,
if (!slang_set_unique_map(common.texture_semantic_map,
name + "Feedback",
slang_texture_semantic_map{ SLANG_TEXTURE_SEMANTIC_PASS_FEEDBACK, j }))
return false;
if (!gl_core_shader_set_unique_map(common.texture_semantic_uniform_map,
if (!slang_set_unique_map(common.texture_semantic_uniform_map,
name + "FeedbackSize",
slang_texture_semantic_map{ SLANG_TEXTURE_SEMANTIC_PASS_FEEDBACK, j }))
return false;
@ -1942,12 +1926,12 @@ bool gl_core_filter_chain::init_alias()
for (i = 0; i < common.luts.size(); i++)
{
j = &common.luts[i] - common.luts.data();
if (!gl_core_shader_set_unique_map(common.texture_semantic_map,
if (!slang_set_unique_map(common.texture_semantic_map,
common.luts[i]->get_id(),
slang_texture_semantic_map{ SLANG_TEXTURE_SEMANTIC_USER, j }))
return false;
if (!gl_core_shader_set_unique_map(common.texture_semantic_uniform_map,
if (!slang_set_unique_map(common.texture_semantic_uniform_map,
common.luts[i]->get_id() + "Size",
slang_texture_semantic_map{ SLANG_TEXTURE_SEMANTIC_USER, j }))
return false;

View file

@ -431,22 +431,6 @@ struct vulkan_filter_chain
void update_history_info();
};
template <typename P>
static bool vk_shader_set_unique_map(unordered_map<string, P> &m,
const string &name, const P &p)
{
auto itr = m.find(name);
if (itr != end(m))
{
RARCH_ERR("[slang]: Alias \"%s\" already exists.\n",
name.c_str());
return false;
}
m[name] = p;
return true;
}
static unsigned num_miplevels(unsigned width, unsigned height)
{
unsigned size = MAX(width, height);
@ -1165,7 +1149,6 @@ bool vulkan_filter_chain::init_feedback()
return true;
}
bool vulkan_filter_chain::init_alias()
{
unsigned i, j;
@ -1180,22 +1163,22 @@ bool vulkan_filter_chain::init_alias()
j = &passes[i] - passes.data();
if (!vk_shader_set_unique_map(
if (!slang_set_unique_map(
common.texture_semantic_map, name,
slang_texture_semantic_map{ SLANG_TEXTURE_SEMANTIC_PASS_OUTPUT, j }))
return false;
if (!vk_shader_set_unique_map(
if (!slang_set_unique_map(
common.texture_semantic_uniform_map, name + "Size",
slang_texture_semantic_map{ SLANG_TEXTURE_SEMANTIC_PASS_OUTPUT, j }))
return false;
if (!vk_shader_set_unique_map(
if (!slang_set_unique_map(
common.texture_semantic_map, name + "Feedback",
slang_texture_semantic_map{ SLANG_TEXTURE_SEMANTIC_PASS_FEEDBACK, j }))
return false;
if (!vk_shader_set_unique_map(
if (!slang_set_unique_map(
common.texture_semantic_uniform_map, name + "FeedbackSize",
slang_texture_semantic_map{ SLANG_TEXTURE_SEMANTIC_PASS_FEEDBACK, j }))
return false;
@ -1204,13 +1187,13 @@ bool vulkan_filter_chain::init_alias()
for (i = 0; i < common.luts.size(); i++)
{
j = &common.luts[i] - common.luts.data();
if (!vk_shader_set_unique_map(
if (!slang_set_unique_map(
common.texture_semantic_map,
common.luts[i]->get_id(),
slang_texture_semantic_map{ SLANG_TEXTURE_SEMANTIC_USER, j }))
return false;
if (!vk_shader_set_unique_map(
if (!slang_set_unique_map(
common.texture_semantic_uniform_map,
common.luts[i]->get_id() + "Size",
slang_texture_semantic_map{ SLANG_TEXTURE_SEMANTIC_USER, j }))
@ -1996,7 +1979,8 @@ bool Pass::build()
for (i = 0; i < parameters.size(); i++)
{
if (!vk_shader_set_unique_map(semantic_map, parameters[i].id,
if (!slang_set_unique_map(
semantic_map, parameters[i].id,
slang_semantic_map{ SLANG_SEMANTIC_FLOAT_PARAMETER, j }))
return false;
j++;

View file

@ -36,21 +36,6 @@ using namespace spirv_cross;
#endif
using namespace std;
template <typename P>
static bool set_unique_map(unordered_map<string, P>& m,
const string& name, const P& p)
{
auto itr = m.find(name);
if (itr != end(m))
{
RARCH_ERR("[slang]: Alias \"%s\" already exists.\n", name.c_str());
return false;
}
m[name] = p;
return true;
}
template <typename M, typename S>
static string get_semantic_name(const unordered_map<string, M>* map,
S semantic, unsigned index)
@ -139,25 +124,25 @@ static bool slang_process_reflection(
string name = shader_info->pass[i].alias;
if (!set_unique_map(
if (!slang_set_unique_map(
texture_semantic_map, name,
slang_texture_semantic_map{
SLANG_TEXTURE_SEMANTIC_PASS_OUTPUT, i }))
return false;
if (!set_unique_map(
if (!slang_set_unique_map(
texture_semantic_uniform_map, name + "Size",
slang_texture_semantic_map{
SLANG_TEXTURE_SEMANTIC_PASS_OUTPUT, i }))
return false;
if (!set_unique_map(
if (!slang_set_unique_map(
texture_semantic_map, name + "Feedback",
slang_texture_semantic_map{
SLANG_TEXTURE_SEMANTIC_PASS_FEEDBACK, i }))
return false;
if (!set_unique_map(
if (!slang_set_unique_map(
texture_semantic_uniform_map, name + "FeedbackSize",
slang_texture_semantic_map{
SLANG_TEXTURE_SEMANTIC_PASS_FEEDBACK, i }))
@ -166,13 +151,13 @@ static bool slang_process_reflection(
for (i = 0; i < shader_info->luts; i++)
{
if (!set_unique_map(
if (!slang_set_unique_map(
texture_semantic_map, shader_info->lut[i].id,
slang_texture_semantic_map{
SLANG_TEXTURE_SEMANTIC_USER, i }))
return false;
if (!set_unique_map(
if (!slang_set_unique_map(
texture_semantic_uniform_map,
string(shader_info->lut[i].id) + "Size",
slang_texture_semantic_map{
@ -184,7 +169,7 @@ static bool slang_process_reflection(
for (i = 0; i < shader_info->num_parameters; i++)
{
if (!set_unique_map(
if (!slang_set_unique_map(
uniform_semantic_map, shader_info->parameters[i].id,
slang_semantic_map{ SLANG_SEMANTIC_FLOAT_PARAMETER, i }))
return false;

View file

@ -79,6 +79,18 @@ struct slang_reflection
unsigned pass_number = 0;
};
template <typename P>
static bool slang_set_unique_map(std::unordered_map<std::string, P> &m,
const std::string &name, const P &p)
{
auto itr = m.find(name);
/* Alias already exists? */
if (itr != end(m))
return false;
m[name] = p;
return true;
}
bool slang_reflect_spirv(
const std::vector<uint32_t> &vertex,
const std::vector<uint32_t> &fragment,