Skip to content

Commit

Permalink
[Render] Instrumented rendering features for profiling purposes
Browse files Browse the repository at this point in the history
- Moved the geometry pass' execution to a dedicated RenderGraph::executeGeometryPass() function

- Moved the input callbacks processing to a dedicated Window::processInputs() function
  • Loading branch information
Razakhel committed Mar 22, 2024
1 parent 3ffe42b commit 3bc1b22
Show file tree
Hide file tree
Showing 16 changed files with 285 additions and 40 deletions.
5 changes: 4 additions & 1 deletion include/RaZ/Render/RenderGraph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ class RenderGraph : public Graph<RenderPass> {
/// Executes the render graph, executing all passes starting with the geometry's.
/// \param renderSystem Render system executing the render graph.
void execute(RenderSystem& renderSystem);
/// Executes the geometry pass.
/// \param renderSystem Render system executing the render graph.
void executeGeometryPass(RenderSystem& renderSystem) const;
/// Executes a render pass, which in turn recursively executes its parents if they have not already been in the current frame.
/// \param renderPass Render pass to be executed.
void execute(const RenderPass& renderPass);
void executePass(const RenderPass& renderPass);

RenderPass m_geometryPass {};
std::vector<std::unique_ptr<RenderProcess>> m_renderProcesses {};
Expand Down
4 changes: 4 additions & 0 deletions include/RaZ/Render/Window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ class Window {
~Window() { close(); }

private:
/// Processes actions corresponding to keyboard & mouse inputs.
/// \param deltaTime Amount of time elapsed since the last frame.
void processInputs(float deltaTime);

static inline int s_refCounter = 0;

OwnerValue<GLFWwindow*, nullptr> m_windowHandle {};
Expand Down
26 changes: 26 additions & 0 deletions src/RaZ/Render/Camera.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "RaZ/Math/Transform.hpp"
#include "RaZ/Render/Camera.hpp"

#include "tracy/Tracy.hpp"

namespace Raz {

Camera::Camera(unsigned int frameWidth, unsigned int frameHeight,
Expand All @@ -10,11 +12,15 @@ Camera::Camera(unsigned int frameWidth, unsigned int frameHeight,
m_fieldOfView{ fieldOfView },
m_nearPlane{ nearPlane }, m_farPlane{ farPlane },
m_projType{ projType } {
ZoneScopedN("Camera::Camera");

computeProjectionMatrix();
computeInverseProjectionMatrix();
}

void Camera::setFieldOfView(Radiansf fieldOfView) {
ZoneScopedN("Camera::setFieldOfView");

m_fieldOfView = fieldOfView;

if (m_projType == ProjectionType::PERSPECTIVE) {
Expand All @@ -24,6 +30,8 @@ void Camera::setFieldOfView(Radiansf fieldOfView) {
}

void Camera::setOrthographicBound(float bound) {
ZoneScopedN("Camera::setOrthographicBound");

m_orthoBound = bound;

if (m_projType == ProjectionType::ORTHOGRAPHIC) {
Expand All @@ -33,6 +41,8 @@ void Camera::setOrthographicBound(float bound) {
}

void Camera::setProjectionType(ProjectionType projType) {
ZoneScopedN("Camera::setProjectionType");

if (projType == m_projType)
return; // No need to recompute the projection matrix

Expand All @@ -43,12 +53,16 @@ void Camera::setProjectionType(ProjectionType projType) {
}

const Mat4f& Camera::computeViewMatrix(const Transform& cameraTransform) {
ZoneScopedN("Camera::computeViewMatrix");

// TODO: the rotation quaternion being supposedly a unit one, the inversion could be replaced by a conjugation
m_viewMat = cameraTransform.getRotation().inverse().computeMatrix() * cameraTransform.computeTranslationMatrix(true);
return m_viewMat;
}

const Mat4f& Camera::computeLookAt(const Vec3f& position) {
ZoneScopedN("Camera::computeLookAt");

const Vec3f zAxis = (position - m_target).normalize();
const Vec3f xAxis = m_upAxis.cross(zAxis).normalize();
const Vec3f yAxis = zAxis.cross(xAxis);
Expand All @@ -62,11 +76,15 @@ const Mat4f& Camera::computeLookAt(const Vec3f& position) {
}

const Mat4f& Camera::computeInverseViewMatrix() {
ZoneScopedN("Camera::computeInverseViewMatrix");

m_invViewMat = m_viewMat.inverse();
return m_invViewMat;
}

const Mat4f& Camera::computePerspectiveMatrix() {
ZoneScopedN("Camera::computePerspectiveMatrix");

const float halfFovTangent = std::tan(m_fieldOfView.value * 0.5f);
const float fovRatio = m_frameRatio * halfFovTangent;
const float planeMult = m_farPlane * m_nearPlane;
Expand All @@ -81,6 +99,8 @@ const Mat4f& Camera::computePerspectiveMatrix() {
}

const Mat4f& Camera::computeOrthographicMatrix(float minX, float maxX, float minY, float maxY, float minZ, float maxZ) {
ZoneScopedN("Camera::computeOrthographicMatrix");

const float invDistX = 1.f / (maxX - minX);
const float invDistY = 1.f / (maxY - minY);
const float invDistZ = 1.f / (maxZ - minZ);
Expand All @@ -99,18 +119,24 @@ const Mat4f& Camera::computeOrthographicMatrix() {
}

const Mat4f& Camera::computeProjectionMatrix() {
ZoneScopedN("Camera::computeProjectionMatrix");

if (m_projType == ProjectionType::ORTHOGRAPHIC)
return computeOrthographicMatrix();

return computePerspectiveMatrix();
}

const Mat4f& Camera::computeInverseProjectionMatrix() {
ZoneScopedN("Camera::computeInverseProjectionMatrix");

m_invProjMat = m_projMat.inverse();
return m_invProjMat;
}

void Camera::resizeViewport(unsigned int frameWidth, unsigned int frameHeight) {
ZoneScopedN("Camera::resizeViewport");

const float newRatio = static_cast<float>(frameWidth) / static_cast<float>(frameHeight);

if (newRatio == m_frameRatio)
Expand Down
10 changes: 10 additions & 0 deletions src/RaZ/Render/Cubemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "RaZ/Render/Renderer.hpp"
#include "RaZ/Utils/Logger.hpp"

#include "tracy/Tracy.hpp"

namespace Raz {

namespace {
Expand Down Expand Up @@ -145,6 +147,8 @@ TextureInternalFormat recoverInternalFormat(ImageColorspace colorspace, ImageDat
} // namespace

Cubemap::Cubemap() {
ZoneScopedN("Cubemap::Cubemap");

Logger::debug("[Cubemap] Creating...");
Renderer::generateTexture(m_index);
Logger::debug("[Cubemap] Created (ID: " + std::to_string(m_index) + ')');
Expand All @@ -155,6 +159,8 @@ const RenderShaderProgram& Cubemap::getProgram() const {
}

void Cubemap::load(const Image& right, const Image& left, const Image& top, const Image& bottom, const Image& front, const Image& back) const {
ZoneScopedN("Cubemap::load");

bind();

constexpr auto mapImage = [] (const Image& img, TextureType type) {
Expand Down Expand Up @@ -220,6 +226,8 @@ void Cubemap::unbind() const {
}

void Cubemap::draw() const {
ZoneScopedN("Cubemap::draw");

Renderer::setDepthFunction(DepthStencilFunction::LESS_EQUAL);

const MeshRenderer& displayCube = getDisplayCube();
Expand All @@ -235,6 +243,8 @@ void Cubemap::draw() const {
}

Cubemap::~Cubemap() {
ZoneScopedN("Cubemap::~Cubemap");

if (!m_index.isValid())
return;

Expand Down
14 changes: 14 additions & 0 deletions src/RaZ/Render/Framebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
#include "RaZ/Render/Texture.hpp"
#include "RaZ/Utils/Logger.hpp"

#include "tracy/Tracy.hpp"

namespace Raz {

namespace {

inline void drawDisplaySurface() {
ZoneScopedN("[Framebuffer]::drawDisplaySurface");

// Creating a triangle large enough to cover the whole render frame:
//
// 3 | \ 3 | \
Expand Down Expand Up @@ -54,6 +58,8 @@ inline void drawDisplaySurface() {
} // namespace

Framebuffer::Framebuffer() {
ZoneScopedN("Framebuffer::Framebuffer");

Logger::debug("[Framebuffer] Creating...");
Renderer::generateFramebuffer(m_index);
Logger::debug("[Framebuffer] Created (ID: " + std::to_string(m_index) + ')');
Expand Down Expand Up @@ -118,6 +124,8 @@ void Framebuffer::clearTextureBuffers() {
}

void Framebuffer::resizeBuffers(unsigned int width, unsigned int height) {
ZoneScopedN("Framebuffer::resizeBuffers");

if (m_depthBuffer)
m_depthBuffer->resize(width, height);

Expand All @@ -126,6 +134,8 @@ void Framebuffer::resizeBuffers(unsigned int width, unsigned int height) {
}

void Framebuffer::mapBuffers() const {
ZoneScopedN("Framebuffer::mapBuffers");

Logger::debug("[Framebuffer] Mapping buffers (ID: " + std::to_string(m_index) + ")...");

Renderer::bindFramebuffer(m_index);
Expand Down Expand Up @@ -168,11 +178,15 @@ void Framebuffer::unbind() const {
}

void Framebuffer::display() const {
ZoneScopedN("Framebuffer::display");

Renderer::clear(MaskType::COLOR);
drawDisplaySurface();
}

Framebuffer::~Framebuffer() {
ZoneScopedN("Framebuffer::~Framebuffer");

if (!m_index.isValid())
return;

Expand Down
4 changes: 4 additions & 0 deletions src/RaZ/Render/Material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "RaZ/Render/Material.hpp"
#include "RaZ/Render/Renderer.hpp"

#include "tracy/Tracy.hpp"

namespace Raz {

namespace {
Expand Down Expand Up @@ -29,6 +31,8 @@ constexpr std::string_view singleTexture3DShaderSource = {
} // namespace

void Material::loadType(MaterialType type) {
ZoneScopedN("Material::loadType");

switch (type) {
case MaterialType::COOK_TORRANCE:
m_program.setShaders(VertexShader::loadFromSource(vertShaderSource), FragmentShader::loadFromSource(cookTorranceShaderSource));
Expand Down
12 changes: 11 additions & 1 deletion src/RaZ/Render/MeshRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "RaZ/Render/MeshRenderer.hpp"
#include "RaZ/Utils/Logger.hpp"

#include "tracy/Tracy.hpp"

namespace Raz {

void MeshRenderer::setRenderMode(RenderMode renderMode, const Mesh& mesh) {
Expand All @@ -10,6 +12,8 @@ void MeshRenderer::setRenderMode(RenderMode renderMode, const Mesh& mesh) {
}

Material& MeshRenderer::setMaterial(Material&& material) {
ZoneScopedN("MeshRenderer::setMaterial");

m_materials.clear();

Material& newMaterial = m_materials.emplace_back(std::move(material));
Expand All @@ -26,7 +30,7 @@ Material& MeshRenderer::setMaterial(Material&& material) {
}

void MeshRenderer::removeMaterial(std::size_t materialIndex) {
assert("Error: Cannot remove a material that doesn't exist." && materialIndex < m_materials.size());
assert("Error: Cannot remove a material that does not exist." && materialIndex < m_materials.size());

m_materials.erase(m_materials.begin() + static_cast<std::ptrdiff_t>(materialIndex));

Expand Down Expand Up @@ -58,6 +62,8 @@ MeshRenderer MeshRenderer::clone() const {
}

void MeshRenderer::load(const Mesh& mesh, RenderMode renderMode) {
ZoneScopedN("MeshRenderer::load");

if (mesh.getSubmeshes().empty()) {
Logger::error("[MeshRenderer] Cannot load an empty mesh.");
return;
Expand All @@ -78,6 +84,8 @@ void MeshRenderer::load(const Mesh& mesh, RenderMode renderMode) {
}

void MeshRenderer::loadMaterials() const {
ZoneScopedN("MeshRenderer::loadMaterials");

for (const Material& material : m_materials) {
material.getProgram().sendAttributes();
material.getProgram().initTextures();
Expand All @@ -88,6 +96,8 @@ void MeshRenderer::loadMaterials() const {
}

void MeshRenderer::draw() const {
ZoneScopedN("MeshRenderer::draw");

for (const SubmeshRenderer& submeshRenderer : m_submeshRenderers) {
if (submeshRenderer.getMaterialIndex() != std::numeric_limits<std::size_t>::max()) {
assert("Error: The material index does not reference any existing material." && (submeshRenderer.getMaterialIndex() < m_materials.size()));
Expand Down
11 changes: 10 additions & 1 deletion src/RaZ/Render/Overlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
#include "RaZ/Render/Texture.hpp"
#include "RaZ/Utils/Logger.hpp"

#include "GLFW/glfw3.h"
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include "misc/cpp/imgui_stdlib.h"
#include "implot.h"

#include "tracy/Tracy.hpp"

namespace Raz {

OverlayWindow& Overlay::addWindow(std::string title, const Vec2f& initSize, const Vec2f& initPos) {
Expand All @@ -25,6 +26,8 @@ bool Overlay::hasMouseFocus() const {
}

void Overlay::render() const {
ZoneScopedN("Overlay::render");

#if !defined(USE_OPENGL_ES) && defined(RAZ_CONFIG_DEBUG)
if (Renderer::checkVersion(4, 3))
Renderer::pushDebugGroup("Overlay pass");
Expand All @@ -47,6 +50,8 @@ void Overlay::render() const {
}

void Overlay::initialize(GLFWwindow* windowHandle) {
ZoneScopedN("Overlay::initialize");

if (ImGui::GetCurrentContext() != nullptr)
return; // The overlay has already been initialized

Expand All @@ -71,6 +76,8 @@ void Overlay::initialize(GLFWwindow* windowHandle) {
}

void Overlay::destroy() {
ZoneScopedN("Overlay::destroy");

if (ImGui::GetCurrentContext() == nullptr)
return; // The overlay has already been destroyed

Expand Down Expand Up @@ -244,6 +251,8 @@ OverlayFpsCounter& OverlayWindow::addFpsCounter(std::string formattedLabel) {
}

void OverlayWindow::render() const {
ZoneScopedN("OverlayWindow::render");

if (!m_enabled)
return;

Expand Down
Loading

0 comments on commit 3bc1b22

Please sign in to comment.