refactor: split frag0 into multiple files

This commit is contained in:
2025-11-08 22:35:10 +01:00
parent b4b79cea12
commit 248c947fad
31 changed files with 2041 additions and 1875 deletions
+1 -1
View File
@@ -70,7 +70,7 @@ void config_file_read(ConfigFile *config, char *path) {
config->map = hashmap_new(sizeof(ConfigFileItem), 0, 0, 0, item_hash,
item_compare, NULL, NULL);
file = file_read(path);
file_read(&file, path);
if (file.error) {
return;
+24 -8
View File
@@ -67,16 +67,32 @@ bool file_update(File *file) {
return true;
}
File file_read(char *path) {
File file;
void file_read(File *file, char *path) {
strncpy(file->path, path, STR_LEN);
file->content = NULL;
file->error = false;
file->last_write = 0;
strncpy(file.path, path, STR_LEN);
file.content = NULL;
file.error = false;
file.last_write = 0;
file_update(file);
}
file_update(&file);
return file;
void file_dump(char *path, char *content) {
FILE *file_pointer;
log_info("Dumping %s...", path);
// open file
file_pointer = fopen(path, "w");
if (file_pointer == NULL) {
log_warn("Cannot open file '%s'", path);
return;
}
// write file
fprintf(file_pointer, "%s", content);
// close file
fclose(file_pointer);
}
void file_write(char *path, StringArray *lines) {
+3 -1
View File
@@ -3,7 +3,7 @@
#ifndef FILE_H
#define FILE_H
File file_read(char *path);
void file_read(File *file, char *path);
bool file_should_update(File *file);
@@ -11,6 +11,8 @@ bool file_update(File *file);
void file_write(char *path, StringArray *lines);
void file_dump(char *path, char *content);
void file_free(File *file);
#endif /* FILE_H */
+14 -12
View File
@@ -11,30 +11,29 @@
#include "string.h"
static bool parse_fragment_shader_file(Project *project, unsigned int i) {
File tmp_file, fragment_shader;
File tmp_file;
char file_path[STR_LEN];
char *include_pos, *include_end;
char included_file[STR_LEN];
char *new_content;
fragment_shader = project->fragment_shaders[i][0];
project->sub_counts.values[i] = 0;
include_pos = strstr(fragment_shader.content, "#include ");
include_pos = strstr(project->fragment_shaders[i][0].content, "\n#include ");
while (include_pos != NULL && project->sub_counts.values[i] < MAX_SUB_FILE) {
include_end = strstr(include_pos, "\n");
include_end = strstr(include_pos + 1, "\n");
if (include_end == NULL) {
log_error("Invalid '#include' directive in '%s'", fragment_shader.path);
log_error("Invalid '#include' directive in '%s'",
project->fragment_shaders[i][0].path);
return false;
}
strlcpy(included_file, include_pos + 9, include_end - include_pos - 8);
strlcpy(included_file, include_pos + 10, include_end - include_pos - 9);
snprintf(file_path, STR_LEN, "%s/%s", project->path, included_file);
tmp_file = file_read(file_path);
file_read(&tmp_file, file_path);
if (tmp_file.error) {
return false;
@@ -43,14 +42,17 @@ static bool parse_fragment_shader_file(Project *project, unsigned int i) {
project->fragment_shaders[i][++project->sub_counts.values[i]] = tmp_file;
new_content = string_replace_at(
fragment_shader.content, include_pos - fragment_shader.content,
include_end - fragment_shader.content, tmp_file.content);
project->fragment_shaders[i][0].content,
include_pos + 1 - project->fragment_shaders[i][0].content,
include_end - project->fragment_shaders[i][0].content,
tmp_file.content);
project->fragment_shaders[i][0].content = new_content;
file_free(&tmp_file);
include_pos = strstr(fragment_shader.content, "#include ");
include_pos =
strstr(project->fragment_shaders[i][0].content, "\n#include ");
}
return true;
@@ -63,7 +65,7 @@ static bool read_fragment_shader_file(Project *project, char *frag_prefix,
snprintf(file_path, STR_LEN, "%s/%s%d.glsl", project->path, frag_prefix,
i + 1);
project->fragment_shaders[i][0] = file_read(file_path);
file_read(&project->fragment_shaders[i][0], file_path);
if (project->fragment_shaders[i][0].error) {
return false;
+16 -6
View File
@@ -8,6 +8,7 @@
#include "config.h"
#include "config_file.h"
#include "constants.h"
#include "file.h"
#include "shaders.h"
#define GLAD_GL_IMPLEMENTATION
@@ -206,6 +207,7 @@ static bool compile_shader(GLuint shader_id, char *name, char *source_code) {
if (status_params == GL_FALSE) {
log_error("Failed to compile\n%s", log);
file_dump("error.glsl", source_code);
} else {
log_info("Compilation successful");
}
@@ -479,7 +481,7 @@ static void write_uniform_multi_3f(GLuint location, unsigned int count,
static void use_program(ShaderProgram *program, int i, bool output,
SharedContext *context) {
unsigned int j, k, offset;
unsigned int j, k, offset, subcount;
GLuint subroutines[ARRAY_SIZE];
// use specific shader program
glUseProgram(program->programs[i]);
@@ -547,14 +549,22 @@ static void use_program(ShaderProgram *program, int i, bool output,
// set subroutines for fragment and update state uniforms
k = context->state.values[i];
subcount = 0;
for (j = 0; j < program->sub_type_count; j++) {
subroutines[j] = program->sub_locations[i * program->sub_type_count *
program->sub_variant_count +
j * program->sub_variant_count + k];
if (program->sub_locations[i * program->sub_variant_count *
program->sub_type_count +
j * program->sub_variant_count + k] !=
unused_uniform) {
subroutines[subcount++] =
program->sub_locations[i * program->sub_variant_count *
program->sub_type_count +
j * program->sub_variant_count + k];
}
}
glUniformSubroutinesuiv(GL_FRAGMENT_SHADER, program->sub_type_count,
subroutines);
if (subcount > 0) {
glUniformSubroutinesuiv(GL_FRAGMENT_SHADER, subcount, subroutines);
}
// set GL_TEXTURE(X) to uniform sampler2D texX
for (j = 0; j < program->tex_count; j++) {
+2 -3
View File
@@ -60,12 +60,11 @@ char *string_replace_at(char *src, unsigned int from, unsigned int to,
src_len = strlen(src);
rpl_len = strlen(rpl);
dst =
malloc(src_len - (to - from) + rpl_len + 1); // +1 for the null-terminator
dst = malloc(src_len - (to - from) + rpl_len + 1);
strncpy(dst, src, from);
strncpy(dst + from, rpl, rpl_len);
strncpy(dst + from + rpl_len, src + to, src_len - to);
strlcpy(dst + from + rpl_len, src + to, src_len - to + 1);
return dst;
}