Skip to content

Commit

Permalink
force use pixel buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
yekm committed Jun 20, 2023
1 parent 1726865 commit 921f435
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<TARGET_OBJECTS:clouds>)
add_executable(cloudtest test.cpp $<TARGET_OBJECTS:clouds>)
Expand Down
33 changes: 26 additions & 7 deletions art.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <string>
#include <vector>
#include <memory>
#include <algorithm>


#include "pixelbuffer.h"
Expand All @@ -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<PixelBuffer>();
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<PixelBuffer>();
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();
Expand Down Expand Up @@ -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;
Expand All @@ -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<uint64_t>(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<PixelBuffer> pb;

protected:
bool use_pixel_buffer = false;
unsigned pixels_drawn = 0;
unsigned pixels_discarded = 0;
void default_resize(int _w, int _h) {
Expand All @@ -128,7 +148,6 @@ class Art {
uint32_t *data() { return pixels.data(); }
std::vector<uint32_t> pixels;
std::string m_name;
std::unique_ptr<PixelBuffer> pb;

};

7 changes: 6 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 4 additions & 1 deletion onepixel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion thornbird.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down
3 changes: 2 additions & 1 deletion thornbird.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class Thornbird : public Art {
public:
Thornbird()
: Art("thornbird --- continuously varying Thornbird set") {
pb = std::make_unique<PixelBuffer>();
//pb = std::make_unique<PixelBuffer>();
use_pixel_buffer = true;
pixel_buffer_maximum = 1024*512;
}
private:
Expand Down

0 comments on commit 921f435

Please sign in to comment.