| 61 | } |
| 62 | |
| 63 | int main() { |
| 64 | |
| 65 | Context ctx = createContext(); |
| 66 | |
| 67 | // static constexpr size_t kRows = 64; |
| 68 | // static constexpr size_t kCols = 96; |
| 69 | static constexpr size_t kRows = 64; |
| 70 | static constexpr size_t kCols = 96; |
| 71 | |
| 72 | kDefLog.level = kError; // suppress screen logging |
| 73 | LOG(kDefLog, kInfo, "Creating screen tensor"); |
| 74 | |
| 75 | std::array<float, kRows * kCols> screenArr; |
| 76 | // std::fill(begin(screenArr), end(screenArr), 0.0); |
| 77 | auto gen = std::mt19937{std::random_device{}()}; |
| 78 | randint(screenArr, gen, 0, 1); |
| 79 | Tensor screen = createTensor(ctx, {kRows, kCols}, kf32, screenArr.data()); |
| 80 | |
| 81 | std::promise<void> promise; |
| 82 | std::future<void> future = promise.get_future(); |
| 83 | |
| 84 | std::string codeString; |
| 85 | struct Params { |
| 86 | float time; |
| 87 | uint32_t screenWidth; |
| 88 | uint32_t screenHeight; |
| 89 | } params = {0.0, kCols, kRows}; |
| 90 | |
| 91 | LOG(kDefLog, kInfo, "Loading shader code from shader.wgsl"); |
| 92 | |
| 93 | loadKernelCode("shader.wgsl", codeString); |
| 94 | KernelCode shader{codeString.c_str(), Shape{16, 16, 1}}; |
| 95 | Kernel renderKernel = |
| 96 | createKernel(ctx, shader, Bindings{screen}, |
| 97 | cdiv({kCols, kRows, 1}, shader.workgroupSize), params); |
| 98 | |
| 99 | LOG(kDefLog, kInfo, "Starting render loop"); |
| 100 | |
| 101 | std::array<char, kRows *(kCols + 1)> raster; |
| 102 | |
| 103 | auto start = std::chrono::high_resolution_clock::now(); |
| 104 | std::chrono::duration<float> elapsed; |
| 105 | size_t ticks = 0; |
| 106 | printf("\033[2J\033[H"); |
| 107 | size_t framesPerLoad = 20; |
| 108 | size_t frame = 0; |
| 109 | while (true) { |
| 110 | if (frame % framesPerLoad == 0) { |
| 111 | loadKernelCode("shader.wgsl", codeString); |
| 112 | if (codeString != shader.data) { |
| 113 | // TODO(avh): Use a better way to avoid write/read race conditions |
| 114 | // and recover from partial write errors |
| 115 | std::this_thread::sleep_for(std::chrono::milliseconds(20)); |
| 116 | loadKernelCode("shader.wgsl", codeString); |
| 117 | shader = {codeString.c_str(), Shape{16, 16, 1}}; |
| 118 | renderKernel = |
| 119 | createKernel(ctx, shader, Bindings{screen}, |
| 120 | cdiv({kCols, kRows, 1}, shader.workgroupSize), params); |
nothing calls this directly
no test coverage detected