|
| 1 | +# Advanced |
| 2 | +This is meant for developers that either want to contribute to this project, or just want to understand the underlying structure behind it. |
| 3 | + |
| 4 | +## Build structure and language(s) of choice. |
| 5 | +This is a project written in Microsoft Visual Studio 2026 that uses a CMake build system with ninja (or jinja), using C++20. It purposefully avoids using MSVC-specific features for portability: had this project used C++ Modules, this wouldn't be portable to other systems, namely linux (since GCC and Clang haven't finished implementing them yet). |
| 6 | +Since all this project does is graphics, we delegate most of the math to our GPU. This is done through the OpenGL framework, the shader code being written in GLSL. This particular stack was chosen because C++ is a highly performant language, and there is many online resources and guides for using it along OpenGL. |
| 7 | + |
| 8 | +## Folder structure |
| 9 | +All the source code is stored in two main directories: |
| 10 | +- `src/`, which stores the source code written for, and specific to, this project |
| 11 | +- `dependencies/`, which are all external libraries/reused code from old projects. Namely, the `learnopengl` subdirectory contains custom classes (like `Texture`), that are not directly taken from an outside source, and rather developed by myself. |
| 12 | +The subdirectories of `src` refer to specific modularized features of the plotter, which will be discussed in this document. |
| 13 | + |
| 14 | +## Types |
| 15 | +There are three main types defined for parsing and handling functions and operators (functions are treated as operators internally, so `sin(z)` and `sin z` are the same): `Operator`, `TokenOperator` and `FullOperator`. |
| 16 | +- `Operator` is an enum used to identify operators, it contains no other information |
| 17 | +- `TokenOperator` is a struct used for most parsing purposes. It contains the operators's arity (an `Arity` enum, `NULLARY` for constants), its associativity (an `Associativity` enum), its string representation, whether it's higher order or not, precedence and value, if a constant. It contains all informations needed to parse and constant fold most expressions. |
| 18 | +- `FullOperator` is a struct all the information needed to prepare shaders: a `TokenOperator`, its GLSL`#define` directory name and its GLSL corresponding function or string (`Operator::ADD` corresponds to `cadd(vec2 z)` and `SHADER_ADD`) |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +## Types of rendering |
| 23 | +There are two main ways we render things: through **Interpreted Shaders** and **Compiled Shaders**. |
| 24 | + |
| 25 | +### Interpreted Shaders |
| 26 | +Interpreted shaders have a list of pre-defined opcodes that are source-injected into it at compile-time. Then, a Texture Buffer Object containing the queue of operations to be done (in reverse polish notation) is sent at runtime, and evaluated by the interpreter. |
| 27 | +Two different stacks are sent to interpreted shaders: a `vec2` queue of constant values, and an `unsigned char` queue of opcodes (internally represented as `uint`). Whenever the interpreter comes across the `CONSTANT` opcode, it pops the top of constant queue, and appends it to our evaluation stack. |
| 28 | +For example, assume we want to represent the expression `(z+3)*2`. Or, in RPN, `2 z 3 + *`. This would be sent as following: |
| 29 | +Operator queue: `CONSTANT, VARIABLEZ, CONSTANT, ADD, MULT` |
| 30 | +Constant stack: `vec2(2.0f,0.0f),vec2(3.0f,0.0f)` |
| 31 | +We use vec2s because our constants are complex-valued: they have real and imaginary components. Whenever the interpreter comes across a `CONSTANT` symbol, it grabs the front of the queue, and pops it. |
| 32 | + |
| 33 | +The advntage of interpreted shaders is that there is instant feedback: binding a TBO is a very cheap operator, meaning it can change seamlessly. The problem is that doing stack operations at runtime is expensive, making it slower for large expressions |
| 34 | + |
| 35 | +### Compiled Shaders |
| 36 | +Compiled shaders inline all the logic that interpreted shaders do. If you want to render the expression `e^z * z`, rather than sending a stack to be evaluated at runtime, it will source inject the string `cmult(exp(z),z)` into the function's value and recompile the shader. |
| 37 | +This is far faster than interpreting the function, but recompiling takes time. So, it doesn't have the same "instant" change as the interpreted shaders do. That's why a shader is by default interpreted, and if the user wants more performance, they can compile them. |
| 38 | + |
| 39 | +## The pipeline |
| 40 | +There are 4 main steps that this program works in, with the following sequence: |
| 41 | +``` |
| 42 | +Pre-processing -> Parsing -> Preparing shaders -> Rendering |
| 43 | +``` |
| 44 | + |
| 45 | +### Pre-processing |
| 46 | +This step is used for two main purposes: |
| 47 | +1. Shader opcode synchronization |
| 48 | +2. Source injection |
| 49 | +**Shader opcode synchronization** is simply making sure that the opcodes we send to the interpreted shaders match the ones it internally stores. After all, if we send the expression `CONSTANT CONSTANT ADD` as `2u,2u, 6u`, and, internally, `6u` is the code for `MULT`, we are getting a wrong plot. This is prevented here, as the source code for all interpreter logic are written at runtime. |
| 50 | +**Source injection** is also done at this step: all the base code is written to our 2D interpreted shader (`plotter.frag`), and all the shaders that require functions shared with `plotter.frag` copy these 'blocks' from it. After all, we might have issues where the same function being interpreted and compiled looks different, which is not something we can have. |
| 51 | + |
| 52 | +We tell the preprocessor where these "blocks" are with the following syntax: |
| 53 | +```c++ |
| 54 | +#define BLOCK_NAME HERE |
| 55 | +[block content...] |
| 56 | +#define END_BLOCK_NAME HERE |
| 57 | +``` |
| 58 | +
|
| 59 | +### Parsing |
| 60 | +The name "parsing" might be misleading, because we do far more things in this step than just processing user input. This step involves: |
| 61 | +1. Processing user input |
| 62 | +2. Evaluate higher-order functions |
| 63 | +3. Simplifying the result |
| 64 | +We **process user input** by first breaking the input string into TokenOperators. This is done by verifying whether the current character matches an operator name, or, if not, is a string with a name matching an operator's (this uses `TokenOperator::str_repr`). |
| 65 | +Once we converted the user's input to `TokenOperator`s, we verify if there is implicit multiplication (`2z` is the same as `2*z`) and break apart ambiguous operations (`-z` is negation, not subtraction), and modify accordingly. Then, we run the shunting-yard algorithm in the resulting list of `TokenOperator` to get a reverse polish notation queue. |
| 66 | +
|
| 67 | +Then, we transform our RPN queue into an Abstract Syntax Tree to **evaluate higher-order functions**. In particular, the derivative has hard-coded rules for binary operators (product rule, quotient rule, etc) and uses a lookup table for unary operators and functions, applying the chain rule to it. |
| 68 | +Namely, we use the `build_expression` function to create a subtree through text, with its variables set to `uplaceholder` (so, for instance, if `sin(z)' = cos(z)`, in our lookup table, we'd have `table[Operator::SIN] = build_expression("cos(uplaceholder)")`). Finally, we replace `uplaceholder` with its derivative, recursively applying the chain rule. |
| 69 | +The resulting syntax tree is then converted back into an RPN stack, and sent to the next steps. |
| 70 | +
|
| 71 | +Then, we have to `simplify` our final results. Simplifying does the following: |
| 72 | +1. Transform broken up pairs `a + b*i` into a single complex scalar, `(a,bi)` |
| 73 | +2. Constant folds known expressions `(2 + 3 * 5) z` -> `17z` |
| 74 | +
|
| 75 | +### Preparing shaders |
| 76 | +This is the step in which we pass uniforms to the shader, and all UI logic is handled. Notably, there's two UI states we are keeping track of: |
| 77 | +1. FunctionState |
| 78 | +2. ViewState |
| 79 | +**Function State** stores all relevant information related to the _rendering_ of complex functions: whether it's interpreted or compiled, if it's 3d, if we should reparse it, between others. Essentially, all the UI buttons you see and interact with, alter function state. |
| 80 | +**View State** contains information regarding user _interactions_. Namely, it is what event callbacks see whenever you zoom or pan across the function's plot. |
| 81 | +This is also in which the program decides which shader to render and show to the screen. |
| 82 | +
|
| 83 | +### Rendering |
| 84 | +Here's where all our math logic is ran. We have a block in `plotter.frag` that contains all complex function definitions we need, which is source-injected to any other shaders that might need to run these functions. Currently, the plotter supports every elementary function. |
| 85 | +The grid is drawn either using `z` or `f(z)` as a parameter, which is what the "distort grid" parameter does. |
| 86 | +The domain_coloring function is what maps `f(z)` to a color. These are the parameters used: |
| 87 | +
|
| 88 | + |
| 89 | +
|
| 90 | +Where $|l(z)| = \frac{2}{\pi}\arctan(|z|)$ |
| 91 | +
|
| 92 | +## 3D mode |
| 93 | +3D mode is rendered by making an NxN grid mesh, handled by the `Mesh` struct and `create_grid_mesh` function, and shaping it around the function's "real" shape (picking points in set spaces and placing vertices there). |
| 94 | +Height is directly mapped to |f(z)|, so information is redundant. Movement is done through a custom `Camera` struct. |
0 commit comments