From 07d5c3b5a73b27bb159f7fd7d8a6dc1f937eb68c Mon Sep 17 00:00:00 2001 From: Jerboa-app Date: Thu, 29 Aug 2024 07:14:22 +0100 Subject: [PATCH 1/4] Fix size bugs, Adds PriorityStore --- examples/Shape/CMakeLists.txt | 2 + examples/Shape/main.cpp | 62 +++++----- include/jGL/OpenGL/glShapeRenderer.h | 40 +++---- include/jGL/Vulkan/vkShapeRenderer.h | 11 +- include/jGL/priorityStore.h | 170 +++++++++++++++++++++++++++ include/jGL/shapeRenderer.h | 150 ++++++++++++++--------- src/jGL/OpenGL/glShapeRenderer.cpp | 144 +++++++++++++---------- src/jGL/shapeRenderer.cpp | 63 +++++++--- 8 files changed, 453 insertions(+), 189 deletions(-) create mode 100644 include/jGL/priorityStore.h diff --git a/examples/Shape/CMakeLists.txt b/examples/Shape/CMakeLists.txt index ce802b9f..88629d36 100644 --- a/examples/Shape/CMakeLists.txt +++ b/examples/Shape/CMakeLists.txt @@ -16,6 +16,8 @@ endif() include_directories(.) +add_link_options("-Wl,--no-as-needed,-lprofiler,--as-needed") + add_executable(${OUTPUT_NAME} ${SRC}) target_link_libraries(${OUTPUT_NAME} jGL) diff --git a/examples/Shape/main.cpp b/examples/Shape/main.cpp index 5ee67bcc..3d57aeca 100644 --- a/examples/Shape/main.cpp +++ b/examples/Shape/main.cpp @@ -11,14 +11,14 @@ int main(int argv, char ** argc) conf.COCOA_RETINA = true; #endif jGL::DesktopDisplay display(glm::ivec2(resX, resY), "Shape", conf); - display.setFrameLimit(30); + display.setFrameLimit(60); glewInit(); glm::ivec2 res = display.frameBufferSize(); resX = res.x; resY = res.y; - + jGLInstance = std::move(std::make_unique(res)); jGL::OrthoCam camera(resX, resY, glm::vec2(0.0,0.0)); @@ -33,19 +33,22 @@ int main(int argv, char ** argc) jGLInstance->setTextProjection(glm::ortho(0.0,double(resX),0.0,double(resY))); jGLInstance->setMSAA(1); - std::shared_ptr circles = jGLInstance->createShapeRenderer - ( - 32 - ); - std::vector> shapes; std::vector trans; RNG rng; + uint64_t n = 1000000; - for (unsigned i = 0; i < 64; i++) + std::shared_ptr circles = jGLInstance->createShapeRenderer + ( + n + ); + + shapes.reserve(n); + trans.reserve(n); + for (unsigned i = 0; i < n; i++) { - trans.push_back(jGL::Transform(rng.nextFloat(), rng.nextFloat(), 0.0, 0.1f)); + trans.push_back(jGL::Transform(rng.nextFloat(), rng.nextFloat(), 0.0, 0.001f)); shapes.push_back ( std::make_shared @@ -70,6 +73,7 @@ int main(int argv, char ** argc) double delta = 0.0; double dt = 1.0/600.0; + jGL::ShapeRenderer::UpdateInfo uinfo; while (display.isOpen()) { @@ -79,19 +83,19 @@ int main(int argv, char ** argc) jGLInstance->clear(); - for (unsigned i = 0; i getTransform(std::to_string(i)); - trans[i] = jGL::Transform - ( - tr.x+dt*(rng.nextFloat()-0.5), - tr.y+dt*(rng.nextFloat()-0.5), - tr.theta, - tr.scaleX - ); - } + // for (unsigned i = 0; i getTransform(std::to_string(i)); + // trans[i] = jGL::Transform + // ( + // tr.x+dt*(rng.nextFloat()-0.5), + // tr.y+dt*(rng.nextFloat()-0.5), + // tr.theta, + // tr.scaleX + // ); + // } - circles->draw(shader); + circles->draw(shader, uinfo); delta = 0.0; for (int n = 0; n < 60; n++) @@ -99,20 +103,20 @@ int main(int argv, char ** argc) delta += deltas[n]; } delta /= 60.0; - + std::stringstream debugText; double mouseX, mouseY; display.mousePosition(mouseX,mouseY); debugText << "Delta: " << fixedLengthNumber(delta,6) - << " ( FPS: " << fixedLengthNumber(1.0/delta,4) + << " ( FPS: " << fixedLengthNumber(1.0/delta,4) << ")\n" - << "Render draw time: \n" + << "Render draw time: \n" << " " << fixedLengthNumber(rdt, 6) << "\n" - << "Mouse (" << fixedLengthNumber(mouseX,4) - << "," - << fixedLengthNumber(mouseY,4) + << "Mouse (" << fixedLengthNumber(mouseX,4) + << "," + << fixedLengthNumber(mouseY,4) << ")\n"; jGLInstance->text( @@ -138,7 +142,9 @@ int main(int argv, char ** argc) deltas[frameId] = duration_cast>(tock-tic).count(); frameId = (frameId+1) % 60; - + uinfo.colour = false; + uinfo.scale = false; + } jGLInstance->finish(); diff --git a/include/jGL/OpenGL/glShapeRenderer.h b/include/jGL/OpenGL/glShapeRenderer.h index 07b75f07..0c36c67c 100644 --- a/include/jGL/OpenGL/glShapeRenderer.h +++ b/include/jGL/OpenGL/glShapeRenderer.h @@ -9,16 +9,16 @@ namespace jGL::GL { /** * @brief OpenGL implementation of ShapeRenderer. - * + * */ class glShapeRenderer : public ShapeRenderer { public: - + /** * @brief Construct a new glShapeRenderer. - * + * * @param sizeHint hint at the number of shapes. */ glShapeRenderer(size_t sizeHint = 8) @@ -28,8 +28,8 @@ namespace jGL::GL scale = std::vector(sizeHint*scaleDim+padShapes*scaleDim,0.0f); colours = std::vector(sizeHint*coloursDim+padShapes*coloursDim,0.0f); initGL(); - defaultShader = std::make_shared(shapeVertexShader, rectangleFragmentShader); - defaultShader->use(); + shader = std::make_shared(shapeVertexShader, rectangleFragmentShader); + shader->use(); } ~glShapeRenderer() @@ -37,24 +37,9 @@ namespace jGL::GL freeGL(); } - /** - * @brief Draw with overriding render priority and shader. - * - * @param shader An glShader to draw all the Sprites with. - * @param ids Render priorities for the Sprites. - */ - void draw(std::shared_ptr shader, std::multimap ids); - - /** - * @brief Draw with overriding render priority. - * - * @param ids Render priorities for the Sprites. - */ - void draw(std::multimap ids) { draw(defaultShader, ids); } - /** * @brief A vertex shader for any default shapes. - * + * */ static const char * shapeVertexShader; @@ -72,15 +57,22 @@ namespace jGL::GL private: + void draw + ( + std::shared_ptr shader, + std::vector>> & shapes, + UpdateInfo info = UpdateInfo() + ); + GLuint vao, a_position, a_xytheta, a_scale, a_colour; - float quad[6*4] = + float quad[6*4] = { // positions / texture coords 0.5f, 0.5f, 1.0f, 1.0f, // top right 0.5f, -0.5f, 1.0f, 0.0f, // bottom right -0.5f, -0.5f, 0.0f, 0.0f, // bottom left - -0.5f, 0.5f, 0.0f, 1.0f, // top left + -0.5f, 0.5f, 0.0f, 1.0f, // top left -0.5f, -0.5f, 0.0f, 0.0f, // bottom left 0.5f, 0.5f, 1.0f, 1.0f // top right }; @@ -102,8 +94,6 @@ namespace jGL::GL void initGL(); void freeGL(); - std::shared_ptr defaultShader; - }; } diff --git a/include/jGL/Vulkan/vkShapeRenderer.h b/include/jGL/Vulkan/vkShapeRenderer.h index 1c90f2b9..1064a0d2 100644 --- a/include/jGL/Vulkan/vkShapeRenderer.h +++ b/include/jGL/Vulkan/vkShapeRenderer.h @@ -13,10 +13,13 @@ namespace jGL::Vulkan vkShapeRenderer(size_t sizeHint) : ShapeRenderer(sizeHint) {} - - void draw(std::shared_ptr shader, std::multimap ids){TODO("jGL::Vulkan::vkShape::draw");} - void draw(std::multimap ids) {TODO("jGL::Vulkan::vkShape::draw");} - + protected: + void draw + ( + std::shared_ptr shader, + std::vector>> & shapes, + UpdateInfo info = UpdateInfo() + ){} }; } diff --git a/include/jGL/priorityStore.h b/include/jGL/priorityStore.h new file mode 100644 index 00000000..d19931eb --- /dev/null +++ b/include/jGL/priorityStore.h @@ -0,0 +1,170 @@ +#ifndef PRIORITYSTORE_H +#define PRIORITYSTORE_H + +#include +#include +#include +#include +#include +#include + +/** + * @brief User name for an Element. + * @typedef ElementId + * */ +typedef std::string ElementId; + +/** + * @brief A priority for an element. + * @typedef Priority + */ +typedef uint64_t Priority; + +/** + * @brief Store elements in a priority ordering, with identities. + * + * @tparam T an element, wrapped in a std::shared_ptr. + * @remark Elements are stored in a std::vector for effficient access. + * @remark Identities are stored for efficient lookup from string ids. + */ +template +class PriorityStore +{ +public: + + /** + * @brief Combine an id and a priority. + * + */ + struct Info + { + Info(ElementId i, Priority p) + : id(i), priority(p) + {} + + Info() + : id(""), priority(0) + {} + + ElementId id; + Priority priority; + }; + + /** + * @brief Construct a new Priority Store with a reserved size. + * + * @param sizeHint reserve this many elements. + */ + PriorityStore(uint64_t sizeHint = 8) + { + idToElement.reserve(sizeHint); + cache.reserve(sizeHint); + } + + void clear() + { + idToElement.clear(); + cache.clear(); + } + + /** + * @brief Insert an element. + * + * @param s the element. + * @param id its identity. + * @param priority its priority. + * @remark Throws std::runtime_error if the id already exists. + */ + void add(std::shared_ptr s, ElementId id, Priority priority = 0) + { + if (idToElement.find(id) != idToElement.end()) + { + throw std::runtime_error("id: "+id+", already use in PriorityStore"); + } + + idToElement[id] = std::pair(s, priority); + auto pos = std::upper_bound + ( + cache.begin(), + cache.end(), + std::pair(Info(id, priority), nullptr), + [] + ( + std::pair> l, + std::pair> r + ) + { + return l.first.priority < r.first.priority; + } + ); + cache.insert(pos, std::pair(Info(id, priority), s)); + } + + void remove(ElementId id) + { + if (idToElement.find(id) == idToElement.end()) { return; } + + idToElement.erase(id); + + auto pos = std::find_if + ( + cache.begin(), + cache.end(), + [id](std::pair> v) + { + return v.first.id == id; + } + ); + if (pos != cache.end()) + { + cache.erase(pos); + } + } + + void updatePriority(ElementId id, Priority newPriority) + { + if (idToElement.find(id) == idToElement.end()){ return; } + + auto element = idToElement[id]; + remove(id); + add(element.first, id, newPriority); + } + + /** + * @brief Return a vector from overriding priorities. + * + * @param oids overriding priorities. + * @return std::vector>> cached values. + * @remark Overriding is expensive, but useful for drawing partially with + * modest element counts. + */ + std::vector>> vectorise + ( + std::multimap & oids + ) + { + std::vector>> s; + s.reserve(oids.size()); + for (const auto & id : oids) + { + s.push_back(std::pair(Info(id.second, id.first), idToElement[id.second].first)); + } + return s; + } + + std::shared_ptr & operator [](ElementId id) { return idToElement[id].first; } + + typename std::vector>>::const_iterator begin() const { return cache.cbegin(); } + typename std::vector>>::const_iterator end() const { return cache.cend(); } + + uint64_t size() const { return cache.size(); } + +protected: + + // fast lookup of ids + std::unordered_map, Priority>> idToElement; + // contiguous for efficient drawing/iteration + std::vector>> cache; +}; + +#endif /* PRIORITYSTORE_H */ diff --git a/include/jGL/shapeRenderer.h b/include/jGL/shapeRenderer.h index f1b86f51..b1b13fc4 100644 --- a/include/jGL/shapeRenderer.h +++ b/include/jGL/shapeRenderer.h @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -11,12 +12,14 @@ #include #include +#include + namespace jGL { - /** + /** * @brief User name for a Shape. - * @typedef SpriteId + * @typedef SpriteId * */ typedef std::string ShapeId; @@ -24,99 +27,134 @@ namespace jGL * @brief Renders shapes with optional rendering priority. * @remark Currently Circle and Rectangle are defined as shaders. */ - class ShapeRenderer + class ShapeRenderer : public PriorityStore { public: - + + /** + * @brief Control updated data for drawing. + * + */ + struct UpdateInfo + { + UpdateInfo() + : xytheta(true), scale(true), colour(true) + {} + + bool xytheta, scale, colour; + }; + /** * @brief Construct a new ShapeRenderer - * + * * @param sizeHint Hint at the number of shapes */ ShapeRenderer(size_t sizeHint = 8) + : PriorityStore(sizeHint), shader(nullptr) { - shapes.reserve(sizeHint); + // shapes.reserve(sizeHint); + // shapes_vectorised.reserve(sizeHint); } - std::shared_ptr getShape(ShapeId id); + /** + * @brief Construct a new ShapeRenderer + * + * @param shader The default shader to use + * @param sizeHint Hint at the number of shapes + */ + ShapeRenderer(std::shared_ptr shader, size_t sizeHint = 8) + : PriorityStore(sizeHint), shader(shader) + { + // shapes.reserve(sizeHint); + // shapes_vectorised.reserve(sizeHint); + } + + std::shared_ptr getShape(ShapeId id) { return this->operator[](id); } const Transform & getTransform(ShapeId id) { return getShape(id)->transform; } const glm::vec4 & getColour(ShapeId id) { return getShape(id)->colour; } /** * @brief Draw with overriding render priority and shader. - * + * * @param shader A Shader to draw all the Sprites with. * @param ids Render priorities for the Sprites. + * @remark Only ShapeIds appearing in the ids argument will be rendered + * in this draw call. + * @remark Overriding priorities with many shapes performs poorly on cpu. + * Consider ShapeRenderer::updatePriority to cache priority. */ - virtual void draw(std::shared_ptr shader, std::multimap ids) = 0; + virtual void draw + ( + std::shared_ptr shader, + std::multimap & ids, + UpdateInfo info = UpdateInfo() + ) + { + std::vector>> shapes = vectorise(ids); + draw(shader, shapes, info); + } /** * @brief Draw with overriding render priority. - * + * * @param ids Render priorities for the Sprites. + * @remark Only ShapeIds appearing in the ids argument will be rendered + * in this draw call. + * @remark Overriding priorities with many shapes performs poorly on cpu. + * Consider ShapeRenderer::updatePriority to cache priorities. */ - virtual void draw(std::multimap ids) = 0; - + virtual void draw + ( + std::multimap & ids, + UpdateInfo info = UpdateInfo() + ) + { + std::vector>> shapes = vectorise(ids); + draw(shader, shapes, info); + } + /** - * @brief Draw with overriding shader. - * - * @param shader An Shader to draw all the Sprites with. + * @brief Draw with overriding shader and cached priorities. + * + * @param shader A Shader to draw all the Sprites with. */ - virtual void draw(std::shared_ptr shader) { draw(shader, ids); } + virtual void draw + ( + std::shared_ptr shader, + UpdateInfo info = UpdateInfo() + ) + { + draw(shader, cache, info); + } /** - * @brief Draw with default shader and priority. - * + * @brief Draw with default shader and cached priorities. + * */ - virtual void draw() { draw(ids); } - - virtual void add(std::shared_ptr s, ShapeId id, RenderPriority priority = 0); - - virtual void remove(ShapeId id) + virtual void draw + ( + UpdateInfo info = UpdateInfo() + ) { - if (shapes.find(id) != shapes.end()) - { - shapes.erase(id); - } - - for (auto & e : ids) - { - if (e.second == id) - { - ids.erase(e.first); - break; - } - } + draw(shader, cache, info); } - virtual void clear() { ids.clear(); shapes.clear(); } - - bool hasId(const ShapeId id) const { return shapes.find(id) != shapes.end(); } + bool hasId(const ShapeId id) const { return idToElement.find(id) != idToElement.end(); } virtual void setProjection(glm::mat4 p) {projection = p;} - virtual void updatePriority(ShapeId id, RenderPriority newPriority) - { - if (shapes.find(id) == shapes.end()){ return; } - - for (auto & e : ids) - { - if (e.second == id) - { - ids.erase(e.first); - ids.insert(std::pair(newPriority, e.second)); - break; - } - } - } - protected: - std::unordered_map> shapes; + virtual void draw + ( + std::shared_ptr shader, + std::vector>> & shapes, + UpdateInfo info = UpdateInfo() + ) = 0; - std::multimap ids; + std::shared_ptr shader; glm::mat4 projection = glm::mat4(0.0f); diff --git a/src/jGL/OpenGL/glShapeRenderer.cpp b/src/jGL/OpenGL/glShapeRenderer.cpp index d48fafc9..896a3e7a 100644 --- a/src/jGL/OpenGL/glShapeRenderer.cpp +++ b/src/jGL/OpenGL/glShapeRenderer.cpp @@ -1,9 +1,9 @@ #include -namespace jGL::GL +namespace jGL::GL { - const char * glShapeRenderer::shapeVertexShader = + const char * glShapeRenderer::shapeVertexShader = "#version " GLSL_VERSION "\n" "precision lowp float; precision lowp int;\n" "layout(location=0) in vec4 a_position;\n" @@ -23,60 +23,71 @@ namespace jGL::GL "colour = a_colour;\n" "}"; - const char * glShapeRenderer::rectangleFragmentShader = + const char * glShapeRenderer::rectangleFragmentShader = "#version " GLSL_VERSION "\n" "precision lowp float; precision lowp int;\n" "in vec2 texCoord;\n" "in vec4 colour;\n" "layout(location=0) out vec4 fragment;\n" - "void main(){\n" + "void main(){\n" "fragment = colour;\n" "}"; - const char * glShapeRenderer::ellipseFragmentShader = + const char * glShapeRenderer::ellipseFragmentShader = "#version " GLSL_VERSION "\n" "precision lowp float; precision lowp int;\n" "in vec2 texCoord;\n" "in vec4 colour;\n" "out vec4 fragment;\n" - "void main(void){\n" + "void main(void){\n" "vec2 c = texCoord-vec2(0.5,0.5);\n" "if (dot(c,c) > 0.5*0.5) {discard;}\n" "fragment = colour;\n" "}"; - void glShapeRenderer::draw(std::shared_ptr shader, std::multimap ids) + void glShapeRenderer::draw + ( + std::shared_ptr shader, + std::vector>> & shapes, + UpdateInfo info + ) { - uint32_t n = ids.size(); + uint32_t n = this->size(); - if (xytheta.size() < 4*n) + if (xytheta.size() < xythetaDim*n) { xytheta.resize(xythetaDim*n+padShapes*xythetaDim); - scale.resize(2*n+padShapes*2); + scale.resize(scaleDim*n+padShapes*scaleDim); colours.resize(coloursDim*n+padShapes*coloursDim); freeGL(); initGL(); + info = UpdateInfo(); } uint64_t i = 0; - for (auto & sid : ids) + for (auto & shape : shapes) { - const auto shape = shapes[sid.second]; - const Transform & trans = shape->transform; - const glm::vec4 & col = shape->colour; - - xytheta[i*xythetaDim] = trans.x; - xytheta[i*xythetaDim+1] = trans.y; - xytheta[i*xythetaDim+2] = trans.theta; - - scale[i*scaleDim] = trans.scaleX; - scale[i*scaleDim+1] = trans.scaleY; - - colours[i*coloursDim] = col.r; - colours[i*coloursDim+1] = col.g; - colours[i*coloursDim+2] = col.b; - colours[i*coloursDim+3] = col.a; + if (info.xytheta) + { + xytheta[i*xythetaDim] = shape.second->transform.x; + xytheta[i*xythetaDim+1] = shape.second->transform.y; + xytheta[i*xythetaDim+2] = shape.second->transform.theta; + } + + if (info.scale) + { + scale[i*scaleDim] = shape.second->transform.scaleX; + scale[i*scaleDim+1] = shape.second->transform.scaleY; + } + + if (info.colour) + { + colours[i*coloursDim] = shape.second->colour.r; + colours[i*coloursDim+1] = shape.second->colour.g; + colours[i*coloursDim+2] = shape.second->colour.b; + colours[i*coloursDim+3] = shape.second->colour.a; + } i += 1; } @@ -86,37 +97,46 @@ namespace jGL::GL glBindVertexArray(vao); - glBindBuffer(GL_ARRAY_BUFFER, a_xytheta); - glBufferSubData - ( - GL_ARRAY_BUFFER, - 0, - xythetaDim*n*sizeof(float), - &xytheta[0] - ); - glBindBuffer(GL_ARRAY_BUFFER, 0); - - glBindBuffer(GL_ARRAY_BUFFER, a_scale); - glBufferSubData - ( - GL_ARRAY_BUFFER, - 0, - scaleDim*n*sizeof(float), - &scale[0] - ); - glBindBuffer(GL_ARRAY_BUFFER, 0); - - glBindBuffer(GL_ARRAY_BUFFER, a_colour); - - glBufferSubData - ( - GL_ARRAY_BUFFER, - 0, - coloursDim*n*sizeof(float), - &colours[0] - ); - - glBindBuffer(GL_ARRAY_BUFFER, 0); + if (info.xytheta) + { + glBindBuffer(GL_ARRAY_BUFFER, a_xytheta); + glBufferSubData + ( + GL_ARRAY_BUFFER, + 0, + xythetaDim*n*sizeof(float), + &xytheta[0] + ); + glBindBuffer(GL_ARRAY_BUFFER, 0); + } + + if (info.scale) + { + glBindBuffer(GL_ARRAY_BUFFER, a_scale); + glBufferSubData + ( + GL_ARRAY_BUFFER, + 0, + scaleDim*n*sizeof(float), + &scale[0] + ); + glBindBuffer(GL_ARRAY_BUFFER, 0); + } + + if (info.colour) + { + glBindBuffer(GL_ARRAY_BUFFER, a_colour); + + glBufferSubData + ( + GL_ARRAY_BUFFER, + 0, + coloursDim*n*sizeof(float), + &colours[0] + ); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + } glBindVertexArray(0); @@ -125,7 +145,7 @@ namespace jGL::GL glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glDisable(GL_DEPTH_TEST); - + glDrawArraysInstanced(GL_TRIANGLES, 0, 6, n); glBindVertexArray(0); @@ -170,8 +190,8 @@ namespace jGL::GL 0 ); glVertexAttribDivisor(0,0); - - + + glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, a_xytheta); @@ -195,7 +215,7 @@ namespace jGL::GL 0 ); glVertexAttribDivisor(xythetaAttribtue, 1); - + glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, a_scale); @@ -219,7 +239,7 @@ namespace jGL::GL 0 ); glVertexAttribDivisor(scaleAttribtue, 1); - + glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, a_colour); diff --git a/src/jGL/shapeRenderer.cpp b/src/jGL/shapeRenderer.cpp index b9359e0d..f31b83eb 100644 --- a/src/jGL/shapeRenderer.cpp +++ b/src/jGL/shapeRenderer.cpp @@ -2,20 +2,55 @@ namespace jGL { - std::shared_ptr ShapeRenderer::getShape(ShapeId id) - { - return shapes[id]; - } + // std::shared_ptr ShapeRenderer::getShape(ShapeId id) + // { + // return shapes[id].first; + // } - void ShapeRenderer::add(std::shared_ptr s, ShapeId id, RenderPriority priority) - { - - if (shapes.find(id) != shapes.end()) - { - throw std::runtime_error("id: "+id+", already use in ShapeRenderer"); - } + // void ShapeRenderer::add(std::shared_ptr s, ShapeId id, RenderPriority priority) + // { + // if (shapes.find(id) != shapes.end()) + // { + // throw std::runtime_error("id: "+id+", already use in ShapeRenderer"); + // } - shapes[id] = s; - ids.insert(std::pair(priority, id)); - } + // shapes[id] = std::pair(s, priority); + // auto pos = std::upper_bound + // ( + // shapes_vectorised.begin(), + // shapes_vectorised.end(), + // std::pair(RenderInfo(id, priority), nullptr), + // [] + // ( + // std::pair> l, + // std::pair> r + // ) + // { + // return l.first.priority < r.first.priority; + // } + // ); + // shapes_vectorised.insert(pos, std::pair(RenderInfo(id, priority), s)); + // } + + // void ShapeRenderer::remove(ShapeId id) + // { + // if (shapes.find(id) == shapes.end()) { return; } + + // auto shape = shapes[id]; + // shapes.erase(id); + + // auto pos = std::find_if + // ( + // shapes_vectorised.begin(), + // shapes_vectorised.end(), + // [id](std::pair> v) + // { + // return v.first.id == id; + // } + // ); + // if (pos != shapes_vectorised.end()) + // { + // shapes_vectorised.erase(pos); + // } + // } } \ No newline at end of file From 6cd2535d9b4a8b09fed1c6514fb12def387c3d4c Mon Sep 17 00:00:00 2001 From: Jerboa-app Date: Sun, 8 Sep 2024 10:31:42 +0100 Subject: [PATCH 2/4] Dynamics, rm profile --- examples/Shape/CMakeLists.txt | 2 -- examples/Shape/main.cpp | 19 ++++++++----------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/examples/Shape/CMakeLists.txt b/examples/Shape/CMakeLists.txt index 88629d36..ce802b9f 100644 --- a/examples/Shape/CMakeLists.txt +++ b/examples/Shape/CMakeLists.txt @@ -16,8 +16,6 @@ endif() include_directories(.) -add_link_options("-Wl,--no-as-needed,-lprofiler,--as-needed") - add_executable(${OUTPUT_NAME} ${SRC}) target_link_libraries(${OUTPUT_NAME} jGL) diff --git a/examples/Shape/main.cpp b/examples/Shape/main.cpp index 3d57aeca..c7b9f910 100644 --- a/examples/Shape/main.cpp +++ b/examples/Shape/main.cpp @@ -83,17 +83,14 @@ int main(int argv, char ** argc) jGLInstance->clear(); - // for (unsigned i = 0; i getTransform(std::to_string(i)); - // trans[i] = jGL::Transform - // ( - // tr.x+dt*(rng.nextFloat()-0.5), - // tr.y+dt*(rng.nextFloat()-0.5), - // tr.theta, - // tr.scaleX - // ); - // } + for (unsigned i = 0; i draw(shader, uinfo); From a368a0eee4dc58ae17f648d7b3bf598e99f72cf3 Mon Sep 17 00:00:00 2001 From: Jerboa-app Date: Sun, 8 Sep 2024 10:35:54 +0100 Subject: [PATCH 3/4] rm --- CMakeLists.txt | 55 ++++++++++++++++++-------------------- src/jGL/shapeRenderer.cpp | 56 --------------------------------------- 2 files changed, 26 insertions(+), 85 deletions(-) delete mode 100644 src/jGL/shapeRenderer.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b16f4a0c..3dde9fc7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,7 +76,6 @@ if (ANDROID) "src/font.cpp" "src/glyph.cpp" "src/shader.cpp" - "src/shapeRenderer.cpp" "src/spriteRenderer.cpp" "src/warning.cpp" "src/jGL/common.cpp" @@ -102,16 +101,16 @@ endif() add_library(jGL STATIC ${jGL_SRC}) -target_include_directories(jGL PUBLIC - include - include/vendored - ${OPENGL_INCLUDE_DIRS} - ${Vulkan_INCLUDE_DIR} +target_include_directories(jGL PUBLIC + include + include/vendored + ${OPENGL_INCLUDE_DIRS} + ${Vulkan_INCLUDE_DIR} ${GLEW_INCLUDE} include/vendored/glew/include/GL - include/vendored/freetype/include + include/vendored/freetype/include include/vendored/VulkanSDK/Include - include/vendored/VulkanSDK/Include/shaderc + include/vendored/VulkanSDK/Include/shaderc include/vendored/VulkanSDK/Include/vulkan include/vendored/glfw/include include/jLog @@ -126,7 +125,7 @@ elseif(OSX) add_compile_definitions(MACOS) target_link_directories(jGL PUBLIC include/vendored/VulkanSDK/MacOS/Lib) elseif(ANDROID) - + else() target_link_directories(jGL PUBLIC include/vendored/VulkanSDK/Linux/Lib) endif() @@ -157,21 +156,20 @@ IF (TEST_SUITE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wa,-mbig-obj") endif() - file(GLOB_RECURSE TEST_SRC + file(GLOB_RECURSE TEST_SRC "tests/glTests/*.cpp" "src/jGL/OpenGL/*.cpp" "src/jGL/shader.cpp" "src/jGL/warning.cpp" "src/jGL/spriteRenderer.cpp" - "src/jGL/shapeRenderer.cpp" - "src/id.cpp" + "src/id.cpp" "src/jGL/Display/*.cpp" "src/jGL/common.cpp" ) include_directories(include "tests/") # GL Enabled - add_executable(glTests + add_executable(glTests ${TEST_SRC} "src/jGL/shader.cpp" "src/id.cpp" @@ -179,13 +177,13 @@ IF (TEST_SUITE) "src/jGL/glyph.cpp" ) - target_include_directories(glTests PUBLIC - include - include/vendored - ${OPENGL_INCLUDE_DIRS} + target_include_directories(glTests PUBLIC + include + include/vendored + ${OPENGL_INCLUDE_DIRS} ${GLEW_INCLUDE} include/vendored/glew/include/GL - include/vendored/freetype/include + include/vendored/freetype/include include/vendored/glfw/include include/vendored/VulkanSDK/Include include/jLog @@ -200,7 +198,7 @@ IF (TEST_SUITE) else() target_link_libraries(glTests stduuid glew freetype glm ${GLEW_LIBRARIES} ${OPENGL_LIBRARIES} glfw) endif() - + include(CTest) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/tests/cmake/) include(Catch) @@ -210,7 +208,7 @@ IF (TEST_SUITE) add_compile_definitions(EXCEPT_ON_VALIDATION) - file(GLOB_RECURSE TEST_SRC + file(GLOB_RECURSE TEST_SRC "tests/vulkanTests/*.cpp" "src/jGL/Vulkan/*.cpp" "src/jGL/Display/*.cpp" @@ -222,18 +220,17 @@ IF (TEST_SUITE) "src/jGL/shader.cpp" "src/jGL/warning.cpp" "src/jGL/spriteRenderer.cpp" - "src/jGL/shapeRenderer.cpp" "src/id.cpp" "src/jGL/font.cpp" "src/jGL/glyph.cpp" ) - target_include_directories(vkTests PUBLIC - include - include/vendored - ${Vulkan_INCLUDE_DIR} + target_include_directories(vkTests PUBLIC + include + include/vendored + ${Vulkan_INCLUDE_DIR} tests - include/vendored/freetype/include + include/vendored/freetype/include include/vendored/VulkanSDK/Include include/jLog include/jThread @@ -247,11 +244,11 @@ IF (TEST_SUITE) add_compile_definitions(MACOS) target_link_directories(vkTests PUBLIC include/vendored/VulkanSDK/MacOS/Lib) elseif(ANDROID) - + else() target_link_directories(vkTests PUBLIC include/vendored/VulkanSDK/Linux/Lib) endif() - + if (WINDOWS) target_link_libraries(vkTests stduuid freetype glm ${Vulkan_LIBRARIES} glfw shaderc_combined "winmm") else() @@ -264,5 +261,5 @@ IF (TEST_SUITE) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/tests/cmake/) include(Catch) catch_discover_tests(vkTests) - + ENDIF(TEST_SUITE) \ No newline at end of file diff --git a/src/jGL/shapeRenderer.cpp b/src/jGL/shapeRenderer.cpp deleted file mode 100644 index f31b83eb..00000000 --- a/src/jGL/shapeRenderer.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include - -namespace jGL -{ - // std::shared_ptr ShapeRenderer::getShape(ShapeId id) - // { - // return shapes[id].first; - // } - - // void ShapeRenderer::add(std::shared_ptr s, ShapeId id, RenderPriority priority) - // { - // if (shapes.find(id) != shapes.end()) - // { - // throw std::runtime_error("id: "+id+", already use in ShapeRenderer"); - // } - - // shapes[id] = std::pair(s, priority); - // auto pos = std::upper_bound - // ( - // shapes_vectorised.begin(), - // shapes_vectorised.end(), - // std::pair(RenderInfo(id, priority), nullptr), - // [] - // ( - // std::pair> l, - // std::pair> r - // ) - // { - // return l.first.priority < r.first.priority; - // } - // ); - // shapes_vectorised.insert(pos, std::pair(RenderInfo(id, priority), s)); - // } - - // void ShapeRenderer::remove(ShapeId id) - // { - // if (shapes.find(id) == shapes.end()) { return; } - - // auto shape = shapes[id]; - // shapes.erase(id); - - // auto pos = std::find_if - // ( - // shapes_vectorised.begin(), - // shapes_vectorised.end(), - // [id](std::pair> v) - // { - // return v.first.id == id; - // } - // ); - // if (pos != shapes_vectorised.end()) - // { - // shapes_vectorised.erase(pos); - // } - // } -} \ No newline at end of file From 9edca6790173ae9828ac8987ff2bfd2ef0c1a164 Mon Sep 17 00:00:00 2001 From: Jerboa-app Date: Mon, 9 Sep 2024 08:37:27 +0100 Subject: [PATCH 4/4] rm comment --- include/jGL/shapeRenderer.h | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/include/jGL/shapeRenderer.h b/include/jGL/shapeRenderer.h index b1b13fc4..7ce7e62f 100644 --- a/include/jGL/shapeRenderer.h +++ b/include/jGL/shapeRenderer.h @@ -52,10 +52,7 @@ namespace jGL */ ShapeRenderer(size_t sizeHint = 8) : PriorityStore(sizeHint), shader(nullptr) - { - // shapes.reserve(sizeHint); - // shapes_vectorised.reserve(sizeHint); - } + {} /** * @brief Construct a new ShapeRenderer @@ -65,10 +62,7 @@ namespace jGL */ ShapeRenderer(std::shared_ptr shader, size_t sizeHint = 8) : PriorityStore(sizeHint), shader(shader) - { - // shapes.reserve(sizeHint); - // shapes_vectorised.reserve(sizeHint); - } + {} std::shared_ptr getShape(ShapeId id) { return this->operator[](id); }