Skip to content

Commit

Permalink
Use more Allocators throughout codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-van-bergen committed Feb 20, 2022
1 parent 094b969 commit a107bda
Show file tree
Hide file tree
Showing 28 changed files with 116 additions and 86 deletions.
1 change: 1 addition & 0 deletions Pathtracer.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@
<ClInclude Include="Src\Core\Allocators\Allocator.h" />
<ClInclude Include="Src\Core\Allocators\LinearAllocator.h" />
<ClInclude Include="Src\Core\Allocators\PinnedAllocator.h" />
<ClInclude Include="Src\Core\Allocators\StackAllocator.h" />
<ClInclude Include="Src\Core\Array.h" />
<ClInclude Include="Src\Core\Assertion.h" />
<ClInclude Include="Src\Core\BitArray.h" />
Expand Down
3 changes: 3 additions & 0 deletions Pathtracer.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -433,5 +433,8 @@
<ClInclude Include="Src\Core\Allocators\LinearAllocator.h">
<Filter>Core\Allocators</Filter>
</ClInclude>
<ClInclude Include="Src\Core\Allocators\StackAllocator.h">
<Filter>Core\Allocators</Filter>
</ClInclude>
</ItemGroup>
</Project>
8 changes: 4 additions & 4 deletions Src/Assets/AssetManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ struct AssetManager {

public:
template<typename FallbackLoader>
MeshDataHandle add_mesh_data(const String & filename, FallbackLoader fallback_loader) {
MeshDataHandle add_mesh_data(const String & filename, Allocator * allocator, FallbackLoader fallback_loader) {
String bvh_filename = BVHLoader::get_bvh_filename(filename.view());
MeshDataHandle mesh_data_handle = add_mesh_data(filename, bvh_filename, fallback_loader);
MeshDataHandle mesh_data_handle = add_mesh_data(filename, bvh_filename, allocator, fallback_loader);

return mesh_data_handle;
}

template<typename FallbackLoader>
MeshDataHandle add_mesh_data(const String & filename, const String & bvh_filename, FallbackLoader fallback_loader) {
MeshDataHandle add_mesh_data(const String & filename, const String & bvh_filename, Allocator * allocator, FallbackLoader fallback_loader) {
MeshDataHandle & mesh_data_handle = mesh_data_cache[filename];

if (mesh_data_handle.handle != INVALID) return mesh_data_handle;
Expand All @@ -55,7 +55,7 @@ struct AssetManager {

bool bvh_loaded = BVHLoader::try_to_load(filename, bvh_filename, mesh_data, bvh);
if (!bvh_loaded) {
mesh_data.triangles = fallback_loader(filename);
mesh_data.triangles = fallback_loader(filename, allocator);

if (mesh_data.triangles.size() == 0) {
return { INVALID };
Expand Down
4 changes: 3 additions & 1 deletion Src/Assets/BVHLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "Core/IO.h"
#include "Core/Parser.h"
#include "Core/Allocators/StackAllocator.h"

#include "Util/Util.h"
#include "Util/StringUtil.h"
Expand Down Expand Up @@ -33,7 +34,8 @@ bool BVHLoader::try_to_load(const String & filename, const String & bvh_filename
return false;
}

String file = IO::file_read(bvh_filename);
StackAllocator<> allocator;
String file = IO::file_read(bvh_filename, &allocator);
Parser parser(file.view(), bvh_filename.view());

BVHFileHeader header = parser.parse_binary<BVHFileHeader>();
Expand Down
8 changes: 4 additions & 4 deletions Src/Assets/Mitsuba/MitshairLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

#include "Renderer/Triangle.h"

Array<Triangle> MitshairLoader::load(const String & filename, SourceLocation location_in_mitsuba_file, float radius) {
String file = IO::file_read(filename);
Array<Triangle> MitshairLoader::load(const String & filename, Allocator * allocator, SourceLocation location_in_mitsuba_file, float radius) {
String file = IO::file_read(filename, allocator);

Parser parser(file.view(), filename.view());

Array<Vector3> hair_vertices;
Array<int> hair_strand_lengths;
Array<Vector3> hair_vertices (allocator);
Array<int> hair_strand_lengths(allocator);

size_t triangle_count = 0;
int strand_size = 0;
Expand Down
2 changes: 1 addition & 1 deletion Src/Assets/Mitsuba/MitshairLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
struct Triangle;

namespace MitshairLoader {
Array<Triangle> load(const String & filename, SourceLocation location_in_mitsuba_file, float radius);
Array<Triangle> load(const String & filename, Allocator * allocator, SourceLocation location_in_mitsuba_file, float radius);
}
42 changes: 21 additions & 21 deletions Src/Assets/Mitsuba/MitsubaLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,17 +431,17 @@ static MediumHandle parse_medium(const XMLNode * node, Scene & scene) {
return MediumHandle { INVALID };
}

static MeshDataHandle parse_shape(const XMLNode * node, Scene & scene, SerializedMap & serialized_map, StringView path, String & name) {
static MeshDataHandle parse_shape(const XMLNode * node, Allocator * allocator, Scene & scene, SerializedMap & serialized_map, StringView path, String & name) {
StringView type = node->get_attribute_value<StringView>("type");

if (type == "obj" || type == "ply") {
String filename = Util::combine_stringviews(path, node->get_child_value<StringView>("filename"));

MeshDataHandle mesh_data_handle;
if (type == "obj") {
mesh_data_handle = scene.asset_manager.add_mesh_data(filename, OBJLoader::load);
mesh_data_handle = scene.asset_manager.add_mesh_data(filename, allocator, OBJLoader::load);
} else {
mesh_data_handle = scene.asset_manager.add_mesh_data(filename, PLYLoader::load);
mesh_data_handle = scene.asset_manager.add_mesh_data(filename, allocator, PLYLoader::load);
}

name = Util::remove_directory(filename.view());
Expand Down Expand Up @@ -495,18 +495,18 @@ static MeshDataHandle parse_shape(const XMLNode * node, Scene & scene, Serialize

String bvh_filename = Format().format("{}.shape_{}.bvh"_sv, filename_abs, shape_index);

auto fallback_loader = [&](const String & filename) {
auto fallback_loader = [&](const String & filename, Allocator * allocator) {
Serialized serialized;
bool found = serialized_map.try_get(filename_abs, serialized);
if (!found) {
serialized = SerializedLoader::load(filename_abs, node->location);
serialized = SerializedLoader::load(filename_abs, allocator, node->location);
serialized_map[filename_rel] = serialized;
}

return std::move(serialized.meshes[shape_index]);
};

MeshDataHandle mesh_data_handle = scene.asset_manager.add_mesh_data(bvh_filename, bvh_filename, fallback_loader);
MeshDataHandle mesh_data_handle = scene.asset_manager.add_mesh_data(bvh_filename, bvh_filename, allocator, fallback_loader);

name = Format().format("{}_{}"_sv, filename_rel, shape_index);

Expand All @@ -517,10 +517,10 @@ static MeshDataHandle parse_shape(const XMLNode * node, Scene & scene, Serialize

float radius = node->get_child_value_optional("radius", 0.0025f);

auto fallback_loader = [&](const String & filename) {
return MitshairLoader::load(filename, node->location, radius);
auto fallback_loader = [&](const String & filename, Allocator * allocator) {
return MitshairLoader::load(filename, allocator, node->location, radius);
};
MeshDataHandle mesh_data_handle = scene.asset_manager.add_mesh_data(filename_abs, fallback_loader);
MeshDataHandle mesh_data_handle = scene.asset_manager.add_mesh_data(filename_abs, allocator, fallback_loader);

name = filename_rel;

Expand All @@ -531,7 +531,7 @@ static MeshDataHandle parse_shape(const XMLNode * node, Scene & scene, Serialize
}
}

static void walk_xml_tree(const XMLNode * node, Scene & scene, ShapeGroupMap & shape_group_map, SerializedMap & serialized_map, MaterialMap & material_map, TextureMap & texture_map, StringView path) {
static void walk_xml_tree(const XMLNode * node, Allocator * allocator, Scene & scene, ShapeGroupMap & shape_group_map, SerializedMap & serialized_map, MaterialMap & material_map, TextureMap & texture_map, StringView path) {
if (node->tag == "bsdf") {
MaterialHandle material_handle = parse_material(node, scene, material_map, texture_map, path);
const Material & material = scene.asset_manager.get_material(material_handle);
Expand All @@ -551,7 +551,7 @@ static void walk_xml_tree(const XMLNode * node, Scene & scene, ShapeGroupMap & s

String name = { };

MeshDataHandle mesh_data_handle = parse_shape(shape, scene, serialized_map, path, name);
MeshDataHandle mesh_data_handle = parse_shape(shape, allocator, scene, serialized_map, path, name);
MaterialHandle material_handle = parse_material(shape, scene, material_map, texture_map, path);

StringView id = node->get_attribute_value<StringView>("id");
Expand All @@ -573,7 +573,7 @@ static void walk_xml_tree(const XMLNode * node, Scene & scene, ShapeGroupMap & s
} else {
String name = { };

MeshDataHandle mesh_data_handle = parse_shape(node, scene, serialized_map, path, name);
MeshDataHandle mesh_data_handle = parse_shape(node, allocator, scene, serialized_map, path, name);
MaterialHandle material_handle = parse_material(node, scene, material_map, texture_map, path);
MediumHandle medium_handle = parse_medium(node, scene);

Expand Down Expand Up @@ -667,14 +667,14 @@ static void walk_xml_tree(const XMLNode * node, Scene & scene, ShapeGroupMap & s
StringView filename_rel = node->get_attribute_value<StringView>("filename");
String filename_abs = Util::combine_stringviews(path, filename_rel);

MitsubaLoader::load(filename_abs, scene);
MitsubaLoader::load(filename_abs, allocator, scene);
} else for (int i = 0; i < node->children.size(); i++) {
walk_xml_tree(&node->children[i], scene, shape_group_map, serialized_map, material_map, texture_map, path);
walk_xml_tree(&node->children[i], allocator, scene, shape_group_map, serialized_map, material_map, texture_map, path);
}
}

void MitsubaLoader::load(const String & filename, Scene & scene) {
XMLParser xml_parser(filename);
void MitsubaLoader::load(const String & filename, Allocator * allocator, Scene & scene) {
XMLParser xml_parser(filename, allocator);

XMLNode root = xml_parser.parse_root();

Expand All @@ -697,9 +697,9 @@ void MitsubaLoader::load(const String & filename, Scene & scene) {
}
}

ShapeGroupMap shape_group_map;
SerializedMap serialized_map;
MaterialMap material_map;
TextureMap texture_map;
walk_xml_tree(scene_node, scene, shape_group_map, serialized_map, material_map, texture_map, Util::get_directory(filename.view()));
ShapeGroupMap shape_group_map(allocator);
SerializedMap serialized_map (allocator);
MaterialMap material_map (allocator);
TextureMap texture_map (allocator);
walk_xml_tree(scene_node, allocator, scene, shape_group_map, serialized_map, material_map, texture_map, Util::get_directory(filename.view()));
}
2 changes: 1 addition & 1 deletion Src/Assets/Mitsuba/MitsubaLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
struct Scene;

namespace MitsubaLoader {
void load(const String & filename, Scene & scene);
void load(const String & filename, Allocator * allocator, Scene & scene);
}
6 changes: 3 additions & 3 deletions Src/Assets/Mitsuba/SerializedLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

#include "XMLParser.h"

Serialized SerializedLoader::load(const String & filename, SourceLocation location_in_mitsuba_file) {
String serialized = IO::file_read(filename);
Serialized SerializedLoader::load(const String & filename, Allocator * allocator, SourceLocation location_in_mitsuba_file) {
String serialized = IO::file_read(filename, allocator);
Parser parser(serialized.view(), filename.view());

uint16_t file_format_id = parser.parse_binary<uint16_t>();
Expand All @@ -22,7 +22,7 @@ Serialized SerializedLoader::load(const String & filename, SourceLocation locati
uint32_t num_meshes = parser.parse_binary<uint32_t>();
uint64_t eof_dictionary_offset = 0;;

Array<uint64_t> mesh_offsets(num_meshes + 1);
Array<uint64_t> mesh_offsets(num_meshes + 1, allocator);

if (file_version <= 3) {
// Version 0.3.0 and earlier use 32 bit mesh offsets
Expand Down
2 changes: 1 addition & 1 deletion Src/Assets/Mitsuba/SerializedLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ struct Serialized {
};

namespace SerializedLoader {
Serialized load(const String & filename, SourceLocation location_in_mitsuba_file);
Serialized load(const String & filename, Allocator * allocator, SourceLocation location_in_mitsuba_file);
}
4 changes: 2 additions & 2 deletions Src/Assets/Mitsuba/XMLParser.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "XMLParser.h"

XMLNode XMLParser::parse_root() {
XMLNode root = XMLNode(&allocator);
XMLNode root = XMLNode(allocator);
root.location = parser.location;

while (!parser.reached_end()) {
Expand All @@ -14,7 +14,7 @@ XMLNode XMLParser::parse_root() {
}

XMLNode XMLParser::parse_tag() {
XMLNode node = XMLNode(&allocator);
XMLNode node = XMLNode(allocator);
if (parser.reached_end()) {
return node;
}
Expand Down
6 changes: 3 additions & 3 deletions Src/Assets/Mitsuba/XMLParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@ struct XMLNode {
};

struct XMLParser {
Allocator * allocator = nullptr;

String source;
Parser parser;

LinearAllocator<GIGABYTE(1)> allocator;

XMLParser(const String & filename) : source(IO::file_read(filename)), parser(source.view(), filename.view()) { }
XMLParser(const String & filename, Allocator * allocator) : allocator(allocator), source(IO::file_read(filename, allocator)), parser(source.view(), filename.view()) { }

XMLNode parse_root();

Expand Down
17 changes: 12 additions & 5 deletions Src/Assets/OBJLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,19 @@ struct OBJFile {
Array<Vector3> normals;

Array<Face> faces;

OBJFile(Allocator * allocator = nullptr) : positions(allocator), tex_coords(allocator), normals(allocator), faces(allocator) { }

DEFAULT_COPYABLE(OBJFile);
DEFAULT_MOVEABLE(OBJFile);

~OBJFile() { }
};

static OBJFile parse_obj(const String & filename) {
String file = IO::file_read(filename);
static OBJFile parse_obj(const String & filename, Allocator * allocator) {
String file = IO::file_read(filename, allocator);

OBJFile obj = { };
OBJFile obj = OBJFile(allocator);

Parser parser(file.view(), filename.view());

Expand Down Expand Up @@ -140,8 +147,8 @@ static OBJFile parse_obj(const String & filename) {
return obj;
}

Array<Triangle> OBJLoader::load(const String & filename) {
OBJFile obj = parse_obj(filename);
Array<Triangle> OBJLoader::load(const String & filename, Allocator * allocator) {
OBJFile obj = parse_obj(filename, allocator);

Array<Triangle> triangles(obj.faces.size());

Expand Down
2 changes: 1 addition & 1 deletion Src/Assets/OBJLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
#include "Core/String.h"

namespace OBJLoader {
Array<Triangle> load(const String & filename);
Array<Triangle> load(const String & filename, Allocator * allocator);
}
4 changes: 2 additions & 2 deletions Src/Assets/PLYLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ static T parse_property_value(Parser & parser, Property::Type::Kind kind, PLYFor
}
}

Array<Triangle> PLYLoader::load(const String & filename) {
String file = IO::file_read(filename);
Array<Triangle> PLYLoader::load(const String & filename, Allocator * allocator) {
String file = IO::file_read(filename, allocator);

Parser parser(file.view(), filename.view());

Expand Down
2 changes: 1 addition & 1 deletion Src/Assets/PLYLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
#include "Core/String.h"

namespace PLYLoader {
Array<Triangle> load(const String & filename);
Array<Triangle> load(const String & filename, Allocator * allocator);
}
4 changes: 3 additions & 1 deletion Src/Assets/TextureLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
#include "Config.h"

#include "Core/Parser.h"
#include "Core/Allocators/StackAllocator.h"

#include "Math/Mipmap.h"
#include "Util/Util.h"

bool TextureLoader::load_dds(const String & filename, Texture & texture) {
String file = IO::file_read(filename);
StackAllocator<> allocator;
String file = IO::file_read(filename, &allocator);
Parser parser(file.view(), filename.view());

// Based on: https://docs.microsoft.com/en-us/windows/win32/direct3ddds/dds-header
Expand Down
6 changes: 3 additions & 3 deletions Src/Core/Allocators/Allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

#include "Util/Util.h"

#define KILOBYTE(n) (n * 1024)
#define MEGABYTE(n) (n * 1024 * 1024)
#define GIGABYTE(n) (n * 1024 * 1024 * 1024)
#define KILOBYTES(n) ((n) * 1024)
#define MEGABYTES(n) ((n) * 1024 * 1024)
#define GIGABYTES(n) ((n) * 1024 * 1024 * 1024)

struct Allocator {
Allocator() = default;
Expand Down
9 changes: 8 additions & 1 deletion Src/Core/Allocators/LinearAllocator.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include "Allocator.h"

template<size_t Size = MEGABYTE(4)>
template<size_t Size = MEGABYTES(4)>
struct LinearAllocator final : Allocator {
char * data = nullptr;
size_t offset = 0;
Expand All @@ -22,6 +22,13 @@ struct LinearAllocator final : Allocator {
}
}

void reset() {
offset = 0;
if (next) {
next->reset();
}
}

private:
char * alloc(size_t num_bytes) override {
if (num_bytes >= Size) {
Expand Down
2 changes: 1 addition & 1 deletion Src/Core/Allocators/StackAllocator.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include "Allocator.h"

template<size_t Size = KILOBYTE(4)>
template<size_t Size = KILOBYTES(4)>
struct StackAllocator final : Allocator {
Allocator * fallback_allocator = nullptr;

Expand Down
Loading

0 comments on commit a107bda

Please sign in to comment.