Skip to content

Commit 5ba94c0

Browse files
committed
Complete removal of file reading logic
1 parent 7923ee3 commit 5ba94c0

12 files changed

Lines changed: 546 additions & 31 deletions

File tree

‎.gitignore‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@
22
out/
33
build/
44
CMakeSettings.json
5-
src/shaders/embedded_shaders.h

‎CMakePresets.7z‎

-17.2 KB
Binary file not shown.

‎dependencies/learnopengl/src/shader.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ Shader::Shader(const char* vertexPath, const char* fragmentPath, const char* geo
9999
}
100100

101101
void Shader::compile(const std::string& vertex_shader, const std::string& fragment_shader) {
102+
fragment_source = fragment_shader;
103+
vertex_source = vertex_shader;
102104
unsigned int vertex, fragment;
103105
// vertex shader
104106
const char* vertex_str = vertex_shader.c_str();

‎src/CMakeLists.txt‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ set(SHADERS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/shaders")
66
set(HEADER_BUILD "${CMAKE_CURRENT_BINARY_DIR}/shaders/embedded_shaders.h")
77
set(HEADER_SRC "${CMAKE_CURRENT_SOURCE_DIR}/shaders/embedded_shaders.h")
88

9-
file(GLOB SHADER_FILES "${CMAKE_CURRENT_SOURCE_DIR}/shaders/*")
9+
file(GLOB_RECURSE SHADER_FILES CONFIGURE_DEPENDS
10+
"${CMAKE_CURRENT_SOURCE_DIR}/shaders/*.vert"
11+
"${CMAKE_CURRENT_SOURCE_DIR}/shaders/*.frag"
12+
"${CMAKE_CURRENT_SOURCE_DIR}/shaders/*.geom"
13+
"${CMAKE_CURRENT_SOURCE_DIR}/shaders/*.glsl"
14+
)
1015

1116
add_custom_command(
1217
OUTPUT ${HEADER_BUILD} ${HEADER_SRC}
@@ -17,7 +22,7 @@ add_custom_command(
1722
-DDEST_SRC="${HEADER_SRC}"
1823
-P "${CMAKE_CURRENT_SOURCE_DIR}/scripts/embed_shaders.cmake"
1924

20-
DEPENDS ${SHADERS_DIR}
25+
DEPENDS ${SHADER_FILES}
2126
COMMENT "[Custom] Generating embedded_shaders.h in build and src folders..."
2227
)
2328

‎src/compiler/compiler_shader.cpp‎

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,8 @@
11
#include <compiler/compiler_shader.h>
2-
2+
#include <preprocessor/preprocessor.h>
33
CompilerShader::CompilerShader(const std::string& vertex_path, const std::string& frag_path, const bool use_vertex) {
4-
std::ifstream vfile;
5-
std::ifstream ffile;
6-
vfile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
7-
ffile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
8-
try {
9-
vfile.open(vertex_path);
10-
ffile.open(frag_path);
11-
std::stringstream v_ss, f_ss;
12-
13-
v_ss << vfile.rdbuf();
14-
f_ss << ffile.rdbuf();
15-
16-
vfile.close();
17-
ffile.close();
18-
19-
vert_source = v_ss.str();
20-
frag_source = f_ss.str();
21-
}
22-
catch (const std::ifstream::failure& e) {
23-
std::cerr << "Error: failed reading contents of file '" << vertex_path << "' or '" << frag_path << "'";
24-
}
4+
vert_source = get_source(vertex_path);
5+
frag_source = get_source(frag_path);
256
prepare_source(vert_source, frag_source, use_vertex);
267
}
278

‎src/graphics/graphics.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <vector>
77
#include <iostream>
88
#include <parser/parser.h>
9+
#include <preprocessor/preprocessor.h>
10+
#include <stdexcept>
911

1012
using std::string;
1113
GLFWwindow* initalize_window(const float width, const float height, const string windowname) {
@@ -44,4 +46,14 @@ void populate_texture(unsigned int& tbo_buffer, unsigned int& tbo_texture, const
4446
glGenTextures(1, &tbo_texture);
4547
glBindTexture(GL_TEXTURE_BUFFER, tbo_texture);
4648
glTexBuffer(GL_TEXTURE_BUFFER, GL_RG32F, tbo_buffer);
49+
}
50+
51+
void build_shader_source(Shader& shader, const std::string& vert_source, const std::string& frag_source) {
52+
shader.compile(vert_source, frag_source);
53+
}
54+
55+
void build_shader_path(Shader& shader, const std::string& vert_path, const std::string& frag_path) {
56+
const std::string vert_source = get_source(vert_path);
57+
const std::string frag_source = get_source(frag_path);
58+
return build_shader_source(shader, vert_source, frag_source);
4759
}

‎src/graphics/graphics.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <glad/glad.h>
33
#include <GLFW/glfw3.h>
44
#include <glm/glm.hpp>
5+
#include <shader/shader.h>
56
#include <string>
67
#include <vector>
78
GLFWwindow* initalize_window(const float width, const float height, const std::string windowname);
@@ -10,3 +11,7 @@ void populate_texture(unsigned int& tbo_buffer, unsigned int& tbo_texture, const
1011

1112
void populate_texture(unsigned int& tbo_buffer, unsigned int& tbo_texture, const std::vector<glm::vec2>& vec_data);
1213

14+
void build_shader_source(Shader& shader, const std::string& vert_source, const std::string& frag_source);
15+
16+
void build_shader_path(Shader& shader, const std::string& vert_path, const std::string& frag_path);
17+

‎src/main.cpp‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,18 @@ unsigned int constants_tbo_buffer, constants_tbo_texture;
4040

4141
int main() {
4242
GLFWwindow* window = initalize_window(WIDTH, HEIGHT, "Domain Coloring");
43-
preprocess("shaders/plotter.frag", operators);
43+
preprocess_string("shaders/plotter.frag", operators);
4444

45-
Shader shader_program("shaders/plotter.vert", "shaders/plotter.frag");
45+
Shader shader_program;
46+
build_shader_path(shader_program, "shaders/plotter.vert", "shaders/plotter.frag");
47+
48+
std::cout << get_source("shaders/plotter.frag");
49+
4650

4751
Shader shader_3d;
4852
const string frag_source = shader_program.fragment_source;
4953
string vert_source_3d = get_source("shaders/plotter3d.vert");
5054
const string frag_source_3d = get_source("shaders/plotter3d.frag");
51-
std::cout << vert_source_3d;
5255
try {
5356
vert_source_3d = build_shader_string(vert_source_3d, frag_source);
5457
std::cout << vert_source_3d;
@@ -80,6 +83,7 @@ int main() {
8083
float last_time = 0.0f;
8184
init_imgui(window);
8285
FunctionState function_state;
86+
function_state.current_shader = &shader_program;
8387

8488
Mesh grid_mesh = generate_grid_mesh(256);
8589

‎src/preprocessor/preprocessor.cpp‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,21 @@
22
#include <types/type_mapper.h>
33
#include <array>
44
#include <stdexcept>
5+
#include <shaders/embedded_shaders.h>
56

67
string get_source(const string& filename) {
8+
static std::map<string, string&> known_files = {
9+
{"shaders/plotter.frag",SRC_PLOTTER_FRAG},
10+
{"shaders/plotter.vert",SRC_PLOTTER_VERT},
11+
{"shaders/plotter3d.frag",SRC_PLOTTER3D_FRAG},
12+
{"shaders/plotter3d.vert",SRC_PLOTTER3D_VERT}
13+
};
14+
const auto it = known_files.find(filename);
15+
if (it != known_files.end()) {
16+
return it->second;
17+
}
18+
19+
720
std::ifstream file(filename);
821
if (!file.is_open()) {
922
throw std::runtime_error("Could not open source file '" + filename + "'");
@@ -14,7 +27,28 @@ string get_source(const string& filename) {
1427
}
1528

1629

17-
void preprocess(const string& filename, const vector<TokenOperator>& operators) {
30+
void preprocess_string(const string& filename, const vector<TokenOperator>& operators) {
31+
string preprocessed_string = get_preprocessor_string(operators);
32+
string raw_source = get_source(filename);
33+
initialize_map_id();
34+
const string marker = "START_WRITING_HERE HERE";
35+
size_t start_pos = raw_source.find(marker) + marker.length();
36+
if (start_pos == string::npos) {
37+
std::cerr << "Marker does not exist";
38+
return;
39+
}
40+
raw_source.insert(start_pos, "\n" + preprocessed_string + "\n");
41+
if (filename == "shaders/plotter.frag") {
42+
SRC_PLOTTER_FRAG = raw_source;
43+
}
44+
if (filename == "shaders/plotter.vert") SRC_PLOTTER_VERT = raw_source;
45+
if (filename == "shaders/plotter3d.vert") SRC_PLOTTER3D_VERT = raw_source;
46+
if (filename == "shaders/plotter3d.frag") SRC_PLOTTER3D_FRAG = raw_source;
47+
std::cout << raw_source;
48+
return;
49+
}
50+
51+
void preprocess_file(const string& filename, const vector<TokenOperator>& operators) {
1852
string preprocessed_string = get_preprocessor_string(operators);
1953
initialize_map_id();
2054
string raw_source = get_source(filename);

‎src/preprocessor/preprocessor.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
string get_source(const string& filename);
88

9-
void preprocess(const string& filename, const vector<TokenOperator>& operators);
9+
void preprocess_string(const string& filename, const vector<TokenOperator>& operators);
10+
11+
void preprocess_file(const string& filename, const vector<TokenOperator>& operators);
1012

1113
string build_shader_string(const string& new_shader, const string& origin);
1214

0 commit comments

Comments
 (0)