Skip to content

Commit

Permalink
Display memory usage to enable debugging high memory usage from texture
Browse files Browse the repository at this point in the history
  • Loading branch information
Hjaltesorgenfrei committed Mar 4, 2023
1 parent cb53824 commit dd8f977
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 19 deletions.
2 changes: 1 addition & 1 deletion ext/VulkanMemoryAllocator
2 changes: 1 addition & 1 deletion ext/glfw
2 changes: 1 addition & 1 deletion ext/glm
Submodule glm updated 61 files
+1 −1 doc/api/a00242.html
+32 −4 glm/detail/setup.hpp
+2 −2 glm/detail/type_mat2x2.hpp
+2 −2 glm/detail/type_mat2x2.inl
+2 −2 glm/detail/type_mat2x3.hpp
+2 −2 glm/detail/type_mat2x3.inl
+2 −2 glm/detail/type_mat2x4.hpp
+2 −2 glm/detail/type_mat2x4.inl
+2 −2 glm/detail/type_mat3x2.hpp
+2 −2 glm/detail/type_mat3x2.inl
+2 −2 glm/detail/type_mat3x3.hpp
+2 −2 glm/detail/type_mat3x3.inl
+2 −2 glm/detail/type_mat3x4.hpp
+2 −2 glm/detail/type_mat3x4.inl
+2 −2 glm/detail/type_mat4x2.hpp
+2 −2 glm/detail/type_mat4x2.inl
+2 −2 glm/detail/type_mat4x3.hpp
+2 −2 glm/detail/type_mat4x3.inl
+2 −2 glm/detail/type_mat4x4.hpp
+2 −2 glm/detail/type_mat4x4.inl
+13 −0 glm/detail/type_vec4_simd.inl
+1 −1 glm/exponential.hpp
+128 −0 glm/ext/_matrix_vectorize.hpp
+9 −9 glm/ext/matrix_clip_space.hpp
+4 −1 glm/ext/matrix_common.hpp
+18 −0 glm/ext/matrix_common.inl
+1 −1 glm/ext/matrix_float3x2.hpp
+1 −1 glm/ext/matrix_uint2x3.hpp
+1 −1 glm/ext/matrix_uint2x4_sized.hpp
+1 −1 glm/ext/matrix_uint3x2.hpp
+1 −1 glm/ext/quaternion_geometric.hpp
+3 −0 glm/ext/scalar_relational.hpp
+3 −0 glm/ext/scalar_ulp.hpp
+1 −1 glm/ext/vector_reciprocal.hpp
+3 −0 glm/ext/vector_ulp.hpp
+5 −4 glm/glm.hpp
+1 −1 glm/gtc/integer.hpp
+2 −2 glm/gtc/matrix_inverse.hpp
+4 −4 glm/gtc/random.hpp
+3 −0 glm/gtc/ulp.hpp
+1 −1 glm/gtx/associated_min_max.hpp
+1 −1 glm/gtx/easing.hpp
+4 −4 glm/gtx/extended_min_max.hpp
+1 −1 glm/gtx/handed_coordinate_space.hpp
+15 −15 glm/gtx/hash.hpp
+15 −24 glm/gtx/hash.inl
+1 −1 glm/gtx/io.hpp
+1 −1 glm/gtx/normalize_dot.hpp
+11 −7 glm/gtx/pca.hpp
+10 −10 glm/gtx/pca.inl
+5 −5 glm/gtx/quaternion.hpp
+8 −1 glm/gtx/scalar_multiplication.hpp
+5 −1 glm/gtx/vec_swizzle.hpp
+1 −1 glm/gtx/vector_query.hpp
+1 −0 glm/simd/platform.h
+64 −46 manual.md
+4 −4 readme.md
+2 −2 test/core/core_func_common.cpp
+183 −0 test/ext/ext_matrix_common.cpp
+1 −0 test/gtx/CMakeLists.txt
+55 −0 test/gtx/gtx_hash.cpp
2 changes: 1 addition & 1 deletion ext/imgui
Submodule imgui updated 119 files
2 changes: 1 addition & 1 deletion ext/stb
28 changes: 21 additions & 7 deletions src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ void App::keyCallback(GLFWwindow* window, int key, int scancode, int action, int
}
}

int App::drawFrame(double delta) {
float bytesToMegaBytes(uint64_t bytes) {
return bytes / 1024.0f / 1024.0f;
}

int App::drawFrame(float delta) {
// std::lock_guard<std::mutex> lockGuard(rendererMutex);
ImGui_ImplVulkan_NewFrame();
ImGui_ImplGlfw_NewFrame();
Expand All @@ -109,6 +113,16 @@ int App::drawFrame(double delta) {
drawImGuizmo(&lastModel->transformMatrix.model);
}


auto memoryUsage = renderer->getMemoryUsage();
// Make a imgui window to show the frame time
ImGui::Begin("Debug Info");
ImGui::Text("Frame Time: %f", delta);
ImGui::Text("FPS: %f", 1000.0 / delta);
ImGui::Text("Memory Usage: %.1fmb", bytesToMegaBytes(memoryUsage));
ImGui::End();


//ImGui::ShowDemoWindow();
FrameInfo frameInfo{};
frameInfo.objects = objects;
Expand All @@ -124,9 +138,9 @@ void App::drawLoop() {
auto timeStart = std::chrono::high_resolution_clock::now();
while (!window->windowShouldClose()) {
auto now = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> delta = now - timeStart;
std::chrono::duration<float, std::milli> delta = now - timeStart;
timeStart = now;
window->setTitle(std::to_string(delta.count()).c_str());

if (updateWindowSize) {
renderer->recreateSwapchain();
updateWindowSize = false;
Expand All @@ -140,15 +154,15 @@ void App::drawLoop() {

void App::mainLoop() {
auto timeStart = std::chrono::high_resolution_clock::now();
// objects.push_back(std::make_shared<RenderObject>(Mesh::LoadFromObj("resources/lost_empire.obj"), Material{}));
objects.push_back(std::make_shared<RenderObject>(Mesh::LoadFromObj("resources/lost_empire.obj"), Material{}));
objects.push_back(std::make_shared<RenderObject>(Mesh::LoadFromObj("resources/rat.obj"), Material{}));
renderer->uploadMeshes(objects);

std::thread drawThread([&](){drawLoop();});

while (!window->windowShouldClose()) {
auto now = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> delta = now - timeStart;
std::chrono::duration<float, std::milli> delta = now - timeStart;
timeStart = now;
glfwPollEvents();
processPressedKeys(delta.count());
Expand All @@ -167,9 +181,9 @@ void App::drawImGuizmo(glm::mat4* matrix) {
ImGuizmo::Manipulate(&camera.viewMatrix()[0][0], &proj[0][0], ImGuizmo::TRANSLATE, ImGuizmo::WORLD, &(*matrix)[0][0]);
}

void App::processPressedKeys(double delta) {
void App::processPressedKeys(float delta) {
auto glfw_window = window->getGLFWwindow();
float cameraSpeed = 0.005f * static_cast<float>(delta);
float cameraSpeed = 0.005f * delta;
if (shiftPressed) {
cameraSpeed *= 4;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class App {
bool updateWindowSize = false;

void mainLoop();
void processPressedKeys(double delta);
void processPressedKeys(float delta);
void setupCallBacks();
void drawImGuizmo(glm::mat4* matrix);

Expand All @@ -35,6 +35,6 @@ class App {

void drawLoop();

int drawFrame(double delta);
int drawFrame(float delta);

};
9 changes: 7 additions & 2 deletions src/BehDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include <utility>

auto apiVersion = VK_API_VERSION_1_2;

BehDevice::BehDevice(std::shared_ptr<WindowWrapper> window) : window{std::move(window)} {
createInstance();
createSurface();
Expand Down Expand Up @@ -51,7 +53,7 @@ void BehDevice::createInstance() {
.applicationVersion = VK_MAKE_VERSION(1, 0, 0),
.pEngineName = "No Engine",
.engineVersion = VK_MAKE_VERSION(1, 0, 0),
.apiVersion = VK_API_VERSION_1_2
.apiVersion = apiVersion
};

vk::InstanceCreateInfo createInfo{
Expand Down Expand Up @@ -203,9 +205,11 @@ void BehDevice::createLogicalDevice() {

void BehDevice::createAllocator() {
VmaAllocatorCreateInfo allocatorInfo{
.flags = VmaAllocatorCreateFlagBits::VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT,
.physicalDevice = _physicalDevice,
.device = _device,
.instance = _instance
.instance = _instance,
.vulkanApiVersion = apiVersion
};
vmaCreateAllocator(&allocatorInfo, &_allocator);
mainDeletionQueue.push_function([&]() {
Expand Down Expand Up @@ -255,6 +259,7 @@ std::vector<const char *> BehDevice::getRequiredExtensions() {
extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
}

extensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);

return extensions;
}
Expand Down
7 changes: 5 additions & 2 deletions src/BehDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,13 @@ class BehDevice {
std::shared_ptr<WindowWrapper> window;

const std::vector<const char*> validationLayers = {
"VK_LAYER_KHRONOS_validation"};
"VK_LAYER_KHRONOS_validation"
};

const std::vector<const char*> deviceExtensions = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME};
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
VK_EXT_MEMORY_BUDGET_EXTENSION_NAME
};
};

#endif
9 changes: 9 additions & 0 deletions src/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ void Renderer::recreateSwapchain() {
std::cout << "Recreated swapchain in " << delta.count() << "ms" << std::endl;
}

uint64_t Renderer::getMemoryUsage()
{
VmaTotalStatistics vmaStats;
vmaCalculateStatistics(device->allocator(), &vmaStats);
uint64_t total = 0, used = 0;

return vmaStats.total.statistics.allocationBytes;
}

vk::SurfaceFormatKHR
Renderer::chooseSwapSurfaceFormat(const std::vector<struct vk::SurfaceFormatKHR> &availableFormats) {
for (const auto &availableFormat: availableFormats) {
Expand Down
1 change: 1 addition & 0 deletions src/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Renderer {
void uploadMeshes(const std::vector<std::shared_ptr<RenderObject>>& objects);
Material createMaterial(std::vector<std::string>& texturePaths);
void recreateSwapchain();
uint64_t getMemoryUsage();
RendererMode rendererMode = RendererMode::NORMAL;

private:
Expand Down

0 comments on commit dd8f977

Please sign in to comment.