Skip to content

Commit

Permalink
easel optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
yekm committed Nov 16, 2023
1 parent a681eac commit 8c54b22
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 27 deletions.
4 changes: 2 additions & 2 deletions art.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ bool Art::gui() {
}

void Art::draw() {
// uint32_t* p = easel->begin();
bool direct = render(0);
easel->begin();
render(0);
easel->render();

++frame_number;
Expand Down
3 changes: 2 additions & 1 deletion easel.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Easel {
// every drawdot call is vertual now :(
virtual void drawdot(int32_t x, int32_t y, uint32_t c) = 0;

virtual void begin() {};
virtual void render() = 0;
virtual void clear() {};
virtual void gui() {};
Expand All @@ -33,7 +34,7 @@ class Easel {
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();
//reset();
}
void set_texture_size(int _w, int _h) {
w = _w; h = _h;
Expand Down
14 changes: 8 additions & 6 deletions easelplane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ EaselPlane::~EaselPlane() {
}

void EaselPlane::reset() {
m_plane.resize(w*h);
//m_plane.resize(w*h);
destroy_pbos();
make_pbos();
}

void EaselPlane::render() {
void EaselPlane::begin() {
int nexti = pbo_index;
pbo_index = pbo_index ? 0 : 1;
glBindTexture(GL_TEXTURE_2D, image_texture);
Expand All @@ -78,11 +78,11 @@ void EaselPlane::render() {
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pboIds[nexti]);
glBufferData(GL_PIXEL_UNPACK_BUFFER, texture_size(),
0, GL_STREAM_DRAW);
uint32_t* ptr = (uint32_t*)glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
assert(ptr);
//art->draw(ptr);
std::copy(m_plane.begin(), m_plane.end(), ptr);
m_plane = (uint32_t*)glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY);
}


void EaselPlane::render() {
glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);

Expand All @@ -105,4 +105,6 @@ void EaselPlane::drawdot(int32_t x, int32_t y, uint32_t c) {
void EaselPlane::gui() {
ImGui::Text("pixels drawn %d, discarded %d",
pixels_drawn, pixels_discarded);
ImGui::Text("texture %d x %d", w, h);
ImGui::Text("window %d x %d", ww, wh);
}
3 changes: 2 additions & 1 deletion easelplane.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class EaselPlane : public Easel {
//void append(Pixel && p);
void drawdot(int32_t x, int32_t y, uint32_t c) override;

virtual void begin() override;
virtual void render() override;
//virtual void clear() override;
virtual void gui() override;
Expand All @@ -34,5 +35,5 @@ class EaselPlane : public Easel {
GLuint image_texture;
GLuint pboIds[2];
int pbo_index = 0;
std::vector<uint32_t> m_plane;
uint32_t* m_plane;
};
29 changes: 12 additions & 17 deletions easelvertex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
#include <iostream>




const char* vertexShaderSource = R"(
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 0) in vec2 aPos;
void main()
{
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);
}
)";

Expand All @@ -28,9 +26,6 @@ const char* fragmentShaderSource = R"(
)";





void EaselVertex::init_shaders() {
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
Expand Down Expand Up @@ -81,10 +76,10 @@ void EaselVertex::create_vertex_buffer() {
glBindBuffer(GL_ARRAY_BUFFER, vbo);


glBufferData(GL_ARRAY_BUFFER, vertex_buffer_maximum()*3*sizeof(float), 0, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, vertex_buffer_maximum()*2*sizeof(float), 0, GL_STATIC_DRAW);
// Specify the vertex attribute pointers
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);
}

void EaselVertex::destroy_vertex_buffer() {
Expand Down Expand Up @@ -117,9 +112,9 @@ void EaselVertex::render() {
if (offset > maxv) {
offset = total_vertices % maxv;
}
unsigned offset_n = offset - m_vertices.size() / 3;
unsigned offset_b = offset_n * 3 * sizeof(float);
//printf("[%d %d]", offset, m_vertices.size()/3);
unsigned offset_n = offset - m_vertices.size() / 2;
unsigned offset_b = offset_n * 2 * sizeof(float);
//printf("[%d %d]", offset, m_vertices.size()/2);
//printf("%.2f_%.2f ", m_vertices.at(0), m_vertices.at(1));
//fflush(stdout);

Expand All @@ -142,17 +137,17 @@ void EaselVertex::gui() {
create_vertex_buffer();
}
ScrollableSliderUInt("Frame vertex target", &frame_vertex_target_k, 1, vertex_buffer_maximum_k, "%d", 8);
ImGui::Text("total vertices %lx", total_vertices);
ImGui::Text("vertices buffer size %d MiB", vertex_buffer_maximum()*2*sizeof(float)/1024/1024);
}

void EaselVertex::dab(float x, float y) {
m_vertices.push_back(x);
m_vertices.push_back(y);
m_vertices.push_back(0);
//m_vertices.push_back(0);
++total_vertices;
// TODO: handle total_vertices overflow
// if (total_vertices > vertex_buffer_maximum()*2)
// total_vertices -= vertex_buffer_maximum();
// TODO: properly handle total_vertices overflow
if (total_vertices > vertex_buffer_maximum()*2)
total_vertices -= vertex_buffer_maximum();
}

/*
Expand Down

0 comments on commit 8c54b22

Please sign in to comment.