From 921f435aee206bd812f47f3725cf09cf589ef4c4 Mon Sep 17 00:00:00 2001 From: Pavel V Date: Tue, 20 Jun 2023 17:13:19 +0300 Subject: [PATCH] force use pixel buffer --- CMakeLists.txt | 1 + art.hpp | 33 ++++++++++++++++++++++++++------- main.cpp | 7 ++++++- onepixel.h | 5 ++++- thornbird.cpp | 2 +- thornbird.h | 3 ++- 6 files changed, 40 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 595d935..0f0161d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -59,6 +59,7 @@ add_library(clouds OBJECT thornbird.cpp) target_link_libraries(clouds IMGUI glfw GL colormap) +#set_property(TARGET clouds PROPERTY CXX_STANDARD 17) add_executable(cloudlife main.cpp $) add_executable(cloudtest test.cpp $) diff --git a/art.hpp b/art.hpp index d9f1235..d78c6ce 100644 --- a/art.hpp +++ b/art.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "pixelbuffer.h" @@ -23,10 +24,19 @@ class Art { : m_name(_name) {} const char * name() {return m_name.c_str();} void resized(int _w, int _h) { // nb: old buffers -> forbidden to drawdot() + if (use_pixel_buffer && !pb) + pb = std::make_unique(); tex_w = _w, tex_h = _h; resize(_w, _h); } bool gui() { + if (ImGui::Checkbox("Force use pixel buffer", &use_pixel_buffer)) { + if (use_pixel_buffer) + pb = std::make_unique(); + else + pb.reset(); + } + if (pb) ScrollableSliderUInt("Max pixels", &pixel_buffer_maximum, 1, pixel_buffer_maximum_max, "%d", pixel_buffer_maximum/16); bool resize_pbo = render_gui(); @@ -69,10 +79,19 @@ class Art { ++pixels_discarded; return; } - screen[ y*tex_w + x ] = c; + + if (pb) + pb->append({x, y, c}); + else + really_drawdot(screen, x, y, c); + ++pixels_drawn; } + void really_drawdot(uint32_t *screen, uint32_t x, uint32_t y, uint32_t c) { + screen[ y*tex_w + x ] = c; + } + virtual void reinit() { resize(w, h); } virtual ~Art() = default; @@ -95,19 +114,20 @@ class Art { void render_pixel_buffer(uint32_t *screen) { std::fill_n(screen, w*h, 0); #if 1 - auto b = pb->buffer.begin(); - auto pbm = pb->buffer.size() > pixel_buffer_maximum ? pixel_buffer_maximum : pb->buffer.size(); - auto end = pb->buffer.begin() + pbm; - for (; b != end; ++b) - drawdot(screen, b->x, b->y, b->color); + auto pbm = std::min(pb->buffer.size(), pixel_buffer_maximum);// ? pixel_buffer_maximum : pb->buffer.size(); + std::for_each_n(pb->buffer.begin(), pbm, [&](PixelBuffer::Pixel & p) { + really_drawdot(screen, p.x, p.y, p.color); + }); #else for (auto &p : pb->buffer) drawdot(screen, p.x, p.y, p.color); #endif } + std::unique_ptr pb; protected: + bool use_pixel_buffer = false; unsigned pixels_drawn = 0; unsigned pixels_discarded = 0; void default_resize(int _w, int _h) { @@ -128,7 +148,6 @@ class Art { uint32_t *data() { return pixels.data(); } std::vector pixels; std::string m_name; - std::unique_ptr pb; }; diff --git a/main.cpp b/main.cpp index d0ba79c..99c89d3 100644 --- a/main.cpp +++ b/main.cpp @@ -95,12 +95,16 @@ int main(int argc, char *argv[]) int opt; int vsync = 1; + char *artarg = NULL; // TODO: - while ((opt = getopt(argc, argv, "s")) != -1) { + while ((opt = getopt(argc, argv, "sa:")) != -1) { switch (opt) { case 's': vsync = 0; break; + case 'a': + artarg = optarg; + break; default: /* '?' */ fprintf(stderr, "Usage: %s [-s]\n", argv[0]); @@ -159,6 +163,7 @@ int main(int argc, char *argv[]) ImVec4 clear_color = ImVec4(0, 0, 0, 1.00f); ArtFactory af; + if (artarg) {} // TODO: art = af.get_art(); get_window_size(); diff --git a/onepixel.h b/onepixel.h index ee1cb98..1cb1b73 100644 --- a/onepixel.h +++ b/onepixel.h @@ -5,7 +5,10 @@ class OnePixel : public Art { public: OnePixel() - : Art("One pixel benchmark") {} + : Art("One pixel benchmark") { + use_pixel_buffer = true; + pixel_buffer_maximum = 1024*512; + } private: //virtual bool render_gui() override {}; //virtual void resize(int _w, int _h) override; diff --git a/thornbird.cpp b/thornbird.cpp index 953b416..1a0857a 100644 --- a/thornbird.cpp +++ b/thornbird.cpp @@ -102,7 +102,7 @@ void Thornbird::draw_thornbird_1() y = (short) (hp->maxy / 2 * (1 - cost*hp->j + sint*cosp*hp->i - sint*sinp*hp->b)); - pb->append({x, y, pal.get_color(count - k)}); + drawdot(x, y, pal.get_color(count - k)); } hp->inc++; diff --git a/thornbird.h b/thornbird.h index c8a0cbe..61b226a 100644 --- a/thornbird.h +++ b/thornbird.h @@ -34,7 +34,8 @@ class Thornbird : public Art { public: Thornbird() : Art("thornbird --- continuously varying Thornbird set") { - pb = std::make_unique(); + //pb = std::make_unique(); + use_pixel_buffer = true; pixel_buffer_maximum = 1024*512; } private: