From 8c54b22f504f1a0b7a24349d272694a28e768e35 Mon Sep 17 00:00:00 2001 From: Pavel V Date: Thu, 16 Nov 2023 10:27:47 +0300 Subject: [PATCH] easel optimisation --- art.cpp | 4 ++-- easel.h | 3 ++- easelplane.cpp | 14 ++++++++------ easelplane.h | 3 ++- easelvertex.cpp | 29 ++++++++++++----------------- 5 files changed, 26 insertions(+), 27 deletions(-) diff --git a/art.cpp b/art.cpp index 42ef953..1b37773 100644 --- a/art.cpp +++ b/art.cpp @@ -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; diff --git a/easel.h b/easel.h index b9ace4d..4360047 100644 --- a/easel.h +++ b/easel.h @@ -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() {}; @@ -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; diff --git a/easelplane.cpp b/easelplane.cpp index ff0916a..b60beeb 100644 --- a/easelplane.cpp +++ b/easelplane.cpp @@ -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); @@ -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); @@ -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); } diff --git a/easelplane.h b/easelplane.h index 8b3958b..27d227d 100644 --- a/easelplane.h +++ b/easelplane.h @@ -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; @@ -34,5 +35,5 @@ class EaselPlane : public Easel { GLuint image_texture; GLuint pboIds[2]; int pbo_index = 0; - std::vector m_plane; + uint32_t* m_plane; }; diff --git a/easelvertex.cpp b/easelvertex.cpp index 420027d..dc949fc 100644 --- a/easelvertex.cpp +++ b/easelvertex.cpp @@ -6,14 +6,12 @@ #include - - 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); } )"; @@ -28,9 +26,6 @@ const char* fragmentShaderSource = R"( )"; - - - void EaselVertex::init_shaders() { unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); @@ -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() { @@ -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); @@ -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(); } /*