opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
scene_cache.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <functional>
7#include <memory>
8#include <string>
9#include <type_traits>
10#include <typeinfo>
11
12namespace osc { class BVH; }
13namespace osc { class MeshBasicMaterial; }
14namespace osc { class ResourceLoader; }
15namespace osc { class Shader; }
16
17namespace osc
18{
19 // a persistent cache that can be used to accelerate initializing
20 // scene-related data (meshes, shaders, materials, etc.)
21 //
22 // this is usually used when rendering multiple images that are likely
23 // to share these datastructures (e.g. you'll keep this around as state
24 // across multiple frames and share it between multiple `SceneRenderer`s)
26 public:
27 // constructs the cache with a defaulted `ResourceLoader`, which will be used
28 // with any method that uses a `ResourcePath`
30
31 // constructs the cache with the provided `ResourceLoader`, which will be used
32 // with any method that uses a `ResourcePath`
33 explicit SceneCache(const ResourceLoader&);
34 SceneCache(const SceneCache&) = delete;
39
40 // clear all cached meshes (can be slow: forces a full reload)
42
43 // always returns (it will use a dummy cube and print a log error if something fails)
44 Mesh get_mesh(const std::string& key, const std::function<Mesh()>& getter);
45
57 Mesh torus_mesh(float tube_center_radius, float tube_radius);
59
61
62 // returns a `Shader` loaded via the `ResourceLoader` that was provided to the constructor
66 );
67
68 // returns a `Shader` loaded via the `ResourceLoader` that was provided to the constructor
73 );
74
75 // Returns an object with the given type via `typeid`, default-constructing it if it isn't
76 // already in the cache
77 //
78 // - `T` must be exactly the required type, not something derived from it (it's typeid-based)
79 // - If an instance of `T` doesn't already exist in this cache, it will be default-constructed
80 // and placed in the cache.
81 // - This typeid-based caching mechanism is independent of other caching methods. E.g. if some
82 // other member method of the cache returns an instance of `T` then it operates independently
83 // of this method.
87 {
88 if (const void* ptr = try_get(typeid(T))) {
89 return *static_cast<const T*>(ptr);
90 }
91
92 auto constructed = std::make_shared<T>();
93 const T& rv = *constructed;
94 insert(typeid(T), std::move(constructed));
95 return rv;
96 }
97
100
101 private:
102 const void* try_get(const std::type_info&) const;
103 void insert(const std::type_info&, std::shared_ptr<void>);
104
105 class Impl;
106 std::unique_ptr<Impl> impl_;
107 };
108}
Definition bvh.h:23
Definition mesh_basic_material.h:18
Definition mesh.h:33
Definition resource_loader.h:24
Definition resource_path.h:12
Definition scene_cache.h:25
const MeshBasicMaterial & basic_material()
Mesh cube_wireframe_mesh()
SceneCache(const SceneCache &)=delete
const BVH & get_bvh(const Mesh &)
Mesh get_mesh(const std::string &key, const std::function< Mesh()> &getter)
SceneCache(SceneCache &&) noexcept
const Shader & get_shader(const ResourcePath &vertex_shader_path, const ResourcePath &fragment_shader_path)
and std::is_destructible_v< T > const T & get()
Definition scene_cache.h:86
Mesh uncapped_cylinder_mesh()
Mesh torus_mesh(float tube_center_radius, float tube_radius)
const MeshBasicMaterial & wireframe_material()
SceneCache(const ResourceLoader &)
Mesh cylinder_mesh()
Mesh sphere_octant_mesh()
Definition shader.h:14
Definition custom_decoration_generator.h:5
constexpr U to(T &&value)
Definition conversion.h:56
and
Definition algorithms.h:158