From 202d4c3dc8cfa931c66bb9a1f887825dce54dc20 Mon Sep 17 00:00:00 2001 From: redstrate <54911369+redstrate@users.noreply.github.com> Date: Wed, 13 May 2020 17:19:21 -0400 Subject: [PATCH] Show render in progress --- include/image.h | 7 ++++++- src/main.cpp | 28 +++++++++++++++++++--------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/include/image.h b/include/image.h index 51182fd..09d9c1c 100644 --- a/include/image.h +++ b/include/image.h @@ -1,10 +1,15 @@ #pragma once #include +#include template class Image { public: + void reset() { + array = {}; + } + T& get(const int32_t x, const int32_t y) { return array[y * Width + x]; } @@ -13,5 +18,5 @@ public: return array[y * Width + x]; } - T array[Width * Height] = {}; + std::array array = {}; }; diff --git a/src/main.cpp b/src/main.cpp index 2409440..fdb670e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,6 +42,7 @@ constexpr int32_t num_tiles_y = height / tile_size; // globals Scene scene = {}; Image colors = {}; +bool image_dirty = false; bool calculate_tile(const int32_t from_x, const int32_t to_width, const int32_t from_y, const int32_t to_height) { for (int32_t y = from_y; y < (from_y + to_height); y++) { @@ -64,6 +65,8 @@ bool calculate_tile(const int32_t from_x, const int32_t to_width, const int32_t const glm::vec3 finalColor = model_color * diffuse * (1.0f - shadow); colors.get(x, y) = glm::vec4(finalColor, 1.0f); + + image_dirty = true; } } } @@ -158,20 +161,22 @@ void setup_gfx() { void update_texture() { glBindTexture(GL_TEXTURE_2D, pixels_texture); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, colors.array); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, colors.array.data()); glBindTexture(GL_TEXTURE_2D, 0); } +std::vector> futures; + void render() { - std::vector> futures; - for(int32_t y = 0; y < num_tiles_y; y++) - for(int32_t x = 0; x < num_tiles_x; x++) - futures.push_back(std::async(std::launch::async, calculate_tile, x * tile_size, tile_size, y * tile_size, tile_size)); + futures.clear(); + colors.reset(); - for(auto& future : futures) - future.wait(); - - update_texture(); + for(int32_t y = 0; y < num_tiles_y; y++) { + for(int32_t x = 0; x < num_tiles_x; x++) { + auto f = std::async(std::launch::async, calculate_tile, x * tile_size, tile_size, y * tile_size, tile_size); + futures.push_back(std::move(f)); + } + } } void dump_to_file() { @@ -244,6 +249,11 @@ int main(int, char*[]) { if(ImGui::Button("Dump to file")) dump_to_file(); + if(image_dirty) { + update_texture(); + image_dirty = false; + } + ImGui::Render(); auto& io = ImGui::GetIO();