Skip to content

Commit

Permalink
improve converting shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
exeldro committed May 27, 2024
1 parent cdeaf3e commit eabeb90
Showing 1 changed file with 124 additions and 23 deletions.
147 changes: 124 additions & 23 deletions obs-shaderfilter.c
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ static void convert_atan(struct dstr *effect_text)
comma = NULL;
if (divide && open && divide > open && divide < close)
divide = NULL;

open = strstr(close + 1, "(");
close = strstr(close + 1, ")");
}
Expand Down Expand Up @@ -1013,6 +1013,64 @@ static void convert_mat_mul(struct dstr *effect_text, char *var_type)
}
}

static bool is_in_function(struct dstr *effect_text, size_t diff)
{
char *pos = effect_text->array + diff;
int depth = 0;
while (depth >= 0) {
char *end = strstr(pos, "}");
char *begin = strstr(pos, "{");
if (end && begin && begin < end) {
pos = begin + 1;
depth++;
} else if (end && begin && end < begin) {
pos = end + 1;
depth--;
} else if (end) {
pos = end + 1;
depth--;
} else if (begin && depth == 0) {
break;
} else if (begin) {
pos = begin + 1;
depth++;
} else {
break;
}
}
return depth != 0;
}

static void convert_init(struct dstr *effect_text, char *name)
{
const size_t len = strlen(name);

char *pos = strstr(effect_text->array, name);
while (pos) {
size_t diff = pos - effect_text->array;

if (!is_in_function(effect_text, diff)) {
char *ch = pos + len;
while (ch != '\0' && (*ch == ' ' || *ch == '\t'))
ch++;
while (is_var_char(*ch))
ch++;
while (ch != '\0' && (*ch == ' ' || *ch == '\t'))
ch++;
if (*ch == '=' && *(ch + 1) != '=') {
char *begin = pos - 1;
while (begin > effect_text->array && (*begin == ' ' || *begin == '\t'))
begin--;
if (memcmp("uniform", begin - 6, 7) != 0 && memcmp("const", begin - 4, 5) != 0) {
dstr_insert(effect_text, begin - effect_text->array + 1, "uniform ");
diff += 8;
}
}
}
pos = strstr(effect_text->array + diff + len, name);
}
}

static void convert_vector_init(struct dstr *effect_text, char *name, int count)
{
const size_t len = strlen(name);
Expand All @@ -1023,15 +1081,14 @@ static void convert_vector_init(struct dstr *effect_text, char *name, int count)
char *ch = pos + len;
int depth = 0;
int function_depth = -1;
bool only_one = false;
bool only_one = true;
bool only_numbers = true;
bool only_float = true;
while (*ch != 0 && (only_numbers || only_float)) {
if (*ch == '(') {
depth++;
} else if (*ch == ')') {
if (depth == 0) {
only_one = true;
break;
}
depth--;
Expand All @@ -1041,7 +1098,6 @@ static void convert_vector_init(struct dstr *effect_text, char *name, int count)
} else if (*ch == ',') {
if (depth == 0) {
only_one = false;
break;
}
} else if (*ch == ';') {
only_one = false;
Expand Down Expand Up @@ -1163,29 +1219,65 @@ static void convert_vector_init(struct dstr *effect_text, char *name, int count)
}
ch++;
}
if (!only_one || (!only_numbers && !only_float)) {
pos = strstr(effect_text->array + diff + len, name);
continue;
}

//only 1 simple arg in the float4
struct dstr found = {0};
dstr_init(&found);
dstr_ncat(&found, pos, ch - pos + 1);
size_t end_diff = ch - effect_text->array;
if (count > 1 && only_one && (only_numbers || only_float)) {
//only 1 simple arg in the float4
struct dstr found = {0};
dstr_init(&found);
dstr_ncat(&found, pos, ch - pos + 1);

struct dstr replacement = {0};
dstr_init_copy(&replacement, name);
dstr_ncat(&replacement, pos + len, ch - (pos + len));
for (int i = 1; i < count; i++) {
dstr_cat(&replacement, ",");
struct dstr replacement = {0};
dstr_init_copy(&replacement, name);
dstr_ncat(&replacement, pos + len, ch - (pos + len));
}
dstr_cat(&replacement, ")");
for (int i = 1; i < count; i++) {
dstr_cat(&replacement, ",");
dstr_ncat(&replacement, pos + len, ch - (pos + len));
}
dstr_cat(&replacement, ")");

dstr_replace(effect_text, found.array, replacement.array);

dstr_replace(effect_text, found.array, replacement.array);
end_diff -= found.len;
end_diff += replacement.len;
dstr_free(&replacement);
dstr_free(&found);
}

dstr_free(&replacement);
dstr_free(&found);
if (!is_in_function(effect_text, diff)) {
char *begin = effect_text->array + diff - 1;
while (begin > effect_text->array && (*begin == ' ' || *begin == '\t'))
begin--;
if (*begin == '=') {
begin--;
while (begin > effect_text->array && (*begin == ' ' || *begin == '\t'))
begin--;
while (is_var_char(*begin))
begin--;
while (begin > effect_text->array && (*begin == ' ' || *begin == '\t'))
begin--;
if (memcmp(name, begin - len + 2, len - 1) == 0) {

begin -= len - 1;
while (begin > effect_text->array && (*begin == ' ' || *begin == '\t'))
begin--;
if (memcmp("uniform", begin - 6, 7) != 0 && memcmp("const", begin - 4, 5) != 0) {
dstr_insert(effect_text, begin - effect_text->array + 1, "uniform ");
diff += 8;
end_diff += 8;
}
if (effect_text->array[end_diff] == ')') {
if (count > 1) {
effect_text->array[end_diff] = '}';
dstr_remove(effect_text, diff, len);
dstr_insert(effect_text, diff, "{");
} else {
dstr_remove(effect_text, end_diff, 1);
dstr_remove(effect_text, diff, len);
}
}
}
}
}

pos = strstr(effect_text->array + diff + len, name);
}
Expand Down Expand Up @@ -1654,15 +1746,24 @@ static bool shader_filter_convert(obs_properties_t *props, obs_property_t *prope
dstr_replace(&effect_text, "mix(", "lerp(");
dstr_replace(&effect_text, "fract(", "frac(");

convert_init(&effect_text, "bool");
convert_init(&effect_text, "uint");
convert_init(&effect_text, "int");
convert_init(&effect_text, "float");

convert_vector_init(&effect_text, "bool(", 1);
convert_vector_init(&effect_text, "bool2(", 2);
convert_vector_init(&effect_text, "bool3(", 3);
convert_vector_init(&effect_text, "bool4(", 4);
convert_vector_init(&effect_text, "uint(", 1);
convert_vector_init(&effect_text, "uint2(", 2);
convert_vector_init(&effect_text, "uint3(", 3);
convert_vector_init(&effect_text, "uint4(", 4);
convert_vector_init(&effect_text, "int(", 1);
convert_vector_init(&effect_text, "int2(", 2);
convert_vector_init(&effect_text, "int3(", 3);
convert_vector_init(&effect_text, "int4(", 4);
convert_vector_init(&effect_text, "float(", 1);
convert_vector_init(&effect_text, "float2(", 2);
convert_vector_init(&effect_text, "float3(", 3);
convert_vector_init(&effect_text, "float4(", 4);
Expand Down

0 comments on commit eabeb90

Please sign in to comment.