Skip to content

Commit 48d3fed

Browse files
committed
Added shader switching for compilation
1 parent a550426 commit 48d3fed

7 files changed

Lines changed: 83 additions & 40 deletions

File tree

‎src/compiler/compiler_shader.cpp‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <compiler/compiler_shader.h>
22

3-
inline CompilerShader::CompilerShader(const std::string& vertex_path, const std::string& frag_path) {
3+
CompilerShader::CompilerShader(const std::string& vertex_path, const std::string& frag_path) {
44
std::ifstream vfile;
55
std::ifstream ffile;
66
vfile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
@@ -26,12 +26,11 @@ inline CompilerShader::CompilerShader(const std::string& vertex_path, const std:
2626
find_injection_point();
2727
}
2828

29-
inline void CompilerShader::compile(const std::string glsl_expression) {
30-
inject(glsl_expression);
31-
shader.compile(vert_source, frag_source);
29+
void CompilerShader::compile(const std::string glsl_expression) {
30+
shader.compile(vert_source, generate_full_source(glsl_expression));
3231
}
3332

34-
inline size_t CompilerShader::find_end(const std::string& source, const std::string& substr, const size_t start) {
33+
size_t CompilerShader::find_end(const std::string& source, const std::string& substr, const size_t start) {
3534
size_t pos = source.find(substr, start);
3635
if (pos == std::string::npos) {
3736
return std::string::npos;
@@ -42,7 +41,7 @@ inline size_t CompilerShader::find_end(const std::string& source, const std::str
4241
return pos + substr.size();
4342
}
4443

45-
inline void CompilerShader::clean_frag_source() {
44+
void CompilerShader::clean_frag_source() {
4645
constexpr std::array kill_these_guys = { "INTERPRETER_SPECIFIC_FUNCTIONS","INTERPRETER_ASSIGNEMENT" };
4746
size_t prev_end = 0;
4847
for (const std::string& s : kill_these_guys) {
@@ -57,14 +56,17 @@ inline void CompilerShader::clean_frag_source() {
5756
}
5857
}
5958

60-
inline void CompilerShader::find_injection_point() {
59+
void CompilerShader::find_injection_point() {
6160
injection_point = find_end(frag_source, "#define INJECTION_POINT HERE", 0);
6261
if (injection_point == std::string::npos) {
6362
std::cerr << "Injection point not found in shader";
6463
}
6564
}
6665

67-
inline void CompilerShader::inject(const std::string& expression) {
66+
std::string CompilerShader::generate_full_source(const std::string& expression) {
6867
size_t line_end = frag_source.find('\n', injection_point);
69-
frag_source.insert(line_end + 1, "\n" + expression + "\n");
68+
std::string copy = frag_source;
69+
copy.insert(line_end + 1, "\n vec2 func_value = " + expression + ";\n");
70+
std::cout << copy;
71+
return copy;
7072
}

‎src/compiler/compiler_shader.h‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@ class CompilerShader {
1616
static size_t find_end(const std::string& source, const std::string& substr, const size_t start = 0);
1717

1818
private:
19-
20-
2119
void clean_frag_source();
2220
void find_injection_point();
23-
void inject(const std::string& expression);
21+
std::string generate_full_source(const std::string& expression);
2422
std::string frag_source;
2523
std::string vert_source;
2624
size_t injection_point;

‎src/graphics/ui.cpp‎

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using string = std::string;
99
using glm::vec2;
1010

11-
const float DEBOUNCE_DELAY = 0.2f;
11+
const float DEBOUNCE_DELAY = 0.05f;
1212

1313
int input_text_callback(ImGuiInputTextCallbackData* data) {
1414
if (data->EventFlag != ImGuiInputTextFlags_CallbackResize) {
@@ -22,7 +22,7 @@ int input_text_callback(ImGuiInputTextCallbackData* data) {
2222

2323

2424

25-
void render(FunctionState& state, unsigned int& op_tex, unsigned int& const_tex) {
25+
void render(FunctionState& state, unsigned int& op_tex, unsigned int& const_tex, Shader& interpreter_shader) {
2626
try {
2727
vector<TokenOperator> stack = parser::parse(state.expression);
2828
vector<unsigned char> operator_stack;
@@ -38,27 +38,51 @@ void render(FunctionState& state, unsigned int& op_tex, unsigned int& const_tex)
3838
state.error_message = e.what();
3939
state.needs_reparse = false;
4040
}
41+
state.current_shader = &interpreter_shader;
42+
state.is_interpreted = true;
4143
}
4244

43-
void render_and_update(FunctionState& state, unsigned int& op_tex, unsigned int& const_tex){
45+
void compile(FunctionState& state, CompilerShader& compiler_shader) {
46+
try {
47+
vector<TokenOperator> stack = parser::parse(state.expression);
48+
const string& expression = stack_to_glsl_string(stack);
49+
compiler_shader.compile(expression);
50+
}
51+
catch (const std::runtime_error& e) {
52+
state.error_message = e.what();
53+
state.needs_reparse = false;
54+
}
55+
state.current_shader = &compiler_shader.shader;
56+
state.is_interpreted = false;
57+
}
58+
59+
void render_and_update(FunctionState& state, unsigned int& op_tex, unsigned int& const_tex, Shader& interpreter_shader, CompilerShader& compiler_shader){
4460
ImGui::Begin("Function editor");
4561
ImGui::Text("Enter a complex function f(z)=");
4662
ImGui::SetNextItemWidth(-FLT_MIN);
47-
bool typed = ImGui::InputText("##source",
63+
bool pressed_enter = ImGui::InputText("##source",
4864
static_cast<char*>(state.expression.data()),
4965
state.expression.capacity() + 1,
50-
ImGuiInputTextFlags_CallbackResize,
66+
ImGuiInputTextFlags_CallbackResize |
67+
ImGuiInputTextFlags_EnterReturnsTrue,
5168
input_text_callback,
5269
&state.expression);
53-
if (typed) {
70+
bool typed = ImGui::IsItemEdited();
71+
72+
if (typed && !pressed_enter) {
5473
state.last_typing_time = glfwGetTime();
5574
state.needs_reparse = true;
5675
}
5776

5877
const float current_time = glfwGetTime();
5978
const float time_dif = current_time - state.last_typing_time;
60-
if (state.needs_reparse && time_dif < DEBOUNCE_DELAY) {
61-
render(state, op_tex, const_tex);
79+
const bool debounced = time_dif > DEBOUNCE_DELAY;
80+
81+
if (pressed_enter) {
82+
compile(state, compiler_shader);
83+
}
84+
if (state.needs_reparse && debounced && !pressed_enter) {
85+
render(state, op_tex, const_tex, interpreter_shader);
6286
}
6387
if (!state.error_message.empty()) {
6488
ImGui::TextColored(ImVec4(1.0f, 0.4f, 0.4f, 1.0f), "Error: %s", state.error_message.c_str());
@@ -70,6 +94,7 @@ void render_and_update(FunctionState& state, unsigned int& op_tex, unsigned int&
7094
ImGui::Text("Controls:");
7195
ImGui::BulletText("Pan: Left Click + Drag");
7296
ImGui::BulletText("Zoom: Scroll Wheel");
97+
ImGui::BulletText("Enter: Compile (increases performance)");
7398

7499
ImGui::End();
75100
}

‎src/graphics/ui.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
#pragma once
22
#include <glad/glad.h>
3+
#include <shader/shader.h>
4+
#include <compiler/compiler_shader.h>
35
#include <graphics/ui_init.h>
46
#include <string>
57

68
typedef struct FunctionState {
9+
Shader* current_shader = nullptr;
710
std::string expression = "z";
811
std::string error_message = "";
912
float last_typing_time = 0.0f;
1013
bool needs_reparse = true;
14+
bool is_interpreted = true;
1115
} FunctionState;
1216

13-
void render(FunctionState& state, unsigned int& op_tex, unsigned int& const_tex);
17+
void render(FunctionState& state, unsigned int& op_tex, unsigned int& const_tex, Shader& interpreter_shader);
1418

15-
void render_and_update(FunctionState& state, unsigned int& op_tex, unsigned int& const_tex);
19+
void render_and_update(FunctionState& state, unsigned int& op_tex, unsigned int& const_tex, Shader& interpreter_shader, CompilerShader& compiler_shader);

‎src/interactions/interactions.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <interactions/interactions.h>
2+
#include <graphics/ui.h>
23
#include <imgui.h>
34
ViewState* get_state(GLFWwindow* window) {
45
return (ViewState*)glfwGetWindowUserPointer(window);
@@ -61,4 +62,15 @@ void window_size_callback(GLFWwindow* window, const int width, const int height)
6162
state->width = static_cast<float>(width);
6263
state->height = static_cast<float>(height);
6364
glViewport(0, 0, width, height);
65+
}
66+
67+
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
68+
{
69+
if (action != GLFW_PRESS) return;
70+
if (key == GLFW_KEY_ESCAPE) {
71+
glfwSetWindowShouldClose(window, true);
72+
}
73+
if (key == GLFW_KEY_SPACE) {
74+
75+
}
6476
}

‎src/interactions/interactions.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include <glad/glad.h>
23
#include <GLFW/glfw3.h>
34
#include <glm/glm.hpp>
45

‎src/main.cpp‎

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#include <interactions/interactions.h>
1212
#include <stdexcept>
1313
#include <iostream>
14-
#include <types/type_mapper.h>
14+
#include <compiler/compiler_shader.h>
1515

1616
#include <imgui.h>
1717
#include <imgui_impl_glfw.h>
@@ -31,9 +31,9 @@ unsigned int constants_tbo_buffer, constants_tbo_texture;
3131
int main() {
3232
GLFWwindow* window = initalize_window(WIDTH, HEIGHT, "Domain Coloring");
3333
preprocess("shaders/plotter.frag", operators);
34-
const auto& token = get_token_operator(CONSTANT);
35-
const auto& add = get_token_operator(ADD);
34+
3635
Shader shader_program("shaders/plotter.vert", "shaders/plotter.frag");
36+
CompilerShader compiled_shader("shaders/plotter.vert", "shaders/plotter.frag");
3737

3838
unsigned int VAO;
3939
glGenVertexArrays(1, &VAO);
@@ -53,29 +53,30 @@ int main() {
5353
float last_time = 0.0f;
5454
init_imgui(window);
5555
FunctionState function_state;
56-
57-
render(function_state, stack_tbo_texture, constants_tbo_texture);
58-
56+
render(function_state, stack_tbo_texture, constants_tbo_texture,shader_program);
5957
while (!glfwWindowShouldClose(window)) {
6058
glfwPollEvents();
6159
glClear(GL_COLOR_BUFFER_BIT);
6260
init_imgui_loop();
6361

64-
render_and_update(function_state, stack_tbo_texture, constants_tbo_texture);
62+
render_and_update(function_state, stack_tbo_texture, constants_tbo_texture, shader_program, compiled_shader);
6563

66-
shader_program.use();
67-
shader_program.setFloat("time", glfwGetTime());
68-
shader_program.setFloat("u_range", view_state.range);
69-
shader_program.setVec2("shift", view_state.shift);
70-
shader_program.setVec2("u_resolution", glm::vec2(view_state.width,view_state.height));
64+
Shader* current_shader = function_state.current_shader;
65+
current_shader->use();
66+
current_shader->setFloat("time", glfwGetTime());
67+
current_shader->setFloat("u_range", view_state.range);
68+
current_shader->setVec2("shift", view_state.shift);
69+
current_shader->setVec2("u_resolution", glm::vec2(view_state.width,view_state.height));
7170

72-
glActiveTexture(GL_TEXTURE0);
73-
glBindTexture(GL_TEXTURE_BUFFER, stack_tbo_texture);
74-
shader_program.setInt("operator_stack", 0);
71+
if (function_state.is_interpreted) {
72+
glActiveTexture(GL_TEXTURE0);
73+
glBindTexture(GL_TEXTURE_BUFFER, stack_tbo_texture);
74+
shader_program.setInt("operator_stack", 0);
7575

76-
glActiveTexture(GL_TEXTURE1);
77-
glBindTexture(GL_TEXTURE_BUFFER, constants_tbo_texture);
78-
shader_program.setInt("constant_stack", 1);
76+
glActiveTexture(GL_TEXTURE1);
77+
glBindTexture(GL_TEXTURE_BUFFER, constants_tbo_texture);
78+
shader_program.setInt("constant_stack", 1);
79+
}
7980

8081
glDrawArrays(GL_TRIANGLES, 0, 3);
8182

0 commit comments

Comments
 (0)