|
1 |
| -#include "gpu.h" |
2 | 1 | #include <array>
|
3 | 2 | #include <chrono>
|
4 | 3 | #include <cstdio>
|
5 | 4 | #include <future>
|
6 | 5 |
|
| 6 | +#include "gpu.h" |
| 7 | +#include "utils/tui.h" // rasterize |
| 8 | + |
7 | 9 | using namespace gpu; // CreateContext, CreateTensor, CreateKernel,
|
8 | 10 | // CreateShader, DispatchKernel, Wait, ToCPU
|
9 | 11 | // Tensor, TensorList Kernel, Context, Shape, kf32
|
@@ -47,36 +49,6 @@ fn main(@builtin(global_invocation_id) global_id : vec3<u32>) {
|
47 | 49 | }
|
48 | 50 | )";
|
49 | 51 |
|
50 |
| -void rasterize(float *pos, size_t n, float maxX, float maxY, std::string &screen, |
51 |
| - size_t screenWidth, size_t screenHeight) { |
52 |
| - static const char intensity[] = " .`'^-+=*x17X$8#%@"; |
53 |
| - const size_t eps = 1; |
54 |
| - // iterate over screen |
55 |
| - for (size_t i = 0; i < screenHeight; ++i) { |
56 |
| - for (size_t j = 0; j < screenWidth - 2; ++j) { |
57 |
| - int count = 0; |
58 |
| - for (size_t k = 0; k < 2 * n; k += 2) { |
59 |
| - float nx = |
60 |
| - (1.0 + pos[k] / maxX) / 2.0 * static_cast<float>(screenWidth); |
61 |
| - // negate y since it extends from top to bottom |
62 |
| - float ny = (1.0 - (pos[k + 1] / maxY)) / 2.0 * |
63 |
| - static_cast<float>(screenHeight); |
64 |
| - // printf("x: %.2f, y: %.2f\n", nx, ny); |
65 |
| - float length = std::sqrt((nx - j) * (nx - j) + (ny - i) * (ny - i)); |
66 |
| - if (length < eps) { |
67 |
| - count++; |
68 |
| - } |
69 |
| - } |
70 |
| - count = std::min(count / 2, 17); // Need to adjust this for N |
71 |
| - screen[i * screenWidth + j] = intensity[count]; |
72 |
| - } |
73 |
| - screen[i * screenWidth + screenWidth - 1] = '\n'; |
74 |
| - } |
75 |
| - // clear screen |
76 |
| - printf("\033[2J\033[1;1H"); |
77 |
| - printf("# simulations: %d\n%s", n / 2, screen.c_str()); |
78 |
| -} |
79 |
| - |
80 | 52 | int main() {
|
81 | 53 | Context ctx = CreateContext();
|
82 | 54 |
|
@@ -123,6 +95,7 @@ int main() {
|
123 | 95 | std::chrono::duration<double> elapsed = end - start;
|
124 | 96 | // N * 2 because there's two objects per pendulum
|
125 | 97 | rasterize(posArr.data(), N * 2, 2.0, 2.0, screen, 80, 40);
|
| 98 | + printf("\033[2J\033[1;1H""# simulations: %lu\n%s", N, screen.c_str()); |
126 | 99 | std::this_thread::sleep_for(std::chrono::milliseconds(8) - elapsed);
|
127 | 100 | }
|
128 | 101 | }
|
0 commit comments