Skip to content

Commit 9dd1515

Browse files
committed
Asynchronous image generation
1 parent aec042f commit 9dd1515

4 files changed

Lines changed: 78 additions & 54 deletions

File tree

‎devlog.md‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
1-
Problem!
1+
ARBITRARY PRECISION IS UPON US!
22

3-
Currently, our Ultra High Precision Mode math library is written in a functional manner. This means that all functions are pure, beautiful and return things. This allows us to transform operations like `x * (y + z)` nicely into `hp_mult(x,hp_add(y,z))`
3+
Keeping things short, I tried, and failed, to implement my own GPU, GLSL arbitrary precision math library. Why? Because nobody else has! And for good reason.
4+
- First, I built the math library in a functional way `(add(x,y) -> z)`. This causes the GPU to run out of registers, because returning arrays means allocating temporary memory
5+
- Then, I rewrote the entire library to use 16 global registers, and by mutating a third input `(add(x,y,z) -> void)`. This worked at first, but for minimally nested functions (like `cos(sin(z))`), the GPU tries, and fails, to unroll the loops. So the shader doesn't compile
6+
- To fix that, I once again optimized the library and added flags for the GPU not to unroll. _then_ we run into a problem where the _assembly_ becomes too large. To my knowledge there is no practical solution for this.
47

5-
The problem is that GLSL really doesn't like when we return arrays, and nesting functions _also_ introduce multiple temporary variables, which occupate registers. It would much rather have this expression turned into `number expr; hp_add(y,z,expr); hp_mult(expr,x);`. It _also, also_ dislikes temporary variables being allocated in functions.
8+
At this point, I realized that true arbitrary precision is impossible in the GPU. Thankfully, GLSL translates pretty neatly to C++, so I could just, at build step, transform my complex functions into C++, and do all my arbitrary precision in the CPU!
69

7-
To fix this, we have to write our code like assembly! This means that we'll have to keep a list of global registers that are used whenever needed by a function, and we do composition on these registers!
10+
It is very slow, but it's meant for very high detail images. So, completely fine! Attached, some plots!
811

9-
This means lots of work ahead:
12+
**Commits**
13+
[2895bc0](https://url.jam06452.uk/2cwiyv): UHPM renders, but not properly
14+
[6d39bca](https://url.jam06452.uk/px4ufa): UHPM works, but not for big expressions
15+
[5d96733](https://url.jam06452.uk/154kn6z): High precision zooming
16+
[43e4423](https://url.jam06452.uk/s0xmt3): GPU to CPU transpiling
17+
[f262c96](https://url.jam06452.uk/137or8f): CPU rendering!
1018

11-
1. Change math library to use `inout` rather than `return`
12-
2. Change transpiler to expand nested expressions
13-
3. Change `generate_glsl_string` to expand nested expressions
14-
4. Rewrite transpiler to automatically use registers
19+
**Issues**
20+
[#Issue #40](.): Arbitrary precision in the CPU
21+
[Issue #41](.): GLSL to C++ Transpiler
22+
-[#48](.): Synchronizing GLSL and C++ functions
23+
-[#49](.): Running shaders in the CPU
24+
[Issue #50](.): Reoptimize UHPM
25+
-[#51](.): Rewrite math library to use inout
26+
-[#52](.): Expanding nested expressions in transpiler #52
27+
-[#53](.): Using registers in transpiler #53

‎src/cpu_drawing/cpu_render.h‎

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ std::array<uint8_t, 3> domain_color(const big_vec2& z) {
100100
return std::array<uint8_t, 3>{static_cast<uint8_t>(r * 255.0), static_cast<uint8_t>(g * 255.0), static_cast<uint8_t>(b * 255.0)};
101101
}
102102

103-
void render_band(int start_y, int end_y, int width, int height, unsigned char* pixel_buffer, const CPU_Interpreter& interpreter, const std::vector<TokenOperator>& stack, const ViewState* view_state) {
103+
void render_band(int start_y, int end_y, int width, int height, unsigned char* pixel_buffer, const CPU_Interpreter& interpreter, const std::vector<TokenOperator>& stack, const ViewState* view_state, std::atomic<int>& rows_completed) {
104104
for (int y = start_y; y < end_y; ++y) {
105105
for (int x = 0; x < width; ++x) {
106106

@@ -119,31 +119,41 @@ void render_band(int start_y, int end_y, int width, int height, unsigned char* p
119119
pixel_buffer[index + 2] = color_val[2];
120120
pixel_buffer[index + 3] = 255;
121121
}
122+
rows_completed++;
122123
}
123124
}
124125

125-
void dispatch_render(int width, int height, unsigned char* pixel_buffer, CPU_Interpreter& interpreter, int user_thread_limit, const std::vector<TokenOperator>& stack, const ViewState* view_state) {
126-
int hardware_threads = std::thread::hardware_concurrency();
127-
if (hardware_threads == 0) hardware_threads = 4;
126+
void dispatch_render(int width, int height, unsigned char* pixel_buffer, CPU_Interpreter& interpreter, int user_thread_limit, std::vector<TokenOperator> stack, const ViewState* view_state, std::atomic<bool>& is_rendering, std::atomic<int>& rows_completed) {
127+
if (is_rendering) return;
128128

129-
int num_threads = std::min(hardware_threads, user_thread_limit);
130-
if (num_threads < 1) num_threads = 1;
129+
is_rendering = true;
130+
rows_completed = 0;
131131

132-
std::vector<std::thread> threads;
133-
int rows_per_thread = height / num_threads;
132+
std::thread([=, &interpreter, &is_rendering, &rows_completed]() {
133+
int hardware_threads = std::thread::hardware_concurrency();
134+
if (hardware_threads == 0) hardware_threads = 4;
134135

135-
for (int i = 0; i < num_threads; ++i) {
136-
int start_y = i * rows_per_thread;
137-
int end_y = (i == num_threads - 1) ? height : start_y + rows_per_thread;
138-
threads.emplace_back(
139-
render_band,
140-
start_y, end_y, width, height,
141-
pixel_buffer, std::ref(interpreter), std::ref(stack), view_state
142-
);
143-
}
144-
for (auto& t : threads) {
145-
if (t.joinable()) {
146-
t.join();
136+
int num_threads = std::min(hardware_threads, user_thread_limit);
137+
if (num_threads < 1) num_threads = 1;
138+
139+
std::vector<std::thread> threads;
140+
int rows_per_thread = height / num_threads;
141+
142+
for (int i = 0; i < num_threads; ++i) {
143+
int start_y = i * rows_per_thread;
144+
int end_y = (i == num_threads - 1) ? height : start_y + rows_per_thread;
145+
threads.emplace_back(
146+
render_band,
147+
start_y, end_y, width, height,
148+
pixel_buffer, std::ref(interpreter), std::ref(stack), view_state, std::ref(rows_completed)
149+
);
147150
}
148-
}
151+
152+
for (auto& t : threads) {
153+
if (t.joinable()) t.join();
154+
}
155+
156+
is_rendering = false;
157+
158+
}).detach();
149159
}

‎src/graphics/ui.cpp‎

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -405,26 +405,23 @@ void render_and_update(FunctionState& state, ViewState& view_state, unsigned int
405405
ImGui::EndPopup();
406406
}
407407
if (view_state.is_high_precision) {
408-
ImGui::Begin("High Precision Render", &view_state.is_high_precision, ImGuiWindowFlags_AlwaysAutoResize);
409-
410-
if (view_state.hp_texture != 0) {
411-
ImGui::Text("Resolution: %dx%d", view_state.width, view_state.height);
412-
413-
ImGui::Image(
414-
(void*)(intptr_t)view_state.hp_texture,
415-
ImVec2((float)view_state.hp_height, (float)view_state.hp_width),
416-
ImVec2(0, 0),
417-
ImVec2(1, 1)
418-
);
419-
420-
if (ImGui::Button("Save to PNG")) {
408+
ImGui::Begin("High Precision Render", &view_state.is_high_precision, ImGuiWindowFlags_AlwaysAutoResize);
409+
if (view_state.hp_texture != 0) {
410+
glBindTexture(GL_TEXTURE_2D, view_state.hp_texture);
411+
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, view_state.hp_width, view_state.hp_height, GL_RGBA, GL_UNSIGNED_BYTE, view_state.hp_cpu_buffer.data());
412+
ImGui::Text("Resolution: %dx%d", view_state.width, view_state.height);
413+
ImGui::Image(
414+
(void*)(intptr_t)view_state.hp_texture,
415+
ImVec2((float)view_state.hp_height, (float)view_state.hp_width),
416+
ImVec2(0, 0),
417+
ImVec2(1, 1)
418+
);
419+
if (ImGui::Button("Save to PNG")) {
420+
}
421+
} else {
422+
ImGui::Text("Generating high precision render... please wait.");
421423
}
422-
} else {
423-
ImGui::Text("Generating high precision render... please wait.");
424+
ImGui::End();
424425
}
425-
426-
ImGui::End();
427-
}
428-
429426
ImGui::End();
430427
}

‎src/main.cpp‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct AppContext {
6464
CPU_Interpreter interpreter;
6565
Shader* picker;
6666
Mesh* grid_mesh;
67+
std::vector<TokenOperator> stack;
6768
unsigned int VAO;
6869
unsigned int picker_fbo;
6970
bool pressing_t;
@@ -199,8 +200,8 @@ void main_loop_step(AppContext* ctx) {
199200
init_imgui_loop();
200201

201202
if(ctx->view_state->wants_high_precision){
202-
const int hp_width = 100;//ctx->view_state->width;
203-
const int hp_height = 100;// ctx->view_state->height;
203+
const int hp_width = 700;//ctx->view_state->width;
204+
const int hp_height = 700;// ctx->view_state->height;
204205
ctx->view_state->hp_width = hp_width;
205206
ctx->view_state->hp_height = hp_height;
206207
ctx->view_state->hp_cpu_buffer.resize(hp_width * hp_height * 4);
@@ -212,11 +213,14 @@ void main_loop_step(AppContext* ctx) {
212213
}
213214
int user_thread_limit = 8;
214215

215-
vector<TokenOperator> stack = parser::parse(ctx->function_state->expression);
216+
std::fill(ctx->view_state->hp_cpu_buffer.begin(), ctx->view_state->hp_cpu_buffer.end(), 30);
216217

217-
dispatch_render(hp_width, hp_height,ctx->view_state->hp_cpu_buffer.data(),(ctx->interpreter),user_thread_limit,stack,ctx->view_state);
218+
ctx->stack = parser::parse(ctx->function_state->expression);
219+
220+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, hp_width, hp_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
221+
222+
dispatch_render(hp_width, hp_height,ctx->view_state->hp_cpu_buffer.data(),(ctx->interpreter),user_thread_limit,ctx->stack,ctx->view_state, ctx->view_state->is_rendering_hp, ctx->view_state->hp_rows_completed);
218223
glBindTexture(GL_TEXTURE_2D, ctx->view_state->hp_texture);
219-
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, hp_width, hp_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, ctx->view_state->hp_cpu_buffer.data());
220224

221225
ctx->view_state->wants_high_precision = false;
222226
ctx->view_state->is_high_precision = true;

0 commit comments

Comments
 (0)