Skip to content

Commit 4a0ab12

Browse files
committed
Fragment and vertex shader for 3D
1 parent 9b08468 commit 4a0ab12

8 files changed

Lines changed: 118 additions & 20 deletions

File tree

‎CMakePresets.7z‎

17.2 KB
Binary file not shown.

‎devlog.md‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
We've got got compiled shaders up and running!
1+
We can take derivatives of functions now!
22

3-
All the benefits and reasoning behind doing this were throughouly discussed in [Issue #23](https://url.jam06452.uk/1ff5wk1). What matters is now we have our interpreted shader as a "preview", and whenever we want improved performance, it's just a matter of hitting "enter" and we're done!
3+
For those not familiar with the term, a derivative is a **higher-order function** (meaning it takes a function as a parameter, and outputs another function). This is not a feature natively supported by our shader's language (GLSL), so we have to do all the higher-order logic ourselves in the parser. Which makes sense: if `d(z^2) = 2z`, and the user types `d(z^2)`, we can just send `2z` to the shader.
4+
Our stack is great for evaluating functions numerically, but not optimal for symbolic manipulation. An **Abstract Syntax Tree**, or AST for short, works far better for this, because it simulates the "recursive" nature of expressions. So, we had to convert our RPN queue to an AST, and then back to RPN. Some time put into that!
45

5-
First, we had to convert our stack of opcodes into a GLSL string. After the type overhaul, this was simple enough of a task.
6-
Since I'm dead-set on having a semi-decent developer experience while coding with my fragment shader (`shader/plotter.frag`), I had to do some string manipulation trickery to guarantee that all important function and coloring definitions are shared between shader modes.
7-
This in turn required me to modify our existing `Shader` class dependency to accept strings rather than file paths to compile, and to create a new `CompilerShader` class to handle all the aforementioned string manipulation. There are some optimizations still there to be made to increase compile time (namely ommiting function definitions we're not using), but this is not necessary for the time being, as compile times are low.
8-
Also, since compile times were shown to be tiny, the async proposal related to [Issue #28](https://url.jam06452.uk/cwjcm1) might not be necessary.
9-
This also involved changing our global function state and callback functions to take a reference to different shaders, so we can change them on the fly.
6+
Now, there is a generic way to calculate any derivative, but it would require us to work with infinitesimally small quantities, which just do not exist in computers. So, if we tried to use it, we'd get a rough approximation at best, and a very innacurate result at worst. This, however, can be fixed with a derivative table, falling back to this numeric method whenever needed
107

11-
That's all for this devlog. Attached, a video of me rendering a huge function with the interpreter vs compiler: the performance difference is palpable!
8+
All this higher-order tree manipulation is of intermediate difficult when using raw pointers (`int*`) but quickly becomes _hell_ when using C++'s smart pointers (`std::unique_ptr`). Safe to say, this difficulty is what caused most of these dev hours.
129

13-
**Related commits:**
14-
[Commit 5f38f07](https://url.jam06452.uk/sg59ba): Converting stack into glsl infix string
15-
[Commit 8f40852](https://url.jam06452.uk/g4japn): CompilerShader class done
16-
[Commit 48d3fed](https://url.jam06452.uk/1lh28x0): Added shader switching for compilation
10+
**Related Issues**:
11+
[Issue #8](https://github.com/Sekqies/complex-plotter/issues/8): Computing higher order functions [And it's sub-issues:]
12+
* Issue #9: Stack into syntax tree
13+
* Issue #10: Syntax tree into RPN stack
14+
* Issue #11: Differentiation
15+
* Issue #12: Operator rules
16+
* Issue #13: Numerical differentiation
17+
* Issue #14: Analytic differentiation
1718

18-
**Related issues**
19-
[Issue #28](https://url.jam06452.uk/cwjcm1) and its subissues.
19+
**Related Commits:**
20+
[Commit b56b9fa](https://url.jam06452.uk/fyai8z): ast -> stack, stack -> ast
21+
[Commit f410dba](https://url.jam06452.uk/utr804): Finished implementing derivatives

‎src/higher_order/derivative.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ unique_ptr<AstNode> differentiate(AstNode* node, const Operator var) {
66
m[Operator::SIN] = build_expression("cos(uplaceholder)");
77
m[Operator::COS] = build_expression("-sin(uplaceholder)");
88
m[Operator::TAN] = build_expression("sec(uplaceholder)^2");
9+
m[Operator::LOG] = build_expression("1/uplaceholder");
910
return m;
1011
}();
1112
if (node->op.op == Operator::CONSTANT) {

‎src/preprocessor/preprocessor.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ void preprocess(const string& filename, const vector<TokenOperator>& operators)
1818
const string marker = "START_WRITING_HERE HERE";
1919
size_t start_pos = raw_source.find(marker) + marker.length();
2020
// Let this exception be an exception! While I don't like the exception handling paradigm
21-
//, it is a necessary evil for file handling
21+
//, it is a necessary evil for file handling. [added some days after]: If only I knew...
22+
2223
try {
2324
raw_source = get_source(filename);
2425
}

‎src/shaders/plotter.frag‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ in vec2 pos;
1515
out vec4 FragColor;
1616

1717

18+
#define UNIFORM_DECLARATIONS HERE
1819

1920
uniform vec2 u_resolution;
2021
uniform float u_range;
2122
uniform vec2 shift;
2223
uniform float time;
2324

25+
#define END_UNIFORM_DECLARATIONS HERE
26+
2427
#define FUNCTION_DEFINITIONS HERE
2528

2629
vec2 cadd(vec2 a, vec2 b){
@@ -34,12 +37,12 @@ vec2 cmult(vec2 a, vec2 b){
3437
return vec2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
3538
}
3639

37-
float carg(vec2 z){
38-
return atan(z.y,z.x);
40+
vec2 carg(vec2 z){
41+
return vec2(atan(z.y,z.x),0.0f);
3942
}
4043

41-
float cmag(vec2 z){
42-
return length(z);
44+
vec2 cmag(vec2 z){
45+
return vec2(length(z),0.0f);
4346
}
4447

4548
vec2 cexp(vec2 z){
@@ -147,7 +150,8 @@ vec2 cneg(vec2 a){
147150
#define POW SHADER_POW
148151
#define EXP SHADER_EXP
149152
#define LOG SHADER_LOG
150-
153+
#define ARG SHADER_ARG
154+
#define MAG SHADER_MAG
151155

152156
#ifndef SHADER_NULL_SYMBOL
153157
#define SHADER_NULL_SYMBOL 0u
@@ -212,6 +216,8 @@ vec2 cneg(vec2 a){
212216
#define SHADER_LOG 20u
213217
#define SHADER_EXP 21u
214218
#define SHADER_POW 22u
219+
#define SHADER_ARG 23u
220+
#define SHADER_MAG 24u
215221
#endif
216222

217223

@@ -266,6 +272,10 @@ vec2 evaluate_unary_operator(in uint operator, inout vec2 stack[16], inout int s
266272
return cexp(param);
267273
case LOG:
268274
return clog(param);
275+
case MAG:
276+
return cmag(param);
277+
case ARG:
278+
return carg(param);
269279

270280

271281
}
@@ -328,6 +338,8 @@ vec2 run_stack(in usamplerBuffer operators, in samplerBuffer constants, in vec2
328338

329339
#define END_INTERPRETER_SPECIFIC_FUNCTIONS HERE
330340

341+
#define COLOR_FUNCTIONS HERE
342+
331343
vec3 hsl2rgb(vec3 hsl) {
332344
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);
333345
return hsl.z + hsl.y * (rgb - 0.5) * (1.0 - abs(2.0 * hsl.z - 1.0));
@@ -340,12 +352,15 @@ vec3 domain_color(in vec2 z){
340352
return vec3(hue,1.0f,light);
341353
}
342354

355+
#define END_COLOR_FUNCTIONS HERE
356+
343357
vec2 convert_coordinates(in vec2 pos, in vec2 resolution, in float range){
344358
return range * (pos - 0.5f * resolution)/resolution.y;
345359
}
346360

347361

348362

363+
349364
void main(){
350365
//vec2 func_value = run_stack(operator_stack,constant_stack,pos);
351366
vec2 z = convert_coordinates(gl_FragCoord.xy,u_resolution,u_range) + shift;

‎src/shaders/plotter3d.frag‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#version 330 core
2+
3+
in vec2 f_z;
4+
out vec4 FragColor;
5+
6+
const float PI = 3.14159265359;
7+
const float TWO_OVER_PI = 2.0 / PI;
8+
9+
vec3 hsl2rgb(vec3 hsl) {
10+
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);
11+
return hsl.z + hsl.y * (rgb - 0.5) * (1.0 - abs(2.0 * hsl.z - 1.0));
12+
}
13+
14+
vec3 domain_color(in vec2 z){
15+
float angle = atan(z.y, z.x);
16+
float hue = (angle / (2.0 * PI)) + 0.5;
17+
float light = (TWO_OVER_PI) * atan(length(z));
18+
return vec3(hue, 1.0, light);
19+
}
20+
21+
void main(){
22+
vec3 hsl = domain_color(f_z);
23+
FragColor = vec4(hsl2rgb(hsl),1.0f);
24+
}

‎src/shaders/plotter3d.vert‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#version 330 core
2+
3+
layout (location=0) in vec3 aPos;
4+
5+
out vec2 f_z;
6+
7+
uniform mat4 model;
8+
uniform mat4 view;
9+
uniform mat4 projection;
10+
11+
uniform vec2 u_resolution;
12+
uniform float u_range;
13+
uniform vec2 shift;
14+
uniform float time;
15+
16+
17+
18+
vec2 run_stack(samplerBuffer operator_stack, usamplerBuffer constant_stack, vec2 z);
19+
20+
#define CONSTANT_DEFINITIONS HERE
21+
22+
#define END_CONSTANT_DEFINITIONS HERE
23+
24+
25+
#define FUNCTION_DEFINITIONS HERE
26+
27+
#define END_FUNCTION_DEFINITIONS HERE
28+
29+
30+
#define INTERRPRETER_SPECIFIC_FUNCTIONS HERE
31+
32+
#define END_INTERRPRETER_SPECIFIC_FUNCTIONS HERE
33+
34+
void main(){
35+
vec2 z = (aPos.xy * u_range) + shift;
36+
37+
#define INTERPRETER_ASSIGNEMENT HERE
38+
vec2 func_value = run_stack(operator_stack,constant_stack,z);
39+
#define END_INTERPRETER_ASSIGNEMENT HERE
40+
41+
#define INJECTION_POINT HERE
42+
43+
float height = length(z);
44+
45+
gl_Position = projection * view * model * vec4(aPos.x,height,aPos.z,1.0f);
46+
47+
f_z = func_value;
48+
}
49+
50+

‎src/types/types.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ enum Operator{
4242
COSEC,
4343
SEC,
4444
COT,
45+
ARG,
46+
MAG,
4547

4648
DERIVATIVE
4749
};
@@ -71,6 +73,8 @@ typedef struct AmbiguousOperator {
7173

7274
inline std::vector<FullOperator> full_operators = {
7375
{ {Arity::UNARY, Associativity::RIGHT, Operator::NEG, "~", 4}, 0, "cneg", "NEG"},
76+
{ {Arity::UNARY, Associativity::RIGHT, Operator::MAG, "mag", 4}, 0, "cmag", "MAG"},
77+
{ {Arity::UNARY, Associativity::RIGHT, Operator::ARG, "arg", 4}, 0, "carg", "ARG"},
7478
// TRIGONOMETRIC
7579
{ {Arity::UNARY, Associativity::RIGHT, Operator::SIN, "sin", 1}, 0, "csin", "SIN"},
7680
{ {Arity::UNARY, Associativity::RIGHT, Operator::COS, "cos", 1}, 0, "ccos", "COS"},
@@ -101,6 +105,7 @@ inline std::vector<FullOperator> full_operators = {
101105
{ {Arity::PAREN, Associativity::NONE, Operator::RPAREN, ")", 0}, 0, "" },
102106
{ {Arity::UNARY, Associativity::RIGHT, Operator::DERIVATIVE, "derivative", 0,glm::vec2(0.0f),true}, 0, ""},
103107

108+
104109
};
105110

106111

0 commit comments

Comments
 (0)