Skip to content

Commit

Permalink
[Data] Instrumented data features for profiling purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
Razakhel committed Mar 21, 2024
1 parent 3d50f0c commit 52a47aa
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/RaZ/Data/BoundingVolumeHierarchy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "RaZ/Math/Matrix.hpp"
#include "RaZ/Math/Transform.hpp"

#include "tracy/Tracy.hpp"

namespace Raz {

namespace {
Expand All @@ -17,6 +19,9 @@ enum CutAxis {
} // namespace

Entity* BoundingVolumeHierarchyNode::query(const Ray& ray, RayHit* hit) const {
// The following call can produce way too many zones, *drastically* increasing the profiling time & memory consumption
//ZoneScopedN("BoundingVolumeHierarchyNode::query");

if (!ray.intersects(m_boundingBox, hit))
return nullptr;

Expand All @@ -42,6 +47,9 @@ Entity* BoundingVolumeHierarchyNode::query(const Ray& ray, RayHit* hit) const {
}

void BoundingVolumeHierarchyNode::build(std::vector<TriangleInfo>& trianglesInfo, std::size_t beginIndex, std::size_t endIndex) {
// The following call can produce way too many zones, *drastically* increasing the profiling time & memory consumption
//ZoneScopedN("BoundingVolumeHierarchyNode::build");

m_boundingBox = trianglesInfo[beginIndex].triangle.computeBoundingBox();

if (endIndex - beginIndex <= 1) {
Expand Down Expand Up @@ -99,6 +107,8 @@ void BoundingVolumeHierarchyNode::build(std::vector<TriangleInfo>& trianglesInfo
}

void BoundingVolumeHierarchy::build(const std::vector<Entity*>& entities) {
ZoneScopedN("BoundingVolumeHierarchy::build");

m_rootNode = BoundingVolumeHierarchyNode();

// Storing all triangles in a list to build the BVH from
Expand Down
6 changes: 6 additions & 0 deletions src/RaZ/Data/BvhFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "RaZ/Utils/FilePath.hpp"
#include "RaZ/Utils/StrUtils.hpp"

#include "tracy/Tracy.hpp"

#include <fstream>
#include <unordered_map>

Expand All @@ -11,6 +13,8 @@ namespace Raz::BvhFormat {
namespace {

void loadJoint(std::ifstream& file, std::unordered_map<std::string, SkeletonJoint&>& joints, Skeleton& skeleton, SkeletonJoint& parentJoint) {
ZoneScopedN("[BvhFormat]::loadJoint");

std::string token;
file >> token;

Expand Down Expand Up @@ -88,6 +92,8 @@ void loadJoint(std::ifstream& file, std::unordered_map<std::string, SkeletonJoin
} // namespace

Skeleton load(const FilePath& filePath) {
ZoneScopedN("BvhFormat::load");

std::ifstream file(filePath, std::ios_base::in | std::ios_base::binary);

if (!file)
Expand Down
8 changes: 8 additions & 0 deletions src/RaZ/Data/FbxLoad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
#include "RaZ/Utils/FileUtils.hpp"
#include "RaZ/Utils/Logger.hpp"

#include "tracy/Tracy.hpp"

#include <fbxsdk.h>

namespace Raz::FbxFormat {

namespace {

inline Texture2DPtr loadTexture(const FilePath& textureFilePath) {
ZoneScopedN("[FbxLoad]::loadTexture");

if (!FileUtils::isReadable(textureFilePath)) {
Logger::warn("[FbxLoad] Cannot load texture '" + textureFilePath + "'; either the file does not exist or it cannot be opened.");
return nullptr;
Expand All @@ -24,6 +28,8 @@ inline Texture2DPtr loadTexture(const FilePath& textureFilePath) {
}

void loadMaterials(fbxsdk::FbxScene* scene, std::vector<Material>& materials, const FilePath& filePath) {
ZoneScopedN("[FbxLoad]::loadMaterials");

for (int matIndex = 0; matIndex < scene->GetMaterialCount(); ++matIndex) {
const fbxsdk::FbxSurfaceMaterial* fbxMaterial = scene->GetMaterial(matIndex);
Material material;
Expand Down Expand Up @@ -121,6 +127,8 @@ void loadMaterials(fbxsdk::FbxScene* scene, std::vector<Material>& materials, co
} // namespace

std::pair<Mesh, MeshRenderer> load(const FilePath& filePath) {
ZoneScopedN("FbxFormat::load");

fbxsdk::FbxManager* manager = fbxsdk::FbxManager::Create();
manager->SetIOSettings(fbxsdk::FbxIOSettings::Create(manager, IOSROOT));

Expand Down
24 changes: 23 additions & 1 deletion src/RaZ/Data/GltfLoad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#include "RaZ/Utils/FileUtils.hpp"
#include "RaZ/Utils/Logger.hpp"

#include <fastgltf/parser.hpp>
#include "fastgltf/parser.hpp"

#include "tracy/Tracy.hpp"

namespace Raz::GltfFormat {

Expand All @@ -30,6 +32,8 @@ void computeNodeTransform(const fastgltf::Node& currentNode,
const std::optional<Transform>& parentTransform,
const std::vector<fastgltf::Node>& nodes,
std::vector<std::optional<Transform>>& transforms) {
ZoneScopedN("[GltfLoad]::computeNodeTransform");

if (!currentNode.meshIndex.has_value())
return;

Expand All @@ -56,6 +60,8 @@ void computeNodeTransform(const fastgltf::Node& currentNode,
}

std::vector<std::optional<Transform>> loadTransforms(const std::vector<fastgltf::Node>& nodes, std::size_t meshCount) {
ZoneScopedN("[GltfLoad]::loadTransforms");

std::vector<std::optional<Transform>> transforms;
transforms.resize(meshCount);

Expand All @@ -71,6 +77,8 @@ void loadVertexData(const fastgltf::Accessor& accessor,
const std::vector<fastgltf::BufferView>& bufferViews,
std::vector<Vertex>& vertices,
void (*callback)(Vertex&, const T*)) {
ZoneScopedN("[GltfLoad]::loadVertexData");

assert("Error: Loading vertex data requires the accessor to reference a buffer view." && accessor.bufferViewIndex.has_value());

const fastgltf::BufferView& bufferView = bufferViews[*accessor.bufferViewIndex];
Expand All @@ -95,6 +103,8 @@ void loadVertices(const fastgltf::Primitive& primitive,
const std::vector<fastgltf::Accessor>& accessors,
const std::optional<Transform>& transform,
Submesh& submesh) {
ZoneScopedN("[GltfLoad]::loadVertices");

Logger::debug("[GltfLoad] Loading vertices...");

const auto positionIt = primitive.findAttribute("POSITION");
Expand Down Expand Up @@ -169,6 +179,8 @@ void loadIndices(const fastgltf::Accessor& indicesAccessor,
const std::vector<fastgltf::Buffer>& buffers,
const std::vector<fastgltf::BufferView>& bufferViews,
std::vector<unsigned int>& indices) {
ZoneScopedN("[GltfLoad]::loadIndices");

Logger::debug("[GltfLoad] Loading indices...");

if (!indicesAccessor.bufferViewIndex.has_value())
Expand Down Expand Up @@ -218,6 +230,8 @@ std::pair<Mesh, MeshRenderer> loadMeshes(const std::vector<fastgltf::Mesh>& mesh
const std::vector<fastgltf::BufferView>& bufferViews,
const std::vector<fastgltf::Accessor>& accessors,
const std::vector<std::optional<Transform>>& transforms) {
ZoneScopedN("[GltfLoad]::loadMeshes");

Logger::debug("[GltfLoad] Loading " + std::to_string(meshes.size()) + " mesh(es)...");

Mesh loadedMesh;
Expand Down Expand Up @@ -249,6 +263,8 @@ std::vector<std::optional<Image>> loadImages(const std::vector<fastgltf::Image>&
const std::vector<fastgltf::Buffer>& buffers,
const std::vector<fastgltf::BufferView>& bufferViews,
const FilePath& rootFilePath) {
ZoneScopedN("[GltfLoad]::loadImages");

Logger::debug("[GltfLoad] Loading " + std::to_string(images.size()) + " image(s)...");

std::vector<std::optional<Image>> loadedImages;
Expand Down Expand Up @@ -332,6 +348,8 @@ void loadTexture(const OptionalT<TextureInfoT>& textureInfo,
const FuncT& callback) {
static_assert(std::is_base_of_v<fastgltf::TextureInfo, TextureInfoT>);

ZoneScopedN("[GltfLoad]::loadTexture");

if (!textureInfo.has_value())
return;

Expand All @@ -347,6 +365,8 @@ void loadMaterials(const std::vector<fastgltf::Material>& materials,
const std::vector<fastgltf::Texture>& textures,
const std::vector<std::optional<Image>>& images,
MeshRenderer& meshRenderer) {
ZoneScopedN("[GltfLoad]::loadMaterials");

Logger::debug("[GltfLoad] Loading " + std::to_string(materials.size()) + " material(s)...");

meshRenderer.getMaterials().clear();
Expand Down Expand Up @@ -396,6 +416,8 @@ void loadMaterials(const std::vector<fastgltf::Material>& materials,
} // namespace

std::pair<Mesh, MeshRenderer> load(const FilePath& filePath) {
ZoneScopedN("GltfFormat::load");

Logger::debug("[GltfLoad] Loading glTF file ('" + filePath + "')...");

if (!FileUtils::isReadable(filePath))
Expand Down
8 changes: 8 additions & 0 deletions src/RaZ/Data/ImageFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#define STBI_WINDOWS_UTF8
#include "stb_image.h"

#include "tracy/Tracy.hpp"

namespace Raz::ImageFormat {

namespace {
Expand Down Expand Up @@ -45,6 +47,8 @@ Image createImageFromData(int width, int height, int channelCount, bool isHdr, c
} // namespace

Image load(const FilePath& filePath, bool flipVertically) {
ZoneScopedN("ImageFormat::load");

Logger::debug("[ImageFormat] Loading image '" + filePath + "'...");

const std::string fileStr = filePath.toUtf8();
Expand Down Expand Up @@ -77,6 +81,8 @@ Image loadFromData(const std::vector<unsigned char>& imgData, bool flipVerticall
}

Image loadFromData(const unsigned char* imgData, std::size_t dataSize, bool flipVertically) {
ZoneScopedN("ImageFormat::loadFromData");

Logger::debug("[ImageFormat] Loading image from data...");

stbi_set_flip_vertically_on_load(flipVertically);
Expand Down Expand Up @@ -104,6 +110,8 @@ Image loadFromData(const unsigned char* imgData, std::size_t dataSize, bool flip
}

void save(const FilePath& filePath, const Image& image, bool flipVertically) {
ZoneScopedN("ImageFormat::save");

Logger::debug("[ImageFormat] Saving image to '" + filePath + "'...");

const std::string fileExt = StrUtils::toLowercaseCopy(filePath.recoverExtension().toUtf8());
Expand Down
20 changes: 20 additions & 0 deletions src/RaZ/Data/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
#include "RaZ/Math/Constants.hpp"
#include "RaZ/Utils/Threading.hpp"

#include "tracy/Tracy.hpp"

#include <unordered_map>

namespace Raz {

Mesh::Mesh(const Plane& plane, float width, float depth) {
ZoneScopedN("Mesh::Mesh(Plane)");

const float height = plane.computeCentroid().y();

// TODO: creating a Mesh from a Plane doesn't take the normal into account for the vertices' position
Expand Down Expand Up @@ -47,6 +51,8 @@ Mesh::Mesh(const Plane& plane, float width, float depth) {
}

Mesh::Mesh(const Sphere& sphere, uint32_t subdivCount, SphereMeshType type) {
ZoneScopedN("Mesh::Mesh(Sphere)");

if (subdivCount < 1)
throw std::invalid_argument("Error: Cannot create a sphere mesh with no subdivision");

Expand All @@ -64,6 +70,8 @@ Mesh::Mesh(const Sphere& sphere, uint32_t subdivCount, SphereMeshType type) {
}

Mesh::Mesh(const Triangle& triangle, const Vec2f& firstTexcoords, const Vec2f& secondTexcoords, const Vec2f& thirdTexcoords) {
ZoneScopedN("Mesh::Mesh(Triangle)");

const Vec3f& firstPos = triangle.getFirstPos();
const Vec3f& secondPos = triangle.getSecondPos();
const Vec3f& thirdPos = triangle.getThirdPos();
Expand Down Expand Up @@ -93,6 +101,8 @@ Mesh::Mesh(const Triangle& triangle, const Vec2f& firstTexcoords, const Vec2f& s
}

Mesh::Mesh(const Quad& quad) {
ZoneScopedN("Mesh::Mesh(Quad)");

const Vec3f& leftTopPos = quad.getLeftTopPos();
const Vec3f& rightTopPos = quad.getRightTopPos();
const Vec3f& rightBottomPos = quad.getRightBottomPos();
Expand Down Expand Up @@ -132,6 +142,8 @@ Mesh::Mesh(const Quad& quad) {
}

Mesh::Mesh(const AABB& box) {
ZoneScopedN("Mesh::Mesh(AABB)");

const auto [minX, minY, minZ] = box.getMinPosition().getData();
const auto [maxX, maxY, maxZ] = box.getMaxPosition().getData();

Expand Down Expand Up @@ -214,6 +226,8 @@ std::size_t Mesh::recoverTriangleCount() const {
}

const AABB& Mesh::computeBoundingBox() {
ZoneScopedN("Mesh::computeBoundingBox");

Vec3f minPos(std::numeric_limits<float>::max());
Vec3f maxPos(std::numeric_limits<float>::lowest());

Expand All @@ -234,6 +248,8 @@ const AABB& Mesh::computeBoundingBox() {
}

void Mesh::computeTangents() {
ZoneScopedN("Mesh::computeTangents");

if (m_submeshes.empty())
return;

Expand All @@ -246,6 +262,8 @@ void Mesh::computeTangents() {
void Mesh::createUvSphere(const Sphere& sphere, uint32_t widthCount, uint32_t heightCount) {
// Algorithm based on the standard/UV sphere presented here: http://www.songho.ca/opengl/gl_sphere.html#sphere

ZoneScopedN("Mesh::createUvSphere");

Submesh& submesh = m_submeshes.emplace_back();

std::vector<Vertex>& vertices = submesh.getVertices();
Expand Down Expand Up @@ -322,6 +340,8 @@ void Mesh::createIcosphere(const Sphere& sphere, uint32_t /* subdivCount */) {
// - http://www.songho.ca/opengl/gl_sphere.html#icosphere
// - https://gist.github.com/warmwaffles/402b9c04318d6ee6dfa4

ZoneScopedN("Mesh::createIcosphere");

const float radius = sphere.getRadius();
const float goldenRadius = radius * GoldenRatio<float>;

Expand Down
8 changes: 8 additions & 0 deletions src/RaZ/Data/MeshDistanceField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "RaZ/Utils/Ray.hpp"
#include "RaZ/Utils/Threading.hpp"

#include "tracy/Tracy.hpp"

namespace Raz {

MeshDistanceField::MeshDistanceField(const AABB& area, unsigned int width, unsigned int height, unsigned int depth)
Expand All @@ -18,6 +20,8 @@ float MeshDistanceField::getDistance(std::size_t widthIndex, std::size_t heightI
}

void MeshDistanceField::compute(std::size_t sampleCount) {
ZoneScopedN("MeshDistanceField::compute");

if (m_bvh == nullptr)
throw std::runtime_error("[MeshDistanceField] Computing a mesh distance field requires having given a BVH.");

Expand All @@ -29,6 +33,8 @@ void MeshDistanceField::compute(std::size_t sampleCount) {
const float depthStep = areaExtents.z() / static_cast<float>(m_depth - 1);

Threading::parallelize(0, m_depth, [this, widthStep, heightStep, depthStep, sampleCount] (const Threading::IndexRange& range) {
ZoneScopedN("MeshDistanceField::compute");

for (std::size_t depthIndex = range.beginIndex; depthIndex < range.endIndex; ++depthIndex) {
for (std::size_t heightIndex = 0; heightIndex < m_height; ++heightIndex) {
for (std::size_t widthIndex = 0; widthIndex < m_width; ++widthIndex) {
Expand Down Expand Up @@ -56,6 +62,8 @@ void MeshDistanceField::compute(std::size_t sampleCount) {
}

std::vector<Image> MeshDistanceField::recoverSlices() const {
ZoneScopedN("MeshDistanceField::recoverSlices");

std::vector<Image> slices;
slices.reserve(m_depth);

Expand Down
6 changes: 6 additions & 0 deletions src/RaZ/Data/MeshFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
#include "RaZ/Utils/FilePath.hpp"
#include "RaZ/Utils/StrUtils.hpp"

#include "tracy/Tracy.hpp"

namespace Raz::MeshFormat {

std::pair<Mesh, MeshRenderer> load(const FilePath& filePath) {
ZoneScopedN("MeshFormat::load");

const std::string fileExt = StrUtils::toLowercaseCopy(filePath.recoverExtension().toUtf8());

if (fileExt == "gltf" || fileExt == "glb") {
Expand All @@ -33,6 +37,8 @@ std::pair<Mesh, MeshRenderer> load(const FilePath& filePath) {
}

void save(const FilePath& filePath, const Mesh& mesh, const MeshRenderer* meshRenderer) {
ZoneScopedN("MeshFormat::save");

const std::string fileExt = StrUtils::toLowercaseCopy(filePath.recoverExtension().toUtf8());

if (fileExt == "obj")
Expand Down
Loading

0 comments on commit 52a47aa

Please sign in to comment.