Skip to content

Commit

Permalink
Add cached priority store, optimise shape/sprite rendering (#90)
Browse files Browse the repository at this point in the history
* Fix size bugs, Adds PriorityStore
  • Loading branch information
Jerboa-app authored Sep 9, 2024
1 parent 0456384 commit 122773e
Show file tree
Hide file tree
Showing 8 changed files with 418 additions and 224 deletions.
55 changes: 26 additions & 29 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -157,35 +156,34 @@ 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"
"src/jGL/font.cpp"
"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
Expand All @@ -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)
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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)
53 changes: 28 additions & 25 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;

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 @@ -81,38 +85,35 @@ int main(int argv, char ** argc)

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
);
auto & tr = trans[i];
tr.x = tr.x+dt*(rng.nextFloat()-0.5);
tr.y = tr.y+dt*(rng.nextFloat()-0.5);
tr.theta = tr.theta;
tr.scaleX = 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 +139,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

0 comments on commit 122773e

Please sign in to comment.