Skip to content

Commit 8f40852

Browse files
committed
CompilerShader class done
1 parent 5f38f07 commit 8f40852

6 files changed

Lines changed: 193 additions & 42 deletions

File tree

‎dependencies/learnopengl/include/shader/shader.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ class Shader
1313
// ------------------------------------------------------------------------
1414
// constructor generates the shader on the fly
1515
// ------------------------------------------------------------------------
16+
Shader() = default;
17+
1618
Shader(const char* vertexPath, const char* fragmentPath, const char* geometryPath = nullptr);
19+
20+
void compile(const std::string& vertex_source, const std::string& fragment_source);
1721
// activate the shader
1822
// ------------------------------------------------------------------------
1923
// activate the shader

‎dependencies/learnopengl/src/shader.cpp‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,30 @@ Shader::Shader(const char* vertexPath, const char* fragmentPath, const char* geo
9696

9797
}
9898

99+
void Shader::compile(const std::string& vertex_shader, const std::string& fragment_shader) {
100+
unsigned int vertex, fragment;
101+
// vertex shader
102+
const char* vertex_str = vertex_shader.c_str();
103+
const char* fragment_str = fragment_shader.c_str();
104+
vertex = glCreateShader(GL_VERTEX_SHADER);
105+
glShaderSource(vertex, 1, &vertex_str, NULL);
106+
glCompileShader(vertex);
107+
checkCompileErrors(vertex, "VERTEX");
108+
// fragment Shader
109+
fragment = glCreateShader(GL_FRAGMENT_SHADER);
110+
glShaderSource(fragment, 1, &fragment_str, NULL);
111+
glCompileShader(fragment);
112+
checkCompileErrors(fragment, "FRAGMENT");
113+
// shader Program
114+
ID = glCreateProgram();
115+
glAttachShader(ID, vertex);
116+
glAttachShader(ID, fragment);
117+
glLinkProgram(ID);
118+
checkCompileErrors(ID, "PROGRAM");
119+
glDeleteShader(vertex);
120+
glDeleteShader(fragment);
121+
}
122+
99123
// activate the shader
100124
// ------------------------------------------------------------------------
101125
// activate the shader

‎src/CMakeLists.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
set(SOURCE_FILES
22
"graphics/graphics.cpp"
33
"graphics/graphics.h"
4-
"types/types.h" "parser/tokenizer.h" "parser/checker.h" "parser/rpn.h" "parser/parser.h" "parser/checker.cpp" "parser/parser.cpp" "parser/rpn.cpp" "parser/tokenizer.cpp" "parser/simplifier.h" "parser/simplifier.cpp" "transformer/constant.h" "transformer/constant.cpp" "preprocessor/preprocessor.h" "preprocessor/string_builder.h" "transformer/operator.h" "transformer/operator.cpp" "transformer/transformer.h" "transformer/transformer.cpp" "preprocessor/string_builder.cpp" "preprocessor/preprocessor.cpp" "interactions/interactions.h" "interactions/interactions.cpp" "parser/validator.cpp" "parser/validator.h" "graphics/ui_init.h" "graphics/ui_init.cpp" "graphics/ui.h" "graphics/ui.cpp" "types/type_mapper.h" "types/type_mapper.cpp")
4+
"types/types.h" "parser/tokenizer.h" "parser/checker.h" "parser/rpn.h" "parser/parser.h" "parser/checker.cpp" "parser/parser.cpp" "parser/rpn.cpp" "parser/tokenizer.cpp" "parser/simplifier.h" "parser/simplifier.cpp" "transformer/constant.h" "transformer/constant.cpp" "preprocessor/preprocessor.h" "preprocessor/string_builder.h" "transformer/operator.h" "transformer/operator.cpp" "transformer/transformer.h" "transformer/transformer.cpp" "preprocessor/string_builder.cpp" "preprocessor/preprocessor.cpp" "interactions/interactions.h" "interactions/interactions.cpp" "parser/validator.cpp" "parser/validator.h" "graphics/ui_init.h" "graphics/ui_init.cpp" "graphics/ui.h" "graphics/ui.cpp" "types/type_mapper.h" "types/type_mapper.cpp" "compiler/compiler_shader.h" "compiler/compiler_shader.cpp")
55

66
add_executable(complex-plotter ${SOURCE_FILES} "main.cpp")
77
find_package(OpenGL REQUIRED)

‎src/compiler/compiler_shader.cpp‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <compiler/compiler_shader.h>
2+
3+
inline CompilerShader::CompilerShader(const std::string& vertex_path, const std::string& frag_path) {
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+
}
25+
clean_frag_source();
26+
find_injection_point();
27+
}
28+
29+
inline void CompilerShader::compile(const std::string glsl_expression) {
30+
inject(glsl_expression);
31+
shader.compile(vert_source, frag_source);
32+
}
33+
34+
inline size_t CompilerShader::find_end(const std::string& source, const std::string& substr, const size_t start) {
35+
size_t pos = source.find(substr, start);
36+
if (pos == std::string::npos) {
37+
return std::string::npos;
38+
}
39+
if (substr.empty()) {
40+
return pos;
41+
}
42+
return pos + substr.size();
43+
}
44+
45+
inline void CompilerShader::clean_frag_source() {
46+
constexpr std::array kill_these_guys = { "INTERPRETER_SPECIFIC_FUNCTIONS","INTERPRETER_ASSIGNEMENT" };
47+
size_t prev_end = 0;
48+
for (const std::string& s : kill_these_guys) {
49+
const size_t start = frag_source.find("#define " + s) - 1;
50+
const size_t end = find_end(frag_source, "#define END_" + s);
51+
const size_t end_of_line = frag_source.find('\n', end);
52+
if (start == std::string::npos || end == std::string::npos) {
53+
std::cerr << "Formatting error for tag " << s;
54+
continue;
55+
}
56+
frag_source.erase(start, end_of_line - start);
57+
}
58+
}
59+
60+
inline void CompilerShader::find_injection_point() {
61+
injection_point = find_end(frag_source, "#define INJECTION_POINT HERE", 0);
62+
if (injection_point == std::string::npos) {
63+
std::cerr << "Injection point not found in shader";
64+
}
65+
}
66+
67+
inline void CompilerShader::inject(const std::string& expression) {
68+
size_t line_end = frag_source.find('\n', injection_point);
69+
frag_source.insert(line_end + 1, "\n" + expression + "\n");
70+
}

‎src/compiler/compiler_shader.h‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
#include <shader/shader.h>
3+
#include <string>
4+
#include <fstream>
5+
#include <sstream>
6+
#include <iostream>
7+
#include <array>
8+
9+
10+
class CompilerShader {
11+
public:
12+
Shader shader;
13+
CompilerShader(const std::string& vertex_path, const std::string& frag_path);
14+
void compile(const std::string glsl_expression);
15+
16+
static size_t find_end(const std::string& source, const std::string& substr, const size_t start = 0);
17+
18+
private:
19+
20+
21+
void clean_frag_source();
22+
void find_injection_point();
23+
void inject(const std::string& expression);
24+
std::string frag_source;
25+
std::string vert_source;
26+
size_t injection_point;
27+
};

‎src/shaders/plotter.frag‎

Lines changed: 67 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,48 @@
11
#version 330
22

3+
// Other useful constants
4+
5+
6+
#define CONSTANT_DEFINITIONS HERE
7+
8+
const float PI = 3.141591f;
9+
const float TWO_PI_OVER_3 = 2*PI*0.66666f;
10+
const float TWO_OVER_PI = 2.0f / PI;
11+
12+
#define END_CONSTANT_DEFINITIONS HERE
13+
14+
in vec2 pos;
15+
out vec4 FragColor;
16+
17+
18+
19+
uniform vec2 u_resolution;
20+
uniform float u_range;
21+
uniform vec2 shift;
22+
uniform float time;
23+
24+
#define FUNCTION_DEFINITIONS HERE
25+
26+
vec2 cadd(vec2 a, vec2 b){
27+
return a + b;
28+
}
29+
vec2 csub(vec2 a, vec2 b){
30+
return a - b;
31+
}
32+
33+
vec2 cmult(vec2 a, vec2 b){
34+
return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
35+
}
36+
37+
vec2 cdiv(vec2 a, vec2 b){
38+
return cmult(a,vec2(b.x,-b.y))/(b.x*b.x+b.y*b.y);
39+
}
40+
41+
42+
#define END_FUNCTION_DEFINITIONS HERE
43+
344

45+
#define INTERPRETER_SPECIFIC_FUNCTIONS HERE
446

547

648
#define START_WRITING_HERE HERE
@@ -25,80 +67,60 @@
2567
#endif
2668

2769
#ifndef SHADER_END
28-
#define SHADER_END 0u
70+
#define SHADER_END 1u
2971
#endif
3072

3173
#ifndef SHADER_CONSTANT
32-
#define SHADER_CONSTANT 0u
74+
#define SHADER_CONSTANT 2u
3375
#endif
3476

3577
#ifndef SHADER_VARIABLEX
36-
#define SHADER_VARIABLEX 0u
78+
#define SHADER_VARIABLEX 3u
3779
#endif
3880

3981
#ifndef SHADER_VARIABLEY
40-
#define SHADER_VARIABLEY 0u
82+
#define SHADER_VARIABLEY 4u
4183
#endif
4284

4385
#ifndef SHADER_VARIABLEZ
44-
#define SHADER_VARIABLEZ 0u
86+
#define SHADER_VARIABLEZ 5u
4587
#endif
4688

4789
#ifndef SHADER_VARIABLET
48-
#define SHADER_VARIABLET 0u
90+
#define SHADER_VARIABLET 6u
4991
#endif
5092

5193
#ifndef SHADER_VALUE_BOUNDARY
52-
#define SHADER_VALUE_BOUNDARY 0u
94+
#define SHADER_VALUE_BOUNDARY 7u
5395
#endif
5496

5597
#ifndef SHADER_ADD
56-
#define SHADER_ADD 0u
98+
#define SHADER_ADD 8u
5799
#endif
58100

59101
#ifndef SHADER_SUB
60-
#define SHADER_SUB 0u
102+
#define SHADER_SUB 9u
61103
#endif
62104

63105
#ifndef SHADER_MULT
64-
#define SHADER_MULT 0u
106+
#define SHADER_MULT 10u
65107
#endif
66108

67109
#ifndef SHADER_DIV
68-
#define SHADER_DIV 0u
110+
#define SHADER_DIV 11u
69111
#endif
70112

71113
#ifndef SHADER_BINARY_BOUNDARY
72-
#define SHADER_BINARY_BOUNDARY 0u
114+
#define SHADER_BINARY_BOUNDARY 12u
73115
#endif
74116

75117
#ifndef SHADER_NEG
76-
#define SHADER_NEG 0u
118+
#define SHADER_NEG 13u
77119
#endif
78120

79121

80-
81-
in vec2 pos;
82-
out vec4 FragColor;
83-
84122
const uint STACK_SIZE = 1024;
85123

86-
87-
88-
// Other useful constants
89-
const float PI = 3.141591f;
90-
const float TWO_PI_OVER_3 = 2*PI*0.66666f;
91-
const float TWO_OVER_PI = 2.0f / PI;
92-
93-
94-
95-
uniform samplerBuffer constant_stack;
96-
uniform usamplerBuffer operator_stack;
97-
uniform vec2 u_resolution;
98-
uniform float u_range;
99-
uniform vec2 shift;
100-
uniform float time;
101-
102124
vec2 evaluate_constant_operator(in uint operator, in samplerBuffer constants, inout int constant_index, in vec2 pos){
103125
switch(operator){
104126
case CONSTANT:
@@ -136,13 +158,9 @@ vec2 evaluate_unary_operator(in uint operator, inout vec2 stack[16], inout int s
136158
return param;
137159
}
138160

139-
vec2 mult(vec2 a, vec2 b){
140-
return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
141-
}
161+
uniform samplerBuffer constant_stack;
162+
uniform usamplerBuffer operator_stack;
142163

143-
vec2 div(vec2 a, vec2 b){
144-
return mult(a,vec2(b.x,-b.y))/(b.x*b.x+b.y*b.y);
145-
}
146164

147165
vec2 evaluate_binary_operator(in uint operator, inout vec2 stack[16], inout int stack_index){
148166
vec2 params[2] = pop_two(stack,stack_index);
@@ -154,9 +172,9 @@ vec2 evaluate_binary_operator(in uint operator, inout vec2 stack[16], inout int
154172
case SUB:
155173
return a - b;
156174
case MULT:
157-
return mult(a,b);
175+
return cmult(a,b);
158176
case DIV:
159-
return div(a,b);
177+
return cdiv(a,b);
160178
}
161179
return vec2(0.0f);
162180
}
@@ -192,6 +210,8 @@ vec2 run_stack(in usamplerBuffer operators, in samplerBuffer constants, in vec2
192210
return (stack_index > 0)? stack[stack_index-1] : vec2(0.0f);
193211
}
194212

213+
#define END_INTERPRETER_SPECIFIC_FUNCTIONS HERE
214+
195215
vec3 hsl2rgb(vec3 hsl) {
196216
vec3 rgb = clamp(abs(mod(hsl.x * 6.0 + vec3(0.0, 4.0, 2.0), 6.0) - 3.0) - 1.0, 0.0, 1.0);
197217
return hsl.z + hsl.y * (rgb - 0.5) * (1.0 - abs(2.0 * hsl.z - 1.0));
@@ -213,7 +233,13 @@ vec2 convert_coordinates(in vec2 pos, in vec2 resolution, in float range){
213233
void main(){
214234
//vec2 func_value = run_stack(operator_stack,constant_stack,pos);
215235
vec2 z = convert_coordinates(gl_FragCoord.xy,u_resolution,u_range) + shift;
236+
237+
#define INTERPRETER_ASSIGNEMENT HERE
216238
vec2 func_value = run_stack(operator_stack,constant_stack,z);
239+
#define END_INTERPRETER_ASSIGNEMENT HERE
240+
241+
#define INJECTION_POINT HERE
242+
217243
vec3 hsl = domain_color(func_value);
218244
FragColor = vec4(hsl2rgb(hsl),1.0f);
219245
}

0 commit comments

Comments
 (0)