Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cached priority store, optimise shape/sprite rendering #90

Merged
merged 4 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/Shape/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
62 changes: 34 additions & 28 deletions examples/Shape/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<jGL::GL::OpenGLInstance>(res));

jGL::OrthoCam camera(resX, resY, glm::vec2(0.0,0.0));
Expand All @@ -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<jGL::ShapeRenderer> circles = jGLInstance->createShapeRenderer
(
32
);

std::vector<std::shared_ptr<jGL::Shape>> shapes;
std::vector<jGL::Transform> trans;

RNG rng;
uint64_t n = 1000000;
Jerboa-app marked this conversation as resolved.
Show resolved Hide resolved

for (unsigned i = 0; i < 64; i++)
std::shared_ptr<jGL::ShapeRenderer> 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<jGL::Shape>
Expand All @@ -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())
{
Expand All @@ -79,40 +83,40 @@ int main(int argv, char ** argc)

jGLInstance->clear();

for (unsigned i = 0; i <shapes.size(); i++)
{
auto tr = circles->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 <shapes.size(); i++)
Jerboa-app marked this conversation as resolved.
Show resolved Hide resolved
// {
// auto tr = circles->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++)
{
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(
Expand All @@ -138,7 +142,9 @@ int main(int argv, char ** argc)

deltas[frameId] = duration_cast<duration<double>>(tock-tic).count();
frameId = (frameId+1) % 60;

uinfo.colour = false;
uinfo.scale = false;

}

jGLInstance->finish();
Expand Down
40 changes: 15 additions & 25 deletions include/jGL/OpenGL/glShapeRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -28,33 +28,18 @@ namespace jGL::GL
scale = std::vector<float>(sizeHint*scaleDim+padShapes*scaleDim,0.0f);
colours = std::vector<float>(sizeHint*coloursDim+padShapes*coloursDim,0.0f);
initGL();
defaultShader = std::make_shared<glShader>(shapeVertexShader, rectangleFragmentShader);
defaultShader->use();
shader = std::make_shared<glShader>(shapeVertexShader, rectangleFragmentShader);
shader->use();
}

~glShapeRenderer()
{
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> shader, std::multimap<RenderPriority, ShapeId> ids);

/**
* @brief Draw with overriding render priority.
*
* @param ids Render priorities for the Sprites.
*/
void draw(std::multimap<RenderPriority, ShapeId> ids) { draw(defaultShader, ids); }

/**
* @brief A vertex shader for any default shapes.
*
*
*/
static const char * shapeVertexShader;

Expand All @@ -72,15 +57,22 @@ namespace jGL::GL

private:

void draw
(
std::shared_ptr<Shader> shader,
std::vector<std::pair<Info, std::shared_ptr<Shape>>> & 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
};
Expand All @@ -102,8 +94,6 @@ namespace jGL::GL
void initGL();
void freeGL();

std::shared_ptr<Shader> defaultShader;

};
}

Expand Down
11 changes: 7 additions & 4 deletions include/jGL/Vulkan/vkShapeRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ namespace jGL::Vulkan
vkShapeRenderer(size_t sizeHint)
: ShapeRenderer(sizeHint)
{}

void draw(std::shared_ptr<Shader> shader, std::multimap<RenderPriority, ShapeId> ids){TODO("jGL::Vulkan::vkShape::draw");}
void draw(std::multimap<RenderPriority, ShapeId> ids) {TODO("jGL::Vulkan::vkShape::draw");}

protected:
void draw
(
std::shared_ptr<Shader> shader,
std::vector<std::pair<Info, std::shared_ptr<Shape>>> & shapes,
UpdateInfo info = UpdateInfo()
){}
};
}

Expand Down
Loading
Loading