Skip to content

Commit

Permalink
ugly attempt to refactor rendering backend
Browse files Browse the repository at this point in the history
  • Loading branch information
yekm committed Nov 15, 2023
1 parent 7beaaa8 commit cbb4612
Show file tree
Hide file tree
Showing 16 changed files with 476 additions and 274 deletions.
15 changes: 8 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ add_library(clouds OBJECT
artfactory.cpp
settings.cpp
imgui_elements.cpp
vertexbuffer.cpp
easelplane.cpp
easelvertex.cpp
random.c
cloudlife.cpp
mtron.cpp
Expand Down Expand Up @@ -83,13 +84,13 @@ 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>)
#add_executable(cloudtest test.cpp $<TARGET_OBJECTS:clouds>)
target_link_libraries(cloudlife IMGUI ${GLFW3_LIBRARY} ${OPENGL_LIBRARIES} colormap)
target_link_libraries(cloudtest IMGUI ${GLFW3_LIBRARY} ${OPENGL_LIBRARIES} colormap)
#target_link_libraries(cloudtest IMGUI ${GLFW3_LIBRARY} ${OPENGL_LIBRARIES} colormap)
set_target_properties(cloudlife PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set_target_properties(cloudtest PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
#set_target_properties(cloudtest PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

include(CTest)
#include(CTest)

add_test(NAME CloudTest1 COMMAND cloudtest -f 1024 -i 2 -r 512)
add_test(NAME CloudTest2 COMMAND cloudtest -f 32 -i 8 -r 16)
#add_test(NAME CloudTest1 COMMAND cloudtest -f 1024 -i 2 -r 512)
#add_test(NAME CloudTest2 COMMAND cloudtest -f 32 -i 8 -r 16)
2 changes: 1 addition & 1 deletion acidworm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ void AcidWorm::resize(int _w, int _h) {
//mpp.resize(1024);
_ip.resize(max_x * max_y); ip = _ip.data();
ref.resize(max_y);
fill0(ref);
#warning "fill0(ref);"

for (int n = 0; n < max_y; ++n) {
ref[n] = ip;
Expand Down
83 changes: 9 additions & 74 deletions art.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,96 +8,32 @@ const char* Art::name() {
}

void Art::resized(int _w, int _h) {
if (use_vertex_buffer)
vb = std::make_unique<VertexBuffer>(vertex_buffer_maximum());
else
if (use_pixel_buffer)
pb = std::make_unique<PixelBuffer>();

tex_w = _w, tex_h = _h;
frame_number = 0;
easel->set_window_size(_w, _h);
resize(_w, _h);
}

bool Art::gui() {
if (ImGui::Checkbox("Force use vertex buffer", &use_vertex_buffer)) {
if (use_vertex_buffer)
vb = std::make_unique<VertexBuffer>(vertex_buffer_maximum());
else
vb.reset();
}

if (!use_vertex_buffer)
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);
if (vb) {
if (ScrollableSliderUInt("Max vertex", &vertex_buffer_maximum_k, 1, 1024*32, "%d", 32)) {
vb = std::make_unique<VertexBuffer>(vertex_buffer_maximum());
}
ScrollableSliderUInt("Frame vertex target", &frame_vertex_target_k, 1, 1024, "%d", 8);

}
easel->gui();

bool resize_pbo = render_gui();

ImGui::Text("pixels drawn %d, discarded %d",
pixels_drawn, pixels_discarded);

if (pb)
ImGui::Text("pixel_buffer_size %ld",
pb->buffer.size());
if (vb)
ImGui::Text("total vertecies %d",
vb->total_vertices);
//ImGui::Text("pixels drawn %d, discarded %d",
// pixels_drawn, pixels_discarded);

return resize_pbo;
}

void Art::draw(uint32_t* p) {
bool direct = render(p);
if (vb)
vb->draw();
else
if (pb) {
pb->erase_old(pixel_buffer_maximum);
render_pixel_buffer(p);
} else if (!direct) {
std::copy(m_pixels.begin(), m_pixels.end(), p);
}
}

void Art::drawdot(uint32_t* screen, uint32_t x, uint32_t y, uint32_t c) {
if (x >= tex_w || y >= tex_h) {
++pixels_discarded;
return;
}

if (vb)
vb->adot((float)x/tex_w-.5, (float)y/tex_h-.5);
else
if (pb)
pb->append({x, y, c});
else
really_drawdot(screen, x, y, c);

++pixels_drawn;
easel->render();
}

void Art::clear() {
fill0(m_pixels);
pixels_drawn = 0;
pixels_discarded = 0;
if (vb)
vb->clear();
easel->clear();
}

/*
void Art::render_pixel_buffer(uint32_t* screen) {
std::fill_n(screen, w*h, 0);
#if 1
Expand All @@ -110,12 +46,12 @@ void Art::render_pixel_buffer(uint32_t* screen) {
drawdot(screen, p.x, p.y, p.color);
#endif
}

*/
void Art::default_resize(int _w, int _h) {
w = _w;
h = _h;
//data = (uint8_t *)xrealloc(data, w*h*sizeof(uint32_t));
m_pixels.resize(w*h);
//m_pixels.resize(w*h);
clear();
/* TODO: fill square in drawdot() if pscale > 1
pscale = 1;
Expand All @@ -124,4 +60,3 @@ void Art::default_resize(int _w, int _h) {
*/

}

54 changes: 22 additions & 32 deletions art.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include <algorithm>


#include "pixelbuffer.h"
#include "vertexbuffer.h"
#include "easelplane.h"
#include "easelvertex.h"


template <int N, typename T>
Expand All @@ -27,7 +27,11 @@ fill0(T &container) {
class Art {
public:
Art(std::string _name)
: m_name(_name) {}
: m_name(_name)
{
if (!easel)
easel = std::make_unique<EaselPlane>();
}
const char * name();

/* called when main window is resized and if reinit needed.
Expand All @@ -44,22 +48,18 @@ class Art {
virtual void load(std::string json) {};
virtual std::string save() { return ""; };

void drawdot(uint32_t x, uint32_t y, double o, uint32_t c) {
drawdot(x, y, c | ((unsigned)(0xff*o)<<24));
void drawdot(uint32_t *p, int32_t x, int32_t y, double o, uint32_t c) {
drawdot(x, y, o, c);
}
void drawdot(uint32_t x, uint32_t y, uint32_t c) {
drawdot(data(), x, y, c);
void drawdot(int32_t x, int32_t y, double o, uint32_t c) {
drawdot(x, y, c | ((unsigned)(0xff*o)<<24));
}
void drawdot(uint32_t *screen, uint32_t x, uint32_t y, double o, uint32_t c) {
drawdot(screen, x, y, c | ((unsigned)(0xff*o)<<24));
void drawdot(uint32_t *p, int32_t x, int32_t y, uint32_t c) {
drawdot(x,y,c);
}

/* don't really draws a dot if pixel buffer is used */
void drawdot(uint32_t *screen, uint32_t x, uint32_t y, uint32_t c);

void really_drawdot(uint32_t *screen, uint32_t x, uint32_t y, uint32_t c) {
//printf("%x ", c);
screen[ y*tex_w + x ] = c;
void drawdot(int32_t x, int32_t y, uint32_t c) {
easel->drawdot(x,y,c);
}

/* deprecated? */
Expand All @@ -71,15 +71,6 @@ class Art {
unsigned frame_number = 0, clear_every = 0, max_kframes = 0;
unsigned pixel_buffer_maximum = 1024*10, pixel_buffer_maximum_max = 1024*1024;

unsigned vertex_buffer_maximum_k = 1024;
unsigned frame_vertex_target_k = 16;

unsigned vertex_buffer_maximum() const {
return vertex_buffer_maximum_k * 1024;
}
unsigned frame_vertex_target() const {
return frame_vertex_target_k * 1024;
}


/* clears m_pixels */
Expand All @@ -95,18 +86,17 @@ class Art {

void render_pixel_buffer(uint32_t *screen);

uint32_t *data() { return m_pixels.data(); }
//uint32_t *data() { return m_pixels.data(); }

protected:
std::unique_ptr<PixelBuffer> pb;
std::unique_ptr<VertexBuffer> vb;
bool use_pixel_buffer = false;
bool use_vertex_buffer = true;
unsigned pixels_drawn = 0;
unsigned pixels_discarded = 0;
void default_resize(int _w, int _h);

std::unique_ptr<Easel> easel;
int w, h;
std::vector<uint32_t> m_pixels;
std::string m_name;

EaselPlane* plane() const {
return dynamic_cast<EaselPlane*>(easel.get());
}
};

9 changes: 6 additions & 3 deletions attractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ bool Attractor::render(uint32_t *p)
int x, y;
double oldi, oldj;

for (int i=0; i < frame_vertex_target() && count < vertex_buffer_maximum(); ++i, ++count) {
auto ftarget = easel->frame_vertex_target();
auto vbmax = easel->vertex_buffer_maximum();
for (int i=0; i < ftarget && count < vbmax; ++i, ++count) {
oldj = aj;
oldi = ai + inc;
aj = a - ai;
Expand All @@ -54,7 +56,8 @@ bool Attractor::render(uint32_t *p)
x = w/2 + (int) (ai + aj);
y = h/2 - (int) (ai - aj);

drawdot(x, y, pal.get_color((vertex_buffer_maximum() - count)>>2));
drawdot(x, y, 0);
//drawdot(x, y, pal.get_color((vertex_buffer_maximum() - count)>>2));
//pb->adot((float)x/w-.5, (float)y/h-.5);
}

Expand Down Expand Up @@ -96,7 +99,7 @@ bool Attractor::render_gui ()
void Attractor::resize(int _w, int _h) {
default_resize(_w, _h);

pal.rescale(vertex_buffer_maximum() >> 2);
pal.rescale(easel->vertex_buffer_maximum() >> 2);
count = 0;

//clear();
Expand Down
3 changes: 2 additions & 1 deletion attractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ class Attractor : public Art {
public:
Attractor()
: Art("strange attractors") {
easel = std::make_unique<EaselVertex>();
//use_pixel_buffer = true;
pixel_buffer_maximum = 1024*1024;
//pixel_buffer_maximum = 1024*1024;
}

private:
Expand Down
83 changes: 83 additions & 0 deletions easel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#pragma once
#include <vector>
//#include <deque>
#include <stdint.h>

#include "imgui.h"

#define GL_GLEXT_PROTOTYPES 1
#define GL3_PROTOTYPES 1
#include <GLFW/glfw3.h> // Will drag system OpenGL headers


#if 0
// attempt to make drawdot call not virtual
enum class Etype { PLANE, PIXEL, VERTEX };
template<Etype E = Etype::VERTEX>
class Easel {
public:
};
#endif

class Easel {
public:
Easel() = default;
virtual ~Easel() = default;

// every drawdot call is vertual now :(
virtual void drawdot(int32_t x, int32_t y, uint32_t c) = 0;

virtual void render() = 0;
virtual void clear() {};
virtual void gui() {};
virtual void reset() {};


// TODO: ugly pieces from vertex easel, move to a better place
unsigned frame_vertex_target_k = 128;
unsigned frame_vertex_target() const {
return frame_vertex_target_k * 1024;
}

unsigned vertex_buffer_maximum_k = 1024*16;
unsigned vertex_buffer_maximum() const {
return vertex_buffer_maximum_k * 1024;
}


// TODO: ugly pieces from plane easel, move to a better place
void set_window_size(int _w, int _h) {
ww = _w; wh = _h;
w = _w; h = _h; // TODO: or do set_texture_size() in Art::default_resize?
reset();
}
void set_texture_size(int _w, int _h) {
w = _w; h = _h;
reset();
}
int w, h;
int ww, wh;
};


// deprecated
/*
class EaselPixel : public Easel {
public:
EaselPixel(unsigned max_vertices);
~EaselPixel();
void append(Pixel && p);
virtual void render() override;
virtual void clear() override;
virtual void gui() override;
private:
unsigned pixels_drawn = 0;
unsigned pixels_discarded = 0;
std::deque<Pixel> buffer;
};
*/

Loading

0 comments on commit cbb4612

Please sign in to comment.